Skip to content

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

feat(tinymemes): add search and inference callbacks

Steven Enamakel7e9aa1855dd0
diff --git a/crates/openhuman-core/src/tinymemes/callbacks.rs b/crates/openhuman-core/src/tinymemes/callbacks.rs
index ffbed0d9e9..e3623c3b8e 100644
--- a/crates/openhuman-core/src/tinymemes/callbacks.rs
+++ b/crates/openhuman-core/src/tinymemes/callbacks.rs
@@ -44,14 +44,23 @@ pub(super) struct Session {
}
impl Session {
- pub(super) fn new(config: &Config) -> Self {
+ /// The dispatch context the registering caller runs under; the host's own
+ /// when it had none (a single-user process), which fails closed in SaaS.
+ pub(super) fn dispatch_ctx(&self) -> DispatchCtx {
+ match self.context.clone() {
+ Some(context) => DispatchCtx::new(context),
+ None => DispatchCtx::host_default(),
+ }
+ }
+
+ pub(super) fn new(ctx: &DispatchCtx, config: &Config) -> Self {
Self {
config: config.clone(),
jev: tokio::sync::OnceCell::new(),
- // Only the caller's own task scope is carried. Without one, a
+ // Only the caller's own context is carried. Without one, a
// single-user process falls back to its default context on the
// callback side as anywhere else, and SaaS fails closed.
- context: CoreContext::scoped(),
+ context: ctx.core().cloned(),
}
}
@@ -105,14 +114,14 @@ impl Drop for Registration {
}
/// Register a remix for `config`'s workspace under a fresh correlation id.
-pub(super) fn register(config: &Config) -> Registration {
+pub(super) fn register(ctx: &DispatchCtx, config: &Config) -> Registration {
let correlation = uuid::Uuid::new_v4().to_string();
let mut sessions = registry();
sessions.retain(|_, entry| entry.registered.elapsed() < MAX_AGE);
sessions.insert(
correlation.clone(),
Entry {
- session: Arc::new(Session::new(config)),
+ session: Arc::new(Session::new(ctx, config)),
registered: Instant::now(),
},
);
@@ -198,7 +207,7 @@ async fn answer(session: &Session, member: &str, payload: Value) -> Result<Value
}
async fn complete(session: &Session, request: CompleteRequest) -> HostResult<String> {
- inference::complete(&session.config, &request.system, &request.user)
+ inference::complete(&session.dispatch_ctx(), &session.config, &request.system, &request.user)
.await
.map_err(|error| {
log::warn!("[tinymemes] complete callback failed: {error}");
@@ -232,7 +241,7 @@ async fn search(
session: &Session,
request: SearchRequest,
) -> HostResult<Vec<tinymemes_bus::SearchResult>> {
- let Some(backend) = search::OpenHumanSearch::available(&session.config) else {
+ let Some(backend) = search::OpenHumanSearch::available(&session.dispatch_ctx(), &session.config) else {
return Err(HostFailure::Unsupported);
};
backend
diff --git a/crates/openhuman-core/src/tinymemes/inference.rs b/crates/openhuman-core/src/tinymemes/inference.rs
index 5d536e765a..214ec21fdb 100644
--- a/crates/openhuman-core/src/tinymemes/inference.rs
+++ b/crates/openhuman-core/src/tinymemes/inference.rs
@@ -10,6 +10,7 @@ use tinyinference_llm::message::Message;
use tinyinference_llm::model::ModelRequest;
use crate::config::Config;
+use crate::core::runtime::DispatchCtx;
use crate::inference::provider;
/// Workload role whose provider tinymemes borrows.
@@ -17,10 +18,15 @@ pub(crate) const ROLE: &str = "summarization";
const TEMPERATURE: f64 = 0.7;
/// One system + user completion on the `summarization` provider.
-pub(crate) async fn complete(config: &Config, system: &str, user: &str) -> Result<String, String> {
+pub(crate) async fn complete(
+ ctx: &DispatchCtx,
+ config: &Config,
+ system: &str,
+ user: &str,
+) -> Result<String, String> {
// Built per call so a provider change in Settings applies without a
// restart; construction is cheap and synchronous.
- let (model, resolved) = provider::create_chat_model_with_model_id(ROLE, config, TEMPERATURE)
+ let (model, resolved) = provider::create_chat_model_with_model_id_in(ctx, ROLE, config, TEMPERATURE)
.map_err(|e| format!("openhuman inference unavailable: {e}"))?;
// Reasoning off for tinymemes' calls only (the rest of OpenHuman keeps
// its configured reasoning). The managed backend honours the
@@ -63,8 +69,8 @@ pub(crate) fn routes_to_openrouter(config: &Config) -> bool {
}
/// The model id OpenHuman would use for tinymemes, for the startup log line.
-pub(crate) fn resolved_model(config: &Config) -> String {
- provider::create_chat_model_with_model_id(ROLE, config, TEMPERATURE)
+pub(crate) fn resolved_model(ctx: &DispatchCtx, config: &Config) -> String {
+ provider::create_chat_model_with_model_id_in(ctx, ROLE, config, TEMPERATURE)
.map(|(_, id)| id)
.unwrap_or_else(|e| format!("unavailable ({e})"))
}
diff --git a/crates/openhuman-core/src/tinymemes/mod.rs b/crates/openhuman-core/src/tinymemes/mod.rs
index 4471aaaee8..3aea1413da 100644
--- a/crates/openhuman-core/src/tinymemes/mod.rs
+++ b/crates/openhuman-core/src/tinymemes/mod.rs
@@ -78,7 +78,7 @@ struct ModuleRemixer<'a> {
#[async_trait]
impl Remixer for ModuleRemixer<'_> {
async fn remix(&self, request: RemixRequest) -> Result<RemixResponse, String> {
- service::remix(self.config, request).await
+ service::remix(self.ctx, self.config, request).await
}
}
@@ -143,10 +143,10 @@ async fn remix_final_reply(
log_outcome(&turn, started, "history_unavailable", None);
return None;
};
- let remixer = ModuleRemixer { config };
+ let remixer = ModuleRemixer { ctx, config };
remix_with(
&remixer,
- options(config),
+ options(ctx, config),
&messages,
&turn,
started,
@@ -212,11 +212,11 @@ async fn load(
/// What the host decides for the module: the meme cooldown
/// (`OPENHUMAN_TINYMEMES_MEME_COOLDOWN` over `[memes] meme_cooldown`) and
/// whether web search can serve research at all.
-fn options(config: &Config) -> RemixOptions {
+fn options(ctx: &DispatchCtx, config: &Config) -> RemixOptions {
options_with(
std::env::var(COOLDOWN_ENV).ok().as_deref(),
config.memes.meme_cooldown,
- search::OpenHumanSearch::available(config).is_some(),
+ search::OpenHumanSearch::available(ctx, config).is_some(),
)
}
diff --git a/crates/openhuman-core/src/tinymemes/search.rs b/crates/openhuman-core/src/tinymemes/search.rs
index 5d9c81fe75..694fdda47b 100644
--- a/crates/openhuman-core/src/tinymemes/search.rs
+++ b/crates/openhuman-core/src/tinymemes/search.rs
@@ -7,16 +7,18 @@
use serde_json::json;
use crate::config::Config;
+use crate::core::runtime::DispatchCtx;
const TOOL: &str = tinysearch_bus::tools::WEB_SEARCH;
pub(crate) struct OpenHumanSearch {
+ ctx: DispatchCtx,
config: Config,
}
impl OpenHumanSearch {
/// A search backend, or `None` when OpenHuman cannot search right now.
- pub(crate) fn available(config: &Config) -> Option<Self> {
+ pub(crate) fn available(ctx: &DispatchCtx, config: &Config) -> Option<Self> {
if !crate::search::is_active(config) {
return None;
}
@@ -29,6 +31,7 @@ impl OpenHumanSearch {
return None;
}
Some(Self {
+ ctx: ctx.clone(),
config: config.clone(),
})
}
@@ -46,7 +49,7 @@ impl OpenHumanSearch {
arguments: json!({ "query": query, "max_results": max_results.clamp(1, 20) }),
};
let response = crate::modules::search::execute_tool(
- &crate::core::runtime::DispatchCtx::ambient(),
+ &self.ctx,
&self.config,
request,
)
diff --git a/crates/openhuman-core/src/tinymemes/service.rs b/crates/openhuman-core/src/tinymemes/service.rs
index 79506c3112..dadd69a5a3 100644
--- a/crates/openhuman-core/src/tinymemes/service.rs
+++ b/crates/openhuman-core/src/tinymemes/service.rs
@@ -116,6 +116,7 @@ fn scope_lock(config: &Config) -> std::sync::Arc<tokio::sync::Mutex<()>> {
/// Remix one reply through the loaded module, serving its callbacks for
/// `config`'s workspace meanwhile. `request.correlation` is set here.
pub(crate) async fn remix(
+ ctx: &crate::core::runtime::DispatchCtx,
config: &Config,
mut request: RemixRequest,
) -> Result<RemixResponse, String> {
@@ -130,7 +131,7 @@ pub(crate) async fn remix(
.connection()
.proxy(STATE_INTERFACE, REMIX_PATH, REMIX_INTERFACE)
.map_err(|error| error.to_string())?;
- let registration = callbacks::register(config);
+ let registration = callbacks::register(ctx, config);
request.correlation = registration.correlation().to_owned();
// A disabled request makes no callbacks and starts no research, so there
// is nothing to keep the registration for.