feat: wire TradingGraph to use settings for model, temperature, and risk config

This commit is contained in:
2026-05-16 21:37:27 +02:00
parent ae45071973
commit 2ab55060f3
5 changed files with 50 additions and 16 deletions
+2 -2
View File
@@ -89,13 +89,13 @@ export function enrichExecutionPlan(decision: TradingDecision, input: any): Trad
}
// Optional LLM verification step: review computed executionPlan and suggest adjustments
export async function verifyExecutionPlanWithLLM(decision: TradingDecision, input: any): Promise<TradingDecision> {
export async function verifyExecutionPlanWithLLM(decision: TradingDecision, input: any, model?: string): Promise<TradingDecision> {
try {
const apiKey = process.env.OPENROUTER_API_KEY;
if (!apiKey) return decision;
const { OpenRouterClient } = await import("./openrouter");
const client = new OpenRouterClient(apiKey);
const client = new OpenRouterClient(apiKey, { defaultModel: model });
const plan = decision.executionPlan || {};
const prices: number[] = input?.technicalData?.prices || [];
+4 -1
View File
@@ -36,7 +36,8 @@ export class OpenRouterClient {
async createChatCompletion(
messages: Message[],
model?: string
model?: string,
options?: { temperature?: number; max_tokens?: number }
): Promise<unknown> {
const response = await fetch(`${this.baseURL}/chat/completions`, {
method: "POST",
@@ -49,6 +50,8 @@ export class OpenRouterClient {
body: JSON.stringify({
model: model ?? this.defaultModel,
messages,
...(options?.temperature != null && { temperature: options.temperature }),
...(options?.max_tokens != null && { max_tokens: options.max_tokens }),
}),
});
+6 -8
View File
@@ -2,8 +2,7 @@ import pkg from "bullmq";
const { Queue, Worker } = pkg as any;
import IORedis from "ioredis";
import { fetchAccount, fetchRecentCloses } from "./alpacaClient";
import { OpenRouterClient } from "./openrouter";
import { TradingGraph } from "../agents/tradingGraph";
import { buildTradingGraph, getTradingConfig } from "./tradingConfig.server";
import { db } from "./db.server";
const REDIS_URL = process.env.REDIS_URL;
@@ -41,8 +40,8 @@ if (REDIS_URL) {
return mockDecision;
}
const client = new OpenRouterClient(apiKey);
const graph = new TradingGraph(client);
const { graph, config } = await buildTradingGraph(apiKey);
console.log("[queue] Trading config:", config);
// Fetch latest Alpaca account and prices; abort job if unavailable so work runs on fresh data
try {
const account = await fetchAccount();
@@ -64,7 +63,7 @@ if (REDIS_URL) {
decision = enrichExecutionPlan(decision, input);
if (process.env.OPENROUTER_API_KEY) {
try {
decision = await verifyExecutionPlanWithLLM(decision, input);
decision = await verifyExecutionPlanWithLLM(decision, input, config.model);
} catch (e) {
console.warn("[queue] LLM verification failed:", e);
}
@@ -191,8 +190,7 @@ if (REDIS_URL) {
});
continue;
}
const client = new OpenRouterClient(process.env.OPENROUTER_API_KEY as string);
const graph = new TradingGraph(client);
const { graph, config } = await buildTradingGraph(process.env.OPENROUTER_API_KEY as string);
// Fetch latest Alpaca account and prices; abort job if unavailable so work runs on fresh data
try {
const account = await fetchAccount();
@@ -215,7 +213,7 @@ if (REDIS_URL) {
decision = enrichExecutionPlan(decision, job.input);
if (process.env.OPENROUTER_API_KEY) {
try {
decision = await verifyExecutionPlanWithLLM(decision, job.input);
decision = await verifyExecutionPlanWithLLM(decision, job.input, config.model);
} catch (e) {
console.warn("[inproc queue] LLM verification failed:", e);
}
+34
View File
@@ -0,0 +1,34 @@
import { settingsService } from "./settings.server";
import { OpenRouterClient } from "./openrouter";
import { TradingGraph } from "../agents/tradingGraph";
export interface TradingConfig {
model: string;
temperature: number;
maxDebateRounds: number;
}
const DEFAULT_CONFIG: TradingConfig = {
model: "openai/gpt-oss-120b:free",
temperature: 0.7,
maxDebateRounds: 3,
};
export async function getTradingConfig(): Promise<TradingConfig> {
try {
await settingsService.init();
const model = (await settingsService.get("llm.model")) ?? DEFAULT_CONFIG.model;
const temperature = (await settingsService.get("llm.temperature")) ?? DEFAULT_CONFIG.temperature;
const maxDebateRounds = (await settingsService.get("llm.maxDebateRounds")) ?? DEFAULT_CONFIG.maxDebateRounds;
return { model, temperature, maxDebateRounds };
} catch {
return DEFAULT_CONFIG;
}
}
export async function buildTradingGraph(apiKey: string): Promise<{ graph: TradingGraph; client: OpenRouterClient; config: TradingConfig }> {
const config = await getTradingConfig();
const client = new OpenRouterClient(apiKey, { defaultModel: config.model });
const graph = new TradingGraph(client, config.model);
return { graph, client, config };
}