Skip to content

Commit a51bbd5

Browse files
committed
iterate
1 parent 27aeba9 commit a51bbd5

File tree

4 files changed

+69
-11
lines changed

4 files changed

+69
-11
lines changed

agent.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ const farcaster = new Farcaster(agentFID, token);
1010

1111
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
1212

13-
import { findUsers, indexUser, createMatch, updateMatchStatus, findUserByFid, findPendingMatch, findExpiredMatches } from './db.js';
13+
import { findUsers, indexUser, createMatch, updateMatchStatus, findUserByFid, findPendingMatch, findExpiredMatches, getUserState } from './db.js';
1414
import { evaluatePossibleMatches, prepareDeclineMessage, prepareIntroMessage, prepareMatchSuccessMessage, prepareAlreadyConnectedMessage } from './find_matches.js';
1515

1616
export const runAgent = async (messages) => {
1717

18-
messages = messages.slice(-100);
19-
18+
//messages = messages.slice(-100);
2019

2120
// Response schema focused on actions
2221
const responseSchema = z.object({
@@ -53,8 +52,8 @@ export const runAgent = async (messages) => {
5352
let agentPromptText = agentPrompt.promptMessages[0].prompt.template;
5453

5554
const aiResponse = await openai.chat.completions.create({
56-
model: "gpt-4o",
57-
//reasoning_effort: "medium",
55+
model: "o3-mini",
56+
reasoning_effort: "medium",
5857
messages: [{ role: "system", content: agentPromptText }, ...messages],
5958
response_format: zodResponseFormat(responseSchema, "response"),
6059
stream: false,
@@ -329,6 +328,16 @@ export const handleExpiredMatches = async () => {
329328
}
330329

331330
export const agentLogic = async ({messages, conversationId, senderFid, name, username}) => {
331+
332+
const matches = await getUserState(senderFid);
333+
334+
messages.unshift({
335+
role: `system`,
336+
content: `Here are the matching states for the user. : ${matches.map(match =>
337+
Object.entries(match).map(([key, value]) => `${key}: ${value}`).join('\n')
338+
).join('\n----\n')}`
339+
});
340+
332341
const response = await runAgent(messages);
333342

334343
console.log(response, 'response');

cast.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"\n- Key skills and expertise" +

db.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,55 @@ export async function findExpiredMatches() {
288288
throw error;
289289
}
290290
}
291+
292+
export async function getUserState(fid) {
293+
try {
294+
// First get the user's basic information
295+
const user = await db('users')
296+
.where({ fid })
297+
.first();
298+
299+
if (!user) {
300+
throw new Error(`User with FID ${fid} not found`);
301+
}
302+
303+
// Get all matches for the user with related usernames and specific fields
304+
const matches = await db.raw(
305+
`SELECT
306+
m.created_at,
307+
CASE
308+
WHEN m.user1_fid = ? THEN m.user1_status
309+
ELSE m.user2_status
310+
END as chatting_user_status,
311+
CASE
312+
WHEN m.user1_fid = ? THEN m.user2_status
313+
ELSE m.user1_status
314+
END as their_status,
315+
CASE
316+
WHEN m.user1_fid = ? THEN u2.username
317+
ELSE u1.username
318+
END as their_username,
319+
CASE
320+
WHEN m.user1_fid = ? THEN u2.profile
321+
ELSE u1.profile
322+
END as their_profile,
323+
CASE
324+
WHEN m.user1_fid = ? THEN u2.intent
325+
ELSE u1.intent
326+
END as their_intent
327+
FROM matches m
328+
JOIN users u1 ON m.user1_fid = u1.fid
329+
JOIN users u2 ON m.user2_fid = u2.fid
330+
WHERE m.user1_fid = ? OR m.user2_fid = ?
331+
ORDER BY m.created_at ASC`,
332+
[fid, fid, fid, fid, fid, fid, fid]
333+
);
334+
335+
return matches.rows;
336+
337+
338+
} catch (error) {
339+
console.error('Error getting user state:', error);
340+
throw error;
341+
}
342+
}

farcaster.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ export const formatMessage = (message) => {
99
if (message.serverTimestamp < 1740239068884) {
1010
return false;
1111
}
12-
//console.log(message);
13-
const name = message.senderContext?.displayName ||
14-
message.senderContext?.username ||
15-
'Unknown';
1612
return {
17-
role: `user`,
18-
content: `${name}: ${message.message}`
13+
role: message.senderFid === parseInt(process.env.AGENT_FID) ? `assistant` : `user`,
14+
content: `${message.senderContext?.username}: ${message.message}`
1915
};
2016
};
2117

0 commit comments

Comments
 (0)