( messages: ApiMessage[], )
| 4464 | } |
| 4465 | |
| 4466 | private buildCleanConversationHistory( |
| 4467 | messages: ApiMessage[], |
| 4468 | ): Array< |
| 4469 | Anthropic.Messages.MessageParam | { type: "reasoning"; encrypted_content: string; id?: string; summary?: any[] } |
| 4470 | > { |
| 4471 | type ReasoningItemForRequest = { |
| 4472 | type: "reasoning" |
| 4473 | encrypted_content: string |
| 4474 | id?: string |
| 4475 | summary?: any[] |
| 4476 | } |
| 4477 | |
| 4478 | const cleanConversationHistory: (Anthropic.Messages.MessageParam | ReasoningItemForRequest)[] = [] |
| 4479 | |
| 4480 | for (const msg of messages) { |
| 4481 | // Standalone reasoning: send encrypted, skip plain text |
| 4482 | if (msg.type === "reasoning") { |
| 4483 | if (msg.encrypted_content) { |
| 4484 | cleanConversationHistory.push({ |
| 4485 | type: "reasoning", |
| 4486 | summary: msg.summary, |
| 4487 | encrypted_content: msg.encrypted_content!, |
| 4488 | ...(msg.id ? { id: msg.id } : {}), |
| 4489 | }) |
| 4490 | } |
| 4491 | continue |
| 4492 | } |
| 4493 | |
| 4494 | // Preferred path: assistant message with embedded reasoning as first content block |
| 4495 | if (msg.role === "assistant") { |
| 4496 | const rawContent = msg.content |
| 4497 | |
| 4498 | const contentArray: Anthropic.Messages.ContentBlockParam[] = Array.isArray(rawContent) |
| 4499 | ? (rawContent as Anthropic.Messages.ContentBlockParam[]) |
| 4500 | : rawContent !== undefined |
| 4501 | ? ([ |
| 4502 | { type: "text", text: rawContent } satisfies Anthropic.Messages.TextBlockParam, |
| 4503 | ] as Anthropic.Messages.ContentBlockParam[]) |
| 4504 | : [] |
| 4505 | |
| 4506 | const [first, ...rest] = contentArray |
| 4507 | |
| 4508 | // Check if this message has reasoning_details (OpenRouter format for Gemini 3, etc.) |
| 4509 | const msgWithDetails = msg |
| 4510 | if (msgWithDetails.reasoning_details && Array.isArray(msgWithDetails.reasoning_details)) { |
| 4511 | // Build the assistant message with reasoning_details |
| 4512 | let assistantContent: Anthropic.Messages.MessageParam["content"] |
| 4513 | |
| 4514 | if (contentArray.length === 0) { |
| 4515 | assistantContent = "" |
| 4516 | } else if (contentArray.length === 1 && contentArray[0].type === "text") { |
| 4517 | assistantContent = (contentArray[0] as Anthropic.Messages.TextBlockParam).text |
| 4518 | } else { |
| 4519 | assistantContent = contentArray |
| 4520 | } |
| 4521 | |
| 4522 | // Create message with reasoning_details property |
| 4523 | cleanConversationHistory.push({ |
no test coverage detected