harn-stdlib 0.8.52

Embedded Harn standard library source catalog
Documentation
/**
 * `harn models test` rendering layer ported to .harn — see harn#2309
 * (W9).
 *
 * **Pragmatic partial port.** The actual smoke test runs in Rust —
 * `harn_vm::llm::run_model_smoke_test` builds bespoke
 * `LlmCallOptions`, streams via `vm_call_llm_full_streaming`, probes
 * provider readiness, and computes pricing. None of that is reachable
 * from script-land today without a wider VM surface than W9 should
 * ship. The Rust shim runs the smoke test, captures the result (or
 * error), and hands a single JSON envelope here for formatting.
 *
 * Inputs (from the dispatch shim):
 *   HARN_MODELS_TEST_RESULT_JSON — JSON envelope. On success:
 *     {
 *       "ok": true,
 *       "result": {
 *         "model_id", "provider", "latency_ms", "first_token_ms"?,
 *         "input_tokens", "output_tokens", "estimated_cost_usd"
 *       }
 *     }
 *     On failure:
 *     {"ok": false, "error": "..."}
 *   HARN_OUTPUT_JSON — "1" for JSON output, else human text.
 */
fn __safe_string(value, fallback: string) -> string {
  if type_of(value) == "string" {
    return value
  }
  return fallback
}

fn __safe_dict(value) -> dict {
  if type_of(value) == "dict" {
    return value
  }
  return {}
}

/**
 * Format a float as `"X.YYYYYY"` (6 decimals, half-up padded). Matches
 * Rust's `format!("{:.6}", x)` for the legacy text-output cost field.
 */
fn __format_float_6(value) -> string {
  let f = to_float(value) ?? 0.0
  let negative = f < 0.0
  let abs_f = if negative {
    -f
  } else {
    f
  }
  let scaled = to_int(round(abs_f * 1000000.0)) ?? 0
  let whole = scaled / 1000000
  let frac = scaled - whole * 1000000
  var frac_str = to_string(frac)
  while len(frac_str) < 6 {
    frac_str = "0" + frac_str
  }
  let sign = if negative && (whole != 0 || frac != 0) {
    "-"
  } else {
    ""
  }
  return sign + to_string(whole) + "." + frac_str
}

/**
 * Render the success line. Order matches the legacy Rust impl:
 * `model_id=... provider=... latency_ms=... first_token_ms=... \
 *  input_tokens=... output_tokens=... estimated_cost_usd={:.6}`.
 * `first_token_ms` is `"-"` when the streaming probe didn't see a
 * delta in time.
 */
fn __render_success_line(result: dict) -> string {
  let first_token = if result["first_token_ms"] == nil {
    "-"
  } else {
    to_string(result["first_token_ms"])
  }
  return "model_id=" + __safe_string(result["model_id"], "")
    + " provider="
    + __safe_string(result["provider"], "")
    + " latency_ms="
    + to_string(result["latency_ms"] ?? 0)
    + " first_token_ms="
    + first_token
    + " input_tokens="
    + to_string(result["input_tokens"] ?? 0)
    + " output_tokens="
    + to_string(result["output_tokens"] ?? 0)
    + " estimated_cost_usd="
    + __format_float_6(result["estimated_cost_usd"] ?? 0.0)
}

/**
 * Build the success JSON envelope. The legacy Rust impl emits
 * `serde_json::to_string_pretty(&result)` directly — fields are:
 * `model_id`, `provider`, `latency_ms`, `first_token_ms?`,
 * `input_tokens`, `output_tokens`, `estimated_cost_usd`. The
 * `#[serde(skip_serializing_if = "Option::is_none")]` attribute on
 * `first_token_ms` means absent when nil; mirror that here so the
 * parity test's structural compare lines up.
 */
fn __render_success_envelope(result: dict) -> string {
  var envelope = {
    model_id: __safe_string(result["model_id"], ""),
    provider: __safe_string(result["provider"], ""),
    latency_ms: result["latency_ms"] ?? 0,
    input_tokens: result["input_tokens"] ?? 0,
    output_tokens: result["output_tokens"] ?? 0,
    estimated_cost_usd: result["estimated_cost_usd"] ?? 0.0,
  }
  if result["first_token_ms"] != nil {
    envelope = envelope + {first_token_ms: result["first_token_ms"]}
  }
  return json_stringify_pretty(envelope)
}

/**
 * Render the failure JSON envelope. Matches the legacy
 * `serde_json::json!({ "ok": false, "error": error })` — note that
 * the Rust impl uses `to_string()` (compact, no trailing newline)
 * for this branch, not `to_string_pretty`. Replicate the compact
 * form so the parity test's structural compare lines up regardless.
 */
fn __render_failure_envelope(error: string) -> string {
  return json_stringify({ok: false, error: error})
}

fn main(harness: Harness) -> int {
  let raw = harness.env.get_or("HARN_MODELS_TEST_RESULT_JSON", "")
  if raw == "" {
    harness.stdio
      .eprintln("internal error: HARN_MODELS_TEST_RESULT_JSON not set by dispatch shim")
    return 70
  }
  let envelope = try {
    json_parse(raw)
  } catch (e) {
    harness.stdio.eprintln("internal error: failed to parse models-test result: " + to_string(e))
    return 70
  }
  let json_mode = harness.env.get_or("HARN_OUTPUT_JSON", "0") == "1"
  let ok = envelope["ok"] ?? false
  if ok {
    let result = __safe_dict(envelope["result"])
    if json_mode {
      harness.stdio.println(__render_success_envelope(result))
    } else {
      harness.stdio.println(__render_success_line(result))
    }
    return 0
  }
  let error = __safe_string(envelope["error"], "unknown error")
  if json_mode {
    // Legacy impl writes the compact `{ok, error}` to STDOUT (not
    // stderr) with `println!` and then `process::exit(1)`. Mirror
    // that — the channel split matters for shells piping `--json`.
    harness.stdio.println(__render_failure_envelope(error))
  } else {
    harness.stdio.eprintln(error)
  }
  return 1
}