Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- services/geminiService.ts +47 -35
services/geminiService.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
import { GoogleGenAI, GenerateContentResponse } from "@google/genai";
|
| 3 |
import { SimulationResult, AIInsight } from "../types/index";
|
| 4 |
|
| 5 |
export const TTS_VOICES = [
|
|
@@ -22,100 +21,113 @@ export const TTS_LANGUAGES = [
|
|
| 22 |
];
|
| 23 |
|
| 24 |
const TEXT_MODEL = 'gemini-3-flash-preview';
|
| 25 |
-
const IMAGE_MODEL = 'gemini-2.5-flash-image';
|
| 26 |
-
|
| 27 |
-
const getAI = () => {
|
| 28 |
-
const apiKey = localStorage.getItem('LQI_API_KEY') || process.env.API_KEY || process.env.GEMINI_API_KEY;
|
| 29 |
-
if (!apiKey) throw new Error("API_KEY_MISSING");
|
| 30 |
-
return new GoogleGenAI({ apiKey });
|
| 31 |
-
};
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
export const callGemini = async (model: string, contents: any, config: any = {}) => {
|
| 34 |
-
const ai =
|
| 35 |
-
const response
|
| 36 |
model: model || TEXT_MODEL,
|
| 37 |
-
contents: typeof contents === 'string' ? contents : contents,
|
| 38 |
config: config,
|
| 39 |
});
|
| 40 |
return response;
|
| 41 |
};
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
export const synthesizeSpeech = async (params: {
|
| 44 |
text: string;
|
| 45 |
voiceName: string;
|
| 46 |
directorNotes?: string;
|
| 47 |
multiSpeaker?: any;
|
| 48 |
}) => {
|
| 49 |
-
|
| 50 |
-
return true;
|
| 51 |
-
};
|
| 52 |
-
|
| 53 |
-
export const speakText = async (text: string) => {
|
| 54 |
-
console.log("Neural Audio Signal: ", text);
|
| 55 |
-
return true;
|
| 56 |
};
|
| 57 |
|
| 58 |
export const processVoiceCommand = async (command: string) => {
|
| 59 |
try {
|
| 60 |
-
const ai =
|
| 61 |
const response = await ai.models.generateContent({
|
| 62 |
model: TEXT_MODEL,
|
| 63 |
-
contents: `
|
| 64 |
config: { responseMimeType: "application/json" }
|
| 65 |
});
|
| 66 |
return JSON.parse(response.text || '{}');
|
| 67 |
} catch (error) {
|
| 68 |
-
|
|
|
|
| 69 |
}
|
| 70 |
};
|
| 71 |
|
| 72 |
export const getFinancialAdviceStream = async (query: string, context: any) => {
|
| 73 |
-
const ai =
|
| 74 |
const response = await ai.models.generateContent({
|
| 75 |
model: TEXT_MODEL,
|
| 76 |
-
contents: `Context: ${JSON.stringify(context)}. User: ${query}
|
| 77 |
});
|
| 78 |
return [{ text: response.text }];
|
| 79 |
};
|
| 80 |
|
| 81 |
export const getSystemIntelligenceFeed = async (): Promise<AIInsight[]> => {
|
| 82 |
try {
|
| 83 |
-
const ai =
|
| 84 |
const response = await ai.models.generateContent({
|
| 85 |
model: TEXT_MODEL,
|
| 86 |
-
contents: "Generate 4
|
| 87 |
config: { responseMimeType: "application/json" }
|
| 88 |
});
|
| 89 |
return JSON.parse(response.text || '[]');
|
| 90 |
} catch {
|
| 91 |
-
return [{ id: '1', title: "
|
| 92 |
}
|
| 93 |
};
|
| 94 |
|
| 95 |
export const runSimulationForecast = async (prompt: string): Promise<SimulationResult> => {
|
| 96 |
try {
|
| 97 |
-
const ai =
|
| 98 |
const response = await ai.models.generateContent({
|
| 99 |
model: TEXT_MODEL,
|
| 100 |
-
contents:
|
| 101 |
config: { responseMimeType: "application/json" }
|
| 102 |
});
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
}
|
| 107 |
};
|
| 108 |
|
| 109 |
export const getPortfolioSuggestions = async (context: any) => {
|
| 110 |
try {
|
| 111 |
-
const ai =
|
| 112 |
const response = await ai.models.generateContent({
|
| 113 |
model: TEXT_MODEL,
|
| 114 |
-
contents: `Suggest 3
|
| 115 |
config: { responseMimeType: "application/json" }
|
| 116 |
});
|
| 117 |
return JSON.parse(response.text || '[]');
|
| 118 |
} catch {
|
| 119 |
return [];
|
| 120 |
}
|
| 121 |
-
};
|
|
|
|
| 1 |
+
import { GoogleGenAI } from "@google/genai";
|
|
|
|
| 2 |
import { SimulationResult, AIInsight } from "../types/index";
|
| 3 |
|
| 4 |
export const TTS_VOICES = [
|
|
|
|
| 21 |
];
|
| 22 |
|
| 23 |
const TEXT_MODEL = 'gemini-3-flash-preview';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
/**
|
| 26 |
+
* Direct call to Gemini 3 Flash.
|
| 27 |
+
* SDK rule: Fresh instantiation within the call captures the most recent process.env.API_KEY.
|
| 28 |
+
*/
|
| 29 |
export const callGemini = async (model: string, contents: any, config: any = {}) => {
|
| 30 |
+
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
|
| 31 |
+
const response = await ai.models.generateContent({
|
| 32 |
model: model || TEXT_MODEL,
|
| 33 |
+
contents: typeof contents === 'string' ? [{ parts: [{ text: contents }] }] : contents,
|
| 34 |
config: config,
|
| 35 |
});
|
| 36 |
return response;
|
| 37 |
};
|
| 38 |
|
| 39 |
+
export const speakText = async (text: string) => {
|
| 40 |
+
return new Promise((resolve) => {
|
| 41 |
+
if (!('speechSynthesis' in window)) {
|
| 42 |
+
console.warn("Synthesis unsupported.");
|
| 43 |
+
resolve(false);
|
| 44 |
+
return;
|
| 45 |
+
}
|
| 46 |
+
window.speechSynthesis.cancel();
|
| 47 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
| 48 |
+
utterance.rate = 1.0;
|
| 49 |
+
utterance.pitch = 0.9;
|
| 50 |
+
utterance.onend = () => resolve(true);
|
| 51 |
+
window.speechSynthesis.speak(utterance);
|
| 52 |
+
});
|
| 53 |
+
};
|
| 54 |
+
|
| 55 |
export const synthesizeSpeech = async (params: {
|
| 56 |
text: string;
|
| 57 |
voiceName: string;
|
| 58 |
directorNotes?: string;
|
| 59 |
multiSpeaker?: any;
|
| 60 |
}) => {
|
| 61 |
+
return speakText(params.text);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
};
|
| 63 |
|
| 64 |
export const processVoiceCommand = async (command: string) => {
|
| 65 |
try {
|
| 66 |
+
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
|
| 67 |
const response = await ai.models.generateContent({
|
| 68 |
model: TEXT_MODEL,
|
| 69 |
+
contents: [{ parts: [{ text: `You are an institutional treasury AI. Analyze: "${command}". Return ONLY JSON: { "action": "SEND_MONEY" | "QUERY", "amount": number, "recipient": string, "category": string, "narration": string }.` }] }],
|
| 70 |
config: { responseMimeType: "application/json" }
|
| 71 |
});
|
| 72 |
return JSON.parse(response.text || '{}');
|
| 73 |
} catch (error) {
|
| 74 |
+
console.error("Neural Signal Loss.");
|
| 75 |
+
return { action: "ERROR", narration: "Registry parity error." };
|
| 76 |
}
|
| 77 |
};
|
| 78 |
|
| 79 |
export const getFinancialAdviceStream = async (query: string, context: any) => {
|
| 80 |
+
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
|
| 81 |
const response = await ai.models.generateContent({
|
| 82 |
model: TEXT_MODEL,
|
| 83 |
+
contents: [{ parts: [{ text: `System Context: ${JSON.stringify(context)}. User Query: ${query}` }] }],
|
| 84 |
});
|
| 85 |
return [{ text: response.text }];
|
| 86 |
};
|
| 87 |
|
| 88 |
export const getSystemIntelligenceFeed = async (): Promise<AIInsight[]> => {
|
| 89 |
try {
|
| 90 |
+
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
|
| 91 |
const response = await ai.models.generateContent({
|
| 92 |
model: TEXT_MODEL,
|
| 93 |
+
contents: [{ parts: [{ text: "Generate 4 corporate treasury alerts. Return ONLY JSON: [{title, description, severity: 'INFO'|'CRITICAL'}]" }] }],
|
| 94 |
config: { responseMimeType: "application/json" }
|
| 95 |
});
|
| 96 |
return JSON.parse(response.text || '[]');
|
| 97 |
} catch {
|
| 98 |
+
return [{ id: '1', title: "Mesh Parity Stable", description: "All nodes performing within 0.001ms delta.", severity: "INFO" }];
|
| 99 |
}
|
| 100 |
};
|
| 101 |
|
| 102 |
export const runSimulationForecast = async (prompt: string): Promise<SimulationResult> => {
|
| 103 |
try {
|
| 104 |
+
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
|
| 105 |
const response = await ai.models.generateContent({
|
| 106 |
model: TEXT_MODEL,
|
| 107 |
+
contents: [{ parts: [{ text: `Simulate: "${prompt}". Return ONLY JSON: { "outcomeNarrative": string, "projectedValue": number, "confidenceScore": number, "status": string, "simulationId": string, "keyRisks": string[] }.` }] }],
|
| 108 |
config: { responseMimeType: "application/json" }
|
| 109 |
});
|
| 110 |
+
const result = JSON.parse(response.text || '{}');
|
| 111 |
+
return {
|
| 112 |
+
simulationId: `SIM-${Math.floor(Math.random() * 9000) + 1000}`,
|
| 113 |
+
status: 'SUCCESS',
|
| 114 |
+
...result
|
| 115 |
+
};
|
| 116 |
+
} catch (error) {
|
| 117 |
+
return { outcomeNarrative: "Forecast link failed.", projectedValue: 0, confidenceScore: 0, status: 'ERROR', simulationId: 'ERR-01' };
|
| 118 |
}
|
| 119 |
};
|
| 120 |
|
| 121 |
export const getPortfolioSuggestions = async (context: any) => {
|
| 122 |
try {
|
| 123 |
+
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
|
| 124 |
const response = await ai.models.generateContent({
|
| 125 |
model: TEXT_MODEL,
|
| 126 |
+
contents: [{ parts: [{ text: `Suggest 3 actions: ${JSON.stringify(context)}. Return ONLY JSON: [{type: 'ALPHA'|'RISK'|'LIQUIDITY', title, description}]` }] }],
|
| 127 |
config: { responseMimeType: "application/json" }
|
| 128 |
});
|
| 129 |
return JSON.parse(response.text || '[]');
|
| 130 |
} catch {
|
| 131 |
return [];
|
| 132 |
}
|
| 133 |
+
};
|