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_policy;
23pub mod record_store;
24pub mod records;
25pub mod router;
26pub mod signature;
27pub mod sources;
28pub mod tests;
29pub mod tools;
30
31pub use actions::{
32    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,
33    ACTION_STEP_KINDS, RECORDS_BATCH_FUNCTION,
34};
35pub use components::validate_package_components;
36pub use contract::*;
37pub use expressions::{literal_type, record_row_type, records_value_type, schema_type, validate_package_expressions, validate_record_computed};
38pub use exposes::{expose_action_access, expose_issues, expose_name_destructive, expose_template_paths, validate_package_exposes, validate_upstreams, ExposeIssue};
39pub use decoders::{decode_jsonl, mcp_action_call, messages_from_mcp_result, DecodeIssue, Decoded, JsonlDecoder, SseDecoder};
40pub use package::{
41    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,
42    PACKAGE_FORMAT_VERSION,
43};
44pub use policy::{
45    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,
46    safe_href, FunctionDecision, FunctionPolicy, MediaOptions, MediaRequest, MediaRule, UrlDecision, UrlPolicy, BUILTIN_FUNCTIONS,
47};
48pub use router::{normalize_path, HostRouter, SurfaceInfo};
49pub use query::{
50    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,
51    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,
52};
53pub use record_policy::{
54    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,
55};
56pub use record_store::RECORDS_FUNCTIONS;
57pub 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};
58pub use signature::{canonical_json, package_file_signed_bytes, AUTHOR_KEY_PREFIX, AUTHOR_NAME_MAX, PACKAGE_FILE_SIGNATURE_ALG, PACKAGE_FILE_SIGNATURE_CONTEXT};
59pub use sources::{parse_source, source_with_params, ParsedSource};
60pub use tests::{
61    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,
62};
63pub use tools::{tool_call, tool_consent, tool_consent_key, validate_tools, ToolCall, ToolConsentRow, TOOL_KINDS};
64
65/// JavaScript's WhiteSpace + LineTerminator (what `trim` drops, regex `\s`).
66pub(crate) fn is_js_space(c: char) -> bool {
67    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}')
68}
69
70/// JavaScript's `String.prototype.trim` (WhiteSpace + LineTerminator).
71pub(crate) fn js_trim(s: &str) -> &str {
72    s.trim_matches(is_js_space)
73}
74
75/// JavaScript's default string order (`sort()`, `<`): by UTF-16 code units,
76/// which differs from Rust's byte order above U+FFFF.
77/// VAPP-202: when either side is ASCII the first difference is an ASCII
78/// byte against a byte of the same or a later char, where byte order and
79/// UTF-16 order agree, so the bytes compare directly.
80pub(crate) fn utf16_cmp(a: &str, b: &str) -> std::cmp::Ordering {
81    if a.is_ascii() || b.is_ascii() {
82        return a.as_bytes().cmp(b.as_bytes());
83    }
84    a.encode_utf16().cmp(b.encode_utf16())
85}
86
87/// JavaScript truthiness of a JSON value.
88pub(crate) fn js_truthy(value: &serde_json::Value) -> bool {
89    use serde_json::Value;
90    match value {
91        Value::Null => false,
92        Value::Bool(b) => *b,
93        Value::Number(n) => n.as_f64().is_some_and(|f| f != 0.0 && !f.is_nan()),
94        Value::String(s) => !s.is_empty(),
95        _ => true,
96    }
97}