Skip to content

Commit 52830aa

Browse files
refactor(model): add input modalities field to model list (tailcallhq#2242)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 34e69d1 commit 52830aa

16 files changed

Lines changed: 602 additions & 108 deletions

File tree

‎crates/forge_app/src/dto/anthropic/response.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ pub struct Model {
2121
impl From<Model> for forge_domain::Model {
2222
fn from(value: Model) -> Self {
2323
let context_length = get_context_length(&value.id);
24+
let input_modalities = if value.id.contains("claude-3")
25+
|| value.id.contains("claude-4")
26+
|| value.id.contains("claude-sonnet")
27+
|| value.id.contains("claude-opus")
28+
|| value.id.contains("claude-haiku")
29+
{
30+
vec![
31+
forge_domain::InputModality::Text,
32+
forge_domain::InputModality::Image,
33+
]
34+
} else {
35+
vec![forge_domain::InputModality::Text]
36+
};
37+
2438
Self {
2539
id: ModelId::new(value.id),
2640
name: value.display_name,
@@ -29,6 +43,7 @@ impl From<Model> for forge_domain::Model {
2943
tools_supported: Some(true),
3044
supports_parallel_tool_calls: None,
3145
supports_reasoning: None,
46+
input_modalities,
3247
}
3348
}
3449
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use forge_domain::Transformer;
2+
3+
use crate::dto::anthropic::Request;
4+
5+
/// Transformer that capitalizes specific tool names for Anthropic
6+
/// compatibility.
7+
///
8+
/// This transformer modifies tool names to use PascalCase for certain tools:
9+
/// - `read` -> `Read`
10+
/// - `write` -> `Write`
11+
///
12+
/// When the LLM sends back tool calls, both the capitalized and lowercase
13+
/// versions are supported through alias handling in the deserialization logic.
14+
pub struct CapitalizeToolNames;
15+
16+
impl Transformer for CapitalizeToolNames {
17+
type Value = Request;
18+
19+
fn transform(&mut self, mut request: Self::Value) -> Self::Value {
20+
for tool in &mut request.tools {
21+
tool.name = match tool.name.as_str() {
22+
"read" => "Read".to_string(),
23+
"write" => "Write".to_string(),
24+
_ => tool.name.clone(),
25+
};
26+
}
27+
request
28+
}
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use forge_domain::{Context, ContextMessage, ModelId, ToolDefinition, Transformer};
34+
35+
use super::*;
36+
37+
#[test]
38+
fn test_capitalizes_read_tool() {
39+
let fixture = Context::default()
40+
.add_tool(ToolDefinition::new("read").description("Read a file"))
41+
.add_message(ContextMessage::user(
42+
"test",
43+
Some(ModelId::new("claude-3-5-sonnet-20241022")),
44+
));
45+
46+
let mut request = Request::try_from(fixture).unwrap();
47+
request = CapitalizeToolNames.transform(request);
48+
49+
assert_eq!(request.tools[0].name, "Read");
50+
}
51+
52+
#[test]
53+
fn test_capitalizes_write_tool() {
54+
let fixture = Context::default()
55+
.add_tool(ToolDefinition::new("write").description("Write a file"))
56+
.add_message(ContextMessage::user(
57+
"test",
58+
Some(ModelId::new("claude-3-5-sonnet-20241022")),
59+
));
60+
61+
let mut request = Request::try_from(fixture).unwrap();
62+
request = CapitalizeToolNames.transform(request);
63+
64+
assert_eq!(request.tools[0].name, "Write");
65+
}
66+
67+
#[test]
68+
fn test_leaves_other_tools_unchanged() {
69+
let fixture = Context::default()
70+
.add_tool(ToolDefinition::new("shell").description("Execute shell command"))
71+
.add_tool(ToolDefinition::new("fs_search").description("Search files"))
72+
.add_message(ContextMessage::user(
73+
"test",
74+
Some(ModelId::new("claude-3-5-sonnet-20241022")),
75+
));
76+
77+
let mut request = Request::try_from(fixture).unwrap();
78+
request = CapitalizeToolNames.transform(request);
79+
80+
assert_eq!(request.tools[0].name, "shell");
81+
assert_eq!(request.tools[1].name, "fs_search");
82+
}
83+
84+
#[test]
85+
fn test_handles_multiple_tools_including_read_and_write() {
86+
let fixture = Context::default()
87+
.add_tool(ToolDefinition::new("read").description("Read a file"))
88+
.add_tool(ToolDefinition::new("write").description("Write a file"))
89+
.add_tool(ToolDefinition::new("shell").description("Execute shell command"))
90+
.add_message(ContextMessage::user(
91+
"test",
92+
Some(ModelId::new("claude-3-5-sonnet-20241022")),
93+
));
94+
95+
let mut request = Request::try_from(fixture).unwrap();
96+
request = CapitalizeToolNames.transform(request);
97+
98+
assert_eq!(request.tools[0].name, "Read");
99+
assert_eq!(request.tools[1].name, "Write");
100+
assert_eq!(request.tools[2].name, "shell");
101+
}
102+
103+
#[test]
104+
fn test_handles_empty_tools_list() {
105+
let fixture = Context::default().add_message(ContextMessage::user(
106+
"test",
107+
Some(ModelId::new("claude-3-5-sonnet-20241022")),
108+
));
109+
110+
let mut request = Request::try_from(fixture).unwrap();
111+
request = CapitalizeToolNames.transform(request);
112+
113+
assert!(request.tools.is_empty());
114+
}
115+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
mod auth_system_message;
2+
mod capitalize_tool_names;
23
mod drop_invalid_toolcalls;
34
mod reasoning_transform;
45
mod set_cache;
56

67
pub use auth_system_message::AuthSystemMessage;
8+
pub use capitalize_tool_names::CapitalizeToolNames;
79
pub use drop_invalid_toolcalls::DropInvalidToolUse;
810
pub use reasoning_transform::ReasoningTransform;
911
pub use set_cache::SetCache;

‎crates/forge_app/src/dto/openai/model.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ pub struct Architecture {
4747
pub modality: String,
4848
pub tokenizer: String,
4949
pub instruct_type: Option<String>,
50+
pub input_modalities: Option<Vec<String>>,
51+
pub output_modalities: Option<Vec<String>>,
5052
}
5153

5254
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
@@ -97,6 +99,19 @@ impl From<Model> for forge_domain::Model {
9799
.map(|params| params.iter().any(|p| p == name))
98100
};
99101

102+
// Parse input modalities from OpenRouter's input_modalities field
103+
let input_modalities = value
104+
.architecture
105+
.as_ref()
106+
.and_then(|arch| arch.input_modalities.as_ref())
107+
.map(|modalities| {
108+
modalities
109+
.iter()
110+
.filter_map(|s| s.parse::<forge_domain::InputModality>().ok())
111+
.collect::<Vec<_>>()
112+
})
113+
.unwrap_or_else(|| vec![forge_domain::InputModality::Text]);
114+
100115
let tools_supported = has_param("tools");
101116
let supports_parallel_tool_calls = has_param("supports_parallel_tool_calls");
102117
let supports_reasoning = has_param("reasoning");
@@ -109,6 +124,7 @@ impl From<Model> for forge_domain::Model {
109124
tools_supported,
110125
supports_parallel_tool_calls,
111126
supports_reasoning,
127+
input_modalities,
112128
}
113129
}
114130
}

‎crates/forge_domain/src/model.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ use derive_more::derive::Display;
22
use derive_setters::Setters;
33
use schemars::JsonSchema;
44
use serde::{Deserialize, Serialize};
5+
use strum_macros::EnumString;
6+
7+
/// Represents input modalities that a model can accept
8+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, EnumString)]
9+
#[serde(rename_all = "lowercase")]
10+
#[strum(serialize_all = "lowercase", ascii_case_insensitive)]
11+
pub enum InputModality {
12+
/// Text input (all models support this)
13+
Text,
14+
/// Image input (vision-capable models)
15+
Image,
16+
}
17+
18+
/// Default input modalities when not specified (text-only)
19+
fn default_input_modalities() -> Vec<InputModality> {
20+
vec![InputModality::Text]
21+
}
522

623
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, Setters)]
724
pub struct Model {
@@ -15,6 +32,9 @@ pub struct Model {
1532
pub supports_parallel_tool_calls: Option<bool>,
1633
/// Whether the model supports reasoning
1734
pub supports_reasoning: Option<bool>,
35+
/// Input modalities supported by the model (defaults to text-only)
36+
#[serde(default = "default_input_modalities")]
37+
pub input_modalities: Vec<InputModality>,
1838
}
1939

2040
#[derive(Default, Debug, Clone, Serialize, Deserialize)]

‎crates/forge_domain/src/tools/catalog.rs‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ use crate::{ToolCallArguments, ToolCallFull, ToolDefinition, ToolDescription, To
3838
#[strum_discriminants(name(ToolKind))]
3939
#[strum(serialize_all = "snake_case")]
4040
pub enum ToolCatalog {
41+
#[serde(alias = "Read")]
4142
Read(FSRead),
4243
ReadImage(ReadImage),
44+
#[serde(alias = "Write")]
4345
Write(FSWrite),
4446
FsSearch(FSSearch),
4547
SemSearch(SemanticSearch),
@@ -925,6 +927,107 @@ mod tests {
925927
}
926928
}
927929

930+
#[test]
931+
fn test_capitalized_read_alias() {
932+
use crate::{ToolCallArguments, ToolCallFull};
933+
934+
// Test that "Read" (capitalized) is normalized to "read"
935+
let tool_call = ToolCallFull {
936+
name: ToolName::new("Read"),
937+
call_id: None,
938+
arguments: ToolCallArguments::from_json(
939+
r#"{"path": "/test/path.rs", "start_line": 10, "end_line": 20}"#,
940+
),
941+
};
942+
943+
let actual = ToolCatalog::try_from(tool_call);
944+
945+
assert!(
946+
actual.is_ok(),
947+
"Should successfully parse capitalized 'Read' tool name"
948+
);
949+
950+
if let Ok(ToolCatalog::Read(fs_read)) = actual {
951+
assert_eq!(fs_read.path, "/test/path.rs");
952+
assert_eq!(fs_read.start_line, Some(10));
953+
assert_eq!(fs_read.end_line, Some(20));
954+
} else {
955+
panic!("Expected FSRead variant");
956+
}
957+
}
958+
959+
#[test]
960+
fn test_capitalized_write_alias() {
961+
use crate::{ToolCallArguments, ToolCallFull};
962+
963+
// Test that "Write" (capitalized) is normalized to "write"
964+
let tool_call = ToolCallFull {
965+
name: ToolName::new("Write"),
966+
call_id: None,
967+
arguments: ToolCallArguments::from_json(
968+
r#"{"path": "/test/path.rs", "content": "test content"}"#,
969+
),
970+
};
971+
972+
let actual = ToolCatalog::try_from(tool_call);
973+
974+
assert!(
975+
actual.is_ok(),
976+
"Should successfully parse capitalized 'Write' tool name"
977+
);
978+
979+
if let Ok(ToolCatalog::Write(fs_write)) = actual {
980+
assert_eq!(fs_write.path, "/test/path.rs");
981+
assert_eq!(fs_write.content, "test content");
982+
} else {
983+
panic!("Expected FSWrite variant");
984+
}
985+
}
986+
987+
#[test]
988+
fn test_lowercase_read_still_works() {
989+
use crate::{ToolCallArguments, ToolCallFull};
990+
991+
// Ensure lowercase still works (backward compatibility)
992+
let tool_call = ToolCallFull {
993+
name: ToolName::new("read"),
994+
call_id: None,
995+
arguments: ToolCallArguments::from_json(r#"{"path": "/test/path.rs"}"#),
996+
};
997+
998+
let actual = ToolCatalog::try_from(tool_call);
999+
1000+
assert!(
1001+
actual.is_ok(),
1002+
"Should successfully parse lowercase 'read' tool name"
1003+
);
1004+
1005+
matches!(actual.unwrap(), ToolCatalog::Read(_));
1006+
}
1007+
1008+
#[test]
1009+
fn test_lowercase_write_still_works() {
1010+
use crate::{ToolCallArguments, ToolCallFull};
1011+
1012+
// Ensure lowercase still works (backward compatibility)
1013+
let tool_call = ToolCallFull {
1014+
name: ToolName::new("write"),
1015+
call_id: None,
1016+
arguments: ToolCallArguments::from_json(
1017+
r#"{"path": "/test/path.rs", "content": "test"}"#,
1018+
),
1019+
};
1020+
1021+
let actual = ToolCatalog::try_from(tool_call);
1022+
1023+
assert!(
1024+
actual.is_ok(),
1025+
"Should successfully parse lowercase 'write' tool name"
1026+
);
1027+
1028+
matches!(actual.unwrap(), ToolCatalog::Write(_));
1029+
}
1030+
9281031
#[test]
9291032
fn test_fs_search_message_with_regex() {
9301033
use std::path::PathBuf;

‎crates/forge_main/src/model.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ impl SlashCommand {
562562
#[cfg(test)]
563563
mod tests {
564564
use console::strip_ansi_codes;
565-
use forge_api::{ModelId, ModelSource, ProviderId, ProviderResponse};
565+
use forge_api::{InputModality, ModelId, ModelSource, ProviderId, ProviderResponse};
566566
use forge_domain::{AnyProvider, Provider};
567567
use pretty_assertions::assert_eq;
568568
use url::Url;
@@ -926,6 +926,7 @@ mod tests {
926926
tools_supported,
927927
supports_parallel_tool_calls: None,
928928
supports_reasoning: None,
929+
input_modalities: vec![InputModality::Text],
929930
}
930931
}
931932

‎crates/forge_main/src/ui.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,19 @@ impl<A: API + 'static, F: Fn() -> A + Send + Sync> UI<A, F> {
10851085
} else {
10861086
info = info.add_key_value("Tools", markers::EMPTY)
10871087
}
1088+
1089+
// Add image modality support indicator
1090+
let supports_image = model
1091+
.input_modalities
1092+
.contains(&forge_domain::InputModality::Image);
1093+
info = info.add_key_value(
1094+
"Image",
1095+
if supports_image {
1096+
status::YES
1097+
} else {
1098+
status::NO
1099+
},
1100+
);
10881101
}
10891102

10901103
if porcelain {

0 commit comments

Comments
 (0)