-
Notifications
You must be signed in to change notification settings - Fork 4
/
atonomous-agent.ts
60 lines (53 loc) · 1.51 KB
/
atonomous-agent.ts
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
import { getYellowstoneChainMetrics } from "./utils";
import {
humanVerification,
signAndBroadcastTransaction,
} from "./agent-helpers";
export async function autonomousAgent() {
try {
const metrics = await getYellowstoneChainMetrics();
const response = await fetch("/api/ai-decision", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ metrics }),
});
const data = await response.json();
if (!data.success) {
throw new Error("Failed to get AI decision");
}
const { decision } = data;
if (decision.shouldTransact) {
if (decision.requiresVerification) {
console.log("Amount exceeds threshold - requesting human verification");
console.log(decision.reasoning);
await humanVerification(parseFloat(decision.amount));
return decision;
}
console.log(
"AI Agent initiating direct transaction:",
decision.reasoning
);
const txHash = await signAndBroadcastTransaction(
false,
undefined,
decision.amount
);
console.log("Transaction completed with hash:", txHash);
return decision;
}
} catch (error) {
console.error("AI agent error:", error);
throw error;
}
}
export async function startAutonomousAgent() {
setInterval(async () => {
try {
await autonomousAgent();
} catch (error) {
console.error("Autonomous cycle error:", error);
}
}, 1 * 60 * 1000); // Run every minute
}