Skip to content

Commit fca1424

Browse files
committed
iterate
1 parent 740bfb6 commit fca1424

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

agent.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ async function handleAlreadyConnected({senderFid, messages, response}) {
242242

243243
await farcaster.sendDirectMessage(senderFid, response.message);
244244

245-
// Check if the other user had already accepted
245+
// Get both users' information
246246
const otherFid = parseInt(match.user1_fid) === senderFid
247247
? match.user2_fid
248248
: match.user1_fid;
@@ -251,14 +251,42 @@ async function handleAlreadyConnected({senderFid, messages, response}) {
251251
? match.user2_status
252252
: match.user1_status;
253253

254+
const currentUser = await findUserByFid(senderFid);
255+
const otherUser = await findUserByFid(otherFid);
256+
257+
// Find new matches for the current user
258+
await handleFindMatches({
259+
senderFid,
260+
name: currentUser.name,
261+
messages,
262+
response: {
263+
...response,
264+
message: "Let's find you another match!"
265+
}
266+
});
267+
268+
// If the other user had accepted, inform them and find them new matches too
254269
if (otherUserStatus === 'accepted') {
255-
const currentUser = await findUserByFid(senderFid);
256270
const alreadyConnectedMessage = await prepareAlreadyConnectedMessage(currentUser);
257271
await farcaster.sendDirectMessage(otherFid, alreadyConnectedMessage);
272+
273+
// Find new matches for the other user
274+
const otherUserMessages = await farcaster.getMessages(`${otherFid}-${agentFID}`);
275+
const lastResponse = await runAgent(otherUserMessages);
276+
277+
await handleFindMatches({
278+
senderFid: otherFid,
279+
name: otherUser.name,
280+
messages: otherUserMessages,
281+
response: {
282+
...lastResponse,
283+
message: "Let's find you another match!"
284+
}
285+
});
258286
}
259287
}
260288

261-
export const agentLogic = async ({messages, conversationId, senderFid, name}) => {
289+
export const agentLogic = async ({messages, conversationId, senderFid, name, username}) => {
262290
const response = await runAgent(messages);
263291

264292
console.log(response, 'response');
@@ -272,6 +300,7 @@ export const agentLogic = async ({messages, conversationId, senderFid, name}) =>
272300
await indexUser({
273301
fid: senderFid,
274302
name,
303+
username,
275304
profile: response.user_profile,
276305
intent: response.user_intent
277306
});

db.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const openai = new OpenAI({
1212
apiKey: process.env.OPENAI_API_KEY
1313
});
1414

15-
export async function indexUser({ fid, name, profile, intent }) {
15+
export async function indexUser({ fid, name, username, profile, intent }) {
1616
try {
1717

1818
const openrank = await getOpenRank(fid);
@@ -30,11 +30,12 @@ export async function indexUser({ fid, name, profile, intent }) {
3030

3131
// Upsert user data with both vector embeddings
3232
await db.raw(
33-
`INSERT INTO users (fid, name, openrank, profile, intent, profile_vector, intent_vector, created_at)
34-
VALUES (?, ?, ?, ?, ?, ?, ?, NOW())
33+
`INSERT INTO users (fid, name, username, openrank, profile, intent, profile_vector, intent_vector, created_at)
34+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW())
3535
ON CONFLICT (fid)
3636
DO UPDATE SET
3737
name = EXCLUDED.name,
38+
username = EXCLUDED.username,
3839
openrank = EXCLUDED.openrank,
3940
profile = EXCLUDED.profile,
4041
intent = EXCLUDED.intent,
@@ -44,6 +45,7 @@ export async function indexUser({ fid, name, profile, intent }) {
4445
[
4546
fid,
4647
name,
48+
username,
4749
openrank,
4850
profile,
4951
intent,
@@ -70,9 +72,7 @@ export async function findUsers({ fid, profile, intent }, limit = 50) {
7072
END AS fid
7173
FROM matches m
7274
WHERE (m.user1_fid = ? OR m.user2_fid = ?)
73-
AND (m.status = 'declined' OR
74-
m.user1_status = 'declined' OR
75-
m.user2_status = 'declined')
75+
AND (m.status = 'accepted')
7676
7777
UNION
7878
@@ -114,6 +114,7 @@ export async function findUsers({ fid, profile, intent }, limit = 50) {
114114
`SELECT
115115
fid,
116116
name,
117+
username,
117118
profile,
118119
intent,
119120
(1 - (profile_vector <=> ?)) * 0.5 +

find_matches.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export async function prepareAlreadyConnectedMessage(user) {
184184
let agentPromptText = agentPrompt.promptMessages[0].prompt.template;
185185

186186
const alreadyConnectedSchema = z.object({
187-
message: z.string().describe("A message informing about already being connected and finding new matches")
187+
message: z.string().describe("A message informing about already being connected with suggested user and finding new matches")
188188
});
189189

190190
const prompt = `Create a message for a user who indicated they are already connected with the suggested match.

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ async function checkMessages() {
3535
payload.message.senderContext?.username ||
3636
'Unknown';
3737

38+
const username = payload.message.senderContext?.username;
39+
40+
3841
const messages = await farcaster.getMessages(conversationId);
3942

4043

@@ -43,7 +46,8 @@ async function checkMessages() {
4346
messages,
4447
conversationId,
4548
senderFid,
46-
name
49+
name,
50+
username
4751
});
4852

4953

0 commit comments

Comments
 (0)