@celljs/ta-agents 4.11.0 → 4.13.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/README.en.md +1 -1
- package/README.md +1 -1
- package/extension/index.mjs +266 -25
- package/package.json +1 -1
package/README.en.md
CHANGED
|
@@ -45,7 +45,7 @@ Fields: `name`, `command`, `args`, `env`, `login` (the sign-in command to tell t
|
|
|
45
45
|
|
|
46
46
|
## How it works
|
|
47
47
|
|
|
48
|
-
- **A turn**: each time pi asks the model, only what the user said since the agent last answered goes to the agent (`session/prompt`), since the agent keeps its own history. The agent's text and thinking stream as pi's message.
|
|
48
|
+
- **A turn**: each time pi asks the model, only what the user said since the agent last answered goes to the agent (`session/prompt`), since the agent keeps its own history. The agent keeps its own system prompt too; the first prompt of each of its sessions (and the first after it compacted its context) starts with what it needs of pi's: the `<ta-…>` notes of Ta's packages and pi's `<skills>`, the skills of Ta, its packages and the user with the files to read, which the agent's own skill tool does not know. The agent's text and thinking stream as pi's message.
|
|
49
49
|
- **Tool calls**: the agent uses its own tools; each call is recorded in pi as a call of `agent_tool` (its arguments are a title, a kind and a target, such as `{ kind: "execute", target: "npm test" }`), so Ta shows it as a line like its own tools, with the agent's output as the result. As soon as a call of kind edit, delete or move names the file it changes (`locations`, or `file_path` and the like in its arguments), or brings an ACP diff, the extension emits `ta:file-write` on `pi.events` (`{ path, before? }`, `TA_FILE_WRITE_EVENT` of ta-protocol), so that ta-changes keeps the file as it was before the agent writes it. When the agent starts a call, the assistant message ends (`toolUse`); pi "running" `agent_tool` only waits for the agent to finish the call, and pi's next request goes on with the same turn. some agents (CodeBuddy Code) stream the arguments first, so a call shows once the agent says they are complete (`toolArgumentsComplete` in `_meta`, or announcing the call again as `pending` with them); without such a sign, 1.5 seconds without news of the call, or the agent saying something else, will do; calls started together go in one message and show in parallel. `agent_tool` is active only while the model is an agent.
|
|
50
50
|
- **Sessions**: a pi session uses one session of each agent; its id is kept in the pi session (the custom entry `ta-agents.session`), and reopening the conversation loads it again (`session/load`). A fork, or going back to an earlier message in pi (`/tree`), starts a new session of the agent on the next turn and hands it the conversation so far as text; so does switching to an agent in the middle of a conversation (30,000 characters at most).
|
|
51
51
|
- **Processes**: the sessions of one agent in one directory share a process; by directory, because CodeBuddy Code runs commands where it was started rather than in the directory of the session. CodeBuddy Code itself runs a process for each conversation (about 650 MB), stopped when the conversation closes: a process of it answers only the session it opened or loaded last, and a prompt of another one waits forever, or ends at once with nothing while that one works. A process stops after 10 minutes without a turn, and starts again, loading its sessions, when it is needed. It keeps pi's process alive only while a request waits for its answer, so `pi -p` exits once it answered.
|
package/README.md
CHANGED
|
@@ -45,7 +45,7 @@ CodeBuddy Code 和在它自己的终端里一样:用 PTC(代码模式,工
|
|
|
45
45
|
|
|
46
46
|
## 机制
|
|
47
47
|
|
|
48
|
-
- **一轮怎么走**:pi 每次请求模型时,只把用户自上次回答以来说的话发给 Agent(`session/prompt`),因为 Agent 自己保存历史。Agent 的文字和思考按流式转成 pi 的消息。
|
|
48
|
+
- **一轮怎么走**:pi 每次请求模型时,只把用户自上次回答以来说的话发给 Agent(`session/prompt`),因为 Agent 自己保存历史。Agent 也用它自己的系统提示;它每个会话的第一条提示(以及压缩上下文后的第一条)前面带上 pi 系统提示里它需要的部分:Ta 各个包的 `<ta-…>` 说明,和 pi 的 `<skills>`(Ta、各个包和用户的技能及其文件路径,Agent 自己的技能工具不认识它们)。Agent 的文字和思考按流式转成 pi 的消息。
|
|
49
49
|
- **工具调用**:Agent 用自己的工具,每次调用在 pi 里记成一次 `agent_tool` 工具调用(参数是标题、种类和对象,例如 `{ kind: "execute", target: "npm test" }`),所以 Ta 显示成和自己的工具一样的一行,结果就是 Agent 工具的输出。种类是 edit、delete、move 的调用一说出要改的文件(`locations` 或参数里的 `file_path` 等),或者带来 ACP diff,扩展就经 `pi.events` 发 `ta:file-write`(`{ path, before? }`,见 ta-protocol 的 `TA_FILE_WRITE_EVENT`),让 ta-changes 在 Agent 动手前留下文件原样。具体做法:Agent 开始一次调用时,结束当前这条 assistant 消息(`toolUse`);pi “执行”`agent_tool` 时只是等 Agent 把这次调用做完;pi 下一次请求时接着流同一轮。有的 Agent(CodeBuddy Code)先流式发参数,等它说参数齐了(`_meta` 的 `toolArgumentsComplete`,或带着参数以 `pending` 再报一次这次调用)才显示;没有这类信号时,1.5 秒没有新消息或 Agent 开始说别的,也算齐了;同时开始的几次调用放进同一条消息,并行显示。`agent_tool` 只在当前模型是 Agent 时启用。
|
|
50
50
|
- **会话**:一个 pi 会话对一个 Agent 用一个 Agent 会话,它的 id 记在 pi 会话里(自定义条目 `ta-agents.session`),重新打开对话时用 `session/load` 接上。分叉,或在 pi 里回退到之前的消息(`/tree`),都会让下一轮新开一个 Agent 会话,并把到此为止的对话作为文字交给它;中途换到 Agent 时也一样(最多 3 万字)。
|
|
51
51
|
- **进程**:同一个 Agent 在同一个目录下的所有会话共用一个进程;按目录分开,是因为 CodeBuddy Code 在启动目录里执行命令,不看会话的目录。CodeBuddy Code 例外,每个对话单独一个进程(约 650 MB),对话关闭时停掉:它的一个进程只回答最后新建或加载的会话,其他会话发消息会一直等不到回复,或在那个会话忙时立刻空着结束。10 分钟没有任何一轮就停掉进程,下次用时再启动并加载会话。只在有请求等待回复时保持 pi 进程不退出,所以 `pi -p` 回答完就退出。
|
package/extension/index.mjs
CHANGED
|
@@ -9,7 +9,10 @@ var Acp;
|
|
|
9
9
|
Acp.PROTOCOL_VERSION = 1;
|
|
10
10
|
Acp.AUTH_REQUIRED = -32000;
|
|
11
11
|
function text(block) {
|
|
12
|
-
if (
|
|
12
|
+
if (Array.isArray(block)) {
|
|
13
|
+
return block.map((item) => text(item)).join("");
|
|
14
|
+
}
|
|
15
|
+
if (!block?.type) {
|
|
13
16
|
return "";
|
|
14
17
|
}
|
|
15
18
|
switch (block.type) {
|
|
@@ -181,6 +184,7 @@ class AgentTurn {
|
|
|
181
184
|
unsolicited;
|
|
182
185
|
command;
|
|
183
186
|
onWrite;
|
|
187
|
+
static stepGraceMs = 1000;
|
|
184
188
|
tools = new Map;
|
|
185
189
|
usage = {};
|
|
186
190
|
ended = false;
|
|
@@ -188,6 +192,8 @@ class AgentTurn {
|
|
|
188
192
|
items = [];
|
|
189
193
|
listeners = new Set;
|
|
190
194
|
written = new Set;
|
|
195
|
+
stepped = new Set;
|
|
196
|
+
stepTimer;
|
|
191
197
|
constructor(unsolicited = false, command, onWrite) {
|
|
192
198
|
this.unsolicited = unsolicited;
|
|
193
199
|
this.command = command;
|
|
@@ -246,7 +252,7 @@ class AgentTurn {
|
|
|
246
252
|
async settle(id, quietMs, signal) {
|
|
247
253
|
for (;; ) {
|
|
248
254
|
const run = this.tools.get(id);
|
|
249
|
-
if (!run || run.complete || this.ended || signal?.aborted || this.items.some((item) => item.type !== "tool")) {
|
|
255
|
+
if (!run || run.complete || run.left || this.ended || signal?.aborted || this.items.some((item) => item.type !== "tool")) {
|
|
250
256
|
return;
|
|
251
257
|
}
|
|
252
258
|
const left = run.updatedAt + quietMs - Date.now();
|
|
@@ -259,12 +265,47 @@ class AgentTurn {
|
|
|
259
265
|
async waitTool(id, signal) {
|
|
260
266
|
for (;; ) {
|
|
261
267
|
const run = this.tools.get(id);
|
|
262
|
-
if (!run || run.done || this.ended || signal?.aborted) {
|
|
268
|
+
if (!run || run.done || run.left || this.ended || signal?.aborted) {
|
|
263
269
|
return run;
|
|
264
270
|
}
|
|
265
271
|
await this.changed(signal);
|
|
266
272
|
}
|
|
267
273
|
}
|
|
274
|
+
open() {
|
|
275
|
+
for (const run of this.tools.values()) {
|
|
276
|
+
if (!run.done && !run.left && !this.stepped.has(run.id)) {
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
step() {
|
|
283
|
+
for (const run of this.tools.values()) {
|
|
284
|
+
if (!run.done && !run.left) {
|
|
285
|
+
this.stepped.add(run.id);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (this.stepped.size && !this.stepTimer) {
|
|
289
|
+
this.stepTimer = setTimeout(() => this.leave(), AgentTurn.stepGraceMs);
|
|
290
|
+
this.stepTimer.unref?.();
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
leave() {
|
|
294
|
+
clearTimeout(this.stepTimer);
|
|
295
|
+
this.stepTimer = undefined;
|
|
296
|
+
let changed = false;
|
|
297
|
+
for (const id of this.stepped) {
|
|
298
|
+
const run = this.tools.get(id);
|
|
299
|
+
if (run && !run.done && !run.left) {
|
|
300
|
+
run.left = true;
|
|
301
|
+
changed = true;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
this.stepped.clear();
|
|
305
|
+
if (changed) {
|
|
306
|
+
this.notify();
|
|
307
|
+
}
|
|
308
|
+
}
|
|
268
309
|
pushText(type, text) {
|
|
269
310
|
if (!text) {
|
|
270
311
|
return;
|
|
@@ -290,6 +331,7 @@ class AgentTurn {
|
|
|
290
331
|
status: "pending",
|
|
291
332
|
output: "",
|
|
292
333
|
done: false,
|
|
334
|
+
left: false,
|
|
293
335
|
complete: false,
|
|
294
336
|
terminal: "",
|
|
295
337
|
updatedAt: Date.now()
|
|
@@ -361,6 +403,7 @@ class AgentTurn {
|
|
|
361
403
|
if (this.ended) {
|
|
362
404
|
return;
|
|
363
405
|
}
|
|
406
|
+
this.leave();
|
|
364
407
|
this.ended = true;
|
|
365
408
|
this.items.push(item);
|
|
366
409
|
this.notify();
|
|
@@ -471,13 +514,15 @@ ${AgentTurn.diff(item.oldText ?? "", item.newText)}`;
|
|
|
471
514
|
}
|
|
472
515
|
|
|
473
516
|
// src/agent-session.ts
|
|
474
|
-
var
|
|
517
|
+
var STEP_PHASES = new Set(["preparing", "model_requesting"]);
|
|
475
518
|
var ABANDON_WAIT_MS = 5000;
|
|
476
519
|
var ANSWER_WAIT_MS = 15000;
|
|
477
520
|
|
|
478
521
|
class AgentSession {
|
|
479
522
|
host;
|
|
480
523
|
id;
|
|
524
|
+
static unfinishedIdleMs = 20000;
|
|
525
|
+
static quietMs = 3000;
|
|
481
526
|
turn;
|
|
482
527
|
options = [];
|
|
483
528
|
models;
|
|
@@ -486,11 +531,17 @@ class AgentSession {
|
|
|
486
531
|
return;
|
|
487
532
|
} };
|
|
488
533
|
told = new Set;
|
|
534
|
+
context;
|
|
489
535
|
queued = [];
|
|
490
536
|
quietTimer;
|
|
491
537
|
abandoned;
|
|
492
538
|
answer;
|
|
493
539
|
working = false;
|
|
540
|
+
phaseOf;
|
|
541
|
+
idleTimer;
|
|
542
|
+
retrying;
|
|
543
|
+
failure;
|
|
544
|
+
delivered = 0;
|
|
494
545
|
requests = new Set;
|
|
495
546
|
earlier = new Set;
|
|
496
547
|
constructor(host, id, setup) {
|
|
@@ -512,6 +563,9 @@ class AgentSession {
|
|
|
512
563
|
for (const id of ids) {
|
|
513
564
|
this.requests.add(id);
|
|
514
565
|
}
|
|
566
|
+
if (AgentSession.internal(meta)) {
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
515
569
|
switch (update.sessionUpdate) {
|
|
516
570
|
case "config_option_update":
|
|
517
571
|
this.options = update.configOptions ?? this.options;
|
|
@@ -523,20 +577,17 @@ class AgentSession {
|
|
|
523
577
|
this.callbacks.title?.(this.title);
|
|
524
578
|
}
|
|
525
579
|
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
|
-
}
|
|
580
|
+
if (phase && !ids.some((id) => this.earlier.has(id)) && AgentSession.own(meta)) {
|
|
581
|
+
this.phase(this.turn, phase, meta);
|
|
534
582
|
}
|
|
535
583
|
if (meta && Object.keys(meta).some((key) => /unsolicitedTurn$/.test(key)) && !this.turn) {
|
|
536
584
|
this.startUnsolicited(false);
|
|
537
585
|
}
|
|
538
586
|
return;
|
|
539
587
|
}
|
|
588
|
+
case "usage_update":
|
|
589
|
+
this.measure(update);
|
|
590
|
+
break;
|
|
540
591
|
case "session_end":
|
|
541
592
|
if (this.turn?.unsolicited) {
|
|
542
593
|
this.turn.end("end_turn");
|
|
@@ -566,8 +617,22 @@ class AgentSession {
|
|
|
566
617
|
const turn = new AgentTurn(false, /^\/[\w:.-]+/.exec(first)?.[0], (path, before) => this.callbacks.write?.(path, before));
|
|
567
618
|
this.turn = turn;
|
|
568
619
|
this.working = false;
|
|
620
|
+
this.phaseOf = undefined;
|
|
621
|
+
this.failure = undefined;
|
|
622
|
+
this.stopWaiting();
|
|
569
623
|
this.earlier = new Set(this.requests);
|
|
570
|
-
const answer = this.host.request("session/prompt", { sessionId: this.id, prompt }).then((response) =>
|
|
624
|
+
const answer = this.host.request("session/prompt", { sessionId: this.id, prompt }).then((response) => {
|
|
625
|
+
this.answered(turn);
|
|
626
|
+
const error = response?.stopReason === "cancelled" ? undefined : AgentSession.errorOf(response?._meta);
|
|
627
|
+
if (error) {
|
|
628
|
+
turn.fail(error);
|
|
629
|
+
} else {
|
|
630
|
+
turn.end(response?.stopReason ?? "end_turn");
|
|
631
|
+
}
|
|
632
|
+
}, (error) => {
|
|
633
|
+
this.answered(turn);
|
|
634
|
+
turn.fail(error.message, error.code);
|
|
635
|
+
});
|
|
571
636
|
this.answer = answer;
|
|
572
637
|
return turn;
|
|
573
638
|
}
|
|
@@ -601,6 +666,7 @@ class AgentSession {
|
|
|
601
666
|
this.turn = undefined;
|
|
602
667
|
clearTimeout(this.quietTimer);
|
|
603
668
|
this.quietTimer = undefined;
|
|
669
|
+
this.stopWaiting();
|
|
604
670
|
}
|
|
605
671
|
}
|
|
606
672
|
cancel() {
|
|
@@ -628,6 +694,47 @@ class AgentSession {
|
|
|
628
694
|
}
|
|
629
695
|
}
|
|
630
696
|
}
|
|
697
|
+
phase(turn, phase, meta) {
|
|
698
|
+
if (phase !== "idle") {
|
|
699
|
+
this.working = true;
|
|
700
|
+
this.stopWaiting();
|
|
701
|
+
this.retried(phase === "retrying" ? AgentSession.retryOf(meta) : undefined);
|
|
702
|
+
if (STEP_PHASES.has(phase)) {
|
|
703
|
+
turn?.step();
|
|
704
|
+
}
|
|
705
|
+
} else if (this.working) {
|
|
706
|
+
this.working = false;
|
|
707
|
+
this.retried(undefined);
|
|
708
|
+
this.stopWaiting();
|
|
709
|
+
if (this.phaseOf === "model_done" && !turn?.open()) {
|
|
710
|
+
turn?.end("end_turn");
|
|
711
|
+
} else if (turn) {
|
|
712
|
+
this.idleTimer = setTimeout(() => {
|
|
713
|
+
this.idleTimer = undefined;
|
|
714
|
+
turn.end("end_turn");
|
|
715
|
+
}, AgentSession.unfinishedIdleMs);
|
|
716
|
+
this.idleTimer.unref?.();
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
this.phaseOf = phase;
|
|
720
|
+
}
|
|
721
|
+
stopWaiting() {
|
|
722
|
+
clearTimeout(this.idleTimer);
|
|
723
|
+
this.idleTimer = undefined;
|
|
724
|
+
}
|
|
725
|
+
answered(turn) {
|
|
726
|
+
if (this.turn === turn) {
|
|
727
|
+
this.stopWaiting();
|
|
728
|
+
this.retried(undefined);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
retried(retry) {
|
|
732
|
+
if (retry?.reason === this.retrying?.reason && retry?.attempt === this.retrying?.attempt && !retry === !this.retrying) {
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
this.retrying = retry;
|
|
736
|
+
this.callbacks.retry?.(retry);
|
|
737
|
+
}
|
|
631
738
|
startUnsolicited(quiet) {
|
|
632
739
|
const turn = new AgentTurn(true, undefined, (path, before) => this.callbacks.write?.(path, before));
|
|
633
740
|
this.turn = turn;
|
|
@@ -638,12 +745,78 @@ class AgentSession {
|
|
|
638
745
|
}
|
|
639
746
|
quiet(turn) {
|
|
640
747
|
clearTimeout(this.quietTimer);
|
|
641
|
-
this.quietTimer = setTimeout(() =>
|
|
748
|
+
this.quietTimer = setTimeout(() => {
|
|
749
|
+
if (turn.ended) {
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
if (this.working || this.idleTimer || turn.open()) {
|
|
753
|
+
this.quiet(turn);
|
|
754
|
+
} else {
|
|
755
|
+
turn.end("end_turn");
|
|
756
|
+
}
|
|
757
|
+
}, AgentSession.quietMs);
|
|
642
758
|
this.quietTimer.unref?.();
|
|
643
759
|
}
|
|
760
|
+
measure(update) {
|
|
761
|
+
const used = AgentSession.contextUsed(update);
|
|
762
|
+
if (used === undefined || Object.keys(update._meta ?? {}).some((key) => /parentToolCallId$/.test(key))) {
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
if (this.context !== undefined && used < this.context / 2) {
|
|
766
|
+
this.told.clear();
|
|
767
|
+
}
|
|
768
|
+
this.context = used;
|
|
769
|
+
}
|
|
770
|
+
static contextUsed(update) {
|
|
771
|
+
if (typeof update.used === "number" && update.used > 0) {
|
|
772
|
+
return update.used;
|
|
773
|
+
}
|
|
774
|
+
const tokens = update._meta?.usage;
|
|
775
|
+
const count = (value) => typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
776
|
+
const total = count(tokens?.prompt_tokens) || count(tokens?.input_tokens) + count(tokens?.cache_read_input_tokens) + count(tokens?.cache_creation_input_tokens);
|
|
777
|
+
return total > 0 ? total : undefined;
|
|
778
|
+
}
|
|
779
|
+
static internal(meta) {
|
|
780
|
+
return Object.entries(meta ?? {}).some(([key, value]) => /isCompactInternal$/.test(key) && value === true);
|
|
781
|
+
}
|
|
782
|
+
static own(meta) {
|
|
783
|
+
return !Object.keys(meta ?? {}).some((key) => /(parentToolCallId|memberEvent)$/.test(key));
|
|
784
|
+
}
|
|
644
785
|
static requestIds(meta) {
|
|
645
786
|
return Object.entries(meta ?? {}).filter(([key, value]) => /requestId$/i.test(key) && typeof value === "string" && value).map(([, value]) => value);
|
|
646
787
|
}
|
|
788
|
+
static retryOf(meta) {
|
|
789
|
+
for (const [key, value] of Object.entries(meta ?? {})) {
|
|
790
|
+
const retry = value?.retry;
|
|
791
|
+
if (/agentPhase$/.test(key) && retry && typeof retry === "object") {
|
|
792
|
+
return {
|
|
793
|
+
...typeof retry.reason === "string" ? { reason: retry.reason } : {},
|
|
794
|
+
...typeof retry.attempt === "number" ? { attempt: retry.attempt } : {}
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
return {};
|
|
799
|
+
}
|
|
800
|
+
static errorOf(meta) {
|
|
801
|
+
for (const [key, value] of Object.entries(meta ?? {})) {
|
|
802
|
+
if (/errorMessage$/.test(key) && typeof value === "string" && value.trim()) {
|
|
803
|
+
return AgentSession.messageOf(value.trim());
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
return;
|
|
807
|
+
}
|
|
808
|
+
static messageOf(text) {
|
|
809
|
+
if (!text.startsWith("{")) {
|
|
810
|
+
return text;
|
|
811
|
+
}
|
|
812
|
+
try {
|
|
813
|
+
const value = JSON.parse(text);
|
|
814
|
+
const message = value.message ?? (typeof value.error === "string" ? value.error : value.error?.message);
|
|
815
|
+
return typeof message === "string" && message.trim() ? message.trim() : text;
|
|
816
|
+
} catch {
|
|
817
|
+
return text;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
647
820
|
static phase(meta) {
|
|
648
821
|
for (const [key, value] of Object.entries(meta ?? {})) {
|
|
649
822
|
const phase = value?.phase;
|
|
@@ -828,6 +1001,18 @@ var TEXTS = {
|
|
|
828
1001
|
en: (agent) => `Starting ${agent}…`,
|
|
829
1002
|
zh: (agent) => `正在启动 ${agent}…`
|
|
830
1003
|
},
|
|
1004
|
+
refused: {
|
|
1005
|
+
en: (agent) => `${agent} declined to go on with this turn.`,
|
|
1006
|
+
zh: (agent) => `${agent} 拒绝继续这一轮。`
|
|
1007
|
+
},
|
|
1008
|
+
retrying: {
|
|
1009
|
+
en: (agent, network, attempt) => `${agent}: ${network ? "the connection to its model dropped" : "a request to its model failed"}, retrying${attempt ? ` (attempt ${attempt})` : ""}…`,
|
|
1010
|
+
zh: (agent, network, attempt) => `${agent}:${network ? "连接模型的网络中断" : "模型请求失败"},正在重试${attempt ? `(第 ${attempt} 次)` : ""}…`
|
|
1011
|
+
},
|
|
1012
|
+
dropped: {
|
|
1013
|
+
en: (agent) => `${agent} went on without finishing this call.`,
|
|
1014
|
+
zh: (agent) => `${agent} 没有完成这次调用就接着往下做了。`
|
|
1015
|
+
},
|
|
831
1016
|
unfinished: {
|
|
832
1017
|
en: (agent) => `${agent} ended its turn before this call finished.`,
|
|
833
1018
|
zh: (agent) => `${agent} 在这次调用完成前结束了这一轮。`
|
|
@@ -1200,6 +1385,11 @@ var AgentPrompt;
|
|
|
1200
1385
|
return result;
|
|
1201
1386
|
}
|
|
1202
1387
|
AgentPrompt.pending = pending;
|
|
1388
|
+
function resume(error) {
|
|
1389
|
+
return `Your last turn stopped with an error before it finished: ${error}
|
|
1390
|
+
Continue where you left off.`;
|
|
1391
|
+
}
|
|
1392
|
+
AgentPrompt.resume = resume;
|
|
1203
1393
|
function blocks(messages, images) {
|
|
1204
1394
|
const result = [];
|
|
1205
1395
|
for (const message of messages) {
|
|
@@ -1250,7 +1440,7 @@ ${kept}
|
|
|
1250
1440
|
const system = messages.filter((message) => message.role === "system").map((message) => [text2(message), ...sections(message)].join(`
|
|
1251
1441
|
`)).join(`
|
|
1252
1442
|
`);
|
|
1253
|
-
return [...system.matchAll(
|
|
1443
|
+
return [...system.matchAll(/^<(ta-[\w-]+|skills)>[\s\S]*?<\/\1>$/gm)].map((match) => match[0]);
|
|
1254
1444
|
}
|
|
1255
1445
|
AgentPrompt.notes = notes;
|
|
1256
1446
|
function sections(message) {
|
|
@@ -1270,6 +1460,7 @@ ${kept}
|
|
|
1270
1460
|
})(AgentPrompt ||= {});
|
|
1271
1461
|
|
|
1272
1462
|
// src/agent-tool.ts
|
|
1463
|
+
var INPUT_CHARS = 8000;
|
|
1273
1464
|
function relative(path, dirs) {
|
|
1274
1465
|
const dir = dirs.find((candidate) => path.startsWith(`${candidate}/`) || path.startsWith(`${candidate}\\`));
|
|
1275
1466
|
return dir ? path.slice(dir.length + 1) : path;
|
|
@@ -1281,30 +1472,58 @@ var AgentTool;
|
|
|
1281
1472
|
((AgentTool) => {
|
|
1282
1473
|
AgentTool.NAME = "agent_tool";
|
|
1283
1474
|
function args(run, cwd = []) {
|
|
1284
|
-
const
|
|
1475
|
+
const input2 = run.input;
|
|
1285
1476
|
const title = run.title || run.name || run.kind;
|
|
1286
1477
|
let target;
|
|
1287
1478
|
switch (run.kind) {
|
|
1288
1479
|
case "execute":
|
|
1289
|
-
target = str2(
|
|
1480
|
+
target = str2(input2.command) ?? title.replace(/^`|`$/g, "");
|
|
1290
1481
|
break;
|
|
1291
1482
|
case "read":
|
|
1292
1483
|
case "edit":
|
|
1293
1484
|
case "delete":
|
|
1294
1485
|
case "move":
|
|
1295
|
-
target = run.locations[0] ?? str2(
|
|
1486
|
+
target = run.locations[0] ?? str2(input2.file_path) ?? str2(input2.path) ?? str2(input2.filePath);
|
|
1296
1487
|
break;
|
|
1297
1488
|
case "search":
|
|
1298
|
-
target = str2(
|
|
1489
|
+
target = str2(input2.pattern) ?? str2(input2.query);
|
|
1299
1490
|
break;
|
|
1300
1491
|
case "fetch":
|
|
1301
|
-
target = str2(
|
|
1492
|
+
target = str2(input2.url) ?? str2(input2.query);
|
|
1302
1493
|
break;
|
|
1303
1494
|
}
|
|
1304
|
-
target ??= [
|
|
1495
|
+
target ??= [
|
|
1496
|
+
"description",
|
|
1497
|
+
"subject",
|
|
1498
|
+
"summary",
|
|
1499
|
+
"skill",
|
|
1500
|
+
"image_path",
|
|
1501
|
+
"file_path",
|
|
1502
|
+
"filePath",
|
|
1503
|
+
"path",
|
|
1504
|
+
"url",
|
|
1505
|
+
"query",
|
|
1506
|
+
"recipient",
|
|
1507
|
+
"prompt",
|
|
1508
|
+
"task_id",
|
|
1509
|
+
"id"
|
|
1510
|
+
].map((key) => str2(input2[key])).find(Boolean);
|
|
1305
1511
|
return { title, kind: run.kind, target: relative(target ?? title, cwd), ...run.name ? { tool: run.name } : {} };
|
|
1306
1512
|
}
|
|
1307
1513
|
AgentTool.args = args;
|
|
1514
|
+
function input(value) {
|
|
1515
|
+
const result = {};
|
|
1516
|
+
for (const [key, item] of Object.entries(value ?? {})) {
|
|
1517
|
+
if (typeof item === "string") {
|
|
1518
|
+
result[key] = item.length > INPUT_CHARS ? `${item.slice(0, INPUT_CHARS)}
|
|
1519
|
+
…` : item;
|
|
1520
|
+
} else if (typeof item === "number" || typeof item === "boolean") {
|
|
1521
|
+
result[key] = item;
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
return result;
|
|
1525
|
+
}
|
|
1526
|
+
AgentTool.input = input;
|
|
1308
1527
|
function definition(calls) {
|
|
1309
1528
|
return {
|
|
1310
1529
|
name: AgentTool.NAME,
|
|
@@ -1334,9 +1553,16 @@ var AgentTool;
|
|
|
1334
1553
|
throw new Error("Cancelled");
|
|
1335
1554
|
}
|
|
1336
1555
|
calls.delete(toolCallId);
|
|
1337
|
-
const details = {
|
|
1556
|
+
const details = {
|
|
1557
|
+
agent: call.agent,
|
|
1558
|
+
kind: run?.kind,
|
|
1559
|
+
title: run?.title,
|
|
1560
|
+
status: run?.status,
|
|
1561
|
+
locations: run?.locations,
|
|
1562
|
+
input: AgentTool.input(run?.input)
|
|
1563
|
+
};
|
|
1338
1564
|
if (!run?.done) {
|
|
1339
|
-
return { content: [{ type: "text", text: text("unfinished", call.agent) }], details, isError: true };
|
|
1565
|
+
return { content: [{ type: "text", text: text(run?.left ? "dropped" : "unfinished", call.agent) }], details, isError: true };
|
|
1340
1566
|
}
|
|
1341
1567
|
return { content: [{ type: "text", text: run.output || "(no output)" }], details, isError: run.status === "failed" };
|
|
1342
1568
|
}
|
|
@@ -1501,7 +1727,10 @@ class AgentStreamer {
|
|
|
1501
1727
|
async run(config, model, context, options, builder) {
|
|
1502
1728
|
const signal = options?.signal;
|
|
1503
1729
|
const { session, fresh, images, dirs } = await this.host.open(config, signal);
|
|
1504
|
-
|
|
1730
|
+
let pending = AgentPrompt.pending(context.messages);
|
|
1731
|
+
if (session.failure !== undefined) {
|
|
1732
|
+
pending = pending.filter((message) => !(message.timestamp <= session.delivered));
|
|
1733
|
+
}
|
|
1505
1734
|
let turn = session.turn;
|
|
1506
1735
|
if (turn) {
|
|
1507
1736
|
if (pending.length) {
|
|
@@ -1509,10 +1738,14 @@ class AgentStreamer {
|
|
|
1509
1738
|
}
|
|
1510
1739
|
} else {
|
|
1511
1740
|
let prompt = pending.length ? AgentPrompt.blocks(pending, images) : session.takeQueued();
|
|
1741
|
+
if (!prompt?.length && session.failure !== undefined) {
|
|
1742
|
+
prompt = [{ type: "text", text: AgentPrompt.resume(session.failure) }];
|
|
1743
|
+
}
|
|
1512
1744
|
if (!prompt?.length) {
|
|
1513
1745
|
builder.done("stop");
|
|
1514
1746
|
return;
|
|
1515
1747
|
}
|
|
1748
|
+
session.delivered = Math.max(session.delivered, ...pending.map((message) => message.timestamp ?? 0));
|
|
1516
1749
|
const earlier = fresh ? AgentPrompt.transcript(context.messages, pending.length) : undefined;
|
|
1517
1750
|
if (earlier) {
|
|
1518
1751
|
prompt = [{ type: "text", text: earlier }, ...prompt];
|
|
@@ -1555,6 +1788,9 @@ class AgentStreamer {
|
|
|
1555
1788
|
}
|
|
1556
1789
|
if (item.reason === "cancelled" && signal?.aborted) {
|
|
1557
1790
|
builder.error("aborted", "Aborted");
|
|
1791
|
+
} else if (item.reason === "refusal") {
|
|
1792
|
+
session.failure = text("refused", config.name);
|
|
1793
|
+
builder.error("error", session.failure);
|
|
1558
1794
|
} else {
|
|
1559
1795
|
if (turn.command && !turn.produced) {
|
|
1560
1796
|
builder.append("text", text("commandDone", config.name, turn.command));
|
|
@@ -1565,7 +1801,8 @@ class AgentStreamer {
|
|
|
1565
1801
|
}
|
|
1566
1802
|
case "error":
|
|
1567
1803
|
session.finished(turn);
|
|
1568
|
-
|
|
1804
|
+
session.failure = AgentStreamer.describe(config, item);
|
|
1805
|
+
builder.error("error", session.failure);
|
|
1569
1806
|
return;
|
|
1570
1807
|
}
|
|
1571
1808
|
}
|
|
@@ -1730,6 +1967,10 @@ function activate(pi, options = {}) {
|
|
|
1730
1967
|
const option = request.options.find((candidate) => candidate.name === chosen);
|
|
1731
1968
|
return option ? { outcome: "selected", optionId: option.optionId } : { outcome: "cancelled" };
|
|
1732
1969
|
},
|
|
1970
|
+
retry: (retry) => {
|
|
1971
|
+
const ui = context?.hasUI ? context.ui : undefined;
|
|
1972
|
+
ui?.setWorkingMessage(retry ? text("retrying", config.name, retry.reason === "network" ? "network" : "", retry.attempt ? String(retry.attempt) : "") : undefined);
|
|
1973
|
+
},
|
|
1733
1974
|
title: (title) => {
|
|
1734
1975
|
if (context && !pi.getSessionName()) {
|
|
1735
1976
|
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.13.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"
|