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.
Files changed (93) hide show
  1. package/LICENSE +21 -0
  2. package/dist/acp/agent.js +601 -0
  3. package/dist/acp/backend.js +2 -0
  4. package/dist/acp/config-options.js +26 -0
  5. package/dist/acp/mock-backend.js +277 -0
  6. package/dist/acp/sdk-backend.js +495 -0
  7. package/dist/browser/mcp-acp-client.js +81 -0
  8. package/dist/browser/provider.js +1045 -0
  9. package/dist/browser/schemas.js +221 -0
  10. package/dist/capability-registry.js +92 -0
  11. package/dist/client-heartbeat.js +118 -0
  12. package/dist/compose/schemas.js +90 -0
  13. package/dist/contacts/schemas.js +26 -0
  14. package/dist/installer/cli.js +70 -0
  15. package/dist/installer/common.js +93 -0
  16. package/dist/installer/index.js +341 -0
  17. package/dist/installer/mcp-clients.js +555 -0
  18. package/dist/installer/mcp.js +166 -0
  19. package/dist/installer/platforms.js +277 -0
  20. package/dist/logger.js +54 -0
  21. package/dist/mail/schemas.js +98 -0
  22. package/dist/mutation/schemas.js +31 -0
  23. package/dist/native-host/broker-ipc.js +344 -0
  24. package/dist/native-host/framing.js +76 -0
  25. package/dist/native-host/main.js +520 -0
  26. package/dist/native-host/mcp-server.js +322 -0
  27. package/dist/native-host/mcp-stdio-bridge.js +179 -0
  28. package/dist/native-host/mcp-stdio-main.js +13 -0
  29. package/dist/native-host/relay.js +84 -0
  30. package/dist/native-host/skills-startup.js +167 -0
  31. package/dist/native-host/transport.js +248 -0
  32. package/dist/permission-store.js +207 -0
  33. package/dist/repl/fs-sandbox.js +187 -0
  34. package/dist/repl/provider.js +252 -0
  35. package/dist/repl/runtime.js +436 -0
  36. package/dist/repl/types.js +10 -0
  37. package/dist/repl/worker.js +731 -0
  38. package/dist/skills/extension.js +99 -0
  39. package/dist/skills/grants-file.js +145 -0
  40. package/dist/skills/isolated-runner.js +259 -0
  41. package/dist/skills/mock-session-factory.js +49 -0
  42. package/dist/skills/package-roots.js +25 -0
  43. package/dist/skills/pi-bridge.js +8 -0
  44. package/dist/skills/pi-integration.js +110 -0
  45. package/dist/skills/planner.js +128 -0
  46. package/dist/skills/registry.js +104 -0
  47. package/dist/skills/skills-protocol.js +253 -0
  48. package/dist/skills/skills-service.js +195 -0
  49. package/dist/tool-bridge.js +8 -0
  50. package/dist/workspace.js +70 -0
  51. package/extensions/pi-browser-tools.ts +65 -0
  52. package/extensions/pi-browser.ts +60 -0
  53. package/extensions/pi-skills.ts +14 -0
  54. package/native/pi-agent-host +2 -0
  55. package/native/pi-browser-host +5 -0
  56. package/native/pi-browser-host.cmd +3 -0
  57. package/node_modules/@pi-browser/protocol/.bundled-by-trusted-user-agent +0 -0
  58. package/node_modules/@pi-browser/protocol/dist/acp.d.ts +10 -0
  59. package/node_modules/@pi-browser/protocol/dist/acp.js +9 -0
  60. package/node_modules/@pi-browser/protocol/dist/browser-tools.d.ts +33 -0
  61. package/node_modules/@pi-browser/protocol/dist/browser-tools.js +478 -0
  62. package/node_modules/@pi-browser/protocol/dist/callers.d.ts +108 -0
  63. package/node_modules/@pi-browser/protocol/dist/callers.js +56 -0
  64. package/node_modules/@pi-browser/protocol/dist/compose-tools.d.ts +30 -0
  65. package/node_modules/@pi-browser/protocol/dist/compose-tools.js +175 -0
  66. package/node_modules/@pi-browser/protocol/dist/contacts-tools.d.ts +23 -0
  67. package/node_modules/@pi-browser/protocol/dist/contacts-tools.js +63 -0
  68. package/node_modules/@pi-browser/protocol/dist/control-tools.d.ts +32 -0
  69. package/node_modules/@pi-browser/protocol/dist/control-tools.js +201 -0
  70. package/node_modules/@pi-browser/protocol/dist/errors.d.ts +63 -0
  71. package/node_modules/@pi-browser/protocol/dist/errors.js +109 -0
  72. package/node_modules/@pi-browser/protocol/dist/index.d.ts +16 -0
  73. package/node_modules/@pi-browser/protocol/dist/index.js +16 -0
  74. package/node_modules/@pi-browser/protocol/dist/integration.d.ts +409 -0
  75. package/node_modules/@pi-browser/protocol/dist/integration.js +260 -0
  76. package/node_modules/@pi-browser/protocol/dist/jsonrpc.d.ts +33 -0
  77. package/node_modules/@pi-browser/protocol/dist/jsonrpc.js +31 -0
  78. package/node_modules/@pi-browser/protocol/dist/mail-mutation-tools.d.ts +27 -0
  79. package/node_modules/@pi-browser/protocol/dist/mail-mutation-tools.js +88 -0
  80. package/node_modules/@pi-browser/protocol/dist/mail-tools.d.ts +133 -0
  81. package/node_modules/@pi-browser/protocol/dist/mail-tools.js +245 -0
  82. package/node_modules/@pi-browser/protocol/dist/permission.d.ts +113 -0
  83. package/node_modules/@pi-browser/protocol/dist/permission.js +257 -0
  84. package/node_modules/@pi-browser/protocol/dist/skills.d.ts +304 -0
  85. package/node_modules/@pi-browser/protocol/dist/skills.js +473 -0
  86. package/node_modules/@pi-browser/protocol/dist/tool-capabilities.d.ts +18 -0
  87. package/node_modules/@pi-browser/protocol/dist/tool-capabilities.js +52 -0
  88. package/node_modules/@pi-browser/protocol/dist/transport.d.ts +17 -0
  89. package/node_modules/@pi-browser/protocol/dist/transport.js +17 -0
  90. package/node_modules/@pi-browser/protocol/package.json +14 -0
  91. package/package.json +71 -6
  92. package/skills/browser-walk/SKILL.md +98 -0
  93. package/README.md +0 -3
@@ -0,0 +1,277 @@
1
+ /**
2
+ * Deterministic scripted Pi backend (PRODUCT.md §6 seam).
3
+ *
4
+ * Selected with PI_BROWSER_BACKEND=mock. Used by the integration test
5
+ * harness (tests/) and by users who want a token-free smoke of the full
6
+ * ACP surface. Behaves like the real backend through the PiBackend seam:
7
+ * sessions, streaming events, cancellation, history replay, and — when a
8
+ * scripted turn requests it — execution of the session's custom tools
9
+ * (the browser tools), which exercises the x-pi-browser/* and
10
+ * MCP-over-ACP paths end to end.
11
+ */
12
+ import { randomUUID } from "node:crypto";
13
+ import { readFileSync } from "node:fs";
14
+ import { PI_BROWSER_ERROR, PiBrowserProtocolError, codeFromErrorObject, isStructuredErrorObject } from "@pi-browser/protocol";
15
+ export class MockSession {
16
+ sessionId;
17
+ cwd;
18
+ customTools;
19
+ script;
20
+ isStreaming = false;
21
+ modelValueId = "mock/model-a";
22
+ thinkingLevel = "off";
23
+ prompts = [];
24
+ modelChanges = [];
25
+ thinkingChanges = [];
26
+ abortedPromptIndexes = [];
27
+ executedTools = [];
28
+ listeners = new Set();
29
+ pendingResolve;
30
+ /** Aborts the in-flight tool calls when the session is cancelled. */
31
+ abortController;
32
+ isDisposed = false;
33
+ history = [];
34
+ /** Per-prompt script; set from the driver before prompting. */
35
+ nextTurn = {};
36
+ constructor(sessionId, cwd, customTools = [], script = []) {
37
+ this.sessionId = sessionId;
38
+ this.cwd = cwd;
39
+ this.customTools = customTools;
40
+ this.script = script;
41
+ }
42
+ /** External script entry matching this prompt, if any. */
43
+ turnFor(text) {
44
+ if (this.nextTurn.events?.length || this.nextTurn.toolCalls?.length || this.nextTurn.fail) {
45
+ return this.nextTurn;
46
+ }
47
+ for (const entry of this.script) {
48
+ if (entry.match && !text.includes(entry.match))
49
+ continue;
50
+ return { events: entry.events, toolCalls: entry.toolCalls, fail: entry.fail, delayMs: entry.delayMs };
51
+ }
52
+ return this.nextTurn;
53
+ }
54
+ emit(event) {
55
+ for (const l of this.listeners)
56
+ l(event);
57
+ }
58
+ async executeTool(toolCallId, spec) {
59
+ const tool = this.customTools.find((t) => t.name === spec.toolName);
60
+ this.emit({ type: "tool_start", toolCallId, toolName: spec.toolName, args: spec.args ?? {} });
61
+ try {
62
+ if (!tool) {
63
+ const err = new PiBrowserProtocolError(PI_BROWSER_ERROR.MCP_TOOL_NOT_FOUND, `mock: unknown tool ${spec.toolName}`);
64
+ this.emit({ type: "tool_end", toolCallId, toolName: spec.toolName, result: { message: err.message }, isError: true });
65
+ this.executedTools.push({ toolCallId, toolName: spec.toolName, isError: true, result: err.message });
66
+ return;
67
+ }
68
+ const result = await tool.execute(toolCallId, spec.args ?? {}, this.abortController?.signal);
69
+ this.emit({ type: "tool_end", toolCallId, toolName: spec.toolName, result, isError: false });
70
+ this.executedTools.push({ toolCallId, toolName: spec.toolName, isError: false, result });
71
+ }
72
+ catch (err) {
73
+ // Preserve the structured code in the agent-visible failure text so
74
+ // consumers can tell a BROWSER_TAB_CLOSED from a generic MCP failure.
75
+ let message = err instanceof Error ? err.message : String(err);
76
+ if (err instanceof PiBrowserProtocolError) {
77
+ message = `[${err.code}] ${message}`;
78
+ }
79
+ else if (isStructuredErrorObject(err)) {
80
+ message = `[${codeFromErrorObject(err)}] ${err.message}`;
81
+ }
82
+ this.emit({ type: "tool_end", toolCallId, toolName: spec.toolName, result: { message }, isError: true });
83
+ this.executedTools.push({ toolCallId, toolName: spec.toolName, isError: true, result: message });
84
+ }
85
+ }
86
+ prompt(text, images) {
87
+ if (this.isStreaming)
88
+ return Promise.reject(new Error("session busy"));
89
+ if (this.isDisposed)
90
+ return Promise.reject(new Error("session disposed"));
91
+ this.isStreaming = true;
92
+ this.abortController = new AbortController();
93
+ const promptIndex = this.prompts.length;
94
+ this.prompts.push({ text, images });
95
+ const turn = this.turnFor(text);
96
+ if (turn.fail) {
97
+ this.isStreaming = false;
98
+ return Promise.reject(new Error(turn.fail));
99
+ }
100
+ const events = turn.events ?? [{ type: "text_delta", delta: `ok: ${text}` }];
101
+ const toolCalls = turn.toolCalls ?? [];
102
+ let i = 0;
103
+ let assistantText = "";
104
+ return new Promise((resolve) => {
105
+ this.pendingResolve = (aborted) => {
106
+ this.isStreaming = false;
107
+ this.pendingResolve = undefined;
108
+ if (aborted) {
109
+ this.abortedPromptIndexes.push(promptIndex);
110
+ }
111
+ else {
112
+ this.history.push({ role: "user", text });
113
+ if (assistantText)
114
+ this.history.push({ role: "assistant", text: assistantText });
115
+ }
116
+ resolve({ aborted });
117
+ };
118
+ const finish = () => this.pendingResolve?.(false);
119
+ const stepTool = (j) => {
120
+ if (this.pendingResolve === undefined)
121
+ return;
122
+ if (j >= toolCalls.length) {
123
+ finish();
124
+ return;
125
+ }
126
+ void this.executeTool(`toolu-${randomUUID()}`, toolCalls[j]).then(() => setTimeout(() => stepTool(j + 1), 1));
127
+ };
128
+ const step = () => {
129
+ if (this.pendingResolve === undefined)
130
+ return;
131
+ if (i < events.length) {
132
+ const ev = events[i++];
133
+ if (ev.type === "text_delta")
134
+ assistantText += ev.delta;
135
+ this.emit(ev);
136
+ setTimeout(step, 1);
137
+ }
138
+ else {
139
+ stepTool(0);
140
+ }
141
+ };
142
+ setTimeout(step, turn.delayMs ?? 1);
143
+ });
144
+ }
145
+ async abort() {
146
+ if (!this.isStreaming)
147
+ return;
148
+ // Abort the in-flight tool call (e.g. the REPL cell) so the child is
149
+ // killed, then settle the prompt as cancelled.
150
+ this.abortController?.abort();
151
+ this.pendingResolve?.(true);
152
+ }
153
+ async setModel(valueId) {
154
+ if (valueId !== "mock/model-a" && valueId !== "mock/model-b") {
155
+ throw new PiBrowserProtocolError(PI_BROWSER_ERROR.ACP_CAPABILITY_UNSUPPORTED, `unknown model: ${valueId}`);
156
+ }
157
+ this.modelValueId = valueId;
158
+ this.modelChanges.push(valueId);
159
+ }
160
+ setThinkingLevel(level) {
161
+ this.thinkingLevel = level;
162
+ this.thinkingChanges.push(level);
163
+ }
164
+ subscribe(listener) {
165
+ this.listeners.add(listener);
166
+ return () => {
167
+ this.listeners.delete(listener);
168
+ };
169
+ }
170
+ dispose() {
171
+ this.isDisposed = true;
172
+ this.listeners.clear();
173
+ }
174
+ getHistory() {
175
+ return Promise.resolve(this.history);
176
+ }
177
+ }
178
+ export class MockBackend {
179
+ ready = Promise.resolve();
180
+ thinkingLevels = ["off", "low", "medium", "high"];
181
+ sessions = new Map();
182
+ /** Sessions that exist (or existed) — the mock's stand-in for persisted sessions. */
183
+ known = new Map();
184
+ /** Optional external script (PI_BROWSER_MOCK_SCRIPT) or inline entries. */
185
+ script;
186
+ constructor(script) {
187
+ if (Array.isArray(script)) {
188
+ this.script = script;
189
+ return;
190
+ }
191
+ if (script) {
192
+ let parsed;
193
+ try {
194
+ parsed = JSON.parse(readFileSync(script, "utf8"));
195
+ }
196
+ catch (err) {
197
+ throw new Error(`mock script unreadable (${script}): ${err instanceof Error ? err.message : String(err)}`);
198
+ }
199
+ if (!Array.isArray(parsed))
200
+ throw new Error("mock script must be a JSON array of entries");
201
+ this.script = parsed;
202
+ }
203
+ else {
204
+ this.script = [];
205
+ }
206
+ }
207
+ async createSession(opts) {
208
+ const session = new MockSession(randomUUID(), opts.cwd, opts.customTools ?? [], this.script);
209
+ if (opts.modelValueId)
210
+ session.modelValueId = opts.modelValueId;
211
+ if (opts.thinkingLevel)
212
+ session.thinkingLevel = opts.thinkingLevel;
213
+ this.sessions.set(session.sessionId, session);
214
+ this.known.set(session.sessionId, { cwd: opts.cwd, tools: opts.customTools ?? [] });
215
+ return session;
216
+ }
217
+ async openSession(opts) {
218
+ const meta = this.known.get(opts.sessionId);
219
+ if (!meta) {
220
+ throw new PiBrowserProtocolError(PI_BROWSER_ERROR.SESSION_NOT_FOUND, `mock session not found: ${opts.sessionId}`);
221
+ }
222
+ const prev = this.sessions.get(opts.sessionId);
223
+ if (prev && !prev.isDisposed) {
224
+ if (opts.customTools) {
225
+ const replacement = new MockSession(prev.sessionId, prev.cwd, opts.customTools, this.script);
226
+ replacement.history = prev.history;
227
+ replacement.modelValueId = prev.modelValueId;
228
+ replacement.thinkingLevel = prev.thinkingLevel;
229
+ this.sessions.set(prev.sessionId, replacement);
230
+ return replacement;
231
+ }
232
+ return prev;
233
+ }
234
+ // Reopen (resume/load after close): a fresh in-memory session with the
235
+ // same identity and the history accumulated so far.
236
+ const fresh = new MockSession(opts.sessionId, meta.cwd, opts.customTools ?? meta.tools, this.script);
237
+ if (prev) {
238
+ fresh.history = prev.history;
239
+ fresh.modelValueId = prev.modelValueId;
240
+ fresh.thinkingLevel = prev.thinkingLevel;
241
+ }
242
+ this.sessions.set(opts.sessionId, fresh);
243
+ return fresh;
244
+ }
245
+ /** Test helper: pre-existing persisted session (for resume/load). */
246
+ precreate(sessionId, cwd, history = [], customTools = []) {
247
+ const session = new MockSession(sessionId, cwd, customTools, this.script);
248
+ session.history = history;
249
+ this.sessions.set(sessionId, session);
250
+ this.known.set(sessionId, { cwd, tools: customTools });
251
+ return session;
252
+ }
253
+ async listSessions(cwd) {
254
+ const now = new Date().toISOString();
255
+ return [...this.sessions.values()]
256
+ .filter((s) => (cwd ? s.cwd === cwd : true))
257
+ .map((s) => ({
258
+ sessionId: s.sessionId,
259
+ cwd: s.cwd,
260
+ updatedAt: now,
261
+ title: "mock",
262
+ messageCount: s.prompts.length,
263
+ }));
264
+ }
265
+ async listModels() {
266
+ return [
267
+ { valueId: "mock/model-a", name: "Mock Model A" },
268
+ { valueId: "mock/model-b", name: "Mock Model B" },
269
+ ];
270
+ }
271
+ dispose() {
272
+ for (const s of this.sessions.values())
273
+ s.dispose();
274
+ this.sessions.clear();
275
+ }
276
+ }
277
+ //# sourceMappingURL=mock-backend.js.map