import { cli_locale } from "std/cli/localization"
import { cli_message } from "std/cli/localization/runs/export_training"
/**
* `harn runs export-training` renderer.
*
* The dispatch shim projects one authoritative run into a
* `harn.agent_training_example.v1` example. This Harn source owns the report
* presentation and JSON passthrough.
*
* Inputs (from the dispatch shim):
* HARN_RUNS_EXPORT_TRAINING_PAYLOAD_JSON - compact report JSON.
* HARN_RUNS_EXPORT_TRAINING_PAYLOAD_PRETTY - pretty report JSON.
* HARN_OUTPUT_JSON - "1" for JSON, else human text.
*/
import {
cli_json_envelope,
safe_bool,
safe_dict,
safe_int_string,
safe_list,
safe_string,
} from "std/cli/render"
fn __message_summary(item: unknown) -> string {
const message = safe_dict(item)
const role = safe_string(message["role"], "")
const calls = safe_list(message["tool_calls"])
let line = " - " + role
if len(calls) > 0 {
const names = calls.map(
{ call -> safe_string(safe_dict(safe_dict(call)["function"])["name"], "") },
)
line = line + " calls " + join(names, ", ")
}
const call_id = safe_string(message["tool_call_id"], "")
if call_id != "" {
line = line + " answers " + call_id
}
return line + " (" + safe_int_string(len(safe_string(message["content"], "")), "0") + " chars)"
}
fn __render_human(__cli_locale: string, stdio: HarnessStdio, report: dict) {
const ok = safe_bool(report["ok"], false)
stdio.println(
cli_message(
__cli_locale,
"Training example export from {0}",
[to_string(safe_string(report["run_record_path"], ""))],
),
)
if !ok {
const error = safe_dict(report["error"])
stdio.eprintln(
cli_message(
__cli_locale,
" refused: {0}",
[to_string(safe_string(error["kind"], "unknown"))],
),
)
stdio.eprintln(" " + safe_string(error["message"], ""))
const event_index = error["event_index"]
if event_index != nil {
stdio.eprintln(" source event: " + safe_int_string(event_index, "0"))
}
return
}
const example = safe_dict(report["example"])
const provenance = safe_dict(example["provenance"])
const source = safe_dict(provenance["source"])
const usage = safe_dict(provenance["usage"])
const messages = safe_list(example["messages"])
const tools = safe_list(example["tools"])
stdio.println(
cli_message(
__cli_locale,
" schema: {0}",
[to_string(safe_string(example["schema_version"], ""))],
),
)
stdio.println(
cli_message(__cli_locale, " run: {0}", [to_string(safe_string(provenance["run_id"], ""))]),
)
stdio.println(
cli_message(
__cli_locale,
" session: {0}",
[to_string(safe_string(provenance["session_id"], ""))],
),
)
stdio.println(
cli_message(
__cli_locale,
" route: {0}/{1} tool_format={2}",
[
to_string(safe_string(provenance["provider"], "")),
to_string(safe_string(provenance["model"], "")),
to_string(safe_string(provenance["effective_tool_format"], "")),
],
),
)
stdio.println(
cli_message(
__cli_locale,
" terminal status: {0}",
[to_string(safe_string(provenance["terminal_status"], ""))],
),
)
stdio.println(
cli_message(
__cli_locale,
" tool catalog: {0} schemas, hash {1}",
[
to_string(safe_int_string(len(tools), "0")),
to_string(safe_string(provenance["tool_catalog_hash"], "")),
],
),
)
stdio.println(
cli_message(
__cli_locale,
" usage: {0} provider calls, {1} in / {2} out",
[
to_string(safe_int_string(usage["provider_calls"], "0")),
to_string(safe_int_string(usage["input_tokens"], "0")),
to_string(safe_int_string(usage["output_tokens"], "0")),
],
),
)
stdio.println(" source transcript: " + safe_string(source["transcript_path"], ""))
stdio.println(" source digest: " + safe_string(source["transcript_sha256"], ""))
stdio.println(
" source events: "
+ safe_int_string(source["first_event_index"], "0")
+ "-"
+ safe_int_string(source["last_event_index"], "0")
+ " of "
+ safe_int_string(source["event_count"], "0"),
)
stdio.println(
cli_message(
__cli_locale,
" messages ({0}):",
[to_string(safe_int_string(len(messages), "0"))],
),
)
for item in messages {
stdio.println(__message_summary(item))
}
const output_path = safe_string(report["output_path"], "")
if output_path != "" {
stdio.println(cli_message(__cli_locale, " wrote: {0}", [to_string(output_path)]))
}
}
fn __render_json(report: dict) -> string {
const ok = safe_bool(report["ok"], false)
const envelope = if ok {
cli_json_envelope({schema_version: 1, ok: true, data: report})
} else {
const error = safe_dict(report["error"])
cli_json_envelope(
{
schema_version: 1,
ok: false,
error: {
code: safe_string(error["kind"], "training_example_export_failed"),
message: safe_string(error["message"], "Training example projection failed."),
details: report,
},
},
)
}
return json_stringify_pretty(envelope)
}
fn main(harness: Harness) -> int {
const __cli_locale = cli_locale(harness.env)
const raw = harness.env.get_or("HARN_RUNS_EXPORT_TRAINING_PAYLOAD_JSON", "")
if raw == "" {
harness.stdio.eprintln("internal error: HARN_RUNS_EXPORT_TRAINING_PAYLOAD_JSON not set")
return 70
}
const report = try {
json_parse(raw)
} catch (e) {
harness.stdio.eprintln(
"internal error: failed to parse training example export payload: " + e.message,
)
return 70
}
const json_mode = harness.env.get_or("HARN_OUTPUT_JSON", "0") == "1"
if json_mode {
harness.stdio.println(__render_json(report))
} else {
__render_human(__cli_locale, harness.stdio, report)
}
if safe_bool(report["ok"], false) {
return 0
}
return 1
}