1+ import { describe , expect , test } from "bun:test" ;
2+ import {
3+ createOpenAIChatAdapter as createOpenAIChatAdapterProduction ,
4+ } from "../src/adapters/openai-chat" ;
5+ import type {
6+ OcxParsedRequest ,
7+ OcxTool ,
8+ } from "../src/types" ;
9+ import { withTestTranslatorBudget } from "./helpers/translator-budget" ;
10+
11+ const createOpenAIChatAdapter = (
12+ ...args : Parameters < typeof createOpenAIChatAdapterProduction >
13+ ) =>
14+ withTestTranslatorBudget (
15+ createOpenAIChatAdapterProduction ( ...args ) ,
16+ ) ;
17+
18+ function parsedRequest (
19+ tool : OcxTool ,
20+ ) : OcxParsedRequest {
21+ return {
22+ modelId : "grok-4.6" ,
23+ context : {
24+ messages : [
25+ {
26+ role : "user" ,
27+ content : "run the command" ,
28+ timestamp : 0 ,
29+ } ,
30+ ] ,
31+ tools : [ tool ] ,
32+ } ,
33+ stream : true ,
34+ options : { } ,
35+ } ;
36+ }
37+
38+ function xaiAdapter ( ) {
39+ return createOpenAIChatAdapter ( {
40+ adapter : "openai-chat" ,
41+ baseUrl : "https://cli-chat-proxy.grok.com/v1" ,
42+ apiKey : "k" ,
43+ } ) ;
44+ }
45+
46+ describe ( "xAI Grok CLI tool schema normalization" , ( ) => {
47+ test ( "keeps Claude Code tools with a root $schema" , async ( ) => {
48+ const tool : OcxTool = {
49+ name : "Bash" ,
50+ description : "Execute a shell command" ,
51+ parameters : {
52+ $schema : "https://json-schema.org/draft/2020-12/schema" ,
53+ type : "object" ,
54+ properties : {
55+ command : {
56+ type : "string" ,
57+ } ,
58+ } ,
59+ required : [ "command" ] ,
60+ additionalProperties : false ,
61+ } ,
62+ } ;
63+
64+ const request = await xaiAdapter ( ) . buildRequest (
65+ parsedRequest ( tool ) ,
66+ ) ;
67+
68+ const body = JSON . parse ( request . body ) as {
69+ tools ?: Array < {
70+ type : string ;
71+ function : {
72+ name : string ;
73+ parameters : Record < string , unknown > ;
74+ } ;
75+ } > ;
76+ } ;
77+
78+ expect ( body . tools ) . toHaveLength ( 1 ) ;
79+ expect ( body . tools ?. [ 0 ] ?. function . name ) . toBe ( "Bash" ) ;
80+ expect ( body . tools ?. [ 0 ] ?. function . parameters ) . toEqual ( {
81+ type : "object" ,
82+ properties : {
83+ command : {
84+ type : "string" ,
85+ } ,
86+ } ,
87+ required : [ "command" ] ,
88+ additionalProperties : false ,
89+ } ) ;
90+ expect (
91+ body . tools ?. [ 0 ] ?. function . parameters ,
92+ ) . not . toHaveProperty ( "$schema" ) ;
93+ } ) ;
94+
95+ test ( "does not reintroduce $schema when flattening a root union" , async ( ) => {
96+ const tool : OcxTool = {
97+ name : "Bash" ,
98+ description : "Execute a shell command" ,
99+ parameters : {
100+ $schema : "https://json-schema.org/draft/2020-12/schema" ,
101+ oneOf : [
102+ {
103+ type : "object" ,
104+ properties : {
105+ command : {
106+ type : "string" ,
107+ } ,
108+ } ,
109+ required : [ "command" ] ,
110+ additionalProperties : false ,
111+ } ,
112+ {
113+ type : "object" ,
114+ properties : {
115+ command : {
116+ type : "string" ,
117+ minLength : 1 ,
118+ } ,
119+ } ,
120+ required : [ "command" ] ,
121+ additionalProperties : false ,
122+ } ,
123+ ] ,
124+ } ,
125+ } ;
126+
127+ const request = await xaiAdapter ( ) . buildRequest (
128+ parsedRequest ( tool ) ,
129+ ) ;
130+
131+ const body = JSON . parse ( request . body ) as {
132+ tools ?: Array < {
133+ function : {
134+ parameters : Record < string , unknown > ;
135+ } ;
136+ } > ;
137+ } ;
138+
139+ expect ( body . tools ) . toHaveLength ( 1 ) ;
140+ expect ( body . tools ?. [ 0 ] ?. function . parameters ) . toEqual ( {
141+ type : "object" ,
142+ properties : {
143+ command : {
144+ anyOf : [
145+ { type : "string" } ,
146+ { type : "string" , minLength : 1 } ,
147+ ] ,
148+ } ,
149+ } ,
150+ required : [ "command" ] ,
151+ additionalProperties : false ,
152+ } ) ;
153+ } ) ;
154+ } ) ;
0 commit comments