Skip to content

Add xAI (Grok) provider for @workglow/ai#622

Merged
sroussey merged 3 commits into
mainfrom
claude/xai-grok-provider-ssq9if
Jul 10, 2026
Merged

Add xAI (Grok) provider for @workglow/ai#622
sroussey merged 3 commits into
mainfrom
claude/xai-grok-provider-ssq9if

Conversation

@sroussey

Copy link
Copy Markdown
Collaborator

Adds a new @workglow/xai provider package that integrates xAI's Grok models with the Workglow AI framework. xAI exposes an OpenAI-compatible REST API, so this provider reuses the openai SDK pointed at https://api.x.ai/v1.

Key Changes

  • New provider package (providers/xai/) with full AI task support:

    • Text generation (chat completions)
    • Tool use / function calling
    • Structured generation (JSON mode)
    • Vision input (for vision-capable models)
    • Image generation (grok-2-image and successors)
    • Text rewriting and summarization
    • Token counting (via tiktoken approximation)
    • Model search and info endpoints
  • Capability inference (Xai_Capabilities.ts):

    • Pattern-matches Grok model IDs to infer capabilities
    • Distinguishes between chat models (grok-3, grok-4), vision models (grok-2-vision), and image models (grok-2-image)
    • Falls back to declared capabilities or baseline metadata operations
  • Client abstraction (Xai_Client.ts):

    • Lazy-loads OpenAI SDK to avoid bundling cost on main thread
    • Validates base URL against allow-list (api.x.ai) with opt-out for enterprise gateways
    • Resolves API key from environment or credential store
  • Streaming run-functions for all capabilities:

    • Unified text generation handler for both TextGenerationTask and AiChatTask
    • Tool calling with defense-in-depth filtering against hallucinated function names
    • Structured generation with partial JSON parsing and streaming
    • Image generation with base64 decoding
  • Browser and Node.js variants:

    • Token counting uses js-tiktoken in browser, WASM tiktoken in Node.js
    • Separate build targets for ai.ts, ai.browser.ts, and ai-runtime.ts
  • Integration tests (Xai_Generic.integration.test.ts):

    • Conformance tests against the AI provider contract
    • Skipped unless XAI_API_KEY environment variable is set
  • Package exports and registration:

    • registerXai() for worker-backed mode
    • registerXaiInline() for inline mode
    • registerXaiWorker() for worker server registration
    • Integrated into @workglow/workglow meta-package

Implementation Details

  • Follows existing provider patterns (Anthropic, OpenAI) with capability-set-based run-fn registration
  • Uses OpenAI SDK's streaming interface for all streaming tasks
  • Token counting approximates with cl100k_base encoding (xAI doesn't publish a public tokenizer)
  • Image generation requests base64 output and converts to ImageValue via data URI
  • Model search falls back to a hardcoded list if the API is unavailable

https://claude.ai/code/session_01Jx794dFEj3P7jT6pgytkaq

claude and others added 3 commits July 9, 2026 23:10
Add @workglow/xai, a cloud provider for the xAI Grok API. xAI exposes an
OpenAI-compatible REST surface, so the provider reuses the `openai` SDK
pointed at https://api.x.ai/v1 and mirrors the @workglow/openai structure.

Capabilities served:
- text.generation (chat + prompt, vision input via message content)
- text.generation + tool-use
- text.generation + json-mode (structured output)
- text.rewriter, text.summary
- image.generation (grok-2-image; non-streaming)
- model.count-tokens (tiktoken cl100k_base approximation)
- model.info, model.search (curated Grok fallback list)

Wiring:
- providers/xai package (node + browser builds, ./ai and ./ai-runtime)
- re-export from the workglow meta-package (./xai, ./xai/runtime)
- register in @workglow/test and examples/cli
- widen validateProviderBaseUrl vendor union to include "xai"
- unit + runtime-metadata tests, plus a skipped conformance integration test

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jx794dFEj3P7jT6pgytkaq
Match "image" only as a full id segment (grok-2-image-1212) instead of any
substring, so a chat/vision model whose id merely contains "image"
(e.g. a hypothetical "reimagine-*") is not misrouted to image-only
capabilities and stripped of text.generation/tool-use/json-mode.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jx794dFEj3P7jT6pgytkaq
@github-actions

Copy link
Copy Markdown

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 62.74% 25962 / 41379
🔵 Statements 62.58% 26865 / 42927
🔵 Functions 63.65% 4914 / 7720
🔵 Branches 51.32% 12709 / 24762
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
scripts/lib/test-credentials.ts 90.62% 78.57% 100% 89.28% 93-98
Generated in workflow #2684 for commit 0ddcb7e by the Vitest Coverage Report Action

@sroussey sroussey merged commit 146f48c into main Jul 10, 2026
10 checks passed
@sroussey sroussey deleted the claude/xai-grok-provider-ssq9if branch July 10, 2026 00:32
sroussey added a commit that referenced this pull request Jul 10, 2026
## @workglow/ai

### Features

#### providers

- add xAI (Grok) AI provider(#622)

## workglow

### Features

- add node-llama-cpp provider to workglow meta-package (#629)
- add OpenRouter provider for @workglow/ai (#626)

#### providers

- add xAI (Grok) AI provider(#622)

## @workglow/storage

### Bug Fixes

#### storage

- genuine CAS for updateWhere on IndexedDb + HttpTabularProxy (#628)

## @workglow/test

### Features

- add OpenRouter provider for @workglow/ai (#626)

#### providers

- add xAI (Grok) AI provider(#622)

### Bug Fixes

#### storage

- genuine CAS for updateWhere on IndexedDb + HttpTabularProxy (#628)

## @workglow/indexeddb

### Bug Fixes

#### storage

- genuine CAS for updateWhere on IndexedDb + HttpTabularProxy (#628)

## @workglow/xai

### Features

#### providers

- add xAI (Grok) AI provider(#622)

## @workglow/openrouter

### Features

- add OpenRouter provider for @workglow/ai (#626)

## @workglow/cli

### Features

#### providers

- add xAI (Grok) AI provider(#622)
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.

2 participants