2024-07-10 16:44:39 +00:00
|
|
|
import { type ActionFunctionArgs } from '@remix-run/cloudflare';
|
2024-07-24 15:43:32 +00:00
|
|
|
import { MAX_RESPONSE_SEGMENTS, MAX_TOKENS } from '~/lib/.server/llm/constants';
|
|
|
|
import { CONTINUE_PROMPT } from '~/lib/.server/llm/prompts';
|
|
|
|
import { streamText, type Messages, type StreamingOptions } from '~/lib/.server/llm/stream-text';
|
|
|
|
import SwitchableStream from '~/lib/.server/llm/switchable-stream';
|
2024-07-10 16:44:39 +00:00
|
|
|
|
2024-07-29 18:31:45 +00:00
|
|
|
export async function action(args: ActionFunctionArgs) {
|
2024-09-26 16:45:41 +00:00
|
|
|
return chatAction(args);
|
2024-07-29 18:31:45 +00:00
|
|
|
}
|
|
|
|
|
2024-12-06 11:28:04 +00:00
|
|
|
function parseCookies(cookieHeader: string) {
|
|
|
|
const cookies: any = {};
|
2024-11-13 20:20:51 +00:00
|
|
|
|
|
|
|
// Split the cookie string by semicolons and spaces
|
2024-11-21 21:05:35 +00:00
|
|
|
const items = cookieHeader.split(';').map((cookie) => cookie.trim());
|
|
|
|
|
|
|
|
items.forEach((item) => {
|
|
|
|
const [name, ...rest] = item.split('=');
|
2024-11-13 20:20:51 +00:00
|
|
|
|
|
|
|
if (name && rest) {
|
|
|
|
// Decode the name and value, and join value parts in case it contains '='
|
|
|
|
const decodedName = decodeURIComponent(name.trim());
|
2024-11-21 21:05:35 +00:00
|
|
|
const decodedValue = decodeURIComponent(rest.join('=').trim());
|
2024-11-13 20:20:51 +00:00
|
|
|
cookies[decodedName] = decodedValue;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return cookies;
|
|
|
|
}
|
|
|
|
|
2024-09-25 18:54:09 +00:00
|
|
|
async function chatAction({ context, request }: ActionFunctionArgs) {
|
2024-12-07 10:28:13 +00:00
|
|
|
const { messages, files } = await request.json<{
|
2024-11-21 21:05:35 +00:00
|
|
|
messages: Messages;
|
2024-12-07 10:28:13 +00:00
|
|
|
files: any;
|
2024-10-29 03:19:30 +00:00
|
|
|
}>();
|
2024-07-25 15:28:23 +00:00
|
|
|
|
2024-11-21 21:05:35 +00:00
|
|
|
const cookieHeader = request.headers.get('Cookie');
|
2024-11-13 20:20:51 +00:00
|
|
|
|
|
|
|
// Parse the cookie's value (returns an object or null if no cookie exists)
|
2024-12-06 11:28:04 +00:00
|
|
|
const apiKeys = JSON.parse(parseCookies(cookieHeader || '').apiKeys || '{}');
|
2024-11-13 20:20:51 +00:00
|
|
|
|
2024-07-19 09:12:55 +00:00
|
|
|
const stream = new SwitchableStream();
|
2024-07-10 16:44:39 +00:00
|
|
|
|
|
|
|
try {
|
2024-07-19 09:12:55 +00:00
|
|
|
const options: StreamingOptions = {
|
|
|
|
toolChoice: 'none',
|
2024-09-25 18:54:09 +00:00
|
|
|
onFinish: async ({ text: content, finishReason }) => {
|
2024-07-19 09:12:55 +00:00
|
|
|
if (finishReason !== 'length') {
|
|
|
|
return stream.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream.switches >= MAX_RESPONSE_SEGMENTS) {
|
2024-07-22 15:40:28 +00:00
|
|
|
throw Error('Cannot continue message: Maximum segments reached');
|
2024-07-19 09:12:55 +00:00
|
|
|
}
|
|
|
|
|
2024-07-22 15:40:28 +00:00
|
|
|
const switchesLeft = MAX_RESPONSE_SEGMENTS - stream.switches;
|
|
|
|
|
|
|
|
console.log(`Reached max token limit (${MAX_TOKENS}): Continuing message (${switchesLeft} switches left)`);
|
|
|
|
|
2024-07-19 09:12:55 +00:00
|
|
|
messages.push({ role: 'assistant', content });
|
|
|
|
messages.push({ role: 'user', content: CONTINUE_PROMPT });
|
|
|
|
|
2024-12-07 10:28:13 +00:00
|
|
|
const result = await streamText(messages, context.cloudflare.env, options, apiKeys, files);
|
2024-07-19 09:12:55 +00:00
|
|
|
|
|
|
|
return stream.switchSource(result.toAIStream());
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-12-07 10:28:13 +00:00
|
|
|
const result = await streamText(messages, context.cloudflare.env, options, apiKeys, files);
|
2024-07-19 09:12:55 +00:00
|
|
|
|
|
|
|
stream.switchSource(result.toAIStream());
|
|
|
|
|
2024-08-08 15:48:36 +00:00
|
|
|
return new Response(stream.readable, {
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
contentType: 'text/plain; charset=utf-8',
|
|
|
|
},
|
|
|
|
});
|
2024-12-06 11:28:04 +00:00
|
|
|
} catch (error: any) {
|
2024-07-10 16:44:39 +00:00
|
|
|
console.log(error);
|
2024-11-13 20:20:51 +00:00
|
|
|
|
2024-10-29 03:19:30 +00:00
|
|
|
if (error.message?.includes('API key')) {
|
|
|
|
throw new Response('Invalid or missing API key', {
|
|
|
|
status: 401,
|
2024-11-21 21:05:35 +00:00
|
|
|
statusText: 'Unauthorized',
|
2024-10-29 03:19:30 +00:00
|
|
|
});
|
|
|
|
}
|
2024-07-10 16:44:39 +00:00
|
|
|
|
|
|
|
throw new Response(null, {
|
|
|
|
status: 500,
|
|
|
|
statusText: 'Internal Server Error',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|