Skip to main content

eui_core/host/
mod.rs

1//! # The host API (VAPP-91)
2//!
3//! What a surface needs from its host, the same on all four platforms
4//! (`packages/exponential-ui/catalog/host.json`): the router turning server
5//! messages into ops, the wire decoders (JSONL, SSE, A2UI-over-MCP), the
6//! function / URL / media policy, binding sources, catalog negotiation and
7//! declarative app packages. A line-by-line port of the TypeScript reference
8//! (`packages/exponential-ui/src/host/`) over `serde_json::Value`; every
9//! output is JSON-equal to it, locked by `fixtures/host-*.json`. The I/O
10//! (transports, the runtime host) stays on each platform; the Swift and
11//! Kotlin painters reach this module through `exponential-ui-ffi`.
12
13pub mod contract;
14pub mod decoders;
15pub mod exposes;
16pub mod package;
17pub mod policy;
18pub mod record_store;
19pub mod records;
20pub mod router;
21pub mod signature;
22pub mod sources;
23pub mod tools;
24
25pub use contract::*;
26pub use exposes::{expose_action_access, expose_issues, expose_name_destructive, expose_template_paths, validate_package_exposes, validate_upstreams, ExposeIssue};
27pub use decoders::{decode_jsonl, mcp_action_call, messages_from_mcp_result, DecodeIssue, Decoded, JsonlDecoder, SseDecoder};
28pub use package::{
29    available_extensions, client_capabilities, merge_data, package_sources, source_allowed, supported_catalog_ids, template_messages, validate_package, validate_package_with, version_satisfies, AvailableExtension, PackageIssue,
30    PACKAGE_FORMAT_VERSION,
31};
32pub use policy::{
33    combine_decisions, decide_function, decide_url, image_dimensions, image_frames, image_header, is_builtin_function, is_private_host, media_image_check, ImageHeader, matches_pattern, media_image_within_limits, media_request, media_within_limits, package_policy, svg_dimensions,
34    safe_href, FunctionDecision, FunctionPolicy, MediaOptions, MediaRequest, MediaRule, UrlDecision, UrlPolicy, BUILTIN_FUNCTIONS,
35};
36pub use router::{normalize_path, HostRouter, SurfaceInfo};
37pub use record_store::{parse_records_source, RecordsFilter, RecordsQuery, RECORDS_FUNCTIONS, RECORDS_RESERVED_PARAMS, RECORDS_SCHEME};
38pub use records::{is_record_kind, validate_record, validate_record_schema, RECORD_FIELD_TYPES, RECORD_FORMATS, RECORD_RESERVED_KEYS};
39pub use signature::{canonical_json, package_file_signed_bytes, AUTHOR_KEY_PREFIX, AUTHOR_NAME_MAX, PACKAGE_FILE_SIGNATURE_ALG, PACKAGE_FILE_SIGNATURE_CONTEXT};
40pub use sources::{parse_source, source_with_params, ParsedSource};
41pub use tools::{tool_call, tool_consent, tool_consent_key, validate_tools, ToolCall, ToolConsentRow, TOOL_KINDS};
42
43/// JavaScript's WhiteSpace + LineTerminator (what `trim` drops, regex `\s`).
44pub(crate) fn is_js_space(c: char) -> bool {
45    matches!(c, '\u{9}' | '\u{a}' | '\u{b}' | '\u{c}' | '\u{d}' | ' ' | '\u{a0}' | '\u{1680}' | '\u{2000}'..='\u{200a}' | '\u{2028}' | '\u{2029}' | '\u{202f}' | '\u{205f}' | '\u{3000}' | '\u{feff}')
46}
47
48/// JavaScript's `String.prototype.trim` (WhiteSpace + LineTerminator).
49pub(crate) fn js_trim(s: &str) -> &str {
50    s.trim_matches(is_js_space)
51}
52
53/// JavaScript's default string order (`sort()`, `<`): by UTF-16 code units,
54/// which differs from Rust's byte order above U+FFFF.
55pub(crate) fn utf16_cmp(a: &str, b: &str) -> std::cmp::Ordering {
56    a.encode_utf16().cmp(b.encode_utf16())
57}
58
59/// JavaScript truthiness of a JSON value.
60pub(crate) fn js_truthy(value: &serde_json::Value) -> bool {
61    use serde_json::Value;
62    match value {
63        Value::Null => false,
64        Value::Bool(b) => *b,
65        Value::Number(n) => n.as_f64().is_some_and(|f| f != 0.0 && !f.is_nan()),
66        Value::String(s) => !s.is_empty(),
67        _ => true,
68    }
69}