import type { AppCommand } from "../commands.js"; import type { WhistleExecutionOrigin, WhistleJsonValue, } from "@fungi.computer/whistle"; import type { AppContext, AppIdentity, AppLifecycle, FocusState, SetupNeed, SetupOutcome, } from "../lifecycle.js"; import type { AppBrowserAction, AppBrowserActionOutcome, AgentSessionSelection, AppPermissionOutcome, AppPermissionRequest, AppStatusAction, AppStatusItem, FileNavigationInput, ToastInput, } from "../ui.js"; import type { APP_NAVIGATION_CONTRACT_VERSION, AppNavigationActivation, AppNavigationState, } from "../window.js"; import type { AppErrorData } from "./app-error.js"; import type { RpcResult } from "./result.js"; import type { WhistleFailure } from "@fungi.computer/whistle"; /** Current App protocol. Version 3 adds computerName to the strict context wire. */ export const APP_PROTOCOL_VERSION = 3 as const; /** Published Apps and self-hosted Oysters can independently remain on protocol 2. */ export const APP_PROTOCOL_VERSIONS = [2, APP_PROTOCOL_VERSION] as const; /** A protocol version supported by both the App and host adapters. */ export type AppProtocolVersion = (typeof APP_PROTOCOL_VERSIONS)[number]; /** Old hosts ignore this tag rather than reject a v3 value in their strict v2 message. */ export const APP_READY_TYPE = "fungi.app.ready.v3" as const; /** The exact readiness tag understood by published protocol-2 hosts. */ export const APP_READY_V2_TYPE = "fungi.app.ready" as const; /** Message type the host posts to hand the App its RPC MessagePort. */ export const APP_CONNECT_TYPE = "fungi.app.connect" as const; export type AppClientRpc = { disconnect(error?: AppErrorData): Promise>; requestClose(): Promise>; /** Ask the App to go back one step in its own history; `true` when it did. */ requestBack(): Promise>; /** Ask the App to go forward one step in its own history; `true` when it did. */ requestForward(): Promise>; /** The person used a crumb or header command; `true` when the App acted. */ activateNavigation( activation: AppNavigationActivation, ): Promise>; /** The person used one of the App's status items; `true` when it acted. */ activateStatus(action: AppStatusAction): Promise>; updateContext(context: VersionedFact): Promise>; reconfigure(value: AppHostReconfigurationWire): Promise>; updateLifecycle( lifecycle: VersionedFact, ): Promise>; updateReservedChords( chords: VersionedFact, ): Promise>; updateTheme(theme: VersionedFact): Promise>; }; /** The host's handshake message that hands the App its RPC MessagePort. */ export type AppConnectMessage = { readonly epoch: string; readonly protocolVersion: AppProtocolVersion; readonly type: typeof APP_CONNECT_TYPE; }; export type AppHostRpc = { requestComputerChoice(): Promise>; requestBrowserAction( input: AppBrowserAction, ): Promise>; connect(request: ConnectRequestWire): Promise>; invokeReservedChord(id: string, repeat: boolean): Promise>; openNavigation(input: FileNavigationInput): Promise>; openTeamBilling(): Promise>; openDesktopSettings(): Promise>; openSetup(need: SetupNeed): Promise>; requestAppPermissions( input: AppPermissionRequest, ): Promise>; registerCommand( command: AppCommand, handler: CommandHandlerRpc, ): Promise>; reportFocus(focus: FocusState): Promise>; reportNavigation(state: NavigationStateWire): Promise>; reportStatus(items: readonly AppStatusItem[]): Promise>; reportAgentSessionSelection( selection: AgentSessionSelection | null, ): Promise>; showToast(input: ToastInput): Promise>; unregisterCommand(commandId: string): Promise>; }; /** The versioned navigation contract an App reports to its host. */ export type NavigationStateWire = Readonly<{ contractVersion: typeof APP_NAVIGATION_CONTRACT_VERSION; }> & AppNavigationState; /** The App iframe's handshake message announcing it is ready to connect. */ export type AppReadyMessage = | { readonly protocolVersion: typeof APP_PROTOCOL_VERSION; readonly type: typeof APP_READY_TYPE; } | { readonly protocolVersion: 2; readonly type: typeof APP_READY_V2_TYPE; }; /** The handle is deliberately opaque: capability owners define its methods. */ export type CapabilityGrantWire = { readonly handle: object; readonly id: string; readonly version: number; }; export type CommandHandlerRpc = { cancel(requestId: string): Promise>; invoke( invocation: CommandInvocationWire, ): Promise>; }; /** Data-only command outcome crossing the App boundary. */ export type CommandInvocationResultWire = | Readonly<{ readonly status: "handled"; readonly result?: WhistleJsonValue; }> | Readonly<{ readonly status: "failed"; readonly error: WhistleFailure; }>; export type CommandInvocationWire = { readonly arguments?: WhistleJsonValue; readonly commandId: string; readonly origin: WhistleExecutionOrigin; readonly requestId: string; }; export type ConnectRequestWire = { readonly epoch: string; readonly protocolVersion: AppProtocolVersion; }; export type HostBootstrapWire = { readonly capabilities: readonly CapabilityGrantWire[]; readonly context: VersionedFact; readonly identity: AppIdentity; readonly lifecycle: VersionedFact; readonly reservedChords: VersionedFact; readonly theme: VersionedFact; }; export type AppHostReconfigurationWire = { readonly capabilities: readonly CapabilityGrantWire[]; readonly context: VersionedFact; }; export type RegisteredCommandWire = { readonly commandId: string; }; export type ReservedChord = { readonly altKey: boolean; readonly ctrlKey: boolean; readonly id: string; readonly key: string; readonly metaKey: boolean; readonly shiftKey: boolean; }; export type VersionedFact = { readonly value: T; readonly version: number; };