import {z} from 'zod' export type GetApiV1ConsoleUsageLogsCountArgs = {search?: string | undefined, status?: string | undefined, modelId?: string | undefined, channelId?: string | undefined, startTime?: string | undefined, endTime?: string | undefined, apiKeyId?: string | undefined, modelName?: string | undefined, apiKeyName?: string | undefined}; export type GetApiV1ConsoleUsageLogsCountOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const usageV1CountLogsReply = z.object({successCount: z.string().optional(), total: z.string().optional(), totalBilledMicros: z.string().optional(), totalTokens: z.string().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 }; /** * CountLogs 返回与 ListLogs 相同筛选条件的精确卡片统计。 * * GET /v1/console/usage/logs/count */ export const getApiV1ConsoleUsageLogsCount = async (args: GetApiV1ConsoleUsageLogsCountArgs, options: GetApiV1ConsoleUsageLogsCountOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.status != null) query.append("status", String(args.status)) if (args.modelId != null) query.append("modelId", String(args.modelId)) if (args.channelId != null) query.append("channelId", String(args.channelId)) if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (args.apiKeyId != null) query.append("apiKeyId", String(args.apiKeyId)) if (args.modelName != null) query.append("modelName", String(args.modelName)) if (args.apiKeyName != null) query.append("apiKeyName", String(args.apiKeyName)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/console/usage/logs/count${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(usageV1CountLogsReply, await res.json()) };