Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
Signed-off-by: Daishan Peng <[email protected]>
  • Loading branch information
StrongMonkey committed Oct 21, 2024
1 parent d99027c commit 2741f7b
Show file tree
Hide file tree
Showing 17 changed files with 210 additions and 152 deletions.
8 changes: 5 additions & 3 deletions pkg/api/handlers/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,11 @@ func (a *AgentHandler) ApproveKnowledgeFile(req api.Context) error {
return err
}

file.Spec.Approved = &[]bool{body.Approve}[0]

return req.Storage.Update(req.Context(), &file)
if file.Spec.Approved == nil || *file.Spec.Approved != body.Approve {
file.Spec.Approved = &body.Approve
return req.Storage.Update(req.Context(), &file)
}
return nil
}

func (a *AgentHandler) DeleteKnowledge(req api.Context) error {
Expand Down
11 changes: 1 addition & 10 deletions pkg/controller/handlers/knowledge/knowledge.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,6 @@ func compileFileStatuses(ctx context.Context, client kclient.Client, ws *v1.Work
return final, errors.Join(errs...)
}

func (a *Handler) BindWorkspace(req router.Request, resp router.Response) error {
kFile := req.Object.(*v1.KnowledgeFile)
// force to associate with workspace so that it can reenqueue workspace when file are changed
if err := req.Get(&v1.Workspace{}, kFile.Namespace, kFile.Spec.WorkspaceName); err != nil {
return err
}
return nil
}

func (a *Handler) CleanupFile(req router.Request, resp router.Response) error {
kFile := req.Object.(*v1.KnowledgeFile)

Expand All @@ -327,7 +318,7 @@ func (a *Handler) CleanupFile(req router.Request, resp router.Response) error {
return err
}

if _, err := a.ingester.DeleteKnowledgeFiles(req.Ctx, kFile.Namespace, filepath.Join(workspace.GetDir(ws.Status.WorkspaceID), kFile.Spec.FileName)); err != nil {
if _, err := a.ingester.DeleteKnowledgeFiles(req.Ctx, kFile.Namespace, filepath.Join(workspace.GetDir(ws.Status.WorkspaceID), kFile.Spec.FileName), ws.Spec.KnowledgeSetName); err != nil {
return err
}

Expand Down
1 change: 0 additions & 1 deletion pkg/controller/handlers/uploads/remoteknowledgesource.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,6 @@ func compileKnowledgeFiles(ctx context.Context, c client.Client,
} else if err != nil {
errs = append(errs, err)
}

}

if len(errs) > 0 {
Expand Down
1 change: 0 additions & 1 deletion pkg/controller/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func (c *Controller) setupRoutes() error {

// Knowledge files
root.Type(&v1.KnowledgeFile{}).HandlerFunc(cleanup.Cleanup)
root.Type(&v1.KnowledgeFile{}).HandlerFunc(knowledge.BindWorkspace)
root.Type(&v1.KnowledgeFile{}).FinalizeFunc(v1.KnowledgeFileFinalizer, knowledge.CleanupFile)

// Workspaces
Expand Down
4 changes: 3 additions & 1 deletion pkg/knowledge/knowledge.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ func (i *Ingester) IngestKnowledge(ctx context.Context, namespace, knowledgeSetN
)
}

func (i *Ingester) DeleteKnowledgeFiles(ctx context.Context, namespace, knowledgeFilePath string) (*invoke.Response, error) {
func (i *Ingester) DeleteKnowledgeFiles(ctx context.Context, namespace, knowledgeFilePath string, knowledgeSetName string) (*invoke.Response, error) {
return i.invoker.SystemAction(
ctx,
"ingest-delete-file-",
namespace,
system.KnowledgeDeleteFileTool,
knowledgeFilePath,
"GPTSCRIPT_DATASET="+knowledgeSetName,
"KNOW_JSON=true",
)

}
Expand Down
152 changes: 104 additions & 48 deletions ui/admin/app/components/knowledge/AgentKnowledgePanel.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Globe, SettingsIcon, UploadIcon } from "lucide-react";
import { useEffect, useMemo, useState } from "react";
import useSWR from "swr";
import useSWR, { SWRResponse } from "swr";

import {
IngestionStatus,
KnowledgeFile,
RemoteKnowledgeSourceType,
getIngestedFilesCount,
getIngestionStatus,
} from "~/lib/model/knowledge";
import { ApiRoutes } from "~/lib/routers/apiRoutes";
import { KnowledgeService } from "~/lib/service/api/knowledgeService";
import { assetUrl } from "~/lib/utils";

import { Button } from "~/components/ui/button";

Expand All @@ -26,7 +28,7 @@ export function AgentKnowledgePanel({ agentId }: { agentId: string }) {
const [isNotionModalOpen, setIsNotionModalOpen] = useState(false);
const [isWebsiteModalOpen, setIsWebsiteModalOpen] = useState(false);

const getKnowledgeFiles = useSWR(
const getKnowledgeFiles: SWRResponse<KnowledgeFile[], Error> = useSWR(
KnowledgeService.getKnowledgeForAgent.key(agentId),
({ agentId }) =>
KnowledgeService.getKnowledgeForAgent(agentId).then((items) =>
Expand Down Expand Up @@ -221,72 +223,132 @@ export function AgentKnowledgePanel({ agentId }: { agentId: string }) {
}
};

const notionFiles = knowledge.filter(
(item) => item.remoteKnowledgeSourceType === "notion"
);
const onedriveFiles = knowledge.filter(
(item) => item.remoteKnowledgeSourceType === "onedrive"
);
const websiteFiles = knowledge.filter(
(item) => item.remoteKnowledgeSourceType === "website"
);
const localFiles = knowledge.filter(
(item) => !item.remoteKnowledgeSourceType
);

return (
<div className="flex flex-col gap-4 justify-center items-center">
<div className="flex w-full justify-between items-center px-4">
<div className="flex w-full items-center justify-between gap-3 rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus-visible:ring-transparent">
<div className="flex items-center gap-2">
<UploadIcon className="h-5 w-5" />
<span className="text-lg font-semibold">Files</span>
</div>
<Button
onClick={() => setIsAddFileModalOpen(true)}
className="flex items-center gap-2"
variant="secondary"
>
<SettingsIcon className="h-5 w-5" />
</Button>
<div className="flex flex-row items-center gap-2">
<div className="flex items-center gap-2">
{getIngestedFilesCount(localFiles) > 0 && (
<span className="text-sm font-medium text-gray-500">
{getIngestedFilesCount(localFiles)}{" "}
{getIngestedFilesCount(localFiles) === 1
? "file"
: "files"}{" "}
ingested
</span>
)}
</div>
<Button
onClick={() => setIsAddFileModalOpen(true)}
className="flex items-center gap-2"
variant="ghost"
>
<SettingsIcon className="h-5 w-5" />
</Button>
</div>
</div>
<div className="flex w-full justify-between items-center px-4">
<div className="flex w-full items-center justify-between gap-3 rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus-visible:ring-transparent">
<div className="flex items-center gap-2">
<Avatar className="h-5 w-5">
<img src="/notion.svg" alt="Notion logo" />
<img src={assetUrl("/notion.svg")} alt="Notion logo" />
</Avatar>
<span className="text-lg font-semibold">Notion</span>
</div>
<Button
onClick={() => onClickNotion()}
className="flex items-center gap-2"
variant="secondary"
>
<SettingsIcon className="h-5 w-5" />
</Button>
<div className="flex flex-row items-center gap-2">
{getIngestedFilesCount(notionFiles) > 0 && (
<span className="text-sm font-medium text-gray-500">
{getIngestedFilesCount(notionFiles)}{" "}
{getIngestedFilesCount(notionFiles) === 1
? "file"
: "files"}{" "}
ingested
</span>
)}
<Button
onClick={() => onClickNotion()}
className="flex items-center gap-2"
variant="ghost"
>
<SettingsIcon className="h-5 w-5" />
</Button>
</div>
</div>
<div className="flex w-full justify-between items-center px-4">
<div className="flex w-full items-center justify-between gap-3 rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus-visible:ring-transparent">
<div className="flex items-center gap-2">
<Avatar className="h-5 w-5">
<img src="/onedrive.svg" alt="OneDrive logo" />
<img
src={assetUrl("/onedrive.svg")}
alt="OneDrive logo"
/>
</Avatar>
<span className="text-lg font-semibold">OneDrive</span>
</div>
<Button
onClick={() => onClickOnedrive()}
className="flex items-center gap-2"
variant="secondary"
>
<SettingsIcon className="h-5 w-5" />
</Button>
<div className="flex flex-row items-center gap-2">
{getIngestedFilesCount(onedriveFiles) > 0 && (
<span className="text-sm font-medium text-gray-500">
{getIngestedFilesCount(onedriveFiles)}{" "}
{getIngestedFilesCount(onedriveFiles) === 1
? "file"
: "files"}{" "}
ingested
</span>
)}
<Button
onClick={() => onClickOnedrive()}
className="flex items-center gap-2"
variant="ghost"
>
<SettingsIcon className="h-5 w-5" />
</Button>
</div>
</div>
<div className="flex w-full justify-between items-center px-4">
<div className="flex w-full items-center justify-between gap-3 rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus-visible:ring-transparent">
<div className="flex items-center gap-2">
<Globe className="h-5 w-5" />
<span className="text-lg font-semibold">Website</span>
</div>
<Button
onClick={() => onClickWebsite()}
className="flex items-center gap-2"
variant="secondary"
>
<SettingsIcon className="h-5 w-5" />
</Button>
<div className="flex flex-row items-center gap-2">
{getIngestedFilesCount(websiteFiles) > 0 && (
<span className="text-sm font-medium text-gray-500">
{getIngestedFilesCount(websiteFiles)}{" "}
{getIngestedFilesCount(websiteFiles) === 1
? "file"
: "files"}{" "}
ingested
</span>
)}
<Button
onClick={() => onClickWebsite()}
className="flex items-center gap-2"
variant="ghost"
>
<SettingsIcon className="h-5 w-5" />
</Button>
</div>
</div>
<FileModal
agentId={agentId}
isOpen={isAddFileModalOpen}
onOpenChange={setIsAddFileModalOpen}
startPolling={startPolling}
knowledge={knowledge.filter(
(item) => !item.remoteKnowledgeSourceType
)}
knowledge={localFiles}
getKnowledgeFiles={getKnowledgeFiles}
/>
<NotionModal
Expand All @@ -295,9 +357,7 @@ export function AgentKnowledgePanel({ agentId }: { agentId: string }) {
onOpenChange={setIsNotionModalOpen}
remoteKnowledgeSources={remoteKnowledgeSources}
startPolling={startPolling}
knowledgeFiles={knowledge.filter(
(item) => item.remoteKnowledgeSourceType === "notion"
)}
knowledgeFiles={notionFiles}
handleRemoteKnowledgeSourceSync={
handleRemoteKnowledgeSourceSync
}
Expand All @@ -308,9 +368,7 @@ export function AgentKnowledgePanel({ agentId }: { agentId: string }) {
onOpenChange={setIsOnedriveModalOpen}
remoteKnowledgeSources={remoteKnowledgeSources}
startPolling={startPolling}
knowledgeFiles={knowledge.filter(
(item) => item.remoteKnowledgeSourceType === "onedrive"
)}
knowledgeFiles={onedriveFiles}
handleRemoteKnowledgeSourceSync={
handleRemoteKnowledgeSourceSync
}
Expand All @@ -321,9 +379,7 @@ export function AgentKnowledgePanel({ agentId }: { agentId: string }) {
onOpenChange={setIsWebsiteModalOpen}
remoteKnowledgeSources={remoteKnowledgeSources}
startPolling={startPolling}
knowledgeFiles={knowledge.filter(
(item) => item.remoteKnowledgeSourceType === "website"
)}
knowledgeFiles={websiteFiles}
handleRemoteKnowledgeSourceSync={
handleRemoteKnowledgeSourceSync
}
Expand Down
8 changes: 4 additions & 4 deletions ui/admin/app/components/knowledge/FileItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ function FileItem({
</div>

{isApproved ? (
// eslint-disable-next-line
<div
className="hover:cursor-pointer"
<Button
variant="ghost"
size="icon"
onClick={() => {
setIsApproved(false);
approveFile(file, false);
}}
>
<FileStatusIcon status={file.ingestionStatus} />
</div>
</Button>
) : (
<Button
variant="ghost"
Expand Down
2 changes: 1 addition & 1 deletion ui/admin/app/components/knowledge/FileStatusIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const FileStatusIcon: React.FC<FileStatusIconProps> = ({ status }) => {
const [Icon, className] = ingestionIcons[status.status];

return (
<div className={cn("flex items-center mr-2", className)}>
<div className={cn("flex items-center", className)}>
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
Expand Down
Loading

0 comments on commit 2741f7b

Please sign in to comment.