Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

2.4 KB
Raw Blame
import { useCallback, useEffect, useState } from "react";
import { translate } from "@/i18n";

/**
 * What keeps running after the desktop window closes — GET/PUT
 * /api/settings/background (dodaios/core/background_service.py).
 */
export interface BackgroundAgentsConfig {
  keep_agents_running: boolean;
  persistent_server: boolean;
  server_url: string;
  running_as_server: boolean;
  background_only_at_login: boolean;
  autostart_enabled: boolean;
  /** This page is served by the windowless background service itself. */
  running_as_service: boolean;
  /** False on a dev instance: it stops with its window, by design. */
  supported: boolean;
  /** The work a quit would hand to the background service right now. */
  work: { routines: number; running: number; channels: string[] };
}

export type BackgroundAgentsPatch = Partial<
  Pick<BackgroundAgentsConfig,
    "keep_agents_running" | "background_only_at_login" | "persistent_server" | "server_url">
>;

/** Loads the background-agents settings and exposes save(). Mirrors useAutostart. */
export function useBackgroundAgents() {
  const [config, setConfig] = useState<BackgroundAgentsConfig | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  const refetch = useCallback(async () => {
    setError(null);
    try {
      const res = await fetch("/api/settings/background");
      if (!res.ok) throw new Error(`HTTP ${res.status}`);
      setConfig((await res.json()) as BackgroundAgentsConfig);
    } catch (e) {
      setError((e as Error).message);
    } finally {
      setLoading(false);
    }
  }, []);

  useEffect(() => {
    void refetch();
  }, [refetch]);

  const save = useCallback(async (patch: BackgroundAgentsPatch) => {
    const res = await fetch("/api/settings/background", {
      method: "PUT",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(patch),
    });
    const body = await res.json().catch(() => ({}));
    if (!res.ok) {
      const detail = body.detail;
      const message = typeof detail === "string" ? detail
        : Array.isArray(detail)
          ? detail.map((item: { msg?: string }) => item.msg ?? translate("settings_view.invalid_setting")).join("; ")
          : `HTTP ${res.status}`;
      throw new Error(message);
    }
    setConfig(body as BackgroundAgentsConfig);
    return body as BackgroundAgentsConfig;
  }, []);

  return { config, loading, error, refetch, save };
}