File size: 9,786 Bytes
529090e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import OpenAI from 'openai';
// DeepSeek uses OpenAI-compatible API, no separate stub needed
// Google Generative AI imported dynamically to avoid issues if package is missing

// CODEX SYMBIOSIS: The system's conscience
import { CODEX_SYSTEM_PROMPT, CODEX_VERSION, buildCodexPrompt } from '../../config/codex.js';

export interface ChatMessage {
  role: 'system' | 'user' | 'assistant';
  content: string;
}

export interface ChatCompletionOptions {
  model: string;
  messages: ChatMessage[];
  temperature?: number;
  maxTokens?: number;
  stream?: boolean;
}

export interface ChatCompletionResponse {
  content: string;
  model: string;
  usage?: {
    promptTokens: number;
    completionTokens: number;
    totalTokens: number;
  };
}

export class LlmService {
  private openai?: OpenAI;
  private deepseek?: any; // DeepSeekAPI
  private googleKey?: string;

  constructor() {
    if (process.env.OPENAI_API_KEY) {
      this.openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
    }
    if (process.env.DEEPSEEK_API_KEY) {
      // Initialize DeepSeek if SDK is available, otherwise we might need a fetch implementation
      // For now, we'll assume the simple OpenAI-compatible usage or the stub
      this.deepseek = new OpenAI({
        apiKey: process.env.DEEPSEEK_API_KEY,
        baseURL: 'https://api.deepseek.com'
      });
    }
    this.googleKey = process.env.GOOGLE_API_KEY;
  }

  private getProvider(model: string): 'openai' | 'google' | 'deepseek' | 'anthropic' {
    if (model.startsWith('gpt-') || model.startsWith('o1-')) return 'openai';
    if (model.startsWith('gemini-')) return 'google';
    if (model.startsWith('deepseek-')) return 'deepseek';
    if (model.startsWith('claude-')) return 'anthropic';
    return 'openai'; // Default
  }

  /**
   * CODEX-ENHANCED COMPLETION
   * All LLM calls now pass through the Codex filter.
   * The system's conscience is injected as the FIRST system message.
   */
  async complete(options: ChatCompletionOptions): Promise<ChatCompletionResponse> {
    const provider = this.getProvider(options.model);

    // CODEX INJECTION: Prepend the system's conscience to all messages
    // This ensures every AI response adheres to our ethical framework
    const codexEnhancedMessages = this.injectCodex(options.messages);

    const enhancedOptions = {
      ...options,
      messages: codexEnhancedMessages
    };

    try {
      switch (provider) {
        case 'openai':
          return await this.completeOpenAI(enhancedOptions);
        case 'deepseek':
          return await this.completeDeepSeek(enhancedOptions);
        case 'google':
          return await this.completeGoogle(enhancedOptions);
        default:
          throw new Error(`Provider ${provider} not supported yet`);
      }
    } catch (error: any) {
      // Fallback to mock if key is missing or API fails
      if (error.message.includes('API key not configured') || error.message.includes('not configured')) {
        console.warn(`⚠️ LLM Provider ${provider} not configured. Using mock response.`);
        return this.completeMock(enhancedOptions);
      }
      throw error;
    }
  }

  /**
   * CODEX INJECTION
   * Injects the Codex system prompt as the FIRST message,
   * ensuring it has the highest priority in the AI's decision-making.
   */
  private injectCodex(messages: ChatMessage[]): ChatMessage[] {
    // Check if Codex is already injected (avoid double injection)
    const hasCodex = messages.some(m =>
      m.role === 'system' && m.content.includes('CODEX SYMBIOSIS')
    );

    if (hasCodex) {
      return messages;
    }

    // Inject Codex as the FIRST system message
    console.log(`🧬 [CODEX v${CODEX_VERSION}] Injecting symbiosis protocol...`);

    return [
      { role: 'system', content: CODEX_SYSTEM_PROMPT },
      ...messages
    ];
  }

  private completeMock(options: ChatCompletionOptions): ChatCompletionResponse {
    const lastMsg = options.messages[options.messages.length - 1];
    return {
      content: `[MOCK RESPONSE] I received your message: "${lastMsg?.content || '...'}". \n\n(No LLM API key configured. Please set OPENAI_API_KEY, DEEPSEEK_API_KEY, or GOOGLE_API_KEY in .env)`,
      model: 'mock-model',
      usage: {
        promptTokens: 0,
        completionTokens: 0,
        totalTokens: 0
      }
    };
  }

  private async completeOpenAI(options: ChatCompletionOptions): Promise<ChatCompletionResponse> {
    if (!this.openai) throw new Error('OpenAI API key not configured');

    const response = await this.openai.chat.completions.create({
      model: options.model,
      messages: options.messages,
      temperature: options.temperature,
      max_tokens: options.maxTokens,
    });

    return {
      content: response.choices[0]?.message?.content || '',
      model: response.model,
      usage: {
        promptTokens: response.usage?.prompt_tokens || 0,
        completionTokens: response.usage?.completion_tokens || 0,
        totalTokens: response.usage?.total_tokens || 0
      }
    };
  }

  private async completeDeepSeek(options: ChatCompletionOptions): Promise<ChatCompletionResponse> {
    if (!this.deepseek) throw new Error('DeepSeek API key not configured');

    // DeepSeek is OpenAI compatible
    const response = await this.deepseek.chat.completions.create({
      model: options.model,
      messages: options.messages,
      temperature: options.temperature,
      max_tokens: options.maxTokens,
    });

    return {
      content: response.choices[0]?.message?.content || '',
      model: response.model,
      usage: {
        promptTokens: response.usage?.prompt_tokens || 0,
        completionTokens: response.usage?.completion_tokens || 0,
        totalTokens: response.usage?.total_tokens || 0
      }
    };
  }

  private async completeGoogle(options: ChatCompletionOptions): Promise<ChatCompletionResponse> {
    if (!this.googleKey) throw new Error('Google API key not configured');

    // Validate messages array is not empty
    if (!options.messages || options.messages.length === 0) {
      throw new Error('Messages array cannot be empty');
    }

    // Dynamic import to avoid issues if package is missing
    const { GoogleGenerativeAI } = await import('@google/generative-ai');
    const genAI = new GoogleGenerativeAI(this.googleKey);
    const model = genAI.getGenerativeModel({ model: options.model });

    // Convert messages to Google format
    // Note: Google's history format is slightly different, simplified here
    const lastMessage = options.messages[options.messages.length - 1];
    if (!lastMessage || !lastMessage.content) {
      throw new Error('Last message must have content');
    }

    const history = options.messages.slice(0, -1).map(m => ({
      role: m.role === 'assistant' ? 'model' : 'user',
      parts: [{ text: m.content }]
    }));

    const chat = model.startChat({
      history: history as any,
      generationConfig: {
        maxOutputTokens: options.maxTokens,
        temperature: options.temperature,
      },
    });

    const result = await chat.sendMessage(lastMessage.content);
    const response = await result.response;
    const text = response.text();

    return {
      content: text,
      model: options.model,
      usage: {
        promptTokens: 0, // Google doesn't always return usage in simple call
        completionTokens: 0,
        totalTokens: 0
      }
    };
  }

  // Legacy method support for existing code
  async generateResponse(prompt: string): Promise<string> {
    const res = await this.complete({
      model: 'gpt-4o', // Default to a strong model
      messages: [{ role: 'user', content: prompt }]
    });
    return res.content;
  }

  async generateContextualResponse(systemContext: string, userQuery: string, additionalContext?: string, model?: string): Promise<string> {
    const messages: ChatMessage[] = [{ role: 'system', content: systemContext }];
    if (additionalContext) {
      messages.push({ role: 'system', content: `Additional Context: ${additionalContext}` });
    }
    messages.push({ role: 'user', content: userQuery });

    const res = await this.complete({
      model: model || 'gpt-4o',
      messages
    });
    return res.content;
  }

  async transcribeAudio(audioData: Buffer, mimeType: string): Promise<string> {
      // Mock implementation for now as we don't have audio setup
      console.log(`[LlmService] Transcribing audio (${audioData.length} bytes, ${mimeType})`);
      return "Audio transcription not yet implemented. This is a placeholder.";
  }

  async analyzeImage(imageData: Buffer, mimeType: string, prompt: string): Promise<string> {
      if (this.googleKey) {
          try {
             const { GoogleGenerativeAI } = await import('@google/generative-ai');
             const genAI = new GoogleGenerativeAI(this.googleKey);
             const model = genAI.getGenerativeModel({ model: 'gemini-1.5-flash' });
             
             const imagePart = {
                inlineData: {
                    data: imageData.toString('base64'),
                    mimeType
                }
             };
             
             const result = await model.generateContent([prompt, imagePart]);
             const response = await result.response;
             return response.text();
          } catch (e) {
              console.error('Gemini image analysis failed:', e);
          }
      }
      return `[Mock Image Analysis] I see an image of size ${imageData.length} bytes. (Configure GOOGLE_API_KEY for real analysis)`;
  }
}

// Singleton instance
let llmServiceInstance: LlmService | null = null;

export function getLlmService(): LlmService {
  if (!llmServiceInstance) {
    llmServiceInstance = new LlmService();
  }
  return llmServiceInstance;
}