-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathrun.sh.prompt.txt
1363 lines (1147 loc) · 51.8 KB
/
run.sh.prompt.txt
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
claude-3-5-sonnet-20240620
# Example Project
This is the example project which has 3 main files "server.py", "index.html" and "run.sh".
Here are their contents:
## server.py
```python
import sqlite3
import threading
import time
import traceback
import csv
import io
from flask import Flask, jsonify, request, send_from_directory, send_file
from flask_cors import CORS
from flask_socketio import SocketIO
from engy import web_search_agent
app = Flask(__name__)
CORS(app)
socketio = SocketIO(app, cors_allowed_origins="*")
DATABASE = 'crm.db'
ENTRIES_PER_PAGE = 10
# Virtual Agent Configuration
AGENT_NAME = "Web Search Agent"
AGENT_AVATAR = "https://robohash.org/WebSearchAgent.png?size=100x100&set=set3"
AGENT_PROMPT = '''Advanced competitor searcher.
Search internet to find all information of competitor company "wordware.ai".
'''
# Data generation control
data_generation_active = False
data_generation_thread = None
def get_db_connection():
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
return conn
def init_db():
with get_db_connection() as conn:
conn.execute('''CREATE TABLE IF NOT EXISTS crm_entries
(id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
link TEXT,
content TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)''')
conn.commit()
def produce_data_point(data_entry: dict):
try:
with get_db_connection() as conn:
c = conn.cursor()
title = data_entry['title']
link = data_entry['link']
content = data_entry['content']
c.execute("INSERT INTO crm_entries (title, link, content) VALUES (?, ?, ?)",
(title, link, content))
conn.commit()
# Fetch the newly inserted entry
new_entry = c.execute("SELECT * FROM crm_entries WHERE id = last_insert_rowid()").fetchone()
# Emit the new entry to all connected clients
socketio.emit('new_entry', dict(new_entry))
print(f"New entry added and emitted: {dict(new_entry)}")
except Exception as e:
print(f"Error in produce_data_point: {str(e)}")
traceback.print_exc()
def ingest_data():
global data_generation_active
while data_generation_active:
web_search_agent(AGENT_PROMPT, produce_data_point)
time.sleep(10)
@app.route('/')
def index():
return send_from_directory('.', 'index.html')
@app.route('/api/entries', methods=['GET'])
def get_entries():
page = request.args.get('page', 1, type=int)
offset = (page - 1) * ENTRIES_PER_PAGE
with get_db_connection() as conn:
total_entries = conn.execute("SELECT COUNT(*) FROM crm_entries").fetchone()[0]
entries = conn.execute("SELECT * FROM crm_entries ORDER BY timestamp DESC LIMIT ? OFFSET ?",
(ENTRIES_PER_PAGE, offset)).fetchall()
return jsonify({
'entries': [dict(entry) for entry in entries],
'total_entries': total_entries,
'total_pages': (total_entries + ENTRIES_PER_PAGE - 1) // ENTRIES_PER_PAGE
})
@app.route('/api/entries', methods=['POST'])
def add_entry():
data = request.json
if not data or 'title' not in data or 'link' not in data or 'content' not in data:
return jsonify({'error': 'Invalid data'}), 400
with get_db_connection() as conn:
c = conn.cursor()
c.execute("INSERT INTO crm_entries (title, link, content) VALUES (?, ?, ?)",
(data['title'], data['link'], data['content']))
conn.commit()
new_id = c.lastrowid
new_entry = c.execute("SELECT * FROM crm_entries WHERE id = ?", (new_id,)).fetchone()
socketio.emit('new_entry', dict(new_entry))
return jsonify({'id': new_id, 'message': 'Entry added successfully'}), 201
@app.route('/api/agent', methods=['GET'])
def get_agent_info():
return jsonify({
'name': AGENT_NAME,
'avatar': AGENT_AVATAR
})
@app.route('/api/data_generation/start', methods=['POST'])
def start_data_generation():
global data_generation_active, data_generation_thread
if not data_generation_active:
data_generation_active = True
data_generation_thread = threading.Thread(target=ingest_data)
data_generation_thread.start()
return jsonify({'message': 'Data generation started'}), 200
return jsonify({'message': 'Data generation already running'}), 200
@app.route('/api/data_generation/stop', methods=['POST'])
def stop_data_generation():
global data_generation_active, data_generation_thread
if data_generation_active:
data_generation_active = False
if data_generation_thread:
data_generation_thread.join()
return jsonify({'message': 'Data generation stopped'}), 200
return jsonify({'message': 'Data generation not running'}), 200
@app.route('/api/data_generation/status', methods=['GET'])
def get_data_generation_status():
global data_generation_active
return jsonify({'active': data_generation_active})
@app.route('/api/export_csv', methods=['GET'])
def export_csv():
with get_db_connection() as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM crm_entries ORDER BY timestamp DESC")
rows = cursor.fetchall()
output = io.StringIO()
writer = csv.writer(output)
# Write header
writer.writerow(['id', 'title', 'link', 'content', 'timestamp'])
# Write data
for row in rows:
writer.writerow(row)
output.seek(0)
return send_file(
io.BytesIO(output.getvalue().encode('utf-8')),
mimetype='text/csv',
as_attachment=True,
download_name='crm_entries.csv'
)
@socketio.on('connect')
def handle_connect():
print('Client connected')
@socketio.on('disconnect')
def handle_disconnect():
print('Client disconnected')
if __name__ == '__main__':
init_db()
port = 6204
print(f"Server running on port {port}")
socketio.run(app, host='localhost', port=port, debug=True)
```
## index.html
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web Search Agent</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
<style>
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.animate-spin {
animation: spin 1s linear infinite;
}
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-10px); }
}
.animate-bounce {
animation: bounce 2s infinite;
}
.table-container {
max-height: 70vh;
overflow-y: auto;
}
#crmTable th, #crmTable td {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 200px;
}
#crmTable td.expanded {
white-space: normal;
max-width: none;
}
.expand-btn {
cursor: pointer;
user-select: none;
transition: transform 0.3s ease;
}
.expand-btn.expanded {
transform: rotate(180deg);
}
</style>
</head>
<body class="bg-gray-100 min-h-screen">
<header class="bg-gradient-to-r from-purple-600 to-indigo-600 text-white shadow-lg">
<div class="container mx-auto px-4 py-6 flex justify-between items-center">
<h1 class="text-3xl font-bold">Web Search Agent</h1>
<div id="agentInfo" class="flex items-center">
<img id="agentAvatar" src="" alt="Agent Avatar" class="w-12 h-12 rounded-full mr-3 animate-bounce">
<div>
<p class="text-sm">Data Agent</p>
<p id="agentName" class="font-bold"></p>
</div>
</div>
<div class="flex items-center">
<button id="startBtn" class="bg-green-500 text-white px-4 py-2 rounded-lg shadow hover:bg-green-600 transition duration-300 ease-in-out mr-2">
Start Data Gen
</button>
<button id="stopBtn" class="bg-red-500 text-white px-4 py-2 rounded-lg shadow hover:bg-red-600 transition duration-300 ease-in-out mr-2">
Stop Data Gen
</button>
<span id="statusIndicator" class="mr-2">⚪</span>
<button id="refreshBtn" class="bg-white text-indigo-600 px-4 py-2 rounded-lg shadow hover:bg-indigo-100 transition duration-300 ease-in-out transform hover:scale-105 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-opacity-50 mr-2">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 inline-block mr-1" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M4 2a1 1 0 011 1v2.101a7.002 7.002 0 0111.601 2.566 1 1 0 11-1.885.666A5.002 5.002 0 005.999 7H9a1 1 0 010 2H4a1 1 0 01-1-1V3a1 1 0 011-1zm.008 9.057a1 1 0 011.276.61A5.002 5.002 0 0014.001 13H11a1 1 0 110-2h5a1 1 0 011 1v5a1 1 0 11-2 0v-2.101a7.002 7.002 0 01-11.601-2.566 1 1 0 01.61-1.276z" clip-rule="evenodd" />
</svg>
Refresh
</button>
<button id="exportCsvBtn" class="bg-yellow-500 text-white px-4 py-2 rounded-lg shadow hover:bg-yellow-600 transition duration-300 ease-in-out">
Export CSV
</button>
</div>
</div>
</header>
<main class="container mx-auto px-4 py-8">
<div id="newEntriesAlert" class="hidden bg-blue-100 border-l-4 border-blue-500 text-blue-700 p-4 mb-4" role="alert">
<p class="font-bold">New entries available!</p>
<p>Click the button below to load the latest entries.</p>
<button id="loadNewEntriesBtn" class="mt-2 bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Load New Entries
</button>
</div>
<div class="bg-white rounded-lg shadow-md overflow-hidden">
<div class="table-container">
<table id="crmTable" class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Expand</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">ID</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Title</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Link</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Content</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Timestamp</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
<!-- Table rows will be dynamically inserted here -->
</tbody>
</table>
</div>
</div>
<div class="mt-4 flex justify-between items-center">
<div>
<span id="pageInfo" class="text-sm text-gray-600"></span>
</div>
<div>
<button id="prevBtn" class="bg-indigo-600 text-white px-4 py-2 rounded-lg shadow hover:bg-indigo-700 transition duration-300 ease-in-out mr-2">Previous</button>
<button id="nextBtn" class="bg-indigo-600 text-white px-4 py-2 rounded-lg shadow hover:bg-indigo-700 transition duration-300 ease-in-out">Next</button>
</div>
</div>
<div id="loadingSpinner" class="hidden fixed top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2">
<div class="animate-spin rounded-full h-16 w-16 border-t-2 border-b-2 border-indigo-500"></div>
</div>
</main>
<script>
const API_URL = `http://${window.location.hostname}:6204/api`;
const loadingSpinner = document.getElementById('loadingSpinner');
const pageInfo = document.getElementById('pageInfo');
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
const startBtn = document.getElementById('startBtn');
const stopBtn = document.getElementById('stopBtn');
const statusIndicator = document.getElementById('statusIndicator');
const newEntriesAlert = document.getElementById('newEntriesAlert');
const loadNewEntriesBtn = document.getElementById('loadNewEntriesBtn');
const exportCsvBtn = document.getElementById('exportCsvBtn');
let currentPage = 1;
let totalPages = 1;
let totalEntries = 0;
let newEntriesCount = 0;
const socket = io(`http://${window.location.hostname}:6204`);
socket.on('connect', () => {
console.log('Connected to WebSocket');
});
socket.on('new_entry', (entry) => {
console.log('New entry received:', entry);
if (currentPage === 1) {
prependEntry(entry);
} else {
newEntriesCount++;
showNewEntriesAlert();
}
updateTotalEntries(totalEntries + 1);
});
async function fetchAgentInfo() {
try {
const response = await fetch(`${API_URL}/agent`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
document.getElementById('agentName').textContent = data.name;
document.getElementById('agentAvatar').src = data.avatar;
} catch (error) {
console.error('Error fetching agent info:', error);
}
}
async function fetchEntries(page = 1) {
try {
loadingSpinner.classList.remove('hidden');
const response = await fetch(`${API_URL}/entries?page=${page}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
updateTable(data.entries);
updatePagination(page, data.total_pages, data.total_entries);
} catch (error) {
console.error('Error fetching entries:', error);
} finally {
loadingSpinner.classList.add('hidden');
}
}
function updateTable(entries) {
const tableBody = document.querySelector('#crmTable tbody');
tableBody.innerHTML = '';
entries.forEach(entry => {
const row = createTableRow(entry);
tableBody.appendChild(row);
});
}
function createTableRow(entry) {
const row = document.createElement('tr');
row.className = 'hover:bg-gray-50 transition duration-150 ease-in-out';
row.innerHTML = `
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
<svg class="expand-btn w-5 h-5 text-gray-500 hover:text-gray-700" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
</svg>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">${escapeHtml(entry.id)}</td>
<td class="px-6 py-4 text-sm font-medium text-gray-900">${escapeHtml(entry.title)}</td>
<td class="px-6 py-4 text-sm text-indigo-600 hover:text-indigo-900">
<a href="${escapeHtml(entry.link)}" target="_blank" class="hover:underline">${escapeHtml(entry.link)}</a>
</td>
<td class="px-6 py-4 text-sm text-gray-500">${escapeHtml(entry.content)}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${new Date(entry.timestamp).toLocaleString()}</td>
`;
const expandBtn = row.querySelector('.expand-btn');
expandBtn.addEventListener('click', () => {
const cells = row.querySelectorAll('td:not(:first-child)');
cells.forEach(cell => cell.classList.toggle('expanded'));
expandBtn.classList.toggle('expanded');
});
return row;
}
function prependEntry(entry) {
const tableBody = document.querySelector('#crmTable tbody');
const row = createTableRow(entry);
tableBody.insertBefore(row, tableBody.firstChild);
// Remove the last row if we have more than 10 entries
if (tableBody.children.length > 10) {
tableBody.removeChild(tableBody.lastChild);
}
}
function updatePagination(page, total, entries) {
currentPage = page;
totalPages = total;
totalEntries = entries;
pageInfo.textContent = `Page ${page} of ${total} (${entries} total entries)`;
prevBtn.disabled = page === 1;
nextBtn.disabled = page === total;
}
function updateTotalEntries(newTotal) {
totalEntries = newTotal;
totalPages = Math.ceil(totalEntries / 10);
pageInfo.textContent = `Page ${currentPage} of ${totalPages} (${totalEntries} total entries)`;
nextBtn.disabled = currentPage === totalPages;
}
function showNewEntriesAlert() {
newEntriesAlert.classList.remove('hidden');
loadNewEntriesBtn.textContent = `Load ${newEntriesCount} New Entries`;
}
function hideNewEntriesAlert() {
newEntriesAlert.classList.add('hidden');
newEntriesCount = 0;
}
function escapeHtml(unsafe) {
if (typeof unsafe === 'number') {
return unsafe.toString();
}
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
async function startDataGeneration() {
try {
const response = await fetch(`${API_URL}/data_generation/start`, { method: 'POST' });
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
updateDataGenerationStatus(true);
} catch (error) {
console.error('Error starting data generation:', error);
}
}
async function stopDataGeneration() {
try {
const response = await fetch(`${API_URL}/data_generation/stop`, { method: 'POST' });
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
updateDataGenerationStatus(false);
} catch (error) {
console.error('Error stopping data generation:', error);
}
}
async function checkDataGenerationStatus() {
try {
const response = await fetch(`${API_URL}/data_generation/status`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
updateDataGenerationStatus(data.active);
} catch (error) {
console.error('Error checking data generation status:', error);
}
}
function updateDataGenerationStatus(active) {
if (active) {
statusIndicator.textContent = '🟢';
statusIndicator.title = 'Data generation is active';
} else {
statusIndicator.textContent = '🔴';
statusIndicator.title = 'Data generation is inactive';
}
}
async function exportCsv() {
try {
const response = await fetch(`${API_URL}/export_csv`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'crm_entries.csv';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
} catch (error) {
console.error('Error exporting CSV:', error);
}
}
prevBtn.addEventListener('click', () => {
if (currentPage > 1) {
fetchEntries(currentPage - 1);
}
});
nextBtn.addEventListener('click', () => {
if (currentPage < totalPages) {
fetchEntries(currentPage + 1);
}
});
document.getElementById('refreshBtn').addEventListener('click', () => fetchEntries(currentPage));
startBtn.addEventListener('click', startDataGeneration);
stopBtn.addEventListener('click', stopDataGeneration);
exportCsvBtn.addEventListener('click', exportCsv);
loadNewEntriesBtn.addEventListener('click', () => {
fetchEntries(1);
hideNewEntriesAlert();
});
// Initial setup
fetchAgentInfo();
fetchEntries();
checkDataGenerationStatus();
// Periodically check data generation status
setInterval(checkDataGenerationStatus, 5000);
</script>
</body>
</html>
```
## run.sh
```bash
#!/bin/bash
# Exit immediately if a command exits with a non-zero status
set -e
# Create a virtual environment if it doesn't exist
if [ ! -d "venv" ]; then
echo "Creating virtual environment..."
python3 -m venv venv
fi
# Activate the virtual environment
source venv/bin/activate
# Upgrade pip
pip install --upgrade pip
# Install required libraries
echo "Installing required libraries..."
pip install Flask flask-cors flask-socketio
# Install local library if it exists
if [ -d "/data/finae/engy" ]; then
echo "Installing local engy library..."
pip install /data/finae/engy
else
echo "Local engy library not found. Skipping..."
fi
# Start the server
echo "Starting the Web Search Agent server..."
python server.py
# Keep the script running
while true; do
echo "Web Search Agent server is running on port 6204. Press Ctrl+C to stop."
sleep 60
done
```
# Objective
Modify and re-generate these files based on new <FEATURE_REQUEST></FEATURE_REQUEST> statements.
# Output
1. Output new "server.py" source code in <SERVER_PYTHON_CODE></SERVER_PYTHON_CODE> block.
2. Output new "index.html" source code in <INDEX_HTML_CODE></INDEX_HTML_CODE> block.
3. Output new "run.sh" source code in <RUN_BASH_CODE></RUN_BASH_CODE> block.
Only output one of them in each conversation round, based on user query.
=== 0: user ===
<FEATURE_REQUEST>
Based on the example.
Create a new agent that scrape the URL using "from engy import web_scraper_agent".
```
def web_scraper_agent(url: str, producer: Callable[[dict], None]):
content = scrape_tool.run(website_url=url)
producer({'url': url, 'webpage_content': content})
```
</FEATURE_REQUEST>
Generate new "server.py" first. Re-assign a new PORT, to "localhost:8593".
=== 1: assistant ===
Certainly! I'll modify the "server.py" file to include the new web scraper agent, change the port to 8593, and make the necessary adjustments. Here's the updated "server.py" code:
<SERVER_PYTHON_CODE>
import sqlite3
import threading
import time
import traceback
import csv
import io
from flask import Flask, jsonify, request, send_from_directory, send_file
from flask_cors import CORS
from flask_socketio import SocketIO
from engy import web_search_agent, web_scraper_agent
app = Flask(__name__)
CORS(app)
socketio = SocketIO(app, cors_allowed_origins="*")
DATABASE = 'crm.db'
ENTRIES_PER_PAGE = 10
# Virtual Agent Configuration
AGENT_NAME = "Web Search & Scraper Agent"
AGENT_AVATAR = "https://robohash.org/WebSearchScraperAgent.png?size=100x100&set=set3"
AGENT_PROMPT = '''Advanced competitor searcher and web scraper.
Search internet to find all information of competitor company "wordware.ai".
Then scrape the found URLs for more detailed information.
'''
# Data generation control
data_generation_active = False
data_generation_thread = None
def get_db_connection():
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
return conn
def init_db():
with get_db_connection() as conn:
conn.execute('''CREATE TABLE IF NOT EXISTS crm_entries
(id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
link TEXT,
content TEXT,
webpage_content TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)''')
conn.commit()
def produce_data_point(data_entry: dict):
try:
with get_db_connection() as conn:
c = conn.cursor()
title = data_entry.get('title', '')
link = data_entry.get('link', '')
content = data_entry.get('content', '')
webpage_content = data_entry.get('webpage_content', '')
c.execute("INSERT INTO crm_entries (title, link, content, webpage_content) VALUES (?, ?, ?, ?)",
(title, link, content, webpage_content))
conn.commit()
new_entry = c.execute("SELECT * FROM crm_entries WHERE id = last_insert_rowid()").fetchone()
socketio.emit('new_entry', dict(new_entry))
print(f"New entry added and emitted: {dict(new_entry)}")
except Exception as e:
print(f"Error in produce_data_point: {str(e)}")
traceback.print_exc()
def ingest_data():
global data_generation_active
while data_generation_active:
web_search_agent(AGENT_PROMPT, produce_data_point)
time.sleep(10)
def scrape_url(url):
def scrape_producer(data):
with get_db_connection() as conn:
c = conn.cursor()
c.execute("UPDATE crm_entries SET webpage_content = ? WHERE link = ?",
(data['webpage_content'], data['url']))
conn.commit()
socketio.emit('entry_updated', {'url': url, 'webpage_content': data['webpage_content']})
web_scraper_agent(url, scrape_producer)
@app.route('/')
def index():
return send_from_directory('.', 'index.html')
@app.route('/api/entries', methods=['GET'])
def get_entries():
page = request.args.get('page', 1, type=int)
offset = (page - 1) * ENTRIES_PER_PAGE
with get_db_connection() as conn:
total_entries = conn.execute("SELECT COUNT(*) FROM crm_entries").fetchone()[0]
entries = conn.execute("SELECT * FROM crm_entries ORDER BY timestamp DESC LIMIT ? OFFSET ?",
(ENTRIES_PER_PAGE, offset)).fetchall()
return jsonify({
'entries': [dict(entry) for entry in entries],
'total_entries': total_entries,
'total_pages': (total_entries + ENTRIES_PER_PAGE - 1) // ENTRIES_PER_PAGE
})
@app.route('/api/entries', methods=['POST'])
def add_entry():
data = request.json
if not data or 'title' not in data or 'link' not in data or 'content' not in data:
return jsonify({'error': 'Invalid data'}), 400
with get_db_connection() as conn:
c = conn.cursor()
c.execute("INSERT INTO crm_entries (title, link, content) VALUES (?, ?, ?)",
(data['title'], data['link'], data['content']))
conn.commit()
new_id = c.lastrowid
new_entry = c.execute("SELECT * FROM crm_entries WHERE id = ?", (new_id,)).fetchone()
socketio.emit('new_entry', dict(new_entry))
# Start scraping the URL in a separate thread
threading.Thread(target=scrape_url, args=(data['link'],)).start()
return jsonify({'id': new_id, 'message': 'Entry added successfully'}), 201
@app.route('/api/agent', methods=['GET'])
def get_agent_info():
return jsonify({
'name': AGENT_NAME,
'avatar': AGENT_AVATAR
})
@app.route('/api/data_generation/start', methods=['POST'])
def start_data_generation():
global data_generation_active, data_generation_thread
if not data_generation_active:
data_generation_active = True
data_generation_thread = threading.Thread(target=ingest_data)
data_generation_thread.start()
return jsonify({'message': 'Data generation started'}), 200
return jsonify({'message': 'Data generation already running'}), 200
@app.route('/api/data_generation/stop', methods=['POST'])
def stop_data_generation():
global data_generation_active, data_generation_thread
if data_generation_active:
data_generation_active = False
if data_generation_thread:
data_generation_thread.join()
return jsonify({'message': 'Data generation stopped'}), 200
return jsonify({'message': 'Data generation not running'}), 200
@app.route('/api/data_generation/status', methods=['GET'])
def get_data_generation_status():
global data_generation_active
return jsonify({'active': data_generation_active})
@app.route('/api/export_csv', methods=['GET'])
def export_csv():
with get_db_connection() as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM crm_entries ORDER BY timestamp DESC")
rows = cursor.fetchall()
output = io.StringIO()
writer = csv.writer(output)
writer.writerow(['id', 'title', 'link', 'content', 'webpage_content', 'timestamp'])
for row in rows:
writer.writerow(row)
output.seek(0)
return send_file(
io.BytesIO(output.getvalue().encode('utf-8')),
mimetype='text/csv',
as_attachment=True,
download_name='crm_entries.csv'
)
@socketio.on('connect')
def handle_connect():
print('Client connected')
@socketio.on('disconnect')
def handle_disconnect():
print('Client disconnected')
if __name__ == '__main__':
init_db()
port = 8593
print(f"Server running on port {port}")
socketio.run(app, host='localhost', port=port, debug=True)
</SERVER_PYTHON_CODE>
This updated "server.py" file includes the following changes:
1. Added the `web_scraper_agent` import from engy.
2. Updated the `AGENT_NAME` to "Web Search & Scraper Agent".
3. Modified the `AGENT_PROMPT` to include web scraping.
4. Updated the database schema to include a `webpage_content` column.
5. Modified the `produce_data_point` function to handle the new `webpage_content` field.
6. Added a new `scrape_url` function to handle web scraping for each URL.
7. Updated the `add_entry` route to start a new thread for scraping the URL after adding an entry.
8. Modified the CSV export to include the `webpage_content` column.
9. Changed the port to 8593 as requested.
These changes incorporate the new web scraper agent functionality while maintaining the existing web search agent capabilities. The server now scrapes URLs for more detailed information after adding new entries.
=== 2: user ===
Generate new "index.html"
=== 3: assistant ===
Certainly! Here's the updated "index.html" file that incorporates the new web scraper functionality and adjusts to the changes made in the server:
<INDEX_HTML_CODE>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web Search & Scraper Agent</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
<style>
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.animate-spin {
animation: spin 1s linear infinite;
}
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-10px); }
}
.animate-bounce {
animation: bounce 2s infinite;
}
.table-container {
max-height: 70vh;
overflow-y: auto;
}
#crmTable th, #crmTable td {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 200px;
}
#crmTable td.expanded {
white-space: normal;
max-width: none;
}
.expand-btn {
cursor: pointer;
user-select: none;
transition: transform 0.3s ease;
}
.expand-btn.expanded {
transform: rotate(180deg);
}
</style>
</head>
<body class="bg-gray-100 min-h-screen">
<header class="bg-gradient-to-r from-purple-600 to-indigo-600 text-white shadow-lg">
<div class="container mx-auto px-4 py-6 flex justify-between items-center">
<h1 class="text-3xl font-bold">Web Search & Scraper Agent</h1>
<div id="agentInfo" class="flex items-center">
<img id="agentAvatar" src="" alt="Agent Avatar" class="w-12 h-12 rounded-full mr-3 animate-bounce">
<div>
<p class="text-sm">Data Agent</p>
<p id="agentName" class="font-bold"></p>
</div>
</div>
<div class="flex items-center">
<button id="startBtn" class="bg-green-500 text-white px-4 py-2 rounded-lg shadow hover:bg-green-600 transition duration-300 ease-in-out mr-2">
Start Data Gen
</button>
<button id="stopBtn" class="bg-red-500 text-white px-4 py-2 rounded-lg shadow hover:bg-red-600 transition duration-300 ease-in-out mr-2">
Stop Data Gen
</button>
<span id="statusIndicator" class="mr-2">⚪</span>
<button id="refreshBtn" class="bg-white text-indigo-600 px-4 py-2 rounded-lg shadow hover:bg-indigo-100 transition duration-300 ease-in-out transform hover:scale-105 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-opacity-50 mr-2">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 inline-block mr-1" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M4 2a1 1 0 011 1v2.101a7.002 7.002 0 0111.601 2.566 1 1 0 11-1.885.666A5.002 5.002 0 005.999 7H9a1 1 0 010 2H4a1 1 0 01-1-1V3a1 1 0 011-1zm.008 9.057a1 1 0 011.276.61A5.002 5.002 0 0014.001 13H11a1 1 0 110-2h5a1 1 0 011 1v5a1 1 0 11-2 0v-2.101a7.002 7.002 0 01-11.601-2.566 1 1 0 01.61-1.276z" clip-rule="evenodd" />
</svg>
Refresh
</button>
<button id="exportCsvBtn" class="bg-yellow-500 text-white px-4 py-2 rounded-lg shadow hover:bg-yellow-600 transition duration-300 ease-in-out">
Export CSV
</button>
</div>
</div>
</header>
<main class="container mx-auto px-4 py-8">
<div id="newEntriesAlert" class="hidden bg-blue-100 border-l-4 border-blue-500 text-blue-700 p-4 mb-4" role="alert">
<p class="font-bold">New entries available!</p>
<p>Click the button below to load the latest entries.</p>
<button id="loadNewEntriesBtn" class="mt-2 bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Load New Entries
</button>
</div>
<div class="bg-white rounded-lg shadow-md overflow-hidden">
<div class="table-container">
<table id="crmTable" class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Expand</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">ID</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Title</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Link</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Content</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Webpage Content</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Timestamp</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
<!-- Table rows will be dynamically inserted here -->