Skip to content

Commit d5654bc

Browse files
authored
Lucas/fix file upload parents (#9957)
* Fixed wrong tableId in parents when updating a table * slightly improved typing, still not good enough * Got rid of unused endpoint and function * Removed types for endpoint * turned name into document_id in upsertArgs * linter spread fix * more linter fixes
1 parent 76c86e5 commit d5654bc

File tree

4 files changed

+42
-205
lines changed

4 files changed

+42
-205
lines changed

front/lib/api/files/upsert.ts

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import { pipeline } from "stream/promises";
2020

2121
import { runAction } from "@app/lib/actions/server";
2222
import config from "@app/lib/api/config";
23+
import type {
24+
UpsertDocumentArgs,
25+
UpsertTableArgs,
26+
} from "@app/lib/api/data_sources";
2327
import { upsertDocument, upsertTable } from "@app/lib/api/data_sources";
2428
import type { Authenticator } from "@app/lib/auth";
2529
import type { DustError } from "@app/lib/error";
@@ -208,7 +212,11 @@ const upsertDocumentToDatasource: ProcessingFunction = async ({
208212
}) => {
209213
// Use the file id as the document id to make it easy to track the document back to the file.
210214
const sourceUrl = file.getPrivateUrl(auth);
211-
const documentId = upsertArgs?.document_id ?? file.sId; // Use the file sId as a fallback to make it easy to track the table back to the file.
215+
let documentId = file.sId;
216+
if (upsertArgs && "document_id" in upsertArgs) {
217+
documentId = upsertArgs.document_id;
218+
}
219+
const { title: upsertTitle, ...restArgs } = upsertArgs ?? {};
212220
const upsertDocumentRes = await upsertDocument({
213221
document_id: documentId,
214222
source_url: sourceUrl,
@@ -219,10 +227,10 @@ const upsertDocumentToDatasource: ProcessingFunction = async ({
219227
dataSource,
220228
auth,
221229
mime_type: file.contentType,
222-
title: file.fileName,
230+
title: upsertTitle ?? file.fileName,
223231

224232
// Used to override defaults.
225-
...(upsertArgs ?? {}),
233+
...restArgs,
226234
});
227235

228236
if (upsertDocumentRes.isErr()) {
@@ -244,7 +252,12 @@ const upsertTableToDatasource: ProcessingFunction = async ({
244252
dataSource,
245253
upsertArgs,
246254
}) => {
247-
const tableId = upsertArgs?.tableId ?? file.sId; // Use the file sId as a fallback for the table_id to make it easy to track the table back to the file.
255+
// Use the file sId as the table id to make it easy to track the table back to the file.
256+
let tableId = file.sId;
257+
if (upsertArgs && "tableId" in upsertArgs) {
258+
tableId = upsertArgs.tableId ?? tableId;
259+
}
260+
const { title: upsertTitle, ...restArgs } = upsertArgs ?? {};
248261
const upsertTableRes = await upsertTable({
249262
tableId,
250263
name: slugify(file.fileName),
@@ -257,11 +270,11 @@ const upsertTableToDatasource: ProcessingFunction = async ({
257270
dataSource,
258271
auth,
259272
useAppForHeaderDetection: true,
260-
title: file.fileName,
273+
title: upsertTitle ?? file.fileName,
261274
mimeType: file.contentType,
262275

263276
// Used to override defaults, for manual file uploads where some fields are user-defined.
264-
...(upsertArgs ?? {}),
277+
...restArgs,
265278
});
266279

267280
if (upsertTableRes.isErr()) {
@@ -287,7 +300,17 @@ type ProcessingFunction = ({
287300
file: FileResource;
288301
content: string;
289302
dataSource: DataSourceResource;
290-
upsertArgs?: Record<string, string>;
303+
upsertArgs?:
304+
| Pick<UpsertDocumentArgs, "document_id" | "title" | "tags">
305+
| Pick<
306+
UpsertTableArgs,
307+
| "name"
308+
| "title"
309+
| "description"
310+
| "tableId"
311+
| "tags"
312+
| "useAppForHeaderDetection"
313+
>;
291314
}) => Promise<Result<undefined, Error>>;
292315

293316
const getProcessingFunction = ({
@@ -403,7 +426,17 @@ export async function processAndUpsertToDataSource(
403426
}: {
404427
file: FileResource;
405428
optionalContent?: string;
406-
upsertArgs?: Record<string, string>;
429+
upsertArgs?:
430+
| Pick<UpsertDocumentArgs, "document_id" | "title" | "tags">
431+
| Pick<
432+
UpsertTableArgs,
433+
| "name"
434+
| "title"
435+
| "description"
436+
| "tableId"
437+
| "tags"
438+
| "useAppForHeaderDetection"
439+
>;
407440
}
408441
): Promise<
409442
Result<

front/lib/swr/data_source_view_tables.ts

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { useSendNotification } from "@dust-tt/sparkle";
22
import type { DataSourceViewType, LightWorkspaceType } from "@dust-tt/types";
3-
import type {
4-
PatchDataSourceTableRequestBody,
5-
PostDataSourceTableRequestBody,
6-
} from "@dust-tt/types";
3+
import type { PatchDataSourceTableRequestBody } from "@dust-tt/types";
74
import { useMemo } from "react";
85
import type { Fetcher } from "swr";
96

@@ -15,7 +12,6 @@ import {
1512
} from "@app/lib/swr/swr";
1613
import type { ListTablesResponseBody } from "@app/pages/api/w/[wId]/spaces/[spaceId]/data_source_views/[dsvId]/tables";
1714
import type { GetDataSourceViewTableResponseBody } from "@app/pages/api/w/[wId]/spaces/[spaceId]/data_source_views/[dsvId]/tables/[tableId]";
18-
import type { PostTableResponseBody } from "@app/pages/api/w/[wId]/spaces/[spaceId]/data_sources/[dsId]/tables";
1915
import type { PatchTableResponseBody } from "@app/pages/api/w/[wId]/spaces/[spaceId]/data_sources/[dsId]/tables/[tableId]";
2016

2117
export function useDataSourceViewTable({
@@ -133,49 +129,3 @@ export function useUpdateDataSourceViewTable(
133129

134130
return doUpdate;
135131
}
136-
137-
export function useCreateDataSourceTable(
138-
owner: LightWorkspaceType,
139-
dataSourceView: DataSourceViewType
140-
) {
141-
const { mutateRegardlessOfQueryParams: mutateContentNodes } =
142-
useDataSourceViewContentNodes({
143-
owner,
144-
dataSourceView,
145-
disabled: true, // Needed just to mutate
146-
});
147-
const sendNotification = useSendNotification();
148-
149-
const doCreate = async (body: PostDataSourceTableRequestBody) => {
150-
const tableUrl = `/api/w/${owner.sId}/spaces/${dataSourceView.spaceId}/data_sources/${dataSourceView.dataSource.sId}/tables`;
151-
const res = await fetch(tableUrl, {
152-
method: "POST",
153-
body: JSON.stringify(body),
154-
headers: {
155-
"Content-Type": "application/json",
156-
},
157-
});
158-
if (!res.ok) {
159-
const errorData = await getErrorFromResponse(res);
160-
sendNotification({
161-
type: "error",
162-
title: "Error creating table",
163-
description: `Error: ${errorData.message}`,
164-
});
165-
return null;
166-
} else {
167-
void mutateContentNodes();
168-
169-
sendNotification({
170-
type: "success",
171-
title: "Table created",
172-
description: "Table has been created",
173-
});
174-
175-
const response: PostTableResponseBody = await res.json();
176-
return response.table;
177-
}
178-
};
179-
180-
return doCreate;
181-
}

front/pages/api/w/[wId]/spaces/[spaceId]/data_sources/[dsId]/tables/index.ts

Lines changed: 0 additions & 134 deletions
This file was deleted.

types/src/front/api_handlers/public/data_sources.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,6 @@ export type PatchDataSourceTableRequestBody = t.TypeOf<
8181
typeof PatchDataSourceTableRequestBodySchema
8282
>;
8383

84-
// Post and Patch require the same request body
85-
export type PostDataSourceTableRequestBody = t.TypeOf<
86-
typeof PatchDataSourceTableRequestBodySchema
87-
>;
88-
89-
export const PostDataSourceTableRequestBodySchema = t.intersection([
90-
PatchDataSourceTableRequestBodySchema,
91-
t.type({
92-
csv: t.string,
93-
}),
94-
]);
95-
9684
export const UpsertTableFromCsvRequestSchema = t.intersection([
9785
t.type({
9886
name: t.string,

0 commit comments

Comments
 (0)