@ganglion/xacpx-relay 0.9.6 → 0.9.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -907,11 +907,14 @@ function createApp(deps) {
907
907
  app.get("/api/active-turns", (c) => {
908
908
  const account = c.get("account");
909
909
  const turns = [];
910
+ const usage = [];
910
911
  for (const inst of deps.instances.listByAccount(account.id)) {
911
912
  for (const t of deps.activeTurns?.(inst.id) ?? [])
912
913
  turns.push(t);
914
+ for (const u of deps.sessionUsage?.(inst.id) ?? [])
915
+ usage.push(u);
913
916
  }
914
- return c.json({ turns });
917
+ return c.json({ turns, usage });
915
918
  });
916
919
  app.get("/api/instances/:id/sessions/:alias/messages", (c) => {
917
920
  const account = c.get("account");
@@ -1048,6 +1051,17 @@ async function createRelayRuntime(dbPath, options = {}) {
1048
1051
  const webGateway = new WebGateway;
1049
1052
  const turnBuffers = new Map;
1050
1053
  const key = (instanceId, alias) => `${instanceId}\x00${alias}`;
1054
+ const sessionUsage = new Map;
1055
+ const listSessionUsage = (instanceId) => {
1056
+ const prefix = `${instanceId}\x00`;
1057
+ const out = [];
1058
+ for (const [k, u] of sessionUsage) {
1059
+ if (!k.startsWith(prefix))
1060
+ continue;
1061
+ out.push({ instanceId, sessionAlias: k.slice(prefix.length), used: u.used, size: u.size, ...u.cost ? { cost: u.cost } : {}, ...u.breakdown ? { breakdown: u.breakdown } : {} });
1062
+ }
1063
+ return out;
1064
+ };
1051
1065
  const listActiveTurns = (instanceId) => {
1052
1066
  const prefix = `${instanceId}\x00`;
1053
1067
  const out = [];
@@ -1098,6 +1112,9 @@ async function createRelayRuntime(dbPath, options = {}) {
1098
1112
  for (const k of turnBuffers.keys())
1099
1113
  if (k.startsWith(prefix))
1100
1114
  turnBuffers.delete(k);
1115
+ for (const k of sessionUsage.keys())
1116
+ if (k.startsWith(prefix))
1117
+ sessionUsage.delete(k);
1101
1118
  }
1102
1119
  webGateway.broadcast(accountId, { kind: "instance-status", instanceId, online });
1103
1120
  },
@@ -1140,6 +1157,8 @@ async function createRelayRuntime(dbPath, options = {}) {
1140
1157
  const structured = hasStructured ? { toolSteps: steps, ...hasReasoning ? { reasoning: a.reasoning } : {}, ...a.parts.length ? { parts: a.parts } : {} } : undefined;
1141
1158
  messages.append(instanceId, event.sessionAlias, "out", a.text, structured);
1142
1159
  }
1160
+ } else if (event.type === "turn-usage") {
1161
+ sessionUsage.set(key(instanceId, event.sessionAlias), { used: event.used, size: event.size, ...event.cost ? { cost: event.cost } : {}, ...event.breakdown ? { breakdown: event.breakdown } : {} });
1143
1162
  } else if (event.type === "session-history") {
1144
1163
  const existing = messages.listBySession(accountId, instanceId, event.sessionAlias, { limit: 1 });
1145
1164
  if (existing.messages.length === 0) {
@@ -1162,6 +1181,7 @@ async function createRelayRuntime(dbPath, options = {}) {
1162
1181
  historyRetentionDays: options.historyRetentionDays ?? 30,
1163
1182
  maxMessagesPerSession: MAX_MESSAGES_PER_SESSION,
1164
1183
  activeTurns: listActiveTurns,
1184
+ sessionUsage: listSessionUsage,
1165
1185
  trustProxy: options.trustProxy,
1166
1186
  checkUpdate: createRelayUpdateChecker({ current: readRelayVersion() })
1167
1187
  });
@@ -1,5 +1,5 @@
1
1
  import { Hono } from "hono";
2
- import { type LiveTurnSnapshotDto } from "@ganglion/xacpx-relay-protocol";
2
+ import { type LiveTurnSnapshotDto, type SessionUsageSnapshotDto } from "@ganglion/xacpx-relay-protocol";
3
3
  import type { AccountRow, AccountStore } from "../stores/accounts.js";
4
4
  import type { InstanceStore } from "../stores/instances.js";
5
5
  import type { MessageStore } from "../stores/messages.js";
@@ -15,6 +15,8 @@ export interface AppDeps {
15
15
  messages: MessageStore;
16
16
  /** Snapshot the in-flight turns for an instance (for the active-turns endpoint). */
17
17
  activeTurns?: (instanceId: string) => LiveTurnSnapshotDto[];
18
+ /** Snapshot the latest per-session context-usage for an instance (for the active-turns endpoint). */
19
+ sessionUsage?: (instanceId: string) => SessionUsageSnapshotDto[];
18
20
  webRoot?: string;
19
21
  sessionTtlMs?: number;
20
22
  pairingTtlMs?: number;