Something went wrong. Try again.
Personal fork of prime agent. Mirror of: https://mnemosyne.sigilzero.dev/sigilzero/prime-agent
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135//! Shared domain and wire types for Prime Agent: the serde port of the TypeScript wire and domain//! contracts ([`ai`] holds the model-facing message surface from TS `packages/ai/src/types.ts`).//!//! # Lossless round-trips//!//! Every wire struct carries a `#[serde(flatten)] rest: JsonMap` catch-all, so unmodeled fields//! survive a parse/serialize round trip: the re-parsed JSON must equal the original.
pub mod ai;pub mod compaction;pub mod daemon;pub mod goal;pub mod incident;pub mod markdown;pub mod memory_release;pub mod model_refresh;pub mod platform;pub mod session;pub mod skill_blocks;pub mod slash_commands;pub mod themes;pub mod usage;
use serde::{Deserialize, Serialize};
/// JSON object map used for opaque payloads and unknown-field catch-alls.pub type JsonMap = serde_json::Map<String, serde_json::Value>;
/// An f64 that (de)serializes with JavaScript `JSON.stringify` number parity. TypeScript numbers/// are/// f64 and `JSON.stringify` prints integral values without a fractional part (`0`, not `0.0`);/// Rust's `f64` serialization always prints `0.0`, which would break lossless round-trips against/// TS-produced files. This newtype prints integral values as integers, everything else via the/// shortest f64 representation.#[derive(Debug, Clone, Copy, PartialEq, Default)]pub struct JsNumber(pub f64);
impl JsNumber { #[must_use] pub fn as_f64(self) -> f64 { self.0 }}
impl From<f64> for JsNumber { fn from(v: f64) -> Self { JsNumber(v) }}
impl From<i64> for JsNumber { // TS number parity: JavaScript numbers are f64 by definition. #[allow(clippy::cast_precision_loss)] fn from(v: i64) -> Self { JsNumber(v as f64) }}
impl From<u64> for JsNumber { // TS number parity: JavaScript numbers are f64 by definition. #[allow(clippy::cast_precision_loss)] fn from(v: u64) -> Self { JsNumber(v as f64) }}
impl Serialize for JsNumber { fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { let v = self.0; if v.is_finite() && v.fract() == 0.0 && v.abs() < 9.007_199_254_740_992e15 { // The guard proves the conversion exact: whole value, |v| < 2^53. #[allow(clippy::cast_possible_truncation)] let whole = v as i64; serializer.serialize_i64(whole) } else { serializer.serialize_f64(v) } }}
impl<'de> Deserialize<'de> for JsNumber { fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { struct V; // TS number parity: u64/i64 JSON numbers deserialize as f64, the // single JavaScript number type. #[allow(clippy::cast_precision_loss)] impl serde::de::Visitor<'_> for V { type Value = JsNumber; fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("a JSON number") } fn visit_u64<E: serde::de::Error>(self, v: u64) -> Result<Self::Value, E> { Ok(JsNumber(v as f64)) } fn visit_i64<E: serde::de::Error>(self, v: i64) -> Result<Self::Value, E> { Ok(JsNumber(v as f64)) } fn visit_f64<E: serde::de::Error>(self, v: f64) -> Result<Self::Value, E> { Ok(JsNumber(v)) } } deserializer.deserialize_any(V) }}
#[cfg(test)]mod numeric_roundtrip_tests { use super::JsNumber;
#[test] fn canonical_usage_cost_numbers_keep_exact_f64_bits_after_physical_json_roundtrip() { let costs = [ 0.01744_f64, 0.000_410_000_000_000_000_05, 0.01785, 0.000_900_000_000_000_000_1, 0.017_972_000_000_000_002, ]; for cost in costs { let original = JsNumber(cost); let encoded = serde_json::to_vec(&original).expect("encode cost"); let decoded: JsNumber = serde_json::from_slice(&encoded).expect("decode cost"); assert_eq!( decoded.as_f64().to_bits(), original.as_f64().to_bits(), "physical JSON must preserve exact canonical numeric intent" ); assert_eq!( serde_json::to_value(decoded).expect("decoded intent"), serde_json::to_value(original).expect("original intent") ); } }}