-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathchat-agent.test.ts
More file actions
240 lines (206 loc) · 6.7 KB
/
Copy pathchat-agent.test.ts
File metadata and controls
240 lines (206 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import '@ocap/repo-tools/test-utils/mock-endoify';
import type {
ChatMessage,
ChatResult,
ToolCall,
} from '@ocap/kernel-language-model-service';
import { describe, expect, it, vi } from 'vitest';
import { makeChatAgent } from './chat-agent.ts';
import type { BoundChat } from './chat-agent.ts';
import { capability } from '../capabilities/capability.ts';
const makeToolCall = (
id: string,
name: string,
args: Record<string, unknown>,
): ToolCall => ({
id,
type: 'function',
function: { name, arguments: JSON.stringify(args) },
});
const makeTextResponse = (content: string): ChatResult => ({
id: '0',
model: 'test',
choices: [
{
message: { role: 'assistant', content },
index: 0,
finish_reason: 'stop',
},
],
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
});
const makeToolCallResponse = (
id: string,
toolCalls: ToolCall[],
): ChatResult => ({
id,
model: 'test',
choices: [
{
message: { role: 'assistant', content: '', tool_calls: toolCalls },
index: 0,
finish_reason: 'tool_calls',
},
],
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
});
const noCapabilities = {};
describe('makeChatAgent', () => {
it('returns plain text response when model does not invoke a tool', async () => {
const chat: BoundChat = async () => makeTextResponse('Hello, world!');
const agent = makeChatAgent({ chat, capabilities: noCapabilities });
const result = await agent.task('say hello');
expect(result).toBe('Hello, world!');
});
it('dispatches a tool call and returns final text answer', async () => {
const add = vi.fn(async ({ a, b }: { a: number; b: number }) => a + b);
const addCap = capability(add, {
description: 'Add two numbers',
args: {
a: { type: 'number' },
b: { type: 'number' },
},
returns: { type: 'number' },
});
let call = 0;
const chat: BoundChat = async () => {
call += 1;
if (call === 1) {
return makeToolCallResponse('0', [
makeToolCall('c1', 'add', { a: 3, b: 4 }),
]);
}
return makeTextResponse('7');
};
const agent = makeChatAgent({ chat, capabilities: { add: addCap } });
const result = await agent.task('add 3 and 4');
expect(add).toHaveBeenCalledWith({ a: 3, b: 4 });
expect(result).toBe('7');
});
it('injects tool result message before next turn', async () => {
const recorded: ChatMessage[][] = [];
const ping = capability(async () => 'pong', {
description: 'Ping',
args: {},
returns: { type: 'string' },
});
let call = 0;
const chat: BoundChat = async ({ messages }) => {
recorded.push([...messages]);
call += 1;
if (call === 1) {
return makeToolCallResponse('0', [makeToolCall('c1', 'ping', {})]);
}
return makeTextResponse('done');
};
const agent = makeChatAgent({ chat, capabilities: { ping } });
await agent.task('ping');
// Second turn must include the tool result message
const secondTurn = recorded[1] ?? [];
expect(
secondTurn.some(
(message) => message.role === 'tool' && message.tool_call_id === 'c1',
),
).toBe(true);
expect(secondTurn.some((message) => message.content === '"pong"')).toBe(
true,
);
});
it('injects error message for unknown tool and continues', async () => {
const recorded: ChatMessage[][] = [];
let call = 0;
const chat: BoundChat = async ({ messages }) => {
recorded.push([...messages]);
call += 1;
if (call === 1) {
return makeToolCallResponse('0', [
makeToolCall('c1', 'nonexistent', {}),
]);
}
return makeTextResponse('recovered');
};
const agent = makeChatAgent({ chat, capabilities: noCapabilities });
const result = await agent.task('do something');
expect(result).toBe('recovered');
const secondTurn = recorded[1] ?? [];
expect(
secondTurn.some(
(message) =>
message.role === 'tool' &&
message.content.includes('Unknown capability'),
),
).toBe(true);
});
it('throws when invocation budget is exceeded', async () => {
const ping = capability(async () => 'pong', {
description: 'Ping',
args: {},
});
const chat: BoundChat = async () =>
makeToolCallResponse('0', [makeToolCall('c1', 'ping', {})]);
const agent = makeChatAgent({ chat, capabilities: { ping } });
await expect(
agent.task('go', undefined, { invocationBudget: 3 }),
).rejects.toThrow('Invocation budget exceeded');
});
it('applies judgment to final answer', async () => {
const chat: BoundChat = async () => makeTextResponse('hello');
const agent = makeChatAgent({ chat, capabilities: noCapabilities });
const isNumber = (result: unknown): result is number =>
typeof result === 'number';
await expect(agent.task('go', isNumber)).rejects.toThrow('Invalid result');
});
it('passes tools to the chat function', async () => {
const recordedTools: unknown[] = [];
const ping = capability(async () => 'pong', {
description: 'Ping the server',
args: {},
returns: { type: 'string' },
});
const chat: BoundChat = async ({ tools }) => {
recordedTools.push(tools);
return makeTextResponse('done');
};
const agent = makeChatAgent({ chat, capabilities: { ping } });
await agent.task('go');
expect(recordedTools[0]).toStrictEqual([
{
type: 'function',
function: {
name: 'ping',
description: 'Ping the server',
parameters: { type: 'object', properties: {}, required: [] },
},
},
]);
});
it('passes undefined tools when there are no capabilities', async () => {
let recordedTools: unknown = 'not-set';
const chat: BoundChat = async ({ tools }) => {
recordedTools = tools;
return makeTextResponse('done');
};
const agent = makeChatAgent({ chat, capabilities: noCapabilities });
await agent.task('go');
expect(recordedTools).toBeUndefined();
});
it('accumulates experiences across tasks', async () => {
let call = 0;
const responses = ['hello', 'world'];
const chat: BoundChat = async () => {
const response = makeTextResponse(responses[call] ?? '');
call += 1;
return response;
};
const agent = makeChatAgent({ chat, capabilities: noCapabilities });
await agent.task('first');
await agent.task('second');
const exps = [];
for await (const exp of agent.experiences) {
exps.push(exp);
}
expect(exps).toHaveLength(2);
expect(exps[0]?.objective.intent).toBe('first');
expect(exps[1]?.objective.intent).toBe('second');
});
});