@celljs/ta-agents 4.12.0 → 4.13.1

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 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` 回答完就退出。
@@ -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 (!block) {
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;
@@ -267,20 +273,35 @@ class AgentTurn {
267
273
  }
268
274
  open() {
269
275
  for (const run of this.tools.values()) {
270
- if (!run.done && !run.left) {
276
+ if (!run.done && !run.left && !this.stepped.has(run.id)) {
271
277
  return true;
272
278
  }
273
279
  }
274
280
  return false;
275
281
  }
276
282
  step() {
277
- let changed = false;
278
283
  for (const run of this.tools.values()) {
279
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) {
280
300
  run.left = true;
281
301
  changed = true;
282
302
  }
283
303
  }
304
+ this.stepped.clear();
284
305
  if (changed) {
285
306
  this.notify();
286
307
  }
@@ -382,6 +403,7 @@ class AgentTurn {
382
403
  if (this.ended) {
383
404
  return;
384
405
  }
406
+ this.leave();
385
407
  this.ended = true;
386
408
  this.items.push(item);
387
409
  this.notify();
@@ -541,6 +563,9 @@ class AgentSession {
541
563
  for (const id of ids) {
542
564
  this.requests.add(id);
543
565
  }
566
+ if (AgentSession.internal(meta)) {
567
+ return;
568
+ }
544
569
  switch (update.sessionUpdate) {
545
570
  case "config_option_update":
546
571
  this.options = update.configOptions ?? this.options;
@@ -751,6 +776,9 @@ class AgentSession {
751
776
  const total = count(tokens?.prompt_tokens) || count(tokens?.input_tokens) + count(tokens?.cache_read_input_tokens) + count(tokens?.cache_creation_input_tokens);
752
777
  return total > 0 ? total : undefined;
753
778
  }
779
+ static internal(meta) {
780
+ return Object.entries(meta ?? {}).some(([key, value]) => /isCompactInternal$/.test(key) && value === true);
781
+ }
754
782
  static own(meta) {
755
783
  return !Object.keys(meta ?? {}).some((key) => /(parentToolCallId|memberEvent)$/.test(key));
756
784
  }
@@ -1412,7 +1440,7 @@ ${kept}
1412
1440
  const system = messages.filter((message) => message.role === "system").map((message) => [text2(message), ...sections(message)].join(`
1413
1441
  `)).join(`
1414
1442
  `);
1415
- return [...system.matchAll(/^<(ta-[\w-]+)>[\s\S]*?<\/\1>$/gm)].map((match) => match[0]);
1443
+ return [...system.matchAll(/^<(ta-[\w-]+|skills)>[\s\S]*?<\/\1>$/gm)].map((match) => match[0]);
1416
1444
  }
1417
1445
  AgentPrompt.notes = notes;
1418
1446
  function sections(message) {
@@ -1432,6 +1460,7 @@ ${kept}
1432
1460
  })(AgentPrompt ||= {});
1433
1461
 
1434
1462
  // src/agent-tool.ts
1463
+ var INPUT_CHARS = 8000;
1435
1464
  function relative(path, dirs) {
1436
1465
  const dir = dirs.find((candidate) => path.startsWith(`${candidate}/`) || path.startsWith(`${candidate}\\`));
1437
1466
  return dir ? path.slice(dir.length + 1) : path;
@@ -1443,30 +1472,58 @@ var AgentTool;
1443
1472
  ((AgentTool) => {
1444
1473
  AgentTool.NAME = "agent_tool";
1445
1474
  function args(run, cwd = []) {
1446
- const input = run.input;
1475
+ const input2 = run.input;
1447
1476
  const title = run.title || run.name || run.kind;
1448
1477
  let target;
1449
1478
  switch (run.kind) {
1450
1479
  case "execute":
1451
- target = str2(input.command) ?? title.replace(/^`|`$/g, "");
1480
+ target = str2(input2.command) ?? title.replace(/^`|`$/g, "");
1452
1481
  break;
1453
1482
  case "read":
1454
1483
  case "edit":
1455
1484
  case "delete":
1456
1485
  case "move":
1457
- target = run.locations[0] ?? str2(input.file_path) ?? str2(input.path) ?? str2(input.filePath);
1486
+ target = run.locations[0] ?? str2(input2.file_path) ?? str2(input2.path) ?? str2(input2.filePath);
1458
1487
  break;
1459
1488
  case "search":
1460
- target = str2(input.pattern) ?? str2(input.query);
1489
+ target = str2(input2.pattern) ?? str2(input2.query);
1461
1490
  break;
1462
1491
  case "fetch":
1463
- target = str2(input.url) ?? str2(input.query);
1492
+ target = str2(input2.url) ?? str2(input2.query);
1464
1493
  break;
1465
1494
  }
1466
- target ??= ["description", "subject", "skill", "image_path", "file_path", "path", "url", "query"].map((key) => str2(input[key])).find(Boolean);
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);
1467
1511
  return { title, kind: run.kind, target: relative(target ?? title, cwd), ...run.name ? { tool: run.name } : {} };
1468
1512
  }
1469
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;
1470
1527
  function definition(calls) {
1471
1528
  return {
1472
1529
  name: AgentTool.NAME,
@@ -1496,7 +1553,14 @@ var AgentTool;
1496
1553
  throw new Error("Cancelled");
1497
1554
  }
1498
1555
  calls.delete(toolCallId);
1499
- const details = { agent: call.agent, kind: run?.kind, title: run?.title, status: run?.status, locations: run?.locations };
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
+ };
1500
1564
  if (!run?.done) {
1501
1565
  return { content: [{ type: "text", text: text(run?.left ? "dropped" : "unfinished", call.agent) }], details, isError: true };
1502
1566
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-agents",
3
- "version": "4.12.0",
3
+ "version": "4.13.1",
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"