@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.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
@@ -125,6 +135,7 @@ var ACPErrorCodes = {
125
135
  LLM_CALL_FAILED: -32130,
126
136
  LLM_EMPTY_RESPONSE: -32135,
127
137
  LLM_TOKEN_LIMIT: -32131,
138
+ LLM_PROXY_CONFIG_INVALID: -32136,
128
139
  LLM_RATE_LIMIT: -32132,
129
140
  LLM_PROVIDER_UNAVAILABLE: -32133,
130
141
  TOKEN_BUDGET_EXCEEDED: -32134,
@@ -189,9 +200,46 @@ var OpenAIContentAdapter = class {
189
200
  }
190
201
  };
191
202
 
203
+ // src/runtime/abort.ts
204
+ function abortError() {
205
+ const error = new Error("Request aborted");
206
+ error.name = "AbortError";
207
+ return error;
208
+ }
209
+ function throwIfAborted(signal) {
210
+ if (signal?.aborted) throw abortError();
211
+ }
212
+ function linkAbort(parent, child) {
213
+ const abort = () => child.abort();
214
+ if (parent?.aborted) abort();
215
+ else parent?.addEventListener("abort", abort, { once: true });
216
+ return () => parent?.removeEventListener("abort", abort);
217
+ }
218
+ function errorMessage(error) {
219
+ return error instanceof Error ? error.message : String(error ?? "Unknown error");
220
+ }
221
+ function abortableDelay(ms, signal) {
222
+ throwIfAborted(signal);
223
+ return new Promise((resolve, reject) => {
224
+ const cleanup = () => signal?.removeEventListener("abort", abort);
225
+ const timer = setTimeout(() => {
226
+ cleanup();
227
+ resolve();
228
+ }, ms);
229
+ const abort = () => {
230
+ clearTimeout(timer);
231
+ cleanup();
232
+ reject(abortError());
233
+ };
234
+ signal?.addEventListener("abort", abort, { once: true });
235
+ });
236
+ }
237
+
192
238
  // src/llm/openai.ts
193
239
  var OpenAIAdapter = class {
194
- constructor(config) {
240
+ constructor(config, options) {
241
+ this.options = options;
242
+ this.verified = false;
195
243
  this.config = config;
196
244
  this.contentAdapter = new OpenAIContentAdapter();
197
245
  }
@@ -210,6 +258,10 @@ var OpenAIAdapter = class {
210
258
  const base = this.getBaseUrl().replace(/\/+$/, "");
211
259
  return `${base}/chat/completions`;
212
260
  }
261
+ getConnectionStatus() {
262
+ const endpoint = this.getEndpoint().trim().split(/[?#]/, 1)[0].replace(/^(https?:\/\/|\/\/)[^/]*@/i, "$1");
263
+ return { endpoint, verified: this.verified };
264
+ }
213
265
  buildHeaders() {
214
266
  const headers = {
215
267
  "Content-Type": "application/json"
@@ -260,51 +312,63 @@ var OpenAIAdapter = class {
260
312
  }));
261
313
  }
262
314
  async chat(messages, tools, options) {
263
- if (this.config.adapter === "custom" && this.config.transformRequest && this.config.transformResponse) {
264
- const reqBody = this.config.transformRequest(messages, tools, {});
265
- const res2 = await fetch(this.getEndpoint(), {
315
+ const request = this.createRequest(options?.signal);
316
+ try {
317
+ if (this.config.adapter === "custom" && this.config.transformRequest && this.config.transformResponse) {
318
+ const reqBody = this.config.transformRequest(messages, tools, {});
319
+ const res2 = await this.fetchResponse({
320
+ method: "POST",
321
+ headers: this.buildHeaders(),
322
+ body: JSON.stringify(reqBody),
323
+ signal: request.controller.signal
324
+ });
325
+ if (!res2.ok) {
326
+ const text = await res2.text().catch(() => "");
327
+ this.checkContextOverflow(res2.status, text);
328
+ throw new Error(`LLM request failed: ${res2.status} ${text}`);
329
+ }
330
+ const data2 = await res2.json();
331
+ const response2 = this.config.transformResponse(data2);
332
+ if (response2?.content || response2?.toolCalls?.length) this.verified = true;
333
+ return response2;
334
+ }
335
+ const body = {
336
+ model: this.config.model,
337
+ messages: this.messagesToOpenAI(messages)
338
+ };
339
+ if (tools.length > 0) {
340
+ body.tools = this.toolsToOpenAI(tools);
341
+ }
342
+ if (this.config.temperature !== void 0) {
343
+ body.temperature = this.config.temperature;
344
+ }
345
+ if (this.config.maxTokens !== void 0) {
346
+ body.max_tokens = this.config.maxTokens;
347
+ }
348
+ if (this.config.extraBody) {
349
+ Object.assign(body, this.config.extraBody);
350
+ }
351
+ const res = await this.fetchResponse({
266
352
  method: "POST",
267
353
  headers: this.buildHeaders(),
268
- body: JSON.stringify(reqBody),
269
- signal: options?.signal
354
+ body: JSON.stringify(body),
355
+ signal: request.controller.signal
270
356
  });
271
- if (!res2.ok) {
272
- const text = await res2.text().catch(() => "");
273
- this.checkContextOverflow(res2.status, text);
274
- throw new Error(`LLM request failed: ${res2.status} ${text}`);
357
+ if (!res.ok) {
358
+ const text = await res.text().catch(() => "");
359
+ this.checkContextOverflow(res.status, text);
360
+ throw new Error(`LLM request failed: ${res.status} ${text}`);
275
361
  }
276
- const data2 = await res2.json();
277
- return this.config.transformResponse(data2);
278
- }
279
- const body = {
280
- model: this.config.model,
281
- messages: this.messagesToOpenAI(messages)
282
- };
283
- if (tools.length > 0) {
284
- body.tools = this.toolsToOpenAI(tools);
285
- }
286
- if (this.config.temperature !== void 0) {
287
- body.temperature = this.config.temperature;
288
- }
289
- if (this.config.maxTokens !== void 0) {
290
- body.max_tokens = this.config.maxTokens;
291
- }
292
- if (this.config.extraBody) {
293
- Object.assign(body, this.config.extraBody);
294
- }
295
- const res = await fetch(this.getEndpoint(), {
296
- method: "POST",
297
- headers: this.buildHeaders(),
298
- body: JSON.stringify(body),
299
- signal: options?.signal
300
- });
301
- if (!res.ok) {
302
- const text = await res.text().catch(() => "");
303
- this.checkContextOverflow(res.status, text);
304
- throw new Error(`LLM request failed: ${res.status} ${text}`);
362
+ const data = await res.json();
363
+ const response = this.parseResponse(data);
364
+ this.verified = true;
365
+ return response;
366
+ } catch (error) {
367
+ if (request.controller.signal.aborted && !options?.signal?.aborted) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "LLM request timed out", { reason: "timeout" });
368
+ throw error;
369
+ } finally {
370
+ request.cleanup();
305
371
  }
306
- const data = await res.json();
307
- return this.parseResponse(data);
308
372
  }
309
373
  /**
310
374
  * Stream LLM responses token-by-token via SSE.
@@ -312,115 +376,206 @@ var OpenAIAdapter = class {
312
376
  * Collects the full response and returns it at the end.
313
377
  */
314
378
  async *chatStream(messages, tools, options) {
315
- const body = {
316
- model: this.config.model,
317
- messages: this.messagesToOpenAI(messages),
318
- stream: true
319
- };
320
- if (tools.length > 0) {
321
- body.tools = this.toolsToOpenAI(tools);
322
- }
323
- if (this.config.temperature !== void 0) {
324
- body.temperature = this.config.temperature;
325
- }
326
- if (this.config.maxTokens !== void 0) {
327
- body.max_tokens = this.config.maxTokens;
328
- }
329
- if (this.config.extraBody) {
330
- Object.assign(body, this.config.extraBody);
331
- }
332
- const res = await fetch(this.getEndpoint(), {
333
- method: "POST",
334
- headers: this.buildHeaders(),
335
- body: JSON.stringify(body),
336
- signal: options?.signal
337
- });
338
- if (!res.ok) {
339
- const text = await res.text().catch(() => "");
340
- this.checkContextOverflow(res.status, text);
341
- throw new Error(`LLM request failed: ${res.status} ${text}`);
342
- }
343
- const reader = res.body?.getReader();
344
- if (!reader) {
345
- const data = await res.json();
346
- const parsed = this.parseResponse(data);
347
- if (parsed.content) {
348
- yield { type: "llm_text", content: parsed.content };
349
- }
379
+ if (this.config.adapter === "custom" && this.config.transformRequest && this.config.transformResponse) {
380
+ const response = await this.chat(messages, tools, options);
381
+ if (response.reasoning) yield { type: "llm_thinking", content: response.reasoning };
382
+ if (response.content) yield { type: "llm_text", content: response.content };
383
+ yield { type: "llm_response", response };
350
384
  return;
351
385
  }
352
- const decoder = new TextDecoder();
353
- let buffer = "";
354
- let content = "";
355
- let reasoning = "";
356
- const toolCallMap = /* @__PURE__ */ new Map();
386
+ const request = this.createRequest(options?.signal);
357
387
  try {
358
- while (true) {
359
- const { done, value } = await reader.read();
360
- if (done) break;
361
- buffer += decoder.decode(value, { stream: true });
362
- const lines = buffer.split("\n");
363
- buffer = lines.pop() ?? "";
364
- for (const line of lines) {
365
- const trimmed = line.trim();
366
- if (!trimmed || !trimmed.startsWith("data:")) continue;
367
- const data = trimmed.slice(5).trim();
368
- if (data === "[DONE]") continue;
369
- try {
370
- const chunk = JSON.parse(data);
371
- const delta = chunk.choices?.[0]?.delta;
372
- if (!delta) continue;
373
- if (delta.content) {
374
- content += delta.content;
375
- yield { type: "llm_text", content: delta.content };
376
- }
377
- if (delta.reasoning_content) {
378
- reasoning += delta.reasoning_content;
379
- yield { type: "llm_thinking", content: delta.reasoning_content };
388
+ const body = {
389
+ model: this.config.model,
390
+ messages: this.messagesToOpenAI(messages),
391
+ stream: true,
392
+ ...this.config.collectPromptCacheUsage && { stream_options: { include_usage: true } }
393
+ };
394
+ if (tools.length > 0) {
395
+ body.tools = this.toolsToOpenAI(tools);
396
+ }
397
+ if (this.config.temperature !== void 0) {
398
+ body.temperature = this.config.temperature;
399
+ }
400
+ if (this.config.maxTokens !== void 0) {
401
+ body.max_tokens = this.config.maxTokens;
402
+ }
403
+ if (this.config.extraBody) {
404
+ Object.assign(body, this.config.extraBody);
405
+ }
406
+ const res = await this.fetchResponse({
407
+ method: "POST",
408
+ headers: this.buildHeaders(),
409
+ body: JSON.stringify(body),
410
+ signal: request.controller.signal
411
+ });
412
+ if (!res.ok) {
413
+ const text = await res.text().catch(() => "");
414
+ this.checkContextOverflow(res.status, text);
415
+ throw new Error(`LLM request failed: ${res.status} ${text}`);
416
+ }
417
+ const reader = res.body?.getReader();
418
+ if (!reader) {
419
+ const data = await res.json();
420
+ const parsed = this.parseResponse(data);
421
+ this.verified = true;
422
+ if (parsed.content) {
423
+ yield { type: "llm_text", content: parsed.content };
424
+ }
425
+ yield { type: "llm_response", response: parsed };
426
+ return;
427
+ }
428
+ const decoder = new TextDecoder();
429
+ let buffer = "";
430
+ let content = "";
431
+ let reasoning = "";
432
+ let finishReason;
433
+ let promptCacheUsage;
434
+ let sawDone = false;
435
+ const toolCallMap = /* @__PURE__ */ new Map();
436
+ try {
437
+ while (true) {
438
+ if (options?.signal?.aborted) {
439
+ this.throwIfAborted(options.signal);
440
+ }
441
+ const { done, value } = await this.readChunk(reader, request.controller);
442
+ if (done) {
443
+ buffer += decoder.decode();
444
+ if (!buffer.trim()) break;
445
+ buffer += "\n";
446
+ }
447
+ buffer += decoder.decode(value, { stream: true });
448
+ const lines = buffer.split("\n");
449
+ buffer = lines.pop() ?? "";
450
+ for (const line of lines) {
451
+ const trimmed = line.trim();
452
+ if (!trimmed || !trimmed.startsWith("data:")) continue;
453
+ const data = trimmed.slice(5).trim();
454
+ if (data === "[DONE]") {
455
+ sawDone = true;
456
+ continue;
380
457
  }
381
- if (delta.tool_calls) {
382
- for (const tc of delta.tool_calls) {
383
- const idx = tc.index ?? 0;
384
- if (!toolCallMap.has(idx)) {
385
- toolCallMap.set(idx, {
386
- id: tc.id ?? "",
387
- name: tc.function?.name ?? "",
388
- arguments: ""
389
- });
458
+ try {
459
+ const chunk = JSON.parse(data);
460
+ if (chunk.error) throw new Error(chunk.error.message ?? "LLM stream error");
461
+ if (this.config.collectPromptCacheUsage && chunk.usage) {
462
+ promptCacheUsage = this.parsePromptCacheUsage(chunk.usage) ?? promptCacheUsage;
463
+ }
464
+ if (chunk.choices?.[0]?.finish_reason) finishReason = chunk.choices[0].finish_reason;
465
+ const delta = chunk.choices?.[0]?.delta;
466
+ if (!delta) continue;
467
+ if (delta.content) {
468
+ content += delta.content;
469
+ yield { type: "llm_text", content: delta.content };
470
+ }
471
+ if (delta.reasoning_content) {
472
+ reasoning += delta.reasoning_content;
473
+ yield { type: "llm_thinking", content: delta.reasoning_content };
474
+ }
475
+ if (delta.tool_calls) {
476
+ for (const tc of delta.tool_calls) {
477
+ const idx = tc.index ?? 0;
478
+ if (!toolCallMap.has(idx)) {
479
+ toolCallMap.set(idx, {
480
+ id: tc.id ?? "",
481
+ name: tc.function?.name ?? "",
482
+ arguments: ""
483
+ });
484
+ }
485
+ const entry = toolCallMap.get(idx);
486
+ if (tc.id) entry.id = tc.id;
487
+ if (tc.function?.name) entry.name = tc.function.name;
488
+ if (tc.function?.arguments) entry.arguments += tc.function.arguments;
390
489
  }
391
- const entry = toolCallMap.get(idx);
392
- if (tc.id) entry.id = tc.id;
393
- if (tc.function?.name) entry.name = tc.function.name;
394
- if (tc.function?.arguments) entry.arguments += tc.function.arguments;
395
490
  }
491
+ } catch (error) {
492
+ throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, `Invalid LLM stream: ${errorMessage(error)}`);
493
+ }
494
+ if (options?.signal?.aborted) {
495
+ this.throwIfAborted(options.signal);
396
496
  }
397
- } catch {
398
497
  }
498
+ if (done || sawDone) break;
399
499
  }
500
+ } finally {
501
+ await reader.cancel().catch(() => {
502
+ });
503
+ reader.releaseLock();
504
+ }
505
+ const toolCalls = [];
506
+ for (const [_, tc] of toolCallMap) {
507
+ toolCalls.push(this.parseToolCall(tc));
400
508
  }
509
+ throwIfAborted(request.controller.signal);
510
+ if (this.config.strictStream && !sawDone && !finishReason) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "LLM stream ended without a finish marker");
511
+ if (finishReason === "length") for (const call of toolCalls) call.parseError = "Tool response exceeded the output token limit";
512
+ if (content || toolCalls.length) this.verified = true;
513
+ yield {
514
+ type: "llm_response",
515
+ response: {
516
+ content,
517
+ reasoning: reasoning || void 0,
518
+ toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
519
+ usage: void 0,
520
+ ...promptCacheUsage && { promptCacheUsage },
521
+ finishReason: finishReason ?? (toolCalls.length > 0 ? "tool_calls" : "stop")
522
+ }
523
+ };
524
+ } catch (error) {
525
+ if (request.controller.signal.aborted && !options?.signal?.aborted) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "LLM request timed out", { reason: "timeout" });
526
+ throw error;
401
527
  } finally {
402
- reader.releaseLock();
528
+ request.cleanup();
529
+ }
530
+ }
531
+ parseToolCall(tc) {
532
+ if (!tc.id || !tc.name) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "Tool call is missing its id or name");
533
+ try {
534
+ const args = JSON.parse(tc.arguments || "{}");
535
+ if (!args || typeof args !== "object" || Array.isArray(args)) throw new Error("Tool arguments must be a JSON object");
536
+ return { id: tc.id, name: tc.name, arguments: args };
537
+ } catch (error) {
538
+ return { id: tc.id, name: tc.name, arguments: {}, parseError: errorMessage(error) };
539
+ }
540
+ }
541
+ createRequest(parent) {
542
+ const controller = new AbortController();
543
+ const unlink = linkAbort(parent, controller);
544
+ const timeout = this.config.requestTimeoutMs;
545
+ const timer = timeout && timeout > 0 ? setTimeout(() => controller.abort(), timeout) : void 0;
546
+ return { controller, cleanup: () => {
547
+ clearTimeout(timer);
548
+ unlink();
549
+ controller.abort();
550
+ } };
551
+ }
552
+ async fetchResponse(init) {
553
+ const retries = Math.max(0, Math.min(this.config.retry?.maxRetries ?? 0, 5));
554
+ const requestInit = this.options?.rejectRedirects ? { ...init, redirect: "error" } : init;
555
+ for (let attempt = 0; ; attempt++) {
556
+ throwIfAborted(init.signal ?? void 0);
557
+ const response = await fetch(this.getEndpoint(), requestInit);
558
+ if (attempt >= retries || ![429, 502, 503, 504].includes(response.status)) return response;
559
+ const retryAfter = Number(response.headers.get("retry-after"));
560
+ const delay = Number.isFinite(retryAfter) && retryAfter > 0 ? Math.min(retryAfter * 1e3, 3e4) : Math.min((this.config.retry?.baseDelayMs ?? 500) * 2 ** attempt, 3e4);
561
+ await response.body?.cancel();
562
+ await abortableDelay(delay, init.signal ?? void 0);
563
+ }
564
+ }
565
+ async readChunk(reader, controller) {
566
+ const idleMs = this.config.streamIdleTimeoutMs;
567
+ if (!idleMs || idleMs <= 0) return reader.read();
568
+ let timer;
569
+ try {
570
+ return await Promise.race([reader.read(), new Promise((_, reject) => {
571
+ timer = setTimeout(() => {
572
+ reject(new Error("LLM stream idle timeout"));
573
+ controller.abort();
574
+ }, idleMs);
575
+ })]);
576
+ } finally {
577
+ clearTimeout(timer);
403
578
  }
404
- const toolCalls = [];
405
- for (const [_, tc] of toolCallMap) {
406
- let args = {};
407
- try {
408
- args = JSON.parse(tc.arguments || "{}");
409
- } catch {
410
- args = {};
411
- }
412
- toolCalls.push({ id: tc.id, name: tc.name, arguments: args });
413
- }
414
- yield {
415
- type: "llm_response",
416
- response: {
417
- content,
418
- reasoning: reasoning || void 0,
419
- toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
420
- usage: void 0,
421
- finishReason: toolCalls.length > 0 ? "tool_calls" : "stop"
422
- }
423
- };
424
579
  }
425
580
  /**
426
581
  * Check if the error response indicates context length exceeded.
@@ -438,6 +593,14 @@ var OpenAIAdapter = class {
438
593
  throw new ACPError(ACPErrorCodes.CONTEXT_OVERFLOW, errMsg, { isContextOverflow: true });
439
594
  }
440
595
  }
596
+ /** AbortError for the given signal — used so a cancelled stream surfaces an abort, not a parse error. */
597
+ throwIfAborted(signal) {
598
+ if (signal?.aborted) {
599
+ const err = new Error("Request aborted");
600
+ err.name = "AbortError";
601
+ throw err;
602
+ }
603
+ }
441
604
  parseResponse(data) {
442
605
  const choice = data.choices?.[0];
443
606
  if (!choice) {
@@ -449,17 +612,9 @@ var OpenAIAdapter = class {
449
612
  const toolCalls = [];
450
613
  if (message?.tool_calls?.length) {
451
614
  for (const tc of message.tool_calls) {
452
- let args = {};
453
- try {
454
- args = JSON.parse(tc.function.arguments || "{}");
455
- } catch {
456
- args = {};
457
- }
458
- toolCalls.push({
459
- id: tc.id,
460
- name: tc.function.name,
461
- arguments: args
462
- });
615
+ const parsed = this.parseToolCall({ id: tc.id, name: tc.function?.name, arguments: tc.function?.arguments });
616
+ if (choice.finish_reason === "length") parsed.parseError = "Tool response exceeded the output token limit";
617
+ toolCalls.push(parsed);
463
618
  }
464
619
  }
465
620
  if (!content && !toolCalls.length) {
@@ -468,16 +623,70 @@ var OpenAIAdapter = class {
468
623
  "LLM returned empty response with no tool calls"
469
624
  );
470
625
  }
626
+ const promptCacheUsage = this.config.collectPromptCacheUsage ? this.parsePromptCacheUsage(data.usage) : void 0;
471
627
  return {
472
628
  content,
473
629
  reasoning,
474
630
  toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
475
631
  usage: data.usage ? { input: data.usage.prompt_tokens, output: data.usage.completion_tokens } : void 0,
632
+ ...promptCacheUsage && { promptCacheUsage },
476
633
  finishReason: choice.finish_reason
477
634
  };
478
635
  }
636
+ parsePromptCacheUsage(raw) {
637
+ const asCount = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : void 0;
638
+ const cachedTokens = asCount(raw?.prompt_tokens_details?.cached_tokens) ?? asCount(raw?.prompt_cache_hit_tokens);
639
+ const cacheWriteTokens = asCount(raw?.prompt_tokens_details?.cache_write_tokens);
640
+ if (cachedTokens === void 0 && cacheWriteTokens === void 0) return void 0;
641
+ const inputTokens = asCount(raw?.prompt_tokens);
642
+ return {
643
+ ...inputTokens !== void 0 && { inputTokens },
644
+ ...cachedTokens !== void 0 && { cachedTokens },
645
+ ...cacheWriteTokens !== void 0 && { cacheWriteTokens }
646
+ };
647
+ }
479
648
  };
480
649
 
650
+ // src/llm/proxy.ts
651
+ function invalidProxyConfig(message) {
652
+ return new ACPError(ACPErrorCodes.LLM_PROXY_CONFIG_INVALID, message);
653
+ }
654
+ function proxyBaseUrl(value, field) {
655
+ const input = typeof value === "string" ? value.trim() : "";
656
+ const absolute = /^https?:\/\//i.test(input);
657
+ const relative = input.startsWith("/") && !input.startsWith("//");
658
+ if (!absolute && !relative || /[\\?#\u0000-\u001f]/.test(input)) {
659
+ throw invalidProxyConfig(`${field} must be an HTTP(S) base URL or a same-origin path without a query or fragment`);
660
+ }
661
+ let url;
662
+ try {
663
+ url = new URL(input, "http://listdoing.invalid");
664
+ } catch {
665
+ throw invalidProxyConfig(`${field} is not a valid proxy URL`);
666
+ }
667
+ if (url.username || url.password) throw invalidProxyConfig(`${field} must not include credentials`);
668
+ if (!absolute && url.pathname.startsWith("//")) throw invalidProxyConfig(`${field} must stay on the same origin`);
669
+ const normalized = (absolute ? url.href : url.pathname).replace(/\/+$/, "");
670
+ return normalized || "/";
671
+ }
672
+ function resolveProxyConfig(config) {
673
+ const mode = config.llmProxyMode;
674
+ if (mode === void 0) return { llm: config.llm, serverUrl: config.serverUrl };
675
+ if (mode !== "backend" && mode !== "listdoing-server") {
676
+ throw invalidProxyConfig("llmProxyMode must be backend or listdoing-server");
677
+ }
678
+ const serverUrl = config.serverUrl !== void 0 || mode === "listdoing-server" ? proxyBaseUrl(config.serverUrl, "serverUrl") : void 0;
679
+ const baseUrl = mode === "listdoing-server" ? `${serverUrl.replace(/\/+$/, "")}/v1` : proxyBaseUrl(config.llm.baseUrl, "llm.baseUrl");
680
+ if (mode === "listdoing-server" && config.llm.baseUrl !== void 0 && proxyBaseUrl(config.llm.baseUrl, "llm.baseUrl") !== baseUrl) {
681
+ throw invalidProxyConfig("llm.baseUrl must match the ListDoing Server proxy or be omitted");
682
+ }
683
+ if (baseUrl.endsWith("/chat/completions")) {
684
+ throw invalidProxyConfig("llm.baseUrl must end before /chat/completions");
685
+ }
686
+ const apiKey = config.llm.apiKey ?? (mode === "listdoing-server" ? config.authToken : void 0);
687
+ return { llm: { ...config.llm, baseUrl, apiKey }, serverUrl };
688
+ }
689
+
481
690
  // src/skill/registry.ts
482
691
  var SkillRegistry = class {
483
692
  constructor() {
@@ -498,6 +707,11 @@ var SkillRegistry = class {
498
707
  get(name) {
499
708
  return this.skills.get(name)?.definition;
500
709
  }
710
+ /** Identity of this registration, replaced on every register call. */
711
+ getRegistrationIdentity(skill) {
712
+ const entry = this.skills.get(skill.name);
713
+ return entry?.definition === skill ? entry : skill;
714
+ }
501
715
  getHandler(name) {
502
716
  return this.skills.get(name)?.handler;
503
717
  }
@@ -552,7 +766,9 @@ var SkillRegistry = class {
552
766
 
553
767
  // src/skill/executor.ts
554
768
  var SkillExecutor = class {
555
- constructor(registry, worker) {
769
+ constructor(registry, worker, options) {
770
+ this.options = options;
771
+ this.validators = /* @__PURE__ */ new WeakMap();
556
772
  this.registry = registry;
557
773
  this.worker = worker ?? {
558
774
  async execute(def, params, ctx) {
@@ -580,19 +796,41 @@ var SkillExecutor = class {
580
796
  throw new ACPError(ACPErrorCodes.SKILL_NOT_FOUND, `Skill not found: ${name}`, { name });
581
797
  }
582
798
  const timeout = def.timeout ?? 3e4;
799
+ throwIfAborted(context.signal);
800
+ if (this.options?.validateInputs) {
801
+ const validate = await this.getValidator(def);
802
+ if (!validate(params)) throw new ACPError(ACPErrorCodes.SKILL_EXECUTION_FAILED, `Invalid arguments for ${name}`, { errors: validate.errors });
803
+ }
583
804
  const controller = new AbortController();
584
- const onParentAbort = () => controller.abort();
585
- context.signal.addEventListener("abort", onParentAbort);
586
- const timeoutId = timeout > 0 ? setTimeout(() => controller.abort(), timeout) : null;
805
+ const unlink = linkAbort(context.signal, controller);
806
+ let timedOut = false;
807
+ const timeoutId = timeout > 0 ? setTimeout(() => {
808
+ timedOut = true;
809
+ controller.abort();
810
+ }, timeout) : null;
587
811
  const execContext = {
588
812
  ...context,
589
813
  signal: controller.signal
590
814
  };
815
+ let removeDeadline = () => {
816
+ };
591
817
  try {
592
- const result = await this.worker.execute(def, params, execContext);
818
+ const execution = Promise.resolve().then(() => {
819
+ throwIfAborted(controller.signal);
820
+ return this.worker.execute(def, params, execContext);
821
+ });
822
+ const result = def.timeoutMode === "deadline" ? await Promise.race([execution, new Promise((_, reject) => {
823
+ const abort = () => reject(abortError());
824
+ controller.signal.addEventListener("abort", abort, { once: true });
825
+ removeDeadline = () => controller.signal.removeEventListener("abort", abort);
826
+ })]) : await execution;
827
+ throwIfAborted(context.signal);
828
+ if (timedOut) throw new Error("Skill exceeded its timeout");
829
+ if (!result || !Array.isArray(result.content)) throw new Error("Skill returned an invalid result (content must be an array)");
593
830
  return result;
594
831
  } catch (err) {
595
- if (err.name === "AbortError" || controller.signal.aborted) {
832
+ if (context.signal.aborted) throw abortError();
833
+ if (timedOut) {
596
834
  throw new ACPError(
597
835
  ACPErrorCodes.SKILL_EXECUTION_TIMEOUT,
598
836
  `Skill execution timeout: ${name}`,
@@ -602,19 +840,44 @@ var SkillExecutor = class {
602
840
  if (err instanceof ACPError) throw err;
603
841
  throw new ACPError(
604
842
  ACPErrorCodes.SKILL_EXECUTION_FAILED,
605
- `Skill execution failed: ${name}: ${err.message}`,
606
- { name, error: err.message }
843
+ `Skill execution failed: ${name}: ${errorMessage(err)}`,
844
+ { name, error: errorMessage(err) }
607
845
  );
608
846
  } finally {
609
847
  if (timeoutId) clearTimeout(timeoutId);
610
- context.signal.removeEventListener("abort", onParentAbort);
848
+ unlink();
849
+ removeDeadline();
850
+ }
851
+ }
852
+ async getValidator(skill) {
853
+ const { default: Ajv } = await import("ajv");
854
+ const registration = this.registry.getRegistrationIdentity(skill);
855
+ const schema = skill.inputSchema;
856
+ let signature;
857
+ try {
858
+ signature = JSON.stringify(schema, function(key, value) {
859
+ if (value !== this[key]) throw new TypeError("Schema cannot be cached");
860
+ if (value === null || typeof value === "string" || typeof value === "boolean" || Number.isFinite(value)) return value;
861
+ if (typeof value === "object") {
862
+ const prototype = Object.getPrototypeOf(value);
863
+ if (prototype === Object.prototype || prototype === null || prototype === Array.prototype) return value;
864
+ }
865
+ throw new TypeError("Schema cannot be cached");
866
+ });
867
+ } catch {
611
868
  }
869
+ const cached = this.validators.get(registration);
870
+ if (cached && cached.schema === signature) return cached.validate;
871
+ const validate = new Ajv({ strict: false, allErrors: true }).compile(schema);
872
+ if (signature !== void 0) this.validators.set(registration, { schema: signature, validate });
873
+ return validate;
612
874
  }
613
875
  /**
614
876
  * Directly invoke a skill by name — used by ctx.invoke().
615
877
  * No LLM loop, no approval. Shares parent's signal and timeout context.
616
878
  */
617
879
  async invokeDirect(name, params, context) {
880
+ throwIfAborted(context.signal);
618
881
  const def = this.registry.get(name);
619
882
  if (!def) {
620
883
  return {
@@ -638,8 +901,9 @@ var SkillExecutor = class {
638
901
  try {
639
902
  return await handler(params, context);
640
903
  } catch (err) {
904
+ if (context.signal.aborted || err?.name === "AbortError") throw abortError();
641
905
  return {
642
- content: [{ type: "text", text: `Invoke failed: ${err.message}` }],
906
+ content: [{ type: "text", text: `Invoke failed: ${errorMessage(err)}` }],
643
907
  isError: true
644
908
  };
645
909
  }
@@ -815,13 +1079,13 @@ var McpRouter = class {
815
1079
  /**
816
1080
  * Route a tool call to the appropriate MCP Server.
817
1081
  */
818
- async call(prefixedName, args) {
1082
+ async call(prefixedName, args, options) {
819
1083
  const { server, tool } = this.parseMcpToolName(prefixedName);
820
1084
  const conn = this.connections.get(server);
821
1085
  if (!conn) {
822
1086
  throw new Error(`MCP Server "${server}" not connected. Available: ${Array.from(this.connections.keys()).join(", ") || "none"}`);
823
1087
  }
824
- return conn.call(tool, args);
1088
+ return conn.call(tool, args, options);
825
1089
  }
826
1090
  /**
827
1091
  * Resolve `mcp:` prefixed tool names from a tools list to ToolDef[].
@@ -894,7 +1158,7 @@ function fileInputsToContentBlocks(files) {
894
1158
  }
895
1159
 
896
1160
  // src/session/manager.ts
897
- var SessionManager = class {
1161
+ var SessionManager = class _SessionManager {
898
1162
  constructor() {
899
1163
  this.sessions = /* @__PURE__ */ new Map();
900
1164
  }
@@ -920,6 +1184,9 @@ var SessionManager = class {
920
1184
  get(sessionId) {
921
1185
  return this.sessions.get(sessionId);
922
1186
  }
1187
+ restore(session) {
1188
+ this.sessions.set(session.id, session);
1189
+ }
923
1190
  getOrThrow(sessionId) {
924
1191
  const session = this.sessions.get(sessionId);
925
1192
  if (!session) {
@@ -981,13 +1248,16 @@ var SessionManager = class {
981
1248
  session.status = "archived";
982
1249
  session.updatedAt = Date.now();
983
1250
  }
984
- merge(fromId, toId, strategy = "summary_and_artifacts") {
1251
+ merge(fromId, toId, strategy) {
985
1252
  const from = this.getOrThrow(fromId);
986
1253
  const to = this.getOrThrow(toId);
1254
+ _SessionManager.mergeContent(from, to, strategy);
1255
+ }
1256
+ static mergeContent(from, to, strategy = "summary_and_artifacts") {
987
1257
  if (strategy === "full") {
988
1258
  to.messages.push(...from.messages);
989
1259
  } else {
990
- const summary = this.createSummary(from);
1260
+ const summary = _SessionManager.createSummary(from);
991
1261
  to.messages.push({
992
1262
  role: "user",
993
1263
  content: `[Context from session "${from.title || from.id}"]:
@@ -1009,7 +1279,7 @@ ${artifacts.map((a) => contentToPreview(a.content, 200)).join("\n")}`
1009
1279
  to.messageCount = to.messages.length;
1010
1280
  to.updatedAt = Date.now();
1011
1281
  }
1012
- createSummary(session) {
1282
+ static createSummary(session) {
1013
1283
  const userMsgs = session.messages.filter((m) => m.role === "user");
1014
1284
  if (userMsgs.length === 0) return "Empty session";
1015
1285
  return userMsgs.slice(-3).map((m) => contentToPreview(m.content, 200)).join("\n");
@@ -1136,6 +1406,64 @@ function estimateRequestTokens(messages, tools, systemPrompt) {
1136
1406
  return total;
1137
1407
  }
1138
1408
 
1409
+ // src/context/messages.ts
1410
+ function repairMessages(messages) {
1411
+ const result = [];
1412
+ for (let i = 0; i < messages.length; i++) {
1413
+ const message = messages[i];
1414
+ if (!message || !["user", "assistant", "system", "tool"].includes(message.role)) continue;
1415
+ if (message.role === "tool") continue;
1416
+ if (message.role === "assistant" && message.toolCalls?.length) {
1417
+ const tools = [];
1418
+ while (i + 1 < messages.length && messages[i + 1]?.role === "tool") tools.push(messages[++i]);
1419
+ const ids = /* @__PURE__ */ new Set();
1420
+ const calls = message.toolCalls.filter((call) => {
1421
+ if (!call?.id || !call.name || ids.has(call.id)) return false;
1422
+ ids.add(call.id);
1423
+ return true;
1424
+ });
1425
+ if (calls.length) {
1426
+ result.push({ ...message, toolCalls: calls });
1427
+ for (const call of calls) {
1428
+ const tool = tools.find((tool2) => tool2.toolCallId === call.id);
1429
+ result.push(tool ? normalizeContent(tool) : {
1430
+ role: "tool",
1431
+ toolCallId: call.id,
1432
+ content: "Tool execution was interrupted; its outcome is unknown. Do not repeat a side-effecting operation without checking its state."
1433
+ });
1434
+ }
1435
+ } else if (message.content) result.push(normalizeContent({ ...message, toolCalls: void 0 }));
1436
+ } else result.push(normalizeContent(message));
1437
+ }
1438
+ return result;
1439
+ }
1440
+ function normalizeContent(message) {
1441
+ const content = message.content;
1442
+ if (content == null || content === "" || Array.isArray(content) && content.length === 0) {
1443
+ return { ...message, content: "." };
1444
+ }
1445
+ if (typeof content !== "string") {
1446
+ if (!Array.isArray(content)) return { ...message, content: "." };
1447
+ const valid = content.filter((block) => {
1448
+ if (!block || typeof block !== "object") return false;
1449
+ if (block.type === "text") return typeof block.text === "string";
1450
+ if (block.type === "image") return typeof block.data === "string" && !!block.data && typeof block.mimeType === "string";
1451
+ if (block.type === "resource") return typeof block.resource?.uri === "string" && (block.resource.text === void 0 || typeof block.resource.text === "string");
1452
+ return false;
1453
+ });
1454
+ if (valid.length !== content.length) return { ...message, content: valid.length ? valid : "." };
1455
+ }
1456
+ return message;
1457
+ }
1458
+ function messageGroups(messages) {
1459
+ const groups = [];
1460
+ for (const message of repairMessages(messages)) {
1461
+ if (message.role === "tool" && groups.length) groups[groups.length - 1].push(message);
1462
+ else groups.push([message]);
1463
+ }
1464
+ return groups;
1465
+ }
1466
+
1139
1467
  // src/context/manager.ts
1140
1468
  var ContextManager = class {
1141
1469
  constructor(config, model) {
@@ -1188,26 +1516,21 @@ var ContextManager = class {
1188
1516
  if (systemPrompt) {
1189
1517
  result.push({ role: "system", content: systemPrompt });
1190
1518
  }
1191
- const sanitized = messages.map((m) => {
1192
- if (m.role === "assistant" && !m.content && !m.toolCalls?.length) {
1193
- return { ...m, content: "." };
1194
- }
1195
- return m;
1196
- });
1197
- const reversed = [...sanitized].reverse();
1198
1519
  const selected = [];
1199
1520
  let usedByMessages = 0;
1200
- for (const msg of reversed) {
1201
- const msgTokens = estimateMessageTokens(msg);
1202
- if (usedByMessages + msgTokens > remainingBudget) break;
1203
- selected.unshift(msg);
1204
- usedByMessages += msgTokens;
1521
+ for (const group of messageGroups(messages).reverse()) {
1522
+ const tokens = group.reduce((sum, msg) => sum + estimateMessageTokens(msg), 0);
1523
+ if (usedByMessages + tokens > remainingBudget) break;
1524
+ selected.unshift(...group);
1525
+ usedByMessages += tokens;
1205
1526
  }
1206
1527
  result.push(...selected);
1207
1528
  if (selected.length === 0 && messages.length > 0) {
1208
1529
  const lastUser = [...messages].reverse().find((m) => m.role === "user");
1209
1530
  if (lastUser) {
1210
- result.push(lastUser);
1531
+ const fallback = repairMessages([lastUser]);
1532
+ result.push(...fallback);
1533
+ usedByMessages += fallback.reduce((sum, message) => sum + estimateMessageTokens(message), 0);
1211
1534
  }
1212
1535
  }
1213
1536
  return { messages: result, usedTokens: fixedTokens + usedByMessages, budget };
@@ -1218,37 +1541,64 @@ var ContextManager = class {
1218
1541
  * summarization (when a summarizer is set and enabled) over mechanical truncation.
1219
1542
  */
1220
1543
  async ensureContextFits(messages, systemPrompt, tools, model, signal) {
1544
+ throwIfAborted(signal);
1221
1545
  if (model) this.setModel(model);
1222
- const { usedTokens, budget } = this.buildWindow(messages, systemPrompt, tools);
1546
+ const { budget } = this.buildWindow(messages, systemPrompt, tools);
1547
+ const beforeTokens = estimateRequestTokens(repairMessages(messages), tools, systemPrompt);
1548
+ if (this.config.autoCompress === false) return { messages, wasCompressed: false };
1223
1549
  const threshold = this.config.compressThreshold ?? 0.85;
1224
- if (usedTokens / budget <= threshold) {
1550
+ if (beforeTokens / budget <= threshold) {
1225
1551
  return { messages, wasCompressed: false };
1226
1552
  }
1227
1553
  const useLLM = this.config.useLLMSummary !== false && !!this.summarizer;
1228
1554
  let result;
1229
1555
  if (useLLM) {
1230
- const llmResult = await this.compressWithLLM(messages, systemPrompt, tools, budget, signal);
1231
- result = llmResult ?? this.compressSync(messages, "hybrid");
1556
+ const llmResult = await this.compressWithLLM(messages, systemPrompt, tools, budget, { signal, groupTools: true });
1557
+ result = llmResult ?? this.compressSync(messages, "hybrid", { groupTools: true });
1232
1558
  } else {
1233
- result = this.compressSync(messages, "hybrid");
1559
+ result = this.compressSync(messages, "hybrid", { groupTools: true });
1234
1560
  }
1235
- const afterCheck = this.buildWindow(result.messages, systemPrompt, tools);
1236
- if (afterCheck.usedTokens / afterCheck.budget > 1) {
1237
- console.warn(`[listdoing] Context still exceeds budget after compression (${afterCheck.usedTokens}/${afterCheck.budget} tokens). Truncating to recent messages only.`);
1238
- const truncated = messages.slice(-3);
1239
- return {
1240
- messages: truncated,
1241
- wasCompressed: true,
1242
- beforeTokens: result.beforeTokens,
1243
- afterTokens: truncated.reduce((sum, m) => sum + estimateMessageTokens(m), 0)
1244
- };
1561
+ let compressed = result.messages;
1562
+ let window2 = this.buildWindow(compressed, systemPrompt, tools);
1563
+ if (window2.usedTokens > budget) {
1564
+ console.warn(`[listdoing] Context still exceeds budget after compression (${window2.usedTokens}/${budget} tokens). Truncating to recent messages only.`);
1565
+ compressed = messageGroups(messages).slice(-3).flat();
1566
+ window2 = this.buildWindow(compressed, systemPrompt, tools);
1567
+ }
1568
+ if (estimateRequestTokens(compressed, tools, systemPrompt) >= beforeTokens || window2.usedTokens >= beforeTokens) {
1569
+ return { messages, wasCompressed: false };
1245
1570
  }
1246
1571
  return {
1247
- messages: result.messages,
1572
+ messages: compressed,
1248
1573
  wasCompressed: true,
1249
- beforeTokens: result.beforeTokens,
1250
- afterTokens: result.afterTokens
1574
+ beforeTokens,
1575
+ afterTokens: window2.usedTokens
1576
+ };
1577
+ }
1578
+ /** Build one smaller request after a provider rejects the current window. */
1579
+ async compressForOverflow(messages, rejected, systemPrompt, tools, signal) {
1580
+ throwIfAborted(signal);
1581
+ const fixedTokens = estimateRequestTokens([], tools, systemPrompt);
1582
+ const available = Math.min(rejected.budget, rejected.usedTokens) - fixedTokens;
1583
+ const historyBudget = Math.floor(available * 0.85);
1584
+ if (historyBudget <= 0) return;
1585
+ const budget = fixedTokens + historyBudget;
1586
+ const latestUser = [...messages].reverse().find((message) => message.role === "user");
1587
+ if (latestUser && estimateMessageTokens(latestUser) > historyBudget) return;
1588
+ const rejectedUserIndex = latestUser ? rejected.messages.lastIndexOf(latestUser) : -1;
1589
+ const activeToolResults = rejected.messages.slice(rejectedUserIndex + 1).filter((message) => message.role === "tool");
1590
+ const summarized = this.config.useLLMSummary !== false ? await this.compressWithLLM(messages, systemPrompt, tools, budget, { signal, groupTools: true }) : null;
1591
+ throwIfAborted(signal);
1592
+ const useCandidate = (result) => {
1593
+ const window2 = this.buildWindow(result.messages, systemPrompt, tools, budget);
1594
+ if (window2.usedTokens > budget || window2.usedTokens >= rejected.usedTokens) return;
1595
+ if (latestUser && !window2.messages.some((message) => message.role === "user" && message.content === latestUser.content)) return;
1596
+ const keptToolIds = new Set(window2.messages.filter((message) => message.role === "tool").map((message) => message.toolCallId));
1597
+ if (activeToolResults.some((message) => !keptToolIds.has(message.toolCallId))) return;
1598
+ return { ...result, window: window2 };
1251
1599
  };
1600
+ const candidate = summarized && useCandidate(summarized);
1601
+ return candidate || useCandidate(this.compressSync(messages, "hybrid", { maxTokens: historyBudget, groupTools: true }));
1252
1602
  }
1253
1603
  /**
1254
1604
  * Compress context by summarizing older messages. When strategy is
@@ -1267,7 +1617,8 @@ var ContextManager = class {
1267
1617
  return this.compressSync(messages, strat);
1268
1618
  }
1269
1619
  /** Mechanical (non-LLM) compression — the original truncation strategies. */
1270
- compressSync(messages, strategy) {
1620
+ compressSync(messages, strategy, options) {
1621
+ messages = repairMessages(messages);
1271
1622
  const estimateTokens = (msgs) => msgs.reduce((sum, m) => sum + estimateMessageTokens(m), 0);
1272
1623
  const beforeTokens = estimateTokens(messages);
1273
1624
  let compressed;
@@ -1294,13 +1645,14 @@ ${summaryText}` },
1294
1645
  case "token_budget":
1295
1646
  case "hybrid":
1296
1647
  default: {
1297
- const maxTokens = this.config.maxTokens ?? this.getBudget();
1648
+ const maxTokens = options?.maxTokens ?? this.config.maxTokens ?? this.getBudget();
1298
1649
  let totalTokens = 0;
1299
1650
  const kept = [];
1300
- for (const msg of [...messages].reverse()) {
1301
- const t = estimateMessageTokens(msg);
1651
+ const groups = options?.groupTools ? messageGroups(messages) : messages.map((message) => [message]);
1652
+ for (const group of groups.reverse()) {
1653
+ const t = estimateTokens(group);
1302
1654
  if (totalTokens + t > maxTokens) break;
1303
- kept.unshift(msg);
1655
+ kept.unshift(...group);
1304
1656
  totalTokens += t;
1305
1657
  }
1306
1658
  if (kept.length < messages.length) {
@@ -1317,16 +1669,18 @@ ${summaryText}` },
1317
1669
  break;
1318
1670
  }
1319
1671
  }
1320
- const afterTokens = estimateTokens(compressed);
1321
- return { messages: compressed, beforeTokens, afterTokens };
1672
+ const repaired = repairMessages(compressed);
1673
+ return { messages: repaired, beforeTokens, afterTokens: estimateTokens(repaired) };
1322
1674
  }
1323
1675
  /**
1324
1676
  * LLM-based semantic summarization. Returns null when it cannot be performed
1325
1677
  * (no summarizer, nothing to summarize, or the LLM call fails) so callers can
1326
1678
  * fall back to mechanical compression.
1327
1679
  */
1328
- async compressWithLLM(messages, systemPrompt, tools, budget, signal) {
1680
+ async compressWithLLM(messages, systemPrompt, tools, budget, options) {
1329
1681
  if (!this.summarizer) return null;
1682
+ const { signal, groupTools } = options ?? {};
1683
+ messages = repairMessages(messages);
1330
1684
  try {
1331
1685
  const estimateTokens = (msgs) => msgs.reduce((sum, m) => sum + estimateMessageTokens(m), 0);
1332
1686
  const beforeTokens = estimateTokens(messages);
@@ -1336,33 +1690,31 @@ ${summaryText}` },
1336
1690
  const summaryReserve = 2e3;
1337
1691
  const outputReserve = this.config.outputReserve ?? 1024;
1338
1692
  const remaining = budget - fixedTokens - summaryReserve - outputReserve;
1693
+ const groups = groupTools ? messageGroups(messages) : messages.map((message) => [message]);
1339
1694
  const kept = [];
1340
1695
  let used = 0;
1341
- if (remaining > 0) {
1342
- for (let i = messages.length - 1; i >= 0; i--) {
1343
- const t = estimateMessageTokens(messages[i]);
1344
- if (used + t > remaining) break;
1345
- kept.unshift(messages[i]);
1346
- used += t;
1347
- }
1696
+ for (let i = groups.length - 1; i >= 0; i--) {
1697
+ const t = estimateTokens(groups[i]);
1698
+ if (used + t > remaining) break;
1699
+ kept.unshift(groups[i]);
1700
+ used += t;
1348
1701
  }
1349
- const oldMessages = messages.slice(0, messages.length - kept.length);
1702
+ const oldMessages = groups.slice(0, groups.length - kept.length).flat();
1350
1703
  if (oldMessages.length === 0) {
1351
1704
  return null;
1352
1705
  }
1353
1706
  const summary = await this.summarizer(oldMessages, signal ? { signal } : void 0);
1354
1707
  const summaryMessage = { role: "user", content: `[\u4E0A\u4E0B\u6587\u6458\u8981]
1355
1708
  ${summary}` };
1356
- let result = [summaryMessage, ...kept];
1357
- let afterTokens = estimateTokens(result);
1358
- while (result.length > 2 && afterTokens > budget) {
1359
- const trimmed = [summaryMessage, ...result.slice(2)];
1360
- if (trimmed.length === result.length) break;
1361
- result = trimmed;
1362
- afterTokens = estimateTokens(result);
1709
+ const limit = groupTools ? budget - fixedTokens : budget;
1710
+ let afterTokens = estimateMessageTokens(summaryMessage) + used;
1711
+ while (kept.length > 1 && afterTokens > limit) {
1712
+ afterTokens -= estimateTokens(kept.shift());
1363
1713
  }
1364
- return { messages: result, beforeTokens, afterTokens };
1714
+ const repaired = repairMessages([summaryMessage, ...kept.flat()]);
1715
+ return { messages: repaired, beforeTokens, afterTokens: estimateTokens(repaired) };
1365
1716
  } catch (err) {
1717
+ throwIfAborted(signal);
1366
1718
  console.warn("[listdoing] LLM summarization failed, falling back to mechanical compression:", err);
1367
1719
  return null;
1368
1720
  }
@@ -1449,6 +1801,7 @@ function createLLMSummarizer(adapter, options) {
1449
1801
  return async (messages, opts) => {
1450
1802
  const formatted = formatMessagesForSummary(messages);
1451
1803
  const controller = new AbortController();
1804
+ const unlink = linkAbort(opts?.signal, controller);
1452
1805
  const timer = setTimeout(() => controller.abort(), timeoutMs);
1453
1806
  try {
1454
1807
  const response = await adapter.chat(
@@ -1457,7 +1810,7 @@ function createLLMSummarizer(adapter, options) {
1457
1810
  { role: "user", content: formatted }
1458
1811
  ],
1459
1812
  [],
1460
- { signal: opts?.signal ?? controller.signal }
1813
+ { signal: controller.signal }
1461
1814
  );
1462
1815
  const summary = (response.content ?? "").trim();
1463
1816
  if (!summary) {
@@ -1466,6 +1819,7 @@ function createLLMSummarizer(adapter, options) {
1466
1819
  return summary;
1467
1820
  } finally {
1468
1821
  clearTimeout(timer);
1822
+ unlink();
1469
1823
  }
1470
1824
  };
1471
1825
  }
@@ -1475,12 +1829,13 @@ var TaskPlanner = class {
1475
1829
  constructor(llmAdapter) {
1476
1830
  this.llmAdapter = llmAdapter;
1477
1831
  }
1478
- async plan(goal, availableTools) {
1832
+ async plan(goal, availableTools, signal) {
1479
1833
  const prompt = this.buildPlanningPrompt(goal, availableTools);
1480
1834
  const response = await this.llmAdapter.chat(
1481
1835
  [{ role: "user", content: prompt }],
1482
- []
1836
+ [],
1483
1837
  // No tools for the planning call itself
1838
+ { signal }
1484
1839
  );
1485
1840
  return this.parsePlan(goal, response.content);
1486
1841
  }
@@ -1799,17 +2154,19 @@ var GraphPlanner = class {
1799
2154
  constructor(llmAdapter) {
1800
2155
  this.llmAdapter = llmAdapter;
1801
2156
  }
1802
- async planGraph(goal, availableTools) {
2157
+ async planGraph(goal, availableTools, signal) {
1803
2158
  const prompt = this.buildGraphPlanningPrompt(goal, availableTools);
1804
2159
  let response;
1805
2160
  try {
1806
2161
  const result = await this.llmAdapter.chat(
1807
2162
  [{ role: "user", content: prompt }],
1808
- []
2163
+ [],
1809
2164
  // No tools for the planning call
2165
+ { signal }
1810
2166
  );
1811
2167
  response = result.content;
1812
2168
  } catch {
2169
+ throwIfAborted(signal);
1813
2170
  return this.fallbackSequentialGraph(goal);
1814
2171
  }
1815
2172
  return this.parseGraphPlan(goal, response);
@@ -1932,9 +2289,7 @@ var SubAgent = class {
1932
2289
  this.config = options.config;
1933
2290
  this.depth = options.depth;
1934
2291
  this.abortController = new AbortController();
1935
- if (options.parentSignal) {
1936
- options.parentSignal.addEventListener("abort", () => this.abortController.abort(), { once: true });
1937
- }
2292
+ this.unlinkParent = linkAbort(options.parentSignal, this.abortController);
1938
2293
  const maxDepth = this.config.maxDepth ?? 2;
1939
2294
  if (this.depth >= maxDepth) {
1940
2295
  this.config.canSpawnSubAgents = false;
@@ -1945,7 +2300,9 @@ var SubAgent = class {
1945
2300
  this.status = "running";
1946
2301
  const startTime = Date.now();
1947
2302
  try {
2303
+ throwIfAborted(this.abortController.signal);
1948
2304
  const session = this.parentAgent.createSession({
2305
+ activate: false,
1949
2306
  title: `SubAgent: ${this.nodeId}`,
1950
2307
  meta: {
1951
2308
  parentSessionId: this.parentAgent.getCurrentSessionId(),
@@ -1959,6 +2316,7 @@ var SubAgent = class {
1959
2316
  const prompt = this.buildPrompt(goal, upstreamOutputs);
1960
2317
  const options = {
1961
2318
  sessionId: session.id,
2319
+ signal: this.abortController.signal,
1962
2320
  maxTurns: this.config.maxTurns ?? 10,
1963
2321
  depth: this.depth + 1,
1964
2322
  taskMode: void 0
@@ -1982,6 +2340,7 @@ var SubAgent = class {
1982
2340
  sessionId: this.sessionId ?? void 0
1983
2341
  };
1984
2342
  }
2343
+ throwIfAborted(this.abortController.signal);
1985
2344
  this.status = "done";
1986
2345
  return {
1987
2346
  nodeId: this.nodeId,
@@ -2003,6 +2362,8 @@ var SubAgent = class {
2003
2362
  retryCount: 0,
2004
2363
  sessionId: this.sessionId ?? void 0
2005
2364
  };
2365
+ } finally {
2366
+ this.unlinkParent();
2006
2367
  }
2007
2368
  }
2008
2369
  /** Abort the sub-agent */
@@ -2047,42 +2408,49 @@ var GraphExecutor = class {
2047
2408
  this.maxDepth = options.maxDepth ?? 2;
2048
2409
  this.baseDepth = options.baseDepth ?? 0;
2049
2410
  this.abortController = new AbortController();
2411
+ this.unlinkParent = linkAbort(options.parentSignal, this.abortController);
2050
2412
  this.emit = options.emit;
2413
+ this.onNodeSettled = options.onNodeSettled;
2051
2414
  }
2052
2415
  /** Execute the entire graph, level by level */
2053
2416
  async execute() {
2054
- this.plan.status = "executing";
2055
- for (let level = 0; level < this.graph.getLevelCount(); level++) {
2056
- if (this.abortController.signal.aborted) {
2417
+ try {
2418
+ this.plan.status = "executing";
2419
+ for (let level = 0; level < this.graph.getLevelCount(); level++) {
2420
+ if (this.abortController.signal.aborted) {
2421
+ this.plan.status = "failed";
2422
+ return this.plan;
2423
+ }
2424
+ const nodes = this.graph.getLevel(level);
2425
+ const nodeIds = nodes.map((n) => n.id);
2426
+ this.emit("graph:level_start", { level, nodeIds });
2427
+ await this.executeLevel(level, nodes);
2428
+ const levelResults = nodeIds.map((id) => this.plan.results[id]).filter(Boolean);
2429
+ this.emit("graph:level_complete", { level, results: levelResults });
2430
+ }
2431
+ const progress = this.getProgress();
2432
+ if (progress.failed === 0 && progress.done === progress.total) {
2433
+ this.plan.status = "completed";
2434
+ this.emit("graph:complete", { plan: this.plan, progress });
2435
+ } else if (progress.done > 0) {
2436
+ this.plan.status = "partial";
2437
+ this.emit("graph:complete", { plan: this.plan, progress });
2438
+ } else {
2057
2439
  this.plan.status = "failed";
2058
- return this.plan;
2440
+ const failedNodes = Object.entries(this.plan.results).filter(([, r]) => r.status === "failed").map(([id]) => id);
2441
+ this.emit("graph:failed", { plan: this.plan, failedNodes });
2059
2442
  }
2060
- const nodes = this.graph.getLevel(level);
2061
- const nodeIds = nodes.map((n) => n.id);
2062
- this.emit("graph:level_start", { level, nodeIds });
2063
- await this.executeLevel(level, nodes);
2064
- const levelResults = nodeIds.map((id) => this.plan.results[id]).filter(Boolean);
2065
- this.emit("graph:level_complete", { level, results: levelResults });
2066
- }
2067
- const progress = this.getProgress();
2068
- if (progress.failed === 0 && progress.done === progress.total) {
2069
- this.plan.status = "completed";
2070
- this.emit("graph:complete", { plan: this.plan, progress });
2071
- } else if (progress.done > 0) {
2072
- this.plan.status = "partial";
2073
- this.emit("graph:complete", { plan: this.plan, progress });
2074
- } else {
2075
- this.plan.status = "failed";
2076
- const failedNodes = Object.entries(this.plan.results).filter(([, r]) => r.status === "failed").map(([id]) => id);
2077
- this.emit("graph:failed", { plan: this.plan, failedNodes });
2443
+ this.plan.updatedAt = Date.now();
2444
+ return this.plan;
2445
+ } finally {
2446
+ this.unlinkParent();
2078
2447
  }
2079
- this.plan.updatedAt = Date.now();
2080
- return this.plan;
2081
2448
  }
2082
2449
  /** Execute a single level — nodes run concurrently (limited by maxConcurrency) */
2083
2450
  async executeLevel(level, nodes) {
2084
- const runnableNodes = nodes.filter((node) => this.shouldRun(node));
2451
+ const runnableNodes = nodes.filter((node) => this.plan.results[node.id]?.status !== "done" && this.shouldRun(node));
2085
2452
  for (const node of nodes) {
2453
+ if (this.plan.results[node.id]?.status === "done") continue;
2086
2454
  if (!runnableNodes.includes(node)) {
2087
2455
  this.plan.results[node.id] = {
2088
2456
  nodeId: node.id,
@@ -2098,12 +2466,20 @@ var GraphExecutor = class {
2098
2466
  (node) => semaphore.acquire().then(async (release) => {
2099
2467
  try {
2100
2468
  await this.executeNode(node);
2469
+ await this.onNodeSettled?.(node.id);
2470
+ } catch (error) {
2471
+ if (this.onNodeSettled) this.abort();
2472
+ throw error;
2101
2473
  } finally {
2102
2474
  release();
2103
2475
  }
2104
2476
  })
2105
2477
  );
2106
- await Promise.allSettled(promises);
2478
+ const outcomes = await Promise.allSettled(promises);
2479
+ if (this.onNodeSettled) {
2480
+ const failure = outcomes.find((result) => result.status === "rejected");
2481
+ if (failure) throw failure.reason;
2482
+ }
2107
2483
  }
2108
2484
  /** Execute a single node with retry + fallback */
2109
2485
  async executeNode(node) {
@@ -2182,7 +2558,8 @@ var GraphExecutor = class {
2182
2558
  willRetry: true
2183
2559
  });
2184
2560
  const delay = Math.min(backoffMs * Math.pow(2, attempt), maxBackoffMs);
2185
- await new Promise((resolve) => setTimeout(resolve, delay));
2561
+ await abortableDelay(delay, this.abortController.signal).catch(() => {
2562
+ });
2186
2563
  }
2187
2564
  }
2188
2565
  if (node.fallback) {
@@ -2241,8 +2618,9 @@ var GraphExecutor = class {
2241
2618
  depth: this.baseDepth
2242
2619
  });
2243
2620
  if (node.timeout) {
2621
+ let timer;
2244
2622
  const timeoutPromise = new Promise((_, reject) => {
2245
- setTimeout(() => reject(new ACPError(
2623
+ timer = setTimeout(() => reject(new ACPError(
2246
2624
  ACPErrorCodes.SUBAGENT_TIMEOUT,
2247
2625
  `Node "${node.id}" timed out after ${node.timeout}ms`
2248
2626
  )), node.timeout);
@@ -2262,6 +2640,8 @@ var GraphExecutor = class {
2262
2640
  duration: 0,
2263
2641
  retryCount: 0
2264
2642
  };
2643
+ } finally {
2644
+ clearTimeout(timer);
2265
2645
  }
2266
2646
  }
2267
2647
  return subAgent.execute(node.goal, upstreamOutputs);
@@ -2462,7 +2842,7 @@ var HttpTransport = class {
2462
2842
  }
2463
2843
  return headers;
2464
2844
  }
2465
- async send(method, params) {
2845
+ async send(method, params, options) {
2466
2846
  const id = ++this.requestId;
2467
2847
  const body = {
2468
2848
  jsonrpc: "2.0",
@@ -2474,21 +2854,31 @@ var HttpTransport = class {
2474
2854
  const res = await fetch(`${this.baseUrl}/mcp`, {
2475
2855
  method: "POST",
2476
2856
  headers: this.buildHeaders({ Accept: "application/json" }),
2857
+ signal: options?.signal,
2477
2858
  body: JSON.stringify(body)
2478
2859
  });
2479
- const data = await res.json();
2860
+ let data;
2861
+ try {
2862
+ data = await res.json();
2863
+ } catch (error) {
2864
+ if (!res.ok) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, `HTTP ${res.status}`, { method, status: res.status });
2865
+ throw error;
2866
+ }
2480
2867
  const sid = res.headers.get("Mcp-Session-Id");
2481
2868
  if (sid) this.sessionId = sid;
2482
- if (data.error) {
2869
+ if (data?.error) {
2483
2870
  throw new ACPError(data.error.code, data.error.message, data.error.data);
2484
2871
  }
2872
+ if (!res.ok) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, `HTTP ${res.status}`, { method, status: res.status });
2873
+ if (!data || !Object.prototype.hasOwnProperty.call(data, "result")) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, "Invalid JSON-RPC response", { method });
2485
2874
  return data.result;
2486
2875
  } catch (err) {
2876
+ throwIfAborted(options?.signal);
2487
2877
  if (err instanceof ACPError) throw err;
2488
2878
  throw new ACPError(
2489
2879
  ACPErrorCodes.TRANSPORT_ERROR,
2490
- `Transport error: ${err.message}`,
2491
- { method, originalError: err.message }
2880
+ `Transport error: ${errorMessage(err)}`,
2881
+ { method, originalError: errorMessage(err) }
2492
2882
  );
2493
2883
  }
2494
2884
  }
@@ -2508,7 +2898,7 @@ var HttpTransport = class {
2508
2898
  /**
2509
2899
  * Stream a chat request via SSE.
2510
2900
  */
2511
- async *streamChat(method, params) {
2901
+ async *streamChat(method, params, options) {
2512
2902
  const id = ++this.requestId;
2513
2903
  const body = {
2514
2904
  jsonrpc: "2.0",
@@ -2519,33 +2909,48 @@ var HttpTransport = class {
2519
2909
  const res = await fetch(`${this.baseUrl}/mcp`, {
2520
2910
  method: "POST",
2521
2911
  headers: this.buildHeaders({ Accept: "text/event-stream" }),
2912
+ signal: options?.signal,
2522
2913
  body: JSON.stringify(body)
2523
2914
  });
2915
+ if (!res.ok) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, `HTTP ${res.status}`, { method, status: res.status });
2524
2916
  if (!res.body) {
2525
2917
  throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, "No response body for stream");
2526
2918
  }
2527
2919
  const reader = res.body.getReader();
2528
2920
  const decoder = new TextDecoder();
2529
2921
  let buffer = "";
2530
- while (true) {
2531
- const { done, value } = await reader.read();
2532
- if (done) break;
2533
- buffer += decoder.decode(value, { stream: true });
2534
- const lines = buffer.split("\n");
2535
- buffer = lines.pop() || "";
2536
- for (const line of lines) {
2537
- if (line.startsWith("data: ")) {
2538
- try {
2539
- const data = JSON.parse(line.slice(6));
2540
- if (data.result !== void 0) {
2541
- yield { id: data.id, result: data.result };
2542
- } else if (data.method) {
2543
- yield { method: data.method, params: data.params };
2922
+ try {
2923
+ while (true) {
2924
+ throwIfAborted(options?.signal);
2925
+ const { done, value } = await reader.read();
2926
+ buffer += done ? decoder.decode() : decoder.decode(value, { stream: true });
2927
+ if (done) {
2928
+ if (!buffer.trim()) break;
2929
+ buffer += "\n";
2930
+ }
2931
+ const lines = buffer.split("\n");
2932
+ buffer = lines.pop() || "";
2933
+ for (const line of lines) {
2934
+ if (line.trim().startsWith("data:")) {
2935
+ const payload = line.trim().slice(5).trim();
2936
+ if (payload === "[DONE]") continue;
2937
+ let data;
2938
+ try {
2939
+ data = JSON.parse(payload);
2940
+ } catch {
2941
+ throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, "Invalid JSON in transport stream");
2544
2942
  }
2545
- } catch {
2943
+ if (data.error) throw new ACPError(data.error.code, data.error.message, data.error.data);
2944
+ if (data.result !== void 0) yield { id: data.id, result: data.result };
2945
+ else if (data.method) yield { method: data.method, params: data.params };
2546
2946
  }
2547
2947
  }
2948
+ if (done) break;
2548
2949
  }
2950
+ } finally {
2951
+ await reader.cancel().catch(() => {
2952
+ });
2953
+ reader.releaseLock();
2549
2954
  }
2550
2955
  }
2551
2956
  };
@@ -2592,28 +2997,35 @@ var Ldoing = class {
2592
2997
  constructor(config) {
2593
2998
  this.initialized = false;
2594
2999
  this.startTime = 0;
3000
+ this.mcpStatus = "unchecked";
3001
+ this.discoveredServerSkills = /* @__PURE__ */ new WeakSet();
2595
3002
  this.currentSessionId = null;
2596
3003
  this.abortControllers = /* @__PURE__ */ new Map();
3004
+ this.runContexts = /* @__PURE__ */ new WeakMap();
2597
3005
  // Session-level concurrency control (方案 B: abort 旧请求 + 等待退出)
2598
3006
  // runningSessions 跟踪正在执行 agent loop 的会话,donePromise 在 agent loop 结束时 resolve
2599
3007
  this.runningSessions = /* @__PURE__ */ new Map();
2600
3008
  // Approval pending promises
2601
3009
  this.approvalResolvers = /* @__PURE__ */ new Map();
3010
+ this.sessionStorageQueues = /* @__PURE__ */ new Map();
3011
+ this.managingSessions = /* @__PURE__ */ new Map();
3012
+ this.compressingSessions = /* @__PURE__ */ new Map();
2602
3013
  // Event listeners
2603
3014
  this.eventHandlers = /* @__PURE__ */ new Map();
2604
3015
  // Token estimation calibration
2605
3016
  this.tokenCalibrationRatio = 1;
2606
- this.config = config;
3017
+ const proxy = resolveProxyConfig(config);
3018
+ this.config = config.llmProxyMode === void 0 ? config : { ...config, llm: { ...config.llm } };
2607
3019
  this.agentId = `agent-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
2608
- this.llmAdapter = new OpenAIAdapter(config.llm);
3020
+ this.llmAdapter = new OpenAIAdapter(proxy.llm, { rejectRedirects: config.llmProxyMode !== void 0 });
2609
3021
  this.skillRegistry = new SkillRegistry();
2610
- this.skillExecutor = new SkillExecutor(this.skillRegistry);
3022
+ this.skillExecutor = new SkillExecutor(this.skillRegistry, void 0, { validateInputs: config.skills?.validateInputs });
2611
3023
  this.mcpRouter = new McpRouter();
2612
3024
  this.sessionManager = new SessionManager();
2613
3025
  this.contextManager = new ContextManager(config.context, config.llm.model);
2614
3026
  this.contextManager.setSummarizer(createLLMSummarizer(this.llmAdapter));
2615
- if (config.serverUrl) {
2616
- this.transport = new HttpTransport(config.serverUrl, config.authToken, config.headers);
3027
+ if (proxy.serverUrl) {
3028
+ this.transport = new HttpTransport(proxy.serverUrl, config.authToken, config.headers);
2617
3029
  } else {
2618
3030
  this.transport = new LocalTransport();
2619
3031
  }
@@ -2634,10 +3046,13 @@ var Ldoing = class {
2634
3046
  }
2635
3047
  }
2636
3048
  let mode = "local";
2637
- let connected = false;
2638
3049
  if (this.config.serverUrl) {
3050
+ const transport = this.transport;
3051
+ const proxyMode = this.config.llmProxyMode;
3052
+ const { adapter, model } = this.config.llm;
3053
+ this.mcpStatus = "unchecked";
2639
3054
  try {
2640
- const result2 = await this.transport.send("initialize", {
3055
+ const result2 = await transport.send("initialize", {
2641
3056
  protocolVersion: "2025-03-26",
2642
3057
  capabilities: {
2643
3058
  acp: {
@@ -2645,47 +3060,47 @@ var Ldoing = class {
2645
3060
  sessions: true,
2646
3061
  contextManagement: true,
2647
3062
  dynamicSkillRegistration: true,
2648
- llmConfig: this.config.llm
3063
+ llmConfig: proxyMode === void 0 ? this.config.llm : { adapter, model }
2649
3064
  }
2650
3065
  },
2651
3066
  clientInfo: { name: "listdoing-sdk", version: "0.1.0" }
2652
3067
  });
2653
- connected = true;
2654
- const serverSkills = result2?.capabilities?.acp?.serverSkills ?? [];
2655
- for (const name of serverSkills) {
2656
- if (!this.skillRegistry.has(name)) {
2657
- this.skillRegistry.register({
2658
- name,
2659
- description: `Server skill: ${name}`,
2660
- inputSchema: { type: "object", properties: {} },
2661
- location: "server"
2662
- });
3068
+ if (proxyMode === void 0 || this.transport === transport) {
3069
+ const serverSkills = result2?.capabilities?.acp?.serverSkills ?? [];
3070
+ for (const name of serverSkills) {
3071
+ if (!this.skillRegistry.has(name)) {
3072
+ const skill = {
3073
+ name,
3074
+ description: `Server skill: ${name}`,
3075
+ inputSchema: { type: "object", properties: {} },
3076
+ location: "server"
3077
+ };
3078
+ this.skillRegistry.register(skill);
3079
+ this.discoveredServerSkills.add(skill);
3080
+ }
2663
3081
  }
3082
+ if (this.transport === transport) this.mcpStatus = "connected";
3083
+ mode = this.skillRegistry.list().some((s) => s.location === "local") ? "hybrid" : "remote";
2664
3084
  }
2665
- mode = this.skillRegistry.list().some((s) => s.location === "local") ? "hybrid" : "remote";
2666
3085
  } catch {
2667
- mode = "local";
3086
+ if (this.transport === transport) this.mcpStatus = "failed";
2668
3087
  }
2669
3088
  }
2670
- let llmConnected = false;
2671
- try {
2672
- llmConnected = true;
2673
- } catch {
2674
- llmConnected = false;
2675
- }
2676
3089
  this.initialized = true;
2677
3090
  const result = {
2678
3091
  agentId: this.agentId,
2679
3092
  mode,
3093
+ ...this.getConnectionDetails(),
2680
3094
  llm: {
2681
- connected: llmConnected,
3095
+ connected: true,
3096
+ // Legacy status. Use llmRoute.verified for a successful request.
2682
3097
  provider: this.config.llm.adapter,
2683
3098
  model: this.config.llm.model
2684
3099
  },
2685
3100
  capabilities: {
2686
3101
  localSkills: this.skillRegistry.list().filter((s) => s.location !== "server").map((s) => s.name),
2687
3102
  serverSkills: this.skillRegistry.list().filter((s) => s.location === "server").map((s) => s.name),
2688
- llmProxy: mode !== "local"
3103
+ llmProxy: this.config.llmProxyMode !== void 0 || mode !== "local"
2689
3104
  }
2690
3105
  };
2691
3106
  this.emit("init", result);
@@ -2696,7 +3111,10 @@ var Ldoing = class {
2696
3111
  controller.abort();
2697
3112
  }
2698
3113
  this.abortControllers.clear();
3114
+ for (const pending of this.approvalResolvers.values()) pending.reject(abortError());
3115
+ this.approvalResolvers.clear();
2699
3116
  this.initialized = false;
3117
+ this.mcpStatus = "unchecked";
2700
3118
  this.emit("destroy", {});
2701
3119
  }
2702
3120
  getStatus() {
@@ -2706,6 +3124,7 @@ var Ldoing = class {
2706
3124
  uptime: Date.now() - this.startTime,
2707
3125
  activeSessions: this.sessionManager.list({ status: "active" }).length,
2708
3126
  loadedSkills: this.skillRegistry.list().length,
3127
+ ...this.getConnectionDetails(),
2709
3128
  llm: {
2710
3129
  adapter: this.config.llm.adapter,
2711
3130
  model: this.config.llm.model,
@@ -2713,15 +3132,26 @@ var Ldoing = class {
2713
3132
  }
2714
3133
  };
2715
3134
  }
3135
+ getConnectionDetails() {
3136
+ const mcpStatus = this.config.serverUrl ? this.mcpStatus : "not-configured";
3137
+ try {
3138
+ const status = this.llmAdapter.getConnectionStatus?.();
3139
+ if (status) return { mcpStatus, llmRoute: { mode: this.config.llmProxyMode ?? "legacy", ...status } };
3140
+ } catch {
3141
+ }
3142
+ return { mcpStatus };
3143
+ }
2716
3144
  // ---- Chat ----
2717
3145
  async chat(message, options) {
2718
3146
  this.ensureInitialized();
2719
3147
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
2720
- this.currentSessionId = sessionId;
3148
+ if (!options?._skipLock || !this.abortControllers.has(sessionId)) this.ensureSessionIsNotBeingManaged(sessionId);
3149
+ if (!options?._skipLock) this.currentSessionId = sessionId;
2721
3150
  const releaseLock = options?._skipLock ? null : await this.acquireSessionLock(sessionId);
2722
- const controller = new AbortController();
2723
- this.abortControllers.set(sessionId, controller);
3151
+ const execution = this.beginExecution(sessionId, options);
3152
+ const controller = execution.controller;
2724
3153
  try {
3154
+ const runContext = this.prepareRunContext(controller.signal, options?.runContext);
2725
3155
  if (options?.taskMode === "plan") {
2726
3156
  return await this.chatWithPlan(message, options);
2727
3157
  }
@@ -2738,12 +3168,14 @@ var Ldoing = class {
2738
3168
  this.sessionManager.addMessage(sessionId, { role: "user", content: userContent });
2739
3169
  let turn = 0;
2740
3170
  let continueLoop = true;
3171
+ let completedReply;
2741
3172
  let retried = false;
2742
3173
  while (continueLoop && turn < maxTurns) {
3174
+ throwIfAborted(controller.signal);
2743
3175
  turn++;
2744
3176
  const session2 = this.sessionManager.getOrThrow(sessionId);
2745
- const systemPrompt = this.resolveSystemPrompt(sessionId, session2.systemPrompt ?? this.config.systemPrompt ?? "");
2746
- const allTools = this.skillRegistry.listToolDefs();
3177
+ const systemPrompt = this.buildRunSystemPrompt(session2, runContext);
3178
+ const allTools = this.listRunTools(runContext, options?._skipLock);
2747
3179
  const tools = this.resolveTools(allTools, { allowedTools: options?.tools, toolsByPermission: options?.toolsByPermission });
2748
3180
  for (const name of this.computeAutoApproval(tools)) {
2749
3181
  requireApproval.add(name);
@@ -2764,24 +3196,27 @@ var Ldoing = class {
2764
3196
  afterTokens: ctxCheck.afterTokens
2765
3197
  });
2766
3198
  }
2767
- const contextMessages = this.contextManager.buildWindow(
3199
+ throwIfAborted(controller.signal);
3200
+ const window2 = this.contextManager.buildWindow(
2768
3201
  session2.messages,
2769
3202
  systemPrompt,
2770
3203
  tools
2771
- ).messages;
3204
+ );
3205
+ let contextMessages = window2.messages;
2772
3206
  let llmResponse;
2773
3207
  try {
2774
- llmResponse = await this.llmAdapter.chat(contextMessages, tools, {
2775
- signal: controller.signal
2776
- });
2777
- } catch (err) {
2778
- if (err.code === ACPErrorCodes.CONTEXT_OVERFLOW && !retried) {
2779
- await this.compressContext(sessionId, "llm_summary");
3208
+ try {
3209
+ llmResponse = await this.llmAdapter.chat(contextMessages, tools, { signal: controller.signal });
3210
+ } catch (err) {
3211
+ if (err?.code !== ACPErrorCodes.CONTEXT_OVERFLOW || retried) throw err;
3212
+ const smaller = await this.prepareOverflowRetry(session2, window2, systemPrompt, tools, controller.signal);
3213
+ if (!smaller) throw err;
2780
3214
  retried = true;
2781
- turn--;
2782
- continue;
3215
+ contextMessages = smaller;
3216
+ llmResponse = await this.llmAdapter.chat(contextMessages, tools, { signal: controller.signal });
2783
3217
  }
2784
- if (err.code === ACPErrorCodes.LLM_EMPTY_RESPONSE) {
3218
+ } catch (err) {
3219
+ if (err?.code === ACPErrorCodes.LLM_EMPTY_RESPONSE) {
2785
3220
  if (!retried) {
2786
3221
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
2787
3222
  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" });
@@ -2790,10 +3225,12 @@ var Ldoing = class {
2790
3225
  continue;
2791
3226
  }
2792
3227
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
3228
+ completedReply = ".";
2793
3229
  continueLoop = false;
2794
3230
  break;
2795
3231
  }
2796
- throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, `LLM call failed: ${err.message}`, { turn });
3232
+ if (controller.signal.aborted || err?.name === "AbortError") throw abortError();
3233
+ throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, `LLM call failed: ${errorMessage(err)}`, { turn });
2797
3234
  }
2798
3235
  if (!llmResponse.content && !llmResponse.toolCalls?.length) {
2799
3236
  if (!retried) {
@@ -2804,9 +3241,11 @@ var Ldoing = class {
2804
3241
  continue;
2805
3242
  }
2806
3243
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
3244
+ completedReply = ".";
2807
3245
  continueLoop = false;
2808
3246
  break;
2809
3247
  }
3248
+ this.reportPromptCacheUsage(sessionId, llmResponse.promptCacheUsage);
2810
3249
  if (llmResponse.usage) {
2811
3250
  totalInputTokens += llmResponse.usage.input;
2812
3251
  totalOutputTokens += llmResponse.usage.output;
@@ -2817,6 +3256,7 @@ var Ldoing = class {
2817
3256
  }
2818
3257
  }
2819
3258
  if (!llmResponse.toolCalls?.length) {
3259
+ completedReply = llmResponse.content;
2820
3260
  this.sessionManager.addMessage(sessionId, {
2821
3261
  role: "assistant",
2822
3262
  content: llmResponse.content
@@ -2830,8 +3270,9 @@ var Ldoing = class {
2830
3270
  toolCalls: llmResponse.toolCalls
2831
3271
  });
2832
3272
  const turnTrace = { turn, toolCalls: [] };
2833
- const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval);
2834
- await this.processApprovalQueue(plan.items, sessionId, (event, data) => this.emit(event, data));
3273
+ const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval, runContext, tools);
3274
+ for await (const _event of this.processApprovalQueue(plan.items, sessionId, controller.signal)) {
3275
+ }
2835
3276
  const batchTraces = await this.executeToolBatch(plan.items, sessionId, controller, plan.canParallelize, (event, data) => this.emit(event, data));
2836
3277
  turnTrace.toolCalls = batchTraces;
2837
3278
  const executableCount = plan.items.filter((i) => !i.resolved).length;
@@ -2839,26 +3280,27 @@ var Ldoing = class {
2839
3280
  executionTrace.push(turnTrace);
2840
3281
  }
2841
3282
  const session = this.sessionManager.getOrThrow(sessionId);
2842
- const lastAssistant = [...session.messages].reverse().find((m) => m.role === "assistant" && !m.toolCalls?.length);
2843
- const reply = typeof lastAssistant?.content === "string" ? lastAssistant.content : lastAssistant ? contentToString(lastAssistant.content) : "";
2844
- const chatResult = { reply, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3283
+ const reply = completedReply ?? "";
3284
+ const terminationReason = completedReply !== void 0 ? "stop" : "max_turns";
3285
+ const chatResult = { reply, terminationReason, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
2845
3286
  this.emit("chat:complete", { sessionId, reply, tokenUsage: chatResult.tokenUsage });
2846
- await this.persistence.saveSession(session).catch(() => {
2847
- });
3287
+ await this.saveSessionSafely(session);
2848
3288
  return chatResult;
2849
3289
  } finally {
2850
- this.abortControllers.delete(sessionId);
3290
+ execution.cleanup();
2851
3291
  if (releaseLock) releaseLock();
2852
3292
  }
2853
3293
  }
2854
3294
  async *chatStream(message, options) {
2855
3295
  this.ensureInitialized();
2856
3296
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
2857
- this.currentSessionId = sessionId;
3297
+ if (!options?._skipLock || !this.abortControllers.has(sessionId)) this.ensureSessionIsNotBeingManaged(sessionId);
3298
+ if (!options?._skipLock) this.currentSessionId = sessionId;
2858
3299
  const releaseLock = options?._skipLock ? null : await this.acquireSessionLock(sessionId);
2859
- const controller = new AbortController();
2860
- this.abortControllers.set(sessionId, controller);
3300
+ const execution = this.beginExecution(sessionId, options);
3301
+ const controller = execution.controller;
2861
3302
  try {
3303
+ const runContext = this.prepareRunContext(controller.signal, options?.runContext);
2862
3304
  if (options?.taskMode === "plan") {
2863
3305
  yield* this.chatStreamWithPlan(message, options);
2864
3306
  return;
@@ -2877,11 +3319,13 @@ var Ldoing = class {
2877
3319
  this.sessionManager.addMessage(sessionId, { role: "user", content: userContent });
2878
3320
  let turn = 0;
2879
3321
  let continueLoop = true;
3322
+ let completedReply;
2880
3323
  while (continueLoop && turn < maxTurns) {
3324
+ throwIfAborted(controller.signal);
2881
3325
  turn++;
2882
3326
  const session2 = this.sessionManager.getOrThrow(sessionId);
2883
- const systemPrompt = this.resolveSystemPrompt(sessionId, session2.systemPrompt ?? this.config.systemPrompt ?? "");
2884
- const allTools = this.skillRegistry.listToolDefs();
3327
+ const systemPrompt = this.buildRunSystemPrompt(session2, runContext);
3328
+ const allTools = this.listRunTools(runContext, options?._skipLock);
2885
3329
  const tools = this.resolveTools(allTools, { allowedTools: options?.tools, toolsByPermission: options?.toolsByPermission });
2886
3330
  for (const name of this.computeAutoApproval(tools)) {
2887
3331
  requireApproval.add(name);
@@ -2909,36 +3353,33 @@ var Ldoing = class {
2909
3353
  reason: "auto"
2910
3354
  };
2911
3355
  }
2912
- const contextMessages = this.contextManager.buildWindow(session2.messages, systemPrompt, tools).messages;
3356
+ throwIfAborted(controller.signal);
3357
+ const window2 = this.contextManager.buildWindow(session2.messages, systemPrompt, tools);
3358
+ let contextMessages = window2.messages;
2913
3359
  let llmResponse;
2914
3360
  try {
2915
- const useStreaming = typeof this.llmAdapter.chatStream === "function";
2916
- if (useStreaming) {
2917
- llmResponse = yield* this.streamLlmResponse(contextMessages, tools, controller.signal);
2918
- } else {
2919
- llmResponse = await this.llmAdapter.chat(contextMessages, tools, { signal: controller.signal });
2920
- if (llmResponse.reasoning) {
2921
- yield { type: "llm_thinking", content: llmResponse.reasoning };
2922
- }
2923
- if (llmResponse.content) {
2924
- yield { type: "llm_text", content: llmResponse.content };
3361
+ for (; ; ) {
3362
+ try {
3363
+ llmResponse = yield* this.streamLlmResponse(contextMessages, tools, controller.signal);
3364
+ break;
3365
+ } catch (err) {
3366
+ if (err?.code !== ACPErrorCodes.CONTEXT_OVERFLOW || retried) throw err;
3367
+ const smaller = await this.prepareOverflowRetry(session2, window2, systemPrompt, tools, controller.signal);
3368
+ if (!smaller) throw err;
3369
+ retried = true;
3370
+ contextMessages = smaller;
2925
3371
  }
2926
3372
  }
2927
3373
  } catch (err) {
2928
- if (err.code === ACPErrorCodes.CONTEXT_OVERFLOW && !retried) {
2929
- await this.compressContext(sessionId, "llm_summary");
2930
- retried = true;
2931
- turn--;
2932
- continue;
2933
- }
2934
- if (err.code === ACPErrorCodes.LLM_EMPTY_RESPONSE && !retried) {
3374
+ if (err?.code === ACPErrorCodes.LLM_EMPTY_RESPONSE && !retried) {
2935
3375
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
2936
3376
  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" });
2937
3377
  retried = true;
2938
3378
  turn--;
2939
3379
  continue;
2940
3380
  }
2941
- yield { type: "error", code: ACPErrorCodes.LLM_CALL_FAILED, message: err.message };
3381
+ if (controller.signal.aborted || err?.name === "AbortError") throw abortError();
3382
+ yield { type: "error", code: ACPErrorCodes.LLM_CALL_FAILED, message: errorMessage(err) };
2942
3383
  return;
2943
3384
  }
2944
3385
  if (!llmResponse.content && !llmResponse.toolCalls?.length) {
@@ -2950,8 +3391,10 @@ var Ldoing = class {
2950
3391
  continue;
2951
3392
  }
2952
3393
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
3394
+ completedReply = ".";
2953
3395
  break;
2954
3396
  }
3397
+ this.reportPromptCacheUsage(sessionId, llmResponse.promptCacheUsage);
2955
3398
  if (llmResponse.usage) {
2956
3399
  totalInputTokens += llmResponse.usage.input;
2957
3400
  totalOutputTokens += llmResponse.usage.output;
@@ -2962,27 +3405,21 @@ var Ldoing = class {
2962
3405
  }
2963
3406
  }
2964
3407
  if (!llmResponse.toolCalls?.length) {
3408
+ completedReply = llmResponse.content;
2965
3409
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: llmResponse.content });
2966
3410
  break;
2967
3411
  }
2968
3412
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: llmResponse.content, toolCalls: llmResponse.toolCalls });
2969
- const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval);
2970
- const approvalEvents = [];
2971
- const approvalYield = (evt) => approvalEvents.push(evt);
2972
- await this.processApprovalQueue(plan.items, sessionId, (event, data) => this.emit(event, data), approvalYield);
2973
- for (const evt of approvalEvents) yield evt;
2974
- const batchEvents = [];
2975
- const batchYield = (evt) => batchEvents.push(evt);
2976
- await this.executeToolBatchStream(plan.items, sessionId, controller, plan.canParallelize, batchYield);
2977
- for (const evt of batchEvents) yield evt;
3413
+ const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval, runContext, tools);
3414
+ yield* this.processApprovalQueue(plan.items, sessionId, controller.signal);
3415
+ yield* this.streamToolBatch(plan.items, sessionId, controller, plan.canParallelize);
2978
3416
  }
2979
3417
  const session = this.sessionManager.getOrThrow(sessionId);
2980
- const lastAssistant = [...session.messages].reverse().find((m) => m.role === "assistant" && !m.toolCalls?.length);
2981
- yield { type: "done", reply: typeof lastAssistant?.content === "string" ? lastAssistant.content : lastAssistant ? contentToString(lastAssistant.content) : "", tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
2982
- await this.persistence.saveSession(session).catch(() => {
2983
- });
3418
+ const saving = this.saveSessionSafely(session);
3419
+ yield { type: "done", terminationReason: completedReply !== void 0 ? "stop" : "max_turns", reply: completedReply ?? "", tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3420
+ await saving;
2984
3421
  } finally {
2985
- this.abortControllers.delete(sessionId);
3422
+ execution.cleanup();
2986
3423
  if (releaseLock) releaseLock();
2987
3424
  }
2988
3425
  }
@@ -2992,7 +3429,6 @@ var Ldoing = class {
2992
3429
  const controller = this.abortControllers.get(sid);
2993
3430
  if (controller) {
2994
3431
  controller.abort();
2995
- this.abortControllers.delete(sid);
2996
3432
  }
2997
3433
  }
2998
3434
  }
@@ -3000,7 +3436,7 @@ var Ldoing = class {
3000
3436
  /**
3001
3437
  * 在启动新的 agent loop 前获取会话锁。
3002
3438
  * 根据并发策略处理正在运行的旧请求:
3003
- * - 'abort' (默认): 中止旧请求,等待其真正退出,清理半截消息,然后返回
3439
+ * - 'abort' (默认): 中止旧请求,等待其真正退出,发送前修复未配对消息,然后返回
3004
3440
  * - 'reject': 若已有运行请求,抛错
3005
3441
  * - 'queue': 预留,当前回退到 'abort'
3006
3442
  *
@@ -3008,48 +3444,27 @@ var Ldoing = class {
3008
3444
  */
3009
3445
  async acquireSessionLock(sessionId) {
3010
3446
  const policy = this.config.concurrentPolicy ?? "abort";
3011
- const running = this.runningSessions.get(sessionId);
3012
- if (running) {
3013
- if (policy === "reject") {
3014
- throw new ACPError(
3015
- ACPErrorCodes.SESSION_BUSY,
3016
- `Session ${sessionId} is already running a chat`
3017
- );
3018
- }
3447
+ let running = this.runningSessions.get(sessionId);
3448
+ while (running) {
3449
+ this.ensureSessionIsNotBeingManaged(sessionId);
3450
+ if (policy === "reject") throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${sessionId} is already running a chat`);
3019
3451
  await this.abort(sessionId);
3020
3452
  await running.donePromise;
3021
- this.cleanupInterruptedMessages(sessionId);
3453
+ running = this.runningSessions.get(sessionId);
3022
3454
  }
3455
+ this.ensureInitialized();
3456
+ this.ensureSessionIsNotBeingManaged(sessionId);
3023
3457
  let resolveDone;
3024
3458
  const donePromise = new Promise((resolve) => {
3025
3459
  resolveDone = resolve;
3026
3460
  });
3027
- this.runningSessions.set(sessionId, { donePromise, resolveDone });
3461
+ const owner = { donePromise, resolveDone };
3462
+ this.runningSessions.set(sessionId, owner);
3028
3463
  return () => {
3029
- this.runningSessions.delete(sessionId);
3464
+ if (this.runningSessions.get(sessionId) === owner) this.runningSessions.delete(sessionId);
3030
3465
  resolveDone();
3031
3466
  };
3032
3467
  }
3033
- /**
3034
- * 清理被 abort 打断的半截消息:
3035
- * 如果 session.messages 最后一条是 assistant 且带 tool_calls,
3036
- * 但后面没有对应的 tool 消息,则删除这条 assistant。
3037
- * 避免下次 LLM 调用因 tool_calls 未配对而报错。
3038
- */
3039
- cleanupInterruptedMessages(sessionId) {
3040
- try {
3041
- const session = this.sessionManager.get(sessionId);
3042
- if (!session?.messages?.length) return;
3043
- const msgs = session.messages;
3044
- const last = msgs[msgs.length - 1];
3045
- if (last.role === "assistant" && last.toolCalls?.length) {
3046
- msgs.pop();
3047
- console.log(`[concurrency] cleaned up interrupted assistant(tool_calls) from session ${sessionId}`);
3048
- }
3049
- } catch (err) {
3050
- console.warn("[concurrency] failed to cleanup interrupted messages:", err);
3051
- }
3052
- }
3053
3468
  // ---- Tool Approval ----
3054
3469
  approveTool(callId, approved, _reason) {
3055
3470
  const resolver = this.approvalResolvers.get(callId);
@@ -3058,15 +3473,28 @@ var Ldoing = class {
3058
3473
  this.approvalResolvers.delete(callId);
3059
3474
  }
3060
3475
  }
3061
- waitForApproval(callId) {
3476
+ waitForApproval(callId, signal) {
3477
+ throwIfAborted(signal);
3062
3478
  return new Promise((resolve, reject) => {
3063
- this.approvalResolvers.set(callId, { resolve, reject });
3064
- setTimeout(() => {
3065
- if (this.approvalResolvers.has(callId)) {
3066
- this.approvalResolvers.delete(callId);
3067
- resolve(false);
3479
+ const cleanup = () => {
3480
+ clearTimeout(timer);
3481
+ signal.removeEventListener("abort", abort);
3482
+ if (this.approvalResolvers.get(callId) === pending) this.approvalResolvers.delete(callId);
3483
+ };
3484
+ const pending = {
3485
+ resolve: (approved) => {
3486
+ cleanup();
3487
+ resolve(approved);
3488
+ },
3489
+ reject: (error) => {
3490
+ cleanup();
3491
+ reject(error);
3068
3492
  }
3069
- }, 6e4);
3493
+ };
3494
+ const abort = () => pending.reject(abortError());
3495
+ const timer = setTimeout(() => pending.resolve(false), 6e4);
3496
+ this.approvalResolvers.set(callId, pending);
3497
+ signal.addEventListener("abort", abort, { once: true });
3070
3498
  });
3071
3499
  }
3072
3500
  // ---- Skill Management ----
@@ -3132,31 +3560,31 @@ ${JSON.stringify(params, null, 2)}`;
3132
3560
  } else {
3133
3561
  prompt = loop.instruction;
3134
3562
  }
3135
- const result = await ctx.agent.chatInner(prompt, ctx.sessionId, { maxTurns: loop.maxTurns ?? 10, tools: loop.tools });
3563
+ const result = await ctx.agent.chatInner(prompt, ctx.sessionId, { maxTurns: loop.maxTurns ?? 10, tools: loop.tools, signal: ctx.signal });
3136
3564
  return { content: [{ type: "text", text: result.reply }] };
3137
3565
  };
3138
3566
  }
3139
3567
  /** Internal chat for agentLoop/workflow/sub-agent execution. Condenses inner messages after completion. */
3140
3568
  async chatInner(prompt, sessionId, options) {
3141
3569
  const session = this.sessionManager.getOrThrow(sessionId);
3142
- const messageCountBefore = session.messages.length;
3143
- const result = await this.chat(prompt, { ...options, _skipLock: true });
3144
- const sessionAfter = this.sessionManager.getOrThrow(sessionId);
3145
- if (sessionAfter.messages.length > messageCountBefore + 2) {
3146
- const innerMessages = sessionAfter.messages.splice(messageCountBefore);
3147
- const lastAssistant = [...innerMessages].reverse().find((m) => m.role === "assistant" && !m.toolCalls?.length);
3148
- if (lastAssistant) {
3149
- sessionAfter.messages.push({ role: "assistant", content: `[Agent loop completed]: ${contentToPreview(lastAssistant.content, 200)}` });
3150
- }
3570
+ const prefix = session.messages.slice();
3571
+ const isToolHandler = prefix[prefix.length - 1]?.role === "assistant" && !!prefix[prefix.length - 1]?.toolCalls?.length;
3572
+ try {
3573
+ const result = await this.chat(prompt, { ...options, sessionId, _skipLock: true });
3574
+ session.messages = prefix;
3575
+ if (!isToolHandler) this.sessionManager.addMessage(sessionId, { role: "assistant", content: `[Agent loop completed]: ${result.reply.slice(0, 200)}` });
3576
+ return result;
3577
+ } finally {
3578
+ if (isToolHandler) session.messages = prefix;
3579
+ session.messageCount = session.messages.length;
3151
3580
  }
3152
- return result;
3153
3581
  }
3154
3582
  // ---- Task Plan Execution ----
3155
3583
  async chatWithPlan(message, options) {
3156
3584
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
3157
3585
  this.currentSessionId = sessionId;
3158
- const controller = new AbortController();
3159
- this.abortControllers.set(sessionId, controller);
3586
+ const execution = this.beginExecution(sessionId, options);
3587
+ const controller = execution.controller;
3160
3588
  const startTime = Date.now();
3161
3589
  let totalInputTokens = 0;
3162
3590
  let totalOutputTokens = 0;
@@ -3170,8 +3598,9 @@ ${JSON.stringify(params, null, 2)}`;
3170
3598
  let plan;
3171
3599
  try {
3172
3600
  const planner = new TaskPlanner(this.llmAdapter);
3173
- plan = await planner.plan(messageText, filteredTools);
3601
+ plan = await planner.plan(messageText, filteredTools, controller.signal);
3174
3602
  } catch (err) {
3603
+ throwIfAborted(controller.signal);
3175
3604
  throw new ACPError(ACPErrorCodes.TASK_PLAN_FAILED, `Task planning failed: ${err.message}`);
3176
3605
  }
3177
3606
  const tracker = new PlanTracker(plan, sessionId, (event, data) => this.emit(event, data));
@@ -3182,13 +3611,14 @@ ${JSON.stringify(params, null, 2)}`;
3182
3611
  const maxRetry = options?.stepRetryCount ?? 1;
3183
3612
  const continueOnFailure = options?.continueOnStepFailure ?? true;
3184
3613
  for (let i = 0; i < plan.steps.length; i++) {
3185
- if (controller.signal.aborted) break;
3614
+ throwIfAborted(controller.signal);
3186
3615
  const step = plan.steps[i];
3187
3616
  if (step.status === "done" || step.status === "skipped") continue;
3188
3617
  tracker.startStep(i);
3189
3618
  let stepResult;
3190
3619
  let succeeded = false;
3191
3620
  for (let attempt = 0; attempt <= maxRetry; attempt++) {
3621
+ throwIfAborted(controller.signal);
3192
3622
  try {
3193
3623
  const result = await this.chatInner(step.goal, sessionId, { maxTurns: options?.maxTurns ?? 10, tools: step.tools });
3194
3624
  stepResult = { output: result.reply, toolCalls: result.executionTrace.reduce((sum, t) => sum + t.toolCalls.length, 0), tokenUsage: result.tokenUsage, duration: result.totalDuration };
@@ -3198,6 +3628,7 @@ ${JSON.stringify(params, null, 2)}`;
3198
3628
  succeeded = true;
3199
3629
  break;
3200
3630
  } catch (err) {
3631
+ throwIfAborted(controller.signal);
3201
3632
  stepResult = { output: "", toolCalls: 0, tokenUsage: { input: 0, output: 0 }, duration: 0, error: err.message };
3202
3633
  plan.steps[i].retryCount = attempt + 1;
3203
3634
  }
@@ -3225,18 +3656,17 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3225
3656
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: finalReply });
3226
3657
  const chatResult = { reply: finalReply, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3227
3658
  this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
3228
- await this.persistence.saveSession(session).catch(() => {
3229
- });
3659
+ await this.saveSessionSafely(session);
3230
3660
  return chatResult;
3231
3661
  } finally {
3232
- this.abortControllers.delete(sessionId);
3662
+ execution.cleanup();
3233
3663
  }
3234
3664
  }
3235
3665
  async *chatStreamWithPlan(message, options) {
3236
3666
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
3237
3667
  this.currentSessionId = sessionId;
3238
- const controller = new AbortController();
3239
- this.abortControllers.set(sessionId, controller);
3668
+ const execution = this.beginExecution(sessionId, options);
3669
+ const controller = execution.controller;
3240
3670
  const startTime = Date.now();
3241
3671
  let totalInputTokens = 0;
3242
3672
  let totalOutputTokens = 0;
@@ -3249,7 +3679,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3249
3679
  let plan;
3250
3680
  try {
3251
3681
  const planner = new TaskPlanner(this.llmAdapter);
3252
- plan = await planner.plan(messageText, filteredTools);
3682
+ plan = await planner.plan(messageText, filteredTools, controller.signal);
3253
3683
  } catch (err) {
3254
3684
  yield { type: "error", code: ACPErrorCodes.TASK_PLAN_FAILED, message: `Task planning failed: ${err.message}` };
3255
3685
  return;
@@ -3263,7 +3693,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3263
3693
  const maxRetry = options?.stepRetryCount ?? 1;
3264
3694
  const continueOnFailure = options?.continueOnStepFailure ?? true;
3265
3695
  for (let i = 0; i < plan.steps.length; i++) {
3266
- if (controller.signal.aborted) break;
3696
+ throwIfAborted(controller.signal);
3267
3697
  const step = plan.steps[i];
3268
3698
  if (step.status === "done" || step.status === "skipped") continue;
3269
3699
  tracker.startStep(i);
@@ -3271,6 +3701,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3271
3701
  let stepResult;
3272
3702
  let succeeded = false;
3273
3703
  for (let attempt = 0; attempt <= maxRetry; attempt++) {
3704
+ throwIfAborted(controller.signal);
3274
3705
  try {
3275
3706
  const result = await this.chatInner(step.goal, sessionId, { maxTurns: options?.maxTurns ?? 10, tools: step.tools });
3276
3707
  stepResult = { output: result.reply, toolCalls: result.executionTrace.reduce((sum, t) => sum + t.toolCalls.length, 0), tokenUsage: result.tokenUsage, duration: result.totalDuration };
@@ -3279,6 +3710,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3279
3710
  succeeded = true;
3280
3711
  break;
3281
3712
  } catch (err) {
3713
+ throwIfAborted(controller.signal);
3282
3714
  stepResult = { output: "", toolCalls: 0, tokenUsage: { input: 0, output: 0 }, duration: 0, error: err.message };
3283
3715
  plan.steps[i].retryCount = attempt + 1;
3284
3716
  }
@@ -3306,11 +3738,11 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3306
3738
 
3307
3739
  ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3308
3740
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: finalReply });
3741
+ const saving = this.saveSessionSafely(session);
3309
3742
  yield { type: "done", reply: finalReply, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3310
- await this.persistence.saveSession(session).catch(() => {
3311
- });
3743
+ await saving;
3312
3744
  } finally {
3313
- this.abortControllers.delete(sessionId);
3745
+ execution.cleanup();
3314
3746
  }
3315
3747
  }
3316
3748
  async resumeTask(sessionId, options) {
@@ -3320,6 +3752,8 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3320
3752
  if (!savedPlan) {
3321
3753
  throw new ACPError(ACPErrorCodes.TASK_PLAN_FAILED, "No task plan found in session");
3322
3754
  }
3755
+ const releaseLock = await this.acquireSessionLock(sessionId);
3756
+ this.currentSessionId = sessionId;
3323
3757
  for (const step of savedPlan.steps) {
3324
3758
  if (step.status === "failed") {
3325
3759
  step.status = "pending";
@@ -3327,8 +3761,8 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3327
3761
  step.result = void 0;
3328
3762
  }
3329
3763
  }
3330
- const controller = new AbortController();
3331
- this.abortControllers.set(sessionId, controller);
3764
+ const execution = this.beginExecution(sessionId, options);
3765
+ const controller = execution.controller;
3332
3766
  const startTime = Date.now();
3333
3767
  let totalInputTokens = 0;
3334
3768
  let totalOutputTokens = 0;
@@ -3339,13 +3773,14 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3339
3773
  const maxRetry = options?.stepRetryCount ?? 1;
3340
3774
  const continueOnFailure = options?.continueOnStepFailure ?? true;
3341
3775
  for (let i = 0; i < savedPlan.steps.length; i++) {
3342
- if (controller.signal.aborted) break;
3776
+ throwIfAborted(controller.signal);
3343
3777
  const step = savedPlan.steps[i];
3344
3778
  if (step.status === "done" || step.status === "skipped") continue;
3345
3779
  tracker.startStep(i);
3346
3780
  let stepResult;
3347
3781
  let succeeded = false;
3348
3782
  for (let attempt = 0; attempt <= maxRetry; attempt++) {
3783
+ throwIfAborted(controller.signal);
3349
3784
  try {
3350
3785
  const result = await this.chatInner(step.goal, sessionId, { maxTurns: options?.maxTurns ?? 10, tools: step.tools });
3351
3786
  stepResult = { output: result.reply, toolCalls: result.executionTrace.reduce((sum, t) => sum + t.toolCalls.length, 0), tokenUsage: result.tokenUsage, duration: result.totalDuration };
@@ -3355,6 +3790,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3355
3790
  succeeded = true;
3356
3791
  break;
3357
3792
  } catch (err) {
3793
+ throwIfAborted(controller.signal);
3358
3794
  stepResult = { output: "", toolCalls: 0, tokenUsage: { input: 0, output: 0 }, duration: 0, error: err.message };
3359
3795
  savedPlan.steps[i].retryCount = attempt + 1;
3360
3796
  }
@@ -3382,11 +3818,11 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
3382
3818
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: finalReply });
3383
3819
  const chatResult = { reply: finalReply, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3384
3820
  this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
3385
- await this.persistence.saveSession(session).catch(() => {
3386
- });
3821
+ await this.saveSessionSafely(session);
3387
3822
  return chatResult;
3388
3823
  } finally {
3389
- this.abortControllers.delete(sessionId);
3824
+ execution.cleanup();
3825
+ releaseLock();
3390
3826
  }
3391
3827
  }
3392
3828
  getTaskPlan(sessionId) {
@@ -3402,8 +3838,8 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
3402
3838
  async chatWithGraph(message, options) {
3403
3839
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
3404
3840
  this.currentSessionId = sessionId;
3405
- const controller = new AbortController();
3406
- this.abortControllers.set(sessionId, controller);
3841
+ const execution = this.beginExecution(sessionId, options);
3842
+ const controller = execution.controller;
3407
3843
  const startTime = Date.now();
3408
3844
  try {
3409
3845
  const userContent = this.buildUserContent(message, options?.attachments);
@@ -3414,8 +3850,9 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
3414
3850
  let plan;
3415
3851
  try {
3416
3852
  const planner = new GraphPlanner(this.llmAdapter);
3417
- plan = await planner.planGraph(messageText, filteredTools);
3853
+ plan = await planner.planGraph(messageText, filteredTools, controller.signal);
3418
3854
  } catch (err) {
3855
+ throwIfAborted(controller.signal);
3419
3856
  throw new ACPError(ACPErrorCodes.GRAPH_PLAN_FAILED, `Graph planning failed: ${err.message}`);
3420
3857
  }
3421
3858
  const graph = new TaskGraph(plan.nodes);
@@ -3429,6 +3866,7 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
3429
3866
  const executor = new GraphExecutor({
3430
3867
  graph,
3431
3868
  parentAgent: this,
3869
+ parentSignal: controller.signal,
3432
3870
  plan,
3433
3871
  maxConcurrency: options?.graphConfig?.maxConcurrency ?? 3,
3434
3872
  maxDepth: options?.graphConfig?.maxDepth ?? 2,
@@ -3436,6 +3874,7 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
3436
3874
  emit: (event, data) => this.emit(event, data)
3437
3875
  });
3438
3876
  const finalPlan = await executor.execute();
3877
+ throwIfAborted(controller.signal);
3439
3878
  const resultLines = Object.entries(finalPlan.results).filter(([, r]) => r.status === "done" && r.output).map(([nodeId, r]) => `[${nodeId}] ${r.output.slice(0, 200)}`);
3440
3879
  const progress = executor.getProgress();
3441
3880
  const finalReply = resultLines.length > 0 ? `Graph task completed (${progress.done}/${progress.total} nodes done, ${progress.failed} failed, ${progress.bypassed} bypassed).
@@ -3454,18 +3893,17 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3454
3893
  totalDuration: Date.now() - startTime
3455
3894
  };
3456
3895
  this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
3457
- await this.persistence.saveSession(session).catch(() => {
3458
- });
3896
+ await this.saveSessionSafely(session);
3459
3897
  return chatResult;
3460
3898
  } finally {
3461
- this.abortControllers.delete(sessionId);
3899
+ execution.cleanup();
3462
3900
  }
3463
3901
  }
3464
3902
  async *chatStreamWithGraph(message, options) {
3465
3903
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
3466
3904
  this.currentSessionId = sessionId;
3467
- const controller = new AbortController();
3468
- this.abortControllers.set(sessionId, controller);
3905
+ const execution = this.beginExecution(sessionId, options);
3906
+ const controller = execution.controller;
3469
3907
  const startTime = Date.now();
3470
3908
  let totalInputTokens = 0;
3471
3909
  let totalOutputTokens = 0;
@@ -3478,7 +3916,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3478
3916
  let plan;
3479
3917
  try {
3480
3918
  const planner = new GraphPlanner(this.llmAdapter);
3481
- plan = await planner.planGraph(messageText, filteredTools);
3919
+ plan = await planner.planGraph(messageText, filteredTools, controller.signal);
3482
3920
  } catch (err) {
3483
3921
  yield { type: "error", code: ACPErrorCodes.GRAPH_PLAN_FAILED, message: `Graph planning failed: ${err.message}` };
3484
3922
  return;
@@ -3495,6 +3933,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3495
3933
  const executor = new GraphExecutor({
3496
3934
  graph,
3497
3935
  parentAgent: this,
3936
+ parentSignal: controller.signal,
3498
3937
  plan,
3499
3938
  maxConcurrency: options?.graphConfig?.maxConcurrency ?? 3,
3500
3939
  maxDepth: options?.graphConfig?.maxDepth ?? 2,
@@ -3506,6 +3945,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3506
3945
  }
3507
3946
  });
3508
3947
  const finalPlan = await executor.execute();
3948
+ throwIfAborted(controller.signal);
3509
3949
  const progress = executor.getProgress();
3510
3950
  const resultLines = Object.entries(finalPlan.results).filter(([, r]) => r.status === "done" && r.output).map(([nodeId, r]) => `[${nodeId}] ${r.output.slice(0, 200)}`);
3511
3951
  const finalReply = resultLines.length > 0 ? `Graph task completed (${progress.done}/${progress.total} nodes done, ${progress.failed} failed, ${progress.bypassed} bypassed).
@@ -3517,11 +3957,11 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3517
3957
  const session = this.sessionManager.getOrThrow(sessionId);
3518
3958
  session.meta = session.meta ?? {};
3519
3959
  session.meta.graphPlan = finalPlan;
3960
+ const saving = this.saveSessionSafely(session);
3520
3961
  yield { type: "done", reply: finalReply, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3521
- await this.persistence.saveSession(session).catch(() => {
3522
- });
3962
+ await saving;
3523
3963
  } finally {
3524
- this.abortControllers.delete(sessionId);
3964
+ execution.cleanup();
3525
3965
  }
3526
3966
  }
3527
3967
  /** Resume a graph execution from checkpoint */
@@ -3532,6 +3972,8 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3532
3972
  if (!savedPlan) {
3533
3973
  throw new ACPError(ACPErrorCodes.GRAPH_PLAN_FAILED, "No graph plan found in session");
3534
3974
  }
3975
+ const releaseLock = await this.acquireSessionLock(sessionId);
3976
+ this.currentSessionId = sessionId;
3535
3977
  for (const [nodeId, result] of Object.entries(savedPlan.results)) {
3536
3978
  if (result.status === "failed" || result.status === "bypassed") {
3537
3979
  savedPlan.results[nodeId] = {
@@ -3544,14 +3986,15 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3544
3986
  }
3545
3987
  }
3546
3988
  savedPlan.status = "executing";
3547
- const controller = new AbortController();
3548
- this.abortControllers.set(sessionId, controller);
3989
+ const execution = this.beginExecution(sessionId, options);
3990
+ const controller = execution.controller;
3549
3991
  const startTime = Date.now();
3550
3992
  try {
3551
3993
  const graph = new TaskGraph(savedPlan.nodes);
3552
3994
  const executor = new GraphExecutor({
3553
3995
  graph,
3554
3996
  parentAgent: this,
3997
+ parentSignal: controller.signal,
3555
3998
  plan: savedPlan,
3556
3999
  maxConcurrency: options?.graphConfig?.maxConcurrency ?? 3,
3557
4000
  maxDepth: options?.graphConfig?.maxDepth ?? 2,
@@ -3559,6 +4002,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3559
4002
  emit: (event, data) => this.emit(event, data)
3560
4003
  });
3561
4004
  const finalPlan = await executor.execute();
4005
+ throwIfAborted(controller.signal);
3562
4006
  const resultLines = Object.entries(finalPlan.results).filter(([, r]) => r.status === "done" && r.output).map(([nodeId, r]) => `[${nodeId}] ${r.output.slice(0, 200)}`);
3563
4007
  const progress = executor.getProgress();
3564
4008
  const finalReply = resultLines.length > 0 ? `Graph resumed & completed (${progress.done}/${progress.total} nodes done).
@@ -3575,11 +4019,11 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3575
4019
  totalDuration: Date.now() - startTime
3576
4020
  };
3577
4021
  this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
3578
- await this.persistence.saveSession(session).catch(() => {
3579
- });
4022
+ await this.saveSessionSafely(session);
3580
4023
  return chatResult;
3581
4024
  } finally {
3582
- this.abortControllers.delete(sessionId);
4025
+ execution.cleanup();
4026
+ releaseLock();
3583
4027
  }
3584
4028
  }
3585
4029
  /** Get graph plan from session */
@@ -3600,6 +4044,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3600
4044
  const failedSteps = [];
3601
4045
  const total = workflow.steps.length;
3602
4046
  for (let i = 0; i < workflow.steps.length; i++) {
4047
+ throwIfAborted(ctx.signal);
3603
4048
  const step = workflow.steps[i];
3604
4049
  const interpolatedPrompt = interpolateTemplate(step.prompt, {
3605
4050
  inputs: params,
@@ -3616,7 +4061,8 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3616
4061
  try {
3617
4062
  const result = await ctx.agent.chatInner(interpolatedPrompt, ctx.sessionId, {
3618
4063
  maxTurns: step.maxTurns ?? 10,
3619
- tools: step.tools
4064
+ tools: step.tools,
4065
+ signal: ctx.signal
3620
4066
  });
3621
4067
  stepOutputs[step.name] = { output: result.reply };
3622
4068
  completedSteps.push(step.name);
@@ -3701,6 +4147,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3701
4147
  const executor = new GraphExecutor({
3702
4148
  graph,
3703
4149
  parentAgent: this,
4150
+ parentSignal: ctx.signal,
3704
4151
  plan,
3705
4152
  maxConcurrency: 3,
3706
4153
  maxDepth: 2,
@@ -3708,6 +4155,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3708
4155
  emit: (event, data) => this.emit(event, data)
3709
4156
  });
3710
4157
  const finalPlan = await executor.execute();
4158
+ throwIfAborted(ctx.signal);
3711
4159
  const aggregation = skill.subgraph?.aggregation ?? "concat";
3712
4160
  if (aggregation === "last") {
3713
4161
  const lastDone = Object.values(finalPlan.results).reverse().find((r) => r.status === "done" && r.output);
@@ -3746,6 +4194,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3746
4194
  const executor = new GraphExecutor({
3747
4195
  graph,
3748
4196
  parentAgent: this,
4197
+ parentSignal: ctx.signal,
3749
4198
  plan,
3750
4199
  maxConcurrency: 3,
3751
4200
  maxDepth: 2,
@@ -3753,6 +4202,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3753
4202
  emit: (event, data) => this.emit(event, data)
3754
4203
  });
3755
4204
  const finalPlan = await executor.execute();
4205
+ throwIfAborted(ctx.signal);
3756
4206
  const aggregation = subgraphDef.aggregation ?? "concat";
3757
4207
  if (aggregation === "last") {
3758
4208
  const lastDone = Object.values(finalPlan.results).reverse().find((r) => r.status === "done" && r.output);
@@ -3771,7 +4221,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3771
4221
  // ---- Session Management ----
3772
4222
  createSession(options) {
3773
4223
  const session = this.sessionManager.create(options);
3774
- this.currentSessionId = session.id;
4224
+ if (options?.activate !== false) this.currentSessionId = session.id;
3775
4225
  return session;
3776
4226
  }
3777
4227
  getSession(sessionId) {
@@ -3784,17 +4234,75 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3784
4234
  return this.sessionManager.search(query, options);
3785
4235
  }
3786
4236
  mergeSessions(fromId, toId, strategy) {
4237
+ this.ensureSessionIsNotBeingManaged(fromId);
4238
+ this.ensureSessionIsNotBeingManaged(toId);
3787
4239
  this.sessionManager.merge(fromId, toId, strategy);
3788
4240
  }
3789
4241
  deleteSession(sessionId) {
4242
+ this.ensureSessionIsNotBeingManaged(sessionId);
4243
+ this.removeLiveSession(sessionId);
4244
+ }
4245
+ removeLiveSession(sessionId) {
3790
4246
  this.sessionManager.delete(sessionId);
3791
4247
  if (this.currentSessionId === sessionId) {
3792
4248
  this.currentSessionId = null;
3793
4249
  }
3794
4250
  }
3795
4251
  archiveSession(sessionId) {
4252
+ this.ensureSessionIsNotBeingManaged(sessionId);
3796
4253
  this.sessionManager.archive(sessionId);
3797
4254
  }
4255
+ /** List committed history without loading it into the Agent. */
4256
+ async listStoredSessions(options) {
4257
+ await Promise.all(this.sessionStorageQueues.values());
4258
+ return (await this.persistence.listSessions(options)).map((session) => this.snapshotSession(session));
4259
+ }
4260
+ /** Load one stored session on demand. A live session takes precedence. */
4261
+ async restoreSession(sessionId, options) {
4262
+ return this.manageSessionStorage([sessionId], async () => {
4263
+ const session = await this.queueSessionStorage(sessionId, () => this.readSessionForStorage(sessionId));
4264
+ if (!session) return null;
4265
+ this.sessionManager.restore(session);
4266
+ if (options?.activate !== false) this.currentSessionId = sessionId;
4267
+ return session;
4268
+ });
4269
+ }
4270
+ /** Archive a stored or live session, publishing the update after commit. */
4271
+ async archiveStoredSession(sessionId) {
4272
+ return this.manageSessionStorage([sessionId], () => this.queueSessionStorage(sessionId, async () => {
4273
+ const session = await this.requireSessionForStorage(sessionId);
4274
+ await this.commitStoredSession(session, { ...session, status: "archived", updatedAt: Date.now() });
4275
+ }));
4276
+ }
4277
+ /** Commit an idle session, then release its live copy. Restore it by ID when needed. */
4278
+ async unloadSession(sessionId) {
4279
+ return this.manageSessionStorage([sessionId], () => this.queueSessionStorage(sessionId, async () => {
4280
+ const session = this.sessionManager.get(sessionId);
4281
+ if (!session) return false;
4282
+ await this.persistence.saveSession(this.snapshotSession(session));
4283
+ this.removeLiveSession(sessionId);
4284
+ return true;
4285
+ }));
4286
+ }
4287
+ /** Merge history into the target session and commit the target. */
4288
+ async mergeStoredSessions(fromId, toId, strategy) {
4289
+ return this.manageSessionStorage([fromId, toId], () => this.queueSessionStorage(toId, async () => {
4290
+ const from = await this.requireSessionForStorage(fromId);
4291
+ const to = fromId === toId ? from : await this.requireSessionForStorage(toId);
4292
+ const updated = { ...to, messages: [...to.messages] };
4293
+ SessionManager.mergeContent(from, updated, strategy);
4294
+ await this.commitStoredSession(to, updated);
4295
+ }));
4296
+ }
4297
+ /** Delete committed and live history, including when a chat save is pending. */
4298
+ async deleteStoredSession(sessionId) {
4299
+ return this.manageSessionStorage([sessionId], (state) => this.queueSessionStorage(sessionId, async () => {
4300
+ await this.persistence.deleteSession(sessionId);
4301
+ state.deleted = true;
4302
+ this.abortControllers.get(sessionId)?.abort();
4303
+ this.removeLiveSession(sessionId);
4304
+ }), { allowRunning: true });
4305
+ }
3798
4306
  // ---- Context Management ----
3799
4307
  getContext(sessionId, format = "window") {
3800
4308
  const session = this.sessionManager.getOrThrow(sessionId);
@@ -3810,16 +4318,45 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3810
4318
  return this.contextManager.search(allMessages, query, options);
3811
4319
  }
3812
4320
  async compressContext(sessionId, strategy) {
4321
+ this.ensureSessionIsNotBeingManaged(sessionId);
3813
4322
  const session = this.sessionManager.getOrThrow(sessionId);
3814
- const result = await this.contextManager.compress(session.messages, strategy);
4323
+ this.compressingSessions.set(sessionId, (this.compressingSessions.get(sessionId) ?? 0) + 1);
4324
+ try {
4325
+ const result = await this.contextManager.compress(session.messages, strategy);
4326
+ session.messages = result.messages;
4327
+ session.tokenCount = result.afterTokens;
4328
+ session.updatedAt = Date.now();
4329
+ if (result.afterTokens < result.beforeTokens) {
4330
+ this.emit("context:compress", { sessionId, beforeTokens: result.beforeTokens, afterTokens: result.afterTokens, reason: "manual" });
4331
+ }
4332
+ return { beforeTokens: result.beforeTokens, afterTokens: result.afterTokens, compressionRatio: result.afterTokens / (result.beforeTokens || 1) };
4333
+ } finally {
4334
+ const remaining = this.compressingSessions.get(sessionId) - 1;
4335
+ if (remaining) this.compressingSessions.set(sessionId, remaining);
4336
+ else this.compressingSessions.delete(sessionId);
4337
+ }
4338
+ }
4339
+ async prepareOverflowRetry(session, rejected, systemPrompt, tools, signal) {
4340
+ const result = await this.contextManager.compressForOverflow(session.messages, rejected, systemPrompt, tools, signal);
4341
+ if (!result) return;
4342
+ throwIfAborted(signal);
3815
4343
  session.messages = result.messages;
3816
4344
  session.tokenCount = result.afterTokens;
3817
4345
  session.updatedAt = Date.now();
3818
- this.emit("context:compress", { sessionId, beforeTokens: result.beforeTokens, afterTokens: result.afterTokens, reason: "manual" });
3819
- return { beforeTokens: result.beforeTokens, afterTokens: result.afterTokens, compressionRatio: result.afterTokens / (result.beforeTokens || 1) };
4346
+ this.emit("context:compress", {
4347
+ sessionId: session.id,
4348
+ beforeTokens: rejected.usedTokens,
4349
+ afterTokens: result.window.usedTokens,
4350
+ reason: "manual"
4351
+ });
4352
+ return result.window.messages;
3820
4353
  }
3821
4354
  // ---- Runtime Config ----
3822
4355
  configure(config) {
4356
+ if (this.config.llmProxyMode !== void 0 || config.llmProxyMode !== void 0) {
4357
+ this.configureProxy(config);
4358
+ return;
4359
+ }
3823
4360
  if (config.llm) {
3824
4361
  Object.assign(this.config.llm, config.llm);
3825
4362
  this.llmAdapter = new OpenAIAdapter(this.config.llm);
@@ -3829,6 +4366,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3829
4366
  if (config.serverUrl) {
3830
4367
  this.config.serverUrl = config.serverUrl;
3831
4368
  this.transport = new HttpTransport(config.serverUrl, this.config.authToken, this.config.headers);
4369
+ this.mcpStatus = "unchecked";
3832
4370
  }
3833
4371
  if (config.authToken) {
3834
4372
  this.config.authToken = config.authToken;
@@ -3837,6 +4375,33 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3837
4375
  this.config.systemPrompt = config.systemPrompt;
3838
4376
  }
3839
4377
  }
4378
+ configureProxy(config) {
4379
+ const current = this.config;
4380
+ const next = {
4381
+ ...current,
4382
+ llm: { ...current.llm, ...config.llm },
4383
+ llmProxyMode: config.llmProxyMode === void 0 ? current.llmProxyMode : config.llmProxyMode,
4384
+ serverUrl: config.serverUrl === void 0 ? current.serverUrl : config.serverUrl,
4385
+ authToken: config.authToken ?? current.authToken,
4386
+ headers: config.headers ?? current.headers,
4387
+ systemPrompt: config.systemPrompt ?? current.systemPrompt
4388
+ };
4389
+ const { llm, serverUrl } = resolveProxyConfig(next);
4390
+ const transportChanged = next.serverUrl !== current.serverUrl || next.authToken !== current.authToken || next.headers !== current.headers || current.llmProxyMode === void 0;
4391
+ this.config = next;
4392
+ if (config.llm || next.llmProxyMode !== current.llmProxyMode || next.llmProxyMode === "listdoing-server" && (next.serverUrl !== current.serverUrl || next.authToken !== current.authToken)) {
4393
+ this.llmAdapter = new OpenAIAdapter(llm, { rejectRedirects: true });
4394
+ this.contextManager.setModel(llm.model);
4395
+ this.contextManager.setSummarizer(createLLMSummarizer(this.llmAdapter));
4396
+ }
4397
+ if (transportChanged) {
4398
+ this.transport = serverUrl ? new HttpTransport(serverUrl, next.authToken, next.headers) : new LocalTransport();
4399
+ this.mcpStatus = "unchecked";
4400
+ for (const skill of this.skillRegistry.list()) {
4401
+ if (this.discoveredServerSkills.has(skill)) this.skillRegistry.unregister(skill.name);
4402
+ }
4403
+ }
4404
+ }
3840
4405
  // ---- Events ----
3841
4406
  on(event, handler) {
3842
4407
  if (!this.eventHandlers.has(event)) {
@@ -3857,10 +4422,15 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3857
4422
  }
3858
4423
  // ---- Private Helpers ----
3859
4424
  /**
3860
- * Stream LLM response token-by-token via the adapter's chatStream.
3861
- * Forwards llm_text events to the caller and collects the full StandardLLMResponse.
4425
+ * Forward streaming events, or emit the complete response for a non-streaming adapter.
3862
4426
  */
3863
4427
  async *streamLlmResponse(messages, tools, signal) {
4428
+ if (typeof this.llmAdapter.chatStream !== "function") {
4429
+ const response = await this.llmAdapter.chat(messages, tools, { signal });
4430
+ if (response.reasoning) yield { type: "llm_thinking", content: response.reasoning };
4431
+ if (response.content) yield { type: "llm_text", content: response.content };
4432
+ return response;
4433
+ }
3864
4434
  let fullResponse = null;
3865
4435
  for await (const event of this.llmAdapter.chatStream(messages, tools, { signal })) {
3866
4436
  if (event.type === "llm_text" || event.type === "llm_thinking") {
@@ -3954,59 +4524,65 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3954
4524
  });
3955
4525
  }
3956
4526
  // ── Parallel tool execution helpers ──
3957
- planToolExecution(toolCalls, chatOptions, requireApproval) {
4527
+ planToolExecution(toolCalls, chatOptions, requireApproval, runContext, exposedTools) {
3958
4528
  const items = toolCalls.map((tc) => {
3959
4529
  const isMcp = this.mcpRouter.isMcpTool(tc.name);
3960
- const skillDef = this.skillRegistry.get(tc.name);
4530
+ const scopedSkill = runContext?.registry.get(tc.name);
4531
+ const skillDef = scopedSkill ?? this.skillRegistry.get(tc.name);
3961
4532
  const location = isMcp ? "server" : skillDef?.location === "server" ? "server" : "local";
3962
- const permPolicy = this.checkToolPermission(skillDef, tc.arguments, chatOptions, requireApproval);
4533
+ const permPolicy = scopedSkill && !exposedTools.some((tool) => tool.name === tc.name) ? "disabled" : this.checkToolPermission(skillDef, tc.arguments, chatOptions, requireApproval);
3963
4534
  const parallelSafe = skillDef?.parallelSafe ?? true;
3964
4535
  return { toolCall: tc, skillDef, location, permPolicy, parallelSafe };
3965
4536
  });
4537
+ const exclusiveSkill = runContext?.exclusiveSkill;
4538
+ if (exclusiveSkill) {
4539
+ const exclusiveCall = items.find((item) => item.toolCall.name === exclusiveSkill && item.permPolicy !== "disabled");
4540
+ if (exclusiveCall) {
4541
+ for (const item of items) {
4542
+ if (item !== exclusiveCall) {
4543
+ item.skippedReason = `Skipped because ${exclusiveSkill} must run alone. Reconsider this call after receiving the interaction result.`;
4544
+ }
4545
+ }
4546
+ }
4547
+ }
3966
4548
  const canParallelize = items.every((item) => item.parallelSafe);
3967
4549
  return { canParallelize, items };
3968
4550
  }
3969
- async processApprovalQueue(items, sessionId, emit, yieldEvent) {
4551
+ async *processApprovalQueue(items, sessionId, signal) {
3970
4552
  for (const item of items) {
3971
- if (item.permPolicy === "disabled") {
3972
- const denyResult = {
3973
- content: [{ type: "text", text: `Tool "${item.toolCall.name}" is disabled in current permission mode.` }],
3974
- isError: true
3975
- };
3976
- item.resolved = true;
3977
- item.result = denyResult;
3978
- this.addToolResult(sessionId, item.toolCall, denyResult);
3979
- emit("tool:result", { sessionId, toolName: item.toolCall.name, isError: true, summary: "Permission denied (disabled)", duration: 0 });
3980
- if (yieldEvent) {
3981
- yieldEvent({ type: "tool_result", callId: item.toolCall.id, toolName: item.toolCall.name, isError: true, summary: "Permission denied (disabled)", duration: 0 });
3982
- }
3983
- } else if (item.permPolicy === "approval" || item.permPolicy === "force-approval") {
3984
- emit("tool:approval", { sessionId, toolName: item.toolCall.name, arguments: item.toolCall.arguments, callId: item.toolCall.id });
3985
- if (yieldEvent) {
3986
- yieldEvent({ type: "tool_approval", callId: item.toolCall.id, toolName: item.toolCall.name, arguments: item.toolCall.arguments });
3987
- }
3988
- const approved = await this.waitForApproval(item.toolCall.id);
3989
- if (!approved) {
3990
- const rejectResult = {
3991
- content: [{ type: "text", text: "Tool call rejected by user." }],
3992
- isError: true
3993
- };
3994
- item.resolved = true;
3995
- item.result = rejectResult;
3996
- this.addToolResult(sessionId, item.toolCall, rejectResult);
3997
- emit("tool:result", { sessionId, toolName: item.toolCall.name, isError: true, summary: "Rejected", duration: 0 });
3998
- if (yieldEvent) {
3999
- yieldEvent({ type: "tool_result", callId: item.toolCall.id, toolName: item.toolCall.name, isError: true, summary: "Rejected", duration: 0 });
4000
- }
4553
+ throwIfAborted(signal);
4554
+ const tc = item.toolCall;
4555
+ this.emit("tool:call", { sessionId, toolName: tc.name, arguments: tc.arguments, location: item.location });
4556
+ yield { type: "tool_call", callId: tc.id, toolName: tc.name, arguments: tc.arguments, location: item.location };
4557
+ let reason;
4558
+ if (tc.parseError) reason = `Invalid tool arguments: ${tc.parseError}`;
4559
+ else if (item.permPolicy === "disabled") reason = `Tool "${tc.name}" is disabled in current permission mode.`;
4560
+ else if (item.skippedReason) reason = item.skippedReason;
4561
+ else if (item.permPolicy === "approval" || item.permPolicy === "force-approval") {
4562
+ const decision = this.waitForApproval(tc.id, signal);
4563
+ decision.catch(() => {
4564
+ });
4565
+ this.emit("tool:approval", { sessionId, toolName: tc.name, arguments: tc.arguments, callId: tc.id });
4566
+ yield { type: "tool_approval", callId: tc.id, toolName: tc.name, arguments: tc.arguments };
4567
+ try {
4568
+ if (!await decision) reason = "Tool call rejected by user.";
4569
+ } finally {
4570
+ this.approvalResolvers.get(tc.id)?.resolve(false);
4001
4571
  }
4002
4572
  }
4573
+ throwIfAborted(signal);
4574
+ if (reason) {
4575
+ const result = { content: [{ type: "text", text: reason }], isError: true };
4576
+ item.resolved = true;
4577
+ item.result = result;
4578
+ this.addToolResult(sessionId, tc, result);
4579
+ this.emit("tool:result", { sessionId, toolName: tc.name, isError: true, summary: reason, duration: 0 });
4580
+ yield { type: "tool_result", callId: tc.id, toolName: tc.name, isError: true, summary: reason, duration: 0 };
4581
+ }
4003
4582
  }
4004
4583
  }
4005
4584
  async executeToolBatch(items, sessionId, controller, canParallelize, emit) {
4006
4585
  const traceEntries = [];
4007
- for (const item of items) {
4008
- emit("tool:call", { sessionId, toolName: item.toolCall.name, arguments: item.toolCall.arguments, location: item.location });
4009
- }
4010
4586
  for (const item of items) {
4011
4587
  if (item.resolved) {
4012
4588
  const summary = item.result?.content.filter((c) => c.type === "text").map((c) => c.text.slice(0, 100)).join(" ") ?? "";
@@ -4062,18 +4638,41 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
4062
4638
  traceEntries.push({ callId: item.toolCall.id, toolName: item.toolCall.name, location: item.location, arguments: item.toolCall.arguments, result: { isError: result.isError ?? false, summary }, duration });
4063
4639
  }
4064
4640
  }
4641
+ throwIfAborted(controller.signal);
4065
4642
  return traceEntries;
4066
4643
  }
4067
- async executeToolBatchStream(items, sessionId, controller, canParallelize, yieldEvent) {
4068
- const traceEntries = [];
4069
- for (const item of items) {
4070
- yieldEvent({ type: "tool_call", callId: item.toolCall.id, toolName: item.toolCall.name, arguments: item.toolCall.arguments, location: item.location });
4071
- if (item.resolved) {
4072
- const summary = item.result?.content.filter((c) => c.type === "text").map((c) => c.text.slice(0, 100)).join(" ") ?? "";
4073
- yieldEvent({ type: "tool_result", callId: item.toolCall.id, toolName: item.toolCall.name, isError: true, summary, duration: 0 });
4074
- traceEntries.push({ callId: item.toolCall.id, toolName: item.toolCall.name, location: item.location, arguments: item.toolCall.arguments, result: { isError: true, summary }, duration: 0 });
4644
+ async *streamToolBatch(items, sessionId, controller, parallel) {
4645
+ const events = [];
4646
+ let wake;
4647
+ let done = false;
4648
+ let failure;
4649
+ const task = this.executeToolBatchStream(items, sessionId, controller, parallel, (event) => {
4650
+ events.push(event);
4651
+ wake?.();
4652
+ }).then(() => {
4653
+ done = true;
4654
+ wake?.();
4655
+ }, (error) => {
4656
+ failure = error;
4657
+ done = true;
4658
+ wake?.();
4659
+ });
4660
+ try {
4661
+ while (!done || events.length) {
4662
+ if (events.length) yield events.shift();
4663
+ else await new Promise((resolve) => {
4664
+ wake = resolve;
4665
+ });
4075
4666
  }
4667
+ if (failure) throw failure;
4668
+ throwIfAborted(controller.signal);
4669
+ } finally {
4670
+ if (!done) controller.abort();
4671
+ await task;
4076
4672
  }
4673
+ }
4674
+ async executeToolBatchStream(items, sessionId, controller, canParallelize, yieldEvent) {
4675
+ const traceEntries = [];
4077
4676
  const executableItems = items.filter((item) => !item.resolved);
4078
4677
  if (executableItems.length === 0) return traceEntries;
4079
4678
  if (executableItems.length === 1) {
@@ -4123,6 +4722,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
4123
4722
  traceEntries.push({ callId: item.toolCall.id, toolName: item.toolCall.name, location: item.location, arguments: item.toolCall.arguments, result: { isError: result.isError ?? false, summary }, duration });
4124
4723
  }
4125
4724
  }
4725
+ throwIfAborted(controller.signal);
4126
4726
  return traceEntries;
4127
4727
  }
4128
4728
  /**
@@ -4130,28 +4730,39 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
4130
4730
  */
4131
4731
  async executeToolCall(toolCall, sessionId, controller) {
4132
4732
  const execStart = Date.now();
4733
+ throwIfAborted(controller.signal);
4734
+ if (toolCall.parseError) return { content: [{ type: "text", text: toolCall.parseError }], isError: true };
4133
4735
  if (this.mcpRouter.isMcpTool(toolCall.name)) {
4134
4736
  try {
4135
- const result = await this.mcpRouter.call(toolCall.name, toolCall.arguments);
4136
- return { content: [{ type: "text", text: typeof result === "string" ? result : JSON.stringify(result) }] };
4737
+ const result = await this.mcpRouter.call(toolCall.name, toolCall.arguments, { signal: controller.signal });
4738
+ throwIfAborted(controller.signal);
4739
+ return { content: [{ type: "text", text: typeof result === "string" ? result : JSON.stringify(result) ?? "" }] };
4137
4740
  } catch (err) {
4138
- return { content: [{ type: "text", text: `MCP error: ${err.message}` }], isError: true };
4741
+ throwIfAborted(controller.signal);
4742
+ return { content: [{ type: "text", text: `MCP error: ${errorMessage(err)}` }], isError: true };
4139
4743
  }
4140
4744
  }
4141
- const skillDef = this.skillRegistry.get(toolCall.name);
4745
+ const scope = this.runContexts.get(controller.signal);
4746
+ const skillDef = scope?.registry.get(toolCall.name) ?? this.skillRegistry.get(toolCall.name);
4142
4747
  const location = skillDef?.location === "server" ? "server" : "local";
4143
4748
  try {
4144
4749
  if (location === "server" && this.transport instanceof HttpTransport) {
4145
- return await this.executeServerSkill(toolCall);
4750
+ return await this.executeServerSkill(toolCall, controller.signal);
4146
4751
  } else {
4147
- const invokeFn = (skillName, params) => this.skillExecutor.invokeDirect(skillName, params, {
4148
- sessionId,
4149
- agent: this,
4150
- signal: controller.signal,
4151
- depth: 0,
4152
- invoke: invokeFn
4153
- });
4154
- return await this.skillExecutor.execute(toolCall.name, toolCall.arguments, {
4752
+ const executorFor = (name) => scope?.registry.has(name) ? scope.executor : this.skillExecutor;
4753
+ const invokeFn = async (skillName, params) => {
4754
+ if (skillName === scope?.exclusiveSkill) {
4755
+ return { isError: true, content: [{ type: "text", text: "This interaction requires a top-level tool call." }] };
4756
+ }
4757
+ return executorFor(skillName).invokeDirect(skillName, params, {
4758
+ sessionId,
4759
+ agent: this,
4760
+ signal: controller.signal,
4761
+ depth: 0,
4762
+ invoke: invokeFn
4763
+ });
4764
+ };
4765
+ return await executorFor(toolCall.name).execute(toolCall.name, toolCall.arguments, {
4155
4766
  sessionId,
4156
4767
  agent: this,
4157
4768
  signal: controller.signal,
@@ -4160,14 +4771,146 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
4160
4771
  });
4161
4772
  }
4162
4773
  } catch (err) {
4163
- return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
4774
+ if (controller.signal.aborted || err?.name === "AbortError") throw abortError();
4775
+ return { content: [{ type: "text", text: `Error: ${errorMessage(err)}` }], isError: true };
4776
+ }
4777
+ }
4778
+ prepareRunContext(signal, context) {
4779
+ if (!context) return this.runContexts.get(signal);
4780
+ const registry = new SkillRegistry();
4781
+ for (const skill of context.skills ?? []) {
4782
+ if (!skill.handler || skill.location === "server" || this.mcpRouter.isMcpTool(skill.name) || this.skillRegistry.has(skill.name) || registry.has(skill.name)) {
4783
+ throw new ACPError(ACPErrorCodes.SKILL_REGISTRATION_FAILED, `Invalid or conflicting run-local skill: ${skill.name}`);
4784
+ }
4785
+ registry.register({ ...skill });
4786
+ }
4787
+ const scope = {
4788
+ systemPromptSuffix: context.systemPromptSuffix,
4789
+ exclusiveSkill: context.exclusiveSkill && registry.has(context.exclusiveSkill) ? context.exclusiveSkill : void 0,
4790
+ registry,
4791
+ executor: new SkillExecutor(registry, void 0, { validateInputs: this.config.skills?.validateInputs })
4792
+ };
4793
+ this.runContexts.set(signal, scope);
4794
+ return scope;
4795
+ }
4796
+ buildRunSystemPrompt(session, runContext) {
4797
+ const base = this.resolveSystemPrompt(session.id, session.systemPrompt ?? this.config.systemPrompt ?? "");
4798
+ return runContext?.systemPromptSuffix ? [base, runContext.systemPromptSuffix].filter(Boolean).join("\n\n") : base;
4799
+ }
4800
+ listRunTools(runContext, nested) {
4801
+ const tools = this.skillRegistry.listToolDefs();
4802
+ if (!runContext) return tools;
4803
+ const localTools = runContext.registry.listToolDefs();
4804
+ const visibleTools = nested && runContext.exclusiveSkill ? localTools.filter((tool) => tool.name !== runContext.exclusiveSkill) : localTools;
4805
+ return [...tools.filter((tool) => !runContext.registry.has(tool.name)), ...visibleTools];
4806
+ }
4807
+ beginExecution(sessionId, options) {
4808
+ const previous = this.abortControllers.get(sessionId);
4809
+ const controller = new AbortController();
4810
+ const unlinkRoot = linkAbort(previous?.signal, controller);
4811
+ const unlinkParent = linkAbort(options?.signal, controller);
4812
+ const inherited = previous && this.runContexts.get(previous.signal);
4813
+ if (inherited) this.runContexts.set(controller.signal, inherited);
4814
+ if (!previous) this.abortControllers.set(sessionId, controller);
4815
+ return { controller, cleanup: () => {
4816
+ controller.abort();
4817
+ this.runContexts.delete(controller.signal);
4818
+ unlinkRoot();
4819
+ unlinkParent();
4820
+ if (this.abortControllers.get(sessionId) === controller) this.abortControllers.delete(sessionId);
4821
+ } };
4822
+ }
4823
+ ensureSessionIsNotBeingManaged(sessionId) {
4824
+ if (this.managingSessions.has(sessionId)) {
4825
+ throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${sessionId} has a pending storage operation`);
4826
+ }
4827
+ }
4828
+ async manageSessionStorage(sessionIds, operation, options) {
4829
+ for (const id of sessionIds) {
4830
+ this.ensureSessionIsNotBeingManaged(id);
4831
+ if (this.compressingSessions.has(id)) {
4832
+ throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${id} is compressing its context`);
4833
+ }
4834
+ if (!options?.allowRunning && (this.runningSessions.has(id) || this.abortControllers.has(id))) {
4835
+ throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${id} is already running a chat`);
4836
+ }
4164
4837
  }
4838
+ const state = { deleted: false };
4839
+ for (const id of sessionIds) this.managingSessions.set(id, state);
4840
+ try {
4841
+ return await operation(state);
4842
+ } finally {
4843
+ for (const id of sessionIds) this.managingSessions.delete(id);
4844
+ }
4845
+ }
4846
+ async readSessionForStorage(sessionId) {
4847
+ const current = this.sessionManager.get(sessionId);
4848
+ if (current) return current;
4849
+ const stored = await this.persistence.loadSession(sessionId);
4850
+ return stored ? this.snapshotSession(stored) : null;
4851
+ }
4852
+ async requireSessionForStorage(sessionId) {
4853
+ const session = await this.readSessionForStorage(sessionId);
4854
+ if (session) return session;
4855
+ throw new ACPError(ACPErrorCodes.SESSION_NOT_FOUND, `Session not found: ${sessionId}`, { sessionId });
4856
+ }
4857
+ async commitStoredSession(original, updated) {
4858
+ await this.persistence.saveSession(this.snapshotSession(updated));
4859
+ if (this.sessionManager.get(original.id) === original) Object.assign(original, updated);
4860
+ }
4861
+ snapshotSession(session) {
4862
+ try {
4863
+ return structuredClone(session);
4864
+ } catch {
4865
+ return { ...session, messages: session.messages.map((message) => ({ ...message })) };
4866
+ }
4867
+ }
4868
+ queueSessionStorage(sessionId, operation) {
4869
+ const previous = this.sessionStorageQueues.get(sessionId) ?? Promise.resolve();
4870
+ const result = previous.then(operation);
4871
+ const settled = result.then(() => {
4872
+ }, () => {
4873
+ });
4874
+ this.sessionStorageQueues.set(sessionId, settled);
4875
+ void settled.then(() => {
4876
+ if (this.sessionStorageQueues.get(sessionId) === settled) this.sessionStorageQueues.delete(sessionId);
4877
+ });
4878
+ return result;
4879
+ }
4880
+ saveSessionSafely(session) {
4881
+ const snapshot = this.snapshotSession(session);
4882
+ const management = this.managingSessions.get(session.id);
4883
+ return this.queueSessionStorage(session.id, async () => {
4884
+ if (management?.deleted) return;
4885
+ try {
4886
+ await this.persistence.saveSession(snapshot);
4887
+ } catch (error) {
4888
+ try {
4889
+ this.config.onDiagnostic?.({ kind: "persistence", sessionId: session.id, message: errorMessage(error) });
4890
+ } catch {
4891
+ }
4892
+ this.emit("persistence:error", { sessionId: session.id, message: errorMessage(error) });
4893
+ }
4894
+ });
4165
4895
  }
4166
4896
  ensureInitialized() {
4167
4897
  if (!this.initialized) {
4168
4898
  throw new ACPError(ACPErrorCodes.AGENT_NOT_INITIALIZED, "Agent not initialized. Call init() first.");
4169
4899
  }
4170
4900
  }
4901
+ reportPromptCacheUsage(sessionId, promptCacheUsage) {
4902
+ if (!this.config.llm.collectPromptCacheUsage || !promptCacheUsage) return;
4903
+ try {
4904
+ this.config.onDiagnostic?.({
4905
+ kind: "llm:prompt-cache",
4906
+ message: "Model reported prompt cache usage",
4907
+ sessionId,
4908
+ model: this.config.llm.model,
4909
+ promptCacheUsage
4910
+ });
4911
+ } catch {
4912
+ }
4913
+ }
4171
4914
  ensureSession() {
4172
4915
  if (this.currentSessionId) {
4173
4916
  const existing = this.sessionManager.get(this.currentSessionId);
@@ -4200,9 +4943,10 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
4200
4943
  this.sessionManager.addMessage(sessionId, { role: "tool", content: result.content, toolCallId: toolCall.id });
4201
4944
  }
4202
4945
  }
4203
- async executeServerSkill(toolCall) {
4946
+ async executeServerSkill(toolCall, signal) {
4204
4947
  const transport = this.transport;
4205
- const result = await transport.send("tools/call", { name: toolCall.name, arguments: toolCall.arguments });
4948
+ const result = await transport.send("tools/call", { name: toolCall.name, arguments: toolCall.arguments }, { signal });
4949
+ if (!result || !Array.isArray(result.content)) throw new Error("Server skill returned an invalid result");
4206
4950
  return result;
4207
4951
  }
4208
4952
  };