Skip to content

Commit ac3a95e

Browse files
[AI SDK Provider] Fix type inference for tools (#7944)
1 parent 00ffe3e commit ac3a95e

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

.changeset/cool-jeans-end.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@thirdweb-dev/ai-sdk-provider": patch
3+
---
4+
5+
Proper type inference

apps/portal/src/app/ai/chat/ai-sdk/page.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Usage with the Vercel AI SDK
22

3-
The `@thirdweb-dev/ai-sdk-provider` A thin provider that plugs thirdweb into the [Vercel AI SDK](https://ai-sdk.dev/docs/introduction).
3+
The `@thirdweb-dev/ai-sdk-provider` is a lightweight provider that lets you use thirdweb AI with the [Vercel AI SDK](https://ai-sdk.dev/docs/introduction).
44

55
It standardizes message parts, exposes wallet-aware tools (`sign_transaction`, `sign_swap`), and ships types/utilities so you can build chat UIs that execute on-chain actions from AI responses.
66

@@ -67,7 +67,6 @@ export async function POST(req: Request) {
6767
},
6868
});
6969
}
70-
7170
```
7271

7372
#### Client side (React, using `useChat`)

apps/portal/src/app/ai/chat/page.mdx

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,50 @@ The thirdweb API exposes a standard OpenAI-compatible chat completion API that a
1414
- Search the web
1515
- And more!
1616

17-
You can use the API with the HTTP API directly, or with any OpenAI-compatible client library.
17+
You can use the API with the HTTP API directly, the AI SDK or with any OpenAI-compatible client library.
1818

1919
<Tabs defaultValue="api">
2020
<TabsList>
2121
<TabsTrigger value="api">HTTP API</TabsTrigger>
22+
<TabsTrigger value="ai-sdk">Vercel AI SDK</TabsTrigger>
2223
<TabsTrigger value="openai">OpenAI Client</TabsTrigger>
2324
</TabsList>
2425
<TabsContent value="api">
2526
<OpenApiEndpoint path="/ai/chat" method="POST" />
2627
</TabsContent>
28+
29+
<TabsContent value="ai-sdk">
30+
### Usage with the Vercel AI SDK
31+
32+
The `@thirdweb-dev/ai-sdk-provider` is a lightweight provider that lets you use thirdweb AI with the [Vercel AI SDK](https://ai-sdk.dev/docs/introduction).
33+
34+
```ts
35+
import { streamText } from "ai";
36+
import { createThirdwebAI } from "@thirdweb-dev/ai-sdk-provider";
37+
38+
const thirdwebAI = createThirdwebAI({
39+
secretKey: "<your-project-secret-key>",
40+
});
41+
42+
const result = streamText({
43+
model: thirdwebAI.chat({
44+
context: {
45+
chain_ids: [8453], // optional chain ids
46+
from: "0x...", // optional wallet address
47+
auto_execute_transactions: true, // defaults to false
48+
},
49+
}),
50+
tools: thirdwebAI.tools(),
51+
messages: [
52+
{
53+
role: "user",
54+
content: "Swap 0.01 ETH to USDC",
55+
},
56+
],
57+
});
58+
```
59+
</TabsContent>
60+
2761
<TabsContent value="openai">
2862

2963
### Using the OpenAI Client
@@ -41,7 +75,7 @@ chat_completion = client.chat.completions.create(
4175
model="t0",
4276
messages=[{"role": "user", "content": "Transfer 10 USDC to vitalik.eth"}],
4377
stream=False,
44-
extra_body={ "context": { "from": "0x...", "chain_ids": [8453] }}
78+
extra_body={ "context": { "from": "0x...", "chain_ids": [8453] }}
4579
)
4680

4781
print(chat_completion)
@@ -52,4 +86,6 @@ print(chat_completion)
5286
### Going further
5387

5488
- [Handle streaming responses](/ai/chat/streaming)
89+
- [Transaction Execution](/ai/chat/execution)
90+
- [Use the Vercel AI SDK](/ai/chat/ai-sdk)
5591
- [Full API Reference](https://api.thirdweb.com/reference#tag/ai/post/ai/chat)

packages/ai-sdk-provider/src/tools.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,36 +48,36 @@ export type MonitorTransactionOutput = z.infer<
4848
typeof AgentActionMonitorTransactionOutputData
4949
>;
5050

51-
export function createTools(_config: ThirdwebConfig): Record<string, Tool> {
51+
export type SignTransactionInput = z.infer<
52+
typeof AgentActionSignTransactionData
53+
>;
54+
export type SignSwapInput = z.infer<typeof AgentActionSignSwapData>;
55+
export type MonitorTransactionInput = z.infer<
56+
typeof AgentActionMonitorTransactionData
57+
>;
58+
59+
export function createTools(_config: ThirdwebConfig) {
5260
return {
5361
sign_transaction: {
5462
id: "thirdweb.sign_transaction" as const,
5563
name: "sign_transaction",
5664
description: "Sign a transaction",
5765
inputSchema: AgentActionSignTransactionData,
5866
outputSchema: AgentActionTransactionOutputData,
59-
} as const,
67+
} satisfies Tool<SignTransactionInput, TransactionOutput>,
6068
sign_swap: {
6169
id: "thirdweb.sign_swap" as const,
6270
name: "sign_swap",
6371
description: "Sign a swap transaction",
6472
inputSchema: AgentActionSignSwapData,
6573
outputSchema: AgentActionTransactionOutputData,
66-
} as const,
74+
} satisfies Tool<SignSwapInput, TransactionOutput>,
6775
monitor_transaction: {
6876
id: "thirdweb.monitor_transaction" as const,
6977
name: "monitor_transaction",
7078
description: "Monitor a transaction",
7179
inputSchema: AgentActionMonitorTransactionData,
7280
outputSchema: AgentActionMonitorTransactionOutputData,
73-
} as const,
81+
} satisfies Tool<MonitorTransactionInput, MonitorTransactionOutput>,
7482
} as const;
7583
}
76-
77-
export type SignTransactionInput = z.infer<
78-
typeof AgentActionSignTransactionData
79-
>;
80-
export type SignSwapInput = z.infer<typeof AgentActionSignSwapData>;
81-
export type MonitorTransactionInput = z.infer<
82-
typeof AgentActionMonitorTransactionData
83-
>;

0 commit comments

Comments
 (0)