| 1 | import { fetchwithRequestOptions } from "@continuedev/fetch"; |
| 2 | import { ChatMessage, IDE, PromptLog } from ".."; |
| 3 | import { ConfigHandler } from "../config/ConfigHandler"; |
| 4 | import { FromCoreProtocol, ToCoreProtocol } from "../protocol"; |
| 5 | import { IMessenger, Message } from "../protocol/messenger"; |
| 6 | import { Telemetry } from "../util/posthog"; |
| 7 | import { TTS } from "../util/tts"; |
| 8 | |
| 9 | export async function* llmStreamChat( |
| 10 | configHandler: ConfigHandler, |
| 11 | abortedMessageIds: Set<string>, |
| 12 | msg: Message<ToCoreProtocol["llm/streamChat"][0]>, |
| 13 | ide: IDE, |
| 14 | messenger: IMessenger<ToCoreProtocol, FromCoreProtocol>, |
| 15 | ): AsyncGenerator<ChatMessage, PromptLog> { |
| 16 | const { config } = await configHandler.loadConfig(); |
| 17 | if (!config) { |
| 18 | throw new Error("Config not loaded"); |
| 19 | } |
| 20 | |
| 21 | // Stop TTS on new StreamChat |
| 22 | if (config.experimental?.readResponseTTS) { |
| 23 | void TTS.kill(); |
| 24 | } |
| 25 | |
| 26 | const { legacySlashCommandData, completionOptions, messages } = msg.data; |
| 27 | |
| 28 | const model = config.selectedModelByRole.chat; |
| 29 | |
| 30 | if (!model) { |
| 31 | throw new Error("No chat model selected"); |
| 32 | } |
| 33 | |
| 34 | // Log to return in case of error |
| 35 | const errorPromptLog = { |
| 36 | modelTitle: model?.title ?? model?.model, |
| 37 | completion: "", |
| 38 | prompt: "", |
| 39 | completionOptions: { |
| 40 | ...msg.data.completionOptions, |
| 41 | model: model?.model, |
| 42 | }, |
| 43 | }; |
| 44 | |
| 45 | if (legacySlashCommandData) { |
| 46 | const { command, contextItems, historyIndex, input, selectedCode } = |
| 47 | legacySlashCommandData; |
| 48 | const slashCommand = config.slashCommands?.find( |
| 49 | (sc) => sc.name === command.name, |
| 50 | ); |
| 51 | if (!slashCommand) { |
| 52 | throw new Error(`Unknown slash command ${command.name}`); |
| 53 | } |
| 54 | void Telemetry.capture( |
| 55 | "useSlashCommand", |
| 56 | { |
| 57 | name: command.name, |
| 58 | }, |
| 59 | true, |
| 60 | ); |
| 61 | |
| 62 | const gen = slashCommand.run({ |
| 63 | input, |
| 64 | history: messages, |
| 65 | llm: model, |
| 66 | contextItems, |
| 67 | params: command.params, |
| 68 | ide, |
| 69 | addContextItem: (item) => { |
| 70 | void messenger.request("addContextItem", { |
| 71 | item, |
| 72 | historyIndex, |
| 73 | }); |
| 74 | }, |
| 75 | selectedCode, |
| 76 | config, |
| 77 | fetch: (url, init) => |
| 78 | fetchwithRequestOptions(url, init, config.requestOptions), |
| 79 | completionOptions, |
| 80 | }); |
| 81 | const checkActiveInterval = setInterval(() => { |
| 82 | if (abortedMessageIds.has(msg.messageId)) { |
| 83 | abortedMessageIds.delete(msg.messageId); |
| 84 | clearInterval(checkActiveInterval); |
| 85 | } |
| 86 | }, 100); |
| 87 | try { |
| 88 | let next = await gen.next(); |
| 89 | while (!next.done) { |
| 90 | if (abortedMessageIds.has(msg.messageId)) { |
| 91 | abortedMessageIds.delete(msg.messageId); |
| 92 | next = await gen.return(errorPromptLog); |
| 93 | clearInterval(checkActiveInterval); |
| 94 | break; |
| 95 | } |
| 96 | if (next.value) { |
| 97 | yield { |
| 98 | role: "assistant", |
| 99 | content: next.value, |
| 100 | }; |
| 101 | } |
| 102 | next = await gen.next(); |
| 103 | } |
| 104 | if (!next.done) { |
| 105 | throw new Error("Will never happen"); |
| 106 | } |
| 107 | |
| 108 | return next.value; |
| 109 | } catch (e) { |
| 110 | throw e; |
| 111 | } finally { |
| 112 | clearInterval(checkActiveInterval); |
| 113 | } |
| 114 | } else { |
| 115 | const gen = model.streamChat( |
| 116 | messages, |
| 117 | new AbortController().signal, |
| 118 | completionOptions, |
| 119 | ); |
| 120 | let next = await gen.next(); |
| 121 | while (!next.done) { |
| 122 | if (abortedMessageIds.has(msg.messageId)) { |
| 123 | abortedMessageIds.delete(msg.messageId); |
| 124 | next = await gen.return(errorPromptLog); |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | const chunk = next.value; |
| 129 | |
| 130 | yield chunk; |
| 131 | next = await gen.next(); |
| 132 | } |
| 133 | if (config.experimental?.readResponseTTS && "completion" in next.value) { |
| 134 | void TTS.read(next.value?.completion); |
| 135 | } |
| 136 | |
| 137 | void Telemetry.capture( |
| 138 | "chat", |
| 139 | { |
| 140 | model: model.model, |
| 141 | provider: model.providerName, |
| 142 | }, |
| 143 | true, |
| 144 | ); |
| 145 | |
| 146 | if (!next.done) { |
| 147 | throw new Error("Will never happen"); |
| 148 | } |
| 149 | |
| 150 | return next.value; |
| 151 | } |
| 152 | } |