@namzu/anthropic 3.1.0 → 3.1.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/CHANGELOG.md +29 -0
- package/package.json +12 -2
- package/dist/__tests__/anthropic.test.d.ts +0 -2
- package/dist/__tests__/anthropic.test.d.ts.map +0 -1
- package/dist/__tests__/anthropic.test.js +0 -425
- package/dist/__tests__/anthropic.test.js.map +0 -1
- package/dist/__tests__/background-delegation.live.test.d.ts +0 -2
- package/dist/__tests__/background-delegation.live.test.d.ts.map +0 -1
- package/dist/__tests__/background-delegation.live.test.js +0 -212
- package/dist/__tests__/background-delegation.live.test.js.map +0 -1
- package/dist/__tests__/block-shape-guard.test.d.ts +0 -2
- package/dist/__tests__/block-shape-guard.test.d.ts.map +0 -1
- package/dist/__tests__/block-shape-guard.test.js +0 -218
- package/dist/__tests__/block-shape-guard.test.js.map +0 -1
- package/dist/__tests__/cache-anchors.test.d.ts +0 -2
- package/dist/__tests__/cache-anchors.test.d.ts.map +0 -1
- package/dist/__tests__/cache-anchors.test.js +0 -220
- package/dist/__tests__/cache-anchors.test.js.map +0 -1
- package/dist/__tests__/citations.test.d.ts +0 -2
- package/dist/__tests__/citations.test.d.ts.map +0 -1
- package/dist/__tests__/citations.test.js +0 -189
- package/dist/__tests__/citations.test.js.map +0 -1
- package/dist/__tests__/content-blocks.test.d.ts +0 -2
- package/dist/__tests__/content-blocks.test.d.ts.map +0 -1
- package/dist/__tests__/content-blocks.test.js +0 -131
- package/dist/__tests__/content-blocks.test.js.map +0 -1
- package/dist/__tests__/document-input.test.d.ts +0 -2
- package/dist/__tests__/document-input.test.d.ts.map +0 -1
- package/dist/__tests__/document-input.test.js +0 -98
- package/dist/__tests__/document-input.test.js.map +0 -1
- package/dist/__tests__/effort-levels-are-askable.test.d.ts +0 -2
- package/dist/__tests__/effort-levels-are-askable.test.d.ts.map +0 -1
- package/dist/__tests__/effort-levels-are-askable.test.js +0 -74
- package/dist/__tests__/effort-levels-are-askable.test.js.map +0 -1
- package/dist/__tests__/error-taxonomy.test.d.ts +0 -16
- package/dist/__tests__/error-taxonomy.test.d.ts.map +0 -1
- package/dist/__tests__/error-taxonomy.test.js +0 -242
- package/dist/__tests__/error-taxonomy.test.js.map +0 -1
- package/dist/__tests__/reasoning-close.test.d.ts +0 -2
- package/dist/__tests__/reasoning-close.test.d.ts.map +0 -1
- package/dist/__tests__/reasoning-close.test.js +0 -219
- package/dist/__tests__/reasoning-close.test.js.map +0 -1
- package/dist/__tests__/thinking-capability.test.d.ts +0 -2
- package/dist/__tests__/thinking-capability.test.d.ts.map +0 -1
- package/dist/__tests__/thinking-capability.test.js +0 -206
- package/dist/__tests__/thinking-capability.test.js.map +0 -1
- package/dist/__tests__/thinking-contract.live.test.d.ts +0 -2
- package/dist/__tests__/thinking-contract.live.test.d.ts.map +0 -1
- package/dist/__tests__/thinking-contract.live.test.js +0 -97
- package/dist/__tests__/thinking-contract.live.test.js.map +0 -1
- package/dist/__tests__/thinking-request-body.test.d.ts +0 -2
- package/dist/__tests__/thinking-request-body.test.d.ts.map +0 -1
- package/dist/__tests__/thinking-request-body.test.js +0 -90
- package/dist/__tests__/thinking-request-body.test.js.map +0 -1
- package/dist/__tests__/wire-contract.live.test.d.ts +0 -2
- package/dist/__tests__/wire-contract.live.test.d.ts.map +0 -1
- package/dist/__tests__/wire-contract.live.test.js +0 -272
- package/dist/__tests__/wire-contract.live.test.js.map +0 -1
- package/src/__tests__/anthropic.test.ts +0 -490
- package/src/__tests__/background-delegation.live.test.ts +0 -243
- package/src/__tests__/block-shape-guard.test.ts +0 -256
- package/src/__tests__/cache-anchors.test.ts +0 -262
- package/src/__tests__/citations.test.ts +0 -218
- package/src/__tests__/content-blocks.test.ts +0 -153
- package/src/__tests__/document-input.test.ts +0 -131
- package/src/__tests__/effort-levels-are-askable.test.ts +0 -86
- package/src/__tests__/error-taxonomy.test.ts +0 -274
- package/src/__tests__/reasoning-close.test.ts +0 -262
- package/src/__tests__/thinking-capability.test.ts +0 -235
- package/src/__tests__/thinking-contract.live.test.ts +0 -125
- package/src/__tests__/thinking-request-body.test.ts +0 -111
- package/src/__tests__/wire-contract.live.test.ts +0 -331
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CompletionInbox,
|
|
3
|
-
ToolRegistry,
|
|
4
|
-
buildCoordinatorTools,
|
|
5
|
-
drainQuery,
|
|
6
|
-
runAgent,
|
|
7
|
-
} from '@namzu/sdk'
|
|
8
|
-
import type { TaskGateway, TaskHandle } from '@namzu/sdk'
|
|
9
|
-
import { describe, expect, it } from 'vitest'
|
|
10
|
-
|
|
11
|
-
import { AnthropicProvider } from '../client.js'
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Does a real model actually receive a worker it stopped waiting for?
|
|
15
|
-
*
|
|
16
|
-
* Everything else about this mechanism is tested against fakes: the inbox
|
|
17
|
-
* queues what nobody claimed, the tools claim what they deliver, the loop
|
|
18
|
-
* drains the rest into the transcript. All of it can be true and the feature
|
|
19
|
-
* still useless, because the part that decides whether it works is whether a
|
|
20
|
-
* MODEL reads the notification and acts on it — and no fake can answer that.
|
|
21
|
-
*
|
|
22
|
-
* So this runs the whole thing against the live API: a real supervisor model,
|
|
23
|
-
* a real worker model, a background launch, and an assertion that the answer
|
|
24
|
-
* the supervisor finally gives contains something only the worker knew.
|
|
25
|
-
*
|
|
26
|
-
* The gateway here is deliberately hand-rolled over `runAgent` rather than
|
|
27
|
-
* the kernel's `LocalTaskGateway`. That is the shape a host implements, so it
|
|
28
|
-
* doubles as proof that the inbox needs nothing from a gateway beyond the
|
|
29
|
-
* `onTaskCompleted` every gateway already has.
|
|
30
|
-
*
|
|
31
|
-
* Skipped without a key. Run with:
|
|
32
|
-
*
|
|
33
|
-
* ANTHROPIC_API_KEY=… pnpm --filter @namzu/anthropic test
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
const KEY = process.env.ANTHROPIC_API_KEY
|
|
37
|
-
const MODEL = process.env.NAMZU_WIRE_TEST_MODEL ?? 'claude-haiku-4-5'
|
|
38
|
-
|
|
39
|
-
/** The fact only the worker can know, so its presence proves delivery. */
|
|
40
|
-
const SECRET = 'PELICAN-7731'
|
|
41
|
-
|
|
42
|
-
function provider(): AnthropicProvider {
|
|
43
|
-
return new AnthropicProvider({ apiKey: KEY as string, model: MODEL })
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* A host-shaped gateway: spawns a real child run per task and announces it.
|
|
48
|
-
*
|
|
49
|
-
* Nothing here is kernel code. It implements `TaskGateway` the way an
|
|
50
|
-
* embedding application does, which is the point — the inbox attaches to it
|
|
51
|
-
* unchanged.
|
|
52
|
-
*/
|
|
53
|
-
function liveGateway(): TaskGateway {
|
|
54
|
-
const handles = new Map<string, TaskHandle>()
|
|
55
|
-
const settled = new Map<string, Promise<TaskHandle>>()
|
|
56
|
-
const listeners = new Set<(h: TaskHandle) => void>()
|
|
57
|
-
let seq = 0
|
|
58
|
-
|
|
59
|
-
return {
|
|
60
|
-
async createTask({ agentId, prompt }) {
|
|
61
|
-
seq += 1
|
|
62
|
-
const taskId = `tsk_live_${seq}`
|
|
63
|
-
const handle: TaskHandle = {
|
|
64
|
-
taskId: taskId as TaskHandle['taskId'],
|
|
65
|
-
agentId,
|
|
66
|
-
state: 'running',
|
|
67
|
-
createdAt: Date.now(),
|
|
68
|
-
}
|
|
69
|
-
handles.set(taskId, handle)
|
|
70
|
-
|
|
71
|
-
settled.set(
|
|
72
|
-
taskId,
|
|
73
|
-
runAgent({
|
|
74
|
-
provider: provider(),
|
|
75
|
-
model: MODEL,
|
|
76
|
-
prompt,
|
|
77
|
-
instructions: 'Answer in one short sentence. Do not explain yourself.',
|
|
78
|
-
maxIterations: 2,
|
|
79
|
-
tokenBudget: 20_000,
|
|
80
|
-
}).then((run) => {
|
|
81
|
-
const done: TaskHandle = {
|
|
82
|
-
...handle,
|
|
83
|
-
state: 'completed',
|
|
84
|
-
completedAt: Date.now(),
|
|
85
|
-
result: {
|
|
86
|
-
status: 'completed',
|
|
87
|
-
result: run.output ?? '',
|
|
88
|
-
} as TaskHandle['result'],
|
|
89
|
-
}
|
|
90
|
-
handles.set(taskId, done)
|
|
91
|
-
for (const cb of listeners) cb(done)
|
|
92
|
-
return done
|
|
93
|
-
}),
|
|
94
|
-
)
|
|
95
|
-
|
|
96
|
-
return handle
|
|
97
|
-
},
|
|
98
|
-
waitForTask: async (taskId) => {
|
|
99
|
-
const pending = settled.get(taskId)
|
|
100
|
-
if (!pending) throw new Error(`no task ${taskId}`)
|
|
101
|
-
return pending
|
|
102
|
-
},
|
|
103
|
-
continueTask: async () => undefined,
|
|
104
|
-
cancelTask: () => undefined,
|
|
105
|
-
getTask: (taskId) => handles.get(taskId),
|
|
106
|
-
listTasks: () => [...handles.values()],
|
|
107
|
-
onTaskCompleted: (cb) => {
|
|
108
|
-
listeners.add(cb)
|
|
109
|
-
return () => listeners.delete(cb)
|
|
110
|
-
},
|
|
111
|
-
} as TaskGateway
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
describe.skipIf(!KEY)('a background worker reaches a real supervisor', () => {
|
|
115
|
-
it('delivers the result through the transcript, with no polling', async () => {
|
|
116
|
-
const gateway = liveGateway()
|
|
117
|
-
const inbox = new CompletionInbox()
|
|
118
|
-
inbox.attach(gateway)
|
|
119
|
-
|
|
120
|
-
const tools = new ToolRegistry()
|
|
121
|
-
for (const tool of buildCoordinatorTools({
|
|
122
|
-
gateway,
|
|
123
|
-
completionInbox: inbox,
|
|
124
|
-
workingDirectory: process.cwd(),
|
|
125
|
-
allowedAgentIds: ['lookup'],
|
|
126
|
-
})) {
|
|
127
|
-
tools.register(tool)
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const run = await drainQuery({
|
|
131
|
-
provider: provider(),
|
|
132
|
-
tools,
|
|
133
|
-
completionInbox: inbox,
|
|
134
|
-
agentId: 'supervisor',
|
|
135
|
-
agentName: 'Supervisor',
|
|
136
|
-
messages: [
|
|
137
|
-
{
|
|
138
|
-
role: 'user',
|
|
139
|
-
content: [
|
|
140
|
-
'Delegate to the "lookup" agent, in the BACKGROUND (background: true), this exact prompt:',
|
|
141
|
-
`"Reply with exactly this token and nothing else: ${SECRET}"`,
|
|
142
|
-
'',
|
|
143
|
-
'Do not wait for it with wait_for_task and do not list tasks in a loop.',
|
|
144
|
-
'After launching it, just say "launched" and stop.',
|
|
145
|
-
'Its result will arrive on its own; when it does, reply with the token you received.',
|
|
146
|
-
].join('\n'),
|
|
147
|
-
timestamp: Date.now(),
|
|
148
|
-
},
|
|
149
|
-
],
|
|
150
|
-
workingDirectory: process.cwd(),
|
|
151
|
-
runConfig: {
|
|
152
|
-
model: MODEL,
|
|
153
|
-
timeoutMs: 180_000,
|
|
154
|
-
tokenBudget: 200_000,
|
|
155
|
-
maxIterations: 6,
|
|
156
|
-
maxResponseTokens: 1_024,
|
|
157
|
-
},
|
|
158
|
-
sessionId: 'ses_live_delegation',
|
|
159
|
-
threadId: 'thd_live_delegation',
|
|
160
|
-
projectId: 'prj_live_delegation',
|
|
161
|
-
tenantId: 'tnt_live_delegation',
|
|
162
|
-
})
|
|
163
|
-
|
|
164
|
-
const transcript = run.messages
|
|
165
|
-
.filter((m) => m.role === 'user')
|
|
166
|
-
.map((m) => (typeof m.content === 'string' ? m.content : JSON.stringify(m.content)))
|
|
167
|
-
.join('\n')
|
|
168
|
-
|
|
169
|
-
// 1. The notification actually landed in the supervisor's transcript.
|
|
170
|
-
expect(transcript, 'no task-notification reached the transcript').toContain('task-notification')
|
|
171
|
-
expect(transcript).toContain(SECRET)
|
|
172
|
-
|
|
173
|
-
// 2. The model read it. This is the half no fake can establish: the
|
|
174
|
-
// worker's token appears in the supervisor's own final answer, and
|
|
175
|
-
// the supervisor never had it any other way.
|
|
176
|
-
expect(run.result ?? '', 'the supervisor never used what it was told').toContain(SECRET)
|
|
177
|
-
|
|
178
|
-
// 3. It got there without polling, which is the whole complaint the
|
|
179
|
-
// fix answers — `agent_task_list` in a sleep loop was the only move
|
|
180
|
-
// on the board before this.
|
|
181
|
-
const listCalls = (run.steps ?? [])
|
|
182
|
-
.flatMap((s) => s.toolCalls ?? [])
|
|
183
|
-
.filter((c) => c.function.name === 'agent_task_list')
|
|
184
|
-
expect(listCalls.length, 'the model still had to poll').toBe(0)
|
|
185
|
-
}, 300_000)
|
|
186
|
-
|
|
187
|
-
it('still delivers the result inline when the launch is not backgrounded', async () => {
|
|
188
|
-
// The path `dc16d58` protects: a blocking launch answers as its own
|
|
189
|
-
// tool_result, and must NOT also arrive as an envelope.
|
|
190
|
-
const gateway = liveGateway()
|
|
191
|
-
const inbox = new CompletionInbox()
|
|
192
|
-
inbox.attach(gateway)
|
|
193
|
-
|
|
194
|
-
const tools = new ToolRegistry()
|
|
195
|
-
for (const tool of buildCoordinatorTools({
|
|
196
|
-
gateway,
|
|
197
|
-
completionInbox: inbox,
|
|
198
|
-
workingDirectory: process.cwd(),
|
|
199
|
-
allowedAgentIds: ['lookup'],
|
|
200
|
-
})) {
|
|
201
|
-
tools.register(tool)
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const run = await drainQuery({
|
|
205
|
-
provider: provider(),
|
|
206
|
-
tools,
|
|
207
|
-
completionInbox: inbox,
|
|
208
|
-
agentId: 'supervisor',
|
|
209
|
-
agentName: 'Supervisor',
|
|
210
|
-
messages: [
|
|
211
|
-
{
|
|
212
|
-
role: 'user',
|
|
213
|
-
content: `Use create_task on the "lookup" agent with this prompt: "Reply with exactly this token and nothing else: ${SECRET}". Do NOT set background. Then report the token it returned.`,
|
|
214
|
-
timestamp: Date.now(),
|
|
215
|
-
},
|
|
216
|
-
],
|
|
217
|
-
workingDirectory: process.cwd(),
|
|
218
|
-
runConfig: {
|
|
219
|
-
model: MODEL,
|
|
220
|
-
timeoutMs: 180_000,
|
|
221
|
-
tokenBudget: 200_000,
|
|
222
|
-
maxIterations: 6,
|
|
223
|
-
maxResponseTokens: 1_024,
|
|
224
|
-
},
|
|
225
|
-
sessionId: 'ses_live_delegation',
|
|
226
|
-
threadId: 'thd_live_delegation',
|
|
227
|
-
projectId: 'prj_live_delegation',
|
|
228
|
-
tenantId: 'tnt_live_delegation',
|
|
229
|
-
})
|
|
230
|
-
|
|
231
|
-
expect(run.result ?? '').toContain(SECRET)
|
|
232
|
-
|
|
233
|
-
const transcript = run.messages
|
|
234
|
-
.filter((m) => m.role === 'user')
|
|
235
|
-
.map((m) => (typeof m.content === 'string' ? m.content : JSON.stringify(m.content)))
|
|
236
|
-
.join('\n')
|
|
237
|
-
|
|
238
|
-
// Delivered once, as a tool_result — no duplicate envelope.
|
|
239
|
-
expect(transcript, 'the blocking result was announced a second time').not.toContain(
|
|
240
|
-
'task-notification',
|
|
241
|
-
)
|
|
242
|
-
}, 300_000)
|
|
243
|
-
})
|
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
import type { ChatCompletionParams } from '@namzu/sdk'
|
|
2
|
-
import { describe, expect, it, vi } from 'vitest'
|
|
3
|
-
|
|
4
|
-
import { AnthropicProvider } from '../client.js'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* The wire rejects a whole request over one malformed block, so the
|
|
8
|
-
* failures that matter here are structural: a block in the wrong place, a
|
|
9
|
-
* message whose content array is empty, an orphan tool result, or a
|
|
10
|
-
* `tool_choice` sent without tools. None of them is visible in isolation —
|
|
11
|
-
* each shows up as a 400 for the entire conversation.
|
|
12
|
-
*
|
|
13
|
-
* These pin the shapes the mapper must not produce, alongside the ones it
|
|
14
|
-
* must. What must NOT hold is the point: a guard asserting only the happy
|
|
15
|
-
* shape passes while the mapper emits a request no server accepts.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
function bodyCapturer() {
|
|
19
|
-
const seen: { body?: Record<string, unknown> } = {}
|
|
20
|
-
const provider = new AnthropicProvider({ apiKey: 'test-key' })
|
|
21
|
-
;(provider as unknown as { client: { messages: { create: unknown } } }).client = {
|
|
22
|
-
messages: {
|
|
23
|
-
create: vi.fn(async (body: Record<string, unknown>) => {
|
|
24
|
-
seen.body = body
|
|
25
|
-
return (async function* () {
|
|
26
|
-
yield { type: 'message_start', message: { id: 'msg_1' } }
|
|
27
|
-
})()
|
|
28
|
-
}),
|
|
29
|
-
},
|
|
30
|
-
}
|
|
31
|
-
return { provider, seen }
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async function bodyFor(params: Partial<ChatCompletionParams>): Promise<Record<string, unknown>> {
|
|
35
|
-
const { provider, seen } = bodyCapturer()
|
|
36
|
-
for await (const _chunk of provider.chatStream({
|
|
37
|
-
model: 'm',
|
|
38
|
-
messages: [{ role: 'user', content: 'hi' }],
|
|
39
|
-
...params,
|
|
40
|
-
} as ChatCompletionParams)) {
|
|
41
|
-
// drain
|
|
42
|
-
}
|
|
43
|
-
return seen.body ?? {}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
type WireMessage = { role: string; content: string | Record<string, unknown>[] }
|
|
47
|
-
|
|
48
|
-
function messagesOf(body: Record<string, unknown>): WireMessage[] {
|
|
49
|
-
return (body.messages ?? []) as WireMessage[]
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function allBlocks(body: Record<string, unknown>): Record<string, unknown>[] {
|
|
53
|
-
return messagesOf(body).flatMap((m) => (Array.isArray(m.content) ? m.content : []))
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const TOOLS = [
|
|
57
|
-
{
|
|
58
|
-
type: 'function' as const,
|
|
59
|
-
function: { name: 'read', description: 'read', parameters: { type: 'object' } },
|
|
60
|
-
},
|
|
61
|
-
]
|
|
62
|
-
|
|
63
|
-
describe('no message goes out with an empty content array', () => {
|
|
64
|
-
it('holds for a plain conversation', async () => {
|
|
65
|
-
const body = await bodyFor({
|
|
66
|
-
messages: [
|
|
67
|
-
{ role: 'user', content: 'a' },
|
|
68
|
-
{ role: 'assistant', content: 'b' },
|
|
69
|
-
] as ChatCompletionParams['messages'],
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
for (const message of messagesOf(body)) {
|
|
73
|
-
if (Array.isArray(message.content)) expect(message.content.length).toBeGreaterThan(0)
|
|
74
|
-
}
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
it('holds for an assistant turn that called a tool with no prose', async () => {
|
|
78
|
-
const body = await bodyFor({
|
|
79
|
-
messages: [
|
|
80
|
-
{ role: 'user', content: 'read it' },
|
|
81
|
-
{
|
|
82
|
-
role: 'assistant',
|
|
83
|
-
content: null,
|
|
84
|
-
toolCalls: [
|
|
85
|
-
{ id: 'call_1', type: 'function', function: { name: 'read', arguments: '{}' } },
|
|
86
|
-
],
|
|
87
|
-
},
|
|
88
|
-
{ role: 'tool', toolCallId: 'call_1', content: 'file body' },
|
|
89
|
-
] as unknown as ChatCompletionParams['messages'],
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
for (const message of messagesOf(body)) {
|
|
93
|
-
if (Array.isArray(message.content)) expect(message.content.length).toBeGreaterThan(0)
|
|
94
|
-
}
|
|
95
|
-
})
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
describe('a system message never appears among the turns', () => {
|
|
99
|
-
it('is lifted out entirely, not left as a user message', async () => {
|
|
100
|
-
const body = await bodyFor({
|
|
101
|
-
messages: [
|
|
102
|
-
{ role: 'system', content: 'be terse' },
|
|
103
|
-
{ role: 'user', content: 'hi' },
|
|
104
|
-
] as ChatCompletionParams['messages'],
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
expect(messagesOf(body).map((m) => m.role)).toEqual(['user'])
|
|
108
|
-
expect(body.system).toBeDefined()
|
|
109
|
-
})
|
|
110
|
-
})
|
|
111
|
-
|
|
112
|
-
describe('tool results are grouped into one user turn, in order', () => {
|
|
113
|
-
it('collapses two results answering one assistant turn', async () => {
|
|
114
|
-
const body = await bodyFor({
|
|
115
|
-
messages: [
|
|
116
|
-
{ role: 'user', content: 'read both' },
|
|
117
|
-
{
|
|
118
|
-
role: 'assistant',
|
|
119
|
-
content: null,
|
|
120
|
-
toolCalls: [
|
|
121
|
-
{ id: 'call_1', type: 'function', function: { name: 'read', arguments: '{}' } },
|
|
122
|
-
{ id: 'call_2', type: 'function', function: { name: 'read', arguments: '{}' } },
|
|
123
|
-
],
|
|
124
|
-
},
|
|
125
|
-
{ role: 'tool', toolCallId: 'call_1', content: 'one' },
|
|
126
|
-
{ role: 'tool', toolCallId: 'call_2', content: 'two' },
|
|
127
|
-
] as unknown as ChatCompletionParams['messages'],
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
const resultTurns = messagesOf(body).filter(
|
|
131
|
-
(m) => Array.isArray(m.content) && m.content.every((b) => b.type === 'tool_result'),
|
|
132
|
-
)
|
|
133
|
-
expect(resultTurns).toHaveLength(1)
|
|
134
|
-
expect(
|
|
135
|
-
(resultTurns[0]?.content as Record<string, unknown>[]).map((b) => b.tool_use_id),
|
|
136
|
-
).toEqual(['call_1', 'call_2'])
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
it('every tool result answers a tool call that was actually made', async () => {
|
|
140
|
-
const body = await bodyFor({
|
|
141
|
-
messages: [
|
|
142
|
-
{ role: 'user', content: 'read it' },
|
|
143
|
-
{
|
|
144
|
-
role: 'assistant',
|
|
145
|
-
content: null,
|
|
146
|
-
toolCalls: [
|
|
147
|
-
{ id: 'call_1', type: 'function', function: { name: 'read', arguments: '{}' } },
|
|
148
|
-
],
|
|
149
|
-
},
|
|
150
|
-
{ role: 'tool', toolCallId: 'call_1', content: 'body' },
|
|
151
|
-
] as unknown as ChatCompletionParams['messages'],
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
const issued = new Set(
|
|
155
|
-
allBlocks(body)
|
|
156
|
-
.filter((b) => b.type === 'tool_use')
|
|
157
|
-
.map((b) => b.id),
|
|
158
|
-
)
|
|
159
|
-
for (const result of allBlocks(body).filter((b) => b.type === 'tool_result')) {
|
|
160
|
-
// An orphan tool_result is rejected for the whole conversation.
|
|
161
|
-
expect(issued.has(result.tool_use_id)).toBe(true)
|
|
162
|
-
}
|
|
163
|
-
})
|
|
164
|
-
|
|
165
|
-
it('places the results after the turn that asked for them', async () => {
|
|
166
|
-
const body = await bodyFor({
|
|
167
|
-
messages: [
|
|
168
|
-
{ role: 'user', content: 'read it' },
|
|
169
|
-
{
|
|
170
|
-
role: 'assistant',
|
|
171
|
-
content: null,
|
|
172
|
-
toolCalls: [
|
|
173
|
-
{ id: 'call_1', type: 'function', function: { name: 'read', arguments: '{}' } },
|
|
174
|
-
],
|
|
175
|
-
},
|
|
176
|
-
{ role: 'tool', toolCallId: 'call_1', content: 'body' },
|
|
177
|
-
] as unknown as ChatCompletionParams['messages'],
|
|
178
|
-
})
|
|
179
|
-
const roles = messagesOf(body).map((m) => m.role)
|
|
180
|
-
|
|
181
|
-
expect(roles).toEqual(['user', 'assistant', 'user'])
|
|
182
|
-
})
|
|
183
|
-
})
|
|
184
|
-
|
|
185
|
-
describe('a malformed tool argument does not become a malformed block', () => {
|
|
186
|
-
it('sends an empty object rather than an unparsed string', async () => {
|
|
187
|
-
const body = await bodyFor({
|
|
188
|
-
messages: [
|
|
189
|
-
{ role: 'user', content: 'go' },
|
|
190
|
-
{
|
|
191
|
-
role: 'assistant',
|
|
192
|
-
content: null,
|
|
193
|
-
toolCalls: [
|
|
194
|
-
{ id: 'call_1', type: 'function', function: { name: 'read', arguments: '{not json' } },
|
|
195
|
-
],
|
|
196
|
-
},
|
|
197
|
-
] as unknown as ChatCompletionParams['messages'],
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
expect(allBlocks(body).find((b) => b.type === 'tool_use')?.input).toEqual({})
|
|
201
|
-
})
|
|
202
|
-
|
|
203
|
-
it('reads empty arguments as an empty object', async () => {
|
|
204
|
-
const body = await bodyFor({
|
|
205
|
-
messages: [
|
|
206
|
-
{ role: 'user', content: 'go' },
|
|
207
|
-
{
|
|
208
|
-
role: 'assistant',
|
|
209
|
-
content: null,
|
|
210
|
-
toolCalls: [
|
|
211
|
-
{ id: 'call_1', type: 'function', function: { name: 'read', arguments: '' } },
|
|
212
|
-
],
|
|
213
|
-
},
|
|
214
|
-
] as unknown as ChatCompletionParams['messages'],
|
|
215
|
-
})
|
|
216
|
-
|
|
217
|
-
expect(allBlocks(body).find((b) => b.type === 'tool_use')?.input).toEqual({})
|
|
218
|
-
})
|
|
219
|
-
})
|
|
220
|
-
|
|
221
|
-
describe('tool_choice is only legal beside tools', () => {
|
|
222
|
-
it('is sent when tools are', async () => {
|
|
223
|
-
const body = await bodyFor({ tools: TOOLS, toolChoice: 'required' })
|
|
224
|
-
|
|
225
|
-
expect(body.tools).toBeDefined()
|
|
226
|
-
expect(body.tool_choice).toBeDefined()
|
|
227
|
-
})
|
|
228
|
-
|
|
229
|
-
it('is withheld when there are no tools', async () => {
|
|
230
|
-
const body = await bodyFor({ toolChoice: 'required' })
|
|
231
|
-
|
|
232
|
-
// The API rejects the request outright; a driver that forwards it
|
|
233
|
-
// turns a caller's harmless default into a hard failure.
|
|
234
|
-
expect(body.tool_choice).toBeUndefined()
|
|
235
|
-
})
|
|
236
|
-
|
|
237
|
-
it('is withheld when a parallel-call hint is the only reason for it', async () => {
|
|
238
|
-
const body = await bodyFor({ parallelToolCalls: false })
|
|
239
|
-
|
|
240
|
-
expect(body.tool_choice).toBeUndefined()
|
|
241
|
-
})
|
|
242
|
-
})
|
|
243
|
-
|
|
244
|
-
describe('the required fields are always present', () => {
|
|
245
|
-
it('always sends max_tokens, which the API demands', async () => {
|
|
246
|
-
expect(await bodyFor({})).toHaveProperty('max_tokens')
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
it('honours the caller max over the default', async () => {
|
|
250
|
-
expect(await bodyFor({ maxTokens: 128 })).toMatchObject({ max_tokens: 128 })
|
|
251
|
-
})
|
|
252
|
-
|
|
253
|
-
it('marks the request as streaming', async () => {
|
|
254
|
-
expect(await bodyFor({})).toMatchObject({ stream: true })
|
|
255
|
-
})
|
|
256
|
-
})
|