import { z } from "zod"; import { defineAction } from "../../../action.js"; import { getRequestOrgId, getRequestUserEmail, } from "../../../server/request-context.js"; import { callerOwnsThread } from "../../run-ownership.js"; import { readContextManifest, upsertContextDirective, writeContextManifestStatus, } from "../directives-store.js"; import { contextXrayAuthError, contextXraySystemSegmentError, contextXrayThreadNotFoundError, isContextXraySystemSegment, } from "./errors.js"; export default defineAction({ description: "Pin a Context X-Ray segment so it is preserved through future context compaction.", schema: z.object({ threadId: z.string(), segmentId: z.string(), }), run: async (args) => { const ownerEmail = getRequestUserEmail(); if (!ownerEmail) throw contextXrayAuthError(); if (!(await callerOwnsThread(ownerEmail, args.threadId))) { throw contextXrayThreadNotFoundError(); } if ( isContextXraySystemSegment( await readContextManifest(args.threadId), args.segmentId, ) ) { throw contextXraySystemSegmentError(); } const directive = await upsertContextDirective({ threadId: args.threadId, segmentId: args.segmentId, action: "pin", ownerEmail, orgId: getRequestOrgId() ?? null, }); const manifest = await writeContextManifestStatus({ threadId: args.threadId, segmentId: args.segmentId, status: "pinned", }); return { directive, manifest }; }, });