-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
889 lines (813 loc) Β· 40.9 KB
/
server.js
File metadata and controls
889 lines (813 loc) Β· 40.9 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
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
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const path = require('path');
const fetch = require('node-fetch');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
let multer, pdfParse, mammoth;
try { multer = require('multer'); } catch(e) {}
try { pdfParse = require('pdf-parse'); } catch(e) {}
try { mammoth = require('mammoth'); } catch(e) {}
const upload = multer ? multer({ storage: multer.memoryStorage(), limits: { fileSize: 10*1024*1024 } }) : null;
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// ββ CONFIG ββββββββββββββββββββββββββββββββββββββββββββββββ
const ANTHROPIC_KEY = process.env.ANTHROPIC_API_KEY || '';
const INSFORGE_URL = (process.env.INSFORGE_URL || '').replace(/\/$/, '');
const INSFORGE_KEY = process.env.INSFORGE_ADMIN_KEY || '';
const TINYFISH_KEY = process.env.TINYFISH_API_KEY || '';
const JWT_SECRET = process.env.JWT_SECRET || 'civicpulse_secret';
const GOV_CREDS = {};
(process.env.GOV_CREDENTIALS || 'gov_admin:Admin@2024,gov_officer:Officer@2024')
.split(',').forEach(pair => {
const [u, p] = pair.trim().split(':');
if (u && p) GOV_CREDS[u] = p;
});
const useIF = () => !!(INSFORGE_URL && INSFORGE_KEY);
console.log('\nβββββββββββββββ CivicPulse Config ββββββββββββββββ');
console.log(' INSFORGE_URL :', INSFORGE_URL || '(not set)');
console.log(' INSFORGE_KEY :', INSFORGE_KEY ? INSFORGE_KEY.slice(0,12)+'...' : '(not set)');
console.log(' ANTHROPIC_KEY :', ANTHROPIC_KEY ? ANTHROPIC_KEY.slice(0,16)+'...' : '(not set)');
console.log(' useInsForge() :', useIF());
console.log('ββββββββββββββββββββββββββββββββββββββββββββββββββ\n');
// ββ IN-MEMORY FALLBACK ββββββββββββββββββββββββββββββββββββ
let mem = { users: [], surveys: [], responses: [] };
let memId = { users: 1, surveys: 1, responses: 1 };
// Analysis cache β keyed by survey id
const analysisCache = {};
const CHUNK_SIZE = 250; // InsForge string column max ~255 chars
// Split a string into chunks
function toChunks(str) {
const chunks = {};
for (let i = 0; i < 20; i++) {
chunks[`chunk_${i}`] = str.slice(i * CHUNK_SIZE, (i + 1) * CHUNK_SIZE) || null;
}
return chunks;
}
// Reassemble chunks back into string
function fromChunks(row) {
let result = '';
for (let i = 0; i < 20; i++) {
if (row[`chunk_${i}`]) result += row[`chunk_${i}`];
else break;
}
return result;
}
// Save analysis to InsForge cp_analysis table
async function saveAnalysisToIF(surveyId, analysisJson) {
try {
// Check if record exists
const existing = await ifGet(`/api/database/records/cp_analysis?survey_id=eq.${encodeURIComponent(surveyId)}`);
const rows = unwrap(existing);
const chunks = toChunks(analysisJson);
const payload = { survey_id: String(surveyId), ...chunks, created_at: new Date().toISOString() };
if (rows.length > 0) {
// Update existing
await ifPatch(`/api/database/records/cp_analysis/${rows[0].id}`, chunks);
console.log(' β Analysis updated in InsForge cp_analysis');
} else {
// Insert new
await ifPost('/api/database/records/cp_analysis', payload);
console.log(' β Analysis saved to InsForge cp_analysis');
}
} catch(e) {
console.warn(' β Could not save analysis to InsForge:', e.message);
}
}
// Load analysis from InsForge cp_analysis table
async function loadAnalysisFromIF(surveyId) {
try {
const raw = await ifGet(`/api/database/records/cp_analysis?survey_id=eq.${encodeURIComponent(surveyId)}`);
const rows = unwrap(raw);
if (rows.length > 0) {
const json = fromChunks(rows[0]);
if (json && json.length > 10) return json;
}
} catch(e) {
console.warn(' β Could not load analysis from InsForge:', e.message);
}
return null;
}
// ββ INSFORGE HELPERS ββββββββββββββββββββββββββββββββββββββ
async function ifReq(method, path, body = null) {
const opts = {
method,
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${INSFORGE_KEY}` }
};
if (body) opts.body = JSON.stringify(body);
const r = await fetch(INSFORGE_URL + path, opts);
const text = await r.text();
try { return JSON.parse(text); } catch { return text; }
}
const ifGet = p => ifReq('GET', p);
const ifPost = (p, b) => ifReq('POST', p, b);
const ifPatch = (p, b) => ifReq('PATCH', p, b);
const ifDel = p => ifReq('DELETE', p);
// Unwrap any InsForge list response into a plain array
function unwrap(raw) {
if (Array.isArray(raw)) return raw;
if (raw && Array.isArray(raw.data)) return raw.data;
if (raw && Array.isArray(raw.records)) return raw.records;
if (raw && Array.isArray(raw.rows)) return raw.rows;
if (raw && Array.isArray(raw.results)) return raw.results;
return [];
}
// Filter records by column value
async function ifWhere(table, col, val) {
const raw = await ifGet(`/api/database/records/${table}?${col}=eq.${encodeURIComponent(val)}`);
return unwrap(raw);
}
// ββ DB INIT βββββββββββββββββββββββββββββββββββββββββββββββ
async function initDB() {
if (!useIF()) {
console.log('β‘ In-memory mode (no InsForge configured)');
return;
}
console.log('π InsForge DB Init...');
// List existing tables
let existing = [];
try {
const r = await ifGet('/api/database/tables');
existing = Array.isArray(r) ? r : [];
console.log(' β Existing tables:', existing.join(', ') || 'none');
} catch(e) { console.log(' β Could not list tables:', e.message); }
// InsForge column schema (confirmed from validator errors):
// { columnName, type, isNullable, isUnique, defaultValue? }
// Valid types: string, text, integer, boolean, timestamp, uuid
const c = (columnName, type, isNullable, isUnique, defaultValue) =>
({ columnName, type, isNullable, isUnique, ...(defaultValue ? { defaultValue } : {}) });
// InsForge confirmed valid types: string, integer, boolean, datetime
// 'text' crashes with sqlType error, 'timestamp' must be 'datetime', 'uuid' β 'string'
const TABLES = [
{
tableName: 'cp_users', rlsEnabled: false,
columns: [
c('username', 'string', false, true),
c('password_hash', 'string', false, false),
c('created_at', 'datetime', true, false)
]
},
{
tableName: 'cp_surveys', rlsEnabled: false,
columns: [
c('question', 'string', false, false),
c('author', 'string', false, false),
c('target_responses', 'integer', true, false),
c('context_json', 'string', true, false),
c('status', 'string', true, false),
c('analysis_json', 'string', true, false),
c('published_at', 'datetime', true, false)
]
},
{
tableName: 'cp_responses', rlsEnabled: false,
columns: [
c('survey_id', 'string', false, false),
c('username', 'string', false, false),
c('answer', 'string', false, false),
c('submitted_at', 'datetime', true, false)
]
},
{
tableName: 'cp_analysis', rlsEnabled: false,
columns: [
c('survey_id', 'string', false, true),
c('chunk_0', 'string', true, false),
c('chunk_1', 'string', true, false),
c('chunk_2', 'string', true, false),
c('chunk_3', 'string', true, false),
c('chunk_4', 'string', true, false),
c('chunk_5', 'string', true, false),
c('chunk_6', 'string', true, false),
c('chunk_7', 'string', true, false),
c('chunk_8', 'string', true, false),
c('chunk_9', 'string', true, false),
c('chunk_10', 'string', true, false),
c('chunk_11', 'string', true, false),
c('chunk_12', 'string', true, false),
c('chunk_13', 'string', true, false),
c('chunk_14', 'string', true, false),
c('chunk_15', 'string', true, false),
c('chunk_16', 'string', true, false),
c('chunk_17', 'string', true, false),
c('chunk_18', 'string', true, false),
c('chunk_19', 'string', true, false),
c('created_at', 'datetime', true, false)
]
}
];
// Always drop our tables to ensure correct column types (text for long fields)
for (const t of ['cp_analysis', 'cp_responses', 'cp_surveys', 'cp_users']) {
if (existing.includes(t)) {
try {
await ifDel(`/api/database/tables/${t}`);
console.log(` β Dropped: ${t}`);
} catch(e) { console.log(` ~ Could not drop ${t}: ${e.message}`); }
}
}
// Recreate with correct types
for (const t of TABLES) {
try {
const r = await ifPost('/api/database/tables', t);
if (r && r.error) {
console.error(` β ${t.tableName}: ${r.message || JSON.stringify(r).slice(0,150)}`);
} else {
console.log(` β Created: ${t.tableName}`);
}
} catch(e) { console.error(` β ${t.tableName}:`, e.message); }
}
console.log('β
InsForge ready\n');
}
// ββ AUTH MIDDLEWARE βββββββββββββββββββββββββββββββββββββββ
function auth(req, res, next) {
const token = (req.headers.authorization || '').replace('Bearer ', '');
if (!token) return res.status(401).json({ error: 'No token' });
try { req.user = jwt.verify(token, JWT_SECRET); next(); }
catch { res.status(401).json({ error: 'Invalid token' }); }
}
// ββ CITIZEN REGISTER ββββββββββββββββββββββββββββββββββββββ
app.post('/api/auth/register', async (req, res) => {
const { username, password } = req.body;
if (!username || username.length < 3)
return res.status(400).json({ error: 'Username must be at least 3 characters' });
if (!password || password.length < 6)
return res.status(400).json({ error: 'Password must be at least 6 characters' });
try {
// Check duplicate
if (useIF()) {
const rows = await ifWhere('cp_users', 'username', username);
if (rows.length > 0) return res.status(409).json({ error: 'Username already taken' });
} else {
if (mem.users.find(u => u.username === username))
return res.status(409).json({ error: 'Username already taken' });
}
const hash = await bcrypt.hash(password, 10);
if (useIF()) {
const r = await ifPost('/api/database/records/cp_users', { username, password_hash: hash });
console.log(`β Registered: ${username} β`, JSON.stringify(r).slice(0,100));
if (r && (r.code || r.error)) throw new Error(r.message || JSON.stringify(r));
} else {
mem.users.push({ id: memId.users++, username, password_hash: hash });
}
// Auto-login β return token immediately
const token = jwt.sign({ username, role: 'citizen' }, JWT_SECRET, { expiresIn: '7d' });
res.json({ success: true, token, username });
} catch(e) {
console.error('Register error:', e.message);
res.status(500).json({ error: 'Registration failed: ' + e.message });
}
});
// ββ CITIZEN LOGIN βββββββββββββββββββββββββββββββββββββββββ
app.post('/api/auth/login', async (req, res) => {
const { username, password } = req.body;
console.log(`π Login: "${username}"`);
if (!username || !password)
return res.status(400).json({ error: 'Username and password required' });
try {
let user;
if (useIF()) {
const rows = await ifWhere('cp_users', 'username', username);
console.log(` β InsForge lookup: ${rows.length} row(s)`);
user = rows[0] || null;
} else {
user = mem.users.find(u => u.username === username) || null;
}
if (!user) return res.status(401).json({ error: 'User not found. Please register first.' });
const ok = await bcrypt.compare(password, user.password_hash);
if (!ok) return res.status(401).json({ error: 'Incorrect password' });
const token = jwt.sign({ username, role: 'citizen' }, JWT_SECRET, { expiresIn: '7d' });
console.log(` β Login success: "${username}"`);
res.json({ token, username });
} catch(e) {
console.error('Login error:', e.message);
res.status(500).json({ error: 'Login failed: ' + e.message });
}
});
// ββ GOVERNMENT LOGIN ββββββββββββββββββββββββββββββββββββββ
app.post('/api/auth/gov-login', (req, res) => {
const { username, password } = req.body;
if (!GOV_CREDS[username] || GOV_CREDS[username] !== password)
return res.status(401).json({ error: 'Invalid government credentials' });
const token = jwt.sign({ username, role: 'government' }, JWT_SECRET, { expiresIn: '7d' });
res.json({ token, username });
});
// ββ GET ALL SURVEYS βββββββββββββββββββββββββββββββββββββββ
app.get('/api/surveys', async (req, res) => {
try {
let surveys;
if (useIF()) {
const raw = await ifGet('/api/database/records/cp_surveys');
surveys = unwrap(raw);
console.log(`π GET surveys β ${surveys.length} rows`);
// Inject analysis β from memory cache first, then load from InsForge if missing
for (let i = 0; i < surveys.length; i++) {
const sv = surveys[i];
if (analysisCache[sv.id]) {
surveys[i] = { ...sv, analysis_json: analysisCache[sv.id] };
} else if (sv.status === 'complete') {
// Not in memory cache β load from InsForge
const stored = await loadAnalysisFromIF(sv.id);
if (stored) {
analysisCache[sv.id] = stored; // re-cache
surveys[i] = { ...sv, analysis_json: stored };
console.log(` β Loaded analysis from InsForge for survey ${sv.id}`);
}
}
}
} else {
surveys = mem.surveys;
}
res.json(surveys);
} catch(e) {
console.error('GET surveys:', e.message);
res.status(500).json({ error: e.message });
}
});
// ββ POST SURVEY (gov only) ββββββββββββββββββββββββββββββββ
app.post('/api/surveys', auth, async (req, res) => {
if (req.user.role !== 'government')
return res.status(403).json({ error: 'Government only' });
try {
const sv = {
question: req.body.question,
author: req.user.username,
target_responses: req.body.target_responses || 10,
status: 'active',
published_at: new Date().toISOString()
};
if (req.body.context_json) sv.context_json = req.body.context_json;
let saved;
if (useIF()) {
const raw = await ifPost('/api/database/records/cp_surveys', sv);
console.log(` β Survey insert raw:`, JSON.stringify(raw).slice(0, 200));
if (raw && (raw.code || raw.error)) throw new Error(raw.message || JSON.stringify(raw));
// Extract saved record
if (Array.isArray(raw) && raw[0]) saved = raw[0];
else if (raw && raw.data && Array.isArray(raw.data)) saved = raw.data[0];
else if (raw && raw.data) saved = raw.data;
else if (raw && raw.id) saved = raw;
else saved = { ...sv, id: 'local_' + Date.now() };
} else {
sv.id = memId.surveys++;
mem.surveys.push(sv);
saved = sv;
}
console.log(`β Survey posted by ${req.user.username}: "${sv.question.slice(0,50)}" id=${saved.id}`);
res.json(saved);
} catch(e) {
console.error('POST survey:', e.message);
res.status(500).json({ error: e.message });
}
});
// ββ PATCH SURVEY ββββββββββββββββββββββββββββββββββββββββββ
app.patch('/api/surveys/:id', auth, async (req, res) => {
if (req.user.role !== 'government') return res.status(403).json({ error: 'Forbidden' });
try {
if (useIF()) {
await ifPatch(`/api/database/records/cp_surveys/${req.params.id}`, req.body);
} else {
const s = mem.surveys.find(s => s.id == req.params.id);
if (s) Object.assign(s, req.body);
}
res.json({ success: true });
} catch(e) { res.status(500).json({ error: e.message }); }
});
// ββ GET RESPONSES βββββββββββββββββββββββββββββββββββββββββ
app.get('/api/responses/:surveyId', async (req, res) => {
try {
let responses;
if (useIF()) {
const raw = await ifGet(`/api/database/records/cp_responses?survey_id=eq.${encodeURIComponent(req.params.surveyId)}`);
responses = unwrap(raw);
} else {
responses = mem.responses.filter(r => r.survey_id == req.params.surveyId);
}
res.json(responses);
} catch(e) { res.status(500).json({ error: e.message }); }
});
// ββ POST RESPONSE (citizen) βββββββββββββββββββββββββββββββ
app.post('/api/responses', auth, async (req, res) => {
const { survey_id, answer } = req.body;
const username = req.user.username;
if (!answer || !answer.trim()) return res.status(400).json({ error: 'Answer cannot be empty' });
try {
// Duplicate check
if (useIF()) {
const raw = await ifGet(`/api/database/records/cp_responses?survey_id=eq.${survey_id}&username=eq.${encodeURIComponent(username)}`);
if (unwrap(raw).length > 0) return res.status(409).json({ error: 'Already responded' });
} else {
if (mem.responses.some(r => r.survey_id == survey_id && r.username === username))
return res.status(409).json({ error: 'Already responded' });
}
const resp = { survey_id: String(survey_id), username, answer, submitted_at: new Date().toISOString() };
if (useIF()) {
const r = await ifPost('/api/database/records/cp_responses', resp);
if (r && (r.code || r.error)) throw new Error(r.message || JSON.stringify(r));
} else {
resp.id = memId.responses++;
mem.responses.push(resp);
}
// Check if target reached
let allResp;
if (useIF()) {
const raw = await ifGet(`/api/database/records/cp_responses?survey_id=eq.${survey_id}`);
allResp = unwrap(raw);
} else {
allResp = mem.responses.filter(r => r.survey_id == survey_id);
}
let survey;
if (useIF()) {
const raw = await ifGet(`/api/database/records/cp_surveys/${survey_id}`);
survey = Array.isArray(raw) ? raw[0] : (raw && raw.id ? raw : null);
} else {
survey = mem.surveys.find(s => s.id == survey_id);
}
if (survey && allResp.length >= (survey.target_responses || 10)) {
if (useIF()) {
await ifPatch(`/api/database/records/cp_surveys/${survey_id}`, { status: 'complete' });
} else {
Object.assign(survey, { status: 'complete' });
}
triggerAnalysis(survey, allResp);
}
res.json({ success: true });
} catch(e) {
console.error('Response error:', e.message);
res.status(500).json({ error: e.message });
}
});
// ββ AI PROXY ββββββββββββββββββββββββββββββββββββββββββββββ
app.post('/api/ai', auth, async (req, res) => {
if (!ANTHROPIC_KEY) return res.status(503).json({ error: 'No Anthropic API key' });
try {
const r = await fetch('https://api.anthropic.com/v1/messages', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'x-api-key': ANTHROPIC_KEY, 'anthropic-version': '2023-06-01' },
body: JSON.stringify({
model: req.body.model || 'claude-sonnet-4-20250514',
max_tokens: req.body.max_tokens || 1000,
system: req.body.system,
messages: req.body.messages
})
});
res.json(await r.json());
} catch(e) { res.status(500).json({ error: e.message }); }
});
// ββ TINYFISH PROXY ββββββββββββββββββββββββββββββββββββββββ
app.post('/api/research', auth, async (req, res) => {
if (!TINYFISH_KEY) return res.status(503).json({ error: 'TinyFish not configured' });
try {
const r = await fetch('https://agent.tinyfish.ai/v1/automation/run-sse', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-API-Key': TINYFISH_KEY },
body: JSON.stringify({ url: req.body.url, goal: req.body.goal, proxy_config: { enabled: false } })
});
const lines = (await r.text()).split('\n').filter(l => l.startsWith('data:'));
let result = '';
for (const l of lines) {
try { const d = JSON.parse(l.slice(5)); result += d.result || d.output || d.text || ''; } catch {}
}
res.json({ result: result || null });
} catch(e) { res.status(500).json({ error: e.message }); }
});
// ββ CONFIG STATUS βββββββββββββββββββββββββββββββββββββββββ
app.get('/api/config/status', (req, res) => {
res.json({
ai: !!ANTHROPIC_KEY,
insforge: useIF(),
tinyfish: !!TINYFISH_KEY,
storage: useIF() ? 'insforge' : 'memory',
mem_users: useIF() ? null : mem.users.length
});
});
// ββ DOCUMENT UPLOAD βββββββββββββββββββββββββββββββββββββββ
app.post('/api/upload-doc', auth, (req, res, next) => {
if (req.user.role !== 'government') return res.status(403).json({ error: 'Government only' });
if (!upload) return res.status(503).json({ error: 'multer not installed' });
upload.single('document')(req, res, next);
}, async (req, res) => {
if (!req.file) return res.status(400).json({ error: 'No file uploaded' });
const { originalname, mimetype, buffer } = req.file;
try {
let text = '';
if (mimetype === 'application/pdf' || originalname.endsWith('.pdf')) {
if (!pdfParse) return res.status(503).json({ error: 'pdf-parse not installed' });
text = (await pdfParse(buffer)).text;
} else if (originalname.endsWith('.docx')) {
if (!mammoth) return res.status(503).json({ error: 'mammoth not installed' });
text = (await mammoth.extractRawText({ buffer })).value;
} else {
return res.status(400).json({ error: 'Only PDF and DOCX supported' });
}
res.json({ text: text.slice(0, 15000), filename: originalname, length: text.length });
} catch(e) { res.status(500).json({ error: e.message }); }
});
// ββ SHOOT QUESTION TO PUBLIC ββββββββββββββββββββββββββββββ
const shootQs = {};
app.post('/api/surveys/:id/shoot-question', auth, (req, res) => {
if (req.user.role !== 'government') return res.status(403).json({ error: 'Government only' });
const { question } = req.body;
if (!question || !question.trim()) return res.status(400).json({ error: 'Question required' });
const svId = req.params.id;
if (!shootQs[svId]) shootQs[svId] = [];
const q = { id: Date.now(), question: question.trim(), author: req.user.username, postedAt: new Date().toISOString() };
shootQs[svId].push(q);
res.json(q);
});
app.get('/api/surveys/:id/shoot-questions', (req, res) => {
res.json(shootQs[req.params.id] || []);
});
// ββ AI ANALYSIS (background) ββββββββββββββββββββββββββββββ
// ββ TINYFISH HELPER ββββββββββββββββββββββββββββββββββββββ
// ββ TINYFISH: SSE reader using node-fetch v2 buffer βββββ
async function tinyfishSearch(url, goal) {
if (!TINYFISH_KEY) return '';
return new Promise((resolve) => {
let result = '';
let settled = false;
let buffer = '';
const done = (val) => {
if (settled) return;
settled = true;
console.log(' TinyFish done, chars:', (val||'').length);
resolve((val || '').trim());
};
const hardTimer = setTimeout(() => {
console.warn(' β± TinyFish 90s timeout β partial:', result.length, 'chars');
done(result);
}, 20000);
const processLine = (line) => {
line = line.trim();
if (!line.startsWith('data:')) return;
const raw = line.slice(5).trim();
if (!raw) return;
try {
const d = JSON.parse(raw);
if (d.type) console.log(' TinyFish event:', d.type);
// COMPLETE event β result can be object or string
if (d.type === 'COMPLETE' || d.type === 'complete') {
let text = '';
if (typeof d.result === 'string') {
text = d.result;
} else if (typeof d.result === 'object' && d.result !== null) {
text = JSON.stringify(d.result);
} else if (typeof d.output === 'string') {
text = d.output;
} else if (d.data) {
text = typeof d.data === 'string' ? d.data : JSON.stringify(d.data);
}
if (text) result = text;
console.log(' β TinyFish COMPLETE, result chars:', result.length);
clearTimeout(hardTimer);
done(result);
return;
}
// PROGRESS events may carry partial data
if (d.type === 'PROGRESS' || d.type === 'progress') {
const partial = d.result || d.output || d.text || d.content || '';
if (partial && typeof partial === 'string') result += partial;
}
// Error event
if (d.type === 'ERROR' || d.type === 'error' || d.status === 'FAILED') {
console.warn(' TinyFish error event:', d.message || d.error || JSON.stringify(d).slice(0,100));
clearTimeout(hardTimer);
done(result);
}
} catch {}
};
fetch('https://agent.tinyfish.ai/v1/automation/run-sse', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-API-Key': TINYFISH_KEY },
body: JSON.stringify({ url, goal, proxy_config: { enabled: false } })
}).then(r => {
if (!r.ok) { console.warn(' TinyFish HTTP', r.status); clearTimeout(hardTimer); done(''); return; }
console.log(' TinyFish connected, streaming...');
r.body.on('data', chunk => {
buffer += chunk.toString('utf8');
const lines = buffer.split('\n');
buffer = lines.pop(); // keep incomplete last line
lines.forEach(processLine);
});
r.body.on('end', () => { clearTimeout(hardTimer); done(result); });
r.body.on('error', e => { console.warn(' TinyFish stream error:', e.message); clearTimeout(hardTimer); done(result); });
}).catch(e => {
console.warn(' TinyFish fetch error:', e.message);
clearTimeout(hardTimer); done('');
});
});
}
async function triggerAnalysis(surveyInput, responsesInput) {
console.log('\nπ¬ βββββββββββββββββββββββββββββββββββββββββββββββββββ');
console.log(' CIVICAI ANALYSIS PIPELINE STARTED');
console.log('βββββββββββββββββββββββββββββββββββββββββββββββββββ');
console.log(' β Input survey fields:', surveyInput ? Object.keys(surveyInput).join(', ') : 'NULL');
// ββ STEP 1: Re-fetch fresh data from InsForge βββββββββ
let survey = surveyInput;
let responses = responsesInput;
// Normalize field names (InsForge may return capitalized keys)
if (survey) {
survey = {
id: survey.id || survey.Id,
question: survey.question || survey.Question || '',
author: survey.author || survey.Author || '',
target_responses: survey.target_responses || survey.Target_responses || 10,
context_json: survey.context_json || survey.Context_json || null,
status: survey.status || survey.Status || 'active',
analysis_json: survey.analysis_json || survey.Analysis_json || null,
published_at: survey.published_at || survey.Published_at || null,
};
}
if (useIF()) {
try {
console.log(' π₯ Step 1: Fetching fresh survey data from InsForge...');
const svRaw = await ifGet(`/api/database/records/cp_surveys/${surveyInput.id}`);
const svFresh = Array.isArray(svRaw) ? svRaw[0] : (svRaw && svRaw.id ? svRaw : null);
if (svFresh) { survey = svFresh; console.log(' β Survey fetched:', survey.question.slice(0,60)); }
const respRaw = await ifGet(`/api/database/records/cp_responses?survey_id=eq.${encodeURIComponent(surveyInput.id)}`);
const respFresh = unwrap(respRaw);
if (respFresh.length > 0) { responses = respFresh; }
console.log(` β Responses fetched: ${responses.length} total`);
responses.forEach((r, i) => console.log(` [${i+1}] ${r.username}: "${String(r.answer).slice(0,60)}"`));
} catch(e) {
console.warn(' β Could not re-fetch from InsForge, using passed data:', e.message);
}
}
// ββ STEP 2: Extract government document βββββββββββββββ
let govDoc = '';
try {
const ctx = JSON.parse(survey.context_json || '[]');
if (Array.isArray(ctx)) {
const d = ctx.find(m => m.role === 'system' && m.content && m.content.startsWith('[GOV_DOC]:'));
if (d) govDoc = d.content.replace('[GOV_DOC]:', '').trim();
}
} catch {}
console.log(` π Step 2: Gov document: ${govDoc ? govDoc.length + ' chars' : 'none'}`);
// ββ STEP 3: TinyFish research (Wikipedia only β reliable) ββ
console.log(' π Step 3: TinyFish research...');
const topic = survey.question || '(unknown topic)';
const topicShort = topic.split(' ').slice(0, 6).join(' ');
const citizenOpinions = responses.map((r, i) => `${i+1}. ${r.username}: "${r.answer}"`).join('\n');
const govDocSnippet = govDoc ? govDoc.slice(0, 3000) : '';
let precedentData = '';
try {
console.log(' π Searching Wikipedia for policy precedents...');
precedentData = await tinyfishSearch(
`https://en.wikipedia.org/wiki/Special:Search?search=${encodeURIComponent(topicShort + ' government policy transport infrastructure')}&ns0=1`,
`Search for and read the most relevant Wikipedia article about "${topicShort}". Extract: (1) real countries or cities that implemented similar government policies, (2) specific statistics and measurable outcomes, (3) lessons learned. Return as plain text with bullet points and numbers.`
);
console.log(` β Research: ${precedentData.length} chars`);
} catch(e) { console.warn(' β Research error:', e.message); }
// ββ STEP 4: TinyFish AI analysis ββββββββββββββββββββββββ
console.log(' π Step 4: TinyFish AI analysis with all data...');
// ββ STEP 5: TinyFish analysis call ββββββββββββββββββββββ
try {
// Build comprehensive goal for TinyFish AI analysis
const analysisGoal = `You are a government policy analyst. Analyse the following data and return a JSON report.
SURVEY QUESTION: "${survey.question}"
GOVERNMENT DOCUMENT (${govDoc ? govDoc.length + ' chars' : 'none provided'}):
${govDocSnippet || 'No document uploaded.'}
CITIZEN RESPONSES (${responses.length} total):
${citizenOpinions}
RESEARCH DATA:
${precedentData ? 'Policy Precedents:\n' + precedentData.slice(0,1000) : ''}
${impactData ? 'Impact Data:\n' + impactData.slice(0,1000) : ''}
Return ONLY valid JSON with this exact structure (no markdown, no explanation):
{"final_decision":"2-3 sentence decision","government_intent":"what gov wants to achieve","government_concern":"core problem being solved","citizen_emotions":"emotions in responses","citizen_concerns":"main citizen concerns","sentiment_breakdown":{"support_percent":60,"oppose_percent":30,"neutral_percent":10,"support_reasons":["reason"],"oppose_reasons":["reason"]},"conflict_analysis":"where they conflict","win_win_solution":"creative solution for both sides","alternative_approaches":[{"name":"name","description":"how","benefits":"benefits","tradeoffs":"tradeoffs"}],"recommended_course_of_action":["Step 1","Step 2","Step 3"],"statistics":{"key_stats":["stat with number"],"comparable_cases":["case with outcome"],"projected_impact":"expected outcome"},"pros":["pro"],"cons":["con"],"environmental_social_factors":"context","urgency":"MEDIUM","confidence":85}`;
console.log(' π Sending to TinyFish for AI analysis...');
const rawResult = await tinyfishSearch(
'https://en.wikipedia.org/wiki/Government',
analysisGoal
);
console.log(' π TinyFish analysis result length:', rawResult.length);
if (!rawResult || rawResult.length < 50) {
throw new Error('TinyFish returned empty analysis β check your TinyFish credits/quota');
}
// Parse JSON from result
const clean = rawResult.replace(/^```json\s*/,'').replace(/^```\s*/,'').replace(/```\s*$/,'').trim();
let analysis;
try {
analysis = JSON.parse(clean);
} catch(parseErr) {
const match = clean.match(/\{[\s\S]*\}/);
if (match) {
analysis = JSON.parse(match[0]);
} else {
// TinyFish couldn't do structured analysis β build basic one from what we have
console.warn(' β Could not parse JSON, building basic analysis...');
const supportCount = responses.filter(r =>
/good|useful|benefit|support|like|great|yes|agree|positive/i.test(r.answer)
).length;
const opposeCount = responses.length - supportCount;
analysis = {
final_decision: `Based on ${responses.length} citizen responses about "${survey.question}", the analysis shows mixed public opinion requiring careful consideration.`,
government_intent: survey.question,
government_concern: govDoc ? 'See uploaded policy document for details.' : 'As stated in survey question.',
citizen_emotions: responses.map(r => r.answer).join(' | '),
citizen_concerns: responses.map(r => `${r.username}: ${r.answer}`).join('; '),
sentiment_breakdown: {
support_percent: Math.round((supportCount / responses.length) * 100),
oppose_percent: Math.round((opposeCount / responses.length) * 100),
neutral_percent: 0,
support_reasons: responses.filter(r => /good|useful|benefit/i.test(r.answer)).map(r => r.answer),
oppose_reasons: responses.filter(r => /bad|not|don't|against/i.test(r.answer)).map(r => r.answer)
},
conflict_analysis: 'Citizens have differing views on this policy.',
win_win_solution: 'Implement in phases with public consultation at each stage.',
alternative_approaches: [{ name: 'Phased Implementation', description: 'Roll out in stages with feedback', benefits: 'Reduces risk', tradeoffs: 'Takes longer' }],
recommended_course_of_action: ['Conduct detailed feasibility study', 'Hold public consultation', 'Implement pilot phase', 'Evaluate and expand'],
statistics: { key_stats: [precedentData.slice(0,200) || 'Research data unavailable'], comparable_cases: ['Similar projects in other cities have shown mixed results'], projected_impact: 'Impact depends on implementation quality' },
pros: ['Addresses stated government goal', 'Has some public support'],
cons: ['Some citizen opposition', 'Requires careful planning'],
environmental_social_factors: impactData.slice(0,300) || 'Environmental assessment required.',
urgency: 'MEDIUM',
confidence: 60
};
}
}
const analysisJson = JSON.stringify(analysis);
console.log(' β Analysis parsed, length:', analysisJson.length);
// ββ STEP 6: Save analysis βββββββββββββββββββββββββ
// Cache in memory for fast access
analysisCache[survey.id] = analysisJson;
console.log(' β Cached analysis in memory, length:', analysisJson.length);
if (useIF()) {
// Save chunks to cp_analysis table
await saveAnalysisToIF(survey.id, analysisJson);
// Update survey status
await ifPatch(`/api/database/records/cp_surveys/${survey.id}`, { status: 'complete' });
console.log(' β Survey status set to complete in InsForge');
} else {
const s = mem.surveys.find(s => s.id == survey.id);
if (s) { s.analysis_json = analysisJson; s.status = 'complete'; }
}
console.log('\n β
ANALYSIS COMPLETE for survey:', survey.id);
console.log(' π Sentiment:', analysis.sentiment_breakdown?.support_percent + '% support,', analysis.sentiment_breakdown?.oppose_percent + '% oppose');
console.log(' β‘ Urgency:', analysis.urgency, '| Confidence:', analysis.confidence + '%');
console.log('βββββββββββββββββββββββββββββββββββββββββββββββββββ\n');
} catch(e) {
console.error('\n β ANALYSIS FAILED:', e.message);
console.error('βββββββββββββββββββββββββββββββββββββββββββββββββββ\n');
}
}
// ββ MANUAL TRIGGER (for testing without hitting response target) ββ
app.post('/api/admin/trigger-analysis/:surveyId', auth, async (req, res) => {
if (req.user.role !== 'government') return res.status(403).json({ error: 'Government only' });
try {
const surveyId = req.params.surveyId;
let survey, responses;
if (useIF()) {
// Fetch all surveys and find by id (single record fetch may return unexpected shape)
const allRaw = await ifGet('/api/database/records/cp_surveys');
const all = unwrap(allRaw);
console.log(` β All surveys: ${all.length}, looking for id="${surveyId}"`);
survey = all.find(s => String(s.id) === String(surveyId));
if (!survey) {
// Also try direct fetch
const svRaw = await ifGet(`/api/database/records/cp_surveys/${surveyId}`);
console.log(' β Direct fetch raw:', JSON.stringify(svRaw).slice(0,200));
survey = Array.isArray(svRaw) ? svRaw[0] : (svRaw && svRaw.id ? svRaw : null);
if (!survey && svRaw && typeof svRaw === 'object') {
// Try unwrapping
const arr = unwrap(svRaw);
survey = arr[0] || null;
}
}
const respRaw = await ifGet(`/api/database/records/cp_responses?survey_id=eq.${encodeURIComponent(surveyId)}`);
responses = unwrap(respRaw);
console.log(` β Survey found: ${!!survey}, responses: ${responses.length}`);
if (survey) console.log(' β Survey fields:', Object.keys(survey).join(', '));
} else {
survey = mem.surveys.find(s => String(s.id) === String(surveyId));
responses = mem.responses.filter(r => String(r.survey_id) === String(surveyId));
}
if (!survey) return res.status(404).json({ error: `Survey not found (id=${surveyId})` });
if (!responses.length) return res.status(400).json({ error: 'No responses yet to analyse' });
const q = survey.question || survey.Question || '(no question field)';
res.json({ success: true, message: `Analysis triggered for "${String(q).slice(0,50)}" with ${responses.length} response(s)` });
triggerAnalysis(survey, responses);
} catch(e) {
console.error('trigger-analysis error:', e.message);
res.status(500).json({ error: e.message });
}
});
// ββ GET ANALYSIS FOR A SURVEY ββββββββββββββββββββββββββββ
app.get('/api/surveys/:id/analysis', (req, res) => {
const cached = analysisCache[req.params.id];
if (cached) return res.json({ analysis_json: cached, source: 'cache' });
res.status(404).json({ error: 'Analysis not ready yet' });
});
// ββ SERVE FRONTEND ββββββββββββββββββββββββββββββββββββββββ
app.get('*', (req, res) => res.sendFile(path.join(__dirname, 'public', 'index.html')));
// ββ START βββββββββββββββββββββββββββββββββββββββββββββββββ
initDB().then(() => {
app.listen(PORT, () => {
console.log(`ποΈ CivicPulse β http://localhost:${PORT}`);
console.log(` AI: ${ANTHROPIC_KEY ? 'β
Claude' : 'β No key'}`);
console.log(` Backend: ${useIF() ? 'β
InsForge' : 'β‘ Memory'}`);
console.log(` Research: ${TINYFISH_KEY ? 'β
TinyFish' : 'β οΈ No key'}\n`);
});
});