import { z } from "zod"; export const THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"] as const; export type AgentThinkingLevel = (typeof THINKING_LEVELS)[number]; /** * 设置页 → Agent 运行参数。apiKey 为调用方从 secret store 解密后的明文, * 只在请求时经 provider auth.resolve 交给 SDK,不进入事件、日志、错误信息。 */ export const AgentSettingsSchema = z.object({ provider: z.enum(["anthropic", "openai-compatible"]), /** Anthropic 写不带 /v1 的根地址;OpenAI 兼容写带 /v1 的地址 */ baseUrl: z.string().url().optional(), apiKey: z.string().min(1), model: z.string().min(1), thinkingLevel: z.enum(THINKING_LEVELS).default("off"), /** 上下文预算 = 模型 contextWindow(可以比上游真实窗口小) */ contextBudgetTokens: z.number().int().min(1024).default(128_000), /** 单次回复最大输出 token */ maxOutputTokens: z.number().int().min(256).default(8192), compaction: z .object({ mode: z.enum(["threshold", "manual"]), /** 上下文占用达到预算的该比例时自动压缩(0.5~0.95) */ threshold: z.number().min(0.5).max(0.95).default(0.8), }) .default({ mode: "threshold", threshold: 0.8 }), /** v0.1 未实现子代理;false 时忽略,true 也只记录(由 agent.delegate 能力后续实现) */ subagentsEnabled: z.boolean().default(false), maxToolCalls: z.number().int().min(1).default(40), maxDurationMs: z .number() .int() .min(1000) .default(15 * 60_000), costBudget: z .object({ maxTotalTokens: z.number().int().positive().optional(), maxCostUsd: z.number().positive().optional(), }) .optional(), /** 单次请求超时 */ requestTimeoutMs: z.number().int().positive().default(120_000), maxRetries: z.number().int().min(0).max(10).default(2), }); export type AgentSettings = z.input; export type ResolvedAgentSettings = z.output; export function resolveAgentSettings(settings: AgentSettings): ResolvedAgentSettings { return AgentSettingsSchema.parse(settings); } /** 用于日志/展示:去掉 apiKey */ export function describeSettings(settings: ResolvedAgentSettings): Omit & { apiKey: "[REDACTED]"; } { return { ...settings, apiKey: "[REDACTED]" }; }