|
| 1 | +import type { FormatDescriptor, ToolOutput } from "../types" |
| 2 | +import type { PluginState } from "../../state" |
| 3 | + |
| 4 | +/** |
| 5 | + * Anthropic Messages API format with top-level `system` array. |
| 6 | + * Tool calls: `tool_use` blocks in assistant content with `id` |
| 7 | + * Tool results: `tool_result` blocks in user content with `tool_use_id` |
| 8 | + */ |
| 9 | +export const anthropicFormat: FormatDescriptor = { |
| 10 | + name: 'anthropic', |
| 11 | + |
| 12 | + detect(body: any): boolean { |
| 13 | + // Anthropic has top-level `system` field (can be string or array) AND messages array |
| 14 | + // This distinguishes it from OpenAI (no top-level system) and Bedrock (has inferenceConfig) |
| 15 | + return ( |
| 16 | + body.system !== undefined && |
| 17 | + Array.isArray(body.messages) |
| 18 | + ) |
| 19 | + }, |
| 20 | + |
| 21 | + getDataArray(body: any): any[] | undefined { |
| 22 | + return body.messages |
| 23 | + }, |
| 24 | + |
| 25 | + injectSystemMessage(body: any, injection: string): boolean { |
| 26 | + if (!injection) return false |
| 27 | + |
| 28 | + // Anthropic system can be: |
| 29 | + // 1. A string: "You are a helpful assistant" |
| 30 | + // 2. An array of blocks: [{"type": "text", "text": "...", "cache_control": {...}}] |
| 31 | + |
| 32 | + // Convert to array if needed |
| 33 | + if (typeof body.system === 'string') { |
| 34 | + body.system = [{ type: 'text', text: body.system }] |
| 35 | + } else if (!Array.isArray(body.system)) { |
| 36 | + body.system = [] |
| 37 | + } |
| 38 | + |
| 39 | + // Append the injection as a text block |
| 40 | + body.system.push({ type: 'text', text: injection }) |
| 41 | + return true |
| 42 | + }, |
| 43 | + |
| 44 | + extractToolOutputs(data: any[], state: PluginState): ToolOutput[] { |
| 45 | + const outputs: ToolOutput[] = [] |
| 46 | + |
| 47 | + for (const m of data) { |
| 48 | + // Tool results are in user messages with type='tool_result' |
| 49 | + if (m.role === 'user' && Array.isArray(m.content)) { |
| 50 | + for (const block of m.content) { |
| 51 | + if (block.type === 'tool_result' && block.tool_use_id) { |
| 52 | + const toolUseId = block.tool_use_id.toLowerCase() |
| 53 | + const metadata = state.toolParameters.get(toolUseId) |
| 54 | + outputs.push({ |
| 55 | + id: toolUseId, |
| 56 | + toolName: metadata?.tool |
| 57 | + }) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return outputs |
| 64 | + }, |
| 65 | + |
| 66 | + replaceToolOutput(data: any[], toolId: string, prunedMessage: string, _state: PluginState): boolean { |
| 67 | + const toolIdLower = toolId.toLowerCase() |
| 68 | + let replaced = false |
| 69 | + |
| 70 | + for (let i = 0; i < data.length; i++) { |
| 71 | + const m = data[i] |
| 72 | + |
| 73 | + if (m.role === 'user' && Array.isArray(m.content)) { |
| 74 | + let messageModified = false |
| 75 | + const newContent = m.content.map((block: any) => { |
| 76 | + if (block.type === 'tool_result' && block.tool_use_id?.toLowerCase() === toolIdLower) { |
| 77 | + messageModified = true |
| 78 | + // Anthropic tool_result content can be string or array of content blocks |
| 79 | + // Replace with simple string |
| 80 | + return { |
| 81 | + ...block, |
| 82 | + content: prunedMessage |
| 83 | + } |
| 84 | + } |
| 85 | + return block |
| 86 | + }) |
| 87 | + if (messageModified) { |
| 88 | + data[i] = { ...m, content: newContent } |
| 89 | + replaced = true |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return replaced |
| 95 | + }, |
| 96 | + |
| 97 | + hasToolOutputs(data: any[]): boolean { |
| 98 | + for (const m of data) { |
| 99 | + if (m.role === 'user' && Array.isArray(m.content)) { |
| 100 | + for (const block of m.content) { |
| 101 | + if (block.type === 'tool_result') return true |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return false |
| 106 | + }, |
| 107 | + |
| 108 | + getLogMetadata(data: any[], replacedCount: number, inputUrl: string): Record<string, any> { |
| 109 | + return { |
| 110 | + url: inputUrl, |
| 111 | + replacedCount, |
| 112 | + totalMessages: data.length, |
| 113 | + format: 'anthropic' |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments