@celljs/ta-agents 4.15.2 → 4.17.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 +14 -2
- package/README.md +14 -2
- package/extension/index.mjs +286 -111
- package/host/index.mjs +662 -0
- package/package.json +21 -5
- package/web/index.js +385 -0
package/host/index.mjs
ADDED
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
// src/host.ts
|
|
2
|
+
import { readFileSync as readFileSync3 } from "node:fs";
|
|
3
|
+
import { join as join5 } from "node:path";
|
|
4
|
+
|
|
5
|
+
// src/agent-catalog.ts
|
|
6
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { delimiter, dirname, join } from "node:path";
|
|
8
|
+
var CLAUDE_ADAPTER = "@agentclientprotocol/claude-agent-acp@0.106.0";
|
|
9
|
+
var CODEX_ADAPTER = "@agentclientprotocol/codex-acp@2.6.0";
|
|
10
|
+
var PRESETS = [
|
|
11
|
+
{
|
|
12
|
+
id: "codebuddy-code",
|
|
13
|
+
name: "CodeBuddy Code",
|
|
14
|
+
command: "cbc",
|
|
15
|
+
args: ["--acp"],
|
|
16
|
+
programs: ["cbc", "codebuddy"],
|
|
17
|
+
login: "cbc",
|
|
18
|
+
env: { CODEBUDDY_CODE_MODE_ENABLED: "1" },
|
|
19
|
+
clientMeta: { "codebuddy.ai": { mainAgentSupport: true, toolModeSupport: true } },
|
|
20
|
+
options: { mode: "fullAccess" },
|
|
21
|
+
processPerSession: true,
|
|
22
|
+
install: { packages: ["@tencent-ai/codebuddy-code"], bin: "cbc", args: ["--acp"], login: ["cbc"] }
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
id: "claude-code",
|
|
26
|
+
name: "Claude Code",
|
|
27
|
+
command: "npx",
|
|
28
|
+
args: ["-y", CLAUDE_ADAPTER],
|
|
29
|
+
programs: ["claude"],
|
|
30
|
+
login: "claude",
|
|
31
|
+
install: {
|
|
32
|
+
packages: ["@anthropic-ai/claude-code", CLAUDE_ADAPTER],
|
|
33
|
+
bin: "claude-agent-acp",
|
|
34
|
+
args: [],
|
|
35
|
+
programs: { CLAUDE_CODE_EXECUTABLE: "claude" },
|
|
36
|
+
login: ["claude"]
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
id: "codex",
|
|
41
|
+
name: "Codex",
|
|
42
|
+
command: "npx",
|
|
43
|
+
args: ["-y", CODEX_ADAPTER],
|
|
44
|
+
programs: ["codex"],
|
|
45
|
+
login: "codex login",
|
|
46
|
+
install: { packages: ["@openai/codex", CODEX_ADAPTER], bin: "codex-acp", args: [], programs: { CODEX_PATH: "codex" }, login: ["codex", "login"] }
|
|
47
|
+
}
|
|
48
|
+
];
|
|
49
|
+
var AgentCatalog;
|
|
50
|
+
((AgentCatalog) => {
|
|
51
|
+
AgentCatalog.DEFAULT_MODEL = "default";
|
|
52
|
+
function presets() {
|
|
53
|
+
return PRESETS.map(({ id, name, programs, install }) => ({ id, name, programs, install }));
|
|
54
|
+
}
|
|
55
|
+
AgentCatalog.presets = presets;
|
|
56
|
+
function agents(settings, find = Programs.find, local) {
|
|
57
|
+
const configured = settings && typeof settings === "object" ? settings : {};
|
|
58
|
+
const result = [];
|
|
59
|
+
for (const preset of PRESETS) {
|
|
60
|
+
const setting = configured[preset.id];
|
|
61
|
+
if (setting === false) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const found = preset.programs.find((program) => find(program));
|
|
65
|
+
const installed = found ? undefined : localConfig(preset, local);
|
|
66
|
+
if (!found && !installed && !setting) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const { programs, install, ...plain } = preset;
|
|
70
|
+
const config = installed ?? plain;
|
|
71
|
+
const command = programs.includes(config.command) && found ? found : config.command;
|
|
72
|
+
const own = setting || {};
|
|
73
|
+
result.push({
|
|
74
|
+
...config,
|
|
75
|
+
command,
|
|
76
|
+
...own,
|
|
77
|
+
id: preset.id,
|
|
78
|
+
env: config.env || own.env ? { ...config.env, ...own.env } : undefined,
|
|
79
|
+
options: config.options || own.options ? { ...config.options, ...own.options } : undefined
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
for (const [id, setting] of Object.entries(configured)) {
|
|
83
|
+
if (setting && typeof setting === "object" && !PRESETS.some((preset) => preset.id === id) && typeof setting.command === "string") {
|
|
84
|
+
result.push({ name: id, args: [], ...setting, id, command: setting.command });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
AgentCatalog.agents = agents;
|
|
90
|
+
function localConfig(preset, local) {
|
|
91
|
+
const { programs, install, command: _npx, ...config } = preset;
|
|
92
|
+
const command = local?.bin(install.bin);
|
|
93
|
+
if (!local || !command) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const env = { ...config.env };
|
|
97
|
+
for (const [name, program] of Object.entries(install.programs ?? {})) {
|
|
98
|
+
const path2 = local.bin(program);
|
|
99
|
+
if (path2) {
|
|
100
|
+
env[name] = path2;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const path = local.path();
|
|
104
|
+
if (path) {
|
|
105
|
+
env.PATH = path;
|
|
106
|
+
}
|
|
107
|
+
const [login, ...loginArgs] = install.login;
|
|
108
|
+
const loginPath = local.bin(login);
|
|
109
|
+
return {
|
|
110
|
+
...config,
|
|
111
|
+
command,
|
|
112
|
+
args: install.args,
|
|
113
|
+
env,
|
|
114
|
+
login: loginPath ? Programs.commandLine([loginPath, ...loginArgs]) : config.login
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
})(AgentCatalog ||= {});
|
|
118
|
+
var Programs;
|
|
119
|
+
((Programs) => {
|
|
120
|
+
function commandLine(words, platform = process.platform) {
|
|
121
|
+
return words.map((word) => {
|
|
122
|
+
if (/^[\w@%+=:,./\\-]+$/.test(word)) {
|
|
123
|
+
return word;
|
|
124
|
+
}
|
|
125
|
+
return platform === "win32" ? `"${word}"` : `'${word.replace(/'/g, `'\\''`)}'`;
|
|
126
|
+
}).join(" ");
|
|
127
|
+
}
|
|
128
|
+
Programs.commandLine = commandLine;
|
|
129
|
+
function find(program, path = process.env.PATH) {
|
|
130
|
+
const extensions = process.platform === "win32" ? (process.env.PATHEXT ?? ".EXE;.CMD;.BAT").split(";") : [""];
|
|
131
|
+
for (const dir of (path ?? "").split(delimiter)) {
|
|
132
|
+
for (const extension of extensions) {
|
|
133
|
+
if (dir && isFile(join(dir, program + extension))) {
|
|
134
|
+
return program;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
Programs.find = find;
|
|
141
|
+
function isFile(path) {
|
|
142
|
+
try {
|
|
143
|
+
return statSync(path).isFile();
|
|
144
|
+
} catch {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
})(Programs ||= {});
|
|
149
|
+
|
|
150
|
+
// src/agent-installer.ts
|
|
151
|
+
import { spawn } from "node:child_process";
|
|
152
|
+
import { existsSync as existsSync3, mkdirSync as mkdirSync3, renameSync as renameSync3, rmSync as rmSync2, writeFileSync as writeFileSync2 } from "node:fs";
|
|
153
|
+
import { join as join3 } from "node:path";
|
|
154
|
+
|
|
155
|
+
// ../ta-exec/esm/node/ssh/node-dist.js
|
|
156
|
+
import { createHash } from "node:crypto";
|
|
157
|
+
import { createReadStream, createWriteStream, existsSync as existsSync2, mkdirSync as mkdirSync2, renameSync as renameSync2, rmSync } from "node:fs";
|
|
158
|
+
import { join as join2 } from "node:path";
|
|
159
|
+
var NodeDist;
|
|
160
|
+
(function(NodeDist2) {
|
|
161
|
+
NodeDist2.DEFAULT_MIRROR = "https://nodejs.org/dist";
|
|
162
|
+
NodeDist2.LINE = "latest-v22.x";
|
|
163
|
+
function find(sums, platform) {
|
|
164
|
+
for (const line of sums.split(`
|
|
165
|
+
`)) {
|
|
166
|
+
const match = /^([0-9a-f]{64})\s+(node-(v[\d.]+)-([\w-]+)\.tar\.gz)$/.exec(line.trim());
|
|
167
|
+
if (match && match[4] === platform) {
|
|
168
|
+
return { sha256: match[1], name: match[2], version: match[3] };
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
NodeDist2.find = find;
|
|
174
|
+
async function download(platform, dir, mirror = NodeDist2.DEFAULT_MIRROR, onVersion) {
|
|
175
|
+
const base = `${mirror.replace(/\/+$/, "")}/${NodeDist2.LINE}`;
|
|
176
|
+
const sums = await get(`${base}/SHASUMS256.txt`);
|
|
177
|
+
const found = find(await sums.text(), platform);
|
|
178
|
+
if (!found) {
|
|
179
|
+
throw new Error(`Node.js has no build for ${platform} at ${base}.`);
|
|
180
|
+
}
|
|
181
|
+
onVersion === null || onVersion === undefined || onVersion(found.version);
|
|
182
|
+
mkdirSync2(dir, { recursive: true });
|
|
183
|
+
const file = join2(dir, found.name);
|
|
184
|
+
if (existsSync2(file) && await sha256(file) === found.sha256) {
|
|
185
|
+
return { version: found.version, file };
|
|
186
|
+
}
|
|
187
|
+
const response = await get(`${base}/${found.name}`);
|
|
188
|
+
const partial = `${file}.${process.pid}.part`;
|
|
189
|
+
const hash = createHash("sha256");
|
|
190
|
+
const out = createWriteStream(partial);
|
|
191
|
+
try {
|
|
192
|
+
for await (const chunk of response.body) {
|
|
193
|
+
hash.update(chunk);
|
|
194
|
+
if (!out.write(chunk)) {
|
|
195
|
+
await new Promise((resolve) => out.once("drain", resolve));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
await new Promise((resolve, reject) => out.end((error) => error ? reject(error) : resolve()));
|
|
199
|
+
if (hash.digest("hex") !== found.sha256) {
|
|
200
|
+
throw new Error(`The download of ${found.name} does not match its SHA-256 sum.`);
|
|
201
|
+
}
|
|
202
|
+
renameSync2(partial, file);
|
|
203
|
+
} finally {
|
|
204
|
+
out.destroy();
|
|
205
|
+
rmSync(partial, { force: true });
|
|
206
|
+
}
|
|
207
|
+
return { version: found.version, file };
|
|
208
|
+
}
|
|
209
|
+
NodeDist2.download = download;
|
|
210
|
+
async function get(url) {
|
|
211
|
+
const response = await fetch(url);
|
|
212
|
+
if (!response.ok || !response.body) {
|
|
213
|
+
throw new Error(`Cannot download ${url}: HTTP ${response.status}.`);
|
|
214
|
+
}
|
|
215
|
+
return response;
|
|
216
|
+
}
|
|
217
|
+
function sha256(file) {
|
|
218
|
+
return new Promise((resolve, reject) => {
|
|
219
|
+
const hash = createHash("sha256");
|
|
220
|
+
createReadStream(file).on("data", (data) => hash.update(data)).on("end", () => resolve(hash.digest("hex"))).on("error", reject);
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
})(NodeDist || (NodeDist = {}));
|
|
224
|
+
|
|
225
|
+
// src/texts.ts
|
|
226
|
+
function spaced(word) {
|
|
227
|
+
return /^[\x20-\x7e]+$/.test(word) ? ` ${word} ` : word;
|
|
228
|
+
}
|
|
229
|
+
var TEXTS = {
|
|
230
|
+
remote: {
|
|
231
|
+
"en-US": (agent) => `${agent} works in directories of this computer; it cannot work in a remote directory yet.`,
|
|
232
|
+
"zh-CN": (agent) => `${agent} 只能在这台电脑上的目录里工作,暂不支持远程目录。`,
|
|
233
|
+
"zh-TW": (agent) => `${agent} 只能在這台電腦上的目錄裡工作,暫不支援遠端目錄。`,
|
|
234
|
+
"ja-JP": (agent) => `${agent} はこのコンピュータのディレクトリでのみ動作します。リモートディレクトリはまだサポートしていません。`
|
|
235
|
+
},
|
|
236
|
+
signIn: {
|
|
237
|
+
"en-US": (agent, command) => `${agent} is not signed in. Run \`${command}\` in a terminal to sign in, then try again.`,
|
|
238
|
+
"zh-CN": (agent, command) => `${agent} 还没有登录。请在终端里运行 \`${command}\` 登录后再试。`,
|
|
239
|
+
"zh-TW": (agent, command) => `${agent} 還沒有登入。請在終端機裡執行 \`${command}\` 登入後再試。`,
|
|
240
|
+
"ja-JP": (agent, command) => `${agent} はサインインしていません。ターミナルで \`${command}\` を実行してサインインしてから、もう一度試してください。`
|
|
241
|
+
},
|
|
242
|
+
signInPick: {
|
|
243
|
+
"en-US": (agent) => `${agent} is not signed in yet. Sign in to go on:`,
|
|
244
|
+
"zh-CN": (agent) => `${agent} 还没有登录,登录后接着做:`,
|
|
245
|
+
"zh-TW": (agent) => `${agent} 還沒有登入,登入後接著做:`,
|
|
246
|
+
"ja-JP": (agent) => `${agent} はまだサインインしていません。サインインして続けます:`
|
|
247
|
+
},
|
|
248
|
+
signInWith: {
|
|
249
|
+
"en-US": (way) => `Sign in with ${way}`,
|
|
250
|
+
"zh-CN": (way) => `用${spaced(way)}登录`,
|
|
251
|
+
"zh-TW": (way) => `用${spaced(way)}登入`,
|
|
252
|
+
"ja-JP": (way) => `${way} でサインイン`
|
|
253
|
+
},
|
|
254
|
+
signInMethod: {
|
|
255
|
+
"en-US": (id) => ({ iOA: "iOA", external: "Google / GitHub", internal: "WeChat", selfhosted: "an enterprise domain" })[id] ?? "",
|
|
256
|
+
"zh-CN": (id) => ({ iOA: "iOA", external: "Google / GitHub", internal: "微信", selfhosted: "企业域名" })[id] ?? "",
|
|
257
|
+
"zh-TW": (id) => ({ iOA: "iOA", external: "Google / GitHub", internal: "微信", selfhosted: "企業網域" })[id] ?? "",
|
|
258
|
+
"ja-JP": (id) => ({ iOA: "iOA", external: "Google / GitHub", internal: "WeChat", selfhosted: "企業ドメイン" })[id] ?? ""
|
|
259
|
+
},
|
|
260
|
+
signingIn: {
|
|
261
|
+
"en-US": (agent) => `Waiting for you to sign in to ${agent} in the browser…`,
|
|
262
|
+
"zh-CN": (agent) => `等你在浏览器里登录 ${agent}…`,
|
|
263
|
+
"zh-TW": (agent) => `等你在瀏覽器裡登入 ${agent}…`,
|
|
264
|
+
"ja-JP": (agent) => `ブラウザで ${agent} にサインインするのを待っています…`
|
|
265
|
+
},
|
|
266
|
+
signInPage: {
|
|
267
|
+
"en-US": (agent, url) => `Sign in to ${agent} in the browser: ${url}`,
|
|
268
|
+
"zh-CN": (agent, url) => `在浏览器里登录 ${agent}:${url}`,
|
|
269
|
+
"zh-TW": (agent, url) => `在瀏覽器裡登入 ${agent}:${url}`,
|
|
270
|
+
"ja-JP": (agent, url) => `ブラウザで ${agent} にサインインしてください:${url}`
|
|
271
|
+
},
|
|
272
|
+
signInFailed: {
|
|
273
|
+
"en-US": (agent) => `Could not sign in to ${agent}.`,
|
|
274
|
+
"zh-CN": (agent) => `没能登录 ${agent}。`,
|
|
275
|
+
"zh-TW": (agent) => `沒能登入 ${agent}。`,
|
|
276
|
+
"ja-JP": (agent) => `${agent} にサインインできませんでした。`
|
|
277
|
+
},
|
|
278
|
+
signedIn: {
|
|
279
|
+
"en-US": (agent) => `Signed in to ${agent}`,
|
|
280
|
+
"zh-CN": (agent) => `已登录 ${agent}`,
|
|
281
|
+
"zh-TW": (agent) => `已登入 ${agent}`,
|
|
282
|
+
"ja-JP": (agent) => `${agent} にサインインしました`
|
|
283
|
+
},
|
|
284
|
+
notFound: {
|
|
285
|
+
"en-US": (agent, command) => `${agent}: \`${command}\` was not found. Install it, or set its command in \`agents\` of settings.json.`,
|
|
286
|
+
"zh-CN": (agent, command) => `${agent}:找不到命令 \`${command}\`。请先安装,或在 settings.json 的 \`agents\` 里指定它的命令。`,
|
|
287
|
+
"zh-TW": (agent, command) => `${agent}:找不到指令 \`${command}\`。請先安裝,或在 settings.json 的 \`agents\` 裡指定它的指令。`,
|
|
288
|
+
"ja-JP": (agent, command) => `${agent}:コマンド \`${command}\` が見つかりません。インストールするか、settings.json の \`agents\` でそのコマンドを設定してください。`
|
|
289
|
+
},
|
|
290
|
+
needNode: {
|
|
291
|
+
"en-US": (major) => `Installing needs Node.js ${major} or newer, which this computer does not have. Install Node.js from nodejs.org, then try again.`,
|
|
292
|
+
"zh-CN": (major) => `安装需要 Node.js ${major} 或更新版本,这台电脑上没有。请先从 nodejs.org 安装 Node.js,再试一次。`,
|
|
293
|
+
"zh-TW": (major) => `安裝需要 Node.js ${major} 或更新版本,這台電腦上沒有。請先從 nodejs.org 安裝 Node.js,再試一次。`,
|
|
294
|
+
"ja-JP": (major) => `インストールには Node.js ${major} 以降が必要ですが、このコンピュータにはありません。nodejs.org から Node.js をインストールしてから、もう一度試してください。`
|
|
295
|
+
},
|
|
296
|
+
installFailed: {
|
|
297
|
+
"en-US": (detail) => `Could not install it. npm said:
|
|
298
|
+
${detail}`,
|
|
299
|
+
"zh-CN": (detail) => `没能装上。npm 的输出:
|
|
300
|
+
${detail}`,
|
|
301
|
+
"zh-TW": (detail) => `沒能裝上。npm 的輸出:
|
|
302
|
+
${detail}`,
|
|
303
|
+
"ja-JP": (detail) => `インストールできませんでした。npm の出力:
|
|
304
|
+
${detail}`
|
|
305
|
+
},
|
|
306
|
+
unknownAgent: {
|
|
307
|
+
"en-US": (id) => `Ta cannot install the agent ${id}.`,
|
|
308
|
+
"zh-CN": (id) => `Ta 不能安装 ${id} 这个 Agent。`,
|
|
309
|
+
"zh-TW": (id) => `Ta 不能安裝 ${id} 這個 Agent。`,
|
|
310
|
+
"ja-JP": (id) => `Ta はエージェント ${id} をインストールできません。`
|
|
311
|
+
},
|
|
312
|
+
exited: {
|
|
313
|
+
"en-US": (agent, code, last) => `${agent} exited (${code})${last ? `: ${last}` : ""}`,
|
|
314
|
+
"zh-CN": (agent, code, last) => `${agent} 已退出(${code})${last ? `:${last}` : ""}`,
|
|
315
|
+
"zh-TW": (agent, code, last) => `${agent} 已結束(${code})${last ? `:${last}` : ""}`,
|
|
316
|
+
"ja-JP": (agent, code, last) => `${agent} は終了しました(${code})${last ? `:${last}` : ""}`
|
|
317
|
+
},
|
|
318
|
+
commandDone: {
|
|
319
|
+
"en-US": (agent, command) => `${agent} ran \`${command}\` and said nothing back; it may only show in ${agent}'s own interface.`,
|
|
320
|
+
"zh-CN": (agent, command) => `${agent} 已执行 \`${command}\`,没有返回内容(这个命令可能只在 ${agent} 自己的界面里有显示)。`,
|
|
321
|
+
"zh-TW": (agent, command) => `${agent} 已執行 \`${command}\`,沒有傳回內容(這個指令可能只在 ${agent} 自己的介面裡有顯示)。`,
|
|
322
|
+
"ja-JP": (agent, command) => `${agent} は \`${command}\` を実行しましたが、何も返しません。${agent} 自体のインターフェースにのみ表示される可能性があります。`
|
|
323
|
+
},
|
|
324
|
+
starting: {
|
|
325
|
+
"en-US": (agent) => `Starting ${agent}…`,
|
|
326
|
+
"zh-CN": (agent) => `正在启动 ${agent}…`,
|
|
327
|
+
"zh-TW": (agent) => `正在啟動 ${agent}…`,
|
|
328
|
+
"ja-JP": (agent) => `${agent} を起動中…`
|
|
329
|
+
},
|
|
330
|
+
refused: {
|
|
331
|
+
"en-US": (agent) => `${agent} declined to go on with this turn.`,
|
|
332
|
+
"zh-CN": (agent) => `${agent} 拒绝继续这一轮。`,
|
|
333
|
+
"zh-TW": (agent) => `${agent} 拒絕繼續這一輪。`,
|
|
334
|
+
"ja-JP": (agent) => `${agent} はこのターンを続行することを拒否しました。`
|
|
335
|
+
},
|
|
336
|
+
retrying: {
|
|
337
|
+
"en-US": (agent, network, attempt) => `${agent}: ${network ? "the connection to its model dropped" : "a request to its model failed"}, retrying${attempt ? ` (attempt ${attempt})` : ""}…`,
|
|
338
|
+
"zh-CN": (agent, network, attempt) => `${agent}:${network ? "连接模型的网络中断" : "模型请求失败"},正在重试${attempt ? `(第 ${attempt} 次)` : ""}…`,
|
|
339
|
+
"zh-TW": (agent, network, attempt) => `${agent}:${network ? "連線模型的網路中斷" : "模型請求失敗"},正在重試${attempt ? `(第 ${attempt} 次)` : ""}…`,
|
|
340
|
+
"ja-JP": (agent, network, attempt) => `${agent}:${network ? "モデルへの接続が切断されました" : "モデルへのリクエストが失敗しました"}、再試行中${attempt ? `(試行 ${attempt})` : ""}…`
|
|
341
|
+
},
|
|
342
|
+
dropped: {
|
|
343
|
+
"en-US": (agent) => `${agent} went on without finishing this call.`,
|
|
344
|
+
"zh-CN": (agent) => `${agent} 没有完成这次调用就接着往下做了。`,
|
|
345
|
+
"zh-TW": (agent) => `${agent} 沒有完成這次呼叫就接著往下做了。`,
|
|
346
|
+
"ja-JP": (agent) => `${agent} はこの呼び出しを完了せずに進みました。`
|
|
347
|
+
},
|
|
348
|
+
unfinished: {
|
|
349
|
+
"en-US": (agent) => `${agent} ended its turn before this call finished.`,
|
|
350
|
+
"zh-CN": (agent) => `${agent} 在这次调用完成前结束了这一轮。`,
|
|
351
|
+
"zh-TW": (agent) => `${agent} 在這次呼叫完成前結束了這一輪。`,
|
|
352
|
+
"ja-JP": (agent) => `${agent} はこの呼び出しが完了する前にターンを終了しました。`
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
function ofLanguage(tag) {
|
|
356
|
+
if (/^zh/i.test(tag)) {
|
|
357
|
+
return /(?:tw|hk|mo|hant)/i.test(tag) ? "zh-TW" : "zh-CN";
|
|
358
|
+
}
|
|
359
|
+
if (/^ja/i.test(tag)) {
|
|
360
|
+
return "ja-JP";
|
|
361
|
+
}
|
|
362
|
+
return "en-US";
|
|
363
|
+
}
|
|
364
|
+
function language(env = process.env) {
|
|
365
|
+
const locale = env.TA_LANGUAGE || env.LC_ALL || env.LC_MESSAGES || env.LANG || Intl.DateTimeFormat().resolvedOptions().locale;
|
|
366
|
+
return ofLanguage(locale);
|
|
367
|
+
}
|
|
368
|
+
function text(key, ...args) {
|
|
369
|
+
return TEXTS[key][language()](...args);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// src/agent-installer.ts
|
|
373
|
+
var NODE_MAJOR = 22;
|
|
374
|
+
var INSTALL_MS = 15 * 60000;
|
|
375
|
+
var CHECK_MS = 1e4;
|
|
376
|
+
|
|
377
|
+
class AgentInstaller {
|
|
378
|
+
installs;
|
|
379
|
+
run;
|
|
380
|
+
find;
|
|
381
|
+
downloadNode;
|
|
382
|
+
platform;
|
|
383
|
+
arch;
|
|
384
|
+
env;
|
|
385
|
+
constructor(installs, options = {}) {
|
|
386
|
+
this.installs = installs;
|
|
387
|
+
this.run = options.run ?? AgentInstaller.spawnRun;
|
|
388
|
+
this.find = options.find ?? Programs.find;
|
|
389
|
+
this.downloadNode = options.downloadNode ?? ((dir, build) => AgentInstaller.download(dir, build, this.run, this.env));
|
|
390
|
+
this.platform = options.platform ?? process.platform;
|
|
391
|
+
this.arch = options.arch ?? process.arch;
|
|
392
|
+
this.env = options.env ?? process.env;
|
|
393
|
+
}
|
|
394
|
+
async source() {
|
|
395
|
+
if (this.installs.nodeBin()) {
|
|
396
|
+
return "ta";
|
|
397
|
+
}
|
|
398
|
+
if (await this.systemNode()) {
|
|
399
|
+
return "system";
|
|
400
|
+
}
|
|
401
|
+
return this.build() ? "download" : "none";
|
|
402
|
+
}
|
|
403
|
+
async install(install) {
|
|
404
|
+
const source = await this.source();
|
|
405
|
+
if (source === "none") {
|
|
406
|
+
throw new Error(text("needNode", String(NODE_MAJOR)));
|
|
407
|
+
}
|
|
408
|
+
if (source === "download") {
|
|
409
|
+
await this.downloadNode(this.installs.nodeDir, this.build());
|
|
410
|
+
}
|
|
411
|
+
const own = this.installs.nodeBin();
|
|
412
|
+
const npm = source === "system" || !own ? "npm" : join3(own, "npm");
|
|
413
|
+
const env = { ...this.env, ...own && source !== "system" ? { PATH: this.installs.path(this.env) } : {} };
|
|
414
|
+
const dir = this.installs.dir;
|
|
415
|
+
mkdirSync3(dir, { recursive: true });
|
|
416
|
+
const manifest = join3(dir, "package.json");
|
|
417
|
+
if (!existsSync3(manifest)) {
|
|
418
|
+
writeFileSync2(manifest, `${JSON.stringify({ name: "ta-coding-agents", private: true }, undefined, 2)}
|
|
419
|
+
`);
|
|
420
|
+
}
|
|
421
|
+
const result = await this.run(npm, ["install", "--prefix", dir, "--no-audit", "--no-fund", "--loglevel=error", ...install.packages], {
|
|
422
|
+
cwd: dir,
|
|
423
|
+
env,
|
|
424
|
+
timeoutMs: INSTALL_MS
|
|
425
|
+
});
|
|
426
|
+
if (result.code !== 0) {
|
|
427
|
+
throw new Error(text("installFailed", AgentInstaller.tail(result.stderr || result.stdout) || String(result.code)));
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
async systemNode() {
|
|
431
|
+
if (!this.find("npm", this.env.PATH) || !this.find("node", this.env.PATH)) {
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
const result = await this.run("node", ["--version"], { env: this.env, timeoutMs: CHECK_MS }).catch(() => {
|
|
435
|
+
return;
|
|
436
|
+
});
|
|
437
|
+
const major = Number(/^v(\d+)\./.exec(result?.stdout.trim() ?? "")?.[1]);
|
|
438
|
+
return major >= NODE_MAJOR;
|
|
439
|
+
}
|
|
440
|
+
build() {
|
|
441
|
+
const os = { darwin: "darwin", linux: "linux" };
|
|
442
|
+
const arch = { x64: "x64", arm64: "arm64" };
|
|
443
|
+
return os[this.platform] && arch[this.arch] ? `${os[this.platform]}-${arch[this.arch]}` : undefined;
|
|
444
|
+
}
|
|
445
|
+
static tail(output, lines = 6) {
|
|
446
|
+
return output.split(/\r?\n/).map((line) => line.trimEnd()).filter(Boolean).slice(-lines).join(`
|
|
447
|
+
`);
|
|
448
|
+
}
|
|
449
|
+
static async download(dir, build, run, env) {
|
|
450
|
+
const tarball = await NodeDist.download(build, `${dir}.download`);
|
|
451
|
+
const partial = `${dir}.part`;
|
|
452
|
+
rmSync2(partial, { recursive: true, force: true });
|
|
453
|
+
mkdirSync3(partial, { recursive: true });
|
|
454
|
+
try {
|
|
455
|
+
const result = await run("tar", ["-xzf", tarball.file, "-C", partial, "--strip-components=1"], { env, timeoutMs: INSTALL_MS });
|
|
456
|
+
if (result.code !== 0) {
|
|
457
|
+
throw new Error(text("installFailed", AgentInstaller.tail(result.stderr) || String(result.code)));
|
|
458
|
+
}
|
|
459
|
+
rmSync2(dir, { recursive: true, force: true });
|
|
460
|
+
renameSync3(partial, dir);
|
|
461
|
+
} finally {
|
|
462
|
+
rmSync2(partial, { recursive: true, force: true });
|
|
463
|
+
rmSync2(`${dir}.download`, { recursive: true, force: true });
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
static spawnRun = (command, args, options) => new Promise((resolve, reject) => {
|
|
467
|
+
const child = spawn(command, args, {
|
|
468
|
+
cwd: options.cwd,
|
|
469
|
+
env: options.env,
|
|
470
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
471
|
+
shell: process.platform === "win32",
|
|
472
|
+
windowsHide: true
|
|
473
|
+
});
|
|
474
|
+
let stdout = "";
|
|
475
|
+
let stderr = "";
|
|
476
|
+
child.stdout?.setEncoding("utf8").on("data", (data) => {
|
|
477
|
+
stdout = (stdout + data).slice(-65536);
|
|
478
|
+
});
|
|
479
|
+
child.stderr?.setEncoding("utf8").on("data", (data) => {
|
|
480
|
+
stderr = (stderr + data).slice(-65536);
|
|
481
|
+
});
|
|
482
|
+
const timer = setTimeout(() => child.kill(), options.timeoutMs);
|
|
483
|
+
child.on("error", (error) => {
|
|
484
|
+
clearTimeout(timer);
|
|
485
|
+
reject(error);
|
|
486
|
+
});
|
|
487
|
+
child.on("close", (code) => {
|
|
488
|
+
clearTimeout(timer);
|
|
489
|
+
resolve({ code, stdout, stderr });
|
|
490
|
+
});
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// src/agent-installs.ts
|
|
495
|
+
import { readFileSync as readFileSync2, statSync as statSync2 } from "node:fs";
|
|
496
|
+
import { delimiter as delimiter2, join as join4 } from "node:path";
|
|
497
|
+
|
|
498
|
+
class AgentInstalls {
|
|
499
|
+
dir;
|
|
500
|
+
platform;
|
|
501
|
+
static DIR = "coding-agents";
|
|
502
|
+
constructor(dir, platform = process.platform) {
|
|
503
|
+
this.dir = dir;
|
|
504
|
+
this.platform = platform;
|
|
505
|
+
}
|
|
506
|
+
static of(agentDir) {
|
|
507
|
+
return new AgentInstalls(join4(agentDir, AgentInstalls.DIR));
|
|
508
|
+
}
|
|
509
|
+
get binDir() {
|
|
510
|
+
return join4(this.dir, "node_modules", ".bin");
|
|
511
|
+
}
|
|
512
|
+
get nodeDir() {
|
|
513
|
+
return join4(this.dir, "node");
|
|
514
|
+
}
|
|
515
|
+
bin(name) {
|
|
516
|
+
const file = join4(this.binDir, this.platform === "win32" ? `${name}.cmd` : name);
|
|
517
|
+
return isFile(file) ? file : undefined;
|
|
518
|
+
}
|
|
519
|
+
nodeBin() {
|
|
520
|
+
const bin = join4(this.nodeDir, "bin");
|
|
521
|
+
return isFile(join4(bin, "node")) ? bin : undefined;
|
|
522
|
+
}
|
|
523
|
+
path(env = process.env) {
|
|
524
|
+
const bin = this.nodeBin();
|
|
525
|
+
return bin ? [bin, env.PATH].filter(Boolean).join(delimiter2) : undefined;
|
|
526
|
+
}
|
|
527
|
+
version(spec) {
|
|
528
|
+
try {
|
|
529
|
+
return JSON.parse(readFileSync2(join4(this.dir, "node_modules", AgentInstalls.packageName(spec), "package.json"), "utf8")).version;
|
|
530
|
+
} catch {
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
static packageName(spec) {
|
|
535
|
+
const at = spec.indexOf("@", 1);
|
|
536
|
+
return at > 0 ? spec.slice(0, at) : spec;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
function isFile(path) {
|
|
540
|
+
try {
|
|
541
|
+
return statSync2(path).isFile();
|
|
542
|
+
} catch {
|
|
543
|
+
return false;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// src/agent-refresh.ts
|
|
548
|
+
var REFRESH = Symbol.for("ta-agents.refresh");
|
|
549
|
+
var AgentRefresh;
|
|
550
|
+
((AgentRefresh) => {
|
|
551
|
+
function all() {
|
|
552
|
+
const global = globalThis;
|
|
553
|
+
const set = global[REFRESH] ?? new Set;
|
|
554
|
+
global[REFRESH] = set;
|
|
555
|
+
return set;
|
|
556
|
+
}
|
|
557
|
+
function add(refresh) {
|
|
558
|
+
all().add(refresh);
|
|
559
|
+
return () => all().delete(refresh);
|
|
560
|
+
}
|
|
561
|
+
AgentRefresh.add = add;
|
|
562
|
+
function run() {
|
|
563
|
+
for (const refresh of all()) {
|
|
564
|
+
try {
|
|
565
|
+
refresh();
|
|
566
|
+
} catch {}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
AgentRefresh.run = run;
|
|
570
|
+
})(AgentRefresh ||= {});
|
|
571
|
+
|
|
572
|
+
// src/agents-protocol.ts
|
|
573
|
+
var AGENTS_NAME = "@celljs/ta-agents";
|
|
574
|
+
var AgentsKeys;
|
|
575
|
+
((AgentsKeys) => {
|
|
576
|
+
AgentsKeys.INSTALLING = "installing";
|
|
577
|
+
})(AgentsKeys ||= {});
|
|
578
|
+
|
|
579
|
+
// src/host.ts
|
|
580
|
+
class AgentsHost {
|
|
581
|
+
host;
|
|
582
|
+
installs;
|
|
583
|
+
installer;
|
|
584
|
+
find;
|
|
585
|
+
installing = new Set;
|
|
586
|
+
queue = Promise.resolve();
|
|
587
|
+
constructor(host, options = {}) {
|
|
588
|
+
this.host = host;
|
|
589
|
+
this.installs = options.installs ?? AgentInstalls.of(host.agentDir);
|
|
590
|
+
this.installer = options.installer ?? new AgentInstaller(this.installs);
|
|
591
|
+
this.find = options.find ?? Programs.find;
|
|
592
|
+
}
|
|
593
|
+
async list() {
|
|
594
|
+
const settings = this.settings();
|
|
595
|
+
const agents = AgentCatalog.agents(settings, this.find, this.installs);
|
|
596
|
+
const node = await this.installer.source().catch(() => "none");
|
|
597
|
+
return {
|
|
598
|
+
node,
|
|
599
|
+
agents: AgentCatalog.presets().map((preset) => {
|
|
600
|
+
const offered = agents.find((agent) => agent.id === preset.id);
|
|
601
|
+
const own = !!preset.programs.find((program) => this.find(program));
|
|
602
|
+
const state = settings?.[preset.id] === false ? "off" : own ? "installed" : this.installs.bin(preset.install.bin) ? "ta" : "missing";
|
|
603
|
+
return {
|
|
604
|
+
id: preset.id,
|
|
605
|
+
name: preset.name,
|
|
606
|
+
state,
|
|
607
|
+
...state === "ta" ? { version: this.installs.version(preset.install.packages[0]) } : {},
|
|
608
|
+
...offered?.login && state !== "off" && state !== "missing" ? { login: offered.login } : {},
|
|
609
|
+
...this.installing.has(preset.id) ? { installing: true } : {}
|
|
610
|
+
};
|
|
611
|
+
})
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
install(id) {
|
|
615
|
+
const preset = AgentCatalog.presets().find((candidate) => candidate.id === id);
|
|
616
|
+
if (!preset) {
|
|
617
|
+
return Promise.reject(new Error(text("unknownAgent", String(id))));
|
|
618
|
+
}
|
|
619
|
+
if (this.installing.has(id)) {
|
|
620
|
+
return this.queue.then(() => this.list());
|
|
621
|
+
}
|
|
622
|
+
this.installing.add(id);
|
|
623
|
+
this.changed();
|
|
624
|
+
const done = this.queue.then(() => this.installOne(preset));
|
|
625
|
+
this.queue = done.catch(() => {
|
|
626
|
+
return;
|
|
627
|
+
});
|
|
628
|
+
return done;
|
|
629
|
+
}
|
|
630
|
+
async installOne(preset) {
|
|
631
|
+
try {
|
|
632
|
+
await this.installer.install(preset.install);
|
|
633
|
+
} finally {
|
|
634
|
+
this.installing.delete(preset.id);
|
|
635
|
+
await this.changed();
|
|
636
|
+
}
|
|
637
|
+
AgentRefresh.run();
|
|
638
|
+
return this.list();
|
|
639
|
+
}
|
|
640
|
+
dispose() {
|
|
641
|
+
this.installing.clear();
|
|
642
|
+
}
|
|
643
|
+
changed() {
|
|
644
|
+
return this.host.setExtensionData(AGENTS_NAME, AgentsKeys.INSTALLING, [...this.installing]).catch(() => {
|
|
645
|
+
return;
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
settings() {
|
|
649
|
+
try {
|
|
650
|
+
return JSON.parse(readFileSync3(join5(this.host.agentDir, "settings.json"), "utf8")).agents;
|
|
651
|
+
} catch {
|
|
652
|
+
return;
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
function activate(host) {
|
|
657
|
+
return new AgentsHost(host);
|
|
658
|
+
}
|
|
659
|
+
export {
|
|
660
|
+
activate as default,
|
|
661
|
+
AgentsHost
|
|
662
|
+
};
|