forked from LIT-Protocol/policy-AI-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.ts
55 lines (47 loc) · 1.75 KB
/
route.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
import { NextResponse } from 'next/server';
import { getPkpSessionSigs } from '@/app/utils';
import { LitNodeClient } from '@lit-protocol/lit-node-client';
import { LIT_NETWORK } from '@lit-protocol/constants';
import { aiActionCode } from '../../LitActions/aiAction';
const LIT_PKP_PUBLIC_KEY = process.env.NEXT_PUBLIC_LIT_PKP_PUBLIC_KEY;
export async function POST(request: Request) {
try {
const { metrics } = await request.json();
const decision = await makeSwapDecision(metrics);
console.log("🔄 Decision:", decision);
return NextResponse.json({
success: true,
decision: decision
});
} catch (error) {
console.error('AI decision failed:', error);
return NextResponse.json({
success: false,
error: 'Failed to make AI decision'
});
}
}
async function makeSwapDecision(metrics: any) {
console.log("🔄 Connecting to Lit network...");
const litNodeClient = new LitNodeClient({
litNetwork: LIT_NETWORK.DatilDev,
debug: false,
});
await litNodeClient.connect();
console.log("✅ Connected to Lit network");
const sessionSigs = await getPkpSessionSigs(litNodeClient);
console.log("🔄 Executing AI Lit Action...");
const litActionResponse = await litNodeClient.executeJs({
sessionSigs: sessionSigs,
code: aiActionCode,
jsParams: {
publicKey: LIT_PKP_PUBLIC_KEY!,
sigName: "sig",
metrics: metrics,
amount_threshold: process.env.AMOUNT_THRESHOLD,
apiKey: process.env.OPENAI_API_KEY
},
});
console.log("✅ Executed AI Lit Action");
return JSON.parse(litActionResponse.response as string);
}