core/apps/webapp/app/routes/api.v1.activity.tsx
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

108 lines
3.0 KiB
TypeScript

import { json } from "@remix-run/node";
import { z } from "zod";
import { createActionApiRoute } from "~/services/routeBuilders/apiBuilder.server";
import { addToQueue } from "~/lib/ingest.server";
import { prisma } from "~/db.server";
import { logger } from "~/services/logger.service";
import { triggerWebhookDelivery } from "~/trigger/webhooks/webhook-delivery";
import { EpisodeTypeEnum } from "@core/types";
const ActivityCreateSchema = z.object({
text: z.string().min(1, "Text is required"),
source: z.string().min(1, "Source is required"),
sourceURL: z.string().url().optional(),
integrationAccountId: z.string().optional(),
taskId: z.string().optional(),
});
const { action, loader } = createActionApiRoute(
{
body: ActivityCreateSchema,
allowJWT: true,
authorization: {
action: "create",
},
corsStrategy: "all",
},
async ({ body, authentication }) => {
try {
logger.log("Creating activity", { body, userId: authentication.userId });
const user = await prisma.user.findUnique({
where: {
id: authentication.userId,
},
include: {
Workspace: true,
},
});
if (!user) {
throw new Error("User not found");
}
// Create the activity record
const activity = await prisma.activity.create({
data: {
text: body.text,
sourceURL: body.sourceURL,
integrationAccountId: body.integrationAccountId,
workspaceId: user.Workspace?.id || "",
},
});
// Add activity to knowledge graph ingestion queue
const ingestData = {
episodeBody: body.text,
referenceTime: new Date().toISOString(),
source: body.source,
type: EpisodeTypeEnum.CONVERSATION,
};
const queueResponse = await addToQueue(
ingestData,
authentication.userId,
activity.id,
);
logger.log("Activity created and queued for ingestion", {
activityId: activity.id,
queueId: queueResponse.id,
});
// Trigger webhook delivery for the new activity
if (user.Workspace?.id) {
try {
await triggerWebhookDelivery(activity.id, user.Workspace.id);
logger.log("Webhook delivery triggered for activity", { activityId: activity.id });
} catch (webhookError) {
logger.error("Failed to trigger webhook delivery", {
activityId: activity.id,
error: webhookError
});
// Don't fail the entire request if webhook delivery fails
}
}
return json({
success: true,
activity: {
id: activity.id,
text: activity.text,
sourceURL: activity.sourceURL,
createdAt: activity.createdAt,
},
ingestion: {
queueId: queueResponse.id,
},
});
} catch (error) {
logger.error("Failed to create activity", { error, body });
throw error;
}
},
);
export { action, loader };