Tokitai Core
Core Type Definitions
Tokitai Core provides the foundational types and traits that underpin the Tokitai AI tool integration system. All tool metadata is generated at compile time, which guarantees zero runtime overhead and maximum type safety.
Core Features
- Zero runtime dependencies — the core types are kept as lean as possible.
- Type-safe — tool definitions are produced at compile time, eliminating a class of runtime errors.
- Serde integration — optional serialization support is available behind the
serdefeature.
Core Types
| Type | Description |
|---|---|
[ToolDefinition] |
A tool definition, including name, description, and input schema. |
[ToolParameter] |
Definition of a tool parameter. |
[ParamType] |
Enumeration of JSON Schema types. |
[ToolError] |
Error type returned by tool invocations. |
[ToolErrorKind] |
Enumeration used to classify errors. |
[ToolProvider] |
Trait implemented by tool providers (auto-implemented by the #[tool] macro). |
Quick Start
Add the Dependency
[]
= "0.6"
Basic Usage
use ToolDefinition;
// Create a tool definition
let tool = new;
assert_eq!;
assert_eq!;
// When the `serde` feature is enabled, the definition can be serialized to JSON.
Type Mapping
The [ParamType] enum maps Rust types to their JSON Schema counterparts:
| Rust type | JSON Schema type | ParamType variant |
|---|---|---|
String, &str |
string |
ParamType::String |
i8, i16, i32, i64, u8, u16, u32, u64 |
integer |
ParamType::Integer |
f32, f64 |
number |
ParamType::Number |
bool |
boolean |
ParamType::Boolean |
Vec<T> |
array |
ParamType::Array |
| Custom struct | object |
ParamType::Object |
use ParamType;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Error Handling
The [ToolError] type provides structured error reporting for tool invocations:
use ;
// Build different kinds of errors
let validation_err = validation_error;
assert_eq!;
let not_found_err = not_found;
assert_eq!;
let internal_err = internal_error;
assert_eq!;
Error Variants
| Variant | Value | Description |
|---|---|---|
ToolErrorKind::ValidationError |
0 | Parameter validation failed. |
ToolErrorKind::NotFound |
1 | The requested tool was not found. |
ToolErrorKind::InternalError |
2 | An internal error occurred. |
ToolErrorKind::TypeError |
3 | A type error occurred. |
The ToolProvider Trait
The [ToolProvider] trait is automatically implemented by the #[tool] macro:
use ToolProvider;
// Once you have applied `#[tool]` to your type:
// struct Calculator;
// #[tool] impl Calculator { ... }
// Retrieve every tool definition
let tools = tool_definitions;
// Count the registered tools
let count = tool_count;
// Look up a specific tool by name
let tool = find_tool;
The json_schema! Macro
The json_schema! macro helps you build JSON Schema strings at compile time:
use json_schema;
const SCHEMA: &str = json_schema!;
Features
| Feature | Description |
|---|---|
serde (enabled by default) |
Enables serde-based serialization and JSON support. |
Requirements
- Rust version: 1.80+
- Edition: 2021
License
Licensed under either of:
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.
Related Crates
| Crate | Crates.io | Description |
|---|---|---|
tokitai |
Main crate, bundling runtime support. | |
tokitai-macros |
Procedural macro implementation. |
Documentation
- API Reference — complete API documentation.
- Usage Guide — detailed walk-throughs.
- Architecture — internal design notes.
Happy Coding!