import { randomBytes } from 'crypto'import type { AppState } from './state/AppState.js'import type { AgentId } from './types/ids.js'import { getTaskOutputPath } from './utils/task/diskOutput.js'export type TaskType =| 'local_bash'| 'local_agent'| 'remote_agent'| 'in_process_teammate'| 'local_workflow'| 'monitor_mcp'| 'dream'export type TaskStatus =| 'pending'| 'running'| 'completed'| 'failed'| 'killed'/*** True when a task is in a terminal state and will not transition further.* Used to guard against injecting messages into dead teammates, evicting* finished tasks from AppState, and orphan-cleanup paths.*/export function isTerminalTaskStatus(status: TaskStatus): boolean {return status === 'completed' || status === 'failed' || status === 'killed'}export type TaskHandle = {taskId: stringcleanup?: () => void}export type SetAppState = (f: (prev: AppState) => AppState) => voidexport type TaskContext = {abortController: AbortControllergetAppState: () => AppStatesetAppState: SetAppState}// Base fields shared by all task statesexport type TaskStateBase = {id: stringtype: TaskTypestatus: TaskStatusdescription: stringtoolUseId?: stringstartTime: numberendTime?: numbertotalPausedMs?: numberoutputFile: stringoutputOffset: numbernotified: boolean}export type LocalShellSpawnInput = {command: stringdescription: stringtimeout?: numbertoolUseId?: stringagentId?: AgentId/** UI display variant: description-as-label, dialog title, status bar pill. */kind?: 'bash' | 'monitor'}// What getTaskByType dispatches for: kill. spawn/render were never// called polymorphically (removed in #22546). All six kill implementations// use only setAppState — getAppState/abortController were dead weight.export type Task = {name: stringtype: TaskTypekill(taskId: string, setAppState: SetAppState): Promise<void>}// Task ID prefixesconst TASK_ID_PREFIXES: Record<string, string> = {local_bash: 'b', // Keep as 'b' for backward compatibilitylocal_agent: 'a',remote_agent: 'r',in_process_teammate: 't',local_workflow: 'w',monitor_mcp: 'm',dream: 'd',}// Get task ID prefixfunction getTaskIdPrefix(type: TaskType): string {return TASK_ID_PREFIXES[type] ?? 'x'}// Case-insensitive-safe alphabet (digits + lowercase) for task IDs.// 36^8 ≈ 2.8 trillion combinations, sufficient to resist brute-force symlink attacks.const TASK_ID_ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz'export function generateTaskId(type: TaskType): string {const prefix = getTaskIdPrefix(type)const bytes = randomBytes(8)let id = prefixfor (let i = 0; i < 8; i++) {id += TASK_ID_ALPHABET[bytes[i]! % TASK_ID_ALPHABET.length]}return id}export function createTaskStateBase(id: string,type: TaskType,description: string,toolUseId?: string,): TaskStateBase {return {id,type,status: 'pending',description,toolUseId,startTime: Date.now(),outputFile: getTaskOutputPath(id),outputOffset: 0,notified: false,}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。