| 159 | } |
| 160 | |
| 161 | const toModelOutput = (options: { toolCallId: string; input: unknown; output: unknown }) => { |
| 162 | const output = options.output |
| 163 | if (typeof output === "string") { |
| 164 | return { type: "text", value: output } |
| 165 | } |
| 166 | |
| 167 | if (typeof output === "object") { |
| 168 | const outputObject = output as { |
| 169 | text: string |
| 170 | attachments?: Array<{ mime: string; url: string }> |
| 171 | } |
| 172 | const attachments = (outputObject.attachments ?? []).filter((attachment) => { |
| 173 | return attachment.url.startsWith("data:") && attachment.url.includes(",") |
| 174 | }) |
| 175 | |
| 176 | return { |
| 177 | type: "content", |
| 178 | value: [ |
| 179 | ...(outputObject.text ? [{ type: "text", text: outputObject.text }] : []), |
| 180 | ...attachments.map((attachment) => ({ |
| 181 | type: "media", |
| 182 | mediaType: attachment.mime, |
| 183 | data: iife(() => { |
| 184 | const commaIndex = attachment.url.indexOf(",") |
| 185 | return commaIndex === -1 ? attachment.url : attachment.url.slice(commaIndex + 1) |
| 186 | }), |
| 187 | })), |
| 188 | ], |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return { type: "json", value: output as never } |
| 193 | } |
| 194 | |
| 195 | for (const msg of input) { |
| 196 | if (msg.parts.length === 0) continue |