| 20 | const XAI_DEFAULT_TEMPERATURE = 0 |
| 21 | |
| 22 | export class XAIHandler extends BaseProvider implements SingleCompletionHandler { |
| 23 | protected options: ApiHandlerOptions |
| 24 | private client: OpenAI |
| 25 | private readonly providerName = "xAI" |
| 26 | |
| 27 | constructor(options: ApiHandlerOptions) { |
| 28 | super() |
| 29 | this.options = options |
| 30 | |
| 31 | const apiKey = this.options.xaiApiKey ?? "not-provided" |
| 32 | |
| 33 | this.client = new OpenAI({ |
| 34 | baseURL: "https://api.x.ai/v1", |
| 35 | apiKey: apiKey, |
| 36 | defaultHeaders: DEFAULT_HEADERS, |
| 37 | timeout: this.timeoutMs, |
| 38 | }) |
| 39 | } |
| 40 | |
| 41 | override getModel() { |
| 42 | const id = |
| 43 | this.options.apiModelId && this.options.apiModelId in xaiModels |
| 44 | ? (this.options.apiModelId as XAIModelId) |
| 45 | : xaiDefaultModelId |
| 46 | |
| 47 | const info = xaiModels[id] |
| 48 | const params = getModelParams({ |
| 49 | format: "openai", |
| 50 | modelId: id, |
| 51 | model: info, |
| 52 | settings: this.options, |
| 53 | defaultTemperature: XAI_DEFAULT_TEMPERATURE, |
| 54 | }) |
| 55 | return { id, info, ...params } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Convert tools from OpenAI Chat Completions format to Responses API format. |
| 60 | * Chat Completions: { type: "function", function: { name, description, parameters } } |
| 61 | * Responses API: { type: "function", name, description, parameters } |
| 62 | * |
| 63 | * Uses base provider's convertToolSchemaForOpenAI() for schema hardening |
| 64 | * (additionalProperties: false, ensureAllRequired) and handles MCP tools. |
| 65 | */ |
| 66 | private mapResponseTools(tools?: any[]): any[] | undefined { |
| 67 | const converted = this.convertToolsForOpenAI(tools) |
| 68 | if (!converted?.length) { |
| 69 | return undefined |
| 70 | } |
| 71 | return converted |
| 72 | .filter((tool) => tool?.type === "function") |
| 73 | .map((tool) => { |
| 74 | const isMcp = isMcpTool(tool.function.name) |
| 75 | return { |
| 76 | type: "function", |
| 77 | name: tool.function.name, |
| 78 | description: tool.function.description, |
| 79 | parameters: isMcp |
nothing calls this directly
no outgoing calls
no test coverage detected