| 1 | import { composerProfileWithMode, type ComposerProfile, type ComposerProfileField } from "../lib/composerProfile"; |
| 2 | import { modeHasPlan, type CollaborationMode, type Mode, type ToolApprovalMode } from "../lib/types"; |
| 3 | import type { SessionOperationAuthority, SessionResource } from "./useSessionOperations"; |
| 4 | |
| 5 | export type ComposerModeRequest = |
| 6 | | { kind: "mode"; mode: Mode } |
| 7 | | { kind: "collaboration"; mode: CollaborationMode } |
| 8 | | { kind: "approval"; mode: ToolApprovalMode }; |
| 9 | export type ComposerModePorts = { |
| 10 | setMode: (tabId: string, mode: Mode) => Promise<void> | void; |
| 11 | setCollaboration: (tabId: string, mode: CollaborationMode) => Promise<void>; |
| 12 | setApproval: (tabId: string, mode: ToolApprovalMode) => Promise<void> | void; |
| 13 | clearGoal: (tabId: string) => Promise<void>; |
| 14 | setRemote: (tabId: string, collaboration: CollaborationMode, approval: ToolApprovalMode, goal: string) => Promise<string[]>; |
| 15 | drainRemote: (tabId: string, ids: string[]) => void; |
| 16 | patch: (tabId: string, patch: Partial<Omit<ComposerProfile, "pending">>, fields: ComposerProfileField[]) => void; |
| 17 | rememberPlan: (tabId: string, enabled: boolean) => void; |
| 18 | }; |
| 19 | export type ComposerModeInput = { |
| 20 | target: SessionResource; |
| 21 | request: ComposerModeRequest; |
| 22 | remote: boolean; |
| 23 | collaborationMode: CollaborationMode; |
| 24 | toolApprovalMode: ToolApprovalMode; |
| 25 | goal: string; |
| 26 | ports: ComposerModePorts; |
| 27 | }; |
| 28 | |
| 29 | export async function executeComposerMode(input: ComposerModeInput, authority: SessionOperationAuthority): Promise<void> { |
| 30 | const { target: { tabId }, request, ports } = input; |
| 31 | authority.checkpoint(); |
| 32 | let patch: Partial<Omit<ComposerProfile, "pending">>; |
| 33 | let fields: ComposerProfileField[]; |
| 34 | if (request.kind === "mode") { |
| 35 | patch = composerProfileWithMode(request.mode); |
| 36 | fields = ["collaborationMode", "toolApprovalMode", "goal"]; |
| 37 | if (input.remote) { |
| 38 | const ids = await ports.setRemote(tabId, patch.collaborationMode ?? "normal", patch.toolApprovalMode ?? "ask", ""); |
| 39 | authority.checkpoint(); |
| 40 | if (authority.ownsUI()) ports.drainRemote(tabId, ids); |
| 41 | } else await ports.setMode(tabId, request.mode); |
| 42 | authority.checkpoint(); |
| 43 | ports.rememberPlan(tabId, modeHasPlan(request.mode)); |
| 44 | } else if (request.kind === "collaboration") { |
| 45 | const mode = request.mode === "goal" ? "normal" : request.mode; |
| 46 | patch = { collaborationMode: mode, goalDraftMode: request.mode === "goal", goal: "" }; |
| 47 | fields = ["collaborationMode", "goal"]; |
| 48 | if (input.remote) { |
| 49 | const ids = await ports.setRemote(tabId, mode, input.toolApprovalMode, ""); |
| 50 | authority.checkpoint(); |
| 51 | if (authority.ownsUI()) ports.drainRemote(tabId, ids); |
| 52 | } else { |
| 53 | if (input.goal.trim()) { |
| 54 | await ports.clearGoal(tabId); |
| 55 | authority.checkpoint(); |
| 56 | } |
| 57 | await ports.setCollaboration(tabId, mode); |
| 58 | } |
| 59 | authority.checkpoint(); |
| 60 | ports.rememberPlan(tabId, request.mode === "plan"); |
| 61 | } else { |
| 62 | patch = { toolApprovalMode: request.mode }; |
| 63 | fields = ["toolApprovalMode"]; |
| 64 | if (input.remote) { |
| 65 | const mode = input.goal.trim() ? "goal" : input.collaborationMode === "plan" ? "plan" : "normal"; |
| 66 | const ids = await ports.setRemote(tabId, mode, request.mode, input.goal); |
| 67 | authority.checkpoint(); |
| 68 | if (authority.ownsUI()) ports.drainRemote(tabId, ids); |
| 69 | } else await ports.setApproval(tabId, request.mode); |
| 70 | authority.checkpoint(); |
| 71 | } |
| 72 | authority.checkpoint(); |
| 73 | ports.patch(tabId, patch, fields); |
| 74 | } |
| 75 |