Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions backend/serveAgentSessionsApi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ import {
querySessionCostDetail,
querySessionCostOptions,
} from "./cost-analysis/cost-overview-2-query.mjs";
import {
buildDigitalEmployeeOverview,
buildDigitalEmployeeProfile,
} from "./digital-employee/digital-employee-service.mjs";

import { queryOtelOverviewData } from "./otel-metrics/otel-overview-query.mjs";
const port = Number(process.env.PORT ?? 8787);

function sendJson(res, status, body) {
Expand All @@ -43,6 +48,22 @@ const server = http.createServer(async (req, res) => {
return;
}

if (url.startsWith("/api/otel-overview")) {
try {
const u = new URL(url, "http://127.0.0.1");
const hours = Number(u.searchParams.get("hours") ?? "1");
const granularityMinutes = Number(u.searchParams.get("granularityMinutes") ?? "1");
const startTime = u.searchParams.get("startTime");
const endTime = u.searchParams.get("endTime");
const data = await queryOtelOverviewData({ hours, granularityMinutes, startTime, endTime });
sendJson(res, 200, data);
} catch (e) {
const msg = e instanceof Error ? e.message : String(e);
sendJson(res, 500, { error: msg });
}
return;
}

if (url.startsWith("/api/cost-overview")) {
try {
const u = new URL(url, "http://127.0.0.1");
Expand Down Expand Up @@ -103,6 +124,48 @@ const server = http.createServer(async (req, res) => {
return;
}

if (url.startsWith("/api/digital-employees/overview")) {
try {
const u = new URL(url, "http://127.0.0.1");
const daysParam = u.searchParams.get("days");
const hoursParam = u.searchParams.get("hours");
const data = await buildDigitalEmployeeOverview(daysParam ?? "7", hoursParam);
sendJson(res, 200, data);
} catch (e) {
const msg = e instanceof Error ? e.message : String(e);
sendJson(res, 500, { error: msg });
}
return;
}

if (url.startsWith("/api/digital-employees/profile")) {
try {
const u = new URL(url, "http://127.0.0.1");
const agentName = u.searchParams.get("agentName");
const daysParam = u.searchParams.get("days");
const hoursParam = u.searchParams.get("hours");
const sessionKeyParam = u.searchParams.get("sessionKey") || u.searchParams.get("session_key");
if (!agentName || !String(agentName).trim()) {
sendJson(res, 400, { error: "缺少 agentName" });
return;
}
const data = await buildDigitalEmployeeProfile(agentName, daysParam ?? "7", hoursParam, sessionKeyParam);
if (data.error === "missing_agent") {
sendJson(res, 400, { error: data.message || "缺少 agentName" });
return;
}
if (data.error === "not_found") {
sendJson(res, 404, { error: data.message || "未找到", source: data.source });
return;
}
sendJson(res, 200, data);
} catch (e) {
const msg = e instanceof Error ? e.message : String(e);
sendJson(res, 500, { error: msg });
}
return;
}

if (url.startsWith("/api/agent-sessions-logs-search")) {
try {
const u = new URL(url, "http://127.0.0.1");
Expand Down Expand Up @@ -269,6 +332,9 @@ const server = http.createServer(async (req, res) => {
});

server.listen(port, "0.0.0.0", () => {
console.log(`[agent-sessions] http://127.0.0.1:${port}/api/digital-employees/overview`);
console.log(`[agent-sessions] http://127.0.0.1:${port}/api/digital-employees/profile?agentName=`);
console.log(`[agent-sessions] http://127.0.0.1:${port}/api/otel-overview`);
console.log(`[agent-sessions] http://127.0.0.1:${port}/api/cost-overview`);
console.log(`[agent-sessions] http://127.0.0.1:${port}/api/agent-cost-list?startDay=&endDay=`);
console.log(`[agent-sessions] http://127.0.0.1:${port}/api/llm-cost-detail?startDay=&endDay=`);
Expand Down
Loading