| 1 | import { |
| 2 | ChatMessage, |
| 3 | ILLM, |
| 4 | Prediction, |
| 5 | PromptLog, |
| 6 | type StreamDiffLinesType, |
| 7 | } from ".."; |
| 8 | import { DEFAULT_MAX_TOKENS } from "../llm/constants"; |
| 9 | import { countTokens } from "../llm/countTokens"; |
| 10 | import { renderChatMessage } from "../util/messageContent"; |
| 11 | import { APPLY_UNIQUE_TOKEN } from "./constants.js"; |
| 12 | |
| 13 | const INFINITE_STREAM_SAFETY = 0.9; |
| 14 | |
| 15 | const DUD_PROMPT_LOG: PromptLog = { |
| 16 | modelTitle: "", |
| 17 | modelProvider: "", |
| 18 | prompt: "", |
| 19 | completion: "", |
| 20 | }; |
| 21 | |
| 22 | const RECURSIVE_PROMPT = `Continue EXACTLY where you left`; |
| 23 | |
| 24 | export async function* recursiveStream( |
| 25 | llm: ILLM, |
| 26 | abortController: AbortController, |
| 27 | type: StreamDiffLinesType, |
| 28 | prompt: ChatMessage[] | string, |
| 29 | prediction: Prediction | undefined, |
| 30 | currentBuffer = "", |
| 31 | isContinuation = false, |
| 32 | ): AsyncGenerator<string | ChatMessage> { |
| 33 | const maxTokens = llm.completionOptions?.maxTokens ?? DEFAULT_MAX_TOKENS; |
| 34 | const safeTokens = maxTokens * INFINITE_STREAM_SAFETY; |
| 35 | let totalTokens = 0; |
| 36 | let buffer = currentBuffer; |
| 37 | // let whiteSpaceAtEndOfBuffer = buffer.match(/\s*$/)?.[0] ?? ""; // attempts at fixing whitespace bug with recursive boundaries |
| 38 | |
| 39 | const injectApplyToken = type === "apply" && shouldInjectApplyToken(llm); |
| 40 | if (typeof prompt === "string") { |
| 41 | const finalPrompt = injectApplyToken ? prompt + APPLY_UNIQUE_TOKEN : prompt; |
| 42 | |
| 43 | const generator = llm.streamComplete(finalPrompt, abortController.signal, { |
| 44 | raw: true, |
| 45 | prediction: undefined, |
| 46 | reasoning: false, |
| 47 | }); |
| 48 | |
| 49 | for await (const chunk of generator) { |
| 50 | yield chunk; |
| 51 | buffer += chunk; |
| 52 | totalTokens += countTokens(chunk); |
| 53 | |
| 54 | if (totalTokens >= safeTokens) { |
| 55 | throw new Error( |
| 56 | "Token limit reached. File/range likely too large for this edit", |
| 57 | ); |
| 58 | // const continuationPrompt = `${RECURSIVE_PROMPT}:\n\n${buffer}`; |
| 59 | |
| 60 | // await generator.return(DUD_PROMPT_LOG); // kill the previous generator |
| 61 | |
| 62 | // // TODO - Prediction capabilities lost because of partial input |
| 63 | // yield* recursiveStream( |
| 64 | // llm, |
| 65 | // abortController, |
| 66 | // continuationPrompt, |
| 67 | // undefined, |
| 68 | // buffer, |
| 69 | // true, |
| 70 | // ); // Recursively stream the continuation |
| 71 | |
| 72 | // return; |
| 73 | } |
| 74 | } |
| 75 | } else { |
| 76 | const promptMessages = injectApplyToken |
| 77 | ? appendTokenToLastMessage(prompt, APPLY_UNIQUE_TOKEN) |
| 78 | : prompt; |
| 79 | |
| 80 | const generator = llm.streamChat(promptMessages, abortController.signal, { |
| 81 | raw: true, |
| 82 | prediction: undefined, |
| 83 | reasoning: false, |
| 84 | }); |
| 85 | |
| 86 | for await (const chunk of generator) { |
| 87 | yield chunk; |
| 88 | const rendered = renderChatMessage(chunk); |
| 89 | buffer += rendered; |
| 90 | totalTokens += countTokens(chunk.content); |
| 91 | |
| 92 | if (totalTokens >= safeTokens) { |
| 93 | throw new Error( |
| 94 | "Token limit reached. File/range likely too large for this edit", |
| 95 | ); |
| 96 | // const continuationPrompt: ChatMessage[] = [ |
| 97 | // ...(isContinuation ? prompt.slice(0, -2) : prompt), |
| 98 | // { |
| 99 | // role: "assistant", |
| 100 | // content: buffer, |
| 101 | // }, |
| 102 | // { |
| 103 | // role: "user", |
| 104 | // content: RECURSIVE_PROMPT, |
| 105 | // }, |
| 106 | // ]; |
| 107 | |
| 108 | // await generator.return(DUD_PROMPT_LOG); |
| 109 | // yield* recursiveStream( |
| 110 | // llm, |
| 111 | // abortController, |
| 112 | // continuationPrompt, |
| 113 | // undefined, |
| 114 | // buffer, |
| 115 | // true, |
| 116 | // ); |
| 117 | // return; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | function shouldInjectApplyToken(llm: ILLM): boolean { |
| 124 | const model = llm.model?.toLowerCase() ?? ""; |
| 125 | return ( |
| 126 | llm.underlyingProviderName === "inception" && model.includes("mercury") |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | function appendTokenToLastMessage( |
| 131 | messages: ChatMessage[], |
| 132 | token: string, |
| 133 | ): ChatMessage[] { |
| 134 | if (messages.length === 0) { |
| 135 | return messages; |
| 136 | } |
| 137 | |
| 138 | const lastMessage = messages[messages.length - 1]; |
| 139 | if (typeof lastMessage.content !== "string") { |
| 140 | return messages; |
| 141 | } |
| 142 | |
| 143 | if (lastMessage.content.endsWith(token)) { |
| 144 | return messages; |
| 145 | } |
| 146 | |
| 147 | const updatedMessages = [...messages]; |
| 148 | updatedMessages[updatedMessages.length - 1] = { |
| 149 | ...lastMessage, |
| 150 | content: lastMessage.content + token, |
| 151 | }; |
| 152 | |
| 153 | return updatedMessages; |
| 154 | } |