mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 21:41:36 +00:00
Co-authored-by: Josh Lehman <josh@martian.engineering>
This commit is contained in:
committed by
GitHub
parent
5d37b204c0
commit
990413534a
@@ -17,7 +17,7 @@ Docs: https://docs.openclaw.ai
|
|||||||
- Docs/Mermaid: remove hardcoded Mermaid init theme blocks from four docs diagrams so dark mode inherits readable theme defaults. (#15157) Thanks @heytulsiprasad.
|
- Docs/Mermaid: remove hardcoded Mermaid init theme blocks from four docs diagrams so dark mode inherits readable theme defaults. (#15157) Thanks @heytulsiprasad.
|
||||||
- Outbound/Threading: pass `replyTo` and `threadId` from `message send` tool actions through the core outbound send path to channel adapters, preserving thread/reply routing. (#14948) Thanks @mcaxtr.
|
- Outbound/Threading: pass `replyTo` and `threadId` from `message send` tool actions through the core outbound send path to channel adapters, preserving thread/reply routing. (#14948) Thanks @mcaxtr.
|
||||||
- Sessions/Agents: pass `agentId` when resolving existing transcript paths in reply runs so non-default agents and heartbeat/chat handlers no longer fail with `Session file path must be within sessions directory`. (#15141) Thanks @Goldenmonstew.
|
- Sessions/Agents: pass `agentId` when resolving existing transcript paths in reply runs so non-default agents and heartbeat/chat handlers no longer fail with `Session file path must be within sessions directory`. (#15141) Thanks @Goldenmonstew.
|
||||||
- Status/Sessions: stop clamping derived `totalTokens` to context-window size, keep prompt-token snapshots wired through session accounting, and surface context usage as unknown when fresh snapshot data is missing to avoid false 100% reports. (#15114) Thanks @echoVic.
|
- Sessions/Agents: pass `agentId` through status and usage transcript-resolution paths (auto-reply, gateway usage APIs, and session cost/log loaders) so non-default agents can resolve absolute session files without path-validation failures. (#15103) Thanks @jalehman.
|
||||||
|
|
||||||
## 2026.2.12
|
## 2026.2.12
|
||||||
|
|
||||||
@@ -141,7 +141,6 @@ Docs: https://docs.openclaw.ai
|
|||||||
- Sessions: prune stale entries, cap session store size, rotate large stores, accept duration/size thresholds, default to warn-only maintenance, and prune cron run sessions after retention windows. (#13083) Thanks @skyfallsin, @Glucksberg, @gumadeiras.
|
- Sessions: prune stale entries, cap session store size, rotate large stores, accept duration/size thresholds, default to warn-only maintenance, and prune cron run sessions after retention windows. (#13083) Thanks @skyfallsin, @Glucksberg, @gumadeiras.
|
||||||
- CI: Implement pipeline and workflow order. Thanks @quotentiroler.
|
- CI: Implement pipeline and workflow order. Thanks @quotentiroler.
|
||||||
- WhatsApp: preserve original filenames for inbound documents. (#12691) Thanks @akramcodez.
|
- WhatsApp: preserve original filenames for inbound documents. (#12691) Thanks @akramcodez.
|
||||||
- Feishu: enforce DM `dmPolicy`/pairing gating and sender allow checks for inbound DMs. (#14876) Thanks @coygeek.
|
|
||||||
- Telegram: harden quote parsing; preserve quote context; avoid QUOTE_TEXT_INVALID; avoid nested reply quote misclassification. (#12156) Thanks @rybnikov.
|
- Telegram: harden quote parsing; preserve quote context; avoid QUOTE_TEXT_INVALID; avoid nested reply quote misclassification. (#12156) Thanks @rybnikov.
|
||||||
- Telegram: recover proactive sends when stale topic thread IDs are used by retrying without `message_thread_id`. (#11620)
|
- Telegram: recover proactive sends when stale topic thread IDs are used by retrying without `message_thread_id`. (#11620)
|
||||||
- Discord: auto-create forum/media thread posts on send, with chunked follow-up replies and media handling for forum sends. (#12380) Thanks @magendary, @thewilloftheshadow.
|
- Discord: auto-create forum/media thread posts on send, with chunked follow-up replies and media handling for forum sends. (#12380) Thanks @magendary, @thewilloftheshadow.
|
||||||
|
|||||||
@@ -436,6 +436,7 @@ export function createSessionStatusTool(opts?: {
|
|||||||
...agentDefaults,
|
...agentDefaults,
|
||||||
model: agentModel,
|
model: agentModel,
|
||||||
},
|
},
|
||||||
|
agentId,
|
||||||
sessionEntry: resolved.entry,
|
sessionEntry: resolved.entry,
|
||||||
sessionKey: resolved.key,
|
sessionKey: resolved.key,
|
||||||
sessionStorePath: storePath,
|
sessionStorePath: storePath,
|
||||||
|
|||||||
@@ -167,6 +167,7 @@ export const handleUsageCommand: CommandHandler = async (params, allowTextComman
|
|||||||
sessionEntry: params.sessionEntry,
|
sessionEntry: params.sessionEntry,
|
||||||
sessionFile: params.sessionEntry?.sessionFile,
|
sessionFile: params.sessionEntry?.sessionFile,
|
||||||
config: params.cfg,
|
config: params.cfg,
|
||||||
|
agentId: params.agentId,
|
||||||
});
|
});
|
||||||
const summary = await loadCostUsageSummary({ days: 30, config: params.cfg });
|
const summary = await loadCostUsageSummary({ days: 30, config: params.cfg });
|
||||||
|
|
||||||
|
|||||||
@@ -224,6 +224,7 @@ export async function buildStatusReply(params: {
|
|||||||
verboseDefault: agentDefaults.verboseDefault,
|
verboseDefault: agentDefaults.verboseDefault,
|
||||||
elevatedDefault: agentDefaults.elevatedDefault,
|
elevatedDefault: agentDefaults.elevatedDefault,
|
||||||
},
|
},
|
||||||
|
agentId: statusAgentId,
|
||||||
sessionEntry,
|
sessionEntry,
|
||||||
sessionKey,
|
sessionKey,
|
||||||
sessionScope,
|
sessionScope,
|
||||||
|
|||||||
@@ -55,12 +55,13 @@ export type SessionInitResult = {
|
|||||||
|
|
||||||
function forkSessionFromParent(params: {
|
function forkSessionFromParent(params: {
|
||||||
parentEntry: SessionEntry;
|
parentEntry: SessionEntry;
|
||||||
|
agentId: string;
|
||||||
sessionsDir: string;
|
sessionsDir: string;
|
||||||
}): { sessionId: string; sessionFile: string } | null {
|
}): { sessionId: string; sessionFile: string } | null {
|
||||||
const parentSessionFile = resolveSessionFilePath(
|
const parentSessionFile = resolveSessionFilePath(
|
||||||
params.parentEntry.sessionId,
|
params.parentEntry.sessionId,
|
||||||
params.parentEntry,
|
params.parentEntry,
|
||||||
{ sessionsDir: params.sessionsDir },
|
{ agentId: params.agentId, sessionsDir: params.sessionsDir },
|
||||||
);
|
);
|
||||||
if (!parentSessionFile || !fs.existsSync(parentSessionFile)) {
|
if (!parentSessionFile || !fs.existsSync(parentSessionFile)) {
|
||||||
return null;
|
return null;
|
||||||
@@ -225,11 +226,7 @@ export async function initSessionState(params: {
|
|||||||
? evaluateSessionFreshness({ updatedAt: entry.updatedAt, now, policy: resetPolicy }).fresh
|
? evaluateSessionFreshness({ updatedAt: entry.updatedAt, now, policy: resetPolicy }).fresh
|
||||||
: false;
|
: false;
|
||||||
|
|
||||||
// When this is the first user message in a thread, the session entry may already
|
if (!isNewSession && freshEntry) {
|
||||||
// exist (created by recordInboundSession in prepare.ts), but we should still treat
|
|
||||||
// it as a new session so that thread context (history/starter/fork) is applied.
|
|
||||||
const forceNewForThread = Boolean(ctx.IsFirstThreadTurn) && !resetTriggered;
|
|
||||||
if (!isNewSession && freshEntry && !forceNewForThread) {
|
|
||||||
sessionId = entry.sessionId;
|
sessionId = entry.sessionId;
|
||||||
systemSent = entry.systemSent ?? false;
|
systemSent = entry.systemSent ?? false;
|
||||||
abortedLastRun = entry.abortedLastRun ?? false;
|
abortedLastRun = entry.abortedLastRun ?? false;
|
||||||
@@ -335,6 +332,7 @@ export async function initSessionState(params: {
|
|||||||
);
|
);
|
||||||
const forked = forkSessionFromParent({
|
const forked = forkSessionFromParent({
|
||||||
parentEntry: sessionStore[parentSessionKey],
|
parentEntry: sessionStore[parentSessionKey],
|
||||||
|
agentId,
|
||||||
sessionsDir: path.dirname(storePath),
|
sessionsDir: path.dirname(storePath),
|
||||||
});
|
});
|
||||||
if (forked) {
|
if (forked) {
|
||||||
@@ -358,7 +356,6 @@ export async function initSessionState(params: {
|
|||||||
// Clear stale token metrics from previous session so /status doesn't
|
// Clear stale token metrics from previous session so /status doesn't
|
||||||
// display the old session's context usage after /new or /reset.
|
// display the old session's context usage after /new or /reset.
|
||||||
sessionEntry.totalTokens = undefined;
|
sessionEntry.totalTokens = undefined;
|
||||||
sessionEntry.totalTokensFresh = false;
|
|
||||||
sessionEntry.inputTokens = undefined;
|
sessionEntry.inputTokens = undefined;
|
||||||
sessionEntry.outputTokens = undefined;
|
sessionEntry.outputTokens = undefined;
|
||||||
sessionEntry.contextTokens = undefined;
|
sessionEntry.contextTokens = undefined;
|
||||||
|
|||||||
@@ -258,25 +258,6 @@ describe("buildStatusMessage", () => {
|
|||||||
expect(normalized).toContain("Queue: collect");
|
expect(normalized).toContain("Queue: collect");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("treats stale cached totals as unknown context usage", () => {
|
|
||||||
const text = buildStatusMessage({
|
|
||||||
agent: { model: "anthropic/claude-opus-4-5", contextTokens: 32_000 },
|
|
||||||
sessionEntry: {
|
|
||||||
sessionId: "stale-1",
|
|
||||||
updatedAt: 0,
|
|
||||||
totalTokens: 12_345,
|
|
||||||
totalTokensFresh: false,
|
|
||||||
contextTokens: 32_000,
|
|
||||||
},
|
|
||||||
sessionKey: "agent:main:main",
|
|
||||||
sessionScope: "per-sender",
|
|
||||||
queue: { mode: "collect", depth: 0 },
|
|
||||||
modelAuth: "api-key",
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(normalizeTestText(text)).toContain("Context: ?/32k");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("includes group activation for group sessions", () => {
|
it("includes group activation for group sessions", () => {
|
||||||
const text = buildStatusMessage({
|
const text = buildStatusMessage({
|
||||||
agent: {},
|
agent: {},
|
||||||
@@ -487,6 +468,69 @@ describe("buildStatusMessage", () => {
|
|||||||
{ prefix: "openclaw-status-" },
|
{ prefix: "openclaw-status-" },
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("reads transcript usage using explicit agentId when sessionKey is missing", async () => {
|
||||||
|
await withTempHome(
|
||||||
|
async (dir) => {
|
||||||
|
vi.resetModules();
|
||||||
|
const { buildStatusMessage: buildStatusMessageDynamic } = await import("./status.js");
|
||||||
|
|
||||||
|
const sessionId = "sess-worker2";
|
||||||
|
const logPath = path.join(
|
||||||
|
dir,
|
||||||
|
".openclaw",
|
||||||
|
"agents",
|
||||||
|
"worker2",
|
||||||
|
"sessions",
|
||||||
|
`${sessionId}.jsonl`,
|
||||||
|
);
|
||||||
|
fs.mkdirSync(path.dirname(logPath), { recursive: true });
|
||||||
|
|
||||||
|
fs.writeFileSync(
|
||||||
|
logPath,
|
||||||
|
[
|
||||||
|
JSON.stringify({
|
||||||
|
type: "message",
|
||||||
|
message: {
|
||||||
|
role: "assistant",
|
||||||
|
model: "claude-opus-4-5",
|
||||||
|
usage: {
|
||||||
|
input: 2,
|
||||||
|
output: 3,
|
||||||
|
cacheRead: 1200,
|
||||||
|
cacheWrite: 0,
|
||||||
|
totalTokens: 1205,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
].join("\n"),
|
||||||
|
"utf-8",
|
||||||
|
);
|
||||||
|
|
||||||
|
const text = buildStatusMessageDynamic({
|
||||||
|
agent: {
|
||||||
|
model: "anthropic/claude-opus-4-5",
|
||||||
|
contextTokens: 32_000,
|
||||||
|
},
|
||||||
|
agentId: "worker2",
|
||||||
|
sessionEntry: {
|
||||||
|
sessionId,
|
||||||
|
updatedAt: 0,
|
||||||
|
totalTokens: 5,
|
||||||
|
contextTokens: 32_000,
|
||||||
|
},
|
||||||
|
// Intentionally omitted: sessionKey
|
||||||
|
sessionScope: "per-sender",
|
||||||
|
queue: { mode: "collect", depth: 0 },
|
||||||
|
includeTranscriptUsage: true,
|
||||||
|
modelAuth: "api-key",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(normalizeTestText(text)).toContain("Context: 1.2k/32k");
|
||||||
|
},
|
||||||
|
{ prefix: "openclaw-status-" },
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("buildCommandsMessage", () => {
|
describe("buildCommandsMessage", () => {
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import { resolveSandboxRuntimeStatus } from "../agents/sandbox.js";
|
|||||||
import { derivePromptTokens, normalizeUsage, type UsageLike } from "../agents/usage.js";
|
import { derivePromptTokens, normalizeUsage, type UsageLike } from "../agents/usage.js";
|
||||||
import {
|
import {
|
||||||
resolveMainSessionKey,
|
resolveMainSessionKey,
|
||||||
resolveFreshSessionTotalTokens,
|
|
||||||
resolveSessionFilePath,
|
resolveSessionFilePath,
|
||||||
resolveSessionFilePathOptions,
|
resolveSessionFilePathOptions,
|
||||||
type SessionEntry,
|
type SessionEntry,
|
||||||
@@ -59,6 +58,7 @@ type QueueStatus = {
|
|||||||
type StatusArgs = {
|
type StatusArgs = {
|
||||||
config?: OpenClawConfig;
|
config?: OpenClawConfig;
|
||||||
agent: AgentConfig;
|
agent: AgentConfig;
|
||||||
|
agentId?: string;
|
||||||
sessionEntry?: SessionEntry;
|
sessionEntry?: SessionEntry;
|
||||||
sessionKey?: string;
|
sessionKey?: string;
|
||||||
sessionScope?: SessionScope;
|
sessionScope?: SessionScope;
|
||||||
@@ -169,6 +169,7 @@ const formatQueueDetails = (queue?: QueueStatus) => {
|
|||||||
const readUsageFromSessionLog = (
|
const readUsageFromSessionLog = (
|
||||||
sessionId?: string,
|
sessionId?: string,
|
||||||
sessionEntry?: SessionEntry,
|
sessionEntry?: SessionEntry,
|
||||||
|
agentId?: string,
|
||||||
sessionKey?: string,
|
sessionKey?: string,
|
||||||
storePath?: string,
|
storePath?: string,
|
||||||
):
|
):
|
||||||
@@ -186,11 +187,12 @@ const readUsageFromSessionLog = (
|
|||||||
}
|
}
|
||||||
let logPath: string;
|
let logPath: string;
|
||||||
try {
|
try {
|
||||||
const agentId = sessionKey ? resolveAgentIdFromSessionKey(sessionKey) : undefined;
|
const resolvedAgentId =
|
||||||
|
agentId ?? (sessionKey ? resolveAgentIdFromSessionKey(sessionKey) : undefined);
|
||||||
logPath = resolveSessionFilePath(
|
logPath = resolveSessionFilePath(
|
||||||
sessionId,
|
sessionId,
|
||||||
sessionEntry,
|
sessionEntry,
|
||||||
resolveSessionFilePathOptions({ agentId, storePath }),
|
resolveSessionFilePathOptions({ agentId: resolvedAgentId, storePath }),
|
||||||
);
|
);
|
||||||
} catch {
|
} catch {
|
||||||
return undefined;
|
return undefined;
|
||||||
@@ -344,7 +346,7 @@ export function buildStatusMessage(args: StatusArgs): string {
|
|||||||
|
|
||||||
let inputTokens = entry?.inputTokens;
|
let inputTokens = entry?.inputTokens;
|
||||||
let outputTokens = entry?.outputTokens;
|
let outputTokens = entry?.outputTokens;
|
||||||
let totalTokens = resolveFreshSessionTotalTokens(entry);
|
let totalTokens = entry?.totalTokens ?? (entry?.inputTokens ?? 0) + (entry?.outputTokens ?? 0);
|
||||||
|
|
||||||
// Prefer prompt-size tokens from the session transcript when it looks larger
|
// Prefer prompt-size tokens from the session transcript when it looks larger
|
||||||
// (cached prompt tokens are often missing from agent meta/store).
|
// (cached prompt tokens are often missing from agent meta/store).
|
||||||
@@ -352,6 +354,7 @@ export function buildStatusMessage(args: StatusArgs): string {
|
|||||||
const logUsage = readUsageFromSessionLog(
|
const logUsage = readUsageFromSessionLog(
|
||||||
entry?.sessionId,
|
entry?.sessionId,
|
||||||
entry,
|
entry,
|
||||||
|
args.agentId,
|
||||||
args.sessionKey,
|
args.sessionKey,
|
||||||
args.sessionStorePath,
|
args.sessionStorePath,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -64,10 +64,20 @@ vi.mock("../../infra/session-cost-usage.js", async () => {
|
|||||||
cacheWriteCost: 0,
|
cacheWriteCost: 0,
|
||||||
missingCostEntries: 0,
|
missingCostEntries: 0,
|
||||||
})),
|
})),
|
||||||
|
loadSessionUsageTimeSeries: vi.fn(async () => ({
|
||||||
|
sessionId: "s-opus",
|
||||||
|
points: [],
|
||||||
|
})),
|
||||||
|
loadSessionLogs: vi.fn(async () => []),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
import { discoverAllSessions } from "../../infra/session-cost-usage.js";
|
import {
|
||||||
|
discoverAllSessions,
|
||||||
|
loadSessionCostSummary,
|
||||||
|
loadSessionLogs,
|
||||||
|
loadSessionUsageTimeSeries,
|
||||||
|
} from "../../infra/session-cost-usage.js";
|
||||||
import { loadCombinedSessionStoreForGateway } from "../session-utils.js";
|
import { loadCombinedSessionStoreForGateway } from "../session-utils.js";
|
||||||
import { usageHandlers } from "./usage.js";
|
import { usageHandlers } from "./usage.js";
|
||||||
|
|
||||||
@@ -148,6 +158,10 @@ describe("sessions.usage", () => {
|
|||||||
const result = respond.mock.calls[0]?.[1] as unknown as { sessions: Array<{ key: string }> };
|
const result = respond.mock.calls[0]?.[1] as unknown as { sessions: Array<{ key: string }> };
|
||||||
expect(result.sessions).toHaveLength(1);
|
expect(result.sessions).toHaveLength(1);
|
||||||
expect(result.sessions[0]?.key).toBe(storeKey);
|
expect(result.sessions[0]?.key).toBe(storeKey);
|
||||||
|
expect(vi.mocked(loadSessionCostSummary)).toHaveBeenCalled();
|
||||||
|
expect(
|
||||||
|
vi.mocked(loadSessionCostSummary).mock.calls.some((call) => call[0]?.agentId === "opus"),
|
||||||
|
).toBe(true);
|
||||||
} finally {
|
} finally {
|
||||||
if (previousStateDir === undefined) {
|
if (previousStateDir === undefined) {
|
||||||
delete process.env.OPENCLAW_STATE_DIR;
|
delete process.env.OPENCLAW_STATE_DIR;
|
||||||
@@ -176,4 +190,32 @@ describe("sessions.usage", () => {
|
|||||||
const error = respond.mock.calls[0]?.[2] as { message?: string } | undefined;
|
const error = respond.mock.calls[0]?.[2] as { message?: string } | undefined;
|
||||||
expect(error?.message).toContain("Invalid session reference");
|
expect(error?.message).toContain("Invalid session reference");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("passes parsed agentId into sessions.usage.timeseries", async () => {
|
||||||
|
const respond = vi.fn();
|
||||||
|
|
||||||
|
await usageHandlers["sessions.usage.timeseries"]({
|
||||||
|
respond,
|
||||||
|
params: {
|
||||||
|
key: "agent:opus:s-opus",
|
||||||
|
},
|
||||||
|
} as unknown as Parameters<(typeof usageHandlers)["sessions.usage.timeseries"]>[0]);
|
||||||
|
|
||||||
|
expect(vi.mocked(loadSessionUsageTimeSeries)).toHaveBeenCalled();
|
||||||
|
expect(vi.mocked(loadSessionUsageTimeSeries).mock.calls[0]?.[0]?.agentId).toBe("opus");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("passes parsed agentId into sessions.usage.logs", async () => {
|
||||||
|
const respond = vi.fn();
|
||||||
|
|
||||||
|
await usageHandlers["sessions.usage.logs"]({
|
||||||
|
respond,
|
||||||
|
params: {
|
||||||
|
key: "agent:opus:s-opus",
|
||||||
|
},
|
||||||
|
} as unknown as Parameters<(typeof usageHandlers)["sessions.usage.logs"]>[0]);
|
||||||
|
|
||||||
|
expect(vi.mocked(loadSessionLogs)).toHaveBeenCalled();
|
||||||
|
expect(vi.mocked(loadSessionLogs).mock.calls[0]?.[0]?.agentId).toBe("opus");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -496,11 +496,13 @@ export const usageHandlers: GatewayRequestHandlers = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
for (const merged of limitedEntries) {
|
for (const merged of limitedEntries) {
|
||||||
|
const agentId = parseAgentSessionKey(merged.key)?.agentId;
|
||||||
const usage = await loadSessionCostSummary({
|
const usage = await loadSessionCostSummary({
|
||||||
sessionId: merged.sessionId,
|
sessionId: merged.sessionId,
|
||||||
sessionEntry: merged.storeEntry,
|
sessionEntry: merged.storeEntry,
|
||||||
sessionFile: merged.sessionFile,
|
sessionFile: merged.sessionFile,
|
||||||
config,
|
config,
|
||||||
|
agentId,
|
||||||
startMs,
|
startMs,
|
||||||
endMs,
|
endMs,
|
||||||
});
|
});
|
||||||
@@ -519,7 +521,6 @@ export const usageHandlers: GatewayRequestHandlers = {
|
|||||||
aggregateTotals.missingCostEntries += usage.missingCostEntries;
|
aggregateTotals.missingCostEntries += usage.missingCostEntries;
|
||||||
}
|
}
|
||||||
|
|
||||||
const agentId = parseAgentSessionKey(merged.key)?.agentId;
|
|
||||||
const channel = merged.storeEntry?.channel ?? merged.storeEntry?.origin?.provider;
|
const channel = merged.storeEntry?.channel ?? merged.storeEntry?.origin?.provider;
|
||||||
const chatType = merged.storeEntry?.chatType ?? merged.storeEntry?.origin?.chatType;
|
const chatType = merged.storeEntry?.chatType ?? merged.storeEntry?.origin?.chatType;
|
||||||
|
|
||||||
@@ -796,6 +797,7 @@ export const usageHandlers: GatewayRequestHandlers = {
|
|||||||
sessionEntry: entry,
|
sessionEntry: entry,
|
||||||
sessionFile,
|
sessionFile,
|
||||||
config,
|
config,
|
||||||
|
agentId,
|
||||||
maxPoints: 200,
|
maxPoints: 200,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -849,6 +851,7 @@ export const usageHandlers: GatewayRequestHandlers = {
|
|||||||
sessionEntry: entry,
|
sessionEntry: entry,
|
||||||
sessionFile,
|
sessionFile,
|
||||||
config,
|
config,
|
||||||
|
agentId,
|
||||||
limit,
|
limit,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import {
|
|||||||
discoverAllSessions,
|
discoverAllSessions,
|
||||||
loadCostUsageSummary,
|
loadCostUsageSummary,
|
||||||
loadSessionCostSummary,
|
loadSessionCostSummary,
|
||||||
|
loadSessionLogs,
|
||||||
|
loadSessionUsageTimeSeries,
|
||||||
} from "./session-cost-usage.js";
|
} from "./session-cost-usage.js";
|
||||||
|
|
||||||
describe("session cost usage", () => {
|
describe("session cost usage", () => {
|
||||||
@@ -240,4 +242,133 @@ describe("session cost usage", () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("resolves non-main absolute sessionFile using explicit agentId for cost summary", async () => {
|
||||||
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-cost-agent-"));
|
||||||
|
const workerSessionsDir = path.join(root, "agents", "worker1", "sessions");
|
||||||
|
await fs.mkdir(workerSessionsDir, { recursive: true });
|
||||||
|
const workerSessionFile = path.join(workerSessionsDir, "sess-worker-1.jsonl");
|
||||||
|
const now = new Date("2026-02-12T10:00:00.000Z");
|
||||||
|
|
||||||
|
await fs.writeFile(
|
||||||
|
workerSessionFile,
|
||||||
|
JSON.stringify({
|
||||||
|
type: "message",
|
||||||
|
timestamp: now.toISOString(),
|
||||||
|
message: {
|
||||||
|
role: "assistant",
|
||||||
|
provider: "openai",
|
||||||
|
model: "gpt-5.2",
|
||||||
|
usage: {
|
||||||
|
input: 7,
|
||||||
|
output: 11,
|
||||||
|
totalTokens: 18,
|
||||||
|
cost: { total: 0.01 },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
"utf-8",
|
||||||
|
);
|
||||||
|
|
||||||
|
const originalState = process.env.OPENCLAW_STATE_DIR;
|
||||||
|
process.env.OPENCLAW_STATE_DIR = root;
|
||||||
|
try {
|
||||||
|
const summary = await loadSessionCostSummary({
|
||||||
|
sessionId: "sess-worker-1",
|
||||||
|
sessionEntry: { sessionFile: workerSessionFile } as { sessionFile: string },
|
||||||
|
agentId: "worker1",
|
||||||
|
});
|
||||||
|
expect(summary?.totalTokens).toBe(18);
|
||||||
|
expect(summary?.totalCost).toBeCloseTo(0.01, 5);
|
||||||
|
} finally {
|
||||||
|
if (originalState === undefined) {
|
||||||
|
delete process.env.OPENCLAW_STATE_DIR;
|
||||||
|
} else {
|
||||||
|
process.env.OPENCLAW_STATE_DIR = originalState;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("resolves non-main absolute sessionFile using explicit agentId for timeseries", async () => {
|
||||||
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-timeseries-agent-"));
|
||||||
|
const workerSessionsDir = path.join(root, "agents", "worker2", "sessions");
|
||||||
|
await fs.mkdir(workerSessionsDir, { recursive: true });
|
||||||
|
const workerSessionFile = path.join(workerSessionsDir, "sess-worker-2.jsonl");
|
||||||
|
|
||||||
|
await fs.writeFile(
|
||||||
|
workerSessionFile,
|
||||||
|
[
|
||||||
|
JSON.stringify({
|
||||||
|
type: "message",
|
||||||
|
timestamp: "2026-02-12T10:00:00.000Z",
|
||||||
|
message: {
|
||||||
|
role: "assistant",
|
||||||
|
provider: "openai",
|
||||||
|
model: "gpt-5.2",
|
||||||
|
usage: { input: 5, output: 3, totalTokens: 8, cost: { total: 0.001 } },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
].join("\n"),
|
||||||
|
"utf-8",
|
||||||
|
);
|
||||||
|
|
||||||
|
const originalState = process.env.OPENCLAW_STATE_DIR;
|
||||||
|
process.env.OPENCLAW_STATE_DIR = root;
|
||||||
|
try {
|
||||||
|
const timeseries = await loadSessionUsageTimeSeries({
|
||||||
|
sessionId: "sess-worker-2",
|
||||||
|
sessionEntry: { sessionFile: workerSessionFile } as { sessionFile: string },
|
||||||
|
agentId: "worker2",
|
||||||
|
});
|
||||||
|
expect(timeseries?.points.length).toBe(1);
|
||||||
|
expect(timeseries?.points[0]?.totalTokens).toBe(8);
|
||||||
|
} finally {
|
||||||
|
if (originalState === undefined) {
|
||||||
|
delete process.env.OPENCLAW_STATE_DIR;
|
||||||
|
} else {
|
||||||
|
process.env.OPENCLAW_STATE_DIR = originalState;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("resolves non-main absolute sessionFile using explicit agentId for logs", async () => {
|
||||||
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-logs-agent-"));
|
||||||
|
const workerSessionsDir = path.join(root, "agents", "worker3", "sessions");
|
||||||
|
await fs.mkdir(workerSessionsDir, { recursive: true });
|
||||||
|
const workerSessionFile = path.join(workerSessionsDir, "sess-worker-3.jsonl");
|
||||||
|
|
||||||
|
await fs.writeFile(
|
||||||
|
workerSessionFile,
|
||||||
|
[
|
||||||
|
JSON.stringify({
|
||||||
|
type: "message",
|
||||||
|
timestamp: "2026-02-12T10:00:00.000Z",
|
||||||
|
message: {
|
||||||
|
role: "user",
|
||||||
|
content: "hello worker",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
].join("\n"),
|
||||||
|
"utf-8",
|
||||||
|
);
|
||||||
|
|
||||||
|
const originalState = process.env.OPENCLAW_STATE_DIR;
|
||||||
|
process.env.OPENCLAW_STATE_DIR = root;
|
||||||
|
try {
|
||||||
|
const logs = await loadSessionLogs({
|
||||||
|
sessionId: "sess-worker-3",
|
||||||
|
sessionEntry: { sessionFile: workerSessionFile } as { sessionFile: string },
|
||||||
|
agentId: "worker3",
|
||||||
|
});
|
||||||
|
expect(logs).toHaveLength(1);
|
||||||
|
expect(logs?.[0]?.content).toContain("hello worker");
|
||||||
|
expect(logs?.[0]?.role).toBe("user");
|
||||||
|
} finally {
|
||||||
|
if (originalState === undefined) {
|
||||||
|
delete process.env.OPENCLAW_STATE_DIR;
|
||||||
|
} else {
|
||||||
|
process.env.OPENCLAW_STATE_DIR = originalState;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -561,12 +561,17 @@ export async function loadSessionCostSummary(params: {
|
|||||||
sessionEntry?: SessionEntry;
|
sessionEntry?: SessionEntry;
|
||||||
sessionFile?: string;
|
sessionFile?: string;
|
||||||
config?: OpenClawConfig;
|
config?: OpenClawConfig;
|
||||||
|
agentId?: string;
|
||||||
startMs?: number;
|
startMs?: number;
|
||||||
endMs?: number;
|
endMs?: number;
|
||||||
}): Promise<SessionCostSummary | null> {
|
}): Promise<SessionCostSummary | null> {
|
||||||
const sessionFile =
|
const sessionFile =
|
||||||
params.sessionFile ??
|
params.sessionFile ??
|
||||||
(params.sessionId ? resolveSessionFilePath(params.sessionId, params.sessionEntry) : undefined);
|
(params.sessionId
|
||||||
|
? resolveSessionFilePath(params.sessionId, params.sessionEntry, {
|
||||||
|
agentId: params.agentId,
|
||||||
|
})
|
||||||
|
: undefined);
|
||||||
if (!sessionFile || !fs.existsSync(sessionFile)) {
|
if (!sessionFile || !fs.existsSync(sessionFile)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -851,11 +856,16 @@ export async function loadSessionUsageTimeSeries(params: {
|
|||||||
sessionEntry?: SessionEntry;
|
sessionEntry?: SessionEntry;
|
||||||
sessionFile?: string;
|
sessionFile?: string;
|
||||||
config?: OpenClawConfig;
|
config?: OpenClawConfig;
|
||||||
|
agentId?: string;
|
||||||
maxPoints?: number;
|
maxPoints?: number;
|
||||||
}): Promise<SessionUsageTimeSeries | null> {
|
}): Promise<SessionUsageTimeSeries | null> {
|
||||||
const sessionFile =
|
const sessionFile =
|
||||||
params.sessionFile ??
|
params.sessionFile ??
|
||||||
(params.sessionId ? resolveSessionFilePath(params.sessionId, params.sessionEntry) : undefined);
|
(params.sessionId
|
||||||
|
? resolveSessionFilePath(params.sessionId, params.sessionEntry, {
|
||||||
|
agentId: params.agentId,
|
||||||
|
})
|
||||||
|
: undefined);
|
||||||
if (!sessionFile || !fs.existsSync(sessionFile)) {
|
if (!sessionFile || !fs.existsSync(sessionFile)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -931,11 +941,16 @@ export async function loadSessionLogs(params: {
|
|||||||
sessionEntry?: SessionEntry;
|
sessionEntry?: SessionEntry;
|
||||||
sessionFile?: string;
|
sessionFile?: string;
|
||||||
config?: OpenClawConfig;
|
config?: OpenClawConfig;
|
||||||
|
agentId?: string;
|
||||||
limit?: number;
|
limit?: number;
|
||||||
}): Promise<SessionLogEntry[] | null> {
|
}): Promise<SessionLogEntry[] | null> {
|
||||||
const sessionFile =
|
const sessionFile =
|
||||||
params.sessionFile ??
|
params.sessionFile ??
|
||||||
(params.sessionId ? resolveSessionFilePath(params.sessionId, params.sessionEntry) : undefined);
|
(params.sessionId
|
||||||
|
? resolveSessionFilePath(params.sessionId, params.sessionEntry, {
|
||||||
|
agentId: params.agentId,
|
||||||
|
})
|
||||||
|
: undefined);
|
||||||
if (!sessionFile || !fs.existsSync(sessionFile)) {
|
if (!sessionFile || !fs.existsSync(sessionFile)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user