harn-stdlib 0.8.50

Embedded Harn standard library source catalog
Documentation
/**
 * `harn providers recommend` ported to .harn — see harn#2310 (W10).
 *
 * **Pragmatic partial port.** The legacy Rust impl reads coding-agent
 * readiness JSON / summary JSON / default-report from disk (with a
 * non-trivial summary→outcome reduction pipeline), filters by
 * `--provider`, then renders. File I/O + the reduction pipeline stays
 * in Rust — the script sandbox can't reach the default readiness
 * paths, and the reduction code is heavy enough to be its own port
 * later. The Rust shim performs the load + filter and hands the
 * already-filtered report here for rendering.
 *
 * Inputs (from the dispatch shim):
 *   HARN_PROVIDERS_RECOMMEND_PAYLOAD_JSON — the filtered
 *     `LocalReadinessReport` envelope. Required keys: source (string),
 *     recommendations (list of {rank, provider, model, selector,
 *     status, recommended_tool_format?, caveats: list<string>}).
 *   HARN_OUTPUT_JSON — "1" for the JSON envelope, else human text.
 */
fn __safe_string(value, fallback: string) -> string {
  if type_of(value) == "string" {
    return value
  }
  return fallback
}

fn __safe_list(value) -> list {
  if type_of(value) == "list" {
    return value
  }
  return []
}

fn __safe_int(value, fallback: int) -> int {
  if type_of(value) == "int" {
    return value
  }
  return fallback
}

fn __recommended_tool(value) -> string {
  if type_of(value) == "string" {
    return value
  }
  return "unproven"
}

fn __render_human(report: dict) -> string {
  var out = "local provider recommendations\n"
  out = out + "source: " + __safe_string(report["source"], "") + "\n"
  let recommendations = __safe_list(report["recommendations"])
  if len(recommendations) == 0 {
    return out + "(no local model outcomes found)\n"
  }
  for rec in recommendations {
    if type_of(rec) != "dict" {
      continue
    }
    let rank = __safe_int(rec["rank"], 0)
    let provider = __safe_string(rec["provider"], "")
    let model = __safe_string(rec["model"], "")
    let status = __safe_string(rec["status"], "")
    let selector = __safe_string(rec["selector"], "")
    let tool_format = __recommended_tool(rec["recommended_tool_format"])
    out = out + to_string(rank) + ". " + provider + " " + model
      + " ("
      + status
      + ", selector "
      + selector
      + ", tools "
      + tool_format
      + ")\n"
    let caveats = __safe_list(rec["caveats"])
    for caveat in caveats {
      if type_of(caveat) == "string" {
        out = out + "   caveat: " + caveat + "\n"
      }
    }
  }
  return out
}

fn main(harness: Harness) -> int {
  let raw = harness.env.get_or("HARN_PROVIDERS_RECOMMEND_PAYLOAD_JSON", "")
  if raw == "" {
    harness.stdio
      .eprintln("internal error: HARN_PROVIDERS_RECOMMEND_PAYLOAD_JSON not set by dispatch shim")
    return 70
  }
  let report = try {
    json_parse(raw)
  } catch (e) {
    harness.stdio
      .eprintln("internal error: failed to parse providers-recommend payload: " + to_string(e))
    return 70
  }
  let json_mode = harness.env.get_or("HARN_OUTPUT_JSON", "0") == "1"
  if json_mode {
    // Forward the pretty-printed bytes the dispatch shim handed us so
    // we don't lose serde's float fidelity (`score`/`recommended_*`
    // fields) through Harn's `json_parse`/`json_stringify` round-trip.
    let pretty = harness.env.get_or("HARN_PROVIDERS_RECOMMEND_PAYLOAD_PRETTY", "")
    if pretty != "" {
      harness.stdio.println(pretty)
    } else {
      harness.stdio.println(json_stringify_pretty(report))
    }
    return 0
  }
  let text = __render_human(report)
  // The legacy renderer always ends each line with `println!`; the
  // last println leaves a single trailing newline. Strip the trailing
  // newline so `harness.stdio.println` can re-add exactly one and the
  // bytes match the Rust path.
  let trimmed = if len(text) > 0 && text[len(text) - 1] == "\n" {
    text[0:len(text) - 1]
  } else {
    text
  }
  harness.stdio.println(trimmed)
  return 0
}