@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/react.js
CHANGED
|
@@ -18,6 +18,29 @@ import {
|
|
|
18
18
|
import {
|
|
19
19
|
TabMcp
|
|
20
20
|
} from "./index.js";
|
|
21
|
+
|
|
22
|
+
// src/text.ts
|
|
23
|
+
function format(template, values) {
|
|
24
|
+
return template.replace(/\{(\w+)\}/g, (all, key) => values[key] ?? all);
|
|
25
|
+
}
|
|
26
|
+
function inLanguage(make, lang) {
|
|
27
|
+
try {
|
|
28
|
+
return make(lang);
|
|
29
|
+
} catch {
|
|
30
|
+
return make("en");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
var isPlural = (value) => typeof value === "object" && value !== null && typeof value.other === "string" && Object.values(value).every((form) => typeof form === "string");
|
|
34
|
+
var warned = new WeakSet;
|
|
35
|
+
function pluralForm(text, n, s) {
|
|
36
|
+
const category = inLanguage((l) => new Intl.PluralRules(l), s.lang).select(n);
|
|
37
|
+
return format(text[category] ?? text.other, { n: String(n) });
|
|
38
|
+
}
|
|
39
|
+
function plural(text, n, s, values = {}) {
|
|
40
|
+
return format(pluralForm(text, n, s), values);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// src/react.ts
|
|
21
44
|
"use client";
|
|
22
45
|
var EMPTY = Object.freeze([]);
|
|
23
46
|
var SERVER_SNAPSHOT = Object.freeze({
|
|
@@ -25,17 +48,21 @@ var SERVER_SNAPSHOT = Object.freeze({
|
|
|
25
48
|
session: undefined,
|
|
26
49
|
grants: EMPTY,
|
|
27
50
|
pairingCode: undefined,
|
|
51
|
+
pairingUntil: undefined,
|
|
28
52
|
identity: "none",
|
|
29
53
|
identityMessage: undefined,
|
|
30
54
|
reconnecting: undefined,
|
|
31
|
-
tools: EMPTY
|
|
55
|
+
tools: EMPTY,
|
|
56
|
+
extension: undefined
|
|
32
57
|
});
|
|
33
58
|
var STATE_EVENTS = [
|
|
34
59
|
"statechange",
|
|
35
60
|
"grantschange",
|
|
36
61
|
"pairingcode",
|
|
62
|
+
"pairinguntil",
|
|
37
63
|
"identitychange",
|
|
38
|
-
"toolschange"
|
|
64
|
+
"toolschange",
|
|
65
|
+
"extensionchange"
|
|
39
66
|
];
|
|
40
67
|
|
|
41
68
|
class Store {
|
|
@@ -69,10 +96,12 @@ class Store {
|
|
|
69
96
|
session: mcp.session,
|
|
70
97
|
grants: mcp.grants,
|
|
71
98
|
pairingCode: mcp.pairingCode,
|
|
99
|
+
pairingUntil: mcp.pairingUntil,
|
|
72
100
|
identity: mcp.identity,
|
|
73
101
|
identityMessage: mcp.identityMessage,
|
|
74
102
|
reconnecting: mcp.reconnecting,
|
|
75
|
-
tools: tools.length === previous.tools.length && tools.every((t, i) => t === previous.tools[i]) ? previous.tools : tools
|
|
103
|
+
tools: tools.length === previous.tools.length && tools.every((t, i) => t === previous.tools[i]) ? previous.tools : tools,
|
|
104
|
+
extension: mcp.extension
|
|
76
105
|
};
|
|
77
106
|
const same = Object.keys(next).every((key) => next[key] === previous[key]);
|
|
78
107
|
return same ? previous : next;
|
|
@@ -233,14 +262,31 @@ function useToolCalls(options = {}) {
|
|
|
233
262
|
return calls;
|
|
234
263
|
}
|
|
235
264
|
var LOST_AFTER_ATTEMPTS = 3;
|
|
236
|
-
|
|
265
|
+
var BUTTON_TEXTS = {
|
|
266
|
+
lang: "en",
|
|
267
|
+
connectTitle: "Connect your AI app",
|
|
268
|
+
connecting: "Connecting…",
|
|
269
|
+
reconnecting: "Connection lost. Reconnecting…",
|
|
270
|
+
buttonReconnecting: "Reconnecting…",
|
|
271
|
+
buttonConnected: { one: "AI app connected", other: "{n} AI apps connected" }
|
|
272
|
+
};
|
|
273
|
+
function buttonTexts(strings) {
|
|
274
|
+
const s = { ...BUTTON_TEXTS };
|
|
275
|
+
for (const [key, english] of Object.entries(BUTTON_TEXTS)) {
|
|
276
|
+
const value = strings?.[key];
|
|
277
|
+
if (typeof english === "string" ? typeof value === "string" : isPlural(value))
|
|
278
|
+
s[key] = value;
|
|
279
|
+
}
|
|
280
|
+
return s;
|
|
281
|
+
}
|
|
282
|
+
function connectLabel(state, s) {
|
|
237
283
|
if (state.state === "connected" && state.grants.length > 0)
|
|
238
|
-
return
|
|
284
|
+
return plural(s.buttonConnected, state.grants.length, s);
|
|
239
285
|
if (state.state === "reconnecting")
|
|
240
|
-
return (state.reconnecting?.attempt ?? 0) > LOST_AFTER_ATTEMPTS ?
|
|
286
|
+
return (state.reconnecting?.attempt ?? 0) > LOST_AFTER_ATTEMPTS ? s.reconnecting : s.buttonReconnecting;
|
|
241
287
|
if (state.state === "connecting")
|
|
242
|
-
return
|
|
243
|
-
return
|
|
288
|
+
return s.connecting;
|
|
289
|
+
return s.connectTitle;
|
|
244
290
|
}
|
|
245
291
|
function ConnectButton({
|
|
246
292
|
children,
|
|
@@ -251,7 +297,7 @@ function ConnectButton({
|
|
|
251
297
|
const state = useTabMcpState();
|
|
252
298
|
if (!mcp.hasRelay)
|
|
253
299
|
return null;
|
|
254
|
-
const label = children === undefined ? connectLabel(state) : typeof children === "function" ? children(state) : children;
|
|
300
|
+
const label = children === undefined ? connectLabel(state, buttonTexts(mcp._ui?.strings)) : typeof children === "function" ? children(state) : children;
|
|
255
301
|
return createElement("button", {
|
|
256
302
|
type: "button",
|
|
257
303
|
"data-state": state.state,
|
|
@@ -267,6 +313,7 @@ function ConnectButton({
|
|
|
267
313
|
}, label);
|
|
268
314
|
}
|
|
269
315
|
export {
|
|
316
|
+
BUTTON_TEXTS,
|
|
270
317
|
ConnectButton,
|
|
271
318
|
TabMcpProvider,
|
|
272
319
|
useAllowedTools,
|
|
@@ -276,5 +323,5 @@ export {
|
|
|
276
323
|
useToolCalls
|
|
277
324
|
};
|
|
278
325
|
|
|
279
|
-
//# debugId=
|
|
326
|
+
//# debugId=6A93EB197910F78264756E2164756E21
|
|
280
327
|
//# sourceMappingURL=react.js.map
|
package/react.js.map
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/react.ts"],
|
|
3
|
+
"sources": ["../src/react.ts", "../src/text.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"\"use client\";\n/**\n * `@ractive-ch/tab-mcp-client/react`: React bindings for single-page apps\n * (iteration 9). One `TabMcp` per `<TabMcpProvider>`, hooks for its state,\n * its tools and the calls of AI apps, and a button that opens the connect\n * dialog. `react` (18.3 or 19) is an optional peer dependency; the core\n * entry stays free of it.\n */\n\nimport {\n\ttype ButtonHTMLAttributes,\n\tcreateContext,\n\tcreateElement,\n\ttype MouseEvent,\n\ttype ReactElement,\n\ttype ReactNode,\n\tuseCallback,\n\tuseContext,\n\tuseEffect,\n\tuseInsertionEffect,\n\tuseRef,\n\tuseState,\n\tuseSyncExternalStore,\n} from \"react\";\nimport {\n\ttype AllowedTool,\n\ttype AllowedTools,\n\tallowedTools,\n} from \"./allowed.ts\";\nimport {\n\ttype ConfirmRequest,\n\ttype ConnectionState,\n\ttype Grant,\n\ttype IdentityAssertionRequest,\n\ttype IdentityState,\n\ttype PairingCode,\n\ttype ReconnectingInfo,\n\ttype RegisterToolOptions,\n\ttype SessionInfo,\n\tTabMcp,\n\ttype TabMcpEventMap,\n\ttype TabMcpOptions,\n\ttype Tool,\n\ttype ToolCallEnd,\n\ttype ToolCallStart,\n} from \"./index.ts\";\n\n/** What `useTabMcpState()` returns: one object per change, the same object otherwise. */\nexport interface TabMcpSnapshot {\n\tstate: ConnectionState;\n\t/** The session while connected; `session.address` is the MCP address to give to AI apps. */\n\tsession: SessionInfo | undefined;\n\t/** The AI apps connected to this address. */\n\tgrants: readonly Grant[];\n\t/** The pairing code to show, while the connect dialog is open. */\n\tpairingCode: PairingCode | undefined;\n\t/** Who the relay knows as the page's user (identity binding). */\n\tidentity: IdentityState;\n\t/** Why the relay refused the identity (`identity: \"refused\"`). */\n\tidentityMessage: string | undefined;\n\t/** While `state` is `reconnecting`: the next attempt and when it starts. */\n\treconnecting: ReconnectingInfo | undefined;\n\t/** Names of the registered tools. */\n\ttools: readonly string[];\n}\n\nconst EMPTY: readonly never[] = Object.freeze([]);\n\n/** What the server renders, and what hydration starts from. */\nconst SERVER_SNAPSHOT: TabMcpSnapshot = Object.freeze({\n\tstate: \"disconnected\",\n\tsession: undefined,\n\tgrants: EMPTY,\n\tpairingCode: undefined,\n\tidentity: \"none\",\n\tidentityMessage: undefined,\n\treconnecting: undefined,\n\ttools: EMPTY,\n});\n\nconst STATE_EVENTS = [\n\t\"statechange\",\n\t\"grantschange\",\n\t\"pairingcode\",\n\t\"identitychange\",\n\t\"toolschange\",\n] as const satisfies readonly (keyof TabMcpEventMap)[];\n\n/** The external store of one `TabMcp`: a snapshot that changes only with the instance. */\nclass Store {\n\treadonly #mcp: TabMcp;\n\treadonly #listeners = new Set<() => void>();\n\t#snapshot: TabMcpSnapshot;\n\n\tconstructor(mcp: TabMcp) {\n\t\tthis.#mcp = mcp;\n\t\tthis.#snapshot = this.#read(SERVER_SNAPSHOT);\n\t\tconst update = () => {\n\t\t\tconst next = this.#read(this.#snapshot);\n\t\t\tif (next === this.#snapshot) return;\n\t\t\tthis.#snapshot = next;\n\t\t\tfor (const listener of [...this.#listeners]) listener();\n\t\t};\n\t\tfor (const type of STATE_EVENTS) mcp.addEventListener(type, update);\n\t}\n\n\treadonly subscribe = (listener: () => void): (() => void) => {\n\t\tthis.#listeners.add(listener);\n\t\treturn () => this.#listeners.delete(listener);\n\t};\n\n\treadonly getSnapshot = (): TabMcpSnapshot => this.#snapshot;\n\n\t/** The current values; `previous` itself when none changed. */\n\t#read(previous: TabMcpSnapshot): TabMcpSnapshot {\n\t\tconst mcp = this.#mcp;\n\t\tconst tools = mcp.tools;\n\t\tconst next: TabMcpSnapshot = {\n\t\t\tstate: mcp.state,\n\t\t\tsession: mcp.session,\n\t\t\tgrants: mcp.grants,\n\t\t\tpairingCode: mcp.pairingCode,\n\t\t\tidentity: mcp.identity,\n\t\t\tidentityMessage: mcp.identityMessage,\n\t\t\treconnecting: mcp.reconnecting,\n\t\t\ttools:\n\t\t\t\ttools.length === previous.tools.length &&\n\t\t\t\ttools.every((t, i) => t === previous.tools[i])\n\t\t\t\t\t? previous.tools\n\t\t\t\t\t: tools,\n\t\t};\n\t\tconst same = (Object.keys(next) as (keyof TabMcpSnapshot)[]).every(\n\t\t\t(key) => next[key] === previous[key],\n\t\t);\n\t\treturn same ? previous : next;\n\t}\n}\n\nconst stores = new WeakMap<TabMcp, Store>();\n\nfunction storeOf(mcp: TabMcp): Store {\n\tlet store = stores.get(mcp);\n\tif (store === undefined) {\n\t\tstore = new Store(mcp);\n\t\tstores.set(mcp, store);\n\t}\n\treturn store;\n}\n\nconst Context = createContext<TabMcp | undefined>(undefined);\n\n/** Props of `TabMcpProvider`: the `TabMcp` options, or an instance of your own. */\nexport type TabMcpProviderProps = (\n\t| (TabMcpOptions & { client?: undefined })\n\t| { client: TabMcp }\n) & { children?: ReactNode };\n\n/**\n * Makes one `TabMcp` for its tree from the props of its first render\n * (`relay` may be left out, as for `TabMcp`). A\n * changed `relay` or `server.name` makes a new instance (and warns); other\n * changes are ignored, but `onConfirm` and `identityAssertion` are always\n * the latest ones (also when added or removed later). Pass `client` to use an instance of your own instead. The\n * provider's own instance is disconnected when the provider unmounts; it\n * never connects on the server.\n */\nexport function TabMcpProvider(props: TabMcpProviderProps): ReactElement {\n\tconst latest = useRef(props);\n\tuseInsertionEffect(() => {\n\t\tlatest.current = props;\n\t});\n\tconst own = props.client === undefined ? props : undefined;\n\tconst relay = own?.relay;\n\tconst serverName = own?.server?.name;\n\tconst create = (): TabMcp => {\n\t\tconst {\n\t\t\tchildren: _,\n\t\t\tclient: __,\n\t\t\t...options\n\t\t} = props as TabMcpOptions & {\n\t\t\tchildren?: ReactNode;\n\t\t\tclient?: undefined;\n\t\t};\n\t\t// Always installed, so that a handler added, changed or removed in a\n\t\t// later render counts: without one, the library's own behaviour.\n\t\tconst handlers = () => latest.current as Partial<TabMcpOptions>;\n\t\tconst instance: TabMcp = new TabMcp({\n\t\t\t...options,\n\t\t\tonConfirm: (request: ConfirmRequest) => {\n\t\t\t\tconst ask = handlers().onConfirm;\n\t\t\t\treturn ask === undefined\n\t\t\t\t\t? instance.confirmInPage(request)\n\t\t\t\t\t: ask(request);\n\t\t\t},\n\t\t\tidentityAssertion: (request: IdentityAssertionRequest) =>\n\t\t\t\thandlers().identityAssertion?.(request) ?? null,\n\t\t});\n\t\treturn instance;\n\t};\n\t// In state, not in `useMemo`, which React may discard and run again. A\n\t// changed relay or server name replaces it while rendering, as React\n\t// recommends for state that follows props.\n\tconst [state, setState] = useState(() =>\n\t\town === undefined ? undefined : { relay, serverName, mcp: create() },\n\t);\n\tlet current = state;\n\tif (own === undefined) {\n\t\tcurrent = undefined;\n\t} else if (\n\t\tcurrent === undefined ||\n\t\tcurrent.relay !== relay ||\n\t\tcurrent.serverName !== serverName\n\t) {\n\t\tcurrent = { relay, serverName, mcp: create() };\n\t}\n\tif (current !== state) setState(current);\n\tconst mcp = props.client ?? (current as { mcp: TabMcp }).mcp;\n\n\tconst previous = useRef<TabMcp | undefined>(undefined);\n\t/** The instance the mounted effect is for; `undefined` after its cleanup. */\n\tconst active = useRef<TabMcp | undefined>(undefined);\n\tuseEffect(() => {\n\t\tif (\n\t\t\tprevious.current !== undefined &&\n\t\t\tprevious.current !== mcp &&\n\t\t\tprops.client === undefined\n\t\t) {\n\t\t\tconsole.warn(\n\t\t\t\t\"TabMcpProvider: relay or server.name changed, so the page has a new TabMcp; its tools and connection start over\",\n\t\t\t);\n\t\t}\n\t\tprevious.current = mcp;\n\t\tif (props.client !== undefined) return;\n\t\tactive.current = mcp;\n\t\t// Disconnected a moment later, unless the effect ran again for the same\n\t\t// instance meanwhile: StrictMode's simulated unmount and mount would\n\t\t// otherwise cancel a `connect()` that a child's effect started (CU 24).\n\t\treturn () => {\n\t\t\tactive.current = undefined;\n\t\t\tqueueMicrotask(() => {\n\t\t\t\tif (active.current !== mcp) mcp.disconnect();\n\t\t\t});\n\t\t};\n\t}, [mcp, props.client]);\n\n\treturn createElement(Context.Provider, { value: mcp }, props.children);\n}\n\n/** The provider's `TabMcp`, for everything the hooks do not cover. */\nexport function useTabMcp(): TabMcp {\n\tconst mcp = useContext(Context);\n\tif (mcp === undefined)\n\t\tthrow new Error(\"useTabMcp: no <TabMcpProvider> above this component\");\n\treturn mcp;\n}\n\n/**\n * The connection state, address, grants, pairing code, identity and\n * reconnecting of the provider's `TabMcp`. Re-renders only when one of them\n * changes; the server and hydration see `disconnected`.\n */\nexport function useTabMcpState(): TabMcpSnapshot {\n\tconst store = storeOf(useTabMcp());\n\treturn useSyncExternalStore(\n\t\tstore.subscribe,\n\t\tstore.getSnapshot,\n\t\t() => SERVER_SNAPSHOT,\n\t);\n}\n\n/** What `useAllowedTools(grantId)` returns: one object per change, the same object otherwise. */\nexport interface AllowedToolsSnapshot {\n\t/** The page's tools that ask, each with whether it runs without asking for this app. */\n\ttools: (AllowedTool & { allowed: boolean })[];\n\t/** \"Allow everything, no questions asked\" is on for this app. */\n\teverything: boolean;\n\t/** To change them: `allow`, `ask`, `setEverything`, `forget`. */\n\tallowedTools: AllowedTools;\n}\n\n/**\n * The tools that run without asking for one connected AI app\n * (`grant.grantId`), for switches of the page's own: `allowedTools(mcp)` of\n * `@ractive-ch/tab-mcp-client/allowed` as state. Re-renders when they or the\n * tools change; the server and hydration see none.\n */\nexport function useAllowedTools(grantId: string): AllowedToolsSnapshot {\n\tconst allowed = allowedTools(useTabMcp());\n\tconst subscribe = useCallback(\n\t\t(changed: () => void) => allowed.onChange(changed),\n\t\t[allowed],\n\t);\n\tconst last = useRef<{ key: string; snapshot: AllowedToolsSnapshot }>(\n\t\tundefined,\n\t);\n\tconst server = useRef<AllowedToolsSnapshot>(undefined);\n\t// Reads only: a changed tool left the list already.\n\tconst getSnapshot = () => {\n\t\tconst everything = allowed.everything(grantId);\n\t\tconst listed = new Set(allowed.list(grantId));\n\t\tconst tools = allowed\n\t\t\t.tools()\n\t\t\t.map((t) => ({ ...t, allowed: everything || listed.has(t.name) }));\n\t\tconst key = JSON.stringify([grantId, everything, tools]);\n\t\tif (\n\t\t\tlast.current?.key !== key ||\n\t\t\tlast.current.snapshot.allowedTools !== allowed\n\t\t)\n\t\t\tlast.current = {\n\t\t\t\tkey,\n\t\t\t\tsnapshot: { tools, everything, allowedTools: allowed },\n\t\t\t};\n\t\treturn last.current.snapshot;\n\t};\n\tconst getServerSnapshot = () => {\n\t\tif (server.current?.allowedTools !== allowed)\n\t\t\tserver.current = { tools: [], everything: false, allowedTools: allowed };\n\t\treturn server.current;\n\t};\n\treturn useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n\n/** Options of `useTool`: `RegisterToolOptions` without the signal (the hook has its own). */\nexport type UseToolOptions = Omit<RegisterToolOptions, \"signal\">;\n\n/**\n * Registers a tool while the component is mounted (with an `AbortSignal`,\n * aborted on unmount). `execute` is always the latest one, so a new closure\n * on each render does not register again; the tool is registered again only\n * when its name, title, description, input or output schema or annotations\n * change, or `options.callers` or `options.exposedTo` (a tool allowed until reload stays allowed when it is\n * registered again unchanged). A duplicate name or an invalid tool is thrown\n * from the component's next render, for an error boundary.\n */\nexport function useTool<Input extends object = Record<string, unknown>>(\n\ttool: Tool<Input>,\n\toptions: UseToolOptions = {},\n): void {\n\tconst mcp = useTabMcp();\n\tconst latest = useRef(tool);\n\tuseInsertionEffect(() => {\n\t\tlatest.current = tool;\n\t});\n\tconst [failure, setFailure] = useState<{ error: unknown } | undefined>();\n\tif (failure !== undefined) throw failure.error;\n\tconst shape = JSON.stringify([\n\t\ttool.name,\n\t\ttool.title,\n\t\ttool.description,\n\t\ttool.inputSchema,\n\t\ttool.outputSchema,\n\t\ttool.annotations,\n\t\toptions.callers,\n\t\toptions.exposedTo,\n\t]);\n\t// biome-ignore lint/correctness/useExhaustiveDependencies: `shape` stands for the tool and its options; `execute` is read through the ref\n\tuseEffect(() => {\n\t\tconst controller = new AbortController();\n\t\tconst { execute: _, ...fields } = latest.current;\n\t\tmcp\n\t\t\t.registerTool(\n\t\t\t\t{\n\t\t\t\t\t...fields,\n\t\t\t\t\texecute: (input: Input, options) =>\n\t\t\t\t\t\tlatest.current.execute(input, options),\n\t\t\t\t},\n\t\t\t\t{ ...options, signal: controller.signal },\n\t\t\t)\n\t\t\t.catch((error: unknown) => {\n\t\t\t\tif (!controller.signal.aborted) setFailure({ error });\n\t\t\t});\n\t\treturn () => controller.abort();\n\t}, [mcp, shape]);\n}\n\n/** A tool call of an AI app, as `useToolCalls()` lists it. */\nexport interface ToolCallRecord extends ToolCallStart {\n\t/** When it started, `Date.now()`. */\n\tstartedAt: number;\n\t/** How it ended; missing while it runs. */\n\tend?: ToolCallEnd;\n}\n\nexport interface UseToolCallsOptions {\n\t/** How many calls to keep; default 20. */\n\tlimit?: number;\n}\n\n/**\n * The recent tool calls of AI apps since the component mounted, newest\n * first, each with its end once it is over: for an activity view.\n */\nexport function useToolCalls(\n\toptions: UseToolCallsOptions = {},\n): readonly ToolCallRecord[] {\n\tconst mcp = useTabMcp();\n\tconst limit = Math.max(0, options.limit ?? 20);\n\tconst [calls, setCalls] = useState<readonly ToolCallRecord[]>(EMPTY);\n\tuseEffect(() => {\n\t\tconst started = (event: TabMcpEventMap[\"toolcall\"]) => {\n\t\t\tconst record: ToolCallRecord = { ...event.detail, startedAt: Date.now() };\n\t\t\tsetCalls((list) => [record, ...list].slice(0, limit));\n\t\t};\n\t\tconst ended = (event: TabMcpEventMap[\"toolcallend\"]) => {\n\t\t\tconst end = event.detail;\n\t\t\tsetCalls((list) =>\n\t\t\t\tlist.some((r) => r.id === end.id)\n\t\t\t\t\t? list.map((r) => (r.id === end.id ? { ...r, end } : r))\n\t\t\t\t\t: list,\n\t\t\t);\n\t\t};\n\t\tmcp.addEventListener(\"toolcall\", started);\n\t\tmcp.addEventListener(\"toolcallend\", ended);\n\t\tsetCalls((list) => (list.length > limit ? list.slice(0, limit) : list));\n\t\treturn () => {\n\t\t\tmcp.removeEventListener(\"toolcall\", started);\n\t\t\tmcp.removeEventListener(\"toolcallend\", ended);\n\t\t};\n\t}, [mcp, limit]);\n\treturn calls;\n}\n\n/** Props of `ConnectButton`: a button's, with `children` that may depend on the state. */\nexport interface ConnectButtonProps\n\textends Omit<ButtonHTMLAttributes<HTMLButtonElement>, \"children\" | \"type\"> {\n\t/** The label; default: the state (\"Connect your AI app\", \"AI app connected\"). */\n\tchildren?: ReactNode | ((state: TabMcpSnapshot) => ReactNode);\n}\n\n/** Failed reconnect attempts after which the default label says the connection is lost. */\nconst LOST_AFTER_ATTEMPTS = 3;\n\n/** The default label of `ConnectButton`. */\nfunction connectLabel(state: TabMcpSnapshot): string {\n\tif (state.state === \"connected\" && state.grants.length > 0)\n\t\treturn state.grants.length === 1\n\t\t\t? \"AI app connected\"\n\t\t\t: `${state.grants.length} AI apps connected`;\n\tif (state.state === \"reconnecting\")\n\t\treturn (state.reconnecting?.attempt ?? 0) > LOST_AFTER_ATTEMPTS\n\t\t\t? \"Connection lost, retrying…\"\n\t\t\t: \"Reconnecting…\";\n\tif (state.state === \"connecting\") return \"Connecting…\";\n\treturn \"Connect your AI app\";\n}\n\n/**\n * A button that opens the library's \"Connect your AI app\" dialog\n * (`showConnectDialog()`), labelled with the state. `data-state` is\n * `disconnected`, `connecting`, `connected` or `reconnecting`, for styling.\n * An `onClick` that calls `event.preventDefault()` keeps the dialog closed.\n * Renders nothing for a `TabMcp` without a relay: there is nothing to\n * connect.\n */\nexport function ConnectButton({\n\tchildren,\n\tonClick,\n\t...rest\n}: ConnectButtonProps): ReactElement | null {\n\tconst mcp = useTabMcp();\n\tconst state = useTabMcpState();\n\tif (!mcp.hasRelay) return null;\n\tconst label =\n\t\tchildren === undefined\n\t\t\t? connectLabel(state)\n\t\t\t: typeof children === \"function\"\n\t\t\t\t? children(state)\n\t\t\t\t: children;\n\treturn createElement(\n\t\t\"button\",\n\t\t{\n\t\t\ttype: \"button\",\n\t\t\t\"data-state\": state.state,\n\t\t\t...rest,\n\t\t\tonClick: (event: MouseEvent<HTMLButtonElement>) => {\n\t\t\t\tonClick?.(event);\n\t\t\t\tif (event.defaultPrevented) return;\n\t\t\t\tmcp.showConnectDialog().catch((error: unknown) => {\n\t\t\t\t\tconsole.error(error);\n\t\t\t\t});\n\t\t\t},\n\t\t},\n\t\tlabel,\n\t);\n}\n"
|
|
5
|
+
"\"use client\";\n/**\n * `@ractive-ch/tab-mcp-client/react`: React bindings for single-page apps\n * (iteration 9). One `TabMcp` per `<TabMcpProvider>`, hooks for its state,\n * its tools and the calls of AI apps, and a button that opens the connect\n * dialog. `react` (18.3 or 19) is an optional peer dependency; the core\n * entry stays free of it.\n */\n\nimport {\n\ttype ButtonHTMLAttributes,\n\tcreateContext,\n\tcreateElement,\n\ttype MouseEvent,\n\ttype ReactElement,\n\ttype ReactNode,\n\tuseCallback,\n\tuseContext,\n\tuseEffect,\n\tuseInsertionEffect,\n\tuseRef,\n\tuseState,\n\tuseSyncExternalStore,\n} from \"react\";\nimport {\n\ttype AllowedTool,\n\ttype AllowedTools,\n\tallowedTools,\n} from \"./allowed.ts\";\nimport {\n\ttype BrowserExtension,\n\ttype ConfirmRequest,\n\ttype ConnectionState,\n\ttype Grant,\n\ttype IdentityAssertionRequest,\n\ttype IdentityState,\n\ttype PairingCode,\n\ttype ReconnectingInfo,\n\ttype RegisterToolOptions,\n\ttype SessionInfo,\n\tTabMcp,\n\ttype TabMcpEventMap,\n\ttype TabMcpOptions,\n\ttype Tool,\n\ttype ToolCallEnd,\n\ttype ToolCallStart,\n} from \"./index.ts\";\nimport type { DialogStrings } from \"./strings.ts\";\nimport { isPlural, plural } from \"./text.ts\";\n\n/** What `useTabMcpState()` returns: one object per change, the same object otherwise. */\nexport interface TabMcpSnapshot {\n\tstate: ConnectionState;\n\t/** The session while connected; `session.address` is the MCP address to give to AI apps. */\n\tsession: SessionInfo | undefined;\n\t/** The AI apps connected to this address. */\n\tgrants: readonly Grant[];\n\t/** The pairing code to show, while the connect dialog is open. */\n\tpairingCode: PairingCode | undefined;\n\t/** Identity binding: until when a sign-in pairs the page (`TabMcp.pairingUntil`). */\n\tpairingUntil: string | undefined;\n\t/** Who the relay knows as the page's user (identity binding). */\n\tidentity: IdentityState;\n\t/** Why the relay refused the identity (`identity: \"refused\"`). */\n\tidentityMessage: string | undefined;\n\t/** While `state` is `reconnecting`: the next attempt and when it starts. */\n\treconnecting: ReconnectingInfo | undefined;\n\t/** Names of the registered tools. */\n\ttools: readonly string[];\n\t/**\n\t * The tab-mcp browser extension while it serves the page, with the AI\n\t * apps connected through it (`TabMcp.extension`, `webmcp: true`).\n\t */\n\textension: BrowserExtension | undefined;\n}\n\nconst EMPTY: readonly never[] = Object.freeze([]);\n\n/** What the server renders, and what hydration starts from. */\nconst SERVER_SNAPSHOT: TabMcpSnapshot = Object.freeze({\n\tstate: \"disconnected\",\n\tsession: undefined,\n\tgrants: EMPTY,\n\tpairingCode: undefined,\n\tpairingUntil: undefined,\n\tidentity: \"none\",\n\tidentityMessage: undefined,\n\treconnecting: undefined,\n\ttools: EMPTY,\n\textension: undefined,\n});\n\nconst STATE_EVENTS = [\n\t\"statechange\",\n\t\"grantschange\",\n\t\"pairingcode\",\n\t\"pairinguntil\",\n\t\"identitychange\",\n\t\"toolschange\",\n\t\"extensionchange\",\n] as const satisfies readonly (keyof TabMcpEventMap)[];\n\n/** The external store of one `TabMcp`: a snapshot that changes only with the instance. */\nclass Store {\n\treadonly #mcp: TabMcp;\n\treadonly #listeners = new Set<() => void>();\n\t#snapshot: TabMcpSnapshot;\n\n\tconstructor(mcp: TabMcp) {\n\t\tthis.#mcp = mcp;\n\t\tthis.#snapshot = this.#read(SERVER_SNAPSHOT);\n\t\tconst update = () => {\n\t\t\tconst next = this.#read(this.#snapshot);\n\t\t\tif (next === this.#snapshot) return;\n\t\t\tthis.#snapshot = next;\n\t\t\tfor (const listener of [...this.#listeners]) listener();\n\t\t};\n\t\tfor (const type of STATE_EVENTS) mcp.addEventListener(type, update);\n\t}\n\n\treadonly subscribe = (listener: () => void): (() => void) => {\n\t\tthis.#listeners.add(listener);\n\t\treturn () => this.#listeners.delete(listener);\n\t};\n\n\treadonly getSnapshot = (): TabMcpSnapshot => this.#snapshot;\n\n\t/** The current values; `previous` itself when none changed. */\n\t#read(previous: TabMcpSnapshot): TabMcpSnapshot {\n\t\tconst mcp = this.#mcp;\n\t\tconst tools = mcp.tools;\n\t\tconst next: TabMcpSnapshot = {\n\t\t\tstate: mcp.state,\n\t\t\tsession: mcp.session,\n\t\t\tgrants: mcp.grants,\n\t\t\tpairingCode: mcp.pairingCode,\n\t\t\tpairingUntil: mcp.pairingUntil,\n\t\t\tidentity: mcp.identity,\n\t\t\tidentityMessage: mcp.identityMessage,\n\t\t\treconnecting: mcp.reconnecting,\n\t\t\ttools:\n\t\t\t\ttools.length === previous.tools.length &&\n\t\t\t\ttools.every((t, i) => t === previous.tools[i])\n\t\t\t\t\t? previous.tools\n\t\t\t\t\t: tools,\n\t\t\textension: mcp.extension,\n\t\t};\n\t\tconst same = (Object.keys(next) as (keyof TabMcpSnapshot)[]).every(\n\t\t\t(key) => next[key] === previous[key],\n\t\t);\n\t\treturn same ? previous : next;\n\t}\n}\n\nconst stores = new WeakMap<TabMcp, Store>();\n\nfunction storeOf(mcp: TabMcp): Store {\n\tlet store = stores.get(mcp);\n\tif (store === undefined) {\n\t\tstore = new Store(mcp);\n\t\tstores.set(mcp, store);\n\t}\n\treturn store;\n}\n\nconst Context = createContext<TabMcp | undefined>(undefined);\n\n/** Props of `TabMcpProvider`: the `TabMcp` options, or an instance of your own. */\nexport type TabMcpProviderProps = (\n\t| (TabMcpOptions & { client?: undefined })\n\t| { client: TabMcp }\n) & { children?: ReactNode };\n\n/**\n * Makes one `TabMcp` for its tree from the props of its first render\n * (`relay` may be left out, as for `TabMcp`). A\n * changed `relay` or `server.name` makes a new instance (and warns); other\n * changes are ignored, but `onConfirm` and `identityAssertion` are always\n * the latest ones (also when added or removed later). Pass `client` to use an instance of your own instead. The\n * provider's own instance is disconnected when the provider unmounts; it\n * never connects on the server.\n */\nexport function TabMcpProvider(props: TabMcpProviderProps): ReactElement {\n\tconst latest = useRef(props);\n\tuseInsertionEffect(() => {\n\t\tlatest.current = props;\n\t});\n\tconst own = props.client === undefined ? props : undefined;\n\tconst relay = own?.relay;\n\tconst serverName = own?.server?.name;\n\tconst create = (): TabMcp => {\n\t\tconst {\n\t\t\tchildren: _,\n\t\t\tclient: __,\n\t\t\t...options\n\t\t} = props as TabMcpOptions & {\n\t\t\tchildren?: ReactNode;\n\t\t\tclient?: undefined;\n\t\t};\n\t\t// Always installed, so that a handler added, changed or removed in a\n\t\t// later render counts: without one, the library's own behaviour.\n\t\tconst handlers = () => latest.current as Partial<TabMcpOptions>;\n\t\tconst instance: TabMcp = new TabMcp({\n\t\t\t...options,\n\t\t\tonConfirm: (request: ConfirmRequest) => {\n\t\t\t\tconst ask = handlers().onConfirm;\n\t\t\t\treturn ask === undefined\n\t\t\t\t\t? instance.confirmInPage(request)\n\t\t\t\t\t: ask(request);\n\t\t\t},\n\t\t\tidentityAssertion: (request: IdentityAssertionRequest) =>\n\t\t\t\thandlers().identityAssertion?.(request) ?? null,\n\t\t});\n\t\treturn instance;\n\t};\n\t// In state, not in `useMemo`, which React may discard and run again. A\n\t// changed relay or server name replaces it while rendering, as React\n\t// recommends for state that follows props.\n\tconst [state, setState] = useState(() =>\n\t\town === undefined ? undefined : { relay, serverName, mcp: create() },\n\t);\n\tlet current = state;\n\tif (own === undefined) {\n\t\tcurrent = undefined;\n\t} else if (\n\t\tcurrent === undefined ||\n\t\tcurrent.relay !== relay ||\n\t\tcurrent.serverName !== serverName\n\t) {\n\t\tcurrent = { relay, serverName, mcp: create() };\n\t}\n\tif (current !== state) setState(current);\n\tconst mcp = props.client ?? (current as { mcp: TabMcp }).mcp;\n\n\tconst previous = useRef<TabMcp | undefined>(undefined);\n\t/** The instance the mounted effect is for; `undefined` after its cleanup. */\n\tconst active = useRef<TabMcp | undefined>(undefined);\n\tuseEffect(() => {\n\t\tif (\n\t\t\tprevious.current !== undefined &&\n\t\t\tprevious.current !== mcp &&\n\t\t\tprops.client === undefined\n\t\t) {\n\t\t\tconsole.warn(\n\t\t\t\t\"TabMcpProvider: relay or server.name changed, so the page has a new TabMcp; its tools and connection start over\",\n\t\t\t);\n\t\t}\n\t\tprevious.current = mcp;\n\t\tif (props.client !== undefined) return;\n\t\tactive.current = mcp;\n\t\t// Disconnected a moment later, unless the effect ran again for the same\n\t\t// instance meanwhile: StrictMode's simulated unmount and mount would\n\t\t// otherwise cancel a `connect()` that a child's effect started (CU 24).\n\t\treturn () => {\n\t\t\tactive.current = undefined;\n\t\t\tqueueMicrotask(() => {\n\t\t\t\tif (active.current !== mcp) mcp.disconnect();\n\t\t\t});\n\t\t};\n\t}, [mcp, props.client]);\n\n\treturn createElement(Context.Provider, { value: mcp }, props.children);\n}\n\n/** The provider's `TabMcp`, for everything the hooks do not cover. */\nexport function useTabMcp(): TabMcp {\n\tconst mcp = useContext(Context);\n\tif (mcp === undefined)\n\t\tthrow new Error(\"useTabMcp: no <TabMcpProvider> above this component\");\n\treturn mcp;\n}\n\n/**\n * The connection state, address, grants, pairing code, identity,\n * reconnecting and the tab-mcp browser extension of the provider's `TabMcp`. Re-renders only when one of them\n * changes; the server and hydration see `disconnected`.\n */\nexport function useTabMcpState(): TabMcpSnapshot {\n\tconst store = storeOf(useTabMcp());\n\treturn useSyncExternalStore(\n\t\tstore.subscribe,\n\t\tstore.getSnapshot,\n\t\t() => SERVER_SNAPSHOT,\n\t);\n}\n\n/** What `useAllowedTools(grantId)` returns: one object per change, the same object otherwise. */\nexport interface AllowedToolsSnapshot {\n\t/** The page's tools that ask, each with whether it runs without asking for this app. */\n\ttools: (AllowedTool & { allowed: boolean })[];\n\t/** \"Always allow everything\" is on for this app. */\n\teverything: boolean;\n\t/** To change them: `allow`, `ask`, `setEverything`, `forget`. */\n\tallowedTools: AllowedTools;\n}\n\n/**\n * The tools that run without asking for one connected AI app\n * (`grant.grantId`), for switches of the page's own: `allowedTools(mcp)` of\n * `@ractive-ch/tab-mcp-client/allowed` as state. Re-renders when they or the\n * tools change; the server and hydration see none.\n */\nexport function useAllowedTools(grantId: string): AllowedToolsSnapshot {\n\tconst allowed = allowedTools(useTabMcp());\n\tconst subscribe = useCallback(\n\t\t(changed: () => void) => allowed.onChange(changed),\n\t\t[allowed],\n\t);\n\tconst last = useRef<{ key: string; snapshot: AllowedToolsSnapshot }>(\n\t\tundefined,\n\t);\n\tconst server = useRef<AllowedToolsSnapshot>(undefined);\n\t// Reads only: a changed tool left the list already.\n\tconst getSnapshot = () => {\n\t\tconst everything = allowed.everything(grantId);\n\t\tconst listed = new Set(allowed.list(grantId));\n\t\tconst tools = allowed\n\t\t\t.tools()\n\t\t\t.map((t) => ({ ...t, allowed: everything || listed.has(t.name) }));\n\t\tconst key = JSON.stringify([grantId, everything, tools]);\n\t\tif (\n\t\t\tlast.current?.key !== key ||\n\t\t\tlast.current.snapshot.allowedTools !== allowed\n\t\t)\n\t\t\tlast.current = {\n\t\t\t\tkey,\n\t\t\t\tsnapshot: { tools, everything, allowedTools: allowed },\n\t\t\t};\n\t\treturn last.current.snapshot;\n\t};\n\tconst getServerSnapshot = () => {\n\t\tif (server.current?.allowedTools !== allowed)\n\t\t\tserver.current = { tools: [], everything: false, allowedTools: allowed };\n\t\treturn server.current;\n\t};\n\treturn useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n\n/** Options of `useTool`: `RegisterToolOptions` without the signal (the hook has its own). */\nexport type UseToolOptions = Omit<RegisterToolOptions, \"signal\">;\n\n/**\n * Registers a tool while the component is mounted (with an `AbortSignal`,\n * aborted on unmount). `execute` is always the latest one, so a new closure\n * on each render does not register again; the tool is registered again only\n * when its name, title, description, input or output schema or annotations\n * change, or `options.callers` or `options.exposedTo` (a tool allowed until reload stays allowed when it is\n * registered again unchanged). A duplicate name or an invalid tool is thrown\n * from the component's next render, for an error boundary.\n */\nexport function useTool<Input extends object = Record<string, unknown>>(\n\ttool: Tool<Input>,\n\toptions: UseToolOptions = {},\n): void {\n\tconst mcp = useTabMcp();\n\tconst latest = useRef(tool);\n\tuseInsertionEffect(() => {\n\t\tlatest.current = tool;\n\t});\n\tconst [failure, setFailure] = useState<{ error: unknown } | undefined>();\n\tif (failure !== undefined) throw failure.error;\n\tconst shape = JSON.stringify([\n\t\ttool.name,\n\t\ttool.title,\n\t\ttool.description,\n\t\ttool.inputSchema,\n\t\ttool.outputSchema,\n\t\ttool.annotations,\n\t\toptions.callers,\n\t\toptions.exposedTo,\n\t]);\n\t// biome-ignore lint/correctness/useExhaustiveDependencies: `shape` stands for the tool and its options; `execute` is read through the ref\n\tuseEffect(() => {\n\t\tconst controller = new AbortController();\n\t\tconst { execute: _, ...fields } = latest.current;\n\t\tmcp\n\t\t\t.registerTool(\n\t\t\t\t{\n\t\t\t\t\t...fields,\n\t\t\t\t\texecute: (input: Input, options) =>\n\t\t\t\t\t\tlatest.current.execute(input, options),\n\t\t\t\t},\n\t\t\t\t{ ...options, signal: controller.signal },\n\t\t\t)\n\t\t\t.catch((error: unknown) => {\n\t\t\t\tif (!controller.signal.aborted) setFailure({ error });\n\t\t\t});\n\t\treturn () => controller.abort();\n\t}, [mcp, shape]);\n}\n\n/** A tool call of an AI app, as `useToolCalls()` lists it. */\nexport interface ToolCallRecord extends ToolCallStart {\n\t/** When it started, `Date.now()`. */\n\tstartedAt: number;\n\t/** How it ended; missing while it runs. */\n\tend?: ToolCallEnd;\n}\n\nexport interface UseToolCallsOptions {\n\t/** How many calls to keep; default 20. */\n\tlimit?: number;\n}\n\n/**\n * The recent tool calls of AI apps since the component mounted, newest\n * first, each with its end once it is over: for an activity view.\n */\nexport function useToolCalls(\n\toptions: UseToolCallsOptions = {},\n): readonly ToolCallRecord[] {\n\tconst mcp = useTabMcp();\n\tconst limit = Math.max(0, options.limit ?? 20);\n\tconst [calls, setCalls] = useState<readonly ToolCallRecord[]>(EMPTY);\n\tuseEffect(() => {\n\t\tconst started = (event: TabMcpEventMap[\"toolcall\"]) => {\n\t\t\tconst record: ToolCallRecord = { ...event.detail, startedAt: Date.now() };\n\t\t\tsetCalls((list) => [record, ...list].slice(0, limit));\n\t\t};\n\t\tconst ended = (event: TabMcpEventMap[\"toolcallend\"]) => {\n\t\t\tconst end = event.detail;\n\t\t\tsetCalls((list) =>\n\t\t\t\tlist.some((r) => r.id === end.id)\n\t\t\t\t\t? list.map((r) => (r.id === end.id ? { ...r, end } : r))\n\t\t\t\t\t: list,\n\t\t\t);\n\t\t};\n\t\tmcp.addEventListener(\"toolcall\", started);\n\t\tmcp.addEventListener(\"toolcallend\", ended);\n\t\tsetCalls((list) => (list.length > limit ? list.slice(0, limit) : list));\n\t\treturn () => {\n\t\t\tmcp.removeEventListener(\"toolcall\", started);\n\t\t\tmcp.removeEventListener(\"toolcallend\", ended);\n\t\t};\n\t}, [mcp, limit]);\n\treturn calls;\n}\n\n/** Props of `ConnectButton`: a button's, with `children` that may depend on the state. */\nexport interface ConnectButtonProps\n\textends Omit<ButtonHTMLAttributes<HTMLButtonElement>, \"children\" | \"type\"> {\n\t/** The label; default: the state (\"Connect your AI app\", \"AI app connected\"). */\n\tchildren?: ReactNode | ((state: TabMcpSnapshot) => ReactNode);\n}\n\n/** Failed reconnect attempts after which the default label says the connection is lost. */\nconst LOST_AFTER_ATTEMPTS = 3;\n\n/**\n * `ConnectButton`'s texts in English: the same as these keys of `STRINGS`\n * (a test keeps them equal), here so that a page loads not the whole table\n * of `./strings` with the button, only these few texts (review of PR 181).\n * @internal\n */\nexport const BUTTON_TEXTS = {\n\tlang: \"en\",\n\tconnectTitle: \"Connect your AI app\",\n\tconnecting: \"Connecting…\",\n\treconnecting: \"Connection lost. Reconnecting…\",\n\tbuttonReconnecting: \"Reconnecting…\",\n\tbuttonConnected: { one: \"AI app connected\", other: \"{n} AI apps connected\" },\n};\ntype ButtonTexts = Pick<DialogStrings, keyof typeof BUTTON_TEXTS>;\n\n/** The button's texts: the `TabMcp`'s `ui.strings` where they are of the right kind. */\nfunction buttonTexts(strings: Partial<DialogStrings> | undefined): ButtonTexts {\n\tconst s: Record<string, unknown> = { ...BUTTON_TEXTS };\n\tfor (const [key, english] of Object.entries(BUTTON_TEXTS)) {\n\t\tconst value: unknown = strings?.[key as keyof ButtonTexts];\n\t\tif (\n\t\t\ttypeof english === \"string\" ? typeof value === \"string\" : isPlural(value)\n\t\t)\n\t\t\ts[key] = value;\n\t}\n\treturn s as ButtonTexts;\n}\n\n/** The default label of `ConnectButton`, in the texts of the `TabMcp`'s `ui.strings`. */\nfunction connectLabel(state: TabMcpSnapshot, s: ButtonTexts): string {\n\tif (state.state === \"connected\" && state.grants.length > 0)\n\t\treturn plural(s.buttonConnected, state.grants.length, s);\n\tif (state.state === \"reconnecting\")\n\t\treturn (state.reconnecting?.attempt ?? 0) > LOST_AFTER_ATTEMPTS\n\t\t\t? s.reconnecting\n\t\t\t: s.buttonReconnecting;\n\tif (state.state === \"connecting\") return s.connecting;\n\treturn s.connectTitle;\n}\n\n/**\n * A button that opens the library's \"Connect your AI app\" dialog\n * (`showConnectDialog()`), labelled with the state. `data-state` is\n * `disconnected`, `connecting`, `connected` or `reconnecting`, for styling.\n * An `onClick` that calls `event.preventDefault()` keeps the dialog closed.\n * Renders nothing for a `TabMcp` without a relay: there is nothing to\n * connect.\n */\nexport function ConnectButton({\n\tchildren,\n\tonClick,\n\t...rest\n}: ConnectButtonProps): ReactElement | null {\n\tconst mcp = useTabMcp();\n\tconst state = useTabMcpState();\n\tif (!mcp.hasRelay) return null;\n\tconst label =\n\t\tchildren === undefined\n\t\t\t? connectLabel(state, buttonTexts(mcp._ui?.strings))\n\t\t\t: typeof children === \"function\"\n\t\t\t\t? children(state)\n\t\t\t\t: children;\n\treturn createElement(\n\t\t\"button\",\n\t\t{\n\t\t\ttype: \"button\",\n\t\t\t\"data-state\": state.state,\n\t\t\t...rest,\n\t\t\tonClick: (event: MouseEvent<HTMLButtonElement>) => {\n\t\t\t\tonClick?.(event);\n\t\t\t\tif (event.defaultPrevented) return;\n\t\t\t\tmcp.showConnectDialog().catch((error: unknown) => {\n\t\t\t\t\tconsole.error(error);\n\t\t\t\t});\n\t\t\t},\n\t\t},\n\t\tlabel,\n\t);\n}\n",
|
|
6
|
+
"/**\n * Filling in the texts of `strings.ts`: placeholders, plural forms and lists,\n * by the rules of the texts' language (`Intl`), not by grammar made by hand.\n * No DOM and no texts, so that `./react` can use it without loading the\n * table, as well as the dialogs.\n */\n\nimport type { DialogStrings, PluralText } from \"./strings.ts\";\n\n/** What the helpers need of the texts: their language. */\ntype InLanguage = { lang: string };\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 * `Intl` objects for the texts' language; a `lang` the browser cannot\n * read (a malformed tag throws) uses English rules instead.\n */\nfunction inLanguage<T>(make: (lang: string) => T, lang: string): T {\n\ttry {\n\t\treturn make(lang);\n\t} catch {\n\t\treturn make(\"en\");\n\t}\n}\n\n/** Whether `value` is a plural text: an object of strings with `other`. */\nexport const isPlural = (value: unknown): value is PluralText =>\n\ttypeof value === \"object\" &&\n\tvalue !== null &&\n\ttypeof (value as { other?: unknown }).other === \"string\" &&\n\tObject.values(value).every((form) => typeof form === \"string\");\n\nconst warned = new WeakSet<object>();\n\n/**\n * A table with texts but no `lang` is taken as English: its plural forms\n * follow English rules and the dialogs say `lang=\"en\"`. Said once per table\n * in the console, as it is almost always a translation that forgot its tag.\n */\nexport function warnWithoutLang(strings: object | undefined): void {\n\tif (strings === undefined || warned.has(strings)) return;\n\tconst texts = Object.entries(strings).filter(\n\t\t([key, value]) => key !== \"lang\" && key !== \"dir\" && value !== undefined,\n\t);\n\tif (texts.length === 0 || typeof (strings as InLanguage).lang === \"string\")\n\t\treturn;\n\twarned.add(strings);\n\tconsole.warn(\n\t\t\"tab-mcp: ui.strings has texts but no lang, so they count as English (plural forms, lists, the dialogs' lang). Add lang, the language tag of the texts.\",\n\t);\n}\n\n/**\n * The form of `text` for the count `n`, by the plural rules of the texts'\n * language, with `{n}` filled in; `other` where the table lacks the form.\n */\nexport function pluralForm(text: PluralText, n: number, s: InLanguage): string {\n\tconst category = inLanguage((l) => new Intl.PluralRules(l), s.lang).select(n);\n\treturn format(text[category] ?? text.other, { n: String(n) });\n}\n\n/** `pluralForm` with the other placeholders filled in too, as text. */\nexport function plural(\n\ttext: PluralText,\n\tn: number,\n\ts: InLanguage,\n\tvalues: Record<string, string> = {},\n): string {\n\treturn format(pluralForm(text, n, s), values);\n}\n\n/** How the texts' language joins a list (\"A, B and C\"). */\nexport function listFormat(s: InLanguage): Intl.ListFormat {\n\treturn inLanguage(\n\t\t(l) => new Intl.ListFormat(l, { type: \"conjunction\" }),\n\t\ts.lang,\n\t);\n}\n\n/** \"A and B\", \"A, B and C\", as the texts' language joins a list. */\nexport function listOf(items: string[], s: InLanguage): string {\n\treturn listFormat(s).format(items);\n}\n\n/**\n * The direction of the texts: their own `dir`, else the one the browser\n * knows for `lang` (`Intl.Locale`'s text info, not in every browser), else\n * left to right.\n */\nexport function directionOf(s: DialogStrings): \"ltr\" | \"rtl\" {\n\tif (s.dir !== undefined) return s.dir;\n\ttry {\n\t\tconst locale = new Intl.Locale(s.lang) as Intl.Locale & {\n\t\t\tgetTextInfo?: () => { direction?: string };\n\t\t\ttextInfo?: { direction?: string };\n\t\t};\n\t\tconst info = locale.getTextInfo?.() ?? locale.textInfo;\n\t\treturn info?.direction === \"rtl\" ? \"rtl\" : \"ltr\";\n\t} catch {\n\t\treturn \"ltr\";\n\t}\n}\n"
|
|
6
7
|
],
|
|
7
|
-
"mappings": ";;;AASA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAeA;AAAA;AAAA;AAKA;AAAA;AAAA;
|
|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a,YACV,aAAa,OAAO,YAAY,IAAI,KAAK,OAAO,CAAC,IACjD,OAAO,aAAa,aACnB,SAAS,KAAK,IACd;AAAA,EACL,OAAO,cACN,UACA;AAAA,IACC,MAAM;AAAA,IACN,cAAc,MAAM;AAAA,OACjB;AAAA,IACH,SAAS,CAAC,UAAyC;AAAA,MAClD,UAAU,KAAK;AAAA,MACf,IAAI,MAAM;AAAA,QAAkB;AAAA,MAC5B,IAAI,kBAAkB,EAAE,MAAM,CAAC,UAAmB;AAAA,QACjD,QAAQ,MAAM,KAAK;AAAA,OACnB;AAAA;AAAA,EAEH,GACA,KACD;AAAA;",
|
|
9
|
+
"debugId": "6A93EB197910F78264756E2164756E21",
|
|
9
10
|
"names": []
|
|
10
11
|
}
|
package/secret.d.ts
CHANGED
|
@@ -22,6 +22,6 @@ export declare class SecretStore {
|
|
|
22
22
|
* yet); storage first, so a rotation in another tab is seen.
|
|
23
23
|
*/
|
|
24
24
|
current(): string | undefined;
|
|
25
|
-
/** Make and keep a new secret (
|
|
25
|
+
/** Make and keep a new secret (`disconnectAll()`). */
|
|
26
26
|
rotate(): string;
|
|
27
27
|
}
|
package/setup.d.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* and extensions that show them in their own UI. Plain data, no DOM. An entry
|
|
5
5
|
* of its own, out of the core (DEC-118).
|
|
6
6
|
*/
|
|
7
|
+
import { type DialogStrings } from "./strings.js";
|
|
7
8
|
import type { SetupNotePart, SetupStep } from "./types.js";
|
|
8
9
|
export type { SetupNotePart, SetupStep };
|
|
9
10
|
/** The guide for every AI app, step by step and what to do when it fails (its source is on ractive.ch). */
|
|
@@ -30,5 +31,7 @@ export interface SetupInstruction extends SetupStep {
|
|
|
30
31
|
* characters as `-`, at most 40 characters, `tools` if nothing is left).
|
|
31
32
|
* End the list with a link to {@link SETUP_GUIDE}: the steps here are the
|
|
32
33
|
* minimum (CU 20), the guide has the rest and follows the apps' menus.
|
|
34
|
+
* `strings` translates the texts (the `setup…` keys of `./strings`, the
|
|
35
|
+
* same table as the dialogs' `ui.strings`); missing keys stay English.
|
|
33
36
|
*/
|
|
34
|
-
export declare function setupInstructions(serverName: string, address: string): SetupInstruction[];
|
|
37
|
+
export declare function setupInstructions(serverName: string, address: string, strings?: Partial<DialogStrings>): SetupInstruction[];
|
package/setup.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
SETUP_GUIDE2,
|
|
3
3
|
setupInstructions2
|
|
4
|
-
} from "./index-
|
|
4
|
+
} from "./index-1tssct09.js";
|
|
5
|
+
import"./index-00p8b969.js";
|
|
5
6
|
export {
|
|
6
7
|
SETUP_GUIDE2 as SETUP_GUIDE,
|
|
7
8
|
setupInstructions2 as setupInstructions
|
|
8
9
|
};
|
|
9
10
|
|
|
10
|
-
//# debugId=
|
|
11
|
+
//# debugId=2157CE28BE0F36C364756E2164756E21
|
|
11
12
|
//# sourceMappingURL=setup.js.map
|
package/setup.js.map
CHANGED
package/strings.d.ts
CHANGED
|
@@ -1,22 +1,44 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
2
|
+
* `@ractive-ch/tab-mcp-client/strings`: every text the library can show to
|
|
3
|
+
* a person, in English, in one table (`STRINGS`), `pickStrings` to choose
|
|
4
|
+
* a page's own translation by the user's languages, and `withEnglish` for
|
|
5
|
+
* a page that shows these texts in its own UI. The package ships English only;
|
|
6
|
+
* a page passes its own table once, as `ui.strings` of `TabMcp` (or of
|
|
7
|
+
* `<TabMcpProvider>`), and every missing key stays English.
|
|
8
|
+
*
|
|
9
|
+
* `{name}` is a placeholder the library fills in: names of tools and AI
|
|
10
|
+
* apps (isolated, so that they cannot reorder the sentence), the code,
|
|
11
|
+
* counts. A text that depends on a count is a {@link PluralText}, one form
|
|
12
|
+
* per plural category of the language (`Intl.PluralRules`). Lists of names
|
|
13
|
+
* are joined by `Intl.ListFormat`. Both follow `lang`, the language of the
|
|
14
|
+
* texts, which the dialogs also set on themselves with its direction.
|
|
15
|
+
*
|
|
16
|
+
* Texts an AI app reads (tool results, refusals) are not here: they stay
|
|
17
|
+
* English on purpose, as the AI app quotes them back in its own words.
|
|
9
18
|
*/
|
|
10
|
-
|
|
19
|
+
/**
|
|
20
|
+
* A text that depends on a count `{n}` (or on the number of names in a
|
|
21
|
+
* list): one form per plural category the language has, as
|
|
22
|
+
* `Intl.PluralRules` names them. `other` is required; English needs `one`
|
|
23
|
+
* and `other`, Polish `one`, `few`, `many` and `other`, Japanese only
|
|
24
|
+
* `other`. A category the table lacks uses `other`.
|
|
25
|
+
*/
|
|
26
|
+
export type PluralText = {
|
|
27
|
+
other: string;
|
|
28
|
+
} & {
|
|
29
|
+
[category in Intl.LDMLPluralRule]?: string;
|
|
30
|
+
};
|
|
31
|
+
declare const english: {
|
|
32
|
+
/**
|
|
33
|
+
* The language of the texts (a BCP 47 tag): set on every dialog, and the
|
|
34
|
+
* language `Intl.PluralRules` and `Intl.ListFormat` follow.
|
|
35
|
+
*/
|
|
11
36
|
lang: string;
|
|
12
|
-
dir: string;
|
|
13
|
-
/** Between the last two items of a list ("A, B and C"). */
|
|
14
|
-
and: string;
|
|
15
37
|
confirmTitle: string;
|
|
16
38
|
confirmWho: string;
|
|
17
39
|
deny: string;
|
|
18
40
|
allow: string;
|
|
19
|
-
/** `alwaysAllow: true`: kept until the app is disconnected. */
|
|
41
|
+
/** `alwaysAllow: true`: kept until the AI app is disconnected. */
|
|
20
42
|
alwaysAllow: string;
|
|
21
43
|
alwaysAllowNote: string;
|
|
22
44
|
/** `alwaysAllow: "until-reload"`, or kept in memory only (`storage: null`). */
|
|
@@ -24,8 +46,10 @@ export declare const STRINGS: {
|
|
|
24
46
|
allowUntilReloadNote: string;
|
|
25
47
|
argumentsHidden: string;
|
|
26
48
|
scrollToAllow: string;
|
|
27
|
-
|
|
28
|
-
|
|
49
|
+
waiting: {
|
|
50
|
+
one: string;
|
|
51
|
+
other: string;
|
|
52
|
+
};
|
|
29
53
|
/** Before the page's title while a confirmation waits in a hidden tab. */
|
|
30
54
|
titlePrefix: string;
|
|
31
55
|
clientWebMcp: string;
|
|
@@ -43,6 +67,20 @@ export declare const STRINGS: {
|
|
|
43
67
|
copyCode: string;
|
|
44
68
|
copyCommand: string;
|
|
45
69
|
connectTitle: string;
|
|
70
|
+
/**
|
|
71
|
+
* At the top, while AI apps reach the page through the tab-mcp browser
|
|
72
|
+
* extension (`TabMcp.extension`, DEC-207); `…InFront` when the extension
|
|
73
|
+
* serves only the tab in front, as it does. `{apps}`: their names, as a
|
|
74
|
+
* list.
|
|
75
|
+
*/
|
|
76
|
+
viaExtension: {
|
|
77
|
+
one: string;
|
|
78
|
+
other: string;
|
|
79
|
+
};
|
|
80
|
+
viaExtensionInFront: {
|
|
81
|
+
one: string;
|
|
82
|
+
other: string;
|
|
83
|
+
};
|
|
46
84
|
connecting: string;
|
|
47
85
|
connectAgain: string;
|
|
48
86
|
reconnecting: string;
|
|
@@ -68,18 +106,32 @@ export declare const STRINGS: {
|
|
|
68
106
|
codeNote: string;
|
|
69
107
|
codeUsed: string;
|
|
70
108
|
signIn: string;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
109
|
+
/** Under `signIn`: until when the relay pairs a sign-in (`{time}`: hours and minutes). */
|
|
110
|
+
signInUntil: string;
|
|
111
|
+
/** The status once that time is up; Continue starts it again. */
|
|
112
|
+
signInTimeUp: string;
|
|
113
|
+
continuePairing: string;
|
|
114
|
+
disconnectAll: string;
|
|
115
|
+
disconnectAllNote: string;
|
|
116
|
+
/** `{apps}`: the AI apps it disconnects, as a list. */
|
|
117
|
+
disconnectAllQuestion: {
|
|
118
|
+
one: string;
|
|
119
|
+
other: string;
|
|
120
|
+
};
|
|
121
|
+
/** Repeats the button that asked. */
|
|
122
|
+
disconnectAllConfirm: string;
|
|
76
123
|
cancel: string;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
124
|
+
disconnectingAll: string;
|
|
125
|
+
disconnectAllDone: string;
|
|
126
|
+
/** `{apps}`: the connected AI apps, as a list. */
|
|
127
|
+
connected: {
|
|
128
|
+
one: string;
|
|
129
|
+
other: string;
|
|
130
|
+
};
|
|
131
|
+
connectedNext: {
|
|
132
|
+
one: string;
|
|
133
|
+
other: string;
|
|
134
|
+
};
|
|
83
135
|
connectAnother: string;
|
|
84
136
|
connectedHeading: string;
|
|
85
137
|
appConnected: string;
|
|
@@ -90,7 +142,6 @@ export declare const STRINGS: {
|
|
|
90
142
|
storeUnavailable: string;
|
|
91
143
|
/** Disconnect failed for another reason. */
|
|
92
144
|
disconnectFailed: string;
|
|
93
|
-
runsWithoutAsking: string;
|
|
94
145
|
/** After a tool's name: it may delete or overwrite (`destructiveHint`). */
|
|
95
146
|
destructive: string;
|
|
96
147
|
/** A tool's switch, as read out: starts with what it shows (WCAG 2.5.3). */
|
|
@@ -98,10 +149,31 @@ export declare const STRINGS: {
|
|
|
98
149
|
allowDestructiveToolApp: string;
|
|
99
150
|
allowEverything: string;
|
|
100
151
|
allowEverythingApp: string;
|
|
152
|
+
/** Kept until the AI app is disconnected (`alwaysAllow: true`). */
|
|
101
153
|
allowEverythingNote: string;
|
|
154
|
+
/** Kept in memory only (`alwaysAllow: "until-reload"`, `storage: null`). */
|
|
155
|
+
allowEverythingUntilReloadNote: string;
|
|
102
156
|
closeNote: string;
|
|
103
157
|
close: string;
|
|
104
158
|
done: string;
|
|
159
|
+
setupClaudeApp: string;
|
|
160
|
+
setupClaude: string;
|
|
161
|
+
setupChatGpt: string;
|
|
162
|
+
/** Before a command to run in a terminal. */
|
|
163
|
+
setupRun: string;
|
|
164
|
+
/** Without a command (an address a shell would not read as plain). */
|
|
165
|
+
setupClaudeCode: string;
|
|
166
|
+
/** `{command}`: `/mcp`, shown as code. */
|
|
167
|
+
setupClaudeCodeNote: string;
|
|
168
|
+
/** Without a command; `{feature}`: the Codex feature to turn on. */
|
|
169
|
+
setupCodex: string;
|
|
170
|
+
setupCodexNote: string;
|
|
171
|
+
/** While reconnecting, before it says the connection is lost. */
|
|
172
|
+
buttonReconnecting: string;
|
|
173
|
+
buttonConnected: {
|
|
174
|
+
one: string;
|
|
175
|
+
other: string;
|
|
176
|
+
};
|
|
105
177
|
replacedSiblingTitle: string;
|
|
106
178
|
replacedSibling: string;
|
|
107
179
|
replacedTitle: string;
|
|
@@ -110,7 +182,56 @@ export declare const STRINGS: {
|
|
|
110
182
|
/** `{reason}`: why disconnecting all apps failed, one of the texts above. */
|
|
111
183
|
tryAgainOrClose: string;
|
|
112
184
|
};
|
|
113
|
-
/**
|
|
185
|
+
/**
|
|
186
|
+
* The texts of the library, key by key: each a string, or a
|
|
187
|
+
* {@link PluralText} where it depends on a count. `dir` is the direction of
|
|
188
|
+
* the texts; without it, the dialogs take it from `lang` where the browser
|
|
189
|
+
* knows it (`Intl.Locale`), else left to right: set `dir: "rtl"` for a
|
|
190
|
+
* right-to-left language to be sure.
|
|
191
|
+
*
|
|
192
|
+
* Type a translation as `DialogStrings` and the type check names every key
|
|
193
|
+
* it lacks or misspells, also after an upgrade added one; as
|
|
194
|
+
* `Partial<DialogStrings>` it may leave keys out (they stay English).
|
|
195
|
+
*/
|
|
114
196
|
export type DialogStrings = {
|
|
115
|
-
[K in keyof typeof
|
|
197
|
+
[K in keyof typeof english]: (typeof english)[K] extends string ? string : PluralText;
|
|
198
|
+
} & {
|
|
199
|
+
dir?: "ltr" | "rtl";
|
|
116
200
|
};
|
|
201
|
+
/** The English texts, the defaults for every key. */
|
|
202
|
+
export declare const STRINGS: DialogStrings;
|
|
203
|
+
/**
|
|
204
|
+
* Every text: `strings` where it has one of the right kind, English for
|
|
205
|
+
* the rest. A missing translation (`undefined`), an unknown key or a plain
|
|
206
|
+
* string where the English text has plural forms keeps the English text:
|
|
207
|
+
* a table from an older version still works, only less translated. A table
|
|
208
|
+
* with texts but no `lang` counts as English, and the console says so once.
|
|
209
|
+
*/
|
|
210
|
+
export declare function withEnglish(strings: Partial<DialogStrings> | undefined): DialogStrings;
|
|
211
|
+
/** A table of texts, or a module whose default export is one. */
|
|
212
|
+
type Loaded = Partial<DialogStrings> | {
|
|
213
|
+
default: Partial<DialogStrings>;
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* The page's own table for the user's language, loaded only once chosen:
|
|
217
|
+
* `tables` maps language tags (`"de"`, `"pt-BR"`, `"zh-Hant"`) to functions
|
|
218
|
+
* that load one, such as `() => import("./tab-mcp.de.ts")`. The user's
|
|
219
|
+
* languages (`navigator.languages`, most preferred first) are tried in
|
|
220
|
+
* order; each takes the same tag, else a table of the same language: the
|
|
221
|
+
* same script first, then the same region, then the language alone, then
|
|
222
|
+
* the first in alphabetical order. English is built in: a user who prefers
|
|
223
|
+
* English before any of the tables, or none of them, gets `undefined`,
|
|
224
|
+
* which keeps the English texts. The table comes with `lang` set to the
|
|
225
|
+
* matched tag unless it has its own.
|
|
226
|
+
*
|
|
227
|
+
* It never rejects: a table that fails to load (a chunk gone after a
|
|
228
|
+
* deploy, offline) or is not an object is said in the console and gives
|
|
229
|
+
* `undefined`, so the page still starts, in English.
|
|
230
|
+
*
|
|
231
|
+
* ```ts
|
|
232
|
+
* const strings = await pickStrings({ de: () => import("./tab-mcp.de.ts") });
|
|
233
|
+
* const mcp = new TabMcp({ relay, server, ui: { strings } });
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
export declare function pickStrings(tables: Record<string, () => Promise<Loaded>>, languages?: readonly string[]): Promise<Partial<DialogStrings> | undefined>;
|
|
237
|
+
export {};
|
package/strings.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {
|
|
2
|
+
STRINGS2,
|
|
3
|
+
withEnglish2,
|
|
4
|
+
pickStrings2
|
|
5
|
+
} from "./index-00p8b969.js";
|
|
6
|
+
export {
|
|
7
|
+
STRINGS2 as STRINGS,
|
|
8
|
+
pickStrings2 as pickStrings,
|
|
9
|
+
withEnglish2 as withEnglish
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
//# debugId=E200B705FC1EE38E64756E2164756E21
|
|
13
|
+
//# sourceMappingURL=strings.js.map
|