MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / convertToR1Format

Function convertToR1Format

src/api/transform/r1-format.ts:39–251  ·  view source on GitHub ↗
(
	messages: AnthropicMessage[],
	options?: {
		mergeToolResultText?: boolean
		normalizeToolCallId?: (id: string) => string
	},
)

Source from the content-addressed store, hash-verified

37 * @returns Array of OpenAI messages where consecutive messages with the same role are combined
38 */
39export function convertToR1Format(
40 messages: AnthropicMessage[],
41 options?: {
42 mergeToolResultText?: boolean
43 normalizeToolCallId?: (id: string) => string
44 },
45): Message[] {
46 const result: Message[] = []
47
48 for (const message of messages) {
49 // Check if the message has reasoning_content (for DeepSeek interleaved thinking)
50 const messageWithReasoning = message as AnthropicMessage & { reasoning_content?: string }
51 const reasoningContent = messageWithReasoning.reasoning_content
52
53 if (message.role === "user") {
54 // Handle user messages - may contain tool_result blocks
55 if (Array.isArray(message.content)) {
56 const textParts: string[] = []
57 const imageParts: ContentPartImage[] = []
58 const toolResults: { tool_use_id: string; content: string }[] = []
59
60 for (const part of message.content) {
61 if (part.type === "text") {
62 textParts.push(part.text)
63 } else if (part.type === "image") {
64 if (part.source.type === "base64") {
65 imageParts.push({
66 type: "image_url",
67 image_url: { url: `data:${part.source.media_type};base64,${part.source.data}` },
68 })
69 }
70 } else if (part.type === "tool_result") {
71 // Convert tool_result to OpenAI tool message format
72 let content: string
73 if (typeof part.content === "string") {
74 content = part.content
75 } else if (Array.isArray(part.content)) {
76 content =
77 part.content
78 ?.map((c) => {
79 if (c.type === "text") return c.text
80 if (c.type === "image") return "(image)"
81 return ""
82 })
83 .join("\n") ?? ""
84 } else {
85 content = ""
86 }
87 toolResults.push({
88 tool_use_id: part.tool_use_id,
89 content,
90 })
91 }
92 }
93
94 // Add tool messages first (they must follow assistant tool_use)
95 for (const toolResult of toolResults) {
96 const toolMessage: ToolMessage = {

Callers 7

createMessageMethod · 0.90
createMessageMethod · 0.90
createMessageMethod · 0.90
createMessageMethod · 0.90
createMessageMethod · 0.90
convertFunction · 0.90
r1-format.spec.tsFile · 0.90

Calls

no outgoing calls

Tested by 1

convertFunction · 0.72