SurfSense/surfsense_web/atoms/agent-tools/agent-tools.atoms.ts
DESKTOP-RTLN3BA\$punk a64c8205fe feat: implement ensure_publication for zero_publication management
- Added the ensure_publication function to create and verify the zero_publication if it is missing, ensuring idempotency during database initialization.
- Integrated ensure_publication into the create_db_and_tables function to prevent zero-cache crash loops on startup.
- Introduced a self-check script to validate the ensure_publication functionality on a create_all-bootstrapped database.
- Updated various components to reflect the transition from search space to workspace, including adjustments in imports and routing paths.
2026-07-05 23:17:13 -07:00

96 lines
3.3 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(searchSpaceId: string): string[] {
if (typeof window === "undefined") return [];
try {
const raw = localStorage.getItem(`${STORAGE_PREFIX}${searchSpaceId}`);
return raw ? (JSON.parse(raw) as string[]) : [];
} catch {
return [];
}
}
function saveDisabledTools(searchSpaceId: string, tools: string[]) {
if (typeof window === "undefined") return;
if (tools.length === 0) {
localStorage.removeItem(`${STORAGE_PREFIX}${searchSpaceId}`);
} else {
localStorage.setItem(`${STORAGE_PREFIX}${searchSpaceId}`, JSON.stringify(tools));
}
}
const disabledToolsBaseAtom = atom<string[]>([]);
/** Tracks whether the atom has been hydrated from localStorage for the current search space */
const hydratedForAtom = atom<string | null>(null);
/**
* Read/write atom for the set of disabled tool names.
* Persists to localStorage keyed by search space ID.
*/
export const disabledToolsAtom = atom(
(get) => {
const searchSpaceId = get(activeWorkspaceIdAtom);
const hydratedFor = get(hydratedForAtom);
if (searchSpaceId && hydratedFor !== searchSpaceId) {
return loadDisabledTools(searchSpaceId);
}
return get(disabledToolsBaseAtom);
},
(get, set, update: string[] | ((prev: string[]) => string[])) => {
const searchSpaceId = get(activeWorkspaceIdAtom);
const prev = get(disabledToolsBaseAtom);
const next = typeof update === "function" ? update(prev) : update;
set(disabledToolsBaseAtom, next);
set(hydratedForAtom, searchSpaceId);
if (searchSpaceId) {
saveDisabledTools(searchSpaceId, next);
}
}
);
/**
* Hydrate disabled tools from localStorage when search space changes.
* Call this from a useEffect in a component that has access to the search space.
*/
export const hydrateDisabledToolsAtom = atom(null, (get, set) => {
const searchSpaceId = get(activeWorkspaceIdAtom);
if (!searchSpaceId) return;
const stored = loadDisabledTools(searchSpaceId);
set(disabledToolsBaseAtom, stored);
set(hydratedForAtom, searchSpaceId);
});
/** Toggle a single tool's enabled/disabled state */
export const toggleToolAtom = atom(null, (get, set, toolName: string) => {
const searchSpaceId = get(activeWorkspaceIdAtom);
const current = get(disabledToolsBaseAtom);
const next = current.includes(toolName)
? current.filter((t) => t !== toolName)
: [...current, toolName];
set(disabledToolsBaseAtom, next);
set(hydratedForAtom, searchSpaceId);
if (searchSpaceId) {
saveDisabledTools(searchSpaceId, 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;
});