UNPKG

contextual-agent-sdk

Version:

SDK for building AI agents with seamless voice-text context switching

96 lines 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GenericProvider = void 0; const BaseLLMProvider_1 = require("./BaseLLMProvider"); class GenericProvider extends BaseLLMProvider_1.BaseLLMProvider { type = 'custom'; config; constructor(config) { super(); this.config = config; } supportsTools() { return false; } supportsStreaming() { return false; } getMaxTokens() { return this.config.options?.maxTokens || 4096; } async generateResponse(options) { if (!this.config.baseURL) { throw new Error('Generic provider not configured. Please provide a baseURL.'); } try { const requestBody = this.config.options?.requestTransform ? this.config.options.requestTransform(options) : this.defaultRequestTransform(options); const headers = { 'Content-Type': 'application/json', ...this.config.options?.headers }; if (this.config.apiKey) { headers['Authorization'] = `Bearer ${this.config.apiKey}`; } const response = await fetch(this.config.baseURL, { method: 'POST', headers, body: JSON.stringify(requestBody) }); if (!response.ok) { const errorData = await response.text(); throw new Error(`Generic API error: ${response.status} ${errorData}`); } const data = await response.json(); return this.config.options?.responseTransform ? this.config.options.responseTransform(data) : this.defaultResponseTransform(data); } catch (error) { throw new Error(`Generic API error: ${error.message}`); } } async isAvailable() { return !!this.config.baseURL; } async test() { if (!(await this.isAvailable())) { throw new Error('Generic provider not configured'); } await this.generateResponse({ messages: [{ role: 'user', content: 'Hello' }], maxTokens: 10 }); } defaultRequestTransform(options) { return { model: this.config.model || 'default', messages: options.messages, temperature: options.temperature ?? 0.7, max_tokens: options.maxTokens ?? 500 }; } defaultResponseTransform(data) { const content = data.choices?.[0]?.message?.content || data.content || data.response || data.text || ''; if (!content) { throw new Error('No response content from Generic API'); } return { content, usage: data.usage ? { promptTokens: data.usage.prompt_tokens || data.usage.input_tokens || 0, completionTokens: data.usage.completion_tokens || data.usage.output_tokens || 0, totalTokens: data.usage.total_tokens || (data.usage.prompt_tokens || 0) + (data.usage.completion_tokens || 0) || (data.usage.input_tokens || 0) + (data.usage.output_tokens || 0) || 0 } : undefined }; } } exports.GenericProvider = GenericProvider; //# sourceMappingURL=GenericProvider.js.map