mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-13 23:16:55 +00:00
fixing tool calling bug
This commit is contained in:
@@ -32,10 +32,10 @@ export class SolanaBalanceTool extends Tool {
|
||||
try {
|
||||
// Parse input as JSON if provided, otherwise use empty object
|
||||
const parsedInput = input ? JSON.parse(input) : {};
|
||||
|
||||
|
||||
// Validate and execute using the action
|
||||
const result = await this.action.handler(this.solanaKit, parsedInput);
|
||||
|
||||
|
||||
return JSON.stringify(result);
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
@@ -60,10 +60,10 @@ export class SolanaTransferTool extends Tool {
|
||||
try {
|
||||
// Parse input as JSON
|
||||
const parsedInput = JSON.parse(input);
|
||||
|
||||
|
||||
// Validate and execute using the action
|
||||
const result = await this.action.handler(this.solanaKit, parsedInput);
|
||||
|
||||
|
||||
return JSON.stringify(result);
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
@@ -88,10 +88,10 @@ export class SolanaDeployTokenTool extends Tool {
|
||||
try {
|
||||
// Parse input as JSON
|
||||
const parsedInput = JSON.parse(input);
|
||||
|
||||
|
||||
// Validate and execute using the action
|
||||
const result = await this.action.handler(this.solanaKit, parsedInput);
|
||||
|
||||
|
||||
return JSON.stringify(result);
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
@@ -116,10 +116,10 @@ export class SolanaDeployCollectionTool extends Tool {
|
||||
try {
|
||||
// Parse input as JSON
|
||||
const parsedInput = JSON.parse(input);
|
||||
|
||||
|
||||
// Validate and execute using the action
|
||||
const result = await this.action.handler(this.solanaKit, parsedInput);
|
||||
|
||||
|
||||
return JSON.stringify(result);
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
@@ -144,10 +144,10 @@ export class SolanaMintNFTTool extends Tool {
|
||||
try {
|
||||
// Parse input as JSON
|
||||
const parsedInput = JSON.parse(input);
|
||||
|
||||
|
||||
// Validate and execute using the action
|
||||
const result = await this.action.handler(this.solanaKit, parsedInput);
|
||||
|
||||
|
||||
return JSON.stringify(result);
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
@@ -172,10 +172,10 @@ export class SolanaTradeTool extends Tool {
|
||||
try {
|
||||
// Parse input as JSON
|
||||
const parsedInput = JSON.parse(input);
|
||||
|
||||
|
||||
// Validate and execute using the action
|
||||
const result = await this.action.handler(this.solanaKit, parsedInput);
|
||||
|
||||
|
||||
return JSON.stringify(result);
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
@@ -200,7 +200,7 @@ export class SolanaRequestFundsTool extends Tool {
|
||||
try {
|
||||
// No input needed for this action
|
||||
const result = await this.action.handler(this.solanaKit, {});
|
||||
|
||||
|
||||
return JSON.stringify(result);
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
@@ -1151,7 +1151,7 @@ export class SolanaCreateGibworkTask extends Tool {
|
||||
}
|
||||
|
||||
export function createSolanaTools(solanaKit: SolanaAgentKit) {
|
||||
const tools = [
|
||||
return [
|
||||
new SolanaBalanceTool(solanaKit),
|
||||
new SolanaTransferTool(solanaKit),
|
||||
new SolanaDeployTokenTool(solanaKit),
|
||||
@@ -1185,7 +1185,4 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) {
|
||||
new SolanaResolveAllDomainsTool(solanaKit),
|
||||
new SolanaCreateGibworkTask(solanaKit),
|
||||
];
|
||||
|
||||
// Convert LangChain tools to our Action interface
|
||||
return tools.map(tool => wrapLangChainTool(tool, solanaKit));
|
||||
}
|
||||
|
||||
@@ -35,52 +35,6 @@ validateEnvironment();
|
||||
|
||||
const WALLET_DATA_FILE = "wallet_data.txt";
|
||||
|
||||
// Convert our Action interface to LangChain Tool
|
||||
function convertActionToTool(action: Action, solanaAgent: SolanaAgentKit): Tool {
|
||||
class ActionTool extends Tool {
|
||||
name = action.name;
|
||||
description = action.description;
|
||||
|
||||
async _call(input: string): Promise<string> {
|
||||
try {
|
||||
let parsedInput;
|
||||
try {
|
||||
// Try to parse as JSON first
|
||||
parsedInput = input ? JSON.parse(input) : {};
|
||||
} catch {
|
||||
// If JSON parsing fails, use the raw input string
|
||||
parsedInput = { input };
|
||||
}
|
||||
|
||||
// Validate input against schema if available
|
||||
if (action.schema) {
|
||||
try {
|
||||
parsedInput = action.schema.parse(parsedInput);
|
||||
} catch (validationError: any) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: `Invalid input: ${validationError.message}`,
|
||||
code: "VALIDATION_ERROR"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const result = await action.handler(solanaAgent, parsedInput);
|
||||
return JSON.stringify(result);
|
||||
} catch (error: any) {
|
||||
console.error("Action execution error:", error);
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: error.message,
|
||||
code: error.code || "UNKNOWN_ERROR"
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new ActionTool();
|
||||
}
|
||||
|
||||
async function initializeAgent() {
|
||||
try {
|
||||
const llm = new ChatOpenAI({
|
||||
@@ -104,10 +58,8 @@ async function initializeAgent() {
|
||||
process.env.OPENAI_API_KEY!,
|
||||
);
|
||||
|
||||
const actions = createSolanaTools(solanaAgent);
|
||||
// Convert our Actions to LangChain Tools
|
||||
const tools = actions.map(action => convertActionToTool(action, solanaAgent));
|
||||
|
||||
const tools = createSolanaTools(solanaAgent);
|
||||
|
||||
const memory = new MemorySaver();
|
||||
const config = { configurable: { thread_id: "Solana Agent Kit!" } };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user