use fetch package for streaming utils
This commit is contained in:
parent
8f3ee86d50
commit
35958b225d
18 changed files with 18 additions and 143 deletions
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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 [];
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -1,125 +0,0 @@
|
|||
async function* toAsyncIterable(
|
||||
nodeReadable: NodeJS.ReadableStream,
|
||||
): AsyncGenerator<Uint8Array> {
|
||||
for await (const chunk of nodeReadable) {
|
||||
// @ts-ignore
|
||||
yield chunk as Uint8Array;
|
||||
}
|
||||
}
|
||||
|
||||
export async function* streamResponse(
|
||||
response: Response,
|
||||
): AsyncGenerator<string> {
|
||||
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<any> {
|
||||
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<any> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue