import { GoogleGenAI } from "@google/genai"; const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY!, httpOptions: { headers: { 'User-Agent': 'aistudio-build', } } }); export interface AgentTask { id: string; inquiry: string; context: object; } export interface AgentResult { taskId: string; synthesis: string; status: 'success' | 'failure'; } export async function dispatchAgentTask(task: AgentTask): Promise { const MAX_RETRIES = 3; for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) { try { console.warn(`[AGENT_RUNTIME] Dispatching task ${task.id} (Attempt ${attempt}): ${task.inquiry}`); // Simulate high-fidelity synthesis using Gemini const systemInstruction = "You are a distributed agent runtime executor. Synthesize information accurately."; const response = await ai.models.generateContent({ model: "gemini-3.5-flash", contents: `Perform task ${task.id}: ${task.inquiry}. Context: ${JSON.stringify(task.context)}`, config: { systemInstruction, }, }); return { taskId: task.id, synthesis: response.candidates?.[0]?.content?.parts?.[0]?.text || "No synthesis returned.", status: 'success', }; } catch (error) { if (attempt === MAX_RETRIES) { console.error(`[AGENT_RUNTIME] Execution error in task ${task.id}:`, error); return { taskId: task.id, synthesis: error instanceof Error ? error.message : String(error), status: 'failure', }; } console.warn(`[AGENT_RUNTIME] Retrying task ${task.id} (Attempt ${attempt}) due to error: ${error}`); await new Promise(resolve => setTimeout(resolve, attempt * 1000)); // Simple linear backoff } } return { taskId: task.id, synthesis: 'Failed after retries', status: 'failure' }; }