harn-stdlib 0.8.26

Embedded Harn standard library source catalog
Documentation
import { filter_nil } from "std/collections"

/**
 * runtime_task.
 *
 * @effects: [host]
 * @allocation: heap
 * @errors: []
 * @api_stability: stable
 * @example: runtime_task()
 */
pub fn runtime_task() -> string {
  return host_call("runtime.task", {}) ?? ""
}

/**
 * runtime_pipeline_input.
 *
 * @effects: [host]
 * @allocation: heap
 * @errors: []
 * @api_stability: stable
 * @example: runtime_pipeline_input()
 */
pub fn runtime_pipeline_input() {
  return host_call("runtime.pipeline_input", {})
}

/**
 * runtime_dry_run.
 *
 * @effects: [host]
 * @allocation: stack-only
 * @errors: []
 * @api_stability: stable
 * @example: runtime_dry_run()
 */
pub fn runtime_dry_run() -> bool {
  return host_call("runtime.dry_run", {}) ?? false
}

/**
 * runtime_approved_plan.
 *
 * @effects: [host]
 * @allocation: heap
 * @errors: []
 * @api_stability: stable
 * @example: runtime_approved_plan()
 */
pub fn runtime_approved_plan() -> string {
  return host_call("runtime.approved_plan", {}) ?? ""
}

/**
 * process_run.
 *
 * @effects: [host]
 * @allocation: heap
 * @errors: []
 * @api_stability: stable
 * @example: process_run(argv, options)
 */
pub fn process_run(argv: list<string>, options = nil) {
  let opts = options ?? {}
  return host_call("process.exec", filter_nil(opts + {mode: "argv", argv: argv})) ?? {}
}

/**
 * process_shell.
 *
 * @effects: [host]
 * @allocation: heap
 * @errors: []
 * @api_stability: stable
 * @example: process_shell(command, options)
 */
pub fn process_shell(command: string, options = nil) {
  let opts = options ?? {}
  return host_call("process.exec", filter_nil(opts + {mode: "shell", command: command})) ?? {}
}

/**
 * process_result_text.
 *
 * @effects: []
 * @allocation: heap
 * @errors: []
 * @api_stability: stable
 * @example: process_result_text(result)
 */
pub fn process_result_text(result: dict) -> string {
  let stdout = result?.stdout ?? ""
  let stderr = result?.stderr ?? ""
  if stdout && stderr {
    return "${stdout}\n${stderr}"
  }
  if stdout {
    return stdout
  }
  if stderr {
    return stderr
  }
  return result?.combined ?? result?.inline_output ?? ""
}

/**
 * process_result_success.
 *
 * @effects: []
 * @allocation: stack-only
 * @errors: []
 * @api_stability: stable
 * @example: process_result_success(result)
 */
pub fn process_result_success(result: dict) -> bool {
  if result?.success != nil {
    return result?.success ? true : false
  }
  let status = result?.status ?? "completed"
  let exit_code = result?.exit_code ?? -1
  return status == "completed" && exit_code == 0
}

/**
 * shell_quote.
 *
 * @effects: []
 * @allocation: heap
 * @errors: []
 * @api_stability: stable
 * @example: shell_quote(value)
 */
pub fn shell_quote(value) -> string {
  return "'" + to_string(value ?? "").replace("'", "'\"'\"'") + "'"
}

/**
 * interaction_ask.
 *
 * @effects: [host]
 * @allocation: heap
 * @errors: []
 * @api_stability: stable
 * @example: interaction_ask(question)
 */
pub fn interaction_ask(question) -> string {
  return host_call("interaction.ask", {question: question}) ?? ""
}

/**
 * interaction_ask_with_kind.
 *
 * @effects: [host]
 * @allocation: heap
 * @errors: []
 * @api_stability: stable
 * @example: interaction_ask_with_kind(question, kind)
 */
pub fn interaction_ask_with_kind(question, kind) -> string {
  return host_call("interaction.ask", {question: question, type: kind}) ?? ""
}

/**
 * record_run_metadata.
 *
 * @effects: [host]
 * @allocation: heap
 * @errors: []
 * @api_stability: stable
 * @example: record_run_metadata(run, workflow_name)
 */
pub fn record_run_metadata(run, workflow_name) {
  if run?.path {
    host_call(
      "runtime.record_run",
      {
        workflow: workflow_name,
        path: run?.path,
        status: run?.status ?? "",
        fixture_type: run?.run?.replay_fixture?._type,
        usage: run?.usage,
        persisted_path: run?.persisted_path,
        summary: transcript_summary(run?.transcript),
        transcript_state: run?.transcript?.state ?? "",
        message_count: len(transcript_messages(run?.transcript)),
        event_count: len(transcript_events(run?.transcript)),
        asset_count: len(transcript_assets(run?.transcript)),
      },
    )
  }
}