mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-16 13:25:51 +00:00
Compare commits
4 Commits
memory-lea
...
fix/third-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
941bcbd240 | ||
|
|
5c107e5f8c | ||
|
|
c4e9efb7a8 | ||
|
|
26ddbda849 |
225
docs/features/background-agent-selector.md
Normal file
225
docs/features/background-agent-selector.md
Normal file
@@ -0,0 +1,225 @@
|
||||
# Background Agent Selector — 底部统一后台 Agent 切换器
|
||||
|
||||
> Feature Flag: 无(直接启用)
|
||||
> 实现状态:完整可用
|
||||
> 依赖:`viewingAgentTaskId` / `enterTeammateView` / `exitTeammateView` 已有机制
|
||||
|
||||
## 一、功能概述
|
||||
|
||||
Background Agent Selector 是渲染在 PromptInput 下方的常驻状态条,列出当前所有 **backgrounded 的 local_agent 任务**(包括 `/fork` 派生的 fork agent 和 Task/AgentTool 调用 `run_in_background: true` 派生的子 agent)。用户可以用 ↑/↓ 方向键在 `main` 和各 agent 之间切换焦点,按 Enter 把 REPL 主视图替换为所选 agent 的实时 transcript,再按 Enter 选中 `main` 即可回到主对话。
|
||||
|
||||
整个机制完全复用官方已有的 teammate transcript 查看基础设施,不引入新的视图层 / 数据流,仅新增一条 footer pill 类型。
|
||||
|
||||
### 核心特性
|
||||
|
||||
- **统一入口**:`/fork`、Task 派生的 subagent、所有 `run_in_background: true` 的 agent 都在同一栏显示
|
||||
- **就地切换**:prompt 为空时按 ↓ 溢出进入底部 selector,↑↓ 选中某行,Enter 即切主视图
|
||||
- **实时状态**:每行显示 agent 类型 + 描述 + 运行时长 + 已消耗 token;running 时圆点为绿色
|
||||
- **Keep-alive 视图**:agent 完成后在 `evictAfter` grace 窗口内保留一段时间,用户可回看
|
||||
- **零界面侵入**:tasks 数为 0 时 selector 完全不渲染,不占屏幕高度
|
||||
- **与旧 Dialog 共存**:Shift+↓ 打开的 `BackgroundTasksDialog` 原有行为保留,selector 只作为展示 + 快捷切换
|
||||
|
||||
## 二、用户交互
|
||||
|
||||
### 触发方式
|
||||
|
||||
有任何 background agent 时,selector 自动出现在 `bypass permissions on` 行下方:
|
||||
|
||||
```
|
||||
claude-code | Opus 4.7 (1M context) | ctx:4%
|
||||
▶▶ bypass permissions on (shift+tab to cycle)
|
||||
|
||||
○ main ↑/↓ to select · Enter to view
|
||||
● Explore Research src/hooks 23s · ↓ 10.9k tokens
|
||||
○ Explore Research src/components 22s · ↓ 9.5k tokens
|
||||
○ Explore Research src/utils 21s · ↓ 13.6k tokens
|
||||
```
|
||||
|
||||
### 键盘路由
|
||||
|
||||
| 位置 / 状态 | 按键 | 行为 |
|
||||
|---|---|---|
|
||||
| PromptInput 非空 | ↑↓ | 光标移动 / 翻历史(不变) |
|
||||
| PromptInput 空 + 历史底部 | ↓ | 焦点下放到 selector,高亮到 `● main` |
|
||||
| Selector 聚焦(`footerSelection === 'bg_agent'`) | ↓ | 高亮下移,-1 → 0 → ... → N-1 |
|
||||
| Selector 聚焦 | ↑ | 高亮上移;在 `main` 再 ↑ → 焦点回 PromptInput |
|
||||
| Selector 聚焦 | Enter | `-1` → `exitTeammateView`;`>=0` → `enterTeammateView(agentId)`。焦点保留在 pill |
|
||||
| Selector 聚焦 | Esc | `footer:clearSelection`,焦点回 PromptInput |
|
||||
|
||||
### 视觉规则
|
||||
|
||||
- `● main` / `● <agent>`:当前被**查看**(viewingAgentTaskId 指向)或被**光标聚焦**(pill focused 时以光标为准)的一行
|
||||
- running 状态的 agent:圆点渲染为 `success` 色(绿色),与 `BackgroundTasksDialog` 状态语义对齐
|
||||
- 右上角 hint 随状态变化:
|
||||
- pill 聚焦:`↑/↓ to select · Enter to view`
|
||||
- 已选中 running agent:`shift+↓ to manage · x to stop`
|
||||
- 已选中 terminal agent:`shift+↓ to manage · x to clear`
|
||||
- 未选中任何 agent:`shift+↓ to manage background agents`
|
||||
|
||||
## 三、实现架构
|
||||
|
||||
### 3.1 数据层:`useBackgroundAgentTasks`
|
||||
|
||||
文件:`src/hooks/useBackgroundAgentTasks.ts`
|
||||
|
||||
封装对 `useAppState(s => s.tasks)` 的过滤:
|
||||
|
||||
```ts
|
||||
export function useBackgroundAgentTasks(): LocalAgentTaskState[] {
|
||||
const tasks = useAppState(s => s.tasks)
|
||||
return useMemo(() => {
|
||||
const now = Date.now()
|
||||
return Object.values(tasks)
|
||||
.filter(isLocalAgentTask)
|
||||
.filter(t => t.agentType !== 'main-session')
|
||||
.filter(t => t.isBackgrounded !== false)
|
||||
.filter(t => t.evictAfter === undefined || t.evictAfter > now)
|
||||
.sort((a, b) => a.startTime - b.startTime)
|
||||
}, [tasks])
|
||||
}
|
||||
```
|
||||
|
||||
`/fork` 和 `AgentTool` 的 `run_in_background: true` 底层都走 `registerAsyncAgent → runAsyncAgentLifecycle`,最终写入同一个 `appState.tasks` Map;此 hook 是唯一数据源,Selector 和 PromptInput 的 `bgAgentList` 都消费它。
|
||||
|
||||
### 3.2 状态层:新增两个字段
|
||||
|
||||
文件:`src/state/AppStateStore.ts`
|
||||
|
||||
```ts
|
||||
export type FooterItem =
|
||||
| 'tasks' | 'tmux' | 'bagel' | 'teams' | 'bridge' | 'companion'
|
||||
| 'bg_agent' // ← 新增
|
||||
|
||||
export type AppState = DeepImmutable<{
|
||||
// ...
|
||||
selectedBgAgentIndex: number // -1 = main, 0..N-1 = 选中的 agent
|
||||
}>
|
||||
```
|
||||
|
||||
- `'bg_agent'` 作为 `FooterItem` 加入 footer pill 体系,享受既有的 `footer:up` / `footer:down` / `footer:openSelected` keybinding 路由
|
||||
- `selectedBgAgentIndex` 记录 selector 的光标位置,与 `viewingAgentTaskId`("正在看什么")独立;它不可从 `viewingAgentTaskId` 派生——Enter 后光标留在 pill 继续导航,查看目标才变
|
||||
|
||||
### 3.3 键盘路由:PromptInput footer pill 分支
|
||||
|
||||
文件:`src/components/PromptInput/PromptInput.tsx`
|
||||
|
||||
1. **`bg_agent` 进入 footerItems[0]**:保证 prompt ↓ 溢出时(`handleHistoryDown` → `selectFooterItem(footerItems[0])`)直接进入 selector,而不是 `tasks` 等其他 pill
|
||||
2. **`footer:up` 分支**:`bgAgentSelected` 时 `selectedBgAgentIndex > -1` 则递减;在 -1 → `selectFooterItem(null)` 退出 pill
|
||||
3. **`footer:down` 分支**:`selectedBgAgentIndex < bgAgentList.length - 1` 则递增,到底 clamp
|
||||
4. **`footer:openSelected` 分支**:index === -1 → `exitTeammateView`;否则 `enterTeammateView(bgAgentList[i].agentId)`。**不清理 pill 焦点**,光标留在 selector 上继续导航
|
||||
5. **`selectFooterItem('bg_agent')`**:入 pill 时重置 `selectedBgAgentIndex = -1`(光标落到 `main`)
|
||||
|
||||
### 3.4 渲染层:`BackgroundAgentSelector`
|
||||
|
||||
文件:`src/components/tasks/BackgroundAgentSelector.tsx`
|
||||
|
||||
纯展示组件,不订阅键盘:
|
||||
|
||||
```tsx
|
||||
const tasks = useBackgroundAgentTasks()
|
||||
const viewingId = useAppState(s => s.viewingAgentTaskId)
|
||||
const footerSelection = useAppState(s => s.footerSelection)
|
||||
const selectedBgIndex = useAppState(s => s.selectedBgAgentIndex)
|
||||
|
||||
if (tasks.length === 0) return null
|
||||
|
||||
const pillFocused = footerSelection === 'bg_agent'
|
||||
const highlightedId = pillFocused
|
||||
? (selectedBgIndex === -1 ? null : tasks[selectedBgIndex]?.agentId ?? null)
|
||||
: (viewingId ?? null)
|
||||
```
|
||||
|
||||
**高亮派生规则**:pill 聚焦 → 跟 `selectedBgAgentIndex`;未聚焦 → 镜像 `viewingAgentTaskId`。这样当用户通过 Shift+↓ Dialog 或 `enterTeammateView` 其它途径切换视图时,selector 也会正确反映。
|
||||
|
||||
### 3.5 主视图切换:复用 `viewingAgentTaskId`
|
||||
|
||||
REPL.tsx 主体仍复用原有查看逻辑:
|
||||
|
||||
```ts
|
||||
const viewedTask = viewingAgentTaskId ? tasks[viewingAgentTaskId] : undefined
|
||||
const viewedAgentTask = ... (isLocalAgentTask(viewedTask) ? viewedTask : undefined)
|
||||
const displayedMessages = viewedAgentTask ? displayedAgentMessages : messages
|
||||
```
|
||||
|
||||
当 `enterTeammateView(agentId)` 把 `viewingAgentTaskId` 设成某个 local_agent 的 id:
|
||||
|
||||
- `viewedAgentTask` 解析成该 agent
|
||||
- `displayedMessages` 切换到 agent 的 messages
|
||||
- 消息列表、spinner、unseen divider 等一整套组件自动用 agent transcript 重渲染
|
||||
- 主对话流被"暂停"(并非销毁,回到 `main` 时仍在原处)
|
||||
|
||||
`enterTeammateView` 同步负责:设 `retain: true` 阻止 eviction、清 `evictAfter`、触发 disk bootstrap 从 `agent-<id>.jsonl` 加载完整 transcript 到 `task.messages`。
|
||||
|
||||
#### Fork agent prompt 归一化
|
||||
|
||||
`/fork` agent 的 transcript 和普通 subagent 不同:它继承 main agent 的上下文,真实初始消息形态是:
|
||||
|
||||
```text
|
||||
...parent messages
|
||||
assistant([...tool_use])
|
||||
user([tool_result..., text("<fork-boilerplate>...Your directive: <prompt>")])
|
||||
...fork live messages
|
||||
```
|
||||
|
||||
这里的 prompt 文本混在 `[tool_result..., text]` 多 block user message 里。消息渲染管线会优先把这条 user message 当作 tool-result plumbing 来处理,导致 `<fork-boilerplate>` 里的用户 prompt 不稳定可见。为保证切换到 fork agent 时总能看到用户发起的 fork prompt,REPL.tsx 对 fork 视图做一次展示层归一化:
|
||||
|
||||
1. 仅当 `viewedAgentTask.agentType === 'fork'` 时启用,不影响普通 Explore / Task subagent。
|
||||
2. 从原始 messages 中识别包含 `<fork-boilerplate>` 的 carrier message。
|
||||
3. 剥离 carrier message 里的 boilerplate text block,但保留 `tool_result` blocks,避免破坏父 assistant `tool_use` 的承接关系。
|
||||
4. 强制插入一条独立 `createUserMessage({ content: viewedAgentTask.prompt })` 作为可见用户 prompt。
|
||||
5. 插入位置优先为 boilerplate carrier 后;如果 sidechain bootstrap 还没读到 carrier,则插到最后一条 inherited `assistant tool_use` 后面,确保 prompt 接在 main 上下文之后,而不是跑到视图顶部。
|
||||
|
||||
这个归一化只影响 UI 展示用的 `displayedAgentMessages`,不回写 `task.messages`,也不改变发送给模型的 fork transcript。
|
||||
|
||||
### 3.6 生命周期
|
||||
|
||||
完全复用官方既有机制:
|
||||
|
||||
- **运行中**:`isBackgroundTask()` 谓词为真,selector 列出
|
||||
- **完成 / 失败 / 中止**:`completeAgentTask` / `failAgentTask` / `killAsyncAgent` 设 `status` 为 terminal
|
||||
- **回访后退出**:`exitTeammateView` 调 `release(task)`——清 `retain`、清 `messages`、terminal 状态下设 `evictAfter = now + PANEL_GRACE_MS (30s)`
|
||||
- **evictAfter 过期**:`useBackgroundAgentTasks` 过滤时自然剔除,selector 行消失
|
||||
- **手动清除**:`stopOrDismissAgent(taskId)` 设 `evictAfter = 0`,立即消失
|
||||
|
||||
## 四、设计决策
|
||||
|
||||
1. **数据源单一**:`useBackgroundAgentTasks` 是唯一过滤点,PromptInput 也复用,避免过滤条件散落
|
||||
2. **pill 聚焦保留**:Enter 切视图后不松焦,让 ↑↓ 连续导航,贴近官方体验
|
||||
3. **`bg_agent` 放 footerItems[0]**:确保 ↓ 溢出直接进入 selector 而非其它 pill
|
||||
4. **selector 不订阅键盘**:所有按键路由集中在 PromptInput 的 `footer:*` 分支,避免 selector 组件和 PromptInput 双重 `useInput` 的冲突
|
||||
5. **`selectedBgAgentIndex` 存 AppState 而非局部 state**:selector 和 PromptInput 分别在两棵不同子树,需要全局字段协调;该值不能从 `viewingAgentTaskId` 派生
|
||||
6. **与 `BackgroundTasksDialog` 共存**:Shift+↓ 行为完全不变,selector 是补充快捷入口;Dialog 仍管 shell / workflow / monitor_mcp 等 selector 不显示的 task 类型
|
||||
7. **fork prompt 展示层兜底**:fork prompt 不依赖 boilerplate 自身渲染,统一在 `displayedAgentMessages` 中合成独立用户消息;普通 subagent 不走该分支,避免 prompt 重复
|
||||
|
||||
## 五、关键 API 复用
|
||||
|
||||
| 官方已有能力 | selector 如何使用 |
|
||||
|---|---|
|
||||
| `AppState.tasks` | 单一数据源,无需 file watcher / output JSONL 订阅 |
|
||||
| `registerAsyncAgent` | `/fork` 和 AgentTool 共用,selector 不区分来源 |
|
||||
| `enterTeammateView(id)` | Enter 时调用,负责 retain + disk bootstrap |
|
||||
| `exitTeammateView` | Enter 选中 `main` 时调用 |
|
||||
| `release(task)` + `PANEL_GRACE_MS` | 30s keep-alive,selector 自动生效 |
|
||||
| `useElapsedTime` | 每行时长显示,非 running 自动停 interval |
|
||||
| `formatTokens` (`utils/format.ts`) | token 数 1k 缩写 |
|
||||
| `footer:up` / `footer:down` / `footer:openSelected` keybinding | 键盘路由复用 Footer context |
|
||||
|
||||
## 六、文件索引
|
||||
|
||||
| 文件 | 职责 |
|
||||
|------|------|
|
||||
| `src/hooks/useBackgroundAgentTasks.ts` | 数据过滤 hook(backgrounded local_agent + evictAfter 过滤 + startTime 排序) |
|
||||
| `src/components/tasks/BackgroundAgentSelector.tsx` | 底部 selector UI,纯展示 |
|
||||
| `src/components/PromptInput/PromptInput.tsx` | 新增 `'bg_agent'` footer pill + 对应的 `footer:up/down/openSelected` 分支 |
|
||||
| `src/state/AppStateStore.ts` | `FooterItem` 加 `'bg_agent'`;新增 `selectedBgAgentIndex` 字段 |
|
||||
| `src/main.tsx` | `getDefaultAppState` 同步初始化 `selectedBgAgentIndex: -1` |
|
||||
| `src/screens/REPL.tsx` | 在 PromptInput + SessionBackgroundHint 之后挂载 `<BackgroundAgentSelector />`;切换 agent 主视图;对 fork transcript 做 prompt 归一化 |
|
||||
| `src/components/messages/AssistantToolUseMessage.tsx` | 新增 `defaultCollapsed?: boolean` prop,为后续详情视图默认折叠工具块预留 |
|
||||
| `src/components/messages/UserTextMessage.tsx` | 识别 `<fork-boilerplate>`,交给 fork 专用 renderer 处理 |
|
||||
| `src/components/messages/UserForkBoilerplateMessage.tsx` | 将 fork boilerplate text 折叠为纯用户 prompt;作为 transcript 中原位渲染的兼容路径 |
|
||||
|
||||
## 七、已知限制
|
||||
|
||||
- `Date.now()` 在 `useBackgroundAgentTasks` 的 useMemo 里冻结于 `[tasks]` 触发时:若长时间没有新 task 变更事件,某个 terminal agent 的 grace 期过期后不会立即从 selector 消失,要等下一次 tasks 变化才刷新。在典型使用(主对话一直在产生消息)下感知不到,暂不额外加 interval。
|
||||
- Selector 当前不处理 Shell Task / Workflow / Monitor MCP 等类型——这些仍走 `BackgroundTasksDialog`(Shift+↓)管理。
|
||||
- `AssistantToolUseMessage` 的 `defaultCollapsed` prop 目前无调用方传值,保留作为后续"agent 详情视图内工具块默认折叠"扩展点。
|
||||
@@ -43,8 +43,12 @@ export async function call(
|
||||
// Omitting subagent_type triggers implicit fork.
|
||||
const input = {
|
||||
prompt: directive,
|
||||
fork: true, // 触发 AgentTool 的 fork 路径:继承父会话上下文 + system prompt + 模型
|
||||
run_in_background: true, // fork always runs async
|
||||
description: `Fork: ${directive.slice(0, 30)}${directive.length > 30 ? '...' : ''}`,
|
||||
// description 只显示在底部 selector / BackgroundTasksDialog,保持简短标签
|
||||
// 即可;用户输入的 prompt 会作为第一条用户消息呈现在主视图里,这里不要
|
||||
// 重复显示。
|
||||
description: 'forked from main',
|
||||
};
|
||||
|
||||
// Call AgentTool with proper parameters:
|
||||
|
||||
@@ -26,6 +26,7 @@ import { useSetPromptOverlayDialog } from '../../context/promptOverlayContext.js
|
||||
import { formatImageRef, formatPastedTextRef, getPastedTextRefNumLines, parseReferences } from '../../history.js';
|
||||
import type { VerificationStatus } from '../../hooks/useApiKeyVerification.js';
|
||||
import { type HistoryMode, useArrowKeyHistory } from '../../hooks/useArrowKeyHistory.js';
|
||||
import { useBackgroundAgentTasks } from '../../hooks/useBackgroundAgentTasks.js';
|
||||
import { useDoublePress } from '../../hooks/useDoublePress.js';
|
||||
import { useHistorySearch } from '../../hooks/useHistorySearch.js';
|
||||
import type { IDESelection } from '../../hooks/useIdeSelection.js';
|
||||
@@ -415,6 +416,16 @@ function PromptInput({
|
||||
// First ↓ selects the pill, second ↓ moves to row 0. Prevents double-select
|
||||
// of pill + row when both bg tasks (pill) and forked agents (rows) are visible.
|
||||
const coordinatorTaskIndex = useAppState(s => s.coordinatorTaskIndex);
|
||||
const selectedBgAgentIndex = useAppState(s => s.selectedBgAgentIndex);
|
||||
const setSelectedBgAgentIndex = useCallback(
|
||||
(v: number | ((prev: number) => number)) =>
|
||||
setAppState(prev => {
|
||||
const next = typeof v === 'function' ? v(prev.selectedBgAgentIndex) : v;
|
||||
if (next === prev.selectedBgAgentIndex) return prev;
|
||||
return { ...prev, selectedBgAgentIndex: next };
|
||||
}),
|
||||
[setAppState],
|
||||
);
|
||||
const setCoordinatorTaskIndex = useCallback(
|
||||
(v: number | ((prev: number) => number)) =>
|
||||
setAppState(prev => {
|
||||
@@ -501,10 +512,13 @@ function PromptInput({
|
||||
(runningTaskCount > 0 || (process.env.USER_TYPE === 'ant' && coordinatorTaskCount > 0)) &&
|
||||
!shouldHideTasksFooter(tasks, showSpinnerTree);
|
||||
const teamsFooterVisible = cachedTeams.length > 0;
|
||||
const bgAgentList = useBackgroundAgentTasks();
|
||||
const bgAgentFooterVisible = bgAgentList.length > 0;
|
||||
|
||||
const footerItems = useMemo(
|
||||
() =>
|
||||
[
|
||||
bgAgentFooterVisible && 'bg_agent',
|
||||
tasksFooterVisible && 'tasks',
|
||||
tmuxFooterVisible && 'tmux',
|
||||
bagelFooterVisible && 'bagel',
|
||||
@@ -513,6 +527,7 @@ function PromptInput({
|
||||
companionFooterVisible && 'companion',
|
||||
].filter(Boolean) as FooterItem[],
|
||||
[
|
||||
bgAgentFooterVisible,
|
||||
tasksFooterVisible,
|
||||
tmuxFooterVisible,
|
||||
bagelFooterVisible,
|
||||
@@ -540,6 +555,7 @@ function PromptInput({
|
||||
const _bagelSelected = footerItemSelected === 'bagel';
|
||||
const teamsSelected = footerItemSelected === 'teams';
|
||||
const bridgeSelected = footerItemSelected === 'bridge';
|
||||
const bgAgentSelected = footerItemSelected === 'bg_agent';
|
||||
|
||||
function selectFooterItem(item: FooterItem | null): void {
|
||||
setAppState(prev => (prev.footerSelection === item ? prev : { ...prev, footerSelection: item }));
|
||||
@@ -547,6 +563,9 @@ function PromptInput({
|
||||
setTeammateFooterIndex(0);
|
||||
setCoordinatorTaskIndex(minCoordinatorIndex);
|
||||
}
|
||||
if (item === 'bg_agent') {
|
||||
setSelectedBgAgentIndex(-1);
|
||||
}
|
||||
}
|
||||
|
||||
// delta: +1 = down/right, -1 = up/left. Returns true if nav happened
|
||||
@@ -1808,6 +1827,15 @@ function PromptInput({
|
||||
useKeybindings(
|
||||
{
|
||||
'footer:up': () => {
|
||||
// ↑ in bg_agent pill: move selection up (-1 = main). At -1, leave pill.
|
||||
if (bgAgentSelected) {
|
||||
if (selectedBgAgentIndex > -1) {
|
||||
setSelectedBgAgentIndex(prev => prev - 1);
|
||||
} else {
|
||||
selectFooterItem(null);
|
||||
}
|
||||
return;
|
||||
}
|
||||
// ↑ scrolls within the coordinator task list before leaving the pill
|
||||
if (
|
||||
tasksSelected &&
|
||||
@@ -1821,6 +1849,13 @@ function PromptInput({
|
||||
navigateFooter(-1, true);
|
||||
},
|
||||
'footer:down': () => {
|
||||
// ↓ in bg_agent pill: move selection down through agents. Clamp at last.
|
||||
if (bgAgentSelected) {
|
||||
if (selectedBgAgentIndex < bgAgentList.length - 1) {
|
||||
setSelectedBgAgentIndex(prev => prev + 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
// ↓ scrolls within the coordinator task list, never leaves the pill
|
||||
if (tasksSelected && process.env.USER_TYPE === 'ant' && coordinatorTaskCount > 0) {
|
||||
if (coordinatorTaskIndex < coordinatorTaskCount - 1) {
|
||||
@@ -1906,6 +1941,15 @@ function PromptInput({
|
||||
setShowBridgeDialog(true);
|
||||
selectFooterItem(null);
|
||||
break;
|
||||
case 'bg_agent':
|
||||
if (selectedBgAgentIndex === -1) {
|
||||
exitTeammateView(setAppState);
|
||||
} else {
|
||||
const picked = bgAgentList[selectedBgAgentIndex];
|
||||
if (picked) enterTeammateView(picked.agentId, setAppState);
|
||||
}
|
||||
// Keep the pill focused so ↑/↓ continue to work after Enter.
|
||||
break;
|
||||
}
|
||||
},
|
||||
'footer:clearSelection': () => {
|
||||
|
||||
@@ -30,6 +30,7 @@ type Props = {
|
||||
inProgressToolCallCount?: number;
|
||||
lookups: ReturnType<typeof buildMessageLookups>;
|
||||
isTranscriptMode?: boolean;
|
||||
defaultCollapsed?: boolean;
|
||||
};
|
||||
|
||||
export function AssistantToolUseMessage({
|
||||
@@ -45,6 +46,7 @@ export function AssistantToolUseMessage({
|
||||
inProgressToolCallCount,
|
||||
lookups,
|
||||
isTranscriptMode,
|
||||
defaultCollapsed,
|
||||
}: Props): React.ReactNode {
|
||||
const terminalSize = useTerminalSize();
|
||||
const [theme] = useTheme();
|
||||
@@ -167,6 +169,7 @@ export function AssistantToolUseMessage({
|
||||
</Box>
|
||||
{!isResolved &&
|
||||
!isQueued &&
|
||||
!defaultCollapsed &&
|
||||
(isClassifierChecking ? (
|
||||
<MessageResponse height={1}>
|
||||
<Text dimColor>
|
||||
|
||||
@@ -3,28 +3,31 @@
|
||||
*/
|
||||
import type { TextBlockParam } from '@anthropic-ai/sdk/resources/index.mjs';
|
||||
import * as React from 'react';
|
||||
import { Box, Text } from '@anthropic/ink';
|
||||
import { FORK_BOILERPLATE_TAG, FORK_DIRECTIVE_PREFIX } from '../../constants/xml.js';
|
||||
import { extractTag } from '../../utils/messages.js';
|
||||
import { UserPromptMessage } from './UserPromptMessage.js';
|
||||
|
||||
type Props = {
|
||||
addMargin: boolean;
|
||||
param: TextBlockParam;
|
||||
isTranscriptMode?: boolean;
|
||||
timestamp?: string;
|
||||
};
|
||||
|
||||
export function UserForkBoilerplateMessage({ param, addMargin }: Props): React.ReactNode {
|
||||
const text = param.text;
|
||||
const extracted = extractTag(text, 'fork-boilerplate');
|
||||
if (!extracted) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const firstLine = extracted.trim().split('\n')[0] ?? '';
|
||||
const preview = firstLine.length > 80 ? firstLine.slice(0, 77) + '...' : firstLine;
|
||||
export function UserForkBoilerplateMessage({ param, addMargin, isTranscriptMode, timestamp }: Props): React.ReactNode {
|
||||
if (!extractTag(param.text, FORK_BOILERPLATE_TAG)) return null;
|
||||
const closeTag = `</${FORK_BOILERPLATE_TAG}>`;
|
||||
const afterTag = param.text.slice(param.text.indexOf(closeTag) + closeTag.length).trimStart();
|
||||
const userPrompt = afterTag.startsWith(FORK_DIRECTIVE_PREFIX)
|
||||
? afterTag.slice(FORK_DIRECTIVE_PREFIX.length)
|
||||
: afterTag;
|
||||
|
||||
return (
|
||||
<Box flexDirection="row" marginTop={addMargin ? 1 : 0}>
|
||||
<Text dimColor>[fork] </Text>
|
||||
<Text>{preview}</Text>
|
||||
</Box>
|
||||
<UserPromptMessage
|
||||
addMargin={addMargin}
|
||||
param={{ type: 'text', text: userPrompt }}
|
||||
isTranscriptMode={isTranscriptMode}
|
||||
timestamp={timestamp}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import * as React from 'react';
|
||||
import { NO_CONTENT_MESSAGE } from '../../constants/messages.js';
|
||||
import {
|
||||
COMMAND_MESSAGE_TAG,
|
||||
FORK_BOILERPLATE_TAG,
|
||||
LOCAL_COMMAND_CAVEAT_TAG,
|
||||
TASK_NOTIFICATION_TAG,
|
||||
TEAMMATE_MESSAGE_TAG,
|
||||
@@ -124,16 +125,21 @@ export function UserTextMessage({
|
||||
}
|
||||
|
||||
// Fork child's first message: collapse the rules/format boilerplate, show
|
||||
// only the directive. FORK_BOILERPLATE_TAG is inlined so the import doesn't
|
||||
// ship in external builds where feature('FORK_SUBAGENT') is false.
|
||||
if (feature('FORK_SUBAGENT')) {
|
||||
if (param.text.includes('<fork-boilerplate>')) {
|
||||
/* eslint-disable @typescript-eslint/no-require-imports */
|
||||
const { UserForkBoilerplateMessage } =
|
||||
require('./UserForkBoilerplateMessage.js') as typeof import('./UserForkBoilerplateMessage.js');
|
||||
/* eslint-enable @typescript-eslint/no-require-imports */
|
||||
return <UserForkBoilerplateMessage addMargin={addMargin} param={param} />;
|
||||
}
|
||||
// only the user prompt. Independent of FORK_SUBAGENT flag — the fork agent
|
||||
// transcript always needs to render the prompt as a normal user bubble.
|
||||
if (param.text.includes(`<${FORK_BOILERPLATE_TAG}>`)) {
|
||||
/* eslint-disable @typescript-eslint/no-require-imports */
|
||||
const { UserForkBoilerplateMessage } =
|
||||
require('./UserForkBoilerplateMessage.js') as typeof import('./UserForkBoilerplateMessage.js');
|
||||
/* eslint-enable @typescript-eslint/no-require-imports */
|
||||
return (
|
||||
<UserForkBoilerplateMessage
|
||||
addMargin={addMargin}
|
||||
param={param}
|
||||
isTranscriptMode={isTranscriptMode}
|
||||
timestamp={timestamp}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// Cross-session UDS message (from another Claude session's SendMessage).
|
||||
|
||||
63
src/components/tasks/BackgroundAgentSelector.tsx
Normal file
63
src/components/tasks/BackgroundAgentSelector.tsx
Normal file
@@ -0,0 +1,63 @@
|
||||
import { Box, Text } from '@anthropic/ink';
|
||||
import { useBackgroundAgentTasks } from '../../hooks/useBackgroundAgentTasks.js';
|
||||
import { useElapsedTime } from '../../hooks/useElapsedTime.js';
|
||||
import { useAppState } from '../../state/AppState.js';
|
||||
import type { LocalAgentTaskState } from '../../tasks/LocalAgentTask/LocalAgentTask.js';
|
||||
import { formatTokens } from '../../utils/format.js';
|
||||
|
||||
function AgentRow({ task, selected }: { task: LocalAgentTaskState; selected: boolean }) {
|
||||
const elapsed = useElapsedTime(task.startTime, task.status === 'running');
|
||||
const tokens = task.progress?.tokenCount ?? 0;
|
||||
const isRunning = task.status === 'running';
|
||||
return (
|
||||
<Box flexDirection="row" width="100%" justifyContent="space-between">
|
||||
<Box flexDirection="row" flexShrink={1}>
|
||||
<Text color={isRunning ? 'success' : undefined}>{selected ? '● ' : '○ '}</Text>
|
||||
<Text bold={selected} wrap="truncate-end">
|
||||
{task.agentType} <Text dimColor>{task.description}</Text>
|
||||
</Text>
|
||||
</Box>
|
||||
<Box flexShrink={0}>
|
||||
<Text dimColor>
|
||||
{elapsed} · ↓ {formatTokens(tokens)} tokens
|
||||
</Text>
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
function getHint(pillFocused: boolean, viewedTask: LocalAgentTaskState | null): string {
|
||||
if (pillFocused) return '↑/↓ to select · Enter to view';
|
||||
if (!viewedTask) return 'shift+↓ to manage background agents';
|
||||
return viewedTask.status === 'running' ? 'shift+↓ to manage · x to stop' : 'shift+↓ to manage · x to clear';
|
||||
}
|
||||
|
||||
export function BackgroundAgentSelector(): React.ReactNode {
|
||||
const tasks = useBackgroundAgentTasks();
|
||||
const viewingId = useAppState(s => s.viewingAgentTaskId);
|
||||
const footerSelection = useAppState(s => s.footerSelection);
|
||||
const selectedBgIndex = useAppState(s => s.selectedBgAgentIndex);
|
||||
|
||||
if (tasks.length === 0) return null;
|
||||
|
||||
const pillFocused = footerSelection === 'bg_agent';
|
||||
const highlightedId = pillFocused
|
||||
? selectedBgIndex === -1
|
||||
? null
|
||||
: (tasks[selectedBgIndex]?.agentId ?? null)
|
||||
: (viewingId ?? null);
|
||||
const mainHighlighted = pillFocused ? selectedBgIndex === -1 : viewingId === undefined;
|
||||
const viewedTask = viewingId ? (tasks.find(t => t.agentId === viewingId) ?? null) : null;
|
||||
|
||||
return (
|
||||
<Box flexDirection="column" width="100%">
|
||||
<Box flexDirection="row" width="100%" justifyContent="space-between">
|
||||
<Text bold={mainHighlighted}>{mainHighlighted ? '● ' : '○ '}main</Text>
|
||||
<Text dimColor>{getHint(pillFocused, viewedTask)}</Text>
|
||||
</Box>
|
||||
{tasks.map(task => (
|
||||
<AgentRow key={task.agentId} task={task} selected={task.agentId === highlightedId} />
|
||||
))}
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
19
src/hooks/useBackgroundAgentTasks.ts
Normal file
19
src/hooks/useBackgroundAgentTasks.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { useMemo } from 'react'
|
||||
import { useAppState } from '../state/AppState.js'
|
||||
import {
|
||||
isLocalAgentTask,
|
||||
type LocalAgentTaskState,
|
||||
} from '../tasks/LocalAgentTask/LocalAgentTask.js'
|
||||
|
||||
export function useBackgroundAgentTasks(): LocalAgentTaskState[] {
|
||||
const tasks = useAppState(s => s.tasks)
|
||||
return useMemo(() => {
|
||||
const now = Date.now()
|
||||
return Object.values(tasks)
|
||||
.filter(isLocalAgentTask)
|
||||
.filter(t => t.agentType !== 'main-session')
|
||||
.filter(t => t.isBackgrounded !== false)
|
||||
.filter(t => t.evictAfter === undefined || t.evictAfter > now)
|
||||
.sort((a, b) => a.startTime - b.startTime)
|
||||
}, [tasks])
|
||||
}
|
||||
@@ -3485,6 +3485,7 @@ async function run(): Promise<CommanderCommand> {
|
||||
: 'none',
|
||||
showTeammateMessagePreview: isAgentSwarmsEnabled() ? false : undefined,
|
||||
selectedIPAgentIndex: -1,
|
||||
selectedBgAgentIndex: -1,
|
||||
coordinatorTaskIndex: -1,
|
||||
viewSelectionMode: 'none',
|
||||
footerSelection: null,
|
||||
|
||||
@@ -244,7 +244,14 @@ import {
|
||||
formatCommandInputTags,
|
||||
} from '../utils/messages.js';
|
||||
import { generateSessionTitle } from '../utils/sessionTitle.js';
|
||||
import { BASH_INPUT_TAG, COMMAND_MESSAGE_TAG, COMMAND_NAME_TAG, LOCAL_COMMAND_STDOUT_TAG } from '../constants/xml.js';
|
||||
import {
|
||||
BASH_INPUT_TAG,
|
||||
COMMAND_MESSAGE_TAG,
|
||||
COMMAND_NAME_TAG,
|
||||
FORK_BOILERPLATE_TAG,
|
||||
LOCAL_COMMAND_STDOUT_TAG,
|
||||
} from '../constants/xml.js';
|
||||
import { FORK_SUBAGENT_TYPE } from '@claude-code-best/builtin-tools/tools/AgentTool/forkSubagent.js';
|
||||
import { escapeXml } from '../utils/xml.js';
|
||||
import type { ThinkingConfig } from '../utils/thinking.js';
|
||||
import { gracefulShutdownSync } from '../utils/gracefulShutdown.js';
|
||||
@@ -336,6 +343,7 @@ import {
|
||||
import { isBgSession, updateSessionName, updateSessionActivity } from '../utils/concurrentSessions.js';
|
||||
import { isInProcessTeammateTask, type InProcessTeammateTaskState } from '../tasks/InProcessTeammateTask/types.js';
|
||||
import { restoreRemoteAgentTasks } from '../tasks/RemoteAgentTask/RemoteAgentTask.js';
|
||||
import { BackgroundAgentSelector } from '../components/tasks/BackgroundAgentSelector.js';
|
||||
import { useInboxPoller } from '../hooks/useInboxPoller.js';
|
||||
// Dead code elimination: conditional import for loop mode
|
||||
/* eslint-disable @typescript-eslint/no-require-imports */
|
||||
@@ -800,6 +808,21 @@ export type Props = {
|
||||
|
||||
export type Screen = 'prompt' | 'transcript';
|
||||
|
||||
// Boilerplate carrier lives in a mixed user message ([tool_result..., text])
|
||||
// that AgentTool/forkSubagent.buildForkedMessages emits as the fork child's
|
||||
// first user turn. The text block wraps <FORK_BOILERPLATE_TAG>...</..> + the
|
||||
// user prompt; tool_result siblings keep the parent's tool calls closed.
|
||||
const FORK_BOILERPLATE_OPEN_TAG = `<${FORK_BOILERPLATE_TAG}>`;
|
||||
|
||||
function isForkBoilerplateTextBlock(block: { type: string; text?: string }): boolean {
|
||||
return block.type === 'text' && typeof block.text === 'string' && block.text.includes(FORK_BOILERPLATE_OPEN_TAG);
|
||||
}
|
||||
|
||||
function isForkBoilerplateMessage(message: MessageType): boolean {
|
||||
if (message.type !== 'user' || !Array.isArray(message.message?.content)) return false;
|
||||
return message.message.content.some(isForkBoilerplateTextBlock);
|
||||
}
|
||||
|
||||
export function REPL({
|
||||
commands: initialCommands,
|
||||
debug,
|
||||
@@ -5548,8 +5571,72 @@ export function REPL({
|
||||
const usesSyncMessages = showStreamingText || !isLoading;
|
||||
// When viewing an agent, never fall through to leader — empty until
|
||||
// bootstrap/stream fills. Closes the see-leader-type-agent footgun.
|
||||
const rawAgentMessages = viewedAgentTask?.messages;
|
||||
// Fork sidechain encodes the user prompt inside a mixed user message alongside
|
||||
// tool_result blocks; surface the prompt as a standalone bubble and strip the
|
||||
// boilerplate text from its original carrier while preserving tool_results.
|
||||
const displayedAgentMessages = useMemo(() => {
|
||||
if (!viewedAgentTask) return undefined;
|
||||
const agentMessages = rawAgentMessages ?? [];
|
||||
if (
|
||||
!isLocalAgentTask(viewedAgentTask) ||
|
||||
viewedAgentTask.agentType !== FORK_SUBAGENT_TYPE ||
|
||||
!viewedAgentTask.prompt
|
||||
) {
|
||||
return agentMessages;
|
||||
}
|
||||
// Single pass: locate boilerplate carrier, check whether the prompt text is
|
||||
// already present elsewhere, and find the fallback insertion point (after
|
||||
// the last parent assistant tool_use).
|
||||
const trimmedPrompt = viewedAgentTask.prompt.trim();
|
||||
let boilerplateIndex = -1;
|
||||
let lastAssistantToolUseIndex = -1;
|
||||
let promptAlreadyRendered = false;
|
||||
for (let i = 0; i < agentMessages.length; i++) {
|
||||
const m = agentMessages[i]!;
|
||||
if (m.type === 'user' && Array.isArray(m.message?.content)) {
|
||||
const hasBoilerplate = m.message.content.some(isForkBoilerplateTextBlock);
|
||||
if (hasBoilerplate) {
|
||||
boilerplateIndex = i;
|
||||
} else if (!promptAlreadyRendered) {
|
||||
const firstText = m.message.content.find(b => b.type === 'text' && typeof b.text === 'string') as
|
||||
| { type: 'text'; text: string }
|
||||
| undefined;
|
||||
if (firstText && firstText.text.trim() === trimmedPrompt) promptAlreadyRendered = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (m.type === 'assistant' && Array.isArray(m.message?.content)) {
|
||||
if (m.message.content.some(b => b.type === 'tool_use')) lastAssistantToolUseIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
const stripped =
|
||||
boilerplateIndex === -1
|
||||
? agentMessages
|
||||
: agentMessages.map((m, i) => {
|
||||
if (i !== boilerplateIndex) return m;
|
||||
if (!Array.isArray(m.message?.content)) return m;
|
||||
return {
|
||||
...m,
|
||||
message: {
|
||||
...m.message,
|
||||
content: m.message.content.filter(b => !isForkBoilerplateTextBlock(b)),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
if (promptAlreadyRendered) return stripped;
|
||||
|
||||
const insertAt = boilerplateIndex !== -1 ? boilerplateIndex + 1 : lastAssistantToolUseIndex + 1;
|
||||
const synthetic = createUserMessage({
|
||||
content: viewedAgentTask.prompt,
|
||||
timestamp: new Date(viewedAgentTask.startTime).toISOString(),
|
||||
});
|
||||
return [...stripped.slice(0, insertAt), synthetic, ...stripped.slice(insertAt)];
|
||||
}, [viewedAgentTask, rawAgentMessages]);
|
||||
const displayedMessages = viewedAgentTask
|
||||
? (viewedAgentTask.messages ?? [])
|
||||
? (displayedAgentMessages ?? [])
|
||||
: usesSyncMessages
|
||||
? messages
|
||||
: deferredMessages;
|
||||
@@ -6286,6 +6373,7 @@ export function REPL({
|
||||
voiceInterimRange={voice.interimRange}
|
||||
/>
|
||||
<SessionBackgroundHint onBackgroundSession={handleBackgroundSession} isLoading={isLoading} />
|
||||
<BackgroundAgentSelector />
|
||||
</>
|
||||
)}
|
||||
{cursor && (
|
||||
|
||||
@@ -509,10 +509,30 @@ export function getAPIMetadata() {
|
||||
}
|
||||
}
|
||||
|
||||
const deviceId = getOrCreateUserID()
|
||||
|
||||
// Third-party API providers (DeepSeek, etc.) validate user_id against
|
||||
// ^[a-zA-Z0-9_-]+$ which rejects JSON strings containing {, ", :, etc.
|
||||
// When using a non-Anthropic base URL, send only the device_id (hex string).
|
||||
const baseUrl = process.env.ANTHROPIC_BASE_URL
|
||||
const isThirdParty =
|
||||
baseUrl &&
|
||||
(() => {
|
||||
try {
|
||||
return new URL(baseUrl).host !== 'api.anthropic.com'
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
})()
|
||||
|
||||
if (isThirdParty) {
|
||||
return { user_id: deviceId }
|
||||
}
|
||||
|
||||
return {
|
||||
user_id: jsonStringify({
|
||||
...extra,
|
||||
device_id: getOrCreateUserID(),
|
||||
device_id: deviceId,
|
||||
// Only include OAuth account UUID when actively using OAuth authentication
|
||||
account_uuid: getOauthAccountInfo()?.accountUuid ?? '',
|
||||
session_id: getSessionId(),
|
||||
|
||||
@@ -74,7 +74,10 @@ import {
|
||||
} from '../../utils/errors.js'
|
||||
import { getMCPUserAgent } from '../../utils/http.js'
|
||||
import { maybeNotifyIDEConnected } from '../../utils/ide.js'
|
||||
import { maybeResizeAndDownsampleImageBuffer } from '../../utils/imageResizer.js'
|
||||
import {
|
||||
type ImageLimits,
|
||||
maybeResizeAndDownsampleImageBuffer,
|
||||
} from '../../utils/imageResizer.js'
|
||||
import { logMCPDebug, logMCPError } from '../../utils/log.js'
|
||||
import {
|
||||
getBinaryBlobSavedMessage,
|
||||
@@ -102,6 +105,7 @@ import {
|
||||
isPersistError,
|
||||
persistToolResult,
|
||||
} from '../../utils/toolResultStorage.js'
|
||||
import { checkStatsigFeatureGate_CACHED_MAY_BE_STALE } from '../analytics/growthbook.js'
|
||||
import {
|
||||
type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
||||
logEvent,
|
||||
@@ -2486,15 +2490,23 @@ export function prefetchAllMcpResources(
|
||||
export async function transformResultContent(
|
||||
resultContent: PromptMessage['content'],
|
||||
serverName: string,
|
||||
limits?: ImageLimits,
|
||||
includeMeta = false,
|
||||
): Promise<Array<ContentBlockParam>> {
|
||||
switch (resultContent.type) {
|
||||
case 'text':
|
||||
return [
|
||||
{
|
||||
type: 'text',
|
||||
text: resultContent.text,
|
||||
},
|
||||
]
|
||||
case 'text': {
|
||||
const block: ContentBlockParam = {
|
||||
type: 'text',
|
||||
text: resultContent.text,
|
||||
}
|
||||
if (includeMeta) {
|
||||
const meta = resultContent._meta
|
||||
if (meta) {
|
||||
;(block as { _meta?: unknown })._meta = meta
|
||||
}
|
||||
}
|
||||
return [block]
|
||||
}
|
||||
case 'audio': {
|
||||
const audioData = resultContent as {
|
||||
type: 'audio'
|
||||
@@ -2516,6 +2528,7 @@ export async function transformResultContent(
|
||||
imageBuffer,
|
||||
imageBuffer.length,
|
||||
ext,
|
||||
limits,
|
||||
)
|
||||
return [
|
||||
{
|
||||
@@ -2551,6 +2564,7 @@ export async function transformResultContent(
|
||||
imageBuffer,
|
||||
imageBuffer.length,
|
||||
ext,
|
||||
limits,
|
||||
)
|
||||
const content: MessageParam['content'] = []
|
||||
if (prefix) {
|
||||
@@ -2671,6 +2685,7 @@ export async function transformMCPResult(
|
||||
result: unknown,
|
||||
tool: string, // Tool name for validation (e.g., "search")
|
||||
name: string, // Server name for transformation (e.g., "slack")
|
||||
limits?: ImageLimits, // Image processing limits, plumbed to transformResultContent
|
||||
): Promise<TransformedMCPResult> {
|
||||
if (result && typeof result === 'object') {
|
||||
if ('toolResult' in result) {
|
||||
@@ -2694,7 +2709,9 @@ export async function transformMCPResult(
|
||||
if ('content' in result && Array.isArray(result.content)) {
|
||||
const transformedContent = (
|
||||
await Promise.all(
|
||||
result.content.map(item => transformResultContent(item, name)),
|
||||
result.content.map(item =>
|
||||
transformResultContent(item, name, limits, true),
|
||||
),
|
||||
)
|
||||
).flat()
|
||||
return {
|
||||
@@ -2729,8 +2746,15 @@ export async function processMCPResult(
|
||||
result: unknown,
|
||||
tool: string, // Tool name for validation (e.g., "search")
|
||||
name: string, // Server name for IDE check and transformation (e.g., "slack")
|
||||
limits?: ImageLimits, // Image processing limits, plumbed to transformMCPResult
|
||||
skipLargeOutput = false, // If true, skip large-output handling for non-image content
|
||||
): Promise<MCPToolResult> {
|
||||
const { content, type, schema } = await transformMCPResult(result, tool, name)
|
||||
const { content, type, schema } = await transformMCPResult(
|
||||
result,
|
||||
tool,
|
||||
name,
|
||||
limits,
|
||||
)
|
||||
|
||||
// IDE tools are not going to the model directly, so we don't need to
|
||||
// handle large output.
|
||||
@@ -2738,6 +2762,12 @@ export async function processMCPResult(
|
||||
return content
|
||||
}
|
||||
|
||||
// Caller opted out of large-output handling (e.g., result already truncated
|
||||
// upstream); only continue if the content has images that may need handling.
|
||||
if (skipLargeOutput && !contentContainsImages(content)) {
|
||||
return content
|
||||
}
|
||||
|
||||
// Check if content needs truncation (i.e., is too large)
|
||||
if (!(await mcpContentNeedsTruncation(content))) {
|
||||
return content
|
||||
@@ -2775,9 +2805,15 @@ export async function processMCPResult(
|
||||
// Generate a unique ID for the persisted file (server__tool-timestamp)
|
||||
const timestamp = Date.now()
|
||||
const persistId = `mcp-${normalizeNameForMCP(name)}-${normalizeNameForMCP(tool)}-${timestamp}`
|
||||
// Convert to string for persistence (persistToolResult expects string or specific block types)
|
||||
// When the large-string format gate is on, unwrap a single bare text block
|
||||
// (no annotations, no _meta) into raw text so the model gets plain text in
|
||||
// the persisted file instead of a JSON-wrapped block. The `_meta` check is
|
||||
// why transformResultContent preserves _meta on text blocks.
|
||||
const unwrappedText = unwrapSingleTextBlock(content)
|
||||
const contentStr =
|
||||
typeof content === 'string' ? content : jsonStringify(content, null, 2)
|
||||
typeof content === 'string'
|
||||
? content
|
||||
: (unwrappedText ?? jsonStringify(content, null, 2))
|
||||
const persistResult = await persistToolResult(contentStr, persistId)
|
||||
|
||||
if (isPersistError(persistResult)) {
|
||||
@@ -2798,7 +2834,10 @@ export async function processMCPResult(
|
||||
persistedSizeChars: persistResult.originalSize,
|
||||
} as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS)
|
||||
|
||||
const formatDescription = getFormatDescription(type, schema)
|
||||
const formatDescription =
|
||||
unwrappedText !== undefined
|
||||
? getFormatDescription('toolResult')
|
||||
: getFormatDescription(type, schema)
|
||||
return getLargeOutputInstructions(
|
||||
persistResult.filepath,
|
||||
persistResult.originalSize,
|
||||
@@ -2806,6 +2845,39 @@ export async function processMCPResult(
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true when the large-string output format is enabled (matches
|
||||
* binary's `sf8()`). When enabled, processMCPResult unwraps a single bare
|
||||
* text block to raw text for persistence instead of JSON-wrapping it.
|
||||
*
|
||||
* Gating sources, in order:
|
||||
* 1. MCP_TRUNCATION_PROMPT_OVERRIDE env var (anything except "legacy" enables)
|
||||
* 2. Statsig gate `tengu_mcp_subagent_prompt`
|
||||
*/
|
||||
function isLargeStringFormatEnabled(): boolean {
|
||||
const override = process.env.MCP_TRUNCATION_PROMPT_OVERRIDE
|
||||
if (override) return override !== 'legacy'
|
||||
return checkStatsigFeatureGate_CACHED_MAY_BE_STALE(
|
||||
'tengu_mcp_subagent_prompt',
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Unwraps a single bare text content block to its raw text when the
|
||||
* large-string format gate is on. Returns undefined when the gate is off,
|
||||
* the content is not an array, the array doesn't contain exactly one text
|
||||
* block, or the block carries annotations or _meta. Matches binary mg5's
|
||||
* `M=...` computation.
|
||||
*/
|
||||
function unwrapSingleTextBlock(content: MCPToolResult): string | undefined {
|
||||
if (!isLargeStringFormatEnabled()) return undefined
|
||||
if (!Array.isArray(content) || content.length !== 1) return undefined
|
||||
const block = content[0]
|
||||
if (!block || block.type !== 'text') return undefined
|
||||
if ('annotations' in block || '_meta' in block) return undefined
|
||||
return block.text
|
||||
}
|
||||
|
||||
/**
|
||||
* Call an MCP tool, handling UrlElicitationRequiredError (-32042) by
|
||||
* displaying the URL elicitation to the user, waiting for the completion
|
||||
@@ -2827,6 +2899,8 @@ export async function callMCPToolWithUrlElicitationRetry({
|
||||
signal,
|
||||
setAppState,
|
||||
onProgress,
|
||||
imageLimits,
|
||||
hasResultSizeAnnotation = false,
|
||||
callToolFn = callMCPTool,
|
||||
handleElicitation,
|
||||
}: {
|
||||
@@ -2838,6 +2912,8 @@ export async function callMCPToolWithUrlElicitationRetry({
|
||||
signal: AbortSignal
|
||||
setAppState: (f: (prev: AppState) => AppState) => void
|
||||
onProgress?: (data: MCPProgress) => void
|
||||
imageLimits?: ImageLimits
|
||||
hasResultSizeAnnotation?: boolean
|
||||
/** Injectable for testing. Defaults to callMCPTool. */
|
||||
callToolFn?: (opts: {
|
||||
client: ConnectedMCPServer
|
||||
@@ -2846,6 +2922,8 @@ export async function callMCPToolWithUrlElicitationRetry({
|
||||
meta?: Record<string, unknown>
|
||||
signal: AbortSignal
|
||||
onProgress?: (data: MCPProgress) => void
|
||||
imageLimits?: ImageLimits
|
||||
hasResultSizeAnnotation?: boolean
|
||||
}) => Promise<MCPToolCallResult>
|
||||
/** Handler for URL elicitations when no hook handles them.
|
||||
* In print/SDK mode, delegates to structuredIO. In REPL, falls back to queue. */
|
||||
@@ -2865,6 +2943,8 @@ export async function callMCPToolWithUrlElicitationRetry({
|
||||
meta,
|
||||
signal,
|
||||
onProgress,
|
||||
imageLimits,
|
||||
hasResultSizeAnnotation,
|
||||
})
|
||||
} catch (error) {
|
||||
// The MCP SDK's Protocol creates plain McpError (not UrlElicitationRequiredError)
|
||||
@@ -3041,6 +3121,8 @@ async function callMCPTool({
|
||||
meta,
|
||||
signal,
|
||||
onProgress,
|
||||
imageLimits,
|
||||
hasResultSizeAnnotation = false,
|
||||
}: {
|
||||
client: ConnectedMCPServer
|
||||
tool: string
|
||||
@@ -3048,6 +3130,8 @@ async function callMCPTool({
|
||||
meta?: Record<string, unknown>
|
||||
signal: AbortSignal
|
||||
onProgress?: (data: MCPProgress) => void
|
||||
imageLimits?: ImageLimits
|
||||
hasResultSizeAnnotation?: boolean
|
||||
}): Promise<{
|
||||
content: MCPToolResult
|
||||
_meta?: Record<string, unknown>
|
||||
@@ -3176,7 +3260,13 @@ async function callMCPTool({
|
||||
})
|
||||
}
|
||||
|
||||
const content = await processMCPResult(result, tool, name)
|
||||
const content = await processMCPResult(
|
||||
result,
|
||||
tool,
|
||||
name,
|
||||
imageLimits,
|
||||
hasResultSizeAnnotation,
|
||||
)
|
||||
return {
|
||||
content,
|
||||
_meta: result._meta as Record<string, unknown> | undefined,
|
||||
|
||||
@@ -85,6 +85,7 @@ export type FooterItem =
|
||||
| 'teams'
|
||||
| 'bridge'
|
||||
| 'companion'
|
||||
| 'bg_agent'
|
||||
|
||||
export type AppState = DeepImmutable<{
|
||||
settings: SettingsJson
|
||||
@@ -97,6 +98,9 @@ export type AppState = DeepImmutable<{
|
||||
// Optional - only present when ENABLE_AGENT_SWARMS is true (for dead code elimination)
|
||||
showTeammateMessagePreview?: boolean
|
||||
selectedIPAgentIndex: number
|
||||
// Selection index for the bottom BackgroundAgentSelector.
|
||||
// -1 = main, 0..N-1 = index into useBackgroundAgentTasks().
|
||||
selectedBgAgentIndex: number
|
||||
// CoordinatorTaskPanel selection: -1 = pill, 0 = main, 1..N = agent rows.
|
||||
// AppState (not local) so the panel can read it directly without prop-drilling
|
||||
// through PromptInput → PromptInputFooter.
|
||||
@@ -477,6 +481,7 @@ export function getDefaultAppState(): AppState {
|
||||
isBriefOnly: false,
|
||||
showTeammateMessagePreview: false,
|
||||
selectedIPAgentIndex: -1,
|
||||
selectedBgAgentIndex: -1,
|
||||
coordinatorTaskIndex: -1,
|
||||
viewSelectionMode: 'none',
|
||||
footerSelection: null,
|
||||
|
||||
@@ -162,6 +162,16 @@ interface CompressedImageResult {
|
||||
originalSize: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Per-call image processing limits, overriding the module-level defaults.
|
||||
*/
|
||||
export interface ImageLimits {
|
||||
targetRawSize: number
|
||||
maxWidth: number
|
||||
maxHeight: number
|
||||
maxBase64Size: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracted from FileReadTool's readImage function
|
||||
* Resizes image buffer to meet size and dimension constraints
|
||||
@@ -170,7 +180,13 @@ export async function maybeResizeAndDownsampleImageBuffer(
|
||||
imageBuffer: Buffer,
|
||||
originalSize: number,
|
||||
ext: string,
|
||||
limits?: ImageLimits,
|
||||
): Promise<ResizeResult> {
|
||||
const targetRawSize = limits?.targetRawSize ?? IMAGE_TARGET_RAW_SIZE
|
||||
const maxWidth = limits?.maxWidth ?? IMAGE_MAX_WIDTH
|
||||
const maxHeight = limits?.maxHeight ?? IMAGE_MAX_HEIGHT
|
||||
const maxBase64Size = limits?.maxBase64Size ?? API_IMAGE_MAX_BASE64_SIZE
|
||||
|
||||
if (imageBuffer.length === 0) {
|
||||
// Empty buffer would fall through the catch block below (sharp throws
|
||||
// "Unable to determine image format"), and the fallback's size check
|
||||
@@ -189,7 +205,7 @@ export async function maybeResizeAndDownsampleImageBuffer(
|
||||
|
||||
// If dimensions aren't available from metadata
|
||||
if (!metadata.width || !metadata.height) {
|
||||
if (originalSize > IMAGE_TARGET_RAW_SIZE) {
|
||||
if (originalSize > targetRawSize) {
|
||||
// Create fresh sharp instance for compression
|
||||
const compressedBuffer = await sharp(imageBuffer)
|
||||
.jpeg({ quality: 80 })
|
||||
@@ -210,9 +226,9 @@ export async function maybeResizeAndDownsampleImageBuffer(
|
||||
|
||||
// Check if the original file just works
|
||||
if (
|
||||
originalSize <= IMAGE_TARGET_RAW_SIZE &&
|
||||
width <= IMAGE_MAX_WIDTH &&
|
||||
height <= IMAGE_MAX_HEIGHT
|
||||
originalSize <= targetRawSize &&
|
||||
width <= maxWidth &&
|
||||
height <= maxHeight
|
||||
) {
|
||||
return {
|
||||
buffer: imageBuffer,
|
||||
@@ -226,20 +242,19 @@ export async function maybeResizeAndDownsampleImageBuffer(
|
||||
}
|
||||
}
|
||||
|
||||
const needsDimensionResize =
|
||||
width > IMAGE_MAX_WIDTH || height > IMAGE_MAX_HEIGHT
|
||||
const needsDimensionResize = width > maxWidth || height > maxHeight
|
||||
const isPng = normalizedMediaType === 'png'
|
||||
|
||||
// If dimensions are within limits but file is too large, try compression first
|
||||
// This preserves full resolution when possible
|
||||
if (!needsDimensionResize && originalSize > IMAGE_TARGET_RAW_SIZE) {
|
||||
if (!needsDimensionResize && originalSize > targetRawSize) {
|
||||
// For PNGs, try PNG compression first to preserve transparency
|
||||
if (isPng) {
|
||||
// Create fresh sharp instance for each compression attempt
|
||||
const pngCompressed = await sharp(imageBuffer)
|
||||
.png({ compressionLevel: 9, palette: true })
|
||||
.toBuffer()
|
||||
if (pngCompressed.length <= IMAGE_TARGET_RAW_SIZE) {
|
||||
if (pngCompressed.length <= targetRawSize) {
|
||||
return {
|
||||
buffer: pngCompressed,
|
||||
mediaType: 'png',
|
||||
@@ -258,7 +273,7 @@ export async function maybeResizeAndDownsampleImageBuffer(
|
||||
const compressedBuffer = await sharp(imageBuffer)
|
||||
.jpeg({ quality })
|
||||
.toBuffer()
|
||||
if (compressedBuffer.length <= IMAGE_TARGET_RAW_SIZE) {
|
||||
if (compressedBuffer.length <= targetRawSize) {
|
||||
return {
|
||||
buffer: compressedBuffer,
|
||||
mediaType: 'jpeg',
|
||||
@@ -275,14 +290,14 @@ export async function maybeResizeAndDownsampleImageBuffer(
|
||||
}
|
||||
|
||||
// Constrain dimensions if needed
|
||||
if (width > IMAGE_MAX_WIDTH) {
|
||||
height = Math.round((height * IMAGE_MAX_WIDTH) / width)
|
||||
width = IMAGE_MAX_WIDTH
|
||||
if (width > maxWidth) {
|
||||
height = Math.round((height * maxWidth) / width)
|
||||
width = maxWidth
|
||||
}
|
||||
|
||||
if (height > IMAGE_MAX_HEIGHT) {
|
||||
width = Math.round((width * IMAGE_MAX_HEIGHT) / height)
|
||||
height = IMAGE_MAX_HEIGHT
|
||||
if (height > maxHeight) {
|
||||
width = Math.round((width * maxHeight) / height)
|
||||
height = maxHeight
|
||||
}
|
||||
|
||||
// IMPORTANT: Always create fresh sharp(imageBuffer) instances for each operation.
|
||||
@@ -298,7 +313,7 @@ export async function maybeResizeAndDownsampleImageBuffer(
|
||||
.toBuffer()
|
||||
|
||||
// If still too large after resize, try compression
|
||||
if (resizedImageBuffer.length > IMAGE_TARGET_RAW_SIZE) {
|
||||
if (resizedImageBuffer.length > targetRawSize) {
|
||||
// For PNGs, try PNG compression first to preserve transparency
|
||||
if (isPng) {
|
||||
const pngCompressed = await sharp(imageBuffer)
|
||||
@@ -308,7 +323,7 @@ export async function maybeResizeAndDownsampleImageBuffer(
|
||||
})
|
||||
.png({ compressionLevel: 9, palette: true })
|
||||
.toBuffer()
|
||||
if (pngCompressed.length <= IMAGE_TARGET_RAW_SIZE) {
|
||||
if (pngCompressed.length <= targetRawSize) {
|
||||
return {
|
||||
buffer: pngCompressed,
|
||||
mediaType: 'png',
|
||||
@@ -331,7 +346,7 @@ export async function maybeResizeAndDownsampleImageBuffer(
|
||||
})
|
||||
.jpeg({ quality })
|
||||
.toBuffer()
|
||||
if (compressedBuffer.length <= IMAGE_TARGET_RAW_SIZE) {
|
||||
if (compressedBuffer.length <= targetRawSize) {
|
||||
return {
|
||||
buffer: compressedBuffer,
|
||||
mediaType: 'jpeg',
|
||||
@@ -407,11 +422,11 @@ export async function maybeResizeAndDownsampleImageBuffer(
|
||||
imageBuffer[1] === 0x50 &&
|
||||
imageBuffer[2] === 0x4e &&
|
||||
imageBuffer[3] === 0x47 &&
|
||||
(imageBuffer.readUInt32BE(16) > IMAGE_MAX_WIDTH ||
|
||||
imageBuffer.readUInt32BE(20) > IMAGE_MAX_HEIGHT)
|
||||
(imageBuffer.readUInt32BE(16) > maxWidth ||
|
||||
imageBuffer.readUInt32BE(20) > maxHeight)
|
||||
|
||||
// If original image's base64 encoding is within API limit, allow it through uncompressed
|
||||
if (base64Size <= API_IMAGE_MAX_BASE64_SIZE && !overDim) {
|
||||
if (base64Size <= maxBase64Size && !overDim) {
|
||||
logEvent('tengu_image_resize_fallback', {
|
||||
original_size_bytes: originalSize,
|
||||
base64_size_bytes: base64Size,
|
||||
@@ -423,7 +438,7 @@ export async function maybeResizeAndDownsampleImageBuffer(
|
||||
// Image is too large and we failed to compress it - fail with user-friendly error
|
||||
throw new ImageResizeError(
|
||||
overDim
|
||||
? `Unable to resize image — dimensions exceed the ${IMAGE_MAX_WIDTH}x${IMAGE_MAX_HEIGHT}px limit and image processing failed. ` +
|
||||
? `Unable to resize image — dimensions exceed the ${maxWidth}x${maxHeight}px limit and image processing failed. ` +
|
||||
`Please resize the image to reduce its pixel dimensions.`
|
||||
: `Unable to resize image (${formatFileSize(originalSize)} raw, ${formatFileSize(base64Size)} base64). ` +
|
||||
`The image exceeds the 5MB API limit and compression failed. ` +
|
||||
|
||||
Reference in New Issue
Block a user