@listdoing/core 0.1.5 → 0.2.1

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/dist/index.mjs CHANGED
@@ -65,6 +65,7 @@ var ACPErrorCodes = {
65
65
  LLM_CALL_FAILED: -32130,
66
66
  LLM_EMPTY_RESPONSE: -32135,
67
67
  LLM_TOKEN_LIMIT: -32131,
68
+ LLM_PROXY_CONFIG_INVALID: -32136,
68
69
  LLM_RATE_LIMIT: -32132,
69
70
  LLM_PROVIDER_UNAVAILABLE: -32133,
70
71
  TOKEN_BUDGET_EXCEEDED: -32134,
@@ -129,9 +130,46 @@ var OpenAIContentAdapter = class {
129
130
  }
130
131
  };
131
132
 
133
+ // src/runtime/abort.ts
134
+ function abortError() {
135
+ const error = new Error("Request aborted");
136
+ error.name = "AbortError";
137
+ return error;
138
+ }
139
+ function throwIfAborted(signal) {
140
+ if (signal?.aborted) throw abortError();
141
+ }
142
+ function linkAbort(parent, child) {
143
+ const abort = () => child.abort();
144
+ if (parent?.aborted) abort();
145
+ else parent?.addEventListener("abort", abort, { once: true });
146
+ return () => parent?.removeEventListener("abort", abort);
147
+ }
148
+ function errorMessage(error) {
149
+ return error instanceof Error ? error.message : String(error ?? "Unknown error");
150
+ }
151
+ function abortableDelay(ms, signal) {
152
+ throwIfAborted(signal);
153
+ return new Promise((resolve, reject) => {
154
+ const cleanup = () => signal?.removeEventListener("abort", abort);
155
+ const timer = setTimeout(() => {
156
+ cleanup();
157
+ resolve();
158
+ }, ms);
159
+ const abort = () => {
160
+ clearTimeout(timer);
161
+ cleanup();
162
+ reject(abortError());
163
+ };
164
+ signal?.addEventListener("abort", abort, { once: true });
165
+ });
166
+ }
167
+
132
168
  // src/llm/openai.ts
133
169
  var OpenAIAdapter = class {
134
- constructor(config) {
170
+ constructor(config, options) {
171
+ this.options = options;
172
+ this.verified = false;
135
173
  this.config = config;
136
174
  this.contentAdapter = new OpenAIContentAdapter();
137
175
  }
@@ -150,6 +188,10 @@ var OpenAIAdapter = class {
150
188
  const base = this.getBaseUrl().replace(/\/+$/, "");
151
189
  return `${base}/chat/completions`;
152
190
  }
191
+ getConnectionStatus() {
192
+ const endpoint = this.getEndpoint().trim().split(/[?#]/, 1)[0].replace(/^(https?:\/\/|\/\/)[^/]*@/i, "$1");
193
+ return { endpoint, verified: this.verified };
194
+ }
153
195
  buildHeaders() {
154
196
  const headers = {
155
197
  "Content-Type": "application/json"
@@ -200,51 +242,63 @@ var OpenAIAdapter = class {
200
242
  }));
201
243
  }
202
244
  async chat(messages, tools, options) {
203
- if (this.config.adapter === "custom" && this.config.transformRequest && this.config.transformResponse) {
204
- const reqBody = this.config.transformRequest(messages, tools, {});
205
- const res2 = await fetch(this.getEndpoint(), {
245
+ const request = this.createRequest(options?.signal);
246
+ try {
247
+ if (this.config.adapter === "custom" && this.config.transformRequest && this.config.transformResponse) {
248
+ const reqBody = this.config.transformRequest(messages, tools, {});
249
+ const res2 = await this.fetchResponse({
250
+ method: "POST",
251
+ headers: this.buildHeaders(),
252
+ body: JSON.stringify(reqBody),
253
+ signal: request.controller.signal
254
+ });
255
+ if (!res2.ok) {
256
+ const text = await res2.text().catch(() => "");
257
+ this.checkContextOverflow(res2.status, text);
258
+ throw new Error(`LLM request failed: ${res2.status} ${text}`);
259
+ }
260
+ const data2 = await res2.json();
261
+ const response2 = this.config.transformResponse(data2);
262
+ if (response2?.content || response2?.toolCalls?.length) this.verified = true;
263
+ return response2;
264
+ }
265
+ const body = {
266
+ model: this.config.model,
267
+ messages: this.messagesToOpenAI(messages)
268
+ };
269
+ if (tools.length > 0) {
270
+ body.tools = this.toolsToOpenAI(tools);
271
+ }
272
+ if (this.config.temperature !== void 0) {
273
+ body.temperature = this.config.temperature;
274
+ }
275
+ if (this.config.maxTokens !== void 0) {
276
+ body.max_tokens = this.config.maxTokens;
277
+ }
278
+ if (this.config.extraBody) {
279
+ Object.assign(body, this.config.extraBody);
280
+ }
281
+ const res = await this.fetchResponse({
206
282
  method: "POST",
207
283
  headers: this.buildHeaders(),
208
- body: JSON.stringify(reqBody),
209
- signal: options?.signal
284
+ body: JSON.stringify(body),
285
+ signal: request.controller.signal
210
286
  });
211
- if (!res2.ok) {
212
- const text = await res2.text().catch(() => "");
213
- this.checkContextOverflow(res2.status, text);
214
- throw new Error(`LLM request failed: ${res2.status} ${text}`);
287
+ if (!res.ok) {
288
+ const text = await res.text().catch(() => "");
289
+ this.checkContextOverflow(res.status, text);
290
+ throw new Error(`LLM request failed: ${res.status} ${text}`);
215
291
  }
216
- const data2 = await res2.json();
217
- return this.config.transformResponse(data2);
218
- }
219
- const body = {
220
- model: this.config.model,
221
- messages: this.messagesToOpenAI(messages)
222
- };
223
- if (tools.length > 0) {
224
- body.tools = this.toolsToOpenAI(tools);
225
- }
226
- if (this.config.temperature !== void 0) {
227
- body.temperature = this.config.temperature;
228
- }
229
- if (this.config.maxTokens !== void 0) {
230
- body.max_tokens = this.config.maxTokens;
231
- }
232
- if (this.config.extraBody) {
233
- Object.assign(body, this.config.extraBody);
234
- }
235
- const res = await fetch(this.getEndpoint(), {
236
- method: "POST",
237
- headers: this.buildHeaders(),
238
- body: JSON.stringify(body),
239
- signal: options?.signal
240
- });
241
- if (!res.ok) {
242
- const text = await res.text().catch(() => "");
243
- this.checkContextOverflow(res.status, text);
244
- throw new Error(`LLM request failed: ${res.status} ${text}`);
292
+ const data = await res.json();
293
+ const response = this.parseResponse(data);
294
+ this.verified = true;
295
+ return response;
296
+ } catch (error) {
297
+ if (request.controller.signal.aborted && !options?.signal?.aborted) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "LLM request timed out", { reason: "timeout" });
298
+ throw error;
299
+ } finally {
300
+ request.cleanup();
245
301
  }
246
- const data = await res.json();
247
- return this.parseResponse(data);
248
302
  }
249
303
  /**
250
304
  * Stream LLM responses token-by-token via SSE.
@@ -252,115 +306,206 @@ var OpenAIAdapter = class {
252
306
  * Collects the full response and returns it at the end.
253
307
  */
254
308
  async *chatStream(messages, tools, options) {
255
- const body = {
256
- model: this.config.model,
257
- messages: this.messagesToOpenAI(messages),
258
- stream: true
259
- };
260
- if (tools.length > 0) {
261
- body.tools = this.toolsToOpenAI(tools);
262
- }
263
- if (this.config.temperature !== void 0) {
264
- body.temperature = this.config.temperature;
265
- }
266
- if (this.config.maxTokens !== void 0) {
267
- body.max_tokens = this.config.maxTokens;
268
- }
269
- if (this.config.extraBody) {
270
- Object.assign(body, this.config.extraBody);
271
- }
272
- const res = await fetch(this.getEndpoint(), {
273
- method: "POST",
274
- headers: this.buildHeaders(),
275
- body: JSON.stringify(body),
276
- signal: options?.signal
277
- });
278
- if (!res.ok) {
279
- const text = await res.text().catch(() => "");
280
- this.checkContextOverflow(res.status, text);
281
- throw new Error(`LLM request failed: ${res.status} ${text}`);
282
- }
283
- const reader = res.body?.getReader();
284
- if (!reader) {
285
- const data = await res.json();
286
- const parsed = this.parseResponse(data);
287
- if (parsed.content) {
288
- yield { type: "llm_text", content: parsed.content };
289
- }
309
+ if (this.config.adapter === "custom" && this.config.transformRequest && this.config.transformResponse) {
310
+ const response = await this.chat(messages, tools, options);
311
+ if (response.reasoning) yield { type: "llm_thinking", content: response.reasoning };
312
+ if (response.content) yield { type: "llm_text", content: response.content };
313
+ yield { type: "llm_response", response };
290
314
  return;
291
315
  }
292
- const decoder = new TextDecoder();
293
- let buffer = "";
294
- let content = "";
295
- let reasoning = "";
296
- const toolCallMap = /* @__PURE__ */ new Map();
316
+ const request = this.createRequest(options?.signal);
297
317
  try {
298
- while (true) {
299
- const { done, value } = await reader.read();
300
- if (done) break;
301
- buffer += decoder.decode(value, { stream: true });
302
- const lines = buffer.split("\n");
303
- buffer = lines.pop() ?? "";
304
- for (const line of lines) {
305
- const trimmed = line.trim();
306
- if (!trimmed || !trimmed.startsWith("data:")) continue;
307
- const data = trimmed.slice(5).trim();
308
- if (data === "[DONE]") continue;
309
- try {
310
- const chunk = JSON.parse(data);
311
- const delta = chunk.choices?.[0]?.delta;
312
- if (!delta) continue;
313
- if (delta.content) {
314
- content += delta.content;
315
- yield { type: "llm_text", content: delta.content };
316
- }
317
- if (delta.reasoning_content) {
318
- reasoning += delta.reasoning_content;
319
- yield { type: "llm_thinking", content: delta.reasoning_content };
318
+ const body = {
319
+ model: this.config.model,
320
+ messages: this.messagesToOpenAI(messages),
321
+ stream: true,
322
+ ...this.config.collectPromptCacheUsage && { stream_options: { include_usage: true } }
323
+ };
324
+ if (tools.length > 0) {
325
+ body.tools = this.toolsToOpenAI(tools);
326
+ }
327
+ if (this.config.temperature !== void 0) {
328
+ body.temperature = this.config.temperature;
329
+ }
330
+ if (this.config.maxTokens !== void 0) {
331
+ body.max_tokens = this.config.maxTokens;
332
+ }
333
+ if (this.config.extraBody) {
334
+ Object.assign(body, this.config.extraBody);
335
+ }
336
+ const res = await this.fetchResponse({
337
+ method: "POST",
338
+ headers: this.buildHeaders(),
339
+ body: JSON.stringify(body),
340
+ signal: request.controller.signal
341
+ });
342
+ if (!res.ok) {
343
+ const text = await res.text().catch(() => "");
344
+ this.checkContextOverflow(res.status, text);
345
+ throw new Error(`LLM request failed: ${res.status} ${text}`);
346
+ }
347
+ const reader = res.body?.getReader();
348
+ if (!reader) {
349
+ const data = await res.json();
350
+ const parsed = this.parseResponse(data);
351
+ this.verified = true;
352
+ if (parsed.content) {
353
+ yield { type: "llm_text", content: parsed.content };
354
+ }
355
+ yield { type: "llm_response", response: parsed };
356
+ return;
357
+ }
358
+ const decoder = new TextDecoder();
359
+ let buffer = "";
360
+ let content = "";
361
+ let reasoning = "";
362
+ let finishReason;
363
+ let promptCacheUsage;
364
+ let sawDone = false;
365
+ const toolCallMap = /* @__PURE__ */ new Map();
366
+ try {
367
+ while (true) {
368
+ if (options?.signal?.aborted) {
369
+ this.throwIfAborted(options.signal);
370
+ }
371
+ const { done, value } = await this.readChunk(reader, request.controller);
372
+ if (done) {
373
+ buffer += decoder.decode();
374
+ if (!buffer.trim()) break;
375
+ buffer += "\n";
376
+ }
377
+ buffer += decoder.decode(value, { stream: true });
378
+ const lines = buffer.split("\n");
379
+ buffer = lines.pop() ?? "";
380
+ for (const line of lines) {
381
+ const trimmed = line.trim();
382
+ if (!trimmed || !trimmed.startsWith("data:")) continue;
383
+ const data = trimmed.slice(5).trim();
384
+ if (data === "[DONE]") {
385
+ sawDone = true;
386
+ continue;
320
387
  }
321
- if (delta.tool_calls) {
322
- for (const tc of delta.tool_calls) {
323
- const idx = tc.index ?? 0;
324
- if (!toolCallMap.has(idx)) {
325
- toolCallMap.set(idx, {
326
- id: tc.id ?? "",
327
- name: tc.function?.name ?? "",
328
- arguments: ""
329
- });
388
+ try {
389
+ const chunk = JSON.parse(data);
390
+ if (chunk.error) throw new Error(chunk.error.message ?? "LLM stream error");
391
+ if (this.config.collectPromptCacheUsage && chunk.usage) {
392
+ promptCacheUsage = this.parsePromptCacheUsage(chunk.usage) ?? promptCacheUsage;
393
+ }
394
+ if (chunk.choices?.[0]?.finish_reason) finishReason = chunk.choices[0].finish_reason;
395
+ const delta = chunk.choices?.[0]?.delta;
396
+ if (!delta) continue;
397
+ if (delta.content) {
398
+ content += delta.content;
399
+ yield { type: "llm_text", content: delta.content };
400
+ }
401
+ if (delta.reasoning_content) {
402
+ reasoning += delta.reasoning_content;
403
+ yield { type: "llm_thinking", content: delta.reasoning_content };
404
+ }
405
+ if (delta.tool_calls) {
406
+ for (const tc of delta.tool_calls) {
407
+ const idx = tc.index ?? 0;
408
+ if (!toolCallMap.has(idx)) {
409
+ toolCallMap.set(idx, {
410
+ id: tc.id ?? "",
411
+ name: tc.function?.name ?? "",
412
+ arguments: ""
413
+ });
414
+ }
415
+ const entry = toolCallMap.get(idx);
416
+ if (tc.id) entry.id = tc.id;
417
+ if (tc.function?.name) entry.name = tc.function.name;
418
+ if (tc.function?.arguments) entry.arguments += tc.function.arguments;
330
419
  }
331
- const entry = toolCallMap.get(idx);
332
- if (tc.id) entry.id = tc.id;
333
- if (tc.function?.name) entry.name = tc.function.name;
334
- if (tc.function?.arguments) entry.arguments += tc.function.arguments;
335
420
  }
421
+ } catch (error) {
422
+ throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, `Invalid LLM stream: ${errorMessage(error)}`);
423
+ }
424
+ if (options?.signal?.aborted) {
425
+ this.throwIfAborted(options.signal);
336
426
  }
337
- } catch {
338
427
  }
428
+ if (done || sawDone) break;
339
429
  }
430
+ } finally {
431
+ await reader.cancel().catch(() => {
432
+ });
433
+ reader.releaseLock();
434
+ }
435
+ const toolCalls = [];
436
+ for (const [_, tc] of toolCallMap) {
437
+ toolCalls.push(this.parseToolCall(tc));
340
438
  }
439
+ throwIfAborted(request.controller.signal);
440
+ if (this.config.strictStream && !sawDone && !finishReason) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "LLM stream ended without a finish marker");
441
+ if (finishReason === "length") for (const call of toolCalls) call.parseError = "Tool response exceeded the output token limit";
442
+ if (content || toolCalls.length) this.verified = true;
443
+ yield {
444
+ type: "llm_response",
445
+ response: {
446
+ content,
447
+ reasoning: reasoning || void 0,
448
+ toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
449
+ usage: void 0,
450
+ ...promptCacheUsage && { promptCacheUsage },
451
+ finishReason: finishReason ?? (toolCalls.length > 0 ? "tool_calls" : "stop")
452
+ }
453
+ };
454
+ } catch (error) {
455
+ if (request.controller.signal.aborted && !options?.signal?.aborted) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "LLM request timed out", { reason: "timeout" });
456
+ throw error;
341
457
  } finally {
342
- reader.releaseLock();
458
+ request.cleanup();
459
+ }
460
+ }
461
+ parseToolCall(tc) {
462
+ if (!tc.id || !tc.name) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "Tool call is missing its id or name");
463
+ try {
464
+ const args = JSON.parse(tc.arguments || "{}");
465
+ if (!args || typeof args !== "object" || Array.isArray(args)) throw new Error("Tool arguments must be a JSON object");
466
+ return { id: tc.id, name: tc.name, arguments: args };
467
+ } catch (error) {
468
+ return { id: tc.id, name: tc.name, arguments: {}, parseError: errorMessage(error) };
469
+ }
470
+ }
471
+ createRequest(parent) {
472
+ const controller = new AbortController();
473
+ const unlink = linkAbort(parent, controller);
474
+ const timeout = this.config.requestTimeoutMs;
475
+ const timer = timeout && timeout > 0 ? setTimeout(() => controller.abort(), timeout) : void 0;
476
+ return { controller, cleanup: () => {
477
+ clearTimeout(timer);
478
+ unlink();
479
+ controller.abort();
480
+ } };
481
+ }
482
+ async fetchResponse(init) {
483
+ const retries = Math.max(0, Math.min(this.config.retry?.maxRetries ?? 0, 5));
484
+ const requestInit = this.options?.rejectRedirects ? { ...init, redirect: "error" } : init;
485
+ for (let attempt = 0; ; attempt++) {
486
+ throwIfAborted(init.signal ?? void 0);
487
+ const response = await fetch(this.getEndpoint(), requestInit);
488
+ if (attempt >= retries || ![429, 502, 503, 504].includes(response.status)) return response;
489
+ const retryAfter = Number(response.headers.get("retry-after"));
490
+ const delay = Number.isFinite(retryAfter) && retryAfter > 0 ? Math.min(retryAfter * 1e3, 3e4) : Math.min((this.config.retry?.baseDelayMs ?? 500) * 2 ** attempt, 3e4);
491
+ await response.body?.cancel();
492
+ await abortableDelay(delay, init.signal ?? void 0);
493
+ }
494
+ }
495
+ async readChunk(reader, controller) {
496
+ const idleMs = this.config.streamIdleTimeoutMs;
497
+ if (!idleMs || idleMs <= 0) return reader.read();
498
+ let timer;
499
+ try {
500
+ return await Promise.race([reader.read(), new Promise((_, reject) => {
501
+ timer = setTimeout(() => {
502
+ reject(new Error("LLM stream idle timeout"));
503
+ controller.abort();
504
+ }, idleMs);
505
+ })]);
506
+ } finally {
507
+ clearTimeout(timer);
343
508
  }
344
- const toolCalls = [];
345
- for (const [_, tc] of toolCallMap) {
346
- let args = {};
347
- try {
348
- args = JSON.parse(tc.arguments || "{}");
349
- } catch {
350
- args = {};
351
- }
352
- toolCalls.push({ id: tc.id, name: tc.name, arguments: args });
353
- }
354
- yield {
355
- type: "llm_response",
356
- response: {
357
- content,
358
- reasoning: reasoning || void 0,
359
- toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
360
- usage: void 0,
361
- finishReason: toolCalls.length > 0 ? "tool_calls" : "stop"
362
- }
363
- };
364
509
  }
365
510
  /**
366
511
  * Check if the error response indicates context length exceeded.
@@ -378,6 +523,14 @@ var OpenAIAdapter = class {
378
523
  throw new ACPError(ACPErrorCodes.CONTEXT_OVERFLOW, errMsg, { isContextOverflow: true });
379
524
  }
380
525
  }
526
+ /** AbortError for the given signal — used so a cancelled stream surfaces an abort, not a parse error. */
527
+ throwIfAborted(signal) {
528
+ if (signal?.aborted) {
529
+ const err = new Error("Request aborted");
530
+ err.name = "AbortError";
531
+ throw err;
532
+ }
533
+ }
381
534
  parseResponse(data) {
382
535
  const choice = data.choices?.[0];
383
536
  if (!choice) {
@@ -389,17 +542,9 @@ var OpenAIAdapter = class {
389
542
  const toolCalls = [];
390
543
  if (message?.tool_calls?.length) {
391
544
  for (const tc of message.tool_calls) {
392
- let args = {};
393
- try {
394
- args = JSON.parse(tc.function.arguments || "{}");
395
- } catch {
396
- args = {};
397
- }
398
- toolCalls.push({
399
- id: tc.id,
400
- name: tc.function.name,
401
- arguments: args
402
- });
545
+ const parsed = this.parseToolCall({ id: tc.id, name: tc.function?.name, arguments: tc.function?.arguments });
546
+ if (choice.finish_reason === "length") parsed.parseError = "Tool response exceeded the output token limit";
547
+ toolCalls.push(parsed);
403
548
  }
404
549
  }
405
550
  if (!content && !toolCalls.length) {
@@ -408,16 +553,70 @@ var OpenAIAdapter = class {
408
553
  "LLM returned empty response with no tool calls"
409
554
  );
410
555
  }
556
+ const promptCacheUsage = this.config.collectPromptCacheUsage ? this.parsePromptCacheUsage(data.usage) : void 0;
411
557
  return {
412
558
  content,
413
559
  reasoning,
414
560
  toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
415
561
  usage: data.usage ? { input: data.usage.prompt_tokens, output: data.usage.completion_tokens } : void 0,
562
+ ...promptCacheUsage && { promptCacheUsage },
416
563
  finishReason: choice.finish_reason
417
564
  };
418
565
  }
566
+ parsePromptCacheUsage(raw) {
567
+ const asCount = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : void 0;
568
+ const cachedTokens = asCount(raw?.prompt_tokens_details?.cached_tokens) ?? asCount(raw?.prompt_cache_hit_tokens);
569
+ const cacheWriteTokens = asCount(raw?.prompt_tokens_details?.cache_write_tokens);
570
+ if (cachedTokens === void 0 && cacheWriteTokens === void 0) return void 0;
571
+ const inputTokens = asCount(raw?.prompt_tokens);
572
+ return {
573
+ ...inputTokens !== void 0 && { inputTokens },
574
+ ...cachedTokens !== void 0 && { cachedTokens },
575
+ ...cacheWriteTokens !== void 0 && { cacheWriteTokens }
576
+ };
577
+ }
419
578
  };
420
579
 
580
+ // src/llm/proxy.ts
581
+ function invalidProxyConfig(message) {
582
+ return new ACPError(ACPErrorCodes.LLM_PROXY_CONFIG_INVALID, message);
583
+ }
584
+ function proxyBaseUrl(value, field) {
585
+ const input = typeof value === "string" ? value.trim() : "";
586
+ const absolute = /^https?:\/\//i.test(input);
587
+ const relative = input.startsWith("/") && !input.startsWith("//");
588
+ if (!absolute && !relative || /[\\?#\u0000-\u001f]/.test(input)) {
589
+ throw invalidProxyConfig(`${field} must be an HTTP(S) base URL or a same-origin path without a query or fragment`);
590
+ }
591
+ let url;
592
+ try {
593
+ url = new URL(input, "http://listdoing.invalid");
594
+ } catch {
595
+ throw invalidProxyConfig(`${field} is not a valid proxy URL`);
596
+ }
597
+ if (url.username || url.password) throw invalidProxyConfig(`${field} must not include credentials`);
598
+ if (!absolute && url.pathname.startsWith("//")) throw invalidProxyConfig(`${field} must stay on the same origin`);
599
+ const normalized = (absolute ? url.href : url.pathname).replace(/\/+$/, "");
600
+ return normalized || "/";
601
+ }
602
+ function resolveProxyConfig(config) {
603
+ const mode = config.llmProxyMode;
604
+ if (mode === void 0) return { llm: config.llm, serverUrl: config.serverUrl };
605
+ if (mode !== "backend" && mode !== "listdoing-server") {
606
+ throw invalidProxyConfig("llmProxyMode must be backend or listdoing-server");
607
+ }
608
+ const serverUrl = config.serverUrl !== void 0 || mode === "listdoing-server" ? proxyBaseUrl(config.serverUrl, "serverUrl") : void 0;
609
+ const baseUrl = mode === "listdoing-server" ? `${serverUrl.replace(/\/+$/, "")}/v1` : proxyBaseUrl(config.llm.baseUrl, "llm.baseUrl");
610
+ if (mode === "listdoing-server" && config.llm.baseUrl !== void 0 && proxyBaseUrl(config.llm.baseUrl, "llm.baseUrl") !== baseUrl) {
611
+ throw invalidProxyConfig("llm.baseUrl must match the ListDoing Server proxy or be omitted");
612
+ }
613
+ if (baseUrl.endsWith("/chat/completions")) {
614
+ throw invalidProxyConfig("llm.baseUrl must end before /chat/completions");
615
+ }
616
+ const apiKey = config.llm.apiKey ?? (mode === "listdoing-server" ? config.authToken : void 0);
617
+ return { llm: { ...config.llm, baseUrl, apiKey }, serverUrl };
618
+ }
619
+
421
620
  // src/skill/registry.ts
422
621
  var SkillRegistry = class {
423
622
  constructor() {
@@ -438,6 +637,11 @@ var SkillRegistry = class {
438
637
  get(name) {
439
638
  return this.skills.get(name)?.definition;
440
639
  }
640
+ /** Identity of this registration, replaced on every register call. */
641
+ getRegistrationIdentity(skill) {
642
+ const entry = this.skills.get(skill.name);
643
+ return entry?.definition === skill ? entry : skill;
644
+ }
441
645
  getHandler(name) {
442
646
  return this.skills.get(name)?.handler;
443
647
  }
@@ -492,7 +696,9 @@ var SkillRegistry = class {
492
696
 
493
697
  // src/skill/executor.ts
494
698
  var SkillExecutor = class {
495
- constructor(registry, worker) {
699
+ constructor(registry, worker, options) {
700
+ this.options = options;
701
+ this.validators = /* @__PURE__ */ new WeakMap();
496
702
  this.registry = registry;
497
703
  this.worker = worker ?? {
498
704
  async execute(def, params, ctx) {
@@ -520,19 +726,41 @@ var SkillExecutor = class {
520
726
  throw new ACPError(ACPErrorCodes.SKILL_NOT_FOUND, `Skill not found: ${name}`, { name });
521
727
  }
522
728
  const timeout = def.timeout ?? 3e4;
729
+ throwIfAborted(context.signal);
730
+ if (this.options?.validateInputs) {
731
+ const validate = await this.getValidator(def);
732
+ if (!validate(params)) throw new ACPError(ACPErrorCodes.SKILL_EXECUTION_FAILED, `Invalid arguments for ${name}`, { errors: validate.errors });
733
+ }
523
734
  const controller = new AbortController();
524
- const onParentAbort = () => controller.abort();
525
- context.signal.addEventListener("abort", onParentAbort);
526
- const timeoutId = timeout > 0 ? setTimeout(() => controller.abort(), timeout) : null;
735
+ const unlink = linkAbort(context.signal, controller);
736
+ let timedOut = false;
737
+ const timeoutId = timeout > 0 ? setTimeout(() => {
738
+ timedOut = true;
739
+ controller.abort();
740
+ }, timeout) : null;
527
741
  const execContext = {
528
742
  ...context,
529
743
  signal: controller.signal
530
744
  };
745
+ let removeDeadline = () => {
746
+ };
531
747
  try {
532
- const result = await this.worker.execute(def, params, execContext);
748
+ const execution = Promise.resolve().then(() => {
749
+ throwIfAborted(controller.signal);
750
+ return this.worker.execute(def, params, execContext);
751
+ });
752
+ const result = def.timeoutMode === "deadline" ? await Promise.race([execution, new Promise((_, reject) => {
753
+ const abort = () => reject(abortError());
754
+ controller.signal.addEventListener("abort", abort, { once: true });
755
+ removeDeadline = () => controller.signal.removeEventListener("abort", abort);
756
+ })]) : await execution;
757
+ throwIfAborted(context.signal);
758
+ if (timedOut) throw new Error("Skill exceeded its timeout");
759
+ if (!result || !Array.isArray(result.content)) throw new Error("Skill returned an invalid result (content must be an array)");
533
760
  return result;
534
761
  } catch (err) {
535
- if (err.name === "AbortError" || controller.signal.aborted) {
762
+ if (context.signal.aborted) throw abortError();
763
+ if (timedOut) {
536
764
  throw new ACPError(
537
765
  ACPErrorCodes.SKILL_EXECUTION_TIMEOUT,
538
766
  `Skill execution timeout: ${name}`,
@@ -542,19 +770,44 @@ var SkillExecutor = class {
542
770
  if (err instanceof ACPError) throw err;
543
771
  throw new ACPError(
544
772
  ACPErrorCodes.SKILL_EXECUTION_FAILED,
545
- `Skill execution failed: ${name}: ${err.message}`,
546
- { name, error: err.message }
773
+ `Skill execution failed: ${name}: ${errorMessage(err)}`,
774
+ { name, error: errorMessage(err) }
547
775
  );
548
776
  } finally {
549
777
  if (timeoutId) clearTimeout(timeoutId);
550
- context.signal.removeEventListener("abort", onParentAbort);
778
+ unlink();
779
+ removeDeadline();
780
+ }
781
+ }
782
+ async getValidator(skill) {
783
+ const { default: Ajv } = await import("ajv");
784
+ const registration = this.registry.getRegistrationIdentity(skill);
785
+ const schema = skill.inputSchema;
786
+ let signature;
787
+ try {
788
+ signature = JSON.stringify(schema, function(key, value) {
789
+ if (value !== this[key]) throw new TypeError("Schema cannot be cached");
790
+ if (value === null || typeof value === "string" || typeof value === "boolean" || Number.isFinite(value)) return value;
791
+ if (typeof value === "object") {
792
+ const prototype = Object.getPrototypeOf(value);
793
+ if (prototype === Object.prototype || prototype === null || prototype === Array.prototype) return value;
794
+ }
795
+ throw new TypeError("Schema cannot be cached");
796
+ });
797
+ } catch {
551
798
  }
799
+ const cached = this.validators.get(registration);
800
+ if (cached && cached.schema === signature) return cached.validate;
801
+ const validate = new Ajv({ strict: false, allErrors: true }).compile(schema);
802
+ if (signature !== void 0) this.validators.set(registration, { schema: signature, validate });
803
+ return validate;
552
804
  }
553
805
  /**
554
806
  * Directly invoke a skill by name — used by ctx.invoke().
555
807
  * No LLM loop, no approval. Shares parent's signal and timeout context.
556
808
  */
557
809
  async invokeDirect(name, params, context) {
810
+ throwIfAborted(context.signal);
558
811
  const def = this.registry.get(name);
559
812
  if (!def) {
560
813
  return {
@@ -578,8 +831,9 @@ var SkillExecutor = class {
578
831
  try {
579
832
  return await handler(params, context);
580
833
  } catch (err) {
834
+ if (context.signal.aborted || err?.name === "AbortError") throw abortError();
581
835
  return {
582
- content: [{ type: "text", text: `Invoke failed: ${err.message}` }],
836
+ content: [{ type: "text", text: `Invoke failed: ${errorMessage(err)}` }],
583
837
  isError: true
584
838
  };
585
839
  }
@@ -755,13 +1009,13 @@ var McpRouter = class {
755
1009
  /**
756
1010
  * Route a tool call to the appropriate MCP Server.
757
1011
  */
758
- async call(prefixedName, args) {
1012
+ async call(prefixedName, args, options) {
759
1013
  const { server, tool } = this.parseMcpToolName(prefixedName);
760
1014
  const conn = this.connections.get(server);
761
1015
  if (!conn) {
762
1016
  throw new Error(`MCP Server "${server}" not connected. Available: ${Array.from(this.connections.keys()).join(", ") || "none"}`);
763
1017
  }
764
- return conn.call(tool, args);
1018
+ return conn.call(tool, args, options);
765
1019
  }
766
1020
  /**
767
1021
  * Resolve `mcp:` prefixed tool names from a tools list to ToolDef[].
@@ -834,7 +1088,7 @@ function fileInputsToContentBlocks(files) {
834
1088
  }
835
1089
 
836
1090
  // src/session/manager.ts
837
- var SessionManager = class {
1091
+ var SessionManager = class _SessionManager {
838
1092
  constructor() {
839
1093
  this.sessions = /* @__PURE__ */ new Map();
840
1094
  }
@@ -860,6 +1114,9 @@ var SessionManager = class {
860
1114
  get(sessionId) {
861
1115
  return this.sessions.get(sessionId);
862
1116
  }
1117
+ restore(session) {
1118
+ this.sessions.set(session.id, session);
1119
+ }
863
1120
  getOrThrow(sessionId) {
864
1121
  const session = this.sessions.get(sessionId);
865
1122
  if (!session) {
@@ -921,13 +1178,16 @@ var SessionManager = class {
921
1178
  session.status = "archived";
922
1179
  session.updatedAt = Date.now();
923
1180
  }
924
- merge(fromId, toId, strategy = "summary_and_artifacts") {
1181
+ merge(fromId, toId, strategy) {
925
1182
  const from = this.getOrThrow(fromId);
926
1183
  const to = this.getOrThrow(toId);
1184
+ _SessionManager.mergeContent(from, to, strategy);
1185
+ }
1186
+ static mergeContent(from, to, strategy = "summary_and_artifacts") {
927
1187
  if (strategy === "full") {
928
1188
  to.messages.push(...from.messages);
929
1189
  } else {
930
- const summary = this.createSummary(from);
1190
+ const summary = _SessionManager.createSummary(from);
931
1191
  to.messages.push({
932
1192
  role: "user",
933
1193
  content: `[Context from session "${from.title || from.id}"]:
@@ -949,7 +1209,7 @@ ${artifacts.map((a) => contentToPreview(a.content, 200)).join("\n")}`
949
1209
  to.messageCount = to.messages.length;
950
1210
  to.updatedAt = Date.now();
951
1211
  }
952
- createSummary(session) {
1212
+ static createSummary(session) {
953
1213
  const userMsgs = session.messages.filter((m) => m.role === "user");
954
1214
  if (userMsgs.length === 0) return "Empty session";
955
1215
  return userMsgs.slice(-3).map((m) => contentToPreview(m.content, 200)).join("\n");
@@ -1076,6 +1336,64 @@ function estimateRequestTokens(messages, tools, systemPrompt) {
1076
1336
  return total;
1077
1337
  }
1078
1338
 
1339
+ // src/context/messages.ts
1340
+ function repairMessages(messages) {
1341
+ const result = [];
1342
+ for (let i = 0; i < messages.length; i++) {
1343
+ const message = messages[i];
1344
+ if (!message || !["user", "assistant", "system", "tool"].includes(message.role)) continue;
1345
+ if (message.role === "tool") continue;
1346
+ if (message.role === "assistant" && message.toolCalls?.length) {
1347
+ const tools = [];
1348
+ while (i + 1 < messages.length && messages[i + 1]?.role === "tool") tools.push(messages[++i]);
1349
+ const ids = /* @__PURE__ */ new Set();
1350
+ const calls = message.toolCalls.filter((call) => {
1351
+ if (!call?.id || !call.name || ids.has(call.id)) return false;
1352
+ ids.add(call.id);
1353
+ return true;
1354
+ });
1355
+ if (calls.length) {
1356
+ result.push({ ...message, toolCalls: calls });
1357
+ for (const call of calls) {
1358
+ const tool = tools.find((tool2) => tool2.toolCallId === call.id);
1359
+ result.push(tool ? normalizeContent(tool) : {
1360
+ role: "tool",
1361
+ toolCallId: call.id,
1362
+ content: "Tool execution was interrupted; its outcome is unknown. Do not repeat a side-effecting operation without checking its state."
1363
+ });
1364
+ }
1365
+ } else if (message.content) result.push(normalizeContent({ ...message, toolCalls: void 0 }));
1366
+ } else result.push(normalizeContent(message));
1367
+ }
1368
+ return result;
1369
+ }
1370
+ function normalizeContent(message) {
1371
+ const content = message.content;
1372
+ if (content == null || content === "" || Array.isArray(content) && content.length === 0) {
1373
+ return { ...message, content: "." };
1374
+ }
1375
+ if (typeof content !== "string") {
1376
+ if (!Array.isArray(content)) return { ...message, content: "." };
1377
+ const valid = content.filter((block) => {
1378
+ if (!block || typeof block !== "object") return false;
1379
+ if (block.type === "text") return typeof block.text === "string";
1380
+ if (block.type === "image") return typeof block.data === "string" && !!block.data && typeof block.mimeType === "string";
1381
+ if (block.type === "resource") return typeof block.resource?.uri === "string" && (block.resource.text === void 0 || typeof block.resource.text === "string");
1382
+ return false;
1383
+ });
1384
+ if (valid.length !== content.length) return { ...message, content: valid.length ? valid : "." };
1385
+ }
1386
+ return message;
1387
+ }
1388
+ function messageGroups(messages) {
1389
+ const groups = [];
1390
+ for (const message of repairMessages(messages)) {
1391
+ if (message.role === "tool" && groups.length) groups[groups.length - 1].push(message);
1392
+ else groups.push([message]);
1393
+ }
1394
+ return groups;
1395
+ }
1396
+
1079
1397
  // src/context/manager.ts
1080
1398
  var ContextManager = class {
1081
1399
  constructor(config, model) {
@@ -1128,26 +1446,21 @@ var ContextManager = class {
1128
1446
  if (systemPrompt) {
1129
1447
  result.push({ role: "system", content: systemPrompt });
1130
1448
  }
1131
- const sanitized = messages.map((m) => {
1132
- if (m.role === "assistant" && !m.content && !m.toolCalls?.length) {
1133
- return { ...m, content: "." };
1134
- }
1135
- return m;
1136
- });
1137
- const reversed = [...sanitized].reverse();
1138
1449
  const selected = [];
1139
1450
  let usedByMessages = 0;
1140
- for (const msg of reversed) {
1141
- const msgTokens = estimateMessageTokens(msg);
1142
- if (usedByMessages + msgTokens > remainingBudget) break;
1143
- selected.unshift(msg);
1144
- usedByMessages += msgTokens;
1451
+ for (const group of messageGroups(messages).reverse()) {
1452
+ const tokens = group.reduce((sum, msg) => sum + estimateMessageTokens(msg), 0);
1453
+ if (usedByMessages + tokens > remainingBudget) break;
1454
+ selected.unshift(...group);
1455
+ usedByMessages += tokens;
1145
1456
  }
1146
1457
  result.push(...selected);
1147
1458
  if (selected.length === 0 && messages.length > 0) {
1148
1459
  const lastUser = [...messages].reverse().find((m) => m.role === "user");
1149
1460
  if (lastUser) {
1150
- result.push(lastUser);
1461
+ const fallback = repairMessages([lastUser]);
1462
+ result.push(...fallback);
1463
+ usedByMessages += fallback.reduce((sum, message) => sum + estimateMessageTokens(message), 0);
1151
1464
  }
1152
1465
  }
1153
1466
  return { messages: result, usedTokens: fixedTokens + usedByMessages, budget };
@@ -1158,37 +1471,64 @@ var ContextManager = class {
1158
1471
  * summarization (when a summarizer is set and enabled) over mechanical truncation.
1159
1472
  */
1160
1473
  async ensureContextFits(messages, systemPrompt, tools, model, signal) {
1474
+ throwIfAborted(signal);
1161
1475
  if (model) this.setModel(model);
1162
- const { usedTokens, budget } = this.buildWindow(messages, systemPrompt, tools);
1476
+ const { budget } = this.buildWindow(messages, systemPrompt, tools);
1477
+ const beforeTokens = estimateRequestTokens(repairMessages(messages), tools, systemPrompt);
1478
+ if (this.config.autoCompress === false) return { messages, wasCompressed: false };
1163
1479
  const threshold = this.config.compressThreshold ?? 0.85;
1164
- if (usedTokens / budget <= threshold) {
1480
+ if (beforeTokens / budget <= threshold) {
1165
1481
  return { messages, wasCompressed: false };
1166
1482
  }
1167
1483
  const useLLM = this.config.useLLMSummary !== false && !!this.summarizer;
1168
1484
  let result;
1169
1485
  if (useLLM) {
1170
- const llmResult = await this.compressWithLLM(messages, systemPrompt, tools, budget, signal);
1171
- result = llmResult ?? this.compressSync(messages, "hybrid");
1486
+ const llmResult = await this.compressWithLLM(messages, systemPrompt, tools, budget, { signal, groupTools: true });
1487
+ result = llmResult ?? this.compressSync(messages, "hybrid", { groupTools: true });
1172
1488
  } else {
1173
- result = this.compressSync(messages, "hybrid");
1489
+ result = this.compressSync(messages, "hybrid", { groupTools: true });
1174
1490
  }
1175
- const afterCheck = this.buildWindow(result.messages, systemPrompt, tools);
1176
- if (afterCheck.usedTokens / afterCheck.budget > 1) {
1177
- console.warn(`[listdoing] Context still exceeds budget after compression (${afterCheck.usedTokens}/${afterCheck.budget} tokens). Truncating to recent messages only.`);
1178
- const truncated = messages.slice(-3);
1179
- return {
1180
- messages: truncated,
1181
- wasCompressed: true,
1182
- beforeTokens: result.beforeTokens,
1183
- afterTokens: truncated.reduce((sum, m) => sum + estimateMessageTokens(m), 0)
1184
- };
1491
+ let compressed = result.messages;
1492
+ let window2 = this.buildWindow(compressed, systemPrompt, tools);
1493
+ if (window2.usedTokens > budget) {
1494
+ console.warn(`[listdoing] Context still exceeds budget after compression (${window2.usedTokens}/${budget} tokens). Truncating to recent messages only.`);
1495
+ compressed = messageGroups(messages).slice(-3).flat();
1496
+ window2 = this.buildWindow(compressed, systemPrompt, tools);
1497
+ }
1498
+ if (estimateRequestTokens(compressed, tools, systemPrompt) >= beforeTokens || window2.usedTokens >= beforeTokens) {
1499
+ return { messages, wasCompressed: false };
1185
1500
  }
1186
1501
  return {
1187
- messages: result.messages,
1502
+ messages: compressed,
1188
1503
  wasCompressed: true,
1189
- beforeTokens: result.beforeTokens,
1190
- afterTokens: result.afterTokens
1504
+ beforeTokens,
1505
+ afterTokens: window2.usedTokens
1506
+ };
1507
+ }
1508
+ /** Build one smaller request after a provider rejects the current window. */
1509
+ async compressForOverflow(messages, rejected, systemPrompt, tools, signal) {
1510
+ throwIfAborted(signal);
1511
+ const fixedTokens = estimateRequestTokens([], tools, systemPrompt);
1512
+ const available = Math.min(rejected.budget, rejected.usedTokens) - fixedTokens;
1513
+ const historyBudget = Math.floor(available * 0.85);
1514
+ if (historyBudget <= 0) return;
1515
+ const budget = fixedTokens + historyBudget;
1516
+ const latestUser = [...messages].reverse().find((message) => message.role === "user");
1517
+ if (latestUser && estimateMessageTokens(latestUser) > historyBudget) return;
1518
+ const rejectedUserIndex = latestUser ? rejected.messages.lastIndexOf(latestUser) : -1;
1519
+ const activeToolResults = rejected.messages.slice(rejectedUserIndex + 1).filter((message) => message.role === "tool");
1520
+ const summarized = this.config.useLLMSummary !== false ? await this.compressWithLLM(messages, systemPrompt, tools, budget, { signal, groupTools: true }) : null;
1521
+ throwIfAborted(signal);
1522
+ const useCandidate = (result) => {
1523
+ const window2 = this.buildWindow(result.messages, systemPrompt, tools, budget);
1524
+ if (window2.usedTokens > budget || window2.usedTokens >= rejected.usedTokens) return;
1525
+ if (latestUser && !window2.messages.some((message) => message.role === "user" && message.content === latestUser.content)) return;
1526
+ const keptToolIds = new Set(window2.messages.filter((message) => message.role === "tool").map((message) => message.toolCallId));
1527
+ if (activeToolResults.some((message) => !keptToolIds.has(message.toolCallId))) return;
1528
+ return { ...result, window: window2 };
1191
1529
  };
1530
+ const candidate = summarized && useCandidate(summarized);
1531
+ return candidate || useCandidate(this.compressSync(messages, "hybrid", { maxTokens: historyBudget, groupTools: true }));
1192
1532
  }
1193
1533
  /**
1194
1534
  * Compress context by summarizing older messages. When strategy is
@@ -1207,7 +1547,8 @@ var ContextManager = class {
1207
1547
  return this.compressSync(messages, strat);
1208
1548
  }
1209
1549
  /** Mechanical (non-LLM) compression — the original truncation strategies. */
1210
- compressSync(messages, strategy) {
1550
+ compressSync(messages, strategy, options) {
1551
+ messages = repairMessages(messages);
1211
1552
  const estimateTokens = (msgs) => msgs.reduce((sum, m) => sum + estimateMessageTokens(m), 0);
1212
1553
  const beforeTokens = estimateTokens(messages);
1213
1554
  let compressed;
@@ -1234,13 +1575,14 @@ ${summaryText}` },
1234
1575
  case "token_budget":
1235
1576
  case "hybrid":
1236
1577
  default: {
1237
- const maxTokens = this.config.maxTokens ?? this.getBudget();
1578
+ const maxTokens = options?.maxTokens ?? this.config.maxTokens ?? this.getBudget();
1238
1579
  let totalTokens = 0;
1239
1580
  const kept = [];
1240
- for (const msg of [...messages].reverse()) {
1241
- const t = estimateMessageTokens(msg);
1581
+ const groups = options?.groupTools ? messageGroups(messages) : messages.map((message) => [message]);
1582
+ for (const group of groups.reverse()) {
1583
+ const t = estimateTokens(group);
1242
1584
  if (totalTokens + t > maxTokens) break;
1243
- kept.unshift(msg);
1585
+ kept.unshift(...group);
1244
1586
  totalTokens += t;
1245
1587
  }
1246
1588
  if (kept.length < messages.length) {
@@ -1257,16 +1599,18 @@ ${summaryText}` },
1257
1599
  break;
1258
1600
  }
1259
1601
  }
1260
- const afterTokens = estimateTokens(compressed);
1261
- return { messages: compressed, beforeTokens, afterTokens };
1602
+ const repaired = repairMessages(compressed);
1603
+ return { messages: repaired, beforeTokens, afterTokens: estimateTokens(repaired) };
1262
1604
  }
1263
1605
  /**
1264
1606
  * LLM-based semantic summarization. Returns null when it cannot be performed
1265
1607
  * (no summarizer, nothing to summarize, or the LLM call fails) so callers can
1266
1608
  * fall back to mechanical compression.
1267
1609
  */
1268
- async compressWithLLM(messages, systemPrompt, tools, budget, signal) {
1610
+ async compressWithLLM(messages, systemPrompt, tools, budget, options) {
1269
1611
  if (!this.summarizer) return null;
1612
+ const { signal, groupTools } = options ?? {};
1613
+ messages = repairMessages(messages);
1270
1614
  try {
1271
1615
  const estimateTokens = (msgs) => msgs.reduce((sum, m) => sum + estimateMessageTokens(m), 0);
1272
1616
  const beforeTokens = estimateTokens(messages);
@@ -1276,33 +1620,31 @@ ${summaryText}` },
1276
1620
  const summaryReserve = 2e3;
1277
1621
  const outputReserve = this.config.outputReserve ?? 1024;
1278
1622
  const remaining = budget - fixedTokens - summaryReserve - outputReserve;
1623
+ const groups = groupTools ? messageGroups(messages) : messages.map((message) => [message]);
1279
1624
  const kept = [];
1280
1625
  let used = 0;
1281
- if (remaining > 0) {
1282
- for (let i = messages.length - 1; i >= 0; i--) {
1283
- const t = estimateMessageTokens(messages[i]);
1284
- if (used + t > remaining) break;
1285
- kept.unshift(messages[i]);
1286
- used += t;
1287
- }
1626
+ for (let i = groups.length - 1; i >= 0; i--) {
1627
+ const t = estimateTokens(groups[i]);
1628
+ if (used + t > remaining) break;
1629
+ kept.unshift(groups[i]);
1630
+ used += t;
1288
1631
  }
1289
- const oldMessages = messages.slice(0, messages.length - kept.length);
1632
+ const oldMessages = groups.slice(0, groups.length - kept.length).flat();
1290
1633
  if (oldMessages.length === 0) {
1291
1634
  return null;
1292
1635
  }
1293
1636
  const summary = await this.summarizer(oldMessages, signal ? { signal } : void 0);
1294
1637
  const summaryMessage = { role: "user", content: `[\u4E0A\u4E0B\u6587\u6458\u8981]
1295
1638
  ${summary}` };
1296
- let result = [summaryMessage, ...kept];
1297
- let afterTokens = estimateTokens(result);
1298
- while (result.length > 2 && afterTokens > budget) {
1299
- const trimmed = [summaryMessage, ...result.slice(2)];
1300
- if (trimmed.length === result.length) break;
1301
- result = trimmed;
1302
- afterTokens = estimateTokens(result);
1639
+ const limit = groupTools ? budget - fixedTokens : budget;
1640
+ let afterTokens = estimateMessageTokens(summaryMessage) + used;
1641
+ while (kept.length > 1 && afterTokens > limit) {
1642
+ afterTokens -= estimateTokens(kept.shift());
1303
1643
  }
1304
- return { messages: result, beforeTokens, afterTokens };
1644
+ const repaired = repairMessages([summaryMessage, ...kept.flat()]);
1645
+ return { messages: repaired, beforeTokens, afterTokens: estimateTokens(repaired) };
1305
1646
  } catch (err) {
1647
+ throwIfAborted(signal);
1306
1648
  console.warn("[listdoing] LLM summarization failed, falling back to mechanical compression:", err);
1307
1649
  return null;
1308
1650
  }
@@ -1389,6 +1731,7 @@ function createLLMSummarizer(adapter, options) {
1389
1731
  return async (messages, opts) => {
1390
1732
  const formatted = formatMessagesForSummary(messages);
1391
1733
  const controller = new AbortController();
1734
+ const unlink = linkAbort(opts?.signal, controller);
1392
1735
  const timer = setTimeout(() => controller.abort(), timeoutMs);
1393
1736
  try {
1394
1737
  const response = await adapter.chat(
@@ -1397,7 +1740,7 @@ function createLLMSummarizer(adapter, options) {
1397
1740
  { role: "user", content: formatted }
1398
1741
  ],
1399
1742
  [],
1400
- { signal: opts?.signal ?? controller.signal }
1743
+ { signal: controller.signal }
1401
1744
  );
1402
1745
  const summary = (response.content ?? "").trim();
1403
1746
  if (!summary) {
@@ -1406,6 +1749,7 @@ function createLLMSummarizer(adapter, options) {
1406
1749
  return summary;
1407
1750
  } finally {
1408
1751
  clearTimeout(timer);
1752
+ unlink();
1409
1753
  }
1410
1754
  };
1411
1755
  }
@@ -1415,12 +1759,13 @@ var TaskPlanner = class {
1415
1759
  constructor(llmAdapter) {
1416
1760
  this.llmAdapter = llmAdapter;
1417
1761
  }
1418
- async plan(goal, availableTools) {
1762
+ async plan(goal, availableTools, signal) {
1419
1763
  const prompt = this.buildPlanningPrompt(goal, availableTools);
1420
1764
  const response = await this.llmAdapter.chat(
1421
1765
  [{ role: "user", content: prompt }],
1422
- []
1766
+ [],
1423
1767
  // No tools for the planning call itself
1768
+ { signal }
1424
1769
  );
1425
1770
  return this.parsePlan(goal, response.content);
1426
1771
  }
@@ -1739,17 +2084,19 @@ var GraphPlanner = class {
1739
2084
  constructor(llmAdapter) {
1740
2085
  this.llmAdapter = llmAdapter;
1741
2086
  }
1742
- async planGraph(goal, availableTools) {
2087
+ async planGraph(goal, availableTools, signal) {
1743
2088
  const prompt = this.buildGraphPlanningPrompt(goal, availableTools);
1744
2089
  let response;
1745
2090
  try {
1746
2091
  const result = await this.llmAdapter.chat(
1747
2092
  [{ role: "user", content: prompt }],
1748
- []
2093
+ [],
1749
2094
  // No tools for the planning call
2095
+ { signal }
1750
2096
  );
1751
2097
  response = result.content;
1752
2098
  } catch {
2099
+ throwIfAborted(signal);
1753
2100
  return this.fallbackSequentialGraph(goal);
1754
2101
  }
1755
2102
  return this.parseGraphPlan(goal, response);
@@ -1872,9 +2219,7 @@ var SubAgent = class {
1872
2219
  this.config = options.config;
1873
2220
  this.depth = options.depth;
1874
2221
  this.abortController = new AbortController();
1875
- if (options.parentSignal) {
1876
- options.parentSignal.addEventListener("abort", () => this.abortController.abort(), { once: true });
1877
- }
2222
+ this.unlinkParent = linkAbort(options.parentSignal, this.abortController);
1878
2223
  const maxDepth = this.config.maxDepth ?? 2;
1879
2224
  if (this.depth >= maxDepth) {
1880
2225
  this.config.canSpawnSubAgents = false;
@@ -1885,7 +2230,9 @@ var SubAgent = class {
1885
2230
  this.status = "running";
1886
2231
  const startTime = Date.now();
1887
2232
  try {
2233
+ throwIfAborted(this.abortController.signal);
1888
2234
  const session = this.parentAgent.createSession({
2235
+ activate: false,
1889
2236
  title: `SubAgent: ${this.nodeId}`,
1890
2237
  meta: {
1891
2238
  parentSessionId: this.parentAgent.getCurrentSessionId(),
@@ -1899,6 +2246,7 @@ var SubAgent = class {
1899
2246
  const prompt = this.buildPrompt(goal, upstreamOutputs);
1900
2247
  const options = {
1901
2248
  sessionId: session.id,
2249
+ signal: this.abortController.signal,
1902
2250
  maxTurns: this.config.maxTurns ?? 10,
1903
2251
  depth: this.depth + 1,
1904
2252
  taskMode: void 0
@@ -1922,6 +2270,7 @@ var SubAgent = class {
1922
2270
  sessionId: this.sessionId ?? void 0
1923
2271
  };
1924
2272
  }
2273
+ throwIfAborted(this.abortController.signal);
1925
2274
  this.status = "done";
1926
2275
  return {
1927
2276
  nodeId: this.nodeId,
@@ -1943,6 +2292,8 @@ var SubAgent = class {
1943
2292
  retryCount: 0,
1944
2293
  sessionId: this.sessionId ?? void 0
1945
2294
  };
2295
+ } finally {
2296
+ this.unlinkParent();
1946
2297
  }
1947
2298
  }
1948
2299
  /** Abort the sub-agent */
@@ -1987,42 +2338,49 @@ var GraphExecutor = class {
1987
2338
  this.maxDepth = options.maxDepth ?? 2;
1988
2339
  this.baseDepth = options.baseDepth ?? 0;
1989
2340
  this.abortController = new AbortController();
2341
+ this.unlinkParent = linkAbort(options.parentSignal, this.abortController);
1990
2342
  this.emit = options.emit;
2343
+ this.onNodeSettled = options.onNodeSettled;
1991
2344
  }
1992
2345
  /** Execute the entire graph, level by level */
1993
2346
  async execute() {
1994
- this.plan.status = "executing";
1995
- for (let level = 0; level < this.graph.getLevelCount(); level++) {
1996
- if (this.abortController.signal.aborted) {
2347
+ try {
2348
+ this.plan.status = "executing";
2349
+ for (let level = 0; level < this.graph.getLevelCount(); level++) {
2350
+ if (this.abortController.signal.aborted) {
2351
+ this.plan.status = "failed";
2352
+ return this.plan;
2353
+ }
2354
+ const nodes = this.graph.getLevel(level);
2355
+ const nodeIds = nodes.map((n) => n.id);
2356
+ this.emit("graph:level_start", { level, nodeIds });
2357
+ await this.executeLevel(level, nodes);
2358
+ const levelResults = nodeIds.map((id) => this.plan.results[id]).filter(Boolean);
2359
+ this.emit("graph:level_complete", { level, results: levelResults });
2360
+ }
2361
+ const progress = this.getProgress();
2362
+ if (progress.failed === 0 && progress.done === progress.total) {
2363
+ this.plan.status = "completed";
2364
+ this.emit("graph:complete", { plan: this.plan, progress });
2365
+ } else if (progress.done > 0) {
2366
+ this.plan.status = "partial";
2367
+ this.emit("graph:complete", { plan: this.plan, progress });
2368
+ } else {
1997
2369
  this.plan.status = "failed";
1998
- return this.plan;
2370
+ const failedNodes = Object.entries(this.plan.results).filter(([, r]) => r.status === "failed").map(([id]) => id);
2371
+ this.emit("graph:failed", { plan: this.plan, failedNodes });
1999
2372
  }
2000
- const nodes = this.graph.getLevel(level);
2001
- const nodeIds = nodes.map((n) => n.id);
2002
- this.emit("graph:level_start", { level, nodeIds });
2003
- await this.executeLevel(level, nodes);
2004
- const levelResults = nodeIds.map((id) => this.plan.results[id]).filter(Boolean);
2005
- this.emit("graph:level_complete", { level, results: levelResults });
2006
- }
2007
- const progress = this.getProgress();
2008
- if (progress.failed === 0 && progress.done === progress.total) {
2009
- this.plan.status = "completed";
2010
- this.emit("graph:complete", { plan: this.plan, progress });
2011
- } else if (progress.done > 0) {
2012
- this.plan.status = "partial";
2013
- this.emit("graph:complete", { plan: this.plan, progress });
2014
- } else {
2015
- this.plan.status = "failed";
2016
- const failedNodes = Object.entries(this.plan.results).filter(([, r]) => r.status === "failed").map(([id]) => id);
2017
- this.emit("graph:failed", { plan: this.plan, failedNodes });
2373
+ this.plan.updatedAt = Date.now();
2374
+ return this.plan;
2375
+ } finally {
2376
+ this.unlinkParent();
2018
2377
  }
2019
- this.plan.updatedAt = Date.now();
2020
- return this.plan;
2021
2378
  }
2022
2379
  /** Execute a single level — nodes run concurrently (limited by maxConcurrency) */
2023
2380
  async executeLevel(level, nodes) {
2024
- const runnableNodes = nodes.filter((node) => this.shouldRun(node));
2381
+ const runnableNodes = nodes.filter((node) => this.plan.results[node.id]?.status !== "done" && this.shouldRun(node));
2025
2382
  for (const node of nodes) {
2383
+ if (this.plan.results[node.id]?.status === "done") continue;
2026
2384
  if (!runnableNodes.includes(node)) {
2027
2385
  this.plan.results[node.id] = {
2028
2386
  nodeId: node.id,
@@ -2038,12 +2396,20 @@ var GraphExecutor = class {
2038
2396
  (node) => semaphore.acquire().then(async (release) => {
2039
2397
  try {
2040
2398
  await this.executeNode(node);
2399
+ await this.onNodeSettled?.(node.id);
2400
+ } catch (error) {
2401
+ if (this.onNodeSettled) this.abort();
2402
+ throw error;
2041
2403
  } finally {
2042
2404
  release();
2043
2405
  }
2044
2406
  })
2045
2407
  );
2046
- await Promise.allSettled(promises);
2408
+ const outcomes = await Promise.allSettled(promises);
2409
+ if (this.onNodeSettled) {
2410
+ const failure = outcomes.find((result) => result.status === "rejected");
2411
+ if (failure) throw failure.reason;
2412
+ }
2047
2413
  }
2048
2414
  /** Execute a single node with retry + fallback */
2049
2415
  async executeNode(node) {
@@ -2122,7 +2488,8 @@ var GraphExecutor = class {
2122
2488
  willRetry: true
2123
2489
  });
2124
2490
  const delay = Math.min(backoffMs * Math.pow(2, attempt), maxBackoffMs);
2125
- await new Promise((resolve) => setTimeout(resolve, delay));
2491
+ await abortableDelay(delay, this.abortController.signal).catch(() => {
2492
+ });
2126
2493
  }
2127
2494
  }
2128
2495
  if (node.fallback) {
@@ -2181,8 +2548,9 @@ var GraphExecutor = class {
2181
2548
  depth: this.baseDepth
2182
2549
  });
2183
2550
  if (node.timeout) {
2551
+ let timer;
2184
2552
  const timeoutPromise = new Promise((_, reject) => {
2185
- setTimeout(() => reject(new ACPError(
2553
+ timer = setTimeout(() => reject(new ACPError(
2186
2554
  ACPErrorCodes.SUBAGENT_TIMEOUT,
2187
2555
  `Node "${node.id}" timed out after ${node.timeout}ms`
2188
2556
  )), node.timeout);
@@ -2202,6 +2570,8 @@ var GraphExecutor = class {
2202
2570
  duration: 0,
2203
2571
  retryCount: 0
2204
2572
  };
2573
+ } finally {
2574
+ clearTimeout(timer);
2205
2575
  }
2206
2576
  }
2207
2577
  return subAgent.execute(node.goal, upstreamOutputs);
@@ -2402,7 +2772,7 @@ var HttpTransport = class {
2402
2772
  }
2403
2773
  return headers;
2404
2774
  }
2405
- async send(method, params) {
2775
+ async send(method, params, options) {
2406
2776
  const id = ++this.requestId;
2407
2777
  const body = {
2408
2778
  jsonrpc: "2.0",
@@ -2414,21 +2784,31 @@ var HttpTransport = class {
2414
2784
  const res = await fetch(`${this.baseUrl}/mcp`, {
2415
2785
  method: "POST",
2416
2786
  headers: this.buildHeaders({ Accept: "application/json" }),
2787
+ signal: options?.signal,
2417
2788
  body: JSON.stringify(body)
2418
2789
  });
2419
- const data = await res.json();
2790
+ let data;
2791
+ try {
2792
+ data = await res.json();
2793
+ } catch (error) {
2794
+ if (!res.ok) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, `HTTP ${res.status}`, { method, status: res.status });
2795
+ throw error;
2796
+ }
2420
2797
  const sid = res.headers.get("Mcp-Session-Id");
2421
2798
  if (sid) this.sessionId = sid;
2422
- if (data.error) {
2799
+ if (data?.error) {
2423
2800
  throw new ACPError(data.error.code, data.error.message, data.error.data);
2424
2801
  }
2802
+ if (!res.ok) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, `HTTP ${res.status}`, { method, status: res.status });
2803
+ if (!data || !Object.prototype.hasOwnProperty.call(data, "result")) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, "Invalid JSON-RPC response", { method });
2425
2804
  return data.result;
2426
2805
  } catch (err) {
2806
+ throwIfAborted(options?.signal);
2427
2807
  if (err instanceof ACPError) throw err;
2428
2808
  throw new ACPError(
2429
2809
  ACPErrorCodes.TRANSPORT_ERROR,
2430
- `Transport error: ${err.message}`,
2431
- { method, originalError: err.message }
2810
+ `Transport error: ${errorMessage(err)}`,
2811
+ { method, originalError: errorMessage(err) }
2432
2812
  );
2433
2813
  }
2434
2814
  }
@@ -2448,7 +2828,7 @@ var HttpTransport = class {
2448
2828
  /**
2449
2829
  * Stream a chat request via SSE.
2450
2830
  */
2451
- async *streamChat(method, params) {
2831
+ async *streamChat(method, params, options) {
2452
2832
  const id = ++this.requestId;
2453
2833
  const body = {
2454
2834
  jsonrpc: "2.0",
@@ -2459,33 +2839,48 @@ var HttpTransport = class {
2459
2839
  const res = await fetch(`${this.baseUrl}/mcp`, {
2460
2840
  method: "POST",
2461
2841
  headers: this.buildHeaders({ Accept: "text/event-stream" }),
2842
+ signal: options?.signal,
2462
2843
  body: JSON.stringify(body)
2463
2844
  });
2845
+ if (!res.ok) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, `HTTP ${res.status}`, { method, status: res.status });
2464
2846
  if (!res.body) {
2465
2847
  throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, "No response body for stream");
2466
2848
  }
2467
2849
  const reader = res.body.getReader();
2468
2850
  const decoder = new TextDecoder();
2469
2851
  let buffer = "";
2470
- while (true) {
2471
- const { done, value } = await reader.read();
2472
- if (done) break;
2473
- buffer += decoder.decode(value, { stream: true });
2474
- const lines = buffer.split("\n");
2475
- buffer = lines.pop() || "";
2476
- for (const line of lines) {
2477
- if (line.startsWith("data: ")) {
2478
- try {
2479
- const data = JSON.parse(line.slice(6));
2480
- if (data.result !== void 0) {
2481
- yield { id: data.id, result: data.result };
2482
- } else if (data.method) {
2483
- yield { method: data.method, params: data.params };
2852
+ try {
2853
+ while (true) {
2854
+ throwIfAborted(options?.signal);
2855
+ const { done, value } = await reader.read();
2856
+ buffer += done ? decoder.decode() : decoder.decode(value, { stream: true });
2857
+ if (done) {
2858
+ if (!buffer.trim()) break;
2859
+ buffer += "\n";
2860
+ }
2861
+ const lines = buffer.split("\n");
2862
+ buffer = lines.pop() || "";
2863
+ for (const line of lines) {
2864
+ if (line.trim().startsWith("data:")) {
2865
+ const payload = line.trim().slice(5).trim();
2866
+ if (payload === "[DONE]") continue;
2867
+ let data;
2868
+ try {
2869
+ data = JSON.parse(payload);
2870
+ } catch {
2871
+ throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, "Invalid JSON in transport stream");
2484
2872
  }
2485
- } catch {
2873
+ if (data.error) throw new ACPError(data.error.code, data.error.message, data.error.data);
2874
+ if (data.result !== void 0) yield { id: data.id, result: data.result };
2875
+ else if (data.method) yield { method: data.method, params: data.params };
2486
2876
  }
2487
2877
  }
2878
+ if (done) break;
2488
2879
  }
2880
+ } finally {
2881
+ await reader.cancel().catch(() => {
2882
+ });
2883
+ reader.releaseLock();
2489
2884
  }
2490
2885
  }
2491
2886
  };
@@ -2532,28 +2927,35 @@ var Ldoing = class {
2532
2927
  constructor(config) {
2533
2928
  this.initialized = false;
2534
2929
  this.startTime = 0;
2930
+ this.mcpStatus = "unchecked";
2931
+ this.discoveredServerSkills = /* @__PURE__ */ new WeakSet();
2535
2932
  this.currentSessionId = null;
2536
2933
  this.abortControllers = /* @__PURE__ */ new Map();
2934
+ this.runContexts = /* @__PURE__ */ new WeakMap();
2537
2935
  // Session-level concurrency control (方案 B: abort 旧请求 + 等待退出)
2538
2936
  // runningSessions 跟踪正在执行 agent loop 的会话,donePromise 在 agent loop 结束时 resolve
2539
2937
  this.runningSessions = /* @__PURE__ */ new Map();
2540
2938
  // Approval pending promises
2541
2939
  this.approvalResolvers = /* @__PURE__ */ new Map();
2940
+ this.sessionStorageQueues = /* @__PURE__ */ new Map();
2941
+ this.managingSessions = /* @__PURE__ */ new Map();
2942
+ this.compressingSessions = /* @__PURE__ */ new Map();
2542
2943
  // Event listeners
2543
2944
  this.eventHandlers = /* @__PURE__ */ new Map();
2544
2945
  // Token estimation calibration
2545
2946
  this.tokenCalibrationRatio = 1;
2546
- this.config = config;
2947
+ const proxy = resolveProxyConfig(config);
2948
+ this.config = config.llmProxyMode === void 0 ? config : { ...config, llm: { ...config.llm } };
2547
2949
  this.agentId = `agent-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
2548
- this.llmAdapter = new OpenAIAdapter(config.llm);
2950
+ this.llmAdapter = new OpenAIAdapter(proxy.llm, { rejectRedirects: config.llmProxyMode !== void 0 });
2549
2951
  this.skillRegistry = new SkillRegistry();
2550
- this.skillExecutor = new SkillExecutor(this.skillRegistry);
2952
+ this.skillExecutor = new SkillExecutor(this.skillRegistry, void 0, { validateInputs: config.skills?.validateInputs });
2551
2953
  this.mcpRouter = new McpRouter();
2552
2954
  this.sessionManager = new SessionManager();
2553
2955
  this.contextManager = new ContextManager(config.context, config.llm.model);
2554
2956
  this.contextManager.setSummarizer(createLLMSummarizer(this.llmAdapter));
2555
- if (config.serverUrl) {
2556
- this.transport = new HttpTransport(config.serverUrl, config.authToken, config.headers);
2957
+ if (proxy.serverUrl) {
2958
+ this.transport = new HttpTransport(proxy.serverUrl, config.authToken, config.headers);
2557
2959
  } else {
2558
2960
  this.transport = new LocalTransport();
2559
2961
  }
@@ -2574,10 +2976,13 @@ var Ldoing = class {
2574
2976
  }
2575
2977
  }
2576
2978
  let mode = "local";
2577
- let connected = false;
2578
2979
  if (this.config.serverUrl) {
2980
+ const transport = this.transport;
2981
+ const proxyMode = this.config.llmProxyMode;
2982
+ const { adapter, model } = this.config.llm;
2983
+ this.mcpStatus = "unchecked";
2579
2984
  try {
2580
- const result2 = await this.transport.send("initialize", {
2985
+ const result2 = await transport.send("initialize", {
2581
2986
  protocolVersion: "2025-03-26",
2582
2987
  capabilities: {
2583
2988
  acp: {
@@ -2585,47 +2990,47 @@ var Ldoing = class {
2585
2990
  sessions: true,
2586
2991
  contextManagement: true,
2587
2992
  dynamicSkillRegistration: true,
2588
- llmConfig: this.config.llm
2993
+ llmConfig: proxyMode === void 0 ? this.config.llm : { adapter, model }
2589
2994
  }
2590
2995
  },
2591
2996
  clientInfo: { name: "listdoing-sdk", version: "0.1.0" }
2592
2997
  });
2593
- connected = true;
2594
- const serverSkills = result2?.capabilities?.acp?.serverSkills ?? [];
2595
- for (const name of serverSkills) {
2596
- if (!this.skillRegistry.has(name)) {
2597
- this.skillRegistry.register({
2598
- name,
2599
- description: `Server skill: ${name}`,
2600
- inputSchema: { type: "object", properties: {} },
2601
- location: "server"
2602
- });
2998
+ if (proxyMode === void 0 || this.transport === transport) {
2999
+ const serverSkills = result2?.capabilities?.acp?.serverSkills ?? [];
3000
+ for (const name of serverSkills) {
3001
+ if (!this.skillRegistry.has(name)) {
3002
+ const skill = {
3003
+ name,
3004
+ description: `Server skill: ${name}`,
3005
+ inputSchema: { type: "object", properties: {} },
3006
+ location: "server"
3007
+ };
3008
+ this.skillRegistry.register(skill);
3009
+ this.discoveredServerSkills.add(skill);
3010
+ }
2603
3011
  }
3012
+ if (this.transport === transport) this.mcpStatus = "connected";
3013
+ mode = this.skillRegistry.list().some((s) => s.location === "local") ? "hybrid" : "remote";
2604
3014
  }
2605
- mode = this.skillRegistry.list().some((s) => s.location === "local") ? "hybrid" : "remote";
2606
3015
  } catch {
2607
- mode = "local";
3016
+ if (this.transport === transport) this.mcpStatus = "failed";
2608
3017
  }
2609
3018
  }
2610
- let llmConnected = false;
2611
- try {
2612
- llmConnected = true;
2613
- } catch {
2614
- llmConnected = false;
2615
- }
2616
3019
  this.initialized = true;
2617
3020
  const result = {
2618
3021
  agentId: this.agentId,
2619
3022
  mode,
3023
+ ...this.getConnectionDetails(),
2620
3024
  llm: {
2621
- connected: llmConnected,
3025
+ connected: true,
3026
+ // Legacy status. Use llmRoute.verified for a successful request.
2622
3027
  provider: this.config.llm.adapter,
2623
3028
  model: this.config.llm.model
2624
3029
  },
2625
3030
  capabilities: {
2626
3031
  localSkills: this.skillRegistry.list().filter((s) => s.location !== "server").map((s) => s.name),
2627
3032
  serverSkills: this.skillRegistry.list().filter((s) => s.location === "server").map((s) => s.name),
2628
- llmProxy: mode !== "local"
3033
+ llmProxy: this.config.llmProxyMode !== void 0 || mode !== "local"
2629
3034
  }
2630
3035
  };
2631
3036
  this.emit("init", result);
@@ -2636,7 +3041,10 @@ var Ldoing = class {
2636
3041
  controller.abort();
2637
3042
  }
2638
3043
  this.abortControllers.clear();
3044
+ for (const pending of this.approvalResolvers.values()) pending.reject(abortError());
3045
+ this.approvalResolvers.clear();
2639
3046
  this.initialized = false;
3047
+ this.mcpStatus = "unchecked";
2640
3048
  this.emit("destroy", {});
2641
3049
  }
2642
3050
  getStatus() {
@@ -2646,6 +3054,7 @@ var Ldoing = class {
2646
3054
  uptime: Date.now() - this.startTime,
2647
3055
  activeSessions: this.sessionManager.list({ status: "active" }).length,
2648
3056
  loadedSkills: this.skillRegistry.list().length,
3057
+ ...this.getConnectionDetails(),
2649
3058
  llm: {
2650
3059
  adapter: this.config.llm.adapter,
2651
3060
  model: this.config.llm.model,
@@ -2653,15 +3062,26 @@ var Ldoing = class {
2653
3062
  }
2654
3063
  };
2655
3064
  }
3065
+ getConnectionDetails() {
3066
+ const mcpStatus = this.config.serverUrl ? this.mcpStatus : "not-configured";
3067
+ try {
3068
+ const status = this.llmAdapter.getConnectionStatus?.();
3069
+ if (status) return { mcpStatus, llmRoute: { mode: this.config.llmProxyMode ?? "legacy", ...status } };
3070
+ } catch {
3071
+ }
3072
+ return { mcpStatus };
3073
+ }
2656
3074
  // ---- Chat ----
2657
3075
  async chat(message, options) {
2658
3076
  this.ensureInitialized();
2659
3077
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
2660
- this.currentSessionId = sessionId;
3078
+ if (!options?._skipLock || !this.abortControllers.has(sessionId)) this.ensureSessionIsNotBeingManaged(sessionId);
3079
+ if (!options?._skipLock) this.currentSessionId = sessionId;
2661
3080
  const releaseLock = options?._skipLock ? null : await this.acquireSessionLock(sessionId);
2662
- const controller = new AbortController();
2663
- this.abortControllers.set(sessionId, controller);
3081
+ const execution = this.beginExecution(sessionId, options);
3082
+ const controller = execution.controller;
2664
3083
  try {
3084
+ const runContext = this.prepareRunContext(controller.signal, options?.runContext);
2665
3085
  if (options?.taskMode === "plan") {
2666
3086
  return await this.chatWithPlan(message, options);
2667
3087
  }
@@ -2678,12 +3098,14 @@ var Ldoing = class {
2678
3098
  this.sessionManager.addMessage(sessionId, { role: "user", content: userContent });
2679
3099
  let turn = 0;
2680
3100
  let continueLoop = true;
3101
+ let completedReply;
2681
3102
  let retried = false;
2682
3103
  while (continueLoop && turn < maxTurns) {
3104
+ throwIfAborted(controller.signal);
2683
3105
  turn++;
2684
3106
  const session2 = this.sessionManager.getOrThrow(sessionId);
2685
- const systemPrompt = this.resolveSystemPrompt(sessionId, session2.systemPrompt ?? this.config.systemPrompt ?? "");
2686
- const allTools = this.skillRegistry.listToolDefs();
3107
+ const systemPrompt = this.buildRunSystemPrompt(session2, runContext);
3108
+ const allTools = this.listRunTools(runContext, options?._skipLock);
2687
3109
  const tools = this.resolveTools(allTools, { allowedTools: options?.tools, toolsByPermission: options?.toolsByPermission });
2688
3110
  for (const name of this.computeAutoApproval(tools)) {
2689
3111
  requireApproval.add(name);
@@ -2704,24 +3126,27 @@ var Ldoing = class {
2704
3126
  afterTokens: ctxCheck.afterTokens
2705
3127
  });
2706
3128
  }
2707
- const contextMessages = this.contextManager.buildWindow(
3129
+ throwIfAborted(controller.signal);
3130
+ const window2 = this.contextManager.buildWindow(
2708
3131
  session2.messages,
2709
3132
  systemPrompt,
2710
3133
  tools
2711
- ).messages;
3134
+ );
3135
+ let contextMessages = window2.messages;
2712
3136
  let llmResponse;
2713
3137
  try {
2714
- llmResponse = await this.llmAdapter.chat(contextMessages, tools, {
2715
- signal: controller.signal
2716
- });
2717
- } catch (err) {
2718
- if (err.code === ACPErrorCodes.CONTEXT_OVERFLOW && !retried) {
2719
- await this.compressContext(sessionId, "llm_summary");
3138
+ try {
3139
+ llmResponse = await this.llmAdapter.chat(contextMessages, tools, { signal: controller.signal });
3140
+ } catch (err) {
3141
+ if (err?.code !== ACPErrorCodes.CONTEXT_OVERFLOW || retried) throw err;
3142
+ const smaller = await this.prepareOverflowRetry(session2, window2, systemPrompt, tools, controller.signal);
3143
+ if (!smaller) throw err;
2720
3144
  retried = true;
2721
- turn--;
2722
- continue;
3145
+ contextMessages = smaller;
3146
+ llmResponse = await this.llmAdapter.chat(contextMessages, tools, { signal: controller.signal });
2723
3147
  }
2724
- if (err.code === ACPErrorCodes.LLM_EMPTY_RESPONSE) {
3148
+ } catch (err) {
3149
+ if (err?.code === ACPErrorCodes.LLM_EMPTY_RESPONSE) {
2725
3150
  if (!retried) {
2726
3151
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
2727
3152
  this.sessionManager.addMessage(sessionId, { role: "user", content: "System: \u521A\u521A\u8FD4\u56DE\u4E86\u7A7A\u5185\u5BB9\uFF0C\u8BF7\u751F\u6210\u6709\u6548\u56DE\u590D\u3002" });
@@ -2730,10 +3155,12 @@ var Ldoing = class {
2730
3155
  continue;
2731
3156
  }
2732
3157
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
3158
+ completedReply = ".";
2733
3159
  continueLoop = false;
2734
3160
  break;
2735
3161
  }
2736
- throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, `LLM call failed: ${err.message}`, { turn });
3162
+ if (controller.signal.aborted || err?.name === "AbortError") throw abortError();
3163
+ throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, `LLM call failed: ${errorMessage(err)}`, { turn });
2737
3164
  }
2738
3165
  if (!llmResponse.content && !llmResponse.toolCalls?.length) {
2739
3166
  if (!retried) {
@@ -2744,9 +3171,11 @@ var Ldoing = class {
2744
3171
  continue;
2745
3172
  }
2746
3173
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
3174
+ completedReply = ".";
2747
3175
  continueLoop = false;
2748
3176
  break;
2749
3177
  }
3178
+ this.reportPromptCacheUsage(sessionId, llmResponse.promptCacheUsage);
2750
3179
  if (llmResponse.usage) {
2751
3180
  totalInputTokens += llmResponse.usage.input;
2752
3181
  totalOutputTokens += llmResponse.usage.output;
@@ -2757,6 +3186,7 @@ var Ldoing = class {
2757
3186
  }
2758
3187
  }
2759
3188
  if (!llmResponse.toolCalls?.length) {
3189
+ completedReply = llmResponse.content;
2760
3190
  this.sessionManager.addMessage(sessionId, {
2761
3191
  role: "assistant",
2762
3192
  content: llmResponse.content
@@ -2770,8 +3200,9 @@ var Ldoing = class {
2770
3200
  toolCalls: llmResponse.toolCalls
2771
3201
  });
2772
3202
  const turnTrace = { turn, toolCalls: [] };
2773
- const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval);
2774
- await this.processApprovalQueue(plan.items, sessionId, (event, data) => this.emit(event, data));
3203
+ const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval, runContext, tools);
3204
+ for await (const _event of this.processApprovalQueue(plan.items, sessionId, controller.signal)) {
3205
+ }
2775
3206
  const batchTraces = await this.executeToolBatch(plan.items, sessionId, controller, plan.canParallelize, (event, data) => this.emit(event, data));
2776
3207
  turnTrace.toolCalls = batchTraces;
2777
3208
  const executableCount = plan.items.filter((i) => !i.resolved).length;
@@ -2779,26 +3210,27 @@ var Ldoing = class {
2779
3210
  executionTrace.push(turnTrace);
2780
3211
  }
2781
3212
  const session = this.sessionManager.getOrThrow(sessionId);
2782
- const lastAssistant = [...session.messages].reverse().find((m) => m.role === "assistant" && !m.toolCalls?.length);
2783
- const reply = typeof lastAssistant?.content === "string" ? lastAssistant.content : lastAssistant ? contentToString(lastAssistant.content) : "";
2784
- const chatResult = { reply, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3213
+ const reply = completedReply ?? "";
3214
+ const terminationReason = completedReply !== void 0 ? "stop" : "max_turns";
3215
+ const chatResult = { reply, terminationReason, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
2785
3216
  this.emit("chat:complete", { sessionId, reply, tokenUsage: chatResult.tokenUsage });
2786
- await this.persistence.saveSession(session).catch(() => {
2787
- });
3217
+ await this.saveSessionSafely(session);
2788
3218
  return chatResult;
2789
3219
  } finally {
2790
- this.abortControllers.delete(sessionId);
3220
+ execution.cleanup();
2791
3221
  if (releaseLock) releaseLock();
2792
3222
  }
2793
3223
  }
2794
3224
  async *chatStream(message, options) {
2795
3225
  this.ensureInitialized();
2796
3226
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
2797
- this.currentSessionId = sessionId;
3227
+ if (!options?._skipLock || !this.abortControllers.has(sessionId)) this.ensureSessionIsNotBeingManaged(sessionId);
3228
+ if (!options?._skipLock) this.currentSessionId = sessionId;
2798
3229
  const releaseLock = options?._skipLock ? null : await this.acquireSessionLock(sessionId);
2799
- const controller = new AbortController();
2800
- this.abortControllers.set(sessionId, controller);
3230
+ const execution = this.beginExecution(sessionId, options);
3231
+ const controller = execution.controller;
2801
3232
  try {
3233
+ const runContext = this.prepareRunContext(controller.signal, options?.runContext);
2802
3234
  if (options?.taskMode === "plan") {
2803
3235
  yield* this.chatStreamWithPlan(message, options);
2804
3236
  return;
@@ -2817,11 +3249,13 @@ var Ldoing = class {
2817
3249
  this.sessionManager.addMessage(sessionId, { role: "user", content: userContent });
2818
3250
  let turn = 0;
2819
3251
  let continueLoop = true;
3252
+ let completedReply;
2820
3253
  while (continueLoop && turn < maxTurns) {
3254
+ throwIfAborted(controller.signal);
2821
3255
  turn++;
2822
3256
  const session2 = this.sessionManager.getOrThrow(sessionId);
2823
- const systemPrompt = this.resolveSystemPrompt(sessionId, session2.systemPrompt ?? this.config.systemPrompt ?? "");
2824
- const allTools = this.skillRegistry.listToolDefs();
3257
+ const systemPrompt = this.buildRunSystemPrompt(session2, runContext);
3258
+ const allTools = this.listRunTools(runContext, options?._skipLock);
2825
3259
  const tools = this.resolveTools(allTools, { allowedTools: options?.tools, toolsByPermission: options?.toolsByPermission });
2826
3260
  for (const name of this.computeAutoApproval(tools)) {
2827
3261
  requireApproval.add(name);
@@ -2849,36 +3283,33 @@ var Ldoing = class {
2849
3283
  reason: "auto"
2850
3284
  };
2851
3285
  }
2852
- const contextMessages = this.contextManager.buildWindow(session2.messages, systemPrompt, tools).messages;
3286
+ throwIfAborted(controller.signal);
3287
+ const window2 = this.contextManager.buildWindow(session2.messages, systemPrompt, tools);
3288
+ let contextMessages = window2.messages;
2853
3289
  let llmResponse;
2854
3290
  try {
2855
- const useStreaming = typeof this.llmAdapter.chatStream === "function";
2856
- if (useStreaming) {
2857
- llmResponse = yield* this.streamLlmResponse(contextMessages, tools, controller.signal);
2858
- } else {
2859
- llmResponse = await this.llmAdapter.chat(contextMessages, tools, { signal: controller.signal });
2860
- if (llmResponse.reasoning) {
2861
- yield { type: "llm_thinking", content: llmResponse.reasoning };
2862
- }
2863
- if (llmResponse.content) {
2864
- yield { type: "llm_text", content: llmResponse.content };
3291
+ for (; ; ) {
3292
+ try {
3293
+ llmResponse = yield* this.streamLlmResponse(contextMessages, tools, controller.signal);
3294
+ break;
3295
+ } catch (err) {
3296
+ if (err?.code !== ACPErrorCodes.CONTEXT_OVERFLOW || retried) throw err;
3297
+ const smaller = await this.prepareOverflowRetry(session2, window2, systemPrompt, tools, controller.signal);
3298
+ if (!smaller) throw err;
3299
+ retried = true;
3300
+ contextMessages = smaller;
2865
3301
  }
2866
3302
  }
2867
3303
  } catch (err) {
2868
- if (err.code === ACPErrorCodes.CONTEXT_OVERFLOW && !retried) {
2869
- await this.compressContext(sessionId, "llm_summary");
2870
- retried = true;
2871
- turn--;
2872
- continue;
2873
- }
2874
- if (err.code === ACPErrorCodes.LLM_EMPTY_RESPONSE && !retried) {
3304
+ if (err?.code === ACPErrorCodes.LLM_EMPTY_RESPONSE && !retried) {
2875
3305
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
2876
3306
  this.sessionManager.addMessage(sessionId, { role: "user", content: "System: \u521A\u521A\u8FD4\u56DE\u4E86\u7A7A\u5185\u5BB9\uFF0C\u8BF7\u751F\u6210\u6709\u6548\u56DE\u590D\u3002" });
2877
3307
  retried = true;
2878
3308
  turn--;
2879
3309
  continue;
2880
3310
  }
2881
- yield { type: "error", code: ACPErrorCodes.LLM_CALL_FAILED, message: err.message };
3311
+ if (controller.signal.aborted || err?.name === "AbortError") throw abortError();
3312
+ yield { type: "error", code: ACPErrorCodes.LLM_CALL_FAILED, message: errorMessage(err) };
2882
3313
  return;
2883
3314
  }
2884
3315
  if (!llmResponse.content && !llmResponse.toolCalls?.length) {
@@ -2890,8 +3321,10 @@ var Ldoing = class {
2890
3321
  continue;
2891
3322
  }
2892
3323
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
3324
+ completedReply = ".";
2893
3325
  break;
2894
3326
  }
3327
+ this.reportPromptCacheUsage(sessionId, llmResponse.promptCacheUsage);
2895
3328
  if (llmResponse.usage) {
2896
3329
  totalInputTokens += llmResponse.usage.input;
2897
3330
  totalOutputTokens += llmResponse.usage.output;
@@ -2902,27 +3335,21 @@ var Ldoing = class {
2902
3335
  }
2903
3336
  }
2904
3337
  if (!llmResponse.toolCalls?.length) {
3338
+ completedReply = llmResponse.content;
2905
3339
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: llmResponse.content });
2906
3340
  break;
2907
3341
  }
2908
3342
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: llmResponse.content, toolCalls: llmResponse.toolCalls });
2909
- const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval);
2910
- const approvalEvents = [];
2911
- const approvalYield = (evt) => approvalEvents.push(evt);
2912
- await this.processApprovalQueue(plan.items, sessionId, (event, data) => this.emit(event, data), approvalYield);
2913
- for (const evt of approvalEvents) yield evt;
2914
- const batchEvents = [];
2915
- const batchYield = (evt) => batchEvents.push(evt);
2916
- await this.executeToolBatchStream(plan.items, sessionId, controller, plan.canParallelize, batchYield);
2917
- for (const evt of batchEvents) yield evt;
3343
+ const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval, runContext, tools);
3344
+ yield* this.processApprovalQueue(plan.items, sessionId, controller.signal);
3345
+ yield* this.streamToolBatch(plan.items, sessionId, controller, plan.canParallelize);
2918
3346
  }
2919
3347
  const session = this.sessionManager.getOrThrow(sessionId);
2920
- const lastAssistant = [...session.messages].reverse().find((m) => m.role === "assistant" && !m.toolCalls?.length);
2921
- yield { type: "done", reply: typeof lastAssistant?.content === "string" ? lastAssistant.content : lastAssistant ? contentToString(lastAssistant.content) : "", tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
2922
- await this.persistence.saveSession(session).catch(() => {
2923
- });
3348
+ const saving = this.saveSessionSafely(session);
3349
+ yield { type: "done", terminationReason: completedReply !== void 0 ? "stop" : "max_turns", reply: completedReply ?? "", tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3350
+ await saving;
2924
3351
  } finally {
2925
- this.abortControllers.delete(sessionId);
3352
+ execution.cleanup();
2926
3353
  if (releaseLock) releaseLock();
2927
3354
  }
2928
3355
  }
@@ -2932,7 +3359,6 @@ var Ldoing = class {
2932
3359
  const controller = this.abortControllers.get(sid);
2933
3360
  if (controller) {
2934
3361
  controller.abort();
2935
- this.abortControllers.delete(sid);
2936
3362
  }
2937
3363
  }
2938
3364
  }
@@ -2940,7 +3366,7 @@ var Ldoing = class {
2940
3366
  /**
2941
3367
  * 在启动新的 agent loop 前获取会话锁。
2942
3368
  * 根据并发策略处理正在运行的旧请求:
2943
- * - 'abort' (默认): 中止旧请求,等待其真正退出,清理半截消息,然后返回
3369
+ * - 'abort' (默认): 中止旧请求,等待其真正退出,发送前修复未配对消息,然后返回
2944
3370
  * - 'reject': 若已有运行请求,抛错
2945
3371
  * - 'queue': 预留,当前回退到 'abort'
2946
3372
  *
@@ -2948,48 +3374,27 @@ var Ldoing = class {
2948
3374
  */
2949
3375
  async acquireSessionLock(sessionId) {
2950
3376
  const policy = this.config.concurrentPolicy ?? "abort";
2951
- const running = this.runningSessions.get(sessionId);
2952
- if (running) {
2953
- if (policy === "reject") {
2954
- throw new ACPError(
2955
- ACPErrorCodes.SESSION_BUSY,
2956
- `Session ${sessionId} is already running a chat`
2957
- );
2958
- }
3377
+ let running = this.runningSessions.get(sessionId);
3378
+ while (running) {
3379
+ this.ensureSessionIsNotBeingManaged(sessionId);
3380
+ if (policy === "reject") throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${sessionId} is already running a chat`);
2959
3381
  await this.abort(sessionId);
2960
3382
  await running.donePromise;
2961
- this.cleanupInterruptedMessages(sessionId);
3383
+ running = this.runningSessions.get(sessionId);
2962
3384
  }
3385
+ this.ensureInitialized();
3386
+ this.ensureSessionIsNotBeingManaged(sessionId);
2963
3387
  let resolveDone;
2964
3388
  const donePromise = new Promise((resolve) => {
2965
3389
  resolveDone = resolve;
2966
3390
  });
2967
- this.runningSessions.set(sessionId, { donePromise, resolveDone });
3391
+ const owner = { donePromise, resolveDone };
3392
+ this.runningSessions.set(sessionId, owner);
2968
3393
  return () => {
2969
- this.runningSessions.delete(sessionId);
3394
+ if (this.runningSessions.get(sessionId) === owner) this.runningSessions.delete(sessionId);
2970
3395
  resolveDone();
2971
3396
  };
2972
3397
  }
2973
- /**
2974
- * 清理被 abort 打断的半截消息:
2975
- * 如果 session.messages 最后一条是 assistant 且带 tool_calls,
2976
- * 但后面没有对应的 tool 消息,则删除这条 assistant。
2977
- * 避免下次 LLM 调用因 tool_calls 未配对而报错。
2978
- */
2979
- cleanupInterruptedMessages(sessionId) {
2980
- try {
2981
- const session = this.sessionManager.get(sessionId);
2982
- if (!session?.messages?.length) return;
2983
- const msgs = session.messages;
2984
- const last = msgs[msgs.length - 1];
2985
- if (last.role === "assistant" && last.toolCalls?.length) {
2986
- msgs.pop();
2987
- console.log(`[concurrency] cleaned up interrupted assistant(tool_calls) from session ${sessionId}`);
2988
- }
2989
- } catch (err) {
2990
- console.warn("[concurrency] failed to cleanup interrupted messages:", err);
2991
- }
2992
- }
2993
3398
  // ---- Tool Approval ----
2994
3399
  approveTool(callId, approved, _reason) {
2995
3400
  const resolver = this.approvalResolvers.get(callId);
@@ -2998,15 +3403,28 @@ var Ldoing = class {
2998
3403
  this.approvalResolvers.delete(callId);
2999
3404
  }
3000
3405
  }
3001
- waitForApproval(callId) {
3406
+ waitForApproval(callId, signal) {
3407
+ throwIfAborted(signal);
3002
3408
  return new Promise((resolve, reject) => {
3003
- this.approvalResolvers.set(callId, { resolve, reject });
3004
- setTimeout(() => {
3005
- if (this.approvalResolvers.has(callId)) {
3006
- this.approvalResolvers.delete(callId);
3007
- resolve(false);
3409
+ const cleanup = () => {
3410
+ clearTimeout(timer);
3411
+ signal.removeEventListener("abort", abort);
3412
+ if (this.approvalResolvers.get(callId) === pending) this.approvalResolvers.delete(callId);
3413
+ };
3414
+ const pending = {
3415
+ resolve: (approved) => {
3416
+ cleanup();
3417
+ resolve(approved);
3418
+ },
3419
+ reject: (error) => {
3420
+ cleanup();
3421
+ reject(error);
3008
3422
  }
3009
- }, 6e4);
3423
+ };
3424
+ const abort = () => pending.reject(abortError());
3425
+ const timer = setTimeout(() => pending.resolve(false), 6e4);
3426
+ this.approvalResolvers.set(callId, pending);
3427
+ signal.addEventListener("abort", abort, { once: true });
3010
3428
  });
3011
3429
  }
3012
3430
  // ---- Skill Management ----
@@ -3072,31 +3490,31 @@ ${JSON.stringify(params, null, 2)}`;
3072
3490
  } else {
3073
3491
  prompt = loop.instruction;
3074
3492
  }
3075
- const result = await ctx.agent.chatInner(prompt, ctx.sessionId, { maxTurns: loop.maxTurns ?? 10, tools: loop.tools });
3493
+ const result = await ctx.agent.chatInner(prompt, ctx.sessionId, { maxTurns: loop.maxTurns ?? 10, tools: loop.tools, signal: ctx.signal });
3076
3494
  return { content: [{ type: "text", text: result.reply }] };
3077
3495
  };
3078
3496
  }
3079
3497
  /** Internal chat for agentLoop/workflow/sub-agent execution. Condenses inner messages after completion. */
3080
3498
  async chatInner(prompt, sessionId, options) {
3081
3499
  const session = this.sessionManager.getOrThrow(sessionId);
3082
- const messageCountBefore = session.messages.length;
3083
- const result = await this.chat(prompt, { ...options, _skipLock: true });
3084
- const sessionAfter = this.sessionManager.getOrThrow(sessionId);
3085
- if (sessionAfter.messages.length > messageCountBefore + 2) {
3086
- const innerMessages = sessionAfter.messages.splice(messageCountBefore);
3087
- const lastAssistant = [...innerMessages].reverse().find((m) => m.role === "assistant" && !m.toolCalls?.length);
3088
- if (lastAssistant) {
3089
- sessionAfter.messages.push({ role: "assistant", content: `[Agent loop completed]: ${contentToPreview(lastAssistant.content, 200)}` });
3090
- }
3500
+ const prefix = session.messages.slice();
3501
+ const isToolHandler = prefix[prefix.length - 1]?.role === "assistant" && !!prefix[prefix.length - 1]?.toolCalls?.length;
3502
+ try {
3503
+ const result = await this.chat(prompt, { ...options, sessionId, _skipLock: true });
3504
+ session.messages = prefix;
3505
+ if (!isToolHandler) this.sessionManager.addMessage(sessionId, { role: "assistant", content: `[Agent loop completed]: ${result.reply.slice(0, 200)}` });
3506
+ return result;
3507
+ } finally {
3508
+ if (isToolHandler) session.messages = prefix;
3509
+ session.messageCount = session.messages.length;
3091
3510
  }
3092
- return result;
3093
3511
  }
3094
3512
  // ---- Task Plan Execution ----
3095
3513
  async chatWithPlan(message, options) {
3096
3514
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
3097
3515
  this.currentSessionId = sessionId;
3098
- const controller = new AbortController();
3099
- this.abortControllers.set(sessionId, controller);
3516
+ const execution = this.beginExecution(sessionId, options);
3517
+ const controller = execution.controller;
3100
3518
  const startTime = Date.now();
3101
3519
  let totalInputTokens = 0;
3102
3520
  let totalOutputTokens = 0;
@@ -3110,8 +3528,9 @@ ${JSON.stringify(params, null, 2)}`;
3110
3528
  let plan;
3111
3529
  try {
3112
3530
  const planner = new TaskPlanner(this.llmAdapter);
3113
- plan = await planner.plan(messageText, filteredTools);
3531
+ plan = await planner.plan(messageText, filteredTools, controller.signal);
3114
3532
  } catch (err) {
3533
+ throwIfAborted(controller.signal);
3115
3534
  throw new ACPError(ACPErrorCodes.TASK_PLAN_FAILED, `Task planning failed: ${err.message}`);
3116
3535
  }
3117
3536
  const tracker = new PlanTracker(plan, sessionId, (event, data) => this.emit(event, data));
@@ -3122,13 +3541,14 @@ ${JSON.stringify(params, null, 2)}`;
3122
3541
  const maxRetry = options?.stepRetryCount ?? 1;
3123
3542
  const continueOnFailure = options?.continueOnStepFailure ?? true;
3124
3543
  for (let i = 0; i < plan.steps.length; i++) {
3125
- if (controller.signal.aborted) break;
3544
+ throwIfAborted(controller.signal);
3126
3545
  const step = plan.steps[i];
3127
3546
  if (step.status === "done" || step.status === "skipped") continue;
3128
3547
  tracker.startStep(i);
3129
3548
  let stepResult;
3130
3549
  let succeeded = false;
3131
3550
  for (let attempt = 0; attempt <= maxRetry; attempt++) {
3551
+ throwIfAborted(controller.signal);
3132
3552
  try {
3133
3553
  const result = await this.chatInner(step.goal, sessionId, { maxTurns: options?.maxTurns ?? 10, tools: step.tools });
3134
3554
  stepResult = { output: result.reply, toolCalls: result.executionTrace.reduce((sum, t) => sum + t.toolCalls.length, 0), tokenUsage: result.tokenUsage, duration: result.totalDuration };
@@ -3138,6 +3558,7 @@ ${JSON.stringify(params, null, 2)}`;
3138
3558
  succeeded = true;
3139
3559
  break;
3140
3560
  } catch (err) {
3561
+ throwIfAborted(controller.signal);
3141
3562
  stepResult = { output: "", toolCalls: 0, tokenUsage: { input: 0, output: 0 }, duration: 0, error: err.message };
3142
3563
  plan.steps[i].retryCount = attempt + 1;
3143
3564
  }
@@ -3165,18 +3586,17 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3165
3586
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: finalReply });
3166
3587
  const chatResult = { reply: finalReply, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3167
3588
  this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
3168
- await this.persistence.saveSession(session).catch(() => {
3169
- });
3589
+ await this.saveSessionSafely(session);
3170
3590
  return chatResult;
3171
3591
  } finally {
3172
- this.abortControllers.delete(sessionId);
3592
+ execution.cleanup();
3173
3593
  }
3174
3594
  }
3175
3595
  async *chatStreamWithPlan(message, options) {
3176
3596
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
3177
3597
  this.currentSessionId = sessionId;
3178
- const controller = new AbortController();
3179
- this.abortControllers.set(sessionId, controller);
3598
+ const execution = this.beginExecution(sessionId, options);
3599
+ const controller = execution.controller;
3180
3600
  const startTime = Date.now();
3181
3601
  let totalInputTokens = 0;
3182
3602
  let totalOutputTokens = 0;
@@ -3189,7 +3609,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3189
3609
  let plan;
3190
3610
  try {
3191
3611
  const planner = new TaskPlanner(this.llmAdapter);
3192
- plan = await planner.plan(messageText, filteredTools);
3612
+ plan = await planner.plan(messageText, filteredTools, controller.signal);
3193
3613
  } catch (err) {
3194
3614
  yield { type: "error", code: ACPErrorCodes.TASK_PLAN_FAILED, message: `Task planning failed: ${err.message}` };
3195
3615
  return;
@@ -3203,7 +3623,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3203
3623
  const maxRetry = options?.stepRetryCount ?? 1;
3204
3624
  const continueOnFailure = options?.continueOnStepFailure ?? true;
3205
3625
  for (let i = 0; i < plan.steps.length; i++) {
3206
- if (controller.signal.aborted) break;
3626
+ throwIfAborted(controller.signal);
3207
3627
  const step = plan.steps[i];
3208
3628
  if (step.status === "done" || step.status === "skipped") continue;
3209
3629
  tracker.startStep(i);
@@ -3211,6 +3631,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3211
3631
  let stepResult;
3212
3632
  let succeeded = false;
3213
3633
  for (let attempt = 0; attempt <= maxRetry; attempt++) {
3634
+ throwIfAborted(controller.signal);
3214
3635
  try {
3215
3636
  const result = await this.chatInner(step.goal, sessionId, { maxTurns: options?.maxTurns ?? 10, tools: step.tools });
3216
3637
  stepResult = { output: result.reply, toolCalls: result.executionTrace.reduce((sum, t) => sum + t.toolCalls.length, 0), tokenUsage: result.tokenUsage, duration: result.totalDuration };
@@ -3219,6 +3640,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3219
3640
  succeeded = true;
3220
3641
  break;
3221
3642
  } catch (err) {
3643
+ throwIfAborted(controller.signal);
3222
3644
  stepResult = { output: "", toolCalls: 0, tokenUsage: { input: 0, output: 0 }, duration: 0, error: err.message };
3223
3645
  plan.steps[i].retryCount = attempt + 1;
3224
3646
  }
@@ -3246,11 +3668,11 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3246
3668
 
3247
3669
  ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3248
3670
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: finalReply });
3671
+ const saving = this.saveSessionSafely(session);
3249
3672
  yield { type: "done", reply: finalReply, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3250
- await this.persistence.saveSession(session).catch(() => {
3251
- });
3673
+ await saving;
3252
3674
  } finally {
3253
- this.abortControllers.delete(sessionId);
3675
+ execution.cleanup();
3254
3676
  }
3255
3677
  }
3256
3678
  async resumeTask(sessionId, options) {
@@ -3260,6 +3682,8 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3260
3682
  if (!savedPlan) {
3261
3683
  throw new ACPError(ACPErrorCodes.TASK_PLAN_FAILED, "No task plan found in session");
3262
3684
  }
3685
+ const releaseLock = await this.acquireSessionLock(sessionId);
3686
+ this.currentSessionId = sessionId;
3263
3687
  for (const step of savedPlan.steps) {
3264
3688
  if (step.status === "failed") {
3265
3689
  step.status = "pending";
@@ -3267,8 +3691,8 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3267
3691
  step.result = void 0;
3268
3692
  }
3269
3693
  }
3270
- const controller = new AbortController();
3271
- this.abortControllers.set(sessionId, controller);
3694
+ const execution = this.beginExecution(sessionId, options);
3695
+ const controller = execution.controller;
3272
3696
  const startTime = Date.now();
3273
3697
  let totalInputTokens = 0;
3274
3698
  let totalOutputTokens = 0;
@@ -3279,13 +3703,14 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3279
3703
  const maxRetry = options?.stepRetryCount ?? 1;
3280
3704
  const continueOnFailure = options?.continueOnStepFailure ?? true;
3281
3705
  for (let i = 0; i < savedPlan.steps.length; i++) {
3282
- if (controller.signal.aborted) break;
3706
+ throwIfAborted(controller.signal);
3283
3707
  const step = savedPlan.steps[i];
3284
3708
  if (step.status === "done" || step.status === "skipped") continue;
3285
3709
  tracker.startStep(i);
3286
3710
  let stepResult;
3287
3711
  let succeeded = false;
3288
3712
  for (let attempt = 0; attempt <= maxRetry; attempt++) {
3713
+ throwIfAborted(controller.signal);
3289
3714
  try {
3290
3715
  const result = await this.chatInner(step.goal, sessionId, { maxTurns: options?.maxTurns ?? 10, tools: step.tools });
3291
3716
  stepResult = { output: result.reply, toolCalls: result.executionTrace.reduce((sum, t) => sum + t.toolCalls.length, 0), tokenUsage: result.tokenUsage, duration: result.totalDuration };
@@ -3295,6 +3720,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3295
3720
  succeeded = true;
3296
3721
  break;
3297
3722
  } catch (err) {
3723
+ throwIfAborted(controller.signal);
3298
3724
  stepResult = { output: "", toolCalls: 0, tokenUsage: { input: 0, output: 0 }, duration: 0, error: err.message };
3299
3725
  savedPlan.steps[i].retryCount = attempt + 1;
3300
3726
  }
@@ -3322,11 +3748,11 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
3322
3748
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: finalReply });
3323
3749
  const chatResult = { reply: finalReply, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3324
3750
  this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
3325
- await this.persistence.saveSession(session).catch(() => {
3326
- });
3751
+ await this.saveSessionSafely(session);
3327
3752
  return chatResult;
3328
3753
  } finally {
3329
- this.abortControllers.delete(sessionId);
3754
+ execution.cleanup();
3755
+ releaseLock();
3330
3756
  }
3331
3757
  }
3332
3758
  getTaskPlan(sessionId) {
@@ -3342,8 +3768,8 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
3342
3768
  async chatWithGraph(message, options) {
3343
3769
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
3344
3770
  this.currentSessionId = sessionId;
3345
- const controller = new AbortController();
3346
- this.abortControllers.set(sessionId, controller);
3771
+ const execution = this.beginExecution(sessionId, options);
3772
+ const controller = execution.controller;
3347
3773
  const startTime = Date.now();
3348
3774
  try {
3349
3775
  const userContent = this.buildUserContent(message, options?.attachments);
@@ -3354,8 +3780,9 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
3354
3780
  let plan;
3355
3781
  try {
3356
3782
  const planner = new GraphPlanner(this.llmAdapter);
3357
- plan = await planner.planGraph(messageText, filteredTools);
3783
+ plan = await planner.planGraph(messageText, filteredTools, controller.signal);
3358
3784
  } catch (err) {
3785
+ throwIfAborted(controller.signal);
3359
3786
  throw new ACPError(ACPErrorCodes.GRAPH_PLAN_FAILED, `Graph planning failed: ${err.message}`);
3360
3787
  }
3361
3788
  const graph = new TaskGraph(plan.nodes);
@@ -3369,6 +3796,7 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
3369
3796
  const executor = new GraphExecutor({
3370
3797
  graph,
3371
3798
  parentAgent: this,
3799
+ parentSignal: controller.signal,
3372
3800
  plan,
3373
3801
  maxConcurrency: options?.graphConfig?.maxConcurrency ?? 3,
3374
3802
  maxDepth: options?.graphConfig?.maxDepth ?? 2,
@@ -3376,6 +3804,7 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
3376
3804
  emit: (event, data) => this.emit(event, data)
3377
3805
  });
3378
3806
  const finalPlan = await executor.execute();
3807
+ throwIfAborted(controller.signal);
3379
3808
  const resultLines = Object.entries(finalPlan.results).filter(([, r]) => r.status === "done" && r.output).map(([nodeId, r]) => `[${nodeId}] ${r.output.slice(0, 200)}`);
3380
3809
  const progress = executor.getProgress();
3381
3810
  const finalReply = resultLines.length > 0 ? `Graph task completed (${progress.done}/${progress.total} nodes done, ${progress.failed} failed, ${progress.bypassed} bypassed).
@@ -3394,18 +3823,17 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3394
3823
  totalDuration: Date.now() - startTime
3395
3824
  };
3396
3825
  this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
3397
- await this.persistence.saveSession(session).catch(() => {
3398
- });
3826
+ await this.saveSessionSafely(session);
3399
3827
  return chatResult;
3400
3828
  } finally {
3401
- this.abortControllers.delete(sessionId);
3829
+ execution.cleanup();
3402
3830
  }
3403
3831
  }
3404
3832
  async *chatStreamWithGraph(message, options) {
3405
3833
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
3406
3834
  this.currentSessionId = sessionId;
3407
- const controller = new AbortController();
3408
- this.abortControllers.set(sessionId, controller);
3835
+ const execution = this.beginExecution(sessionId, options);
3836
+ const controller = execution.controller;
3409
3837
  const startTime = Date.now();
3410
3838
  let totalInputTokens = 0;
3411
3839
  let totalOutputTokens = 0;
@@ -3418,7 +3846,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3418
3846
  let plan;
3419
3847
  try {
3420
3848
  const planner = new GraphPlanner(this.llmAdapter);
3421
- plan = await planner.planGraph(messageText, filteredTools);
3849
+ plan = await planner.planGraph(messageText, filteredTools, controller.signal);
3422
3850
  } catch (err) {
3423
3851
  yield { type: "error", code: ACPErrorCodes.GRAPH_PLAN_FAILED, message: `Graph planning failed: ${err.message}` };
3424
3852
  return;
@@ -3435,6 +3863,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3435
3863
  const executor = new GraphExecutor({
3436
3864
  graph,
3437
3865
  parentAgent: this,
3866
+ parentSignal: controller.signal,
3438
3867
  plan,
3439
3868
  maxConcurrency: options?.graphConfig?.maxConcurrency ?? 3,
3440
3869
  maxDepth: options?.graphConfig?.maxDepth ?? 2,
@@ -3446,6 +3875,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3446
3875
  }
3447
3876
  });
3448
3877
  const finalPlan = await executor.execute();
3878
+ throwIfAborted(controller.signal);
3449
3879
  const progress = executor.getProgress();
3450
3880
  const resultLines = Object.entries(finalPlan.results).filter(([, r]) => r.status === "done" && r.output).map(([nodeId, r]) => `[${nodeId}] ${r.output.slice(0, 200)}`);
3451
3881
  const finalReply = resultLines.length > 0 ? `Graph task completed (${progress.done}/${progress.total} nodes done, ${progress.failed} failed, ${progress.bypassed} bypassed).
@@ -3457,11 +3887,11 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3457
3887
  const session = this.sessionManager.getOrThrow(sessionId);
3458
3888
  session.meta = session.meta ?? {};
3459
3889
  session.meta.graphPlan = finalPlan;
3890
+ const saving = this.saveSessionSafely(session);
3460
3891
  yield { type: "done", reply: finalReply, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3461
- await this.persistence.saveSession(session).catch(() => {
3462
- });
3892
+ await saving;
3463
3893
  } finally {
3464
- this.abortControllers.delete(sessionId);
3894
+ execution.cleanup();
3465
3895
  }
3466
3896
  }
3467
3897
  /** Resume a graph execution from checkpoint */
@@ -3472,6 +3902,8 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3472
3902
  if (!savedPlan) {
3473
3903
  throw new ACPError(ACPErrorCodes.GRAPH_PLAN_FAILED, "No graph plan found in session");
3474
3904
  }
3905
+ const releaseLock = await this.acquireSessionLock(sessionId);
3906
+ this.currentSessionId = sessionId;
3475
3907
  for (const [nodeId, result] of Object.entries(savedPlan.results)) {
3476
3908
  if (result.status === "failed" || result.status === "bypassed") {
3477
3909
  savedPlan.results[nodeId] = {
@@ -3484,14 +3916,15 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3484
3916
  }
3485
3917
  }
3486
3918
  savedPlan.status = "executing";
3487
- const controller = new AbortController();
3488
- this.abortControllers.set(sessionId, controller);
3919
+ const execution = this.beginExecution(sessionId, options);
3920
+ const controller = execution.controller;
3489
3921
  const startTime = Date.now();
3490
3922
  try {
3491
3923
  const graph = new TaskGraph(savedPlan.nodes);
3492
3924
  const executor = new GraphExecutor({
3493
3925
  graph,
3494
3926
  parentAgent: this,
3927
+ parentSignal: controller.signal,
3495
3928
  plan: savedPlan,
3496
3929
  maxConcurrency: options?.graphConfig?.maxConcurrency ?? 3,
3497
3930
  maxDepth: options?.graphConfig?.maxDepth ?? 2,
@@ -3499,6 +3932,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3499
3932
  emit: (event, data) => this.emit(event, data)
3500
3933
  });
3501
3934
  const finalPlan = await executor.execute();
3935
+ throwIfAborted(controller.signal);
3502
3936
  const resultLines = Object.entries(finalPlan.results).filter(([, r]) => r.status === "done" && r.output).map(([nodeId, r]) => `[${nodeId}] ${r.output.slice(0, 200)}`);
3503
3937
  const progress = executor.getProgress();
3504
3938
  const finalReply = resultLines.length > 0 ? `Graph resumed & completed (${progress.done}/${progress.total} nodes done).
@@ -3515,11 +3949,11 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3515
3949
  totalDuration: Date.now() - startTime
3516
3950
  };
3517
3951
  this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
3518
- await this.persistence.saveSession(session).catch(() => {
3519
- });
3952
+ await this.saveSessionSafely(session);
3520
3953
  return chatResult;
3521
3954
  } finally {
3522
- this.abortControllers.delete(sessionId);
3955
+ execution.cleanup();
3956
+ releaseLock();
3523
3957
  }
3524
3958
  }
3525
3959
  /** Get graph plan from session */
@@ -3540,6 +3974,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3540
3974
  const failedSteps = [];
3541
3975
  const total = workflow.steps.length;
3542
3976
  for (let i = 0; i < workflow.steps.length; i++) {
3977
+ throwIfAborted(ctx.signal);
3543
3978
  const step = workflow.steps[i];
3544
3979
  const interpolatedPrompt = interpolateTemplate(step.prompt, {
3545
3980
  inputs: params,
@@ -3556,7 +3991,8 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3556
3991
  try {
3557
3992
  const result = await ctx.agent.chatInner(interpolatedPrompt, ctx.sessionId, {
3558
3993
  maxTurns: step.maxTurns ?? 10,
3559
- tools: step.tools
3994
+ tools: step.tools,
3995
+ signal: ctx.signal
3560
3996
  });
3561
3997
  stepOutputs[step.name] = { output: result.reply };
3562
3998
  completedSteps.push(step.name);
@@ -3641,6 +4077,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3641
4077
  const executor = new GraphExecutor({
3642
4078
  graph,
3643
4079
  parentAgent: this,
4080
+ parentSignal: ctx.signal,
3644
4081
  plan,
3645
4082
  maxConcurrency: 3,
3646
4083
  maxDepth: 2,
@@ -3648,6 +4085,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3648
4085
  emit: (event, data) => this.emit(event, data)
3649
4086
  });
3650
4087
  const finalPlan = await executor.execute();
4088
+ throwIfAborted(ctx.signal);
3651
4089
  const aggregation = skill.subgraph?.aggregation ?? "concat";
3652
4090
  if (aggregation === "last") {
3653
4091
  const lastDone = Object.values(finalPlan.results).reverse().find((r) => r.status === "done" && r.output);
@@ -3686,6 +4124,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3686
4124
  const executor = new GraphExecutor({
3687
4125
  graph,
3688
4126
  parentAgent: this,
4127
+ parentSignal: ctx.signal,
3689
4128
  plan,
3690
4129
  maxConcurrency: 3,
3691
4130
  maxDepth: 2,
@@ -3693,6 +4132,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3693
4132
  emit: (event, data) => this.emit(event, data)
3694
4133
  });
3695
4134
  const finalPlan = await executor.execute();
4135
+ throwIfAborted(ctx.signal);
3696
4136
  const aggregation = subgraphDef.aggregation ?? "concat";
3697
4137
  if (aggregation === "last") {
3698
4138
  const lastDone = Object.values(finalPlan.results).reverse().find((r) => r.status === "done" && r.output);
@@ -3711,7 +4151,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3711
4151
  // ---- Session Management ----
3712
4152
  createSession(options) {
3713
4153
  const session = this.sessionManager.create(options);
3714
- this.currentSessionId = session.id;
4154
+ if (options?.activate !== false) this.currentSessionId = session.id;
3715
4155
  return session;
3716
4156
  }
3717
4157
  getSession(sessionId) {
@@ -3724,17 +4164,75 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3724
4164
  return this.sessionManager.search(query, options);
3725
4165
  }
3726
4166
  mergeSessions(fromId, toId, strategy) {
4167
+ this.ensureSessionIsNotBeingManaged(fromId);
4168
+ this.ensureSessionIsNotBeingManaged(toId);
3727
4169
  this.sessionManager.merge(fromId, toId, strategy);
3728
4170
  }
3729
4171
  deleteSession(sessionId) {
4172
+ this.ensureSessionIsNotBeingManaged(sessionId);
4173
+ this.removeLiveSession(sessionId);
4174
+ }
4175
+ removeLiveSession(sessionId) {
3730
4176
  this.sessionManager.delete(sessionId);
3731
4177
  if (this.currentSessionId === sessionId) {
3732
4178
  this.currentSessionId = null;
3733
4179
  }
3734
4180
  }
3735
4181
  archiveSession(sessionId) {
4182
+ this.ensureSessionIsNotBeingManaged(sessionId);
3736
4183
  this.sessionManager.archive(sessionId);
3737
4184
  }
4185
+ /** List committed history without loading it into the Agent. */
4186
+ async listStoredSessions(options) {
4187
+ await Promise.all(this.sessionStorageQueues.values());
4188
+ return (await this.persistence.listSessions(options)).map((session) => this.snapshotSession(session));
4189
+ }
4190
+ /** Load one stored session on demand. A live session takes precedence. */
4191
+ async restoreSession(sessionId, options) {
4192
+ return this.manageSessionStorage([sessionId], async () => {
4193
+ const session = await this.queueSessionStorage(sessionId, () => this.readSessionForStorage(sessionId));
4194
+ if (!session) return null;
4195
+ this.sessionManager.restore(session);
4196
+ if (options?.activate !== false) this.currentSessionId = sessionId;
4197
+ return session;
4198
+ });
4199
+ }
4200
+ /** Archive a stored or live session, publishing the update after commit. */
4201
+ async archiveStoredSession(sessionId) {
4202
+ return this.manageSessionStorage([sessionId], () => this.queueSessionStorage(sessionId, async () => {
4203
+ const session = await this.requireSessionForStorage(sessionId);
4204
+ await this.commitStoredSession(session, { ...session, status: "archived", updatedAt: Date.now() });
4205
+ }));
4206
+ }
4207
+ /** Commit an idle session, then release its live copy. Restore it by ID when needed. */
4208
+ async unloadSession(sessionId) {
4209
+ return this.manageSessionStorage([sessionId], () => this.queueSessionStorage(sessionId, async () => {
4210
+ const session = this.sessionManager.get(sessionId);
4211
+ if (!session) return false;
4212
+ await this.persistence.saveSession(this.snapshotSession(session));
4213
+ this.removeLiveSession(sessionId);
4214
+ return true;
4215
+ }));
4216
+ }
4217
+ /** Merge history into the target session and commit the target. */
4218
+ async mergeStoredSessions(fromId, toId, strategy) {
4219
+ return this.manageSessionStorage([fromId, toId], () => this.queueSessionStorage(toId, async () => {
4220
+ const from = await this.requireSessionForStorage(fromId);
4221
+ const to = fromId === toId ? from : await this.requireSessionForStorage(toId);
4222
+ const updated = { ...to, messages: [...to.messages] };
4223
+ SessionManager.mergeContent(from, updated, strategy);
4224
+ await this.commitStoredSession(to, updated);
4225
+ }));
4226
+ }
4227
+ /** Delete committed and live history, including when a chat save is pending. */
4228
+ async deleteStoredSession(sessionId) {
4229
+ return this.manageSessionStorage([sessionId], (state) => this.queueSessionStorage(sessionId, async () => {
4230
+ await this.persistence.deleteSession(sessionId);
4231
+ state.deleted = true;
4232
+ this.abortControllers.get(sessionId)?.abort();
4233
+ this.removeLiveSession(sessionId);
4234
+ }), { allowRunning: true });
4235
+ }
3738
4236
  // ---- Context Management ----
3739
4237
  getContext(sessionId, format = "window") {
3740
4238
  const session = this.sessionManager.getOrThrow(sessionId);
@@ -3750,16 +4248,45 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3750
4248
  return this.contextManager.search(allMessages, query, options);
3751
4249
  }
3752
4250
  async compressContext(sessionId, strategy) {
4251
+ this.ensureSessionIsNotBeingManaged(sessionId);
3753
4252
  const session = this.sessionManager.getOrThrow(sessionId);
3754
- const result = await this.contextManager.compress(session.messages, strategy);
4253
+ this.compressingSessions.set(sessionId, (this.compressingSessions.get(sessionId) ?? 0) + 1);
4254
+ try {
4255
+ const result = await this.contextManager.compress(session.messages, strategy);
4256
+ session.messages = result.messages;
4257
+ session.tokenCount = result.afterTokens;
4258
+ session.updatedAt = Date.now();
4259
+ if (result.afterTokens < result.beforeTokens) {
4260
+ this.emit("context:compress", { sessionId, beforeTokens: result.beforeTokens, afterTokens: result.afterTokens, reason: "manual" });
4261
+ }
4262
+ return { beforeTokens: result.beforeTokens, afterTokens: result.afterTokens, compressionRatio: result.afterTokens / (result.beforeTokens || 1) };
4263
+ } finally {
4264
+ const remaining = this.compressingSessions.get(sessionId) - 1;
4265
+ if (remaining) this.compressingSessions.set(sessionId, remaining);
4266
+ else this.compressingSessions.delete(sessionId);
4267
+ }
4268
+ }
4269
+ async prepareOverflowRetry(session, rejected, systemPrompt, tools, signal) {
4270
+ const result = await this.contextManager.compressForOverflow(session.messages, rejected, systemPrompt, tools, signal);
4271
+ if (!result) return;
4272
+ throwIfAborted(signal);
3755
4273
  session.messages = result.messages;
3756
4274
  session.tokenCount = result.afterTokens;
3757
4275
  session.updatedAt = Date.now();
3758
- this.emit("context:compress", { sessionId, beforeTokens: result.beforeTokens, afterTokens: result.afterTokens, reason: "manual" });
3759
- return { beforeTokens: result.beforeTokens, afterTokens: result.afterTokens, compressionRatio: result.afterTokens / (result.beforeTokens || 1) };
4276
+ this.emit("context:compress", {
4277
+ sessionId: session.id,
4278
+ beforeTokens: rejected.usedTokens,
4279
+ afterTokens: result.window.usedTokens,
4280
+ reason: "manual"
4281
+ });
4282
+ return result.window.messages;
3760
4283
  }
3761
4284
  // ---- Runtime Config ----
3762
4285
  configure(config) {
4286
+ if (this.config.llmProxyMode !== void 0 || config.llmProxyMode !== void 0) {
4287
+ this.configureProxy(config);
4288
+ return;
4289
+ }
3763
4290
  if (config.llm) {
3764
4291
  Object.assign(this.config.llm, config.llm);
3765
4292
  this.llmAdapter = new OpenAIAdapter(this.config.llm);
@@ -3769,6 +4296,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3769
4296
  if (config.serverUrl) {
3770
4297
  this.config.serverUrl = config.serverUrl;
3771
4298
  this.transport = new HttpTransport(config.serverUrl, this.config.authToken, this.config.headers);
4299
+ this.mcpStatus = "unchecked";
3772
4300
  }
3773
4301
  if (config.authToken) {
3774
4302
  this.config.authToken = config.authToken;
@@ -3777,6 +4305,33 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3777
4305
  this.config.systemPrompt = config.systemPrompt;
3778
4306
  }
3779
4307
  }
4308
+ configureProxy(config) {
4309
+ const current = this.config;
4310
+ const next = {
4311
+ ...current,
4312
+ llm: { ...current.llm, ...config.llm },
4313
+ llmProxyMode: config.llmProxyMode === void 0 ? current.llmProxyMode : config.llmProxyMode,
4314
+ serverUrl: config.serverUrl === void 0 ? current.serverUrl : config.serverUrl,
4315
+ authToken: config.authToken ?? current.authToken,
4316
+ headers: config.headers ?? current.headers,
4317
+ systemPrompt: config.systemPrompt ?? current.systemPrompt
4318
+ };
4319
+ const { llm, serverUrl } = resolveProxyConfig(next);
4320
+ const transportChanged = next.serverUrl !== current.serverUrl || next.authToken !== current.authToken || next.headers !== current.headers || current.llmProxyMode === void 0;
4321
+ this.config = next;
4322
+ if (config.llm || next.llmProxyMode !== current.llmProxyMode || next.llmProxyMode === "listdoing-server" && (next.serverUrl !== current.serverUrl || next.authToken !== current.authToken)) {
4323
+ this.llmAdapter = new OpenAIAdapter(llm, { rejectRedirects: true });
4324
+ this.contextManager.setModel(llm.model);
4325
+ this.contextManager.setSummarizer(createLLMSummarizer(this.llmAdapter));
4326
+ }
4327
+ if (transportChanged) {
4328
+ this.transport = serverUrl ? new HttpTransport(serverUrl, next.authToken, next.headers) : new LocalTransport();
4329
+ this.mcpStatus = "unchecked";
4330
+ for (const skill of this.skillRegistry.list()) {
4331
+ if (this.discoveredServerSkills.has(skill)) this.skillRegistry.unregister(skill.name);
4332
+ }
4333
+ }
4334
+ }
3780
4335
  // ---- Events ----
3781
4336
  on(event, handler) {
3782
4337
  if (!this.eventHandlers.has(event)) {
@@ -3797,10 +4352,15 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3797
4352
  }
3798
4353
  // ---- Private Helpers ----
3799
4354
  /**
3800
- * Stream LLM response token-by-token via the adapter's chatStream.
3801
- * Forwards llm_text events to the caller and collects the full StandardLLMResponse.
4355
+ * Forward streaming events, or emit the complete response for a non-streaming adapter.
3802
4356
  */
3803
4357
  async *streamLlmResponse(messages, tools, signal) {
4358
+ if (typeof this.llmAdapter.chatStream !== "function") {
4359
+ const response = await this.llmAdapter.chat(messages, tools, { signal });
4360
+ if (response.reasoning) yield { type: "llm_thinking", content: response.reasoning };
4361
+ if (response.content) yield { type: "llm_text", content: response.content };
4362
+ return response;
4363
+ }
3804
4364
  let fullResponse = null;
3805
4365
  for await (const event of this.llmAdapter.chatStream(messages, tools, { signal })) {
3806
4366
  if (event.type === "llm_text" || event.type === "llm_thinking") {
@@ -3894,59 +4454,65 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3894
4454
  });
3895
4455
  }
3896
4456
  // ── Parallel tool execution helpers ──
3897
- planToolExecution(toolCalls, chatOptions, requireApproval) {
4457
+ planToolExecution(toolCalls, chatOptions, requireApproval, runContext, exposedTools) {
3898
4458
  const items = toolCalls.map((tc) => {
3899
4459
  const isMcp = this.mcpRouter.isMcpTool(tc.name);
3900
- const skillDef = this.skillRegistry.get(tc.name);
4460
+ const scopedSkill = runContext?.registry.get(tc.name);
4461
+ const skillDef = scopedSkill ?? this.skillRegistry.get(tc.name);
3901
4462
  const location = isMcp ? "server" : skillDef?.location === "server" ? "server" : "local";
3902
- const permPolicy = this.checkToolPermission(skillDef, tc.arguments, chatOptions, requireApproval);
4463
+ const permPolicy = scopedSkill && !exposedTools.some((tool) => tool.name === tc.name) ? "disabled" : this.checkToolPermission(skillDef, tc.arguments, chatOptions, requireApproval);
3903
4464
  const parallelSafe = skillDef?.parallelSafe ?? true;
3904
4465
  return { toolCall: tc, skillDef, location, permPolicy, parallelSafe };
3905
4466
  });
4467
+ const exclusiveSkill = runContext?.exclusiveSkill;
4468
+ if (exclusiveSkill) {
4469
+ const exclusiveCall = items.find((item) => item.toolCall.name === exclusiveSkill && item.permPolicy !== "disabled");
4470
+ if (exclusiveCall) {
4471
+ for (const item of items) {
4472
+ if (item !== exclusiveCall) {
4473
+ item.skippedReason = `Skipped because ${exclusiveSkill} must run alone. Reconsider this call after receiving the interaction result.`;
4474
+ }
4475
+ }
4476
+ }
4477
+ }
3906
4478
  const canParallelize = items.every((item) => item.parallelSafe);
3907
4479
  return { canParallelize, items };
3908
4480
  }
3909
- async processApprovalQueue(items, sessionId, emit, yieldEvent) {
4481
+ async *processApprovalQueue(items, sessionId, signal) {
3910
4482
  for (const item of items) {
3911
- if (item.permPolicy === "disabled") {
3912
- const denyResult = {
3913
- content: [{ type: "text", text: `Tool "${item.toolCall.name}" is disabled in current permission mode.` }],
3914
- isError: true
3915
- };
3916
- item.resolved = true;
3917
- item.result = denyResult;
3918
- this.addToolResult(sessionId, item.toolCall, denyResult);
3919
- emit("tool:result", { sessionId, toolName: item.toolCall.name, isError: true, summary: "Permission denied (disabled)", duration: 0 });
3920
- if (yieldEvent) {
3921
- yieldEvent({ type: "tool_result", callId: item.toolCall.id, toolName: item.toolCall.name, isError: true, summary: "Permission denied (disabled)", duration: 0 });
3922
- }
3923
- } else if (item.permPolicy === "approval" || item.permPolicy === "force-approval") {
3924
- emit("tool:approval", { sessionId, toolName: item.toolCall.name, arguments: item.toolCall.arguments, callId: item.toolCall.id });
3925
- if (yieldEvent) {
3926
- yieldEvent({ type: "tool_approval", callId: item.toolCall.id, toolName: item.toolCall.name, arguments: item.toolCall.arguments });
3927
- }
3928
- const approved = await this.waitForApproval(item.toolCall.id);
3929
- if (!approved) {
3930
- const rejectResult = {
3931
- content: [{ type: "text", text: "Tool call rejected by user." }],
3932
- isError: true
3933
- };
3934
- item.resolved = true;
3935
- item.result = rejectResult;
3936
- this.addToolResult(sessionId, item.toolCall, rejectResult);
3937
- emit("tool:result", { sessionId, toolName: item.toolCall.name, isError: true, summary: "Rejected", duration: 0 });
3938
- if (yieldEvent) {
3939
- yieldEvent({ type: "tool_result", callId: item.toolCall.id, toolName: item.toolCall.name, isError: true, summary: "Rejected", duration: 0 });
3940
- }
4483
+ throwIfAborted(signal);
4484
+ const tc = item.toolCall;
4485
+ this.emit("tool:call", { sessionId, toolName: tc.name, arguments: tc.arguments, location: item.location });
4486
+ yield { type: "tool_call", callId: tc.id, toolName: tc.name, arguments: tc.arguments, location: item.location };
4487
+ let reason;
4488
+ if (tc.parseError) reason = `Invalid tool arguments: ${tc.parseError}`;
4489
+ else if (item.permPolicy === "disabled") reason = `Tool "${tc.name}" is disabled in current permission mode.`;
4490
+ else if (item.skippedReason) reason = item.skippedReason;
4491
+ else if (item.permPolicy === "approval" || item.permPolicy === "force-approval") {
4492
+ const decision = this.waitForApproval(tc.id, signal);
4493
+ decision.catch(() => {
4494
+ });
4495
+ this.emit("tool:approval", { sessionId, toolName: tc.name, arguments: tc.arguments, callId: tc.id });
4496
+ yield { type: "tool_approval", callId: tc.id, toolName: tc.name, arguments: tc.arguments };
4497
+ try {
4498
+ if (!await decision) reason = "Tool call rejected by user.";
4499
+ } finally {
4500
+ this.approvalResolvers.get(tc.id)?.resolve(false);
3941
4501
  }
3942
4502
  }
4503
+ throwIfAborted(signal);
4504
+ if (reason) {
4505
+ const result = { content: [{ type: "text", text: reason }], isError: true };
4506
+ item.resolved = true;
4507
+ item.result = result;
4508
+ this.addToolResult(sessionId, tc, result);
4509
+ this.emit("tool:result", { sessionId, toolName: tc.name, isError: true, summary: reason, duration: 0 });
4510
+ yield { type: "tool_result", callId: tc.id, toolName: tc.name, isError: true, summary: reason, duration: 0 };
4511
+ }
3943
4512
  }
3944
4513
  }
3945
4514
  async executeToolBatch(items, sessionId, controller, canParallelize, emit) {
3946
4515
  const traceEntries = [];
3947
- for (const item of items) {
3948
- emit("tool:call", { sessionId, toolName: item.toolCall.name, arguments: item.toolCall.arguments, location: item.location });
3949
- }
3950
4516
  for (const item of items) {
3951
4517
  if (item.resolved) {
3952
4518
  const summary = item.result?.content.filter((c) => c.type === "text").map((c) => c.text.slice(0, 100)).join(" ") ?? "";
@@ -4002,18 +4568,41 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
4002
4568
  traceEntries.push({ callId: item.toolCall.id, toolName: item.toolCall.name, location: item.location, arguments: item.toolCall.arguments, result: { isError: result.isError ?? false, summary }, duration });
4003
4569
  }
4004
4570
  }
4571
+ throwIfAborted(controller.signal);
4005
4572
  return traceEntries;
4006
4573
  }
4007
- async executeToolBatchStream(items, sessionId, controller, canParallelize, yieldEvent) {
4008
- const traceEntries = [];
4009
- for (const item of items) {
4010
- yieldEvent({ type: "tool_call", callId: item.toolCall.id, toolName: item.toolCall.name, arguments: item.toolCall.arguments, location: item.location });
4011
- if (item.resolved) {
4012
- const summary = item.result?.content.filter((c) => c.type === "text").map((c) => c.text.slice(0, 100)).join(" ") ?? "";
4013
- yieldEvent({ type: "tool_result", callId: item.toolCall.id, toolName: item.toolCall.name, isError: true, summary, duration: 0 });
4014
- traceEntries.push({ callId: item.toolCall.id, toolName: item.toolCall.name, location: item.location, arguments: item.toolCall.arguments, result: { isError: true, summary }, duration: 0 });
4574
+ async *streamToolBatch(items, sessionId, controller, parallel) {
4575
+ const events = [];
4576
+ let wake;
4577
+ let done = false;
4578
+ let failure;
4579
+ const task = this.executeToolBatchStream(items, sessionId, controller, parallel, (event) => {
4580
+ events.push(event);
4581
+ wake?.();
4582
+ }).then(() => {
4583
+ done = true;
4584
+ wake?.();
4585
+ }, (error) => {
4586
+ failure = error;
4587
+ done = true;
4588
+ wake?.();
4589
+ });
4590
+ try {
4591
+ while (!done || events.length) {
4592
+ if (events.length) yield events.shift();
4593
+ else await new Promise((resolve) => {
4594
+ wake = resolve;
4595
+ });
4015
4596
  }
4597
+ if (failure) throw failure;
4598
+ throwIfAborted(controller.signal);
4599
+ } finally {
4600
+ if (!done) controller.abort();
4601
+ await task;
4016
4602
  }
4603
+ }
4604
+ async executeToolBatchStream(items, sessionId, controller, canParallelize, yieldEvent) {
4605
+ const traceEntries = [];
4017
4606
  const executableItems = items.filter((item) => !item.resolved);
4018
4607
  if (executableItems.length === 0) return traceEntries;
4019
4608
  if (executableItems.length === 1) {
@@ -4063,6 +4652,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
4063
4652
  traceEntries.push({ callId: item.toolCall.id, toolName: item.toolCall.name, location: item.location, arguments: item.toolCall.arguments, result: { isError: result.isError ?? false, summary }, duration });
4064
4653
  }
4065
4654
  }
4655
+ throwIfAborted(controller.signal);
4066
4656
  return traceEntries;
4067
4657
  }
4068
4658
  /**
@@ -4070,28 +4660,39 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
4070
4660
  */
4071
4661
  async executeToolCall(toolCall, sessionId, controller) {
4072
4662
  const execStart = Date.now();
4663
+ throwIfAborted(controller.signal);
4664
+ if (toolCall.parseError) return { content: [{ type: "text", text: toolCall.parseError }], isError: true };
4073
4665
  if (this.mcpRouter.isMcpTool(toolCall.name)) {
4074
4666
  try {
4075
- const result = await this.mcpRouter.call(toolCall.name, toolCall.arguments);
4076
- return { content: [{ type: "text", text: typeof result === "string" ? result : JSON.stringify(result) }] };
4667
+ const result = await this.mcpRouter.call(toolCall.name, toolCall.arguments, { signal: controller.signal });
4668
+ throwIfAborted(controller.signal);
4669
+ return { content: [{ type: "text", text: typeof result === "string" ? result : JSON.stringify(result) ?? "" }] };
4077
4670
  } catch (err) {
4078
- return { content: [{ type: "text", text: `MCP error: ${err.message}` }], isError: true };
4671
+ throwIfAborted(controller.signal);
4672
+ return { content: [{ type: "text", text: `MCP error: ${errorMessage(err)}` }], isError: true };
4079
4673
  }
4080
4674
  }
4081
- const skillDef = this.skillRegistry.get(toolCall.name);
4675
+ const scope = this.runContexts.get(controller.signal);
4676
+ const skillDef = scope?.registry.get(toolCall.name) ?? this.skillRegistry.get(toolCall.name);
4082
4677
  const location = skillDef?.location === "server" ? "server" : "local";
4083
4678
  try {
4084
4679
  if (location === "server" && this.transport instanceof HttpTransport) {
4085
- return await this.executeServerSkill(toolCall);
4680
+ return await this.executeServerSkill(toolCall, controller.signal);
4086
4681
  } else {
4087
- const invokeFn = (skillName, params) => this.skillExecutor.invokeDirect(skillName, params, {
4088
- sessionId,
4089
- agent: this,
4090
- signal: controller.signal,
4091
- depth: 0,
4092
- invoke: invokeFn
4093
- });
4094
- return await this.skillExecutor.execute(toolCall.name, toolCall.arguments, {
4682
+ const executorFor = (name) => scope?.registry.has(name) ? scope.executor : this.skillExecutor;
4683
+ const invokeFn = async (skillName, params) => {
4684
+ if (skillName === scope?.exclusiveSkill) {
4685
+ return { isError: true, content: [{ type: "text", text: "This interaction requires a top-level tool call." }] };
4686
+ }
4687
+ return executorFor(skillName).invokeDirect(skillName, params, {
4688
+ sessionId,
4689
+ agent: this,
4690
+ signal: controller.signal,
4691
+ depth: 0,
4692
+ invoke: invokeFn
4693
+ });
4694
+ };
4695
+ return await executorFor(toolCall.name).execute(toolCall.name, toolCall.arguments, {
4095
4696
  sessionId,
4096
4697
  agent: this,
4097
4698
  signal: controller.signal,
@@ -4100,14 +4701,146 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
4100
4701
  });
4101
4702
  }
4102
4703
  } catch (err) {
4103
- return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
4704
+ if (controller.signal.aborted || err?.name === "AbortError") throw abortError();
4705
+ return { content: [{ type: "text", text: `Error: ${errorMessage(err)}` }], isError: true };
4706
+ }
4707
+ }
4708
+ prepareRunContext(signal, context) {
4709
+ if (!context) return this.runContexts.get(signal);
4710
+ const registry = new SkillRegistry();
4711
+ for (const skill of context.skills ?? []) {
4712
+ if (!skill.handler || skill.location === "server" || this.mcpRouter.isMcpTool(skill.name) || this.skillRegistry.has(skill.name) || registry.has(skill.name)) {
4713
+ throw new ACPError(ACPErrorCodes.SKILL_REGISTRATION_FAILED, `Invalid or conflicting run-local skill: ${skill.name}`);
4714
+ }
4715
+ registry.register({ ...skill });
4716
+ }
4717
+ const scope = {
4718
+ systemPromptSuffix: context.systemPromptSuffix,
4719
+ exclusiveSkill: context.exclusiveSkill && registry.has(context.exclusiveSkill) ? context.exclusiveSkill : void 0,
4720
+ registry,
4721
+ executor: new SkillExecutor(registry, void 0, { validateInputs: this.config.skills?.validateInputs })
4722
+ };
4723
+ this.runContexts.set(signal, scope);
4724
+ return scope;
4725
+ }
4726
+ buildRunSystemPrompt(session, runContext) {
4727
+ const base = this.resolveSystemPrompt(session.id, session.systemPrompt ?? this.config.systemPrompt ?? "");
4728
+ return runContext?.systemPromptSuffix ? [base, runContext.systemPromptSuffix].filter(Boolean).join("\n\n") : base;
4729
+ }
4730
+ listRunTools(runContext, nested) {
4731
+ const tools = this.skillRegistry.listToolDefs();
4732
+ if (!runContext) return tools;
4733
+ const localTools = runContext.registry.listToolDefs();
4734
+ const visibleTools = nested && runContext.exclusiveSkill ? localTools.filter((tool) => tool.name !== runContext.exclusiveSkill) : localTools;
4735
+ return [...tools.filter((tool) => !runContext.registry.has(tool.name)), ...visibleTools];
4736
+ }
4737
+ beginExecution(sessionId, options) {
4738
+ const previous = this.abortControllers.get(sessionId);
4739
+ const controller = new AbortController();
4740
+ const unlinkRoot = linkAbort(previous?.signal, controller);
4741
+ const unlinkParent = linkAbort(options?.signal, controller);
4742
+ const inherited = previous && this.runContexts.get(previous.signal);
4743
+ if (inherited) this.runContexts.set(controller.signal, inherited);
4744
+ if (!previous) this.abortControllers.set(sessionId, controller);
4745
+ return { controller, cleanup: () => {
4746
+ controller.abort();
4747
+ this.runContexts.delete(controller.signal);
4748
+ unlinkRoot();
4749
+ unlinkParent();
4750
+ if (this.abortControllers.get(sessionId) === controller) this.abortControllers.delete(sessionId);
4751
+ } };
4752
+ }
4753
+ ensureSessionIsNotBeingManaged(sessionId) {
4754
+ if (this.managingSessions.has(sessionId)) {
4755
+ throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${sessionId} has a pending storage operation`);
4756
+ }
4757
+ }
4758
+ async manageSessionStorage(sessionIds, operation, options) {
4759
+ for (const id of sessionIds) {
4760
+ this.ensureSessionIsNotBeingManaged(id);
4761
+ if (this.compressingSessions.has(id)) {
4762
+ throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${id} is compressing its context`);
4763
+ }
4764
+ if (!options?.allowRunning && (this.runningSessions.has(id) || this.abortControllers.has(id))) {
4765
+ throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${id} is already running a chat`);
4766
+ }
4104
4767
  }
4768
+ const state = { deleted: false };
4769
+ for (const id of sessionIds) this.managingSessions.set(id, state);
4770
+ try {
4771
+ return await operation(state);
4772
+ } finally {
4773
+ for (const id of sessionIds) this.managingSessions.delete(id);
4774
+ }
4775
+ }
4776
+ async readSessionForStorage(sessionId) {
4777
+ const current = this.sessionManager.get(sessionId);
4778
+ if (current) return current;
4779
+ const stored = await this.persistence.loadSession(sessionId);
4780
+ return stored ? this.snapshotSession(stored) : null;
4781
+ }
4782
+ async requireSessionForStorage(sessionId) {
4783
+ const session = await this.readSessionForStorage(sessionId);
4784
+ if (session) return session;
4785
+ throw new ACPError(ACPErrorCodes.SESSION_NOT_FOUND, `Session not found: ${sessionId}`, { sessionId });
4786
+ }
4787
+ async commitStoredSession(original, updated) {
4788
+ await this.persistence.saveSession(this.snapshotSession(updated));
4789
+ if (this.sessionManager.get(original.id) === original) Object.assign(original, updated);
4790
+ }
4791
+ snapshotSession(session) {
4792
+ try {
4793
+ return structuredClone(session);
4794
+ } catch {
4795
+ return { ...session, messages: session.messages.map((message) => ({ ...message })) };
4796
+ }
4797
+ }
4798
+ queueSessionStorage(sessionId, operation) {
4799
+ const previous = this.sessionStorageQueues.get(sessionId) ?? Promise.resolve();
4800
+ const result = previous.then(operation);
4801
+ const settled = result.then(() => {
4802
+ }, () => {
4803
+ });
4804
+ this.sessionStorageQueues.set(sessionId, settled);
4805
+ void settled.then(() => {
4806
+ if (this.sessionStorageQueues.get(sessionId) === settled) this.sessionStorageQueues.delete(sessionId);
4807
+ });
4808
+ return result;
4809
+ }
4810
+ saveSessionSafely(session) {
4811
+ const snapshot = this.snapshotSession(session);
4812
+ const management = this.managingSessions.get(session.id);
4813
+ return this.queueSessionStorage(session.id, async () => {
4814
+ if (management?.deleted) return;
4815
+ try {
4816
+ await this.persistence.saveSession(snapshot);
4817
+ } catch (error) {
4818
+ try {
4819
+ this.config.onDiagnostic?.({ kind: "persistence", sessionId: session.id, message: errorMessage(error) });
4820
+ } catch {
4821
+ }
4822
+ this.emit("persistence:error", { sessionId: session.id, message: errorMessage(error) });
4823
+ }
4824
+ });
4105
4825
  }
4106
4826
  ensureInitialized() {
4107
4827
  if (!this.initialized) {
4108
4828
  throw new ACPError(ACPErrorCodes.AGENT_NOT_INITIALIZED, "Agent not initialized. Call init() first.");
4109
4829
  }
4110
4830
  }
4831
+ reportPromptCacheUsage(sessionId, promptCacheUsage) {
4832
+ if (!this.config.llm.collectPromptCacheUsage || !promptCacheUsage) return;
4833
+ try {
4834
+ this.config.onDiagnostic?.({
4835
+ kind: "llm:prompt-cache",
4836
+ message: "Model reported prompt cache usage",
4837
+ sessionId,
4838
+ model: this.config.llm.model,
4839
+ promptCacheUsage
4840
+ });
4841
+ } catch {
4842
+ }
4843
+ }
4111
4844
  ensureSession() {
4112
4845
  if (this.currentSessionId) {
4113
4846
  const existing = this.sessionManager.get(this.currentSessionId);
@@ -4140,9 +4873,10 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
4140
4873
  this.sessionManager.addMessage(sessionId, { role: "tool", content: result.content, toolCallId: toolCall.id });
4141
4874
  }
4142
4875
  }
4143
- async executeServerSkill(toolCall) {
4876
+ async executeServerSkill(toolCall, signal) {
4144
4877
  const transport = this.transport;
4145
- const result = await transport.send("tools/call", { name: toolCall.name, arguments: toolCall.arguments });
4878
+ const result = await transport.send("tools/call", { name: toolCall.name, arguments: toolCall.arguments }, { signal });
4879
+ if (!result || !Array.isArray(result.content)) throw new Error("Server skill returned an invalid result");
4146
4880
  return result;
4147
4881
  }
4148
4882
  };