import {z} from 'zod' export type AdminV1EstimateModelPricingRequest = {ai?: number | undefined, ao?: number | undefined, c?: number | undefined, cc?: number | undefined, cc1h?: number | undefined, cr?: number | undefined, expr?: string | undefined, hasVideoIn?: number | undefined, img?: number | undefined, imgCr?: number | undefined, imgN?: number | undefined, imgO?: number | undefined, imgON?: number | undefined, imgRes?: string | undefined, len?: number | undefined, mode?: string | undefined, p?: number | undefined}; export type CreateApiV1AdminModelsPricingEstimateArgs = {body: AdminV1EstimateModelPricingRequest}; export type CreateApiV1AdminModelsPricingEstimateOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const adminV1EstimateUsedVars = z.object({ai: z.boolean().optional(), ao: z.boolean().optional(), cc: z.boolean().optional(), cc1h: z.boolean().optional(), cr: z.boolean().optional(), hasVideoIn: z.boolean().optional(), img: z.boolean().optional(), imgCr: z.boolean().optional(), imgN: z.boolean().optional(), imgO: z.boolean().optional(), imgON: z.boolean().optional(), imgRes: z.boolean().optional()}); export const adminV1EstimateModelPricingReply = z.object({costMicros: z.string().optional(), matchedTier: z.string().optional(), usedVars: adminV1EstimateUsedVars.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * EstimateModelPricing 计费表达式 dry-run: 前端 Token 估算器实时预览。 * * POST /v1/admin/models/pricing/estimate */ export const createApiV1AdminModelsPricingEstimate = async (args: CreateApiV1AdminModelsPricingEstimateArgs, options: CreateApiV1AdminModelsPricingEstimateOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/admin/models/pricing/estimate${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(adminV1EstimateModelPricingReply, await res.json()) };