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
20 changes: 10 additions & 10 deletions coverage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
ℹ compaction.js | 98.84 | 78.43 | 100.00 | 89-90
ℹ contextBackend.js | 100.00 | 100.00 | 100.00 |
ℹ coreBackend.js | 100.00 | 100.00 | 100.00 |
ℹ deepAgents.js | 94.80 | 69.70 | 77.78 | 155-156 180 196-198 308-315 344-345 350-351 458-460 531-535 543-545
ℹ deepAgents.js | 93.91 | 70.83 | 77.78 | 155-156 180 196-198 308-315 344-345 458-460 474-480 531-535 543-545
ℹ evictionCeiling.js | 100.00 | 100.00 | 100.00 |
ℹ skillsMiddleware.js | 100.00 | 87.50 | 100.00 |
ℹ config | | | |
Expand Down Expand Up @@ -60,8 +60,8 @@
ℹ copilotAuth.js | 96.80 | 78.87 | 90.00 | 12-13 131-132 196-197 237-239 352-353 378-379 391-393 484 565-566 575-576
ℹ copilotRetryMiddleware.js | 88.52 | 79.49 | 83.33 | 75-88
ℹ index.js | 100.00 | 100.00 | 100.00 |
ℹ modelInfo.js | 98.17 | 96.39 | 100.00 | 128-129 179-180
ℹ openai.js | 92.72 | 74.65 | 78.57 | 65 90-92 138-139 145-146 233 236-238 257-259 267-271 279-280 292-295
ℹ modelInfo.js | 97.25 | 93.98 | 100.00 | 128-129 131-132 179-180
ℹ openai.js | 92.72 | 75.71 | 78.57 | 65 90-92 138-139 145-146 233 236-238 257-259 267-271 279-280 292-295
ℹ tokenBudget.js | 97.14 | 85.71 | 82.35 | 50-51 83-84
ℹ tokenBudgetMiddleware.js | 98.10 | 78.67 | 87.50 | 94-95 97-98
ℹ sandbox | | | |
Expand Down Expand Up @@ -186,7 +186,7 @@
ℹ xlsxParser.js | 82.21 | 83.33 | 88.89 | 47-48 93-112 130-137 150-156
ℹ zipExtractor.js | 82.32 | 77.78 | 66.67 | 62-63 104-107 124-142 149-152
ℹ git | | | |
ℹ index.js | 87.41 | 64.13 | 83.69 | 324-326 379-380 393-396 481-483 515-516 626-643 689-691 722-723 755-757 821-824 840-843 866-872 906 908-912 914-917 929-933 976-980 984 989-991 1006-1015 1024 1038-1044 1046-1049 1057-1063 1086-1091 1093-1099 1101-1110 1112-1115 1122-1126 1136-1143 1191-1192 1194-1199 1291-1292 1314-1315 1323-1324 1331-1332 1334-1340 1362 1366-1368 1373-1376 1428-1435 1439-1447 1450-1453 1455-1458
ℹ index.js | 87.41 | 64.37 | 83.69 | 324-326 379-380 393-396 481-483 515-516 626-643 689-691 722-723 755-757 821-824 840-843 866-872 906 908-912 914-917 929-933 976-980 984 989-991 1006-1015 1024 1038-1044 1046-1049 1057-1063 1086-1091 1093-1099 1101-1110 1112-1115 1122-1126 1136-1143 1191-1192 1194-1199 1291-1292 1314-1315 1323-1324 1331-1332 1334-1340 1362 1366-1368 1373-1376 1428-1435 1439-1447 1450-1453 1455-1458
ℹ graphql | | | |
ℹ index.js | 97.85 | 98.13 | 93.75 | 57-68
ℹ image | | | |
Expand Down Expand Up @@ -250,20 +250,20 @@
ℹ conversationPanel.js | 95.45 | 95.83 | 100.00 | 26-31
ℹ filePicker.js | 69.72 | 89.66 | 73.33 | 143-168 194-195 200-244 267-279
ℹ headerClick.js | 99.02 | 96.55 | 100.00 | 99
ℹ inputArea.js | 81.41 | 75.51 | 46.43 | 160-161 175 189-192 200-212 220-236 240-241 247-248 251-256 324-332 336-340 344-348
ℹ inputArea.js | 81.41 | 81.25 | 46.43 | 160-161 175 189-192 200-212 220-236 240-241 247-248 251-256 324-332 336-340 344-348
ℹ inputPanel.js | 100.00 | 100.00 | 100.00 |
ℹ linkHit.js | 100.00 | 91.67 | 100.00 |
ℹ markdownText.js | 97.50 | 86.87 | 96.15 | 76 206-212 236-238
ℹ memoryPanel.js | 8.80 | 100.00 | 0.00 | 20-216
ℹ messageBubble.js | 95.53 | 93.15 | 84.00 | 369 373-391 415-420 641-644 656-659
ℹ messageList.js | 83.16 | 75.00 | 46.94 | 164 181-184 225-230 272-303 312 319-324 382 419-444 454-456 463-465 473 481 489 498-502 511-518 526 534 542 552 561-567 576 584-589 611-613 702-712 759-760
ℹ messageBubble.js | 98.55 | 94.30 | 90.20 | 415-420 642 656-659
ℹ messageList.js | 88.99 | 86.17 | 65.31 | 164 319-324 419-444 454-456 463-465 473 481 489 498-502 511-518 526 534 552 561-567 584-589 611-613 702-712 759-760
ℹ messages.js | 100.00 | 100.00 | 100.00 |
ℹ openUrl.js | 100.00 | 100.00 | 100.00 |
ℹ panels.js | 100.00 | 100.00 | 100.00 |
ℹ projectsPanel.js | 26.83 | 100.00 | 80.00 | 34-123
ℹ quotes.js | 96.97 | 87.50 | 100.00 | 62-63
ℹ quotes.js | 100.00 | 100.00 | 100.00 |
ℹ sanitize.js | 100.00 | 100.00 | 100.00 |
ℹ scrollView.js | 98.83 | 97.73 | 73.91 | 197-199
ℹ scrollView.js | 100.00 | 97.83 | 79.17 |
ℹ selectComponents.js | 100.00 | 100.00 | 100.00 |
ℹ selectionLayout.js | 100.00 | 95.00 | 100.00 |
ℹ selectMode.js | 100.00 | 100.00 | 100.00 |
Expand All @@ -285,6 +285,6 @@
ℹ loadAgents.js | 74.29 | 78.57 | 50.00 | 25-26 41 56-70
ℹ project.js | 100.00 | 100.00 | 100.00 |
ℹ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ℹ all files | 93.39 | 86.69 | 88.51 |
ℹ all files | 93.56 | 86.87 | 89.14 |
ℹ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ℹ end of coverage report
19 changes: 18 additions & 1 deletion src/tui/conversationArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,8 @@ const ConversationArea = forwardRef(function ConversationArea(
onContextUpdate,
completedToolCalls = {},
) => {
// Reasoning text streamed this turn; it is sent back as context.
let streamedReasoning = "";
// Debounced context size update — coalesces rapid chunks into a single
// token calculation every ~200ms so the status bar stays responsive.
const debouncedContextUpdate = (content) => {
Expand All @@ -908,7 +910,9 @@ const ConversationArea = forwardRef(function ConversationArea(
pendingContextRef.current.content = content;
contextUpdateTimerRef.current = setTimeout(async () => {
contextUpdateTimerRef.current = null;
const text = pendingContextRef.current.content;
const text = [streamedReasoning, pendingContextRef.current.content]
.filter(Boolean)
.join("\n");
const cached = tokenCacheRef.current;
if (text && cached.content !== text) {
cached.content = text;
Expand Down Expand Up @@ -987,6 +991,8 @@ const ConversationArea = forwardRef(function ConversationArea(
if (event.type === "reasoning") {
const reasoningText = event.data?.text || event.text || "";
if (reasoningText) {
streamedReasoning += reasoningText;
debouncedContextUpdate(committedContentRef.current);
if (
!(
committedReasoningRef.current.endsWith(".") ||
Expand Down Expand Up @@ -1026,6 +1032,8 @@ const ConversationArea = forwardRef(function ConversationArea(
}
if (event.data?.chunk?.reasoning) {
const reasoningChunk = event.data.chunk.reasoning;
streamedReasoning += reasoningChunk;
debouncedContextUpdate(committedContentRef.current);
if (
!(
committedReasoningRef.current.endsWith(".") ||
Expand All @@ -1046,6 +1054,15 @@ const ConversationArea = forwardRef(function ConversationArea(
}

if (event.type === "on_tool_start") {
// The call's name and arguments are sent back as context.
let callText = event.name || "";
try {
callText = `${callText} ${JSON.stringify(event.data?.input ?? "")}`.trim();
} catch {
// keep the name only
}
toolMessageTokensRef.current += await countToolMessageTokens(model, callText);
debouncedContextUpdate(committedContentRef.current);
messageListRef.current?.updateMessage(streamingMsgIdRef.current, {
activeToolCall: {
name: event.name,
Expand Down
141 changes: 141 additions & 0 deletions tests/integration/tui-status-bar-context.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* Integration test: the rendered status bar reflects the live context counter.
* Mounts ConversationArea and InputArea wired the way App wires them
* (ConversationArea.onContextChange -> InputArea.setContextSize), streams events,
* and reads the frames Ink actually writes.
*/

import { describe, it, afterEach } from "node:test";
import assert from "node:assert";
import React from "react";
import { EventEmitter } from "node:events";
import { render } from "ink";
import ConversationArea from "../../src/tui/conversationArea.js";
import InputArea from "../../src/tui/inputArea.js";

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const words = (n) => Array.from({ length: n }, (_, i) => `w${i}`).join(" ");

/** Minimal TTY-like stdin that supports Ink raw mode. */
function makeStdin() {
const s = new EventEmitter();
s.isTTY = true;
s.isRaw = false;
s.setRawMode = (v) => {
s.isRaw = v;
};
s.setEncoding = () => {};
s.resume = () => {};
s.pause = () => {};
s.ref = () => {};
s.unref = () => {};
s.read = () => null;
s.readableEncoding = null;
return s;
}

/** TTY-like stdout that records frames. */
function makeStdout() {
const s = new EventEmitter();
s.isTTY = true;
s.columns = 120;
s.rows = 40;
s.frames = [];
s.write = (d) => {
s.frames.push(String(d));
return true;
};
return s;
}

/** One token per whitespace-separated word. */
const model = {
getNumTokensFromMessages: async (msgs) => ({
totalCount: msgs.reduce(
(n, m) =>
n +
String(m.content ?? "")
.split(/\s+/)
.filter(Boolean).length,
0,
),
}),
};

let instance;
afterEach(() => {
instance?.unmount();
instance = undefined;
});

/**
* Mount both components wired like App and send one turn emitting `events`.
* @param {Array<Object>} events - Stream events the provider emits
* @returns {Promise<{stdout: Object, reported: number[]}>} Output frames and reported sizes
*/
async function runWired(events) {
const stdout = makeStdout();
const stdin = makeStdin();
const reported = [];
const convRef = React.createRef();
const inputRef = React.createRef();
const dispatchProvider = async (_text, _provider, handler) => {
for (const event of events) await handler(event);
await sleep(150);
return { content: "", lastEventType: "message" };
};
const messageCountRef = { current: 0 };
const Wired = () =>
React.createElement(
React.Fragment,
null,
React.createElement(ConversationArea, {
ref: convRef,
config: { tui: { name: "madz" } },
model,
dispatchProvider,
messageCountRef,
contextWindow: 1000,
onContextChange: (n) => {
reported.push(n);
inputRef.current?.setContextSize(n);
},
onCompactingChange: () => {},
onInterruptInput: () => {},
onQuit: () => {},
onNewSession: () => {},
}),
React.createElement(InputArea, {
ref: inputRef,
onSubmit: () => {},
messageCountRef,
contextWindow: 1000,
appInfo: { version: "0", model: "m" },
statusBar: {},
}),
);
instance = render(React.createElement(Wired), { stdout, stdin, debug: true });
await sleep(100);
await convRef.current.handleChat("hi");
await sleep(300);
return { stdout, reported };
}

describe("status bar reflects the live context counter", () => {
it("shows the context meter after streamed reasoning", async () => {
const { stdout, reported } = await runWired([{ type: "reasoning", text: words(500) }]);
assert.strictEqual(reported.at(-1), 500);
const last = stdout.frames.filter((f) => f.includes("%")).at(-1) || "";
assert.match(last, /50%/, `status bar did not show 50%; last frame:\n${last}`);
});

it("shows the context meter after a tool call and result", async () => {
const { stdout, reported } = await runWired([
{ type: "on_tool_start", name: "readFile", data: { input: { path: "/x" } } },
{ type: "tool", text: words(298), name: "readFile" },
]);
assert.strictEqual(reported.at(-1), 300);
const last = stdout.frames.filter((f) => f.includes("%")).at(-1) || "";
assert.match(last, /30%/, `status bar did not show 30%; last frame:\n${last}`);
});
});
102 changes: 102 additions & 0 deletions tests/unit/tui/conversationAreaContext.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { describe, it } from "node:test";
import assert from "node:assert";
import React from "react";
import { render } from "ink";
import ConversationArea from "../../../src/tui/conversationArea.js";

const words = (n) => Array.from({ length: n }, (_, i) => `w${i}`).join(" ");
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

/** Deterministic tokenizer: one token per whitespace-separated word. */
const model = {
getNumTokensFromMessages: async (msgs) => ({
totalCount: msgs.reduce(
(n, m) =>
n +
String(m.content ?? "")
.split(/\s+/)
.filter(Boolean).length,
0,
),
}),
};

/**
* Mount ConversationArea, send one chat turn whose provider emits the given
* stream events, and return every value reported to onContextChange.
* @param {Array<Object>} events - Stream events the provider emits
* @returns {Promise<number[]>} Context sizes reported during the turn
*/
async function runTurnWithEvents(events) {
const reported = [];
const ref = React.createRef();
const dispatchProvider = async (_text, _provider, handler) => {
for (const event of events) await handler(event);
await sleep(150);
return { content: "", lastEventType: "message" };
};
const { unmount } = render(
React.createElement(ConversationArea, {
ref,
config: { tui: { name: "madz" } },
model,
dispatchProvider,
messageCountRef: { current: 0 },
onContextChange: (n) => reported.push(n),
onCompactingChange: () => {},
onInterruptInput: () => {},
onQuit: () => {},
onNewSession: () => {},
contextWindow: 10000,
}),
);
try {
await sleep(50);
await ref.current.handleChat("hi");
await sleep(250);
} finally {
unmount();
}
return reported;
}

describe("ConversationArea live context counter", () => {
it("counts streamed message text", async () => {
const reported = await runTurnWithEvents([{ type: "message", text: words(30) }]);
assert.strictEqual(reported.at(-1), 30);
});

it("counts streamed reasoning text", async () => {
const reported = await runTurnWithEvents([{ type: "reasoning", text: words(100) }]);
assert.strictEqual(reported.at(-1), 100);
});

it("counts reasoning and message text together", async () => {
const reported = await runTurnWithEvents([
{ type: "reasoning", text: words(40) },
{ type: "message", text: words(30) },
]);
assert.strictEqual(reported.at(-1), 70);
});

it("counts tool result text", async () => {
const reported = await runTurnWithEvents([{ type: "tool", text: words(50), name: "readFile" }]);
assert.strictEqual(reported.at(-1), 50);
});

it("counts the tool call name and arguments", async () => {
const reported = await runTurnWithEvents([
{ type: "on_tool_start", name: "readFile", data: { input: { path: "/x" } } },
]);
// `readFile {"path":"/x"}` is two whitespace-separated tokens.
assert.strictEqual(reported.at(-1), 2);
});

it("counts tool call and tool result together", async () => {
const reported = await runTurnWithEvents([
{ type: "on_tool_start", name: "readFile", data: { input: { path: "/x" } } },
{ type: "tool", text: words(50), name: "readFile" },
]);
assert.strictEqual(reported.at(-1), 52);
});
});
Loading