Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add indexing status

Marc Seitz7b3f8d68e304
diff --git a/app/(ee)/api/ai/store/runs/[runId]/route.ts b/app/(ee)/api/ai/store/runs/[runId]/route.ts
new file mode 100644
index 00000000..cd93d35f
--- /dev/null
+++ b/app/(ee)/api/ai/store/runs/[runId]/route.ts
@@ -0,0 +1,41 @@
+import { runs } from "@trigger.dev/sdk/v3";
+import { NextRequest, NextResponse } from "next/server";
+
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
+import { getServerSession } from "next-auth";
+
+/**
+ * GET /api/ai/store/runs/[runId]
+ * Get the status of a Trigger.dev run for polling
+ */
+export async function GET(
+ req: NextRequest,
+ { params }: { params: { runId: string } },
+) {
+ try {
+ const session = await getServerSession(authOptions);
+
+ if (!session) {
+ return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
+ }
+
+ const { runId } = params;
+
+ const run = await runs.retrieve(runId);
+
+ return NextResponse.json({
+ id: run.id,
+ status: run.status,
+ metadata: run.metadata,
+ isCompleted: run.isCompleted,
+ isFailed: run.isFailed,
+ output: run.output,
+ });
+ } catch (error) {
+ console.error("Error retrieving run status:", error);
+ return NextResponse.json(
+ { error: "Failed to retrieve run status" },
+ { status: 500 },
+ );
+ }
+}
diff --git a/components/documents/document-header.tsx b/components/documents/document-header.tsx
index 8112fb3d..9d1fd957 100644
--- a/components/documents/document-header.tsx
+++ b/components/documents/document-header.tsx
@@ -10,7 +10,6 @@ import { Document, DocumentVersion } from "@prisma/client";
import {
ArrowRightIcon,
BetweenHorizontalStartIcon,
- Bot,
ChevronRight,
CloudDownloadIcon,
DownloadIcon,
@@ -41,6 +40,7 @@ import { fileIcon } from "@/lib/utils/get-file-icon";
import FileUp from "@/components/shared/icons/file-up";
import MoreVertical from "@/components/shared/icons/more-vertical";
+import PapermarkSparkle from "@/components/shared/icons/papermark-sparkle";
import { Button } from "@/components/ui/button";
import {
Collapsible,
@@ -197,56 +197,63 @@ export default function DocumentHeader({
}
};
- const activateOrRedirectAssistant = async (document: Document) => {
- if (document.assistantEnabled) {
- router.push(`/documents/${document.id}/chat`);
- } else {
- toast.promise(
- fetch("/api/assistants", {
- method: "POST",
+ const [enablingAI, setEnablingAI] = useState<boolean>(false);
+
+ // Enable AI agents and automatically index the document
+ const enableAIAgents = async () => {
+ if (!canUseAI) {
+ toast.error(
+ "AI agents are not available. Please enable them in team settings first.",
+ );
+ return;
+ }
+
+ setEnablingAI(true);
+
+ try {
+ // Step 1: Enable AI agents on the document
+ const enableResponse = await fetch(
+ `/api/teams/${teamId}/documents/${prismaDocument.id}`,
+ {
+ method: "PATCH",
headers: {
"Content-Type": "application/json",
},
- body: JSON.stringify({
- documentId: document.id,
- }),
- }).then(() => {
- // refetch to fix the UI delay
- mutate(`/api/teams/${teamId}/documents/${document.id}`);
+ body: JSON.stringify({ agentsEnabled: true }),
+ },
+ );
- router.push(`/documents/${document.id}/chat`);
- }),
+ if (!enableResponse.ok) {
+ throw new Error("Failed to enable AI agents");
+ }
+
+ // Step 2: Index the document automatically
+ const indexResponse = await fetch(
+ `/api/ai/store/teams/${teamId}/documents/${prismaDocument.id}`,
{
- loading: "Activating Assistant...",
- success: "Papermark Assistant successfully activated.",
- error: "Activation failed. Please try again.",
+ method: "POST",
},
);
- }
- };
- const activateOrDeactivateAssistant = async (
- active: boolean,
- prismaDocumentId: string,
- ) => {
- const fetchPromise = fetch("/api/assistants", {
- method: active ? "POST" : "DELETE",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- documentId: prismaDocumentId,
- }),
- }).then(() => {
- // refetch to fix the UI delay
- mutate(`/api/teams/${teamId}/documents/${prismaDocumentId}`);
- });
-
- toast.promise(fetchPromise, {
- loading: `${active ? "Activating" : "Deactivating"} Assistant...`,
- success: `Papermark Assistant successfully ${active ? "activated" : "deactivated"}.`,
- error: `${active ? "Activation" : "Deactivation"} failed. Please try again.`,
- });
+ if (!indexResponse.ok) {
+ const error = await indexResponse.json();
+ // If indexing fails, still keep AI enabled but show warning
+ toast.warning(
+ error.error ||
+ "AI enabled, but document indexing failed. You can re-index from settings.",
+ );
+ } else {
+ toast.success("AI agents enabled and document indexed successfully");
+ }
+
+ // Refresh document data
+ mutate(`/api/teams/${teamId}/documents/${prismaDocument.id}`);
+ } catch (error) {
+ console.error("Error enabling AI agents:", error);
+ toast.error("Failed to enable AI agents. Please try again.");
+ } finally {
+ setEnablingAI(false);
+ }
};
const changeDocumentOrientation = async () => {
@@ -496,6 +503,8 @@ export default function DocumentHeader({
);
};
+ console.log("prismaDocument", prismaDocument);
+
return (
<header className="flex flex-col gap-y-4">
<div className="flex items-center justify-between gap-x-8">
@@ -595,23 +604,40 @@ export default function DocumentHeader({
</AddDocumentModal>
)}
- {/* TODO: Assistant feature temporarily disabled. Will be re-enabled in a future update */}
- {/* {prismaDocument.type !== "notion" &&
- prismaDocument.type !== "sheet" &&
+ {/* AI Agents Button */}
+ {isAIEnabled &&
+ prismaDocument.type !== "notion" &&
+ primaryVersion.type !== "link" &&
prismaDocument.type !== "zip" &&
- prismaDocument.type !== "video" &&
- prismaDocument.assistantEnabled && (
- <Button
- className="group hidden h-8 space-x-1 whitespace-nowrap bg-gradient-to-r from-[#16222A] via-emerald-500 to-[#16222A] text-xs duration-200 ease-linear hover:bg-right md:flex lg:h-9 lg:text-sm"
- variant={"special"}
- size={"icon"}
- style={{ backgroundSize: "200% auto" }}
- onClick={() => activateOrRedirectAssistant(prismaDocument)}
- title="Open AI Assistant"
- >
- <PapermarkSparkle className="h-5 w-5" />
- </Button>
- )} */}
+ primaryVersion.type !== "video" &&
+ (prismaDocument.agentsEnabled ? (
+ <ButtonTooltip content="AI Agents Settings">
+ <Button
+ variant="outline"
+ size="icon"
+ className="hidden size-8 md:flex lg:size-9"
+ onClick={() => setAiDialogOpen(true)}
+ >
+ <PapermarkSparkle className="h-5 w-5 text-emerald-500" />
+ </Button>
+ </ButtonTooltip>
+ ) : (
+ <ButtonTooltip content="Enable AI Agents">
+ <Button
+ variant="outline"
+ size="icon"
+ className="hidden size-8 md:flex lg:size-9"
+ onClick={enableAIAgents}
+ disabled={enablingAI}
+ >
+ {enablingAI ? (
+ <LoadingSpinner className="h-5 w-5" />
+ ) : (
+ <PapermarkSparkle className="h-5 w-5" />
+ )}
+ </Button>
+ </ButtonTooltip>
+ ))}
<div className="flex items-center gap-x-1">
{actionRows.map((row, i) => (
@@ -664,60 +690,8 @@ export default function DocumentHeader({
</DropdownMenuItem>
)}
- {/* TODO: Assistant feature temporarily disabled. Will be re-enabled in a future update */}
- {/* {prismaDocument.type !== "notion" &&
- prismaDocument.type !== "sheet" &&
- prismaDocument.type !== "zip" &&
- primaryVersion.type !== "video" && (
- <>
- <DropdownMenuItem
- onClick={() => changeDocumentOrientation()}
- >
- <PortraitLandscape
- className={cn(
- "mr-2 h-4 w-4",
- !primaryVersion.isVertical &&
- "-rotate-90 transform",
- )}
- />
- Change orientation
- </DropdownMenuItem>
-
- <DropdownMenuItem
- onClick={() =>
- activateOrRedirectAssistant(prismaDocument)
- }
- >
- <PapermarkSparkle className="mr-2 h-4 w-4" />
- Open AI Assistant
- </DropdownMenuItem>
- </>
- )} */}
-
<DropdownMenuSeparator />
</DropdownMenuGroup>
- {/* TODO: Assistant feature temporarily disabled. Will be re-enabled in a future update */}
- {/* {primaryVersion.type !== "notion" &&
- primaryVersion.type !== "sheet" &&
- primaryVersion.type !== "zip" &&
- primaryVersion.type !== "video" &&
- (!prismaDocument.assistantEnabled ? (
- <DropdownMenuItem
- onClick={() =>
- activateOrDeactivateAssistant(true, prismaDocument.id)
- }
- >
- <Sparkles className="mr-2 h-4 w-4" /> Activate Assistant
- </DropdownMenuItem>
- ) : (
- <DropdownMenuItem
- onClick={() =>
- activateOrDeactivateAssistant(false, prismaDocument.id)
- }
- >
- <Sparkles className="mr-2 h-4 w-4" /> Disable Assistant
- </DropdownMenuItem>
- ))} */}
{prismaDocument.type === "sheet" &&
supportsAdvancedExcelMode(primaryVersion.contentType) &&
(isPro || isBusiness || isDatarooms || isTrial) && (
@@ -746,21 +720,30 @@ export default function DocumentHeader({
{isAIEnabled &&
prismaDocument.type !== "notion" &&
primaryVersion.type !== "link" &&
- prismaDocument.type !== "sheet" &&
prismaDocument.type !== "zip" &&
- primaryVersion.type !== "video" && (
+ primaryVersion.type !== "video" &&
+ (prismaDocument.agentsEnabled ? (
<DropdownMenuItem
onClick={() => {
setAiDialogOpen(true);
setMenuOpen(false);
}}
>
- <Bot className="mr-2 h-4 w-4" />
- {prismaDocument.agentsEnabled
- ? "AI Agents Settings"
- : "Enable AI Agents"}
+ <PapermarkSparkle className="mr-2 h-4 w-4 text-emerald-500" />
+ AI Agents Settings
</DropdownMenuItem>
- )}
+ ) : (
+ <DropdownMenuItem
+ onClick={() => {
+ enableAIAgents();
+ setMenuOpen(false);
+ }}
+ disabled={enablingAI}
+ >
+ <PapermarkSparkle className="mr-2 h-4 w-4" />
+ {enablingAI ? "Enabling AI..." : "Enable AI Agents"}
+ </DropdownMenuItem>
+ ))}
{primaryVersion.type !== "notion" &&
primaryVersion.type !== "link" &&
diff --git a/ee/features/ai/components/agents-settings-card.tsx b/ee/features/ai/components/agents-settings-card.tsx
index 323e851a..48d4de2e 100644
--- a/ee/features/ai/components/agents-settings-card.tsx
+++ b/ee/features/ai/components/agents-settings-card.tsx
@@ -1,8 +1,10 @@
"use client";
-import { useState } from "react";
+import { useEffect, useState } from "react";
-import { Bot, Loader2 } from "lucide-react";
+import { CheckCircle, Loader2, XCircle } from "lucide-react";
+
+import PapermarkSparkle from "@/components/shared/icons/papermark-sparkle";
import { toast } from "sonner";
import { mutate } from "swr";
@@ -20,20 +22,27 @@ import {
} from "@/components/ui/card";
import { Label } from "@/components/ui/label";
import LoadingSpinner from "@/components/ui/loading-spinner";
+import { Progress } from "@/components/ui/progress";
import { Switch } from "@/components/ui/switch";
+import { useAIIndexingStatus } from "../hooks/use-ai-indexing-status";
+
interface AgentsSettingsCardProps {
- type: "document" | "dataroom" | "team";
- entityId: string;
+ dataroomId: string;
teamId: string;
agentsEnabled: boolean;
vectorStoreId?: string | null;
onUpdate?: () => void;
}
+interface IndexingRun {
+ documentId: string;
+ documentName: string;
+ runId: string;
+}
+
export function AgentsSettingsCard({
- type,
- entityId,
+ dataroomId,
teamId,
agentsEnabled: initialEnabled,
vectorStoreId,
@@ -43,6 +52,9 @@ export function AgentsSettingsCard({
const [loading, setLoading] = useState(false);
const [indexing, setIndexing] = useState(false);
+ // Run tracking state for polling
+ const [indexingRuns, setIndexingRuns] = useState<IndexingRun[]>([]);
+
// Check if AI feature is enabled for this team
const { isFeatureEnabled, isLoading: featuresLoading } = useFeatureFlags();
const isAIFeatureEnabled = isFeatureEnabled("ai");
@@ -67,20 +79,16 @@ export function AgentsSettingsCard({
setLoading(true);
try {
- const endpoint =
- type === "document"
- ? `/api/teams/${teamId}/documents/${entityId}`
- : type === "dataroom"
- ? `/api/teams/${teamId}/datarooms/${entityId}`
- : `/api/teams/${teamId}`;
-
- const response = await fetch(endpoint, {
- method: "PATCH",
- headers: {
- "Content-Type": "application/json",
+ const response = await fetch(
+ `/api/teams/${teamId}/datarooms/${dataroomId}`,
+ {
+ method: "PATCH",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({ agentsEnabled: enabled }),
},
- body: JSON.stringify({ agentsEnabled: enabled }),
- });
+ );
if (!response.ok) {
throw new Error("Failed to update agents setting");
@@ -90,11 +98,7 @@ export function AgentsSettingsCard({
toast.success(`AI Agents ${enabled ? "enabled" : "disabled"}`);
// Mutate the relevant SWR cache
- if (type === "document") {
- await mutate(`/api/teams/${teamId}/documents/${entityId}`);
- } else if (type === "dataroom") {
- await mutate(`/api/teams/${teamId}/datarooms/${entityId}`);
- }
+ await mutate(`/api/teams/${teamId}/datarooms/${dataroomId}`);
onUpdate?.();
} catch (error) {
@@ -106,53 +110,81 @@ export function AgentsSettingsCard({
}
};
- const handleIndexDocuments = async () => {
+ const handleIndexDataroom = async () => {
setIndexing(true);
+ setIndexingRuns([]);
try {
- const endpoint =
- type === "document"
- ? `/api/ai/store/teams/${teamId}/documents/${entityId}`
- : `/api/ai/store/teams/${teamId}/datarooms/${entityId}`;
-
- const response = await fetch(endpoint, {
- method: "POST",
- });
+ const response = await fetch(
+ `/api/ai/store/teams/${teamId}/datarooms/${dataroomId}`,
+ {
+ method: "POST",
+ },
+ );
if (!response.ok) {
const error = await response.json();
- throw new Error(error.error || "Failed to index documents");
+ throw new Error(error.error || "Failed to index dataroom");
}
const data = await response.json();
- if (data.errors && data.errors.length > 0) {
- toast.warning(
- `Indexed ${data.filesIndexed}/${data.totalDocuments || "N/A"} documents with some errors`,
- );
- } else {
+ if (data.error) {
+ throw new Error(data.error);
+ }
+
+ // Dataroom batch indexing
+ if (data.runs && data.runs.length > 0) {
+ // Track runs for documents that need indexing
+ setIndexingRuns(data.runs);
+
+ // Show info about skipped documents
+ if (data.skippedCount > 0) {
+ toast.info(
+ `${data.skippedCount} document${data.skippedCount > 1 ? "s" : ""} already indexed, indexing ${data.triggeredCount} new document${data.triggeredCount > 1 ? "s" : ""}`,
+ );
+ }
+ } else if (data.skippedCount > 0 && data.triggeredCount === 0) {
+ // All documents were already indexed
toast.success(
- `Successfully indexed ${type === "document" ? "document" : `${data.filesIndexed} documents`}`,
+ `All ${data.skippedCount} document${data.skippedCount > 1 ? "s are" : " is"} already indexed`,
);
+ setIndexing(false);
+ } else if (data.totalDocuments === 0) {
+ // No documents in dataroom
+ toast.info("No documents found in dataroom to index");
+ setIndexing(false);
}
- onUpdate?.();
+ if (data.errors && data.errors.length > 0) {
+ toast.warning(`Some documents had errors: ${data.errors.join(", ")}`);
+ }
} catch (error: any) {
- console.error("Error indexing documents:", error);
- toast.error(error.message || "Failed to index documents");
- } finally {
+ console.error("Error indexing dataroom:", error);
+ toast.error(error.message || "Failed to index dataroom");
setIndexing(false);
}
};
- const isIndexed = type === "document" ? !!vectorStoreId : !!vectorStoreId;
+ // Handle batch indexing completion
+ const handleBatchIndexingComplete = async () => {
+ setIndexing(false);
+ setIndexingRuns([]);
+ toast.success("All documents indexed successfully");
+
+ // Mutate the dataroom cache
+ await mutate(`/api/teams/${teamId}/datarooms/${dataroomId}`);
+ onUpdate?.();
+ };
+
+ const isIndexed = !!vectorStoreId;
return (
<Card>
<CardHeader>
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
- <Bot className="h-5 w-5 text-primary" />
+ <PapermarkSparkle className="h-5 w-5 text-primary" />
<CardTitle>AI Agents</CardTitle>
</div>
<span
@@ -174,9 +206,8 @@ export function AgentsSettingsCard({
</span>
</div>
<CardDescription>
- Enable AI-powered chat to let visitors ask questions about{" "}
- {type === "document" ? "this document" : "documents in this " + type}.
- Documents must be indexed for chat to work.
+ Enable AI-powered chat to let visitors ask questions about documents
+ in this dataroom. Documents must be indexed for chat to work.
</CardDescription>
</CardHeader>
@@ -185,8 +216,7 @@ export function AgentsSettingsCard({
<Label htmlFor="agents-enabled" className="flex flex-col space-y-1">
<span>Enable AI Agents</span>
<span className="text-xs font-normal leading-snug text-muted-foreground">
- Allow visitors to chat with AI about{" "}
- {type === "document" ? "this document" : "these documents"}
+ Allow visitors to chat with AI about these documents
</span>
</Label>
<Switch
@@ -198,39 +228,179 @@ export function AgentsSettingsCard({
</div>
{agentsEnabled && (
- <div className="flex items-center justify-between space-x-2 border-t pt-4">
- <div className="flex flex-col space-y-1">
- <span className="text-sm font-medium">Index Status</span>
- <span className="text-xs text-muted-foreground">
- {isIndexed
- ? `${type === "document" ? "Document" : "Documents"} indexed and ready`
- : `${type === "document" ? "Document needs" : "Documents need"} to be indexed`}
- </span>
+ <div className="space-y-4 border-t pt-4">
+ <div className="flex items-center justify-between space-x-2">
+ <div className="flex flex-col space-y-1">
+ <span className="text-sm font-medium">Index Status</span>
+ <span className="text-xs text-muted-foreground">
+ {isIndexed
+ ? "Dataroom indexed and ready"
+ : "Dataroom needs to be indexed"}
+ </span>
+ </div>
+ <Button
+ onClick={handleIndexDataroom}
+ disabled={indexing}
+ size="sm"
+ >
+ {indexing ? (
+ <>
+ <Loader2 className="mr-2 h-4 w-4 animate-spin" />
+ Indexing...
+ </>
+ ) : (
+ <>Index Dataroom</>
+ )}
+ </Button>
</div>
- <Button
- onClick={handleIndexDocuments}
- disabled={indexing}
- size="sm"
- >
- {indexing ? (
- <>
- <Loader2 className="mr-2 h-4 w-4 animate-spin" />
- Indexing...
- </>
- ) : (
- <>Index {type === "document" ? "Document" : "Documents"}</>
- )}
- </Button>
+
+ {/* Batch dataroom indexing status */}
+ {indexingRuns.length > 0 && (
+ <DataroomIndexingStatus
+ runs={indexingRuns}
+ onAllComplete={handleBatchIndexingComplete}
+ />
+ )}
</div>
)}
</CardContent>
<CardFooter className="flex items-center justify-between rounded-b-lg border-t bg-muted px-6 py-3">
<p className="text-sm text-muted-foreground transition-colors">
- AI Agents use OpenAI to answer questions. Only PDF documents are
- currently supported.
+ AI Agents use OpenAI to answer questions. Supports PDF, DOCX, PPTX,
+ Excel, CSV, and image files.
</p>
</CardFooter>
</Card>
);
}
+
+/**
+ * Component for displaying batch indexing status for datarooms
+ */
+function DataroomIndexingStatus({
+ runs,
+ onAllComplete,
+}: {
+ runs: IndexingRun[];
+ onAllComplete: () => void;
+}) {
+ const [completedRuns, setCompletedRuns] = useState<Set<string>>(new Set());
+ const [failedRuns, setFailedRuns] = useState<Set<string>>(new Set());
+
+ const handleRunComplete = (runId: string) => {
+ setCompletedRuns((prev) => new Set(prev).add(runId));
+ };
+
+ const handleRunError = (runId: string) => {
+ setFailedRuns((prev) => new Set(prev).add(runId));
+ };
+
+ // Check if all runs are finished (completed or failed)
+ useEffect(() => {
+ const allFinished = runs.every(
+ (run) => completedRuns.has(run.runId) || failedRuns.has(run.runId),
+ );
+ if (allFinished && runs.length > 0) {
+ onAllComplete();
+ }
+ }, [completedRuns, failedRuns, runs, onAllComplete]);
+
+ const completedCount = completedRuns.size;
+ const failedCount = failedRuns.size;
+ const totalCount = runs.length;
+ const progress = Math.round(
+ ((completedCount + failedCount) / totalCount) * 100,
+ );
+
+ return (
+ <div className="space-y-3 rounded-lg border p-3">
+ <div className="flex items-center justify-between">
+ <div className="flex items-center gap-2">
+ <Loader2 className="h-4 w-4 animate-spin text-primary" />
+ <span className="text-sm font-medium">
+ Indexing {totalCount} document{totalCount > 1 ? "s" : ""}
+ </span>
+ </div>
+ <span className="text-xs text-muted-foreground">
+ {completedCount}/{totalCount} completed
+ {failedCount > 0 && `, ${failedCount} failed`}
+ </span>
+ </div>
+
+ <Progress value={progress} className="h-1.5" />
+
+ <div className="max-h-40 space-y-2 overflow-y-auto">
+ {runs.map((run) => (
+ <DataroomDocumentIndexingRow
+ key={run.runId}
+ run={run}
+ isCompleted={completedRuns.has(run.runId)}
+ isFailed={failedRuns.has(run.runId)}
+ onComplete={() => handleRunComplete(run.runId)}
+ onError={() => handleRunError(run.runId)}
+ />
+ ))}
+ </div>
+ </div>
+ );
+}
+
+/**
+ * Individual document row in batch indexing status
+ */
+function DataroomDocumentIndexingRow({
+ run,
+ isCompleted,
+ isFailed,
+ onComplete,
+ onError,
+}: {
+ run: IndexingRun;
+ isCompleted: boolean;
+ isFailed: boolean;
+ onComplete: () => void;
+ onError: () => void;
+}) {
+ const status = useAIIndexingStatus({ runId: run.runId });
+
+ // Trigger callbacks when status changes
+ useEffect(() => {
+ if (status.isCompleted && !isCompleted) {
+ onComplete();
+ }
+ if (status.isFailed && !isFailed) {
+ onError();
+ }
+ }, [
+ status.isCompleted,
+ status.isFailed,
+ isCompleted,
+ isFailed,
+ onComplete,
+ onError,
+ ]);
+
+ return (
+ <div className="flex items-center justify-between rounded border px-2 py-1.5">
+ <span className="max-w-[60%] truncate text-xs" title={run.documentName}>
+ {run.documentName}
+ </span>
+ <div className="flex items-center gap-2">
+ {status.isProcessing && (
+ <>
+ <Loader2 className="h-3 w-3 animate-spin text-primary" />
+ <span className="text-xs text-muted-foreground">
+ {status.progress}%
+ </span>
+ </>
+ )}
+ {isCompleted && <CheckCircle className="h-3.5 w-3.5 text-green-500" />}
+ {isFailed && <XCircle className="h-3.5 w-3.5 text-red-500" />}
+ {!status.isProcessing && !isCompleted && !isFailed && (
+ <span className="text-xs text-muted-foreground">Queued</span>
+ )}
+ </div>
+ </div>
+ );
+}
diff --git a/ee/features/ai/components/ai-indexing-status.tsx b/ee/features/ai/components/ai-indexing-status.tsx
new file mode 100644
index 00000000..a32de767
--- /dev/null
+++ b/ee/features/ai/components/ai-indexing-status.tsx
@@ -0,0 +1,129 @@
+"use client";
+
+import { useEffect } from "react";
+
+import { CheckCircle, Loader2, XCircle } from "lucide-react";
+
+import { cn } from "@/lib/utils";
+
+import { Progress } from "@/components/ui/progress";
+
+import { useAIIndexingStatus } from "../hooks/use-ai-indexing-status";
+
+interface AIIndexingStatusProps {
+ runId: string | null;
+ onComplete?: (vectorStoreFileId: string) => void;
+ onError?: (error: string) => void;
+ className?: string;
+}
+
+/**
+ * AI indexing status display component with polling
+ * Shows progress, current step, and final status
+ */
+export function AIIndexingStatus({
+ runId,
+ onComplete,
+ onError,
+ className,
+}: AIIndexingStatusProps) {
+ const {
+ isProcessing,
+ isCompleted,
+ isFailed,
+ step,
+ progress,
+ error,
+ vectorStoreFileId,
+ } = useAIIndexingStatus({ runId });
+
+ // Handle completion callback
+ useEffect(() => {
+ if (isCompleted && vectorStoreFileId && onComplete) {
+ onComplete(vectorStoreFileId);
+ }
+ }, [isCompleted, vectorStoreFileId, onComplete]);
+
+ // Handle error callback
+ useEffect(() => {
+ if (isFailed && error && onError) {
+ onError(error);
+ }
+ }, [isFailed, error, onError]);
+
+ if (!runId) {
+ return null;
+ }
+
+ return (
+ <div className={cn("flex flex-col gap-2", className)}>
+ {isProcessing && (
+ <div className="flex items-center gap-3">
+ <Loader2 className="h-4 w-4 animate-spin text-primary" />
+ <div className="flex flex-1 flex-col gap-1">
+ <span className="text-sm text-muted-foreground">{step || "Processing..."}</span>
+ <Progress value={progress} className="h-1.5" />
+ </div>
+ <span className="text-xs text-muted-foreground">{progress}%</span>
+ </div>
+ )}
+
+ {isCompleted && (
+ <div className="flex items-center gap-2">
+ <CheckCircle className="h-4 w-4 text-green-500" />
+ <span className="text-sm text-green-600">Indexed successfully</span>
+ </div>
+ )}
+
+ {isFailed && (
+ <div className="flex items-center gap-2">
+ <XCircle className="h-4 w-4 text-red-500" />
+ <span className="text-sm text-red-600">
+ {error || "Indexing failed"}
+ </span>
+ </div>
+ )}
+ </div>
+ );
+}
+
+interface AIIndexingBatchStatusProps {
+ runs: Array<{
+ documentId: string;
+ documentName: string;
+ runId: string;
+ }>;
+ onAllComplete?: () => void;
+ className?: string;
+}
+
+/**
+ * Status display for batch AI indexing (multiple documents)
+ */
+export function AIIndexingBatchStatus({
+ runs,
+ onAllComplete,
+ className,
+}: AIIndexingBatchStatusProps) {
+ if (runs.length === 0) {
+ return null;
+ }
+
+ return (
+ <div className={cn("space-y-3", className)}>
+ <div className="text-sm font-medium">
+ Indexing {runs.length} document{runs.length > 1 ? "s" : ""}...
+ </div>
+ <div className="max-h-60 space-y-2 overflow-y-auto">
+ {runs.map((run) => (
+ <div key={run.documentId} className="rounded border p-2">
+ <div className="mb-1 text-xs font-medium truncate">
+ {run.documentName}
+ </div>
+ <AIIndexingStatus runId={run.runId} />
+ </div>
+ ))}
+ </div>
+ </div>
+ );
+}
diff --git a/ee/features/ai/components/document-ai-dialog.tsx b/ee/features/ai/components/document-ai-dialog.tsx
index e1a26536..7c6e714b 100644
--- a/ee/features/ai/components/document-ai-dialog.tsx
+++ b/ee/features/ai/components/document-ai-dialog.tsx
@@ -2,7 +2,9 @@
import { useState } from "react";
-import { Bot, ExternalLink, Loader2, Shield } from "lucide-react";
+import { ExternalLink, Shield } from "lucide-react";
+
+import PapermarkSparkle from "@/components/shared/icons/papermark-sparkle";
import { toast } from "sonner";
import { mutate } from "swr";
@@ -21,6 +23,8 @@ import {
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
+import { AIIndexingStatus } from "./ai-indexing-status";
+
interface DocumentAIDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
@@ -45,12 +49,18 @@ export function DocumentAIDialog({
const [loading, setLoading] = useState(false);
const [indexing, setIndexing] = useState(false);
+ // Run tracking state for polling
+ const [indexingRunId, setIndexingRunId] = useState<string | null>(null);
+
// Don't render if AI feature is not enabled
if (!isAIFeatureEnabled) {
return null;
}
- const handleToggleAgents = async (enabled: boolean) => {
+ const isIndexed = !!vectorStoreFileId;
+
+ // Disable agents (only allowed if already enabled)
+ const handleDisableAgents = async () => {
setLoading(true);
try {
@@ -61,30 +71,31 @@ export function DocumentAIDialog({
headers: {
"Content-Type": "application/json",
},
- body: JSON.stringify({ agentsEnabled: enabled }),
+ body: JSON.stringify({ agentsEnabled: false }),
},
);
if (!response.ok) {
- throw new Error("Failed to update agents setting");
+ throw new Error("Failed to disable agents");
}
- setAgentsEnabled(enabled);
- toast.success(`AI Agents ${enabled ? "enabled" : "disabled"}`);
+ setAgentsEnabled(false);
+ toast.success("AI Agents disabled");
// Mutate the document cache
await mutate(`/api/teams/${teamId}/documents/${documentId}`);
} catch (error) {
- console.error("Error toggling agents:", error);
- toast.error("Failed to update agents setting");
- setAgentsEnabled(!enabled);
+ console.error("Error disabling agents:", error);
+ toast.error("Failed to disable agents");
} finally {
setLoading(false);
}
};
- const handleIndexDocument = async () => {
+ // Enable agents and index document in one flow
+ const handleEnableAndIndex = async () => {
setIndexing(true);
+ setIndexingRunId(null);
try {
const response = await fetch(
@@ -105,26 +116,101 @@ export function DocumentAIDialog({
throw new Error(data.error);
}
- toast.success("Document indexed successfully");
-
- // Mutate the document cache
- await mutate(`/api/teams/${teamId}/documents/${documentId}`);
+ // Set up polling tracking
+ if (data.runId) {
+ setIndexingRunId(data.runId);
+ }
} catch (error: any) {
console.error("Error indexing document:", error);
toast.error(error.message || "Failed to index document");
- } finally {
setIndexing(false);
}
};
- const isIndexed = !!vectorStoreFileId;
+ // Re-index an already enabled document
+ const handleReindex = async () => {
+ setIndexing(true);
+ setIndexingRunId(null);
+
+ try {
+ const response = await fetch(
+ `/api/ai/store/teams/${teamId}/documents/${documentId}`,
+ {
+ method: "POST",
+ },
+ );
+
+ if (!response.ok) {
+ const error = await response.json();
+ throw new Error(error.error || "Failed to re-index document");
+ }
+
+ const data = await response.json();
+
+ if (data.error) {
+ throw new Error(data.error);
+ }
+
+ // Set up polling tracking
+ if (data.runId) {
+ setIndexingRunId(data.runId);
+ }
+ } catch (error: any) {
+ console.error("Error re-indexing document:", error);
+ toast.error(error.message || "Failed to re-index document");
+ setIndexing(false);
+ }
+ };
+
+ const handleIndexingComplete = async () => {
+ setIndexing(false);
+ setIndexingRunId(null);
+
+ // Only enable agents after successful indexing
+ if (!agentsEnabled) {
+ try {
+ const response = await fetch(
+ `/api/teams/${teamId}/documents/${documentId}`,
+ {
+ method: "PATCH",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({ agentsEnabled: true }),
+ },
+ );
+
+ if (!response.ok) {
+ throw new Error("Failed to enable agents");
+ }
+
+ setAgentsEnabled(true);
+ } catch (error) {
+ console.error("Error enabling agents after indexing:", error);
+ toast.error("Indexed successfully but failed to enable agents");
+ await mutate(`/api/teams/${teamId}/documents/${documentId}`);
+ return;
+ }
+ }
+
+ toast.success("Document indexed successfully");
+ // Mutate the document cache
+ await mutate(`/api/teams/${teamId}/documents/${documentId}`);
+ };
+
+ const handleIndexingError = (error: string) => {
+ setIndexing(false);
+ setIndexingRunId(null);
+ toast.error(error || "Failed to index document");
+ // Don't enable agentsEnabled on failure
+ };
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
- <Bot className="h-5 w-5 text-primary" />
+ <PapermarkSparkle className="h-5 w-5 text-primary" />
AI Agents
</DialogTitle>
<DialogDescription>
@@ -134,60 +220,72 @@ export function DocumentAIDialog({
</DialogHeader>
<div className="space-y-4 py-4">
- {/* Toggle Section */}
- <div className="flex items-center justify-between space-x-2 rounded-lg border p-4">
- <Label htmlFor="agents-toggle" className="flex flex-col space-y-1">
- <span className="font-medium">Enable AI Chat</span>
- <span className="text-xs font-normal leading-snug text-muted-foreground">
- Allow visitors to chat with AI about this document
- </span>
- </Label>
- <Switch
- id="agents-toggle"
- checked={agentsEnabled}
- onCheckedChange={handleToggleAgents}
- disabled={loading}
- />
- </div>
-
- {/* Index Status */}
- {agentsEnabled && (
- <div className="flex items-center justify-between space-x-2 rounded-lg border p-4">
+ {/* Status & Actions Section */}
+ <div className="space-y-3 rounded-lg border p-4">
+ <div className="flex items-center justify-between space-x-2">
<div className="flex flex-col space-y-1">
- <span className="text-sm font-medium">Index Status</span>
+ <span className="text-sm font-medium">
+ {agentsEnabled ? "AI Chat Status" : "Enable AI Chat"}
+ </span>
<span className="text-xs text-muted-foreground">
- {isIndexed
- ? "Document is indexed and ready for AI chat"
- : "Document needs to be indexed before AI chat can work"}
+ {agentsEnabled
+ ? isIndexed
+ ? "Document is indexed and ready for AI chat"
+ : "Document needs to be re-indexed"
+ : "Index your document to enable AI-powered chat"}
</span>
</div>
<div className="flex items-center gap-2">
- <span
- className={cn(
- "inline-flex h-2 w-2 rounded-full",
- isIndexed ? "bg-green-500" : "bg-amber-500",
- )}
- />
- <Button
- onClick={handleIndexDocument}
- disabled={indexing}
- size="sm"
- variant={isIndexed ? "outline" : "default"}
- >
- {indexing ? (
- <>
- <Loader2 className="mr-2 h-4 w-4 animate-spin" />
- Indexing...
- </>
- ) : isIndexed ? (
- "Re-index"
- ) : (
- "Index Document"
- )}
- </Button>
+ {agentsEnabled && (
+ <span
+ className={cn(
+ "inline-flex h-2 w-2 rounded-full",
+ isIndexed ? "bg-green-500" : "bg-amber-500",
+ )}
+ />
+ )}
+ {agentsEnabled ? (
+ <>
+ <Button
+ onClick={handleReindex}
+ disabled={indexing}
+ size="sm"
+ variant="outline"
+ >
+ {indexing ? "Indexing..." : "Re-index"}
+ </Button>
+ <Button
+ onClick={handleDisableAgents}
+ disabled={loading || indexing}
+ size="sm"
+ variant="ghost"
+ className="text-destructive hover:text-destructive"
+ >
+ Disable
+ </Button>
+ </>
+ ) : (
+ <Button
+ onClick={handleEnableAndIndex}
+ disabled={indexing}
+ size="sm"
+ >
+ {indexing ? "Indexing..." : "Enable AI Chat"}
+ </Button>
+ )}
</div>
</div>
- )}
+
+ {/* Indexing status with polling */}
+ {indexingRunId && (
+ <AIIndexingStatus
+ runId={indexingRunId}
+ onComplete={handleIndexingComplete}
+ onError={handleIndexingError}
+ className="pt-2 border-t"
+ />
+ )}
+ </div>
{/* Privacy Notice */}
<div className="rounded-lg border border-green-200 bg-green-50 p-4 dark:border-green-900 dark:bg-green-950/30">
diff --git a/ee/features/ai/hooks/use-ai-indexing-status.ts b/ee/features/ai/hooks/use-ai-indexing-status.ts
new file mode 100644
index 00000000..f8b85f50
--- /dev/null
+++ b/ee/features/ai/hooks/use-ai-indexing-status.ts
@@ -0,0 +1,127 @@
+"use client";
+
+import { useCallback, useEffect, useState } from "react";
+
+import type { AIProcessingMetadata, AIProcessingStatus } from "../lib/trigger/types";
+
+interface UseAIIndexingStatusOptions {
+ runId: string | null;
+ pollInterval?: number;
+}
+
+interface UseAIIndexingStatusReturn {
+ isProcessing: boolean;
+ isCompleted: boolean;
+ isFailed: boolean;
+ status: AIProcessingStatus | undefined;
+ step: string | undefined;
+ progress: number;
+ error: string | undefined;
+ vectorStoreFileId: string | undefined;
+ fileId: string | undefined;
+ documentName: string | undefined;
+}
+
+interface RunStatusResponse {
+ id: string;
+ status: string;
+ metadata?: AIProcessingMetadata;
+ isCompleted: boolean;
+ isFailed: boolean;
+ output?: {
+ vectorStoreFileId?: string;
+ fileId?: string;
+ };
+}
+
+/**
+ * Custom hook for tracking AI indexing status via polling
+ * Polls the run status API endpoint at regular intervals
+ */
+export function useAIIndexingStatus({
+ runId,
+ pollInterval = 2000,
+}: UseAIIndexingStatusOptions): UseAIIndexingStatusReturn {
+ const [runStatus, setRunStatus] = useState<RunStatusResponse | null>(null);
+ const [error, setError] = useState<string | undefined>();
+
+ const fetchStatus = useCallback(async () => {
+ if (!runId) return;
+
+ try {
+ const response = await fetch(`/api/ai/store/runs/${runId}`);
+ if (!response.ok) {
+ throw new Error("Failed to fetch run status");
+ }
+ const data: RunStatusResponse = await response.json();
+ setRunStatus(data);
+ } catch (err) {
+ setError(err instanceof Error ? err.message : "Failed to fetch status");
+ }
+ }, [runId]);
+
+ useEffect(() => {
+ if (!runId) return;
+
+ // Initial fetch
+ fetchStatus();
+
+ // Poll while processing
+ const interval = setInterval(() => {
+ if (runStatus?.isCompleted || runStatus?.isFailed) {
+ clearInterval(interval);
+ return;
+ }
+ fetchStatus();
+ }, pollInterval);
+
+ return () => clearInterval(interval);
+ }, [runId, fetchStatus, pollInterval, runStatus?.isCompleted, runStatus?.isFailed]);
+
+ const metadata = runStatus?.metadata;
+
+ return {
+ isProcessing: runStatus?.status === "EXECUTING",
+ isCompleted: runStatus?.isCompleted ?? false,
+ isFailed: (runStatus?.isFailed ?? false) || metadata?.status === "failed",
+ status: metadata?.status,
+ step: metadata?.step,
+ progress: metadata?.progress ?? 0,
+ error: metadata?.error || error,
+ vectorStoreFileId: metadata?.vectorStoreFileId || runStatus?.output?.vectorStoreFileId,
+ fileId: metadata?.fileId || runStatus?.output?.fileId,
+ documentName: metadata?.documentName,
+ };
+}
+
+/**
+ * Hook for tracking multiple AI indexing runs (for dataroom batch processing)
+ */
+export function useAIIndexingBatchStatus() {
+ // For batch tracking, we track individual statuses
+ // The component using this should map over runs and call useAIIndexingStatus for each
+ // This is a helper to calculate aggregate stats
+
+ const calculateAggregateStats = (
+ statuses: UseAIIndexingStatusReturn[],
+ ) => {
+ const total = statuses.length;
+ const completed = statuses.filter((s) => s.isCompleted).length;
+ const failed = statuses.filter((s) => s.isFailed).length;
+ const processing = statuses.filter((s) => s.isProcessing).length;
+ const pending = total - completed - failed - processing;
+
+ return {
+ total,
+ completed,
+ failed,
+ processing,
+ pending,
+ progress: total > 0 ? Math.round((completed / total) * 100) : 0,
+ isAllCompleted: completed === total,
+ hasErrors: failed > 0,
+ };
+ };
+
+ return { calculateAggregateStats };
+}
diff --git a/pages/datarooms/[id]/settings/index.tsx b/pages/datarooms/[id]/settings/index.tsx
index 90be38a4..1556f473 100644
--- a/pages/datarooms/[id]/settings/index.tsx
+++ b/pages/datarooms/[id]/settings/index.tsx
@@ -132,8 +132,7 @@ export default function Settings() {
{/* AI Agents Settings */}
<AgentsSettingsCard
- type="dataroom"
- entityId={dataroom.id}
+ dataroomId={dataroom.id}
teamId={teamId!}
agentsEnabled={dataroom.agentsEnabled}
vectorStoreId={dataroom.vectorStoreId}
diff --git a/pages/settings/ai.tsx b/pages/settings/ai.tsx
index d4cbc05d..2fec19a4 100644
--- a/pages/settings/ai.tsx
+++ b/pages/settings/ai.tsx
@@ -3,7 +3,9 @@ import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { useTeam } from "@/context/team-context";
-import { Bot, ExternalLink, Shield, Sparkles } from "lucide-react";
+import { ExternalLink, Shield, Sparkles } from "lucide-react";
+
+import PapermarkSparkle from "@/components/shared/icons/papermark-sparkle";
import { useSession } from "next-auth/react";
import { toast } from "sonner";
import useSWR from "swr";
@@ -146,7 +148,7 @@ export default function AISettings() {
<Card>
<CardHeader>
<div className="flex items-center gap-2">
- <Bot className="h-5 w-5 text-primary" />
+ <PapermarkSparkle className="h-5 w-5 text-primary" />
<CardTitle>Enable AI Agents</CardTitle>
</div>
<CardDescription>