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