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 ยท 73 lines
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374import { eq } from "@openstatus/db";import { type ChatSessionRow, type ChatStoredMessage, chatSession,} from "@openstatus/db/src/schema";
import { type ServiceContext, tryGetActorUserId, withTransaction,} from "../context";import { UnauthorizedError } from "../errors";import { compactMessages, getChatSessionInWorkspace } from "./internal";import { SetChatSessionMessagesInput } from "./schemas";
/** * Replace the chat session's full message list. Idempotent across * onFinish retries โ HITL turns can fire onFinish multiple times, and * full-replace lets each fire be the canonical snapshot. Existing-row * `createdAt` wins on overlap so older messages don't drift forward. */// Per-user UI state, not workspace configuration: only user actors get past// `tryGetActorUserId`, so `requireScope` could never fire, and the audit action// union has no `chat_session` verb.// oxlint-disable-next-line openstatus/services-mutation-guardsexport async function setChatSessionMessages(args: { ctx: ServiceContext; input: SetChatSessionMessagesInput;}): Promise<ChatSessionRow> { const { ctx } = args; const input = SetChatSessionMessagesInput.parse(args.input);
const userId = tryGetActorUserId(ctx.actor); if (userId == null) { throw new UnauthorizedError( "Chat sessions must be updated by a known user actor.", ); }
return withTransaction(ctx, async (tx) => { const session = await getChatSessionInWorkspace({ tx, sessionId: input.sessionId, workspaceId: ctx.workspace.id, userId, });
const stamped = mergeTimestamps(session.messages, input.messages); const compacted = compactMessages(stamped);
const updated = await tx .update(chatSession) .set({ messages: compacted, updatedAt: new Date() }) .where(eq(chatSession.id, session.id)) .returning() .get();
return updated; });}
function mergeTimestamps( existing: ChatStoredMessage[], incoming: ChatStoredMessage[],): ChatStoredMessage[] { const existingStamps = new Map<string, number>(); for (const m of existing) existingStamps.set(m.id, m.createdAt); return incoming.map((m) => { const prior = existingStamps.get(m.id); return prior !== undefined ? { ...m, createdAt: prior } : m; });}