Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

Merge main into fix/tui-zero-width-terminal: size the new frame code with sized() too

rc81dd5ef1b40
diff --cc plugins/ruflo-console/tests/tui-nav.spec.ts
index 5905e82303,392ce65ec9..8ac52db86f
--- a/plugins/ruflo-console/tests/tui-nav.spec.ts
+++ b/plugins/ruflo-console/tests/tui-nav.spec.ts
@@@ -158,23 -158,9 +158,23 @@@ describe('every page, on a host with n
})
describe('the terminal loop', () => {
+ it('a terminal that reports 0 columns draws the page at the default width, not a repeated "T" (#4115)', async () => {
+ const t = terminal(0, 0)
+ const running = runTui({ cwd: project, home: join(root, 'home') }, t.io)
+
+ await settle()
+ t.press('q')
+ await expect(running).resolves.toBe(0)
+
+ const text = plain(t.out.join(''))
+
+ expect(text).toMatch(/1 Overview {1,3}2 Cost/)
+ expect(text).not.toContain('Too narrow')
+ })
+
it('jumps to a page on a digit, cycles with tab and shift-tab, and the page bar follows', async () => {
const t = terminal()
- const running = runTui({ cwd: project, home: join(root, 'home') }, t.io)
+ const running = runTui({ look: 'plain', fps: 0, cwd: project, home: join(root, 'home') }, t.io)
await settle()
t.press('3')
diff --cc plugins/ruflo-console/tui/run.ts
index 6edac9f5fb,01937bad07..51a603210d
--- a/plugins/ruflo-console/tui/run.ts
+++ b/plugins/ruflo-console/tui/run.ts
@@@ -117,14 -162,37 +162,40 @@@ export function keyOf(chunk: string): K
}
}
+/** A terminal that reports 0 (or nothing) does not know its size; some `docker exec` without -t and CI `script` wrappers do. Use the default rather than calling it too narrow (#4115). */
+const sized = (value: number | undefined, fallback: number): number => (value !== undefined && Number.isFinite(value) && value > 0 ? value : fallback)
+
- function paint(session: Session, io: TuiIO, depth: Depth, scroll: number, clear = false): number {
+ /** What the last frame put on the screen, so the next one rewrites only the rows that changed (a moving picture then costs its own rows, not a whole redraw). */
+ type Memory = { lines: string[]; columns: number; rows: number }
+
+ /** A whole frame, from the home position: the page bar, the body rows, then the key line (the shape every key press and resize draws). */
+ function writeFrame(io: TuiIO, screen: string[], memory: Memory, columns: number, rows: number, clear: boolean): void {
+ const body = screen.slice(1, -1)
+
+ memory.lines = screen
+ memory.columns = columns
+ memory.rows = rows
+ io.stdout.write(`${clear ? `${ESC}[2J` : ''}${ESC}[H${screen[0]}${ESC}[K\r\n${body.map(line => `${line}${ESC}[K`).join('\r\n')}${ESC}[J\r\n${screen[screen.length - 1]}`)
+ }
+
+ /** An animation tick: only the rows that differ from the last frame, each at its own position (a moving picture costs its own rows, not a whole redraw). Anything else is a whole frame. */
+ function writeTick(io: TuiIO, screen: string[], memory: Memory, columns: number, rows: number): void {
+ if (memory.columns !== columns || memory.rows !== rows || memory.lines.length !== screen.length) return writeFrame(io, screen, memory, columns, rows, true)
+
+ let out = ''
+
+ screen.forEach((line, i) => {
+ if (memory.lines[i] !== line) out += `${ESC}[${i + 1};1H${line}${ESC}[K`
+ })
+ memory.lines = screen
+
+ if (out !== '') io.stdout.write(out)
+ }
+
+ function paint(session: Session, io: TuiIO, depth: Depth, scroll: number, memory: Memory, clear = false, tick = false, line = ''): number {
const ai = session.page === 'ai' ? session.ai : null
- const raw = io.stdout.columns ?? 100
- const rows = Math.max(5, io.stdout.rows ?? 30)
+ const raw = sized(io.stdout.columns, 100)
+ const rows = Math.max(5, sized(io.stdout.rows, 30))
- const reset = clear ? `${ESC}[2J` : ''
// Too narrow for a page: say so in a line that fits, rather than a squeezed frame of broken cards.
if (raw < MIN_COLUMNS) {
@@@ -157,10 -235,10 +238,10 @@@
}
/** `--plain`: one frame as plain text on stdout (the same views, read-only, no escapes) and exit 0. Nothing on stderr, so a script's output is only the frame. */
- async function once(session: Session, io: TuiIO, view: TuiView): Promise<number> {
+ async function once(session: Session, io: TuiIO, view: Page): Promise<number> {
session.show(view)
await session.refresh()
- const columns = Math.max(MIN_COLUMNS, Math.min(200, io.stdout.columns ?? 100))
+ const columns = Math.max(MIN_COLUMNS, Math.min(200, sized(io.stdout.columns, 100)))
io.stdout.write(`${navLine(view, columns, 'none')}\n${session.frame(columns, 'none').join('\n')}\n`)
@@@ -197,17 -282,103 +285,103 @@@ export async function runTui(options: T
}
session.onChange(() => void (isDone || draw()))
+ const open = (next: Page) => {
+ if (next !== view) history.push(view), history.length > 30 && history.shift()
+
+ view = next
+ scroll = 0
+ session.show(view)
+ view = session.page
+ if (session.page === 'ai') session.ai?.enter()
+ else if (!(TUI_VIEWS as readonly string[]).includes(view)) void reread()
+ }
+ const goTo = (word: string) => {
+ const name = word.trim().replace(/^(go|open)\s+/i, '')
+ const found = name.toLowerCase() === 'ai' ? 'ai' : viewOf(name)
+
+ // A name that is not a page is an action, asked the console's way: a read runs at once, a change shows a card and waits for a y.
+ if (name.startsWith('>')) notice = session.palette(name.slice(1))
+ else if (found === null || found === 'agent') notice = session.palette(name)
+ else open(found)
+ }
+ const onMouse = (chunk: string): boolean => {
+ const hit = /^\u001b\[<(\d+);(\d+);(\d+)([Mm])$/.exec(chunk)
+
+ if (hit === null) return false
+
+ const [button, x, y] = [Number(hit[1]), Number(hit[2]), Number(hit[3])]
+
+ if (hit[4] !== 'M') return true
+ if (button === 64) scroll = Math.max(0, scroll - 3)
+ else if (button === 65) scroll += 3
+ else if (button === 0 && y === 1) {
- const page = navHit(x, Math.min(200, io.stdout.columns ?? 100))
++ const page = navHit(x, Math.min(200, sized(io.stdout.columns, 100)))
+
+ if (page !== null) open(page)
+ }
+
+ draw()
+
+ return true
+ }
+
const onKey = (chunk: Buffer | string) => {
// The AI page takes the keys while you type (digits and letters are text there): Esc hands them back to the shell, and Ctrl-C always quits.
- if (view === 'ai' && session.ai !== null && session.ai.press(chunk.toString()).consumed) return draw()
+ if (session.page === 'ai' && session.ai !== null && session.ai.press(chunk.toString()).consumed) return draw()
+
+ // Focus reports (the terminal tells us when it is behind another window): a hidden terminal draws nothing, and a refocused one draws at once.
+ if (chunk.toString() === `${ESC}[O`) return void (isFocused = false)
+ if (chunk.toString() === `${ESC}[I`) return void ((isFocused = true), draw())
+
+ if (options.mouse === true && onMouse(chunk.toString())) return
+
+ const text = chunk.toString()
+
+ notice = ''
+
+ if (prompt !== null) {
+ if (text === '\r' || text === '\n') {
+ const word = prompt
+
+ prompt = null
+ goTo(word)
+ } else if (text === ESC || text === '\u0003') prompt = null
+ else if (text === '\u007f' || text === '\b') prompt = prompt.slice(0, -1)
+ else if (text.length === 1 && text >= ' ' && text !== '\u007f' && prompt.length < 40) prompt += text
+ else if (text.length > 1 && !text.includes(ESC) && prompt.length + text.length <= 40) prompt += clean(text).replace(/[\r\n]/g, '')
+
+ return draw()
+ }
+
+ // A waiting card: only a lone y, while the whole card is on screen, runs it; n or Esc drops it (a paste is text, never an answer).
+ if (session.page !== 'ai' && session.pending() !== null) {
+ if (text === 'y' || text === 'Y') return void (session.answer(true), draw())
+ if (text === 'n' || text === 'N' || text === ESC) return void (session.answer(false), draw())
+ }
+
+ // The main menu's keys are the console's own: an entry opens by its key chip (q still quits, and p is the palette, here the go prompt).
+ if (view === 'menu' && text.length === 1 && text !== 'q') {
+ const entry = VIEWS.find(candidate => candidate.key !== '' && candidate.key === text)
+
+ if (entry !== undefined) return void (open(entry.id), draw())
+ if (text === 'p') return void ((prompt = ''), draw())
+ }
- const key = keyOf(chunk.toString())
- const page = Math.max(1, sized(io.stdout.rows, 30) - 2)
+ const key = keyOf(text)
- const page = Math.max(1, (io.stdout.rows ?? 30) - 2)
++ const page = Math.max(1, (sized(io.stdout.rows, 30)) - 2)
if (key === 'quit') return finish()
- if (typeof key === 'object' && key !== null) (view = key.page), (scroll = 0), session.show(view), view === 'ai' && session.ai?.enter()
- else if (key === 'next') (view = TUI_VIEWS[(TUI_VIEWS.indexOf(view) + 1) % TUI_VIEWS.length] as TuiView), (scroll = 0), session.show(view)
- else if (key === 'prev') (view = TUI_VIEWS[(TUI_VIEWS.indexOf(view) + TUI_VIEWS.length - 1) % TUI_VIEWS.length] as TuiView), (scroll = 0), session.show(view)
+ if (typeof key === 'object' && key !== null) open(key.page)
+ else if (key === 'menu') open('menu')
+ else if (key === 'prompt') prompt = ''
+ else if (key === 'back') {
+ const previous = history.pop()
+
+ if (previous === undefined) notice = 'nothing to go back to'
+ else (view = previous), (scroll = 0), session.show(view), (view = session.page)
+ }
+ else if (key === 'next') open(TUI_VIEWS[(TUI_VIEWS.indexOf(view as TuiView) + 1) % TUI_VIEWS.length] as TuiView)
+ else if (key === 'prev') open(TUI_VIEWS[(TUI_VIEWS.indexOf(view as TuiView) + TUI_VIEWS.length - 1) % TUI_VIEWS.length] as TuiView)
else if (key === 'down') scroll += 1
else if (key === 'up') scroll = Math.max(0, scroll - 1)
else if (key === 'pagedown') scroll += page