converge-provider 3.7.0

LLM provider implementations for Converge
Documentation
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright 2024-2026 Reflective Labs
// SPDX-License-Identifier: MIT

//! Core tool definition types.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Describes a tool's interface and capabilities.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolDefinition {
    /// Unique tool name.
    pub name: String,
    /// Human-readable description.
    pub description: String,
    /// JSON Schema for input parameters.
    pub input_schema: InputSchema,
    /// Where this tool came from.
    #[serde(default)]
    pub source: ToolSource,
    /// Optional annotations/metadata.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub annotations: HashMap<String, String>,
}

impl ToolDefinition {
    /// Creates a new tool definition.
    #[must_use]
    pub fn new(
        name: impl Into<String>,
        description: impl Into<String>,
        input_schema: InputSchema,
    ) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            input_schema,
            source: ToolSource::Inline,
            annotations: HashMap::new(),
        }
    }

    /// Sets the tool source.
    #[must_use]
    pub fn with_source(mut self, source: ToolSource) -> Self {
        self.source = source;
        self
    }

    /// Adds an annotation.
    #[must_use]
    pub fn with_annotation(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.annotations.insert(key.into(), value.into());
        self
    }

    /// Returns true if this is an MCP tool.
    #[must_use]
    pub fn is_mcp(&self) -> bool {
        matches!(self.source, ToolSource::Mcp { .. })
    }

    /// Returns true if this is an `OpenAPI` tool.
    #[must_use]
    pub fn is_openapi(&self) -> bool {
        matches!(self.source, ToolSource::OpenApi { .. })
    }

    /// Returns true if this is a GraphQL tool.
    #[must_use]
    pub fn is_graphql(&self) -> bool {
        matches!(self.source, ToolSource::GraphQl { .. })
    }
}

/// Where a tool definition originated from.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ToolSource {
    /// Tool from an MCP server.
    Mcp {
        server_name: String,
        server_uri: String,
    },
    /// Tool from an `OpenAPI` specification.
    OpenApi {
        spec_path: String,
        operation_id: String,
        method: String,
        path: String,
    },
    /// Tool from a GraphQL schema.
    GraphQl {
        endpoint: String,
        operation_name: String,
        operation_type: GraphQlOperationType,
    },
    /// Tool defined inline in code.
    #[default]
    Inline,
}

/// Type of GraphQL operation.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum GraphQlOperationType {
    #[default]
    Query,
    Mutation,
    Subscription,
}

/// JSON Schema for tool input parameters.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct InputSchema {
    #[serde(flatten)]
    pub schema: serde_json::Value,
}

impl InputSchema {
    #[must_use]
    pub fn from_json_schema(schema: serde_json::Value) -> Self {
        Self { schema }
    }

    #[must_use]
    pub fn empty() -> Self {
        Self {
            schema: serde_json::json!({
                "type": "object",
                "properties": {},
                "required": []
            }),
        }
    }

    #[must_use]
    pub fn required_properties(&self) -> Vec<String> {
        self.schema
            .get("required")
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str().map(String::from))
                    .collect()
            })
            .unwrap_or_default()
    }

    #[must_use]
    pub fn properties(&self) -> Option<&serde_json::Map<String, serde_json::Value>> {
        self.schema.get("properties").and_then(|v| v.as_object())
    }
}

/// A request to invoke a tool.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
    pub call_id: String,
    pub tool_name: String,
    pub arguments: serde_json::Value,
}

impl ToolCall {
    #[must_use]
    pub fn new(tool_name: impl Into<String>, arguments: serde_json::Value) -> Self {
        Self {
            call_id: generate_call_id(),
            tool_name: tool_name.into(),
            arguments,
        }
    }
}

/// The result of a tool invocation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolResult {
    pub call_id: String,
    pub content: ToolResultContent,
    pub is_error: bool,
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub metadata: HashMap<String, String>,
}

impl ToolResult {
    #[must_use]
    pub fn text(call_id: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            call_id: call_id.into(),
            content: ToolResultContent::Text(content.into()),
            is_error: false,
            metadata: HashMap::new(),
        }
    }

    #[must_use]
    pub fn error(call_id: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            call_id: call_id.into(),
            content: ToolResultContent::Text(message.into()),
            is_error: true,
            metadata: HashMap::new(),
        }
    }

    #[must_use]
    pub fn as_text(&self) -> Option<&str> {
        match &self.content {
            ToolResultContent::Text(s) => Some(s),
            ToolResultContent::Json(v) => v.as_str(),
        }
    }
}

/// Content types for tool results.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ToolResultContent {
    Text(String),
    Json(serde_json::Value),
}

fn generate_call_id() -> String {
    use std::sync::atomic::{AtomicU64, Ordering};
    static COUNTER: AtomicU64 = AtomicU64::new(0);
    let count = COUNTER.fetch_add(1, Ordering::Relaxed);
    let timestamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis())
        .unwrap_or(0);
    format!("call-{timestamp:x}-{count:x}")
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_tool_definition_creation() {
        let tool = ToolDefinition::new("test_tool", "A test tool", InputSchema::empty());
        assert_eq!(tool.name, "test_tool");
        assert!(matches!(tool.source, ToolSource::Inline));
    }

    #[test]
    fn test_tool_call_creation() {
        let call = ToolCall::new("test_tool", json!({"input": "hello"}));
        assert_eq!(call.tool_name, "test_tool");
        assert!(call.call_id.starts_with("call-"));
    }

    #[test]
    fn test_tool_result() {
        let result = ToolResult::text("call-123", "Success");
        assert!(!result.is_error);
        assert_eq!(result.as_text(), Some("Success"));
    }

    // ========================================================================
    // ToolDefinition builder and source detection
    // ========================================================================

    #[test]
    fn tool_definition_with_source_and_annotations() {
        let tool = ToolDefinition::new("get_user", "Fetch a user", InputSchema::empty())
            .with_source(ToolSource::Mcp {
                server_name: "user-svc".into(),
                server_uri: "stdio://user-svc".into(),
            })
            .with_annotation("category", "data")
            .with_annotation("auth", "required");

        assert!(tool.is_mcp());
        assert!(!tool.is_openapi());
        assert!(!tool.is_graphql());
        assert_eq!(tool.annotations.len(), 2);
        assert_eq!(tool.annotations["category"], "data");
    }

    #[test]
    fn tool_source_openapi_detection() {
        let tool = ToolDefinition::new("list_items", "List items", InputSchema::empty())
            .with_source(ToolSource::OpenApi {
                spec_path: "openapi.yaml".into(),
                operation_id: "listItems".into(),
                method: "GET".into(),
                path: "/items".into(),
            });

        assert!(tool.is_openapi());
        assert!(!tool.is_mcp());
        assert!(!tool.is_graphql());
    }

    #[test]
    fn tool_source_graphql_detection() {
        let tool = ToolDefinition::new("query_users", "Query users", InputSchema::empty())
            .with_source(ToolSource::GraphQl {
                endpoint: "https://api.example.com/graphql".into(),
                operation_name: "GetUsers".into(),
                operation_type: GraphQlOperationType::Query,
            });

        assert!(tool.is_graphql());
        assert!(!tool.is_mcp());
        assert!(!tool.is_openapi());
    }

    #[test]
    fn inline_source_is_default() {
        let tool = ToolDefinition::new("test", "test", InputSchema::empty());
        assert!(matches!(tool.source, ToolSource::Inline));
        assert!(!tool.is_mcp());
        assert!(!tool.is_openapi());
        assert!(!tool.is_graphql());
    }

    // ========================================================================
    // InputSchema
    // ========================================================================

    #[test]
    fn input_schema_empty_has_no_required() {
        let schema = InputSchema::empty();
        assert!(schema.required_properties().is_empty());
        assert!(schema.properties().unwrap().is_empty());
    }

    #[test]
    fn input_schema_from_json_extracts_required() {
        let schema = InputSchema::from_json_schema(json!({
            "type": "object",
            "properties": {
                "name": {"type": "string"},
                "age": {"type": "integer"}
            },
            "required": ["name"]
        }));
        assert_eq!(schema.required_properties(), vec!["name"]);
        assert_eq!(schema.properties().unwrap().len(), 2);
    }

    #[test]
    fn input_schema_no_required_field_returns_empty() {
        let schema = InputSchema::from_json_schema(json!({
            "type": "object",
            "properties": {"x": {"type": "string"}}
        }));
        assert!(schema.required_properties().is_empty());
    }

    // ========================================================================
    // ToolResult
    // ========================================================================

    #[test]
    fn tool_result_error_flag() {
        let result = ToolResult::error("call-1", "something failed");
        assert!(result.is_error);
        assert_eq!(result.as_text(), Some("something failed"));
    }

    #[test]
    fn tool_result_json_content() {
        let result = ToolResult {
            call_id: "call-1".into(),
            content: ToolResultContent::Json(json!({"status": "ok"})),
            is_error: false,
            metadata: HashMap::new(),
        };
        // as_text returns None for json objects (only works for json strings)
        assert!(result.as_text().is_none());
    }

    #[test]
    fn tool_call_ids_are_unique() {
        let c1 = ToolCall::new("tool", json!({}));
        let c2 = ToolCall::new("tool", json!({}));
        assert_ne!(c1.call_id, c2.call_id);
    }

    // ========================================================================
    // Serde round-trips
    // ========================================================================

    #[test]
    fn tool_definition_serde_roundtrip() {
        let tool = ToolDefinition::new("my_tool", "A tool", InputSchema::empty())
            .with_annotation("version", "1.0");

        let json_str = serde_json::to_string(&tool).unwrap();
        let round: ToolDefinition = serde_json::from_str(&json_str).unwrap();
        assert_eq!(round.name, "my_tool");
        assert_eq!(round.annotations["version"], "1.0");
    }

    #[test]
    fn tool_source_serde_roundtrip() {
        let sources = vec![
            ToolSource::Inline,
            ToolSource::Mcp {
                server_name: "s".into(),
                server_uri: "u".into(),
            },
            ToolSource::OpenApi {
                spec_path: "p".into(),
                operation_id: "o".into(),
                method: "GET".into(),
                path: "/x".into(),
            },
            ToolSource::GraphQl {
                endpoint: "e".into(),
                operation_name: "Q".into(),
                operation_type: GraphQlOperationType::Mutation,
            },
        ];

        for source in &sources {
            let json_str = serde_json::to_string(source).unwrap();
            let round: ToolSource = serde_json::from_str(&json_str).unwrap();
            assert_eq!(format!("{round:?}"), format!("{source:?}"));
        }
    }
}