attempt to make things quicker
This commit is contained in:
parent
89c6fea1e5
commit
79d13fdfac
4 changed files with 81 additions and 10 deletions
|
|
@ -69,6 +69,7 @@
|
|||
"@fontsource-variable/geist-mono": "^5.2.6",
|
||||
"@fontsource-variable/inter": "^5.2.6",
|
||||
"better-auth": "^1.2.9",
|
||||
"convex-helpers": "^0.1.94",
|
||||
"openai": "^5.3.0",
|
||||
"zod": "^3.25.64"
|
||||
}
|
||||
|
|
|
|||
32
pnpm-lock.yaml
generated
32
pnpm-lock.yaml
generated
|
|
@ -23,6 +23,9 @@ importers:
|
|||
better-auth:
|
||||
specifier: ^1.2.9
|
||||
version: 1.2.9
|
||||
convex-helpers:
|
||||
specifier: ^0.1.94
|
||||
version: 0.1.94(convex@1.24.8)(typescript@5.8.3)(zod@3.25.64)
|
||||
openai:
|
||||
specifier: ^5.3.0
|
||||
version: 5.3.0(ws@8.18.2)(zod@3.25.64)
|
||||
|
|
@ -1202,6 +1205,28 @@ packages:
|
|||
confbox@0.2.2:
|
||||
resolution: {integrity: sha512-1NB+BKqhtNipMsov4xI/NnhCKp9XG9NamYp5PVm9klAT0fsrNPjaFICsCFhNhwZJKNh7zB/3q8qXz0E9oaMNtQ==}
|
||||
|
||||
convex-helpers@0.1.94:
|
||||
resolution: {integrity: sha512-35o9TzEUdze3wGHxksk9Ynutw8ekxp/kbjs1dlfK6iZwPJeKbv9sqRXIyvPytTGvMmoi40fGm09f/z22gg6L+A==}
|
||||
hasBin: true
|
||||
peerDependencies:
|
||||
'@standard-schema/spec': ^1.0.0
|
||||
convex: ^1.13.0
|
||||
hono: ^4.0.5
|
||||
react: ^17.0.2 || ^18.0.0 || ^19.0.0
|
||||
typescript: ^5.5
|
||||
zod: ^3.22.4
|
||||
peerDependenciesMeta:
|
||||
'@standard-schema/spec':
|
||||
optional: true
|
||||
hono:
|
||||
optional: true
|
||||
react:
|
||||
optional: true
|
||||
typescript:
|
||||
optional: true
|
||||
zod:
|
||||
optional: true
|
||||
|
||||
convex-svelte@0.0.11:
|
||||
resolution: {integrity: sha512-N/29gg5Zqy72vKL4xHSLk3jGwXVKIWXPs6xzq6KxGL84y/D6hG85pG2CPOzn08EzMmByts5FTkJ5p3var6yDng==}
|
||||
peerDependencies:
|
||||
|
|
@ -3420,6 +3445,13 @@ snapshots:
|
|||
|
||||
confbox@0.2.2: {}
|
||||
|
||||
convex-helpers@0.1.94(convex@1.24.8)(typescript@5.8.3)(zod@3.25.64):
|
||||
dependencies:
|
||||
convex: 1.24.8
|
||||
optionalDependencies:
|
||||
typescript: 5.8.3
|
||||
zod: 3.25.64
|
||||
|
||||
convex-svelte@0.0.11(convex@1.24.8)(svelte@5.34.1):
|
||||
dependencies:
|
||||
convex: 1.24.8
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { api } from './_generated/api';
|
|||
import { mutation, query } from './_generated/server';
|
||||
import { type Id } from './_generated/dataModel';
|
||||
import { type SessionObj } from './betterAuth';
|
||||
import { messageRoleValidator } from './schema';
|
||||
|
||||
export const get = query({
|
||||
args: {
|
||||
|
|
@ -49,3 +50,41 @@ export const create = mutation({
|
|||
return res;
|
||||
},
|
||||
});
|
||||
|
||||
export const createAndAddMessage = mutation({
|
||||
args: {
|
||||
content: v.string(),
|
||||
role: messageRoleValidator,
|
||||
session_token: v.string(),
|
||||
},
|
||||
handler: async (
|
||||
ctx,
|
||||
args
|
||||
): Promise<{ conversationId: Id<'conversations'>; messageId: Id<'messages'> }> => {
|
||||
const session = await ctx.runQuery(api.betterAuth.publicGetSession, {
|
||||
session_token: args.session_token,
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
throw new Error('Unauthorized');
|
||||
}
|
||||
|
||||
const conversationId = await ctx.db.insert('conversations', {
|
||||
title: 'Untitled (for now)',
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Id type is janking out
|
||||
user_id: session.userId as any,
|
||||
});
|
||||
|
||||
const messageId = await ctx.runMutation(api.messages.create, {
|
||||
content: args.content,
|
||||
role: args.role,
|
||||
conversation_id: conversationId,
|
||||
session_token: args.session_token,
|
||||
});
|
||||
|
||||
return {
|
||||
conversationId,
|
||||
messageId,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -246,25 +246,24 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||
|
||||
let conversationId = args.conversation_id;
|
||||
if (!conversationId) {
|
||||
const conversationResult = await ResultAsync.fromPromise(
|
||||
client.mutation(api.conversations.create, {
|
||||
session_token: args.session_token,
|
||||
const convMessageResult = await ResultAsync.fromPromise(
|
||||
client.mutation(api.conversations.createAndAddMessage, {
|
||||
content: args.message,
|
||||
role: 'user',
|
||||
session_token: session.token,
|
||||
}),
|
||||
(e) => `Failed to create conversation: ${e}`
|
||||
);
|
||||
|
||||
if (conversationResult.isErr()) {
|
||||
log(`Conversation creation failed: ${conversationResult.error}`, startTime);
|
||||
if (convMessageResult.isErr()) {
|
||||
log(`Conversation creation failed: ${convMessageResult.error}`, startTime);
|
||||
return error(500, 'Failed to create conversation');
|
||||
}
|
||||
|
||||
conversationId = conversationResult.value;
|
||||
log('New conversation created', startTime);
|
||||
conversationId = convMessageResult.value.conversationId;
|
||||
log('New conversation and message created', startTime);
|
||||
} else {
|
||||
log('Using existing conversation', startTime);
|
||||
}
|
||||
|
||||
if (args.message) {
|
||||
const userMessageResult = await ResultAsync.fromPromise(
|
||||
client.mutation(api.messages.create, {
|
||||
conversation_id: conversationId as Id<'conversations'>,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue