File size: 7,621 Bytes
17f328f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//! Apps SDK `openai/fileParams` metadata and schema shaping.
//!
//! For each declared file argument, this module derives the provided-file fields
//! accepted by its input schema and records them on `ToolInfo` for execution-time
//! argument rewriting. It also presents file arguments to the model as local paths.
//!
//! See <https://developers.openai.com/apps-sdk/reference#tool-descriptor-parameters>.

use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::Arc;

use rmcp::model::Tool;
use serde_json::Map;
use serde_json::Value as JsonValue;

use crate::tools::ToolInfo;

const META_OPENAI_FILE_PARAMS: &str = "openai/fileParams";

#[derive(Default)]
struct OpenAiFileSchemaInfo {
    accepts_mime_type: bool,
    accepts_file_name: bool,
}

pub fn declared_openai_file_input_param_names(
    meta: Option<&Map<String, JsonValue>>,
) -> Vec<String> {
    let Some(meta) = meta else {
        return Vec::new();
    };

    meta.get(META_OPENAI_FILE_PARAMS)
        .and_then(JsonValue::as_array)
        .into_iter()
        .flatten()
        .filter_map(JsonValue::as_str)
        .filter(|value| !value.is_empty())
        .map(str::to_string)
        .collect()
}

/// Derives execution-time file capabilities from the raw schema, then masks
/// declared file arguments as local paths for the model.
pub(crate) fn prepare_openai_file_params_for_model(tool_info: &mut ToolInfo) {
    let file_params = declared_openai_file_input_param_names(tool_info.tool.meta.as_deref());
    tool_info.openai_file_input_optional_fields =
        supported_openai_file_input_optional_fields(&tool_info.tool, &file_params);

    if file_params.is_empty() {
        return;
    }

    let mut tool = tool_info.tool.clone();
    let mut input_schema = JsonValue::Object(tool.input_schema.as_ref().clone());
    rewrite_input_schema_for_local_file_paths(&mut input_schema, &file_params);
    if let JsonValue::Object(input_schema) = input_schema {
        tool.input_schema = Arc::new(input_schema);
    }
    tool_info.tool = tool;
}

fn supported_openai_file_input_optional_fields(
    tool: &Tool,
    file_params: &[String],
) -> HashMap<String, Vec<String>> {
    let properties = tool
        .input_schema
        .get("properties")
        .and_then(JsonValue::as_object);

    file_params
        .iter()
        .map(|field_name| {
            let optional_fields = properties
                .and_then(|properties| properties.get(field_name))
                .map(|schema| {
                    let schema_info = openai_file_schema_info(schema, tool.input_schema.as_ref());
                    let mut optional_fields = Vec::new();
                    if schema_info.accepts_mime_type {
                        optional_fields.push("mime_type".to_string());
                    }
                    if schema_info.accepts_file_name {
                        optional_fields.push("file_name".to_string());
                    }
                    optional_fields
                })
                .unwrap_or_default();
            (field_name.clone(), optional_fields)
        })
        .collect()
}

fn openai_file_schema_info(
    schema: &JsonValue,
    root_schema: &Map<String, JsonValue>,
) -> OpenAiFileSchemaInfo {
    let mut info = OpenAiFileSchemaInfo::default();
    let mut pending = vec![schema];
    let mut visited_refs = HashSet::new();

    while let Some(schema) = pending.pop() {
        let Some(schema) = schema.as_object() else {
            continue;
        };

        if let Some(schema_ref) = schema.get("$ref").and_then(JsonValue::as_str)
            && visited_refs.insert(schema_ref)
            && let Some(referenced_schema) = resolve_local_schema_ref(root_schema, schema_ref)
        {
            pending.push(referenced_schema);
        }

        for keyword in ["anyOf", "oneOf", "allOf"] {
            if let Some(variants) = schema.get(keyword).and_then(JsonValue::as_array) {
                pending.extend(variants);
            }
        }

        if schema.get("type").and_then(JsonValue::as_str) == Some("array")
            || schema.contains_key("items")
        {
            if let Some(items) = schema.get("items") {
                pending.push(items);
            }
            continue;
        }

        let properties = schema.get("properties").and_then(JsonValue::as_object);
        let is_object_schema = schema.get("type").and_then(JsonValue::as_str) == Some("object")
            || properties.is_some()
            || schema.contains_key("additionalProperties");
        if !is_object_schema {
            continue;
        }
        let accepts_additional_properties = !matches!(
            schema.get("additionalProperties"),
            Some(JsonValue::Bool(false) | JsonValue::Object(_))
        );
        info.accepts_mime_type |= accepts_additional_properties
            || properties.is_some_and(|properties| properties.contains_key("mime_type"));
        info.accepts_file_name |= accepts_additional_properties
            || properties.is_some_and(|properties| properties.contains_key("file_name"));
    }

    info
}

fn resolve_local_schema_ref<'a>(
    root_schema: &'a Map<String, JsonValue>,
    schema_ref: &str,
) -> Option<&'a JsonValue> {
    let pointer = schema_ref.strip_prefix("#/")?;
    let mut segments = pointer.split('/');
    let first_segment = segments.next()?.replace("~1", "/").replace("~0", "~");
    let mut referenced_schema = root_schema.get(&first_segment)?;

    for segment in segments {
        let segment = segment.replace("~1", "/").replace("~0", "~");
        referenced_schema = match referenced_schema {
            JsonValue::Object(object) => object.get(&segment)?,
            JsonValue::Array(array) => array.get(segment.parse::<usize>().ok()?)?,
            _ => return None,
        };
    }

    Some(referenced_schema)
}

fn rewrite_input_schema_for_local_file_paths(input_schema: &mut JsonValue, file_params: &[String]) {
    let Some(properties) = input_schema
        .as_object_mut()
        .and_then(|schema| schema.get_mut("properties"))
        .and_then(JsonValue::as_object_mut)
    else {
        return;
    };

    for field_name in file_params {
        let Some(property_schema) = properties.get_mut(field_name) else {
            continue;
        };
        rewrite_input_property_schema_as_local_file_path(property_schema);
    }
}

fn rewrite_input_property_schema_as_local_file_path(schema: &mut JsonValue) {
    let Some(object) = schema.as_object_mut() else {
        return;
    };

    let mut description = object
        .get("description")
        .and_then(JsonValue::as_str)
        .map(str::to_string)
        .unwrap_or_default();
    let guidance = "This parameter expects an absolute local file path. If you want to upload a file, provide the absolute path to that file here.";
    if description.is_empty() {
        description = guidance.to_string();
    } else if !description.contains(guidance) {
        description = format!("{description} {guidance}");
    }

    let is_array = object.get("type").and_then(JsonValue::as_str) == Some("array")
        || object.get("items").is_some();
    object.clear();
    object.insert("description".to_string(), JsonValue::String(description));
    if is_array {
        object.insert("type".to_string(), JsonValue::String("array".to_string()));
        object.insert("items".to_string(), serde_json::json!({ "type": "string" }));
    } else {
        object.insert("type".to_string(), JsonValue::String("string".to_string()));
    }
}

#[cfg(test)]
#[path = "file_params_tests.rs"]
mod tests;