Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

feat:新增/usage命令查看当前会话使用token情况

openJiuwen-bot81fa40ec380a
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/help.ts b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/help.ts
index fced176ba6..63850a9483 100644
--- a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/help.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/help.ts
@@ -28,7 +28,7 @@ const COMMAND_GROUPS: Record<string, { name: string; commands: string[] }> = {
},
config: {
name: "Config",
- commands: ["config", "permissions", "workspace", "diff", "plan"],
+ commands: ["config", "permissions", "workspace", "diff", "plan", "usage"],
},
};
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/usage.ts b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/usage.ts
new file mode 100644
index 0000000000..cf74e3642d
--- /dev/null
+++ b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/usage.ts
@@ -0,0 +1,53 @@
+import { addInfo, addError } from "../helpers.js";
+import { CommandKind, type SlashCommand } from "../types.js";
+
+function showUsage(ctx: import("../types.js").CommandContext): void {
+ const summary = ctx.getUsageSummary();
+ const fmt = (n: number) => n.toLocaleString("en-US");
+
+ const items = [
+ { label: "input_tokens", value: fmt(summary.total_input_tokens) },
+ { label: "output_tokens", value: fmt(summary.total_output_tokens) },
+ { label: "total_tokens", value: fmt(summary.total_tokens) },
+ ];
+
+ if (summary.byModel.length > 0) {
+ for (const entry of summary.byModel) {
+ items.push(
+ { label: `model: ${entry.model}`, value: `${fmt(entry.total_tokens)} tokens` },
+ { label: ` input`, value: fmt(entry.input_tokens) },
+ { label: ` output`, value: fmt(entry.output_tokens) },
+ );
+ }
+ }
+
+ ctx.addItem(
+ addInfo(ctx.sessionId, "Session usage", "u", {
+ view: "kv",
+ title: "Usage",
+ items,
+ }),
+ );
+}
+
+export function createUsageCommand(): SlashCommand {
+ return {
+ name: "usage",
+ description: "Show session token usage (input / output / total)",
+ usage: "/usage",
+ example: "/usage",
+ kind: CommandKind.BUILT_IN,
+ action: async (ctx) => {
+ if (ctx.enterStatusView) {
+ ctx.enterStatusView("usage");
+ return;
+ }
+ try {
+ showUsage(ctx);
+ } catch (error) {
+ const message = error instanceof Error ? error.message : String(error);
+ ctx.addItem(addError(ctx.sessionId, `usage failed: ${message}`));
+ }
+ },
+ };
+}
\ No newline at end of file
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/commands/registry.ts b/jiuwenswarm/channels/tui/frontend/src/core/commands/registry.ts
index cf2b44c20f..a50fb2d074 100644
--- a/jiuwenswarm/channels/tui/frontend/src/core/commands/registry.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/core/commands/registry.ts
@@ -39,6 +39,7 @@ import { createTeamSkillsCommand } from "./builtins/teamskills.js";
import { createAutoHarnessCommand } from "./builtins/auto-harness.js";
import { createThemeCommand } from "./builtins/theme.js";
import { createWorkspaceCommand } from "./builtins/workspace-dir.js";
+import { createUsageCommand } from "./builtins/usage.js";
export function createBuiltinCommands(): SlashCommand[] {
const commands: SlashCommand[] = [
@@ -77,6 +78,7 @@ export function createBuiltinCommands(): SlashCommand[] {
createAutoHarnessCommand(),
createThemeCommand(),
createWorkspaceCommand(),
+ createUsageCommand(),
createHotkeyCommand(),
createMemoryCommand(),
];