| 1 | import { useCommittedCommand } from "./useCommittedCommand"; |
| 2 | import { executeComposerMode, type ComposerModePorts, type ComposerModeRequest } from "../app-runtime/composerModeOwner"; |
| 3 | import { updateUserPlanModeIntent, type UserPlanModeIntents } from "./composerProfile"; |
| 4 | import type { SessionResource, useSessionOperations } from "../app-runtime/useSessionOperations"; |
| 5 | import type { CollaborationMode, Mode, ToolApprovalMode } from "./types"; |
| 6 | |
| 7 | type ComposerModeActionsOptions = { |
| 8 | target: SessionResource; |
| 9 | remote: boolean; |
| 10 | collaborationMode: CollaborationMode; |
| 11 | toolApprovalMode: ToolApprovalMode; |
| 12 | goal: string; |
| 13 | operations: ReturnType<typeof useSessionOperations>; |
| 14 | ports: Omit<ComposerModePorts, "rememberPlan">; |
| 15 | planIntentsRef: { current: UserPlanModeIntents }; |
| 16 | showError: (message: string) => void; |
| 17 | }; |
| 18 | |
| 19 | /** Display inputs commit here; source-bound execution lives outside React. */ |
| 20 | export function useComposerModeActions(options: ComposerModeActionsOptions) { |
| 21 | const notePlanModeForTab = useCommittedCommand((tabId: string, enabled: boolean) => { |
| 22 | options.planIntentsRef.current = updateUserPlanModeIntent(options.planIntentsRef.current, tabId, enabled); |
| 23 | }); |
| 24 | const run = useCommittedCommand(async (request: ComposerModeRequest): Promise<void> => { |
| 25 | const { target, remote, collaborationMode, toolApprovalMode, goal, operations } = options; |
| 26 | // All axes share the backend profile transaction; stop/send have other channels. |
| 27 | const ports: ComposerModePorts = { |
| 28 | ...options.ports, |
| 29 | rememberPlan: notePlanModeForTab, |
| 30 | }; |
| 31 | const result = await operations(target, "composer-profile", { |
| 32 | target, remote, collaborationMode, toolApprovalMode, goal, ports, request, |
| 33 | }, executeComposerMode); |
| 34 | if (result.status === "failed") throw result.error; |
| 35 | }); |
| 36 | const report = useCommittedCommand((error: unknown) => { |
| 37 | options.showError(error instanceof Error ? error.message : String(error)); |
| 38 | }); |
| 39 | const applyMode = useCommittedCommand((mode: Mode) => { void run({ kind: "mode", mode }).catch(report); }); |
| 40 | const applyCollaborationMode = useCommittedCommand((mode: CollaborationMode) => run({ kind: "collaboration", mode })); |
| 41 | const applyToolApprovalMode = useCommittedCommand((mode: ToolApprovalMode) => { void run({ kind: "approval", mode }).catch(report); }); |
| 42 | |
| 43 | return { applyMode, applyCollaborationMode, applyToolApprovalMode, notePlanModeForTab }; |
| 44 | } |
| 45 |