waitUntil and parallelize (#10)
This commit is contained in:
parent
923935f310
commit
743f300e04
4 changed files with 46 additions and 27 deletions
|
|
@ -28,6 +28,7 @@
|
||||||
"@tailwindcss/vite": "^4.0.0",
|
"@tailwindcss/vite": "^4.0.0",
|
||||||
"@testing-library/jest-dom": "^6.6.3",
|
"@testing-library/jest-dom": "^6.6.3",
|
||||||
"@testing-library/svelte": "^5.2.4",
|
"@testing-library/svelte": "^5.2.4",
|
||||||
|
"@vercel/functions": "^2.2.0",
|
||||||
"clsx": "^2.1.1",
|
"clsx": "^2.1.1",
|
||||||
"concurrently": "^9.1.2",
|
"concurrently": "^9.1.2",
|
||||||
"convex": "^1.24.8",
|
"convex": "^1.24.8",
|
||||||
|
|
|
||||||
14
pnpm-lock.yaml
generated
14
pnpm-lock.yaml
generated
|
|
@ -63,6 +63,9 @@ importers:
|
||||||
'@testing-library/svelte':
|
'@testing-library/svelte':
|
||||||
specifier: ^5.2.4
|
specifier: ^5.2.4
|
||||||
version: 5.2.8(svelte@5.34.1)(vite@6.3.5(@types/node@24.0.1)(jiti@2.4.2)(lightningcss@1.30.1))(vitest@3.2.3(@types/node@24.0.1)(jiti@2.4.2)(jsdom@26.1.0)(lightningcss@1.30.1))
|
version: 5.2.8(svelte@5.34.1)(vite@6.3.5(@types/node@24.0.1)(jiti@2.4.2)(lightningcss@1.30.1))(vitest@3.2.3(@types/node@24.0.1)(jiti@2.4.2)(jsdom@26.1.0)(lightningcss@1.30.1))
|
||||||
|
'@vercel/functions':
|
||||||
|
specifier: ^2.2.0
|
||||||
|
version: 2.2.0
|
||||||
clsx:
|
clsx:
|
||||||
specifier: ^2.1.1
|
specifier: ^2.1.1
|
||||||
version: 2.1.1
|
version: 2.1.1
|
||||||
|
|
@ -1026,6 +1029,15 @@ packages:
|
||||||
resolution: {integrity: sha512-qHV7pW7E85A0x6qyrFn+O+q1k1p3tQCsqIZ1KZ5ESLXY57aTvUd3/a4rdPTeXisvhXn2VQG0VSKUqs8KHF2zcA==}
|
resolution: {integrity: sha512-qHV7pW7E85A0x6qyrFn+O+q1k1p3tQCsqIZ1KZ5ESLXY57aTvUd3/a4rdPTeXisvhXn2VQG0VSKUqs8KHF2zcA==}
|
||||||
engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0}
|
engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0}
|
||||||
|
|
||||||
|
'@vercel/functions@2.2.0':
|
||||||
|
resolution: {integrity: sha512-x1Zrc2jOclTSB9+Ic/XNMDinO0SG4ZS5YeV2Xz1m/tuJOM7QtPVU3Epw2czBao0dukefmC8HCNpyUL8ZchJ/Tg==}
|
||||||
|
engines: {node: '>= 18'}
|
||||||
|
peerDependencies:
|
||||||
|
'@aws-sdk/credential-provider-web-identity': '*'
|
||||||
|
peerDependenciesMeta:
|
||||||
|
'@aws-sdk/credential-provider-web-identity':
|
||||||
|
optional: true
|
||||||
|
|
||||||
'@vitest/expect@3.2.3':
|
'@vitest/expect@3.2.3':
|
||||||
resolution: {integrity: sha512-W2RH2TPWVHA1o7UmaFKISPvdicFJH+mjykctJFoAkUw+SPTJTGjUNdKscFBrqM7IPnCVu6zihtKYa7TkZS1dkQ==}
|
resolution: {integrity: sha512-W2RH2TPWVHA1o7UmaFKISPvdicFJH+mjykctJFoAkUw+SPTJTGjUNdKscFBrqM7IPnCVu6zihtKYa7TkZS1dkQ==}
|
||||||
|
|
||||||
|
|
@ -3224,6 +3236,8 @@ snapshots:
|
||||||
'@typescript-eslint/types': 8.34.0
|
'@typescript-eslint/types': 8.34.0
|
||||||
eslint-visitor-keys: 4.2.1
|
eslint-visitor-keys: 4.2.1
|
||||||
|
|
||||||
|
'@vercel/functions@2.2.0': {}
|
||||||
|
|
||||||
'@vitest/expect@3.2.3':
|
'@vitest/expect@3.2.3':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/chai': 5.2.2
|
'@types/chai': 5.2.2
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,6 @@
|
||||||
|
|
||||||
async function toggleEnabled(v: boolean) {
|
async function toggleEnabled(v: boolean) {
|
||||||
enabled = v; // Optimistic!
|
enabled = v; // Optimistic!
|
||||||
console.log('hi');
|
|
||||||
if (!session.current?.user.id) return;
|
if (!session.current?.user.id) return;
|
||||||
|
|
||||||
const res = await ResultAsync.fromPromise(
|
const res = await ResultAsync.fromPromise(
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import { error, json, type RequestHandler } from '@sveltejs/kit';
|
||||||
import { ConvexHttpClient } from 'convex/browser';
|
import { ConvexHttpClient } from 'convex/browser';
|
||||||
import { ResultAsync } from 'neverthrow';
|
import { ResultAsync } from 'neverthrow';
|
||||||
import OpenAI from 'openai';
|
import OpenAI from 'openai';
|
||||||
|
import { waitUntil } from '@vercel/functions';
|
||||||
|
|
||||||
import { z } from 'zod/v4';
|
import { z } from 'zod/v4';
|
||||||
|
|
||||||
|
|
@ -48,14 +49,30 @@ async function generateAIResponse(
|
||||||
) {
|
) {
|
||||||
log('Starting AI response generation in background', startTime);
|
log('Starting AI response generation in background', startTime);
|
||||||
|
|
||||||
const modelResult = await ResultAsync.fromPromise(
|
const [modelResult, keyResult, messagesQueryResult] = await Promise.all([
|
||||||
client.query(api.user_enabled_models.get, {
|
ResultAsync.fromPromise(
|
||||||
provider: Provider.OpenRouter,
|
client.query(api.user_enabled_models.get, {
|
||||||
model_id: modelId,
|
provider: Provider.OpenRouter,
|
||||||
session_token: session.token,
|
model_id: modelId,
|
||||||
}),
|
session_token: session.token,
|
||||||
(e) => `Failed to get model: ${e}`
|
}),
|
||||||
);
|
(e) => `Failed to get model: ${e}`
|
||||||
|
),
|
||||||
|
ResultAsync.fromPromise(
|
||||||
|
client.query(api.user_keys.get, {
|
||||||
|
provider: Provider.OpenRouter,
|
||||||
|
session_token: session.token,
|
||||||
|
}),
|
||||||
|
(e) => `Failed to get API key: ${e}`
|
||||||
|
),
|
||||||
|
ResultAsync.fromPromise(
|
||||||
|
client.query(api.messages.getAllFromConversation, {
|
||||||
|
conversation_id: conversationId as Id<'conversations'>,
|
||||||
|
session_token: session.token,
|
||||||
|
}),
|
||||||
|
(e) => `Failed to get messages: ${e}`
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
if (modelResult.isErr()) {
|
if (modelResult.isErr()) {
|
||||||
log(`Background model query failed: ${modelResult.error}`, startTime);
|
log(`Background model query failed: ${modelResult.error}`, startTime);
|
||||||
|
|
@ -70,13 +87,7 @@ async function generateAIResponse(
|
||||||
|
|
||||||
log('Background: Model found and enabled', startTime);
|
log('Background: Model found and enabled', startTime);
|
||||||
|
|
||||||
const messagesQuery = await ResultAsync.fromPromise(
|
const messagesQuery = await messagesQueryResult;
|
||||||
client.query(api.messages.getAllFromConversation, {
|
|
||||||
conversation_id: conversationId as Id<'conversations'>,
|
|
||||||
session_token: session.token,
|
|
||||||
}),
|
|
||||||
(e) => `Failed to get messages: ${e}`
|
|
||||||
);
|
|
||||||
|
|
||||||
if (messagesQuery.isErr()) {
|
if (messagesQuery.isErr()) {
|
||||||
log(`Background messages query failed: ${messagesQuery.error}`, startTime);
|
log(`Background messages query failed: ${messagesQuery.error}`, startTime);
|
||||||
|
|
@ -86,14 +97,6 @@ async function generateAIResponse(
|
||||||
const messages = messagesQuery.value;
|
const messages = messagesQuery.value;
|
||||||
log(`Background: Retrieved ${messages.length} messages from conversation`, startTime);
|
log(`Background: Retrieved ${messages.length} messages from conversation`, startTime);
|
||||||
|
|
||||||
const keyResult = await ResultAsync.fromPromise(
|
|
||||||
client.query(api.user_keys.get, {
|
|
||||||
provider: Provider.OpenRouter,
|
|
||||||
session_token: session.token,
|
|
||||||
}),
|
|
||||||
(e) => `Failed to get API key: ${e}`
|
|
||||||
);
|
|
||||||
|
|
||||||
if (keyResult.isErr()) {
|
if (keyResult.isErr()) {
|
||||||
log(`Background API key query failed: ${keyResult.error}`, startTime);
|
log(`Background API key query failed: ${keyResult.error}`, startTime);
|
||||||
return;
|
return;
|
||||||
|
|
@ -271,9 +274,11 @@ export const POST: RequestHandler = async ({ request }) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start AI response generation in background - don't await
|
// Start AI response generation in background - don't await
|
||||||
generateAIResponse(conversationId, session, args.model_id, startTime).catch((error) => {
|
waitUntil(
|
||||||
log(`Background AI response generation error: ${error}`, startTime);
|
generateAIResponse(conversationId, session, args.model_id, startTime).catch((error) => {
|
||||||
});
|
log(`Background AI response generation error: ${error}`, startTime);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
log('Response sent, AI generation started in background', startTime);
|
log('Response sent, AI generation started in background', startTime);
|
||||||
return response({ ok: true, conversation_id: conversationId });
|
return response({ ok: true, conversation_id: conversationId });
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue