mirror of
https://github.com/lWolvesl/claw-code.git
synced 2026-04-02 16:11:52 +08:00
Enable local image prompts without breaking text-only CLI flows
The Rust CLI now recognizes explicit local image references in prompt text, encodes supported image files as base64, and serializes mixed text/image content blocks for the API. The request conversion path was kept narrow so existing runtime/session structures remain stable while prompt mode and user text conversion gain multimodal support. Constraint: Must support PNG, JPG/JPEG, GIF, and WebP without adding broad runtime abstractions Constraint: Existing text-only prompt behavior and API tool flows must keep working unchanged Rejected: Add only explicit --image CLI flags | does not satisfy auto-detect image refs in prompt text Rejected: Persist native image blocks in runtime session model | broader refactor than needed for prompt support Confidence: high Scope-risk: moderate Reversibility: clean Directive: Keep image parsing scoped to outbound user prompt adaptation unless session persistence truly needs multimodal history Tested: cargo fmt --all; cargo clippy --workspace --all-targets -- -D warnings; cargo test --workspace Not-tested: Live remote multimodal request against Anthropic API
This commit is contained in:
@@ -4,8 +4,8 @@ use std::time::Duration;
|
||||
|
||||
use api::{
|
||||
AnthropicClient, ApiError, ContentBlockDelta, ContentBlockDeltaEvent, ContentBlockStartEvent,
|
||||
InputContentBlock, InputMessage, MessageDeltaEvent, MessageRequest, OutputContentBlock,
|
||||
StreamEvent, ToolChoice, ToolDefinition,
|
||||
ImageSource, InputContentBlock, InputMessage, MessageDeltaEvent, MessageRequest,
|
||||
OutputContentBlock, StreamEvent, ToolChoice, ToolDefinition,
|
||||
};
|
||||
use serde_json::json;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
@@ -75,6 +75,39 @@ async fn send_message_posts_json_and_parses_response() {
|
||||
assert_eq!(body["tool_choice"]["type"], json!("auto"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_content_blocks_serialize_with_base64_source() {
|
||||
let request = MessageRequest {
|
||||
model: "claude-3-7-sonnet-latest".to_string(),
|
||||
max_tokens: 64,
|
||||
messages: vec![InputMessage {
|
||||
role: "user".to_string(),
|
||||
content: vec![InputContentBlock::Image {
|
||||
source: ImageSource {
|
||||
kind: "base64".to_string(),
|
||||
media_type: "image/png".to_string(),
|
||||
data: "AQID".to_string(),
|
||||
},
|
||||
}],
|
||||
}],
|
||||
system: None,
|
||||
tools: None,
|
||||
tool_choice: None,
|
||||
stream: false,
|
||||
};
|
||||
|
||||
let json = serde_json::to_value(request).expect("request should serialize");
|
||||
assert_eq!(json["messages"][0]["content"][0]["type"], json!("image"));
|
||||
assert_eq!(
|
||||
json["messages"][0]["content"][0]["source"],
|
||||
json!({
|
||||
"type": "base64",
|
||||
"media_type": "image/png",
|
||||
"data": "AQID"
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn stream_message_parses_sse_events_with_tool_use() {
|
||||
let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
|
||||
|
||||
Reference in New Issue
Block a user