Skip to content

Commit 94cb7c3

Browse files
committed
update cashtags
1 parent 302e356 commit 94cb7c3

File tree

2 files changed

+235
-159
lines changed

2 files changed

+235
-159
lines changed

core/src/actions/cashtags.ts

+82-44
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import {
77
Memory,
88
State,
99
UUID,
10+
ModelClass,
1011
} from "../core/types.ts";
11-
import { embeddingZeroVector } from "../core/memory.ts";
12-
import { log_to_file } from "../core/logger.ts";
12+
13+
import { composeContext } from "../core/context.ts";
14+
import { generateText } from "../core/generation.ts";
1315

1416
const API_URL = "https://api.dexscreener.com";
1517

@@ -52,33 +54,41 @@ interface DexScreenerResponse {
5254
schemaVersion: string;
5355
pairs: TokenPair[];
5456
}
57+
export const cashtagHandlerTemplate = `
58+
59+
{{recentMessages}}
60+
61+
{{attachments}}
62+
63+
{{messageDirections}}
64+
65+
# Instructions: Respond with a short message max 50 words in responses the users last message - respond in PLAIN TEXT (do not use Markdown) with the asked for token information always include the Dexscreener URL and Current Market Cap unless asked otherwise`;
5566

5667
/**
5768
* Cleans a string by removing dollar signs, spaces, and converting to lowercase
58-
*
69+
*
5970
* @param {string} input - The string to clean
6071
* @returns {string} The cleaned string
6172
* @throws {Error} If input is not a string
62-
*
73+
*
6374
* @example
6475
* cleanString("$Hello World$") // returns "helloworld"
6576
* cleanString("$100.00 USD") // returns "100.00usd"
6677
* cleanString(" MIXED case $STRING$ ") // returns "mixedcasestring"
6778
*/
6879
function cleanString(input) {
6980
// Input validation
70-
if (typeof input !== 'string') {
71-
throw new Error('Input must be a string');
81+
if (typeof input !== "string") {
82+
throw new Error("Input must be a string");
7283
}
7384

7485
// Remove dollar signs, remove spaces, and convert to lowercase
7586
return input
76-
.replace(/\$/g, '') // Remove all dollar signs
77-
.replace(/\s+/g, '') // Remove all whitespace (spaces, tabs, newlines)
78-
.toLowerCase(); // Convert to lowercase
87+
.replace(/\$/g, "") // Remove all dollar signs
88+
.replace(/\s+/g, "") // Remove all whitespace (spaces, tabs, newlines)
89+
.toLowerCase(); // Convert to lowercase
7990
}
8091

81-
8292
function calculatePairScore(pair: TokenPair): number {
8393
let score = 0;
8494

@@ -153,7 +163,7 @@ export const cashtags: Action = {
153163
description:
154164
"Searches for the best matching token pair (ca) or $cashtag ($SOL) based on age, liquidity, volume, and transaction count",
155165
validate: async (runtime: IAgentRuntime, message: Memory, state: State) => {
156-
return true
166+
return true;
157167
},
158168
handler: async (
159169
runtime: IAgentRuntime,
@@ -165,16 +175,16 @@ export const cashtags: Action = {
165175
const userId = runtime.agentId;
166176
const { roomId } = message;
167177

168-
169178
// Extract cashtag from message
170179
const cashtag = message.content.text
171180
.match(/\$[A-Za-z]+/)?.[0]
172181
?.replace("$", "");
182+
let responseContent;
173183

174184
const callbackData: Content = {
175185
text: undefined,
176186
action: "FIND_BEST_MATCH_RESPONSE",
177-
source: "DexScreener",
187+
source: "DEXSCREENER",
178188
attachments: [],
179189
};
180190

@@ -184,12 +194,16 @@ export const cashtags: Action = {
184194
// action: "FIND_BEST_MATCH_RESPONSE",
185195
// source: "DexScreener",
186196
// });
187-
callbackData.text= "No cashtag found in the message. Please include a cashtag (e.g. $PNUT)";
188-
197+
callbackData.text =
198+
"No cashtag found in the message. Please include a cashtag (e.g. $PNUT)";
199+
189200
return;
190201
}
191202

192-
console.log(`[${roomId}] Processing FIND_BEST_MATCH request... $`, cashtag);
203+
console.log(
204+
`[${roomId}] Processing FIND_BEST_MATCH request... $`,
205+
cashtag
206+
);
193207

194208
try {
195209
const { data: bestMatch, error } = await searchCashTags(cashtag);
@@ -199,13 +213,12 @@ export const cashtags: Action = {
199213
// callback(callbackData);
200214
return;
201215
}
202-
console.log(bestMatch);
203216

204217
// Format response
205218
const responseText = `
206219
Best match for $${cashtag}:
207220
Token: ${bestMatch.baseToken.name} (${bestMatch.baseToken.symbol})
208-
MCAP: $${(bestMatch.marketCap).toFixed(2)}M
221+
MCAP: $${bestMatch.marketCap.toFixed(2)}M
209222
Age: ${Math.floor((Date.now() - bestMatch.pairCreatedAt) / (1000 * 60 * 60 * 24))} days
210223
Liquidity: $${bestMatch.liquidity.usd.toLocaleString()}
211224
24h Volume: $${bestMatch.volume.h24.toLocaleString()}
@@ -216,10 +229,7 @@ export const cashtags: Action = {
216229
217230
URL: ${bestMatch.url}`;
218231

219-
callbackData.text = `Create a reply with the token information for $${cashtag}.
220-
Message from user - ${message.content.text}
221-
Token information - ${responseText}
222-
`;
232+
callbackData.text = responseText;
223233

224234
// Store the full response as an attachment
225235
const attachmentId =
@@ -230,12 +240,10 @@ export const cashtags: Action = {
230240
id: attachmentId,
231241
url: bestMatch.url,
232242
title: `Best Match for $${cashtag}`,
233-
source: "DexScreener",
243+
source: "DEXSCREENER",
234244
description: `Token analysis for ${bestMatch.baseToken.symbol}`,
235-
text: JSON.stringify(bestMatch, null, 2)
236-
})
237-
238-
// callback(callbackData);
245+
text: JSON.stringify(bestMatch, null, 2),
246+
});
239247

240248
// Log to database
241249
runtime.databaseAdapter.log({
@@ -244,30 +252,60 @@ export const cashtags: Action = {
244252
roomId,
245253
type: "dexscreener",
246254
});
247-
248-
// Create memory
249-
const memory = {
250-
userId,
251-
agentId: runtime.agentId,
252-
content: callbackData,
253-
roomId,
254-
embedding: embeddingZeroVector,
255-
};
256-
257-
258-
await runtime.messageManager.createMemory(memory);
259-
const response = await runtime.evaluate(message, state);
260-
261-
262255
} catch (error) {
263256
console.error("Error in findBestMatch:", error);
264257
callbackData.text = `Error processing request: ${error.message}`;
265258
callback(callbackData);
259+
return;
260+
}
261+
262+
const memory: Memory = {
263+
agentId: runtime.agentId,
264+
userId,
265+
roomId,
266+
content: callbackData,
267+
createdAt: Date.now(),
268+
};
269+
270+
// Update state with the new memory
271+
state = await runtime.composeState(memory);
272+
273+
const context = composeContext({
274+
state,
275+
template: cashtagHandlerTemplate,
276+
});
277+
278+
responseContent = await generateText({
279+
runtime,
280+
context,
281+
modelClass: ModelClass.SMALL,
282+
});
283+
284+
if (!responseContent) {
285+
return;
266286
}
287+
const agentMessage = {
288+
userId,
289+
roomId,
290+
agentId: runtime.agentId,
291+
};
267292

268-
console.log(callbackData);
269-
typeof callback === "function" && callback(callbackData);
293+
const content = {
294+
text: responseContent,
295+
action: "FIND_BEST_MATCH_RESPONSE",
296+
source: "DEXSCREENER",
297+
};
298+
299+
// save response to memory
300+
const responseMessage = {
301+
...agentMessage,
302+
userId: runtime.agentId,
303+
content: content,
304+
};
270305

306+
await runtime.messageManager.createMemory(responseMessage);
307+
callbackData.content = responseContent;
308+
callback(content);
271309
return callbackData;
272310
},
273311
examples: [

0 commit comments

Comments
 (0)