|
| 1 | +import { |
| 2 | + formatAutonomyFlowDetail, |
| 3 | + formatAutonomyFlowsList, |
| 4 | + formatAutonomyFlowsStatus, |
| 5 | + getAutonomyFlowById, |
| 6 | + listAutonomyFlows, |
| 7 | + requestManagedAutonomyFlowCancel, |
| 8 | +} from '../../utils/autonomyFlows.js' |
| 9 | +import { |
| 10 | + formatAutonomyRunsList, |
| 11 | + formatAutonomyRunsStatus, |
| 12 | + listAutonomyRuns, |
| 13 | + markAutonomyRunCancelled, |
| 14 | + resumeManagedAutonomyFlowPrompt, |
| 15 | +} from '../../utils/autonomyRuns.js' |
| 16 | +import { |
| 17 | + formatAutonomyDeepStatus, |
| 18 | + formatAutonomyDeepStatusSections, |
| 19 | + type AutonomyDeepStatusSectionId, |
| 20 | +} from '../../utils/autonomyStatus.js' |
| 21 | +import { |
| 22 | + AUTONOMY_USAGE, |
| 23 | + parseAutonomyArgs, |
| 24 | +} from '../../utils/autonomyCommandSpec.js' |
| 25 | +import { |
| 26 | + enqueuePendingNotification, |
| 27 | + removeByFilter, |
| 28 | +} from '../../utils/messageQueueManager.js' |
| 29 | + |
| 30 | +export function parseAutonomyLimit(raw?: string | number): number { |
| 31 | + const parsed = typeof raw === 'number' ? raw : Number.parseInt(raw ?? '', 10) |
| 32 | + if (!Number.isFinite(parsed) || parsed <= 0) { |
| 33 | + return 10 |
| 34 | + } |
| 35 | + return Math.min(parsed, 50) |
| 36 | +} |
| 37 | + |
| 38 | +export async function getAutonomyStatusText(options?: { |
| 39 | + deep?: boolean |
| 40 | +}): Promise<string> { |
| 41 | + const [runs, flows] = await Promise.all([ |
| 42 | + listAutonomyRuns(), |
| 43 | + listAutonomyFlows(), |
| 44 | + ]) |
| 45 | + |
| 46 | + if (options?.deep) { |
| 47 | + return formatAutonomyDeepStatus({ runs, flows }) |
| 48 | + } |
| 49 | + |
| 50 | + return [ |
| 51 | + formatAutonomyRunsStatus(runs), |
| 52 | + formatAutonomyFlowsStatus(flows), |
| 53 | + ].join('\n') |
| 54 | +} |
| 55 | + |
| 56 | +export async function getAutonomyDeepSectionText( |
| 57 | + sectionId: AutonomyDeepStatusSectionId, |
| 58 | +): Promise<string> { |
| 59 | + const [runs, flows] = await Promise.all([ |
| 60 | + listAutonomyRuns(), |
| 61 | + listAutonomyFlows(), |
| 62 | + ]) |
| 63 | + const sections = await formatAutonomyDeepStatusSections({ runs, flows }) |
| 64 | + const section = sections.find(item => item.id === sectionId) |
| 65 | + if (!section) { |
| 66 | + return `Autonomy deep status section not found: ${sectionId}` |
| 67 | + } |
| 68 | + return [`# ${section.title}`, section.content].join('\n') |
| 69 | +} |
| 70 | + |
| 71 | +export async function autonomyStatusHandler(options?: { |
| 72 | + deep?: boolean |
| 73 | +}): Promise<void> { |
| 74 | + process.stdout.write(`${await getAutonomyStatusText(options)}\n`) |
| 75 | +} |
| 76 | + |
| 77 | +export async function getAutonomyRunsText( |
| 78 | + limit?: string | number, |
| 79 | +): Promise<string> { |
| 80 | + return formatAutonomyRunsList( |
| 81 | + await listAutonomyRuns(), |
| 82 | + parseAutonomyLimit(limit), |
| 83 | + ) |
| 84 | +} |
| 85 | + |
| 86 | +export async function autonomyRunsHandler( |
| 87 | + limit?: string | number, |
| 88 | +): Promise<void> { |
| 89 | + process.stdout.write(`${await getAutonomyRunsText(limit)}\n`) |
| 90 | +} |
| 91 | + |
| 92 | +export async function getAutonomyFlowsText( |
| 93 | + limit?: string | number, |
| 94 | +): Promise<string> { |
| 95 | + return formatAutonomyFlowsList( |
| 96 | + await listAutonomyFlows(), |
| 97 | + parseAutonomyLimit(limit), |
| 98 | + ) |
| 99 | +} |
| 100 | + |
| 101 | +export async function autonomyFlowsHandler( |
| 102 | + limit?: string | number, |
| 103 | +): Promise<void> { |
| 104 | + process.stdout.write(`${await getAutonomyFlowsText(limit)}\n`) |
| 105 | +} |
| 106 | + |
| 107 | +export async function getAutonomyFlowText(flowId: string): Promise<string> { |
| 108 | + return formatAutonomyFlowDetail(await getAutonomyFlowById(flowId)) |
| 109 | +} |
| 110 | + |
| 111 | +export async function autonomyFlowHandler(flowId: string): Promise<void> { |
| 112 | + process.stdout.write(`${await getAutonomyFlowText(flowId)}\n`) |
| 113 | +} |
| 114 | + |
| 115 | +export async function cancelAutonomyFlowText( |
| 116 | + flowId: string, |
| 117 | + options?: { |
| 118 | + removeQueuedInMemory?: boolean |
| 119 | + }, |
| 120 | +): Promise<string> { |
| 121 | + const cancelled = await requestManagedAutonomyFlowCancel({ flowId }) |
| 122 | + if (!cancelled) { |
| 123 | + return 'Autonomy flow not found.' |
| 124 | + } |
| 125 | + if (!cancelled.accepted) { |
| 126 | + return `Autonomy flow ${flowId} is already terminal (${cancelled.flow.status}).` |
| 127 | + } |
| 128 | + |
| 129 | + let removedCount = 0 |
| 130 | + if (options?.removeQueuedInMemory) { |
| 131 | + const removed = removeByFilter(cmd => cmd.autonomy?.flowId === flowId) |
| 132 | + removedCount = removed.length |
| 133 | + for (const command of removed) { |
| 134 | + if (command.autonomy?.runId) { |
| 135 | + await markAutonomyRunCancelled(command.autonomy.runId) |
| 136 | + } |
| 137 | + } |
| 138 | + } else { |
| 139 | + for (const runId of cancelled.queuedRunIds) { |
| 140 | + await markAutonomyRunCancelled(runId) |
| 141 | + } |
| 142 | + removedCount = cancelled.queuedRunIds.length |
| 143 | + } |
| 144 | + |
| 145 | + return cancelled.flow.status === 'running' |
| 146 | + ? `Cancellation requested for flow ${flowId}. The current step is still running, and no new steps will be started.` |
| 147 | + : `Cancelled flow ${flowId}. Removed ${removedCount} queued step(s).` |
| 148 | +} |
| 149 | + |
| 150 | +export async function autonomyFlowCancelHandler(flowId: string): Promise<void> { |
| 151 | + process.stdout.write(`${await cancelAutonomyFlowText(flowId)}\n`) |
| 152 | +} |
| 153 | + |
| 154 | +export async function resumeAutonomyFlowText( |
| 155 | + flowId: string, |
| 156 | + options?: { |
| 157 | + enqueueInMemory?: boolean |
| 158 | + }, |
| 159 | +): Promise<string> { |
| 160 | + const command = await resumeManagedAutonomyFlowPrompt({ flowId }) |
| 161 | + if (!command) { |
| 162 | + return 'Autonomy flow is not waiting or was not found.' |
| 163 | + } |
| 164 | + |
| 165 | + if (options?.enqueueInMemory) { |
| 166 | + enqueuePendingNotification(command) |
| 167 | + return `Queued the next managed step for flow ${flowId}.` |
| 168 | + } |
| 169 | + |
| 170 | + const runId = command.autonomy?.runId ?? 'unknown' |
| 171 | + return [ |
| 172 | + `Prepared the next managed step for flow ${flowId}.`, |
| 173 | + `Run ID: ${runId}`, |
| 174 | + '', |
| 175 | + 'Prompt:', |
| 176 | + typeof command.value === 'string' ? command.value : String(command.value), |
| 177 | + ].join('\n') |
| 178 | +} |
| 179 | + |
| 180 | +export async function autonomyFlowResumeHandler(flowId: string): Promise<void> { |
| 181 | + process.stdout.write(`${await resumeAutonomyFlowText(flowId)}\n`) |
| 182 | +} |
| 183 | + |
| 184 | +export async function getAutonomyCommandText( |
| 185 | + args: string, |
| 186 | + options?: { |
| 187 | + enqueueInMemory?: boolean |
| 188 | + removeQueuedInMemory?: boolean |
| 189 | + }, |
| 190 | +): Promise<string> { |
| 191 | + const parsed = parseAutonomyArgs(args) |
| 192 | + |
| 193 | + switch (parsed.type) { |
| 194 | + case 'status': |
| 195 | + return getAutonomyStatusText({ deep: parsed.deep }) |
| 196 | + case 'runs': |
| 197 | + return getAutonomyRunsText(parsed.limit) |
| 198 | + case 'flows': |
| 199 | + return getAutonomyFlowsText(parsed.limit) |
| 200 | + case 'flow-detail': |
| 201 | + return getAutonomyFlowText(parsed.flowId) |
| 202 | + case 'flow-cancel': |
| 203 | + return cancelAutonomyFlowText(parsed.flowId, { |
| 204 | + removeQueuedInMemory: options?.removeQueuedInMemory, |
| 205 | + }) |
| 206 | + case 'flow-resume': |
| 207 | + return resumeAutonomyFlowText(parsed.flowId, { |
| 208 | + enqueueInMemory: options?.enqueueInMemory, |
| 209 | + }) |
| 210 | + case 'usage': |
| 211 | + return AUTONOMY_USAGE |
| 212 | + } |
| 213 | +} |
0 commit comments