| 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 | // extensionActionsForCtrl maps the control port's action views to their JSON |
| 35 | // twins. Nil controller → empty, so the palette simply shows no extension |
| 36 | // group while a tab's runtime is still starting. |
| 37 | func extensionActionsForCtrl(ctrl control.SessionAPI) []ExtensionActionView { |
| 38 | out := []ExtensionActionView{} |
| 39 | if ctrl == nil { |
| 40 | return out |
| 41 | } |
| 42 | for _, action := range ctrl.ExtensionActions() { |
| 43 | out = append(out, ExtensionActionView{ |
| 44 | Plugin: action.PluginID, |
| 45 | Action: action.ActionID, |
| 46 | Slash: action.Slash, |
| 47 | Description: action.Label, |
| 48 | }) |
| 49 | } |
| 50 | return out |
| 51 | } |
| 52 | |
| 53 | // ExtensionActions enumerates the handshake-declared extension UI actions of |
| 54 | // the runtime owning tabID (empty tabID = active tab). Unknown tabs and tabs |
| 55 | // without a running runtime yield an empty list rather than an error so the |
| 56 | // command palette can poll freely. |
| 57 | func (a *App) ExtensionActions(tabID string) ([]ExtensionActionView, error) { |
| 58 | return extensionActionsForCtrl(a.ctrlByTabID(tabID)), nil |
| 59 | } |
| 60 | |
| 61 | // extensionCtrlForTab resolves the driving controller for extension calls, |
| 62 | // erroring instead of silently no-opping (unlike the read path above, invoke |
| 63 | // and submit are user-initiated mutations that must surface misrouting). |
| 64 | func (a *App) extensionCtrlForTab(tabID string) (control.SessionAPI, error) { |
| 65 | if a.tabByID(tabID) == nil { |
| 66 | return nil, fmt.Errorf("no tab %q", tabID) |
| 67 | } |
| 68 | ctrl := a.ctrlByTabID(tabID) |
| 69 | if ctrl == nil { |
| 70 | return nil, fmt.Errorf("tab %q has no running runtime", tabID) |
| 71 | } |
| 72 | return ctrl, nil |
| 73 | } |
| 74 | |
| 75 | // InvokeExtensionAction invokes one registered extension action by its public |
| 76 | // "/<plugin>:<action>" name on behalf of tabID (empty = active tab) and |
| 77 | // returns the extension's (already redacted) result message. |
| 78 | func (a *App) InvokeExtensionAction(tabID, name string, args map[string]string) (string, error) { |
| 79 | if strings.TrimSpace(name) == "" { |
| 80 | return "", fmt.Errorf("extension action name is required") |
| 81 | } |
| 82 | ctrl, err := a.extensionCtrlForTab(tabID) |
| 83 | if err != nil { |
| 84 | return "", err |
| 85 | } |
| 86 | return ctrl.InvokeExtensionAction(a.bootContext(), name, args) |
| 87 | } |
| 88 | |
| 89 | // SubmitExtensionForm delivers one extension form surface's values back to the |
| 90 | // owning sidecar. A dismissal rides the same path as values{"cancelled": true} |
| 91 | // — the structured-UI protocol has no dedicated cancel verb on the submit |
| 92 | // channel, so the frontend reports cancellation as an explicit value, distinct |
| 93 | // from an empty value set. |
| 94 | func (a *App) SubmitExtensionForm(tabID, pluginID, surfaceID string, values map[string]any) error { |
| 95 | if strings.TrimSpace(pluginID) == "" || strings.TrimSpace(surfaceID) == "" { |
| 96 | return fmt.Errorf("extension form submission requires plugin and surface ids") |
| 97 | } |
| 98 | ctrl, err := a.extensionCtrlForTab(tabID) |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | submitter, ok := ctrl.(extensionFormSubmitter) |
| 103 | if !ok { |
| 104 | return fmt.Errorf("extension form submission is unavailable on this runtime") |
| 105 | } |
| 106 | return submitter.SubmitExtensionForm(a.bootContext(), pluginID, surfaceID, values) |
| 107 | } |
| 108 |