@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/src/agent-streamer.ts
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
import { Acp } from './acp-protocol';
|
|
2
|
-
import { AgentCatalog, type AgentConfig } from './agent-catalog';
|
|
3
|
-
import { AgentPrompt } from './agent-prompt';
|
|
4
|
-
import type { AgentSession } from './agent-session';
|
|
5
|
-
import { type AgentCall, AgentTool } from './agent-tool';
|
|
6
|
-
import type { AgentTurn } from './agent-turn';
|
|
7
|
-
import { emptyUsage, MessageBuilder, MessageStream } from './message-stream';
|
|
8
|
-
import type { Model, SimpleStreamOptions, TranscriptContext, Usage } from './pi-types';
|
|
9
|
-
import { text } from './texts';
|
|
10
|
-
|
|
11
|
-
/** A session of an agent for the pi session, and whether it starts empty. */
|
|
12
|
-
export interface OpenedSession {
|
|
13
|
-
session: AgentSession;
|
|
14
|
-
fresh: boolean;
|
|
15
|
-
images: boolean;
|
|
16
|
-
/** The directory of the session, as given and as its real path: agents may report either. */
|
|
17
|
-
dirs: string[];
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface AgentStreamHost {
|
|
21
|
-
open(config: AgentConfig, signal?: AbortSignal): Promise<OpenedSession>;
|
|
22
|
-
readonly calls: Map<string, AgentCall>;
|
|
23
|
-
/** A turn of `session` ended. */
|
|
24
|
-
ended(config: AgentConfig, session: AgentSession): void;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** How long a tool call of the agent may stay silent before its arguments count as complete anyway. */
|
|
28
|
-
const SETTLE_MS = 1500;
|
|
29
|
-
|
|
30
|
-
/** The values of an agent's `thought_level` that stand for pi's thinking levels, best first. */
|
|
31
|
-
const THINKING: Record<string, string[]> = {
|
|
32
|
-
minimal: ['minimal', 'low'],
|
|
33
|
-
low: ['low'],
|
|
34
|
-
medium: ['medium'],
|
|
35
|
-
high: ['high'],
|
|
36
|
-
xhigh: ['xhigh', 'max', 'high'],
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* pi's `streamSimple` for an agent. A pi turn sends what the user said since the agent last answered
|
|
41
|
-
* and streams the agent's turn; each tool call of the agent ends the assistant message with a call of
|
|
42
|
-
* {@link AgentTool}, and the next `streamSimple`, after pi "ran" it, goes on with the same turn.
|
|
43
|
-
*/
|
|
44
|
-
export class AgentStreamer {
|
|
45
|
-
constructor(protected readonly host: AgentStreamHost) {}
|
|
46
|
-
|
|
47
|
-
stream(config: AgentConfig, model: Model, context: TranscriptContext, options?: SimpleStreamOptions): MessageStream {
|
|
48
|
-
const stream = new MessageStream();
|
|
49
|
-
const builder = new MessageBuilder(stream, model);
|
|
50
|
-
this.run(config, model, context, options, builder).catch(error => builder.error('error', AgentStreamer.describe(config, error)));
|
|
51
|
-
return stream;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
protected async run(
|
|
55
|
-
config: AgentConfig,
|
|
56
|
-
model: Model,
|
|
57
|
-
context: TranscriptContext,
|
|
58
|
-
options: SimpleStreamOptions | undefined,
|
|
59
|
-
builder: MessageBuilder,
|
|
60
|
-
): Promise<void> {
|
|
61
|
-
const signal = options?.signal;
|
|
62
|
-
const { session, fresh, images, dirs } = await this.host.open(config, signal);
|
|
63
|
-
const pending = AgentPrompt.pending(context.messages);
|
|
64
|
-
let turn = session.turn;
|
|
65
|
-
if (turn) {
|
|
66
|
-
// What the user said while the agent works goes once it ended.
|
|
67
|
-
if (pending.length) {
|
|
68
|
-
session.queue(AgentPrompt.blocks(pending, images));
|
|
69
|
-
}
|
|
70
|
-
} else {
|
|
71
|
-
let prompt = pending.length ? AgentPrompt.blocks(pending, images) : session.takeQueued();
|
|
72
|
-
if (!prompt?.length) {
|
|
73
|
-
builder.done('stop');
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
const earlier = fresh ? AgentPrompt.transcript(context.messages, pending.length) : undefined;
|
|
77
|
-
if (earlier) {
|
|
78
|
-
prompt = [{ type: 'text', text: earlier }, ...prompt];
|
|
79
|
-
}
|
|
80
|
-
await session.idle();
|
|
81
|
-
await this.configure(session, model, options, signal);
|
|
82
|
-
turn = session.prompt(prompt);
|
|
83
|
-
}
|
|
84
|
-
for (;;) {
|
|
85
|
-
const item = await turn.next(signal);
|
|
86
|
-
if (!item) {
|
|
87
|
-
session.abandon(turn);
|
|
88
|
-
builder.error('aborted', 'Aborted');
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
switch (item.type) {
|
|
92
|
-
case 'text':
|
|
93
|
-
case 'thinking':
|
|
94
|
-
builder.append(item.type, item.text);
|
|
95
|
-
break;
|
|
96
|
-
case 'tool':
|
|
97
|
-
await this.tools(config, session, turn, item.id, builder, dirs, signal);
|
|
98
|
-
return;
|
|
99
|
-
case 'end': {
|
|
100
|
-
session.finished(turn);
|
|
101
|
-
this.host.ended(config, session);
|
|
102
|
-
const queued = session.takeQueued();
|
|
103
|
-
if (queued) {
|
|
104
|
-
turn = session.prompt(queued);
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
if (item.reason === 'cancelled' && signal?.aborted) {
|
|
108
|
-
builder.error('aborted', 'Aborted');
|
|
109
|
-
} else {
|
|
110
|
-
if (turn.command && !turn.produced) {
|
|
111
|
-
// A command of the agent that only acts in its own interface (a panel, a reset) says nothing.
|
|
112
|
-
builder.append('text', text('commandDone', config.name, turn.command));
|
|
113
|
-
}
|
|
114
|
-
builder.done('stop', AgentStreamer.usage(turn));
|
|
115
|
-
}
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
case 'error':
|
|
119
|
-
session.finished(turn);
|
|
120
|
-
builder.error('error', AgentStreamer.describe(config, item));
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/** Ends the message with the calls the agent started, once their arguments are complete. */
|
|
127
|
-
protected async tools(
|
|
128
|
-
config: AgentConfig,
|
|
129
|
-
session: AgentSession,
|
|
130
|
-
turn: AgentTurn,
|
|
131
|
-
first: string,
|
|
132
|
-
builder: MessageBuilder,
|
|
133
|
-
dirs: string[],
|
|
134
|
-
signal?: AbortSignal,
|
|
135
|
-
): Promise<void> {
|
|
136
|
-
// Calls started together go in one message: each once its arguments are complete.
|
|
137
|
-
const ids = [first];
|
|
138
|
-
while (turn.peek()?.type === 'tool') {
|
|
139
|
-
ids.push((turn.shift() as { id: string }).id);
|
|
140
|
-
}
|
|
141
|
-
for (const id of ids) {
|
|
142
|
-
await turn.settle(id, SETTLE_MS, signal);
|
|
143
|
-
}
|
|
144
|
-
if (signal?.aborted) {
|
|
145
|
-
session.abandon(turn);
|
|
146
|
-
builder.error('aborted', 'Aborted');
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
for (const id of ids) {
|
|
150
|
-
const run = turn.tools.get(id);
|
|
151
|
-
if (run) {
|
|
152
|
-
this.host.calls.set(id, { agent: config.name, session, turn });
|
|
153
|
-
builder.toolCall({ type: 'toolCall', id, name: AgentTool.NAME, arguments: AgentTool.args(run, dirs) });
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
builder.done('toolUse');
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/** The model and thinking level pi has, set on the agent; what it does not offer stays as it is. */
|
|
160
|
-
protected async configure(session: AgentSession, model: Model, options: SimpleStreamOptions | undefined, signal?: AbortSignal): Promise<void> {
|
|
161
|
-
try {
|
|
162
|
-
if (model.id !== AgentCatalog.DEFAULT_MODEL) {
|
|
163
|
-
await session.select('model', model.id, signal);
|
|
164
|
-
}
|
|
165
|
-
const level = options?.reasoning;
|
|
166
|
-
if (level && model.reasoning) {
|
|
167
|
-
const offered = Acp.values(Acp.optionOf(session.options, 'thought_level')).map(value => value.value);
|
|
168
|
-
await session.select(
|
|
169
|
-
'thought_level',
|
|
170
|
-
(THINKING[level] ?? [level]).find(value => offered.includes(value)),
|
|
171
|
-
signal,
|
|
172
|
-
);
|
|
173
|
-
}
|
|
174
|
-
} catch {
|
|
175
|
-
// The agent goes on with what it had.
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
static usage(turn: AgentTurn): Usage {
|
|
180
|
-
const usage = emptyUsage();
|
|
181
|
-
const { input, output, cacheRead, used } = turn.usage;
|
|
182
|
-
usage.cacheRead = cacheRead ?? 0;
|
|
183
|
-
usage.input = input !== undefined ? Math.max(0, input - usage.cacheRead) : (used ?? 0);
|
|
184
|
-
usage.output = output ?? 0;
|
|
185
|
-
usage.totalTokens = usage.input + usage.output + usage.cacheRead;
|
|
186
|
-
return usage;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
static describe(config: AgentConfig, error: { message?: string; code?: number } | unknown): string {
|
|
190
|
-
const { message, code } = (error ?? {}) as { message?: string; code?: number };
|
|
191
|
-
if (code === Acp.AUTH_REQUIRED) {
|
|
192
|
-
return text('signIn', config.name, config.login ?? config.command);
|
|
193
|
-
}
|
|
194
|
-
return message || String(error);
|
|
195
|
-
}
|
|
196
|
-
}
|
package/src/agent-tool.spec.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test';
|
|
2
|
-
import { AgentTool } from './agent-tool';
|
|
3
|
-
import type { ToolRun } from './agent-turn';
|
|
4
|
-
|
|
5
|
-
const run = (fields: Partial<ToolRun>): ToolRun => ({
|
|
6
|
-
id: 't',
|
|
7
|
-
title: '',
|
|
8
|
-
kind: 'other',
|
|
9
|
-
input: {},
|
|
10
|
-
locations: [],
|
|
11
|
-
status: 'completed',
|
|
12
|
-
output: '',
|
|
13
|
-
done: true,
|
|
14
|
-
complete: true,
|
|
15
|
-
terminal: '',
|
|
16
|
-
updatedAt: 0,
|
|
17
|
-
...fields,
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe('AgentTool', () => {
|
|
21
|
-
it('describes a call by its kind and what it is on, paths in the session relative', () => {
|
|
22
|
-
const dirs = ['/tmp/w', '/private/tmp/w'];
|
|
23
|
-
expect(AgentTool.args(run({ kind: 'read', title: 'Read', locations: ['/private/tmp/w/src/a.ts'], name: 'Read' }), dirs)).toEqual({
|
|
24
|
-
title: 'Read',
|
|
25
|
-
kind: 'read',
|
|
26
|
-
target: 'src/a.ts',
|
|
27
|
-
tool: 'Read',
|
|
28
|
-
});
|
|
29
|
-
expect(AgentTool.args(run({ kind: 'execute', title: '`npm test`' })).target).toBe('npm test');
|
|
30
|
-
expect(AgentTool.args(run({ kind: 'search', title: 'Grep', input: { pattern: 'TODO' } })).target).toBe('TODO');
|
|
31
|
-
expect(AgentTool.args(run({ kind: 'edit', title: 'Edit /etc/hosts', locations: ['/etc/hosts'] }), dirs).target).toBe('/etc/hosts');
|
|
32
|
-
expect(AgentTool.args(run({ title: 'Skill' })).target).toBe('Skill');
|
|
33
|
-
});
|
|
34
|
-
});
|
package/src/agent-tool.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import type { AgentSession } from './agent-session';
|
|
2
|
-
import type { AgentTurn, ToolRun } from './agent-turn';
|
|
3
|
-
import type { AgentToolResult, ToolDefinition } from './pi-types';
|
|
4
|
-
import { text } from './texts';
|
|
5
|
-
|
|
6
|
-
/** Where a call the provider showed to pi runs: in a turn of an agent's session. */
|
|
7
|
-
export interface AgentCall {
|
|
8
|
-
agent: string;
|
|
9
|
-
session: AgentSession;
|
|
10
|
-
turn: AgentTurn;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function relative(path: string, dirs: string[]): string {
|
|
14
|
-
const dir = dirs.find(candidate => path.startsWith(`${candidate}/`) || path.startsWith(`${candidate}\\`));
|
|
15
|
-
return dir ? path.slice(dir.length + 1) : path;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function str(value: unknown): string | undefined {
|
|
19
|
-
return typeof value === 'string' && value.trim() ? value.trim() : undefined;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* The tool that stands for the tool calls an agent makes with its own tools. The provider ends an
|
|
24
|
-
* assistant message with such a call when the agent makes one, so that pi records and shows it as it
|
|
25
|
-
* does its own; running it only waits for the agent to finish the call. The arguments describe the
|
|
26
|
-
* call for clients: what kind of thing it does, on what.
|
|
27
|
-
*/
|
|
28
|
-
export namespace AgentTool {
|
|
29
|
-
export const NAME = 'agent_tool';
|
|
30
|
-
|
|
31
|
-
/** `cwd`: the directory of the session (its real path too), for paths in it to show relative. */
|
|
32
|
-
export function args(run: ToolRun, cwd: string[] = []): Record<string, string> {
|
|
33
|
-
const input = run.input;
|
|
34
|
-
const title = run.title || run.name || run.kind;
|
|
35
|
-
let target: string | undefined;
|
|
36
|
-
switch (run.kind) {
|
|
37
|
-
case 'execute':
|
|
38
|
-
target = str(input.command) ?? title.replace(/^`|`$/g, '');
|
|
39
|
-
break;
|
|
40
|
-
case 'read':
|
|
41
|
-
case 'edit':
|
|
42
|
-
case 'delete':
|
|
43
|
-
case 'move':
|
|
44
|
-
target = run.locations[0] ?? str(input.file_path) ?? str(input.path) ?? str(input.filePath);
|
|
45
|
-
break;
|
|
46
|
-
case 'search':
|
|
47
|
-
target = str(input.pattern) ?? str(input.query);
|
|
48
|
-
break;
|
|
49
|
-
case 'fetch':
|
|
50
|
-
target = str(input.url) ?? str(input.query);
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
// A call that says what it is for (a script of CodeBuddy Code's code mode, a command) is shown by that.
|
|
54
|
-
target ??= str(input.description);
|
|
55
|
-
return { title, kind: run.kind, target: relative(target ?? title, cwd), ...(run.name ? { tool: run.name } : {}) };
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function definition(calls: Map<string, AgentCall>): ToolDefinition {
|
|
59
|
-
return {
|
|
60
|
-
name: NAME,
|
|
61
|
-
label: 'Agent tool',
|
|
62
|
-
description: 'A tool call another coding agent made with its own tools. Only that agent calls it.',
|
|
63
|
-
parameters: {
|
|
64
|
-
type: 'object',
|
|
65
|
-
properties: {
|
|
66
|
-
title: { type: 'string' },
|
|
67
|
-
kind: { type: 'string' },
|
|
68
|
-
target: { type: 'string' },
|
|
69
|
-
tool: { type: 'string' },
|
|
70
|
-
},
|
|
71
|
-
} as unknown as ToolDefinition['parameters'],
|
|
72
|
-
exposure: 'direct',
|
|
73
|
-
defaultActive: false,
|
|
74
|
-
annotations: { readOnlyHint: true },
|
|
75
|
-
async execute(toolCallId, _params, signal): Promise<AgentToolResult> {
|
|
76
|
-
const call = calls.get(toolCallId);
|
|
77
|
-
if (!call) {
|
|
78
|
-
return { content: [{ type: 'text', text: 'This call ran in an earlier run of the agent.' }], details: {} };
|
|
79
|
-
}
|
|
80
|
-
const run = await call.turn.waitTool(toolCallId, signal);
|
|
81
|
-
if (signal?.aborted) {
|
|
82
|
-
call.session.abandon(call.turn);
|
|
83
|
-
calls.delete(toolCallId);
|
|
84
|
-
throw new Error('Cancelled');
|
|
85
|
-
}
|
|
86
|
-
calls.delete(toolCallId);
|
|
87
|
-
const details = { agent: call.agent, kind: run?.kind, title: run?.title, status: run?.status, locations: run?.locations };
|
|
88
|
-
if (!run?.done) {
|
|
89
|
-
return { content: [{ type: 'text', text: text('unfinished', call.agent) }], details, isError: true };
|
|
90
|
-
}
|
|
91
|
-
return { content: [{ type: 'text', text: run.output || '(no output)' }], details, isError: run.status === 'failed' };
|
|
92
|
-
},
|
|
93
|
-
} as ToolDefinition;
|
|
94
|
-
}
|
|
95
|
-
}
|
package/src/agent-turn.spec.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test';
|
|
2
|
-
import { AgentTurn } from './agent-turn';
|
|
3
|
-
|
|
4
|
-
describe('AgentTurn', () => {
|
|
5
|
-
it('tells of the files calls change as soon as they name them, once each, with their text before from a diff', () => {
|
|
6
|
-
const told: [string, (string | null)?][] = [];
|
|
7
|
-
const turn = new AgentTurn(false, undefined, (path, before) => told.push(before === undefined ? [path] : [path, before]));
|
|
8
|
-
turn.update({ sessionUpdate: 'tool_call', toolCallId: 'r', title: 'Read', kind: 'read', status: 'pending', rawInput: { file_path: '/w/read.txt' } });
|
|
9
|
-
turn.update({ sessionUpdate: 'tool_call', toolCallId: 'e', title: 'Edit', kind: 'edit', status: 'pending', rawInput: null });
|
|
10
|
-
expect(told).toEqual([]);
|
|
11
|
-
turn.update({ sessionUpdate: 'tool_call_update', toolCallId: 'e', status: 'pending', rawInput: { file_path: '/w/a.txt' } });
|
|
12
|
-
turn.update({ sessionUpdate: 'tool_call_update', toolCallId: 'e', status: 'completed', locations: [{ path: '/w/a.txt' }] });
|
|
13
|
-
turn.update({
|
|
14
|
-
sessionUpdate: 'tool_call',
|
|
15
|
-
toolCallId: 'w',
|
|
16
|
-
title: 'Write',
|
|
17
|
-
kind: 'edit',
|
|
18
|
-
status: 'pending',
|
|
19
|
-
content: [
|
|
20
|
-
{ type: 'diff', path: '/w/new.txt', oldText: null, newText: 'x' },
|
|
21
|
-
{ type: 'diff', path: '/w/old.txt', oldText: 'before', newText: 'after' },
|
|
22
|
-
],
|
|
23
|
-
});
|
|
24
|
-
expect(told).toEqual([['/w/a.txt'], ['/w/new.txt', null], ['/w/old.txt', 'before']]);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('queues text and thinking, joined while they go on, and the end', async () => {
|
|
28
|
-
const turn = new AgentTurn();
|
|
29
|
-
turn.update({ sessionUpdate: 'agent_thought_chunk', content: { type: 'text', text: 'hm' } });
|
|
30
|
-
turn.update({ sessionUpdate: 'agent_message_chunk', content: { type: 'text', text: 'Hel' } });
|
|
31
|
-
turn.update({ sessionUpdate: 'agent_message_chunk', content: { type: 'text', text: 'lo' } });
|
|
32
|
-
turn.update({
|
|
33
|
-
sessionUpdate: 'plan',
|
|
34
|
-
entries: [
|
|
35
|
-
{ content: 'Read', status: 'completed' },
|
|
36
|
-
{ content: 'Fix', status: 'pending' },
|
|
37
|
-
],
|
|
38
|
-
});
|
|
39
|
-
turn.end('end_turn');
|
|
40
|
-
turn.update({ sessionUpdate: 'agent_message_chunk', content: { type: 'text', text: 'late' } });
|
|
41
|
-
const items = [];
|
|
42
|
-
for (let item = await turn.next(); item; item = turn.peek() ? await turn.next() : undefined) {
|
|
43
|
-
items.push(item);
|
|
44
|
-
}
|
|
45
|
-
expect(items).toEqual([
|
|
46
|
-
{ type: 'thinking', text: 'hm' },
|
|
47
|
-
{ type: 'text', text: 'Hello' },
|
|
48
|
-
{ type: 'thinking', text: '- [x] Read\n- [ ] Fix\n' },
|
|
49
|
-
{ type: 'end', reason: 'end_turn' },
|
|
50
|
-
]);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('follows a tool call through its updates to its output', async () => {
|
|
54
|
-
const turn = new AgentTurn();
|
|
55
|
-
turn.update({
|
|
56
|
-
sessionUpdate: 'tool_call',
|
|
57
|
-
toolCallId: 't',
|
|
58
|
-
title: 'Bash',
|
|
59
|
-
kind: 'other',
|
|
60
|
-
status: 'in_progress',
|
|
61
|
-
rawInput: {},
|
|
62
|
-
_meta: { 'x.ai/toolName': 'Bash' },
|
|
63
|
-
});
|
|
64
|
-
turn.update({ sessionUpdate: 'tool_call_update', toolCallId: 't', content: [{ type: 'content', content: { type: 'text', text: '{"command": "l' } }] });
|
|
65
|
-
turn.update({ sessionUpdate: 'tool_call', toolCallId: 't', title: '`ls`', kind: 'execute', status: 'pending', rawInput: { command: 'ls' } });
|
|
66
|
-
expect(turn.tools.get('t')).toMatchObject({ title: '`ls`', kind: 'execute', name: 'Bash', input: { command: 'ls' }, done: false });
|
|
67
|
-
const done = turn.waitTool('t');
|
|
68
|
-
turn.update({ sessionUpdate: 'tool_call_update', toolCallId: 't', status: 'completed', rawOutput: { type: 'text', text: 'a\nb' } });
|
|
69
|
-
expect(await done).toMatchObject({ status: 'completed', output: 'a\nb', done: true });
|
|
70
|
-
expect(await turn.next()).toEqual({ type: 'tool', id: 't' });
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('takes the arguments of a call as complete once it is quiet, or the agent says something else', async () => {
|
|
74
|
-
const turn = new AgentTurn();
|
|
75
|
-
turn.update({ sessionUpdate: 'tool_call', toolCallId: 't', title: 'Read', kind: 'read', status: 'pending' });
|
|
76
|
-
const started = Date.now();
|
|
77
|
-
await turn.settle('t', 60);
|
|
78
|
-
expect(Date.now() - started).toBeGreaterThanOrEqual(50);
|
|
79
|
-
turn.update({ sessionUpdate: 'tool_call', toolCallId: 'u', title: 'Bash', kind: 'other', status: 'in_progress', rawInput: {} });
|
|
80
|
-
const settling = turn.settle('u', 5000);
|
|
81
|
-
turn.update({ sessionUpdate: 'tool_call_update', toolCallId: 'u', rawInput: { command: 'l' } });
|
|
82
|
-
expect(turn.tools.get('u')?.complete).toBe(false);
|
|
83
|
-
turn.update({ sessionUpdate: 'tool_call', toolCallId: 'u', title: '`ls`', kind: 'execute', status: 'pending', rawInput: { command: 'ls' } });
|
|
84
|
-
await settling;
|
|
85
|
-
expect(turn.tools.get('u')?.complete).toBe(true);
|
|
86
|
-
turn.update({
|
|
87
|
-
sessionUpdate: 'tool_call',
|
|
88
|
-
toolCallId: 'v',
|
|
89
|
-
title: 'Read',
|
|
90
|
-
kind: 'other',
|
|
91
|
-
status: 'in_progress',
|
|
92
|
-
_meta: { 'x/toolArgumentsComplete': true },
|
|
93
|
-
});
|
|
94
|
-
expect(turn.tools.get('v')?.complete).toBe(true);
|
|
95
|
-
turn.update({ sessionUpdate: 'tool_call', toolCallId: 'w', title: 'Grep', kind: 'search', status: 'in_progress' });
|
|
96
|
-
const moved = turn.settle('w', 5000);
|
|
97
|
-
turn.update({ sessionUpdate: 'agent_message_chunk', content: { type: 'text', text: 'meanwhile' } });
|
|
98
|
-
await moved;
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('takes the output of a terminal the agent reports in pieces, and its name of the tool', async () => {
|
|
102
|
-
const turn = new AgentTurn();
|
|
103
|
-
turn.update({ sessionUpdate: 'tool_call', toolCallId: 'x', name: 'exec_command', title: 'List', kind: 'execute', status: 'in_progress' } as never);
|
|
104
|
-
turn.update({ sessionUpdate: 'tool_call_update', toolCallId: 'x', _meta: { terminal_output_delta: { data: 'a\n' } } });
|
|
105
|
-
turn.update({
|
|
106
|
-
sessionUpdate: 'tool_call_update',
|
|
107
|
-
toolCallId: 'x',
|
|
108
|
-
status: 'completed',
|
|
109
|
-
rawOutput: { exit_code: 0 },
|
|
110
|
-
_meta: { terminal_output_delta: { data: 'b\n' } },
|
|
111
|
-
});
|
|
112
|
-
expect(turn.tools.get('x')).toMatchObject({ name: 'exec_command', output: 'a\nb\n' });
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it('shows a diff and reads usage from what the agent reports', () => {
|
|
116
|
-
expect(AgentTurn.output({ toolCallId: 'e', content: [{ type: 'diff', path: 'a.ts', oldText: 'a\nb\nc', newText: 'a\nB\nc' }] })).toBe('a.ts\n- b\n+ B');
|
|
117
|
-
expect(AgentTurn.raw({ exitCode: 0 })).toBe('{\n "exitCode": 0\n}');
|
|
118
|
-
const turn = new AgentTurn();
|
|
119
|
-
turn.update({ sessionUpdate: 'usage_update', used: 10, size: 100, _meta: { usage: { input_tokens: 7, output_tokens: 3 } } });
|
|
120
|
-
expect(turn.usage).toEqual({ used: 10, size: 100, input: 7, output: 3, cacheRead: undefined });
|
|
121
|
-
});
|
|
122
|
-
});
|