import { useEffect, useId, useState } from "react"; import { useQuery } from "@tanstack/react-query"; import type { ChatExecutionDefaults as Defaults, TaskWorkspaceSelection } from "@paperclipai/shared"; import { executionWorkspacesApi } from "@/api/execution-workspaces"; import { Button } from "@/components/ui/button"; import { NativeSelect } from "@/components/ui/select"; import { companyProjectsQuery } from "@/lib/company-projects-query"; function selectionKey(value: TaskWorkspaceSelection | null | undefined): string { if (value === undefined) return "inherit"; if (value === null) return "clear"; if (value.kind === "task_directory") return "task"; if (value.kind === "existing") return `existing:${value.workspaceId}`; return `source:${value.projectWorkspaceId}:${value.mode}`; } export function ChatExecutionDefaults({ companyId, value, resource = false, onSave }: { companyId: string; value: Defaults | null | undefined; resource?: boolean; onSave: (defaults: Defaults | null) => Promise; }) { const id = useId(); const [draft, setDraft] = useState(null); const [pending, setPending] = useState(false); const [error, setError] = useState(null); const [savedFrom, setSavedFrom] = useState(null); const projects = useQuery({ ...companyProjectsQuery(companyId) }); const workspaces = useQuery({ queryKey: ["channel-workspace-choices", companyId], queryFn: () => executionWorkspacesApi.listSummaries(companyId, { selectableForTask: true }), }); const current = draft ?? value ?? {}; const valueKey = JSON.stringify(value ?? {}); const saved = savedFrom !== null; // Keep the accepted selections until the parent publishes refreshed defaults. useEffect(() => { if (savedFrom !== null && savedFrom !== valueKey) setDraft(null); }, [savedFrom, valueKey]); const projectValue = current.projectId === undefined ? "inherit" : current.projectId ?? "none"; const workspaceValue = selectionKey(current.workspace); const choices = new Map(); choices.set("task", { kind: "task_directory" }); for (const project of projects.data ?? []) { for (const source of project.workspaces ?? []) { for (const mode of ["managed_isolated", "shared"] as const) { choices.set(`source:${source.id}:${mode}`, { kind: "configured_source", projectWorkspaceId: source.id, mode }); } } } for (const workspace of workspaces.data ?? []) { choices.set(`existing:${workspace.id}`, { kind: "existing", workspaceId: workspace.id }); } const dirty = !saved && JSON.stringify(current) !== valueKey; const change = (next: Defaults) => { setDraft(next); setSavedFrom(null); setError(null); }; return
{ event.preventDefault(); if (pending || !dirty) return; setPending(true); setError(null); setSavedFrom(null); try { await onSave(Object.keys(current).length ? current : null); setSavedFrom(valueKey); } catch (cause) { setError(cause instanceof Error ? cause.message : "Couldn’t save task defaults. Try again."); } finally { setPending(false); } }}>

Task defaults

Applies to new conversations. Existing tasks keep their files and project.

{(projects.isError || workspaces.isError) &&

Couldn’t load available projects or workspaces.

}
{ const { projectId: _previous, ...rest } = current; change(event.target.value === "inherit" ? rest : { ...rest, projectId: event.target.value === "none" ? null : event.target.value }); }}> {current.projectId && !projects.data?.some((project) => project.id === current.projectId) && } {(projects.data ?? []).map((project) => )}
{ const key = event.target.value; const { workspace: _previous, ...rest } = current; if (key === "inherit") change(rest); else if (key === "clear") change({ ...rest, workspace: null }); else { const workspace = choices.get(key); if (workspace) change({ ...rest, workspace }); } }}> {resource && } {!resource && workspaceValue === "clear" && } {current.workspace && !choices.has(workspaceValue) && } {(projects.data ?? []).filter((project) => project.workspaces?.length).map((project) => {project.workspaces.flatMap((source) => [ , , ])} )} {(workspaces.data?.length ?? 0) > 0 && {workspaces.data!.map((workspace) => )}}

Project and workspace access is checked for each task. A shared folder must also be allowed by the agent’s execution policy.

{error &&

{error}

}
{dirty ? : saved ? Saved for new tasks. : null}
; }