86 lines
2.9 KiB
TypeScript
86 lines
2.9 KiB
TypeScript
import * as z from 'zod';
|
|
import { models } from '~~/drizzle/schema';
|
|
import { db } from '~~/server/lib/db';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
await protectRoute(event);
|
|
|
|
const result = await readValidatedBody(event, (body) =>
|
|
z.object({
|
|
id: z.string(),
|
|
providerId: z.string(),
|
|
externalId: z.string(),
|
|
name: z.string(),
|
|
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().nullable().optional(),
|
|
supportedParameters: z.array(z.string()).optional(),
|
|
isCustom: z.boolean().optional(),
|
|
enabled: z.boolean().optional(),
|
|
releasedAt: z.date().nullable().optional(),
|
|
})
|
|
.safeParse(body),
|
|
);
|
|
|
|
if (!result.success) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
...result.error
|
|
});
|
|
}
|
|
|
|
const provider = await db.query.providers.findFirst({
|
|
where: {
|
|
id: result.data.providerId,
|
|
userId: event.context.user!.id,
|
|
},
|
|
with: {
|
|
models: true,
|
|
}
|
|
});
|
|
|
|
if (!provider) {
|
|
throw createError({
|
|
statusCode: 404,
|
|
message: 'Provider not found',
|
|
});
|
|
}
|
|
|
|
const model = await db.insert(models).values({
|
|
id: result.data.id,
|
|
userId: event.context.user!.id,
|
|
providerId: result.data.providerId,
|
|
externalId: result.data.externalId,
|
|
name: result.data.name,
|
|
cost: result.data.cost,
|
|
inputModalities: result.data.inputModalities,
|
|
outputModalities: result.data.outputModalities,
|
|
capabilities: result.data.capabilities,
|
|
contextWindow: result.data.contextWindow,
|
|
supportedParameters: result.data.supportedParameters,
|
|
isCustom: result.data.isCustom,
|
|
enabled: result.data.enabled,
|
|
releasedAt: result.data.releasedAt,
|
|
}).onConflictDoNothing().returning();
|
|
|
|
return {
|
|
model,
|
|
};
|
|
}); |