Skip to content

Commit e9f004b

Browse files
authored
Merge pull request #12 from BitPodAI/dev
Dev merge to Main
2 parents d834f0b + 211a434 commit e9f004b

7 files changed

Lines changed: 404 additions & 85 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ coverage
6767
.eslintcache
6868

6969
agent/content
70+
agent/xuserlabels.json
71+
packages/plugin-data-enrich/data
7072

7173
eliza.manifest
7274
eliza.manifest.sgx

agent/xuserlabels.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"kolname": "abc",
4+
"kollabel": "abc CEO"
5+
},
6+
{
7+
"kolname": "abc2",
8+
"kollabel": "abc2 CEO"
9+
}
10+
]

packages/client-direct/src/routes.ts

Lines changed: 141 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ export class Routes {
219219
"/:agentId/twitter_profile_search",
220220
this.handleTwitterProfileSearch.bind(this)
221221
);
222+
app.post(
223+
"/:agentId/twitter_labels",
224+
this.handleTwitterLabels.bind(this)
225+
);
226+
app.post(
227+
"/:agentId/twitter_profile_kols",
228+
this.handleTwitterProfileKols.bind(this)
229+
);
222230
app.post("/:agentId/re_twitter", this.handleReTwitter.bind(this));
223231
app.post(
224232
"/:agentId/translate_text",
@@ -283,6 +291,14 @@ export class Routes {
283291
);
284292

285293
const userManager = new UserManager(runtime.cacheManager);
294+
295+
const cacheProfile = await userManager.verifyExistingUser(userId);
296+
if(cacheProfile) {
297+
return {
298+
profile: cacheProfile,
299+
};
300+
}
301+
286302
const userProfile = userManager.createDefaultProfile(userId, gmail);
287303
await userManager.saveUserData(userProfile);
288304

@@ -324,6 +340,12 @@ export class Routes {
324340
);
325341

326342
const userManager = new UserManager(runtime.cacheManager);
343+
const cacheProfile = await userManager.verifyExistingUser(userId);
344+
if(cacheProfile) {
345+
return {
346+
profile: cacheProfile,
347+
};
348+
}
327349
const userProfile = userManager.createDefaultProfile(userId, email);
328350
await userManager.saveUserData(userProfile);
329351

@@ -471,39 +493,12 @@ export class Routes {
471493
<head>
472494
<meta charset="UTF-8">
473495
<meta name="viewport" content="width=device-width, initial-scale=1.0">
474-
<title>FungIPle</title>
475-
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTczIiBoZWlnaHQ9IjE2MyIgdmlld0JveD0iMCAwIDE3MyAxNjMiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxwYXRoIGQ9Ik0wIDEwNi44OUgxOS4yMDM1TDE5LjIzMzYgNzcuNDYwOUgwLjAzMDA3NkwwIDEwNi44OVoiIGZpbGw9IiNGRjk5MDAiLz4KPHBhdGggZD0iTTE3Mi45NTIgMjkuNDI5NUwxNzIuOTY3IDE5LjcxNDlDMTcyLjk2NyA5LjUxOTA5IDE2My4yNjggMCAxNTIuODYyIDBIODMuNzkyMkg4MS43OTIxSDc0LjQzODZINzMuOTQyM1YwLjAxNTAzODFDNjAuMDQ3MiAwLjI0MDYwOSA1MC4wOTIxIDEwLjEzNTcgNTAuMDc3IDIzLjg1MDRMNTAuMDE2OSA3Ny40NjExSDI5LjU2NTJMMjkuNTM1MiAxMDYuODkxSDQ5Ljk4NjhMNDkuOTI2NyAxNjIuNjIySDgzLjY4NjlMODMuNzQ3MSAxMDYuODkxSDgzLjc3NzJIMTQ4LjUzMUMxNjIuNjgxIDEwNi44OTEgMTcyLjg3NyA5Ni45MDUzIDE3Mi44OTIgODMuMDQwMkwxNzIuOTM3IDQxLjQ2NzJIMTM5LjE3N0wxMzkuMTMyIDc3LjQ2MTFIODMuNzkyMlYyOS40Mjk1SDEzOS4xOTJIMTcyLjk1MloiIGZpbGw9IiNGRjk5MDAiLz4KPC9zdmc+Cg==">
476-
<style>
477-
body {
478-
margin: 0;
479-
}
480-
.container {
481-
display: flex;
482-
justify-content: center;
483-
align-items: center;
484-
width: 100vw;
485-
}
486-
.ad_img {
487-
max-width: 1000px;
488-
width: 100%;
489-
height: auto;
490-
}
491-
@media only screen and (max-width: 670px) {
492-
.ad_img {
493-
max-width: 660px;
494-
width: 100%;
495-
height: auto;
496-
}
497-
}
498-
</style>
496+
<title>Auth</title>
499497
</head>
500498
<body>
501499
<div style="text-align: center; font-size: 20px; font-weight: bold;">
502-
<h1>FungIPle Agent</h1>
503-
<br>Login Success!<br>
500+
<br>Auth Success! Redirecting...<br>
504501
<script type="text/javascript">
505-
console.log('window.opener');
506-
console.log(window.opener);
507502
function closeWindow() {
508503
console.log('closeWindow');
509504
try {
@@ -523,20 +518,13 @@ export class Routes {
523518
console.log(e);
524519
}
525520
}
521+
closeWindow();
526522
</script>
527523
<button style="text-align: center; width: 40%; height: 40px; font-size: 20px; background-color: #9F91ED; color: #ffffff; margin: 20px; border: none; border-radius: 10px;"
528524
onclick="closeWindow()">
529525
Click to Close</button>
530526
<br>
531527
</div>
532-
<div class="container">
533-
<img style="max-width: 40%; width: 40%; height: auto;" src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTczIiBoZWlnaHQ9IjE2MyIgdmlld0JveD0iMCAwIDE3MyAxNjMiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxwYXRoIGQ9Ik0wIDEwNi44OUgxOS4yMDM1TDE5LjIzMzYgNzcuNDYwOUgwLjAzMDA3NkwwIDEwNi44OVoiIGZpbGw9IiNGRjk5MDAiLz4KPHBhdGggZD0iTTE3Mi45NTIgMjkuNDI5NUwxNzIuOTY3IDE5LjcxNDlDMTcyLjk2NyA5LjUxOTA5IDE2My4yNjggMCAxNTIuODYyIDBIODMuNzkyMkg4MS43OTIxSDc0LjQzODZINzMuOTQyM1YwLjAxNTAzODFDNjAuMDQ3MiAwLjI0MDYwOSA1MC4wOTIxIDEwLjEzNTcgNTAuMDc3IDIzLjg1MDRMNTAuMDE2OSA3Ny40NjExSDI5LjU2NTJMMjkuNTM1MiAxMDYuODkxSDQ5Ljk4NjhMNDkuOTI2NyAxNjIuNjIySDgzLjY4NjlMODMuNzQ3MSAxMDYuODkxSDgzLjc3NzJIMTQ4LjUzMUMxNjIuNjgxIDEwNi44OTEgMTcyLjg3NyA5Ni45MDUzIDE3Mi44OTIgODMuMDQwMkwxNzIuOTM3IDQxLjQ2NzJIMTM5LjE3N0wxMzkuMTMyIDc3LjQ2MTFIODMuNzkyMlYyOS40Mjk1SDEzOS4xOTJIMTcyLjk1MloiIGZpbGw9IiNGRjk5MDAiLz4KPC9zdmc+Cg==">
534-
</div>
535-
536-
<div>
537-
<br>
538-
</div>
539-
540528
</body>
541529
</html>`);
542530
} catch (error) {
@@ -635,7 +623,7 @@ export class Routes {
635623
});
636624
}
637625
}
638-
626+
639627
async handleArenaQuery(req: express.Request, res: express.Response) {
640628
const kol = typeof req.query.username === 'string' ? req.query.username : '';
641629
const kolname = kol.trim();
@@ -770,6 +758,82 @@ export class Routes {
770758
});
771759
}
772760

761+
async handleTwitterLabels(
762+
req: express.Request,
763+
res: express.Response
764+
) {
765+
return this.authUtils.withErrorHandling(req, res, async () => {
766+
console.log("handleTwitterLabels");
767+
if (!Array.isArray(req.body.xnamelist)) {
768+
return res.status(400).json({ error: 'param not a array' });
769+
}
770+
const receivedStrings = req.body.xnamelist;
771+
772+
// receivedStrings.forEach((str, index) => {
773+
// console.log(`getlabels string: ${index + 1}: ${str}`);
774+
// });
775+
776+
// const runtime = await this.authUtils.getRuntime(req.params.agentId);
777+
778+
try {
779+
let profilesOutput = [];
780+
const promise = new Promise((resolve, reject) => {
781+
// Listen
782+
twEventCenter.on('MSG_TWITTER_LABELS_RESP', (data) => {
783+
//console.log('Received Resp message:', data);
784+
resolve(data);
785+
});
786+
787+
twEventCenter.emit('MSG_TWITTER_LABELS', { xuserlist: receivedStrings });
788+
});
789+
790+
// wait for result
791+
profilesOutput = await promise;
792+
return profilesOutput;
793+
} catch (error) {
794+
console.error("handleTwitterLabels error:", error);
795+
return [];
796+
}
797+
});
798+
}
799+
800+
async handleTwitterProfileKols(
801+
req: express.Request,
802+
res: express.Response
803+
) {
804+
return this.authUtils.withErrorHandling(req, res, async () => {
805+
// const { username, count, userId } = req.body;
806+
const {userId } = req.body;
807+
//const fetchCount = Math.min(20, count);
808+
const runtime = await this.authUtils.getRuntime(req.params.agentId);
809+
console.log("kols handleTwitterProfileKols" + userId);
810+
if (!userId) {
811+
console.error("userId is empty.");
812+
return [];
813+
}
814+
815+
try {
816+
let profilesOutput = [];
817+
const promise = new Promise((resolve, reject) => {
818+
twEventCenter.on('MSG_KOLS_TWITTER_PROFILE_RESP', (data) => {
819+
resolve(data);
820+
});
821+
822+
// set request
823+
twEventCenter.emit('MSG_KOLS_TWITTER_PROFILE', { });
824+
//console.log("Send search request");
825+
});
826+
827+
// wait for result
828+
profilesOutput = await promise;
829+
return profilesOutput;
830+
} catch (error) {
831+
console.error("Profile search error:", error);
832+
return [];
833+
}
834+
});
835+
}
836+
773837
async handleReTwitter(req: express.Request, res: express.Response) {
774838
try {
775839
console.log("handleReTwitter");
@@ -791,22 +855,29 @@ export class Routes {
791855

792856
async handleTranslateText(req: express.Request, res: express.Response) {
793857
try {
794-
console.log("handleTranslateText 1");
795-
const { text } = req.body;
796-
console.log("handleTranslateText 2" + text);
858+
const {languagecode, text } = req.body;
859+
console.log("handleTranslateText, code: " + languagecode);
860+
console.log("handleTranslateText, text: " + text);
861+
if(!languagecode || languagecode === "en" || languagecode.includes("en-")) {
862+
return res.json({
863+
success: true,
864+
data: {"result": text},
865+
});
866+
}
867+
797868

798869
const runtime = await this.authUtils.getRuntime(req.params.agentId);
799870
const prompt =
800-
'You are a helpful translator. If the following text is in English, please translate it into Chinese. If it is in another language, translate it into English; The returned result only includes the translated result,The JSON structure of the returned result is: {"result":""}. The text that needs to be translated starts with [Text]. [TEXT]: ' +
871+
'You are a helpful translator. Please translate the following text into the language corresponding to this code: ' + languagecode + ', The returned result only includes the translated result,The JSON structure of the returned result is: {"result":""}, No need to use markdown syntax to modify JSON, just include JSON. The text that needs to be translated starts with [Text]. [TEXT]: ' +
801872
text;
802-
//console.log("handleTranslateText 3" + prompt);
873+
//console.log("handleTranslateText " + prompt);
803874

804875
const response = await generateText({
805876
runtime: runtime,
806877
context: prompt,
807878
modelClass: ModelClass.SMALL,
808879
});
809-
//console.log("handleTranslateText 4" + response);
880+
//console.log("handleTranslateText " + response);
810881

811882
if (!response) {
812883
throw new Error("No response from generateText");
@@ -1061,7 +1132,11 @@ export class Routes {
10611132
userManager.saveUserData(profile);
10621133

10631134
try {
1064-
const { cursor, watchlist } = req.body;
1135+
//const { cursor, watchlist } = req.body;
1136+
const { userId } = req.body;
1137+
const cursor = "";
1138+
const watchItemList = await userManager.getWatchList(userId);
1139+
const watchlist: string[] = watchItemList.map(user => user.username);
10651140
let report;
10661141
if (watchlist && watchlist.length > 0) {
10671142
report =
@@ -1077,6 +1152,14 @@ export class Routes {
10771152
cursor
10781153
);
10791154
}
1155+
else if (report && report.items?.length < 10) {
1156+
let newReport =
1157+
await InferMessageProvider.getAllWatchItemsPaginated(
1158+
runtime.cacheManager,
1159+
cursor
1160+
);
1161+
report.push(...newReport);
1162+
}
10801163
} else {
10811164
report =
10821165
await InferMessageProvider.getAllWatchItemsPaginated(
@@ -1148,7 +1231,7 @@ export class Routes {
11481231
const tokenAmount = 1; // tokenAmount Backend control
11491232
switch (typestr) {
11501233
case "sol-spl":
1151-
// Handle sol-spl transfer
1234+
// Handle sol-spl transfer
11521235
try {
11531236
const signature = await createSolSplTransferTransaction({
11541237
//fromTokenAccountPubkey: settings.SOL_SPL_FROM_PUBKEY,
@@ -1161,18 +1244,6 @@ export class Routes {
11611244
signature,
11621245
data: "Sol-SPL reward processed",
11631246
});
1164-
1165-
// Confirm the transction
1166-
/*const connection = new Connection(
1167-
clusterApiUrl("mainnet-beta"),
1168-
"confirmed"
1169-
);
1170-
const signature = await sendAndConfirmTransaction(
1171-
connection,
1172-
transaction,
1173-
[settings.SOL_SPL_OWNER_PUBKEY]
1174-
);
1175-
return { signature };*/
11761247
} catch (error) {
11771248
if (error instanceof SplInvalidPublicKeyError) {
11781249
throw new ApiError(400, error.message);
@@ -1185,7 +1256,7 @@ export class Routes {
11851256
}
11861257
break;
11871258
case "sol":
1188-
// Handle sol transfer
1259+
// Handle sol transfer
11891260
try {
11901261
const transaction = await createSolTransferTransaction({
11911262
fromPubkey: settings.SOL_FROM_PUBKEY,
@@ -1203,7 +1274,11 @@ export class Routes {
12031274
transaction,
12041275
[settings.SOL_OWNER_PUBKEY]
12051276
);
1206-
return { signature };
1277+
return res.json({
1278+
success: true,
1279+
signature,
1280+
data: "Sol reward processed",
1281+
});
12071282
} catch (error) {
12081283
if (error instanceof InvalidPublicKeyError) {
12091284
throw new ApiError(400, error.message);
@@ -1215,7 +1290,7 @@ export class Routes {
12151290
throw new ApiError(500, "Internal server error");
12161291
}
12171292
case "sol-agent-kit":
1218-
// Handle sol-spl agent-kit transfer
1293+
// Handle sol-spl agent-kit transfer
12191294
try {
12201295
//return res.json({
12211296
// success: true,
@@ -1226,7 +1301,11 @@ export class Routes {
12261301
mintPubkey: settings.SOL_SPL_OWNER_PUBKEY,
12271302
tokenAmount,
12281303
});
1229-
return { transaction };
1304+
return res.json({
1305+
success: true,
1306+
transaction,
1307+
data: "Sol-Agent-Kit reward processed",
1308+
});
12301309
} catch (error) {
12311310
if (error instanceof SplInvalidPublicKeyError) {
12321311
throw new ApiError(400, error.message);

0 commit comments

Comments
 (0)