-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.config.ts
More file actions
284 lines (241 loc) · 10.7 KB
/
agent.config.ts
File metadata and controls
284 lines (241 loc) · 10.7 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import "dotenv/config";
import Anthropic from "@anthropic-ai/sdk";
import { MCPRegistry } from "@core/mcp/MCPRegistry";
const apiKey = process.env.ANTHROPIC_API_KEY;
// Only initialize if key exists, otherwise we might want to handle gracefully in the app
// provided this is running in a server environment that has the env.
const anthropic = apiKey ? new Anthropic({ apiKey }) : null;
const SYSTEM_PROMPT = `
You are a banking assistant for Jumpa. Your job is to facilitate withdrawals and blockchain actions.
RESPONSE RULES:
- Be extremely concise
- NO explanations, NO reasoning, NO thinking out loud
- When asking for information, ask ONLY the question
IMAGE PROCESSING:
- If the user provides an image, scan it for withdrawal details:
- Account Number
- Bank Name
- Account Name
- Amount (if visible)
- Extract chain and currency from the user's message (e.g., "send from my usdc on sol" means SOLANA and USDC)
- If user sends MULTIPLE images simultaneously, treat as bulk transfer (extract details from each image)
BANK TRANSFER WORKFLOW:
1. Extract account number, bank name, amount from image
2. Call 'validate_withdrawal_details' to verify the account (this returns the verified account_name)
3. Extract chain and currency from the user's text message
4. Once you have ALL details (amount, account_number, bank_name, account_name, chain, currency):
→ IMMEDIATELY call 'confirm_withdrawal' tool
→ DO NOT ask for user confirmation before calling the tool
→ The tool will trigger the PIN flow automatically
5. If any details are missing, ask ONLY for the missing ones
BULK TRANSFER WORKFLOW:
1. Detect bulk intent: Multiple images OR user says "send to each" OR lists multiple accounts
2. Extract details for EACH recipient (up to 5 maximum)
3. Validate ALL accounts in parallel using 'validate_withdrawal_details'
4. Extract common chain and currency from user's message
5. Once you have ALL recipient details:
→ IMMEDIATELY call 'confirm_bulk_withdrawal' tool with recipients array
→ Each recipient can have different amount
→ DO NOT ask for confirmation before calling the tool
6. The tool will trigger bulk PIN flow with summary of all transfers
SUPPORTED MODES:
1. **BANK TRANSFER**: User wants to send money to a Nigerian Bank Account.
- Destination: Bank Name + Account Number.
- Source: Crypto Chain + Currency.
- Amount: Naira (NGN).
2. **CRYPTO TRANSFER**: User wants to send crypto to an external wallet address.
- Destination: Wallet Address.
- Source: Crypto Chain + Currency.
- Amount: Naira (NGN).
3. **AMADEUS ACTIONS**: General blockchain actions (send tokens, read state, claim testnet tokens) on Amadeus chain.
- Use provided MCP tools.
- **CRITICAL PROTOCOL FOR TRANSACTIONS**:
A. Call tool to CREATE transaction (e.g. 'create_transaction').
B. This tool will return a 'signing_payload' and 'blob'.
C. STOP. Do NOT try to sign it yourself.
D. Return the payload to the user by explicitly stating: "Transaction created. Please sign."
E. Wait for user to provide signature.
F. Once you receive signature + blob, call 'submit_transaction'.
- **CRITICAL RULES FOR AMA TRANSFERS**:
To transfer AMA tokens, use 'create_transaction' with these EXACT parameters:
1. signer: the sender's address
2. contract: "Coin" (exactly this string, NOT a hex address)
3. function: "transfer"
4. args: an array with exactly 3 elements:
- [0]: {"b58": "RECIPIENT_ADDRESS"} (object with b58 key)
- [1]: "AMOUNT_IN_BASE_UNITS" (string, e.g., "10000000000" for 10 AMA. 1 AMA = 1,000,000,000 base units)
- [2]: "AMA" (the token symbol)
Example: {"signer": "...", "contract": "Coin", "function": "transfer", "args": [{"b58":"RECIPIENT"},"10000000000","AMA"]}
DO NOT set attached_symbol or attached_amount for standard transfers.
INTERACTION FLOWS:
[Amadeus Transfer]
User: "Send 10 AMA to [Address]"
-> Agent: Calls 'create_transaction' with EXACT parameters above (Coin, transfer, args)
-> Tool: Returns { signing_payload: "...", blob: "..." }
-> Agent: "Transaction ready. Please sign." (Stops)
-> User: (Signs via UI) -> Returns Signature
-> Agent: Calls 'submit_transaction'
-> Agent: Calls 'submit_transaction'
-> Tool: Returns TxHash
-> Agent: "✅ Transaction Successful: \n\n [TxHash] \n\n [View in Explorer](https://testnet.explorer.ama.one/network/tx/[TxHash])"
(ALWAYS include the explorer link for Amadeus transactions. Default to testnet explorer unless mainnet specified).
`;
export interface AgentResponse {
type: "text" | "confirmation" | "bulk_confirmation" | "error" | "signature_request";
message?: string;
data?: any;
updatedHistory?: any[];
}
export async function processUserQuery(
userId: number,
userMessage: string | Array<any>,
previousHistory: any[] = []
): Promise<AgentResponse> {
if (!anthropic) {
console.error("Anthropic API Key missing");
return { type: "error", message: "AI Service Not Configured" };
}
// Input validation for text-only messages
const MAX_MESSAGE_LENGTH = 10000;
if (typeof userMessage === 'string' && userMessage.length > MAX_MESSAGE_LENGTH) {
return { type: "error", message: "Message too long. Please keep it under 10000 characters." };
}
if (typeof userMessage === 'string') {
const BLOCKED_PATTERNS = [/\<script\>/i, /javascript:/i];
for (const pattern of BLOCKED_PATTERNS) {
if (pattern.test(userMessage)) {
return { type: "error", message: "Invalid input." };
}
}
}
try {
// Dynamic Tool Loading
const tools = await MCPRegistry.getInstance().getAllTools();
// Start with previous history and append the new user message
let messages: any[] = [...previousHistory];
// Add new message correctly based on type
if (Array.isArray(userMessage)) {
// It's a multimodal message (e.g. text + image)
messages.push({ role: "user", content: userMessage });
} else {
// It's a simple text string
messages.push({ role: "user", content: userMessage });
}
// Run the loop (max 5 turns to prevent infinite loops)
for (let i = 0; i < 5; i++) {
const response = await anthropic.messages.create({
model: "claude-sonnet-4-5-20250929",
max_tokens: 1024,
system: SYSTEM_PROMPT,
tools: tools,
messages: messages,
});
const stopReason = response.stop_reason;
const content = response.content;
console.log(`[AI Agent] Turn ${i + 1} Response: StopReason=${stopReason}`, JSON.stringify(content));
// Append assistant's response to history
messages.push({ role: "assistant", content: content });
if (stopReason === "tool_use") {
const toolResults = [];
for (const block of content) {
if (block.type === "tool_use") {
const toolName = block.name;
const toolInput = block.input;
const toolId = block.id;
console.log(`[AI Agent] Invoking tool: ${toolName}`, toolInput);
// 1. Handle Withdrawal Confirmation (Bank/Crypto)
if (toolName === "confirm_withdrawal") {
return {
type: "confirmation",
data: toolInput,
updatedHistory: messages
};
}
// 2. Handle Bulk Withdrawal Confirmation
if (toolName === "confirm_bulk_withdrawal") {
return {
type: "bulk_confirmation",
data: toolInput,
updatedHistory: messages
};
}
// 3. Execute Generic/MCP Tool
try {
const result = await MCPRegistry.getInstance().executeTool(toolName, toolInput);
// 3. Handle Signature Request Interception
// Check if result is standard MCP format with content list
let signingData = result;
// If result has content array, try to parse the first text block
if (result && result.content && Array.isArray(result.content)) {
const textBlock = result.content.find((c: any) => c.type === 'text');
if (textBlock && textBlock.text) {
try {
const parsed = JSON.parse(textBlock.text);
if (parsed && parsed.signing_payload && parsed.blob) {
signingData = parsed;
}
} catch (e) {
// Not JSON, ignore
}
}
}
// If the tool returns a signing payload (direct or parsed), we must pause and ask user to sign.
if (signingData && signingData.signing_payload && signingData.blob) {
// IMPORTANT: append the tool result to history so the conversation is valid (Tool Use -> Tool Result)
messages.push({
role: "user",
content: [{
type: "tool_result",
tool_use_id: toolId,
content: JSON.stringify(result)
}]
});
return {
type: "signature_request",
data: {
toolName,
payload: signingData.signing_payload,
blob: signingData.blob,
rawResult: result
},
message: "Please sign this transaction to proceed.",
updatedHistory: messages
};
}
toolResults.push({
type: "tool_result",
tool_use_id: toolId,
content: JSON.stringify(result)
});
} catch (err: any) {
toolResults.push({
type: "tool_result",
tool_use_id: toolId,
content: JSON.stringify({ error: err.message }),
is_error: true
});
}
}
}
console.log(`[AI Agent] Tool Results:`, JSON.stringify(toolResults));
messages.push({ role: "user", content: toolResults });
// Continue loop to let Claude process tool results
} else {
// Text response
const textBlock = content.find(c => c.type === "text");
if (textBlock && textBlock.text) {
return {
type: "text",
message: textBlock.text,
updatedHistory: messages
};
}
return { type: "error", message: "No response from AI" };
}
}
return { type: "error", message: "Agent loop limit reached" };
} catch (error: any) {
console.error("[AI Agent] Process Error:", error);
return { type: "error", message: "Sorry, I encountered an error processing your request." };
}
}