Skip to content

Commit 2d6ed38

Browse files
committed
feat: 1.using generateObject and templated type safe schema. 2.naming convention camelCase. 3.create a template and type file
1 parent cf0dc4f commit 2d6ed38

File tree

3 files changed

+57
-45
lines changed

3 files changed

+57
-45
lines changed

packages/plugin-nft-generation/src/actions/mintNFTAction.ts

+28-45
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {
22
Action,
33
composeContext,
4-
Content,
54
elizaLogger,
6-
generateObjectDeprecated,
5+
generateObject,
76
HandlerCallback,
87
IAgentRuntime,
98
Memory,
@@ -15,34 +14,10 @@ import { verifyNFT } from "../handlers/verifyNFT.ts";
1514
import { sleep } from "../index.ts";
1615
import WalletSolana from "../provider/wallet/walletSolana.ts";
1716
import { PublicKey } from "@solana/web3.js";
17+
import { mintNFTTemplate } from "../templates.ts";
18+
import { MintNFTContent, MintNFTSchema } from "../types.ts";
1819

19-
const mintTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined.
20-
21-
Example response:
22-
\`\`\`json
23-
{
24-
"collectionAddress": "D8j4ubQ3MKwmAqiJw83qT7KQNKjhsuoC7zJJdJa5BkvS",
25-
}
26-
\`\`\`
27-
28-
{{recentMessages}}
29-
30-
Given the recent messages, extract the following information about the requested mint nft:
31-
- collection contract address
32-
33-
Respond with a JSON markdown block containing only the extracted values.
34-
35-
Note: Make sure to extract the collection address from the most recent messages whenever possible.`;
36-
37-
export interface MintContent extends Content {
38-
collectionAddress: string;
39-
}
40-
41-
function isMintNFTContent(
42-
runtime: IAgentRuntime,
43-
content: any
44-
): content is MintContent {
45-
console.log("Content for mint", content);
20+
function isMintNFTContent(content: any): content is MintNFTContent {
4621
return typeof content.collectionAddress === "string";
4722
}
4823

@@ -60,18 +35,26 @@ const mintNFTAction: Action = {
6035
],
6136
description: "Mint NFTs for the collection",
6237
validate: async (runtime: IAgentRuntime, _message: Memory) => {
63-
const AwsAccessKeyIdOk = !!runtime.getSetting("AWS_ACCESS_KEY_ID");
64-
const AwsSecretAccessKeyOk = !!runtime.getSetting(
38+
const awsAccessKeyIdOk = !!runtime.getSetting("AWS_ACCESS_KEY_ID");
39+
const awsSecretAccessKeyOk = !!runtime.getSetting(
6540
"AWS_SECRET_ACCESS_KEY"
6641
);
67-
const AwsRegionOk = !!runtime.getSetting("AWS_REGION");
68-
const AwsS3BucketOk = !!runtime.getSetting("AWS_S3_BUCKET");
42+
const awsRegionOk = !!runtime.getSetting("AWS_REGION");
43+
const awsS3BucketOk = !!runtime.getSetting("AWS_S3_BUCKET");
44+
const solanaAdminPrivateKeyOk = !!runtime.getSetting(
45+
"SOLANA_ADMIN_PRIVATE_KEY"
46+
);
47+
const solanaAdminPublicKeyOk = !!runtime.getSetting(
48+
"SOLANA_ADMIN_PUBLIC_KEY"
49+
);
6950

7051
return (
71-
AwsAccessKeyIdOk ||
72-
AwsSecretAccessKeyOk ||
73-
AwsRegionOk ||
74-
AwsS3BucketOk
52+
awsAccessKeyIdOk ||
53+
awsSecretAccessKeyOk ||
54+
awsRegionOk ||
55+
awsS3BucketOk ||
56+
solanaAdminPrivateKeyOk ||
57+
solanaAdminPublicKeyOk
7558
);
7659
},
7760
handler: async (
@@ -92,18 +75,20 @@ const mintNFTAction: Action = {
9275
// Compose transfer context
9376
const transferContext = composeContext({
9477
state,
95-
template: mintTemplate,
78+
template: mintNFTTemplate,
9679
});
9780

98-
const content = await generateObjectDeprecated({
81+
const res = await generateObject({
9982
runtime,
10083
context: transferContext,
10184
modelClass: ModelClass.LARGE,
85+
schema: MintNFTSchema,
10286
});
87+
const content = res.object;
10388

104-
elizaLogger.log("generateObjectDeprecated:", transferContext);
89+
elizaLogger.log("Generate Object:", content);
10590

106-
if (!isMintNFTContent(runtime, content)) {
91+
if (!isMintNFTContent(content)) {
10792
elizaLogger.error("Invalid content for MINT_NFT action.");
10893
if (callback) {
10994
callback({
@@ -137,7 +122,6 @@ const mintNFTAction: Action = {
137122
return false;
138123
}
139124
if (metadata) {
140-
elizaLogger.log("nft params", {});
141125
const nftRes = await createNFT({
142126
runtime,
143127
collectionName: metadata.name,
@@ -176,10 +160,9 @@ const mintNFTAction: Action = {
176160
}
177161
return [];
178162
} catch (e: any) {
179-
console.log(e);
163+
elizaLogger.log(e);
164+
throw e;
180165
}
181-
182-
// callback();
183166
},
184167
examples: [
185168
[
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
export const mintNFTTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined.
3+
4+
Example response:
5+
\`\`\`json
6+
{
7+
"collectionAddress": "D8j4ubQ3MKwmAqiJw83qT7KQNKjhsuoC7zJJdJa5BkvS",
8+
}
9+
\`\`\`
10+
11+
{{recentMessages}}
12+
13+
Given the recent messages, extract the following information about the requested mint nft:
14+
- collection contract address
15+
16+
Respond with a JSON markdown block containing only the extracted values.
17+
18+
Note: Make sure to extract the collection address from the most recent messages whenever possible.`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { z } from "zod";
2+
import { Content } from "@ai16z/eliza";
3+
4+
5+
export interface MintNFTContent extends Content {
6+
collectionAddress: string;
7+
}
8+
9+
export const MintNFTSchema = z.object({
10+
collectionAddress: z.string(),
11+
});

0 commit comments

Comments
 (0)