-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathcode-agent-settings.tsx
More file actions
180 lines (163 loc) · 7.36 KB
/
code-agent-settings.tsx
File metadata and controls
180 lines (163 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"use client";
import React, { useState, useEffect } from "react";
import CodeMirror from "@uiw/react-codemirror";
import { javascript } from "@codemirror/lang-javascript";
import { githubLight } from "@uiw/codemirror-theme-github";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Label } from "@/components/ui/label";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { AlertCircle, Save } from "lucide-react";
import { toast } from "sonner";
import { SupabaseService } from "@/lib/supabase-service";
import { useUserProfile } from "@/hooks/useUserProfile";
interface CodeAgentConfig {
claudeCode?: Record<string, string>;
codexCLI?: Record<string, string>;
}
const DEFAULT_CLAUDE_ENV = {
ANTHROPIC_API_KEY: "",
// Add other Claude-specific env vars here if needed
};
const DEFAULT_CODEX_ENV = {
OPENAI_API_KEY: "",
DISABLE_SANDBOX: "yes",
CONTINUE_ON_BROWSER: "no",
// Add other Codex-specific env vars here if needed
};
export function CodeAgentSettings() {
const { profile, refreshProfile } = useUserProfile();
const [claudeEnv, setClaudeEnv] = useState("");
const [codexEnv, setCodexEnv] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [errors, setErrors] = useState<{ claude?: string; codex?: string }>({});
// Load settings from profile on mount
useEffect(() => {
if (profile?.preferences) {
const prefs = profile.preferences as CodeAgentConfig;
setClaudeEnv(JSON.stringify(prefs.claudeCode || DEFAULT_CLAUDE_ENV, null, 2));
setCodexEnv(JSON.stringify(prefs.codexCLI || DEFAULT_CODEX_ENV, null, 2));
} else {
setClaudeEnv(JSON.stringify(DEFAULT_CLAUDE_ENV, null, 2));
setCodexEnv(JSON.stringify(DEFAULT_CODEX_ENV, null, 2));
}
}, [profile]);
const validateJSON = (value: string, agent: "claude" | "codex") => {
try {
JSON.parse(value);
setErrors(prev => ({ ...prev, [agent]: undefined }));
return true;
} catch (e) {
setErrors(prev => ({ ...prev, [agent]: "Invalid JSON format" }));
return false;
}
};
const handleSave = async () => {
// Validate both JSONs
const isClaudeValid = validateJSON(claudeEnv, "claude");
const isCodexValid = validateJSON(codexEnv, "codex");
if (!isClaudeValid || !isCodexValid) {
toast.error("Please fix JSON errors before saving");
return;
}
setIsLoading(true);
try {
const claudeConfig = JSON.parse(claudeEnv);
const codexConfig = JSON.parse(codexEnv);
const preferences: CodeAgentConfig = {
claudeCode: claudeConfig,
codexCLI: codexConfig,
};
// Merge with existing preferences if any
const existingPrefs = (profile?.preferences || {}) as Record<string, any>;
const mergedPrefs = {
...existingPrefs,
...preferences,
};
await SupabaseService.updateUserProfile({ preferences: mergedPrefs });
await refreshProfile();
toast.success("Code agent settings saved successfully");
} catch (error) {
console.error("Failed to save settings:", error);
toast.error("Failed to save settings");
} finally {
setIsLoading(false);
}
};
return (
<div className="space-y-6">
<Card>
<CardHeader>
<CardTitle>Code Agent Settings</CardTitle>
<CardDescription>
Configure environment variables for each code agent. These settings will be used when creating containers.
</CardDescription>
</CardHeader>
<CardContent className="space-y-6">
<Alert>
<AlertCircle className="h-4 w-4" />
<AlertDescription>
<strong>Important:</strong> Store sensitive API keys here instead of hardcoding them. Personal settings will override default environment variables.
</AlertDescription>
</Alert>
{/* Claude Code Settings */}
<div className="space-y-2">
<Label htmlFor="claude-env">Claude Code Environment Variables</Label>
<div className="border rounded-lg overflow-hidden">
<CodeMirror
id="claude-env"
value={claudeEnv}
height="200px"
extensions={[javascript({ jsx: false })]}
theme={githubLight}
onChange={(value) => {
setClaudeEnv(value);
validateJSON(value, "claude");
}}
placeholder={JSON.stringify(DEFAULT_CLAUDE_ENV, null, 2)}
/>
</div>
{errors.claude && (
<p className="text-sm text-red-500 mt-1">{errors.claude}</p>
)}
<p className="text-sm text-muted-foreground">
Configure environment variables for Claude Code CLI (@anthropic-ai/claude-code)
</p>
</div>
{/* Codex CLI Settings */}
<div className="space-y-2">
<Label htmlFor="codex-env">Codex CLI Environment Variables</Label>
<div className="border rounded-lg overflow-hidden">
<CodeMirror
id="codex-env"
value={codexEnv}
height="200px"
extensions={[javascript({ jsx: false })]}
theme={githubLight}
onChange={(value) => {
setCodexEnv(value);
validateJSON(value, "codex");
}}
placeholder={JSON.stringify(DEFAULT_CODEX_ENV, null, 2)}
/>
</div>
{errors.codex && (
<p className="text-sm text-red-500 mt-1">{errors.codex}</p>
)}
<p className="text-sm text-muted-foreground">
Configure environment variables for Codex CLI (@openai/codex)
</p>
</div>
<Button
onClick={handleSave}
disabled={isLoading || !!errors.claude || !!errors.codex}
className="w-full"
>
<Save className="w-4 h-4 mr-2" />
{isLoading ? "Saving..." : "Save Settings"}
</Button>
</CardContent>
</Card>
</div>
);
}