Spaces:
Running
Running
File size: 13,614 Bytes
c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 9dcdd49 c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 306fc1f c343876 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
import { type ActionFunctionArgs } from '@remix-run/cloudflare';
import { createDataStream, generateId } from 'ai';
import { MAX_RESPONSE_SEGMENTS, MAX_TOKENS, type FileMap } from '~/lib/.server/llm/constants';
import { CONTINUE_PROMPT } from '~/lib/common/prompts/prompts';
import { streamText, type Messages, type StreamingOptions } from '~/lib/.server/llm/stream-text';
import SwitchableStream from '~/lib/.server/llm/switchable-stream';
import type { IProviderSetting } from '~/types/model';
import { createScopedLogger } from '~/utils/logger';
import { getFilePaths, selectContext } from '~/lib/.server/llm/select-context';
import type { ContextAnnotation, ProgressAnnotation } from '~/types/context';
import { WORK_DIR } from '~/utils/constants';
import { createSummary } from '~/lib/.server/llm/create-summary';
import { extractPropertiesFromMessage } from '~/lib/.server/llm/utils';
const CLAUDE_CACHE_TOKENS_MULTIPLIER = {
WRITE: 1.25,
READ: 0.1,
};
export async function action(args: ActionFunctionArgs) {
return chatAction(args);
}
const logger = createScopedLogger('api.chat');
function parseCookies(cookieHeader: string): Record<string, string> {
const cookies: Record<string, string> = {};
const items = cookieHeader.split(';').map((cookie) => cookie.trim());
items.forEach((item) => {
const [name, ...rest] = item.split('=');
if (name && rest) {
const decodedName = decodeURIComponent(name.trim());
const decodedValue = decodeURIComponent(rest.join('=').trim());
cookies[decodedName] = decodedValue;
}
});
return cookies;
}
async function chatAction({ context, request }: ActionFunctionArgs) {
const { messages, files, promptId, contextOptimization, isPromptCachingEnabled } = await request.json<{
messages: Messages;
files: any;
promptId?: string;
contextOptimization: boolean;
isPromptCachingEnabled: boolean;
}>();
const cookieHeader = request.headers.get('Cookie');
const apiKeys = JSON.parse(parseCookies(cookieHeader || '').apiKeys || '{}');
const providerSettings: Record<string, IProviderSetting> = JSON.parse(
parseCookies(cookieHeader || '').providers || '{}',
);
const stream = new SwitchableStream();
const cumulativeUsage = {
completionTokens: 0,
promptTokens: 0,
totalTokens: 0,
};
let isCacheHit = false;
let isCacheMiss = false;
const encoder: TextEncoder = new TextEncoder();
let progressCounter: number = 1;
try {
const totalMessageContent = messages.reduce((acc, message) => acc + message.content, '');
logger.debug(`Total message length: ${totalMessageContent.split(' ').length}, words`);
let lastChunk: string | undefined = undefined;
const dataStream = createDataStream({
async execute(dataStream) {
const filePaths = getFilePaths(files || {});
let filteredFiles: FileMap | undefined = undefined;
let summary: string | undefined = undefined;
let messageSliceId = 0;
if (messages.length > 3) {
messageSliceId = messages.length - 3;
}
if (filePaths.length > 0 && contextOptimization) {
logger.debug('Generating Chat Summary');
dataStream.writeData({
type: 'progress',
label: 'summary',
status: 'in-progress',
order: progressCounter++,
message: 'Analysing Request',
} satisfies ProgressAnnotation);
// Create a summary of the chat
console.log(`Messages count: ${messages.length}`);
summary = await createSummary({
messages: [...messages],
env: context.cloudflare?.env,
apiKeys,
providerSettings,
promptId,
contextOptimization,
onFinish(resp) {
if (resp.usage) {
logger.debug('createSummary token usage', JSON.stringify(resp.usage));
cumulativeUsage.completionTokens += resp.usage.completionTokens || 0;
cumulativeUsage.promptTokens += resp.usage.promptTokens || 0;
cumulativeUsage.totalTokens += resp.usage.totalTokens || 0;
}
},
});
dataStream.writeData({
type: 'progress',
label: 'summary',
status: 'complete',
order: progressCounter++,
message: 'Analysis Complete',
} satisfies ProgressAnnotation);
dataStream.writeMessageAnnotation({
type: 'chatSummary',
summary,
chatId: messages.slice(-1)?.[0]?.id,
} as ContextAnnotation);
// Update context buffer
logger.debug('Updating Context Buffer');
dataStream.writeData({
type: 'progress',
label: 'context',
status: 'in-progress',
order: progressCounter++,
message: 'Determining Files to Read',
} satisfies ProgressAnnotation);
// Select context files
console.log(`Messages count: ${messages.length}`);
filteredFiles = await selectContext({
messages: [...messages],
env: context.cloudflare?.env,
apiKeys,
files,
providerSettings,
promptId,
contextOptimization,
summary,
onFinish(resp) {
const cacheUsage = resp?.experimental_providerMetadata?.anthropic;
console.debug({ cacheUsage });
isCacheHit = !!cacheUsage?.cacheReadInputTokens;
isCacheMiss = !!cacheUsage?.cacheCreationInputTokens && !isCacheHit;
if (resp.usage) {
logger.debug('selectContext token usage', JSON.stringify(resp.usage));
cumulativeUsage.completionTokens += Math.round(resp?.usage.completionTokens || 0);
cumulativeUsage.promptTokens += Math.round(
(resp?.usage.promptTokens || 0) +
((cacheUsage?.cacheCreationInputTokens as number) || 0) * CLAUDE_CACHE_TOKENS_MULTIPLIER.WRITE +
((cacheUsage?.cacheReadInputTokens as number) || 0) * CLAUDE_CACHE_TOKENS_MULTIPLIER.READ,
);
cumulativeUsage.totalTokens = cumulativeUsage.completionTokens + cumulativeUsage.promptTokens;
}
},
});
if (filteredFiles) {
logger.debug(`files in context : ${JSON.stringify(Object.keys(filteredFiles))}`);
}
dataStream.writeMessageAnnotation({
type: 'codeContext',
files: Object.keys(filteredFiles).map((key) => {
let path = key;
if (path.startsWith(WORK_DIR)) {
path = path.replace(WORK_DIR, '');
}
return path;
}),
} as ContextAnnotation);
dataStream.writeData({
type: 'progress',
label: 'context',
status: 'complete',
order: progressCounter++,
message: 'Code Files Selected',
} satisfies ProgressAnnotation);
// logger.debug('Code Files Selected');
}
// Stream the text
const options: StreamingOptions = {
toolChoice: 'none',
// eslint-disable-next-line @typescript-eslint/naming-convention
onFinish: async ({ text: content, finishReason, usage, experimental_providerMetadata }) => {
logger.debug('usage', JSON.stringify(usage));
const cacheUsage = experimental_providerMetadata?.anthropic;
console.debug({ cacheUsage });
isCacheHit = !!cacheUsage?.cacheReadInputTokens;
isCacheMiss = !!cacheUsage?.cacheCreationInputTokens && !isCacheHit;
if (usage) {
cumulativeUsage.completionTokens += Math.round(usage.completionTokens || 0);
cumulativeUsage.promptTokens += Math.round(
(usage.promptTokens || 0) +
((cacheUsage?.cacheCreationInputTokens as number) || 0) * CLAUDE_CACHE_TOKENS_MULTIPLIER.WRITE +
((cacheUsage?.cacheReadInputTokens as number) || 0) * CLAUDE_CACHE_TOKENS_MULTIPLIER.READ,
);
cumulativeUsage.totalTokens = cumulativeUsage.completionTokens + cumulativeUsage.promptTokens;
}
if (finishReason !== 'length') {
dataStream.writeMessageAnnotation({
type: 'usage',
value: {
completionTokens: cumulativeUsage.completionTokens,
promptTokens: cumulativeUsage.promptTokens,
totalTokens: cumulativeUsage.totalTokens,
isCacheHit,
isCacheMiss,
},
});
dataStream.writeData({
type: 'progress',
label: 'response',
status: 'complete',
order: progressCounter++,
message: 'Response Generated',
} satisfies ProgressAnnotation);
await new Promise((resolve) => setTimeout(resolve, 0));
// stream.close();
return;
}
if (stream.switches >= MAX_RESPONSE_SEGMENTS) {
throw Error('Cannot continue message: Maximum segments reached');
}
const switchesLeft = MAX_RESPONSE_SEGMENTS - stream.switches;
logger.info(`Reached max token limit (${MAX_TOKENS}): Continuing message (${switchesLeft} switches left)`);
const lastUserMessage = messages.filter((x) => x.role == 'user').slice(-1)[0];
const { model, provider } = extractPropertiesFromMessage(lastUserMessage);
messages.push({ id: generateId(), role: 'assistant', content });
messages.push({
id: generateId(),
role: 'user',
content: `[Model: ${model}]\n\n[Provider: ${provider}]\n\n${CONTINUE_PROMPT}`,
});
const result = await streamText({
messages,
env: context.cloudflare?.env,
options,
apiKeys,
files,
providerSettings,
promptId,
contextOptimization,
contextFiles: filteredFiles,
summary,
messageSliceId,
});
result.mergeIntoDataStream(dataStream);
(async () => {
for await (const part of result.fullStream) {
if (part.type === 'error') {
const error: any = part.error;
logger.error(`${error}`);
return;
}
}
})();
return;
},
};
dataStream.writeData({
type: 'progress',
label: 'response',
status: 'in-progress',
order: progressCounter++,
message: 'Generating Response',
} satisfies ProgressAnnotation);
const result = await streamText({
messages,
env: context.cloudflare?.env,
options,
apiKeys,
files,
providerSettings,
promptId,
contextOptimization,
contextFiles: filteredFiles,
summary,
isPromptCachingEnabled,
messageSliceId,
});
(async () => {
for await (const part of result.fullStream) {
if (part.type === 'error') {
const error: any = part.error;
logger.error(`${error}`);
return;
}
}
})();
result.mergeIntoDataStream(dataStream);
},
onError: (error: any) => `Custom error: ${error.message}`,
}).pipeThrough(
new TransformStream({
transform: (chunk, controller) => {
if (!lastChunk) {
lastChunk = ' ';
}
if (typeof chunk === 'string') {
if (chunk.startsWith('g') && !lastChunk.startsWith('g')) {
controller.enqueue(encoder.encode(`0: "<div class=\\"__boltThought__\\">"\n`));
}
if (lastChunk.startsWith('g') && !chunk.startsWith('g')) {
controller.enqueue(encoder.encode(`0: "</div>\\n"\n`));
}
}
lastChunk = chunk;
let transformedChunk = chunk;
if (typeof chunk === 'string' && chunk.startsWith('g')) {
let content = chunk.split(':').slice(1).join(':');
if (content.endsWith('\n')) {
content = content.slice(0, content.length - 1);
}
transformedChunk = `0:${content}\n`;
}
// Convert the string stream to a byte stream
const str = typeof transformedChunk === 'string' ? transformedChunk : JSON.stringify(transformedChunk);
controller.enqueue(encoder.encode(str));
},
}),
);
return new Response(dataStream, {
status: 200,
headers: {
'Content-Type': 'text/event-stream; charset=utf-8',
Connection: 'keep-alive',
'Cache-Control': 'no-cache',
'Text-Encoding': 'chunked',
},
});
} catch (error: any) {
logger.error(error);
if (error.message?.includes('API key')) {
throw new Response('Invalid or missing API key', {
status: 401,
statusText: 'Unauthorized',
});
}
throw new Response(null, {
status: 500,
statusText: 'Internal Server Error',
});
}
}
|