Skip to content

Commit

Permalink
simplified the fix
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodacus committed Dec 17, 2024
1 parent 05146c1 commit 18d04ca
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 43 deletions.
2 changes: 1 addition & 1 deletion app/commit.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "commit": "1e72d52278730f7d22448be9d5cf2daf12559486", "version": "0.0.2" }
{ "commit": "05146c18d6e3b5410100089fed99c30d60dccad7" }
43 changes: 1 addition & 42 deletions app/routes/api.enhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 18d04ca

Please sign in to comment.