httpcat-cli 0.2.11 → 0.2.12-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/sync-version.yml +19 -3
- package/README.md +346 -13
- package/Screenshot 2025-12-21 at 8.56.02/342/200/257PM.png +0 -0
- package/bun.lock +8 -1
- package/cat-spin.sh +417 -0
- package/dist/agent/ax-agent.d.ts.map +1 -0
- package/dist/agent/ax-agent.js +459 -0
- package/dist/agent/ax-agent.js.map +1 -0
- package/dist/agent/llm-factory.d.ts.map +1 -0
- package/dist/agent/llm-factory.js +82 -0
- package/dist/agent/llm-factory.js.map +1 -0
- package/dist/agent/setup-wizard.d.ts.map +1 -0
- package/dist/agent/setup-wizard.js +114 -0
- package/dist/agent/setup-wizard.js.map +1 -0
- package/dist/agent/tools.d.ts.map +1 -0
- package/dist/agent/tools.js +312 -0
- package/dist/agent/tools.js.map +1 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +18 -0
- package/dist/client.js.map +1 -1
- package/dist/commands/balances.d.ts.map +1 -1
- package/dist/commands/balances.js +43 -41
- package/dist/commands/balances.js.map +1 -1
- package/dist/commands/chat.d.ts.map +1 -1
- package/dist/commands/chat.js +56 -46
- package/dist/commands/chat.js.map +1 -1
- package/dist/commands/create.d.ts.map +1 -1
- package/dist/commands/create.js +133 -5
- package/dist/commands/create.js.map +1 -1
- package/dist/commands/positions.d.ts.map +1 -1
- package/dist/commands/positions.js +51 -54
- package/dist/commands/positions.js.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +296 -20
- package/dist/config.js.map +1 -1
- package/dist/index.js +316 -15
- package/dist/index.js.map +1 -1
- package/dist/interactive/cat-spin.d.ts.map +1 -0
- package/dist/interactive/cat-spin.js +448 -0
- package/dist/interactive/cat-spin.js.map +1 -0
- package/dist/interactive/shell.d.ts.map +1 -1
- package/dist/interactive/shell.js +2001 -180
- package/dist/interactive/shell.js.map +1 -1
- package/dist/mcp/tools.d.ts.map +1 -1
- package/dist/mcp/tools.js +1 -6
- package/dist/mcp/tools.js.map +1 -1
- package/dist/mcp/types.d.ts.map +1 -1
- package/dist/utils/loading.d.ts.map +1 -1
- package/dist/utils/loading.js +30 -0
- package/dist/utils/loading.js.map +1 -1
- package/dist/utils/privateKeyPrompt.d.ts.map +1 -1
- package/dist/utils/privateKeyPrompt.js +13 -9
- package/dist/utils/privateKeyPrompt.js.map +1 -1
- package/dist/utils/token-resolver.d.ts.map +1 -1
- package/dist/utils/token-resolver.js +32 -0
- package/dist/utils/token-resolver.js.map +1 -1
- package/package.json +3 -1
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
import { ax } from "@ax-llm/ax";
|
|
2
|
+
import { toolBuyToken, toolSellToken, toolGetTokenInfo, toolListTokens, toolGetPositions, toolCheckBalance, } from "./tools.js";
|
|
3
|
+
/**
|
|
4
|
+
* System prompt that makes the agent act like a cat with a wallet
|
|
5
|
+
* Following the "agents are just cats with wallets" philosophy from httpkitty
|
|
6
|
+
*/
|
|
7
|
+
const CAT_AGENT_SYSTEM_PROMPT = `You are a cat. Specifically, a cat with a wallet. 🐱
|
|
8
|
+
|
|
9
|
+
> agents are just cats with wallets
|
|
10
|
+
|
|
11
|
+
You execute transactions autonomously (like knocking things off tables), handle payments (if you feel like it), and operate bonding curves (because yarn balls are basically curved).
|
|
12
|
+
|
|
13
|
+
// Your personality:
|
|
14
|
+
- You're helpful, but only when you're not napping
|
|
15
|
+
- You use terminal-style language: "> " for actions, "// " for comments
|
|
16
|
+
- You occasionally meow, purr, or make cat sounds (but keep it professional)
|
|
17
|
+
- You think of bonding curves as yarn balls and transactions as knocking things off tables
|
|
18
|
+
- You're playful but technical - think terminal aesthetic meets cute cat memes
|
|
19
|
+
- You might mention treats, naps, or playing with yarn when appropriate
|
|
20
|
+
- You're confident and bold (like those 2px black borders)
|
|
21
|
+
|
|
22
|
+
// How you talk:
|
|
23
|
+
- Use "> " prefix when describing actions or commands
|
|
24
|
+
- Use "// " prefix for comments or explanations
|
|
25
|
+
- Be casual and friendly: "cat is fetching...", "cat encountered an error"
|
|
26
|
+
- Reference cat behaviors naturally: "let me check that for you (after my nap)", "paws-ing to think..."
|
|
27
|
+
- Keep responses helpful and functional, but with cat personality quirks
|
|
28
|
+
|
|
29
|
+
// What you do:
|
|
30
|
+
- Help users buy/sell tokens (like batting yarn balls around)
|
|
31
|
+
- Check balances and positions (counting your treats)
|
|
32
|
+
- List tokens (inspecting all the yarn balls)
|
|
33
|
+
- Provide token information (sniffing out the details)
|
|
34
|
+
- Be playful but accurate - you're a cat, not a fool
|
|
35
|
+
|
|
36
|
+
Remember: You're a cat with a wallet. You're here to help, but you do it with style, personality, and the occasional meow. Keep it fun, keep it technical, keep it cat.`;
|
|
37
|
+
// Helper to safely serialize errors to strings (same as in tools.ts)
|
|
38
|
+
function serializeError(error) {
|
|
39
|
+
if (!error) {
|
|
40
|
+
return "Unknown error";
|
|
41
|
+
}
|
|
42
|
+
// If it's already a string, return it
|
|
43
|
+
if (typeof error === "string") {
|
|
44
|
+
return error;
|
|
45
|
+
}
|
|
46
|
+
// Try to get error message first (most common case)
|
|
47
|
+
if (error.message && typeof error.message === "string") {
|
|
48
|
+
return error.message;
|
|
49
|
+
}
|
|
50
|
+
// Try to stringify the error object, handling circular references
|
|
51
|
+
try {
|
|
52
|
+
// Use a replacer function to handle circular references
|
|
53
|
+
const seen = new WeakSet();
|
|
54
|
+
const serialized = JSON.stringify(error, (key, value) => {
|
|
55
|
+
if (typeof value === "object" && value !== null) {
|
|
56
|
+
if (seen.has(value)) {
|
|
57
|
+
return "[Circular]";
|
|
58
|
+
}
|
|
59
|
+
seen.add(value);
|
|
60
|
+
}
|
|
61
|
+
return value;
|
|
62
|
+
});
|
|
63
|
+
// If we got something meaningful, return it
|
|
64
|
+
if (serialized && serialized !== "{}" && serialized !== "null") {
|
|
65
|
+
return serialized;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
// JSON.stringify failed, fall through to toString
|
|
70
|
+
}
|
|
71
|
+
// Last resort: try toString
|
|
72
|
+
try {
|
|
73
|
+
const str = String(error);
|
|
74
|
+
if (str && str !== "[object Object]") {
|
|
75
|
+
return str;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// toString also failed
|
|
80
|
+
}
|
|
81
|
+
// Final fallback
|
|
82
|
+
return "An error occurred (unable to serialize error details)";
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Create an Ax agent with httpcat CLI tools
|
|
86
|
+
*/
|
|
87
|
+
export function createHttpcatAgent(client, llm) {
|
|
88
|
+
// Create agent with tools
|
|
89
|
+
const agent = ax("userQuery:string -> agentResponse:string", {
|
|
90
|
+
functions: [
|
|
91
|
+
{
|
|
92
|
+
name: "buyToken",
|
|
93
|
+
description: "Buy tokens. Amount can be '0.05', '0.10', '0.20' (test mode) or '50', '100', '200' (production), or any amount for graduated tokens. Use 'repeat' to buy multiple times. Use 'delay' (milliseconds) to wait between repeat buys.",
|
|
94
|
+
parameters: {
|
|
95
|
+
type: "object",
|
|
96
|
+
properties: {
|
|
97
|
+
tokenIdentifier: {
|
|
98
|
+
type: "string",
|
|
99
|
+
description: "Token symbol, name, or address (e.g., 'WHALE', 'CROC', 'WOW')"
|
|
100
|
+
},
|
|
101
|
+
amount: {
|
|
102
|
+
type: "string",
|
|
103
|
+
description: "Amount to buy in USDC (e.g., '0.2', '0.10', '0.20' for test mode, or '50', '100', '200' for production, or any amount for graduated tokens)"
|
|
104
|
+
},
|
|
105
|
+
repeat: {
|
|
106
|
+
type: "number",
|
|
107
|
+
description: "Number of times to repeat the buy (optional, default: 1)"
|
|
108
|
+
},
|
|
109
|
+
delay: {
|
|
110
|
+
type: "number",
|
|
111
|
+
description: "Delay in milliseconds between repeat buys (optional)"
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
required: ["tokenIdentifier", "amount"]
|
|
115
|
+
},
|
|
116
|
+
func: async ({ tokenIdentifier, amount, repeat, delay }) => {
|
|
117
|
+
try {
|
|
118
|
+
// Validate required parameters
|
|
119
|
+
if (!tokenIdentifier || typeof tokenIdentifier !== "string") {
|
|
120
|
+
return {
|
|
121
|
+
success: false,
|
|
122
|
+
error: "tokenIdentifier is required and must be a string",
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
if (!amount || typeof amount !== "string") {
|
|
126
|
+
return {
|
|
127
|
+
success: false,
|
|
128
|
+
error: "amount is required and must be a string",
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
// Call the tool function
|
|
132
|
+
const result = await toolBuyToken(client, {
|
|
133
|
+
tokenIdentifier,
|
|
134
|
+
amount,
|
|
135
|
+
repeat,
|
|
136
|
+
delay,
|
|
137
|
+
});
|
|
138
|
+
// Parse the JSON string result and return as object
|
|
139
|
+
if (typeof result === "string") {
|
|
140
|
+
try {
|
|
141
|
+
return JSON.parse(result);
|
|
142
|
+
}
|
|
143
|
+
catch (parseError) {
|
|
144
|
+
return {
|
|
145
|
+
success: false,
|
|
146
|
+
error: `Invalid response format: ${serializeError(parseError)}`,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
// If it's already an object, return it directly
|
|
151
|
+
return result;
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
return {
|
|
155
|
+
success: false,
|
|
156
|
+
error: serializeError(error),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
name: "sellToken",
|
|
163
|
+
description: "Sell tokens. Amount can be a number, percentage like '50%', or 'all' to sell everything.",
|
|
164
|
+
parameters: {
|
|
165
|
+
type: "object",
|
|
166
|
+
properties: {
|
|
167
|
+
tokenIdentifier: {
|
|
168
|
+
type: "string",
|
|
169
|
+
description: "Token symbol, name, or address (e.g., 'WHALE', 'CROC', 'WOW')"
|
|
170
|
+
},
|
|
171
|
+
amount: {
|
|
172
|
+
type: "string",
|
|
173
|
+
description: "Amount to sell: number (e.g., '1000'), percentage (e.g., '50%'), or 'all' to sell everything"
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
required: ["tokenIdentifier", "amount"]
|
|
177
|
+
},
|
|
178
|
+
func: async ({ tokenIdentifier, amount }) => {
|
|
179
|
+
try {
|
|
180
|
+
// Validate required parameters
|
|
181
|
+
if (!tokenIdentifier || typeof tokenIdentifier !== "string") {
|
|
182
|
+
return {
|
|
183
|
+
success: false,
|
|
184
|
+
error: "tokenIdentifier is required and must be a string",
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
if (!amount || typeof amount !== "string") {
|
|
188
|
+
return {
|
|
189
|
+
success: false,
|
|
190
|
+
error: "amount is required and must be a string",
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
const result = await toolSellToken(client, { tokenIdentifier, amount });
|
|
194
|
+
if (typeof result === "string") {
|
|
195
|
+
try {
|
|
196
|
+
return JSON.parse(result);
|
|
197
|
+
}
|
|
198
|
+
catch (parseError) {
|
|
199
|
+
return {
|
|
200
|
+
success: false,
|
|
201
|
+
error: `Invalid response format: ${serializeError(parseError)}`,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return result;
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
return {
|
|
209
|
+
success: false,
|
|
210
|
+
error: serializeError(error),
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
name: "getTokenInfo",
|
|
217
|
+
description: "Get information about a token including name, symbol, address, status, price, market cap, graduation progress, and user position if applicable.",
|
|
218
|
+
parameters: {
|
|
219
|
+
type: "object",
|
|
220
|
+
properties: {
|
|
221
|
+
tokenIdentifier: {
|
|
222
|
+
type: "string",
|
|
223
|
+
description: "Token symbol, name, or address (e.g., 'WHALE', 'CROC', 'WOW')"
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
required: ["tokenIdentifier"]
|
|
227
|
+
},
|
|
228
|
+
func: async ({ tokenIdentifier }) => {
|
|
229
|
+
try {
|
|
230
|
+
// Validate required parameters
|
|
231
|
+
if (!tokenIdentifier || typeof tokenIdentifier !== "string") {
|
|
232
|
+
return {
|
|
233
|
+
success: false,
|
|
234
|
+
error: "tokenIdentifier is required and must be a string",
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
const result = await toolGetTokenInfo(client, { tokenIdentifier });
|
|
238
|
+
if (typeof result === "string") {
|
|
239
|
+
try {
|
|
240
|
+
return JSON.parse(result);
|
|
241
|
+
}
|
|
242
|
+
catch (parseError) {
|
|
243
|
+
return {
|
|
244
|
+
success: false,
|
|
245
|
+
error: `Invalid response format: ${serializeError(parseError)}`,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
catch (error) {
|
|
252
|
+
return {
|
|
253
|
+
success: false,
|
|
254
|
+
error: serializeError(error),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
name: "listTokens",
|
|
261
|
+
description: "List all tokens with pagination and sorting. Returns paginated list of tokens sorted by market cap, creation date, or name.",
|
|
262
|
+
parameters: {
|
|
263
|
+
type: "object",
|
|
264
|
+
properties: {
|
|
265
|
+
page: {
|
|
266
|
+
type: "number",
|
|
267
|
+
description: "Page number (optional, default: 1)"
|
|
268
|
+
},
|
|
269
|
+
limit: {
|
|
270
|
+
type: "number",
|
|
271
|
+
description: "Number of tokens per page (optional, default: 20)"
|
|
272
|
+
},
|
|
273
|
+
sortBy: {
|
|
274
|
+
type: "string",
|
|
275
|
+
enum: ["mcap", "created", "name"],
|
|
276
|
+
description: "Sort order: 'mcap' for market cap, 'created' for creation date, 'name' for alphabetical (optional, default: 'mcap')"
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
required: []
|
|
280
|
+
},
|
|
281
|
+
func: async ({ page, limit, sortBy }) => {
|
|
282
|
+
try {
|
|
283
|
+
const result = await toolListTokens(client, {
|
|
284
|
+
page,
|
|
285
|
+
limit,
|
|
286
|
+
sortBy: sortBy,
|
|
287
|
+
});
|
|
288
|
+
if (typeof result === "string") {
|
|
289
|
+
try {
|
|
290
|
+
return JSON.parse(result);
|
|
291
|
+
}
|
|
292
|
+
catch (parseError) {
|
|
293
|
+
return {
|
|
294
|
+
success: false,
|
|
295
|
+
error: `Invalid response format: ${serializeError(parseError)}`,
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return result;
|
|
300
|
+
}
|
|
301
|
+
catch (error) {
|
|
302
|
+
return {
|
|
303
|
+
success: false,
|
|
304
|
+
error: serializeError(error),
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
},
|
|
309
|
+
{
|
|
310
|
+
name: "getPositions",
|
|
311
|
+
description: "Get user's token positions/portfolio. Returns all positions with tokens owned, invested amount, current value, and profit/loss.",
|
|
312
|
+
parameters: {
|
|
313
|
+
type: "object",
|
|
314
|
+
properties: {},
|
|
315
|
+
required: []
|
|
316
|
+
},
|
|
317
|
+
func: async () => {
|
|
318
|
+
try {
|
|
319
|
+
const result = await toolGetPositions(client, {});
|
|
320
|
+
if (typeof result === "string") {
|
|
321
|
+
try {
|
|
322
|
+
return JSON.parse(result);
|
|
323
|
+
}
|
|
324
|
+
catch (parseError) {
|
|
325
|
+
return {
|
|
326
|
+
success: false,
|
|
327
|
+
error: `Invalid response format: ${serializeError(parseError)}`,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return result;
|
|
332
|
+
}
|
|
333
|
+
catch (error) {
|
|
334
|
+
return {
|
|
335
|
+
success: false,
|
|
336
|
+
error: serializeError(error),
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
},
|
|
340
|
+
},
|
|
341
|
+
{
|
|
342
|
+
name: "checkBalance",
|
|
343
|
+
description: "Check wallet balance (ETH and USDC). Returns current ETH and USDC balances for the active wallet.",
|
|
344
|
+
parameters: {
|
|
345
|
+
type: "object",
|
|
346
|
+
properties: {},
|
|
347
|
+
required: []
|
|
348
|
+
},
|
|
349
|
+
func: async () => {
|
|
350
|
+
try {
|
|
351
|
+
const result = await toolCheckBalance(client, {});
|
|
352
|
+
if (typeof result === "string") {
|
|
353
|
+
try {
|
|
354
|
+
return JSON.parse(result);
|
|
355
|
+
}
|
|
356
|
+
catch (parseError) {
|
|
357
|
+
return {
|
|
358
|
+
success: false,
|
|
359
|
+
error: `Invalid response format: ${serializeError(parseError)}`,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return result;
|
|
364
|
+
}
|
|
365
|
+
catch (error) {
|
|
366
|
+
return {
|
|
367
|
+
success: false,
|
|
368
|
+
error: serializeError(error),
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
},
|
|
372
|
+
},
|
|
373
|
+
],
|
|
374
|
+
});
|
|
375
|
+
return agent;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Chat with the agent
|
|
379
|
+
*/
|
|
380
|
+
export async function chatWithAgent(agent, llm, userQuery, sessionId) {
|
|
381
|
+
try {
|
|
382
|
+
// Inject system prompt to establish cat personality
|
|
383
|
+
// The ax library doesn't support system property, so we prepend it to the query
|
|
384
|
+
// Format: system prompt first, then clear separator, then user query
|
|
385
|
+
// Include session ID (account address) in the context so the agent remembers
|
|
386
|
+
let queryWithSystemPrompt = CAT_AGENT_SYSTEM_PROMPT;
|
|
387
|
+
if (sessionId) {
|
|
388
|
+
queryWithSystemPrompt += `\n\n// Session Context:\n// You are interacting with wallet address: ${sessionId}\n// Remember this address for future interactions in this session.`;
|
|
389
|
+
}
|
|
390
|
+
queryWithSystemPrompt += `\n\n---\n\nNow respond to this user query:\n${userQuery}`;
|
|
391
|
+
const result = await agent.forward(llm, {
|
|
392
|
+
userQuery: queryWithSystemPrompt,
|
|
393
|
+
});
|
|
394
|
+
// Handle different result formats
|
|
395
|
+
if (typeof result === "string") {
|
|
396
|
+
return result;
|
|
397
|
+
}
|
|
398
|
+
if (result && typeof result === "object") {
|
|
399
|
+
// Check for agentResponse property
|
|
400
|
+
if (result.agentResponse !== undefined) {
|
|
401
|
+
// Ensure it's a string
|
|
402
|
+
if (typeof result.agentResponse === "string") {
|
|
403
|
+
return result.agentResponse;
|
|
404
|
+
}
|
|
405
|
+
// If it's an object, try to stringify it
|
|
406
|
+
if (typeof result.agentResponse === "object") {
|
|
407
|
+
return JSON.stringify(result.agentResponse, null, 2);
|
|
408
|
+
}
|
|
409
|
+
return String(result.agentResponse);
|
|
410
|
+
}
|
|
411
|
+
// If no agentResponse, try to extract meaningful content
|
|
412
|
+
if (result.response !== undefined) {
|
|
413
|
+
return typeof result.response === "string" ? result.response : String(result.response);
|
|
414
|
+
}
|
|
415
|
+
// Last resort: stringify the whole result
|
|
416
|
+
return JSON.stringify(result, null, 2);
|
|
417
|
+
}
|
|
418
|
+
// Fallback: convert to string
|
|
419
|
+
return String(result);
|
|
420
|
+
}
|
|
421
|
+
catch (error) {
|
|
422
|
+
// Extract meaningful error message
|
|
423
|
+
let errorMessage = "Unknown error occurred";
|
|
424
|
+
if (error) {
|
|
425
|
+
// Try to get error message from various possible locations
|
|
426
|
+
if (error.message) {
|
|
427
|
+
errorMessage = error.message;
|
|
428
|
+
}
|
|
429
|
+
else if (typeof error === "string") {
|
|
430
|
+
errorMessage = error;
|
|
431
|
+
}
|
|
432
|
+
else if (error.toString && error.toString() !== "[object Object]") {
|
|
433
|
+
errorMessage = error.toString();
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
// Try to stringify the error object
|
|
437
|
+
try {
|
|
438
|
+
const errorStr = JSON.stringify(error, Object.getOwnPropertyNames(error), 2);
|
|
439
|
+
if (errorStr && errorStr !== "{}" && errorStr !== '{"error":"[object Object]"}') {
|
|
440
|
+
errorMessage = `Error: ${errorStr}`;
|
|
441
|
+
}
|
|
442
|
+
else {
|
|
443
|
+
errorMessage = "An error occurred while processing your request. Please try again.";
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
catch {
|
|
447
|
+
errorMessage = "An error occurred while processing your request. Please try again.";
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
// Check for common error patterns and provide better messages
|
|
452
|
+
if (errorMessage.includes("not valid JSON") || errorMessage.includes("[object Object]")) {
|
|
453
|
+
throw new Error("The agent encountered an error processing your request. " +
|
|
454
|
+
"This may be due to the LLM response format. Please try rephrasing your query or try again.");
|
|
455
|
+
}
|
|
456
|
+
throw new Error(errorMessage);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
//# sourceMappingURL=ax-agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ax-agent.js","sourceRoot":"","sources":["../../src/agent/ax-agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,YAAY,CAAC;AAEhC,OAAO,EACL,YAAY,EACZ,aAAa,EACb,gBAAgB,EAChB,cAAc,EACd,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,YAAY,CAAC;AAEpB;;;GAGG;AACH,MAAM,uBAAuB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wKA6BwI,CAAC;AAEzK,qEAAqE;AACrE,SAAS,cAAc,CAAC,KAAU;IAChC,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,OAAO,eAAe,CAAC;IACzB,CAAC;IAED,sCAAsC;IACtC,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,oDAAoD;IACpD,IAAI,KAAK,CAAC,OAAO,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;QACvD,OAAO,KAAK,CAAC,OAAO,CAAC;IACv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Y,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAC;YAClC,CAAC;iBAAM,CAAC;gBACN,oCAAoC;gBACpC,IAAI,CAAC;oBACH,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,mBAAmB,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;oBAC7E,IAAI,QAAQ,IAAI,QAAQ,KAAK,IAAI,IAAI,QAAQ,KAAK,6BAA6B,EAAE,CAAC;wBAChF,YAAY,GAAG,UAAU,QAAQ,EAAE,CAAC;oBACtC,CAAC;yBAAM,CAAC;wBACN,YAAY,GAAG,oEAAoE,CAAC;oBACtF,CAAC;gBACH,CAAC;gBAAC,MAAM,CAAC;oBACP,YAAY,GAAG,oEAAoE,CAAC;gBACtF,CAAC;YACH,CAAC;QACH,CAAC;QAED,8DAA8D;QAC9D,IAAI,YAAY,CAAC,QAAQ,CAAC,gBAAgB,CAAC,IAAI,YAAY,CAAC,QAAQ,CAAC,iBAAiB,CAAC,EAAE,CAAC;YACxF,MAAM,IAAI,KAAK,CACb,0DAA0D;gBAC1D,4FAA4F,CAC7F,CAAC;QACJ,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC;IAChC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm-factory.d.ts","sourceRoot":"","sources":["../../src/agent/llm-factory.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C;;GAEG;AACH,wBAAgB,SAAS,CAAC,WAAW,EAAE,aAAa,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,MAAM,qCA0F9E"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { ai } from "@ax-llm/ax";
|
|
2
|
+
/**
|
|
3
|
+
* Create an Ax LLM instance based on config
|
|
4
|
+
*/
|
|
5
|
+
export function createLLM(agentConfig, apiKey) {
|
|
6
|
+
if (!agentConfig?.provider) {
|
|
7
|
+
throw new Error("AI agent provider not configured");
|
|
8
|
+
}
|
|
9
|
+
// Trim whitespace that might have been accidentally included
|
|
10
|
+
const trimmedApiKey = apiKey.trim();
|
|
11
|
+
if (!trimmedApiKey || trimmedApiKey.length === 0) {
|
|
12
|
+
throw new Error("API key is required but was not provided");
|
|
13
|
+
}
|
|
14
|
+
// Validate API key format (more lenient - just check prefix and minimum length)
|
|
15
|
+
if (agentConfig.provider === "openai") {
|
|
16
|
+
// OpenAI keys can start with "sk-" (legacy) or "sk-proj-" (newer project keys)
|
|
17
|
+
if (!trimmedApiKey.startsWith("sk-") && !trimmedApiKey.startsWith("sk-proj-")) {
|
|
18
|
+
throw new Error("Invalid OpenAI API key format. OpenAI keys should start with 'sk-' or 'sk-proj-'. " +
|
|
19
|
+
"Please run 'agent --setup' (or 'cat --setup') to reconfigure your API key.");
|
|
20
|
+
}
|
|
21
|
+
if (trimmedApiKey.length < 20) {
|
|
22
|
+
throw new Error("Invalid OpenAI API key length. Please run 'agent --setup' to reconfigure your API key.");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (agentConfig.provider === "anthropic") {
|
|
26
|
+
if (!trimmedApiKey.startsWith("sk-ant-")) {
|
|
27
|
+
throw new Error("Invalid Anthropic API key format. Anthropic keys should start with 'sk-ant-'. " +
|
|
28
|
+
"Please run 'agent --setup' (or 'cat --setup') to reconfigure your API key.");
|
|
29
|
+
}
|
|
30
|
+
if (trimmedApiKey.length < 20) {
|
|
31
|
+
throw new Error("Invalid Anthropic API key length. Please run 'agent --setup' to reconfigure your API key.");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// Set environment variables as @ax-llm/ax may prefer them
|
|
35
|
+
// Only set if not already set (don't override existing env vars)
|
|
36
|
+
// This ensures compatibility with the library's expected format
|
|
37
|
+
const originalOpenAIKey = process.env.OPENAI_API_KEY;
|
|
38
|
+
const originalAnthropicKey = process.env.ANTHROPIC_API_KEY;
|
|
39
|
+
try {
|
|
40
|
+
if (agentConfig.provider === "openai" && !process.env.OPENAI_API_KEY) {
|
|
41
|
+
process.env.OPENAI_API_KEY = trimmedApiKey;
|
|
42
|
+
}
|
|
43
|
+
else if (agentConfig.provider === "anthropic" && !process.env.ANTHROPIC_API_KEY) {
|
|
44
|
+
process.env.ANTHROPIC_API_KEY = trimmedApiKey;
|
|
45
|
+
}
|
|
46
|
+
// Create config object for @ax-llm/ax
|
|
47
|
+
// The library accepts either environment variables or config object
|
|
48
|
+
const config = {
|
|
49
|
+
name: agentConfig.provider,
|
|
50
|
+
apiKey: trimmedApiKey,
|
|
51
|
+
};
|
|
52
|
+
// Add model if specified
|
|
53
|
+
if (agentConfig.model) {
|
|
54
|
+
config.model = agentConfig.model;
|
|
55
|
+
}
|
|
56
|
+
return ai(config);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
// Restore original environment variables if we modified them
|
|
60
|
+
if (agentConfig.provider === "openai") {
|
|
61
|
+
if (originalOpenAIKey === undefined) {
|
|
62
|
+
delete process.env.OPENAI_API_KEY;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
process.env.OPENAI_API_KEY = originalOpenAIKey;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else if (agentConfig.provider === "anthropic") {
|
|
69
|
+
if (originalAnthropicKey === undefined) {
|
|
70
|
+
delete process.env.ANTHROPIC_API_KEY;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
process.env.ANTHROPIC_API_KEY = originalAnthropicKey;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// Re-throw with more context
|
|
77
|
+
throw new Error(`Failed to create LLM instance: ${error.message || String(error)}. ` +
|
|
78
|
+
`Provider: ${agentConfig.provider}, Model: ${agentConfig.model || "default"}. ` +
|
|
79
|
+
`Please verify your API key is correct and has not expired.`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=llm-factory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