@qearlyao/familiar 0.2.4 → 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 (83) hide show
  1. package/README.md +4 -0
  2. package/config.example.toml +2 -2
  3. package/dist/agent/payload-normalizers.js +52 -0
  4. package/dist/agent/session-helpers.js +86 -0
  5. package/dist/agent/tool-descriptions.js +4 -0
  6. package/dist/agent/tools.js +30 -0
  7. package/dist/agent/transcript-log.js +93 -0
  8. package/dist/agent/types.js +1 -0
  9. package/dist/agent-core.js +82 -0
  10. package/dist/agent-work-queue.js +55 -0
  11. package/dist/agent.js +91 -322
  12. package/dist/browser-tools.js +80 -28
  13. package/dist/chat-log.js +15 -3
  14. package/dist/cli.js +36 -6
  15. package/dist/config/enums.js +35 -0
  16. package/dist/config/interpolate.js +15 -0
  17. package/dist/config/model-refs.js +11 -0
  18. package/dist/config/readers.js +116 -0
  19. package/dist/config/sections.js +113 -0
  20. package/dist/config/types.js +1 -0
  21. package/dist/config-registry.js +26 -7
  22. package/dist/config.js +8 -271
  23. package/dist/discord/channel.js +32 -0
  24. package/dist/discord/chunking.js +163 -0
  25. package/dist/discord/client.js +44 -0
  26. package/dist/discord/commands.js +181 -0
  27. package/dist/discord/inbound.js +44 -0
  28. package/dist/discord/send.js +106 -0
  29. package/dist/discord/turn.js +55 -0
  30. package/dist/discord.js +266 -1186
  31. package/dist/ids.js +11 -0
  32. package/dist/image-gen.js +90 -10
  33. package/dist/index.js +1 -0
  34. package/dist/memory/index/store.js +21 -17
  35. package/dist/memory/index/vector-codec.js +2 -2
  36. package/dist/memory/lcm/context-transformer.js +6 -2
  37. package/dist/memory/lcm/segment-manager.js +6 -2
  38. package/dist/memory/lcm/store/index-ids.js +6 -0
  39. package/dist/memory/lcm/store/inserts.js +31 -0
  40. package/dist/memory/lcm/store/normalizers.js +91 -0
  41. package/dist/memory/lcm/store/row-mappers.js +114 -0
  42. package/dist/memory/lcm/store/row-types.js +1 -0
  43. package/dist/memory/lcm/store/serialization.js +37 -0
  44. package/dist/memory/lcm/store/snapshots.js +73 -0
  45. package/dist/memory/lcm/store.js +20 -360
  46. package/dist/owner-identity.js +29 -0
  47. package/dist/runtime-manager.js +51 -0
  48. package/dist/runtime.js +89 -41
  49. package/dist/scheduler-runner.js +243 -0
  50. package/dist/scheduler.js +1 -1
  51. package/dist/service.js +1 -0
  52. package/dist/settings.js +3 -0
  53. package/dist/util/fs.js +1 -1
  54. package/dist/web/event-hub.js +246 -0
  55. package/dist/{web-http.js → web/http.js} +19 -5
  56. package/dist/web/memes.js +25 -0
  57. package/dist/web/messages.js +345 -0
  58. package/dist/web/multipart.js +80 -0
  59. package/dist/web/payloads.js +34 -0
  60. package/dist/{web-static.js → web/static.js} +19 -14
  61. package/dist/web/stream.js +69 -0
  62. package/dist/web-tools/cache.js +42 -0
  63. package/dist/web-tools/config.js +16 -0
  64. package/dist/web-tools/fetch-providers.js +119 -0
  65. package/dist/web-tools/format.js +88 -0
  66. package/dist/web-tools/http.js +81 -0
  67. package/dist/web-tools/routing.js +29 -0
  68. package/dist/web-tools/safety.js +73 -0
  69. package/dist/web-tools/search-providers.js +277 -0
  70. package/dist/web-tools/types.js +54 -0
  71. package/dist/web-tools/util.js +23 -0
  72. package/dist/web-tools.js +9 -798
  73. package/dist/web.js +416 -984
  74. package/npm-shrinkwrap.json +242 -201
  75. package/package.json +4 -4
  76. package/web/dist/assets/index-CSkxUQCr.js +63 -0
  77. package/web/dist/assets/index-DllM6RqL.css +2 -0
  78. package/web/dist/index.html +6 -3
  79. package/web/dist/assets/index-B23WT77N.js +0 -63
  80. package/web/dist/assets/index-D3MotFzN.css +0 -2
  81. /package/dist/{web-auth.js → web/auth.js} +0 -0
  82. /package/dist/{web-events.js → web/events.js} +0 -0
  83. /package/dist/{web-types.js → web/types.js} +0 -0
package/dist/agent.js CHANGED
@@ -1,257 +1,24 @@
1
- import { createHash } from "node:crypto";
2
- import { appendFile, mkdir, readdir, readFile } from "node:fs/promises";
3
- import { dirname, resolve } from "node:path";
4
1
  import { Agent } from "@earendil-works/pi-agent-core";
5
2
  import { streamSimple } from "@earendil-works/pi-ai";
6
- import { createBashTool, createEditTool, createReadTool, createWriteTool } from "@earendil-works/pi-coding-agent";
7
3
  import { setAddedModelsPath } from "./added-models.js";
8
- import { createBrowserTools } from "./browser-tools.js";
4
+ import { normalizeProviderPayload } from "./agent/payload-normalizers.js";
5
+ import { assertModelAllowed, deriveSessionId, formatModel, getLastAssistantText, getRequestApiKey, installProviderDebugFilter, isNoisyProviderDebug, logUsage, resolveModelName, userTextMessage, } from "./agent/session-helpers.js";
6
+ import { createFamiliarTools, setReferenceAttachments } from "./agent/tools.js";
7
+ import { loadStoredMessages, writePayloadLog, writeTranscriptLog } from "./agent/transcript-log.js";
9
8
  import { setConfigOverridesPath } from "./config-overrides.js";
10
9
  import { applyConfigOverridesToConfig } from "./config-registry.js";
11
10
  import { createGeneratedMediaSink } from "./generated-media.js";
12
- import { createImageGenTool } from "./image-gen.js";
13
- import { assertModelCanAuthenticate, clampConfiguredThinkingLevel, createConfiguredModel, isAllowedModel, isThinkingLevel, parseModelRef, resolveModel, resolveModelApiKey, supportedThinkingLevels, } from "./models.js";
11
+ import { clampConfiguredThinkingLevel, createConfiguredModel, isThinkingLevel, parseModelRef, resolveModel, supportedThinkingLevels, } from "./models.js";
14
12
  import { buildSystemPrompt, loadPersona } from "./persona.js";
15
13
  import { formatFamiliarSkillsForPrompt, loadFamiliarSkills, logSkillDiagnostics } from "./skills.js";
16
- import { createTtsTool } from "./tts.js";
17
- import { isEnoent } from "./util/fs.js";
18
- import { isRecord } from "./util/guards.js";
19
- import { createWebTools } from "./web-tools.js";
20
- const BASH_DESCRIPTION = "run a bash command. defaults to the workspace; absolute paths and `~/...` reach anywhere else. returns stdout and stderr. output truncates to the last 2000 lines or 50KB, whichever hits first; full output lands in a temp file if cut. timeout in seconds optional.";
21
- const READ_DESCRIPTION = "read a file. paths resolve from the workspace, but absolute paths and `~/...` work too. text and images (jpg, png, gif, webp); images come back as attachments. text output truncates to 2000 lines or 50KB, whichever hits first — use offset and limit for long files, and keep paging until you have what you need.";
22
- const WRITE_DESCRIPTION = "write a file from scratch or replace it wholesale. creates the file if missing, overwrites if not, and makes parent directories as needed. paths resolve from the workspace; absolute and `~/...` also accepted.";
23
- const EDIT_DESCRIPTION = "edit a file with exact text replacement. each edits[].oldText must match a unique, non-overlapping slice of the original — overlapping or nested edits are rejected, so merge nearby changes into one entry rather than chaining them. don't pad oldText with large unchanged regions just to connect distant changes. paths resolve from the workspace; absolute and `~/...` also work.";
24
- function deriveSessionId(workspacePath, sessionKey) {
25
- const digest = createHash("sha256").update(`${workspacePath}\0${sessionKey}`).digest("hex").slice(0, 32);
26
- return `familiar-${digest}`;
27
- }
28
- function dailyLogPath(dataDir, streamName, now = new Date()) {
29
- const date = now.toISOString().slice(0, 10);
30
- return resolve(dataDir, streamName, `${date}.jsonl`);
31
- }
32
- async function appendJsonl(path, record) {
33
- await mkdir(dirname(path), { recursive: true });
34
- await appendFile(path, `${JSON.stringify(record)}\n`, "utf8");
35
- }
36
- function writePayloadLog(config, record) {
37
- appendJsonl(dailyLogPath(config.workspace.dataDir, "payloads"), record).catch((err) => console.error("payload log write failed", err));
38
- }
39
- function writeTranscriptLog(config, record) {
40
- appendJsonl(dailyLogPath(config.workspace.dataDir, "transcripts"), record).catch((err) => console.error("transcript log write failed", err));
41
- }
42
- function clonePayload(payload) {
43
- if (typeof structuredClone === "function")
44
- return structuredClone(payload);
45
- return JSON.parse(JSON.stringify(payload));
46
- }
47
- // TODO: remove once pi-ai handles store:false reasoning replay upstream.
48
- function stripOpenAIStoredReasoningItems(payload, model) {
49
- if (model.api !== "openai-responses" && model.api !== "azure-openai-responses")
50
- return payload;
51
- const nextPayload = clonePayload(payload);
52
- if (!isRecord(nextPayload))
53
- return nextPayload;
54
- const request = nextPayload;
55
- if (request.store !== false)
56
- return nextPayload;
57
- const input = request.input;
58
- if (!Array.isArray(input))
59
- return nextPayload;
60
- request.input = input.filter((item) => {
61
- if (!item || typeof item !== "object" || Array.isArray(item))
62
- return true;
63
- return item.type !== "reasoning";
64
- });
65
- return nextPayload;
66
- }
67
- function moveAnthropicCacheControlBeforeInjectedMemory(payload, model) {
68
- if (model.api !== "anthropic-messages")
69
- return payload;
70
- if (!isRecord(payload) || !Array.isArray(payload.messages))
71
- return payload;
72
- const messages = payload.messages;
73
- const lastMessage = messages.at(-1);
74
- if (!isRecord(lastMessage) || lastMessage.role !== "user")
75
- return payload;
76
- const content = lastMessage.content;
77
- if (!Array.isArray(content) || content.length < 2)
78
- return payload;
79
- const injectedBlock = content.at(-1);
80
- const stableBlock = content.at(-2);
81
- if (!isInjectedMemoryTextBlock(injectedBlock) || !isRecord(stableBlock))
82
- return payload;
83
- const cacheControl = injectedBlock.cache_control;
84
- if (!cacheControl)
85
- return payload;
86
- delete injectedBlock.cache_control;
87
- stableBlock.cache_control = cacheControl;
88
- return payload;
89
- }
90
- function isInjectedMemoryTextBlock(value) {
91
- if (!isRecord(value) || value.type !== "text" || typeof value.text !== "string")
92
- return false;
93
- return value.text.trim().startsWith("<injected_memory>");
94
- }
95
- function normalizeProviderPayload(payload, model) {
96
- return moveAnthropicCacheControlBeforeInjectedMemory(stripOpenAIStoredReasoningItems(payload, model), model);
97
- }
98
14
  export const __agentTest = {
15
+ isNoisyProviderDebug,
99
16
  normalizeProviderPayload,
100
17
  };
101
- function isStoredMessageRecord(value) {
102
- if (!value || typeof value !== "object")
103
- return false;
104
- const record = value;
105
- return typeof record.ts === "string" && typeof record.sessionId === "string" && !!record.message;
106
- }
107
- function isStoredResetRecord(value) {
108
- if (!value || typeof value !== "object")
109
- return false;
110
- const record = value;
111
- return record.type === "reset" && typeof record.ts === "string" && typeof record.sessionId === "string";
112
- }
113
- async function loadStoredMessages(dataDir, sessionId) {
114
- const transcriptsDir = resolve(dataDir, "transcripts");
115
- let files;
116
- try {
117
- files = await readdir(transcriptsDir);
118
- }
119
- catch (error) {
120
- if (isEnoent(error))
121
- return [];
122
- console.error("transcript history read failed", error);
123
- return [];
124
- }
125
- const records = [];
126
- for (const file of files.filter((entry) => entry.endsWith(".jsonl")).sort()) {
127
- const path = resolve(transcriptsDir, file);
128
- let contents;
129
- try {
130
- contents = await readFile(path, "utf8");
131
- }
132
- catch (error) {
133
- console.error(`transcript file read failed: ${path}`, error);
134
- continue;
135
- }
136
- for (const [index, line] of contents.split(/\r?\n/).entries()) {
137
- if (!line.trim())
138
- continue;
139
- try {
140
- const parsed = JSON.parse(line);
141
- if (!isStoredMessageRecord(parsed) && !isStoredResetRecord(parsed)) {
142
- console.error(`skipping malformed transcript line: ${path}:${index + 1}`);
143
- continue;
144
- }
145
- if (parsed.sessionId !== sessionId)
146
- continue;
147
- records.push(parsed);
148
- }
149
- catch (error) {
150
- console.error(`skipping unparsable transcript line: ${path}:${index + 1}`, error);
151
- }
152
- }
153
- }
154
- records.sort((a, b) => a.ts.localeCompare(b.ts));
155
- let lastResetIndex = -1;
156
- for (let index = records.length - 1; index >= 0; index--) {
157
- const record = records[index];
158
- if (record && "type" in record && record.type === "reset") {
159
- lastResetIndex = index;
160
- break;
161
- }
162
- }
163
- const activeRecords = lastResetIndex >= 0 ? records.slice(lastResetIndex + 1) : records;
164
- return activeRecords.flatMap((record) => ("message" in record ? [record.message] : []));
165
- }
166
- function getRequestApiKey(config, model) {
167
- const apiKey = resolveModelApiKey(config, model);
168
- assertModelCanAuthenticate(config, model);
169
- return apiKey;
170
- }
171
- function formatModel(model) {
172
- return `${model.provider}/${model.id}`;
173
- }
174
- function resolveModelName(value, fallback) {
175
- return value ?? formatModel(fallback);
176
- }
177
- function assertModelAllowed(config, ref) {
178
- if (!isAllowedModel(config, ref))
179
- throw new Error(`Model is not allowlisted: ${ref.key}`);
180
- }
181
- function extractText(message) {
182
- if (!message || typeof message !== "object")
183
- return "";
184
- const record = message;
185
- if (record.stopReason === "error" && typeof record.errorMessage === "string" && record.errorMessage.trim()) {
186
- return `Model error: ${record.errorMessage}`;
187
- }
188
- if (!("content" in record))
189
- return "";
190
- const content = record.content;
191
- if (typeof content === "string")
192
- return content;
193
- if (!Array.isArray(content))
194
- return "";
195
- return content
196
- .filter((item) => {
197
- return !!item && typeof item === "object" && item.type === "text";
198
- })
199
- .map((item) => item.text)
200
- .join("");
201
- }
202
- function getLastAssistantText(agent) {
203
- for (let i = agent.state.messages.length - 1; i >= 0; i--) {
204
- const message = agent.state.messages[i];
205
- if (message.role === "assistant")
206
- return extractText(message);
207
- }
208
- return "";
209
- }
210
- function logUsage(event) {
211
- if (event.type !== "message_end" || event.message.role !== "assistant")
212
- return;
213
- const usage = event.message.usage;
214
- console.log(JSON.stringify({
215
- type: "usage",
216
- input: usage.input,
217
- output: usage.output,
218
- cacheRead: usage.cacheRead,
219
- cacheWrite: usage.cacheWrite,
220
- cost: usage.cost.total,
221
- }));
222
- }
223
- function userTextMessage(text, timestamp = Date.now()) {
224
- return {
225
- role: "user",
226
- content: [{ type: "text", text }],
227
- timestamp,
228
- };
229
- }
230
- function createFamiliarTools(config, mediaSink, referenceAttachments = () => [], memoryService) {
231
- const bashTool = createBashTool(config.workspacePath);
232
- bashTool.description = BASH_DESCRIPTION;
233
- const readTool = createReadTool(config.workspacePath);
234
- readTool.description = READ_DESCRIPTION;
235
- const writeTool = createWriteTool(config.workspacePath);
236
- writeTool.description = WRITE_DESCRIPTION;
237
- const editTool = createEditTool(config.workspacePath);
238
- editTool.description = EDIT_DESCRIPTION;
239
- return [
240
- bashTool,
241
- readTool,
242
- writeTool,
243
- editTool,
244
- createTtsTool(config, mediaSink),
245
- ...(config.imageGen.enabled ? [createImageGenTool(config, mediaSink, { referenceAttachments })] : []),
246
- ...createWebTools(config),
247
- ...createBrowserTools(config, mediaSink),
248
- ...(memoryService?.memoryTools() ?? []),
249
- ];
250
- }
251
- function setReferenceAttachments(session, attachments = []) {
252
- session.referenceAttachments.splice(0, session.referenceAttachments.length, ...attachments);
253
- }
18
+ const PROVIDER_MAX_RETRIES = 2;
19
+ const PROVIDER_MAX_RETRY_DELAY_MS = 60_000;
254
20
  export async function createFamiliarAgent(config, settings, memoryService, options = {}) {
21
+ installProviderDebugFilter();
255
22
  setAddedModelsPath(config.workspace.dataDir);
256
23
  setConfigOverridesPath(config.workspace.dataDir);
257
24
  applyConfigOverridesToConfig(config);
@@ -269,21 +36,8 @@ export async function createFamiliarAgent(config, settings, memoryService, optio
269
36
  // activePromptOptions covers the synchronous promptMessage window; skipAmbientMessages
270
37
  // tags message identities so followUpMessage's fire-and-forget path also opts out.
271
38
  const activePromptOptions = new Map();
272
- const softStopRequested = new Map();
273
39
  const skipAmbientMessages = new WeakSet();
274
40
  let reloadInProgress;
275
- const installSoftStopHook = (sessionKey, agent) => {
276
- const agentWithLoopConfig = agent;
277
- const createLoopConfig = agentWithLoopConfig.createLoopConfig?.bind(agent);
278
- if (!createLoopConfig)
279
- return;
280
- agentWithLoopConfig.createLoopConfig = ((options) => {
281
- return {
282
- ...createLoopConfig(options),
283
- shouldStopAfterTurn: async () => softStopRequested.get(sessionKey) === true,
284
- };
285
- });
286
- };
287
41
  const resolveChannelModel = (sessionKey) => {
288
42
  const override = settings.getChannelModel(sessionKey);
289
43
  const modelName = resolveModelName(override.value, defaultModel);
@@ -344,6 +98,8 @@ export async function createFamiliarAgent(config, settings, memoryService, optio
344
98
  ...options,
345
99
  apiKey: getRequestApiKey(config, streamModel),
346
100
  cacheRetention: config.agent.cacheRetention,
101
+ maxRetries: options?.maxRetries ?? PROVIDER_MAX_RETRIES,
102
+ maxRetryDelayMs: options?.maxRetryDelayMs ?? PROVIDER_MAX_RETRY_DELAY_MS,
347
103
  onPayload: (payload, payloadModel) => {
348
104
  const requestPayload = normalizeProviderPayload(payload, payloadModel);
349
105
  writePayloadLog(config, {
@@ -381,7 +137,6 @@ export async function createFamiliarAgent(config, settings, memoryService, optio
381
137
  }
382
138
  : undefined,
383
139
  });
384
- installSoftStopHook(sessionKey, agent);
385
140
  agent.subscribe((event) => {
386
141
  logUsage(event);
387
142
  if (event.type === "message_end") {
@@ -465,25 +220,90 @@ export async function createFamiliarAgent(config, settings, memoryService, optio
465
220
  };
466
221
  }));
467
222
  };
223
+ // Shared serialization wrapper for prompt/promptMessage: run after the prior turn
224
+ // settles on the session's promptQueue, keep the stored tail non-rejecting, and run
225
+ // teardown (onTurnEnd → reference reset → enterTurn's exit → unsubscribe) in a fixed
226
+ // order regardless of how the turn ends. enterTurn returns its own cleanup so callers
227
+ // can scope per-turn state (e.g. activePromptOptions) across the exact finally window.
228
+ const runPromptTurn = async (sessionKey, options, eventHandler, dispatch, enterTurn) => {
229
+ const session = await getSession(sessionKey);
230
+ const run = session.promptQueue.then(async () => {
231
+ session.mediaSink.drain();
232
+ setReferenceAttachments(session, options.referenceAttachments);
233
+ const unsubscribe = eventHandler ? session.agent.subscribe((event) => eventHandler(event)) : undefined;
234
+ const exitTurn = enterTurn?.();
235
+ try {
236
+ await dispatch(session);
237
+ }
238
+ finally {
239
+ try {
240
+ await options.onTurnEnd?.();
241
+ }
242
+ catch (error) {
243
+ console.error("turn end callback failed", error);
244
+ }
245
+ finally {
246
+ setReferenceAttachments(session);
247
+ exitTurn?.();
248
+ unsubscribe?.();
249
+ }
250
+ }
251
+ return {
252
+ text: getLastAssistantText(session.agent),
253
+ attachments: session.mediaSink.drain(),
254
+ };
255
+ });
256
+ session.promptQueue = run.then(() => undefined, () => undefined);
257
+ return run;
258
+ };
259
+ const popLastAssistant = (session, action) => {
260
+ const messages = session.agent.state.messages;
261
+ const message = messages.at(-1);
262
+ if (!message || message.role !== "assistant") {
263
+ throw new Error(`No assistant message to ${action}`);
264
+ }
265
+ if (action === "retry" && message.stopReason === "aborted") {
266
+ throw new Error("Cannot retry an aborted assistant message");
267
+ }
268
+ session.agent.state.messages = messages.slice(0, -1);
269
+ writeTranscriptLog(config, {
270
+ ts: new Date().toISOString(),
271
+ sessionId: session.sessionId,
272
+ type: "supersede",
273
+ messageTimestamp: message.timestamp,
274
+ });
275
+ };
468
276
  return {
469
- abort(sessionKey) {
277
+ async abort(sessionKey) {
470
278
  const session = sessions.get(sessionKey);
471
- void session
472
- ?.then((resolved) => {
279
+ if (!session)
280
+ return;
281
+ try {
282
+ const resolved = await session;
473
283
  resolved.agent.abort();
474
284
  resolved.agent.clearAllQueues();
475
- })
476
- .catch((error) => console.error(`failed to abort familiar session ${sessionKey}`, error));
285
+ await resolved.agent.waitForIdle();
286
+ }
287
+ catch (error) {
288
+ console.error(`failed to abort familiar session ${sessionKey}`, error);
289
+ }
290
+ },
291
+ async retryLastAssistant(sessionKey, eventHandler, options = {}) {
292
+ return runPromptTurn(sessionKey, options, eventHandler, async (session) => {
293
+ popLastAssistant(session, "retry");
294
+ await session.agent.continue();
295
+ });
477
296
  },
478
- requestSoftStop(sessionKey) {
479
- softStopRequested.set(sessionKey, true);
297
+ async deleteLastAssistant(sessionKey) {
298
+ const session = await getSession(sessionKey);
299
+ await session.promptQueue;
300
+ popLastAssistant(session, "delete");
480
301
  },
481
302
  async reset(sessionKey) {
482
303
  const existing = sessions.get(sessionKey);
483
304
  if (!existing)
484
305
  return;
485
306
  const session = await existing;
486
- softStopRequested.set(sessionKey, false);
487
307
  resetSession(session);
488
308
  },
489
309
  async reload() {
@@ -577,74 +397,23 @@ export async function createFamiliarAgent(config, settings, memoryService, optio
577
397
  return `Thinking set to ${clamped}${suffix} for this channel\nSupported: ${supportedThinkingLevels(model).join(", ")}`;
578
398
  },
579
399
  async prompt(sessionKey, input, imagesOrOnEvent, onEvent, options = {}) {
580
- const session = await getSession(sessionKey);
581
400
  const images = Array.isArray(imagesOrOnEvent) ? imagesOrOnEvent : undefined;
582
401
  const eventHandler = Array.isArray(imagesOrOnEvent) ? onEvent : imagesOrOnEvent;
583
- const run = session.promptQueue.then(async () => {
584
- softStopRequested.set(sessionKey, false);
585
- session.mediaSink.drain();
586
- setReferenceAttachments(session, options.referenceAttachments);
587
- const unsubscribe = eventHandler ? session.agent.subscribe((event) => eventHandler(event)) : undefined;
588
- try {
589
- await session.agent.prompt(input, images);
590
- }
591
- finally {
592
- try {
593
- await options.onTurnEnd?.();
594
- }
595
- catch (error) {
596
- console.error("turn end callback failed", error);
597
- }
598
- finally {
599
- setReferenceAttachments(session);
600
- unsubscribe?.();
601
- }
602
- }
603
- return {
604
- text: getLastAssistantText(session.agent),
605
- attachments: session.mediaSink.drain(),
606
- };
607
- });
608
- session.promptQueue = run.then(() => undefined, () => undefined);
609
- return run;
402
+ return runPromptTurn(sessionKey, options, eventHandler, (session) => session.agent.prompt(input, images));
610
403
  },
611
404
  async promptMessage(sessionKey, message, onEvent, options = {}) {
612
- const session = await getSession(sessionKey);
613
- const run = session.promptQueue.then(async () => {
614
- softStopRequested.set(sessionKey, false);
615
- session.mediaSink.drain();
616
- setReferenceAttachments(session, options.referenceAttachments);
617
- const unsubscribe = onEvent ? session.agent.subscribe((event) => onEvent(event)) : undefined;
405
+ return runPromptTurn(sessionKey, options, onEvent, (session) => session.agent.prompt(message), () => {
618
406
  const previousOptions = activePromptOptions.get(sessionKey);
619
407
  activePromptOptions.set(sessionKey, options);
620
408
  if (options.skipAmbient)
621
409
  skipAmbientMessages.add(message);
622
- try {
623
- await session.agent.prompt(message);
624
- }
625
- finally {
626
- try {
627
- await options.onTurnEnd?.();
628
- }
629
- catch (error) {
630
- console.error("turn end callback failed", error);
631
- }
632
- finally {
633
- setReferenceAttachments(session);
634
- if (previousOptions)
635
- activePromptOptions.set(sessionKey, previousOptions);
636
- else
637
- activePromptOptions.delete(sessionKey);
638
- unsubscribe?.();
639
- }
640
- }
641
- return {
642
- text: getLastAssistantText(session.agent),
643
- attachments: session.mediaSink.drain(),
410
+ return () => {
411
+ if (previousOptions)
412
+ activePromptOptions.set(sessionKey, previousOptions);
413
+ else
414
+ activePromptOptions.delete(sessionKey);
644
415
  };
645
416
  });
646
- session.promptQueue = run.then(() => undefined, () => undefined);
647
- return run;
648
417
  },
649
418
  steer(sessionKey, input) {
650
419
  const session = sessions.get(sessionKey);
@@ -142,6 +142,7 @@ function defaultBrowserRunner() {
142
142
  reject(new Error(`Browser command timed out after ${options.timeoutMs}ms.`));
143
143
  }, options.timeoutMs);
144
144
  const abort = () => {
145
+ clearTimeout(timeout);
145
146
  child.kill("SIGTERM");
146
147
  reject(new Error("Browser command aborted."));
147
148
  };
@@ -192,6 +193,15 @@ function parseJson(text) {
192
193
  return undefined;
193
194
  }
194
195
  }
196
+ function parseOpenCliJsonOutput(result, context) {
197
+ if (result.json !== undefined)
198
+ return result.json;
199
+ const output = result.stdout.trim();
200
+ if (!output)
201
+ throw new Error(`OpenCLI ${context} returned no JSON output.`);
202
+ const tail = output.slice(-120).replace(/\s+/g, " ").trim();
203
+ throw new Error(`OpenCLI ${context} returned malformed JSON output near: ${tail || "(empty)"}`);
204
+ }
195
205
  function stringArg(value) {
196
206
  if (value === undefined || value === null)
197
207
  return undefined;
@@ -522,24 +532,47 @@ function assertSiteAllowed(config, site) {
522
532
  if (!config.browser.allowedSites[site])
523
533
  throw new Error(`OpenCLI site is not allowlisted: ${site}`);
524
534
  }
535
+ function commandInfoFromJson(json) {
536
+ if (!isRecord(json))
537
+ return undefined;
538
+ const name = stringArg(json.name);
539
+ if (!name)
540
+ return undefined;
541
+ return {
542
+ name,
543
+ access: stringArg(json.access) ?? "unknown",
544
+ description: stringArg(json.description),
545
+ usage: stringArg(json.usage),
546
+ };
547
+ }
525
548
  function parseSiteCommands(json) {
526
549
  const commands = isRecord(json) && Array.isArray(json.commands) ? json.commands : [];
527
- return commands.flatMap((command) => {
528
- if (!isRecord(command))
529
- return [];
530
- const name = stringArg(command.name);
531
- if (!name)
550
+ return {
551
+ commands: commands.flatMap((command) => {
552
+ const info = commandInfoFromJson(command);
553
+ return info ? [info] : [];
554
+ }),
555
+ complete: true,
556
+ };
557
+ }
558
+ function parsePlainSiteHelp(site, text) {
559
+ const commandSection = text.match(/Commands:\n(?<body>[\s\S]*?)(?:\n\n[A-Z][^\n]* options:|\n\nCommon options:|\n\nBrowser common options:|\n\nAgent tip:|$)/);
560
+ const body = commandSection?.groups?.body;
561
+ if (!body)
562
+ return undefined;
563
+ const commands = body.split("\n").flatMap((line) => {
564
+ const match = line.match(/^\s{2}(?<name>[A-Za-z0-9._-]+)\b.*\[(?<access>read|write|admin)\]/);
565
+ if (!match?.groups)
532
566
  return [];
533
- const access = stringArg(command.access) ?? "unknown";
534
567
  return [
535
568
  {
536
- name,
537
- access,
538
- description: stringArg(command.description),
539
- usage: stringArg(command.usage),
569
+ name: match.groups.name,
570
+ access: match.groups.access,
571
+ usage: `opencli ${site} ${match.groups.name}`,
540
572
  },
541
573
  ];
542
574
  });
575
+ return { commands, complete: false };
543
576
  }
544
577
  async function loadSiteCommands(site, config, runner, signal) {
545
578
  const result = await runner(openCliSpec(config, [...baseArgs(config), site, "--help", "-f", "json"]), {
@@ -548,15 +581,35 @@ async function loadSiteCommands(site, config, runner, signal) {
548
581
  });
549
582
  if (!result.ok)
550
583
  throw new Error(formatBrowserResult(result, config.browser.maxOutputChars).text);
551
- return parseSiteCommands(result.json);
584
+ try {
585
+ return parseSiteCommands(parseOpenCliJsonOutput(result, `${site} metadata`));
586
+ }
587
+ catch (error) {
588
+ const plainResult = await runner(openCliSpec(config, [...baseArgs(config), site, "--help"]), {
589
+ timeoutMs: config.browser.timeoutMs,
590
+ signal,
591
+ });
592
+ if (!plainResult.ok)
593
+ throw error;
594
+ const plainListing = parsePlainSiteHelp(site, plainResult.stdout);
595
+ if (!plainListing)
596
+ throw error;
597
+ return plainListing;
598
+ }
552
599
  }
553
- function findSiteCommand(commands, site, command) {
554
- const match = commands.find((item) => item.name === command);
555
- if (!match)
600
+ async function loadSiteCommand(site, command, config, runner, signal) {
601
+ const result = await runner(openCliSpec(config, [...baseArgs(config), site, command, "--help", "-f", "json"]), {
602
+ timeoutMs: config.browser.timeoutMs,
603
+ signal,
604
+ });
605
+ if (!result.ok)
606
+ throw new Error(formatBrowserResult(result, config.browser.maxOutputChars).text);
607
+ const info = commandInfoFromJson(parseOpenCliJsonOutput(result, `${site} ${command} metadata`));
608
+ if (!info || info.name !== command)
556
609
  throw new Error(`OpenCLI site command is not available: ${site} ${command}`);
557
- return match;
610
+ return info;
558
611
  }
559
- function buildSiteArgs(input, config, commandInfo) {
612
+ function resolveSiteCommand(input, config) {
560
613
  const site = stringArg(input.site);
561
614
  const command = stringArg(input.command);
562
615
  if (!site || !command)
@@ -564,6 +617,10 @@ function buildSiteArgs(input, config, commandInfo) {
564
617
  assertSafeName(site, "browser.site");
565
618
  assertSafeName(command, "browser.command");
566
619
  assertSiteAllowed(config, site);
620
+ return { site, command };
621
+ }
622
+ function buildSiteArgs(input, config, commandInfo) {
623
+ const { site, command } = resolveSiteCommand(input, config);
567
624
  if (commandInfo.access === "write" && !config.browser.readWrite) {
568
625
  throw new Error(`OpenCLI write command is disabled until browser.read_write is true: ${site} ${command}`);
569
626
  }
@@ -596,15 +653,9 @@ function buildSiteArgs(input, config, commandInfo) {
596
653
  return args;
597
654
  }
598
655
  async function buildSiteRunSpec(input, config, runner, signal) {
599
- const site = stringArg(input.site);
600
- const command = stringArg(input.command);
601
- if (!site || !command)
602
- throw new Error("browser site mode requires site and command.");
603
- assertSafeName(site, "browser.site");
604
- assertSafeName(command, "browser.command");
605
- assertSiteAllowed(config, site);
606
- const commands = await loadSiteCommands(site, config, runner, signal);
607
- return openCliSpec(config, buildSiteArgs(input, config, findSiteCommand(commands, site, command)));
656
+ const { site, command } = resolveSiteCommand(input, config);
657
+ const commandInfo = await loadSiteCommand(site, command, config, runner, signal);
658
+ return openCliSpec(config, buildSiteArgs(input, config, commandInfo));
608
659
  }
609
660
  function buildRunSpec(input, config) {
610
661
  const backend = pageBackend(input, config);
@@ -622,15 +673,16 @@ async function listCommands(input, config, runner, signal) {
622
673
  const sites = site ? [site] : Object.keys(config.browser.allowedSites);
623
674
  const lines = ["allowlisted site commands:"];
624
675
  for (const name of sites) {
625
- const commands = await loadSiteCommands(name, config, runner, signal);
676
+ const listing = await loadSiteCommands(name, config, runner, signal);
626
677
  const groups = new Map();
627
- for (const command of commands) {
678
+ for (const command of listing.commands) {
628
679
  const names = groups.get(command.access) ?? [];
629
680
  names.push(command.name);
630
681
  groups.set(command.access, names);
631
682
  }
632
683
  const parts = Array.from(groups.entries()).map(([access, names]) => `${access}=[${names.join(", ")}]`);
633
- lines.push(`- ${name}: ${parts.join(" ")}`);
684
+ const suffix = listing.complete ? "" : " (from plain help)";
685
+ lines.push(`- ${name}: ${parts.join(" ")}${suffix}`);
634
686
  }
635
687
  return lines.join("\n");
636
688
  }