@celljs/ta-agents 4.9.1 → 4.11.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.
Files changed (2) hide show
  1. package/extension/index.mjs +185 -140
  2. package/package.json +3 -3
@@ -36,12 +36,136 @@ var Acp;
36
36
 
37
37
  // src/agent-bindings.ts
38
38
  import { realpathSync } from "node:fs";
39
- import { homedir } from "node:os";
40
- import { join, sep } from "node:path";
39
+ import { homedir as homedir2 } from "node:os";
40
+ import { join as join2, sep } from "node:path";
41
41
 
42
42
  // src/acp-agent.ts
43
43
  import { spawn } from "node:child_process";
44
44
 
45
+ // src/agent-catalog.ts
46
+ import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
47
+ import { homedir } from "node:os";
48
+ import { delimiter, dirname, join } from "node:path";
49
+ var PRESETS = [
50
+ {
51
+ id: "codebuddy-code",
52
+ name: "CodeBuddy Code",
53
+ command: "cbc",
54
+ args: ["--acp"],
55
+ programs: ["cbc", "codebuddy"],
56
+ login: "cbc",
57
+ env: { CODEBUDDY_CODE_MODE_ENABLED: "1" },
58
+ clientMeta: { "codebuddy.ai": { mainAgentSupport: true, toolModeSupport: true } },
59
+ options: { mode: "fullAccess" },
60
+ processPerSession: true
61
+ },
62
+ {
63
+ id: "claude-code",
64
+ name: "Claude Code",
65
+ command: "npx",
66
+ args: ["-y", "@agentclientprotocol/claude-agent-acp@0.106.0"],
67
+ programs: ["claude"],
68
+ login: "claude"
69
+ },
70
+ { id: "codex", name: "Codex", command: "npx", args: ["-y", "@agentclientprotocol/codex-acp@2.6.0"], programs: ["codex"], login: "codex login" }
71
+ ];
72
+ var AgentCatalog;
73
+ ((AgentCatalog) => {
74
+ AgentCatalog.DEFAULT_MODEL = "default";
75
+ function agents(settings, find = Programs.find) {
76
+ const configured = settings && typeof settings === "object" ? settings : {};
77
+ const result = [];
78
+ for (const preset of PRESETS) {
79
+ const setting = configured[preset.id];
80
+ if (setting === false) {
81
+ continue;
82
+ }
83
+ const found = preset.programs.find((program) => find(program));
84
+ if (!found && !setting) {
85
+ continue;
86
+ }
87
+ const { programs, ...config } = preset;
88
+ const command = programs.includes(config.command) && found ? found : config.command;
89
+ const own = setting || {};
90
+ result.push({
91
+ ...config,
92
+ command,
93
+ ...own,
94
+ id: preset.id,
95
+ env: config.env || own.env ? { ...config.env, ...own.env } : undefined,
96
+ options: config.options || own.options ? { ...config.options, ...own.options } : undefined
97
+ });
98
+ }
99
+ for (const [id, setting] of Object.entries(configured)) {
100
+ if (setting && typeof setting === "object" && !PRESETS.some((preset) => preset.id === id) && typeof setting.command === "string") {
101
+ result.push({ name: id, args: [], ...setting, id, command: setting.command });
102
+ }
103
+ }
104
+ return result;
105
+ }
106
+ AgentCatalog.agents = agents;
107
+ })(AgentCatalog ||= {});
108
+ var Programs;
109
+ ((Programs) => {
110
+ function find(program, path = process.env.PATH) {
111
+ const extensions = process.platform === "win32" ? (process.env.PATHEXT ?? ".EXE;.CMD;.BAT").split(";") : [""];
112
+ for (const dir of (path ?? "").split(delimiter)) {
113
+ for (const extension of extensions) {
114
+ if (dir && isFile(join(dir, program + extension))) {
115
+ return program;
116
+ }
117
+ }
118
+ }
119
+ return;
120
+ }
121
+ Programs.find = find;
122
+ function isFile(path) {
123
+ try {
124
+ return statSync(path).isFile();
125
+ } catch {
126
+ return false;
127
+ }
128
+ }
129
+ })(Programs ||= {});
130
+
131
+ class ModelCache {
132
+ file;
133
+ entries;
134
+ constructor(file = ModelCache.defaultFile()) {
135
+ this.file = file;
136
+ }
137
+ get(id) {
138
+ return this.read()[id];
139
+ }
140
+ set(id, models) {
141
+ const entries = this.read();
142
+ if (JSON.stringify(entries[id]) === JSON.stringify(models)) {
143
+ return false;
144
+ }
145
+ entries[id] = models;
146
+ try {
147
+ mkdirSync(dirname(this.file), { recursive: true });
148
+ const temp = `${this.file}.${process.pid}.tmp`;
149
+ writeFileSync(temp, JSON.stringify(entries, undefined, 2));
150
+ renameSync(temp, this.file);
151
+ } catch {}
152
+ return true;
153
+ }
154
+ read() {
155
+ if (!this.entries) {
156
+ try {
157
+ this.entries = existsSync(this.file) ? JSON.parse(readFileSync(this.file, "utf8")) : {};
158
+ } catch {
159
+ this.entries = {};
160
+ }
161
+ }
162
+ return this.entries ?? {};
163
+ }
164
+ static defaultFile() {
165
+ return join(process.env.XDG_CACHE_HOME || join(homedir(), ".cache"), "ta-agents", "models.json");
166
+ }
167
+ }
168
+
45
169
  // src/agent-turn.ts
46
170
  var OUTPUT_CHARS = 8000;
47
171
  var WRITING_KINDS = new Set(["edit", "delete", "move"]);
@@ -361,6 +485,7 @@ class AgentSession {
361
485
  callbacks = { permission: async (request) => AgentSession.allow(request), activity: () => {
362
486
  return;
363
487
  } };
488
+ told = new Set;
364
489
  queued = [];
365
490
  quietTimer;
366
491
  abandoned;
@@ -699,6 +824,10 @@ var TEXTS = {
699
824
  en: (agent, command) => `${agent} ran \`${command}\` and said nothing back; it may only show in ${agent}'s own interface.`,
700
825
  zh: (agent, command) => `${agent} 已执行 \`${command}\`,没有返回内容(这个命令可能只在 ${agent} 自己的界面里有显示)。`
701
826
  },
827
+ starting: {
828
+ en: (agent) => `Starting ${agent}…`,
829
+ zh: (agent) => `正在启动 ${agent}…`
830
+ },
702
831
  unfinished: {
703
832
  en: (agent) => `${agent} ended its turn before this call finished.`,
704
833
  zh: (agent) => `${agent} 在这次调用完成前结束了这一轮。`
@@ -714,6 +843,8 @@ function text(key, ...args) {
714
843
 
715
844
  // src/acp-agent.ts
716
845
  var STDERR_CHARS = 2000;
846
+ var LIFELINE = '(read _ <&3; kill $$) </dev/null >/dev/null 2>&1 & exec "$@" 3<&-';
847
+ var NOT_FOUND = 127;
717
848
  function agentEnv(extra) {
718
849
  const env = {};
719
850
  for (const [key, value] of Object.entries(process.env)) {
@@ -806,7 +937,7 @@ class AcpAgent {
806
937
  }
807
938
  reference(on) {
808
939
  const child = this.child;
809
- for (const handle of [child, child?.stdin, child?.stdout, child?.stderr]) {
940
+ for (const handle of [child, child?.stdin, child?.stdout, child?.stderr, child?.stdio[3]]) {
810
941
  if (on) {
811
942
  handle?.ref?.();
812
943
  } else {
@@ -833,13 +964,14 @@ class AcpAgent {
833
964
  async launch() {
834
965
  this.stop();
835
966
  this.stderr = "";
836
- const child = spawn(this.program.command, this.program.args, {
837
- cwd: this.program.cwd,
838
- env: agentEnv(this.program.env),
839
- stdio: ["pipe", "pipe", "pipe"],
840
- shell: process.platform === "win32",
841
- windowsHide: true
842
- });
967
+ const posix = process.platform !== "win32";
968
+ const { command, args, cwd } = this.program;
969
+ const env = agentEnv(this.program.env);
970
+ const notFound = () => new Error(text("notFound", this.name, command));
971
+ if (posix && !command.includes("/") && !Programs.find(command, env.PATH)) {
972
+ throw notFound();
973
+ }
974
+ const child = posix ? spawn("/bin/sh", ["-c", LIFELINE, "sh", command, ...args], { cwd, env, stdio: ["pipe", "pipe", "pipe", "pipe"] }) : spawn(command, args, { cwd, env, stdio: ["pipe", "pipe", "pipe"], shell: true, windowsHide: true });
843
975
  this.child = child;
844
976
  const { stdin, stdout } = child;
845
977
  if (!stdin || !stdout) {
@@ -849,8 +981,11 @@ class AcpAgent {
849
981
  this.connection = connection;
850
982
  child.stderr?.setEncoding("utf8");
851
983
  child.stderr?.on("data", (chunk) => this.stderr = (this.stderr + chunk).slice(-STDERR_CHARS));
852
- child.on("error", (error) => connection.close(error.code === "ENOENT" ? new Error(text("notFound", this.name, this.program.command)) : error));
853
- child.on("exit", (code) => connection.close(new Error(text("exited", this.name, String(code ?? "signal"), this.lastError()))));
984
+ child.on("error", (error) => connection.close(error.code === "ENOENT" ? notFound() : error));
985
+ child.on("exit", (code) => {
986
+ child.stdio[3]?.destroy();
987
+ connection.close(posix && code === NOT_FOUND ? notFound() : new Error(text("exited", this.name, String(code ?? "signal"), this.lastError())));
988
+ });
854
989
  connection.onNotification = (method, params) => this.notified(method, params);
855
990
  connection.onRequest = (method, params) => this.requested(method, params);
856
991
  connection.onClose = (error) => {
@@ -986,6 +1121,9 @@ class AgentBindings {
986
1121
  sessionId(agent) {
987
1122
  return this.ids.get(agent);
988
1123
  }
1124
+ ready(config) {
1125
+ return this.opened.has(config.id) && this.agentOf(config, this.cwd, this.owner).running;
1126
+ }
989
1127
  open(config, signal) {
990
1128
  this.used.set(JSON.stringify([config.id, this.cwd, this.owner]), { config, cwd: this.cwd, owner: this.owner });
991
1129
  const agent = this.agentOf(config, this.cwd, this.owner);
@@ -1035,132 +1173,8 @@ class AgentBindings {
1035
1173
  }
1036
1174
  }
1037
1175
  static isRemote(cwd) {
1038
- const agentDir = process.env.TA_CODING_AGENT_DIR ?? join(homedir(), ".ta", "agent");
1039
- return cwd.startsWith(join(agentDir, "remote") + sep);
1040
- }
1041
- }
1042
-
1043
- // src/agent-catalog.ts
1044
- import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
1045
- import { homedir as homedir2 } from "node:os";
1046
- import { delimiter, dirname, join as join2 } from "node:path";
1047
- var PRESETS = [
1048
- {
1049
- id: "codebuddy-code",
1050
- name: "CodeBuddy Code",
1051
- command: "cbc",
1052
- args: ["--acp"],
1053
- programs: ["cbc", "codebuddy"],
1054
- login: "cbc",
1055
- env: { CODEBUDDY_CODE_MODE_ENABLED: "1" },
1056
- clientMeta: { "codebuddy.ai": { mainAgentSupport: true, toolModeSupport: true } },
1057
- options: { mode: "fullAccess" },
1058
- processPerSession: true
1059
- },
1060
- {
1061
- id: "claude-code",
1062
- name: "Claude Code",
1063
- command: "npx",
1064
- args: ["-y", "@agentclientprotocol/claude-agent-acp@0.106.0"],
1065
- programs: ["claude"],
1066
- login: "claude"
1067
- },
1068
- { id: "codex", name: "Codex", command: "npx", args: ["-y", "@agentclientprotocol/codex-acp@2.6.0"], programs: ["codex"], login: "codex login" }
1069
- ];
1070
- var AgentCatalog;
1071
- ((AgentCatalog) => {
1072
- AgentCatalog.DEFAULT_MODEL = "default";
1073
- function agents(settings, find = Programs.find) {
1074
- const configured = settings && typeof settings === "object" ? settings : {};
1075
- const result = [];
1076
- for (const preset of PRESETS) {
1077
- const setting = configured[preset.id];
1078
- if (setting === false) {
1079
- continue;
1080
- }
1081
- const found = preset.programs.find((program) => find(program));
1082
- if (!found && !setting) {
1083
- continue;
1084
- }
1085
- const { programs, ...config } = preset;
1086
- const command = programs.includes(config.command) && found ? found : config.command;
1087
- const own = setting || {};
1088
- result.push({
1089
- ...config,
1090
- command,
1091
- ...own,
1092
- id: preset.id,
1093
- env: config.env || own.env ? { ...config.env, ...own.env } : undefined,
1094
- options: config.options || own.options ? { ...config.options, ...own.options } : undefined
1095
- });
1096
- }
1097
- for (const [id, setting] of Object.entries(configured)) {
1098
- if (setting && typeof setting === "object" && !PRESETS.some((preset) => preset.id === id) && typeof setting.command === "string") {
1099
- result.push({ name: id, args: [], ...setting, id, command: setting.command });
1100
- }
1101
- }
1102
- return result;
1103
- }
1104
- AgentCatalog.agents = agents;
1105
- })(AgentCatalog ||= {});
1106
- var Programs;
1107
- ((Programs) => {
1108
- function find(program) {
1109
- const extensions = process.platform === "win32" ? (process.env.PATHEXT ?? ".EXE;.CMD;.BAT").split(";") : [""];
1110
- for (const dir of (process.env.PATH ?? "").split(delimiter)) {
1111
- for (const extension of extensions) {
1112
- if (dir && isFile(join2(dir, program + extension))) {
1113
- return program;
1114
- }
1115
- }
1116
- }
1117
- return;
1118
- }
1119
- Programs.find = find;
1120
- function isFile(path) {
1121
- try {
1122
- return statSync(path).isFile();
1123
- } catch {
1124
- return false;
1125
- }
1126
- }
1127
- })(Programs ||= {});
1128
-
1129
- class ModelCache {
1130
- file;
1131
- entries;
1132
- constructor(file = ModelCache.defaultFile()) {
1133
- this.file = file;
1134
- }
1135
- get(id) {
1136
- return this.read()[id];
1137
- }
1138
- set(id, models) {
1139
- const entries = this.read();
1140
- if (JSON.stringify(entries[id]) === JSON.stringify(models)) {
1141
- return false;
1142
- }
1143
- entries[id] = models;
1144
- try {
1145
- mkdirSync(dirname(this.file), { recursive: true });
1146
- const temp = `${this.file}.${process.pid}.tmp`;
1147
- writeFileSync(temp, JSON.stringify(entries, undefined, 2));
1148
- renameSync(temp, this.file);
1149
- } catch {}
1150
- return true;
1151
- }
1152
- read() {
1153
- if (!this.entries) {
1154
- try {
1155
- this.entries = existsSync(this.file) ? JSON.parse(readFileSync(this.file, "utf8")) : {};
1156
- } catch {
1157
- this.entries = {};
1158
- }
1159
- }
1160
- return this.entries ?? {};
1161
- }
1162
- static defaultFile() {
1163
- return join2(process.env.XDG_CACHE_HOME || join2(homedir2(), ".cache"), "ta-agents", "models.json");
1176
+ const agentDir = process.env.TA_CODING_AGENT_DIR ?? join2(homedir2(), ".ta", "agent");
1177
+ return cwd.startsWith(join2(agentDir, "remote") + sep);
1164
1178
  }
1165
1179
  }
1166
1180
 
@@ -1232,6 +1246,17 @@ ${kept}
1232
1246
  </earlier_conversation>`;
1233
1247
  }
1234
1248
  AgentPrompt.transcript = transcript;
1249
+ function notes(messages) {
1250
+ const system = messages.filter((message) => message.role === "system").map((message) => [text2(message), ...sections(message)].join(`
1251
+ `)).join(`
1252
+ `);
1253
+ return [...system.matchAll(/<(ta-[\w-]+)>[\s\S]*?<\/\1>/g)].map((match) => match[0]);
1254
+ }
1255
+ AgentPrompt.notes = notes;
1256
+ function sections(message) {
1257
+ const parts = message.sections ?? {};
1258
+ return Object.values(parts).filter((part) => typeof part === "string");
1259
+ }
1235
1260
  function text2(message) {
1236
1261
  if (message.role === "user" || message.role === "system") {
1237
1262
  return typeof message.content === "string" ? message.content : message.content.map((part) => part.type === "text" ? part.text : "").join("");
@@ -1276,7 +1301,7 @@ var AgentTool;
1276
1301
  target = str2(input.url) ?? str2(input.query);
1277
1302
  break;
1278
1303
  }
1279
- target ??= str2(input.description);
1304
+ target ??= ["description", "subject", "skill", "image_path", "file_path", "path", "url", "query"].map((key) => str2(input[key])).find(Boolean);
1280
1305
  return { title, kind: run.kind, target: relative(target ?? title, cwd), ...run.name ? { tool: run.name } : {} };
1281
1306
  }
1282
1307
  AgentTool.args = args;
@@ -1492,6 +1517,15 @@ class AgentStreamer {
1492
1517
  if (earlier) {
1493
1518
  prompt = [{ type: "text", text: earlier }, ...prompt];
1494
1519
  }
1520
+ const notes = AgentPrompt.notes(context.messages).filter((note) => !session.told.has(note));
1521
+ if (notes.length) {
1522
+ for (const note of notes) {
1523
+ session.told.add(note);
1524
+ }
1525
+ prompt = [{ type: "text", text: notes.join(`
1526
+
1527
+ `) }, ...prompt];
1528
+ }
1495
1529
  await session.idle();
1496
1530
  await this.configure(session, model, options, signal);
1497
1531
  turn = session.prompt(prompt);
@@ -1717,7 +1751,18 @@ function activate(pi, options = {}) {
1717
1751
  learn(config, session);
1718
1752
  }, options.agentOf, options.release);
1719
1753
  const streamer = new AgentStreamer({
1720
- open: (config, signal) => bindings.open(config, signal),
1754
+ open: async (config, signal) => {
1755
+ if (bindings.ready(config)) {
1756
+ return bindings.open(config, signal);
1757
+ }
1758
+ const ui = context?.hasUI ? context.ui : undefined;
1759
+ ui?.setWorkingMessage(text("starting", config.name));
1760
+ try {
1761
+ return await bindings.open(config, signal);
1762
+ } finally {
1763
+ ui?.setWorkingMessage();
1764
+ }
1765
+ },
1721
1766
  calls,
1722
1767
  ended: (config, session) => {
1723
1768
  if (session.title && !pi.getSessionName()) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-agents",
3
- "version": "4.9.1",
3
+ "version": "4.11.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"
@@ -26,8 +26,8 @@
26
26
  "watch": "tsc -b -w --preserveWatchOutput"
27
27
  },
28
28
  "devDependencies": {
29
- "@earendil-works/pi-ai": "^1.5.0",
30
- "@earendil-works/pi-coding-agent": "^1.5.0"
29
+ "@earendil-works/pi-ai": "^1.5.2",
30
+ "@earendil-works/pi-coding-agent": "^1.5.2"
31
31
  },
32
32
  "publishConfig": {
33
33
  "access": "public"