-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support custom action provider (#214)
- Loading branch information
Showing
3 changed files
with
92 additions
and
5 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
cdp-agentkit-core/typescript/src/action-providers/customActionProvider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import { z } from "zod"; | ||
import { CreateAction } from "./actionDecorator"; | ||
import { ActionProvider } from "./actionProvider"; | ||
import { Network } from "../network"; | ||
import { WalletProvider } from "../wallet-providers"; | ||
|
||
interface CustomActionProviderOptions<TWalletProvider extends WalletProvider> { | ||
name: string; | ||
description: string; | ||
schema: z.ZodSchema; | ||
invoke: | ||
| ((args: any) => Promise<any>) | ||
| ((walletProvider: TWalletProvider, args: any) => Promise<any>); | ||
} | ||
|
||
/** | ||
* CustomActionProvider is a custom action provider that allows for custom action registration | ||
*/ | ||
export class CustomActionProvider<TWalletProvider extends WalletProvider> extends ActionProvider { | ||
/** | ||
* Creates a new CustomActionProvider that dynamically adds decorated action methods | ||
* | ||
* @param actions - Array of custom actions to be added to the provider | ||
*/ | ||
constructor(actions: CustomActionProviderOptions<TWalletProvider>[]) { | ||
super("custom", []); | ||
|
||
actions.forEach(({ name, description, schema, invoke }) => { | ||
// Check if the invoke function expects a wallet provider | ||
const takesWalletProvider = invoke.length === 2; | ||
|
||
// Define the method on the prototype with the correct signature | ||
Object.defineProperty(CustomActionProvider.prototype, name, { | ||
value: takesWalletProvider | ||
? async function (walletProvider: WalletProvider, args: unknown) { | ||
const parsedArgs = schema.parse(args); | ||
return await (invoke as any)(walletProvider, parsedArgs); | ||
} | ||
: async function (args: unknown) { | ||
const parsedArgs = schema.parse(args); | ||
return await (invoke as any)(parsedArgs); | ||
}, | ||
configurable: true, | ||
writable: true, | ||
enumerable: true, | ||
}); | ||
|
||
// Manually set the parameter metadata | ||
const paramTypes = takesWalletProvider ? [WalletProvider, Object] : [Object]; | ||
Reflect.defineMetadata("design:paramtypes", paramTypes, CustomActionProvider.prototype, name); | ||
|
||
// Apply the decorator using original name | ||
const decoratedMethod = CreateAction({ | ||
name, | ||
description, | ||
schema, | ||
})( | ||
CustomActionProvider.prototype, | ||
name, | ||
Object.getOwnPropertyDescriptor(CustomActionProvider.prototype, name)!, | ||
); | ||
|
||
// Add the decorated method to the instance | ||
Object.defineProperty(this, name, { | ||
value: decoratedMethod, | ||
configurable: true, | ||
writable: true, | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Custom action providers are supported on all networks | ||
* | ||
* @param _ - The network to checkpointSaver | ||
* @returns true | ||
*/ | ||
supportsNetwork(_: Network): boolean { | ||
return true; | ||
} | ||
} | ||
|
||
export const customActionProvider = <TWalletProvider extends WalletProvider>( | ||
actions: | ||
| CustomActionProviderOptions<TWalletProvider> | ||
| CustomActionProviderOptions<TWalletProvider>[], | ||
) => new CustomActionProvider<TWalletProvider>(Array.isArray(actions) ? actions : [actions]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters