-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_14.js
275 lines (238 loc) · 8.75 KB
/
task_14.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import fetch from 'node-fetch';
import dotenv from 'dotenv';
import OpenAI from 'openai';
// Load environment variables
dotenv.config();
// Initialize OpenAI
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
// Set to store processed items to avoid infinite loops
const processedPeople = new Set();
const processedPlaces = new Set();
const allConnections = new Map();
// Dodajmy funkcję do śledzenia powiązań
const connections = new Map();
function addConnection(person, city) {
if (!connections.has(person)) {
connections.set(person, new Set());
}
connections.get(person).add(city);
}
async function downloadNote() {
try {
const response = await fetch('https://centrala.ag3nts.org/dane/barbara.txt');
const text = await response.text();
console.log('Downloaded note:', text);
return text;
} catch (error) {
console.error('Error downloading note:', error);
throw error;
}
}
async function searchPerson(name) {
if (processedPeople.has(name)) {
return null;
}
try {
console.log(`Searching for person: ${name}`);
const response = await fetch('https://centrala.ag3nts.org/people', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
apikey: process.env.PERSONAL_API_KEY,
query: name
})
});
const data = await response.json();
processedPeople.add(name);
return data;
} catch (error) {
console.error(`Error searching for person ${name}:`, error);
return null;
}
}
async function searchPlace(city) {
if (processedPlaces.has(city)) {
return null;
}
try {
const response = await fetch('https://centrala.ag3nts.org/places', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
apikey: process.env.PERSONAL_API_KEY,
query: city
})
});
const data = await response.json();
processedPlaces.add(city);
console.log(`Search results for city ${city}:`, data);
return data;
} catch (error) {
console.error(`Error searching for city ${city}:`, error);
return null;
}
}
async function extractEntities(text) {
try {
const response = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [
{
role: "system",
content: "Extract names of people and cities from the text. Important rules:\n" +
"1. Use only capital ASCII letters (A-Z)\n" +
"2. Replace Polish letters: Ą->A, Ę->E, Ł->L, Ó->O, Ś->S, Ż/Ź->Z, Ć->C, Ń->N\n" +
"3. For people, use only first names\n" +
"4. Format response exactly as: {\"people\": [\"NAME1\", \"NAME2\"], \"cities\": [\"CITY1\", \"CITY2\"]}"
},
{
role: "user",
content: text
}
]
});
const result = JSON.parse(response.choices[0].message.content);
console.log('Extracted entities:', result);
return result;
} catch (error) {
console.error('Error extracting entities:', error);
throw error;
}
}
async function processData(initialEntities) {
const toProcess = {
people: [...initialEntities.people],
cities: [...initialEntities.cities]
};
while (toProcess.people.length > 0 || toProcess.cities.length > 0) {
// Process people
while (toProcess.people.length > 0) {
const person = toProcess.people.shift();
const personResult = await searchPerson(person);
if (personResult && personResult.cities) {
for (const city of personResult.cities) {
if (!processedPlaces.has(city)) {
toProcess.cities.push(city);
}
}
}
}
// Process cities
while (toProcess.cities.length > 0) {
const city = toProcess.cities.shift();
const cityResult = await searchPlace(city);
if (cityResult && cityResult.people) {
for (const person of cityResult.people) {
if (!processedPeople.has(person)) {
toProcess.people.push(person);
}
}
}
}
}
}
async function sendReport(location) {
try {
// Najpierw pobierz token dla zadania
const tokenResponse = await fetch('https://centrala.ag3nts.org/report', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
apikey: process.env.PERSONAL_API_KEY
})
});
const tokenData = await tokenResponse.json();
// Teraz wyślij odpowiedź z tokenem
const response = await fetch('https://centrala.ag3nts.org/report' + tokenData.token, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
task: 'loop',
apikey: process.env.PERSONAL_API_KEY,
answer: location
})
});
const result = await response.json();
console.log('Report result:', result);
return result;
} catch (error) {
console.error('Error sending report:', error);
throw error;
}
}
// Dodajmy funkcję do czyszczenia imion
function cleanName(name) {
return name.split(' ')[0].toUpperCase();
}
async function main() {
try {
let connections = new Map();
let barbaraPath = [];
// Najpierw sprawdźmy ruch Barbary
console.log('\nTracking Barbara\'s movement:');
const barbaraResult = await searchPerson('BARBARA');
if (barbaraResult && barbaraResult.message) {
console.log('Barbara\'s known locations:', barbaraResult.message);
}
// Sprawdźmy osoby powiązane z Barbarą
const keyPeople = ['ALEKSANDER', 'ADAM', 'AZAZEL', 'GABRIEL'];
console.log('\nChecking people connected to Barbara:');
for (const person of keyPeople) {
const result = await searchPerson(person);
if (result && result.message) {
const locations = result.message.split(' ')
.filter(loc => !loc.includes('**'));
console.log(`${person}'s path:`, locations.join(' -> '));
connections.set(person, locations);
}
}
// Sprawdźmy chronologicznie miasta
const citiesToCheck = ['KRAKOW', 'WARSZAWA', 'LUBLIN', 'GRUDZIADZ', 'CIECHOCINEK', 'ELBLAG', 'FROMBORK', 'KONIN'];
console.log('\nChecking each city for current visitors:');
for (const city of citiesToCheck) {
const result = await searchPlace(city);
if (result && result.message) {
const visitors = result.message.split(' ')
.filter(v => !v.includes('**'));
console.log(`${city} current visitors:`, visitors);
if (visitors.includes('BARBARA')) {
barbaraPath.push(city);
}
}
}
console.log('\nBarbara\'s movement path:', barbaraPath.join(' -> '));
// Szczególnie sprawdźmy ELBLAG i FROMBORK
console.log('\nDetailed check of key locations:');
const keyLocations = ['ELBLAG', 'FROMBORK'];
for (const city of keyLocations) {
const result = await searchPlace(city);
if (result && result.message) {
console.log(`${city} detailed check:`, result.message);
}
}
// Analiza wzorców
console.log('\nAnalyzing patterns:');
console.log('1. Barbara\'s known path:', barbaraPath);
console.log('2. AZAZEL\'s movements:', connections.get('AZAZEL'));
console.log('3. GABRIEL\'s locations:', connections.get('GABRIEL'));
// Jeśli znaleźliśmy Barbarę w ostatnim mieście jej ścieżki
if (barbaraPath.length > 0) {
const lastLocation = barbaraPath[barbaraPath.length - 1];
console.log('\nAttempting to report Barbara\'s location:', lastLocation);
await sendReport(lastLocation);
}
} catch (error) {
console.error('Error in main:', error);
}
}
main();