From cf9aefc8463fbf194fd7f96224171065a78ac576 Mon Sep 17 00:00:00 2001 From: oiupoyt Date: Tue, 22 Sep 2026 07:38:29 +0530 Subject: [PATCH] feat(deploy): route Cloudflare Worker and Pages to Laya via public tunnel --- functions/api/ask.js | 55 +++++++++++++++--------------- functions/api/status.js | 17 +++++----- server.js | 2 +- worker.mjs | 75 +++++++++++++++++++++++++++-------------- wrangler.json | 3 ++ 5 files changed, 88 insertions(+), 64 deletions(-) diff --git a/functions/api/ask.js b/functions/api/ask.js index 6c338e7..f849d7a 100644 --- a/functions/api/ask.js +++ b/functions/api/ask.js @@ -67,36 +67,35 @@ export async function onRequestPost(context) { const cleanQuestion = question.trim().slice(0, 300); const startTime = Date.now(); - if (env.LAYA_API_URL) { - try { - const layaRes = await fetch(`${env.LAYA_API_URL}/api/ask`, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ question: cleanQuestion }), - signal: AbortSignal.timeout(6000) + const layaUrl = env.LAYA_API_URL || "https://laya-api.oiupoyt.space"; + try { + const layaRes = await fetch(`${layaUrl}/api/ask`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ question: cleanQuestion }), + signal: AbortSignal.timeout(35000) + }); + if (layaRes.ok) { + const data = await layaRes.json(); + return json({ + question: cleanQuestion, + answer: data.answer, + sentiment: data.sentiment, + noul: data.noul, + confidence: data.confidence, + probabilities: data.probabilities || {}, + aphorismKey: data.aphorismKey || "signs_yes", + latency: Date.now() - startTime, + cost: 0, + usage: { prompt_tokens: data.raw?.usage?.input_tokens ?? 38, completion_tokens: 0 }, + model: data.model || "convaiinnovations/laya-typed-decisions", + engine: "Laya System 1 (Android Tablet)", + tablet: true, + raw: data }); - if (layaRes.ok) { - const data = await layaRes.json(); - return json({ - question: cleanQuestion, - answer: data.answer, - sentiment: data.sentiment, - noul: data.noul, - confidence: data.confidence, - probabilities: data.probabilities || {}, - aphorismKey: data.aphorismKey || "signs_yes", - latency: Date.now() - startTime, - cost: 0, - usage: { prompt_tokens: data.raw?.usage?.input_tokens ?? 38, completion_tokens: 0 }, - model: data.model || "convaiinnovations/laya-typed-decisions", - engine: "Laya System 1 (Tablet via Tunnel)", - tablet: true, - raw: data - }); - } - } catch (e) { - // Fall back to OpenRouter } + } catch (e) { + // Fall back to OpenRouter } try { diff --git a/functions/api/status.js b/functions/api/status.js index 295e239..8ab3163 100644 --- a/functions/api/status.js +++ b/functions/api/status.js @@ -7,15 +7,14 @@ export async function onRequestGet(context) { let layaOnline = false; let layaData = null; - if (env.LAYA_API_URL) { - try { - const res = await fetch(`${env.LAYA_API_URL}/api/status`, { signal: AbortSignal.timeout(1500) }); - if (res.ok) { - layaData = await res.json(); - layaOnline = true; - } - } catch (e) {} - } + const layaUrl = env.LAYA_API_URL || "https://laya-api.oiupoyt.space"; + try { + const res = await fetch(`${layaUrl}/api/status`, { signal: AbortSignal.timeout(2500) }); + if (res.ok) { + layaData = await res.json(); + layaOnline = Boolean(layaData.status === "online" || layaData.ready); + } + } catch (e) {} return new Response(JSON.stringify({ status: "online", diff --git a/server.js b/server.js index cb5c2a3..71a5c88 100644 --- a/server.js +++ b/server.js @@ -104,7 +104,7 @@ app.post('/api/ask', async (req, res) => { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ question: cleanQuestion }), - signal: AbortSignal.timeout(6500) + signal: AbortSignal.timeout(35000) }); if (layaRes.ok) { diff --git a/worker.mjs b/worker.mjs index 90d60cb..193b37f 100644 --- a/worker.mjs +++ b/worker.mjs @@ -63,10 +63,30 @@ export default { // GET /api/status if (url.pathname === "/api/status") { + let layaOnline = false; + let layaData = null; + + if (env.LAYA_API_URL) { + try { + const layaRes = await fetch(`${env.LAYA_API_URL}/api/status`, { + signal: AbortSignal.timeout(2500) + }); + if (layaRes.ok) { + layaData = await layaRes.json(); + layaOnline = Boolean(layaData.status === "online" || layaData.ready); + } + } catch (e) { + layaOnline = false; + } + } + return json({ status: "online", - model: MODEL, - engine: "TypeSafe Jev (Cloudflare Edge Worker)", + model: layaOnline ? (layaData?.model || "convaiinnovations/laya-typed-decisions") : MODEL, + engine: layaOnline ? "Laya System 1 Decision Model (Android Tablet)" : "TypeSafe Jev (Cloudflare Edge Worker)", + tablet: layaOnline, + layaUrl: env.LAYA_API_URL || null, + layaInfo: layaData, hasKey: Boolean(resolveApiKey(env)), keySource: env.OPENROUTER_API_KEY ? "env" : "repo" }); @@ -74,11 +94,6 @@ export default { // POST /api/ask if (url.pathname === "/api/ask" && request.method === "POST") { - const apiKey = resolveApiKey(env); - if (!apiKey) { - return json({ error: "Oracle is not configured. Set the OPENROUTER_API_KEY secret." }, 503); - } - let body; try { body = await request.json(); @@ -94,39 +109,47 @@ export default { const cleanQuestion = question.trim().slice(0, 300); const startTime = Date.now(); - // Check if tablet Laya URL is configured in Worker environment + // 1. Primary Engine: Tablet Laya System 1 if configured in environment if (env.LAYA_API_URL) { try { const layaRes = await fetch(`${env.LAYA_API_URL}/api/ask`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ question: cleanQuestion }), - signal: AbortSignal.timeout(6000) + signal: AbortSignal.timeout(35000) }); if (layaRes.ok) { const data = await layaRes.json(); - return json({ - question: cleanQuestion, - answer: data.answer, - sentiment: data.sentiment, - noul: data.noul, - confidence: data.confidence, - probabilities: data.probabilities || {}, - aphorismKey: data.aphorismKey || "signs_yes", - latency: Date.now() - startTime, - cost: 0, - usage: { prompt_tokens: data.raw?.usage?.input_tokens ?? 38, completion_tokens: 0 }, - model: data.model || "convaiinnovations/laya-typed-decisions", - engine: "Laya System 1 (Tablet via Tunnel)", - tablet: true, - raw: data - }); + if (data && data.answer) { + return json({ + question: cleanQuestion, + answer: data.answer, + sentiment: data.sentiment, + noul: data.noul, + confidence: data.confidence, + probabilities: data.probabilities || {}, + aphorismKey: data.aphorismKey || "signs_yes", + latency: Date.now() - startTime, + cost: 0, + usage: { prompt_tokens: data.raw?.usage?.input_tokens ?? 38, completion_tokens: 0 }, + model: data.model || "convaiinnovations/laya-typed-decisions", + engine: "Laya System 1 (Android Tablet)", + tablet: true, + raw: data + }); + } } } catch (layaErr) { - // Fall back to OpenRouter + // Fall back to OpenRouter Jev } } + // 2. Fallback Engine: OpenRouter Jev + const apiKey = resolveApiKey(env); + if (!apiKey) { + return json({ error: "Oracle is not configured. Set the OPENROUTER_API_KEY secret." }, 503); + } + try { const payload = { model: MODEL, diff --git a/wrangler.json b/wrangler.json index 13a7c61..5dcd40c 100644 --- a/wrangler.json +++ b/wrangler.json @@ -5,5 +5,8 @@ "assets": { "directory": "./public", "binding": "ASSETS" + }, + "vars": { + "LAYA_API_URL": "https://laya-api.oiupoyt.space" } }