SurfSense/surfsense_web/atoms/agent-tools/agent-tools.atoms.ts
Anish Sarkar a8c1fb660d feat(rename): complete searchSpace to workspace transition across frontend and backend
- Introduced a comprehensive specification for renaming `searchSpace` to `workspace` in `surfsense_web` and `surfsense_desktop`, ensuring all TypeScript identifiers, React props, and local data structures are updated.
- Implemented migration shims for persisted local state to prevent data loss during the transition.
- Updated observability metrics and IPC channels to reflect the new naming convention.
- Removed legacy `active-search-space` module and replaced it with `active-workspace` to maintain consistency.
- Ensured no behavioral changes or data loss for users during the renaming process.
2026-07-06 15:12:40 +05:30

96 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { atom } from "jotai";
import { atomWithQuery } from "jotai-tanstack-query";
import { agentToolsApiService } from "@/lib/apis/agent-tools-api.service";
import { cacheKeys } from "@/lib/query-client/cache-keys";
import { activeWorkspaceIdAtom } from "../workspaces/workspace-query.atoms";
export const agentToolsAtom = atomWithQuery((_get) => ({
queryKey: cacheKeys.agentTools.all(),
staleTime: 30 * 60 * 1000, // 30 min tool list rarely changes
queryFn: async () => agentToolsApiService.getTools(),
}));
const STORAGE_PREFIX = "surfsense-disabled-tools-";
function loadDisabledTools(workspaceId: string): string[] {
if (typeof window === "undefined") return [];
try {
const raw = localStorage.getItem(`${STORAGE_PREFIX}${workspaceId}`);
return raw ? (JSON.parse(raw) as string[]) : [];
} catch {
return [];
}
}
function saveDisabledTools(workspaceId: string, tools: string[]) {
if (typeof window === "undefined") return;
if (tools.length === 0) {
localStorage.removeItem(`${STORAGE_PREFIX}${workspaceId}`);
} else {
localStorage.setItem(`${STORAGE_PREFIX}${workspaceId}`, JSON.stringify(tools));
}
}
const disabledToolsBaseAtom = atom<string[]>([]);
/** Tracks whether the atom has been hydrated from localStorage for the current workspace */
const hydratedForAtom = atom<string | null>(null);
/**
* Read/write atom for the set of disabled tool names.
* Persists to localStorage keyed by workspace ID.
*/
export const disabledToolsAtom = atom(
(get) => {
const workspaceId = get(activeWorkspaceIdAtom);
const hydratedFor = get(hydratedForAtom);
if (workspaceId && hydratedFor !== workspaceId) {
return loadDisabledTools(workspaceId);
}
return get(disabledToolsBaseAtom);
},
(get, set, update: string[] | ((prev: string[]) => string[])) => {
const workspaceId = get(activeWorkspaceIdAtom);
const prev = get(disabledToolsBaseAtom);
const next = typeof update === "function" ? update(prev) : update;
set(disabledToolsBaseAtom, next);
set(hydratedForAtom, workspaceId);
if (workspaceId) {
saveDisabledTools(workspaceId, next);
}
}
);
/**
* Hydrate disabled tools from localStorage when workspace changes.
* Call this from a useEffect in a component that has access to the workspace.
*/
export const hydrateDisabledToolsAtom = atom(null, (get, set) => {
const workspaceId = get(activeWorkspaceIdAtom);
if (!workspaceId) return;
const stored = loadDisabledTools(workspaceId);
set(disabledToolsBaseAtom, stored);
set(hydratedForAtom, workspaceId);
});
/** Toggle a single tool's enabled/disabled state */
export const toggleToolAtom = atom(null, (get, set, toolName: string) => {
const workspaceId = get(activeWorkspaceIdAtom);
const current = get(disabledToolsBaseAtom);
const next = current.includes(toolName)
? current.filter((t) => t !== toolName)
: [...current, toolName];
set(disabledToolsBaseAtom, next);
set(hydratedForAtom, workspaceId);
if (workspaceId) {
saveDisabledTools(workspaceId, next);
}
});
/** Derive the count of currently enabled tools */
export const enabledToolCountAtom = atom((get) => {
const { data: tools } = get(agentToolsAtom);
const disabled = get(disabledToolsAtom);
if (!tools) return 0;
return tools.length - disabled.filter((d) => tools.some((t) => t.name === d)).length;
});