Skip to content

Commit 7a598cc

Browse files
authored
feat: add vercel ai sdk (#101)
1 parent b58c7de commit 7a598cc

File tree

8 files changed

+224
-178
lines changed

8 files changed

+224
-178
lines changed

packages/common/src/chains/chains.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
westendAssetHubChain,
1212
westendChain,
1313
westendPeopleChain
14-
} from "./supported-chains"
14+
} from "./supportedChains"
1515

1616
export type ChainIdRelay = "polkadot" | "west" | "paseo" | "kusama"
1717
export type ChainIdAssetHub =
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export * from "./chains"
2-
export * from "./supported-chains"
2+
export * from "./supportedChains"
File renamed without changes.

packages/sdk/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,8 @@
6262
"@langchain/ollama": "^0.2.2",
6363
"langchain": "^0.1.21",
6464
"@agentic/langchain": "^7.6.9"
65+
},
66+
"peerDependencies": {
67+
"ai": "^4.1.16"
6568
}
6669
}

packages/sdk/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./api"
22
export * from "./langchain"
3+
export * from "./vercel"

packages/sdk/src/vercel.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { Action } from "@polkadot-agent-kit/llm"
2+
import type { Tool } from "ai"
3+
import { tool, type ToolSet } from "ai"
4+
import type { z } from "zod"
5+
6+
import type { PolkadotAgentKit } from "./api"
7+
8+
/**
9+
* Get Vercel AI SDK tools from an PolkadotAgentKit instance
10+
*
11+
* @param polkadotAgentKit - The PolkadotAgentKit instance
12+
* @returns An object containing Vercel AI SDK tools keyed by tool name
13+
*/
14+
export function getVercelAITools(polkadotAgentKit: PolkadotAgentKit): ToolSet {
15+
const actions: Action[] = polkadotAgentKit.getActions()
16+
const toolSet: Record<string, Tool> = {}
17+
18+
for (const action of actions) {
19+
toolSet[action.name] = tool({
20+
description: action.description,
21+
parameters: action.schema,
22+
execute: async (args: z.infer<typeof action.schema>) => {
23+
const result = await action.invoke(args)
24+
return result
25+
}
26+
})
27+
}
28+
29+
return toolSet
30+
}

packages/sdk/tests/e2e/sdk.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest';
22
import { PolkadotAgentKit } from '../../src/api';
3+
import { getVercelAITools } from '../../src/vercel';
34
import type { AgentConfig } from '@polkadot-agent-kit/common';
45

56
const mockBalanceResult = { balance: '100.00', symbol: 'WND', chain: 'westend' };
@@ -58,6 +59,20 @@ vi.mock('../../src/api', () => {
5859
mintVdotTool: vi.fn(() => ({
5960
call: vi.fn(async (input: any) => mockMintVdotResult)
6061
})),
62+
getActions: vi.fn(() => [
63+
{
64+
name: 'check_balance',
65+
description: 'Check balance of the wallet address on a specific chain',
66+
schema: {} as any,
67+
invoke: vi.fn(async (args: any) => JSON.stringify(mockBalanceResult))
68+
},
69+
{
70+
name: 'transfer_native',
71+
description: 'Transfer native tokens to an address',
72+
schema: {} as any,
73+
invoke: vi.fn(async (args: any) => JSON.stringify(mockTransferResult))
74+
}
75+
]),
6176
disconnect: vi.fn().mockResolvedValue(undefined),
6277
config: instanceConfig,
6378
};
@@ -282,5 +297,27 @@ describe('PolkadotAgentKit E2E', () => {
282297
assert(result);
283298
});
284299
});
300+
301+
describe('getVercelAITools', () => {
302+
it('should return a toolset with correct structure', () => {
303+
const toolSet = getVercelAITools(agent);
304+
305+
expect(toolSet).toBeDefined();
306+
expect(typeof toolSet).toBe('object');
307+
expect(toolSet.check_balance).toBeDefined();
308+
expect(toolSet.transfer_native).toBeDefined();
309+
310+
// Check that the tools have the expected structure
311+
expect(typeof toolSet.check_balance.execute).toBe('function');
312+
expect(typeof toolSet.transfer_native.execute).toBe('function');
313+
});
314+
315+
it('should have tools with correct descriptions', () => {
316+
const toolSet = getVercelAITools(agent);
317+
318+
expect(toolSet.check_balance.description).toBe('Check balance of the wallet address on a specific chain');
319+
expect(toolSet.transfer_native.description).toBe('Transfer native tokens to an address');
320+
});
321+
});
285322
});
286323

0 commit comments

Comments
 (0)