Skip to content

Commit 40ba112

Browse files
Refactor AI types and move definitions to types.ts
1 parent 89eeac1 commit 40ba112

File tree

3 files changed

+91
-151
lines changed

3 files changed

+91
-151
lines changed

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

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,11 @@
11
import { stream } from "fetch-event-stream";
2-
import type { NebulaTxData, NebulaUserMessage } from "./types";
3-
4-
const API_URL = `https://${process.env.NEXT_PUBLIC_API_URL || "api.thirdweb.com"}`;
5-
6-
export type NebulaContext = {
7-
chainIds: string[] | null;
8-
walletAddress: string | null;
9-
sessionId: string | null;
10-
};
11-
12-
type NebulaSwapData = {
13-
action: string;
14-
transaction: {
15-
chainId: number;
16-
to: `0x${string}`;
17-
data: `0x${string}`;
18-
};
19-
to: {
20-
address: `0x${string}`;
21-
amount: string;
22-
chain_id: number;
23-
decimals: number;
24-
symbol: string;
25-
};
26-
from: {
27-
address: `0x${string}`;
28-
amount: string;
29-
chain_id: number;
30-
decimals: number;
31-
symbol: string;
32-
};
33-
intent: {
34-
amount: string;
35-
destinationChainId: number;
36-
destinationTokenAddress: `0x${string}`;
37-
originChainId: number;
38-
originTokenAddress: `0x${string}`;
39-
receiver: `0x${string}`;
40-
sender: `0x${string}`;
41-
};
42-
};
2+
import {
3+
API_URL,
4+
type NebulaContext,
5+
type NebulaSwapData,
6+
type NebulaTxData,
7+
type NebulaUserMessage,
8+
} from "./types";
439

4410
export async function promptNebula(params: {
4511
message: NebulaUserMessage;

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export const API_URL = `https://${process.env.NEXT_PUBLIC_API_URL || "api.thirdweb.com"}`;
2+
13
type NebulaUserMessageContentItem =
24
| {
35
type: "image";
@@ -27,3 +29,75 @@ export type NebulaTxData = {
2729
to: string;
2830
value: string;
2931
};
32+
33+
export type NebulaContext = {
34+
chainIds: string[] | null;
35+
walletAddress: string | null;
36+
sessionId: string | null;
37+
};
38+
39+
export type NebulaSwapData = {
40+
action: string;
41+
transaction: {
42+
chain_id: number;
43+
to: `0x${string}`;
44+
data: `0x${string}`;
45+
value: string;
46+
};
47+
intent: {
48+
amount: string;
49+
destinationChainId: number;
50+
destinationTokenAddress: `0x${string}`;
51+
originChainId: number;
52+
originTokenAddress: `0x${string}`;
53+
receiver: `0x${string}`;
54+
sender: `0x${string}`;
55+
};
56+
};
57+
58+
// Simplified types for the playground version
59+
export type WalletMeta = {
60+
walletId: string;
61+
address: string;
62+
};
63+
64+
export type ChatMessage =
65+
| {
66+
type: "user";
67+
content: NebulaUserMessageContent;
68+
}
69+
| {
70+
text: string;
71+
type: "error";
72+
}
73+
| {
74+
texts: string[];
75+
type: "presence";
76+
}
77+
| {
78+
// assistant type message loaded from history doesn't have request_id
79+
request_id: string | undefined;
80+
text: string;
81+
type: "assistant";
82+
}
83+
| {
84+
type: "action";
85+
subtype: "sign_transaction";
86+
request_id: string;
87+
data: NebulaTxData;
88+
}
89+
| {
90+
type: "action";
91+
subtype: "sign_swap";
92+
request_id: string;
93+
data: NebulaSwapData;
94+
}
95+
| {
96+
type: "image";
97+
request_id: string;
98+
data: {
99+
width: number;
100+
height: number;
101+
url: string;
102+
};
103+
};

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

Lines changed: 10 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -31,116 +31,16 @@ import { MarkdownRenderer } from "@/components/ui/markdown-renderer";
3131
import { Img } from "../../../components/ui/Img";
3232
import { Spinner } from "../../../components/ui/Spinner";
3333
import { THIRDWEB_CLIENT } from "../../../lib/client";
34-
import { type NebulaContext, promptNebula } from "../api/chat";
35-
import type { NebulaUserMessage } from "../api/types";
34+
import { promptNebula } from "../api/chat";
35+
import type {
36+
ChatMessage,
37+
NebulaContext,
38+
NebulaUserMessage,
39+
WalletMeta,
40+
} from "../api/types";
3641
import { examplePrompts } from "../data/examplePrompts";
3742
import { resolveSchemeWithErrorHandler } from "./resolveSchemeWithErrorHandler";
3843

39-
// Simplified types for the playground version
40-
type WalletMeta = {
41-
walletId: string;
42-
address: string;
43-
};
44-
45-
type NebulaUserMessageContentItem =
46-
| {
47-
type: "image";
48-
image_url: string | null;
49-
b64: string | null;
50-
}
51-
| {
52-
type: "text";
53-
text: string;
54-
}
55-
| {
56-
type: "transaction";
57-
transaction_hash: string;
58-
chain_id: number;
59-
};
60-
61-
type NebulaUserMessageContent = NebulaUserMessageContentItem[];
62-
63-
type ChatMessage =
64-
| {
65-
type: "user";
66-
content: NebulaUserMessageContent;
67-
}
68-
| {
69-
text: string;
70-
type: "error";
71-
}
72-
| {
73-
texts: string[];
74-
type: "presence";
75-
}
76-
| {
77-
// assistant type message loaded from history doesn't have request_id
78-
request_id: string | undefined;
79-
text: string;
80-
type: "assistant";
81-
}
82-
| {
83-
type: "action";
84-
subtype: "sign_transaction";
85-
request_id: string;
86-
data: NebulaTxData;
87-
}
88-
| {
89-
type: "action";
90-
subtype: "sign_swap";
91-
request_id: string;
92-
data: NebulaSwapData;
93-
}
94-
| {
95-
type: "image";
96-
request_id: string;
97-
data: {
98-
width: number;
99-
height: number;
100-
url: string;
101-
};
102-
};
103-
104-
type NebulaTxData = {
105-
chain_id: number;
106-
data: `0x${string}`;
107-
to: string;
108-
value?: string;
109-
};
110-
111-
type NebulaSwapData = {
112-
action: string;
113-
transaction: {
114-
chainId: number;
115-
to: `0x${string}`;
116-
data: `0x${string}`;
117-
value?: string;
118-
};
119-
to: {
120-
address: `0x${string}`;
121-
amount: string;
122-
chain_id: number;
123-
decimals: number;
124-
symbol: string;
125-
};
126-
from: {
127-
address: `0x${string}`;
128-
amount: string;
129-
chain_id: number;
130-
decimals: number;
131-
symbol: string;
132-
};
133-
intent: {
134-
amount: string;
135-
destinationChainId: number;
136-
destinationTokenAddress: `0x${string}`;
137-
originChainId: number;
138-
originTokenAddress: `0x${string}`;
139-
receiver: `0x${string}`;
140-
sender: `0x${string}`;
141-
};
142-
};
143-
14444
export function ChatPageContent(props: {
14545
client: ThirdwebClient;
14646
type: "landing" | "new-chat";
@@ -752,7 +652,7 @@ function RenderMessage(props: {
752652
transaction={() =>
753653
prepareTransaction({
754654
client: THIRDWEB_CLIENT,
755-
chain: defineChain(message.data.transaction.chainId),
655+
chain: defineChain(message.data.transaction.chain_id),
756656
data: message.data.transaction.data,
757657
to: message.data.transaction.to,
758658
value: message.data.transaction.value
@@ -768,7 +668,7 @@ function RenderMessage(props: {
768668
props.sendMessage({
769669
content: [
770670
{
771-
chain_id: message.data.transaction.chainId,
671+
chain_id: message.data.transaction.chain_id,
772672
transaction_hash: tx.transactionHash,
773673
type: "transaction",
774674
},
@@ -785,7 +685,7 @@ function RenderMessage(props: {
785685
) : (
786686
<ConnectButton
787687
client={THIRDWEB_CLIENT}
788-
chain={defineChain(message.data.transaction.chainId)}
688+
chain={defineChain(message.data.transaction.chain_id)}
789689
/>
790690
)}
791691
</div>

0 commit comments

Comments
 (0)