Elffuss BraidHub

kikocis/continue public

main · 3.5 KiBRawPermalink
1import { ILLM, LLMFullCompletionOptions } from "..";
2
3import { removeCodeBlocksAndTrim, removeQuotesAndEscapes } from ".";
4
5import type { FromCoreProtocol, ToCoreProtocol } from "../protocol";
6import type { IMessenger } from "../protocol/messenger";
7import { renderChatMessage } from "./messageContent";
8import { convertFromUnifiedHistory } from "./messageConversion";
9
10export class ChatDescriber {
11 static maxTokens = 12;
12 static prompt: string | undefined =
13 "Given the following... please reply with a title for the chat that is 3-4 words in length, all words used should be directly related to the content of the chat, avoid using verbs unless they are directly related to the content of the chat, no additional text or explanation, you don't need ending punctuation.\n\n";
14 static messenger: IMessenger<ToCoreProtocol, FromCoreProtocol>;
15
16 static async describe(
17 model: ILLM,
18 completionOptions: LLMFullCompletionOptions,
19 message: string,
20 ): Promise<string | undefined> {
21 if (!ChatDescriber.prompt) {
22 return;
23 }
24
25 // Clean up and distill the message we want to send to the LLM
26 message = removeCodeBlocksAndTrim(message);
27
28 if (!message) {
29 return;
30 }
31
32 completionOptions.maxTokens = ChatDescriber.maxTokens;
33
34 // Prompt the user's current LLM for the title
35 const titleResponse = await model.chat(
36 [
37 {
38 role: "user",
39 content: ChatDescriber.prompt + message,
40 },
41 ],
42 new AbortController().signal,
43 completionOptions,
44 );
45
46 // Set the title
47 return removeQuotesAndEscapes(renderChatMessage(titleResponse));
48 }
49
50 // CLI-specific method that works with BaseLlmApi
51 static async describeWithBaseLlmApi(
52 llmApi: any, // BaseLlmApi - using any to avoid import issues
53 modelConfig: any, // ModelConfig - using any to avoid import issues
54 message: string,
55 ): Promise<string | undefined> {
56 if (!ChatDescriber.prompt) {
57 return;
58 }
59
60 // Clean up and distill the message we want to send to the LLM
61 message = removeCodeBlocksAndTrim(message);
62
63 if (!message) {
64 return;
65 }
66
67 try {
68 // Create the chat message in the unified format
69 const chatMessage = {
70 role: "user" as const,
71 content: ChatDescriber.prompt + message,
72 };
73
74 // Convert to OpenAI format - use a simple fallback to avoid import issues
75 const openaiMessages = convertFromUnifiedHistory([
76 {
77 message: chatMessage,
78 contextItems: [],
79 },
80 ]);
81
82 // Set up completion options for non-streaming
83 const completionOptions = {
84 model: modelConfig.model,
85 messages: openaiMessages,
86 max_tokens: ChatDescriber.maxTokens,
87 stream: false as const,
88 };
89
90 // Call the LLM
91 const titleResponse = await llmApi.chatCompletionNonStream(
92 completionOptions,
93 new AbortController().signal,
94 );
95
96 // Extract and clean up the response
97 if (titleResponse.choices && titleResponse.choices.length > 0) {
98 const content = titleResponse.choices[0].message.content;
99 if (content) {
100 return removeQuotesAndEscapes(content);
101 }
102 }
103
104 return undefined;
105 } catch (error) {
106 return undefined;
107 }
108 }
109
110 // // TODO: Allow the user to manually set specific/tailored prompts to generate their titles
111 // static async setup() {
112 // if(config?.prompt) {
113 // ChatDescriber.prompt = config?.prompt;
114 // }
115 // }
116}