Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
2.3 kB ยท 77 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778import { and, eq, inArray } from "@openstatus/db";import { type ChatStoredMessage, MAX_CHAT_MESSAGES, chatSession,} from "@openstatus/db/src/schema";
import type { DB } from "../context";import { NotFoundError } from "../errors";
export async function getChatSessionInWorkspace(args: { tx: DB; sessionId: number; workspaceId: number; userId: number;}) { const { tx, sessionId, workspaceId, userId } = args; // Single combined WHERE โ collapses ownership checks into NotFoundError // so callers can't distinguish "exists but forbidden" from "missing". const row = await tx .select() .from(chatSession) .where( and( eq(chatSession.id, sessionId), eq(chatSession.workspaceId, workspaceId), eq(chatSession.userId, userId), ), ) .get(); if (!row) throw new NotFoundError("chat_session", sessionId); return row;}
/** * Bound message storage by dropping oldest entries if the array is over * `MAX_CHAT_MESSAGES`. Pending HITL tool parts are left intact โ the * route filters them at `convertToModelMessages` time before they reach * the provider. */export function compactMessages( messages: ChatStoredMessage[],): ChatStoredMessage[] { if (messages.length <= MAX_CHAT_MESSAGES) return messages.slice(); return messages.slice(messages.length - MAX_CHAT_MESSAGES);}
/** * Drop the oldest sessions for `(workspace, user)` so the row count * stays at `capAfterInsert` once the caller inserts a new one. */export async function enforceSessionCap(args: { tx: DB; workspaceId: number; userId: number; capAfterInsert: number;}): Promise<void> { const { tx, workspaceId, userId, capAfterInsert } = args; const rows = await tx .select({ id: chatSession.id, updatedAt: chatSession.updatedAt }) .from(chatSession) .where( and( eq(chatSession.workspaceId, workspaceId), eq(chatSession.userId, userId), ), ) .all(); if (rows.length <= capAfterInsert) return; const sorted = rows.sort( (a, b) => a.updatedAt.getTime() - b.updatedAt.getTime(), ); const idsToDelete = sorted .slice(0, rows.length - capAfterInsert) .map((r) => r.id); await tx.delete(chatSession).where(inArray(chatSession.id, idsToDelete));}