Files
ruflo/plugins/ruflo-console/hooks/dispatch.ts
T
ruv 89e3f6e737 feat(ruflo-console): animated-ASCII boot and a Cave-BBS main menu as the landing screen
- Boot: RUFLO materialises as big ANSI block art out of an animated ASCII field.
  - A 4x4 ordered dither plus a left-to-right sweep decides when each cell resolves.
  - Cells still resolving flicker as noise characters, with a dim plasma behind, and resolved letters get a drop shadow.
  - This is the image-to-ASCII effect of tools like ascii-magic, done in cells: offline and deterministic.
  - Moved to gfx/boot.ts.
- Main menu (0, 📟): where the cockpit lands in the BBS look (bare /ruflo, auto-open).
  - "RUFLO BBS" block title, a host line, and three boxed menus (Swarm Commands, System Features, Other Commands) whose entries go where their keys go.
  - A modem-style status bar with this project's live facts, a T - mm:ss clock, and a (1:1) prompt that takes a key or a name.
  - b goes back to the menu; /ruflo <view> still opens that view.
- Auto-open no longer overrides a view a /ruflo command opened meanwhile.

Co-Authored-By: RuFlo <ruv@ruv.net>
2026-10-02 15:11:55 -04:00

162 lines
7.1 KiB
TypeScript

/**
* Carries out a `/ruflo` intent (./commands) and answers the command's row. The pane's keys all have an intent here,
* so everything works without focus. `mods` and `swarm <sub>` are passed on to the plugins that own them; when neither
* answers, the row says which plugin to load rather than pretending.
*/
import { HELP, parseRuflo, type Intent } from './commands'
import { CATALOG_PATH, commandsText, FALLBACK, parseCatalog, type Catalog } from './data/catalog'
import type { Controller } from './controller'
import { median, p95 } from './controller'
import { plain } from './data/parse'
import { VIEWS, type State } from './state'
import { barText } from './views/bar'
import { viewText } from './views/pane'
/** The engine's words when a registered command reaches it with no hook answering (Claude Code 2.1.287). */
const NO_HOOK_ANSWERED = /registered \/ruflo but no command\.run hook answered/
export type Delegate = () => Promise<{ text?: string } | undefined>
/** The one-line answer of `/ruflo status`, with the measured render, refresh and frame costs. */
export function statusLine(state: State): string {
const stat = (name: string, values: readonly number[]) => (values.length === 0 ? `${name} n/a` : `${name} median ${median(values)}ms p95 ${p95(values)}ms (n=${values.length})`)
return [barText(state), stat('render', state.stats.renders), stat('refresh', state.stats.refreshes), stat('frame', state.stats.frames)].join(' · ')
}
const OWNER_HINT = {
'ruflo-mods': 'ruflo-mods is not loaded in this session, so nothing answered `/ruflo mods`. Install it with `npx ruflo mods install` (or enable ruflo-mods@ruflo); its old `/ruflo-mods` command is the same report.',
'ruflo-swarm': 'ruflo-swarm is not loaded in this session, so nothing answered `/ruflo swarm …`. Enable ruflo-swarm@ruflo; the Swarm view (/ruflo swarm) is the console\'s own.',
} as const
async function open(control: Controller, state: State, label: string): Promise<{ text: string }> {
const opened = await control.open()
return { text: opened.isPlaced ? `ruflo console: ${label}` : `The ruflo console could not be shown: ${opened.reason}` }
}
const DUMP_WAIT_MS = 8_000
/** The catalog this plugin ships (read once per session), or the built-in mod list when it is missing or another contract. */
async function loadCatalog(control: Controller): Promise<Catalog> {
control.catalog ??= control.host.fs
.read(`${control.host.pluginRoot}/${CATALOG_PATH}`)
.then(text => parseCatalog(text) ?? FALLBACK, () => FALLBACK)
return control.catalog
}
/**
* A view as text, with its CLI probes run first and waited for (at most DUMP_WAIT_MS: a probe still running then reads
* "asking the ruflo CLI…", as it would on screen). The pane's own view is put back afterwards.
*/
async function dumpOf(control: Controller, state: State, view: State['view']): Promise<string> {
const shown = state.view
state.view = view
try {
await control.refresh()
await Promise.race([control.probe(true), new Promise(resolve => setTimeout(resolve, DUMP_WAIT_MS))])
return viewText({ state, nowMs: Date.now(), columns: 100, act: control.actions }, view)
} finally {
state.view = shown
}
}
export async function dispatch(control: Controller, state: State, args: string, delegate: Delegate): Promise<{ text: string }> {
const intent: Intent = parseRuflo(args)
switch (intent.kind) {
case 'open':
// Without a pane to show (claude -p, an SDK host), the view is answered as text in the command's row instead.
if (!state.isInteractive) return { text: await dumpOf(control, state, intent.view ?? state.view) }
// The BBS look lands on its main menu when the cockpit opens with no view named, like a board after login.
if (intent.view !== null) control.setView(intent.view)
else if (!state.pane.isOpen && state.options.look === 'bbs') control.setView('menu')
return open(control, state, VIEWS.find(view => view.id === state.view)?.label ?? 'Agent')
case 'help':
return { text: HELP }
case 'close':
await control.close()
return { text: 'ruflo console closed (/ruflo opens it again)' }
case 'status':
await control.refresh()
return { text: statusLine(state) }
case 'delegate': {
try {
const answer = await delegate()
// With nothing beneath, the engine answers in its own words that no hook answered: that is no answer either.
if (typeof answer?.text === 'string' && answer.text.trim() !== '' && !NO_HOOK_ANSWERED.test(answer.text)) return { text: answer.text }
} catch {
// Nothing beneath answers this command: fall through to the hint.
}
return { text: OWNER_HINT[intent.owner] }
}
case 'palette':
state.palette = { isOpen: true, query: plain(intent.query, 200), index: 0, context: 'all' }
return open(control, state, 'palette')
case 'run': {
const isRun = control.actions.run(intent.paletteId, intent.text)
if (!isRun) return { text: `No palette entry "${plain(intent.paletteId, 40)}" right now. /ruflo palette lists them; ids look like spawn-coder, claim-release, worker-audit, route.` }
await control.open()
return { text: state.pending !== null ? `Asked: ${state.pending.label}. Confirm with /ruflo yes (or y in the pane), cancel with /ruflo no.` : (state.outcome?.label ?? 'done') }
}
case 'confirm':
if (state.pending === null) return { text: 'Nothing is waiting for a confirm.' }
if (!intent.isYes) {
control.runner.cancel()
return { text: 'Cancelled.' }
}
await control.runner.confirm()
return { text: state.outcome === null ? 'Ran.' : `${state.outcome.ok ? '✓' : '✗'} ${state.outcome.label}: ${state.outcome.detail}${state.outcome.verified === 'yes' ? ' (on disk)' : state.outcome.verified === 'no' ? ' (not on disk yet)' : ''}` }
case 'agent': {
const who = intent.who.toLowerCase()
const agent = state.snapshot?.agents.find(entry => entry.id.toLowerCase() === who || entry.name?.toLowerCase() === who) ?? state.snapshot?.agents.find(entry => entry.id.toLowerCase().endsWith(who))
if (agent === undefined) return { text: `No agent "${plain(intent.who, 40)}" in .claude-flow/agents/store.json.` }
control.drill(agent.id)
return open(control, state, `agent ${agent.name ?? agent.type}`)
}
case 'back':
control.actions.back()
return open(control, state, VIEWS.find(view => view.id === state.view)?.label ?? 'back')
case 'select':
control.actions.select(intent.by)
return { text: `selection moved (${intent.by > 0 ? 'next' : 'prev'}) on ${state.view}` }
case 'filter':
state.eventFilter = intent.filter
control.setView('events')
return open(control, state, `events · ${intent.filter}`)
case 'dump': {
const view = intent.view ?? state.view
const shown = state.view
return { text: await dumpOf(control, state, view) }
}
case 'commands':
return { text: commandsText(await loadCatalog(control), intent.query) }
case 'unknown':
return { text: `Unknown: "${plain(intent.word, 30)}". /ruflo help lists the views (${VIEWS.map(view => view.id).join(', ')}) and commands.` }
}
}