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
//! # Tokitai
//!
//! **AI Tool Integration System with Compile-time Tool Definitions**
//!
//! Tokitai is a zero-runtime-dependency procedural macro library that transforms your Rust methods
//! into AI-callable tools with a single `#[tool]` attribute. All tool definitions are generated at
//! compile time, ensuring type errors are caught before runtime.
//!
//! ## Key Features
//!
//! - **Zero Runtime Intrusion** - The macro itself has no runtime dependencies
//! - **Compile-time Type Safety** - Tool definitions generated at compile time, parameter type errors exposed during compilation
//! - **Single Attribute** - Just `#[tool]`, no need for multiple tags
//! - **Optional Runtime** - Control dependencies via features, supports async-free environments
//! - **Vendor Neutral** - Works with any AI/LLM provider (Ollama, OpenAI, Anthropic, etc.)
//! ## Quick Start
//!
//! ### 1. Add Dependencies
//!
//! ```toml
//! [dependencies]
//! tokitai = "0.6"
//! ```
//!
//! That's it! All required dependencies (serde, serde_json, thiserror) are included automatically.
//!
//! ### 2. Define Your Tools
//!
//! ```rust,ignore
//! use tokitai::tool;
//!
//! pub struct Calculator;
//!
//! #[tool]
//! impl Calculator {
//! /// Add two numbers together
//! pub fn add(&self, a: i32, b: i32) -> i32 {
//! a + b
//! }
//!
//! /// Calculate SHA256 hash of a string
//! pub fn sha256(&self, input: String) -> String {
//! // Your implementation...
//! format!("hash of {}", input)
//! }
//! }
//! ```
//!
//! ### 3. Get Tool Definitions (Send to AI)
//!
//! ```rust,ignore
//! // Compile-time generated tool definitions
//! let tools = Calculator::tool_definitions();
//!
//! // Convert to JSON and send to AI
//! let tools_json = serde_json::to_string_pretty(tools)?;
//! println!("{}", tools_json);
//! ```
//!
//! Output:
//!
//! ```json
//! [
//! {
//! "name": "add",
//! "description": "Add two numbers together",
//! "input_schema": "{\"type\":\"object\",\"properties\":{\"a\":{\"type\":\"integer\"},\"b\":{\"type\":\"integer\"}},\"required\":[\"a\",\"b\"]}"
//! },
//! {
//! "name": "sha256",
//! "description": "Calculate SHA256 hash of a string",
//! "input_schema": "{\"type\":\"object\",\"properties\":{\"input\":{\"type\":\"string\"}},\"required\":[\"input\"]}"
//! }
//! ]
//! ```
//!
//! ### 4. Handle AI Calls
//!
//! ```rust,ignore
//! use tokitai::json;
//!
//! let calc = Calculator;
//!
//! // AI decides to call a tool
//! let call_request = json!({
//! "name": "add",
//! "arguments": {"a": 10, "b": 20}
//! });
//!
//! // Execute the tool
//! let result = calc.call_tool(
//! call_request["name"].as_str().unwrap(),
//! &call_request["arguments"]
//! )?;
//!
//! println!("Result: {}", result); // 30
//! ```
//!
//! ## Crate Structure
//!
//! Tokitai is organized as a workspace with three crates:
//!
//! | Crate | Description |
//! |-------|-------------|
//! | [`tokitai`](https://crates.io/crates/tokitai) | Main crate with runtime support (this crate) |
//! | [`tokitai-core`](https://crates.io/crates/tokitai-core) | Core types and traits (zero dependencies) |
//! | [`tokitai-macros`](https://crates.io/crates/tokitai-macros) | Procedural macros (compile-time code generation) |
//!
//! ## How It Works
//!
//! ```text
//! +---------------+ Tool Definitions +---------------+
//! | Your Code | ----------------------> | AI Service |
//! | #[tool] | | (Ollama, |
//! +---------------+ | OpenAI, |
//! ^ | etc.) |
//! | Execution Result +---------------+
//! | |
//! | | Call Request
//! | v
//! +---------------+ +---------------+
//! | Rust Method | <------ call_tool ------ | JSON Call |
//! | (Local) | | {"name":..} |
//! +---------------+ +---------------+
//! ```
//!
//! 1. **Define Rust methods** → Implement your business logic
//! 2. **Send to AI** → AI knows what tools are available
//! 3. **Receive call request** → AI returns "I want to call a tool"
//! 4. **Execute and return** → Run Rust code locally
//!
//! ## Features
//!
//! | Feature | Description |
//! |---------|-------------|
//! | `default` | Enables full runtime support |
//! | `runtime` | Basic runtime support (async, error handling) |
//! | `mcp` | MCP protocol support (requires `runtime`) |
//!
//! ### Minimal Dependencies (Compile-time Only)
//!
//! If you only need compile-time tool definitions without runtime support:
//!
//! ```toml
//! [dependencies]
//! tokitai = { version = "0.6", default-features = false }
//! ```
//!
//! Note: Runtime features (call_tool, etc.) require serde/serde_json which are included by default.
//!
//! ## API Overview
//!
//! ### Re-exported Core Types
//!
//! - [`ToolDefinition`] - Tool definition with name, description, and input schema
//! - [`ToolError`] - Tool invocation error type
//! - [`ToolErrorKind`] - Error classification
//! - [`ParamType`] - JSON Schema type enumeration
//! - [`ToolProvider`] - Trait for tool providers (auto-implemented by `#[tool]`)
//! - [`json!`] - Macro for creating JSON values (from serde_json)
//! - [`Value`], [`Map`] - JSON value types (from serde_json)
//!
//! ### Runtime Types
//!
//! - [`AiToolError`] - Enhanced error type for runtime
//!
//! ### Macro
//!
//! - [`tool`] - Attribute macro for marking tool implementations
//!
//! ## Type Mapping
//!
//! Rust types are automatically mapped to JSON Schema types:
//!
//! | Rust Type | JSON Schema Type |
//! |-----------|------------------|
//! | `String`, `&str` | `string` |
//! | `i8`, `i16`, `i32`, `i64`, `u8`, `u16`, `u32`, `u64` | `integer` |
//! | `f32`, `f64` | `number` |
//! | `bool` | `boolean` |
//! | `Vec<T>` | `array` |
//! | Custom structs | `object` |
//!
//! ## Examples
//!
//! See the [examples directory](https://github.com/silverenternal/tokitai/tree/main/examples) for more:
//!
//! - `basic_usage.rs` - Basic usage example
//! - `ollama_integration.rs` - Ollama AI integration with SHA256 tool
//! - `multi_tool_chat.rs` - Multi-tool collaborative chatbot
//!
//! ## Requirements
//!
//! - **Rust Version**: 1.70+
//! - **Edition**: 2021
//!
//! ## License
//!
//! Licensed under either of:
//!
//! - Apache License, Version 2.0 ([LICENSE-APACHE](https://github.com/silverenternal/tokitai/blob/main/LICENSE))
//! - MIT License ([LICENSE-MIT](https://github.com/silverenternal/tokitai/blob/main/LICENSE))
//!
//! at your option.
//!
//! ## Contributing
//!
//! Unless you explicitly state otherwise, any contribution intentionally submitted
//! for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be
//! dual licensed as above, without any additional terms or conditions.
//!
//! ## See Also
//!
//! - [`tokitai-core`](https://crates.io/crates/tokitai-core) - Core types and traits
//! - [`tokitai-macros`](https://crates.io/crates/tokitai-macros) - Procedural macros
// Re-export core types (always available)
pub use ;
// T-013: re-export the version-gating helpers so users can set a
// program-wide current version from their `main` without depending
// on `tokitai-core` directly.
pub use ;
// Re-export serde_json for convenience (users don't need to add extra dependency)
pub use ;
// Re-export config types (when serde feature is enabled)
pub use ;
// T-010: dynamic tool registry re-exports. The trait is opt-in;
// macro-generated providers do not implement it.
pub use ;
// Runtime module (always available)
// Export runtime types
pub use AiToolError;
pub use *;
// Re-export macros
pub use ;
/// T-016: `call!(self.method(args) => result)` declarative macro.
///
/// The macro is a marker — its real work happens inside the
/// `#[tool]` proc-macro, which sees the *pre-expansion* tokens
/// (because attribute arguments are not macro-expanded before the
/// proc-macro sees them). The macro_rules! body emits a
/// `compile_error!` so the user gets a clear pointer to T-016
/// when they accidentally use `call!` outside a `#[tool(...)]`
/// attribute. Inside a valid attribute, the macro is never
/// expanded — the parser detects the `call` ident and parses the
/// parenthesised body as raw tokens.
///
/// # Example
///
/// ```rust,ignore
/// use tokitai::{tool, call};
///
/// #[tool]
/// impl Calculator {
/// #[tool(example = call!(self.add(1, 2) => 3))]
/// pub fn add(&self, a: i32, b: i32) -> i32 { a + b }
/// }
/// ```
// T-015: re-export the `tracing` crate when the `trace`
// feature is on so consumers do not need to add a separate
// `tracing = "0.1"` dependency in their Cargo.toml. The
// `#[tracing::instrument]` attribute the macro emits only
// resolves at the consumer site, so the consumer must have
// the crate in its dep graph; the re-export short-circuits
// the dep-graph plumbing for the common case.
pub use tracing;
/// Version string of this `tokitai` crate, sourced from the package's
/// `Cargo.toml` at compile time. Useful for diagnostics and for emitting
/// version-aware MCP responses.
pub const VERSION: &str = env!;