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 contract;
15pub mod decoders;
16pub mod exposes;
17pub mod expressions;
18pub mod package;
19pub mod policy;
20pub mod query;
21pub mod record_store;
22pub mod records;
23pub mod router;
24pub mod signature;
25pub mod sources;
26pub mod tests;
27pub mod tools;
28
29pub use actions::{
30    action_call_name, action_var_types, apply_record_batch, 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,
31    ACTION_STEP_KINDS, RECORDS_BATCH_FUNCTION,
32};
33pub use contract::*;
34pub use expressions::{literal_type, record_row_type, records_value_type, schema_type, validate_package_expressions};
35pub use exposes::{expose_action_access, expose_issues, expose_name_destructive, expose_template_paths, validate_package_exposes, validate_upstreams, ExposeIssue};
36pub use decoders::{decode_jsonl, mcp_action_call, messages_from_mcp_result, DecodeIssue, Decoded, JsonlDecoder, SseDecoder};
37pub use package::{
38    available_extensions, client_capabilities, merge_data, package_sources, source_allowed, supported_catalog_ids, template_messages, validate_package, validate_package_with, version_satisfies, AvailableExtension, PackageIssue,
39    PACKAGE_FORMAT_VERSION,
40};
41pub use policy::{
42    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,
43    safe_href, FunctionDecision, FunctionPolicy, MediaOptions, MediaRequest, MediaRule, UrlDecision, UrlPolicy, BUILTIN_FUNCTIONS,
44};
45pub use router::{normalize_path, HostRouter, SurfaceInfo};
46pub use query::{
47    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,
48    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,
49};
50pub use record_store::RECORDS_FUNCTIONS;
51pub use records::{is_record_kind, validate_record, validate_record_schema, RECORD_FIELD_TYPES, RECORD_FORMATS, RECORD_RESERVED_KEYS};
52pub use signature::{canonical_json, package_file_signed_bytes, AUTHOR_KEY_PREFIX, AUTHOR_NAME_MAX, PACKAGE_FILE_SIGNATURE_ALG, PACKAGE_FILE_SIGNATURE_CONTEXT};
53pub use sources::{parse_source, source_with_params, ParsedSource};
54pub use tests::{
55    package_test_issues, run_package_tests, test_failure, test_matches, validate_package_tests, PackageTestResult, PACKAGE_TEST_DEFAULT_AS, PACKAGE_TEST_DEFAULT_NOW, PACKAGE_TEST_KINDS, PACKAGE_TEST_LIMIT_GOT_MAX, PACKAGE_TEST_LIMIT_TESTS,
56};
57pub use tools::{tool_call, tool_consent, tool_consent_key, validate_tools, ToolCall, ToolConsentRow, TOOL_KINDS};
58
59/// JavaScript's WhiteSpace + LineTerminator (what `trim` drops, regex `\s`).
60pub(crate) fn is_js_space(c: char) -> bool {
61    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}')
62}
63
64/// JavaScript's `String.prototype.trim` (WhiteSpace + LineTerminator).
65pub(crate) fn js_trim(s: &str) -> &str {
66    s.trim_matches(is_js_space)
67}
68
69/// JavaScript's default string order (`sort()`, `<`): by UTF-16 code units,
70/// which differs from Rust's byte order above U+FFFF.
71pub(crate) fn utf16_cmp(a: &str, b: &str) -> std::cmp::Ordering {
72    a.encode_utf16().cmp(b.encode_utf16())
73}
74
75/// JavaScript truthiness of a JSON value.
76pub(crate) fn js_truthy(value: &serde_json::Value) -> bool {
77    use serde_json::Value;
78    match value {
79        Value::Null => false,
80        Value::Bool(b) => *b,
81        Value::Number(n) => n.as_f64().is_some_and(|f| f != 0.0 && !f.is_nan()),
82        Value::String(s) => !s.is_empty(),
83        _ => true,
84    }
85}