Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/providers/x-ai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,43 @@ import {
responseTransformers,
} from '../open-ai-base';

interface XAIErrorResponse {
error:
| {
message: string;
code: string;
param: string | null;
type: string | null;
}
| string;
code?: string;
}

const xAIResponseTransform = <T>(response: T) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Code Refactor

Issue: The function lacks documentation explaining its purpose and behavior.
Fix: Add JSDoc comments to explain the function's purpose and return value.
Impact: Improves code maintainability and makes it easier for other developers to understand the transformation logic.

Suggested change
const xAIResponseTransform = <T>(response: T) => {
/**
* Transforms X-AI error responses to match the OpenAI error format
* for consistent error handling across providers.
* @param response The original response from X-AI
* @returns Transformed response in OpenAI-compatible format or the original response if not an error
*/
const xAIResponseTransform = <T>(response: T) => {

let _response = response as XAIErrorResponse;
if ('error' in _response) {
return {
error: {
message: _response.error as string,
code: _response.code ?? null,
param: null,
type: null,
},
provider: X_AI,
};
}
return response;
Comment on lines +24 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Bug Fix

Issue: The error handling doesn't properly handle the case when the error property is an object rather than a string, despite the interface defining it as potentially being an object.
Fix: Add proper type checking and handle both string and object error formats.
Impact: Prevents potential runtime errors when X-AI returns an error object instead of a string.

Suggested change
let _response = response as XAIErrorResponse;
if ('error' in _response) {
return {
error: {
message: _response.error as string,
code: _response.code ?? null,
param: null,
type: null,
},
provider: X_AI,
};
}
return response;
let _response = response as XAIErrorResponse;
if ('error' in _response) {
return {
error: {
message: typeof _response.error === 'string'
? _response.error
: _response.error?.message ?? 'Unknown error',
code: _response.code ?? _response.error?.code ?? null,
param: _response.error?.param ?? null,
type: _response.error?.type ?? null,
},
provider: X_AI,
};
}
return response;

};

const XAIConfig: ProviderConfigs = {
chatComplete: chatCompleteParams([], { model: 'grok-beta' }),
complete: completeParams([], { model: 'grok-beta' }),
embed: embedParams([], { model: 'v1' }),
api: XAIAPIConfig,
responseTransforms: responseTransformers(X_AI, {
chatComplete: true,
complete: true,
embed: true,
chatComplete: xAIResponseTransform,
complete: xAIResponseTransform,
embed: xAIResponseTransform,
}),
};

Expand Down