@listdoing/core 0.1.5 → 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 +160 -27
- package/dist/index.d.ts +160 -27
- package/dist/index.js +1232 -517
- package/dist/index.mjs +1222 -517
- 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();
|
|
504
|
+
}
|
|
505
|
+
const toolCalls = [];
|
|
506
|
+
for (const [_, tc] of toolCallMap) {
|
|
507
|
+
toolCalls.push(this.parseToolCall(tc));
|
|
400
508
|
}
|
|
509
|
+
throwIfAborted(request.controller.signal);
|
|
510
|
+
if (this.config.strictStream && !sawDone && !finishReason) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "LLM stream ended without a finish marker");
|
|
511
|
+
if (finishReason === "length") for (const call of toolCalls) call.parseError = "Tool response exceeded the output token limit";
|
|
512
|
+
if (content || toolCalls.length) this.verified = true;
|
|
513
|
+
yield {
|
|
514
|
+
type: "llm_response",
|
|
515
|
+
response: {
|
|
516
|
+
content,
|
|
517
|
+
reasoning: reasoning || void 0,
|
|
518
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
519
|
+
usage: void 0,
|
|
520
|
+
...promptCacheUsage && { promptCacheUsage },
|
|
521
|
+
finishReason: finishReason ?? (toolCalls.length > 0 ? "tool_calls" : "stop")
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
} catch (error) {
|
|
525
|
+
if (request.controller.signal.aborted && !options?.signal?.aborted) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "LLM request timed out", { reason: "timeout" });
|
|
526
|
+
throw error;
|
|
401
527
|
} finally {
|
|
402
|
-
|
|
528
|
+
request.cleanup();
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
parseToolCall(tc) {
|
|
532
|
+
if (!tc.id || !tc.name) throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, "Tool call is missing its id or name");
|
|
533
|
+
try {
|
|
534
|
+
const args = JSON.parse(tc.arguments || "{}");
|
|
535
|
+
if (!args || typeof args !== "object" || Array.isArray(args)) throw new Error("Tool arguments must be a JSON object");
|
|
536
|
+
return { id: tc.id, name: tc.name, arguments: args };
|
|
537
|
+
} catch (error) {
|
|
538
|
+
return { id: tc.id, name: tc.name, arguments: {}, parseError: errorMessage(error) };
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
createRequest(parent) {
|
|
542
|
+
const controller = new AbortController();
|
|
543
|
+
const unlink = linkAbort(parent, controller);
|
|
544
|
+
const timeout = this.config.requestTimeoutMs;
|
|
545
|
+
const timer = timeout && timeout > 0 ? setTimeout(() => controller.abort(), timeout) : void 0;
|
|
546
|
+
return { controller, cleanup: () => {
|
|
547
|
+
clearTimeout(timer);
|
|
548
|
+
unlink();
|
|
549
|
+
controller.abort();
|
|
550
|
+
} };
|
|
551
|
+
}
|
|
552
|
+
async fetchResponse(init) {
|
|
553
|
+
const retries = Math.max(0, Math.min(this.config.retry?.maxRetries ?? 0, 5));
|
|
554
|
+
const requestInit = this.options?.rejectRedirects ? { ...init, redirect: "error" } : init;
|
|
555
|
+
for (let attempt = 0; ; attempt++) {
|
|
556
|
+
throwIfAborted(init.signal ?? void 0);
|
|
557
|
+
const response = await fetch(this.getEndpoint(), requestInit);
|
|
558
|
+
if (attempt >= retries || ![429, 502, 503, 504].includes(response.status)) return response;
|
|
559
|
+
const retryAfter = Number(response.headers.get("retry-after"));
|
|
560
|
+
const delay = Number.isFinite(retryAfter) && retryAfter > 0 ? Math.min(retryAfter * 1e3, 3e4) : Math.min((this.config.retry?.baseDelayMs ?? 500) * 2 ** attempt, 3e4);
|
|
561
|
+
await response.body?.cancel();
|
|
562
|
+
await abortableDelay(delay, init.signal ?? void 0);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
async readChunk(reader, controller) {
|
|
566
|
+
const idleMs = this.config.streamIdleTimeoutMs;
|
|
567
|
+
if (!idleMs || idleMs <= 0) return reader.read();
|
|
568
|
+
let timer;
|
|
569
|
+
try {
|
|
570
|
+
return await Promise.race([reader.read(), new Promise((_, reject) => {
|
|
571
|
+
timer = setTimeout(() => {
|
|
572
|
+
reject(new Error("LLM stream idle timeout"));
|
|
573
|
+
controller.abort();
|
|
574
|
+
}, idleMs);
|
|
575
|
+
})]);
|
|
576
|
+
} finally {
|
|
577
|
+
clearTimeout(timer);
|
|
403
578
|
}
|
|
404
|
-
const toolCalls = [];
|
|
405
|
-
for (const [_, tc] of toolCallMap) {
|
|
406
|
-
let args = {};
|
|
407
|
-
try {
|
|
408
|
-
args = JSON.parse(tc.arguments || "{}");
|
|
409
|
-
} catch {
|
|
410
|
-
args = {};
|
|
411
|
-
}
|
|
412
|
-
toolCalls.push({ id: tc.id, name: tc.name, arguments: args });
|
|
413
|
-
}
|
|
414
|
-
yield {
|
|
415
|
-
type: "llm_response",
|
|
416
|
-
response: {
|
|
417
|
-
content,
|
|
418
|
-
reasoning: reasoning || void 0,
|
|
419
|
-
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
420
|
-
usage: void 0,
|
|
421
|
-
finishReason: toolCalls.length > 0 ? "tool_calls" : "stop"
|
|
422
|
-
}
|
|
423
|
-
};
|
|
424
579
|
}
|
|
425
580
|
/**
|
|
426
581
|
* Check if the error response indicates context length exceeded.
|
|
@@ -438,6 +593,14 @@ var OpenAIAdapter = class {
|
|
|
438
593
|
throw new ACPError(ACPErrorCodes.CONTEXT_OVERFLOW, errMsg, { isContextOverflow: true });
|
|
439
594
|
}
|
|
440
595
|
}
|
|
596
|
+
/** AbortError for the given signal — used so a cancelled stream surfaces an abort, not a parse error. */
|
|
597
|
+
throwIfAborted(signal) {
|
|
598
|
+
if (signal?.aborted) {
|
|
599
|
+
const err = new Error("Request aborted");
|
|
600
|
+
err.name = "AbortError";
|
|
601
|
+
throw err;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
441
604
|
parseResponse(data) {
|
|
442
605
|
const choice = data.choices?.[0];
|
|
443
606
|
if (!choice) {
|
|
@@ -449,17 +612,9 @@ var OpenAIAdapter = class {
|
|
|
449
612
|
const toolCalls = [];
|
|
450
613
|
if (message?.tool_calls?.length) {
|
|
451
614
|
for (const tc of message.tool_calls) {
|
|
452
|
-
|
|
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();
|
|
611
850
|
}
|
|
612
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 {
|
|
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;
|
|
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) {
|
|
@@ -1188,26 +1516,21 @@ var ContextManager = class {
|
|
|
1188
1516
|
if (systemPrompt) {
|
|
1189
1517
|
result.push({ role: "system", content: systemPrompt });
|
|
1190
1518
|
}
|
|
1191
|
-
const sanitized = messages.map((m) => {
|
|
1192
|
-
if (m.role === "assistant" && !m.content && !m.toolCalls?.length) {
|
|
1193
|
-
return { ...m, content: "." };
|
|
1194
|
-
}
|
|
1195
|
-
return m;
|
|
1196
|
-
});
|
|
1197
|
-
const reversed = [...sanitized].reverse();
|
|
1198
1519
|
const selected = [];
|
|
1199
1520
|
let usedByMessages = 0;
|
|
1200
|
-
for (const
|
|
1201
|
-
const
|
|
1202
|
-
if (usedByMessages +
|
|
1203
|
-
selected.unshift(
|
|
1204
|
-
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;
|
|
1205
1526
|
}
|
|
1206
1527
|
result.push(...selected);
|
|
1207
1528
|
if (selected.length === 0 && messages.length > 0) {
|
|
1208
1529
|
const lastUser = [...messages].reverse().find((m) => m.role === "user");
|
|
1209
1530
|
if (lastUser) {
|
|
1210
|
-
|
|
1531
|
+
const fallback = repairMessages([lastUser]);
|
|
1532
|
+
result.push(...fallback);
|
|
1533
|
+
usedByMessages += fallback.reduce((sum, message) => sum + estimateMessageTokens(message), 0);
|
|
1211
1534
|
}
|
|
1212
1535
|
}
|
|
1213
1536
|
return { messages: result, usedTokens: fixedTokens + usedByMessages, budget };
|
|
@@ -1218,37 +1541,64 @@ var ContextManager = class {
|
|
|
1218
1541
|
* summarization (when a summarizer is set and enabled) over mechanical truncation.
|
|
1219
1542
|
*/
|
|
1220
1543
|
async ensureContextFits(messages, systemPrompt, tools, model, signal) {
|
|
1544
|
+
throwIfAborted(signal);
|
|
1221
1545
|
if (model) this.setModel(model);
|
|
1222
|
-
const {
|
|
1546
|
+
const { budget } = this.buildWindow(messages, systemPrompt, tools);
|
|
1547
|
+
const beforeTokens = estimateRequestTokens(repairMessages(messages), tools, systemPrompt);
|
|
1548
|
+
if (this.config.autoCompress === false) return { messages, wasCompressed: false };
|
|
1223
1549
|
const threshold = this.config.compressThreshold ?? 0.85;
|
|
1224
|
-
if (
|
|
1550
|
+
if (beforeTokens / budget <= threshold) {
|
|
1225
1551
|
return { messages, wasCompressed: false };
|
|
1226
1552
|
}
|
|
1227
1553
|
const useLLM = this.config.useLLMSummary !== false && !!this.summarizer;
|
|
1228
1554
|
let result;
|
|
1229
1555
|
if (useLLM) {
|
|
1230
|
-
const llmResult = await this.compressWithLLM(messages, systemPrompt, tools, budget, signal);
|
|
1231
|
-
result = llmResult ?? this.compressSync(messages, "hybrid");
|
|
1556
|
+
const llmResult = await this.compressWithLLM(messages, systemPrompt, tools, budget, { signal, groupTools: true });
|
|
1557
|
+
result = llmResult ?? this.compressSync(messages, "hybrid", { groupTools: true });
|
|
1232
1558
|
} else {
|
|
1233
|
-
result = this.compressSync(messages, "hybrid");
|
|
1559
|
+
result = this.compressSync(messages, "hybrid", { groupTools: true });
|
|
1234
1560
|
}
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
};
|
|
1561
|
+
let compressed = result.messages;
|
|
1562
|
+
let window2 = this.buildWindow(compressed, systemPrompt, tools);
|
|
1563
|
+
if (window2.usedTokens > budget) {
|
|
1564
|
+
console.warn(`[listdoing] Context still exceeds budget after compression (${window2.usedTokens}/${budget} tokens). Truncating to recent messages only.`);
|
|
1565
|
+
compressed = messageGroups(messages).slice(-3).flat();
|
|
1566
|
+
window2 = this.buildWindow(compressed, systemPrompt, tools);
|
|
1567
|
+
}
|
|
1568
|
+
if (estimateRequestTokens(compressed, tools, systemPrompt) >= beforeTokens || window2.usedTokens >= beforeTokens) {
|
|
1569
|
+
return { messages, wasCompressed: false };
|
|
1245
1570
|
}
|
|
1246
1571
|
return {
|
|
1247
|
-
messages:
|
|
1572
|
+
messages: compressed,
|
|
1248
1573
|
wasCompressed: true,
|
|
1249
|
-
beforeTokens
|
|
1250
|
-
afterTokens:
|
|
1574
|
+
beforeTokens,
|
|
1575
|
+
afterTokens: window2.usedTokens
|
|
1576
|
+
};
|
|
1577
|
+
}
|
|
1578
|
+
/** Build one smaller request after a provider rejects the current window. */
|
|
1579
|
+
async compressForOverflow(messages, rejected, systemPrompt, tools, signal) {
|
|
1580
|
+
throwIfAborted(signal);
|
|
1581
|
+
const fixedTokens = estimateRequestTokens([], tools, systemPrompt);
|
|
1582
|
+
const available = Math.min(rejected.budget, rejected.usedTokens) - fixedTokens;
|
|
1583
|
+
const historyBudget = Math.floor(available * 0.85);
|
|
1584
|
+
if (historyBudget <= 0) return;
|
|
1585
|
+
const budget = fixedTokens + historyBudget;
|
|
1586
|
+
const latestUser = [...messages].reverse().find((message) => message.role === "user");
|
|
1587
|
+
if (latestUser && estimateMessageTokens(latestUser) > historyBudget) return;
|
|
1588
|
+
const rejectedUserIndex = latestUser ? rejected.messages.lastIndexOf(latestUser) : -1;
|
|
1589
|
+
const activeToolResults = rejected.messages.slice(rejectedUserIndex + 1).filter((message) => message.role === "tool");
|
|
1590
|
+
const summarized = this.config.useLLMSummary !== false ? await this.compressWithLLM(messages, systemPrompt, tools, budget, { signal, groupTools: true }) : null;
|
|
1591
|
+
throwIfAborted(signal);
|
|
1592
|
+
const useCandidate = (result) => {
|
|
1593
|
+
const window2 = this.buildWindow(result.messages, systemPrompt, tools, budget);
|
|
1594
|
+
if (window2.usedTokens > budget || window2.usedTokens >= rejected.usedTokens) return;
|
|
1595
|
+
if (latestUser && !window2.messages.some((message) => message.role === "user" && message.content === latestUser.content)) return;
|
|
1596
|
+
const keptToolIds = new Set(window2.messages.filter((message) => message.role === "tool").map((message) => message.toolCallId));
|
|
1597
|
+
if (activeToolResults.some((message) => !keptToolIds.has(message.toolCallId))) return;
|
|
1598
|
+
return { ...result, window: window2 };
|
|
1251
1599
|
};
|
|
1600
|
+
const candidate = summarized && useCandidate(summarized);
|
|
1601
|
+
return candidate || useCandidate(this.compressSync(messages, "hybrid", { maxTokens: historyBudget, groupTools: true }));
|
|
1252
1602
|
}
|
|
1253
1603
|
/**
|
|
1254
1604
|
* Compress context by summarizing older messages. When strategy is
|
|
@@ -1267,7 +1617,8 @@ var ContextManager = class {
|
|
|
1267
1617
|
return this.compressSync(messages, strat);
|
|
1268
1618
|
}
|
|
1269
1619
|
/** Mechanical (non-LLM) compression — the original truncation strategies. */
|
|
1270
|
-
compressSync(messages, strategy) {
|
|
1620
|
+
compressSync(messages, strategy, options) {
|
|
1621
|
+
messages = repairMessages(messages);
|
|
1271
1622
|
const estimateTokens = (msgs) => msgs.reduce((sum, m) => sum + estimateMessageTokens(m), 0);
|
|
1272
1623
|
const beforeTokens = estimateTokens(messages);
|
|
1273
1624
|
let compressed;
|
|
@@ -1294,13 +1645,14 @@ ${summaryText}` },
|
|
|
1294
1645
|
case "token_budget":
|
|
1295
1646
|
case "hybrid":
|
|
1296
1647
|
default: {
|
|
1297
|
-
const maxTokens = this.config.maxTokens ?? this.getBudget();
|
|
1648
|
+
const maxTokens = options?.maxTokens ?? this.config.maxTokens ?? this.getBudget();
|
|
1298
1649
|
let totalTokens = 0;
|
|
1299
1650
|
const kept = [];
|
|
1300
|
-
|
|
1301
|
-
|
|
1651
|
+
const groups = options?.groupTools ? messageGroups(messages) : messages.map((message) => [message]);
|
|
1652
|
+
for (const group of groups.reverse()) {
|
|
1653
|
+
const t = estimateTokens(group);
|
|
1302
1654
|
if (totalTokens + t > maxTokens) break;
|
|
1303
|
-
kept.unshift(
|
|
1655
|
+
kept.unshift(...group);
|
|
1304
1656
|
totalTokens += t;
|
|
1305
1657
|
}
|
|
1306
1658
|
if (kept.length < messages.length) {
|
|
@@ -1317,16 +1669,18 @@ ${summaryText}` },
|
|
|
1317
1669
|
break;
|
|
1318
1670
|
}
|
|
1319
1671
|
}
|
|
1320
|
-
const
|
|
1321
|
-
return { messages:
|
|
1672
|
+
const repaired = repairMessages(compressed);
|
|
1673
|
+
return { messages: repaired, beforeTokens, afterTokens: estimateTokens(repaired) };
|
|
1322
1674
|
}
|
|
1323
1675
|
/**
|
|
1324
1676
|
* LLM-based semantic summarization. Returns null when it cannot be performed
|
|
1325
1677
|
* (no summarizer, nothing to summarize, or the LLM call fails) so callers can
|
|
1326
1678
|
* fall back to mechanical compression.
|
|
1327
1679
|
*/
|
|
1328
|
-
async compressWithLLM(messages, systemPrompt, tools, budget,
|
|
1680
|
+
async compressWithLLM(messages, systemPrompt, tools, budget, options) {
|
|
1329
1681
|
if (!this.summarizer) return null;
|
|
1682
|
+
const { signal, groupTools } = options ?? {};
|
|
1683
|
+
messages = repairMessages(messages);
|
|
1330
1684
|
try {
|
|
1331
1685
|
const estimateTokens = (msgs) => msgs.reduce((sum, m) => sum + estimateMessageTokens(m), 0);
|
|
1332
1686
|
const beforeTokens = estimateTokens(messages);
|
|
@@ -1336,33 +1690,31 @@ ${summaryText}` },
|
|
|
1336
1690
|
const summaryReserve = 2e3;
|
|
1337
1691
|
const outputReserve = this.config.outputReserve ?? 1024;
|
|
1338
1692
|
const remaining = budget - fixedTokens - summaryReserve - outputReserve;
|
|
1693
|
+
const groups = groupTools ? messageGroups(messages) : messages.map((message) => [message]);
|
|
1339
1694
|
const kept = [];
|
|
1340
1695
|
let used = 0;
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
used += t;
|
|
1347
|
-
}
|
|
1696
|
+
for (let i = groups.length - 1; i >= 0; i--) {
|
|
1697
|
+
const t = estimateTokens(groups[i]);
|
|
1698
|
+
if (used + t > remaining) break;
|
|
1699
|
+
kept.unshift(groups[i]);
|
|
1700
|
+
used += t;
|
|
1348
1701
|
}
|
|
1349
|
-
const oldMessages =
|
|
1702
|
+
const oldMessages = groups.slice(0, groups.length - kept.length).flat();
|
|
1350
1703
|
if (oldMessages.length === 0) {
|
|
1351
1704
|
return null;
|
|
1352
1705
|
}
|
|
1353
1706
|
const summary = await this.summarizer(oldMessages, signal ? { signal } : void 0);
|
|
1354
1707
|
const summaryMessage = { role: "user", content: `[\u4E0A\u4E0B\u6587\u6458\u8981]
|
|
1355
1708
|
${summary}` };
|
|
1356
|
-
|
|
1357
|
-
let afterTokens =
|
|
1358
|
-
while (
|
|
1359
|
-
|
|
1360
|
-
if (trimmed.length === result.length) break;
|
|
1361
|
-
result = trimmed;
|
|
1362
|
-
afterTokens = estimateTokens(result);
|
|
1709
|
+
const limit = groupTools ? budget - fixedTokens : budget;
|
|
1710
|
+
let afterTokens = estimateMessageTokens(summaryMessage) + used;
|
|
1711
|
+
while (kept.length > 1 && afterTokens > limit) {
|
|
1712
|
+
afterTokens -= estimateTokens(kept.shift());
|
|
1363
1713
|
}
|
|
1364
|
-
|
|
1714
|
+
const repaired = repairMessages([summaryMessage, ...kept.flat()]);
|
|
1715
|
+
return { messages: repaired, beforeTokens, afterTokens: estimateTokens(repaired) };
|
|
1365
1716
|
} catch (err) {
|
|
1717
|
+
throwIfAborted(signal);
|
|
1366
1718
|
console.warn("[listdoing] LLM summarization failed, falling back to mechanical compression:", err);
|
|
1367
1719
|
return null;
|
|
1368
1720
|
}
|
|
@@ -1449,6 +1801,7 @@ function createLLMSummarizer(adapter, options) {
|
|
|
1449
1801
|
return async (messages, opts) => {
|
|
1450
1802
|
const formatted = formatMessagesForSummary(messages);
|
|
1451
1803
|
const controller = new AbortController();
|
|
1804
|
+
const unlink = linkAbort(opts?.signal, controller);
|
|
1452
1805
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
1453
1806
|
try {
|
|
1454
1807
|
const response = await adapter.chat(
|
|
@@ -1457,7 +1810,7 @@ function createLLMSummarizer(adapter, options) {
|
|
|
1457
1810
|
{ role: "user", content: formatted }
|
|
1458
1811
|
],
|
|
1459
1812
|
[],
|
|
1460
|
-
{ signal:
|
|
1813
|
+
{ signal: controller.signal }
|
|
1461
1814
|
);
|
|
1462
1815
|
const summary = (response.content ?? "").trim();
|
|
1463
1816
|
if (!summary) {
|
|
@@ -1466,6 +1819,7 @@ function createLLMSummarizer(adapter, options) {
|
|
|
1466
1819
|
return summary;
|
|
1467
1820
|
} finally {
|
|
1468
1821
|
clearTimeout(timer);
|
|
1822
|
+
unlink();
|
|
1469
1823
|
}
|
|
1470
1824
|
};
|
|
1471
1825
|
}
|
|
@@ -1475,12 +1829,13 @@ var TaskPlanner = class {
|
|
|
1475
1829
|
constructor(llmAdapter) {
|
|
1476
1830
|
this.llmAdapter = llmAdapter;
|
|
1477
1831
|
}
|
|
1478
|
-
async plan(goal, availableTools) {
|
|
1832
|
+
async plan(goal, availableTools, signal) {
|
|
1479
1833
|
const prompt = this.buildPlanningPrompt(goal, availableTools);
|
|
1480
1834
|
const response = await this.llmAdapter.chat(
|
|
1481
1835
|
[{ role: "user", content: prompt }],
|
|
1482
|
-
[]
|
|
1836
|
+
[],
|
|
1483
1837
|
// No tools for the planning call itself
|
|
1838
|
+
{ signal }
|
|
1484
1839
|
);
|
|
1485
1840
|
return this.parsePlan(goal, response.content);
|
|
1486
1841
|
}
|
|
@@ -1799,17 +2154,19 @@ var GraphPlanner = class {
|
|
|
1799
2154
|
constructor(llmAdapter) {
|
|
1800
2155
|
this.llmAdapter = llmAdapter;
|
|
1801
2156
|
}
|
|
1802
|
-
async planGraph(goal, availableTools) {
|
|
2157
|
+
async planGraph(goal, availableTools, signal) {
|
|
1803
2158
|
const prompt = this.buildGraphPlanningPrompt(goal, availableTools);
|
|
1804
2159
|
let response;
|
|
1805
2160
|
try {
|
|
1806
2161
|
const result = await this.llmAdapter.chat(
|
|
1807
2162
|
[{ role: "user", content: prompt }],
|
|
1808
|
-
[]
|
|
2163
|
+
[],
|
|
1809
2164
|
// No tools for the planning call
|
|
2165
|
+
{ signal }
|
|
1810
2166
|
);
|
|
1811
2167
|
response = result.content;
|
|
1812
2168
|
} catch {
|
|
2169
|
+
throwIfAborted(signal);
|
|
1813
2170
|
return this.fallbackSequentialGraph(goal);
|
|
1814
2171
|
}
|
|
1815
2172
|
return this.parseGraphPlan(goal, response);
|
|
@@ -1932,9 +2289,7 @@ var SubAgent = class {
|
|
|
1932
2289
|
this.config = options.config;
|
|
1933
2290
|
this.depth = options.depth;
|
|
1934
2291
|
this.abortController = new AbortController();
|
|
1935
|
-
|
|
1936
|
-
options.parentSignal.addEventListener("abort", () => this.abortController.abort(), { once: true });
|
|
1937
|
-
}
|
|
2292
|
+
this.unlinkParent = linkAbort(options.parentSignal, this.abortController);
|
|
1938
2293
|
const maxDepth = this.config.maxDepth ?? 2;
|
|
1939
2294
|
if (this.depth >= maxDepth) {
|
|
1940
2295
|
this.config.canSpawnSubAgents = false;
|
|
@@ -1945,7 +2300,9 @@ var SubAgent = class {
|
|
|
1945
2300
|
this.status = "running";
|
|
1946
2301
|
const startTime = Date.now();
|
|
1947
2302
|
try {
|
|
2303
|
+
throwIfAborted(this.abortController.signal);
|
|
1948
2304
|
const session = this.parentAgent.createSession({
|
|
2305
|
+
activate: false,
|
|
1949
2306
|
title: `SubAgent: ${this.nodeId}`,
|
|
1950
2307
|
meta: {
|
|
1951
2308
|
parentSessionId: this.parentAgent.getCurrentSessionId(),
|
|
@@ -1959,6 +2316,7 @@ var SubAgent = class {
|
|
|
1959
2316
|
const prompt = this.buildPrompt(goal, upstreamOutputs);
|
|
1960
2317
|
const options = {
|
|
1961
2318
|
sessionId: session.id,
|
|
2319
|
+
signal: this.abortController.signal,
|
|
1962
2320
|
maxTurns: this.config.maxTurns ?? 10,
|
|
1963
2321
|
depth: this.depth + 1,
|
|
1964
2322
|
taskMode: void 0
|
|
@@ -1982,6 +2340,7 @@ var SubAgent = class {
|
|
|
1982
2340
|
sessionId: this.sessionId ?? void 0
|
|
1983
2341
|
};
|
|
1984
2342
|
}
|
|
2343
|
+
throwIfAborted(this.abortController.signal);
|
|
1985
2344
|
this.status = "done";
|
|
1986
2345
|
return {
|
|
1987
2346
|
nodeId: this.nodeId,
|
|
@@ -2003,6 +2362,8 @@ var SubAgent = class {
|
|
|
2003
2362
|
retryCount: 0,
|
|
2004
2363
|
sessionId: this.sessionId ?? void 0
|
|
2005
2364
|
};
|
|
2365
|
+
} finally {
|
|
2366
|
+
this.unlinkParent();
|
|
2006
2367
|
}
|
|
2007
2368
|
}
|
|
2008
2369
|
/** Abort the sub-agent */
|
|
@@ -2047,42 +2408,48 @@ var GraphExecutor = class {
|
|
|
2047
2408
|
this.maxDepth = options.maxDepth ?? 2;
|
|
2048
2409
|
this.baseDepth = options.baseDepth ?? 0;
|
|
2049
2410
|
this.abortController = new AbortController();
|
|
2411
|
+
this.unlinkParent = linkAbort(options.parentSignal, this.abortController);
|
|
2050
2412
|
this.emit = options.emit;
|
|
2051
2413
|
}
|
|
2052
2414
|
/** Execute the entire graph, level by level */
|
|
2053
2415
|
async execute() {
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
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 {
|
|
2057
2438
|
this.plan.status = "failed";
|
|
2058
|
-
|
|
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 });
|
|
2059
2441
|
}
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
const levelResults = nodeIds.map((id) => this.plan.results[id]).filter(Boolean);
|
|
2065
|
-
this.emit("graph:level_complete", { level, results: levelResults });
|
|
2066
|
-
}
|
|
2067
|
-
const progress = this.getProgress();
|
|
2068
|
-
if (progress.failed === 0 && progress.done === progress.total) {
|
|
2069
|
-
this.plan.status = "completed";
|
|
2070
|
-
this.emit("graph:complete", { plan: this.plan, progress });
|
|
2071
|
-
} else if (progress.done > 0) {
|
|
2072
|
-
this.plan.status = "partial";
|
|
2073
|
-
this.emit("graph:complete", { plan: this.plan, progress });
|
|
2074
|
-
} else {
|
|
2075
|
-
this.plan.status = "failed";
|
|
2076
|
-
const failedNodes = Object.entries(this.plan.results).filter(([, r]) => r.status === "failed").map(([id]) => id);
|
|
2077
|
-
this.emit("graph:failed", { plan: this.plan, failedNodes });
|
|
2442
|
+
this.plan.updatedAt = Date.now();
|
|
2443
|
+
return this.plan;
|
|
2444
|
+
} finally {
|
|
2445
|
+
this.unlinkParent();
|
|
2078
2446
|
}
|
|
2079
|
-
this.plan.updatedAt = Date.now();
|
|
2080
|
-
return this.plan;
|
|
2081
2447
|
}
|
|
2082
2448
|
/** Execute a single level — nodes run concurrently (limited by maxConcurrency) */
|
|
2083
2449
|
async executeLevel(level, nodes) {
|
|
2084
|
-
const runnableNodes = nodes.filter((node) => this.shouldRun(node));
|
|
2450
|
+
const runnableNodes = nodes.filter((node) => this.plan.results[node.id]?.status !== "done" && this.shouldRun(node));
|
|
2085
2451
|
for (const node of nodes) {
|
|
2452
|
+
if (this.plan.results[node.id]?.status === "done") continue;
|
|
2086
2453
|
if (!runnableNodes.includes(node)) {
|
|
2087
2454
|
this.plan.results[node.id] = {
|
|
2088
2455
|
nodeId: node.id,
|
|
@@ -2182,7 +2549,8 @@ var GraphExecutor = class {
|
|
|
2182
2549
|
willRetry: true
|
|
2183
2550
|
});
|
|
2184
2551
|
const delay = Math.min(backoffMs * Math.pow(2, attempt), maxBackoffMs);
|
|
2185
|
-
await
|
|
2552
|
+
await abortableDelay(delay, this.abortController.signal).catch(() => {
|
|
2553
|
+
});
|
|
2186
2554
|
}
|
|
2187
2555
|
}
|
|
2188
2556
|
if (node.fallback) {
|
|
@@ -2241,8 +2609,9 @@ var GraphExecutor = class {
|
|
|
2241
2609
|
depth: this.baseDepth
|
|
2242
2610
|
});
|
|
2243
2611
|
if (node.timeout) {
|
|
2612
|
+
let timer;
|
|
2244
2613
|
const timeoutPromise = new Promise((_, reject) => {
|
|
2245
|
-
setTimeout(() => reject(new ACPError(
|
|
2614
|
+
timer = setTimeout(() => reject(new ACPError(
|
|
2246
2615
|
ACPErrorCodes.SUBAGENT_TIMEOUT,
|
|
2247
2616
|
`Node "${node.id}" timed out after ${node.timeout}ms`
|
|
2248
2617
|
)), node.timeout);
|
|
@@ -2262,6 +2631,8 @@ var GraphExecutor = class {
|
|
|
2262
2631
|
duration: 0,
|
|
2263
2632
|
retryCount: 0
|
|
2264
2633
|
};
|
|
2634
|
+
} finally {
|
|
2635
|
+
clearTimeout(timer);
|
|
2265
2636
|
}
|
|
2266
2637
|
}
|
|
2267
2638
|
return subAgent.execute(node.goal, upstreamOutputs);
|
|
@@ -2462,7 +2833,7 @@ var HttpTransport = class {
|
|
|
2462
2833
|
}
|
|
2463
2834
|
return headers;
|
|
2464
2835
|
}
|
|
2465
|
-
async send(method, params) {
|
|
2836
|
+
async send(method, params, options) {
|
|
2466
2837
|
const id = ++this.requestId;
|
|
2467
2838
|
const body = {
|
|
2468
2839
|
jsonrpc: "2.0",
|
|
@@ -2474,21 +2845,31 @@ var HttpTransport = class {
|
|
|
2474
2845
|
const res = await fetch(`${this.baseUrl}/mcp`, {
|
|
2475
2846
|
method: "POST",
|
|
2476
2847
|
headers: this.buildHeaders({ Accept: "application/json" }),
|
|
2848
|
+
signal: options?.signal,
|
|
2477
2849
|
body: JSON.stringify(body)
|
|
2478
2850
|
});
|
|
2479
|
-
|
|
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
|
+
}
|
|
2480
2858
|
const sid = res.headers.get("Mcp-Session-Id");
|
|
2481
2859
|
if (sid) this.sessionId = sid;
|
|
2482
|
-
if (data
|
|
2860
|
+
if (data?.error) {
|
|
2483
2861
|
throw new ACPError(data.error.code, data.error.message, data.error.data);
|
|
2484
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 });
|
|
2485
2865
|
return data.result;
|
|
2486
2866
|
} catch (err) {
|
|
2867
|
+
throwIfAborted(options?.signal);
|
|
2487
2868
|
if (err instanceof ACPError) throw err;
|
|
2488
2869
|
throw new ACPError(
|
|
2489
2870
|
ACPErrorCodes.TRANSPORT_ERROR,
|
|
2490
|
-
`Transport error: ${err
|
|
2491
|
-
{ method, originalError: err
|
|
2871
|
+
`Transport error: ${errorMessage(err)}`,
|
|
2872
|
+
{ method, originalError: errorMessage(err) }
|
|
2492
2873
|
);
|
|
2493
2874
|
}
|
|
2494
2875
|
}
|
|
@@ -2508,7 +2889,7 @@ var HttpTransport = class {
|
|
|
2508
2889
|
/**
|
|
2509
2890
|
* Stream a chat request via SSE.
|
|
2510
2891
|
*/
|
|
2511
|
-
async *streamChat(method, params) {
|
|
2892
|
+
async *streamChat(method, params, options) {
|
|
2512
2893
|
const id = ++this.requestId;
|
|
2513
2894
|
const body = {
|
|
2514
2895
|
jsonrpc: "2.0",
|
|
@@ -2519,33 +2900,48 @@ var HttpTransport = class {
|
|
|
2519
2900
|
const res = await fetch(`${this.baseUrl}/mcp`, {
|
|
2520
2901
|
method: "POST",
|
|
2521
2902
|
headers: this.buildHeaders({ Accept: "text/event-stream" }),
|
|
2903
|
+
signal: options?.signal,
|
|
2522
2904
|
body: JSON.stringify(body)
|
|
2523
2905
|
});
|
|
2906
|
+
if (!res.ok) throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, `HTTP ${res.status}`, { method, status: res.status });
|
|
2524
2907
|
if (!res.body) {
|
|
2525
2908
|
throw new ACPError(ACPErrorCodes.TRANSPORT_ERROR, "No response body for stream");
|
|
2526
2909
|
}
|
|
2527
2910
|
const reader = res.body.getReader();
|
|
2528
2911
|
const decoder = new TextDecoder();
|
|
2529
2912
|
let buffer = "";
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
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");
|
|
2544
2933
|
}
|
|
2545
|
-
|
|
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 };
|
|
2546
2937
|
}
|
|
2547
2938
|
}
|
|
2939
|
+
if (done) break;
|
|
2548
2940
|
}
|
|
2941
|
+
} finally {
|
|
2942
|
+
await reader.cancel().catch(() => {
|
|
2943
|
+
});
|
|
2944
|
+
reader.releaseLock();
|
|
2549
2945
|
}
|
|
2550
2946
|
}
|
|
2551
2947
|
};
|
|
@@ -2592,28 +2988,35 @@ var Ldoing = class {
|
|
|
2592
2988
|
constructor(config) {
|
|
2593
2989
|
this.initialized = false;
|
|
2594
2990
|
this.startTime = 0;
|
|
2991
|
+
this.mcpStatus = "unchecked";
|
|
2992
|
+
this.discoveredServerSkills = /* @__PURE__ */ new WeakSet();
|
|
2595
2993
|
this.currentSessionId = null;
|
|
2596
2994
|
this.abortControllers = /* @__PURE__ */ new Map();
|
|
2995
|
+
this.runContexts = /* @__PURE__ */ new WeakMap();
|
|
2597
2996
|
// Session-level concurrency control (方案 B: abort 旧请求 + 等待退出)
|
|
2598
2997
|
// runningSessions 跟踪正在执行 agent loop 的会话,donePromise 在 agent loop 结束时 resolve
|
|
2599
2998
|
this.runningSessions = /* @__PURE__ */ new Map();
|
|
2600
2999
|
// Approval pending promises
|
|
2601
3000
|
this.approvalResolvers = /* @__PURE__ */ new Map();
|
|
3001
|
+
this.sessionStorageQueues = /* @__PURE__ */ new Map();
|
|
3002
|
+
this.managingSessions = /* @__PURE__ */ new Map();
|
|
3003
|
+
this.compressingSessions = /* @__PURE__ */ new Map();
|
|
2602
3004
|
// Event listeners
|
|
2603
3005
|
this.eventHandlers = /* @__PURE__ */ new Map();
|
|
2604
3006
|
// Token estimation calibration
|
|
2605
3007
|
this.tokenCalibrationRatio = 1;
|
|
2606
|
-
|
|
3008
|
+
const proxy = resolveProxyConfig(config);
|
|
3009
|
+
this.config = config.llmProxyMode === void 0 ? config : { ...config, llm: { ...config.llm } };
|
|
2607
3010
|
this.agentId = `agent-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
2608
|
-
this.llmAdapter = new OpenAIAdapter(config.
|
|
3011
|
+
this.llmAdapter = new OpenAIAdapter(proxy.llm, { rejectRedirects: config.llmProxyMode !== void 0 });
|
|
2609
3012
|
this.skillRegistry = new SkillRegistry();
|
|
2610
|
-
this.skillExecutor = new SkillExecutor(this.skillRegistry);
|
|
3013
|
+
this.skillExecutor = new SkillExecutor(this.skillRegistry, void 0, { validateInputs: config.skills?.validateInputs });
|
|
2611
3014
|
this.mcpRouter = new McpRouter();
|
|
2612
3015
|
this.sessionManager = new SessionManager();
|
|
2613
3016
|
this.contextManager = new ContextManager(config.context, config.llm.model);
|
|
2614
3017
|
this.contextManager.setSummarizer(createLLMSummarizer(this.llmAdapter));
|
|
2615
|
-
if (
|
|
2616
|
-
this.transport = new HttpTransport(
|
|
3018
|
+
if (proxy.serverUrl) {
|
|
3019
|
+
this.transport = new HttpTransport(proxy.serverUrl, config.authToken, config.headers);
|
|
2617
3020
|
} else {
|
|
2618
3021
|
this.transport = new LocalTransport();
|
|
2619
3022
|
}
|
|
@@ -2634,10 +3037,13 @@ var Ldoing = class {
|
|
|
2634
3037
|
}
|
|
2635
3038
|
}
|
|
2636
3039
|
let mode = "local";
|
|
2637
|
-
let connected = false;
|
|
2638
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";
|
|
2639
3045
|
try {
|
|
2640
|
-
const result2 = await
|
|
3046
|
+
const result2 = await transport.send("initialize", {
|
|
2641
3047
|
protocolVersion: "2025-03-26",
|
|
2642
3048
|
capabilities: {
|
|
2643
3049
|
acp: {
|
|
@@ -2645,47 +3051,47 @@ var Ldoing = class {
|
|
|
2645
3051
|
sessions: true,
|
|
2646
3052
|
contextManagement: true,
|
|
2647
3053
|
dynamicSkillRegistration: true,
|
|
2648
|
-
llmConfig: this.config.llm
|
|
3054
|
+
llmConfig: proxyMode === void 0 ? this.config.llm : { adapter, model }
|
|
2649
3055
|
}
|
|
2650
3056
|
},
|
|
2651
3057
|
clientInfo: { name: "listdoing-sdk", version: "0.1.0" }
|
|
2652
3058
|
});
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
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
|
+
}
|
|
2663
3072
|
}
|
|
3073
|
+
if (this.transport === transport) this.mcpStatus = "connected";
|
|
3074
|
+
mode = this.skillRegistry.list().some((s) => s.location === "local") ? "hybrid" : "remote";
|
|
2664
3075
|
}
|
|
2665
|
-
mode = this.skillRegistry.list().some((s) => s.location === "local") ? "hybrid" : "remote";
|
|
2666
3076
|
} catch {
|
|
2667
|
-
|
|
3077
|
+
if (this.transport === transport) this.mcpStatus = "failed";
|
|
2668
3078
|
}
|
|
2669
3079
|
}
|
|
2670
|
-
let llmConnected = false;
|
|
2671
|
-
try {
|
|
2672
|
-
llmConnected = true;
|
|
2673
|
-
} catch {
|
|
2674
|
-
llmConnected = false;
|
|
2675
|
-
}
|
|
2676
3080
|
this.initialized = true;
|
|
2677
3081
|
const result = {
|
|
2678
3082
|
agentId: this.agentId,
|
|
2679
3083
|
mode,
|
|
3084
|
+
...this.getConnectionDetails(),
|
|
2680
3085
|
llm: {
|
|
2681
|
-
connected:
|
|
3086
|
+
connected: true,
|
|
3087
|
+
// Legacy status. Use llmRoute.verified for a successful request.
|
|
2682
3088
|
provider: this.config.llm.adapter,
|
|
2683
3089
|
model: this.config.llm.model
|
|
2684
3090
|
},
|
|
2685
3091
|
capabilities: {
|
|
2686
3092
|
localSkills: this.skillRegistry.list().filter((s) => s.location !== "server").map((s) => s.name),
|
|
2687
3093
|
serverSkills: this.skillRegistry.list().filter((s) => s.location === "server").map((s) => s.name),
|
|
2688
|
-
llmProxy: mode !== "local"
|
|
3094
|
+
llmProxy: this.config.llmProxyMode !== void 0 || mode !== "local"
|
|
2689
3095
|
}
|
|
2690
3096
|
};
|
|
2691
3097
|
this.emit("init", result);
|
|
@@ -2696,7 +3102,10 @@ var Ldoing = class {
|
|
|
2696
3102
|
controller.abort();
|
|
2697
3103
|
}
|
|
2698
3104
|
this.abortControllers.clear();
|
|
3105
|
+
for (const pending of this.approvalResolvers.values()) pending.reject(abortError());
|
|
3106
|
+
this.approvalResolvers.clear();
|
|
2699
3107
|
this.initialized = false;
|
|
3108
|
+
this.mcpStatus = "unchecked";
|
|
2700
3109
|
this.emit("destroy", {});
|
|
2701
3110
|
}
|
|
2702
3111
|
getStatus() {
|
|
@@ -2706,6 +3115,7 @@ var Ldoing = class {
|
|
|
2706
3115
|
uptime: Date.now() - this.startTime,
|
|
2707
3116
|
activeSessions: this.sessionManager.list({ status: "active" }).length,
|
|
2708
3117
|
loadedSkills: this.skillRegistry.list().length,
|
|
3118
|
+
...this.getConnectionDetails(),
|
|
2709
3119
|
llm: {
|
|
2710
3120
|
adapter: this.config.llm.adapter,
|
|
2711
3121
|
model: this.config.llm.model,
|
|
@@ -2713,15 +3123,26 @@ var Ldoing = class {
|
|
|
2713
3123
|
}
|
|
2714
3124
|
};
|
|
2715
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
|
+
}
|
|
2716
3135
|
// ---- Chat ----
|
|
2717
3136
|
async chat(message, options) {
|
|
2718
3137
|
this.ensureInitialized();
|
|
2719
3138
|
const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
|
|
2720
|
-
this.
|
|
3139
|
+
if (!options?._skipLock || !this.abortControllers.has(sessionId)) this.ensureSessionIsNotBeingManaged(sessionId);
|
|
3140
|
+
if (!options?._skipLock) this.currentSessionId = sessionId;
|
|
2721
3141
|
const releaseLock = options?._skipLock ? null : await this.acquireSessionLock(sessionId);
|
|
2722
|
-
const
|
|
2723
|
-
|
|
3142
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3143
|
+
const controller = execution.controller;
|
|
2724
3144
|
try {
|
|
3145
|
+
const runContext = this.prepareRunContext(controller.signal, options?.runContext);
|
|
2725
3146
|
if (options?.taskMode === "plan") {
|
|
2726
3147
|
return await this.chatWithPlan(message, options);
|
|
2727
3148
|
}
|
|
@@ -2738,12 +3159,14 @@ var Ldoing = class {
|
|
|
2738
3159
|
this.sessionManager.addMessage(sessionId, { role: "user", content: userContent });
|
|
2739
3160
|
let turn = 0;
|
|
2740
3161
|
let continueLoop = true;
|
|
3162
|
+
let completedReply;
|
|
2741
3163
|
let retried = false;
|
|
2742
3164
|
while (continueLoop && turn < maxTurns) {
|
|
3165
|
+
throwIfAborted(controller.signal);
|
|
2743
3166
|
turn++;
|
|
2744
3167
|
const session2 = this.sessionManager.getOrThrow(sessionId);
|
|
2745
|
-
const systemPrompt = this.
|
|
2746
|
-
const allTools = this.
|
|
3168
|
+
const systemPrompt = this.buildRunSystemPrompt(session2, runContext);
|
|
3169
|
+
const allTools = this.listRunTools(runContext);
|
|
2747
3170
|
const tools = this.resolveTools(allTools, { allowedTools: options?.tools, toolsByPermission: options?.toolsByPermission });
|
|
2748
3171
|
for (const name of this.computeAutoApproval(tools)) {
|
|
2749
3172
|
requireApproval.add(name);
|
|
@@ -2764,24 +3187,27 @@ var Ldoing = class {
|
|
|
2764
3187
|
afterTokens: ctxCheck.afterTokens
|
|
2765
3188
|
});
|
|
2766
3189
|
}
|
|
2767
|
-
|
|
3190
|
+
throwIfAborted(controller.signal);
|
|
3191
|
+
const window2 = this.contextManager.buildWindow(
|
|
2768
3192
|
session2.messages,
|
|
2769
3193
|
systemPrompt,
|
|
2770
3194
|
tools
|
|
2771
|
-
)
|
|
3195
|
+
);
|
|
3196
|
+
let contextMessages = window2.messages;
|
|
2772
3197
|
let llmResponse;
|
|
2773
3198
|
try {
|
|
2774
|
-
|
|
2775
|
-
signal: controller.signal
|
|
2776
|
-
})
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
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;
|
|
2780
3205
|
retried = true;
|
|
2781
|
-
|
|
2782
|
-
|
|
3206
|
+
contextMessages = smaller;
|
|
3207
|
+
llmResponse = await this.llmAdapter.chat(contextMessages, tools, { signal: controller.signal });
|
|
2783
3208
|
}
|
|
2784
|
-
|
|
3209
|
+
} catch (err) {
|
|
3210
|
+
if (err?.code === ACPErrorCodes.LLM_EMPTY_RESPONSE) {
|
|
2785
3211
|
if (!retried) {
|
|
2786
3212
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
|
|
2787
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" });
|
|
@@ -2790,10 +3216,12 @@ var Ldoing = class {
|
|
|
2790
3216
|
continue;
|
|
2791
3217
|
}
|
|
2792
3218
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
|
|
3219
|
+
completedReply = ".";
|
|
2793
3220
|
continueLoop = false;
|
|
2794
3221
|
break;
|
|
2795
3222
|
}
|
|
2796
|
-
|
|
3223
|
+
if (controller.signal.aborted || err?.name === "AbortError") throw abortError();
|
|
3224
|
+
throw new ACPError(ACPErrorCodes.LLM_CALL_FAILED, `LLM call failed: ${errorMessage(err)}`, { turn });
|
|
2797
3225
|
}
|
|
2798
3226
|
if (!llmResponse.content && !llmResponse.toolCalls?.length) {
|
|
2799
3227
|
if (!retried) {
|
|
@@ -2804,9 +3232,11 @@ var Ldoing = class {
|
|
|
2804
3232
|
continue;
|
|
2805
3233
|
}
|
|
2806
3234
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
|
|
3235
|
+
completedReply = ".";
|
|
2807
3236
|
continueLoop = false;
|
|
2808
3237
|
break;
|
|
2809
3238
|
}
|
|
3239
|
+
this.reportPromptCacheUsage(sessionId, llmResponse.promptCacheUsage);
|
|
2810
3240
|
if (llmResponse.usage) {
|
|
2811
3241
|
totalInputTokens += llmResponse.usage.input;
|
|
2812
3242
|
totalOutputTokens += llmResponse.usage.output;
|
|
@@ -2817,6 +3247,7 @@ var Ldoing = class {
|
|
|
2817
3247
|
}
|
|
2818
3248
|
}
|
|
2819
3249
|
if (!llmResponse.toolCalls?.length) {
|
|
3250
|
+
completedReply = llmResponse.content;
|
|
2820
3251
|
this.sessionManager.addMessage(sessionId, {
|
|
2821
3252
|
role: "assistant",
|
|
2822
3253
|
content: llmResponse.content
|
|
@@ -2830,8 +3261,9 @@ var Ldoing = class {
|
|
|
2830
3261
|
toolCalls: llmResponse.toolCalls
|
|
2831
3262
|
});
|
|
2832
3263
|
const turnTrace = { turn, toolCalls: [] };
|
|
2833
|
-
const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval);
|
|
2834
|
-
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
|
+
}
|
|
2835
3267
|
const batchTraces = await this.executeToolBatch(plan.items, sessionId, controller, plan.canParallelize, (event, data) => this.emit(event, data));
|
|
2836
3268
|
turnTrace.toolCalls = batchTraces;
|
|
2837
3269
|
const executableCount = plan.items.filter((i) => !i.resolved).length;
|
|
@@ -2839,26 +3271,27 @@ var Ldoing = class {
|
|
|
2839
3271
|
executionTrace.push(turnTrace);
|
|
2840
3272
|
}
|
|
2841
3273
|
const session = this.sessionManager.getOrThrow(sessionId);
|
|
2842
|
-
const
|
|
2843
|
-
const
|
|
2844
|
-
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 };
|
|
2845
3277
|
this.emit("chat:complete", { sessionId, reply, tokenUsage: chatResult.tokenUsage });
|
|
2846
|
-
await this.
|
|
2847
|
-
});
|
|
3278
|
+
await this.saveSessionSafely(session);
|
|
2848
3279
|
return chatResult;
|
|
2849
3280
|
} finally {
|
|
2850
|
-
|
|
3281
|
+
execution.cleanup();
|
|
2851
3282
|
if (releaseLock) releaseLock();
|
|
2852
3283
|
}
|
|
2853
3284
|
}
|
|
2854
3285
|
async *chatStream(message, options) {
|
|
2855
3286
|
this.ensureInitialized();
|
|
2856
3287
|
const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
|
|
2857
|
-
this.
|
|
3288
|
+
if (!options?._skipLock || !this.abortControllers.has(sessionId)) this.ensureSessionIsNotBeingManaged(sessionId);
|
|
3289
|
+
if (!options?._skipLock) this.currentSessionId = sessionId;
|
|
2858
3290
|
const releaseLock = options?._skipLock ? null : await this.acquireSessionLock(sessionId);
|
|
2859
|
-
const
|
|
2860
|
-
|
|
3291
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3292
|
+
const controller = execution.controller;
|
|
2861
3293
|
try {
|
|
3294
|
+
const runContext = this.prepareRunContext(controller.signal, options?.runContext);
|
|
2862
3295
|
if (options?.taskMode === "plan") {
|
|
2863
3296
|
yield* this.chatStreamWithPlan(message, options);
|
|
2864
3297
|
return;
|
|
@@ -2877,11 +3310,13 @@ var Ldoing = class {
|
|
|
2877
3310
|
this.sessionManager.addMessage(sessionId, { role: "user", content: userContent });
|
|
2878
3311
|
let turn = 0;
|
|
2879
3312
|
let continueLoop = true;
|
|
3313
|
+
let completedReply;
|
|
2880
3314
|
while (continueLoop && turn < maxTurns) {
|
|
3315
|
+
throwIfAborted(controller.signal);
|
|
2881
3316
|
turn++;
|
|
2882
3317
|
const session2 = this.sessionManager.getOrThrow(sessionId);
|
|
2883
|
-
const systemPrompt = this.
|
|
2884
|
-
const allTools = this.
|
|
3318
|
+
const systemPrompt = this.buildRunSystemPrompt(session2, runContext);
|
|
3319
|
+
const allTools = this.listRunTools(runContext);
|
|
2885
3320
|
const tools = this.resolveTools(allTools, { allowedTools: options?.tools, toolsByPermission: options?.toolsByPermission });
|
|
2886
3321
|
for (const name of this.computeAutoApproval(tools)) {
|
|
2887
3322
|
requireApproval.add(name);
|
|
@@ -2909,36 +3344,33 @@ var Ldoing = class {
|
|
|
2909
3344
|
reason: "auto"
|
|
2910
3345
|
};
|
|
2911
3346
|
}
|
|
2912
|
-
|
|
3347
|
+
throwIfAborted(controller.signal);
|
|
3348
|
+
const window2 = this.contextManager.buildWindow(session2.messages, systemPrompt, tools);
|
|
3349
|
+
let contextMessages = window2.messages;
|
|
2913
3350
|
let llmResponse;
|
|
2914
3351
|
try {
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
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;
|
|
2925
3362
|
}
|
|
2926
3363
|
}
|
|
2927
3364
|
} catch (err) {
|
|
2928
|
-
if (err
|
|
2929
|
-
await this.compressContext(sessionId, "llm_summary");
|
|
2930
|
-
retried = true;
|
|
2931
|
-
turn--;
|
|
2932
|
-
continue;
|
|
2933
|
-
}
|
|
2934
|
-
if (err.code === ACPErrorCodes.LLM_EMPTY_RESPONSE && !retried) {
|
|
3365
|
+
if (err?.code === ACPErrorCodes.LLM_EMPTY_RESPONSE && !retried) {
|
|
2935
3366
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
|
|
2936
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" });
|
|
2937
3368
|
retried = true;
|
|
2938
3369
|
turn--;
|
|
2939
3370
|
continue;
|
|
2940
3371
|
}
|
|
2941
|
-
|
|
3372
|
+
if (controller.signal.aborted || err?.name === "AbortError") throw abortError();
|
|
3373
|
+
yield { type: "error", code: ACPErrorCodes.LLM_CALL_FAILED, message: errorMessage(err) };
|
|
2942
3374
|
return;
|
|
2943
3375
|
}
|
|
2944
3376
|
if (!llmResponse.content && !llmResponse.toolCalls?.length) {
|
|
@@ -2950,8 +3382,10 @@ var Ldoing = class {
|
|
|
2950
3382
|
continue;
|
|
2951
3383
|
}
|
|
2952
3384
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: "." });
|
|
3385
|
+
completedReply = ".";
|
|
2953
3386
|
break;
|
|
2954
3387
|
}
|
|
3388
|
+
this.reportPromptCacheUsage(sessionId, llmResponse.promptCacheUsage);
|
|
2955
3389
|
if (llmResponse.usage) {
|
|
2956
3390
|
totalInputTokens += llmResponse.usage.input;
|
|
2957
3391
|
totalOutputTokens += llmResponse.usage.output;
|
|
@@ -2962,27 +3396,21 @@ var Ldoing = class {
|
|
|
2962
3396
|
}
|
|
2963
3397
|
}
|
|
2964
3398
|
if (!llmResponse.toolCalls?.length) {
|
|
3399
|
+
completedReply = llmResponse.content;
|
|
2965
3400
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: llmResponse.content });
|
|
2966
3401
|
break;
|
|
2967
3402
|
}
|
|
2968
3403
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: llmResponse.content, toolCalls: llmResponse.toolCalls });
|
|
2969
|
-
const plan = this.planToolExecution(llmResponse.toolCalls, options ?? {}, requireApproval);
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
await this.processApprovalQueue(plan.items, sessionId, (event, data) => this.emit(event, data), approvalYield);
|
|
2973
|
-
for (const evt of approvalEvents) yield evt;
|
|
2974
|
-
const batchEvents = [];
|
|
2975
|
-
const batchYield = (evt) => batchEvents.push(evt);
|
|
2976
|
-
await this.executeToolBatchStream(plan.items, sessionId, controller, plan.canParallelize, batchYield);
|
|
2977
|
-
for (const evt of batchEvents) yield evt;
|
|
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);
|
|
2978
3407
|
}
|
|
2979
3408
|
const session = this.sessionManager.getOrThrow(sessionId);
|
|
2980
|
-
const
|
|
2981
|
-
yield { type: "done",
|
|
2982
|
-
await
|
|
2983
|
-
});
|
|
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;
|
|
2984
3412
|
} finally {
|
|
2985
|
-
|
|
3413
|
+
execution.cleanup();
|
|
2986
3414
|
if (releaseLock) releaseLock();
|
|
2987
3415
|
}
|
|
2988
3416
|
}
|
|
@@ -2992,7 +3420,6 @@ var Ldoing = class {
|
|
|
2992
3420
|
const controller = this.abortControllers.get(sid);
|
|
2993
3421
|
if (controller) {
|
|
2994
3422
|
controller.abort();
|
|
2995
|
-
this.abortControllers.delete(sid);
|
|
2996
3423
|
}
|
|
2997
3424
|
}
|
|
2998
3425
|
}
|
|
@@ -3000,7 +3427,7 @@ var Ldoing = class {
|
|
|
3000
3427
|
/**
|
|
3001
3428
|
* 在启动新的 agent loop 前获取会话锁。
|
|
3002
3429
|
* 根据并发策略处理正在运行的旧请求:
|
|
3003
|
-
* - 'abort' (默认):
|
|
3430
|
+
* - 'abort' (默认): 中止旧请求,等待其真正退出,发送前修复未配对消息,然后返回
|
|
3004
3431
|
* - 'reject': 若已有运行请求,抛错
|
|
3005
3432
|
* - 'queue': 预留,当前回退到 'abort'
|
|
3006
3433
|
*
|
|
@@ -3008,48 +3435,27 @@ var Ldoing = class {
|
|
|
3008
3435
|
*/
|
|
3009
3436
|
async acquireSessionLock(sessionId) {
|
|
3010
3437
|
const policy = this.config.concurrentPolicy ?? "abort";
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
ACPErrorCodes.SESSION_BUSY,
|
|
3016
|
-
`Session ${sessionId} is already running a chat`
|
|
3017
|
-
);
|
|
3018
|
-
}
|
|
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`);
|
|
3019
3442
|
await this.abort(sessionId);
|
|
3020
3443
|
await running.donePromise;
|
|
3021
|
-
this.
|
|
3444
|
+
running = this.runningSessions.get(sessionId);
|
|
3022
3445
|
}
|
|
3446
|
+
this.ensureInitialized();
|
|
3447
|
+
this.ensureSessionIsNotBeingManaged(sessionId);
|
|
3023
3448
|
let resolveDone;
|
|
3024
3449
|
const donePromise = new Promise((resolve) => {
|
|
3025
3450
|
resolveDone = resolve;
|
|
3026
3451
|
});
|
|
3027
|
-
|
|
3452
|
+
const owner = { donePromise, resolveDone };
|
|
3453
|
+
this.runningSessions.set(sessionId, owner);
|
|
3028
3454
|
return () => {
|
|
3029
|
-
this.runningSessions.delete(sessionId);
|
|
3455
|
+
if (this.runningSessions.get(sessionId) === owner) this.runningSessions.delete(sessionId);
|
|
3030
3456
|
resolveDone();
|
|
3031
3457
|
};
|
|
3032
3458
|
}
|
|
3033
|
-
/**
|
|
3034
|
-
* 清理被 abort 打断的半截消息:
|
|
3035
|
-
* 如果 session.messages 最后一条是 assistant 且带 tool_calls,
|
|
3036
|
-
* 但后面没有对应的 tool 消息,则删除这条 assistant。
|
|
3037
|
-
* 避免下次 LLM 调用因 tool_calls 未配对而报错。
|
|
3038
|
-
*/
|
|
3039
|
-
cleanupInterruptedMessages(sessionId) {
|
|
3040
|
-
try {
|
|
3041
|
-
const session = this.sessionManager.get(sessionId);
|
|
3042
|
-
if (!session?.messages?.length) return;
|
|
3043
|
-
const msgs = session.messages;
|
|
3044
|
-
const last = msgs[msgs.length - 1];
|
|
3045
|
-
if (last.role === "assistant" && last.toolCalls?.length) {
|
|
3046
|
-
msgs.pop();
|
|
3047
|
-
console.log(`[concurrency] cleaned up interrupted assistant(tool_calls) from session ${sessionId}`);
|
|
3048
|
-
}
|
|
3049
|
-
} catch (err) {
|
|
3050
|
-
console.warn("[concurrency] failed to cleanup interrupted messages:", err);
|
|
3051
|
-
}
|
|
3052
|
-
}
|
|
3053
3459
|
// ---- Tool Approval ----
|
|
3054
3460
|
approveTool(callId, approved, _reason) {
|
|
3055
3461
|
const resolver = this.approvalResolvers.get(callId);
|
|
@@ -3058,15 +3464,28 @@ var Ldoing = class {
|
|
|
3058
3464
|
this.approvalResolvers.delete(callId);
|
|
3059
3465
|
}
|
|
3060
3466
|
}
|
|
3061
|
-
waitForApproval(callId) {
|
|
3467
|
+
waitForApproval(callId, signal) {
|
|
3468
|
+
throwIfAborted(signal);
|
|
3062
3469
|
return new Promise((resolve, reject) => {
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
|
|
3066
|
-
|
|
3067
|
-
|
|
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);
|
|
3068
3483
|
}
|
|
3069
|
-
}
|
|
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 });
|
|
3070
3489
|
});
|
|
3071
3490
|
}
|
|
3072
3491
|
// ---- Skill Management ----
|
|
@@ -3132,31 +3551,31 @@ ${JSON.stringify(params, null, 2)}`;
|
|
|
3132
3551
|
} else {
|
|
3133
3552
|
prompt = loop.instruction;
|
|
3134
3553
|
}
|
|
3135
|
-
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 });
|
|
3136
3555
|
return { content: [{ type: "text", text: result.reply }] };
|
|
3137
3556
|
};
|
|
3138
3557
|
}
|
|
3139
3558
|
/** Internal chat for agentLoop/workflow/sub-agent execution. Condenses inner messages after completion. */
|
|
3140
3559
|
async chatInner(prompt, sessionId, options) {
|
|
3141
3560
|
const session = this.sessionManager.getOrThrow(sessionId);
|
|
3142
|
-
const
|
|
3143
|
-
const
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
|
|
3149
|
-
|
|
3150
|
-
|
|
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;
|
|
3151
3571
|
}
|
|
3152
|
-
return result;
|
|
3153
3572
|
}
|
|
3154
3573
|
// ---- Task Plan Execution ----
|
|
3155
3574
|
async chatWithPlan(message, options) {
|
|
3156
3575
|
const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
|
|
3157
3576
|
this.currentSessionId = sessionId;
|
|
3158
|
-
const
|
|
3159
|
-
|
|
3577
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3578
|
+
const controller = execution.controller;
|
|
3160
3579
|
const startTime = Date.now();
|
|
3161
3580
|
let totalInputTokens = 0;
|
|
3162
3581
|
let totalOutputTokens = 0;
|
|
@@ -3170,8 +3589,9 @@ ${JSON.stringify(params, null, 2)}`;
|
|
|
3170
3589
|
let plan;
|
|
3171
3590
|
try {
|
|
3172
3591
|
const planner = new TaskPlanner(this.llmAdapter);
|
|
3173
|
-
plan = await planner.plan(messageText, filteredTools);
|
|
3592
|
+
plan = await planner.plan(messageText, filteredTools, controller.signal);
|
|
3174
3593
|
} catch (err) {
|
|
3594
|
+
throwIfAborted(controller.signal);
|
|
3175
3595
|
throw new ACPError(ACPErrorCodes.TASK_PLAN_FAILED, `Task planning failed: ${err.message}`);
|
|
3176
3596
|
}
|
|
3177
3597
|
const tracker = new PlanTracker(plan, sessionId, (event, data) => this.emit(event, data));
|
|
@@ -3182,13 +3602,14 @@ ${JSON.stringify(params, null, 2)}`;
|
|
|
3182
3602
|
const maxRetry = options?.stepRetryCount ?? 1;
|
|
3183
3603
|
const continueOnFailure = options?.continueOnStepFailure ?? true;
|
|
3184
3604
|
for (let i = 0; i < plan.steps.length; i++) {
|
|
3185
|
-
|
|
3605
|
+
throwIfAborted(controller.signal);
|
|
3186
3606
|
const step = plan.steps[i];
|
|
3187
3607
|
if (step.status === "done" || step.status === "skipped") continue;
|
|
3188
3608
|
tracker.startStep(i);
|
|
3189
3609
|
let stepResult;
|
|
3190
3610
|
let succeeded = false;
|
|
3191
3611
|
for (let attempt = 0; attempt <= maxRetry; attempt++) {
|
|
3612
|
+
throwIfAborted(controller.signal);
|
|
3192
3613
|
try {
|
|
3193
3614
|
const result = await this.chatInner(step.goal, sessionId, { maxTurns: options?.maxTurns ?? 10, tools: step.tools });
|
|
3194
3615
|
stepResult = { output: result.reply, toolCalls: result.executionTrace.reduce((sum, t) => sum + t.toolCalls.length, 0), tokenUsage: result.tokenUsage, duration: result.totalDuration };
|
|
@@ -3198,6 +3619,7 @@ ${JSON.stringify(params, null, 2)}`;
|
|
|
3198
3619
|
succeeded = true;
|
|
3199
3620
|
break;
|
|
3200
3621
|
} catch (err) {
|
|
3622
|
+
throwIfAborted(controller.signal);
|
|
3201
3623
|
stepResult = { output: "", toolCalls: 0, tokenUsage: { input: 0, output: 0 }, duration: 0, error: err.message };
|
|
3202
3624
|
plan.steps[i].retryCount = attempt + 1;
|
|
3203
3625
|
}
|
|
@@ -3225,18 +3647,17 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3225
3647
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: finalReply });
|
|
3226
3648
|
const chatResult = { reply: finalReply, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
|
|
3227
3649
|
this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
|
|
3228
|
-
await this.
|
|
3229
|
-
});
|
|
3650
|
+
await this.saveSessionSafely(session);
|
|
3230
3651
|
return chatResult;
|
|
3231
3652
|
} finally {
|
|
3232
|
-
|
|
3653
|
+
execution.cleanup();
|
|
3233
3654
|
}
|
|
3234
3655
|
}
|
|
3235
3656
|
async *chatStreamWithPlan(message, options) {
|
|
3236
3657
|
const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
|
|
3237
3658
|
this.currentSessionId = sessionId;
|
|
3238
|
-
const
|
|
3239
|
-
|
|
3659
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3660
|
+
const controller = execution.controller;
|
|
3240
3661
|
const startTime = Date.now();
|
|
3241
3662
|
let totalInputTokens = 0;
|
|
3242
3663
|
let totalOutputTokens = 0;
|
|
@@ -3249,7 +3670,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3249
3670
|
let plan;
|
|
3250
3671
|
try {
|
|
3251
3672
|
const planner = new TaskPlanner(this.llmAdapter);
|
|
3252
|
-
plan = await planner.plan(messageText, filteredTools);
|
|
3673
|
+
plan = await planner.plan(messageText, filteredTools, controller.signal);
|
|
3253
3674
|
} catch (err) {
|
|
3254
3675
|
yield { type: "error", code: ACPErrorCodes.TASK_PLAN_FAILED, message: `Task planning failed: ${err.message}` };
|
|
3255
3676
|
return;
|
|
@@ -3263,7 +3684,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3263
3684
|
const maxRetry = options?.stepRetryCount ?? 1;
|
|
3264
3685
|
const continueOnFailure = options?.continueOnStepFailure ?? true;
|
|
3265
3686
|
for (let i = 0; i < plan.steps.length; i++) {
|
|
3266
|
-
|
|
3687
|
+
throwIfAborted(controller.signal);
|
|
3267
3688
|
const step = plan.steps[i];
|
|
3268
3689
|
if (step.status === "done" || step.status === "skipped") continue;
|
|
3269
3690
|
tracker.startStep(i);
|
|
@@ -3271,6 +3692,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3271
3692
|
let stepResult;
|
|
3272
3693
|
let succeeded = false;
|
|
3273
3694
|
for (let attempt = 0; attempt <= maxRetry; attempt++) {
|
|
3695
|
+
throwIfAborted(controller.signal);
|
|
3274
3696
|
try {
|
|
3275
3697
|
const result = await this.chatInner(step.goal, sessionId, { maxTurns: options?.maxTurns ?? 10, tools: step.tools });
|
|
3276
3698
|
stepResult = { output: result.reply, toolCalls: result.executionTrace.reduce((sum, t) => sum + t.toolCalls.length, 0), tokenUsage: result.tokenUsage, duration: result.totalDuration };
|
|
@@ -3279,6 +3701,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3279
3701
|
succeeded = true;
|
|
3280
3702
|
break;
|
|
3281
3703
|
} catch (err) {
|
|
3704
|
+
throwIfAborted(controller.signal);
|
|
3282
3705
|
stepResult = { output: "", toolCalls: 0, tokenUsage: { input: 0, output: 0 }, duration: 0, error: err.message };
|
|
3283
3706
|
plan.steps[i].retryCount = attempt + 1;
|
|
3284
3707
|
}
|
|
@@ -3306,11 +3729,11 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3306
3729
|
|
|
3307
3730
|
${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
3308
3731
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: finalReply });
|
|
3732
|
+
const saving = this.saveSessionSafely(session);
|
|
3309
3733
|
yield { type: "done", reply: finalReply, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
|
|
3310
|
-
await
|
|
3311
|
-
});
|
|
3734
|
+
await saving;
|
|
3312
3735
|
} finally {
|
|
3313
|
-
|
|
3736
|
+
execution.cleanup();
|
|
3314
3737
|
}
|
|
3315
3738
|
}
|
|
3316
3739
|
async resumeTask(sessionId, options) {
|
|
@@ -3320,6 +3743,8 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3320
3743
|
if (!savedPlan) {
|
|
3321
3744
|
throw new ACPError(ACPErrorCodes.TASK_PLAN_FAILED, "No task plan found in session");
|
|
3322
3745
|
}
|
|
3746
|
+
const releaseLock = await this.acquireSessionLock(sessionId);
|
|
3747
|
+
this.currentSessionId = sessionId;
|
|
3323
3748
|
for (const step of savedPlan.steps) {
|
|
3324
3749
|
if (step.status === "failed") {
|
|
3325
3750
|
step.status = "pending";
|
|
@@ -3327,8 +3752,8 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3327
3752
|
step.result = void 0;
|
|
3328
3753
|
}
|
|
3329
3754
|
}
|
|
3330
|
-
const
|
|
3331
|
-
|
|
3755
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3756
|
+
const controller = execution.controller;
|
|
3332
3757
|
const startTime = Date.now();
|
|
3333
3758
|
let totalInputTokens = 0;
|
|
3334
3759
|
let totalOutputTokens = 0;
|
|
@@ -3339,13 +3764,14 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3339
3764
|
const maxRetry = options?.stepRetryCount ?? 1;
|
|
3340
3765
|
const continueOnFailure = options?.continueOnStepFailure ?? true;
|
|
3341
3766
|
for (let i = 0; i < savedPlan.steps.length; i++) {
|
|
3342
|
-
|
|
3767
|
+
throwIfAborted(controller.signal);
|
|
3343
3768
|
const step = savedPlan.steps[i];
|
|
3344
3769
|
if (step.status === "done" || step.status === "skipped") continue;
|
|
3345
3770
|
tracker.startStep(i);
|
|
3346
3771
|
let stepResult;
|
|
3347
3772
|
let succeeded = false;
|
|
3348
3773
|
for (let attempt = 0; attempt <= maxRetry; attempt++) {
|
|
3774
|
+
throwIfAborted(controller.signal);
|
|
3349
3775
|
try {
|
|
3350
3776
|
const result = await this.chatInner(step.goal, sessionId, { maxTurns: options?.maxTurns ?? 10, tools: step.tools });
|
|
3351
3777
|
stepResult = { output: result.reply, toolCalls: result.executionTrace.reduce((sum, t) => sum + t.toolCalls.length, 0), tokenUsage: result.tokenUsage, duration: result.totalDuration };
|
|
@@ -3355,6 +3781,7 @@ ${summaryLines.join("\n")}` : "Task completed with no successful steps.";
|
|
|
3355
3781
|
succeeded = true;
|
|
3356
3782
|
break;
|
|
3357
3783
|
} catch (err) {
|
|
3784
|
+
throwIfAborted(controller.signal);
|
|
3358
3785
|
stepResult = { output: "", toolCalls: 0, tokenUsage: { input: 0, output: 0 }, duration: 0, error: err.message };
|
|
3359
3786
|
savedPlan.steps[i].retryCount = attempt + 1;
|
|
3360
3787
|
}
|
|
@@ -3382,11 +3809,11 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
|
|
|
3382
3809
|
this.sessionManager.addMessage(sessionId, { role: "assistant", content: finalReply });
|
|
3383
3810
|
const chatResult = { reply: finalReply, sessionId, executionTrace, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
|
|
3384
3811
|
this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
|
|
3385
|
-
await this.
|
|
3386
|
-
});
|
|
3812
|
+
await this.saveSessionSafely(session);
|
|
3387
3813
|
return chatResult;
|
|
3388
3814
|
} finally {
|
|
3389
|
-
|
|
3815
|
+
execution.cleanup();
|
|
3816
|
+
releaseLock();
|
|
3390
3817
|
}
|
|
3391
3818
|
}
|
|
3392
3819
|
getTaskPlan(sessionId) {
|
|
@@ -3402,8 +3829,8 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
|
|
|
3402
3829
|
async chatWithGraph(message, options) {
|
|
3403
3830
|
const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
|
|
3404
3831
|
this.currentSessionId = sessionId;
|
|
3405
|
-
const
|
|
3406
|
-
|
|
3832
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3833
|
+
const controller = execution.controller;
|
|
3407
3834
|
const startTime = Date.now();
|
|
3408
3835
|
try {
|
|
3409
3836
|
const userContent = this.buildUserContent(message, options?.attachments);
|
|
@@ -3414,8 +3841,9 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
|
|
|
3414
3841
|
let plan;
|
|
3415
3842
|
try {
|
|
3416
3843
|
const planner = new GraphPlanner(this.llmAdapter);
|
|
3417
|
-
plan = await planner.planGraph(messageText, filteredTools);
|
|
3844
|
+
plan = await planner.planGraph(messageText, filteredTools, controller.signal);
|
|
3418
3845
|
} catch (err) {
|
|
3846
|
+
throwIfAborted(controller.signal);
|
|
3419
3847
|
throw new ACPError(ACPErrorCodes.GRAPH_PLAN_FAILED, `Graph planning failed: ${err.message}`);
|
|
3420
3848
|
}
|
|
3421
3849
|
const graph = new TaskGraph(plan.nodes);
|
|
@@ -3429,6 +3857,7 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
|
|
|
3429
3857
|
const executor = new GraphExecutor({
|
|
3430
3858
|
graph,
|
|
3431
3859
|
parentAgent: this,
|
|
3860
|
+
parentSignal: controller.signal,
|
|
3432
3861
|
plan,
|
|
3433
3862
|
maxConcurrency: options?.graphConfig?.maxConcurrency ?? 3,
|
|
3434
3863
|
maxDepth: options?.graphConfig?.maxDepth ?? 2,
|
|
@@ -3436,6 +3865,7 @@ ${summaryLines.join("\n")}` : "Task resumed but no steps completed successfully.
|
|
|
3436
3865
|
emit: (event, data) => this.emit(event, data)
|
|
3437
3866
|
});
|
|
3438
3867
|
const finalPlan = await executor.execute();
|
|
3868
|
+
throwIfAborted(controller.signal);
|
|
3439
3869
|
const resultLines = Object.entries(finalPlan.results).filter(([, r]) => r.status === "done" && r.output).map(([nodeId, r]) => `[${nodeId}] ${r.output.slice(0, 200)}`);
|
|
3440
3870
|
const progress = executor.getProgress();
|
|
3441
3871
|
const finalReply = resultLines.length > 0 ? `Graph task completed (${progress.done}/${progress.total} nodes done, ${progress.failed} failed, ${progress.bypassed} bypassed).
|
|
@@ -3454,18 +3884,17 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3454
3884
|
totalDuration: Date.now() - startTime
|
|
3455
3885
|
};
|
|
3456
3886
|
this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
|
|
3457
|
-
await this.
|
|
3458
|
-
});
|
|
3887
|
+
await this.saveSessionSafely(session);
|
|
3459
3888
|
return chatResult;
|
|
3460
3889
|
} finally {
|
|
3461
|
-
|
|
3890
|
+
execution.cleanup();
|
|
3462
3891
|
}
|
|
3463
3892
|
}
|
|
3464
3893
|
async *chatStreamWithGraph(message, options) {
|
|
3465
3894
|
const sessionId = options?.sessionId ?? this.currentSessionId ?? this.ensureSession().id;
|
|
3466
3895
|
this.currentSessionId = sessionId;
|
|
3467
|
-
const
|
|
3468
|
-
|
|
3896
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3897
|
+
const controller = execution.controller;
|
|
3469
3898
|
const startTime = Date.now();
|
|
3470
3899
|
let totalInputTokens = 0;
|
|
3471
3900
|
let totalOutputTokens = 0;
|
|
@@ -3478,7 +3907,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3478
3907
|
let plan;
|
|
3479
3908
|
try {
|
|
3480
3909
|
const planner = new GraphPlanner(this.llmAdapter);
|
|
3481
|
-
plan = await planner.planGraph(messageText, filteredTools);
|
|
3910
|
+
plan = await planner.planGraph(messageText, filteredTools, controller.signal);
|
|
3482
3911
|
} catch (err) {
|
|
3483
3912
|
yield { type: "error", code: ACPErrorCodes.GRAPH_PLAN_FAILED, message: `Graph planning failed: ${err.message}` };
|
|
3484
3913
|
return;
|
|
@@ -3495,6 +3924,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3495
3924
|
const executor = new GraphExecutor({
|
|
3496
3925
|
graph,
|
|
3497
3926
|
parentAgent: this,
|
|
3927
|
+
parentSignal: controller.signal,
|
|
3498
3928
|
plan,
|
|
3499
3929
|
maxConcurrency: options?.graphConfig?.maxConcurrency ?? 3,
|
|
3500
3930
|
maxDepth: options?.graphConfig?.maxDepth ?? 2,
|
|
@@ -3506,6 +3936,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3506
3936
|
}
|
|
3507
3937
|
});
|
|
3508
3938
|
const finalPlan = await executor.execute();
|
|
3939
|
+
throwIfAborted(controller.signal);
|
|
3509
3940
|
const progress = executor.getProgress();
|
|
3510
3941
|
const resultLines = Object.entries(finalPlan.results).filter(([, r]) => r.status === "done" && r.output).map(([nodeId, r]) => `[${nodeId}] ${r.output.slice(0, 200)}`);
|
|
3511
3942
|
const finalReply = resultLines.length > 0 ? `Graph task completed (${progress.done}/${progress.total} nodes done, ${progress.failed} failed, ${progress.bypassed} bypassed).
|
|
@@ -3517,11 +3948,11 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3517
3948
|
const session = this.sessionManager.getOrThrow(sessionId);
|
|
3518
3949
|
session.meta = session.meta ?? {};
|
|
3519
3950
|
session.meta.graphPlan = finalPlan;
|
|
3951
|
+
const saving = this.saveSessionSafely(session);
|
|
3520
3952
|
yield { type: "done", reply: finalReply, tokenUsage: { input: totalInputTokens, output: totalOutputTokens }, totalDuration: Date.now() - startTime };
|
|
3521
|
-
await
|
|
3522
|
-
});
|
|
3953
|
+
await saving;
|
|
3523
3954
|
} finally {
|
|
3524
|
-
|
|
3955
|
+
execution.cleanup();
|
|
3525
3956
|
}
|
|
3526
3957
|
}
|
|
3527
3958
|
/** Resume a graph execution from checkpoint */
|
|
@@ -3532,6 +3963,8 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3532
3963
|
if (!savedPlan) {
|
|
3533
3964
|
throw new ACPError(ACPErrorCodes.GRAPH_PLAN_FAILED, "No graph plan found in session");
|
|
3534
3965
|
}
|
|
3966
|
+
const releaseLock = await this.acquireSessionLock(sessionId);
|
|
3967
|
+
this.currentSessionId = sessionId;
|
|
3535
3968
|
for (const [nodeId, result] of Object.entries(savedPlan.results)) {
|
|
3536
3969
|
if (result.status === "failed" || result.status === "bypassed") {
|
|
3537
3970
|
savedPlan.results[nodeId] = {
|
|
@@ -3544,14 +3977,15 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3544
3977
|
}
|
|
3545
3978
|
}
|
|
3546
3979
|
savedPlan.status = "executing";
|
|
3547
|
-
const
|
|
3548
|
-
|
|
3980
|
+
const execution = this.beginExecution(sessionId, options);
|
|
3981
|
+
const controller = execution.controller;
|
|
3549
3982
|
const startTime = Date.now();
|
|
3550
3983
|
try {
|
|
3551
3984
|
const graph = new TaskGraph(savedPlan.nodes);
|
|
3552
3985
|
const executor = new GraphExecutor({
|
|
3553
3986
|
graph,
|
|
3554
3987
|
parentAgent: this,
|
|
3988
|
+
parentSignal: controller.signal,
|
|
3555
3989
|
plan: savedPlan,
|
|
3556
3990
|
maxConcurrency: options?.graphConfig?.maxConcurrency ?? 3,
|
|
3557
3991
|
maxDepth: options?.graphConfig?.maxDepth ?? 2,
|
|
@@ -3559,6 +3993,7 @@ ${resultLines.join("\n")}` : "Graph task completed with no successful nodes.";
|
|
|
3559
3993
|
emit: (event, data) => this.emit(event, data)
|
|
3560
3994
|
});
|
|
3561
3995
|
const finalPlan = await executor.execute();
|
|
3996
|
+
throwIfAborted(controller.signal);
|
|
3562
3997
|
const resultLines = Object.entries(finalPlan.results).filter(([, r]) => r.status === "done" && r.output).map(([nodeId, r]) => `[${nodeId}] ${r.output.slice(0, 200)}`);
|
|
3563
3998
|
const progress = executor.getProgress();
|
|
3564
3999
|
const finalReply = resultLines.length > 0 ? `Graph resumed & completed (${progress.done}/${progress.total} nodes done).
|
|
@@ -3575,11 +4010,11 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3575
4010
|
totalDuration: Date.now() - startTime
|
|
3576
4011
|
};
|
|
3577
4012
|
this.emit("chat:complete", { sessionId, reply: finalReply, tokenUsage: chatResult.tokenUsage });
|
|
3578
|
-
await this.
|
|
3579
|
-
});
|
|
4013
|
+
await this.saveSessionSafely(session);
|
|
3580
4014
|
return chatResult;
|
|
3581
4015
|
} finally {
|
|
3582
|
-
|
|
4016
|
+
execution.cleanup();
|
|
4017
|
+
releaseLock();
|
|
3583
4018
|
}
|
|
3584
4019
|
}
|
|
3585
4020
|
/** Get graph plan from session */
|
|
@@ -3600,6 +4035,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3600
4035
|
const failedSteps = [];
|
|
3601
4036
|
const total = workflow.steps.length;
|
|
3602
4037
|
for (let i = 0; i < workflow.steps.length; i++) {
|
|
4038
|
+
throwIfAborted(ctx.signal);
|
|
3603
4039
|
const step = workflow.steps[i];
|
|
3604
4040
|
const interpolatedPrompt = interpolateTemplate(step.prompt, {
|
|
3605
4041
|
inputs: params,
|
|
@@ -3616,7 +4052,8 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3616
4052
|
try {
|
|
3617
4053
|
const result = await ctx.agent.chatInner(interpolatedPrompt, ctx.sessionId, {
|
|
3618
4054
|
maxTurns: step.maxTurns ?? 10,
|
|
3619
|
-
tools: step.tools
|
|
4055
|
+
tools: step.tools,
|
|
4056
|
+
signal: ctx.signal
|
|
3620
4057
|
});
|
|
3621
4058
|
stepOutputs[step.name] = { output: result.reply };
|
|
3622
4059
|
completedSteps.push(step.name);
|
|
@@ -3701,6 +4138,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3701
4138
|
const executor = new GraphExecutor({
|
|
3702
4139
|
graph,
|
|
3703
4140
|
parentAgent: this,
|
|
4141
|
+
parentSignal: ctx.signal,
|
|
3704
4142
|
plan,
|
|
3705
4143
|
maxConcurrency: 3,
|
|
3706
4144
|
maxDepth: 2,
|
|
@@ -3708,6 +4146,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3708
4146
|
emit: (event, data) => this.emit(event, data)
|
|
3709
4147
|
});
|
|
3710
4148
|
const finalPlan = await executor.execute();
|
|
4149
|
+
throwIfAborted(ctx.signal);
|
|
3711
4150
|
const aggregation = skill.subgraph?.aggregation ?? "concat";
|
|
3712
4151
|
if (aggregation === "last") {
|
|
3713
4152
|
const lastDone = Object.values(finalPlan.results).reverse().find((r) => r.status === "done" && r.output);
|
|
@@ -3746,6 +4185,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3746
4185
|
const executor = new GraphExecutor({
|
|
3747
4186
|
graph,
|
|
3748
4187
|
parentAgent: this,
|
|
4188
|
+
parentSignal: ctx.signal,
|
|
3749
4189
|
plan,
|
|
3750
4190
|
maxConcurrency: 3,
|
|
3751
4191
|
maxDepth: 2,
|
|
@@ -3753,6 +4193,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3753
4193
|
emit: (event, data) => this.emit(event, data)
|
|
3754
4194
|
});
|
|
3755
4195
|
const finalPlan = await executor.execute();
|
|
4196
|
+
throwIfAborted(ctx.signal);
|
|
3756
4197
|
const aggregation = subgraphDef.aggregation ?? "concat";
|
|
3757
4198
|
if (aggregation === "last") {
|
|
3758
4199
|
const lastDone = Object.values(finalPlan.results).reverse().find((r) => r.status === "done" && r.output);
|
|
@@ -3771,7 +4212,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3771
4212
|
// ---- Session Management ----
|
|
3772
4213
|
createSession(options) {
|
|
3773
4214
|
const session = this.sessionManager.create(options);
|
|
3774
|
-
this.currentSessionId = session.id;
|
|
4215
|
+
if (options?.activate !== false) this.currentSessionId = session.id;
|
|
3775
4216
|
return session;
|
|
3776
4217
|
}
|
|
3777
4218
|
getSession(sessionId) {
|
|
@@ -3784,17 +4225,75 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3784
4225
|
return this.sessionManager.search(query, options);
|
|
3785
4226
|
}
|
|
3786
4227
|
mergeSessions(fromId, toId, strategy) {
|
|
4228
|
+
this.ensureSessionIsNotBeingManaged(fromId);
|
|
4229
|
+
this.ensureSessionIsNotBeingManaged(toId);
|
|
3787
4230
|
this.sessionManager.merge(fromId, toId, strategy);
|
|
3788
4231
|
}
|
|
3789
4232
|
deleteSession(sessionId) {
|
|
4233
|
+
this.ensureSessionIsNotBeingManaged(sessionId);
|
|
4234
|
+
this.removeLiveSession(sessionId);
|
|
4235
|
+
}
|
|
4236
|
+
removeLiveSession(sessionId) {
|
|
3790
4237
|
this.sessionManager.delete(sessionId);
|
|
3791
4238
|
if (this.currentSessionId === sessionId) {
|
|
3792
4239
|
this.currentSessionId = null;
|
|
3793
4240
|
}
|
|
3794
4241
|
}
|
|
3795
4242
|
archiveSession(sessionId) {
|
|
4243
|
+
this.ensureSessionIsNotBeingManaged(sessionId);
|
|
3796
4244
|
this.sessionManager.archive(sessionId);
|
|
3797
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
|
+
}
|
|
3798
4297
|
// ---- Context Management ----
|
|
3799
4298
|
getContext(sessionId, format = "window") {
|
|
3800
4299
|
const session = this.sessionManager.getOrThrow(sessionId);
|
|
@@ -3810,16 +4309,45 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3810
4309
|
return this.contextManager.search(allMessages, query, options);
|
|
3811
4310
|
}
|
|
3812
4311
|
async compressContext(sessionId, strategy) {
|
|
4312
|
+
this.ensureSessionIsNotBeingManaged(sessionId);
|
|
3813
4313
|
const session = this.sessionManager.getOrThrow(sessionId);
|
|
3814
|
-
|
|
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);
|
|
3815
4334
|
session.messages = result.messages;
|
|
3816
4335
|
session.tokenCount = result.afterTokens;
|
|
3817
4336
|
session.updatedAt = Date.now();
|
|
3818
|
-
this.emit("context:compress", {
|
|
3819
|
-
|
|
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;
|
|
3820
4344
|
}
|
|
3821
4345
|
// ---- Runtime Config ----
|
|
3822
4346
|
configure(config) {
|
|
4347
|
+
if (this.config.llmProxyMode !== void 0 || config.llmProxyMode !== void 0) {
|
|
4348
|
+
this.configureProxy(config);
|
|
4349
|
+
return;
|
|
4350
|
+
}
|
|
3823
4351
|
if (config.llm) {
|
|
3824
4352
|
Object.assign(this.config.llm, config.llm);
|
|
3825
4353
|
this.llmAdapter = new OpenAIAdapter(this.config.llm);
|
|
@@ -3829,6 +4357,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3829
4357
|
if (config.serverUrl) {
|
|
3830
4358
|
this.config.serverUrl = config.serverUrl;
|
|
3831
4359
|
this.transport = new HttpTransport(config.serverUrl, this.config.authToken, this.config.headers);
|
|
4360
|
+
this.mcpStatus = "unchecked";
|
|
3832
4361
|
}
|
|
3833
4362
|
if (config.authToken) {
|
|
3834
4363
|
this.config.authToken = config.authToken;
|
|
@@ -3837,6 +4366,33 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3837
4366
|
this.config.systemPrompt = config.systemPrompt;
|
|
3838
4367
|
}
|
|
3839
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
|
+
}
|
|
3840
4396
|
// ---- Events ----
|
|
3841
4397
|
on(event, handler) {
|
|
3842
4398
|
if (!this.eventHandlers.has(event)) {
|
|
@@ -3857,10 +4413,15 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3857
4413
|
}
|
|
3858
4414
|
// ---- Private Helpers ----
|
|
3859
4415
|
/**
|
|
3860
|
-
*
|
|
3861
|
-
* 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.
|
|
3862
4417
|
*/
|
|
3863
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
|
+
}
|
|
3864
4425
|
let fullResponse = null;
|
|
3865
4426
|
for await (const event of this.llmAdapter.chatStream(messages, tools, { signal })) {
|
|
3866
4427
|
if (event.type === "llm_text" || event.type === "llm_thinking") {
|
|
@@ -3954,59 +4515,53 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
3954
4515
|
});
|
|
3955
4516
|
}
|
|
3956
4517
|
// ── Parallel tool execution helpers ──
|
|
3957
|
-
planToolExecution(toolCalls, chatOptions, requireApproval) {
|
|
4518
|
+
planToolExecution(toolCalls, chatOptions, requireApproval, runContext, exposedTools) {
|
|
3958
4519
|
const items = toolCalls.map((tc) => {
|
|
3959
4520
|
const isMcp = this.mcpRouter.isMcpTool(tc.name);
|
|
3960
|
-
const
|
|
4521
|
+
const scopedSkill = runContext?.registry.get(tc.name);
|
|
4522
|
+
const skillDef = scopedSkill ?? this.skillRegistry.get(tc.name);
|
|
3961
4523
|
const location = isMcp ? "server" : skillDef?.location === "server" ? "server" : "local";
|
|
3962
|
-
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);
|
|
3963
4525
|
const parallelSafe = skillDef?.parallelSafe ?? true;
|
|
3964
4526
|
return { toolCall: tc, skillDef, location, permPolicy, parallelSafe };
|
|
3965
4527
|
});
|
|
3966
4528
|
const canParallelize = items.every((item) => item.parallelSafe);
|
|
3967
4529
|
return { canParallelize, items };
|
|
3968
4530
|
}
|
|
3969
|
-
async processApprovalQueue(items, sessionId,
|
|
4531
|
+
async *processApprovalQueue(items, sessionId, signal) {
|
|
3970
4532
|
for (const item of items) {
|
|
3971
|
-
|
|
3972
|
-
|
|
3973
|
-
|
|
3974
|
-
|
|
3975
|
-
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
3979
|
-
|
|
3980
|
-
|
|
3981
|
-
|
|
3982
|
-
}
|
|
3983
|
-
|
|
3984
|
-
|
|
3985
|
-
|
|
3986
|
-
|
|
3987
|
-
|
|
3988
|
-
const approved = await this.waitForApproval(item.toolCall.id);
|
|
3989
|
-
if (!approved) {
|
|
3990
|
-
const rejectResult = {
|
|
3991
|
-
content: [{ type: "text", text: "Tool call rejected by user." }],
|
|
3992
|
-
isError: true
|
|
3993
|
-
};
|
|
3994
|
-
item.resolved = true;
|
|
3995
|
-
item.result = rejectResult;
|
|
3996
|
-
this.addToolResult(sessionId, item.toolCall, rejectResult);
|
|
3997
|
-
emit("tool:result", { sessionId, toolName: item.toolCall.name, isError: true, summary: "Rejected", duration: 0 });
|
|
3998
|
-
if (yieldEvent) {
|
|
3999
|
-
yieldEvent({ type: "tool_result", callId: item.toolCall.id, toolName: item.toolCall.name, isError: true, summary: "Rejected", duration: 0 });
|
|
4000
|
-
}
|
|
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);
|
|
4001
4550
|
}
|
|
4002
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
|
+
}
|
|
4003
4561
|
}
|
|
4004
4562
|
}
|
|
4005
4563
|
async executeToolBatch(items, sessionId, controller, canParallelize, emit) {
|
|
4006
4564
|
const traceEntries = [];
|
|
4007
|
-
for (const item of items) {
|
|
4008
|
-
emit("tool:call", { sessionId, toolName: item.toolCall.name, arguments: item.toolCall.arguments, location: item.location });
|
|
4009
|
-
}
|
|
4010
4565
|
for (const item of items) {
|
|
4011
4566
|
if (item.resolved) {
|
|
4012
4567
|
const summary = item.result?.content.filter((c) => c.type === "text").map((c) => c.text.slice(0, 100)).join(" ") ?? "";
|
|
@@ -4062,18 +4617,41 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
4062
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 });
|
|
4063
4618
|
}
|
|
4064
4619
|
}
|
|
4620
|
+
throwIfAborted(controller.signal);
|
|
4065
4621
|
return traceEntries;
|
|
4066
4622
|
}
|
|
4067
|
-
async
|
|
4068
|
-
const
|
|
4069
|
-
|
|
4070
|
-
|
|
4071
|
-
|
|
4072
|
-
|
|
4073
|
-
|
|
4074
|
-
|
|
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
|
+
});
|
|
4075
4645
|
}
|
|
4646
|
+
if (failure) throw failure;
|
|
4647
|
+
throwIfAborted(controller.signal);
|
|
4648
|
+
} finally {
|
|
4649
|
+
if (!done) controller.abort();
|
|
4650
|
+
await task;
|
|
4076
4651
|
}
|
|
4652
|
+
}
|
|
4653
|
+
async executeToolBatchStream(items, sessionId, controller, canParallelize, yieldEvent) {
|
|
4654
|
+
const traceEntries = [];
|
|
4077
4655
|
const executableItems = items.filter((item) => !item.resolved);
|
|
4078
4656
|
if (executableItems.length === 0) return traceEntries;
|
|
4079
4657
|
if (executableItems.length === 1) {
|
|
@@ -4123,6 +4701,7 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
4123
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 });
|
|
4124
4702
|
}
|
|
4125
4703
|
}
|
|
4704
|
+
throwIfAborted(controller.signal);
|
|
4126
4705
|
return traceEntries;
|
|
4127
4706
|
}
|
|
4128
4707
|
/**
|
|
@@ -4130,28 +4709,34 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
4130
4709
|
*/
|
|
4131
4710
|
async executeToolCall(toolCall, sessionId, controller) {
|
|
4132
4711
|
const execStart = Date.now();
|
|
4712
|
+
throwIfAborted(controller.signal);
|
|
4713
|
+
if (toolCall.parseError) return { content: [{ type: "text", text: toolCall.parseError }], isError: true };
|
|
4133
4714
|
if (this.mcpRouter.isMcpTool(toolCall.name)) {
|
|
4134
4715
|
try {
|
|
4135
|
-
const result = await this.mcpRouter.call(toolCall.name, toolCall.arguments);
|
|
4136
|
-
|
|
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) ?? "" }] };
|
|
4137
4719
|
} catch (err) {
|
|
4138
|
-
|
|
4720
|
+
throwIfAborted(controller.signal);
|
|
4721
|
+
return { content: [{ type: "text", text: `MCP error: ${errorMessage(err)}` }], isError: true };
|
|
4139
4722
|
}
|
|
4140
4723
|
}
|
|
4141
|
-
const
|
|
4724
|
+
const scope = this.runContexts.get(controller.signal);
|
|
4725
|
+
const skillDef = scope?.registry.get(toolCall.name) ?? this.skillRegistry.get(toolCall.name);
|
|
4142
4726
|
const location = skillDef?.location === "server" ? "server" : "local";
|
|
4143
4727
|
try {
|
|
4144
4728
|
if (location === "server" && this.transport instanceof HttpTransport) {
|
|
4145
|
-
return await this.executeServerSkill(toolCall);
|
|
4729
|
+
return await this.executeServerSkill(toolCall, controller.signal);
|
|
4146
4730
|
} else {
|
|
4147
|
-
const
|
|
4731
|
+
const executorFor = (name) => scope?.registry.has(name) ? scope.executor : this.skillExecutor;
|
|
4732
|
+
const invokeFn = (skillName, params) => executorFor(skillName).invokeDirect(skillName, params, {
|
|
4148
4733
|
sessionId,
|
|
4149
4734
|
agent: this,
|
|
4150
4735
|
signal: controller.signal,
|
|
4151
4736
|
depth: 0,
|
|
4152
4737
|
invoke: invokeFn
|
|
4153
4738
|
});
|
|
4154
|
-
return await
|
|
4739
|
+
return await executorFor(toolCall.name).execute(toolCall.name, toolCall.arguments, {
|
|
4155
4740
|
sessionId,
|
|
4156
4741
|
agent: this,
|
|
4157
4742
|
signal: controller.signal,
|
|
@@ -4160,14 +4745,143 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
4160
4745
|
});
|
|
4161
4746
|
}
|
|
4162
4747
|
} catch (err) {
|
|
4163
|
-
|
|
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);
|
|
4815
|
+
}
|
|
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 })) };
|
|
4164
4837
|
}
|
|
4165
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
|
+
}
|
|
4166
4867
|
ensureInitialized() {
|
|
4167
4868
|
if (!this.initialized) {
|
|
4168
4869
|
throw new ACPError(ACPErrorCodes.AGENT_NOT_INITIALIZED, "Agent not initialized. Call init() first.");
|
|
4169
4870
|
}
|
|
4170
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
|
+
}
|
|
4171
4885
|
ensureSession() {
|
|
4172
4886
|
if (this.currentSessionId) {
|
|
4173
4887
|
const existing = this.sessionManager.get(this.currentSessionId);
|
|
@@ -4200,9 +4914,10 @@ ${resultLines.join("\n")}` : "Graph resumed but no nodes completed successfully.
|
|
|
4200
4914
|
this.sessionManager.addMessage(sessionId, { role: "tool", content: result.content, toolCallId: toolCall.id });
|
|
4201
4915
|
}
|
|
4202
4916
|
}
|
|
4203
|
-
async executeServerSkill(toolCall) {
|
|
4917
|
+
async executeServerSkill(toolCall, signal) {
|
|
4204
4918
|
const transport = this.transport;
|
|
4205
|
-
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");
|
|
4206
4921
|
return result;
|
|
4207
4922
|
}
|
|
4208
4923
|
};
|