Skip to content

Commit e17fe95

Browse files
committed
fix(xai): route grok build compactions via large context model
1 parent e77018a commit e17fe95

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

internal/runtime/executor/xai_executor.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ type xaiPreparedRequest struct {
480480

481481
func (e *XAIExecutor) prepareResponsesRequest(ctx context.Context, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, stream bool) (*xaiPreparedRequest, error) {
482482
baseModel := thinking.ParseSuffix(req.Model).ModelName
483+
baseModel = xaiCompactionModel(baseModel, opts)
483484
from := opts.SourceFormat
484485
to := sdktranslator.FromString("codex")
485486
originalPayloadSource := req.Payload
@@ -601,6 +602,43 @@ func xaiExecutionSessionID(req cliproxyexecutor.Request, opts cliproxyexecutor.O
601602
return ""
602603
}
603604

605+
func xaiCompactionModel(model string, opts cliproxyexecutor.Options) string {
606+
name := strings.ToLower(strings.TrimSpace(thinking.ParseSuffix(model).ModelName))
607+
if idx := strings.LastIndex(name, "/"); idx >= 0 {
608+
name = name[idx+1:]
609+
}
610+
if name != "grok-build-0.1" || !xaiIsCompactionRequest(opts) {
611+
return model
612+
}
613+
return "grok-4.3"
614+
}
615+
616+
func xaiIsCompactionRequest(opts cliproxyexecutor.Options) bool {
617+
if opts.Alt == "responses/compact" {
618+
return true
619+
}
620+
if opts.Headers != nil {
621+
if xaiTurnMetadataIsCompaction(opts.Headers.Get("X-Codex-Turn-Metadata")) {
622+
return true
623+
}
624+
}
625+
if raw, ok := opts.Metadata["X-Codex-Turn-Metadata"]; ok {
626+
return xaiTurnMetadataIsCompaction(fmt.Sprint(raw))
627+
}
628+
if raw, ok := opts.Metadata["x-codex-turn-metadata"]; ok {
629+
return xaiTurnMetadataIsCompaction(fmt.Sprint(raw))
630+
}
631+
return false
632+
}
633+
634+
func xaiTurnMetadataIsCompaction(raw string) bool {
635+
raw = strings.TrimSpace(raw)
636+
if raw == "" || !gjson.Valid(raw) {
637+
return false
638+
}
639+
return strings.TrimSpace(gjson.Get(raw, "request_kind").String()) == "compaction"
640+
}
641+
604642
func xaiImageEndpointPath(opts cliproxyexecutor.Options) string {
605643
if opts.SourceFormat.String() != xaiImageHandlerType {
606644
return ""

internal/runtime/executor/xai_executor_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,57 @@ func TestXAIExecutorOmitsUnsupportedReasoningEffort(t *testing.T) {
196196
}
197197
}
198198

199+
func TestXAIExecutorUsesLargeContextModelForGrokBuildCompaction(t *testing.T) {
200+
exec := NewXAIExecutor(&config.Config{})
201+
req := cliproxyexecutor.Request{
202+
Model: "grok-build-0.1",
203+
Payload: []byte(`{"model":"grok-build-0.1","input":"hello"}`),
204+
}
205+
206+
for _, opts := range []cliproxyexecutor.Options{
207+
{
208+
SourceFormat: sdktranslator.FormatOpenAIResponse,
209+
Alt: "responses/compact",
210+
},
211+
{
212+
SourceFormat: sdktranslator.FormatOpenAIResponse,
213+
Headers: http.Header{
214+
"X-Codex-Turn-Metadata": []string{`{"request_kind":"compaction","compaction":{"trigger":"auto","reason":"context_limit"}}`},
215+
},
216+
},
217+
} {
218+
prepared, err := exec.prepareResponsesRequest(context.Background(), req, opts, true)
219+
if err != nil {
220+
t.Fatalf("prepareResponsesRequest() error = %v", err)
221+
}
222+
if prepared.baseModel != "grok-4.3" {
223+
t.Fatalf("baseModel = %q, want grok-4.3", prepared.baseModel)
224+
}
225+
if got := gjson.GetBytes(prepared.body, "model").String(); got != "grok-4.3" {
226+
t.Fatalf("body model = %q, want grok-4.3; body=%s", got, string(prepared.body))
227+
}
228+
}
229+
}
230+
231+
func TestXAIExecutorKeepsGrokBuildModelForNormalRequests(t *testing.T) {
232+
exec := NewXAIExecutor(&config.Config{})
233+
prepared, err := exec.prepareResponsesRequest(context.Background(), cliproxyexecutor.Request{
234+
Model: "grok-build-0.1",
235+
Payload: []byte(`{"model":"grok-build-0.1","input":"hello"}`),
236+
}, cliproxyexecutor.Options{
237+
SourceFormat: sdktranslator.FormatOpenAIResponse,
238+
}, true)
239+
if err != nil {
240+
t.Fatalf("prepareResponsesRequest() error = %v", err)
241+
}
242+
if prepared.baseModel != "grok-build-0.1" {
243+
t.Fatalf("baseModel = %q, want grok-build-0.1", prepared.baseModel)
244+
}
245+
if got := gjson.GetBytes(prepared.body, "model").String(); got != "grok-build-0.1" {
246+
t.Fatalf("body model = %q, want grok-build-0.1; body=%s", got, string(prepared.body))
247+
}
248+
}
249+
199250
func TestXAIExecutorAppliesThinkingSuffix(t *testing.T) {
200251
var gotBody []byte
201252
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)