fix(browser): accept targetId prefixes

This commit is contained in:
Peter Steinberger
2025-12-13 18:16:47 +00:00
parent 2a71c20ee4
commit 238afbc2f8
3 changed files with 97 additions and 4 deletions

24
src/browser/target-id.ts Normal file
View File

@@ -0,0 +1,24 @@
export type TargetIdResolution =
| { ok: true; targetId: string }
| { ok: false; reason: "not_found" | "ambiguous"; matches?: string[] };
export function resolveTargetIdFromTabs(
input: string,
tabs: Array<{ targetId: string }>,
): TargetIdResolution {
const needle = input.trim();
if (!needle) return { ok: false, reason: "not_found" };
const exact = tabs.find((t) => t.targetId === needle);
if (exact) return { ok: true, targetId: exact.targetId };
const lower = needle.toLowerCase();
const matches = tabs
.map((t) => t.targetId)
.filter((id) => id.toLowerCase().startsWith(lower));
const only = matches.length === 1 ? matches[0] : undefined;
if (only) return { ok: true, targetId: only };
if (matches.length === 0) return { ok: false, reason: "not_found" };
return { ok: false, reason: "ambiguous", matches };
}