-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
167 lines (143 loc) · 5.22 KB
/
Copy pathserver.ts
File metadata and controls
167 lines (143 loc) · 5.22 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
import express from "express";
import { createServer as createViteServer } from "vite";
import { GoogleGenAI, Type } from "@google/genai";
async function startServer() {
const app = express();
const PORT = 3000;
app.use(express.json());
// API routes FIRST
app.get("/api/health", (req, res) => {
res.json({ status: "ok" });
});
// POST /v1/projects/{id}/generate
app.post("/v1/projects/:id/generate", async (req, res) => {
try {
const { id } = req.params;
const { outline, brand_config, platform_params } = req.body;
// In a real scenario, we would use the Gemini API to generate content based on the outline.
// For this demo, we'll simulate the SPLIT_SLIDE algorithm and CoverCandidate generation.
// Mocking the SPLIT_SLIDE logic
const slides = splitSlideLogic(outline, platform_params);
// Mocking the CoverCandidate generation
const coverCandidates = generateCoverCandidates(outline, brand_config);
// Mocking Scannability Score
const scannabilityScore = slides.reduce((acc, slide, index) => {
acc[`page${index + 1}`] = Math.floor(Math.random() * 20) + 80; // 80-100 score
return acc;
}, {} as Record<string, number>);
res.json({
job_id: `job_${Date.now()}`,
cover_candidates: coverCandidates,
slides: slides,
scannabilityScore: scannabilityScore,
});
} catch (error) {
console.error("Error generating project:", error);
res.status(500).json({ error: "Failed to generate project" });
}
});
// Vite middleware for development
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
app.use(express.static("dist"));
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${PORT}`);
});
}
// SPLIT_SLIDE Algorithm Simulation
function splitSlideLogic(outline: string, platform_params: any) {
// Hard thresholds: > 7 lines or > 60 words triggers split
const maxLines = 7;
const maxWords = 60;
// Safe area checks (mock)
const safeAreaTop = platform_params?.safe_area?.top || 160;
const safeAreaBottom = platform_params?.safe_area?.bottom || 220;
// Compression Resistance: Ensure auxiliary text is at least 28pt
const minFontPt = platform_params?.min_font_pt || 28;
// Simple mock splitting based on paragraphs
const paragraphs = outline.split('\n\n').filter(p => p.trim());
const slides = [];
let currentSlide = { l1Title: '', l2Points: [] as string[], l3Notes: '', validation: {} };
for (let i = 0; i < paragraphs.length; i++) {
const p = paragraphs[i];
const words = p.split(/\s+/).length;
if (i === 0) {
currentSlide.l1Title = p.substring(0, 30) + (p.length > 30 ? '...' : ''); // HOOK
currentSlide.l2Points.push(p);
} else {
currentSlide.l2Points.push(p); // VALUE
}
// Check if we need to split
if (currentSlide.l2Points.length >= maxLines || currentSlide.l2Points.join(' ').split(/\s+/).length >= maxWords) {
slides.push({
...currentSlide,
aspectRatio: platform_params?.aspect_ratio || '3:4',
validation: { safeArea: `passed (${safeAreaTop}px/${safeAreaBottom}px)`, compressionResistance: `passed (min ${minFontPt}pt)` }
});
currentSlide = { l1Title: 'Continued...', l2Points: [], l3Notes: '', validation: {} };
}
}
if (currentSlide.l1Title || currentSlide.l2Points.length > 0) {
// Last slide can be CTA
currentSlide.l3Notes = "CTA: Like and Subscribe!";
slides.push({
...currentSlide,
aspectRatio: platform_params?.aspect_ratio || '3:4',
validation: { safeArea: `passed (${safeAreaTop}px/${safeAreaBottom}px)`, compressionResistance: `passed (min ${minFontPt}pt)` }
});
}
return slides;
}
function generateCoverCandidates(outline: string, brand_config: any) {
// WCAG 2.2 Contrast Ratio thresholds
const TARGET_CONTRAST = 7.0;
const MIN_CONTRAST = 4.5;
return [
{
id: "cover_1",
imageUrl: "https://picsum.photos/seed/cover1/1080/1440",
coverScore: {
sReadability: 0.9,
sSaliency: 0.85,
sSemantic: 0.88,
sBrand: 0.95
},
contrastRatio: 8.2,
riskFlags: [],
reasons: ["WCAG 2.2 Contrast 8.2:1 (Target ≥7:1 met)", "Title within 160px/220px safe area"]
},
{
id: "cover_2",
imageUrl: "https://picsum.photos/seed/cover2/1080/1440",
coverScore: {
sReadability: 0.7,
sSaliency: 0.9,
sSemantic: 0.8,
sBrand: 0.85
},
contrastRatio: 5.1,
riskFlags: ["low_contrast_warning"],
reasons: ["Contrast 5.1:1 (Below 7:1 target, above 4.5:1 baseline). Auto-fixed with background layer."]
},
{
id: "cover_3",
imageUrl: "https://picsum.photos/seed/cover3/1080/1440",
coverScore: {
sReadability: 0.4,
sSaliency: 0.75,
sSemantic: 0.9,
sBrand: 0.8
},
contrastRatio: 3.2,
riskFlags: ["contrast_failure", "traffic_routing_risk"],
reasons: ["Contrast 3.2:1 (Fails WCAG 4.5:1 baseline)", "Contains potential external link"]
}
];
}
startServer();