Update geminiService.ts

This commit is contained in:
fcarraUniSa
2026-02-17 11:26:56 +01:00
committed by GitHub
parent 30d7a22eef
commit 4358a1a580

View File

@@ -36,7 +36,8 @@ export const getSupportResponse = async (
chatHistory: string[], chatHistory: string[],
knowledgeBase: KBArticle[], knowledgeBase: KBArticle[],
provider: AiProvider = AiProvider.GEMINI, provider: AiProvider = AiProvider.GEMINI,
model: string = 'gemini-3-flash-preview' model: string = 'gemini-3-flash-preview',
customConfig: { agentName?: string; customPrompt?: string } = {}
): Promise<string> => { ): Promise<string> => {
if (!apiKey) { if (!apiKey) {
return "L'assistente AI non è configurato (API Key mancante). Contatta l'amministratore."; return "L'assistente AI non è configurato (API Key mancante). Contatta l'amministratore.";
@@ -45,28 +46,33 @@ export const getSupportResponse = async (
// Prepare Context from KB // Prepare Context from KB
const kbContext = knowledgeBase.map(a => { const kbContext = knowledgeBase.map(a => {
if (a.type === 'url') { if (a.type === 'url') {
return `Fonte Esterna [${a.category}]: ${a.title} - URL: ${a.url}\nContenuto Estratto: ${a.content}`; return `Fonte Esterna [${a.category}] (${a.visibility}): ${a.title} - URL: ${a.url}\nContenuto Estratto: ${a.content}`;
} }
return `Articolo [${a.category}]: ${a.title}\nContenuto: ${a.content}`; return `Articolo [${a.category}] (${a.visibility}): ${a.title}\nContenuto: ${a.content}`;
}).join('\n\n'); }).join('\n\n');
const agentName = customConfig.agentName || "OmniSupport AI";
const extraPrompt = customConfig.customPrompt ? `ISTRUZIONI SPECIFICHE AGGIUNTIVE:\n${customConfig.customPrompt}` : "";
const systemInstructionText = ` const systemInstructionText = `
Sei "OmniSupport AI", un assistente clienti virtuale globale. Sei "${agentName}", un assistente clienti virtuale.
${extraPrompt}
IL TUO COMPITO: IL TUO COMPITO:
Rispondere alle domande dei clienti basandoti ESCLUSIVAMENTE sulla seguente Base di Conoscenza (KB) fornita in ITALIANO. Rispondere alle domande dei clienti basandoti ESCLUSIVAMENTE sulla seguente Base di Conoscenza (KB).
GESTIONE LINGUA (IMPORTANTE): GESTIONE LINGUA (IMPORTANTE):
1. Rileva automaticamente la lingua utilizzata dall'utente nel suo ultimo messaggio. 1. Rileva automaticamente la lingua utilizzata dall'utente nel suo ultimo messaggio.
2. Anche se la KB è in Italiano, devi tradurre mentalmente la richiesta, cercare la risposta nella KB Italiana, e poi RISPONDERE NELLA LINGUA DELL'UTENTE. 2. Rispondi SEMPRE nella lingua dell'utente.
BASE DI CONOSCENZA (ITALIANO): BASE DI CONOSCENZA:
${kbContext} ${kbContext}
REGOLE: REGOLE:
1. Se la risposta è nella KB, forniscila. 1. Se la risposta è nella KB (che sia 'public' o 'internal'), usala per formulare la risposta. Non menzionare se l'articolo è interno o pubblico, usa solo l'informazione.
2. Se l'articolo è una fonte web (URL), usa il "Contenuto Estratto" per rispondere e fornisci anche il link originale all'utente. 2. Se l'articolo è una fonte web (URL), usa il "Contenuto Estratto" per rispondere e fornisci anche il link originale all'utente.
3. Se la risposta NON si trova nella KB, ammettilo gentilmente (nella lingua dell'utente) e consiglia di aprire un ticket. 3. Se la risposta NON si trova nella KB, ammettilo gentilmente e consiglia di aprire un ticket.
4. Sii cortese, professionale e sintetico. 4. Sii cortese, professionale e sintetico.
`; `;
@@ -118,8 +124,8 @@ export const generateNewKBArticle = async (
): Promise<Array<{ title: string; content: string; category: string }> | null> => { ): Promise<Array<{ title: string; content: string; category: string }> | null> => {
if (!apiKey) return null; if (!apiKey) return null;
// Filter only resolved tickets // Filter only resolved tickets that haven't been analyzed yet
const relevantTickets = resolvedTickets.filter(t => t.status === TicketStatus.RESOLVED); const relevantTickets = resolvedTickets.filter(t => t.status === TicketStatus.RESOLVED && !t.hasBeenAnalyzed);
if (relevantTickets.length === 0) return null; if (relevantTickets.length === 0) return null;