Changesets · Branches · Tags · Compare
| Changeset | ca8db4c0a2a870e3d1b3df8b41788ba44a48dffc8b9b9f21a175b48273d3d9ff |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-08-14 12:50 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-08-14 12:50 -07:00 |
| Parents | 701ab6fa2274, 5c77af9155f7 |
1 file changed
modified core/llm/streamChat.ts +119 −97
| @@ -48,114 +48,136 @@ | ||
| 48 | 48 | }, |
| 49 | 49 | }; |
| 50 | 50 | |
| 51 | - if (legacySlashCommandData) { | |
| 52 | - const { command, contextItems, historyIndex, input, selectedCode } = | |
| 53 | - legacySlashCommandData; | |
| 54 | - const slashCommand = config.slashCommands?.find( | |
| 55 | - (sc) => sc.name === command.name, | |
| 56 | - ); | |
| 57 | - if (!slashCommand) { | |
| 58 | - throw new Error(`Unknown slash command ${command.name}`); | |
| 59 | - } | |
| 60 | - void Telemetry.capture( | |
| 61 | - "useSlashCommand", | |
| 62 | - { | |
| 63 | - name: command.name, | |
| 64 | - }, | |
| 65 | - true, | |
| 66 | - ); | |
| 67 | - if (!slashCommand.run) { | |
| 68 | - console.error( | |
| 69 | - `Slash command ${command.name} (${command.source}) has no run function`, | |
| 51 | + try { | |
| 52 | + if (legacySlashCommandData) { | |
| 53 | + const { command, contextItems, historyIndex, input, selectedCode } = | |
| 54 | + legacySlashCommandData; | |
| 55 | + const slashCommand = config.slashCommands?.find( | |
| 56 | + (sc) => sc.name === command.name, | |
| 70 | 57 | ); |
| 71 | - throw new Error(`Slash command not found`); | |
| 72 | - } | |
| 58 | + if (!slashCommand) { | |
| 59 | + throw new Error(`Unknown slash command ${command.name}`); | |
| 60 | + } | |
| 61 | + void Telemetry.capture( | |
| 62 | + "useSlashCommand", | |
| 63 | + { | |
| 64 | + name: command.name, | |
| 65 | + }, | |
| 66 | + true, | |
| 67 | + ); | |
| 68 | + if (!slashCommand.run) { | |
| 69 | + console.error( | |
| 70 | + `Slash command ${command.name} (${command.source}) has no run function`, | |
| 71 | + ); | |
| 72 | + throw new Error(`Slash command not found`); | |
| 73 | + } | |
| 73 | 74 | |
| 74 | - const gen = slashCommand.run({ | |
| 75 | - input, | |
| 76 | - history: messages, | |
| 77 | - llm: model, | |
| 78 | - contextItems, | |
| 79 | - params: command.params, | |
| 80 | - ide, | |
| 81 | - addContextItem: (item) => { | |
| 82 | - void messenger.request("addContextItem", { | |
| 83 | - item, | |
| 84 | - historyIndex, | |
| 85 | - }); | |
| 86 | - }, | |
| 87 | - selectedCode, | |
| 88 | - config, | |
| 89 | - fetch: (url, init) => | |
| 90 | - fetchwithRequestOptions( | |
| 91 | - url, | |
| 92 | - { | |
| 93 | - ...init, | |
| 94 | - signal: abortController.signal, | |
| 95 | - }, | |
| 96 | - config.requestOptions, | |
| 97 | - ), | |
| 98 | - completionOptions, | |
| 99 | - abortController, | |
| 100 | - }); | |
| 101 | - let next = await gen.next(); | |
| 102 | - while (!next.done) { | |
| 103 | - if (abortController.signal.aborted) { | |
| 104 | - next = await gen.return(errorPromptLog); | |
| 105 | - break; | |
| 75 | + const gen = slashCommand.run({ | |
| 76 | + input, | |
| 77 | + history: messages, | |
| 78 | + llm: model, | |
| 79 | + contextItems, | |
| 80 | + params: command.params, | |
| 81 | + ide, | |
| 82 | + addContextItem: (item) => { | |
| 83 | + void messenger.request("addContextItem", { | |
| 84 | + item, | |
| 85 | + historyIndex, | |
| 86 | + }); | |
| 87 | + }, | |
| 88 | + selectedCode, | |
| 89 | + config, | |
| 90 | + fetch: (url, init) => | |
| 91 | + fetchwithRequestOptions( | |
| 92 | + url, | |
| 93 | + { | |
| 94 | + ...init, | |
| 95 | + signal: abortController.signal, | |
| 96 | + }, | |
| 97 | + config.requestOptions, | |
| 98 | + ), | |
| 99 | + completionOptions, | |
| 100 | + abortController, | |
| 101 | + }); | |
| 102 | + let next = await gen.next(); | |
| 103 | + while (!next.done) { | |
| 104 | + if (abortController.signal.aborted) { | |
| 105 | + next = await gen.return(errorPromptLog); | |
| 106 | + break; | |
| 107 | + } | |
| 108 | + if (next.value) { | |
| 109 | + yield { | |
| 110 | + role: "assistant", | |
| 111 | + content: next.value, | |
| 112 | + }; | |
| 113 | + } | |
| 114 | + next = await gen.next(); | |
| 106 | 115 | } |
| 107 | - if (next.value) { | |
| 108 | - yield { | |
| 109 | - role: "assistant", | |
| 110 | - content: next.value, | |
| 111 | - }; | |
| 116 | + if (!next.done) { | |
| 117 | + throw new Error("Will never happen"); | |
| 112 | 118 | } |
| 113 | - next = await gen.next(); | |
| 114 | - } | |
| 115 | - if (!next.done) { | |
| 116 | - throw new Error("Will never happen"); | |
| 117 | - } | |
| 118 | 119 | |
| 119 | - return next.value; | |
| 120 | - } else { | |
| 121 | - const gen = model.streamChat( | |
| 122 | - messages, | |
| 123 | - abortController.signal, | |
| 124 | - completionOptions, | |
| 125 | - messageOptions, | |
| 126 | - ); | |
| 127 | - let next = await gen.next(); | |
| 128 | - while (!next.done) { | |
| 129 | - if (abortController.signal.aborted) { | |
| 130 | - next = await gen.return(errorPromptLog); | |
| 131 | - break; | |
| 120 | + return next.value; | |
| 121 | + } else { | |
| 122 | + const gen = model.streamChat( | |
| 123 | + messages, | |
| 124 | + abortController.signal, | |
| 125 | + completionOptions, | |
| 126 | + messageOptions, | |
| 127 | + ); | |
| 128 | + let next = await gen.next(); | |
| 129 | + while (!next.done) { | |
| 130 | + if (abortController.signal.aborted) { | |
| 131 | + next = await gen.return(errorPromptLog); | |
| 132 | + break; | |
| 133 | + } | |
| 134 | + | |
| 135 | + const chunk = next.value; | |
| 136 | + | |
| 137 | + yield chunk; | |
| 138 | + next = await gen.next(); | |
| 139 | + } | |
| 140 | + if (config.experimental?.readResponseTTS && "completion" in next.value) { | |
| 141 | + void TTS.read(next.value?.completion); | |
| 132 | 142 | } |
| 133 | 143 | |
| 134 | - const chunk = next.value; | |
| 144 | + void Telemetry.capture( | |
| 145 | + "chat", | |
| 146 | + { | |
| 147 | + model: model.model, | |
| 148 | + provider: model.providerName, | |
| 149 | + }, | |
| 150 | + true, | |
| 151 | + ); | |
| 135 | 152 | |
| 136 | - yield chunk; | |
| 137 | - next = await gen.next(); | |
| 138 | - } | |
| 139 | - if (config.experimental?.readResponseTTS && "completion" in next.value) { | |
| 140 | - void TTS.read(next.value?.completion); | |
| 141 | - } | |
| 153 | + void checkForFreeTrialExceeded(configHandler, messenger); | |
| 142 | 154 | |
| 143 | - void Telemetry.capture( | |
| 144 | - "chat", | |
| 145 | - { | |
| 146 | - model: model.model, | |
| 147 | - provider: model.providerName, | |
| 148 | - }, | |
| 149 | - true, | |
| 150 | - ); | |
| 151 | - | |
| 152 | - void checkForFreeTrialExceeded(configHandler, messenger); | |
| 155 | + if (!next.done) { | |
| 156 | + throw new Error("Will never happen"); | |
| 157 | + } | |
| 153 | 158 | |
| 154 | - if (!next.done) { | |
| 155 | - throw new Error("Will never happen"); | |
| 159 | + return next.value; | |
| 156 | 160 | } |
| 157 | - | |
| 158 | - return next.value; | |
| 161 | + } catch (error) { | |
| 162 | + if ( | |
| 163 | + error instanceof Error && | |
| 164 | + error.message.toLowerCase().includes("premature close") | |
| 165 | + ) { | |
| 166 | + void Telemetry.capture( | |
| 167 | + "stream_premature_close_error", | |
| 168 | + { | |
| 169 | + model: model.model, | |
| 170 | + provider: model.providerName, | |
| 171 | + errorMessage: error.message, | |
| 172 | + context: legacySlashCommandData ? "slash_command" : "regular_chat", | |
| 173 | + ...(legacySlashCommandData && { | |
| 174 | + command: legacySlashCommandData.command.name, | |
| 175 | + }), | |
| 176 | + }, | |
| 177 | + false, | |
| 178 | + ); | |
| 179 | + } | |
| 180 | + throw error; | |
| 159 | 181 | } |
| 160 | 182 | } |
| 161 | 183 |