"use client"; import { useState } from "react"; import { Loader2, Save, Plus, Trash2, Eye, EyeOff, Lock, } from "lucide-react"; import type { Agent } from "@multica/core/types"; import { Button } from "@multica/ui/components/ui/button"; import { Input } from "@multica/ui/components/ui/input"; import { Label } from "@multica/ui/components/ui/label"; import { toast } from "sonner"; let nextEnvId = 0; interface EnvEntry { id: number; key: string; value: string; visible: boolean; } function envMapToEntries(env: Record): EnvEntry[] { return Object.entries(env).map(([key, value]) => ({ id: nextEnvId++, key, value, visible: false, })); } function entriesToEnvMap(entries: EnvEntry[]): Record { const map: Record = {}; for (const entry of entries) { const key = entry.key.trim(); if (key) { map[key] = entry.value; } } return map; } export function EnvTab({ agent, readOnly = false, onSave, }: { agent: Agent; readOnly?: boolean; onSave: (updates: Partial) => Promise; }) { const [envEntries, setEnvEntries] = useState( envMapToEntries(agent.custom_env ?? {}), ); const [saving, setSaving] = useState(false); const currentEnvMap = entriesToEnvMap(envEntries); const originalEnvMap = agent.custom_env ?? {}; const dirty = JSON.stringify(currentEnvMap) !== JSON.stringify(originalEnvMap); const addEnvEntry = () => { setEnvEntries([ ...envEntries, { id: nextEnvId++, key: "", value: "", visible: true }, ]); }; const removeEnvEntry = (index: number) => { setEnvEntries(envEntries.filter((_, i) => i !== index)); }; const updateEnvEntry = ( index: number, field: "key" | "value", val: string, ) => { setEnvEntries( envEntries.map((entry, i) => i === index ? { ...entry, [field]: val } : entry, ), ); }; const toggleEnvVisibility = (index: number) => { setEnvEntries( envEntries.map((entry, i) => i === index ? { ...entry, visible: !entry.visible } : entry, ), ); }; const handleSave = async () => { const keys = envEntries.filter((e) => e.key.trim()).map((e) => e.key.trim()); const uniqueKeys = new Set(keys); if (uniqueKeys.size < keys.length) { toast.error("Duplicate environment variable keys"); return; } setSaving(true); try { await onSave({ custom_env: currentEnvMap }); toast.success("Environment variables saved"); } catch { toast.error("Failed to save environment variables"); } finally { setSaving(false); } }; if (readOnly) { return (

Injected into the agent process at launch. Values are hidden — only the agent owner or workspace admin can view and edit them.

{envEntries.length > 0 ? (
{envEntries.map((entry) => (
))}
) : (

No environment variables configured.

)}
); } return (

Injected into the agent process at launch (e.g. ANTHROPIC_API_KEY, ANTHROPIC_BASE_URL)

{envEntries.length > 0 && (
{envEntries.map((entry, index) => (
updateEnvEntry(index, "key", e.target.value)} placeholder="KEY" className="w-[40%] font-mono text-xs" />
updateEnvEntry(index, "value", e.target.value) } placeholder="value" className="pr-8 font-mono text-xs" />
))}
)}
); }