@listdoing/core 0.1.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -65,6 +65,7 @@ var ACPErrorCodes = {
65
65
  LLM_CALL_FAILED: -32130,
66
66
  LLM_EMPTY_RESPONSE: -32135,
67
67
  LLM_TOKEN_LIMIT: -32131,
68
+ LLM_PROXY_CONFIG_INVALID: -32136,
68
69
  LLM_RATE_LIMIT: -32132,
69
70
  LLM_PROVIDER_UNAVAILABLE: -32133,
70
71
  TOKEN_BUDGET_EXCEEDED: -32134,
@@ -129,9 +130,46 @@ var OpenAIContentAdapter = class {
129
130
  }
130
131
  };
131
132
 
133
+ // src/runtime/abort.ts
134
+ function abortError() {
135
+ const error = new Error("Request aborted");
136
+ error.name = "AbortError";
137
+ return error;
138
+ }
139
+ function throwIfAborted(signal) {
140
+ if (signal?.aborted) throw abortError();
141
+ }
142
+ function linkAbort(parent, child) {
143
+ const abort = () => child.abort();
144
+ if (parent?.aborted) abort();
145
+ else parent?.addEventListener("abort", abort, { once: true });
146
+ return () => parent?.removeEventListener("abort", abort);
147
+ }
148
+ function errorMessage(error) {
149
+ return error instanceof Error ? error.message : String(error ?? "Unknown error");
150
+ }
151
+ function abortableDelay(ms, signal) {
152
+ throwIfAborted(signal);
153
+ return new Promise((resolve, reject) => {
154
+ const cleanup = () => signal?.removeEventListener("abort", abort);
155
+ const timer = setTimeout(() => {
156
+ cleanup();
157
+ resolve();
158
+ }, ms);
159
+ const abort = () => {
160
+ clearTimeout(timer);
161
+ cleanup();
162
+ reject(abortError());
163
+ };
164
+ signal?.addEventListener("abort", abort, { once: true });
165
+ });
166
+ }
167
+
132
168
  // src/llm/openai.ts
133
169
  var OpenAIAdapter = class {
134
- constructor(config) {
170
+ constructor(config, options) {
171
+ this.options = options;
172
+ this.verified = false;
135
173
  this.config = config;
136
174
  this.contentAdapter = new OpenAIContentAdapter();
137
175
  }
@@ -150,6 +188,10 @@ var OpenAIAdapter = class {
150
188
  const base = this.getBaseUrl().replace(/\/+$/, "");
151
189
  return `${base}/chat/completions`;
152
190
  }
191
+ getConnectionStatus() {
192
+ const endpoint = this.getEndpoint().trim().split(/[?#]/, 1)[0].replace(/^(https?:\/\/|\/\/)[^/]*@/i, "$1");
193
+ return { endpoint, verified: this.verified };
194
+ }
153
195
  buildHeaders() {
154
196
  const headers = {
155
197
  "Content-Type": "application/json"
@@ -200,51 +242,63 @@ var OpenAIAdapter = class {
200
242
  }));
201
243
  }
202
244
  async chat(messages, tools, options) {
203
- if (this.config.adapter === "custom" && this.config.transformRequest && this.config.transformResponse) {
204
- const reqBody = this.config.transformRequest(messages, tools, {});
205
- const res2 = await fetch(this.getEndpoint(), {
245
+ const request = this.createRequest(options?.signal);
246
+ try {
247
+ if (this.config.adapter === "custom" && this.config.transformRequest && this.config.transformResponse) {
248
+ const reqBody = this.config.transformRequest(messages, tools, {});
249
+ const res2 = await this.fetchResponse({
250
+ method: "POST",
251
+ headers: this.buildHeaders(),
252
+ body: JSON.stringify(reqBody),
253
+ signal: request.controller.signal
254
+ });
255
+ if (!res2.ok) {
256
+ const text = await res2.text().catch(() => "");
257
+ this.checkContextOverflow(res2.status, text);
258
+ throw new Error(`LLM request failed: ${res2.status} ${text}`);
259
+ }
260
+ const data2 = await res2.json();
261
+ const response2 = this.config.transformResponse(data2);
262
+ if (response2?.content || response2?.toolCalls?.length) this.verified = true;
263
+ return response2;
264
+ }
265
+ const body = {
266
+ model: this.config.model,
267
+ messages: this.messagesToOpenAI(messages)
268
+ };
269
+ if (tools.length > 0) {
270
+ body.tools = this.toolsToOpenAI(tools);
271
+ }
272
+ if (this.config.temperature !== void 0) {
273
+ body.temperature = this.config.temperature;
274
+ }
275
+ if (this.config.maxTokens !== void 0) {
276
+ body.max_tokens = this.config.maxTokens;
277
+ }
278
+ if (this.config.extraBody) {
279
+ Object.assign(body, this.config.extraBody);
280
+ }
281
+ const res = await this.fetchResponse({
206
282
  method: "POST",
207
283
  headers: this.buildHeaders(),
208
- body: JSON.stringify(reqBody),
209
- signal: options?.signal
284
+ body: JSON.stringify(body),
285
+ signal: request.controller.signal
210
286
  });
211
- if (!res2.ok) {
212
- const text = await res2.text().catch(() => "");
213
- this.checkContextOverflow(res2.status, text);
214
- throw new Error(`LLM request failed: ${res2.status} ${text}`);
287
+ if (!res.ok) {
288
+ const text = await res.text().catch(() => "");
289
+ this.checkContextOverflow(res.status, text);
290
+ throw new Error(`LLM request failed: ${res.status} ${text}`);
215
291
  }
216
- const data2 = await res2.json();
217
- return this.config.transformResponse(data2);
218
- }
219
- const body = {
220
- model: this.config.model,
221
- messages: this.messagesToOpenAI(messages)
222
- };
223
- if (tools.length > 0) {
224
- body.tools = this.toolsToOpenAI(tools);
225
- }
226
- if (this.config.temperature !== void 0) {
227
- body.temperature = this.config.temperature;
228
- }
229
- if (this.config.maxTokens !== void 0) {
230
- body.max_tokens = this.config.maxTokens;
231
- }
232
- if (this.config.extraBody) {
233
- Object.assign(body, this.config.extraBody);
234
- }
235
- const res = await fetch(this.getEndpoint(), {
236
- method: "POST",
237
- headers: this.buildHeaders(),
238
- body: JSON.stringify(body),
239
- signal: options?.signal
240
- });
241
- if (!res.ok) {
242
- const text = await res.text().catch(() => "");
243
- this.checkContextOverflow(res.status, text);
244
- throw new Error(`LLM request failed: ${res.status} ${text}`);
292
+ const data = await res.json();
293
+ const response = this.parseResponse(data);
294
+ this.verified = true;
295
+ return response;
296
+ } catch (error) {
297
+ if (request.controller.signal.aborted && !options?.signal?.aborted) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "LLM request timed out", { reason: "timeout" });
298
+ throw error;
299
+ } finally {
300
+ request.cleanup();
245
301
  }
246
- const data = await res.json();
247
- return this.parseResponse(data);
248
302
  }
249
303
  /**
250
304
  * Stream LLM responses token-by-token via SSE.
@@ -252,115 +306,206 @@ var OpenAIAdapter = class {
252
306
  * Collects the full response and returns it at the end.
253
307
  */
254
308
  async *chatStream(messages, tools, options) {
255
- const body = {
256
- model: this.config.model,
257
- messages: this.messagesToOpenAI(messages),
258
- stream: true
259
- };
260
- if (tools.length > 0) {
261
- body.tools = this.toolsToOpenAI(tools);
262
- }
263
- if (this.config.temperature !== void 0) {
264
- body.temperature = this.config.temperature;
265
- }
266
- if (this.config.maxTokens !== void 0) {
267
- body.max_tokens = this.config.maxTokens;
268
- }
269
- if (this.config.extraBody) {
270
- Object.assign(body, this.config.extraBody);
271
- }
272
- const res = await fetch(this.getEndpoint(), {
273
- method: "POST",
274
- headers: this.buildHeaders(),
275
- body: JSON.stringify(body),
276
- signal: options?.signal
277
- });
278
- if (!res.ok) {
279
- const text = await res.text().catch(() => "");
280
- this.checkContextOverflow(res.status, text);
281
- throw new Error(`LLM request failed: ${res.status} ${text}`);
282
- }
283
- const reader = res.body?.getReader();
284
- if (!reader) {
285
- const data = await res.json();
286
- const parsed = this.parseResponse(data);
287
- if (parsed.content) {
288
- yield { type: "llm_text", content: parsed.content };
289
- }
309
+ if (this.config.adapter === "custom" && this.config.transformRequest && this.config.transformResponse) {
310
+ const response = await this.chat(messages, tools, options);
311
+ if (response.reasoning) yield { type: "llm_thinking", content: response.reasoning };
312
+ if (response.content) yield { type: "llm_text", content: response.content };
313
+ yield { type: "llm_response", response };
290
314
  return;
291
315
  }
292
- const decoder = new TextDecoder();
293
- let buffer = "";
294
- let content = "";
295
- let reasoning = "";
296
- const toolCallMap = /* @__PURE__ */ new Map();
316
+ const request = this.createRequest(options?.signal);
297
317
  try {
298
- while (true) {
299
- const { done, value } = await reader.read();
300
- if (done) break;
301
- buffer += decoder.decode(value, { stream: true });
302
- const lines = buffer.split("\n");
303
- buffer = lines.pop() ?? "";
304
- for (const line of lines) {
305
- const trimmed = line.trim();
306
- if (!trimmed || !trimmed.startsWith("data:")) continue;
307
- const data = trimmed.slice(5).trim();
308
- if (data === "[DONE]") continue;
309
- try {
310
- const chunk = JSON.parse(data);
311
- const delta = chunk.choices?.[0]?.delta;
312
- if (!delta) continue;
313
- if (delta.content) {
314
- content += delta.content;
315
- yield { type: "llm_text", content: delta.content };
316
- }
317
- if (delta.reasoning_content) {
318
- reasoning += delta.reasoning_content;
319
- yield { type: "llm_thinking", content: delta.reasoning_content };
318
+ const body = {
319
+ model: this.config.model,
320
+ messages: this.messagesToOpenAI(messages),
321
+ stream: true,
322
+ ...this.config.collectPromptCacheUsage && { stream_options: { include_usage: true } }
323
+ };
324
+ if (tools.length > 0) {
325
+ body.tools = this.toolsToOpenAI(tools);
326
+ }
327
+ if (this.config.temperature !== void 0) {
328
+ body.temperature = this.config.temperature;
329
+ }
330
+ if (this.config.maxTokens !== void 0) {
331
+ body.max_tokens = this.config.maxTokens;
332
+ }
333
+ if (this.config.extraBody) {
334
+ Object.assign(body, this.config.extraBody);
335
+ }
336
+ const res = await this.fetchResponse({
337
+ method: "POST",
338
+ headers: this.buildHeaders(),
339
+ body: JSON.stringify(body),
340
+ signal: request.controller.signal
341
+ });
342
+ if (!res.ok) {
343
+ const text = await res.text().catch(() => "");
344
+ this.checkContextOverflow(res.status, text);
345
+ throw new Error(`LLM request failed: ${res.status} ${text}`);
346
+ }
347
+ const reader = res.body?.getReader();
348
+ if (!reader) {
349
+ const data = await res.json();
350
+ const parsed = this.parseResponse(data);
351
+ this.verified = true;
352
+ if (parsed.content) {
353
+ yield { type: "llm_text", content: parsed.content };
354
+ }
355
+ yield { type: "llm_response", response: parsed };
356
+ return;
357
+ }
358
+ const decoder = new TextDecoder();
359
+ let buffer = "";
360
+ let content = "";
361
+ let reasoning = "";
362
+ let finishReason;
363
+ let promptCacheUsage;
364
+ let sawDone = false;
365
+ const toolCallMap = /* @__PURE__ */ new Map();
366
+ try {
367
+ while (true) {
368
+ if (options?.signal?.aborted) {
369
+ this.throwIfAborted(options.signal);
370
+ }
371
+ const { done, value } = await this.readChunk(reader, request.controller);
372
+ if (done) {
373
+ buffer += decoder.decode();
374
+ if (!buffer.trim()) break;
375
+ buffer += "\n";
376
+ }
377
+ buffer += decoder.decode(value, { stream: true });
378
+ const lines = buffer.split("\n");
379
+ buffer = lines.pop() ?? "";
380
+ for (const line of lines) {
381
+ const trimmed = line.trim();
382
+ if (!trimmed || !trimmed.startsWith("data:")) continue;
383
+ const data = trimmed.slice(5).trim();
384
+ if (data === "[DONE]") {
385
+ sawDone = true;
386
+ continue;
320
387
  }
321
- if (delta.tool_calls) {
322
- for (const tc of delta.tool_calls) {
323
- const idx = tc.index ?? 0;
324
- if (!toolCallMap.has(idx)) {
325
- toolCallMap.set(idx, {
326
- id: tc.id ?? "",
327
- name: tc.function?.name ?? "",
328
- arguments: ""
329
- });
388
+ try {
389
+ const chunk = JSON.parse(data);
390
+ if (chunk.error) throw new Error(chunk.error.message ?? "LLM stream error");
391
+ if (this.config.collectPromptCacheUsage && chunk.usage) {
392
+ promptCacheUsage = this.parsePromptCacheUsage(chunk.usage) ?? promptCacheUsage;
393
+ }
394
+ if (chunk.choices?.[0]?.finish_reason) finishReason = chunk.choices[0].finish_reason;
395
+ const delta = chunk.choices?.[0]?.delta;
396
+ if (!delta) continue;
397
+ if (delta.content) {
398
+ content += delta.content;
399
+ yield { type: "llm_text", content: delta.content };
400
+ }
401
+ if (delta.reasoning_content) {
402
+ reasoning += delta.reasoning_content;
403
+ yield { type: "llm_thinking", content: delta.reasoning_content };
404
+ }
405
+ if (delta.tool_calls) {
406
+ for (const tc of delta.tool_calls) {
407
+ const idx = tc.index ?? 0;
408
+ if (!toolCallMap.has(idx)) {
409
+ toolCallMap.set(idx, {
410
+ id: tc.id ?? "",
411
+ name: tc.function?.name ?? "",
412
+ arguments: ""
413
+ });
414
+ }
415
+ const entry = toolCallMap.get(idx);
416
+ if (tc.id) entry.id = tc.id;
417
+ if (tc.function?.name) entry.name = tc.function.name;
418
+ if (tc.function?.arguments) entry.arguments += tc.function.arguments;
330
419
  }
331
- const entry = toolCallMap.get(idx);
332
- if (tc.id) entry.id = tc.id;
333
- if (tc.function?.name) entry.name = tc.function.name;
334
- if (tc.function?.arguments) entry.arguments += tc.function.arguments;
335
420
  }
421
+ } catch (error) {
422
+ throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, `Invalid LLM stream: ${errorMessage(error)}`);
423
+ }
424
+ if (options?.signal?.aborted) {
425
+ this.throwIfAborted(options.signal);
336
426
  }
337
- } catch {
338
427
  }
428
+ if (done || sawDone) break;
339
429
  }
430
+ } finally {
431
+ await reader.cancel().catch(() => {
432
+ });
433
+ reader.releaseLock();
340
434
  }
435
+ const toolCalls = [];
436
+ for (const [_, tc] of toolCallMap) {
437
+ toolCalls.push(this.parseToolCall(tc));
438
+ }
439
+ throwIfAborted(request.controller.signal);
440
+ if (this.config.strictStream && !sawDone && !finishReason) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "LLM stream ended without a finish marker");
441
+ if (finishReason === "length") for (const call of toolCalls) call.parseError = "Tool response exceeded the output token limit";
442
+ if (content || toolCalls.length) this.verified = true;
443
+ yield {
444
+ type: "llm_response",
445
+ response: {
446
+ content,
447
+ reasoning: reasoning || void 0,
448
+ toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
449
+ usage: void 0,
450
+ ...promptCacheUsage && { promptCacheUsage },
451
+ finishReason: finishReason ?? (toolCalls.length > 0 ? "tool_calls" : "stop")
452
+ }
453
+ };
454
+ } catch (error) {
455
+ if (request.controller.signal.aborted && !options?.signal?.aborted) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "LLM request timed out", { reason: "timeout" });
456
+ throw error;
341
457
  } finally {
342
- reader.releaseLock();
458
+ request.cleanup();
343
459
  }
344
- const toolCalls = [];
345
- for (const [_, tc] of toolCallMap) {
346
- let args = {};
347
- try {
348
- args = JSON.parse(tc.arguments || "{}");
349
- } catch {
350
- args = {};
351
- }
352
- toolCalls.push({ id: tc.id, name: tc.name, arguments: args });
460
+ }
461
+ parseToolCall(tc) {
462
+ if (!tc.id || !tc.name) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "Tool call is missing its id or name");
463
+ try {
464
+ const args = JSON.parse(tc.arguments || "{}");
465
+ if (!args || typeof args !== "object" || Array.isArray(args)) throw new Error("Tool arguments must be a JSON object");
466
+ return { id: tc.id, name: tc.name, arguments: args };
467
+ } catch (error) {
468
+ return { id: tc.id, name: tc.name, arguments: {}, parseError: errorMessage(error) };
469
+ }
470
+ }
471
+ createRequest(parent) {
472
+ const controller = new AbortController();
473
+ const unlink = linkAbort(parent, controller);
474
+ const timeout = this.config.requestTimeoutMs;
475
+ const timer = timeout && timeout > 0 ? setTimeout(() => controller.abort(), timeout) : void 0;
476
+ return { controller, cleanup: () => {
477
+ clearTimeout(timer);
478
+ unlink();
479
+ controller.abort();
480
+ } };
481
+ }
482
+ async fetchResponse(init) {
483
+ const retries = Math.max(0, Math.min(this.config.retry?.maxRetries ?? 0, 5));
484
+ const requestInit = this.options?.rejectRedirects ? { ...init, redirect: "error" } : init;
485
+ for (let attempt = 0; ; attempt++) {
486
+ throwIfAborted(init.signal ?? void 0);
487
+ const response = await fetch(this.getEndpoint(), requestInit);
488
+ if (attempt >= retries || ![429, 502, 503, 504].includes(response.status)) return response;
489
+ const retryAfter = Number(response.headers.get("retry-after"));
490
+ const delay = Number.isFinite(retryAfter) && retryAfter > 0 ? Math.min(retryAfter * 1e3, 3e4) : Math.min((this.config.retry?.baseDelayMs ?? 500) * 2 ** attempt, 3e4);
491
+ await response.body?.cancel();
492
+ await abortableDelay(delay, init.signal ?? void 0);
493
+ }
494
+ }
495
+ async readChunk(reader, controller) {
496
+ const idleMs = this.config.streamIdleTimeoutMs;
497
+ if (!idleMs || idleMs <= 0) return reader.read();
498
+ let timer;
499
+ try {
500
+ return await Promise.race([reader.read(), new Promise((_, reject) => {
501
+ timer = setTimeout(() => {
502
+ reject(new Error("LLM stream idle timeout"));
503
+ controller.abort();
504
+ }, idleMs);
505
+ })]);
506
+ } finally {
507
+ clearTimeout(timer);
353
508
  }
354
- yield {
355
- type: "llm_response",
356
- response: {
357
- content,
358
- reasoning: reasoning || void 0,
359
- toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
360
- usage: void 0,
361
- finishReason: toolCalls.length > 0 ? "tool_calls" : "stop"
362
- }
363
- };
364
509
  }
365
510
  /**
366
511
  * Check if the error response indicates context length exceeded.
@@ -378,6 +523,14 @@ var OpenAIAdapter = class {
378
523
  throw new ACPError(ACPErrorCodes.CONTEXT_OVERFLOW, errMsg, { isContextOverflow: true });
379
524
  }
380
525
  }
526
+ /** AbortError for the given signal — used so a cancelled stream surfaces an abort, not a parse error. */
527
+ throwIfAborted(signal) {
528
+ if (signal?.aborted) {
529
+ const err = new Error("Request aborted");
530
+ err.name = "AbortError";
531
+ throw err;
532
+ }
533
+ }
381
534
  parseResponse(data) {
382
535
  const choice = data.choices?.[0];
383
536
  if (!choice) {
@@ -389,17 +542,9 @@ var OpenAIAdapter = class {
389
542
  const toolCalls = [];
390
543
  if (message?.tool_calls?.length) {
391
544
  for (const tc of message.tool_calls) {
392
- let args = {};
393
- try {
394
- args = JSON.parse(tc.function.arguments || "{}");
395
- } catch {
396
- args = {};
397
- }
398
- toolCalls.push({
399
- id: tc.id,
400
- name: tc.function.name,
401
- arguments: args
402
- });
545
+ const parsed = this.parseToolCall({ id: tc.id, name: tc.function?.name, arguments: tc.function?.arguments });
546
+ if (choice.finish_reason === "length") parsed.parseError = "Tool response exceeded the output token limit";
547
+ toolCalls.push(parsed);
403
548
  }
404
549
  }
405
550
  if (!content && !toolCalls.length) {
@@ -408,16 +553,70 @@ var OpenAIAdapter = class {
408
553
  "LLM returned empty response with no tool calls"
409
554
  );
410
555
  }
556
+ const promptCacheUsage = this.config.collectPromptCacheUsage ? this.parsePromptCacheUsage(data.usage) : void 0;
411
557
  return {
412
558
  content,
413
559
  reasoning,
414
560
  toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
415
561
  usage: data.usage ? { input: data.usage.prompt_tokens, output: data.usage.completion_tokens } : void 0,
562
+ ...promptCacheUsage && { promptCacheUsage },
416
563
  finishReason: choice.finish_reason
417
564
  };
418
565
  }
566
+ parsePromptCacheUsage(raw) {
567
+ const asCount = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : void 0;
568
+ const cachedTokens = asCount(raw?.prompt_tokens_details?.cached_tokens) ?? asCount(raw?.prompt_cache_hit_tokens);
569
+ const cacheWriteTokens = asCount(raw?.prompt_tokens_details?.cache_write_tokens);
570
+ if (cachedTokens === void 0 && cacheWriteTokens === void 0) return void 0;
571
+ const inputTokens = asCount(raw?.prompt_tokens);
572
+ return {
573
+ ...inputTokens !== void 0 && { inputTokens },
574
+ ...cachedTokens !== void 0 && { cachedTokens },
575
+ ...cacheWriteTokens !== void 0 && { cacheWriteTokens }
576
+ };
577
+ }
419
578
  };
420
579
 
580
+ // src/llm/proxy.ts
581
+ function invalidProxyConfig(message) {
582
+ return new ACPError(ACPErrorCodes.LLM_PROXY_CONFIG_INVALID, message);
583
+ }
584
+ function proxyBaseUrl(value, field) {
585
+ const input = typeof value === "string" ? value.trim() : "";
586
+ const absolute = /^https?:\/\//i.test(input);
587
+ const relative = input.startsWith("/") && !input.startsWith("//");
588
+ if (!absolute && !relative || /[\\?#\u0000-\u001f]/.test(input)) {
589
+ throw invalidProxyConfig(`${field} must be an HTTP(S) base URL or a same-origin path without a query or fragment`);
590
+ }
591
+ let url;
592
+ try {
593
+ url = new URL(input, "http://listdoing.invalid");
594
+ } catch {
595
+ throw invalidProxyConfig(`${field} is not a valid proxy URL`);
596
+ }
597
+ if (url.username || url.password) throw invalidProxyConfig(`${field} must not include credentials`);
598
+ if (!absolute && url.pathname.startsWith("//")) throw invalidProxyConfig(`${field} must stay on the same origin`);
599
+ const normalized = (absolute ? url.href : url.pathname).replace(/\/+$/, "");
600
+ return normalized || "/";
601
+ }
602
+ function resolveProxyConfig(config) {
603
+ const mode = config.llmProxyMode;
604
+ if (mode === void 0) return { llm: config.llm, serverUrl: config.serverUrl };
605
+ if (mode !== "backend" && mode !== "listdoing-server") {
606
+ throw invalidProxyConfig("llmProxyMode must be backend or listdoing-server");
607
+ }
608
+ const serverUrl = config.serverUrl !== void 0 || mode === "listdoing-server" ? proxyBaseUrl(config.serverUrl, "serverUrl") : void 0;
609
+ const baseUrl = mode === "listdoing-server" ? `${serverUrl.replace(/\/+$/, "")}/v1` : proxyBaseUrl(config.llm.baseUrl, "llm.baseUrl");
610
+ if (mode === "listdoing-server" && config.llm.baseUrl !== void 0 && proxyBaseUrl(config.llm.baseUrl, "llm.baseUrl") !== baseUrl) {
611
+ throw invalidProxyConfig("llm.baseUrl must match the ListDoing Server proxy or be omitted");
612
+ }
613
+ if (baseUrl.endsWith("/chat/completions")) {
614
+ throw invalidProxyConfig("llm.baseUrl must end before /chat/completions");
615
+ }
616
+ const apiKey = config.llm.apiKey ?? (mode === "listdoing-server" ? config.authToken : void 0);
617
+ return { llm: { ...config.llm, baseUrl, apiKey }, serverUrl };
618
+ }
619
+
421
620
  // src/skill/registry.ts
422
621
  var SkillRegistry = class {
423
622
  constructor() {
@@ -438,6 +637,11 @@ var SkillRegistry = class {
438
637
  get(name) {
439
638
  return this.skills.get(name)?.definition;
440
639
  }
640
+ /** Identity of this registration, replaced on every register call. */
641
+ getRegistrationIdentity(skill) {
642
+ const entry = this.skills.get(skill.name);
643
+ return entry?.definition === skill ? entry : skill;
644
+ }
441
645
  getHandler(name) {
442
646
  return this.skills.get(name)?.handler;
443
647
  }
@@ -492,7 +696,9 @@ var SkillRegistry = class {
492
696
 
493
697
  // src/skill/executor.ts
494
698
  var SkillExecutor = class {
495
- constructor(registry, worker) {
699
+ constructor(registry, worker, options) {
700
+ this.options = options;
701
+ this.validators = /* @__PURE__ */ new WeakMap();
496
702
  this.registry = registry;
497
703
  this.worker = worker ?? {
498
704
  async execute(def, params, ctx) {
@@ -520,19 +726,41 @@ var SkillExecutor = class {
520
726
  throw new ACPError(ACPErrorCodes.SKILL_NOT_FOUND, `Skill not found: ${name}`, { name });
521
727
  }
522
728
  const timeout = def.timeout ?? 3e4;
729
+ throwIfAborted(context.signal);
730
+ if (this.options?.validateInputs) {
731
+ const validate = await this.getValidator(def);
732
+ if (!validate(params)) throw new ACPError(ACPErrorCodes.SKILL_EXECUTION_FAILED, `Invalid arguments for ${name}`, { errors: validate.errors });
733
+ }
523
734
  const controller = new AbortController();
524
- const onParentAbort = () => controller.abort();
525
- context.signal.addEventListener("abort", onParentAbort);
526
- const timeoutId = timeout > 0 ? setTimeout(() => controller.abort(), timeout) : null;
735
+ const unlink = linkAbort(context.signal, controller);
736
+ let timedOut = false;
737
+ const timeoutId = timeout > 0 ? setTimeout(() => {
738
+ timedOut = true;
739
+ controller.abort();
740
+ }, timeout) : null;
527
741
  const execContext = {
528
742
  ...context,
529
743
  signal: controller.signal
530
744
  };
745
+ let removeDeadline = () => {
746
+ };
531
747
  try {
532
- const result = await this.worker.execute(def, params, execContext);
748
+ const execution = Promise.resolve().then(() => {
749
+ throwIfAborted(controller.signal);
750
+ return this.worker.execute(def, params, execContext);
751
+ });
752
+ const result = def.timeoutMode === "deadline" ? await Promise.race([execution, new Promise((_, reject) => {
753
+ const abort = () => reject(abortError());
754
+ controller.signal.addEventListener("abort", abort, { once: true });
755
+ removeDeadline = () => controller.signal.removeEventListener("abort", abort);
756
+ })]) : await execution;
757
+ throwIfAborted(context.signal);
758
+ if (timedOut) throw new Error("Skill exceeded its timeout");
759
+ if (!result || !Array.isArray(result.content)) throw new Error("Skill returned an invalid result (content must be an array)");
533
760
  return result;
534
761
  } catch (err) {
535
- if (err.name === "AbortError" || controller.signal.aborted) {
762
+ if (context.signal.aborted) throw abortError();
763
+ if (timedOut) {
536
764
  throw new ACPError(
537
765
  ACPErrorCodes.SKILL_EXECUTION_TIMEOUT,
538
766
  `Skill execution timeout: ${name}`,
@@ -542,19 +770,44 @@ var SkillExecutor = class {
542
770
  if (err instanceof ACPError) throw err;
543
771
  throw new ACPError(
544
772
  ACPErrorCodes.SKILL_EXECUTION_FAILED,
545
- `Skill execution failed: ${name}: ${err.message}`,
546
- { name, error: err.message }
773
+ `Skill execution failed: ${name}: ${errorMessage(err)}`,
774
+ { name, error: errorMessage(err) }
547
775
  );
548
776
  } finally {
549
777
  if (timeoutId) clearTimeout(timeoutId);
550
- context.signal.removeEventListener("abort", onParentAbort);
778
+ unlink();
779
+ removeDeadline();
780
+ }
781
+ }
782
+ async getValidator(skill) {
783
+ const { default: Ajv } = await import("ajv");
784
+ const registration = this.registry.getRegistrationIdentity(skill);
785
+ const schema = skill.inputSchema;
786
+ let signature;
787
+ try {
788
+ signature = JSON.stringify(schema, function(key, value) {
789
+ if (value !== this[key]) throw new TypeError("Schema cannot be cached");
790
+ if (value === null || typeof value === "string" || typeof value === "boolean" || Number.isFinite(value)) return value;
791
+ if (typeof value === "object") {
792
+ const prototype = Object.getPrototypeOf(value);
793
+ if (prototype === Object.prototype || prototype === null || prototype === Array.prototype) return value;
794
+ }
795
+ throw new TypeError("Schema cannot be cached");
796
+ });
797
+ } catch {
551
798
  }
799
+ const cached = this.validators.get(registration);
800
+ if (cached && cached.schema === signature) return cached.validate;
801
+ const validate = new Ajv({ strict: false, allErrors: true }).compile(schema);
802
+ if (signature !== void 0) this.validators.set(registration, { schema: signature, validate });
803
+ return validate;
552
804
  }
553
805
  /**
554
806
  * Directly invoke a skill by name — used by ctx.invoke().
555
807
  * No LLM loop, no approval. Shares parent's signal and timeout context.
556
808
  */
557
809
  async invokeDirect(name, params, context) {
810
+ throwIfAborted(context.signal);
558
811
  const def = this.registry.get(name);
559
812
  if (!def) {
560
813
  return {
@@ -578,8 +831,9 @@ var SkillExecutor = class {
578
831
  try {
579
832
  return await handler(params, context);
580
833
  } catch (err) {
834
+ if (context.signal.aborted || err?.name === "AbortError") throw abortError();
581
835
  return {
582
- content: [{ type: "text", text: `Invoke failed: ${err.message}` }],
836
+ content: [{ type: "text", text: `Invoke failed: ${errorMessage(err)}` }],
583
837
  isError: true
584
838
  };
585
839
  }
@@ -755,13 +1009,13 @@ var McpRouter = class {
755
1009
  /**
756
1010
  * Route a tool call to the appropriate MCP Server.
757
1011
  */
758
- async call(prefixedName, args) {
1012
+ async call(prefixedName, args, options) {
759
1013
  const { server, tool } = this.parseMcpToolName(prefixedName);
760
1014
  const conn = this.connections.get(server);
761
1015
  if (!conn) {
762
1016
  throw new Error(`MCP Server "${server}" not connected. Available: ${Array.from(this.connections.keys()).join(", ") || "none"}`);
763
1017
  }
764
- return conn.call(tool, args);
1018
+ return conn.call(tool, args, options);
765
1019
  }
766
1020
  /**
767
1021
  * Resolve `mcp:` prefixed tool names from a tools list to ToolDef[].
@@ -834,7 +1088,7 @@ function fileInputsToContentBlocks(files) {
834
1088
  }
835
1089
 
836
1090
  // src/session/manager.ts
837
- var SessionManager = class {
1091
+ var SessionManager = class _SessionManager {
838
1092
  constructor() {
839
1093
  this.sessions = /* @__PURE__ */ new Map();
840
1094
  }
@@ -860,6 +1114,9 @@ var SessionManager = class {
860
1114
  get(sessionId) {
861
1115
  return this.sessions.get(sessionId);
862
1116
  }
1117
+ restore(session) {
1118
+ this.sessions.set(session.id, session);
1119
+ }
863
1120
  getOrThrow(sessionId) {
864
1121
  const session = this.sessions.get(sessionId);
865
1122
  if (!session) {
@@ -921,13 +1178,16 @@ var SessionManager = class {
921
1178
  session.status = "archived";
922
1179
  session.updatedAt = Date.now();
923
1180
  }
924
- merge(fromId, toId, strategy = "summary_and_artifacts") {
1181
+ merge(fromId, toId, strategy) {
925
1182
  const from = this.getOrThrow(fromId);
926
1183
  const to = this.getOrThrow(toId);
1184
+ _SessionManager.mergeContent(from, to, strategy);
1185
+ }
1186
+ static mergeContent(from, to, strategy = "summary_and_artifacts") {
927
1187
  if (strategy === "full") {
928
1188
  to.messages.push(...from.messages);
929
1189
  } else {
930
- const summary = this.createSummary(from);
1190
+ const summary = _SessionManager.createSummary(from);
931
1191
  to.messages.push({
932
1192
  role: "user",
933
1193
  content: `[Context from session "${from.title || from.id}"]:
@@ -949,7 +1209,7 @@ ${artifacts.map((a) => contentToPreview(a.content, 200)).join("\n")}`
949
1209
  to.messageCount = to.messages.length;
950
1210
  to.updatedAt = Date.now();
951
1211
  }
952
- createSummary(session) {
1212
+ static createSummary(session) {
953
1213
  const userMsgs = session.messages.filter((m) => m.role === "user");
954
1214
  if (userMsgs.length === 0) return "Empty session";
955
1215
  return userMsgs.slice(-3).map((m) => contentToPreview(m.content, 200)).join("\n");
@@ -1076,6 +1336,64 @@ function estimateRequestTokens(messages, tools, systemPrompt) {
1076
1336
  return total;
1077
1337
  }
1078
1338
 
1339
+ // src/context/messages.ts
1340
+ function repairMessages(messages) {
1341
+ const result = [];
1342
+ for (let i = 0; i < messages.length; i++) {
1343
+ const message = messages[i];
1344
+ if (!message || !["user", "assistant", "system", "tool"].includes(message.role)) continue;
1345
+ if (message.role === "tool") continue;
1346
+ if (message.role === "assistant" && message.toolCalls?.length) {
1347
+ const tools = [];
1348
+ while (i + 1 < messages.length && messages[i + 1]?.role === "tool") tools.push(messages[++i]);
1349
+ const ids = /* @__PURE__ */ new Set();
1350
+ const calls = message.toolCalls.filter((call) => {
1351
+ if (!call?.id || !call.name || ids.has(call.id)) return false;
1352
+ ids.add(call.id);
1353
+ return true;
1354
+ });
1355
+ if (calls.length) {
1356
+ result.push({ ...message, toolCalls: calls });
1357
+ for (const call of calls) {
1358
+ const tool = tools.find((tool2) => tool2.toolCallId === call.id);
1359
+ result.push(tool ? normalizeContent(tool) : {
1360
+ role: "tool",
1361
+ toolCallId: call.id,
1362
+ content: "Tool execution was interrupted; its outcome is unknown. Do not repeat a side-effecting operation without checking its state."
1363
+ });
1364
+ }
1365
+ } else if (message.content) result.push(normalizeContent({ ...message, toolCalls: void 0 }));
1366
+ } else result.push(normalizeContent(message));
1367
+ }
1368
+ return result;
1369
+ }
1370
+ function normalizeContent(message) {
1371
+ const content = message.content;
1372
+ if (content == null || content === "" || Array.isArray(content) && content.length === 0) {
1373
+ return { ...message, content: "." };
1374
+ }
1375
+ if (typeof content !== "string") {
1376
+ if (!Array.isArray(content)) return { ...message, content: "." };
1377
+ const valid = content.filter((block) => {
1378
+ if (!block || typeof block !== "object") return false;
1379
+ if (block.type === "text") return typeof block.text === "string";
1380
+ if (block.type === "image") return typeof block.data === "string" && !!block.data && typeof block.mimeType === "string";
1381
+ if (block.type === "resource") return typeof block.resource?.uri === "string" && (block.resource.text === void 0 || typeof block.resource.text === "string");
1382
+ return false;
1383
+ });
1384
+ if (valid.length !== content.length) return { ...message, content: valid.length ? valid : "." };
1385
+ }
1386
+ return message;
1387
+ }
1388
+ function messageGroups(messages) {
1389
+ const groups = [];
1390
+ for (const message of repairMessages(messages)) {
1391
+ if (message.role === "tool" && groups.length) groups[groups.length - 1].push(message);
1392
+ else groups.push([message]);
1393
+ }
1394
+ return groups;
1395
+ }
1396
+
1079
1397
  // src/context/manager.ts
1080
1398
  var ContextManager = class {
1081
1399
  constructor(config, model) {
@@ -1093,6 +1411,10 @@ var ContextManager = class {
1093
1411
  setModel(model) {
1094
1412
  this.model = model;
1095
1413
  }
1414
+ /** Inject an LLM-based summarizer. When set, auto-compression uses semantic summarization. */
1415
+ setSummarizer(fn) {
1416
+ this.summarizer = fn;
1417
+ }
1096
1418
  /** Compute the token budget for the current model & config */
1097
1419
  getBudget() {
1098
1420
  const ctxLen = resolveModelContextLength(this.model, this.config.contextLength);
@@ -1124,59 +1446,113 @@ var ContextManager = class {
1124
1446
  if (systemPrompt) {
1125
1447
  result.push({ role: "system", content: systemPrompt });
1126
1448
  }
1127
- const sanitized = messages.map((m) => {
1128
- if (m.role === "assistant" && !m.content && !m.toolCalls?.length) {
1129
- return { ...m, content: "." };
1130
- }
1131
- return m;
1132
- });
1133
- const reversed = [...sanitized].reverse();
1134
1449
  const selected = [];
1135
1450
  let usedByMessages = 0;
1136
- for (const msg of reversed) {
1137
- const msgTokens = estimateMessageTokens(msg);
1138
- if (usedByMessages + msgTokens > remainingBudget) break;
1139
- selected.unshift(msg);
1140
- usedByMessages += msgTokens;
1451
+ for (const group of messageGroups(messages).reverse()) {
1452
+ const tokens = group.reduce((sum, msg) => sum + estimateMessageTokens(msg), 0);
1453
+ if (usedByMessages + tokens > remainingBudget) break;
1454
+ selected.unshift(...group);
1455
+ usedByMessages += tokens;
1141
1456
  }
1142
1457
  result.push(...selected);
1143
1458
  if (selected.length === 0 && messages.length > 0) {
1144
1459
  const lastUser = [...messages].reverse().find((m) => m.role === "user");
1145
1460
  if (lastUser) {
1146
- result.push(lastUser);
1461
+ const fallback = repairMessages([lastUser]);
1462
+ result.push(...fallback);
1463
+ usedByMessages += fallback.reduce((sum, message) => sum + estimateMessageTokens(message), 0);
1147
1464
  }
1148
1465
  }
1149
1466
  return { messages: result, usedTokens: fixedTokens + usedByMessages, budget };
1150
1467
  }
1151
1468
  /**
1152
1469
  * Ensure context fits within budget. Triggers auto-compression when
1153
- * usage exceeds the configured threshold.
1470
+ * usage exceeds the configured threshold. Prefers LLM-based semantic
1471
+ * summarization (when a summarizer is set and enabled) over mechanical truncation.
1154
1472
  */
1155
- ensureContextFits(messages, systemPrompt, tools, model) {
1473
+ async ensureContextFits(messages, systemPrompt, tools, model, signal) {
1474
+ throwIfAborted(signal);
1156
1475
  if (model) this.setModel(model);
1157
- const { usedTokens, budget } = this.buildWindow(messages, systemPrompt, tools);
1476
+ const { budget } = this.buildWindow(messages, systemPrompt, tools);
1477
+ const beforeTokens = estimateRequestTokens(repairMessages(messages), tools, systemPrompt);
1478
+ if (this.config.autoCompress === false) return { messages, wasCompressed: false };
1158
1479
  const threshold = this.config.compressThreshold ?? 0.85;
1159
- if (usedTokens / budget <= threshold) {
1480
+ if (beforeTokens / budget <= threshold) {
1160
1481
  return { messages, wasCompressed: false };
1161
1482
  }
1162
- const compressed = this.compress(messages, "hybrid");
1163
- const afterCheck = this.buildWindow(compressed.messages, systemPrompt, tools);
1164
- if (afterCheck.usedTokens / afterCheck.budget > 1) {
1165
- console.warn(`[listdoing] Context still exceeds budget after compression (${afterCheck.usedTokens}/${afterCheck.budget} tokens). Truncating to recent messages only.`);
1166
- const truncated = messages.slice(-3);
1167
- return { messages: truncated, wasCompressed: true };
1483
+ const useLLM = this.config.useLLMSummary !== false && !!this.summarizer;
1484
+ let result;
1485
+ if (useLLM) {
1486
+ const llmResult = await this.compressWithLLM(messages, systemPrompt, tools, budget, { signal, groupTools: true });
1487
+ result = llmResult ?? this.compressSync(messages, "hybrid", { groupTools: true });
1488
+ } else {
1489
+ result = this.compressSync(messages, "hybrid", { groupTools: true });
1490
+ }
1491
+ let compressed = result.messages;
1492
+ let window2 = this.buildWindow(compressed, systemPrompt, tools);
1493
+ if (window2.usedTokens > budget) {
1494
+ console.warn(`[listdoing] Context still exceeds budget after compression (${window2.usedTokens}/${budget} tokens). Truncating to recent messages only.`);
1495
+ compressed = messageGroups(messages).slice(-3).flat();
1496
+ window2 = this.buildWindow(compressed, systemPrompt, tools);
1497
+ }
1498
+ if (estimateRequestTokens(compressed, tools, systemPrompt) >= beforeTokens || window2.usedTokens >= beforeTokens) {
1499
+ return { messages, wasCompressed: false };
1168
1500
  }
1169
- return { messages: compressed.messages, wasCompressed: true };
1501
+ return {
1502
+ messages: compressed,
1503
+ wasCompressed: true,
1504
+ beforeTokens,
1505
+ afterTokens: window2.usedTokens
1506
+ };
1507
+ }
1508
+ /** Build one smaller request after a provider rejects the current window. */
1509
+ async compressForOverflow(messages, rejected, systemPrompt, tools, signal) {
1510
+ throwIfAborted(signal);
1511
+ const fixedTokens = estimateRequestTokens([], tools, systemPrompt);
1512
+ const available = Math.min(rejected.budget, rejected.usedTokens) - fixedTokens;
1513
+ const historyBudget = Math.floor(available * 0.85);
1514
+ if (historyBudget <= 0) return;
1515
+ const budget = fixedTokens + historyBudget;
1516
+ const latestUser = [...messages].reverse().find((message) => message.role === "user");
1517
+ if (latestUser && estimateMessageTokens(latestUser) > historyBudget) return;
1518
+ const rejectedUserIndex = latestUser ? rejected.messages.lastIndexOf(latestUser) : -1;
1519
+ const activeToolResults = rejected.messages.slice(rejectedUserIndex + 1).filter((message) => message.role === "tool");
1520
+ const summarized = this.config.useLLMSummary !== false ? await this.compressWithLLM(messages, systemPrompt, tools, budget, { signal, groupTools: true }) : null;
1521
+ throwIfAborted(signal);
1522
+ const useCandidate = (result) => {
1523
+ const window2 = this.buildWindow(result.messages, systemPrompt, tools, budget);
1524
+ if (window2.usedTokens > budget || window2.usedTokens >= rejected.usedTokens) return;
1525
+ if (latestUser && !window2.messages.some((message) => message.role === "user" && message.content === latestUser.content)) return;
1526
+ const keptToolIds = new Set(window2.messages.filter((message) => message.role === "tool").map((message) => message.toolCallId));
1527
+ if (activeToolResults.some((message) => !keptToolIds.has(message.toolCallId))) return;
1528
+ return { ...result, window: window2 };
1529
+ };
1530
+ const candidate = summarized && useCandidate(summarized);
1531
+ return candidate || useCandidate(this.compressSync(messages, "hybrid", { maxTokens: historyBudget, groupTools: true }));
1170
1532
  }
1171
1533
  /**
1172
- * Compress context by summarizing older messages.
1534
+ * Compress context by summarizing older messages. When strategy is
1535
+ * 'llm_summary' (or resolved via config) it uses the injected summarizer,
1536
+ * falling back to mechanical 'hybrid' truncation on failure.
1173
1537
  */
1174
- compress(messages, strategy) {
1538
+ async compress(messages, strategy) {
1539
+ const strat = strategy ?? this.config.historyStrategy?.type ?? "hybrid";
1540
+ if (strat === "llm_summary") {
1541
+ if (!this.summarizer) {
1542
+ return this.compressSync(messages, "hybrid");
1543
+ }
1544
+ const llmResult = await this.compressWithLLM(messages, void 0, [], this.getBudget());
1545
+ return llmResult ?? this.compressSync(messages, "hybrid");
1546
+ }
1547
+ return this.compressSync(messages, strat);
1548
+ }
1549
+ /** Mechanical (non-LLM) compression — the original truncation strategies. */
1550
+ compressSync(messages, strategy, options) {
1551
+ messages = repairMessages(messages);
1175
1552
  const estimateTokens = (msgs) => msgs.reduce((sum, m) => sum + estimateMessageTokens(m), 0);
1176
1553
  const beforeTokens = estimateTokens(messages);
1177
- const strat = strategy ?? this.config.historyStrategy?.type ?? "hybrid";
1178
1554
  let compressed;
1179
- switch (strat) {
1555
+ switch (strategy) {
1180
1556
  case "sliding_window": {
1181
1557
  compressed = messages.slice(-10);
1182
1558
  break;
@@ -1199,13 +1575,14 @@ ${summaryText}` },
1199
1575
  case "token_budget":
1200
1576
  case "hybrid":
1201
1577
  default: {
1202
- const maxTokens = this.config.maxTokens ?? this.getBudget();
1578
+ const maxTokens = options?.maxTokens ?? this.config.maxTokens ?? this.getBudget();
1203
1579
  let totalTokens = 0;
1204
1580
  const kept = [];
1205
- for (const msg of [...messages].reverse()) {
1206
- const t = estimateMessageTokens(msg);
1581
+ const groups = options?.groupTools ? messageGroups(messages) : messages.map((message) => [message]);
1582
+ for (const group of groups.reverse()) {
1583
+ const t = estimateTokens(group);
1207
1584
  if (totalTokens + t > maxTokens) break;
1208
- kept.unshift(msg);
1585
+ kept.unshift(...group);
1209
1586
  totalTokens += t;
1210
1587
  }
1211
1588
  if (kept.length < messages.length) {
@@ -1222,8 +1599,55 @@ ${summaryText}` },
1222
1599
  break;
1223
1600
  }
1224
1601
  }
1225
- const afterTokens = estimateTokens(compressed);
1226
- return { messages: compressed, beforeTokens, afterTokens };
1602
+ const repaired = repairMessages(compressed);
1603
+ return { messages: repaired, beforeTokens, afterTokens: estimateTokens(repaired) };
1604
+ }
1605
+ /**
1606
+ * LLM-based semantic summarization. Returns null when it cannot be performed
1607
+ * (no summarizer, nothing to summarize, or the LLM call fails) so callers can
1608
+ * fall back to mechanical compression.
1609
+ */
1610
+ async compressWithLLM(messages, systemPrompt, tools, budget, options) {
1611
+ if (!this.summarizer) return null;
1612
+ const { signal, groupTools } = options ?? {};
1613
+ messages = repairMessages(messages);
1614
+ try {
1615
+ const estimateTokens = (msgs) => msgs.reduce((sum, m) => sum + estimateMessageTokens(m), 0);
1616
+ const beforeTokens = estimateTokens(messages);
1617
+ let fixedTokens = 0;
1618
+ if (systemPrompt) fixedTokens += estimateTextTokens(systemPrompt) + 4;
1619
+ for (const t of tools ?? []) fixedTokens += estimateToolDefTokens(t);
1620
+ const summaryReserve = 2e3;
1621
+ const outputReserve = this.config.outputReserve ?? 1024;
1622
+ const remaining = budget - fixedTokens - summaryReserve - outputReserve;
1623
+ const groups = groupTools ? messageGroups(messages) : messages.map((message) => [message]);
1624
+ const kept = [];
1625
+ let used = 0;
1626
+ for (let i = groups.length - 1; i >= 0; i--) {
1627
+ const t = estimateTokens(groups[i]);
1628
+ if (used + t > remaining) break;
1629
+ kept.unshift(groups[i]);
1630
+ used += t;
1631
+ }
1632
+ const oldMessages = groups.slice(0, groups.length - kept.length).flat();
1633
+ if (oldMessages.length === 0) {
1634
+ return null;
1635
+ }
1636
+ const summary = await this.summarizer(oldMessages, signal ? { signal } : void 0);
1637
+ const summaryMessage = { role: "user", content: `[\u4E0A\u4E0B\u6587\u6458\u8981]
1638
+ ${summary}` };
1639
+ const limit = groupTools ? budget - fixedTokens : budget;
1640
+ let afterTokens = estimateMessageTokens(summaryMessage) + used;
1641
+ while (kept.length > 1 && afterTokens > limit) {
1642
+ afterTokens -= estimateTokens(kept.shift());
1643
+ }
1644
+ const repaired = repairMessages([summaryMessage, ...kept.flat()]);
1645
+ return { messages: repaired, beforeTokens, afterTokens: estimateTokens(repaired) };
1646
+ } catch (err) {
1647
+ throwIfAborted(signal);
1648
+ console.warn("[listdoing] LLM summarization failed, falling back to mechanical compression:", err);
1649
+ return null;
1650
+ }
1227
1651
  }
1228
1652
  /**
1229
1653
  * Search across messages for relevant content.
@@ -1247,17 +1671,101 @@ ${summaryText}` },
1247
1671
  }
1248
1672
  };
1249
1673
 
1674
+ // src/context/summarizer.ts
1675
+ var SUMMARIZE_PROMPT = `\u4F60\u662F\u4E00\u4E2A\u5BF9\u8BDD\u4E0A\u4E0B\u6587\u538B\u7F29\u52A9\u624B\u3002\u8BF7\u9605\u8BFB\u4E0B\u9762\u7684\u5BF9\u8BDD\u7247\u6BB5\uFF0C\u751F\u6210\u4E00\u4EFD\u7ED3\u6784\u5316\u6458\u8981\uFF0C\u7528\u4E8E\u5728\u540E\u7EED\u5BF9\u8BDD\u4E2D\u4FDD\u7559\u5173\u952E\u4E0A\u4E0B\u6587\u3002
1676
+
1677
+ \u4E25\u683C\u6309\u4EE5\u4E0B\u7ED3\u6784\u8F93\u51FA\uFF08markdown\uFF09\uFF1A
1678
+
1679
+ ## \u76EE\u6807
1680
+ \u7528\u6237\u60F3\u8981\u5B8C\u6210\u4EC0\u4E48
1681
+
1682
+ ## \u5173\u952E\u51B3\u7B56
1683
+ \u5DF2\u505A\u7684\u51B3\u7B56\u53CA\u5176\u7406\u7531
1684
+
1685
+ ## \u6D89\u53CA\u6587\u4EF6
1686
+ \u521B\u5EFA/\u4FEE\u6539/\u8BFB\u53D6\u7684\u6587\u4EF6\uFF0C\u7B80\u8FF0\u5176\u5185\u5BB9\u6216\u53D8\u66F4
1687
+
1688
+ ## \u91CD\u8981\u53D1\u73B0
1689
+ \u7EE7\u7EED\u5DE5\u4F5C\u6240\u9700\u7684\u5173\u952E\u4E8B\u5B9E\uFF08\u6587\u4EF6\u7ED3\u6784\u3001\u6570\u636E\u3001\u641C\u7D22\u7ED3\u679C\u7B49\uFF09
1690
+
1691
+ ## \u5F53\u524D\u72B6\u6001
1692
+ \u5BF9\u8BDD\u7247\u6BB5\u7ED3\u675F\u65F6\u6B63\u5728\u505A\u4EC0\u4E48
1693
+
1694
+ ## \u5F85\u529E\u4E8B\u9879
1695
+ \u672A\u5B8C\u6210\u7684\u4EFB\u52A1\u6216\u660E\u786E\u7684\u4E0B\u4E00\u6B65
1696
+
1697
+ \u8981\u6C42\uFF1A
1698
+ - \u7B80\u6D01\uFF0C\u53EA\u4FDD\u7559\u7EE7\u7EED\u5DE5\u4F5C\u5FC5\u9700\u7684\u4FE1\u606F
1699
+ - \u4E0D\u8981\u8F93\u51FA\u4E0E\u4E0A\u8FF0\u7ED3\u6784\u65E0\u5173\u7684\u5185\u5BB9
1700
+ - \u7528\u4E0E\u5BF9\u8BDD\u5185\u5BB9\u76F8\u540C\u7684\u8BED\u8A00\u8F93\u51FA
1701
+
1702
+ --- \u5F85\u538B\u7F29\u7684\u5BF9\u8BDD ---`;
1703
+ function truncate(s, max) {
1704
+ return s.length > max ? s.slice(0, max) + "\u2026" : s;
1705
+ }
1706
+ function truncateMiddle(s, head, tail) {
1707
+ if (s.length <= head + tail) return s;
1708
+ return s.slice(0, head) + `
1709
+ \u2026[\u4E2D\u95F4 ${s.length - head - tail} \u5B57\u7B26\u5DF2\u7701\u7565]\u2026
1710
+ ` + s.slice(-tail);
1711
+ }
1712
+ function formatMessagesForSummary(messages) {
1713
+ return messages.map((m) => {
1714
+ if (m.role === "user") return `\u7528\u6237: ${contentToString(m.content)}`;
1715
+ if (m.role === "assistant") {
1716
+ let s = `\u52A9\u624B: ${contentToString(m.content)}`;
1717
+ if (m.toolCalls?.length) {
1718
+ s += "\n" + m.toolCalls.map((tc) => ` [\u8C03\u7528\u5DE5\u5177 ${tc.name}: ${truncate(JSON.stringify(tc.arguments), 200)}]`).join("\n");
1719
+ }
1720
+ return s;
1721
+ }
1722
+ if (m.role === "tool") {
1723
+ return `\u5DE5\u5177\u7ED3\u679C: ${truncateMiddle(contentToString(m.content), 500, 200)}`;
1724
+ }
1725
+ if (m.role === "system") return `\u7CFB\u7EDF: ${contentToString(m.content)}`;
1726
+ return "";
1727
+ }).filter(Boolean).join("\n\n");
1728
+ }
1729
+ function createLLMSummarizer(adapter, options) {
1730
+ const timeoutMs = options?.timeoutMs ?? 3e4;
1731
+ return async (messages, opts) => {
1732
+ const formatted = formatMessagesForSummary(messages);
1733
+ const controller = new AbortController();
1734
+ const unlink = linkAbort(opts?.signal, controller);
1735
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
1736
+ try {
1737
+ const response = await adapter.chat(
1738
+ [
1739
+ { role: "system", content: SUMMARIZE_PROMPT },
1740
+ { role: "user", content: formatted }
1741
+ ],
1742
+ [],
1743
+ { signal: controller.signal }
1744
+ );
1745
+ const summary = (response.content ?? "").trim();
1746
+ if (!summary) {
1747
+ throw new Error("Summarizer returned empty content");
1748
+ }
1749
+ return summary;
1750
+ } finally {
1751
+ clearTimeout(timer);
1752
+ unlink();
1753
+ }
1754
+ };
1755
+ }
1756
+
1250
1757
  // src/task/planner.ts
1251
1758
  var TaskPlanner = class {
1252
1759
  constructor(llmAdapter) {
1253
1760
  this.llmAdapter = llmAdapter;
1254
1761
  }
1255
- async plan(goal, availableTools) {
1762
+ async plan(goal, availableTools, signal) {
1256
1763
  const prompt = this.buildPlanningPrompt(goal, availableTools);
1257
1764
  const response = await this.llmAdapter.chat(
1258
1765
  [{ role: "user", content: prompt }],
1259
- []
1766
+ [],
1260
1767
  // No tools for the planning call itself
1768
+ { signal }
1261
1769
  );
1262
1770
  return this.parsePlan(goal, response.content);
1263
1771
  }
@@ -1576,17 +2084,19 @@ var GraphPlanner = class {
1576
2084
  constructor(llmAdapter) {
1577
2085
  this.llmAdapter = llmAdapter;
1578
2086
  }
1579
- async planGraph(goal, availableTools) {
2087
+ async planGraph(goal, availableTools, signal) {
1580
2088
  const prompt = this.buildGraphPlanningPrompt(goal, availableTools);
1581
2089
  let response;
1582
2090
  try {
1583
2091
  const result = await this.llmAdapter.chat(
1584
2092
  [{ role: "user", content: prompt }],
1585
- []
2093
+ [],
1586
2094
  // No tools for the planning call
2095
+ { signal }
1587
2096
  );
1588
2097
  response = result.content;
1589
2098
  } catch {
2099
+ throwIfAborted(signal);
1590
2100
  return this.fallbackSequentialGraph(goal);
1591
2101
  }
1592
2102
  return this.parseGraphPlan(goal, response);
@@ -1709,9 +2219,7 @@ var SubAgent = class {
1709
2219
  this.config = options.config;
1710
2220
  this.depth = options.depth;
1711
2221
  this.abortController = new AbortController();
1712
- if (options.parentSignal) {
1713
- options.parentSignal.addEventListener("abort", () => this.abortController.abort(), { once: true });
1714
- }
2222
+ this.unlinkParent = linkAbort(options.parentSignal, this.abortController);
1715
2223
  const maxDepth = this.config.maxDepth ?? 2;
1716
2224
  if (this.depth >= maxDepth) {
1717
2225
  this.config.canSpawnSubAgents = false;
@@ -1722,7 +2230,9 @@ var SubAgent = class {
1722
2230
  this.status = "running";
1723
2231
  const startTime = Date.now();
1724
2232
  try {
2233
+ throwIfAborted(this.abortController.signal);
1725
2234
  const session = this.parentAgent.createSession({
2235
+ activate: false,
1726
2236
  title: `SubAgent: ${this.nodeId}`,
1727
2237
  meta: {
1728
2238
  parentSessionId: this.parentAgent.getCurrentSessionId(),
@@ -1736,6 +2246,7 @@ var SubAgent = class {
1736
2246
  const prompt = this.buildPrompt(goal, upstreamOutputs);
1737
2247
  const options = {
1738
2248
  sessionId: session.id,
2249
+ signal: this.abortController.signal,
1739
2250
  maxTurns: this.config.maxTurns ?? 10,
1740
2251
  depth: this.depth + 1,
1741
2252
  taskMode: void 0
@@ -1759,6 +2270,7 @@ var SubAgent = class {
1759
2270
  sessionId: this.sessionId ?? void 0
1760
2271
  };
1761
2272
  }
2273
+ throwIfAborted(this.abortController.signal);
1762
2274
  this.status = "done";
1763
2275
  return {
1764
2276
  nodeId: this.nodeId,
@@ -1780,6 +2292,8 @@ var SubAgent = class {
1780
2292
  retryCount: 0,
1781
2293
  sessionId: this.sessionId ?? void 0
1782
2294
  };
2295
+ } finally {
2296
+ this.unlinkParent();
1783
2297
  }
1784
2298
  }
1785
2299
  /** Abort the sub-agent */
@@ -1824,42 +2338,48 @@ var GraphExecutor = class {
1824
2338
  this.maxDepth = options.maxDepth ?? 2;
1825
2339
  this.baseDepth = options.baseDepth ?? 0;
1826
2340
  this.abortController = new AbortController();
2341
+ this.unlinkParent = linkAbort(options.parentSignal, this.abortController);
1827
2342
  this.emit = options.emit;
1828
2343
  }
1829
2344
  /** Execute the entire graph, level by level */
1830
2345
  async execute() {
1831
- this.plan.status = "executing";
1832
- for (let level = 0; level < this.graph.getLevelCount(); level++) {
1833
- if (this.abortController.signal.aborted) {
2346
+ try {
2347
+ this.plan.status = "executing";
2348
+ for (let level = 0; level < this.graph.getLevelCount(); level++) {
2349
+ if (this.abortController.signal.aborted) {
2350
+ this.plan.status = "failed";
2351
+ return this.plan;
2352
+ }
2353
+ const nodes = this.graph.getLevel(level);
2354
+ const nodeIds = nodes.map((n) => n.id);
2355
+ this.emit("graph:level_start", { level, nodeIds });
2356
+ await this.executeLevel(level, nodes);
2357
+ const levelResults = nodeIds.map((id) => this.plan.results[id]).filter(Boolean);
2358
+ this.emit("graph:level_complete", { level, results: levelResults });
2359
+ }
2360
+ const progress = this.getProgress();
2361
+ if (progress.failed === 0 && progress.done === progress.total) {
2362
+ this.plan.status = "completed";
2363
+ this.emit("graph:complete", { plan: this.plan, progress });
2364
+ } else if (progress.done > 0) {
2365
+ this.plan.status = "partial";
2366
+ this.emit("graph:complete", { plan: this.plan, progress });
2367
+ } else {
1834
2368
  this.plan.status = "failed";
1835
- return this.plan;
1836
- }
1837
- const nodes = this.graph.getLevel(level);
1838
- const nodeIds = nodes.map((n) => n.id);
1839
- this.emit("graph:level_start", { level, nodeIds });
1840
- await this.executeLevel(level, nodes);
1841
- const levelResults = nodeIds.map((id) => this.plan.results[id]).filter(Boolean);
1842
- this.emit("graph:level_complete", { level, results: levelResults });
1843
- }
1844
- const progress = this.getProgress();
1845
- if (progress.failed === 0 && progress.done === progress.total) {
1846
- this.plan.status = "completed";
1847
- this.emit("graph:complete", { plan: this.plan, progress });
1848
- } else if (progress.done > 0) {
1849
- this.plan.status = "partial";
1850
- this.emit("graph:complete", { plan: this.plan, progress });
1851
- } else {
1852
- this.plan.status = "failed";
1853
- const failedNodes = Object.entries(this.plan.results).filter(([, r]) => r.status === "failed").map(([id]) => id);
1854
- this.emit("graph:failed", { plan: this.plan, failedNodes });
2369
+ const failedNodes = Object.entries(this.plan.results).filter(([, r]) => r.status === "failed").map(([id]) => id);
2370
+ this.emit("graph:failed", { plan: this.plan, failedNodes });
2371
+ }
2372
+ this.plan.updatedAt = Date.now();
2373
+ return this.plan;
2374
+ } finally {
2375
+ this.unlinkParent();
1855
2376
  }
1856
- this.plan.updatedAt = Date.now();
1857
- return this.plan;
1858
2377
  }
1859
2378
  /** Execute a single level — nodes run concurrently (limited by maxConcurrency) */
1860
2379
  async executeLevel(level, nodes) {
1861
- const runnableNodes = nodes.filter((node) => this.shouldRun(node));
2380
+ const runnableNodes = nodes.filter((node) => this.plan.results[node.id]?.status !== "done" && this.shouldRun(node));
1862
2381
  for (const node of nodes) {
2382
+ if (this.plan.results[node.id]?.status === "done") continue;
1863
2383
  if (!runnableNodes.includes(node)) {
1864
2384
  this.plan.results[node.id] = {
1865
2385
  nodeId: node.id,
@@ -1959,7 +2479,8 @@ var GraphExecutor = class {
1959
2479
  willRetry: true
1960
2480
  });
1961
2481
  const delay = Math.min(backoffMs * Math.pow(2, attempt), maxBackoffMs);
1962
- await new Promise((resolve) => setTimeout(resolve, delay));
2482
+ await abortableDelay(delay, this.abortController.signal).catch(() => {
2483
+ });
1963
2484
  }
1964
2485
  }
1965
2486
  if (node.fallback) {
@@ -2018,8 +2539,9 @@ var GraphExecutor = class {
2018
2539
  depth: this.baseDepth
2019
2540
  });
2020
2541
  if (node.timeout) {
2542
+ let timer;
2021
2543
  const timeoutPromise = new Promise((_, reject) => {
2022
- setTimeout(() => reject(new ACPError(
2544
+ timer = setTimeout(() => reject(new ACPError(
2023
2545
  ACPErrorCodes.SUBAGENT_TIMEOUT,
2024
2546
  `Node "${node.id}" timed out after ${node.timeout}ms`
2025
2547
  )), node.timeout);
@@ -2039,6 +2561,8 @@ var GraphExecutor = class {
2039
2561
  duration: 0,
2040
2562
  retryCount: 0
2041
2563
  };
2564
+ } finally {
2565
+ clearTimeout(timer);
2042
2566
  }
2043
2567
  }
2044
2568
  return subAgent.execute(node.goal, upstreamOutputs);
@@ -2239,7 +2763,7 @@ var HttpTransport = class {
2239
2763
  }
2240
2764
  return headers;
2241
2765
  }
2242
- async send(method, params) {
2766
+ async send(method, params, options) {
2243
2767
  const id = ++this.requestId;
2244
2768
  const body = {
2245
2769
  jsonrpc: "2.0",
@@ -2251,21 +2775,31 @@ var HttpTransport = class {
2251
2775
  const res = await fetch(`${this.baseUrl}/mcp`, {
2252
2776
  method: "POST",
2253
2777
  headers: this.buildHeaders({ Accept: "application/json" }),
2778
+ signal: options?.signal,
2254
2779
  body: JSON.stringify(body)
2255
2780
  });
2256
- const data = await res.json();
2781
+ let data;
2782
+ try {
2783
+ data = await res.json();
2784
+ } catch (error) {
2785
+ if (!res.ok) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, `HTTP ${res.status}`, { method, status: res.status });
2786
+ throw error;
2787
+ }
2257
2788
  const sid = res.headers.get("Mcp-Session-Id");
2258
2789
  if (sid) this.sessionId = sid;
2259
- if (data.error) {
2790
+ if (data?.error) {
2260
2791
  throw new ACPError(data.error.code, data.error.message, data.error.data);
2261
2792
  }
2793
+ if (!res.ok) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, `HTTP ${res.status}`, { method, status: res.status });
2794
+ if (!data || !Object.prototype.hasOwnProperty.call(data, "result")) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, "Invalid JSON-RPC response", { method });
2262
2795
  return data.result;
2263
2796
  } catch (err) {
2797
+ throwIfAborted(options?.signal);
2264
2798
  if (err instanceof ACPError) throw err;
2265
2799
  throw new ACPError(
2266
2800
  ACPErrorCodes.TRANSPORT_ERROR,
2267
- `Transport error: ${err.message}`,
2268
- { method, originalError: err.message }
2801
+ `Transport error: ${errorMessage(err)}`,
2802
+ { method, originalError: errorMessage(err) }
2269
2803
  );
2270
2804
  }
2271
2805
  }
@@ -2285,7 +2819,7 @@ var HttpTransport = class {
2285
2819
  /**
2286
2820
  * Stream a chat request via SSE.
2287
2821
  */
2288
- async *streamChat(method, params) {
2822
+ async *streamChat(method, params, options) {
2289
2823
  const id = ++this.requestId;
2290
2824
  const body = {
2291
2825
  jsonrpc: "2.0",
@@ -2296,33 +2830,48 @@ var HttpTransport = class {
2296
2830
  const res = await fetch(`${this.baseUrl}/mcp`, {
2297
2831
  method: "POST",
2298
2832
  headers: this.buildHeaders({ Accept: "text/event-stream" }),
2833
+ signal: options?.signal,
2299
2834
  body: JSON.stringify(body)
2300
2835
  });
2836
+ if (!res.ok) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, `HTTP ${res.status}`, { method, status: res.status });
2301
2837
  if (!res.body) {
2302
2838
  throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, "No response body for stream");
2303
2839
  }
2304
2840
  const reader = res.body.getReader();
2305
2841
  const decoder = new TextDecoder();
2306
2842
  let buffer = "";
2307
- while (true) {
2308
- const { done, value } = await reader.read();
2309
- if (done) break;
2310
- buffer += decoder.decode(value, { stream: true });
2311
- const lines = buffer.split("\n");
2312
- buffer = lines.pop() || "";
2313
- for (const line of lines) {
2314
- if (line.startsWith("data: ")) {
2315
- try {
2316
- const data = JSON.parse(line.slice(6));
2317
- if (data.result !== void 0) {
2318
- yield { id: data.id, result: data.result };
2319
- } else if (data.method) {
2320
- yield { method: data.method, params: data.params };
2843
+ try {
2844
+ while (true) {
2845
+ throwIfAborted(options?.signal);
2846
+ const { done, value } = await reader.read();
2847
+ buffer += done ? decoder.decode() : decoder.decode(value, { stream: true });
2848
+ if (done) {
2849
+ if (!buffer.trim()) break;
2850
+ buffer += "\n";
2851
+ }
2852
+ const lines = buffer.split("\n");
2853
+ buffer = lines.pop() || "";
2854
+ for (const line of lines) {
2855
+ if (line.trim().startsWith("data:")) {
2856
+ const payload = line.trim().slice(5).trim();
2857
+ if (payload === "[DONE]") continue;
2858
+ let data;
2859
+ try {
2860
+ data = JSON.parse(payload);
2861
+ } catch {
2862
+ throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, "Invalid JSON in transport stream");
2321
2863
  }
2322
- } catch {
2864
+ if (data.error) throw new ACPError(data.error.code, data.error.message, data.error.data);
2865
+ if (data.result !== void 0) yield { id: data.id, result: data.result };
2866
+ else if (data.method) yield { method: data.method, params: data.params };
2323
2867
  }
2324
2868
  }
2869
+ if (done) break;
2325
2870
  }
2871
+ } finally {
2872
+ await reader.cancel().catch(() => {
2873
+ });
2874
+ reader.releaseLock();
2326
2875
  }
2327
2876
  }
2328
2877
  };
@@ -2369,27 +2918,35 @@ var Ldoing = class {
2369
2918
  constructor(config) {
2370
2919
  this.initialized = false;
2371
2920
  this.startTime = 0;
2921
+ this.mcpStatus = "unchecked";
2922
+ this.discoveredServerSkills = /* @__PURE__ */ new WeakSet();
2372
2923
  this.currentSessionId = null;
2373
2924
  this.abortControllers = /* @__PURE__ */ new Map();
2925
+ this.runContexts = /* @__PURE__ */ new WeakMap();
2374
2926
  // Session-level concurrency control (方案 B: abort 旧请求 + 等待退出)
2375
2927
  // runningSessions 跟踪正在执行 agent loop 的会话,donePromise 在 agent loop 结束时 resolve
2376
2928
  this.runningSessions = /* @__PURE__ */ new Map();
2377
2929
  // Approval pending promises
2378
2930
  this.approvalResolvers = /* @__PURE__ */ new Map();
2931
+ this.sessionStorageQueues = /* @__PURE__ */ new Map();
2932
+ this.managingSessions = /* @__PURE__ */ new Map();
2933
+ this.compressingSessions = /* @__PURE__ */ new Map();
2379
2934
  // Event listeners
2380
2935
  this.eventHandlers = /* @__PURE__ */ new Map();
2381
2936
  // Token estimation calibration
2382
2937
  this.tokenCalibrationRatio = 1;
2383
- this.config = config;
2938
+ const proxy = resolveProxyConfig(config);
2939
+ this.config = config.llmProxyMode === void 0 ? config : { ...config, llm: { ...config.llm } };
2384
2940
  this.agentId = `agent-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
2385
- this.llmAdapter = new OpenAIAdapter(config.llm);
2941
+ this.llmAdapter = new OpenAIAdapter(proxy.llm, { rejectRedirects: config.llmProxyMode !== void 0 });
2386
2942
  this.skillRegistry = new SkillRegistry();
2387
- this.skillExecutor = new SkillExecutor(this.skillRegistry);
2943
+ this.skillExecutor = new SkillExecutor(this.skillRegistry, void 0, { validateInputs: config.skills?.validateInputs });
2388
2944
  this.mcpRouter = new McpRouter();
2389
2945
  this.sessionManager = new SessionManager();
2390
2946
  this.contextManager = new ContextManager(config.context, config.llm.model);
2391
- if (config.serverUrl) {
2392
- this.transport = new HttpTransport(config.serverUrl, config.authToken, config.headers);
2947
+ this.contextManager.setSummarizer(createLLMSummarizer(this.llmAdapter));
2948
+ if (proxy.serverUrl) {
2949
+ this.transport = new HttpTransport(proxy.serverUrl, config.authToken, config.headers);
2393
2950
  } else {
2394
2951
  this.transport = new LocalTransport();
2395
2952
  }
@@ -2410,10 +2967,13 @@ var Ldoing = class {
2410
2967
  }
2411
2968
  }
2412
2969
  let mode = "local";
2413
- let connected = false;
2414
2970
  if (this.config.serverUrl) {
2971
+ const transport = this.transport;
2972
+ const proxyMode = this.config.llmProxyMode;
2973
+ const { adapter, model } = this.config.llm;
2974
+ this.mcpStatus = "unchecked";
2415
2975
  try {
2416
- const result2 = await this.transport.send("initialize", {
2976
+ const result2 = await transport.send("initialize", {
2417
2977
  protocolVersion: "2025-03-26",
2418
2978
  capabilities: {
2419
2979
  acp: {
@@ -2421,47 +2981,47 @@ var Ldoing = class {
2421
2981
  sessions: true,
2422
2982
  contextManagement: true,
2423
2983
  dynamicSkillRegistration: true,
2424
- llmConfig: this.config.llm
2984
+ llmConfig: proxyMode === void 0 ? this.config.llm : { adapter, model }
2425
2985
  }
2426
2986
  },
2427
2987
  clientInfo: { name: "listdoing-sdk", version: "0.1.0" }
2428
2988
  });
2429
- connected = true;
2430
- const serverSkills = result2?.capabilities?.acp?.serverSkills ?? [];
2431
- for (const name of serverSkills) {
2432
- if (!this.skillRegistry.has(name)) {
2433
- this.skillRegistry.register({
2434
- name,
2435
- description: `Server skill: ${name}`,
2436
- inputSchema: { type: "object", properties: {} },
2437
- location: "server"
2438
- });
2989
+ if (proxyMode === void 0 || this.transport === transport) {
2990
+ const serverSkills = result2?.capabilities?.acp?.serverSkills ?? [];
2991
+ for (const name of serverSkills) {
2992
+ if (!this.skillRegistry.has(name)) {
2993
+ const skill = {
2994
+ name,
2995
+ description: `Server skill: ${name}`,
2996
+ inputSchema: { type: "object", properties: {} },
2997
+ location: "server"
2998
+ };
2999
+ this.skillRegistry.register(skill);
3000
+ this.discoveredServerSkills.add(skill);
3001
+ }
2439
3002
  }
3003
+ if (this.transport === transport) this.mcpStatus = "connected";
3004
+ mode = this.skillRegistry.list().some((s) => s.location === "local") ? "hybrid" : "remote";
2440
3005
  }
2441
- mode = this.skillRegistry.list().some((s) => s.location === "local") ? "hybrid" : "remote";
2442
3006
  } catch {
2443
- mode = "local";
3007
+ if (this.transport === transport) this.mcpStatus = "failed";
2444
3008
  }
2445
3009
  }
2446
- let llmConnected = false;
2447
- try {
2448
- llmConnected = true;
2449
- } catch {
2450
- llmConnected = false;
2451
- }
2452
3010
  this.initialized = true;
2453
3011
  const result = {
2454
3012
  agentId: this.agentId,
2455
3013
  mode,
3014
+ ...this.getConnectionDetails(),
2456
3015
  llm: {
2457
- connected: llmConnected,
3016
+ connected: true,
3017
+ // Legacy status. Use llmRoute.verified for a successful request.
2458
3018
  provider: this.config.llm.adapter,
2459
3019
  model: this.config.llm.model
2460
3020
  },
2461
3021
  capabilities: {
2462
3022
  localSkills: this.skillRegistry.list().filter((s) => s.location !== "server").map((s) => s.name),
2463
3023
  serverSkills: this.skillRegistry.list().filter((s) => s.location === "server").map((s) => s.name),
2464
- llmProxy: mode !== "local"
3024
+ llmProxy: this.config.llmProxyMode !== void 0 || mode !== "local"
2465
3025
  }
2466
3026
  };
2467
3027
  this.emit("init", result);
@@ -2472,7 +3032,10 @@ var Ldoing = class {
2472
3032
  controller.abort();
2473
3033
  }
2474
3034
  this.abortControllers.clear();
3035
+ for (const pending of this.approvalResolvers.values()) pending.reject(abortError());
3036
+ this.approvalResolvers.clear();
2475
3037
  this.initialized = false;
3038
+ this.mcpStatus = "unchecked";
2476
3039
  this.emit("destroy", {});
2477
3040
  }
2478
3041
  getStatus() {
@@ -2482,6 +3045,7 @@ var Ldoing = class {
2482
3045
  uptime: Date.now() - this.startTime,
2483
3046
  activeSessions: this.sessionManager.list({ status: "active" }).length,
2484
3047
  loadedSkills: this.skillRegistry.list().length,
3048
+ ...this.getConnectionDetails(),
2485
3049
  llm: {
2486
3050
  adapter: this.config.llm.adapter,
2487
3051
  model: this.config.llm.model,
@@ -2489,15 +3053,26 @@ var Ldoing = class {
2489
3053
  }
2490
3054
  };
2491
3055
  }
3056
+ getConnectionDetails() {
3057
+ const mcpStatus = this.config.serverUrl ? this.mcpStatus : "not-configured";
3058
+ try {
3059
+ const status = this.llmAdapter.getConnectionStatus?.();
3060
+ if (status) return { mcpStatus, llmRoute: { mode: this.config.llmProxyMode ?? "legacy", ...status } };
3061
+ } catch {
3062
+ }
3063
+ return { mcpStatus };
3064
+ }
2492
3065
  // ---- Chat ----
2493
3066
  async chat(message, options) {
2494
3067
  this.ensureInitialized();
2495
3068
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
2496
- this.currentSessionId = sessionId;
3069
+ if (!options?._skipLock || !this.abortControllers.has(sessionId)) this.ensureSessionIsNotBeingManaged(sessionId);
3070
+ if (!options?._skipLock) this.currentSessionId = sessionId;
2497
3071
  const releaseLock = options?._skipLock ? null : await this.acquireSessionLock(sessionId);
2498
- const controller = new AbortController();
2499
- this.abortControllers.set(sessionId, controller);
3072
+ const execution = this.beginExecution(sessionId, options);
3073
+ const controller = execution.controller;
2500
3074
  try {
3075
+ const runContext = this.prepareRunContext(controller.signal, options?.runContext);
2501
3076
  if (options?.taskMode === "plan") {
2502
3077
  return await this.chatWithPlan(message, options);
2503
3078
  }
@@ -2514,44 +3089,55 @@ var Ldoing = class {
2514
3089
  this.sessionManager.addMessage(sessionId, { role: "user", content: userContent });
2515
3090
  let turn = 0;
2516
3091
  let continueLoop = true;
3092
+ let completedReply;
2517
3093
  let retried = false;
2518
3094
  while (continueLoop && turn < maxTurns) {
3095
+ throwIfAborted(controller.signal);
2519
3096
  turn++;
2520
3097
  const session2 = this.sessionManager.getOrThrow(sessionId);
2521
- const systemPrompt = this.resolveSystemPrompt(sessionId, session2.systemPrompt ?? this.config.systemPrompt ?? "");
2522
- const allTools = this.skillRegistry.listToolDefs();
3098
+ const systemPrompt = this.buildRunSystemPrompt(session2, runContext);
3099
+ const allTools = this.listRunTools(runContext);
2523
3100
  const tools = this.resolveTools(allTools, { allowedTools: options?.tools, toolsByPermission: options?.toolsByPermission });
2524
3101
  for (const name of this.computeAutoApproval(tools)) {
2525
3102
  requireApproval.add(name);
2526
3103
  }
2527
- const ctxCheck = this.contextManager.ensureContextFits(
3104
+ const ctxCheck = await this.contextManager.ensureContextFits(
2528
3105
  session2.messages,
2529
3106
  systemPrompt,
2530
3107
  tools,
2531
- this.config.llm.model
3108
+ this.config.llm.model,
3109
+ controller.signal
2532
3110
  );
2533
3111
  if (ctxCheck.wasCompressed) {
2534
3112
  session2.messages = ctxCheck.messages;
2535
- this.emit("context:compress", { sessionId, reason: "auto" });
3113
+ this.emit("context:compress", {
3114
+ sessionId,
3115
+ reason: "auto",
3116
+ beforeTokens: ctxCheck.beforeTokens,
3117
+ afterTokens: ctxCheck.afterTokens
3118
+ });
2536
3119
  }
2537
- const contextMessages = this.contextManager.buildWindow(
3120
+ throwIfAborted(controller.signal);
3121
+ const window2 = this.contextManager.buildWindow(
2538
3122
  session2.messages,
2539
3123
  systemPrompt,
2540
3124
  tools
2541
- ).messages;
3125
+ );
3126
+ let contextMessages = window2.messages;
2542
3127
  let llmResponse;
2543
3128
  try {
2544
- llmResponse = await this.llmAdapter.chat(contextMessages, tools, {
2545
- signal: controller.signal
2546
- });
2547
- } catch (err) {
2548
- if (err.code === ACPErrorCodes.CONTEXT_OVERFLOW && !retried) {
2549
- this.compressContext(sessionId, "hybrid");
3129
+ try {
3130
+ llmResponse = await this.llmAdapter.chat(contextMessages, tools, { signal: controller.signal });
3131
+ } catch (err) {
3132
+ if (err?.code !== ACPErrorCodes.CONTEXT_OVERFLOW || retried) throw err;
3133
+ const smaller = await this.prepareOverflowRetry(session2, window2, systemPrompt, tools, controller.signal);
3134
+ if (!smaller) throw err;
2550
3135
  retried = true;
2551
- turn--;
2552
- continue;
3136
+ contextMessages = smaller;
3137
+ llmResponse = await this.llmAdapter.chat(contextMessages, tools, { signal: controller.signal });
2553
3138
  }
2554
- if (err.code === ACPErrorCodes.LLM_EMPTY_RESPONSE) {
3139
+ } catch (err) {
3140
+ if (err?.code === ACPErrorCodes.LLM_EMPTY_RESPONSE) {
2555
3141
  if (!retried) {
2556
3142
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
2557
3143
  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" });
@@ -2560,10 +3146,12 @@ var Ldoing = class {
2560
3146
  continue;
2561
3147
  }
2562
3148
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
3149
+ completedReply = ".";
2563
3150
  continueLoop = false;
2564
3151
  break;
2565
3152
  }
2566
- throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, `LLM call failed: ${err.message}`, { turn });
3153
+ if (controller.signal.aborted || err?.name === "AbortError") throw abortError();
3154
+ throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, `LLM call failed: ${errorMessage(err)}`, { turn });
2567
3155
  }
2568
3156
  if (!llmResponse.content && !llmResponse.toolCalls?.length) {
2569
3157
  if (!retried) {
@@ -2574,9 +3162,11 @@ var Ldoing = class {
2574
3162
  continue;
2575
3163
  }
2576
3164
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
3165
+ completedReply = ".";
2577
3166
  continueLoop = false;
2578
3167
  break;
2579
3168
  }
3169
+ this.reportPromptCacheUsage(sessionId, llmResponse.promptCacheUsage);
2580
3170
  if (llmResponse.usage) {
2581
3171
  totalInputTokens += llmResponse.usage.input;
2582
3172
  totalOutputTokens += llmResponse.usage.output;
@@ -2587,6 +3177,7 @@ var Ldoing = class {
2587
3177
  }
2588
3178
  }
2589
3179
  if (!llmResponse.toolCalls?.length) {
3180
+ completedReply = llmResponse.content;
2590
3181
  this.sessionManager.addMessage(sessionId, {
2591
3182
  role: "assistant",
2592
3183
  content: llmResponse.content
@@ -2600,8 +3191,9 @@ var Ldoing = class {
2600
3191
  toolCalls: llmResponse.toolCalls
2601
3192
  });
2602
3193
  const turnTrace = { turn, toolCalls: [] };
2603
- const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval);
2604
- await this.processApprovalQueue(plan.items, sessionId, (event, data) => this.emit(event, data));
3194
+ const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval, runContext, tools);
3195
+ for await (const _event of this.processApprovalQueue(plan.items, sessionId, controller.signal)) {
3196
+ }
2605
3197
  const batchTraces = await this.executeToolBatch(plan.items, sessionId, controller, plan.canParallelize, (event, data) => this.emit(event, data));
2606
3198
  turnTrace.toolCalls = batchTraces;
2607
3199
  const executableCount = plan.items.filter((i) => !i.resolved).length;
@@ -2609,26 +3201,27 @@ var Ldoing = class {
2609
3201
  executionTrace.push(turnTrace);
2610
3202
  }
2611
3203
  const session = this.sessionManager.getOrThrow(sessionId);
2612
- const lastAssistant = [...session.messages].reverse().find((m) => m.role === "assistant" && !m.toolCalls?.length);
2613
- const reply = typeof lastAssistant?.content === "string" ? lastAssistant.content : lastAssistant ? contentToString(lastAssistant.content) : "";
2614
- const chatResult = { reply, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3204
+ const reply = completedReply ?? "";
3205
+ const terminationReason = completedReply !== void 0 ? "stop" : "max_turns";
3206
+ const chatResult = { reply, terminationReason, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
2615
3207
  this.emit("chat:complete", { sessionId, reply, tokenUsage: chatResult.tokenUsage });
2616
- await this.persistence.saveSession(session).catch(() => {
2617
- });
3208
+ await this.saveSessionSafely(session);
2618
3209
  return chatResult;
2619
3210
  } finally {
2620
- this.abortControllers.delete(sessionId);
3211
+ execution.cleanup();
2621
3212
  if (releaseLock) releaseLock();
2622
3213
  }
2623
3214
  }
2624
3215
  async *chatStream(message, options) {
2625
3216
  this.ensureInitialized();
2626
3217
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
2627
- this.currentSessionId = sessionId;
3218
+ if (!options?._skipLock || !this.abortControllers.has(sessionId)) this.ensureSessionIsNotBeingManaged(sessionId);
3219
+ if (!options?._skipLock) this.currentSessionId = sessionId;
2628
3220
  const releaseLock = options?._skipLock ? null : await this.acquireSessionLock(sessionId);
2629
- const controller = new AbortController();
2630
- this.abortControllers.set(sessionId, controller);
3221
+ const execution = this.beginExecution(sessionId, options);
3222
+ const controller = execution.controller;
2631
3223
  try {
3224
+ const runContext = this.prepareRunContext(controller.signal, options?.runContext);
2632
3225
  if (options?.taskMode === "plan") {
2633
3226
  yield* this.chatStreamWithPlan(message, options);
2634
3227
  return;
@@ -2647,50 +3240,67 @@ var Ldoing = class {
2647
3240
  this.sessionManager.addMessage(sessionId, { role: "user", content: userContent });
2648
3241
  let turn = 0;
2649
3242
  let continueLoop = true;
3243
+ let completedReply;
2650
3244
  while (continueLoop && turn < maxTurns) {
3245
+ throwIfAborted(controller.signal);
2651
3246
  turn++;
2652
3247
  const session2 = this.sessionManager.getOrThrow(sessionId);
2653
- const systemPrompt = this.resolveSystemPrompt(sessionId, session2.systemPrompt ?? this.config.systemPrompt ?? "");
2654
- const allTools = this.skillRegistry.listToolDefs();
3248
+ const systemPrompt = this.buildRunSystemPrompt(session2, runContext);
3249
+ const allTools = this.listRunTools(runContext);
2655
3250
  const tools = this.resolveTools(allTools, { allowedTools: options?.tools, toolsByPermission: options?.toolsByPermission });
2656
3251
  for (const name of this.computeAutoApproval(tools)) {
2657
3252
  requireApproval.add(name);
2658
3253
  }
2659
- const ctxCheck = this.contextManager.ensureContextFits(session2.messages, systemPrompt, tools, this.config.llm.model);
3254
+ const ctxCheck = await this.contextManager.ensureContextFits(
3255
+ session2.messages,
3256
+ systemPrompt,
3257
+ tools,
3258
+ this.config.llm.model,
3259
+ controller.signal
3260
+ );
2660
3261
  if (ctxCheck.wasCompressed) {
2661
3262
  session2.messages = ctxCheck.messages;
2662
- this.emit("context:compress", { sessionId, reason: "auto" });
3263
+ this.emit("context:compress", {
3264
+ sessionId,
3265
+ reason: "auto",
3266
+ beforeTokens: ctxCheck.beforeTokens,
3267
+ afterTokens: ctxCheck.afterTokens
3268
+ });
3269
+ yield {
3270
+ type: "context:compressed",
3271
+ sessionId,
3272
+ beforeTokens: ctxCheck.beforeTokens ?? 0,
3273
+ afterTokens: ctxCheck.afterTokens ?? 0,
3274
+ reason: "auto"
3275
+ };
2663
3276
  }
2664
- const contextMessages = this.contextManager.buildWindow(session2.messages, systemPrompt, tools).messages;
3277
+ throwIfAborted(controller.signal);
3278
+ const window2 = this.contextManager.buildWindow(session2.messages, systemPrompt, tools);
3279
+ let contextMessages = window2.messages;
2665
3280
  let llmResponse;
2666
3281
  try {
2667
- const useStreaming = typeof this.llmAdapter.chatStream === "function";
2668
- if (useStreaming) {
2669
- llmResponse = yield* this.streamLlmResponse(contextMessages, tools, controller.signal);
2670
- } else {
2671
- llmResponse = await this.llmAdapter.chat(contextMessages, tools, { signal: controller.signal });
2672
- if (llmResponse.reasoning) {
2673
- yield { type: "llm_thinking", content: llmResponse.reasoning };
2674
- }
2675
- if (llmResponse.content) {
2676
- yield { type: "llm_text", content: llmResponse.content };
3282
+ for (; ; ) {
3283
+ try {
3284
+ llmResponse = yield* this.streamLlmResponse(contextMessages, tools, controller.signal);
3285
+ break;
3286
+ } catch (err) {
3287
+ if (err?.code !== ACPErrorCodes.CONTEXT_OVERFLOW || retried) throw err;
3288
+ const smaller = await this.prepareOverflowRetry(session2, window2, systemPrompt, tools, controller.signal);
3289
+ if (!smaller) throw err;
3290
+ retried = true;
3291
+ contextMessages = smaller;
2677
3292
  }
2678
3293
  }
2679
3294
  } catch (err) {
2680
- if (err.code === ACPErrorCodes.CONTEXT_OVERFLOW && !retried) {
2681
- this.compressContext(sessionId, "hybrid");
2682
- retried = true;
2683
- turn--;
2684
- continue;
2685
- }
2686
- if (err.code === ACPErrorCodes.LLM_EMPTY_RESPONSE && !retried) {
3295
+ if (err?.code === ACPErrorCodes.LLM_EMPTY_RESPONSE && !retried) {
2687
3296
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
2688
3297
  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" });
2689
3298
  retried = true;
2690
3299
  turn--;
2691
3300
  continue;
2692
3301
  }
2693
- yield { type: "error", code: ACPErrorCodes.LLM_CALL_FAILED, message: err.message };
3302
+ if (controller.signal.aborted || err?.name === "AbortError") throw abortError();
3303
+ yield { type: "error", code: ACPErrorCodes.LLM_CALL_FAILED, message: errorMessage(err) };
2694
3304
  return;
2695
3305
  }
2696
3306
  if (!llmResponse.content && !llmResponse.toolCalls?.length) {
@@ -2702,8 +3312,10 @@ var Ldoing = class {
2702
3312
  continue;
2703
3313
  }
2704
3314
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
3315
+ completedReply = ".";
2705
3316
  break;
2706
3317
  }
3318
+ this.reportPromptCacheUsage(sessionId, llmResponse.promptCacheUsage);
2707
3319
  if (llmResponse.usage) {
2708
3320
  totalInputTokens += llmResponse.usage.input;
2709
3321
  totalOutputTokens += llmResponse.usage.output;
@@ -2714,27 +3326,21 @@ var Ldoing = class {
2714
3326
  }
2715
3327
  }
2716
3328
  if (!llmResponse.toolCalls?.length) {
3329
+ completedReply = llmResponse.content;
2717
3330
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: llmResponse.content });
2718
3331
  break;
2719
3332
  }
2720
3333
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: llmResponse.content, toolCalls: llmResponse.toolCalls });
2721
- const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval);
2722
- const approvalEvents = [];
2723
- const approvalYield = (evt) => approvalEvents.push(evt);
2724
- await this.processApprovalQueue(plan.items, sessionId, (event, data) => this.emit(event, data), approvalYield);
2725
- for (const evt of approvalEvents) yield evt;
2726
- const batchEvents = [];
2727
- const batchYield = (evt) => batchEvents.push(evt);
2728
- await this.executeToolBatchStream(plan.items, sessionId, controller, plan.canParallelize, batchYield);
2729
- for (const evt of batchEvents) yield evt;
3334
+ const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval, runContext, tools);
3335
+ yield* this.processApprovalQueue(plan.items, sessionId, controller.signal);
3336
+ yield* this.streamToolBatch(plan.items, sessionId, controller, plan.canParallelize);
2730
3337
  }
2731
3338
  const session = this.sessionManager.getOrThrow(sessionId);
2732
- const lastAssistant = [...session.messages].reverse().find((m) => m.role === "assistant" && !m.toolCalls?.length);
2733
- yield { type: "done", reply: typeof lastAssistant?.content === "string" ? lastAssistant.content : lastAssistant ? contentToString(lastAssistant.content) : "", tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
2734
- await this.persistence.saveSession(session).catch(() => {
2735
- });
3339
+ const saving = this.saveSessionSafely(session);
3340
+ yield { type: "done", terminationReason: completedReply !== void 0 ? "stop" : "max_turns", reply: completedReply ?? "", tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3341
+ await saving;
2736
3342
  } finally {
2737
- this.abortControllers.delete(sessionId);
3343
+ execution.cleanup();
2738
3344
  if (releaseLock) releaseLock();
2739
3345
  }
2740
3346
  }
@@ -2744,7 +3350,6 @@ var Ldoing = class {
2744
3350
  const controller = this.abortControllers.get(sid);
2745
3351
  if (controller) {
2746
3352
  controller.abort();
2747
- this.abortControllers.delete(sid);
2748
3353
  }
2749
3354
  }
2750
3355
  }
@@ -2752,7 +3357,7 @@ var Ldoing = class {
2752
3357
  /**
2753
3358
  * 在启动新的 agent loop 前获取会话锁。
2754
3359
  * 根据并发策略处理正在运行的旧请求:
2755
- * - 'abort' (默认): 中止旧请求,等待其真正退出,清理半截消息,然后返回
3360
+ * - 'abort' (默认): 中止旧请求,等待其真正退出,发送前修复未配对消息,然后返回
2756
3361
  * - 'reject': 若已有运行请求,抛错
2757
3362
  * - 'queue': 预留,当前回退到 'abort'
2758
3363
  *
@@ -2760,48 +3365,27 @@ var Ldoing = class {
2760
3365
  */
2761
3366
  async acquireSessionLock(sessionId) {
2762
3367
  const policy = this.config.concurrentPolicy ?? "abort";
2763
- const running = this.runningSessions.get(sessionId);
2764
- if (running) {
2765
- if (policy === "reject") {
2766
- throw new ACPError(
2767
- ACPErrorCodes.SESSION_BUSY,
2768
- `Session ${sessionId} is already running a chat`
2769
- );
2770
- }
3368
+ let running = this.runningSessions.get(sessionId);
3369
+ while (running) {
3370
+ this.ensureSessionIsNotBeingManaged(sessionId);
3371
+ if (policy === "reject") throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${sessionId} is already running a chat`);
2771
3372
  await this.abort(sessionId);
2772
3373
  await running.donePromise;
2773
- this.cleanupInterruptedMessages(sessionId);
3374
+ running = this.runningSessions.get(sessionId);
2774
3375
  }
3376
+ this.ensureInitialized();
3377
+ this.ensureSessionIsNotBeingManaged(sessionId);
2775
3378
  let resolveDone;
2776
3379
  const donePromise = new Promise((resolve) => {
2777
3380
  resolveDone = resolve;
2778
3381
  });
2779
- this.runningSessions.set(sessionId, { donePromise, resolveDone });
3382
+ const owner = { donePromise, resolveDone };
3383
+ this.runningSessions.set(sessionId, owner);
2780
3384
  return () => {
2781
- this.runningSessions.delete(sessionId);
3385
+ if (this.runningSessions.get(sessionId) === owner) this.runningSessions.delete(sessionId);
2782
3386
  resolveDone();
2783
3387
  };
2784
3388
  }
2785
- /**
2786
- * 清理被 abort 打断的半截消息:
2787
- * 如果 session.messages 最后一条是 assistant 且带 tool_calls,
2788
- * 但后面没有对应的 tool 消息,则删除这条 assistant。
2789
- * 避免下次 LLM 调用因 tool_calls 未配对而报错。
2790
- */
2791
- cleanupInterruptedMessages(sessionId) {
2792
- try {
2793
- const session = this.sessionManager.get(sessionId);
2794
- if (!session?.messages?.length) return;
2795
- const msgs = session.messages;
2796
- const last = msgs[msgs.length - 1];
2797
- if (last.role === "assistant" && last.toolCalls?.length) {
2798
- msgs.pop();
2799
- console.log(`[concurrency] cleaned up interrupted assistant(tool_calls) from session ${sessionId}`);
2800
- }
2801
- } catch (err) {
2802
- console.warn("[concurrency] failed to cleanup interrupted messages:", err);
2803
- }
2804
- }
2805
3389
  // ---- Tool Approval ----
2806
3390
  approveTool(callId, approved, _reason) {
2807
3391
  const resolver = this.approvalResolvers.get(callId);
@@ -2810,15 +3394,28 @@ var Ldoing = class {
2810
3394
  this.approvalResolvers.delete(callId);
2811
3395
  }
2812
3396
  }
2813
- waitForApproval(callId) {
3397
+ waitForApproval(callId, signal) {
3398
+ throwIfAborted(signal);
2814
3399
  return new Promise((resolve, reject) => {
2815
- this.approvalResolvers.set(callId, { resolve, reject });
2816
- setTimeout(() => {
2817
- if (this.approvalResolvers.has(callId)) {
2818
- this.approvalResolvers.delete(callId);
2819
- resolve(false);
3400
+ const cleanup = () => {
3401
+ clearTimeout(timer);
3402
+ signal.removeEventListener("abort", abort);
3403
+ if (this.approvalResolvers.get(callId) === pending) this.approvalResolvers.delete(callId);
3404
+ };
3405
+ const pending = {
3406
+ resolve: (approved) => {
3407
+ cleanup();
3408
+ resolve(approved);
3409
+ },
3410
+ reject: (error) => {
3411
+ cleanup();
3412
+ reject(error);
2820
3413
  }
2821
- }, 6e4);
3414
+ };
3415
+ const abort = () => pending.reject(abortError());
3416
+ const timer = setTimeout(() => pending.resolve(false), 6e4);
3417
+ this.approvalResolvers.set(callId, pending);
3418
+ signal.addEventListener("abort", abort, { once: true });
2822
3419
  });
2823
3420
  }
2824
3421
  // ---- Skill Management ----
@@ -2884,31 +3481,31 @@ ${JSON.stringify(params, null, 2)}`;
2884
3481
  } else {
2885
3482
  prompt = loop.instruction;
2886
3483
  }
2887
- const result = await ctx.agent.chatInner(prompt, ctx.sessionId, { maxTurns: loop.maxTurns ?? 10, tools: loop.tools });
3484
+ const result = await ctx.agent.chatInner(prompt, ctx.sessionId, { maxTurns: loop.maxTurns ?? 10, tools: loop.tools, signal: ctx.signal });
2888
3485
  return { content: [{ type: "text", text: result.reply }] };
2889
3486
  };
2890
3487
  }
2891
3488
  /** Internal chat for agentLoop/workflow/sub-agent execution. Condenses inner messages after completion. */
2892
3489
  async chatInner(prompt, sessionId, options) {
2893
3490
  const session = this.sessionManager.getOrThrow(sessionId);
2894
- const messageCountBefore = session.messages.length;
2895
- const result = await this.chat(prompt, { ...options, _skipLock: true });
2896
- const sessionAfter = this.sessionManager.getOrThrow(sessionId);
2897
- if (sessionAfter.messages.length > messageCountBefore + 2) {
2898
- const innerMessages = sessionAfter.messages.splice(messageCountBefore);
2899
- const lastAssistant = [...innerMessages].reverse().find((m) => m.role === "assistant" && !m.toolCalls?.length);
2900
- if (lastAssistant) {
2901
- sessionAfter.messages.push({ role: "assistant", content: `[Agent loop completed]: ${contentToPreview(lastAssistant.content, 200)}` });
2902
- }
3491
+ const prefix = session.messages.slice();
3492
+ const isToolHandler = prefix[prefix.length - 1]?.role === "assistant" && !!prefix[prefix.length - 1]?.toolCalls?.length;
3493
+ try {
3494
+ const result = await this.chat(prompt, { ...options, sessionId, _skipLock: true });
3495
+ session.messages = prefix;
3496
+ if (!isToolHandler) this.sessionManager.addMessage(sessionId, { role: "assistant", content: `[Agent loop completed]: ${result.reply.slice(0, 200)}` });
3497
+ return result;
3498
+ } finally {
3499
+ if (isToolHandler) session.messages = prefix;
3500
+ session.messageCount = session.messages.length;
2903
3501
  }
2904
- return result;
2905
3502
  }
2906
3503
  // ---- Task Plan Execution ----
2907
3504
  async chatWithPlan(message, options) {
2908
3505
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
2909
3506
  this.currentSessionId = sessionId;
2910
- const controller = new AbortController();
2911
- this.abortControllers.set(sessionId, controller);
3507
+ const execution = this.beginExecution(sessionId, options);
3508
+ const controller = execution.controller;
2912
3509
  const startTime = Date.now();
2913
3510
  let totalInputTokens = 0;
2914
3511
  let totalOutputTokens = 0;
@@ -2922,8 +3519,9 @@ ${JSON.stringify(params, null, 2)}`;
2922
3519
  let plan;
2923
3520
  try {
2924
3521
  const planner = new TaskPlanner(this.llmAdapter);
2925
- plan = await planner.plan(messageText, filteredTools);
3522
+ plan = await planner.plan(messageText, filteredTools, controller.signal);
2926
3523
  } catch (err) {
3524
+ throwIfAborted(controller.signal);
2927
3525
  throw new ACPError(ACPErrorCodes.TASK_PLAN_FAILED, `Task planning failed: ${err.message}`);
2928
3526
  }
2929
3527
  const tracker = new PlanTracker(plan, sessionId, (event, data) => this.emit(event, data));
@@ -2934,13 +3532,14 @@ ${JSON.stringify(params, null, 2)}`;
2934
3532
  const maxRetry = options?.stepRetryCount ?? 1;
2935
3533
  const continueOnFailure = options?.continueOnStepFailure ?? true;
2936
3534
  for (let i = 0; i < plan.steps.length; i++) {
2937
- if (controller.signal.aborted) break;
3535
+ throwIfAborted(controller.signal);
2938
3536
  const step = plan.steps[i];
2939
3537
  if (step.status === "done" || step.status === "skipped") continue;
2940
3538
  tracker.startStep(i);
2941
3539
  let stepResult;
2942
3540
  let succeeded = false;
2943
3541
  for (let attempt = 0; attempt <= maxRetry; attempt++) {
3542
+ throwIfAborted(controller.signal);
2944
3543
  try {
2945
3544
  const result = await this.chatInner(step.goal, sessionId, { maxTurns: options?.maxTurns ?? 10, tools: step.tools });
2946
3545
  stepResult = { output: result.reply, toolCalls: result.executionTrace.reduce((sum, t) => sum + t.toolCalls.length, 0), tokenUsage: result.tokenUsage, duration: result.totalDuration };
@@ -2950,6 +3549,7 @@ ${JSON.stringify(params, null, 2)}`;
2950
3549
  succeeded = true;
2951
3550
  break;
2952
3551
  } catch (err) {
3552
+ throwIfAborted(controller.signal);
2953
3553
  stepResult = { output: "", toolCalls: 0, tokenUsage: { input: 0, output: 0 }, duration: 0, error: err.message };
2954
3554
  plan.steps[i].retryCount = attempt + 1;
2955
3555
  }
@@ -2977,18 +3577,17 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
2977
3577
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: finalReply });
2978
3578
  const chatResult = { reply: finalReply, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
2979
3579
  this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
2980
- await this.persistence.saveSession(session).catch(() => {
2981
- });
3580
+ await this.saveSessionSafely(session);
2982
3581
  return chatResult;
2983
3582
  } finally {
2984
- this.abortControllers.delete(sessionId);
3583
+ execution.cleanup();
2985
3584
  }
2986
3585
  }
2987
3586
  async *chatStreamWithPlan(message, options) {
2988
3587
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
2989
3588
  this.currentSessionId = sessionId;
2990
- const controller = new AbortController();
2991
- this.abortControllers.set(sessionId, controller);
3589
+ const execution = this.beginExecution(sessionId, options);
3590
+ const controller = execution.controller;
2992
3591
  const startTime = Date.now();
2993
3592
  let totalInputTokens = 0;
2994
3593
  let totalOutputTokens = 0;
@@ -3001,7 +3600,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3001
3600
  let plan;
3002
3601
  try {
3003
3602
  const planner = new TaskPlanner(this.llmAdapter);
3004
- plan = await planner.plan(messageText, filteredTools);
3603
+ plan = await planner.plan(messageText, filteredTools, controller.signal);
3005
3604
  } catch (err) {
3006
3605
  yield { type: "error", code: ACPErrorCodes.TASK_PLAN_FAILED, message: `Task planning failed: ${err.message}` };
3007
3606
  return;
@@ -3015,7 +3614,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3015
3614
  const maxRetry = options?.stepRetryCount ?? 1;
3016
3615
  const continueOnFailure = options?.continueOnStepFailure ?? true;
3017
3616
  for (let i = 0; i < plan.steps.length; i++) {
3018
- if (controller.signal.aborted) break;
3617
+ throwIfAborted(controller.signal);
3019
3618
  const step = plan.steps[i];
3020
3619
  if (step.status === "done" || step.status === "skipped") continue;
3021
3620
  tracker.startStep(i);
@@ -3023,6 +3622,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3023
3622
  let stepResult;
3024
3623
  let succeeded = false;
3025
3624
  for (let attempt = 0; attempt <= maxRetry; attempt++) {
3625
+ throwIfAborted(controller.signal);
3026
3626
  try {
3027
3627
  const result = await this.chatInner(step.goal, sessionId, { maxTurns: options?.maxTurns ?? 10, tools: step.tools });
3028
3628
  stepResult = { output: result.reply, toolCalls: result.executionTrace.reduce((sum, t) => sum + t.toolCalls.length, 0), tokenUsage: result.tokenUsage, duration: result.totalDuration };
@@ -3031,6 +3631,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3031
3631
  succeeded = true;
3032
3632
  break;
3033
3633
  } catch (err) {
3634
+ throwIfAborted(controller.signal);
3034
3635
  stepResult = { output: "", toolCalls: 0, tokenUsage: { input: 0, output: 0 }, duration: 0, error: err.message };
3035
3636
  plan.steps[i].retryCount = attempt + 1;
3036
3637
  }
@@ -3058,11 +3659,11 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3058
3659
 
3059
3660
  ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3060
3661
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: finalReply });
3662
+ const saving = this.saveSessionSafely(session);
3061
3663
  yield { type: "done", reply: finalReply, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3062
- await this.persistence.saveSession(session).catch(() => {
3063
- });
3664
+ await saving;
3064
3665
  } finally {
3065
- this.abortControllers.delete(sessionId);
3666
+ execution.cleanup();
3066
3667
  }
3067
3668
  }
3068
3669
  async resumeTask(sessionId, options) {
@@ -3072,6 +3673,8 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3072
3673
  if (!savedPlan) {
3073
3674
  throw new ACPError(ACPErrorCodes.TASK_PLAN_FAILED, "No task plan found in session");
3074
3675
  }
3676
+ const releaseLock = await this.acquireSessionLock(sessionId);
3677
+ this.currentSessionId = sessionId;
3075
3678
  for (const step of savedPlan.steps) {
3076
3679
  if (step.status === "failed") {
3077
3680
  step.status = "pending";
@@ -3079,8 +3682,8 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3079
3682
  step.result = void 0;
3080
3683
  }
3081
3684
  }
3082
- const controller = new AbortController();
3083
- this.abortControllers.set(sessionId, controller);
3685
+ const execution = this.beginExecution(sessionId, options);
3686
+ const controller = execution.controller;
3084
3687
  const startTime = Date.now();
3085
3688
  let totalInputTokens = 0;
3086
3689
  let totalOutputTokens = 0;
@@ -3091,13 +3694,14 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3091
3694
  const maxRetry = options?.stepRetryCount ?? 1;
3092
3695
  const continueOnFailure = options?.continueOnStepFailure ?? true;
3093
3696
  for (let i = 0; i < savedPlan.steps.length; i++) {
3094
- if (controller.signal.aborted) break;
3697
+ throwIfAborted(controller.signal);
3095
3698
  const step = savedPlan.steps[i];
3096
3699
  if (step.status === "done" || step.status === "skipped") continue;
3097
3700
  tracker.startStep(i);
3098
3701
  let stepResult;
3099
3702
  let succeeded = false;
3100
3703
  for (let attempt = 0; attempt <= maxRetry; attempt++) {
3704
+ throwIfAborted(controller.signal);
3101
3705
  try {
3102
3706
  const result = await this.chatInner(step.goal, sessionId, { maxTurns: options?.maxTurns ?? 10, tools: step.tools });
3103
3707
  stepResult = { output: result.reply, toolCalls: result.executionTrace.reduce((sum, t) => sum + t.toolCalls.length, 0), tokenUsage: result.tokenUsage, duration: result.totalDuration };
@@ -3107,6 +3711,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
3107
3711
  succeeded = true;
3108
3712
  break;
3109
3713
  } catch (err) {
3714
+ throwIfAborted(controller.signal);
3110
3715
  stepResult = { output: "", toolCalls: 0, tokenUsage: { input: 0, output: 0 }, duration: 0, error: err.message };
3111
3716
  savedPlan.steps[i].retryCount = attempt + 1;
3112
3717
  }
@@ -3134,11 +3739,11 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
3134
3739
  this.sessionManager.addMessage(sessionId, { role: "assistant", content: finalReply });
3135
3740
  const chatResult = { reply: finalReply, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3136
3741
  this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
3137
- await this.persistence.saveSession(session).catch(() => {
3138
- });
3742
+ await this.saveSessionSafely(session);
3139
3743
  return chatResult;
3140
3744
  } finally {
3141
- this.abortControllers.delete(sessionId);
3745
+ execution.cleanup();
3746
+ releaseLock();
3142
3747
  }
3143
3748
  }
3144
3749
  getTaskPlan(sessionId) {
@@ -3154,8 +3759,8 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
3154
3759
  async chatWithGraph(message, options) {
3155
3760
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
3156
3761
  this.currentSessionId = sessionId;
3157
- const controller = new AbortController();
3158
- this.abortControllers.set(sessionId, controller);
3762
+ const execution = this.beginExecution(sessionId, options);
3763
+ const controller = execution.controller;
3159
3764
  const startTime = Date.now();
3160
3765
  try {
3161
3766
  const userContent = this.buildUserContent(message, options?.attachments);
@@ -3166,8 +3771,9 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
3166
3771
  let plan;
3167
3772
  try {
3168
3773
  const planner = new GraphPlanner(this.llmAdapter);
3169
- plan = await planner.planGraph(messageText, filteredTools);
3774
+ plan = await planner.planGraph(messageText, filteredTools, controller.signal);
3170
3775
  } catch (err) {
3776
+ throwIfAborted(controller.signal);
3171
3777
  throw new ACPError(ACPErrorCodes.GRAPH_PLAN_FAILED, `Graph planning failed: ${err.message}`);
3172
3778
  }
3173
3779
  const graph = new TaskGraph(plan.nodes);
@@ -3181,6 +3787,7 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
3181
3787
  const executor = new GraphExecutor({
3182
3788
  graph,
3183
3789
  parentAgent: this,
3790
+ parentSignal: controller.signal,
3184
3791
  plan,
3185
3792
  maxConcurrency: options?.graphConfig?.maxConcurrency ?? 3,
3186
3793
  maxDepth: options?.graphConfig?.maxDepth ?? 2,
@@ -3188,6 +3795,7 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
3188
3795
  emit: (event, data) => this.emit(event, data)
3189
3796
  });
3190
3797
  const finalPlan = await executor.execute();
3798
+ throwIfAborted(controller.signal);
3191
3799
  const resultLines = Object.entries(finalPlan.results).filter(([, r]) => r.status === "done" && r.output).map(([nodeId, r]) => `[${nodeId}] ${r.output.slice(0, 200)}`);
3192
3800
  const progress = executor.getProgress();
3193
3801
  const finalReply = resultLines.length > 0 ? `Graph task completed (${progress.done}/${progress.total} nodes done, ${progress.failed} failed, ${progress.bypassed} bypassed).
@@ -3206,18 +3814,17 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3206
3814
  totalDuration: Date.now() - startTime
3207
3815
  };
3208
3816
  this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
3209
- await this.persistence.saveSession(session).catch(() => {
3210
- });
3817
+ await this.saveSessionSafely(session);
3211
3818
  return chatResult;
3212
3819
  } finally {
3213
- this.abortControllers.delete(sessionId);
3820
+ execution.cleanup();
3214
3821
  }
3215
3822
  }
3216
3823
  async *chatStreamWithGraph(message, options) {
3217
3824
  const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
3218
3825
  this.currentSessionId = sessionId;
3219
- const controller = new AbortController();
3220
- this.abortControllers.set(sessionId, controller);
3826
+ const execution = this.beginExecution(sessionId, options);
3827
+ const controller = execution.controller;
3221
3828
  const startTime = Date.now();
3222
3829
  let totalInputTokens = 0;
3223
3830
  let totalOutputTokens = 0;
@@ -3230,7 +3837,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3230
3837
  let plan;
3231
3838
  try {
3232
3839
  const planner = new GraphPlanner(this.llmAdapter);
3233
- plan = await planner.planGraph(messageText, filteredTools);
3840
+ plan = await planner.planGraph(messageText, filteredTools, controller.signal);
3234
3841
  } catch (err) {
3235
3842
  yield { type: "error", code: ACPErrorCodes.GRAPH_PLAN_FAILED, message: `Graph planning failed: ${err.message}` };
3236
3843
  return;
@@ -3247,6 +3854,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3247
3854
  const executor = new GraphExecutor({
3248
3855
  graph,
3249
3856
  parentAgent: this,
3857
+ parentSignal: controller.signal,
3250
3858
  plan,
3251
3859
  maxConcurrency: options?.graphConfig?.maxConcurrency ?? 3,
3252
3860
  maxDepth: options?.graphConfig?.maxDepth ?? 2,
@@ -3258,6 +3866,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3258
3866
  }
3259
3867
  });
3260
3868
  const finalPlan = await executor.execute();
3869
+ throwIfAborted(controller.signal);
3261
3870
  const progress = executor.getProgress();
3262
3871
  const resultLines = Object.entries(finalPlan.results).filter(([, r]) => r.status === "done" && r.output).map(([nodeId, r]) => `[${nodeId}] ${r.output.slice(0, 200)}`);
3263
3872
  const finalReply = resultLines.length > 0 ? `Graph task completed (${progress.done}/${progress.total} nodes done, ${progress.failed} failed, ${progress.bypassed} bypassed).
@@ -3269,11 +3878,11 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3269
3878
  const session = this.sessionManager.getOrThrow(sessionId);
3270
3879
  session.meta = session.meta ?? {};
3271
3880
  session.meta.graphPlan = finalPlan;
3881
+ const saving = this.saveSessionSafely(session);
3272
3882
  yield { type: "done", reply: finalReply, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
3273
- await this.persistence.saveSession(session).catch(() => {
3274
- });
3883
+ await saving;
3275
3884
  } finally {
3276
- this.abortControllers.delete(sessionId);
3885
+ execution.cleanup();
3277
3886
  }
3278
3887
  }
3279
3888
  /** Resume a graph execution from checkpoint */
@@ -3284,6 +3893,8 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3284
3893
  if (!savedPlan) {
3285
3894
  throw new ACPError(ACPErrorCodes.GRAPH_PLAN_FAILED, "No graph plan found in session");
3286
3895
  }
3896
+ const releaseLock = await this.acquireSessionLock(sessionId);
3897
+ this.currentSessionId = sessionId;
3287
3898
  for (const [nodeId, result] of Object.entries(savedPlan.results)) {
3288
3899
  if (result.status === "failed" || result.status === "bypassed") {
3289
3900
  savedPlan.results[nodeId] = {
@@ -3296,14 +3907,15 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3296
3907
  }
3297
3908
  }
3298
3909
  savedPlan.status = "executing";
3299
- const controller = new AbortController();
3300
- this.abortControllers.set(sessionId, controller);
3910
+ const execution = this.beginExecution(sessionId, options);
3911
+ const controller = execution.controller;
3301
3912
  const startTime = Date.now();
3302
3913
  try {
3303
3914
  const graph = new TaskGraph(savedPlan.nodes);
3304
3915
  const executor = new GraphExecutor({
3305
3916
  graph,
3306
3917
  parentAgent: this,
3918
+ parentSignal: controller.signal,
3307
3919
  plan: savedPlan,
3308
3920
  maxConcurrency: options?.graphConfig?.maxConcurrency ?? 3,
3309
3921
  maxDepth: options?.graphConfig?.maxDepth ?? 2,
@@ -3311,6 +3923,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
3311
3923
  emit: (event, data) => this.emit(event, data)
3312
3924
  });
3313
3925
  const finalPlan = await executor.execute();
3926
+ throwIfAborted(controller.signal);
3314
3927
  const resultLines = Object.entries(finalPlan.results).filter(([, r]) => r.status === "done" && r.output).map(([nodeId, r]) => `[${nodeId}] ${r.output.slice(0, 200)}`);
3315
3928
  const progress = executor.getProgress();
3316
3929
  const finalReply = resultLines.length > 0 ? `Graph resumed & completed (${progress.done}/${progress.total} nodes done).
@@ -3327,11 +3940,11 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3327
3940
  totalDuration: Date.now() - startTime
3328
3941
  };
3329
3942
  this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
3330
- await this.persistence.saveSession(session).catch(() => {
3331
- });
3943
+ await this.saveSessionSafely(session);
3332
3944
  return chatResult;
3333
3945
  } finally {
3334
- this.abortControllers.delete(sessionId);
3946
+ execution.cleanup();
3947
+ releaseLock();
3335
3948
  }
3336
3949
  }
3337
3950
  /** Get graph plan from session */
@@ -3352,6 +3965,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3352
3965
  const failedSteps = [];
3353
3966
  const total = workflow.steps.length;
3354
3967
  for (let i = 0; i < workflow.steps.length; i++) {
3968
+ throwIfAborted(ctx.signal);
3355
3969
  const step = workflow.steps[i];
3356
3970
  const interpolatedPrompt = interpolateTemplate(step.prompt, {
3357
3971
  inputs: params,
@@ -3368,7 +3982,8 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3368
3982
  try {
3369
3983
  const result = await ctx.agent.chatInner(interpolatedPrompt, ctx.sessionId, {
3370
3984
  maxTurns: step.maxTurns ?? 10,
3371
- tools: step.tools
3985
+ tools: step.tools,
3986
+ signal: ctx.signal
3372
3987
  });
3373
3988
  stepOutputs[step.name] = { output: result.reply };
3374
3989
  completedSteps.push(step.name);
@@ -3453,6 +4068,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3453
4068
  const executor = new GraphExecutor({
3454
4069
  graph,
3455
4070
  parentAgent: this,
4071
+ parentSignal: ctx.signal,
3456
4072
  plan,
3457
4073
  maxConcurrency: 3,
3458
4074
  maxDepth: 2,
@@ -3460,6 +4076,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3460
4076
  emit: (event, data) => this.emit(event, data)
3461
4077
  });
3462
4078
  const finalPlan = await executor.execute();
4079
+ throwIfAborted(ctx.signal);
3463
4080
  const aggregation = skill.subgraph?.aggregation ?? "concat";
3464
4081
  if (aggregation === "last") {
3465
4082
  const lastDone = Object.values(finalPlan.results).reverse().find((r) => r.status === "done" && r.output);
@@ -3498,6 +4115,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3498
4115
  const executor = new GraphExecutor({
3499
4116
  graph,
3500
4117
  parentAgent: this,
4118
+ parentSignal: ctx.signal,
3501
4119
  plan,
3502
4120
  maxConcurrency: 3,
3503
4121
  maxDepth: 2,
@@ -3505,6 +4123,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3505
4123
  emit: (event, data) => this.emit(event, data)
3506
4124
  });
3507
4125
  const finalPlan = await executor.execute();
4126
+ throwIfAborted(ctx.signal);
3508
4127
  const aggregation = subgraphDef.aggregation ?? "concat";
3509
4128
  if (aggregation === "last") {
3510
4129
  const lastDone = Object.values(finalPlan.results).reverse().find((r) => r.status === "done" && r.output);
@@ -3523,7 +4142,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3523
4142
  // ---- Session Management ----
3524
4143
  createSession(options) {
3525
4144
  const session = this.sessionManager.create(options);
3526
- this.currentSessionId = session.id;
4145
+ if (options?.activate !== false) this.currentSessionId = session.id;
3527
4146
  return session;
3528
4147
  }
3529
4148
  getSession(sessionId) {
@@ -3536,17 +4155,75 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3536
4155
  return this.sessionManager.search(query, options);
3537
4156
  }
3538
4157
  mergeSessions(fromId, toId, strategy) {
4158
+ this.ensureSessionIsNotBeingManaged(fromId);
4159
+ this.ensureSessionIsNotBeingManaged(toId);
3539
4160
  this.sessionManager.merge(fromId, toId, strategy);
3540
4161
  }
3541
4162
  deleteSession(sessionId) {
4163
+ this.ensureSessionIsNotBeingManaged(sessionId);
4164
+ this.removeLiveSession(sessionId);
4165
+ }
4166
+ removeLiveSession(sessionId) {
3542
4167
  this.sessionManager.delete(sessionId);
3543
4168
  if (this.currentSessionId === sessionId) {
3544
4169
  this.currentSessionId = null;
3545
4170
  }
3546
4171
  }
3547
4172
  archiveSession(sessionId) {
4173
+ this.ensureSessionIsNotBeingManaged(sessionId);
3548
4174
  this.sessionManager.archive(sessionId);
3549
4175
  }
4176
+ /** List committed history without loading it into the Agent. */
4177
+ async listStoredSessions(options) {
4178
+ await Promise.all(this.sessionStorageQueues.values());
4179
+ return (await this.persistence.listSessions(options)).map((session) => this.snapshotSession(session));
4180
+ }
4181
+ /** Load one stored session on demand. A live session takes precedence. */
4182
+ async restoreSession(sessionId, options) {
4183
+ return this.manageSessionStorage([sessionId], async () => {
4184
+ const session = await this.queueSessionStorage(sessionId, () => this.readSessionForStorage(sessionId));
4185
+ if (!session) return null;
4186
+ this.sessionManager.restore(session);
4187
+ if (options?.activate !== false) this.currentSessionId = sessionId;
4188
+ return session;
4189
+ });
4190
+ }
4191
+ /** Archive a stored or live session, publishing the update after commit. */
4192
+ async archiveStoredSession(sessionId) {
4193
+ return this.manageSessionStorage([sessionId], () => this.queueSessionStorage(sessionId, async () => {
4194
+ const session = await this.requireSessionForStorage(sessionId);
4195
+ await this.commitStoredSession(session, { ...session, status: "archived", updatedAt: Date.now() });
4196
+ }));
4197
+ }
4198
+ /** Commit an idle session, then release its live copy. Restore it by ID when needed. */
4199
+ async unloadSession(sessionId) {
4200
+ return this.manageSessionStorage([sessionId], () => this.queueSessionStorage(sessionId, async () => {
4201
+ const session = this.sessionManager.get(sessionId);
4202
+ if (!session) return false;
4203
+ await this.persistence.saveSession(this.snapshotSession(session));
4204
+ this.removeLiveSession(sessionId);
4205
+ return true;
4206
+ }));
4207
+ }
4208
+ /** Merge history into the target session and commit the target. */
4209
+ async mergeStoredSessions(fromId, toId, strategy) {
4210
+ return this.manageSessionStorage([fromId, toId], () => this.queueSessionStorage(toId, async () => {
4211
+ const from = await this.requireSessionForStorage(fromId);
4212
+ const to = fromId === toId ? from : await this.requireSessionForStorage(toId);
4213
+ const updated = { ...to, messages: [...to.messages] };
4214
+ SessionManager.mergeContent(from, updated, strategy);
4215
+ await this.commitStoredSession(to, updated);
4216
+ }));
4217
+ }
4218
+ /** Delete committed and live history, including when a chat save is pending. */
4219
+ async deleteStoredSession(sessionId) {
4220
+ return this.manageSessionStorage([sessionId], (state) => this.queueSessionStorage(sessionId, async () => {
4221
+ await this.persistence.deleteSession(sessionId);
4222
+ state.deleted = true;
4223
+ this.abortControllers.get(sessionId)?.abort();
4224
+ this.removeLiveSession(sessionId);
4225
+ }), { allowRunning: true });
4226
+ }
3550
4227
  // ---- Context Management ----
3551
4228
  getContext(sessionId, format = "window") {
3552
4229
  const session = this.sessionManager.getOrThrow(sessionId);
@@ -3561,25 +4238,56 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3561
4238
  }
3562
4239
  return this.contextManager.search(allMessages, query, options);
3563
4240
  }
3564
- compressContext(sessionId, strategy) {
4241
+ async compressContext(sessionId, strategy) {
4242
+ this.ensureSessionIsNotBeingManaged(sessionId);
3565
4243
  const session = this.sessionManager.getOrThrow(sessionId);
3566
- const result = this.contextManager.compress(session.messages, strategy);
4244
+ this.compressingSessions.set(sessionId, (this.compressingSessions.get(sessionId) ?? 0) + 1);
4245
+ try {
4246
+ const result = await this.contextManager.compress(session.messages, strategy);
4247
+ session.messages = result.messages;
4248
+ session.tokenCount = result.afterTokens;
4249
+ session.updatedAt = Date.now();
4250
+ if (result.afterTokens < result.beforeTokens) {
4251
+ this.emit("context:compress", { sessionId, beforeTokens: result.beforeTokens, afterTokens: result.afterTokens, reason: "manual" });
4252
+ }
4253
+ return { beforeTokens: result.beforeTokens, afterTokens: result.afterTokens, compressionRatio: result.afterTokens / (result.beforeTokens || 1) };
4254
+ } finally {
4255
+ const remaining = this.compressingSessions.get(sessionId) - 1;
4256
+ if (remaining) this.compressingSessions.set(sessionId, remaining);
4257
+ else this.compressingSessions.delete(sessionId);
4258
+ }
4259
+ }
4260
+ async prepareOverflowRetry(session, rejected, systemPrompt, tools, signal) {
4261
+ const result = await this.contextManager.compressForOverflow(session.messages, rejected, systemPrompt, tools, signal);
4262
+ if (!result) return;
4263
+ throwIfAborted(signal);
3567
4264
  session.messages = result.messages;
3568
4265
  session.tokenCount = result.afterTokens;
3569
4266
  session.updatedAt = Date.now();
3570
- this.emit("context:compress", { sessionId, beforeTokens: result.beforeTokens, afterTokens: result.afterTokens });
3571
- return { beforeTokens: result.beforeTokens, afterTokens: result.afterTokens, compressionRatio: result.afterTokens / (result.beforeTokens || 1) };
4267
+ this.emit("context:compress", {
4268
+ sessionId: session.id,
4269
+ beforeTokens: rejected.usedTokens,
4270
+ afterTokens: result.window.usedTokens,
4271
+ reason: "manual"
4272
+ });
4273
+ return result.window.messages;
3572
4274
  }
3573
4275
  // ---- Runtime Config ----
3574
4276
  configure(config) {
4277
+ if (this.config.llmProxyMode !== void 0 || config.llmProxyMode !== void 0) {
4278
+ this.configureProxy(config);
4279
+ return;
4280
+ }
3575
4281
  if (config.llm) {
3576
4282
  Object.assign(this.config.llm, config.llm);
3577
4283
  this.llmAdapter = new OpenAIAdapter(this.config.llm);
3578
4284
  this.contextManager.setModel(this.config.llm.model);
4285
+ this.contextManager.setSummarizer(createLLMSummarizer(this.llmAdapter));
3579
4286
  }
3580
4287
  if (config.serverUrl) {
3581
4288
  this.config.serverUrl = config.serverUrl;
3582
4289
  this.transport = new HttpTransport(config.serverUrl, this.config.authToken, this.config.headers);
4290
+ this.mcpStatus = "unchecked";
3583
4291
  }
3584
4292
  if (config.authToken) {
3585
4293
  this.config.authToken = config.authToken;
@@ -3588,6 +4296,33 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3588
4296
  this.config.systemPrompt = config.systemPrompt;
3589
4297
  }
3590
4298
  }
4299
+ configureProxy(config) {
4300
+ const current = this.config;
4301
+ const next = {
4302
+ ...current,
4303
+ llm: { ...current.llm, ...config.llm },
4304
+ llmProxyMode: config.llmProxyMode === void 0 ? current.llmProxyMode : config.llmProxyMode,
4305
+ serverUrl: config.serverUrl === void 0 ? current.serverUrl : config.serverUrl,
4306
+ authToken: config.authToken ?? current.authToken,
4307
+ headers: config.headers ?? current.headers,
4308
+ systemPrompt: config.systemPrompt ?? current.systemPrompt
4309
+ };
4310
+ const { llm, serverUrl } = resolveProxyConfig(next);
4311
+ const transportChanged = next.serverUrl !== current.serverUrl || next.authToken !== current.authToken || next.headers !== current.headers || current.llmProxyMode === void 0;
4312
+ this.config = next;
4313
+ if (config.llm || next.llmProxyMode !== current.llmProxyMode || next.llmProxyMode === "listdoing-server" && (next.serverUrl !== current.serverUrl || next.authToken !== current.authToken)) {
4314
+ this.llmAdapter = new OpenAIAdapter(llm, { rejectRedirects: true });
4315
+ this.contextManager.setModel(llm.model);
4316
+ this.contextManager.setSummarizer(createLLMSummarizer(this.llmAdapter));
4317
+ }
4318
+ if (transportChanged) {
4319
+ this.transport = serverUrl ? new HttpTransport(serverUrl, next.authToken, next.headers) : new LocalTransport();
4320
+ this.mcpStatus = "unchecked";
4321
+ for (const skill of this.skillRegistry.list()) {
4322
+ if (this.discoveredServerSkills.has(skill)) this.skillRegistry.unregister(skill.name);
4323
+ }
4324
+ }
4325
+ }
3591
4326
  // ---- Events ----
3592
4327
  on(event, handler) {
3593
4328
  if (!this.eventHandlers.has(event)) {
@@ -3608,10 +4343,15 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3608
4343
  }
3609
4344
  // ---- Private Helpers ----
3610
4345
  /**
3611
- * Stream LLM response token-by-token via the adapter's chatStream.
3612
- * Forwards llm_text events to the caller and collects the full StandardLLMResponse.
4346
+ * Forward streaming events, or emit the complete response for a non-streaming adapter.
3613
4347
  */
3614
4348
  async *streamLlmResponse(messages, tools, signal) {
4349
+ if (typeof this.llmAdapter.chatStream !== "function") {
4350
+ const response = await this.llmAdapter.chat(messages, tools, { signal });
4351
+ if (response.reasoning) yield { type: "llm_thinking", content: response.reasoning };
4352
+ if (response.content) yield { type: "llm_text", content: response.content };
4353
+ return response;
4354
+ }
3615
4355
  let fullResponse = null;
3616
4356
  for await (const event of this.llmAdapter.chatStream(messages, tools, { signal })) {
3617
4357
  if (event.type === "llm_text" || event.type === "llm_thinking") {
@@ -3705,59 +4445,53 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3705
4445
  });
3706
4446
  }
3707
4447
  // ── Parallel tool execution helpers ──
3708
- planToolExecution(toolCalls, chatOptions, requireApproval) {
4448
+ planToolExecution(toolCalls, chatOptions, requireApproval, runContext, exposedTools) {
3709
4449
  const items = toolCalls.map((tc) => {
3710
4450
  const isMcp = this.mcpRouter.isMcpTool(tc.name);
3711
- const skillDef = this.skillRegistry.get(tc.name);
4451
+ const scopedSkill = runContext?.registry.get(tc.name);
4452
+ const skillDef = scopedSkill ?? this.skillRegistry.get(tc.name);
3712
4453
  const location = isMcp ? "server" : skillDef?.location === "server" ? "server" : "local";
3713
- const permPolicy = this.checkToolPermission(skillDef, tc.arguments, chatOptions, requireApproval);
4454
+ const permPolicy = scopedSkill && !exposedTools.some((tool) => tool.name === tc.name) ? "disabled" : this.checkToolPermission(skillDef, tc.arguments, chatOptions, requireApproval);
3714
4455
  const parallelSafe = skillDef?.parallelSafe ?? true;
3715
4456
  return { toolCall: tc, skillDef, location, permPolicy, parallelSafe };
3716
4457
  });
3717
4458
  const canParallelize = items.every((item) => item.parallelSafe);
3718
4459
  return { canParallelize, items };
3719
4460
  }
3720
- async processApprovalQueue(items, sessionId, emit, yieldEvent) {
4461
+ async *processApprovalQueue(items, sessionId, signal) {
3721
4462
  for (const item of items) {
3722
- if (item.permPolicy === "disabled") {
3723
- const denyResult = {
3724
- content: [{ type: "text", text: `Tool "${item.toolCall.name}" is disabled in current permission mode.` }],
3725
- isError: true
3726
- };
3727
- item.resolved = true;
3728
- item.result = denyResult;
3729
- this.addToolResult(sessionId, item.toolCall, denyResult);
3730
- emit("tool:result", { sessionId, toolName: item.toolCall.name, isError: true, summary: "Permission denied (disabled)", duration: 0 });
3731
- if (yieldEvent) {
3732
- yieldEvent({ type: "tool_result", callId: item.toolCall.id, toolName: item.toolCall.name, isError: true, summary: "Permission denied (disabled)", duration: 0 });
3733
- }
3734
- } else if (item.permPolicy === "approval" || item.permPolicy === "force-approval") {
3735
- emit("tool:approval", { sessionId, toolName: item.toolCall.name, arguments: item.toolCall.arguments, callId: item.toolCall.id });
3736
- if (yieldEvent) {
3737
- yieldEvent({ type: "tool_approval", callId: item.toolCall.id, toolName: item.toolCall.name, arguments: item.toolCall.arguments });
3738
- }
3739
- const approved = await this.waitForApproval(item.toolCall.id);
3740
- if (!approved) {
3741
- const rejectResult = {
3742
- content: [{ type: "text", text: "Tool call rejected by user." }],
3743
- isError: true
3744
- };
3745
- item.resolved = true;
3746
- item.result = rejectResult;
3747
- this.addToolResult(sessionId, item.toolCall, rejectResult);
3748
- emit("tool:result", { sessionId, toolName: item.toolCall.name, isError: true, summary: "Rejected", duration: 0 });
3749
- if (yieldEvent) {
3750
- yieldEvent({ type: "tool_result", callId: item.toolCall.id, toolName: item.toolCall.name, isError: true, summary: "Rejected", duration: 0 });
3751
- }
4463
+ throwIfAborted(signal);
4464
+ const tc = item.toolCall;
4465
+ this.emit("tool:call", { sessionId, toolName: tc.name, arguments: tc.arguments, location: item.location });
4466
+ yield { type: "tool_call", callId: tc.id, toolName: tc.name, arguments: tc.arguments, location: item.location };
4467
+ let reason;
4468
+ if (tc.parseError) reason = `Invalid tool arguments: ${tc.parseError}`;
4469
+ else if (item.permPolicy === "disabled") reason = `Tool "${tc.name}" is disabled in current permission mode.`;
4470
+ else if (item.permPolicy === "approval" || item.permPolicy === "force-approval") {
4471
+ const decision = this.waitForApproval(tc.id, signal);
4472
+ decision.catch(() => {
4473
+ });
4474
+ this.emit("tool:approval", { sessionId, toolName: tc.name, arguments: tc.arguments, callId: tc.id });
4475
+ yield { type: "tool_approval", callId: tc.id, toolName: tc.name, arguments: tc.arguments };
4476
+ try {
4477
+ if (!await decision) reason = "Tool call rejected by user.";
4478
+ } finally {
4479
+ this.approvalResolvers.get(tc.id)?.resolve(false);
3752
4480
  }
3753
4481
  }
4482
+ throwIfAborted(signal);
4483
+ if (reason) {
4484
+ const result = { content: [{ type: "text", text: reason }], isError: true };
4485
+ item.resolved = true;
4486
+ item.result = result;
4487
+ this.addToolResult(sessionId, tc, result);
4488
+ this.emit("tool:result", { sessionId, toolName: tc.name, isError: true, summary: reason, duration: 0 });
4489
+ yield { type: "tool_result", callId: tc.id, toolName: tc.name, isError: true, summary: reason, duration: 0 };
4490
+ }
3754
4491
  }
3755
4492
  }
3756
4493
  async executeToolBatch(items, sessionId, controller, canParallelize, emit) {
3757
4494
  const traceEntries = [];
3758
- for (const item of items) {
3759
- emit("tool:call", { sessionId, toolName: item.toolCall.name, arguments: item.toolCall.arguments, location: item.location });
3760
- }
3761
4495
  for (const item of items) {
3762
4496
  if (item.resolved) {
3763
4497
  const summary = item.result?.content.filter((c) => c.type === "text").map((c) => c.text.slice(0, 100)).join(" ") ?? "";
@@ -3813,18 +4547,41 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3813
4547
  traceEntries.push({ callId: item.toolCall.id, toolName: item.toolCall.name, location: item.location, arguments: item.toolCall.arguments, result: { isError: result.isError ?? false, summary }, duration });
3814
4548
  }
3815
4549
  }
4550
+ throwIfAborted(controller.signal);
3816
4551
  return traceEntries;
3817
4552
  }
3818
- async executeToolBatchStream(items, sessionId, controller, canParallelize, yieldEvent) {
3819
- const traceEntries = [];
3820
- for (const item of items) {
3821
- yieldEvent({ type: "tool_call", callId: item.toolCall.id, toolName: item.toolCall.name, arguments: item.toolCall.arguments, location: item.location });
3822
- if (item.resolved) {
3823
- const summary = item.result?.content.filter((c) => c.type === "text").map((c) => c.text.slice(0, 100)).join(" ") ?? "";
3824
- yieldEvent({ type: "tool_result", callId: item.toolCall.id, toolName: item.toolCall.name, isError: true, summary, duration: 0 });
3825
- traceEntries.push({ callId: item.toolCall.id, toolName: item.toolCall.name, location: item.location, arguments: item.toolCall.arguments, result: { isError: true, summary }, duration: 0 });
4553
+ async *streamToolBatch(items, sessionId, controller, parallel) {
4554
+ const events = [];
4555
+ let wake;
4556
+ let done = false;
4557
+ let failure;
4558
+ const task = this.executeToolBatchStream(items, sessionId, controller, parallel, (event) => {
4559
+ events.push(event);
4560
+ wake?.();
4561
+ }).then(() => {
4562
+ done = true;
4563
+ wake?.();
4564
+ }, (error) => {
4565
+ failure = error;
4566
+ done = true;
4567
+ wake?.();
4568
+ });
4569
+ try {
4570
+ while (!done || events.length) {
4571
+ if (events.length) yield events.shift();
4572
+ else await new Promise((resolve) => {
4573
+ wake = resolve;
4574
+ });
3826
4575
  }
4576
+ if (failure) throw failure;
4577
+ throwIfAborted(controller.signal);
4578
+ } finally {
4579
+ if (!done) controller.abort();
4580
+ await task;
3827
4581
  }
4582
+ }
4583
+ async executeToolBatchStream(items, sessionId, controller, canParallelize, yieldEvent) {
4584
+ const traceEntries = [];
3828
4585
  const executableItems = items.filter((item) => !item.resolved);
3829
4586
  if (executableItems.length === 0) return traceEntries;
3830
4587
  if (executableItems.length === 1) {
@@ -3874,6 +4631,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3874
4631
  traceEntries.push({ callId: item.toolCall.id, toolName: item.toolCall.name, location: item.location, arguments: item.toolCall.arguments, result: { isError: result.isError ?? false, summary }, duration });
3875
4632
  }
3876
4633
  }
4634
+ throwIfAborted(controller.signal);
3877
4635
  return traceEntries;
3878
4636
  }
3879
4637
  /**
@@ -3881,28 +4639,34 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3881
4639
  */
3882
4640
  async executeToolCall(toolCall, sessionId, controller) {
3883
4641
  const execStart = Date.now();
4642
+ throwIfAborted(controller.signal);
4643
+ if (toolCall.parseError) return { content: [{ type: "text", text: toolCall.parseError }], isError: true };
3884
4644
  if (this.mcpRouter.isMcpTool(toolCall.name)) {
3885
4645
  try {
3886
- const result = await this.mcpRouter.call(toolCall.name, toolCall.arguments);
3887
- return { content: [{ type: "text", text: typeof result === "string" ? result : JSON.stringify(result) }] };
4646
+ const result = await this.mcpRouter.call(toolCall.name, toolCall.arguments, { signal: controller.signal });
4647
+ throwIfAborted(controller.signal);
4648
+ return { content: [{ type: "text", text: typeof result === "string" ? result : JSON.stringify(result) ?? "" }] };
3888
4649
  } catch (err) {
3889
- return { content: [{ type: "text", text: `MCP error: ${err.message}` }], isError: true };
4650
+ throwIfAborted(controller.signal);
4651
+ return { content: [{ type: "text", text: `MCP error: ${errorMessage(err)}` }], isError: true };
3890
4652
  }
3891
4653
  }
3892
- const skillDef = this.skillRegistry.get(toolCall.name);
4654
+ const scope = this.runContexts.get(controller.signal);
4655
+ const skillDef = scope?.registry.get(toolCall.name) ?? this.skillRegistry.get(toolCall.name);
3893
4656
  const location = skillDef?.location === "server" ? "server" : "local";
3894
4657
  try {
3895
4658
  if (location === "server" && this.transport instanceof HttpTransport) {
3896
- return await this.executeServerSkill(toolCall);
4659
+ return await this.executeServerSkill(toolCall, controller.signal);
3897
4660
  } else {
3898
- const invokeFn = (skillName, params) => this.skillExecutor.invokeDirect(skillName, params, {
4661
+ const executorFor = (name) => scope?.registry.has(name) ? scope.executor : this.skillExecutor;
4662
+ const invokeFn = (skillName, params) => executorFor(skillName).invokeDirect(skillName, params, {
3899
4663
  sessionId,
3900
4664
  agent: this,
3901
4665
  signal: controller.signal,
3902
4666
  depth: 0,
3903
4667
  invoke: invokeFn
3904
4668
  });
3905
- return await this.skillExecutor.execute(toolCall.name, toolCall.arguments, {
4669
+ return await executorFor(toolCall.name).execute(toolCall.name, toolCall.arguments, {
3906
4670
  sessionId,
3907
4671
  agent: this,
3908
4672
  signal: controller.signal,
@@ -3911,14 +4675,143 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3911
4675
  });
3912
4676
  }
3913
4677
  } catch (err) {
3914
- return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
4678
+ if (controller.signal.aborted || err?.name === "AbortError") throw abortError();
4679
+ return { content: [{ type: "text", text: `Error: ${errorMessage(err)}` }], isError: true };
4680
+ }
4681
+ }
4682
+ prepareRunContext(signal, context) {
4683
+ if (!context) return this.runContexts.get(signal);
4684
+ const registry = new SkillRegistry();
4685
+ for (const skill of context.skills ?? []) {
4686
+ if (!skill.handler || skill.location === "server" || this.mcpRouter.isMcpTool(skill.name) || this.skillRegistry.has(skill.name) || registry.has(skill.name)) {
4687
+ throw new ACPError(ACPErrorCodes.SKILL_REGISTRATION_FAILED, `Invalid or conflicting run-local skill: ${skill.name}`);
4688
+ }
4689
+ registry.register({ ...skill });
4690
+ }
4691
+ const scope = {
4692
+ systemPromptSuffix: context.systemPromptSuffix,
4693
+ registry,
4694
+ executor: new SkillExecutor(registry, void 0, { validateInputs: this.config.skills?.validateInputs })
4695
+ };
4696
+ this.runContexts.set(signal, scope);
4697
+ return scope;
4698
+ }
4699
+ buildRunSystemPrompt(session, runContext) {
4700
+ const base = this.resolveSystemPrompt(session.id, session.systemPrompt ?? this.config.systemPrompt ?? "");
4701
+ return runContext?.systemPromptSuffix ? [base, runContext.systemPromptSuffix].filter(Boolean).join("\n\n") : base;
4702
+ }
4703
+ listRunTools(runContext) {
4704
+ const tools = this.skillRegistry.listToolDefs();
4705
+ if (!runContext) return tools;
4706
+ return [...tools.filter((tool) => !runContext.registry.has(tool.name)), ...runContext.registry.listToolDefs()];
4707
+ }
4708
+ beginExecution(sessionId, options) {
4709
+ const previous = this.abortControllers.get(sessionId);
4710
+ const controller = new AbortController();
4711
+ const unlinkRoot = linkAbort(previous?.signal, controller);
4712
+ const unlinkParent = linkAbort(options?.signal, controller);
4713
+ const inherited = previous && this.runContexts.get(previous.signal);
4714
+ if (inherited) this.runContexts.set(controller.signal, inherited);
4715
+ if (!previous) this.abortControllers.set(sessionId, controller);
4716
+ return { controller, cleanup: () => {
4717
+ controller.abort();
4718
+ this.runContexts.delete(controller.signal);
4719
+ unlinkRoot();
4720
+ unlinkParent();
4721
+ if (this.abortControllers.get(sessionId) === controller) this.abortControllers.delete(sessionId);
4722
+ } };
4723
+ }
4724
+ ensureSessionIsNotBeingManaged(sessionId) {
4725
+ if (this.managingSessions.has(sessionId)) {
4726
+ throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${sessionId} has a pending storage operation`);
4727
+ }
4728
+ }
4729
+ async manageSessionStorage(sessionIds, operation, options) {
4730
+ for (const id of sessionIds) {
4731
+ this.ensureSessionIsNotBeingManaged(id);
4732
+ if (this.compressingSessions.has(id)) {
4733
+ throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${id} is compressing its context`);
4734
+ }
4735
+ if (!options?.allowRunning && (this.runningSessions.has(id) || this.abortControllers.has(id))) {
4736
+ throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${id} is already running a chat`);
4737
+ }
4738
+ }
4739
+ const state = { deleted: false };
4740
+ for (const id of sessionIds) this.managingSessions.set(id, state);
4741
+ try {
4742
+ return await operation(state);
4743
+ } finally {
4744
+ for (const id of sessionIds) this.managingSessions.delete(id);
3915
4745
  }
3916
4746
  }
4747
+ async readSessionForStorage(sessionId) {
4748
+ const current = this.sessionManager.get(sessionId);
4749
+ if (current) return current;
4750
+ const stored = await this.persistence.loadSession(sessionId);
4751
+ return stored ? this.snapshotSession(stored) : null;
4752
+ }
4753
+ async requireSessionForStorage(sessionId) {
4754
+ const session = await this.readSessionForStorage(sessionId);
4755
+ if (session) return session;
4756
+ throw new ACPError(ACPErrorCodes.SESSION_NOT_FOUND, `Session not found: ${sessionId}`, { sessionId });
4757
+ }
4758
+ async commitStoredSession(original, updated) {
4759
+ await this.persistence.saveSession(this.snapshotSession(updated));
4760
+ if (this.sessionManager.get(original.id) === original) Object.assign(original, updated);
4761
+ }
4762
+ snapshotSession(session) {
4763
+ try {
4764
+ return structuredClone(session);
4765
+ } catch {
4766
+ return { ...session, messages: session.messages.map((message) => ({ ...message })) };
4767
+ }
4768
+ }
4769
+ queueSessionStorage(sessionId, operation) {
4770
+ const previous = this.sessionStorageQueues.get(sessionId) ?? Promise.resolve();
4771
+ const result = previous.then(operation);
4772
+ const settled = result.then(() => {
4773
+ }, () => {
4774
+ });
4775
+ this.sessionStorageQueues.set(sessionId, settled);
4776
+ void settled.then(() => {
4777
+ if (this.sessionStorageQueues.get(sessionId) === settled) this.sessionStorageQueues.delete(sessionId);
4778
+ });
4779
+ return result;
4780
+ }
4781
+ saveSessionSafely(session) {
4782
+ const snapshot = this.snapshotSession(session);
4783
+ const management = this.managingSessions.get(session.id);
4784
+ return this.queueSessionStorage(session.id, async () => {
4785
+ if (management?.deleted) return;
4786
+ try {
4787
+ await this.persistence.saveSession(snapshot);
4788
+ } catch (error) {
4789
+ try {
4790
+ this.config.onDiagnostic?.({ kind: "persistence", sessionId: session.id, message: errorMessage(error) });
4791
+ } catch {
4792
+ }
4793
+ this.emit("persistence:error", { sessionId: session.id, message: errorMessage(error) });
4794
+ }
4795
+ });
4796
+ }
3917
4797
  ensureInitialized() {
3918
4798
  if (!this.initialized) {
3919
4799
  throw new ACPError(ACPErrorCodes.AGENT_NOT_INITIALIZED, "Agent not initialized. Call init() first.");
3920
4800
  }
3921
4801
  }
4802
+ reportPromptCacheUsage(sessionId, promptCacheUsage) {
4803
+ if (!this.config.llm.collectPromptCacheUsage || !promptCacheUsage) return;
4804
+ try {
4805
+ this.config.onDiagnostic?.({
4806
+ kind: "llm:prompt-cache",
4807
+ message: "Model reported prompt cache usage",
4808
+ sessionId,
4809
+ model: this.config.llm.model,
4810
+ promptCacheUsage
4811
+ });
4812
+ } catch {
4813
+ }
4814
+ }
3922
4815
  ensureSession() {
3923
4816
  if (this.currentSessionId) {
3924
4817
  const existing = this.sessionManager.get(this.currentSessionId);
@@ -3951,9 +4844,10 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
3951
4844
  this.sessionManager.addMessage(sessionId, { role: "tool", content: result.content, toolCallId: toolCall.id });
3952
4845
  }
3953
4846
  }
3954
- async executeServerSkill(toolCall) {
4847
+ async executeServerSkill(toolCall, signal) {
3955
4848
  const transport = this.transport;
3956
- const result = await transport.send("tools/call", { name: toolCall.name, arguments: toolCall.arguments });
4849
+ const result = await transport.send("tools/call", { name: toolCall.name, arguments: toolCall.arguments }, { signal });
4850
+ if (!result || !Array.isArray(result.content)) throw new Error("Server skill returned an invalid result");
3957
4851
  return result;
3958
4852
  }
3959
4853
  };