| 47 | * This groups parts by their messageID to reconstruct the hierarchy before writing to disk. |
| 48 | */ |
| 49 | export function transformShareData(shareData: ShareData[]): { |
| 50 | info: SDKSession |
| 51 | messages: Array<{ info: Message; parts: Part[] }> |
| 52 | } | null { |
| 53 | const sessionItem = shareData.find((d) => d.type === "session") |
| 54 | if (!sessionItem) return null |
| 55 | |
| 56 | const messageMap = new Map<string, Message>() |
| 57 | const partMap = new Map<string, Part[]>() |
| 58 | |
| 59 | for (const item of shareData) { |
| 60 | if (item.type === "message") { |
| 61 | messageMap.set(item.data.id, item.data) |
| 62 | } else if (item.type === "part") { |
| 63 | if (!partMap.has(item.data.messageID)) { |
| 64 | partMap.set(item.data.messageID, []) |
| 65 | } |
| 66 | partMap.get(item.data.messageID)!.push(item.data) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (messageMap.size === 0) return null |
| 71 | |
| 72 | return { |
| 73 | info: sessionItem.data, |
| 74 | messages: Array.from(messageMap.values()).map((msg) => ({ |
| 75 | info: msg, |
| 76 | parts: partMap.get(msg.id) ?? [], |
| 77 | })), |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | type ExportData = { info: SDKSession; messages: Array<{ info: Message; parts: Part[] }> } |
| 82 | |