diff --git a/packages/sdk/src/catalog/CatalogClient.ts b/packages/sdk/src/catalog/CatalogClient.ts index 2c024cc15..3fb875511 100644 --- a/packages/sdk/src/catalog/CatalogClient.ts +++ b/packages/sdk/src/catalog/CatalogClient.ts @@ -1,105 +1,80 @@ -import { getInstillAdditionalHeaders, getQueryString } from "../helper"; -import { APIResource } from "../main/resource"; -import { - Catalog, - Chunk, - CreateCatalogRequest, - CreateCatalogResponse, +import type { + AskNamespaceCatalogQuestionRequest, + AskNamespaceCatalogQuestionResponse, + CreateNamespaceCatalogFileRequest, + CreateNamespaceCatalogFileResponse, + CreateNamespaceCatalogRequest, + CreateNamespaceCatalogResponse, DeleteCatalogFileRequest, - DeleteCatalogRequest, - File, - FileContent, - GetCatalogSingleSourceOfTruthFileRequest, - GetCatalogSingleSourceOfTruthFileResponse, - GetFileDetailsRequest, - GetFileDetailsResponse, - ListCatalogFilesRequest, - ListCatalogFilesResponse, - ListCatalogsRequest, + DeleteNamespaceCatalogRequest, + GetNamespaceCatalogSingleSourceOfTruthFileRequest, + GetNamespaceCatalogSingleSourceOfTruthFileResponse, ListCatalogsResponse, - ListChunksRequest, - ListChunksResponse, + ListNamespaceCatalogChunksRequest, + ListNamespaceCatalogChunksResponse, + ListNamespaceCatalogFilesRequest, + ListNamespaceCatalogFilesResponse, + ListNamespaceCatalogRunsRequest, + ListNamespaceCatalogRunsResponse, + ListNamespaceCatalogsRequest, + ListPaginatedNamespaceCatalogFilesRequest, + ListPaginatedNamespaceCatalogFilesResponse, + ListPaginatedNamespaceCatalogRunsRequest, + ListPaginatedNamespaceCatalogRunsResponse, ProcessCatalogFilesRequest, ProcessCatalogFilesResponse, - UpdateCatalogRequest, - UpdateCatalogResponse, - UpdateChunkRequest, - UpdateChunkResponse, - UploadCatalogFileRequest, - UploadCatalogFileResponse, + RetrieveSimilarNamespaceCatalogChunksRequest, + RetrieveSimilarNamespaceCatalogChunksResponse, + UpdateCatalogChunkRequest, + UpdateCatalogChunkResponse, + UpdateNamespaceCatalogRequest, + UpdateNamespaceCatalogResponse, } from "./types"; +import { getInstillAdditionalHeaders, getQueryString } from "../helper"; +import { APIResource } from "../main/resource"; export class CatalogClient extends APIResource { - async listCatalogs( - props: ListCatalogsRequest & { enablePagination: true }, - ): Promise; - async listCatalogs( - props: ListCatalogsRequest & { enablePagination: false }, - ): Promise; - async listCatalogs( - props: ListCatalogsRequest & { enablePagination: boolean }, - ): Promise { - const { pageSize, pageToken, view, enablePagination, namespaceId } = props; + /* ---------------------------------------------------------------------------- + * Catalog + * ---------------------------------------------------------------------------*/ + // This is a non paginated endpoint + async listNamespaceCatalogs({ namespaceId }: ListNamespaceCatalogsRequest) { try { - const catalogs: Catalog[] = []; - const queryString = getQueryString({ baseURL: `/namespaces/${namespaceId}/catalogs`, - pageSize, - pageToken, - view, }); const data = await this._client.get(queryString); - if (enablePagination) { - return Promise.resolve(data); - } - - catalogs.push(...data.catalogs); - - if (data.nextPageToken) { - catalogs.push( - ...(await this.listCatalogs({ - pageSize, - pageToken: data.nextPageToken, - enablePagination: false, - view, - namespaceId, - })), - ); - } - - return Promise.resolve(catalogs); + return Promise.resolve(data); } catch (err) { return Promise.reject(err); } } - async createCatalog( - props: CreateCatalogRequest, - ): Promise { - const { namespaceId, payload } = props; + async createNamespaceCatalog(props: CreateNamespaceCatalogRequest) { + const { namespaceId, ...payload } = props; try { const queryString = getQueryString({ baseURL: `/namespaces/${namespaceId}/catalogs`, }); - const data = await this._client.post<{ catalog: Catalog }>(queryString, { - body: JSON.stringify(payload), - }); + const data = await this._client.post( + queryString, + { + body: JSON.stringify(payload), + }, + ); - return Promise.resolve(data.catalog); + return Promise.resolve(data); } catch (error) { return Promise.reject(error); } } - async updateCatalog( - props: UpdateCatalogRequest, - ): Promise { + async updateNamespaceCatalog(props: UpdateNamespaceCatalogRequest) { const { namespaceId, catalogId, ...payload } = props; try { @@ -107,65 +82,91 @@ export class CatalogClient extends APIResource { baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}`, }); - const data = await this._client.put<{ catalog: Catalog }>(queryString, { - body: JSON.stringify(payload), - }); + const data = await this._client.put( + queryString, + { + body: JSON.stringify(payload), + }, + ); - return Promise.resolve(data.catalog); + return Promise.resolve(data); } catch (error) { return Promise.reject(error); } } - async deleteCatalog(props: DeleteCatalogRequest): Promise { - const { namespaceId, catalogId } = props; - + async deleteNamespaceCatalog({ + namespaceId, + catalogId, + }: DeleteNamespaceCatalogRequest) { try { const queryString = getQueryString({ baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}`, }); await this._client.delete(queryString); - return Promise.resolve(); } catch (error) { return Promise.reject(error); } } - async uploadCatalogFile( - props: UploadCatalogFileRequest, - ): Promise { - const { namespaceId, catalogId, payload } = props; + /* ---------------------------------------------------------------------------- + * Catalog File + * ---------------------------------------------------------------------------*/ + + async createNamespaceCatalogFile(props: CreateNamespaceCatalogFileRequest) { + const { namespaceId, catalogId, ...payload } = props; try { const queryString = getQueryString({ baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}/files`, }); - const data = await this._client.post<{ file: File }>(queryString, { - body: JSON.stringify(payload), - }); + const data = await this._client.post( + queryString, + { + body: JSON.stringify(payload), + }, + ); - return Promise.resolve(data.file); + return Promise.resolve(data); } catch (error) { return Promise.reject(error); } } - async listCatalogFiles( - props: ListCatalogFilesRequest & { enablePagination: true }, - ): Promise; - async listCatalogFiles( - props: ListCatalogFilesRequest & { enablePagination: false }, - ): Promise; - async listCatalogFiles( - props: ListCatalogFilesRequest & { enablePagination: boolean }, - ): Promise { - const { namespaceId, catalogId, pageSize, pageToken, enablePagination } = - props; + async listPaginatedCatalogFiles({ + namespaceId, + catalogId, + pageSize, + pageToken, + }: ListPaginatedNamespaceCatalogFilesRequest) { + try { + const queryString = getQueryString({ + baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}/files`, + pageSize, + pageToken, + }); + + const data = + await this._client.get( + queryString, + ); + return Promise.resolve(data); + } catch (error) { + return Promise.reject(error); + } + } + + async listNamespaceCatalogFiles({ + namespaceId, + catalogId, + pageSize = 100, + pageToken, + }: ListNamespaceCatalogFilesRequest) { try { - const files: File[] = []; + const files: ListNamespaceCatalogFilesResponse = []; const queryString = getQueryString({ baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}/files`, @@ -174,22 +175,19 @@ export class CatalogClient extends APIResource { }); const data = - await this._client.get(queryString); - - if (enablePagination) { - return Promise.resolve(data); - } + await this._client.get( + queryString, + ); files.push(...data.files); if (data.nextPageToken) { files.push( - ...(await this.listCatalogFiles({ + ...(await this.listNamespaceCatalogFiles({ namespaceId, catalogId, pageSize, pageToken: data.nextPageToken, - enablePagination: false, })), ); } @@ -200,148 +198,229 @@ export class CatalogClient extends APIResource { } } - async deleteCatalogFile(props: DeleteCatalogFileRequest): Promise { - const { fileUid } = props; - + async deleteCatalogFile({ fileUid }: DeleteCatalogFileRequest) { try { await this._client.delete(`/catalogs/files?fileUid=${fileUid}`); - return Promise.resolve(); } catch (error) { return Promise.reject(error); } } - async getFileDetails( - props: GetFileDetailsRequest, - ): Promise { - const { namespaceId, catalogId, fileId } = props; - + async processCatalogFiles({ + fileUids, + requesterUid, + }: ProcessCatalogFilesRequest) { try { const queryString = getQueryString({ - baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}/files/${fileId}`, + baseURL: `/catalogs/files/processAsync`, }); - const data = await this._client.get<{ file: File }>(queryString); - return Promise.resolve(data.file); + const additionalHeaders = getInstillAdditionalHeaders({ + requesterUid: requesterUid, + }); + + const data = await this._client.post( + queryString, + { + body: JSON.stringify({ fileUids }), + additionalHeaders, + }, + ); + + return Promise.resolve(data.files); } catch (error) { return Promise.reject(error); } } - async getCatalogSingleSourceOfTruthFile( - props: GetCatalogSingleSourceOfTruthFileRequest, - ): Promise { - const { namespaceId, catalogId, fileUid } = props; + async listNamespaceCatalogChunks({ + namespaceId, + catalogId, + fileUid, + chunkUids, + }: ListNamespaceCatalogChunksRequest) { + const queryString = getQueryString({ + baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}/chunks`, + fileUid, + chunkUids, + }); + + try { + const data = + await this._client.get(queryString); + + return Promise.resolve(data); + } catch (error) { + return Promise.reject(error); + } + } + async getNamespaceCatalogSingleSourceOfTruthFile({ + namespaceId, + catalogId, + fileUid, + }: GetNamespaceCatalogSingleSourceOfTruthFileRequest) { try { const queryString = getQueryString({ baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}/files/${fileUid}/source`, }); - const data = await this._client.get<{ sourceFile: FileContent }>( + const data = + await this._client.get( + queryString, + ); + + return Promise.resolve(data); + } catch (error) { + return Promise.reject(error); + } + } + + async updateCatalogChunk({ + chunkUid, + retrievable, + }: UpdateCatalogChunkRequest) { + const queryString = getQueryString({ + baseURL: `/chunks/${chunkUid}`, + }); + + try { + const data = await this._client.post( queryString, + { + body: JSON.stringify({ retrievable }), + }, ); - return Promise.resolve(data.sourceFile); + + return Promise.resolve(data); } catch (error) { return Promise.reject(error); } } - async processCatalogFiles( - props: ProcessCatalogFilesRequest, - ): Promise { - const { fileUids, requesterUid } = props; + async retrieveSimilarNamespaceCatalogChunks({ + namespaceId, + catalogId, + requesterUid, + textPrompt, + topK, + }: RetrieveSimilarNamespaceCatalogChunksRequest) { + const queryString = getQueryString({ + baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}/chunks/retrieve`, + }); + + const additionalHeaders = getInstillAdditionalHeaders({ + requesterUid: requesterUid, + }); + + try { + const data = + await this._client.post( + queryString, + { + body: JSON.stringify({ textPrompt, topK }), + additionalHeaders, + }, + ); - if (!requesterUid) { - return Promise.reject(new Error("requesterUid not provided")); + return Promise.resolve(data); + } catch (error) { + return Promise.reject(error); } + } + + async askNamespaceCatalogQuestion(props: AskNamespaceCatalogQuestionRequest) { + const { namespaceId, catalogId, requesterUid, ...payload } = props; + + const queryString = getQueryString({ + baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}/ask`, + }); + + const additionalHeaders = getInstillAdditionalHeaders({ + requesterUid: requesterUid, + }); try { - const queryString = getQueryString({ - baseURL: `/catalogs/files/processAsync`, - }); + const data = await this._client.post( + queryString, + { + body: JSON.stringify(payload), + additionalHeaders, + }, + ); + return Promise.resolve(data); + } catch (error) { + return Promise.reject(error); + } + } - const additionalHeaders = getInstillAdditionalHeaders({ - requesterUid: requesterUid, - }); + async listPaginatedNamespaceCatalogRuns( + props: ListPaginatedNamespaceCatalogRunsRequest, + ) { + const { namespaceId, catalogId, pageSize, page, filter, orderBy } = props; - const data = await this._client.post<{ files: File[] }>(queryString, { - body: JSON.stringify({ file_uids: fileUids }), - additionalHeaders, - }); + const queryString = getQueryString({ + baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}/runs`, + page, + pageSize, + filter, + orderBy, + }); - return Promise.resolve(data.files); + try { + const data = + await this._client.get( + queryString, + ); + + return Promise.resolve(data); } catch (error) { return Promise.reject(error); } } - async listChunks( - props: ListChunksRequest & { enablePagination: true }, - ): Promise; - async listChunks( - props: ListChunksRequest & { enablePagination: false }, - ): Promise; - async listChunks( - props: ListChunksRequest & { enablePagination: boolean }, - ): Promise { + async listNamespaceCatalogRuns(props: ListNamespaceCatalogRunsRequest) { const { namespaceId, catalogId, - fileUid, - pageSize, - pageToken, - enablePagination, + page, + pageSize = 100, + filter, + orderBy, } = props; - try { - const baseUrl = `/namespaces/${namespaceId}/catalogs/${catalogId}/chunks?fileUid=${fileUid}`; + const runs: ListNamespaceCatalogRunsResponse = []; - const queryString = getQueryString({ - baseURL: baseUrl, - pageSize, - pageToken, - }); + const queryString = getQueryString({ + baseURL: `/namespaces/${namespaceId}/catalogs/${catalogId}/runs`, + page, + pageSize, + filter, + orderBy, + }); - const data = await this._client.get(queryString); + try { + const data = + await this._client.get( + queryString, + ); - if (enablePagination) { - return Promise.resolve(data); - } + runs.push(...data.catalogRuns); - const chunks = data.chunks || []; - if (data.nextPageToken) { - chunks.push( - ...(await this.listChunks({ + if (data.totalSize / data.pageSize > data.page) { + runs.push( + ...(await this.listNamespaceCatalogRuns({ namespaceId, catalogId, - fileUid, + page: data.page + 1, pageSize, - pageToken: data.nextPageToken, - enablePagination: false, + filter, + orderBy, })), ); } - return Promise.resolve(chunks); - } catch (error) { - return Promise.reject(error); - } - } - - async updateChunk(props: UpdateChunkRequest): Promise { - const { chunkUid, retrievable } = props; - - try { - const queryString = getQueryString({ - baseURL: `/chunks/${chunkUid}`, - }); - - const data = await this._client.post<{ chunk: Chunk }>(queryString, { - body: JSON.stringify({ retrievable }), - }); - - return Promise.resolve(data.chunk); + return Promise.resolve(runs); } catch (error) { return Promise.reject(error); } diff --git a/packages/sdk/src/catalog/types.ts b/packages/sdk/src/catalog/types.ts index c918fe513..786e040c6 100644 --- a/packages/sdk/src/catalog/types.ts +++ b/packages/sdk/src/catalog/types.ts @@ -1,4 +1,4 @@ -import { Nullable } from "../types"; +import { GeneralRecord } from "../types"; export type Catalog = { chunks: Chunk[]; @@ -21,6 +21,39 @@ export type Catalog = { catalogUid: string; }; +export type CatalogRunAction = + | "CATALOG_RUN_ACTION_CREATE" + | "CATALOG_RUN_ACTION_UPDATE" + | "CATALOG_RUN_ACTION_DELETE" + | "CATALOG_RUN_ACTION_CREATE_FILE" + | "CATALOG_RUN_ACTION_PROCESS_FILE" + | "CATALOG_RUN_ACTION_DELETE_FILE"; + +export type CatalogRunStatus = + | "RUN_STATUS_PROCESSING" + | "RUN_STATUS_COMPLETED" + | "RUN_STATUS_FAILED" + | "RUN_STATUS_QUEUED"; + +export type CatalogRunSource = "RUN_SOURCE_CONSOLE" | "RUN_SOURCE_API"; + +export type CatalogRun = { + uid: string; + catalogUid: string; + fileUids: string[]; + action: CatalogRunAction; + status: CatalogRunStatus; + source: CatalogRunSource; + totalDuration: number; + runnerId: string; + namespaceId: string; + payload: GeneralRecord; + startTime: string; + completeTime: string; + error?: string; + creditAmount: number; +}; + export type Chunk = { chunkUid: string; content: string; @@ -32,6 +65,14 @@ export type Chunk = { type: string; }; +export type SimilarChunk = { + chunkUid: string; + similarityScore: number; + textContent: string; + sourceFile: string; + chunkMetadata: GeneralRecord; +}; + export type FileStatus = | "FILE_PROCESS_STATUS_NOTSTARTED" | "FILE_PROCESS_STATUS_WAITING" @@ -41,6 +82,20 @@ export type FileStatus = | "FILE_PROCESS_STATUS_COMPLETED" | "FILE_PROCESS_STATUS_FAILED"; +export type FileType = + | "FILE_TYPE_TEXT" + | "FILE_TYPE_PDF" + | "FILE_TYPE_MARKDOWN" + | "FILE_TYPE_HTML" + | "FILE_TYPE_DOCX" + | "FILE_TYPE_DOC" + | "FILE_TYPE_PPT" + | "FILE_TYPE_PPTX" + | "FILE_TYPE_XLS" + | "FILE_TYPE_XLSX" + | "FILE_TYPE_CSV" + | "FILE_TYPE_UNSPECIFIED"; + export type File = { fileUid: string; name: string; @@ -73,11 +128,8 @@ export type CatalogFile = { retrievable?: boolean; }; -export type ListCatalogsRequest = { +export type ListNamespaceCatalogsRequest = { namespaceId: string; - pageSize?: number; - pageToken?: string; - view?: string; }; export type ListCatalogsResponse = { @@ -86,100 +138,159 @@ export type ListCatalogsResponse = { totalSize: number; }; -export type CreateCatalogRequest = { +export type CreateNamespaceCatalogRequest = { namespaceId: string; - payload: { - name: string; - description?: string; - tags?: string[]; - namespaceId: string; - }; + name: string; + description?: string; + tags?: string[]; }; -export type CreateCatalogResponse = Catalog; +export type CreateNamespaceCatalogResponse = { + catalog: Catalog; +}; -export type UpdateCatalogRequest = { +export type UpdateNamespaceCatalogRequest = { namespaceId: string; catalogId: string; description?: string; + tags?: string[]; }; -export type UpdateCatalogResponse = Catalog; - -export type DeleteCatalogRequest = { - namespaceId: string; - catalogId: string; +export type UpdateNamespaceCatalogResponse = { + catalog: Catalog; }; -export type GetFileDetailsRequest = { +export type DeleteNamespaceCatalogRequest = { namespaceId: string; catalogId: string; - fileId: string; }; -export type GetFileDetailsResponse = File; - -export type GetCatalogSingleSourceOfTruthFileRequest = { +export type GetNamespaceCatalogSingleSourceOfTruthFileRequest = { namespaceId: string; catalogId: string; fileUid: string; }; -export type GetCatalogSingleSourceOfTruthFileResponse = FileContent; +export type GetNamespaceCatalogSingleSourceOfTruthFileResponse = { + sourceFile: FileContent; +}; -export type UploadCatalogFileRequest = { +export type CreateNamespaceCatalogFileRequest = { namespaceId: string; catalogId: string; - payload: { - name: string; - type: string; - content: string; - }; + name: string; + type: FileType; + content?: string; }; -export type UploadCatalogFileResponse = File; +export type CreateNamespaceCatalogFileResponse = { + file: File; +}; -export type ListCatalogFilesRequest = { +export type ListPaginatedNamespaceCatalogFilesRequest = { namespaceId: string; catalogId: string; pageSize?: number; pageToken?: string; }; -export type ListCatalogFilesResponse = { +export type ListPaginatedNamespaceCatalogFilesResponse = { files: File[]; nextPageToken: string; totalSize: number; }; +export type ListNamespaceCatalogFilesRequest = { + namespaceId: string; + catalogId: string; + pageSize?: number; + pageToken?: string; +}; + +export type ListNamespaceCatalogFilesResponse = File[]; + export type DeleteCatalogFileRequest = { fileUid: string; }; export type ProcessCatalogFilesRequest = { fileUids: string[]; - requesterUid: Nullable; + requesterUid?: string; }; -export type ProcessCatalogFilesResponse = File[]; +export type ProcessCatalogFilesResponse = { + files: File[]; +}; -export type ListChunksRequest = { +export type ListNamespaceCatalogChunksRequest = { namespaceId: string; catalogId: string; - fileUid: string; - pageSize?: number; - pageToken?: string; + fileUid?: string; + chunkUids?: string[]; }; -export type ListChunksResponse = { +export type ListNamespaceCatalogChunksResponse = { chunks: Chunk[]; - nextPageToken: string; - totalSize: number; }; -export type UpdateChunkRequest = { +export type UpdateCatalogChunkRequest = { chunkUid: string; retrievable: boolean; }; -export type UpdateChunkResponse = Chunk; +export type UpdateCatalogChunkResponse = { + chunk: Chunk; +}; + +export type RetrieveSimilarNamespaceCatalogChunksRequest = { + namespaceId: string; + catalogId: string; + textPrompt?: string; + topK?: number; + requesterUid?: string; +}; + +export type RetrieveSimilarNamespaceCatalogChunksResponse = { + similarChunks: SimilarChunk[]; +}; + +export type AskNamespaceCatalogQuestionRequest = { + namespaceId: string; + catalogId: string; + question?: string; + topK?: number; + requesterUid?: string; +}; + +export type AskNamespaceCatalogQuestionResponse = { + answer: { + similarChunks: SimilarChunk[]; + }; +}; + +export type ListNamespaceCatalogRunsRequest = { + namespaceId: string; + catalogId: string; + pageSize?: number; + page?: number; + filter?: string; + orderBy?: string; +}; + +export type ListNamespaceCatalogRunsResponse = CatalogRun[]; + +export type ListPaginatedNamespaceCatalogRunsRequest = { + namespaceId: string; + catalogId: string; + pageSize?: number; + page?: number; + filter?: string; + orderBy?: string; +}; + +export type ListPaginatedNamespaceCatalogRunsResponse = { + catalogRuns: CatalogRun[]; + totalSize: number; + pageSize: number; + page: number; +}; diff --git a/packages/sdk/src/helper/getQueryString.ts b/packages/sdk/src/helper/getQueryString.ts index 5ac0c871b..805d52b25 100644 --- a/packages/sdk/src/helper/getQueryString.ts +++ b/packages/sdk/src/helper/getQueryString.ts @@ -15,6 +15,8 @@ export const getQueryString = ({ stop, aggregationWindow, showDeleted, + fileUid, + chunkUids, }: { baseURL: string; pageSize?: number; @@ -32,6 +34,8 @@ export const getQueryString = ({ stop?: string; aggregationWindow?: string; showDeleted?: boolean; + fileUid?: string; + chunkUids?: string[]; }) => { let url = baseURL; @@ -110,6 +114,16 @@ export const getQueryString = ({ url += `showDeleted=${showDeleted}&`; } + if (fileUid) { + url += `fileUid=${fileUid}&`; + } + + if (chunkUids) { + for (const chunkUid of chunkUids) { + url += `chunkUids=${chunkUid}&`; + } + } + if (url.endsWith("&")) { url = url.slice(0, -1); } diff --git a/packages/toolkit/src/lib/react-query-service/catalog/index.ts b/packages/toolkit/src/lib/react-query-service/catalog/index.ts index 5e26e4a94..a57ce0181 100644 --- a/packages/toolkit/src/lib/react-query-service/catalog/index.ts +++ b/packages/toolkit/src/lib/react-query-service/catalog/index.ts @@ -1,13 +1,11 @@ -export * from "./useCreateCatalog"; -export * from "./useDeleteCatalog"; -export * from "./useUpdateCatalog"; -export * from "./useGetCatalogs"; -export * from "./useUploadCatalogFile"; +export * from "./useCreateNamespaceCatalog"; +export * from "./useDeleteNamespaceCatalog"; +export * from "./useUpdateNamespaceCatalog"; +export * from "./useListNamespaceCatalogs"; +export * from "./useCreateNamespaceCatalogFile"; export * from "./useProcessCatalogFiles"; -export * from "./useListCatalogFiles"; +export * from "./useListNamespaceCatalogFiles"; export * from "./useDeleteCatalogFile"; -export * from "./useGetFileDetails"; -export * from "./useGetCatalogSingleSourceOfTruthFile"; -export * from "./useListChunks"; -export * from "./useUpdateChunk"; -export * from "./useGetAllChunks"; +export * from "./useGetNamespaceCatalogSingleSourceOfTruthFile"; +export * from "./useUpdateCatalogChunk"; +export * from "./useListNamespaceCatalogChunks"; diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useCreateCatalog.ts b/packages/toolkit/src/lib/react-query-service/catalog/useCreateCatalog.ts deleted file mode 100644 index 9ebb00129..000000000 --- a/packages/toolkit/src/lib/react-query-service/catalog/useCreateCatalog.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { useMutation } from "@tanstack/react-query"; -import { Nullable } from "instill-sdk"; - -import { getInstillCatalogAPIClient } from "../../sdk-helper"; - -export function useCreateCatalog() { - return useMutation({ - mutationFn: async ({ - payload, - namespaceId, - accessToken, - }: { - payload: { - name: string; - description?: string; - tags?: string[]; - namespaceId: string; - }; - namespaceId: string; - accessToken: Nullable; - }) => { - if (!accessToken) { - throw new Error("accessToken not provided"); - } - if (!namespaceId) { - throw new Error("namespaceId not provided"); - } - if (!payload) { - throw new Error("payload must be provided"); - } - if (!payload.name) { - throw new Error("payload.name is required"); - } - - const client = getInstillCatalogAPIClient({ accessToken }); - const catalog = await client.catalog.createCatalog({ - namespaceId, - payload, - }); - return catalog; - }, - }); -} diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useCreateNamespaceCatalog.ts b/packages/toolkit/src/lib/react-query-service/catalog/useCreateNamespaceCatalog.ts new file mode 100644 index 000000000..774184f57 --- /dev/null +++ b/packages/toolkit/src/lib/react-query-service/catalog/useCreateNamespaceCatalog.ts @@ -0,0 +1,44 @@ +"use client"; + +import type { CreateNamespaceCatalogRequest, Nullable } from "instill-sdk"; +import { useMutation, useQueryClient } from "@tanstack/react-query"; + +import { getInstillCatalogAPIClient } from "../../sdk-helper"; +import { queryKeyStore } from "../queryKeyStore"; + +export function useCreateNamespaceCatalog() { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: async ({ + payload, + accessToken, + }: { + payload: CreateNamespaceCatalogRequest; + accessToken: Nullable; + }) => { + if (!accessToken) { + throw new Error("accessToken is required"); + } + + if (!payload.namespaceId) { + throw new Error("namespaceId is required"); + } + + if (!payload.name) { + throw new Error("payload.name is required"); + } + + const client = getInstillCatalogAPIClient({ accessToken }); + const res = await client.catalog.createNamespaceCatalog(payload); + return Promise.resolve(res.catalog); + }, + onSuccess: (_, variables) => { + queryClient.invalidateQueries({ + queryKey: queryKeyStore.catalog.getUseListNamespaceCatalogsQueryKey({ + namespaceId: variables.payload.namespaceId, + }), + }); + }, + }); +} diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useCreateNamespaceCatalogFile.ts b/packages/toolkit/src/lib/react-query-service/catalog/useCreateNamespaceCatalogFile.ts new file mode 100644 index 000000000..67673a222 --- /dev/null +++ b/packages/toolkit/src/lib/react-query-service/catalog/useCreateNamespaceCatalogFile.ts @@ -0,0 +1,48 @@ +"use client"; + +import type { CreateNamespaceCatalogFileRequest, Nullable } from "instill-sdk"; +import { useMutation, useQueryClient } from "@tanstack/react-query"; + +import { getInstillCatalogAPIClient } from "../../sdk-helper"; +import { queryKeyStore } from "../queryKeyStore"; + +export function useCreateNamespaceCatalogFile() { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: async ({ + payload, + accessToken, + }: { + payload: CreateNamespaceCatalogFileRequest; + accessToken: Nullable; + }) => { + if (!accessToken) { + throw new Error("accessToken is required"); + } + + if (!payload.namespaceId) { + throw new Error("namespaceId is required"); + } + + if (!payload.catalogId) { + throw new Error("catalogId is required"); + } + + const client = getInstillCatalogAPIClient({ accessToken }); + const res = await client.catalog.createNamespaceCatalogFile(payload); + + return Promise.resolve(res.file); + }, + onSuccess: (_, variables) => { + queryClient.invalidateQueries({ + queryKey: queryKeyStore.catalog.getUseListNamespaceCatalogFilesQueryKey( + { + namespaceId: variables.payload.namespaceId, + catalogId: variables.payload.catalogId, + }, + ), + }); + }, + }); +} diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useDeleteCatalogFile.tsx b/packages/toolkit/src/lib/react-query-service/catalog/useDeleteCatalogFile.tsx index 66f5402a4..34e753448 100644 --- a/packages/toolkit/src/lib/react-query-service/catalog/useDeleteCatalogFile.tsx +++ b/packages/toolkit/src/lib/react-query-service/catalog/useDeleteCatalogFile.tsx @@ -1,7 +1,10 @@ +"use client"; + +import type { Nullable } from "instill-sdk"; import { useMutation, useQueryClient } from "@tanstack/react-query"; -import { Nullable } from "instill-sdk"; import { getInstillCatalogAPIClient } from "../../sdk-helper"; +import { queryKeyStore } from "../queryKeyStore"; export function useDeleteCatalogFile() { const queryClient = useQueryClient(); @@ -9,25 +12,42 @@ export function useDeleteCatalogFile() { return useMutation({ mutationFn: async ({ fileUid, + namespaceId, + catalogId, accessToken, }: { fileUid: string; + namespaceId: Nullable; + catalogId: Nullable; accessToken: Nullable; }) => { if (!accessToken) { - throw new Error("accessToken not provided"); + throw new Error("accessToken is required"); } + if (!fileUid) { - throw new Error("fileUid not provided"); + throw new Error("fileUid is required"); } const client = getInstillCatalogAPIClient({ accessToken }); await client.catalog.deleteCatalogFile({ fileUid, }); + + return Promise.resolve({ + namespaceId, + catalogId, + }); }, - onSuccess: () => { - queryClient.invalidateQueries({ queryKey: ["catalogFiles"] }); + onSuccess: ({ namespaceId, catalogId }) => { + queryClient.invalidateQueries({ + queryKey: queryKeyStore.catalog.getUseListNamespaceCatalogFilesQueryKey( + { + namespaceId, + catalogId, + }, + ), + }); }, }); } diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useDeleteCatalog.ts b/packages/toolkit/src/lib/react-query-service/catalog/useDeleteNamespaceCatalog.ts similarity index 52% rename from packages/toolkit/src/lib/react-query-service/catalog/useDeleteCatalog.ts rename to packages/toolkit/src/lib/react-query-service/catalog/useDeleteNamespaceCatalog.ts index b46e1c2db..1f20cac02 100644 --- a/packages/toolkit/src/lib/react-query-service/catalog/useDeleteCatalog.ts +++ b/packages/toolkit/src/lib/react-query-service/catalog/useDeleteNamespaceCatalog.ts @@ -1,9 +1,12 @@ +"use client"; + +import type { Nullable } from "instill-sdk"; import { useMutation, useQueryClient } from "@tanstack/react-query"; -import { Catalog, Nullable } from "instill-sdk"; import { getInstillCatalogAPIClient } from "../../sdk-helper"; +import { queryKeyStore } from "../queryKeyStore"; -export function useDeleteCatalog() { +export function useDeleteNamespaceCatalog() { const queryClient = useQueryClient(); return useMutation({ @@ -17,29 +20,26 @@ export function useDeleteCatalog() { accessToken: Nullable; }) => { if (!accessToken) { - throw new Error("accessToken not provided"); + throw new Error("accessToken is required"); } + if (!namespaceId) { - throw new Error("namespaceId not provided"); + throw new Error("namespaceId is required"); } + if (!catalogId) { - throw new Error("catalogId not provided"); + throw new Error("catalogId is required"); } const client = getInstillCatalogAPIClient({ accessToken }); - await client.catalog.deleteCatalog({ namespaceId, catalogId }); + await client.catalog.deleteNamespaceCatalog({ namespaceId, catalogId }); }, onSuccess: (_, variables) => { queryClient.invalidateQueries({ - queryKey: ["catalogs", variables.namespaceId], + queryKey: queryKeyStore.catalog.getUseListNamespaceCatalogsQueryKey({ + namespaceId: variables.namespaceId, + }), }); - queryClient.setQueryData( - ["catalogs", variables.namespaceId], - (oldData) => - oldData?.filter( - (catalog) => catalog.catalogId !== variables.catalogId, - ) || [], - ); }, }); } diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useGetAllChunks.ts b/packages/toolkit/src/lib/react-query-service/catalog/useGetAllChunks.ts deleted file mode 100644 index 09b1e1cf2..000000000 --- a/packages/toolkit/src/lib/react-query-service/catalog/useGetAllChunks.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { useQuery } from "@tanstack/react-query"; -import { Nullable } from "instill-sdk"; - -import { getInstillCatalogAPIClient } from "../../sdk-helper"; - -export function useGetAllChunks({ - accessToken, - ownerName, - catalogId, - fileUid, - enabled, -}: { - accessToken: Nullable; - ownerName: string; - catalogId: string; - fileUid: string | undefined; - enabled: boolean; -}) { - return useQuery({ - queryKey: ["chunks", catalogId, fileUid], - queryFn: async () => { - if (!accessToken) { - throw new Error("accessToken not provided"); - } - if (!fileUid) { - throw new Error("fileUid not provided"); - } - - const client = getInstillCatalogAPIClient({ accessToken }); - const chunks = await client.catalog.listChunks({ - namespaceId: ownerName, - catalogId, - fileUid, - enablePagination: false, - }); - - return Promise.resolve(chunks); - }, - enabled: enabled && Boolean(accessToken) && Boolean(fileUid), - }); -} diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useGetFileDetails.ts b/packages/toolkit/src/lib/react-query-service/catalog/useGetFileDetails.ts deleted file mode 100644 index ef1560cee..000000000 --- a/packages/toolkit/src/lib/react-query-service/catalog/useGetFileDetails.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { useQuery } from "@tanstack/react-query"; -import { CatalogFile, Nullable } from "instill-sdk"; - -import { getInstillCatalogAPIClient } from "../../sdk-helper"; - -export function useGetFileDetails({ - fileUid, - accessToken, - enabled, - catalogId, - namespaceId, -}: { - fileUid: string; - accessToken: Nullable; - enabled: boolean; - catalogId: string; - namespaceId: string; -}) { - return useQuery({ - queryKey: ["fileDetails", fileUid], - queryFn: async () => { - if (!accessToken) { - throw new Error("accessToken not provided"); - } - if (!fileUid) { - throw new Error("fileUid not provided"); - } - if (!catalogId) { - throw new Error("catalogId not provided"); - } - if (!namespaceId) { - throw new Error("namespaceId not provided"); - } - - const client = getInstillCatalogAPIClient({ accessToken }); - const file = await client.catalog.getFileDetails({ - namespaceId, - catalogId, - fileId: fileUid, - }); - - return file; - }, - enabled, - }); -} diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useGetCatalogSingleSourceOfTruthFile.ts b/packages/toolkit/src/lib/react-query-service/catalog/useGetNamespaceCatalogSingleSourceOfTruthFile.ts similarity index 59% rename from packages/toolkit/src/lib/react-query-service/catalog/useGetCatalogSingleSourceOfTruthFile.ts rename to packages/toolkit/src/lib/react-query-service/catalog/useGetNamespaceCatalogSingleSourceOfTruthFile.ts index be354c8ea..5d8f2e778 100644 --- a/packages/toolkit/src/lib/react-query-service/catalog/useGetCatalogSingleSourceOfTruthFile.ts +++ b/packages/toolkit/src/lib/react-query-service/catalog/useGetNamespaceCatalogSingleSourceOfTruthFile.ts @@ -1,9 +1,11 @@ +"use client"; + +import type { Nullable } from "instill-sdk"; import { useQuery } from "@tanstack/react-query"; -import { Nullable } from "instill-sdk"; import { getInstillCatalogAPIClient } from "../../sdk-helper"; -export function useGetCatalogSingleSourceOfTruthFile({ +export function useGetNamespaceCatalogSingleSourceOfTruthFile({ fileUid, accessToken, enabled, @@ -16,31 +18,34 @@ export function useGetCatalogSingleSourceOfTruthFile({ catalogId: string; namespaceId: string; }) { - return useQuery({ + return useQuery({ queryKey: ["fileContent", fileUid], queryFn: async () => { if (!accessToken) { - throw new Error("accessToken not provided"); + throw new Error("accessToken is required"); } + if (!fileUid) { - throw new Error("fileUid not provided"); + throw new Error("fileUid is required"); } + if (!catalogId) { - throw new Error("catalogId not provided"); + throw new Error("catalogId is required"); } + if (!namespaceId) { - throw new Error("namespaceId not provided"); + throw new Error("namespaceId is required"); } const client = getInstillCatalogAPIClient({ accessToken }); - const fileContent = - await client.catalog.getCatalogSingleSourceOfTruthFile({ + const res = + await client.catalog.getNamespaceCatalogSingleSourceOfTruthFile({ namespaceId, catalogId, fileUid, }); - return Promise.resolve(fileContent.content); + return Promise.resolve(res.sourceFile); }, enabled: enabled && Boolean(accessToken) && Boolean(fileUid), }); diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useListChunks.ts b/packages/toolkit/src/lib/react-query-service/catalog/useListChunks.ts deleted file mode 100644 index bfb11d98a..000000000 --- a/packages/toolkit/src/lib/react-query-service/catalog/useListChunks.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { useQuery } from "@tanstack/react-query"; -import { Chunk, Nullable } from "instill-sdk"; - -import { getInstillCatalogAPIClient } from "../../sdk-helper"; - -export function useListChunks({ - catalogId, - accessToken, - enabled, - namespaceId, - fileUid, -}: { - catalogId: string; - accessToken: Nullable; - enabled: boolean; - namespaceId: string; - fileUid: string; -}) { - return useQuery({ - queryKey: ["chunks", catalogId, fileUid], - queryFn: async () => { - if (!accessToken) { - throw new Error("accessToken not provided"); - } - if (!catalogId) { - throw new Error("catalogId not provided"); - } - if (!namespaceId) { - throw new Error("namespaceId not provided"); - } - if (!fileUid) { - throw new Error("fileUid not provided"); - } - - const client = getInstillCatalogAPIClient({ accessToken }); - const chunks = await client.catalog.listChunks({ - namespaceId, - catalogId, - fileUid, - enablePagination: false, - }); - - return Promise.resolve(chunks); - }, - enabled: enabled && Boolean(accessToken), - }); -} diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useListNamespaceCatalogChunks.ts b/packages/toolkit/src/lib/react-query-service/catalog/useListNamespaceCatalogChunks.ts new file mode 100644 index 000000000..aa3216051 --- /dev/null +++ b/packages/toolkit/src/lib/react-query-service/catalog/useListNamespaceCatalogChunks.ts @@ -0,0 +1,62 @@ +"use client"; + +import type { Nullable } from "instill-sdk"; +import { useQuery } from "@tanstack/react-query"; + +import { getInstillCatalogAPIClient } from "../../sdk-helper"; +import { queryKeyStore } from "../queryKeyStore"; + +export function useListNamespaceCatalogChunks({ + accessToken, + namespaceId, + catalogId, + fileUid, + chunkUids, + enabled, +}: { + accessToken: Nullable; + namespaceId: Nullable; + catalogId: Nullable; + fileUid: Nullable; + chunkUids: Nullable; + enabled: boolean; +}) { + let enabledQuery = false; + + if (enabled && accessToken && namespaceId && catalogId) { + enabledQuery = true; + } + + return useQuery({ + queryKey: queryKeyStore.catalog.getUseListNamespaceCatalogChunksQueryKey({ + namespaceId, + catalogId, + fileUid, + chunkUids, + }), + queryFn: async () => { + if (!accessToken) { + throw new Error("accessToken is required"); + } + + if (!namespaceId) { + throw new Error("namespaceId is required"); + } + + if (!catalogId) { + throw new Error("catalogId is required"); + } + + const client = getInstillCatalogAPIClient({ accessToken }); + const res = await client.catalog.listNamespaceCatalogChunks({ + namespaceId, + catalogId, + fileUid: fileUid ?? undefined, + chunkUids: chunkUids ?? undefined, + }); + + return Promise.resolve(res.chunks); + }, + enabled: enabledQuery, + }); +} diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useListCatalogFiles.ts b/packages/toolkit/src/lib/react-query-service/catalog/useListNamespaceCatalogFiles.ts similarity index 56% rename from packages/toolkit/src/lib/react-query-service/catalog/useListCatalogFiles.ts rename to packages/toolkit/src/lib/react-query-service/catalog/useListNamespaceCatalogFiles.ts index 30b6a4d97..957c0c907 100644 --- a/packages/toolkit/src/lib/react-query-service/catalog/useListCatalogFiles.ts +++ b/packages/toolkit/src/lib/react-query-service/catalog/useListNamespaceCatalogFiles.ts @@ -1,9 +1,12 @@ +"use client"; + +import type { File, Nullable } from "instill-sdk"; import { useQuery } from "@tanstack/react-query"; -import { File, Nullable } from "instill-sdk"; import { getInstillCatalogAPIClient } from "../../sdk-helper"; +import { queryKeyStore } from "../queryKeyStore"; -export function useListCatalogFiles({ +export function useListNamespaceCatalogFiles({ namespaceId, catalogId, accessToken, @@ -15,26 +18,30 @@ export function useListCatalogFiles({ enabled: boolean; }) { return useQuery({ - queryKey: ["catalogFiles", namespaceId, catalogId], + queryKey: queryKeyStore.catalog.getUseListNamespaceCatalogFilesQueryKey({ + namespaceId, + catalogId, + }), queryFn: async () => { - if (!namespaceId) { - throw new Error("namespaceId not provided"); - } if (!accessToken) { - throw new Error("accessToken not provided"); + throw new Error("accessToken is required"); } + + if (!namespaceId) { + throw new Error("namespaceId is required"); + } + if (!catalogId) { - throw new Error("catalogId not provided"); + throw new Error("catalogId is required"); } const client = getInstillCatalogAPIClient({ accessToken }); - const files = await client.catalog.listCatalogFiles({ + const res = await client.catalog.listNamespaceCatalogFiles({ namespaceId, catalogId, - enablePagination: false, }); - return Promise.resolve(files); + return Promise.resolve(res); }, enabled: enabled && diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useGetCatalogs.ts b/packages/toolkit/src/lib/react-query-service/catalog/useListNamespaceCatalogs.ts similarity index 54% rename from packages/toolkit/src/lib/react-query-service/catalog/useGetCatalogs.ts rename to packages/toolkit/src/lib/react-query-service/catalog/useListNamespaceCatalogs.ts index 63974f7ab..2d1652bfe 100644 --- a/packages/toolkit/src/lib/react-query-service/catalog/useGetCatalogs.ts +++ b/packages/toolkit/src/lib/react-query-service/catalog/useListNamespaceCatalogs.ts @@ -1,9 +1,12 @@ +"use client"; + +import type { Nullable } from "instill-sdk"; import { useQuery } from "@tanstack/react-query"; -import { Nullable } from "instill-sdk"; import { getInstillCatalogAPIClient } from "../../sdk-helper"; +import { queryKeyStore } from "../queryKeyStore"; -export function useGetCatalogs({ +export function useListNamespaceCatalogs({ accessToken, namespaceId, enabled, @@ -13,22 +16,24 @@ export function useGetCatalogs({ enabled: boolean; }) { return useQuery({ - queryKey: ["catalogs", namespaceId], + queryKey: queryKeyStore.catalog.getUseListNamespaceCatalogsQueryKey({ + namespaceId, + }), queryFn: async () => { if (!namespaceId) { - throw new Error("namespaceId not provided"); + throw new Error("namespaceId is required"); } + if (!accessToken) { - throw new Error("accessToken not provided"); + throw new Error("accessToken is required"); } const client = getInstillCatalogAPIClient({ accessToken }); - const catalogs = await client.catalog.listCatalogs({ + const res = await client.catalog.listNamespaceCatalogs({ namespaceId, - enablePagination: false, }); - return Promise.resolve(catalogs); + return Promise.resolve(res.catalogs); }, enabled: enabled && Boolean(namespaceId) && Boolean(accessToken), }); diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useProcessCatalogFiles.ts b/packages/toolkit/src/lib/react-query-service/catalog/useProcessCatalogFiles.ts index 1c9a0290c..126ea372f 100644 --- a/packages/toolkit/src/lib/react-query-service/catalog/useProcessCatalogFiles.ts +++ b/packages/toolkit/src/lib/react-query-service/catalog/useProcessCatalogFiles.ts @@ -1,5 +1,7 @@ +"use client"; + +import type { CatalogFile, Nullable } from "instill-sdk"; import { useMutation, useQueryClient } from "@tanstack/react-query"; -import { CatalogFile, Nullable } from "instill-sdk"; import { getInstillCatalogAPIClient } from "../../sdk-helper"; @@ -17,12 +19,18 @@ export function useProcessCatalogFiles() { requesterUid: Nullable; }): Promise => { if (!accessToken) { - throw new Error("accessToken not provided"); + throw new Error("accessToken is required"); } + if (!requesterUid) { - throw new Error("requesterUid not provided"); + throw new Error("requesterUid is required"); } - if (!fileUids || fileUids.length === 0) { + + if (!fileUids) { + throw new Error("fileUids is required"); + } + + if (fileUids.length === 0) { throw new Error("fileUids must be a non-empty array"); } @@ -32,7 +40,7 @@ export function useProcessCatalogFiles() { requesterUid, }); - return files; + return Promise.resolve(files); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["catalogFiles"] }); diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useUpdateCatalog.ts b/packages/toolkit/src/lib/react-query-service/catalog/useUpdateCatalog.ts deleted file mode 100644 index 8ab1875d3..000000000 --- a/packages/toolkit/src/lib/react-query-service/catalog/useUpdateCatalog.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { useMutation, useQueryClient } from "@tanstack/react-query"; -import { Catalog, Nullable } from "instill-sdk"; - -import { getInstillCatalogAPIClient } from "../../sdk-helper"; - -export function useUpdateCatalog() { - const queryClient = useQueryClient(); - - return useMutation({ - mutationFn: async ({ - namespaceId, - catalogId, - payload, - accessToken, - }: { - namespaceId: string; - catalogId: string; - payload: { - name: string; - description?: string; - tags?: string[]; - }; - accessToken: Nullable; - }) => { - if (!accessToken) { - throw new Error("accessToken not provided"); - } - if (!namespaceId) { - throw new Error("namespaceId not provided"); - } - if (!catalogId) { - throw new Error("catalogId not provided"); - } - if (!payload || !payload.name) { - throw new Error("payload with at least a name is required"); - } - - const client = getInstillCatalogAPIClient({ accessToken }); - const catalog = await client.catalog.updateCatalog({ - namespaceId, - catalogId, - ...payload, - }); - - return Promise.resolve(catalog); - }, - onSuccess: (updatedCatalog, variables) => { - queryClient.invalidateQueries({ - queryKey: ["catalogs", variables.namespaceId], - }); - queryClient.setQueryData( - ["catalogs", variables.namespaceId], - (oldData) => - oldData?.map((catalog) => - catalog.catalogId === variables.catalogId - ? updatedCatalog - : catalog, - ) || [], - ); - }, - }); -} diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useUpdateCatalogChunk.ts b/packages/toolkit/src/lib/react-query-service/catalog/useUpdateCatalogChunk.ts new file mode 100644 index 000000000..9b71db221 --- /dev/null +++ b/packages/toolkit/src/lib/react-query-service/catalog/useUpdateCatalogChunk.ts @@ -0,0 +1,67 @@ +"use client"; + +import type { Nullable } from "instill-sdk"; +import { useMutation, useQueryClient } from "@tanstack/react-query"; + +import { getInstillCatalogAPIClient } from "../../sdk-helper"; +import { queryKeyStore } from "../queryKeyStore"; + +export function useUpdateCatalogChunk() { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: async ({ + // Because the mutation reponse didn't have these information, but we need them to + // update the cache, so we need to pass them in as variables. + namespaceId, + catalogId, + fileUid, + chunkUid, + accessToken, + retrievable, + }: { + namespaceId: Nullable; + catalogId: Nullable; + fileUid: Nullable; + chunkUid: Nullable; + accessToken: Nullable; + retrievable: boolean; + }) => { + if (!accessToken) { + throw new Error("accessToken is required"); + } + + if (!chunkUid) { + throw new Error("chunkUid is required"); + } + + if (retrievable === undefined || retrievable === null) { + throw new Error("retrievable flag is required"); + } + + const client = getInstillCatalogAPIClient({ accessToken }); + const res = await client.catalog.updateCatalogChunk({ + chunkUid, + retrievable, + }); + + return Promise.resolve({ + namespaceId, + catalogId, + fileUid, + response: res, + }); + }, + onSuccess: ({ namespaceId, catalogId, fileUid }) => { + queryClient.invalidateQueries({ + queryKey: + queryKeyStore.catalog.getUseListNamespaceCatalogChunksQueryKey({ + namespaceId, + catalogId, + fileUid, + chunkUids: null, + }), + }); + }, + }); +} diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useUpdateChunk.ts b/packages/toolkit/src/lib/react-query-service/catalog/useUpdateChunk.ts deleted file mode 100644 index b84483e9f..000000000 --- a/packages/toolkit/src/lib/react-query-service/catalog/useUpdateChunk.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { useMutation, useQueryClient } from "@tanstack/react-query"; -import { Nullable } from "instill-sdk"; - -import { getInstillCatalogAPIClient } from "../../sdk-helper"; - -export function useUpdateChunk() { - const queryClient = useQueryClient(); - - return useMutation({ - mutationFn: async ({ - chunkUid, - accessToken, - retrievable, - }: { - chunkUid: string; - accessToken: Nullable; - retrievable: boolean; - }) => { - if (!accessToken) { - throw new Error("accessToken not provided"); - } - if (!chunkUid) { - throw new Error("chunkUid not provided"); - } - if (retrievable === undefined || retrievable === null) { - throw new Error("retrievable flag must be provided"); - } - - const client = getInstillCatalogAPIClient({ accessToken }); - await client.catalog.updateChunk({ - chunkUid, - retrievable, - }); - }, - onSuccess: (_, variables) => { - queryClient.invalidateQueries({ - queryKey: ["chunks", variables.chunkUid], - }); - }, - }); -} diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useUpdateNamespaceCatalog.ts b/packages/toolkit/src/lib/react-query-service/catalog/useUpdateNamespaceCatalog.ts new file mode 100644 index 000000000..0dd816aee --- /dev/null +++ b/packages/toolkit/src/lib/react-query-service/catalog/useUpdateNamespaceCatalog.ts @@ -0,0 +1,45 @@ +"use client"; + +import type { Nullable, UpdateNamespaceCatalogRequest } from "instill-sdk"; +import { useMutation, useQueryClient } from "@tanstack/react-query"; + +import { getInstillCatalogAPIClient } from "../../sdk-helper"; +import { queryKeyStore } from "../queryKeyStore"; + +export function useUpdateNamespaceCatalog() { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: async ({ + payload, + accessToken, + }: { + payload: UpdateNamespaceCatalogRequest; + accessToken: Nullable; + }) => { + if (!accessToken) { + throw new Error("accessToken is required"); + } + + if (!payload.namespaceId) { + throw new Error("namespaceId is required"); + } + + if (!payload.catalogId) { + throw new Error("catalogId is required"); + } + + const client = getInstillCatalogAPIClient({ accessToken }); + const catalog = await client.catalog.updateNamespaceCatalog(payload); + + return Promise.resolve(catalog); + }, + onSuccess: (_, variables) => { + queryClient.invalidateQueries({ + queryKey: queryKeyStore.catalog.getUseListNamespaceCatalogsQueryKey({ + namespaceId: variables.payload.namespaceId, + }), + }); + }, + }); +} diff --git a/packages/toolkit/src/lib/react-query-service/catalog/useUploadCatalogFile.ts b/packages/toolkit/src/lib/react-query-service/catalog/useUploadCatalogFile.ts deleted file mode 100644 index 9eeba5790..000000000 --- a/packages/toolkit/src/lib/react-query-service/catalog/useUploadCatalogFile.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { useMutation, useQueryClient } from "@tanstack/react-query"; -import { Nullable } from "instill-sdk"; - -import { getInstillCatalogAPIClient } from "../../sdk-helper"; - -export function useUploadCatalogFile() { - const queryClient = useQueryClient(); - - return useMutation({ - mutationFn: async ({ - namespaceId, - catalogId, - payload, - accessToken, - }: { - namespaceId: string; - catalogId: string; - payload: { - name: string; - type: string; - content: string; - }; - accessToken: Nullable; - }) => { - if (!accessToken) { - throw new Error("accessToken not provided"); - } - if (!namespaceId) { - throw new Error("namespaceId not provided"); - } - if (!catalogId) { - throw new Error("catalogId not provided"); - } - if (!payload) { - throw new Error("payload must be provided"); - } - if (!payload.name) { - throw new Error("payload.name is required"); - } - if (!payload.type) { - throw new Error("payload.type is required"); - } - if (!payload.content) { - throw new Error("payload.content is required"); - } - - const client = getInstillCatalogAPIClient({ accessToken }); - const file = await client.catalog.uploadCatalogFile({ - namespaceId, - catalogId, - payload, - }); - - return Promise.resolve(file); - }, - onSuccess: (_, variables) => { - queryClient.invalidateQueries({ - queryKey: ["catalogFiles", variables.namespaceId, variables.catalogId], - }); - }, - }); -} diff --git a/packages/toolkit/src/lib/react-query-service/index.ts b/packages/toolkit/src/lib/react-query-service/index.ts index 97741e457..cb8b78f74 100644 --- a/packages/toolkit/src/lib/react-query-service/index.ts +++ b/packages/toolkit/src/lib/react-query-service/index.ts @@ -1,3 +1,4 @@ +export * from "./catalog"; export * from "./connector"; export * from "./hub"; export * from "./integration"; diff --git a/packages/toolkit/src/lib/react-query-service/queryKeyStore.ts b/packages/toolkit/src/lib/react-query-service/queryKeyStore.ts index a2010f8f1..2480ed16a 100644 --- a/packages/toolkit/src/lib/react-query-service/queryKeyStore.ts +++ b/packages/toolkit/src/lib/react-query-service/queryKeyStore.ts @@ -506,10 +506,53 @@ export const mgmtQueryKeyStore = { }, }; +export const catalogQueryKeyStore = { + getUseListNamespaceCatalogsQueryKey({ + namespaceId, + }: { + namespaceId: Nullable; + }) { + return [namespaceId, "catalogs"]; + }, + getUseListNamespaceCatalogFilesQueryKey({ + namespaceId, + catalogId, + }: { + namespaceId: Nullable; + catalogId: Nullable; + }) { + return [namespaceId, "catalogs", catalogId, "files"]; + }, + getUseListNamespaceCatalogChunksQueryKey({ + namespaceId, + catalogId, + fileUid, + chunkUids, + }: { + namespaceId: Nullable; + catalogId: Nullable; + fileUid: Nullable; + chunkUids: Nullable; + }) { + const queryKey = [namespaceId, "catalogs", catalogId]; + + if (fileUid) { + queryKey.push("files", fileUid); + } + + if (chunkUids) { + queryKey.push("chunks", ...chunkUids); + } + + return queryKey; + }, +}; + export const queryKeyStore = { model: modelQueryKeyStore, pipeline: pipelineQueryKeyStore, release: releaseQueryKeyStore, secret: secretQueryKeyStore, mgmt: mgmtQueryKeyStore, + catalog: catalogQueryKeyStore, }; diff --git a/packages/toolkit/src/view/catalog/CatalogMainView.tsx b/packages/toolkit/src/view/catalog/CatalogMainView.tsx index 94104141c..38728f794 100644 --- a/packages/toolkit/src/view/catalog/CatalogMainView.tsx +++ b/packages/toolkit/src/view/catalog/CatalogMainView.tsx @@ -10,15 +10,13 @@ import { GeneralAppPageProp, InstillStore, useAuthenticatedUserSubscription, + useDeleteNamespaceCatalog, useInstillStore, + useListNamespaceCatalogFiles, + useListNamespaceCatalogs, useOrganizationSubscription, useShallow, } from "../../lib"; -import { - useDeleteCatalog, - useGetCatalogs, - useListCatalogFiles, -} from "../../lib/react-query-service/catalog"; import { env } from "../../server"; import { Sidebar, WarnDiscardFilesDialog } from "./components"; import { CREDIT_TIMEOUT } from "./components/lib/constant"; @@ -71,16 +69,16 @@ export const CatalogMainView = (props: CatalogViewProps) => { ); const isLocalEnvironment = env("NEXT_PUBLIC_APP_ENV") === "CE"; - const deleteCatalog = useDeleteCatalog(); - const catalogs = useGetCatalogs({ + const deleteCatalog = useDeleteNamespaceCatalog(); + const catalogs = useListNamespaceCatalogs({ accessToken, namespaceId: selectedNamespace ?? null, enabled: enabledQuery && !!selectedNamespace, }); - const filesData = useListCatalogFiles({ + const filesData = useListNamespaceCatalogFiles({ namespaceId: selectedNamespace ?? null, - catalogId: selectedCatalog?.catalogId ?? "", + catalogId: selectedCatalog?.catalogId ?? null, accessToken, enabled: enabledQuery && Boolean(selectedNamespace) && Boolean(selectedCatalog), diff --git a/packages/toolkit/src/view/catalog/components/CloneCatalogDialog.tsx b/packages/toolkit/src/view/catalog/components/CloneCatalogDialog.tsx index 2716607c9..c6d7c3f04 100644 --- a/packages/toolkit/src/view/catalog/components/CloneCatalogDialog.tsx +++ b/packages/toolkit/src/view/catalog/components/CloneCatalogDialog.tsx @@ -24,7 +24,7 @@ import { useOrganizationSubscription, useShallow, } from "../../../lib"; -import { useGetCatalogs } from "../../../lib/react-query-service/catalog"; +import { useListNamespaceCatalogs } from "../../../lib/react-query-service/catalog"; import { useUserNamespaces } from "../../../lib/useUserNamespaces"; import { MAX_DESCRIPTION_LENGTH } from "./lib/constant"; import { @@ -121,7 +121,7 @@ export const CloneCatalogDialog = ({ getNamespaceType(); }, [selectedNamespace, accessToken]); - const catalogs = useGetCatalogs({ + const catalogs = useListNamespaceCatalogs({ accessToken, namespaceId: selectedNamespace, enabled: enabledQuery && !!selectedNamespace, diff --git a/packages/toolkit/src/view/catalog/components/CreateCatalogCard.tsx b/packages/toolkit/src/view/catalog/components/CreateCatalogCard.tsx index 52b2d4bf2..1954ca806 100644 --- a/packages/toolkit/src/view/catalog/components/CreateCatalogCard.tsx +++ b/packages/toolkit/src/view/catalog/components/CreateCatalogCard.tsx @@ -12,11 +12,13 @@ import { EditCatalogDialogData, } from "."; import { GeneralDeleteResourceDialog } from "../../../components"; -import { InstillStore, useInstillStore, useShallow } from "../../../lib"; import { - useGetAllChunks, - useListCatalogFiles, -} from "../../../lib/react-query-service/catalog"; + InstillStore, + useInstillStore, + useListNamespaceCatalogChunks, + useListNamespaceCatalogFiles, + useShallow, +} from "../../../lib"; import { convertTagsToArray } from "./lib/helpers"; type CreateCatalogCardProps = { @@ -54,21 +56,22 @@ export const CreateCatalogCard = ({ useShallow(selector), ); - const existingFiles = useListCatalogFiles({ + const existingFiles = useListNamespaceCatalogFiles({ namespaceId: selectedNamespace, catalogId: catalog.catalogId, accessToken: accessToken || null, enabled: enabledQuery && isHovered, }); - const chunks = useGetAllChunks({ + const chunks = useListNamespaceCatalogChunks({ accessToken, - ownerName: catalog.ownerName, + namespaceId: catalog.namespaceId, catalogId: catalog.catalogId, fileUid: existingFiles.isSuccess - ? existingFiles.data?.[0]?.fileUid - : undefined, + ? (existingFiles.data?.[0]?.fileUid ?? null) + : null, enabled: Boolean(existingFiles.data) && Boolean(accessToken) && isHovered, + chunkUids: null, }); const totalChunks = React.useMemo(() => { diff --git a/packages/toolkit/src/view/catalog/components/FileChunks.tsx b/packages/toolkit/src/view/catalog/components/FileChunks.tsx index 83e4c5d5c..ffe847467 100644 --- a/packages/toolkit/src/view/catalog/components/FileChunks.tsx +++ b/packages/toolkit/src/view/catalog/components/FileChunks.tsx @@ -5,10 +5,8 @@ import { Catalog, CatalogFile, Chunk } from "instill-sdk"; import { cn, Icons, Nullable } from "@instill-ai/design-system"; -import { - useGetCatalogSingleSourceOfTruthFile, - useListChunks, -} from "../../../lib/react-query-service/catalog"; +import { useGetNamespaceCatalogSingleSourceOfTruthFile } from "../../.."; +import { useListNamespaceCatalogChunks } from "../../../lib"; import ChunkCard from "./ChunkCard"; type FileChunksProps = { @@ -33,21 +31,23 @@ const FileChunks = ({ onChunkClick, onRetrievableToggle, }: FileChunksProps) => { - const { data: chunks, refetch } = useListChunks({ + const namespaceCatalogChunks = useListNamespaceCatalogChunks({ catalogId: catalog.catalogId, accessToken: accessToken || null, enabled: expanded, namespaceId: catalog.ownerName, fileUid: file.fileUid, + chunkUids: null, }); - const { data: fileContent } = useGetCatalogSingleSourceOfTruthFile({ - fileUid: file.fileUid, - catalogId: catalog.catalogId, - accessToken: accessToken || null, - enabled: expanded, - namespaceId: catalog.ownerName, - }); + const catalogSingleSourceOfTruthFile = + useGetNamespaceCatalogSingleSourceOfTruthFile({ + fileUid: file.fileUid, + catalogId: catalog.catalogId, + accessToken: accessToken || null, + enabled: expanded, + namespaceId: catalog.ownerName, + }); const isProcessing = file.processStatus !== "FILE_PROCESS_STATUS_COMPLETED"; @@ -63,7 +63,7 @@ const FileChunks = ({ // Set up an interval to refetch chunks if the file is still processing if (isProcessing) { interval = setInterval(() => { - refetch(); + namespaceCatalogChunks.refetch(); }, 5000); } @@ -72,7 +72,7 @@ const FileChunks = ({ clearInterval(interval); } }; - }, [isProcessing, refetch]); + }, [isProcessing, namespaceCatalogChunks.refetch]); return (
@@ -106,20 +106,27 @@ const FileChunks = ({

)}
- {expanded && !isProcessing && chunks && chunks.length > 0 && ( -
- {chunks.map((chunk: Chunk, i: number) => ( - onChunkClick(file, chunk)} - onRetrievableToggle={onRetrievableToggle} - fileContent={fileContent || ""} - /> - ))} -
- )} + {expanded && + !isProcessing && + namespaceCatalogChunks.isSuccess && + namespaceCatalogChunks.data.length > 0 && ( +
+ {namespaceCatalogChunks.data.map((chunk: Chunk, i: number) => ( + onChunkClick(file, chunk)} + onRetrievableToggle={onRetrievableToggle} + fileContent={ + catalogSingleSourceOfTruthFile.isSuccess + ? catalogSingleSourceOfTruthFile.data.content + : "" + } + /> + ))} +
+ )} ); }; diff --git a/packages/toolkit/src/view/catalog/components/FileDetailsOverlay.tsx b/packages/toolkit/src/view/catalog/components/FileDetailsOverlay.tsx index a1c19d75c..c398cba8d 100644 --- a/packages/toolkit/src/view/catalog/components/FileDetailsOverlay.tsx +++ b/packages/toolkit/src/view/catalog/components/FileDetailsOverlay.tsx @@ -8,9 +8,9 @@ import sanitizeHtml from "sanitize-html"; import { Dialog, ScrollArea, Skeleton } from "@instill-ai/design-system"; import { - useGetCatalogSingleSourceOfTruthFile, - useListChunks, -} from "../../../lib/react-query-service/catalog"; + useGetNamespaceCatalogSingleSourceOfTruthFile, + useListNamespaceCatalogChunks, +} from "../../../lib"; import { getFileIcon } from "./lib/helpers"; type FileDetailsOverlayProps = { @@ -40,8 +40,8 @@ const FileDetailsOverlay = ({ highlightChunk = false, fileType, }: FileDetailsOverlayProps) => { - const { data: fileContent, isLoading: isLoadingContent } = - useGetCatalogSingleSourceOfTruthFile({ + const catalogSingleSourceOfTruthFile = + useGetNamespaceCatalogSingleSourceOfTruthFile({ fileUid, catalogId, accessToken, @@ -49,18 +49,19 @@ const FileDetailsOverlay = ({ namespaceId, }); - const { data: chunks } = useListChunks({ + const namespaceCatalogChunks = useListNamespaceCatalogChunks({ catalogId, accessToken, enabled: isOpen && highlightChunk, namespaceId, fileUid, + chunkUids: null, }); const highlightChunkInContent = React.useCallback( (content: string, chunkUid?: string) => { if (!highlightChunk || !chunkUid || !content) return content; - const chunk = chunks?.find( + const chunk = namespaceCatalogChunks.data?.find( (c: { chunkUid: string }) => c.chunkUid === chunkUid, ); if (!chunk) return content; @@ -95,13 +96,23 @@ const FileDetailsOverlay = ({ .join("\n"); } }, - [chunks, highlightChunk, fileType], + [namespaceCatalogChunks.data, highlightChunk, fileType], ); const displayContent = React.useMemo( () => - fileContent ? highlightChunkInContent(fileContent, selectedChunkUid) : "", - [fileContent, highlightChunkInContent, selectedChunkUid], + catalogSingleSourceOfTruthFile.isSuccess + ? highlightChunkInContent( + catalogSingleSourceOfTruthFile.data.content, + selectedChunkUid, + ) + : "", + [ + catalogSingleSourceOfTruthFile.data, + catalogSingleSourceOfTruthFile.isSuccess, + highlightChunkInContent, + selectedChunkUid, + ], ); const sanitizedHtmlText = React.useMemo( @@ -134,7 +145,7 @@ const FileDetailsOverlay = ({ - {isLoadingContent ? ( + {catalogSingleSourceOfTruthFile.isLoading ? (
diff --git a/packages/toolkit/src/view/catalog/components/lib/helpers.tsx b/packages/toolkit/src/view/catalog/components/lib/helpers.tsx index 2cdd415fc..33c968661 100644 --- a/packages/toolkit/src/view/catalog/components/lib/helpers.tsx +++ b/packages/toolkit/src/view/catalog/components/lib/helpers.tsx @@ -2,6 +2,7 @@ import { Catalog, CatalogFile, FileStatus, + FileType, Nullable, OrganizationSubscription, OrganizationSubscriptionPlan, @@ -56,7 +57,7 @@ export const getFileIcon = (fileType: string) => { } }; -export const getFileType = (file: File) => { +export const getFileType = (file: File): FileType => { const extension = file.name.split(".").pop()?.toLowerCase(); switch (extension) { case "txt": diff --git a/packages/toolkit/src/view/catalog/components/tabs/CatalogFilesTab.tsx b/packages/toolkit/src/view/catalog/components/tabs/CatalogFilesTab.tsx index 0442799e3..f7f4df894 100644 --- a/packages/toolkit/src/view/catalog/components/tabs/CatalogFilesTab.tsx +++ b/packages/toolkit/src/view/catalog/components/tabs/CatalogFilesTab.tsx @@ -14,13 +14,11 @@ import { Separator, Skeleton } from "@instill-ai/design-system"; import { InstillStore, useAuthenticatedUser, + useDeleteCatalogFile, useInstillStore, + useListNamespaceCatalogFiles, useShallow, } from "../../../../lib"; -import { - useDeleteCatalogFile, - useListCatalogFiles, -} from "../../../../lib/react-query-service/catalog"; import { EmptyState } from "../EmptyState"; import FileDetailsOverlay from "../FileDetailsOverlay"; import { FileTable } from "../FileTable"; @@ -69,7 +67,7 @@ export const CatalogFilesTab = ({ accessToken, }); - const filesData = useListCatalogFiles({ + const filesData = useListNamespaceCatalogFiles({ namespaceId: selectedNamespace, catalogId: catalog.catalogId, accessToken, @@ -137,6 +135,8 @@ export const CatalogFilesTab = ({ await deleteCatalogFile.mutateAsync({ fileUid, accessToken, + namespaceId: catalog.namespaceId, + catalogId: catalog.catalogId, }); updateRemainingSpace(fileToDelete.size, false); } diff --git a/packages/toolkit/src/view/catalog/components/tabs/CatalogMainTab.tsx b/packages/toolkit/src/view/catalog/components/tabs/CatalogMainTab.tsx index 35db5a729..b6e2e1184 100644 --- a/packages/toolkit/src/view/catalog/components/tabs/CatalogMainTab.tsx +++ b/packages/toolkit/src/view/catalog/components/tabs/CatalogMainTab.tsx @@ -3,20 +3,24 @@ import * as React from "react"; import { Catalog, + CreateNamespaceCatalogRequest, Nullable, OrganizationSubscription, + UpdateNamespaceCatalogRequest, UserSubscription, } from "instill-sdk"; import * as z from "zod"; import { Separator, Skeleton } from "@instill-ai/design-system"; -import { InstillStore, useInstillStore, useShallow } from "../../../../lib"; import { - useCreateCatalog, - useGetCatalogs, - useUpdateCatalog, -} from "../../../../lib/react-query-service/catalog"; + InstillStore, + useCreateNamespaceCatalog, + useInstillStore, + useListNamespaceCatalogs, + useShallow, + useUpdateNamespaceCatalog, +} from "../../../../lib"; import { CatalogCard } from "../CatalogCard"; import CatalogSearchSort, { SortAnchor, SortOrder } from "../CatalogSearchSort"; import { CreateCatalogCard } from "../CreateCatalogCard"; @@ -66,12 +70,12 @@ export const CatalogTab = ({ useShallow(selector), ); - const createCatalog = useCreateCatalog(); - const updateCatalog = useUpdateCatalog(); + const createCatalog = useCreateNamespaceCatalog(); + const updateCatalog = useUpdateNamespaceCatalog(); const isEnterprisePlan = subscription?.plan === "PLAN_ENTERPRISE"; const isTeamPlan = subscription?.plan === "PLAN_TEAM"; - const catalogState = useGetCatalogs({ + const catalogState = useListNamespaceCatalogs({ accessToken, namespaceId: selectedNamespace ?? null, enabled: enabledQuery && !!selectedNamespace, @@ -88,15 +92,16 @@ export const CatalogTab = ({ ) => { if (!accessToken) return; + const payload: CreateNamespaceCatalogRequest = { + name: data.name, + description: data.description, + tags: convertTagsToArray(data.tags), + namespaceId: data.namespaceId, + }; + try { await createCatalog.mutateAsync({ - payload: { - name: data.name, - description: data.description, - tags: convertTagsToArray(data.tags), - namespaceId: data.namespaceId, - }, - namespaceId: data.namespaceId, + payload, accessToken, }); catalogState.refetch(); @@ -111,15 +116,17 @@ export const CatalogTab = ({ catalogId: string, ) => { if (!selectedNamespace || !accessToken) return; + + const payload: UpdateNamespaceCatalogRequest = { + namespaceId: selectedNamespace, + catalogId: catalogId, + description: data.description, + tags: convertTagsToArray(data.tags), + }; + try { await updateCatalog.mutateAsync({ - namespaceId: selectedNamespace, - catalogId: catalogId, - payload: { - name: data.name, - description: data.description, - tags: convertTagsToArray(data.tags), - }, + payload, accessToken, }); catalogState.refetch(); @@ -134,19 +141,16 @@ export const CatalogTab = ({ ) => { if (!accessToken) return; - const clonedCatalog = { + const clonedCatalog: CreateNamespaceCatalogRequest = { name: `${catalog.name}-clone`, description: catalog.description ?? "", tags: catalog.tags ?? [], + namespaceId: newNamespaceId, }; try { await createCatalog.mutateAsync({ - payload: { - ...clonedCatalog, - namespaceId: newNamespaceId, - }, - namespaceId: newNamespaceId, + payload: clonedCatalog, accessToken, }); catalogState.refetch(); diff --git a/packages/toolkit/src/view/catalog/components/tabs/ChunkTab.tsx b/packages/toolkit/src/view/catalog/components/tabs/ChunkTab.tsx index a4f86ea00..3544870a0 100644 --- a/packages/toolkit/src/view/catalog/components/tabs/ChunkTab.tsx +++ b/packages/toolkit/src/view/catalog/components/tabs/ChunkTab.tsx @@ -10,12 +10,10 @@ import { InstillStore, useAuthenticatedUser, useInstillStore, + useListNamespaceCatalogFiles, useShallow, + useUpdateCatalogChunk, } from "../../../../lib"; -import { - useListCatalogFiles, - useUpdateChunk, -} from "../../../../lib/react-query-service/catalog"; import FileChunks from "../FileChunks"; import FileDetailsOverlay from "../FileDetailsOverlay"; @@ -47,7 +45,7 @@ export const ChunkTab = ({ catalog, onGoToUpload }: ChunkTabProps) => { accessToken, }); - const catalogFiles = useListCatalogFiles({ + const namespaceCatalogFiles = useListNamespaceCatalogFiles({ namespaceId: selectedNamespace, catalogId: catalog.catalogId, accessToken, @@ -55,15 +53,15 @@ export const ChunkTab = ({ catalog, onGoToUpload }: ChunkTabProps) => { }); const files = React.useMemo(() => { - if (!catalogFiles.isSuccess) { + if (!namespaceCatalogFiles.isSuccess) { return []; } - return (catalogFiles.data || []).filter( + return (namespaceCatalogFiles.data || []).filter( (file) => file.processStatus !== "FILE_PROCESS_STATUS_FAILED", ); - }, [catalogFiles.isSuccess, catalogFiles.data]); + }, [namespaceCatalogFiles.isSuccess, namespaceCatalogFiles.data]); - const updateChunkMutation = useUpdateChunk(); + const updateCatalogChunk = useUpdateCatalogChunk(); const toggleFileExpansion = (fileUid: string) => { setExpandedFiles((prev) => @@ -90,7 +88,10 @@ export const ChunkTab = ({ catalog, onGoToUpload }: ChunkTabProps) => { currentValue: boolean, ) => { try { - await updateChunkMutation.mutateAsync({ + await updateCatalogChunk.mutateAsync({ + namespaceId: catalog.namespaceId, + catalogId: catalog.catalogId, + fileUid: null, chunkUid, accessToken, retrievable: !currentValue, @@ -109,7 +110,7 @@ export const ChunkTab = ({ catalog, onGoToUpload }: ChunkTabProps) => { ) ) { interval = setInterval(() => { - catalogFiles.refetch(); + namespaceCatalogFiles.refetch(); }, 5000); } @@ -118,7 +119,7 @@ export const ChunkTab = ({ catalog, onGoToUpload }: ChunkTabProps) => { clearInterval(interval); } }; - }, [files, catalogFiles]); + }, [files, namespaceCatalogFiles.refetch]); return (
@@ -128,7 +129,7 @@ export const ChunkTab = ({ catalog, onGoToUpload }: ChunkTabProps) => {

- {catalogFiles.isLoading ? ( + {namespaceCatalogFiles.isLoading ? (
{Array.from({ length: 6 }).map((_, index) => (
>(null); - const uploadCatalogFile = useUploadCatalogFile(); + const createNamespaceCatalogFile = useCreateNamespaceCatalogFile(); const processCatalogFiles = useProcessCatalogFiles(); const { accessToken, navigationNamespaceAnchor, enabledQuery } = @@ -167,7 +166,7 @@ export const UploadExploreTab = ({ const userNamespaces = useUserNamespaces(); - const existingFiles = useListCatalogFiles({ + const namespaceCatalogFiles = useListNamespaceCatalogFiles({ namespaceId: navigationNamespaceAnchor, catalogId: catalog.catalogId, accessToken, @@ -209,7 +208,7 @@ export const UploadExploreTab = ({ file, planMaxFileSize, remainingStorageSpace, - existingFiles.data || [], + namespaceCatalogFiles.data || [], ); if (!validationResult.isValid) { @@ -283,7 +282,11 @@ export const UploadExploreTab = ({ setIsProcessing(true); const files = form.getValues("files"); - if (files.length === 0 || !userNamespaces.isSuccess) { + if ( + files.length === 0 || + !userNamespaces.isSuccess || + !navigationNamespaceAnchor + ) { setIsProcessing(false); return; } @@ -308,14 +311,16 @@ export const UploadExploreTab = ({ try { const content = await readFileAsBase64(file); - const uploadedFile = await uploadCatalogFile.mutateAsync({ - namespaceId: navigationNamespaceAnchor ?? "", + const payload: CreateNamespaceCatalogFileRequest = { + namespaceId: navigationNamespaceAnchor ?? null, catalogId: catalog.catalogId, - payload: { - name: file.name, - type: getFileType(file), - content, - }, + name: file.name, + type: getFileType(file), + content, + }; + + const uploadedFile = await createNamespaceCatalogFile.mutateAsync({ + payload, accessToken, });