Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
3.6 KB
Raw Blame
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { api } from "../api";
import { runtimeKeys } from "./queries";
import { workspaceKeys } from "../workspace/queries";
import { agentTaskSnapshotKeys } from "../agents/queries";

export function useDeleteRuntime(wsId: string) {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (runtimeId: string) => api.deleteRuntime(runtimeId),
    onSettled: () => {
      qc.invalidateQueries({ queryKey: runtimeKeys.all(wsId) });
    },
  });
}

// Confirmed-delete counterpart to useDeleteRuntime. The dialog routes here when
// the strict DELETE refused with `runtime_has_active_agents` (or when the
// caller already knows the runtime has active agents and wants to skip the
// pre-flight refusal). Mutation fn returns the server-reported counts so
// the caller can render a richer success toast.
//
// Invalidates runtimes (the list / detail), workspace agents (they are unbound,
// so their runtime column and readiness change) and the agent presence snapshot
// (the delete also cancels queued/running tasks). Without the agent-side
// invalidation the Agents page would keep showing them as runnable.
export function useUnbindAgentsAndDeleteRuntime(wsId: string) {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      runtimeId,
      expectedActiveAgentIds,
    }: {
      runtimeId: string;
      expectedActiveAgentIds: string[];
    }) => api.unbindAgentsAndDeleteRuntime(runtimeId, expectedActiveAgentIds),
    onSettled: () => {
      qc.invalidateQueries({ queryKey: runtimeKeys.all(wsId) });
      qc.invalidateQueries({ queryKey: workspaceKeys.agents(wsId) });
      qc.invalidateQueries({ queryKey: agentTaskSnapshotKeys.all(wsId) });
    },
  });
}

// Confirmed access restriction to private or selected members. Like runtime deletion, this changes
// runtime rows, agent bindings, and task presence, so every affected server
// projection is invalidated after the transaction settles.
export function useRevokeRuntimeWorkspaceAccess(wsId: string) {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      runtimeId,
      expectedNonownerAgentIds,
      expectedTaskIds,
      sharedWith,
    }: {
      runtimeId: string;
      expectedNonownerAgentIds: string[];
      expectedTaskIds: string[];
      sharedWith?: string[];
    }) =>
      api.revokeRuntimeWorkspaceAccess(
        runtimeId,
        expectedNonownerAgentIds,
        expectedTaskIds,
        sharedWith,
      ),
    onSettled: () => {
      qc.invalidateQueries({ queryKey: runtimeKeys.all(wsId) });
      qc.invalidateQueries({ queryKey: workspaceKeys.agents(wsId) });
      qc.invalidateQueries({ queryKey: agentTaskSnapshotKeys.all(wsId) });
    },
  });
}

// useUpdateRuntime patches editable fields on a runtime (visibility, custom
// name). Invalidates the runtime list so the picker disabled-state and
// display names recompute.
export function useUpdateRuntime(wsId: string) {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({
      runtimeId,
      patch,
    }: {
      runtimeId: string;
      patch: {
        visibility?: "private" | "public";
        shared_with?: string[];
        // Empty string clears the custom name; omit to leave unchanged.
        custom_name?: string;
        apply_to_machine?: boolean;
        // null removes the runtime-wide concurrency cap; omit to leave unchanged.
        max_concurrent_tasks?: number | null;
      };
    }) => api.updateRuntime(runtimeId, patch),
    onSettled: () => {
      qc.invalidateQueries({ queryKey: runtimeKeys.all(wsId) });
    },
  });
}