Files
bolt.new/app/routes/api.enhancer.ts
Dustin Loring 0bf3cafe83 chore: pnpm lint fix
ran pnpm lint fix and fixed all lint errors
2025-01-16 10:45:06 -05:00

65 lines
1.7 KiB
TypeScript

import { type ActionFunctionArgs } from '@remix-run/cloudflare';
import { streamText } from '~/lib/.server/llm/stream-text';
import { stripIndents } from '~/utils/stripIndent';
const encoder = new TextEncoder();
const decoder = new TextDecoder();
export async function action(args: ActionFunctionArgs) {
return enhancerAction(args);
}
async function enhancerAction({ context, request }: ActionFunctionArgs) {
const { message } = await request.json<{ message: string }>();
try {
const result = await streamText(
[
{
role: 'user',
content: stripIndents`
I want you to improve the user prompt that is wrapped in \`<original_prompt>\` tags.
IMPORTANT: Only respond with the improved prompt and nothing else!
<original_prompt>
${message}
</original_prompt>
`,
},
],
context.cloudflare.env,
);
const transformStream = new TransformStream({
transform(chunk, controller) {
const text = decoder.decode(chunk);
const lines = text.split('\n');
for (const line of lines) {
if (line.startsWith('0:')) {
// extract only the content after "0:"
const content = line.slice(2);
controller.enqueue(encoder.encode(content));
}
// ignore 'e:' and 'd:' lines as they contain metadata
}
},
});
const stream = result.toDataStream().pipeThrough(transformStream);
return new Response(stream, {
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
});
} catch (error) {
console.log(error);
throw new Response(null, {
status: 500,
statusText: 'Internal Server Error',
});
}
}