fix(protocol): send attachments where AstrBot reads them
Attachments went out under a top-level `attachments` key holding
camelCase objects. AstrBot's `OpenApiChatRequest` has no such field —
its properties are message, session_id, conversation_id, username,
config_id, config_name, platform_id, enable_streaming and flags,
read from the live server's own /api/v1/openapi.json — and FastAPI
ignores unknown keys instead of rejecting them. So every attachment this
client ever sent was discarded in silence while the request reported
success. The same is true of `nickname`, now removed.
Attachments belong inside `message`, which AstrBot accepts either as a
bare string or as a list of segments: {"type":"plain","text":...} and
{"type":"image"|"record"|"file"|"video","attachment_id":...}, per the
documented format at docs.astrbot.app/en/dev/openapi.html. Segment type
now follows the uploaded media type, with `file` as the fallback so an
unrecognised type still travels rather than being dropped.
This closes the outbound half of P1-9, which the queue had recorded as
externally blocked on a real container. It did not need one: the schema
is public and the server serves it unauthenticated.
It also settles how voice input will be sent — an audio upload is a
`record` segment — so B4 no longer has to guess at the contract.
Text-only requests keep the bare-string shape they always had. A test
now asserts the serialized request carries no key outside the nine the
server declares, since anything else fails by being ignored rather than
by erroring.
Protocol tests 19 -> 22. All three new assertions confirmed to fail
under a deliberate regression.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
这个提交存在于:
父节点
1e9d1c1e77
当前提交
21d85be4fa
共有 2 个文件被更改,包括 141 次插入 和 22 次删除
|
|
@ -5,7 +5,7 @@
|
|||
//! into that model without claiming full product compatibility.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use serde_json::{Value, json};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{
|
||||
|
|
@ -69,17 +69,64 @@ pub struct AstrBotChatRequest {
|
|||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub username: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub nickname: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub config_id: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub attachments: Option<Vec<AstrBotAttachment>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub platform_id: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub enable_streaming: Option<bool>,
|
||||
}
|
||||
|
||||
impl AstrBotChatRequest {
|
||||
/// Builds the `message` field for a text plus any uploaded attachments.
|
||||
///
|
||||
/// Attachments used to be sent under a top-level `attachments` key holding
|
||||
/// camelCase objects. AstrBot's `OpenApiChatRequest` has no such field —
|
||||
/// its properties are `message`, `session_id`, `conversation_id`,
|
||||
/// `username`, `config_id`, `config_name`, `platform_id`,
|
||||
/// `enable_streaming` and `flags`, confirmed against the server's own
|
||||
/// `/api/v1/openapi.json` — and unknown keys are ignored rather than
|
||||
/// rejected, so every attachment this client ever sent was dropped in
|
||||
/// silence and the request still succeeded. Attachments belong inside
|
||||
/// `message` as segments, each naming an `attachment_id` returned by the
|
||||
/// upload.
|
||||
pub fn compose_message(text: &str, attachments: &[AstrBotAttachment]) -> Value {
|
||||
if attachments.is_empty() {
|
||||
// A bare string is the documented shorthand, and the shape every
|
||||
// text-only request has always used.
|
||||
return Value::String(text.to_owned());
|
||||
}
|
||||
let mut segments = Vec::with_capacity(attachments.len() + 1);
|
||||
if !text.is_empty() {
|
||||
segments.push(json!({ "type": "plain", "text": text }));
|
||||
}
|
||||
for attachment in attachments {
|
||||
segments.push(json!({
|
||||
"type": attachment_segment_type(&attachment.mime_type),
|
||||
"attachment_id": attachment.attachment_id,
|
||||
}));
|
||||
}
|
||||
Value::Array(segments)
|
||||
}
|
||||
}
|
||||
|
||||
/// The AstrBot message segment type for an uploaded file's media type.
|
||||
///
|
||||
/// `file` is the fallback rather than an error: AstrBot accepts a generic file
|
||||
/// segment, so an unrecognised type is still delivered instead of silently
|
||||
/// dropped.
|
||||
fn attachment_segment_type(mime_type: &str) -> &'static str {
|
||||
let mime_type = mime_type.trim().to_ascii_lowercase();
|
||||
if mime_type.starts_with("image/") {
|
||||
"image"
|
||||
} else if mime_type.starts_with("audio/") {
|
||||
"record"
|
||||
} else if mime_type.starts_with("video/") {
|
||||
"video"
|
||||
} else {
|
||||
"file"
|
||||
}
|
||||
}
|
||||
|
||||
/// A model/provider configuration exposed by AstrBot.
|
||||
///
|
||||
/// Serialized towards React, so the field names must match the frontend
|
||||
|
|
@ -1110,28 +1157,102 @@ mod tests {
|
|||
assert!(a.starts_with("astrbot-"));
|
||||
}
|
||||
|
||||
/// Every key AstrBot's `OpenApiChatRequest` declares, and nothing else.
|
||||
///
|
||||
/// Taken from the live server's own `/api/v1/openapi.json`. FastAPI ignores
|
||||
/// unknown keys rather than rejecting them, so a field outside this list is
|
||||
/// discarded in silence while the request still reports success — which is
|
||||
/// exactly how attachments went undelivered for as long as they did.
|
||||
const ASTRBOT_CHAT_REQUEST_KEYS: [&str; 9] = [
|
||||
"message",
|
||||
"session_id",
|
||||
"conversation_id",
|
||||
"username",
|
||||
"config_id",
|
||||
"config_name",
|
||||
"platform_id",
|
||||
"enable_streaming",
|
||||
"flags",
|
||||
];
|
||||
|
||||
#[test]
|
||||
fn chat_request_serializes_model_and_attachment_options() {
|
||||
let request = AstrBotChatRequest {
|
||||
message: Value::String("hello".into()),
|
||||
session_id: Some("session-a".into()),
|
||||
conversation_id: None,
|
||||
username: Some("desktop".into()),
|
||||
nickname: None,
|
||||
config_id: Some("model-a".into()),
|
||||
attachments: Some(vec![AstrBotAttachment {
|
||||
attachment_id: "file-a".into(),
|
||||
file_name: "image.png".into(),
|
||||
fn attachments_are_sent_as_message_segments() {
|
||||
let attachments = [
|
||||
AstrBotAttachment {
|
||||
attachment_id: "att-image".into(),
|
||||
file_name: "a.png".into(),
|
||||
mime_type: "image/png".into(),
|
||||
size_bytes: 12,
|
||||
url: None,
|
||||
}]),
|
||||
},
|
||||
AstrBotAttachment {
|
||||
attachment_id: "att-audio".into(),
|
||||
file_name: "b.wav".into(),
|
||||
mime_type: "audio/wav".into(),
|
||||
size_bytes: 34,
|
||||
url: None,
|
||||
},
|
||||
];
|
||||
let request = AstrBotChatRequest {
|
||||
message: AstrBotChatRequest::compose_message("看这张图", &attachments),
|
||||
session_id: Some("session-a".into()),
|
||||
conversation_id: None,
|
||||
username: Some("desktop".into()),
|
||||
config_id: Some("model-a".into()),
|
||||
platform_id: None,
|
||||
enable_streaming: Some(true),
|
||||
};
|
||||
let value = serde_json::to_value(request).expect("request serializes");
|
||||
|
||||
let value = serde_json::to_value(&request).expect("request serializes");
|
||||
assert_eq!(value["config_id"], "model-a");
|
||||
assert_eq!(value["attachments"][0]["attachmentId"], "file-a");
|
||||
assert_eq!(
|
||||
value["message"],
|
||||
json!([
|
||||
{"type": "plain", "text": "看这张图"},
|
||||
{"type": "image", "attachment_id": "att-image"},
|
||||
{"type": "record", "attachment_id": "att-audio"},
|
||||
])
|
||||
);
|
||||
for key in value.as_object().expect("object").keys() {
|
||||
assert!(
|
||||
ASTRBOT_CHAT_REQUEST_KEYS.contains(&key.as_str()),
|
||||
"{key} is not a field AstrBot reads, so it would be dropped in silence"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_text_only_message_stays_a_bare_string() {
|
||||
assert_eq!(
|
||||
AstrBotChatRequest::compose_message("hello", &[]),
|
||||
json!("hello")
|
||||
);
|
||||
}
|
||||
|
||||
/// An attachment with no accompanying text carries no empty text segment.
|
||||
#[test]
|
||||
fn an_attachment_without_text_sends_only_its_own_segment() {
|
||||
let attachment = AstrBotAttachment {
|
||||
attachment_id: "att-1".into(),
|
||||
file_name: "clip.wav".into(),
|
||||
mime_type: "audio/wav".into(),
|
||||
size_bytes: 1,
|
||||
url: None,
|
||||
};
|
||||
assert_eq!(
|
||||
AstrBotChatRequest::compose_message("", std::slice::from_ref(&attachment)),
|
||||
json!([{"type": "record", "attachment_id": "att-1"}])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_segment_type_follows_the_media_type() {
|
||||
assert_eq!(attachment_segment_type("image/webp"), "image");
|
||||
assert_eq!(attachment_segment_type("AUDIO/OGG"), "record");
|
||||
assert_eq!(attachment_segment_type("video/mp4"), "video");
|
||||
// Anything else still travels, as a generic file rather than not at all.
|
||||
assert_eq!(attachment_segment_type("application/pdf"), "file");
|
||||
assert_eq!(attachment_segment_type(""), "file");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -622,13 +622,11 @@ impl AstrBotClient {
|
|||
self.set_state(ConnectionState::Syncing);
|
||||
let session_id = self.session_id.lock().await.clone();
|
||||
let request = AstrBotChatRequest {
|
||||
message: Value::String(message.to_owned()),
|
||||
message: AstrBotChatRequest::compose_message(message, &attachments),
|
||||
session_id: session_id.clone(),
|
||||
conversation_id: None,
|
||||
username: Some(self.config.remote_username.clone()),
|
||||
nickname: self.config.remote_display_name.clone(),
|
||||
config_id,
|
||||
attachments: (!attachments.is_empty()).then_some(attachments),
|
||||
platform_id: None,
|
||||
enable_streaming: Some(true),
|
||||
};
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用