Skip to content

Commit 6d6c7aa

Browse files
committed
inject URLs for primitive array schema items
1 parent 0791404 commit 6d6c7aa

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

.changeset/twelve-suits-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": patch
3+
---
4+
5+
fix: url extraction not working inside an array

lib/utils.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,29 @@ export function injectUrls(
382382
idToUrlMapping: Record<string, string>,
383383
): void {
384384
if (path.length === 0) return;
385+
const toId = (value: unknown): string | undefined => {
386+
if (typeof value === "number") {
387+
return String(value);
388+
}
389+
if (typeof value === "string" && ID_PATTERN.test(value)) {
390+
return value;
391+
}
392+
return undefined;
393+
};
385394
const [key, ...rest] = path;
386395

387396
if (key === "*") {
388397
if (Array.isArray(obj)) {
389-
for (const item of obj) injectUrls(item, rest, idToUrlMapping);
398+
if (rest.length === 0) {
399+
for (let i = 0; i < obj.length; i += 1) {
400+
const id = toId(obj[i]);
401+
if (id !== undefined) {
402+
obj[i] = idToUrlMapping[id] ?? "";
403+
}
404+
}
405+
} else {
406+
for (const item of obj) injectUrls(item, rest, idToUrlMapping);
407+
}
390408
}
391409
return;
392410
}
@@ -395,14 +413,7 @@ export function injectUrls(
395413
const record = obj as Record<string | number, unknown>;
396414
if (path.length === 1) {
397415
const fieldValue = record[key];
398-
399-
const id =
400-
typeof fieldValue === "number"
401-
? String(fieldValue)
402-
: typeof fieldValue === "string" && ID_PATTERN.test(fieldValue)
403-
? fieldValue
404-
: undefined;
405-
416+
const id = toId(fieldValue);
406417
if (id !== undefined) {
407418
record[key] = idToUrlMapping[id] ?? "";
408419
}

0 commit comments

Comments
 (0)