anda_kip/lib.rs
1//! # Anda KIP - Knowledge Interaction Protocol Implementation
2//!
3//! A comprehensive Rust implementation of KIP (Knowledge Interaction Protocol) for building
4//! sustainable AI knowledge memory systems and enabling sophisticated interactions between
5//! Large Language Models (LLMs) and knowledge graphs.
6//!
7//! ## Overview
8//!
9//! **🧬 KIP (Knowledge Interaction Protocol)** is a knowledge memory interaction protocol
10//! designed specifically for Large Language Models (LLMs), aimed at building sustainable
11//! learning and self-evolving knowledge memory systems for AI Agents.
12//!
13//! This crate provides a complete, type-safe Rust implementation of the KIP specification,
14//! offering robust parsing, execution, and communication capabilities for knowledge graph
15//! operations.
16//!
17//! ## Standards Compliance
18//!
19//! This implementation follows the official KIP specification. For detailed information
20//! about the protocol, syntax, and semantics, please refer to:
21//!
22//! **👉 [KIP Specification](https://github.com/ldclabs/KIP)**
23//!
24//! ## Architecture
25//!
26//! The crate is organized into several key modules:
27//!
28//! - [`ast`]: Abstract Syntax Tree definitions for all KIP language constructs
29//! - [`error`]: Comprehensive error types and structured error handling
30//! - [`executor`]: Execution framework with async traits for implementing KIP backends
31//! - [`parser`]: High-performance nom-based parsers for KQL, KML, and META commands
32//! - [`request`]: Standardized JSON-based request/response structures for communication
33//!
34//! ## Quick Start
35//!
36//! ```rust,no_run
37//! use anda_kip::{parse_kip, Command, Executor, Response};
38//!
39//! // Parse a KQL query
40//! let query = parse_kip(r#"
41//! FIND(?drug.name, ?drug.attributes.risk_level)
42//! WHERE {
43//! ?drug {type: "Drug"}
44//! ?headache {name: "Headache"}
45//! (?drug, "treats", ?headache)
46//!
47//! FILTER(?drug.attributes.risk_level < 3)
48//! }
49//! ORDER BY ?drug.attributes.risk_level ASC
50//! LIMIT 10
51//! "#).unwrap();
52//!
53//! // Parse a KML statement
54//! let statement = parse_kip(r#"
55//! UPSERT {
56//! CONCEPT ?new_drug {
57//! { type: "Drug", name: "Aspirin" }
58//! SET ATTRIBUTES {
59//! molecular_formula: "C9H8O4",
60//! risk_level: 1
61//! }
62//! }
63//! }
64//! "#).unwrap();
65//! ```
66//!
67//! ## Examples
68//!
69//! For comprehensive examples and usage patterns, see the individual module documentation
70//! and the project's examples directory.
71
72use std::sync::LazyLock;
73
74pub mod ast;
75pub mod capsule;
76pub mod error;
77pub mod executor;
78pub mod parser;
79pub mod request;
80pub mod types;
81
82pub use ast::*;
83pub use capsule::*;
84pub use error::*;
85pub use executor::*;
86pub use parser::*;
87pub use request::*;
88pub use types::*;
89
90pub static SELF_INSTRUCTIONS: &str = include_str!("../SelfInstructions.md");
91pub static SYSTEM_INSTRUCTIONS: &str = include_str!("../SystemInstructions.md");
92
93/// JSON schema definition for the `execute_kip` function
94pub static KIP_FUNCTION_DEFINITION: LazyLock<Json> =
95 LazyLock::new(|| serde_json::from_str(include_str!("../FunctionDefinition.json")).unwrap());
96
97#[cfg(test)]
98mod tests {
99 use super::*;
100
101 #[test]
102 fn test_function() {
103 let json = KIP_FUNCTION_DEFINITION.clone();
104 println!("{json:#?}");
105 assert!(json.is_object());
106 assert_eq!(
107 json.get("name"),
108 Some(&Json::String("execute_kip".to_string()))
109 );
110 }
111}