forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.ts
172 lines (159 loc) · 5.25 KB
/
upload.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import {
Action,
HandlerCallback,
IAgentRuntime,
Memory,
State,
ModelClass,
Content,
ActionExample,
generateObject,
} from "@elizaos/core";
import { Indexer, ZgFile, getFlowContract } from "@0glabs/0g-ts-sdk";
import { ethers } from "ethers";
import { composeContext } from "@elizaos/core";
import { promises as fs } from "fs";
import { uploadTemplate } from "../templates/upload";
export interface UploadContent extends Content {
filePath: string;
}
function isUploadContent(
_runtime: IAgentRuntime,
content: any
): content is UploadContent {
console.log("Content for upload", content);
return typeof content.filePath === "string";
}
export const zgUpload: Action = {
name: "ZG_UPLOAD",
similes: [
"UPLOAD_FILE_TO_ZG",
"STORE_FILE_ON_ZG",
"SAVE_FILE_TO_ZG",
"UPLOAD_TO_ZERO_GRAVITY",
"STORE_ON_ZERO_GRAVITY",
"SHARE_FILE_ON_ZG",
"PUBLISH_FILE_TO_ZG",
],
description: "Store data using 0G protocol",
validate: async (runtime: IAgentRuntime, message: Memory) => {
const zgIndexerRpc = !!runtime.getSetting("ZEROG_INDEXER_RPC");
const zgEvmRpc = !!runtime.getSetting("ZEROG_EVM_RPC");
const zgPrivateKey = !!runtime.getSetting("ZEROG_PRIVATE_KEY");
const flowAddr = !!runtime.getSetting("ZEROG_FLOW_ADDRESS");
return zgIndexerRpc && zgEvmRpc && zgPrivateKey && flowAddr;
},
handler: async (
runtime: IAgentRuntime,
message: Memory,
state: State,
_options: any,
callback: HandlerCallback
) => {
console.log("ZG_UPLOAD action called");
if (!state) {
state = (await runtime.composeState(message)) as State;
} else {
state = await runtime.updateRecentMessageState(state);
}
// Compose upload context
const uploadContext = composeContext({
state,
template: uploadTemplate,
});
// Generate upload content
const content = await generateObject({
runtime,
context: uploadContext,
modelClass: ModelClass.LARGE,
});
// Validate upload content
if (!isUploadContent(runtime, content)) {
console.error("Invalid content for UPLOAD action.");
if (callback) {
callback({
text: "Unable to process 0G upload request. Invalid content provided.",
content: { error: "Invalid upload content" },
});
}
return false;
}
try {
const zgIndexerRpc = runtime.getSetting("ZEROG_INDEXER_RPC");
const zgEvmRpc = runtime.getSetting("ZEROG_EVM_RPC");
const zgPrivateKey = runtime.getSetting("ZEROG_PRIVATE_KEY");
const flowAddr = runtime.getSetting("ZEROG_FLOW_ADDRESS");
const filePath = content.filePath;
if (!filePath) {
console.error("File path is required");
return false;
}
// Check if file exists and is accessible
try {
await fs.access(filePath);
} catch (error) {
console.error(
`File ${filePath} does not exist or is not accessible:`,
error
);
return false;
}
const file = await ZgFile.fromFilePath(filePath);
let [tree, err] = await file.merkleTree();
if (err === null) {
console.log("File Root Hash: ", tree.rootHash());
} else {
console.log("Error getting file root hash: ", err);
return false;
}
const provider = new ethers.JsonRpcProvider(zgEvmRpc);
const signer = new ethers.Wallet(zgPrivateKey, provider);
const indexer = new Indexer(zgIndexerRpc);
const flowContract = getFlowContract(flowAddr, signer);
let [tx, err] = await indexer.upload(
file,
0,
zgEvmRpc,
flowContract
);
if (err === null) {
console.log("File uploaded successfully, tx: ", tx);
} else {
console.error("Error uploading file: ", err);
return false;
}
await file.close();
} catch (error) {
console.error("Error getting settings for 0G upload:", error);
}
},
examples: [
[
{
user: "{{user1}}",
content: {
text: "upload my resume.pdf file",
action: "ZG_UPLOAD",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "can you help me upload this document.docx?",
action: "ZG_UPLOAD",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "I need to upload an image file image.png",
action: "ZG_UPLOAD",
},
},
],
] as ActionExample[][],
} as Action;