@ractive-ch/tab-mcp-client 0.17.0 → 0.18.1-rc.1
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 +87 -0
- package/README.md +130 -19
- package/allowed.d.ts +28 -0
- package/allowed.js +14 -0
- package/allowed.js.map +10 -0
- package/always-allowed.d.ts +156 -22
- package/cdn/allowed.min.js +4 -0
- package/cdn/allowed.min.js.map +10 -0
- package/cdn/confirm-d722a9eb.min.js +4 -0
- package/cdn/confirm-d722a9eb.min.js.map +10 -0
- package/cdn/connect-x6ws6wqt.min.js +6 -0
- package/cdn/connect-x6ws6wqt.min.js.map +12 -0
- package/cdn/{index-t33bb2g4.min.js → tab-mcp-client-2vx9mcww.min.js} +20 -9
- package/cdn/tab-mcp-client-2vx9mcww.min.js.map +12 -0
- package/cdn/tab-mcp-client-5j3brdf3.min.js +5 -0
- package/cdn/tab-mcp-client-5j3brdf3.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/{confirm-vj2zbq4w.js → confirm-bqfdrx3f.js} +31 -18
- package/confirm-bqfdrx3f.js.map +10 -0
- package/confirm.d.ts +49 -20
- package/{connect-9pefy0xe.js → connect-ztsgybpq.js} +113 -16
- package/connect-ztsgybpq.js.map +10 -0
- package/connect.d.ts +2 -2
- package/dialog.d.ts +1 -1
- package/{index-qw8qy5ed.js → index-c1c266tm.js} +27 -89
- package/index-c1c266tm.js.map +11 -0
- package/index-s31xrfa2.js +267 -0
- package/index-s31xrfa2.js.map +10 -0
- package/index.js +16 -19
- package/index.js.map +4 -4
- package/package.json +7 -2
- package/react.d.ts +19 -0
- package/react.js +30 -1
- package/react.js.map +3 -3
- package/secret.d.ts +6 -0
- package/strings.d.ts +12 -3
- package/tab-mcp.d.ts +5 -4
- package/types.d.ts +16 -7
- package/cdn/confirm-mqmvsxg1.min.js +0 -4
- package/cdn/confirm-mqmvsxg1.min.js.map +0 -10
- package/cdn/connect-c7wn48yp.min.js +0 -6
- package/cdn/connect-c7wn48yp.min.js.map +0 -12
- package/cdn/index-t33bb2g4.min.js.map +0 -13
- package/confirm-vj2zbq4w.js.map +0 -10
- package/connect-9pefy0xe.js.map +0 -10
- package/index-qw8qy5ed.js.map +0 -12
|
@@ -2,85 +2,6 @@ import {
|
|
|
2
2
|
escapeInvisible2
|
|
3
3
|
} from "./index-dmh5vm5m.js";
|
|
4
4
|
|
|
5
|
-
// src/always-allowed.ts
|
|
6
|
-
var seen = (tool) => JSON.stringify([tool.title, tool.description]);
|
|
7
|
-
|
|
8
|
-
class AlwaysAllowed {
|
|
9
|
-
#tools = new Map;
|
|
10
|
-
#listeners = new Set;
|
|
11
|
-
has(grantId, tool) {
|
|
12
|
-
const tools = this.#tools.get(grantId);
|
|
13
|
-
const allowed = tools?.get(tool.name)?.seen;
|
|
14
|
-
if (allowed === undefined || allowed === seen(tool)) {
|
|
15
|
-
return allowed !== undefined;
|
|
16
|
-
}
|
|
17
|
-
tools?.delete(tool.name);
|
|
18
|
-
this.#changed();
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
add(grantId, tool) {
|
|
22
|
-
const tools = this.#tools.get(grantId) ?? new Map;
|
|
23
|
-
tools.set(tool.name, { seen: seen(tool), label: tool.title ?? tool.name });
|
|
24
|
-
this.#tools.set(grantId, tools);
|
|
25
|
-
this.#changed();
|
|
26
|
-
}
|
|
27
|
-
list(grantId) {
|
|
28
|
-
return [...this.#tools.get(grantId)?.values() ?? []].map((t) => t.label);
|
|
29
|
-
}
|
|
30
|
-
forget(grantId) {
|
|
31
|
-
if (this.#tools.delete(grantId))
|
|
32
|
-
this.#changed();
|
|
33
|
-
}
|
|
34
|
-
keepGrants(grantIds) {
|
|
35
|
-
const keep = new Set(grantIds);
|
|
36
|
-
for (const grantId of this.#tools.keys())
|
|
37
|
-
if (!keep.has(grantId))
|
|
38
|
-
this.forget(grantId);
|
|
39
|
-
}
|
|
40
|
-
keepTools(names) {
|
|
41
|
-
const keep = new Set(names);
|
|
42
|
-
let changed = false;
|
|
43
|
-
for (const tools of this.#tools.values()) {
|
|
44
|
-
for (const tool of tools.keys()) {
|
|
45
|
-
if (keep.has(tool))
|
|
46
|
-
continue;
|
|
47
|
-
tools.delete(tool);
|
|
48
|
-
changed = true;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
if (changed)
|
|
52
|
-
this.#changed();
|
|
53
|
-
}
|
|
54
|
-
onChange(listener) {
|
|
55
|
-
this.#listeners.add(listener);
|
|
56
|
-
return () => this.#listeners.delete(listener);
|
|
57
|
-
}
|
|
58
|
-
#changed() {
|
|
59
|
-
for (const listener of [...this.#listeners])
|
|
60
|
-
listener();
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
var allowed = new WeakMap;
|
|
64
|
-
function allowedFor(mcp) {
|
|
65
|
-
const kept = allowed.get(mcp);
|
|
66
|
-
if (kept !== undefined)
|
|
67
|
-
return kept;
|
|
68
|
-
const made = new AlwaysAllowed;
|
|
69
|
-
mcp.addEventListener("grantschange", (e) => made.keepGrants(e.detail.grants.map((g) => g.grantId)));
|
|
70
|
-
let tools;
|
|
71
|
-
mcp.addEventListener("toolschange", (e) => {
|
|
72
|
-
if (tools === undefined)
|
|
73
|
-
queueMicrotask(() => {
|
|
74
|
-
if (tools !== undefined)
|
|
75
|
-
made.keepTools(tools);
|
|
76
|
-
tools = undefined;
|
|
77
|
-
});
|
|
78
|
-
tools = e.detail.tools;
|
|
79
|
-
});
|
|
80
|
-
allowed.set(mcp, made);
|
|
81
|
-
return made;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
5
|
// src/strings.ts
|
|
85
6
|
var STRINGS = {
|
|
86
7
|
lang: "en",
|
|
@@ -90,6 +11,8 @@ var STRINGS = {
|
|
|
90
11
|
confirmWho: "{client} wants to run {tool}.",
|
|
91
12
|
deny: "Don't allow",
|
|
92
13
|
allow: "Allow",
|
|
14
|
+
alwaysAllow: "Always allow",
|
|
15
|
+
alwaysAllowNote: "Always allow: {tool} will run for {client} without asking, with any arguments, until you disconnect {client} from this page.",
|
|
93
16
|
allowUntilReload: "Allow until reload",
|
|
94
17
|
allowUntilReloadNote: "Allow until reload: {tool} will run for {client} without asking, with any arguments, until you reload this page or disconnect {client}.",
|
|
95
18
|
argumentsHidden: "The arguments cannot be shown, so this call cannot be allowed.",
|
|
@@ -153,9 +76,13 @@ var STRINGS = {
|
|
|
153
76
|
disconnectNote: "To disconnect an app, connect this page again first.",
|
|
154
77
|
storeUnavailable: "The relay could not save this. Try again.",
|
|
155
78
|
disconnectFailed: "Could not disconnect the app. Try again.",
|
|
156
|
-
runsWithoutAsking: "Runs without asking
|
|
157
|
-
|
|
158
|
-
|
|
79
|
+
runsWithoutAsking: "Runs without asking",
|
|
80
|
+
destructive: "destructive",
|
|
81
|
+
allowToolApp: "{tool} runs without asking for {app}",
|
|
82
|
+
allowDestructiveToolApp: "{tool} (destructive) runs without asking for {app}",
|
|
83
|
+
allowEverything: "Allow everything, no questions asked",
|
|
84
|
+
allowEverythingApp: "Allow everything, no questions asked, for {app}",
|
|
85
|
+
allowEverythingNote: "Every tool of this page runs for {app} without asking, with any arguments: also tools you have not seen yet and ones that delete or overwrite.",
|
|
159
86
|
closeNote: "Closing this stops the code. To connect later, open this again from the page.",
|
|
160
87
|
close: "Close",
|
|
161
88
|
done: "Done",
|
|
@@ -256,7 +183,7 @@ html dialog.tab-mcp-dialog:where(:focus) { outline: none; }
|
|
|
256
183
|
.tab-mcp-dialog :where(h2) { font-size: var(--tab-mcp-title-size, 1.25em); line-height: 1.3; font-weight: 600; overflow-wrap: anywhere; }
|
|
257
184
|
.tab-mcp-dialog :where(h3) { font-size: 0.875em; font-weight: 600; color: var(--_tab-mcp-muted); margin-bottom: 0.5rem; }
|
|
258
185
|
.tab-mcp-dialog :where(small) { font-size: 0.875em; }
|
|
259
|
-
.tab-mcp-dialog :where(small, .tab-mcp-status, .tab-mcp-description, .tab-mcp-always-note, .tab-mcp-apps-note, .tab-mcp-success-next, .tab-mcp-waiting, .tab-mcp-scroll-note
|
|
186
|
+
.tab-mcp-dialog :where(small, .tab-mcp-status, .tab-mcp-description, .tab-mcp-always-note, .tab-mcp-apps-note, .tab-mcp-success-next, .tab-mcp-waiting, .tab-mcp-scroll-note) { color: var(--_tab-mcp-muted); }
|
|
260
187
|
html dialog.tab-mcp-dialog :where(:focus-visible) { outline: 2px solid var(--tab-mcp-focus, var(--_tab-mcp-fg)); outline-offset: 2px; }
|
|
261
188
|
.tab-mcp-dialog :where(button) { font: inherit; font-size: 0.9375em; line-height: 1.25; text-transform: none; letter-spacing: normal; width: auto; min-height: 2.5rem; padding: 0.5rem 1rem; margin: 0; border: 1px solid color-mix(in srgb, currentColor 35%, transparent); border-radius: var(--_tab-mcp-small-radius); background: transparent; color: inherit; cursor: pointer; }
|
|
262
189
|
html dialog.tab-mcp-dialog :where(button:hover:not(:disabled)) { color: inherit; background: color-mix(in srgb, currentColor 8%, transparent); }
|
|
@@ -291,7 +218,18 @@ html button.tab-mcp-danger { color: var(--_tab-mcp-danger); border-color: color-
|
|
|
291
218
|
.tab-mcp-help :where(code) { font: 0.875em var(--_tab-mcp-mono); padding: 0.125em 0.375em; background: var(--_tab-mcp-surface); border-radius: 0.25rem; -webkit-box-decoration-break: clone; box-decoration-break: clone; }
|
|
292
219
|
.tab-mcp-apps :where(li) { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.5rem 0.75rem; padding: 0.5rem 0.5rem 0.5rem 0.75rem; border: 1px solid var(--_tab-mcp-line); border-radius: var(--_tab-mcp-small-radius); }
|
|
293
220
|
.tab-mcp-app { flex: 1 1 10rem; min-width: 0; overflow-wrap: anywhere; }
|
|
294
|
-
.tab-mcp-allowed { display:
|
|
221
|
+
.tab-mcp-allowed { display: grid; flex-basis: 100%; gap: 0.25rem; padding-top: 0.5rem; border-top: 1px solid var(--_tab-mcp-line); font-size: 0.875em; overflow-wrap: anywhere; }
|
|
222
|
+
.tab-mcp-allowed-title { font-weight: 600; color: var(--_tab-mcp-muted); }
|
|
223
|
+
.tab-mcp-switches { display: grid; gap: 0.125rem; }
|
|
224
|
+
.tab-mcp-switch { display: flex; align-items: center; gap: 0.625rem; min-height: 2rem; cursor: pointer; }
|
|
225
|
+
.tab-mcp-switch > :where(input) { appearance: none; -webkit-appearance: none; flex: none; position: relative; width: 2.25rem; height: 1.25rem; margin: 0; padding: 0; border: 1px solid var(--_tab-mcp-line); border-radius: 0.625rem; background: var(--_tab-mcp-surface); cursor: pointer; }
|
|
226
|
+
.tab-mcp-switch > :where(input)::before { content: ""; position: absolute; top: 2px; left: 2px; width: calc(1.25rem - 6px); height: calc(1.25rem - 6px); border-radius: 50%; background: var(--_tab-mcp-muted); }
|
|
227
|
+
.tab-mcp-switch > :where(input:checked) { background: var(--tab-mcp-accent, var(--_tab-mcp-fg)); border-color: transparent; }
|
|
228
|
+
.tab-mcp-switch > :where(input:checked)::before { left: calc(100% - 1.25rem + 4px); background: var(--tab-mcp-accent-text, var(--_tab-mcp-bg)); }
|
|
229
|
+
html dialog.tab-mcp-dialog :where(.tab-mcp-switch > input:disabled) { opacity: 0.45; cursor: not-allowed; }
|
|
230
|
+
.tab-mcp-destructive { color: var(--_tab-mcp-danger); }
|
|
231
|
+
.tab-mcp-everything { font-weight: 600; }
|
|
232
|
+
.tab-mcp-everything-note { color: var(--_tab-mcp-muted); }
|
|
295
233
|
.tab-mcp-apps-note { margin-top: 0.5rem; }
|
|
296
234
|
.tab-mcp-success { display: flex; align-items: center; gap: 0.875rem; padding: 1rem; background: color-mix(in srgb, var(--_tab-mcp-success) 10%, transparent); border: 1px solid color-mix(in srgb, var(--_tab-mcp-success) 45%, transparent); border-radius: var(--_tab-mcp-small-radius); }
|
|
297
235
|
.tab-mcp-success-icon { flex: none; display: grid; place-items: center; width: 2.75rem; height: 2.75rem; border-radius: 50%; font-size: 1.5em; font-weight: 700; line-height: 1; color: var(--_tab-mcp-success); background: color-mix(in srgb, var(--_tab-mcp-success) 16%, transparent); }
|
|
@@ -312,8 +250,8 @@ html button.tab-mcp-danger { color: var(--_tab-mcp-danger); border-color: color-
|
|
|
312
250
|
.tab-mcp-arguments { box-sizing: content-box; font: 0.875em / 1.45 var(--_tab-mcp-mono); padding: 0.75rem; background: var(--_tab-mcp-surface); border: 1px solid var(--_tab-mcp-line); border-radius: var(--_tab-mcp-small-radius); }
|
|
313
251
|
.tab-mcp-always-note, .tab-mcp-waiting, .tab-mcp-scroll-note { font-size: 0.875em; }
|
|
314
252
|
@media (max-width: 30rem) { .tab-mcp-actions > :where(button) { flex: 1 1 auto; } .tab-mcp-connect > :where(.tab-mcp-actions) > :where(button) { flex: 0 0 auto; } .tab-mcp-code { grid-template-columns: minmax(0, 1fr); } .tab-mcp-code > :where(button) { justify-self: start; } }
|
|
315
|
-
@media (prefers-reduced-motion: no-preference) { .tab-mcp-dialog:where([open]) { animation: tab-mcp-in 0.15s ease-out; } .tab-mcp-success-icon { animation: tab-mcp-pop 0.4s cubic-bezier(0.3, 1.4, 0.6, 1); } }
|
|
316
|
-
@media (forced-colors: active) { html button.tab-mcp-primary { border-color: ButtonText; } }
|
|
253
|
+
@media (prefers-reduced-motion: no-preference) { .tab-mcp-dialog:where([open]) { animation: tab-mcp-in 0.15s ease-out; } .tab-mcp-success-icon { animation: tab-mcp-pop 0.4s cubic-bezier(0.3, 1.4, 0.6, 1); } .tab-mcp-switch > :where(input)::before { transition: left 0.15s ease-out; } }
|
|
254
|
+
@media (forced-colors: active) { html button.tab-mcp-primary { border-color: ButtonText; } .tab-mcp-switch > :where(input) { forced-color-adjust: none; background: Canvas; border-color: ButtonText; } .tab-mcp-switch > :where(input)::before { forced-color-adjust: none; background: ButtonText; } .tab-mcp-switch > :where(input:checked) { background: Highlight; } .tab-mcp-switch > :where(input:checked)::before { background: HighlightText; } }
|
|
317
255
|
@keyframes tab-mcp-in { from { opacity: 0; transform: translateY(0.5rem); } }
|
|
318
256
|
@keyframes tab-mcp-pop { from { opacity: 0; transform: scale(0.4); } }
|
|
319
257
|
`;
|
|
@@ -386,7 +324,7 @@ function copyButton(doc, s, label, value, select, announce, className, kind) {
|
|
|
386
324
|
return { button: node, reset };
|
|
387
325
|
}
|
|
388
326
|
|
|
389
|
-
export {
|
|
327
|
+
export { nextId, stringsOf, format, fill, el, button, visible, clientLabel, listOf, newDialog, copyButton };
|
|
390
328
|
|
|
391
|
-
//# debugId=
|
|
392
|
-
//# sourceMappingURL=index-
|
|
329
|
+
//# debugId=461C87FA3A5E330064756E2164756E21
|
|
330
|
+
//# sourceMappingURL=index-c1c266tm.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/strings.ts", "../src/dialog.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"/**\n * Every text of the library's dialogs, in one table (CU 5). A page passes\n * its own for some or all keys (`ui: { strings }`); the dialogs use these for\n * the rest. `{name}` is a placeholder the dialog fills in: names of tools\n * and AI apps (isolated, so that they cannot reorder the sentence), the\n * code, counts. Where English needs two forms, there are two keys\n * (`…One` for one, `…Many` for more). `lang` and `dir` are the language and\n * direction of the texts, set on every dialog.\n */\nexport const STRINGS = {\n\tlang: \"en\",\n\tdir: \"ltr\",\n\t/** Between the last two items of a list (\"A, B and C\"). */\n\tand: \"and\",\n\n\t// The confirmation of a consequential tool call.\n\tconfirmTitle: \"Allow {tool}?\",\n\tconfirmWho: \"{client} wants to run {tool}.\",\n\tdeny: \"Don't allow\",\n\tallow: \"Allow\",\n\t/** `alwaysAllow: true`: kept until the app is disconnected. */\n\talwaysAllow: \"Always allow\",\n\talwaysAllowNote:\n\t\t\"Always allow: {tool} will run for {client} without asking, with any arguments, until you disconnect {client} from this page.\",\n\t/** `alwaysAllow: \"until-reload\"`, or kept in memory only (`storage: null`). */\n\tallowUntilReload: \"Allow until reload\",\n\tallowUntilReloadNote:\n\t\t\"Allow until reload: {tool} will run for {client} without asking, with any arguments, until you reload this page or disconnect {client}.\",\n\targumentsHidden:\n\t\t\"The arguments cannot be shown, so this call cannot be allowed.\",\n\tscrollToAllow: \"Scroll to the end of the arguments to allow this call.\",\n\twaitingOne: \"1 more call is waiting after this one.\",\n\twaitingMany: \"{n} more calls are waiting after this one.\",\n\t/** Before the page's title while a confirmation waits in a hidden tab. */\n\ttitlePrefix: \"Allow? – \",\n\n\t// How the dialogs name a caller.\n\tclientWebMcp: \"An agent in this browser\",\n\tclientLocal: \"{name} (this site's assistant)\",\n\t/** An AI app through the relay: its name and the host of its client id. */\n\tclientRelay: \"{name} ({host})\",\n\n\t// Copy buttons.\n\tcopy: \"Copy\",\n\tcopied: \"Copied\",\n\t/** On the button after a refused Copy: the text is selected instead. */\n\tpressMac: \"Press ⌘C\",\n\tpressOther: \"Press Ctrl+C\",\n\t/** The same on a touch screen, where there is no key to press. */\n\tselected: \"Selected\",\n\tcopyAddress: \"Copy address\",\n\tcopyCode: \"Copy code\",\n\tcopyCommand: \"Copy {app} command\",\n\n\t// The connect dialog.\n\tconnectTitle: \"Connect your AI app\",\n\tconnecting: \"Connecting…\",\n\tconnectAgain: \"Connect again\",\n\treconnecting: \"Connection lost. Reconnecting…\",\n\tnotConnected: \"Not connected.\",\n\tunreachable: \"Could not connect. Check your internet connection.\",\n\trateLimited: \"Too many attempts. Wait a minute, then connect again.\",\n\trefused: \"The connection was refused.\",\n\tended: \"The session ended.\",\n\tlost: \"The connection was lost.\",\n\tclosed: \"The connection was closed.\",\n\tdisconnected: \"Disconnected.\",\n\tcheckingSignIn: \"Checking your sign-in…\",\n\tsignInFirst: \"Sign in to this page first.\",\n\tsignInRefused: \"Your sign-in on this page was not accepted.\",\n\t/** `{reason}`: the relay's reason, as it gave it. */\n\tsignInRefusedWhy: \"Your sign-in on this page was not accepted ({reason}).\",\n\treadyApp: \"{app} is ready for your AI app.\",\n\treadyPage: \"This page is ready for your AI app.\",\n\taddressLabel: \"Add this address to your AI app:\",\n\thelp: \"How do I add it?\",\n\tguide: \"Step by step, and what to do when it fails\",\n\tcodeSentence: \"When asked, enter the code {code}\",\n\tcodeNote: \"A new code appears every 2 minutes and after each use.\",\n\tcodeUsed: \"Code used. Waiting for your AI app…\",\n\tsignIn:\n\t\t\"When your AI app asks, sign in with the account you use on this page. No code is needed.\",\n\tnewAddress: \"Disconnect all apps\",\n\tnewAddressNote:\n\t\t\"An AI app is connected that should not be? This disconnects every AI app from this page; each one has to connect again with a new code.\",\n\tnewAddressQuestionOne:\n\t\t\"This disconnects {apps}. To use this page with it again, connect it again and enter a new code.\",\n\tnewAddressQuestionMany:\n\t\t\"This disconnects {apps}. To use this page with them again, connect them again and enter a new code.\",\n\tnewAddressConfirm: \"Disconnect all\",\n\tcancel: \"Cancel\",\n\tgettingNewAddress: \"Disconnecting…\",\n\tnewAddressDone: \"Every AI app is disconnected. Connect yours again.\",\n\tconnectedOne: \"{apps} is connected\",\n\tconnectedMany: \"{apps} are connected\",\n\tconnectedNextOne:\n\t\t\"You can close this now and ask it to use the tools of this page.\",\n\tconnectedNextMany:\n\t\t\"You can close this now and ask them to use the tools of this page.\",\n\tconnectAnother: \"Connect another app\",\n\tconnectedHeading: \"Connected\",\n\tappConnected: \"{app} connected\",\n\tdisconnect: \"Disconnect\",\n\tdisconnectApp: \"Disconnect {app}\",\n\tdisconnectNote: \"To disconnect an app, connect this page again first.\",\n\t/** Disconnect failed: the relay could not save it (`store_unavailable`). */\n\tstoreUnavailable: \"The relay could not save this. Try again.\",\n\t/** Disconnect failed for another reason. */\n\tdisconnectFailed: \"Could not disconnect the app. Try again.\",\n\t// Under a connected app: a switch per tool that asks, and one for all.\n\trunsWithoutAsking: \"Runs without asking\",\n\t/** After a tool's name: it may delete or overwrite (`destructiveHint`). */\n\tdestructive: \"destructive\",\n\t/** A tool's switch, as read out: starts with what it shows (WCAG 2.5.3). */\n\tallowToolApp: \"{tool} runs without asking for {app}\",\n\tallowDestructiveToolApp: \"{tool} (destructive) runs without asking for {app}\",\n\tallowEverything: \"Allow everything, no questions asked\",\n\tallowEverythingApp: \"Allow everything, no questions asked, for {app}\",\n\tallowEverythingNote:\n\t\t\"Every tool of this page runs for {app} without asking, with any arguments: also tools you have not seen yet and ones that delete or overwrite.\",\n\tcloseNote:\n\t\t\"Closing this stops the code. To connect later, open this again from the page.\",\n\tclose: \"Close\",\n\tdone: \"Done\",\n\n\t// The notice when another tab or program took over the address.\n\treplacedSiblingTitle: \"Opened in another tab\",\n\treplacedSibling:\n\t\t\"This page is open in another tab, and your AI apps now use that one. To use them here, connect this page again.\",\n\treplacedTitle: \"Another tab or program took over this page\",\n\treplaced:\n\t\t\"Your AI apps now reach that tab or program instead of this page. If you did not open this page in another tab or browser, someone may have copied what this page keeps to connect. “Disconnect all apps” ends that; then connect your AI apps again with a new code.\",\n\ttakenOverAgain:\n\t\t\"It was taken over again before its AI apps were disconnected. Try again.\",\n\t/** `{reason}`: why disconnecting all apps failed, one of the texts above. */\n\ttryAgainOrClose: \"{reason} Try again, or close this.\",\n};\n\n/** The texts of the dialogs: the keys of {@link STRINGS}, each a string. */\nexport type DialogStrings = { [K in keyof typeof STRINGS]: string };\n",
|
|
6
|
+
"/**\n * What the library's dialogs share: the stylesheet, elements, buttons, the\n * texts (`strings.ts`) and how a caller is named. The confirmation\n * (`confirm.ts`) and the connect dialog (`connect.ts`) are chunks of their\n * own, loaded on demand; pages with their own UI use the events and methods\n * of `TabMcp` instead.\n */\n\nimport { type DialogStrings, STRINGS } from \"./strings.ts\";\nimport type { DialogOptions, Grant, ToolCallClient } from \"./types.ts\";\nimport { escapeInvisible } from \"./visible.ts\";\n\nlet counter = 0;\n\n/** A new id for the elements of one dialog. */\nexport function nextId(): string {\n\treturn `tab-mcp-${++counter}`;\n}\n\n/** The texts: the page's own where it gave them, else the English ones. */\nexport function stringsOf(options: DialogOptions | undefined): DialogStrings {\n\tconst s: DialogStrings = { ...STRINGS };\n\t// Only strings: a missing translation (`undefined`) keeps the English text.\n\tfor (const [key, value] of Object.entries(options?.strings ?? {}))\n\t\tif (typeof value === \"string\" && key in s)\n\t\t\ts[key as keyof DialogStrings] = value;\n\treturn s;\n}\n\n/** `template` with its `{name}` placeholders filled in, as text. */\nexport function format(\n\ttemplate: string,\n\tvalues: Record<string, string>,\n): string {\n\treturn template.replace(\n\t\t/\\{(\\w+)\\}/g,\n\t\t(all, key: string) => values[key] ?? all,\n\t);\n}\n\n/**\n * `template` with its `{name}` placeholders filled in, as nodes: a string\n * value is a name, isolated so that it cannot reorder the words around it\n * (and may stand twice); a node is put in as it is.\n */\nexport function fill(\n\tdoc: Document,\n\ttemplate: string,\n\tvalues: Record<string, string | Node>,\n): (Node | string)[] {\n\treturn template.split(/\\{(\\w+)\\}/).map((part, i) => {\n\t\tif (i % 2 === 0) return part;\n\t\tconst value = values[part];\n\t\tif (value === undefined) return `{${part}}`;\n\t\treturn typeof value === \"string\" ? name(doc, value) : value;\n\t});\n}\n\nexport function el<K extends keyof HTMLElementTagNameMap>(\n\tdoc: Document,\n\ttag: K,\n\tprops: { className?: string; text?: string; id?: string } = {},\n\t...children: (Node | string)[]\n): HTMLElementTagNameMap[K] {\n\tconst node = doc.createElement(tag);\n\tif (props.className !== undefined) node.className = props.className;\n\tif (props.id !== undefined) node.id = props.id;\n\tif (props.text !== undefined) node.textContent = props.text;\n\tnode.append(...children);\n\treturn node;\n}\n\n/**\n * A button. `className` is its own class; `kind` marks the one next step\n * (`tab-mcp-primary`) or one that disconnects (`tab-mcp-danger`); without\n * one it is a plain, outlined button.\n */\nexport function button(\n\tdoc: Document,\n\ttext: string,\n\tonClick: () => void,\n\tclassName?: string,\n\tkind?: \"primary\" | \"danger\",\n): HTMLButtonElement {\n\tconst node = el(doc, \"button\", { text });\n\tnode.type = \"button\";\n\tif (className !== undefined) node.classList.add(className);\n\tif (kind !== undefined) node.classList.add(`tab-mcp-${kind}`);\n\tnode.addEventListener(\"click\", onClick);\n\treturn node;\n}\n\n/** A name in a sentence, isolated so that it cannot reorder the words around it. */\nexport function name(doc: Document, text: string): HTMLElement {\n\tconst node = el(doc, \"bdi\", { text });\n\tnode.dir = \"ltr\";\n\treturn node;\n}\n\n/** Invisible, blank and direction characters as `\\uXXXX` (`visible.ts`). */\nexport const visible: (text: string) => string = escapeInvisible;\n\n/**\n * How the dialogs name a caller: an AI app's name and verified host; for\n * the caller of a served port, the name the page gave it and what it is.\n */\nexport function clientLabel(\n\tclient: ToolCallClient | Grant[\"client\"],\n\ts: DialogStrings,\n): string {\n\tif (\"kind\" in client && client.kind === \"webmcp\") return s.clientWebMcp;\n\tif (\"kind\" in client && client.kind === \"local\")\n\t\treturn format(s.clientLocal, { name: visible(client.name ?? client.id) });\n\tconst host = visible(client.host || client.id);\n\treturn client.name !== undefined && client.name !== \"\"\n\t\t? format(s.clientRelay, { name: visible(client.name), host })\n\t\t: host;\n}\n\n/** \"A and B\", \"A, B and C\". */\nexport function listOf(items: string[], s: DialogStrings): string {\n\treturn items.length < 2\n\t\t? (items[0] ?? \"\")\n\t\t: `${items.slice(0, -1).join(\", \")} ${s.and} ${items.at(-1)}`;\n}\n\n/**\n * The look of the dialogs. Every selector has the specificity of one class\n * (the rest is in `:where()`), so rules of the page for elements, such as\n * `button { … }` or a reset's `* { border-width: 0 }`, do not change the\n * dialogs. The rules for states (hover, focus, disabled) and the primary\n * and danger buttons add two elements (`html dialog.…`, `html button.…`),\n * so `button:hover`, `button:focus` or `button:disabled` of the page do not\n * either. A page overrides with two classes, `.tab-mcp-dialog .tab-mcp-x`,\n * which wins over all of them; one class does not. The `--tab-mcp-*`\n * custom properties (on `:root` or on `.tab-mcp-dialog`) change single\n * values; the library only reads them. The defaults use system colours and the\n * page's `color-scheme`, so the dialogs follow its light or dark scheme. The\n * `--_tab-mcp-*` properties are internal. Order matters among equals.\n */\nexport const DIALOG_CSS = `.tab-mcp-dialog {\n--_tab-mcp-fg: var(--tab-mcp-color, CanvasText);\n--_tab-mcp-bg: var(--tab-mcp-background, Canvas);\n--_tab-mcp-muted: var(--tab-mcp-muted, color-mix(in srgb, var(--_tab-mcp-fg) 72%, transparent));\n--_tab-mcp-line: color-mix(in srgb, var(--_tab-mcp-fg) 22%, transparent);\n--_tab-mcp-surface: var(--tab-mcp-surface, color-mix(in srgb, var(--_tab-mcp-fg) 6%, transparent));\n--_tab-mcp-danger: var(--tab-mcp-danger, light-dark(#b3261e, #ffb4ab));\n--_tab-mcp-success: var(--tab-mcp-success, light-dark(#1a7f37, #6fdd8b));\n--_tab-mcp-mono: var(--tab-mcp-mono, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);\n--_tab-mcp-small-radius: calc(var(--tab-mcp-radius, 0.75rem) * 0.6);\nbox-sizing: border-box; margin: auto; width: calc(100% - 2rem); max-width: var(--tab-mcp-max-width, 34rem); max-height: calc(100% - 2rem); overflow: auto;\nfont: var(--tab-mcp-font, inherit); line-height: 1.5; color: var(--_tab-mcp-fg); background: var(--_tab-mcp-bg);\nborder: var(--tab-mcp-border, 1px solid var(--_tab-mcp-line)); border-radius: var(--tab-mcp-radius, 0.75rem);\npadding: var(--tab-mcp-padding, clamp(1rem, 5vw, 1.5rem)); box-shadow: var(--tab-mcp-shadow, 0 1rem 3rem rgb(0 0 0 / 0.3));\npointer-events: auto;\n}\n.tab-mcp-dialog::backdrop { background: var(--tab-mcp-backdrop, rgb(0 0 0 / 0.4)); }\n.tab-mcp-dialog:where([open]) { display: flex; flex-direction: column; gap: 1rem; }\n.tab-mcp-dialog:where([open]) > * { flex-shrink: 0; }\n.tab-mcp-dialog :where([hidden]) { display: none !important; }\nhtml dialog.tab-mcp-dialog:where(:focus) { outline: none; }\n.tab-mcp-dialog :where(*) { box-sizing: border-box; color: inherit; }\n.tab-mcp-dialog :where(h2, h3, p, ul, li, pre) { margin: 0; }\n.tab-mcp-dialog :where(h2) { font-size: var(--tab-mcp-title-size, 1.25em); line-height: 1.3; font-weight: 600; overflow-wrap: anywhere; }\n.tab-mcp-dialog :where(h3) { font-size: 0.875em; font-weight: 600; color: var(--_tab-mcp-muted); margin-bottom: 0.5rem; }\n.tab-mcp-dialog :where(small) { font-size: 0.875em; }\n.tab-mcp-dialog :where(small, .tab-mcp-status, .tab-mcp-description, .tab-mcp-always-note, .tab-mcp-apps-note, .tab-mcp-success-next, .tab-mcp-waiting, .tab-mcp-scroll-note) { color: var(--_tab-mcp-muted); }\nhtml dialog.tab-mcp-dialog :where(:focus-visible) { outline: 2px solid var(--tab-mcp-focus, var(--_tab-mcp-fg)); outline-offset: 2px; }\n.tab-mcp-dialog :where(button) { font: inherit; font-size: 0.9375em; line-height: 1.25; text-transform: none; letter-spacing: normal; width: auto; min-height: 2.5rem; padding: 0.5rem 1rem; margin: 0; border: 1px solid color-mix(in srgb, currentColor 35%, transparent); border-radius: var(--_tab-mcp-small-radius); background: transparent; color: inherit; cursor: pointer; }\nhtml dialog.tab-mcp-dialog :where(button:hover:not(:disabled)) { color: inherit; background: color-mix(in srgb, currentColor 8%, transparent); }\nhtml dialog.tab-mcp-dialog :where(button:disabled) { opacity: 0.45; cursor: not-allowed; }\nhtml button.tab-mcp-primary { background: var(--tab-mcp-accent, var(--_tab-mcp-fg)); color: var(--tab-mcp-accent-text, var(--_tab-mcp-bg)); border-color: transparent; font-weight: 600; }\nhtml button.tab-mcp-primary:where(:hover:not(:disabled)) { background: var(--tab-mcp-accent, var(--_tab-mcp-fg)); opacity: 0.85; }\nhtml button.tab-mcp-danger { color: var(--_tab-mcp-danger); border-color: color-mix(in srgb, currentColor 45%, transparent); }\n.tab-mcp-copy { min-width: 6em; }\n.tab-mcp-close { font-size: 0.875em; }\n.tab-mcp-status { margin-top: -0.5rem; }\n.tab-mcp-description:where(:empty) { display: none; }\n.tab-mcp-actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 0.5rem; }\n.tab-mcp-address { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; }\n.tab-mcp-address :where(label) { flex-basis: 100%; font-weight: 600; }\n.tab-mcp-address :where(input) { flex: 1 1 12rem; min-width: 0; min-height: 2.5rem; margin: 0; padding: 0.5rem 0.625rem; font: 0.875em var(--_tab-mcp-mono); text-overflow: ellipsis; color: inherit; background: var(--_tab-mcp-surface); border: 1px solid var(--_tab-mcp-line); border-radius: var(--_tab-mcp-small-radius); }\n.tab-mcp-code { display: grid; grid-template-columns: 1fr auto; align-items: end; gap: 0.25rem 1rem; padding: 1rem; background: var(--_tab-mcp-surface); border-radius: var(--_tab-mcp-small-radius); }\n.tab-mcp-code > :where(span) { font-weight: 600; }\n.tab-mcp-code :where(br) { display: none; }\n.tab-mcp-code :where(small) { grid-column: 1 / -1; }\n.tab-mcp-code-value { display: block; font: 700 var(--tab-mcp-code-size, 2em) / 1.2 var(--_tab-mcp-mono); letter-spacing: 0.12em; font-variant-numeric: tabular-nums; }\n.tab-mcp-help { border: 1px solid var(--_tab-mcp-line); border-radius: var(--_tab-mcp-small-radius); }\n.tab-mcp-help :where(summary) { padding: 0.75rem 1rem; font-weight: 600; cursor: pointer; }\n.tab-mcp-help > :where(:not(summary)) { margin-inline: 1rem; }\n.tab-mcp-help > :where(:last-child:not(summary)) { margin-bottom: 1rem; }\n.tab-mcp-dialog :where(.tab-mcp-help ul, .tab-mcp-apps ul) { list-style: none; padding: 0; display: grid; gap: 0.5rem; }\n.tab-mcp-help :where(ul) { margin-top: 0.25rem; margin-bottom: 0.75rem; gap: 0.75rem; }\n.tab-mcp-help :where(li) { overflow-wrap: anywhere; }\n.tab-mcp-help :where(strong) { display: block; font-weight: 600; }\n.tab-mcp-command { display: flex; flex-wrap: wrap; justify-content: flex-end; align-items: flex-start; gap: 0.5rem; margin-top: 0.375rem; }\n.tab-mcp-command > :where(pre) { flex: 1 1 16rem; min-width: 0; margin: 0; padding: 0.5rem 0.625rem; font: 0.8125em / 1.5 var(--_tab-mcp-mono); white-space: pre-wrap; word-break: break-all; background: var(--_tab-mcp-surface); border: 1px solid var(--_tab-mcp-line); border-radius: var(--_tab-mcp-small-radius); }\n.tab-mcp-command-note { display: block; margin-top: 0.375rem; }\n.tab-mcp-help :where(code) { font: 0.875em var(--_tab-mcp-mono); padding: 0.125em 0.375em; background: var(--_tab-mcp-surface); border-radius: 0.25rem; -webkit-box-decoration-break: clone; box-decoration-break: clone; }\n.tab-mcp-apps :where(li) { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.5rem 0.75rem; padding: 0.5rem 0.5rem 0.5rem 0.75rem; border: 1px solid var(--_tab-mcp-line); border-radius: var(--_tab-mcp-small-radius); }\n.tab-mcp-app { flex: 1 1 10rem; min-width: 0; overflow-wrap: anywhere; }\n.tab-mcp-allowed { display: grid; flex-basis: 100%; gap: 0.25rem; padding-top: 0.5rem; border-top: 1px solid var(--_tab-mcp-line); font-size: 0.875em; overflow-wrap: anywhere; }\n.tab-mcp-allowed-title { font-weight: 600; color: var(--_tab-mcp-muted); }\n.tab-mcp-switches { display: grid; gap: 0.125rem; }\n.tab-mcp-switch { display: flex; align-items: center; gap: 0.625rem; min-height: 2rem; cursor: pointer; }\n.tab-mcp-switch > :where(input) { appearance: none; -webkit-appearance: none; flex: none; position: relative; width: 2.25rem; height: 1.25rem; margin: 0; padding: 0; border: 1px solid var(--_tab-mcp-line); border-radius: 0.625rem; background: var(--_tab-mcp-surface); cursor: pointer; }\n.tab-mcp-switch > :where(input)::before { content: \"\"; position: absolute; top: 2px; left: 2px; width: calc(1.25rem - 6px); height: calc(1.25rem - 6px); border-radius: 50%; background: var(--_tab-mcp-muted); }\n.tab-mcp-switch > :where(input:checked) { background: var(--tab-mcp-accent, var(--_tab-mcp-fg)); border-color: transparent; }\n.tab-mcp-switch > :where(input:checked)::before { left: calc(100% - 1.25rem + 4px); background: var(--tab-mcp-accent-text, var(--_tab-mcp-bg)); }\nhtml dialog.tab-mcp-dialog :where(.tab-mcp-switch > input:disabled) { opacity: 0.45; cursor: not-allowed; }\n.tab-mcp-destructive { color: var(--_tab-mcp-danger); }\n.tab-mcp-everything { font-weight: 600; }\n.tab-mcp-everything-note { color: var(--_tab-mcp-muted); }\n.tab-mcp-apps-note { margin-top: 0.5rem; }\n.tab-mcp-success { display: flex; align-items: center; gap: 0.875rem; padding: 1rem; background: color-mix(in srgb, var(--_tab-mcp-success) 10%, transparent); border: 1px solid color-mix(in srgb, var(--_tab-mcp-success) 45%, transparent); border-radius: var(--_tab-mcp-small-radius); }\n.tab-mcp-success-icon { flex: none; display: grid; place-items: center; width: 2.75rem; height: 2.75rem; border-radius: 50%; font-size: 1.5em; font-weight: 700; line-height: 1; color: var(--_tab-mcp-success); background: color-mix(in srgb, var(--_tab-mcp-success) 16%, transparent); }\n.tab-mcp-success-icon::before { content: \"\\\\2713\"; }\n.tab-mcp-success-text { display: grid; gap: 0.125rem; min-width: 0; }\n.tab-mcp-success-title { font-size: 1.125em; line-height: 1.3; font-weight: 600; overflow-wrap: anywhere; }\n.tab-mcp-setup { display: flex; flex-direction: column; gap: 1rem; }\n.tab-mcp-setup-toggle { display: flex; align-items: center; gap: 0.75rem; width: 100%; padding: 0.75rem 1rem; text-align: start; font-weight: 600; border-color: var(--_tab-mcp-line); }\n.tab-mcp-setup-toggle::before { content: \"\"; flex: none; width: 0.45em; height: 0.45em; border: solid currentColor; border-width: 0 2px 2px 0; transform: translateX(-0.1em) rotate(-45deg); }\n.tab-mcp-setup-toggle:where([aria-expanded=\"true\"])::before { transform: translateY(-0.15em) rotate(45deg); }\n.tab-mcp-code-used { display: flex; align-items: center; min-height: calc(var(--tab-mcp-code-size, 2em) * 1.2); }\n.tab-mcp-new-address-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.5rem 1rem; }\n.tab-mcp-new-address-row > :where(small) { flex: 1 1 14rem; color: var(--_tab-mcp-muted); }\n.tab-mcp-new-address-question { display: grid; gap: 0.75rem; padding: 1rem; border: 1px solid color-mix(in srgb, var(--_tab-mcp-danger) 50%, transparent); border-radius: var(--_tab-mcp-small-radius); }\n.tab-mcp-connect > :where(.tab-mcp-actions) { gap: 0.75rem; padding-top: 1rem; border-top: 1px solid var(--_tab-mcp-line); }\n.tab-mcp-close-note { flex: 1 1 12rem; }\n.tab-mcp-confirm :where(p bdi) { font-weight: 600; }\n.tab-mcp-arguments { box-sizing: content-box; font: 0.875em / 1.45 var(--_tab-mcp-mono); padding: 0.75rem; background: var(--_tab-mcp-surface); border: 1px solid var(--_tab-mcp-line); border-radius: var(--_tab-mcp-small-radius); }\n.tab-mcp-always-note, .tab-mcp-waiting, .tab-mcp-scroll-note { font-size: 0.875em; }\n@media (max-width: 30rem) { .tab-mcp-actions > :where(button) { flex: 1 1 auto; } .tab-mcp-connect > :where(.tab-mcp-actions) > :where(button) { flex: 0 0 auto; } .tab-mcp-code { grid-template-columns: minmax(0, 1fr); } .tab-mcp-code > :where(button) { justify-self: start; } }\n@media (prefers-reduced-motion: no-preference) { .tab-mcp-dialog:where([open]) { animation: tab-mcp-in 0.15s ease-out; } .tab-mcp-success-icon { animation: tab-mcp-pop 0.4s cubic-bezier(0.3, 1.4, 0.6, 1); } .tab-mcp-switch > :where(input)::before { transition: left 0.15s ease-out; } }\n@media (forced-colors: active) { html button.tab-mcp-primary { border-color: ButtonText; } .tab-mcp-switch > :where(input) { forced-color-adjust: none; background: Canvas; border-color: ButtonText; } .tab-mcp-switch > :where(input)::before { forced-color-adjust: none; background: ButtonText; } .tab-mcp-switch > :where(input:checked) { background: Highlight; } .tab-mcp-switch > :where(input:checked)::before { background: HighlightText; } }\n@keyframes tab-mcp-in { from { opacity: 0; transform: translateY(0.5rem); } }\n@keyframes tab-mcp-pop { from { opacity: 0; transform: scale(0.4); } }\n`;\n\nconst sheets = new WeakMap<Document, CSSStyleSheet>();\n\n/**\n * Adopts `DIALOG_CSS` into the document once, next to the page's own adopted\n * sheets. A constructed stylesheet needs no `style-src` exception under a\n * strict CSP. Where constructed stylesheets are not supported, the dialogs\n * keep the browser's default look and work the same.\n */\nfunction adoptStyles(doc: Document): void {\n\ttry {\n\t\tlet sheet = sheets.get(doc);\n\t\tif (sheet === undefined) {\n\t\t\tconst Sheet = (doc.defaultView as (Window & typeof globalThis) | null)\n\t\t\t\t?.CSSStyleSheet;\n\t\t\tif (Sheet === undefined || !Array.isArray(doc.adoptedStyleSheets)) return;\n\t\t\tsheet = new Sheet();\n\t\t\tsheet.replaceSync(DIALOG_CSS);\n\t\t\tsheets.set(doc, sheet);\n\t\t}\n\t\t// The page may have replaced the list since.\n\t\tif (!doc.adoptedStyleSheets.includes(sheet))\n\t\t\tdoc.adoptedStyleSheets = [...doc.adoptedStyleSheets, sheet];\n\t} catch {\n\t\t// Not supported: the default look.\n\t}\n}\n\n/**\n * A dialog with its heading (`title`, or filled in by the caller). It\n * carries the texts' language and direction (`lang`, `dir` of the\n * strings), whatever the page's: read out with that voice, laid out that way.\n */\nexport function newDialog(\n\tdoc: Document,\n\tclassName: string,\n\ts: DialogStrings,\n\t...title: (Node | string)[]\n): { dialog: HTMLDialogElement; heading: HTMLHeadingElement; id: string } {\n\tconst id = nextId();\n\tconst dialog = el(doc, \"dialog\", {\n\t\tclassName: `tab-mcp-dialog ${className}`,\n\t});\n\tdialog.setAttribute(\"aria-labelledby\", `${id}-title`);\n\tdialog.lang = s.lang;\n\tdialog.dir = s.dir;\n\tadoptStyles(doc);\n\tconst heading = el(doc, \"h2\", { id: `${id}-title` }, ...title);\n\tdialog.append(heading);\n\treturn { dialog, heading, id };\n}\n\n/** How long a Copy button says \"Copied\", and how long it says to press Ctrl+C. */\nexport const COPIED_MS = 2000;\nexport const SELECTED_MS = 8000;\n\n/** What the button says after a refused Copy, by the user's device. */\nfunction pressToCopy(doc: Document, s: DialogStrings): string {\n\tconst view = doc.defaultView;\n\tif (view?.matchMedia?.(\"(pointer: coarse)\").matches === true)\n\t\treturn s.selected;\n\tconst platform = view?.navigator.platform ?? \"\";\n\treturn /^(Mac|iP)/.test(platform) ? s.pressMac : s.pressOther;\n}\n\n/**\n * A Copy button for `value()`, read at click time. Says \"Copied\" for a\n * moment once the clipboard took it; without a clipboard (or when it refuses)\n * `select()` puts the text in the selection and the button says to press\n * Ctrl+C (⌘C on Apple devices). `label` names it for assistive technology\n * (\"Copy code\"), as the dialog has more than one Copy; while it says what\n * happened, that is its name, and `announce` says it too.\n */\nexport function copyButton(\n\tdoc: Document,\n\ts: DialogStrings,\n\tlabel: string,\n\tvalue: () => string,\n\tselect: () => void,\n\tannounce: (text: string) => void,\n\tclassName: string,\n\tkind?: \"primary\",\n): { button: HTMLButtonElement; reset: () => void } {\n\tlet timer: ReturnType<typeof setTimeout> | undefined;\n\tconst say = (text: string, ms: number) => {\n\t\tnode.textContent = text;\n\t\tnode.removeAttribute(\"aria-label\");\n\t\tannounce(text);\n\t\tclearTimeout(timer);\n\t\ttimer = setTimeout(reset, ms);\n\t};\n\tconst node = button(\n\t\tdoc,\n\t\ts.copy,\n\t\t() => {\n\t\t\tconst clipboard = (doc.defaultView?.navigator as Navigator | undefined)\n\t\t\t\t?.clipboard;\n\t\t\tconst selected = () => {\n\t\t\t\tselect();\n\t\t\t\tsay(pressToCopy(doc, s), SELECTED_MS);\n\t\t\t};\n\t\t\tif (clipboard !== undefined)\n\t\t\t\tclipboard\n\t\t\t\t\t.writeText(value())\n\t\t\t\t\t.then(() => say(s.copied, COPIED_MS), selected);\n\t\t\telse selected();\n\t\t},\n\t\tclassName,\n\t\tkind,\n\t);\n\t// The same width for \"Copy\" and \"Copied\": nothing moves.\n\tnode.classList.add(\"tab-mcp-copy\");\n\tconst reset = () => {\n\t\tclearTimeout(timer);\n\t\ttimer = undefined;\n\t\tnode.textContent = s.copy;\n\t\tnode.setAttribute(\"aria-label\", label);\n\t};\n\treset();\n\treturn { button: node, reset };\n}\n"
|
|
7
|
+
],
|
|
8
|
+
"mappings": ";;;;;AASO,IAAM,UAAU;AAAA,EACtB,MAAM;AAAA,EACN,KAAK;AAAA,EAEL,KAAK;AAAA,EAGL,cAAc;AAAA,EACd,YAAY;AAAA,EACZ,MAAM;AAAA,EACN,OAAO;AAAA,EAEP,aAAa;AAAA,EACb,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B,aAAa,KAAK;AAAA,IAClB,QAAQ;AAAA,IACR,KAAK,cAAc,EAAE;AAAA,IACrB,KAAK,aAAa,cAAc,KAAK;AAAA;AAAA,EAEtC,MAAM;AAAA,EACN,OAAO,EAAE,QAAQ,MAAM,MAAM;AAAA;",
|
|
9
|
+
"debugId": "461C87FA3A5E330064756E2164756E21",
|
|
10
|
+
"names": []
|
|
11
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
// src/always-allowed.ts
|
|
2
|
+
function fingerprint(secret) {
|
|
3
|
+
if (secret === undefined)
|
|
4
|
+
return "";
|
|
5
|
+
let hash = 2166136261;
|
|
6
|
+
for (let i = 0;i < secret.length; i++)
|
|
7
|
+
hash = Math.imul(hash ^ secret.charCodeAt(i), 16777619) >>> 0;
|
|
8
|
+
return hash.toString(16);
|
|
9
|
+
}
|
|
10
|
+
var isString = (v) => typeof v === "string";
|
|
11
|
+
function parse(text) {
|
|
12
|
+
const none = { secret: "", grants: new Map };
|
|
13
|
+
let value;
|
|
14
|
+
try {
|
|
15
|
+
value = JSON.parse(text);
|
|
16
|
+
} catch {
|
|
17
|
+
return none;
|
|
18
|
+
}
|
|
19
|
+
const v = value;
|
|
20
|
+
if (!isString(v?.secret) || !Array.isArray(v.grants))
|
|
21
|
+
return none;
|
|
22
|
+
const grants = new Map;
|
|
23
|
+
for (const g of v.grants) {
|
|
24
|
+
const e = g;
|
|
25
|
+
if (!isString(e?.grantId) || typeof e.everything !== "boolean" || !Array.isArray(e.tools))
|
|
26
|
+
return none;
|
|
27
|
+
const tools = [];
|
|
28
|
+
for (const t of e.tools) {
|
|
29
|
+
const tool = t;
|
|
30
|
+
if (!isString(tool?.name) || !isString(tool.description) || tool.title !== undefined && !isString(tool.title))
|
|
31
|
+
return none;
|
|
32
|
+
tools.push(seenOf(tool));
|
|
33
|
+
}
|
|
34
|
+
grants.set(e.grantId, { everything: e.everything, tools });
|
|
35
|
+
}
|
|
36
|
+
return { secret: v.secret, grants };
|
|
37
|
+
}
|
|
38
|
+
function serialize(state) {
|
|
39
|
+
return JSON.stringify({
|
|
40
|
+
secret: state.secret,
|
|
41
|
+
grants: [...state.grants].map(([grantId, e]) => ({ grantId, ...e }))
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function seenOf(tool) {
|
|
45
|
+
const seen = { name: tool.name, description: tool.description };
|
|
46
|
+
if (tool.title !== undefined)
|
|
47
|
+
seen.title = tool.title;
|
|
48
|
+
return seen;
|
|
49
|
+
}
|
|
50
|
+
var sameSeen = (a, b) => a.title === b.title && a.description === b.description;
|
|
51
|
+
var counts = (kept, now) => now === undefined || sameSeen(kept, now);
|
|
52
|
+
var NO_TOOLS = {
|
|
53
|
+
list: () => [],
|
|
54
|
+
get: () => {
|
|
55
|
+
return;
|
|
56
|
+
},
|
|
57
|
+
onChange: () => () => {}
|
|
58
|
+
};
|
|
59
|
+
var isFunction = (v) => typeof v === "function";
|
|
60
|
+
function isInternals(value) {
|
|
61
|
+
if (!Array.isArray(value) || value.length !== 3)
|
|
62
|
+
return false;
|
|
63
|
+
const [registry, secrets, alwaysAllow] = value;
|
|
64
|
+
const r = registry;
|
|
65
|
+
const s = secrets;
|
|
66
|
+
return ["register", "list", "get", "onChange"].every((k) => isFunction(r?.[k])) && (s === undefined || isFunction(s?.current)) && [undefined, true, false, "until-reload"].includes(alwaysAllow);
|
|
67
|
+
}
|
|
68
|
+
function internalsOf(host) {
|
|
69
|
+
const internals = host._allowed;
|
|
70
|
+
return isInternals(internals) ? internals : [NO_TOOLS, undefined, undefined];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
class Allowances {
|
|
74
|
+
kept;
|
|
75
|
+
offered;
|
|
76
|
+
#mcp;
|
|
77
|
+
#registry;
|
|
78
|
+
#secret;
|
|
79
|
+
#storage;
|
|
80
|
+
#key;
|
|
81
|
+
#memory = { secret: "", grants: new Map };
|
|
82
|
+
#unsaved = false;
|
|
83
|
+
#listeners = new Set;
|
|
84
|
+
#unwatch;
|
|
85
|
+
constructor(mcp) {
|
|
86
|
+
const [registry, secrets, alwaysAllow] = internalsOf(mcp);
|
|
87
|
+
this.#mcp = mcp;
|
|
88
|
+
this.#registry = registry;
|
|
89
|
+
this.#secret = () => fingerprint(secrets?.current());
|
|
90
|
+
this.#storage = alwaysAllow === "until-reload" ? null : secrets?.storage ?? null;
|
|
91
|
+
this.#key = `${secrets?.key}#allowed`;
|
|
92
|
+
this.kept = this.#storage !== null;
|
|
93
|
+
this.offered = alwaysAllow === true || alwaysAllow === "until-reload";
|
|
94
|
+
}
|
|
95
|
+
tools = () => this.#registry.list().filter((t) => t.annotations.consequentialHint === true && (t.callers?.includes("relay") ?? true)).map((t) => {
|
|
96
|
+
const tool = {
|
|
97
|
+
name: t.name,
|
|
98
|
+
description: t.description,
|
|
99
|
+
destructive: t.annotations.destructiveHint === true
|
|
100
|
+
};
|
|
101
|
+
if (t.title !== undefined)
|
|
102
|
+
tool.title = t.title;
|
|
103
|
+
return tool;
|
|
104
|
+
});
|
|
105
|
+
list = (grantId) => (this.#read().get(grantId)?.tools ?? []).filter((t) => counts(t, this.#registry.get(t.name))).map((t) => t.name);
|
|
106
|
+
has = (grantId, name) => {
|
|
107
|
+
const tool = this.#registry.get(name);
|
|
108
|
+
return this.#granted(grantId) && (this.everything(grantId) || tool !== undefined && this.allows(grantId, tool));
|
|
109
|
+
};
|
|
110
|
+
allows(grantId, tool) {
|
|
111
|
+
if (!this.#granted(grantId))
|
|
112
|
+
return false;
|
|
113
|
+
const entry = this.#read().get(grantId);
|
|
114
|
+
if (entry === undefined)
|
|
115
|
+
return false;
|
|
116
|
+
if (entry.everything)
|
|
117
|
+
return true;
|
|
118
|
+
const kept = entry.tools.find((t) => t.name === tool.name);
|
|
119
|
+
return kept !== undefined && sameSeen(kept, tool);
|
|
120
|
+
}
|
|
121
|
+
allow = (grantId, name) => {
|
|
122
|
+
const tool = this.tools().find((t) => t.name === name);
|
|
123
|
+
if (tool !== undefined)
|
|
124
|
+
this.keep(grantId, tool);
|
|
125
|
+
};
|
|
126
|
+
keep(grantId, tool) {
|
|
127
|
+
if (!this.#granted(grantId))
|
|
128
|
+
return;
|
|
129
|
+
const grants = this.#read();
|
|
130
|
+
const entry = grants.get(grantId) ?? { everything: false, tools: [] };
|
|
131
|
+
entry.tools = [
|
|
132
|
+
...entry.tools.filter((t) => t.name !== tool.name),
|
|
133
|
+
seenOf(tool)
|
|
134
|
+
];
|
|
135
|
+
grants.set(grantId, entry);
|
|
136
|
+
this.#write(grants);
|
|
137
|
+
}
|
|
138
|
+
ask = (grantId, name) => {
|
|
139
|
+
const grants = this.#read();
|
|
140
|
+
const entry = grants.get(grantId);
|
|
141
|
+
if (entry === undefined)
|
|
142
|
+
return;
|
|
143
|
+
const tools = entry.tools.filter((t) => t.name !== name);
|
|
144
|
+
if (!entry.everything && tools.length === entry.tools.length)
|
|
145
|
+
return;
|
|
146
|
+
entry.everything = false;
|
|
147
|
+
entry.tools = tools;
|
|
148
|
+
if (tools.length === 0)
|
|
149
|
+
grants.delete(grantId);
|
|
150
|
+
this.#write(grants);
|
|
151
|
+
};
|
|
152
|
+
everything = (grantId) => this.#read().get(grantId)?.everything === true;
|
|
153
|
+
setEverything = (grantId, on) => {
|
|
154
|
+
if (on && !this.#granted(grantId))
|
|
155
|
+
return;
|
|
156
|
+
const grants = this.#read();
|
|
157
|
+
const entry = grants.get(grantId) ?? { everything: false, tools: [] };
|
|
158
|
+
if (entry.everything === on)
|
|
159
|
+
return;
|
|
160
|
+
entry.everything = on;
|
|
161
|
+
if (on || entry.tools.length > 0)
|
|
162
|
+
grants.set(grantId, entry);
|
|
163
|
+
else
|
|
164
|
+
grants.delete(grantId);
|
|
165
|
+
this.#write(grants);
|
|
166
|
+
};
|
|
167
|
+
forget = (grantId) => {
|
|
168
|
+
const grants = this.#read();
|
|
169
|
+
if (grants.delete(grantId))
|
|
170
|
+
this.#write(grants);
|
|
171
|
+
};
|
|
172
|
+
keepGrants(grantIds) {
|
|
173
|
+
const keep = new Set(grantIds);
|
|
174
|
+
const grants = this.#read();
|
|
175
|
+
let changed = this.#memory.grants !== grants && this.#memory.grants.size > 0;
|
|
176
|
+
for (const grantId of [...grants.keys()])
|
|
177
|
+
if (!keep.has(grantId))
|
|
178
|
+
changed = grants.delete(grantId) || changed;
|
|
179
|
+
if (changed)
|
|
180
|
+
this.#write(grants);
|
|
181
|
+
}
|
|
182
|
+
toolsChanged() {
|
|
183
|
+
const grants = this.#read();
|
|
184
|
+
let changed = false;
|
|
185
|
+
for (const [grantId, entry] of [...grants]) {
|
|
186
|
+
const tools = entry.tools.filter((t) => counts(t, this.#registry.get(t.name)));
|
|
187
|
+
if (tools.length === entry.tools.length)
|
|
188
|
+
continue;
|
|
189
|
+
changed = true;
|
|
190
|
+
entry.tools = tools;
|
|
191
|
+
if (!entry.everything && tools.length === 0)
|
|
192
|
+
grants.delete(grantId);
|
|
193
|
+
}
|
|
194
|
+
if (changed)
|
|
195
|
+
this.#write(grants);
|
|
196
|
+
else
|
|
197
|
+
this.#changed();
|
|
198
|
+
}
|
|
199
|
+
onChange = (listener) => {
|
|
200
|
+
this.#listeners.add(listener);
|
|
201
|
+
this.#watch();
|
|
202
|
+
return () => {
|
|
203
|
+
this.#listeners.delete(listener);
|
|
204
|
+
if (this.#listeners.size === 0) {
|
|
205
|
+
this.#unwatch?.();
|
|
206
|
+
this.#unwatch = undefined;
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
#watch() {
|
|
211
|
+
if (this.#unwatch !== undefined || this.#storage === null)
|
|
212
|
+
return;
|
|
213
|
+
const g = globalThis;
|
|
214
|
+
const changed = (event) => {
|
|
215
|
+
if (event.key === this.#key)
|
|
216
|
+
this.toolsChanged();
|
|
217
|
+
};
|
|
218
|
+
g.addEventListener?.("storage", changed);
|
|
219
|
+
this.#unwatch = () => g.removeEventListener?.("storage", changed);
|
|
220
|
+
}
|
|
221
|
+
#granted(grantId) {
|
|
222
|
+
return this.#mcp.grants.some((g) => g.grantId === grantId);
|
|
223
|
+
}
|
|
224
|
+
#read() {
|
|
225
|
+
if (this.#storage !== null && !this.#unsaved) {
|
|
226
|
+
try {
|
|
227
|
+
const text = this.#storage.getItem(this.#key);
|
|
228
|
+
if (text !== null)
|
|
229
|
+
this.#memory = parse(text);
|
|
230
|
+
} catch {}
|
|
231
|
+
}
|
|
232
|
+
return this.#memory.secret === this.#secret() ? this.#memory.grants : new Map;
|
|
233
|
+
}
|
|
234
|
+
#write(grants) {
|
|
235
|
+
this.#memory = { secret: this.#secret(), grants };
|
|
236
|
+
try {
|
|
237
|
+
this.#storage?.setItem(this.#key, serialize(this.#memory));
|
|
238
|
+
this.#unsaved = false;
|
|
239
|
+
} catch {
|
|
240
|
+
this.#unsaved = true;
|
|
241
|
+
}
|
|
242
|
+
this.#changed();
|
|
243
|
+
}
|
|
244
|
+
#changed() {
|
|
245
|
+
for (const listener of [...this.#listeners])
|
|
246
|
+
listener();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
var allowances = new WeakMap;
|
|
250
|
+
function allowedFor(mcp) {
|
|
251
|
+
const kept = allowances.get(mcp);
|
|
252
|
+
if (kept !== undefined)
|
|
253
|
+
return kept;
|
|
254
|
+
const made = new Allowances(mcp);
|
|
255
|
+
allowances.set(mcp, made);
|
|
256
|
+
mcp.addEventListener?.("grantschange", () => made.keepGrants(mcp.grants.map((g) => g.grantId)));
|
|
257
|
+
internalsOf(mcp)[0].onChange(() => made.toolsChanged());
|
|
258
|
+
made.toolsChanged();
|
|
259
|
+
if ("session" in mcp && mcp.session !== undefined)
|
|
260
|
+
made.keepGrants(mcp.grants.map((g) => g.grantId));
|
|
261
|
+
return made;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export { allowedFor };
|
|
265
|
+
|
|
266
|
+
//# debugId=22280D6FFA5A23FE64756E2164756E21
|
|
267
|
+
//# sourceMappingURL=index-s31xrfa2.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/always-allowed.ts"],
|
|
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.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