Skip to content

Commit 4afbd2a

Browse files
feat: Support multiple messages in Nebula API and update input props
1 parent eed734c commit 4afbd2a

File tree

6 files changed

+93
-25
lines changed

6 files changed

+93
-25
lines changed

.changeset/rude-sheep-hear.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
"thirdweb": minor
3+
---
4+
5+
Support multiple messages for Nebula API, updated input props
6+
7+
The Nebula.chat and Nebula.execute functions now support multiple input messages, and the input properties have been updated to match the http API.
8+
9+
```ts
10+
Nebula.chat({
11+
client,
12+
messages: [{ role: "user", content: "Hello" }], // new message format
13+
// context is now contextFilter
14+
contextFilter: {
15+
chains: [sepolia],
16+
},
17+
});
18+
```
19+
20+
Same changes apply to Nebula.execute.
21+
22+
```ts
23+
Nebula.execute({
24+
client,
25+
account,
26+
messages: [{ role: "user", content: "Hello" }],
27+
contextFilter: {
28+
chains: [sepolia],
29+
},
30+
});
31+
```

packages/thirdweb/src/ai/chat.test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ describe.runIf(process.env.TW_SECRET_KEY).skip("chat", () => {
99
it("should respond with a message", async () => {
1010
const response = await Nebula.chat({
1111
client: TEST_CLIENT,
12-
prompt: `What's the symbol of this contract: 0xe2cb0eb5147b42095c2FfA6F7ec953bb0bE347D8`,
13-
context: {
12+
messages: [
13+
{
14+
role: "user",
15+
content: `What's the symbol of this contract: 0xe2cb0eb5147b42095c2FfA6F7ec953bb0bE347D8`,
16+
},
17+
],
18+
contextFilter: {
1419
chains: [sepolia],
1520
},
1621
});
@@ -20,9 +25,14 @@ describe.runIf(process.env.TW_SECRET_KEY).skip("chat", () => {
2025
it("should respond with a transaction", async () => {
2126
const response = await Nebula.chat({
2227
client: TEST_CLIENT,
23-
prompt: `send 0.0001 ETH on sepolia to ${TEST_ACCOUNT_B.address}`,
28+
messages: [
29+
{
30+
role: "user",
31+
content: `send 0.0001 ETH on sepolia to ${TEST_ACCOUNT_B.address}`,
32+
},
33+
],
2434
account: TEST_ACCOUNT_A,
25-
context: {
35+
contextFilter: {
2636
chains: [sepolia],
2737
walletAddresses: [TEST_ACCOUNT_A.address],
2838
},

packages/thirdweb/src/ai/chat.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { type Input, type Output, nebulaFetch } from "./common.js";
55
*
66
* @param input - The input for the chat.
77
* @returns The chat response.
8-
* @beta
8+
* @beta This API is in early access and might change in the future.
99
* @nebula
1010
*
1111
* @example
@@ -14,8 +14,13 @@ import { type Input, type Output, nebulaFetch } from "./common.js";
1414
*
1515
* const response = await Nebula.chat({
1616
* client: TEST_CLIENT,
17-
* prompt: "What's the symbol of this contract: 0xe2cb0eb5147b42095c2FfA6F7ec953bb0bE347D8",
18-
* context: {
17+
* messages: [
18+
* {
19+
* role: "user",
20+
* content: "What's the symbol of this contract: 0xe2cb0eb5147b42095c2FfA6F7ec953bb0bE347D8",
21+
* },
22+
* ],
23+
* contextFilter: {
1924
* chains: [sepolia],
2025
* },
2126
* });

packages/thirdweb/src/ai/common.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ const NEBULA_API_URL = "https://nebula-api.thirdweb.com";
1515

1616
export type Input = {
1717
client: ThirdwebClient;
18-
prompt: string | string[];
18+
messages: {
19+
role: "user" | "assistant";
20+
content: string;
21+
}[];
1922
account?: Account;
20-
context?: {
23+
contextFilter?: {
2124
chains?: Chain[];
2225
walletAddresses?: string[];
2326
contractAddresses?: string[];
@@ -52,7 +55,7 @@ export async function nebulaFetch(
5255
"Content-Type": "application/json",
5356
},
5457
body: JSON.stringify({
55-
message: input.prompt, // TODO: support array of messages
58+
messages: input.messages,
5659
session_id: input.sessionId,
5760
...(input.account
5861
? {
@@ -62,13 +65,15 @@ export async function nebulaFetch(
6265
},
6366
}
6467
: {}),
65-
...(input.context
68+
...(input.contextFilter
6669
? {
6770
context_filter: {
6871
chain_ids:
69-
input.context.chains?.map((c) => c.id.toString()) || [],
70-
signer_wallet_address: input.context.walletAddresses || [],
71-
contract_addresses: input.context.contractAddresses || [],
72+
input.contextFilter.chains?.map((c) => c.id.toString()) || [],
73+
wallet_addresses:
74+
input.contextFilter.walletAddresses ||
75+
(input.account ? [input.account.address] : []),
76+
contract_addresses: input.contextFilter.contractAddresses || [],
7277
},
7378
}
7479
: {}),

packages/thirdweb/src/ai/execute.test.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ describe.runIf(process.env.TW_SECRET_KEY).skip("execute", () => {
1111
await expect(
1212
Nebula.execute({
1313
client: TEST_CLIENT,
14-
prompt: `send 0.0001 ETH to ${TEST_ACCOUNT_B.address}`,
14+
messages: [
15+
{
16+
role: "user",
17+
content: `send 0.0001 ETH to ${TEST_ACCOUNT_B.address}`,
18+
},
19+
],
1520
account: TEST_ACCOUNT_A,
16-
context: {
21+
contextFilter: {
1722
chains: [sepolia],
1823
walletAddresses: [TEST_ACCOUNT_A.address],
1924
},
@@ -22,7 +27,7 @@ describe.runIf(process.env.TW_SECRET_KEY).skip("execute", () => {
2227
});
2328

2429
// TODO make this work reliably
25-
it.skip("should execute a contract call", async () => {
30+
it("should execute a contract call", async () => {
2631
const nftContract = getContract({
2732
client: TEST_CLIENT,
2833
chain: sepolia,
@@ -31,11 +36,15 @@ describe.runIf(process.env.TW_SECRET_KEY).skip("execute", () => {
3136

3237
const response = await Nebula.execute({
3338
client: TEST_CLIENT,
34-
prompt: `approve 1 token of token id 0 to ${TEST_ACCOUNT_B.address} using the approve function`,
39+
messages: [
40+
{
41+
role: "user",
42+
content: `approve 1 token of token id 0 to ${TEST_ACCOUNT_B.address} using the approve function`,
43+
},
44+
],
3545
account: TEST_ACCOUNT_A,
36-
context: {
46+
contextFilter: {
3747
chains: [nftContract.chain],
38-
walletAddresses: [TEST_ACCOUNT_A.address],
3948
contractAddresses: [nftContract.address],
4049
},
4150
});

packages/thirdweb/src/ai/execute.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,26 @@ import { type Input, nebulaFetch } from "./common.js";
88
*
99
* @param input - The input for the transaction.
1010
* @returns The transaction hash.
11-
* @beta
11+
* @beta This API is in early access and might change in the future.
1212
* @nebula
1313
*
1414
* @example
1515
* ```ts
1616
* import { Nebula } from "thirdweb/ai";
1717
*
18+
* const wallet = createWallet("io.metamask");
19+
* const account = wallet.connect({ client });
20+
*
1821
* const result = await Nebula.execute({
19-
* client: TEST_CLIENT,
20-
* prompt: "send 0.0001 ETH to vitalik.eth",
21-
* account: TEST_ACCOUNT_A,
22-
* context: {
22+
* client,
23+
* account, // transactions will be sent from this account
24+
* messages: [
25+
* {
26+
* role: "user",
27+
* content: "send 0.0001 ETH to vitalik.eth",
28+
* },
29+
* ],
30+
* contextFilter: {
2331
* chains: [sepolia],
2432
* },
2533
* });

0 commit comments

Comments
 (0)