(messages)
| 197 | } |
| 198 | |
| 199 | async _chatCompletion(messages) { |
| 200 | const headers = { 'Content-Type': 'application/json' }; |
| 201 | if (this.config.apiKey) { |
| 202 | headers['Authorization'] = `Bearer ${this.config.apiKey}`; |
| 203 | } |
| 204 | if (this.config.baseUrl.includes('openrouter.ai')) { |
| 205 | headers['HTTP-Referer'] = 'https://github.com/Doorman11991/smallcode'; |
| 206 | headers['X-Title'] = 'SmallCode'; |
| 207 | } |
| 208 | |
| 209 | const tools = this._getTools(); |
| 210 | |
| 211 | const body = { |
| 212 | model: this.config.model, |
| 213 | messages, |
| 214 | tools: tools.length > 0 ? tools : undefined, |
| 215 | temperature: 0.1, |
| 216 | max_tokens: 4096, |
| 217 | }; |
| 218 | |
| 219 | const response = await fetch(`${this.config.baseUrl}/chat/completions`, { |
| 220 | method: 'POST', |
| 221 | headers, |
| 222 | body: JSON.stringify(body), |
| 223 | }); |
| 224 | |
| 225 | if (!response.ok) { |
| 226 | const err = await response.text(); |
| 227 | // Redact provider error before throwing — some providers echo the |
| 228 | // request back including Authorization headers on 401/403. |
| 229 | throw new Error(`API error ${response.status}: ${sanitizeToolOutput(err).slice(0, 200)}`); |
| 230 | } |
| 231 | |
| 232 | return response.json(); |
| 233 | } |
| 234 | |
| 235 | _getTools() { |
| 236 | const fs = require('fs'); |
no test coverage detected