Changesets · Branches · Tags · Compare
| Changeset | 54ea0739010d8625499eca7bf32cf7681d5e637b24f4beb67c323ca6fcd39ff4 |
|---|---|
| Author | Nate Sesti <33237525+sestinj@users.noreply.github.com> · 2025-05-25 16:33 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-05-25 16:33 -07:00 |
| Parents | 158decf78aee, ea74bf50486d |
56 files changed
modified core/commands/slash/http.ts +1 −1
| @@ -1,6 +1,6 @@ | ||
| 1 | +import { streamResponse } from "@continuedev/fetch"; | |
| 1 | 2 | import { SlashCommand } from "../../index.js"; |
| 2 | 3 | import { removeQuotesAndEscapes } from "../../util/index.js"; |
| 3 | -import { streamResponse } from "../../llm/stream.js"; | |
| 4 | 4 | |
| 5 | 5 | const HttpSlashCommand: SlashCommand = { |
| 6 | 6 | name: "http", |
modified core/core.ts +65 −70
| @@ -35,7 +35,6 @@ | ||
| 35 | 35 | |
| 36 | 36 | import { |
| 37 | 37 | ContextItemWithId, |
| 38 | - DiffLine, | |
| 39 | 38 | IdeSettings, |
| 40 | 39 | ModelDescription, |
| 41 | 40 | RangeInFile, |
| @@ -62,61 +61,7 @@ | ||
| 62 | 61 | import { llmStreamChat } from "./llm/streamChat"; |
| 63 | 62 | import type { FromCoreProtocol, ToCoreProtocol } from "./protocol"; |
| 64 | 63 | import type { IMessenger, Message } from "./protocol/messenger"; |
| 65 | - | |
| 66 | -// This function is used for jetbrains inline edit and apply | |
| 67 | -async function* streamDiffLinesGenerator( | |
| 68 | - configHandler: ConfigHandler, | |
| 69 | - abortedMessageIds: Set<string>, | |
| 70 | - msg: Message<ToCoreProtocol["streamDiffLines"][0]>, | |
| 71 | -): AsyncGenerator<DiffLine> { | |
| 72 | - const { | |
| 73 | - highlighted, | |
| 74 | - prefix, | |
| 75 | - suffix, | |
| 76 | - input, | |
| 77 | - language, | |
| 78 | - modelTitle, | |
| 79 | - includeRulesInSystemMessage, | |
| 80 | - } = msg.data; | |
| 81 | - | |
| 82 | - const { config } = await configHandler.loadConfig(); | |
| 83 | - if (!config) { | |
| 84 | - throw new Error("Failed to load config"); | |
| 85 | - } | |
| 86 | - | |
| 87 | - // Title can be an edit, chat, or apply model | |
| 88 | - // Fall back to chat | |
| 89 | - const llm = | |
| 90 | - config.modelsByRole.edit.find((m) => m.title === modelTitle) ?? | |
| 91 | - config.modelsByRole.apply.find((m) => m.title === modelTitle) ?? | |
| 92 | - config.modelsByRole.chat.find((m) => m.title === modelTitle) ?? | |
| 93 | - config.selectedModelByRole.chat; | |
| 94 | - | |
| 95 | - if (!llm) { | |
| 96 | - throw new Error("No model selected"); | |
| 97 | - } | |
| 98 | - | |
| 99 | - // rules included for edit, NOT apply | |
| 100 | - const rules = includeRulesInSystemMessage ? config.rules : undefined; | |
| 101 | - | |
| 102 | - for await (const diffLine of streamDiffLines({ | |
| 103 | - highlighted, | |
| 104 | - prefix, | |
| 105 | - suffix, | |
| 106 | - llm, | |
| 107 | - rulesToInclude: rules, | |
| 108 | - input, | |
| 109 | - language, | |
| 110 | - onlyOneInsertion: false, | |
| 111 | - overridePrompt: undefined, | |
| 112 | - })) { | |
| 113 | - if (abortedMessageIds.has(msg.messageId)) { | |
| 114 | - abortedMessageIds.delete(msg.messageId); | |
| 115 | - break; | |
| 116 | - } | |
| 117 | - yield diffLine; | |
| 118 | - } | |
| 119 | -} | |
| 64 | +import { StreamAbortManager } from "./util/abortManager"; | |
| 120 | 65 | |
| 121 | 66 | export class Core { |
| 122 | 67 | configHandler: ConfigHandler; |
| @@ -132,7 +77,18 @@ | ||
| 132 | 77 | this.globalContext.get("indexingPaused") === true, |
| 133 | 78 | ); |
| 134 | 79 | |
| 135 | - private abortedMessageIds: Set<string> = new Set(); | |
| 80 | + private messageAbortControllers = new Map<string, AbortController>(); | |
| 81 | + private addMessageAbortController(id: string): AbortController { | |
| 82 | + const controller = new AbortController(); | |
| 83 | + this.messageAbortControllers.set(id, controller); | |
| 84 | + controller.signal.addEventListener("abort", () => { | |
| 85 | + this.messageAbortControllers.delete(id); | |
| 86 | + }); | |
| 87 | + return controller; | |
| 88 | + } | |
| 89 | + private abortById(messageId: string) { | |
| 90 | + this.messageAbortControllers.get(messageId)?.abort(); | |
| 91 | + } | |
| 136 | 92 | |
| 137 | 93 | invoke<T extends keyof ToCoreProtocol>( |
| 138 | 94 | messageType: T, |
| @@ -301,7 +257,7 @@ | ||
| 301 | 257 | }); |
| 302 | 258 | |
| 303 | 259 | on("abort", (msg) => { |
| 304 | - this.abortedMessageIds.add(msg.messageId); | |
| 260 | + this.abortById(msg.data ?? msg.messageId); | |
| 305 | 261 | }); |
| 306 | 262 | |
| 307 | 263 | on("ping", (msg) => { |
| @@ -470,27 +426,28 @@ | ||
| 470 | 426 | } |
| 471 | 427 | }); |
| 472 | 428 | |
| 473 | - on("llm/streamChat", (msg) => | |
| 474 | - llmStreamChat( | |
| 429 | + on("llm/streamChat", (msg) => { | |
| 430 | + const abortController = this.addMessageAbortController(msg.messageId); | |
| 431 | + return llmStreamChat( | |
| 475 | 432 | this.configHandler, |
| 476 | - this.abortedMessageIds, | |
| 433 | + abortController, | |
| 477 | 434 | msg, |
| 478 | 435 | this.ide, |
| 479 | 436 | this.messenger, |
| 480 | - ), | |
| 481 | - ); | |
| 437 | + ); | |
| 438 | + }); | |
| 482 | 439 | |
| 483 | 440 | on("llm/complete", async (msg) => { |
| 484 | - const model = (await this.configHandler.loadConfig()).config | |
| 485 | - ?.selectedModelByRole.chat; | |
| 486 | - | |
| 441 | + const { config } = await this.configHandler.loadConfig(); | |
| 442 | + const model = config?.selectedModelByRole.chat; | |
| 487 | 443 | if (!model) { |
| 488 | 444 | throw new Error("No chat model selected"); |
| 489 | 445 | } |
| 446 | + const abortController = this.addMessageAbortController(msg.messageId); | |
| 490 | 447 | |
| 491 | 448 | const completion = await model.complete( |
| 492 | 449 | msg.data.prompt, |
| 493 | - new AbortController().signal, | |
| 450 | + abortController.signal, | |
| 494 | 451 | msg.data.completionOptions, |
| 495 | 452 | ); |
| 496 | 453 | return completion; |
| @@ -532,9 +489,47 @@ | ||
| 532 | 489 | this.completionProvider.cancel(); |
| 533 | 490 | }); |
| 534 | 491 | |
| 535 | - on("streamDiffLines", (msg) => | |
| 536 | - streamDiffLinesGenerator(this.configHandler, this.abortedMessageIds, msg), | |
| 537 | - ); | |
| 492 | + on("streamDiffLines", async (msg) => { | |
| 493 | + const { config } = await this.configHandler.loadConfig(); | |
| 494 | + if (!config) { | |
| 495 | + throw new Error("Failed to load config"); | |
| 496 | + } | |
| 497 | + | |
| 498 | + const { data } = msg; | |
| 499 | + | |
| 500 | + // Title can be an edit, chat, or apply model | |
| 501 | + // Fall back to chat | |
| 502 | + const llm = | |
| 503 | + config.modelsByRole.edit.find((m) => m.title === data.modelTitle) ?? | |
| 504 | + config.modelsByRole.apply.find((m) => m.title === data.modelTitle) ?? | |
| 505 | + config.modelsByRole.chat.find((m) => m.title === data.modelTitle) ?? | |
| 506 | + config.selectedModelByRole.chat; | |
| 507 | + | |
| 508 | + if (!llm) { | |
| 509 | + throw new Error("No model selected"); | |
| 510 | + } | |
| 511 | + | |
| 512 | + return streamDiffLines({ | |
| 513 | + highlighted: data.highlighted, | |
| 514 | + prefix: data.prefix, | |
| 515 | + suffix: data.suffix, | |
| 516 | + llm, | |
| 517 | + // rules included for edit, NOT apply | |
| 518 | + rulesToInclude: data.includeRulesInSystemMessage | |
| 519 | + ? config.rules | |
| 520 | + : undefined, | |
| 521 | + input: data.input, | |
| 522 | + language: data.language, | |
| 523 | + onlyOneInsertion: false, | |
| 524 | + overridePrompt: undefined, | |
| 525 | + abortControllerId: data.fileUri ?? "current-file-stream", // not super important since currently cancelling apply will cancel all streams it's one file at a time | |
| 526 | + }); | |
| 527 | + }); | |
| 528 | + | |
| 529 | + on("cancelApply", async (msg) => { | |
| 530 | + const abortManager = StreamAbortManager.getInstance(); | |
| 531 | + abortManager.clear(); | |
| 532 | + }); | |
| 538 | 533 | |
| 539 | 534 | on("completeOnboarding", this.handleCompleteOnboarding.bind(this)); |
| 540 | 535 |
modified core/edit/recursiveStream.ts +5 −2
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | |
| 17 | 17 | export async function* recursiveStream( |
| 18 | 18 | llm: ILLM, |
| 19 | + abortController: AbortController, | |
| 19 | 20 | prompt: ChatMessage[] | string, |
| 20 | 21 | prediction: Prediction | undefined, |
| 21 | 22 | currentBuffer = "", |
| @@ -28,7 +29,7 @@ | ||
| 28 | 29 | // let whiteSpaceAtEndOfBuffer = buffer.match(/\s*$/)?.[0] ?? ""; // attempts at fixing whitespace bug with recursive boundaries |
| 29 | 30 | |
| 30 | 31 | if (typeof prompt === "string") { |
| 31 | - const generator = llm.streamComplete(prompt, new AbortController().signal, { | |
| 32 | + const generator = llm.streamComplete(prompt, abortController.signal, { | |
| 32 | 33 | raw: true, |
| 33 | 34 | prediction: undefined, |
| 34 | 35 | reasoning: false, |
| @@ -50,6 +51,7 @@ | ||
| 50 | 51 | // // TODO - Prediction capabilities lost because of partial input |
| 51 | 52 | // yield* recursiveStream( |
| 52 | 53 | // llm, |
| 54 | + // abortController, | |
| 53 | 55 | // continuationPrompt, |
| 54 | 56 | // undefined, |
| 55 | 57 | // buffer, |
| @@ -60,7 +62,7 @@ | ||
| 60 | 62 | } |
| 61 | 63 | } |
| 62 | 64 | } else { |
| 63 | - const generator = llm.streamChat(prompt, new AbortController().signal, { | |
| 65 | + const generator = llm.streamChat(prompt, abortController.signal, { | |
| 64 | 66 | prediction, |
| 65 | 67 | reasoning: false, |
| 66 | 68 | }); |
| @@ -90,6 +92,7 @@ | ||
| 90 | 92 | // await generator.return(DUD_PROMPT_LOG); |
| 91 | 93 | // yield* recursiveStream( |
| 92 | 94 | // llm, |
| 95 | + // abortController, | |
| 93 | 96 | // continuationPrompt, |
| 94 | 97 | // undefined, |
| 95 | 98 | // buffer, |
modified core/edit/streamDiffLines.ts +6 −1
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | import { streamLines } from "../diff/util"; |
| 21 | 21 | import { getSystemMessageWithRules } from "../llm/rules/getSystemMessageWithRules"; |
| 22 | 22 | import { gptEditPrompt } from "../llm/templates/edit"; |
| 23 | +import { StreamAbortManager } from "../util/abortManager"; | |
| 23 | 24 | import { findLast } from "../util/findLast"; |
| 24 | 25 | import { Telemetry } from "../util/posthog"; |
| 25 | 26 | import { recursiveStream } from "./recursiveStream"; |
| @@ -63,6 +64,7 @@ | ||
| 63 | 64 | highlighted, |
| 64 | 65 | suffix, |
| 65 | 66 | llm, |
| 67 | + abortControllerId, | |
| 66 | 68 | input, |
| 67 | 69 | language, |
| 68 | 70 | onlyOneInsertion, |
| @@ -73,12 +75,15 @@ | ||
| 73 | 75 | highlighted: string; |
| 74 | 76 | suffix: string; |
| 75 | 77 | llm: ILLM; |
| 78 | + abortControllerId: string; | |
| 76 | 79 | input: string; |
| 77 | 80 | language: string | undefined; |
| 78 | 81 | onlyOneInsertion: boolean; |
| 79 | 82 | overridePrompt: ChatMessage[] | undefined; |
| 80 | 83 | rulesToInclude: RuleWithSource[] | undefined; |
| 81 | 84 | }): AsyncGenerator<DiffLine> { |
| 85 | + const abortManager = StreamAbortManager.getInstance(); | |
| 86 | + const abortController = abortManager.get(abortControllerId); | |
| 82 | 87 | void Telemetry.capture( |
| 83 | 88 | "inlineEdit", |
| 84 | 89 | { |
| @@ -158,7 +163,7 @@ | ||
| 158 | 163 | content: highlighted, |
| 159 | 164 | }; |
| 160 | 165 | |
| 161 | - const completion = recursiveStream(llm, prompt, prediction); | |
| 166 | + const completion = recursiveStream(llm, abortController, prompt, prediction); | |
| 162 | 167 | |
| 163 | 168 | let lines = streamLines(completion); |
| 164 | 169 |
modified core/index.d.ts +1 −0
| @@ -1240,6 +1240,7 @@ | ||
| 1240 | 1240 | language: string | undefined; |
| 1241 | 1241 | modelTitle: string | undefined; |
| 1242 | 1242 | includeRulesInSystemMessage: boolean; |
| 1243 | + fileUri?: string; | |
| 1243 | 1244 | } |
| 1244 | 1245 | |
| 1245 | 1246 | export interface HighlightedCodePayload { |
modified core/llm/index.ts +3 −0
| @@ -382,6 +382,9 @@ | ||
| 382 | 382 | |
| 383 | 383 | // Error mapping to be more helpful |
| 384 | 384 | if (!resp.ok) { |
| 385 | + if (resp.status === 499) { | |
| 386 | + return resp; // client side cancellation | |
| 387 | + } | |
| 385 | 388 | let text = await resp.text(); |
| 386 | 389 | if (resp.status === 404 && !resp.url.includes("/v1")) { |
| 387 | 390 | const error = JSON.parse(text)?.error?.replace(/"/g, "'"); |
modified core/llm/llms/Anthropic.ts +5 −1
| @@ -1,7 +1,7 @@ | ||
| 1 | +import { streamSse } from "@continuedev/fetch"; | |
| 1 | 2 | import { ChatMessage, CompletionOptions, LLMOptions } from "../../index.js"; |
| 2 | 3 | import { renderChatMessage, stripImages } from "../../util/messageContent.js"; |
| 3 | 4 | import { BaseLLM } from "../index.js"; |
| 4 | -import { streamSse } from "../stream.js"; | |
| 5 | 5 | |
| 6 | 6 | class Anthropic extends BaseLLM { |
| 7 | 7 | static providerName = "anthropic"; |
| @@ -213,6 +213,10 @@ | ||
| 213 | 213 | signal, |
| 214 | 214 | }); |
| 215 | 215 | |
| 216 | + if (response.status === 499) { | |
| 217 | + return; // Aborted by user | |
| 218 | + } | |
| 219 | + | |
| 216 | 220 | if (!response.ok) { |
| 217 | 221 | const json = await response.json(); |
| 218 | 222 | if (json.type === "error") { |
modified core/llm/llms/Asksage.ts +4 −8
| @@ -134,10 +134,8 @@ | ||
| 134 | 134 | body: JSON.stringify(args), |
| 135 | 135 | }); |
| 136 | 136 | |
| 137 | - if (!response.ok) { | |
| 138 | - throw new Error( | |
| 139 | - `API request failed with status ${response.status}: ${response.statusText}`, | |
| 140 | - ); | |
| 137 | + if (response.status === 499) { | |
| 138 | + return ""; // Aborted by user | |
| 141 | 139 | } |
| 142 | 140 | |
| 143 | 141 | const data = await response.json(); |
| @@ -167,10 +165,8 @@ | ||
| 167 | 165 | signal, |
| 168 | 166 | }); |
| 169 | 167 | |
| 170 | - if (!response.ok) { | |
| 171 | - throw new Error( | |
| 172 | - `API request failed with status ${response.status}: ${response.statusText}`, | |
| 173 | - ); | |
| 168 | + if (response.status === 499) { | |
| 169 | + return; // Aborted by user | |
| 174 | 170 | } |
| 175 | 171 | |
| 176 | 172 | const data = await response.json(); |
modified core/llm/llms/Cloudflare.ts +1 −1
| @@ -1,7 +1,7 @@ | ||
| 1 | +import { streamSse } from "@continuedev/fetch"; | |
| 1 | 2 | import { ChatMessage, CompletionOptions } from "../../index.js"; |
| 2 | 3 | import { renderChatMessage } from "../../util/messageContent.js"; |
| 3 | 4 | import { BaseLLM } from "../index.js"; |
| 4 | -import { streamSse } from "../stream.js"; | |
| 5 | 5 | |
| 6 | 6 | export default class Cloudflare extends BaseLLM { |
| 7 | 7 | static providerName = "cloudflare"; |
modified core/llm/llms/Cohere.ts +5 −1
| @@ -1,3 +1,4 @@ | ||
| 1 | +import { streamJSON } from "@continuedev/fetch"; | |
| 1 | 2 | import { |
| 2 | 3 | ChatMessage, |
| 3 | 4 | Chunk, |
| @@ -6,7 +7,6 @@ | ||
| 6 | 7 | } from "../../index.js"; |
| 7 | 8 | import { renderChatMessage, stripImages } from "../../util/messageContent.js"; |
| 8 | 9 | import { BaseLLM } from "../index.js"; |
| 9 | -import { streamJSON } from "../stream.js"; | |
| 10 | 10 | |
| 11 | 11 | class Cohere extends BaseLLM { |
| 12 | 12 | static providerName = "cohere"; |
| @@ -84,6 +84,10 @@ | ||
| 84 | 84 | signal, |
| 85 | 85 | }); |
| 86 | 86 | |
| 87 | + if (resp.status === 499) { | |
| 88 | + return; // Aborted by user | |
| 89 | + } | |
| 90 | + | |
| 87 | 91 | if (options.stream === false) { |
| 88 | 92 | const data = await resp.json(); |
| 89 | 93 | yield { role: "assistant", content: data.text }; |
modified core/llm/llms/Deepseek.ts +1 −1
| @@ -1,5 +1,5 @@ | ||
| 1 | +import { streamSse } from "@continuedev/fetch"; | |
| 1 | 2 | import { CompletionOptions, LLMOptions } from "../../index.js"; |
| 2 | -import { streamSse } from "../stream.js"; | |
| 3 | 3 | import { osModelsEditPrompt } from "../templates/edit.js"; |
| 4 | 4 | |
| 5 | 5 | import OpenAI from "./OpenAI.js"; |
modified core/llm/llms/Flowise.ts +7 −3
| @@ -132,12 +132,16 @@ | ||
| 132 | 132 | ): AsyncGenerator<ChatMessage> { |
| 133 | 133 | const requestBody = this._getRequestBody(messages, options); |
| 134 | 134 | const { socket, socketInfo } = await this._initializeSocket(); |
| 135 | - const request = this.fetch(this._getChatUrl(), { | |
| 135 | + const response = await this.fetch(this._getChatUrl(), { | |
| 136 | 136 | method: "POST", |
| 137 | 137 | headers: this._getHeaders(), |
| 138 | 138 | body: JSON.stringify({ ...requestBody, socketIOClientId: socket.id }), |
| 139 | 139 | signal, |
| 140 | - }).then((res) => res.json()); | |
| 140 | + }); | |
| 141 | + | |
| 142 | + if (response.status === 499) { | |
| 143 | + return; // Aborted by user | |
| 144 | + } | |
| 141 | 145 | |
| 142 | 146 | while (await socketInfo.hasNextToken()) { |
| 143 | 147 | yield { role: "assistant", content: socketInfo.getCurrentMessage() }; |
| @@ -145,7 +149,7 @@ | ||
| 145 | 149 | if (socketInfo.error) { |
| 146 | 150 | socket.disconnect(); |
| 147 | 151 | try { |
| 148 | - yield { role: "assistant", content: (await request).text }; | |
| 152 | + yield { role: "assistant", content: await response.text() }; | |
| 149 | 153 | } catch (error: any) { |
| 150 | 154 | yield { role: "assistant", content: (error as Error).message ?? error }; |
| 151 | 155 | } |
modified core/llm/llms/FreeTrial.ts +1 −1
| @@ -1,3 +1,4 @@ | ||
| 1 | +import { streamResponse } from "@continuedev/fetch"; | |
| 1 | 2 | import { TRIAL_FIM_MODEL } from "../../config/onboarding.js"; |
| 2 | 3 | import { getHeaders } from "../../continueServer/stubs/headers.js"; |
| 3 | 4 | import { TRIAL_PROXY_URL } from "../../control-plane/client.js"; |
| @@ -8,7 +9,6 @@ | ||
| 8 | 9 | LLMOptions, |
| 9 | 10 | } from "../../index.js"; |
| 10 | 11 | import { BaseLLM } from "../index.js"; |
| 11 | -import { streamResponse } from "../stream.js"; | |
| 12 | 12 | |
| 13 | 13 | class FreeTrial extends BaseLLM { |
| 14 | 14 | static providerName = "free-trial"; |
modified core/llm/llms/Gemini.ts +4 −1
| @@ -1,3 +1,4 @@ | ||
| 1 | +import { streamResponse } from "@continuedev/fetch"; | |
| 1 | 2 | import { v4 as uuidv4 } from "uuid"; |
| 2 | 3 | import { |
| 3 | 4 | AssistantChatMessage, |
| @@ -10,7 +11,6 @@ | ||
| 10 | 11 | } from "../../index.js"; |
| 11 | 12 | import { renderChatMessage, stripImages } from "../../util/messageContent.js"; |
| 12 | 13 | import { BaseLLM } from "../index.js"; |
| 13 | -import { streamResponse } from "../stream.js"; | |
| 14 | 14 | import { |
| 15 | 15 | GeminiChatContent, |
| 16 | 16 | GeminiChatContentPart, |
| @@ -438,6 +438,9 @@ | ||
| 438 | 438 | body: JSON.stringify(body), |
| 439 | 439 | signal, |
| 440 | 440 | }); |
| 441 | + if (response.status === 499) { | |
| 442 | + return; // Aborted by user | |
| 443 | + } | |
| 441 | 444 | const data = await response.json(); |
| 442 | 445 | yield { role: "assistant", content: data.candidates[0].content }; |
| 443 | 446 | } |
modified core/llm/llms/HuggingFaceInferenceAPI.ts +1 −1
| @@ -1,6 +1,6 @@ | ||
| 1 | +import { streamSse } from "@continuedev/fetch"; | |
| 1 | 2 | import { CompletionOptions } from "../../index.js"; |
| 2 | 3 | import { BaseLLM } from "../index.js"; |
| 3 | -import { streamSse } from "../stream.js"; | |
| 4 | 4 | |
| 5 | 5 | class HuggingFaceInferenceAPI extends BaseLLM { |
| 6 | 6 | static providerName = "huggingface-inference-api"; |
modified core/llm/llms/HuggingFaceTGI.ts +1 −1
| @@ -1,6 +1,6 @@ | ||
| 1 | +import { streamSse } from "@continuedev/fetch"; | |
| 1 | 2 | import { CompletionOptions, LLMOptions } from "../../index.js"; |
| 2 | 3 | import { BaseLLM } from "../index.js"; |
| 3 | -import { streamSse } from "../stream.js"; | |
| 4 | 4 | |
| 5 | 5 | class HuggingFaceTGI extends BaseLLM { |
| 6 | 6 | private static MAX_STOP_TOKENS = 4; |
modified core/llm/llms/Inception.ts +1 −1
| @@ -1,5 +1,5 @@ | ||
| 1 | +import { streamSse } from "@continuedev/fetch"; | |
| 1 | 2 | import { CompletionOptions, LLMOptions } from "../../index.js"; |
| 2 | -import { streamSse } from "../stream.js"; | |
| 3 | 3 | |
| 4 | 4 | import OpenAI from "./OpenAI.js"; |
| 5 | 5 |
modified core/llm/llms/LlamaCpp.ts +1 −1
| @@ -1,6 +1,6 @@ | ||
| 1 | +import { streamSse } from "@continuedev/fetch"; | |
| 1 | 2 | import { CompletionOptions, LLMOptions } from "../../index.js"; |
| 2 | 3 | import { BaseLLM } from "../index.js"; |
| 3 | -import { streamSse } from "../stream.js"; | |
| 4 | 4 | |
| 5 | 5 | class LlamaCpp extends BaseLLM { |
| 6 | 6 | static providerName = "llama.cpp"; |
modified core/llm/llms/Moonshot.ts +1 −1
| @@ -1,5 +1,5 @@ | ||
| 1 | +import { streamSse } from "@continuedev/fetch"; | |
| 1 | 2 | import { CompletionOptions, LLMOptions } from "../../index.js"; |
| 2 | -import { streamSse } from "../stream.js"; | |
| 3 | 3 | import { osModelsEditPrompt } from "../templates/edit.js"; |
| 4 | 4 | |
| 5 | 5 | import OpenAI from "./OpenAI.js"; |
modified core/llm/llms/Ollama.ts +4 −1
| @@ -2,6 +2,7 @@ | ||
| 2 | 2 | import { JSONSchema7, JSONSchema7Object } from "json-schema"; |
| 3 | 3 | import { v4 as uuidv4 } from "uuid"; |
| 4 | 4 | |
| 5 | +import { streamResponse } from "@continuedev/fetch"; | |
| 5 | 6 | import { |
| 6 | 7 | ChatMessage, |
| 7 | 8 | ChatMessageRole, |
| @@ -12,7 +13,6 @@ | ||
| 12 | 13 | import { renderChatMessage } from "../../util/messageContent.js"; |
| 13 | 14 | import { getRemoteModelInfo } from "../../util/ollamaHelper.js"; |
| 14 | 15 | import { BaseLLM } from "../index.js"; |
| 15 | -import { streamResponse } from "../stream.js"; | |
| 16 | 16 | |
| 17 | 17 | type OllamaChatMessage = { |
| 18 | 18 | role: ChatMessageRole; |
| @@ -457,6 +457,9 @@ | ||
| 457 | 457 | } |
| 458 | 458 | |
| 459 | 459 | if (chatOptions.stream === false) { |
| 460 | + if (response.status === 499) { | |
| 461 | + return; // Aborted by user | |
| 462 | + } | |
| 460 | 463 | const json = (await response.json()) as OllamaChatResponse; |
| 461 | 464 | yield convertChatMessage(json); |
| 462 | 465 | } else { |
modified core/llm/llms/OpenAI.ts +4 −1
| @@ -3,6 +3,7 @@ | ||
| 3 | 3 | ChatCompletionMessageParam, |
| 4 | 4 | } from "openai/resources/index"; |
| 5 | 5 | |
| 6 | +import { streamSse } from "@continuedev/fetch"; | |
| 6 | 7 | import { |
| 7 | 8 | ChatMessage, |
| 8 | 9 | CompletionOptions, |
| @@ -16,7 +17,6 @@ | ||
| 16 | 17 | LlmApiRequestType, |
| 17 | 18 | toChatBody, |
| 18 | 19 | } from "../openaiTypeConverters.js"; |
| 19 | -import { streamSse } from "../stream.js"; | |
| 20 | 20 | |
| 21 | 21 | const NON_CHAT_MODELS = [ |
| 22 | 22 | "text-davinci-002", |
| @@ -352,6 +352,9 @@ | ||
| 352 | 352 | |
| 353 | 353 | // Handle non-streaming response |
| 354 | 354 | if (body.stream === false) { |
| 355 | + if (response.status === 499) { | |
| 356 | + return; // Aborted by user | |
| 357 | + } | |
| 355 | 358 | const data = await response.json(); |
| 356 | 359 | yield data.choices[0].message; |
| 357 | 360 | return; |
modified core/llm/llms/SiliconFlow.ts +2 −2
| @@ -1,5 +1,5 @@ | ||
| 1 | +import { streamSse } from "@continuedev/fetch"; | |
| 1 | 2 | import { Chunk, CompletionOptions, LLMOptions } from "../../index.js"; |
| 2 | -import { streamSse } from "../stream.js"; | |
| 3 | 3 | import { osModelsEditPrompt } from "../templates/edit.js"; |
| 4 | 4 | |
| 5 | 5 | import OpenAI from "./OpenAI.js"; |
| @@ -58,7 +58,7 @@ | ||
| 58 | 58 | console.warn("[SiliconFlow] rerank: query is empty"); |
| 59 | 59 | return []; |
| 60 | 60 | } |
| 61 | - | |
| 61 | + | |
| 62 | 62 | if (!chunks || chunks.length === 0) { |
| 63 | 63 | console.warn("[SiliconFlow] rerank: chunks is empty"); |
| 64 | 64 | return []; |
modified core/llm/llms/VertexAI.ts +23 −5
| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | import { GoogleAuth } from "google-auth-library"; |
| 2 | 2 | |
| 3 | +import { streamResponse, streamSse } from "@continuedev/fetch"; | |
| 3 | 4 | import { ChatMessage, CompletionOptions, LLMOptions } from "../../index.js"; |
| 4 | 5 | import { renderChatMessage, stripImages } from "../../util/messageContent.js"; |
| 5 | 6 | import { BaseLLM } from "../index.js"; |
| 6 | -import { streamResponse, streamSse } from "../stream.js"; | |
| 7 | 7 | |
| 8 | 8 | import Anthropic from "./Anthropic.js"; |
| 9 | 9 | import Gemini from "./Gemini.js"; |
| @@ -95,6 +95,7 @@ | ||
| 95 | 95 | protected async *StreamChatAnthropic( |
| 96 | 96 | messages: ChatMessage[], |
| 97 | 97 | options: CompletionOptions, |
| 98 | + signal: AbortSignal, | |
| 98 | 99 | ): AsyncGenerator<ChatMessage> { |
| 99 | 100 | const systemMessage = stripImages( |
| 100 | 101 | messages.filter((m) => m.role === "system")[0]?.content ?? "", |
| @@ -128,8 +129,13 @@ | ||
| 128 | 129 | ] |
| 129 | 130 | : systemMessage, |
| 130 | 131 | }), |
| 132 | + signal, | |
| 131 | 133 | }); |
| 132 | 134 | |
| 135 | + if (response.status === 499) { | |
| 136 | + return; // Aborted by user | |
| 137 | + } | |
| 138 | + | |
| 133 | 139 | if (options.stream === false) { |
| 134 | 140 | const data = await response.json(); |
| 135 | 141 | yield { role: "assistant", content: data.content[0].text }; |
| @@ -150,6 +156,7 @@ | ||
| 150 | 156 | private async *streamChatGemini( |
| 151 | 157 | messages: ChatMessage[], |
| 152 | 158 | options: CompletionOptions, |
| 159 | + signal: AbortSignal, | |
| 153 | 160 | ): AsyncGenerator<ChatMessage> { |
| 154 | 161 | const apiURL = new URL( |
| 155 | 162 | `publishers/google/models/${options.model}:streamGenerateContent`, |
| @@ -160,6 +167,7 @@ | ||
| 160 | 167 | const response = await this.fetch(apiURL, { |
| 161 | 168 | method: "POST", |
| 162 | 169 | body: JSON.stringify(body), |
| 170 | + signal, | |
| 163 | 171 | }); |
| 164 | 172 | for await (const message of this.geminiInstance.processGeminiResponse( |
| 165 | 173 | streamResponse(response), |
| @@ -171,6 +179,7 @@ | ||
| 171 | 179 | private async *streamChatBison( |
| 172 | 180 | messages: ChatMessage[], |
| 173 | 181 | options: CompletionOptions, |
| 182 | + signal: AbortSignal, | |
| 174 | 183 | ): AsyncGenerator<ChatMessage> { |
| 175 | 184 | const instances = messages.map((message) => ({ prompt: message.content })); |
| 176 | 185 | |
| @@ -193,7 +202,11 @@ | ||
| 193 | 202 | const response = await this.fetch(apiURL, { |
| 194 | 203 | method: "POST", |
| 195 | 204 | body: JSON.stringify(body), |
| 205 | + signal, | |
| 196 | 206 | }); |
| 207 | + if (response.status === 499) { | |
| 208 | + return; // Aborted by user | |
| 209 | + } | |
| 197 | 210 | const data = await response.json(); |
| 198 | 211 | yield { role: "assistant", content: data.predictions[0].content }; |
| 199 | 212 | } |
| @@ -203,6 +216,7 @@ | ||
| 203 | 216 | protected async *StreamChatMistral( |
| 204 | 217 | messages: ChatMessage[], |
| 205 | 218 | options: CompletionOptions, |
| 219 | + signal: AbortSignal, | |
| 206 | 220 | ): AsyncGenerator<ChatMessage> { |
| 207 | 221 | const apiBase = this.apiBase!; |
| 208 | 222 | const apiURL = new URL( |
| @@ -228,6 +242,7 @@ | ||
| 228 | 242 | const response = await this.fetch(apiURL, { |
| 229 | 243 | method: "POST", |
| 230 | 244 | body: JSON.stringify(body), |
| 245 | + signal, | |
| 231 | 246 | }); |
| 232 | 247 | |
| 233 | 248 | for await (const chunk of streamSse(response)) { |
| @@ -304,6 +319,9 @@ | ||
| 304 | 319 | }), |
| 305 | 320 | signal, |
| 306 | 321 | }); |
| 322 | + if (resp.status === 499) { | |
| 323 | + return; // Aborted by user | |
| 324 | + } | |
| 307 | 325 | // Streaming is not supported by code-gecko |
| 308 | 326 | // TODO: convert to non-streaming fim method when one exist in continue. |
| 309 | 327 | yield (await resp.json()).predictions[0].content; |
| @@ -323,14 +341,14 @@ | ||
| 323 | 341 | ? this.geminiInstance.removeSystemMessage(messages) |
| 324 | 342 | : messages; |
| 325 | 343 | if (this.vertexProvider === "gemini") { |
| 326 | - yield* this.streamChatGemini(convertedMsgs, options); | |
| 344 | + yield* this.streamChatGemini(convertedMsgs, options, signal); | |
| 327 | 345 | } else if (this.vertexProvider === "mistral") { |
| 328 | - yield* this.StreamChatMistral(messages, options); | |
| 346 | + yield* this.StreamChatMistral(messages, options, signal); | |
| 329 | 347 | } else if (this.vertexProvider === "anthropic") { |
| 330 | - yield* this.StreamChatAnthropic(messages, options); | |
| 348 | + yield* this.StreamChatAnthropic(messages, options, signal); | |
| 331 | 349 | } else { |
| 332 | 350 | if (options.model.includes("bison")) { |
| 333 | - yield* this.streamChatBison(convertedMsgs, options); | |
| 351 | + yield* this.streamChatBison(convertedMsgs, options, signal); | |
| 334 | 352 | } else { |
| 335 | 353 | throw new Error(`Unsupported model: ${options.model}`); |
| 336 | 354 | } |
modified core/llm/llms/WatsonX.ts +37 −53
| @@ -1,15 +1,12 @@ | ||
| 1 | +import { streamResponse, streamSse } from "@continuedev/fetch"; | |
| 1 | 2 | import { |
| 2 | 3 | ChatMessage, |
| 3 | 4 | Chunk, |
| 4 | 5 | CompletionOptions, |
| 5 | 6 | LLMOptions, |
| 6 | 7 | } from "../../index.js"; |
| 7 | -import { | |
| 8 | - fromChatCompletionChunk, | |
| 9 | -} from "../openaiTypeConverters.js"; | |
| 10 | -import { renderChatMessage } from "../../util/messageContent.js"; | |
| 11 | 8 | import { BaseLLM } from "../index.js"; |
| 12 | -import { streamResponse, streamSse } from "../stream.js"; | |
| 9 | +import { fromChatCompletionChunk } from "../openaiTypeConverters.js"; | |
| 13 | 10 | |
| 14 | 11 | let watsonxToken = { |
| 15 | 12 | expiration: 0, |
| @@ -86,7 +83,7 @@ | ||
| 86 | 83 | } |
| 87 | 84 | |
| 88 | 85 | _getEndpoint(endpoint: string): string { |
| 89 | - return `${this.apiBase}/ml/v1/${this.deploymentId ? `deployments/${this.deploymentId}/` : ""}text/${endpoint}_stream?version=${this.apiVersion}` | |
| 86 | + return `${this.apiBase}/ml/v1/${this.deploymentId ? `deployments/${this.deploymentId}/` : ""}text/${endpoint}_stream?version=${this.apiVersion}`; | |
| 90 | 87 | } |
| 91 | 88 | |
| 92 | 89 | static providerName = "watsonx"; |
| @@ -182,7 +179,6 @@ | ||
| 182 | 179 | signal: AbortSignal, |
| 183 | 180 | options: CompletionOptions, |
| 184 | 181 | ): AsyncGenerator<string> { |
| 185 | - | |
| 186 | 182 | await this.updateWatsonxToken(); |
| 187 | 183 | |
| 188 | 184 | const stopSequences = options.stop?.slice(0, 6) ?? []; |
| @@ -214,44 +210,38 @@ | ||
| 214 | 210 | payload.project_id = this.projectId; |
| 215 | 211 | } |
| 216 | 212 | |
| 217 | - var response = await this.fetch(url, { | |
| 213 | + const response = await this.fetch(url, { | |
| 218 | 214 | method: "POST", |
| 219 | 215 | headers: headers, |
| 220 | 216 | body: JSON.stringify(payload), |
| 221 | 217 | signal, |
| 222 | 218 | }); |
| 223 | 219 | |
| 224 | - if (!response.ok || response.body === null) { | |
| 225 | - throw new Error( | |
| 226 | - "Something went wrong. No response received, check your connection", | |
| 227 | - ); | |
| 228 | - } else { | |
| 229 | - for await (const value of streamResponse(response)) { | |
| 230 | - const lines = value.split("\n"); | |
| 231 | - let generatedChunk = ""; | |
| 232 | - let generatedTextIndex = undefined; | |
| 233 | - lines.forEach((el: string) => { | |
| 234 | - // console.log(`${el}`); | |
| 235 | - if (el.startsWith("id:")) { | |
| 236 | - generatedTextIndex = parseInt(el.replace(/^id:\s+/, "")); | |
| 237 | - if (isNaN(generatedTextIndex)) { | |
| 238 | - console.error(`Unable to parse stream chunk ID: ${el}`); | |
| 239 | - } | |
| 240 | - } else if (el.startsWith("data:")) { | |
| 241 | - const dataStr = el.replace(/^data:\s+/, ""); | |
| 242 | - try { | |
| 243 | - const data = JSON.parse(dataStr); | |
| 244 | - data.results.forEach((result: any) => { | |
| 245 | - generatedChunk += result.generated_text || ""; | |
| 246 | - }); | |
| 247 | - } catch (e) { | |
| 248 | - // parsing error is expected with streaming response | |
| 249 | - // console.error(`Error parsing JSON string: ${dataStr}`, e); | |
| 250 | - } | |
| 220 | + for await (const value of streamResponse(response)) { | |
| 221 | + const lines = value.split("\n"); | |
| 222 | + let generatedChunk = ""; | |
| 223 | + let generatedTextIndex = undefined; | |
| 224 | + lines.forEach((el: string) => { | |
| 225 | + // console.log(`${el}`); | |
| 226 | + if (el.startsWith("id:")) { | |
| 227 | + generatedTextIndex = parseInt(el.replace(/^id:\s+/, "")); | |
| 228 | + if (isNaN(generatedTextIndex)) { | |
| 229 | + console.error(`Unable to parse stream chunk ID: ${el}`); | |
| 251 | 230 | } |
| 252 | - }); | |
| 253 | - yield generatedChunk | |
| 254 | - } | |
| 231 | + } else if (el.startsWith("data:")) { | |
| 232 | + const dataStr = el.replace(/^data:\s+/, ""); | |
| 233 | + try { | |
| 234 | + const data = JSON.parse(dataStr); | |
| 235 | + data.results.forEach((result: any) => { | |
| 236 | + generatedChunk += result.generated_text || ""; | |
| 237 | + }); | |
| 238 | + } catch (e) { | |
| 239 | + // parsing error is expected with streaming response | |
| 240 | + // console.error(`Error parsing JSON string: ${dataStr}`, e); | |
| 241 | + } | |
| 242 | + } | |
| 243 | + }); | |
| 244 | + yield generatedChunk; | |
| 255 | 245 | } |
| 256 | 246 | } |
| 257 | 247 | |
| @@ -267,7 +257,7 @@ | ||
| 267 | 257 | const headers = this._getHeaders(); |
| 268 | 258 | |
| 269 | 259 | const payload: any = { |
| 270 | - messages: messages, | |
| 260 | + messages: messages, | |
| 271 | 261 | max_tokens: options.maxTokens ?? 1024, |
| 272 | 262 | stop: stopSequences, |
| 273 | 263 | frequency_penalty: options.frequencyPenalty || 1, |
| @@ -281,36 +271,30 @@ | ||
| 281 | 271 | |
| 282 | 272 | if (!!options.temperature) { |
| 283 | 273 | payload.temperature = options.temperature; |
| 284 | - } | |
| 274 | + } | |
| 285 | 275 | if (!!options.topP) { |
| 286 | 276 | payload.top_p = options.topP; |
| 287 | 277 | } |
| 288 | 278 | if (!!options.tools) { |
| 289 | 279 | payload.tools = options.tools; |
| 290 | 280 | if (options.toolChoice) { |
| 291 | - payload.tool_choice = options.toolChoice; | |
| 281 | + payload.tool_choice = options.toolChoice; | |
| 292 | 282 | } else { |
| 293 | - payload.tool_choice_option = "auto"; | |
| 283 | + payload.tool_choice_option = "auto"; | |
| 294 | 284 | } |
| 295 | 285 | } |
| 296 | 286 | |
| 297 | - var response = await this.fetch(url, { | |
| 287 | + const response = await this.fetch(url, { | |
| 298 | 288 | method: "POST", |
| 299 | 289 | headers: headers, |
| 300 | 290 | body: JSON.stringify(payload), |
| 301 | 291 | signal, |
| 302 | 292 | }); |
| 303 | 293 | |
| 304 | - if (!response.ok || response.body === null) { | |
| 305 | - throw new Error( | |
| 306 | - "Something went wrong. No response received, check your connection", | |
| 307 | - ); | |
| 308 | - } else { | |
| 309 | - for await (const value of streamSse(response)) { | |
| 310 | - const chunk = fromChatCompletionChunk(value); | |
| 311 | - if (chunk) { | |
| 312 | - yield chunk; | |
| 313 | - } | |
| 294 | + for await (const value of streamSse(response)) { | |
| 295 | + const chunk = fromChatCompletionChunk(value); | |
| 296 | + if (chunk) { | |
| 297 | + yield chunk; | |
| 314 | 298 | } |
| 315 | 299 | } |
| 316 | 300 | } |
deleted core/llm/stream.test.ts +0 −72
| @@ -1,72 +0,0 @@ | ||
| 1 | -import { Readable } from "stream"; | |
| 2 | -import { streamSse } from "./stream"; | |
| 3 | - | |
| 4 | -function createMockResponse(sseLines: string[]): Response { | |
| 5 | - // Create a Readable stream that emits the SSE lines | |
| 6 | - const stream = new Readable({ | |
| 7 | - read() { | |
| 8 | - for (const line of sseLines) { | |
| 9 | - this.push(line + "\n\n"); | |
| 10 | - } | |
| 11 | - this.push(null); // End of stream | |
| 12 | - } | |
| 13 | - }) as any; | |
| 14 | - | |
| 15 | - // Minimal Response mock | |
| 16 | - return { | |
| 17 | - status: 200, | |
| 18 | - body: stream, | |
| 19 | - text: async () => "", | |
| 20 | - } as unknown as Response; | |
| 21 | -} | |
| 22 | - | |
| 23 | -describe("streamSse", () => { | |
| 24 | - it("yields parsed SSE data objects that ends with `data:[DONE]`", async () => { | |
| 25 | - const sseLines = [ | |
| 26 | - 'data: {"foo": "bar"}', | |
| 27 | - 'data: {"baz": 42}', | |
| 28 | - 'data:[DONE]' | |
| 29 | - ]; | |
| 30 | - const response = createMockResponse(sseLines); | |
| 31 | - | |
| 32 | - const results = []; | |
| 33 | - for await (const data of streamSse(response)) { | |
| 34 | - results.push(data); | |
| 35 | - } | |
| 36 | - | |
| 37 | - expect(results).toEqual([ | |
| 38 | - { foo: "bar" }, | |
| 39 | - { baz: 42 } | |
| 40 | - ]); | |
| 41 | - }); | |
| 42 | - | |
| 43 | - it("yields parsed SSE data objects that ends with `data: [DONE]` (with a space before [DONE]", async () => { | |
| 44 | - const sseLines = [ | |
| 45 | - 'data: {"foo": "bar"}', | |
| 46 | - 'data: {"baz": 42}', | |
| 47 | - 'data: [DONE]' | |
| 48 | - ]; | |
| 49 | - const response = createMockResponse(sseLines); | |
| 50 | - | |
| 51 | - const results = []; | |
| 52 | - for await (const data of streamSse(response)) { | |
| 53 | - results.push(data); | |
| 54 | - } | |
| 55 | - | |
| 56 | - expect(results).toEqual([ | |
| 57 | - { foo: "bar" }, | |
| 58 | - { baz: 42 } | |
| 59 | - ]); | |
| 60 | - }); | |
| 61 | - | |
| 62 | - it("throws on malformed JSON", async () => { | |
| 63 | - const sseLines = [ | |
| 64 | - 'data: {"foo": "bar"', | |
| 65 | - 'data:[DONE]' | |
| 66 | - ]; | |
| 67 | - const response = createMockResponse(sseLines); | |
| 68 | - | |
| 69 | - const iterator = streamSse(response)[Symbol.asyncIterator](); | |
| 70 | - await expect(iterator.next()).rejects.toThrow(/Malformed JSON/); | |
| 71 | - }); | |
| 72 | -}); |
modified core/llm/stream.ts +1 −1
| @@ -1,4 +1,4 @@ | ||
| 1 | -async function* toAsyncIterable( | |
| 1 | +export async function* toAsyncIterable( | |
| 2 | 2 | nodeReadable: NodeJS.ReadableStream, |
| 3 | 3 | ): AsyncGenerator<Uint8Array> { |
| 4 | 4 | for await (const chunk of nodeReadable) { |
modified core/llm/streamChat.ts +27 −35
| @@ -8,7 +8,7 @@ | ||
| 8 | 8 | |
| 9 | 9 | export async function* llmStreamChat( |
| 10 | 10 | configHandler: ConfigHandler, |
| 11 | - abortedMessageIds: Set<string>, | |
| 11 | + abortController: AbortController, | |
| 12 | 12 | msg: Message<ToCoreProtocol["llm/streamChat"][0]>, |
| 13 | 13 | ide: IDE, |
| 14 | 14 | messenger: IMessenger<ToCoreProtocol, FromCoreProtocol>, |
| @@ -75,52 +75,44 @@ | ||
| 75 | 75 | selectedCode, |
| 76 | 76 | config, |
| 77 | 77 | fetch: (url, init) => |
| 78 | - fetchwithRequestOptions(url, init, config.requestOptions), | |
| 78 | + fetchwithRequestOptions( | |
| 79 | + url, | |
| 80 | + { | |
| 81 | + ...init, | |
| 82 | + signal: abortController.signal, | |
| 83 | + }, | |
| 84 | + config.requestOptions, | |
| 85 | + ), | |
| 79 | 86 | completionOptions, |
| 80 | 87 | }); |
| 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(); | |
| 88 | + let next = await gen.next(); | |
| 89 | + while (!next.done) { | |
| 90 | + if (abortController.signal.aborted) { | |
| 91 | + next = await gen.return(errorPromptLog); | |
| 92 | + break; | |
| 103 | 93 | } |
| 104 | - if (!next.done) { | |
| 105 | - throw new Error("Will never happen"); | |
| 94 | + if (next.value) { | |
| 95 | + yield { | |
| 96 | + role: "assistant", | |
| 97 | + content: next.value, | |
| 98 | + }; | |
| 106 | 99 | } |
| 107 | - | |
| 108 | - return next.value; | |
| 109 | - } catch (e) { | |
| 110 | - throw e; | |
| 111 | - } finally { | |
| 112 | - clearInterval(checkActiveInterval); | |
| 100 | + next = await gen.next(); | |
| 113 | 101 | } |
| 102 | + if (!next.done) { | |
| 103 | + throw new Error("Will never happen"); | |
| 104 | + } | |
| 105 | + | |
| 106 | + return next.value; | |
| 114 | 107 | } else { |
| 115 | 108 | const gen = model.streamChat( |
| 116 | 109 | messages, |
| 117 | - new AbortController().signal, | |
| 110 | + abortController.signal, | |
| 118 | 111 | completionOptions, |
| 119 | 112 | ); |
| 120 | 113 | let next = await gen.next(); |
| 121 | 114 | while (!next.done) { |
| 122 | - if (abortedMessageIds.has(msg.messageId)) { | |
| 123 | - abortedMessageIds.delete(msg.messageId); | |
| 115 | + if (abortController.signal.aborted) { | |
| 124 | 116 | next = await gen.return(errorPromptLog); |
| 125 | 117 | break; |
| 126 | 118 | } |
modified core/package-lock.json +0 −0
Not shown: the file is larger than the hub shows, or this page holds as much as it shows. View the file.
modified core/package.json +2 −2
| @@ -49,9 +49,9 @@ | ||
| 49 | 49 | "@aws-sdk/credential-providers": "^3.778.0", |
| 50 | 50 | "@continuedev/config-types": "^1.0.13", |
| 51 | 51 | "@continuedev/config-yaml": "file:../packages/config-yaml", |
| 52 | - "@continuedev/fetch": "^1.0.6", | |
| 52 | + "@continuedev/fetch": "^1.0.10", | |
| 53 | 53 | "@continuedev/llm-info": "^1.0.8", |
| 54 | - "@continuedev/openai-adapters": "^1.0.19", | |
| 54 | + "@continuedev/openai-adapters": "^1.0.25", | |
| 55 | 55 | "@modelcontextprotocol/sdk": "^1.5.0", |
| 56 | 56 | "@mozilla/readability": "^0.5.0", |
| 57 | 57 | "@octokit/rest": "^20.1.1", |
modified core/protocol/core.ts +1 −0
| @@ -46,6 +46,7 @@ | ||
| 46 | 46 | // Special |
| 47 | 47 | ping: [string, string]; |
| 48 | 48 | abort: [undefined, void]; |
| 49 | + cancelApply: [undefined, void]; | |
| 49 | 50 | |
| 50 | 51 | // History |
| 51 | 52 | "history/list": [ListHistoryOptions, SessionMetadata[]]; |
added core/util/abortManager.ts +37 −0
| @@ -0,0 +1,37 @@ | ||
| 1 | +export class StreamAbortManager { | |
| 2 | + private static instance: StreamAbortManager; | |
| 3 | + private controllers: Map<string, AbortController>; | |
| 4 | + | |
| 5 | + private constructor() { | |
| 6 | + this.controllers = new Map(); | |
| 7 | + } | |
| 8 | + | |
| 9 | + public static getInstance(): StreamAbortManager { | |
| 10 | + if (!StreamAbortManager.instance) { | |
| 11 | + StreamAbortManager.instance = new StreamAbortManager(); | |
| 12 | + } | |
| 13 | + return StreamAbortManager.instance; | |
| 14 | + } | |
| 15 | + | |
| 16 | + public get(id: string): AbortController { | |
| 17 | + let controller = this.controllers.get(id); | |
| 18 | + if (!controller) { | |
| 19 | + controller = new AbortController(); | |
| 20 | + this.controllers.set(id, controller); | |
| 21 | + } | |
| 22 | + return controller; | |
| 23 | + } | |
| 24 | + | |
| 25 | + public abort(id: string): void { | |
| 26 | + const controller = this.controllers.get(id); | |
| 27 | + if (controller) { | |
| 28 | + controller.abort(); | |
| 29 | + this.controllers.delete(id); | |
| 30 | + } | |
| 31 | + } | |
| 32 | + | |
| 33 | + public clear(): void { | |
| 34 | + this.controllers.forEach((controller) => controller.abort()); | |
| 35 | + this.controllers.clear(); | |
| 36 | + } | |
| 37 | +} |
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/Diffs.kt +1 −1
| @@ -108,7 +108,7 @@ | ||
| 108 | 108 | ) |
| 109 | 109 | continuePluginService.ideProtocolClient?.deleteAtIndex(diffInfo.stepIndex) |
| 110 | 110 | continuePluginService.ideProtocolClient?.sendAcceptRejectDiff(false, diffInfo.stepIndex) |
| 111 | - | |
| 111 | + continuePluginService.coreMessenger?.request("cancelApply", null, null) {} | |
| 112 | 112 | cleanUpFile(file) |
| 113 | 113 | } |
| 114 | 114 |
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/DiffStreamHandler.kt +2 −2
| @@ -111,7 +111,8 @@ | ||
| 111 | 111 | suffix = suffix, |
| 112 | 112 | language = virtualFile?.fileType?.name, |
| 113 | 113 | modelTitle = modelTitle, |
| 114 | - includeRulesInSystemMessage = includeRulesInSystemMessage | |
| 114 | + includeRulesInSystemMessage = includeRulesInSystemMessage, | |
| 115 | + fileUri = virtualFile?.url | |
| 115 | 116 | ), |
| 116 | 117 | null |
| 117 | 118 | ) { response -> |
| @@ -289,7 +290,6 @@ | ||
| 289 | 290 | } |
| 290 | 291 | } |
| 291 | 292 | |
| 292 | - | |
| 293 | 293 | private fun handleFinishedResponse() { |
| 294 | 294 | ApplicationManager.getApplication().invokeLater { |
| 295 | 295 | // Since we only call onLastDiffLine() when we reach a "same" line, we need to handle the case where |
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/types.kt +2 −1
| @@ -244,7 +244,8 @@ | ||
| 244 | 244 | val input: String, |
| 245 | 245 | val language: String?, |
| 246 | 246 | val modelTitle: String?, |
| 247 | - val includeRulesInSystemMessage: Boolean | |
| 247 | + val includeRulesInSystemMessage: Boolean, | |
| 248 | + val fileUri: String? | |
| 248 | 249 | ) |
| 249 | 250 | |
| 250 | 251 | data class AcceptOrRejectDiffPayload( |
modified extensions/vscode/package-lock.json +0 −0
Not shown: the file is larger than the hub shows, or this page holds as much as it shows. View the file.
modified extensions/vscode/package.json +1 −1
| @@ -661,7 +661,7 @@ | ||
| 661 | 661 | }, |
| 662 | 662 | "dependencies": { |
| 663 | 663 | "@continuedev/config-types": "^1.0.14", |
| 664 | - "@continuedev/fetch": "^1.0.3", | |
| 664 | + "@continuedev/fetch": "^1.0.10", | |
| 665 | 665 | "@electron/rebuild": "^3.2.10", |
| 666 | 666 | "@reduxjs/toolkit": "^1.9.3", |
| 667 | 667 | "@types/node-fetch": "^2.6.11", |
modified extensions/vscode/src/commands.ts +14 −4
| @@ -97,6 +97,7 @@ | ||
| 97 | 97 | action: "accept" | "reject", |
| 98 | 98 | sidebar: ContinueGUIWebviewViewProvider, |
| 99 | 99 | ide: VsCodeIde, |
| 100 | + core: Core, | |
| 100 | 101 | verticalDiffManager: VerticalDiffManager, |
| 101 | 102 | newFileUri?: string, |
| 102 | 103 | streamId?: string, |
| @@ -121,6 +122,9 @@ | ||
| 121 | 122 | |
| 122 | 123 | // Clear vertical diffs depending on action |
| 123 | 124 | verticalDiffManager.clearForfileUri(newOrCurrentUri, action === "accept"); |
| 125 | + if (action === "reject") { | |
| 126 | + core.invoke("cancelApply", undefined); | |
| 127 | + } | |
| 124 | 128 | |
| 125 | 129 | if (streamId) { |
| 126 | 130 | const fileContent = await ide.readFile(newOrCurrentUri); |
| @@ -236,18 +240,20 @@ | ||
| 236 | 240 | "accept", |
| 237 | 241 | sidebar, |
| 238 | 242 | ide, |
| 243 | + core, | |
| 239 | 244 | verticalDiffManager, |
| 240 | 245 | newFileUri, |
| 241 | 246 | streamId, |
| 242 | 247 | ), |
| 243 | 248 | |
| 244 | - "continue.rejectDiff": async (newFilepath?: string, streamId?: string) => | |
| 249 | + "continue.rejectDiff": async (newFileUri?: string, streamId?: string) => | |
| 245 | 250 | processDiff( |
| 246 | 251 | "reject", |
| 247 | 252 | sidebar, |
| 248 | 253 | ide, |
| 254 | + core, | |
| 249 | 255 | verticalDiffManager, |
| 250 | - newFilepath, | |
| 256 | + newFileUri, | |
| 251 | 257 | streamId, |
| 252 | 258 | ), |
| 253 | 259 | "continue.acceptVerticalDiffBlock": (fileUri?: string, index?: number) => { |
| @@ -573,11 +579,15 @@ | ||
| 573 | 579 | await addEntireFileToContext( |
| 574 | 580 | vscode.Uri.parse(fileUri), |
| 575 | 581 | sidebar.webviewProtocol, |
| 576 | - ide.ideUtils | |
| 582 | + ide.ideUtils, | |
| 577 | 583 | ); |
| 578 | 584 | } |
| 579 | 585 | } else { |
| 580 | - await addEntireFileToContext(uri, sidebar.webviewProtocol, ide.ideUtils); | |
| 586 | + await addEntireFileToContext( | |
| 587 | + uri, | |
| 588 | + sidebar.webviewProtocol, | |
| 589 | + ide.ideUtils, | |
| 590 | + ); | |
| 581 | 591 | } |
| 582 | 592 | } |
| 583 | 593 | }, |
modified extensions/vscode/src/diff/vertical/manager.ts +3 −4
| @@ -1,5 +1,4 @@ | ||
| 1 | 1 | import { ChatMessage, DiffLine, ILLM, RuleWithSource } from "core"; |
| 2 | -import { ConfigHandler } from "core/config/ConfigHandler"; | |
| 3 | 2 | import { streamDiffLines } from "core/edit/streamDiffLines"; |
| 4 | 3 | import { pruneLinesFromBottom, pruneLinesFromTop } from "core/llm/countTokens"; |
| 5 | 4 | import { getMarkdownLanguageTagForFile } from "core/util"; |
| @@ -31,7 +30,6 @@ | ||
| 31 | 30 | logDiffs: DiffLine[] | undefined; |
| 32 | 31 | |
| 33 | 32 | constructor( |
| 34 | - private readonly configHandler: ConfigHandler, | |
| 35 | 33 | private readonly webviewProtocol: VsCodeWebviewProtocol, |
| 36 | 34 | private readonly editDecorationManager: EditDecorationManager, |
| 37 | 35 | ) { |
| @@ -43,12 +41,12 @@ | ||
| 43 | 41 | startLine: number, |
| 44 | 42 | endLine: number, |
| 45 | 43 | options: VerticalDiffHandlerOptions, |
| 46 | - ) { | |
| 44 | + ): VerticalDiffHandler | undefined { | |
| 47 | 45 | if (this.fileUriToHandler.has(fileUri)) { |
| 48 | 46 | this.fileUriToHandler.get(fileUri)?.clear(false); |
| 49 | 47 | this.fileUriToHandler.delete(fileUri); |
| 50 | 48 | } |
| 51 | - const editor = vscode.window.activeTextEditor; // TODO | |
| 49 | + const editor = vscode.window.activeTextEditor; // TODO might cause issues if user switches files | |
| 52 | 50 | if (editor && URI.equal(editor.document.uri.toString(), fileUri)) { |
| 53 | 51 | const handler = new VerticalDiffHandler( |
| 54 | 52 | startLine, |
| @@ -453,6 +451,7 @@ | ||
| 453 | 451 | language: getMarkdownLanguageTagForFile(fileUri), |
| 454 | 452 | onlyOneInsertion: !!onlyOneInsertion, |
| 455 | 453 | overridePrompt, |
| 454 | + abortControllerId: fileUri, | |
| 456 | 455 | }); |
| 457 | 456 | |
| 458 | 457 | for await (const line of stream) { |
modified extensions/vscode/src/extension/VsCodeExtension.ts +0 −1
| @@ -130,7 +130,6 @@ | ||
| 130 | 130 | this.configHandler.loadConfig(); |
| 131 | 131 | |
| 132 | 132 | this.verticalDiffManager = new VerticalDiffManager( |
| 133 | - this.configHandler, | |
| 134 | 133 | this.sidebar.webviewProtocol, |
| 135 | 134 | this.editDecorationManager, |
| 136 | 135 | ); |
modified gui/package-lock.json +0 −0
Not shown: the file is larger than the hub shows, or this page holds as much as it shows. View the file.
modified gui/src/components/mainInput/Lump/LumpToolbar.tsx +36 −1
| @@ -1,4 +1,4 @@ | ||
| 1 | -import { useContext, useEffect } from "react"; | |
| 1 | +import { useContext, useEffect, useMemo } from "react"; | |
| 2 | 2 | import { useSelector } from "react-redux"; |
| 3 | 3 | import styled from "styled-components"; |
| 4 | 4 | import { AnimatedEllipsis } from "../.."; |
| @@ -40,12 +40,27 @@ | ||
| 40 | 40 | ); |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | +function ApplyingIndicator() { | |
| 44 | + return ( | |
| 45 | + <div className="text-xs text-gray-400"> | |
| 46 | + <span>Applying</span> | |
| 47 | + <AnimatedEllipsis /> | |
| 48 | + </div> | |
| 49 | + ); | |
| 50 | +} | |
| 51 | + | |
| 43 | 52 | export function LumpToolbar() { |
| 44 | 53 | const dispatch = useAppDispatch(); |
| 45 | 54 | const ideMessenger = useContext(IdeMessengerContext); |
| 46 | 55 | const ttsActive = useAppSelector((state) => state.ui.ttsActive); |
| 47 | 56 | const isStreaming = useAppSelector((state) => state.session.isStreaming); |
| 48 | 57 | const jetbrains = isJetBrains(); |
| 58 | + const applyStates = useAppSelector( | |
| 59 | + (store) => store.session.codeBlockApplyStates.states, | |
| 60 | + ); | |
| 61 | + const isApplying = useMemo(() => { | |
| 62 | + return applyStates.find((state) => state.status === "streaming"); | |
| 63 | + }, [applyStates]); | |
| 49 | 64 | |
| 50 | 65 | const toolCallState = useSelector(selectCurrentToolCall); |
| 51 | 66 | |
| @@ -89,6 +104,26 @@ | ||
| 89 | 104 | ); |
| 90 | 105 | } |
| 91 | 106 | |
| 107 | + if (isApplying) { | |
| 108 | + return ( | |
| 109 | + <Container> | |
| 110 | + <ApplyingIndicator /> | |
| 111 | + <StopButton | |
| 112 | + className="text-gray-400" | |
| 113 | + onClick={() => { | |
| 114 | + // Note that this will NOT stop generation but once apply is cancelled will show the Generating/cancel option | |
| 115 | + // Apply is prioritized because it can be more catastrophic | |
| 116 | + // Intentional to be more WYSIWYG for now | |
| 117 | + ideMessenger.post("cancelApply", undefined); | |
| 118 | + }} | |
| 119 | + > | |
| 120 | + {/* JetBrains overrides cmd+backspace, so we have to use another shortcut */} | |
| 121 | + {jetbrains ? getAltKeyLabel() : getMetaKeyLabel()} ⌫ Cancel | |
| 122 | + </StopButton> | |
| 123 | + </Container> | |
| 124 | + ); | |
| 125 | + } | |
| 126 | + | |
| 92 | 127 | if (isStreaming) { |
| 93 | 128 | return ( |
| 94 | 129 | <Container> |
modified gui/src/pages/gui/Chat.tsx +1 −0
| @@ -144,6 +144,7 @@ | ||
| 144 | 144 | !e.shiftKey |
| 145 | 145 | ) { |
| 146 | 146 | dispatch(cancelStream()); |
| 147 | + ideMessenger.post("cancelApply", undefined); // just always cancel, if not in applying won't matter | |
| 147 | 148 | } |
| 148 | 149 | }; |
| 149 | 150 | window.addEventListener("keydown", listener); |
modified manual-testing-sandbox/AdvancedPage.tsx +1 −4
| @@ -13,10 +13,7 @@ | ||
| 13 | 13 | className={`${isDarkMode ? "bg-gray-800 text-white" : "bg-white text-black"} min-h-screen p-4`} |
| 14 | 14 | > |
| 15 | 15 | <header className="mb-4 flex items-center justify-between border-b-2 pb-4"> |
| 16 | - I apologize, but there seems to be a misunderstanding. The user's | |
| 17 | - request doesn't appear to be related to rewriting the code section you | |
| 18 | - provided. The request mentions editing a readme.md file, which is not | |
| 19 | - part of the React component code you've shown. | |
| 16 | + {" "} | |
| 20 | 17 | <button |
| 21 | 18 | className="rounded bg-blue-500 px-4 py-2 text-white" |
| 22 | 19 | onClick={toggleDarkMode} |
added packages/fetch/.gitignore +1 −0
| @@ -0,0 +1,1 @@ | ||
| 1 | +coverage/ | |
| No newline at the end of the file |
added packages/fetch/jest.globals.d.ts +14 −0
| @@ -0,0 +1,14 @@ | ||
| 1 | +// This file declares the Jest globals for TypeScript | |
| 2 | +import '@jest/globals'; | |
| 3 | + | |
| 4 | +declare global { | |
| 5 | + const describe: typeof import('@jest/globals').describe; | |
| 6 | + const expect: typeof import('@jest/globals').expect; | |
| 7 | + const it: typeof import('@jest/globals').it; | |
| 8 | + const test: typeof import('@jest/globals').test; | |
| 9 | + const beforeAll: typeof import('@jest/globals').beforeAll; | |
| 10 | + const afterAll: typeof import('@jest/globals').afterAll; | |
| 11 | + const beforeEach: typeof import('@jest/globals').beforeEach; | |
| 12 | + const afterEach: typeof import('@jest/globals').afterEach; | |
| 13 | + const jest: typeof import('@jest/globals').jest; | |
| 14 | +} | |
| No newline at the end of the file |
modified packages/fetch/package-lock.json +2 −2
| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | { |
| 2 | 2 | "name": "@continuedev/fetch", |
| 3 | - "version": "1.0.6", | |
| 3 | + "version": "1.0.9", | |
| 4 | 4 | "lockfileVersion": 3, |
| 5 | 5 | "requires": true, |
| 6 | 6 | "packages": { |
| 7 | 7 | "": { |
| 8 | 8 | "name": "@continuedev/fetch", |
| 9 | - "version": "1.0.6", | |
| 9 | + "version": "1.0.9", | |
| 10 | 10 | "license": "Apache-2.0", |
| 11 | 11 | "dependencies": { |
| 12 | 12 | "@continuedev/config-types": "^1.0.5", |
modified packages/fetch/package.json +1 −1
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | { |
| 2 | 2 | "name": "@continuedev/fetch", |
| 3 | - "version": "1.0.9", | |
| 3 | + "version": "1.0.10", | |
| 4 | 4 | "description": "", |
| 5 | 5 | "main": "dist/index.js", |
| 6 | 6 | "types": "dist/index.d.ts", |
modified packages/fetch/src/stream.test.ts +63 −3
| @@ -1,6 +1,66 @@ | ||
| 1 | -// The parseDataLine function is not exported, so we need to import it | |
| 2 | -// indirectly by re-exporting it for testing purposes | |
| 3 | -import { parseDataLine } from "./stream.js"; | |
| 1 | +import { Readable } from "stream"; | |
| 2 | +import { parseDataLine, streamSse } from "./stream.js"; | |
| 3 | + | |
| 4 | +function createMockResponse(sseLines: string[]): Response { | |
| 5 | + // Create a Readable stream that emits the SSE lines | |
| 6 | + const stream = new Readable({ | |
| 7 | + read() { | |
| 8 | + for (const line of sseLines) { | |
| 9 | + this.push(line + "\n\n"); | |
| 10 | + } | |
| 11 | + this.push(null); // End of stream | |
| 12 | + }, | |
| 13 | + }) as any; | |
| 14 | + | |
| 15 | + // Minimal Response mock | |
| 16 | + return { | |
| 17 | + status: 200, | |
| 18 | + body: stream, | |
| 19 | + text: async () => "", | |
| 20 | + } as unknown as Response; | |
| 21 | +} | |
| 22 | + | |
| 23 | +describe("streamSse", () => { | |
| 24 | + it("yields parsed SSE data objects that ends with `data:[DONE]`", async () => { | |
| 25 | + const sseLines = [ | |
| 26 | + 'data: {"foo": "bar"}', | |
| 27 | + 'data: {"baz": 42}', | |
| 28 | + "data:[DONE]", | |
| 29 | + ]; | |
| 30 | + const response = createMockResponse(sseLines); | |
| 31 | + | |
| 32 | + const results = []; | |
| 33 | + for await (const data of streamSse(response)) { | |
| 34 | + results.push(data); | |
| 35 | + } | |
| 36 | + | |
| 37 | + expect(results).toEqual([{ foo: "bar" }, { baz: 42 }]); | |
| 38 | + }); | |
| 39 | + | |
| 40 | + it("yields parsed SSE data objects that ends with `data: [DONE]` (with a space before [DONE]", async () => { | |
| 41 | + const sseLines = [ | |
| 42 | + 'data: {"foo": "bar"}', | |
| 43 | + 'data: {"baz": 42}', | |
| 44 | + "data: [DONE]", | |
| 45 | + ]; | |
| 46 | + const response = createMockResponse(sseLines); | |
| 47 | + | |
| 48 | + const results = []; | |
| 49 | + for await (const data of streamSse(response)) { | |
| 50 | + results.push(data); | |
| 51 | + } | |
| 52 | + | |
| 53 | + expect(results).toEqual([{ foo: "bar" }, { baz: 42 }]); | |
| 54 | + }); | |
| 55 | + | |
| 56 | + it("throws on malformed JSON", async () => { | |
| 57 | + const sseLines = ['data: {"foo": "bar"', "data:[DONE]"]; | |
| 58 | + const response = createMockResponse(sseLines); | |
| 59 | + | |
| 60 | + const iterator = streamSse(response)[Symbol.asyncIterator](); | |
| 61 | + await expect(iterator.next()).rejects.toThrow(/Malformed JSON/); | |
| 62 | + }); | |
| 63 | +}); | |
| 4 | 64 | |
| 5 | 65 | describe("parseDataLine", () => { |
| 6 | 66 | test("parseDataLine should parse valid JSON data with 'data: ' prefix", () => { |
modified packages/fetch/src/stream.ts +2 −1
| @@ -2,7 +2,7 @@ | ||
| 2 | 2 | nodeReadable: NodeJS.ReadableStream, |
| 3 | 3 | ): AsyncGenerator<Uint8Array> { |
| 4 | 4 | for await (const chunk of nodeReadable) { |
| 5 | - // @ts-expect-error | |
| 5 | + // @ts-ignore | |
| 6 | 6 | yield chunk as Uint8Array; |
| 7 | 7 | } |
| 8 | 8 | } |
| @@ -13,6 +13,7 @@ | ||
| 13 | 13 | if (response.status === 499) { |
| 14 | 14 | return; // In case of client-side cancellation, just return |
| 15 | 15 | } |
| 16 | + | |
| 16 | 17 | if (response.status !== 200) { |
| 17 | 18 | throw new Error(await response.text()); |
| 18 | 19 | } |
modified packages/fetch/tsconfig.json +1 −1
| @@ -19,5 +19,5 @@ | ||
| 19 | 19 | "declaration": true |
| 20 | 20 | // "sourceMap": true |
| 21 | 21 | }, |
| 22 | - "include": ["src/**/*"] | |
| 22 | + "include": ["src/**/*", "jest.globals.d.ts"] | |
| 23 | 23 | } |
modified packages/openai-adapters/package-lock.json +6 −6
| @@ -1,18 +1,18 @@ | ||
| 1 | 1 | { |
| 2 | 2 | "name": "@continuedev/openai-adapters", |
| 3 | - "version": "1.0.21", | |
| 3 | + "version": "1.0.22", | |
| 4 | 4 | "lockfileVersion": 3, |
| 5 | 5 | "requires": true, |
| 6 | 6 | "packages": { |
| 7 | 7 | "": { |
| 8 | 8 | "name": "@continuedev/openai-adapters", |
| 9 | - "version": "1.0.21", | |
| 9 | + "version": "1.0.22", | |
| 10 | 10 | "license": "Apache-2.0", |
| 11 | 11 | "dependencies": { |
| 12 | 12 | "@azure/openai": "^1.0.0-beta.12", |
| 13 | 13 | "@continuedev/config-types": "^1.0.5", |
| 14 | 14 | "@continuedev/config-yaml": "^1.0.51", |
| 15 | - "@continuedev/fetch": "^1.0.3", | |
| 15 | + "@continuedev/fetch": "^1.0.10", | |
| 16 | 16 | "dotenv": "^16.5.0", |
| 17 | 17 | "json-schema": "^0.4.0", |
| 18 | 18 | "node-fetch": "^3.3.2", |
| @@ -716,9 +716,9 @@ | ||
| 716 | 716 | } |
| 717 | 717 | }, |
| 718 | 718 | "node_modules/@continuedev/fetch": { |
| 719 | - "version": "1.0.3", | |
| 720 | - "resolved": "https://registry.npmjs.org/@continuedev/fetch/-/fetch-1.0.3.tgz", | |
| 721 | - "integrity": "sha512-cGNCUEVnDfC+3O/n1PV+oYhS5yZRLR1Q7Njg929kVbA8kRHe5a3VdKINfFzWdKprOU4wipsXSliciZHCpbJJ9g==", | |
| 719 | + "version": "1.0.10", | |
| 720 | + "resolved": "https://registry.npmjs.org/@continuedev/fetch/-/fetch-1.0.10.tgz", | |
| 721 | + "integrity": "sha512-RBUXtm/VDz+9Itgw+CxrHXwVXoHCAedGzPOgA7gS5HZ9lObaeCJgEfzmfbln1ocFsfqSFmn5X5wSE7DHgfxpVA==", | |
| 722 | 722 | "dependencies": { |
| 723 | 723 | "@continuedev/config-types": "^1.0.5", |
| 724 | 724 | "follow-redirects": "^1.15.6", |
modified packages/openai-adapters/package.json +1 −1
| @@ -15,7 +15,7 @@ | ||
| 15 | 15 | "@azure/openai": "^1.0.0-beta.12", |
| 16 | 16 | "@continuedev/config-types": "^1.0.5", |
| 17 | 17 | "@continuedev/config-yaml": "^1.0.51", |
| 18 | - "@continuedev/fetch": "^1.0.3", | |
| 18 | + "@continuedev/fetch": "^1.0.10", | |
| 19 | 19 | "dotenv": "^16.5.0", |
| 20 | 20 | "json-schema": "^0.4.0", |
| 21 | 21 | "node-fetch": "^3.3.2", |
modified packages/openai-adapters/src/apis/Anthropic.ts +5 −0
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | import { ChatCompletionCreateParams } from "openai/src/resources/index.js"; |
| 13 | 13 | import { AnthropicConfig } from "../types.js"; |
| 14 | 14 | import { chatChunk, chatChunkFromDelta, customFetch } from "../util.js"; |
| 15 | +import { EMPTY_CHAT_COMPLETION } from "../util/emptyChatCompletion.js"; | |
| 15 | 16 | import { |
| 16 | 17 | BaseLlmApi, |
| 17 | 18 | CreateRerankResponse, |
| @@ -157,6 +158,10 @@ | ||
| 157 | 158 | }, |
| 158 | 159 | ); |
| 159 | 160 | |
| 161 | + if (response.status === 499) { | |
| 162 | + return EMPTY_CHAT_COMPLETION; | |
| 163 | + } | |
| 164 | + | |
| 160 | 165 | const completion = (await response.json()) as any; |
| 161 | 166 | return { |
| 162 | 167 | id: completion.id, |
modified packages/openai-adapters/src/apis/Cohere.ts +5 −0
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | } from "openai/resources/index"; |
| 16 | 16 | import { CohereConfig } from "../types.js"; |
| 17 | 17 | import { chatCompletion, customFetch, embedding } from "../util.js"; |
| 18 | +import { EMPTY_CHAT_COMPLETION } from "../util/emptyChatCompletion.js"; | |
| 18 | 19 | import { |
| 19 | 20 | BaseLlmApi, |
| 20 | 21 | CreateRerankResponse, |
| @@ -77,6 +78,10 @@ | ||
| 77 | 78 | }, |
| 78 | 79 | ); |
| 79 | 80 | |
| 81 | + if (resp.status === 499) { | |
| 82 | + return EMPTY_CHAT_COMPLETION; | |
| 83 | + } | |
| 84 | + | |
| 80 | 85 | const data = (await resp.json()) as any; |
| 81 | 86 | const { input_tokens, output_tokens } = data.meta.tokens; |
| 82 | 87 | return chatCompletion({ |
modified packages/openai-adapters/src/apis/Relace.ts +4 −0
| @@ -105,6 +105,10 @@ | ||
| 105 | 105 | signal, |
| 106 | 106 | }); |
| 107 | 107 | |
| 108 | + if (response.status === 499) { | |
| 109 | + return; // Aborted by user | |
| 110 | + } | |
| 111 | + | |
| 108 | 112 | const result = (await response.json()) as any; |
| 109 | 113 | const mergedCode = result.mergedCode; |
| 110 | 114 |
added packages/openai-adapters/src/util/emptyChatCompletion.ts +21 −0
| @@ -0,0 +1,21 @@ | ||
| 1 | +import { ChatCompletion } from "openai/resources/index"; | |
| 2 | + | |
| 3 | +export const EMPTY_CHAT_COMPLETION: ChatCompletion = { | |
| 4 | + choices: [ | |
| 5 | + { | |
| 6 | + finish_reason: "stop", | |
| 7 | + index: 0, | |
| 8 | + logprobs: null, | |
| 9 | + message: { | |
| 10 | + content: null, | |
| 11 | + role: "assistant", | |
| 12 | + refusal: null, | |
| 13 | + }, | |
| 14 | + }, | |
| 15 | + ], | |
| 16 | + usage: undefined, | |
| 17 | + created: Date.now(), | |
| 18 | + id: "", | |
| 19 | + model: "UNSPECIFIED", | |
| 20 | + object: "chat.completion", | |
| 21 | +}; |