feat(webui): complete bang commands and literal output (NAN-291) (#6173)
* feat(webui): complete direct bang shell commands Reuse the gateway user-shell side channel, retain failed drafts without reconnect replay, and preserve literal output across live delivery and history. Highlight the bang prefix before and after sending. NAN-291 * fix(clients): preserve literal shell output through display metadata
This commit is contained in:
1 parent
f23a6d1485
commit
7e5dcb79e0
43 files changed
+805
-100
No files matched your search
+3
-1
@@ -770,7 +770,7 @@ export class NanobotTui {
|
||||
if (this.modelCommandTurns.delete(event.turn_id)) {
|
||||
void this.refreshSessionMetadata(event.chat_id)
|
||||
}
|
||||
if (!silent) this.transcript.assistant(event.text)
|
||||
if (!silent) this.transcript.assistant(event.text, event.render_as)
|
||||
if (!this.activeTurn) this.status.content = "Ready"
|
||||
return
|
||||
}
|
||||
@@ -780,6 +780,8 @@ export class NanobotTui {
|
||||
this.activeLabel = event.kind === "tool_hint" ? "Working" : "Thinking"
|
||||
this.transcript.progress(event.text, event.tool_events)
|
||||
this.setActive(true)
|
||||
} else if (event.render_as === "text") {
|
||||
this.transcript.assistant(event.text, event.render_as)
|
||||
} else {
|
||||
this.finalMessage = event.text
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import type { ClipboardImageReader } from "../composer/clipboard-image"
|
||||
import { options, client, mount, waitUntil, occurrences } from "./test-support"
|
||||
import { LOCAL_COMMANDS } from "./commands"
|
||||
import type { CommandMenu } from "../menus/command-menu"
|
||||
import type { Transcript } from "../rendering/transcript"
|
||||
|
||||
describe("NanobotTui composer", () => {
|
||||
let setup: TestRendererSetup | undefined
|
||||
@@ -986,8 +987,8 @@ describe("NanobotTui composer", () => {
|
||||
expect(sent).toEqual(["start an active turn"])
|
||||
})
|
||||
|
||||
test("runs bang commands through the gateway without steering the agent", async () => {
|
||||
setup = await createRenderer({ width: 80, height: 24, screenMode: "alternate-screen" })
|
||||
test("runs bang commands through the gateway and copies their literal output without steering the agent", async () => {
|
||||
setup = await createRenderer({ width: 100, height: 32, screenMode: "alternate-screen" })
|
||||
const sent: string[] = []
|
||||
const sentOptions: MessageOptions[] = []
|
||||
const app = NanobotTui.mount(
|
||||
@@ -997,7 +998,9 @@ describe("NanobotTui composer", () => {
|
||||
new MockTreeSitterClient({ autoResolveTimeout: 0 }),
|
||||
)
|
||||
app.accept({ event: "attached", chat_id: "chat" })
|
||||
const ui = app as unknown as { ready: boolean; composer: TextareaRenderable; activeTurn: boolean }
|
||||
const ui = app as unknown as {
|
||||
ready: boolean; composer: TextareaRenderable; activeTurn: boolean; transcript: Transcript
|
||||
}
|
||||
await waitUntil(() => ui.ready)
|
||||
|
||||
app.accept({ event: "goal_status", chat_id: "chat", status: "running" })
|
||||
@@ -1009,9 +1012,32 @@ describe("NanobotTui composer", () => {
|
||||
expect(sentOptions).toEqual([{ userShell: true }])
|
||||
expect(ui.activeTurn).toBe(true)
|
||||
|
||||
app.accept({ event: "message", chat_id: "chat", text: "/tmp/project", turn_id: "turn" })
|
||||
const output = [
|
||||
" __pycache__ `ticks` ``` ````",
|
||||
" <b>HTML</b>",
|
||||
"\\(x_1^2\\)",
|
||||
"```mermaid",
|
||||
"graph TD; A --> B",
|
||||
"```",
|
||||
"",
|
||||
].join("\n")
|
||||
app.accept({ event: "message", chat_id: "chat", text: output, render_as: "text", turn_id: "turn" })
|
||||
await setup.flush()
|
||||
expect(setup.captureCharFrame()).toContain("/tmp/project")
|
||||
const text = ui.transcript.root.getChildren().flatMap((row) => row.getChildren())
|
||||
.find((child) => child instanceof TextRenderable && child.plainText === output)
|
||||
expect(text).toBeInstanceOf(TextRenderable)
|
||||
expect(setup.captureCharFrame()).toContain(" <b>HTML</b>")
|
||||
expect(setup.captureCharFrame()).toContain("\\(x_1^2\\)")
|
||||
expect(setup.captureCharFrame()).toContain("```mermaid")
|
||||
expect(ui.activeTurn).toBe(true)
|
||||
|
||||
let copied = ""
|
||||
setup.renderer.copyToClipboardOSC52 = (value) => { copied = value; return true }
|
||||
if (!(text instanceof TextRenderable)) throw new Error("missing literal shell output")
|
||||
await setup.mockMouse.drag(text.screenX, text.screenY, text.screenX, text.screenY + text.height - 1)
|
||||
expect(setup.renderer.getSelection()?.getSelectedText()).toBe(output)
|
||||
setup.mockInput.pressCtrlC()
|
||||
await waitUntil(() => copied.length > 0)
|
||||
expect(copied).toBe(output)
|
||||
})
|
||||
})
|
||||
@@ -1,8 +1,9 @@
|
||||
import { afterEach, describe, expect, test } from "bun:test"
|
||||
import { BoxRenderable, CliRenderEvents, TextareaRenderable, TextRenderable } from "@opentui/core"
|
||||
import { BoxRenderable, CliRenderEvents, MarkdownRenderable, TextareaRenderable, TextRenderable } from "@opentui/core"
|
||||
import { MockTreeSitterClient, createTestRenderer, type TestRendererSetup } from "@opentui/core/testing"
|
||||
import { NanobotTui } from "./app"
|
||||
import { options, client, mount, waitUntil, occurrences } from "./test-support"
|
||||
import type { Transcript } from "../rendering/transcript"
|
||||
|
||||
describe("NanobotTui transcript", () => {
|
||||
let setup: TestRendererSetup | undefined
|
||||
@@ -16,9 +17,11 @@ describe("NanobotTui transcript", () => {
|
||||
createTestRenderer(rendererOptions)
|
||||
)
|
||||
test("loads earlier transcript pages in place when PageUp reaches the top", async () => {
|
||||
setup = await createRenderer({ width: 80, height: 22, screenMode: "alternate-screen" })
|
||||
setup = await createRenderer({ width: 100, height: 40, screenMode: "alternate-screen" })
|
||||
const original = globalThis.fetch
|
||||
const requests: string[] = []
|
||||
const oldest = "oldest answer `ticks` __pycache__\n  <b>HTML</b>\n"
|
||||
const recent = "recent answer \\(x_1^2\\)\n```mermaid\ngraph TD; A --> B\n```\n"
|
||||
globalThis.fetch = ((input: string | URL | Request) => {
|
||||
const url = String(input)
|
||||
requests.push(url)
|
||||
@@ -27,12 +30,13 @@ describe("NanobotTui transcript", () => {
|
||||
schemaVersion: 3, projection: "events",
|
||||
events: older
|
||||
? [
|
||||
{ event: "user_message", chat_id: "chat", starts_turn: true, text: "oldest question" },
|
||||
{ event: "stream_end", chat_id: "chat", text: "oldest answer" },
|
||||
{ event: "user_message", chat_id: "chat", starts_turn: false, text: "!cat oldest.log", turn_id: "old-shell" },
|
||||
{ event: "message", chat_id: "chat", text: oldest, render_as: "text", turn_id: "old-shell" },
|
||||
]
|
||||
: [
|
||||
{ event: "user_message", chat_id: "chat", starts_turn: true, text: "recent question" },
|
||||
{ event: "stream_end", chat_id: "chat", text: "recent answer" },
|
||||
{ event: "user_message", chat_id: "chat", starts_turn: false, text: "!cat recent.log", turn_id: "recent-shell" },
|
||||
{ event: "message", chat_id: "chat", text: recent, render_as: "text", turn_id: "recent-shell" },
|
||||
{ event: "stream_end", chat_id: "chat", text: "Normal \\(x_1^2\\)", turn_id: "agent" },
|
||||
],
|
||||
page: older
|
||||
? { has_more_before: false, before_cursor: null }
|
||||
@@ -55,20 +59,48 @@ describe("NanobotTui transcript", () => {
|
||||
await setup.flush()
|
||||
const frame = setup.captureCharFrame()
|
||||
|
||||
expect(frame.indexOf("oldest question")).toBeLessThan(frame.indexOf("recent question"))
|
||||
expect(frame.indexOf("!cat oldest.log")).toBeLessThan(frame.indexOf("!cat recent.log"))
|
||||
expect(frame.indexOf("oldest answer")).toBeLessThan(frame.indexOf("recent answer"))
|
||||
expect((app as unknown as { historyHasMore: boolean }).historyHasMore).toBe(false)
|
||||
const transcript = (app as unknown as { transcript: Transcript }).transcript
|
||||
const cells = transcript.root.getChildren().flatMap((row) => row.getChildren())
|
||||
expect(cells.filter((child) => child instanceof TextRenderable)
|
||||
.map((child) => child.plainText)).toEqual(expect.arrayContaining([oldest, recent]))
|
||||
expect(cells.filter((child) => child instanceof MarkdownRenderable)
|
||||
.map((child) => child.content)).toEqual(["Normal x₁²"])
|
||||
expect(frame).toContain(" <b>HTML</b>")
|
||||
expect(frame).toContain("```mermaid")
|
||||
|
||||
const composer = (app as unknown as { composer: TextareaRenderable }).composer
|
||||
setup.mockInput.pressArrow("up")
|
||||
expect(composer.plainText).toBe("recent question")
|
||||
expect(composer.plainText).toBe("!cat recent.log")
|
||||
setup.mockInput.pressArrow("up")
|
||||
expect(composer.plainText).toBe("oldest question")
|
||||
expect(composer.plainText).toBe("!cat oldest.log")
|
||||
} finally {
|
||||
globalThis.fetch = original
|
||||
}
|
||||
})
|
||||
|
||||
test("shows literal shell output from another client without replacing a pending Markdown reply", async () => {
|
||||
setup = await createRenderer({ width: 100, height: 26, screenMode: "alternate-screen" })
|
||||
const app = mount(setup)
|
||||
app.accept({ event: "goal_status", chat_id: "chat", status: "running", turn_id: "agent" })
|
||||
app.accept({ event: "message", chat_id: "chat", text: "Normal \\(x_1^2\\)", turn_id: "agent" })
|
||||
const output = " __pycache__ `ticks`  <b>HTML</b>\n"
|
||||
app.accept({ event: "message", chat_id: "chat", text: output, render_as: "text", turn_id: "other-shell" })
|
||||
await setup.flush()
|
||||
expect(setup.captureCharFrame()).toContain("__pycache__ `ticks`  <b>HTML</b>")
|
||||
const ui = app as unknown as { activeTurn: boolean; transcript: Transcript }
|
||||
expect(ui.activeTurn).toBe(true)
|
||||
|
||||
app.accept({ event: "turn_end", chat_id: "chat", turn_id: "agent" })
|
||||
await setup.flush()
|
||||
const cells = ui.transcript.root.getChildren().flatMap((row) => row.getChildren())
|
||||
expect(cells.some((child) => child instanceof TextRenderable && child.plainText === output)).toBe(true)
|
||||
expect(cells.some((child) => child instanceof MarkdownRenderable && child.content === "Normal x₁²")).toBe(true)
|
||||
expect(ui.activeTurn).toBe(false)
|
||||
})
|
||||
|
||||
test("replaces streamed drafts with canonical stream-end text", async () => {
|
||||
setup = await createRenderer({ width: 80, height: 20, screenMode: "alternate-screen" })
|
||||
const app = mount(setup)
|
||||
|
||||
@@ -229,12 +229,15 @@ export async function fetchHistory(
|
||||
})
|
||||
} else {
|
||||
// A final message can repeat the answer already saved by stream_end.
|
||||
if (stream && stream.turnId === event.turn_id) stream.row.content = event.text
|
||||
else if (!lastAnswer || lastAnswer.turnId !== event.turn_id || lastAnswer.row.content !== event.text) {
|
||||
if (stream && stream.turnId === event.turn_id) {
|
||||
stream.row.content = event.text
|
||||
lastAnswer = stream
|
||||
} else if (!lastAnswer || lastAnswer.turnId !== event.turn_id || lastAnswer.row.content !== event.text) {
|
||||
const row: HistoryMessage = { role: "assistant", content: event.text, forkIndex: userIndex }
|
||||
messages.push(row)
|
||||
lastAnswer = { row, turnId: event.turn_id }
|
||||
}
|
||||
if (event.render_as === "text" && lastAnswer) lastAnswer.row.renderAs = event.render_as
|
||||
stream = null
|
||||
}
|
||||
break
|
||||
@@ -258,7 +261,8 @@ export async function fetchHistory(
|
||||
}
|
||||
}
|
||||
return {
|
||||
messages: messages.filter((row) => row.role !== "assistant" || row.content.trim()),
|
||||
messages: messages.filter((row) => row.role !== "assistant"
|
||||
|| (row.renderAs === "text" ? row.content : row.content.trim())),
|
||||
hasMoreBefore: payload.page?.has_more_before === true,
|
||||
beforeCursor: typeof payload.page?.before_cursor === "string"
|
||||
? payload.page.before_cursor
|
||||
|
||||
@@ -502,6 +502,8 @@ describe("gateway protocol", () => {
|
||||
starts_turn: true,
|
||||
media_urls: [{ kind: "file", url: "/api/media/sig/file", name: "report.pdf" }],
|
||||
},
|
||||
{ event: "message", chat_id: "one", text: " __pycache__ `ticks`\n", render_as: "text" },
|
||||
{ event: "message", chat_id: "one", text: "**Markdown reply**" },
|
||||
{ event: "delta", chat_id: "one", text: "hello" },
|
||||
{ event: "stream_end", chat_id: "one", resuming: false },
|
||||
{ event: "turn_end", chat_id: "one", latency_ms: 12 },
|
||||
@@ -522,6 +524,10 @@ describe("gateway protocol", () => {
|
||||
socket.emit("message", { data: JSON.stringify(frame) })
|
||||
}
|
||||
expect(delivered).toEqual(frames)
|
||||
socket.emit("message", { data: JSON.stringify({
|
||||
event: "message", chat_id: "one", text: "**Markdown reply**", render_as: true,
|
||||
}) })
|
||||
expect(delivered.at(-1)).toEqual({ event: "message", chat_id: "one", text: "**Markdown reply**" })
|
||||
} finally {
|
||||
client.close()
|
||||
Object.defineProperty(globalThis, "WebSocket", { configurable: true, value: original })
|
||||
|
||||
@@ -40,6 +40,25 @@ test("restores canonical event history, media, tools, file edits and absolute fo
|
||||
})
|
||||
})
|
||||
|
||||
test("preserves literal shell output and leaves ordinary replies as Markdown", async () => {
|
||||
const output = " __pycache__ `ticks`\n <b>HTML</b>\n```mermaid\ngraph TD; A --> B\n```\n"
|
||||
respond([
|
||||
{ event: "user_message", starts_turn: false, text: "!ls", turn_id: "shell" },
|
||||
{ event: "message", text: output, render_as: "text", turn_id: "shell" },
|
||||
{ event: "user_message", starts_turn: true, text: "explain", turn_id: "answer" },
|
||||
{ event: "message", text: "**Markdown** \\(x_1^2\\)", turn_id: "answer" },
|
||||
{ event: "turn_end", turn_id: "answer" },
|
||||
{ event: "message", text: "**Another reply**", render_as: true, turn_id: "next" },
|
||||
])
|
||||
expect((await fetchHistory("http://fixture", "token", "chat")).messages).toEqual([
|
||||
{ role: "user", content: "!ls", turnId: "shell" },
|
||||
{ role: "assistant", content: output, renderAs: "text", forkIndex: 1 },
|
||||
{ role: "user", content: "explain", turnId: "answer" },
|
||||
{ role: "assistant", content: "**Markdown** \\(x_1^2\\)", forkIndex: 2 },
|
||||
{ role: "assistant", content: "**Another reply**", forkIndex: 2 },
|
||||
])
|
||||
})
|
||||
|
||||
test("keeps distinct answer segments and resumes an unfinished stream", async () => {
|
||||
respond([
|
||||
{ event: "user_message", starts_turn: true, text: "question" },
|
||||
|
||||
@@ -116,6 +116,7 @@ export type InboundEvent =
|
||||
event: "message"
|
||||
chat_id: string
|
||||
text: string
|
||||
render_as?: "text"
|
||||
kind?: "tool_hint" | "progress" | "reasoning"
|
||||
tool_events?: ToolProgressEvent[]
|
||||
turn_id?: string
|
||||
@@ -229,6 +230,7 @@ export type ApiReauthenticator = (
|
||||
export interface HistoryMessage {
|
||||
role: "user" | "assistant" | "activity"
|
||||
content: string
|
||||
renderAs?: "text"
|
||||
turnId?: string
|
||||
media?: MediaAttachment[]
|
||||
toolEvents?: ToolProgressEvent[]
|
||||
|
||||
@@ -233,5 +233,10 @@ export function decodeInboundEvent(value: unknown): InboundEvent | null | undefi
|
||||
&& typeof record.model_preset !== "string")
|
||||
|| !optional(record.context_window_tokens, "number"))
|
||||
) return null
|
||||
if (name === "message" && record.render_as !== undefined && record.render_as !== "text") {
|
||||
const message = { ...record }
|
||||
delete message.render_as
|
||||
return message as InboundEvent
|
||||
}
|
||||
return value as InboundEvent
|
||||
}
|
||||
@@ -262,7 +262,7 @@ export class Transcript {
|
||||
else if (message.role === "user") {
|
||||
this.user(message.content, message.turnId, message.media)
|
||||
}
|
||||
else if (message.role === "assistant") this.assistant(message.content)
|
||||
else if (message.role === "assistant") this.assistant(message.content, message.renderAs)
|
||||
else if (message.fileEdits?.length) this.fileEdits(message.fileEdits)
|
||||
else this.progress(message.content, message.toolEvents)
|
||||
}
|
||||
@@ -282,7 +282,7 @@ export class Transcript {
|
||||
this.writeUser(message.content, message.media, index++)
|
||||
if (message.turnId) this.userTurnIds.add(message.turnId)
|
||||
} else if (message.role === "assistant") {
|
||||
this.writeMarkdown(message.content, false, index++)
|
||||
this.writeAnswer(message.content, message.renderAs, index++)
|
||||
} else {
|
||||
const activity = this.createActivity(index++)
|
||||
const events: ToolProgressEvent[] = message.fileEdits?.length
|
||||
@@ -319,11 +319,11 @@ export class Transcript {
|
||||
return true
|
||||
}
|
||||
|
||||
assistant(content: string): void {
|
||||
if (!content.trim()) return
|
||||
assistant(content: string, renderAs?: "text"): void {
|
||||
if (!(renderAs === "text" ? content : content.trim())) return
|
||||
this.noteOutput()
|
||||
this.finishActivity()
|
||||
this.writeMarkdown(content, false)
|
||||
this.writeAnswer(content, renderAs)
|
||||
}
|
||||
|
||||
notice(content: string, error = false): void {
|
||||
@@ -756,17 +756,26 @@ export class Transcript {
|
||||
return markdown
|
||||
}
|
||||
|
||||
private writeMarkdown(content: string, streaming: boolean, index?: number): void {
|
||||
this.writeAssistant(this.createMarkdown(content, streaming), index)
|
||||
private writeAnswer(content: string, renderAs?: "text", index?: number): void {
|
||||
if (renderAs !== "text") {
|
||||
this.writeAssistant(this.createMarkdown(content, false), index)
|
||||
return
|
||||
}
|
||||
const text = this.createText(content, "text", false, "assistant-text")
|
||||
text.width = "auto"
|
||||
text.minWidth = 0
|
||||
text.flexGrow = 1
|
||||
text.flexShrink = 1
|
||||
this.writeAssistant(text, index)
|
||||
}
|
||||
|
||||
private writeAssistant(markdown: MarkdownRenderable, index?: number): BoxRenderable {
|
||||
private writeAssistant(content: MarkdownRenderable | TextRenderable, index?: number): BoxRenderable {
|
||||
const row = this.createRow("assistant", "row")
|
||||
const prefix = this.createText("•", "muted", false, "role-marker")
|
||||
prefix.width = 2
|
||||
prefix.flexShrink = 0
|
||||
row.add(prefix)
|
||||
row.add(markdown)
|
||||
row.add(content)
|
||||
this.root.add(row, index)
|
||||
this.wrote = true
|
||||
return row
|
||||
|
||||
Reference in new issue
Block a user