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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mathieu Gosbee
|
|
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.
|
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ACP agent: implements the ACP agent-side methods over the transport
|
|
3
|
+
* (PRODUCT.md §5, §17–24).
|
|
4
|
+
*
|
|
5
|
+
* ACP owns agent session semantics: one Native Messaging connection carries
|
|
6
|
+
* one ACP connection with many independent sessions. The agent maps each
|
|
7
|
+
* ACP session to one Pi backend session and streams `session/update`
|
|
8
|
+
* notifications back to the client.
|
|
9
|
+
*/
|
|
10
|
+
import { AGENT_METHODS, CLIENT_METHODS, PI_AGENT_META, PI_BROWSER_ERROR, PiBrowserProtocolError, PI_BROWSER_META, X_PI_BROWSER, JSONRPC_ERROR, PROTOCOL_VERSION, toErrorObject, parseAgentHello, } from "@pi-browser/protocol";
|
|
11
|
+
import { buildConfigOptions, CONFIG_ID_MODEL, CONFIG_ID_THINKING } from "./config-options.js";
|
|
12
|
+
import { isNeutralCwd, TaskWorkspace } from "../workspace.js";
|
|
13
|
+
import { touchClientHeartbeat } from "../client-heartbeat.js";
|
|
14
|
+
function toolKindFor(toolName) {
|
|
15
|
+
if (toolName.startsWith("browser_get") || toolName === "browser_wait_for" || toolName === "browser_screenshot") {
|
|
16
|
+
return "fetch";
|
|
17
|
+
}
|
|
18
|
+
if (toolName.startsWith("browser_"))
|
|
19
|
+
return "other";
|
|
20
|
+
if (toolName === "read" || toolName === "grep" || toolName === "find" || toolName === "ls")
|
|
21
|
+
return "read";
|
|
22
|
+
if (toolName === "edit" || toolName === "write")
|
|
23
|
+
return "edit";
|
|
24
|
+
if (toolName === "bash" || toolName === "powershell")
|
|
25
|
+
return "execute";
|
|
26
|
+
return "other";
|
|
27
|
+
}
|
|
28
|
+
/** Preserve every text/image result, including mixed-output JavaScript cells. */
|
|
29
|
+
function toToolCallContent(result) {
|
|
30
|
+
const r = result;
|
|
31
|
+
const blocks = [];
|
|
32
|
+
for (const c of Array.isArray(r?.content) ? r.content : []) {
|
|
33
|
+
if (c?.type === "text" && typeof c.text === "string") {
|
|
34
|
+
blocks.push({ type: "content", content: { type: "text", text: c.text } });
|
|
35
|
+
}
|
|
36
|
+
else if (c?.type === "image" && typeof c.data === "string" && typeof c.mimeType === "string") {
|
|
37
|
+
blocks.push({ type: "content", content: { type: "image", data: c.data, mimeType: c.mimeType } });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return blocks.length ? blocks : [{ type: "content", content: { type: "text", text: result === undefined ? "" : JSON.stringify(result) } }];
|
|
41
|
+
}
|
|
42
|
+
/** Delay between the first initialize reply and the workspace prewarm (lets the add-on's startup traffic finish first). */
|
|
43
|
+
const PREWARM_DELAY_MS = 2_000;
|
|
44
|
+
export class AcpAgent {
|
|
45
|
+
opts;
|
|
46
|
+
sessions = new Map();
|
|
47
|
+
/** Name of the connected client (set on initialize) — for the add-on heartbeat. */
|
|
48
|
+
clientIdentityName;
|
|
49
|
+
/**
|
|
50
|
+
* Application + capabilities from the pi.agent.hello handshake (THUNDERBIRD-PLAN.md
|
|
51
|
+
* §24). Legacy clients that never send a hello default to a browser-only
|
|
52
|
+
* Firefox so existing installations keep working unchanged.
|
|
53
|
+
*/
|
|
54
|
+
clientApplication = "firefox";
|
|
55
|
+
clientCapabilities = ["browser"];
|
|
56
|
+
/** Per-task filesystem scratch (one directory per session). */
|
|
57
|
+
workspaces;
|
|
58
|
+
hasCapability(cap) {
|
|
59
|
+
return this.clientCapabilities.includes(cap);
|
|
60
|
+
}
|
|
61
|
+
constructor(opts) {
|
|
62
|
+
this.opts = opts;
|
|
63
|
+
this.workspaces = new TaskWorkspace({ root: opts.workspaceRoot });
|
|
64
|
+
opts.transport.onRequest = (method, params, id) => {
|
|
65
|
+
void this.handleRequest(method, params, id);
|
|
66
|
+
};
|
|
67
|
+
opts.transport.onNotification = (method, params) => {
|
|
68
|
+
void this.handleNotification(method, params);
|
|
69
|
+
};
|
|
70
|
+
// Incremental tool progress (mail_search batches) → ACP tool_call_update.
|
|
71
|
+
// The final tool response still completes the tool call, so clients that
|
|
72
|
+
// ignore updates keep working.
|
|
73
|
+
opts.provider.onToolUpdate = (sessionId, toolCallId, update) => {
|
|
74
|
+
this.emitToolUpdate(sessionId, toolCallId, update);
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** Map one validated tool update onto a session/update notification. */
|
|
78
|
+
emitToolUpdate(sessionId, toolCallId, update) {
|
|
79
|
+
if (!this.sessions.has(sessionId))
|
|
80
|
+
return;
|
|
81
|
+
if (update.kind === "batch") {
|
|
82
|
+
this.sendSessionUpdate(sessionId, {
|
|
83
|
+
sessionUpdate: "tool_call_update",
|
|
84
|
+
toolCallId,
|
|
85
|
+
content: [{ type: "content", content: { type: "text", text: JSON.stringify(update.result) } }],
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
else if (update.kind === "progress") {
|
|
89
|
+
this.sendSessionUpdate(sessionId, {
|
|
90
|
+
sessionUpdate: "tool_call_update",
|
|
91
|
+
toolCallId,
|
|
92
|
+
content: [{ type: "content", content: { type: "text", text: `mail_search: scanned ${update.scanned} messages so far…` } }],
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
// "complete" emits nothing: the final tool response is the authoritative
|
|
96
|
+
// close for ACP clients.
|
|
97
|
+
}
|
|
98
|
+
transport() {
|
|
99
|
+
return this.opts.transport;
|
|
100
|
+
}
|
|
101
|
+
// ------------------------------------------------------------------
|
|
102
|
+
// Wire handlers
|
|
103
|
+
// ------------------------------------------------------------------
|
|
104
|
+
async handleRequest(method, params, id) {
|
|
105
|
+
try {
|
|
106
|
+
switch (method) {
|
|
107
|
+
case AGENT_METHODS.initialize:
|
|
108
|
+
this.transport().respond(id, this.initialize(params));
|
|
109
|
+
return;
|
|
110
|
+
case AGENT_METHODS.session_new:
|
|
111
|
+
this.transport().respond(id, await this.sessionNew(params));
|
|
112
|
+
return;
|
|
113
|
+
case AGENT_METHODS.session_list:
|
|
114
|
+
this.transport().respond(id, await this.sessionList(params));
|
|
115
|
+
return;
|
|
116
|
+
case AGENT_METHODS.session_resume:
|
|
117
|
+
this.transport().respond(id, await this.sessionResume(params));
|
|
118
|
+
return;
|
|
119
|
+
case AGENT_METHODS.session_load:
|
|
120
|
+
this.transport().respond(id, await this.sessionLoad(params));
|
|
121
|
+
return;
|
|
122
|
+
case AGENT_METHODS.session_prompt:
|
|
123
|
+
this.transport().respond(id, await this.sessionPrompt(params));
|
|
124
|
+
return;
|
|
125
|
+
case AGENT_METHODS.session_cancel:
|
|
126
|
+
this.sessionCancel(params);
|
|
127
|
+
this.transport().respond(id, {});
|
|
128
|
+
return;
|
|
129
|
+
case AGENT_METHODS.session_close:
|
|
130
|
+
this.sessionClose(params);
|
|
131
|
+
this.transport().respond(id, {});
|
|
132
|
+
return;
|
|
133
|
+
case AGENT_METHODS.session_set_config_option:
|
|
134
|
+
this.transport().respond(id, await this.sessionSetConfigOption(params));
|
|
135
|
+
return;
|
|
136
|
+
case X_PI_BROWSER.permissions:
|
|
137
|
+
this.transport().respond(id, this.opts.provider.permissionConfig());
|
|
138
|
+
return;
|
|
139
|
+
case X_PI_BROWSER.permission_set: {
|
|
140
|
+
const p = (params ?? {});
|
|
141
|
+
const state = p.state === "allow" || p.state === "deny" || p.state === "ask" ? p.state : "ask";
|
|
142
|
+
if (typeof p.externalAccess === "boolean") {
|
|
143
|
+
this.transport().respond(id, this.opts.provider.setExternalAccess(p.externalAccess));
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const caller = parseCallerRef(p.caller);
|
|
147
|
+
if (caller) {
|
|
148
|
+
this.transport().respond(id, this.opts.provider.setCallerPermission(caller, state));
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const tool = typeof p.tool === "string" ? p.tool : "";
|
|
152
|
+
this.transport().respond(id, this.opts.provider.setToolPermission(tool, state));
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
case X_PI_BROWSER.permission_clear: {
|
|
156
|
+
const p = params;
|
|
157
|
+
const caller = parseCallerRef(p?.caller);
|
|
158
|
+
if (caller) {
|
|
159
|
+
this.opts.provider.setCallerPermission(caller, "ask");
|
|
160
|
+
this.transport().respond(id, { cleared: [`${caller.application}:${caller.name}`] });
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const tool = p && typeof p.tool === "string" ? p.tool : undefined;
|
|
164
|
+
this.transport().respond(id, this.opts.provider.clearToolPermissions(tool));
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
case X_PI_BROWSER.ping: {
|
|
168
|
+
const backendReady = await this.opts.backend.ready.then(() => true, () => false);
|
|
169
|
+
// Refresh the add-on heartbeat (no-op for non-add-on clients), so
|
|
170
|
+
// /pi-browser status|doctor can report add-on presence.
|
|
171
|
+
touchClientHeartbeat(this.clientIdentityName);
|
|
172
|
+
this.transport().respond(id, { pong: true, meta: PI_BROWSER_META, backendReady });
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
default: {
|
|
176
|
+
// Broker-level extension surface (skills): the hook claims a
|
|
177
|
+
// request by resolving to true and answers it itself.
|
|
178
|
+
const handled = this.opts.extendedRequest?.(method, params, id);
|
|
179
|
+
if (handled !== undefined && (await handled) === true)
|
|
180
|
+
return;
|
|
181
|
+
this.transport().respondError(id, {
|
|
182
|
+
code: JSONRPC_ERROR.METHOD_NOT_FOUND,
|
|
183
|
+
message: `unknown method: ${method}`,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
this.respondFailure(id, err);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
async handleNotification(method, params) {
|
|
193
|
+
if (method === X_PI_BROWSER.notify) {
|
|
194
|
+
this.opts.provider.handleNotify(params);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (method === X_PI_BROWSER.tool_update) {
|
|
198
|
+
// This agent's transport IS the registered client connection (one
|
|
199
|
+
// agent per client), so source-client identity is structural here;
|
|
200
|
+
// the provider validates session, call identity, tool, and sequence.
|
|
201
|
+
this.opts.provider.handleToolUpdate(params, this.opts.clientId);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
// ACP notifications the agent does not use are ignored by design.
|
|
205
|
+
this.opts.log.debug(`ignoring notification ${method}`);
|
|
206
|
+
}
|
|
207
|
+
respondFailure(id, err) {
|
|
208
|
+
if (err instanceof PiBrowserProtocolError) {
|
|
209
|
+
this.opts.log.warn(`request failed [${err.code}] ${err.message}`);
|
|
210
|
+
this.transport().respondError(id, err.toErrorObject());
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
214
|
+
this.opts.log.error(`request failed: ${message}`, err);
|
|
215
|
+
this.transport().respondError(id, toErrorObject(PI_BROWSER_ERROR.INTERNAL, message));
|
|
216
|
+
}
|
|
217
|
+
// ------------------------------------------------------------------
|
|
218
|
+
// ACP methods
|
|
219
|
+
// ------------------------------------------------------------------
|
|
220
|
+
/**
|
|
221
|
+
* Push x-pi-browser/capabilities_changed (the current union) to this
|
|
222
|
+
* client. The broker calls this for every connected client when the
|
|
223
|
+
* provider set changes, so UIs update their "capabilities:" line.
|
|
224
|
+
*/
|
|
225
|
+
notifyCapabilitiesChanged(capabilities) {
|
|
226
|
+
this.opts.transport.notify(X_PI_BROWSER.capabilities_changed, { capabilities });
|
|
227
|
+
}
|
|
228
|
+
initialize(req) {
|
|
229
|
+
if (req.protocolVersion !== PROTOCOL_VERSION) {
|
|
230
|
+
throw new PiBrowserProtocolError(PI_BROWSER_ERROR.PROTOCOL_VERSION_MISMATCH, `unsupported ACP protocol version: ${req.protocolVersion} (agent supports ${PROTOCOL_VERSION})`);
|
|
231
|
+
}
|
|
232
|
+
const hello = parseAgentHello(req);
|
|
233
|
+
this.clientApplication = hello?.client.application ?? "firefox";
|
|
234
|
+
this.clientCapabilities = hello ? hello.capabilities : ["browser"];
|
|
235
|
+
this.opts.log.info(`initialize: client=${req.clientInfo?.name ?? "?"} v${req.clientInfo?.version ?? "?"} ` +
|
|
236
|
+
`application=${this.clientApplication} capabilities=[${this.clientCapabilities.join(",")}] ` +
|
|
237
|
+
`proto=${req.protocolVersion}`);
|
|
238
|
+
// Register in the broker provider registry (plan §28): this client's
|
|
239
|
+
// capabilities become routable to/from every session.
|
|
240
|
+
if (this.opts.registry) {
|
|
241
|
+
this.opts.registry.register({
|
|
242
|
+
clientId: this.opts.clientId ?? "stdio",
|
|
243
|
+
application: this.clientApplication,
|
|
244
|
+
capabilities: this.clientCapabilities,
|
|
245
|
+
transport: this.opts.transport,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
// Record add-on presence (no-op for non-add-on clients like test harnesses).
|
|
249
|
+
this.clientIdentityName = req.clientInfo?.name;
|
|
250
|
+
touchClientHeartbeat(req.clientInfo?.name, req.clientInfo?.version, this.clientApplication);
|
|
251
|
+
// Prewarm one fresh workspace shortly AFTER this reply goes out: the
|
|
252
|
+
// build blocks the event loop for several seconds, so it must not sit
|
|
253
|
+
// between the add-on and its initialize answer. Once per host (the
|
|
254
|
+
// backend ignores repeats); only the real pi backend implements it.
|
|
255
|
+
if (this.opts.backend.prewarmWorkspace) {
|
|
256
|
+
setTimeout(() => this.opts.backend.prewarmWorkspace?.(() => this.workspaces.create()), PREWARM_DELAY_MS).unref?.();
|
|
257
|
+
}
|
|
258
|
+
const piAgentMeta = {
|
|
259
|
+
...PI_AGENT_META,
|
|
260
|
+
application: this.clientApplication,
|
|
261
|
+
capabilities: this.clientCapabilities,
|
|
262
|
+
// Session tool surface (plan §29): the union of every connected
|
|
263
|
+
// client, so the add-on UI can show peer-app capabilities (e.g.
|
|
264
|
+
// browsing from the mail client). Without a registry (standalone
|
|
265
|
+
// single-app mode) the union is just this client's own caps.
|
|
266
|
+
connectedCapabilities: this.opts.registry
|
|
267
|
+
? this.opts.registry.allCapabilities()
|
|
268
|
+
: this.clientCapabilities,
|
|
269
|
+
};
|
|
270
|
+
return {
|
|
271
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
272
|
+
agentCapabilities: {
|
|
273
|
+
loadSession: true,
|
|
274
|
+
promptCapabilities: { image: true },
|
|
275
|
+
sessionCapabilities: { list: {}, resume: {}, close: {} },
|
|
276
|
+
mcpCapabilities: { acp: true },
|
|
277
|
+
},
|
|
278
|
+
agentInfo: this.opts.agentInfo,
|
|
279
|
+
_meta: { piBrowser: { ...PI_BROWSER_META, workspaceRoot: this.workspaces.root }, piAgent: piAgentMeta },
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
async sessionNew(req) {
|
|
283
|
+
// A session with no meaningful requested cwd (the add-on's empty-field
|
|
284
|
+
// fallback is "/") gets a fresh per-task workspace as its cwd, so the
|
|
285
|
+
// model's file tools and the REPL share one task-scoped scratch.
|
|
286
|
+
// A prewarmed workspace (services already built) starts near-instantly.
|
|
287
|
+
const cwd = isNeutralCwd(req.cwd)
|
|
288
|
+
? (this.opts.backend.takePrewarmedWorkspace?.() ?? (await this.workspaces.create()))
|
|
289
|
+
: req.cwd;
|
|
290
|
+
const { state } = await this.openBackendSession((tools) => this.opts.backend.createSession({ cwd, customTools: tools }), req.mcpServers);
|
|
291
|
+
this.opts.log.info(`session/new -> ${state.id} cwd=${state.cwd}`);
|
|
292
|
+
return {
|
|
293
|
+
sessionId: state.id,
|
|
294
|
+
configOptions: state.configOptions,
|
|
295
|
+
_meta: { piBrowser: { workspace: state.cwd } },
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
async sessionResume(req) {
|
|
299
|
+
const { state } = await this.openBackendSession(
|
|
300
|
+
// Resumed sessions must keep the browser/`javascript` tool surface
|
|
301
|
+
// (same as sessionNew) — dropping customTools here silently removes
|
|
302
|
+
// every provider tool from the model's tool list on resume.
|
|
303
|
+
(tools) => this.opts.backend.openSession({ sessionId: req.sessionId, customTools: tools }), req.mcpServers, req.cwd);
|
|
304
|
+
this.opts.log.info(`session/resume -> ${state.id}`);
|
|
305
|
+
return { configOptions: state.configOptions };
|
|
306
|
+
}
|
|
307
|
+
async sessionLoad(req) {
|
|
308
|
+
const existing = this.sessions.get(req.sessionId);
|
|
309
|
+
if (existing) {
|
|
310
|
+
for (const update of await this.replayHistory(existing))
|
|
311
|
+
this.sendSessionUpdate(existing.id, update);
|
|
312
|
+
this.opts.log.info(`session/load -> ${existing.id} (already open; replayed history)`);
|
|
313
|
+
return { configOptions: existing.configOptions };
|
|
314
|
+
}
|
|
315
|
+
const { state } = await this.openBackendSession(
|
|
316
|
+
// Loaded sessions must keep the browser/`javascript` tool surface
|
|
317
|
+
// (same as sessionNew) — see sessionResume.
|
|
318
|
+
(tools) => this.opts.backend.openSession({ sessionId: req.sessionId, customTools: tools }), req.mcpServers, req.cwd);
|
|
319
|
+
// Replay history as session/update notifications (ACP session/load).
|
|
320
|
+
for (const update of await this.replayHistory(state)) {
|
|
321
|
+
this.sendSessionUpdate(state.id, update);
|
|
322
|
+
}
|
|
323
|
+
this.opts.log.info(`session/load -> ${state.id}`);
|
|
324
|
+
return { configOptions: state.configOptions };
|
|
325
|
+
}
|
|
326
|
+
async sessionList(req) {
|
|
327
|
+
const sessions = await this.opts.backend.listSessions(req.cwd ?? undefined);
|
|
328
|
+
return {
|
|
329
|
+
sessions: sessions.map(toSessionInfo),
|
|
330
|
+
nextCursor: null,
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
async sessionPrompt(req) {
|
|
334
|
+
const st = this.sessions.get(req.sessionId);
|
|
335
|
+
if (!st)
|
|
336
|
+
throw new PiBrowserProtocolError(PI_BROWSER_ERROR.SESSION_NOT_FOUND, `unknown session: ${req.sessionId}`);
|
|
337
|
+
// Bridge fallback target: bridge tool calls without an explicit session
|
|
338
|
+
// id route to the session the user is actively driving.
|
|
339
|
+
this.opts.bridge?.touchSession(req.sessionId);
|
|
340
|
+
if (st.session.isStreaming) {
|
|
341
|
+
throw new PiBrowserProtocolError(PI_BROWSER_ERROR.SESSION_BUSY, `session ${req.sessionId} is busy`);
|
|
342
|
+
}
|
|
343
|
+
const { text, images } = extractPromptContent(req.prompt);
|
|
344
|
+
if (!text && images.length === 0) {
|
|
345
|
+
throw new PiBrowserProtocolError(PI_BROWSER_ERROR.INTERNAL, "empty prompt");
|
|
346
|
+
}
|
|
347
|
+
this.opts.log.info(`session/prompt ${req.sessionId} (${text.length} chars, ${images.length} images)`);
|
|
348
|
+
this.opts.log.debug(`session/prompt ${req.sessionId} text=${JSON.stringify(text)}`);
|
|
349
|
+
const result = await st.session.prompt(text, images.length > 0 ? images : undefined);
|
|
350
|
+
return { stopReason: result.aborted ? "cancelled" : "end_turn" };
|
|
351
|
+
}
|
|
352
|
+
sessionCancel(req) {
|
|
353
|
+
const st = this.sessions.get(req.sessionId);
|
|
354
|
+
if (!st) {
|
|
355
|
+
this.opts.log.warn(`cancel for unknown session ${req.sessionId}`);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
this.opts.log.info(`session/cancel ${req.sessionId}`);
|
|
359
|
+
void st.session.abort();
|
|
360
|
+
}
|
|
361
|
+
sessionClose(req) {
|
|
362
|
+
this.disposeSession(req.sessionId);
|
|
363
|
+
this.opts.bridge?.closeSession(req.sessionId);
|
|
364
|
+
this.opts.log.info(`session/close ${req.sessionId}`);
|
|
365
|
+
}
|
|
366
|
+
async sessionSetConfigOption(req) {
|
|
367
|
+
const st = this.sessions.get(req.sessionId);
|
|
368
|
+
if (!st)
|
|
369
|
+
throw new PiBrowserProtocolError(PI_BROWSER_ERROR.SESSION_NOT_FOUND, `unknown session: ${req.sessionId}`);
|
|
370
|
+
const value = req.value;
|
|
371
|
+
if (req.configId === CONFIG_ID_MODEL) {
|
|
372
|
+
if (typeof value !== "string" || !value) {
|
|
373
|
+
throw new PiBrowserProtocolError("INTERNAL", "model option requires a non-empty value id");
|
|
374
|
+
}
|
|
375
|
+
await st.session.setModel(value);
|
|
376
|
+
}
|
|
377
|
+
else if (req.configId === CONFIG_ID_THINKING) {
|
|
378
|
+
if (typeof value !== "string") {
|
|
379
|
+
throw new PiBrowserProtocolError("INTERNAL", "thinking option requires a string value");
|
|
380
|
+
}
|
|
381
|
+
st.session.setThinkingLevel(value);
|
|
382
|
+
}
|
|
383
|
+
else {
|
|
384
|
+
throw new PiBrowserProtocolError(PI_BROWSER_ERROR.ACP_CAPABILITY_UNSUPPORTED, `unknown config option: ${req.configId}`);
|
|
385
|
+
}
|
|
386
|
+
st.configOptions = await this.currentConfigOptions(st);
|
|
387
|
+
return { configOptions: st.configOptions };
|
|
388
|
+
}
|
|
389
|
+
// ------------------------------------------------------------------
|
|
390
|
+
// Helpers
|
|
391
|
+
// ------------------------------------------------------------------
|
|
392
|
+
/**
|
|
393
|
+
* Open a backend session and register all ACP-side state. Browser tools
|
|
394
|
+
* are created with a lazily-bound session id because the backend assigns
|
|
395
|
+
* the Pi session id at creation time.
|
|
396
|
+
*/
|
|
397
|
+
async openBackendSession(open, mcpServers, explicitCwd) {
|
|
398
|
+
const browserMode = this.opts.provider.selectMode(mcpServers);
|
|
399
|
+
const mcpServer = mcpServers?.find((s) => s.type === "acp");
|
|
400
|
+
const mcpServerId = mcpServer?.serverId;
|
|
401
|
+
// Tools bind to the session id at execute time (id assigned below).
|
|
402
|
+
const idRef = {};
|
|
403
|
+
// Register the tool surface the client advertised in its hello
|
|
404
|
+
// (plan §19): "browser" → browser tools, "mail"/"attachments" → the
|
|
405
|
+
// read-only mail tools. Legacy clients (no hello) default to browser.
|
|
406
|
+
// In broker mode (registry present) the surface is instead the union of
|
|
407
|
+
// ALL connected clients' capabilities (plan §29) and each call is
|
|
408
|
+
// routed to the client that provides the tool.
|
|
409
|
+
const tools = this.opts.provider.createTools(idRef, browserMode, mcpServerId, this.clientCapabilities, this.opts.registry ? (this.opts.clientId ?? "stdio") : undefined, this.clientApplication);
|
|
410
|
+
const session = await open(tools);
|
|
411
|
+
idRef.id = session.sessionId;
|
|
412
|
+
if (this.sessions.has(session.sessionId)) {
|
|
413
|
+
session.dispose();
|
|
414
|
+
this.opts.provider.disposeSession(session.sessionId);
|
|
415
|
+
throw new PiBrowserProtocolError(PI_BROWSER_ERROR.SESSION_BUSY, `session ${session.sessionId} is already open`);
|
|
416
|
+
}
|
|
417
|
+
let state = {
|
|
418
|
+
id: session.sessionId,
|
|
419
|
+
cwd: explicitCwd ?? session.cwd,
|
|
420
|
+
session,
|
|
421
|
+
unsubscribe: () => undefined,
|
|
422
|
+
configOptions: [],
|
|
423
|
+
browserMode,
|
|
424
|
+
...(mcpServerId ? { mcpServerId } : {}),
|
|
425
|
+
};
|
|
426
|
+
// The session cwd is the task's scratch: point the `javascript` REPL at it
|
|
427
|
+
// so cell artifacts, checkpoints and images land with the files the model
|
|
428
|
+
// writes. The REPL falls back to its own per-session dir if unbound.
|
|
429
|
+
this.opts.provider.bindWorkspace(session.sessionId, state.cwd);
|
|
430
|
+
// Stream backend events to the client as session/update notifications.
|
|
431
|
+
state.unsubscribe = session.subscribe((event) => {
|
|
432
|
+
for (const update of this.mapEvent(state.id, event)) {
|
|
433
|
+
this.sendSessionUpdate(state.id, update);
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
const models = await this.opts.backend.listModels().catch(() => []);
|
|
437
|
+
state.configOptions = buildConfigOptions({
|
|
438
|
+
models,
|
|
439
|
+
currentModel: session.modelValueId,
|
|
440
|
+
currentThinking: session.thinkingLevel,
|
|
441
|
+
});
|
|
442
|
+
this.sessions.set(session.sessionId, state);
|
|
443
|
+
// Advertise the session on the in-process tool bridge so subagent
|
|
444
|
+
// sessions in this process can route browser tool calls back through
|
|
445
|
+
// the provider with this session's routing context.
|
|
446
|
+
this.opts.bridge?.registerSession(session.sessionId, {
|
|
447
|
+
...(this.opts.clientId !== undefined ? { ownerClientId: this.opts.clientId } : {}),
|
|
448
|
+
ownerApplication: this.clientApplication,
|
|
449
|
+
mode: state.browserMode,
|
|
450
|
+
...(state.mcpServerId ? { mcpServerId: state.mcpServerId } : {}),
|
|
451
|
+
});
|
|
452
|
+
this.opts.bridge?.touchSession(session.sessionId);
|
|
453
|
+
return { session, state };
|
|
454
|
+
}
|
|
455
|
+
async currentConfigOptions(st) {
|
|
456
|
+
const models = await this.opts.backend.listModels().catch(() => []);
|
|
457
|
+
return buildConfigOptions({
|
|
458
|
+
models,
|
|
459
|
+
currentModel: st.session.modelValueId,
|
|
460
|
+
currentThinking: st.session.thinkingLevel,
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
sendSessionUpdate(sessionId, update) {
|
|
464
|
+
const notification = { sessionId, update };
|
|
465
|
+
this.opts.transport.notify(CLIENT_METHODS.session_update, notification);
|
|
466
|
+
}
|
|
467
|
+
mapEvent(sessionId, event) {
|
|
468
|
+
// Debug-only evidence: omit image payloads and keep normal logs content-free.
|
|
469
|
+
if (event.type === "tool_start") {
|
|
470
|
+
this.opts.log.debug(`session/tool_start ${sessionId} ${event.toolName} ${JSON.stringify(event.args)}`);
|
|
471
|
+
}
|
|
472
|
+
else if (event.type === "tool_end") {
|
|
473
|
+
this.opts.log.debug(`session/tool_end ${sessionId} ${event.toolName} isError=${event.isError} ${JSON.stringify(event.result, (key, value) => key === "data" ? "[omitted]" : value)}`);
|
|
474
|
+
}
|
|
475
|
+
switch (event.type) {
|
|
476
|
+
case "text_delta":
|
|
477
|
+
return [
|
|
478
|
+
{
|
|
479
|
+
sessionUpdate: "agent_message_chunk",
|
|
480
|
+
content: { type: "text", text: event.delta },
|
|
481
|
+
},
|
|
482
|
+
];
|
|
483
|
+
case "thinking_delta":
|
|
484
|
+
return [
|
|
485
|
+
{
|
|
486
|
+
sessionUpdate: "agent_thought_chunk",
|
|
487
|
+
content: { type: "text", text: event.delta },
|
|
488
|
+
},
|
|
489
|
+
];
|
|
490
|
+
case "tool_start":
|
|
491
|
+
return [
|
|
492
|
+
{
|
|
493
|
+
sessionUpdate: "tool_call",
|
|
494
|
+
toolCallId: event.toolCallId,
|
|
495
|
+
title: event.toolName,
|
|
496
|
+
kind: toolKindFor(event.toolName),
|
|
497
|
+
status: "in_progress",
|
|
498
|
+
rawInput: event.args ?? {},
|
|
499
|
+
},
|
|
500
|
+
];
|
|
501
|
+
case "tool_update": {
|
|
502
|
+
const updates = [];
|
|
503
|
+
const partial = event.partial;
|
|
504
|
+
const text = partial?.content
|
|
505
|
+
?.filter((c) => c?.type === "text" && typeof c.text === "string")
|
|
506
|
+
.map((c) => c.text)
|
|
507
|
+
.join("");
|
|
508
|
+
if (text) {
|
|
509
|
+
updates.push({
|
|
510
|
+
sessionUpdate: "tool_call_update",
|
|
511
|
+
toolCallId: event.toolCallId,
|
|
512
|
+
content: [{ type: "content", content: { type: "text", text } }],
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
return updates;
|
|
516
|
+
}
|
|
517
|
+
case "tool_end":
|
|
518
|
+
return [
|
|
519
|
+
{
|
|
520
|
+
sessionUpdate: "tool_call_update",
|
|
521
|
+
toolCallId: event.toolCallId,
|
|
522
|
+
status: event.isError ? "failed" : "completed",
|
|
523
|
+
content: toToolCallContent(event.result),
|
|
524
|
+
rawOutput: event.result,
|
|
525
|
+
},
|
|
526
|
+
];
|
|
527
|
+
default:
|
|
528
|
+
return [];
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
async replayHistory(st) {
|
|
532
|
+
const messages = await st.session.getHistory?.().catch(() => []);
|
|
533
|
+
if (!messages)
|
|
534
|
+
return [];
|
|
535
|
+
const updates = [];
|
|
536
|
+
for (const msg of messages) {
|
|
537
|
+
if (msg.role === "user" || msg.role === "assistant") {
|
|
538
|
+
if (msg.text)
|
|
539
|
+
updates.push(msg.role === "user"
|
|
540
|
+
? { sessionUpdate: "user_message_chunk", content: { type: "text", text: msg.text } }
|
|
541
|
+
: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: msg.text } });
|
|
542
|
+
for (const call of msg.toolCalls ?? []) {
|
|
543
|
+
updates.push({ sessionUpdate: "tool_call", toolCallId: call.toolCallId, title: call.toolName, kind: toolKindFor(call.toolName), status: "in_progress", rawInput: call.input ?? {} });
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
else if (msg.role === "tool") {
|
|
547
|
+
updates.push({ sessionUpdate: "tool_call_update", toolCallId: msg.toolCallId, status: msg.isError ? "failed" : "completed", content: toToolCallContent(msg), rawOutput: msg.content });
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return updates;
|
|
551
|
+
}
|
|
552
|
+
disposeSession(sessionId) {
|
|
553
|
+
const st = this.sessions.get(sessionId);
|
|
554
|
+
if (!st)
|
|
555
|
+
return;
|
|
556
|
+
st.unsubscribe();
|
|
557
|
+
this.sessions.delete(sessionId);
|
|
558
|
+
this.opts.provider.disposeSession(sessionId);
|
|
559
|
+
st.session.dispose();
|
|
560
|
+
}
|
|
561
|
+
/** Tear down every session (host shutdown). */
|
|
562
|
+
shutdown() {
|
|
563
|
+
for (const id of [...this.sessions.keys()])
|
|
564
|
+
this.disposeSession(id);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
function toSessionInfo(s) {
|
|
568
|
+
return {
|
|
569
|
+
sessionId: s.sessionId,
|
|
570
|
+
cwd: s.cwd,
|
|
571
|
+
updatedAt: s.updatedAt,
|
|
572
|
+
title: s.title ?? null,
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
/** Split ACP prompt content blocks into text and image attachments. */
|
|
576
|
+
export function extractPromptContent(prompt) {
|
|
577
|
+
const texts = [];
|
|
578
|
+
const images = [];
|
|
579
|
+
for (const block of prompt) {
|
|
580
|
+
if (block.type === "text")
|
|
581
|
+
texts.push(block.text);
|
|
582
|
+
else if (block.type === "image") {
|
|
583
|
+
const img = block;
|
|
584
|
+
images.push({ data: img.data, mimeType: img.mimeType });
|
|
585
|
+
}
|
|
586
|
+
// resource/resource_link blocks: accepted but not forwarded (MVP).
|
|
587
|
+
}
|
|
588
|
+
return { text: texts.join("\n"), images };
|
|
589
|
+
}
|
|
590
|
+
/** Parse a `caller` ref from x-pi-browser/permission_set|clear params. */
|
|
591
|
+
function parseCallerRef(raw) {
|
|
592
|
+
if (typeof raw !== "object" || raw === null)
|
|
593
|
+
return undefined;
|
|
594
|
+
const r = raw;
|
|
595
|
+
if (r.application !== "firefox" && r.application !== "thunderbird")
|
|
596
|
+
return undefined;
|
|
597
|
+
if (typeof r.name !== "string" || r.name === "")
|
|
598
|
+
return undefined;
|
|
599
|
+
return { application: r.application, name: r.name };
|
|
600
|
+
}
|
|
601
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const CONFIG_ID_MODEL = "model";
|
|
2
|
+
export const CONFIG_ID_THINKING = "thinking";
|
|
3
|
+
export const THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"];
|
|
4
|
+
export function buildConfigOptions(opts) {
|
|
5
|
+
const options = [];
|
|
6
|
+
options.push({
|
|
7
|
+
id: CONFIG_ID_MODEL,
|
|
8
|
+
name: "Model",
|
|
9
|
+
description: "LLM model for this session",
|
|
10
|
+
category: "model",
|
|
11
|
+
type: "select",
|
|
12
|
+
currentValue: opts.currentModel ?? "",
|
|
13
|
+
options: opts.models.map((m) => ({ value: m.valueId, name: m.name })),
|
|
14
|
+
});
|
|
15
|
+
options.push({
|
|
16
|
+
id: CONFIG_ID_THINKING,
|
|
17
|
+
name: "Reasoning",
|
|
18
|
+
description: "Reasoning effort for this session",
|
|
19
|
+
category: "thought_level",
|
|
20
|
+
type: "select",
|
|
21
|
+
currentValue: opts.currentThinking,
|
|
22
|
+
options: THINKING_LEVELS.map((level) => ({ value: level, name: level })),
|
|
23
|
+
});
|
|
24
|
+
return options;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=config-options.js.map
|