(sessionId: string, mode: CollaborationMode)
| 1835 | } |
| 1836 | |
| 1837 | async setCollaborationMode(sessionId: string, mode: CollaborationMode): Promise<SessionSummary> { |
| 1838 | const previous = await this.deps.store.readHeader(sessionId); |
| 1839 | const from = previous.collaborationMode ?? 'agent'; |
| 1840 | if (from === mode) return headerToSummary(previous); |
| 1841 | if (mode === 'plan' && previous.subagentParent) { |
| 1842 | throw new PlanConflictError('Linked child Sessions cannot enter Plan mode'); |
| 1843 | } |
| 1844 | if (this.runtimeKernel.hasActiveRuns(sessionId)) { |
| 1845 | throw new Error('当前任务正在运行,等结束后再切换协作模式。'); |
| 1846 | } |
| 1847 | if (previous.status === 'waiting_for_user') { |
| 1848 | throw new Error('当前有工具调用正在等待确认,处理后再切换协作模式。'); |
| 1849 | } |
| 1850 | const planState = await this.requirePlanStore().readState(sessionId); |
| 1851 | if (mode === 'plan' && planState.activeExecutionId) { |
| 1852 | throw new Error('当前计划仍在执行,结束或中断后才能切换到 Plan Mode。'); |
| 1853 | } |
| 1854 | const latestProposal = planState.proposals.find( |
| 1855 | (proposal) => proposal.proposalId === planState.latestProposalId, |
| 1856 | ); |
| 1857 | if (mode === 'agent' && latestProposal?.status === 'pending_approval') { |
| 1858 | throw new Error('当前方案正在等待审批,请明确放弃方案后再退出 Plan Mode。'); |
| 1859 | } |
| 1860 | |
| 1861 | const next = await this.deps.store.updateHeader(sessionId, { |
| 1862 | collaborationMode: mode, |
| 1863 | }); |
| 1864 | await this.deps.store.appendMessage(sessionId, { |
| 1865 | type: 'system_note', |
| 1866 | id: this.deps.newId(), |
| 1867 | ts: this.deps.now(), |
| 1868 | kind: 'mode_change', |
| 1869 | data: { dimension: 'collaboration', from, to: mode }, |
| 1870 | } satisfies SystemNoteMessage); |
| 1871 | this.runtimeKernel.updateCachedHeader(sessionId, next); |
| 1872 | await this.runtimeKernel.disposeBackend(sessionId); |
| 1873 | return headerToSummary(next); |
| 1874 | } |
| 1875 | |
| 1876 | async setOrchestrationMode(sessionId: string, mode: OrchestrationMode): Promise<SessionSummary> { |
| 1877 | const previous = await this.deps.store.readHeader(sessionId); |
no test coverage detected