Skip to main content

ri_esp_local_language/
lib.rs

1#![no_std]
2
3//! Contract crate for the ESP32-S3 H320 p15 local-language layer.
4//!
5//! Owns receipt schema constants, the canonical prompt-to-output table verified
6//! by the 2026-07-01 real S3 hard test, and fixed-capacity receipt structs.
7//!
8//! Does not choose policy — use `ri-esp-policy` for deterministic prompt selection.
9
10use heapless::String;
11use ri_esp_policy::PromptId;
12
13/// Schema constant for S3 local-language receipts.
14pub const LOCAL_LANGUAGE_SCHEMA: &str = "ri_esp32s3_local_language_v1";
15
16/// Schema constant for sensor-policy to S3 local-language integration receipts.
17pub const SENSOR_POLICY_INTEGRATION_SCHEMA: &str = "ri_sensor_policy_to_s3_language_integration_v1";
18
19/// Default model identifier for the ESP32-S3 local-language layer.
20pub const DEFAULT_LOCAL_LANGUAGE_MODEL: &str = "esp32s3-h320-p15-char-lstm";
21
22/// Returns the canonical S3 model output for a given prompt id.
23///
24/// Verified by 24 real ESP32-S3 H320 p15 hardware generations on 2026-07-01.
25pub fn canonical_output(id: PromptId) -> &'static str {
26    match id {
27        PromptId::HighHeatHumidity => "escalate.",
28        PromptId::HotRoom => "check airflow.",
29        PromptId::HumidRoom => "ventilate.",
30        PromptId::LocalFirstMeans => "decide before cloud.",
31        PromptId::MissingSensor => "no claim.",
32        PromptId::NormalRoom => "log receipt.",
33        PromptId::SafeAction => "no claim without evidence.",
34        PromptId::StaleData => "wait for fresh data.",
35    }
36}
37
38/// Fixed-capacity receipt for a single S3 local-language generation.
39#[derive(Debug, Clone)]
40pub struct S3LanguageReceipt {
41    pub schema: &'static str,
42    pub prompt: String<64>,
43    pub output: String<64>,
44    pub passed: bool,
45}
46
47impl S3LanguageReceipt {
48    /// Creates a receipt from the canonical prompt/output table with passed=true.
49    pub fn static_passed(id: PromptId) -> Self {
50        let mut prompt = String::new();
51        let _ = prompt.push_str(id.prompt_text());
52
53        let mut output = String::new();
54        let _ = output.push_str(canonical_output(id));
55
56        Self {
57            schema: LOCAL_LANGUAGE_SCHEMA,
58            prompt,
59            output,
60            passed: true,
61        }
62    }
63}
64
65/// Fixed-capacity integration receipt linking sensor policy to S3 language output.
66#[derive(Debug, Clone)]
67pub struct IntegrationReceipt {
68    pub schema: &'static str,
69    pub device_id: String<64>,
70    pub derived_reason: String<64>,
71    pub policy_prompt: String<64>,
72    pub oled_text: String<64>,
73}
74
75impl IntegrationReceipt {
76    /// Creates an integration receipt from a device id, derived reason, and
77    /// the local S3 language receipt.
78    pub fn new(device_id: &str, derived_reason: &str, local: S3LanguageReceipt) -> Self {
79        let mut did = String::new();
80        let _ = did.push_str(device_id);
81
82        let mut dr = String::new();
83        let _ = dr.push_str(derived_reason);
84
85        let mut pp = String::new();
86        let _ = pp.push_str(local.prompt.as_str());
87
88        let mut ot = String::new();
89        let _ = ot.push_str(local.output.as_str());
90
91        Self {
92            schema: SENSOR_POLICY_INTEGRATION_SCHEMA,
93            device_id: did,
94            derived_reason: dr,
95            policy_prompt: pp,
96            oled_text: ot,
97        }
98    }
99}