Skip to content

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

feat(tinyhumans): forward transport context on channel delivery

Steven Enamakel47ed72d2af68
diff --git a/crates/openhuman-tinyhumans/src/channels/delivery.rs b/crates/openhuman-tinyhumans/src/channels/delivery.rs
index 7c0323bfa9..50bde14499 100644
--- a/crates/openhuman-tinyhumans/src/channels/delivery.rs
+++ b/crates/openhuman-tinyhumans/src/channels/delivery.rs
@@ -8,7 +8,8 @@ use openhuman_embed::__host::channel_callback::ChannelCallbackCtx;
use openhuman_embed::__host::security::credentials::session_support::{
get_session_token, is_local_session_token, BackendCredential,
};
-use serde_json::json;
+use reqwest::Method;
+use serde_json::{json, Map, Value};
use tinychannels_bus::module_progressive::{
ProgressiveTransportRequest, ProgressiveTransportResult,
};
@@ -28,10 +29,58 @@ pub(super) fn map_backend_error(error: anyhow::Error) -> ProgressiveTransportRes
fn request_channel(request: &ProgressiveTransportRequest) -> &str {
match request {
- ProgressiveTransportRequest::Send { channel, .. }
+ ProgressiveTransportRequest::OutputEvent { channel, .. }
+ | ProgressiveTransportRequest::Send { channel, .. }
+ | ProgressiveTransportRequest::SendTyped { channel, .. }
| ProgressiveTransportRequest::Edit { channel, .. }
| ProgressiveTransportRequest::Delete { channel, .. }
- | ProgressiveTransportRequest::Typing { channel } => channel,
+ | ProgressiveTransportRequest::Typing { channel, .. } => channel,
+ }
+}
+
+fn transport_context(
+ turn_id: Option<String>,
+ channel_ref: Option<tinychannels_bus::ChannelRef>,
+ conversation: Option<tinychannels_bus::ConversationRef>,
+ sequence: Option<u64>,
+ idempotency_key: Option<String>,
+ receipt: Option<tinychannels_bus::MessageReceipt>,
+ segment_index: Option<u32>,
+) -> Map<String, Value> {
+ let mut context = Map::new();
+ if let Some(value) = turn_id {
+ context.insert("turnId".into(), json!(value));
+ }
+ if let Some(value) = channel_ref {
+ context.insert("channelRef".into(), json!(value));
+ }
+ if let Some(value) = conversation {
+ context.insert("conversation".into(), json!(value));
+ }
+ if let Some(value) = sequence {
+ context.insert("sequence".into(), json!(value));
+ }
+ if let Some(value) = idempotency_key {
+ context.insert("idempotencyKey".into(), json!(value));
+ }
+ if let Some(value) = receipt {
+ context.insert("receipt".into(), json!(value));
+ }
+ if let Some(value) = segment_index {
+ context.insert("segmentIndex".into(), json!(value));
+ }
+ context
+}
+
+fn attach_transport_context(mut body: Value, context: Map<String, Value>) -> Value {
+ if context.is_empty() {
+ return body;
+ }
+ if let Some(object) = body.as_object_mut() {
+ object.insert("transportContext".into(), Value::Object(context));
+ body
+ } else {
+ json!({"message": body, "transportContext": context})
}
}
@@ -86,37 +135,156 @@ async fn send(
request: ProgressiveTransportRequest,
) -> ProgressiveTransportResult {
let result = match request {
- ProgressiveTransportRequest::Send { channel, message } => {
+ ProgressiveTransportRequest::OutputEvent { .. } => {
+ // This callback performs hosted REST operations; it is not the
+ // observation/event subscriber described by Acknowledged.
+ return ProgressiveTransportResult::Failed;
+ }
+ ProgressiveTransportRequest::Send {
+ channel,
+ turn_id,
+ channel_ref,
+ conversation,
+ sequence,
+ idempotency_key,
+ message,
+ } => {
+ let context = transport_context(
+ turn_id,
+ channel_ref,
+ conversation,
+ sequence,
+ idempotency_key,
+ None,
+ None,
+ );
client
- .send_channel_message(&channel, credential.clone(), message)
+ .send_channel_message(
+ &channel,
+ credential.clone(),
+ attach_transport_context(message, context),
+ )
+ .await
+ }
+ ProgressiveTransportRequest::SendTyped {
+ channel,
+ turn_id,
+ channel_ref,
+ conversation,
+ sequence,
+ segment_index,
+ idempotency_key,
+ parts,
+ text,
+ } => {
+ let context = transport_context(
+ turn_id,
+ channel_ref,
+ conversation,
+ sequence,
+ idempotency_key,
+ None,
+ segment_index,
+ );
+ client
+ .send_channel_message(
+ &channel,
+ credential.clone(),
+ attach_transport_context(json!({"text": text, "parts": parts}), context),
+ )
.await
}
ProgressiveTransportRequest::Edit {
channel,
+ turn_id,
+ channel_ref,
+ conversation,
message_id,
+ receipt,
text,
} => {
+ let context = transport_context(
+ turn_id,
+ channel_ref,
+ conversation,
+ None,
+ None,
+ receipt,
+ None,
+ );
client
.send_channel_edit(
&channel,
&message_id,
credential.clone(),
- json!({"text": text}),
+ attach_transport_context(json!({"text": text}), context),
)
.await
}
ProgressiveTransportRequest::Delete {
channel,
+ turn_id,
+ channel_ref,
+ conversation,
message_id,
+ receipt,
} => {
- client
- .send_channel_delete(&channel, &message_id, credential.clone())
- .await
+ let context = transport_context(
+ turn_id,
+ channel_ref,
+ conversation,
+ None,
+ None,
+ receipt,
+ None,
+ );
+ if context.is_empty() {
+ client
+ .send_channel_delete(&channel, &message_id, credential.clone())
+ .await
+ } else {
+ let channel = urlencoding::encode(channel.trim().trim_matches('/'));
+ let message_id = urlencoding::encode(&message_id);
+ client
+ .authed_json(
+ credential.clone(),
+ Method::DELETE,
+ &format!("channels/{channel}/messages/{message_id}"),
+ Some(json!({"transportContext": context})),
+ )
+ .await
+ }
}
- ProgressiveTransportRequest::Typing { channel } => {
- client
- .send_channel_typing(&channel, credential.clone())
- .await
+ ProgressiveTransportRequest::Typing {
+ channel,
+ turn_id,
+ channel_ref,
+ conversation,
+ } => {
+ let context = transport_context(
+ turn_id,
+ channel_ref,
+ conversation,
+ None,
+ None,
+ None,
+ None,
+ );
+ if context.is_empty() {
+ client
+ .send_channel_typing(&channel, credential.clone())
+ .await
+ } else {
+ let channel = urlencoding::encode(channel.trim().trim_matches('/'));
+ client
+ .authed_json(
+ credential.clone(),
+ Method::POST,
+ &format!("channels/{channel}/typing"),
+ Some(json!({"transportContext": context})),
+ )
+ .await
+ }
}
};
match result {