-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaiProvider.ts
More file actions
415 lines (373 loc) · 14.3 KB
/
Copy pathaiProvider.ts
File metadata and controls
415 lines (373 loc) · 14.3 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import { GoogleGenAI, Type } from "@google/genai";
function rewriteLocalhost(url?: string): string | undefined {
if (!url) return url;
if (process.env.RUNNING_IN_DOCKER === "true" || process.env.NODE_ENV === "production") {
// Replace localhost or 127.0.0.1 with host.docker.internal
return url.replace(/:\/\/(localhost|127\.0\.0\.1)(:|\/|$)/, "://host.docker.internal$2");
}
return url;
}
// Standard configurations for our unified Pluggable AI system
export interface AIConfig {
provider: "gemini" | "lm-studio" | "custom";
geminiConfig?: {
apiKey?: string;
model?: string;
};
lmStudioConfig?: {
endpoint?: string;
model?: string;
};
customConfig?: {
endpoint?: string;
apiKey?: string;
model?: string;
};
embeddingProvider: "gemini" | "local" | "custom";
embeddingConfig?: {
model?: string;
endpoint?: string;
apiKey?: string;
};
}
export interface ModelCapability {
chat: boolean;
embeddings: boolean;
vision: boolean;
structuredOutput: boolean;
streaming: boolean;
}
// Global list of standard Gemini models (used as a fallback list and defaults)
export const STANDARD_GEMINI_MODELS = [
"gemini-3.5-flash",
"gemini-3.1-pro-preview",
"gemini-3.1-flash-lite",
"gemini-2.5-flash-image",
"gemini-embedding-2-preview"
];
// Helper to generate deterministic fallback vectors
export function getDeterministicMockEmbedding(text: string, dimensions = 1536): number[] {
const hash = Array.from(text).reduce((acc, char) => acc + char.charCodeAt(0), 0);
const vec: number[] = [];
for (let i = 0; i < dimensions; i++) {
// Generate values between -1.0 and 1.0 using Math.sin
vec.push(Math.sin(hash + i * 17) * 0.1);
}
// L2 Normalize
const norm = Math.sqrt(vec.reduce((sum, val) => sum + val * val, 0));
return vec.map(v => (norm === 0 ? 0 : v / norm));
}
// Abstraction class for the AI Provider
export class AIProvider {
private config: AIConfig;
constructor(config: AIConfig) {
this.config = {
provider: config.provider || "gemini",
geminiConfig: config.geminiConfig || {},
lmStudioConfig: config.lmStudioConfig || {},
customConfig: config.customConfig || {},
embeddingProvider: config.embeddingProvider || "gemini",
embeddingConfig: config.embeddingConfig || {},
};
}
// Get current active model name
public getActiveModelName(): string {
if (this.config.provider === "gemini") {
return this.config.geminiConfig?.model || "gemini-3.5-flash";
} else if (this.config.provider === "lm-studio") {
return this.config.lmStudioConfig?.model || "Currently Loaded Model";
} else {
return this.config.customConfig?.model || "custom-model";
}
}
// Query LM Studio directly to fetch the active loaded model name
public async fetchActiveLMStudioModel(): Promise<string> {
try {
const baseEndpoint = rewriteLocalhost(this.config.lmStudioConfig?.endpoint || "http://localhost:1234")!;
// Clean up base URL by removing /v1 if present to query models list
let cleanUrl = baseEndpoint.trim();
if (cleanUrl.endsWith("/")) {
cleanUrl = cleanUrl.slice(0, -1);
}
if (!cleanUrl.endsWith("/v1")) {
cleanUrl = `${cleanUrl}/v1`;
}
const res = await fetch(`${cleanUrl}/models`);
if (res.ok) {
const data = await res.json();
const modelsList = data?.data || [];
const chatModels = modelsList.filter((m: any) => {
const id = (m.id || "").toLowerCase();
return !id.includes("embed");
});
const loadedModel = chatModels[0]?.id || modelsList[0]?.id;
if (loadedModel) return loadedModel;
}
} catch (e) {}
return "Currently Loaded Model";
}
// Get active embedding model name
public getActiveEmbeddingModelName(): string {
return this.config.embeddingConfig?.model || (this.config.embeddingProvider === "gemini" ? "gemini-embedding-2-preview" : "nomic-embed-text");
}
// Get capabilities of the currently configured provider & model combination
public getCapabilities(): ModelCapability {
const provider = this.config.provider;
if (provider === "gemini") {
const model = this.getActiveModelName();
const isEmbed = model.includes("embed");
return {
chat: !isEmbed,
embeddings: true,
vision: model.includes("flash") || model.includes("pro") || model.includes("image"),
structuredOutput: true,
streaming: !isEmbed,
};
} else {
// LM Studio & Custom OpenAI Compatible endpoint
return {
chat: true,
embeddings: true,
vision: false, // Local servers usually don't support multi-modal vision by default unless specialized
structuredOutput: true, // Supported by most OpenAI-compatible custom endpoints
streaming: true,
};
}
}
/**
* Abstracted method to generate response. Handles JSON response structure.
*/
public async generateResponse(
prompt: string,
options?: {
systemInstruction?: string;
responseSchema?: any;
signal?: AbortSignal;
}
): Promise<string> {
const provider = this.config.provider;
if (provider === "gemini") {
// Use the Google GenAI SDK (with appropriate fallback)
const apiKey = this.config.geminiConfig?.apiKey || process.env.GEMINI_API_KEY;
if (!apiKey) {
throw new Error("Gemini API key is not configured in settings or environment.");
}
const client = new GoogleGenAI({
apiKey,
httpOptions: {
headers: {
"User-Agent": "aistudio-build",
},
},
});
const model = this.getActiveModelName();
console.log(`[AIProvider] Running Gemini Response Generation via SDK [Model: ${model}]`);
const configPayload: any = {};
if (options?.systemInstruction) {
configPayload.systemInstruction = options.systemInstruction;
}
if (options?.responseSchema) {
configPayload.responseMimeType = "application/json";
configPayload.responseSchema = options.responseSchema;
}
const response = await client.models.generateContent({
model,
contents: prompt,
config: configPayload,
});
return response.text || "{}";
} else if (provider === "lm-studio") {
// LM Studio (Local OpenAI compatible)
const url = rewriteLocalhost(this.config.lmStudioConfig?.endpoint || "http://localhost:1234")!;
let model = this.config.lmStudioConfig?.model || "Currently Loaded Model";
if (model === "Currently Loaded Model") {
const resolvedModel = await this.fetchActiveLMStudioModel();
if (resolvedModel && resolvedModel !== "Currently Loaded Model") {
model = resolvedModel;
}
}
console.log(`[AIProvider] Running LM Studio Request [Endpoint: ${url}, Model: ${model}]`);
return this.callOpenAICompatibleAPI(url, "", model, prompt, options);
} else {
// Custom OpenAI Compatible Endpoint
const url = rewriteLocalhost(this.config.customConfig?.endpoint || "")!;
const apiKey = this.config.customConfig?.apiKey || "";
const model = this.config.customConfig?.model || "custom-model";
console.log(`[AIProvider] Running Custom OpenAI Compatible request [Endpoint: ${url}, Model: ${model}]`);
return this.callOpenAICompatibleAPI(url, apiKey, model, prompt, options);
}
}
/**
* Helper function to call OpenAI style endpoints (/v1/chat/completions)
*/
private async callOpenAICompatibleAPI(
endpoint: string,
apiKey: string,
model: string,
prompt: string,
options?: {
systemInstruction?: string;
responseSchema?: any;
signal?: AbortSignal;
}
): Promise<string> {
// Sanitize endpoint base URL (ensure it has /v1 if missing/standard, but respect raw input)
let finalUrl = endpoint.trim();
if (finalUrl.endsWith("/")) {
finalUrl = finalUrl.slice(0, -1);
}
if (!finalUrl.includes("/chat/completions")) {
finalUrl = `${finalUrl}/v1/chat/completions`;
}
const messages: any[] = [];
if (options?.systemInstruction) {
messages.push({ role: "system", content: options.systemInstruction });
}
messages.push({ role: "user", content: prompt });
const payload: any = {
model,
messages,
temperature: 0.1, // low temperature for precise JSON matching
};
if (options?.responseSchema) {
if (this.config.provider !== "lm-studio") {
payload.response_format = { type: "json_object" };
}
}
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (apiKey) {
headers["Authorization"] = `Bearer ${apiKey}`;
}
try {
const response = await fetch(finalUrl, {
method: "POST",
headers,
body: JSON.stringify(payload),
signal: options?.signal,
});
if (!response.ok) {
const errText = await response.text();
throw new Error(`API returned HTTP ${response.status}: ${errText}`);
}
const resJson = await response.json();
return resJson?.choices?.[0]?.message?.content || "{}";
} catch (e: any) {
console.error("[AIProvider] Call to OpenAI Compatible endpoint failed:", e.message || e);
throw new Error(`Connection to provider endpoint [${endpoint}] failed: ${e.message}`);
}
}
/**
* Abstracted method to generate embeddings.
*/
public async generateEmbedding(text: string): Promise<number[]> {
const provider = this.config.embeddingProvider;
console.log(`[AIProvider] Running Embedding Generation [Provider: ${provider}]`);
try {
if (provider === "gemini") {
const apiKey = this.config.embeddingConfig?.apiKey || this.config.geminiConfig?.apiKey || process.env.GEMINI_API_KEY;
const model = this.config.embeddingConfig?.model || "gemini-embedding-2-preview";
if (!apiKey) {
throw new Error("Embedding Gemini API key is missing.");
}
const client = new GoogleGenAI({
apiKey,
httpOptions: {
headers: {
"User-Agent": "aistudio-build",
},
},
});
const response = await client.models.embedContent({
model,
contents: text,
});
const embedRes: any = response;
const values = embedRes.embedding?.values || embedRes.embeddings?.[0]?.values || embedRes.embeddings?.values;
if (values && Array.isArray(values)) {
return values;
}
throw new Error("Empty embedding vector returned from Gemini.");
} else {
// Local or Custom Embedding Provider
let endpoint = "";
let apiKey = "";
let model = "";
if (provider === "local") {
endpoint = rewriteLocalhost(this.config.lmStudioConfig?.endpoint || "http://localhost:1234") || "";
model = this.config.embeddingConfig?.model || "nomic-embed-text";
} else {
endpoint = rewriteLocalhost(this.config.embeddingConfig?.endpoint || this.config.customConfig?.endpoint || "") || "";
apiKey = this.config.embeddingConfig?.apiKey || this.config.customConfig?.apiKey || "";
model = this.config.embeddingConfig?.model || "custom-embedding-model";
}
let finalUrl = endpoint.trim();
if (finalUrl.endsWith("/")) {
finalUrl = finalUrl.slice(0, -1);
}
if (!finalUrl.includes("/embeddings")) {
finalUrl = `${finalUrl}/v1/embeddings`;
}
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (apiKey) {
headers["Authorization"] = `Bearer ${apiKey}`;
}
const response = await fetch(finalUrl, {
method: "POST",
headers,
body: JSON.stringify({
input: text,
model,
}),
});
if (!response.ok) {
throw new Error(`Embedding API returned HTTP ${response.status}`);
}
const json = await response.json();
const embedding = json?.data?.[0]?.embedding;
if (embedding && Array.isArray(embedding)) {
return embedding;
}
throw new Error("No embedding values found in response payload.");
}
} catch (e: any) {
console.warn(`[AIProvider] Embedding failed (${e.message || e}), falling back to deterministic safe embedding vectors.`);
// Return beautiful, safe, deterministic vector so matching continues to behave correctly
return getDeterministicMockEmbedding(text);
}
}
/**
* Classify a node into hierarchy Level 1, 2, or 3 based on parent context
*/
public async classifyNode(nodeLabel: string, otherNodesText: string): Promise<string> {
const systemPrompt = `You are a taxonomy classifier. Based on the surrounding knowledge graph, classify the node label into Level 1 (initiative/program/system container), Level 2 (phase, milestone, domain, workstream), or Level 3 (atomic deliverable, meeting, log output, report document). Return ONLY the single number: 1, 2, or 3.`;
const prompt = `Node Label to classify: "${nodeLabel}"\nSurrounding contexts:\n${otherNodesText}`;
try {
const response = await this.generateResponse(prompt, { systemInstruction: systemPrompt });
const cleaned = response.trim();
const numMatch = cleaned.match(/[123]/);
return numMatch ? numMatch[0] : "2"; // fallback to Level 2
} catch (e) {
console.warn("[AIProvider] classifyNode failed, using regex classification fallback.");
return "2";
}
}
/**
* Summarize a work log or a note in 1 sentence
*/
public async summarizeMemory(memoryText: string): Promise<string> {
const prompt = `Please summarize the following work log or daily journal entry in a single clean, concise, elegant, first-person sentence:\n"${memoryText}"`;
try {
const summary = await this.generateResponse(prompt, {
systemInstruction: "You are the compact double of a developer's brain. Write a clean 1-sentence first-person summary of the event."
});
return summary.trim().replace(/^"/, "").replace(/"$/, "");
} catch (e) {
console.warn("[AIProvider] summarizeMemory failed, using raw truncation.");
return memoryText.length > 80 ? memoryText.substring(0, 80) + "..." : memoryText;
}
}
}