@ractive-ch/tab-mcp-client 0.18.1 → 0.19.0-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.
Files changed (86) hide show
  1. package/CHANGELOG.md +160 -0
  2. package/README.md +348 -68
  3. package/allowed.d.ts +2 -2
  4. package/allowed.js.map +1 -1
  5. package/always-allowed.d.ts +15 -15
  6. package/cdn/allowed.min.js +2 -2
  7. package/cdn/allowed.min.js.map +2 -2
  8. package/cdn/confirm-ba3dkf9p.min.js +4 -0
  9. package/cdn/confirm-ba3dkf9p.min.js.map +10 -0
  10. package/cdn/connect-my0pbqd7.min.js +6 -0
  11. package/cdn/connect-my0pbqd7.min.js.map +12 -0
  12. package/cdn/connection-yf2x9zz2.min.js +17 -0
  13. package/cdn/{connection-0xqwz17v.min.js.map → connection-yf2x9zz2.min.js.map} +5 -5
  14. package/cdn/strings.min.js +4 -0
  15. package/cdn/strings.min.js.map +9 -0
  16. package/cdn/{tab-mcp-client-2vx9mcww.min.js → tab-mcp-client-8r6h3wza.min.js} +9 -8
  17. package/cdn/tab-mcp-client-8r6h3wza.min.js.map +11 -0
  18. package/cdn/tab-mcp-client-nnxcwrxp.min.js +5 -0
  19. package/cdn/tab-mcp-client-nnxcwrxp.min.js.map +11 -0
  20. package/cdn/{tab-mcp-client-5j3brdf3.min.js → tab-mcp-client-q758jr4g.min.js} +4 -4
  21. package/cdn/tab-mcp-client-q758jr4g.min.js.map +10 -0
  22. package/cdn/tab-mcp-client.min.js +3 -3
  23. package/cdn/tab-mcp-client.min.js.map +4 -4
  24. package/cdn/webmcp-84k58gsw.min.js +4 -0
  25. package/cdn/webmcp-84k58gsw.min.js.map +11 -0
  26. package/{confirm-bqfdrx3f.js → confirm-hymzwx3b.js} +7 -5
  27. package/confirm-hymzwx3b.js.map +10 -0
  28. package/confirm.d.ts +2 -2
  29. package/{connect-ztsgybpq.js → connect-fhtjdmf3.js} +75 -37
  30. package/connect-fhtjdmf3.js.map +10 -0
  31. package/connect.d.ts +7 -6
  32. package/{connection-gang7f5x.js → connection-73fbg99k.js} +70 -8
  33. package/connection-73fbg99k.js.map +16 -0
  34. package/connection.d.ts +23 -2
  35. package/dialog.d.ts +19 -9
  36. package/extension.d.ts +347 -0
  37. package/extension.js +394 -0
  38. package/extension.js.map +10 -0
  39. package/handshake.d.ts +82 -0
  40. package/index-00p8b969.js +221 -0
  41. package/index-00p8b969.js.map +11 -0
  42. package/index-1tssct09.js +58 -0
  43. package/index-1tssct09.js.map +11 -0
  44. package/{index-c1c266tm.js → index-7qqhn3zv.js} +28 -110
  45. package/index-7qqhn3zv.js.map +10 -0
  46. package/index-s31xrfa2.js.map +1 -1
  47. package/index.d.ts +1 -1
  48. package/index.js +59 -64
  49. package/index.js.map +4 -4
  50. package/internal.d.ts +2 -0
  51. package/mcp.d.ts +5 -3
  52. package/package.json +11 -2
  53. package/protocol.d.ts +11 -1
  54. package/react.d.ts +11 -4
  55. package/react.js +57 -10
  56. package/react.js.map +5 -4
  57. package/secret.d.ts +1 -1
  58. package/setup.d.ts +4 -1
  59. package/setup.js +3 -2
  60. package/setup.js.map +1 -1
  61. package/strings.d.ts +149 -28
  62. package/strings.js +13 -0
  63. package/strings.js.map +9 -0
  64. package/tab-mcp.d.ts +30 -7
  65. package/text.d.ts +39 -0
  66. package/types.d.ts +76 -17
  67. package/webmcp-d2ec3v3g.js +221 -0
  68. package/webmcp-d2ec3v3g.js.map +11 -0
  69. package/webmcp.d.ts +17 -4
  70. package/cdn/confirm-d722a9eb.min.js +0 -4
  71. package/cdn/confirm-d722a9eb.min.js.map +0 -10
  72. package/cdn/connect-x6ws6wqt.min.js +0 -6
  73. package/cdn/connect-x6ws6wqt.min.js.map +0 -12
  74. package/cdn/connection-0xqwz17v.min.js +0 -17
  75. package/cdn/tab-mcp-client-2vx9mcww.min.js.map +0 -12
  76. package/cdn/tab-mcp-client-5j3brdf3.min.js.map +0 -10
  77. package/cdn/webmcp-khy4f5rj.min.js +0 -4
  78. package/cdn/webmcp-khy4f5rj.min.js.map +0 -10
  79. package/confirm-bqfdrx3f.js.map +0 -10
  80. package/connect-ztsgybpq.js.map +0 -10
  81. package/connection-gang7f5x.js.map +0 -16
  82. package/index-5jyqehpk.js +0 -63
  83. package/index-5jyqehpk.js.map +0 -11
  84. package/index-c1c266tm.js.map +0 -11
  85. package/webmcp-zq1enyz0.js +0 -57
  86. package/webmcp-zq1enyz0.js.map +0 -10
package/extension.d.ts ADDED
@@ -0,0 +1,347 @@
1
+ /**
2
+ * `@ractive-ch/tab-mcp-client/extension`: for a browser extension that is a
3
+ * tab-mcp page itself (the tab-mcp browser extension, Sitegloss). It fills
4
+ * in the extension's own pairing code on the relay's pairing page
5
+ * (`/authorize`) when an AI app opens it in the same browser, after asking
6
+ * the user (DEC-199, DEC-203). What is the same for every such extension
7
+ * lives here, reviewed once (DEC-206); how the user is asked, where the
8
+ * setting is kept, the permission prompt, the content script's registration
9
+ * and holding pairing on are each extension's own, given as callbacks.
10
+ *
11
+ * The relay's page has no scripts (DEC-057), so it cannot call the
12
+ * extension: a content script reads the form's `data-tab-mcp-*` hooks and
13
+ * tells the worker who asks (the AI app's name and verified host). The
14
+ * worker asks its user and, on Allow, hands the code to that one page over
15
+ * its port. The content script never sees a code before that, and the
16
+ * worker never takes a page's word for where it is: any site can copy the
17
+ * markup, so the origin the browser reports for the port is the check.
18
+ *
19
+ * An entry of its own that a web page never loads, out of the core. It
20
+ * imports nothing of the rest of the library at run time and touches no
21
+ * `window` or `document` until called, so a service worker and a content
22
+ * script can both import it; a content script's bundle keeps only the page
23
+ * side (`runPairingPage` and what it uses). No `chrome` types: the browser's
24
+ * ports and senders are taken as the small structural types below.
25
+ *
26
+ * ```ts
27
+ * // The content script, registered for the relay's /authorize page:
28
+ * import { PAIRING_PORT, runPairingPage } from "@ractive-ch/tab-mcp-client/extension";
29
+ * runPairingPage({ app: "my-app", connect: () => chrome.runtime.connect({ name: PAIRING_PORT }) });
30
+ *
31
+ * // The worker:
32
+ * import { CodeFiller, PAIRING_PORT, pagePort } from "@ractive-ch/tab-mcp-client/extension";
33
+ * const filler = new CodeFiller({ extensionId: chrome.runtime.id, app: "my-app", ... });
34
+ * chrome.runtime.onConnect.addListener((port) => {
35
+ * if (port.name === PAIRING_PORT) void filler.page(pagePort(port));
36
+ * });
37
+ * ```
38
+ */
39
+ import type { TabMcp } from "./tab-mcp.js";
40
+ import type { PairingCode } from "./types.js";
41
+ /** The name of the port a pairing page's content script opens. */
42
+ export declare const PAIRING_PORT = "pairing-page";
43
+ /** The relay's pairing page. */
44
+ export declare const PAIRING_PATH = "/authorize";
45
+ /** What the content script says once, on its page's port. */
46
+ export interface PairingHello {
47
+ type: "pairing-page";
48
+ /** This page load's own: the code comes back only with it. */
49
+ nonce: string;
50
+ /** `data-app`: the app the AI app asks to connect to. */
51
+ app: string;
52
+ /** `data-client-name`: the AI app's name for itself. */
53
+ clientName: string;
54
+ /** `data-client-host`: the host the relay verified for the AI app. */
55
+ clientHost: string;
56
+ /**
57
+ * The form's hidden `request_id`: the relay's id of this authorization
58
+ * request, the same on its page again after a wrong code. The one fill
59
+ * without a question is for this request only.
60
+ */
61
+ requestId: string;
62
+ }
63
+ /** What the worker sends after the user's Allow: the code to fill in and submit. */
64
+ export interface FillMessage {
65
+ type: "fill";
66
+ nonce: string;
67
+ code: string;
68
+ }
69
+ /** The longest name and host a hello may carry (the relay's own limits are lower). */
70
+ export declare const MAX_CLIENT_NAME = 200;
71
+ export declare const MAX_CLIENT_HOST = 253;
72
+ /**
73
+ * The content script's hello, checked; `undefined` for anything else. The
74
+ * name and host are what the relay's page says, kept as they are: the
75
+ * extension's question must show invisible characters escaped (`./visible`).
76
+ */
77
+ export declare function parseHello(message: unknown): PairingHello | undefined;
78
+ /** The worker's fill message, checked against this page's nonce. */
79
+ export declare function parseFill(message: unknown, nonce: string): FillMessage | undefined;
80
+ /**
81
+ * The origin of the relay's web pages for the relay address `relay`: its
82
+ * `ws:`/`wss:` forms are the same server's `http:`/`https:`. `undefined` for
83
+ * anything that is not one.
84
+ */
85
+ export declare function relayPageOrigin(relay: string): string | undefined;
86
+ /**
87
+ * The host permission for the relay's site (`<scheme>://<host>/*`), the one
88
+ * origin to ask for (`permissions.request({ origins: [it] })`) before the
89
+ * extension can see the pairing page. `undefined` for an address that has
90
+ * no pattern: the user types the code.
91
+ */
92
+ export declare function relaySitePattern(relay: string): string | undefined;
93
+ /** The match pattern of the relay's pairing page, for a content script's `matches`. */
94
+ export declare function pairingPagePattern(relay: string): string | undefined;
95
+ /** The page's pairing form, as far as filling it in needs it. */
96
+ export interface PairingForm {
97
+ form: HTMLFormElement;
98
+ field: HTMLInputElement;
99
+ button: HTMLButtonElement;
100
+ app: string;
101
+ clientName: string;
102
+ clientHost: string;
103
+ /** The relay's id of this request (the form's hidden `request_id`). */
104
+ requestId: string;
105
+ }
106
+ /**
107
+ * The pairing form of app `app` on `doc`, if this is a top-level page at
108
+ * `/authorize` with exactly one such form that has a code field and a
109
+ * Connect button (DEC-199's hooks, version 1). The worker checks the origin
110
+ * itself; this only spares it the pages that are not one. A page that pairs
111
+ * by sign-in only has no code field.
112
+ */
113
+ export declare function findPairingForm(doc: Document, where: {
114
+ topLevel: boolean;
115
+ pathname: string;
116
+ }, app: string): PairingForm | undefined;
117
+ /**
118
+ * Write `code` into the form's field and submit it with its Connect button:
119
+ * the page's own post, as if the user had typed it and clicked.
120
+ */
121
+ export declare function fillIn(found: PairingForm, code: string): void;
122
+ /** The hello for `found`, with this page load's nonce. */
123
+ export declare function helloFor(found: PairingForm, nonce: string): PairingHello;
124
+ /** The content script's end of its port (`chrome.runtime.Port` is one). */
125
+ export interface ContentPort {
126
+ postMessage(message: unknown): void;
127
+ readonly onMessage: {
128
+ addListener(listener: (message: unknown) => void): void;
129
+ };
130
+ }
131
+ /**
132
+ * The content script of the relay's pairing page: if the page has app
133
+ * `app`'s pairing form, open a port to the worker (`connect`, with the name
134
+ * PAIRING_PORT), say who asks, and wait. Only the worker's fill with this
135
+ * page load's nonce is taken, once: its code is written into the field and
136
+ * the form submitted. Once per document, also if the script is put in again
137
+ * (after the user gave access with the page already open). Whether the
138
+ * page has the form. `page` is the content script's own page unless a test
139
+ * gives one.
140
+ */
141
+ export declare function runPairingPage(options: {
142
+ app: string;
143
+ connect: () => ContentPort;
144
+ page?: {
145
+ document: Document;
146
+ topLevel: boolean;
147
+ pathname: string;
148
+ };
149
+ }): boolean;
150
+ /** The sender of a port, as far as the check needs it (`chrome.runtime.MessageSender`). */
151
+ export interface PageSender {
152
+ id?: string;
153
+ url?: string;
154
+ /** Chrome gives the document's origin; Firefox may not. */
155
+ origin?: string;
156
+ tab?: unknown;
157
+ frameId?: number;
158
+ }
159
+ /**
160
+ * A port from the pairing page of the relay at `relayOrigin`: this
161
+ * extension's content script, in a tab, in its top frame, on a page whose
162
+ * origin is exactly the relay's (port included) and whose path is
163
+ * `/authorize`. A page anywhere else can carry the same markup: it is
164
+ * ignored.
165
+ */
166
+ export declare function isPairingSender(sender: PageSender, extensionId: string, relayOrigin: string | undefined): boolean;
167
+ /** A page will not say who asks in this time: it is dropped. */
168
+ export declare const HELLO_WAIT_MS = 5000;
169
+ /**
170
+ * The same request's page in the same tab within this time after a fill is
171
+ * the relay's answer to it: the code was not taken.
172
+ */
173
+ export declare const RETRY_WINDOW_MS = 30000;
174
+ /** `takeCode`: how long to wait for the next code. */
175
+ export declare const CODE_WAIT_MS = 10000;
176
+ /** `takeCode`: a code with less time left than this is not used, the next one is. */
177
+ export declare const MIN_CODE_LEFT_MS = 5000;
178
+ /**
179
+ * A filled-in page's port is closed this long after the fill: the page has
180
+ * posted by then, and its answer, if any, is a new document with a port of
181
+ * its own.
182
+ */
183
+ export declare const FILLED_CLOSE_MS = 5000;
184
+ /** The worker's end of a page's port (`chrome.runtime.Port` is one). */
185
+ export interface WorkerPort {
186
+ readonly sender?: PageSender;
187
+ postMessage(message: unknown): void;
188
+ disconnect(): void;
189
+ readonly onMessage: {
190
+ addListener(listener: (message: unknown) => void): void;
191
+ removeListener(listener: (message: unknown) => void): void;
192
+ };
193
+ readonly onDisconnect: {
194
+ addListener(listener: () => void): void;
195
+ };
196
+ }
197
+ /**
198
+ * A pairing page's port in the worker. Its first message and its end are
199
+ * taken from the moment it connects: the worker may still be starting, and
200
+ * Chrome hands a message only to the listeners there are.
201
+ */
202
+ export interface PagePort {
203
+ readonly sender: PageSender;
204
+ /** The first message; `undefined` if the port went before one came. */
205
+ readonly hello: Promise<unknown>;
206
+ /** Aborted when the page goes (or the worker closes the port). */
207
+ readonly signal: AbortSignal;
208
+ send(message: FillMessage): void;
209
+ close(): void;
210
+ }
211
+ /**
212
+ * `port`, taken over at once (see PagePort): call it in `onConnect` itself,
213
+ * before any `await`.
214
+ */
215
+ export declare function pagePort(port: WorkerPort): PagePort;
216
+ /**
217
+ * The user's answer to "Connect <name> (<host>)?": only `allow` fills in.
218
+ * `busy`: the extension could not ask (too many questions wait); the page is
219
+ * reported (`failed("busy")`), not dropped unseen.
220
+ */
221
+ export type FillAnswer = "allow" | "deny" | "timeout" | "busy";
222
+ /** Why a page that asked got no code: the extension tells its user. */
223
+ export type FillFailure =
224
+ /** It could not be asked about (`ask` answered `busy`). */
225
+ "busy"
226
+ /** Allow, but no code came from the relay (`code` gave none). */
227
+ | "no-code";
228
+ /** What `CodeFiller` needs from the extension: its parts of the fill-in. */
229
+ export interface FillHost {
230
+ /** This extension's id (`chrome.runtime.id`): only its own content script speaks. */
231
+ extensionId: string;
232
+ /** The app id of the extension's page: only forms with this `data-app`. */
233
+ app: string;
234
+ /**
235
+ * The origin of the relay's pages (`relayPageOrigin`) whose pairing page
236
+ * may be filled in; `undefined` for none (another relay than the one the
237
+ * extension fills in for).
238
+ */
239
+ relayOrigin(): string | undefined;
240
+ /** AI access is on: connected, or on its way. Off, nothing is asked. */
241
+ aiOn(): boolean;
242
+ /** The user turned filling in on, for this relay (opt-in, off by default). */
243
+ enabled(): boolean;
244
+ /**
245
+ * Ask the user whether to connect the AI app `client` (its own name for
246
+ * itself and the host the relay verified, both to be shown with invisible
247
+ * characters escaped) in tab `tabId`. `signal` withdraws the question:
248
+ * the page went, AI access or the setting went off, the relay changed,
249
+ * or the AI app connected meanwhile; answer then with anything but
250
+ * `allow`.
251
+ */
252
+ ask(tabId: number, client: {
253
+ name: string;
254
+ host: string;
255
+ }, signal: AbortSignal): Promise<FillAnswer>;
256
+ /**
257
+ * The code to fill in now, with pairing held on for the relay to check it
258
+ * (`takeCode`); `undefined` if none came or `signal` aborted (the page
259
+ * went, or the fill was withdrawn as a question is). Even with a code,
260
+ * nothing is sent if what holds for asking no longer does.
261
+ */
262
+ code(signal: AbortSignal): Promise<string | undefined>;
263
+ /** A filled-in code paired its AI app: pairing need not be held for it. */
264
+ release(): void;
265
+ /** A page filled in nothing for `why`, or was not asked about: say so. */
266
+ failed(why: FillFailure): void;
267
+ /** The clock (`Date.now`; a test's own). */
268
+ now(): number;
269
+ }
270
+ /**
271
+ * The worker's side of filling in the code (DEC-199, DEC-203): a page's port
272
+ * is taken only from this extension's content script in the top frame of a
273
+ * tab at exactly the relay's origin, and only while AI access is on and the
274
+ * user turned filling in on. Then the user is asked, with the AI app's
275
+ * verified host; on Allow the code goes to that one page, which fills it in
276
+ * and submits.
277
+ *
278
+ * No loops: one question per page load (one port per document, one hello
279
+ * per port). When the same tab shows the page of the same request (the
280
+ * relay's `request_id`) again soon after a fill, it is the relay's answer
281
+ * that the code was not taken (it had just changed): filled once more with
282
+ * the code of then, without asking; the time after, nothing. Any other
283
+ * request asks. Don't allow, a timeout, the page going, AI access or the
284
+ * setting going off, another relay or the AI app connecting meanwhile fill
285
+ * nothing, and typing by hand works as before.
286
+ */
287
+ export declare class CodeFiller {
288
+ #private;
289
+ /**
290
+ * `grants`: the ids of the grants there are already, if the extension kept
291
+ * them between worker starts (not new when listed again); without them,
292
+ * the first `grantsChanged` is taken as the list there is, nothing in it
293
+ * new. `closeAfterMs`: FILLED_CLOSE_MS unless a test's is shorter.
294
+ */
295
+ constructor(host: FillHost, options?: {
296
+ grants?: Iterable<string>;
297
+ closeAfterMs?: number;
298
+ });
299
+ /**
300
+ * A pairing page's port: checked, then asked about and maybe filled in.
301
+ * Never rejects: a host callback that throws fills nothing, closes the
302
+ * port and is logged.
303
+ */
304
+ page(port: PagePort): Promise<void>;
305
+ /**
306
+ * The relay's list of grants (`grantschange`): an AI app that just
307
+ * connected (typed by hand, or filled in) withdraws the questions for its
308
+ * host, and its fills are done with.
309
+ */
310
+ grantsChanged(grants: readonly {
311
+ grantId: string;
312
+ host: string;
313
+ }[]): void;
314
+ /**
315
+ * AI access off, or filling in turned off: the questions waiting are
316
+ * withdrawn and no page gets the one more fill.
317
+ */
318
+ withdrawAll(): void;
319
+ /**
320
+ * Another relay: no page of the one before is asked about or filled in,
321
+ * and the new relay's first list of grants is taken as it is.
322
+ */
323
+ reset(): void;
324
+ }
325
+ /** Where the pairing codes come from (`pairingCodes(mcp)`). */
326
+ export interface CodeSource {
327
+ current(): PairingCode | undefined;
328
+ /** Call `listener` on every new code (or none); returns how to stop. */
329
+ onChange(listener: () => void): () => void;
330
+ }
331
+ /** The pairing codes of the extension's `TabMcp`, for `takeCode`. */
332
+ export declare function pairingCodes(mcp: Pick<TabMcp, "pairingCode" | "addEventListener" | "removeEventListener">): CodeSource;
333
+ /**
334
+ * The code to fill in. The current one if it has `minLeftMs` left by this
335
+ * clock (or an expiry that cannot be read); else the next one the relay
336
+ * sends, which is fresh whatever the clocks say. If none comes within
337
+ * `waitMs`, the current one after all: with this clock ahead every code
338
+ * would look expired, and the relay refuses one that is, which the one
339
+ * more fill for the same request covers. `undefined` only without any code,
340
+ * or when `signal` aborts. Pairing must be on (`startPairing()`) for codes
341
+ * to come.
342
+ */
343
+ export declare function takeCode(source: CodeSource, signal: AbortSignal, options: {
344
+ now: number;
345
+ waitMs?: number;
346
+ minLeftMs?: number;
347
+ }): Promise<string | undefined>;