export function parseCommaArgs(value: string): string[] { return value .split(",") .map((item) => item.trim()) .filter(Boolean); } export function parseEnvVars(text: string): Record { const env: Record = {}; for (const line of text.split(/\r?\n/)) { const trimmed = line.trim(); if (!trimmed || trimmed.startsWith("#")) continue; const eq = trimmed.indexOf("="); if (eq <= 0) continue; const key = trimmed.slice(0, eq).trim(); const value = trimmed.slice(eq + 1); if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) continue; env[key] = value; } return env; } export function parseEnvBindings(bindings: unknown): Record { if (typeof bindings !== "object" || bindings === null || Array.isArray(bindings)) return {}; const env: Record = {}; for (const [key, raw] of Object.entries(bindings)) { if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) continue; if (typeof raw === "string") { env[key] = { type: "plain", value: raw }; continue; } if (typeof raw !== "object" || raw === null || Array.isArray(raw)) continue; const rec = raw as Record; if (rec.type === "plain" && typeof rec.value === "string") { env[key] = { type: "plain", value: rec.value }; continue; } if (rec.type === "secret_ref" && typeof rec.secretId === "string") { env[key] = { type: "secret_ref", secretId: rec.secretId, ...(typeof rec.version === "number" || rec.version === "latest" ? { version: rec.version } : {}), }; } } return env; } export function parseJsonObject(text: string): Record | null { const trimmed = text.trim(); if (!trimmed) return null; try { const parsed = JSON.parse(trimmed); if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null; return parsed as Record; } catch { return null; } }