Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

9.3 KB
Raw Blame
import {
  Alert,
  Button,
  CircularProgress,
  Dialog,
  DialogActions,
  DialogContent,
  DialogTitle,
  FormControlLabel,
  MenuItem,
  Stack,
  Switch,
  TextField,
} from "@mui/material";
import { useEffect, useState } from "react";
import {
  type Controller,
  createManagedAgent,
  type Definition,
  type Isolation,
  type DesiredState,
  type ManagedAgent,
  type Provider,
  type ProviderInfo,
  updateManagedAgent,
} from "../../data/management";

const NAME_PATTERN = /^[a-z0-9][a-z0-9._-]*$/;

interface Form {
  name: string;
  description: string;
  controllerId: string;
  provider: Provider;
  model: string;
  instructions: string;
  directory: string;
  autoMode: boolean;
  autoApprove: boolean;
  isolation: Isolation;
  /** Not edited here; kept as it is so saving does not clear it. */
  advancedConfig: Definition["advanced_config"];
  running: boolean;
}

function initialForm(
  agent: ManagedAgent | null,
  controllers: Controller[],
  providers: ProviderInfo[],
): Form {
  if (agent)
    return {
      name: agent.name,
      description: agent.description,
      controllerId: agent.controller_id ?? "",
      provider: agent.definition.provider,
      model: agent.definition.model ?? "",
      instructions: agent.definition.instructions,
      directory: agent.definition.directory ?? "",
      autoMode: agent.definition.auto_mode,
      autoApprove: agent.definition.auto_approve,
      isolation: agent.definition.isolation,
      advancedConfig: agent.definition.advanced_config,
      running: agent.desired_state === "running",
    };
  const online = controllers.find((c) => c.state === "online");
  return {
    name: "",
    description: "",
    controllerId: online?.id ?? "",
    provider: providers[0]?.id ?? "",
    model: "",
    instructions: "",
    directory: "",
    autoMode: true,
    autoApprove: false,
    isolation: "shared",
    advancedConfig: {},
    running: true,
  };
}

function definitionFrom(form: Form): Definition {
  return {
    provider: form.provider,
    model: form.model.trim() || null,
    instructions: form.instructions,
    auto_mode: form.autoMode,
    auto_approve: form.autoApprove,
    directory: form.directory.trim() || null,
    isolation: form.isolation,
    advanced_config: form.advancedConfig,
  };
}

/**
 * Creates a managed agent, or edits one: its definition, the machine it runs
 * on, and whether it should be running. Server refusals (an offline machine,
 * a provider that is not installed or not logged in there) are shown as the
 * server words them.
 */
export default function ManagedAgentDialog({
  open,
  agent,
  controllers,
  providers,
  onClose,
  onSaved,
}: {
  open: boolean;
  /** The agent to edit, or null to create one. */
  agent: ManagedAgent | null;
  controllers: Controller[];
  /** The providers the server runs; empty until they have loaded. */
  providers: ProviderInfo[];
  onClose: () => void;
  onSaved: () => void;
}) {
  const [form, setForm] = useState<Form>(() => initialForm(agent, controllers, providers));
  const [saving, setSaving] = useState(false);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    if (!open) return;
    setForm(initialForm(agent, controllers, providers));
    setError(null);
    // Reset only when the dialog opens or switches agent, not on every status refresh.
  }, [open, agent?.agent_id]);

  const set = <K extends keyof Form>(key: K, value: Form[K]) =>
    setForm((current) => ({ ...current, [key]: value }));

  const placeable = controllers.filter(
    (c) => c.state !== "revoked" || c.id === agent?.controller_id,
  );
  const nameValid = agent !== null || NAME_PATTERN.test(form.name);
  const canSave =
    nameValid &&
    (agent !== null || form.description.trim() !== "") &&
    form.provider !== "" &&
    !saving;
  // An agent keeps its provider even if the server no longer lists it.
  const providerChoices =
    form.provider === "" || providers.some((p) => p.id === form.provider)
      ? providers
      : [...providers, { id: form.provider, label: form.provider, advanced_fields: [] }];

  const save = async () => {
    setSaving(true);
    setError(null);
    const desired: DesiredState = form.running ? "running" : "stopped";
    const controllerId = form.controllerId || null;
    try {
      if (agent === null)
        await createManagedAgent({
          name: form.name,
          description: form.description.trim(),
          display_name: null,
          controller_id: controllerId,
          desired_state: desired,
          definition: definitionFrom(form),
        });
      else
        await updateManagedAgent(agent.agent_id, {
          definition: definitionFrom(form),
          desired_state: desired,
          controller_id: controllerId,
        });
      onSaved();
      onClose();
    } catch (err) {
      setError(err instanceof Error ? err.message : String(err));
    } finally {
      setSaving(false);
    }
  };

  return (
    <Dialog open={open} onClose={onClose} maxWidth="sm" fullWidth>
      <DialogTitle>{agent ? `Edit ${agent.name}` : "New managed agent"}</DialogTitle>
      <DialogContent>
        <Stack spacing={2} sx={{ mt: 1 }}>
          {error && <Alert severity="error">{error}</Alert>}
          {agent === null && (
            <>
              <TextField
                label="Name"
                value={form.name}
                onChange={(e) => set("name", e.target.value)}
                error={form.name !== "" && !nameValid}
                helperText="Lowercase letters, digits, '.', '-' and '_'. How the agent is addressed in rooms."
                required
                autoFocus
              />
              <TextField
                label="Description"
                value={form.description}
                onChange={(e) => set("description", e.target.value)}
                required
              />
            </>
          )}
          <TextField
            select
            label="Device"
            value={form.controllerId}
            onChange={(e) => set("controllerId", e.target.value)}
            helperText={
              placeable.length === 0
                ? "No device yet: add one first, or save the agent unplaced."
                : "Where the agent runs."
            }
          >
            <MenuItem value="">
              <em>Not placed</em>
            </MenuItem>
            {placeable.map((c) => (
              <MenuItem key={c.id} value={c.id}>
                {c.name} ({c.state})
              </MenuItem>
            ))}
          </TextField>
          <TextField
            select
            label="Provider"
            value={form.provider}
            onChange={(e) => set("provider", e.target.value as Provider)}
            helperText={providers.length === 0 ? "Loading providers…" : undefined}
          >
            {providerChoices.map((p) => (
              <MenuItem key={p.id} value={p.id}>
                {p.label}
              </MenuItem>
            ))}
          </TextField>
          <TextField
            label="Model"
            value={form.model}
            onChange={(e) => set("model", e.target.value)}
            helperText="Leave empty for the provider's default."
          />
          <TextField
            label="Instructions"
            value={form.instructions}
            onChange={(e) => set("instructions", e.target.value)}
            multiline
            minRows={3}
          />
          <TextField
            label="Working directory"
            value={form.directory}
            onChange={(e) => set("directory", e.target.value)}
            helperText="An absolute path that exists on the device. Empty uses a workspace the controller creates."
          />
          <TextField
            select
            label="Isolation"
            value={form.isolation}
            onChange={(e) => set("isolation", e.target.value as Isolation)}
            helperText="Cloud devices run every agent isolated."
          >
            <MenuItem value="shared">Shared: runs inside the device's controller</MenuItem>
            <MenuItem value="isolated">Isolated: runs as a process of its own</MenuItem>
          </TextField>
          <FormControlLabel
            control={
              <Switch
                checked={form.autoMode}
                disabled={form.autoApprove}
                onChange={(e) => set("autoMode", e.target.checked)}
              />
            }
            label="Auto mode (the provider reviews each action: safe ones run, risky ones still ask)"
          />
          <FormControlLabel
            control={
              <Switch
                checked={form.autoApprove}
                onChange={(e) => set("autoApprove", e.target.checked)}
              />
            }
            label="Bypass permissions (unsafe: runs every action without asking; overrides auto mode)"
          />
          <FormControlLabel
            control={
              <Switch checked={form.running} onChange={(e) => set("running", e.target.checked)} />
            }
            label="Running"
          />
        </Stack>
      </DialogContent>
      <DialogActions>
        <Button onClick={onClose}>Cancel</Button>
        <Button
          variant="contained"
          onClick={save}
          disabled={!canSave}
          startIcon={saving ? <CircularProgress size={16} /> : undefined}
        >
          {agent ? "Save" : "Create"}
        </Button>
      </DialogActions>
    </Dialog>
  );
}