fix: make property-assessor safer for whatsapp runs
This commit is contained in:
37
skills/property-assessor/src/async-timeout.ts
Normal file
37
skills/property-assessor/src/async-timeout.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
export class TimeoutError extends Error {
|
||||
readonly timeoutMs: number;
|
||||
|
||||
constructor(operationName: string, timeoutMs: number) {
|
||||
super(`${operationName} timed out after ${timeoutMs}ms`);
|
||||
this.name = "TimeoutError";
|
||||
this.timeoutMs = timeoutMs;
|
||||
}
|
||||
}
|
||||
|
||||
export async function withTimeout<T>(
|
||||
operation: () => Promise<T>,
|
||||
{
|
||||
operationName,
|
||||
timeoutMs
|
||||
}: {
|
||||
operationName: string;
|
||||
timeoutMs: number;
|
||||
}
|
||||
): Promise<T> {
|
||||
let timer: NodeJS.Timeout | undefined;
|
||||
|
||||
try {
|
||||
return await Promise.race([
|
||||
operation(),
|
||||
new Promise<T>((_, reject) => {
|
||||
timer = setTimeout(() => {
|
||||
reject(new TimeoutError(operationName, timeoutMs));
|
||||
}, timeoutMs);
|
||||
})
|
||||
]);
|
||||
} finally {
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { discoverHarListing } from "../../web-automation/scripts/har-discover.js";
|
||||
import { discoverZillowListing } from "../../web-automation/scripts/zillow-discover.js";
|
||||
import { TimeoutError, withTimeout } from "./async-timeout.js";
|
||||
|
||||
export interface ListingDiscoveryResult {
|
||||
attempts: string[];
|
||||
@@ -7,29 +8,65 @@ export interface ListingDiscoveryResult {
|
||||
harUrl: string | null;
|
||||
}
|
||||
|
||||
export async function discoverListingSources(address: string): Promise<ListingDiscoveryResult> {
|
||||
interface ListingDiscoveryDeps {
|
||||
timeoutMs?: number;
|
||||
discoverZillowListingFn?: typeof discoverZillowListing;
|
||||
discoverHarListingFn?: typeof discoverHarListing;
|
||||
}
|
||||
|
||||
const DEFAULT_DISCOVERY_TIMEOUT_MS = Number(
|
||||
process.env.PROPERTY_ASSESSOR_DISCOVERY_TIMEOUT_MS || 20_000
|
||||
);
|
||||
|
||||
export async function discoverListingSources(
|
||||
address: string,
|
||||
deps: ListingDiscoveryDeps = {}
|
||||
): Promise<ListingDiscoveryResult> {
|
||||
const attempts: string[] = [];
|
||||
let zillowUrl: string | null = null;
|
||||
let harUrl: string | null = null;
|
||||
const timeoutMs = deps.timeoutMs ?? DEFAULT_DISCOVERY_TIMEOUT_MS;
|
||||
const discoverZillowListingFn = deps.discoverZillowListingFn || discoverZillowListing;
|
||||
const discoverHarListingFn = deps.discoverHarListingFn || discoverHarListing;
|
||||
|
||||
try {
|
||||
const result = await discoverZillowListing(address);
|
||||
const result = await withTimeout(
|
||||
() => discoverZillowListingFn(address),
|
||||
{
|
||||
operationName: "Zillow discovery",
|
||||
timeoutMs
|
||||
}
|
||||
);
|
||||
zillowUrl = result.listingUrl;
|
||||
attempts.push(...result.attempts);
|
||||
} catch (error) {
|
||||
attempts.push(
|
||||
`Zillow discovery failed: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
if (error instanceof TimeoutError) {
|
||||
attempts.push(`Zillow discovery timed out after ${timeoutMs}ms.`);
|
||||
} else {
|
||||
attempts.push(
|
||||
`Zillow discovery failed: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await discoverHarListing(address);
|
||||
const result = await withTimeout(
|
||||
() => discoverHarListingFn(address),
|
||||
{
|
||||
operationName: "HAR discovery",
|
||||
timeoutMs
|
||||
}
|
||||
);
|
||||
harUrl = result.listingUrl;
|
||||
attempts.push(...result.attempts);
|
||||
} catch (error) {
|
||||
attempts.push(
|
||||
`HAR discovery failed: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
if (error instanceof TimeoutError) {
|
||||
attempts.push(`HAR discovery timed out after ${timeoutMs}ms.`);
|
||||
} else {
|
||||
attempts.push(
|
||||
`HAR discovery failed: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { extractHarPhotos } from "../../web-automation/scripts/har-photos.js";
|
||||
import { extractZillowPhotos } from "../../web-automation/scripts/zillow-photos.js";
|
||||
import { withTimeout } from "./async-timeout.js";
|
||||
|
||||
export type PhotoSource = "zillow" | "har";
|
||||
|
||||
@@ -19,12 +20,33 @@ export interface PhotoReviewResolution {
|
||||
discoveredListingUrls: Array<{ label: string; url: string }>;
|
||||
}
|
||||
|
||||
interface PhotoReviewDeps {
|
||||
timeoutMs?: number;
|
||||
extractZillowPhotosFn?: typeof extractZillowPhotos;
|
||||
extractHarPhotosFn?: typeof extractHarPhotos;
|
||||
}
|
||||
|
||||
const DEFAULT_PHOTO_EXTRACTION_TIMEOUT_MS = Number(
|
||||
process.env.PROPERTY_ASSESSOR_PHOTO_TIMEOUT_MS || 25_000
|
||||
);
|
||||
|
||||
export async function extractPhotoData(
|
||||
source: PhotoSource,
|
||||
url: string
|
||||
url: string,
|
||||
deps: PhotoReviewDeps = {}
|
||||
): Promise<PhotoExtractionResult> {
|
||||
const timeoutMs = deps.timeoutMs ?? DEFAULT_PHOTO_EXTRACTION_TIMEOUT_MS;
|
||||
const extractZillowPhotosFn = deps.extractZillowPhotosFn || extractZillowPhotos;
|
||||
const extractHarPhotosFn = deps.extractHarPhotosFn || extractHarPhotos;
|
||||
|
||||
if (source === "zillow") {
|
||||
const payload = await extractZillowPhotos(url);
|
||||
const payload = await withTimeout(
|
||||
() => extractZillowPhotosFn(url),
|
||||
{
|
||||
operationName: "Zillow photo extraction",
|
||||
timeoutMs
|
||||
}
|
||||
);
|
||||
return {
|
||||
source,
|
||||
requestedUrl: String(payload.requestedUrl || url),
|
||||
@@ -37,7 +59,13 @@ export async function extractPhotoData(
|
||||
};
|
||||
}
|
||||
|
||||
const payload = await extractHarPhotos(url);
|
||||
const payload = await withTimeout(
|
||||
() => extractHarPhotosFn(url),
|
||||
{
|
||||
operationName: "HAR photo extraction",
|
||||
timeoutMs
|
||||
}
|
||||
);
|
||||
return {
|
||||
source,
|
||||
requestedUrl: String(payload.requestedUrl || url),
|
||||
|
||||
Reference in New Issue
Block a user