Skip to content

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

fix(attachments): cache fallback readouts and drop untrusted metadata

Steven Enamakel07bef8ec4778
diff --git a/crates/openhuman-core/src/attachments/provider_fallback.rs b/crates/openhuman-core/src/attachments/provider_fallback.rs
index acfef34063..c9031fc09b 100644
--- a/crates/openhuman-core/src/attachments/provider_fallback.rs
+++ b/crates/openhuman-core/src/attachments/provider_fallback.rs
@@ -212,7 +212,7 @@ impl AttachmentModel {
Ok((self.limit_context(text), cacheable))
}
- fn limit_context(&self, mut text: String) -> String {
+ pub(super) fn limit_context(&self, mut text: String) -> String {
let (_, _, limit) = self.config.multimodal_files.effective_limits();
if text.chars().count() > limit {
text = text.chars().take(limit).collect();
@@ -221,7 +221,7 @@ impl AttachmentModel {
text
}
- async fn image_readout(&self, mime: &str, bytes: &[u8]) -> tinyinference_llm::Result<String> {
+ pub(super) async fn image_readout(&self, mime: &str, bytes: &[u8]) -> tinyinference_llm::Result<String> {
if bytes.is_empty() {
return Err(tinyinference_llm::Error::Model(
"image reference has no bytes".into(),
diff --git a/crates/openhuman-core/src/attachments/provider_owner.rs b/crates/openhuman-core/src/attachments/provider_owner.rs
index 6a1fbbe0eb..7d5a9d3659 100644
--- a/crates/openhuman-core/src/attachments/provider_owner.rs
+++ b/crates/openhuman-core/src/attachments/provider_owner.rs
@@ -117,16 +117,19 @@ impl MediaFallbackRenderer<AttachmentAccessScope> for AttachmentModel {
attachment: &ResolvedAttachment,
) -> Result<Option<String>, String> {
if modality == InputModality::Image {
+ let key = self.fallback_key(&attachment.name, &attachment.mime, &attachment.bytes);
+ if let Some(text) = self.cached_fallback(&attachment.name, &key, _scope).await {
+ return Ok(Some(text));
+ }
let text = format!(
- "[Attachment: {}; {} bytes; workspace path: {}]\nImage readout:\n{}",
- attachment.mime,
- attachment.size_bytes,
- attachment.name,
+ "Image readout (untrusted):\n{}",
self.image_readout(&attachment.mime, &attachment.bytes)
.await
.map_err(|error| error.to_string())?
);
- return Ok(Some(self.limit_context(text)));
+ let text = self.limit_context(text);
+ self.save_fallback(&attachment.name, &key, &text, _scope).await;
+ return Ok(Some(text));
}
#[cfg(feature = "documents")]
if matches!(attachment.mime.as_str(), "application/pdf"
@@ -134,8 +137,21 @@ impl MediaFallbackRenderer<AttachmentAccessScope> for AttachmentModel {
| "application/vnd.openxmlformats-officedocument.presentationml.presentation"
| "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
- let (text, cacheable) = self.document_fallback(attachment).await?;
- return Ok(cacheable.then_some(text));
+ let key = self.fallback_key(&attachment.name, &attachment.mime, &attachment.bytes);
+ if let Some(text) = self.cached_fallback(&attachment.name, &key, _scope).await {
+ return Ok(Some(text));
+ }
+ let (text, cacheable) = self.fallback(
+ modality,
+ &attachment.name,
+ &attachment.mime,
+ &attachment.bytes,
+ ).await.map_err(|error| error.to_string())?;
+ let body = text.split_once('\n').map(|(_, body)| body.to_owned()).unwrap_or(text);
+ if cacheable {
+ self.save_fallback(&attachment.name, &key, &body, _scope).await;
+ }
+ return Ok(Some(body));
}
Ok(None)
}