core/apps/webapp/app/lib/ingest.server.ts
Harshith Mullapudi 4a0a57cb97
Feat: add documents to the kg (#64)
* Feat: add documents to the kg

* Feat: add versioning to documents

* Fix: invalidation of evolved facts

* fix: mcp return

* fix: invalidAt is not displayed in graph popover

* Fix: use document id for the flow

* refactor: consolidate document versioning around sessionId instead of documentId

* fix: add docs link in welcome email

* fix: give more time for larger graphs to settle on

* bump: new version 0.1.20

---------

Co-authored-by: Manoj K <saimanoj58@gmail.com>
2025-09-03 12:39:46 +05:30

75 lines
1.8 KiB
TypeScript

// lib/ingest.queue.ts
import { IngestionStatus } from "@core/database";
import { EpisodeType } from "@core/types";
import { type z } from "zod";
import { prisma } from "~/db.server";
import { type IngestBodyRequest, ingestTask } from "~/trigger/ingest/ingest";
import { ingestDocumentTask } from "~/trigger/ingest/ingest-document";
export const addToQueue = async (
body: z.infer<typeof IngestBodyRequest>,
userId: string,
activityId?: string,
) => {
const user = await prisma.user.findFirst({
where: {
id: userId,
},
include: {
Workspace: true,
},
});
if (!user?.Workspace?.id) {
throw new Error(
"Workspace ID is required to create an ingestion queue entry.",
);
}
const queuePersist = await prisma.ingestionQueue.create({
data: {
spaceId: body.spaceId ? body.spaceId : null,
data: body,
status: IngestionStatus.PENDING,
priority: 1,
workspaceId: user.Workspace.id,
activityId,
},
});
let handler;
if (body.type === EpisodeType.DOCUMENT) {
handler = await ingestDocumentTask.trigger(
{
body,
userId,
workspaceId: user.Workspace.id,
queueId: queuePersist.id,
},
{
queue: "document-ingestion-queue",
concurrencyKey: userId,
tags: [user.id, queuePersist.id],
},
);
} else if (body.type === EpisodeType.CONVERSATION) {
handler = await ingestTask.trigger(
{
body,
userId,
workspaceId: user.Workspace.id,
queueId: queuePersist.id,
},
{
queue: "ingestion-queue",
concurrencyKey: userId,
tags: [user.id, queuePersist.id],
},
);
}
return { id: handler?.id, token: handler?.publicAccessToken };
};
export { IngestBodyRequest };