intake/relay rewire

This commit is contained in:
serversdwn
2025-12-06 04:32:42 -05:00
parent fc85557f76
commit 4acaddfd12
4 changed files with 123 additions and 185 deletions

View File

@@ -1,3 +1,6 @@
// relay v0.3.0
// Core relay server for Lyra project
// Handles incoming chat requests and forwards them to Cortex services
import express from "express";
import dotenv from "dotenv";
import cors from "cors";
@@ -10,9 +13,8 @@ app.use(express.json());
const PORT = Number(process.env.PORT || 7078);
// core endpoints
// Cortex endpoints (only these are used now)
const CORTEX_REASON = process.env.CORTEX_REASON_URL || "http://cortex:7081/reason";
const CORTEX_INGEST = process.env.CORTEX_INGEST_URL || "http://cortex:7081/ingest";
// -----------------------------------------------------
// Helper request wrapper
@@ -27,7 +29,6 @@ async function postJSON(url, data) {
const raw = await resp.text();
let json;
// Try to parse JSON safely
try {
json = raw ? JSON.parse(raw) : null;
} catch (e) {
@@ -42,11 +43,12 @@ async function postJSON(url, data) {
}
// -----------------------------------------------------
// Shared chat handler logic
// The unified chat handler
// -----------------------------------------------------
async function handleChatRequest(session_id, user_msg) {
// 1. → Cortex.reason: the main pipeline
let reason;
// 1. → Cortex.reason (main pipeline)
try {
reason = await postJSON(CORTEX_REASON, {
session_id,
@@ -57,19 +59,13 @@ async function handleChatRequest(session_id, user_msg) {
throw new Error(`cortex_reason_failed: ${e.message}`);
}
const persona = reason.final_output || reason.persona || "(no persona text)";
// Correct persona field
const persona =
reason.persona ||
reason.final_output ||
"(no persona text)";
// 2. → Cortex.ingest (async, non-blocking)
// Cortex might still want this for separate ingestion pipeline.
postJSON(CORTEX_INGEST, {
session_id,
user_msg,
assistant_msg: persona
}).catch(e =>
console.warn("Relay → Cortex.ingest failed:", e.message)
);
// 3. Return corrected result
// Return final answer
return {
session_id,
reply: persona
@@ -84,7 +80,7 @@ app.get("/_health", (_, res) => {
});
// -----------------------------------------------------
// OPENAI-COMPATIBLE ENDPOINT (for UI & clients)
// OPENAI-COMPATIBLE ENDPOINT
// -----------------------------------------------------
app.post("/v1/chat/completions", async (req, res) => {
try {
@@ -101,7 +97,7 @@ app.post("/v1/chat/completions", async (req, res) => {
const result = await handleChatRequest(session_id, user_msg);
return res.json({
res.json({
id: `chatcmpl-${Date.now()}`,
object: "chat.completion",
created: Math.floor(Date.now() / 1000),
@@ -134,7 +130,7 @@ app.post("/v1/chat/completions", async (req, res) => {
});
// -----------------------------------------------------
// MAIN ENDPOINT (canonical Lyra UI entrance)
// MAIN ENDPOINT (Lyra-native UI)
// -----------------------------------------------------
app.post("/chat", async (req, res) => {
try {
@@ -144,7 +140,7 @@ app.post("/chat", async (req, res) => {
console.log(`Relay → received: "${user_msg}"`);
const result = await handleChatRequest(session_id, user_msg);
return res.json(result);
res.json(result);
} catch (err) {
console.error("Relay fatal:", err);