/** * The surface door: the one UI and diagnostic port for script results. Print * and submitForm effects from the actions plane never reach it (the * JavaScript executor extracts them and routes them through * `firePrintThroughAdapter` and `performSubmit` after the transaction, since * the /WP and /DP wrap and the post-commit dataset must run outside the host * transaction). Such effects here come from the form pipeline's K/V/C/F path; * a print goes to the kernel's print when the host gave a printer. */ import type { ActionOrigin, ActionSource } from '../contract'; import type { ActionsHostCapability, ScriptCommitSurface, ScriptRealmKind, ScriptSurfaceResult, } from '../host-contract'; import type { ActionsServices } from '../services'; import { intentOfSubmitEffect } from '../submit/intent'; import type { ActionsSubmit } from '../submit/perform'; export function createScriptSurface( services: Pick< ActionsServices, 'events' | 'ports' | 'authority' | 'documentPrint' | 'printLatch' >, { performSubmit }: ActionsSubmit, ) { const { reportDiagnostic, scriptDiagnosticReported, scriptFailed } = services.events; const ports = services.ports.slots; const { allowsPrint } = services.authority; const { documentPrint } = services; const printLatch = services.printLatch; const surfaceScriptResult = (result: ScriptSurfaceResult): void => { const uiContext = { origin: result.origin, phase: result.phase }; const source = result.source ?? null; for (const effect of result.uiEffects) { // A detached realm has no document surface: the document it scripted // is not displayed, and this door's print, goto and submit act on this // document. Only alerts have a valid target (the user); everything // else is suppressed observably, never routed to the wrong document. if (result.realm === 'detached' && effect.type !== 'alert') { scriptDiagnosticReported.emit({ code: 'ui-effect-suppressed', message: `script ${effect.type} request from a detached realm suppressed: no document surface`, }); continue; } if (effect.type === 'submitForm') { // A form-pipeline scripted submit: the same door, not awaited (the // form queue must not wait on submit sinks). Policy, sinks and // diagnostics all live inside performSubmit. void performSubmit( intentOfSubmitEffect(effect), { origin: result.origin, source: result.source ?? { kind: 'api' }, event: { type: 'activate' }, }, (diagnostic) => reportDiagnostic(diagnostic, { action: 'javascript', source }), ); continue; } // Permission, not preference: a print request without doc.print // authority reaches no adapter; not overridable, and observable. if (effect.type === 'print' && !allowsPrint()) { scriptDiagnosticReported.emit({ code: 'ui-effect-suppressed', message: 'script print request withheld: doc.print is not allowed', }); continue; } // The print latch: a /WP or /DP script (or anything running while a // print wrapper is active) printing again is suppressed, so there is // one dialog per outer request. if (effect.type === 'print' && printLatch.active) { scriptDiagnosticReported.emit({ code: 'ui-effect-suppressed', message: 'script print request during a document print event — suppressed (reentrant)', }); continue; } // With the kernel's print, a user's print request prints the document, and the kernel // runs /WP and /DP around it. A hover or lifecycle request never prints. if (effect.type === 'print' && documentPrint.available()) { if (result.origin !== 'user') { scriptDiagnosticReported.emit({ code: 'ui-effect-suppressed', message: `script print request from a ${result.origin} event suppressed`, }); continue; } documentPrint.start().catch((error: unknown) => reportDiagnostic( { code: 'trigger-failed', message: `print: ${error instanceof Error ? error.message : String(error)}`, }, { action: 'javascript', source }, ), ); continue; } // The cursor into a field is the form plugin's, not the app's UI. if (effect.type === 'focusField') { if (!ports.formFocus?.(effect.field)) { scriptDiagnosticReported.emit({ code: 'ui-effect-suppressed', message: `script setFocus on "${effect.field}" withheld: no such field to fill in on screen`, }); } continue; } if (!ports.uiAdapter) { reportDiagnostic( { code: 'no-adapter', message: `script ${effect.type}: no UI adapter installed` }, { action: 'javascript', source }, ); continue; } if (effect.type === 'alert') { ports.uiAdapter.alert?.(effect.message, { ...uiContext, icon: effect.icon, ...(effect.title !== undefined ? { title: effect.title } : {}), }); } else if (effect.type === 'goToPage') { ports.uiAdapter.goToPage?.(effect.pageIndex, uiContext); } else { ports.uiAdapter.print(uiContext); } } for (const diagnostic of result.diagnostics) scriptDiagnosticReported.emit(diagnostic); if (result.error) scriptFailed.emit({ error: result.error, source }); }; /** A K/V/C/F commit surfaces as up to two results: the boot phase (only * when it produced effects) and the user phase (always — it carries the * diagnostics and the error). */ const surfaceScriptCommit = ( commit: ScriptCommitSurface, context: { origin: ActionOrigin; realm: ScriptRealmKind; source?: ActionSource }, ): void => { const phases: Array<'boot' | 'user'> = ['boot', 'user']; for (const phase of phases) { const uiEffects = commit.uiEffects.filter((effect) => effect.phase === phase); if (uiEffects.length === 0 && phase === 'boot') continue; surfaceScriptResult({ uiEffects, diagnostics: phase === 'user' ? commit.diagnostics : [], ...(phase === 'user' && commit.error ? { error: commit.error } : {}), origin: context.origin, phase, realm: context.realm, ...(context.source ? { source: context.source } : {}), }); } }; return { surfaceScriptResult, api: { surfaceScriptResult, surfaceScriptCommit } satisfies Partial, }; } export type ActionsScriptSurface = ReturnType;