-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
191 lines (167 loc) · 8.14 KB
/
index.js
File metadata and controls
191 lines (167 loc) · 8.14 KB
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
import 'dotenv/config';
import Farcaster from './farcaster.js';
import { findUsers, indexUser, createMatch, updateMatchStatus } from './db.js';
import { evaluatePossibleMatches, prepareIntroMessage, prepareMatchSuccessMessage, prepareNoMatchMessage } from './find_matches.js';
import { runAgent } from './agent.js';
const token = process.env.FARCASTER_TOKEN;
const agentFID = parseInt(process.env.AGENT_FID);
const farcaster = new Farcaster(agentFID, token);
async function manageGroup() {
try {
// Create a new group
const result = await farcaster.matchUsers('Seren <> Seren', [7317], 'You matched!');
console.log(result);
console.log('Removed self from group');
} catch (error) {
console.error('Error managing group:', error);
}
}
async function checkMessages() {
try {
const ws = await farcaster.connectToWarpcastStream();
ws.on('message', async (data) => {
try {
const incomingMessage = JSON.parse(data.toString());
const { messageType, payload } = incomingMessage;
if (messageType !== 'refresh-direct-cast-conversation' || payload.message.senderFid === agentFID) {
//console.log("skipping message", messageType,payload);
return;
}
const { conversationId, senderFid } = payload.message;
const name = payload.message.senderContext?.displayName ||
payload.message.senderContext?.username ||
'Unknown';
const messages = await farcaster.getMessages(conversationId);
const response = await runAgent({
fid: senderFid,
name: name,
}, messages);
console.log(response);
// Handle the response
if (response.message) {
await farcaster.sendMessage(conversationId, [senderFid], response.message);
}
// Handle user attributes if provided
if (response.user_attributes && response.user_intent) {
await indexUser({
fid: senderFid,
name,
attributes: response.user_attributes,
intent: response.user_intent
});
}
// Handle find_matches action
if (response.action === 'find_matches' && response.user_intent) {
const potentialMatches = await findUsers({
fid: senderFid,
attributes: response.user_attributes,
intent: response.user_intent
}, 30);
if (potentialMatches.length > 0) {
const evaluation = await evaluatePossibleMatches(
{
fid: senderFid,
name,
attributes: response.user_attributes,
intent: response.user_intent
},
potentialMatches
);
if (evaluation.match && evaluation.match.status !== 'none') {
const matchedUser = potentialMatches.find(m => parseInt(m.fid) === parseInt(evaluation.match.fid));
if (!matchedUser) {
console.log(evaluation, potentialMatches);
console.log("No matched user found");
return;
}
// Create a pending match in the database
await createMatch(senderFid.toString(), matchedUser.fid.toString());
// Send introduction message to the original user
const introMessage = await prepareIntroMessage({
fid: senderFid,
name,
attributes: response.user_attributes,
intent: response.user_intent
}, matchedUser, true); // true indicates this is the first user
await farcaster.sendMessage(conversationId, [senderFid], introMessage);
} else {
// No suitable match was found among potential matches
const noMatchMessage = await prepareNoMatchMessage({
name,
attributes: response.user_attributes,
intent: response.user_intent
});
await farcaster.sendMessage(
conversationId,
[senderFid],
noMatchMessage
);
}
} else {
// No potential matches were found at all
const noMatchMessage = await prepareNoMatchMessage({
name,
attributes: response.user_attributes,
intent: response.user_intent
});
await farcaster.sendMessage(
conversationId,
[senderFid],
noMatchMessage
);
}
} else if (response.action === 'accept_match') {
// Handle match acceptance
const match = await updateMatchStatus(
response.user_fid,
response.match_fid,
senderFid.toString(),
'accepted'
);
if (match.status === 'accepted') {
// Both users have accepted, create the group
const matchedUser = await findUserByFid(response.match_fid);
const currentUser = await findUserByFid(senderFid);
const groupMessage = await prepareMatchSuccessMessage(currentUser, matchedUser);
await farcaster.matchUsers(
`${currentUser.name} <> ${matchedUser.name}`,
[parseInt(currentUser.fid), parseInt(matchedUser.fid)],
groupMessage
);
} else {
// Inform the user their acceptance is recorded
await farcaster.sendMessage(
conversationId,
[senderFid],
"Great! I've recorded your acceptance. I'll connect you two once they accept as well!"
);
}
} else if (response.action === 'decline_match') {
// Handle match decline
await updateMatchStatus(
response.user_fid,
response.match_fid,
senderFid.toString(),
'declined'
);
await farcaster.sendMessage(
conversationId,
[senderFid],
"No problem! I'll keep looking for other potential matches for you."
);
}
} catch (error) {
console.error('Error handling message:', error);
}
});
// Keep the process running
process.on('SIGINT', () => {
console.log('Closing WebSocket connection...');
ws.close();
process.exit();
});
} catch (error) {
console.error('Error connecting to Warpcast stream:', error);
}
}
checkMessages();