95 lines
3.0 KiB
TypeScript
95 lines
3.0 KiB
TypeScript
import { and, eq } from "drizzle-orm";
|
|
import { models } from "~~/drizzle/schema";
|
|
import { db } from "~~/server/lib/db";
|
|
import * as z from 'zod';
|
|
|
|
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(),
|
|
cost: z.object({
|
|
prompt: z.string().optional(),
|
|
completion: z.string().optional(),
|
|
request: z.string().optional(),
|
|
image: z.string().optional(),
|
|
imageTokens: z.string().optional(),
|
|
imageOutput: z.string().optional(),
|
|
audio: z.string().optional(),
|
|
audioOutput: z.string().optional(),
|
|
inputAudioCache: z.string().optional(),
|
|
webSearch: z.string().optional(),
|
|
internalReasoning: z.string().optional(),
|
|
inputCacheRead: z.string().optional(),
|
|
inputCacheWrite: z.string().optional(),
|
|
discount: z.string().optional(),
|
|
}).optional(),
|
|
inputModalities: z.array(z.string()).optional(),
|
|
outputModalities: z.array(z.string()).optional(),
|
|
capabilities: z.array(z.string()).optional(),
|
|
contextWindow: z.number().optional(),
|
|
supportedParameters: z.array(z.string()).optional(),
|
|
isCustom: z.boolean().optional(),
|
|
enabled: z.boolean().optional(),
|
|
releasedAt: z.string().optional(),
|
|
})
|
|
.safeParse(body),
|
|
);
|
|
|
|
if (!result.success) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: result.error.issues[0]!.message,
|
|
});
|
|
}
|
|
|
|
const modelId = getRouterParam(event, 'modelId')!;
|
|
|
|
const { name, cost, inputModalities, outputModalities, capabilities, contextWindow, supportedParameters, isCustom, enabled, releasedAt } = result.data;
|
|
|
|
const existing = await db.query.models.findFirst({
|
|
where: {
|
|
id: modelId,
|
|
userId,
|
|
},
|
|
});
|
|
|
|
if (!existing) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Invalid model',
|
|
});
|
|
}
|
|
|
|
const res = await db.update(models)
|
|
.set({
|
|
name,
|
|
cost,
|
|
inputModalities,
|
|
outputModalities,
|
|
capabilities,
|
|
contextWindow,
|
|
supportedParameters,
|
|
isCustom,
|
|
enabled,
|
|
releasedAt: releasedAt ? new Date(releasedAt) : existing.releasedAt ?? null,
|
|
})
|
|
.where(
|
|
and(
|
|
eq(models.id, modelId),
|
|
eq(models.userId, userId),
|
|
)
|
|
);
|
|
|
|
if (res.rowCount === 0) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Invalid model',
|
|
});
|
|
}
|
|
|
|
return { ok: true };
|
|
}); |