@ractive-ch/tab-mcp-client 0.21.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,10 +2,10 @@
2
2
  "version": 3,
3
3
  "sources": ["../src/handshake.ts", "../src/webmcp.ts"],
4
4
  "sourcesContent": [
5
- "/**\n * The page's side of the handshake with the tab-mcp browser extension\n * (DEC-207), in the WebMCP mirror's chunk: only a page that offers its tools\n * to agents in the browser (`webmcp: true`) takes part.\n *\n * Plain DOM events on `document`, each with a JSON string as `detail`\n * (objects do not cross between a content script's world and the page's in\n * every browser): `tab-mcp-page` from the page, `tab-mcp-extension` from\n * the extension. Every message has `v: 1` and a `type`; a message of\n * another version, an unknown type or a malformed one is ignored, and so\n * are unknown fields.\n *\n * - The page announces itself (`hello`: the library's version, the app id,\n * the AI apps connected to it directly) when it starts, again (`update`)\n * when those apps change, and answers the extension's `hello` with an\n * `update`. It marks `<html data-tab-mcp-handshake>`, so that an\n * extension that starts later (its content script comes at\n * `document_idle`, or with a click) knows it may speak without having\n * heard the `hello`.\n * - The extension says what it is, whether it serves the page now and\n * which AI apps are connected to it (`hello`, `update`), and, right before\n * it runs a page tool the user allowed there, `vouch`: the tool, the exact\n * arguments, a nonce and the AI app; once that call ended or failed,\n * `withdraw` with the nonce, and a vouch still unused is dropped.\n * - Each side cuts the other's list of AI apps to 64 and sends no more.\n *\n * No proof of who sent what: anything that could fake the extension runs in\n * the page already, where it could skip any check. A convenience between\n * two parts the user trusts, not a security boundary.\n */\n\nimport type { RegisteredTool, ToolRegistry } from \"./registry.ts\";\nimport type { BrowserExtension, Grant } from \"./types.ts\";\n\n/** From the page to the extension. */\nexport const PAGE_EVENT = \"tab-mcp-page\";\n/** From the extension to the page. */\nexport const EXTENSION_EVENT = \"tab-mcp-extension\";\n/** On `<html>`: this page takes part (for an extension that starts later). */\nexport const MARK = \"data-tab-mcp-handshake\";\n/** A vouch is kept this long (milliseconds), then dropped unused. */\nexport const VOUCH_MS = 2_000;\n/** At most this many vouches wait; beyond, the oldest goes. */\nconst MAX_VOUCHES = 16;\n/** Texts from the extension are taken up to these lengths, apps up to this count either way. */\nconst MAX_TEXT = 256;\nconst MAX_APPS = 64;\n\n/** One `TabMcp` of the page, as the handshake sees it. */\nexport interface HandshakePage {\n\t/** The library's version. */\n\treadonly version: string;\n\treadonly app: string;\n\treadonly registry: ToolRegistry;\n\t/** Vouches are taken for its tools (`trustExtension`). */\n\treadonly trust: boolean;\n\t/** The `TabMcp`: its `grants` and `grantschange`. */\n\treadonly mcp: EventTarget & { readonly grants: readonly Grant[] };\n\t/** The extension's state changed. */\n\tset(extension: BrowserExtension | undefined): void;\n}\n\n/** The AI app a vouched call came from. */\nexport interface VouchedBy {\n\tname: string;\n\thost: string;\n}\n\ninterface Vouch {\n\ttool: RegisteredTool;\n\t/** The arguments, canonical (`canonical`). */\n\targs: string;\n\tnonce: string;\n\tapp: VouchedBy;\n\tat: number;\n}\n\nconst isObject = (value: unknown): value is Record<string, unknown> =>\n\ttypeof value === \"object\" && value !== null && !Array.isArray(value);\n\nconst text = (value: unknown): value is string =>\n\ttypeof value === \"string\" && value.length <= MAX_TEXT;\n\n/** An AI app as the extension names it; `undefined` if it is malformed. */\nfunction appOf(value: unknown): VouchedBy | undefined {\n\treturn isObject(value) && text(value[\"name\"]) && text(value[\"host\"])\n\t\t? { name: value[\"name\"], host: value[\"host\"] }\n\t\t: undefined;\n}\n\n/**\n * `value` as JSON with the keys of every object sorted: two argument\n * objects are equal when their canonical forms are.\n */\nexport function canonical(value: unknown): string {\n\treturn (\n\t\tJSON.stringify(value, (_key, v: unknown) =>\n\t\t\tisObject(v)\n\t\t\t\t? Object.fromEntries(\n\t\t\t\t\t\tObject.entries(v).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)),\n\t\t\t\t\t)\n\t\t\t\t: v,\n\t\t) ?? \"\"\n\t);\n}\n\n/** Monotonic milliseconds: a vouch's age must not follow the wall clock. */\nconst now = () => performance.now();\n\nclass Handshake {\n\treadonly #doc: Document;\n\treadonly #pages: HandshakePage[] = [];\n\t#extension: BrowserExtension | undefined;\n\t#vouches: Vouch[] = [];\n\n\tconstructor(doc: Document) {\n\t\tthis.#doc = doc;\n\t\tdoc.addEventListener(EXTENSION_EVENT, (event) =>\n\t\t\tthis.#heard((event as CustomEvent).detail),\n\t\t);\n\t\tdoc.documentElement?.setAttribute(MARK, \"1\");\n\t}\n\n\tjoin(page: HandshakePage): void {\n\t\tthis.#pages.push(page);\n\t\tif (this.#extension !== undefined) page.set(this.#extension);\n\t\tpage.mcp.addEventListener(\"grantschange\", () => this.#say(page, \"update\"));\n\t\tthis.#say(page, \"hello\");\n\t}\n\n\t#say(page: HandshakePage, type: \"hello\" | \"update\"): void {\n\t\t// The document's own `CustomEvent`: a document may take only events\n\t\t// of its realm.\n\t\tconst Custom = this.#doc.defaultView?.CustomEvent ?? CustomEvent;\n\t\tthis.#doc.dispatchEvent(\n\t\t\tnew Custom(PAGE_EVENT, {\n\t\t\t\tdetail: JSON.stringify({\n\t\t\t\t\tv: 1,\n\t\t\t\t\ttype,\n\t\t\t\t\tlibrary: page.version,\n\t\t\t\t\tapp: page.app,\n\t\t\t\t\t// Capped as the extension cuts what it hears: more would\n\t\t\t\t\t// not be heard anyway.\n\t\t\t\t\tapps: page.mcp.grants.slice(0, MAX_APPS).map((g) => ({\n\t\t\t\t\t\tname: g.client.name,\n\t\t\t\t\t\thost: g.client.host,\n\t\t\t\t\t})),\n\t\t\t\t}),\n\t\t\t}),\n\t\t);\n\t}\n\n\t#heard(detail: unknown): void {\n\t\tlet m: unknown;\n\t\ttry {\n\t\t\tm = typeof detail === \"string\" ? JSON.parse(detail) : undefined;\n\t\t} catch {\n\t\t\treturn;\n\t\t}\n\t\tif (!isObject(m) || m[\"v\"] !== 1) return;\n\t\tif (m[\"type\"] === \"vouch\") this.#vouch(m);\n\t\t// The call it was for ended or never reached the page: an unused vouch\n\t\t// must not wait its 2 s for another caller.\n\t\telse if (m[\"type\"] === \"withdraw\")\n\t\t\tthis.#vouches = this.#vouches.filter((v) => v.nonce !== m[\"nonce\"]);\n\t\telse if (m[\"type\"] === \"hello\" || m[\"type\"] === \"update\") {\n\t\t\tconst extension = extensionOf(m);\n\t\t\tif (extension === null) return;\n\t\t\tif (canonical(extension) !== canonical(this.#extension)) {\n\t\t\t\tthis.#extension = extension;\n\t\t\t\tfor (const page of this.#pages) page.set(extension);\n\t\t\t}\n\t\t\tif (m[\"type\"] === \"hello\")\n\t\t\t\tfor (const page of this.#pages) this.#say(page, \"update\");\n\t\t}\n\t}\n\n\t/**\n\t * A vouch, kept for the tool registered under its name now, by a page\n\t * that takes vouches: a tool registered anew after this does not get it.\n\t */\n\t#vouch(m: Record<string, unknown>): void {\n\t\tconst app = appOf(m[\"app\"]);\n\t\tconst name = m[\"tool\"];\n\t\tconst nonce = m[\"nonce\"];\n\t\tif (\n\t\t\tapp === undefined ||\n\t\t\ttypeof name !== \"string\" ||\n\t\t\ttypeof nonce !== \"string\" ||\n\t\t\t!isObject(m[\"arguments\"])\n\t\t)\n\t\t\treturn;\n\t\tthis.#drop();\n\t\t// The same vouch heard twice counts once.\n\t\tif (this.#vouches.some((v) => v.nonce === nonce)) return;\n\t\tfor (const page of this.#pages) {\n\t\t\tconst tool = page.trust ? page.registry.get(name) : undefined;\n\t\t\tif (tool === undefined) continue;\n\t\t\tthis.#vouches.push({\n\t\t\t\ttool,\n\t\t\t\targs: canonical(m[\"arguments\"]),\n\t\t\t\tnonce,\n\t\t\t\tapp,\n\t\t\t\tat: now(),\n\t\t\t});\n\t\t\tif (this.#vouches.length > MAX_VOUCHES) this.#vouches.shift();\n\t\t\treturn;\n\t\t}\n\t}\n\n\t/** Forget the vouches older than `VOUCH_MS`. */\n\t#drop(): void {\n\t\tconst t = now();\n\t\tthis.#vouches = this.#vouches.filter((v) => t - v.at <= VOUCH_MS);\n\t}\n\n\t/**\n\t * The vouch for this call of `tool` with `input`, used up; `undefined`\n\t * if there is none (the call is asked as without the extension).\n\t */\n\ttake(tool: RegisteredTool, input: unknown): VouchedBy | undefined {\n\t\tthis.#drop();\n\t\tconst args = canonical(input);\n\t\tconst i = this.#vouches.findIndex(\n\t\t\t(v) => v.tool === tool && v.args === args,\n\t\t);\n\t\treturn i < 0 ? undefined : this.#vouches.splice(i, 1)[0]?.app;\n\t}\n}\n\n/**\n * The extension's state from its `hello` or `update`: `undefined` when it\n * does not serve the page now, `null` when the message is malformed.\n */\nfunction extensionOf(\n\tm: Record<string, unknown>,\n): BrowserExtension | undefined | null {\n\tif (m[\"on\"] !== true) return m[\"on\"] === false ? undefined : null;\n\tconst { name, version, apps } = m;\n\tif (!text(name) || !text(version) || !Array.isArray(apps)) return null;\n\tconst list = apps.slice(0, MAX_APPS).map(appOf);\n\tif (list.some((a) => a === undefined)) return null;\n\treturn Object.freeze({\n\t\tname,\n\t\tversion,\n\t\tapps: Object.freeze(list.map((a) => Object.freeze(a as VouchedBy))),\n\t\twhileInFront: m[\"whileInFront\"] !== false,\n\t});\n}\n\nconst handshakes = new WeakMap<Document, Handshake>();\n\n/**\n * The handshake of the page's top-level document, which `page` joins; the\n * extension speaks only to the top frame. `undefined` in a frame or\n * without a document.\n */\nexport function joinHandshake(page: HandshakePage): Handshake | undefined {\n\tconst doc = globalThis.document;\n\tif (doc === undefined || globalThis.top !== globalThis.window)\n\t\treturn undefined;\n\tlet handshake = handshakes.get(doc);\n\tif (handshake === undefined) {\n\t\thandshake = new Handshake(doc);\n\t\thandshakes.set(doc, handshake);\n\t}\n\thandshake.join(page);\n\treturn handshake;\n}\n",
5
+ "/**\n * The page's side of the handshake with the tab-mcp browser extension\n * (DEC-207), in the WebMCP mirror's chunk: only a page that offers its tools\n * to agents in the browser (`webmcp: true`) takes part.\n *\n * Plain DOM events on `document`, each with a JSON string as `detail`\n * (objects do not cross between a content script's world and the page's in\n * every browser): `tab-mcp-page` from the page, `tab-mcp-extension` from\n * the extension. Every message has `v: 1` and a `type`; a message of\n * another version, an unknown type or a malformed one is ignored, and so\n * are unknown fields.\n *\n * - The page announces itself (`hello`: the library's version, the app id,\n * the AI apps connected to it directly) when it starts, again (`update`)\n * when those apps change, and answers the extension's `hello` with an\n * `update`. It marks `<html data-tab-mcp-handshake>`, so that an\n * extension that starts later (its content script comes at\n * `document_idle`, or with a click) knows it may speak without having\n * heard the `hello`.\n * - The extension says what it is and which AI apps are connected to it\n * (`hello`, `update`), and, right before it runs a page tool the user\n * allowed there, `vouch`: the tool, the exact arguments, a nonce and the\n * AI app; once that call ended or failed, `withdraw` with the nonce, and\n * a vouch still unused is dropped. It speaks only while it serves the\n * page (DEC-221): while its AI access is off the page hears nothing, as\n * without the extension, except `on: false` once if it was told \"on\".\n * - Each side cuts the other's list of AI apps to 64 and sends no more.\n *\n * No proof of who sent what: anything that could fake the extension runs in\n * the page already, where it could skip any check. A convenience between\n * two parts the user trusts, not a security boundary.\n */\n\nimport type { RegisteredTool, ToolRegistry } from \"./registry.ts\";\nimport type { BrowserExtension, Grant } from \"./types.ts\";\n\n/** From the page to the extension. */\nexport const PAGE_EVENT = \"tab-mcp-page\";\n/** From the extension to the page. */\nexport const EXTENSION_EVENT = \"tab-mcp-extension\";\n/** On `<html>`: this page takes part (for an extension that starts later). */\nexport const MARK = \"data-tab-mcp-handshake\";\n/** A vouch is kept this long (milliseconds), then dropped unused. */\nexport const VOUCH_MS = 2_000;\n/** At most this many vouches wait; beyond, the oldest goes. */\nconst MAX_VOUCHES = 16;\n/** Texts from the extension are taken up to these lengths, apps up to this count either way. */\nconst MAX_TEXT = 256;\nconst MAX_APPS = 64;\n\n/** One `TabMcp` of the page, as the handshake sees it. */\nexport interface HandshakePage {\n\t/** The library's version. */\n\treadonly version: string;\n\treadonly app: string;\n\treadonly registry: ToolRegistry;\n\t/** Vouches are taken for its tools (`trustExtension`). */\n\treadonly trust: boolean;\n\t/** The `TabMcp`: its `grants` and `grantschange`. */\n\treadonly mcp: EventTarget & { readonly grants: readonly Grant[] };\n\t/** The extension's state changed. */\n\tset(extension: BrowserExtension | undefined): void;\n}\n\n/** The AI app a vouched call came from. */\nexport interface VouchedBy {\n\tname: string;\n\thost: string;\n}\n\ninterface Vouch {\n\ttool: RegisteredTool;\n\t/** The arguments, canonical (`canonical`). */\n\targs: string;\n\tnonce: string;\n\tapp: VouchedBy;\n\tat: number;\n}\n\nconst isObject = (value: unknown): value is Record<string, unknown> =>\n\ttypeof value === \"object\" && value !== null && !Array.isArray(value);\n\nconst text = (value: unknown): value is string =>\n\ttypeof value === \"string\" && value.length <= MAX_TEXT;\n\n/** An AI app as the extension names it; `undefined` if it is malformed. */\nfunction appOf(value: unknown): VouchedBy | undefined {\n\treturn isObject(value) && text(value[\"name\"]) && text(value[\"host\"])\n\t\t? { name: value[\"name\"], host: value[\"host\"] }\n\t\t: undefined;\n}\n\n/**\n * `value` as JSON with the keys of every object sorted: two argument\n * objects are equal when their canonical forms are.\n */\nexport function canonical(value: unknown): string {\n\treturn (\n\t\tJSON.stringify(value, (_key, v: unknown) =>\n\t\t\tisObject(v)\n\t\t\t\t? Object.fromEntries(\n\t\t\t\t\t\tObject.entries(v).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)),\n\t\t\t\t\t)\n\t\t\t\t: v,\n\t\t) ?? \"\"\n\t);\n}\n\n/** Monotonic milliseconds: a vouch's age must not follow the wall clock. */\nconst now = () => performance.now();\n\nclass Handshake {\n\treadonly #doc: Document;\n\treadonly #pages: HandshakePage[] = [];\n\t#extension: BrowserExtension | undefined;\n\t#vouches: Vouch[] = [];\n\n\tconstructor(doc: Document) {\n\t\tthis.#doc = doc;\n\t\tdoc.addEventListener(EXTENSION_EVENT, (event) =>\n\t\t\tthis.#heard((event as CustomEvent).detail),\n\t\t);\n\t\tdoc.documentElement?.setAttribute(MARK, \"1\");\n\t}\n\n\tjoin(page: HandshakePage): void {\n\t\tthis.#pages.push(page);\n\t\tif (this.#extension !== undefined) page.set(this.#extension);\n\t\tpage.mcp.addEventListener(\"grantschange\", () => this.#say(page, \"update\"));\n\t\tthis.#say(page, \"hello\");\n\t}\n\n\t#say(page: HandshakePage, type: \"hello\" | \"update\"): void {\n\t\t// The document's own `CustomEvent`: a document may take only events\n\t\t// of its realm.\n\t\tconst Custom = this.#doc.defaultView?.CustomEvent ?? CustomEvent;\n\t\tthis.#doc.dispatchEvent(\n\t\t\tnew Custom(PAGE_EVENT, {\n\t\t\t\tdetail: JSON.stringify({\n\t\t\t\t\tv: 1,\n\t\t\t\t\ttype,\n\t\t\t\t\tlibrary: page.version,\n\t\t\t\t\tapp: page.app,\n\t\t\t\t\t// Capped as the extension cuts what it hears: more would\n\t\t\t\t\t// not be heard anyway.\n\t\t\t\t\tapps: page.mcp.grants.slice(0, MAX_APPS).map((g) => ({\n\t\t\t\t\t\tname: g.client.name,\n\t\t\t\t\t\thost: g.client.host,\n\t\t\t\t\t})),\n\t\t\t\t}),\n\t\t\t}),\n\t\t);\n\t}\n\n\t#heard(detail: unknown): void {\n\t\tlet m: unknown;\n\t\ttry {\n\t\t\tm = typeof detail === \"string\" ? JSON.parse(detail) : undefined;\n\t\t} catch {\n\t\t\treturn;\n\t\t}\n\t\tif (!isObject(m) || m[\"v\"] !== 1) return;\n\t\tif (m[\"type\"] === \"vouch\") this.#vouch(m);\n\t\t// The call it was for ended or never reached the page: an unused vouch\n\t\t// must not wait its 2 s for another caller.\n\t\telse if (m[\"type\"] === \"withdraw\")\n\t\t\tthis.#vouches = this.#vouches.filter((v) => v.nonce !== m[\"nonce\"]);\n\t\telse if (m[\"type\"] === \"hello\" || m[\"type\"] === \"update\") {\n\t\t\tconst extension = extensionOf(m);\n\t\t\tif (extension === null) return;\n\t\t\tif (canonical(extension) !== canonical(this.#extension)) {\n\t\t\t\tthis.#extension = extension;\n\t\t\t\tfor (const page of this.#pages) page.set(extension);\n\t\t\t}\n\t\t\tif (m[\"type\"] === \"hello\")\n\t\t\t\tfor (const page of this.#pages) this.#say(page, \"update\");\n\t\t}\n\t}\n\n\t/**\n\t * A vouch, kept for the tool registered under its name now, by a page\n\t * that takes vouches: a tool registered anew after this does not get it.\n\t */\n\t#vouch(m: Record<string, unknown>): void {\n\t\tconst app = appOf(m[\"app\"]);\n\t\tconst name = m[\"tool\"];\n\t\tconst nonce = m[\"nonce\"];\n\t\tif (\n\t\t\tapp === undefined ||\n\t\t\ttypeof name !== \"string\" ||\n\t\t\ttypeof nonce !== \"string\" ||\n\t\t\t!isObject(m[\"arguments\"])\n\t\t)\n\t\t\treturn;\n\t\tthis.#drop();\n\t\t// The same vouch heard twice counts once.\n\t\tif (this.#vouches.some((v) => v.nonce === nonce)) return;\n\t\tfor (const page of this.#pages) {\n\t\t\tconst tool = page.trust ? page.registry.get(name) : undefined;\n\t\t\tif (tool === undefined) continue;\n\t\t\tthis.#vouches.push({\n\t\t\t\ttool,\n\t\t\t\targs: canonical(m[\"arguments\"]),\n\t\t\t\tnonce,\n\t\t\t\tapp,\n\t\t\t\tat: now(),\n\t\t\t});\n\t\t\tif (this.#vouches.length > MAX_VOUCHES) this.#vouches.shift();\n\t\t\treturn;\n\t\t}\n\t}\n\n\t/** Forget the vouches older than `VOUCH_MS`. */\n\t#drop(): void {\n\t\tconst t = now();\n\t\tthis.#vouches = this.#vouches.filter((v) => t - v.at <= VOUCH_MS);\n\t}\n\n\t/**\n\t * The vouch for this call of `tool` with `input`, used up; `undefined`\n\t * if there is none (the call is asked as without the extension).\n\t */\n\ttake(tool: RegisteredTool, input: unknown): VouchedBy | undefined {\n\t\tthis.#drop();\n\t\tconst args = canonical(input);\n\t\tconst i = this.#vouches.findIndex(\n\t\t\t(v) => v.tool === tool && v.args === args,\n\t\t);\n\t\treturn i < 0 ? undefined : this.#vouches.splice(i, 1)[0]?.app;\n\t}\n}\n\n/**\n * The extension's state from its `hello` or `update`: `undefined` when it\n * does not serve the page now, `null` when the message is malformed.\n */\nfunction extensionOf(\n\tm: Record<string, unknown>,\n): BrowserExtension | undefined | null {\n\tif (m[\"on\"] !== true) return m[\"on\"] === false ? undefined : null;\n\tconst { name, apps } = m;\n\tif (!text(name) || !Array.isArray(apps)) return null;\n\tconst list = apps.slice(0, MAX_APPS).map(appOf);\n\tif (list.some((a) => a === undefined)) return null;\n\treturn Object.freeze({\n\t\tname,\n\t\tapps: Object.freeze(list.map((a) => Object.freeze(a as VouchedBy))),\n\t\twhileInFront: m[\"whileInFront\"] !== false,\n\t});\n}\n\nconst handshakes = new WeakMap<Document, Handshake>();\n\n/**\n * The handshake of the page's top-level document, which `page` joins; the\n * extension speaks only to the top frame. `undefined` in a frame or\n * without a document.\n */\nexport function joinHandshake(page: HandshakePage): Handshake | undefined {\n\tconst doc = globalThis.document;\n\tif (doc === undefined || globalThis.top !== globalThis.window)\n\t\treturn undefined;\n\tlet handshake = handshakes.get(doc);\n\tif (handshake === undefined) {\n\t\thandshake = new Handshake(doc);\n\t\thandshakes.set(doc, handshake);\n\t}\n\thandshake.join(page);\n\treturn handshake;\n}\n",
6
6
  "/**\n * Mirroring the tools into the browser's own WebMCP (iteration 25, DEC-115,\n * DEC-119, DEC-143): with `webmcp: true`, each tool that is offered there is\n * also registered with `document.modelContext.registerTool()`, under its own\n * `AbortController`, aborted when the tool leaves the registry. A chunk of\n * its own (DEC-171), loaded by a `TabMcp` whose constructor found native\n * WebMCP (`nativeContext` in the core); a call loads the connection chunk,\n * which checks and runs it. The handshake with the tab-mcp browser\n * extension (`handshake.ts`, DEC-207) is here too: only a page that mirrors\n * takes part.\n */\n\nimport {\n\ttype HandshakePage,\n\tjoinHandshake,\n\ttype VouchedBy,\n} from \"./handshake.ts\";\nimport type { RegisteredTool, ToolRegistry } from \"./registry.ts\";\nimport type { BrowserExtension } from \"./types.ts\";\n\n/** What the library uses of the browser's `ModelContext`. */\nexport interface ModelContext {\n\tregisterTool(\n\t\ttool: object,\n\t\toptions: { signal: AbortSignal; exposedTo: string[] | undefined },\n\t): unknown;\n}\n\n/**\n * Runs a call of a mirrored tool; `signal` is the browser's, if it gives\n * one; `by`: the AI app the tab-mcp browser extension vouched for it.\n */\nexport type WebMcpCall = (\n\ttool: RegisteredTool,\n\tinput: unknown,\n\tsignal: unknown,\n\tby: VouchedBy | undefined,\n) => Promise<unknown>;\n\n/** The WebMCP hints; the MCP-only ones are left out. */\nconst HINTS = [\n\t\"readOnlyHint\",\n\t\"untrustedContentHint\",\n\t\"consequentialHint\",\n\t\"debugging\",\n] as const;\n\n/**\n * Mirror `registry`'s tools into `context`, now and whenever the registry\n * changes: every tool, unless its `callers` leave `\"webmcp\"` out (DEC-165,\n * DEC-209). A tool that asks is marked `consequentialHint`, so that the agent\n * asks its user; MCP's `destructiveHint` counts as `consequentialHint`\n * (DEC-143). The page's `onConfirm` decides such a call, never the library's\n * dialog (DEC-215). Every failure of the native `registerTool()`\n * stays here: warned once per name, the tool still offered through the relay.\n * The page joins the handshake with the tab-mcp browser extension: `mcp`\n * is its `TabMcp`, `app` its app id, `options` its options\n * (`trustExtension`, and `onConfirm` for the warning below), `version` the\n * library's. A tool that asks, mirrored with an `exposedTo` that names\n * other origins on a page without `onConfirm`, is warned about once per\n * name (`warnUnasked`): documents of those origins can run it, and the\n * library asks no WebMCP caller (DEC-215). The extension's state is `mcp.extension`, defined\n * here on the instance (the core only declares it, for its bytes), and\n * `extensionchange` is sent here.\n */\nexport function mirror(\n\tregistry: ToolRegistry,\n\tcontext: ModelContext,\n\tcall: WebMcpCall,\n\tmcp: HandshakePage[\"mcp\"],\n\tapp: string,\n\toptions: { trustExtension?: boolean; onConfirm?: unknown },\n\tversion: string,\n): void {\n\tlet state: BrowserExtension | undefined;\n\tObject.defineProperty(mcp, \"extension\", {\n\t\tget: () => state,\n\t\tconfigurable: true,\n\t});\n\tlet handshake: ReturnType<typeof joinHandshake>;\n\ttry {\n\t\thandshake = joinHandshake({\n\t\t\tversion,\n\t\t\tapp,\n\t\t\tregistry,\n\t\t\ttrust: options.trustExtension !== false,\n\t\t\tmcp,\n\t\t\tset: (extension) => {\n\t\t\t\tstate = extension;\n\t\t\t\tmcp.dispatchEvent(\n\t\t\t\t\tnew CustomEvent(\"extensionchange\", { detail: { extension } }),\n\t\t\t\t);\n\t\t\t},\n\t\t});\n\t} catch (error) {\n\t\t// The tools are mirrored all the same; no call is vouched for.\n\t\tconsole.warn(\"tab-mcp: no handshake with the browser extension:\", error);\n\t}\n\tconst mirrors = new Map<RegisteredTool, AbortController>();\n\tconst warned = new Set<string>();\n\tconst unasked = new Set<string>();\n\t/**\n\t * The warning about a tool that asks with `exposedTo` (DEC-215), once its\n\t * native registration succeeded (so Chrome accepted every origin), once\n\t * per name. Decided then: a handler the page removes later is not\n\t * caught. Never throws, whatever `exposedTo` holds.\n\t */\n\tconst warnUnasked = (tool: RegisteredTool, asks: boolean | undefined) => {\n\t\ttry {\n\t\t\t// React's provider always passes a handler; `own` says whether the\n\t\t\t// page gave one.\n\t\t\tconst ask = options.onConfirm as { own?: boolean } | undefined;\n\t\t\tconst page = globalThis.document?.location?.origin;\n\t\t\tconst others = (\n\t\t\t\tArray.isArray(tool.exposedTo) ? tool.exposedTo : []\n\t\t\t).filter((entry: unknown) => {\n\t\t\t\ttry {\n\t\t\t\t\treturn new URL(String(entry)).origin !== page;\n\t\t\t\t} catch {\n\t\t\t\t\treturn false; // the browser refuses it anyway\n\t\t\t\t}\n\t\t\t});\n\t\t\tif (\n\t\t\t\tasks !== true ||\n\t\t\t\tothers.length === 0 ||\n\t\t\t\t(ask && ask.own !== false) ||\n\t\t\t\tunasked.has(tool.name)\n\t\t\t)\n\t\t\t\treturn;\n\t\t\tunasked.add(tool.name);\n\t\t\tconsole.warn(\n\t\t\t\t`tab-mcp: ${tool.name} asks before it runs, but documents of ${others.join(\", \")} (its exposedTo) can call it through WebMCP without a question: the library asks no WebMCP caller. Refuse such calls in onConfirm, or leave exposedTo off.`,\n\t\t\t);\n\t\t} catch {\n\t\t\t// a warning never stops the mirror\n\t\t}\n\t};\n\tconst sync = (): void => {\n\t\tfor (const [tool, controller] of mirrors) {\n\t\t\tif (registry.get(tool.name) === tool) continue;\n\t\t\tcontroller.abort();\n\t\t\tmirrors.delete(tool);\n\t\t}\n\t\tfor (const tool of registry.list()) {\n\t\t\tconst { annotations, callers } = tool;\n\t\t\tconst consequential =\n\t\t\t\tannotations.consequentialHint ?? annotations.destructiveHint;\n\t\t\t// The one place of the rule for WebMCP (the relay and a port: `toolsFor`).\n\t\t\tif (mirrors.has(tool) || !(callers?.includes(\"webmcp\") ?? true)) continue;\n\t\t\tconst controller = new AbortController();\n\t\t\tmirrors.set(tool, controller);\n\t\t\tconst hints: Record<string, boolean | undefined> = {\n\t\t\t\tconsequentialHint: consequential,\n\t\t\t};\n\t\t\tfor (const key of HINTS) hints[key] ??= annotations[key];\n\t\t\tconst { signal } = controller;\n\t\t\tPromise.resolve()\n\t\t\t\t// Not once unmirrored: whatever the browser does with an aborted\n\t\t\t\t// signal.\n\t\t\t\t.then(\n\t\t\t\t\t() =>\n\t\t\t\t\t\tsignal.aborted ||\n\t\t\t\t\t\tcontext.registerTool(\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tname: tool.name,\n\t\t\t\t\t\t\t\t// WebIDL dictionaries: `undefined` is the same as left out.\n\t\t\t\t\t\t\t\ttitle: tool.title,\n\t\t\t\t\t\t\t\tdescription: tool.description,\n\t\t\t\t\t\t\t\tinputSchema: tool.inputSchema,\n\t\t\t\t\t\t\t\toutputSchema: tool.outputSchema,\n\t\t\t\t\t\t\t\tannotations: hints,\n\t\t\t\t\t\t\t\t// The vouch is looked up as the call arrives: it was\n\t\t\t\t\t\t\t\t// sent right before the extension made the call.\n\t\t\t\t\t\t\t\texecute: (input: unknown, options?: { signal?: unknown }) =>\n\t\t\t\t\t\t\t\t\tcall(\n\t\t\t\t\t\t\t\t\t\ttool,\n\t\t\t\t\t\t\t\t\t\tinput,\n\t\t\t\t\t\t\t\t\t\toptions?.signal,\n\t\t\t\t\t\t\t\t\t\thandshake?.take(tool, input),\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t{ signal, exposedTo: tool.exposedTo },\n\t\t\t\t\t\t),\n\t\t\t\t)\n\t\t\t\t.then(() => {\n\t\t\t\t\tif (!signal.aborted) warnUnasked(tool, consequential);\n\t\t\t\t})\n\t\t\t\t.catch((error: unknown) => {\n\t\t\t\t\tif (signal.aborted) return; // unmirrored meanwhile\n\t\t\t\t\tif (warned.has(tool.name)) return;\n\t\t\t\t\twarned.add(tool.name);\n\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t`tab-mcp: ${tool.name} is not offered through WebMCP:`,\n\t\t\t\t\t\terror,\n\t\t\t\t\t);\n\t\t\t\t});\n\t\t}\n\t};\n\tregistry.onChange(sync);\n\tsync();\n}\n"
7
7
  ],
8
- "mappings": ";AAmCO,IAAM,aAAa;AAEnB,IAAM,kBAAkB;AAExB,IAAM,OAAO;AAEb,IAAM,WAAW;AAExB,IAAM,cAAc;AAEpB,IAAM,WAAW;AACjB,IAAM,WAAW;AA+BjB,IAAM,WAAW,CAAC,UACjB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;AAEpE,IAAM,OAAO,CAAC,UACb,OAAO,UAAU,YAAY,MAAM,UAAU;AAG9C,SAAS,KAAK,CAAC,OAAuC;AAAA,EACrD,OAAO,SAAS,KAAK,KAAK,KAAK,MAAM,OAAO,KAAK,KAAK,MAAM,OAAO,IAChE,EAAE,MAAM,MAAM,SAAS,MAAM,MAAM,QAAQ,IAC3C;AAAA;AAOG,SAAS,SAAS,CAAC,OAAwB;AAAA,EACjD,OACC,KAAK,UAAU,OAAO,CAAC,MAAM,MAC5B,SAAS,CAAC,IACP,OAAO,YACP,OAAO,QAAQ,CAAC,EAAE,KAAK,EAAE,KAAK,OAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,CAAE,CAClE,IACC,CACJ,KAAK;AAAA;AAKP,IAAM,MAAM,MAAM,YAAY,IAAI;AAAA;AAElC,MAAM,UAAU;AAAA,E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O,IAAI,KAAK,IAAI;AAAA,QACpB,QAAQ,KACP,YAAY,KAAK,uCACjB,KACD;AAAA,OACA;AAAA,IACH;AAAA;AAAA,EAED,SAAS,SAAS,IAAI;AAAA,EACtB,KAAK;AAAA;",
9
+ "debugId": "9DAD2DB75032006364756E2164756E21",
10
10
  "names": []
11
11
  }
@@ -1,4 +0,0 @@
1
- import{u}from"./tab-mcp-client-gdj1e1wh.min.js";import{a,r,e,t,s,d,l}from"./tab-mcp-client-yjcmhpdf.min.js";import{o}from"./tab-mcp-client-q758jr4g.min.js";function ee(f){try{return s(JSON.stringify(f,null,2))}catch{return}}var ne=750,te=500,J=new WeakMap;function K(f,n,b={}){if(f.signal.aborted)return Promise.resolve("deny");let p=J.get(n);if(p===void 0)p={shown:!1,waiting:[]},J.set(n,p);let i=p,c=()=>{let h=i.waiting.shift();i.shown=h!==void 0,h?.(),i.changed?.()},D=()=>{try{return ie(f,n,b,i,c)}catch(h){return c(),Promise.reject(h)}};if(!i.shown)return i.shown=!0,D();return new Promise((h)=>{let v=()=>{if(f.signal.removeEventListener("abort",E),b.allowedNow?.()===!0){h("allow"),c();return}h(D())},E=()=>{i.waiting.splice(i.waiting.indexOf(v),1),i.changed?.(),h("deny")};f.signal.addEventListener("abort",E,{once:!0}),i.waiting.push(v),i.changed?.()})}function ie(f,n,b,p,i){let c=b.strings??a(void 0),D=b.allowDelayMs??ne,h=f.tool.title??f.tool.name,v=d(f.client,c),E={tool:h,client:v},{dialog:m,id:L}=l(n,"tab-mcp-confirm",c,...r(n,c.confirmTitle,E)),I=e(n,"p",{className:"tab-mcp-waiting"}),S=()=>{let g=p.waiting.length;I.hidden=g===0,I.textContent=u(c.waiting,g,c)};p.changed=S,S();let z=t(n,c.deny,()=>m.close("deny"),"tab-mcp-deny","primary"),B=(g,j,O)=>{let A=t(n,g,()=>{if(!A.disabled)m.close(j)},O);return A},N=[B(c.allow,"allow","tab-mcp-allow")],W=[],Q=b.alwaysAllow;if(Q===!0||Q==="until-reload"){let g=Q===!0;N.unshift(B(g?c.alwaysAllow:c.allowUntilReload,"always","tab-mcp-always")),W.push(e(n,"p",{className:"tab-mcp-always-note"},...r(n,g?c.alwaysAllowNote:c.allowUntilReloadNote,E)))}let Y=e(n,"div",{className:"tab-mcp-actions"},z,...N.flatMap((g)=>[" ",g])),x=!1,U=!1,R=()=>{for(let g of N)g.disabled=!(x&&U)};R();let M=ee(f.arguments),y=e(n,"pre",{className:"tab-mcp-arguments",id:`${L}-arguments`,text:M??c.argumentsHidden});if(M===void 0)for(let g of[...N,...W])g.hidden=!0;y.dir="ltr",y.style.setProperty("white-space","pre-wrap"),y.style.setProperty("overflow-wrap","anywhere"),y.style.setProperty("max-height","var(--tab-mcp-arguments-height, min(50vh, 24rem))"),y.style.setProperty("overflow","auto"),y.hidden=Object.keys(f.arguments).length===0;let T=e(n,"p",{className:"tab-mcp-scroll-note",text:c.scrollToAllow});T.hidden=!0;let _=e(n,"p",{id:`${L}-who`},...r(n,c.confirmWho,E));m.setAttribute("aria-describedby",M!==void 0&&!y.hidden&&M.length<=te?`${_.id} ${y.id}`:_.id),m.append(_,e(n,"p",{className:"tab-mcp-description",text:f.tool.description}),y,T,Y,...W,I),n.body.append(m);let k=n.defaultView;return new Promise((g,j)=>{let O=()=>m.close("deny");f.signal.addEventListener("abort",O,{once:!0});let A,P,F=()=>{if(P!==void 0&&n.title===P.shown)n.title=P.page;P=void 0},X=()=>{if(n.visibilityState!=="hidden")F();else if(P===void 0)P={page:n.title,shown:`${c.titlePrefix}${n.title}`},n.title=P.shown},C=(w)=>{if(w?.type==="close"&&w.target===m)return;if(X(),clearTimeout(A),A=void 0,x=!1,R(),w?.type==="blur"||n.visibilityState==="hidden"||!n.hasFocus())return;A=setTimeout(()=>{x=!0,R()},D)},H=()=>{if(U)return;if(y.scrollTop+y.clientHeight<y.scrollHeight-2){T.hidden=!1,y.tabIndex=0;return}U=!0,T.hidden=!0,R()},V=(w)=>{if(x)return;let q=w.target?.localName==="button";if(w.key==="Escape"||q&&(w.key==="Enter"||w.key===" "))w.preventDefault()},Z=(w)=>{if(!x)w.preventDefault()};n.addEventListener("visibilitychange",C),n.addEventListener("close",C,!0),k?.addEventListener("focus",C),k?.addEventListener("blur",C),k?.addEventListener("resize",H),y.addEventListener("scroll",H),m.addEventListener("keydown",V),m.addEventListener("keyup",V),m.addEventListener("cancel",Z);let G=()=>{if(clearTimeout(A),F(),n.removeEventListener("visibilitychange",C),n.removeEventListener("close",C,!0),k?.removeEventListener("focus",C),k?.removeEventListener("blur",C),k?.removeEventListener("resize",H),f.signal.removeEventListener("abort",O),m.remove(),p.changed===S)p.changed=void 0};m.addEventListener("close",()=>{let w=m.returnValue;G(),g(w==="allow"||w==="always"?w:"deny"),i()},{once:!0});try{m.showModal()}catch(w){G(),j(w),i();return}z.focus(),C(),H()})}function le(f,n,b,p={}){let{client:i}=n;if(i.kind==="webmcp")return Promise.resolve(!0);let c=a(p.ui),{allowDelayMs:D}=p;if(i.kind!=="relay")return K({...n,client:i},b,{strings:c,allowDelayMs:D}).then((L)=>L==="allow");let{grantId:h}=i,v=o(f),E=()=>v.allows(h,n.tool);if(E())return Promise.resolve(!0);let m=p.alwaysAllow??v.offered;return K({...n,client:i},b,{strings:c,allowDelayMs:D,alwaysAllow:m!==!1&&(m===!0&&v.kept?!0:"until-reload"),allowedNow:E}).then((L)=>{if(L==="always")v.keep(h,n.tool);return L!=="deny"})}function se(f,n,b){let{client:p}=n;if(p.kind==="relay"&&o(f).allows(p.grantId,n.tool))return Promise.resolve(!0);if(b===void 0)return f.confirmInPage(n);return Promise.resolve().then(()=>b(n)).then((i)=>i==="unanswered"?i:i===!0)}export{se as confirmCall,le as confirmInPage};
2
-
3
- //# debugId=A703208B3089B74564756E2164756E21
4
- //# sourceMappingURL=confirm-xgm1ymd0.min.js.map
@@ -1,10 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/confirm.ts"],
4
- "sourcesContent": [
5
- "/**\n * The confirmation of consequential tools, loaded on demand: a `<dialog>`\n * per call, one shown at a time per document, and Always allow (or Allow\n * until reload) for the tools a user allows (`always-allowed.ts`).\n */\n\nimport { allowedFor, type ConfirmHost } from \"./always-allowed.ts\";\nimport {\n\tbutton,\n\tclientLabel,\n\tel,\n\tfill,\n\tnewDialog,\n\tplural,\n\tstringsOf,\n\tvisible,\n} from \"./dialog.ts\";\nimport type { DialogStrings } from \"./strings.ts\";\nimport type { TabMcp } from \"./tab-mcp.ts\";\nimport type {\n\tConfirmAnswer,\n\tConfirmHandler,\n\tConfirmRequest,\n\tDialogOptions,\n\tLocalClient,\n\tRelayClient,\n} from \"./types.ts\";\n\n/** A request the dialog is shown for: never a WebMCP call's (DEC-215). */\nexport type AskedRequest = ConfirmRequest & {\n\tclient: RelayClient | LocalClient;\n};\n\n/**\n * The arguments as text, in full: what the user approves must be visible.\n * (The relay caps a request at 1 MiB, so this stays bounded.) Invisible and\n * direction characters are shown as escapes, so the text is still JSON for\n * the same arguments; the tool gets them unchanged. `undefined` when they\n * cannot be shown, and then \"Allow once\" is not offered.\n */\nfunction showArguments(args: Record<string, unknown>): string | undefined {\n\ttry {\n\t\treturn visible(JSON.stringify(args, null, 2));\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\n/**\n * How long \"Allow once\" stays disabled after the confirmation is shown, while the\n * page is visible and focused (CU 17: a constant; tests pass `allowDelayMs`).\n */\nexport const ALLOW_DELAY_MS = 750;\n\n/** The user's answer: \"always\" allows this tool for this AI app (Always allow, or until reload). */\nexport type DialogAnswer = \"allow\" | \"always\" | \"deny\";\n\n/** Arguments up to this many characters are read out with the question. */\nconst DESCRIBED_ARGUMENTS = 500;\n\n/** Options of `confirmDialog`. */\nexport interface ConfirmDialogOptions {\n\t/** Tests only: another delay than {@link ALLOW_DELAY_MS}. */\n\tallowDelayMs?: number | undefined;\n\t/**\n\t * Offer a third button: `true` \"Always allow\" (kept until the app is\n\t * disconnected), `\"until-reload\"` \"Allow until reload\" (kept in memory).\n\t */\n\talwaysAllow?: boolean | \"until-reload\" | undefined;\n\t/** Asked when a waiting call's turn comes: `true` answers \"allow\" unseen. */\n\tallowedNow?: () => boolean;\n\tstrings?: DialogStrings;\n}\n\n/**\n * The confirmations of one document: one is shown at a time, the others\n * wait in order, so that the click that answered one cannot answer the next\n * (each starts its own delay when it is shown).\n */\ninterface ConfirmQueue {\n\tshown: boolean;\n\twaiting: (() => void)[];\n\t/** Updates the shown dialog's line about the calls that wait. */\n\tchanged?: (() => void) | undefined;\n}\n\nconst queues = new WeakMap<Document, ConfirmQueue>();\n\n/**\n * Ask whether a consequential tool may run. \"Always allow\" (or \"Allow until\n * reload\") is offered only with `alwaysAllow` (DEC-096, DEC-200); never\n * \"allow everything\": that is a deliberate act in the connect dialog, not\n * an answer to one question. One confirmation is shown at a time per\n * document; the others wait their turn, and a call cancelled while it waits\n * is never shown. \"Allow once\" and \"Always allow\" stay disabled until the\n * page has been visible and focused for 750 ms while the dialog is open and\n * is the top one, so a click meant for the page, the click that brings the\n * tab or window back, or the one that closed another dialog, cannot\n * approve; leaving the page disables them again. They also wait until the\n * arguments have been scrolled to their end. Before the delay has run out,\n * Enter or Space on a button and Escape are ignored, so typing meant for the\n * page cannot refuse either. Everything else (Don't allow, Escape, an\n * aborted call) is \"deny\". `allowedNow` is asked when a waiting call's turn\n * comes: `true` (the user allowed it meanwhile) answers \"allow\"\n * without showing it.\n */\nexport function confirmDialog(\n\trequest: AskedRequest,\n\tdoc: Document,\n\toptions: ConfirmDialogOptions = {},\n): Promise<DialogAnswer> {\n\tif (request.signal.aborted) return Promise.resolve(\"deny\");\n\tlet queue = queues.get(doc);\n\tif (queue === undefined) {\n\t\tqueue = { shown: false, waiting: [] };\n\t\tqueues.set(doc, queue);\n\t}\n\tconst q = queue;\n\t/** The shown confirmation closed: the next one, if any. */\n\tconst next = () => {\n\t\tconst start = q.waiting.shift();\n\t\tq.shown = start !== undefined;\n\t\tstart?.();\n\t\tq.changed?.();\n\t};\n\tconst show = () => {\n\t\ttry {\n\t\t\treturn showConfirm(request, doc, options, q, next);\n\t\t} catch (error) {\n\t\t\tnext(); // not shown: the queue goes on\n\t\t\treturn Promise.reject(error);\n\t\t}\n\t};\n\tif (!q.shown) {\n\t\tq.shown = true;\n\t\treturn show();\n\t}\n\treturn new Promise((resolve) => {\n\t\tconst start = () => {\n\t\t\trequest.signal.removeEventListener(\"abort\", onAbort);\n\t\t\tif (options.allowedNow?.() === true) {\n\t\t\t\tresolve(\"allow\");\n\t\t\t\tnext();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tresolve(show());\n\t\t};\n\t\tconst onAbort = () => {\n\t\t\tq.waiting.splice(q.waiting.indexOf(start), 1);\n\t\t\tq.changed?.();\n\t\t\tresolve(\"deny\");\n\t\t};\n\t\trequest.signal.addEventListener(\"abort\", onAbort, { once: true });\n\t\tq.waiting.push(start);\n\t\tq.changed?.();\n\t});\n}\n\n/** `confirmDialog` once it is this request's turn; calls `next` when it closes. */\nfunction showConfirm(\n\trequest: AskedRequest,\n\tdoc: Document,\n\toptions: ConfirmDialogOptions,\n\tqueue: ConfirmQueue,\n\tnext: () => void,\n): Promise<DialogAnswer> {\n\tconst s = options.strings ?? stringsOf(undefined);\n\tconst delay = options.allowDelayMs ?? ALLOW_DELAY_MS;\n\tconst tool = request.tool.title ?? request.tool.name;\n\tconst client = clientLabel(request.client, s);\n\tconst names = { tool, client };\n\tconst { dialog, id } = newDialog(\n\t\tdoc,\n\t\t\"tab-mcp-confirm\",\n\t\ts,\n\t\t...fill(doc, s.confirmTitle, names),\n\t);\n\tconst waiting = el(doc, \"p\", { className: \"tab-mcp-waiting\" });\n\tconst showWaiting = () => {\n\t\tconst n = queue.waiting.length;\n\t\twaiting.hidden = n === 0;\n\t\twaiting.textContent = plural(s.waiting, n, s);\n\t};\n\tqueue.changed = showWaiting;\n\tshowWaiting();\n\t// The safe choice is the default: focused, and the one that stands out.\n\tconst deny = button(\n\t\tdoc,\n\t\ts.deny,\n\t\t() => dialog.close(\"deny\"),\n\t\t\"tab-mcp-deny\",\n\t\t\"primary\",\n\t);\n\tconst approving = (text: string, answer: DialogAnswer, className: string) => {\n\t\tconst node = button(\n\t\t\tdoc,\n\t\t\ttext,\n\t\t\t() => {\n\t\t\t\tif (!node.disabled) dialog.close(answer);\n\t\t\t},\n\t\t\tclassName,\n\t\t);\n\t\treturn node;\n\t};\n\t// The approving buttons: Allow, and Always allow (or Allow until reload)\n\t// when the page offers it, saying how long it lasts.\n\tconst approve = [approving(s.allow, \"allow\", \"tab-mcp-allow\")];\n\tconst extra: HTMLElement[] = [];\n\tconst mode = options.alwaysAllow;\n\tif (mode === true || mode === \"until-reload\") {\n\t\tconst kept = mode === true;\n\t\t// Left of Allow: not where most platforms put the default (CU 11).\n\t\tapprove.unshift(\n\t\t\tapproving(\n\t\t\t\tkept ? s.alwaysAllow : s.allowUntilReload,\n\t\t\t\t\"always\",\n\t\t\t\t\"tab-mcp-always\",\n\t\t\t),\n\t\t);\n\t\textra.push(\n\t\t\tel(\n\t\t\t\tdoc,\n\t\t\t\t\"p\",\n\t\t\t\t{ className: \"tab-mcp-always-note\" },\n\t\t\t\t...fill(doc, kept ? s.alwaysAllowNote : s.allowUntilReloadNote, names),\n\t\t\t),\n\t\t);\n\t}\n\tconst actions = el(\n\t\tdoc,\n\t\t\"div\",\n\t\t{ className: \"tab-mcp-actions\" },\n\t\tdeny,\n\t\t...approve.flatMap((b) => [\" \", b]),\n\t);\n\t// Approving needs both: the delay ran out while the user saw the page\n\t// (`settled`), and the arguments were seen to their end (`seen`).\n\tlet settled = false;\n\tlet seen = false;\n\tconst enable = () => {\n\t\tfor (const b of approve) b.disabled = !(settled && seen);\n\t};\n\tenable();\n\tconst shown = showArguments(request.arguments);\n\tconst args = el(doc, \"pre\", {\n\t\tclassName: \"tab-mcp-arguments\",\n\t\tid: `${id}-arguments`,\n\t\ttext: shown ?? s.argumentsHidden,\n\t});\n\tif (shown === undefined) {\n\t\tfor (const node of [...approve, ...extra]) node.hidden = true;\n\t}\n\targs.dir = \"ltr\";\n\t// Inline, so the arguments stay readable in full without the stylesheet:\n\t// wrapped (also a long word, at 360 px), scrolling when long.\n\targs.style.setProperty(\"white-space\", \"pre-wrap\");\n\targs.style.setProperty(\"overflow-wrap\", \"anywhere\");\n\targs.style.setProperty(\n\t\t\"max-height\",\n\t\t\"var(--tab-mcp-arguments-height, min(50vh, 24rem))\",\n\t);\n\targs.style.setProperty(\"overflow\", \"auto\");\n\targs.hidden = Object.keys(request.arguments).length === 0;\n\t// The caller chooses the order of the keys, so what matters may come\n\t// last: below the end of the box, the user is told, and Allow waits.\n\tconst scrollNote = el(doc, \"p\", {\n\t\tclassName: \"tab-mcp-scroll-note\",\n\t\ttext: s.scrollToAllow,\n\t});\n\tscrollNote.hidden = true;\n\tconst who = el(\n\t\tdoc,\n\t\t\"p\",\n\t\t{ id: `${id}-who` },\n\t\t...fill(doc, s.confirmWho, names),\n\t);\n\tdialog.setAttribute(\n\t\t\"aria-describedby\",\n\t\tshown !== undefined && !args.hidden && shown.length <= DESCRIBED_ARGUMENTS\n\t\t\t? `${who.id} ${args.id}`\n\t\t\t: who.id,\n\t);\n\tdialog.append(\n\t\twho,\n\t\tel(doc, \"p\", {\n\t\t\tclassName: \"tab-mcp-description\",\n\t\t\ttext: request.tool.description,\n\t\t}),\n\t\targs,\n\t\tscrollNote,\n\t\tactions,\n\t\t...extra,\n\t\twaiting,\n\t);\n\tdoc.body.append(dialog);\n\tconst view = doc.defaultView;\n\treturn new Promise((resolve, reject) => {\n\t\tconst onAbort = () => dialog.close(\"deny\");\n\t\trequest.signal.addEventListener(\"abort\", onAbort, { once: true });\n\t\tlet timer: ReturnType<typeof setTimeout> | undefined;\n\t\t// In a hidden tab, the tab's title says that the page asks (CU 9): the\n\t\t// AI app waits meanwhile. Put back when the tab is shown or the dialog\n\t\t// closes, unless the page changed its title since.\n\t\tlet title: { page: string; shown: string } | undefined;\n\t\tconst untitle = () => {\n\t\t\tif (title !== undefined && doc.title === title.shown)\n\t\t\t\tdoc.title = title.page;\n\t\t\ttitle = undefined;\n\t\t};\n\t\tconst retitle = () => {\n\t\t\tif (doc.visibilityState !== \"hidden\") untitle();\n\t\t\telse if (title === undefined) {\n\t\t\t\ttitle = { page: doc.title, shown: `${s.titlePrefix}${doc.title}` };\n\t\t\t\tdoc.title = title.shown;\n\t\t\t}\n\t\t};\n\t\t// The delay counts only while the user can see the page: tools are\n\t\t// called while the user is in the AI app, so the dialog often opens in\n\t\t// a background tab or window. Any change starts it again from zero\n\t\t// (DEC-087), and so does another dialog of the page that closes over\n\t\t// this one.\n\t\tconst arm = (event?: Event) => {\n\t\t\tif (event?.type === \"close\" && event.target === dialog) return;\n\t\t\tretitle();\n\t\t\tclearTimeout(timer);\n\t\t\ttimer = undefined;\n\t\t\tsettled = false;\n\t\t\tenable();\n\t\t\tif (\n\t\t\t\tevent?.type === \"blur\" ||\n\t\t\t\tdoc.visibilityState === \"hidden\" ||\n\t\t\t\t!doc.hasFocus()\n\t\t\t)\n\t\t\t\treturn;\n\t\t\ttimer = setTimeout(() => {\n\t\t\t\tsettled = true;\n\t\t\t\tenable();\n\t\t\t}, delay);\n\t\t};\n\t\t// Scrolled to the end, or nothing to scroll (also without layout).\n\t\tconst check = () => {\n\t\t\tif (seen) return;\n\t\t\tif (args.scrollTop + args.clientHeight < args.scrollHeight - 2) {\n\t\t\t\tscrollNote.hidden = false;\n\t\t\t\targs.tabIndex = 0; // to scroll it with the keyboard\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tseen = true;\n\t\t\tscrollNote.hidden = true;\n\t\t\tenable();\n\t\t};\n\t\t// Not answered by a key before the delay has run out.\n\t\tconst guard = (event: KeyboardEvent) => {\n\t\t\tif (settled) return;\n\t\t\tconst onButton = (event.target as Element | null)?.localName === \"button\";\n\t\t\tif (\n\t\t\t\tevent.key === \"Escape\" ||\n\t\t\t\t(onButton && (event.key === \"Enter\" || event.key === \" \"))\n\t\t\t)\n\t\t\t\tevent.preventDefault();\n\t\t};\n\t\tconst onCancel = (event: Event) => {\n\t\t\tif (!settled) event.preventDefault();\n\t\t};\n\t\tdoc.addEventListener(\"visibilitychange\", arm);\n\t\tdoc.addEventListener(\"close\", arm, true); // `close` does not bubble\n\t\tview?.addEventListener(\"focus\", arm);\n\t\tview?.addEventListener(\"blur\", arm);\n\t\tview?.addEventListener(\"resize\", check);\n\t\targs.addEventListener(\"scroll\", check);\n\t\tdialog.addEventListener(\"keydown\", guard);\n\t\tdialog.addEventListener(\"keyup\", guard);\n\t\tdialog.addEventListener(\"cancel\", onCancel);\n\t\tconst cleanup = () => {\n\t\t\tclearTimeout(timer);\n\t\t\tuntitle();\n\t\t\tdoc.removeEventListener(\"visibilitychange\", arm);\n\t\t\tdoc.removeEventListener(\"close\", arm, true);\n\t\t\tview?.removeEventListener(\"focus\", arm);\n\t\t\tview?.removeEventListener(\"blur\", arm);\n\t\t\tview?.removeEventListener(\"resize\", check);\n\t\t\trequest.signal.removeEventListener(\"abort\", onAbort);\n\t\t\tdialog.remove();\n\t\t\tif (queue.changed === showWaiting) queue.changed = undefined;\n\t\t};\n\t\tdialog.addEventListener(\n\t\t\t\"close\",\n\t\t\t() => {\n\t\t\t\tconst value = dialog.returnValue;\n\t\t\t\tcleanup();\n\t\t\t\tresolve(value === \"allow\" || value === \"always\" ? value : \"deny\");\n\t\t\t\tnext();\n\t\t\t},\n\t\t\t{ once: true },\n\t\t);\n\t\ttry {\n\t\t\tdialog.showModal();\n\t\t} catch (error) {\n\t\t\t// Never shown, so never closed: the queue must not wait for it.\n\t\t\tcleanup();\n\t\t\treject(error);\n\t\t\tnext();\n\t\t\treturn;\n\t\t}\n\t\tdeny.focus(); // the safe choice is the default\n\t\t// Counted from when the dialog is shown, not from when it was built.\n\t\tarm();\n\t\tcheck();\n\t});\n}\n\n/** Options of this chunk's `confirmInPage`. */\nexport interface ConfirmInPageOptions {\n\t/**\n\t * The third button, for an AI app through the relay (`kind: \"relay\"`):\n\t * `true` \"Always allow\" (kept in the `TabMcp`'s `storage`; \"Allow until\n\t * reload\" where it is kept in memory, as for a stand-in), `\"until-reload\"`\n\t * \"Allow until reload\", `false` none. Default: the `TabMcp`'s\n\t * `alwaysAllow`; none for a stand-in.\n\t */\n\talwaysAllow?: boolean | \"until-reload\" | undefined;\n\t/** Texts of the dialog, as `TabMcpOptions.ui`. */\n\tui?: DialogOptions | undefined;\n\t/** Tests only: another delay than {@link ALLOW_DELAY_MS}. */\n\tallowDelayMs?: number | undefined;\n}\n\n/**\n * `TabMcp.confirmInPage`: the confirmation, and the tools the user allowed\n * (`always-allowed.ts`), which run unasked. Those only for an AI app through\n * the relay: a served port has no identity to remember an answer for\n * (DEC-115, DEC-124). A WebMCP call is never shown it (an agent that drives\n * the tab would answer it itself; DEC-215): it resolves `true`.\n *\n * Code that loads this chunk itself may pass a stand-in of its own for\n * `mcp` (`ConfirmHost`: `grants`, optionally `grantschange` events). Its\n * allowances live in memory, for its grants only, until the page is\n * reloaded, and exist only where the caller offers them with\n * `options.alwaysAllow`; without it nothing is allowed and nothing is kept.\n */\nexport function confirmInPage(\n\tmcp: TabMcp | ConfirmHost,\n\trequest: ConfirmRequest,\n\tdoc: Document,\n\toptions: ConfirmInPageOptions = {},\n): Promise<boolean> {\n\tconst { client } = request;\n\tif (client.kind === \"webmcp\") return Promise.resolve(true);\n\tconst strings = stringsOf(options.ui);\n\tconst { allowDelayMs } = options;\n\tif (client.kind !== \"relay\")\n\t\treturn confirmDialog({ ...request, client }, doc, {\n\t\t\tstrings,\n\t\t\tallowDelayMs,\n\t\t}).then((answer) => answer === \"allow\");\n\tconst { grantId } = client;\n\tconst allowed = allowedFor(mcp);\n\tconst allowedNow = () => allowed.allows(grantId, request.tool);\n\tif (allowedNow()) return Promise.resolve(true);\n\tconst offer = options.alwaysAllow ?? allowed.offered;\n\t// Asked again when its turn comes: the call before it in the queue may\n\t// have been allowed meanwhile.\n\treturn confirmDialog({ ...request, client }, doc, {\n\t\tstrings,\n\t\tallowDelayMs,\n\t\t// \"Always\" only where it outlives a reload.\n\t\talwaysAllow:\n\t\t\toffer !== false &&\n\t\t\t(offer === true && allowed.kept ? true : \"until-reload\"),\n\t\tallowedNow,\n\t}).then((answer) => {\n\t\t// Only while the grant is still there: a revoked app gets nothing\n\t\t// (`keep` checks).\n\t\tif (answer === \"always\") allowed.keep(grantId, request.tool);\n\t\treturn answer !== \"deny\";\n\t});\n}\n\n/**\n * The core's question about a consequential call: an AI app's call that the\n * user allowed runs without asking, with the dialog and with the page's own\n * `onConfirm` alike; any other goes to `onConfirm`, else to the dialog.\n * Only AI apps through the relay have a grant to allow for.\n */\nexport function confirmCall(\n\tmcp: TabMcp,\n\trequest: ConfirmRequest,\n\tonConfirm: ConfirmHandler | undefined,\n): Promise<ConfirmAnswer> {\n\tconst { client } = request;\n\tif (\n\t\tclient.kind === \"relay\" &&\n\t\tallowedFor(mcp).allows(client.grantId, request.tool)\n\t)\n\t\treturn Promise.resolve(true);\n\tif (onConfirm === undefined) return mcp.confirmInPage(request);\n\t// Anything but true or \"unanswered\" is a refusal.\n\treturn Promise.resolve()\n\t\t.then(() => onConfirm(request))\n\t\t.then((answer) => (answer === \"unanswered\" ? answer : answer === true));\n}\n"
6
- ],
7
- "mappings": "4JAwCA,SAAS,EAAa,CAAC,EAAmD,CACzE,GAAI,CACH,OAAO,EAAQ,KAAK,UAAU,EAAM,KAAM,CAAC,CAAC,EAC3C,KAAM,CACP,QAQK,IAAM,GAAiB,IAMxB,GAAsB,IA4BtB,EAAS,IAAI,QAoBZ,SAAS,CAAa,CAC5B,EACA,EACA,EAAgC,CAAC,EACT,CACxB,GAAI,EAAQ,OAAO,QAAS,OAAO,QAAQ,QAAQ,MAAM,EACzD,IAAI,EAAQ,EAAO,IAAI,CAAG,EAC1B,GAAI,IAAU,OACb,EAAQ,CAAE,MAAO,GAAO,QAAS,CAAC,CAAE,EACpC,EAAO,IAAI,EAAK,CAAK,EAEtB,IAAM,EAAI,EAEJ,EAAO,IAAM,CAClB,IAAM,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
- }
@@ -1,4 +0,0 @@
1
- var m=(e)=>typeof e==="object"&&e!==null&&!Array.isArray(e),y=(e)=>typeof e==="string"&&e.length<=256;function C(e){return m(e)&&y(e.name)&&y(e.host)?{name:e.name,host:e.host}:void 0}function g(e){return JSON.stringify(e,(n,o)=>m(o)?Object.fromEntries(Object.entries(o).sort(([s],[r])=>s<r?-1:s>r?1:0)):o)??""}var T=()=>performance.now();class H{#o;#n=[];#t;#e=[];constructor(e){this.#o=e,e.addEventListener("tab-mcp-extension",(n)=>this.#i(n.detail)),e.documentElement?.setAttribute("data-tab-mcp-handshake","1")}join(e){if(this.#n.push(e),this.#t!==void 0)e.set(this.#t);e.mcp.addEventListener("grantschange",()=>this.#s(e,"update")),this.#s(e,"hello")}#s(e,n){let o=this.#o.defaultView?.CustomEvent??CustomEvent;this.#o.dispatchEvent(new o("tab-mcp-page",{detail:JSON.stringify({v:1,type:n,library:e.version,app:e.app,apps:e.mcp.grants.slice(0,64).map((s)=>({name:s.client.name,host:s.client.host}))})}))}#i(e){let n;try{n=typeof e==="string"?JSON.parse(e):void 0}catch{return}if(!m(n)||n.v!==1)return;if(n.type==="vouch")this.#a(n);else if(n.type==="withdraw")this.#e=this.#e.filter((o)=>o.nonce!==n.nonce);else if(n.type==="hello"||n.type==="update"){let o=R(n);if(o===null)return;if(g(o)!==g(this.#t)){this.#t=o;for(let s of this.#n)s.set(o)}if(n.type==="hello")for(let s of this.#n)this.#s(s,"update")}}#a(e){let n=C(e.app),{tool:o,nonce:s}=e;if(n===void 0||typeof o!=="string"||typeof s!=="string"||!m(e.arguments))return;if(this.#r(),this.#e.some((r)=>r.nonce===s))return;for(let r of this.#n){let i=r.trust?r.registry.get(o):void 0;if(i===void 0)continue;if(this.#e.push({tool:i,args:g(e.arguments),nonce:s,app:n,at:T()}),this.#e.length>16)this.#e.shift();return}}#r(){let e=T();this.#e=this.#e.filter((n)=>e-n.at<=2000)}take(e,n){this.#r();let o=g(n),s=this.#e.findIndex((r)=>r.tool===e&&r.args===o);return s<0?void 0:this.#e.splice(s,1)[0]?.app}}function R(e){if(e.on!==!0)return e.on===!1?void 0:null;let{name:n,version:o,apps:s}=e;if(!y(n)||!y(o)||!Array.isArray(s))return null;let r=s.slice(0,64).map(C);if(r.some((i)=>i===void 0))return null;return Object.freeze({name:n,version:o,apps:Object.freeze(r.map((i)=>Object.freeze(i))),whileInFront:e.whileInFront!==!1})}var A=new WeakMap;function P(e){let n=globalThis.document;if(n===void 0||globalThis.top!==globalThis.window)return;let o=A.get(n);if(o===void 0)o=new H(n),A.set(n,o);return o.join(e),o}var O=["readOnlyHint","untrustedContentHint","consequentialHint","debugging"];function _(e,n,o,s,r,i,S){let w;Object.defineProperty(s,"extension",{get:()=>w,configurable:!0});let k;try{k=P({version:S,app:r,registry:e,trust:i.trustExtension!==!1,mcp:s,set:(t)=>{w=t,s.dispatchEvent(new CustomEvent("extensionchange",{detail:{extension:t}}))}})}catch(t){console.warn("tab-mcp: no handshake with the browser extension:",t)}let u=new Map,b=new Set,x=new Set,v=(t,a)=>{try{let l=i.onConfirm,h=globalThis.document?.location?.origin,d=(Array.isArray(t.exposedTo)?t.exposedTo:[]).filter((f)=>{try{return new URL(String(f)).origin!==h}catch{return!1}});if(a!==!0||d.length===0||l&&l.own!==!1||x.has(t.name))return;x.add(t.name),console.warn(`tab-mcp: ${t.name} asks before it runs, but documents of ${d.join(", ")} (its exposedTo) can call it through WebMCP without a question: the library asks no WebMCP caller. Refuse such calls in onConfirm, or leave exposedTo off.`)}catch{}},E=()=>{for(let[t,a]of u){if(e.get(t.name)===t)continue;a.abort(),u.delete(t)}for(let t of e.list()){let{annotations:a,callers:l}=t,h=a.consequentialHint??a.destructiveHint;if(u.has(t)||!(l?.includes("webmcp")??!0))continue;let d=new AbortController;u.set(t,d);let f={consequentialHint:h};for(let c of O)f[c]??=a[c];let{signal:p}=d;Promise.resolve().then(()=>p.aborted||n.registerTool({name:t.name,title:t.title,description:t.description,inputSchema:t.inputSchema,outputSchema:t.outputSchema,annotations:f,execute:(c,M)=>o(t,c,M?.signal,k?.take(t,c))},{signal:p,exposedTo:t.exposedTo})).then(()=>{if(!p.aborted)v(t,h)}).catch((c)=>{if(p.aborted)return;if(b.has(t.name))return;b.add(t.name),console.warn(`tab-mcp: ${t.name} is not offered through WebMCP:`,c)})}};e.onChange(E),E()}export{_ as mirror};
2
-
3
- //# debugId=2AA4983CA4EEC0B064756E2164756E21
4
- //# sourceMappingURL=webmcp-8xby3trn.min.js.map
@@ -1,10 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/confirm.ts"],
4
- "sourcesContent": [
5
- "/**\n * The confirmation of consequential tools, loaded on demand: a `<dialog>`\n * per call, one shown at a time per document, and Always allow (or Allow\n * until reload) for the tools a user allows (`always-allowed.ts`).\n */\n\nimport { allowedFor, type ConfirmHost } from \"./always-allowed.ts\";\nimport {\n\tbutton,\n\tclientLabel,\n\tel,\n\tfill,\n\tnewDialog,\n\tplural,\n\tstringsOf,\n\tvisible,\n} from \"./dialog.ts\";\nimport type { DialogStrings } from \"./strings.ts\";\nimport type { TabMcp } from \"./tab-mcp.ts\";\nimport type {\n\tConfirmAnswer,\n\tConfirmHandler,\n\tConfirmRequest,\n\tDialogOptions,\n\tLocalClient,\n\tRelayClient,\n} from \"./types.ts\";\n\n/** A request the dialog is shown for: never a WebMCP call's (DEC-215). */\nexport type AskedRequest = ConfirmRequest & {\n\tclient: RelayClient | LocalClient;\n};\n\n/**\n * The arguments as text, in full: what the user approves must be visible.\n * (The relay caps a request at 1 MiB, so this stays bounded.) Invisible and\n * direction characters are shown as escapes, so the text is still JSON for\n * the same arguments; the tool gets them unchanged. `undefined` when they\n * cannot be shown, and then \"Allow once\" is not offered.\n */\nfunction showArguments(args: Record<string, unknown>): string | undefined {\n\ttry {\n\t\treturn visible(JSON.stringify(args, null, 2));\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\n/**\n * How long \"Allow once\" stays disabled after the confirmation is shown, while the\n * page is visible and focused (CU 17: a constant; tests pass `allowDelayMs`).\n */\nexport const ALLOW_DELAY_MS = 750;\n\n/** The user's answer: \"always\" allows this tool for this AI app (Always allow, or until reload). */\nexport type DialogAnswer = \"allow\" | \"always\" | \"deny\";\n\n/** Arguments up to this many characters are read out with the question. */\nconst DESCRIBED_ARGUMENTS = 500;\n\n/** Options of `confirmDialog`. */\nexport interface ConfirmDialogOptions {\n\t/** Tests only: another delay than {@link ALLOW_DELAY_MS}. */\n\tallowDelayMs?: number | undefined;\n\t/**\n\t * Offer a third button: `true` \"Always allow\" (kept until the app is\n\t * disconnected), `\"until-reload\"` \"Allow until reload\" (kept in memory).\n\t */\n\talwaysAllow?: boolean | \"until-reload\" | undefined;\n\t/** Asked when a waiting call's turn comes: `true` answers \"allow\" unseen. */\n\tallowedNow?: () => boolean;\n\tstrings?: DialogStrings;\n}\n\n/**\n * The confirmations of one document: one is shown at a time, the others\n * wait in order, so that the click that answered one cannot answer the next\n * (each starts its own delay when it is shown).\n */\ninterface ConfirmQueue {\n\tshown: boolean;\n\twaiting: (() => void)[];\n\t/** Updates the shown dialog's line about the calls that wait. */\n\tchanged?: (() => void) | undefined;\n}\n\nconst queues = new WeakMap<Document, ConfirmQueue>();\n\n/**\n * Ask whether a consequential tool may run. \"Always allow\" (or \"Allow until\n * reload\") is offered only with `alwaysAllow` (DEC-096, DEC-200); never\n * \"allow everything\": that is a deliberate act in the connect dialog, not\n * an answer to one question. One confirmation is shown at a time per\n * document; the others wait their turn, and a call cancelled while it waits\n * is never shown. \"Allow once\" and \"Always allow\" stay disabled until the\n * page has been visible and focused for 750 ms while the dialog is open and\n * is the top one, so a click meant for the page, the click that brings the\n * tab or window back, or the one that closed another dialog, cannot\n * approve; leaving the page disables them again. They also wait until the\n * arguments have been scrolled to their end. Before the delay has run out,\n * Enter or Space on a button and Escape are ignored, so typing meant for the\n * page cannot refuse either. Everything else (Don't allow, Escape, an\n * aborted call) is \"deny\". `allowedNow` is asked when a waiting call's turn\n * comes: `true` (the user allowed it meanwhile) answers \"allow\"\n * without showing it.\n */\nexport function confirmDialog(\n\trequest: AskedRequest,\n\tdoc: Document,\n\toptions: ConfirmDialogOptions = {},\n): Promise<DialogAnswer> {\n\tif (request.signal.aborted) return Promise.resolve(\"deny\");\n\tlet queue = queues.get(doc);\n\tif (queue === undefined) {\n\t\tqueue = { shown: false, waiting: [] };\n\t\tqueues.set(doc, queue);\n\t}\n\tconst q = queue;\n\t/** The shown confirmation closed: the next one, if any. */\n\tconst next = () => {\n\t\tconst start = q.waiting.shift();\n\t\tq.shown = start !== undefined;\n\t\tstart?.();\n\t\tq.changed?.();\n\t};\n\tconst show = () => {\n\t\ttry {\n\t\t\treturn showConfirm(request, doc, options, q, next);\n\t\t} catch (error) {\n\t\t\tnext(); // not shown: the queue goes on\n\t\t\treturn Promise.reject(error);\n\t\t}\n\t};\n\tif (!q.shown) {\n\t\tq.shown = true;\n\t\treturn show();\n\t}\n\treturn new Promise((resolve) => {\n\t\tconst start = () => {\n\t\t\trequest.signal.removeEventListener(\"abort\", onAbort);\n\t\t\tif (options.allowedNow?.() === true) {\n\t\t\t\tresolve(\"allow\");\n\t\t\t\tnext();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tresolve(show());\n\t\t};\n\t\tconst onAbort = () => {\n\t\t\tq.waiting.splice(q.waiting.indexOf(start), 1);\n\t\t\tq.changed?.();\n\t\t\tresolve(\"deny\");\n\t\t};\n\t\trequest.signal.addEventListener(\"abort\", onAbort, { once: true });\n\t\tq.waiting.push(start);\n\t\tq.changed?.();\n\t});\n}\n\n/** `confirmDialog` once it is this request's turn; calls `next` when it closes. */\nfunction showConfirm(\n\trequest: AskedRequest,\n\tdoc: Document,\n\toptions: ConfirmDialogOptions,\n\tqueue: ConfirmQueue,\n\tnext: () => void,\n): Promise<DialogAnswer> {\n\tconst s = options.strings ?? stringsOf(undefined);\n\tconst delay = options.allowDelayMs ?? ALLOW_DELAY_MS;\n\tconst tool = request.tool.title ?? request.tool.name;\n\tconst client = clientLabel(request.client, s);\n\tconst names = { tool, client };\n\tconst { dialog, id } = newDialog(\n\t\tdoc,\n\t\t\"tab-mcp-confirm\",\n\t\ts,\n\t\t...fill(doc, s.confirmTitle, names),\n\t);\n\tconst waiting = el(doc, \"p\", { className: \"tab-mcp-waiting\" });\n\tconst showWaiting = () => {\n\t\tconst n = queue.waiting.length;\n\t\twaiting.hidden = n === 0;\n\t\twaiting.textContent = plural(s.waiting, n, s);\n\t};\n\tqueue.changed = showWaiting;\n\tshowWaiting();\n\t// The safe choice is the default: focused, and the one that stands out.\n\tconst deny = button(\n\t\tdoc,\n\t\ts.deny,\n\t\t() => dialog.close(\"deny\"),\n\t\t\"tab-mcp-deny\",\n\t\t\"primary\",\n\t);\n\tconst approving = (text: string, answer: DialogAnswer, className: string) => {\n\t\tconst node = button(\n\t\t\tdoc,\n\t\t\ttext,\n\t\t\t() => {\n\t\t\t\tif (!node.disabled) dialog.close(answer);\n\t\t\t},\n\t\t\tclassName,\n\t\t);\n\t\treturn node;\n\t};\n\t// The approving buttons: Allow, and Always allow (or Allow until reload)\n\t// when the page offers it, saying how long it lasts.\n\tconst approve = [approving(s.allow, \"allow\", \"tab-mcp-allow\")];\n\tconst extra: HTMLElement[] = [];\n\tconst mode = options.alwaysAllow;\n\tif (mode === true || mode === \"until-reload\") {\n\t\tconst kept = mode === true;\n\t\t// Left of Allow: not where most platforms put the default (CU 11).\n\t\tapprove.unshift(\n\t\t\tapproving(\n\t\t\t\tkept ? s.alwaysAllow : s.allowUntilReload,\n\t\t\t\t\"always\",\n\t\t\t\t\"tab-mcp-always\",\n\t\t\t),\n\t\t);\n\t\textra.push(\n\t\t\tel(\n\t\t\t\tdoc,\n\t\t\t\t\"p\",\n\t\t\t\t{ className: \"tab-mcp-always-note\" },\n\t\t\t\t...fill(doc, kept ? s.alwaysAllowNote : s.allowUntilReloadNote, names),\n\t\t\t),\n\t\t);\n\t}\n\tconst actions = el(\n\t\tdoc,\n\t\t\"div\",\n\t\t{ className: \"tab-mcp-actions\" },\n\t\tdeny,\n\t\t...approve.flatMap((b) => [\" \", b]),\n\t);\n\t// Approving needs both: the delay ran out while the user saw the page\n\t// (`settled`), and the arguments were seen to their end (`seen`).\n\tlet settled = false;\n\tlet seen = false;\n\tconst enable = () => {\n\t\tfor (const b of approve) b.disabled = !(settled && seen);\n\t};\n\tenable();\n\tconst shown = showArguments(request.arguments);\n\tconst args = el(doc, \"pre\", {\n\t\tclassName: \"tab-mcp-arguments\",\n\t\tid: `${id}-arguments`,\n\t\ttext: shown ?? s.argumentsHidden,\n\t});\n\tif (shown === undefined) {\n\t\tfor (const node of [...approve, ...extra]) node.hidden = true;\n\t}\n\targs.dir = \"ltr\";\n\t// Inline, so the arguments stay readable in full without the stylesheet:\n\t// wrapped (also a long word, at 360 px), scrolling when long.\n\targs.style.setProperty(\"white-space\", \"pre-wrap\");\n\targs.style.setProperty(\"overflow-wrap\", \"anywhere\");\n\targs.style.setProperty(\n\t\t\"max-height\",\n\t\t\"var(--tab-mcp-arguments-height, min(50vh, 24rem))\",\n\t);\n\targs.style.setProperty(\"overflow\", \"auto\");\n\targs.hidden = Object.keys(request.arguments).length === 0;\n\t// The caller chooses the order of the keys, so what matters may come\n\t// last: below the end of the box, the user is told, and Allow waits.\n\tconst scrollNote = el(doc, \"p\", {\n\t\tclassName: \"tab-mcp-scroll-note\",\n\t\ttext: s.scrollToAllow,\n\t});\n\tscrollNote.hidden = true;\n\tconst who = el(\n\t\tdoc,\n\t\t\"p\",\n\t\t{ id: `${id}-who` },\n\t\t...fill(doc, s.confirmWho, names),\n\t);\n\tdialog.setAttribute(\n\t\t\"aria-describedby\",\n\t\tshown !== undefined && !args.hidden && shown.length <= DESCRIBED_ARGUMENTS\n\t\t\t? `${who.id} ${args.id}`\n\t\t\t: who.id,\n\t);\n\tdialog.append(\n\t\twho,\n\t\tel(doc, \"p\", {\n\t\t\tclassName: \"tab-mcp-description\",\n\t\t\ttext: request.tool.description,\n\t\t}),\n\t\targs,\n\t\tscrollNote,\n\t\tactions,\n\t\t...extra,\n\t\twaiting,\n\t);\n\tdoc.body.append(dialog);\n\tconst view = doc.defaultView;\n\treturn new Promise((resolve, reject) => {\n\t\tconst onAbort = () => dialog.close(\"deny\");\n\t\trequest.signal.addEventListener(\"abort\", onAbort, { once: true });\n\t\tlet timer: ReturnType<typeof setTimeout> | undefined;\n\t\t// In a hidden tab, the tab's title says that the page asks (CU 9): the\n\t\t// AI app waits meanwhile. Put back when the tab is shown or the dialog\n\t\t// closes, unless the page changed its title since.\n\t\tlet title: { page: string; shown: string } | undefined;\n\t\tconst untitle = () => {\n\t\t\tif (title !== undefined && doc.title === title.shown)\n\t\t\t\tdoc.title = title.page;\n\t\t\ttitle = undefined;\n\t\t};\n\t\tconst retitle = () => {\n\t\t\tif (doc.visibilityState !== \"hidden\") untitle();\n\t\t\telse if (title === undefined) {\n\t\t\t\ttitle = { page: doc.title, shown: `${s.titlePrefix}${doc.title}` };\n\t\t\t\tdoc.title = title.shown;\n\t\t\t}\n\t\t};\n\t\t// The delay counts only while the user can see the page: tools are\n\t\t// called while the user is in the AI app, so the dialog often opens in\n\t\t// a background tab or window. Any change starts it again from zero\n\t\t// (DEC-087), and so does another dialog of the page that closes over\n\t\t// this one.\n\t\tconst arm = (event?: Event) => {\n\t\t\tif (event?.type === \"close\" && event.target === dialog) return;\n\t\t\tretitle();\n\t\t\tclearTimeout(timer);\n\t\t\ttimer = undefined;\n\t\t\tsettled = false;\n\t\t\tenable();\n\t\t\tif (\n\t\t\t\tevent?.type === \"blur\" ||\n\t\t\t\tdoc.visibilityState === \"hidden\" ||\n\t\t\t\t!doc.hasFocus()\n\t\t\t)\n\t\t\t\treturn;\n\t\t\ttimer = setTimeout(() => {\n\t\t\t\tsettled = true;\n\t\t\t\tenable();\n\t\t\t}, delay);\n\t\t};\n\t\t// Scrolled to the end, or nothing to scroll (also without layout).\n\t\tconst check = () => {\n\t\t\tif (seen) return;\n\t\t\tif (args.scrollTop + args.clientHeight < args.scrollHeight - 2) {\n\t\t\t\tscrollNote.hidden = false;\n\t\t\t\targs.tabIndex = 0; // to scroll it with the keyboard\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tseen = true;\n\t\t\tscrollNote.hidden = true;\n\t\t\tenable();\n\t\t};\n\t\t// Not answered by a key before the delay has run out.\n\t\tconst guard = (event: KeyboardEvent) => {\n\t\t\tif (settled) return;\n\t\t\tconst onButton = (event.target as Element | null)?.localName === \"button\";\n\t\t\tif (\n\t\t\t\tevent.key === \"Escape\" ||\n\t\t\t\t(onButton && (event.key === \"Enter\" || event.key === \" \"))\n\t\t\t)\n\t\t\t\tevent.preventDefault();\n\t\t};\n\t\tconst onCancel = (event: Event) => {\n\t\t\tif (!settled) event.preventDefault();\n\t\t};\n\t\tdoc.addEventListener(\"visibilitychange\", arm);\n\t\tdoc.addEventListener(\"close\", arm, true); // `close` does not bubble\n\t\tview?.addEventListener(\"focus\", arm);\n\t\tview?.addEventListener(\"blur\", arm);\n\t\tview?.addEventListener(\"resize\", check);\n\t\targs.addEventListener(\"scroll\", check);\n\t\tdialog.addEventListener(\"keydown\", guard);\n\t\tdialog.addEventListener(\"keyup\", guard);\n\t\tdialog.addEventListener(\"cancel\", onCancel);\n\t\tconst cleanup = () => {\n\t\t\tclearTimeout(timer);\n\t\t\tuntitle();\n\t\t\tdoc.removeEventListener(\"visibilitychange\", arm);\n\t\t\tdoc.removeEventListener(\"close\", arm, true);\n\t\t\tview?.removeEventListener(\"focus\", arm);\n\t\t\tview?.removeEventListener(\"blur\", arm);\n\t\t\tview?.removeEventListener(\"resize\", check);\n\t\t\trequest.signal.removeEventListener(\"abort\", onAbort);\n\t\t\tdialog.remove();\n\t\t\tif (queue.changed === showWaiting) queue.changed = undefined;\n\t\t};\n\t\tdialog.addEventListener(\n\t\t\t\"close\",\n\t\t\t() => {\n\t\t\t\tconst value = dialog.returnValue;\n\t\t\t\tcleanup();\n\t\t\t\tresolve(value === \"allow\" || value === \"always\" ? value : \"deny\");\n\t\t\t\tnext();\n\t\t\t},\n\t\t\t{ once: true },\n\t\t);\n\t\ttry {\n\t\t\tdialog.showModal();\n\t\t} catch (error) {\n\t\t\t// Never shown, so never closed: the queue must not wait for it.\n\t\t\tcleanup();\n\t\t\treject(error);\n\t\t\tnext();\n\t\t\treturn;\n\t\t}\n\t\tdeny.focus(); // the safe choice is the default\n\t\t// Counted from when the dialog is shown, not from when it was built.\n\t\tarm();\n\t\tcheck();\n\t});\n}\n\n/** Options of this chunk's `confirmInPage`. */\nexport interface ConfirmInPageOptions {\n\t/**\n\t * The third button, for an AI app through the relay (`kind: \"relay\"`):\n\t * `true` \"Always allow\" (kept in the `TabMcp`'s `storage`; \"Allow until\n\t * reload\" where it is kept in memory, as for a stand-in), `\"until-reload\"`\n\t * \"Allow until reload\", `false` none. Default: the `TabMcp`'s\n\t * `alwaysAllow`; none for a stand-in.\n\t */\n\talwaysAllow?: boolean | \"until-reload\" | undefined;\n\t/** Texts of the dialog, as `TabMcpOptions.ui`. */\n\tui?: DialogOptions | undefined;\n\t/** Tests only: another delay than {@link ALLOW_DELAY_MS}. */\n\tallowDelayMs?: number | undefined;\n}\n\n/**\n * `TabMcp.confirmInPage`: the confirmation, and the tools the user allowed\n * (`always-allowed.ts`), which run unasked. Those only for an AI app through\n * the relay: a served port has no identity to remember an answer for\n * (DEC-115, DEC-124). A WebMCP call is never shown it (an agent that drives\n * the tab would answer it itself; DEC-215): it resolves `true`.\n *\n * Code that loads this chunk itself may pass a stand-in of its own for\n * `mcp` (`ConfirmHost`: `grants`, optionally `grantschange` events). Its\n * allowances live in memory, for its grants only, until the page is\n * reloaded, and exist only where the caller offers them with\n * `options.alwaysAllow`; without it nothing is allowed and nothing is kept.\n */\nexport function confirmInPage(\n\tmcp: TabMcp | ConfirmHost,\n\trequest: ConfirmRequest,\n\tdoc: Document,\n\toptions: ConfirmInPageOptions = {},\n): Promise<boolean> {\n\tconst { client } = request;\n\tif (client.kind === \"webmcp\") return Promise.resolve(true);\n\tconst strings = stringsOf(options.ui);\n\tconst { allowDelayMs } = options;\n\tif (client.kind !== \"relay\")\n\t\treturn confirmDialog({ ...request, client }, doc, {\n\t\t\tstrings,\n\t\t\tallowDelayMs,\n\t\t}).then((answer) => answer === \"allow\");\n\tconst { grantId } = client;\n\tconst allowed = allowedFor(mcp);\n\tconst allowedNow = () => allowed.allows(grantId, request.tool);\n\tif (allowedNow()) return Promise.resolve(true);\n\tconst offer = options.alwaysAllow ?? allowed.offered;\n\t// Asked again when its turn comes: the call before it in the queue may\n\t// have been allowed meanwhile.\n\treturn confirmDialog({ ...request, client }, doc, {\n\t\tstrings,\n\t\tallowDelayMs,\n\t\t// \"Always\" only where it outlives a reload.\n\t\talwaysAllow:\n\t\t\toffer !== false &&\n\t\t\t(offer === true && allowed.kept ? true : \"until-reload\"),\n\t\tallowedNow,\n\t}).then((answer) => {\n\t\t// Only while the grant is still there: a revoked app gets nothing\n\t\t// (`keep` checks).\n\t\tif (answer === \"always\") allowed.keep(grantId, request.tool);\n\t\treturn answer !== \"deny\";\n\t});\n}\n\n/**\n * The core's question about a consequential call: an AI app's call that the\n * user allowed runs without asking, with the dialog and with the page's own\n * `onConfirm` alike; any other goes to `onConfirm`, else to the dialog.\n * Only AI apps through the relay have a grant to allow for.\n */\nexport function confirmCall(\n\tmcp: TabMcp,\n\trequest: ConfirmRequest,\n\tonConfirm: ConfirmHandler | undefined,\n): Promise<ConfirmAnswer> {\n\tconst { client } = request;\n\tif (\n\t\tclient.kind === \"relay\" &&\n\t\tallowedFor(mcp).allows(client.grantId, request.tool)\n\t)\n\t\treturn Promise.resolve(true);\n\tif (onConfirm === undefined) return mcp.confirmInPage(request);\n\t// Anything but true or \"unanswered\" is a refusal.\n\treturn Promise.resolve()\n\t\t.then(() => onConfirm(request))\n\t\t.then((answer) => (answer === \"unanswered\" ? answer : answer === true));\n}\n"
6
- ],
7
- "mappings": ";;;;;;;;;;;;;;;;;;AAwCA,SAAS,aAAa,CAAC,MAAmD;AAAA,E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