@celljs/ta-agents 0.0.0-stage → 4.8.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 +68 -0
- package/README.md +67 -2
- package/extension/index.mjs +1746 -0
- package/fixtures/fake-acp-agent.mjs +183 -0
- package/package.json +51 -4
- package/src/acp-agent.ts +239 -0
- package/src/acp-protocol.ts +135 -0
- package/src/agent-bindings.ts +147 -0
- package/src/agent-catalog.spec.ts +56 -0
- package/src/agent-catalog.ts +176 -0
- package/src/agent-prompt.spec.ts +44 -0
- package/src/agent-prompt.ts +83 -0
- package/src/agent-session.spec.ts +45 -0
- package/src/agent-session.ts +252 -0
- package/src/agent-streamer.ts +196 -0
- package/src/agent-tool.spec.ts +34 -0
- package/src/agent-tool.ts +95 -0
- package/src/agent-turn.spec.ts +122 -0
- package/src/agent-turn.ts +400 -0
- package/src/extension.spec.ts +327 -0
- package/src/extension.ts +273 -0
- package/src/json-rpc.spec.ts +72 -0
- package/src/json-rpc.ts +166 -0
- package/src/message-stream.ts +139 -0
- package/src/pi-types.ts +22 -0
- package/src/texts.spec.ts +11 -0
- package/src/texts.ts +42 -0
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import { afterAll, describe, expect, it } from 'bun:test';
|
|
2
|
+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { homedir, tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { AcpAgent } from './acp-agent';
|
|
6
|
+
import type { AgentConfig } from './agent-catalog';
|
|
7
|
+
import { ModelCache } from './agent-catalog';
|
|
8
|
+
import { ACTIVITY_TEXT } from './agent-prompt';
|
|
9
|
+
import { AgentTool } from './agent-tool';
|
|
10
|
+
import { activate, agentDir } from './extension';
|
|
11
|
+
import type { AssistantMessage, ExtensionAPI, Message, Model, ToolDefinition } from './pi-types';
|
|
12
|
+
|
|
13
|
+
const FIXTURE = join(__dirname, '..', 'fixtures', 'fake-acp-agent.mjs');
|
|
14
|
+
const root = mkdtempSync(join(tmpdir(), 'ta-agents-'));
|
|
15
|
+
const work = mkdtempSync(join(tmpdir(), 'ta-agents-work-'));
|
|
16
|
+
const agents = new Map<string, AcpAgent>();
|
|
17
|
+
|
|
18
|
+
afterAll(() => {
|
|
19
|
+
for (const agent of agents.values()) {
|
|
20
|
+
agent.stop();
|
|
21
|
+
}
|
|
22
|
+
rmSync(root, { recursive: true, force: true });
|
|
23
|
+
rmSync(work, { recursive: true, force: true });
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
type Handler = (event: unknown, ctx: unknown) => unknown;
|
|
27
|
+
|
|
28
|
+
/** The part of pi the extension uses, and pi's agent loop as far as a provider sees it. */
|
|
29
|
+
class FakePi {
|
|
30
|
+
readonly providers = new Map<
|
|
31
|
+
string,
|
|
32
|
+
{
|
|
33
|
+
api: string;
|
|
34
|
+
models: { id: string; reasoning: boolean }[];
|
|
35
|
+
streamSimple: (...args: unknown[]) => AsyncIterable<{ type: string }> & { result(): Promise<unknown> };
|
|
36
|
+
}
|
|
37
|
+
>();
|
|
38
|
+
readonly tools = new Map<string, ToolDefinition>();
|
|
39
|
+
readonly handlers = new Map<string, Handler[]>();
|
|
40
|
+
readonly entries: unknown[] = [];
|
|
41
|
+
readonly sent: { message: { content: string }; options?: { triggerTurn?: boolean } }[] = [];
|
|
42
|
+
readonly userMessages: string[] = [];
|
|
43
|
+
active = ['read'];
|
|
44
|
+
settings: Record<string, unknown> = {};
|
|
45
|
+
sessionName?: string;
|
|
46
|
+
answer = 'Allow';
|
|
47
|
+
current?: Model;
|
|
48
|
+
readonly sessionId = `pi-${Math.random().toString(16).slice(2)}`;
|
|
49
|
+
|
|
50
|
+
constructor(entries: unknown[] = []) {
|
|
51
|
+
this.entries.push(...entries);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get api(): ExtensionAPI {
|
|
55
|
+
return {
|
|
56
|
+
registerProvider: (name: string, config: never) => this.providers.set(name, config),
|
|
57
|
+
unregisterProvider: (name: string) => this.providers.delete(name),
|
|
58
|
+
registerTool: (tool: ToolDefinition) => this.tools.set(tool.name, tool),
|
|
59
|
+
on: (event: string, handler: Handler) => this.handlers.set(event, [...(this.handlers.get(event) ?? []), handler]),
|
|
60
|
+
getActiveTools: () => [...this.active],
|
|
61
|
+
setActiveTools: (names: string[]) => (this.active = names),
|
|
62
|
+
getSettings: () => this.settings,
|
|
63
|
+
appendEntry: (customType: string, data: unknown) => this.entries.push({ type: 'custom', customType, data }),
|
|
64
|
+
sendMessage: (message: never, options: never) => this.sent.push({ message, options }),
|
|
65
|
+
sendUserMessage: (text: string) => this.userMessages.push(text),
|
|
66
|
+
getSessionName: () => this.sessionName,
|
|
67
|
+
setSessionName: (name: string) => (this.sessionName = name),
|
|
68
|
+
} as unknown as ExtensionAPI;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
ctx(model?: Model): unknown {
|
|
72
|
+
const pi = this;
|
|
73
|
+
return {
|
|
74
|
+
cwd: work,
|
|
75
|
+
hasUI: true,
|
|
76
|
+
get model() {
|
|
77
|
+
return model ?? pi.current;
|
|
78
|
+
},
|
|
79
|
+
ui: { select: async () => this.answer, notify: () => undefined },
|
|
80
|
+
sessionManager: { getBranch: () => this.entries, getSessionId: () => this.sessionId },
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async emit(event: string, payload: unknown, model?: Model): Promise<unknown[]> {
|
|
85
|
+
const results = [];
|
|
86
|
+
for (const handler of this.handlers.get(event) ?? []) {
|
|
87
|
+
results.push(await handler(payload, this.ctx(model)));
|
|
88
|
+
}
|
|
89
|
+
return results;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
model(provider: string, id = 'default'): Model {
|
|
93
|
+
const config = this.providers.get(provider)!;
|
|
94
|
+
const model = config.models.find(candidate => candidate.id === id)!;
|
|
95
|
+
return { ...model, provider, api: config.api } as unknown as Model;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async start(model: Model, reason = 'startup'): Promise<void> {
|
|
99
|
+
await this.emit('session_start', { type: 'session_start', reason }, model);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** A pi turn: the provider's messages, and the results of the calls of the agent's tools. */
|
|
103
|
+
async run(model: Model, messages: Message[], given: { signal?: AbortSignal; reasoning?: string; sessionId?: string } = {}): Promise<Message[]> {
|
|
104
|
+
const options = { sessionId: this.sessionId, ...given };
|
|
105
|
+
const added: Message[] = [];
|
|
106
|
+
for (let step = 0; step < 20; step++) {
|
|
107
|
+
const provider = this.providers.get(model.provider)!;
|
|
108
|
+
const stream = provider.streamSimple(model, { messages: [...messages, ...added] }, options);
|
|
109
|
+
const types: string[] = [];
|
|
110
|
+
for await (const event of stream as AsyncIterable<{ type: string }>) {
|
|
111
|
+
types.push(event.type);
|
|
112
|
+
}
|
|
113
|
+
expect(types[0]).toBe('start');
|
|
114
|
+
const message = (await stream.result()) as AssistantMessage;
|
|
115
|
+
added.push(message);
|
|
116
|
+
if (message.stopReason !== 'toolUse') {
|
|
117
|
+
return added;
|
|
118
|
+
}
|
|
119
|
+
const tool = this.tools.get(AgentTool.NAME)!;
|
|
120
|
+
for (const call of message.content.filter(block => block.type === 'toolCall')) {
|
|
121
|
+
const result = await tool.execute(call.id, call.arguments as never, options.signal, undefined, this.ctx() as never).then(
|
|
122
|
+
value => ({ content: value.content, isError: !!value.isError }),
|
|
123
|
+
(error: Error) => ({ content: [{ type: 'text' as const, text: error.message }], isError: true }),
|
|
124
|
+
);
|
|
125
|
+
added.push({ role: 'toolResult', toolCallId: call.id, toolName: call.name, ...result, timestamp: Date.now() } as Message);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
throw new Error('too many steps');
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function user(text: string): Message {
|
|
133
|
+
return { role: 'user', content: text, timestamp: Date.now() };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function text(message: Message | undefined): string {
|
|
137
|
+
return message?.role === 'assistant' ? message.content.map(block => (block.type === 'text' ? block.text : '')).join('') : '';
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const FAKE: AgentConfig = {
|
|
141
|
+
id: 'fake',
|
|
142
|
+
name: 'Fake',
|
|
143
|
+
command: process.execPath,
|
|
144
|
+
args: [FIXTURE],
|
|
145
|
+
env: { FAKE_ACP_STATE: join(root, 'state') },
|
|
146
|
+
login: 'fake login',
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
function agentOf(config: AgentConfig, cwd: string): AcpAgent {
|
|
150
|
+
const key = `${config.id}:${cwd}`;
|
|
151
|
+
let agent = agents.get(key);
|
|
152
|
+
if (!agent) {
|
|
153
|
+
agent = new AcpAgent(config.name, { command: config.command, args: config.args, env: config.env, cwd });
|
|
154
|
+
agents.set(key, agent);
|
|
155
|
+
}
|
|
156
|
+
return agent;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async function setUp(settings: Record<string, unknown> = {}, entries: unknown[] = [], pi = new FakePi(entries)): Promise<{ pi: FakePi; model: Model }> {
|
|
160
|
+
pi.settings = { agents: { fake: { ...FAKE }, ...settings } };
|
|
161
|
+
activate(pi.api, { find: () => undefined, cache: new ModelCache(join(root, `models-${Math.random()}.json`)), agentOf });
|
|
162
|
+
await pi.start(undefined as never);
|
|
163
|
+
const model = pi.model('fake');
|
|
164
|
+
pi.current = model;
|
|
165
|
+
await pi.emit('model_select', { type: 'model_select', model }, model);
|
|
166
|
+
return { pi, model };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
describe('ta-agents', () => {
|
|
170
|
+
it('offers the built-in agents that are installed, and those of the settings', () => {
|
|
171
|
+
const pi = new FakePi();
|
|
172
|
+
activate(pi.api, { find: program => (program === 'codebuddy' ? program : undefined), cache: new ModelCache(join(root, 'none.json')), agentOf });
|
|
173
|
+
expect([...pi.providers.keys()]).toEqual(['codebuddy-code']);
|
|
174
|
+
expect(pi.model('codebuddy-code').provider).toBe('codebuddy-code');
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('streams a turn of the agent, its tool calls as calls of agent_tool', async () => {
|
|
178
|
+
const { pi, model } = await setUp();
|
|
179
|
+
expect(pi.active).toContain(AgentTool.NAME);
|
|
180
|
+
const added = await pi.run(model, [user('use tools')]);
|
|
181
|
+
// Calls started together go in one message, unless the second comes after the first is complete.
|
|
182
|
+
const assistants = added.filter(message => message.role === 'assistant') as AssistantMessage[];
|
|
183
|
+
expect(assistants.slice(0, -1).every(message => message.stopReason === 'toolUse')).toBe(true);
|
|
184
|
+
const calls = assistants.flatMap(message => message.content.filter(block => block.type === 'toolCall'));
|
|
185
|
+
expect(calls.map(block => (block as { arguments: unknown }).arguments)).toEqual([
|
|
186
|
+
{ title: '`echo hi`', kind: 'execute', target: 'echo hi', tool: 'Bash' },
|
|
187
|
+
{ title: 'Read a.txt', kind: 'read', target: '/w/a.txt' },
|
|
188
|
+
]);
|
|
189
|
+
const results = added.filter(message => message.role === 'toolResult');
|
|
190
|
+
expect(results.map(message => [(message as { content: { text: string }[] }).content[0].text, (message as { isError: boolean }).isError])).toEqual([
|
|
191
|
+
['hi', false],
|
|
192
|
+
['no such file', true],
|
|
193
|
+
]);
|
|
194
|
+
const last = assistants[assistants.length - 1];
|
|
195
|
+
expect([last.stopReason, text(last)]).toEqual(['stop', 'done']);
|
|
196
|
+
expect([last.usage.input, last.usage.cacheRead, last.usage.output]).toEqual([600, 400, 20]);
|
|
197
|
+
expect(pi.entries).toEqual([{ type: 'custom', customType: 'ta-agents.session', data: { agent: 'fake', session: expect.any(String) } }]);
|
|
198
|
+
// The agent's models, learned from its session.
|
|
199
|
+
expect(pi.providers.get('fake')!.models.map(candidate => [candidate.id, candidate.reasoning])).toEqual([
|
|
200
|
+
['default', true],
|
|
201
|
+
['fast', true],
|
|
202
|
+
['smart', true],
|
|
203
|
+
]);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("sets the agent's model and thinking level, and its title names the conversation", async () => {
|
|
207
|
+
const { pi } = await setUp();
|
|
208
|
+
const [message] = await pi.run(pi.model('fake'), [user('hi')]);
|
|
209
|
+
expect(text(message)).toBe('model fast effort low prompts 1: hi');
|
|
210
|
+
const [smart] = await pi.run(pi.model('fake', 'smart'), [user('hi'), message, user('again')], { reasoning: 'xhigh' });
|
|
211
|
+
expect(text(smart)).toBe('model smart effort max prompts 2: again');
|
|
212
|
+
expect(pi.sessionName).toBe('Fake title');
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('loads the session of the agent when the conversation opens again, and starts a new one after going back', async () => {
|
|
216
|
+
const { pi, model } = await setUp();
|
|
217
|
+
const [first] = await pi.run(model, [user('one')]);
|
|
218
|
+
for (const agent of agents.values()) {
|
|
219
|
+
agent.stop();
|
|
220
|
+
}
|
|
221
|
+
const again = await setUp({}, pi.entries);
|
|
222
|
+
const [second] = await again.pi.run(again.model, [user('one'), first, user('two')]);
|
|
223
|
+
expect(text(second)).toBe('model fast effort low prompts 2: two');
|
|
224
|
+
await again.pi.emit('session_tree', {});
|
|
225
|
+
const [third] = await again.pi.run(again.model, [user('one'), first, user('three')]);
|
|
226
|
+
expect(text(third)).toContain('prompts 1: <earlier_conversation>');
|
|
227
|
+
expect(text(third)).toContain('User: one');
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("answers each session with its own agent's session, though the provider is shared", async () => {
|
|
231
|
+
const first = await setUp();
|
|
232
|
+
const shared = first.pi.providers;
|
|
233
|
+
// A second session of the process registers the same provider again, in the same model runtime.
|
|
234
|
+
const second = new FakePi();
|
|
235
|
+
(second as unknown as { providers: typeof shared }).providers = shared;
|
|
236
|
+
await setUp({}, [], second);
|
|
237
|
+
await first.pi.run(first.model, [user('one')]);
|
|
238
|
+
await second.run(first.model, [user('two')]);
|
|
239
|
+
const [again] = await first.pi.run(first.model, [user('one'), user('three')].slice(1));
|
|
240
|
+
expect(text(again)).toBe('model fast effort low prompts 2: three');
|
|
241
|
+
expect(first.pi.entries).toHaveLength(1);
|
|
242
|
+
expect(second.entries).toHaveLength(1);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('ends the turn when the agent says it is idle, and waits for its answer before the next prompt', async () => {
|
|
246
|
+
const { pi, model } = await setUp();
|
|
247
|
+
const started = Date.now();
|
|
248
|
+
const [quick] = await pi.run(model, [user('phases')]);
|
|
249
|
+
expect(text(quick)).toBe('quick');
|
|
250
|
+
expect(Date.now() - started).toBeLessThan(1000);
|
|
251
|
+
const [next] = await pi.run(model, [user('phases'), quick, user('hi')]);
|
|
252
|
+
expect(text(next)).toBe('model fast effort low prompts 2: hi');
|
|
253
|
+
expect(pi.sessionName).toBe('Named late');
|
|
254
|
+
// The phases the agent still reported of the first request do not end a later turn.
|
|
255
|
+
const [again] = await pi.run(model, [user('phases'), quick, user('hi'), next, user('phases')]);
|
|
256
|
+
expect(text(again)).toBe('quick');
|
|
257
|
+
const [after] = await pi.run(model, [user('phases'), quick, user('hi'), next, user('phases'), again, user('more')]);
|
|
258
|
+
expect(text(after)).toBe('model fast effort low prompts 4: more');
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it('says when a command of the agent gives nothing back', async () => {
|
|
262
|
+
const { pi, model } = await setUp();
|
|
263
|
+
const [message] = await pi.run(model, [user('/silent now')]);
|
|
264
|
+
expect(text(message)).toContain('`/silent`');
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it('works in the directory of the session', async () => {
|
|
268
|
+
const { pi, model } = await setUp();
|
|
269
|
+
const [message] = await pi.run(model, [user('where')]);
|
|
270
|
+
expect(text(message)).toContain(`session ${work}`);
|
|
271
|
+
expect(text(message)).toMatch(new RegExp(`cwd .*${work.split('/').pop()}`));
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('cancels the turn when pi aborts, and takes the next prompt', async () => {
|
|
275
|
+
const { pi, model } = await setUp();
|
|
276
|
+
const controller = new AbortController();
|
|
277
|
+
setTimeout(() => controller.abort(), 150);
|
|
278
|
+
const [aborted] = await pi.run(model, [user('slow')], { signal: controller.signal });
|
|
279
|
+
expect((aborted as AssistantMessage).stopReason).toBe('aborted');
|
|
280
|
+
const [next] = await pi.run(model, [user('slow'), aborted, user('hi')]);
|
|
281
|
+
expect(text(next)).toBe('model fast effort low prompts 2: hi');
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it("allows the agent's permission requests unless the settings say to ask", async () => {
|
|
285
|
+
const allowed = await setUp();
|
|
286
|
+
expect(text((await allowed.pi.run(allowed.model, [user('permission')]))[0])).toBe('permission: yes');
|
|
287
|
+
const asked = await setUp({ fake: { ...FAKE, permissions: 'ask' } });
|
|
288
|
+
asked.pi.answer = 'Deny';
|
|
289
|
+
expect(text((await asked.pi.run(asked.model, [user('permission')]))[0])).toBe('permission: no');
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it('says how to sign in when the agent asks for it', async () => {
|
|
293
|
+
const { pi, model } = await setUp();
|
|
294
|
+
const [message] = await pi.run(model, [user('auth')]);
|
|
295
|
+
expect((message as AssistantMessage).stopReason).toBe('error');
|
|
296
|
+
expect((message as AssistantMessage).errorMessage).toContain('`fake login`');
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it('starts a pi turn when the agent goes on by itself', async () => {
|
|
300
|
+
const { pi, model } = await setUp();
|
|
301
|
+
const [message] = await pi.run(model, [user('background')]);
|
|
302
|
+
expect(text(message)).toBe('started');
|
|
303
|
+
for (let i = 0; i < 50 && !pi.sent.length; i++) {
|
|
304
|
+
await Bun.sleep(20);
|
|
305
|
+
}
|
|
306
|
+
expect(pi.sent).toEqual([{ message: expect.objectContaining({ content: ACTIVITY_TEXT, display: false }), options: { triggerTurn: true } }]);
|
|
307
|
+
const [report] = await pi.run(model, [user('background'), message, user(ACTIVITY_TEXT)]);
|
|
308
|
+
expect(text(report)).toBe('background finished');
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("finds pi's agent directory as pi does, under the name pi runs as", () => {
|
|
312
|
+
const pkg = mkdtempSync(join(tmpdir(), 'ta-agents-pkg-'));
|
|
313
|
+
writeFileSync(join(pkg, 'package.json'), JSON.stringify({ piConfig: { name: 'ta', configDir: '.ta' } }));
|
|
314
|
+
expect(agentDir({ PI_PACKAGE_DIR: pkg })).toBe(join(homedir(), '.ta', 'agent'));
|
|
315
|
+
expect(agentDir({ PI_PACKAGE_DIR: pkg, TA_CODING_AGENT_DIR: '/x' })).toBe('/x');
|
|
316
|
+
expect(agentDir({})).toBe(join(homedir(), '.pi', 'agent'));
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
it('leaves compacting to the agent', async () => {
|
|
320
|
+
const { pi, model } = await setUp();
|
|
321
|
+
expect(await pi.emit('session_before_compact', { reason: 'threshold' }, model)).toEqual([{ cancel: true }]);
|
|
322
|
+
expect(await pi.emit('session_before_compact', { reason: 'manual' }, model)).toEqual([{ cancel: true }]);
|
|
323
|
+
await Bun.sleep(5);
|
|
324
|
+
expect(pi.userMessages).toEqual(['/compact']);
|
|
325
|
+
expect(await pi.emit('session_before_compact', { reason: 'manual' }, { provider: 'anthropic' } as Model)).toEqual([undefined]);
|
|
326
|
+
});
|
|
327
|
+
});
|
package/src/extension.ts
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join, resolve } from 'node:path';
|
|
4
|
+
import type { AcpAgent } from './acp-agent';
|
|
5
|
+
import { Acp } from './acp-protocol';
|
|
6
|
+
import { AgentBindings, BINDING_ENTRY } from './agent-bindings';
|
|
7
|
+
import { AgentCatalog, type AgentConfig, type AgentModels, type AgentSetting, ModelCache, Programs } from './agent-catalog';
|
|
8
|
+
import { ACTIVITY_TEXT } from './agent-prompt';
|
|
9
|
+
import { AgentSession } from './agent-session';
|
|
10
|
+
import { AgentStreamer } from './agent-streamer';
|
|
11
|
+
import { type AgentCall, AgentTool } from './agent-tool';
|
|
12
|
+
import { emptyUsage } from './message-stream';
|
|
13
|
+
import type { AssistantMessageEventStream, ExtensionAPI, ExtensionContext, Model, ProviderModelConfig } from './pi-types';
|
|
14
|
+
|
|
15
|
+
/** TA_FILE_WRITE_EVENT of @celljs/ta-protocol, which this bundle does not import. */
|
|
16
|
+
const TA_FILE_WRITE_EVENT = 'ta:file-write';
|
|
17
|
+
interface TaFileWrite {
|
|
18
|
+
path: string;
|
|
19
|
+
before?: string | null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The custom message that starts a pi turn when an agent began one on its own. */
|
|
23
|
+
const ACTIVITY_ENTRY = 'ta-agents.activity';
|
|
24
|
+
const CONTEXT_WINDOW = 200_000;
|
|
25
|
+
const MAX_TOKENS = 32_000;
|
|
26
|
+
|
|
27
|
+
/** What the extension depends on, so that tests can give their own. */
|
|
28
|
+
export interface AgentsOptions {
|
|
29
|
+
find?: (program: string) => string | undefined;
|
|
30
|
+
cache?: ModelCache;
|
|
31
|
+
agentOf?: (config: AgentConfig, cwd: string) => AcpAgent;
|
|
32
|
+
/** `agents` of the user's settings.json, read when the extension loads, before pi's settings are there. */
|
|
33
|
+
userAgents?: () => Record<string, AgentSetting> | undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface Instance {
|
|
37
|
+
configs(): AgentConfig[];
|
|
38
|
+
streamer: AgentStreamer;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const INSTANCES = Symbol.for('ta-agents.instances');
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The extension of each pi session, by the session's id. The sessions of a process share one model
|
|
45
|
+
* runtime, so a provider registered by one session is the provider of all: its `streamSimple` finds the
|
|
46
|
+
* session that asks by `options.sessionId`.
|
|
47
|
+
*/
|
|
48
|
+
namespace Sessions {
|
|
49
|
+
function all(): Map<string, Instance> {
|
|
50
|
+
const global = globalThis as Record<symbol, Map<string, Instance> | undefined>;
|
|
51
|
+
const map = global[INSTANCES] ?? new Map<string, Instance>();
|
|
52
|
+
global[INSTANCES] = map;
|
|
53
|
+
return map;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function set(id: string, instance: Instance): void {
|
|
57
|
+
all().set(id, instance);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function remove(id: string, instance: Instance): void {
|
|
61
|
+
if (all().get(id) === instance) {
|
|
62
|
+
all().delete(id);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function get(id: string | undefined): Instance | undefined {
|
|
67
|
+
return id ? all().get(id) : undefined;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Other coding agents as pi models: CodeBuddy Code, Claude Code, Codex, and those of `agents` in pi's
|
|
73
|
+
* settings.json, over the Agent Client Protocol. Each is a provider; its models are the agent's own,
|
|
74
|
+
* and `default` leaves the choice to the agent. A conversation with one runs on the agent's harness
|
|
75
|
+
* (its prompt, tools, context and login); pi keeps the transcript, and shows the agent's tool calls as
|
|
76
|
+
* calls of {@link AgentTool}.
|
|
77
|
+
*/
|
|
78
|
+
export function activate(pi: ExtensionAPI, options: AgentsOptions = {}): void {
|
|
79
|
+
const find = options.find ?? Programs.find;
|
|
80
|
+
const userAgents = options.userAgents ?? (() => undefined);
|
|
81
|
+
const cache = options.cache ?? new ModelCache();
|
|
82
|
+
const calls = new Map<string, AgentCall>();
|
|
83
|
+
let configs: AgentConfig[] = [];
|
|
84
|
+
let context: ExtensionContext | undefined;
|
|
85
|
+
|
|
86
|
+
const ours = (model: Model | undefined) => configs.find(config => config.id === model?.provider);
|
|
87
|
+
|
|
88
|
+
const providerModels = (config: AgentConfig): ProviderModelConfig[] => {
|
|
89
|
+
const known = cache.get(config.id);
|
|
90
|
+
const listed = [
|
|
91
|
+
{ id: AgentCatalog.DEFAULT_MODEL, name: config.name },
|
|
92
|
+
...(known?.models ?? []).filter(model => model.id !== AgentCatalog.DEFAULT_MODEL).map(model => ({ id: model.id, name: model.name })),
|
|
93
|
+
];
|
|
94
|
+
return listed.map(model => ({
|
|
95
|
+
...model,
|
|
96
|
+
reasoning: known?.reasoning ?? false,
|
|
97
|
+
input: ['text', 'image'],
|
|
98
|
+
cost: { ...emptyUsage().cost },
|
|
99
|
+
contextWindow: known?.contextWindow ?? CONTEXT_WINDOW,
|
|
100
|
+
maxTokens: MAX_TOKENS,
|
|
101
|
+
})) as ProviderModelConfig[];
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const register = (config: AgentConfig) =>
|
|
105
|
+
pi.registerProvider(config.id, {
|
|
106
|
+
name: config.name,
|
|
107
|
+
baseUrl: `acp://${config.id}`,
|
|
108
|
+
apiKey: 'acp',
|
|
109
|
+
api: `acp-${config.id}`,
|
|
110
|
+
streamSimple: (model, transcript, streamOptions) => {
|
|
111
|
+
const owner = Sessions.get(streamOptions?.sessionId) ?? self;
|
|
112
|
+
const own = owner.configs().find(candidate => candidate.id === config.id) ?? config;
|
|
113
|
+
return owner.streamer.stream(own, model as Model, transcript, streamOptions) as unknown as AssistantMessageEventStream;
|
|
114
|
+
},
|
|
115
|
+
models: providerModels(config),
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const offer = (next: AgentConfig[]) => {
|
|
119
|
+
for (const config of configs) {
|
|
120
|
+
if (!next.some(candidate => candidate.id === config.id)) {
|
|
121
|
+
pi.unregisterProvider(config.id);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
configs = next;
|
|
125
|
+
configs.forEach(register);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/** The models the agent lists, kept for next time; its provider again when they changed. */
|
|
129
|
+
const learn = (config: AgentConfig, session: AgentSession, contextWindow?: number) => {
|
|
130
|
+
const option = Acp.optionOf(session.options, 'model');
|
|
131
|
+
const models = option
|
|
132
|
+
? Acp.values(option).map(value => ({ id: value.value, name: value.name }))
|
|
133
|
+
: (session.models?.availableModels ?? []).map(model => ({ id: model.modelId, name: model.name }));
|
|
134
|
+
const known = cache.get(config.id);
|
|
135
|
+
const next: AgentModels = {
|
|
136
|
+
models,
|
|
137
|
+
reasoning: !!Acp.optionOf(session.options, 'thought_level'),
|
|
138
|
+
contextWindow: contextWindow ?? known?.contextWindow,
|
|
139
|
+
};
|
|
140
|
+
if (cache.set(config.id, next)) {
|
|
141
|
+
register(config);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const toggleTool = (model: Model | undefined) => {
|
|
146
|
+
const active = pi.getActiveTools();
|
|
147
|
+
const has = active.includes(AgentTool.NAME);
|
|
148
|
+
if (ours(model) && !has) {
|
|
149
|
+
pi.setActiveTools([...active, AgentTool.NAME]);
|
|
150
|
+
} else if (!ours(model) && has) {
|
|
151
|
+
pi.setActiveTools(active.filter(name => name !== AgentTool.NAME));
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const bindings = new AgentBindings(
|
|
156
|
+
binding => pi.appendEntry(BINDING_ENTRY, binding),
|
|
157
|
+
(config, session) => {
|
|
158
|
+
session.callbacks = {
|
|
159
|
+
permission: async request => {
|
|
160
|
+
const ui = context?.hasUI ? context.ui : undefined;
|
|
161
|
+
if (config.permissions !== 'ask' || !ui) {
|
|
162
|
+
return AgentSession.allow(request);
|
|
163
|
+
}
|
|
164
|
+
const chosen = await ui.select(
|
|
165
|
+
`${config.name}: ${request.toolCall.title ?? 'permission'}`,
|
|
166
|
+
request.options.map(option => option.name),
|
|
167
|
+
);
|
|
168
|
+
const option = request.options.find(candidate => candidate.name === chosen);
|
|
169
|
+
return option ? { outcome: 'selected', optionId: option.optionId } : { outcome: 'cancelled' };
|
|
170
|
+
},
|
|
171
|
+
title: title => {
|
|
172
|
+
if (!pi.getSessionName()) {
|
|
173
|
+
pi.setSessionName(title);
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
activity: () => {
|
|
177
|
+
if (context?.model?.provider === config.id) {
|
|
178
|
+
pi.sendMessage({ customType: ACTIVITY_ENTRY, content: ACTIVITY_TEXT, display: false }, { triggerTurn: true });
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
// Other extensions (the list of changed files) hear of the files the agent writes with its own tools.
|
|
182
|
+
write: (path, before) => {
|
|
183
|
+
const file: TaFileWrite = { path: resolve(context?.cwd ?? bindings.cwd, path), ...(before !== undefined ? { before } : {}) };
|
|
184
|
+
pi.events.emit(TA_FILE_WRITE_EVENT, file);
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
learn(config, session);
|
|
188
|
+
},
|
|
189
|
+
options.agentOf,
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
const streamer = new AgentStreamer({
|
|
193
|
+
open: (config, signal) => bindings.open(config, signal),
|
|
194
|
+
calls,
|
|
195
|
+
ended: (config, session) => {
|
|
196
|
+
if (session.title && !pi.getSessionName()) {
|
|
197
|
+
pi.setSessionName(session.title);
|
|
198
|
+
}
|
|
199
|
+
const size = session.turn?.usage.size;
|
|
200
|
+
if (size) {
|
|
201
|
+
learn(config, session, size);
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
const self: Instance = { configs: () => configs, streamer };
|
|
207
|
+
let sessionId: string | undefined;
|
|
208
|
+
|
|
209
|
+
pi.registerTool(AgentTool.definition(calls));
|
|
210
|
+
// The agents of the user now, so that a session that was on one finds its model; those of the project come with the session.
|
|
211
|
+
offer(AgentCatalog.agents(userAgents(), find));
|
|
212
|
+
|
|
213
|
+
pi.on('session_start', (event, ctx) => {
|
|
214
|
+
context = ctx;
|
|
215
|
+
if (sessionId) {
|
|
216
|
+
Sessions.remove(sessionId, self);
|
|
217
|
+
}
|
|
218
|
+
sessionId = ctx.sessionManager.getSessionId();
|
|
219
|
+
Sessions.set(sessionId, self);
|
|
220
|
+
bindings.cwd = ctx.cwd;
|
|
221
|
+
bindings.restore(ctx.sessionManager.getBranch(), event.reason === 'fork');
|
|
222
|
+
const settings = (pi.getSettings() as { agents?: Record<string, AgentSetting> }).agents;
|
|
223
|
+
offer(AgentCatalog.agents(settings, find));
|
|
224
|
+
toggleTool(ctx.model);
|
|
225
|
+
});
|
|
226
|
+
pi.on('model_select', event => toggleTool(event.model));
|
|
227
|
+
pi.on('session_tree', () => bindings.forget());
|
|
228
|
+
pi.on('session_before_compact', (event, ctx) => {
|
|
229
|
+
if (!ours(ctx.model)) {
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
// The agent keeps its own context and compacts it itself: asked to, with its own command.
|
|
233
|
+
if (event.reason === 'manual') {
|
|
234
|
+
setTimeout(() => pi.sendUserMessage('/compact'), 0);
|
|
235
|
+
}
|
|
236
|
+
return { cancel: true };
|
|
237
|
+
});
|
|
238
|
+
pi.on('session_shutdown', () => {
|
|
239
|
+
bindings.close();
|
|
240
|
+
context = undefined;
|
|
241
|
+
if (sessionId) {
|
|
242
|
+
Sessions.remove(sessionId, self);
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* pi's agent directory, as pi finds it, without loading pi: `<NAME>_CODING_AGENT_DIR`, else
|
|
249
|
+
* `~/<configDir>/agent`, with the name and directory of `piConfig` of the package pi runs as (ta's, in Ta).
|
|
250
|
+
*/
|
|
251
|
+
export function agentDir(env: NodeJS.ProcessEnv = process.env): string {
|
|
252
|
+
let config: { name?: string; configDir?: string } = {};
|
|
253
|
+
try {
|
|
254
|
+
config = JSON.parse(readFileSync(join(env.PI_PACKAGE_DIR ?? '', 'package.json'), 'utf8')).piConfig ?? {};
|
|
255
|
+
} catch {
|
|
256
|
+
// pi's own package: no piConfig.
|
|
257
|
+
}
|
|
258
|
+
const name = config.name || 'pi';
|
|
259
|
+
return env[`${name.toUpperCase()}_CODING_AGENT_DIR`] || join(homedir(), config.configDir || '.pi', 'agent');
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** `agents` of settings.json in pi's agent directory. */
|
|
263
|
+
function readUserAgents(agentDir: string): Record<string, AgentSetting> | undefined {
|
|
264
|
+
try {
|
|
265
|
+
return JSON.parse(readFileSync(join(agentDir, 'settings.json'), 'utf8')).agents;
|
|
266
|
+
} catch {
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export default function (pi: ExtensionAPI): void {
|
|
272
|
+
activate(pi, { userAgents: () => readUserAgents(agentDir()) });
|
|
273
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { PassThrough } from 'node:stream';
|
|
3
|
+
import { JsonRpcConnection, JsonRpcError } from './json-rpc';
|
|
4
|
+
|
|
5
|
+
function pair() {
|
|
6
|
+
const toAgent = new PassThrough();
|
|
7
|
+
const fromAgent = new PassThrough();
|
|
8
|
+
const connection = new JsonRpcConnection(toAgent, fromAgent);
|
|
9
|
+
const sent: unknown[] = [];
|
|
10
|
+
toAgent.setEncoding('utf8');
|
|
11
|
+
toAgent.on('data', (chunk: string) =>
|
|
12
|
+
sent.push(
|
|
13
|
+
...chunk
|
|
14
|
+
.trim()
|
|
15
|
+
.split('\n')
|
|
16
|
+
.map(line => JSON.parse(line)),
|
|
17
|
+
),
|
|
18
|
+
);
|
|
19
|
+
const reply = (message: object) => fromAgent.write(`${JSON.stringify({ jsonrpc: '2.0', ...message })}\n`);
|
|
20
|
+
return { connection, sent, reply, fromAgent };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe('JsonRpcConnection', () => {
|
|
24
|
+
it('answers requests, in lines that may come in pieces, and skips lines that are not JSON', async () => {
|
|
25
|
+
const { connection, sent, fromAgent } = pair();
|
|
26
|
+
const answer = connection.request('initialize', { a: 1 });
|
|
27
|
+
await Bun.sleep(1);
|
|
28
|
+
expect(sent).toEqual([{ jsonrpc: '2.0', id: 1, method: 'initialize', params: { a: 1 } }]);
|
|
29
|
+
fromAgent.write('a log line\n{"jsonrpc":"2.0","id":1,');
|
|
30
|
+
fromAgent.write('"result":{"ok":true}}\n');
|
|
31
|
+
expect(await answer).toEqual({ ok: true });
|
|
32
|
+
expect(connection.pendingCount).toBe(0);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('fails a request with the error of the answer, and all of them when it closes', async () => {
|
|
36
|
+
const { connection, reply } = pair();
|
|
37
|
+
const failed = connection.request('x', {});
|
|
38
|
+
reply({ id: 1, error: { code: -32000, message: 'Authentication required' } });
|
|
39
|
+
const error = (await failed.catch(caught => caught)) as JsonRpcError;
|
|
40
|
+
expect([error instanceof JsonRpcError, error.code, error.message]).toEqual([true, -32000, 'Authentication required']);
|
|
41
|
+
const pending = connection.request('y', {});
|
|
42
|
+
connection.close(new Error('gone'));
|
|
43
|
+
expect(((await pending.catch(caught => caught)) as Error).message).toBe('gone');
|
|
44
|
+
expect(((await connection.request('z', {}).catch(caught => caught)) as Error).message).toBe('gone');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('answers the requests of the other side and passes on its notifications', async () => {
|
|
48
|
+
const { connection, sent, reply } = pair();
|
|
49
|
+
const notes: unknown[] = [];
|
|
50
|
+
connection.onNotification = (method, params) => notes.push([method, params]);
|
|
51
|
+
connection.onRequest = async (method, params) => ({ method, params });
|
|
52
|
+
reply({ method: 'session/update', params: { n: 1 } });
|
|
53
|
+
reply({ id: 'c1', method: 'session/request_permission', params: { p: 2 } });
|
|
54
|
+
reply({ id: 'c2', method: 'unknown/thing' });
|
|
55
|
+
await Bun.sleep(5);
|
|
56
|
+
expect(notes).toEqual([['session/update', { n: 1 }]]);
|
|
57
|
+
expect(sent).toContainEqual({ jsonrpc: '2.0', id: 'c1', result: { method: 'session/request_permission', params: { p: 2 } } });
|
|
58
|
+
const plain = pair();
|
|
59
|
+
plain.reply({ id: 7, method: 'unknown/thing' });
|
|
60
|
+
await Bun.sleep(5);
|
|
61
|
+
expect(plain.sent).toEqual([{ jsonrpc: '2.0', id: 7, error: { code: -32601, message: 'Method not found: unknown/thing' } }]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('rejects a request whose signal aborts', async () => {
|
|
65
|
+
const { connection } = pair();
|
|
66
|
+
const controller = new AbortController();
|
|
67
|
+
const request = connection.request('slow', {}, controller.signal);
|
|
68
|
+
controller.abort();
|
|
69
|
+
expect(((await request.catch(caught => caught)) as Error).message).toBeTruthy();
|
|
70
|
+
expect(connection.pendingCount).toBe(0);
|
|
71
|
+
});
|
|
72
|
+
});
|