(messages: any[])
| 41 | * 3. Legacy format without partsOrder (fallback) |
| 42 | */ |
| 43 | export function convertToMessageV2(messages: any[]): MessageV2[] { |
| 44 | return messages.map((m) => { |
| 45 | // If message already has properly typed parts (new format), use them directly |
| 46 | if (m.parts && Array.isArray(m.parts) && m.parts.length > 0 && m.parts[0]?.type) { |
| 47 | return { |
| 48 | id: m.id, |
| 49 | threadId: m.threadId, |
| 50 | role: m.role, |
| 51 | parts: m.parts, |
| 52 | createdAt: m.createdAt, |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | // If partsOrder is available, use it to reconstruct parts in the correct order |
| 57 | if (m.partsOrder && Array.isArray(m.partsOrder) && m.partsOrder.length > 0) { |
| 58 | const parts: any[] = []; |
| 59 | const reasoningMap = new Map<string, any>(); |
| 60 | const toolCallMap = new Map<string, any>(); |
| 61 | |
| 62 | if (m.reasoning) { |
| 63 | for (const r of m.reasoning) { |
| 64 | reasoningMap.set(r.id || `reasoning-${r.timestamp}`, r); |
| 65 | } |
| 66 | } |
| 67 | if (m.toolCalls) { |
| 68 | for (const tc of m.toolCalls) { |
| 69 | toolCallMap.set(tc.id, tc); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | for (const entry of m.partsOrder) { |
| 74 | switch (entry.type) { |
| 75 | case "text": |
| 76 | parts.push({ |
| 77 | id: entry.id, |
| 78 | type: "text", |
| 79 | text: entry.text || m.content, |
| 80 | status: "completed", |
| 81 | createdAt: m.createdAt, |
| 82 | }); |
| 83 | break; |
| 84 | case "quote": |
| 85 | parts.push({ |
| 86 | id: entry.id, |
| 87 | type: "quote", |
| 88 | text: entry.text || "", |
| 89 | source: entry.source, |
| 90 | status: "completed", |
| 91 | createdAt: m.createdAt, |
| 92 | }); |
| 93 | break; |
| 94 | case "reasoning": { |
| 95 | const r = reasoningMap.get(entry.id); |
| 96 | if (r) { |
| 97 | parts.push({ |
| 98 | id: entry.id, |
| 99 | type: "reasoning", |
| 100 | text: r.content, |
no test coverage detected