| 21 | const DEFAULT_TASK_OUTPUT_PREVIEW_BYTES = 32 * 1024; |
| 22 | |
| 23 | export class TaskService extends Disposable implements ITaskService { |
| 24 | readonly _serviceBrand: undefined; |
| 25 | |
| 26 | constructor(@ICoreProcessService private readonly core: ICoreProcessService) { |
| 27 | super(); |
| 28 | } |
| 29 | |
| 30 | async list(sessionId: string, query: TaskListQuery): Promise<readonly BackgroundTask[]> { |
| 31 | await this._requireSession(sessionId); |
| 32 | const raw = await this._getAllRaw(sessionId); |
| 33 | const all = raw.map((info) => toProtocolTask(sessionId, info)); |
| 34 | if (query.status !== undefined) { |
| 35 | return all.filter((t) => t.status === query.status); |
| 36 | } |
| 37 | return all; |
| 38 | } |
| 39 | |
| 40 | async get( |
| 41 | sessionId: string, |
| 42 | taskId: string, |
| 43 | options?: GetTaskOptions, |
| 44 | ): Promise<BackgroundTask> { |
| 45 | await this._requireSession(sessionId); |
| 46 | const raw = await this._getAllRaw(sessionId); |
| 47 | const found = raw.find((t) => t.taskId === taskId); |
| 48 | if (found === undefined) { |
| 49 | throw new TaskNotFoundError(sessionId, taskId); |
| 50 | } |
| 51 | |
| 52 | let output: { preview: string; bytes: number } | undefined; |
| 53 | if (options?.withOutput) { |
| 54 | const tailBytes = options.outputBytes ?? DEFAULT_TASK_OUTPUT_PREVIEW_BYTES; |
| 55 | try { |
| 56 | const preview = await this.core.rpc.getBackgroundOutput({ |
| 57 | sessionId, |
| 58 | agentId: MAIN_AGENT_ID, |
| 59 | taskId, |
| 60 | tail: tailBytes, |
| 61 | }); |
| 62 | if (preview.length > 0) { |
| 63 | output = { preview, bytes: Buffer.byteLength(preview, 'utf-8') }; |
| 64 | } |
| 65 | } catch { |
| 66 | // Output may not be available yet; fall back to task metadata only. |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return toProtocolTask(sessionId, found, output); |
| 71 | } |
| 72 | |
| 73 | async cancel(sessionId: string, taskId: string): Promise<{ cancelled: true }> { |
| 74 | await this._requireSession(sessionId); |
| 75 | // Pre-fetch so we can distinguish the 40406 (not found) and 40904 (already |
| 76 | // finished) cases deterministically — agent-core's `stopBackground` is a |
| 77 | // fire-and-forget call that doesn't surface this. |
| 78 | const raw = await this._getAllRaw(sessionId); |
| 79 | const found = raw.find((t) => t.taskId === taskId); |
| 80 | if (found === undefined) { |
nothing calls this directly
no outgoing calls
no test coverage detected