11import type { CodexAccount , OcxConfig } from "../types" ;
22import { COMBO_NAMESPACE } from "../combos/types" ;
33import { OPENAI_CODEX_PROVIDER_ID } from "../providers/openai-tiers" ;
4+ import {
5+ POLICY_NAMESPACE ,
6+ routingProfileAliasNamespacePrefixes ,
7+ } from "../routing/profile-namespace" ;
48import {
59 CODEX_ACCOUNT_LOG_LABEL_RE ,
610 createCodexAccountLogLabel ,
@@ -21,6 +25,7 @@ const RESERVED_NAMESPACE_KEYS = new Set([
2125 "constructor" ,
2226 COMBO_NAMESPACE ,
2327 OPENAI_CODEX_PROVIDER_ID ,
28+ POLICY_NAMESPACE ,
2429] . map ( codexProviderNamespaceKey ) ) ;
2530const PUBLIC_ACCOUNT_SELECTOR_MAX_ATTEMPTS = 16 ;
2631
@@ -68,17 +73,21 @@ function claimNamespace(requested: string, used: Set<string>): string {
6873 return namespace ;
6974}
7075
71- function occupiedNamespaces ( config : Pick < OcxConfig , "combos" | "providers" > ) : Set < string > {
76+ /** Collect every public namespace that a generated account selector must not claim. */
77+ function occupiedNamespaces (
78+ config : Pick < OcxConfig , "combos" | "providers" | "routingProfiles" > ,
79+ ) : Set < string > {
7280 return new Set ( [
7381 ...Object . keys ( config . providers ) . map ( codexProviderNamespaceKey ) ,
7482 ...comboAliasNamespaces ( config ) ,
83+ ...routingProfileAliasNamespacePrefixes ( config ) ,
7584 ...RESERVED_NAMESPACE_KEYS ,
7685 ] ) ;
7786}
7887
7988/** Build an initial account-selector map without deriving public selectors from aliases or ids. */
8089export function defaultCodexAccountNamespaces (
81- config : Pick < OcxConfig , "codexAccounts" | "combos" | "providers" > ,
90+ config : Pick < OcxConfig , "codexAccounts" | "combos" | "providers" | "routingProfiles" > ,
8291) : Record < string , string > {
8392 const namespaces : Record < string , string > = { } ;
8493 const used = occupiedNamespaces ( config ) ;
@@ -99,13 +108,39 @@ export function defaultCodexAccountNamespaces(
99108 return namespaces ;
100109}
101110
111+ /**
112+ * Initialize generated selectors only for an explicit opt-in with no existing bindings.
113+ * A true result means the map was replaced and the caller must persist the updated config.
114+ */
115+ export function initializeDefaultCodexAccountNamespaces (
116+ config : Pick <
117+ OcxConfig ,
118+ | "codexAccountPickerEnabled"
119+ | "codexAccountNamespaces"
120+ | "codexAccounts"
121+ | "combos"
122+ | "providers"
123+ | "routingProfiles"
124+ > ,
125+ ) : boolean {
126+ if ( config . codexAccountPickerEnabled !== true
127+ || Object . keys ( config . codexAccountNamespaces ?? { } ) . length > 0 ) return false ;
128+
129+ const namespaces = defaultCodexAccountNamespaces ( config ) ;
130+ config . codexAccountNamespaces = namespaces ;
131+ return true ;
132+ }
133+
102134/**
103135 * Add one account to a generated map without renaming or replacing explicit existing entries.
104136 * The account-creation layer must reject a new id that already equals an existing selector key.
105137 * A true result means the map was mutated in place; callers must persist the updated config.
106138 */
107139export function appendDefaultCodexAccountNamespace (
108- config : Pick < OcxConfig , "codexAccountNamespaces" | "codexAccounts" | "combos" | "providers" > ,
140+ config : Pick <
141+ OcxConfig ,
142+ "codexAccountNamespaces" | "codexAccounts" | "combos" | "providers" | "routingProfiles"
143+ > ,
109144 account : Pick < CodexAccount , "id" | "isMain" | "logLabel" > ,
110145) : boolean {
111146 const namespaces = config . codexAccountNamespaces ;
0 commit comments