Skip to content

Commit 082b50d

Browse files
committed
1 parent 500152b commit 082b50d

4 files changed

Lines changed: 139 additions & 2 deletions

File tree

‎src/api/providers/__tests__/minimax.spec.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,57 @@ describe("MiniMaxHandler", () => {
342342
expect(firstChunk.value).toEqual({ type: "reasoning", text: thinkingContent })
343343
})
344344

345+
it("captures thinking signatures for the next tool-loop request", async () => {
346+
mockCreate.mockResolvedValueOnce(
347+
asyncStreamFrom([
348+
{
349+
type: "content_block_delta",
350+
index: 0,
351+
delta: { type: "thinking_delta", thinking: "Inspect the file." },
352+
},
353+
{
354+
type: "content_block_delta",
355+
index: 0,
356+
delta: { type: "signature_delta", signature: "signed-reasoning" },
357+
},
358+
]),
359+
)
360+
361+
const chunks = await collectStream(handler.createMessage("system prompt", []))
362+
363+
expect(chunks).toEqual([{ type: "reasoning", text: "Inspect the file." }])
364+
expect(handler.getThoughtSignature()).toBe("signed-reasoning")
365+
})
366+
367+
it("filters legacy reasoning blocks while preserving signed thinking blocks", async () => {
368+
mockCreate.mockResolvedValueOnce(asyncStreamFrom([]))
369+
// The Anthropic SDK does not model Zoo Code's legacy internal reasoning block,
370+
// which can still be present in persisted conversation history.
371+
const messages = [
372+
{
373+
role: "assistant",
374+
content: [
375+
{ type: "reasoning", text: "legacy unsigned reasoning", summary: [] },
376+
{ type: "thinking", thinking: "signed reasoning", signature: "signature" },
377+
{ type: "text", text: "I will inspect the file." },
378+
],
379+
},
380+
] as unknown as Anthropic.Messages.MessageParam[]
381+
382+
await collectStream(handler.createMessage("system prompt", messages))
383+
384+
const request = mockCreate.mock.calls[0][0] as Anthropic.Messages.MessageCreateParams
385+
expect(request.messages).toEqual([
386+
{
387+
role: "assistant",
388+
content: [
389+
{ type: "thinking", thinking: "signed reasoning", signature: "signature" },
390+
{ type: "text", text: "I will inspect the file." },
391+
],
392+
},
393+
])
394+
})
395+
345396
it("should handle tool calls in stream", async () => {
346397
mockCreate.mockResolvedValueOnce(
347398
asyncStreamFrom([

‎src/api/providers/minimax.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { ApiHandlerOptions } from "../../shared/api"
1010
import { ApiStream } from "../transform/stream"
1111
import { getModelParams } from "../transform/model-params"
1212
import { mergeEnvironmentDetailsForMiniMax } from "../transform/minimax-format"
13+
import { filterNonAnthropicBlocks } from "../transform/anthropic-filter"
1314

1415
import { BaseProvider } from "./base-provider"
1516
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata, CompletePromptOptions } from "../index"
@@ -53,6 +54,7 @@ function convertOpenAIToolChoice(
5354
export class MiniMaxHandler extends BaseProvider implements SingleCompletionHandler {
5455
private options: ApiHandlerOptions
5556
private client: Anthropic
57+
private lastThoughtSignature?: string
5658

5759
constructor(options: ApiHandlerOptions) {
5860
super()
@@ -84,6 +86,7 @@ export class MiniMaxHandler extends BaseProvider implements SingleCompletionHand
8486
): ApiStream {
8587
const cacheControl: CacheControlEphemeral = { type: "ephemeral" }
8688
const { id: modelId, info, maxTokens, temperature } = this.getModel()
89+
this.lastThoughtSignature = undefined
8790

8891
// MiniMax M2 models support prompt caching
8992
const supportsPromptCache = info.supportsPromptCache ?? false
@@ -92,7 +95,7 @@ export class MiniMaxHandler extends BaseProvider implements SingleCompletionHand
9295
// into the tool_result content. This preserves reasoning continuity for
9396
// thinking models by preventing user messages from interrupting the
9497
// reasoning context after tool use (similar to r1-format's mergeToolResultText).
95-
const processedMessages = mergeEnvironmentDetailsForMiniMax(messages)
98+
const processedMessages = filterNonAnthropicBlocks(mergeEnvironmentDetailsForMiniMax(messages))
9699

97100
// Build the system blocks array
98101
const systemBlocks: Anthropic.Messages.TextBlockParam[] = [
@@ -194,6 +197,9 @@ export class MiniMaxHandler extends BaseProvider implements SingleCompletionHand
194197
case "thinking_delta":
195198
yield { type: "reasoning", text: chunk.delta.thinking }
196199
break
200+
case "signature_delta":
201+
this.lastThoughtSignature = chunk.delta.signature
202+
break
197203
case "text_delta":
198204
yield { type: "text", text: chunk.delta.text }
199205
break
@@ -236,6 +242,10 @@ export class MiniMaxHandler extends BaseProvider implements SingleCompletionHand
236242
}
237243
}
238244

245+
getThoughtSignature(): string | undefined {
246+
return this.lastThoughtSignature
247+
}
248+
239249
/**
240250
* Add cache control to the last two user messages for prompt caching
241251
*/

‎src/api/transform/__tests__/bedrock-converse-format.spec.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,61 @@ describe("convertToBedrockConverseMessages", () => {
2626
])
2727
})
2828

29+
it("converts internal reasoning blocks to Bedrock reasoning content", () => {
30+
// The Anthropic SDK does not model Zoo Code's internal reasoning block,
31+
// though this converter receives it from persisted conversation history.
32+
const messages = [
33+
{
34+
role: "assistant",
35+
content: [{ type: "reasoning", text: "I should inspect the file first.", summary: [] }],
36+
},
37+
] as unknown as Anthropic.Messages.MessageParam[]
38+
39+
expect(convertToBedrockConverseMessages(messages)).toEqual([
40+
{
41+
role: "assistant",
42+
content: [
43+
{
44+
reasoningContent: {
45+
reasoningText: { text: "I should inspect the file first." },
46+
},
47+
},
48+
],
49+
},
50+
])
51+
})
52+
53+
it("converts signed thinking blocks to Bedrock reasoning content", () => {
54+
const messages: Anthropic.Messages.MessageParam[] = [
55+
{
56+
role: "assistant",
57+
content: [
58+
{
59+
type: "thinking",
60+
thinking: "I should inspect the file first.",
61+
signature: "signed-reasoning",
62+
},
63+
],
64+
},
65+
]
66+
67+
expect(convertToBedrockConverseMessages(messages)).toEqual([
68+
{
69+
role: "assistant",
70+
content: [
71+
{
72+
reasoningContent: {
73+
reasoningText: {
74+
text: "I should inspect the file first.",
75+
signature: "signed-reasoning",
76+
},
77+
},
78+
},
79+
],
80+
},
81+
])
82+
})
83+
2984
it("converts messages with images correctly", () => {
3085
const messages: Anthropic.Messages.MessageParam[] = [
3186
{

‎src/api/transform/bedrock-converse-format.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { ConversationRole, Message, ContentBlock } from "@aws-sdk/client-bedrock
33
import { sanitizeOpenAiCallId } from "../../utils/tool-id"
44

55
interface BedrockMessageContent {
6-
type: "text" | "image" | "video" | "tool_use" | "tool_result"
6+
type: "text" | "image" | "video" | "tool_use" | "tool_result" | "reasoning" | "thinking"
77
text?: string
8+
thinking?: string
9+
signature?: string
810
source?: {
911
type: "base64"
1012
data: string | Uint8Array // string for Anthropic, Uint8Array for Bedrock
@@ -58,6 +60,25 @@ export function convertToBedrockConverseMessages(anthropicMessages: Anthropic.Me
5860
} as ContentBlock
5961
}
6062

63+
if (messageBlock.type === "reasoning" && typeof messageBlock.text === "string") {
64+
return {
65+
reasoningContent: {
66+
reasoningText: { text: messageBlock.text },
67+
},
68+
} as ContentBlock
69+
}
70+
71+
if (messageBlock.type === "thinking" && typeof messageBlock.thinking === "string") {
72+
return {
73+
reasoningContent: {
74+
reasoningText: {
75+
text: messageBlock.thinking,
76+
...(messageBlock.signature ? { signature: messageBlock.signature } : {}),
77+
},
78+
},
79+
} as ContentBlock
80+
}
81+
6182
if (messageBlock.type === "image" && messageBlock.source) {
6283
// Convert base64 string to byte array if needed
6384
let byteArray: Uint8Array

0 commit comments

Comments
 (0)