|
| 1 | +import { |
| 2 | + extractProviderOptionsApiKey, |
| 3 | + getApiKeyCheckedPaths, |
| 4 | + getFirstAuthEntryValue, |
| 5 | + getGlobalOpencodeConfigCandidatePaths, |
| 6 | + resolveApiKeyFromEnvAndConfig, |
| 7 | +} from "./api-key-resolver.js"; |
| 8 | +import { sanitizeDisplayText } from "./display-sanitize.js"; |
| 9 | +import { getAuthPaths, readAuthFileCached } from "./opencode-auth.js"; |
| 10 | + |
| 11 | +import type { AuthData, ZaiAuthData } from "./types.js"; |
| 12 | + |
| 13 | +export const DEFAULT_ZHIPU_AUTH_CACHE_MAX_AGE_MS = 5_000; |
| 14 | +const ZHIPU_AUTH_KEYS = ["zhipu-coding-plan"] as const; |
| 15 | +const ZHIPU_PROVIDER_KEYS = ["zhipu", "zhipu-coding-plan", "glm-coding-plan"] as const; |
| 16 | +const ALLOWED_ZHIPU_ENV_VARS = ["ZHIPU_API_KEY", "ZHIPU_CODING_PLAN_API_KEY"] as const; |
| 17 | + |
| 18 | +export type ZhipuKeySource = |
| 19 | + | "env:ZHIPU_API_KEY" |
| 20 | + | "env:ZHIPU_CODING_PLAN_API_KEY" |
| 21 | + | "opencode.json" |
| 22 | + | "opencode.jsonc" |
| 23 | + | "auth.json"; |
| 24 | + |
| 25 | +export type ResolvedZhipuAuth = |
| 26 | + | { state: "none" } |
| 27 | + | { state: "configured"; apiKey: string } |
| 28 | + | { state: "invalid"; error: string }; |
| 29 | + |
| 30 | +export type ZhipuAuthDiagnostics = |
| 31 | + | { |
| 32 | + state: "none"; |
| 33 | + source: null; |
| 34 | + checkedPaths: string[]; |
| 35 | + authPaths: string[]; |
| 36 | + } |
| 37 | + | { |
| 38 | + state: "configured"; |
| 39 | + source: ZhipuKeySource; |
| 40 | + checkedPaths: string[]; |
| 41 | + authPaths: string[]; |
| 42 | + } |
| 43 | + | { |
| 44 | + state: "invalid"; |
| 45 | + source: "auth.json"; |
| 46 | + checkedPaths: string[]; |
| 47 | + authPaths: string[]; |
| 48 | + error: string; |
| 49 | + }; |
| 50 | + |
| 51 | +export { getGlobalOpencodeConfigCandidatePaths as getOpencodeConfigCandidatePaths } from "./api-key-resolver.js"; |
| 52 | + |
| 53 | +function getZhipuAuthEntry(auth: AuthData | null | undefined): unknown { |
| 54 | + return getFirstAuthEntryValue(auth, ZHIPU_AUTH_KEYS); |
| 55 | +} |
| 56 | + |
| 57 | +function isZhipuAuthData(value: unknown): value is ZaiAuthData { |
| 58 | + return value !== null && typeof value === "object"; |
| 59 | +} |
| 60 | + |
| 61 | +function sanitizeZhipuAuthValue(value: string): string { |
| 62 | + const sanitized = sanitizeDisplayText(value).replace(/\s+/g, " ").trim(); |
| 63 | + return (sanitized || "unknown").slice(0, 120); |
| 64 | +} |
| 65 | + |
| 66 | +export function resolveZhipuAuth(auth: AuthData | null | undefined): ResolvedZhipuAuth { |
| 67 | + const zhipu = getZhipuAuthEntry(auth); |
| 68 | + if (zhipu === null || zhipu === undefined) { |
| 69 | + return { state: "none" }; |
| 70 | + } |
| 71 | + |
| 72 | + if (!isZhipuAuthData(zhipu)) { |
| 73 | + return { state: "invalid", error: "Zhipu auth entry has invalid shape" }; |
| 74 | + } |
| 75 | + |
| 76 | + if (typeof zhipu.type !== "string") { |
| 77 | + return { state: "invalid", error: "Zhipu auth entry present but type is missing or invalid" }; |
| 78 | + } |
| 79 | + |
| 80 | + if (zhipu.type !== "api") { |
| 81 | + return { |
| 82 | + state: "invalid", |
| 83 | + error: `Unsupported Zhipu auth type: "${sanitizeZhipuAuthValue(zhipu.type)}"`, |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + const key = typeof zhipu.key === "string" ? zhipu.key.trim() : ""; |
| 88 | + if (!key) { |
| 89 | + return { state: "invalid", error: "Zhipu auth entry present but key is empty" }; |
| 90 | + } |
| 91 | + |
| 92 | + return { state: "configured", apiKey: key }; |
| 93 | +} |
| 94 | + |
| 95 | +async function resolveZhipuAuthWithSource(params?: { |
| 96 | + maxAgeMs?: number; |
| 97 | +}): Promise<{ auth: ResolvedZhipuAuth; source: ZhipuKeySource | null }> { |
| 98 | + const resolvedFromEnvOrConfig = await resolveApiKeyFromEnvAndConfig<ZhipuKeySource>({ |
| 99 | + envVars: [ |
| 100 | + { name: "ZHIPU_API_KEY", source: "env:ZHIPU_API_KEY" }, |
| 101 | + { |
| 102 | + name: "ZHIPU_CODING_PLAN_API_KEY", |
| 103 | + source: "env:ZHIPU_CODING_PLAN_API_KEY", |
| 104 | + }, |
| 105 | + ], |
| 106 | + extractFromConfig: (config) => |
| 107 | + extractProviderOptionsApiKey(config, { |
| 108 | + providerKeys: ZHIPU_PROVIDER_KEYS, |
| 109 | + allowedEnvVars: ALLOWED_ZHIPU_ENV_VARS, |
| 110 | + }), |
| 111 | + configJsonSource: "opencode.json", |
| 112 | + configJsoncSource: "opencode.jsonc", |
| 113 | + getConfigCandidates: getGlobalOpencodeConfigCandidatePaths, |
| 114 | + }); |
| 115 | + |
| 116 | + if (resolvedFromEnvOrConfig) { |
| 117 | + return { |
| 118 | + auth: { state: "configured", apiKey: resolvedFromEnvOrConfig.key }, |
| 119 | + source: resolvedFromEnvOrConfig.source, |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + const maxAgeMs = Math.max(0, params?.maxAgeMs ?? DEFAULT_ZHIPU_AUTH_CACHE_MAX_AGE_MS); |
| 124 | + const authData = await readAuthFileCached({ maxAgeMs }); |
| 125 | + const auth = resolveZhipuAuth(authData); |
| 126 | + |
| 127 | + return { |
| 128 | + auth, |
| 129 | + source: auth.state === "none" ? null : "auth.json", |
| 130 | + }; |
| 131 | +} |
| 132 | + |
| 133 | +export async function resolveZhipuAuthCached(params?: { |
| 134 | + maxAgeMs?: number; |
| 135 | +}): Promise<ResolvedZhipuAuth> { |
| 136 | + return (await resolveZhipuAuthWithSource(params)).auth; |
| 137 | +} |
| 138 | + |
| 139 | +export async function getZhipuAuthDiagnostics(params?: { |
| 140 | + maxAgeMs?: number; |
| 141 | +}): Promise<ZhipuAuthDiagnostics> { |
| 142 | + const { auth, source } = await resolveZhipuAuthWithSource(params); |
| 143 | + const checkedPaths = getApiKeyCheckedPaths({ |
| 144 | + envVarNames: [...ALLOWED_ZHIPU_ENV_VARS], |
| 145 | + getConfigCandidates: getGlobalOpencodeConfigCandidatePaths, |
| 146 | + }); |
| 147 | + const authPaths = getAuthPaths(); |
| 148 | + |
| 149 | + if (auth.state === "none") { |
| 150 | + return { |
| 151 | + state: "none", |
| 152 | + source: null, |
| 153 | + checkedPaths, |
| 154 | + authPaths, |
| 155 | + }; |
| 156 | + } |
| 157 | + |
| 158 | + if (auth.state === "invalid") { |
| 159 | + return { |
| 160 | + state: "invalid", |
| 161 | + source: "auth.json", |
| 162 | + checkedPaths, |
| 163 | + authPaths, |
| 164 | + error: auth.error, |
| 165 | + }; |
| 166 | + } |
| 167 | + |
| 168 | + return { |
| 169 | + state: "configured", |
| 170 | + source: source ?? "auth.json", |
| 171 | + checkedPaths, |
| 172 | + authPaths, |
| 173 | + }; |
| 174 | +} |
0 commit comments