| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | |
| 8 | "reasonix/internal/control" |
| 9 | ) |
| 10 | |
| 11 | // ExtensionActionView is the JSON twin of control.ExtensionActionView for the |
| 12 | // desktop frontend: one handshake-declared extension UI action. Description |
| 13 | // carries the control view's Label — the protocol declares a single |
| 14 | // human-readable label per action, so the frontend shows it as the action's |
| 15 | // description line. Slash is the public invocation name, "/<plugin>:<action>". |
| 16 | type ExtensionActionView struct { |
| 17 | Plugin string `json:"plugin"` |
| 18 | Action string `json:"action"` |
| 19 | Slash string `json:"slash"` |
| 20 | Description string `json:"description,omitempty"` |
| 21 | } |
| 22 | |
| 23 | // extensionFormSubmitter is the slice of *control.Controller that delivers a |
| 24 | // form surface's values back to the owning sidecar. control.SessionAPI does |
| 25 | // not expose SubmitExtensionForm yet (stage 8a added it to the concrete |
| 26 | // controller only; the Capabilities port carries just ExtensionActions and |
| 27 | // InvokeExtensionAction), so the desktop binding asserts it locally, mirroring |
| 28 | // cancelJobForController's CancelJob assertion. If the port grows the method, |
| 29 | // this alias can go away. |
| 30 | type extensionFormSubmitter interface { |
| 31 | SubmitExtensionForm(ctx context.Context, pluginID, surfaceID string, values map[string]any) error |
| 32 | } |
| 33 | |
| 34 | type exactExtensionFormSubmitter interface { |
| 35 | SubmitExtensionFormExact(ctx context.Context, pluginID, surfaceID string, generation uint64, formInstanceID string, values map[string]any) error |
| 36 | } |
| 37 | |
| 38 | type ExtensionFormTarget struct { |
| 39 | TabID string `json:"tabId"` |
| 40 | HostID string `json:"hostId"` |
| 41 | SessionID string `json:"sessionId"` |
| 42 | SessionGeneration uint64 `json:"sessionGeneration"` |
| 43 | PluginID string `json:"pluginId"` |
| 44 | SurfaceID string `json:"surfaceId"` |
| 45 | PluginGeneration uint64 `json:"pluginGeneration"` |
| 46 | FormInstanceID string `json:"formInstanceId"` |
| 47 | } |
| 48 | |
| 49 | // extensionActionsForCtrl maps the control port's action views to their JSON |
| 50 | // twins. Nil controller → empty, so the palette simply shows no extension |
| 51 | // group while a tab's runtime is still starting. |
| 52 | func extensionActionsForCtrl(ctrl control.SessionAPI) []ExtensionActionView { |
| 53 | out := []ExtensionActionView{} |
| 54 | if ctrl == nil { |
| 55 | return out |
| 56 | } |
| 57 | for _, action := range ctrl.ExtensionActions() { |
| 58 | out = append(out, ExtensionActionView{ |
| 59 | Plugin: action.PluginID, |
| 60 | Action: action.ActionID, |
| 61 | Slash: action.Slash, |
| 62 | Description: action.Label, |
| 63 | }) |
| 64 | } |
| 65 | return out |
| 66 | } |
| 67 | |
| 68 | // ExtensionActions enumerates the handshake-declared extension UI actions of |
| 69 | // the runtime owning tabID (empty tabID = active tab). Unknown tabs and tabs |
| 70 | // without a running runtime yield an empty list rather than an error so the |
| 71 | // command palette can poll freely. |
| 72 | func (a *App) ExtensionActions(tabID string) ([]ExtensionActionView, error) { |
| 73 | return extensionActionsForCtrl(a.ctrlByTabID(tabID)), nil |
| 74 | } |
| 75 | |
| 76 | // extensionCtrlForTab resolves the driving controller for extension calls, |
| 77 | // erroring instead of silently no-opping (unlike the read path above, invoke |
| 78 | // and submit are user-initiated mutations that must surface misrouting). |
| 79 | func (a *App) extensionCtrlForTab(tabID string) (control.SessionAPI, error) { |
| 80 | if a.tabByID(tabID) == nil { |
| 81 | return nil, fmt.Errorf("no tab %q", tabID) |
| 82 | } |
| 83 | ctrl := a.ctrlByTabID(tabID) |
| 84 | if ctrl == nil { |
| 85 | return nil, fmt.Errorf("tab %q has no running runtime", tabID) |
| 86 | } |
| 87 | return ctrl, nil |
| 88 | } |
| 89 | |
| 90 | // InvokeExtensionAction invokes one registered extension action by its public |
| 91 | // "/<plugin>:<action>" name on behalf of tabID (empty = active tab) and |
| 92 | // returns the extension's (already redacted) result message. |
| 93 | func (a *App) InvokeExtensionAction(tabID, name string, args map[string]string) (string, error) { |
| 94 | if strings.TrimSpace(name) == "" { |
| 95 | return "", fmt.Errorf("extension action name is required") |
| 96 | } |
| 97 | ctrl, err := a.extensionCtrlForTab(tabID) |
| 98 | if err != nil { |
| 99 | return "", err |
| 100 | } |
| 101 | return ctrl.InvokeExtensionAction(a.bootContext(), name, args) |
| 102 | } |
| 103 | |
| 104 | // SubmitExtensionForm delivers one extension form surface's values back to the |
| 105 | // owning sidecar. A dismissal rides the same path as values{"cancelled": true} |
| 106 | // — the structured-UI protocol has no dedicated cancel verb on the submit |
| 107 | // channel, so the frontend reports cancellation as an explicit value, distinct |
| 108 | // from an empty value set. |
| 109 | func (a *App) SubmitExtensionForm(tabID, pluginID, surfaceID string, values map[string]any) error { |
| 110 | if strings.TrimSpace(pluginID) == "" || strings.TrimSpace(surfaceID) == "" { |
| 111 | return fmt.Errorf("extension form submission requires plugin and surface ids") |
| 112 | } |
| 113 | ctrl, err := a.extensionCtrlForTab(tabID) |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | submitter, ok := ctrl.(extensionFormSubmitter) |
| 118 | if !ok { |
| 119 | return fmt.Errorf("extension form submission is unavailable on this runtime") |
| 120 | } |
| 121 | return submitter.SubmitExtensionForm(a.bootContext(), pluginID, surfaceID, values) |
| 122 | } |
| 123 | |
| 124 | func (a *App) SubmitExtensionFormExact(target ExtensionFormTarget, values map[string]any) error { |
| 125 | if strings.TrimSpace(target.TabID) == "" || strings.TrimSpace(target.PluginID) == "" || |
| 126 | strings.TrimSpace(target.SurfaceID) == "" || strings.TrimSpace(target.FormInstanceID) == "" { |
| 127 | return fmt.Errorf("exact extension form identity is required") |
| 128 | } |
| 129 | if target.HostID != "" && target.HostID != localDesktopHostID { |
| 130 | return fmt.Errorf("extension form host binding is stale") |
| 131 | } |
| 132 | a.mu.RLock() |
| 133 | tab := a.tabByIDLocked(target.TabID) |
| 134 | if tab == nil || tab.SessionID != target.SessionID || tab.SessionGeneration != target.SessionGeneration { |
| 135 | a.mu.RUnlock() |
| 136 | return fmt.Errorf("extension form binding is stale") |
| 137 | } |
| 138 | ctrl := tab.Ctrl |
| 139 | a.mu.RUnlock() |
| 140 | submitter, ok := ctrl.(exactExtensionFormSubmitter) |
| 141 | if !ok { |
| 142 | return fmt.Errorf("exact extension form submission is unavailable on this runtime") |
| 143 | } |
| 144 | return submitter.SubmitExtensionFormExact(a.bootContext(), target.PluginID, target.SurfaceID, |
| 145 | target.PluginGeneration, target.FormInstanceID, values) |
| 146 | } |
| 147 |