Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat(ee): add ai processing for various document types
Marc Seitz08bbc630de35
diff --git a/app/(ee)/api/ai/store/teams/[teamId]/datarooms/[dataroomId]/route.ts b/app/(ee)/api/ai/store/teams/[teamId]/datarooms/[dataroomId]/route.tsindex cffacb26..69170dee 100644--- a/app/(ee)/api/ai/store/teams/[teamId]/datarooms/[dataroomId]/route.ts+++ b/app/(ee)/api/ai/store/teams/[teamId]/datarooms/[dataroomId]/route.ts@@ -1,8 +1,11 @@import { NextRequest, NextResponse } from "next/server";-import { processDocumentForVectorStore } from "@/ee/features/ai/lib/file-processing/process-document-for-vector-store";+import {+ SUPPORTED_AI_CONTENT_TYPES,+ addFileToVectorStoreTask,+ processDocumentForAITask,+} from "@/ee/features/ai/lib/trigger";import { createDataroomVectorStore } from "@/ee/features/ai/lib/vector-stores/create-dataroom-vector-store";-import { addFileToVectorStore } from "@/ee/features/ai/lib/vector-stores/upload-file-to-vector-store";import { authOptions } from "@/pages/api/auth/[...nextauth]";import { getServerSession } from "next-auth";@@ -13,6 +16,7 @@ import { CustomUser } from "@/lib/types";/*** POST /api/ai/store/teams/[teamId]/datarooms/[dataroomId]* Index all documents in a dataroom into its vector store+ * Returns batch runIds for status tracking via polling*/export async function POST(req: NextRequest,@@ -66,10 +70,20 @@ export async function POST(documents: {include: {document: {- include: {+ select: {+ id: true,+ name: true,versions: {where: { isPrimary: true },take: 1,+ select: {+ id: true,+ fileId: true,+ contentType: true,+ storageType: true,+ originalFile: true,+ file: true,+ },},},},@@ -117,10 +131,16 @@ export async function POST(});}- // Process and upload documents- let filesIndexed = 0;+ // Track triggered runs for status polling+ const triggeredRuns: Array<{+ documentId: string;+ documentName: string;+ runId: string;+ }> = [];+ const skippedDocuments: string[] = [];const errors: string[] = [];+ // Trigger processing for each documentfor (const dataroomDoc of dataroom.documents) {const document = dataroomDoc.document;const primaryVersion = document.versions[0];@@ -130,41 +150,29 @@ export async function POST(continue;}- // Skip if already indexed+ // Skip if already indexed in this dataroomif (dataroomDoc.vectorStoreFileId) {- filesIndexed++;+ skippedDocuments.push(document.name);continue;}- try {- // Check if document type is supported- const supportedTypes = ["application/pdf"];- if (!supportedTypes.includes(primaryVersion.contentType || "")) {- errors.push(- `Unsupported file type for document: ${document.name} (${primaryVersion.contentType})`,- );- continue;- }-- // get version's fileId- let fileId = primaryVersion.fileId;-- if (!fileId) {- const { fileId: newFileId } = await processDocumentForVectorStore(- primaryVersion.file,- primaryVersion.storageType,- );-- // Update document version with file ID- await prisma.documentVersion.update({- where: { id: primaryVersion.id },- data: { fileId: newFileId },- });+ // Check if document type is supported+ const contentType = primaryVersion.contentType || "";+ if (!SUPPORTED_AI_CONTENT_TYPES.includes(contentType)) {+ errors.push(+ `Unsupported file type for document: ${document.name} (${contentType})`,+ );+ continue;+ }- fileId = newFileId;- }+ try {+ // Determine file path+ const filePath =+ primaryVersion.originalFile && contentType !== "application/pdf"+ ? primaryVersion.originalFile+ : primaryVersion.file;- const metadata = {+ const fileMetadata = {teamId: dataroom.teamId,documentId: document.id,documentName: document.name,@@ -174,33 +182,65 @@ export async function POST(dataroomFolderId: dataroomDoc.folderId || "root",};- // Add to vector store- const vectorStoreFileId = await addFileToVectorStore({- vectorStoreId,- fileId,- metadata,- });+ let handle;- // Update dataroom document with vector store file ID- await prisma.dataroomDocument.update({- where: { id: dataroomDoc.id },- data: { vectorStoreFileId },- });+ // If document already has fileId, just add to vector store+ if (primaryVersion.fileId) {+ handle = await addFileToVectorStoreTask.trigger({+ fileId: primaryVersion.fileId,+ vectorStoreId,+ metadata: fileMetadata,+ });+ } else {+ // Trigger full processing+ handle = await processDocumentForAITask.trigger(+ {+ documentId: document.id,+ documentVersionId: primaryVersion.id,+ teamId: dataroom.teamId,+ vectorStoreId,+ documentName: document.name,+ filePath,+ storageType: primaryVersion.storageType,+ contentType,+ metadata: fileMetadata,+ },+ {+ idempotencyKey: `ai-index-dataroom-${dataroom.id}-${primaryVersion.id}`,+ tags: [+ `team_${dataroom.teamId}`,+ `dataroom_${dataroom.id}`,+ `document_${document.id}`,+ `version_${primaryVersion.id}`,+ ],+ },+ );+ }- filesIndexed++;+ triggeredRuns.push({+ documentId: document.id,+ documentName: document.name,+ runId: handle.id,+ });} catch (error) {- console.error(`Error indexing document ${document.name}:`, error);+ console.error(+ `Error triggering processing for ${document.name}:`,+ error,+ );errors.push(- `Failed to index document: ${document.name} - ${error instanceof Error ? error.message : "Unknown error"}`,+ `Failed to trigger processing for: ${document.name} - ${error instanceof Error ? error.message : "Unknown error"}`,);}}return NextResponse.json({success: true,- filesIndexed,- totalDocuments: dataroom.documents.length,vectorStoreId,+ totalDocuments: dataroom.documents.length,+ triggeredCount: triggeredRuns.length,+ skippedCount: skippedDocuments.length,+ runs: triggeredRuns,+ skipped: skippedDocuments.length > 0 ? skippedDocuments : undefined,errors: errors.length > 0 ? errors : undefined,});} catch (error) {diff --git a/app/(ee)/api/ai/store/teams/[teamId]/documents/[documentId]/route.ts b/app/(ee)/api/ai/store/teams/[teamId]/documents/[documentId]/route.tsindex 1da9f4ad..b5ec38de 100644--- a/app/(ee)/api/ai/store/teams/[teamId]/documents/[documentId]/route.ts+++ b/app/(ee)/api/ai/store/teams/[teamId]/documents/[documentId]/route.ts@@ -1,9 +1,12 @@import { NextRequest, NextResponse } from "next/server";-import { processDocumentForVectorStore } from "@/ee/features/ai/lib/file-processing/process-document-for-vector-store";+import {+ addFileToVectorStoreTask,+ processDocumentForAITask,+ SUPPORTED_AI_CONTENT_TYPES,+} from "@/ee/features/ai/lib/trigger";import { createTeamVectorStore } from "@/ee/features/ai/lib/vector-stores/create-team-vector-store";import { removeFileFromVectorStore } from "@/ee/features/ai/lib/vector-stores/remove-file-from-vector-store";-import { addFileToVectorStore } from "@/ee/features/ai/lib/vector-stores/upload-file-to-vector-store";import { authOptions } from "@/pages/api/auth/[...nextauth]";import { getServerSession } from "next-auth";@@ -14,6 +17,7 @@ import { CustomUser } from "@/lib/types";/*** POST /api/ai/store/teams/[teamId]/documents/[documentId]* Index a single document into the team vector store+ * Returns runId for status tracking via polling*/export async function POST(req: NextRequest,@@ -118,34 +122,23 @@ export async function POST(}// Check if document type is supported- const supportedTypes = ["application/pdf"];- if (!supportedTypes.includes(primaryVersion.contentType || "")) {+ const contentType = primaryVersion.contentType || "";+ if (!SUPPORTED_AI_CONTENT_TYPES.includes(contentType)) {return NextResponse.json({- error: `Unsupported file type: ${primaryVersion.contentType}. Only PDF files are supported.`,+ error: `Unsupported file type: ${contentType}. Supported types: PDF, Excel, and images (JPEG, PNG, WebP).`,},{ status: 400 },);}- let fileId = primaryVersion.fileId;+ // Determine file path - use originalFile for non-PDF, file for PDF+ const filePath =+ primaryVersion.originalFile && contentType !== "application/pdf"+ ? primaryVersion.originalFile+ : primaryVersion.file;- if (!fileId) {- const { fileId: newFileId } = await processDocumentForVectorStore(- primaryVersion.file,- primaryVersion.storageType,- );-- // Update document version with file ID- await prisma.documentVersion.update({- where: { id: primaryVersion.id },- data: { fileId: newFileId },- });-- fileId = newFileId;- }-- const metadata = {+ const fileMetadata = {teamId: document.teamId,documentId: document.id,documentName: document.name,@@ -153,23 +146,49 @@ export async function POST(folderId: document.folderId || "root",};- // Add to vector store- const vectorStoreFileId = await addFileToVectorStore({- vectorStoreId,- fileId,- metadata,- });+ // Check if document already has a fileId - use lightweight task+ if (primaryVersion.fileId) {+ // Document already processed, just add to vector store+ const handle = await addFileToVectorStoreTask.trigger({+ fileId: primaryVersion.fileId,+ vectorStoreId,+ metadata: fileMetadata,+ });- // Update document version with vector store file ID- await prisma.documentVersion.update({- where: { id: primaryVersion.id },- data: { vectorStoreFileId },- });+ return NextResponse.json({+ success: true,+ runId: handle.id,+ isReprocessing: false,+ });+ }++ // Trigger full document processing task+ const handle = await processDocumentForAITask.trigger(+ {+ documentId: document.id,+ documentVersionId: primaryVersion.id,+ teamId: document.teamId,+ vectorStoreId,+ documentName: document.name,+ filePath,+ storageType: primaryVersion.storageType,+ contentType,+ metadata: fileMetadata,+ },+ {+ idempotencyKey: `ai-index-${document.teamId}-${primaryVersion.id}`,+ tags: [+ `team_${document.teamId}`,+ `document_${document.id}`,+ `version_${primaryVersion.id}`,+ ],+ },+ );return NextResponse.json({success: true,- vectorStoreFileId,- vectorStoreId,+ runId: handle.id,+ isReprocessing: true,});} catch (error) {console.error("Error indexing document:", error);diff --git a/ee/features/ai/lib/models/google.ts b/ee/features/ai/lib/models/google.tsnew file mode 100644index 00000000..3f5bf16b--- /dev/null+++ b/ee/features/ai/lib/models/google.ts@@ -0,0 +1,7 @@+import { createVertex } from "@ai-sdk/google-vertex";++const vertex = createVertex({+ apiKey: process.env.GOOGLE_VERTEX_API_KEY,+});++export { vertex };diff --git a/ee/features/ai/lib/trigger/add-file-to-vector-store.ts b/ee/features/ai/lib/trigger/add-file-to-vector-store.tsnew file mode 100644index 00000000..39a9199a--- /dev/null+++ b/ee/features/ai/lib/trigger/add-file-to-vector-store.ts@@ -0,0 +1,55 @@+import { logger, metadata, task } from "@trigger.dev/sdk/v3";++import { openai } from "@/ee/features/ai/lib/models/openai";++import type { AddToVectorStorePayload } from "./types";++/**+ * Add an existing OpenAI file to a vector store+ * This is used when a document has already been processed and we just need to add it+ * to a new vector store (e.g., when adding to a dataroom)+ */+export const addFileToVectorStoreTask = task({+ id: "add-file-to-vector-store",+ retry: { maxAttempts: 3 },+ queue: {+ concurrencyLimit: 10,+ },+ run: async (+ payload: AddToVectorStorePayload,+ ): Promise<{ vectorStoreFileId: string }> => {+ const { fileId, vectorStoreId, metadata: fileMetadata } = payload;++ logger.info("Adding file to vector store", {+ fileId,+ vectorStoreId,+ documentId: fileMetadata.documentId,+ });++ metadata.set("status", "indexing");+ metadata.set("step", "Adding to vector store...");+ metadata.set("progress", 80);++ // Add file to vector store with metadata+ const vectorStoreFile = await openai.vectorStores.files.create(+ vectorStoreId,+ {+ file_id: fileId,+ attributes: fileMetadata,+ },+ );++ metadata.set("status", "completed");+ metadata.set("step", "Indexed successfully");+ metadata.set("progress", 100);+ metadata.set("vectorStoreFileId", vectorStoreFile.id);++ logger.info("File added to vector store successfully", {+ fileId,+ vectorStoreFileId: vectorStoreFile.id,+ documentId: fileMetadata.documentId,+ });++ return { vectorStoreFileId: vectorStoreFile.id };+ },+});diff --git a/ee/features/ai/lib/trigger/index.ts b/ee/features/ai/lib/trigger/index.tsnew file mode 100644index 00000000..4b318acd--- /dev/null+++ b/ee/features/ai/lib/trigger/index.ts@@ -0,0 +1,9 @@+// Export all AI processing tasks+export { addFileToVectorStoreTask } from "./add-file-to-vector-store";+export { processDocumentForAITask } from "./process-document-for-ai";+export { processExcelForAITask } from "./process-excel-for-ai";+export { processImageForAITask } from "./process-image-for-ai";+export { processPdfForAITask } from "./process-pdf-for-ai";++// Export types+export * from "./types";diff --git a/ee/features/ai/lib/trigger/process-document-for-ai.ts b/ee/features/ai/lib/trigger/process-document-for-ai.tsnew file mode 100644index 00000000..4717ee73--- /dev/null+++ b/ee/features/ai/lib/trigger/process-document-for-ai.ts@@ -0,0 +1,194 @@+import { logger, metadata, task } from "@trigger.dev/sdk/v3";++import prisma from "@/lib/prisma";++import { addFileToVectorStoreTask } from "./add-file-to-vector-store";+import { processExcelForAITask } from "./process-excel-for-ai";+import { processImageForAITask } from "./process-image-for-ai";+import { processPdfForAITask } from "./process-pdf-for-ai";+import {+ EXCEL_CONTENT_TYPES,+ IMAGE_CONTENT_TYPES,+ PDF_CONTENT_TYPES,+ SUPPORTED_AI_CONTENT_TYPES,+ type ProcessDocumentPayload,+} from "./types";++/**+ * Main orchestrator task for processing documents for AI+ * Routes to appropriate subtask based on content type, then adds to vector store+ */+export const processDocumentForAITask = task({+ id: "process-document-for-ai",+ retry: { maxAttempts: 3 },+ queue: {+ concurrencyLimit: 10,+ },+ run: async (+ payload: ProcessDocumentPayload,+ ): Promise<{ vectorStoreFileId: string; fileId: string }> => {+ const {+ documentId,+ documentVersionId,+ teamId,+ vectorStoreId,+ documentName,+ filePath,+ storageType,+ contentType,+ metadata: fileMetadata,+ } = payload;++ logger.info("Starting document processing for AI", {+ documentId,+ documentVersionId,+ teamId,+ contentType,+ });++ // Initialize metadata for real-time tracking+ metadata.set("status", "pending");+ metadata.set("documentName", documentName);+ metadata.set("documentId", documentId);+ metadata.set("step", "Initializing...");+ metadata.set("progress", 0);++ // Validate content type+ if (!SUPPORTED_AI_CONTENT_TYPES.includes(contentType)) {+ metadata.set("status", "failed");+ metadata.set("step", "Unsupported file type");+ metadata.set("error", `Unsupported content type: ${contentType}`);+ throw new Error(`Unsupported content type: ${contentType}`);+ }++ // Check if fileId already exists+ const version = await prisma.documentVersion.findUnique({+ where: { id: documentVersionId },+ select: { fileId: true },+ });++ let fileId = version?.fileId;++ // Process document if no fileId exists+ if (!fileId) {+ metadata.set("status", "processing");+ metadata.set("step", "Processing document...");+ metadata.set("progress", 10);++ // Route to appropriate processing task based on content type+ if (PDF_CONTENT_TYPES.includes(contentType)) {+ const result = await processPdfForAITask.triggerAndWait({+ documentId,+ documentVersionId,+ teamId,+ documentName,+ filePath,+ storageType,+ contentType,+ });++ if (!result.ok) {+ metadata.set("status", "failed");+ metadata.set("step", "PDF processing failed");+ metadata.set("error", "Failed to process PDF");+ throw new Error("Failed to process PDF");+ }++ fileId = result.output.fileId;+ } else if (EXCEL_CONTENT_TYPES.includes(contentType)) {+ const result = await processExcelForAITask.triggerAndWait({+ documentId,+ documentVersionId,+ teamId,+ documentName,+ filePath,+ storageType,+ contentType,+ });++ if (!result.ok) {+ metadata.set("status", "failed");+ metadata.set("step", "Excel processing failed");+ metadata.set("error", "Failed to process Excel file");+ throw new Error("Failed to process Excel file");+ }++ fileId = result.output.fileId;+ } else if (IMAGE_CONTENT_TYPES.includes(contentType)) {+ const result = await processImageForAITask.triggerAndWait({+ documentId,+ documentVersionId,+ teamId,+ documentName,+ filePath,+ storageType,+ contentType,+ });++ if (!result.ok) {+ metadata.set("status", "failed");+ metadata.set("step", "Image processing failed");+ metadata.set("error", "Failed to process image");+ throw new Error("Failed to process image");+ }++ fileId = result.output.fileId;+ }+ } else {+ logger.info("Document already processed, reusing fileId", {+ documentId,+ fileId,+ });+ metadata.set("step", "Using existing processed file...");+ metadata.set("progress", 50);+ }++ if (!fileId) {+ metadata.set("status", "failed");+ metadata.set("step", "Processing failed");+ metadata.set("error", "No file ID returned from processing");+ throw new Error("No file ID returned from processing");+ }++ metadata.set("fileId", fileId);++ // Add file to vector store+ metadata.set("status", "indexing");+ metadata.set("step", "Adding to vector store...");+ metadata.set("progress", 80);++ const vsResult = await addFileToVectorStoreTask.triggerAndWait({+ fileId,+ vectorStoreId,+ metadata: fileMetadata,+ });++ if (!vsResult.ok) {+ metadata.set("status", "failed");+ metadata.set("step", "Failed to add to vector store");+ metadata.set("error", "Failed to add file to vector store");+ throw new Error("Failed to add file to vector store");+ }++ const { vectorStoreFileId } = vsResult.output;++ // Update document version with vectorStoreFileId+ await prisma.documentVersion.update({+ where: { id: documentVersionId },+ data: { vectorStoreFileId },+ });++ metadata.set("status", "completed");+ metadata.set("step", "Indexed successfully");+ metadata.set("progress", 100);+ metadata.set("vectorStoreFileId", vectorStoreFileId);++ logger.info("Document processed and indexed successfully", {+ documentId,+ fileId,+ vectorStoreFileId,+ });++ return { vectorStoreFileId, fileId };+ },+});diff --git a/ee/features/ai/lib/trigger/process-excel-for-ai.ts b/ee/features/ai/lib/trigger/process-excel-for-ai.tsnew file mode 100644index 00000000..7a51e3b8--- /dev/null+++ b/ee/features/ai/lib/trigger/process-excel-for-ai.ts@@ -0,0 +1,193 @@+import { openai } from "@/ee/features/ai/lib/models/openai";+import { logger, metadata, task } from "@trigger.dev/sdk/v3";+import path from "path";+import * as XLSX from "xlsx";++import { getFile } from "@/lib/files/get-file";+import { putFileServer } from "@/lib/files/put-file-server";+import prisma from "@/lib/prisma";++import type { ProcessFilePayload } from "./types";++/**+ * Convert Excel workbook to Markdown format+ */+function excelToMarkdown(workbook: XLSX.WorkBook): string {+ const markdown: string[] = [];++ for (const sheetName of workbook.SheetNames) {+ const sheet = workbook.Sheets[sheetName];+ if (!sheet) continue;++ // Add sheet header+ markdown.push(`## ${sheetName}\n`);++ // Convert sheet to array of arrays+ const data: unknown[][] = XLSX.utils.sheet_to_json(sheet, {+ header: 1,+ defval: "",+ });++ if (data.length === 0) {+ markdown.push("*Empty sheet*\n");+ continue;+ }++ // Get headers (first row)+ const headers = data[0] as string[];+ if (!headers || headers.length === 0) continue;++ // Create markdown table header+ markdown.push(+ "| " + headers.map((h) => String(h || "")).join(" | ") + " |",+ );+ markdown.push("| " + headers.map(() => "---").join(" | ") + " |");++ // Add data rows+ for (let i = 1; i < data.length; i++) {+ const row = data[i] as unknown[];+ if (!row) continue;++ const cells = headers.map((_, idx) => {+ const cell = row[idx];+ // Handle different cell types+ if (cell === null || cell === undefined) return "";+ if (typeof cell === "object") return JSON.stringify(cell);+ return String(cell);+ });++ markdown.push("| " + cells.join(" | ") + " |");+ }++ markdown.push(""); // Empty line between sheets+ }++ return markdown.join("\n");+}++/**+ * Process an Excel file for AI indexing+ * Converts Excel to Markdown, saves to S3, then uploads to OpenAI+ */+export const processExcelForAITask = task({+ id: "process-excel-for-ai",+ retry: { maxAttempts: 3 },+ queue: {+ concurrencyLimit: 5,+ },+ run: async (+ payload: ProcessFilePayload,+ ): Promise<{ fileId: string; markdownPath?: string }> => {+ const {+ documentId,+ documentVersionId,+ teamId,+ documentName,+ filePath,+ storageType,+ } = payload;++ logger.info("Processing Excel for AI", {+ documentId,+ documentVersionId,+ teamId,+ });++ // Check if fileId already exists+ const version = await prisma.documentVersion.findUnique({+ where: { id: documentVersionId },+ select: { fileId: true },+ });++ if (version?.fileId) {+ logger.info("Excel already processed, reusing fileId", {+ documentId,+ fileId: version.fileId,+ });+ return { fileId: version.fileId };+ }++ metadata.set("status", "retrieving");+ metadata.set("step", "Retrieving Excel file...");+ metadata.set("progress", 10);++ // Get file URL+ const fileUrl = await getFile({+ type: storageType,+ data: filePath,+ isDownload: true,+ });++ // Fetch file+ const response = await fetch(fileUrl);+ if (!response.ok) {+ throw new Error(`Failed to fetch Excel file: ${response.statusText}`);+ }++ const buffer = Buffer.from(await response.arrayBuffer());++ metadata.set("status", "processing");+ metadata.set("step", "Converting Excel to Markdown...");+ metadata.set("progress", 30);++ // Parse Excel file+ const workbook = XLSX.read(buffer, { type: "buffer" });++ // Convert to markdown+ const markdown = excelToMarkdown(workbook);++ // Add document metadata to markdown+ const fullMarkdown = `# ${documentName}\n\n${markdown}`;++ metadata.set("step", "Saving processed content...");+ metadata.set("progress", 50);++ // Save markdown to S3 alongside original file+ // Derive markdown path from original file path+ const markdownPath = filePath.replace(/\.[^.]+$/, ".ai.md");+ const markdownBuffer = Buffer.from(fullMarkdown, "utf-8");++ // Extract docId from file path+ const match = filePath.match(/(doc_[^\/]+)\//);+ const docId = match ? match[1] : undefined;++ // Save markdown file to S3+ await putFileServer({+ file: {+ name: path.basename(markdownPath),+ type: "text/markdown",+ buffer: markdownBuffer,+ },+ teamId,+ docId,+ restricted: false,+ });++ metadata.set("status", "uploading");+ metadata.set("step", "Uploading to OpenAI...");+ metadata.set("progress", 70);++ // Upload markdown to OpenAI Files+ const file = new File([markdownBuffer], `${documentName}.md`, {+ type: "text/markdown",+ });+ const fileResponse = await openai.files.create({+ file,+ purpose: "assistants",+ });++ // Update document version with fileId+ await prisma.documentVersion.update({+ where: { id: documentVersionId },+ data: { fileId: fileResponse.id },+ });++ logger.info("Excel processed successfully", {+ documentId,+ fileId: fileResponse.id,+ markdownPath,+ });++ return { fileId: fileResponse.id, markdownPath };+ },+});diff --git a/ee/features/ai/lib/trigger/process-image-for-ai.ts b/ee/features/ai/lib/trigger/process-image-for-ai.tsnew file mode 100644index 00000000..eb1161ac--- /dev/null+++ b/ee/features/ai/lib/trigger/process-image-for-ai.ts@@ -0,0 +1,173 @@+import { vertex } from "@/ee/features/ai/lib/models/google";+import { openai } from "@/ee/features/ai/lib/models/openai";+import { logger, metadata, task } from "@trigger.dev/sdk/v3";+import { generateText } from "ai";+import path from "path";++import { getFile } from "@/lib/files/get-file";+import { putFileServer } from "@/lib/files/put-file-server";+import prisma from "@/lib/prisma";++import type { ProcessFilePayload } from "./types";++const IMAGE_ANALYSIS_PROMPT = `Analyze this image thoroughly and provide a detailed description for document search purposes.++Include the following in your analysis:+1. **Type of Image**: What kind of image is this? (e.g., diagram, chart, photo, screenshot, infographic, flowchart, etc.)+2. **Main Content**: What is the primary subject or content of the image?+3. **Text Content**: Extract and transcribe any visible text, labels, titles, or captions.+4. **Data & Numbers**: If there are charts, graphs, or tables, describe the data being presented.+5. **Key Details**: Note any important visual elements, colors, symbols, or indicators that convey meaning.+6. **Context**: What is the likely purpose or context of this image? What document or presentation might it belong to?++Format your response as clear, searchable text that would help someone find this image when searching for related topics.`;++/**+ * Process an image file for AI indexing+ * Uses Google Vertex AI (Gemini Flash) to analyze the image and generate a description+ */+export const processImageForAITask = task({+ id: "process-image-for-ai",+ retry: { maxAttempts: 3 },+ queue: {+ concurrencyLimit: 5,+ },+ run: async (+ payload: ProcessFilePayload,+ ): Promise<{ fileId: string; markdownPath?: string }> => {+ const {+ documentId,+ documentVersionId,+ teamId,+ documentName,+ filePath,+ storageType,+ contentType,+ } = payload;++ logger.info("Processing image for AI", {+ documentId,+ documentVersionId,+ teamId,+ contentType,+ });++ // Check if fileId already exists+ const version = await prisma.documentVersion.findUnique({+ where: { id: documentVersionId },+ select: { fileId: true },+ });++ if (version?.fileId) {+ logger.info("Image already processed, reusing fileId", {+ documentId,+ fileId: version.fileId,+ });+ return { fileId: version.fileId };+ }++ metadata.set("status", "retrieving");+ metadata.set("step", "Retrieving image...");+ metadata.set("progress", 10);++ // Get file URL+ const fileUrl = await getFile({+ type: storageType,+ data: filePath,+ isDownload: true,+ });++ // Fetch file+ const response = await fetch(fileUrl);+ if (!response.ok) {+ throw new Error(`Failed to fetch image: ${response.statusText}`);+ }++ const imageBuffer = Buffer.from(await response.arrayBuffer());++ metadata.set("status", "processing");+ metadata.set("step", "Analyzing image with AI...");+ metadata.set("progress", 30);++ // Use Gemini Flash to analyze the image+ const { text: imageDescription } = await generateText({+ model: vertex("gemini-3-flash-preview"),+ messages: [+ {+ role: "user",+ content: [+ {+ type: "image",+ image: imageBuffer,+ },+ {+ type: "text",+ text: IMAGE_ANALYSIS_PROMPT,+ },+ ],+ },+ ],+ });++ metadata.set("step", "Saving processed content...");+ metadata.set("progress", 50);++ // Create markdown content with image analysis+ const fullMarkdown = `# ${documentName}++## Image Analysis++${imageDescription}++---+*This content was automatically generated from image analysis.*+`;++ // Save markdown to S3 alongside original file+ const markdownPath = filePath.replace(/\.[^.]+$/, ".ai.md");+ const markdownBuffer = Buffer.from(fullMarkdown, "utf-8");++ // Extract docId from file path+ const match = filePath.match(/(doc_[^\/]+)\//);+ const docId = match ? match[1] : undefined;++ // Save markdown file to S3+ await putFileServer({+ file: {+ name: path.basename(markdownPath),+ type: "text/markdown",+ buffer: markdownBuffer,+ },+ teamId,+ docId,+ restricted: false,+ });++ metadata.set("status", "uploading");+ metadata.set("step", "Uploading to OpenAI...");+ metadata.set("progress", 70);++ // Upload markdown to OpenAI Files+ const file = new File([markdownBuffer], `${documentName}.md`, {+ type: "text/markdown",+ });+ const fileResponse = await openai.files.create({+ file,+ purpose: "assistants",+ });++ // Update document version with fileId+ await prisma.documentVersion.update({+ where: { id: documentVersionId },+ data: { fileId: fileResponse.id },+ });++ logger.info("Image processed successfully", {+ documentId,+ fileId: fileResponse.id,+ markdownPath,+ });++ return { fileId: fileResponse.id, markdownPath };+ },+});diff --git a/ee/features/ai/lib/trigger/process-pdf-for-ai.ts b/ee/features/ai/lib/trigger/process-pdf-for-ai.tsnew file mode 100644index 00000000..b883b30b--- /dev/null+++ b/ee/features/ai/lib/trigger/process-pdf-for-ai.ts@@ -0,0 +1,88 @@+import { logger, metadata, task } from "@trigger.dev/sdk/v3";+import path from "path";++import { openai } from "@/ee/features/ai/lib/models/openai";+import { getFile } from "@/lib/files/get-file";+import prisma from "@/lib/prisma";++import type { ProcessFilePayload } from "./types";++/**+ * Process a PDF file for AI indexing+ * PDFs can be uploaded directly to OpenAI without conversion+ */+export const processPdfForAITask = task({+ id: "process-pdf-for-ai",+ retry: { maxAttempts: 3 },+ queue: {+ concurrencyLimit: 5,+ },+ run: async (payload: ProcessFilePayload): Promise<{ fileId: string }> => {+ const { documentId, documentVersionId, teamId, documentName, filePath, storageType } =+ payload;++ logger.info("Processing PDF for AI", {+ documentId,+ documentVersionId,+ teamId,+ });++ // Check if fileId already exists+ const version = await prisma.documentVersion.findUnique({+ where: { id: documentVersionId },+ select: { fileId: true },+ });++ if (version?.fileId) {+ logger.info("PDF already processed, reusing fileId", {+ documentId,+ fileId: version.fileId,+ });+ return { fileId: version.fileId };+ }++ metadata.set("status", "retrieving");+ metadata.set("step", "Retrieving PDF...");+ metadata.set("progress", 20);++ // Get file URL+ const fileUrl = await getFile({+ type: storageType,+ data: filePath,+ isDownload: true,+ });++ // Fetch file+ const response = await fetch(fileUrl);+ if (!response.ok) {+ throw new Error(`Failed to fetch PDF: ${response.statusText}`);+ }++ const buffer = Buffer.from(await response.arrayBuffer());+ const fileName = path.basename(filePath);++ metadata.set("status", "uploading");+ metadata.set("step", "Uploading to OpenAI...");+ metadata.set("progress", 60);++ // Upload to OpenAI Files+ const file = new File([buffer], fileName, { type: "application/pdf" });+ const fileResponse = await openai.files.create({+ file,+ purpose: "assistants",+ });++ // Update document version with fileId+ await prisma.documentVersion.update({+ where: { id: documentVersionId },+ data: { fileId: fileResponse.id },+ });++ logger.info("PDF processed successfully", {+ documentId,+ fileId: fileResponse.id,+ });++ return { fileId: fileResponse.id };+ },+});diff --git a/ee/features/ai/lib/trigger/types.ts b/ee/features/ai/lib/trigger/types.tsnew file mode 100644index 00000000..43832d5b--- /dev/null+++ b/ee/features/ai/lib/trigger/types.ts@@ -0,0 +1,84 @@+import { DocumentStorageType } from "@prisma/client";++export type AIProcessingStatus =+ | "pending"+ | "retrieving"+ | "processing"+ | "uploading"+ | "indexing"+ | "completed"+ | "failed";++export type AIProcessingMetadata = {+ status: AIProcessingStatus;+ documentName: string;+ documentId: string;+ step: string;+ progress: number;+ error?: string;+ vectorStoreFileId?: string;+ fileId?: string;+};++export type ProcessDocumentPayload = {+ documentId: string;+ documentVersionId: string;+ teamId: string;+ vectorStoreId: string;+ documentName: string;+ filePath: string;+ storageType: DocumentStorageType;+ contentType: string;+ metadata: {+ teamId: string;+ documentId: string;+ documentName: string;+ versionId: string;+ folderId?: string;+ dataroomId?: string;+ dataroomDocumentId?: string;+ dataroomFolderId?: string;+ };+};++export type ProcessFilePayload = {+ documentId: string;+ documentVersionId: string;+ teamId: string;+ documentName: string;+ filePath: string;+ storageType: DocumentStorageType;+ contentType: string;+};++export type AddToVectorStorePayload = {+ fileId: string;+ vectorStoreId: string;+ metadata: {+ teamId: string;+ documentId: string;+ documentName: string;+ versionId: string;+ folderId?: string;+ dataroomId?: string;+ dataroomDocumentId?: string;+ dataroomFolderId?: string;+ };+};++// Supported content types+export const PDF_CONTENT_TYPES = ["application/pdf"];++export const EXCEL_CONTENT_TYPES = [+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",+ "application/vnd.ms-excel",+ "text/csv",+];++export const IMAGE_CONTENT_TYPES = ["image/jpeg", "image/png", "image/webp"];++export const SUPPORTED_AI_CONTENT_TYPES = [+ ...PDF_CONTENT_TYPES,+ ...EXCEL_CONTENT_TYPES,+ ...IMAGE_CONTENT_TYPES,+];diff --git a/lib/constants.ts b/lib/constants.tsindex cbcbf264..f5b5f849 100644--- a/lib/constants.ts+++ b/lib/constants.ts@@ -83,6 +83,7 @@ export const SUPPORTED_DOCUMENT_MIME_TYPES = ["application/rtf", // .rtf"text/rtf", // .rtf"text/plain", // .txt+ "text/markdown", // .md"image/vnd.dwg", // .dwg"image/vnd.dxf", // .dxf"image/png", // .pngdiff --git a/package-lock.json b/package-lock.jsonindex b865b911..3466b131 100644--- a/package-lock.json+++ b/package-lock.json@@ -9,6 +9,7 @@"version": "0.1.0","hasInstallScript": true,"dependencies": {+ "@ai-sdk/google-vertex": "^4.0.1","@ai-sdk/openai": "^3.0.1","@ai-sdk/react": "^3.0.3","@aws-sdk/client-lambda": "^3.958.0",@@ -177,6 +178,22 @@"node": ">=22"}},+ "node_modules/@ai-sdk/anthropic": {+ "version": "3.0.1",+ "resolved": "https://registry.npmjs.org/@ai-sdk/anthropic/-/anthropic-3.0.1.tgz",+ "integrity": "sha512-MOiwKs76ilEmau/WRMnGWlheTUoB+cbvXCse+SAtpW5ATLreInsuYlspLABn12Dxu3w1Xzke1dT+tmEnxhy9SA==",+ "license": "Apache-2.0",+ "dependencies": {+ "@ai-sdk/provider": "3.0.0",+ "@ai-sdk/provider-utils": "4.0.1"+ },+ "engines": {+ "node": ">=18"+ },+ "peerDependencies": {+ "zod": "^3.25.76 || ^4.1.8"+ }+ },"node_modules/@ai-sdk/gateway": {"version": "3.0.2","resolved": "https://registry.npmjs.org/@ai-sdk/gateway/-/gateway-3.0.2.tgz",@@ -194,6 +211,41 @@"zod": "^3.25.76 || ^4.1.8"}},+ "node_modules/@ai-sdk/google": {+ "version": "3.0.1",+ "resolved": "https://registry.npmjs.org/@ai-sdk/google/-/google-3.0.1.tgz",+ "integrity": "sha512-gh7i4lEvd1CElmefkq7+RoUhNkhP2OTshzVxSt7/Vh2AV5wTPLhduKJMg1c7SFwErytqffO3el/M/LlfCsqzEw==",+ "license": "Apache-2.0",+ "dependencies": {+ "@ai-sdk/provider": "3.0.0",+ "@ai-sdk/provider-utils": "4.0.1"+ },+ "engines": {+ "node": ">=18"+ },+ "peerDependencies": {+ "zod": "^3.25.76 || ^4.1.8"+ }+ },+ "node_modules/@ai-sdk/google-vertex": {+ "version": "4.0.1",+ "resolved": "https://registry.npmjs.org/@ai-sdk/google-vertex/-/google-vertex-4.0.1.tgz",+ "integrity": "sha512-WVyHy8cLlFHng3nmC3d1a/eRWZJ2zQjQkKss1kMZnU5x7M5FtAS97xKjf5UtKWvswaI9+AfcM632Dc1Hmu3M0w==",+ "license": "Apache-2.0",+ "dependencies": {+ "@ai-sdk/anthropic": "3.0.1",+ "@ai-sdk/google": "3.0.1",+ "@ai-sdk/provider": "3.0.0",+ "@ai-sdk/provider-utils": "4.0.1",+ "google-auth-library": "^10.5.0"+ },+ "engines": {+ "node": ">=18"+ },+ "peerDependencies": {+ "zod": "^3.25.76 || ^4.1.8"+ }+ },"node_modules/@ai-sdk/openai": {"version": "3.0.1","resolved": "https://registry.npmjs.org/@ai-sdk/openai/-/openai-3.0.1.tgz",@@ -9522,6 +9574,15 @@"node": ">=0.6"}},+ "node_modules/bignumber.js": {+ "version": "9.3.1",+ "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.3.1.tgz",+ "integrity": "sha512-Ko0uX15oIUS7wJ3Rb30Fs6SkVbLmPBAKdlm7q9+ak9bbIeFf0MwuBsQV6z7+X768/cHsfg+WlysDWJcmthjsjQ==",+ "license": "MIT",+ "engines": {+ "node": "*"+ }+ },"node_modules/binary": {"version": "0.3.0","resolved": "https://registry.npmjs.org/binary/-/binary-0.3.0.tgz",@@ -10916,6 +10977,15 @@"resolved": "https://registry.npmjs.org/damerau-levenshtein/-/damerau-levenshtein-1.0.8.tgz","integrity": "sha512-sdQSFB7+llfUcQHUQO3+B8ERRj0Oa4w9POWMI/puGtuf7gFywGmkaLCElnudfTiKZV+NvHqL0ifzdrI8Ro7ESA=="},+ "node_modules/data-uri-to-buffer": {+ "version": "4.0.1",+ "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz",+ "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==",+ "license": "MIT",+ "engines": {+ "node": ">= 12"+ }+ },"node_modules/data-view-buffer": {"version": "1.0.1","resolved": "https://registry.npmjs.org/data-view-buffer/-/data-view-buffer-1.0.1.tgz",@@ -12513,6 +12583,29 @@"reusify": "^1.0.4"}},+ "node_modules/fetch-blob": {+ "version": "3.2.0",+ "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz",+ "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==",+ "funding": [+ {+ "type": "github",+ "url": "https://github.com/sponsors/jimmywarting"+ },+ {+ "type": "paypal",+ "url": "https://paypal.me/jimmywarting"+ }+ ],+ "license": "MIT",+ "dependencies": {+ "node-domexception": "^1.0.0",+ "web-streams-polyfill": "^3.0.3"+ },+ "engines": {+ "node": "^12.20 || >= 14.13"+ }+ },"node_modules/fflate": {"version": "0.4.8","resolved": "https://registry.npmjs.org/fflate/-/fflate-0.4.8.tgz",@@ -12640,6 +12733,18 @@"url": "https://github.com/sponsors/isaacs"}},+ "node_modules/formdata-polyfill": {+ "version": "4.0.10",+ "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz",+ "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==",+ "license": "MIT",+ "dependencies": {+ "fetch-blob": "^3.1.2"+ },+ "engines": {+ "node": ">=12.20.0"+ }+ },"node_modules/fraction.js": {"version": "5.3.4","resolved": "https://registry.npmjs.org/fraction.js/-/fraction.js-5.3.4.tgz",@@ -12880,6 +12985,90 @@"node": ">=8"}},+ "node_modules/gaxios": {+ "version": "7.1.3",+ "resolved": "https://registry.npmjs.org/gaxios/-/gaxios-7.1.3.tgz",+ "integrity": "sha512-YGGyuEdVIjqxkxVH1pUTMY/XtmmsApXrCVv5EU25iX6inEPbV+VakJfLealkBtJN69AQmh1eGOdCl9Sm1UP6XQ==",+ "license": "Apache-2.0",+ "dependencies": {+ "extend": "^3.0.2",+ "https-proxy-agent": "^7.0.1",+ "node-fetch": "^3.3.2",+ "rimraf": "^5.0.1"+ },+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/gaxios/node_modules/agent-base": {+ "version": "7.1.4",+ "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.4.tgz",+ "integrity": "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ==",+ "license": "MIT",+ "engines": {+ "node": ">= 14"+ }+ },+ "node_modules/gaxios/node_modules/https-proxy-agent": {+ "version": "7.0.6",+ "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.6.tgz",+ "integrity": "sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==",+ "license": "MIT",+ "dependencies": {+ "agent-base": "^7.1.2",+ "debug": "4"+ },+ "engines": {+ "node": ">= 14"+ }+ },+ "node_modules/gaxios/node_modules/node-fetch": {+ "version": "3.3.2",+ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz",+ "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==",+ "license": "MIT",+ "dependencies": {+ "data-uri-to-buffer": "^4.0.0",+ "fetch-blob": "^3.1.4",+ "formdata-polyfill": "^4.0.10"+ },+ "engines": {+ "node": "^12.20.0 || ^14.13.1 || >=16.0.0"+ },+ "funding": {+ "type": "opencollective",+ "url": "https://opencollective.com/node-fetch"+ }+ },+ "node_modules/gaxios/node_modules/rimraf": {+ "version": "5.0.10",+ "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-5.0.10.tgz",+ "integrity": "sha512-l0OE8wL34P4nJH/H2ffoaniAokM2qSmrtXHmlpvYr5AVVX8msAyW0l8NVJFDxlSK4u3Uh/f41cQheDVdnYijwQ==",+ "license": "ISC",+ "dependencies": {+ "glob": "^10.3.7"+ },+ "bin": {+ "rimraf": "dist/esm/bin.mjs"+ },+ "funding": {+ "url": "https://github.com/sponsors/isaacs"+ }+ },+ "node_modules/gcp-metadata": {+ "version": "8.1.2",+ "resolved": "https://registry.npmjs.org/gcp-metadata/-/gcp-metadata-8.1.2.tgz",+ "integrity": "sha512-zV/5HKTfCeKWnxG0Dmrw51hEWFGfcF2xiXqcA3+J90WDuP0SvoiSO5ORvcBsifmx/FoIjgQN3oNOGaQ5PhLFkg==",+ "license": "Apache-2.0",+ "dependencies": {+ "gaxios": "^7.0.0",+ "google-logging-utils": "^1.0.0",+ "json-bigint": "^1.0.0"+ },+ "engines": {+ "node": ">=18"+ }+ },"node_modules/generic-pool": {"version": "3.9.0","resolved": "https://registry.npmjs.org/generic-pool/-/generic-pool-3.9.0.tgz",@@ -13060,6 +13249,33 @@"url": "https://github.com/sponsors/ljharb"}},+ "node_modules/google-auth-library": {+ "version": "10.5.0",+ "resolved": "https://registry.npmjs.org/google-auth-library/-/google-auth-library-10.5.0.tgz",+ "integrity": "sha512-7ABviyMOlX5hIVD60YOfHw4/CxOfBhyduaYB+wbFWCWoni4N7SLcV46hrVRktuBbZjFC9ONyqamZITN7q3n32w==",+ "license": "Apache-2.0",+ "dependencies": {+ "base64-js": "^1.3.0",+ "ecdsa-sig-formatter": "^1.0.11",+ "gaxios": "^7.0.0",+ "gcp-metadata": "^8.0.0",+ "google-logging-utils": "^1.0.0",+ "gtoken": "^8.0.0",+ "jws": "^4.0.0"+ },+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/google-logging-utils": {+ "version": "1.1.3",+ "resolved": "https://registry.npmjs.org/google-logging-utils/-/google-logging-utils-1.1.3.tgz",+ "integrity": "sha512-eAmLkjDjAFCVXg7A1unxHsLf961m6y17QFqXqAXGj/gVkKFrEICfStRfwUlGNfeCEjNRa32JEWOUTlYXPyyKvA==",+ "license": "Apache-2.0",+ "engines": {+ "node": ">=14"+ }+ },"node_modules/gopd": {"version": "1.2.0","resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz",@@ -13082,6 +13298,19 @@"resolved": "https://registry.npmjs.org/graphemer/-/graphemer-1.4.0.tgz","integrity": "sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag=="},+ "node_modules/gtoken": {+ "version": "8.0.0",+ "resolved": "https://registry.npmjs.org/gtoken/-/gtoken-8.0.0.tgz",+ "integrity": "sha512-+CqsMbHPiSTdtSO14O51eMNlrp9N79gmeqmXeouJOhfucAedHw9noVe/n5uJk3tbKE6a+6ZCQg3RPhVhHByAIw==",+ "license": "MIT",+ "dependencies": {+ "gaxios": "^7.0.0",+ "jws": "^4.0.0"+ },+ "engines": {+ "node": ">=18"+ }+ },"node_modules/hachure-fill": {"version": "0.5.2","resolved": "https://registry.npmjs.org/hachure-fill/-/hachure-fill-0.5.2.tgz",@@ -14333,6 +14562,15 @@"node": ">=6"}},+ "node_modules/json-bigint": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/json-bigint/-/json-bigint-1.0.0.tgz",+ "integrity": "sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==",+ "license": "MIT",+ "dependencies": {+ "bignumber.js": "^9.0.0"+ }+ },"node_modules/json-buffer": {"version": "3.0.1","resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz",@@ -16580,6 +16818,26 @@"node": "^10 || ^12 || >=14"}},+ "node_modules/node-domexception": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",+ "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==",+ "deprecated": "Use your platform's native DOMException instead",+ "funding": [+ {+ "type": "github",+ "url": "https://github.com/sponsors/jimmywarting"+ },+ {+ "type": "github",+ "url": "https://paypal.me/jimmywarting"+ }+ ],+ "license": "MIT",+ "engines": {+ "node": ">=10.5.0"+ }+ },"node_modules/node-fetch": {"version": "2.7.0","resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",@@ -21730,6 +21988,15 @@"url": "https://github.com/sponsors/wooorm"}},+ "node_modules/web-streams-polyfill": {+ "version": "3.3.3",+ "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz",+ "integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==",+ "license": "MIT",+ "engines": {+ "node": ">= 8"+ }+ },"node_modules/web-vitals": {"version": "4.2.4","resolved": "https://registry.npmjs.org/web-vitals/-/web-vitals-4.2.4.tgz",diff --git a/package.json b/package.jsonindex 9deb63c4..73cbfb69 100644--- a/package.json+++ b/package.json@@ -20,6 +20,7 @@"dev:prisma": "npx prisma generate && npx prisma migrate deploy"},"dependencies": {+ "@ai-sdk/google-vertex": "^4.0.1","@ai-sdk/openai": "^3.0.1","@ai-sdk/react": "^3.0.3","@aws-sdk/client-lambda": "^3.958.0",diff --git a/pages/api/teams/[teamId]/datarooms/[id]/documents/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/documents/index.tsindex 70f13952..5e8513e5 100644--- a/pages/api/teams/[teamId]/datarooms/[id]/documents/index.ts+++ b/pages/api/teams/[teamId]/datarooms/[id]/documents/index.ts@@ -1,11 +1,17 @@import { NextApiRequest, NextApiResponse } from "next";import { isTeamPausedById } from "@/ee/features/billing/cancellation/lib/is-team-paused";+import {+ addFileToVectorStoreTask,+ processDocumentForAITask,+ SUPPORTED_AI_CONTENT_TYPES,+} from "@/ee/features/ai/lib/trigger";import { authOptions } from "@/pages/api/auth/[...nextauth]";import { runs } from "@trigger.dev/sdk/v3";import { waitUntil } from "@vercel/functions";import { getServerSession } from "next-auth/next";+import { getFeatureFlags } from "@/lib/featureFlags";import { errorhandler } from "@/lib/errorHandler";import prisma from "@/lib/prisma";import { sendDataroomChangeNotificationTask } from "@/lib/trigger/dataroom-change-notification";@@ -164,16 +170,28 @@ export default async function handle(},});- const document = await prisma.dataroomDocument.create({+ const dataroomDocument = await prisma.dataroomDocument.create({data: {documentId,dataroomId,folderId: folder?.id,},include: {+ document: {+ include: {+ versions: {+ where: { isPrimary: true },+ take: 1,+ },+ },+ },dataroom: {select: {+ teamId: true,+ name: true,enableChangeNotifications: true,+ agentsEnabled: true,+ vectorStoreId: true,links: {select: { id: true },orderBy: { createdAt: "desc" },@@ -187,8 +205,79 @@ export default async function handle(},});+ // Auto-index document if dataroom has AI agents enabled+ if (+ dataroomDocument.dataroom.agentsEnabled &&+ dataroomDocument.dataroom.vectorStoreId+ ) {+ const primaryVersion = dataroomDocument.document.versions[0];+ const contentType = primaryVersion?.contentType || "";++ // Check if AI feature is enabled for the team+ const features = await getFeatureFlags({ teamId });++ if (features.ai && primaryVersion && SUPPORTED_AI_CONTENT_TYPES.includes(contentType)) {+ const filePath =+ primaryVersion.originalFile && contentType !== "application/pdf"+ ? primaryVersion.originalFile+ : primaryVersion.file;++ const fileMetadata = {+ teamId: dataroomDocument.dataroom.teamId,+ documentId: dataroomDocument.document.id,+ documentName: dataroomDocument.document.name,+ versionId: primaryVersion.id,+ dataroomId: dataroomDocument.dataroomId,+ dataroomDocumentId: dataroomDocument.id,+ dataroomFolderId: dataroomDocument.folderId || "root",+ };++ try {+ // If document already has fileId, just add to vector store+ if (primaryVersion.fileId) {+ waitUntil(+ addFileToVectorStoreTask.trigger({+ fileId: primaryVersion.fileId,+ vectorStoreId: dataroomDocument.dataroom.vectorStoreId,+ metadata: fileMetadata,+ }),+ );+ } else {+ // Trigger full processing+ waitUntil(+ processDocumentForAITask.trigger(+ {+ documentId: dataroomDocument.document.id,+ documentVersionId: primaryVersion.id,+ teamId: dataroomDocument.dataroom.teamId,+ vectorStoreId: dataroomDocument.dataroom.vectorStoreId,+ documentName: dataroomDocument.document.name,+ filePath,+ storageType: primaryVersion.storageType,+ contentType,+ metadata: fileMetadata,+ },+ {+ idempotencyKey: `ai-index-dataroom-${dataroomId}-${primaryVersion.id}`,+ tags: [+ `team_${teamId}`,+ `dataroom_${dataroomId}`,+ `document_${dataroomDocument.document.id}`,+ `version_${primaryVersion.id}`,+ ],+ },+ ),+ );+ }+ } catch (error) {+ console.error("Error triggering AI indexing for document:", error);+ // Don't fail the document add, just log the error+ }+ }+ }+// Check if the team has the dataroom change notification enabled- if (document.dataroom.enableChangeNotifications) {+ if (dataroomDocument.dataroom.enableChangeNotifications) {// Get all delayed and queued runs for this dataroomconst allRuns = await runs.list({taskIdentifier: ["send-dataroom-change-notification"],@@ -204,16 +293,16 @@ export default async function handle(sendDataroomChangeNotificationTask.trigger({dataroomId,- dataroomDocumentId: document.id,+ dataroomDocumentId: dataroomDocument.id,senderUserId: userId,teamId,},{- idempotencyKey: `dataroom-notification-${teamId}-${dataroomId}-${document.id}`,+ idempotencyKey: `dataroom-notification-${teamId}-${dataroomId}-${dataroomDocument.id}`,tags: [`team_${teamId}`,`dataroom_${dataroomId}`,- `document_${document.id}`,+ `document_${dataroomDocument.id}`,],delay: new Date(Date.now() + 10 * 60 * 1000), // 10 minute delay},@@ -221,7 +310,7 @@ export default async function handle();}- return res.status(201).json(document);+ return res.status(201).json(dataroomDocument);} catch (error) {log({message: `Failed to create dataroom document. \n\n*teamId*: _${teamId}_, \n\n*dataroomId*: ${dataroomId} \n\n ${error}`,diff --git a/pages/api/teams/[teamId]/documents/[id]/add-to-dataroom.ts b/pages/api/teams/[teamId]/documents/[id]/add-to-dataroom.tsindex 18225405..3c01b187 100644--- a/pages/api/teams/[teamId]/documents/[id]/add-to-dataroom.ts+++ b/pages/api/teams/[teamId]/documents/[id]/add-to-dataroom.ts@@ -1,12 +1,24 @@import { NextApiRequest, NextApiResponse } from "next";+import {+ addFileToVectorStoreTask,+ processDocumentForAITask,+ SUPPORTED_AI_CONTENT_TYPES,+} from "@/ee/features/ai/lib/trigger";import { authOptions } from "@/pages/api/auth/[...nextauth]";+import { waitUntil } from "@vercel/functions";import { getServerSession } from "next-auth/next";+import { getFeatureFlags } from "@/lib/featureFlags";import { errorhandler } from "@/lib/errorHandler";import prisma from "@/lib/prisma";import { CustomUser } from "@/lib/types";+export const config = {+ // in order to enable `waitUntil` function+ supportsResponseStreaming: true,+};+export default async function handle(req: NextApiRequest,res: NextApiResponse,@@ -59,17 +71,47 @@ export default async function handle(});}- try {- await prisma.dataroom.update({- where: {- id: dataroomId,+ // Fetch dataroom with AI settings+ const dataroom = await prisma.dataroom.findUnique({+ where: { id: dataroomId },+ select: {+ id: true,+ teamId: true,+ name: true,+ agentsEnabled: true,+ vectorStoreId: true,+ },+ });++ if (!dataroom) {+ return res.status(404).json({+ message: "Dataroom not found!",+ });+ }++ // Fetch document with primary version+ const document = await prisma.document.findUnique({+ where: { id: docId },+ include: {+ versions: {+ where: { isPrimary: true },+ take: 1,},+ },+ });++ if (!document) {+ return res.status(404).json({+ message: "Document not found!",+ });+ }++ let dataroomDocument;+ try {+ dataroomDocument = await prisma.dataroomDocument.create({data: {- documents: {- create: {- documentId: docId,- },- },+ documentId: docId,+ dataroomId,},});} catch (error) {@@ -78,6 +120,74 @@ export default async function handle(});}+ // Auto-index document if dataroom has AI agents enabled+ if (dataroom.agentsEnabled && dataroom.vectorStoreId) {+ const primaryVersion = document.versions[0];+ const contentType = primaryVersion?.contentType || "";++ // Check if AI feature is enabled for the team+ const features = await getFeatureFlags({ teamId });++ if (features.ai && primaryVersion && SUPPORTED_AI_CONTENT_TYPES.includes(contentType)) {+ const filePath =+ primaryVersion.originalFile && contentType !== "application/pdf"+ ? primaryVersion.originalFile+ : primaryVersion.file;++ const fileMetadata = {+ teamId: dataroom.teamId,+ documentId: document.id,+ documentName: document.name,+ versionId: primaryVersion.id,+ dataroomId: dataroom.id,+ dataroomDocumentId: dataroomDocument.id,+ dataroomFolderId: "root",+ };++ try {+ // If document already has fileId, just add to vector store+ if (primaryVersion.fileId) {+ waitUntil(+ addFileToVectorStoreTask.trigger({+ fileId: primaryVersion.fileId,+ vectorStoreId: dataroom.vectorStoreId,+ metadata: fileMetadata,+ }),+ );+ } else {+ // Trigger full processing+ waitUntil(+ processDocumentForAITask.trigger(+ {+ documentId: document.id,+ documentVersionId: primaryVersion.id,+ teamId: dataroom.teamId,+ vectorStoreId: dataroom.vectorStoreId,+ documentName: document.name,+ filePath,+ storageType: primaryVersion.storageType,+ contentType,+ metadata: fileMetadata,+ },+ {+ idempotencyKey: `ai-index-dataroom-${dataroomId}-${primaryVersion.id}`,+ tags: [+ `team_${teamId}`,+ `dataroom_${dataroomId}`,+ `document_${document.id}`,+ `version_${primaryVersion.id}`,+ ],+ },+ ),+ );+ }+ } catch (error) {+ console.error("Error triggering AI indexing for document:", error);+ // Don't fail the document add, just log the error+ }+ }+ }+return res.status(200).json({message: "Document added to dataroom!",});diff --git a/trigger.config.ts b/trigger.config.tsindex bc170916..4c0cc7fa 100644--- a/trigger.config.ts+++ b/trigger.config.ts@@ -4,7 +4,7 @@ import { defineConfig, timeout } from "@trigger.dev/sdk/v3";export default defineConfig({project: "proj_plmsfqvqunboixacjjus",- dirs: ["./lib/trigger"],+ dirs: ["./lib/trigger", "./ee/features/ai/lib/trigger"],maxDuration: timeout.None, // no max durationretries: {enabledInDev: false,