( oldLog: LLMLog, items: LLMInteractionItem[], )
| 116 | } |
| 117 | |
| 118 | function appendItemsToLLMLog( |
| 119 | oldLog: LLMLog, |
| 120 | items: LLMInteractionItem[], |
| 121 | ): LLMLog { |
| 122 | const oldInteractions = oldLog.interactions; |
| 123 | const newInteractions: Map<string, LLMInteraction> = new Map(); |
| 124 | let order = oldLog.order; |
| 125 | let interactionsAdded = false; |
| 126 | |
| 127 | // Add the new items to the log, making mutable copies of old |
| 128 | // LLMInteraction as necessary |
| 129 | for (const item of items) { |
| 130 | let interaction = newInteractions.get(item.interactionId); |
| 131 | if (interaction === undefined) { |
| 132 | const oldInteraction = oldInteractions.get(item.interactionId); |
| 133 | if (oldInteraction) { |
| 134 | interaction = copyInteractionForMutation(oldInteraction); |
| 135 | } else { |
| 136 | interaction = { |
| 137 | results: [], |
| 138 | }; |
| 139 | |
| 140 | if (interactionsAdded) { |
| 141 | order.push(item.interactionId); |
| 142 | } else { |
| 143 | order = [...order, item.interactionId]; |
| 144 | interactionsAdded = true; |
| 145 | } |
| 146 | } |
| 147 | newInteractions.set(item.interactionId, interaction); |
| 148 | } |
| 149 | |
| 150 | switch (item.kind) { |
| 151 | case "startChat": |
| 152 | case "startComplete": |
| 153 | case "startFim": |
| 154 | interaction.start = item; |
| 155 | break; |
| 156 | case "chunk": |
| 157 | case "message": |
| 158 | appendItemToInteractionResult(interaction, item); |
| 159 | break; |
| 160 | case "success": |
| 161 | case "error": |
| 162 | case "cancel": |
| 163 | interaction.end = item; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // Copy over unchanged interactions |
| 169 | for (const interactionId of oldInteractions.keys()) { |
| 170 | if (!newInteractions.has(interactionId)) { |
| 171 | newInteractions.set(interactionId, oldInteractions.get(interactionId)!); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return { |
no test coverage detected