From 8372298fba3f9603981abe5ac0e1da206fd4d321 Mon Sep 17 00:00:00 2001 From: viettranx Date: Tue, 31 Mar 2026 14:35:57 +0700 Subject: [PATCH] fix(providers): xAI generation models bypass chat verification grok-imagine-video and grok-2-image were not in the isNonChatModel() allowlist, causing verify to call Chat API which fails with malformed error. Also fix friendlyVerifyError() fallback splitting inside JSON values via LastIndex. --- internal/http/provider_verify.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/internal/http/provider_verify.go b/internal/http/provider_verify.go index 97385a7e..5ffc03a0 100644 --- a/internal/http/provider_verify.go +++ b/internal/http/provider_verify.go @@ -166,6 +166,8 @@ func isNonChatModel(model string) bool { "veo-", "google/veo-", "dall-e-", "imagen-", "google/imagen-", "gemini-2.5-flash-image", "google/gemini-2.5-flash-image", + "grok-imagine", // xAI video generation (grok-imagine-video) + "grok-2-image", // xAI image generation } m := strings.ToLower(model) for _, prefix := range nonChatPrefixes { @@ -209,14 +211,24 @@ func friendlyVerifyError(err error) string { } } - // Fallback: strip "HTTP NNN: provider: " prefix for cleaner display - if idx := strings.LastIndex(msg, ": "); idx >= 0 && idx < len(msg)-2 { + // Fallback: strip "HTTP NNN: provider: " prefix for cleaner display. + // Use the FIRST ": " after "HTTP NNN" to avoid splitting inside JSON values. + if idx := strings.Index(msg, ": "); idx >= 0 && idx < len(msg)-2 { suffix := msg[idx+2:] - // If the remainder still looks like JSON, just say "invalid model" + // Skip one more ": " to strip "provider: " prefix (e.g. "xai: {...}") + if idx2 := strings.Index(suffix, ": "); idx2 >= 0 && idx2 < 30 { + suffix = suffix[idx2+2:] + } + // If the remainder looks like JSON, say "invalid model" if strings.HasPrefix(suffix, "{") { return "Model not recognized by provider" } - return suffix + // Strip leaked JSON quotes/braces from partial extraction + suffix = strings.TrimRight(suffix, "{}[]") + suffix = strings.Trim(suffix, `"`) + if suffix != "" { + return suffix + } } return msg