From 9233518b11a8ad9bd2d5abf7a810f3a76a5ed0f1 Mon Sep 17 00:00:00 2001 From: DevinZeng Date: Fri, 3 Apr 2026 00:30:56 +0800 Subject: [PATCH] Enable and fix /buddy command The /buddy command was completely disabled by bun:bundle feature('BUDDY') flag which evaluates to false at runtime. Removed all feature('BUDDY') checks across the codebase to register the command, and added keyboard event handling (q/Enter to dismiss) which was missing from the UI. --- src/buddy/CompanionSprite.tsx | 5 +- src/buddy/observer.ts | 67 +++++++ src/buddy/prompt.ts | 2 - src/buddy/useBuddyNotification.tsx | 5 - src/commands.ts | 10 +- src/commands/buddy/buddy.tsx | 198 +++++++++++++++++++++ src/commands/buddy/index.ts | 11 ++ src/components/PromptInput/PromptInput.tsx | 15 +- src/screens/REPL.tsx | 17 +- src/utils/attachments.ts | 10 +- 10 files changed, 296 insertions(+), 44 deletions(-) create mode 100644 src/buddy/observer.ts create mode 100644 src/commands/buddy/buddy.tsx create mode 100644 src/commands/buddy/index.ts diff --git a/src/buddy/CompanionSprite.tsx b/src/buddy/CompanionSprite.tsx index f7f1f72..e60afa6 100644 --- a/src/buddy/CompanionSprite.tsx +++ b/src/buddy/CompanionSprite.tsx @@ -1,5 +1,4 @@ import { c as _c } from "react/compiler-runtime"; -import { feature } from 'bun:bundle'; import figures from 'figures'; import React, { useEffect, useRef, useState } from 'react'; import { useTerminalSize } from '../hooks/useTerminalSize.js'; @@ -165,7 +164,6 @@ function spriteColWidth(nameWidth: number): number { // Narrow terminals: 0 — REPL.tsx stacks the one-liner on its own row // (above input in fullscreen, below in scrollback), so no reservation. export function companionReservedColumns(terminalColumns: number, speaking: boolean): number { - if (!feature('BUDDY')) return 0; const companion = getCompanion(); if (!companion || getGlobalConfig().companionMuted) return 0; if (terminalColumns < MIN_COLS_FOR_FULL_SPRITE) return 0; @@ -212,7 +210,6 @@ export function CompanionSprite(): React.ReactNode { return () => clearTimeout(timer); // eslint-disable-next-line react-hooks/exhaustive-deps -- tick intentionally captured at reaction-change, not tracked }, [reaction, setAppState]); - if (!feature('BUDDY')) return null; const companion = getCompanion(); if (!companion || getGlobalConfig().companionMuted) return null; const color = RARITY_COLORS[companion.rarity]; @@ -337,7 +334,7 @@ export function CompanionFloatingBubble() { t3 = $[4]; } useEffect(t2, t3); - if (!feature("BUDDY") || !reaction) { + if (!reaction) { return null; } const companion = getCompanion(); diff --git a/src/buddy/observer.ts b/src/buddy/observer.ts new file mode 100644 index 0000000..ede2514 --- /dev/null +++ b/src/buddy/observer.ts @@ -0,0 +1,67 @@ +import type { Message } from '../types/message.js' +import { getCompanion } from './companion.js' +import { getGlobalConfig } from '../utils/config.js' + +// Simple companion observer: picks a reaction based on the last assistant message. +// This is a lightweight placeholder that generates fun reactions without an LLM call. + +const DEBUGGING_QUIPS = [ + 'Found it!', + 'Interesting...', + 'Have you tried rubber duck debugging?', + 'Stack trace time!', + 'I see what happened.', +] + +const GENERAL_QUIPS = [ + 'Looking good!', + 'Keep it up!', + 'Nice work!', + 'I believe in you!', + 'You got this!', +] + +const CODE_QUIPS = [ + 'Fancy!', + 'Clean code!', + 'Elegant solution!', + 'Ship it!', +] + +function pickQuip(messages: Message[]): string | undefined { + const lastAssistant = [...messages].reverse().find(m => m.role === 'assistant') + if (!lastAssistant) return undefined + + const content = Array.isArray(lastAssistant.content) + ? lastAssistant.content.map(c => (typeof c === 'string' ? c : c.type === 'text' ? c.text : '')).join('') + : typeof lastAssistant.content === 'string' + ? lastAssistant.content + : '' + + if (!content) return undefined + + // Only react occasionally (1 in 5 turns) + if (Math.random() > 0.2) return undefined + + const lower = content.toLowerCase() + if (lower.includes('error') || lower.includes('bug') || lower.includes('fix') || lower.includes('debug')) { + return DEBUGGING_QUIPS[Math.floor(Math.random() * DEBUGGING_QUIPS.length)] + } + if (lower.includes('function') || lower.includes('class') || lower.includes('const') || lower.includes('```')) { + return CODE_QUIPS[Math.floor(Math.random() * CODE_QUIPS.length)] + } + return GENERAL_QUIPS[Math.floor(Math.random() * GENERAL_QUIPS.length)] +} + +export async function fireCompanionObserver( + messages: Message[], + onReaction: (reaction: string) => void, +): Promise { + const companion = getCompanion() + if (!companion || getGlobalConfig().companionMuted) return + + const quip = pickQuip(messages) + if (quip) { + onReaction(quip) + } +} diff --git a/src/buddy/prompt.ts b/src/buddy/prompt.ts index c5782c0..302b8b9 100644 --- a/src/buddy/prompt.ts +++ b/src/buddy/prompt.ts @@ -1,4 +1,3 @@ -import { feature } from 'bun:bundle' import type { Message } from '../types/message.js' import type { Attachment } from '../utils/attachments.js' import { getGlobalConfig } from '../utils/config.js' @@ -15,7 +14,6 @@ When the user addresses ${name} directly (by name), its bubble will answer. Your export function getCompanionIntroAttachment( messages: Message[] | undefined, ): Attachment[] { - if (!feature('BUDDY')) return [] const companion = getCompanion() if (!companion || getGlobalConfig().companionMuted) return [] diff --git a/src/buddy/useBuddyNotification.tsx b/src/buddy/useBuddyNotification.tsx index d6eed22..fddcb70 100644 --- a/src/buddy/useBuddyNotification.tsx +++ b/src/buddy/useBuddyNotification.tsx @@ -1,5 +1,4 @@ import { c as _c } from "react/compiler-runtime"; -import { feature } from 'bun:bundle'; import React, { useEffect } from 'react'; import { useNotifications } from '../context/notifications.js'; import { Text } from '../ink.js'; @@ -50,9 +49,6 @@ export function useBuddyNotification() { let t1; if ($[0] !== addNotification || $[1] !== removeNotification) { t0 = () => { - if (!feature("BUDDY")) { - return; - } const config = getGlobalConfig(); if (config.companion || !isBuddyTeaserWindow()) { return; @@ -80,7 +76,6 @@ export function findBuddyTriggerPositions(text: string): Array<{ start: number; end: number; }> { - if (!feature('BUDDY')) return []; const triggers: Array<{ start: number; end: number; diff --git a/src/commands.ts b/src/commands.ts index 10f03b2..0d8ae71 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -115,11 +115,9 @@ const forkCmd = feature('FORK_SUBAGENT') require('./commands/fork/index.js') as typeof import('./commands/fork/index.js') ).default : null -const buddy = feature('BUDDY') - ? ( - require('./commands/buddy/index.js') as typeof import('./commands/buddy/index.js') - ).default - : null +const buddy = ( + require('./commands/buddy/index.js') as typeof import('./commands/buddy/index.js') +).default /* eslint-enable @typescript-eslint/no-require-imports */ import thinkback from './commands/thinkback/index.js' import thinkbackPlay from './commands/thinkback-play/index.js' @@ -319,7 +317,7 @@ const COMMANDS = memoize((): Command[] => [ vim, ...(webCmd ? [webCmd] : []), ...(forkCmd ? [forkCmd] : []), - ...(buddy ? [buddy] : []), + buddy, ...(proactive ? [proactive] : []), ...(briefCommand ? [briefCommand] : []), ...(assistantCommand ? [assistantCommand] : []), diff --git a/src/commands/buddy/buddy.tsx b/src/commands/buddy/buddy.tsx new file mode 100644 index 0000000..56d7e9c --- /dev/null +++ b/src/commands/buddy/buddy.tsx @@ -0,0 +1,198 @@ +import * as React from 'react' +import { Box, Text } from '../../ink.js' +import type { LocalJSXCommandCall } from '../../types/command.js' +import { + getCompanion, + roll, + companionUserId, +} from '../../buddy/companion.js' +import { renderSprite } from '../../buddy/sprites.js' +import { + RARITY_COLORS, + RARITY_STARS, + STAT_NAMES, + type StoredCompanion, +} from '../../buddy/types.js' +import { saveGlobalConfig } from '../../utils/config.js' + +function CompanionCard({ + onDone, + args, + setAppState, +}: { + onDone: (result?: string, options?: { display?: string }) => void + args: string + setAppState: (updater: (prev: any) => any) => void +}) { + const trimmed = args.trim().toLowerCase() + const companion = getCompanion() + + // Handle keyboard input to dismiss + const handleKeyDown = (e: any) => { + if (e.key === 'q' || e.key === 'Enter') { + e.preventDefault() + onDone() + } + } + + // Handle subcommands + React.useEffect(() => { + if (trimmed === 'mute') { + saveGlobalConfig(c => ({ ...c, companionMuted: true })) + onDone(`${companion?.name ?? 'Companion'} is now muted.`, { + display: 'system', + }) + return + } + + if (trimmed === 'unmute') { + saveGlobalConfig(c => ({ ...c, companionMuted: false })) + onDone(`${companion?.name ?? 'Companion'} says hello!`, { + display: 'system', + }) + return + } + + if (trimmed === 'pet') { + if (!companion) { + onDone('You need to hatch a companion first! Use /buddy hatch', { + display: 'system', + }) + return + } + setAppState((prev: any) => ({ ...prev, companionPetAt: Date.now() })) + onDone(`You pet ${companion.name}! ♥`, { display: 'system' }) + return + } + + if (trimmed === 'hatch') { + if (companion) { + onDone( + `You already have ${companion.name}! Use /buddy info to see them.`, + { display: 'system' }, + ) + return + } + // Hatch a new companion with a generated name + const { bones } = roll(companionUserId()) + const adjectives = [ + 'Bright', 'Cozy', 'Swift', 'Calm', 'Wise', 'Bold', + 'Fuzzy', 'Lucky', 'Snappy', 'Quirky', + ] + const nouns = [ + 'Spark', 'Pixel', 'Ember', 'Glitch', 'Byte', + 'Flux', 'Drift', 'Blip', 'Quip', 'Zap', + ] + const adj = adjectives[Math.floor(Math.random() * adjectives.length)]! + const noun = nouns[Math.floor(Math.random() * nouns.length)]! + const name = `${adj} ${noun}` + const soul: StoredCompanion = { + name, + personality: `A ${bones.rarity} ${bones.species} who loves debugging and hanging out.`, + hatchedAt: Date.now(), + } + saveGlobalConfig(c => ({ ...c, companion: soul })) + onDone( + `✨ You hatched ${name} the ${bones.rarity} ${bones.species}! Say hello!`, + { display: 'system' }, + ) + return + } + + if (trimmed === 'release') { + if (!companion) { + onDone('No companion to release.', { display: 'system' }) + return + } + const name = companion.name + saveGlobalConfig(c => { + const next = { ...c } + delete next.companion + return next + }) + onDone(`Goodbye, ${name}! You'll be missed.`, { display: 'system' }) + return + } + }, []) + + // Render companion info + if (!companion) { + const { bones } = roll(companionUserId()) + const preview = renderSprite(bones, 0) + const color = RARITY_COLORS[bones.rarity] + return ( + + You haven't hatched a companion yet! + Here's a preview of yours: + + {preview.map((line, i) => ( + + {line} + + ))} + + A {bones.rarity} {bones.species} {RARITY_STARS[bones.rarity]} + + + Run /buddy hatch to bring them to life! + Or type q to dismiss. + + ) + } + + const sprite = renderSprite(companion, 0) + const color = RARITY_COLORS[companion.rarity] + + return ( + + + + {sprite.map((line, i) => ( + + {line} + + ))} + + {companion.name} + + + + + Species:{' '} + {companion.species} + + + Rarity:{' '} + + {companion.rarity} {RARITY_STARS[companion.rarity]} + + + {companion.shiny && ✦ Shiny!} + {'─'.repeat(20)} + Stats: + {STAT_NAMES.map(stat => ( + + {stat}:{' '} + {companion.stats[stat]} + + ))} + + + {'─'.repeat(40)} + + /buddy pet · /buddy mute · /buddy unmute · /buddy release + + Press q or Enter to dismiss + + ) +} + +export const call: LocalJSXCommandCall = async (onDone, context, args = '') => { + return ( + + ) +} diff --git a/src/commands/buddy/index.ts b/src/commands/buddy/index.ts new file mode 100644 index 0000000..0975fef --- /dev/null +++ b/src/commands/buddy/index.ts @@ -0,0 +1,11 @@ +import type { Command } from '../../commands.js' + +const buddyCommand = { + type: 'local-jsx', + name: 'buddy', + description: 'Meet your companion', + argumentHint: '[hatch|pet|mute|unmute|info]', + load: () => import('./buddy.js'), +} satisfies Command + +export default buddyCommand diff --git a/src/components/PromptInput/PromptInput.tsx b/src/components/PromptInput/PromptInput.tsx index 128e73c..0f7263d 100644 --- a/src/components/PromptInput/PromptInput.tsx +++ b/src/components/PromptInput/PromptInput.tsx @@ -309,10 +309,7 @@ function PromptInput({ const { companion: _companion, companionMuted - } = feature('BUDDY') ? getGlobalConfig() : { - companion: undefined, - companionMuted: undefined - }; + } = getGlobalConfig(); const companionFooterVisible = !!_companion && !companionMuted; // Brief mode: BriefSpinner/BriefIdleStatus own the 2-row footprint above // the input. Dropping marginTop here lets the spinner sit flush against @@ -1786,10 +1783,8 @@ function PromptInput({ } switch (footerItemSelected) { case 'companion': - if (feature('BUDDY')) { - selectFooterItem(null); - void onSubmit('/buddy'); - } + selectFooterItem(null); + void onSubmit('/buddy'); break; case 'tasks': if (isTeammateMode) { @@ -1981,9 +1976,9 @@ function PromptInput({ }); }, [effortNotificationText, addNotification, removeNotification]); useBuddyNotification(); - const companionSpeaking = feature('BUDDY') ? + const companionSpeaking = // biome-ignore lint/correctness/useHookAtTopLevel: feature() is a compile-time constant - useAppState(s => s.companionReaction !== undefined) : false; + useAppState(s => s.companionReaction !== undefined); const { columns, rows diff --git a/src/screens/REPL.tsx b/src/screens/REPL.tsx index 11cc4d8..56b2665 100644 --- a/src/screens/REPL.tsx +++ b/src/screens/REPL.tsx @@ -274,6 +274,7 @@ const WebBrowserPanelModule = feature('WEB_BROWSER_TOOL') ? require('../tools/We import { IssueFlagBanner } from '../components/PromptInput/IssueFlagBanner.js'; import { useIssueFlagBanner } from '../hooks/useIssueFlagBanner.js'; import { CompanionSprite, CompanionFloatingBubble, MIN_COLS_FOR_FULL_SPRITE } from '../buddy/CompanionSprite.js'; +import { fireCompanionObserver } from '../buddy/observer.js'; import { DevBar } from '../components/DevBar.js'; // Session manager removed - using AppState now import type { RemoteSessionConfig } from '../remote/RemoteSessionManager.js'; @@ -1299,12 +1300,10 @@ export function REPL({ // Dismiss the companion bubble on scroll — it's absolute-positioned // at bottom-right and covers transcript content. Scrolling = user is // trying to read something under it. - if (feature('BUDDY')) { - setAppState(prev => prev.companionReaction === undefined ? prev : { + setAppState(prev => prev.companionReaction === undefined ? prev : { ...prev, companionReaction: undefined }); - } } }, [onRepin, onScrollAway, maybeLoadOlder, setAppState]); // Deferred SessionStart hook messages — REPL renders immediately and @@ -2801,12 +2800,10 @@ export function REPL({ })) { onQueryEvent(event); } - if (feature('BUDDY')) { - void fireCompanionObserver(messagesRef.current, reaction => setAppState(prev => prev.companionReaction === reaction ? prev : { + void fireCompanionObserver(messagesRef.current, reaction => setAppState(prev => prev.companionReaction === reaction ? prev : { ...prev, companionReaction: reaction })); - } queryCheckpoint('query_end'); // Capture ant-only API metrics before resetLoadingState clears the ref. @@ -4562,7 +4559,7 @@ export function REPL({ {feature('MESSAGE_ACTIONS') && isFullscreenEnvEnabled() && !disableMessageActions ? : null} - : undefined} modal={centeredModal} modalScrollRef={modalScrollRef} dividerYRef={dividerYRef} hidePill={!!viewedAgentTask} hideSticky={!!viewedTeammateTask} newMessageCount={unseenDivider?.count ?? 0} onPillClick={() => { + : undefined} modal={centeredModal} modalScrollRef={modalScrollRef} dividerYRef={dividerYRef} hidePill={!!viewedAgentTask} hideSticky={!!viewedTeammateTask} newMessageCount={unseenDivider?.count ?? 0} onPillClick={() => { setCursor(null); jumpToNew(scrollRef.current); }} scrollable={<> @@ -4587,8 +4584,8 @@ export function REPL({ {showSpinner && 0} leaderIsIdle={!isLoading} />} {!showSpinner && !isLoading && !userInputOnProcessing && !hasRunningTeammates && isBriefOnly && !viewedAgentTask && } {isFullscreenEnvEnabled() && } - } bottom={ - {feature('BUDDY') && companionNarrow && isFullscreenEnvEnabled() && companionVisible ? : null} + } bottom={ + {companionNarrow && isFullscreenEnvEnabled() && companionVisible ? : null} {permissionStickyFooter} {/* Immediate local-jsx commands (/btw, /sandbox, /assistant, @@ -4992,7 +4989,7 @@ export function REPL({ }} />} {"external" === 'ant' && } - {feature('BUDDY') && !(companionNarrow && isFullscreenEnvEnabled()) && companionVisible ? : null} + {!(companionNarrow && isFullscreenEnvEnabled()) && companionVisible ? : null} } /> ; diff --git a/src/utils/attachments.ts b/src/utils/attachments.ts index 8a1612a..556defe 100644 --- a/src/utils/attachments.ts +++ b/src/utils/attachments.ts @@ -861,13 +861,9 @@ export async function getAttachments( ), ), ), - ...(feature('BUDDY') - ? [ - maybe('companion_intro', () => - Promise.resolve(getCompanionIntroAttachment(messages)), - ), - ] - : []), + maybe('companion_intro', () => + Promise.resolve(getCompanionIntroAttachment(messages)), + ), maybe('changed_files', () => getChangedFiles(context)), maybe('nested_memory', () => getNestedMemoryAttachments(context)), // relevant_memories moved to async prefetch (startRelevantMemoryPrefetch)