simplified the fix

This commit is contained in:
Anirban Kar 2024-12-18 02:20:14 +05:30
parent 05146c18d6
commit 18d04ca065
2 changed files with 2 additions and 43 deletions

View File

@ -1 +1 @@
{ "commit": "1e72d52278730f7d22448be9d5cf2daf12559486", "version": "0.0.2" }
{ "commit": "05146c18d6e3b5410100089fed99c30d60dccad7" }

View File

@ -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',