trusted-user-agent 0.0.0-stage → 0.3.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 +21 -0
- package/dist/acp/agent.js +601 -0
- package/dist/acp/backend.js +2 -0
- package/dist/acp/config-options.js +26 -0
- package/dist/acp/mock-backend.js +277 -0
- package/dist/acp/sdk-backend.js +495 -0
- package/dist/browser/mcp-acp-client.js +81 -0
- package/dist/browser/provider.js +1045 -0
- package/dist/browser/schemas.js +221 -0
- package/dist/capability-registry.js +92 -0
- package/dist/client-heartbeat.js +118 -0
- package/dist/compose/schemas.js +90 -0
- package/dist/contacts/schemas.js +26 -0
- package/dist/installer/cli.js +70 -0
- package/dist/installer/common.js +93 -0
- package/dist/installer/index.js +341 -0
- package/dist/installer/mcp-clients.js +555 -0
- package/dist/installer/mcp.js +166 -0
- package/dist/installer/platforms.js +277 -0
- package/dist/logger.js +54 -0
- package/dist/mail/schemas.js +98 -0
- package/dist/mutation/schemas.js +31 -0
- package/dist/native-host/broker-ipc.js +344 -0
- package/dist/native-host/framing.js +76 -0
- package/dist/native-host/main.js +520 -0
- package/dist/native-host/mcp-server.js +322 -0
- package/dist/native-host/mcp-stdio-bridge.js +179 -0
- package/dist/native-host/mcp-stdio-main.js +13 -0
- package/dist/native-host/relay.js +84 -0
- package/dist/native-host/skills-startup.js +167 -0
- package/dist/native-host/transport.js +248 -0
- package/dist/permission-store.js +207 -0
- package/dist/repl/fs-sandbox.js +187 -0
- package/dist/repl/provider.js +252 -0
- package/dist/repl/runtime.js +436 -0
- package/dist/repl/types.js +10 -0
- package/dist/repl/worker.js +731 -0
- package/dist/skills/extension.js +99 -0
- package/dist/skills/grants-file.js +145 -0
- package/dist/skills/isolated-runner.js +259 -0
- package/dist/skills/mock-session-factory.js +49 -0
- package/dist/skills/package-roots.js +25 -0
- package/dist/skills/pi-bridge.js +8 -0
- package/dist/skills/pi-integration.js +110 -0
- package/dist/skills/planner.js +128 -0
- package/dist/skills/registry.js +104 -0
- package/dist/skills/skills-protocol.js +253 -0
- package/dist/skills/skills-service.js +195 -0
- package/dist/tool-bridge.js +8 -0
- package/dist/workspace.js +70 -0
- package/extensions/pi-browser-tools.ts +65 -0
- package/extensions/pi-browser.ts +60 -0
- package/extensions/pi-skills.ts +14 -0
- package/native/pi-agent-host +2 -0
- package/native/pi-browser-host +5 -0
- package/native/pi-browser-host.cmd +3 -0
- package/node_modules/@pi-browser/protocol/.bundled-by-trusted-user-agent +0 -0
- package/node_modules/@pi-browser/protocol/dist/acp.d.ts +10 -0
- package/node_modules/@pi-browser/protocol/dist/acp.js +9 -0
- package/node_modules/@pi-browser/protocol/dist/browser-tools.d.ts +33 -0
- package/node_modules/@pi-browser/protocol/dist/browser-tools.js +478 -0
- package/node_modules/@pi-browser/protocol/dist/callers.d.ts +108 -0
- package/node_modules/@pi-browser/protocol/dist/callers.js +56 -0
- package/node_modules/@pi-browser/protocol/dist/compose-tools.d.ts +30 -0
- package/node_modules/@pi-browser/protocol/dist/compose-tools.js +175 -0
- package/node_modules/@pi-browser/protocol/dist/contacts-tools.d.ts +23 -0
- package/node_modules/@pi-browser/protocol/dist/contacts-tools.js +63 -0
- package/node_modules/@pi-browser/protocol/dist/control-tools.d.ts +32 -0
- package/node_modules/@pi-browser/protocol/dist/control-tools.js +201 -0
- package/node_modules/@pi-browser/protocol/dist/errors.d.ts +63 -0
- package/node_modules/@pi-browser/protocol/dist/errors.js +109 -0
- package/node_modules/@pi-browser/protocol/dist/index.d.ts +16 -0
- package/node_modules/@pi-browser/protocol/dist/index.js +16 -0
- package/node_modules/@pi-browser/protocol/dist/integration.d.ts +409 -0
- package/node_modules/@pi-browser/protocol/dist/integration.js +260 -0
- package/node_modules/@pi-browser/protocol/dist/jsonrpc.d.ts +33 -0
- package/node_modules/@pi-browser/protocol/dist/jsonrpc.js +31 -0
- package/node_modules/@pi-browser/protocol/dist/mail-mutation-tools.d.ts +27 -0
- package/node_modules/@pi-browser/protocol/dist/mail-mutation-tools.js +88 -0
- package/node_modules/@pi-browser/protocol/dist/mail-tools.d.ts +133 -0
- package/node_modules/@pi-browser/protocol/dist/mail-tools.js +245 -0
- package/node_modules/@pi-browser/protocol/dist/permission.d.ts +113 -0
- package/node_modules/@pi-browser/protocol/dist/permission.js +257 -0
- package/node_modules/@pi-browser/protocol/dist/skills.d.ts +304 -0
- package/node_modules/@pi-browser/protocol/dist/skills.js +473 -0
- package/node_modules/@pi-browser/protocol/dist/tool-capabilities.d.ts +18 -0
- package/node_modules/@pi-browser/protocol/dist/tool-capabilities.js +52 -0
- package/node_modules/@pi-browser/protocol/dist/transport.d.ts +17 -0
- package/node_modules/@pi-browser/protocol/dist/transport.js +17 -0
- package/node_modules/@pi-browser/protocol/package.json +14 -0
- package/package.json +71 -6
- package/skills/browser-walk/SKILL.md +98 -0
- package/README.md +0 -3
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declarative skills runtime v2.
|
|
3
|
+
*
|
|
4
|
+
* A skill package is ONE activation.json: the package declares the site it
|
|
5
|
+
* covers (hosts), how that site's DOM maps to site events (targets + events),
|
|
6
|
+
* and the skills that listen to those events. Subscriptions may only name
|
|
7
|
+
* events defined in the same file — there is no global event catalog and no
|
|
8
|
+
* cross-file reference to validate.
|
|
9
|
+
*
|
|
10
|
+
* `targets` carry a selector: a CSS selector string (isolated-world runtime,
|
|
11
|
+
* no eval) or a JS dialect with the strict `el =>` prefix (main-world runtime
|
|
12
|
+
* for rules that use it). Everything else is inert data.
|
|
13
|
+
*/
|
|
14
|
+
export const SKILL_CAPABILITIES = ["context.read", "suggestion.present", "draft.replace-on-accept", "memory.read", "memory.propose"];
|
|
15
|
+
/** DOM event types the page interpreter can observe. */
|
|
16
|
+
export const SKILL_DOM_TYPES = ["input", "keydown", "click"];
|
|
17
|
+
export const SKILL_KEY_MODIFIERS = ["alt", "ctrl", "shift", "meta"];
|
|
18
|
+
/** Global event identity: `<packageId>:<local event name>`. */
|
|
19
|
+
export function skillEventName(packageId, local) {
|
|
20
|
+
return `${packageId}:${local}`;
|
|
21
|
+
}
|
|
22
|
+
/** Strict objects reject unsupported operators instead of silently ignoring them. */
|
|
23
|
+
function object(value, keys) {
|
|
24
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
25
|
+
throw new Error("Expected object");
|
|
26
|
+
for (const key of Object.keys(value))
|
|
27
|
+
if (!keys.includes(key))
|
|
28
|
+
throw new Error(`Unknown field: ${key}`);
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
function text(value, max = 256) {
|
|
32
|
+
if (typeof value !== "string" || !value.trim() || value.length > max)
|
|
33
|
+
throw new Error("Invalid string");
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
function choice(value, values) {
|
|
37
|
+
if (!values.includes(value))
|
|
38
|
+
throw new Error(`Unsupported value: ${String(value)}`);
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
function integer(value, min, max) {
|
|
42
|
+
if (!Number.isSafeInteger(value) || value < min || value > max)
|
|
43
|
+
throw new Error("Invalid integer");
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
function list(value, parse, max = 64) {
|
|
47
|
+
if (!Array.isArray(value) || value.length > max)
|
|
48
|
+
throw new Error("Invalid array");
|
|
49
|
+
return value.map(parse);
|
|
50
|
+
}
|
|
51
|
+
function identifier(value, max = 64) {
|
|
52
|
+
const result = text(value, max);
|
|
53
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(result))
|
|
54
|
+
throw new Error("Invalid identifier");
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
/** Local event names: dotted/hyphenated lowercase, e.g. `draft.paused`. */
|
|
58
|
+
function eventName(value, max = 64) {
|
|
59
|
+
const result = text(value, max);
|
|
60
|
+
if (!/^[a-z0-9]+(?:[.-][a-z0-9]+)*$/.test(result))
|
|
61
|
+
throw new Error("Invalid event name");
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Context/payload field names: camelCase identifiers (package-defined).
|
|
66
|
+
* The page payload, the skill's context requests, and the coordinator's
|
|
67
|
+
* snapshot store all speak these names.
|
|
68
|
+
*/
|
|
69
|
+
function fieldName(value, max = 64) {
|
|
70
|
+
const result = text(value, max);
|
|
71
|
+
if (!/^[a-z][a-zA-Z0-9]*$/.test(result))
|
|
72
|
+
throw new Error("Invalid field name");
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
/** Global event identity `<packageId>:<local>` (max 128 total). */
|
|
76
|
+
function qualifiedEventName(value) {
|
|
77
|
+
const result = text(value, 128);
|
|
78
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*:[a-z0-9]+(?:[.-][a-z0-9]+)*$/.test(result))
|
|
79
|
+
throw new Error("Invalid event name");
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
export function skillRelativePath(value) {
|
|
83
|
+
const result = text(value, 512);
|
|
84
|
+
if (result.includes("\\") || result.includes("\0") || result.includes(":"))
|
|
85
|
+
throw new Error("Invalid package path");
|
|
86
|
+
if (result.split("/").some((part) => !part || part === "." || part === ".."))
|
|
87
|
+
throw new Error("Path escapes package");
|
|
88
|
+
if (!result.endsWith("/SKILL.md"))
|
|
89
|
+
throw new Error("Instructions must name SKILL.md");
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
export function normalizeSkillHost(value) {
|
|
93
|
+
const host = text(value, 253).toLowerCase().replace(/\.$/, "");
|
|
94
|
+
if (!host.split(".").every((part) => /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/.test(part)))
|
|
95
|
+
throw new Error("Invalid hostname");
|
|
96
|
+
return host;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Target selectors: two dialects, detected statically by the strict `el =>`
|
|
100
|
+
* prefix. CSS is the isolated-world default; the JS dialect is syntax-checked
|
|
101
|
+
* at load (the host has no CSP) and forces its rule into the main world.
|
|
102
|
+
* Bounds keep a hostile package from shipping megabyte selectors.
|
|
103
|
+
*/
|
|
104
|
+
const JS_TARGET = /^el\s*=>/;
|
|
105
|
+
/** Extract the expression after the `el =>` prefix. */
|
|
106
|
+
function jsTargetExpression(selector) {
|
|
107
|
+
const match = /^el\s*=>\s*([\s\S]*)$/.exec(selector);
|
|
108
|
+
if (!match)
|
|
109
|
+
throw new Error("Invalid JS target");
|
|
110
|
+
return match[1];
|
|
111
|
+
}
|
|
112
|
+
function parseTargetValue(value, context) {
|
|
113
|
+
let selector;
|
|
114
|
+
if (typeof value === "string") {
|
|
115
|
+
selector = text(value, 16_384);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
const entry = object(value, ["selector", "text", "textFlags", "maxTextLength"]);
|
|
119
|
+
selector = text(entry.selector, 16_384);
|
|
120
|
+
return parseTargetFields(selector, entry, context);
|
|
121
|
+
}
|
|
122
|
+
return parseTargetFields(selector, {}, context);
|
|
123
|
+
}
|
|
124
|
+
function parseTargetFields(selector, entry, context) {
|
|
125
|
+
const target = { selector, kind: JS_TARGET.test(selector) ? "js" : "css" };
|
|
126
|
+
if (entry.text !== undefined) {
|
|
127
|
+
target.text = text(entry.text, 1_024);
|
|
128
|
+
try {
|
|
129
|
+
new RegExp(target.text, target.textFlags ?? "");
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
throw new Error(`Invalid target text regex: ${String(err instanceof Error ? err.message : err)}`.slice(0, 200));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (entry.textFlags !== undefined) {
|
|
136
|
+
if (entry.textFlags !== "i")
|
|
137
|
+
throw new Error(`Unsupported textFlags: ${String(entry.textFlags)}`);
|
|
138
|
+
target.textFlags = "i";
|
|
139
|
+
}
|
|
140
|
+
if (entry.maxTextLength !== undefined)
|
|
141
|
+
target.maxTextLength = integer(entry.maxTextLength, 1, 100_000);
|
|
142
|
+
if (target.kind === "js")
|
|
143
|
+
checkJsTarget(target.selector, context);
|
|
144
|
+
return target;
|
|
145
|
+
}
|
|
146
|
+
/** The page interpreter compiles the same shape at arm time (main world). */
|
|
147
|
+
function checkJsTarget(selector, context) {
|
|
148
|
+
try {
|
|
149
|
+
// eslint-disable-next-line no-new-func
|
|
150
|
+
new Function("el", `return (${jsTargetExpression(selector)});`);
|
|
151
|
+
}
|
|
152
|
+
catch (err) {
|
|
153
|
+
throw new Error(`Target selector failed to parse (${context}): ${String(err instanceof Error ? err.message : err)}`.slice(0, 200));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function parseTarget(value) {
|
|
157
|
+
return parseTargetValue(value, "target");
|
|
158
|
+
}
|
|
159
|
+
function parseMatcher(value) {
|
|
160
|
+
const entry = object(value, ["dom", "target", "key", "modifiers"]);
|
|
161
|
+
const result = { dom: choice(entry.dom, SKILL_DOM_TYPES), target: fieldName(entry.target, 64) };
|
|
162
|
+
if (entry.key !== undefined) {
|
|
163
|
+
if (result.dom !== "keydown")
|
|
164
|
+
throw new Error("key is only valid for keydown");
|
|
165
|
+
result.key = text(entry.key, 16);
|
|
166
|
+
}
|
|
167
|
+
if (entry.modifiers !== undefined) {
|
|
168
|
+
if (result.dom !== "keydown")
|
|
169
|
+
throw new Error("modifiers are only valid for keydown");
|
|
170
|
+
result.modifiers = list(entry.modifiers, (m) => choice(m, SKILL_KEY_MODIFIERS), 4);
|
|
171
|
+
}
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
function parsePayloadField(value) {
|
|
175
|
+
const entry = object(value, ["from", "field", "resolve", "maxChars"]);
|
|
176
|
+
const result = { from: fieldName(entry.from, 64), field: choice(entry.field, ["value", "text"]) };
|
|
177
|
+
if (entry.resolve !== undefined)
|
|
178
|
+
result.resolve = choice(entry.resolve, ["matched", "document"]);
|
|
179
|
+
if (entry.maxChars !== undefined)
|
|
180
|
+
result.maxChars = integer(entry.maxChars, 1, 262_144);
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
/** Parse one event definition; `names` must contain every referenced target. */
|
|
184
|
+
function parseEventDefinition(value, targetNames) {
|
|
185
|
+
const entry = object(value, ["kind", "debounceMs", "suspendDuringComposition", "sequence", "chainId", "alternatives", "stopDefaultAction", "payload"]);
|
|
186
|
+
const result = { kind: choice(entry.kind, ["debounced", "gate"]) };
|
|
187
|
+
const matchers = (value) => list(value, parseMatcher, 8).map((m) => { if (!targetNames.has(m.target))
|
|
188
|
+
throw new Error(`Unknown target: ${m.target}`); return m; });
|
|
189
|
+
if (result.kind === "debounced") {
|
|
190
|
+
if (entry.sequence === undefined)
|
|
191
|
+
throw new Error("Debounced events require a sequence");
|
|
192
|
+
result.sequence = matchers(entry.sequence);
|
|
193
|
+
if (entry.chainId !== undefined || entry.alternatives !== undefined || entry.stopDefaultAction !== undefined)
|
|
194
|
+
throw new Error("Invalid debounced event fields");
|
|
195
|
+
result.debounceMs = integer(entry.debounceMs ?? 0, 0, 60_000);
|
|
196
|
+
if (entry.suspendDuringComposition !== undefined)
|
|
197
|
+
result.suspendDuringComposition = entry.suspendDuringComposition === true;
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
if (entry.alternatives === undefined)
|
|
201
|
+
throw new Error("Gate events require alternatives");
|
|
202
|
+
result.alternatives = list(entry.alternatives, (alt) => matchers(alt), 16);
|
|
203
|
+
if (entry.sequence !== undefined)
|
|
204
|
+
throw new Error("Invalid gate event fields");
|
|
205
|
+
if (entry.stopDefaultAction !== undefined)
|
|
206
|
+
result.stopDefaultAction = entry.stopDefaultAction === true;
|
|
207
|
+
result.chainId = identifier(entry.chainId, 64);
|
|
208
|
+
}
|
|
209
|
+
if (entry.payload !== undefined) {
|
|
210
|
+
const payload = object(entry.payload, Object.keys(entry.payload));
|
|
211
|
+
result.payload = {};
|
|
212
|
+
const names = Object.keys(payload);
|
|
213
|
+
if (names.length > 8)
|
|
214
|
+
throw new Error("Too many payload fields");
|
|
215
|
+
for (const name of names) {
|
|
216
|
+
if (!fieldName(name, 64))
|
|
217
|
+
throw new Error(`Invalid payload field name: ${name}`);
|
|
218
|
+
const field = parsePayloadField(payload[name]);
|
|
219
|
+
if (!targetNames.has(field.from))
|
|
220
|
+
throw new Error(`Unknown payload source: ${field.from}`);
|
|
221
|
+
result.payload[name] = field;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return result;
|
|
225
|
+
}
|
|
226
|
+
function parseSubscription(value, eventNames) {
|
|
227
|
+
const entry = object(value, ["event", "conditions"]);
|
|
228
|
+
const name = eventName(entry.event);
|
|
229
|
+
if (!eventNames.has(name))
|
|
230
|
+
throw new Error(`Subscription names undefined event: ${name}`);
|
|
231
|
+
const result = { event: name };
|
|
232
|
+
if (entry.conditions !== undefined) {
|
|
233
|
+
const conditions = object(entry.conditions, ["minCharacters"]);
|
|
234
|
+
result.conditions = {};
|
|
235
|
+
if (conditions.minCharacters !== undefined)
|
|
236
|
+
result.conditions.minCharacters = integer(conditions.minCharacters, 0, 32_768);
|
|
237
|
+
}
|
|
238
|
+
return result;
|
|
239
|
+
}
|
|
240
|
+
/** Context field names: package-defined payload field names (camelCase). */
|
|
241
|
+
function contextField(value) {
|
|
242
|
+
return fieldName(value, 64);
|
|
243
|
+
}
|
|
244
|
+
function parseSkillActivation(entry, result, eventNames, payloadFields) {
|
|
245
|
+
const checkFields = (fields) => {
|
|
246
|
+
for (const field of fields)
|
|
247
|
+
if (!payloadFields.has(field))
|
|
248
|
+
throw new Error(`Context field not in any subscribed event payload: ${field}`);
|
|
249
|
+
};
|
|
250
|
+
if (result.type === "active") {
|
|
251
|
+
result.command = identifier(entry.command);
|
|
252
|
+
if (entry.subscriptions !== undefined || entry.observations !== undefined || entry.batch !== undefined)
|
|
253
|
+
throw new Error("Active skills require a command only");
|
|
254
|
+
checkFields(result.context.required);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
if (entry.command !== undefined)
|
|
258
|
+
throw new Error("Automatic skills cannot declare commands");
|
|
259
|
+
if (result.type === "triggered") {
|
|
260
|
+
result.subscriptions = list(entry.subscriptions, (v) => parseSubscription(v, eventNames));
|
|
261
|
+
if (!result.subscriptions.length || entry.observations !== undefined || entry.batch !== undefined)
|
|
262
|
+
throw new Error("Invalid triggered activation");
|
|
263
|
+
checkFields([...result.context.required, ...result.context.optional]);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
result.observations = list(entry.observations, (v) => parseSubscription(v, eventNames));
|
|
267
|
+
if (!result.observations.length || entry.subscriptions !== undefined)
|
|
268
|
+
throw new Error("Invalid passive activation");
|
|
269
|
+
checkFields([...result.context.required, ...result.context.optional]);
|
|
270
|
+
const batch = object(entry.batch, ["maxEvents", "maxWaitMs"]);
|
|
271
|
+
result.batch = { maxEvents: integer(batch.maxEvents, 1, 64), maxWaitMs: integer(batch.maxWaitMs, 1, 300_000) };
|
|
272
|
+
}
|
|
273
|
+
function parseDefinition(value, eventNames, payloadFields) {
|
|
274
|
+
const entry = object(value, ["id", "type", "instructions", "subscriptions", "command", "observations", "batch", "context", "review", "capabilities", "memory", "execution"]);
|
|
275
|
+
const context = object(entry.context, ["required", "optional"]);
|
|
276
|
+
const execution = object(entry.execution, ["handler", "timeoutMs"]);
|
|
277
|
+
const result = {
|
|
278
|
+
id: identifier(entry.id), type: choice(entry.type, ["triggered", "active", "passive"]),
|
|
279
|
+
instructions: skillRelativePath(entry.instructions),
|
|
280
|
+
context: { required: list(context.required, contextField), optional: list(context.optional ?? [], contextField) },
|
|
281
|
+
capabilities: list(entry.capabilities, (v) => choice(v, SKILL_CAPABILITIES)),
|
|
282
|
+
execution: { handler: choice(execution.handler, ["agent"]), timeoutMs: integer(execution.timeoutMs, 1, 10_000) },
|
|
283
|
+
};
|
|
284
|
+
parseSkillActivation(entry, result, eventNames, payloadFields);
|
|
285
|
+
if (entry.review !== undefined) {
|
|
286
|
+
const review = object(entry.review, ["defaultMode"]);
|
|
287
|
+
result.review = { defaultMode: choice(review.defaultMode, ["advisory", "review-before-submit"]) };
|
|
288
|
+
}
|
|
289
|
+
if (entry.memory !== undefined) {
|
|
290
|
+
const memory = object(entry.memory, ["namespace", "scope"]);
|
|
291
|
+
result.memory = { namespace: identifier(memory.namespace), scope: choice(memory.scope, ["account", "profile"]) };
|
|
292
|
+
}
|
|
293
|
+
return result;
|
|
294
|
+
}
|
|
295
|
+
export function parseSkillPackage(value) {
|
|
296
|
+
const manifest = object(value, ["schemaVersion", "packageId", "application", "hosts", "subdomainHosts", "pathPrefixes", "targets", "events", "skills"]);
|
|
297
|
+
if (manifest.schemaVersion !== 2)
|
|
298
|
+
throw new Error("Unsupported skill schema version");
|
|
299
|
+
const packageId = identifier(manifest.packageId);
|
|
300
|
+
const application = choice(manifest.application, ["firefox", "thunderbird"]);
|
|
301
|
+
const hosts = list(manifest.hosts, normalizeSkillHost, 128);
|
|
302
|
+
const subdomainHosts = manifest.subdomainHosts === undefined ? [] : list(manifest.subdomainHosts, normalizeSkillHost, 128);
|
|
303
|
+
if (!hosts.length && !subdomainHosts.length)
|
|
304
|
+
throw new Error("Package requires at least one host");
|
|
305
|
+
const result = {
|
|
306
|
+
schemaVersion: 2, packageId, application, hosts,
|
|
307
|
+
targets: {}, events: {},
|
|
308
|
+
skills: [],
|
|
309
|
+
};
|
|
310
|
+
if (subdomainHosts.length)
|
|
311
|
+
result.subdomainHosts = subdomainHosts;
|
|
312
|
+
if (manifest.pathPrefixes !== undefined)
|
|
313
|
+
result.pathPrefixes = list(manifest.pathPrefixes, (value) => {
|
|
314
|
+
const prefix = text(value, 512);
|
|
315
|
+
if (!prefix.startsWith("/") || /[?#\\*]/.test(prefix))
|
|
316
|
+
throw new Error("Invalid path prefix");
|
|
317
|
+
return prefix;
|
|
318
|
+
});
|
|
319
|
+
const targets = object(manifest.targets, Object.keys(manifest.targets));
|
|
320
|
+
const targetNames = Object.keys(targets);
|
|
321
|
+
if (targetNames.length > 8)
|
|
322
|
+
throw new Error("Too many targets");
|
|
323
|
+
for (const name of targetNames) {
|
|
324
|
+
if (!fieldName(name, 64))
|
|
325
|
+
throw new Error(`Invalid target name: ${name}`);
|
|
326
|
+
result.targets[name] = parseTarget(targets[name]);
|
|
327
|
+
}
|
|
328
|
+
const events = object(manifest.events, Object.keys(manifest.events));
|
|
329
|
+
const eventNames = Object.keys(events);
|
|
330
|
+
if (!eventNames.length)
|
|
331
|
+
throw new Error("Package requires at least one event");
|
|
332
|
+
for (const name of eventNames) {
|
|
333
|
+
if (!eventName(name, 64))
|
|
334
|
+
throw new Error(`Invalid event name: ${name}`);
|
|
335
|
+
result.events[name] = parseEventDefinition(events[name], new Set(targetNames));
|
|
336
|
+
}
|
|
337
|
+
const payloadFields = new Set();
|
|
338
|
+
for (const def of Object.values(result.events)) {
|
|
339
|
+
for (const field of Object.keys(def.payload ?? {}))
|
|
340
|
+
payloadFields.add(field);
|
|
341
|
+
}
|
|
342
|
+
result.skills = list(manifest.skills, (v) => parseDefinition(v, new Set(eventNames), payloadFields), 16);
|
|
343
|
+
if (new Set(result.skills.map((skill) => skill.id)).size !== result.skills.length)
|
|
344
|
+
throw new Error("Duplicate skill ID");
|
|
345
|
+
return result;
|
|
346
|
+
}
|
|
347
|
+
/** Shape validation is separate from coordinator verification of sender identity. */
|
|
348
|
+
export function parseSkillEvent(value) {
|
|
349
|
+
const entry = object(value, ["version", "eventId", "event", "source", "interactionId", "draftRevision", "contextRevision", "planVersion", "origin", "causedBy", "contextRef"]);
|
|
350
|
+
if (entry.version !== 1)
|
|
351
|
+
throw new Error("Unsupported event version");
|
|
352
|
+
const result = {
|
|
353
|
+
version: 1, eventId: text(entry.eventId), event: qualifiedEventName(entry.event),
|
|
354
|
+
source: parseEventSource(entry.source), interactionId: text(entry.interactionId),
|
|
355
|
+
contextRevision: integer(entry.contextRevision, 0, Number.MAX_SAFE_INTEGER),
|
|
356
|
+
planVersion: integer(entry.planVersion, 0, Number.MAX_SAFE_INTEGER),
|
|
357
|
+
origin: choice(entry.origin, ["user", "site", "skill"]), contextRef: text(entry.contextRef),
|
|
358
|
+
};
|
|
359
|
+
if (entry.draftRevision !== undefined)
|
|
360
|
+
result.draftRevision = integer(entry.draftRevision, 0, Number.MAX_SAFE_INTEGER);
|
|
361
|
+
if (entry.causedBy !== undefined)
|
|
362
|
+
result.causedBy = text(entry.causedBy);
|
|
363
|
+
return result;
|
|
364
|
+
}
|
|
365
|
+
function parseEventSource(value) {
|
|
366
|
+
const source = object(value, ["application", "connectionEpoch", "documentGeneration", "tabId", "frameId", "accountId"]);
|
|
367
|
+
const result = {
|
|
368
|
+
application: choice(source.application, ["firefox", "thunderbird"]), connectionEpoch: text(source.connectionEpoch),
|
|
369
|
+
};
|
|
370
|
+
if (source.documentGeneration !== undefined)
|
|
371
|
+
result.documentGeneration = text(source.documentGeneration);
|
|
372
|
+
if (source.accountId !== undefined)
|
|
373
|
+
result.accountId = text(source.accountId);
|
|
374
|
+
if (source.tabId !== undefined)
|
|
375
|
+
result.tabId = integer(source.tabId, 0, Number.MAX_SAFE_INTEGER);
|
|
376
|
+
if (source.frameId !== undefined)
|
|
377
|
+
result.frameId = integer(source.frameId, 0, Number.MAX_SAFE_INTEGER);
|
|
378
|
+
if (result.application === "firefox" && (result.documentGeneration === undefined || result.tabId === undefined || result.frameId === undefined)) {
|
|
379
|
+
throw new Error("Browser events require document, tab and frame identity");
|
|
380
|
+
}
|
|
381
|
+
return result;
|
|
382
|
+
}
|
|
383
|
+
function parseDocumentUrl(value) {
|
|
384
|
+
const raw = text(value, 2048);
|
|
385
|
+
let url;
|
|
386
|
+
try {
|
|
387
|
+
url = new URL(raw);
|
|
388
|
+
}
|
|
389
|
+
catch {
|
|
390
|
+
throw new Error("Invalid document URL");
|
|
391
|
+
}
|
|
392
|
+
if (!["http:", "https:"].includes(url.protocol))
|
|
393
|
+
throw new Error("Unsupported document URL");
|
|
394
|
+
return raw;
|
|
395
|
+
}
|
|
396
|
+
export function parseSkillsPlanParams(value) {
|
|
397
|
+
const entry = object(value, ["source", "url"]);
|
|
398
|
+
return { source: parseEventSource(entry.source), url: parseDocumentUrl(entry.url) };
|
|
399
|
+
}
|
|
400
|
+
export function parseSkillsEventParams(value) {
|
|
401
|
+
const entry = object(value, ["event"]);
|
|
402
|
+
return { event: parseSkillEvent(entry.event) };
|
|
403
|
+
}
|
|
404
|
+
export function parseSkillsCancelParams(value) {
|
|
405
|
+
const entry = object(value, ["interactionId"]);
|
|
406
|
+
return { interactionId: text(entry.interactionId) };
|
|
407
|
+
}
|
|
408
|
+
export function parseSkillsContextRequestParams(value) {
|
|
409
|
+
const entry = object(value, ["contextRef", "interactionId", "planVersion", "fields", "maxBytes"]);
|
|
410
|
+
return {
|
|
411
|
+
contextRef: text(entry.contextRef), interactionId: text(entry.interactionId),
|
|
412
|
+
planVersion: integer(entry.planVersion, 0, Number.MAX_SAFE_INTEGER),
|
|
413
|
+
fields: list(entry.fields, (f) => fieldName(f, 64), 8),
|
|
414
|
+
maxBytes: integer(entry.maxBytes, 1, 32_768),
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
/** The coordinator's snapshot payload; the host binds it to the job identity. */
|
|
418
|
+
export function parseSkillsContextResult(value, maxBytes) {
|
|
419
|
+
const entry = object(value, ["contextRef", "context", "bytes"]);
|
|
420
|
+
const inner = object(entry.context, Object.keys(entry.context));
|
|
421
|
+
for (const key of Object.keys(inner))
|
|
422
|
+
if (!fieldName(key, 64))
|
|
423
|
+
throw new Error("Invalid context field");
|
|
424
|
+
const json = JSON.stringify(inner);
|
|
425
|
+
if (json.length > maxBytes)
|
|
426
|
+
throw new Error("Context snapshot exceeds limit");
|
|
427
|
+
return { contextRef: text(entry.contextRef), context: inner, bytes: json.length };
|
|
428
|
+
}
|
|
429
|
+
export function parseSkillsResultParams(value) {
|
|
430
|
+
const entry = object(value, ["jobId", "eventId", "skillKey", "skillRevision", "status", "decision", "reason", "explanation", "replacementText"]);
|
|
431
|
+
const result = {
|
|
432
|
+
jobId: text(entry.jobId), eventId: text(entry.eventId), skillKey: text(entry.skillKey),
|
|
433
|
+
skillRevision: text(entry.skillRevision), status: choice(entry.status, ["done", "unavailable"]),
|
|
434
|
+
};
|
|
435
|
+
if (result.status === "done") {
|
|
436
|
+
result.decision = choice(entry.decision, ["clear", "suggest", "abstain"]);
|
|
437
|
+
if (entry.explanation !== undefined)
|
|
438
|
+
result.explanation = text(entry.explanation, 4096);
|
|
439
|
+
if (entry.replacementText !== undefined) {
|
|
440
|
+
if (result.decision !== "suggest")
|
|
441
|
+
throw new Error("Only suggestions can replace text");
|
|
442
|
+
if (typeof entry.replacementText !== "string" || entry.replacementText.length > 32_768)
|
|
443
|
+
throw new Error("Invalid replacement text");
|
|
444
|
+
result.replacementText = entry.replacementText;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
else {
|
|
448
|
+
if (entry.decision !== undefined || entry.explanation !== undefined || entry.replacementText !== undefined) {
|
|
449
|
+
throw new Error("Unavailable results carry no decision");
|
|
450
|
+
}
|
|
451
|
+
result.reason = text(entry.reason, 4096);
|
|
452
|
+
}
|
|
453
|
+
return result;
|
|
454
|
+
}
|
|
455
|
+
/** Parse semantic output only. The caller must bind it to the immutable job identity. */
|
|
456
|
+
export function parseReviewResult(value) {
|
|
457
|
+
const entry = object(value, ["jobId", "eventId", "skillKey", "skillRevision", "decision", "explanation", "replacementText"]);
|
|
458
|
+
const result = {
|
|
459
|
+
jobId: text(entry.jobId), eventId: text(entry.eventId), skillKey: text(entry.skillKey),
|
|
460
|
+
skillRevision: text(entry.skillRevision), decision: choice(entry.decision, ["clear", "suggest", "abstain"]),
|
|
461
|
+
};
|
|
462
|
+
if (entry.explanation !== undefined)
|
|
463
|
+
result.explanation = text(entry.explanation, 4096);
|
|
464
|
+
if (entry.replacementText !== undefined) {
|
|
465
|
+
if (result.decision !== "suggest")
|
|
466
|
+
throw new Error("Only suggestions can replace text");
|
|
467
|
+
if (typeof entry.replacementText !== "string" || entry.replacementText.length > 32_768)
|
|
468
|
+
throw new Error("Invalid replacement text");
|
|
469
|
+
result.replacementText = entry.replacementText;
|
|
470
|
+
}
|
|
471
|
+
return result;
|
|
472
|
+
}
|
|
473
|
+
//# sourceMappingURL=skills.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { AgentApplication } from "./integration.js";
|
|
2
|
+
/**
|
|
3
|
+
* True when a client advertising `caps` serves `toolName`.
|
|
4
|
+
* Mirrors the provider's capability → schema selection:
|
|
5
|
+
* - browser_* / pi_* (control) → "browser"
|
|
6
|
+
* - mail mutations → "mailModify"
|
|
7
|
+
* - compose_* → "compose"
|
|
8
|
+
* - contacts_* → "contacts"
|
|
9
|
+
* - mail_* (incl. attachments) → "mail" or "attachments"
|
|
10
|
+
*/
|
|
11
|
+
export declare function capabilitiesProvideTool(caps: readonly string[], toolName: string): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* The application that serves a tool (undefined for host-side tools such
|
|
14
|
+
* as the `javascript` REPL). Used for "needs <App>" errors and per-app
|
|
15
|
+
* caller approval.
|
|
16
|
+
*/
|
|
17
|
+
export declare function applicationForTool(toolName: string): AgentApplication | undefined;
|
|
18
|
+
//# sourceMappingURL=tool-capabilities.d.ts.map
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool → capability mapping for cross-application routing
|
|
3
|
+
* (THUNDERBIRD-PLAN.md §29).
|
|
4
|
+
*
|
|
5
|
+
* The broker routes a tool call to the connected client whose advertised
|
|
6
|
+
* capabilities provide the tool. The rule must stay identical to the
|
|
7
|
+
* host's tool-surface selection (CapabilityToolProvider.createTools), so
|
|
8
|
+
* every tool a client could be shown is a tool it can be routed.
|
|
9
|
+
*/
|
|
10
|
+
import { isBrowserTool } from "./browser-tools.js";
|
|
11
|
+
import { isControlTool } from "./control-tools.js";
|
|
12
|
+
import { isMailTool } from "./mail-tools.js";
|
|
13
|
+
import { isComposeTool } from "./compose-tools.js";
|
|
14
|
+
import { isMailMutationTool } from "./mail-mutation-tools.js";
|
|
15
|
+
import { isContactsTool } from "./contacts-tools.js";
|
|
16
|
+
/**
|
|
17
|
+
* True when a client advertising `caps` serves `toolName`.
|
|
18
|
+
* Mirrors the provider's capability → schema selection:
|
|
19
|
+
* - browser_* / pi_* (control) → "browser"
|
|
20
|
+
* - mail mutations → "mailModify"
|
|
21
|
+
* - compose_* → "compose"
|
|
22
|
+
* - contacts_* → "contacts"
|
|
23
|
+
* - mail_* (incl. attachments) → "mail" or "attachments"
|
|
24
|
+
*/
|
|
25
|
+
export function capabilitiesProvideTool(caps, toolName) {
|
|
26
|
+
const has = (c) => caps.includes(c);
|
|
27
|
+
if (isBrowserTool(toolName) || isControlTool(toolName))
|
|
28
|
+
return has("browser");
|
|
29
|
+
if (isMailMutationTool(toolName))
|
|
30
|
+
return has("mailModify");
|
|
31
|
+
if (isComposeTool(toolName))
|
|
32
|
+
return has("compose");
|
|
33
|
+
if (isContactsTool(toolName))
|
|
34
|
+
return has("contacts");
|
|
35
|
+
if (isMailTool(toolName))
|
|
36
|
+
return has("mail") || has("attachments");
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The application that serves a tool (undefined for host-side tools such
|
|
41
|
+
* as the `javascript` REPL). Used for "needs <App>" errors and per-app
|
|
42
|
+
* caller approval.
|
|
43
|
+
*/
|
|
44
|
+
export function applicationForTool(toolName) {
|
|
45
|
+
if (isBrowserTool(toolName) || isControlTool(toolName))
|
|
46
|
+
return "firefox";
|
|
47
|
+
if (isMailMutationTool(toolName) || isComposeTool(toolName) || isContactsTool(toolName) || isMailTool(toolName)) {
|
|
48
|
+
return "thunderbird";
|
|
49
|
+
}
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=tool-capabilities.js.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal transport contract shared by the host and the add-on.
|
|
3
|
+
* Full transports (stdio-framed on the host, runtime-port on the add-on)
|
|
4
|
+
* implement this interface; higher layers only depend on it.
|
|
5
|
+
*/
|
|
6
|
+
import type { JsonRpcErrorObject } from "./jsonrpc.js";
|
|
7
|
+
export interface AcpTransportLike {
|
|
8
|
+
/** Send a request and wait for its response. Rejects with a JsonRpcErrorObject on peer errors. */
|
|
9
|
+
request<T = unknown>(method: string, params?: unknown, timeoutMs?: number): Promise<T>;
|
|
10
|
+
/** Send a notification (no response expected). */
|
|
11
|
+
notify(method: string, params?: unknown): void;
|
|
12
|
+
}
|
|
13
|
+
/** True when a rejected request value is a structured JSON-RPC error object. */
|
|
14
|
+
export declare function isJsonRpcErrorObject(value: unknown): value is JsonRpcErrorObject;
|
|
15
|
+
/** True for any structured error object (with or without the jsonrpc marker). */
|
|
16
|
+
export declare function isStructuredErrorObject(value: unknown): value is JsonRpcErrorObject;
|
|
17
|
+
//# sourceMappingURL=transport.d.ts.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** True when a rejected request value is a structured JSON-RPC error object. */
|
|
2
|
+
export function isJsonRpcErrorObject(value) {
|
|
3
|
+
if (typeof value !== "object" || value === null)
|
|
4
|
+
return false;
|
|
5
|
+
const v = value;
|
|
6
|
+
// The `jsonrpc` marker is present when a full response object is passed;
|
|
7
|
+
// bare error objects ({ code, message, data }) are equally valid.
|
|
8
|
+
return v.jsonrpc === "2.0" && typeof v.code === "number" && typeof v.message === "string";
|
|
9
|
+
}
|
|
10
|
+
/** True for any structured error object (with or without the jsonrpc marker). */
|
|
11
|
+
export function isStructuredErrorObject(value) {
|
|
12
|
+
if (typeof value !== "object" || value === null)
|
|
13
|
+
return false;
|
|
14
|
+
const v = value;
|
|
15
|
+
return typeof v.code === "number" && typeof v.message === "string";
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=transport.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pi-browser/protocol",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Shared protocol types for Pi Browser: ACP surface, integration metadata, browser tool schemas, structured errors",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,71 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "trusted-user-agent",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "trusted-user-agent",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Trusted user agents for AI agents: Firefox and Thunderbird tools for any MCP client (Claude Code, Codex, Cursor, Gemini CLI, VS Code, pi), with per-app approval. Native Messaging host com.matbee.agent + ACP adapter for pi.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"pi-package",
|
|
8
|
+
"mcp",
|
|
9
|
+
"firefox",
|
|
10
|
+
"thunderbird",
|
|
11
|
+
"native-messaging",
|
|
12
|
+
"acp"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/matbeedotcom/pi-agent-firefox.git",
|
|
18
|
+
"directory": "packages/pi-agent"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/matbeedotcom/pi-agent-firefox#readme",
|
|
21
|
+
"bin": {
|
|
22
|
+
"trusted-ua": "dist/installer/cli.js"
|
|
23
|
+
},
|
|
24
|
+
"main": "./dist/native-host/main.js",
|
|
25
|
+
"pi": {
|
|
26
|
+
"extensions": [
|
|
27
|
+
"extensions"
|
|
28
|
+
]
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"!dist/**/*.map",
|
|
33
|
+
"!dist/**/*.d.ts",
|
|
34
|
+
"!dist/**/*.d.ts.map",
|
|
35
|
+
"extensions",
|
|
36
|
+
"skills",
|
|
37
|
+
"native/pi-agent-host",
|
|
38
|
+
"native/pi-browser-host",
|
|
39
|
+
"native/pi-browser-host.cmd"
|
|
40
|
+
],
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=22"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsc -p tsconfig.json",
|
|
49
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
50
|
+
"test": "tsc -p tsconfig.tests.json && node --test \"dist-tests/test/**/*.test.js\"",
|
|
51
|
+
"prepack": "node scripts/bundle-protocol.mjs copy",
|
|
52
|
+
"postpack": "node scripts/bundle-protocol.mjs clean"
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"@agentclientprotocol/sdk": "^1.4.0",
|
|
56
|
+
"@earendil-works/pi-coding-agent": "^1.5.2",
|
|
57
|
+
"@pi-browser/protocol": "0.3.0",
|
|
58
|
+
"typebox": "^1.1.32",
|
|
59
|
+
"zod": "^4.0.0"
|
|
60
|
+
},
|
|
61
|
+
"peerDependencies": {
|
|
62
|
+
"@earendil-works/pi-coding-agent": ">=1.5.0"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@types/node": "^22.10.0",
|
|
66
|
+
"typescript": "^5.6.0"
|
|
67
|
+
},
|
|
68
|
+
"bundleDependencies": [
|
|
69
|
+
"@pi-browser/protocol"
|
|
70
|
+
]
|
|
71
|
+
}
|