Community MoonBit SDK for TypeSafe System One (Jev): typed Noul/Choice/Score questions, formally verified confidence gates and retry policy, and network-free testing.
moon add huanguan-Q/typesafe-sdk-moonbit// 1) Build a typed request.
let questions : Map[String, Question] = Map([])
questions.set(
"billing",
Question::NoulQ({ instructions: "Is this message about billing?", yes_when: None, no_when: None }),
)
let request : SystemOneRequest =::{
model: "jev-latest",
state: "I was charged twice for my subscription.",
questions,
}
// 2) Call the API through any transport — in production a real HTTP
// client wrapped in `Transport`, in tests a `Transport::scripted`.
let (transport, calls) = Transport::scripted([/* ... */])
let config = ClientConfig::make(api_key=env_key)
let result = evaluate_with_retries(
config,
transport,
request,
fn(ms) { sleep(ms) }, // injected timer: tests pass a recorder instead
)
// 3) Gate the answer with verified policy.
match result {
Ok(response) =>
match response.answers.get("billing") {
Some(Answer::NoulA(probability)) => {
let verdict = noul_gate(to_bp(probability), 9000, 1000)
let decision = decide(
to_bp(probability),
9000, // confidence threshold: 0.9
response.usage.input_tokens,
10000, // token budget for this workflow
)
// verdict / decision drive routing, escalation, or a hard stop.
}
_ => ()
}
Err(SdkError::RateLimited(retry_after)) => () // classified, retry-aware
Err(_) => ()
}// import "huanguan-Q/typesafe-sdk-moonbit/src/fetch"
// check/build with: moon check --target js
evaluate_async(config, request, fn(result) { /* callback */ })| Function | Theorem (all discharged by Z3) |
|---|---|
| decide_code | Blocked ⟺ over budget; Proceed ⟺ in budget ∧ confidence ≥ threshold; Escalate ⟺ in budget ∧ below threshold. Budget dominates any confidence. |
| noul_gate_code | Each verdict code ⟺ its probability band. |
| on_failure_code | Blocked ⟺ fail-closed; never Proceed — a transport failure never yields a green light. |
| should_retry | Exactly attempts_done < max_retries. |
| exp_delay | Result stays within [d, 2·max]; overflow-safe for max ≤ 2³⁰−1. |
| delay_ms | Any directive (≥ 0, including 0 = immediate) is honored exactly; without one the delay stays within [base, max]. |
pub(all) struct AsyncTransport {
deliver : (HttpRequest, (Result[HttpResponse, TransportError]) -> Unit) -> Unit
}fn AsyncTransport::scripted(responses : Array[Result[HttpResponse, TransportError]]) -> (AsyncTransport, Array[HttpRequest])pub(all) struct BoxCoords {
top : Double
bottom : Double
left : Double
right : Double
} derive(Debug)pub(all) struct ClientConfig {
api_key : String
model : String
base_url : String
retry : RetryPolicy
} derive(Debug)fn ClientConfig::make(api_key~ : String, model? : String, base_url? : String, retry? : RetryPolicy) -> ClientConfigpub(all) struct NoulQuestion {
instructions : String
yes_when : String?
no_when : String?
} derive(Debug)pub(all) enum Question {
NoulQ(NoulQuestion)
ChoiceQ(ChoiceQuestion)
ScoreQ(ScoreQuestion)
BoxQ(BoxQuestion)
} derive(Debug)pub(all) struct RetryPolicy {
max_retries : Int
base_delay_ms : Int
max_delay_ms : Int
} derive(Debug)pub(all) enum SdkError {
Network(String)
RateLimited(Int?)
Server(Int, String)
InvalidRequest(Int, String)
Decode(String)
} derive(Debug)pub(all) struct SystemOneRequest {
model : String
state : String
questions : Map[String, Question]
assets : Map[String, ImageAsset]
} derive(Debug)fn SystemOneRequest::make(model~ : String, state~ : String, questions~ : Map[String, Question]) -> SystemOneRequestfn Transport::scripted(responses : Array[Result[HttpResponse, TransportError]]) -> (Transport, Array[HttpRequest])fn classify_response(response : HttpResponse, now_secs : Int) -> Result[SystemOneResponse, SdkError]fn days_from_civil(year : Int, month : Int, day : Int) -> Intfn decide(confidence_bp : Int, threshold_bp : Int, tokens_used : Int, token_budget : Int) -> Decisionfn decide_code(confidence_bp : Int, threshold_bp : Int, tokens_used : Int, token_budget : Int) -> Intfn delay_ms(base_delay_ms : Int, max_delay_ms : Int, attempt : Int, retry_after_secs : Int) -> Intfn evaluate_once(config : ClientConfig, transport : Transport, request : SystemOneRequest, now_secs : () -> Int) -> Result[SystemOneResponse, SdkError]fn evaluate_with_retries(config : ClientConfig, transport : Transport, request : SystemOneRequest, sleep_ms : (Int) -> Unit, now_secs : () -> Int) -> Result[SystemOneResponse, SdkError]fn evaluate_with_retries_async(config : ClientConfig, transport : AsyncTransport, request : SystemOneRequest, sleep_async : (Int, () -> Unit) -> Unit, now_secs : () -> Int, on_done : (Result[SystemOneResponse, SdkError]) -> Unit) -> Unitfn from_bp(bp : Int) -> Doublefn noul_gate_code(noul_bp : Int, high_bp : Int, low_bp : Int) -> Intfn on_failure_code(fail_closed : Bool) -> Intfn parse_http_date_secs(raw : String) -> Int?fn parse_retry_after(header : String?, now_secs : Int) -> Int?fn should_retry(attempts_done : Int, max_retries : Int) -> Boolfn to_bp(probability : Double) -> IntInstall
Download zipCommunity MoonBit SDK for TypeSafe System One (Jev): typed Noul/Choice/Score questions, formally verified confidence gates and retry policy, and network-free testing.