@extension-use/host 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ahmed Gamal and Extension-use contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,7 @@
1
- # Temporary Holding Version
1
+ # @extension-use/host
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Runs inside your extension: `defineHost()` (router, grants, site policy), the content runtime with page actions, the side panel frame bridge, manifest helpers, the `extension-use` CLI and no-build bundles.
4
+
5
+ Part of **Extension-use**, which lets a web app drive a browser extension through a fixed, typed set of actions. See the main README and docs in the Extension-use repository.
6
+
7
+ License: MIT
@@ -0,0 +1,317 @@
1
+ import * as _extension_use_protocol from '@extension-use/protocol';
2
+ import { Envelope, Target, Capability } from '@extension-use/protocol';
3
+ export { ErrorCode, ExtensionUseError } from '@extension-use/protocol';
4
+ import { S as SessionInfo, a as ActionDef, R as ResolvedTab, A as ActionGroup } from '../groups-DC3obZGP.js';
5
+ export { c as ActionContext, O as OPT_IN_GROUPS, d as allBuiltinGroups, b as builtinGroups, e as defineAction } from '../groups-DC3obZGP.js';
6
+ import '@extension-use/protocol/schemas';
7
+ import 'zod';
8
+
9
+ /**
10
+ * The router is the core of a host. It knows nothing about chrome.* APIs:
11
+ * transports hand it a channel, and the environment (tab lookup, content
12
+ * messaging) is injected. This keeps it unit-testable and reusable for the
13
+ * remote relay connection later.
14
+ */
15
+
16
+ declare const SDK_VERSION = "0.1.0";
17
+ /** Pages a controller may never act on, whatever the host's own policy says. */
18
+ declare const ALWAYS_BLOCKED: string[];
19
+ interface Policy {
20
+ /** Extra URL patterns controllers may not act on. */
21
+ blockedSites?: string[];
22
+ /** What to do with password and payment fields. Default "refuse". */
23
+ sensitiveFields?: "refuse" | "allow";
24
+ }
25
+ interface ContentRequest {
26
+ callId: string;
27
+ action: string;
28
+ input: unknown;
29
+ timeoutMs: number;
30
+ policy: {
31
+ sensitiveFields: "refuse" | "allow";
32
+ };
33
+ /** The call's target, for hosts and mocks that need it. */
34
+ target?: Target;
35
+ /** Who is calling, so the page can show that it is being controlled. */
36
+ controller?: {
37
+ transport: string;
38
+ origin: string;
39
+ };
40
+ }
41
+ interface RouterEnv {
42
+ resolveTab(target: Target | undefined, session: SessionInfo): Promise<ResolvedTab>;
43
+ sendToContent(tab: ResolvedTab, frameId: number, req: ContentRequest): Promise<unknown>;
44
+ cancelContent?(tab: ResolvedTab, frameId: number, callId: string): void;
45
+ /** Find a frame by URL in a tab (for target.frame = { urlMatch }). */
46
+ resolveFrame?(tab: ResolvedTab, frame: {
47
+ urlMatch: string;
48
+ }): Promise<number>;
49
+ }
50
+ interface AuditEntry {
51
+ ts: number;
52
+ session: string;
53
+ transport: string;
54
+ origin: string;
55
+ action: string;
56
+ tabId?: number;
57
+ ok: boolean;
58
+ code?: string;
59
+ ms: number;
60
+ }
61
+ interface RouterOptions {
62
+ host: {
63
+ id: string;
64
+ name?: string;
65
+ version?: string;
66
+ };
67
+ actions: ActionDef[];
68
+ env: RouterEnv;
69
+ policy?: Policy;
70
+ defaultTimeoutMs?: number;
71
+ auditSize?: number;
72
+ onAudit?(entry: AuditEntry): void;
73
+ debug?: boolean;
74
+ /** Called when a controller session opens or closes. */
75
+ onSessionsChange?(sessions: SessionInfo[]): void;
76
+ }
77
+ interface Channel {
78
+ transport: string;
79
+ /** Origin of the controller, already verified by the transport. */
80
+ origin: string;
81
+ /** Action patterns this controller may call, e.g. ["*"] or ["dom.*", "tabs.list"]. */
82
+ grants: string[];
83
+ windowId?: number;
84
+ send(env: Envelope): void;
85
+ }
86
+ interface Connection {
87
+ receive(message: unknown): void;
88
+ close(): void;
89
+ readonly session: SessionInfo | undefined;
90
+ }
91
+ type Router = ReturnType<typeof createRouter>;
92
+ declare function createRouter(opts: RouterOptions): {
93
+ connect: (channel: Channel) => Connection;
94
+ emit: (name: string, payload: unknown, tabId?: number) => void;
95
+ capabilities: () => Capability[];
96
+ sessions: () => SessionInfo[];
97
+ audit: () => AuditEntry[];
98
+ /** Kill switch: reject every call until resumed. */
99
+ pause(): void;
100
+ resume(): void;
101
+ readonly paused: boolean;
102
+ assertAllowedUrl: (url: string) => void;
103
+ };
104
+
105
+ type RemoteStatus = "disconnected" | "connecting" | "connected" | "unauthorized";
106
+ interface RemoteState {
107
+ status: RemoteStatus;
108
+ relay: string;
109
+ deviceId?: string;
110
+ name?: string;
111
+ error?: string;
112
+ sessions: {
113
+ id: string;
114
+ origin: string;
115
+ label?: string;
116
+ }[];
117
+ }
118
+ interface RemoteLinkOptions {
119
+ /** Relay base URL, e.g. "http://127.0.0.1:7777". */
120
+ relay: string;
121
+ /** Full WebSocket URL, when it isn't `<relay>/v1/device` (a local agent link). */
122
+ url?: string;
123
+ /** Device token from pairing. Ignored when `agentKey` is set. */
124
+ deviceToken?: string;
125
+ /**
126
+ * Authenticate with the local agent handshake instead of a device token:
127
+ * both sides prove they know this key, and nothing is accepted before the
128
+ * other side's proof checks out.
129
+ */
130
+ agentKey?: string;
131
+ /** Transport name sessions get in the router. Default "remote". */
132
+ transport?: string;
133
+ router: Router;
134
+ /** Action patterns remote controllers may call (the relay may narrow them further per token). */
135
+ grants: string[];
136
+ WebSocket?: typeof globalThis.WebSocket;
137
+ onState?(state: RemoteState): void;
138
+ log?(...args: unknown[]): void;
139
+ /** Reconnect delays in ms. Default 1s, 2s, 5s, 10s, then every 30s. */
140
+ backoff?: number[];
141
+ }
142
+ declare function createRemoteLink(opts: RemoteLinkOptions): {
143
+ start(): void;
144
+ /** Reconnect now (for example when an alarm wakes the service worker). */
145
+ poke(): void;
146
+ stop(): void;
147
+ readonly state: RemoteState;
148
+ };
149
+ type RemoteLink = ReturnType<typeof createRemoteLink>;
150
+
151
+ interface RemoteConfig {
152
+ /** Action patterns remote controllers may call. Default: every action. Each relay token can narrow this further. */
153
+ grants?: "all" | string[];
154
+ /** Relays the person may pair with (URL patterns). Default: this machine (any port) and any https relay. */
155
+ relays?: string[];
156
+ /** Pre-filled relay URL in the control panel. Default "http://127.0.0.1:7777". */
157
+ defaultRelay?: string;
158
+ }
159
+ interface PairingState {
160
+ relay: string;
161
+ pairingId: string;
162
+ code: string;
163
+ expiresAt: number;
164
+ status: "pending" | "denied" | "expired" | "error";
165
+ error?: string;
166
+ }
167
+ interface RemoteUiState {
168
+ paired: boolean;
169
+ link?: RemoteState;
170
+ pairing?: PairingState;
171
+ defaultRelay: string;
172
+ }
173
+
174
+ interface AgentConfig {
175
+ /** Action patterns agents may call. Default: every action. */
176
+ grants?: "all" | string[];
177
+ /** Local ports to dial. Default: AGENT_PORTS (47821–47825). Must match the MCP server's. */
178
+ ports?: readonly number[];
179
+ /** Name of the MCP server in the instructions. Default "browser". */
180
+ serverName?: string;
181
+ }
182
+ interface AgentUiState {
183
+ enabled: boolean;
184
+ /** The key and the instructions to paste into an agent (only while enabled). */
185
+ key?: string;
186
+ instructions?: string;
187
+ /** Agents connected right now, by the name their MCP client reported. */
188
+ agents: string[];
189
+ }
190
+ /** The text the person pastes into an agent. */
191
+ declare function agentInstructions(key: string, opts?: {
192
+ hostName?: string;
193
+ serverName?: string;
194
+ }): string;
195
+
196
+ type UiCommand = {
197
+ cmd: "state";
198
+ } | {
199
+ cmd: "pause";
200
+ } | {
201
+ cmd: "resume";
202
+ } | {
203
+ cmd: "pair";
204
+ relay: string;
205
+ name: string;
206
+ } | {
207
+ cmd: "cancel-pairing";
208
+ } | {
209
+ cmd: "unpair";
210
+ } | {
211
+ cmd: "reconnect";
212
+ } | {
213
+ cmd: "agent-enable";
214
+ } | {
215
+ cmd: "agent-disable";
216
+ } | {
217
+ cmd: "describe";
218
+ } | {
219
+ cmd: "call";
220
+ id: string;
221
+ action: string;
222
+ input: unknown;
223
+ target?: _extension_use_protocol.Target;
224
+ };
225
+ interface UiState {
226
+ host: {
227
+ id: string;
228
+ name?: string;
229
+ version?: string;
230
+ };
231
+ paused: boolean;
232
+ sessions: {
233
+ id: string;
234
+ transport: string;
235
+ origin: string;
236
+ }[];
237
+ audit: {
238
+ ts: number;
239
+ transport: string;
240
+ origin: string;
241
+ action: string;
242
+ ok: boolean;
243
+ code?: string;
244
+ ms: number;
245
+ }[];
246
+ /** Undefined when the host has no `controllers.remote`. */
247
+ remote?: RemoteUiState;
248
+ /** Undefined when the host has no `controllers.agent`. */
249
+ agent?: AgentUiState;
250
+ error?: string;
251
+ }
252
+
253
+ interface ControllerConfig {
254
+ /** Origins allowed to connect, e.g. ["https://app.example.com", "http://localhost:5173"]. */
255
+ origins: string[];
256
+ /** Action patterns this controller may call. "all" means every action. */
257
+ grants?: "all" | string[];
258
+ }
259
+ interface HostOptions {
260
+ id: string;
261
+ name?: string;
262
+ /** Default: the extension's manifest version. */
263
+ version?: string;
264
+ /** Built-in groups to enable. Default: all of them. */
265
+ groups?: ActionGroup[];
266
+ /** Host-defined actions, such as `myapp.saveArticle`. */
267
+ actions?: ActionDef[];
268
+ controllers: {
269
+ /** Web apps loaded in an iframe inside an extension page (side panel, popup...). */
270
+ frame?: ControllerConfig;
271
+ /** Websites that connect directly via `externally_connectable`. */
272
+ page?: ControllerConfig;
273
+ /**
274
+ * Scripts, servers and agents that reach this browser through a relay the
275
+ * person paired with (in the control panel). Needs "storage" and "alarms".
276
+ */
277
+ remote?: RemoteConfig;
278
+ /**
279
+ * Coding agents and local models on this computer, with no relay: the
280
+ * person clicks "Connect an agent" in the control panel and pastes the
281
+ * instructions into their agent. Needs "storage" and "alarms".
282
+ */
283
+ agent?: AgentConfig;
284
+ };
285
+ /** Show control state on the toolbar icon (needs an `action` in the manifest). Default true. */
286
+ badge?: boolean;
287
+ policy?: Policy;
288
+ /**
289
+ * Path of the bundled content script inside the extension (e.g. "content.js").
290
+ * When set, the host injects it into tabs that don't have it yet (needs the
291
+ * "scripting" permission).
292
+ */
293
+ contentScript?: string;
294
+ /**
295
+ * Path of an extension page that calls mountOffscreen() from
296
+ * "@extension-use/host/offscreen" (e.g. "offscreen.html"). The tabCapture
297
+ * group holds its capture streams there, and screenshots of background tabs
298
+ * read frames from it.
299
+ */
300
+ offscreen?: string;
301
+ defaultTimeoutMs?: number;
302
+ onAudit?(entry: AuditEntry): void;
303
+ debug?: boolean;
304
+ }
305
+ declare function defineHost(options: HostOptions): {
306
+ connect: (channel: Channel) => Connection;
307
+ emit: (name: string, payload: unknown, tabId?: number) => void;
308
+ capabilities: () => _extension_use_protocol.Capability[];
309
+ sessions: () => SessionInfo[];
310
+ audit: () => AuditEntry[];
311
+ pause(): void;
312
+ resume(): void;
313
+ readonly paused: boolean;
314
+ assertAllowedUrl: (url: string) => void;
315
+ };
316
+
317
+ export { ALWAYS_BLOCKED, ActionDef, ActionGroup, type AgentConfig, type AgentUiState, type AuditEntry, type Channel, type Connection, type ContentRequest, type ControllerConfig, type HostOptions, type PairingState, type Policy, type RemoteConfig, type RemoteLink, type RemoteLinkOptions, type RemoteState, type RemoteUiState, ResolvedTab, type Router, type RouterEnv, type RouterOptions, SDK_VERSION, SessionInfo, type UiCommand, type UiState, agentInstructions, createRemoteLink, createRouter, defineHost };