@celljs/ta-agents 4.8.0 → 4.9.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 +2 -2
- package/README.md +2 -2
- package/extension/index.mjs +61 -13
- package/package.json +11 -3
- package/fixtures/fake-acp-agent.mjs +0 -183
- package/src/acp-agent.ts +0 -239
- package/src/acp-protocol.ts +0 -135
- package/src/agent-bindings.ts +0 -147
- package/src/agent-catalog.spec.ts +0 -56
- package/src/agent-catalog.ts +0 -176
- package/src/agent-prompt.spec.ts +0 -44
- package/src/agent-prompt.ts +0 -83
- package/src/agent-session.spec.ts +0 -45
- package/src/agent-session.ts +0 -252
- package/src/agent-streamer.ts +0 -196
- package/src/agent-tool.spec.ts +0 -34
- package/src/agent-tool.ts +0 -95
- package/src/agent-turn.spec.ts +0 -122
- package/src/agent-turn.ts +0 -400
- package/src/extension.spec.ts +0 -327
- package/src/extension.ts +0 -273
- package/src/json-rpc.spec.ts +0 -72
- package/src/json-rpc.ts +0 -166
- package/src/message-stream.ts +0 -139
- package/src/pi-types.ts +0 -22
- package/src/texts.spec.ts +0 -11
- package/src/texts.ts +0 -42
package/README.en.md
CHANGED
|
@@ -32,7 +32,7 @@ Pick one in the model list of the + on the left of the input box, an agent's mod
|
|
|
32
32
|
}
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
Fields: `name`, `command`, `args`, `env`, `login` (the sign-in command to tell the user), `permissions`, `options` (the agent's settings of a new session, by the id or category of the option, such as `{ "mode": "default" }`), `clientMeta` (`_meta` of the client's capabilities in `initialize`). `env` and `options` of a built-in agent merge with those of the settings. `false` turns a built-in agent off. A built-in agent named in the settings is offered even when its program is not found, so it can run another one: the adapters read `CLAUDE_CODE_EXECUTABLE` (Claude Code's executable) and `CODEX_PATH` (the `codex` command), for example for wrappers built on the same code:
|
|
35
|
+
Fields: `name`, `command`, `args`, `env`, `login` (the sign-in command to tell the user), `permissions`, `options` (the agent's settings of a new session, by the id or category of the option, such as `{ "mode": "default" }`), `clientMeta` (`_meta` of the client's capabilities in `initialize`), `processPerSession` (a process for each pi session, for an agent whose process serves one session). `env` and `options` of a built-in agent merge with those of the settings. `false` turns a built-in agent off. A built-in agent named in the settings is offered even when its program is not found, so it can run another one: the adapters read `CLAUDE_CODE_EXECUTABLE` (Claude Code's executable) and `CODEX_PATH` (the `codex` command), for example for wrappers built on the same code:
|
|
36
36
|
|
|
37
37
|
```json
|
|
38
38
|
{
|
|
@@ -48,7 +48,7 @@ Fields: `name`, `command`, `args`, `env`, `login` (the sign-in command to tell t
|
|
|
48
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.
|
|
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
|
-
- **Processes**: the sessions of one agent in one directory share a process
|
|
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.
|
|
52
52
|
- **When the agent goes on by itself**: after a background task, an agent may start a turn on its own (CodeBuddy Code starts it with `unsolicitedTurn` in a `session_info_update` and ends it with `session_end`; other agents give no end, and the turn ends after 3 quiet seconds). The extension sends pi a message that is not shown, which starts a pi turn that shows it.
|
|
53
53
|
- **When the user talks while the agent works**: ACP has no way to steer a turn, so it is kept and sent as the next turn as soon as this one ends. Stopping sends `session/cancel`; the next turn waits for the agent to end the cancelled one (5 seconds at most).
|
|
54
54
|
- **Permissions**: as in pi, nothing is held back: a permission request of the agent is answered "allow once". With `permissions: "ask"` the user picks in the UI. The agent's own permission mode (CodeBuddy Code's settings, for example) still applies.
|
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@ CodeBuddy Code 和在它自己的终端里一样:用 PTC(代码模式,工
|
|
|
32
32
|
}
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
字段:`name`、`command`、`args`、`env`、`login`(提示用户执行的登录命令)、`permissions`、`options`(新会话的 Agent 设置,按选项的 id 或类别,如 `{ "mode": "default" }`)、`clientMeta`(`initialize` 里客户端能力的 `_meta
|
|
35
|
+
字段:`name`、`command`、`args`、`env`、`login`(提示用户执行的登录命令)、`permissions`、`options`(新会话的 Agent 设置,按选项的 id 或类别,如 `{ "mode": "default" }`)、`clientMeta`(`initialize` 里客户端能力的 `_meta`)、`processPerSession`(每个 pi 会话单独一个进程,用于一个进程只服务一个会话的 Agent)。内置 Agent 的 `env`、`options` 与设置里的合并。`false` 关掉一个内置 Agent。设置里写了某个内置 Agent,即使没找到它的程序也会提供,适合改用别的程序:适配器认环境变量 `CLAUDE_CODE_EXECUTABLE`(Claude Code 的可执行文件)和 `CODEX_PATH`(`codex` 命令),比如用同源的包装版:
|
|
36
36
|
|
|
37
37
|
```json
|
|
38
38
|
{
|
|
@@ -48,7 +48,7 @@ CodeBuddy Code 和在它自己的终端里一样:用 PTC(代码模式,工
|
|
|
48
48
|
- **一轮怎么走**:pi 每次请求模型时,只把用户自上次回答以来说的话发给 Agent(`session/prompt`),因为 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
|
-
- **进程**:同一个 Agent
|
|
51
|
+
- **进程**:同一个 Agent 在同一个目录下的所有会话共用一个进程;按目录分开,是因为 CodeBuddy Code 在启动目录里执行命令,不看会话的目录。CodeBuddy Code 例外,每个对话单独一个进程(约 650 MB),对话关闭时停掉:它的一个进程只回答最后新建或加载的会话,其他会话发消息会一直等不到回复,或在那个会话忙时立刻空着结束。10 分钟没有任何一轮就停掉进程,下次用时再启动并加载会话。只在有请求等待回复时保持 pi 进程不退出,所以 `pi -p` 回答完就退出。
|
|
52
52
|
- **Agent 自己继续时**:后台任务结束后,Agent 可能自己开始一轮(CodeBuddy Code 用 `session_info_update` 的 `unsolicitedTurn` 开始、`session_end` 收尾;其他 Agent 没有明确的结束标志,就在 3 秒没有消息后收尾)。扩展发一条不显示的消息让 pi 开一轮,把这一轮显示出来。
|
|
53
53
|
- **用户在 Agent 干活时说话**:ACP 没有“插话”,所以先存下,这一轮结束后马上作为下一轮发出。停止(中止)会发 `session/cancel`,下一轮会等 Agent 把被取消的这轮收尾(最多 5 秒)。
|
|
54
54
|
- **权限**:和 pi 一样默认不拦:Agent 请求权限时选“允许一次”。设置 `permissions: "ask"` 后,改为在界面上让用户选。Agent 自己的权限模式(例如 CodeBuddy Code 的配置)照常生效。
|
package/extension/index.mjs
CHANGED
|
@@ -422,6 +422,10 @@ class AgentSession {
|
|
|
422
422
|
case "tool_call":
|
|
423
423
|
case "tool_call_update":
|
|
424
424
|
case "plan":
|
|
425
|
+
if (!this.turn && this.abandoned && !this.abandoned.ended) {
|
|
426
|
+
this.abandoned.update(update);
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
425
429
|
if (!this.turn) {
|
|
426
430
|
this.startUnsolicited(true);
|
|
427
431
|
}
|
|
@@ -653,7 +657,11 @@ class JsonRpcConnection {
|
|
|
653
657
|
}
|
|
654
658
|
}
|
|
655
659
|
} else if (message.id === undefined || message.id === null) {
|
|
656
|
-
|
|
660
|
+
try {
|
|
661
|
+
this.onNotification(message.method, message.params);
|
|
662
|
+
} catch (error) {
|
|
663
|
+
console.error(error);
|
|
664
|
+
}
|
|
657
665
|
} else {
|
|
658
666
|
this.answer(message.id, message.method, message.params);
|
|
659
667
|
}
|
|
@@ -901,11 +909,9 @@ var BINDING_ENTRY = "ta-agents.session";
|
|
|
901
909
|
var POOL = Symbol.for("ta-agents.pool");
|
|
902
910
|
var AgentPool;
|
|
903
911
|
((AgentPool) => {
|
|
904
|
-
function of(config, cwd) {
|
|
905
|
-
const
|
|
906
|
-
const
|
|
907
|
-
global[POOL] = pool;
|
|
908
|
-
const key = JSON.stringify([config.command, config.args, config.env ?? {}, config.clientMeta ?? {}, cwd]);
|
|
912
|
+
function of(config, cwd, owner) {
|
|
913
|
+
const pool = all();
|
|
914
|
+
const key = keyOf(config, cwd, owner);
|
|
909
915
|
let agent = pool.get(key);
|
|
910
916
|
if (!agent) {
|
|
911
917
|
agent = new AcpAgent(config.name, { command: config.command, args: config.args, env: config.env, clientMeta: config.clientMeta, cwd });
|
|
@@ -914,6 +920,25 @@ var AgentPool;
|
|
|
914
920
|
return agent;
|
|
915
921
|
}
|
|
916
922
|
AgentPool.of = of;
|
|
923
|
+
function release(config, cwd, owner) {
|
|
924
|
+
if (!config.processPerSession) {
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
const pool = all();
|
|
928
|
+
const key = keyOf(config, cwd, owner);
|
|
929
|
+
pool.get(key)?.stop();
|
|
930
|
+
pool.delete(key);
|
|
931
|
+
}
|
|
932
|
+
AgentPool.release = release;
|
|
933
|
+
function keyOf(config, cwd, owner) {
|
|
934
|
+
return JSON.stringify([config.command, config.args, config.env ?? {}, config.clientMeta ?? {}, cwd, config.processPerSession ? owner ?? "" : ""]);
|
|
935
|
+
}
|
|
936
|
+
function all() {
|
|
937
|
+
const global = globalThis;
|
|
938
|
+
const pool = global[POOL] ?? create();
|
|
939
|
+
global[POOL] = pool;
|
|
940
|
+
return pool;
|
|
941
|
+
}
|
|
917
942
|
function create() {
|
|
918
943
|
const pool = new Map;
|
|
919
944
|
process.once("exit", () => {
|
|
@@ -929,13 +954,17 @@ class AgentBindings {
|
|
|
929
954
|
save;
|
|
930
955
|
prepare;
|
|
931
956
|
agentOf;
|
|
957
|
+
release;
|
|
932
958
|
cwd = process.cwd();
|
|
959
|
+
owner;
|
|
933
960
|
ids = new Map;
|
|
934
961
|
opened = new Map;
|
|
935
|
-
|
|
962
|
+
used = new Map;
|
|
963
|
+
constructor(save, prepare, agentOf = AgentPool.of, release = AgentPool.release) {
|
|
936
964
|
this.save = save;
|
|
937
965
|
this.prepare = prepare;
|
|
938
966
|
this.agentOf = agentOf;
|
|
967
|
+
this.release = release;
|
|
939
968
|
}
|
|
940
969
|
restore(entries, fork) {
|
|
941
970
|
this.ids.clear();
|
|
@@ -958,7 +987,8 @@ class AgentBindings {
|
|
|
958
987
|
return this.ids.get(agent);
|
|
959
988
|
}
|
|
960
989
|
open(config, signal) {
|
|
961
|
-
|
|
990
|
+
this.used.set(JSON.stringify([config.id, this.cwd, this.owner]), { config, cwd: this.cwd, owner: this.owner });
|
|
991
|
+
const agent = this.agentOf(config, this.cwd, this.owner);
|
|
962
992
|
let opened = this.opened.get(config.id);
|
|
963
993
|
if (opened) {
|
|
964
994
|
opened = opened.then((value) => agent.running && agent.sessions.get(value.session.id) === value.session ? { ...value, fresh: false } : this.connect(config, agent, signal));
|
|
@@ -971,6 +1001,10 @@ class AgentBindings {
|
|
|
971
1001
|
}
|
|
972
1002
|
close() {
|
|
973
1003
|
this.opened.clear();
|
|
1004
|
+
for (const { config, cwd, owner } of this.used.values()) {
|
|
1005
|
+
this.release(config, cwd, owner);
|
|
1006
|
+
}
|
|
1007
|
+
this.used.clear();
|
|
974
1008
|
}
|
|
975
1009
|
async connect(config, agent, signal) {
|
|
976
1010
|
if (AgentBindings.isRemote(this.cwd)) {
|
|
@@ -1020,7 +1054,8 @@ var PRESETS = [
|
|
|
1020
1054
|
login: "cbc",
|
|
1021
1055
|
env: { CODEBUDDY_CODE_MODE_ENABLED: "1" },
|
|
1022
1056
|
clientMeta: { "codebuddy.ai": { mainAgentSupport: true, toolModeSupport: true } },
|
|
1023
|
-
options: { mode: "fullAccess" }
|
|
1057
|
+
options: { mode: "fullAccess" },
|
|
1058
|
+
processPerSession: true
|
|
1024
1059
|
},
|
|
1025
1060
|
{
|
|
1026
1061
|
id: "claude-code",
|
|
@@ -1429,7 +1464,13 @@ class AgentStreamer {
|
|
|
1429
1464
|
stream(config, model, context, options) {
|
|
1430
1465
|
const stream = new MessageStream;
|
|
1431
1466
|
const builder = new MessageBuilder(stream, model);
|
|
1432
|
-
this.run(config, model, context, options, builder).catch((error) =>
|
|
1467
|
+
this.run(config, model, context, options, builder).catch((error) => {
|
|
1468
|
+
if (options?.signal?.aborted) {
|
|
1469
|
+
builder.error("aborted", "Aborted");
|
|
1470
|
+
} else {
|
|
1471
|
+
builder.error("error", AgentStreamer.describe(config, error));
|
|
1472
|
+
}
|
|
1473
|
+
});
|
|
1433
1474
|
return stream;
|
|
1434
1475
|
}
|
|
1435
1476
|
async run(config, model, context, options, builder) {
|
|
@@ -1656,22 +1697,25 @@ function activate(pi, options = {}) {
|
|
|
1656
1697
|
return option ? { outcome: "selected", optionId: option.optionId } : { outcome: "cancelled" };
|
|
1657
1698
|
},
|
|
1658
1699
|
title: (title) => {
|
|
1659
|
-
if (!pi.getSessionName()) {
|
|
1700
|
+
if (context && !pi.getSessionName()) {
|
|
1660
1701
|
pi.setSessionName(title);
|
|
1661
1702
|
}
|
|
1662
1703
|
},
|
|
1663
1704
|
activity: () => {
|
|
1664
|
-
if (context
|
|
1705
|
+
if (context && context.model?.provider === config.id) {
|
|
1665
1706
|
pi.sendMessage({ customType: ACTIVITY_ENTRY, content: ACTIVITY_TEXT, display: false }, { triggerTurn: true });
|
|
1666
1707
|
}
|
|
1667
1708
|
},
|
|
1668
1709
|
write: (path, before) => {
|
|
1710
|
+
if (!context) {
|
|
1711
|
+
return;
|
|
1712
|
+
}
|
|
1669
1713
|
const file = { path: resolve(context?.cwd ?? bindings.cwd, path), ...before !== undefined ? { before } : {} };
|
|
1670
1714
|
pi.events.emit(TA_FILE_WRITE_EVENT, file);
|
|
1671
1715
|
}
|
|
1672
1716
|
};
|
|
1673
1717
|
learn(config, session);
|
|
1674
|
-
}, options.agentOf);
|
|
1718
|
+
}, options.agentOf, options.release);
|
|
1675
1719
|
const streamer = new AgentStreamer({
|
|
1676
1720
|
open: (config, signal) => bindings.open(config, signal),
|
|
1677
1721
|
calls,
|
|
@@ -1696,6 +1740,10 @@ function activate(pi, options = {}) {
|
|
|
1696
1740
|
}
|
|
1697
1741
|
sessionId = ctx.sessionManager.getSessionId();
|
|
1698
1742
|
Sessions.set(sessionId, self);
|
|
1743
|
+
if (bindings.owner !== sessionId) {
|
|
1744
|
+
bindings.close();
|
|
1745
|
+
bindings.owner = sessionId;
|
|
1746
|
+
}
|
|
1699
1747
|
bindings.cwd = ctx.cwd;
|
|
1700
1748
|
bindings.restore(ctx.sessionManager.getBranch(), event.reason === "fork");
|
|
1701
1749
|
const settings = pi.getSettings().agents;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-agents",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.9.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"
|
|
@@ -17,8 +17,7 @@
|
|
|
17
17
|
"files": [
|
|
18
18
|
"esm",
|
|
19
19
|
"extension",
|
|
20
|
-
"
|
|
21
|
-
"src"
|
|
20
|
+
"!**/*.map"
|
|
22
21
|
],
|
|
23
22
|
"scripts": {
|
|
24
23
|
"build": "tsc -b && bun build src/extension.ts --format esm --target node --outfile extension/index.mjs",
|
|
@@ -34,6 +33,15 @@
|
|
|
34
33
|
"access": "public"
|
|
35
34
|
},
|
|
36
35
|
"pi": {
|
|
36
|
+
"title": {
|
|
37
|
+
"en": "Other coding agents",
|
|
38
|
+
"zh-CN": "其他编程 Agent"
|
|
39
|
+
},
|
|
40
|
+
"summary": {
|
|
41
|
+
"en": "Pick Claude Code, Codex, CodeBuddy Code or another ACP agent as the model of a conversation.",
|
|
42
|
+
"zh-CN": "把 Claude Code、Codex、CodeBuddy Code 等 Agent 当作模型,在对话里选用。"
|
|
43
|
+
},
|
|
44
|
+
"icon": "SquareTerminal",
|
|
37
45
|
"extensions": [
|
|
38
46
|
"./extension/index.mjs"
|
|
39
47
|
]
|
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
// An ACP agent for the tests of @celljs/ta-agents: what it does depends on the words of the prompt.
|
|
2
|
-
// Sessions are kept in FAKE_ACP_STATE (a directory), so that another process can load them.
|
|
3
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
4
|
-
import { join } from 'node:path';
|
|
5
|
-
|
|
6
|
-
const state = process.env.FAKE_ACP_STATE;
|
|
7
|
-
if (state) {
|
|
8
|
-
mkdirSync(state, { recursive: true });
|
|
9
|
-
}
|
|
10
|
-
const sessions = new Map();
|
|
11
|
-
const cancelled = new Set();
|
|
12
|
-
let nextId = 0;
|
|
13
|
-
const waiting = new Map();
|
|
14
|
-
|
|
15
|
-
const send = message => process.stdout.write(JSON.stringify({ jsonrpc: '2.0', ...message }) + '\n');
|
|
16
|
-
const update = (sessionId, value) => send({ method: 'session/update', params: { sessionId, update: value } });
|
|
17
|
-
const ask = (method, params) =>
|
|
18
|
-
new Promise(resolve => {
|
|
19
|
-
const id = 'c' + ++nextId;
|
|
20
|
-
waiting.set(id, resolve);
|
|
21
|
-
send({ id, method, params });
|
|
22
|
-
});
|
|
23
|
-
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
|
24
|
-
const save = session => state && writeFileSync(join(state, session.id + '.json'), JSON.stringify(session));
|
|
25
|
-
const options = session => [
|
|
26
|
-
{ type: 'select', id: 'model', name: 'Model', category: 'model', currentValue: session.model, options: [{ value: 'fast', name: 'Fast' }, { value: 'smart', name: 'Smart' }] },
|
|
27
|
-
{ type: 'select', id: 'effort', name: 'Effort', category: 'thought_level', currentValue: session.effort, options: [{ value: 'low', name: 'Low' }, { value: 'high', name: 'High' }, { value: 'max', name: 'Max' }] },
|
|
28
|
-
];
|
|
29
|
-
|
|
30
|
-
async function prompt(session, blocks) {
|
|
31
|
-
const text = blocks.map(block => (block.type === 'text' ? block.text : '[' + block.type + ']')).join(' ');
|
|
32
|
-
session.prompts.push(text);
|
|
33
|
-
save(session);
|
|
34
|
-
const say = value => update(session.id, { sessionUpdate: 'agent_message_chunk', content: { type: 'text', text: value } });
|
|
35
|
-
if (text.startsWith('/silent')) {
|
|
36
|
-
return 'end_turn';
|
|
37
|
-
}
|
|
38
|
-
if (text.includes('auth')) {
|
|
39
|
-
throw Object.assign(new Error('Authentication required'), { code: -32000 });
|
|
40
|
-
}
|
|
41
|
-
if (text.includes('tools')) {
|
|
42
|
-
update(session.id, { sessionUpdate: 'tool_call', toolCallId: 't1', title: 'Bash', kind: 'other', status: 'in_progress', rawInput: {}, _meta: { 'fake/toolName': 'Bash' } });
|
|
43
|
-
update(session.id, { sessionUpdate: 'tool_call_update', toolCallId: 't1', rawInput: { command: 'ec' } });
|
|
44
|
-
update(session.id, { sessionUpdate: 'tool_call', toolCallId: 't1', title: '`echo hi`', kind: 'execute', status: 'pending', rawInput: { command: 'echo hi' } });
|
|
45
|
-
update(session.id, { sessionUpdate: 'tool_call', toolCallId: 't2', title: 'Read a.txt', kind: 'read', status: 'pending', locations: [{ path: '/w/a.txt' }] });
|
|
46
|
-
await sleep(400);
|
|
47
|
-
update(session.id, { sessionUpdate: 'tool_call_update', toolCallId: 't1', status: 'completed', rawOutput: { type: 'text', text: 'hi' } });
|
|
48
|
-
update(session.id, { sessionUpdate: 'tool_call_update', toolCallId: 't2', status: 'failed', content: [{ type: 'content', content: { type: 'text', text: 'no such file' } }] });
|
|
49
|
-
await sleep(20);
|
|
50
|
-
update(session.id, { sessionUpdate: 'usage_update', used: 1200, size: 100000, _meta: { usage: { prompt_tokens: 1000, completion_tokens: 20, cached_tokens: 400 } } });
|
|
51
|
-
say('done');
|
|
52
|
-
return 'end_turn';
|
|
53
|
-
}
|
|
54
|
-
if (text.includes('edits')) {
|
|
55
|
-
// As CodeBuddy Code: it names the file it edits, then writes it with its own tool. As Claude Code: a diff.
|
|
56
|
-
const file = join(session.cwd, 'notes.txt');
|
|
57
|
-
update(session.id, { sessionUpdate: 'tool_call', toolCallId: 'e1', title: 'Edit', kind: 'edit', status: 'pending', rawInput: null });
|
|
58
|
-
update(session.id, { sessionUpdate: 'tool_call_update', toolCallId: 'e1', status: 'pending', rawInput: { file_path: file } });
|
|
59
|
-
await sleep(50);
|
|
60
|
-
writeFileSync(file, readFileSync(file, 'utf8').replace('two', 'TWO'));
|
|
61
|
-
update(session.id, { sessionUpdate: 'tool_call_update', toolCallId: 'e1', status: 'completed', content: [{ type: 'content', content: { type: 'text', text: 'Edited' } }] });
|
|
62
|
-
const created = join(session.cwd, 'made.txt');
|
|
63
|
-
writeFileSync(created, 'made\n');
|
|
64
|
-
update(session.id, { sessionUpdate: 'tool_call', toolCallId: 'e2', title: 'Write made.txt', kind: 'edit', status: 'completed', content: [{ type: 'diff', path: created, oldText: null, newText: 'made\n' }] });
|
|
65
|
-
say('edited');
|
|
66
|
-
return 'end_turn';
|
|
67
|
-
}
|
|
68
|
-
if (text.includes('slow')) {
|
|
69
|
-
say('working');
|
|
70
|
-
for (let i = 0; i < 100 && !cancelled.has(session.id); i++) {
|
|
71
|
-
await sleep(20);
|
|
72
|
-
}
|
|
73
|
-
cancelled.delete(session.id);
|
|
74
|
-
return 'cancelled';
|
|
75
|
-
}
|
|
76
|
-
if (text.includes('permission')) {
|
|
77
|
-
const answer = await ask('session/request_permission', {
|
|
78
|
-
sessionId: session.id,
|
|
79
|
-
toolCall: { toolCallId: 'p1', title: 'Write b.txt' },
|
|
80
|
-
options: [
|
|
81
|
-
{ optionId: 'yes', name: 'Allow', kind: 'allow_once' },
|
|
82
|
-
{ optionId: 'no', name: 'Deny', kind: 'reject_once' },
|
|
83
|
-
],
|
|
84
|
-
});
|
|
85
|
-
say('permission: ' + (answer.outcome.optionId ?? answer.outcome.outcome));
|
|
86
|
-
return 'end_turn';
|
|
87
|
-
}
|
|
88
|
-
if (text.includes('background')) {
|
|
89
|
-
say('started');
|
|
90
|
-
setTimeout(() => {
|
|
91
|
-
update(session.id, { sessionUpdate: 'session_info_update', _meta: { 'fake/unsolicitedTurn': { requestId: 'r1', reason: 'background_drain' } } });
|
|
92
|
-
say('background finished');
|
|
93
|
-
update(session.id, { sessionUpdate: 'session_end' });
|
|
94
|
-
}, 150);
|
|
95
|
-
return 'end_turn';
|
|
96
|
-
}
|
|
97
|
-
if (text.includes('phases')) {
|
|
98
|
-
// As CodeBuddy Code: idle once done, and the answer after it named the session.
|
|
99
|
-
const request = 'r' + session.prompts.length;
|
|
100
|
-
const phase = value => update(session.id, { sessionUpdate: 'session_info_update', _meta: { 'fake/agentPhase': { phase: value }, 'fake/requestId': request } });
|
|
101
|
-
phase('idle');
|
|
102
|
-
phase('model_streaming');
|
|
103
|
-
say('quick');
|
|
104
|
-
phase('idle');
|
|
105
|
-
// Still working on this request after it said idle (as a compaction does), then answering late.
|
|
106
|
-
setTimeout(() => {
|
|
107
|
-
phase('model_streaming');
|
|
108
|
-
phase('idle');
|
|
109
|
-
}, 300);
|
|
110
|
-
await sleep(1500);
|
|
111
|
-
update(session.id, { sessionUpdate: 'session_info_update', title: 'Named late' });
|
|
112
|
-
return 'end_turn';
|
|
113
|
-
}
|
|
114
|
-
if (text.includes('where')) {
|
|
115
|
-
say('cwd ' + process.cwd() + ' session ' + session.cwd);
|
|
116
|
-
return 'end_turn';
|
|
117
|
-
}
|
|
118
|
-
say('model ' + session.model + ' effort ' + session.effort + ' prompts ' + session.prompts.length + ': ' + text);
|
|
119
|
-
update(session.id, { sessionUpdate: 'session_info_update', title: 'Fake title' });
|
|
120
|
-
return 'end_turn';
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
async function handle(method, params) {
|
|
124
|
-
switch (method) {
|
|
125
|
-
case 'initialize':
|
|
126
|
-
return { protocolVersion: 1, agentCapabilities: { loadSession: true, promptCapabilities: { image: true } }, authMethods: [] };
|
|
127
|
-
case 'session/new': {
|
|
128
|
-
const session = { id: 's' + Date.now() + Math.random().toString(16).slice(2, 6), cwd: params.cwd, model: 'fast', effort: 'low', prompts: [] };
|
|
129
|
-
sessions.set(session.id, session);
|
|
130
|
-
save(session);
|
|
131
|
-
return { sessionId: session.id, configOptions: options(session) };
|
|
132
|
-
}
|
|
133
|
-
case 'session/load': {
|
|
134
|
-
const file = state && join(state, params.sessionId + '.json');
|
|
135
|
-
if (!file || !existsSync(file)) {
|
|
136
|
-
throw Object.assign(new Error('Session not found'), { code: -32602 });
|
|
137
|
-
}
|
|
138
|
-
const session = JSON.parse(readFileSync(file, 'utf8'));
|
|
139
|
-
sessions.set(session.id, session);
|
|
140
|
-
for (const text of session.prompts) {
|
|
141
|
-
update(session.id, { sessionUpdate: 'user_message_chunk', content: { type: 'text', text } });
|
|
142
|
-
update(session.id, { sessionUpdate: 'agent_message_chunk', content: { type: 'text', text: 'replayed' } });
|
|
143
|
-
}
|
|
144
|
-
return { configOptions: options(session) };
|
|
145
|
-
}
|
|
146
|
-
case 'session/set_config_option': {
|
|
147
|
-
const session = sessions.get(params.sessionId);
|
|
148
|
-
session[params.configId] = params.value;
|
|
149
|
-
save(session);
|
|
150
|
-
return { configOptions: options(session) };
|
|
151
|
-
}
|
|
152
|
-
case 'session/prompt':
|
|
153
|
-
return { stopReason: await prompt(sessions.get(params.sessionId), params.prompt) };
|
|
154
|
-
default:
|
|
155
|
-
throw Object.assign(new Error('Method not found'), { code: -32601 });
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
let buffer = '';
|
|
160
|
-
process.stdin.setEncoding('utf8');
|
|
161
|
-
process.stdin.on('data', chunk => {
|
|
162
|
-
buffer += chunk;
|
|
163
|
-
let end;
|
|
164
|
-
while ((end = buffer.indexOf('\n')) >= 0) {
|
|
165
|
-
const line = buffer.slice(0, end);
|
|
166
|
-
buffer = buffer.slice(end + 1);
|
|
167
|
-
if (!line.trim()) continue;
|
|
168
|
-
const message = JSON.parse(line);
|
|
169
|
-
if (message.method === undefined) {
|
|
170
|
-
waiting.get(message.id)?.(message.result);
|
|
171
|
-
waiting.delete(message.id);
|
|
172
|
-
} else if (message.id === undefined) {
|
|
173
|
-
if (message.method === 'session/cancel') cancelled.add(message.params.sessionId);
|
|
174
|
-
} else {
|
|
175
|
-
handle(message.method, message.params).then(
|
|
176
|
-
result => send({ id: message.id, result }),
|
|
177
|
-
error => send({ id: message.id, error: { code: error.code ?? -32603, message: error.message } }),
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
process.stdin.on('end', () => process.exit(0));
|
|
183
|
-
process.stdout.write('not json: a program that logs on stdout\n');
|