harn-stdlib 0.10.39

Embedded Harn standard library source catalog
Documentation
import {
  agent_tool_lifecycle_error_category,
  agent_tool_result_is_ok,
  agent_tool_result_outcome,
} from "std/agent/tool_lifecycle"

pub fn __dispatch_results_list(dispatch) {
  if dispatch == nil {
    return []
  }
  if type_of(dispatch) == "list" {
    return dispatch
  }
  return dispatch?.results ?? []
}

fn __error_category_is_dispatch_rejection(category) -> bool {
  return category == "schema_validation" || category == "permission_denied"
}

/**
 * Dispatch rejection from STRUCTURED fields only: the outer envelope's typed
 * error category (via `agent_tool_result_outcome`), or the nested host body
 * `result.error` / `result.error_category` that denial producers already stamp.
 * Never substring-matches rendered prose.
 *
 * @effects: []
 * @errors: []
 */
pub fn __tool_result_dispatch_rejection(result) -> bool {
  if type_of(result) != "dict" {
    return false
  }
  const outcome = agent_tool_result_outcome(result)
  if __error_category_is_dispatch_rejection(outcome.error_category) {
    return true
  }
  const nested = result?.result
  if type_of(nested) == "dict" {
    const nested_cat = agent_tool_lifecycle_error_category(nested?.error_category ?? nested?.error)
    if __error_category_is_dispatch_rejection(nested_cat) {
      return true
    }
  }
  return false
}

pub fn __tool_result_product_error(result) -> bool {
  if type_of(result) != "dict" {
    return false
  }
  const outcome = agent_tool_result_outcome(result)
  if outcome.product_error != nil {
    return outcome.product_error ? true : false
  }
  return __tool_result_dispatch_rejection(result)
}

pub fn __tool_result_ok(result) -> bool {
  const outcome = agent_tool_result_outcome(result)
  const structured = agent_tool_result_is_ok(outcome)
  if structured == false {
    return false
  }
  if __tool_result_product_error(result) {
    return false
  }
  if structured == true {
    return true
  }
  if result?.ok != nil {
    return result.ok ? true : false
  }
  if result?.success != nil {
    return result.success ? true : false
  }
  const status = result?.status ?? ""
  return status == "ok" || status == "success"
}

pub fn __tool_result_name(result) {
  return result?.tool_name ?? result?.name ?? ""
}

pub fn __tool_names_by_status(dispatch, want_ok) {
  const results = __dispatch_results_list(dispatch)
  let names = []
  for result in results {
    const name = __tool_result_name(result)
    if name != "" && __tool_result_ok(result) == want_ok {
      names = names.appending(name)
    }
  }
  return names
}