Skip to content

Commit 34239ea

Browse files
[Docs] Rename Chat API to Blockchain LLM and add transaction execution docs (#7904)
1 parent 8828df0 commit 34239ea

File tree

11 files changed

+104
-31
lines changed

11 files changed

+104
-31
lines changed

apps/playground-web/src/app/ai/api/chat.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export async function promptNebula(params: {
119119

120120
if (data.type === "sign_transaction") {
121121
try {
122-
const parsedTxData = JSON.parse(data.data) as NebulaTxData;
122+
const parsedTxData = data.data as NebulaTxData;
123123
params.handleStream({
124124
data: parsedTxData,
125125
event: "action",
@@ -133,7 +133,7 @@ export async function promptNebula(params: {
133133

134134
if (data.type === "sign_swap") {
135135
try {
136-
const swapData = JSON.parse(data.data) as NebulaSwapData;
136+
const swapData = data.data as NebulaSwapData;
137137
params.handleStream({
138138
data: swapData,
139139
event: "action",

apps/playground-web/src/app/ai/api/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export type NebulaUserMessage = {
2222
};
2323

2424
export type NebulaTxData = {
25-
chainId: number;
25+
chain_id: number;
2626
data: `0x${string}`;
2727
to: string;
28-
value?: string;
28+
value: string;
2929
};

apps/playground-web/src/app/ai/chat/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ export default function ChatPage() {
1313
}
1414

1515
export const metadata = {
16-
title: "AI Chat API - Playground",
16+
title: "Blockchain LLM - Playground",
1717
description: "Chat with thirdweb AI for blockchain interactions",
1818
};

apps/playground-web/src/app/ai/components/ChatPageContent.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ type ChatMessage =
102102
};
103103

104104
type NebulaTxData = {
105-
chainId: number;
105+
chain_id: number;
106106
data: `0x${string}`;
107107
to: string;
108108
value?: string;
@@ -703,7 +703,7 @@ function RenderMessage(props: {
703703
transaction={() =>
704704
prepareTransaction({
705705
client: THIRDWEB_CLIENT,
706-
chain: defineChain(message.data.chainId),
706+
chain: defineChain(message.data.chain_id),
707707
data: message.data.data,
708708
to: message.data.to,
709709
value: message.data.value
@@ -719,7 +719,7 @@ function RenderMessage(props: {
719719
props.sendMessage({
720720
content: [
721721
{
722-
chain_id: message.data.chainId,
722+
chain_id: message.data.chain_id,
723723
transaction_hash: tx.transactionHash,
724724
type: "transaction",
725725
},
@@ -734,7 +734,7 @@ function RenderMessage(props: {
734734
) : (
735735
<ConnectButton
736736
client={THIRDWEB_CLIENT}
737-
chain={defineChain(message.data.chainId)}
737+
chain={defineChain(message.data.chain_id)}
738738
/>
739739
)}
740740
</div>

apps/playground-web/src/app/navLinks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const ai: ShadcnSidebarLink = {
1818
links: [
1919
{
2020
href: "/ai/chat",
21-
label: "Chat API",
21+
label: "Blockchain LLM",
2222
},
2323
],
2424
};

apps/portal/src/app/Header.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ const apisLinks = [
144144
const aiLinks = [
145145
{
146146
href: "/ai/chat",
147-
name: "Chat API",
147+
name: "Blockchain LLM",
148148
},
149149
{
150150
href: "/ai/mcp",
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {OpenApiEndpoint} from "@doc";
2+
3+
# Transaction Execution
4+
5+
thirdweb AI can either auto execute transactions or prepare them for signing.
6+
7+
- **Auto Execute**: The API will auto execute the transaction. Requires wallet authentication.
8+
- **Prepare for Signing**: The API will only prepare the transaction for signing. The user will need to sign the transaction manually.
9+
10+
## Auto Execute
11+
12+
Requirements:
13+
14+
- The context object must be set with:
15+
- `from` set to the wallet address to execute the transaction from
16+
- `auto_execute_transaction` set to `true`
17+
- (Optional) `chain_ids` set to the chain IDs to execute the transaction on
18+
- The API must be called with either:
19+
- your `x-secret-key` header for server wallet authentication
20+
- OR a `Authorization` header with a valid wallet JWT for user wallet authentication
21+
22+
Model behavior:
23+
24+
If all the requirements are met, the transaction will be executed automatically and the model will return a message with the transaction ID.
25+
26+
It will also include an `actions` array with a `monitor_transaction` action. This action can be used to monitor the transaction status and get the transaction receipt via the thirdweb API.
27+
28+
<OpenApiEndpoint path="/ai/chat" method="POST" requestBodyOverride={{
29+
"messages": [
30+
{
31+
"role": "user",
32+
"content": "Transfer 10 USDC to vitalik.eth"
33+
}
34+
],
35+
"context": {
36+
"from": "0x...",
37+
"auto_execute_transaction": true,
38+
"chain_ids": [8453]
39+
}
40+
}} responseExampleOverride={{
41+
"message": "I've sent 10 USDC to Vitalik's wallet with transaction ID 11cd95e3-c0ee-4468-97c1-fd05dc919cdb. It should be confirmed in a few seconds.",
42+
"session_id": "123",
43+
"request_id": "456",
44+
"actions": [{
45+
"session_id": "8d1bfac4-e6c0-473b-bbb6-649f459d37e6",
46+
"request_id": "79066871-b86f-4814-8027-5f34d99df389",
47+
"source": "model",
48+
"type": "monitor_transaction",
49+
"data": {
50+
"transaction_id": "11cd95e3-c0ee-4468-97c1-fd05dc919cdb"
51+
}
52+
}]
53+
}} />
54+
55+
## Prepare for Signing
56+
57+
When the `auto_execute` parameter is set to `false` or omitted, the API will prepare the transaction for signing and return the transaction data in the `actions` array as a 'sign_transaction' action type.
58+
59+
<OpenApiEndpoint path="/ai/chat" method="POST" />

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { Tabs, TabsContent, TabsList, TabsTrigger, OpenApiEndpoint } from "@doc";
22

3-
# Chat API
3+
# Blockchain LLM
44

5-
The thirdweb AI chat API is a standard OpenAI-compatible chat completion API that allows you to interact with the thirdweb AI model, optimized for blockchain interactions.
5+
The thirdweb API exposes a standard OpenAI-compatible chat completion API that allows you to interact with the thirdweb AI model, optimized for blockchain interactions.
66

77
- Query real-time data from the blockchain
88
- Analyze transactions
99
- Fetch token balances, prices and metadata
10-
- Prepare any contract call or transaction for signing
11-
- Prepare swaps from/to any token pair
10+
- Execute or prepare any contract call or transaction
11+
- Execute or prepare swaps from/to any token pair
1212
- Deploy contracts
1313
- Generate images
1414
- Search the web

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ data: {
5858
"request_id": "9efc7f6a-8576-4d9c-8603-f6c72aa72164",
5959
"type": "sign_transaction",
6060
"source": "executor",
61-
"data": "{\"chainId\": 11155111, \"to\": \"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045\", \"data\": \"0x\", \"value\": \"0x5af3107a4000\"}"
61+
"data": {
62+
"chain_id": 11155111,
63+
"to": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
64+
"data": "0x",
65+
"value": "0x5af3107a4000"
66+
}
6267
}
6368
```
6469

@@ -90,52 +95,57 @@ for await (const event of events) {
9095
if (!event.data) {
9196
continue;
9297
}
98+
const parsedEventData = JSON.parse(event.data);
9399
switch (event.event) {
94100
case "init": {
95-
console.log("Init event", event.data);
101+
console.log("Init event", parsedEventData);
96102
// handle init event (session id and request id)
97103
break;
98104
}
99105
case "presence": {
100-
console.log("Presence event", event.data);
106+
console.log("Presence event", parsedEventData);
101107
// handle intermediate thinking steps
102108
break;
103109
}
104110
case "delta": {
105-
console.log("Delta event", event.data);
111+
console.log("Delta event", parsedEventData);
106112
// handle delta event (streamed output text response)
107113
break;
108114
}
109115
case "action": {
110-
const data = JSON.parse(event.data);
111-
112116
// handle transaction signing
113-
if (data.type === "sign_transaction") {
114-
const transactionData = JSON.parse(data.data);
117+
if (parsedEventData.type === "sign_transaction") {
118+
const transactionData = parsedEventData.data;
115119
console.log("Sign transaction event", transactionData);
116120
}
117121

118122
// handle swap signing
119-
if (data.type === "sign_swap") {
120-
const swapData = JSON.parse(data.data);
123+
if (parsedEventData.type === "sign_swap") {
124+
const swapData = parsedEventData.data;
121125
console.log("Sign swap event", swapData);
122126
}
123127

128+
// handle transaction monitoring
129+
if (parsedEventData.type === "monitor_transaction") {
130+
const transactionId = parsedEventData.data.transaction_id;
131+
console.log("Monitor transaction event", transactionId);
132+
}
133+
124134
break;
125135
}
126136
case "image": {
127-
const data = JSON.parse(event.data);
137+
const imageData = parsedEventData.data;
128138
// handle image rendering
129-
console.log("Image data", data);
139+
console.log("Image data", imageData);
130140
break;
131141
}
132142
case "context": {
133-
console.log("Context event", event.data);
143+
console.log("Context event", parsedEventData);
134144
// handle context changes (chain ids, wallet address, etc)
135145
break;
136146
}
137147
case "error": {
138-
console.log("Error event", event.data);
148+
console.log("Error event", parsedEventData);
139149
// handle error event (error message)
140150
break;
141151
}

apps/portal/src/app/ai/sidebar.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { SideBar } from "@/components/Layouts/DocLayout";
44
export const sidebar: SideBar = {
55
links: [
66
{
7-
name: "Chat API",
7+
name: "Blockchain LLM",
88
isCollapsible: false,
99
links: [
1010
{
@@ -17,6 +17,10 @@ export const sidebar: SideBar = {
1717
href: "https://playground.thirdweb.com/ai/chat",
1818
icon: <ExternalLinkIcon />,
1919
},
20+
{
21+
name: "Transaction Execution",
22+
href: "/ai/chat/execution",
23+
},
2024
{
2125
name: "Streaming Responses",
2226
href: "/ai/chat/streaming",

0 commit comments

Comments
 (0)