@namzu/anthropic 1.1.2 → 1.3.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/CHANGELOG.md +289 -0
- package/README.md +10 -0
- package/dist/__tests__/anthropic.test.js +144 -6
- package/dist/__tests__/anthropic.test.js.map +1 -1
- package/dist/__tests__/block-shape-guard.test.d.ts +2 -0
- package/dist/__tests__/block-shape-guard.test.d.ts.map +1 -0
- package/dist/__tests__/block-shape-guard.test.js +218 -0
- package/dist/__tests__/block-shape-guard.test.js.map +1 -0
- package/dist/__tests__/cache-anchors.test.d.ts +2 -0
- package/dist/__tests__/cache-anchors.test.d.ts.map +1 -0
- package/dist/__tests__/cache-anchors.test.js +220 -0
- package/dist/__tests__/cache-anchors.test.js.map +1 -0
- package/dist/__tests__/citations.test.d.ts +2 -0
- package/dist/__tests__/citations.test.d.ts.map +1 -0
- package/dist/__tests__/citations.test.js +189 -0
- package/dist/__tests__/citations.test.js.map +1 -0
- package/dist/__tests__/content-blocks.test.d.ts +2 -0
- package/dist/__tests__/content-blocks.test.d.ts.map +1 -0
- package/dist/__tests__/content-blocks.test.js +131 -0
- package/dist/__tests__/content-blocks.test.js.map +1 -0
- package/dist/__tests__/document-input.test.d.ts +2 -0
- package/dist/__tests__/document-input.test.d.ts.map +1 -0
- package/dist/__tests__/document-input.test.js +98 -0
- package/dist/__tests__/document-input.test.js.map +1 -0
- package/dist/__tests__/reasoning-close.test.d.ts +2 -0
- package/dist/__tests__/reasoning-close.test.d.ts.map +1 -0
- package/dist/__tests__/reasoning-close.test.js +219 -0
- package/dist/__tests__/reasoning-close.test.js.map +1 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +203 -11
- package/dist/client.js.map +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/__tests__/anthropic.test.ts +175 -12
- package/src/__tests__/block-shape-guard.test.ts +256 -0
- package/src/__tests__/cache-anchors.test.ts +262 -0
- package/src/__tests__/citations.test.ts +218 -0
- package/src/__tests__/content-blocks.test.ts +153 -0
- package/src/__tests__/document-input.test.ts +131 -0
- package/src/__tests__/reasoning-close.test.ts +262 -0
- package/src/client.ts +278 -14
- package/src/types.ts +6 -0
package/src/client.ts
CHANGED
|
@@ -3,18 +3,22 @@ import Anthropic from '@anthropic-ai/sdk'
|
|
|
3
3
|
import type {
|
|
4
4
|
ChatCompletionParams,
|
|
5
5
|
ChatCompletionResponse,
|
|
6
|
+
Citation,
|
|
6
7
|
LLMProvider,
|
|
7
8
|
ModelInfo,
|
|
8
9
|
ProviderCapabilities,
|
|
10
|
+
ReasoningBlock,
|
|
9
11
|
StreamChunk,
|
|
10
12
|
TokenUsage,
|
|
11
13
|
ToolChoice,
|
|
14
|
+
ToolResultContent,
|
|
12
15
|
} from '@namzu/sdk'
|
|
13
16
|
import {
|
|
14
17
|
ProviderRequestError,
|
|
15
18
|
isCallerAbortError,
|
|
16
19
|
isProviderRequestError,
|
|
17
20
|
providerVendorError,
|
|
21
|
+
toolResultToText,
|
|
18
22
|
} from '@namzu/sdk'
|
|
19
23
|
import type { AnthropicConfig } from './types.js'
|
|
20
24
|
|
|
@@ -92,7 +96,8 @@ interface AnthropicToolUseBlock {
|
|
|
92
96
|
interface AnthropicToolResultBlock {
|
|
93
97
|
type: 'tool_result'
|
|
94
98
|
tool_use_id: string
|
|
95
|
-
|
|
99
|
+
/** Text, or the blocks this wire accepts inside a tool result. */
|
|
100
|
+
content: string | Array<AnthropicTextBlock | AnthropicImageBlock>
|
|
96
101
|
cache_control?: AnthropicCacheControl
|
|
97
102
|
}
|
|
98
103
|
|
|
@@ -106,13 +111,42 @@ interface AnthropicToolParam {
|
|
|
106
111
|
name: string
|
|
107
112
|
description: string
|
|
108
113
|
input_schema: unknown
|
|
114
|
+
strict?: boolean
|
|
109
115
|
cache_control?: AnthropicCacheControl
|
|
110
116
|
}
|
|
111
117
|
|
|
112
|
-
/** Shape of an SDK `
|
|
118
|
+
/** Shape of an SDK `MessageAttachment` (read structurally to avoid coupling). */
|
|
113
119
|
interface AttachmentLike {
|
|
120
|
+
readonly type?: 'image' | 'document'
|
|
114
121
|
readonly data: string
|
|
115
122
|
readonly mediaType: string
|
|
123
|
+
readonly name?: string
|
|
124
|
+
readonly citations?: boolean
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* A reasoning block, replayed exactly as it arrived.
|
|
129
|
+
*
|
|
130
|
+
* The signature is cryptographic and verified upstream: a block echoed
|
|
131
|
+
* back with its text edited, its signature dropped, or its order changed
|
|
132
|
+
* is rejected, which fails the whole conversation rather than one block.
|
|
133
|
+
* So this is a passthrough, never a re-render.
|
|
134
|
+
*/
|
|
135
|
+
interface AnthropicThinkingBlock {
|
|
136
|
+
type: 'thinking' | 'redacted_thinking'
|
|
137
|
+
thinking?: string
|
|
138
|
+
signature?: string
|
|
139
|
+
data?: string
|
|
140
|
+
cache_control?: AnthropicCacheControl
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
interface AnthropicDocumentBlock {
|
|
144
|
+
type: 'document'
|
|
145
|
+
source: { type: 'base64'; media_type: string; data: string }
|
|
146
|
+
title?: string
|
|
147
|
+
citations?: { enabled: true }
|
|
148
|
+
/** A document is the block most worth a breakpoint, being the largest. */
|
|
149
|
+
cache_control?: AnthropicCacheControl
|
|
116
150
|
}
|
|
117
151
|
|
|
118
152
|
type AnthropicContentBlock =
|
|
@@ -120,6 +154,8 @@ type AnthropicContentBlock =
|
|
|
120
154
|
| AnthropicToolUseBlock
|
|
121
155
|
| AnthropicToolResultBlock
|
|
122
156
|
| AnthropicImageBlock
|
|
157
|
+
| AnthropicDocumentBlock
|
|
158
|
+
| AnthropicThinkingBlock
|
|
123
159
|
|
|
124
160
|
interface AnthropicMessageParam {
|
|
125
161
|
role: 'user' | 'assistant'
|
|
@@ -154,6 +190,64 @@ function extractSystem(
|
|
|
154
190
|
return blocks
|
|
155
191
|
}
|
|
156
192
|
|
|
193
|
+
/**
|
|
194
|
+
* The reasoning blocks an assistant message carries, in wire form.
|
|
195
|
+
*
|
|
196
|
+
* Verbatim by contract, and first in the message: the API requires
|
|
197
|
+
* thinking blocks to lead, and the signature is verified, so re-rendering
|
|
198
|
+
* or reordering them invalidates the turn they belong to.
|
|
199
|
+
*/
|
|
200
|
+
function replayReasoning(msg: { reasoning?: readonly ReasoningBlock[] }): AnthropicThinkingBlock[] {
|
|
201
|
+
if (!msg.reasoning || msg.reasoning.length === 0) return []
|
|
202
|
+
return msg.reasoning.map((block) =>
|
|
203
|
+
block.type === 'redacted_thinking'
|
|
204
|
+
? {
|
|
205
|
+
type: 'redacted_thinking' as const,
|
|
206
|
+
...(block.encrypted ? { data: block.encrypted } : {}),
|
|
207
|
+
}
|
|
208
|
+
: {
|
|
209
|
+
type: 'thinking' as const,
|
|
210
|
+
thinking: block.text ?? '',
|
|
211
|
+
...(block.signature ? { signature: block.signature } : {}),
|
|
212
|
+
},
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* A tool result, in the richest form this wire accepts.
|
|
218
|
+
*
|
|
219
|
+
* The blocks used to be `JSON.stringify`d, so a screenshot reached the
|
|
220
|
+
* model as a wall of quoted base64 — the one thing the SDK's own degrade
|
|
221
|
+
* helper exists to avoid, and pure cost besides: the model paid for every
|
|
222
|
+
* character and could read none of them. This wire carries image blocks in
|
|
223
|
+
* a tool result natively, so they are sent as images.
|
|
224
|
+
*
|
|
225
|
+
* A document block still degrades to the named placeholder: tool results
|
|
226
|
+
* on this wire take text and images, not documents, and inventing an image
|
|
227
|
+
* block around a PDF would fail the request instead of the block.
|
|
228
|
+
*/
|
|
229
|
+
function toolResultContent(
|
|
230
|
+
content: ToolResultContent | undefined,
|
|
231
|
+
): string | Array<AnthropicTextBlock | AnthropicImageBlock> {
|
|
232
|
+
if (content === undefined) return ''
|
|
233
|
+
if (typeof content === 'string') return content
|
|
234
|
+
|
|
235
|
+
const blocks: Array<AnthropicTextBlock | AnthropicImageBlock> = []
|
|
236
|
+
for (const block of content) {
|
|
237
|
+
if (block.type === 'image') {
|
|
238
|
+
blocks.push({
|
|
239
|
+
type: 'image',
|
|
240
|
+
source: { type: 'base64', media_type: block.mediaType, data: block.data },
|
|
241
|
+
})
|
|
242
|
+
continue
|
|
243
|
+
}
|
|
244
|
+
const text = toolResultToText([block])
|
|
245
|
+
if (text.length > 0) blocks.push({ type: 'text', text })
|
|
246
|
+
}
|
|
247
|
+
// An empty array is rejected upstream; an empty string is not.
|
|
248
|
+
return blocks.length > 0 ? blocks : ''
|
|
249
|
+
}
|
|
250
|
+
|
|
157
251
|
function toAnthropicMessages(messages: ChatCompletionParams['messages']): AnthropicMessageParam[] {
|
|
158
252
|
const out: AnthropicMessageParam[] = []
|
|
159
253
|
let pendingToolResults: AnthropicToolResultBlock[] = []
|
|
@@ -169,12 +263,11 @@ function toAnthropicMessages(messages: ChatCompletionParams['messages']): Anthro
|
|
|
169
263
|
if (msg.role === 'system') continue
|
|
170
264
|
|
|
171
265
|
if (msg.role === 'tool') {
|
|
172
|
-
const toolMsg = msg as { toolCallId?: string; content?:
|
|
266
|
+
const toolMsg = msg as { toolCallId?: string; content?: ToolResultContent }
|
|
173
267
|
pendingToolResults.push({
|
|
174
268
|
type: 'tool_result',
|
|
175
269
|
tool_use_id: toolMsg.toolCallId ?? 'unknown',
|
|
176
|
-
content:
|
|
177
|
-
typeof toolMsg.content === 'string' ? toolMsg.content : JSON.stringify(toolMsg.content),
|
|
270
|
+
content: toolResultContent(toolMsg.content),
|
|
178
271
|
})
|
|
179
272
|
continue
|
|
180
273
|
}
|
|
@@ -182,7 +275,7 @@ function toAnthropicMessages(messages: ChatCompletionParams['messages']): Anthro
|
|
|
182
275
|
flushToolResults()
|
|
183
276
|
|
|
184
277
|
if (msg.role === 'assistant' && 'toolCalls' in msg && msg.toolCalls) {
|
|
185
|
-
const blocks: AnthropicContentBlock[] = []
|
|
278
|
+
const blocks: AnthropicContentBlock[] = [...replayReasoning(msg)]
|
|
186
279
|
if (msg.content && typeof msg.content === 'string') {
|
|
187
280
|
blocks.push({ type: 'text', text: msg.content })
|
|
188
281
|
}
|
|
@@ -204,7 +297,16 @@ function toAnthropicMessages(messages: ChatCompletionParams['messages']): Anthro
|
|
|
204
297
|
continue
|
|
205
298
|
}
|
|
206
299
|
|
|
300
|
+
// An assistant turn that reasoned replays those blocks even when it
|
|
301
|
+
// called no tool: dropping them invalidates the turn upstream.
|
|
302
|
+
const replayed = msg.role === 'assistant' ? replayReasoning(msg) : []
|
|
207
303
|
const content = typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content)
|
|
304
|
+
if (replayed.length > 0) {
|
|
305
|
+
const trailing: AnthropicContentBlock[] =
|
|
306
|
+
content.length > 0 ? [{ type: 'text', text: content }] : []
|
|
307
|
+
out.push({ role: 'assistant', content: [...replayed, ...trailing] })
|
|
308
|
+
continue
|
|
309
|
+
}
|
|
208
310
|
// User message with image attachments → multimodal content blocks
|
|
209
311
|
// (text first, then each image as a base64 image block).
|
|
210
312
|
const attachments = (msg as { attachments?: readonly AttachmentLike[] }).attachments
|
|
@@ -212,6 +314,23 @@ function toAnthropicMessages(messages: ChatCompletionParams['messages']): Anthro
|
|
|
212
314
|
const blocks: AnthropicContentBlock[] = []
|
|
213
315
|
if (content.length > 0) blocks.push({ type: 'text', text: content })
|
|
214
316
|
for (const att of attachments) {
|
|
317
|
+
// A document is not an image with a different media type. It
|
|
318
|
+
// used to be mapped as one, so a PDF went up as an image block
|
|
319
|
+
// the API rejects — while the capability set claimed documents
|
|
320
|
+
// were supported. The native block buys page structure, OCR,
|
|
321
|
+
// and the citations the SDK contract already has a slot for.
|
|
322
|
+
if (att.type === 'document') {
|
|
323
|
+
blocks.push({
|
|
324
|
+
type: 'document',
|
|
325
|
+
source: { type: 'base64', media_type: att.mediaType, data: att.data },
|
|
326
|
+
...(att.name ? { title: att.name } : {}),
|
|
327
|
+
// Opt-in: citations cost tokens, so they are requested
|
|
328
|
+
// only when the caller asked to be able to check the
|
|
329
|
+
// answer.
|
|
330
|
+
...(att.citations ? { citations: { enabled: true as const } } : {}),
|
|
331
|
+
})
|
|
332
|
+
continue
|
|
333
|
+
}
|
|
215
334
|
blocks.push({
|
|
216
335
|
type: 'image',
|
|
217
336
|
source: { type: 'base64', media_type: att.mediaType, data: att.data },
|
|
@@ -233,13 +352,19 @@ function toAnthropicMessages(messages: ChatCompletionParams['messages']): Anthro
|
|
|
233
352
|
function toAnthropicTools(
|
|
234
353
|
params: ChatCompletionParams,
|
|
235
354
|
cachingEnabled: boolean,
|
|
355
|
+
strictToolUseEnabled: boolean,
|
|
236
356
|
): AnthropicToolParam[] | undefined {
|
|
237
357
|
if (!params.tools || params.tools.length === 0) return undefined
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
358
|
+
const enforcedNames = new Set(params.enforceToolInputSchema ?? [])
|
|
359
|
+
const tools: AnthropicToolParam[] = params.tools.map((t) => {
|
|
360
|
+
const strict = strictToolUseEnabled && enforcedNames.has(t.function.name)
|
|
361
|
+
return {
|
|
362
|
+
name: t.function.name,
|
|
363
|
+
description: t.function.description ?? '',
|
|
364
|
+
input_schema: t.function.parameters ?? { type: 'object' },
|
|
365
|
+
...(strict ? { strict: true } : {}),
|
|
366
|
+
}
|
|
367
|
+
})
|
|
243
368
|
if (cachingEnabled) {
|
|
244
369
|
// Breakpoint on the tools-array tail: tools render at position 0 of
|
|
245
370
|
// the cache prefix, so this caches every tool schema even when the
|
|
@@ -250,6 +375,24 @@ function toAnthropicTools(
|
|
|
250
375
|
return tools
|
|
251
376
|
}
|
|
252
377
|
|
|
378
|
+
function shouldUseStrictToolInputs(
|
|
379
|
+
model: string,
|
|
380
|
+
mode: AnthropicConfig['strictToolUse'] = 'auto',
|
|
381
|
+
): boolean {
|
|
382
|
+
if (mode === 'on') return true
|
|
383
|
+
if (mode === 'off') return false
|
|
384
|
+
|
|
385
|
+
const normalized = model.toLowerCase()
|
|
386
|
+
if (/^(?:anthropic\/)?claude-mythos-preview$/.test(normalized)) return true
|
|
387
|
+
const version = normalized.match(
|
|
388
|
+
/^(?:anthropic\/)?claude-(?:haiku|sonnet|opus|fable|mythos)-(\d+)(?:[-_.](\d+))?(?:-\d{8})?$/,
|
|
389
|
+
)
|
|
390
|
+
if (!version) return false
|
|
391
|
+
const major = Number(version[1])
|
|
392
|
+
const minor = Number(version[2] ?? 0)
|
|
393
|
+
return major > 4 || (major === 4 && minor >= 5)
|
|
394
|
+
}
|
|
395
|
+
|
|
253
396
|
function toAnthropicToolChoice(tc?: ToolChoice, parallelToolCalls?: boolean): unknown {
|
|
254
397
|
// Anthropic expresses "no parallel tool calls" as a field ON tool_choice,
|
|
255
398
|
// not a top-level param. Only auto/any/tool accept it ('none' fires no
|
|
@@ -357,16 +500,77 @@ function mapStopReason(reason?: string | null): NamzuFinishReason {
|
|
|
357
500
|
// Stream event types
|
|
358
501
|
// --------------------------------------------------------------------------------------
|
|
359
502
|
|
|
503
|
+
/**
|
|
504
|
+
* A cited passage as it arrives on the wire.
|
|
505
|
+
*
|
|
506
|
+
* The location is reported three different ways depending on how the
|
|
507
|
+
* provider segmented the source, which is why the SDK's `Citation` keeps
|
|
508
|
+
* a discriminated union rather than flattening everything to a page
|
|
509
|
+
* number: two of the three shapes have no page to report.
|
|
510
|
+
*/
|
|
511
|
+
interface RawAnthropicCitation {
|
|
512
|
+
type?: string
|
|
513
|
+
cited_text?: string
|
|
514
|
+
document_index?: number
|
|
515
|
+
document_title?: string | null
|
|
516
|
+
start_page_number?: number
|
|
517
|
+
end_page_number?: number
|
|
518
|
+
start_char_index?: number
|
|
519
|
+
end_char_index?: number
|
|
520
|
+
start_block_index?: number
|
|
521
|
+
end_block_index?: number
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Map a wire citation onto the SDK's shape.
|
|
526
|
+
*
|
|
527
|
+
* Returns undefined rather than inventing a location: a citation whose
|
|
528
|
+
* source segment cannot be named is worse than none, because it looks
|
|
529
|
+
* checkable and is not.
|
|
530
|
+
*/
|
|
531
|
+
function toCitation(raw: RawAnthropicCitation, index: number): Citation | undefined {
|
|
532
|
+
const citedText = raw.cited_text
|
|
533
|
+
if (!citedText) return undefined
|
|
534
|
+
|
|
535
|
+
const location = (():
|
|
536
|
+
| { kind: 'page'; start: number; end: number }
|
|
537
|
+
| { kind: 'char'; start: number; end: number }
|
|
538
|
+
| { kind: 'block'; start: number; end: number }
|
|
539
|
+
| undefined => {
|
|
540
|
+
if (raw.start_page_number !== undefined && raw.end_page_number !== undefined) {
|
|
541
|
+
return { kind: 'page', start: raw.start_page_number, end: raw.end_page_number }
|
|
542
|
+
}
|
|
543
|
+
if (raw.start_char_index !== undefined && raw.end_char_index !== undefined) {
|
|
544
|
+
return { kind: 'char', start: raw.start_char_index, end: raw.end_char_index }
|
|
545
|
+
}
|
|
546
|
+
if (raw.start_block_index !== undefined && raw.end_block_index !== undefined) {
|
|
547
|
+
return { kind: 'block', start: raw.start_block_index, end: raw.end_block_index }
|
|
548
|
+
}
|
|
549
|
+
return undefined
|
|
550
|
+
})()
|
|
551
|
+
if (!location) return undefined
|
|
552
|
+
|
|
553
|
+
return {
|
|
554
|
+
citedText,
|
|
555
|
+
documentIndex: raw.document_index ?? index,
|
|
556
|
+
...(raw.document_title ? { documentTitle: raw.document_title } : {}),
|
|
557
|
+
location,
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
360
561
|
interface StreamEvent {
|
|
361
562
|
type: string
|
|
362
563
|
message?: { id?: string; usage?: RawAnthropicUsage }
|
|
363
564
|
index?: number
|
|
364
|
-
content_block?: { type?: string; id?: string; name?: string }
|
|
565
|
+
content_block?: { type?: string; id?: string; name?: string; data?: string }
|
|
365
566
|
delta?: {
|
|
366
567
|
type?: string
|
|
367
568
|
text?: string
|
|
368
569
|
partial_json?: string
|
|
369
570
|
stop_reason?: string | null
|
|
571
|
+
citation?: RawAnthropicCitation
|
|
572
|
+
thinking?: string
|
|
573
|
+
signature?: string
|
|
370
574
|
}
|
|
371
575
|
usage?: RawAnthropicUsage
|
|
372
576
|
}
|
|
@@ -385,6 +589,7 @@ export const ANTHROPIC_CAPABILITIES: ProviderCapabilities = {
|
|
|
385
589
|
supportsStreaming: true,
|
|
386
590
|
supportsFunctionCalling: true,
|
|
387
591
|
supportsVision: true,
|
|
592
|
+
supportsDocuments: true,
|
|
388
593
|
}
|
|
389
594
|
|
|
390
595
|
export class AnthropicProvider implements LLMProvider {
|
|
@@ -445,14 +650,19 @@ export class AnthropicProvider implements LLMProvider {
|
|
|
445
650
|
// is tools → system → messages, so each later breakpoint covers all
|
|
446
651
|
// earlier sections too).
|
|
447
652
|
const cachingEnabled = params.cacheControl !== undefined
|
|
653
|
+
const model = this.resolveModel(params)
|
|
448
654
|
const system = extractSystem(params.messages, cachingEnabled)
|
|
449
655
|
const messages = toAnthropicMessages(params.messages)
|
|
450
656
|
if (cachingEnabled) applyMessageCacheBreakpoint(messages)
|
|
451
|
-
const tools = toAnthropicTools(
|
|
657
|
+
const tools = toAnthropicTools(
|
|
658
|
+
params,
|
|
659
|
+
cachingEnabled,
|
|
660
|
+
shouldUseStrictToolInputs(model, this.config.strictToolUse),
|
|
661
|
+
)
|
|
452
662
|
const toolChoice = toAnthropicToolChoice(params.toolChoice, params.parallelToolCalls)
|
|
453
663
|
|
|
454
664
|
const body: Record<string, unknown> = {
|
|
455
|
-
model
|
|
665
|
+
model,
|
|
456
666
|
messages,
|
|
457
667
|
max_tokens: params.maxTokens ?? this.config.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
458
668
|
stream,
|
|
@@ -476,6 +686,20 @@ export class AnthropicProvider implements LLMProvider {
|
|
|
476
686
|
// otherwise) — this also drops a parallelToolCalls-derived choice on
|
|
477
687
|
// tool-less requests.
|
|
478
688
|
if (tools && toolChoice) body.tool_choice = toolChoice
|
|
689
|
+
if (params.thinking) {
|
|
690
|
+
// Thinking could not be REQUESTED at all before this: on a model
|
|
691
|
+
// where it is off by default the feature was unreachable, and on
|
|
692
|
+
// one where it is on by default its budget could not be tuned.
|
|
693
|
+
body.thinking =
|
|
694
|
+
params.thinking.type === 'enabled'
|
|
695
|
+
? {
|
|
696
|
+
type: 'enabled',
|
|
697
|
+
...(params.thinking.budgetTokens !== undefined
|
|
698
|
+
? { budget_tokens: params.thinking.budgetTokens }
|
|
699
|
+
: {}),
|
|
700
|
+
}
|
|
701
|
+
: { type: 'disabled' }
|
|
702
|
+
}
|
|
479
703
|
if (params.temperature !== undefined) body.temperature = params.temperature
|
|
480
704
|
if (params.topP !== undefined) body.top_p = params.topP
|
|
481
705
|
if (params.topK !== undefined) body.top_k = params.topK
|
|
@@ -528,6 +752,7 @@ export class AnthropicProvider implements LLMProvider {
|
|
|
528
752
|
// Track active tool-use blocks by content_block index so input_json_delta
|
|
529
753
|
// fragments can reference the right tool call.
|
|
530
754
|
const activeTools = new Map<number, { id: string; name: string }>()
|
|
755
|
+
const activeReasoning = new Set<number>()
|
|
531
756
|
|
|
532
757
|
// Anthropic Messages API streams over SSE. Do not impose a
|
|
533
758
|
// provider-local 90s idle cutoff by default: long reasoning or
|
|
@@ -588,6 +813,20 @@ export class AnthropicProvider implements LLMProvider {
|
|
|
588
813
|
case 'content_block_start': {
|
|
589
814
|
const idx = event.index ?? 0
|
|
590
815
|
const block = event.content_block
|
|
816
|
+
if (block?.type === 'thinking' || block?.type === 'redacted_thinking') {
|
|
817
|
+
activeReasoning.add(idx)
|
|
818
|
+
yield {
|
|
819
|
+
id: messageId,
|
|
820
|
+
delta: {
|
|
821
|
+
reasoning: {
|
|
822
|
+
index: idx,
|
|
823
|
+
type: block.type,
|
|
824
|
+
...(block.data ? { encrypted: block.data } : {}),
|
|
825
|
+
},
|
|
826
|
+
},
|
|
827
|
+
}
|
|
828
|
+
break
|
|
829
|
+
}
|
|
591
830
|
if (block?.type === 'tool_use') {
|
|
592
831
|
const toolId = block.id ?? `tool-${Date.now()}`
|
|
593
832
|
activeTools.set(idx, { id: toolId, name: block.name ?? '' })
|
|
@@ -612,6 +851,24 @@ export class AnthropicProvider implements LLMProvider {
|
|
|
612
851
|
const delta = event.delta
|
|
613
852
|
if (delta?.type === 'text_delta' && delta.text) {
|
|
614
853
|
yield { id: messageId, delta: { content: delta.text } }
|
|
854
|
+
} else if (delta?.type === 'thinking_delta' && delta.thinking !== undefined) {
|
|
855
|
+
yield {
|
|
856
|
+
id: messageId,
|
|
857
|
+
delta: { reasoning: { index: idx, text: delta.thinking } },
|
|
858
|
+
}
|
|
859
|
+
} else if (delta?.type === 'signature_delta' && delta.signature !== undefined) {
|
|
860
|
+
// Arrives once, at the end of the block, and has to
|
|
861
|
+
// reach the next request unmodified.
|
|
862
|
+
yield {
|
|
863
|
+
id: messageId,
|
|
864
|
+
delta: { reasoning: { index: idx, signature: delta.signature } },
|
|
865
|
+
}
|
|
866
|
+
} else if (delta?.type === 'citations_delta' && delta.citation) {
|
|
867
|
+
// Not text: a citation lands on the assistant
|
|
868
|
+
// message beside the prose, which is where the
|
|
869
|
+
// runtime's stream aggregator already puts it.
|
|
870
|
+
const citation = toCitation(delta.citation, idx)
|
|
871
|
+
if (citation) yield { id: messageId, delta: { citation } }
|
|
615
872
|
} else if (delta?.type === 'input_json_delta' && delta.partial_json !== undefined) {
|
|
616
873
|
const active = activeTools.get(idx)
|
|
617
874
|
yield {
|
|
@@ -640,6 +897,13 @@ export class AnthropicProvider implements LLMProvider {
|
|
|
640
897
|
// — exactly the failure the live cowork test
|
|
641
898
|
// surfaced (Bash + Write both blank-input failed).
|
|
642
899
|
const idx = event.index ?? 0
|
|
900
|
+
if (activeReasoning.delete(idx)) {
|
|
901
|
+
// Closes the block, exactly as toolCallEnd closes a
|
|
902
|
+
// tool call: without it the aggregator cannot know
|
|
903
|
+
// the signature has finished arriving.
|
|
904
|
+
yield { id: messageId, delta: { reasoning: { index: idx, done: true } } }
|
|
905
|
+
break
|
|
906
|
+
}
|
|
643
907
|
const active = activeTools.get(idx)
|
|
644
908
|
if (active) {
|
|
645
909
|
yield {
|
package/src/types.ts
CHANGED
|
@@ -29,6 +29,12 @@ export interface AnthropicConfig {
|
|
|
29
29
|
defaultHeaders?: Record<string, string>
|
|
30
30
|
/** Default max_tokens (Anthropic requires this field). Default: 64000. */
|
|
31
31
|
maxTokens?: number
|
|
32
|
+
/**
|
|
33
|
+
* Constrained tool-input policy. `auto` enables strict tool use for known
|
|
34
|
+
* Claude 4.5+ model identifiers, `on` opts a compatible proxy alias in,
|
|
35
|
+
* and `off` disables it. Default: `auto`.
|
|
36
|
+
*/
|
|
37
|
+
strictToolUse?: 'auto' | 'on' | 'off'
|
|
32
38
|
/**
|
|
33
39
|
* Bearer-style OAuth access token (mutually exclusive with `apiKey`).
|
|
34
40
|
* Use when the credential is an Anthropic OAuth or Claude Code OAuth
|