-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
411 lines (361 loc) · 19.5 KB
/
Copy pathserver.js
File metadata and controls
411 lines (361 loc) · 19.5 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
// maarmapa — Claude API proxy v6 stable
const express = require('express');
const app = express();
app.use(express.json());
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.header('Access-Control-Max-Age', '86400');
if (req.method === 'OPTIONS') { res.sendStatus(204); return; }
next();
});
// ── Rate limiting (in-memory, per-IP+route) ──────────────────────────────
// Este server no tiene auth: cualquiera con la URL puede llamar /chat, /post,
// /marketing, /factory, /grok, y cada llamada gasta API_KEY (Anthropic) y/o
// GROK_KEY (x.ai) real. Sin límite, un spam script podía generar costos sin
// techo — /factory en particular dispara 1 llamada Claude + 8 generaciones de
// imagen Grok por request. Instancia única en Railway → Map en memoria basta,
// no hace falta Redis.
const rateLimitBuckets = new Map(); // `${path}:${ip}` -> { count, windowStart }
function rateLimit({ windowMs, max }) {
return (req, res, next) => {
const ip = (req.headers['x-forwarded-for'] || '').split(',')[0].trim() || req.socket.remoteAddress || 'unknown';
const key = `${req.path}:${ip}`;
const now = Date.now();
const bucket = rateLimitBuckets.get(key);
if (!bucket || now - bucket.windowStart > windowMs) {
rateLimitBuckets.set(key, { count: 1, windowStart: now });
return next();
}
if (bucket.count >= max) {
const retryAfterSec = Math.ceil((windowMs - (now - bucket.windowStart)) / 1000);
res.set('Retry-After', String(retryAfterSec));
return res.status(429).json({ error: 'rate_limited', retry_after_seconds: retryAfterSec });
}
bucket.count++;
next();
};
}
// Limpieza periódica para que el Map no crezca sin límite.
setInterval(() => {
const cutoff = Date.now() - 60 * 60 * 1000;
for (const [key, b] of rateLimitBuckets) if (b.windowStart < cutoff) rateLimitBuckets.delete(key);
}, 10 * 60 * 1000).unref();
const standardLimit = rateLimit({ windowMs: 15 * 60 * 1000, max: 20 }); // 20 req / 15 min por ruta+IP
const expensiveLimit = rateLimit({ windowMs: 60 * 60 * 1000, max: 3 }); // /factory: 3 req / hora por IP (8 imágenes c/u)
let substackCache = { posts: [], lastFetch: 0 };
async function fetchSubstackRSS() {
const now = Date.now();
if (now - substackCache.lastFetch < 30 * 60 * 1000 && substackCache.posts.length) return substackCache.posts;
try {
const res = await fetch('https://maarmapa.substack.com/feed');
const xml = await res.text();
const items = [...xml.matchAll(/<item>([\s\S]*?)<\/item>/g)];
const posts = items.slice(0, 10).map(m => {
const item = m[1];
const title = (item.match(/<title><!\[CDATA\[(.*?)\]\]><\/title>/) || item.match(/<title>(.*?)<\/title>/))?.[1] || '';
const desc = (item.match(/<description><!\[CDATA\[(.*?)\]\]><\/description>/) || item.match(/<description>(.*?)<\/description>/))?.[1]?.replace(/<[^>]+>/g,'').slice(0,200) || '';
return { title, desc };
});
substackCache = { posts, lastFetch: now };
return posts;
} catch (e) { return substackCache.posts; }
}
function buildSubstackContext(posts) {
if (!posts.length) return '';
return `\n\nPUBLISHED SUBSTACK POSTS:\n` + posts.map((p,i) => `${i+1}. "${p.title}" — ${p.desc}`).join('\n');
}
const INSTAGRAM_CONTEXT = `
INSTAGRAM ECOSYSTEM: @youngspacegallery @art.viewer @painterspainting @contemporaryartcurator @frieze_magazine @artforum @theartbystander @elenasoboleva @whos____who @artnews @ignant @jerrysaltz @matthewjhiggs @antwaunsargent @thegreatwomenartists @dianalnawi @mothflower @hello_artists @anotherkind @themuseumofmodernart @lacma @louisianamuseum @banksy @obeygiant @okudart @globepainter @streetartcities @globalstreetart`;
const BASE_SYSTEM = `You are a research companion and the voice of maarmapa — a Chilean contemporary artist based in Santiago who paints cities as abstract systems. Started in the streets (stencil, spray), evolved to canvas and IPFS/blockchain. Maps cities as nervous systems. The city is a language. Early works: jibtone.blogspot.com. Site: maarmapa.eth.limo. Instagram: @maarmapa.eth. Substack: maarmapa.substack.com.
WORKS (CLP): Subway City Distopy $5M, Subway City $1M, Connect $350K, Rio $250K, MiniCity $250K, SCL $500K, BairesPA $5M, City OG $6.7M, City Love $5M, Cities 0 $5M, Mario Bros Roses $50M, Strawberry Gummy Plant $7M, GlobeFish $9M.
Search web for street art, philosophy (Lefebvre, Benjamin, Baudrillard), art history (Situationism, Latin American avant-gardes), art+blockchain, exhibitions.
${INSTAGRAM_CONTEXT}
Bilingual. Laconic but deep. Never corporate.`;
const SUBSTACK_SYSTEM = `Editorial intelligence for maarmapa's Substack. Sharp, poetic. Spanish primary. Sources: e-flux.com, artforum.com, frieze.com, artishock.net. Structure: Hook → Contexto → Presente → Cierre provocador. 500-700 words. 2-3 real references.${INSTAGRAM_CONTEXT}`;
const DIGEST_SYSTEM = `Weekly art intelligence digest for maarmapa. Search past 7 days: street art, Latin American art, art+blockchain, urbanism philosophy, gallery news. Per item: title, 2-3 line summary, why relevant, Substack angle, Instagram accounts. 5-8 items ranked by relevance.${INSTAGRAM_CONTEXT}`;
const POST_SYSTEM = `You are an expert editorial writer for maarmapa — Chilean contemporary urban artist and art intelligence voice. Generate high-quality, complete Substack posts about art, culture, marketing, blockchain and cities. Always search the web for real, current, specific information before writing. Write complete, untruncated posts with full paragraphs. Never use ellipsis or cut sentences mid-thought.
OUTPUT strict JSON only, no preamble, no markdown fences:
{
"title": "post title",
"subtitle": "one line subtitle",
"tags": ["tag1", "tag2", "tag3"],
"body": "full Substack post in markdown, 500-700 words, Hook→Contexto→Presente→Cierre, end with open question",
"thumbnail_prompt": "detailed image generation prompt in English for the post thumbnail, editorial/dark aesthetic, no people, conceptual",
"instagram": {
"caption": "short instagram caption in Spanish, max 150 chars, lowercase, end with relevant hashtags",
"slides": [
"Slide 1 text (hook, max 10 words)",
"Slide 2 text",
"Slide 3 text",
"Slide 4 text",
"Slide 5 text",
"Slide 6 text (key question or insight)",
"Slide 7 text (CTA or conclusion)"
]
}
}
RULES:
- This is EDITORIAL content, not promotional. Do not mix with maarmapa artworks unless the topic is specifically about them.
- Search web for current facts, dates, names, context
- Spanish for body and instagram. English for thumbnail_prompt.
- Thumbnail must be conceptual, dark editorial aesthetic, no faces/people
- Instagram slides must be punchy, minimal text, designed to stop the scroll
- ONLY output valid JSON, nothing else`;
const TOOLS = [{ type: "web_search_20250305", name: "web_search", max_uses: 2 }];
async function callClaude(messages, system, maxTokens = 1024) {
const res = await fetch('https://api.anthropic.com/v1/messages', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.API_KEY,
'anthropic-version': '2023-06-01'
},
body: JSON.stringify({ model: 'claude-haiku-4-5-20251001', max_tokens: maxTokens, system, tools: TOOLS, messages }),
});
return res.json();
}
async function runWithTools(messages, system, maxTokens) {
let data = await callClaude(messages, system, maxTokens);
let loopMessages = [...messages];
let iterations = 0;
while (data.stop_reason === 'tool_use' && iterations < 5) {
loopMessages.push({ role: 'assistant', content: data.content });
const toolResults = data.content.filter(b => b.type === 'tool_use').map(b => ({ type: 'tool_result', tool_use_id: b.id, content: 'Search completed.' }));
loopMessages.push({ role: 'user', content: toolResults });
data = await callClaude(loopMessages, system, maxTokens);
iterations++;
}
return (data.content || []).filter(b => b.type === 'text').map(b => b.text).join('') || '...';
}
app.post('/chat', standardLimit, async (req, res) => {
const { messages, mode } = req.body;
if (!messages || !Array.isArray(messages)) return res.status(400).json({ error: 'messages array required' });
try {
const posts = await fetchSubstackRSS();
const ctx = buildSubstackContext(posts);
let system = BASE_SYSTEM + ctx, maxTokens = 1024;
if (mode === 'substack') { system = SUBSTACK_SYSTEM + ctx; maxTokens = 2048; }
if (mode === 'digest') { system = DIGEST_SYSTEM + ctx; maxTokens = 2048; }
const reply = await runWithTools(messages, system, maxTokens);
res.json({ reply });
} catch (err) {
console.error(err);
res.status(500).json({ error: err.message });
}
});
app.post('/post', standardLimit, async (req, res) => {
const { topic } = req.body;
if (!topic) return res.status(400).json({ error: 'topic required' });
try {
const posts = await fetchSubstackRSS();
const ctx = buildSubstackContext(posts);
const raw = await runWithTools([{ role: 'user', content: `Generate a complete Substack post about: ${topic}` }], POST_SYSTEM + ctx, 4096);
const clean = raw.replace(/```json|```/g, '').trim();
try { res.json({ post: JSON.parse(clean) }); }
catch { res.json({ post: { title: topic, subtitle: '', tags: [], body: raw } }); }
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.get('/digest', standardLimit, async (req, res) => {
try {
const posts = await fetchSubstackRSS();
const ctx = buildSubstackContext(posts);
const digest = await runWithTools([{ role: 'user', content: 'Generate the weekly art intelligence digest. Search for the most relevant developments from the past 7 days.' }], DIGEST_SYSTEM + ctx, 2048);
res.json({ digest });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.get('/substack', async (req, res) => {
res.json({ posts: await fetchSubstackRSS() });
});
app.get('/', (req, res) => res.json({
status: 'online', service: 'maarmapa agent v6',
endpoints: { 'POST /chat': 'agent|substack|digest', 'POST /post': '{topic}', 'GET /digest': 'weekly', 'GET /substack': 'posts' }
}));
// ── MARKETING / REDES SOCIALES ────────────────────────
const MARKETING_SYSTEM = `You are a creative marketing strategist for maarmapa — a Chilean contemporary urban artist. Search for the latest creative ideas, trends and strategies for visual artists on social media in 2026.
Focus on:
- Instagram, TikTok, YouTube Shorts strategies for visual artists
- Blockchain/NFT marketing for Latin American artists
- Community building and storytelling for urban/street artists
- Content ideas that connect physical street art with digital presence
- Collaboration strategies with galleries, curators, collectors
MAARMAPA CONTEXT: Started in streets (stencil/spray), now canvas + IPFS/blockchain. Maps cities as nervous systems. Instagram @maarmapa.eth, Substack maarmapa.substack.com, Shop wgrtgz-nk.myshopify.com.
Respond in Spanish. Be specific, actionable and creative. Connect ideas to maarmapa's unique position at the intersection of street art, blockchain and Latin American urban culture.`;
app.post('/marketing', standardLimit, async (req, res) => {
const { query } = req.body || {};
const userQuery = query || 'mejores ideas creativas premiadas Cannes Lions marketing redes sociales 2026';
try {
const posts = await fetchSubstackRSS();
const ctx = buildSubstackContext(posts);
const [claudeReply, grokPulse] = await Promise.all([
runWithTools([{ role: 'user', content: userQuery }], MARKETING_SYSTEM + ctx, 2048),
callGrok(`Busca en X/Twitter las campanas de marketing mas creativas y virales de 2026: ${userQuery}`)
]);
res.json({ reply: claudeReply, xPulse: grokPulse });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// ── CONTENT FACTORY ──────────────────────────────────
const FACTORY_SYSTEM = `You are a content factory for maarmapa — Chilean contemporary urban artist and art intelligence voice. Generate complete editorial content packages about art, culture, blockchain and cities.
Search the web for current, real information about the topic before generating.
OUTPUT strict JSON only, no preamble, no markdown fences:
{
"title": "compelling post title",
"subtitle": "one sharp line",
"tags": ["tag1", "tag2", "tag3"],
"body": "full Substack post in markdown, 500-700 words, Spanish, Hook→Contexto→Presente→Cierre, 2-3 real references, end with open question",
"thumbnail_prompt": "detailed English prompt for Grok image generation: Square 1:1 format editorial Instagram slide. Dark background. Strict center-safe text zone with 100px margins on ALL sides. [describe specific background image at 10-15% opacity, blurred, film grain, cinematic vignette]. Typography: top-left small caps label in dark gray. Large bold condensed Bebas Neue style text left-aligned in white fitting inside margins: [MAIN HEADLINE]. Bottom left small gray subtext. Bottom right T/07 faint. ALL text inside 100px safe margins. No watermarks. Dark cinematic editorial style.",
"slides": [
{
"num": "01",
"prompt": "Square 1:1 editorial Instagram slide. [full Grok image prompt with safe margins, background, typography, film grain, specific text for this slide]"
},
{ "num": "02", "prompt": "..." },
{ "num": "03", "prompt": "..." },
{ "num": "04", "prompt": "..." },
{ "num": "05", "prompt": "..." },
{ "num": "06", "prompt": "..." },
{ "num": "07", "prompt": "..." }
],
"instagram_caption": "short punchy caption in Spanish lowercase, max 150 chars, relevant hashtags at end"
}
RULES:
- Search web first for real facts, dates, quotes
- Editorial content only — do not mix with maarmapa artworks unless topic is specifically about them
- Each slide prompt must be self-contained and complete for Grok image generation
- Slides tell a story: Hook → Context → Data → Quote → Analysis → Key Question → CTA
- Slide 07 always ends with @maarmapa.eth handle
- ONLY output valid JSON`;
async function generateImages(slides) {
if (!process.env.GROK_KEY) return slides.map(s => ({ ...s, url: null }));
const results = await Promise.allSettled(
slides.map(async (slide) => {
try {
const res = await fetch('https://api.x.ai/v1/images/generations', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.GROK_KEY}`
},
body: JSON.stringify({
model: 'grok-2-image',
prompt: slide.prompt,
n: 1,
response_format: 'url'
})
});
const data = await res.json();
return { num: slide.num, prompt: slide.prompt, url: data.data?.[0]?.url || null };
} catch(e) {
return { num: slide.num, prompt: slide.prompt, url: null };
}
})
);
return results.map(r => r.status === 'fulfilled' ? r.value : { url: null });
}
app.post('/factory', expensiveLimit, async (req, res) => {
const { topic } = req.body;
if (!topic) return res.status(400).json({ error: 'topic required' });
console.log('Factory generating for:', topic);
try {
// Step 1: Claude generates all content + prompts
const posts = await fetchSubstackRSS();
const ctx = buildSubstackContext(posts);
const raw = await runWithTools(
[{ role: 'user', content: `Generate a complete content package about: ${topic}` }],
FACTORY_SYSTEM + ctx,
4096
);
const clean = raw.replace(/```json|```/g, '').trim();
let content;
try {
content = JSON.parse(clean);
} catch(e) {
return res.status(500).json({ error: 'JSON parse failed', raw: raw.slice(0, 500) });
}
// Step 2: Grok generates thumbnail + all 7 slide images in parallel
console.log('Generating images with Grok...');
const [thumbnailResult, slidesWithImages] = await Promise.all([
// Thumbnail
(async () => {
try {
const r = await fetch('https://api.x.ai/v1/images/generations', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${process.env.GROK_KEY}` },
body: JSON.stringify({ model: 'grok-2-image', prompt: content.thumbnail_prompt, n: 1, response_format: 'url' })
});
const d = await r.json();
return d.data?.[0]?.url || null;
} catch(e) { return null; }
})(),
// 7 slides
generateImages(content.slides || [])
]);
res.json({
topic,
substack: {
title: content.title,
subtitle: content.subtitle,
tags: content.tags,
body: content.body
},
thumbnail_url: thumbnailResult,
carousel: slidesWithImages,
instagram_caption: content.instagram_caption,
generated_at: new Date().toISOString()
});
} catch (err) {
console.error('Factory error:', err);
res.status(500).json({ error: err.message });
}
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, '0.0.0.0', () => console.log(`maarmapa agent v6 on port ${PORT}`));
// ── GROK / X.AI ───────────────────────────────────────
async function callGrok(userMessage) {
try {
const res = await fetch('https://api.x.ai/v1/responses', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${process.env.GROK_KEY}` },
body: JSON.stringify({
model: 'grok-4-1-fast',
tools: [{ type: 'web_search' }, { type: 'x_search' }],
input: [
{ role: 'system', content: 'Art intelligence assistant for maarmapa — Chilean urban artist. Search X/Twitter and web for street art, urban art, Latin American art, art+blockchain news. Same language as query.' },
{ role: 'user', content: userMessage }
],
}),
});
const text = await res.text();
const data = JSON.parse(text);
const output = data.output || [];
return output.filter(b => b.type === 'message').flatMap(b => b.content || []).filter(c => c.type === 'output_text').map(c => c.text).join('')
|| output.map(b => b.content?.[0]?.text || '').join('')
|| data.error || '...';
} catch(e) {
console.error('Grok error:', e.message);
return '...';
}
}
app.post('/grok', standardLimit, async (req, res) => {
const { query } = req.body;
if (!query) return res.status(400).json({ error: 'query required' });
res.json({ reply: await callGrok(query), source: 'grok-4-1-fast + x_search' });
});
app.get('/digest/full', standardLimit, async (req, res) => {
try {
const posts = await fetchSubstackRSS();
const ctx = buildSubstackContext(posts);
const [claudeDigest, grokPulse] = await Promise.all([
runWithTools([{ role: 'user', content: 'Generate the weekly art intelligence digest. Search for the most relevant developments from the past 7 days.' }], DIGEST_SYSTEM + ctx, 2048),
callGrok('What are the most relevant conversations on X RIGHT NOW about: street art, urban art, Latin American art, art+blockchain? 4-5 key trends.')
]);
res.json({ digest: claudeDigest, xPulse: grokPulse, sources: ['Claude + web search', 'Grok + X/Twitter'] });
} catch (err) { res.status(500).json({ error: err.message }); }
});