|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | +// |
| 4 | +// Gossamer_Core.res — Core Gossamer invoke and command bindings. |
| 5 | +// |
| 6 | +// Provides the primary IPC bridge to the Gossamer runtime via |
| 7 | +// `window.__gossamer_invoke`. This is the Gossamer equivalent of |
| 8 | +// Tauri_Core.res (which wraps @tauri-apps/api/core). |
| 9 | +// |
| 10 | +// All Gossamer commands are dispatched through a single invoke function |
| 11 | +// injected by gossamer_channel_open() into the webview global scope. |
| 12 | + |
| 13 | +open RescriptCore |
| 14 | + |
| 15 | +// ============================================================================ |
| 16 | +// Types |
| 17 | +// ============================================================================ |
| 18 | + |
| 19 | +/// Invoke arguments — any JSON-serializable value. |
| 20 | +type invokeArgs = JSON.t |
| 21 | + |
| 22 | +/// Invoke options for customizing command calls. |
| 23 | +type invokeOptions = { |
| 24 | + timeout?: int, |
| 25 | + retries?: int, |
| 26 | +} |
| 27 | + |
| 28 | +/// Permission state for capability requests. |
| 29 | +type permissionState = |
| 30 | + | @as("granted") Granted |
| 31 | + | @as("denied") Denied |
| 32 | + | @as("prompt") Prompt |
| 33 | + |
| 34 | +// ============================================================================ |
| 35 | +// Runtime Detection |
| 36 | +// ============================================================================ |
| 37 | + |
| 38 | +/// Check whether the Gossamer runtime is available in the current context. |
| 39 | +%%raw(` |
| 40 | +function isGossamerAvailable() { |
| 41 | + return typeof window !== 'undefined' |
| 42 | + && typeof window.__gossamer_invoke === 'function'; |
| 43 | +} |
| 44 | +`) |
| 45 | +@val external isGossamerAvailable: unit => bool = "isGossamerAvailable" |
| 46 | + |
| 47 | +// ============================================================================ |
| 48 | +// Core Invoke API |
| 49 | +// ============================================================================ |
| 50 | + |
| 51 | +/// Raw invoke binding — calls window.__gossamer_invoke(cmd, args). |
| 52 | +/// This is the foundation for all Gossamer IPC. |
| 53 | +%%raw(` |
| 54 | +function gossamerInvokeRaw(cmd, args) { |
| 55 | + if (typeof window !== 'undefined' && typeof window.__gossamer_invoke === 'function') { |
| 56 | + return window.__gossamer_invoke(cmd, args); |
| 57 | + } |
| 58 | + return Promise.reject(new Error('Gossamer runtime not available')); |
| 59 | +} |
| 60 | +`) |
| 61 | +@val external invokeRaw: (string, 'a) => promise<'b> = "gossamerInvokeRaw" |
| 62 | + |
| 63 | +/// Invoke a Gossamer command with typed arguments and response. |
| 64 | +/// Commands are defined in the Gossamer backend (Rust/Zig) and |
| 65 | +/// exposed via the IPC channel. |
| 66 | +let invoke = (command: string, ~args: invokeArgs=?): promise<'response> => { |
| 67 | + let finalArgs = switch args { |
| 68 | + | Some(a) => a |
| 69 | + | None => Obj.magic(Dict.make()) |
| 70 | + } |
| 71 | + invokeRaw(command, finalArgs) |
| 72 | +} |
| 73 | + |
| 74 | +/// Invoke a command with automatic JSON serialization/deserialization. |
| 75 | +/// Use this when you want type safety on both request and response. |
| 76 | +let invokeTyped = async ( |
| 77 | + ~command: string, |
| 78 | + ~args: 'args, |
| 79 | + ~decoder: JSON.t => result<'response, string>, |
| 80 | +): result<'response, string> => { |
| 81 | + try { |
| 82 | + let response = await invokeRaw(command, args) |
| 83 | + decoder(Obj.magic(response)) |
| 84 | + } catch { |
| 85 | + | Exn.Error(err) => |
| 86 | + Error(Exn.message(err)->Option.getOr("Unknown Gossamer invoke error")) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// ============================================================================ |
| 91 | +// App Info API |
| 92 | +// ============================================================================ |
| 93 | + |
| 94 | +/// Get the application name from the Gossamer runtime. |
| 95 | +let getName = (): promise<string> => { |
| 96 | + invokeRaw("__gossamer_app_get_name", {}) |
| 97 | +} |
| 98 | + |
| 99 | +/// Get the application version from the Gossamer runtime. |
| 100 | +let getVersion = (): promise<string> => { |
| 101 | + invokeRaw("__gossamer_app_get_version", {}) |
| 102 | +} |
| 103 | + |
| 104 | +/// Get the Gossamer runtime version. |
| 105 | +let getGossamerVersion = (): promise<string> => { |
| 106 | + invokeRaw("__gossamer_app_get_runtime_version", {}) |
| 107 | +} |
| 108 | + |
| 109 | +/// Show the application window. |
| 110 | +let show = (): promise<unit> => { |
| 111 | + invokeRaw("__gossamer_app_show", {}) |
| 112 | +} |
| 113 | + |
| 114 | +/// Hide the application window. |
| 115 | +let hide = (): promise<unit> => { |
| 116 | + invokeRaw("__gossamer_app_hide", {}) |
| 117 | +} |
| 118 | + |
| 119 | +// ============================================================================ |
| 120 | +// Capability API |
| 121 | +// ============================================================================ |
| 122 | + |
| 123 | +/// Check if a capability is available. |
| 124 | +let hasCapability = (capability: string): promise<bool> => { |
| 125 | + invokeRaw("__gossamer_capability_check", {"capability": capability}) |
| 126 | +} |
| 127 | + |
| 128 | +/// Request a capability from the runtime. |
| 129 | +let requestCapability = (capability: string): promise<permissionState> => { |
| 130 | + invokeRaw("__gossamer_capability_request", {"capability": capability}) |
| 131 | +} |
0 commit comments