Skip to content

Commit

Permalink
Lucas/fix file upload parents (#9957)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
overmode authored Jan 14, 2025
1 parent 76c86e5 commit d5654bc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 205 deletions.
49 changes: 41 additions & 8 deletions front/lib/api/files/upsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import { pipeline } from "stream/promises";

import { runAction } from "@app/lib/actions/server";
import config from "@app/lib/api/config";
import type {
UpsertDocumentArgs,
UpsertTableArgs,
} from "@app/lib/api/data_sources";
import { upsertDocument, upsertTable } from "@app/lib/api/data_sources";
import type { Authenticator } from "@app/lib/auth";
import type { DustError } from "@app/lib/error";
Expand Down Expand Up @@ -208,7 +212,11 @@ const upsertDocumentToDatasource: ProcessingFunction = async ({
}) => {
// Use the file id as the document id to make it easy to track the document back to the file.
const sourceUrl = file.getPrivateUrl(auth);
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.
let documentId = file.sId;
if (upsertArgs && "document_id" in upsertArgs) {
documentId = upsertArgs.document_id;
}
const { title: upsertTitle, ...restArgs } = upsertArgs ?? {};
const upsertDocumentRes = await upsertDocument({
document_id: documentId,
source_url: sourceUrl,
Expand All @@ -219,10 +227,10 @@ const upsertDocumentToDatasource: ProcessingFunction = async ({
dataSource,
auth,
mime_type: file.contentType,
title: file.fileName,
title: upsertTitle ?? file.fileName,

// Used to override defaults.
...(upsertArgs ?? {}),
...restArgs,
});

if (upsertDocumentRes.isErr()) {
Expand All @@ -244,7 +252,12 @@ const upsertTableToDatasource: ProcessingFunction = async ({
dataSource,
upsertArgs,
}) => {
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.
// Use the file sId as the table id to make it easy to track the table back to the file.
let tableId = file.sId;
if (upsertArgs && "tableId" in upsertArgs) {
tableId = upsertArgs.tableId ?? tableId;
}
const { title: upsertTitle, ...restArgs } = upsertArgs ?? {};
const upsertTableRes = await upsertTable({
tableId,
name: slugify(file.fileName),
Expand All @@ -257,11 +270,11 @@ const upsertTableToDatasource: ProcessingFunction = async ({
dataSource,
auth,
useAppForHeaderDetection: true,
title: file.fileName,
title: upsertTitle ?? file.fileName,
mimeType: file.contentType,

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

if (upsertTableRes.isErr()) {
Expand All @@ -287,7 +300,17 @@ type ProcessingFunction = ({
file: FileResource;
content: string;
dataSource: DataSourceResource;
upsertArgs?: Record<string, string>;
upsertArgs?:
| Pick<UpsertDocumentArgs, "document_id" | "title" | "tags">
| Pick<
UpsertTableArgs,
| "name"
| "title"
| "description"
| "tableId"
| "tags"
| "useAppForHeaderDetection"
>;
}) => Promise<Result<undefined, Error>>;

const getProcessingFunction = ({
Expand Down Expand Up @@ -403,7 +426,17 @@ export async function processAndUpsertToDataSource(
}: {
file: FileResource;
optionalContent?: string;
upsertArgs?: Record<string, string>;
upsertArgs?:
| Pick<UpsertDocumentArgs, "document_id" | "title" | "tags">
| Pick<
UpsertTableArgs,
| "name"
| "title"
| "description"
| "tableId"
| "tags"
| "useAppForHeaderDetection"
>;
}
): Promise<
Result<
Expand Down
52 changes: 1 addition & 51 deletions front/lib/swr/data_source_view_tables.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { useSendNotification } from "@dust-tt/sparkle";
import type { DataSourceViewType, LightWorkspaceType } from "@dust-tt/types";
import type {
PatchDataSourceTableRequestBody,
PostDataSourceTableRequestBody,
} from "@dust-tt/types";
import type { PatchDataSourceTableRequestBody } from "@dust-tt/types";
import { useMemo } from "react";
import type { Fetcher } from "swr";

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

export function useDataSourceViewTable({
Expand Down Expand Up @@ -133,49 +129,3 @@ export function useUpdateDataSourceViewTable(

return doUpdate;
}

export function useCreateDataSourceTable(
owner: LightWorkspaceType,
dataSourceView: DataSourceViewType
) {
const { mutateRegardlessOfQueryParams: mutateContentNodes } =
useDataSourceViewContentNodes({
owner,
dataSourceView,
disabled: true, // Needed just to mutate
});
const sendNotification = useSendNotification();

const doCreate = async (body: PostDataSourceTableRequestBody) => {
const tableUrl = `/api/w/${owner.sId}/spaces/${dataSourceView.spaceId}/data_sources/${dataSourceView.dataSource.sId}/tables`;
const res = await fetch(tableUrl, {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
},
});
if (!res.ok) {
const errorData = await getErrorFromResponse(res);
sendNotification({
type: "error",
title: "Error creating table",
description: `Error: ${errorData.message}`,
});
return null;
} else {
void mutateContentNodes();

sendNotification({
type: "success",
title: "Table created",
description: "Table has been created",
});

const response: PostTableResponseBody = await res.json();
return response.table;
}
};

return doCreate;
}

This file was deleted.

12 changes: 0 additions & 12 deletions types/src/front/api_handlers/public/data_sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,6 @@ export type PatchDataSourceTableRequestBody = t.TypeOf<
typeof PatchDataSourceTableRequestBodySchema
>;

// Post and Patch require the same request body
export type PostDataSourceTableRequestBody = t.TypeOf<
typeof PatchDataSourceTableRequestBodySchema
>;

export const PostDataSourceTableRequestBodySchema = t.intersection([
PatchDataSourceTableRequestBodySchema,
t.type({
csv: t.string,
}),
]);

export const UpsertTableFromCsvRequestSchema = t.intersection([
t.type({
name: t.string,
Expand Down

0 comments on commit d5654bc

Please sign in to comment.