Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: display list of retrived documents when clicking on the chip #1912

Merged
merged 4 commits into from
Oct 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 100 additions & 45 deletions front/components/assistant/conversation/RetrievalAction.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import {
ChevronDownIcon,
ChevronRightIcon,
Chip,
DocumentDuplicateStrokeIcon,
Icon,
Spinner,
Tooltip,
} from "@dust-tt/sparkle";
import { Transition } from "@headlessui/react";
import { useState } from "react";

import { classNames } from "@app/lib/utils";
import {
Expand All @@ -24,65 +29,115 @@ export default function RetrievalAction({
retrievalAction: RetrievalActionType;
}) {
const { query, relativeTimeFrame } = retrievalAction.params;
const [docListVisible, setDocListVisible] = useState(false);

function shortText(text: string, maxLength = 20) {
return text.length > maxLength
? text.substring(0, maxLength) + "..."
: text;
}

return (
<div>
<div className="text-xs font-bold text-element-600">
Searching for:
<div className="ml-2 inline-block">
<Chip.List>
<Tooltip label="Docs created or updated during that time are included in the search">
<Chip
color="amber"
label={
relativeTimeFrame
? "During the last " +
(relativeTimeFrame.duration > 1
? `${relativeTimeFrame.duration} ${relativeTimeFrame.unit}s`
: `${relativeTimeFrame.unit}`)
: "All time"
}
/>
</Tooltip>
<Tooltip label={`Query used for semantic search: ${query}`}>
<Chip
color="slate"
label={query ? shortText(query) : "No query"}
/>
</Tooltip>
</Chip.List>
</div>
<>
<div className="flex flex-row items-center gap-2 pb-2">
<div className="text-xs font-bold text-element-600">Searching for:</div>
<Chip.List>
<Tooltip label="Docs created or updated during that time are included in the search">
<Chip
color="amber"
label={
relativeTimeFrame
? "During the last " +
(relativeTimeFrame.duration > 1
? `${relativeTimeFrame.duration} ${relativeTimeFrame.unit}s`
: `${relativeTimeFrame.unit}`)
: "All time"
}
/>
</Tooltip>
<Tooltip label={`Query used for semantic search: ${query}`}>
<Chip color="slate" label={query ? shortText(query) : "No query"} />
</Tooltip>
</Chip.List>
</div>
{!retrievalAction.documents ? (
<div>
<div className="my-2 text-xs font-bold text-element-600">
Retrieving...
</div>
<Spinner size="sm" />
<div className="grid grid-cols-[auto,1fr] gap-2">
<div className="grid-cols-auto grid items-center">
{!retrievalAction.documents ? (
<div>
<div className="text-xs font-bold text-element-600">
Retrieving...
</div>
<Spinner size="sm" />
</div>
) : (
<div className="text-xs font-bold text-element-600">
<span>Retrieved:</span>
</div>
)}
</div>
) : (
<div className="mt-2 text-xs font-bold text-element-600">
Retrieved:
<Chip color="violet" className="ml-2">
{retrievalAction.documents.length > 0
? RetrievedDocumentsInfo(retrievalAction.documents)
: "No documents found"}
</Chip>
<div className="row-span-1">
{retrievalAction.documents && (
<div onClick={() => setDocListVisible(!docListVisible)}>
<Chip color="violet">
{retrievalAction.documents.length > 0
? RetrievedDocumentsInfo(retrievalAction.documents)
: "No documents found"}
<Icon
visual={docListVisible ? ChevronDownIcon : ChevronRightIcon}
/>
</Chip>
</div>
)}
</div>
)}
</div>
<div className="col-start-2 row-span-1">
{!!retrievalAction.documents?.length && (
<Transition
show={docListVisible}
enter="transition ease-out duration-200 transform"
enterFrom="opacity-0 scale-95"
enterTo="opacity-100 scale-100"
leave="transition ease-in duration-75 transform"
leaveFrom="opacity-100 scale-100"
leaveTo="opacity-0 scale-95"
>
<ul className="ml-2 gap-2">
{retrievalAction.documents.map((document, i) => {
const provider = providerFromDocument(document);
return (
<li key={i}>
<a
href={document.sourceUrl || ""}
className="front-bold text-xs text-element-800"
>
{provider === "none" ? (
<DocumentDuplicateStrokeIcon className="mr-1 inline-block h-4 w-4 text-element-500" />
) : (
<img
src={
PROVIDER_LOGO_PATH[providerFromDocument(document)]
}
className="mr-1 inline-block h-4 w-4"
/>
)}
{titleFromDocument(document)}
</a>
</li>
);
})}
</ul>
</Transition>
)}
</div>
</div>
</>
);
}

function RetrievedDocumentsInfo(documents: RetrievalDocumentType[]) {
const summary = documentsSummary(documents);
return (
<>
<span>{documents.length} results</span>
<div className="flex flex-row items-center">
<span className="hidden lg:block">{documents.length} results</span>
{Object.keys(summary).map((k) => {
return (
<div key={k} className="ml-3 flex flex-initial flex-row items-center">
Expand All @@ -97,7 +152,7 @@ function RetrievedDocumentsInfo(documents: RetrievalDocumentType[]) {
</div>
);
})}
</>
</div>
);
}

Expand Down