API: support background analyze - enqueue TradingGraph and persist decision to DB when body.background is true\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { OpenRouterClient } from "../../lib/openrouter";
|
||||
import { TradingGraph } from "../../agents/tradingGraph";
|
||||
import { db } from "../../lib/db.server";
|
||||
|
||||
export async function action({ request }: { request: Request }) {
|
||||
console.log("[analyze] Request received:", request.method, request.url);
|
||||
@@ -72,6 +73,37 @@ export async function action({ request }: { request: Request }) {
|
||||
|
||||
try {
|
||||
console.log("[analyze] Running trading graph...");
|
||||
|
||||
if (body.background) {
|
||||
// Run in background: start async propagation and return 202 immediately
|
||||
(async () => {
|
||||
try {
|
||||
const decision = await graph.propagate(ticker, input);
|
||||
console.log("[analyze] Background decision received:", JSON.stringify(decision));
|
||||
// persist last decision to DB
|
||||
await db.stock.upsert({
|
||||
where: { ticker },
|
||||
create: {
|
||||
ticker,
|
||||
lastDecision: decision.action as string,
|
||||
lastExplanation: (decision as any).reasoning || null,
|
||||
lastExecutionPlan: decision.executionPlan ? JSON.stringify(decision.executionPlan) : null,
|
||||
},
|
||||
update: {
|
||||
lastDecision: decision.action as string,
|
||||
lastExplanation: (decision as any).reasoning || null,
|
||||
lastExecutionPlan: decision.executionPlan ? JSON.stringify(decision.executionPlan) : null,
|
||||
},
|
||||
});
|
||||
console.log("[analyze] Background decision saved to DB for", ticker);
|
||||
} catch (bgErr) {
|
||||
console.error("[analyze] Background error:", bgErr);
|
||||
}
|
||||
})();
|
||||
|
||||
return Response.json({ status: "queued" }, { status: 202 });
|
||||
}
|
||||
|
||||
const decision = await graph.propagate(ticker, input);
|
||||
console.log("[analyze] Decision received:", JSON.stringify(decision));
|
||||
return Response.json(decision);
|
||||
|
||||
Reference in New Issue
Block a user