-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
461 lines (399 loc) · 14.6 KB
/
Copy pathserver.js
File metadata and controls
461 lines (399 loc) · 14.6 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
// watchtron white-box tracing. Must be required before express/http so the
// OpenTelemetry instrumentation can patch them. No-op unless WATCHTRON_OTLP_ENDPOINT is set.
require("@swantron/otel-bootstrap/register");
const express = require("express");
const { GoogleGenerativeAI } = require("@google/generative-ai");
const { syntheticMarkerMiddleware } = require("@swantron/otel-bootstrap");
const { McpServer } = require("@modelcontextprotocol/server");
const {
NodeStreamableHTTPServerTransport,
} = require("@modelcontextprotocol/node");
const { z } = require("zod");
const fs = require("fs");
const app = express();
// Stamp synthetic run ids from watchtron probes onto the server span.
app.use(syntheticMarkerMiddleware());
const port = process.env.PORT || 8080;
// Initialize Gemini
// Model options (as of May 2026):
// - gemini-3.1-flash-lite (RECOMMENDED - stable, budget-friendly free tier)
// - gemini-3.5-flash (stable, highest capability in flash family)
// - gemini-2.5-flash-lite (previous gen, still available)
// - gemini-2.5-flash (previous gen, still available)
//
// If you see "limit: 0" errors, try enabling billing (pay-as-you-go) even if you
// stay within free usage - this unlocks Tier 1 quotas.
const GEMINI_MODEL = process.env.GEMINI_MODEL || "gemini-3.1-flash-lite";
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: GEMINI_MODEL });
// Usage tracking (in-memory, resets on restart)
let usageStats = {
totalRequests: 0,
successfulRequests: 0,
quotaErrors: 0,
otherErrors: 0,
lastRequestTime: null,
model: GEMINI_MODEL,
};
// Simple in-memory recipe cache (per-instance, helps with concurrent requests)
// Note: Serverless instances are ephemeral, but this helps with burst traffic on same instance
const recipeCache = new Map();
const CACHE_TTL = 24 * 60 * 60 * 1000; // 24 hours
const MAX_CACHE_SIZE = 100; // Limit cache size
// Helper to create cache key from ingredients
const createCacheKey = (ingredients, dietaryPrefs = {}) => {
const normalized = ingredients.toLowerCase().trim().replace(/\s+/g, " ");
const prefs = Object.keys(dietaryPrefs)
.filter((k) => dietaryPrefs[k])
.sort()
.join(",");
return `${normalized}|${prefs}`;
};
console.log(`Initialized Gemini with model: ${GEMINI_MODEL}`);
app.use(express.json());
app.use(express.static("."));
// Serve sitemap.xml with correct content type
app.get("/sitemap.xml", (req, res) => {
res.set("Content-Type", "application/xml");
res.sendFile(__dirname + "/sitemap.xml");
});
// Serve robots.txt with correct content type
app.get("/robots.txt", (req, res) => {
res.set("Content-Type", "text/plain");
res.sendFile(__dirname + "/robots.txt");
});
// Health check endpoint for Cloud Run
app.get("/health", (req, res) => {
res.status(200).json({ status: "healthy", service: "chomptron" });
});
// Readiness check - verifies AI service is configured
app.get("/ready", (req, res) => {
if (!process.env.GEMINI_API_KEY) {
return res.status(503).json({
status: "not ready",
error: "GEMINI_API_KEY not configured",
});
}
res.status(200).json({
status: "ready",
service: "chomptron",
model: GEMINI_MODEL,
});
});
// Usage stats endpoint to help monitor quota
app.get("/api/usage", (req, res) => {
res.json({
...usageStats,
uptime: process.uptime(),
timestamp: new Date().toISOString(),
});
});
// Helper function to sleep for a given duration
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// Helper function to extract retry delay from error
const extractRetryDelay = (error) => {
try {
// Check if error has retry info in the message or details
const errorString = JSON.stringify(error);
const retryMatch = errorString.match(
/retryDelay["\s:]+"?(\d+(?:\.\d+)?)s/i,
);
if (retryMatch) {
return Math.ceil(parseFloat(retryMatch[1]) * 1000); // Convert to milliseconds
}
// Check error.details for retry info
if (error.details) {
for (const detail of error.details) {
if (detail["@type"] === "type.googleapis.com/google.rpc.RetryInfo") {
const delay = detail.retryDelay;
if (delay) {
// Parse duration string like "33s" or seconds number
if (typeof delay === "string") {
const match = delay.match(/(\d+(?:\.\d+)?)s/);
if (match) return Math.ceil(parseFloat(match[1]) * 1000);
}
return Math.ceil(parseFloat(delay) * 1000);
}
}
}
}
} catch {
// Fallback if parsing fails
}
return null;
};
// Helper function to check if error is a quota/rate limit error
const isQuotaError = (error) => {
const errorMessage = error.message || "";
const errorCode = error.code || error.status || "";
return (
errorCode === 429 ||
errorMessage.includes("429") ||
errorMessage.includes("quota") ||
errorMessage.includes("rate limit") ||
errorMessage.includes("Too Many Requests")
);
};
// Helper function to generate content with retry logic
const generateContentWithRetry = async (prompt, maxRetries = 3) => {
let lastError;
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
const result = await model.generateContent(prompt);
const response = await result.response;
return response.text();
} catch (error) {
lastError = error;
// If it's a quota error, extract retry delay and wait
if (isQuotaError(error) && attempt < maxRetries) {
const retryDelay =
extractRetryDelay(error) || Math.pow(2, attempt) * 1000; // Exponential backoff fallback
console.warn(
`Quota exceeded (attempt ${attempt + 1}/${maxRetries + 1}). Retrying in ${retryDelay}ms...`,
);
await sleep(retryDelay);
continue;
}
// If not a quota error or max retries reached, throw
throw error;
}
}
throw lastError;
};
// Shared by both the REST route and the MCP tool: cache lookup, prompt
// build, Gemini call with retry, cache write. Callers own their own
// usage tracking and error presentation.
async function generateRecipe(ingredients, dietaryPreferences = {}) {
const cacheKey = createCacheKey(ingredients, dietaryPreferences);
const cached = recipeCache.get(cacheKey);
if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
console.log(`Cache hit for: ${ingredients.substring(0, 50)}...`);
return { recipe: cached.recipe, cached: true };
}
const dietaryNotes = [];
if (dietaryPreferences.vegan) dietaryNotes.push("vegan");
if (dietaryPreferences.vegetarian) dietaryNotes.push("vegetarian");
if (dietaryPreferences.glutenFree) dietaryNotes.push("gluten-free");
if (dietaryPreferences.dairyFree) dietaryNotes.push("dairy-free");
if (dietaryPreferences.nutFree) dietaryNotes.push("nut-free");
if (dietaryPreferences.shellfishFree) dietaryNotes.push("shellfish-free");
if (dietaryPreferences.eggFree) dietaryNotes.push("egg-free");
if (dietaryPreferences.soyFree) dietaryNotes.push("soy-free");
const dietaryString =
dietaryNotes.length > 0
? `\n\nIMPORTANT: This recipe must be ${dietaryNotes.join(", ")}. Do not include any ingredients that violate these dietary restrictions.`
: "";
const prompt = `You are a creative chef. Create a delicious recipe using these ingredients: ${ingredients}${dietaryString}
Please provide the recipe in the following structured format:
**Recipe Name:** [Creative recipe name]
**Serving Size:** [Number] servings
**Prep Time:** [Time in minutes]
**Cook Time:** [Time in minutes]
**Total Time:** [Total time]
**Ingredients:**
- [Ingredient 1 with measurement]
- [Ingredient 2 with measurement]
- [Continue list...]
**Instructions:**
1. [Step 1]
2. [Step 2]
3. [Continue steps...]
**Tips:** [Optional cooking tips or variations]
Make the recipe practical and delicious!`;
const recipe = await generateContentWithRetry(prompt);
if (recipeCache.size >= MAX_CACHE_SIZE) {
const firstKey = recipeCache.keys().next().value;
recipeCache.delete(firstKey);
}
recipeCache.set(cacheKey, { recipe, timestamp: Date.now() });
return { recipe, cached: false };
}
app.post("/api/generate-recipe", async (req, res) => {
try {
const { ingredients, dietaryPreferences = {} } = req.body;
if (!ingredients) {
return res
.status(400)
.json({ success: false, error: "No ingredients provided" });
}
checkRestBudget(req);
// Track usage
usageStats.totalRequests++;
usageStats.lastRequestTime = new Date().toISOString();
const { recipe, cached } = await generateRecipe(
ingredients,
dietaryPreferences,
);
usageStats.successfulRequests++;
res.json({ success: true, recipe, cached });
} catch (error) {
console.error("Error:", error);
// Handle quota/rate limit errors with user-friendly messages
if (isQuotaError(error)) {
usageStats.quotaErrors++;
const retryDelay = extractRetryDelay(error);
const retrySeconds = retryDelay ? Math.ceil(retryDelay / 1000) : null;
let errorMessage = "API quota exceeded. ";
if (retrySeconds) {
errorMessage += `Please try again in ${retrySeconds} seconds. `;
}
errorMessage += `Current model: ${GEMINI_MODEL}. `;
// Provide helpful guidance based on the error
if (error.message && error.message.includes("limit: 0")) {
errorMessage +=
"⚠️ Seeing 'limit: 0'? This often means the model was removed from fully free tier. ";
errorMessage +=
"Try switching to gemini-3.1-flash-lite or enable billing (pay-as-you-go) to unlock Tier 1 quotas. ";
} else {
errorMessage += "The free tier has daily and per-minute limits. ";
if (GEMINI_MODEL !== "gemini-3.1-flash-lite") {
errorMessage +=
"Consider switching to gemini-3.1-flash-lite for better free tier limits. ";
}
errorMessage += "You can also enable billing to access higher limits. ";
}
errorMessage += "Check your quota at https://ai.dev/usage";
return res.status(429).json({
success: false,
error: errorMessage,
retryAfter: retrySeconds,
quotaExceeded: true,
model: GEMINI_MODEL,
usageStats: {
totalRequests: usageStats.totalRequests,
quotaErrors: usageStats.quotaErrors,
},
});
}
// Track other errors
usageStats.otherErrors++;
// Generic error handling
const status = error.status === 429 ? 429 : 500;
res.status(status).json({
success: false,
error: error.message || "An error occurred while generating the recipe",
});
}
});
// MCP tool exposure. Separate budget from the website's own usageStats —
// this bounds the *incremental* Gemini quota an MCP client (Claude, ChatGPT,
// etc.) can consume, independent of real chomptron.com traffic. Persisted to
// a file so a process restart on the same Cloud Run instance does not reset
// the counter; a brand-new instance still starts at zero (no shared DB).
const MCP_DAILY_LIMIT = parseInt(
process.env.MCP_DAILY_RECIPE_LIMIT || "20",
10,
);
const MCP_BUDGET_FILE =
process.env.MCP_BUDGET_FILE || "/tmp/chomptron-mcp-budget.json";
const REST_HOURLY_LIMIT = parseInt(
process.env.REST_HOURLY_RECIPE_LIMIT || "30",
10,
);
let mcpCallsToday = 0;
let mcpWindowStart = Date.now();
const restHits = new Map();
function loadMcpBudget() {
try {
const raw = JSON.parse(fs.readFileSync(MCP_BUDGET_FILE, "utf8"));
if (typeof raw.count === "number" && typeof raw.windowStart === "number") {
if (Date.now() - raw.windowStart < 24 * 60 * 60 * 1000) {
mcpCallsToday = raw.count;
mcpWindowStart = raw.windowStart;
}
}
} catch {
// First boot, or tmp wiped — start a fresh window.
}
}
function saveMcpBudget() {
try {
fs.writeFileSync(
MCP_BUDGET_FILE,
JSON.stringify({ count: mcpCallsToday, windowStart: mcpWindowStart }),
);
} catch (err) {
console.warn("Could not persist MCP budget:", err.message);
}
}
loadMcpBudget();
function checkMcpBudget() {
if (Date.now() - mcpWindowStart > 24 * 60 * 60 * 1000) {
mcpCallsToday = 0;
mcpWindowStart = Date.now();
}
if (mcpCallsToday >= MCP_DAILY_LIMIT) {
throw new Error(
`Daily MCP recipe limit (${MCP_DAILY_LIMIT}) reached — try again tomorrow, or use chomptron.com directly.`,
);
}
mcpCallsToday++;
saveMcpBudget();
}
function clientIp(req) {
const forwarded = req.headers["x-forwarded-for"];
if (typeof forwarded === "string" && forwarded.length > 0) {
return forwarded.split(",")[0].trim();
}
return req.ip || req.socket?.remoteAddress || "unknown";
}
function checkRestBudget(req) {
const ip = clientIp(req);
const now = Date.now();
const rec = restHits.get(ip);
if (!rec || now - rec.start > 60 * 60 * 1000) {
restHits.set(ip, { count: 1, start: now });
return;
}
if (rec.count >= REST_HOURLY_LIMIT) {
const err = new Error(
`Hourly recipe limit (${REST_HOURLY_LIMIT}) reached for this address — try again later.`,
);
err.status = 429;
throw err;
}
rec.count++;
}
const mcpServer = new McpServer({ name: "chomptron", version: "1.0.0" });
mcpServer.registerTool(
"generate_recipe",
{
title: "Generate a recipe",
description:
"Given ingredients on hand (and optional dietary restrictions), generate a complete recipe: name, servings, timing, ingredient list, and steps.",
inputSchema: {
ingredients: z
.string()
.describe("Ingredients on hand, comma-separated or free text"),
dietaryPreferences: z
.object({
vegan: z.boolean().optional(),
vegetarian: z.boolean().optional(),
glutenFree: z.boolean().optional(),
dairyFree: z.boolean().optional(),
nutFree: z.boolean().optional(),
shellfishFree: z.boolean().optional(),
eggFree: z.boolean().optional(),
soyFree: z.boolean().optional(),
})
.optional(),
},
annotations: { readOnlyHint: true, openWorldHint: true },
},
async ({ ingredients, dietaryPreferences }) => {
checkMcpBudget();
const { recipe } = await generateRecipe(ingredients, dietaryPreferences);
return { content: [{ type: "text", text: recipe }] };
},
);
app.post("/mcp", async (req, res) => {
const transport = new NodeStreamableHTTPServerTransport({
sessionIdGenerator: undefined,
});
await mcpServer.connect(transport);
await transport.handleRequest(req, res, req.body);
});
const server = app.listen(port, () => {
console.log(`Chomptron AI Recipe Generator running on port ${port}`);
});
module.exports = server;