From 18d04ca065f4a3823779b905dbf3006373892a56 Mon Sep 17 00:00:00 2001 From: Anirban Kar Date: Wed, 18 Dec 2024 02:20:14 +0530 Subject: [PATCH] simplified the fix --- app/commit.json | 2 +- app/routes/api.enhancer.ts | 43 +------------------------------------- 2 files changed, 2 insertions(+), 43 deletions(-) diff --git a/app/commit.json b/app/commit.json index 832678f..f7222f2 100644 --- a/app/commit.json +++ b/app/commit.json @@ -1 +1 @@ -{ "commit": "1e72d52278730f7d22448be9d5cf2daf12559486", "version": "0.0.2" } +{ "commit": "05146c18d6e3b5410100089fed99c30d60dccad7" } diff --git a/app/routes/api.enhancer.ts b/app/routes/api.enhancer.ts index c53d25a..2b8fee8 100644 --- a/app/routes/api.enhancer.ts +++ b/app/routes/api.enhancer.ts @@ -5,9 +5,6 @@ import { streamText } from '~/lib/.server/llm/stream-text'; import { stripIndents } from '~/utils/stripIndent'; import type { IProviderSetting, ProviderInfo } from '~/types/model'; -const encoder = new TextEncoder(); -const decoder = new TextDecoder(); - export async function action(args: ActionFunctionArgs) { return enhancerAction(args); } @@ -107,45 +104,7 @@ async function enhancerAction({ context, request }: ActionFunctionArgs) { providerSettings, }); - const transformStream = new TransformStream({ - transform(chunk, controller) { - const text = decoder.decode(chunk); - const lines = text.split('\n').filter((line) => line.trim() !== ''); - - for (const line of lines) { - try { - // Handle token-based streaming format - if (line.includes('0:"')) { - // Extract all token contents and join them - const tokens = line.match(/0:"([^"]+)"/g) || []; - const content = tokens - .map(token => token.slice(3, -1)) // Remove the '0:"' prefix and '"' suffix - .join(''); - - if (content) { - controller.enqueue(encoder.encode(content)); - } - continue; - } - - // Try to parse as JSON if it's not token-based format - const parsed = JSON.parse(line); - if (parsed.type === 'text') { - controller.enqueue(encoder.encode(parsed.value)); - } - } catch (e) { - // If not JSON and not token-based, treat as plain text - if (!line.includes('e:') && !line.includes('d:')) { // Skip metadata lines - controller.enqueue(encoder.encode(line)); - } - } - } - }, - }); - - const transformedStream = result.toDataStream().pipeThrough(transformStream); - - return new Response(transformedStream, { + return new Response(result.textStream, { status: 200, headers: { 'Content-Type': 'text/plain; charset=utf-8',