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 expressions;
17pub mod package;
18pub mod policy;
19pub mod query;
20pub mod record_store;
21pub mod records;
22pub mod router;
23pub mod signature;
24pub mod sources;
25pub mod tools;
26
27pub use contract::*;
28pub use expressions::{literal_type, record_row_type, records_value_type, schema_type, validate_package_expressions};
29pub use exposes::{expose_action_access, expose_issues, expose_name_destructive, expose_template_paths, validate_package_exposes, validate_upstreams, ExposeIssue};
30pub use decoders::{decode_jsonl, mcp_action_call, messages_from_mcp_result, DecodeIssue, Decoded, JsonlDecoder, SseDecoder};
31pub use package::{
32    available_extensions, client_capabilities, merge_data, package_sources, source_allowed, supported_catalog_ids, template_messages, validate_package, validate_package_with, version_satisfies, AvailableExtension, PackageIssue,
33    PACKAGE_FORMAT_VERSION,
34};
35pub use policy::{
36    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,
37    safe_href, FunctionDecision, FunctionPolicy, MediaOptions, MediaRequest, MediaRule, UrlDecision, UrlPolicy, BUILTIN_FUNCTIONS,
38};
39pub use router::{normalize_path, HostRouter, SurfaceInfo};
40pub use query::{
41    expand_query_source, fill_query_templates, month_days, month_label, parse_records_json, parse_records_query, parse_records_source, records_value, records_value_owned, shift_month, RecordRow, RecordRowRef, RecordsAggregate, RecordsAggregateOp, RecordsBefore, RecordsContext, RecordsDay, RecordsFilter, RecordsFilterOp, RecordsFilterValue, RecordsGroupKey, RecordsMonthView, RecordsOrder, RecordsQueryIssue, RecordsRunning, RecordsRunningOp,
42    RecordsQuery, RecordsSortKey, RECORDS_AGGREGATE_OPS, RECORDS_AGGREGATE_RESERVED, RECORDS_FILTER_OPS, RECORDS_MONTHS, RECORDS_OP_ALIASES, RECORDS_RUNNING_OPS, RECORDS_RESERVED_PARAMS, RECORDS_ROW_KEYS, RECORDS_SCHEME, RECORDS_WEEKDAYS,
43};
44pub use record_store::RECORDS_FUNCTIONS;
45pub use records::{is_record_kind, validate_record, validate_record_schema, RECORD_FIELD_TYPES, RECORD_FORMATS, RECORD_RESERVED_KEYS};
46pub use signature::{canonical_json, package_file_signed_bytes, AUTHOR_KEY_PREFIX, AUTHOR_NAME_MAX, PACKAGE_FILE_SIGNATURE_ALG, PACKAGE_FILE_SIGNATURE_CONTEXT};
47pub use sources::{parse_source, source_with_params, ParsedSource};
48pub use tools::{tool_call, tool_consent, tool_consent_key, validate_tools, ToolCall, ToolConsentRow, TOOL_KINDS};
49
50/// JavaScript's WhiteSpace + LineTerminator (what `trim` drops, regex `\s`).
51pub(crate) fn is_js_space(c: char) -> bool {
52    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}')
53}
54
55/// JavaScript's `String.prototype.trim` (WhiteSpace + LineTerminator).
56pub(crate) fn js_trim(s: &str) -> &str {
57    s.trim_matches(is_js_space)
58}
59
60/// JavaScript's default string order (`sort()`, `<`): by UTF-16 code units,
61/// which differs from Rust's byte order above U+FFFF.
62pub(crate) fn utf16_cmp(a: &str, b: &str) -> std::cmp::Ordering {
63    a.encode_utf16().cmp(b.encode_utf16())
64}
65
66/// JavaScript truthiness of a JSON value.
67pub(crate) fn js_truthy(value: &serde_json::Value) -> bool {
68    use serde_json::Value;
69    match value {
70        Value::Null => false,
71        Value::Bool(b) => *b,
72        Value::Number(n) => n.as_f64().is_some_and(|f| f != 0.0 && !f.is_nan()),
73        Value::String(s) => !s.is_empty(),
74        _ => true,
75    }
76}