forked from SLFMR1/ADAdev.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
893 lines (749 loc) Β· 28.2 KB
/
server.js
File metadata and controls
893 lines (749 loc) Β· 28.2 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
890
891
892
893
require('dotenv').config()
const express = require('express')
const path = require('path')
const cors = require('cors')
const OpenAI = require('openai')
const app = express()
const PORT = process.env.PORT || 3000
// Initialize OpenAI client (server-side only)
const openai = new OpenAI({
apiKey: process.env.VITE_OPENAI_API_KEY,
})
// Security: Configure CORS with specific origins
const corsOptions = {
origin: process.env.NODE_ENV === 'production'
? ['https://adadev.io', 'https://www.adadev.io'] // Production domain
: ['http://localhost:5173', 'http://localhost:3000'],
credentials: true,
optionsSuccessStatus: 200
}
app.use(cors(corsOptions))
// Security: Add basic security headers
app.use((req, res, next) => {
res.setHeader('X-Content-Type-Options', 'nosniff')
res.setHeader('X-Frame-Options', 'DENY')
res.setHeader('X-XSS-Protection', '1; mode=block')
res.setHeader('Referrer-Policy', 'strict-origin-when-cross-origin')
next()
})
// Add JSON body parsing middleware with size limit
app.use(express.json({ limit: '1mb' }))
// Serve static files from dist directory
app.use(express.static(path.join(__dirname, 'dist')))
// Application-level GitHub cache
const GITHUB_CACHE = {
data: new Map(),
timestamps: new Map(),
CACHE_DURATION: 24 * 60 * 60 * 1000, // 24 hours in milliseconds
lastCleanup: Date.now()
}
// Cache cleanup function
const cleanupExpiredCache = () => {
const now = Date.now()
const expiredKeys = []
for (const [key, timestamp] of GITHUB_CACHE.timestamps.entries()) {
if (now - timestamp > GITHUB_CACHE.CACHE_DURATION) {
expiredKeys.push(key)
}
}
expiredKeys.forEach(key => {
GITHUB_CACHE.data.delete(key)
GITHUB_CACHE.timestamps.delete(key)
})
if (expiredKeys.length > 0) {
console.log(`π§Ή Cleaned up ${expiredKeys.length} expired cache entries`)
}
GITHUB_CACHE.lastCleanup = now
}
// Clean up expired cache every hour
setInterval(cleanupExpiredCache, 60 * 60 * 1000)
// GitHub API utilities
const GITHUB_API_BASE = 'https://api.github.com'
const GITHUB_TOKEN = process.env.GITHUB_TOKEN || null // Use token if available
// Log token status (without exposing sensitive information)
console.log(`π GitHub Token Status: ${GITHUB_TOKEN ? 'β
Token available' : 'β No token found'}`)
// Rate limiting for server-side requests
let requestCount = 0
const MAX_REQUESTS_PER_HOUR = 60
let lastRequestTime = 0
const MIN_REQUEST_INTERVAL = 1000 // 1 second between requests
const rateLimitedFetch = async (url, options = {}) => {
const now = Date.now()
// Rate limiting
const timeSinceLastRequest = now - lastRequestTime
if (timeSinceLastRequest < MIN_REQUEST_INTERVAL) {
const waitTime = MIN_REQUEST_INTERVAL - timeSinceLastRequest
await new Promise(resolve => setTimeout(resolve, waitTime))
}
requestCount++
lastRequestTime = Date.now()
console.log(`π‘ Server making GitHub API request (${requestCount}/${MAX_REQUESTS_PER_HOUR}): ${url}`)
try {
const response = await fetch(url, {
...options,
headers: {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'adaDEV-Platform',
...(GITHUB_TOKEN && { 'Authorization': `token ${GITHUB_TOKEN}` }),
...options.headers
}
})
if (response.status === 429) {
throw new Error('GitHub API rate limit exceeded')
}
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`)
}
return response
} catch (error) {
console.error(`β GitHub API request failed: ${error.message}`)
throw error
}
}
// Extract repo path from GitHub URL
const extractRepoPath = (githubUrl) => {
if (!githubUrl) return null
// Handle organization URLs (e.g., https://github.com/masumi-network)
const orgMatch = githubUrl.match(/github\.com\/([^\/]+)$/)
if (orgMatch) {
return orgMatch[1]
}
// Handle repository URLs (e.g., https://github.com/owner/repo)
const repoMatch = githubUrl.match(/github\.com\/([^\/]+\/[^\/]+)/)
return repoMatch ? repoMatch[1] : null
}
// Fetch latest releases
const fetchLatestReleases = async (repoPath, limit = 3) => {
try {
const response = await rateLimitedFetch(`${GITHUB_API_BASE}/repos/${repoPath}/releases?per_page=${limit}`)
const releases = await response.json()
return releases.map(release => ({
id: release.id,
name: release.name || release.tag_name,
tagName: release.tag_name,
publishedAt: release.published_at,
body: release.body,
htmlUrl: release.html_url,
prerelease: release.prerelease,
draft: release.draft
}))
} catch (error) {
if (error.message.includes('404')) {
console.log(`β οΈ No releases found for ${repoPath} (repository might be private or moved)`)
} else {
console.error(`Error fetching releases for ${repoPath}:`, error)
}
return []
}
}
// Fetch recent commits
const fetchRecentCommits = async (repoPath, limit = 5) => {
try {
const response = await rateLimitedFetch(`${GITHUB_API_BASE}/repos/${repoPath}/commits?per_page=${limit}`)
const commits = await response.json()
return commits.map(commit => ({
sha: commit.sha,
message: commit.commit.message,
author: commit.commit.author,
date: commit.commit.author.date,
htmlUrl: commit.html_url,
shortSha: commit.sha.substring(0, 7)
}))
} catch (error) {
if (error.message.includes('404')) {
console.log(`β οΈ No commits found for ${repoPath} (repository might be private or moved)`)
} else {
console.error(`Error fetching commits for ${repoPath}:`, error)
}
return []
}
}
// Fetch repository information
const fetchRepositoryInfo = async (repoPath) => {
try {
const response = await rateLimitedFetch(`${GITHUB_API_BASE}/repos/${repoPath}`)
const repo = await response.json()
return {
name: repo.name,
fullName: repo.full_name,
description: repo.description,
stargazersCount: repo.stargazers_count,
forksCount: repo.forks_count,
language: repo.language,
pushedAt: repo.pushed_at,
htmlUrl: repo.html_url
}
} catch (error) {
if (error.message.includes('404')) {
console.log(`β οΈ Repository info not found for ${repoPath} (repository might be private or moved)`)
} else {
console.error(`Error fetching repository info for ${repoPath}:`, error)
}
return null
}
}
// Fetch organization repositories
const fetchOrganizationRepos = async (orgName, limit = 5) => {
try {
const response = await rateLimitedFetch(`${GITHUB_API_BASE}/orgs/${orgName}/repos?sort=updated&per_page=${limit}`)
const repos = await response.json()
return repos.map(repo => ({
name: repo.name,
fullName: repo.full_name,
description: repo.description,
stargazersCount: repo.stargazers_count,
forksCount: repo.forks_count,
language: repo.language,
pushedAt: repo.pushed_at,
htmlUrl: repo.html_url,
updatedAt: repo.updated_at
}))
} catch (error) {
console.error(`Error fetching organization repos for ${orgName}:`, error)
return []
}
}
// Generate cache key for a resource
const generateCacheKey = (resource) => {
const githubUrl = resource.social?.github
if (!githubUrl) return null
const repoPath = extractRepoPath(githubUrl)
if (!repoPath) return null
const normalizedName = resource.name.toLowerCase().replace(/[^a-z0-9]/g, '_')
return `github_${normalizedName}_${repoPath.replace('/', '_')}`
}
// Get cached data for a resource
const getCachedData = (resource) => {
const cacheKey = generateCacheKey(resource)
if (!cacheKey) return null
const cached = GITHUB_CACHE.data.get(cacheKey)
const timestamp = GITHUB_CACHE.timestamps.get(cacheKey)
if (cached && timestamp && (Date.now() - timestamp) < GITHUB_CACHE.CACHE_DURATION) {
console.log(`π Server cache hit for ${resource.name}`)
return cached
}
console.log(`β Server cache miss for ${resource.name}`)
return null
}
// Set cached data for a resource
const setCachedData = (resource, data) => {
const cacheKey = generateCacheKey(resource)
if (!cacheKey) return false
GITHUB_CACHE.data.set(cacheKey, data)
GITHUB_CACHE.timestamps.set(cacheKey, Date.now())
console.log(`πΎ Server cached data for ${resource.name}`)
return true
}
// Fetch GitHub updates for a resource (with caching)
const fetchGitHubUpdates = async (resource) => {
const githubUrl = resource.social?.github
if (!githubUrl) {
return { releases: [], commits: [], repoInfo: null }
}
const repoPath = extractRepoPath(githubUrl)
if (!repoPath) {
return { releases: [], commits: [], repoInfo: null }
}
// Check cache first
const cachedData = getCachedData(resource)
if (cachedData) {
return cachedData
}
console.log(`π Server fetching fresh GitHub data for ${resource.name} (${repoPath})`)
try {
let releases = []
let commits = []
let repoInfo = null
// Check if this is an organization URL
if (!repoPath.includes('/')) {
console.log(`π’ Server detected organization: ${repoPath}`)
const repos = await fetchOrganizationRepos(repoPath, 1)
if (repos.length === 0) {
return { releases: [], commits: [], repoInfo: null }
}
const mostActiveRepo = repos[0]
console.log(`π¦ Server using most active repo: ${mostActiveRepo.fullName}`)
const [repoReleases, repoCommits] = await Promise.all([
fetchLatestReleases(mostActiveRepo.fullName, 3),
fetchRecentCommits(mostActiveRepo.fullName, 3)
])
releases = repoReleases
commits = repoCommits
repoInfo = mostActiveRepo
} else {
console.log(`π¦ Server detected repository: ${repoPath}`)
const [repoReleases, repoCommits, repoInfo] = await Promise.all([
fetchLatestReleases(repoPath, 3),
fetchRecentCommits(repoPath, 3),
fetchRepositoryInfo(repoPath)
])
releases = repoReleases
commits = repoCommits
}
const result = {
releases,
commits,
repoInfo,
lastUpdated: new Date().toISOString()
}
// Cache the result
setCachedData(resource, result)
console.log(`π Server ${resource.name} results:`, {
releases: result.releases.length,
commits: result.commits.length,
repoInfo: repoInfo ? 'available' : 'skipped'
})
return result
} catch (error) {
console.error(`Server error fetching GitHub updates for ${resource.name}:`, error)
return { releases: [], commits: [], repoInfo: null }
}
}
// Import resources data for initial fetch
const fs = require('fs')
// Load resources data for initial fetch
const loadResourcesData = () => {
try {
// Read the resources.js file and extract the data
const resourcesPath = path.join(__dirname, 'src', 'data', 'resources.js')
const resourcesContent = fs.readFileSync(resourcesPath, 'utf8')
// Extract the cardanoResources object using regex
const resourcesMatch = resourcesContent.match(/export const cardanoResources = ({[\s\S]*});/)
if (!resourcesMatch) {
console.log('β Could not parse resources data')
return []
}
// Evaluate the resources object (safe since it's our own file)
const resourcesString = resourcesMatch[1]
const resources = eval(`(${resourcesString})`)
// Flatten all resources from all categories
const allResources = []
Object.values(resources).forEach(category => {
if (Array.isArray(category)) {
allResources.push(...category)
}
})
// Filter resources with GitHub URLs
const resourcesWithGitHub = allResources.filter(resource =>
resource.social?.github
)
console.log(`π Loaded ${resourcesWithGitHub.length} resources with GitHub URLs`)
return resourcesWithGitHub
} catch (error) {
console.error('β Error loading resources data:', error)
return []
}
}
// Initial data fetch function
const performInitialDataFetch = async () => {
console.log('π Starting initial GitHub data fetch...')
const resources = loadResourcesData()
if (resources.length === 0) {
console.log('β No resources found for initial fetch')
return
}
// Select top 15 most popular resources for initial fetch (increased from 10)
const topResources = resources.slice(0, 15)
console.log(`π Pre-loading cache for ${topResources.length} resources`)
let successCount = 0
let errorCount = 0
for (let i = 0; i < topResources.length; i++) {
const resource = topResources[i]
try {
console.log(`π [${i + 1}/${topResources.length}] Fetching data for ${resource.name}...`)
const data = await fetchGitHubUpdates(resource)
if (data.releases.length > 0 || data.commits.length > 0) {
console.log(`β
${resource.name}: ${data.releases.length} releases, ${data.commits.length} commits`)
successCount++
} else {
console.log(`β οΈ ${resource.name}: No data available`)
}
// Add delay between requests to respect rate limits
if (i < topResources.length - 1) {
await new Promise(resolve => setTimeout(resolve, 2000)) // 2 second delay
}
} catch (error) {
console.error(`β Error fetching data for ${resource.name}:`, error.message)
errorCount++
}
}
console.log(`π Initial fetch complete: ${successCount} successful, ${errorCount} errors`)
console.log(`π Cache now contains ${GITHUB_CACHE.data.size} entries`)
// Start background process to load remaining resources
setTimeout(() => {
performBackgroundDataFetch(resources.slice(15))
}, 5000) // 5 second delay before starting background fetch
}
// Background data fetch for remaining resources
const performBackgroundDataFetch = async (remainingResources) => {
if (remainingResources.length === 0) {
console.log('β
No remaining resources to fetch')
return
}
console.log(`π Starting background fetch for ${remainingResources.length} remaining resources...`)
let successCount = 0
let errorCount = 0
for (let i = 0; i < remainingResources.length; i++) {
const resource = remainingResources[i]
try {
console.log(`π Background [${i + 1}/${remainingResources.length}] Fetching data for ${resource.name}...`)
const data = await fetchGitHubUpdates(resource)
if (data.releases.length > 0 || data.commits.length > 0) {
console.log(`β
Background ${resource.name}: ${data.releases.length} releases, ${data.commits.length} commits`)
successCount++
} else {
console.log(`β οΈ Background ${resource.name}: No data available`)
}
// Longer delay for background fetch to be less aggressive
if (i < remainingResources.length - 1) {
await new Promise(resolve => setTimeout(resolve, 3000)) // 3 second delay
}
} catch (error) {
console.error(`β Background error fetching data for ${resource.name}:`, error.message)
errorCount++
}
}
console.log(`π Background fetch complete: ${successCount} successful, ${errorCount} errors`)
console.log(`π Cache now contains ${GITHUB_CACHE.data.size} entries`)
}
// AI Processing Functions
const filterRelevantResources = (allResources, userInput) => {
const inputLower = userInput.toLowerCase()
const keywords = inputLower.split(' ').filter(word => word.length > 2)
// Define category priorities based on keywords
const categoryPriorities = {
'smart contract': ['Development Tools', 'Libraries & Languages'],
'defi': ['Development Tools', 'Infrastructure & APIs', 'Wallets & User Tools'],
'nft': ['Minting and NFTs', 'Development Tools'],
'wallet': ['Wallets & User Tools', 'Development Tools'],
'api': ['Infrastructure & APIs', 'Development Tools'],
'security': ['Security & Auditing', 'Development Tools'],
'ai': ['AI & Machine Learning', 'Development Tools'],
'oracle': ['Oracles & External Data', 'Infrastructure & APIs'],
'privacy': ['Privacy & Zero-Knowledge', 'Security & Auditing'],
'identity': ['Identity & Authentication', 'Security & Auditing'],
'analytics': ['Analytics & Data', 'Infrastructure & APIs'],
'education': ['Education & Documentation'],
'community': ['Community & Engagement'],
'infrastructure': ['Core Infrastructure', 'Infrastructure & APIs'],
'scaling': ['Layer 2 Scaling Solutions', 'Infrastructure & APIs'],
'governance': ['Governance & DAOs', 'Community & Engagement']
}
// Score resources based on relevance
const scoredResources = allResources.map(resource => {
let score = 0
// Check category priority
for (const [keyword, priorityCategories] of Object.entries(categoryPriorities)) {
if (inputLower.includes(keyword) && priorityCategories.includes(resource.category)) {
score += 10
}
}
// Check name and description matches
if (resource.name.toLowerCase().includes(inputLower)) score += 5
if (resource.description.toLowerCase().includes(inputLower)) score += 3
// Check key solutions
resource.keySolutions.forEach(solution => {
if (solution.toLowerCase().includes(inputLower)) score += 2
})
return { ...resource, relevanceScore: score }
})
// Sort by relevance and return top 30
return scoredResources
.sort((a, b) => b.relevanceScore - a.relevanceScore)
.slice(0, 30)
.map(({ relevanceScore, ...resource }) => resource)
}
// AI Analysis endpoint
app.post('/api/ai/analyze', async (req, res) => {
try {
const { userInput } = req.body
// Security: Input validation and sanitization
if (!userInput || typeof userInput !== 'string') {
return res.status(400).json({
error: 'Invalid input',
message: 'userInput must be a non-empty string'
})
}
// Sanitize input
const sanitizedInput = userInput.trim().substring(0, 1000) // Limit length
if (sanitizedInput.length < 10) {
return res.status(400).json({
error: 'Input too short',
message: 'Please provide a more detailed description (minimum 10 characters)'
})
}
// Check for suspicious patterns
const suspiciousPatterns = [
/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
/javascript:/gi,
/on\w+\s*=/gi,
/data:text\/html/gi
]
for (const pattern of suspiciousPatterns) {
if (pattern.test(sanitizedInput)) {
return res.status(400).json({
error: 'Invalid input',
message: 'Input contains disallowed content'
})
}
}
// Load resources data
const resources = loadResourcesData()
if (resources.length === 0) {
return res.status(500).json({
error: 'No resources available',
message: 'Failed to load resources data'
})
}
// Filter to most relevant resources
const relevantResources = filterRelevantResources(resources, userInput)
// Create a concise resource summary for the AI
const resourceSummary = relevantResources.map(resource => ({
name: resource.name,
category: resource.category,
description: resource.description,
keySolutions: resource.keySolutions
}))
const prompt = `
You are a Cardano development expert. A developer wants to build something on Cardano.
User Requirements: "${sanitizedInput}"
Available Cardano Tools (most relevant):
${JSON.stringify(resourceSummary, null, 2)}
Analyze the user's requirements and return a JSON object with this structure:
{
"analysis": "Brief analysis of what the user wants to build",
"recommendedTools": [
{
"resource": "Resource name from the list",
"reason": "Why this tool is recommended",
"priority": "high|medium|low"
}
],
"developmentPlan": {
"overview": "Brief overview of the development approach",
"approaches": [
{
"name": "Approach name",
"description": "Description of this approach",
"tools": ["List of tools for this approach"],
"complexity": "beginner|intermediate|advanced"
}
]
}
}
Keep the response concise and focus on the most relevant tools.`
const completion = await openai.chat.completions.create({
model: "gpt-4",
messages: [
{
role: "system",
content: "You are a Cardano development expert. Provide accurate, practical advice for building on Cardano. Always return valid JSON. Keep responses concise."
},
{
role: "user",
content: prompt
}
],
temperature: 0.3,
max_tokens: 1500
}).catch(error => {
console.error('OpenAI API error:', error)
if (error.code === 'insufficient_quota') {
throw new Error('AI service quota exceeded. Please try again later.')
} else if (error.code === 'rate_limit_exceeded') {
throw new Error('AI service rate limit exceeded. Please try again later.')
} else if (error.message.includes('context length')) {
throw new Error('Request too complex. Please try a more specific description.')
} else {
throw new Error('AI service temporarily unavailable. Please try again.')
}
})
const response = completion.choices[0]?.message?.content
if (!response) {
throw new Error('No response from AI service')
}
let parsedResponse
try {
parsedResponse = JSON.parse(response)
} catch (parseError) {
console.error('Failed to parse AI response:', parseError)
throw new Error('Invalid response format from AI service')
}
// Validate required fields
if (!parsedResponse.analysis || !parsedResponse.recommendedTools || !parsedResponse.developmentPlan) {
throw new Error('AI response missing required fields')
}
// Map recommended tools back to actual resource objects
const recommendedResources = (parsedResponse.recommendedTools || [])
.map(rec => {
if (!rec || !rec.resource) return null
const resource = resources.find(r => r.name === rec.resource)
return resource ? { ...resource, reason: rec.reason || 'Recommended for your use case', priority: rec.priority || 'medium' } : null
})
.filter(Boolean)
const result = {
analysis: parsedResponse.analysis || 'Analysis not available',
recommendedResources,
developmentPlan: parsedResponse.developmentPlan || { overview: 'Development plan not available', approaches: [] }
}
res.json(result)
} catch (error) {
console.error('AI analysis error:', error)
if (error.message.includes('context length') || error.message.includes('tokens')) {
return res.status(400).json({
error: 'Request too complex',
message: 'Please try a more specific description of what you want to build.'
})
}
res.status(500).json({
error: 'AI analysis failed',
message: 'Failed to analyze requirements. Please try again.'
})
}
})
// API Routes
// Get GitHub updates for a specific resource
app.get('/api/github/:resourceId', async (req, res) => {
try {
const { resourceId } = req.params
// For now, we'll need to get the resource data from the client
// In a real implementation, you'd have a resources database
res.json({
error: 'Resource not found',
message: 'Please provide resource data in request body'
})
} catch (error) {
console.error('API error:', error)
res.status(500).json({ error: 'Internal server error' })
}
})
// Get GitHub updates for a resource (POST with resource data)
app.post('/api/github/updates', async (req, res) => {
try {
console.log('π₯ Received POST /api/github/updates for resource:', req.body?.name || 'unknown')
const resource = req.body
if (!resource || !resource.name || !resource.social?.github) {
console.log('β Invalid resource data:', {
hasResource: !!resource,
hasName: !!resource?.name,
hasGithub: !!resource?.social?.github
})
return res.status(400).json({
error: 'Invalid resource data',
message: 'Resource must have name and social.github URL',
received: req.body
})
}
const data = await fetchGitHubUpdates(resource)
res.json(data)
} catch (error) {
console.error('API error:', error)
res.status(500).json({ error: 'Internal server error' })
}
})
// Get global GitHub updates (aggregated from multiple resources)
app.post('/api/github/global', async (req, res) => {
try {
const { resources } = req.body
if (!Array.isArray(resources)) {
return res.status(400).json({
error: 'Invalid resources data',
message: 'Resources must be an array'
})
}
const allData = []
const maxResources = Math.min(5, resources.length) // Limit to 5 resources
for (let i = 0; i < maxResources; i++) {
const resource = resources[i]
if (resource.social?.github) {
try {
const data = await fetchGitHubUpdates(resource)
if (data.releases.length > 0 || data.commits.length > 0) {
allData.push({
resource,
...data
})
}
} catch (error) {
console.warn(`Failed to fetch data for ${resource.name}:`, error.message)
}
}
}
// Sort by latest activity
const sortedData = allData.sort((a, b) => {
const aReleases = (a.releases || []).map(r => new Date(r.publishedAt || 0).getTime())
const aCommits = (a.commits || []).map(c => new Date(c.date || 0).getTime())
const aLatest = aReleases.length > 0 || aCommits.length > 0 ? Math.max(...aReleases, ...aCommits) : 0
const bReleases = (b.releases || []).map(r => new Date(r.publishedAt || 0).getTime())
const bCommits = (b.commits || []).map(c => new Date(c.date || 0).getTime())
const bLatest = bReleases.length > 0 || bCommits.length > 0 ? Math.max(...bReleases, ...bCommits) : 0
return bLatest - aLatest
})
res.json(sortedData)
} catch (error) {
console.error('API error:', error)
res.status(500).json({ error: 'Internal server error' })
}
})
// Get cache status
app.get('/api/github/cache/status', (req, res) => {
const status = {
entries: GITHUB_CACHE.data.size,
lastCleanup: GITHUB_CACHE.lastCleanup,
cacheDuration: GITHUB_CACHE.CACHE_DURATION,
keys: Array.from(GITHUB_CACHE.data.keys())
}
res.json(status)
})
// Clear cache
app.post('/api/github/cache/clear', (req, res) => {
GITHUB_CACHE.data.clear()
GITHUB_CACHE.timestamps.clear()
GITHUB_CACHE.lastCleanup = Date.now()
console.log('ποΈ Server cache cleared')
res.json({ message: 'Cache cleared successfully' })
})
// Trigger initial data fetch manually
app.post('/api/github/cache/preload', async (req, res) => {
try {
console.log('π Manual cache preload triggered')
await performInitialDataFetch()
res.json({
message: 'Cache preload completed',
cacheEntries: GITHUB_CACHE.data.size
})
} catch (error) {
console.error('Error during manual preload:', error)
res.status(500).json({ error: 'Preload failed' })
}
})
// Trigger background fetch for remaining resources
app.post('/api/github/cache/background-fetch', async (req, res) => {
try {
console.log('π Manual background fetch triggered')
const resources = loadResourcesData()
const remainingResources = resources.slice(15) // Skip first 15 that were loaded initially
// Start background fetch
performBackgroundDataFetch(remainingResources)
res.json({
message: 'Background fetch started',
remainingResources: remainingResources.length,
currentCacheEntries: GITHUB_CACHE.data.size
})
} catch (error) {
console.error('Error during background fetch:', error)
res.status(500).json({ error: 'Background fetch failed' })
}
})
// Serve React app for all other routes
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'))
})
app.listen(PORT, () => {
console.log(`π Server running on port ${PORT}`)
console.log(`π GitHub cache initialized with ${GITHUB_CACHE.CACHE_DURATION / (60 * 60 * 1000)} hour duration`)
// Start initial data fetch after server is running
setTimeout(() => {
performInitialDataFetch()
}, 1000) // 1 second delay to ensure server is fully started
})