Skip to content

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

feat(channels): add contract schema validation for channel controllers

Steven Enamakel6ffb0f10e21a
diff --git a/crates/openhuman-core/src/channels/contract_schema.rs b/crates/openhuman-core/src/channels/contract_schema.rs
new file mode 100644
index 0000000000..4371baee33
--- /dev/null
+++ b/crates/openhuman-core/src/channels/contract_schema.rs
@@ -0,0 +1,65 @@
+//! `tinychannels-bus` controller schemas → core [`ControllerSchema`]s.
+//!
+//! Always compiled (a dependency-free carve-out like `traits` / `cli`): the
+//! core's channel controllers use it behind the `channels` gate, and
+//! `openhuman-tinyhumans` uses it for the managed-bot link controllers it
+//! serves (`channels.telegram_login_*`, `channels.discord_link_*`), whose
+//! schemas the contract defines.
+
+use tinychannels_bus::controllers::{
+ channel_controller_schema, ChannelControllerField, ChannelControllerFieldType,
+ ChannelControllerSchema,
+};
+
+use crate::core::{ControllerSchema, FieldSchema, TypeSchema};
+
+/// The contract's schema for channel controller `function`, converted.
+pub fn contract_controller_schema(function: &str) -> ControllerSchema {
+ from_channel_controller_schema(channel_controller_schema(function))
+}
+
+/// Convert one contract schema.
+pub fn from_channel_controller_schema(schema: ChannelControllerSchema) -> ControllerSchema {
+ ControllerSchema {
+ namespace: schema.namespace,
+ function: schema.function,
+ description: schema.description,
+ inputs: schema
+ .inputs
+ .into_iter()
+ .map(from_channel_controller_field)
+ .collect(),
+ outputs: schema
+ .outputs
+ .into_iter()
+ .map(from_channel_controller_field)
+ .collect(),
+ }
+}
+
+fn from_channel_controller_field(field: ChannelControllerField) -> FieldSchema {
+ FieldSchema {
+ name: field.name,
+ ty: from_channel_controller_field_type(field.ty),
+ comment: field.comment,
+ required: field.required,
+ }
+}
+
+fn from_channel_controller_field_type(ty: ChannelControllerFieldType) -> TypeSchema {
+ match ty {
+ ChannelControllerFieldType::Bool => TypeSchema::Bool,
+ ChannelControllerFieldType::I64 => TypeSchema::I64,
+ ChannelControllerFieldType::U64 => TypeSchema::U64,
+ ChannelControllerFieldType::F64 => TypeSchema::F64,
+ ChannelControllerFieldType::String => TypeSchema::String,
+ ChannelControllerFieldType::Json => TypeSchema::Json,
+ ChannelControllerFieldType::Option(inner) => {
+ TypeSchema::Option(Box::new(from_channel_controller_field_type(*inner)))
+ }
+ }
+}
+
+#[cfg(test)]
+#[path = "contract_schema_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/channels/contract_schema_tests.rs b/crates/openhuman-core/src/channels/contract_schema_tests.rs
new file mode 100644
index 0000000000..1e463d3656
--- /dev/null
+++ b/crates/openhuman-core/src/channels/contract_schema_tests.rs
@@ -0,0 +1,17 @@
+use super::*;
+
+#[test]
+fn converts_the_contract_schema_with_optional_types() {
+ let s = contract_controller_schema("telegram_login_check");
+ assert_eq!(s.namespace, "channels");
+ assert_eq!(s.function, "telegram_login_check");
+ let token = s.inputs.iter().find(|f| f.name == "linkToken").unwrap();
+ assert!(token.required);
+ assert!(matches!(token.ty, TypeSchema::String));
+
+ let status = contract_controller_schema("status");
+ assert!(status
+ .inputs
+ .iter()
+ .any(|f| !f.required && matches!(f.ty, TypeSchema::Option(_))));
+}
diff --git a/crates/openhuman-core/src/channels/controllers/schemas.rs b/crates/openhuman-core/src/channels/controllers/schemas.rs
index 4a348c0949..8a58170f7c 100644
--- a/crates/openhuman-core/src/channels/controllers/schemas.rs
+++ b/crates/openhuman-core/src/channels/controllers/schemas.rs
@@ -12,9 +12,9 @@ use crate::rpc::RpcOutcome;
use super::backend::OpenHumanChannelBackend;
use super::definitions::ChannelAuthMode;
+use crate::channels::contract_schema::from_channel_controller_schema;
use tinychannels::controllers::{
all_channel_controller_schemas, channel_controller_schema, channel_credential_provider,
- ChannelControllerField, ChannelControllerFieldType, ChannelControllerSchema,
};
use tinychannels::{ChannelManager, ChannelsConfig};
@@ -584,47 +584,6 @@ fn to_json<T: serde::Serialize>(outcome: RpcOutcome<T>) -> Result<Value, String>
outcome.into_cli_compatible_json()
}
-fn from_channel_controller_schema(schema: ChannelControllerSchema) -> ControllerSchema {
- ControllerSchema {
- namespace: schema.namespace,
- function: schema.function,
- description: schema.description,
- inputs: schema
- .inputs
- .into_iter()
- .map(from_channel_controller_field)
- .collect(),
- outputs: schema
- .outputs
- .into_iter()
- .map(from_channel_controller_field)
- .collect(),
- }
-}
-
-fn from_channel_controller_field(field: ChannelControllerField) -> FieldSchema {
- FieldSchema {
- name: field.name,
- ty: from_channel_controller_field_type(field.ty),
- comment: field.comment,
- required: field.required,
- }
-}
-
-fn from_channel_controller_field_type(ty: ChannelControllerFieldType) -> TypeSchema {
- match ty {
- ChannelControllerFieldType::Bool => TypeSchema::Bool,
- ChannelControllerFieldType::I64 => TypeSchema::I64,
- ChannelControllerFieldType::U64 => TypeSchema::U64,
- ChannelControllerFieldType::F64 => TypeSchema::F64,
- ChannelControllerFieldType::String => TypeSchema::String,
- ChannelControllerFieldType::Json => TypeSchema::Json,
- ChannelControllerFieldType::Option(inner) => {
- TypeSchema::Option(Box::new(from_channel_controller_field_type(*inner)))
- }
- }
-}
-
#[cfg(test)]
#[path = "schemas_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/channels/mod.rs b/crates/openhuman-core/src/channels/mod.rs
index 21c68dfdea..7c970a3f62 100644
--- a/crates/openhuman-core/src/channels/mod.rs
+++ b/crates/openhuman-core/src/channels/mod.rs
@@ -9,6 +9,9 @@
//! loop (`agent::session_host::runtime::run_interactive`).
//! * `cli` — `CliChannel`, the dependency-free local stdin/stdout REPL the same
//! interactive loop drives in every build.
+//! * `contract_schema` — the `tinychannels-bus` controller-schema conversion,
+//! shared with `openhuman-tinyhumans`, which serves the managed-bot link
+//! controllers (`channels.telegram_login_*`, `channels.discord_link_*`).
//!
//! Everything else (`providers`, `host`, `controllers`, `runtime`, `bus`,
//! `proactive`, `commands`, `context`, `routes`, `relay_runtime`,
@@ -24,6 +27,7 @@
// Always-compiled carve-outs (see module docs).
pub mod cli;
+pub mod contract_schema;
pub mod traits;
pub use cli::CliChannel;