2025-12-12 23:22:05 +00:00
|
|
|
|
import fs from "node:fs";
|
|
|
|
|
|
import path from "node:path";
|
2025-12-07 16:53:19 +00:00
|
|
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
2026-01-09 16:49:01 +01:00
|
|
|
|
import { normalizeTestText } from "../../test/helpers/normalize-text.js";
|
2026-01-09 16:39:02 +01:00
|
|
|
|
import { withTempHome } from "../../test/helpers/temp-home.js";
|
2026-02-18 01:34:35 +00:00
|
|
|
|
import type { OpenClawConfig } from "../config/config.js";
|
2026-03-09 01:59:16 +08:00
|
|
|
|
import { applyModelOverrideToSessionEntry } from "../sessions/model-overrides.js";
|
2026-02-16 14:52:09 +00:00
|
|
|
|
import { createSuccessfulImageMediaDecision } from "./media-understanding.test-fixtures.js";
|
2026-01-27 02:35:09 -05:00
|
|
|
|
import {
|
|
|
|
|
|
buildCommandsMessage,
|
|
|
|
|
|
buildCommandsMessagePaginated,
|
|
|
|
|
|
buildHelpMessage,
|
|
|
|
|
|
buildStatusMessage,
|
|
|
|
|
|
} from "./status.js";
|
|
|
|
|
|
|
|
|
|
|
|
const { listPluginCommands } = vi.hoisted(() => ({
|
2026-02-17 11:16:36 +09:00
|
|
|
|
listPluginCommands: vi.fn(
|
|
|
|
|
|
(): Array<{ name: string; description: string; pluginId: string }> => [],
|
|
|
|
|
|
),
|
2026-01-27 02:35:09 -05:00
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
vi.mock("../plugins/commands.js", () => ({
|
|
|
|
|
|
listPluginCommands,
|
|
|
|
|
|
}));
|
2025-12-07 16:53:19 +00:00
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
|
vi.restoreAllMocks();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe("buildStatusMessage", () => {
|
|
|
|
|
|
it("summarizes agent readiness and context usage", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
2026-01-09 03:14:39 +00:00
|
|
|
|
config: {
|
|
|
|
|
|
models: {
|
|
|
|
|
|
providers: {
|
|
|
|
|
|
anthropic: {
|
|
|
|
|
|
apiKey: "test-key",
|
|
|
|
|
|
models: [
|
|
|
|
|
|
{
|
|
|
|
|
|
id: "pi:opus",
|
|
|
|
|
|
cost: {
|
|
|
|
|
|
input: 1,
|
|
|
|
|
|
output: 1,
|
|
|
|
|
|
cacheRead: 0,
|
|
|
|
|
|
cacheWrite: 0,
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-02-17 14:32:18 +09:00
|
|
|
|
} as unknown as OpenClawConfig,
|
2025-12-26 00:43:44 +01:00
|
|
|
|
agent: {
|
|
|
|
|
|
model: "anthropic/pi:opus",
|
|
|
|
|
|
contextTokens: 32_000,
|
|
|
|
|
|
},
|
2025-12-07 16:53:19 +00:00
|
|
|
|
sessionEntry: {
|
|
|
|
|
|
sessionId: "abc",
|
|
|
|
|
|
updatedAt: 0,
|
2026-01-09 03:14:39 +00:00
|
|
|
|
inputTokens: 1200,
|
|
|
|
|
|
outputTokens: 800,
|
2025-12-07 16:53:19 +00:00
|
|
|
|
totalTokens: 16_000,
|
|
|
|
|
|
contextTokens: 32_000,
|
|
|
|
|
|
thinkingLevel: "low",
|
|
|
|
|
|
verboseLevel: "on",
|
2026-01-06 02:41:48 +01:00
|
|
|
|
compactionCount: 2,
|
2025-12-07 16:53:19 +00:00
|
|
|
|
},
|
2026-01-06 18:25:37 +00:00
|
|
|
|
sessionKey: "agent:main:main",
|
2025-12-07 16:53:19 +00:00
|
|
|
|
sessionScope: "per-sender",
|
|
|
|
|
|
resolvedThink: "medium",
|
|
|
|
|
|
resolvedVerbose: "off",
|
2026-01-07 07:21:56 +01:00
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
2026-01-09 02:21:17 +00:00
|
|
|
|
modelAuth: "api-key",
|
2026-01-09 03:14:39 +00:00
|
|
|
|
now: 10 * 60_000, // 10 minutes later
|
2025-12-07 16:53:19 +00:00
|
|
|
|
});
|
2026-01-09 16:49:01 +01:00
|
|
|
|
const normalized = normalizeTestText(text);
|
2025-12-07 16:53:19 +00:00
|
|
|
|
|
2026-01-30 03:15:10 +01:00
|
|
|
|
expect(normalized).toContain("OpenClaw");
|
2026-01-09 16:49:01 +01:00
|
|
|
|
expect(normalized).toContain("Model: anthropic/pi:opus");
|
|
|
|
|
|
expect(normalized).toContain("api-key");
|
|
|
|
|
|
expect(normalized).toContain("Tokens: 1.2k in / 800 out");
|
|
|
|
|
|
expect(normalized).toContain("Cost: $0.0020");
|
|
|
|
|
|
expect(normalized).toContain("Context: 16k/32k (50%)");
|
|
|
|
|
|
expect(normalized).toContain("Compactions: 2");
|
|
|
|
|
|
expect(normalized).toContain("Session: agent:main:main");
|
|
|
|
|
|
expect(normalized).toContain("updated 10m ago");
|
|
|
|
|
|
expect(normalized).toContain("Runtime: direct");
|
|
|
|
|
|
expect(normalized).toContain("Think: medium");
|
2026-01-09 23:41:52 +00:00
|
|
|
|
expect(normalized).not.toContain("verbose");
|
|
|
|
|
|
expect(normalized).toContain("elevated");
|
2026-01-09 16:49:01 +01:00
|
|
|
|
expect(normalized).toContain("Queue: collect");
|
2025-12-07 16:53:19 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-03-01 08:14:00 +09:00
|
|
|
|
it("falls back to sessionEntry levels when resolved levels are not passed", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
agent: {
|
|
|
|
|
|
model: "anthropic/pi:opus",
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionEntry: {
|
|
|
|
|
|
sessionId: "abc",
|
|
|
|
|
|
updatedAt: 0,
|
|
|
|
|
|
thinkingLevel: "high",
|
|
|
|
|
|
verboseLevel: "full",
|
|
|
|
|
|
reasoningLevel: "on",
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionKey: "agent:main:main",
|
|
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
|
|
|
|
|
});
|
|
|
|
|
|
const normalized = normalizeTestText(text);
|
|
|
|
|
|
|
|
|
|
|
|
expect(normalized).toContain("Think: high");
|
|
|
|
|
|
expect(normalized).toContain("verbose:full");
|
|
|
|
|
|
expect(normalized).toContain("Reasoning: on");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-20 19:26:25 -06:00
|
|
|
|
it("notes channel model overrides in status output", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
config: {
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
modelByChannel: {
|
|
|
|
|
|
discord: {
|
|
|
|
|
|
"123": "openai/gpt-4.1",
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
} as unknown as OpenClawConfig,
|
|
|
|
|
|
agent: {
|
|
|
|
|
|
model: "openai/gpt-4.1",
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionEntry: {
|
|
|
|
|
|
sessionId: "abc",
|
|
|
|
|
|
updatedAt: 0,
|
|
|
|
|
|
channel: "discord",
|
|
|
|
|
|
groupId: "123",
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionKey: "agent:main:discord:channel:123",
|
|
|
|
|
|
sessionScope: "per-sender",
|
|
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
|
|
|
|
|
});
|
|
|
|
|
|
const normalized = normalizeTestText(text);
|
|
|
|
|
|
|
|
|
|
|
|
expect(normalized).toContain("Model: openai/gpt-4.1");
|
|
|
|
|
|
expect(normalized).toContain("channel override");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-23 12:29:09 -05:00
|
|
|
|
it("shows 1M context window when anthropic context1m is enabled", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
config: {
|
|
|
|
|
|
agents: {
|
|
|
|
|
|
defaults: {
|
|
|
|
|
|
model: "anthropic/claude-opus-4-6",
|
|
|
|
|
|
models: {
|
|
|
|
|
|
"anthropic/claude-opus-4-6": {
|
|
|
|
|
|
params: { context1m: true },
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
} as unknown as OpenClawConfig,
|
|
|
|
|
|
agent: {
|
|
|
|
|
|
model: "anthropic/claude-opus-4-6",
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionEntry: {
|
|
|
|
|
|
sessionId: "ctx1m",
|
|
|
|
|
|
updatedAt: 0,
|
|
|
|
|
|
totalTokens: 200_000,
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionKey: "agent:main:main",
|
|
|
|
|
|
sessionScope: "per-sender",
|
|
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(normalizeTestText(text)).toContain("Context: 200k/1.0m");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-03-09 01:59:16 +08:00
|
|
|
|
it("recomputes context window from the active model after switching away from a smaller session override", () => {
|
|
|
|
|
|
const sessionEntry = {
|
|
|
|
|
|
sessionId: "switch-back",
|
|
|
|
|
|
updatedAt: 0,
|
|
|
|
|
|
providerOverride: "local",
|
|
|
|
|
|
modelOverride: "small-model",
|
|
|
|
|
|
contextTokens: 4_096,
|
|
|
|
|
|
totalTokens: 1_024,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
applyModelOverrideToSessionEntry({
|
|
|
|
|
|
entry: sessionEntry,
|
|
|
|
|
|
selection: {
|
|
|
|
|
|
provider: "local",
|
|
|
|
|
|
model: "large-model",
|
|
|
|
|
|
isDefault: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
agent: {
|
|
|
|
|
|
model: "local/large-model",
|
|
|
|
|
|
contextTokens: 65_536,
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionEntry,
|
|
|
|
|
|
sessionKey: "agent:main:main",
|
|
|
|
|
|
sessionScope: "per-sender",
|
|
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(normalizeTestText(text)).toContain("Context: 1.0k/66k");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-21 09:07:21 +00:00
|
|
|
|
it("uses per-agent sandbox config when config and session key are provided", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
config: {
|
|
|
|
|
|
agents: {
|
|
|
|
|
|
list: [
|
|
|
|
|
|
{ id: "main", default: true },
|
|
|
|
|
|
{ id: "discord", sandbox: { mode: "all" } },
|
|
|
|
|
|
],
|
|
|
|
|
|
},
|
2026-02-17 14:32:18 +09:00
|
|
|
|
} as unknown as OpenClawConfig,
|
2026-01-21 09:07:21 +00:00
|
|
|
|
agent: {},
|
|
|
|
|
|
sessionKey: "agent:discord:discord:channel:1456350065223270435",
|
|
|
|
|
|
sessionScope: "per-sender",
|
|
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(normalizeTestText(text)).toContain("Runtime: docker/all");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-09 03:18:41 +01:00
|
|
|
|
it("shows verbose/elevated labels only when enabled", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
agent: { model: "anthropic/claude-opus-4-5" },
|
|
|
|
|
|
sessionEntry: { sessionId: "v1", updatedAt: 0 },
|
|
|
|
|
|
sessionKey: "agent:main:main",
|
|
|
|
|
|
sessionScope: "per-sender",
|
|
|
|
|
|
resolvedThink: "low",
|
|
|
|
|
|
resolvedVerbose: "on",
|
2026-01-09 23:43:24 +00:00
|
|
|
|
resolvedElevated: "on",
|
|
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
|
|
|
|
|
});
|
2026-01-09 03:18:41 +01:00
|
|
|
|
|
2026-01-09 23:41:52 +00:00
|
|
|
|
expect(text).toContain("verbose");
|
|
|
|
|
|
expect(text).toContain("elevated");
|
2025-12-07 16:53:19 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-17 07:17:09 +00:00
|
|
|
|
it("includes media understanding decisions when present", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
agent: { model: "anthropic/claude-opus-4-5" },
|
|
|
|
|
|
sessionEntry: { sessionId: "media", updatedAt: 0 },
|
|
|
|
|
|
sessionKey: "agent:main:main",
|
|
|
|
|
|
queue: { mode: "none" },
|
|
|
|
|
|
mediaDecisions: [
|
2026-02-17 15:48:29 +09:00
|
|
|
|
createSuccessfulImageMediaDecision() as unknown as NonNullable<
|
|
|
|
|
|
Parameters<typeof buildStatusMessage>[0]["mediaDecisions"]
|
|
|
|
|
|
>[number],
|
2026-01-17 07:17:09 +00:00
|
|
|
|
{
|
|
|
|
|
|
capability: "audio",
|
|
|
|
|
|
outcome: "skipped",
|
|
|
|
|
|
attachments: [
|
|
|
|
|
|
{
|
|
|
|
|
|
attachmentIndex: 1,
|
|
|
|
|
|
attempts: [
|
|
|
|
|
|
{
|
|
|
|
|
|
type: "provider",
|
|
|
|
|
|
outcome: "skipped",
|
|
|
|
|
|
reason: "maxBytes: too large",
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const normalized = normalizeTestText(text);
|
|
|
|
|
|
expect(normalized).toContain("Media: image ok (openai/gpt-5.2) · audio skipped (maxBytes)");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-21 03:32:11 +00:00
|
|
|
|
it("omits media line when all decisions are none", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
agent: { model: "anthropic/claude-opus-4-5" },
|
|
|
|
|
|
sessionEntry: { sessionId: "media-none", updatedAt: 0 },
|
|
|
|
|
|
sessionKey: "agent:main:main",
|
|
|
|
|
|
queue: { mode: "none" },
|
|
|
|
|
|
mediaDecisions: [
|
|
|
|
|
|
{ capability: "image", outcome: "no-attachment", attachments: [] },
|
|
|
|
|
|
{ capability: "audio", outcome: "no-attachment", attachments: [] },
|
|
|
|
|
|
{ capability: "video", outcome: "no-attachment", attachments: [] },
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(normalizeTestText(text)).not.toContain("Media:");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-10 05:31:48 +01:00
|
|
|
|
it("does not show elevated label when session explicitly disables it", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
agent: { model: "anthropic/claude-opus-4-5", elevatedDefault: "on" },
|
|
|
|
|
|
sessionEntry: { sessionId: "v1", updatedAt: 0, elevatedLevel: "off" },
|
|
|
|
|
|
sessionKey: "agent:main:main",
|
|
|
|
|
|
sessionScope: "per-sender",
|
|
|
|
|
|
resolvedThink: "low",
|
|
|
|
|
|
resolvedVerbose: "off",
|
|
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-14 14:31:43 +00:00
|
|
|
|
const optionsLine = text.split("\n").find((line) => line.trim().startsWith("⚙️"));
|
2026-01-10 05:31:48 +01:00
|
|
|
|
expect(optionsLine).toBeTruthy();
|
|
|
|
|
|
expect(optionsLine).not.toContain("elevated");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-19 14:33:02 -08:00
|
|
|
|
it("shows selected model and active runtime model when they differ", () => {
|
2026-01-07 18:38:55 +00:00
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
agent: {
|
|
|
|
|
|
model: "anthropic/claude-opus-4-5",
|
|
|
|
|
|
contextTokens: 32_000,
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionEntry: {
|
|
|
|
|
|
sessionId: "override-1",
|
|
|
|
|
|
updatedAt: 0,
|
|
|
|
|
|
providerOverride: "openai",
|
|
|
|
|
|
modelOverride: "gpt-4.1-mini",
|
|
|
|
|
|
modelProvider: "anthropic",
|
|
|
|
|
|
model: "claude-haiku-4-5",
|
2026-02-19 14:33:02 -08:00
|
|
|
|
fallbackNoticeSelectedModel: "openai/gpt-4.1-mini",
|
|
|
|
|
|
fallbackNoticeActiveModel: "anthropic/claude-haiku-4-5",
|
|
|
|
|
|
fallbackNoticeReason: "rate limit",
|
2026-01-07 18:38:55 +00:00
|
|
|
|
contextTokens: 32_000,
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionKey: "agent:main:main",
|
|
|
|
|
|
sessionScope: "per-sender",
|
|
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
2026-01-09 02:21:17 +00:00
|
|
|
|
modelAuth: "api-key",
|
2026-02-19 14:33:02 -08:00
|
|
|
|
activeModelAuth: "api-key di_123…abc (deepinfra:default)",
|
2026-01-07 18:38:55 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-19 14:33:02 -08:00
|
|
|
|
const normalized = normalizeTestText(text);
|
|
|
|
|
|
expect(normalized).toContain("Model: openai/gpt-4.1-mini");
|
|
|
|
|
|
expect(normalized).toContain("Fallback: anthropic/claude-haiku-4-5");
|
|
|
|
|
|
expect(normalized).toContain("(rate limit)");
|
|
|
|
|
|
expect(normalized).not.toContain(" - Reason:");
|
|
|
|
|
|
expect(normalized).not.toContain("Active:");
|
|
|
|
|
|
expect(normalized).toContain("di_123...abc");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("omits active fallback details when runtime drift does not match fallback state", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
agent: {
|
|
|
|
|
|
model: "openai/gpt-4.1-mini",
|
|
|
|
|
|
contextTokens: 32_000,
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionEntry: {
|
|
|
|
|
|
sessionId: "runtime-drift-only",
|
|
|
|
|
|
updatedAt: 0,
|
|
|
|
|
|
modelProvider: "anthropic",
|
|
|
|
|
|
model: "claude-haiku-4-5",
|
|
|
|
|
|
fallbackNoticeSelectedModel: "fireworks/minimax-m2p5",
|
|
|
|
|
|
fallbackNoticeActiveModel: "deepinfra/moonshotai/Kimi-K2.5",
|
|
|
|
|
|
fallbackNoticeReason: "rate limit",
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionKey: "agent:main:main",
|
|
|
|
|
|
sessionScope: "per-sender",
|
|
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
|
|
|
|
|
modelAuth: "api-key",
|
|
|
|
|
|
activeModelAuth: "api-key di_123…abc (deepinfra:default)",
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const normalized = normalizeTestText(text);
|
|
|
|
|
|
expect(normalized).toContain("Model: openai/gpt-4.1-mini");
|
|
|
|
|
|
expect(normalized).not.toContain("Fallback:");
|
|
|
|
|
|
expect(normalized).not.toContain("(rate limit)");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("omits active lines when runtime matches selected model", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
agent: {
|
|
|
|
|
|
model: "openai/gpt-4.1-mini",
|
|
|
|
|
|
contextTokens: 32_000,
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionEntry: {
|
|
|
|
|
|
sessionId: "selected-active-same",
|
|
|
|
|
|
updatedAt: 0,
|
|
|
|
|
|
modelProvider: "openai",
|
|
|
|
|
|
model: "gpt-4.1-mini",
|
|
|
|
|
|
fallbackNoticeReason: "unknown",
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionKey: "agent:main:main",
|
|
|
|
|
|
sessionScope: "per-sender",
|
|
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
|
|
|
|
|
modelAuth: "api-key",
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const normalized = normalizeTestText(text);
|
|
|
|
|
|
expect(normalized).not.toContain("Fallback:");
|
2026-01-07 18:38:55 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-08 20:23:38 +01:00
|
|
|
|
it("keeps provider prefix from configured model", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
agent: {
|
|
|
|
|
|
model: "google-antigravity/claude-sonnet-4-5",
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionScope: "per-sender",
|
|
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
2026-01-09 02:21:17 +00:00
|
|
|
|
modelAuth: "api-key",
|
2026-01-08 20:23:38 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-14 14:31:43 +00:00
|
|
|
|
expect(normalizeTestText(text)).toContain("Model: google-antigravity/claude-sonnet-4-5");
|
2026-01-08 20:23:38 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-12-17 11:29:04 +01:00
|
|
|
|
it("handles missing agent config gracefully", () => {
|
2025-12-07 16:53:19 +00:00
|
|
|
|
const text = buildStatusMessage({
|
2025-12-17 11:29:04 +01:00
|
|
|
|
agent: {},
|
2025-12-07 16:53:19 +00:00
|
|
|
|
sessionScope: "per-sender",
|
2026-01-09 02:21:17 +00:00
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
|
|
|
|
|
modelAuth: "api-key",
|
2025-12-07 16:53:19 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-09 16:49:01 +01:00
|
|
|
|
const normalized = normalizeTestText(text);
|
|
|
|
|
|
expect(normalized).toContain("Model:");
|
|
|
|
|
|
expect(normalized).toContain("Context:");
|
|
|
|
|
|
expect(normalized).toContain("Queue: collect");
|
2025-12-07 16:53:19 +00:00
|
|
|
|
});
|
2025-12-12 23:22:05 +00:00
|
|
|
|
|
2025-12-22 20:36:29 +01:00
|
|
|
|
it("includes group activation for group sessions", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
agent: {},
|
|
|
|
|
|
sessionEntry: {
|
|
|
|
|
|
sessionId: "g1",
|
|
|
|
|
|
updatedAt: 0,
|
|
|
|
|
|
groupActivation: "always",
|
2026-01-02 10:14:58 +01:00
|
|
|
|
chatType: "group",
|
2025-12-22 20:36:29 +01:00
|
|
|
|
},
|
2026-01-06 18:25:37 +00:00
|
|
|
|
sessionKey: "agent:main:whatsapp:group:123@g.us",
|
2025-12-22 20:36:29 +01:00
|
|
|
|
sessionScope: "per-sender",
|
2026-01-07 07:21:56 +01:00
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
2026-01-09 02:21:17 +00:00
|
|
|
|
modelAuth: "api-key",
|
2025-12-22 20:36:29 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-09 03:14:39 +00:00
|
|
|
|
expect(text).toContain("Activation: always");
|
2026-01-07 07:21:56 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("shows queue details when overridden", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
agent: {},
|
|
|
|
|
|
sessionEntry: { sessionId: "q1", updatedAt: 0 },
|
|
|
|
|
|
sessionKey: "agent:main:main",
|
|
|
|
|
|
sessionScope: "per-sender",
|
|
|
|
|
|
queue: {
|
|
|
|
|
|
mode: "collect",
|
|
|
|
|
|
depth: 3,
|
|
|
|
|
|
debounceMs: 2000,
|
|
|
|
|
|
cap: 5,
|
|
|
|
|
|
dropPolicy: "old",
|
|
|
|
|
|
showDetails: true,
|
|
|
|
|
|
},
|
2026-01-09 02:21:17 +00:00
|
|
|
|
modelAuth: "api-key",
|
2026-01-07 07:21:56 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-14 14:31:43 +00:00
|
|
|
|
expect(text).toContain("Queue: collect (depth 3 · debounce 2s · cap 5 · drop old)");
|
2025-12-22 20:36:29 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-07 11:42:41 +01:00
|
|
|
|
it("inserts usage summary beneath context line", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
agent: { model: "anthropic/claude-opus-4-5", contextTokens: 32_000 },
|
|
|
|
|
|
sessionEntry: { sessionId: "u1", updatedAt: 0, totalTokens: 1000 },
|
|
|
|
|
|
sessionKey: "agent:main:main",
|
|
|
|
|
|
sessionScope: "per-sender",
|
|
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
|
|
|
|
|
usageLine: "📊 Usage: Claude 80% left (5h)",
|
2026-01-09 02:21:17 +00:00
|
|
|
|
modelAuth: "api-key",
|
2026-01-07 11:42:41 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-09 16:49:01 +01:00
|
|
|
|
const lines = normalizeTestText(text).split("\n");
|
|
|
|
|
|
const contextIndex = lines.findIndex((line) => line.includes("Context:"));
|
2026-01-09 03:14:39 +00:00
|
|
|
|
expect(contextIndex).toBeGreaterThan(-1);
|
2026-01-09 16:49:01 +01:00
|
|
|
|
expect(lines[contextIndex + 1]).toContain("Usage: Claude 80% left (5h)");
|
2026-01-09 03:14:39 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("hides cost when not using an API key", () => {
|
|
|
|
|
|
const text = buildStatusMessage({
|
|
|
|
|
|
config: {
|
|
|
|
|
|
models: {
|
|
|
|
|
|
providers: {
|
|
|
|
|
|
anthropic: {
|
|
|
|
|
|
models: [
|
|
|
|
|
|
{
|
|
|
|
|
|
id: "claude-opus-4-5",
|
|
|
|
|
|
cost: {
|
|
|
|
|
|
input: 1,
|
|
|
|
|
|
output: 1,
|
|
|
|
|
|
cacheRead: 0,
|
|
|
|
|
|
cacheWrite: 0,
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-02-17 14:32:18 +09:00
|
|
|
|
} as unknown as OpenClawConfig,
|
2026-01-09 03:14:39 +00:00
|
|
|
|
agent: { model: "anthropic/claude-opus-4-5" },
|
|
|
|
|
|
sessionEntry: { sessionId: "c1", updatedAt: 0, inputTokens: 10 },
|
|
|
|
|
|
sessionKey: "agent:main:main",
|
|
|
|
|
|
sessionScope: "per-sender",
|
|
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
|
|
|
|
|
modelAuth: "oauth",
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(text).not.toContain("💵 Cost:");
|
2026-01-07 11:42:41 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-15 15:32:20 +00:00
|
|
|
|
function writeTranscriptUsageLog(params: {
|
|
|
|
|
|
dir: string;
|
|
|
|
|
|
agentId: string;
|
|
|
|
|
|
sessionId: string;
|
|
|
|
|
|
usage: {
|
|
|
|
|
|
input: number;
|
|
|
|
|
|
output: number;
|
|
|
|
|
|
cacheRead: number;
|
|
|
|
|
|
cacheWrite: number;
|
|
|
|
|
|
totalTokens: number;
|
|
|
|
|
|
};
|
|
|
|
|
|
}) {
|
|
|
|
|
|
const logPath = path.join(
|
|
|
|
|
|
params.dir,
|
|
|
|
|
|
".openclaw",
|
|
|
|
|
|
"agents",
|
|
|
|
|
|
params.agentId,
|
|
|
|
|
|
"sessions",
|
|
|
|
|
|
`${params.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: params.usage,
|
|
|
|
|
|
},
|
|
|
|
|
|
}),
|
|
|
|
|
|
].join("\n"),
|
|
|
|
|
|
"utf-8",
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-16 14:52:09 +00:00
|
|
|
|
const baselineTranscriptUsage = {
|
|
|
|
|
|
input: 1,
|
|
|
|
|
|
output: 2,
|
|
|
|
|
|
cacheRead: 1000,
|
|
|
|
|
|
cacheWrite: 0,
|
|
|
|
|
|
totalTokens: 1003,
|
|
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
|
|
|
|
function writeBaselineTranscriptUsageLog(params: {
|
|
|
|
|
|
dir: string;
|
|
|
|
|
|
agentId: string;
|
|
|
|
|
|
sessionId: string;
|
|
|
|
|
|
}) {
|
|
|
|
|
|
writeTranscriptUsageLog({
|
|
|
|
|
|
...params,
|
|
|
|
|
|
usage: baselineTranscriptUsage,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function buildTranscriptStatusText(params: { sessionId: string; sessionKey: string }) {
|
|
|
|
|
|
return buildStatusMessage({
|
|
|
|
|
|
agent: {
|
|
|
|
|
|
model: "anthropic/claude-opus-4-5",
|
|
|
|
|
|
contextTokens: 32_000,
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionEntry: {
|
|
|
|
|
|
sessionId: params.sessionId,
|
|
|
|
|
|
updatedAt: 0,
|
|
|
|
|
|
totalTokens: 3,
|
|
|
|
|
|
contextTokens: 32_000,
|
|
|
|
|
|
},
|
|
|
|
|
|
sessionKey: params.sessionKey,
|
|
|
|
|
|
sessionScope: "per-sender",
|
|
|
|
|
|
queue: { mode: "collect", depth: 0 },
|
|
|
|
|
|
includeTranscriptUsage: true,
|
|
|
|
|
|
modelAuth: "api-key",
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-17 11:29:04 +01:00
|
|
|
|
it("prefers cached prompt tokens from the session log", async () => {
|
2026-01-09 16:39:02 +01:00
|
|
|
|
await withTempHome(
|
|
|
|
|
|
async (dir) => {
|
|
|
|
|
|
const sessionId = "sess-1";
|
2026-02-16 14:52:09 +00:00
|
|
|
|
writeBaselineTranscriptUsageLog({
|
2026-01-09 16:39:02 +01:00
|
|
|
|
dir,
|
2026-02-15 15:32:20 +00:00
|
|
|
|
agentId: "main",
|
|
|
|
|
|
sessionId,
|
|
|
|
|
|
});
|
2025-12-12 23:22:05 +00:00
|
|
|
|
|
2026-02-16 14:52:09 +00:00
|
|
|
|
const text = buildTranscriptStatusText({
|
|
|
|
|
|
sessionId,
|
2026-01-09 16:39:02 +01:00
|
|
|
|
sessionKey: "agent:main:main",
|
|
|
|
|
|
});
|
2025-12-12 23:22:05 +00:00
|
|
|
|
|
2026-01-09 16:49:01 +01:00
|
|
|
|
expect(normalizeTestText(text)).toContain("Context: 1.0k/32k");
|
2026-01-09 16:39:02 +01:00
|
|
|
|
},
|
2026-01-30 03:15:10 +01:00
|
|
|
|
{ prefix: "openclaw-status-" },
|
2026-01-09 16:39:02 +01:00
|
|
|
|
);
|
2025-12-12 23:22:05 +00:00
|
|
|
|
});
|
2026-02-12 23:23:12 -05:00
|
|
|
|
|
|
|
|
|
|
it("reads transcript usage for non-default agents", async () => {
|
|
|
|
|
|
await withTempHome(
|
|
|
|
|
|
async (dir) => {
|
|
|
|
|
|
const sessionId = "sess-worker1";
|
2026-02-16 14:52:09 +00:00
|
|
|
|
writeBaselineTranscriptUsageLog({
|
2026-02-12 23:23:12 -05:00
|
|
|
|
dir,
|
2026-02-15 15:32:20 +00:00
|
|
|
|
agentId: "worker1",
|
|
|
|
|
|
sessionId,
|
|
|
|
|
|
});
|
2026-02-12 23:23:12 -05:00
|
|
|
|
|
2026-02-16 14:52:09 +00:00
|
|
|
|
const text = buildTranscriptStatusText({
|
|
|
|
|
|
sessionId,
|
2026-02-12 23:23:12 -05:00
|
|
|
|
sessionKey: "agent:worker1:telegram:12345",
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(normalizeTestText(text)).toContain("Context: 1.0k/32k");
|
|
|
|
|
|
},
|
|
|
|
|
|
{ prefix: "openclaw-status-" },
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
2026-02-13 14:17:24 +01:00
|
|
|
|
|
|
|
|
|
|
it("reads transcript usage using explicit agentId when sessionKey is missing", async () => {
|
|
|
|
|
|
await withTempHome(
|
|
|
|
|
|
async (dir) => {
|
|
|
|
|
|
const sessionId = "sess-worker2";
|
2026-02-15 15:32:20 +00:00
|
|
|
|
writeTranscriptUsageLog({
|
2026-02-13 14:17:24 +01:00
|
|
|
|
dir,
|
2026-02-15 15:32:20 +00:00
|
|
|
|
agentId: "worker2",
|
|
|
|
|
|
sessionId,
|
|
|
|
|
|
usage: {
|
|
|
|
|
|
input: 2,
|
|
|
|
|
|
output: 3,
|
|
|
|
|
|
cacheRead: 1200,
|
|
|
|
|
|
cacheWrite: 0,
|
|
|
|
|
|
totalTokens: 1205,
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
2026-02-13 14:17:24 +01:00
|
|
|
|
|
2026-02-13 15:07:28 +00:00
|
|
|
|
const text = buildStatusMessage({
|
2026-02-13 14:17:24 +01:00
|
|
|
|
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-" },
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
2025-12-07 16:53:19 +00:00
|
|
|
|
});
|
2026-01-08 16:02:54 +01:00
|
|
|
|
|
|
|
|
|
|
describe("buildCommandsMessage", () => {
|
2026-02-13 07:47:25 +05:30
|
|
|
|
it("lists commands with aliases and hints", () => {
|
2026-01-11 02:17:10 +01:00
|
|
|
|
const text = buildCommandsMessage({
|
|
|
|
|
|
commands: { config: false, debug: false },
|
2026-02-17 14:32:18 +09:00
|
|
|
|
} as unknown as OpenClawConfig);
|
2026-01-27 02:35:09 -05:00
|
|
|
|
expect(text).toContain("ℹ️ Slash commands");
|
|
|
|
|
|
expect(text).toContain("Status");
|
2026-01-08 16:02:54 +01:00
|
|
|
|
expect(text).toContain("/commands - List all slash commands.");
|
2026-01-20 13:19:55 +00:00
|
|
|
|
expect(text).toContain("/skill - Run a skill by name.");
|
2026-01-27 10:18:53 +08:00
|
|
|
|
expect(text).toContain("/think (/thinking, /t) - Set thinking level.");
|
2026-02-13 07:47:25 +05:30
|
|
|
|
expect(text).toContain("/compact - Compact the session context.");
|
2026-01-11 02:17:10 +01:00
|
|
|
|
expect(text).not.toContain("/config");
|
|
|
|
|
|
expect(text).not.toContain("/debug");
|
|
|
|
|
|
});
|
2026-01-16 12:10:20 +00:00
|
|
|
|
|
|
|
|
|
|
it("includes skill commands when provided", () => {
|
|
|
|
|
|
const text = buildCommandsMessage(
|
|
|
|
|
|
{
|
|
|
|
|
|
commands: { config: false, debug: false },
|
2026-02-17 14:32:18 +09:00
|
|
|
|
} as unknown as OpenClawConfig,
|
2026-01-16 12:10:20 +00:00
|
|
|
|
[
|
|
|
|
|
|
{
|
|
|
|
|
|
name: "demo_skill",
|
|
|
|
|
|
skillName: "demo-skill",
|
|
|
|
|
|
description: "Demo skill",
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
);
|
|
|
|
|
|
expect(text).toContain("/demo_skill - Demo skill");
|
|
|
|
|
|
});
|
2026-01-11 02:17:10 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe("buildHelpMessage", () => {
|
|
|
|
|
|
it("hides config/debug when disabled", () => {
|
|
|
|
|
|
const text = buildHelpMessage({
|
|
|
|
|
|
commands: { config: false, debug: false },
|
2026-02-17 14:32:18 +09:00
|
|
|
|
} as unknown as OpenClawConfig);
|
2026-01-27 10:18:53 +08:00
|
|
|
|
expect(text).toContain("Skills");
|
|
|
|
|
|
expect(text).toContain("/skill <name> [input]");
|
2026-01-11 02:17:10 +01:00
|
|
|
|
expect(text).not.toContain("/config");
|
|
|
|
|
|
expect(text).not.toContain("/debug");
|
2026-01-08 16:02:54 +01:00
|
|
|
|
});
|
|
|
|
|
|
});
|
2026-01-27 02:35:09 -05:00
|
|
|
|
|
|
|
|
|
|
describe("buildCommandsMessagePaginated", () => {
|
|
|
|
|
|
it("formats telegram output with pages", () => {
|
|
|
|
|
|
const result = buildCommandsMessagePaginated(
|
|
|
|
|
|
{
|
|
|
|
|
|
commands: { config: false, debug: false },
|
2026-02-17 14:32:18 +09:00
|
|
|
|
} as unknown as OpenClawConfig,
|
2026-01-27 02:35:09 -05:00
|
|
|
|
undefined,
|
|
|
|
|
|
{ surface: "telegram", page: 1 },
|
|
|
|
|
|
);
|
|
|
|
|
|
expect(result.text).toContain("ℹ️ Commands (1/");
|
|
|
|
|
|
expect(result.text).toContain("Session");
|
|
|
|
|
|
expect(result.text).toContain("/stop - Stop the current run.");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("includes plugin commands in the paginated list", () => {
|
|
|
|
|
|
listPluginCommands.mockReturnValue([
|
|
|
|
|
|
{ name: "plugin_cmd", description: "Plugin command", pluginId: "demo-plugin" },
|
|
|
|
|
|
]);
|
|
|
|
|
|
const result = buildCommandsMessagePaginated(
|
|
|
|
|
|
{
|
|
|
|
|
|
commands: { config: false, debug: false },
|
2026-02-17 14:32:18 +09:00
|
|
|
|
} as unknown as OpenClawConfig,
|
2026-01-27 02:35:09 -05:00
|
|
|
|
undefined,
|
|
|
|
|
|
{ surface: "telegram", page: 99 },
|
|
|
|
|
|
);
|
|
|
|
|
|
expect(result.text).toContain("Plugins");
|
|
|
|
|
|
expect(result.text).toContain("/plugin_cmd (demo-plugin) - Plugin command");
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|