-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-exammple.js
More file actions
190 lines (161 loc) · 6 KB
/
agent-exammple.js
File metadata and controls
190 lines (161 loc) · 6 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
#!/usr/bin/env node
/**
* SolanaProx Agent Example
*
* An AI agent that autonomously pays for its own inference using USDC.
* No API keys. No accounts. The wallet IS the credential.
*
* Setup:
* npm install
* SOLANA_WALLET=your_wallet_address node agent-example.js
*/
const SOLANAPROX_URL = process.env.SOLANAPROX_URL || "https://solanaprox.com";
const WALLET = process.env.SOLANA_WALLET;
if (!WALLET) {
console.error("❌ Set SOLANA_WALLET to your Phantom wallet address");
process.exit(1);
}
// ─── Core API call ───────────────────────────────────────────────────────────
async function askAI(prompt, model = "claude-sonnet-4-20250514", maxTokens = 512) {
const res = await fetch(`${SOLANAPROX_URL}/v1/messages`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Wallet-Address": WALLET,
},
body: JSON.stringify({
model,
max_tokens: maxTokens,
messages: [{ role: "user", content: prompt }],
}),
});
if (res.status === 402) {
const err = await res.json();
throw new Error(`💸 Insufficient balance: ${err.error}. Deposit at ${SOLANAPROX_URL}`);
}
if (!res.ok) {
const text = await res.text();
throw new Error(`API error ${res.status}: ${text}`);
}
const data = await res.json();
return {
text: data.content?.[0]?.text || data.choices?.[0]?.message?.content,
usage: data.usage,
model: data.model,
};
}
async function checkBalance() {
const res = await fetch(`${SOLANAPROX_URL}/api/balance/${WALLET}`);
if (!res.ok) throw new Error("Failed to check balance");
return res.json();
}
// ─── Example: Research Agent ─────────────────────────────────────────────────
async function researchAgent(topic) {
console.log(`\n🤖 Research Agent starting...`);
console.log(`📋 Topic: ${topic}`);
console.log(`💳 Wallet: ${WALLET.slice(0, 8)}...${WALLET.slice(-4)}`);
// Check balance before starting
const balance = await checkBalance();
console.log(`💰 Balance: $${balance.total_usd?.toFixed(4)} USDC\n`);
if (balance.total_usd < 0.01) {
console.log(`⚠️ Low balance. Deposit USDC at ${SOLANAPROX_URL}`);
return;
}
// Step 1: Generate research questions
console.log("Step 1: Generating research questions...");
const questions = await askAI(
`Generate 3 focused research questions about: "${topic}".
Format as a numbered list. Be specific and actionable.`,
"claude-sonnet-4-20250514",
256
);
console.log(questions.text);
// Step 2: Answer each question
console.log("\nStep 2: Researching each question...");
const answers = await askAI(
`Based on these questions about "${topic}":
${questions.text}
Provide concise, factual answers to each. Focus on key insights.`,
"claude-sonnet-4-20250514",
512
);
console.log(answers.text);
// Step 3: Synthesize into summary
console.log("\nStep 3: Synthesizing research...");
const summary = await askAI(
`Synthesize this research about "${topic}" into a 3-sentence executive summary:
${answers.text}`,
"claude-sonnet-4-20250514",
256
);
console.log("\n📊 RESEARCH SUMMARY");
console.log("─".repeat(50));
console.log(summary.text);
console.log("─".repeat(50));
// Final balance check
const finalBalance = await checkBalance();
console.log(`\n💳 Remaining balance: $${finalBalance.total_usd?.toFixed(4)} USDC`);
console.log(`⚡ Powered by SolanaProx | solanaprox.com`);
}
// ─── Example: Code Review Agent ──────────────────────────────────────────────
async function codeReviewAgent(code, language = "javascript") {
console.log(`\n🔍 Code Review Agent starting...`);
const balance = await checkBalance();
if (balance.total_usd < 0.005) {
console.log(`⚠️ Insufficient balance. Deposit at ${SOLANAPROX_URL}`);
return;
}
const review = await askAI(
`Review this ${language} code for:
1. Bugs and potential errors
2. Security issues
3. Performance improvements
4. Best practices
Code:
\`\`\`${language}
${code}
\`\`\`
Be specific with line references where applicable.`,
"claude-sonnet-4-20250514",
1024
);
console.log("\n📝 CODE REVIEW");
console.log("─".repeat(50));
console.log(review.text);
console.log("─".repeat(50));
console.log(`⚡ Tokens used: ${review.usage?.input_tokens || "?"} in / ${review.usage?.output_tokens || "?"} out`);
}
// ─── Run examples ─────────────────────────────────────────────────────────────
async function main() {
const [,, command, ...rest] = process.argv;
switch (command) {
case "research":
await researchAgent(rest.join(" ") || "the future of AI payments");
break;
case "review":
// Read from stdin for code review
const code = rest.join("\n") || `
function fetchUser(id) {
return fetch('/api/users/' + id)
.then(r => r.json())
.catch(e => null)
}
`;
await codeReviewAgent(code);
break;
case "balance":
const bal = await checkBalance();
console.log(`💰 Balance: $${bal.total_usd?.toFixed(4)} USDC`);
break;
default:
// Demo: run a quick AI call
console.log("🚀 SolanaProx Agent Demo\n");
const result = await askAI(
"In one sentence, explain why paying for AI with USDC is better than API keys."
);
console.log("Response:", result.text);
console.log(`\nTokens: ${result.usage?.input_tokens}in / ${result.usage?.output_tokens}out`);
console.log(`⚡ Powered by solanaprox.com`);
}
}
main().catch(console.error);