File size: 18,324 Bytes
39e315a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | import { c as _c } from "react-compiler-runtime";
import React from 'react';
import { z } from 'zod/v4';
import { FallbackToolUseErrorMessage } from '../../components/FallbackToolUseErrorMessage.js';
import { FallbackToolUseRejectedMessage } from '../../components/FallbackToolUseRejectedMessage.js';
import { MessageResponse } from '../../components/MessageResponse.js';
import { Box, Text } from '../../ink.js';
import { useShortcutDisplay } from '../../keybindings/useShortcutDisplay.js';
import type { TaskType } from '../../Task.js';
import type { Tool } from '../../Tool.js';
import { buildTool, type ToolDef } from '../../Tool.js';
import type { LocalAgentTaskState } from '../../tasks/LocalAgentTask/LocalAgentTask.js';
import type { LocalShellTaskState } from '../../tasks/LocalShellTask/guards.js';
import type { RemoteAgentTaskState } from '../../tasks/RemoteAgentTask/RemoteAgentTask.js';
import type { TaskState } from '../../tasks/types.js';
import { AbortError } from '../../utils/errors.js';
import { lazySchema } from '../../utils/lazySchema.js';
import { extractTextContent } from '../../utils/messages.js';
import { semanticBoolean } from '../../utils/semanticBoolean.js';
import { sleep } from '../../utils/sleep.js';
import { jsonParse } from '../../utils/slowOperations.js';
import { countCharInString } from '../../utils/stringUtils.js';
import { getTaskOutput } from '../../utils/task/diskOutput.js';
import { updateTaskState } from '../../utils/task/framework.js';
import { formatTaskOutput } from '../../utils/task/outputFormatting.js';
import type { ThemeName } from '../../utils/theme.js';
import { AgentPromptDisplay, AgentResponseDisplay } from '../AgentTool/UI.js';
import BashToolResultMessage from '../BashTool/BashToolResultMessage.js';
import { TASK_OUTPUT_TOOL_NAME } from './constants.js';
const inputSchema = lazySchema(() => z.strictObject({
task_id: z.string().describe('The task ID to get output from'),
block: semanticBoolean(z.boolean().default(true)).describe('Whether to wait for completion'),
timeout: z.number().min(0).max(600000).default(30000).describe('Max wait time in ms')
}));
type InputSchema = ReturnType<typeof inputSchema>;
type TaskOutputToolInput = z.infer<InputSchema>;
// Unified output type covering all task types
type TaskOutput = {
task_id: string;
task_type: TaskType;
status: string;
description: string;
output: string;
exitCode?: number | null;
error?: string;
// For agents
prompt?: string;
result?: string;
};
type TaskOutputToolOutput = {
retrieval_status: 'success' | 'timeout' | 'not_ready';
task: TaskOutput | null;
};
// Re-export Progress from centralized types to break import cycles
export type { TaskOutputProgress as Progress } from '../../types/tools.js';
// Get output for any task type
async function getTaskOutputData(task: TaskState): Promise<TaskOutput> {
let output: string;
if (task.type === 'local_bash') {
const bashTask = task as LocalShellTaskState;
const taskOutputObj = bashTask.shellCommand?.taskOutput;
if (taskOutputObj) {
const stdout = await taskOutputObj.getStdout();
const stderr = taskOutputObj.getStderr();
output = [stdout, stderr].filter(Boolean).join('\n');
} else {
output = await getTaskOutput(task.id);
}
} else {
output = await getTaskOutput(task.id);
}
const baseOutput: TaskOutput = {
task_id: task.id,
task_type: task.type,
status: task.status,
description: task.description,
output
};
// Add type-specific fields
if (task.type === 'local_bash') {
const bashTask = task as LocalShellTaskState;
return {
...baseOutput,
exitCode: bashTask.result?.code ?? null
};
}
if (task.type === 'local_agent') {
const agentTask = task as LocalAgentTaskState;
// Prefer the clean final answer from the in-memory result over the raw
// JSONL transcript on disk. The disk output is a symlink to the full
// session transcript (every message, tool use, etc.), not just the
// subagent's answer. The in-memory result contains only the final
// assistant text content blocks.
const cleanResult = agentTask.result ? extractTextContent(agentTask.result.content, '\n') : undefined;
return {
...baseOutput,
prompt: agentTask.prompt,
result: cleanResult || output,
output: cleanResult || output,
error: agentTask.error
};
}
if (task.type === 'remote_agent') {
const remoteTask = task as RemoteAgentTaskState;
return {
...baseOutput,
prompt: remoteTask.command
};
}
return baseOutput;
}
// Wait for task to complete
async function waitForTaskCompletion(taskId: string, getAppState: () => {
tasks?: Record<string, TaskState>;
}, timeoutMs: number, abortController?: AbortController): Promise<TaskState | null> {
const startTime = Date.now();
while (Date.now() - startTime < timeoutMs) {
// Check abort signal
if (abortController?.signal.aborted) {
throw new AbortError();
}
const state = getAppState();
const task = state.tasks?.[taskId] as TaskState | undefined;
if (!task) {
return null;
}
if (task.status !== 'running' && task.status !== 'pending') {
return task;
}
// Wait before polling again
await sleep(100);
}
// Timeout - return current state
const finalState = getAppState();
return finalState.tasks?.[taskId] as TaskState ?? null;
}
export const TaskOutputTool: Tool<InputSchema, TaskOutputToolOutput> = buildTool({
name: TASK_OUTPUT_TOOL_NAME,
searchHint: 'read output/logs from a background task',
maxResultSizeChars: 100_000,
shouldDefer: true,
// Backwards-compatible aliases for renamed tools
aliases: ['AgentOutputTool', 'BashOutputTool'],
userFacingName() {
return 'Task Output';
},
get inputSchema(): InputSchema {
return inputSchema();
},
async description() {
return '[Deprecated] — prefer Read on the task output file path';
},
isConcurrencySafe(_input) {
return this.isReadOnly?.(_input) ?? false;
},
isEnabled() {
return "external" !== 'ant';
},
isReadOnly(_input) {
return true;
},
toAutoClassifierInput(input) {
return input.task_id;
},
async prompt() {
return `DEPRECATED: Prefer using the Read tool on the task's output file path instead. Background tasks return their output file path in the tool result, and you receive a <task-notification> with the same path when the task completes — Read that file directly.
- Retrieves output from a running or completed task (background shell, agent, or remote session)
- Takes a task_id parameter identifying the task
- Returns the task output along with status information
- Use block=true (default) to wait for task completion
- Use block=false for non-blocking check of current status
- Task IDs can be found using the /tasks command
- Works with all task types: background shells, async agents, and remote sessions`;
},
async validateInput({
task_id
}, {
getAppState
}) {
if (!task_id) {
return {
result: false,
message: 'Task ID is required',
errorCode: 1
};
}
const appState = getAppState();
const task = appState.tasks?.[task_id] as TaskState | undefined;
if (!task) {
return {
result: false,
message: `No task found with ID: ${task_id}`,
errorCode: 2
};
}
return {
result: true
};
},
async call(input: TaskOutputToolInput, toolUseContext, _canUseTool, _parentMessage, onProgress) {
const {
task_id,
block,
timeout
} = input;
const appState = toolUseContext.getAppState();
const task = appState.tasks?.[task_id] as TaskState | undefined;
if (!task) {
throw new Error(`No task found with ID: ${task_id}`);
}
if (!block) {
// Non-blocking: return current state
if (task.status !== 'running' && task.status !== 'pending') {
// Mark as notified
updateTaskState(task_id, toolUseContext.setAppState, t => ({
...t,
notified: true
}));
return {
data: {
retrieval_status: 'success' as const,
task: await getTaskOutputData(task)
}
};
}
return {
data: {
retrieval_status: 'not_ready' as const,
task: await getTaskOutputData(task)
}
};
}
// Blocking: wait for completion
if (onProgress) {
onProgress({
toolUseID: `task-output-waiting-${Date.now()}`,
data: {
type: 'waiting_for_task',
taskDescription: task.description,
taskType: task.type
}
});
}
const completedTask = await waitForTaskCompletion(task_id, toolUseContext.getAppState, timeout, toolUseContext.abortController);
if (!completedTask) {
return {
data: {
retrieval_status: 'timeout' as const,
task: null
}
};
}
if (completedTask.status === 'running' || completedTask.status === 'pending') {
return {
data: {
retrieval_status: 'timeout' as const,
task: await getTaskOutputData(completedTask)
}
};
}
// Mark as notified
updateTaskState(task_id, toolUseContext.setAppState, t => ({
...t,
notified: true
}));
return {
data: {
retrieval_status: 'success' as const,
task: await getTaskOutputData(completedTask)
}
};
},
mapToolResultToToolResultBlockParam(data, toolUseID) {
const parts: string[] = [];
parts.push(`<retrieval_status>${data.retrieval_status}</retrieval_status>`);
if (data.task) {
parts.push(`<task_id>${data.task.task_id}</task_id>`);
parts.push(`<task_type>${data.task.task_type}</task_type>`);
parts.push(`<status>${data.task.status}</status>`);
if (data.task.exitCode !== undefined && data.task.exitCode !== null) {
parts.push(`<exit_code>${data.task.exitCode}</exit_code>`);
}
if (data.task.output?.trim()) {
const {
content
} = formatTaskOutput(data.task.output, data.task.task_id);
parts.push(`<output>\n${content.trimEnd()}\n</output>`);
}
if (data.task.error) {
parts.push(`<error>${data.task.error}</error>`);
}
}
return {
tool_use_id: toolUseID,
type: 'tool_result' as const,
content: parts.join('\n\n')
};
},
renderToolUseMessage(input) {
const {
block = true
} = input;
if (!block) {
return 'non-blocking';
}
return '';
},
renderToolUseTag(input) {
if (!input.task_id) {
return null;
}
return <Text dimColor> {input.task_id}</Text>;
},
renderToolUseProgressMessage(progressMessages) {
const lastProgress = progressMessages[progressMessages.length - 1];
const progressData = lastProgress?.data as {
taskDescription?: string;
taskType?: string;
} | undefined;
return <Box flexDirection="column">
{progressData?.taskDescription && <Text> {progressData.taskDescription}</Text>}
<Text>
Waiting for task{' '}
<Text dimColor>(esc to give additional instructions)</Text>
</Text>
</Box>;
},
renderToolResultMessage(content, _, {
verbose,
theme
}) {
return <TaskOutputResultDisplay content={content} verbose={verbose} theme={theme} />;
},
renderToolUseRejectedMessage() {
return <FallbackToolUseRejectedMessage />;
},
renderToolUseErrorMessage(result, {
verbose
}) {
return <FallbackToolUseErrorMessage result={result} verbose={verbose} />;
}
} satisfies ToolDef<InputSchema, TaskOutputToolOutput>);
function TaskOutputResultDisplay(t0) {
const $ = _c(54);
const {
content,
verbose: t1,
theme
} = t0;
const verbose = t1 === undefined ? false : t1;
const expandShortcut = useShortcutDisplay("app:toggleTranscript", "Global", "ctrl+o");
let t2;
if ($[0] !== content) {
t2 = typeof content === "string" ? jsonParse(content) : content;
$[0] = content;
$[1] = t2;
} else {
t2 = $[1];
}
const result = t2;
if (!result.task) {
let t3;
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
t3 = <MessageResponse><Text dimColor={true}>No task output available</Text></MessageResponse>;
$[2] = t3;
} else {
t3 = $[2];
}
return t3;
}
const {
task
} = result;
if (task.task_type === "local_bash") {
let t3;
if ($[3] !== task.error || $[4] !== task.output) {
t3 = {
stdout: task.output,
stderr: "",
isImage: false,
dangerouslyDisableSandbox: true,
returnCodeInterpretation: task.error
};
$[3] = task.error;
$[4] = task.output;
$[5] = t3;
} else {
t3 = $[5];
}
const bashOut = t3;
let t4;
if ($[6] !== bashOut || $[7] !== verbose) {
t4 = <BashToolResultMessage content={bashOut} verbose={verbose} />;
$[6] = bashOut;
$[7] = verbose;
$[8] = t4;
} else {
t4 = $[8];
}
return t4;
}
if (task.task_type === "local_agent") {
const lineCount = task.result ? countCharInString(task.result, "\n") + 1 : 0;
if (result.retrieval_status === "success") {
if (verbose) {
let t3;
if ($[9] !== lineCount || $[10] !== task.description) {
t3 = <Text>{task.description} ({lineCount} lines)</Text>;
$[9] = lineCount;
$[10] = task.description;
$[11] = t3;
} else {
t3 = $[11];
}
let t4;
if ($[12] !== task.prompt || $[13] !== theme) {
t4 = task.prompt && <AgentPromptDisplay prompt={task.prompt} theme={theme} dim={true} />;
$[12] = task.prompt;
$[13] = theme;
$[14] = t4;
} else {
t4 = $[14];
}
let t5;
if ($[15] !== task.result || $[16] !== theme) {
t5 = task.result && <Box marginTop={1}><AgentResponseDisplay content={[{
type: "text",
text: task.result
}]} theme={theme} /></Box>;
$[15] = task.result;
$[16] = theme;
$[17] = t5;
} else {
t5 = $[17];
}
let t6;
if ($[18] !== task.error) {
t6 = task.error && <Box flexDirection="column" marginTop={1}><Text color="error" bold={true}>Error:</Text><Box paddingLeft={2}><Text color="error">{task.error}</Text></Box></Box>;
$[18] = task.error;
$[19] = t6;
} else {
t6 = $[19];
}
let t7;
if ($[20] !== t4 || $[21] !== t5 || $[22] !== t6) {
t7 = <Box flexDirection="column" paddingLeft={2} marginTop={1}>{t4}{t5}{t6}</Box>;
$[20] = t4;
$[21] = t5;
$[22] = t6;
$[23] = t7;
} else {
t7 = $[23];
}
let t8;
if ($[24] !== t3 || $[25] !== t7) {
t8 = <Box flexDirection="column">{t3}{t7}</Box>;
$[24] = t3;
$[25] = t7;
$[26] = t8;
} else {
t8 = $[26];
}
return t8;
}
let t3;
if ($[27] !== expandShortcut) {
t3 = <MessageResponse><Text dimColor={true}>Read output ({expandShortcut} to expand)</Text></MessageResponse>;
$[27] = expandShortcut;
$[28] = t3;
} else {
t3 = $[28];
}
return t3;
}
if (result.retrieval_status === "timeout" || task.status === "running") {
let t3;
if ($[29] === Symbol.for("react.memo_cache_sentinel")) {
t3 = <MessageResponse><Text dimColor={true}>Task is still running…</Text></MessageResponse>;
$[29] = t3;
} else {
t3 = $[29];
}
return t3;
}
if (result.retrieval_status === "not_ready") {
let t3;
if ($[30] === Symbol.for("react.memo_cache_sentinel")) {
t3 = <MessageResponse><Text dimColor={true}>Task is still running…</Text></MessageResponse>;
$[30] = t3;
} else {
t3 = $[30];
}
return t3;
}
let t3;
if ($[31] === Symbol.for("react.memo_cache_sentinel")) {
t3 = <MessageResponse><Text dimColor={true}>Task not ready</Text></MessageResponse>;
$[31] = t3;
} else {
t3 = $[31];
}
return t3;
}
if (task.task_type === "remote_agent") {
let t3;
if ($[32] !== task.description || $[33] !== task.status) {
t3 = <Text> {task.description} [{task.status}]</Text>;
$[32] = task.description;
$[33] = task.status;
$[34] = t3;
} else {
t3 = $[34];
}
let t4;
if ($[35] !== task.output || $[36] !== verbose) {
t4 = task.output && verbose && <Box paddingLeft={4} marginTop={1}><Text>{task.output}</Text></Box>;
$[35] = task.output;
$[36] = verbose;
$[37] = t4;
} else {
t4 = $[37];
}
let t5;
if ($[38] !== expandShortcut || $[39] !== task.output || $[40] !== verbose) {
t5 = !verbose && task.output && <Text dimColor={true}>{" "}({expandShortcut} to expand)</Text>;
$[38] = expandShortcut;
$[39] = task.output;
$[40] = verbose;
$[41] = t5;
} else {
t5 = $[41];
}
let t6;
if ($[42] !== t3 || $[43] !== t4 || $[44] !== t5) {
t6 = <Box flexDirection="column">{t3}{t4}{t5}</Box>;
$[42] = t3;
$[43] = t4;
$[44] = t5;
$[45] = t6;
} else {
t6 = $[45];
}
return t6;
}
let t3;
if ($[46] !== task.description || $[47] !== task.status) {
t3 = <Text> {task.description} [{task.status}]</Text>;
$[46] = task.description;
$[47] = task.status;
$[48] = t3;
} else {
t3 = $[48];
}
let t4;
if ($[49] !== task.output) {
t4 = task.output && <Box paddingLeft={4}><Text>{task.output.slice(0, 500)}</Text></Box>;
$[49] = task.output;
$[50] = t4;
} else {
t4 = $[50];
}
let t5;
if ($[51] !== t3 || $[52] !== t4) {
t5 = <Box flexDirection="column">{t3}{t4}</Box>;
$[51] = t3;
$[52] = t4;
$[53] = t5;
} else {
t5 = $[53];
}
return t5;
}
export default TaskOutputTool;
|