Skip to content

Commit

Permalink
Fix: don't show deleted in progress item (#393)
Browse files Browse the repository at this point in the history
Signed-off-by: Daishan Peng <[email protected]>
  • Loading branch information
StrongMonkey authored Oct 31, 2024
1 parent e85650d commit af06aef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 66 deletions.
29 changes: 15 additions & 14 deletions ui/admin/app/components/knowledge/AgentKnowledgePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default function AgentKnowledgePanel({ agentId }: { agentId: string }) {
...item,
}) as KnowledgeFile
)
.filter((item) => !item.deleted)
),
{
revalidateOnFocus: false,
Expand Down Expand Up @@ -84,10 +85,10 @@ export default function AgentKnowledgePanel({ agentId }: { agentId: string }) {
);

let notionSource = knowledgeSources.find((source) => source.notionConfig);
let onedriveSource = knowledgeSources.find(
const onedriveSource = knowledgeSources.find(
(source) => source.onedriveConfig
);
let websiteSource = knowledgeSources.find(
const websiteSource = knowledgeSources.find(
(source) => source.websiteCrawlingConfig
);

Expand Down Expand Up @@ -258,7 +259,7 @@ export default function AgentKnowledgePanel({ agentId }: { agentId: string }) {
} else {
setBlockPollingNotionFiles(false);
}
}, [notionFiles]);
}, [notionFiles, notionSource?.state]);

useEffect(() => {
if (
Expand All @@ -280,7 +281,7 @@ export default function AgentKnowledgePanel({ agentId }: { agentId: string }) {
} else {
setBlockPollingOneDriveFiles(false);
}
}, [onedriveFiles]);
}, [onedriveFiles, onedriveSource?.state]);

useEffect(() => {
if (
Expand All @@ -302,10 +303,9 @@ export default function AgentKnowledgePanel({ agentId }: { agentId: string }) {
} else {
setBlockPollingWebsiteFiles(false);
}
}, [websiteFiles]);
}, [websiteFiles, websiteSource?.state]);

useEffect(() => {
notionSource = knowledgeSources.find((source) => source.notionConfig);
if (
!notionSource ||
notionSource?.state === KnowledgeSourceStatus.Synced
Expand All @@ -316,9 +316,6 @@ export default function AgentKnowledgePanel({ agentId }: { agentId: string }) {
setBlockPollingNotion(false);
}

onedriveSource = knowledgeSources.find(
(source) => source.onedriveConfig
);
if (
!onedriveSource ||
onedriveSource?.state === KnowledgeSourceStatus.Synced
Expand All @@ -329,9 +326,6 @@ export default function AgentKnowledgePanel({ agentId }: { agentId: string }) {
setBlockPollingOneDrive(false);
}

websiteSource = knowledgeSources.find(
(source) => source.websiteCrawlingConfig
);
if (
!websiteSource ||
websiteSource?.state === KnowledgeSourceStatus.Synced
Expand All @@ -341,7 +335,15 @@ export default function AgentKnowledgePanel({ agentId }: { agentId: string }) {
} else {
setBlockPollingWebsite(false);
}
}, [getKnowledgeSources]);
}, [
getKnowledgeSources,
notionSource,
onedriveSource,
websiteSource,
getNotionFiles,
getOnedriveFiles,
getWebsiteFiles,
]);

return (
<div className="flex flex-col gap-4 justify-center items-center">
Expand Down Expand Up @@ -456,7 +458,6 @@ export default function AgentKnowledgePanel({ agentId }: { agentId: string }) {
onOpenChange={setIsAddFileModalOpen}
startPolling={startPollingLocalFiles}
files={localFiles}
getLocalFiles={getLocalFiles}
/>
<NotionModal
agentId={agentId}
Expand Down
55 changes: 3 additions & 52 deletions ui/admin/app/components/knowledge/file/FileModal.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { UploadIcon } from "lucide-react";
import { useCallback, useRef } from "react";
import { SWRResponse } from "swr";

import { KnowledgeFile, KnowledgeFileState } from "~/lib/model/knowledge";
import { KnowledgeFile } from "~/lib/model/knowledge";
import { KnowledgeService } from "~/lib/service/api/knowledgeService";
import { cn } from "~/lib/utils";

Expand Down Expand Up @@ -30,7 +29,6 @@ import IngestionStatusComponent from "../IngestionStatus";

interface FileModalProps {
agentId: string;
getLocalFiles: SWRResponse<KnowledgeFile[], Error>;
isOpen: boolean;
onOpenChange: (open: boolean) => void;
startPolling: () => void;
Expand All @@ -39,7 +37,6 @@ interface FileModalProps {

function FileModal({
agentId,
getLocalFiles,
startPolling,
files,
isOpen,
Expand All @@ -51,51 +48,9 @@ function FileModal({
async (_index: number, file: File) => {
await new Promise((resolve) => setTimeout(resolve, 1000));
await KnowledgeService.addKnowledgeFilesToAgent(agentId, file);

// once added, we can immediately mutate the cache value
// without revalidating.
// Revalidating here would cause knowledge to be refreshed
// for each file being uploaded, which is not desirable.
const newItem: KnowledgeFile = {
id: "",
fileName: file.name,
agentID: agentId,
// set ingestion status to starting to ensure polling is enabled
approved: true,
created: new Date().toISOString(),
state: KnowledgeFileState.PendingApproval,
knowledgeSetID: "",
knowledgeSourceID: "",
url: "",
updatedAt: "",
checksum: "",
lastRunID: "",
error: "",
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
getLocalFiles.mutate(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(prev: any) => {
const existingItemIndex = prev?.findIndex(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(item: any) => item.fileName === newItem.fileName
);
if (existingItemIndex !== -1 && prev) {
const updatedPrev = [...prev];
updatedPrev[existingItemIndex!] = newItem;
return updatedPrev;
} else {
return [newItem, ...(prev || [])];
}
},
{
revalidate: false,
}
);
startPolling();
},
[agentId, getLocalFiles, startPolling]
[agentId, startPolling]
);

// use multi async to handle uploading multiple files at once
Expand All @@ -116,11 +71,7 @@ function FileModal({
agentId,
item.fileName
);

// optomistic update without cache revalidation
getLocalFiles.mutate((prev: KnowledgeFile[] | undefined) =>
prev?.filter((prevItem) => prevItem.fileName !== item.fileName)
);
startPolling();
});

return (
Expand Down
1 change: 1 addition & 0 deletions ui/admin/app/lib/model/knowledge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type FolderSet = {
export type KnowledgeFile = {
id: string;
created: string;
deleted: string;
fileName: string;
state: KnowledgeFileState;
agentID: string;
Expand Down

0 comments on commit af06aef

Please sign in to comment.