(messages: Message[])
| 300 | * and emitted as user turns. |
| 301 | */ |
| 302 | export function buildTranscriptEntries(messages: Message[]): TranscriptEntry[] { |
| 303 | const transcript: TranscriptEntry[] = [] |
| 304 | for (const msg of messages) { |
| 305 | if (msg.type === 'attachment' && msg.attachment.type === 'queued_command') { |
| 306 | const prompt = msg.attachment.prompt |
| 307 | let text: string | null = null |
| 308 | if (typeof prompt === 'string') { |
| 309 | text = prompt |
| 310 | } else if (Array.isArray(prompt)) { |
| 311 | text = |
| 312 | prompt |
| 313 | .filter( |
| 314 | (block): block is { type: 'text'; text: string } => |
| 315 | block.type === 'text', |
| 316 | ) |
| 317 | .map(block => block.text) |
| 318 | .join('\n') || null |
| 319 | } |
| 320 | if (text !== null) { |
| 321 | transcript.push({ |
| 322 | role: 'user', |
| 323 | content: [{ type: 'text', text }], |
| 324 | }) |
| 325 | } |
| 326 | } else if (msg.type === 'user') { |
| 327 | const content = msg.message.content |
| 328 | const textBlocks: TranscriptBlock[] = [] |
| 329 | if (typeof content === 'string') { |
| 330 | textBlocks.push({ type: 'text', text: content }) |
| 331 | } else if (Array.isArray(content)) { |
| 332 | for (const block of content) { |
| 333 | if (block.type === 'text') { |
| 334 | textBlocks.push({ type: 'text', text: block.text }) |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | if (textBlocks.length > 0) { |
| 339 | transcript.push({ role: 'user', content: textBlocks }) |
| 340 | } |
| 341 | } else if (msg.type === 'assistant') { |
| 342 | const blocks: TranscriptBlock[] = [] |
| 343 | for (const block of msg.message.content) { |
| 344 | // Only include tool_use blocks — assistant text is model-authored |
| 345 | // and could be crafted to influence the classifier's decision. |
| 346 | if (block.type === 'tool_use') { |
| 347 | blocks.push({ |
| 348 | type: 'tool_use', |
| 349 | name: block.name, |
| 350 | input: block.input, |
| 351 | }) |
| 352 | } |
| 353 | } |
| 354 | if (blocks.length > 0) { |
| 355 | transcript.push({ role: 'assistant', content: blocks }) |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | return transcript |
no test coverage detected