codewhale_models/
tool_projection.rs1use serde_json::Value;
2
3use crate::schema_sanitize::{
4 KimiParameterSchemaError, sanitize_for_kimi_parameters, sanitize_for_responses,
5 sanitize_for_strict, strict_schema_supported,
6};
7
8pub const TOOL_PROJECTION_WARNING_MAX_NAMES: usize = 8;
9pub const TOOL_PROJECTION_WARNING_MAX_NAME_CHARS: usize = 64;
10
11#[must_use]
17pub fn bounded_tool_projection_warning_names(omitted_tool_names: &[String]) -> Vec<String> {
18 omitted_tool_names
19 .iter()
20 .take(TOOL_PROJECTION_WARNING_MAX_NAMES)
21 .map(|name| {
22 let cleaned: String = name
23 .chars()
24 .filter(|c| !c.is_control() && !codewhale_sanitize::sanitize::is_bidi_control(*c))
25 .collect();
26 let collapsed = cleaned.split_whitespace().collect::<Vec<_>>().join(" ");
27 let collapsed = if collapsed.is_empty() {
28 "<unnamed>".to_string()
29 } else {
30 collapsed
31 };
32 if collapsed.chars().count() <= TOOL_PROJECTION_WARNING_MAX_NAME_CHARS {
33 return collapsed;
34 }
35 let mut out: String = collapsed
36 .chars()
37 .take(TOOL_PROJECTION_WARNING_MAX_NAME_CHARS - 1)
38 .collect();
39 out.push('…');
40 out
41 })
42 .collect()
43}
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub enum RouteToolProjection {
47 Exact { strict_honored: bool },
48 Relaxed { strict_honored: bool },
49 RelaxedNonStrict,
50 Mfjs { strict_honored: bool },
51}
52
53#[derive(Debug, Clone, PartialEq)]
54pub enum ToolSchemaProjection {
55 Exact {
56 parameters: Value,
57 strict: Option<bool>,
58 constraint_note: Option<String>,
59 },
60 Relaxed {
61 parameters: Value,
62 strict: Option<bool>,
63 constraint_note: Option<String>,
64 },
65 Omitted {
66 reason: KimiParameterSchemaError,
67 },
68}
69
70fn non_strict_flag(strict_intent: Option<bool>, strict_honored: bool) -> Option<bool> {
71 match (strict_intent, strict_honored) {
72 (_, false) => None,
73 (Some(true), true) => None,
74 (other, true) => other,
75 }
76}
77
78fn projected(
79 input_schema: &Value,
80 strict_intent: Option<bool>,
81 parameters: Value,
82 strict: Option<bool>,
83 constraint_note: Option<String>,
84) -> ToolSchemaProjection {
85 if parameters == *input_schema && strict == strict_intent && constraint_note.is_none() {
86 ToolSchemaProjection::Exact {
87 parameters,
88 strict,
89 constraint_note,
90 }
91 } else {
92 ToolSchemaProjection::Relaxed {
93 parameters,
94 strict,
95 constraint_note,
96 }
97 }
98}
99
100#[must_use]
101pub fn project_tool_schema(
102 route: RouteToolProjection,
103 input_schema: &Value,
104 strict_intent: Option<bool>,
105) -> ToolSchemaProjection {
106 match route {
107 RouteToolProjection::Exact { strict_honored } => {
108 let mut parameters = input_schema.clone();
109 let strict = if strict_intent == Some(true)
110 && strict_honored
111 && strict_schema_supported(input_schema)
112 {
113 sanitize_for_strict(&mut parameters);
114 Some(true)
115 } else {
116 non_strict_flag(strict_intent, strict_honored)
117 };
118 projected(input_schema, strict_intent, parameters, strict, None)
119 }
120 RouteToolProjection::Relaxed { strict_honored } => {
121 let mut parameters = input_schema.clone();
122 let constraint_note = sanitize_for_responses(&mut parameters);
123 let strict = if strict_intent == Some(true)
124 && strict_honored
125 && strict_schema_supported(input_schema)
126 && strict_schema_supported(¶meters)
127 {
128 sanitize_for_strict(&mut parameters);
129 Some(true)
130 } else {
131 non_strict_flag(strict_intent, strict_honored)
132 };
133 projected(
134 input_schema,
135 strict_intent,
136 parameters,
137 strict,
138 constraint_note,
139 )
140 }
141 RouteToolProjection::RelaxedNonStrict => {
142 let mut parameters = input_schema.clone();
143 let constraint_note = sanitize_for_responses(&mut parameters);
144 projected(
145 input_schema,
146 strict_intent,
147 parameters,
148 Some(false),
149 constraint_note,
150 )
151 }
152 RouteToolProjection::Mfjs { strict_honored } => {
153 let strict_shape = strict_intent == Some(true)
154 && strict_honored
155 && strict_schema_supported(input_schema);
156 let mut parameters = input_schema.clone();
157 if strict_shape {
158 sanitize_for_strict(&mut parameters);
159 }
160 match sanitize_for_kimi_parameters(&mut parameters) {
161 Ok(constraint_note) => projected(
162 input_schema,
163 strict_intent,
164 parameters,
165 if strict_shape {
166 Some(true)
167 } else {
168 non_strict_flag(strict_intent, strict_honored)
169 },
170 constraint_note,
171 ),
172 Err(reason) => ToolSchemaProjection::Omitted { reason },
173 }
174 }
175 }
176}
177
178#[must_use]
179pub fn description_with_constraint_note(
180 description: &str,
181 constraint_note: Option<String>,
182) -> String {
183 match constraint_note {
184 None => description.to_string(),
185 Some(note) if description.trim().is_empty() => note,
186 Some(note) => format!("{}\n\n{}", description.trim(), note),
187 }
188}
189
190#[must_use]
191pub fn forced_tool_name(tool_choice: &Value) -> Option<&str> {
192 if let Some(name) = tool_choice.as_str() {
193 return (!matches!(name, "auto" | "none" | "any" | "required")).then_some(name);
194 }
195 match tool_choice.get("type").and_then(Value::as_str) {
196 Some("tool") => tool_choice.get("name").and_then(Value::as_str),
197 Some("function") => tool_choice.get("name").and_then(Value::as_str).or_else(|| {
198 tool_choice
199 .pointer("/function/name")
200 .and_then(Value::as_str)
201 }),
202 _ => None,
203 }
204}