11// Openai tests cover openai provider plugin behavior.
2+ import fs from "node:fs" ;
23import type { StreamFn } from "openclaw/plugin-sdk/agent-core" ;
34import type { Context , Model , SimpleStreamOptions } from "openclaw/plugin-sdk/llm" ;
45import {
@@ -165,6 +166,19 @@ vi.mock("openclaw/plugin-sdk/provider-stream-family", async (importOriginal) =>
165166 } ;
166167} ) ;
167168
169+ const OPENAI_CODEX_MODELS_URL = `${ OPENAI_CODEX_RESPONSES_BASE_URL } /models?client_version=${ readPinnedCodexClientVersion ( ) } ` ;
170+
171+ function readPinnedCodexClientVersion ( ) : string {
172+ const packageJson = JSON . parse (
173+ fs . readFileSync ( new URL ( "../codex/package.json" , import . meta. url ) , "utf8" ) ,
174+ ) as { dependencies ?: Record < string , unknown > } ;
175+ const version = packageJson . dependencies ?. [ "@openai/codex" ] ;
176+ if ( typeof version !== "string" ) {
177+ throw new Error ( "expected an exact @openai/codex dependency" ) ;
178+ }
179+ return version ;
180+ }
181+
168182function runWrappedPayloadCase ( params : {
169183 wrap : NonNullable < ReturnType < typeof buildOpenAIProvider > [ "wrapStreamFn" ] > ;
170184 provider : string ;
@@ -676,9 +690,7 @@ describe("buildOpenAIProvider", () => {
676690 maxTokens : 64_000 ,
677691 } ) ;
678692 expect ( fetchSpy ) . toHaveBeenCalledOnce ( ) ;
679- expect ( fetchSpy . mock . calls [ 0 ] ?. [ 0 ] ) . toBe (
680- "https://chatgpt.com/backend-api/codex/models?client_version=1.0.0" ,
681- ) ;
693+ expect ( fetchSpy . mock . calls [ 0 ] ?. [ 0 ] ) . toBe ( OPENAI_CODEX_MODELS_URL ) ;
682694 const headers = fetchSpy . mock . calls [ 0 ] ?. [ 1 ] ?. headers ;
683695 expect ( headers ) . toBeInstanceOf ( Headers ) ;
684696 if ( ! ( headers instanceof Headers ) ) {
@@ -691,6 +703,25 @@ describe("buildOpenAIProvider", () => {
691703 }
692704 } ) ;
693705
706+ it ( "uses the managed Codex package version for OAuth model discovery" , async ( ) => {
707+ const pinnedVersion = readPinnedCodexClientVersion ( ) ;
708+ const fetchGuard : LiveModelCatalogFetchGuard = vi . fn ( async ( params ) => ( {
709+ response : Response . json ( { models : [ { slug : "gpt-5.5" , visibility : "list" } ] } ) ,
710+ finalUrl : params . url ,
711+ release : async ( ) => undefined ,
712+ } ) ) ;
713+
714+ await buildOpenAICodexLiveProviderConfig ( {
715+ discoveryApiKey : "placeholder" ,
716+ fetchGuard,
717+ } ) ;
718+
719+ const requestUrl = vi . mocked ( fetchGuard ) . mock . calls [ 0 ] ?. [ 0 ] . url ;
720+ expect ( new URL ( requestUrl ?? "https://placeholder" ) . searchParams . get ( "client_version" ) ) . toBe (
721+ pinnedVersion ,
722+ ) ;
723+ } ) ;
724+
694725 it ( "uses runtime OAuth profiles when catalog auth resolution is empty" , async ( ) => {
695726 mocks . resolveApiKeyForProvider . mockResolvedValue ( {
696727 mode : "oauth" ,
@@ -797,9 +828,7 @@ describe("buildOpenAIProvider", () => {
797828 maxTokens : 128_000 ,
798829 } ) ;
799830 const fetchParams = vi . mocked ( fetchGuard ) . mock . calls [ 0 ] ?. [ 0 ] ;
800- expect ( fetchParams ?. url ) . toBe (
801- "https://chatgpt.com/backend-api/codex/models?client_version=1.0.0" ,
802- ) ;
831+ expect ( fetchParams ?. url ) . toBe ( OPENAI_CODEX_MODELS_URL ) ;
803832 const init = fetchParams ?. init ;
804833 const headers = init ?. headers ;
805834 expect ( headers ) . toBeInstanceOf ( Headers ) ;
@@ -876,52 +905,9 @@ describe("buildOpenAIProvider", () => {
876905 supportedReasoningEfforts : [ "low" , "medium" , "high" , "xhigh" , "max" , "ultra" ] ,
877906 } ,
878907 } ) ;
879- expect ( provider . models . find ( ( model ) => model . id === "gpt-5.6-terra" ) ) . toMatchObject ( {
880- contextWindow : 372_000 ,
881- contextTokens : 372_000 ,
882- compat : {
883- supportedReasoningEfforts : [ "low" , "medium" , "high" , "xhigh" , "max" , "ultra" ] ,
884- } ,
885- } ) ;
886- expect ( provider . models . find ( ( model ) => model . id === "gpt-5.6-luna" ) ) . toMatchObject ( {
887- contextWindow : 372_000 ,
888- contextTokens : 372_000 ,
889- compat : { supportedReasoningEfforts : [ "low" , "medium" , "high" , "xhigh" , "max" ] } ,
890- } ) ;
908+ expect ( provider . models . map ( ( model ) => model . id ) ) . not . toContain ( "gpt-5.6-terra" ) ;
909+ expect ( provider . models . map ( ( model ) => model . id ) ) . not . toContain ( "gpt-5.6-luna" ) ;
891910 expect ( provider . models . map ( ( model ) => model . id ) ) . toContain ( "gpt-5.5" ) ;
892- const gpt56Models = Object . fromEntries (
893- provider . models
894- . filter ( ( model ) => model . id . startsWith ( "gpt-5.6-" ) )
895- . map ( ( model ) => [ model . id , model ] ) ,
896- ) ;
897- for ( const modelId of [ "gpt-5.6-sol" , "gpt-5.6-terra" ] ) {
898- const model = gpt56Models [ modelId ] ;
899- expect ( model ?. compat ?. supportedReasoningEfforts ) . toContain ( "ultra" ) ;
900- expect (
901- buildOpenAIProvider ( )
902- . resolveThinkingProfile ?.( {
903- provider : "openai" ,
904- modelId,
905- agentRuntime : "codex" ,
906- api : "openai-chatgpt-responses" ,
907- compat : model ?. compat ,
908- } as never )
909- ?. levels . map ( ( level ) => level . id ) ,
910- ) . toContain ( "ultra" ) ;
911- }
912- const luna = gpt56Models [ "gpt-5.6-luna" ] ;
913- expect ( luna ?. compat ?. supportedReasoningEfforts ) . not . toContain ( "ultra" ) ;
914- const lunaLevels = buildOpenAIProvider ( )
915- . resolveThinkingProfile ?.( {
916- provider : "openai" ,
917- modelId : "gpt-5.6-luna" ,
918- agentRuntime : "codex" ,
919- api : "openai-chatgpt-responses" ,
920- compat : luna ?. compat ,
921- } as never )
922- ?. levels . map ( ( level ) => level . id ) ;
923- expect ( lunaLevels ) . toContain ( "max" ) ;
924- expect ( lunaLevels ) . not . toContain ( "ultra" ) ;
925911 expect ( release ) . toHaveBeenCalledOnce ( ) ;
926912 } ) ;
927913
0 commit comments