-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.test.ts
More file actions
242 lines (226 loc) · 9.53 KB
/
Copy pathagent.test.ts
File metadata and controls
242 lines (226 loc) · 9.53 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
241
242
import { describe, it, expect } from 'vitest'
import { createAgentRuntime } from './agent'
import type { AppToolHandlers, AppToolProducedEvent } from '../tools/types'
// Build an OpenAI-compat SSE Response from scripted chunks.
function sseResponse(chunks: object[]): Response {
const enc = new TextEncoder()
const body = new ReadableStream<Uint8Array>({
start(controller) {
for (const c of chunks) controller.enqueue(enc.encode(`data: ${JSON.stringify(c)}\n`))
controller.enqueue(enc.encode('data: [DONE]\n'))
controller.close()
},
})
return new Response(body, { status: 200, headers: { 'Content-Type': 'text/event-stream' } })
}
// A fetch that returns each scripted turn in sequence.
function scriptedFetch(turns: object[][]): typeof fetch {
let i = 0
return (async () => sseResponse(turns[i++] ?? [])) as unknown as typeof fetch
}
function recordingHandlers() {
const calls: { tool: string; args: unknown }[] = []
const handlers: AppToolHandlers = {
async submitProposal(args) {
calls.push({ tool: 'submit_proposal', args })
return { proposalId: 'p-1', deduped: false }
},
async scheduleFollowup(args) {
calls.push({ tool: 'schedule_followup', args })
return { id: 'd-1', dueDate: args.dueDate, deduped: false }
},
async renderUi(args) {
calls.push({ tool: 'render_ui', args })
return { path: 'ui/x.json', content: '{}' }
},
async addCitation(args) {
calls.push({ tool: 'add_citation', args })
return { citationId: 'c-1', path: args.path }
},
}
return { handlers, calls }
}
const taxonomy = { proposalTypes: ['propose_swap'], regulatedTypes: ['propose_swap'] }
const ctx = { userId: 'u1', workspaceId: 'w1', threadId: 't1' }
describe('createAgentRuntime', () => {
it('advertises tools, fires the handler on a tool_call, folds the result, returns final text', async () => {
const { handlers, calls } = recordingHandlers()
const produced: AppToolProducedEvent[] = []
const fetchImpl = scriptedFetch([
// Turn 1: model emits a submit_proposal tool_call (args fragmented like real OpenAI).
[
{ choices: [{ delta: { tool_calls: [{ index: 0, id: 'call_1', function: { name: 'submit_proposal' } }] } }] },
{ choices: [{ delta: { tool_calls: [{ index: 0, function: { arguments: '{"type":"propose_swap",' } }] } }] },
{ choices: [{ delta: { tool_calls: [{ index: 0, function: { arguments: '"title":"Swap A→B","description":"Swap from carrier A to carrier B; premium drops 12%, coverage equal."}' } }] } }] },
],
// Turn 2: after the tool result is folded back, model answers.
[{ choices: [{ delta: { content: 'Queued the swap for approval.' } }] }],
])
const runtime = createAgentRuntime({
model: { baseUrl: 'https://router.test/v1', apiKey: 'k', model: 'test-model', fetchImpl },
taxonomy,
handlers,
systemPrompt: 'You are a test agent.',
})
const result = await runtime.run('Swap my policy.', { ctx, onProduced: (e) => produced.push(e) })
expect(calls).toHaveLength(1)
expect(calls[0]).toEqual({
tool: 'submit_proposal',
args: {
type: 'propose_swap',
title: 'Swap A→B',
description: 'Swap from carrier A to carrier B; premium drops 12%, coverage equal.',
regulated: true,
},
})
expect(result.toolResults).toHaveLength(1)
expect(result.toolResults[0]!.outcome).toEqual({
ok: true,
result: { proposalId: 'p-1', deduped: false, regulated: true, status: 'queued_for_approval' },
})
expect(result.finalText).toBe('Queued the swap for approval.')
expect(result.turns).toBe(2)
// The proposal body threads in-band through the tool loop → produced event.
expect(produced).toEqual([
{
type: 'proposal_created',
proposalId: 'p-1',
title: 'Swap A→B',
status: 'pending',
content: 'Swap from carrier A to carrier B; premium drops 12%, coverage equal.',
},
])
})
it('rejects a proposal type outside the taxonomy without calling the handler', async () => {
const { handlers, calls } = recordingHandlers()
const fetchImpl = scriptedFetch([
[
{
choices: [
{
delta: {
tool_calls: [
{ index: 0, id: 'c', function: { name: 'submit_proposal', arguments: '{"type":"not_a_type","title":"X"}' } },
],
},
},
],
},
],
[{ choices: [{ delta: { content: 'ok' } }] }],
])
const runtime = createAgentRuntime({
model: { baseUrl: 'https://router.test/v1', apiKey: 'k', model: 'm', fetchImpl },
taxonomy,
handlers,
systemPrompt: 's',
})
const result = await runtime.run('go', { ctx })
expect(calls).toHaveLength(0)
expect(result.toolResults[0]!.outcome).toMatchObject({ ok: false, code: 'invalid_type' })
})
it('routes a non-app tool to executeOtherTool', async () => {
const { handlers } = recordingHandlers()
const other: string[] = []
const fetchImpl = scriptedFetch([
[{ choices: [{ delta: { tool_calls: [{ index: 0, id: 'c', function: { name: 'integration_invoke', arguments: '{"action":"crm.read"}' } }] } }] }],
[{ choices: [{ delta: { content: 'fetched' } }] }],
])
const runtime = createAgentRuntime({
model: { baseUrl: 'https://router.test/v1', apiKey: 'k', model: 'm', fetchImpl },
taxonomy,
handlers,
systemPrompt: 's',
extraTools: [{ type: 'function', function: { name: 'integration_invoke', description: 'd', parameters: { type: 'object' } } }],
isOtherExecutableTool: (n) => n === 'integration_invoke',
executeOtherTool: async (call) => {
other.push(call.toolName)
return { ok: true, result: { rows: 0 } }
},
})
const result = await runtime.run('read crm', { ctx })
expect(other).toEqual(['integration_invoke'])
expect(result.finalText).toBe('fetched')
})
it('throws if executeOtherTool is set without isOtherExecutableTool', () => {
const { handlers } = recordingHandlers()
expect(() =>
createAgentRuntime({
model: { baseUrl: 'b', apiKey: 'k', model: 'm' },
taxonomy,
handlers,
systemPrompt: 's',
executeOtherTool: async () => ({ ok: true, result: {} }),
}),
).toThrow(/isOtherExecutableTool/)
})
it('streams raw events and tool results in order', async () => {
const { handlers } = recordingHandlers()
const fetchImpl = scriptedFetch([
[
{ choices: [{ delta: { content: 'Working… ' } }] },
{ choices: [{ delta: { tool_calls: [{ index: 0, id: 'c', function: { name: 'add_citation', arguments: '{"path":"a.md","quote":"q"}' } }] } }] },
],
[{ choices: [{ delta: { content: 'done' } }] }],
])
const runtime = createAgentRuntime({
model: { baseUrl: 'b', apiKey: 'k', model: 'm', fetchImpl },
taxonomy,
handlers,
systemPrompt: 's',
})
const kinds: string[] = []
for await (const y of runtime.stream('go', { ctx })) kinds.push(y.kind)
// event(text) + event(tool_call) for turn 1, tool_result, then event(text) for turn 2.
expect(kinds).toContain('tool_result')
expect(kinds.filter((k) => k === 'event').length).toBeGreaterThanOrEqual(2)
})
})
describe('createAgentRuntime — profile delivery (composeProfile)', () => {
// A fetch that captures each request body so we can assert what reached the model.
function capturingFetch(captured: Array<Record<string, unknown>>): typeof fetch {
return (async (_url: string, init: RequestInit) => {
captured.push(JSON.parse(String(init.body)))
return sseResponse([{ choices: [{ delta: { content: 'done' }, finish_reason: 'stop' }] }])
}) as unknown as typeof fetch
}
it('applies the profile transform: folds the composed prompt AND advertises delivered tools', async () => {
const { handlers } = recordingHandlers()
const captured: Array<Record<string, unknown>> = []
const deliveredTool = {
type: 'function',
function: { name: 'delivered_tool', parameters: { type: 'object', properties: {} } },
}
const runtime = createAgentRuntime({
model: { baseUrl: 'b', apiKey: 'k', model: 'm', fetchImpl: capturingFetch(captured) },
taxonomy,
handlers,
systemPrompt: 'BASE',
composeProfile: (base) => ({
systemPrompt: `${base.systemPrompt}\n\n## Certified guidance\nverify the invoice id`,
extraTools: [...base.extraTools, deliveredTool],
}),
})
await runtime.run('hi', { ctx })
const req = captured[0]!
expect(JSON.stringify(req.messages)).toContain('BASE')
expect(JSON.stringify(req.messages)).toContain('verify the invoice id')
const tools = (req.tools ?? []) as Array<{ function?: { name?: string } }>
expect(tools.some((t) => t.function?.name === 'delivered_tool')).toBe(true)
})
it('without composeProfile the base prompt + tools are unchanged (zero behavior change)', async () => {
const { handlers } = recordingHandlers()
const captured: Array<Record<string, unknown>> = []
const runtime = createAgentRuntime({
model: { baseUrl: 'b', apiKey: 'k', model: 'm', fetchImpl: capturingFetch(captured) },
taxonomy,
handlers,
systemPrompt: 'BASE',
})
await runtime.run('hi', { ctx })
const req = captured[0]!
expect(JSON.stringify(req.messages)).toContain('BASE')
const tools = (req.tools ?? []) as Array<{ function?: { name?: string } }>
expect(tools.some((t) => t.function?.name === 'delivered_tool')).toBe(false)
})
})