@@ -17,6 +17,13 @@ export interface CodexOptions {
1717 apiKey ?: string ;
1818 model ?: string ;
1919 reasoningEffort ?: string ;
20+ /**
21+ * Static HTTP headers forwarded on every request to the PostHog gateway
22+ * (the codex equivalent of Claude's `ANTHROPIC_CUSTOM_HEADERS`). Carries the
23+ * `x-posthog-property-*` attribution headers the gateway lifts onto the
24+ * `$ai_generation` event (team_id, ai_stage, task metadata).
25+ */
26+ httpHeaders ?: Record < string , string > ;
2027 /** Guidance appended on top of Codex's base prompt via `developer_instructions`. */
2128 developerInstructions ?: string ;
2229 /**
@@ -50,6 +57,12 @@ export interface CodexAppServerProcessOptions {
5057 codexHome ?: string ;
5158 /** Guidance appended to Codex's base prompt via `developer_instructions`. */
5259 developerInstructions ?: string ;
60+ /**
61+ * Static HTTP headers forwarded on every request to the PostHog gateway, set
62+ * as `model_providers.posthog.http_headers`. Codex equivalent of Claude's
63+ * `ANTHROPIC_CUSTOM_HEADERS` (see {@link CodexOptions.httpHeaders}).
64+ */
65+ httpHeaders ?: Record < string , string > ;
5366 /** Extra codex `-c key=value` config overrides (e.g. auto_compact_token_limit). */
5467 configOverrides ?: Record < string , string | number > ;
5568 logger ?: Logger ;
@@ -63,6 +76,19 @@ export interface CodexAppServerProcess {
6376 kill : ( ) => void ;
6477}
6578
79+ /** Serialize a string map as a TOML basic string (escapes `\` and `"`). */
80+ function tomlBasicString ( value : string ) : string {
81+ return `"${ value . replace ( / \\ / g, "\\\\" ) . replace ( / " / g, '\\"' ) } "` ;
82+ }
83+
84+ /** Render a `Record<string, string>` as a TOML inline table. */
85+ function tomlInlineTable ( entries : Record < string , string > ) : string {
86+ const pairs = Object . entries ( entries ) . map (
87+ ( [ key , value ] ) => `${ tomlBasicString ( key ) } = ${ tomlBasicString ( value ) } ` ,
88+ ) ;
89+ return `{ ${ pairs . join ( ", " ) } }` ;
90+ }
91+
6692export function buildAppServerArgs (
6793 options : CodexAppServerProcessOptions ,
6894) : string [ ] {
@@ -116,6 +142,17 @@ export function buildAppServerArgs(
116142 "-c" ,
117143 `model_providers.posthog.env_key="POSTHOG_GATEWAY_API_KEY"` ,
118144 ) ;
145+
146+ // Attribution + task-metadata headers the gateway lifts onto the captured
147+ // $ai_generation event. Passed as a single TOML inline table so hyphenated
148+ // header names (`x-posthog-property-*`) stay quoted rather than becoming
149+ // bare-key segments of a dotted `-c` path.
150+ if ( options . httpHeaders && Object . keys ( options . httpHeaders ) . length > 0 ) {
151+ args . push (
152+ "-c" ,
153+ `model_providers.posthog.http_headers=${ tomlInlineTable ( options . httpHeaders ) } ` ,
154+ ) ;
155+ }
119156 }
120157
121158 // developer_instructions are set per-thread in thread/start (with the host's
0 commit comments