66 lines
1.6 KiB
TypeScript
66 lines
1.6 KiB
TypeScript
import { and, eq } from 'drizzle-orm';
|
|
import * as z from 'zod';
|
|
import { agents } from '~~/drizzle/schema';
|
|
import { db } from '~~/server/lib/db';
|
|
import { userEvents } from '~~/server/utils/events';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
await protectRoute(event);
|
|
const userId = event.context.user!.id;
|
|
|
|
const result = await readValidatedBody(event, (body) =>
|
|
z
|
|
.object({
|
|
name: z.string().optional(),
|
|
systemPrompt: z.string().nullable().optional(),
|
|
imageUrl: z.string().optional(),
|
|
defaultModelId: z.string().optional(),
|
|
})
|
|
.safeParse(body),
|
|
);
|
|
|
|
if (!result.success) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: result.error.issues[0]!.message,
|
|
});
|
|
}
|
|
|
|
const agentId = getRouterParam(event, 'id')!;
|
|
|
|
const { name, systemPrompt, imageUrl, defaultModelId } = result.data;
|
|
|
|
const res = await db.update(agents)
|
|
.set({
|
|
name,
|
|
systemPrompt,
|
|
imageUrl,
|
|
defaultModelId,
|
|
})
|
|
.where(
|
|
and(
|
|
eq(agents.id, agentId),
|
|
eq(agents.userId, userId),
|
|
)
|
|
);
|
|
|
|
if (res.rowCount === 0) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Invalid agent',
|
|
});
|
|
}
|
|
|
|
userEvents.emit(userId, 'agents', {
|
|
op: 'update',
|
|
payload: {
|
|
id: agentId,
|
|
name,
|
|
systemPrompt,
|
|
imageUrl,
|
|
defaultModelId,
|
|
},
|
|
});
|
|
|
|
return { ok: true };
|
|
}); |