Content-Length: 401479 | pFad | http://github.com/opencode-ai/opencode/pull/307

DA feat: add xAI Grok integration with its current features by askiiRobotics · Pull Request #307 · opencode-ai/opencode · GitHub
Skip to content

feat: add xAI Grok integration with its current features #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

askiiRobotics
Copy link

Hey folks! Just a heads up - I mainly tested this with xAI/Grok since
that's what I wanted to try out, and it felt solid enough to use as a
foundation. Haven't thoroughly tested with other providers yet, so if you
notice any issues or regressions with OpenAI/Anthropic/etc., please feel
free to jump in and fix this PR! 🙏

  • Complete xAI provider with the current Grok models (2/3/4 series)
  • Deferred completion support for long-running requests
  • Concurrent request handling for improved performance
  • Web search tool integration for real-time information
  • Image generation and vision capabilities
  • Advanced reasoning support with model-specific validation
  • Test coverage for (almost) all new xAI capabilities

Configuration enhancements:

  • Runtime options support via CLI flags (--deferred, --deferred-timeout)
  • Provider-level deferred completion configuration
  • Auto-enable rules for smart feature activation
  • Model-specific reasoning effort validation (xAI: low/high only)
  • Enhanced agent configuration with override capabilities

Documentation and schema updates:

  • README updates with xAI model details
  • Updated configuration schema with new provider options
  • Updated CLI help text and examples

Alex Belets and others added 7 commits July 11, 2025 17:26
  - Complete xAI provider with 8 Grok models (2/3/4 series)
  - Deferred completion support for long-running requests
  - Concurrent request handling for improved performance
  - Web search tool integration for real-time information
  - Image generation and vision capabilities
  - Advanced reasoning support with model-specific validation

  Core provider features:
  - Full streaming support with proper error handling
  - Intelligent model selection and configuration
  - Robust caching and retry mechanisms
  - Provider-specific option handling (reasoning, deferred, concurrent)
  - Comprehensive test coverage for all capabilities

  Configuration enhancements:
  - Runtime options support via CLI flags (--deferred, --deferred-timeout)
  - Provider-level deferred completion configuration
  - Auto-enable rules for smart feature activation
  - Model-specific reasoning effort validation (xAI: low/high only)
  - Enhanced agent configuration with override capabilities

  Tool ecosystem expansion:
  - Web search tool for real-time information retrieval
  - Enhanced coder agent toolset with web capabilities
  - Improved tool validation and error handling
  - Better integration with existing LSP and file tools

  Documentation and schema updates:
  - Comprehensive README updates with xAI model details
  - Updated configuration schema with new provider options
  - Enhanced CLI help text and examples
  - Added feature capability documentation
.claude/settings.local.json - deleted
- Fix EventThinkingDelta to use event.Thinking field instead of event.Content
- Add ReasoningContent field to ProviderResponse for XAI reasoning models
- Fix tool schema double-wrapping issue causing "Invalid function schema" errors
- Add reasoning_effort validation: convert "medium" to "high" for Grok-3-mini models
- Add XAIHTTPClient for proper request handling with reasoning support
- Add ReasoningHandler to process reasoning content and emit thinking deltas
- Fix ls tool to handle uninitialized config in test environments
- Fix message rendering to properly display reasoning content
- Add content truncation for very long messages to prevent UI issues
- Fix platform-specific help key bindings (macOS vs Linux)
@Tycale
Copy link

Tycale commented Jul 11, 2025

Thanks for this PR, I wanted to try Grok4 with OpenCode but got this message:

failed to process events: POST "https://api.x.ai/v1/chat/completions": 400 Bad Request "Invalid request content: Invalid function schema."

Alex Belets added 2 commits July 12, 2025 01:12
  - Updated ShouldUseReasoning() to properly handle Grok 4's automatic reasoning
- Grok 4 now uses the reasoning handler path without requiring reasoning_effort parameter
- This ensures proper tool schema handling for Grok 4 models
@askiiRobotics
Copy link
Author

Thanks for this PR, I wanted to try Grok4 with OpenCode but got this message:

failed to process events: POST "https://api.x.ai/v1/chat/completions": 400 Bad Request "Invalid request content: Invalid function schema."

Dear Tycale,
Yes, you were right — that issue did exist. The code definitely needed some improvements, and the paths for models with thinking capabilities but no reasoning (e.g., the Grok-4 case) were incorrect.

This has now been fixed — thanks for pointing it out!

Heads-up: Still includes many untested features and plenty of room for improvement beyond basic chat. Core functionality is there, but it needs proper integration with UI and other components.

@Tycale
Copy link

Tycale commented Jul 11, 2025

Thanks, I will ASAP !

@Tycale
Copy link

Tycale commented Jul 11, 2025

hmm, it's kind of working, I got some responses but I have a lot of

failed to process events: reasoning request failed: failed to send request: Post "https://api.x.ai/v1/chat/completions": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

(Grok4)

@Tycale
Copy link

Tycale commented Jul 11, 2025

Just noticed that switching back to o3 will produce the following error :

│                                                                                                                                                                                                            │
│Message:                                                                                                                                                                                                    │
│  POST "https://api.openai.com/v1/chat/completions": 400 Bad Request {                                                                                                                                      │
│      "message": "Invalid schema for function 'web_search': ['query'] is not of type 'object', 'boolean'.",                                                                                                 │
│      "type": "invalid_request_error",                                                                                                                                                                      │
│      "param": "tools[11].function.parameters",                                                                                                                                                             │
│      "code": "invalid_function_parameters"                                                                                                                                                                 │
│    }

  - Implement FilterToolsByProvider to restrict tools based on provider
  capabilities
  - Add Providers field to ToolInfo struct for specifying tool availability
  - Integrate filtering across all provider clients (OpenAI, Gemini, xAI,
  Copilot)
  - Add comprehensive tests for tool filtering logic with case-insensitive
  matching
  - Update web search tool to be xAI-only as it requires live search
  capabilities
  - Clarify Grok 4 reasoning behavior in docs and comments (internal
  reasoning only)
@askiiRobotics
Copy link
Author

askiiRobotics commented Jul 11, 2025

hmm, it's kind of working, I got some responses but I have a lot of

failed to process events: reasoning request failed: failed to send request: Post "https://api.x.ai/v1/chat/completions": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

(Grok4)

Just noticed that switching back to o3 will produce the following error :

│                                                                                                                                                                                                            │
│Message:                                                                                                                                                                                                    │
│  POST "https://api.openai.com/v1/chat/completions": 400 Bad Request {                                                                                                                                      │
│      "message": "Invalid schema for function 'web_search': ['query'] is not of type 'object', 'boolean'.",                                                                                                 │
│      "type": "invalid_request_error",                                                                                                                                                                      │
│      "param": "tools[11].function.parameters",                                                                                                                                                             │
│      "code": "invalid_function_parameters"                                                                                                                                                                 │
│    }

Thanks for pointing this out — you're right on both counts.

  • The Grok-4 issue (context deadline exceeded) was related to how reasoning was being handled for models with internal-only reasoning capabilities. This has now been addressed.
  • The OpenAI error stemmed from tool schema incompatibility — particularly with the web_search tool being assigned to models that don’t support it.

I’ve just pushed a commit that should resolve both: feat: Add provider-based tool filtering for model compatibility

This should prevent the mismatches you were seeing and improve fallback behavior.

One caveat: I still have concerns about pricing accuracy for xAI — this will need another pass to verify.

Appreciate the testing and feedback — feel free to flag anything else that comes up!

@Tycale
Copy link

Tycale commented Jul 11, 2025

➜  opencode git:(askiiRobotics/main) go build -o opencode                                                                                                                                       
# github.com/opencode-ai/opencode/internal/llm/tools
internal/llm/tools/web_search.go:147:3: unknown field Providers in struct literal of type ToolInfo

Can be fixed with

➜  opencode git:(askiiRobotics/main) ✗ git diff                                                                                                                                                 
diff --git a/internal/llm/tools/tools.go b/internal/llm/tools/tools.go
index bf0f8df..542fc90 100644
--- a/internal/llm/tools/tools.go
+++ b/internal/llm/tools/tools.go
@@ -10,6 +10,7 @@ type ToolInfo struct {
        Description string
        Parameters  map[string]any
        Required    []string
+       Providers   []string
 }

 type toolResponseType string

I will be able to play a bit more now :)

Thanks for this PR, I really appreciate it ! 🙏🏻

@Tycale
Copy link

Tycale commented Jul 12, 2025

Grok 4, 32k token-per-minute cap is somehow annoying… C’mon, Elon, let me spend my money ! 😢

@kujtimiihoxha
Copy link
Collaborator

@askiiRobotics we have added you to the early access for what will become the next version of opencode would love yo get your feedback (you should be invited on discord).

@Tycale make sure to submit https://charm.sh/early-access/ if you want to try out the new version.

@Tycale
Copy link

Tycale commented Jul 14, 2025

@kujtimiihoxha I’d be keen to also try it out and share my feedback. I’m a heavy user of Claude Code and OpenCode, and I’ve also relied on aider.chat quite a bit in the past.

@ruslanvasylev
Copy link

Just merge it! YOLO!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/opencode-ai/opencode/pull/307

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy