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 actions;
14pub mod components;
15pub mod contract;
16pub mod decoders;
17pub mod exposes;
18pub mod expressions;
19pub mod package;
20pub mod policy;
21pub mod query;
22pub mod record_migrations;
23pub mod record_policy;
24pub mod record_transitions;
25pub mod record_store;
26pub mod records;
27pub mod router;
28pub mod signature;
29pub mod sources;
30pub mod tests;
31pub mod tools;
32
33pub use actions::{
34    action_call_name, action_var_types, apply_record_batch, apply_record_batch_as, function_effect, is_action_name, run_action, validate_package_actions, validate_package_effects, ActionContext, ACTION_CALL_PREFIX, ACTION_LIMIT_ACTIONS, ACTION_LIMIT_MAX_STEPS, ACTION_LIMIT_STEPS, ACTION_NAME_PATTERN,
35    ACTION_STEP_KINDS, RECORDS_BATCH_FUNCTION,
36};
37pub use components::validate_package_components;
38pub use contract::*;
39pub use expressions::{literal_type, record_row_type, records_value_type, schema_type, validate_package_expressions, validate_record_computed};
40pub use exposes::{expose_action_access, expose_issues, expose_name_destructive, expose_template_paths, validate_package_exposes, validate_upstreams, ExposeIssue};
41pub use decoders::{decode_jsonl, mcp_action_call, messages_from_mcp_result, DecodeIssue, Decoded, JsonlDecoder, SseDecoder};
42pub use package::{
43    available_extensions, client_capabilities, merge_data, package_sources, source_allowed, supported_catalog_ids, template_messages, validate_package, validate_package_in, validate_package_with, version_satisfies, AvailableExtension, PackageIssue,
44    PACKAGE_FORMAT_VERSION,
45};
46pub use policy::{
47    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,
48    safe_href, FunctionDecision, FunctionPolicy, MediaOptions, MediaRequest, MediaRule, UrlDecision, UrlPolicy, BUILTIN_FUNCTIONS,
49};
50pub use router::{normalize_path, HostRouter, SurfaceInfo};
51pub use query::{
52    computed_data, expand_query_source, fill_query_templates, kind_computed, kind_refs, 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,
53    RecordsQuery, RecordsSortKey, RECORDS_AGGREGATE_OPS, RECORDS_AGGREGATE_RESERVED, RECORDS_COMPUTED_MAX, RECORDS_COMPUTED_ROW_KEYS, RECORDS_FILTER_OPS, RECORDS_MONTHS, RECORDS_OP_ALIASES, RECORDS_RUNNING_OPS, RECORDS_RESERVED_PARAMS, RECORDS_ROW_KEYS, RECORDS_SCHEME, RECORDS_WEEKDAYS,
54};
55pub use record_migrations::{is_version_range, kind_changes, kind_changes_json, migrate_record, migration_refusal, package_migrations, range_holds, validate_migrations, KindChange, MigrateResult, MIGRATION_OP_NAMES, RECORD_MIGRATIONS_MAX, RECORD_MIGRATION_OPS_MAX};
56pub use record_transitions::{initial_states, kind_transitions, transition_refusal, transitions_from, validate_record_transitions, RecordTransitions, TransitionRefusal, TransitionViewer, RECORD_TRANSITIONS_ROW_KEY, TRANSITION_LEVELS};
57pub use record_policy::{
58    readable_rows, record_allowed, record_author_of, record_policy, record_policy_copy, record_refusal, validate_record_policy, RecordOp, RecordPolicy, RecordViewer, APP_ACCESS_LEVELS, DEFAULT_RECORD_POLICY, RECORD_POLICY_LEVELS, RECORD_POLICY_OPS,
59};
60pub use record_store::RECORDS_FUNCTIONS;
61pub use records::{is_record_kind, record_attachment_fields, record_attachment_id, record_attachment_ids, validate_record, validate_record_refs, validate_record_schema, RECORD_FIELD_TYPES, RECORD_FORMATS, RECORD_RESERVED_KEYS};
62pub use signature::{canonical_json, package_file_signed_bytes, AUTHOR_KEY_PREFIX, AUTHOR_NAME_MAX, PACKAGE_FILE_SIGNATURE_ALG, PACKAGE_FILE_SIGNATURE_CONTEXT};
63pub use sources::{parse_source, source_with_params, ParsedSource};
64pub use tests::{
65    package_test_issues, run_package_tests, test_failure, test_matches, validate_package_tests, PackageTestResult, PACKAGE_TEST_DEFAULT_AS, PACKAGE_TEST_DEFAULT_NOW, PACKAGE_TEST_DEFAULT_ROLE, PACKAGE_TEST_ROLES, PACKAGE_TEST_KINDS, PACKAGE_TEST_LIMIT_GOT_MAX, PACKAGE_TEST_LIMIT_TESTS,
66};
67pub use tools::{tool_call, tool_consent, tool_consent_key, validate_tools, ToolCall, ToolConsentRow, TOOL_KINDS};
68
69/// JavaScript's WhiteSpace + LineTerminator (what `trim` drops, regex `\s`).
70pub(crate) fn is_js_space(c: char) -> bool {
71    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}')
72}
73
74/// JavaScript's `String.prototype.trim` (WhiteSpace + LineTerminator).
75pub(crate) fn js_trim(s: &str) -> &str {
76    s.trim_matches(is_js_space)
77}
78
79/// JavaScript's default string order (`sort()`, `<`): by UTF-16 code units,
80/// which differs from Rust's byte order above U+FFFF.
81/// VAPP-202: when either side is ASCII the first difference is an ASCII
82/// byte against a byte of the same or a later char, where byte order and
83/// UTF-16 order agree, so the bytes compare directly.
84pub(crate) fn utf16_cmp(a: &str, b: &str) -> std::cmp::Ordering {
85    if a.is_ascii() || b.is_ascii() {
86        return a.as_bytes().cmp(b.as_bytes());
87    }
88    a.encode_utf16().cmp(b.encode_utf16())
89}
90
91/// JavaScript truthiness of a JSON value.
92pub(crate) fn js_truthy(value: &serde_json::Value) -> bool {
93    use serde_json::Value;
94    match value {
95        Value::Null => false,
96        Value::Bool(b) => *b,
97        Value::Number(n) => n.as_f64().is_some_and(|f| f != 0.0 && !f.is_nan()),
98        Value::String(s) => !s.is_empty(),
99        _ => true,
100    }
101}