@ai-sdk/anthropic 3.0.145 → 3.0.146
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 +6 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +32 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +32 -9
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +31 -8
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +31 -8
- package/dist/internal/index.mjs.map +1 -1
- package/docs/05-anthropic.mdx +23 -0
- package/package.json +1 -1
- package/src/anthropic-messages-api.ts +2 -2
- package/src/anthropic-messages-language-model.ts +2 -0
- package/src/anthropic-messages-options.ts +10 -0
- package/src/convert-to-anthropic-messages-prompt.ts +33 -4
package/docs/05-anthropic.mdx
CHANGED
|
@@ -124,6 +124,15 @@ The following optional provider options are available for Anthropic models:
|
|
|
124
124
|
Without this opt-in, emitted by-value definitions are rejected before sending
|
|
125
125
|
the request, including with default or automatic tool choice.
|
|
126
126
|
|
|
127
|
+
- `midConversationSystemMessages` _boolean_
|
|
128
|
+
|
|
129
|
+
Optional. Defaults to `false`. Keeps a system message that follows user or
|
|
130
|
+
assistant messages in `messages` even when no system message comes before
|
|
131
|
+
it. By default the first system message becomes the top-level `system`
|
|
132
|
+
prompt wherever it appears. Enable this only for models that support
|
|
133
|
+
mid-conversation system messages. See
|
|
134
|
+
[Mid-Conversation System Controls](#mid-conversation-system-controls).
|
|
135
|
+
|
|
127
136
|
- `sendReasoning` _boolean_
|
|
128
137
|
|
|
129
138
|
Optional. Include reasoning content in requests sent to the model. Defaults to `true`.
|
|
@@ -1639,6 +1648,20 @@ follow Anthropic's [content placement rules](https://platform.claude.com/docs/en
|
|
|
1639
1648
|
Turn-scoped messages with `clearAt` support text only. Put effort updates in a
|
|
1640
1649
|
separate message, as in the example above.
|
|
1641
1650
|
|
|
1651
|
+
The first system message of a prompt becomes the top-level `system` field even
|
|
1652
|
+
when it follows other messages. To keep such a message in place, for example a
|
|
1653
|
+
reminder appended to a conversation that has no initial instructions, set
|
|
1654
|
+
`providerOptions.anthropic.midConversationSystemMessages: true` on the request.
|
|
1655
|
+
|
|
1656
|
+
Use the same value on every request of a conversation. Changing it moves the
|
|
1657
|
+
message between the top-level `system` field and `messages`, so the next request
|
|
1658
|
+
misses the prompt cache, and thinking blocks from earlier turns can fail
|
|
1659
|
+
Anthropic's [prefix check](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking/prefix-checks).
|
|
1660
|
+
|
|
1661
|
+
A `cacheControl` provider option on a mid-conversation system message marks
|
|
1662
|
+
its text block. When the message has no text and only carries `toolChanges`,
|
|
1663
|
+
it marks the last tool change instead.
|
|
1664
|
+
|
|
1642
1665
|
<Note>
|
|
1643
1666
|
`clearAt` applies only to mid-conversation system messages. On initial system
|
|
1644
1667
|
messages, the provider ignores `clearAt` and effort attached to non-empty
|
package/package.json
CHANGED
|
@@ -43,7 +43,7 @@ export type AnthropicToolChangeContent =
|
|
|
43
43
|
| {
|
|
44
44
|
type: 'tool_addition' | 'tool_removal';
|
|
45
45
|
tool: { type: 'tool_reference'; name: string };
|
|
46
|
-
cache_control?:
|
|
46
|
+
cache_control?: AnthropicCacheControl;
|
|
47
47
|
}
|
|
48
48
|
| {
|
|
49
49
|
type: 'tool_addition';
|
|
@@ -53,7 +53,7 @@ export type AnthropicToolChangeContent =
|
|
|
53
53
|
* from this point on.
|
|
54
54
|
*/
|
|
55
55
|
tool: { type: 'tool_definition'; definition: AnthropicTool };
|
|
56
|
-
cache_control?:
|
|
56
|
+
cache_control?: AnthropicCacheControl;
|
|
57
57
|
};
|
|
58
58
|
|
|
59
59
|
export interface AnthropicUserMessage {
|
|
@@ -622,6 +622,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
622
622
|
toolNameMapping,
|
|
623
623
|
toolsetNames,
|
|
624
624
|
convertMediaPart: this.config.convertMediaPart,
|
|
625
|
+
keepMidConversationSystemMessages:
|
|
626
|
+
anthropicOptions?.midConversationSystemMessages ?? false,
|
|
625
627
|
convertInlineTool: async tool => {
|
|
626
628
|
const {
|
|
627
629
|
tools: definitions,
|
|
@@ -173,6 +173,16 @@ export const anthropicLanguageModelOptions = lazySchema(() => {
|
|
|
173
173
|
*/
|
|
174
174
|
sendReasoning: z.boolean().optional(),
|
|
175
175
|
|
|
176
|
+
/**
|
|
177
|
+
* Keep a system message that follows user or assistant messages in the
|
|
178
|
+
* `messages` array, even when no system message comes before it. By
|
|
179
|
+
* default the first system message becomes the top-level `system`
|
|
180
|
+
* prompt wherever it appears, which models without mid-conversation
|
|
181
|
+
* system message support require. Enable this only for models that
|
|
182
|
+
* support mid-conversation system messages.
|
|
183
|
+
*/
|
|
184
|
+
midConversationSystemMessages: z.boolean().optional(),
|
|
185
|
+
|
|
176
186
|
/**
|
|
177
187
|
* Allow tool definitions supplied by value in system-message tool changes.
|
|
178
188
|
* Defaults to false. Enable only for server-controlled messages: inline
|
|
@@ -193,6 +193,7 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
193
193
|
toolsetNames = {},
|
|
194
194
|
convertMediaPart,
|
|
195
195
|
convertInlineTool,
|
|
196
|
+
keepMidConversationSystemMessages = false,
|
|
196
197
|
}: {
|
|
197
198
|
prompt: LanguageModelV3Prompt;
|
|
198
199
|
sendReasoning: boolean;
|
|
@@ -213,6 +214,13 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
213
214
|
* serialized as toolset member calls.
|
|
214
215
|
*/
|
|
215
216
|
toolsetNames?: Record<string, string>;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Keeps a system message that follows other messages in `messages` even
|
|
220
|
+
* when no system message comes before it, instead of making it the
|
|
221
|
+
* top-level `system` prompt.
|
|
222
|
+
*/
|
|
223
|
+
keepMidConversationSystemMessages?: boolean;
|
|
216
224
|
}): Promise<{
|
|
217
225
|
prompt: AnthropicMessagesPrompt;
|
|
218
226
|
betas: Set<string>;
|
|
@@ -277,10 +285,10 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
277
285
|
|
|
278
286
|
// A system message that only carries message-level options or tool
|
|
279
287
|
// changes may have empty text; do not emit an empty text block for it.
|
|
280
|
-
|
|
288
|
+
const emitsText =
|
|
281
289
|
text !== '' ||
|
|
282
|
-
(toolChanges.length === 0 && clearAt == null && effort == null)
|
|
283
|
-
) {
|
|
290
|
+
(toolChanges.length === 0 && clearAt == null && effort == null);
|
|
291
|
+
if (emitsText) {
|
|
284
292
|
content.push({
|
|
285
293
|
type: 'text' as const,
|
|
286
294
|
text,
|
|
@@ -348,6 +356,26 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
348
356
|
} satisfies AnthropicToolChangeContent);
|
|
349
357
|
}
|
|
350
358
|
|
|
359
|
+
// Without a text block, the message's cache control marks its last
|
|
360
|
+
// tool change, so the cached prefix ends where this message ends.
|
|
361
|
+
// A definition with its own cache control already ends the prefix
|
|
362
|
+
// there, and Anthropic rejects a marker in both places.
|
|
363
|
+
const lastToolChange = content.at(-1);
|
|
364
|
+
const definesOwnCacheControl =
|
|
365
|
+
lastToolChange?.type === 'tool_addition' &&
|
|
366
|
+
lastToolChange.tool.type === 'tool_definition' &&
|
|
367
|
+
'cache_control' in lastToolChange.tool.definition &&
|
|
368
|
+
lastToolChange.tool.definition.cache_control != null;
|
|
369
|
+
if (!emitsText && lastToolChange != null && !definesOwnCacheControl) {
|
|
370
|
+
const cacheControl = validator.getCacheControl(providerOptions, {
|
|
371
|
+
type: 'system message',
|
|
372
|
+
canCache: true,
|
|
373
|
+
});
|
|
374
|
+
if (cacheControl != null) {
|
|
375
|
+
lastToolChange.cache_control = cacheControl;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
351
379
|
// Every change was ignored and nothing else is left to send.
|
|
352
380
|
if (content.length === 0 && clearAt == null && effort == null) {
|
|
353
381
|
continue;
|
|
@@ -371,7 +399,8 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
371
399
|
// text).
|
|
372
400
|
if (
|
|
373
401
|
i === 0 ||
|
|
374
|
-
(
|
|
402
|
+
(!keepMidConversationSystemMessages &&
|
|
403
|
+
system == null &&
|
|
375
404
|
toolChangeCount === 0 &&
|
|
376
405
|
!hasMidConversationOptions)
|
|
377
406
|
) {
|