( msgs: ModelMessage[], model: Provider.Model, _options: Record<string, unknown>, )
| 63 | |
| 64 | // TODO: fix this stupid inefficient dogshit function |
| 65 | function normalizeMessages( |
| 66 | msgs: ModelMessage[], |
| 67 | model: Provider.Model, |
| 68 | _options: Record<string, unknown>, |
| 69 | ): ModelMessage[] { |
| 70 | const sanitizeToolResultOutput = (content: ToolResultPart) => { |
| 71 | if (content.output.type === "text" || content.output.type === "error-text") { |
| 72 | content.output.value = sanitizeSurrogates(content.output.value) |
| 73 | } |
| 74 | if (content.output.type === "content") { |
| 75 | content.output.value = content.output.value.map((item) => { |
| 76 | if (item.type === "text") { |
| 77 | item.text = sanitizeSurrogates(item.text) |
| 78 | } |
| 79 | return item |
| 80 | }) |
| 81 | } |
| 82 | return content |
| 83 | } |
| 84 | |
| 85 | msgs = msgs.map((msg) => { |
| 86 | switch (msg.role) { |
| 87 | case "tool": |
| 88 | if (!Array.isArray(msg.content)) return msg |
| 89 | msg.content = msg.content.map((content) => { |
| 90 | if (content.type === "tool-result") { |
| 91 | return sanitizeToolResultOutput(content) |
| 92 | } |
| 93 | return content |
| 94 | }) |
| 95 | return msg |
| 96 | |
| 97 | case "system": |
| 98 | msg.content = sanitizeSurrogates(msg.content) |
| 99 | return msg |
| 100 | |
| 101 | case "user": |
| 102 | if (typeof msg.content === "string") { |
| 103 | msg.content = sanitizeSurrogates(msg.content) |
| 104 | } else { |
| 105 | msg.content = msg.content.map((content) => { |
| 106 | if (content.type === "text") { |
| 107 | content.text = sanitizeSurrogates(content.text) |
| 108 | } |
| 109 | return content |
| 110 | }) |
| 111 | } |
| 112 | return msg |
| 113 | |
| 114 | case "assistant": |
| 115 | if (typeof msg.content === "string") { |
| 116 | msg.content = sanitizeSurrogates(msg.content) |
| 117 | } else { |
| 118 | msg.content = msg.content.map((content) => { |
| 119 | if (content.type === "text" || content.type === "reasoning") { |
| 120 | content.text = sanitizeSurrogates(content.text) |
| 121 | } |
| 122 | if (content.type === "tool-result") { |
no test coverage detected