import { parse as parseToml, stringify as stringifyToml } from "smol-toml"; import type { DeepLinkImportRequest } from "@/lib/api/deeplink"; import { decodeBase64Utf8 } from "@/lib/utils/base64"; import { isSensitiveConfigKey, maskSensitiveValue } from "@/utils/deeplinkRisk"; export interface ParsedDeepLinkConfig { type: "claude" | "codex" | "gemini" | "grokbuild"; env?: Record; auth?: Record; tomlConfig?: string; } const maskStructuredSecrets = ( value: unknown, key = "", inheritedSensitive = false, ): unknown => { const sensitive = inheritedSensitive || isSensitiveConfigKey(key); if (typeof value === "string") { return sensitive ? maskSensitiveValue(value) : value; } if (Array.isArray(value)) { return value.map((item) => maskStructuredSecrets(item, key, sensitive)); } if (value && typeof value === "object") { return Object.fromEntries( Object.entries(value as Record).map( ([childKey, childValue]) => [ childKey, maskStructuredSecrets(childValue, childKey, sensitive), ], ), ); } return value; }; const sanitizeTomlForPreview = (configToml: string): string => { const parsed = parseToml(configToml) as Record; return `${stringifyToml(maskStructuredSecrets(parsed) as Record).trim()}\n`; }; export function parseDeepLinkConfigPreview( request: Pick, ): ParsedDeepLinkConfig | null { if (!request.config) return null; try { const decoded = decodeBase64Utf8(request.config); const format = request.configFormat?.trim().toLowerCase(); if (request.app === "grokbuild" && format === "toml") { return { type: "grokbuild", tomlConfig: sanitizeTomlForPreview(decoded), }; } const parsed = JSON.parse(decoded) as Record; if (request.app === "claude") { return { type: "claude", env: (parsed.env as Record) || {}, }; } if (request.app === "codex") { const config = typeof parsed.config === "string" ? parsed.config : ""; return { type: "codex", auth: (parsed.auth as Record) || {}, tomlConfig: config ? sanitizeTomlForPreview(config) : "", }; } if (request.app === "gemini") { return { type: "gemini", env: parsed as Record, }; } if (request.app === "grokbuild") { const config = typeof parsed.config === "string" ? parsed.config : stringifyToml(parsed); return { type: "grokbuild", tomlConfig: sanitizeTomlForPreview(config), }; } return null; } catch (error) { console.error("Failed to parse deep link config preview:", error); return null; } }