From 437c17715c22be9be93854f4399c908f0b408b31 Mon Sep 17 00:00:00 2001 From: Jagger <634750802@qq.com> Date: Thu, 8 Aug 2024 15:07:46 +0800 Subject: [PATCH] feat(frontend): support deleting datasource --- frontend/app/src/api/datasources.ts | 13 +++++-- .../app/(main)/(admin)/datasources/page.tsx | 34 +++++++++++++++++-- frontend/app/src/components/llm/LlmInfo.tsx | 8 ++--- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/frontend/app/src/api/datasources.ts b/frontend/app/src/api/datasources.ts index ae6b13ad..f78e778d 100644 --- a/frontend/app/src/api/datasources.ts +++ b/frontend/app/src/api/datasources.ts @@ -1,5 +1,5 @@ import { type IndexProgress, indexSchema, type IndexTotalStats, totalSchema } from '@/api/rag'; -import { BASE_URL, buildUrlParams, handleResponse, opaqueCookieHeader, type Page, type PageParams, zodPage } from '@/lib/request'; +import { BASE_URL, buildUrlParams, handleErrors, handleResponse, opaqueCookieHeader, type Page, type PageParams, zodPage } from '@/lib/request'; import { zodJsonDate } from '@/lib/zod'; import { z, type ZodType } from 'zod'; @@ -11,7 +11,7 @@ interface DatasourceBase { updated_at: Date; user_id: string; build_kg_index: boolean; - llm_id: number | null + llm_id: number | null; } export type Datasource = DatasourceBase & ({ @@ -65,7 +65,7 @@ const baseDatasourceSchema = z.object({ updated_at: zodJsonDate(), user_id: z.string(), build_kg_index: z.boolean(), - llm_id: z.number().nullable() + llm_id: z.number().nullable(), }); const datasourceSchema = baseDatasourceSchema @@ -112,6 +112,13 @@ export async function getDatasource (id: number): Promise { }).then(handleResponse(datasourceSchema)); } +export async function deleteDatasource (id: number): Promise { + await fetch(`${BASE_URL}/api/v1/admin/datasources/${id}`, { + method: 'DELETE', + headers: await opaqueCookieHeader(), + }).then(handleErrors); +} + export async function getDatasourceOverview (id: number): Promise { return fetch(`${BASE_URL}/api/v1/admin/datasources/${id}/overview`, { headers: await opaqueCookieHeader(), diff --git a/frontend/app/src/app/(main)/(admin)/datasources/page.tsx b/frontend/app/src/app/(main)/(admin)/datasources/page.tsx index 7f5cc4a1..e73f7198 100644 --- a/frontend/app/src/app/(main)/(admin)/datasources/page.tsx +++ b/frontend/app/src/app/(main)/(admin)/datasources/page.tsx @@ -1,14 +1,16 @@ 'use client'; -import { type Datasource, listDataSources } from '@/api/datasources'; +import { type Datasource, deleteDatasource, listDataSources } from '@/api/datasources'; import { AdminPageHeading } from '@/components/admin-page-heading'; +import { DangerousActionButton } from '@/components/dangerous-action-button'; import { DataTableHeading } from '@/components/data-table-heading'; import { DataTableRemote } from '@/components/data-table-remote'; import { LlmInfo } from '@/components/llm/LlmInfo'; import { buttonVariants } from '@/components/ui/button'; +import { useDataTable } from '@/components/use-data-table'; import type { ColumnDef } from '@tanstack/react-table'; import { createColumnHelper } from '@tanstack/table-core'; -import { PlusIcon } from 'lucide-react'; +import { PlusIcon, TrashIcon } from 'lucide-react'; import Link from 'next/link'; const helper = createColumnHelper(); @@ -21,9 +23,17 @@ const columns = [ ), }), helper.accessor('data_source_type', {}), - helper.accessor('llm_id', { cell: (ctx) => }), + helper.accessor('llm_id', { cell: (ctx) => }), helper.accessor('build_kg_index', {}), helper.accessor('user_id', {}), + helper.display({ + header: 'Actions', + cell: ({ row }) => ( + + + + ), + }), ] as ColumnDef[]; export default function ChatEnginesPage () { @@ -49,3 +59,21 @@ export default function ChatEnginesPage () { ); } + +function DeleteButton ({ datasource }: { datasource: Datasource }) { + const { reload } = useDataTable(); + + return ( + { + await deleteDatasource(datasource.id); + reload?.(); + }} + variant="ghost" + className="text-xs text-destructive hover:text-destructive hover:bg-destructive/20" + > + + Delete + + ); +} diff --git a/frontend/app/src/components/llm/LlmInfo.tsx b/frontend/app/src/components/llm/LlmInfo.tsx index 5bd95bae..dec00131 100644 --- a/frontend/app/src/components/llm/LlmInfo.tsx +++ b/frontend/app/src/components/llm/LlmInfo.tsx @@ -6,19 +6,19 @@ import { cn } from '@/lib/utils'; import { Loader2Icon } from 'lucide-react'; import Link from 'next/link'; -export function LlmInfo ({ reverse = false, id }: { reverse?: boolean, id: number | undefined | null }) { +export function LlmInfo ({ className, reverse = false, id }: { className?: string, reverse?: boolean, id: number | undefined | null }) { const { llm, isLoading } = useLlm(id); if (isLoading) { - return ; + return ; } if (!llm) { - return Default LLM; + return Default LLM; } return ( - + {llm.provider}:{llm.model} {llm.name}