| 1 | import { |
| 2 | LLMInteractionCancel, |
| 3 | LLMInteractionChunk, |
| 4 | LLMInteractionError, |
| 5 | LLMInteractionItem, |
| 6 | LLMInteractionMessage, |
| 7 | LLMInteractionStartChat, |
| 8 | LLMInteractionStartComplete, |
| 9 | LLMInteractionStartFim, |
| 10 | LLMInteractionSuccess, |
| 11 | } from "core"; |
| 12 | import { useEffect, useReducer } from "react"; |
| 13 | import { v4 as uuidv4 } from "uuid"; |
| 14 | |
| 15 | declare const vscode: any; |
| 16 | |
| 17 | export type LLMResult = LLMInteractionMessage | LLMInteractionChunk; |
| 18 | |
| 19 | const MAX_GROUP_LENGTH = 32; |
| 20 | |
| 21 | interface ToConsoleViewInit { |
| 22 | type: "init"; |
| 23 | uuid: string; |
| 24 | items: LLMInteractionItem[]; |
| 25 | } |
| 26 | |
| 27 | interface ToConsoleViewItem { |
| 28 | type: "item"; |
| 29 | uuid: string; |
| 30 | item: LLMInteractionItem; |
| 31 | } |
| 32 | |
| 33 | interface ToConsoleViewRemove { |
| 34 | type: "remove"; |
| 35 | uuid: string; |
| 36 | interactionId: string; |
| 37 | } |
| 38 | |
| 39 | interface ToConsoleViewClear { |
| 40 | type: "clear"; |
| 41 | uuid: string; |
| 42 | } |
| 43 | |
| 44 | type ToConsoleView = |
| 45 | | ToConsoleViewInit |
| 46 | | ToConsoleViewItem |
| 47 | | ToConsoleViewRemove |
| 48 | | ToConsoleViewClear; |
| 49 | |
| 50 | /** |
| 51 | * Represents a linear list of LLMInteractionItem, transformed into |
| 52 | * a form that is more convenient for rendering. |
| 53 | */ |
| 54 | export interface LLMInteraction { |
| 55 | start?: |
| 56 | | LLMInteractionStartChat |
| 57 | | LLMInteractionStartComplete |
| 58 | | LLMInteractionStartFim; |
| 59 | |
| 60 | // We use an array-of-arrays for efficiency when rendering a streamed result |
| 61 | // with lots and lots of separate tokens; instead of having a linear list |
| 62 | // of 1024 Result components, we have 32 ResultGroup components each rendering |
| 63 | // 32 Results. |
| 64 | // |
| 65 | // Inline content can split between one group and the next - we'll let the |
| 66 | // browser engine sort that out. |
| 67 | results: LLMResult[][]; |
| 68 | |
| 69 | end?: LLMInteractionSuccess | LLMInteractionError | LLMInteractionCancel; |
| 70 | } |
| 71 | |
| 72 | export interface LLMLog { |
| 73 | loading: boolean; |
| 74 | interactions: Map<string, LLMInteraction>; |
| 75 | order: string[]; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Appends a new result item (chunk or message) to an interaction, |
| 80 | * existing iteractions must already have been copied with |
| 81 | * copyInteractionForMutation(). |
| 82 | */ |
| 83 | function appendItemToInteractionResult( |
| 84 | interaction: LLMInteraction, |
| 85 | item: LLMInteractionMessage | LLMInteractionChunk, |
| 86 | ) { |
| 87 | let lastGroup = interaction.results[interaction.results.length - 1]; |
| 88 | |
| 89 | if (lastGroup == undefined || lastGroup.length == MAX_GROUP_LENGTH) { |
| 90 | interaction.results.push([item]); |
| 91 | } else { |
| 92 | lastGroup.push(item); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Makes a copy of an interaction that can be mutated without changing |
| 98 | * any objects/arrays that are part of the old state. We always make |
| 99 | * a copy of the last (open) result group, so that we can simply append |
| 100 | * to it; this is very slightly inefficient for the case where we're |
| 101 | * handling a single end item with no results, but makes things simpler. |
| 102 | */ |
| 103 | function copyInteractionForMutation(oldInteraction: LLMInteraction) { |
| 104 | const oldResults = oldInteraction.results; |
| 105 | let oldLastGroup = oldInteraction.results[oldInteraction.results.length - 1]; |
| 106 | let newResults; |
| 107 | |
| 108 | if (oldLastGroup == undefined || oldLastGroup.length == MAX_GROUP_LENGTH) { |
| 109 | newResults = [...oldInteraction.results]; |
| 110 | } else { |
| 111 | newResults = oldInteraction.results.slice(0, -1); |
| 112 | newResults.push([...oldLastGroup]); |
| 113 | } |
| 114 | |
| 115 | return { ...oldInteraction, results: newResults }; |
| 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 { |
| 176 | loading: false, |
| 177 | interactions: newInteractions, |
| 178 | order, |
| 179 | }; |
| 180 | } |
| 181 | |
| 182 | function removeInteractionFromLLMLog( |
| 183 | llmLog: LLMLog, |
| 184 | interactionId: string, |
| 185 | ): LLMLog { |
| 186 | const newInteractions = new Map(llmLog.interactions); |
| 187 | newInteractions.delete(interactionId); |
| 188 | const newOrder = llmLog.order.filter((id) => id !== interactionId); |
| 189 | |
| 190 | return { |
| 191 | loading: false, |
| 192 | interactions: newInteractions, |
| 193 | order: newOrder, |
| 194 | }; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Hook to accumulate log data structures based on messages passed |
| 199 | * from the core. Note that each call site will create an independent |
| 200 | * data structure, so this should be only used once at a toplevel |
| 201 | * component. |
| 202 | * @returns currently log datastructure. |
| 203 | */ |
| 204 | export default function useLLMLog() { |
| 205 | const [llmLog, dispatchLlmLog] = useReducer( |
| 206 | (llmLog: LLMLog, message: ToConsoleView) => { |
| 207 | switch (message.type) { |
| 208 | case "init": |
| 209 | return appendItemsToLLMLog(llmLog, message.items); |
| 210 | case "item": |
| 211 | return appendItemsToLLMLog(llmLog, [message.item]); |
| 212 | case "remove": |
| 213 | return removeInteractionFromLLMLog(llmLog, message.interactionId); |
| 214 | case "clear": |
| 215 | return { |
| 216 | loading: false, |
| 217 | interactions: new Map(), |
| 218 | order: [], |
| 219 | }; |
| 220 | } |
| 221 | }, |
| 222 | { |
| 223 | loading: true, |
| 224 | interactions: new Map(), |
| 225 | order: [], |
| 226 | }, |
| 227 | ); |
| 228 | |
| 229 | useEffect(function () { |
| 230 | // The uuid here marks the "generation" when the webview is |
| 231 | // reloaded, so we don't get confused if there are inflight |
| 232 | // messages from the previous generation. In particular, this |
| 233 | // avoids problems when React.StrictMode runs this effect |
| 234 | // twice - we don't want to process two "init" messages. |
| 235 | const uuid = uuidv4(); |
| 236 | const onMessage = (event: MessageEvent<ToConsoleView>) => { |
| 237 | if (event.data.uuid !== uuid) { |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | dispatchLlmLog(event.data); |
| 242 | }; |
| 243 | window.addEventListener("message", onMessage); |
| 244 | vscode.postMessage({ type: "start", uuid }); |
| 245 | |
| 246 | return () => { |
| 247 | vscode.postMessage({ type: "stop", uuid }); |
| 248 | window.removeEventListener("message", onMessage); |
| 249 | }; |
| 250 | }, []); |
| 251 | |
| 252 | return llmLog; |
| 253 | } |