The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(telemetry): Ink TUI reports each typed slash command to shared metrics
teknium1d3e667e04b61
diff --git a/ui-tui/src/__tests__/createSlashHandler.test.ts b/ui-tui/src/__tests__/createSlashHandler.test.tsindex 804753b9a02..2750e928db3 100644--- a/ui-tui/src/__tests__/createSlashHandler.test.ts+++ b/ui-tui/src/__tests__/createSlashHandler.test.ts@@ -56,7 +56,7 @@ describe('createSlashHandler', () => {expect(createSlashHandler(ctx)('/sessions')).toBe(true)expect(getOverlayState().sessions).toBe(true)expect(ctx.session.guardBusySessionSwitch).not.toHaveBeenCalled()- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])})it('blocks immediate resume-by-id while a turn is busy', () => {@@ -81,14 +81,14 @@ describe('createSlashHandler', () => {expect(createSlashHandler(ctx)('/grid-test 6x4')).toBe(true)expect(getOverlayState().widget).toMatchObject({ appId: 'grid-test' })expect(getOverlayState().widget?.state).toMatchObject({ cols: 6, nested: false, rows: 4, streams: false })- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])})it('handles /redraw locally without slash worker fallback', () => {const ctx = buildCtx()expect(createSlashHandler(ctx)('/redraw')).toBe(true)- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])})it('opens the editor locally for /prompt without slash worker fallback', () => {@@ -96,7 +96,7 @@ describe('createSlashHandler', () => {expect(createSlashHandler(ctx)('/prompt')).toBe(true)expect(ctx.composer.openEditor).toHaveBeenCalledTimes(1)- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])})it('routes /compose to the editor and seeds inline text', () => {@@ -112,7 +112,7 @@ describe('createSlashHandler', () => {expect(createSlashHandler(ctx)('/quit')).toBe(true)expect(ctx.session.die).toHaveBeenCalledTimes(1)- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])})it('keeps hosted dashboard chat alive for /exit', () => {@@ -121,7 +121,7 @@ describe('createSlashHandler', () => {expect(createSlashHandler(ctx)('/exit')).toBe(true)expect(ctx.session.die).not.toHaveBeenCalled()- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])expect(ctx.transcript.sys).toHaveBeenCalledWith(DASHBOARD_EXIT_DISABLED_MESSAGE)})@@ -130,7 +130,7 @@ describe('createSlashHandler', () => {const ctx = buildCtx()expect(createSlashHandler(ctx)('/update')).toBe(true)- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])// Advance past the 100ms setTimeoutvi.advanceTimersByTime(150)@@ -146,7 +146,7 @@ describe('createSlashHandler', () => {expect(createSlashHandler(ctx)('/update')).toBe(true)expect(ctx.session.dieWithCode).not.toHaveBeenCalled()- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])expect(ctx.transcript.sys).toHaveBeenCalledWith(DASHBOARD_UPDATE_DISABLED_MESSAGE)vi.advanceTimersByTime(150)@@ -162,7 +162,7 @@ describe('createSlashHandler', () => {expect(createSlashHandler(ctx)('/status')).toBe(true)expect(rpc).toHaveBeenCalledWith('session.status', { session_id: 'sid-abc' })- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])await vi.waitFor(() => {expect(ctx.transcript.page).toHaveBeenCalledWith('Hermes TUI Status', 'Status')})@@ -369,7 +369,7 @@ describe('createSlashHandler', () => {expect(createSlashHandler(ctx)('/skills')).toBe(true)expect(getOverlayState().skillsHub).toBe(true)expect(ctx.gateway.rpc).not.toHaveBeenCalled()- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])})it('routes /skills install <name> to skills.manage without opening overlay', () => {@@ -388,7 +388,7 @@ describe('createSlashHandler', () => {expect(createSlashHandler(ctx)('/pet list')).toBe(true)expect(getOverlayState().petPicker).toBe(true)- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])resetOverlayState()expect(createSlashHandler(ctx)('/pet')).toBe(true)@@ -464,7 +464,7 @@ describe('createSlashHandler', () => {getOverlayState().confirm?.onConfirm()expect(ctx.session.newSession).toHaveBeenCalledWith('new session started', undefined)- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])})it('keeps visible scrollback when branching a TUI session', async () => {@@ -506,7 +506,7 @@ describe('createSlashHandler', () => {expect.objectContaining({ canon: { '/new-skill': '/new-skill' }, pairs: [['/new-skill', 'demo']] }))})- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])})// Regressions from Copilot review on #19835: /voice output + frontend@@ -618,7 +618,7 @@ describe('createSlashHandler', () => {expect(createSlashHandler(ctx)(command)).toBe(true)expect(rpc).toHaveBeenCalledWith(method, params)- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])})it('renders browser connect progress messages from the gateway', async () => {@@ -654,7 +654,7 @@ describe('createSlashHandler', () => {expect(createSlashHandler(ctx)('/rollback')).toBe(true)expect(rpc).toHaveBeenCalledWith('rollback.list', { session_id: 'sid-abc' })- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])})it('hot-swaps the live indicator when /indicator <style> succeeds', async () => {@@ -793,7 +793,7 @@ describe('createSlashHandler', () => {expect(createSlashHandler(ctx)('/stat')).toBe(true)expect(ctx.transcript.sys).toHaveBeenCalledWith(expect.stringMatching(/\/status\b.*\/statusbar/))- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])})it('falls through to command.dispatch for skill commands, sending the body but showing the invocation', async () => {@@ -948,7 +948,7 @@ describe('createSlashHandler', () => {expect(body).toContain('hi there')expect(body).toContain('test')expect(body).not.toContain('ignore me')- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])})it('/history reports empty state without paging', () => {@@ -978,7 +978,7 @@ describe('createSlashHandler', () => {createSlashHandler(ctx)('/save')- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])expect(rpc).toHaveBeenCalledWith('session.save', { session_id: 'sid-abc' })await vi.waitFor(() => {@@ -992,7 +992,7 @@ describe('createSlashHandler', () => {createSlashHandler(ctx)('/save')- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])expect(rpc).not.toHaveBeenCalled()expect(ctx.transcript.sys).toHaveBeenCalled()})@@ -1070,7 +1070,7 @@ describe('createSlashHandler', () => {createSlashHandler(ctx)('/title my title')expect(rpc).toHaveBeenCalledWith('session.title', { session_id: 'sid-abc', title: 'my title' })- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])await vi.waitFor(() => {expect(ctx.transcript.sys).toHaveBeenCalledWith(expect.stringContaining('my title'))})@@ -1084,7 +1084,7 @@ describe('createSlashHandler', () => {createSlashHandler(ctx)('/title')expect(rpc).toHaveBeenCalledWith('session.title', { session_id: 'sid-abc' })- expect(ctx.gateway.gw.request).not.toHaveBeenCalled()+ expect(gatewayWork(ctx)).toEqual([])await vi.waitFor(() => {expect(ctx.transcript.sys).toHaveBeenCalledWith(expect.stringContaining('demo title'))})@@ -1112,6 +1112,12 @@ const buildComposer = () => ({setInput: vi.fn()})+// Gateway traffic other than the fire-and-forget shared_metrics.slash_command every typed command reports.+const gatewayWork = (ctx: { gateway: { gw: { request: unknown } } }) =>+ vi+ .mocked(ctx.gateway.gw.request as (method: string) => unknown)+ .mock.calls.filter(([method]) => method !== 'shared_metrics.slash_command')+const buildGateway = () => ({gw: {getLogTail: vi.fn(() => ''),diff --git a/ui-tui/src/app/createSlashHandler.ts b/ui-tui/src/app/createSlashHandler.tsindex fd8bc359fab..5b868ba38e6 100644--- a/ui-tui/src/app/createSlashHandler.ts+++ b/ui-tui/src/app/createSlashHandler.ts@@ -1,5 +1,6 @@import { parseCommandDispatch, parseSlashCommand } from '@hermes/shared/slash'+import type { GatewayClient } from '../gatewayClient.js'import type { SlashExecResponse } from '../gatewayTypes.js'import { rpcErrorMessage } from '../lib/rpc.js'import { launchWidget } from '../sdk/host.js'@@ -12,18 +13,38 @@ import type { SlashRunCtx } from './slash/types.js'import { getUiState } from './uiStore.js'import { describeSlashExecError, shouldFallbackToDispatch } from './userMessages.js'-export function createSlashHandler(ctx: SlashHandlerContext): (cmd: string) => boolean {+/** Shared metrics count each user-typed command once, from the client: the gateway no longer+ * counts slash.exec, so locally handled commands (/resume, /skin, overlays) land too.+ * Fire-and-forget; the backend canonicalizes the raw name. */+export function reportSlashCommand(gw: GatewayClient, name: string, sid: null | string | undefined): void {+ if (name) {+ gw.request('shared_metrics.slash_command', { command: name, ...(sid ? { session_id: sid } : {}) }).catch(+ () => undefined+ )+ }+}++/** `typed` is false for programmatic calls (a picker re-issuing `/model <x>`) and for the+ * backend's alias re-dispatch; prefix/alias expansion keeps it, so a typed `/hea` counts once+ * as the /heartbeat it resolved to. */+export function createSlashHandler(ctx: SlashHandlerContext): (cmd: string, typed?: boolean) => boolean {const { gw } = ctx.gatewayconst { catalog } = ctx.localconst { page, send, sys } = ctx.transcript- const handler = (cmd: string): boolean => {+ const handler = (cmd: string, typed = true): boolean => {const flight = ++ctx.slashFlightRef.currentconst ui = getUiState()const sid = ui.sidconst parsed = parseSlashCommand(cmd)const argTail = parsed.arg ? ` ${parsed.arg}` : ''+ const countTyped = () => {+ if (typed) {+ reportSlashCommand(gw, parsed.name, sid)+ }+ }+const stale = () => flight !== ctx.slashFlightRef.current || getUiState().sid !== sidconst guarded =@@ -45,6 +66,7 @@ export function createSlashHandler(ctx: SlashHandlerContext): (cmd: string) => bconst found = findSlashCommand(parsed.name)if (found) {+ countTyped()found.run(parsed.arg, runCtx, cmd)return true@@ -54,6 +76,7 @@ export function createSlashHandler(ctx: SlashHandlerContext): (cmd: string) => b// command table was built (user widgets from $HERMES_HOME/tui-widgets,// /widgets-reload) dispatch straight off the live registry.if (getWidgetApp(parsed.name)) {+ countTyped()const err = launchWidget(parsed.name, parsed.arg)if (err) {@@ -69,7 +92,7 @@ export function createSlashHandler(ctx: SlashHandlerContext): (cmd: string) => bif (exact) {if (exact.toLowerCase() !== needle) {- return handler(`${exact}${argTail}`)+ return handler(`${exact}${argTail}`, typed)}} else {// Tiered name scoring (ported from grok-cli's slash menu): prefix@@ -87,7 +110,7 @@ export function createSlashHandler(ctx: SlashHandlerContext): (cmd: string) => bconst matches = [...new Set(scored.filter(entry => entry.score === best).map(entry => entry.canon))]if (matches.length === 1 && matches[0]!.toLowerCase() !== needle) {- return handler(`${matches[0]}${argTail}`)+ return handler(`${matches[0]}${argTail}`, typed)}if (matches.length > 1) {@@ -110,7 +133,7 @@ export function createSlashHandler(ctx: SlashHandlerContext): (cmd: string) => b}if (d.type === 'alias') {- return void handler(`/${d.target}${argTail}`)+ return void handler(`/${d.target}${argTail}`, false)}// A skill/bundle dispatch's `message` is the expanded skill body —@@ -153,6 +176,7 @@ export function createSlashHandler(ctx: SlashHandlerContext): (cmd: string) => b}}+ countTyped()gw.request<SlashExecResponse>('slash.exec', { command: cmd.slice(1), session_id: sid }).then(r => {if (stale()) {diff --git a/ui-tui/src/app/useMainApp.ts b/ui-tui/src/app/useMainApp.tsindex a9003bafa0d..5f7cb30c2cb 100644--- a/ui-tui/src/app/useMainApp.ts+++ b/ui-tui/src/app/useMainApp.ts@@ -243,7 +243,7 @@ export function useMainApp(gw: GatewayClient) {)const slashFlightRef = useRef(0)- const slashRef = useRef<(cmd: string) => boolean>(() => false)+ const slashRef = useRef<(cmd: string, typed?: boolean) => boolean>(() => false)const colsRef = useRef(cols)const scrollRef = useRef<null | ScrollBoxHandle>(null)const onEventRef = useRef<(ev: AnyGatewayEvent) => void>(() => {})@@ -1204,7 +1204,7 @@ export function useMainApp(gw: GatewayClient) {const onModelSelect = useCallback((value: string) => {patchOverlayState({ modelPicker: false })- slashRef.current(`/model ${value}`)+ slashRef.current(`/model ${value}`, false) // the typed /model that opened the picker already counted}, [])const closeLiveSession = useCallback(diff --git a/ui-tui/src/app/useSubmission.ts b/ui-tui/src/app/useSubmission.tsindex 048f2f32406..76fe467e4a8 100644--- a/ui-tui/src/app/useSubmission.ts+++ b/ui-tui/src/app/useSubmission.ts@@ -11,6 +11,7 @@ import { asRpcResult } from '../lib/rpc.js'import { hasInterpolation, INTERPOLATION_RE } from '../protocol/interpolation.js'import type { Msg } from '../types.js'+import { reportSlashCommand } from './createSlashHandler.js'import type { ComposerActions, ComposerRefs, ComposerState, ComposerToken } from './interfaces.js'import { submitPrompt } from './submissionCore.js'import { turnController } from './turnController.js'@@ -277,6 +278,8 @@ export function useSubmission(opts: UseSubmissionOptions) {parsed.name === 'queue' || parsed.name === 'q' ? queueItemFromSlash(slash.display, slash.command) : undefinedif (queued) {+ // Handled here, before the slash handler, so it is counted here.+ reportSlashCommand(gw, parsed.name, getUiState().sid)composerActions.enqueue(queued.text, queued.display)sys(`queued: "${queued.display.slice(0, 50)}${queued.display.length > 50 ? '…' : ''}"`)} else {@@ -349,6 +352,7 @@ export function useSubmission(opts: UseSubmissionOptions) {appendMessage,composerActions,composerRefs,+ gw,handleBusyInput,interpolate,send,@@ -436,7 +440,7 @@ export interface UseSubmissionOptions {composerState: ComposerStategw: GatewayClientsetLastUserMsg: (value: string) => void- slashRef: MutableRefObject<(cmd: string) => boolean>+ slashRef: MutableRefObject<(cmd: string, typed?: boolean) => boolean>submitRef: MutableRefObject<(value: string) => void>sys: (text: string) => void}