-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.js
More file actions
347 lines (304 loc) · 14 KB
/
Copy pathagent.js
File metadata and controls
347 lines (304 loc) · 14 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import dotenv from 'dotenv';
dotenv.config();
import { AgentMode } from '@hashgraph/hedera-agent-kit';
import {
coreAccountPluginToolNames,
coreAccountPlugin,
coreAccountQueryPluginToolNames,
coreAccountQueryPlugin,
} from '@hashgraph/hedera-agent-kit/plugins';
import { HederaLangchainToolkit, ResponseParserService } from '@hashgraph/hedera-agent-kit-langchain';
import { Client, PrivateKey } from '@hiero-ledger/sdk';
import prompts from 'prompts';
import { createAgent } from 'langchain';
import { MemorySaver } from '@langchain/langgraph';
import { ChatGroq } from '@langchain/groq';
import { loadConfig } from './config.js';
import {
createGameState,
buildReceipt,
updateStats,
formatStats,
formatReceipts,
buildClosingMessage,
} from './gameState.js';
async function bootstrap() {
const config = loadConfig();
function getHederaClient() {
if (config.network === 'mainnet') return Client.forMainnet();
if (config.network === 'previewnet') return Client.forPreviewnet();
return Client.forTestnet();
}
function parsePrivateKey(keyStr) {
try { return PrivateKey.fromStringDer(keyStr); } catch (_) {}
try { return PrivateKey.fromStringECDSA(keyStr); } catch (_) {}
try { return PrivateKey.fromStringED25519(keyStr); } catch (_) {}
throw new Error('Could not parse private key — check format');
}
const operatorKey = parsePrivateKey(config.operatorPrivateKey);
let prizePoolKey = null;
if (config.prizePoolPrivateKey) {
try { prizePoolKey = parsePrivateKey(config.prizePoolPrivateKey); } catch (_) {
console.warn('Warning: PRIZE_POOL_PRIVATE_KEY could not be parsed.');
}
}
const client = getHederaClient().setOperator(config.operatorAccountId, operatorKey);
const { TRANSFER_HBAR_TOOL } = coreAccountPluginToolNames;
const { GET_HBAR_BALANCE_QUERY_TOOL } = coreAccountQueryPluginToolNames;
// Build toolkit when switching operators for payout
function buildToolkit(accountId) {
return new HederaLangchainToolkit({
client,
configuration: {
plugins: [coreAccountPlugin, coreAccountQueryPlugin],
tools: [TRANSFER_HBAR_TOOL, GET_HBAR_BALANCE_QUERY_TOOL],
context: { mode: AgentMode.AUTONOMOUS, accountId },
},
});
}
let toolkit = buildToolkit(config.operatorAccountId);
let responseParserService = new ResponseParserService(toolkit.getTools());
const memory = new MemorySaver();
const agentConfig = { configurable: { thread_id: 'hash-gordon-trivia' } };
const llm = new ChatGroq({ model: 'llama-3.1-8b-instant', apiKey: config.groqApiKey });
const systemPrompt = `You are Hash Gordon, an enthusiastic blockchain game-show host.
You have ONLY two tools: TRANSFER_HBAR and GET_HBAR_BALANCE.
When told to transfer HBAR, call TRANSFER_HBAR immediately with the exact amounts given.
When told to check balance, call GET_HBAR_BALANCE.
For ALL other questions, answer from your own knowledge — NEVER call any other tool.
Keep responses short, punny, and energetic.`;
let agent = createAgent({
model: llm,
tools: toolkit.getTools(),
checkpointer: memory,
stateModifier: systemPrompt,
});
// Switch the Hedera client operator and rebuild the agent with new toolkit
function switchOperator(accountId, key) {
client.setOperator(accountId, key);
toolkit = buildToolkit(accountId);
responseParserService = new ResponseParserService(toolkit.getTools());
agent = createAgent({
model: llm,
tools: toolkit.getTools(),
checkpointer: memory,
stateModifier: systemPrompt,
});
}
const gameState = createGameState();
let lastCompletedRound = null;
// Extract transaction ID from all possible locations in the response
function extractTxId(toolCall, result) {
if (!toolCall) return null;
const candidates = [
toolCall.transactionId,
toolCall.result?.transactionId,
toolCall.raw?.transactionId,
toolCall.parsedData?.raw?.transactionId,
toolCall.parsedData?.transactionId,
];
for (const c of candidates) {
if (c && typeof c === 'string') return c;
}
// Try extracting from tool message content
for (const msg of (result?.messages ?? [])) {
const content = typeof msg.content === 'string' ? msg.content : '';
const match = content.match(/\b(\d+\.\d+\.\d+@\d+\.\d+)\b/);
if (match) return match[1];
}
return null;
}
async function invokeAgent(userMessage) {
const result = await agent.invoke(
{ messages: [{ role: 'user', content: userMessage }] },
agentConfig,
);
const parsed = responseParserService.parseNewToolMessages(result);
const toolCall = parsed[0] ?? null;
const txId = extractTxId(toolCall, result);
let text = '';
for (let i = result.messages.length - 1; i >= 0; i--) {
const msg = result.messages[i];
const role = msg.role ?? msg._getType?.();
if (role === 'assistant' || role === 'ai') {
const content = typeof msg.content === 'string' ? msg.content : '';
if (content) { text = content; break; }
}
}
return { text, toolCall, txId };
}
// Question generation
async function generateQuestion() {
const topics = [
'Hedera Hashgraph consensus algorithm',
'Gossip about Gossip protocol',
'Asynchronous Byzantine Fault Tolerance (aBFT)',
'Hedera finality time (3-5 seconds)',
'HBAR tokenomics and total supply',
'Hedera Token Service (HTS)',
'Hedera Consensus Service (HCS)',
'Hedera Smart Contract Service (HSCS)',
'Hedera Governing Council structure',
'Hedera predictable fee structure',
'Hedera account ID format (shard.realm.num)',
'Hedera transaction ID format',
'Hedera EVM compatibility and JSON-RPC relay',
'Hedera mirror nodes and REST API',
'Hedera state proofs',
'Hedera carbon-negative sustainability',
'Hedera HIP process',
'Hedera scheduled transactions',
'Hedera token custom fees',
'Hedera ECDSA secp256k1 key support',
];
const topic = topics[Math.floor(Math.random() * topics.length)];
const result = await llm.invoke([{
role: 'user',
content: `Generate a multiple-choice trivia question about ${topic} on the Hedera network.
Key fact: Hedera fees are predictable and USD-denominated, NOT variable based on congestion.
Respond with ONLY valid JSON, no markdown:
{"text":"...","choices":{"A":"...","B":"...","C":"...","D":"..."},"answer":"A","explanation":"...","hint":"..."}`,
}]);
const raw = typeof result.content === 'string' ? result.content : JSON.stringify(result.content);
const match = raw.match(/\{[\s\S]*\}/);
if (!match) throw new Error('Could not parse question JSON');
return JSON.parse(match[0]);
}
function formatQuestion(q) {
return [`\n❓ ${q.text}`, ` A) ${q.choices.A}`, ` B) ${q.choices.B}`, ` C) ${q.choices.C}`, ` D) ${q.choices.D}`, ''].join('\n');
}
function evaluateAnswer(question, input) {
const n = input.trim().toUpperCase();
if (!['A', 'B', 'C', 'D'].includes(n)) return 'invalid';
return n === question.answer.toUpperCase() ? 'correct' : 'incorrect';
}
async function askYesNo(msg) {
const r = await prompts({ type: 'text', name: 'a', message: msg });
return /^(y|yes|yeah|yep|sure|ok|okay)/i.test((r.a ?? '').trim());
}
const puns = [
"Hash-tastic! You're on fire! 🔥",
"Absolutely chain-sational! 💎",
"You just mined that answer! ⛏️",
"Block by block, you're building a fortune! 🏗️",
"Cryptographically correct! 🔐",
];
// Welcome
console.log('\n🎰 ============================================ 🎰');
console.log(" Welcome to HASH GORDON'S BLOCKCHAIN BONANZA!");
console.log('🎰 ============================================ 🎰');
console.log(`\n💰 Entry fee: ${config.entryFeeHbar} HBAR | 🏆 Payout: ${config.payoutHbar} HBAR`);
console.log(`🏦 Prize Pool: ${config.prizePoolAccountId}`);
console.log('\nType anything to chat | "play" to start | "score" | "history" | "exit"\n');
// Main REPL
while (true) {
const r = await prompts({ type: 'text', name: 'input', message: 'You: ' });
if (!r.input) continue;
const input = r.input.trim();
const lower = input.toLowerCase();
if (['exit', 'quit'].includes(lower)) {
console.log('\n' + buildClosingMessage(gameState) + '\n');
break;
}
if (['score', 'stats'].includes(lower)) { console.log('\n' + formatStats(gameState) + '\n'); continue; }
if (lower === 'history') { console.log('\n' + formatReceipts(gameState) + '\n'); continue; }
if (gameState.currentRound?.awaitingAnswer) {
if (lower === 'hint') {
console.log(`\n💡 Hint: ${gameState.currentRound.question.hint}\n`);
continue;
}
const evalResult = evaluateAnswer(gameState.currentRound.question, input);
if (evalResult === 'invalid') { console.log('\n⚠️ Please answer A, B, C, or D.\n'); continue; }
const round = gameState.currentRound;
const isCorrect = evalResult === 'correct';
if (isCorrect) {
console.log('\n✅ CORRECT! ' + puns[Math.floor(Math.random() * puns.length)] + '\n');
let payoutTxId = null;
if (prizePoolKey) {
try {
console.log('🏆 Processing payout via agent...\n');
// Switch operator to prize pool so the agent signs with the right key
switchOperator(config.prizePoolAccountId, prizePoolKey);
const { text, toolCall, txId } = await invokeAgent(
`Transfer exactly ${config.payoutHbar} HBAR from ${config.prizePoolAccountId} to ${config.operatorAccountId} as the player's prize payout.`
);
// Restore operator back to player
switchOperator(config.operatorAccountId, operatorKey);
if (text) console.log('Hash Gordon: ' + text + '\n');
if (!toolCall) throw new Error('Agent did not execute payout transfer.');
payoutTxId = txId;
if (payoutTxId) console.log(`🏆 ${config.payoutHbar} HBAR sent! Tx: ${payoutTxId}\n`);
} catch (err) {
switchOperator(config.operatorAccountId, operatorKey); // always restore
console.log(`⚠️ Payout failed: ${err.message}\n`);
console.log('💡 Make sure the prize pool account is properly configured and has sufficient funds.\n');
}
} else {
console.log('⚠️ Add PRIZE_POOL_PRIVATE_KEY to .env to enable real payouts.\n');
}
const outcome = payoutTxId ? 'win' : 'unresolved';
const receipt = buildReceipt(round.question, outcome, config.payoutHbar, payoutTxId ?? round.entryFeeTxId);
gameState.receipts.push(receipt);
updateStats(gameState, outcome, config.entryFeeHbar, config.payoutHbar);
console.log(`🧾 RECEIPT | WIN🏆 | ${config.payoutHbar} HBAR | Tx: ${payoutTxId ?? round.entryFeeTxId}\n`);
} else {
const correctChoice = round.question.answer;
const correctText = round.question.choices[correctChoice];
console.log(`\n❌ Incorrect! The answer was ${correctChoice}) ${correctText}`);
console.log(`💡 ${round.question.explanation}\n`);
console.log(`🧾 RECEIPT | LOSS💸 | ${config.entryFeeHbar} HBAR | Tx: ${round.entryFeeTxId}\n`);
const receipt = buildReceipt(round.question, 'loss', config.entryFeeHbar, round.entryFeeTxId);
gameState.receipts.push(receipt);
updateStats(gameState, 'loss', config.entryFeeHbar, config.payoutHbar);
}
lastCompletedRound = round;
gameState.currentRound = null;
continue;
}
// Play intent
if (/\b(play|start|let'?s go|new round|another|again|hit me)\b/i.test(lower) && !gameState.currentRound) {
const confirmed = await askYesNo(`💰 Entry fee: ${config.entryFeeHbar} HBAR. Ready to play? (yes/no): `);
if (!confirmed) { console.log('\nNo problem, come back when you\'re ready!\n'); continue; }
console.log('\n⏳ Paying entry fee via agent on-chain...\n');
try {
const { text, toolCall, txId } = await invokeAgent(
`Transfer exactly ${config.entryFeeHbar} HBAR from ${config.operatorAccountId} to ${config.prizePoolAccountId} as the trivia entry fee.`
);
if (text) console.log('Hash Gordon: ' + text + '\n');
if (!toolCall) { console.log('⚠️ Transfer did not go through or TX ID not captured. Round cancelled.\n'); continue; }
const entryTxId = txId ?? 'unknown';
console.log(`✅ Entry fee paid! Tx: ${entryTxId}\n`);
console.log('🤔 Generating your question...\n');
let question;
try { question = await generateQuestion(); }
catch (err) { console.log(`⚠️ Could not generate question: ${err.message}\n`); continue; }
gameState.currentRound = { question, entryFeeTxId: entryTxId, awaitingAnswer: true };
console.log(formatQuestion(question));
console.log('Type A, B, C, or D to answer. Type "hint" for a clue.\n');
} catch (err) {
console.log(`⚠️ Entry fee error: ${err.message}\n`);
console.log('💡 Make sure you have sufficient funds in your wallet.\n');
}
continue;
}
// Balance check
if (/\bbalance\b|\bafford\b/i.test(lower)) {
try {
const { text } = await invokeAgent(`Check the HBAR balance for account ${config.operatorAccountId} and report it.`);
console.log('\nHash Gordon: ' + text + '\n');
} catch (err) { console.log(`Balance check error: ${err.message}\n`); }
continue;
}
// General chat
try {
let chatMessage = input;
if (lastCompletedRound && /explain|why|answer|question|correct|wrong|last/i.test(lower)) {
const q = lastCompletedRound.question;
chatMessage = `Last trivia question: "${q.text}". Correct answer: ${q.answer}) ${q.choices[q.answer]}. Explanation: ${q.explanation}.\nPlayer asks: ${input}`;
}
const { text } = await invokeAgent(chatMessage);
console.log('\nHash Gordon: ' + text + '\n');
} catch (err) { console.log(`Error: ${err.message}\n`); }
}
}
await bootstrap();