Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(tinyagents): add pformat registry and usage metadata helpers

Steven Enamakel1d2fce41f99e
diff --git a/crates/openhuman-core/src/agent/progress_tracing/langfuse/journal_export.rs b/crates/openhuman-core/src/agent/progress_tracing/langfuse/journal_export.rs
index ab6870dff1..30425e43df 100644
--- a/crates/openhuman-core/src/agent/progress_tracing/langfuse/journal_export.rs
+++ b/crates/openhuman-core/src/agent/progress_tracing/langfuse/journal_export.rs
@@ -20,6 +20,21 @@ use crate::security::credentials::session_support::direct_backend_credential;
use super::{environment_for_base, ingestion_url, skip_push, LOG_TARGET, PUSH_TIMEOUT};
+/// Project the run-ledger aggregate onto the upstream run-total shape.
+fn run_totals(telemetry: &RunTelemetry) -> RunTotals {
+ RunTotals {
+ run_id: telemetry.run_id.clone(),
+ input_tokens: telemetry.input_tokens,
+ output_tokens: telemetry.output_tokens,
+ cached_input_tokens: telemetry.cached_input_tokens,
+ cost_usd: telemetry.cost_usd,
+ tool_count: telemetry.tool_count,
+ model: telemetry.model.clone(),
+ provider: telemetry.provider.clone(),
+ error: telemetry.error.clone(),
+ }
+}
+
/// Push durable journal observations through the tinyagents crate Langfuse
/// exporter. The journal is already redacted before persistence, and this
/// exporter additionally strips model/tool payloads unless `capture_content`
diff --git a/crates/openhuman-core/src/agent/tinyagents/model.rs b/crates/openhuman-core/src/agent/tinyagents/model.rs
index 8f8e1ed0de..f36da582c6 100644
--- a/crates/openhuman-core/src/agent/tinyagents/model.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/model.rs
@@ -16,6 +16,29 @@ pub(super) type TurnChatModel = Arc<dyn ChatModel<()>>;
pub(super) type TierRoutes = Vec<(String, TurnChatModel)>;
pub(super) type BuiltTurnModels = (TurnChatModel, TierRoutes, TurnChatModel);
+/// Build a [`PFormatRegistry`](tinytools_agent::PFormatRegistry)
+/// from the tool schemas advertised on a [`ModelRequest`] (issue #4465).
+///
+/// The text-mode fallback parse needs each tool's positional parameter layout
+/// to reconstruct named JSON arguments from a P-Format `name[a|b]` body. The
+/// harness populates `request.tools` when tools are available (schemas are
+/// rendered into the prompt for prompt-guided providers, or advertised natively
+/// otherwise), so the registry is available in both modes. Tool-less requests
+/// skip fallback parsing entirely; this empty registry is therefore consulted
+/// only alongside a non-empty advertised tool list.
+fn pformat_registry_from_request(request: &ModelRequest) -> tinytools_agent::PFormatRegistry {
+ request
+ .tools
+ .iter()
+ .map(|t| {
+ (
+ t.name.clone(),
+ tinytools_agent::PFormatToolParams::from_schema(&t.parameters),
+ )
+ })
+ .collect()
+}
+
/// Translate an openhuman [`ChatResponse`] into a harness [`ModelResponse`]
/// (visible text + tool calls + token usage).
///
@@ -171,6 +194,21 @@ struct OpenhumanUsageMeta {
context_window: u64,
}
+/// Build the `ModelResponse.raw` value carrying charged-USD + context-window
+/// metadata, or `None` when the provider reported neither (so responses from
+/// providers that don't surface billing stay `raw: None`).
+fn openhuman_usage_meta_raw(usage: Option<&UsageInfo>) -> Option<serde_json::Value> {
+ let u = usage?;
+ if u.charged_amount_usd <= 0.0 && u.context_window == 0 {
+ return None;
+ }
+ let meta = OpenhumanUsageMeta {
+ charged_amount_usd: u.charged_amount_usd,
+ context_window: u.context_window,
+ };
+ Some(serde_json::json!({ OPENHUMAN_USAGE_META_KEY: meta }))
+}
+
/// Merge the host billing/context metadata the crate [`Usage`] cannot carry into
/// a crate [`ModelResponse::raw`] under [`OPENHUMAN_USAGE_META_KEY`], so
/// [`usage_info_from_response`] recovers the charged-USD + context window from a