-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathagent.js
239 lines (191 loc) · 6.15 KB
/
agent.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
const path = require('path');
const fs = require('fs');
const utils = require('./utils');
const admin = require('./admin');
const lnbits = require('./lnbits');
const realConfig = require('./config');
function getAgentFromKey(key) {
const xid = utils.getAgentId(key);
const agentData = getAgent(xid);
if (agentData?.merged) {
console.log(`getAgentFromKey relaying ${xid} to ${agentData.merged}`)
return getAgent(agentData.merged);
}
return agentData;
}
async function createAgent(key, config = realConfig) {
const userId = utils.getAgentId(key, config);
agentData = {
xid: userId,
pubkey: key,
name: config.newUser,
tagline: '',
description: '',
credits: config.initialCredits,
depositToCredits: true,
created: new Date().toISOString(),
updated: new Date().toISOString(),
};
saveAgent(agentData, config);
return agentData;
}
function getAgent(xid, config = realConfig) {
const agentJsonPath = path.join(config.agents, xid, 'agent.json');
if (!fs.existsSync(agentJsonPath)) {
return null;
}
const agentJsonContent = fs.readFileSync(agentJsonPath, 'utf-8');
const agentData = JSON.parse(agentJsonContent);
return agentData;
}
function saveAgent(agentData, config = realConfig) {
const agentFolder = path.join(config.agents, agentData.xid);
const agentJsonPath = path.join(agentFolder, 'agent.json');
if (!fs.existsSync(agentFolder)) {
fs.mkdirSync(agentFolder);
}
agentData.updated = new Date().toISOString();
agentData.credits = agentData.credits || 0;
fs.writeFileSync(agentJsonPath, JSON.stringify(agentData, null, 2));
}
function getAssets(xid, config = realConfig) {
const agentFolder = path.join(config.agents, xid);
const jsonPath = path.join(agentFolder, 'assets.json');
let assets = {};
if (fs.existsSync(jsonPath)) {
const jsonContent = fs.readFileSync(jsonPath, 'utf-8');
assets = JSON.parse(jsonContent);
}
else {
assets.owner = xid;
assets.created = [];
assets.collected = [];
assets.collections = [];
}
return assets;
}
function saveAssets(assets, config = realConfig) {
const agentFolder = path.join(config.agents, assets.owner);
const jsonPath = path.join(agentFolder, 'assets.json');
if (!fs.existsSync(agentFolder)) {
fs.mkdirSync(agentFolder);
}
assets.updated = new Date().toISOString();
fs.writeFileSync(jsonPath, JSON.stringify(assets, null, 2));
}
function addAsset(metadata, config = realConfig) {
let assets = getAssets(metadata.asset.owner, config);
if (metadata.file) {
if (!assets.created.includes(metadata.xid)) {
assets.created.push(metadata.xid);
}
}
else if (metadata.collection) {
if (!assets.collections.includes(metadata.xid)) {
assets.collections.push(metadata.xid);
}
}
else if (metadata.nft) {
if (!assets.collected.includes(metadata.xid)) {
assets.collected.push(metadata.xid);
}
}
else {
//console.log(`addAsset: unknown asset type ${metadata.xid}`);
return;
}
saveAssets(assets, config);
}
function removeAsset(metadata, config = realConfig) {
let assets = getAssets(metadata.asset.owner, config);
if (metadata.file) {
assets.created = assets.created.filter(xid => xid != metadata.xid);
}
else if (metadata.collection) {
assets.collections = assets.collections.filter(xid => xid != metadata.xid);
}
else if (metadata.nft) {
assets.collected = assets.collected.filter(xid => xid != metadata.xid);
}
else {
//console.debug(`removeAsset: unknown asset type ${metadata.xid}`);
return;
}
saveAssets(assets, config);
}
function getTxnLog(xid, config = realConfig) {
try {
const jsonlPath = path.join(config.agents, xid, 'txnlog.jsonl');
const data = fs.readFileSync(jsonlPath, 'utf-8');
const lines = data.trim().split('\n');
const log = lines.map(line => JSON.parse(line));
return log.reverse();
} catch (error) {
return [];
}
}
function saveTxnLog(xid, record, config = realConfig) {
record.time = new Date().toISOString();
const recordString = JSON.stringify(record);
const agentFolder = path.join(config.agents, xid);
const jsonlPath = path.join(agentFolder, 'txnlog.jsonl');
if (!fs.existsSync(agentFolder)) {
fs.mkdirSync(agentFolder);
}
fs.appendFileSync(jsonlPath, recordString + '\n');
}
function addCredits(userId, amount, config = realConfig) {
const agentData = getAgent(userId, config);
if (agentData) {
agentData.credits += amount;
const record = {
type: "add-credits",
agent: userId,
agentName: agentData.name,
amount: amount,
};
admin.saveAuditLog(record, config);
saveAgent(agentData, config);
return agentData;
}
}
async function buyCredits(userId, invoice, config = realConfig) {
const agentData = getAgent(userId, config);
if (agentData && invoice?.payment_hash) {
const payment = await lnbits.checkPayment(invoice.payment_hash);
if (payment?.paid) {
const amount = Math.round(payment.details.amount / 1000);
agentData.credits += amount;
invoice.payment = payment;
const record = {
type: "buy-credits",
agent: agentData.xid,
agentName: agentData.name,
amount: amount,
invoice: invoice,
};
admin.saveAuditLog(record, config);
const txn = {
'type': 'credits',
'credits': amount,
};
saveTxnLog(userId, txn, config);
saveAgent(agentData, config);
return agentData;
}
}
}
module.exports = {
addAsset,
addCredits,
buyCredits,
createAgent,
getAgent,
getAgentFromKey,
getAssets,
getTxnLog,
removeAsset,
saveAgent,
saveAssets,
saveTxnLog,
};