From 35958b225df5a2bf28e1c8b093b2616fdc659ab0 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Fri, 16 May 2025 10:33:17 -0700 Subject: [PATCH] use fetch package for streaming utils --- core/commands/slash/http.ts | 2 +- core/llm/llms/Anthropic.ts | 2 +- core/llm/llms/Cloudflare.ts | 2 +- core/llm/llms/Cohere.ts | 2 +- core/llm/llms/Deepseek.ts | 2 +- core/llm/llms/FreeTrial.ts | 2 +- core/llm/llms/Gemini.ts | 2 +- core/llm/llms/HuggingFaceInferenceAPI.ts | 2 +- core/llm/llms/HuggingFaceTGI.ts | 2 +- core/llm/llms/Inception.ts | 2 +- core/llm/llms/LlamaCpp.ts | 2 +- core/llm/llms/Moonshot.ts | 2 +- core/llm/llms/Ollama.ts | 2 +- core/llm/llms/OpenAI.ts | 2 +- core/llm/llms/SiliconFlow.ts | 4 +- core/llm/llms/VertexAI.ts | 2 +- core/llm/llms/WatsonX.ts | 2 +- core/llm/stream.ts | 125 ----------------------- 18 files changed, 18 insertions(+), 143 deletions(-) delete mode 100644 core/llm/stream.ts diff --git a/core/commands/slash/http.ts b/core/commands/slash/http.ts index 7a662561c..e92c7c3f1 100644 --- a/core/commands/slash/http.ts +++ b/core/commands/slash/http.ts @@ -1,6 +1,6 @@ +import { streamResponse } from "@continuedev/fetch"; import { SlashCommand } from "../../index.js"; import { removeQuotesAndEscapes } from "../../util/index.js"; -import { streamResponse } from "../../llm/stream.js"; const HttpSlashCommand: SlashCommand = { name: "http", diff --git a/core/llm/llms/Anthropic.ts b/core/llm/llms/Anthropic.ts index e194f5ddc..b5906590a 100644 --- a/core/llm/llms/Anthropic.ts +++ b/core/llm/llms/Anthropic.ts @@ -1,7 +1,7 @@ +import { streamSse } from "@continuedev/fetch"; import { ChatMessage, CompletionOptions, LLMOptions } from "../../index.js"; import { renderChatMessage, stripImages } from "../../util/messageContent.js"; import { BaseLLM } from "../index.js"; -import { streamSse } from "../stream.js"; class Anthropic extends BaseLLM { static providerName = "anthropic"; diff --git a/core/llm/llms/Cloudflare.ts b/core/llm/llms/Cloudflare.ts index 8d18c4e3a..cba4204ef 100644 --- a/core/llm/llms/Cloudflare.ts +++ b/core/llm/llms/Cloudflare.ts @@ -1,7 +1,7 @@ +import { streamSse } from "@continuedev/fetch"; import { ChatMessage, CompletionOptions } from "../../index.js"; import { renderChatMessage } from "../../util/messageContent.js"; import { BaseLLM } from "../index.js"; -import { streamSse } from "../stream.js"; export default class Cloudflare extends BaseLLM { static providerName = "cloudflare"; diff --git a/core/llm/llms/Cohere.ts b/core/llm/llms/Cohere.ts index 17b5e4352..b4d2c3b41 100644 --- a/core/llm/llms/Cohere.ts +++ b/core/llm/llms/Cohere.ts @@ -1,3 +1,4 @@ +import { streamJSON } from "@continuedev/fetch"; import { ChatMessage, Chunk, @@ -6,7 +7,6 @@ import { } from "../../index.js"; import { renderChatMessage, stripImages } from "../../util/messageContent.js"; import { BaseLLM } from "../index.js"; -import { streamJSON } from "../stream.js"; class Cohere extends BaseLLM { static providerName = "cohere"; diff --git a/core/llm/llms/Deepseek.ts b/core/llm/llms/Deepseek.ts index c1453d0db..305d177e7 100644 --- a/core/llm/llms/Deepseek.ts +++ b/core/llm/llms/Deepseek.ts @@ -1,5 +1,5 @@ +import { streamSse } from "@continuedev/fetch"; import { CompletionOptions, LLMOptions } from "../../index.js"; -import { streamSse } from "../stream.js"; import { osModelsEditPrompt } from "../templates/edit.js"; import OpenAI from "./OpenAI.js"; diff --git a/core/llm/llms/FreeTrial.ts b/core/llm/llms/FreeTrial.ts index ab69aff70..6ca040db0 100644 --- a/core/llm/llms/FreeTrial.ts +++ b/core/llm/llms/FreeTrial.ts @@ -1,3 +1,4 @@ +import { streamResponse } from "@continuedev/fetch"; import { TRIAL_FIM_MODEL } from "../../config/onboarding.js"; import { getHeaders } from "../../continueServer/stubs/headers.js"; import { TRIAL_PROXY_URL } from "../../control-plane/client.js"; @@ -8,7 +9,6 @@ import { LLMOptions, } from "../../index.js"; import { BaseLLM } from "../index.js"; -import { streamResponse } from "../stream.js"; class FreeTrial extends BaseLLM { static providerName = "free-trial"; diff --git a/core/llm/llms/Gemini.ts b/core/llm/llms/Gemini.ts index 3a9a6145a..044ee6b71 100644 --- a/core/llm/llms/Gemini.ts +++ b/core/llm/llms/Gemini.ts @@ -1,3 +1,4 @@ +import { streamResponse } from "@continuedev/fetch"; import { v4 as uuidv4 } from "uuid"; import { AssistantChatMessage, @@ -10,7 +11,6 @@ import { } from "../../index.js"; import { renderChatMessage, stripImages } from "../../util/messageContent.js"; import { BaseLLM } from "../index.js"; -import { streamResponse } from "../stream.js"; import { GeminiChatContent, GeminiChatContentPart, diff --git a/core/llm/llms/HuggingFaceInferenceAPI.ts b/core/llm/llms/HuggingFaceInferenceAPI.ts index 40e67b3e5..208f6fd49 100644 --- a/core/llm/llms/HuggingFaceInferenceAPI.ts +++ b/core/llm/llms/HuggingFaceInferenceAPI.ts @@ -1,6 +1,6 @@ +import { streamSse } from "@continuedev/fetch"; import { CompletionOptions } from "../../index.js"; import { BaseLLM } from "../index.js"; -import { streamSse } from "../stream.js"; class HuggingFaceInferenceAPI extends BaseLLM { static providerName = "huggingface-inference-api"; diff --git a/core/llm/llms/HuggingFaceTGI.ts b/core/llm/llms/HuggingFaceTGI.ts index c9812cca3..8a86634c8 100644 --- a/core/llm/llms/HuggingFaceTGI.ts +++ b/core/llm/llms/HuggingFaceTGI.ts @@ -1,6 +1,6 @@ +import { streamSse } from "@continuedev/fetch"; import { CompletionOptions, LLMOptions } from "../../index.js"; import { BaseLLM } from "../index.js"; -import { streamSse } from "../stream.js"; class HuggingFaceTGI extends BaseLLM { private static MAX_STOP_TOKENS = 4; diff --git a/core/llm/llms/Inception.ts b/core/llm/llms/Inception.ts index 2d75ca86b..385dea23c 100644 --- a/core/llm/llms/Inception.ts +++ b/core/llm/llms/Inception.ts @@ -1,5 +1,5 @@ +import { streamSse } from "@continuedev/fetch"; import { CompletionOptions, LLMOptions } from "../../index.js"; -import { streamSse } from "../stream.js"; import OpenAI from "./OpenAI.js"; diff --git a/core/llm/llms/LlamaCpp.ts b/core/llm/llms/LlamaCpp.ts index 4833405ae..2d849f2d7 100644 --- a/core/llm/llms/LlamaCpp.ts +++ b/core/llm/llms/LlamaCpp.ts @@ -1,6 +1,6 @@ +import { streamSse } from "@continuedev/fetch"; import { CompletionOptions, LLMOptions } from "../../index.js"; import { BaseLLM } from "../index.js"; -import { streamSse } from "../stream.js"; class LlamaCpp extends BaseLLM { static providerName = "llama.cpp"; diff --git a/core/llm/llms/Moonshot.ts b/core/llm/llms/Moonshot.ts index 732ce81cd..2235b5ac2 100644 --- a/core/llm/llms/Moonshot.ts +++ b/core/llm/llms/Moonshot.ts @@ -1,5 +1,5 @@ +import { streamSse } from "@continuedev/fetch"; import { CompletionOptions, LLMOptions } from "../../index.js"; -import { streamSse } from "../stream.js"; import { osModelsEditPrompt } from "../templates/edit.js"; import OpenAI from "./OpenAI.js"; diff --git a/core/llm/llms/Ollama.ts b/core/llm/llms/Ollama.ts index ccfdbe78a..16d0e9542 100644 --- a/core/llm/llms/Ollama.ts +++ b/core/llm/llms/Ollama.ts @@ -2,6 +2,7 @@ import { Mutex } from "async-mutex"; import { JSONSchema7, JSONSchema7Object } from "json-schema"; import { v4 as uuidv4 } from "uuid"; +import { streamResponse } from "@continuedev/fetch"; import { ChatMessage, ChatMessageRole, @@ -12,7 +13,6 @@ import { import { renderChatMessage } from "../../util/messageContent.js"; import { getRemoteModelInfo } from "../../util/ollamaHelper.js"; import { BaseLLM } from "../index.js"; -import { streamResponse } from "../stream.js"; type OllamaChatMessage = { role: ChatMessageRole; diff --git a/core/llm/llms/OpenAI.ts b/core/llm/llms/OpenAI.ts index 3d0daac07..51c6e9794 100644 --- a/core/llm/llms/OpenAI.ts +++ b/core/llm/llms/OpenAI.ts @@ -3,6 +3,7 @@ import { ChatCompletionMessageParam, } from "openai/resources/index"; +import { streamSse } from "@continuedev/fetch"; import { ChatMessage, CompletionOptions, @@ -16,7 +17,6 @@ import { LlmApiRequestType, toChatBody, } from "../openaiTypeConverters.js"; -import { streamSse } from "../stream.js"; const NON_CHAT_MODELS = [ "text-davinci-002", diff --git a/core/llm/llms/SiliconFlow.ts b/core/llm/llms/SiliconFlow.ts index cddb22b0d..d0d3e901d 100644 --- a/core/llm/llms/SiliconFlow.ts +++ b/core/llm/llms/SiliconFlow.ts @@ -1,5 +1,5 @@ +import { streamSse } from "@continuedev/fetch"; import { Chunk, CompletionOptions, LLMOptions } from "../../index.js"; -import { streamSse } from "../stream.js"; import { osModelsEditPrompt } from "../templates/edit.js"; import OpenAI from "./OpenAI.js"; @@ -58,7 +58,7 @@ class SiliconFlow extends OpenAI { console.warn("[SiliconFlow] rerank: query is empty"); return []; } - + if (!chunks || chunks.length === 0) { console.warn("[SiliconFlow] rerank: chunks is empty"); return []; diff --git a/core/llm/llms/VertexAI.ts b/core/llm/llms/VertexAI.ts index 3c49523fb..1a956cf76 100644 --- a/core/llm/llms/VertexAI.ts +++ b/core/llm/llms/VertexAI.ts @@ -1,9 +1,9 @@ import { GoogleAuth } from "google-auth-library"; +import { streamResponse, streamSse } from "@continuedev/fetch"; import { ChatMessage, CompletionOptions, LLMOptions } from "../../index.js"; import { renderChatMessage, stripImages } from "../../util/messageContent.js"; import { BaseLLM } from "../index.js"; -import { streamResponse, streamSse } from "../stream.js"; import Anthropic from "./Anthropic.js"; import Gemini from "./Gemini.js"; diff --git a/core/llm/llms/WatsonX.ts b/core/llm/llms/WatsonX.ts index 0db209ca2..0c700f439 100644 --- a/core/llm/llms/WatsonX.ts +++ b/core/llm/llms/WatsonX.ts @@ -1,3 +1,4 @@ +import { streamResponse, streamSse } from "@continuedev/fetch"; import { ChatMessage, Chunk, @@ -6,7 +7,6 @@ import { } from "../../index.js"; import { BaseLLM } from "../index.js"; import { fromChatCompletionChunk } from "../openaiTypeConverters.js"; -import { streamResponse, streamSse } from "../stream.js"; let watsonxToken = { expiration: 0, diff --git a/core/llm/stream.ts b/core/llm/stream.ts deleted file mode 100644 index dd0983605..000000000 --- a/core/llm/stream.ts +++ /dev/null @@ -1,125 +0,0 @@ -async function* toAsyncIterable( - nodeReadable: NodeJS.ReadableStream, -): AsyncGenerator { - for await (const chunk of nodeReadable) { - // @ts-ignore - yield chunk as Uint8Array; - } -} - -export async function* streamResponse( - response: Response, -): AsyncGenerator { - if (response.status === 499) { - return; // In case of client-side cancellation, just return - } - - if (response.status !== 200) { - throw new Error(await response.text()); - } - - if (!response.body) { - throw new Error("No response body returned."); - } - - // Get the major version of Node.js - const nodeMajorVersion = parseInt(process.versions.node.split(".")[0], 10); - - try { - if (nodeMajorVersion >= 20) { - // Use the new API for Node 20 and above - const stream = (ReadableStream as any).from(response.body); - for await (const chunk of stream.pipeThrough( - new TextDecoderStream("utf-8"), - )) { - yield chunk; - } - } else { - // Fallback for Node versions below 20 - // Streaming with this method doesn't work as version 20+ does - const decoder = new TextDecoder("utf-8"); - const nodeStream = response.body as unknown as NodeJS.ReadableStream; - for await (const chunk of toAsyncIterable(nodeStream)) { - yield decoder.decode(chunk, { stream: true }); - } - } - } catch (e) { - if (e instanceof Error && e.name.startsWith("AbortError")) { - return; // In case of client-side cancellation, just return - } - throw e; - } -} - -function parseDataLine(line: string): any { - const json = line.startsWith("data: ") - ? line.slice("data: ".length) - : line.slice("data:".length); - - try { - const data = JSON.parse(json); - if (data.error) { - throw new Error(`Error streaming response: ${data.error}`); - } - - return data; - } catch (e) { - throw new Error(`Malformed JSON sent from server: ${json}`); - } -} - -function parseSseLine(line: string): { done: boolean; data: any } { - if (line.startsWith("data: [DONE]")) { - return { done: true, data: undefined }; - } - if (line.startsWith("data:")) { - return { done: false, data: parseDataLine(line) }; - } - if (line.startsWith(": ping")) { - return { done: true, data: undefined }; - } - return { done: false, data: undefined }; -} - -export async function* streamSse(response: Response): AsyncGenerator { - let buffer = ""; - for await (const value of streamResponse(response)) { - buffer += value; - - let position: number; - while ((position = buffer.indexOf("\n")) >= 0) { - const line = buffer.slice(0, position); - buffer = buffer.slice(position + 1); - - const { done, data } = parseSseLine(line); - if (done) { - break; - } - if (data) { - yield data; - } - } - } - - if (buffer.length > 0) { - const { done, data } = parseSseLine(buffer); - if (!done && data) { - yield data; - } - } -} - -export async function* streamJSON(response: Response): AsyncGenerator { - let buffer = ""; - for await (const value of streamResponse(response)) { - buffer += value; - - let position; - while ((position = buffer.indexOf("\n")) >= 0) { - const line = buffer.slice(0, position); - const data = JSON.parse(line); - yield data; - buffer = buffer.slice(position + 1); - } - } -}