@celljs/ta-agents 4.11.0 → 4.12.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/extension/index.mjs +194 -17
- package/package.json +1 -1
package/extension/index.mjs
CHANGED
|
@@ -246,7 +246,7 @@ class AgentTurn {
|
|
|
246
246
|
async settle(id, quietMs, signal) {
|
|
247
247
|
for (;; ) {
|
|
248
248
|
const run = this.tools.get(id);
|
|
249
|
-
if (!run || run.complete || this.ended || signal?.aborted || this.items.some((item) => item.type !== "tool")) {
|
|
249
|
+
if (!run || run.complete || run.left || this.ended || signal?.aborted || this.items.some((item) => item.type !== "tool")) {
|
|
250
250
|
return;
|
|
251
251
|
}
|
|
252
252
|
const left = run.updatedAt + quietMs - Date.now();
|
|
@@ -259,12 +259,32 @@ class AgentTurn {
|
|
|
259
259
|
async waitTool(id, signal) {
|
|
260
260
|
for (;; ) {
|
|
261
261
|
const run = this.tools.get(id);
|
|
262
|
-
if (!run || run.done || this.ended || signal?.aborted) {
|
|
262
|
+
if (!run || run.done || run.left || this.ended || signal?.aborted) {
|
|
263
263
|
return run;
|
|
264
264
|
}
|
|
265
265
|
await this.changed(signal);
|
|
266
266
|
}
|
|
267
267
|
}
|
|
268
|
+
open() {
|
|
269
|
+
for (const run of this.tools.values()) {
|
|
270
|
+
if (!run.done && !run.left) {
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
step() {
|
|
277
|
+
let changed = false;
|
|
278
|
+
for (const run of this.tools.values()) {
|
|
279
|
+
if (!run.done && !run.left) {
|
|
280
|
+
run.left = true;
|
|
281
|
+
changed = true;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (changed) {
|
|
285
|
+
this.notify();
|
|
286
|
+
}
|
|
287
|
+
}
|
|
268
288
|
pushText(type, text) {
|
|
269
289
|
if (!text) {
|
|
270
290
|
return;
|
|
@@ -290,6 +310,7 @@ class AgentTurn {
|
|
|
290
310
|
status: "pending",
|
|
291
311
|
output: "",
|
|
292
312
|
done: false,
|
|
313
|
+
left: false,
|
|
293
314
|
complete: false,
|
|
294
315
|
terminal: "",
|
|
295
316
|
updatedAt: Date.now()
|
|
@@ -471,13 +492,15 @@ ${AgentTurn.diff(item.oldText ?? "", item.newText)}`;
|
|
|
471
492
|
}
|
|
472
493
|
|
|
473
494
|
// src/agent-session.ts
|
|
474
|
-
var
|
|
495
|
+
var STEP_PHASES = new Set(["preparing", "model_requesting"]);
|
|
475
496
|
var ABANDON_WAIT_MS = 5000;
|
|
476
497
|
var ANSWER_WAIT_MS = 15000;
|
|
477
498
|
|
|
478
499
|
class AgentSession {
|
|
479
500
|
host;
|
|
480
501
|
id;
|
|
502
|
+
static unfinishedIdleMs = 20000;
|
|
503
|
+
static quietMs = 3000;
|
|
481
504
|
turn;
|
|
482
505
|
options = [];
|
|
483
506
|
models;
|
|
@@ -486,11 +509,17 @@ class AgentSession {
|
|
|
486
509
|
return;
|
|
487
510
|
} };
|
|
488
511
|
told = new Set;
|
|
512
|
+
context;
|
|
489
513
|
queued = [];
|
|
490
514
|
quietTimer;
|
|
491
515
|
abandoned;
|
|
492
516
|
answer;
|
|
493
517
|
working = false;
|
|
518
|
+
phaseOf;
|
|
519
|
+
idleTimer;
|
|
520
|
+
retrying;
|
|
521
|
+
failure;
|
|
522
|
+
delivered = 0;
|
|
494
523
|
requests = new Set;
|
|
495
524
|
earlier = new Set;
|
|
496
525
|
constructor(host, id, setup) {
|
|
@@ -523,20 +552,17 @@ class AgentSession {
|
|
|
523
552
|
this.callbacks.title?.(this.title);
|
|
524
553
|
}
|
|
525
554
|
const phase = AgentSession.phase(meta);
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
if (phase !== "idle") {
|
|
529
|
-
this.working = true;
|
|
530
|
-
} else if (this.working) {
|
|
531
|
-
this.working = false;
|
|
532
|
-
this.turn.end("end_turn");
|
|
533
|
-
}
|
|
555
|
+
if (phase && !ids.some((id) => this.earlier.has(id)) && AgentSession.own(meta)) {
|
|
556
|
+
this.phase(this.turn, phase, meta);
|
|
534
557
|
}
|
|
535
558
|
if (meta && Object.keys(meta).some((key) => /unsolicitedTurn$/.test(key)) && !this.turn) {
|
|
536
559
|
this.startUnsolicited(false);
|
|
537
560
|
}
|
|
538
561
|
return;
|
|
539
562
|
}
|
|
563
|
+
case "usage_update":
|
|
564
|
+
this.measure(update);
|
|
565
|
+
break;
|
|
540
566
|
case "session_end":
|
|
541
567
|
if (this.turn?.unsolicited) {
|
|
542
568
|
this.turn.end("end_turn");
|
|
@@ -566,8 +592,22 @@ class AgentSession {
|
|
|
566
592
|
const turn = new AgentTurn(false, /^\/[\w:.-]+/.exec(first)?.[0], (path, before) => this.callbacks.write?.(path, before));
|
|
567
593
|
this.turn = turn;
|
|
568
594
|
this.working = false;
|
|
595
|
+
this.phaseOf = undefined;
|
|
596
|
+
this.failure = undefined;
|
|
597
|
+
this.stopWaiting();
|
|
569
598
|
this.earlier = new Set(this.requests);
|
|
570
|
-
const answer = this.host.request("session/prompt", { sessionId: this.id, prompt }).then((response) =>
|
|
599
|
+
const answer = this.host.request("session/prompt", { sessionId: this.id, prompt }).then((response) => {
|
|
600
|
+
this.answered(turn);
|
|
601
|
+
const error = response?.stopReason === "cancelled" ? undefined : AgentSession.errorOf(response?._meta);
|
|
602
|
+
if (error) {
|
|
603
|
+
turn.fail(error);
|
|
604
|
+
} else {
|
|
605
|
+
turn.end(response?.stopReason ?? "end_turn");
|
|
606
|
+
}
|
|
607
|
+
}, (error) => {
|
|
608
|
+
this.answered(turn);
|
|
609
|
+
turn.fail(error.message, error.code);
|
|
610
|
+
});
|
|
571
611
|
this.answer = answer;
|
|
572
612
|
return turn;
|
|
573
613
|
}
|
|
@@ -601,6 +641,7 @@ class AgentSession {
|
|
|
601
641
|
this.turn = undefined;
|
|
602
642
|
clearTimeout(this.quietTimer);
|
|
603
643
|
this.quietTimer = undefined;
|
|
644
|
+
this.stopWaiting();
|
|
604
645
|
}
|
|
605
646
|
}
|
|
606
647
|
cancel() {
|
|
@@ -628,6 +669,47 @@ class AgentSession {
|
|
|
628
669
|
}
|
|
629
670
|
}
|
|
630
671
|
}
|
|
672
|
+
phase(turn, phase, meta) {
|
|
673
|
+
if (phase !== "idle") {
|
|
674
|
+
this.working = true;
|
|
675
|
+
this.stopWaiting();
|
|
676
|
+
this.retried(phase === "retrying" ? AgentSession.retryOf(meta) : undefined);
|
|
677
|
+
if (STEP_PHASES.has(phase)) {
|
|
678
|
+
turn?.step();
|
|
679
|
+
}
|
|
680
|
+
} else if (this.working) {
|
|
681
|
+
this.working = false;
|
|
682
|
+
this.retried(undefined);
|
|
683
|
+
this.stopWaiting();
|
|
684
|
+
if (this.phaseOf === "model_done" && !turn?.open()) {
|
|
685
|
+
turn?.end("end_turn");
|
|
686
|
+
} else if (turn) {
|
|
687
|
+
this.idleTimer = setTimeout(() => {
|
|
688
|
+
this.idleTimer = undefined;
|
|
689
|
+
turn.end("end_turn");
|
|
690
|
+
}, AgentSession.unfinishedIdleMs);
|
|
691
|
+
this.idleTimer.unref?.();
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
this.phaseOf = phase;
|
|
695
|
+
}
|
|
696
|
+
stopWaiting() {
|
|
697
|
+
clearTimeout(this.idleTimer);
|
|
698
|
+
this.idleTimer = undefined;
|
|
699
|
+
}
|
|
700
|
+
answered(turn) {
|
|
701
|
+
if (this.turn === turn) {
|
|
702
|
+
this.stopWaiting();
|
|
703
|
+
this.retried(undefined);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
retried(retry) {
|
|
707
|
+
if (retry?.reason === this.retrying?.reason && retry?.attempt === this.retrying?.attempt && !retry === !this.retrying) {
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
this.retrying = retry;
|
|
711
|
+
this.callbacks.retry?.(retry);
|
|
712
|
+
}
|
|
631
713
|
startUnsolicited(quiet) {
|
|
632
714
|
const turn = new AgentTurn(true, undefined, (path, before) => this.callbacks.write?.(path, before));
|
|
633
715
|
this.turn = turn;
|
|
@@ -638,12 +720,75 @@ class AgentSession {
|
|
|
638
720
|
}
|
|
639
721
|
quiet(turn) {
|
|
640
722
|
clearTimeout(this.quietTimer);
|
|
641
|
-
this.quietTimer = setTimeout(() =>
|
|
723
|
+
this.quietTimer = setTimeout(() => {
|
|
724
|
+
if (turn.ended) {
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
if (this.working || this.idleTimer || turn.open()) {
|
|
728
|
+
this.quiet(turn);
|
|
729
|
+
} else {
|
|
730
|
+
turn.end("end_turn");
|
|
731
|
+
}
|
|
732
|
+
}, AgentSession.quietMs);
|
|
642
733
|
this.quietTimer.unref?.();
|
|
643
734
|
}
|
|
735
|
+
measure(update) {
|
|
736
|
+
const used = AgentSession.contextUsed(update);
|
|
737
|
+
if (used === undefined || Object.keys(update._meta ?? {}).some((key) => /parentToolCallId$/.test(key))) {
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
if (this.context !== undefined && used < this.context / 2) {
|
|
741
|
+
this.told.clear();
|
|
742
|
+
}
|
|
743
|
+
this.context = used;
|
|
744
|
+
}
|
|
745
|
+
static contextUsed(update) {
|
|
746
|
+
if (typeof update.used === "number" && update.used > 0) {
|
|
747
|
+
return update.used;
|
|
748
|
+
}
|
|
749
|
+
const tokens = update._meta?.usage;
|
|
750
|
+
const count = (value) => typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
751
|
+
const total = count(tokens?.prompt_tokens) || count(tokens?.input_tokens) + count(tokens?.cache_read_input_tokens) + count(tokens?.cache_creation_input_tokens);
|
|
752
|
+
return total > 0 ? total : undefined;
|
|
753
|
+
}
|
|
754
|
+
static own(meta) {
|
|
755
|
+
return !Object.keys(meta ?? {}).some((key) => /(parentToolCallId|memberEvent)$/.test(key));
|
|
756
|
+
}
|
|
644
757
|
static requestIds(meta) {
|
|
645
758
|
return Object.entries(meta ?? {}).filter(([key, value]) => /requestId$/i.test(key) && typeof value === "string" && value).map(([, value]) => value);
|
|
646
759
|
}
|
|
760
|
+
static retryOf(meta) {
|
|
761
|
+
for (const [key, value] of Object.entries(meta ?? {})) {
|
|
762
|
+
const retry = value?.retry;
|
|
763
|
+
if (/agentPhase$/.test(key) && retry && typeof retry === "object") {
|
|
764
|
+
return {
|
|
765
|
+
...typeof retry.reason === "string" ? { reason: retry.reason } : {},
|
|
766
|
+
...typeof retry.attempt === "number" ? { attempt: retry.attempt } : {}
|
|
767
|
+
};
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
return {};
|
|
771
|
+
}
|
|
772
|
+
static errorOf(meta) {
|
|
773
|
+
for (const [key, value] of Object.entries(meta ?? {})) {
|
|
774
|
+
if (/errorMessage$/.test(key) && typeof value === "string" && value.trim()) {
|
|
775
|
+
return AgentSession.messageOf(value.trim());
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
static messageOf(text) {
|
|
781
|
+
if (!text.startsWith("{")) {
|
|
782
|
+
return text;
|
|
783
|
+
}
|
|
784
|
+
try {
|
|
785
|
+
const value = JSON.parse(text);
|
|
786
|
+
const message = value.message ?? (typeof value.error === "string" ? value.error : value.error?.message);
|
|
787
|
+
return typeof message === "string" && message.trim() ? message.trim() : text;
|
|
788
|
+
} catch {
|
|
789
|
+
return text;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
647
792
|
static phase(meta) {
|
|
648
793
|
for (const [key, value] of Object.entries(meta ?? {})) {
|
|
649
794
|
const phase = value?.phase;
|
|
@@ -828,6 +973,18 @@ var TEXTS = {
|
|
|
828
973
|
en: (agent) => `Starting ${agent}…`,
|
|
829
974
|
zh: (agent) => `正在启动 ${agent}…`
|
|
830
975
|
},
|
|
976
|
+
refused: {
|
|
977
|
+
en: (agent) => `${agent} declined to go on with this turn.`,
|
|
978
|
+
zh: (agent) => `${agent} 拒绝继续这一轮。`
|
|
979
|
+
},
|
|
980
|
+
retrying: {
|
|
981
|
+
en: (agent, network, attempt) => `${agent}: ${network ? "the connection to its model dropped" : "a request to its model failed"}, retrying${attempt ? ` (attempt ${attempt})` : ""}…`,
|
|
982
|
+
zh: (agent, network, attempt) => `${agent}:${network ? "连接模型的网络中断" : "模型请求失败"},正在重试${attempt ? `(第 ${attempt} 次)` : ""}…`
|
|
983
|
+
},
|
|
984
|
+
dropped: {
|
|
985
|
+
en: (agent) => `${agent} went on without finishing this call.`,
|
|
986
|
+
zh: (agent) => `${agent} 没有完成这次调用就接着往下做了。`
|
|
987
|
+
},
|
|
831
988
|
unfinished: {
|
|
832
989
|
en: (agent) => `${agent} ended its turn before this call finished.`,
|
|
833
990
|
zh: (agent) => `${agent} 在这次调用完成前结束了这一轮。`
|
|
@@ -1200,6 +1357,11 @@ var AgentPrompt;
|
|
|
1200
1357
|
return result;
|
|
1201
1358
|
}
|
|
1202
1359
|
AgentPrompt.pending = pending;
|
|
1360
|
+
function resume(error) {
|
|
1361
|
+
return `Your last turn stopped with an error before it finished: ${error}
|
|
1362
|
+
Continue where you left off.`;
|
|
1363
|
+
}
|
|
1364
|
+
AgentPrompt.resume = resume;
|
|
1203
1365
|
function blocks(messages, images) {
|
|
1204
1366
|
const result = [];
|
|
1205
1367
|
for (const message of messages) {
|
|
@@ -1250,7 +1412,7 @@ ${kept}
|
|
|
1250
1412
|
const system = messages.filter((message) => message.role === "system").map((message) => [text2(message), ...sections(message)].join(`
|
|
1251
1413
|
`)).join(`
|
|
1252
1414
|
`);
|
|
1253
|
-
return [...system.matchAll(
|
|
1415
|
+
return [...system.matchAll(/^<(ta-[\w-]+)>[\s\S]*?<\/\1>$/gm)].map((match) => match[0]);
|
|
1254
1416
|
}
|
|
1255
1417
|
AgentPrompt.notes = notes;
|
|
1256
1418
|
function sections(message) {
|
|
@@ -1336,7 +1498,7 @@ var AgentTool;
|
|
|
1336
1498
|
calls.delete(toolCallId);
|
|
1337
1499
|
const details = { agent: call.agent, kind: run?.kind, title: run?.title, status: run?.status, locations: run?.locations };
|
|
1338
1500
|
if (!run?.done) {
|
|
1339
|
-
return { content: [{ type: "text", text: text("unfinished", call.agent) }], details, isError: true };
|
|
1501
|
+
return { content: [{ type: "text", text: text(run?.left ? "dropped" : "unfinished", call.agent) }], details, isError: true };
|
|
1340
1502
|
}
|
|
1341
1503
|
return { content: [{ type: "text", text: run.output || "(no output)" }], details, isError: run.status === "failed" };
|
|
1342
1504
|
}
|
|
@@ -1501,7 +1663,10 @@ class AgentStreamer {
|
|
|
1501
1663
|
async run(config, model, context, options, builder) {
|
|
1502
1664
|
const signal = options?.signal;
|
|
1503
1665
|
const { session, fresh, images, dirs } = await this.host.open(config, signal);
|
|
1504
|
-
|
|
1666
|
+
let pending = AgentPrompt.pending(context.messages);
|
|
1667
|
+
if (session.failure !== undefined) {
|
|
1668
|
+
pending = pending.filter((message) => !(message.timestamp <= session.delivered));
|
|
1669
|
+
}
|
|
1505
1670
|
let turn = session.turn;
|
|
1506
1671
|
if (turn) {
|
|
1507
1672
|
if (pending.length) {
|
|
@@ -1509,10 +1674,14 @@ class AgentStreamer {
|
|
|
1509
1674
|
}
|
|
1510
1675
|
} else {
|
|
1511
1676
|
let prompt = pending.length ? AgentPrompt.blocks(pending, images) : session.takeQueued();
|
|
1677
|
+
if (!prompt?.length && session.failure !== undefined) {
|
|
1678
|
+
prompt = [{ type: "text", text: AgentPrompt.resume(session.failure) }];
|
|
1679
|
+
}
|
|
1512
1680
|
if (!prompt?.length) {
|
|
1513
1681
|
builder.done("stop");
|
|
1514
1682
|
return;
|
|
1515
1683
|
}
|
|
1684
|
+
session.delivered = Math.max(session.delivered, ...pending.map((message) => message.timestamp ?? 0));
|
|
1516
1685
|
const earlier = fresh ? AgentPrompt.transcript(context.messages, pending.length) : undefined;
|
|
1517
1686
|
if (earlier) {
|
|
1518
1687
|
prompt = [{ type: "text", text: earlier }, ...prompt];
|
|
@@ -1555,6 +1724,9 @@ class AgentStreamer {
|
|
|
1555
1724
|
}
|
|
1556
1725
|
if (item.reason === "cancelled" && signal?.aborted) {
|
|
1557
1726
|
builder.error("aborted", "Aborted");
|
|
1727
|
+
} else if (item.reason === "refusal") {
|
|
1728
|
+
session.failure = text("refused", config.name);
|
|
1729
|
+
builder.error("error", session.failure);
|
|
1558
1730
|
} else {
|
|
1559
1731
|
if (turn.command && !turn.produced) {
|
|
1560
1732
|
builder.append("text", text("commandDone", config.name, turn.command));
|
|
@@ -1565,7 +1737,8 @@ class AgentStreamer {
|
|
|
1565
1737
|
}
|
|
1566
1738
|
case "error":
|
|
1567
1739
|
session.finished(turn);
|
|
1568
|
-
|
|
1740
|
+
session.failure = AgentStreamer.describe(config, item);
|
|
1741
|
+
builder.error("error", session.failure);
|
|
1569
1742
|
return;
|
|
1570
1743
|
}
|
|
1571
1744
|
}
|
|
@@ -1730,6 +1903,10 @@ function activate(pi, options = {}) {
|
|
|
1730
1903
|
const option = request.options.find((candidate) => candidate.name === chosen);
|
|
1731
1904
|
return option ? { outcome: "selected", optionId: option.optionId } : { outcome: "cancelled" };
|
|
1732
1905
|
},
|
|
1906
|
+
retry: (retry) => {
|
|
1907
|
+
const ui = context?.hasUI ? context.ui : undefined;
|
|
1908
|
+
ui?.setWorkingMessage(retry ? text("retrying", config.name, retry.reason === "network" ? "network" : "", retry.attempt ? String(retry.attempt) : "") : undefined);
|
|
1909
|
+
},
|
|
1733
1910
|
title: (title) => {
|
|
1734
1911
|
if (context && !pi.getSessionName()) {
|
|
1735
1912
|
pi.setSessionName(title);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-agents",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.12.0",
|
|
4
4
|
"description": "A pi package for Ta: other coding agents (CodeBuddy Code, Claude Code, Codex and any agent that speaks ACP) as models to pick, each running a conversation on its own harness",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package"
|