( taskId: string, success: boolean, setAppState: SetAppState, )
| 166 | * Called when the backgrounded query finishes. |
| 167 | */ |
| 168 | export function completeMainSessionTask( |
| 169 | taskId: string, |
| 170 | success: boolean, |
| 171 | setAppState: SetAppState, |
| 172 | ): void { |
| 173 | let wasBackgrounded = true |
| 174 | let toolUseId: string | undefined |
| 175 | |
| 176 | updateTaskState<LocalMainSessionTaskState>(taskId, setAppState, task => { |
| 177 | if (task.status !== 'running') { |
| 178 | return task |
| 179 | } |
| 180 | |
| 181 | // Track if task was backgrounded (for notification decision) |
| 182 | wasBackgrounded = task.isBackgrounded ?? true |
| 183 | toolUseId = task.toolUseId |
| 184 | |
| 185 | task.unregisterCleanup?.() |
| 186 | |
| 187 | return { |
| 188 | ...task, |
| 189 | status: success ? 'completed' : 'failed', |
| 190 | endTime: Date.now(), |
| 191 | messages: task.messages?.length ? [task.messages.at(-1)!] : undefined, |
| 192 | } |
| 193 | }) |
| 194 | |
| 195 | void evictTaskOutput(taskId) |
| 196 | |
| 197 | // Only send notification if task is still backgrounded (not foregrounded) |
| 198 | // If foregrounded, user is watching it directly - no notification needed |
| 199 | if (wasBackgrounded) { |
| 200 | enqueueMainSessionNotification( |
| 201 | taskId, |
| 202 | 'Background session', |
| 203 | success ? 'completed' : 'failed', |
| 204 | setAppState, |
| 205 | toolUseId, |
| 206 | ) |
| 207 | } else { |
| 208 | // Foregrounded: no XML notification (TUI user is watching), but SDK |
| 209 | // consumers still need to see the task_started bookend close. |
| 210 | // Set notified so evictTerminalTask/generateTaskAttachments eviction |
| 211 | // guards pass; the backgrounded path sets this inside |
| 212 | // enqueueMainSessionNotification's check-and-set. |
| 213 | updateTaskState<LocalMainSessionTaskState>(taskId, setAppState, task => ({ |
| 214 | ...task, |
| 215 | notified: true, |
| 216 | })) |
| 217 | emitTaskTerminatedSdk(taskId, success ? 'completed' : 'failed', { |
| 218 | toolUseId, |
| 219 | summary: 'Background session', |
| 220 | }) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Enqueue a notification about the backgrounded session completing. |
no test coverage detected