(rawMessages: unknown[])
| 914 | } |
| 915 | |
| 916 | export function serializeTaskChat(rawMessages: unknown[]): string { |
| 917 | const filtered: { role: string; content: string }[] = [] |
| 918 | |
| 919 | for (const msg of rawMessages) { |
| 920 | if (!msg || typeof msg !== 'object') continue |
| 921 | const m = msg as Record<string, unknown> |
| 922 | const role = m.role as string | undefined |
| 923 | if (role !== 'user' && role !== 'assistant') continue |
| 924 | |
| 925 | let content = '' |
| 926 | if (role === 'assistant' && Array.isArray(m.contentBlocks)) { |
| 927 | const textParts: string[] = [] |
| 928 | for (const block of m.contentBlocks) { |
| 929 | if ( |
| 930 | block && |
| 931 | typeof block === 'object' && |
| 932 | (block as any).type === 'text' && |
| 933 | (block as any).content |
| 934 | ) { |
| 935 | textParts.push((block as any).content) |
| 936 | } |
| 937 | } |
| 938 | content = textParts.join('') |
| 939 | } |
| 940 | |
| 941 | if (!content && typeof m.content === 'string') { |
| 942 | content = m.content |
| 943 | } |
| 944 | |
| 945 | if (!content) continue |
| 946 | filtered.push({ role, content }) |
| 947 | } |
| 948 | |
| 949 | return JSON.stringify(filtered, null, 2) |
| 950 | } |
no test coverage detected