From 74dbd41e3d942b31975639285320df068149ee6c Mon Sep 17 00:00:00 2001 From: Mahesh Sanikommu Date: Sun, 14 Jun 2026 00:20:33 -0700 Subject: [PATCH] Fix batch payload shape and deselect-to-one bulk submit - Send batch documents as {content} objects (matches the endpoint's existing callers) instead of a raw string array. - Treat bulkUrls as the source of truth: 1 remaining selection saves via single-add instead of erroring. --- apps/web/components/add-document/index.tsx | 20 +++++++++++++++----- apps/web/hooks/use-document-mutations.ts | 10 ++++++---- packages/lib/api.ts | 13 ++++++++++++- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/apps/web/components/add-document/index.tsx b/apps/web/components/add-document/index.tsx index 68c2d2ce..1c231f7a 100644 --- a/apps/web/components/add-document/index.tsx +++ b/apps/web/components/add-document/index.tsx @@ -188,11 +188,21 @@ export function AddDocument({ const handleLinkSubmit = useCallback( (data: LinkData) => { - if (data.bulkUrls && data.bulkUrls.length >= 2) { - bulkLinkMutation.mutate({ - urls: data.bulkUrls, - project: localSelectedProject, - }) + // In bulk mode the selection is the source of truth, not the raw textarea. + if (data.bulkUrls) { + if (data.bulkUrls.length >= 2) { + bulkLinkMutation.mutate({ + urls: data.bulkUrls, + project: localSelectedProject, + }) + return + } + const [onlyUrl] = data.bulkUrls + if (onlyUrl) { + linkMutation.mutate({ url: onlyUrl, project: localSelectedProject }) + return + } + toast.error("Select at least one link") return } if (!data.url.trim()) { diff --git a/apps/web/hooks/use-document-mutations.ts b/apps/web/hooks/use-document-mutations.ts index 7ebd53fa..9509456b 100644 --- a/apps/web/hooks/use-document-mutations.ts +++ b/apps/web/hooks/use-document-mutations.ts @@ -377,10 +377,12 @@ export function useDocumentMutations({ const chunk = urls.slice(i, i + BULK_LINK_BATCH_SIZE) const response = await $fetch("@post/documents/batch", { body: { - documents: chunk, - containerTag: project, - entityContext, - metadata: { sm_source: "consumer" }, + documents: chunk.map((url) => ({ + content: url, + containerTags: [project], + entityContext, + metadata: { sm_source: "consumer" }, + })), }, }) if (response.error) { diff --git a/packages/lib/api.ts b/packages/lib/api.ts index 43a70918..483a3f65 100644 --- a/packages/lib/api.ts +++ b/packages/lib/api.ts @@ -193,7 +193,18 @@ export const apiSchema = createSchema({ }, "@post/documents/batch": { input: z.object({ - documents: z.array(z.string()).min(1).max(600), + documents: z + .array( + z.object({ + content: z.string(), + containerTags: z.array(z.string()).optional(), + containerTag: z.string().optional(), + entityContext: z.string().max(1500).optional(), + metadata: z.record(z.unknown()).optional(), + }), + ) + .min(1) + .max(600), containerTag: z.string().optional(), entityContext: z.string().max(1500).optional(), metadata: z.record(z.unknown()).optional(),