local-agent
Version:
A CLI agentic system for orchestrating tools and memory with per-folder scoping
79 lines • 3.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @fileoverview
* OpenRouter provider implementation for the AI Provider Factory.
*/
const types_1 = require("./types");
/**
* OpenRouter provider implementation.
*/
class OpenRouterProvider {
apiKey = null;
client = null;
/**
* Creates and returns an OpenRouter model instance.
* @param modelName The OpenRouter model name, which might include provider namespace
* @returns An OpenRouter model instance
* @throws ProviderError if OpenRouter client cannot be initialized
*/
getModel(modelName) {
if (!this.client) {
this.initializeClient();
}
// OpenRouter supports both chat and completion models
// For simplicity, we'll use chat for all models
return this.client.chat(modelName);
}
/**
* Indicates if this provider requires an API key.
* OpenRouter always requires an API key.
*/
requiresApiKey() {
return true;
}
/**
* Initializes the provider with an API key.
* @param keys Object containing API keys
*/
initialize(keys) {
if (keys.openrouter) {
this.apiKey = keys.openrouter;
// Set environment variable for SDK to use
process.env.OPENROUTER_API_KEY = this.apiKey;
}
}
/**
* Initialize the OpenRouter client.
* @private
* @throws ProviderError if the client cannot be initialized
*/
initializeClient() {
try {
// Check if @openrouter/ai-sdk-provider is installed
const openrouterModule = require('@openrouter/ai-sdk-provider');
if (!this.apiKey && !process.env.OPENROUTER_API_KEY) {
throw new types_1.ProviderError('OpenRouter API key not provided. Set OPENROUTER_API_KEY environment variable or provide it in keys.json.', 'openrouter', true);
}
this.client = openrouterModule.createOpenRouter({
apiKey: this.apiKey || process.env.OPENROUTER_API_KEY || '',
});
if (!this.client) {
throw new types_1.ProviderError('Failed to initialize OpenRouter client', 'openrouter', true);
}
}
catch (error) {
if (error instanceof types_1.ProviderError) {
throw error;
}
// Check if it's a module not found error
if (error instanceof Error && error.message.includes('Cannot find module')) {
throw new types_1.ProviderError('OpenRouter provider is not available. Install @openrouter/ai-sdk-provider package.', 'openrouter', true);
}
const message = error instanceof Error ? error.message : String(error);
throw new types_1.ProviderError(`Failed to initialize OpenRouter: ${message}`, 'openrouter', true);
}
}
}
exports.default = OpenRouterProvider;
//# sourceMappingURL=openrouter-provider.js.map