agentic-flow 1.3.0 β†’ 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,699 @@
1
+ # Agent Booster: Technical Architecture
2
+
3
+ ## πŸ›οΈ System Architecture
4
+
5
+ ```
6
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
7
+ β”‚ User Application β”‚
8
+ β”‚ (Agentic-flow, CLI, NPM SDK, or Direct API) β”‚
9
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
10
+ β”‚
11
+ β–Ό
12
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
13
+ β”‚ JavaScript/TypeScript Layer β”‚
14
+ β”‚ β”‚
15
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
16
+ β”‚ β”‚ Auto-Detection │────────▢│ Native Addon β”‚ (Node.js) β”‚
17
+ β”‚ β”‚ Loader β”‚ β”‚ (via napi-rs) β”‚ β”‚
18
+ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
19
+ β”‚ β”‚ β”‚ β”‚
20
+ β”‚ β”‚ β”‚ β”‚
21
+ β”‚ β–Ό β–Ό β”‚
22
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
23
+ β”‚ β”‚ WASM Module β”‚ β”‚ Core Rust API β”‚ β”‚
24
+ β”‚ β”‚ (wasm-bindgen) │────────▢│ β”‚ β”‚
25
+ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
26
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
27
+ β”‚
28
+ β–Ό
29
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
30
+ β”‚ Rust Core Library β”‚
31
+ β”‚ (agent-booster crate) β”‚
32
+ β”‚ β”‚
33
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
34
+ β”‚ β”‚ Parser Module β”‚ β”‚
35
+ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
36
+ β”‚ β”‚ β”‚ Tree-sitter β”‚ β”‚ Language β”‚ β”‚ β”‚
37
+ β”‚ β”‚ β”‚ Integration β”‚ β”‚ Grammars β”‚ β”‚ β”‚
38
+ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
39
+ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
40
+ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
41
+ β”‚ β”‚ β–Ό β”‚ β”‚
42
+ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
43
+ β”‚ β”‚ β”‚ AST Extraction β”‚ β”‚ β”‚
44
+ β”‚ β”‚ β”‚ & Chunking β”‚ β”‚ β”‚
45
+ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
46
+ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
47
+ β”‚ β”‚ β”‚
48
+ β”‚ β–Ό β”‚
49
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
50
+ β”‚ β”‚ Embeddings Module β”‚ β”‚
51
+ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
52
+ β”‚ β”‚ β”‚ ONNX Runtime β”‚ β”‚ Model Manager β”‚ β”‚ β”‚
53
+ β”‚ β”‚ β”‚ Inference β”‚ β”‚ (Download/Cache)β”‚ β”‚ β”‚
54
+ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
55
+ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
56
+ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
57
+ β”‚ β”‚ β–Ό β”‚ β”‚
58
+ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
59
+ β”‚ β”‚ β”‚ Tokenization β”‚ β”‚ β”‚
60
+ β”‚ β”‚ β”‚ & Embedding β”‚ β”‚ β”‚
61
+ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
62
+ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
63
+ β”‚ β”‚ β”‚
64
+ β”‚ β–Ό β”‚
65
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
66
+ β”‚ β”‚ Vector Search Module β”‚ β”‚
67
+ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
68
+ β”‚ β”‚ β”‚ HNSW Index β”‚ β”‚ Similarity β”‚ β”‚ β”‚
69
+ β”‚ β”‚ β”‚ (in-memory) β”‚ β”‚ Metrics β”‚ β”‚ β”‚
70
+ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
71
+ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
72
+ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
73
+ β”‚ β”‚ β–Ό β”‚ β”‚
74
+ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
75
+ β”‚ β”‚ β”‚ K-NN Search β”‚ β”‚ β”‚
76
+ β”‚ β”‚ β”‚ & Ranking β”‚ β”‚ β”‚
77
+ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
78
+ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
79
+ β”‚ β”‚ β”‚
80
+ β”‚ β–Ό β”‚
81
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
82
+ β”‚ β”‚ Merge Module β”‚ β”‚
83
+ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
84
+ β”‚ β”‚ β”‚ Strategy β”‚ β”‚ Confidence β”‚ β”‚ β”‚
85
+ β”‚ β”‚ β”‚ Selector β”‚ β”‚ Scorer β”‚ β”‚ β”‚
86
+ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
87
+ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
88
+ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
89
+ β”‚ β”‚ β–Ό β”‚ β”‚
90
+ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
91
+ β”‚ β”‚ β”‚ Code Application β”‚ β”‚ β”‚
92
+ β”‚ β”‚ β”‚ & Validation β”‚ β”‚ β”‚
93
+ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
94
+ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
95
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
96
+ ```
97
+
98
+ ## πŸ“¦ Crate Structure
99
+
100
+ ### Core Library (`agent-booster`)
101
+
102
+ ```rust
103
+ // Public API
104
+ pub struct AgentBooster {
105
+ parser: Parser,
106
+ embedder: Embedder,
107
+ config: Config,
108
+ }
109
+
110
+ impl AgentBooster {
111
+ pub fn new(config: Config) -> Result<Self>;
112
+ pub fn apply_edit(&mut self, request: EditRequest) -> Result<EditResult>;
113
+ pub fn batch_apply(&mut self, requests: Vec<EditRequest>) -> Result<Vec<EditResult>>;
114
+ }
115
+
116
+ // Request/Response types
117
+ pub struct EditRequest {
118
+ pub original_code: String,
119
+ pub edit_snippet: String,
120
+ pub language: Language,
121
+ pub options: EditOptions,
122
+ }
123
+
124
+ pub struct EditResult {
125
+ pub merged_code: String,
126
+ pub confidence: f32,
127
+ pub strategy: MergeStrategy,
128
+ pub metadata: EditMetadata,
129
+ }
130
+
131
+ // Configuration
132
+ pub struct Config {
133
+ pub model: EmbeddingModel,
134
+ pub confidence_threshold: f32,
135
+ pub max_chunks: usize,
136
+ pub cache_embeddings: bool,
137
+ }
138
+ ```
139
+
140
+ ### Native Addon (`agent-booster-native`)
141
+
142
+ ```rust
143
+ use napi::bindgen_prelude::*;
144
+ use napi_derive::napi;
145
+
146
+ #[napi]
147
+ pub struct AgentBoosterNative {
148
+ inner: agent_booster::AgentBooster,
149
+ }
150
+
151
+ #[napi]
152
+ impl AgentBoosterNative {
153
+ #[napi(constructor)]
154
+ pub fn new(config: Option<JsConfig>) -> Result<Self>;
155
+
156
+ #[napi]
157
+ pub async fn apply_edit(&mut self, request: JsEditRequest) -> Result<JsEditResult>;
158
+
159
+ #[napi]
160
+ pub async fn batch_apply(&mut self, requests: Vec<JsEditRequest>) -> Result<Vec<JsEditResult>>;
161
+ }
162
+
163
+ // JavaScript-compatible types
164
+ #[napi(object)]
165
+ pub struct JsConfig {
166
+ pub model: Option<String>,
167
+ pub confidence_threshold: Option<f32>,
168
+ }
169
+
170
+ #[napi(object)]
171
+ pub struct JsEditRequest {
172
+ pub original_code: String,
173
+ pub edit_snippet: String,
174
+ pub language: String,
175
+ }
176
+
177
+ #[napi(object)]
178
+ pub struct JsEditResult {
179
+ pub merged_code: String,
180
+ pub confidence: f32,
181
+ pub strategy: String,
182
+ }
183
+ ```
184
+
185
+ ### WASM Bindings (`agent-booster-wasm`)
186
+
187
+ ```rust
188
+ use wasm_bindgen::prelude::*;
189
+
190
+ #[wasm_bindgen]
191
+ pub struct AgentBoosterWasm {
192
+ inner: agent_booster::AgentBooster,
193
+ }
194
+
195
+ #[wasm_bindgen]
196
+ impl AgentBoosterWasm {
197
+ #[wasm_bindgen(constructor)]
198
+ pub fn new(config: JsValue) -> Result<AgentBoosterWasm, JsValue>;
199
+
200
+ #[wasm_bindgen(js_name = applyEdit)]
201
+ pub async fn apply_edit(&mut self, request: JsValue) -> Result<JsValue, JsValue>;
202
+
203
+ #[wasm_bindgen(js_name = batchApply)]
204
+ pub async fn batch_apply(&mut self, requests: JsValue) -> Result<JsValue, JsValue>;
205
+ }
206
+ ```
207
+
208
+ ## 🧩 Module Breakdown
209
+
210
+ ### 1. Parser Module (`parser.rs`)
211
+
212
+ **Responsibilities:**
213
+ - Initialize tree-sitter parsers for supported languages
214
+ - Parse source code into AST
215
+ - Extract semantic chunks (functions, classes, methods, etc.)
216
+ - Handle incremental parsing for performance
217
+
218
+ **Dependencies:**
219
+ - `tree-sitter`
220
+ - `tree-sitter-{language}` for each supported language
221
+
222
+ **Key Functions:**
223
+ ```rust
224
+ pub struct Parser {
225
+ parsers: HashMap<Language, TreeSitterParser>,
226
+ }
227
+
228
+ impl Parser {
229
+ pub fn new() -> Self;
230
+ pub fn parse(&mut self, code: &str, language: Language) -> Result<Tree>;
231
+ pub fn extract_chunks(&self, tree: &Tree, code: &str) -> Vec<CodeChunk>;
232
+ pub fn incremental_parse(&mut self, old_tree: &Tree, edits: &[InputEdit]) -> Result<Tree>;
233
+ }
234
+
235
+ pub struct CodeChunk {
236
+ pub code: String,
237
+ pub start_byte: usize,
238
+ pub end_byte: usize,
239
+ pub start_line: usize,
240
+ pub end_line: usize,
241
+ pub node_type: String,
242
+ pub parent_type: Option<String>,
243
+ pub complexity: u32,
244
+ }
245
+ ```
246
+
247
+ **Supported Languages (Priority Order):**
248
+ 1. JavaScript (MVP)
249
+ 2. TypeScript (MVP)
250
+ 3. Python
251
+ 4. Rust
252
+ 5. Go
253
+ 6. Java
254
+ 7. C/C++
255
+ 8. Ruby
256
+ 9. PHP
257
+ 10. C#
258
+
259
+ ### 2. Embeddings Module (`embeddings.rs`)
260
+
261
+ **Responsibilities:**
262
+ - Download and cache embedding models
263
+ - Tokenize code into model inputs
264
+ - Generate embeddings via ONNX Runtime
265
+ - Manage multiple model variants
266
+
267
+ **Dependencies:**
268
+ - `ort` (ONNX Runtime for Rust)
269
+ - `tokenizers` (HuggingFace tokenizers)
270
+ - `reqwest` (for model downloads)
271
+
272
+ **Key Functions:**
273
+ ```rust
274
+ pub struct Embedder {
275
+ session: ort::Session,
276
+ tokenizer: Tokenizer,
277
+ model_info: ModelInfo,
278
+ }
279
+
280
+ impl Embedder {
281
+ pub fn new(model: EmbeddingModel) -> Result<Self>;
282
+ pub fn embed(&self, text: &str) -> Result<Vec<f32>>;
283
+ pub fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Vec<f32>>>;
284
+ pub fn dimension(&self) -> usize;
285
+ }
286
+
287
+ pub enum EmbeddingModel {
288
+ JinaCodeV2, // 768 dim, best accuracy
289
+ AllMiniLML6V2, // 384 dim, faster
290
+ CodeBERT, // 768 dim, alternative
291
+ Custom(PathBuf), // User-provided model
292
+ }
293
+
294
+ pub struct ModelInfo {
295
+ pub name: String,
296
+ pub dimension: usize,
297
+ pub max_tokens: usize,
298
+ pub download_url: String,
299
+ }
300
+ ```
301
+
302
+ **Model Management:**
303
+ ```rust
304
+ pub struct ModelManager;
305
+
306
+ impl ModelManager {
307
+ pub fn download_model(model: &EmbeddingModel) -> Result<PathBuf>;
308
+ pub fn get_cached_model(model: &EmbeddingModel) -> Option<PathBuf>;
309
+ pub fn cache_dir() -> PathBuf; // ~/.cache/agent-booster/models/
310
+ }
311
+ ```
312
+
313
+ ### 3. Vector Search Module (`vector.rs`)
314
+
315
+ **Responsibilities:**
316
+ - Build HNSW index from embeddings
317
+ - Perform K-NN similarity search
318
+ - Calculate similarity scores (cosine, dot product)
319
+ - Rank results by confidence
320
+
321
+ **Dependencies:**
322
+ - `hnsw` or custom HNSW implementation
323
+ - `ndarray` for vector operations
324
+
325
+ **Key Functions:**
326
+ ```rust
327
+ pub struct VectorIndex {
328
+ index: Hnsw<f32>,
329
+ metadata: Vec<ChunkMetadata>,
330
+ dimension: usize,
331
+ }
332
+
333
+ impl VectorIndex {
334
+ pub fn new(dimension: usize) -> Self;
335
+ pub fn insert(&mut self, id: usize, vector: &[f32], metadata: ChunkMetadata);
336
+ pub fn search(&self, query: &[f32], k: usize) -> Vec<SearchResult>;
337
+ pub fn clear(&mut self);
338
+ }
339
+
340
+ pub struct SearchResult {
341
+ pub chunk_id: usize,
342
+ pub similarity: f32,
343
+ pub metadata: ChunkMetadata,
344
+ }
345
+
346
+ pub struct ChunkMetadata {
347
+ pub chunk: CodeChunk,
348
+ pub embedding_cache: Option<Vec<f32>>,
349
+ }
350
+
351
+ // Similarity metrics
352
+ pub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32;
353
+ pub fn dot_product(a: &[f32], b: &[f32]) -> f32;
354
+ pub fn euclidean_distance(a: &[f32], b: &[f32]) -> f32;
355
+ ```
356
+
357
+ ### 4. Merge Module (`merge.rs`)
358
+
359
+ **Responsibilities:**
360
+ - Select merge strategy based on confidence
361
+ - Apply edits to code
362
+ - Validate syntax after merge
363
+ - Generate confidence scores
364
+
365
+ **Dependencies:**
366
+ - Tree-sitter (for validation)
367
+
368
+ **Key Functions:**
369
+ ```rust
370
+ pub struct Merger {
371
+ validator: SyntaxValidator,
372
+ }
373
+
374
+ impl Merger {
375
+ pub fn new() -> Self;
376
+ pub fn merge(&self, request: MergeRequest) -> Result<MergeResult>;
377
+ }
378
+
379
+ pub struct MergeRequest {
380
+ pub original_code: String,
381
+ pub edit_snippet: String,
382
+ pub best_match: SearchResult,
383
+ pub language: Language,
384
+ }
385
+
386
+ pub struct MergeResult {
387
+ pub code: String,
388
+ pub strategy: MergeStrategy,
389
+ pub confidence: f32,
390
+ }
391
+
392
+ pub enum MergeStrategy {
393
+ ExactReplace, // Replace entire matched chunk
394
+ InsertBefore, // Insert before matched chunk
395
+ InsertAfter, // Insert after matched chunk
396
+ Append, // Append to end of file
397
+ FuzzyReplace, // GumTree-based fuzzy merge
398
+ }
399
+
400
+ // Strategy selection
401
+ pub fn select_strategy(similarity: f32, context: &MergeContext) -> MergeStrategy;
402
+
403
+ // Confidence calculation
404
+ pub fn calculate_confidence(
405
+ similarity: f32,
406
+ strategy: MergeStrategy,
407
+ syntax_valid: bool,
408
+ ) -> f32;
409
+ ```
410
+
411
+ **Merge Strategy Decision Tree:**
412
+ ```rust
413
+ fn select_strategy(similarity: f32, chunk: &CodeChunk) -> MergeStrategy {
414
+ match similarity {
415
+ s if s >= 0.95 => MergeStrategy::ExactReplace,
416
+ s if s >= 0.85 => {
417
+ if chunk.node_type == "function_declaration" {
418
+ MergeStrategy::ExactReplace
419
+ } else {
420
+ MergeStrategy::InsertAfter
421
+ }
422
+ },
423
+ s if s >= 0.65 => MergeStrategy::InsertAfter,
424
+ s if s >= 0.50 => MergeStrategy::FuzzyReplace,
425
+ _ => MergeStrategy::Append,
426
+ }
427
+ }
428
+ ```
429
+
430
+ ## πŸš€ Performance Optimizations
431
+
432
+ ### 1. Lazy Initialization
433
+ ```rust
434
+ pub struct AgentBooster {
435
+ parser: OnceCell<Parser>,
436
+ embedder: OnceCell<Embedder>,
437
+ config: Config,
438
+ }
439
+
440
+ impl AgentBooster {
441
+ pub fn new(config: Config) -> Self {
442
+ // Don't initialize parser/embedder until first use
443
+ Self {
444
+ parser: OnceCell::new(),
445
+ embedder: OnceCell::new(),
446
+ config,
447
+ }
448
+ }
449
+ }
450
+ ```
451
+
452
+ ### 2. Embedding Caching
453
+ ```rust
454
+ pub struct EmbeddingCache {
455
+ cache: HashMap<u64, Vec<f32>>, // hash(code) -> embedding
456
+ max_size: usize,
457
+ }
458
+
459
+ impl EmbeddingCache {
460
+ pub fn get(&self, code: &str) -> Option<&Vec<f32>>;
461
+ pub fn insert(&mut self, code: &str, embedding: Vec<f32>);
462
+ pub fn clear(&mut self);
463
+ }
464
+ ```
465
+
466
+ ### 3. Parallel Processing
467
+ ```rust
468
+ use rayon::prelude::*;
469
+
470
+ pub fn batch_apply(
471
+ &mut self,
472
+ requests: Vec<EditRequest>
473
+ ) -> Result<Vec<EditResult>> {
474
+ requests
475
+ .par_iter()
476
+ .map(|req| self.apply_edit(req.clone()))
477
+ .collect()
478
+ }
479
+ ```
480
+
481
+ ### 4. Incremental Parsing
482
+ ```rust
483
+ pub struct IncrementalParser {
484
+ last_tree: Option<Tree>,
485
+ last_code: String,
486
+ }
487
+
488
+ impl IncrementalParser {
489
+ pub fn parse(&mut self, code: &str) -> Result<Tree> {
490
+ if let Some(old_tree) = &self.last_tree {
491
+ let edits = compute_edits(&self.last_code, code);
492
+ self.parser.parse_incremental(code, old_tree, &edits)
493
+ } else {
494
+ self.parser.parse(code)
495
+ }
496
+ }
497
+ }
498
+ ```
499
+
500
+ ## πŸ§ͺ Testing Strategy
501
+
502
+ ### Unit Tests
503
+ ```rust
504
+ #[cfg(test)]
505
+ mod tests {
506
+ #[test]
507
+ fn test_exact_match() {
508
+ let booster = AgentBooster::new(Config::default()).unwrap();
509
+ let result = booster.apply_edit(EditRequest {
510
+ original_code: "function foo() { return 1; }",
511
+ edit_snippet: "function foo() { return 2; }",
512
+ language: Language::JavaScript,
513
+ ..Default::default()
514
+ }).unwrap();
515
+
516
+ assert_eq!(result.strategy, MergeStrategy::ExactReplace);
517
+ assert!(result.confidence > 0.95);
518
+ }
519
+ }
520
+ ```
521
+
522
+ ### Integration Tests
523
+ ```rust
524
+ #[test]
525
+ fn test_real_world_edit() {
526
+ let original = include_str!("../../fixtures/complex.ts");
527
+ let edit = include_str!("../../fixtures/complex.edit.ts");
528
+ let expected = include_str!("../../fixtures/complex.expected.ts");
529
+
530
+ let result = apply_edit(original, edit, Language::TypeScript).unwrap();
531
+ assert_eq!(result.code, expected);
532
+ }
533
+ ```
534
+
535
+ ### Benchmark Tests
536
+ ```rust
537
+ use criterion::{black_box, criterion_group, criterion_main, Criterion};
538
+
539
+ fn benchmark_apply_edit(c: &mut Criterion) {
540
+ let booster = AgentBooster::new(Config::default()).unwrap();
541
+
542
+ c.bench_function("apply_simple_edit", |b| {
543
+ b.iter(|| {
544
+ booster.apply_edit(black_box(simple_request()))
545
+ })
546
+ });
547
+ }
548
+
549
+ criterion_group!(benches, benchmark_apply_edit);
550
+ criterion_main!(benches);
551
+ ```
552
+
553
+ ## πŸ”’ Error Handling
554
+
555
+ ```rust
556
+ #[derive(Debug, thiserror::Error)]
557
+ pub enum AgentBoosterError {
558
+ #[error("Failed to parse code: {0}")]
559
+ ParseError(String),
560
+
561
+ #[error("Failed to generate embedding: {0}")]
562
+ EmbeddingError(String),
563
+
564
+ #[error("No suitable merge location found (confidence: {0:.2})")]
565
+ LowConfidence(f32),
566
+
567
+ #[error("Merge resulted in invalid syntax")]
568
+ InvalidSyntax,
569
+
570
+ #[error("Unsupported language: {0}")]
571
+ UnsupportedLanguage(String),
572
+
573
+ #[error("Model not found: {0}")]
574
+ ModelNotFound(String),
575
+
576
+ #[error("IO error: {0}")]
577
+ IoError(#[from] std::io::Error),
578
+ }
579
+
580
+ pub type Result<T> = std::result::Result<T, AgentBoosterError>;
581
+ ```
582
+
583
+ ## πŸ“Š Memory Management
584
+
585
+ ### Memory Budget
586
+ ```
587
+ Total memory target: < 500MB
588
+
589
+ Breakdown:
590
+ - ONNX model: 150-200MB (loaded once)
591
+ - Tokenizer: 20-30MB
592
+ - Tree-sitter: 10-20MB
593
+ - HNSW index: 50-100MB (per file, ephemeral)
594
+ - Working memory: 100-150MB
595
+ - Overhead: 20-50MB
596
+ ```
597
+
598
+ ### Cleanup Strategy
599
+ ```rust
600
+ impl Drop for AgentBooster {
601
+ fn drop(&mut self) {
602
+ // Clear caches
603
+ self.embedding_cache.clear();
604
+ // Free ONNX session
605
+ // (handled by ort automatically)
606
+ }
607
+ }
608
+ ```
609
+
610
+ ## πŸ”„ Data Flow
611
+
612
+ ```
613
+ Input: { original_code, edit_snippet, language }
614
+ β”‚
615
+ β–Ό
616
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
617
+ β”‚ 1. Parse Original Code β”‚
618
+ β”‚ - Tree-sitter β”‚
619
+ β”‚ - Extract chunks (5-50) β”‚
620
+ β”‚ - Cache AST β”‚
621
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
622
+ β”‚ CodeChunk[]
623
+ β–Ό
624
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
625
+ β”‚ 2. Generate Embeddings β”‚
626
+ β”‚ - Tokenize chunks β”‚
627
+ β”‚ - ONNX inference (batch) β”‚
628
+ β”‚ - Cache embeddings β”‚
629
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
630
+ β”‚ Vec<Vec<f32>>
631
+ β–Ό
632
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
633
+ β”‚ 3. Build Vector Index β”‚
634
+ β”‚ - HNSW construction β”‚
635
+ β”‚ - In-memory only β”‚
636
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
637
+ β”‚ VectorIndex
638
+ β–Ό
639
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
640
+ β”‚ 4. Embed Edit Snippet β”‚
641
+ β”‚ - Tokenize β”‚
642
+ β”‚ - ONNX inference β”‚
643
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
644
+ β”‚ Vec<f32>
645
+ β–Ό
646
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
647
+ β”‚ 5. Vector Similarity Search β”‚
648
+ β”‚ - K-NN (k=5) β”‚
649
+ β”‚ - Cosine similarity β”‚
650
+ β”‚ - Rank by score β”‚
651
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
652
+ β”‚ SearchResult[]
653
+ β–Ό
654
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
655
+ β”‚ 6. Select Merge Strategy β”‚
656
+ β”‚ - Analyze confidence β”‚
657
+ β”‚ - Consider context β”‚
658
+ β”‚ - Choose strategy β”‚
659
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
660
+ β”‚ MergeStrategy
661
+ β–Ό
662
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
663
+ β”‚ 7. Apply Merge β”‚
664
+ β”‚ - String manipulation β”‚
665
+ β”‚ - Preserve formatting β”‚
666
+ β”‚ - Handle edge cases β”‚
667
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
668
+ β”‚ String
669
+ β–Ό
670
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
671
+ β”‚ 8. Validate Syntax β”‚
672
+ β”‚ - Parse merged code β”‚
673
+ β”‚ - Check for errors β”‚
674
+ β”‚ - Calculate final confidenceβ”‚
675
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
676
+ β”‚
677
+ β–Ό
678
+ Output: { merged_code, confidence, strategy, metadata }
679
+ ```
680
+
681
+ ## 🎯 Future Enhancements
682
+
683
+ ### Phase 2: Advanced Features
684
+ - [ ] Multi-file refactoring support
685
+ - [ ] Custom model fine-tuning pipeline
686
+ - [ ] LSP integration for better context
687
+ - [ ] Conflict resolution for concurrent edits
688
+
689
+ ### Phase 3: Enterprise Features
690
+ - [ ] Team collaboration features
691
+ - [ ] Audit logging
692
+ - [ ] Custom security policies
693
+ - [ ] On-premise deployment
694
+
695
+ ### Phase 4: AI Enhancements
696
+ - [ ] Learn from corrections
697
+ - [ ] Adaptive confidence thresholds
698
+ - [ ] Context-aware embeddings
699
+ - [ ] Cross-language refactoring