import { cli_locale } from "std/cli/localization"
import { cli_message } from "std/cli/localization/providers/recommend"
import { safe_int, safe_list, safe_string } from "std/cli/render"
/**
* `harn provider catalog recommend` ported to .harn — see harn#2310 (W10).
*
* Host aggregation 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 __recommended_tool(value: string) -> string {
if type_of(value) == "string" {
return value
}
return "unproven"
}
fn __render_human(__cli_locale: string, report: dict) -> string {
let out = cli_message(__cli_locale, "local provider recommendations\n", [])
out = cli_message(
__cli_locale,
"{0}source: {1}\n",
[to_string(out), to_string(safe_string(report["source"], ""))],
)
const recommendations = safe_list(report["recommendations"])
if len(recommendations) == 0 {
return cli_message(__cli_locale, "{0}(no local model outcomes found)\n", [to_string(out)])
}
for rec in recommendations {
if type_of(rec) != "dict" {
continue
}
const rank = safe_int(rec["rank"], 0)
const provider = safe_string(rec["provider"], "")
const model = safe_string(rec["model"], "")
const status = safe_string(rec["status"], "")
const selector = safe_string(rec["selector"], "")
const tool_format = __recommended_tool(rec["recommended_tool_format"])
out = cli_message(
__cli_locale,
"{0}{1}. {2} {3} ({4}, selector {5}, tools {6})\n",
[
to_string(out),
to_string(to_string(rank)),
to_string(provider),
to_string(model),
to_string(status),
to_string(selector),
to_string(tool_format),
],
)
const caveats = safe_list(rec["caveats"])
for caveat in caveats {
if type_of(caveat) == "string" {
out = cli_message(__cli_locale, "{0} caveat: {1}\n", [to_string(out), to_string(caveat)])
}
}
}
return out
}
fn main(harness: Harness) -> int {
const __cli_locale = cli_locale(harness.env)
const 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
}
const report = try {
json_parse(raw)
} catch (e) {
harness.stdio.eprintln(
"internal error: failed to parse providers-recommend payload: " + e.message,
)
return 70
}
const 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.
const 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
}
const text = __render_human(__cli_locale, report)
// The host renderer always ended 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.
const trimmed = if len(text) > 0 && text[len(text) - 1] == "\n" {
text[0:len(text) - 1]
} else {
text
}
harness.stdio.println(trimmed)
return 0
}