UNPKG

agentis

Version:

A TypeScript framework for building sophisticated multi-agent systems

92 lines (91 loc) 3.46 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OpenRouterTool = void 0; const openai_1 = __importDefault(require("openai")); const SupabaseClient_1 = require("../utils/SupabaseClient"); class OpenRouterTool { constructor() { this.name = 'OpenRouterTool'; this.description = 'Processes messages using OpenRouter API'; if (!process.env.OPENROUTER_API_KEY) { throw new Error('OPENROUTER_API_KEY is not set in environment variables'); } this.client = new openai_1.default({ baseURL: 'https://openrouter.ai/api/v1', apiKey: process.env.OPENROUTER_API_KEY, defaultHeaders: { 'HTTP-Referer': process.env.NEXT_PUBLIC_URL || 'http://localhost:3000', 'X-Title': 'ElizaOS Agent Framework', 'Content-Type': 'application/json', }, }); } setAgentContext(id, name, lore, role) { this.agentId = id; this.agentName = name; this.agentLore = lore; this.agentRole = role; } async ensureAgentExists(agentId, name, lore, goals) { const { data, error: fetchError } = await SupabaseClient_1.supabase .from('agents') .select() .eq('id', agentId) .single(); if (!data) { const { error: insertError } = await SupabaseClient_1.supabase .from('agents') .insert([{ id: agentId, name, lore, goals: goals ? JSON.stringify(goals) : null }]); if (insertError) { console.error('Failed to create agent:', insertError); throw insertError; } } } async execute(input) { try { const systemPrompt = `You are ${this.agentName}, ${this.agentLore}. Your role is ${this.agentRole}. Always maintain this identity in your responses. Never identify as Claude or any other AI.`; const completion = await this.client.chat.completions.create({ model: 'anthropic/claude-3-sonnet-20240229', messages: [ { role: 'system', content: systemPrompt }, { role: 'user', content: input } ], temperature: 0.7, max_tokens: 1000, stream: false }); const result = completion.choices[0]?.message?.content; if (!result) { throw new Error('Empty response from OpenRouter API'); } return { result, raw: completion }; } catch (error) { console.error('OpenRouter API error:', error); return { result: "I apologize, but I'm having trouble processing your request at the moment. Please try again.", error: error instanceof Error ? error.message : 'Unknown error occurred' }; } } } exports.OpenRouterTool = OpenRouterTool;