|
|
import { ZodType, z } from "zod"; |
|
|
import { MODEL_FAMILIES, ModelFamily } from "../models"; |
|
|
import { makeOptionalPropsNullable } from "../utils"; |
|
|
|
|
|
|
|
|
|
|
|
export const tokenCountsSchema: ZodType<UserTokenCounts> = z.object( |
|
|
MODEL_FAMILIES.reduce( |
|
|
(acc, family) => ({ |
|
|
...acc, |
|
|
[family]: z |
|
|
.object({ |
|
|
input: z.number().optional().default(0), |
|
|
output: z.number().optional().default(0), |
|
|
legacy_total: z.number().optional(), |
|
|
}) |
|
|
.optional() |
|
|
.default({ input: 0, output: 0 }), |
|
|
}), |
|
|
{} as Record<ModelFamily, ZodType<{ input: number; output: number; legacy_total?: number }>> |
|
|
) |
|
|
); |
|
|
|
|
|
export const UserSchema = z |
|
|
.object({ |
|
|
|
|
|
token: z.string(), |
|
|
|
|
|
ip: z.array(z.string()), |
|
|
|
|
|
nickname: z.string().max(80).optional(), |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type: z.enum(["normal", "special", "temporary"]), |
|
|
|
|
|
promptCount: z.number(), |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tokenCount: z.any().optional(), |
|
|
|
|
|
tokenCounts: tokenCountsSchema, |
|
|
|
|
|
tokenLimits: tokenCountsSchema, |
|
|
|
|
|
tokenRefresh: tokenCountsSchema, |
|
|
|
|
|
createdAt: z.number(), |
|
|
|
|
|
lastUsedAt: z.number().optional(), |
|
|
|
|
|
disabledAt: z.number().optional(), |
|
|
|
|
|
disabledReason: z.string().optional(), |
|
|
|
|
|
expiresAt: z.number().optional(), |
|
|
|
|
|
maxIps: z.coerce.number().int().min(0).optional(), |
|
|
|
|
|
adminNote: z.string().optional(), |
|
|
meta: z.record(z.any()).optional(), |
|
|
}) |
|
|
.strict(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const UserPartialSchema = makeOptionalPropsNullable(UserSchema) |
|
|
.partial() |
|
|
.extend({ token: z.string() }); |
|
|
|
|
|
export type UserTokenCounts = { |
|
|
[K in ModelFamily]: { input: number; output: number; legacy_total?: number } | undefined; |
|
|
}; |
|
|
export type User = z.infer<typeof UserSchema>; |
|
|
export type UserUpdate = z.infer<typeof UserPartialSchema>; |
|
|
|