1+ /**
2+ * Persists and normalizes the Codex app-server thread binding associated with
3+ * an OpenClaw session file.
4+ */
15import fs from "node:fs/promises" ;
26import { embeddedAgentLog } from "openclaw/plugin-sdk/agent-harness-runtime" ;
37import {
@@ -21,13 +25,15 @@ const PUBLIC_OPENAI_MODEL_PROVIDER = "openai";
2125type ProviderAuthAliasLookupParams = Parameters < typeof resolveProviderIdForAuth > [ 1 ] ;
2226type ProviderAuthAliasConfig = NonNullable < ProviderAuthAliasLookupParams > [ "config" ] ;
2327
28+ /** Inputs needed to resolve whether a binding's auth profile is native Codex/OpenAI auth. */
2429export type CodexAppServerAuthProfileLookup = {
2530 authProfileId ?: string ;
2631 authProfileStore ?: AuthProfileStore ;
2732 agentDir ?: string ;
2833 config ?: ProviderAuthAliasConfig ;
2934} ;
3035
36+ /** Durable sidecar binding connecting an OpenClaw session file to a Codex thread. */
3137export type CodexAppServerThreadBinding = {
3238 schemaVersion : 1 ;
3339 threadId : string ;
@@ -53,24 +59,28 @@ export type CodexAppServerThreadBinding = {
5359 updatedAt : string ;
5460} ;
5561
62+ /** Context-engine state persisted with a Codex app-server thread binding. */
5663export type CodexAppServerContextEngineBinding = {
5764 schemaVersion : 1 ;
5865 engineId : string ;
5966 policyFingerprint : string ;
6067 projection ?: CodexAppServerContextEngineProjectionBinding ;
6168} ;
6269
70+ /** Context-engine projection metadata used to guard resumed native threads. */
6371export type CodexAppServerContextEngineProjectionBinding = {
6472 schemaVersion : 1 ;
6573 mode : "thread_bootstrap" ;
6674 epoch : string ;
6775 fingerprint ?: string ;
6876} ;
6977
78+ /** Returns the JSON sidecar path for the Codex app-server binding beside a session file. */
7079export function resolveCodexAppServerBindingPath ( sessionFile : string ) : string {
7180 return `${ sessionFile } .codex-app-server.json` ;
7281}
7382
83+ /** Reads and normalizes a Codex app-server binding sidecar, returning undefined on stale data. */
7484export async function readCodexAppServerBinding (
7585 sessionFile : string ,
7686 lookup : Omit < CodexAppServerAuthProfileLookup , "authProfileId" > = { } ,
@@ -148,6 +158,7 @@ export async function readCodexAppServerBinding(
148158 }
149159}
150160
161+ /** Writes the Codex app-server binding sidecar with normalized provider/auth metadata. */
151162export async function writeCodexAppServerBinding (
152163 sessionFile : string ,
153164 binding : Omit <
@@ -293,6 +304,7 @@ function readPluginAppPolicyContext(value: unknown): PluginAppPolicyContext | un
293304 } ;
294305}
295306
307+ /** Removes the Codex app-server binding sidecar if present. */
296308export async function clearCodexAppServerBinding (
297309 sessionFile : string ,
298310 _lookup : Omit < CodexAppServerAuthProfileLookup , "authProfileId" > = { } ,
@@ -306,6 +318,7 @@ export async function clearCodexAppServerBinding(
306318 }
307319}
308320
321+ /** Clears a binding only when it still points at the expected Codex thread id. */
309322export async function clearCodexAppServerBindingForThread (
310323 sessionFile : string ,
311324 threadId : string ,
@@ -331,6 +344,7 @@ function isNotFound(error: unknown): boolean {
331344 return Boolean ( error && typeof error === "object" && "code" in error && error . code === "ENOENT" ) ;
332345}
333346
347+ /** Returns true when an auth profile uses native Codex/OpenAI app-server auth. */
334348export function isCodexAppServerNativeAuthProfile (
335349 lookup : CodexAppServerAuthProfileLookup ,
336350) : boolean {
@@ -356,6 +370,7 @@ export function isCodexAppServerNativeAuthProfile(
356370 }
357371}
358372
373+ /** Hides redundant OpenAI provider attribution for native Codex auth bindings. */
359374export function normalizeCodexAppServerBindingModelProvider ( params : {
360375 authProfileId ?: string ;
361376 modelProvider ?: string ;
0 commit comments