Elffuss BraidHub

kikocis/continue public

972ea8cfaef0 · 3.4 KiBRawPermalink
1import { fetchwithRequestOptions } from "@continuedev/fetch";
2import { ChatMessage, IDE, PromptLog } from "..";
3import { ConfigHandler } from "../config/ConfigHandler";
4import { FromCoreProtocol, ToCoreProtocol } from "../protocol";
5import { IMessenger, Message } from "../protocol/messenger";
6import { Telemetry } from "../util/posthog";
7import { TTS } from "../util/tts";
8
9export async function* llmStreamChat(
10 configHandler: ConfigHandler,
11 abortController: AbortController,
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(
79 url,
80 {
81 ...init,
82 signal: abortController.signal,
83 },
84 config.requestOptions,
85 ),
86 completionOptions,
87 });
88 let next = await gen.next();
89 while (!next.done) {
90 if (abortController.signal.aborted) {
91 next = await gen.return(errorPromptLog);
92 break;
93 }
94 if (next.value) {
95 yield {
96 role: "assistant",
97 content: next.value,
98 };
99 }
100 next = await gen.next();
101 }
102 if (!next.done) {
103 throw new Error("Will never happen");
104 }
105
106 return next.value;
107 } else {
108 const gen = model.streamChat(
109 messages,
110 abortController.signal,
111 completionOptions,
112 );
113 let next = await gen.next();
114 while (!next.done) {
115 if (abortController.signal.aborted) {
116 next = await gen.return(errorPromptLog);
117 break;
118 }
119
120 const chunk = next.value;
121
122 yield chunk;
123 next = await gen.next();
124 }
125 if (config.experimental?.readResponseTTS && "completion" in next.value) {
126 void TTS.read(next.value?.completion);
127 }
128
129 void Telemetry.capture(
130 "chat",
131 {
132 model: model.model,
133 provider: model.providerName,
134 },
135 true,
136 );
137
138 if (!next.done) {
139 throw new Error("Will never happen");
140 }
141
142 return next.value;
143 }
144}