Repository navigation
feat(desktop): add env editor to agent settings #9310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lacherogwu
wants to merge
8
commits into
ParadevAI:main
Choose a base branch
from
lacherogwu:fix/agent-env-editor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+576
−14
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c87c0e8
feat(desktop): add env editor to agent settings, seed trust through e…
lacherogwu 0ad7b65
fix(desktop): harden agent env editor saves and env-wrapper parsing
lacherogwu dd9fbd9
fix(desktop): settle env editor saves after persistence
lacherogwu e305bfc
fix(desktop): serialize agent env editor saves
lacherogwu d3cc7a2
fix(desktop): make the env editor the only env writer
lacherogwu 8d7ce95
fix(desktop): adopt saved env only when the editor is idle and clean
lacherogwu 0072c7b
fix(desktop): re-check env adoption when editor rows become clean
lacherogwu 97df93a
refactor: move trust change to #9394
lacherogwu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
262 changes: 262 additions & 0 deletions
262
.../settings/agents/components/V2AgentsSettings/components/AgentEnvEditor/AgentEnvEditor.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,262 @@ | ||
| import { Trans, useLingui } from "@lingui/react/macro"; | ||
| import { Button } from "@superset/ui/button"; | ||
| import { Input } from "@superset/ui/input"; | ||
| import { Plus, X } from "lucide-react"; | ||
| import { | ||
| type Ref, | ||
| useEffect, | ||
| useImperativeHandle, | ||
| useRef, | ||
| useState, | ||
| } from "react"; | ||
| import { areEnvMapsEqual } from "renderer/lib/agent-launch-command"; | ||
|
|
||
| const ENV_KEY = /^[A-Za-z_][A-Za-z0-9_]*$/; | ||
|
|
||
| interface EnvRow { | ||
| id: string; | ||
| key: string; | ||
| value: string; | ||
| saved: { key: string; value: string } | null; | ||
| } | ||
|
|
||
| function toRows(env: Record<string, string>): EnvRow[] { | ||
| return Object.entries(env).map(([key, value]) => ({ | ||
| id: crypto.randomUUID(), | ||
| key, | ||
| value, | ||
| saved: { key, value }, | ||
| })); | ||
| } | ||
|
|
||
| /** Rows that keep their saved name claim it before renamed or new rows. */ | ||
| function acceptedRowIds(rows: readonly EnvRow[]): Set<string> { | ||
| const takenKeys = new Set<string>(); | ||
| const ids = new Set<string>(); | ||
| const accept = (row: EnvRow) => { | ||
| const key = row.key.trim(); | ||
| if (ids.has(row.id) || !ENV_KEY.test(key) || takenKeys.has(key)) return; | ||
| takenKeys.add(key); | ||
| ids.add(row.id); | ||
| }; | ||
| for (const row of rows) if (row.saved?.key === row.key.trim()) accept(row); | ||
| for (const row of rows) accept(row); | ||
| return ids; | ||
| } | ||
|
|
||
| /** Rows whose name is not accepted keep their last saved entry. */ | ||
| function toEnv(rows: readonly EnvRow[]): Record<string, string> { | ||
| const accepted = acceptedRowIds(rows); | ||
| const entries = new Map<string, string>(); | ||
| for (const row of rows) { | ||
| if (accepted.has(row.id)) entries.set(row.key.trim(), row.value); | ||
| } | ||
| for (const row of rows) { | ||
| if (!accepted.has(row.id) && row.saved && !entries.has(row.saved.key)) { | ||
| entries.set(row.saved.key, row.saved.value); | ||
| } | ||
| } | ||
| return Object.fromEntries(entries); | ||
| } | ||
|
|
||
| function markSaved(rows: EnvRow[], submitted: readonly EnvRow[]): EnvRow[] { | ||
| const accepted = acceptedRowIds(submitted); | ||
| return rows.map((row) => { | ||
| const sent = submitted.find((candidate) => candidate.id === row.id); | ||
| return sent && accepted.has(row.id) | ||
| ? { ...row, saved: { key: sent.key.trim(), value: sent.value } } | ||
| : row; | ||
| }); | ||
| } | ||
|
|
||
| function withAssignments( | ||
| rows: readonly EnvRow[], | ||
| assignments: Record<string, string>, | ||
| ): EnvRow[] { | ||
| const remaining = new Map(Object.entries(assignments)); | ||
| const accepted = acceptedRowIds(rows); | ||
| const updated = rows.map((row) => { | ||
| const key = row.key.trim(); | ||
| if (!accepted.has(row.id) || !remaining.has(key)) return row; | ||
| const value = remaining.get(key) ?? ""; | ||
| remaining.delete(key); | ||
| return { ...row, value }; | ||
| }); | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
|
||
| const added = [...remaining].map(([key, value]) => ({ | ||
| id: crypto.randomUUID(), | ||
| key, | ||
| value, | ||
| saved: null, | ||
| })); | ||
| return [...updated, ...added]; | ||
| } | ||
|
|
||
| function hasUnsavedEdits(rows: readonly EnvRow[]): boolean { | ||
| return rows.some((row) => | ||
| row.saved === null | ||
| ? row.key.trim().length > 0 || row.value.length > 0 | ||
| : row.key.trim() !== row.saved.key || row.value !== row.saved.value, | ||
| ); | ||
| } | ||
|
|
||
| export interface AgentEnvEditorHandle { | ||
| /** Sets variables, e.g. inline `VAR=value` prefixes from the command field. */ | ||
| assign: (assignments: Record<string, string>) => void; | ||
| } | ||
|
|
||
| interface AgentEnvEditorProps { | ||
| idPrefix: string; | ||
| env: Record<string, string>; | ||
| onCommit: (env: Record<string, string>) => Promise<unknown>; | ||
| ref?: Ref<AgentEnvEditorHandle>; | ||
| } | ||
|
|
||
| export function AgentEnvEditor({ | ||
| idPrefix, | ||
| env, | ||
| onCommit, | ||
| ref, | ||
| }: AgentEnvEditorProps) { | ||
| const { t } = useLingui(); | ||
| const variableNameLabel = t({ message: "Variable name" }); | ||
| const variableValueLabel = t({ message: "Variable value" }); | ||
| const [rows, setRows] = useState(() => toRows(env)); | ||
| const rowsRef = useRef(rows); | ||
| rowsRef.current = rows; | ||
| const saves = useRef({ inFlight: false, queued: false, lastSent: env }); | ||
| const accepted = acceptedRowIds(rows); | ||
|
|
||
| // While a save is pending or rows hold unsaved edits, the rows are the | ||
| // source of truth: a full-config response from another save can carry an | ||
| // older env. | ||
| useEffect(() => { | ||
| const state = saves.current; | ||
| if (state.inFlight || state.queued) return; | ||
| if (hasUnsavedEdits(rows)) return; | ||
| if (areEnvMapsEqual(env, state.lastSent)) return; | ||
| state.lastSent = env; | ||
| setRows(toRows(env)); | ||
| }, [env, rows]); | ||
|
|
||
| const send = (submitted: readonly EnvRow[]) => { | ||
| const state = saves.current; | ||
| const nextEnv = toEnv(submitted); | ||
| if (areEnvMapsEqual(nextEnv, state.lastSent)) { | ||
| setRows((current) => markSaved(current, submitted)); | ||
| return; | ||
| } | ||
| const previous = state.lastSent; | ||
| state.inFlight = true; | ||
| state.lastSent = nextEnv; | ||
| onCommit(nextEnv) | ||
| .then( | ||
| () => setRows((current) => markSaved(current, submitted)), | ||
| () => { | ||
| state.lastSent = previous; | ||
| }, | ||
| ) | ||
| .finally(() => { | ||
| state.inFlight = false; | ||
| if (state.queued) { | ||
| state.queued = false; | ||
| send(rowsRef.current); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| const commit = (next: readonly EnvRow[]) => { | ||
| if (saves.current.inFlight) saves.current.queued = true; | ||
| else send(next); | ||
| }; | ||
|
|
||
| useImperativeHandle(ref, () => ({ | ||
| assign: (assignments) => { | ||
| const next = withAssignments(rowsRef.current, assignments); | ||
| rowsRef.current = next; | ||
| setRows(next); | ||
| commit(next); | ||
| }, | ||
| })); | ||
|
|
||
| const isInvalid = (row: EnvRow) => | ||
| !accepted.has(row.id) && (row.key.trim().length > 0 || row.saved !== null); | ||
|
|
||
| const updateRow = (id: string, patch: Partial<EnvRow>) => | ||
| setRows((current) => | ||
| current.map((row) => (row.id === id ? { ...row, ...patch } : row)), | ||
| ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| const removeRow = (id: string) => { | ||
| const next = rows.filter((row) => row.id !== id); | ||
| rowsRef.current = next; | ||
| setRows(next); | ||
| commit(next); | ||
| }; | ||
|
|
||
| const addRow = () => | ||
| setRows((current) => [ | ||
| ...current, | ||
| { id: crypto.randomUUID(), key: "", value: "", saved: null }, | ||
| ]); | ||
|
|
||
| return ( | ||
| <div className="space-y-2"> | ||
| <p className="text-xs text-muted-foreground"> | ||
| <Trans> | ||
| Set when this agent launches, e.g. <code>CLAUDE_CONFIG_DIR</code> to | ||
| use another Claude login. | ||
| </Trans> | ||
| </p> | ||
| {rows.length === 0 ? ( | ||
| <p className="rounded-md border border-border/70 px-3 py-2 text-xs text-muted-foreground"> | ||
| <Trans>No variables set yet.</Trans> | ||
| </p> | ||
| ) : ( | ||
| rows.map((row) => ( | ||
| <div key={row.id} className="flex items-center gap-2"> | ||
| <Input | ||
| id={`${idPrefix}-env-key-${row.id}`} | ||
| className="w-2/5 font-mono text-xs" | ||
| value={row.key} | ||
| onChange={(e) => updateRow(row.id, { key: e.target.value })} | ||
| onBlur={() => commit(rows)} | ||
| aria-label={variableNameLabel} | ||
| aria-invalid={isInvalid(row)} | ||
| placeholder={variableNameLabel} | ||
| spellCheck={false} | ||
| /> | ||
| <Input | ||
| id={`${idPrefix}-env-value-${row.id}`} | ||
| className="flex-1 font-mono text-xs" | ||
| value={row.value} | ||
| onChange={(e) => updateRow(row.id, { value: e.target.value })} | ||
| onBlur={() => commit(rows)} | ||
| aria-label={variableValueLabel} | ||
| placeholder={variableValueLabel} | ||
| spellCheck={false} | ||
| /> | ||
| <Button | ||
| type="button" | ||
| variant="ghost" | ||
| size="icon-sm" | ||
| onClick={() => removeRow(row.id)} | ||
| aria-label={t({ message: "Remove variable" })} | ||
| > | ||
| <X className="size-3.5" /> | ||
| </Button> | ||
| </div> | ||
| )) | ||
| )} | ||
| <Button | ||
| type="button" | ||
| variant="outline" | ||
| size="xs" | ||
| onClick={addRow} | ||
| className="gap-1.5" | ||
| > | ||
| <Plus className="size-3.5" /> | ||
| <Trans>Add variable</Trans> | ||
| </Button> | ||
| </div> | ||
| ); | ||
| } | ||
1 change: 1 addition & 0 deletions
1
...henticated/settings/agents/components/V2AgentsSettings/components/AgentEnvEditor/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { AgentEnvEditor, type AgentEnvEditorHandle } from "./AgentEnvEditor"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.