xAI Adapter Integration
Relevant source files
The following files were used as context for generating this wiki page:
The xAI Adapter Integration serves as the high-performance cloud intelligence layer for J.A.R.V.I.S. It bridges the local system with xAI's API suite to provide advanced Chat (Grok), Speech-to-Text (Whisper), and Text-to-Speech (Grok-TTS) capabilities. While the system supports local execution via Ollama, the xAI adapter is prioritized for complex reasoning, tool-use loops, and vision-based analysis.
Sources: backend/src/handler.ts:365-375, instructions/design-backend-prompt.txt:59-67
Architecture and Core Logic
The integration is built around a provider-neutral JarvisModelAdapter interface, which is implemented in xai-adapter.ts. The backend dynamically selects the xAI adapter over local providers if an XAI_API_KEY is present in the environment or configuration.
Sources: backend/src/handler.ts:367-375, instructions/design-backend-prompt.txt:60-61
Data Flow for Multi-Modal Interaction
The adapter supports multi-modal inputs, including text and base64-encoded image data for vision tasks. When vision data is detected, the adapter automatically switches to vision-capable models (e.g., grok-2-vision-latest).
Key Components and Functions
Chat and Tool Execution
The xAI adapter is the primary engine for the "Tool Loop," allowing the model to call local functions (file system, browser, memory) and react to their results before finalizing a response.
| Function | File | Description |
|---|---|---|
createXaiAdapter | xai-adapter.ts | Factory function to initialize the adapter with API keys and optional custom fetchers. |
streamChat | xai-adapter.ts | Handles server-sent events for real-time token streaming to the desktop HUD. |
completeChat | xai-adapter.ts | A non-streaming variant used for internal reasoning and complex multi-turn tool loops. |
runToolLoopChat | handler.ts | Orchestrates the iterative calling of tools based on xAI reasoning steps. |
Sources: backend/src/xai-adapter.ts:10-30, backend/src/handler.ts:291-310
Voice Engine Integration
The xAI integration provides low-latency audio processing for speech interaction.
- STT (Speech-to-Text): Uses xAI Whisper to convert audio blobs into text transcripts. The renderer uses a
MediaRecorder+ VAD (Voice Activity Detection) pipeline to trigger these calls. - TTS (Text-to-Speech): Utilizes
grok-ttswith voices likezenithoreve. The backend processes text into MP3 buffers, which are then decoded by the Web Audio API in the renderer.
Sources: backend/src/handler.ts:405-430, src/renderer.tsx:210-230, instructions/design-backend-prompt.txt:63-66
Data Models and Configuration
The adapter interacts with specific request/response schemas defined in the shared contracts.
xAI Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
XAI_API_KEY | string | undefined | Required for activating the cloud adapter. |
model | string | grok-4.1-fast | The default reasoning model for chat. |
ttsVoice | string | zenith | The default voice identity for JARVIS. |
sttLanguage | string | de | Standard language for speech recognition. |
Sources: backend/src/handler.ts:367-375, backend/src/handler.ts:634-645, src/renderer.tsx:550-560
Agent Orchestration Flow
The xAI adapter is used by the DefaultJarvisAgentOrchestrator to facilitate collaboration between sub-agents (Planner, Researcher, Coder, Reviewer).
Implementation Details
Multi-turn Tool Loop Logic
The runToolLoopChat function in handler.ts manages up to 12 turns of interaction between the xAI model and local tools. It maintains a fingerprint of tool calls to prevent duplicate execution and allow the model to self-correct.
// Example Tool Loop Turn (backend/src/handler.ts)
for (let turn = 0; turn < maxTurns; turn++) {
const result = await xaiAdapter.completeChat({ messages, tools: TOOL_DEFINITIONS, ... });
if (!result.toolCalls) break;
for (const toolCall of result.toolCalls) {
const toolResult = await executeTool(toolCall.name, parsedArgs, ...);
messages.push({ role: "tool", tool_call_id: toolCall.id, content: toolResult });
}
}
Sources: backend/src/handler.ts:310-345
Conclusion
The xAI Adapter Integration is a critical component that elevates J.A.R.V.I.S. from a simple local command-line interface to a sophisticated, multi-modal assistant. By handling complex reasoning, vision, and high-fidelity speech through xAI's cloud services, it allows the desktop application to focus on seamless machine control and user experience while maintaining a "privacy-first" fallback to local Ollama models when necessary.
Sources: instructions/design-backend-prompt.txt:59-67, src/renderer.tsx:1050-1065