| @@ -1,7 +1,7 @@ |
| 1 | 1 | "use client"; |
| 2 | 2 | |
| 3 | | −import { useCallback, useEffect, useMemo, useRef, useState } from "react"; |
| 4 | | −import { useInfiniteQuery, type InfiniteData } from "@tanstack/react-query"; |
| 3 | +import { useCallback, useEffect, useRef } from "react"; |
| 4 | +import { useQuery } from "@tanstack/react-query"; |
| 5 | 5 | import { useAction, useQuery as useConvexQuery } from "convex/react"; |
| 6 | 6 | import { convexApi } from "@/lib/convex-api"; |
| 7 | 7 | import type { OpenApiSourceQuality, SourceAuthProfile, ToolDescriptor } from "@/lib/types"; |
| @@ -55,22 +55,9 @@ type ListToolsWithWarningsAction = (args: { · 47 unmodified lines |
| 55 | 55 | cursor?: string; |
| 56 | 56 | limit?: number; |
| 57 | 57 | buildId?: string; |
| 58 | + fetchAll?: boolean; |
| 58 | 59 | }) => Promise<WorkspaceToolsQueryResult>; |
| 59 | 60 | |
| 60 | | −interface ToolInventoryPageParam { |
| 61 | | − cursor?: string; |
| 62 | | − buildId?: string; |
| 63 | | −} |
| 64 | | − |
| 65 | | −interface SourceToolPageState { |
| 66 | | − tools: ToolDescriptor[]; |
| 67 | | − nextCursor?: string | null; |
| 68 | | − buildId?: string; |
| 69 | | − loading: boolean; |
| 70 | | −} |
| 71 | | − |
| 72 | | −const DEFAULT_PAGE_SIZE = 250; |
| 73 | | − |
| 74 | 61 | /** |
| 75 | 62 | * Fetches tool metadata from a Convex action, cached by TanStack Query. |
| 76 | 63 | * |
| @@ -85,7 +72,6 @@ export function useWorkspaceTools( · 8 unmodified lines |
| 85 | 72 | const listToolsWithWarningsRaw = useAction(convexApi.executorNode.listToolsWithWarnings); |
| 86 | 73 | const listToolsWithWarnings = listToolsWithWarningsRaw as ListToolsWithWarningsAction; |
| 87 | 74 | const detailsCacheRef = useRef<Map<string, ToolDescriptor>>(new Map()); |
| 88 | | − const [sourcePages, setSourcePages] = useState<Record<string, SourceToolPageState>>({}); |
| 89 | 75 | |
| 90 | 76 | // Watch tool sources reactively so we invalidate when sources change |
| 91 | 77 | const toolSources = useConvexQuery( |
| @@ -93,7 +79,7 @@ export function useWorkspaceTools( · 1 unmodified line |
| 93 | 79 | context ? { workspaceId: context.workspaceId, sessionId: context.sessionId } : "skip", |
| 94 | 80 | ); |
| 95 | 81 | |
| 96 | | − const inventoryQuery = useInfiniteQuery<WorkspaceToolsQueryResult, Error, InfiniteData<WorkspaceToolsQueryResult>, unknown[], ToolInventoryPageParam>({ |
| 82 | + const inventoryQuery = useQuery<WorkspaceToolsQueryResult, Error>({ |
| 97 | 83 | queryKey: [ |
| 98 | 84 | "workspace-tools-inventory", |
| 99 | 85 | context?.workspaceId, |
| @@ -102,7 +88,7 @@ export function useWorkspaceTools( · 2 unmodified lines |
| 102 | 88 | includeDetails, |
| 103 | 89 | toolSources, |
| 104 | 90 | ], |
| 105 | | − queryFn: async ({ pageParam }): Promise<WorkspaceToolsQueryResult> => { |
| 91 | + queryFn: async (): Promise<WorkspaceToolsQueryResult> => { |
| 106 | 92 | if (!context) { |
| 107 | 93 | return { |
| 108 | 94 | tools: [], |
| @@ -127,27 +113,12 @@ export function useWorkspaceTools( · 18 unmodified lines |
| 127 | 113 | ...(context.clientId && { clientId: context.clientId }), |
| 128 | 114 | ...(context.sessionId && { sessionId: context.sessionId }), |
| 129 | 115 | includeDetails, |
| 130 | | − limit: DEFAULT_PAGE_SIZE, |
| 131 | | − ...(pageParam?.cursor ? { cursor: pageParam.cursor } : {}), |
| 132 | | − ...(pageParam?.buildId ? { buildId: pageParam.buildId } : {}), |
| 116 | + fetchAll: true, |
| 133 | 117 | }); |
| 134 | 118 | }, |
| 135 | | − initialPageParam: {}, |
| 136 | | − getNextPageParam: (lastPage) => { |
| 137 | | − const nextCursor = lastPage.nextCursor; |
| 138 | | − if (!nextCursor) { |
| 139 | | − return undefined; |
| 140 | | − } |
| 141 | | − return { |
| 142 | | − cursor: nextCursor, |
| 143 | | − buildId: lastPage.inventoryStatus.readyBuildId, |
| 144 | | − }; |
| 145 | | − }, |
| 146 | 119 | enabled: !!context, |
| 147 | 120 | refetchInterval: (query) => { |
| 148 | | − const data = query.state.data as InfiniteData<WorkspaceToolsQueryResult> | undefined; |
| 149 | | − const first = data?.pages[0]; |
| 150 | | − const state = first?.inventoryStatus.state; |
| 121 | + const state = query.state.data?.inventoryStatus.state; |
| 151 | 122 | if (state === "initializing" || state === "rebuilding") { |
| 152 | 123 | return 2_000; |
| 153 | 124 | } |
| @@ -156,106 +127,8 @@ export function useWorkspaceTools( · 2 unmodified lines |
| 156 | 127 | placeholderData: (previousData) => previousData, |
| 157 | 128 | }); |
| 158 | 129 | |
| 159 | | − const pages = inventoryQuery.data?.pages ?? []; |
| 160 | | − const inventoryData = pages[0]; |
| 161 | | − const tools = useMemo(() => { |
| 162 | | − const merged = new Map<string, ToolDescriptor>(); |
| 163 | | − for (const page of pages) { |
| 164 | | − for (const tool of page.tools) { |
| 165 | | − merged.set(tool.path, tool); |
| 166 | | − } |
| 167 | | − } |
| 168 | | − for (const sourcePage of Object.values(sourcePages)) { |
| 169 | | − for (const tool of sourcePage.tools) { |
| 170 | | − merged.set(tool.path, tool); |
| 171 | | − } |
| 172 | | − } |
| 173 | | − return [...merged.values()]; |
| 174 | | − }, [pages, sourcePages]); |
| 175 | | − |
| 176 | | − const loadMoreToolsForSource = useCallback(async ( |
| 177 | | − source: { source: string; sourceName: string }, |
| 178 | | − ): Promise<void> => { |
| 179 | | − if (!context) { |
| 180 | | − return; |
| 181 | | − } |
| 182 | | − |
| 183 | | − const current = sourcePages[source.sourceName]; |
| 184 | | − if (current?.loading || current?.nextCursor === null) { |
| 185 | | − return; |
| 186 | | − } |
| 187 | | − |
| 188 | | − setSourcePages((prev) => ({ |
| 189 | | − ...prev, |
| 190 | | − [source.sourceName]: { |
| 191 | | − tools: prev[source.sourceName]?.tools ?? [], |
| 192 | | − nextCursor: prev[source.sourceName]?.nextCursor, |
| 193 | | − buildId: prev[source.sourceName]?.buildId, |
| 194 | | − loading: true, |
| 195 | | − }, |
| 196 | | − })); |
| 197 | | − |
| 198 | | − try { |
| 199 | | − const response = await listToolsWithWarnings({ |
| 200 | | − workspaceId: context.workspaceId, |
| 201 | | − ...(context.accountId && { accountId: context.accountId }), |
| 202 | | − ...(context.clientId && { clientId: context.clientId }), |
| 203 | | − ...(context.sessionId && { sessionId: context.sessionId }), |
| 204 | | − includeDetails, |
| 205 | | − source: source.source, |
| 206 | | − sourceName: source.sourceName, |
| 207 | | − limit: DEFAULT_PAGE_SIZE, |
| 208 | | − ...(current?.nextCursor ? { cursor: current.nextCursor } : {}), |
| 209 | | − ...(current?.buildId ? { buildId: current.buildId } : {}), |
| 210 | | − }); |
| 211 | | − |
| 212 | | − setSourcePages((prev) => { |
| 213 | | − const existingTools = prev[source.sourceName]?.tools ?? []; |
| 214 | | − const merged = new Map<string, ToolDescriptor>(); |
| 215 | | − for (const tool of existingTools) { |
| 216 | | − merged.set(tool.path, tool); |
| 217 | | − } |
| 218 | | − for (const tool of response.tools) { |
| 219 | | − merged.set(tool.path, tool); |
| 220 | | − } |
| 221 | | − return { |
| 222 | | − ...prev, |
| 223 | | − [source.sourceName]: { |
| 224 | | − tools: [...merged.values()], |
| 225 | | − nextCursor: response.nextCursor ?? null, |
| 226 | | − buildId: response.inventoryStatus.readyBuildId, |
| 227 | | − loading: false, |
| 228 | | − }, |
| 229 | | − }; |
| 230 | | − }); |
| 231 | | − } catch { |
| 232 | | − setSourcePages((prev) => ({ |
| 233 | | − ...prev, |
| 234 | | − [source.sourceName]: { |
| 235 | | − tools: prev[source.sourceName]?.tools ?? [], |
| 236 | | − nextCursor: prev[source.sourceName]?.nextCursor ?? null, |
| 237 | | − buildId: prev[source.sourceName]?.buildId, |
| 238 | | − loading: false, |
| 239 | | − }, |
| 240 | | − })); |
| 241 | | − } |
| 242 | | − }, [context, includeDetails, listToolsWithWarnings, sourcePages]); |
| 243 | | − |
| 244 | | − const sourceLoadingMoreTools = useMemo(() => { |
| 245 | | − const result: Record<string, boolean> = {}; |
| 246 | | − for (const [sourceName, state] of Object.entries(sourcePages)) { |
| 247 | | − result[sourceName] = state.loading; |
| 248 | | − } |
| 249 | | − return result; |
| 250 | | − }, [sourcePages]); |
| 251 | | − |
| 252 | | − const sourceHasMoreTools = useMemo(() => { |
| 253 | | − const result: Record<string, boolean> = {}; |
| 254 | | − for (const [sourceName, state] of Object.entries(sourcePages)) { |
| 255 | | − result[sourceName] = state.nextCursor !== null; |
| 256 | | − } |
| 257 | | − return result; |
| 258 | | − }, [sourcePages]); |
| 130 | + const inventoryData = inventoryQuery.data; |
| 131 | + const tools = inventoryData?.tools ?? []; |
| 259 | 132 | |
| 260 | 133 | const loadToolDetails = useCallback(async (toolPaths: string[]): Promise<Record<string, ToolDescriptor>> => { |
| 261 | 134 | const requested = [...new Set(toolPaths.filter((path) => path.length > 0))]; |
| @@ -299,16 +172,6 @@ export function useWorkspaceTools( · 37 unmodified lines |
| 299 | 172 | detailsCacheRef.current.clear(); |
| 300 | 173 | }, [context?.workspaceId, context?.accountId, context?.clientId, context?.sessionId]); |
| 301 | 174 | |
| 302 | | − useEffect(() => { |
| 303 | | − setSourcePages({}); |
| 304 | | − }, [ |
| 305 | | − context?.workspaceId, |
| 306 | | − context?.accountId, |
| 307 | | − context?.clientId, |
| 308 | | − context?.sessionId, |
| 309 | | − inventoryData?.inventoryStatus.readyBuildId, |
| 310 | | − ]); |
| 311 | | − |
| 312 | 175 | useEffect(() => { |
| 313 | 176 | if (!inventoryData || !includeDetails) { |
| 314 | 177 | return; |
| @@ -320,11 +183,12 @@ export function useWorkspaceTools( · 5 unmodified lines |
| 320 | 183 | }, [inventoryData, includeDetails]); |
| 321 | 184 | |
| 322 | 185 | const loadMoreTools = useCallback(async () => { |
| 323 | | − if (!inventoryQuery.hasNextPage || inventoryQuery.isFetchingNextPage) { |
| 324 | | − return; |
| 325 | | − } |
| 326 | | − await inventoryQuery.fetchNextPage(); |
| 327 | | − }, [inventoryQuery]); |
| 186 | + return; |
| 187 | + }, []); |
| 188 | + |
| 189 | + const loadMoreToolsForSource = useCallback(async () => { |
| 190 | + return; |
| 191 | + }, []); |
| 328 | 192 | |
| 329 | 193 | return { |
| 330 | 194 | tools, |
| @@ -338,11 +202,11 @@ export function useWorkspaceTools( · 7 unmodified lines |
| 338 | 202 | loadingSources: inventoryData?.inventoryStatus.loadingSourceNames ?? [], |
| 339 | 203 | loadingTools: !!context && inventoryQuery.isLoading, |
| 340 | 204 | refreshingTools: !!context && inventoryQuery.isFetching, |
| 341 | | − loadingMoreTools: inventoryQuery.isFetchingNextPage, |
| 342 | | − hasMoreTools: Boolean(inventoryQuery.hasNextPage), |
| 205 | + loadingMoreTools: false, |
| 206 | + hasMoreTools: false, |
| 343 | 207 | loadMoreTools, |
| 344 | | − sourceHasMoreTools, |
| 345 | | − sourceLoadingMoreTools, |
| 208 | + sourceHasMoreTools: {} as Record<string, boolean>, |
| 209 | + sourceLoadingMoreTools: {} as Record<string, boolean>, |
| 346 | 210 | loadMoreToolsForSource, |
| 347 | 211 | totalTools: inventoryData?.totalTools ?? tools.length, |
| 348 | 212 | loadedTools: tools.length, |