diff --git a/coverage.txt b/coverage.txt index 28c54fc2..0fd20295 100644 --- a/coverage.txt +++ b/coverage.txt @@ -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 | 93.91 | 70.83 | 77.78 | 155-156 180 196-198 308-315 344-345 458-460 474-480 531-535 543-545 +ℹ deepAgents.js | 93.91 | 69.39 | 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 | | | | @@ -94,7 +94,7 @@ ℹ types.js | 100.00 | 100.00 | 100.00 | ℹ validator.js | 100.00 | 100.00 | 100.00 | ℹ stream | | | | -ℹ consume.js | 96.48 | 98.36 | 100.00 | 156-162 +ℹ consume.js | 96.80 | 98.53 | 100.00 | 176-182 ℹ interrupt.js | 100.00 | 100.00 | 100.00 | ℹ loopCheck.js | 100.00 | 100.00 | 100.00 | ℹ loopDetector.js | 100.00 | 100.00 | 100.00 | @@ -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 | | | | @@ -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.38 | 86.70 | 88.51 | +ℹ all files | 93.38 | 86.71 | 88.51 | ℹ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ℹ end of coverage report diff --git a/index.js b/index.js index 7b295922..100ee08e 100644 --- a/index.js +++ b/index.js @@ -314,14 +314,12 @@ async function runAgent(input, threadExtras, streamingCallback, signal) { }); // A detected loop abandons the stream; the caller (the TUI) sends the silent // nudge, the same way it sends the reasoning-only "Continue". - const { content, reasoning, interrupted, lastEventType, loopDetected } = await consumeStream( - stream, - streamingCallback, - { + const { content, reasoning, interrupted, lastEventType, loopDetected, silentViolation } = + await consumeStream(stream, streamingCallback, { showToolResults, loopDetector, - }, - ); + silent: config.agent?.silent === true, + }); return { provider: providerName, content, @@ -330,6 +328,7 @@ async function runAgent(input, threadExtras, streamingCallback, signal) { interrupted, lastEventType, loopDetected, + silentViolation, }; } diff --git a/src/stream/consume.js b/src/stream/consume.js index 561dd8b8..6208c044 100644 --- a/src/stream/consume.js +++ b/src/stream/consume.js @@ -13,6 +13,9 @@ import { isSubagentNamespace } from "./namespace.js"; * @param {Function} [streamingCallback] - Callback for streamed chunks * @param {Object} [options] - Options * @param {boolean} [options.showToolResults=true] - Whether to emit `tool` events + * @param {boolean} [options.silent=false] - Silent mode (`agent.silent`). When a second + * text-bearing assistant message starts while the turn is still streaming, the stream is + * abandoned before that text is forwarded and `silentViolation` is returned true. * @param {{push: Function, settle?: Function, reset: Function}} [options.loopDetector] - Loop * detector. Each non-subagent message/reasoning delta is pushed to it together with * the stream block's id (the content-block index for messages, `block.id` for @@ -25,7 +28,7 @@ import { isSubagentNamespace } from "./namespace.js"; * id arrives, or a tool result follows) `end()` collects the verdict and releases * the detector's word and sentence lists. The detector is also reset when the * stream itself ends, however it ends. - * @returns {Promise<{content: string, reasoning: string, interrupted: boolean, lastEventType: string | null, loopDetected: boolean}>} + * @returns {Promise<{content: string, reasoning: string, interrupted: boolean, lastEventType: string | null, loopDetected: boolean, silentViolation: boolean}>} */ export async function consumeStream(stream, streamingCallback, options = {}) { try { @@ -42,7 +45,7 @@ export async function consumeStream(stream, streamingCallback, options = {}) { * @param {AsyncIterable} stream - The agent stream (subgraphs mode) * @param {Function} [streamingCallback] - Callback for streamed chunks * @param {Object} options - Options - * @returns {Promise<{content: string, reasoning: string, interrupted: boolean, lastEventType: string | null, loopDetected: boolean}>} + * @returns {Promise<{content: string, reasoning: string, interrupted: boolean, lastEventType: string | null, loopDetected: boolean, silentViolation: boolean}>} */ async function drainStream(stream, streamingCallback, options) { const showToolResults = options.showToolResults; @@ -51,6 +54,10 @@ async function drainStream(stream, streamingCallback, options) { let interrupted = false; let lastEventType = null; let loopDetected = false; + let silentViolation = false; + /** Id of the last assistant message that carried text, and how many have. */ + let lastTextMsgId = null; + let textMessageCount = 0; const loopDetector = options.loopDetector; /** The id shared by the chunks of the message currently streaming. */ let currentMsgId = null; @@ -108,6 +115,19 @@ async function drainStream(stream, streamingCallback, options) { continue; } + // Silent mode: a second text-bearing message in the same turn means the + // assistant is narrating. Stop before its text reaches the bubble. + if (options.silent && !isSubagent && currentMsgId != null) { + if (currentMsgId !== lastTextMsgId) { + lastTextMsgId = currentMsgId; + textMessageCount += 1; + } + if (textMessageCount >= 2) { + silentViolation = true; + break; + } + } + if (!isSubagent) { content += text; const blockIndex = msg?.content?.[0]?.index; @@ -195,5 +215,5 @@ async function drainStream(stream, streamingCallback, options) { // a loop in the final deltas is not missed. if (!loopDetected && loopDetector?.settle) loopDetected = await loopDetector.settle(); - return { content, reasoning, interrupted, lastEventType, loopDetected }; + return { content, reasoning, interrupted, lastEventType, loopDetected, silentViolation }; } diff --git a/src/tui/conversationArea.js b/src/tui/conversationArea.js index 6164cff2..20617110 100644 --- a/src/tui/conversationArea.js +++ b/src/tui/conversationArea.js @@ -78,6 +78,33 @@ export function shouldAutoContinue({ ); } +/** Silent nudge sent when the assistant narrates while `agent.silent` is enabled. */ +export const SILENT_MODE_NUDGE = "Stay quiet, silent mode is enabled"; + +/** + * Decide whether to send the silent-mode nudge after a turn ends. + * + * Fires only when the stream was abandoned for a silent-mode violation (a second + * message streaming in the same turn), the streaming bubble is the last in the + * list, and the turn was not paused by an interrupt. The nudge corrects behavior + * rather than continuing a turn, so it is not counted against `autoContinueLimit`. + * + * @param {Object} params - Decision inputs + * @param {boolean | undefined} params.silentViolation - Whether the stream was abandoned + * @param {boolean} [params.interrupted] - True when the turn paused on an interrupt + * @param {string | undefined} params.streamingMsgId - The streaming bubble's message ID + * @param {string | undefined} params.lastMessageId - The last message ID in the list + * @returns {boolean} True when the silent-mode nudge should be dispatched + */ +export function shouldNudgeSilent({ + silentViolation, + interrupted = false, + streamingMsgId, + lastMessageId, +}) { + return silentViolation === true && !interrupted && streamingMsgId === lastMessageId; +} + /** * Decide whether to send a silent "Continue" after a loop-triggered compaction. * @@ -617,6 +644,21 @@ const ConversationArea = forwardRef(function ConversationArea( completedToolCalls, ); + // Silent mode: the stream was abandoned because a second message started + // streaming. Nudge the assistant to stay quiet. This is not a continuation, + // so it does not count against autoContinueLimit. + if ( + shouldNudgeSilent({ + silentViolation: response?.silentViolation, + interrupted: response?.interrupted, + streamingMsgId: streamingMsgIdRef.current, + lastMessageId: messageListRef.current?.getLastMessageId(), + }) + ) { + await handleChat(SILENT_MODE_NUDGE, { silentUser: true }); + return; + } + // Loop nudge: when the loop detector abandoned the stream, nudge the model // with a silent "You're looping", the same way the reasoning-only // "Continue" below is dispatched. Scoped to the LAST bubble. After diff --git a/tests/unit/stream/silentMode.test.js b/tests/unit/stream/silentMode.test.js new file mode 100644 index 00000000..c8480e66 --- /dev/null +++ b/tests/unit/stream/silentMode.test.js @@ -0,0 +1,112 @@ +/** + * Silent mode (`agent.silent`): when a second text-bearing assistant message + * starts while the turn is still streaming, the stream is abandoned before that + * text is forwarded and `silentViolation` is reported so the TUI can nudge. + */ + +import { describe, it } from "node:test"; +import assert from "node:assert"; +import { AIMessageChunk, ToolMessage } from "@langchain/core/messages"; +import { consumeStream } from "../../../src/stream/consume.js"; +import { shouldNudgeSilent, SILENT_MODE_NUDGE } from "../../../src/tui/conversationArea.js"; + +const ev = (msg, namespace = []) => [namespace, "messages", [msg, {}]]; +const say = (id, text) => new AIMessageChunk({ id, content: text }); +const tool = (id, text) => new ToolMessage({ id, content: text, tool_call_id: "t", name: "x" }); + +const streamOf = (...events) => + (async function* () { + for (const e of events) yield e; + })(); + +describe("consumeStream silent mode", () => { + it("abandons the stream when a second message starts, without forwarding it", async () => { + const seen = []; + const result = await consumeStream( + streamOf(ev(say("A", "first")), ev(tool("T", "ok")), ev(say("B", "second"))), + (e) => seen.push(e), + { silent: true, showToolResults: true }, + ); + assert.strictEqual(result.silentViolation, true); + assert.strictEqual(result.content, "first"); + assert.ok(!seen.some((e) => e.text === "second")); + }); + + it("does not flag chunks of the same message", async () => { + const result = await consumeStream( + streamOf(ev(say("A", "one ")), ev(say("A", "two"))), + undefined, + { silent: true }, + ); + assert.strictEqual(result.silentViolation, false); + assert.strictEqual(result.content, "one two"); + }); + + it("ignores messages with no text (tool-call-only turns)", async () => { + const result = await consumeStream( + streamOf(ev(say("A", "")), ev(tool("T", "ok")), ev(say("B", "answer"))), + undefined, + { silent: true }, + ); + assert.strictEqual(result.silentViolation, false); + assert.strictEqual(result.content, "answer"); + }); + + it("ignores subagent messages", async () => { + const result = await consumeStream( + streamOf( + ev(say("A", "mine")), + ev(say("S1", "sub one"), ["tools:1"]), + ev(say("S2", "sub two"), ["tools:1"]), + ), + undefined, + { silent: true }, + ); + assert.strictEqual(result.silentViolation, false); + }); + + it("never flags when silent mode is off", async () => { + const result = await consumeStream( + streamOf(ev(say("A", "first")), ev(say("B", "second"))), + undefined, + { silent: false }, + ); + assert.strictEqual(result.silentViolation, false); + assert.strictEqual(result.content, "firstsecond"); + }); +}); + +describe("shouldNudgeSilent", () => { + const base = { + silentViolation: true, + streamingMsgId: "m1", + lastMessageId: "m1", + }; + + it("uses the agreed nudge text", () => { + assert.strictEqual(SILENT_MODE_NUDGE, "Stay quiet, silent mode is enabled"); + }); + + it("nudges on a violation in the last bubble", () => { + assert.strictEqual(shouldNudgeSilent(base), true); + }); + + it("does not nudge without a violation", () => { + assert.strictEqual(shouldNudgeSilent({ ...base, silentViolation: false }), false); + }); + + it("does not nudge an interrupted turn", () => { + assert.strictEqual(shouldNudgeSilent({ ...base, interrupted: true }), false); + }); + + it("does not nudge when the bubble is not the last", () => { + assert.strictEqual(shouldNudgeSilent({ ...base, lastMessageId: "m2" }), false); + }); + + it("is not bound by the auto-continue limit", () => { + assert.strictEqual( + shouldNudgeSilent({ ...base, autoContinueCount: 1000, autoContinueLimit: 1000 }), + true, + ); + }); +});