tokmd 1.11.1

Tokei-backed repo inventory receipts (Markdown/TSV/JSONL/CSV) for PRs, CI, and LLM workflows.
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! Tool-schema generation for AI agent tool use.
//!
//! This module introspects a clap `Command` tree and produces schema output in
//! formats commonly consumed by AI tooling.

use anyhow::Result;
use clap::{Arg, ArgAction, Command};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::collections::BTreeMap;

/// Output format for rendered tool schemas.
#[derive(clap::ValueEnum, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
pub enum ToolSchemaFormat {
    /// OpenAI function calling format.
    Openai,
    /// Anthropic tool use format.
    Anthropic,
    /// JSON Schema Draft 7 format.
    #[default]
    Jsonschema,
    /// Raw clap structure dump.
    Clap,
}

/// Schema version for tool definitions.
pub const TOOL_SCHEMA_VERSION: u32 = 1;

/// Top-level schema output with envelope metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolSchemaOutput {
    /// Schema version.
    pub schema_version: u32,

    /// Tool name.
    pub name: String,

    /// Tool version.
    pub version: String,

    /// Tool description.
    pub description: String,

    /// Available commands/tools.
    pub tools: Vec<ToolDefinition>,
}

/// Definition of a single command/tool.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolDefinition {
    /// Command name.
    pub name: String,

    /// Command description.
    pub description: String,

    /// Parameters/arguments.
    pub parameters: Vec<ParameterSchema>,
}

/// Schema for a single parameter/argument.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParameterSchema {
    /// Parameter name.
    pub name: String,

    /// Parameter description.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Parameter type.
    #[serde(rename = "type")]
    pub param_type: String,

    /// Whether the parameter is required.
    pub required: bool,

    /// Default value if any.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default: Option<String>,

    /// Enum values if applicable.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enum_values: Option<Vec<String>>,
}

/// Build the tool schema from a clap `Command`.
pub fn build_tool_schema(cmd: &Command) -> ToolSchemaOutput {
    let mut tools = Vec::new();

    // Add the root command as a tool (for default lang mode).
    tools.push(build_tool_definition(cmd, None));

    // Add all subcommands.
    for subcmd in cmd.get_subcommands() {
        // Skip generated help subcommand.
        let name = subcmd.get_name();
        if name == "help" {
            continue;
        }
        tools.push(build_tool_definition(subcmd, Some(name)));
    }

    ToolSchemaOutput {
        schema_version: TOOL_SCHEMA_VERSION,
        name: cmd.get_name().to_string(),
        version: cmd.get_version().unwrap_or("unknown").to_string(),
        description: cmd.get_about().map(|s| s.to_string()).unwrap_or_default(),
        tools,
    }
}

/// Build a tool definition from a command.
fn build_tool_definition(cmd: &Command, name_override: Option<&str>) -> ToolDefinition {
    let name = name_override.unwrap_or(cmd.get_name()).to_string();
    let description = cmd.get_about().map(|s| s.to_string()).unwrap_or_default();

    let mut parameters = Vec::new();

    // Add arguments.
    for arg in cmd.get_arguments() {
        // Skip generated args.
        if arg.get_id() == "help" || arg.get_id() == "version" {
            continue;
        }
        parameters.push(build_parameter_schema(arg));
    }

    ToolDefinition {
        name,
        description,
        parameters,
    }
}

/// Build a parameter schema from a clap `Arg`.
fn build_parameter_schema(arg: &Arg) -> ParameterSchema {
    let name = arg.get_id().to_string();
    let description = arg.get_help().map(|s| s.to_string());

    // Determine type based on action and value hints.
    let param_type = determine_param_type(arg);

    // Check if required.
    let required = arg.is_required_set();

    // Get default value.
    let default = arg
        .get_default_values()
        .first()
        .map(|v| v.to_string_lossy().to_string());

    // Get enum values if applicable.
    let enum_values = arg
        .get_possible_values()
        .iter()
        .map(|v| v.get_name().to_string())
        .collect::<Vec<_>>();
    let enum_values = if enum_values.is_empty() {
        None
    } else {
        Some(enum_values)
    };

    ParameterSchema {
        name,
        description,
        param_type,
        required,
        default,
        enum_values,
    }
}

/// Determine the parameter type from a clap `Arg`.
fn determine_param_type(arg: &Arg) -> String {
    match arg.get_action() {
        ArgAction::SetTrue | ArgAction::SetFalse => "boolean".to_string(),
        ArgAction::Count => "integer".to_string(),
        ArgAction::Append => "array".to_string(),
        _ => "string".to_string(),
    }
}

/// Render the schema output in the specified format.
pub fn render_output(
    schema: &ToolSchemaOutput,
    format: ToolSchemaFormat,
    pretty: bool,
) -> Result<String> {
    match format {
        ToolSchemaFormat::Jsonschema => render_jsonschema(schema, pretty),
        ToolSchemaFormat::Openai => render_openai(schema, pretty),
        ToolSchemaFormat::Anthropic => render_anthropic(schema, pretty),
        ToolSchemaFormat::Clap => render_clap(schema, pretty),
    }
}

/// Render as JSON Schema format.
fn render_jsonschema(schema: &ToolSchemaOutput, pretty: bool) -> Result<String> {
    let tools_schema: Vec<Value> = schema
        .tools
        .iter()
        .map(|tool| {
            let properties: BTreeMap<String, Value> = tool
                .parameters
                .iter()
                .map(|p| {
                    let mut prop = json!({
                        "type": p.param_type,
                    });

                    if let Some(desc) = &p.description {
                        prop["description"] = json!(desc);
                    }
                    if let Some(def) = &p.default {
                        prop["default"] = json!(def);
                    }
                    if let Some(enums) = &p.enum_values {
                        prop["enum"] = json!(enums);
                    }

                    (p.name.clone(), prop)
                })
                .collect();

            let required: Vec<&str> = tool
                .parameters
                .iter()
                .filter(|p| p.required)
                .map(|p| p.name.as_str())
                .collect();

            json!({
                "name": tool.name,
                "description": tool.description,
                "parameters": {
                    "type": "object",
                    "properties": properties,
                    "required": required,
                }
            })
        })
        .collect();

    let output = json!({
        "$schema": "https://json-schema.org/draft-07/schema#",
        "schema_version": schema.schema_version,
        "name": schema.name,
        "version": schema.version,
        "description": schema.description,
        "tools": tools_schema,
    });

    if pretty {
        Ok(serde_json::to_string_pretty(&output)?)
    } else {
        Ok(serde_json::to_string(&output)?)
    }
}

/// Render in OpenAI function calling format.
fn render_openai(schema: &ToolSchemaOutput, pretty: bool) -> Result<String> {
    let functions: Vec<Value> = schema
        .tools
        .iter()
        .map(|tool| {
            let properties: BTreeMap<String, Value> = tool
                .parameters
                .iter()
                .map(|p| {
                    let mut prop = json!({
                        "type": p.param_type,
                    });

                    if let Some(desc) = &p.description {
                        prop["description"] = json!(desc);
                    }
                    if let Some(enums) = &p.enum_values {
                        prop["enum"] = json!(enums);
                    }

                    (p.name.clone(), prop)
                })
                .collect();

            let required: Vec<&str> = tool
                .parameters
                .iter()
                .filter(|p| p.required)
                .map(|p| p.name.as_str())
                .collect();

            json!({
                "name": tool.name,
                "description": tool.description,
                "parameters": {
                    "type": "object",
                    "properties": properties,
                    "required": required,
                }
            })
        })
        .collect();

    let output = json!({
        "functions": functions,
    });

    if pretty {
        Ok(serde_json::to_string_pretty(&output)?)
    } else {
        Ok(serde_json::to_string(&output)?)
    }
}

/// Render in Anthropic tool use format.
fn render_anthropic(schema: &ToolSchemaOutput, pretty: bool) -> Result<String> {
    let tools: Vec<Value> = schema
        .tools
        .iter()
        .map(|tool| {
            let properties: BTreeMap<String, Value> = tool
                .parameters
                .iter()
                .map(|p| {
                    let mut prop = json!({
                        "type": p.param_type,
                    });

                    if let Some(desc) = &p.description {
                        prop["description"] = json!(desc);
                    }
                    if let Some(enums) = &p.enum_values {
                        prop["enum"] = json!(enums);
                    }

                    (p.name.clone(), prop)
                })
                .collect();

            let required: Vec<&str> = tool
                .parameters
                .iter()
                .filter(|p| p.required)
                .map(|p| p.name.as_str())
                .collect();

            json!({
                "name": tool.name,
                "description": tool.description,
                "input_schema": {
                    "type": "object",
                    "properties": properties,
                    "required": required,
                }
            })
        })
        .collect();

    let output = json!({
        "tools": tools,
    });

    if pretty {
        Ok(serde_json::to_string_pretty(&output)?)
    } else {
        Ok(serde_json::to_string(&output)?)
    }
}

/// Render raw clap structure (for debugging).
fn render_clap(schema: &ToolSchemaOutput, pretty: bool) -> Result<String> {
    if pretty {
        Ok(serde_json::to_string_pretty(schema)?)
    } else {
        Ok(serde_json::to_string(schema)?)
    }
}

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

    fn make_test_cmd() -> Command {
        Command::new("test")
            .version("1.0.0")
            .about("Test command")
            .subcommand(
                Command::new("sub")
                    .about("Subcommand")
                    .arg(Arg::new("flag").long("flag").action(ArgAction::SetTrue))
                    .arg(
                        Arg::new("value")
                            .long("value")
                            .required(true)
                            .help("A value"),
                    ),
            )
    }

    #[test]
    fn build_schema_includes_subcommands() {
        let cmd = make_test_cmd();
        let schema = build_tool_schema(&cmd);

        assert_eq!(schema.name, "test");
        assert_eq!(schema.version, "1.0.0");
        assert!(!schema.tools.is_empty());

        let sub = schema
            .tools
            .iter()
            .find(|tool| tool.name == "sub")
            .expect("subcommand should exist");
        assert_eq!(sub.parameters.len(), 2);
    }

    #[test]
    fn render_openai_has_functions_key() {
        let cmd = make_test_cmd();
        let schema = build_tool_schema(&cmd);
        let output = render_output(&schema, ToolSchemaFormat::Openai, false).unwrap();

        let parsed: Value = serde_json::from_str(&output).unwrap();
        assert!(parsed.get("functions").is_some());
    }

    #[test]
    fn render_anthropic_has_input_schema() {
        let cmd = make_test_cmd();
        let schema = build_tool_schema(&cmd);
        let output = render_output(&schema, ToolSchemaFormat::Anthropic, false).unwrap();

        let parsed: Value = serde_json::from_str(&output).unwrap();
        assert!(parsed.get("tools").is_some());
        let tools = parsed["tools"].as_array().unwrap();
        assert!(tools.iter().any(|tool| tool.get("input_schema").is_some()));
    }
}