claude-code-best/src
claude-code-best 5a7d06fe99
refactor(buddy): align companion system with official CLI (#82)
* refactor(buddy): align companion system with official CLI

## Summary

Reverse-engineered the official Claude Code CLI (v2.1.91) buddy/companion
system and aligned our implementation to match.

## Changes (7 files)

### Added
- `src/buddy/CompanionCard.tsx` (+109)
  JSX bordered card matching official vc8: rarity header, colored sprite,
  name, personality, 10-bar stats, last reaction in nested border.

- `src/buddy/companionReact.ts` (+156)
  Reaction system matching official ZUK+Dc8: 45s rate limiting, @-mention
  detection, transcript builder (12 msgs, 5000 chars), POST buddy_react API.

### Modified
- `src/commands/buddy/index.ts`
  type: local -> local-jsx, description/argumentHint/immediate/isHidden.

- `src/commands/buddy/buddy.ts`
  LocalCommandCall -> LocalJSXCommandCall signature (onDone, context, args).
  Removed mute/unmute/rehatch (official uses off/on only).
  /buddy show returns CompanionCard JSX instead of plain text.
  Pet auto-unmutes. companionMuted writes globalConfig (matches UI read source).

- `src/screens/REPL.tsx` (line 2808)
  globalThis.fireCompanionObserver -> import triggerCompanionReaction.

- `src/state/AppStateStore.ts` — comment fix.
- `src/types/global.d.ts` — removed fireCompanionObserver declaration.

## Data flow (verified consistent)
- companionMuted: saveGlobalConfig() <-> getGlobalConfig() (6 read sites)
- companionReaction: setAppState() <-> useAppState() (4 sites)
- companionPetAt: setAppState() <-> useAppState() (2 sites)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(buddy): address CodeRabbit review findings

- buddy.ts: return type Promise<null> → Promise<React.ReactNode>
  to match LocalJSXCommandCall interface (CompanionCard path returns
  ReactElement, not null).
- CompanionCard.tsx: clamp stat value to 0..100 before .repeat()
  to prevent negative count runtime error on out-of-range values.

Import path alias suggestions (src/ vs ../) dismissed — project
convention uses relative paths (verified against color.ts, help.ts).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(buddy): address second round CodeRabbit findings

- buddy.ts:105: remove unsafe (context as any).messages cast.
  ToolUseContext already declares messages: Message[] at Tool.ts:250,
  so context.messages is properly typed. Other commands (feedback,
  copy, export) access it the same way without cast.

- companionReact.ts:154: wrap resp.json() in try/catch for defensive
  JSON parsing. Malformed 200 responses now return null instead of
  propagating to the outer catch.

Rate-limit timing (set before API call) kept as-is — matches official
ZUK pattern: prevents retry-storm on transient failures.

src/ path alias suggestions dismissed — project uses relative paths.
Auto-unmute on /buddy view kept — matches official behavior.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: unraid <local@unraid.local>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 17:22:54 +08:00
..
__tests__ test: 添加一大堆测试文件 2026-04-02 20:28:08 +08:00
assistant feat: 全面清理类型错误 — tsc 零错误,any 标注全部消除 2026-04-01 01:00:10 +08:00
bootstrap feat: 问就是封包 2026-03-31 23:32:58 +08:00
bridge fix: validate and encode target sessionId in peer messages 2026-04-03 04:23:32 +08:00
buddy fix(buddy): address second round CodeRabbit findings 2026-04-03 17:16:16 +08:00
cli fix: 彻底移除 /loop 及 cron 工具的 feature('AGENT_TRIGGERS') gate 2026-04-01 11:57:16 +08:00
commands fix(buddy): address second round CodeRabbit findings 2026-04-03 17:16:16 +08:00
components feat: /login 命令新增自定义 anthropic 终端登陆 2026-04-03 14:22:47 +08:00
constants fix: 修复定义导入缺失的问题 2026-04-03 14:14:35 +08:00
context feat: 完成第二版类型清理 2026-03-31 23:03:47 +08:00
coordinator feat: 全面清理类型错误 — tsc 零错误,any 标注全部消除 2026-04-01 01:00:10 +08:00
daemon feat: 全面清理类型错误 — tsc 零错误,any 标注全部消除 2026-04-01 01:00:10 +08:00
entrypoints Merge branch 'pr/amDosion/60' 2026-04-03 17:21:09 +08:00
environment-runner feat: 全面清理类型错误 — tsc 零错误,any 标注全部消除 2026-04-01 01:00:10 +08:00
hooks feat: enable Remote Control (BRIDGE_MODE) with stub completions 2026-04-03 03:50:36 +08:00
ink feat: 全面清理类型错误 — tsc 零错误,any 标注全部消除 2026-04-01 01:00:10 +08:00
jobs feat: 全面清理类型错误 — tsc 零错误,any 标注全部消除 2026-04-01 01:00:10 +08:00
keybindings feat: 全面清理类型错误 — tsc 零错误,any 标注全部消除 2026-04-01 01:00:10 +08:00
memdir feat: 全面清理类型错误 — tsc 零错误,any 标注全部消除 2026-04-01 01:00:10 +08:00
migrations feat: 完成stub 2026-03-31 20:40:16 +08:00
moreright feat: 完成第二版类型清理 2026-03-31 23:03:47 +08:00
native-ts fix: 修复 @ typeahead 文件搜索无结果的问题 2026-04-01 17:11:09 +08:00
outputStyles feat: build 2026-03-31 19:22:47 +08:00
plugins feat: build 2026-03-31 19:22:47 +08:00
proactive feat: 全面清理类型错误 — tsc 零错误,any 标注全部消除 2026-04-01 01:00:10 +08:00
query feat: 完成第二版类型清理 2026-03-31 23:03:47 +08:00
remote feat: 完成第二版类型清理 2026-03-31 23:03:47 +08:00
schemas feat: 完成stub 2026-03-31 20:40:16 +08:00
screens refactor(buddy): align companion system with official CLI 2026-04-03 16:36:22 +08:00
self-hosted-runner feat: 全面清理类型错误 — tsc 零错误,any 标注全部消除 2026-04-01 01:00:10 +08:00
server feat: 全面清理类型错误 — tsc 零错误,any 标注全部消除 2026-04-01 01:00:10 +08:00
services feat: 添加 GrowthBook 自定义服务器适配器 2026-04-03 10:37:15 +08:00
skills fix: 彻底移除 /loop 及 cron 工具的 feature('AGENT_TRIGGERS') gate 2026-04-01 11:57:16 +08:00
ssh feat: 全面清理类型错误 — tsc 零错误,any 标注全部消除 2026-04-01 01:00:10 +08:00
state refactor(buddy): align companion system with official CLI 2026-04-03 16:36:22 +08:00
tasks feat: 全面清理类型错误 — tsc 零错误,any 标注全部消除 2026-04-01 01:00:10 +08:00
tools feat: 修正 web search 工具 2026-04-03 00:47:37 +08:00
types refactor(buddy): align companion system with official CLI 2026-04-03 16:36:22 +08:00
upstreamproxy feat: 问就是封包 2026-03-31 23:32:58 +08:00
utils fix: 修复 ERROR getAntModels is not defined 2026-04-03 11:56:49 +08:00
vim feat: build 2026-03-31 19:22:47 +08:00
voice feat: build 2026-03-31 19:22:47 +08:00
commands.ts feat: 更新 buddy 的一些功能 2026-04-02 21:41:19 +08:00
context.ts feat: build 2026-03-31 19:22:47 +08:00
cost-tracker.ts feat: build 2026-03-31 19:22:47 +08:00
costHook.ts feat: build 2026-03-31 19:22:47 +08:00
dialogLaunchers.tsx feat: 完成第二版类型清理 2026-03-31 23:03:47 +08:00
history.ts feat: build 2026-03-31 19:22:47 +08:00
ink.ts feat: build 2026-03-31 19:22:47 +08:00
interactiveHelpers.tsx feat: 完成第二版类型清理 2026-03-31 23:03:47 +08:00
main.tsx fix: 批量修正 external 字面量 2026-04-02 17:01:39 +08:00
projectOnboardingState.ts feat: build 2026-03-31 19:22:47 +08:00
query.ts feat: 全面清理类型错误 — tsc 零错误,any 标注全部消除 2026-04-01 01:00:10 +08:00
QueryEngine.ts feat: 全面清理类型错误 — tsc 零错误,any 标注全部消除 2026-04-01 01:00:10 +08:00
replLauncher.tsx feat: 完成第二版类型清理 2026-03-31 23:03:47 +08:00
setup.ts feat: build 2026-03-31 19:22:47 +08:00
Task.ts feat: build 2026-03-31 19:22:47 +08:00
tasks.ts feat: build 2026-03-31 19:22:47 +08:00
Tool.ts feat: 完成第二版类型清理 2026-03-31 23:03:47 +08:00
tools.ts fix: 彻底移除 /loop 及 cron 工具的 feature('AGENT_TRIGGERS') gate 2026-04-01 11:57:16 +08:00