| 1 | import { fetchwithRequestOptions } from "@continuedev/fetch"; |
| 2 | import { ChatMessage, IDE, PromptLog } from ".."; |
| 3 | import { ConfigHandler } from "../config/ConfigHandler"; |
| 4 | import { usesFreeTrialApiKey } from "../config/usesFreeTrialApiKey"; |
| 5 | import { FromCoreProtocol, ToCoreProtocol } from "../protocol"; |
| 6 | import { IMessenger, Message } from "../protocol/messenger"; |
| 7 | import { Telemetry } from "../util/posthog"; |
| 8 | import { TTS } from "../util/tts"; |
| 9 | |
| 10 | export async function* llmStreamChat( |
| 11 | configHandler: ConfigHandler, |
| 12 | abortController: AbortController, |
| 13 | msg: Message<ToCoreProtocol["llm/streamChat"][0]>, |
| 14 | ide: IDE, |
| 15 | messenger: IMessenger<ToCoreProtocol, FromCoreProtocol>, |
| 16 | ): AsyncGenerator<ChatMessage, PromptLog> { |
| 17 | const { config } = await configHandler.loadConfig(); |
| 18 | if (!config) { |
| 19 | throw new Error("Config not loaded"); |
| 20 | } |
| 21 | |
| 22 | // Stop TTS on new StreamChat |
| 23 | if (config.experimental?.readResponseTTS) { |
| 24 | void TTS.kill(); |
| 25 | } |
| 26 | |
| 27 | const { |
| 28 | legacySlashCommandData, |
| 29 | completionOptions, |
| 30 | messages, |
| 31 | messageOptions, |
| 32 | } = msg.data; |
| 33 | |
| 34 | const model = config.selectedModelByRole.chat; |
| 35 | |
| 36 | if (!model) { |
| 37 | throw new Error("No chat model selected"); |
| 38 | } |
| 39 | |
| 40 | // Log to return in case of error |
| 41 | const errorPromptLog = { |
| 42 | modelTitle: model?.title ?? model?.model, |
| 43 | completion: "", |
| 44 | prompt: "", |
| 45 | completionOptions: { |
| 46 | ...msg.data.completionOptions, |
| 47 | model: model?.model, |
| 48 | }, |
| 49 | }; |
| 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`, |
| 70 | ); |
| 71 | throw new Error(`Slash command not found`); |
| 72 | } |
| 73 | |
| 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; |
| 106 | } |
| 107 | if (next.value) { |
| 108 | yield { |
| 109 | role: "assistant", |
| 110 | content: next.value, |
| 111 | }; |
| 112 | } |
| 113 | next = await gen.next(); |
| 114 | } |
| 115 | if (!next.done) { |
| 116 | throw new Error("Will never happen"); |
| 117 | } |
| 118 | |
| 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; |
| 132 | } |
| 133 | |
| 134 | const chunk = next.value; |
| 135 | |
| 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 | } |
| 142 | |
| 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); |
| 153 | |
| 154 | if (!next.done) { |
| 155 | throw new Error("Will never happen"); |
| 156 | } |
| 157 | |
| 158 | return next.value; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | async function checkForFreeTrialExceeded( |
| 163 | configHandler: ConfigHandler, |
| 164 | messenger: IMessenger<ToCoreProtocol, FromCoreProtocol>, |
| 165 | ) { |
| 166 | const { config } = await configHandler.getSerializedConfig(); |
| 167 | |
| 168 | // Only check if the user is using the free trial |
| 169 | if (config && !usesFreeTrialApiKey(config)) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | try { |
| 174 | const freeTrialStatus = |
| 175 | await configHandler.controlPlaneClient.getFreeTrialStatus(); |
| 176 | if ( |
| 177 | freeTrialStatus && |
| 178 | freeTrialStatus.chatCount && |
| 179 | freeTrialStatus.chatCount > freeTrialStatus.chatLimit |
| 180 | ) { |
| 181 | void messenger.request("freeTrialExceeded", undefined); |
| 182 | } |
| 183 | } catch (error) { |
| 184 | console.error("Error checking free trial status:", error); |
| 185 | } |
| 186 | } |