MCPcopy
hub / github.com/Doorman11991/smallcode / _chatCompletion

Method _chatCompletion

src/api/index.js:199–233  ·  view source on GitHub ↗
(messages)

Source from the content-addressed store, hash-verified

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');

Callers 1

runMethod · 0.95

Calls 2

_getToolsMethod · 0.95
sanitizeToolOutputFunction · 0.85

Tested by

no test coverage detected