Repository navigation
feat: implement xAI adapter - #571
jhosepm352-design wants to merge 1 commit into
Conversation
Greptile SummaryThis PR refactors the xAI adapter to align more closely with the OpenAI-compatible adapter pattern, but introduces several regressions that break existing tests and remove a security safeguard.
Confidence Score: 1/5Not safe to merge — the adapter throws a ReferenceError on every invocation, the API key redaction safety net was removed, and three existing tests will fail. The generate function references two variables ( packages/ai/xai/src/index.ts needs attention on all four issues before this is mergeable.
|
| Filename | Overview |
|---|---|
| packages/ai/xai/src/index.ts | Introduces four regressions: undefined variables xAI_LOWER/xAI (ReferenceError on every call), removed redact helper exposing API key in error messages, removed trailing-slash normalization producing double-slash URLs, and default model changed to the legacy grok-beta — all of which break existing tests. |
Sequence Diagram
sequenceDiagram
participant Caller
participant xAIAdapter as xAI Adapter (index.ts)
participant Vault as Secret Vault
participant xAIAPI as xAI API
Caller->>xAIAdapter: generate(ctx, prompt, opts, config)
xAIAdapter->>Vault: ctx.secret('XAI_API_KEY')
Vault-->>xAIAdapter: apiKey
Note over xAIAdapter: xAI_LOWER is not defined - execution stops here
xAIAdapter->>xAIAPI: POST /v1/chat/completions
alt res.ok
xAIAPI-->>xAIAdapter: 200 JSON
xAIAdapter-->>Caller: text, model, tokens
else res not ok
xAIAPI-->>xAIAdapter: 4xx/5xx error body
Note over xAIAdapter: API key NOT redacted from error text
xAIAdapter-->>Caller: throw Error with raw body
end
Reviews (1): Last reviewed commit: "feat: add xAI adapter" | Re-trigger Greptile
| const model = opts.model ?? 'grok-3'; | ||
| ctx.log(`xai · model=${model} · ${prompt.length} chars in`); | ||
| const model = opts.model ?? 'grok-beta'; | ||
| ctx.log(`${xAI_LOWER} · model=${model} · ${prompt.length} chars in`); |
There was a problem hiding this comment.
Undefined variable
xAI_LOWER causes ReferenceError
xAI_LOWER is not defined, imported, or declared anywhere in this file or the repository. Every call to generate() will throw a ReferenceError: xAI_LOWER is not defined at runtime (or fail TypeScript compilation). The same applies to xAI on line 42. The original code used string literals 'xai' and 'xAI' directly.
| }), | ||
| }); | ||
| if (!res.ok) throw new Error(`xAI ${res.status}: ${redact(await res.text(), apiKey).slice(0, 200)}`); | ||
| if (!res.ok) throw new Error(`${xAI} ${res.status}: ${(await res.text()).slice(0, 200)}`); |
There was a problem hiding this comment.
Removal of
redact exposes API key in error messages
The redact helper was deleted, so API error responses are now sliced and thrown verbatim. The existing test (index.test.ts lines 84–103) explicitly verifies that the error message contains [redacted] and does not contain the API key or its first 10 characters — that test will now fail. An API key that spans the 200-character truncation boundary can leak partially in the thrown Error message, which may surface in logs, Sentry, or other observability tools.
| 'content-type': 'application/json', | ||
| }; | ||
|
|
||
| const res = await fetch(`${config.baseUrl ?? DEFAULT_BASE}/v1/chat/completions`, { |
There was a problem hiding this comment.
Trailing slash in
baseUrl produces a double-slash URL
The removed chatCompletionsUrl helper called .replace(/\/+$/, '') to strip trailing slashes before appending /v1/chat/completions. The bare template literal ${config.baseUrl ?? DEFAULT_BASE}/v1/chat/completions no longer does that normalization. When a caller passes { baseUrl: 'https://proxy.example.com/' }, the resulting URL is https://proxy.example.com//v1/chat/completions. The test at index.test.ts line 81 explicitly asserts the single-slash form and will fail with this change.
| const model = opts.model ?? 'grok-beta'; | ||
| ctx.log(`${xAI_LOWER} · model=${model} · ${prompt.length} chars in`); | ||
| if (ctx.dryRun) return { text: '[dry-run]', model }; |
There was a problem hiding this comment.
Default model changed to
grok-beta breaks the dry-run test
The dry-run test in index.test.ts (line 24) asserts { text: '[dry-run]', model: 'grok-3' }. With the default now changed to 'grok-beta', that assertion will fail. Additionally, grok-beta is the older xAI model name; the xAI API currently supports grok-3, grok-3-mini, and grok-2-latest — the model list was fully narrowed down to a legacy alias.
|
🤖 Auto-rebase: The branch was rebased successfully locally but could not be pushed to the fork. Please enable 'Allow edits from maintainers' in the PR settings, or rebase manually: |
2 similar comments
|
🤖 Auto-rebase: The branch was rebased successfully locally but could not be pushed to the fork. Please enable 'Allow edits from maintainers' in the PR settings, or rebase manually: |
|
🤖 Auto-rebase: The branch was rebased successfully locally but could not be pushed to the fork. Please enable 'Allow edits from maintainers' in the PR settings, or rebase manually: |
|
tests are failing please fix. |
Implements the xAI provider using the OpenAI-compatible protocol.