@@ -2,7 +2,7 @@ import type { KnownChainId } from "@polkadot-agent-kit/common"
22import { convertAddress } from "@polkadot-agent-kit/core"
33import type { ZodType } from "zod"
44
5- import type { Action , ToolError , ToolResponse } from "../types"
5+ import { Action , ToolError , ToolNames , ToolResponse } from "../types"
66import { ChainNotAvailableError , ErrorCodes , InvalidAddressError , isAnyToolError } from "../types"
77
88/**
@@ -140,36 +140,6 @@ export const executeTool = async <T>(
140140 }
141141}
142142
143- /**
144- * Utility function to create chain-specific error responses.
145- *
146- * @example
147- * ```typescript
148- * const response = createChainErrorResponse("invalid-chain", ["polkadot"], "check_balance");
149- * ```
150- */
151- export const createChainErrorResponse = (
152- chain : string ,
153- availableChains : string [ ] ,
154- toolName : string
155- ) : ToolResponse => {
156- const error = new ChainNotAvailableError ( chain , availableChains )
157- return createErrorResponse ( error , toolName )
158- }
159-
160- /**
161- * Utility function to create address-specific error responses.
162- *
163- * @example
164- * ```typescript
165- * const response = createAddressErrorResponse("invalid-address", "transfer");
166- * ```
167- */
168- export const createAddressErrorResponse = ( address : string , toolName : string ) : ToolResponse => {
169- const error = new InvalidAddressError ( address )
170- return createErrorResponse ( error , toolName )
171- }
172-
173143export const createAction = < T > (
174144 tool : { invoke : ( args : T ) => Promise < unknown > } ,
175145 toolConfig : { name : string ; description : string ; schema : ZodType }
@@ -182,3 +152,37 @@ export const createAction = <T>(
182152 return typeof result === "string" ? result : JSON . stringify ( result )
183153 }
184154} )
155+
156+ export function validateTools ( actions : Action [ ] , existingCustomActions : Action [ ] ) : void {
157+ const builtInToolNames = new Set ( Object . values ( ToolNames ) )
158+
159+ for ( const action of actions ) {
160+ if ( ! action . name || typeof action . name !== "string" ) {
161+ throw new Error ( "Action must have a valid 'name' property" )
162+ }
163+
164+ if ( ! action . description || typeof action . description !== "string" ) {
165+ throw new Error ( `Action '${ action . name } ' must have a valid 'description' property` )
166+ }
167+
168+ if ( ! action . schema ) {
169+ throw new Error ( `Action '${ action . name } ' must have a valid 'schema' property` )
170+ }
171+
172+ if ( ! action . invoke || typeof action . invoke !== "function" ) {
173+ throw new Error ( `Action '${ action . name } ' must have a valid 'invoke' function` )
174+ }
175+
176+ if ( builtInToolNames . has ( action . name as ToolNames ) ) {
177+ throw new Error (
178+ `Action name '${ action . name } ' conflicts with a built-in tool. Please use a different name.`
179+ )
180+ }
181+
182+ if ( existingCustomActions . some ( existing => existing . name === action . name ) ) {
183+ throw new Error (
184+ `Action name '${ action . name } ' already exists in custom actions. Please use a unique name.`
185+ )
186+ }
187+ }
188+ }
0 commit comments