import {z} from 'zod' export type ModelConfig = {temperature?: (number | null) | undefined, max_tokens?: (number | null) | undefined, top_p?: (number | null) | undefined, stop_sequences?: (Array | null) | undefined, reasoning_effort?: ('none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max' | null) | undefined, request_timeout_ms?: (number | null) | undefined, thinking_budget?: (number | null) | undefined}; export type ModelChainEntry = {model: string, config?: ModelConfig | undefined}; export type OrgPolicyJudgeModelUpdate = {model_settings?: (ModelChainEntry | null) | undefined}; export type UpdateApiV1PoliciesJudgeModelArgs = {body: OrgPolicyJudgeModelUpdate}; export type UpdateApiV1PoliciesJudgeModelOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const modelConfig = z.object({temperature: z.number().gte(0).lte(2).nullable().optional(), max_tokens: z.number().int().gte(1).nullable().optional(), top_p: z.number().gte(0).lte(1).nullable().optional(), stop_sequences: z.array(z.string()).nullable().optional(), reasoning_effort: z.enum(["none", "minimal", "low", "medium", "high", "xhigh", "max"]).nullable().optional(), request_timeout_ms: z.number().int().gte(1000).lte(20000).nullable().optional(), thinking_budget: z.number().int().gte(1).nullable().optional()}); export const modelChainEntry = z.object({model: z.string(), config: modelConfig.optional()}); export const orgPolicyJudgeModelResponse = z.object({model_settings: modelChainEntry.nullable().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 }; /** * Set/clear the org-level llm_judge model default * * PUT /v1/policies/judge-model */ export const updateApiV1PoliciesJudgeModel = async (args: UpdateApiV1PoliciesJudgeModelArgs, options: UpdateApiV1PoliciesJudgeModelOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/policies/judge-model${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(orgPolicyJudgeModelResponse, await res.json()) };