@ractive-ch/tab-mcp-client 0.18.1 → 0.19.0-rc.2
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.
- package/CHANGELOG.md +175 -0
- package/README.md +360 -68
- package/allowed.d.ts +2 -2
- package/allowed.js.map +1 -1
- package/always-allowed.d.ts +15 -15
- package/cdn/allowed.min.js +2 -2
- package/cdn/allowed.min.js.map +2 -2
- package/cdn/confirm-ba3dkf9p.min.js +4 -0
- package/cdn/confirm-ba3dkf9p.min.js.map +10 -0
- package/cdn/connect-my0pbqd7.min.js +6 -0
- package/cdn/connect-my0pbqd7.min.js.map +12 -0
- package/cdn/connection-yf2x9zz2.min.js +17 -0
- package/cdn/{connection-0xqwz17v.min.js.map → connection-yf2x9zz2.min.js.map} +5 -5
- package/cdn/strings.min.js +4 -0
- package/cdn/strings.min.js.map +9 -0
- package/cdn/{tab-mcp-client-2vx9mcww.min.js → tab-mcp-client-8r6h3wza.min.js} +9 -8
- package/cdn/tab-mcp-client-8r6h3wza.min.js.map +11 -0
- package/cdn/tab-mcp-client-nnxcwrxp.min.js +5 -0
- package/cdn/tab-mcp-client-nnxcwrxp.min.js.map +11 -0
- package/cdn/{tab-mcp-client-5j3brdf3.min.js → tab-mcp-client-q758jr4g.min.js} +4 -4
- package/cdn/tab-mcp-client-q758jr4g.min.js.map +10 -0
- package/cdn/tab-mcp-client.min.js +3 -3
- package/cdn/tab-mcp-client.min.js.map +4 -4
- package/cdn/webmcp-84k58gsw.min.js +4 -0
- package/cdn/webmcp-84k58gsw.min.js.map +11 -0
- package/{confirm-bqfdrx3f.js → confirm-hymzwx3b.js} +7 -5
- package/confirm-hymzwx3b.js.map +10 -0
- package/confirm.d.ts +2 -2
- package/{connect-ztsgybpq.js → connect-fhtjdmf3.js} +75 -37
- package/connect-fhtjdmf3.js.map +10 -0
- package/connect.d.ts +7 -6
- package/{connection-gang7f5x.js → connection-73fbg99k.js} +70 -8
- package/connection-73fbg99k.js.map +16 -0
- package/connection.d.ts +23 -2
- package/dialog.d.ts +19 -9
- package/extension.d.ts +373 -0
- package/extension.js +415 -0
- package/extension.js.map +10 -0
- package/handshake.d.ts +82 -0
- package/index-00p8b969.js +221 -0
- package/index-00p8b969.js.map +11 -0
- package/index-1tssct09.js +58 -0
- package/index-1tssct09.js.map +11 -0
- package/{index-c1c266tm.js → index-7qqhn3zv.js} +28 -110
- package/index-7qqhn3zv.js.map +10 -0
- package/index-s31xrfa2.js.map +1 -1
- package/index.d.ts +1 -1
- package/index.js +59 -64
- package/index.js.map +4 -4
- package/internal.d.ts +2 -0
- package/mcp.d.ts +5 -3
- package/package.json +11 -2
- package/protocol.d.ts +11 -1
- package/react.d.ts +11 -4
- package/react.js +57 -10
- package/react.js.map +5 -4
- package/secret.d.ts +1 -1
- package/setup.d.ts +4 -1
- package/setup.js +3 -2
- package/setup.js.map +1 -1
- package/strings.d.ts +149 -28
- package/strings.js +13 -0
- package/strings.js.map +9 -0
- package/tab-mcp.d.ts +30 -7
- package/text.d.ts +39 -0
- package/types.d.ts +76 -17
- package/webmcp-d2ec3v3g.js +221 -0
- package/webmcp-d2ec3v3g.js.map +11 -0
- package/webmcp.d.ts +17 -4
- package/cdn/confirm-d722a9eb.min.js +0 -4
- package/cdn/confirm-d722a9eb.min.js.map +0 -10
- package/cdn/connect-x6ws6wqt.min.js +0 -6
- package/cdn/connect-x6ws6wqt.min.js.map +0 -12
- package/cdn/connection-0xqwz17v.min.js +0 -17
- package/cdn/tab-mcp-client-2vx9mcww.min.js.map +0 -12
- package/cdn/tab-mcp-client-5j3brdf3.min.js.map +0 -10
- package/cdn/webmcp-khy4f5rj.min.js +0 -4
- package/cdn/webmcp-khy4f5rj.min.js.map +0 -10
- package/confirm-bqfdrx3f.js.map +0 -10
- package/connect-ztsgybpq.js.map +0 -10
- package/connection-gang7f5x.js.map +0 -16
- package/index-5jyqehpk.js +0 -63
- package/index-5jyqehpk.js.map +0 -11
- package/index-c1c266tm.js.map +0 -11
- package/webmcp-zq1enyz0.js +0 -57
- package/webmcp-zq1enyz0.js.map +0 -10
package/index-s31xrfa2.js.map
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/always-allowed.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"/**\n * The tools that run without asking, per AI app (grant), DEC-096 and\n * DEC-200: the ones the user allowed one by one, each with what the user\n * saw of it (title and description; a tool that changed since is asked\n * again and leaves the list), and \"everything\", which covers every tool of\n * the page, also ones the user has not seen.\n *\n * Kept in the page's `storage` under the secret's key with `#allowed`, so\n * that they outlive a reload; in memory with `alwaysAllow: \"until-reload\"`,\n * with `storage: null` and without a relay. Storage is read first on every\n * question, so that a change in another tab of the same address counts at\n * once (only one tab at a time holds the connection, DEC-095).\n *\n * An allowance ends with its grant, and only on what the relay says: the\n * grants of a new session, a removed grant, `newAddress()` (the relay\n * revoked them all). A dropped connection, or a page that has not connected\n * yet, keeps them: `grantschange` does not fire then. They also belong to\n * the address secret they were made under, so a new secret (`newAddress()`\n * here or in another tab) ends them even where nothing listened.\n *\n * Out of the core: the confirmation (`confirm.ts`, which the core loads for\n * every consequential call), the connect dialog and\n * `@ractive-ch/tab-mcp-client/allowed` import it.\n */\n\nimport type { ToolRegistry } from \"./registry.ts\";\nimport type { TabMcp } from \"./tab-mcp.ts\";\nimport type { ConfirmRequest, SecretStorage } from \"./types.ts\";\n\n/** What the user saw of a tool: its name, title and description. */\ntype ToolSeen = ConfirmRequest[\"tool\"];\n/** A tool as the registry or a call names it. */\ntype ToolNow = {\n\tname: string;\n\ttitle?: string | undefined;\n\tdescription: string;\n};\n\n/** A tool that asks before it runs, for a page's switch. */\nexport interface AllowedTool {\n\tname: string;\n\ttitle?: string;\n\tdescription: string;\n\t/** Marked `destructiveHint`: it may delete or overwrite. */\n\tdestructive: boolean;\n}\n\n/**\n * The tools that run without asking, per connected AI app (`Grant.grantId`),\n * for a page that draws its own switches: `allowedTools(mcp)` of\n * `@ractive-ch/tab-mcp-client/allowed`. The library's dialogs use the same.\n * The methods are bound: `const { allow } = allowedTools(mcp)` works. The\n * queries (`tools`, `list`, `has`, `everything`) only read: safe while\n * drawing.\n */\nexport interface AllowedTools {\n\t/**\n\t * `true`: kept in the page's `storage` until the app is disconnected;\n\t * `false`: in memory, until the page is reloaded or the app is\n\t * disconnected (`alwaysAllow: \"until-reload\"`, `storage: null`).\n\t */\n\treadonly kept: boolean;\n\t/**\n\t * The page's tools that ask before they run (`consequentialHint`, callable\n\t * by AI apps through the relay), in the order registered.\n\t */\n\ttools(): AllowedTool[];\n\t/**\n\t * The names of the tools allowed one by one for this app, in the order\n\t * allowed; also one that is not registered right now (it runs without\n\t * asking again once it is back unchanged). One registered now with another\n\t * title or description than the user allowed is not in it (it asks).\n\t */\n\tlist(grantId: string): string[];\n\t/**\n\t * Whether a call of this tool by this app runs without asking now: the\n\t * app is in `mcp.grants`, and \"everything\" is on or the tool is allowed\n\t * and registered unchanged.\n\t */\n\thas(grantId: string, name: string): boolean;\n\t/**\n\t * Allow one tool for this app from now on, as it is registered now (its\n\t * title and description). Nothing happens for an app that is not in\n\t * `mcp.grants` or a tool that is not in `tools()`.\n\t */\n\tallow(grantId: string, name: string): void;\n\t/**\n\t * Ask again before this app runs this tool. While \"everything\" is on for\n\t * the app, that goes off too (asking again errs towards asking); the\n\t * other tools allowed one by one stay allowed.\n\t */\n\task(grantId: string, name: string): void;\n\t/** Whether \"Allow everything, no questions asked\" is on for this app. */\n\teverything(grantId: string): boolean;\n\t/**\n\t * \"Allow everything, no questions asked\" for this app: every tool runs\n\t * without asking, with any arguments, also tools registered later, changed\n\t * ones and destructive ones. Turning it on does nothing for an app that\n\t * is not in `mcp.grants`.\n\t */\n\tsetEverything(grantId: string, on: boolean): void;\n\t/** Ask again for everything of this app: its tools and \"everything\". */\n\tforget(grantId: string): void;\n\t/**\n\t * Calls `listener` on every change of the allowances (also one in\n\t * another tab of the same address) and of `tools()`; returns what stops it.\n\t */\n\tonChange(listener: () => void): () => void;\n}\n\n/** One app's allowances. */\ninterface Entry {\n\teverything: boolean;\n\t/** In the order allowed; `title` and `description` as the user saw them. */\n\ttools: ToolSeen[];\n}\n\n/** What is kept: the grants' entries and the secret they belong to. */\ninterface State {\n\t/** {@link fingerprint} of the address secret. */\n\tsecret: string;\n\tgrants: Map<string, Entry>;\n}\n\n/**\n * A short fingerprint of the address secret (FNV-1a, 32 bits): enough to\n * tell that the secret changed, too short to say anything about it.\n */\nfunction fingerprint(secret: string | undefined): string {\n\tif (secret === undefined) return \"\";\n\tlet hash = 0x811c9dc5;\n\tfor (let i = 0; i < secret.length; i++)\n\t\thash = Math.imul(hash ^ secret.charCodeAt(i), 0x01000193) >>> 0;\n\treturn hash.toString(16);\n}\n\nconst isString = (v: unknown): v is string => typeof v === \"string\";\n\n/**\n * The kept text as a state; anything that is not exactly what\n * {@link serialize} writes allows nothing.\n */\nfunction parse(text: string): State {\n\tconst none: State = { secret: \"\", grants: new Map() };\n\tlet value: unknown;\n\ttry {\n\t\tvalue = JSON.parse(text);\n\t} catch {\n\t\treturn none;\n\t}\n\tconst v = value as { secret?: unknown; grants?: unknown } | null;\n\tif (!isString(v?.secret) || !Array.isArray(v.grants)) return none;\n\tconst grants = new Map<string, Entry>();\n\tfor (const g of v.grants as unknown[]) {\n\t\tconst e = g as {\n\t\t\tgrantId?: unknown;\n\t\t\teverything?: unknown;\n\t\t\ttools?: unknown;\n\t\t} | null;\n\t\tif (\n\t\t\t!isString(e?.grantId) ||\n\t\t\ttypeof e.everything !== \"boolean\" ||\n\t\t\t!Array.isArray(e.tools)\n\t\t)\n\t\t\treturn none;\n\t\tconst tools: ToolSeen[] = [];\n\t\tfor (const t of e.tools as unknown[]) {\n\t\t\tconst tool = t as Partial<Record<keyof ToolSeen, unknown>> | null;\n\t\t\tif (\n\t\t\t\t!isString(tool?.name) ||\n\t\t\t\t!isString(tool.description) ||\n\t\t\t\t(tool.title !== undefined && !isString(tool.title))\n\t\t\t)\n\t\t\t\treturn none;\n\t\t\ttools.push(seenOf(tool as ToolSeen));\n\t\t}\n\t\tgrants.set(e.grantId, { everything: e.everything, tools });\n\t}\n\treturn { secret: v.secret, grants };\n}\n\n/** Grant ids, tool names and what the user saw of each: no arguments, no results. */\nfunction serialize(state: State): string {\n\treturn JSON.stringify({\n\t\tsecret: state.secret,\n\t\tgrants: [...state.grants].map(([grantId, e]) => ({ grantId, ...e })),\n\t});\n}\n\n/** Only what the user saw, as plain strings. */\nfunction seenOf(tool: ToolSeen): ToolSeen {\n\tconst seen: ToolSeen = { name: tool.name, description: tool.description };\n\tif (tool.title !== undefined) seen.title = tool.title;\n\treturn seen;\n}\n\nconst sameSeen = (a: ToolSeen, b: ToolNow) =>\n\ta.title === b.title && a.description === b.description;\n\n/**\n * Whether a tool allowed as `kept` still counts against what is registered\n * now (`now`, `undefined` when it is not): unchanged, or not registered. The\n * one rule for drawing (`list`) and for deciding (`has`).\n */\nconst counts = (kept: ToolSeen, now: ToolNow | undefined) =>\n\tnow === undefined || sameSeen(kept, now);\n\n/**\n * A caller's own stand-in for a `TabMcp` in the confirmation chunk's\n * `confirmInPage` (a desktop app with an MCP endpoint of its own, say): the\n * AI apps it allows for, and optionally `grantschange` events (with\n * `detail.grants`) when they change. Its allowances live in memory, for\n * those grants only, until the page is reloaded.\n */\nexport interface ConfirmHost {\n\treadonly grants: readonly { readonly grantId: string }[];\n\taddEventListener?(type: string, listener: (event: Event) => void): void;\n}\n\n/** What the allowances read of their host's tools. */\ntype ToolsOf = Pick<ToolRegistry, \"list\" | \"get\" | \"onChange\">;\n\n/** A stand-in has no tools to list: a tool is allowed as its question showed it. */\nconst NO_TOOLS: ToolsOf = {\n\tlist: () => [],\n\tget: () => undefined,\n\tonChange: () => () => {},\n};\n\nconst isFunction = (v: unknown) => typeof v === \"function\";\n\n/**\n * Whether `value` is what a `TabMcp`'s `_allowed` is: its registry (with\n * `register`), its secret store or none, its `alwaysAllow`. Anything else\n * (a stand-in that copied part of it, as some did for 0.18.0) is a stand-in.\n */\nfunction isInternals(value: unknown): value is TabMcp[\"_allowed\"] {\n\tif (!Array.isArray(value) || value.length !== 3) return false;\n\tconst [registry, secrets, alwaysAllow] = value as unknown[];\n\tconst r = registry as Partial<Record<string, unknown>> | null;\n\tconst s = secrets as { current?: unknown } | null | undefined;\n\treturn (\n\t\t[\"register\", \"list\", \"get\", \"onChange\"].every((k) => isFunction(r?.[k])) &&\n\t\t(s === undefined || isFunction(s?.current)) &&\n\t\t[undefined, true, false, \"until-reload\"].includes(alwaysAllow as never)\n\t);\n}\n\n/** The host's tools, secret store and `alwaysAllow`: a `TabMcp`'s, or none for a stand-in. */\nfunction internalsOf(\n\thost: TabMcp | ConfirmHost,\n): TabMcp[\"_allowed\"] | [ToolsOf, undefined, undefined] {\n\tconst internals = (host as { _allowed?: unknown })._allowed;\n\treturn isInternals(internals) ? internals : [NO_TOOLS, undefined, undefined];\n}\n\n/**\n * The allowances of one `TabMcp` (or a caller's stand-in, `ConfirmHost`).\n * The public methods are arrow fields, so that they work taken off the\n * object; the queries never write.\n */\nexport class Allowances implements AllowedTools {\n\treadonly kept: boolean;\n\t/** The library's dialogs offer allowing (`alwaysAllow` on). */\n\treadonly offered: boolean;\n\treadonly #mcp: TabMcp | ConfirmHost;\n\treadonly #registry: ToolsOf;\n\treadonly #secret: () => string;\n\t/** `null`: in memory only. */\n\treadonly #storage: SecretStorage | null;\n\treadonly #key: string;\n\t#memory: State = { secret: \"\", grants: new Map() };\n\t/**\n\t * The last write to storage failed (quota, blocked): memory holds what the\n\t * user chose, for the page's lifetime, and storage is not read over it\n\t * until a write succeeds again.\n\t */\n\t#unsaved = false;\n\treadonly #listeners = new Set<() => void>();\n\t#unwatch: (() => void) | undefined;\n\n\tconstructor(mcp: TabMcp | ConfirmHost) {\n\t\tconst [registry, secrets, alwaysAllow] = internalsOf(mcp);\n\t\tthis.#mcp = mcp;\n\t\tthis.#registry = registry;\n\t\tthis.#secret = () => fingerprint(secrets?.current());\n\t\tthis.#storage =\n\t\t\talwaysAllow === \"until-reload\" ? null : (secrets?.storage ?? null);\n\t\tthis.#key = `${secrets?.key}#allowed`;\n\t\tthis.kept = this.#storage !== null;\n\t\tthis.offered = alwaysAllow === true || alwaysAllow === \"until-reload\";\n\t}\n\n\treadonly tools = (): AllowedTool[] =>\n\t\tthis.#registry\n\t\t\t.list()\n\t\t\t.filter(\n\t\t\t\t(t) =>\n\t\t\t\t\tt.annotations.consequentialHint === true &&\n\t\t\t\t\t(t.callers?.includes(\"relay\") ?? true),\n\t\t\t)\n\t\t\t.map((t) => {\n\t\t\t\tconst tool: AllowedTool = {\n\t\t\t\t\tname: t.name,\n\t\t\t\t\tdescription: t.description,\n\t\t\t\t\tdestructive: t.annotations.destructiveHint === true,\n\t\t\t\t};\n\t\t\t\tif (t.title !== undefined) tool.title = t.title;\n\t\t\t\treturn tool;\n\t\t\t});\n\n\treadonly list = (grantId: string): string[] =>\n\t\t(this.#read().get(grantId)?.tools ?? [])\n\t\t\t.filter((t) => counts(t, this.#registry.get(t.name)))\n\t\t\t.map((t) => t.name);\n\n\treadonly has = (grantId: string, name: string): boolean => {\n\t\tconst tool = this.#registry.get(name);\n\t\treturn (\n\t\t\tthis.#granted(grantId) &&\n\t\t\t(this.everything(grantId) ||\n\t\t\t\t(tool !== undefined && this.allows(grantId, tool)))\n\t\t);\n\t};\n\n\t/**\n\t * Whether this call runs without asking: \"everything\", or this tool as\n\t * the user saw it. Only reads: a tool that changed since asks, and leaves\n\t * the list when the registry or another tab's change is seen\n\t * (`toolsChanged`).\n\t */\n\tallows(grantId: string, tool: ToolNow): boolean {\n\t\t// Only for an app the host lists at this moment: a stand-in may drop\n\t\t// one from `grants` without an event, and for a `TabMcp` a call comes\n\t\t// only while the relay's list is current, so it costs nothing there.\n\t\tif (!this.#granted(grantId)) return false;\n\t\tconst entry = this.#read().get(grantId);\n\t\tif (entry === undefined) return false;\n\t\tif (entry.everything) return true;\n\t\tconst kept = entry.tools.find((t) => t.name === tool.name);\n\t\treturn kept !== undefined && sameSeen(kept, tool);\n\t}\n\n\treadonly allow = (grantId: string, name: string): void => {\n\t\tconst tool = this.tools().find((t) => t.name === name);\n\t\tif (tool !== undefined) this.keep(grantId, tool);\n\t};\n\n\t/** Allow `tool` as the user saw it, for an app the relay still lists. */\n\tkeep(grantId: string, tool: ToolSeen): void {\n\t\tif (!this.#granted(grantId)) return;\n\t\tconst grants = this.#read();\n\t\tconst entry = grants.get(grantId) ?? { everything: false, tools: [] };\n\t\tentry.tools = [\n\t\t\t...entry.tools.filter((t) => t.name !== tool.name),\n\t\t\tseenOf(tool),\n\t\t];\n\t\tgrants.set(grantId, entry);\n\t\tthis.#write(grants);\n\t}\n\n\treadonly ask = (grantId: string, name: string): void => {\n\t\tconst grants = this.#read();\n\t\tconst entry = grants.get(grantId);\n\t\tif (entry === undefined) return;\n\t\tconst tools = entry.tools.filter((t) => t.name !== name);\n\t\t// Under \"everything\" this tool ran without asking too: asking again\n\t\t// for it turns that off; the tools allowed one by one stay.\n\t\tif (!entry.everything && tools.length === entry.tools.length) return;\n\t\tentry.everything = false;\n\t\tentry.tools = tools;\n\t\tif (tools.length === 0) grants.delete(grantId);\n\t\tthis.#write(grants);\n\t};\n\n\treadonly everything = (grantId: string): boolean =>\n\t\tthis.#read().get(grantId)?.everything === true;\n\n\treadonly setEverything = (grantId: string, on: boolean): void => {\n\t\tif (on && !this.#granted(grantId)) return;\n\t\tconst grants = this.#read();\n\t\tconst entry = grants.get(grantId) ?? { everything: false, tools: [] };\n\t\tif (entry.everything === on) return;\n\t\tentry.everything = on;\n\t\tif (on || entry.tools.length > 0) grants.set(grantId, entry);\n\t\telse grants.delete(grantId);\n\t\tthis.#write(grants);\n\t};\n\n\treadonly forget = (grantId: string): void => {\n\t\tconst grants = this.#read();\n\t\tif (grants.delete(grantId)) this.#write(grants);\n\t};\n\n\t/**\n\t * Only the grants the relay lists now: the others ended. What was kept\n\t * under another secret is dropped from storage too (it allowed nothing\n\t * already).\n\t */\n\tkeepGrants(grantIds: Iterable<string>): void {\n\t\tconst keep = new Set(grantIds);\n\t\tconst grants = this.#read();\n\t\t// Kept under another secret: nothing to write unless something was kept.\n\t\tlet changed =\n\t\t\tthis.#memory.grants !== grants && this.#memory.grants.size > 0;\n\t\tfor (const grantId of [...grants.keys()])\n\t\t\tif (!keep.has(grantId)) changed = grants.delete(grantId) || changed;\n\t\tif (changed) this.#write(grants);\n\t}\n\n\t/**\n\t * The registry changed, or another tab changed the allowances: a tool\n\t * registered with another title or description than the user saw leaves\n\t * every list. One that is not registered stays (a reload registers them\n\t * all again, a page may offer some only on some views). Tells the\n\t * listeners in any case.\n\t */\n\ttoolsChanged(): void {\n\t\tconst grants = this.#read();\n\t\tlet changed = false;\n\t\tfor (const [grantId, entry] of [...grants]) {\n\t\t\tconst tools = entry.tools.filter((t) =>\n\t\t\t\tcounts(t, this.#registry.get(t.name)),\n\t\t\t);\n\t\t\tif (tools.length === entry.tools.length) continue;\n\t\t\tchanged = true;\n\t\t\tentry.tools = tools;\n\t\t\tif (!entry.everything && tools.length === 0) grants.delete(grantId);\n\t\t}\n\t\tif (changed) this.#write(grants);\n\t\telse this.#changed();\n\t}\n\n\treadonly onChange = (listener: () => void): (() => void) => {\n\t\tthis.#listeners.add(listener);\n\t\tthis.#watch();\n\t\treturn () => {\n\t\t\tthis.#listeners.delete(listener);\n\t\t\tif (this.#listeners.size === 0) {\n\t\t\t\tthis.#unwatch?.();\n\t\t\t\tthis.#unwatch = undefined;\n\t\t\t}\n\t\t};\n\t};\n\n\t/**\n\t * Another tab changed them: clean up against this page's tools and tell\n\t * the listeners (only while there are some).\n\t */\n\t#watch(): void {\n\t\tif (this.#unwatch !== undefined || this.#storage === null) return;\n\t\tconst g = globalThis as Partial<EventTarget>;\n\t\tconst changed = (event: Event) => {\n\t\t\tif ((event as StorageEvent).key === this.#key) this.toolsChanged();\n\t\t};\n\t\tg.addEventListener?.(\"storage\", changed);\n\t\tthis.#unwatch = () => g.removeEventListener?.(\"storage\", changed);\n\t}\n\n\t#granted(grantId: string): boolean {\n\t\treturn this.#mcp.grants.some((g) => g.grantId === grantId);\n\t}\n\n\t/**\n\t * The allowances now: storage first, so that another tab's change counts;\n\t * memory where storage cannot be read or the last write to it failed.\n\t * None once the secret changed.\n\t */\n\t#read(): Map<string, Entry> {\n\t\tif (this.#storage !== null && !this.#unsaved) {\n\t\t\ttry {\n\t\t\t\tconst text = this.#storage.getItem(this.#key);\n\t\t\t\tif (text !== null) this.#memory = parse(text);\n\t\t\t} catch {\n\t\t\t\t// unreadable: what this page kept in memory\n\t\t\t}\n\t\t}\n\t\treturn this.#memory.secret === this.#secret()\n\t\t\t? this.#memory.grants\n\t\t\t: new Map();\n\t}\n\n\t#write(grants: Map<string, Entry>): void {\n\t\tthis.#memory = { secret: this.#secret(), grants };\n\t\ttry {\n\t\t\tthis.#storage?.setItem(this.#key, serialize(this.#memory));\n\t\t\tthis.#unsaved = false;\n\t\t} catch {\n\t\t\t// quota or blocked: kept in memory for the page's lifetime\n\t\t\tthis.#unsaved = true;\n\t\t}\n\t\tthis.#changed();\n\t}\n\n\t#changed(): void {\n\t\tfor (const listener of [...this.#listeners]) listener();\n\t}\n}\n\n/** The allowances per `TabMcp` or stand-in. */\nconst allowances = new WeakMap<TabMcp | ConfirmHost, Allowances>();\n\n/**\n * The allowances of `mcp`, made on first use. They follow its grants (only\n * when the relay names them, see above; a stand-in's `grantschange`) and\n * its tools.\n */\nexport function allowedFor(mcp: TabMcp | ConfirmHost): Allowances {\n\tconst kept = allowances.get(mcp);\n\tif (kept !== undefined) return kept;\n\tconst made = new Allowances(mcp);\n\tallowances.set(mcp, made);\n\t// Against the host's grants as they are now, whatever the event carries\n\t// (a stand-in may send a plain Event; a TabMcp's detail is its grants).\n\tmcp.addEventListener?.(\"grantschange\", () =>\n\t\tmade.keepGrants(mcp.grants.map((g) => g.grantId)),\n\t);\n\tinternalsOf(mcp)[0].onChange(() => made.toolsChanged());\n\tmade.toolsChanged(); // tools may have changed since the last page kept them\n\t// Made while connected: the grants are the relay's, the others ended.\n\tif (\"session\" in mcp && mcp.session !== undefined)\n\t\tmade.keepGrants(mcp.grants.map((g) => g.grantId));\n\treturn made;\n}\n"
|
|
5
|
+
"/**\n * The tools that run without asking, per AI app (grant), DEC-096 and\n * DEC-200: the ones the user allowed one by one, each with what the user\n * saw of it (title and description; a tool that changed since is asked\n * again and leaves the list), and \"everything\", which covers every tool of\n * the page, also ones the user has not seen.\n *\n * Kept in the page's `storage` under the secret's key with `#allowed`, so\n * that they outlive a reload; in memory with `alwaysAllow: \"until-reload\"`,\n * with `storage: null` and without a relay. Storage is read first on every\n * question, so that a change in another tab of the same address counts at\n * once (only one tab at a time holds the connection, DEC-095).\n *\n * An allowance ends with its grant, and only on what the relay says: the\n * grants of a new session, a removed grant, `disconnectAll()` (the relay\n * revoked them all). A dropped connection, or a page that has not connected\n * yet, keeps them: `grantschange` does not fire then. They also belong to\n * the address secret they were made under, so a new secret (`disconnectAll()`\n * here or in another tab) ends them even where nothing listened.\n *\n * Out of the core: the confirmation (`confirm.ts`, which the core loads for\n * every consequential call), the connect dialog and\n * `@ractive-ch/tab-mcp-client/allowed` import it.\n */\n\nimport type { ToolRegistry } from \"./registry.ts\";\nimport type { TabMcp } from \"./tab-mcp.ts\";\nimport type { ConfirmRequest, SecretStorage } from \"./types.ts\";\n\n/** What the user saw of a tool: its name, title and description. */\ntype ToolSeen = ConfirmRequest[\"tool\"];\n/** A tool as the registry or a call names it. */\ntype ToolNow = {\n\tname: string;\n\ttitle?: string | undefined;\n\tdescription: string;\n};\n\n/** A tool that asks before it runs, for a page's switch. */\nexport interface AllowedTool {\n\tname: string;\n\ttitle?: string;\n\tdescription: string;\n\t/** Marked `destructiveHint`: it may delete or overwrite. */\n\tdestructive: boolean;\n}\n\n/**\n * The tools that run without asking, per connected AI app (`Grant.grantId`),\n * for a page that draws its own switches: `allowedTools(mcp)` of\n * `@ractive-ch/tab-mcp-client/allowed`. The library's dialogs use the same.\n * The methods are bound: `const { allow } = allowedTools(mcp)` works. The\n * queries (`tools`, `list`, `has`, `everything`) only read: safe while\n * drawing.\n */\nexport interface AllowedTools {\n\t/**\n\t * `true`: kept in the page's `storage` until the AI app is disconnected;\n\t * `false`: in memory, until the page is reloaded or the AI app is\n\t * disconnected (`alwaysAllow: \"until-reload\"`, `storage: null`).\n\t */\n\treadonly kept: boolean;\n\t/**\n\t * The page's tools that ask before they run (`consequentialHint`, callable\n\t * by AI apps through the relay), in the order registered.\n\t */\n\ttools(): AllowedTool[];\n\t/**\n\t * The names of the tools allowed one by one for this AI app, in the order\n\t * allowed; also one that is not registered right now (it runs without\n\t * asking again once it is back unchanged). One registered now with another\n\t * title or description than the user allowed is not in it (it asks).\n\t */\n\tlist(grantId: string): string[];\n\t/**\n\t * Whether a call of this tool by this AI app runs without asking now: the\n\t * AI app is in `mcp.grants`, and \"everything\" is on or the tool is allowed\n\t * and registered unchanged.\n\t */\n\thas(grantId: string, name: string): boolean;\n\t/**\n\t * Allow one tool for this AI app from now on, as it is registered now (its\n\t * title and description). Nothing happens for an AI app that is not in\n\t * `mcp.grants` or a tool that is not in `tools()`.\n\t */\n\tallow(grantId: string, name: string): void;\n\t/**\n\t * Ask again before this AI app runs this tool. While \"everything\" is on for\n\t * the AI app, that goes off too (asking again errs towards asking); the\n\t * other tools allowed one by one stay allowed.\n\t */\n\task(grantId: string, name: string): void;\n\t/** Whether \"Always allow everything\" is on for this AI app. */\n\teverything(grantId: string): boolean;\n\t/**\n\t * \"Always allow everything\" for this AI app: every tool runs\n\t * without asking, with any arguments, also tools registered later, changed\n\t * ones and destructive ones. Turning it on does nothing for an AI app that\n\t * is not in `mcp.grants`.\n\t */\n\tsetEverything(grantId: string, on: boolean): void;\n\t/** Ask again for everything of this AI app: its tools and \"everything\". */\n\tforget(grantId: string): void;\n\t/**\n\t * Calls `listener` on every change of the allowances (also one in\n\t * another tab of the same address) and of `tools()`; returns what stops it.\n\t */\n\tonChange(listener: () => void): () => void;\n}\n\n/** One app's allowances. */\ninterface Entry {\n\teverything: boolean;\n\t/** In the order allowed; `title` and `description` as the user saw them. */\n\ttools: ToolSeen[];\n}\n\n/** What is kept: the grants' entries and the secret they belong to. */\ninterface State {\n\t/** {@link fingerprint} of the address secret. */\n\tsecret: string;\n\tgrants: Map<string, Entry>;\n}\n\n/**\n * A short fingerprint of the address secret (FNV-1a, 32 bits): enough to\n * tell that the secret changed, too short to say anything about it.\n */\nfunction fingerprint(secret: string | undefined): string {\n\tif (secret === undefined) return \"\";\n\tlet hash = 0x811c9dc5;\n\tfor (let i = 0; i < secret.length; i++)\n\t\thash = Math.imul(hash ^ secret.charCodeAt(i), 0x01000193) >>> 0;\n\treturn hash.toString(16);\n}\n\nconst isString = (v: unknown): v is string => typeof v === \"string\";\n\n/**\n * The kept text as a state; anything that is not exactly what\n * {@link serialize} writes allows nothing.\n */\nfunction parse(text: string): State {\n\tconst none: State = { secret: \"\", grants: new Map() };\n\tlet value: unknown;\n\ttry {\n\t\tvalue = JSON.parse(text);\n\t} catch {\n\t\treturn none;\n\t}\n\tconst v = value as { secret?: unknown; grants?: unknown } | null;\n\tif (!isString(v?.secret) || !Array.isArray(v.grants)) return none;\n\tconst grants = new Map<string, Entry>();\n\tfor (const g of v.grants as unknown[]) {\n\t\tconst e = g as {\n\t\t\tgrantId?: unknown;\n\t\t\teverything?: unknown;\n\t\t\ttools?: unknown;\n\t\t} | null;\n\t\tif (\n\t\t\t!isString(e?.grantId) ||\n\t\t\ttypeof e.everything !== \"boolean\" ||\n\t\t\t!Array.isArray(e.tools)\n\t\t)\n\t\t\treturn none;\n\t\tconst tools: ToolSeen[] = [];\n\t\tfor (const t of e.tools as unknown[]) {\n\t\t\tconst tool = t as Partial<Record<keyof ToolSeen, unknown>> | null;\n\t\t\tif (\n\t\t\t\t!isString(tool?.name) ||\n\t\t\t\t!isString(tool.description) ||\n\t\t\t\t(tool.title !== undefined && !isString(tool.title))\n\t\t\t)\n\t\t\t\treturn none;\n\t\t\ttools.push(seenOf(tool as ToolSeen));\n\t\t}\n\t\tgrants.set(e.grantId, { everything: e.everything, tools });\n\t}\n\treturn { secret: v.secret, grants };\n}\n\n/** Grant ids, tool names and what the user saw of each: no arguments, no results. */\nfunction serialize(state: State): string {\n\treturn JSON.stringify({\n\t\tsecret: state.secret,\n\t\tgrants: [...state.grants].map(([grantId, e]) => ({ grantId, ...e })),\n\t});\n}\n\n/** Only what the user saw, as plain strings. */\nfunction seenOf(tool: ToolSeen): ToolSeen {\n\tconst seen: ToolSeen = { name: tool.name, description: tool.description };\n\tif (tool.title !== undefined) seen.title = tool.title;\n\treturn seen;\n}\n\nconst sameSeen = (a: ToolSeen, b: ToolNow) =>\n\ta.title === b.title && a.description === b.description;\n\n/**\n * Whether a tool allowed as `kept` still counts against what is registered\n * now (`now`, `undefined` when it is not): unchanged, or not registered. The\n * one rule for drawing (`list`) and for deciding (`has`).\n */\nconst counts = (kept: ToolSeen, now: ToolNow | undefined) =>\n\tnow === undefined || sameSeen(kept, now);\n\n/**\n * A caller's own stand-in for a `TabMcp` in the confirmation chunk's\n * `confirmInPage` (a desktop app with an MCP endpoint of its own, say): the\n * AI apps it allows for, and optionally `grantschange` events (with\n * `detail.grants`) when they change. Its allowances live in memory, for\n * those grants only, until the page is reloaded.\n */\nexport interface ConfirmHost {\n\treadonly grants: readonly { readonly grantId: string }[];\n\taddEventListener?(type: string, listener: (event: Event) => void): void;\n}\n\n/** What the allowances read of their host's tools. */\ntype ToolsOf = Pick<ToolRegistry, \"list\" | \"get\" | \"onChange\">;\n\n/** A stand-in has no tools to list: a tool is allowed as its question showed it. */\nconst NO_TOOLS: ToolsOf = {\n\tlist: () => [],\n\tget: () => undefined,\n\tonChange: () => () => {},\n};\n\nconst isFunction = (v: unknown) => typeof v === \"function\";\n\n/**\n * Whether `value` is what a `TabMcp`'s `_allowed` is: its registry (with\n * `register`), its secret store or none, its `alwaysAllow`. Anything else\n * (a stand-in that copied part of it, as some did for 0.18.0) is a stand-in.\n */\nfunction isInternals(value: unknown): value is TabMcp[\"_allowed\"] {\n\tif (!Array.isArray(value) || value.length !== 3) return false;\n\tconst [registry, secrets, alwaysAllow] = value as unknown[];\n\tconst r = registry as Partial<Record<string, unknown>> | null;\n\tconst s = secrets as { current?: unknown } | null | undefined;\n\treturn (\n\t\t[\"register\", \"list\", \"get\", \"onChange\"].every((k) => isFunction(r?.[k])) &&\n\t\t(s === undefined || isFunction(s?.current)) &&\n\t\t[undefined, true, false, \"until-reload\"].includes(alwaysAllow as never)\n\t);\n}\n\n/** The host's tools, secret store and `alwaysAllow`: a `TabMcp`'s, or none for a stand-in. */\nfunction internalsOf(\n\thost: TabMcp | ConfirmHost,\n): TabMcp[\"_allowed\"] | [ToolsOf, undefined, undefined] {\n\tconst internals = (host as { _allowed?: unknown })._allowed;\n\treturn isInternals(internals) ? internals : [NO_TOOLS, undefined, undefined];\n}\n\n/**\n * The allowances of one `TabMcp` (or a caller's stand-in, `ConfirmHost`).\n * The public methods are arrow fields, so that they work taken off the\n * object; the queries never write.\n */\nexport class Allowances implements AllowedTools {\n\treadonly kept: boolean;\n\t/** The library's dialogs offer allowing (`alwaysAllow` on). */\n\treadonly offered: boolean;\n\treadonly #mcp: TabMcp | ConfirmHost;\n\treadonly #registry: ToolsOf;\n\treadonly #secret: () => string;\n\t/** `null`: in memory only. */\n\treadonly #storage: SecretStorage | null;\n\treadonly #key: string;\n\t#memory: State = { secret: \"\", grants: new Map() };\n\t/**\n\t * The last write to storage failed (quota, blocked): memory holds what the\n\t * user chose, for the page's lifetime, and storage is not read over it\n\t * until a write succeeds again.\n\t */\n\t#unsaved = false;\n\treadonly #listeners = new Set<() => void>();\n\t#unwatch: (() => void) | undefined;\n\n\tconstructor(mcp: TabMcp | ConfirmHost) {\n\t\tconst [registry, secrets, alwaysAllow] = internalsOf(mcp);\n\t\tthis.#mcp = mcp;\n\t\tthis.#registry = registry;\n\t\tthis.#secret = () => fingerprint(secrets?.current());\n\t\tthis.#storage =\n\t\t\talwaysAllow === \"until-reload\" ? null : (secrets?.storage ?? null);\n\t\tthis.#key = `${secrets?.key}#allowed`;\n\t\tthis.kept = this.#storage !== null;\n\t\tthis.offered = alwaysAllow === true || alwaysAllow === \"until-reload\";\n\t}\n\n\treadonly tools = (): AllowedTool[] =>\n\t\tthis.#registry\n\t\t\t.list()\n\t\t\t.filter(\n\t\t\t\t(t) =>\n\t\t\t\t\tt.annotations.consequentialHint === true &&\n\t\t\t\t\t(t.callers?.includes(\"relay\") ?? true),\n\t\t\t)\n\t\t\t.map((t) => {\n\t\t\t\tconst tool: AllowedTool = {\n\t\t\t\t\tname: t.name,\n\t\t\t\t\tdescription: t.description,\n\t\t\t\t\tdestructive: t.annotations.destructiveHint === true,\n\t\t\t\t};\n\t\t\t\tif (t.title !== undefined) tool.title = t.title;\n\t\t\t\treturn tool;\n\t\t\t});\n\n\treadonly list = (grantId: string): string[] =>\n\t\t(this.#read().get(grantId)?.tools ?? [])\n\t\t\t.filter((t) => counts(t, this.#registry.get(t.name)))\n\t\t\t.map((t) => t.name);\n\n\treadonly has = (grantId: string, name: string): boolean => {\n\t\tconst tool = this.#registry.get(name);\n\t\treturn (\n\t\t\tthis.#granted(grantId) &&\n\t\t\t(this.everything(grantId) ||\n\t\t\t\t(tool !== undefined && this.allows(grantId, tool)))\n\t\t);\n\t};\n\n\t/**\n\t * Whether this call runs without asking: \"everything\", or this tool as\n\t * the user saw it. Only reads: a tool that changed since asks, and leaves\n\t * the list when the registry or another tab's change is seen\n\t * (`toolsChanged`).\n\t */\n\tallows(grantId: string, tool: ToolNow): boolean {\n\t\t// Only for an app the host lists at this moment: a stand-in may drop\n\t\t// one from `grants` without an event, and for a `TabMcp` a call comes\n\t\t// only while the relay's list is current, so it costs nothing there.\n\t\tif (!this.#granted(grantId)) return false;\n\t\tconst entry = this.#read().get(grantId);\n\t\tif (entry === undefined) return false;\n\t\tif (entry.everything) return true;\n\t\tconst kept = entry.tools.find((t) => t.name === tool.name);\n\t\treturn kept !== undefined && sameSeen(kept, tool);\n\t}\n\n\treadonly allow = (grantId: string, name: string): void => {\n\t\tconst tool = this.tools().find((t) => t.name === name);\n\t\tif (tool !== undefined) this.keep(grantId, tool);\n\t};\n\n\t/** Allow `tool` as the user saw it, for an app the relay still lists. */\n\tkeep(grantId: string, tool: ToolSeen): void {\n\t\tif (!this.#granted(grantId)) return;\n\t\tconst grants = this.#read();\n\t\tconst entry = grants.get(grantId) ?? { everything: false, tools: [] };\n\t\tentry.tools = [\n\t\t\t...entry.tools.filter((t) => t.name !== tool.name),\n\t\t\tseenOf(tool),\n\t\t];\n\t\tgrants.set(grantId, entry);\n\t\tthis.#write(grants);\n\t}\n\n\treadonly ask = (grantId: string, name: string): void => {\n\t\tconst grants = this.#read();\n\t\tconst entry = grants.get(grantId);\n\t\tif (entry === undefined) return;\n\t\tconst tools = entry.tools.filter((t) => t.name !== name);\n\t\t// Under \"everything\" this tool ran without asking too: asking again\n\t\t// for it turns that off; the tools allowed one by one stay.\n\t\tif (!entry.everything && tools.length === entry.tools.length) return;\n\t\tentry.everything = false;\n\t\tentry.tools = tools;\n\t\tif (tools.length === 0) grants.delete(grantId);\n\t\tthis.#write(grants);\n\t};\n\n\treadonly everything = (grantId: string): boolean =>\n\t\tthis.#read().get(grantId)?.everything === true;\n\n\treadonly setEverything = (grantId: string, on: boolean): void => {\n\t\tif (on && !this.#granted(grantId)) return;\n\t\tconst grants = this.#read();\n\t\tconst entry = grants.get(grantId) ?? { everything: false, tools: [] };\n\t\tif (entry.everything === on) return;\n\t\tentry.everything = on;\n\t\tif (on || entry.tools.length > 0) grants.set(grantId, entry);\n\t\telse grants.delete(grantId);\n\t\tthis.#write(grants);\n\t};\n\n\treadonly forget = (grantId: string): void => {\n\t\tconst grants = this.#read();\n\t\tif (grants.delete(grantId)) this.#write(grants);\n\t};\n\n\t/**\n\t * Only the grants the relay lists now: the others ended. What was kept\n\t * under another secret is dropped from storage too (it allowed nothing\n\t * already).\n\t */\n\tkeepGrants(grantIds: Iterable<string>): void {\n\t\tconst keep = new Set(grantIds);\n\t\tconst grants = this.#read();\n\t\t// Kept under another secret: nothing to write unless something was kept.\n\t\tlet changed =\n\t\t\tthis.#memory.grants !== grants && this.#memory.grants.size > 0;\n\t\tfor (const grantId of [...grants.keys()])\n\t\t\tif (!keep.has(grantId)) changed = grants.delete(grantId) || changed;\n\t\tif (changed) this.#write(grants);\n\t}\n\n\t/**\n\t * The registry changed, or another tab changed the allowances: a tool\n\t * registered with another title or description than the user saw leaves\n\t * every list. One that is not registered stays (a reload registers them\n\t * all again, a page may offer some only on some views). Tells the\n\t * listeners in any case.\n\t */\n\ttoolsChanged(): void {\n\t\tconst grants = this.#read();\n\t\tlet changed = false;\n\t\tfor (const [grantId, entry] of [...grants]) {\n\t\t\tconst tools = entry.tools.filter((t) =>\n\t\t\t\tcounts(t, this.#registry.get(t.name)),\n\t\t\t);\n\t\t\tif (tools.length === entry.tools.length) continue;\n\t\t\tchanged = true;\n\t\t\tentry.tools = tools;\n\t\t\tif (!entry.everything && tools.length === 0) grants.delete(grantId);\n\t\t}\n\t\tif (changed) this.#write(grants);\n\t\telse this.#changed();\n\t}\n\n\treadonly onChange = (listener: () => void): (() => void) => {\n\t\tthis.#listeners.add(listener);\n\t\tthis.#watch();\n\t\treturn () => {\n\t\t\tthis.#listeners.delete(listener);\n\t\t\tif (this.#listeners.size === 0) {\n\t\t\t\tthis.#unwatch?.();\n\t\t\t\tthis.#unwatch = undefined;\n\t\t\t}\n\t\t};\n\t};\n\n\t/**\n\t * Another tab changed them: clean up against this page's tools and tell\n\t * the listeners (only while there are some).\n\t */\n\t#watch(): void {\n\t\tif (this.#unwatch !== undefined || this.#storage === null) return;\n\t\tconst g = globalThis as Partial<EventTarget>;\n\t\tconst changed = (event: Event) => {\n\t\t\tif ((event as StorageEvent).key === this.#key) this.toolsChanged();\n\t\t};\n\t\tg.addEventListener?.(\"storage\", changed);\n\t\tthis.#unwatch = () => g.removeEventListener?.(\"storage\", changed);\n\t}\n\n\t#granted(grantId: string): boolean {\n\t\treturn this.#mcp.grants.some((g) => g.grantId === grantId);\n\t}\n\n\t/**\n\t * The allowances now: storage first, so that another tab's change counts;\n\t * memory where storage cannot be read or the last write to it failed.\n\t * None once the secret changed.\n\t */\n\t#read(): Map<string, Entry> {\n\t\tif (this.#storage !== null && !this.#unsaved) {\n\t\t\ttry {\n\t\t\t\tconst text = this.#storage.getItem(this.#key);\n\t\t\t\tif (text !== null) this.#memory = parse(text);\n\t\t\t} catch {\n\t\t\t\t// unreadable: what this page kept in memory\n\t\t\t}\n\t\t}\n\t\treturn this.#memory.secret === this.#secret()\n\t\t\t? this.#memory.grants\n\t\t\t: new Map();\n\t}\n\n\t#write(grants: Map<string, Entry>): void {\n\t\tthis.#memory = { secret: this.#secret(), grants };\n\t\ttry {\n\t\t\tthis.#storage?.setItem(this.#key, serialize(this.#memory));\n\t\t\tthis.#unsaved = false;\n\t\t} catch {\n\t\t\t// quota or blocked: kept in memory for the page's lifetime\n\t\t\tthis.#unsaved = true;\n\t\t}\n\t\tthis.#changed();\n\t}\n\n\t#changed(): void {\n\t\tfor (const listener of [...this.#listeners]) listener();\n\t}\n}\n\n/** The allowances per `TabMcp` or stand-in. */\nconst allowances = new WeakMap<TabMcp | ConfirmHost, Allowances>();\n\n/**\n * The allowances of `mcp`, made on first use. They follow its grants (only\n * when the relay names them, see above; a stand-in's `grantschange`) and\n * its tools.\n */\nexport function allowedFor(mcp: TabMcp | ConfirmHost): Allowances {\n\tconst kept = allowances.get(mcp);\n\tif (kept !== undefined) return kept;\n\tconst made = new Allowances(mcp);\n\tallowances.set(mcp, made);\n\t// Against the host's grants as they are now, whatever the event carries\n\t// (a stand-in may send a plain Event; a TabMcp's detail is its grants).\n\tmcp.addEventListener?.(\"grantschange\", () =>\n\t\tmade.keepGrants(mcp.grants.map((g) => g.grantId)),\n\t);\n\tinternalsOf(mcp)[0].onChange(() => made.toolsChanged());\n\tmade.toolsChanged(); // tools may have changed since the last page kept them\n\t// Made while connected: the grants are the relay's, the others ended.\n\tif (\"session\" in mcp && mcp.session !== undefined)\n\t\tmade.keepGrants(mcp.grants.map((g) => g.grantId));\n\treturn made;\n}\n"
|
|
6
6
|
],
|
|
7
7
|
"mappings": ";AAgIA,SAAS,WAAW,CAAC,QAAoC;AAAA,EACxD,IAAI,WAAW;AAAA,IAAW,OAAO;AAAA,EACjC,IAAI,OAAO;AAAA,EACX,SAAS,IAAI,EAAG,IAAI,OAAO,QAAQ;AAAA,IAClC,OAAO,KAAK,KAAK,OAAO,OAAO,WAAW,CAAC,GAAG,QAAU,MAAM;AAAA,EAC/D,OAAO,KAAK,SAAS,EAAE;AAAA;AAGxB,IAAM,WAAW,CAAC,MAA4B,OAAO,MAAM;AAM3D,SAAS,KAAK,CAAC,MAAqB;AAAA,EACnC,MAAM,OAAc,EAAE,QAAQ,IAAI,QAAQ,IAAI,IAAM;AAAA,EACpD,IAAI;AAAA,EACJ,IAAI;AAAA,IACH,QAAQ,KAAK,MAAM,IAAI;AAAA,IACtB,MAAM;AAAA,IACP,OAAO;AAAA;AAAA,EAER,MAAM,IAAI;AAAA,EACV,IAAI,CAAC,SAAS,GAAG,MAAM,KAAK,CAAC,MAAM,QAAQ,EAAE,MAAM;AAAA,IAAG,OAAO;AAAA,EAC7D,MAAM,SAAS,IAAI;AAAA,EACnB,WAAW,KAAK,EAAE,QAAqB;AAAA,IACtC,MAAM,IAAI;AAAA,IAKV,IACC,CAAC,SAAS,GAAG,OAAO,KACpB,OAAO,EAAE,eAAe,aACxB,CAAC,MAAM,QAAQ,EAAE,KAAK;AAAA,MAEtB,OAAO;AAAA,IACR,MAAM,QAAoB,CAAC;AAAA,IAC3B,WAAW,KAAK,EAAE,OAAoB;AAAA,MACrC,MAAM,OAAO;AAAA,MACb,IACC,CAAC,SAAS,MAAM,IAAI,KACpB,CAAC,SAAS,KAAK,WAAW,KACzB,KAAK,UAAU,aAAa,CAAC,SAAS,KAAK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AAW,GAAG;AAAA,EAC/B,WAAW,IAAI,KAAK,IAAI;AAAA,EAGxB,IAAI,mBAAmB,gBAAgB,MACtC,KAAK,WAAW,IAAI,OAAO,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CACjD;AAAA,EACA,YAAY,GAAG,EAAE,GAAG,SAAS,MAAM,KAAK,aAAa,CAAC;AAAA,EACtD,KAAK,aAAa;AAAA,EAElB,IAAI,aAAa,OAAO,IAAI,YAAY;AAAA,IACvC,KAAK,WAAW,IAAI,OAAO,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC;AAAA,EACjD,OAAO;AAAA;",
|
|
8
8
|
"debugId": "22280D6FFA5A23FE64756E2164756E21",
|
package/index.d.ts
CHANGED
|
@@ -3,5 +3,5 @@
|
|
|
3
3
|
* tab-mcp relay. This entry is the small core; `connect()` loads the rest.
|
|
4
4
|
*/
|
|
5
5
|
export { TabMcp } from "./tab-mcp.js";
|
|
6
|
-
export type { ConfirmAnswer, ConfirmHandler, ConfirmRequest, ConnectionState, ConnectOptions, DialogOptions, DialogStrings, DisconnectReason, Grant, Icon, IdentityAssertionHandler, IdentityAssertionRequest, IdentityState, JsonSchema, Limits, LocalClient, PairingCode, PairingCodeReason, ReconnectingInfo, RegisterToolOptions, RelayClient, ReplacedDetail, SecretStorage, ServeHandle, ServeOptions, ServerInfo, SessionInfo, SetupNotePart, SetupStep, StateChangeDetail, TabMcpEventMap, TabMcpOptions, Tool, ToolAnnotations, ToolCallClient, ToolCallEnd, ToolCaller, ToolCallOutcome, ToolCallResult, ToolCallStart, ToolExecuteOptions, TunnelPort, TunnelPortHandler, WebMcpClient, } from "./types.js";
|
|
6
|
+
export type { BrowserExtension, ConfirmAnswer, ConfirmHandler, ConfirmRequest, ConnectionState, ConnectOptions, DialogOptions, DialogStrings, DisconnectReason, Grant, Icon, IdentityAssertionHandler, IdentityAssertionRequest, IdentityState, JsonSchema, Limits, LocalClient, PairingCode, PairingCodeReason, ReconnectingInfo, RegisterToolOptions, RelayClient, ReplacedDetail, SecretStorage, ServeHandle, ServeOptions, ServerInfo, SessionInfo, SetupNotePart, SetupStep, StateChangeDetail, TabMcpEventMap, TabMcpOptions, Tool, ToolAnnotations, ToolCallClient, ToolCallEnd, ToolCaller, ToolCallOutcome, ToolCallResult, ToolCallStart, ToolExecuteOptions, TunnelPort, TunnelPortHandler, WebMcpClient, } from "./types.js";
|
|
7
7
|
export { VERSION } from "./version.js";
|
package/index.js
CHANGED
|
@@ -228,7 +228,7 @@ class SecretStore {
|
|
|
228
228
|
}
|
|
229
229
|
}
|
|
230
230
|
// package.json
|
|
231
|
-
var version = "0.
|
|
231
|
+
var version = "0.19.0-rc.2";
|
|
232
232
|
|
|
233
233
|
// src/version.ts
|
|
234
234
|
var LIBRARY_NAME = "@ractive-ch/tab-mcp-client";
|
|
@@ -313,8 +313,8 @@ function sessionInfo(m) {
|
|
|
313
313
|
function noRelay() {
|
|
314
314
|
return Promise.reject(new TypeError("TabMcp: this TabMcp has no relay"));
|
|
315
315
|
}
|
|
316
|
-
function changed(error,
|
|
317
|
-
return Object.assign(Object(error), {
|
|
316
|
+
function changed(error, secretChanged) {
|
|
317
|
+
return Object.assign(Object(error), { secretChanged });
|
|
318
318
|
}
|
|
319
319
|
function notConnected() {
|
|
320
320
|
return Promise.reject(new DOMException("TabMcp: not connected", "InvalidStateError"));
|
|
@@ -342,6 +342,9 @@ class TabMcp extends EventTarget {
|
|
|
342
342
|
#grants = Object.freeze([]);
|
|
343
343
|
#pairing = false;
|
|
344
344
|
#pairingCode;
|
|
345
|
+
#pairingUntil;
|
|
346
|
+
#asked = false;
|
|
347
|
+
#clicked = false;
|
|
345
348
|
#connection;
|
|
346
349
|
#connecting;
|
|
347
350
|
#generation = 0;
|
|
@@ -349,6 +352,7 @@ class TabMcp extends EventTarget {
|
|
|
349
352
|
#reconnecting;
|
|
350
353
|
#reconnectTimer;
|
|
351
354
|
#unwatch;
|
|
355
|
+
#lazy;
|
|
352
356
|
#connectDialog;
|
|
353
357
|
#calls = {
|
|
354
358
|
registry: this.#registry,
|
|
@@ -388,7 +392,7 @@ class TabMcp extends EventTarget {
|
|
|
388
392
|
this.#registry.onChange(() => this.#emit("toolschange", { tools: this.tools }));
|
|
389
393
|
const context = options.webmcp === true ? nativeContext() : undefined;
|
|
390
394
|
if (context !== undefined)
|
|
391
|
-
import("./webmcp-
|
|
395
|
+
import("./webmcp-d2ec3v3g.js").then((m) => m.mirror(this.#registry, context, (tool, input, signal, by) => this.#load().then((lazy) => lazy.callFromWebMcp(this.#calls, tool, input, signal, by)), this, app, options, VERSION), (error) => console.warn("tab-mcp: no tools are offered through WebMCP:", error));
|
|
392
396
|
}
|
|
393
397
|
registerTool(tool, options = {}) {
|
|
394
398
|
try {
|
|
@@ -473,16 +477,21 @@ class TabMcp extends EventTarget {
|
|
|
473
477
|
get pairingCode() {
|
|
474
478
|
return this.#pairingCode;
|
|
475
479
|
}
|
|
480
|
+
get pairingUntil() {
|
|
481
|
+
return this.#pairingUntil;
|
|
482
|
+
}
|
|
476
483
|
connect(options = {}) {
|
|
477
484
|
if (this.#relayUrl === undefined)
|
|
478
485
|
return noRelay();
|
|
479
486
|
if (this.#state === "connected" && this.#session !== undefined) {
|
|
480
487
|
return Promise.resolve(this.#session);
|
|
481
488
|
}
|
|
489
|
+
if (this.#reconnecting !== undefined) {
|
|
490
|
+
this.#clicked ||= options.resume !== true;
|
|
491
|
+
return this.#attemptReconnect();
|
|
492
|
+
}
|
|
482
493
|
if (this.#connecting !== undefined)
|
|
483
494
|
return this.#connecting;
|
|
484
|
-
if (this.#reconnecting !== undefined)
|
|
485
|
-
return this.#attemptReconnect();
|
|
486
495
|
const activation = globalThis.navigator?.userActivation;
|
|
487
496
|
if (options.resume !== true && activation !== undefined && !activation.isActive) {
|
|
488
497
|
return Promise.reject(new DOMException("connect() must be called from a user action, such as a click", "NotAllowedError"));
|
|
@@ -508,9 +517,10 @@ class TabMcp extends EventTarget {
|
|
|
508
517
|
return connecting;
|
|
509
518
|
}
|
|
510
519
|
async#load() {
|
|
511
|
-
const lazy = await import("./connection-
|
|
520
|
+
const lazy = await import("./connection-73fbg99k.js");
|
|
512
521
|
for (const error of lazy.compileRegistry(this.#registry))
|
|
513
522
|
this.#emit("error", { error });
|
|
523
|
+
this.#lazy = lazy;
|
|
514
524
|
return lazy;
|
|
515
525
|
}
|
|
516
526
|
async#open(generation, takeBack) {
|
|
@@ -546,7 +556,7 @@ class TabMcp extends EventTarget {
|
|
|
546
556
|
disconnect() {
|
|
547
557
|
this.#end("user");
|
|
548
558
|
}
|
|
549
|
-
|
|
559
|
+
disconnectAll() {
|
|
550
560
|
const secrets = this.#secrets;
|
|
551
561
|
if (!secrets)
|
|
552
562
|
return noRelay();
|
|
@@ -557,7 +567,7 @@ class TabMcp extends EventTarget {
|
|
|
557
567
|
async#renew(secrets) {
|
|
558
568
|
const wasOn = this.#state !== "disconnected" || this.#stayOn;
|
|
559
569
|
const old = secrets.current();
|
|
560
|
-
this.#end("
|
|
570
|
+
this.#end("disconnect_all");
|
|
561
571
|
const generation = this.#generation;
|
|
562
572
|
if (old) {
|
|
563
573
|
this.#setState("connecting");
|
|
@@ -571,7 +581,7 @@ class TabMcp extends EventTarget {
|
|
|
571
581
|
throw changed(error, false);
|
|
572
582
|
} finally {
|
|
573
583
|
if (generation === this.#generation) {
|
|
574
|
-
this.#connection?.close("
|
|
584
|
+
this.#connection?.close("disconnect_all");
|
|
575
585
|
this.#connection = undefined;
|
|
576
586
|
}
|
|
577
587
|
}
|
|
@@ -596,7 +606,7 @@ class TabMcp extends EventTarget {
|
|
|
596
606
|
if (this.#state === "disconnected")
|
|
597
607
|
return;
|
|
598
608
|
this.#session = undefined;
|
|
599
|
-
this.#
|
|
609
|
+
this.#clearPairing();
|
|
600
610
|
this.#identityRound++;
|
|
601
611
|
this.#setIdentity("none");
|
|
602
612
|
this.#setState("disconnected", reason);
|
|
@@ -638,24 +648,10 @@ class TabMcp extends EventTarget {
|
|
|
638
648
|
return attempt;
|
|
639
649
|
}
|
|
640
650
|
#watch() {
|
|
641
|
-
|
|
642
|
-
return;
|
|
643
|
-
const g = globalThis;
|
|
644
|
-
const doc = globalThis.document;
|
|
645
|
-
const now = () => {
|
|
651
|
+
this.#unwatch ??= this.#lazy?.onWake(() => {
|
|
646
652
|
if (this.#reconnectTimer !== undefined)
|
|
647
653
|
this.#attemptReconnect().catch(() => {});
|
|
648
|
-
};
|
|
649
|
-
const visible = () => {
|
|
650
|
-
if (doc?.visibilityState !== "hidden")
|
|
651
|
-
now();
|
|
652
|
-
};
|
|
653
|
-
doc?.addEventListener("visibilitychange", visible);
|
|
654
|
-
g.addEventListener?.("online", now);
|
|
655
|
-
this.#unwatch = () => {
|
|
656
|
-
doc?.removeEventListener("visibilitychange", visible);
|
|
657
|
-
g.removeEventListener?.("online", now);
|
|
658
|
-
};
|
|
654
|
+
});
|
|
659
655
|
}
|
|
660
656
|
#stopReconnecting() {
|
|
661
657
|
clearTimeout(this.#reconnectTimer);
|
|
@@ -665,11 +661,15 @@ class TabMcp extends EventTarget {
|
|
|
665
661
|
this.#reconnecting = undefined;
|
|
666
662
|
}
|
|
667
663
|
startPairing() {
|
|
668
|
-
if (this.#pairing)
|
|
669
|
-
return;
|
|
670
664
|
this.#pairing = true;
|
|
665
|
+
this.#asked = false;
|
|
671
666
|
if (this.#state === "connected")
|
|
672
|
-
this.#
|
|
667
|
+
this.#askPairing();
|
|
668
|
+
}
|
|
669
|
+
#askPairing() {
|
|
670
|
+
this.#setPairingUntil(undefined);
|
|
671
|
+
this.#asked = this.#session?.pairing === "oidc";
|
|
672
|
+
this.#connection?.send({ type: "pairing.start" });
|
|
673
673
|
}
|
|
674
674
|
stopPairing() {
|
|
675
675
|
if (!this.#pairing)
|
|
@@ -677,40 +677,18 @@ class TabMcp extends EventTarget {
|
|
|
677
677
|
this.#pairing = false;
|
|
678
678
|
if (this.#state === "connected")
|
|
679
679
|
this.#connection?.send({ type: "pairing.stop" });
|
|
680
|
+
this.#clearPairing();
|
|
681
|
+
}
|
|
682
|
+
#clearPairing() {
|
|
680
683
|
this.#setPairingCode(undefined);
|
|
684
|
+
this.#setPairingUntil(undefined);
|
|
681
685
|
}
|
|
682
686
|
revoke(grantId) {
|
|
683
687
|
const gone = () => !this.#grants.some((g) => g.grantId === grantId);
|
|
684
688
|
return gone() ? Promise.resolve() : this.#ask({ type: "grant.revoke", grantId }, gone);
|
|
685
689
|
}
|
|
686
690
|
#ask(message, done) {
|
|
687
|
-
|
|
688
|
-
return notConnected();
|
|
689
|
-
return new Promise((resolve, reject) => {
|
|
690
|
-
const types = ["grantschange", "statechange", "error"];
|
|
691
|
-
const settle = (error) => {
|
|
692
|
-
clearTimeout(timer);
|
|
693
|
-
for (const type of types)
|
|
694
|
-
this.removeEventListener(type, check);
|
|
695
|
-
if (error)
|
|
696
|
-
reject(error);
|
|
697
|
-
else
|
|
698
|
-
resolve();
|
|
699
|
-
};
|
|
700
|
-
const check = (event) => {
|
|
701
|
-
const error = event?.detail?.error;
|
|
702
|
-
if (done())
|
|
703
|
-
settle();
|
|
704
|
-
else if ((error?.code === "store_unavailable" || error?.code === "unknown_id") && (error.for === undefined || error.for === message.type && error.grantId === message.grantId))
|
|
705
|
-
settle(error);
|
|
706
|
-
else if (this.#state !== "connected")
|
|
707
|
-
settle(new DOMException("TabMcp: the session ended before the relay answered", "NetworkError"));
|
|
708
|
-
};
|
|
709
|
-
const timer = setTimeout(() => settle(new DOMException("TabMcp: the relay did not answer", "TimeoutError")), timing.answerTimeoutMs);
|
|
710
|
-
for (const type of types)
|
|
711
|
-
this.addEventListener(type, check);
|
|
712
|
-
check();
|
|
713
|
-
});
|
|
691
|
+
return !this.#session || this.#connection?.send(message) !== true ? notConnected() : this.#lazy.answer(this, message, done, () => this.#state === "connected", timing.answerTimeoutMs);
|
|
714
692
|
}
|
|
715
693
|
showConnectDialog(options = {}) {
|
|
716
694
|
if (this.#relayUrl === undefined)
|
|
@@ -730,7 +708,7 @@ class TabMcp extends EventTarget {
|
|
|
730
708
|
async#showConnectDialog(doc) {
|
|
731
709
|
const connecting = this.connect();
|
|
732
710
|
connecting.catch(() => {});
|
|
733
|
-
const { showConnectDialog } = await import("./connect-
|
|
711
|
+
const { showConnectDialog } = await import("./connect-fhtjdmf3.js");
|
|
734
712
|
await showConnectDialog(this, connecting, doc, this.#server.name, this.#ui);
|
|
735
713
|
}
|
|
736
714
|
addEventListener(type, listener, options) {
|
|
@@ -764,22 +742,31 @@ class TabMcp extends EventTarget {
|
|
|
764
742
|
this.#pairingCode = pairingCode;
|
|
765
743
|
this.#emit("pairingcode", { pairingCode });
|
|
766
744
|
}
|
|
745
|
+
#setPairingUntil(pairingUntil) {
|
|
746
|
+
if (this.#pairingUntil === pairingUntil)
|
|
747
|
+
return;
|
|
748
|
+
this.#pairingUntil = pairingUntil;
|
|
749
|
+
this.#emit("pairinguntil", { pairingUntil });
|
|
750
|
+
}
|
|
767
751
|
#setGrants(grants) {
|
|
768
752
|
this.#grants = Object.freeze(grants.map(frozenGrant));
|
|
769
753
|
this.#emit("grantschange", { grants: this.#grants });
|
|
770
754
|
}
|
|
771
755
|
#confirm(request) {
|
|
772
756
|
const ask = this.#onConfirm;
|
|
773
|
-
return import("./confirm-
|
|
757
|
+
return import("./confirm-hymzwx3b.js").then((m) => m.confirmCall(this, request, ask), () => ask?.(request) ?? false);
|
|
774
758
|
}
|
|
775
759
|
get _allowed() {
|
|
776
760
|
return [this.#registry, this.#secrets, this.#alwaysAllow];
|
|
777
761
|
}
|
|
762
|
+
get _ui() {
|
|
763
|
+
return this.#ui;
|
|
764
|
+
}
|
|
778
765
|
confirmInPage(request, options = {}) {
|
|
779
766
|
const doc = options.document ?? globalThis.document;
|
|
780
767
|
if (doc === undefined)
|
|
781
768
|
return Promise.resolve(false);
|
|
782
|
-
return import("./confirm-
|
|
769
|
+
return import("./confirm-hymzwx3b.js").then((ui) => ui.confirmInPage(this, request, doc, { ui: this.#ui }));
|
|
783
770
|
}
|
|
784
771
|
#host(ref) {
|
|
785
772
|
const current = () => ref.connection !== undefined && this.#connection === ref.connection;
|
|
@@ -809,10 +796,14 @@ class TabMcp extends EventTarget {
|
|
|
809
796
|
return session;
|
|
810
797
|
this.#session = session;
|
|
811
798
|
this.#stayOn = false;
|
|
799
|
+
const ended = this.#pairing && this.#reconnecting && !this.#clicked && this.#asked && session.pairing === "oidc";
|
|
800
|
+
this.#clicked = false;
|
|
812
801
|
this.#stopReconnecting();
|
|
813
802
|
this.#setGrants([...m.grants]);
|
|
814
|
-
if (
|
|
815
|
-
this.#
|
|
803
|
+
if (ended)
|
|
804
|
+
this.#setPairingUntil(new Date().toISOString());
|
|
805
|
+
else if (this.#pairing)
|
|
806
|
+
this.#askPairing();
|
|
816
807
|
this.#setState("connected");
|
|
817
808
|
if (session.pairing === "oidc" && this.#identityAssertion)
|
|
818
809
|
this.refreshIdentity();
|
|
@@ -822,6 +813,10 @@ class TabMcp extends EventTarget {
|
|
|
822
813
|
if (current() && this.#pairing)
|
|
823
814
|
this.#setPairingCode(code);
|
|
824
815
|
},
|
|
816
|
+
pairingOpen: (until) => {
|
|
817
|
+
if (current() && this.#pairing)
|
|
818
|
+
this.#setPairingUntil(until);
|
|
819
|
+
},
|
|
825
820
|
grantAdded: (grant) => {
|
|
826
821
|
if (!current())
|
|
827
822
|
return;
|
|
@@ -861,7 +856,7 @@ class TabMcp extends EventTarget {
|
|
|
861
856
|
this.#connection = undefined;
|
|
862
857
|
this.#session = undefined;
|
|
863
858
|
const generation = this.#generation;
|
|
864
|
-
this.#
|
|
859
|
+
this.#clearPairing();
|
|
865
860
|
this.#identityRound++;
|
|
866
861
|
this.#setIdentity("none");
|
|
867
862
|
if (generation !== this.#generation)
|
|
@@ -879,5 +874,5 @@ export {
|
|
|
879
874
|
VERSION
|
|
880
875
|
};
|
|
881
876
|
|
|
882
|
-
//# debugId=
|
|
877
|
+
//# debugId=F606321EEB1E829064756E2164756E21
|
|
883
878
|
//# sourceMappingURL=index.js.map
|