@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.d.mts +209 -41
- package/dist/index.d.ts +209 -41
- package/dist/index.js +1405 -501
- package/dist/index.mjs +1395 -501
- package/package.json +5 -2
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/index.ts
|
|
@@ -125,6 +135,7 @@ var ACPErrorCodes = {
|
|
|
125
135
|
LLM_CALL_FAILED: -32130,
|
|
126
136
|
LLM_EMPTY_RESPONSE: -32135,
|
|
127
137
|
LLM_TOKEN_LIMIT: -32131,
|
|
138
|
+
LLM_PROXY_CONFIG_INVALID: -32136,
|
|
128
139
|
LLM_RATE_LIMIT: -32132,
|
|
129
140
|
LLM_PROVIDER_UNAVAILABLE: -32133,
|
|
130
141
|
TOKEN_BUDGET_EXCEEDED: -32134,
|
|
@@ -189,9 +200,46 @@ var OpenAIContentAdapter = class {
|
|
|
189
200
|
}
|
|
190
201
|
};
|
|
191
202
|
|
|
203
|
+
// src/runtime/abort.ts
|
|
204
|
+
function abortError() {
|
|
205
|
+
const error = new Error("Request aborted");
|
|
206
|
+
error.name = "AbortError";
|
|
207
|
+
return error;
|
|
208
|
+
}
|
|
209
|
+
function throwIfAborted(signal) {
|
|
210
|
+
if (signal?.aborted) throw abortError();
|
|
211
|
+
}
|
|
212
|
+
function linkAbort(parent, child) {
|
|
213
|
+
const abort = () => child.abort();
|
|
214
|
+
if (parent?.aborted) abort();
|
|
215
|
+
else parent?.addEventListener("abort", abort, { once: true });
|
|
216
|
+
return () => parent?.removeEventListener("abort", abort);
|
|
217
|
+
}
|
|
218
|
+
function errorMessage(error) {
|
|
219
|
+
return error instanceof Error ? error.message : String(error ?? "Unknown error");
|
|
220
|
+
}
|
|
221
|
+
function abortableDelay(ms, signal) {
|
|
222
|
+
throwIfAborted(signal);
|
|
223
|
+
return new Promise((resolve, reject) => {
|
|
224
|
+
const cleanup = () => signal?.removeEventListener("abort", abort);
|
|
225
|
+
const timer = setTimeout(() => {
|
|
226
|
+
cleanup();
|
|
227
|
+
resolve();
|
|
228
|
+
}, ms);
|
|
229
|
+
const abort = () => {
|
|
230
|
+
clearTimeout(timer);
|
|
231
|
+
cleanup();
|
|
232
|
+
reject(abortError());
|
|
233
|
+
};
|
|
234
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
192
238
|
// src/llm/openai.ts
|
|
193
239
|
var OpenAIAdapter = class {
|
|
194
|
-
constructor(config) {
|
|
240
|
+
constructor(config, options) {
|
|
241
|
+
this.options = options;
|
|
242
|
+
this.verified = false;
|
|
195
243
|
this.config = config;
|
|
196
244
|
this.contentAdapter = new OpenAIContentAdapter();
|
|
197
245
|
}
|
|
@@ -210,6 +258,10 @@ var OpenAIAdapter = class {
|
|
|
210
258
|
const base = this.getBaseUrl().replace(/\/+$/, "");
|
|
211
259
|
return `${base}/chat/completions`;
|
|
212
260
|
}
|
|
261
|
+
getConnectionStatus() {
|
|
262
|
+
const endpoint = this.getEndpoint().trim().split(/[?#]/, 1)[0].replace(/^(https?:\/\/|\/\/)[^/]*@/i, "$1");
|
|
263
|
+
return { endpoint, verified: this.verified };
|
|
264
|
+
}
|
|
213
265
|
buildHeaders() {
|
|
214
266
|
const headers = {
|
|
215
267
|
"Content-Type": "application/json"
|
|
@@ -260,51 +312,63 @@ var OpenAIAdapter = class {
|
|
|
260
312
|
}));
|
|
261
313
|
}
|
|
262
314
|
async chat(messages, tools, options) {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
315
|
+
const request = this.createRequest(options?.signal);
|
|
316
|
+
try {
|
|
317
|
+
if (this.config.adapter === "custom" && this.config.transformRequest && this.config.transformResponse) {
|
|
318
|
+
const reqBody = this.config.transformRequest(messages, tools, {});
|
|
319
|
+
const res2 = await this.fetchResponse({
|
|
320
|
+
method: "POST",
|
|
321
|
+
headers: this.buildHeaders(),
|
|
322
|
+
body: JSON.stringify(reqBody),
|
|
323
|
+
signal: request.controller.signal
|
|
324
|
+
});
|
|
325
|
+
if (!res2.ok) {
|
|
326
|
+
const text = await res2.text().catch(() => "");
|
|
327
|
+
this.checkContextOverflow(res2.status, text);
|
|
328
|
+
throw new Error(`LLM request failed: ${res2.status} ${text}`);
|
|
329
|
+
}
|
|
330
|
+
const data2 = await res2.json();
|
|
331
|
+
const response2 = this.config.transformResponse(data2);
|
|
332
|
+
if (response2?.content || response2?.toolCalls?.length) this.verified = true;
|
|
333
|
+
return response2;
|
|
334
|
+
}
|
|
335
|
+
const body = {
|
|
336
|
+
model: this.config.model,
|
|
337
|
+
messages: this.messagesToOpenAI(messages)
|
|
338
|
+
};
|
|
339
|
+
if (tools.length > 0) {
|
|
340
|
+
body.tools = this.toolsToOpenAI(tools);
|
|
341
|
+
}
|
|
342
|
+
if (this.config.temperature !== void 0) {
|
|
343
|
+
body.temperature = this.config.temperature;
|
|
344
|
+
}
|
|
345
|
+
if (this.config.maxTokens !== void 0) {
|
|
346
|
+
body.max_tokens = this.config.maxTokens;
|
|
347
|
+
}
|
|
348
|
+
if (this.config.extraBody) {
|
|
349
|
+
Object.assign(body, this.config.extraBody);
|
|
350
|
+
}
|
|
351
|
+
const res = await this.fetchResponse({
|
|
266
352
|
method: "POST",
|
|
267
353
|
headers: this.buildHeaders(),
|
|
268
|
-
body: JSON.stringify(
|
|
269
|
-
signal:
|
|
354
|
+
body: JSON.stringify(body),
|
|
355
|
+
signal: request.controller.signal
|
|
270
356
|
});
|
|
271
|
-
if (!
|
|
272
|
-
const text = await
|
|
273
|
-
this.checkContextOverflow(
|
|
274
|
-
throw new Error(`LLM request failed: ${
|
|
357
|
+
if (!res.ok) {
|
|
358
|
+
const text = await res.text().catch(() => "");
|
|
359
|
+
this.checkContextOverflow(res.status, text);
|
|
360
|
+
throw new Error(`LLM request failed: ${res.status} ${text}`);
|
|
275
361
|
}
|
|
276
|
-
const
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
}
|
|
286
|
-
if (this.config.temperature !== void 0) {
|
|
287
|
-
body.temperature = this.config.temperature;
|
|
288
|
-
}
|
|
289
|
-
if (this.config.maxTokens !== void 0) {
|
|
290
|
-
body.max_tokens = this.config.maxTokens;
|
|
291
|
-
}
|
|
292
|
-
if (this.config.extraBody) {
|
|
293
|
-
Object.assign(body, this.config.extraBody);
|
|
294
|
-
}
|
|
295
|
-
const res = await fetch(this.getEndpoint(), {
|
|
296
|
-
method: "POST",
|
|
297
|
-
headers: this.buildHeaders(),
|
|
298
|
-
body: JSON.stringify(body),
|
|
299
|
-
signal: options?.signal
|
|
300
|
-
});
|
|
301
|
-
if (!res.ok) {
|
|
302
|
-
const text = await res.text().catch(() => "");
|
|
303
|
-
this.checkContextOverflow(res.status, text);
|
|
304
|
-
throw new Error(`LLM request failed: ${res.status} ${text}`);
|
|
362
|
+
const data = await res.json();
|
|
363
|
+
const response = this.parseResponse(data);
|
|
364
|
+
this.verified = true;
|
|
365
|
+
return response;
|
|
366
|
+
} catch (error) {
|
|
367
|
+
if (request.controller.signal.aborted && !options?.signal?.aborted) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "LLM request timed out", { reason: "timeout" });
|
|
368
|
+
throw error;
|
|
369
|
+
} finally {
|
|
370
|
+
request.cleanup();
|
|
305
371
|
}
|
|
306
|
-
const data = await res.json();
|
|
307
|
-
return this.parseResponse(data);
|
|
308
372
|
}
|
|
309
373
|
/**
|
|
310
374
|
* Stream LLM responses token-by-token via SSE.
|
|
@@ -312,115 +376,206 @@ var OpenAIAdapter = class {
|
|
|
312
376
|
* Collects the full response and returns it at the end.
|
|
313
377
|
*/
|
|
314
378
|
async *chatStream(messages, tools, options) {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
if (tools.length > 0) {
|
|
321
|
-
body.tools = this.toolsToOpenAI(tools);
|
|
322
|
-
}
|
|
323
|
-
if (this.config.temperature !== void 0) {
|
|
324
|
-
body.temperature = this.config.temperature;
|
|
325
|
-
}
|
|
326
|
-
if (this.config.maxTokens !== void 0) {
|
|
327
|
-
body.max_tokens = this.config.maxTokens;
|
|
328
|
-
}
|
|
329
|
-
if (this.config.extraBody) {
|
|
330
|
-
Object.assign(body, this.config.extraBody);
|
|
331
|
-
}
|
|
332
|
-
const res = await fetch(this.getEndpoint(), {
|
|
333
|
-
method: "POST",
|
|
334
|
-
headers: this.buildHeaders(),
|
|
335
|
-
body: JSON.stringify(body),
|
|
336
|
-
signal: options?.signal
|
|
337
|
-
});
|
|
338
|
-
if (!res.ok) {
|
|
339
|
-
const text = await res.text().catch(() => "");
|
|
340
|
-
this.checkContextOverflow(res.status, text);
|
|
341
|
-
throw new Error(`LLM request failed: ${res.status} ${text}`);
|
|
342
|
-
}
|
|
343
|
-
const reader = res.body?.getReader();
|
|
344
|
-
if (!reader) {
|
|
345
|
-
const data = await res.json();
|
|
346
|
-
const parsed = this.parseResponse(data);
|
|
347
|
-
if (parsed.content) {
|
|
348
|
-
yield { type: "llm_text", content: parsed.content };
|
|
349
|
-
}
|
|
379
|
+
if (this.config.adapter === "custom" && this.config.transformRequest && this.config.transformResponse) {
|
|
380
|
+
const response = await this.chat(messages, tools, options);
|
|
381
|
+
if (response.reasoning) yield { type: "llm_thinking", content: response.reasoning };
|
|
382
|
+
if (response.content) yield { type: "llm_text", content: response.content };
|
|
383
|
+
yield { type: "llm_response", response };
|
|
350
384
|
return;
|
|
351
385
|
}
|
|
352
|
-
const
|
|
353
|
-
let buffer = "";
|
|
354
|
-
let content = "";
|
|
355
|
-
let reasoning = "";
|
|
356
|
-
const toolCallMap = /* @__PURE__ */ new Map();
|
|
386
|
+
const request = this.createRequest(options?.signal);
|
|
357
387
|
try {
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
388
|
+
const body = {
|
|
389
|
+
model: this.config.model,
|
|
390
|
+
messages: this.messagesToOpenAI(messages),
|
|
391
|
+
stream: true,
|
|
392
|
+
...this.config.collectPromptCacheUsage && { stream_options: { include_usage: true } }
|
|
393
|
+
};
|
|
394
|
+
if (tools.length > 0) {
|
|
395
|
+
body.tools = this.toolsToOpenAI(tools);
|
|
396
|
+
}
|
|
397
|
+
if (this.config.temperature !== void 0) {
|
|
398
|
+
body.temperature = this.config.temperature;
|
|
399
|
+
}
|
|
400
|
+
if (this.config.maxTokens !== void 0) {
|
|
401
|
+
body.max_tokens = this.config.maxTokens;
|
|
402
|
+
}
|
|
403
|
+
if (this.config.extraBody) {
|
|
404
|
+
Object.assign(body, this.config.extraBody);
|
|
405
|
+
}
|
|
406
|
+
const res = await this.fetchResponse({
|
|
407
|
+
method: "POST",
|
|
408
|
+
headers: this.buildHeaders(),
|
|
409
|
+
body: JSON.stringify(body),
|
|
410
|
+
signal: request.controller.signal
|
|
411
|
+
});
|
|
412
|
+
if (!res.ok) {
|
|
413
|
+
const text = await res.text().catch(() => "");
|
|
414
|
+
this.checkContextOverflow(res.status, text);
|
|
415
|
+
throw new Error(`LLM request failed: ${res.status} ${text}`);
|
|
416
|
+
}
|
|
417
|
+
const reader = res.body?.getReader();
|
|
418
|
+
if (!reader) {
|
|
419
|
+
const data = await res.json();
|
|
420
|
+
const parsed = this.parseResponse(data);
|
|
421
|
+
this.verified = true;
|
|
422
|
+
if (parsed.content) {
|
|
423
|
+
yield { type: "llm_text", content: parsed.content };
|
|
424
|
+
}
|
|
425
|
+
yield { type: "llm_response", response: parsed };
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
const decoder = new TextDecoder();
|
|
429
|
+
let buffer = "";
|
|
430
|
+
let content = "";
|
|
431
|
+
let reasoning = "";
|
|
432
|
+
let finishReason;
|
|
433
|
+
let promptCacheUsage;
|
|
434
|
+
let sawDone = false;
|
|
435
|
+
const toolCallMap = /* @__PURE__ */ new Map();
|
|
436
|
+
try {
|
|
437
|
+
while (true) {
|
|
438
|
+
if (options?.signal?.aborted) {
|
|
439
|
+
this.throwIfAborted(options.signal);
|
|
440
|
+
}
|
|
441
|
+
const { done, value } = await this.readChunk(reader, request.controller);
|
|
442
|
+
if (done) {
|
|
443
|
+
buffer += decoder.decode();
|
|
444
|
+
if (!buffer.trim()) break;
|
|
445
|
+
buffer += "\n";
|
|
446
|
+
}
|
|
447
|
+
buffer += decoder.decode(value, { stream: true });
|
|
448
|
+
const lines = buffer.split("\n");
|
|
449
|
+
buffer = lines.pop() ?? "";
|
|
450
|
+
for (const line of lines) {
|
|
451
|
+
const trimmed = line.trim();
|
|
452
|
+
if (!trimmed || !trimmed.startsWith("data:")) continue;
|
|
453
|
+
const data = trimmed.slice(5).trim();
|
|
454
|
+
if (data === "[DONE]") {
|
|
455
|
+
sawDone = true;
|
|
456
|
+
continue;
|
|
380
457
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
458
|
+
try {
|
|
459
|
+
const chunk = JSON.parse(data);
|
|
460
|
+
if (chunk.error) throw new Error(chunk.error.message ?? "LLM stream error");
|
|
461
|
+
if (this.config.collectPromptCacheUsage && chunk.usage) {
|
|
462
|
+
promptCacheUsage = this.parsePromptCacheUsage(chunk.usage) ?? promptCacheUsage;
|
|
463
|
+
}
|
|
464
|
+
if (chunk.choices?.[0]?.finish_reason) finishReason = chunk.choices[0].finish_reason;
|
|
465
|
+
const delta = chunk.choices?.[0]?.delta;
|
|
466
|
+
if (!delta) continue;
|
|
467
|
+
if (delta.content) {
|
|
468
|
+
content += delta.content;
|
|
469
|
+
yield { type: "llm_text", content: delta.content };
|
|
470
|
+
}
|
|
471
|
+
if (delta.reasoning_content) {
|
|
472
|
+
reasoning += delta.reasoning_content;
|
|
473
|
+
yield { type: "llm_thinking", content: delta.reasoning_content };
|
|
474
|
+
}
|
|
475
|
+
if (delta.tool_calls) {
|
|
476
|
+
for (const tc of delta.tool_calls) {
|
|
477
|
+
const idx = tc.index ?? 0;
|
|
478
|
+
if (!toolCallMap.has(idx)) {
|
|
479
|
+
toolCallMap.set(idx, {
|
|
480
|
+
id: tc.id ?? "",
|
|
481
|
+
name: tc.function?.name ?? "",
|
|
482
|
+
arguments: ""
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
const entry = toolCallMap.get(idx);
|
|
486
|
+
if (tc.id) entry.id = tc.id;
|
|
487
|
+
if (tc.function?.name) entry.name = tc.function.name;
|
|
488
|
+
if (tc.function?.arguments) entry.arguments += tc.function.arguments;
|
|
390
489
|
}
|
|
391
|
-
const entry = toolCallMap.get(idx);
|
|
392
|
-
if (tc.id) entry.id = tc.id;
|
|
393
|
-
if (tc.function?.name) entry.name = tc.function.name;
|
|
394
|
-
if (tc.function?.arguments) entry.arguments += tc.function.arguments;
|
|
395
490
|
}
|
|
491
|
+
} catch (error) {
|
|
492
|
+
throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, `Invalid LLM stream: ${errorMessage(error)}`);
|
|
493
|
+
}
|
|
494
|
+
if (options?.signal?.aborted) {
|
|
495
|
+
this.throwIfAborted(options.signal);
|
|
396
496
|
}
|
|
397
|
-
} catch {
|
|
398
497
|
}
|
|
498
|
+
if (done || sawDone) break;
|
|
399
499
|
}
|
|
500
|
+
} finally {
|
|
501
|
+
await reader.cancel().catch(() => {
|
|
502
|
+
});
|
|
503
|
+
reader.releaseLock();
|
|
400
504
|
}
|
|
505
|
+
const toolCalls = [];
|
|
506
|
+
for (const [_, tc] of toolCallMap) {
|
|
507
|
+
toolCalls.push(this.parseToolCall(tc));
|
|
508
|
+
}
|
|
509
|
+
throwIfAborted(request.controller.signal);
|
|
510
|
+
if (this.config.strictStream && !sawDone && !finishReason) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "LLM stream ended without a finish marker");
|
|
511
|
+
if (finishReason === "length") for (const call of toolCalls) call.parseError = "Tool response exceeded the output token limit";
|
|
512
|
+
if (content || toolCalls.length) this.verified = true;
|
|
513
|
+
yield {
|
|
514
|
+
type: "llm_response",
|
|
515
|
+
response: {
|
|
516
|
+
content,
|
|
517
|
+
reasoning: reasoning || void 0,
|
|
518
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
519
|
+
usage: void 0,
|
|
520
|
+
...promptCacheUsage && { promptCacheUsage },
|
|
521
|
+
finishReason: finishReason ?? (toolCalls.length > 0 ? "tool_calls" : "stop")
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
} catch (error) {
|
|
525
|
+
if (request.controller.signal.aborted && !options?.signal?.aborted) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "LLM request timed out", { reason: "timeout" });
|
|
526
|
+
throw error;
|
|
401
527
|
} finally {
|
|
402
|
-
|
|
528
|
+
request.cleanup();
|
|
403
529
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
530
|
+
}
|
|
531
|
+
parseToolCall(tc) {
|
|
532
|
+
if (!tc.id || !tc.name) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "Tool call is missing its id or name");
|
|
533
|
+
try {
|
|
534
|
+
const args = JSON.parse(tc.arguments || "{}");
|
|
535
|
+
if (!args || typeof args !== "object" || Array.isArray(args)) throw new Error("Tool arguments must be a JSON object");
|
|
536
|
+
return { id: tc.id, name: tc.name, arguments: args };
|
|
537
|
+
} catch (error) {
|
|
538
|
+
return { id: tc.id, name: tc.name, arguments: {}, parseError: errorMessage(error) };
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
createRequest(parent) {
|
|
542
|
+
const controller = new AbortController();
|
|
543
|
+
const unlink = linkAbort(parent, controller);
|
|
544
|
+
const timeout = this.config.requestTimeoutMs;
|
|
545
|
+
const timer = timeout && timeout > 0 ? setTimeout(() => controller.abort(), timeout) : void 0;
|
|
546
|
+
return { controller, cleanup: () => {
|
|
547
|
+
clearTimeout(timer);
|
|
548
|
+
unlink();
|
|
549
|
+
controller.abort();
|
|
550
|
+
} };
|
|
551
|
+
}
|
|
552
|
+
async fetchResponse(init) {
|
|
553
|
+
const retries = Math.max(0, Math.min(this.config.retry?.maxRetries ?? 0, 5));
|
|
554
|
+
const requestInit = this.options?.rejectRedirects ? { ...init, redirect: "error" } : init;
|
|
555
|
+
for (let attempt = 0; ; attempt++) {
|
|
556
|
+
throwIfAborted(init.signal ?? void 0);
|
|
557
|
+
const response = await fetch(this.getEndpoint(), requestInit);
|
|
558
|
+
if (attempt >= retries || ![429, 502, 503, 504].includes(response.status)) return response;
|
|
559
|
+
const retryAfter = Number(response.headers.get("retry-after"));
|
|
560
|
+
const delay = Number.isFinite(retryAfter) && retryAfter > 0 ? Math.min(retryAfter * 1e3, 3e4) : Math.min((this.config.retry?.baseDelayMs ?? 500) * 2 ** attempt, 3e4);
|
|
561
|
+
await response.body?.cancel();
|
|
562
|
+
await abortableDelay(delay, init.signal ?? void 0);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
async readChunk(reader, controller) {
|
|
566
|
+
const idleMs = this.config.streamIdleTimeoutMs;
|
|
567
|
+
if (!idleMs || idleMs <= 0) return reader.read();
|
|
568
|
+
let timer;
|
|
569
|
+
try {
|
|
570
|
+
return await Promise.race([reader.read(), new Promise((_, reject) => {
|
|
571
|
+
timer = setTimeout(() => {
|
|
572
|
+
reject(new Error("LLM stream idle timeout"));
|
|
573
|
+
controller.abort();
|
|
574
|
+
}, idleMs);
|
|
575
|
+
})]);
|
|
576
|
+
} finally {
|
|
577
|
+
clearTimeout(timer);
|
|
413
578
|
}
|
|
414
|
-
yield {
|
|
415
|
-
type: "llm_response",
|
|
416
|
-
response: {
|
|
417
|
-
content,
|
|
418
|
-
reasoning: reasoning || void 0,
|
|
419
|
-
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
420
|
-
usage: void 0,
|
|
421
|
-
finishReason: toolCalls.length > 0 ? "tool_calls" : "stop"
|
|
422
|
-
}
|
|
423
|
-
};
|
|
424
579
|
}
|
|
425
580
|
/**
|
|
426
581
|
* Check if the error response indicates context length exceeded.
|
|
@@ -438,6 +593,14 @@ var OpenAIAdapter = class {
|
|
|
438
593
|
throw new ACPError(ACPErrorCodes.CONTEXT_OVERFLOW, errMsg, { isContextOverflow: true });
|
|
439
594
|
}
|
|
440
595
|
}
|
|
596
|
+
/** AbortError for the given signal — used so a cancelled stream surfaces an abort, not a parse error. */
|
|
597
|
+
throwIfAborted(signal) {
|
|
598
|
+
if (signal?.aborted) {
|
|
599
|
+
const err = new Error("Request aborted");
|
|
600
|
+
err.name = "AbortError";
|
|
601
|
+
throw err;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
441
604
|
parseResponse(data) {
|
|
442
605
|
const choice = data.choices?.[0];
|
|
443
606
|
if (!choice) {
|
|
@@ -449,17 +612,9 @@ var OpenAIAdapter = class {
|
|
|
449
612
|
const toolCalls = [];
|
|
450
613
|
if (message?.tool_calls?.length) {
|
|
451
614
|
for (const tc of message.tool_calls) {
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
} catch {
|
|
456
|
-
args = {};
|
|
457
|
-
}
|
|
458
|
-
toolCalls.push({
|
|
459
|
-
id: tc.id,
|
|
460
|
-
name: tc.function.name,
|
|
461
|
-
arguments: args
|
|
462
|
-
});
|
|
615
|
+
const parsed = this.parseToolCall({ id: tc.id, name: tc.function?.name, arguments: tc.function?.arguments });
|
|
616
|
+
if (choice.finish_reason === "length") parsed.parseError = "Tool response exceeded the output token limit";
|
|
617
|
+
toolCalls.push(parsed);
|
|
463
618
|
}
|
|
464
619
|
}
|
|
465
620
|
if (!content && !toolCalls.length) {
|
|
@@ -468,16 +623,70 @@ var OpenAIAdapter = class {
|
|
|
468
623
|
"LLM returned empty response with no tool calls"
|
|
469
624
|
);
|
|
470
625
|
}
|
|
626
|
+
const promptCacheUsage = this.config.collectPromptCacheUsage ? this.parsePromptCacheUsage(data.usage) : void 0;
|
|
471
627
|
return {
|
|
472
628
|
content,
|
|
473
629
|
reasoning,
|
|
474
630
|
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
475
631
|
usage: data.usage ? { input: data.usage.prompt_tokens, output: data.usage.completion_tokens } : void 0,
|
|
632
|
+
...promptCacheUsage && { promptCacheUsage },
|
|
476
633
|
finishReason: choice.finish_reason
|
|
477
634
|
};
|
|
478
635
|
}
|
|
636
|
+
parsePromptCacheUsage(raw) {
|
|
637
|
+
const asCount = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : void 0;
|
|
638
|
+
const cachedTokens = asCount(raw?.prompt_tokens_details?.cached_tokens) ?? asCount(raw?.prompt_cache_hit_tokens);
|
|
639
|
+
const cacheWriteTokens = asCount(raw?.prompt_tokens_details?.cache_write_tokens);
|
|
640
|
+
if (cachedTokens === void 0 && cacheWriteTokens === void 0) return void 0;
|
|
641
|
+
const inputTokens = asCount(raw?.prompt_tokens);
|
|
642
|
+
return {
|
|
643
|
+
...inputTokens !== void 0 && { inputTokens },
|
|
644
|
+
...cachedTokens !== void 0 && { cachedTokens },
|
|
645
|
+
...cacheWriteTokens !== void 0 && { cacheWriteTokens }
|
|
646
|
+
};
|
|
647
|
+
}
|
|
479
648
|
};
|
|
480
649
|
|
|
650
|
+
// src/llm/proxy.ts
|
|
651
|
+
function invalidProxyConfig(message) {
|
|
652
|
+
return new ACPError(ACPErrorCodes.LLM_PROXY_CONFIG_INVALID, message);
|
|
653
|
+
}
|
|
654
|
+
function proxyBaseUrl(value, field) {
|
|
655
|
+
const input = typeof value === "string" ? value.trim() : "";
|
|
656
|
+
const absolute = /^https?:\/\//i.test(input);
|
|
657
|
+
const relative = input.startsWith("/") && !input.startsWith("//");
|
|
658
|
+
if (!absolute && !relative || /[\\?#\u0000-\u001f]/.test(input)) {
|
|
659
|
+
throw invalidProxyConfig(`${field} must be an HTTP(S) base URL or a same-origin path without a query or fragment`);
|
|
660
|
+
}
|
|
661
|
+
let url;
|
|
662
|
+
try {
|
|
663
|
+
url = new URL(input, "http://listdoing.invalid");
|
|
664
|
+
} catch {
|
|
665
|
+
throw invalidProxyConfig(`${field} is not a valid proxy URL`);
|
|
666
|
+
}
|
|
667
|
+
if (url.username || url.password) throw invalidProxyConfig(`${field} must not include credentials`);
|
|
668
|
+
if (!absolute && url.pathname.startsWith("//")) throw invalidProxyConfig(`${field} must stay on the same origin`);
|
|
669
|
+
const normalized = (absolute ? url.href : url.pathname).replace(/\/+$/, "");
|
|
670
|
+
return normalized || "/";
|
|
671
|
+
}
|
|
672
|
+
function resolveProxyConfig(config) {
|
|
673
|
+
const mode = config.llmProxyMode;
|
|
674
|
+
if (mode === void 0) return { llm: config.llm, serverUrl: config.serverUrl };
|
|
675
|
+
if (mode !== "backend" && mode !== "listdoing-server") {
|
|
676
|
+
throw invalidProxyConfig("llmProxyMode must be backend or listdoing-server");
|
|
677
|
+
}
|
|
678
|
+
const serverUrl = config.serverUrl !== void 0 || mode === "listdoing-server" ? proxyBaseUrl(config.serverUrl, "serverUrl") : void 0;
|
|
679
|
+
const baseUrl = mode === "listdoing-server" ? `${serverUrl.replace(/\/+$/, "")}/v1` : proxyBaseUrl(config.llm.baseUrl, "llm.baseUrl");
|
|
680
|
+
if (mode === "listdoing-server" && config.llm.baseUrl !== void 0 && proxyBaseUrl(config.llm.baseUrl, "llm.baseUrl") !== baseUrl) {
|
|
681
|
+
throw invalidProxyConfig("llm.baseUrl must match the ListDoing Server proxy or be omitted");
|
|
682
|
+
}
|
|
683
|
+
if (baseUrl.endsWith("/chat/completions")) {
|
|
684
|
+
throw invalidProxyConfig("llm.baseUrl must end before /chat/completions");
|
|
685
|
+
}
|
|
686
|
+
const apiKey = config.llm.apiKey ?? (mode === "listdoing-server" ? config.authToken : void 0);
|
|
687
|
+
return { llm: { ...config.llm, baseUrl, apiKey }, serverUrl };
|
|
688
|
+
}
|
|
689
|
+
|
|
481
690
|
// src/skill/registry.ts
|
|
482
691
|
var SkillRegistry = class {
|
|
483
692
|
constructor() {
|
|
@@ -498,6 +707,11 @@ var SkillRegistry = class {
|
|
|
498
707
|
get(name) {
|
|
499
708
|
return this.skills.get(name)?.definition;
|
|
500
709
|
}
|
|
710
|
+
/** Identity of this registration, replaced on every register call. */
|
|
711
|
+
getRegistrationIdentity(skill) {
|
|
712
|
+
const entry = this.skills.get(skill.name);
|
|
713
|
+
return entry?.definition === skill ? entry : skill;
|
|
714
|
+
}
|
|
501
715
|
getHandler(name) {
|
|
502
716
|
return this.skills.get(name)?.handler;
|
|
503
717
|
}
|
|
@@ -552,7 +766,9 @@ var SkillRegistry = class {
|
|
|
552
766
|
|
|
553
767
|
// src/skill/executor.ts
|
|
554
768
|
var SkillExecutor = class {
|
|
555
|
-
constructor(registry, worker) {
|
|
769
|
+
constructor(registry, worker, options) {
|
|
770
|
+
this.options = options;
|
|
771
|
+
this.validators = /* @__PURE__ */ new WeakMap();
|
|
556
772
|
this.registry = registry;
|
|
557
773
|
this.worker = worker ?? {
|
|
558
774
|
async execute(def, params, ctx) {
|
|
@@ -580,19 +796,41 @@ var SkillExecutor = class {
|
|
|
580
796
|
throw new ACPError(ACPErrorCodes.SKILL_NOT_FOUND, `Skill not found: ${name}`, { name });
|
|
581
797
|
}
|
|
582
798
|
const timeout = def.timeout ?? 3e4;
|
|
799
|
+
throwIfAborted(context.signal);
|
|
800
|
+
if (this.options?.validateInputs) {
|
|
801
|
+
const validate = await this.getValidator(def);
|
|
802
|
+
if (!validate(params)) throw new ACPError(ACPErrorCodes.SKILL_EXECUTION_FAILED, `Invalid arguments for ${name}`, { errors: validate.errors });
|
|
803
|
+
}
|
|
583
804
|
const controller = new AbortController();
|
|
584
|
-
const
|
|
585
|
-
|
|
586
|
-
const timeoutId = timeout > 0 ? setTimeout(() =>
|
|
805
|
+
const unlink = linkAbort(context.signal, controller);
|
|
806
|
+
let timedOut = false;
|
|
807
|
+
const timeoutId = timeout > 0 ? setTimeout(() => {
|
|
808
|
+
timedOut = true;
|
|
809
|
+
controller.abort();
|
|
810
|
+
}, timeout) : null;
|
|
587
811
|
const execContext = {
|
|
588
812
|
...context,
|
|
589
813
|
signal: controller.signal
|
|
590
814
|
};
|
|
815
|
+
let removeDeadline = () => {
|
|
816
|
+
};
|
|
591
817
|
try {
|
|
592
|
-
const
|
|
818
|
+
const execution = Promise.resolve().then(() => {
|
|
819
|
+
throwIfAborted(controller.signal);
|
|
820
|
+
return this.worker.execute(def, params, execContext);
|
|
821
|
+
});
|
|
822
|
+
const result = def.timeoutMode === "deadline" ? await Promise.race([execution, new Promise((_, reject) => {
|
|
823
|
+
const abort = () => reject(abortError());
|
|
824
|
+
controller.signal.addEventListener("abort", abort, { once: true });
|
|
825
|
+
removeDeadline = () => controller.signal.removeEventListener("abort", abort);
|
|
826
|
+
})]) : await execution;
|
|
827
|
+
throwIfAborted(context.signal);
|
|
828
|
+
if (timedOut) throw new Error("Skill exceeded its timeout");
|
|
829
|
+
if (!result || !Array.isArray(result.content)) throw new Error("Skill returned an invalid result (content must be an array)");
|
|
593
830
|
return result;
|
|
594
831
|
} catch (err) {
|
|
595
|
-
if (
|
|
832
|
+
if (context.signal.aborted) throw abortError();
|
|
833
|
+
if (timedOut) {
|
|
596
834
|
throw new ACPError(
|
|
597
835
|
ACPErrorCodes.SKILL_EXECUTION_TIMEOUT,
|
|
598
836
|
`Skill execution timeout: ${name}`,
|
|
@@ -602,19 +840,44 @@ var SkillExecutor = class {
|
|
|
602
840
|
if (err instanceof ACPError) throw err;
|
|
603
841
|
throw new ACPError(
|
|
604
842
|
ACPErrorCodes.SKILL_EXECUTION_FAILED,
|
|
605
|
-
`Skill execution failed: ${name}: ${err
|
|
606
|
-
{ name, error: err
|
|
843
|
+
`Skill execution failed: ${name}: ${errorMessage(err)}`,
|
|
844
|
+
{ name, error: errorMessage(err) }
|
|
607
845
|
);
|
|
608
846
|
} finally {
|
|
609
847
|
if (timeoutId) clearTimeout(timeoutId);
|
|
610
|
-
|
|
848
|
+
unlink();
|
|
849
|
+
removeDeadline();
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
async getValidator(skill) {
|
|
853
|
+
const { default: Ajv } = await import("ajv");
|
|
854
|
+
const registration = this.registry.getRegistrationIdentity(skill);
|
|
855
|
+
const schema = skill.inputSchema;
|
|
856
|
+
let signature;
|
|
857
|
+
try {
|
|
858
|
+
signature = JSON.stringify(schema, function(key, value) {
|
|
859
|
+
if (value !== this[key]) throw new TypeError("Schema cannot be cached");
|
|
860
|
+
if (value === null || typeof value === "string" || typeof value === "boolean" || Number.isFinite(value)) return value;
|
|
861
|
+
if (typeof value === "object") {
|
|
862
|
+
const prototype = Object.getPrototypeOf(value);
|
|
863
|
+
if (prototype === Object.prototype || prototype === null || prototype === Array.prototype) return value;
|
|
864
|
+
}
|
|
865
|
+
throw new TypeError("Schema cannot be cached");
|
|
866
|
+
});
|
|
867
|
+
} catch {
|
|
611
868
|
}
|
|
869
|
+
const cached = this.validators.get(registration);
|
|
870
|
+
if (cached && cached.schema === signature) return cached.validate;
|
|
871
|
+
const validate = new Ajv({ strict: false, allErrors: true }).compile(schema);
|
|
872
|
+
if (signature !== void 0) this.validators.set(registration, { schema: signature, validate });
|
|
873
|
+
return validate;
|
|
612
874
|
}
|
|
613
875
|
/**
|
|
614
876
|
* Directly invoke a skill by name — used by ctx.invoke().
|
|
615
877
|
* No LLM loop, no approval. Shares parent's signal and timeout context.
|
|
616
878
|
*/
|
|
617
879
|
async invokeDirect(name, params, context) {
|
|
880
|
+
throwIfAborted(context.signal);
|
|
618
881
|
const def = this.registry.get(name);
|
|
619
882
|
if (!def) {
|
|
620
883
|
return {
|
|
@@ -638,8 +901,9 @@ var SkillExecutor = class {
|
|
|
638
901
|
try {
|
|
639
902
|
return await handler(params, context);
|
|
640
903
|
} catch (err) {
|
|
904
|
+
if (context.signal.aborted || err?.name === "AbortError") throw abortError();
|
|
641
905
|
return {
|
|
642
|
-
content: [{ type: "text", text: `Invoke failed: ${err
|
|
906
|
+
content: [{ type: "text", text: `Invoke failed: ${errorMessage(err)}` }],
|
|
643
907
|
isError: true
|
|
644
908
|
};
|
|
645
909
|
}
|
|
@@ -815,13 +1079,13 @@ var McpRouter = class {
|
|
|
815
1079
|
/**
|
|
816
1080
|
* Route a tool call to the appropriate MCP Server.
|
|
817
1081
|
*/
|
|
818
|
-
async call(prefixedName, args) {
|
|
1082
|
+
async call(prefixedName, args, options) {
|
|
819
1083
|
const { server, tool } = this.parseMcpToolName(prefixedName);
|
|
820
1084
|
const conn = this.connections.get(server);
|
|
821
1085
|
if (!conn) {
|
|
822
1086
|
throw new Error(`MCP Server "${server}" not connected. Available: ${Array.from(this.connections.keys()).join(", ") || "none"}`);
|
|
823
1087
|
}
|
|
824
|
-
return conn.call(tool, args);
|
|
1088
|
+
return conn.call(tool, args, options);
|
|
825
1089
|
}
|
|
826
1090
|
/**
|
|
827
1091
|
* Resolve `mcp:` prefixed tool names from a tools list to ToolDef[].
|
|
@@ -894,7 +1158,7 @@ function fileInputsToContentBlocks(files) {
|
|
|
894
1158
|
}
|
|
895
1159
|
|
|
896
1160
|
// src/session/manager.ts
|
|
897
|
-
var SessionManager = class {
|
|
1161
|
+
var SessionManager = class _SessionManager {
|
|
898
1162
|
constructor() {
|
|
899
1163
|
this.sessions = /* @__PURE__ */ new Map();
|
|
900
1164
|
}
|
|
@@ -920,6 +1184,9 @@ var SessionManager = class {
|
|
|
920
1184
|
get(sessionId) {
|
|
921
1185
|
return this.sessions.get(sessionId);
|
|
922
1186
|
}
|
|
1187
|
+
restore(session) {
|
|
1188
|
+
this.sessions.set(session.id, session);
|
|
1189
|
+
}
|
|
923
1190
|
getOrThrow(sessionId) {
|
|
924
1191
|
const session = this.sessions.get(sessionId);
|
|
925
1192
|
if (!session) {
|
|
@@ -981,13 +1248,16 @@ var SessionManager = class {
|
|
|
981
1248
|
session.status = "archived";
|
|
982
1249
|
session.updatedAt = Date.now();
|
|
983
1250
|
}
|
|
984
|
-
merge(fromId, toId, strategy
|
|
1251
|
+
merge(fromId, toId, strategy) {
|
|
985
1252
|
const from = this.getOrThrow(fromId);
|
|
986
1253
|
const to = this.getOrThrow(toId);
|
|
1254
|
+
_SessionManager.mergeContent(from, to, strategy);
|
|
1255
|
+
}
|
|
1256
|
+
static mergeContent(from, to, strategy = "summary_and_artifacts") {
|
|
987
1257
|
if (strategy === "full") {
|
|
988
1258
|
to.messages.push(...from.messages);
|
|
989
1259
|
} else {
|
|
990
|
-
const summary =
|
|
1260
|
+
const summary = _SessionManager.createSummary(from);
|
|
991
1261
|
to.messages.push({
|
|
992
1262
|
role: "user",
|
|
993
1263
|
content: `[Context from session "${from.title || from.id}"]:
|
|
@@ -1009,7 +1279,7 @@ ${artifacts.map((a) => contentToPreview(a.content, 200)).join("\n")}`
|
|
|
1009
1279
|
to.messageCount = to.messages.length;
|
|
1010
1280
|
to.updatedAt = Date.now();
|
|
1011
1281
|
}
|
|
1012
|
-
createSummary(session) {
|
|
1282
|
+
static createSummary(session) {
|
|
1013
1283
|
const userMsgs = session.messages.filter((m) => m.role === "user");
|
|
1014
1284
|
if (userMsgs.length === 0) return "Empty session";
|
|
1015
1285
|
return userMsgs.slice(-3).map((m) => contentToPreview(m.content, 200)).join("\n");
|
|
@@ -1136,6 +1406,64 @@ function estimateRequestTokens(messages, tools, systemPrompt) {
|
|
|
1136
1406
|
return total;
|
|
1137
1407
|
}
|
|
1138
1408
|
|
|
1409
|
+
// src/context/messages.ts
|
|
1410
|
+
function repairMessages(messages) {
|
|
1411
|
+
const result = [];
|
|
1412
|
+
for (let i = 0; i < messages.length; i++) {
|
|
1413
|
+
const message = messages[i];
|
|
1414
|
+
if (!message || !["user", "assistant", "system", "tool"].includes(message.role)) continue;
|
|
1415
|
+
if (message.role === "tool") continue;
|
|
1416
|
+
if (message.role === "assistant" && message.toolCalls?.length) {
|
|
1417
|
+
const tools = [];
|
|
1418
|
+
while (i + 1 < messages.length && messages[i + 1]?.role === "tool") tools.push(messages[++i]);
|
|
1419
|
+
const ids = /* @__PURE__ */ new Set();
|
|
1420
|
+
const calls = message.toolCalls.filter((call) => {
|
|
1421
|
+
if (!call?.id || !call.name || ids.has(call.id)) return false;
|
|
1422
|
+
ids.add(call.id);
|
|
1423
|
+
return true;
|
|
1424
|
+
});
|
|
1425
|
+
if (calls.length) {
|
|
1426
|
+
result.push({ ...message, toolCalls: calls });
|
|
1427
|
+
for (const call of calls) {
|
|
1428
|
+
const tool = tools.find((tool2) => tool2.toolCallId === call.id);
|
|
1429
|
+
result.push(tool ? normalizeContent(tool) : {
|
|
1430
|
+
role: "tool",
|
|
1431
|
+
toolCallId: call.id,
|
|
1432
|
+
content: "Tool execution was interrupted; its outcome is unknown. Do not repeat a side-effecting operation without checking its state."
|
|
1433
|
+
});
|
|
1434
|
+
}
|
|
1435
|
+
} else if (message.content) result.push(normalizeContent({ ...message, toolCalls: void 0 }));
|
|
1436
|
+
} else result.push(normalizeContent(message));
|
|
1437
|
+
}
|
|
1438
|
+
return result;
|
|
1439
|
+
}
|
|
1440
|
+
function normalizeContent(message) {
|
|
1441
|
+
const content = message.content;
|
|
1442
|
+
if (content == null || content === "" || Array.isArray(content) && content.length === 0) {
|
|
1443
|
+
return { ...message, content: "." };
|
|
1444
|
+
}
|
|
1445
|
+
if (typeof content !== "string") {
|
|
1446
|
+
if (!Array.isArray(content)) return { ...message, content: "." };
|
|
1447
|
+
const valid = content.filter((block) => {
|
|
1448
|
+
if (!block || typeof block !== "object") return false;
|
|
1449
|
+
if (block.type === "text") return typeof block.text === "string";
|
|
1450
|
+
if (block.type === "image") return typeof block.data === "string" && !!block.data && typeof block.mimeType === "string";
|
|
1451
|
+
if (block.type === "resource") return typeof block.resource?.uri === "string" && (block.resource.text === void 0 || typeof block.resource.text === "string");
|
|
1452
|
+
return false;
|
|
1453
|
+
});
|
|
1454
|
+
if (valid.length !== content.length) return { ...message, content: valid.length ? valid : "." };
|
|
1455
|
+
}
|
|
1456
|
+
return message;
|
|
1457
|
+
}
|
|
1458
|
+
function messageGroups(messages) {
|
|
1459
|
+
const groups = [];
|
|
1460
|
+
for (const message of repairMessages(messages)) {
|
|
1461
|
+
if (message.role === "tool" && groups.length) groups[groups.length - 1].push(message);
|
|
1462
|
+
else groups.push([message]);
|
|
1463
|
+
}
|
|
1464
|
+
return groups;
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1139
1467
|
// src/context/manager.ts
|
|
1140
1468
|
var ContextManager = class {
|
|
1141
1469
|
constructor(config, model) {
|
|
@@ -1153,6 +1481,10 @@ var ContextManager = class {
|
|
|
1153
1481
|
setModel(model) {
|
|
1154
1482
|
this.model = model;
|
|
1155
1483
|
}
|
|
1484
|
+
/** Inject an LLM-based summarizer. When set, auto-compression uses semantic summarization. */
|
|
1485
|
+
setSummarizer(fn) {
|
|
1486
|
+
this.summarizer = fn;
|
|
1487
|
+
}
|
|
1156
1488
|
/** Compute the token budget for the current model & config */
|
|
1157
1489
|
getBudget() {
|
|
1158
1490
|
const ctxLen = resolveModelContextLength(this.model, this.config.contextLength);
|
|
@@ -1184,59 +1516,113 @@ var ContextManager = class {
|
|
|
1184
1516
|
if (systemPrompt) {
|
|
1185
1517
|
result.push({ role: "system", content: systemPrompt });
|
|
1186
1518
|
}
|
|
1187
|
-
const sanitized = messages.map((m) => {
|
|
1188
|
-
if (m.role === "assistant" && !m.content && !m.toolCalls?.length) {
|
|
1189
|
-
return { ...m, content: "." };
|
|
1190
|
-
}
|
|
1191
|
-
return m;
|
|
1192
|
-
});
|
|
1193
|
-
const reversed = [...sanitized].reverse();
|
|
1194
1519
|
const selected = [];
|
|
1195
1520
|
let usedByMessages = 0;
|
|
1196
|
-
for (const
|
|
1197
|
-
const
|
|
1198
|
-
if (usedByMessages +
|
|
1199
|
-
selected.unshift(
|
|
1200
|
-
usedByMessages +=
|
|
1521
|
+
for (const group of messageGroups(messages).reverse()) {
|
|
1522
|
+
const tokens = group.reduce((sum, msg) => sum + estimateMessageTokens(msg), 0);
|
|
1523
|
+
if (usedByMessages + tokens > remainingBudget) break;
|
|
1524
|
+
selected.unshift(...group);
|
|
1525
|
+
usedByMessages += tokens;
|
|
1201
1526
|
}
|
|
1202
1527
|
result.push(...selected);
|
|
1203
1528
|
if (selected.length === 0 && messages.length > 0) {
|
|
1204
1529
|
const lastUser = [...messages].reverse().find((m) => m.role === "user");
|
|
1205
1530
|
if (lastUser) {
|
|
1206
|
-
|
|
1531
|
+
const fallback = repairMessages([lastUser]);
|
|
1532
|
+
result.push(...fallback);
|
|
1533
|
+
usedByMessages += fallback.reduce((sum, message) => sum + estimateMessageTokens(message), 0);
|
|
1207
1534
|
}
|
|
1208
1535
|
}
|
|
1209
1536
|
return { messages: result, usedTokens: fixedTokens + usedByMessages, budget };
|
|
1210
1537
|
}
|
|
1211
1538
|
/**
|
|
1212
1539
|
* Ensure context fits within budget. Triggers auto-compression when
|
|
1213
|
-
* usage exceeds the configured threshold.
|
|
1540
|
+
* usage exceeds the configured threshold. Prefers LLM-based semantic
|
|
1541
|
+
* summarization (when a summarizer is set and enabled) over mechanical truncation.
|
|
1214
1542
|
*/
|
|
1215
|
-
ensureContextFits(messages, systemPrompt, tools, model) {
|
|
1543
|
+
async ensureContextFits(messages, systemPrompt, tools, model, signal) {
|
|
1544
|
+
throwIfAborted(signal);
|
|
1216
1545
|
if (model) this.setModel(model);
|
|
1217
|
-
const {
|
|
1546
|
+
const { budget } = this.buildWindow(messages, systemPrompt, tools);
|
|
1547
|
+
const beforeTokens = estimateRequestTokens(repairMessages(messages), tools, systemPrompt);
|
|
1548
|
+
if (this.config.autoCompress === false) return { messages, wasCompressed: false };
|
|
1218
1549
|
const threshold = this.config.compressThreshold ?? 0.85;
|
|
1219
|
-
if (
|
|
1550
|
+
if (beforeTokens / budget <= threshold) {
|
|
1220
1551
|
return { messages, wasCompressed: false };
|
|
1221
1552
|
}
|
|
1222
|
-
const
|
|
1223
|
-
|
|
1224
|
-
if (
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1553
|
+
const useLLM = this.config.useLLMSummary !== false && !!this.summarizer;
|
|
1554
|
+
let result;
|
|
1555
|
+
if (useLLM) {
|
|
1556
|
+
const llmResult = await this.compressWithLLM(messages, systemPrompt, tools, budget, { signal, groupTools: true });
|
|
1557
|
+
result = llmResult ?? this.compressSync(messages, "hybrid", { groupTools: true });
|
|
1558
|
+
} else {
|
|
1559
|
+
result = this.compressSync(messages, "hybrid", { groupTools: true });
|
|
1560
|
+
}
|
|
1561
|
+
let compressed = result.messages;
|
|
1562
|
+
let window2 = this.buildWindow(compressed, systemPrompt, tools);
|
|
1563
|
+
if (window2.usedTokens > budget) {
|
|
1564
|
+
console.warn(`[listdoing] Context still exceeds budget after compression (${window2.usedTokens}/${budget} tokens). Truncating to recent messages only.`);
|
|
1565
|
+
compressed = messageGroups(messages).slice(-3).flat();
|
|
1566
|
+
window2 = this.buildWindow(compressed, systemPrompt, tools);
|
|
1567
|
+
}
|
|
1568
|
+
if (estimateRequestTokens(compressed, tools, systemPrompt) >= beforeTokens || window2.usedTokens >= beforeTokens) {
|
|
1569
|
+
return { messages, wasCompressed: false };
|
|
1228
1570
|
}
|
|
1229
|
-
return {
|
|
1571
|
+
return {
|
|
1572
|
+
messages: compressed,
|
|
1573
|
+
wasCompressed: true,
|
|
1574
|
+
beforeTokens,
|
|
1575
|
+
afterTokens: window2.usedTokens
|
|
1576
|
+
};
|
|
1577
|
+
}
|
|
1578
|
+
/** Build one smaller request after a provider rejects the current window. */
|
|
1579
|
+
async compressForOverflow(messages, rejected, systemPrompt, tools, signal) {
|
|
1580
|
+
throwIfAborted(signal);
|
|
1581
|
+
const fixedTokens = estimateRequestTokens([], tools, systemPrompt);
|
|
1582
|
+
const available = Math.min(rejected.budget, rejected.usedTokens) - fixedTokens;
|
|
1583
|
+
const historyBudget = Math.floor(available * 0.85);
|
|
1584
|
+
if (historyBudget <= 0) return;
|
|
1585
|
+
const budget = fixedTokens + historyBudget;
|
|
1586
|
+
const latestUser = [...messages].reverse().find((message) => message.role === "user");
|
|
1587
|
+
if (latestUser && estimateMessageTokens(latestUser) > historyBudget) return;
|
|
1588
|
+
const rejectedUserIndex = latestUser ? rejected.messages.lastIndexOf(latestUser) : -1;
|
|
1589
|
+
const activeToolResults = rejected.messages.slice(rejectedUserIndex + 1).filter((message) => message.role === "tool");
|
|
1590
|
+
const summarized = this.config.useLLMSummary !== false ? await this.compressWithLLM(messages, systemPrompt, tools, budget, { signal, groupTools: true }) : null;
|
|
1591
|
+
throwIfAborted(signal);
|
|
1592
|
+
const useCandidate = (result) => {
|
|
1593
|
+
const window2 = this.buildWindow(result.messages, systemPrompt, tools, budget);
|
|
1594
|
+
if (window2.usedTokens > budget || window2.usedTokens >= rejected.usedTokens) return;
|
|
1595
|
+
if (latestUser && !window2.messages.some((message) => message.role === "user" && message.content === latestUser.content)) return;
|
|
1596
|
+
const keptToolIds = new Set(window2.messages.filter((message) => message.role === "tool").map((message) => message.toolCallId));
|
|
1597
|
+
if (activeToolResults.some((message) => !keptToolIds.has(message.toolCallId))) return;
|
|
1598
|
+
return { ...result, window: window2 };
|
|
1599
|
+
};
|
|
1600
|
+
const candidate = summarized && useCandidate(summarized);
|
|
1601
|
+
return candidate || useCandidate(this.compressSync(messages, "hybrid", { maxTokens: historyBudget, groupTools: true }));
|
|
1230
1602
|
}
|
|
1231
1603
|
/**
|
|
1232
|
-
* Compress context by summarizing older messages.
|
|
1604
|
+
* Compress context by summarizing older messages. When strategy is
|
|
1605
|
+
* 'llm_summary' (or resolved via config) it uses the injected summarizer,
|
|
1606
|
+
* falling back to mechanical 'hybrid' truncation on failure.
|
|
1233
1607
|
*/
|
|
1234
|
-
compress(messages, strategy) {
|
|
1608
|
+
async compress(messages, strategy) {
|
|
1609
|
+
const strat = strategy ?? this.config.historyStrategy?.type ?? "hybrid";
|
|
1610
|
+
if (strat === "llm_summary") {
|
|
1611
|
+
if (!this.summarizer) {
|
|
1612
|
+
return this.compressSync(messages, "hybrid");
|
|
1613
|
+
}
|
|
1614
|
+
const llmResult = await this.compressWithLLM(messages, void 0, [], this.getBudget());
|
|
1615
|
+
return llmResult ?? this.compressSync(messages, "hybrid");
|
|
1616
|
+
}
|
|
1617
|
+
return this.compressSync(messages, strat);
|
|
1618
|
+
}
|
|
1619
|
+
/** Mechanical (non-LLM) compression — the original truncation strategies. */
|
|
1620
|
+
compressSync(messages, strategy, options) {
|
|
1621
|
+
messages = repairMessages(messages);
|
|
1235
1622
|
const estimateTokens = (msgs) => msgs.reduce((sum, m) => sum + estimateMessageTokens(m), 0);
|
|
1236
1623
|
const beforeTokens = estimateTokens(messages);
|
|
1237
|
-
const strat = strategy ?? this.config.historyStrategy?.type ?? "hybrid";
|
|
1238
1624
|
let compressed;
|
|
1239
|
-
switch (
|
|
1625
|
+
switch (strategy) {
|
|
1240
1626
|
case "sliding_window": {
|
|
1241
1627
|
compressed = messages.slice(-10);
|
|
1242
1628
|
break;
|
|
@@ -1259,13 +1645,14 @@ ${summaryText}` },
|
|
|
1259
1645
|
case "token_budget":
|
|
1260
1646
|
case "hybrid":
|
|
1261
1647
|
default: {
|
|
1262
|
-
const maxTokens = this.config.maxTokens ?? this.getBudget();
|
|
1648
|
+
const maxTokens = options?.maxTokens ?? this.config.maxTokens ?? this.getBudget();
|
|
1263
1649
|
let totalTokens = 0;
|
|
1264
1650
|
const kept = [];
|
|
1265
|
-
|
|
1266
|
-
|
|
1651
|
+
const groups = options?.groupTools ? messageGroups(messages) : messages.map((message) => [message]);
|
|
1652
|
+
for (const group of groups.reverse()) {
|
|
1653
|
+
const t = estimateTokens(group);
|
|
1267
1654
|
if (totalTokens + t > maxTokens) break;
|
|
1268
|
-
kept.unshift(
|
|
1655
|
+
kept.unshift(...group);
|
|
1269
1656
|
totalTokens += t;
|
|
1270
1657
|
}
|
|
1271
1658
|
if (kept.length < messages.length) {
|
|
@@ -1282,8 +1669,55 @@ ${summaryText}` },
|
|
|
1282
1669
|
break;
|
|
1283
1670
|
}
|
|
1284
1671
|
}
|
|
1285
|
-
const
|
|
1286
|
-
return { messages:
|
|
1672
|
+
const repaired = repairMessages(compressed);
|
|
1673
|
+
return { messages: repaired, beforeTokens, afterTokens: estimateTokens(repaired) };
|
|
1674
|
+
}
|
|
1675
|
+
/**
|
|
1676
|
+
* LLM-based semantic summarization. Returns null when it cannot be performed
|
|
1677
|
+
* (no summarizer, nothing to summarize, or the LLM call fails) so callers can
|
|
1678
|
+
* fall back to mechanical compression.
|
|
1679
|
+
*/
|
|
1680
|
+
async compressWithLLM(messages, systemPrompt, tools, budget, options) {
|
|
1681
|
+
if (!this.summarizer) return null;
|
|
1682
|
+
const { signal, groupTools } = options ?? {};
|
|
1683
|
+
messages = repairMessages(messages);
|
|
1684
|
+
try {
|
|
1685
|
+
const estimateTokens = (msgs) => msgs.reduce((sum, m) => sum + estimateMessageTokens(m), 0);
|
|
1686
|
+
const beforeTokens = estimateTokens(messages);
|
|
1687
|
+
let fixedTokens = 0;
|
|
1688
|
+
if (systemPrompt) fixedTokens += estimateTextTokens(systemPrompt) + 4;
|
|
1689
|
+
for (const t of tools ?? []) fixedTokens += estimateToolDefTokens(t);
|
|
1690
|
+
const summaryReserve = 2e3;
|
|
1691
|
+
const outputReserve = this.config.outputReserve ?? 1024;
|
|
1692
|
+
const remaining = budget - fixedTokens - summaryReserve - outputReserve;
|
|
1693
|
+
const groups = groupTools ? messageGroups(messages) : messages.map((message) => [message]);
|
|
1694
|
+
const kept = [];
|
|
1695
|
+
let used = 0;
|
|
1696
|
+
for (let i = groups.length - 1; i >= 0; i--) {
|
|
1697
|
+
const t = estimateTokens(groups[i]);
|
|
1698
|
+
if (used + t > remaining) break;
|
|
1699
|
+
kept.unshift(groups[i]);
|
|
1700
|
+
used += t;
|
|
1701
|
+
}
|
|
1702
|
+
const oldMessages = groups.slice(0, groups.length - kept.length).flat();
|
|
1703
|
+
if (oldMessages.length === 0) {
|
|
1704
|
+
return null;
|
|
1705
|
+
}
|
|
1706
|
+
const summary = await this.summarizer(oldMessages, signal ? { signal } : void 0);
|
|
1707
|
+
const summaryMessage = { role: "user", content: `[\u4E0A\u4E0B\u6587\u6458\u8981]
|
|
1708
|
+
${summary}` };
|
|
1709
|
+
const limit = groupTools ? budget - fixedTokens : budget;
|
|
1710
|
+
let afterTokens = estimateMessageTokens(summaryMessage) + used;
|
|
1711
|
+
while (kept.length > 1 && afterTokens > limit) {
|
|
1712
|
+
afterTokens -= estimateTokens(kept.shift());
|
|
1713
|
+
}
|
|
1714
|
+
const repaired = repairMessages([summaryMessage, ...kept.flat()]);
|
|
1715
|
+
return { messages: repaired, beforeTokens, afterTokens: estimateTokens(repaired) };
|
|
1716
|
+
} catch (err) {
|
|
1717
|
+
throwIfAborted(signal);
|
|
1718
|
+
console.warn("[listdoing] LLM summarization failed, falling back to mechanical compression:", err);
|
|
1719
|
+
return null;
|
|
1720
|
+
}
|
|
1287
1721
|
}
|
|
1288
1722
|
/**
|
|
1289
1723
|
* Search across messages for relevant content.
|
|
@@ -1307,17 +1741,101 @@ ${summaryText}` },
|
|
|
1307
1741
|
}
|
|
1308
1742
|
};
|
|
1309
1743
|
|
|
1744
|
+
// src/context/summarizer.ts
|
|
1745
|
+
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
|
|
1746
|
+
|
|
1747
|
+
\u4E25\u683C\u6309\u4EE5\u4E0B\u7ED3\u6784\u8F93\u51FA\uFF08markdown\uFF09\uFF1A
|
|
1748
|
+
|
|
1749
|
+
## \u76EE\u6807
|
|
1750
|
+
\u7528\u6237\u60F3\u8981\u5B8C\u6210\u4EC0\u4E48
|
|
1751
|
+
|
|
1752
|
+
## \u5173\u952E\u51B3\u7B56
|
|
1753
|
+
\u5DF2\u505A\u7684\u51B3\u7B56\u53CA\u5176\u7406\u7531
|
|
1754
|
+
|
|
1755
|
+
## \u6D89\u53CA\u6587\u4EF6
|
|
1756
|
+
\u521B\u5EFA/\u4FEE\u6539/\u8BFB\u53D6\u7684\u6587\u4EF6\uFF0C\u7B80\u8FF0\u5176\u5185\u5BB9\u6216\u53D8\u66F4
|
|
1757
|
+
|
|
1758
|
+
## \u91CD\u8981\u53D1\u73B0
|
|
1759
|
+
\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
|
|
1760
|
+
|
|
1761
|
+
## \u5F53\u524D\u72B6\u6001
|
|
1762
|
+
\u5BF9\u8BDD\u7247\u6BB5\u7ED3\u675F\u65F6\u6B63\u5728\u505A\u4EC0\u4E48
|
|
1763
|
+
|
|
1764
|
+
## \u5F85\u529E\u4E8B\u9879
|
|
1765
|
+
\u672A\u5B8C\u6210\u7684\u4EFB\u52A1\u6216\u660E\u786E\u7684\u4E0B\u4E00\u6B65
|
|
1766
|
+
|
|
1767
|
+
\u8981\u6C42\uFF1A
|
|
1768
|
+
- \u7B80\u6D01\uFF0C\u53EA\u4FDD\u7559\u7EE7\u7EED\u5DE5\u4F5C\u5FC5\u9700\u7684\u4FE1\u606F
|
|
1769
|
+
- \u4E0D\u8981\u8F93\u51FA\u4E0E\u4E0A\u8FF0\u7ED3\u6784\u65E0\u5173\u7684\u5185\u5BB9
|
|
1770
|
+
- \u7528\u4E0E\u5BF9\u8BDD\u5185\u5BB9\u76F8\u540C\u7684\u8BED\u8A00\u8F93\u51FA
|
|
1771
|
+
|
|
1772
|
+
--- \u5F85\u538B\u7F29\u7684\u5BF9\u8BDD ---`;
|
|
1773
|
+
function truncate(s, max) {
|
|
1774
|
+
return s.length > max ? s.slice(0, max) + "\u2026" : s;
|
|
1775
|
+
}
|
|
1776
|
+
function truncateMiddle(s, head, tail) {
|
|
1777
|
+
if (s.length <= head + tail) return s;
|
|
1778
|
+
return s.slice(0, head) + `
|
|
1779
|
+
\u2026[\u4E2D\u95F4 ${s.length - head - tail} \u5B57\u7B26\u5DF2\u7701\u7565]\u2026
|
|
1780
|
+
` + s.slice(-tail);
|
|
1781
|
+
}
|
|
1782
|
+
function formatMessagesForSummary(messages) {
|
|
1783
|
+
return messages.map((m) => {
|
|
1784
|
+
if (m.role === "user") return `\u7528\u6237: ${contentToString(m.content)}`;
|
|
1785
|
+
if (m.role === "assistant") {
|
|
1786
|
+
let s = `\u52A9\u624B: ${contentToString(m.content)}`;
|
|
1787
|
+
if (m.toolCalls?.length) {
|
|
1788
|
+
s += "\n" + m.toolCalls.map((tc) => ` [\u8C03\u7528\u5DE5\u5177 ${tc.name}: ${truncate(JSON.stringify(tc.arguments), 200)}]`).join("\n");
|
|
1789
|
+
}
|
|
1790
|
+
return s;
|
|
1791
|
+
}
|
|
1792
|
+
if (m.role === "tool") {
|
|
1793
|
+
return `\u5DE5\u5177\u7ED3\u679C: ${truncateMiddle(contentToString(m.content), 500, 200)}`;
|
|
1794
|
+
}
|
|
1795
|
+
if (m.role === "system") return `\u7CFB\u7EDF: ${contentToString(m.content)}`;
|
|
1796
|
+
return "";
|
|
1797
|
+
}).filter(Boolean).join("\n\n");
|
|
1798
|
+
}
|
|
1799
|
+
function createLLMSummarizer(adapter, options) {
|
|
1800
|
+
const timeoutMs = options?.timeoutMs ?? 3e4;
|
|
1801
|
+
return async (messages, opts) => {
|
|
1802
|
+
const formatted = formatMessagesForSummary(messages);
|
|
1803
|
+
const controller = new AbortController();
|
|
1804
|
+
const unlink = linkAbort(opts?.signal, controller);
|
|
1805
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
1806
|
+
try {
|
|
1807
|
+
const response = await adapter.chat(
|
|
1808
|
+
[
|
|
1809
|
+
{ role: "system", content: SUMMARIZE_PROMPT },
|
|
1810
|
+
{ role: "user", content: formatted }
|
|
1811
|
+
],
|
|
1812
|
+
[],
|
|
1813
|
+
{ signal: controller.signal }
|
|
1814
|
+
);
|
|
1815
|
+
const summary = (response.content ?? "").trim();
|
|
1816
|
+
if (!summary) {
|
|
1817
|
+
throw new Error("Summarizer returned empty content");
|
|
1818
|
+
}
|
|
1819
|
+
return summary;
|
|
1820
|
+
} finally {
|
|
1821
|
+
clearTimeout(timer);
|
|
1822
|
+
unlink();
|
|
1823
|
+
}
|
|
1824
|
+
};
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1310
1827
|
// src/task/planner.ts
|
|
1311
1828
|
var TaskPlanner = class {
|
|
1312
1829
|
constructor(llmAdapter) {
|
|
1313
1830
|
this.llmAdapter = llmAdapter;
|
|
1314
1831
|
}
|
|
1315
|
-
async plan(goal, availableTools) {
|
|
1832
|
+
async plan(goal, availableTools, signal) {
|
|
1316
1833
|
const prompt = this.buildPlanningPrompt(goal, availableTools);
|
|
1317
1834
|
const response = await this.llmAdapter.chat(
|
|
1318
1835
|
[{ role: "user", content: prompt }],
|
|
1319
|
-
[]
|
|
1836
|
+
[],
|
|
1320
1837
|
// No tools for the planning call itself
|
|
1838
|
+
{ signal }
|
|
1321
1839
|
);
|
|
1322
1840
|
return this.parsePlan(goal, response.content);
|
|
1323
1841
|
}
|
|
@@ -1636,17 +2154,19 @@ var GraphPlanner = class {
|
|
|
1636
2154
|
constructor(llmAdapter) {
|
|
1637
2155
|
this.llmAdapter = llmAdapter;
|
|
1638
2156
|
}
|
|
1639
|
-
async planGraph(goal, availableTools) {
|
|
2157
|
+
async planGraph(goal, availableTools, signal) {
|
|
1640
2158
|
const prompt = this.buildGraphPlanningPrompt(goal, availableTools);
|
|
1641
2159
|
let response;
|
|
1642
2160
|
try {
|
|
1643
2161
|
const result = await this.llmAdapter.chat(
|
|
1644
2162
|
[{ role: "user", content: prompt }],
|
|
1645
|
-
[]
|
|
2163
|
+
[],
|
|
1646
2164
|
// No tools for the planning call
|
|
2165
|
+
{ signal }
|
|
1647
2166
|
);
|
|
1648
2167
|
response = result.content;
|
|
1649
2168
|
} catch {
|
|
2169
|
+
throwIfAborted(signal);
|
|
1650
2170
|
return this.fallbackSequentialGraph(goal);
|
|
1651
2171
|
}
|
|
1652
2172
|
return this.parseGraphPlan(goal, response);
|
|
@@ -1769,9 +2289,7 @@ var SubAgent = class {
|
|
|
1769
2289
|
this.config = options.config;
|
|
1770
2290
|
this.depth = options.depth;
|
|
1771
2291
|
this.abortController = new AbortController();
|
|
1772
|
-
|
|
1773
|
-
options.parentSignal.addEventListener("abort", () => this.abortController.abort(), { once: true });
|
|
1774
|
-
}
|
|
2292
|
+
this.unlinkParent = linkAbort(options.parentSignal, this.abortController);
|
|
1775
2293
|
const maxDepth = this.config.maxDepth ?? 2;
|
|
1776
2294
|
if (this.depth >= maxDepth) {
|
|
1777
2295
|
this.config.canSpawnSubAgents = false;
|
|
@@ -1782,7 +2300,9 @@ var SubAgent = class {
|
|
|
1782
2300
|
this.status = "running";
|
|
1783
2301
|
const startTime = Date.now();
|
|
1784
2302
|
try {
|
|
2303
|
+
throwIfAborted(this.abortController.signal);
|
|
1785
2304
|
const session = this.parentAgent.createSession({
|
|
2305
|
+
activate: false,
|
|
1786
2306
|
title: `SubAgent: ${this.nodeId}`,
|
|
1787
2307
|
meta: {
|
|
1788
2308
|
parentSessionId: this.parentAgent.getCurrentSessionId(),
|
|
@@ -1796,6 +2316,7 @@ var SubAgent = class {
|
|
|
1796
2316
|
const prompt = this.buildPrompt(goal, upstreamOutputs);
|
|
1797
2317
|
const options = {
|
|
1798
2318
|
sessionId: session.id,
|
|
2319
|
+
signal: this.abortController.signal,
|
|
1799
2320
|
maxTurns: this.config.maxTurns ?? 10,
|
|
1800
2321
|
depth: this.depth + 1,
|
|
1801
2322
|
taskMode: void 0
|
|
@@ -1819,6 +2340,7 @@ var SubAgent = class {
|
|
|
1819
2340
|
sessionId: this.sessionId ?? void 0
|
|
1820
2341
|
};
|
|
1821
2342
|
}
|
|
2343
|
+
throwIfAborted(this.abortController.signal);
|
|
1822
2344
|
this.status = "done";
|
|
1823
2345
|
return {
|
|
1824
2346
|
nodeId: this.nodeId,
|
|
@@ -1840,6 +2362,8 @@ var SubAgent = class {
|
|
|
1840
2362
|
retryCount: 0,
|
|
1841
2363
|
sessionId: this.sessionId ?? void 0
|
|
1842
2364
|
};
|
|
2365
|
+
} finally {
|
|
2366
|
+
this.unlinkParent();
|
|
1843
2367
|
}
|
|
1844
2368
|
}
|
|
1845
2369
|
/** Abort the sub-agent */
|
|
@@ -1884,42 +2408,48 @@ var GraphExecutor = class {
|
|
|
1884
2408
|
this.maxDepth = options.maxDepth ?? 2;
|
|
1885
2409
|
this.baseDepth = options.baseDepth ?? 0;
|
|
1886
2410
|
this.abortController = new AbortController();
|
|
2411
|
+
this.unlinkParent = linkAbort(options.parentSignal, this.abortController);
|
|
1887
2412
|
this.emit = options.emit;
|
|
1888
2413
|
}
|
|
1889
2414
|
/** Execute the entire graph, level by level */
|
|
1890
2415
|
async execute() {
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
2416
|
+
try {
|
|
2417
|
+
this.plan.status = "executing";
|
|
2418
|
+
for (let level = 0; level < this.graph.getLevelCount(); level++) {
|
|
2419
|
+
if (this.abortController.signal.aborted) {
|
|
2420
|
+
this.plan.status = "failed";
|
|
2421
|
+
return this.plan;
|
|
2422
|
+
}
|
|
2423
|
+
const nodes = this.graph.getLevel(level);
|
|
2424
|
+
const nodeIds = nodes.map((n) => n.id);
|
|
2425
|
+
this.emit("graph:level_start", { level, nodeIds });
|
|
2426
|
+
await this.executeLevel(level, nodes);
|
|
2427
|
+
const levelResults = nodeIds.map((id) => this.plan.results[id]).filter(Boolean);
|
|
2428
|
+
this.emit("graph:level_complete", { level, results: levelResults });
|
|
2429
|
+
}
|
|
2430
|
+
const progress = this.getProgress();
|
|
2431
|
+
if (progress.failed === 0 && progress.done === progress.total) {
|
|
2432
|
+
this.plan.status = "completed";
|
|
2433
|
+
this.emit("graph:complete", { plan: this.plan, progress });
|
|
2434
|
+
} else if (progress.done > 0) {
|
|
2435
|
+
this.plan.status = "partial";
|
|
2436
|
+
this.emit("graph:complete", { plan: this.plan, progress });
|
|
2437
|
+
} else {
|
|
1894
2438
|
this.plan.status = "failed";
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
this.
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
this.emit("graph:level_complete", { level, results: levelResults });
|
|
1903
|
-
}
|
|
1904
|
-
const progress = this.getProgress();
|
|
1905
|
-
if (progress.failed === 0 && progress.done === progress.total) {
|
|
1906
|
-
this.plan.status = "completed";
|
|
1907
|
-
this.emit("graph:complete", { plan: this.plan, progress });
|
|
1908
|
-
} else if (progress.done > 0) {
|
|
1909
|
-
this.plan.status = "partial";
|
|
1910
|
-
this.emit("graph:complete", { plan: this.plan, progress });
|
|
1911
|
-
} else {
|
|
1912
|
-
this.plan.status = "failed";
|
|
1913
|
-
const failedNodes = Object.entries(this.plan.results).filter(([, r]) => r.status === "failed").map(([id]) => id);
|
|
1914
|
-
this.emit("graph:failed", { plan: this.plan, failedNodes });
|
|
2439
|
+
const failedNodes = Object.entries(this.plan.results).filter(([, r]) => r.status === "failed").map(([id]) => id);
|
|
2440
|
+
this.emit("graph:failed", { plan: this.plan, failedNodes });
|
|
2441
|
+
}
|
|
2442
|
+
this.plan.updatedAt = Date.now();
|
|
2443
|
+
return this.plan;
|
|
2444
|
+
} finally {
|
|
2445
|
+
this.unlinkParent();
|
|
1915
2446
|
}
|
|
1916
|
-
this.plan.updatedAt = Date.now();
|
|
1917
|
-
return this.plan;
|
|
1918
2447
|
}
|
|
1919
2448
|
/** Execute a single level — nodes run concurrently (limited by maxConcurrency) */
|
|
1920
2449
|
async executeLevel(level, nodes) {
|
|
1921
|
-
const runnableNodes = nodes.filter((node) => this.shouldRun(node));
|
|
2450
|
+
const runnableNodes = nodes.filter((node) => this.plan.results[node.id]?.status !== "done" && this.shouldRun(node));
|
|
1922
2451
|
for (const node of nodes) {
|
|
2452
|
+
if (this.plan.results[node.id]?.status === "done") continue;
|
|
1923
2453
|
if (!runnableNodes.includes(node)) {
|
|
1924
2454
|
this.plan.results[node.id] = {
|
|
1925
2455
|
nodeId: node.id,
|
|
@@ -2019,7 +2549,8 @@ var GraphExecutor = class {
|
|
|
2019
2549
|
willRetry: true
|
|
2020
2550
|
});
|
|
2021
2551
|
const delay = Math.min(backoffMs * Math.pow(2, attempt), maxBackoffMs);
|
|
2022
|
-
await
|
|
2552
|
+
await abortableDelay(delay, this.abortController.signal).catch(() => {
|
|
2553
|
+
});
|
|
2023
2554
|
}
|
|
2024
2555
|
}
|
|
2025
2556
|
if (node.fallback) {
|
|
@@ -2078,8 +2609,9 @@ var GraphExecutor = class {
|
|
|
2078
2609
|
depth: this.baseDepth
|
|
2079
2610
|
});
|
|
2080
2611
|
if (node.timeout) {
|
|
2612
|
+
let timer;
|
|
2081
2613
|
const timeoutPromise = new Promise((_, reject) => {
|
|
2082
|
-
setTimeout(() => reject(new ACPError(
|
|
2614
|
+
timer = setTimeout(() => reject(new ACPError(
|
|
2083
2615
|
ACPErrorCodes.SUBAGENT_TIMEOUT,
|
|
2084
2616
|
`Node "${node.id}" timed out after ${node.timeout}ms`
|
|
2085
2617
|
)), node.timeout);
|
|
@@ -2099,6 +2631,8 @@ var GraphExecutor = class {
|
|
|
2099
2631
|
duration: 0,
|
|
2100
2632
|
retryCount: 0
|
|
2101
2633
|
};
|
|
2634
|
+
} finally {
|
|
2635
|
+
clearTimeout(timer);
|
|
2102
2636
|
}
|
|
2103
2637
|
}
|
|
2104
2638
|
return subAgent.execute(node.goal, upstreamOutputs);
|
|
@@ -2299,7 +2833,7 @@ var HttpTransport = class {
|
|
|
2299
2833
|
}
|
|
2300
2834
|
return headers;
|
|
2301
2835
|
}
|
|
2302
|
-
async send(method, params) {
|
|
2836
|
+
async send(method, params, options) {
|
|
2303
2837
|
const id = ++this.requestId;
|
|
2304
2838
|
const body = {
|
|
2305
2839
|
jsonrpc: "2.0",
|
|
@@ -2311,21 +2845,31 @@ var HttpTransport = class {
|
|
|
2311
2845
|
const res = await fetch(`${this.baseUrl}/mcp`, {
|
|
2312
2846
|
method: "POST",
|
|
2313
2847
|
headers: this.buildHeaders({ Accept: "application/json" }),
|
|
2848
|
+
signal: options?.signal,
|
|
2314
2849
|
body: JSON.stringify(body)
|
|
2315
2850
|
});
|
|
2316
|
-
|
|
2851
|
+
let data;
|
|
2852
|
+
try {
|
|
2853
|
+
data = await res.json();
|
|
2854
|
+
} catch (error) {
|
|
2855
|
+
if (!res.ok) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, `HTTP ${res.status}`, { method, status: res.status });
|
|
2856
|
+
throw error;
|
|
2857
|
+
}
|
|
2317
2858
|
const sid = res.headers.get("Mcp-Session-Id");
|
|
2318
2859
|
if (sid) this.sessionId = sid;
|
|
2319
|
-
if (data
|
|
2860
|
+
if (data?.error) {
|
|
2320
2861
|
throw new ACPError(data.error.code, data.error.message, data.error.data);
|
|
2321
2862
|
}
|
|
2863
|
+
if (!res.ok) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, `HTTP ${res.status}`, { method, status: res.status });
|
|
2864
|
+
if (!data || !Object.prototype.hasOwnProperty.call(data, "result")) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, "Invalid JSON-RPC response", { method });
|
|
2322
2865
|
return data.result;
|
|
2323
2866
|
} catch (err) {
|
|
2867
|
+
throwIfAborted(options?.signal);
|
|
2324
2868
|
if (err instanceof ACPError) throw err;
|
|
2325
2869
|
throw new ACPError(
|
|
2326
2870
|
ACPErrorCodes.TRANSPORT_ERROR,
|
|
2327
|
-
`Transport error: ${err
|
|
2328
|
-
{ method, originalError: err
|
|
2871
|
+
`Transport error: ${errorMessage(err)}`,
|
|
2872
|
+
{ method, originalError: errorMessage(err) }
|
|
2329
2873
|
);
|
|
2330
2874
|
}
|
|
2331
2875
|
}
|
|
@@ -2345,7 +2889,7 @@ var HttpTransport = class {
|
|
|
2345
2889
|
/**
|
|
2346
2890
|
* Stream a chat request via SSE.
|
|
2347
2891
|
*/
|
|
2348
|
-
async *streamChat(method, params) {
|
|
2892
|
+
async *streamChat(method, params, options) {
|
|
2349
2893
|
const id = ++this.requestId;
|
|
2350
2894
|
const body = {
|
|
2351
2895
|
jsonrpc: "2.0",
|
|
@@ -2356,33 +2900,48 @@ var HttpTransport = class {
|
|
|
2356
2900
|
const res = await fetch(`${this.baseUrl}/mcp`, {
|
|
2357
2901
|
method: "POST",
|
|
2358
2902
|
headers: this.buildHeaders({ Accept: "text/event-stream" }),
|
|
2903
|
+
signal: options?.signal,
|
|
2359
2904
|
body: JSON.stringify(body)
|
|
2360
2905
|
});
|
|
2906
|
+
if (!res.ok) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, `HTTP ${res.status}`, { method, status: res.status });
|
|
2361
2907
|
if (!res.body) {
|
|
2362
2908
|
throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, "No response body for stream");
|
|
2363
2909
|
}
|
|
2364
2910
|
const reader = res.body.getReader();
|
|
2365
2911
|
const decoder = new TextDecoder();
|
|
2366
2912
|
let buffer = "";
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2913
|
+
try {
|
|
2914
|
+
while (true) {
|
|
2915
|
+
throwIfAborted(options?.signal);
|
|
2916
|
+
const { done, value } = await reader.read();
|
|
2917
|
+
buffer += done ? decoder.decode() : decoder.decode(value, { stream: true });
|
|
2918
|
+
if (done) {
|
|
2919
|
+
if (!buffer.trim()) break;
|
|
2920
|
+
buffer += "\n";
|
|
2921
|
+
}
|
|
2922
|
+
const lines = buffer.split("\n");
|
|
2923
|
+
buffer = lines.pop() || "";
|
|
2924
|
+
for (const line of lines) {
|
|
2925
|
+
if (line.trim().startsWith("data:")) {
|
|
2926
|
+
const payload = line.trim().slice(5).trim();
|
|
2927
|
+
if (payload === "[DONE]") continue;
|
|
2928
|
+
let data;
|
|
2929
|
+
try {
|
|
2930
|
+
data = JSON.parse(payload);
|
|
2931
|
+
} catch {
|
|
2932
|
+
throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, "Invalid JSON in transport stream");
|
|
2381
2933
|
}
|
|
2382
|
-
|
|
2934
|
+
if (data.error) throw new ACPError(data.error.code, data.error.message, data.error.data);
|
|
2935
|
+
if (data.result !== void 0) yield { id: data.id, result: data.result };
|
|
2936
|
+
else if (data.method) yield { method: data.method, params: data.params };
|
|
2383
2937
|
}
|
|
2384
2938
|
}
|
|
2939
|
+
if (done) break;
|
|
2385
2940
|
}
|
|
2941
|
+
} finally {
|
|
2942
|
+
await reader.cancel().catch(() => {
|
|
2943
|
+
});
|
|
2944
|
+
reader.releaseLock();
|
|
2386
2945
|
}
|
|
2387
2946
|
}
|
|
2388
2947
|
};
|
|
@@ -2429,27 +2988,35 @@ var Ldoing = class {
|
|
|
2429
2988
|
constructor(config) {
|
|
2430
2989
|
this.initialized = false;
|
|
2431
2990
|
this.startTime = 0;
|
|
2991
|
+
this.mcpStatus = "unchecked";
|
|
2992
|
+
this.discoveredServerSkills = /* @__PURE__ */ new WeakSet();
|
|
2432
2993
|
this.currentSessionId = null;
|
|
2433
2994
|
this.abortControllers = /* @__PURE__ */ new Map();
|
|
2995
|
+
this.runContexts = /* @__PURE__ */ new WeakMap();
|
|
2434
2996
|
// Session-level concurrency control (方案 B: abort 旧请求 + 等待退出)
|
|
2435
2997
|
// runningSessions 跟踪正在执行 agent loop 的会话,donePromise 在 agent loop 结束时 resolve
|
|
2436
2998
|
this.runningSessions = /* @__PURE__ */ new Map();
|
|
2437
2999
|
// Approval pending promises
|
|
2438
3000
|
this.approvalResolvers = /* @__PURE__ */ new Map();
|
|
3001
|
+
this.sessionStorageQueues = /* @__PURE__ */ new Map();
|
|
3002
|
+
this.managingSessions = /* @__PURE__ */ new Map();
|
|
3003
|
+
this.compressingSessions = /* @__PURE__ */ new Map();
|
|
2439
3004
|
// Event listeners
|
|
2440
3005
|
this.eventHandlers = /* @__PURE__ */ new Map();
|
|
2441
3006
|
// Token estimation calibration
|
|
2442
3007
|
this.tokenCalibrationRatio = 1;
|
|
2443
|
-
|
|
3008
|
+
const proxy = resolveProxyConfig(config);
|
|
3009
|
+
this.config = config.llmProxyMode === void 0 ? config : { ...config, llm: { ...config.llm } };
|
|
2444
3010
|
this.agentId = `agent-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
2445
|
-
this.llmAdapter = new OpenAIAdapter(config.
|
|
3011
|
+
this.llmAdapter = new OpenAIAdapter(proxy.llm, { rejectRedirects: config.llmProxyMode !== void 0 });
|
|
2446
3012
|
this.skillRegistry = new SkillRegistry();
|
|
2447
|
-
this.skillExecutor = new SkillExecutor(this.skillRegistry);
|
|
3013
|
+
this.skillExecutor = new SkillExecutor(this.skillRegistry, void 0, { validateInputs: config.skills?.validateInputs });
|
|
2448
3014
|
this.mcpRouter = new McpRouter();
|
|
2449
3015
|
this.sessionManager = new SessionManager();
|
|
2450
3016
|
this.contextManager = new ContextManager(config.context, config.llm.model);
|
|
2451
|
-
|
|
2452
|
-
|
|
3017
|
+
this.contextManager.setSummarizer(createLLMSummarizer(this.llmAdapter));
|
|
3018
|
+
if (proxy.serverUrl) {
|
|
3019
|
+
this.transport = new HttpTransport(proxy.serverUrl, config.authToken, config.headers);
|
|
2453
3020
|
} else {
|
|
2454
3021
|
this.transport = new LocalTransport();
|
|
2455
3022
|
}
|
|
@@ -2470,10 +3037,13 @@ var Ldoing = class {
|
|
|
2470
3037
|
}
|
|
2471
3038
|
}
|
|
2472
3039
|
let mode = "local";
|
|
2473
|
-
let connected = false;
|
|
2474
3040
|
if (this.config.serverUrl) {
|
|
3041
|
+
const transport = this.transport;
|
|
3042
|
+
const proxyMode = this.config.llmProxyMode;
|
|
3043
|
+
const { adapter, model } = this.config.llm;
|
|
3044
|
+
this.mcpStatus = "unchecked";
|
|
2475
3045
|
try {
|
|
2476
|
-
const result2 = await
|
|
3046
|
+
const result2 = await transport.send("initialize", {
|
|
2477
3047
|
protocolVersion: "2025-03-26",
|
|
2478
3048
|
capabilities: {
|
|
2479
3049
|
acp: {
|
|
@@ -2481,47 +3051,47 @@ var Ldoing = class {
|
|
|
2481
3051
|
sessions: true,
|
|
2482
3052
|
contextManagement: true,
|
|
2483
3053
|
dynamicSkillRegistration: true,
|
|
2484
|
-
llmConfig: this.config.llm
|
|
3054
|
+
llmConfig: proxyMode === void 0 ? this.config.llm : { adapter, model }
|
|
2485
3055
|
}
|
|
2486
3056
|
},
|
|
2487
3057
|
clientInfo: { name: "listdoing-sdk", version: "0.1.0" }
|
|
2488
3058
|
});
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
3059
|
+
if (proxyMode === void 0 || this.transport === transport) {
|
|
3060
|
+
const serverSkills = result2?.capabilities?.acp?.serverSkills ?? [];
|
|
3061
|
+
for (const name of serverSkills) {
|
|
3062
|
+
if (!this.skillRegistry.has(name)) {
|
|
3063
|
+
const skill = {
|
|
3064
|
+
name,
|
|
3065
|
+
description: `Server skill: ${name}`,
|
|
3066
|
+
inputSchema: { type: "object", properties: {} },
|
|
3067
|
+
location: "server"
|
|
3068
|
+
};
|
|
3069
|
+
this.skillRegistry.register(skill);
|
|
3070
|
+
this.discoveredServerSkills.add(skill);
|
|
3071
|
+
}
|
|
2499
3072
|
}
|
|
3073
|
+
if (this.transport === transport) this.mcpStatus = "connected";
|
|
3074
|
+
mode = this.skillRegistry.list().some((s) => s.location === "local") ? "hybrid" : "remote";
|
|
2500
3075
|
}
|
|
2501
|
-
mode = this.skillRegistry.list().some((s) => s.location === "local") ? "hybrid" : "remote";
|
|
2502
3076
|
} catch {
|
|
2503
|
-
|
|
3077
|
+
if (this.transport === transport) this.mcpStatus = "failed";
|
|
2504
3078
|
}
|
|
2505
3079
|
}
|
|
2506
|
-
let llmConnected = false;
|
|
2507
|
-
try {
|
|
2508
|
-
llmConnected = true;
|
|
2509
|
-
} catch {
|
|
2510
|
-
llmConnected = false;
|
|
2511
|
-
}
|
|
2512
3080
|
this.initialized = true;
|
|
2513
3081
|
const result = {
|
|
2514
3082
|
agentId: this.agentId,
|
|
2515
3083
|
mode,
|
|
3084
|
+
...this.getConnectionDetails(),
|
|
2516
3085
|
llm: {
|
|
2517
|
-
connected:
|
|
3086
|
+
connected: true,
|
|
3087
|
+
// Legacy status. Use llmRoute.verified for a successful request.
|
|
2518
3088
|
provider: this.config.llm.adapter,
|
|
2519
3089
|
model: this.config.llm.model
|
|
2520
3090
|
},
|
|
2521
3091
|
capabilities: {
|
|
2522
3092
|
localSkills: this.skillRegistry.list().filter((s) => s.location !== "server").map((s) => s.name),
|
|
2523
3093
|
serverSkills: this.skillRegistry.list().filter((s) => s.location === "server").map((s) => s.name),
|
|
2524
|
-
llmProxy: mode !== "local"
|
|
3094
|
+
llmProxy: this.config.llmProxyMode !== void 0 || mode !== "local"
|
|
2525
3095
|
}
|
|
2526
3096
|
};
|
|
2527
3097
|
this.emit("init", result);
|
|
@@ -2532,7 +3102,10 @@ var Ldoing = class {
|
|
|
2532
3102
|
controller.abort();
|
|
2533
3103
|
}
|
|
2534
3104
|
this.abortControllers.clear();
|
|
3105
|
+
for (const pending of this.approvalResolvers.values()) pending.reject(abortError());
|
|
3106
|
+
this.approvalResolvers.clear();
|
|
2535
3107
|
this.initialized = false;
|
|
3108
|
+
this.mcpStatus = "unchecked";
|
|
2536
3109
|
this.emit("destroy", {});
|
|
2537
3110
|
}
|
|
2538
3111
|
getStatus() {
|
|
@@ -2542,6 +3115,7 @@ var Ldoing = class {
|
|
|
2542
3115
|
uptime: Date.now() - this.startTime,
|
|
2543
3116
|
activeSessions: this.sessionManager.list({ status: "active" }).length,
|
|
2544
3117
|
loadedSkills: this.skillRegistry.list().length,
|
|
3118
|
+
...this.getConnectionDetails(),
|
|
2545
3119
|
llm: {
|
|
2546
3120
|
adapter: this.config.llm.adapter,
|
|
2547
3121
|
model: this.config.llm.model,
|
|
@@ -2549,15 +3123,26 @@ var Ldoing = class {
|
|
|
2549
3123
|
}
|
|
2550
3124
|
};
|
|
2551
3125
|
}
|
|
3126
|
+
getConnectionDetails() {
|
|
3127
|
+
const mcpStatus = this.config.serverUrl ? this.mcpStatus : "not-configured";
|
|
3128
|
+
try {
|
|
3129
|
+
const status = this.llmAdapter.getConnectionStatus?.();
|
|
3130
|
+
if (status) return { mcpStatus, llmRoute: { mode: this.config.llmProxyMode ?? "legacy", ...status } };
|
|
3131
|
+
} catch {
|
|
3132
|
+
}
|
|
3133
|
+
return { mcpStatus };
|
|
3134
|
+
}
|
|
2552
3135
|
// ---- Chat ----
|
|
2553
3136
|
async chat(message, options) {
|
|
2554
3137
|
this.ensureInitialized();
|
|
2555
3138
|
const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
|
|
2556
|
-
this.
|
|
3139
|
+
if (!options?._skipLock || !this.abortControllers.has(sessionId)) this.ensureSessionIsNotBeingManaged(sessionId);
|
|
3140
|
+
if (!options?._skipLock) this.currentSessionId = sessionId;
|
|
2557
3141
|
const releaseLock = options?._skipLock ? null : await this.acquireSessionLock(sessionId);
|
|
2558
|
-
const
|
|
2559
|
-
|
|
3142
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3143
|
+
const controller = execution.controller;
|
|
2560
3144
|
try {
|
|
3145
|
+
const runContext = this.prepareRunContext(controller.signal, options?.runContext);
|
|
2561
3146
|
if (options?.taskMode === "plan") {
|
|
2562
3147
|
return await this.chatWithPlan(message, options);
|
|
2563
3148
|
}
|
|
@@ -2574,44 +3159,55 @@ var Ldoing = class {
|
|
|
2574
3159
|
this.sessionManager.addMessage(sessionId, { role: "user", content: userContent });
|
|
2575
3160
|
let turn = 0;
|
|
2576
3161
|
let continueLoop = true;
|
|
3162
|
+
let completedReply;
|
|
2577
3163
|
let retried = false;
|
|
2578
3164
|
while (continueLoop && turn < maxTurns) {
|
|
3165
|
+
throwIfAborted(controller.signal);
|
|
2579
3166
|
turn++;
|
|
2580
3167
|
const session2 = this.sessionManager.getOrThrow(sessionId);
|
|
2581
|
-
const systemPrompt = this.
|
|
2582
|
-
const allTools = this.
|
|
3168
|
+
const systemPrompt = this.buildRunSystemPrompt(session2, runContext);
|
|
3169
|
+
const allTools = this.listRunTools(runContext);
|
|
2583
3170
|
const tools = this.resolveTools(allTools, { allowedTools: options?.tools, toolsByPermission: options?.toolsByPermission });
|
|
2584
3171
|
for (const name of this.computeAutoApproval(tools)) {
|
|
2585
3172
|
requireApproval.add(name);
|
|
2586
3173
|
}
|
|
2587
|
-
const ctxCheck = this.contextManager.ensureContextFits(
|
|
3174
|
+
const ctxCheck = await this.contextManager.ensureContextFits(
|
|
2588
3175
|
session2.messages,
|
|
2589
3176
|
systemPrompt,
|
|
2590
3177
|
tools,
|
|
2591
|
-
this.config.llm.model
|
|
3178
|
+
this.config.llm.model,
|
|
3179
|
+
controller.signal
|
|
2592
3180
|
);
|
|
2593
3181
|
if (ctxCheck.wasCompressed) {
|
|
2594
3182
|
session2.messages = ctxCheck.messages;
|
|
2595
|
-
this.emit("context:compress", {
|
|
3183
|
+
this.emit("context:compress", {
|
|
3184
|
+
sessionId,
|
|
3185
|
+
reason: "auto",
|
|
3186
|
+
beforeTokens: ctxCheck.beforeTokens,
|
|
3187
|
+
afterTokens: ctxCheck.afterTokens
|
|
3188
|
+
});
|
|
2596
3189
|
}
|
|
2597
|
-
|
|
3190
|
+
throwIfAborted(controller.signal);
|
|
3191
|
+
const window2 = this.contextManager.buildWindow(
|
|
2598
3192
|
session2.messages,
|
|
2599
3193
|
systemPrompt,
|
|
2600
3194
|
tools
|
|
2601
|
-
)
|
|
3195
|
+
);
|
|
3196
|
+
let contextMessages = window2.messages;
|
|
2602
3197
|
let llmResponse;
|
|
2603
3198
|
try {
|
|
2604
|
-
|
|
2605
|
-
signal: controller.signal
|
|
2606
|
-
})
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
3199
|
+
try {
|
|
3200
|
+
llmResponse = await this.llmAdapter.chat(contextMessages, tools, { signal: controller.signal });
|
|
3201
|
+
} catch (err) {
|
|
3202
|
+
if (err?.code !== ACPErrorCodes.CONTEXT_OVERFLOW || retried) throw err;
|
|
3203
|
+
const smaller = await this.prepareOverflowRetry(session2, window2, systemPrompt, tools, controller.signal);
|
|
3204
|
+
if (!smaller) throw err;
|
|
2610
3205
|
retried = true;
|
|
2611
|
-
|
|
2612
|
-
|
|
3206
|
+
contextMessages = smaller;
|
|
3207
|
+
llmResponse = await this.llmAdapter.chat(contextMessages, tools, { signal: controller.signal });
|
|
2613
3208
|
}
|
|
2614
|
-
|
|
3209
|
+
} catch (err) {
|
|
3210
|
+
if (err?.code === ACPErrorCodes.LLM_EMPTY_RESPONSE) {
|
|
2615
3211
|
if (!retried) {
|
|
2616
3212
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
|
|
2617
3213
|
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" });
|
|
@@ -2620,10 +3216,12 @@ var Ldoing = class {
|
|
|
2620
3216
|
continue;
|
|
2621
3217
|
}
|
|
2622
3218
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
|
|
3219
|
+
completedReply = ".";
|
|
2623
3220
|
continueLoop = false;
|
|
2624
3221
|
break;
|
|
2625
3222
|
}
|
|
2626
|
-
|
|
3223
|
+
if (controller.signal.aborted || err?.name === "AbortError") throw abortError();
|
|
3224
|
+
throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, `LLM call failed: ${errorMessage(err)}`, { turn });
|
|
2627
3225
|
}
|
|
2628
3226
|
if (!llmResponse.content && !llmResponse.toolCalls?.length) {
|
|
2629
3227
|
if (!retried) {
|
|
@@ -2634,9 +3232,11 @@ var Ldoing = class {
|
|
|
2634
3232
|
continue;
|
|
2635
3233
|
}
|
|
2636
3234
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
|
|
3235
|
+
completedReply = ".";
|
|
2637
3236
|
continueLoop = false;
|
|
2638
3237
|
break;
|
|
2639
3238
|
}
|
|
3239
|
+
this.reportPromptCacheUsage(sessionId, llmResponse.promptCacheUsage);
|
|
2640
3240
|
if (llmResponse.usage) {
|
|
2641
3241
|
totalInputTokens += llmResponse.usage.input;
|
|
2642
3242
|
totalOutputTokens += llmResponse.usage.output;
|
|
@@ -2647,6 +3247,7 @@ var Ldoing = class {
|
|
|
2647
3247
|
}
|
|
2648
3248
|
}
|
|
2649
3249
|
if (!llmResponse.toolCalls?.length) {
|
|
3250
|
+
completedReply = llmResponse.content;
|
|
2650
3251
|
this.sessionManager.addMessage(sessionId, {
|
|
2651
3252
|
role: "assistant",
|
|
2652
3253
|
content: llmResponse.content
|
|
@@ -2660,8 +3261,9 @@ var Ldoing = class {
|
|
|
2660
3261
|
toolCalls: llmResponse.toolCalls
|
|
2661
3262
|
});
|
|
2662
3263
|
const turnTrace = { turn, toolCalls: [] };
|
|
2663
|
-
const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval);
|
|
2664
|
-
await this.processApprovalQueue(plan.items, sessionId,
|
|
3264
|
+
const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval, runContext, tools);
|
|
3265
|
+
for await (const _event of this.processApprovalQueue(plan.items, sessionId, controller.signal)) {
|
|
3266
|
+
}
|
|
2665
3267
|
const batchTraces = await this.executeToolBatch(plan.items, sessionId, controller, plan.canParallelize, (event, data) => this.emit(event, data));
|
|
2666
3268
|
turnTrace.toolCalls = batchTraces;
|
|
2667
3269
|
const executableCount = plan.items.filter((i) => !i.resolved).length;
|
|
@@ -2669,26 +3271,27 @@ var Ldoing = class {
|
|
|
2669
3271
|
executionTrace.push(turnTrace);
|
|
2670
3272
|
}
|
|
2671
3273
|
const session = this.sessionManager.getOrThrow(sessionId);
|
|
2672
|
-
const
|
|
2673
|
-
const
|
|
2674
|
-
const chatResult = { reply, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
|
|
3274
|
+
const reply = completedReply ?? "";
|
|
3275
|
+
const terminationReason = completedReply !== void 0 ? "stop" : "max_turns";
|
|
3276
|
+
const chatResult = { reply, terminationReason, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
|
|
2675
3277
|
this.emit("chat:complete", { sessionId, reply, tokenUsage: chatResult.tokenUsage });
|
|
2676
|
-
await this.
|
|
2677
|
-
});
|
|
3278
|
+
await this.saveSessionSafely(session);
|
|
2678
3279
|
return chatResult;
|
|
2679
3280
|
} finally {
|
|
2680
|
-
|
|
3281
|
+
execution.cleanup();
|
|
2681
3282
|
if (releaseLock) releaseLock();
|
|
2682
3283
|
}
|
|
2683
3284
|
}
|
|
2684
3285
|
async *chatStream(message, options) {
|
|
2685
3286
|
this.ensureInitialized();
|
|
2686
3287
|
const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
|
|
2687
|
-
this.
|
|
3288
|
+
if (!options?._skipLock || !this.abortControllers.has(sessionId)) this.ensureSessionIsNotBeingManaged(sessionId);
|
|
3289
|
+
if (!options?._skipLock) this.currentSessionId = sessionId;
|
|
2688
3290
|
const releaseLock = options?._skipLock ? null : await this.acquireSessionLock(sessionId);
|
|
2689
|
-
const
|
|
2690
|
-
|
|
3291
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3292
|
+
const controller = execution.controller;
|
|
2691
3293
|
try {
|
|
3294
|
+
const runContext = this.prepareRunContext(controller.signal, options?.runContext);
|
|
2692
3295
|
if (options?.taskMode === "plan") {
|
|
2693
3296
|
yield* this.chatStreamWithPlan(message, options);
|
|
2694
3297
|
return;
|
|
@@ -2707,50 +3310,67 @@ var Ldoing = class {
|
|
|
2707
3310
|
this.sessionManager.addMessage(sessionId, { role: "user", content: userContent });
|
|
2708
3311
|
let turn = 0;
|
|
2709
3312
|
let continueLoop = true;
|
|
3313
|
+
let completedReply;
|
|
2710
3314
|
while (continueLoop && turn < maxTurns) {
|
|
3315
|
+
throwIfAborted(controller.signal);
|
|
2711
3316
|
turn++;
|
|
2712
3317
|
const session2 = this.sessionManager.getOrThrow(sessionId);
|
|
2713
|
-
const systemPrompt = this.
|
|
2714
|
-
const allTools = this.
|
|
3318
|
+
const systemPrompt = this.buildRunSystemPrompt(session2, runContext);
|
|
3319
|
+
const allTools = this.listRunTools(runContext);
|
|
2715
3320
|
const tools = this.resolveTools(allTools, { allowedTools: options?.tools, toolsByPermission: options?.toolsByPermission });
|
|
2716
3321
|
for (const name of this.computeAutoApproval(tools)) {
|
|
2717
3322
|
requireApproval.add(name);
|
|
2718
3323
|
}
|
|
2719
|
-
const ctxCheck = this.contextManager.ensureContextFits(
|
|
3324
|
+
const ctxCheck = await this.contextManager.ensureContextFits(
|
|
3325
|
+
session2.messages,
|
|
3326
|
+
systemPrompt,
|
|
3327
|
+
tools,
|
|
3328
|
+
this.config.llm.model,
|
|
3329
|
+
controller.signal
|
|
3330
|
+
);
|
|
2720
3331
|
if (ctxCheck.wasCompressed) {
|
|
2721
3332
|
session2.messages = ctxCheck.messages;
|
|
2722
|
-
this.emit("context:compress", {
|
|
3333
|
+
this.emit("context:compress", {
|
|
3334
|
+
sessionId,
|
|
3335
|
+
reason: "auto",
|
|
3336
|
+
beforeTokens: ctxCheck.beforeTokens,
|
|
3337
|
+
afterTokens: ctxCheck.afterTokens
|
|
3338
|
+
});
|
|
3339
|
+
yield {
|
|
3340
|
+
type: "context:compressed",
|
|
3341
|
+
sessionId,
|
|
3342
|
+
beforeTokens: ctxCheck.beforeTokens ?? 0,
|
|
3343
|
+
afterTokens: ctxCheck.afterTokens ?? 0,
|
|
3344
|
+
reason: "auto"
|
|
3345
|
+
};
|
|
2723
3346
|
}
|
|
2724
|
-
|
|
3347
|
+
throwIfAborted(controller.signal);
|
|
3348
|
+
const window2 = this.contextManager.buildWindow(session2.messages, systemPrompt, tools);
|
|
3349
|
+
let contextMessages = window2.messages;
|
|
2725
3350
|
let llmResponse;
|
|
2726
3351
|
try {
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
3352
|
+
for (; ; ) {
|
|
3353
|
+
try {
|
|
3354
|
+
llmResponse = yield* this.streamLlmResponse(contextMessages, tools, controller.signal);
|
|
3355
|
+
break;
|
|
3356
|
+
} catch (err) {
|
|
3357
|
+
if (err?.code !== ACPErrorCodes.CONTEXT_OVERFLOW || retried) throw err;
|
|
3358
|
+
const smaller = await this.prepareOverflowRetry(session2, window2, systemPrompt, tools, controller.signal);
|
|
3359
|
+
if (!smaller) throw err;
|
|
3360
|
+
retried = true;
|
|
3361
|
+
contextMessages = smaller;
|
|
2737
3362
|
}
|
|
2738
3363
|
}
|
|
2739
3364
|
} catch (err) {
|
|
2740
|
-
if (err
|
|
2741
|
-
this.compressContext(sessionId, "hybrid");
|
|
2742
|
-
retried = true;
|
|
2743
|
-
turn--;
|
|
2744
|
-
continue;
|
|
2745
|
-
}
|
|
2746
|
-
if (err.code === ACPErrorCodes.LLM_EMPTY_RESPONSE && !retried) {
|
|
3365
|
+
if (err?.code === ACPErrorCodes.LLM_EMPTY_RESPONSE && !retried) {
|
|
2747
3366
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
|
|
2748
3367
|
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" });
|
|
2749
3368
|
retried = true;
|
|
2750
3369
|
turn--;
|
|
2751
3370
|
continue;
|
|
2752
3371
|
}
|
|
2753
|
-
|
|
3372
|
+
if (controller.signal.aborted || err?.name === "AbortError") throw abortError();
|
|
3373
|
+
yield { type: "error", code: ACPErrorCodes.LLM_CALL_FAILED, message: errorMessage(err) };
|
|
2754
3374
|
return;
|
|
2755
3375
|
}
|
|
2756
3376
|
if (!llmResponse.content && !llmResponse.toolCalls?.length) {
|
|
@@ -2762,8 +3382,10 @@ var Ldoing = class {
|
|
|
2762
3382
|
continue;
|
|
2763
3383
|
}
|
|
2764
3384
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
|
|
3385
|
+
completedReply = ".";
|
|
2765
3386
|
break;
|
|
2766
3387
|
}
|
|
3388
|
+
this.reportPromptCacheUsage(sessionId, llmResponse.promptCacheUsage);
|
|
2767
3389
|
if (llmResponse.usage) {
|
|
2768
3390
|
totalInputTokens += llmResponse.usage.input;
|
|
2769
3391
|
totalOutputTokens += llmResponse.usage.output;
|
|
@@ -2774,27 +3396,21 @@ var Ldoing = class {
|
|
|
2774
3396
|
}
|
|
2775
3397
|
}
|
|
2776
3398
|
if (!llmResponse.toolCalls?.length) {
|
|
3399
|
+
completedReply = llmResponse.content;
|
|
2777
3400
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: llmResponse.content });
|
|
2778
3401
|
break;
|
|
2779
3402
|
}
|
|
2780
3403
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: llmResponse.content, toolCalls: llmResponse.toolCalls });
|
|
2781
|
-
const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval);
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
await this.processApprovalQueue(plan.items, sessionId, (event, data) => this.emit(event, data), approvalYield);
|
|
2785
|
-
for (const evt of approvalEvents) yield evt;
|
|
2786
|
-
const batchEvents = [];
|
|
2787
|
-
const batchYield = (evt) => batchEvents.push(evt);
|
|
2788
|
-
await this.executeToolBatchStream(plan.items, sessionId, controller, plan.canParallelize, batchYield);
|
|
2789
|
-
for (const evt of batchEvents) yield evt;
|
|
3404
|
+
const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval, runContext, tools);
|
|
3405
|
+
yield* this.processApprovalQueue(plan.items, sessionId, controller.signal);
|
|
3406
|
+
yield* this.streamToolBatch(plan.items, sessionId, controller, plan.canParallelize);
|
|
2790
3407
|
}
|
|
2791
3408
|
const session = this.sessionManager.getOrThrow(sessionId);
|
|
2792
|
-
const
|
|
2793
|
-
yield { type: "done",
|
|
2794
|
-
await
|
|
2795
|
-
});
|
|
3409
|
+
const saving = this.saveSessionSafely(session);
|
|
3410
|
+
yield { type: "done", terminationReason: completedReply !== void 0 ? "stop" : "max_turns", reply: completedReply ?? "", tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
|
|
3411
|
+
await saving;
|
|
2796
3412
|
} finally {
|
|
2797
|
-
|
|
3413
|
+
execution.cleanup();
|
|
2798
3414
|
if (releaseLock) releaseLock();
|
|
2799
3415
|
}
|
|
2800
3416
|
}
|
|
@@ -2804,7 +3420,6 @@ var Ldoing = class {
|
|
|
2804
3420
|
const controller = this.abortControllers.get(sid);
|
|
2805
3421
|
if (controller) {
|
|
2806
3422
|
controller.abort();
|
|
2807
|
-
this.abortControllers.delete(sid);
|
|
2808
3423
|
}
|
|
2809
3424
|
}
|
|
2810
3425
|
}
|
|
@@ -2812,7 +3427,7 @@ var Ldoing = class {
|
|
|
2812
3427
|
/**
|
|
2813
3428
|
* 在启动新的 agent loop 前获取会话锁。
|
|
2814
3429
|
* 根据并发策略处理正在运行的旧请求:
|
|
2815
|
-
* - 'abort' (默认):
|
|
3430
|
+
* - 'abort' (默认): 中止旧请求,等待其真正退出,发送前修复未配对消息,然后返回
|
|
2816
3431
|
* - 'reject': 若已有运行请求,抛错
|
|
2817
3432
|
* - 'queue': 预留,当前回退到 'abort'
|
|
2818
3433
|
*
|
|
@@ -2820,48 +3435,27 @@ var Ldoing = class {
|
|
|
2820
3435
|
*/
|
|
2821
3436
|
async acquireSessionLock(sessionId) {
|
|
2822
3437
|
const policy = this.config.concurrentPolicy ?? "abort";
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
ACPErrorCodes.SESSION_BUSY,
|
|
2828
|
-
`Session ${sessionId} is already running a chat`
|
|
2829
|
-
);
|
|
2830
|
-
}
|
|
3438
|
+
let running = this.runningSessions.get(sessionId);
|
|
3439
|
+
while (running) {
|
|
3440
|
+
this.ensureSessionIsNotBeingManaged(sessionId);
|
|
3441
|
+
if (policy === "reject") throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${sessionId} is already running a chat`);
|
|
2831
3442
|
await this.abort(sessionId);
|
|
2832
3443
|
await running.donePromise;
|
|
2833
|
-
this.
|
|
3444
|
+
running = this.runningSessions.get(sessionId);
|
|
2834
3445
|
}
|
|
3446
|
+
this.ensureInitialized();
|
|
3447
|
+
this.ensureSessionIsNotBeingManaged(sessionId);
|
|
2835
3448
|
let resolveDone;
|
|
2836
3449
|
const donePromise = new Promise((resolve) => {
|
|
2837
3450
|
resolveDone = resolve;
|
|
2838
3451
|
});
|
|
2839
|
-
|
|
3452
|
+
const owner = { donePromise, resolveDone };
|
|
3453
|
+
this.runningSessions.set(sessionId, owner);
|
|
2840
3454
|
return () => {
|
|
2841
|
-
this.runningSessions.delete(sessionId);
|
|
3455
|
+
if (this.runningSessions.get(sessionId) === owner) this.runningSessions.delete(sessionId);
|
|
2842
3456
|
resolveDone();
|
|
2843
3457
|
};
|
|
2844
3458
|
}
|
|
2845
|
-
/**
|
|
2846
|
-
* 清理被 abort 打断的半截消息:
|
|
2847
|
-
* 如果 session.messages 最后一条是 assistant 且带 tool_calls,
|
|
2848
|
-
* 但后面没有对应的 tool 消息,则删除这条 assistant。
|
|
2849
|
-
* 避免下次 LLM 调用因 tool_calls 未配对而报错。
|
|
2850
|
-
*/
|
|
2851
|
-
cleanupInterruptedMessages(sessionId) {
|
|
2852
|
-
try {
|
|
2853
|
-
const session = this.sessionManager.get(sessionId);
|
|
2854
|
-
if (!session?.messages?.length) return;
|
|
2855
|
-
const msgs = session.messages;
|
|
2856
|
-
const last = msgs[msgs.length - 1];
|
|
2857
|
-
if (last.role === "assistant" && last.toolCalls?.length) {
|
|
2858
|
-
msgs.pop();
|
|
2859
|
-
console.log(`[concurrency] cleaned up interrupted assistant(tool_calls) from session ${sessionId}`);
|
|
2860
|
-
}
|
|
2861
|
-
} catch (err) {
|
|
2862
|
-
console.warn("[concurrency] failed to cleanup interrupted messages:", err);
|
|
2863
|
-
}
|
|
2864
|
-
}
|
|
2865
3459
|
// ---- Tool Approval ----
|
|
2866
3460
|
approveTool(callId, approved, _reason) {
|
|
2867
3461
|
const resolver = this.approvalResolvers.get(callId);
|
|
@@ -2870,15 +3464,28 @@ var Ldoing = class {
|
|
|
2870
3464
|
this.approvalResolvers.delete(callId);
|
|
2871
3465
|
}
|
|
2872
3466
|
}
|
|
2873
|
-
waitForApproval(callId) {
|
|
3467
|
+
waitForApproval(callId, signal) {
|
|
3468
|
+
throwIfAborted(signal);
|
|
2874
3469
|
return new Promise((resolve, reject) => {
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
3470
|
+
const cleanup = () => {
|
|
3471
|
+
clearTimeout(timer);
|
|
3472
|
+
signal.removeEventListener("abort", abort);
|
|
3473
|
+
if (this.approvalResolvers.get(callId) === pending) this.approvalResolvers.delete(callId);
|
|
3474
|
+
};
|
|
3475
|
+
const pending = {
|
|
3476
|
+
resolve: (approved) => {
|
|
3477
|
+
cleanup();
|
|
3478
|
+
resolve(approved);
|
|
3479
|
+
},
|
|
3480
|
+
reject: (error) => {
|
|
3481
|
+
cleanup();
|
|
3482
|
+
reject(error);
|
|
2880
3483
|
}
|
|
2881
|
-
}
|
|
3484
|
+
};
|
|
3485
|
+
const abort = () => pending.reject(abortError());
|
|
3486
|
+
const timer = setTimeout(() => pending.resolve(false), 6e4);
|
|
3487
|
+
this.approvalResolvers.set(callId, pending);
|
|
3488
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
2882
3489
|
});
|
|
2883
3490
|
}
|
|
2884
3491
|
// ---- Skill Management ----
|
|
@@ -2944,31 +3551,31 @@ ${JSON.stringify(params, null, 2)}`;
|
|
|
2944
3551
|
} else {
|
|
2945
3552
|
prompt = loop.instruction;
|
|
2946
3553
|
}
|
|
2947
|
-
const result = await ctx.agent.chatInner(prompt, ctx.sessionId, { maxTurns: loop.maxTurns ?? 10, tools: loop.tools });
|
|
3554
|
+
const result = await ctx.agent.chatInner(prompt, ctx.sessionId, { maxTurns: loop.maxTurns ?? 10, tools: loop.tools, signal: ctx.signal });
|
|
2948
3555
|
return { content: [{ type: "text", text: result.reply }] };
|
|
2949
3556
|
};
|
|
2950
3557
|
}
|
|
2951
3558
|
/** Internal chat for agentLoop/workflow/sub-agent execution. Condenses inner messages after completion. */
|
|
2952
3559
|
async chatInner(prompt, sessionId, options) {
|
|
2953
3560
|
const session = this.sessionManager.getOrThrow(sessionId);
|
|
2954
|
-
const
|
|
2955
|
-
const
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
3561
|
+
const prefix = session.messages.slice();
|
|
3562
|
+
const isToolHandler = prefix[prefix.length - 1]?.role === "assistant" && !!prefix[prefix.length - 1]?.toolCalls?.length;
|
|
3563
|
+
try {
|
|
3564
|
+
const result = await this.chat(prompt, { ...options, sessionId, _skipLock: true });
|
|
3565
|
+
session.messages = prefix;
|
|
3566
|
+
if (!isToolHandler) this.sessionManager.addMessage(sessionId, { role: "assistant", content: `[Agent loop completed]: ${result.reply.slice(0, 200)}` });
|
|
3567
|
+
return result;
|
|
3568
|
+
} finally {
|
|
3569
|
+
if (isToolHandler) session.messages = prefix;
|
|
3570
|
+
session.messageCount = session.messages.length;
|
|
2963
3571
|
}
|
|
2964
|
-
return result;
|
|
2965
3572
|
}
|
|
2966
3573
|
// ---- Task Plan Execution ----
|
|
2967
3574
|
async chatWithPlan(message, options) {
|
|
2968
3575
|
const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
|
|
2969
3576
|
this.currentSessionId = sessionId;
|
|
2970
|
-
const
|
|
2971
|
-
|
|
3577
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3578
|
+
const controller = execution.controller;
|
|
2972
3579
|
const startTime = Date.now();
|
|
2973
3580
|
let totalInputTokens = 0;
|
|
2974
3581
|
let totalOutputTokens = 0;
|
|
@@ -2982,8 +3589,9 @@ ${JSON.stringify(params, null, 2)}`;
|
|
|
2982
3589
|
let plan;
|
|
2983
3590
|
try {
|
|
2984
3591
|
const planner = new TaskPlanner(this.llmAdapter);
|
|
2985
|
-
plan = await planner.plan(messageText, filteredTools);
|
|
3592
|
+
plan = await planner.plan(messageText, filteredTools, controller.signal);
|
|
2986
3593
|
} catch (err) {
|
|
3594
|
+
throwIfAborted(controller.signal);
|
|
2987
3595
|
throw new ACPError(ACPErrorCodes.TASK_PLAN_FAILED, `Task planning failed: ${err.message}`);
|
|
2988
3596
|
}
|
|
2989
3597
|
const tracker = new PlanTracker(plan, sessionId, (event, data) => this.emit(event, data));
|
|
@@ -2994,13 +3602,14 @@ ${JSON.stringify(params, null, 2)}`;
|
|
|
2994
3602
|
const maxRetry = options?.stepRetryCount ?? 1;
|
|
2995
3603
|
const continueOnFailure = options?.continueOnStepFailure ?? true;
|
|
2996
3604
|
for (let i = 0; i < plan.steps.length; i++) {
|
|
2997
|
-
|
|
3605
|
+
throwIfAborted(controller.signal);
|
|
2998
3606
|
const step = plan.steps[i];
|
|
2999
3607
|
if (step.status === "done" || step.status === "skipped") continue;
|
|
3000
3608
|
tracker.startStep(i);
|
|
3001
3609
|
let stepResult;
|
|
3002
3610
|
let succeeded = false;
|
|
3003
3611
|
for (let attempt = 0; attempt <= maxRetry; attempt++) {
|
|
3612
|
+
throwIfAborted(controller.signal);
|
|
3004
3613
|
try {
|
|
3005
3614
|
const result = await this.chatInner(step.goal, sessionId, { maxTurns: options?.maxTurns ?? 10, tools: step.tools });
|
|
3006
3615
|
stepResult = { output: result.reply, toolCalls: result.executionTrace.reduce((sum, t) => sum + t.toolCalls.length, 0), tokenUsage: result.tokenUsage, duration: result.totalDuration };
|
|
@@ -3010,6 +3619,7 @@ ${JSON.stringify(params, null, 2)}`;
|
|
|
3010
3619
|
succeeded = true;
|
|
3011
3620
|
break;
|
|
3012
3621
|
} catch (err) {
|
|
3622
|
+
throwIfAborted(controller.signal);
|
|
3013
3623
|
stepResult = { output: "", toolCalls: 0, tokenUsage: { input: 0, output: 0 }, duration: 0, error: err.message };
|
|
3014
3624
|
plan.steps[i].retryCount = attempt + 1;
|
|
3015
3625
|
}
|
|
@@ -3037,18 +3647,17 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3037
3647
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: finalReply });
|
|
3038
3648
|
const chatResult = { reply: finalReply, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
|
|
3039
3649
|
this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
|
|
3040
|
-
await this.
|
|
3041
|
-
});
|
|
3650
|
+
await this.saveSessionSafely(session);
|
|
3042
3651
|
return chatResult;
|
|
3043
3652
|
} finally {
|
|
3044
|
-
|
|
3653
|
+
execution.cleanup();
|
|
3045
3654
|
}
|
|
3046
3655
|
}
|
|
3047
3656
|
async *chatStreamWithPlan(message, options) {
|
|
3048
3657
|
const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
|
|
3049
3658
|
this.currentSessionId = sessionId;
|
|
3050
|
-
const
|
|
3051
|
-
|
|
3659
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3660
|
+
const controller = execution.controller;
|
|
3052
3661
|
const startTime = Date.now();
|
|
3053
3662
|
let totalInputTokens = 0;
|
|
3054
3663
|
let totalOutputTokens = 0;
|
|
@@ -3061,7 +3670,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3061
3670
|
let plan;
|
|
3062
3671
|
try {
|
|
3063
3672
|
const planner = new TaskPlanner(this.llmAdapter);
|
|
3064
|
-
plan = await planner.plan(messageText, filteredTools);
|
|
3673
|
+
plan = await planner.plan(messageText, filteredTools, controller.signal);
|
|
3065
3674
|
} catch (err) {
|
|
3066
3675
|
yield { type: "error", code: ACPErrorCodes.TASK_PLAN_FAILED, message: `Task planning failed: ${err.message}` };
|
|
3067
3676
|
return;
|
|
@@ -3075,7 +3684,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3075
3684
|
const maxRetry = options?.stepRetryCount ?? 1;
|
|
3076
3685
|
const continueOnFailure = options?.continueOnStepFailure ?? true;
|
|
3077
3686
|
for (let i = 0; i < plan.steps.length; i++) {
|
|
3078
|
-
|
|
3687
|
+
throwIfAborted(controller.signal);
|
|
3079
3688
|
const step = plan.steps[i];
|
|
3080
3689
|
if (step.status === "done" || step.status === "skipped") continue;
|
|
3081
3690
|
tracker.startStep(i);
|
|
@@ -3083,6 +3692,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3083
3692
|
let stepResult;
|
|
3084
3693
|
let succeeded = false;
|
|
3085
3694
|
for (let attempt = 0; attempt <= maxRetry; attempt++) {
|
|
3695
|
+
throwIfAborted(controller.signal);
|
|
3086
3696
|
try {
|
|
3087
3697
|
const result = await this.chatInner(step.goal, sessionId, { maxTurns: options?.maxTurns ?? 10, tools: step.tools });
|
|
3088
3698
|
stepResult = { output: result.reply, toolCalls: result.executionTrace.reduce((sum, t) => sum + t.toolCalls.length, 0), tokenUsage: result.tokenUsage, duration: result.totalDuration };
|
|
@@ -3091,6 +3701,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3091
3701
|
succeeded = true;
|
|
3092
3702
|
break;
|
|
3093
3703
|
} catch (err) {
|
|
3704
|
+
throwIfAborted(controller.signal);
|
|
3094
3705
|
stepResult = { output: "", toolCalls: 0, tokenUsage: { input: 0, output: 0 }, duration: 0, error: err.message };
|
|
3095
3706
|
plan.steps[i].retryCount = attempt + 1;
|
|
3096
3707
|
}
|
|
@@ -3118,11 +3729,11 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3118
3729
|
|
|
3119
3730
|
${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
3120
3731
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: finalReply });
|
|
3732
|
+
const saving = this.saveSessionSafely(session);
|
|
3121
3733
|
yield { type: "done", reply: finalReply, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
|
|
3122
|
-
await
|
|
3123
|
-
});
|
|
3734
|
+
await saving;
|
|
3124
3735
|
} finally {
|
|
3125
|
-
|
|
3736
|
+
execution.cleanup();
|
|
3126
3737
|
}
|
|
3127
3738
|
}
|
|
3128
3739
|
async resumeTask(sessionId, options) {
|
|
@@ -3132,6 +3743,8 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3132
3743
|
if (!savedPlan) {
|
|
3133
3744
|
throw new ACPError(ACPErrorCodes.TASK_PLAN_FAILED, "No task plan found in session");
|
|
3134
3745
|
}
|
|
3746
|
+
const releaseLock = await this.acquireSessionLock(sessionId);
|
|
3747
|
+
this.currentSessionId = sessionId;
|
|
3135
3748
|
for (const step of savedPlan.steps) {
|
|
3136
3749
|
if (step.status === "failed") {
|
|
3137
3750
|
step.status = "pending";
|
|
@@ -3139,8 +3752,8 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3139
3752
|
step.result = void 0;
|
|
3140
3753
|
}
|
|
3141
3754
|
}
|
|
3142
|
-
const
|
|
3143
|
-
|
|
3755
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3756
|
+
const controller = execution.controller;
|
|
3144
3757
|
const startTime = Date.now();
|
|
3145
3758
|
let totalInputTokens = 0;
|
|
3146
3759
|
let totalOutputTokens = 0;
|
|
@@ -3151,13 +3764,14 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3151
3764
|
const maxRetry = options?.stepRetryCount ?? 1;
|
|
3152
3765
|
const continueOnFailure = options?.continueOnStepFailure ?? true;
|
|
3153
3766
|
for (let i = 0; i < savedPlan.steps.length; i++) {
|
|
3154
|
-
|
|
3767
|
+
throwIfAborted(controller.signal);
|
|
3155
3768
|
const step = savedPlan.steps[i];
|
|
3156
3769
|
if (step.status === "done" || step.status === "skipped") continue;
|
|
3157
3770
|
tracker.startStep(i);
|
|
3158
3771
|
let stepResult;
|
|
3159
3772
|
let succeeded = false;
|
|
3160
3773
|
for (let attempt = 0; attempt <= maxRetry; attempt++) {
|
|
3774
|
+
throwIfAborted(controller.signal);
|
|
3161
3775
|
try {
|
|
3162
3776
|
const result = await this.chatInner(step.goal, sessionId, { maxTurns: options?.maxTurns ?? 10, tools: step.tools });
|
|
3163
3777
|
stepResult = { output: result.reply, toolCalls: result.executionTrace.reduce((sum, t) => sum + t.toolCalls.length, 0), tokenUsage: result.tokenUsage, duration: result.totalDuration };
|
|
@@ -3167,6 +3781,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3167
3781
|
succeeded = true;
|
|
3168
3782
|
break;
|
|
3169
3783
|
} catch (err) {
|
|
3784
|
+
throwIfAborted(controller.signal);
|
|
3170
3785
|
stepResult = { output: "", toolCalls: 0, tokenUsage: { input: 0, output: 0 }, duration: 0, error: err.message };
|
|
3171
3786
|
savedPlan.steps[i].retryCount = attempt + 1;
|
|
3172
3787
|
}
|
|
@@ -3194,11 +3809,11 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
|
|
|
3194
3809
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: finalReply });
|
|
3195
3810
|
const chatResult = { reply: finalReply, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
|
|
3196
3811
|
this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
|
|
3197
|
-
await this.
|
|
3198
|
-
});
|
|
3812
|
+
await this.saveSessionSafely(session);
|
|
3199
3813
|
return chatResult;
|
|
3200
3814
|
} finally {
|
|
3201
|
-
|
|
3815
|
+
execution.cleanup();
|
|
3816
|
+
releaseLock();
|
|
3202
3817
|
}
|
|
3203
3818
|
}
|
|
3204
3819
|
getTaskPlan(sessionId) {
|
|
@@ -3214,8 +3829,8 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
|
|
|
3214
3829
|
async chatWithGraph(message, options) {
|
|
3215
3830
|
const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
|
|
3216
3831
|
this.currentSessionId = sessionId;
|
|
3217
|
-
const
|
|
3218
|
-
|
|
3832
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3833
|
+
const controller = execution.controller;
|
|
3219
3834
|
const startTime = Date.now();
|
|
3220
3835
|
try {
|
|
3221
3836
|
const userContent = this.buildUserContent(message, options?.attachments);
|
|
@@ -3226,8 +3841,9 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
|
|
|
3226
3841
|
let plan;
|
|
3227
3842
|
try {
|
|
3228
3843
|
const planner = new GraphPlanner(this.llmAdapter);
|
|
3229
|
-
plan = await planner.planGraph(messageText, filteredTools);
|
|
3844
|
+
plan = await planner.planGraph(messageText, filteredTools, controller.signal);
|
|
3230
3845
|
} catch (err) {
|
|
3846
|
+
throwIfAborted(controller.signal);
|
|
3231
3847
|
throw new ACPError(ACPErrorCodes.GRAPH_PLAN_FAILED, `Graph planning failed: ${err.message}`);
|
|
3232
3848
|
}
|
|
3233
3849
|
const graph = new TaskGraph(plan.nodes);
|
|
@@ -3241,6 +3857,7 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
|
|
|
3241
3857
|
const executor = new GraphExecutor({
|
|
3242
3858
|
graph,
|
|
3243
3859
|
parentAgent: this,
|
|
3860
|
+
parentSignal: controller.signal,
|
|
3244
3861
|
plan,
|
|
3245
3862
|
maxConcurrency: options?.graphConfig?.maxConcurrency ?? 3,
|
|
3246
3863
|
maxDepth: options?.graphConfig?.maxDepth ?? 2,
|
|
@@ -3248,6 +3865,7 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
|
|
|
3248
3865
|
emit: (event, data) => this.emit(event, data)
|
|
3249
3866
|
});
|
|
3250
3867
|
const finalPlan = await executor.execute();
|
|
3868
|
+
throwIfAborted(controller.signal);
|
|
3251
3869
|
const resultLines = Object.entries(finalPlan.results).filter(([, r]) => r.status === "done" && r.output).map(([nodeId, r]) => `[${nodeId}] ${r.output.slice(0, 200)}`);
|
|
3252
3870
|
const progress = executor.getProgress();
|
|
3253
3871
|
const finalReply = resultLines.length > 0 ? `Graph task completed (${progress.done}/${progress.total} nodes done, ${progress.failed} failed, ${progress.bypassed} bypassed).
|
|
@@ -3266,18 +3884,17 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3266
3884
|
totalDuration: Date.now() - startTime
|
|
3267
3885
|
};
|
|
3268
3886
|
this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
|
|
3269
|
-
await this.
|
|
3270
|
-
});
|
|
3887
|
+
await this.saveSessionSafely(session);
|
|
3271
3888
|
return chatResult;
|
|
3272
3889
|
} finally {
|
|
3273
|
-
|
|
3890
|
+
execution.cleanup();
|
|
3274
3891
|
}
|
|
3275
3892
|
}
|
|
3276
3893
|
async *chatStreamWithGraph(message, options) {
|
|
3277
3894
|
const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
|
|
3278
3895
|
this.currentSessionId = sessionId;
|
|
3279
|
-
const
|
|
3280
|
-
|
|
3896
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3897
|
+
const controller = execution.controller;
|
|
3281
3898
|
const startTime = Date.now();
|
|
3282
3899
|
let totalInputTokens = 0;
|
|
3283
3900
|
let totalOutputTokens = 0;
|
|
@@ -3290,7 +3907,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3290
3907
|
let plan;
|
|
3291
3908
|
try {
|
|
3292
3909
|
const planner = new GraphPlanner(this.llmAdapter);
|
|
3293
|
-
plan = await planner.planGraph(messageText, filteredTools);
|
|
3910
|
+
plan = await planner.planGraph(messageText, filteredTools, controller.signal);
|
|
3294
3911
|
} catch (err) {
|
|
3295
3912
|
yield { type: "error", code: ACPErrorCodes.GRAPH_PLAN_FAILED, message: `Graph planning failed: ${err.message}` };
|
|
3296
3913
|
return;
|
|
@@ -3307,6 +3924,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3307
3924
|
const executor = new GraphExecutor({
|
|
3308
3925
|
graph,
|
|
3309
3926
|
parentAgent: this,
|
|
3927
|
+
parentSignal: controller.signal,
|
|
3310
3928
|
plan,
|
|
3311
3929
|
maxConcurrency: options?.graphConfig?.maxConcurrency ?? 3,
|
|
3312
3930
|
maxDepth: options?.graphConfig?.maxDepth ?? 2,
|
|
@@ -3318,6 +3936,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3318
3936
|
}
|
|
3319
3937
|
});
|
|
3320
3938
|
const finalPlan = await executor.execute();
|
|
3939
|
+
throwIfAborted(controller.signal);
|
|
3321
3940
|
const progress = executor.getProgress();
|
|
3322
3941
|
const resultLines = Object.entries(finalPlan.results).filter(([, r]) => r.status === "done" && r.output).map(([nodeId, r]) => `[${nodeId}] ${r.output.slice(0, 200)}`);
|
|
3323
3942
|
const finalReply = resultLines.length > 0 ? `Graph task completed (${progress.done}/${progress.total} nodes done, ${progress.failed} failed, ${progress.bypassed} bypassed).
|
|
@@ -3329,11 +3948,11 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3329
3948
|
const session = this.sessionManager.getOrThrow(sessionId);
|
|
3330
3949
|
session.meta = session.meta ?? {};
|
|
3331
3950
|
session.meta.graphPlan = finalPlan;
|
|
3951
|
+
const saving = this.saveSessionSafely(session);
|
|
3332
3952
|
yield { type: "done", reply: finalReply, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
|
|
3333
|
-
await
|
|
3334
|
-
});
|
|
3953
|
+
await saving;
|
|
3335
3954
|
} finally {
|
|
3336
|
-
|
|
3955
|
+
execution.cleanup();
|
|
3337
3956
|
}
|
|
3338
3957
|
}
|
|
3339
3958
|
/** Resume a graph execution from checkpoint */
|
|
@@ -3344,6 +3963,8 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3344
3963
|
if (!savedPlan) {
|
|
3345
3964
|
throw new ACPError(ACPErrorCodes.GRAPH_PLAN_FAILED, "No graph plan found in session");
|
|
3346
3965
|
}
|
|
3966
|
+
const releaseLock = await this.acquireSessionLock(sessionId);
|
|
3967
|
+
this.currentSessionId = sessionId;
|
|
3347
3968
|
for (const [nodeId, result] of Object.entries(savedPlan.results)) {
|
|
3348
3969
|
if (result.status === "failed" || result.status === "bypassed") {
|
|
3349
3970
|
savedPlan.results[nodeId] = {
|
|
@@ -3356,14 +3977,15 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3356
3977
|
}
|
|
3357
3978
|
}
|
|
3358
3979
|
savedPlan.status = "executing";
|
|
3359
|
-
const
|
|
3360
|
-
|
|
3980
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3981
|
+
const controller = execution.controller;
|
|
3361
3982
|
const startTime = Date.now();
|
|
3362
3983
|
try {
|
|
3363
3984
|
const graph = new TaskGraph(savedPlan.nodes);
|
|
3364
3985
|
const executor = new GraphExecutor({
|
|
3365
3986
|
graph,
|
|
3366
3987
|
parentAgent: this,
|
|
3988
|
+
parentSignal: controller.signal,
|
|
3367
3989
|
plan: savedPlan,
|
|
3368
3990
|
maxConcurrency: options?.graphConfig?.maxConcurrency ?? 3,
|
|
3369
3991
|
maxDepth: options?.graphConfig?.maxDepth ?? 2,
|
|
@@ -3371,6 +3993,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3371
3993
|
emit: (event, data) => this.emit(event, data)
|
|
3372
3994
|
});
|
|
3373
3995
|
const finalPlan = await executor.execute();
|
|
3996
|
+
throwIfAborted(controller.signal);
|
|
3374
3997
|
const resultLines = Object.entries(finalPlan.results).filter(([, r]) => r.status === "done" && r.output).map(([nodeId, r]) => `[${nodeId}] ${r.output.slice(0, 200)}`);
|
|
3375
3998
|
const progress = executor.getProgress();
|
|
3376
3999
|
const finalReply = resultLines.length > 0 ? `Graph resumed & completed (${progress.done}/${progress.total} nodes done).
|
|
@@ -3387,11 +4010,11 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3387
4010
|
totalDuration: Date.now() - startTime
|
|
3388
4011
|
};
|
|
3389
4012
|
this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
|
|
3390
|
-
await this.
|
|
3391
|
-
});
|
|
4013
|
+
await this.saveSessionSafely(session);
|
|
3392
4014
|
return chatResult;
|
|
3393
4015
|
} finally {
|
|
3394
|
-
|
|
4016
|
+
execution.cleanup();
|
|
4017
|
+
releaseLock();
|
|
3395
4018
|
}
|
|
3396
4019
|
}
|
|
3397
4020
|
/** Get graph plan from session */
|
|
@@ -3412,6 +4035,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3412
4035
|
const failedSteps = [];
|
|
3413
4036
|
const total = workflow.steps.length;
|
|
3414
4037
|
for (let i = 0; i < workflow.steps.length; i++) {
|
|
4038
|
+
throwIfAborted(ctx.signal);
|
|
3415
4039
|
const step = workflow.steps[i];
|
|
3416
4040
|
const interpolatedPrompt = interpolateTemplate(step.prompt, {
|
|
3417
4041
|
inputs: params,
|
|
@@ -3428,7 +4052,8 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3428
4052
|
try {
|
|
3429
4053
|
const result = await ctx.agent.chatInner(interpolatedPrompt, ctx.sessionId, {
|
|
3430
4054
|
maxTurns: step.maxTurns ?? 10,
|
|
3431
|
-
tools: step.tools
|
|
4055
|
+
tools: step.tools,
|
|
4056
|
+
signal: ctx.signal
|
|
3432
4057
|
});
|
|
3433
4058
|
stepOutputs[step.name] = { output: result.reply };
|
|
3434
4059
|
completedSteps.push(step.name);
|
|
@@ -3513,6 +4138,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3513
4138
|
const executor = new GraphExecutor({
|
|
3514
4139
|
graph,
|
|
3515
4140
|
parentAgent: this,
|
|
4141
|
+
parentSignal: ctx.signal,
|
|
3516
4142
|
plan,
|
|
3517
4143
|
maxConcurrency: 3,
|
|
3518
4144
|
maxDepth: 2,
|
|
@@ -3520,6 +4146,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3520
4146
|
emit: (event, data) => this.emit(event, data)
|
|
3521
4147
|
});
|
|
3522
4148
|
const finalPlan = await executor.execute();
|
|
4149
|
+
throwIfAborted(ctx.signal);
|
|
3523
4150
|
const aggregation = skill.subgraph?.aggregation ?? "concat";
|
|
3524
4151
|
if (aggregation === "last") {
|
|
3525
4152
|
const lastDone = Object.values(finalPlan.results).reverse().find((r) => r.status === "done" && r.output);
|
|
@@ -3558,6 +4185,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3558
4185
|
const executor = new GraphExecutor({
|
|
3559
4186
|
graph,
|
|
3560
4187
|
parentAgent: this,
|
|
4188
|
+
parentSignal: ctx.signal,
|
|
3561
4189
|
plan,
|
|
3562
4190
|
maxConcurrency: 3,
|
|
3563
4191
|
maxDepth: 2,
|
|
@@ -3565,6 +4193,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3565
4193
|
emit: (event, data) => this.emit(event, data)
|
|
3566
4194
|
});
|
|
3567
4195
|
const finalPlan = await executor.execute();
|
|
4196
|
+
throwIfAborted(ctx.signal);
|
|
3568
4197
|
const aggregation = subgraphDef.aggregation ?? "concat";
|
|
3569
4198
|
if (aggregation === "last") {
|
|
3570
4199
|
const lastDone = Object.values(finalPlan.results).reverse().find((r) => r.status === "done" && r.output);
|
|
@@ -3583,7 +4212,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3583
4212
|
// ---- Session Management ----
|
|
3584
4213
|
createSession(options) {
|
|
3585
4214
|
const session = this.sessionManager.create(options);
|
|
3586
|
-
this.currentSessionId = session.id;
|
|
4215
|
+
if (options?.activate !== false) this.currentSessionId = session.id;
|
|
3587
4216
|
return session;
|
|
3588
4217
|
}
|
|
3589
4218
|
getSession(sessionId) {
|
|
@@ -3596,17 +4225,75 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3596
4225
|
return this.sessionManager.search(query, options);
|
|
3597
4226
|
}
|
|
3598
4227
|
mergeSessions(fromId, toId, strategy) {
|
|
4228
|
+
this.ensureSessionIsNotBeingManaged(fromId);
|
|
4229
|
+
this.ensureSessionIsNotBeingManaged(toId);
|
|
3599
4230
|
this.sessionManager.merge(fromId, toId, strategy);
|
|
3600
4231
|
}
|
|
3601
4232
|
deleteSession(sessionId) {
|
|
4233
|
+
this.ensureSessionIsNotBeingManaged(sessionId);
|
|
4234
|
+
this.removeLiveSession(sessionId);
|
|
4235
|
+
}
|
|
4236
|
+
removeLiveSession(sessionId) {
|
|
3602
4237
|
this.sessionManager.delete(sessionId);
|
|
3603
4238
|
if (this.currentSessionId === sessionId) {
|
|
3604
4239
|
this.currentSessionId = null;
|
|
3605
4240
|
}
|
|
3606
4241
|
}
|
|
3607
4242
|
archiveSession(sessionId) {
|
|
4243
|
+
this.ensureSessionIsNotBeingManaged(sessionId);
|
|
3608
4244
|
this.sessionManager.archive(sessionId);
|
|
3609
4245
|
}
|
|
4246
|
+
/** List committed history without loading it into the Agent. */
|
|
4247
|
+
async listStoredSessions(options) {
|
|
4248
|
+
await Promise.all(this.sessionStorageQueues.values());
|
|
4249
|
+
return (await this.persistence.listSessions(options)).map((session) => this.snapshotSession(session));
|
|
4250
|
+
}
|
|
4251
|
+
/** Load one stored session on demand. A live session takes precedence. */
|
|
4252
|
+
async restoreSession(sessionId, options) {
|
|
4253
|
+
return this.manageSessionStorage([sessionId], async () => {
|
|
4254
|
+
const session = await this.queueSessionStorage(sessionId, () => this.readSessionForStorage(sessionId));
|
|
4255
|
+
if (!session) return null;
|
|
4256
|
+
this.sessionManager.restore(session);
|
|
4257
|
+
if (options?.activate !== false) this.currentSessionId = sessionId;
|
|
4258
|
+
return session;
|
|
4259
|
+
});
|
|
4260
|
+
}
|
|
4261
|
+
/** Archive a stored or live session, publishing the update after commit. */
|
|
4262
|
+
async archiveStoredSession(sessionId) {
|
|
4263
|
+
return this.manageSessionStorage([sessionId], () => this.queueSessionStorage(sessionId, async () => {
|
|
4264
|
+
const session = await this.requireSessionForStorage(sessionId);
|
|
4265
|
+
await this.commitStoredSession(session, { ...session, status: "archived", updatedAt: Date.now() });
|
|
4266
|
+
}));
|
|
4267
|
+
}
|
|
4268
|
+
/** Commit an idle session, then release its live copy. Restore it by ID when needed. */
|
|
4269
|
+
async unloadSession(sessionId) {
|
|
4270
|
+
return this.manageSessionStorage([sessionId], () => this.queueSessionStorage(sessionId, async () => {
|
|
4271
|
+
const session = this.sessionManager.get(sessionId);
|
|
4272
|
+
if (!session) return false;
|
|
4273
|
+
await this.persistence.saveSession(this.snapshotSession(session));
|
|
4274
|
+
this.removeLiveSession(sessionId);
|
|
4275
|
+
return true;
|
|
4276
|
+
}));
|
|
4277
|
+
}
|
|
4278
|
+
/** Merge history into the target session and commit the target. */
|
|
4279
|
+
async mergeStoredSessions(fromId, toId, strategy) {
|
|
4280
|
+
return this.manageSessionStorage([fromId, toId], () => this.queueSessionStorage(toId, async () => {
|
|
4281
|
+
const from = await this.requireSessionForStorage(fromId);
|
|
4282
|
+
const to = fromId === toId ? from : await this.requireSessionForStorage(toId);
|
|
4283
|
+
const updated = { ...to, messages: [...to.messages] };
|
|
4284
|
+
SessionManager.mergeContent(from, updated, strategy);
|
|
4285
|
+
await this.commitStoredSession(to, updated);
|
|
4286
|
+
}));
|
|
4287
|
+
}
|
|
4288
|
+
/** Delete committed and live history, including when a chat save is pending. */
|
|
4289
|
+
async deleteStoredSession(sessionId) {
|
|
4290
|
+
return this.manageSessionStorage([sessionId], (state) => this.queueSessionStorage(sessionId, async () => {
|
|
4291
|
+
await this.persistence.deleteSession(sessionId);
|
|
4292
|
+
state.deleted = true;
|
|
4293
|
+
this.abortControllers.get(sessionId)?.abort();
|
|
4294
|
+
this.removeLiveSession(sessionId);
|
|
4295
|
+
}), { allowRunning: true });
|
|
4296
|
+
}
|
|
3610
4297
|
// ---- Context Management ----
|
|
3611
4298
|
getContext(sessionId, format = "window") {
|
|
3612
4299
|
const session = this.sessionManager.getOrThrow(sessionId);
|
|
@@ -3621,25 +4308,56 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3621
4308
|
}
|
|
3622
4309
|
return this.contextManager.search(allMessages, query, options);
|
|
3623
4310
|
}
|
|
3624
|
-
compressContext(sessionId, strategy) {
|
|
4311
|
+
async compressContext(sessionId, strategy) {
|
|
4312
|
+
this.ensureSessionIsNotBeingManaged(sessionId);
|
|
3625
4313
|
const session = this.sessionManager.getOrThrow(sessionId);
|
|
3626
|
-
|
|
4314
|
+
this.compressingSessions.set(sessionId, (this.compressingSessions.get(sessionId) ?? 0) + 1);
|
|
4315
|
+
try {
|
|
4316
|
+
const result = await this.contextManager.compress(session.messages, strategy);
|
|
4317
|
+
session.messages = result.messages;
|
|
4318
|
+
session.tokenCount = result.afterTokens;
|
|
4319
|
+
session.updatedAt = Date.now();
|
|
4320
|
+
if (result.afterTokens < result.beforeTokens) {
|
|
4321
|
+
this.emit("context:compress", { sessionId, beforeTokens: result.beforeTokens, afterTokens: result.afterTokens, reason: "manual" });
|
|
4322
|
+
}
|
|
4323
|
+
return { beforeTokens: result.beforeTokens, afterTokens: result.afterTokens, compressionRatio: result.afterTokens / (result.beforeTokens || 1) };
|
|
4324
|
+
} finally {
|
|
4325
|
+
const remaining = this.compressingSessions.get(sessionId) - 1;
|
|
4326
|
+
if (remaining) this.compressingSessions.set(sessionId, remaining);
|
|
4327
|
+
else this.compressingSessions.delete(sessionId);
|
|
4328
|
+
}
|
|
4329
|
+
}
|
|
4330
|
+
async prepareOverflowRetry(session, rejected, systemPrompt, tools, signal) {
|
|
4331
|
+
const result = await this.contextManager.compressForOverflow(session.messages, rejected, systemPrompt, tools, signal);
|
|
4332
|
+
if (!result) return;
|
|
4333
|
+
throwIfAborted(signal);
|
|
3627
4334
|
session.messages = result.messages;
|
|
3628
4335
|
session.tokenCount = result.afterTokens;
|
|
3629
4336
|
session.updatedAt = Date.now();
|
|
3630
|
-
this.emit("context:compress", {
|
|
3631
|
-
|
|
4337
|
+
this.emit("context:compress", {
|
|
4338
|
+
sessionId: session.id,
|
|
4339
|
+
beforeTokens: rejected.usedTokens,
|
|
4340
|
+
afterTokens: result.window.usedTokens,
|
|
4341
|
+
reason: "manual"
|
|
4342
|
+
});
|
|
4343
|
+
return result.window.messages;
|
|
3632
4344
|
}
|
|
3633
4345
|
// ---- Runtime Config ----
|
|
3634
4346
|
configure(config) {
|
|
4347
|
+
if (this.config.llmProxyMode !== void 0 || config.llmProxyMode !== void 0) {
|
|
4348
|
+
this.configureProxy(config);
|
|
4349
|
+
return;
|
|
4350
|
+
}
|
|
3635
4351
|
if (config.llm) {
|
|
3636
4352
|
Object.assign(this.config.llm, config.llm);
|
|
3637
4353
|
this.llmAdapter = new OpenAIAdapter(this.config.llm);
|
|
3638
4354
|
this.contextManager.setModel(this.config.llm.model);
|
|
4355
|
+
this.contextManager.setSummarizer(createLLMSummarizer(this.llmAdapter));
|
|
3639
4356
|
}
|
|
3640
4357
|
if (config.serverUrl) {
|
|
3641
4358
|
this.config.serverUrl = config.serverUrl;
|
|
3642
4359
|
this.transport = new HttpTransport(config.serverUrl, this.config.authToken, this.config.headers);
|
|
4360
|
+
this.mcpStatus = "unchecked";
|
|
3643
4361
|
}
|
|
3644
4362
|
if (config.authToken) {
|
|
3645
4363
|
this.config.authToken = config.authToken;
|
|
@@ -3648,6 +4366,33 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3648
4366
|
this.config.systemPrompt = config.systemPrompt;
|
|
3649
4367
|
}
|
|
3650
4368
|
}
|
|
4369
|
+
configureProxy(config) {
|
|
4370
|
+
const current = this.config;
|
|
4371
|
+
const next = {
|
|
4372
|
+
...current,
|
|
4373
|
+
llm: { ...current.llm, ...config.llm },
|
|
4374
|
+
llmProxyMode: config.llmProxyMode === void 0 ? current.llmProxyMode : config.llmProxyMode,
|
|
4375
|
+
serverUrl: config.serverUrl === void 0 ? current.serverUrl : config.serverUrl,
|
|
4376
|
+
authToken: config.authToken ?? current.authToken,
|
|
4377
|
+
headers: config.headers ?? current.headers,
|
|
4378
|
+
systemPrompt: config.systemPrompt ?? current.systemPrompt
|
|
4379
|
+
};
|
|
4380
|
+
const { llm, serverUrl } = resolveProxyConfig(next);
|
|
4381
|
+
const transportChanged = next.serverUrl !== current.serverUrl || next.authToken !== current.authToken || next.headers !== current.headers || current.llmProxyMode === void 0;
|
|
4382
|
+
this.config = next;
|
|
4383
|
+
if (config.llm || next.llmProxyMode !== current.llmProxyMode || next.llmProxyMode === "listdoing-server" && (next.serverUrl !== current.serverUrl || next.authToken !== current.authToken)) {
|
|
4384
|
+
this.llmAdapter = new OpenAIAdapter(llm, { rejectRedirects: true });
|
|
4385
|
+
this.contextManager.setModel(llm.model);
|
|
4386
|
+
this.contextManager.setSummarizer(createLLMSummarizer(this.llmAdapter));
|
|
4387
|
+
}
|
|
4388
|
+
if (transportChanged) {
|
|
4389
|
+
this.transport = serverUrl ? new HttpTransport(serverUrl, next.authToken, next.headers) : new LocalTransport();
|
|
4390
|
+
this.mcpStatus = "unchecked";
|
|
4391
|
+
for (const skill of this.skillRegistry.list()) {
|
|
4392
|
+
if (this.discoveredServerSkills.has(skill)) this.skillRegistry.unregister(skill.name);
|
|
4393
|
+
}
|
|
4394
|
+
}
|
|
4395
|
+
}
|
|
3651
4396
|
// ---- Events ----
|
|
3652
4397
|
on(event, handler) {
|
|
3653
4398
|
if (!this.eventHandlers.has(event)) {
|
|
@@ -3668,10 +4413,15 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3668
4413
|
}
|
|
3669
4414
|
// ---- Private Helpers ----
|
|
3670
4415
|
/**
|
|
3671
|
-
*
|
|
3672
|
-
* Forwards llm_text events to the caller and collects the full StandardLLMResponse.
|
|
4416
|
+
* Forward streaming events, or emit the complete response for a non-streaming adapter.
|
|
3673
4417
|
*/
|
|
3674
4418
|
async *streamLlmResponse(messages, tools, signal) {
|
|
4419
|
+
if (typeof this.llmAdapter.chatStream !== "function") {
|
|
4420
|
+
const response = await this.llmAdapter.chat(messages, tools, { signal });
|
|
4421
|
+
if (response.reasoning) yield { type: "llm_thinking", content: response.reasoning };
|
|
4422
|
+
if (response.content) yield { type: "llm_text", content: response.content };
|
|
4423
|
+
return response;
|
|
4424
|
+
}
|
|
3675
4425
|
let fullResponse = null;
|
|
3676
4426
|
for await (const event of this.llmAdapter.chatStream(messages, tools, { signal })) {
|
|
3677
4427
|
if (event.type === "llm_text" || event.type === "llm_thinking") {
|
|
@@ -3765,59 +4515,53 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3765
4515
|
});
|
|
3766
4516
|
}
|
|
3767
4517
|
// ── Parallel tool execution helpers ──
|
|
3768
|
-
planToolExecution(toolCalls, chatOptions, requireApproval) {
|
|
4518
|
+
planToolExecution(toolCalls, chatOptions, requireApproval, runContext, exposedTools) {
|
|
3769
4519
|
const items = toolCalls.map((tc) => {
|
|
3770
4520
|
const isMcp = this.mcpRouter.isMcpTool(tc.name);
|
|
3771
|
-
const
|
|
4521
|
+
const scopedSkill = runContext?.registry.get(tc.name);
|
|
4522
|
+
const skillDef = scopedSkill ?? this.skillRegistry.get(tc.name);
|
|
3772
4523
|
const location = isMcp ? "server" : skillDef?.location === "server" ? "server" : "local";
|
|
3773
|
-
const permPolicy = this.checkToolPermission(skillDef, tc.arguments, chatOptions, requireApproval);
|
|
4524
|
+
const permPolicy = scopedSkill && !exposedTools.some((tool) => tool.name === tc.name) ? "disabled" : this.checkToolPermission(skillDef, tc.arguments, chatOptions, requireApproval);
|
|
3774
4525
|
const parallelSafe = skillDef?.parallelSafe ?? true;
|
|
3775
4526
|
return { toolCall: tc, skillDef, location, permPolicy, parallelSafe };
|
|
3776
4527
|
});
|
|
3777
4528
|
const canParallelize = items.every((item) => item.parallelSafe);
|
|
3778
4529
|
return { canParallelize, items };
|
|
3779
4530
|
}
|
|
3780
|
-
async processApprovalQueue(items, sessionId,
|
|
4531
|
+
async *processApprovalQueue(items, sessionId, signal) {
|
|
3781
4532
|
for (const item of items) {
|
|
3782
|
-
|
|
3783
|
-
|
|
3784
|
-
|
|
3785
|
-
|
|
3786
|
-
|
|
3787
|
-
|
|
3788
|
-
|
|
3789
|
-
|
|
3790
|
-
|
|
3791
|
-
|
|
3792
|
-
|
|
3793
|
-
}
|
|
3794
|
-
|
|
3795
|
-
|
|
3796
|
-
|
|
3797
|
-
|
|
3798
|
-
|
|
3799
|
-
const approved = await this.waitForApproval(item.toolCall.id);
|
|
3800
|
-
if (!approved) {
|
|
3801
|
-
const rejectResult = {
|
|
3802
|
-
content: [{ type: "text", text: "Tool call rejected by user." }],
|
|
3803
|
-
isError: true
|
|
3804
|
-
};
|
|
3805
|
-
item.resolved = true;
|
|
3806
|
-
item.result = rejectResult;
|
|
3807
|
-
this.addToolResult(sessionId, item.toolCall, rejectResult);
|
|
3808
|
-
emit("tool:result", { sessionId, toolName: item.toolCall.name, isError: true, summary: "Rejected", duration: 0 });
|
|
3809
|
-
if (yieldEvent) {
|
|
3810
|
-
yieldEvent({ type: "tool_result", callId: item.toolCall.id, toolName: item.toolCall.name, isError: true, summary: "Rejected", duration: 0 });
|
|
3811
|
-
}
|
|
4533
|
+
throwIfAborted(signal);
|
|
4534
|
+
const tc = item.toolCall;
|
|
4535
|
+
this.emit("tool:call", { sessionId, toolName: tc.name, arguments: tc.arguments, location: item.location });
|
|
4536
|
+
yield { type: "tool_call", callId: tc.id, toolName: tc.name, arguments: tc.arguments, location: item.location };
|
|
4537
|
+
let reason;
|
|
4538
|
+
if (tc.parseError) reason = `Invalid tool arguments: ${tc.parseError}`;
|
|
4539
|
+
else if (item.permPolicy === "disabled") reason = `Tool "${tc.name}" is disabled in current permission mode.`;
|
|
4540
|
+
else if (item.permPolicy === "approval" || item.permPolicy === "force-approval") {
|
|
4541
|
+
const decision = this.waitForApproval(tc.id, signal);
|
|
4542
|
+
decision.catch(() => {
|
|
4543
|
+
});
|
|
4544
|
+
this.emit("tool:approval", { sessionId, toolName: tc.name, arguments: tc.arguments, callId: tc.id });
|
|
4545
|
+
yield { type: "tool_approval", callId: tc.id, toolName: tc.name, arguments: tc.arguments };
|
|
4546
|
+
try {
|
|
4547
|
+
if (!await decision) reason = "Tool call rejected by user.";
|
|
4548
|
+
} finally {
|
|
4549
|
+
this.approvalResolvers.get(tc.id)?.resolve(false);
|
|
3812
4550
|
}
|
|
3813
4551
|
}
|
|
4552
|
+
throwIfAborted(signal);
|
|
4553
|
+
if (reason) {
|
|
4554
|
+
const result = { content: [{ type: "text", text: reason }], isError: true };
|
|
4555
|
+
item.resolved = true;
|
|
4556
|
+
item.result = result;
|
|
4557
|
+
this.addToolResult(sessionId, tc, result);
|
|
4558
|
+
this.emit("tool:result", { sessionId, toolName: tc.name, isError: true, summary: reason, duration: 0 });
|
|
4559
|
+
yield { type: "tool_result", callId: tc.id, toolName: tc.name, isError: true, summary: reason, duration: 0 };
|
|
4560
|
+
}
|
|
3814
4561
|
}
|
|
3815
4562
|
}
|
|
3816
4563
|
async executeToolBatch(items, sessionId, controller, canParallelize, emit) {
|
|
3817
4564
|
const traceEntries = [];
|
|
3818
|
-
for (const item of items) {
|
|
3819
|
-
emit("tool:call", { sessionId, toolName: item.toolCall.name, arguments: item.toolCall.arguments, location: item.location });
|
|
3820
|
-
}
|
|
3821
4565
|
for (const item of items) {
|
|
3822
4566
|
if (item.resolved) {
|
|
3823
4567
|
const summary = item.result?.content.filter((c) => c.type === "text").map((c) => c.text.slice(0, 100)).join(" ") ?? "";
|
|
@@ -3873,18 +4617,41 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3873
4617
|
traceEntries.push({ callId: item.toolCall.id, toolName: item.toolCall.name, location: item.location, arguments: item.toolCall.arguments, result: { isError: result.isError ?? false, summary }, duration });
|
|
3874
4618
|
}
|
|
3875
4619
|
}
|
|
4620
|
+
throwIfAborted(controller.signal);
|
|
3876
4621
|
return traceEntries;
|
|
3877
4622
|
}
|
|
3878
|
-
async
|
|
3879
|
-
const
|
|
3880
|
-
|
|
3881
|
-
|
|
3882
|
-
|
|
3883
|
-
|
|
3884
|
-
|
|
3885
|
-
|
|
4623
|
+
async *streamToolBatch(items, sessionId, controller, parallel) {
|
|
4624
|
+
const events = [];
|
|
4625
|
+
let wake;
|
|
4626
|
+
let done = false;
|
|
4627
|
+
let failure;
|
|
4628
|
+
const task = this.executeToolBatchStream(items, sessionId, controller, parallel, (event) => {
|
|
4629
|
+
events.push(event);
|
|
4630
|
+
wake?.();
|
|
4631
|
+
}).then(() => {
|
|
4632
|
+
done = true;
|
|
4633
|
+
wake?.();
|
|
4634
|
+
}, (error) => {
|
|
4635
|
+
failure = error;
|
|
4636
|
+
done = true;
|
|
4637
|
+
wake?.();
|
|
4638
|
+
});
|
|
4639
|
+
try {
|
|
4640
|
+
while (!done || events.length) {
|
|
4641
|
+
if (events.length) yield events.shift();
|
|
4642
|
+
else await new Promise((resolve) => {
|
|
4643
|
+
wake = resolve;
|
|
4644
|
+
});
|
|
3886
4645
|
}
|
|
4646
|
+
if (failure) throw failure;
|
|
4647
|
+
throwIfAborted(controller.signal);
|
|
4648
|
+
} finally {
|
|
4649
|
+
if (!done) controller.abort();
|
|
4650
|
+
await task;
|
|
3887
4651
|
}
|
|
4652
|
+
}
|
|
4653
|
+
async executeToolBatchStream(items, sessionId, controller, canParallelize, yieldEvent) {
|
|
4654
|
+
const traceEntries = [];
|
|
3888
4655
|
const executableItems = items.filter((item) => !item.resolved);
|
|
3889
4656
|
if (executableItems.length === 0) return traceEntries;
|
|
3890
4657
|
if (executableItems.length === 1) {
|
|
@@ -3934,6 +4701,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3934
4701
|
traceEntries.push({ callId: item.toolCall.id, toolName: item.toolCall.name, location: item.location, arguments: item.toolCall.arguments, result: { isError: result.isError ?? false, summary }, duration });
|
|
3935
4702
|
}
|
|
3936
4703
|
}
|
|
4704
|
+
throwIfAborted(controller.signal);
|
|
3937
4705
|
return traceEntries;
|
|
3938
4706
|
}
|
|
3939
4707
|
/**
|
|
@@ -3941,28 +4709,34 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3941
4709
|
*/
|
|
3942
4710
|
async executeToolCall(toolCall, sessionId, controller) {
|
|
3943
4711
|
const execStart = Date.now();
|
|
4712
|
+
throwIfAborted(controller.signal);
|
|
4713
|
+
if (toolCall.parseError) return { content: [{ type: "text", text: toolCall.parseError }], isError: true };
|
|
3944
4714
|
if (this.mcpRouter.isMcpTool(toolCall.name)) {
|
|
3945
4715
|
try {
|
|
3946
|
-
const result = await this.mcpRouter.call(toolCall.name, toolCall.arguments);
|
|
3947
|
-
|
|
4716
|
+
const result = await this.mcpRouter.call(toolCall.name, toolCall.arguments, { signal: controller.signal });
|
|
4717
|
+
throwIfAborted(controller.signal);
|
|
4718
|
+
return { content: [{ type: "text", text: typeof result === "string" ? result : JSON.stringify(result) ?? "" }] };
|
|
3948
4719
|
} catch (err) {
|
|
3949
|
-
|
|
4720
|
+
throwIfAborted(controller.signal);
|
|
4721
|
+
return { content: [{ type: "text", text: `MCP error: ${errorMessage(err)}` }], isError: true };
|
|
3950
4722
|
}
|
|
3951
4723
|
}
|
|
3952
|
-
const
|
|
4724
|
+
const scope = this.runContexts.get(controller.signal);
|
|
4725
|
+
const skillDef = scope?.registry.get(toolCall.name) ?? this.skillRegistry.get(toolCall.name);
|
|
3953
4726
|
const location = skillDef?.location === "server" ? "server" : "local";
|
|
3954
4727
|
try {
|
|
3955
4728
|
if (location === "server" && this.transport instanceof HttpTransport) {
|
|
3956
|
-
return await this.executeServerSkill(toolCall);
|
|
4729
|
+
return await this.executeServerSkill(toolCall, controller.signal);
|
|
3957
4730
|
} else {
|
|
3958
|
-
const
|
|
4731
|
+
const executorFor = (name) => scope?.registry.has(name) ? scope.executor : this.skillExecutor;
|
|
4732
|
+
const invokeFn = (skillName, params) => executorFor(skillName).invokeDirect(skillName, params, {
|
|
3959
4733
|
sessionId,
|
|
3960
4734
|
agent: this,
|
|
3961
4735
|
signal: controller.signal,
|
|
3962
4736
|
depth: 0,
|
|
3963
4737
|
invoke: invokeFn
|
|
3964
4738
|
});
|
|
3965
|
-
return await
|
|
4739
|
+
return await executorFor(toolCall.name).execute(toolCall.name, toolCall.arguments, {
|
|
3966
4740
|
sessionId,
|
|
3967
4741
|
agent: this,
|
|
3968
4742
|
signal: controller.signal,
|
|
@@ -3971,14 +4745,143 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3971
4745
|
});
|
|
3972
4746
|
}
|
|
3973
4747
|
} catch (err) {
|
|
3974
|
-
|
|
4748
|
+
if (controller.signal.aborted || err?.name === "AbortError") throw abortError();
|
|
4749
|
+
return { content: [{ type: "text", text: `Error: ${errorMessage(err)}` }], isError: true };
|
|
4750
|
+
}
|
|
4751
|
+
}
|
|
4752
|
+
prepareRunContext(signal, context) {
|
|
4753
|
+
if (!context) return this.runContexts.get(signal);
|
|
4754
|
+
const registry = new SkillRegistry();
|
|
4755
|
+
for (const skill of context.skills ?? []) {
|
|
4756
|
+
if (!skill.handler || skill.location === "server" || this.mcpRouter.isMcpTool(skill.name) || this.skillRegistry.has(skill.name) || registry.has(skill.name)) {
|
|
4757
|
+
throw new ACPError(ACPErrorCodes.SKILL_REGISTRATION_FAILED, `Invalid or conflicting run-local skill: ${skill.name}`);
|
|
4758
|
+
}
|
|
4759
|
+
registry.register({ ...skill });
|
|
4760
|
+
}
|
|
4761
|
+
const scope = {
|
|
4762
|
+
systemPromptSuffix: context.systemPromptSuffix,
|
|
4763
|
+
registry,
|
|
4764
|
+
executor: new SkillExecutor(registry, void 0, { validateInputs: this.config.skills?.validateInputs })
|
|
4765
|
+
};
|
|
4766
|
+
this.runContexts.set(signal, scope);
|
|
4767
|
+
return scope;
|
|
4768
|
+
}
|
|
4769
|
+
buildRunSystemPrompt(session, runContext) {
|
|
4770
|
+
const base = this.resolveSystemPrompt(session.id, session.systemPrompt ?? this.config.systemPrompt ?? "");
|
|
4771
|
+
return runContext?.systemPromptSuffix ? [base, runContext.systemPromptSuffix].filter(Boolean).join("\n\n") : base;
|
|
4772
|
+
}
|
|
4773
|
+
listRunTools(runContext) {
|
|
4774
|
+
const tools = this.skillRegistry.listToolDefs();
|
|
4775
|
+
if (!runContext) return tools;
|
|
4776
|
+
return [...tools.filter((tool) => !runContext.registry.has(tool.name)), ...runContext.registry.listToolDefs()];
|
|
4777
|
+
}
|
|
4778
|
+
beginExecution(sessionId, options) {
|
|
4779
|
+
const previous = this.abortControllers.get(sessionId);
|
|
4780
|
+
const controller = new AbortController();
|
|
4781
|
+
const unlinkRoot = linkAbort(previous?.signal, controller);
|
|
4782
|
+
const unlinkParent = linkAbort(options?.signal, controller);
|
|
4783
|
+
const inherited = previous && this.runContexts.get(previous.signal);
|
|
4784
|
+
if (inherited) this.runContexts.set(controller.signal, inherited);
|
|
4785
|
+
if (!previous) this.abortControllers.set(sessionId, controller);
|
|
4786
|
+
return { controller, cleanup: () => {
|
|
4787
|
+
controller.abort();
|
|
4788
|
+
this.runContexts.delete(controller.signal);
|
|
4789
|
+
unlinkRoot();
|
|
4790
|
+
unlinkParent();
|
|
4791
|
+
if (this.abortControllers.get(sessionId) === controller) this.abortControllers.delete(sessionId);
|
|
4792
|
+
} };
|
|
4793
|
+
}
|
|
4794
|
+
ensureSessionIsNotBeingManaged(sessionId) {
|
|
4795
|
+
if (this.managingSessions.has(sessionId)) {
|
|
4796
|
+
throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${sessionId} has a pending storage operation`);
|
|
4797
|
+
}
|
|
4798
|
+
}
|
|
4799
|
+
async manageSessionStorage(sessionIds, operation, options) {
|
|
4800
|
+
for (const id of sessionIds) {
|
|
4801
|
+
this.ensureSessionIsNotBeingManaged(id);
|
|
4802
|
+
if (this.compressingSessions.has(id)) {
|
|
4803
|
+
throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${id} is compressing its context`);
|
|
4804
|
+
}
|
|
4805
|
+
if (!options?.allowRunning && (this.runningSessions.has(id) || this.abortControllers.has(id))) {
|
|
4806
|
+
throw new ACPError(ACPErrorCodes.SESSION_BUSY, `Session ${id} is already running a chat`);
|
|
4807
|
+
}
|
|
4808
|
+
}
|
|
4809
|
+
const state = { deleted: false };
|
|
4810
|
+
for (const id of sessionIds) this.managingSessions.set(id, state);
|
|
4811
|
+
try {
|
|
4812
|
+
return await operation(state);
|
|
4813
|
+
} finally {
|
|
4814
|
+
for (const id of sessionIds) this.managingSessions.delete(id);
|
|
3975
4815
|
}
|
|
3976
4816
|
}
|
|
4817
|
+
async readSessionForStorage(sessionId) {
|
|
4818
|
+
const current = this.sessionManager.get(sessionId);
|
|
4819
|
+
if (current) return current;
|
|
4820
|
+
const stored = await this.persistence.loadSession(sessionId);
|
|
4821
|
+
return stored ? this.snapshotSession(stored) : null;
|
|
4822
|
+
}
|
|
4823
|
+
async requireSessionForStorage(sessionId) {
|
|
4824
|
+
const session = await this.readSessionForStorage(sessionId);
|
|
4825
|
+
if (session) return session;
|
|
4826
|
+
throw new ACPError(ACPErrorCodes.SESSION_NOT_FOUND, `Session not found: ${sessionId}`, { sessionId });
|
|
4827
|
+
}
|
|
4828
|
+
async commitStoredSession(original, updated) {
|
|
4829
|
+
await this.persistence.saveSession(this.snapshotSession(updated));
|
|
4830
|
+
if (this.sessionManager.get(original.id) === original) Object.assign(original, updated);
|
|
4831
|
+
}
|
|
4832
|
+
snapshotSession(session) {
|
|
4833
|
+
try {
|
|
4834
|
+
return structuredClone(session);
|
|
4835
|
+
} catch {
|
|
4836
|
+
return { ...session, messages: session.messages.map((message) => ({ ...message })) };
|
|
4837
|
+
}
|
|
4838
|
+
}
|
|
4839
|
+
queueSessionStorage(sessionId, operation) {
|
|
4840
|
+
const previous = this.sessionStorageQueues.get(sessionId) ?? Promise.resolve();
|
|
4841
|
+
const result = previous.then(operation);
|
|
4842
|
+
const settled = result.then(() => {
|
|
4843
|
+
}, () => {
|
|
4844
|
+
});
|
|
4845
|
+
this.sessionStorageQueues.set(sessionId, settled);
|
|
4846
|
+
void settled.then(() => {
|
|
4847
|
+
if (this.sessionStorageQueues.get(sessionId) === settled) this.sessionStorageQueues.delete(sessionId);
|
|
4848
|
+
});
|
|
4849
|
+
return result;
|
|
4850
|
+
}
|
|
4851
|
+
saveSessionSafely(session) {
|
|
4852
|
+
const snapshot = this.snapshotSession(session);
|
|
4853
|
+
const management = this.managingSessions.get(session.id);
|
|
4854
|
+
return this.queueSessionStorage(session.id, async () => {
|
|
4855
|
+
if (management?.deleted) return;
|
|
4856
|
+
try {
|
|
4857
|
+
await this.persistence.saveSession(snapshot);
|
|
4858
|
+
} catch (error) {
|
|
4859
|
+
try {
|
|
4860
|
+
this.config.onDiagnostic?.({ kind: "persistence", sessionId: session.id, message: errorMessage(error) });
|
|
4861
|
+
} catch {
|
|
4862
|
+
}
|
|
4863
|
+
this.emit("persistence:error", { sessionId: session.id, message: errorMessage(error) });
|
|
4864
|
+
}
|
|
4865
|
+
});
|
|
4866
|
+
}
|
|
3977
4867
|
ensureInitialized() {
|
|
3978
4868
|
if (!this.initialized) {
|
|
3979
4869
|
throw new ACPError(ACPErrorCodes.AGENT_NOT_INITIALIZED, "Agent not initialized. Call init() first.");
|
|
3980
4870
|
}
|
|
3981
4871
|
}
|
|
4872
|
+
reportPromptCacheUsage(sessionId, promptCacheUsage) {
|
|
4873
|
+
if (!this.config.llm.collectPromptCacheUsage || !promptCacheUsage) return;
|
|
4874
|
+
try {
|
|
4875
|
+
this.config.onDiagnostic?.({
|
|
4876
|
+
kind: "llm:prompt-cache",
|
|
4877
|
+
message: "Model reported prompt cache usage",
|
|
4878
|
+
sessionId,
|
|
4879
|
+
model: this.config.llm.model,
|
|
4880
|
+
promptCacheUsage
|
|
4881
|
+
});
|
|
4882
|
+
} catch {
|
|
4883
|
+
}
|
|
4884
|
+
}
|
|
3982
4885
|
ensureSession() {
|
|
3983
4886
|
if (this.currentSessionId) {
|
|
3984
4887
|
const existing = this.sessionManager.get(this.currentSessionId);
|
|
@@ -4011,9 +4914,10 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
4011
4914
|
this.sessionManager.addMessage(sessionId, { role: "tool", content: result.content, toolCallId: toolCall.id });
|
|
4012
4915
|
}
|
|
4013
4916
|
}
|
|
4014
|
-
async executeServerSkill(toolCall) {
|
|
4917
|
+
async executeServerSkill(toolCall, signal) {
|
|
4015
4918
|
const transport = this.transport;
|
|
4016
|
-
const result = await transport.send("tools/call", { name: toolCall.name, arguments: toolCall.arguments });
|
|
4919
|
+
const result = await transport.send("tools/call", { name: toolCall.name, arguments: toolCall.arguments }, { signal });
|
|
4920
|
+
if (!result || !Array.isArray(result.content)) throw new Error("Server skill returned an invalid result");
|
|
4017
4921
|
return result;
|
|
4018
4922
|
}
|
|
4019
4923
|
};
|