From 9c67dd6ea92065d377c8f5d9b2e68181b2ba39eb Mon Sep 17 00:00:00 2001 From: Aubin Date: Fri, 17 Jan 2025 19:16:58 +0100 Subject: [PATCH] soft switch to core's content nodes and add the mime-type to icon mapping --- front/lib/api/content_nodes.ts | 85 ++++--------------------------- front/lib/api/data_source_view.ts | 4 +- front/lib/content_nodes.ts | 66 +++++++++++++++++++++++- 3 files changed, 75 insertions(+), 80 deletions(-) diff --git a/front/lib/api/content_nodes.ts b/front/lib/api/content_nodes.ts index 7131fd4fa618..6357100eebc4 100644 --- a/front/lib/api/content_nodes.ts +++ b/front/lib/api/content_nodes.ts @@ -4,7 +4,7 @@ import type { DataSourceViewContentNode, DataSourceViewType, } from "@dust-tt/types"; -import { assertNever, MIME_TYPES } from "@dust-tt/types"; +import { assertNever } from "@dust-tt/types"; import type { DataSourceViewResource } from "@app/lib/resources/data_source_view_resource"; import type logger from "@app/logger/logger"; @@ -115,86 +115,19 @@ export function computeNodesDiff({ } } -export function getContentNodeMetadata( - node: CoreAPIContentNode, - viewType: "tables" | "documents" -): { +export function getContentNodeMetadata(node: CoreAPIContentNode): { type: ContentNodeType; } { - switch (node.mime_type) { - case MIME_TYPES.CONFLUENCE.PAGE: - return { type: "file" }; - case MIME_TYPES.CONFLUENCE.SPACE: - return { type: "folder" }; - case MIME_TYPES.GITHUB.REPOSITORY: - return { type: "folder" }; - case MIME_TYPES.GITHUB.CODE_ROOT: - return { type: "folder" }; - case MIME_TYPES.GITHUB.CODE_DIRECTORY: - return { type: "folder" }; - case MIME_TYPES.GITHUB.CODE_FILE: - return { type: "file" }; - case MIME_TYPES.GITHUB.ISSUES: + // this is approximate and will be cleaned up when we turn ContentNodeType into the same nodeType as in core + // the main point is that it correctly identifies documents as files as this is used in ContentNodeTree + switch (node.node_type) { + case "Table": return { type: "database" }; - case MIME_TYPES.GITHUB.ISSUE: - return { type: "file" }; - case MIME_TYPES.GITHUB.DISCUSSIONS: - return { type: "channel" }; - case MIME_TYPES.GITHUB.DISCUSSION: - return { type: "file" }; - case MIME_TYPES.GOOGLE_DRIVE.FOLDER: + case "Folder": return { type: "folder" }; - case MIME_TYPES.INTERCOM.COLLECTION: - return { type: "folder" }; - case MIME_TYPES.INTERCOM.TEAMS_FOLDER: - return { type: "channel" }; - case MIME_TYPES.INTERCOM.CONVERSATION: - return { type: "file" }; - case MIME_TYPES.INTERCOM.TEAM: - return { type: "folder" }; - case MIME_TYPES.INTERCOM.HELP_CENTER: - return { type: "database" }; - case MIME_TYPES.INTERCOM.ARTICLE: - return { type: "file" }; - case MIME_TYPES.MICROSOFT.FOLDER: - return { type: "folder" }; - case MIME_TYPES.NOTION.UNKNOWN_FOLDER: - return { type: "folder" }; - case MIME_TYPES.NOTION.DATABASE: - return { type: "database" }; - case MIME_TYPES.NOTION.PAGE: - return { type: "file" }; - case MIME_TYPES.SLACK.CHANNEL: - return { type: "channel" }; - case MIME_TYPES.SLACK.THREAD: - return { type: "file" }; - case MIME_TYPES.SLACK.MESSAGES: - return { type: "file" }; - case MIME_TYPES.SNOWFLAKE.DATABASE: - return { type: "folder" }; - case MIME_TYPES.SNOWFLAKE.SCHEMA: - return { type: "folder" }; - case MIME_TYPES.SNOWFLAKE.TABLE: - return { type: "database" }; - case MIME_TYPES.WEBCRAWLER.FOLDER: - return { type: "folder" }; - case MIME_TYPES.ZENDESK.BRAND: - return { type: "folder" }; - case MIME_TYPES.ZENDESK.HELP_CENTER: - return { type: "folder" }; - case MIME_TYPES.ZENDESK.CATEGORY: - return { type: "folder" }; - case MIME_TYPES.ZENDESK.ARTICLE: - return { type: "file" }; - case MIME_TYPES.ZENDESK.TICKETS: - return { type: "folder" }; - case MIME_TYPES.ZENDESK.TICKET: + case "Document": return { type: "file" }; default: - let type: ContentNodeType = "file"; - if (node.mime_type === "text/csv") { - type = viewType === "tables" ? "database" : "file"; - } - return { type }; + assertNever(node.node_type); } } diff --git a/front/lib/api/data_source_view.ts b/front/lib/api/data_source_view.ts index 031405319b57..9736c7a06a3e 100644 --- a/front/lib/api/data_source_view.ts +++ b/front/lib/api/data_source_view.ts @@ -167,7 +167,7 @@ function makeCoreDataSourceViewFilter( async function getContentNodesForDataSourceViewFromCore( dataSourceView: DataSourceViewResource | DataSourceViewType, - { internalIds, parentId, viewType }: GetContentNodesForDataSourceViewParams + { internalIds, parentId }: GetContentNodesForDataSourceViewParams ): Promise> { // There's an early return possible on !dataSourceView.dataSource.connectorId && internalIds?.length === 0, // won't include it for now as we are shadow-reading. @@ -189,7 +189,7 @@ async function getContentNodesForDataSourceViewFromCore( return new Ok({ nodes: coreRes.value.nodes.map((node) => { - const { type } = getContentNodeMetadata(node, viewType); + const { type } = getContentNodeMetadata(node); return { internalId: node.node_id, parentInternalId: node.parent_id ?? null, diff --git a/front/lib/content_nodes.ts b/front/lib/content_nodes.ts index 68452c7b1ef1..511cca161261 100644 --- a/front/lib/content_nodes.ts +++ b/front/lib/content_nodes.ts @@ -7,7 +7,35 @@ import { Square3Stack3DIcon, } from "@dust-tt/sparkle"; import type { ContentNode } from "@dust-tt/types"; -import { assertNever } from "@dust-tt/types"; +import { assertNever, MIME_TYPES } from "@dust-tt/types"; + +// Mime types that should be represented with a Channel icon. +const CHANNEL_MIME_TYPES = [ + MIME_TYPES.GITHUB.DISCUSSIONS, + MIME_TYPES.SLACK.CHANNEL, +] as readonly string[]; + +// Mime types that should be represented with a Database icon. +const DATABASE_MIME_TYPES = [ + MIME_TYPES.GITHUB.ISSUES, + MIME_TYPES.SNOWFLAKE.TABLE, +] as readonly string[]; + +// Mime types that should be represented with a Folder icon. +const FOLDER_MIME_TYPES = [ + MIME_TYPES.CONFLUENCE.SPACE, + MIME_TYPES.GOOGLE_DRIVE.FOLDER, + MIME_TYPES.INTERCOM.TEAM, + MIME_TYPES.MICROSOFT.FOLDER, + MIME_TYPES.NOTION.UNKNOWN_FOLDER, + MIME_TYPES.SNOWFLAKE.DATABASE, + MIME_TYPES.SNOWFLAKE.SCHEMA, + MIME_TYPES.WEBCRAWLER.FOLDER, + MIME_TYPES.ZENDESK.BRAND, + MIME_TYPES.ZENDESK.HELP_CENTER, + MIME_TYPES.ZENDESK.CATEGORY, + MIME_TYPES.ZENDESK.TICKETS, +] as readonly string[]; function getVisualForFileContentNode(node: ContentNode & { type: "file" }) { if (node.expandable) { @@ -17,7 +45,18 @@ function getVisualForFileContentNode(node: ContentNode & { type: "file" }) { return DocumentIcon; } -export function getVisualForContentNode(node: ContentNode) { +export function getVisualForContentNode( + node: ContentNode, + useMimeType = false +) { + if (useMimeType) { + return getVisualForContentNodeBasedOnMimeType(node); + } else { + return getVisualForContentNodeBaseOnType(node); + } +} + +function getVisualForContentNodeBaseOnType(node: ContentNode) { switch (node.type) { case "channel": if (node.providerVisibility === "private") { @@ -40,3 +79,26 @@ export function getVisualForContentNode(node: ContentNode) { assertNever(node.type); } } + +function getVisualForContentNodeBasedOnMimeType(node: ContentNode) { + if (!node.mimeType) { + // putting a default value here to please TS, + // but mimeType should always be defined in a world where we get it from core (currently kept for retro-compatibility with connectors' /content_nodes endpoints) + return getVisualForFileContentNode(node as ContentNode & { type: "file" }); + } + + if (CHANNEL_MIME_TYPES.includes(node.mimeType)) { + if (node.providerVisibility === "private") { + return LockIcon; + } + return ChatBubbleLeftRightIcon; + } + if (DATABASE_MIME_TYPES.includes(node.mimeType)) { + return Square3Stack3DIcon; + } + if (FOLDER_MIME_TYPES.includes(node.mimeType)) { + return FolderIcon; + } + // TODO: we have an issue here in that depending on the view type we sometimes want to render "text/csv" as databases or as files + return getVisualForFileContentNode(node as ContentNode & { type: "file" }); +}