Fix formatting issues and removing unusued libaries

This commit is contained in:
Fahri Bilici
2024-12-30 22:31:15 +01:00
parent 3b4fddd65a
commit c00515f3d2
35 changed files with 574 additions and 662 deletions

View File

@@ -7,9 +7,10 @@ import { actions } from "../actions";
*/
export function findAction(query: string): Action | undefined {
const normalizedQuery = query.toLowerCase().trim();
return actions.find(action =>
action.name.toLowerCase() === normalizedQuery ||
action.similes.some(simile => simile.toLowerCase() === normalizedQuery)
return actions.find(
(action) =>
action.name.toLowerCase() === normalizedQuery ||
action.similes.some((simile) => simile.toLowerCase() === normalizedQuery),
);
}
@@ -19,18 +20,18 @@ export function findAction(query: string): Action | undefined {
export async function executeAction(
action: Action,
agent: SolanaAgentKit,
input: Record<string, any>
input: Record<string, any>,
): Promise<Record<string, any>> {
try {
// Validate input using Zod schema
const validatedInput = action.schema.parse(input);
// Execute the action with validated input
const result = await action.handler(agent, validatedInput);
return {
status: "success",
...result
...result,
};
} catch (error: any) {
// Handle Zod validation errors specially
@@ -39,14 +40,14 @@ export async function executeAction(
status: "error",
message: "Validation error",
details: error.errors,
code: "VALIDATION_ERROR"
code: "VALIDATION_ERROR",
};
}
return {
status: "error",
message: error.message,
code: error.code || "EXECUTION_ERROR"
code: error.code || "EXECUTION_ERROR",
};
}
}
@@ -57,11 +58,11 @@ export async function executeAction(
export function getActionExamples(action: Action): string {
return action.examples
.flat()
.map(example => {
.map((example) => {
return `Input: ${JSON.stringify(example.input, null, 2)}
Output: ${JSON.stringify(example.output, null, 2)}
Explanation: ${example.explanation}
---`;
})
.join("\n");
}
}

View File

@@ -1,96 +0,0 @@
import { Tool } from "langchain/tools";
import { Action } from "../types/action";
import { SolanaAgentKit } from "../agent";
import { z } from "zod";
/**
* Convert a LangChain tool to our Action interface
*/
export function wrapLangChainTool(tool: Tool, agent: SolanaAgentKit): Action {
// Parse the description to extract input parameters
const inputParams = parseToolDescription(tool.description);
return {
name: tool.name,
similes: [], // LangChain tools don't have similes
description: tool.description,
examples: [], // LangChain tools don't have examples
schema: createZodSchema(inputParams),
handler: async (agent: SolanaAgentKit, input: Record<string, any>) => {
const result = await tool.call(JSON.stringify(input));
try {
return JSON.parse(result);
} catch {
return { result };
}
}
};
}
/**
* Parse tool description to extract input parameters
*/
function parseToolDescription(description: string): Array<{name: string, type: string, required: boolean}> {
const lines = description.split('\n');
const params: Array<{name: string, type: string, required: boolean}> = [];
let inInputsSection = false;
for (const line of lines) {
const trimmed = line.trim();
if (trimmed === 'Inputs:' || trimmed === 'Inputs (input is a JSON string):') {
inInputsSection = true;
continue;
}
if (inInputsSection && trimmed) {
// Match patterns like: name: string, eg "value" (required)
const match = trimmed.match(/(\w+):\s*([\w\[\]]+)(?:,\s*eg[:\s]+"[^"]+")?(?:\s*\((required|optional)\))?/);
if (match) {
params.push({
name: match[1],
type: match[2],
required: match[3] === 'required'
});
}
}
}
return params;
}
/**
* Create a Zod schema from parsed parameters
*/
function createZodSchema(params: Array<{name: string, type: string, required: boolean}>): z.ZodType<any> {
const schemaObj: Record<string, z.ZodType<any>> = {};
for (const param of params) {
let schema: z.ZodType<any>;
switch (param.type.toLowerCase()) {
case 'string':
schema = z.string();
break;
case 'number':
schema = z.number();
break;
case 'boolean':
schema = z.boolean();
break;
case 'string[]':
schema = z.array(z.string());
break;
default:
schema = z.any();
}
if (!param.required) {
schema = schema.optional();
}
schemaObj[param.name] = schema;
}
return z.object(schemaObj);
}