{"version":3,"file":"volatile-message.mjs","names":[],"sources":["../../src/compaction/volatile-message.ts"],"sourcesContent":["import { existsSync, realpathSync } from 'node:fs';\nimport { dirname, join } from 'node:path';\nimport { pathToFileURL } from 'node:url';\n\nexport const LIVE_COMPACTION_STREAM_CUSTOM_TYPE = 'live-compaction-stream';\n\nconst PATCH_FLAG = Symbol.for('pi-live-compaction.volatile-message-patch');\n\ntype CustomMessageInput = {\n\tcustomType: string;\n\tcontent: unknown;\n\tdisplay: boolean;\n\tdetails?: unknown;\n};\n\ntype AppCustomMessage = CustomMessageInput & {\n\trole: 'custom';\n\ttimestamp: number;\n};\n\ntype SendCustomMessage = (\n\tthis: unknown,\n\tmessage: CustomMessageInput,\n\toptions?: unknown,\n) => Promise<void>;\n\ntype EmitCustomMessage = (event: {\n\ttype: 'message_start' | 'message_end';\n\tmessage: AppCustomMessage;\n}) => void;\n\ntype PatchableAgentSession = {\n\tsendCustomMessage?: SendCustomMessage;\n\t_emit?: EmitCustomMessage;\n} & Record<symbol, unknown>;\n\ntype CodingAgentRuntime = {\n\tAgentSession?: { prototype?: PatchableAgentSession };\n};\n\nfunction createAppMessage(message: CustomMessageInput): AppCustomMessage {\n\treturn {\n\t\trole: 'custom',\n\t\tcustomType: message.customType,\n\t\tcontent: message.content,\n\t\tdisplay: message.display,\n\t\tdetails: message.details,\n\t\ttimestamp: Date.now(),\n\t};\n}\n\nfunction resolveRuntimeCodingAgentIndex(): string {\n\tconst entrypoint = process.argv[1];\n\tif (!entrypoint) {\n\t\tthrow new Error('Cannot resolve Pi runtime: process.argv[1] is unavailable');\n\t}\n\n\tconst resolvedEntrypoint = realpathSync(entrypoint);\n\tconst indexPath = join(dirname(resolvedEntrypoint), 'index.js');\n\tif (!existsSync(indexPath)) {\n\t\tthrow new Error(`Cannot resolve Pi runtime index from ${resolvedEntrypoint}`);\n\t}\n\n\treturn indexPath;\n}\n\nasync function importRuntimeCodingAgent(): Promise<CodingAgentRuntime> {\n\treturn (await import(pathToFileURL(resolveRuntimeCodingAgentIndex()).href)) as CodingAgentRuntime;\n}\n\nexport function installVolatileCompactionMessagePatchFromRuntime(\n\truntime: CodingAgentRuntime,\n): void {\n\tconst prototype = runtime.AgentSession?.prototype;\n\tif (!prototype) {\n\t\tthrow new Error('Runtime AgentSession prototype is unavailable');\n\t}\n\tif (prototype[PATCH_FLAG] === true) return;\n\n\tconst original = prototype.sendCustomMessage;\n\tif (typeof original !== 'function') {\n\t\tthrow new Error('Runtime AgentSession.sendCustomMessage is unavailable');\n\t}\n\n\tprototype.sendCustomMessage = async function sendVolatileCustomMessage(\n\t\tthis: unknown,\n\t\tmessage: CustomMessageInput,\n\t\toptions?: unknown,\n\t): Promise<void> {\n\t\tif (message.customType !== LIVE_COMPACTION_STREAM_CUSTOM_TYPE) {\n\t\t\treturn original.call(this, message, options);\n\t\t}\n\n\t\tconst session = this as PatchableAgentSession;\n\t\tif (typeof session._emit !== 'function') {\n\t\t\treturn original.call(this, message, options);\n\t\t}\n\n\t\tconst appMessage = createAppMessage(message);\n\t\tsession._emit({ type: 'message_start', message: appMessage });\n\t\tsession._emit({ type: 'message_end', message: appMessage });\n\t};\n\tprototype[PATCH_FLAG] = true;\n}\n\nexport async function installVolatileCompactionMessagePatch(): Promise<void> {\n\tinstallVolatileCompactionMessagePatchFromRuntime(await importRuntimeCodingAgent());\n}\n"],"mappings":";;;;AAIA,MAAa,qCAAqC;AAElD,MAAM,aAAa,OAAO,IAAI,2CAA2C;AAkCzE,SAAS,iBAAiB,SAA+C;CACxE,OAAO;EACN,MAAM;EACN,YAAY,QAAQ;EACpB,SAAS,QAAQ;EACjB,SAAS,QAAQ;EACjB,SAAS,QAAQ;EACjB,WAAW,KAAK,IAAI;CACrB;AACD;AAEA,SAAS,iCAAyC;CACjD,MAAM,aAAa,QAAQ,KAAK;CAChC,IAAI,CAAC,YACJ,MAAM,IAAI,MAAM,2DAA2D;CAG5E,MAAM,qBAAqB,aAAa,UAAU;CAClD,MAAM,YAAY,KAAK,Q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