UNPKG

@suiware/ai-tools

Version:

Pluggable tools for Vercel AI SDK which allow AI assistants to interact with Sui Network and perform various actions.

71 lines (68 loc) 2.49 kB
import { disableConsoleLog, NaviService, enableConsoleLog } from './chunk-BF2YFHSF.mjs'; import { __async, SuiService, getSetting } from './chunk-UXHRSDY6.mjs'; import { tool } from 'ai'; import z from 'zod'; import { SuinsClient } from '@mysten/suins'; var SuinsService = class { constructor(suiClient) { this.suiClient = suiClient; this.suinsClient = new SuinsClient({ client: this.suiClient, network: getSetting("SUI_NETWORK") }); } resolveSuinsName(name) { return __async(this, null, function* () { const nameRecord = yield this.suinsClient.getNameRecord(name); return (nameRecord == null ? void 0 : nameRecord.targetAddress) || null; }); } static isValidSuinsName(name) { return name.endsWith(".sui") || name.startsWith("@"); } }; // src/ai/tools/suiTransferTool.ts var suiTransferTool = tool({ description: "Transfer the amount of the specified coin to the specified address", parameters: z.object({ coin: z.string().refine((value) => NaviService.isSupportedCoin(value), { message: "The coin not supported" }).describe( "The target address. Suins names starting with @ or ending with .sui are supported." ), amount: z.number().describe("The amount of SUI"), address: z.string().refine( (value) => SuiService.isValidSuiAddress(value) || SuinsService.isValidSuinsName(value), { message: "Invalid Sui address" } ).describe( "The target address. Suins names starting with @ or ending with .sui are supported." ) }), execute: (_0) => __async(void 0, [_0], function* ({ coin, amount, address }) { const originalConsoleLog = disableConsoleLog(); const naviService = new NaviService(); let resolvedAddress = address; if (SuinsService.isValidSuinsName(address)) { const suinsService = new SuinsService(naviService.getSuiClient()); resolvedAddress = yield suinsService.resolveSuinsName(address); if (!resolvedAddress) { throw new Error(`Suins name ${address} not found`); } } if (resolvedAddress == naviService.getAddress()) { throw new Error("You cannot transfer to your own address"); } const txDigest = yield naviService.transfer( coin, resolvedAddress, amount ); enableConsoleLog(originalConsoleLog); return { digest: txDigest }; }) }); export { suiTransferTool }; //# sourceMappingURL=chunk-RUKH23MU.mjs.map //# sourceMappingURL=chunk-RUKH23MU.mjs.map