Skip to content
Open
Show file tree
Hide file tree
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
34 changes: 34 additions & 0 deletions packages/cli/src/tests/context-provider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { createContextStatusProvider } from "../ui/statusline/context-provider";

test("context statusline provider reports context usage percentage (#234)", async () => {
const provider = createContextStatusProvider("ctx", undefined, false, undefined);
const text = await provider.fetch({
projectRoot: "/tmp",
signal: new AbortController().signal,
getSessionInfo: () => ({
activeSessionId: "s1",
messageCount: 10,
requestCount: 2,
totalTokens: 100000,
activeTokens: 131072,
maxContextTokens: 1024 * 1024,
model: "deepseek-v4-flash",
thinkingEnabled: false,
reasoningEffort: "high",
toolUsage: {},
}),
});
assert.equal(text, "ctx 13%");
});

test("context statusline provider returns empty without an active session (#234)", async () => {
const provider = createContextStatusProvider("ctx");
const text = await provider.fetch({
projectRoot: "/tmp",
signal: new AbortController().signal,
getSessionInfo: () => null,
});
assert.equal(text, "");
});
27 changes: 27 additions & 0 deletions packages/cli/src/ui/statusline/context-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { StatusProvider } from "./types";

/**
* Built-in statusline provider that shows the current session's context-window
* usage as a percentage, e.g. `ctx 12%`. (#234)
*/
export function createContextStatusProvider(
id: string,
color?: string,
newLine?: boolean,
maxLength?: number
): StatusProvider {
return {
id,
color,
newLine,
maxLength,
fetch: async (ctx) => {
const info = ctx.getSessionInfo?.();
if (!info || !info.activeSessionId || info.maxContextTokens <= 0) {
return "";
}
const percent = Math.min(100, Math.round((info.activeTokens / info.maxContextTokens) * 100));
return `ctx ${percent}%`;
},
};
}
4 changes: 4 additions & 0 deletions packages/cli/src/ui/statusline/manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ResolvedStatusLineSettings, StatusLineProviderConfig } from "@vegamo/deepcode-core";
import { sanitizeStatusText } from "./sanitize";
import { createCommandStatusProvider } from "./command-provider";
import { createContextStatusProvider } from "./context-provider";
import { loadModuleProvider, validateModulePath } from "./module-provider";
import type { SessionInfo, StatusProvider, StatusSegment } from "./types";

Expand Down Expand Up @@ -147,6 +148,9 @@ export class StatusLineManager {
}
return provider;
}
if (config.type === "context") {
return createContextStatusProvider(providerId, config.color, config.newLine, config.maxLength);
}
return null;
}

Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ export type StatusLineProviderConfig =
color?: string;
newLine?: boolean;
maxLength?: number;
}
| {
type: "context";
id?: string;
color?: string;
newLine?: boolean;
maxLength?: number;
};

export type StatusLineSettings = {
Expand Down Expand Up @@ -357,6 +364,15 @@ function normalizeStatusLineProvider(value: unknown): StatusLineProviderConfig |
maxLength,
};
}
if (type === "context") {
return {
type: "context",
id,
color,
newLine,
maxLength,
};
}
return null;
}

Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/tests/settings-and-notify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ import { applyModelConfigSelection, resolveSettings, resolveSettingsSources } fr

const TEST_PROCESS_ENV = {};

test("resolveSettingsSources normalizes the statusline context provider (#234)", () => {
const resolved = resolveSettingsSources(
{
statusline: { enabled: true, providers: [{ type: "context" }] },
env: { API_KEY: "sk-test" },
},
null,
{ model: "default-model", baseURL: "https://default.example.com" },
TEST_PROCESS_ENV
);
assert.equal(resolved.statusline.providers.length, 1);
assert.equal(resolved.statusline.providers[0]?.type, "context");
});

test("resolveSettings reads top-level thinkingEnabled, notify, and webSearchTool", () => {
const resolved = resolveSettings(
{
Expand Down