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
//! # Execution framework and traits
//!
//! This module provides the execution framework for Knowledge Interaction Protocol (KIP) commands.
//! It defines the core `Executor` trait that must be implemented by any KIP command processor,
//! and provides a convenient high-level function for executing KIP commands from string input.
//!
//! The executor is responsible for taking parsed KIP commands (KQL queries, KML statements,
//! or META commands) and executing them against a knowledge graph or cognitive nexus,
//! returning structured responses.
use async_trait;
use Arc;
use crate::;
/// The core trait that defines how KIP commands are executed.
///
/// This trait must be implemented by any system that wants to process KIP commands.
/// It provides a single asynchronous method for executing parsed commands and returning
/// structured responses.
///
/// # Design Philosophy
///
/// The `Executor` trait is designed to be:
/// - **Asynchronous**: All operations return futures to support non-blocking I/O
/// - **Generic**: Can be implemented by different backend systems (databases, APIs, etc.)
/// - **Error-safe**: Uses `Result` types for proper error handling
/// - **Send-safe**: Futures are `Send` to support multi-threaded execution
///
/// # Implementation Examples
///
/// ```rust,no_run
/// use anda_kip::{Executor, Command, Json, KipError, Response};
/// use async_trait::async_trait;
///
/// struct MyKnowledgeGraph {
/// // Your knowledge graph implementation
/// }
///
/// #[async_trait]
/// impl Executor for MyKnowledgeGraph {
/// async fn execute(&self, command: Command, dry_run: bool) -> Response {
/// match command {
/// Command::Kql(query) => {
/// // Execute KQL query against knowledge graph
/// todo!("Implement KQL execution")
/// },
/// Command::Kml(statement) => {
/// // Execute KML statement to modify knowledge graph
/// todo!("Implement KML execution")
/// },
/// Command::Meta(meta_cmd) => {
/// // Execute META command for introspection
/// todo!("Implement META execution")
/// }
/// }
/// }
/// }
/// ```
/// High-level convenience function for executing KIP commands from string input.
///
/// This function provides a complete pipeline from raw KIP command string to execution result.
/// It handles parsing the input string into a structured command and then delegates execution
/// to the provided executor implementation.
///
/// # Workflow
///
/// 1. **Parse**: Convert the input string into a structured `Command` AST
/// 2. **Execute**: Pass the parsed command to the executor for processing
/// 3. **Return**: Provide the structured response or detailed error information
///
/// # Arguments
///
/// * `executor` - An implementation of the `Executor` trait that will process the command
/// * `command` - The raw KIP command string to parse and execute
/// * `dry_run` - If true, the command is executed in dry-run mode (no state changes)
///
/// # Returns
///
/// A [`Response`] containing:
/// - `Result(Response)`: Successful execution with structured response data
/// - `Error(KipError)`: Either parsing or execution error with detailed information
///
/// # Error Types
///
/// This function can return errors following KIP Standard Error Codes:
/// - **Parse Errors**: `KIP_1001` (InvalidSyntax) when the input string is malformed
/// - **Execution Errors**: Various KIP error codes returned by the executor
///
/// # Examples
///
/// ```rust,no_run
/// use anda_kip::{execute_kip, Executor, Response};
///
/// async fn example(my_executor: impl Executor) {
/// // Execute a KQL query
/// let kql_result = execute_kip(
/// &my_executor,
/// "FIND(?drug) WHERE { ?drug {type: \"Drug\"} }",
/// true // dry_run
/// ).await;
/// println!("{kql_result:#?}");
///
/// // Execute a KML statement
/// let kml_result = execute_kip(
/// &my_executor,
/// "UPSERT { CONCEPT ?drug { {type: \"Drug\", name: \"Aspirin\" } } }",
/// true // dry_run
/// ).await;
/// println!("{kml_result:#?}");
///
/// // Execute a META command
/// let meta_result = execute_kip(
/// &my_executor,
/// "DESCRIBE PRIMER",
/// false // dry_run
/// ).await;
///
/// println!("{meta_result:#?}");
/// }
/// ```
///
/// # Performance Notes
///
/// - Parsing is performed synchronously before execution
/// - Consider caching parsed commands for repeated execution
/// - The executor implementation determines overall performance characteristics
pub async
/// High-level convenience function for executing KIP commands in read-only mode.
///
/// This function is similar to `execute_kip` but enforces that only read-only commands
/// (KQL queries and META commands, including `DESCRIBE` / `SEARCH` / `EXPORT`) are executed.
/// If a KML command (`UPSERT` / `UPDATE` / `MERGE` / `DELETE`) is detected, an error is
/// returned indicating that only KQL and META commands are allowed in read-only mode.
///
pub async