File size: 1,912 Bytes
35b8b9c | 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 | 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<AgentResult> {
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' };
}
|