Skip to content

Commit 0d27f0a

Browse files
authored
Merge pull request #293 from SharedCode/omni2
Enhance KB routing and navigation with specialized handlers
2 parents 8400dc2 + 921e847 commit 0d27f0a

65 files changed

Lines changed: 4402 additions & 729 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

AI_COPILOT.md

Lines changed: 180 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,189 @@ SOP treats operational guidance as managed data instead of static prompt text.
6666

6767
The ReAct loop in SOP is progressive by design, not a blind repeat-until-success loop.
6868

69-
#### Deep KB path routing and explicit LLM suffixes
69+
#### Gate 1: Advanced KB Routing & Specialized Focus
7070

71-
Two retrieval patterns are now recognized as first-class routing cases:
71+
Gate 1 now provides powerful, deterministic knowledge base routing with flexible syntax that supports hierarchical category navigation, text search, and LLM-assisted filtering.
7272

73-
* **Deep slash-path KB prompts** such as `sop:/a/b/c` or `a/b/c/d` are treated as focused KB retrieval requests. The system tries direct category-path lookup first, which is faster and more deterministic than falling back to a generic discovery loop.
74-
* **Explicit post-retrieval synthesis markers** use the form `:LLM <instruction>`, for example `a/b/c:LLM extract Apple company from the matches`. This suffix is reserved for cases where the user wants the model to summarize, filter, or synthesize after the KB results are already retrieved.
73+
##### Routing Syntax Patterns
7574

76-
In other words, pure path-style lookup stays grounded in KB retrieval, while `:LLM ...` is the opt-in switch for higher-level reasoning over the matched items.
75+
**1. Root Category Display**
76+
77+
Query just the KB name to explore available root categories:
78+
79+
```
80+
omni:<KB> # Display root categories
81+
```
82+
83+
**Example:**
84+
85+
```
86+
Query: omni:sop
87+
88+
Response:
89+
Available Categories:
90+
91+
• Language (150 items, 5 subcategories)
92+
Programming language guides and tutorials
93+
Navigate: omni:sop:language
94+
95+
• Architecture (89 items, 3 subcategories)
96+
System design and architecture patterns
97+
Navigate: omni:sop:architecture
98+
99+
• Operations (203 items, 7 subcategories)
100+
DevOps, deployment, and operational guides
101+
Navigate: omni:sop:operations
102+
```
103+
104+
This provides directory-style exploration without needing to know category names upfront.
105+
106+
**Pagination:** Category displays show 20 items per page. Navigation (supports both `:` and `/` separators):
107+
```
108+
omni:sop # Page 1 (default)
109+
omni:sop:page:2 # Page 2
110+
omni:sop/page/3 # Page 3 (slash separator)
111+
112+
omni:sop:language:page:2 # Page 2 of subcategories under 'language'
113+
omni:sop/language/page/2 # Same, using slash separator
114+
```
115+
116+
When multiple pages exist, the response shows:
117+
```
118+
Available Categories: (Page 2 of 5, showing 21-40 of 87)
119+
...
120+
Previous: omni:sop:page:1 | Next: omni:sop:page:3
121+
```
122+
123+
**2. Hierarchical Category Path Routing**
124+
125+
Any-depth category hierarchies are supported with colon-separated paths:
126+
127+
```
128+
omni:<KB>:cat1 # Single level
129+
omni:<KB>:cat1:subcat1.1 # Two levels
130+
omni:<KB>:cat1:subcat1.1:subsubcat1.1.1 # Deep hierarchy
131+
omni:sop:operations:performance:caching # Real-world example
132+
```
133+
134+
The system performs intelligent category resolution:
135+
- **Direct lookup** via `CategoriesByPath` B-Tree (O(1) when exact match exists)
136+
- **Semantic fallback** using category embeddings when lexical match fails
137+
- **Text-based discovery** for natural language category queries
138+
139+
**3. The `:llm <instruction>` Meta-Token**
140+
141+
**3. The `:llm <instruction>` Meta-Token**
142+
143+
Add `:llm <instruction>` after any routing query to have the LLM process the retrieved results:
144+
145+
```
146+
omni:sop:operations:performance:llm summarize
147+
omni:sop:language bindings:c#:llm explain with code examples
148+
omni:myapp:cat1:subcat1.1:llm extract top 5 by relevance
149+
```
150+
151+
**How it works:**
152+
- The `:llm <instruction>` portion is **stripped from the query** before KB search
153+
- KB retrieval proceeds normally using the clean category path
154+
- Results are passed to the LLM along with the instruction as meta-guidance
155+
- The LLM processes, filters, or synthesizes the matches according to the instruction
156+
157+
**Example flow:**
158+
```
159+
Input: omni:sop:operations:performance:caching:llm summarize the top 3
160+
Parse: category_path = "operations/performance/caching"
161+
search_text = (none)
162+
llm_instruction = "summarize the top 3"
163+
Execute: Search KB → 8 matches found
164+
Pass to LLM: "Here are 8 matches. summarize the top 3"
165+
```
166+
167+
**4. Subcategory Navigation (Path-Level)**
168+
169+
When a category path returns no direct items, Gate 1 automatically provides subcategory navigation:
170+
171+
```
172+
Query: omni:sop:language bindings
173+
174+
Response (no items in parent category):
175+
**Category "language bindings" has no direct items.**
176+
177+
**Available subcategories (3):**
178+
1. **c#** (12 items) - C# language binding documentation
179+
2. **java** (8 items) - Java integration guides
180+
3. **python** (15 items) - Python SDK reference
181+
182+
*Navigate deeper: `omni:sop:language bindings:c#`*
183+
```
184+
185+
This provides granular navigation when exploring deep category hierarchies.
186+
187+
**5. Quoted Text Search (Roadmap)**
188+
189+
Future support for combined category + text search:
190+
191+
```
192+
omni:sop:language bindings "java tutorial"
193+
→ Search for "java tutorial" within the language bindings category
194+
195+
omni:sop:operations:performance "caching strategies":llm summarize top 3
196+
→ Search for text within category, LLM summarizes results
197+
```
198+
199+
**Proposed parsing logic:**
200+
- Category path: everything before the first quote
201+
- Search text: content within quotes
202+
- LLM instruction: everything after `:llm`
203+
204+
##### Three-Way Routing Decision
205+
206+
Gate 1 makes intelligent decisions based on result count:
207+
208+
1. **Case 1: Few matches (1-5)** → Direct display, bypass LLM
209+
- Shows results immediately with category paths
210+
- Includes navigation tips
211+
212+
2. **Case 2: `:llm` instruction present** → LLM processes matches
213+
- User explicitly requested LLM analysis
214+
- LLM receives clean query + results + instruction
215+
216+
3. **Case 3: Too many matches (>5)** → LLM reduction
217+
- Automatic LLM summarization to avoid overwhelming the user
218+
- Instruction: "Analyze and present the most relevant matches"
219+
220+
##### Clean Query Architecture
221+
222+
The `:llm` token is treated as a **meta-instruction**, not part of the actual search query:
223+
224+
```go
225+
type TaskContextClassification struct {
226+
CleanQuery string // Query without :llm meta-token
227+
LLMInstruction string // Extracted instruction
228+
KBSearchResults string // Retrieved matches
229+
DirectDisplay bool // Whether to bypass LLM
230+
}
231+
```
232+
233+
This ensures:
234+
- KB search operates on clean, semantic queries
235+
- LLM receives proper context (clean query + results + instruction)
236+
- No confusion between user intent and meta-commands
237+
238+
##### Flexible Hierarchy Support
239+
240+
All routing patterns work at any depth:
241+
242+
```
243+
✅ omni:myapp:a:b:c:d:e:f:g:llm <instruction>
244+
✅ omni:medical:diagnosis:cardiology:procedures:stent:llm explain risks
245+
✅ omni:sop:architecture:patterns:microservices:llm compare with monolith
246+
```
247+
248+
The routing system automatically:
249+
- Normalizes colon separators to forward slashes for internal paths
250+
- Preserves the full hierarchical context for LLM enrichment
251+
- Strips only the `:llm` meta-token, not the category structure
77252

78253
* **Clarification First When Needed**: Before routing and execution, Gate 0 can now keep the interaction in a clarification-first mode. If the assistant asks a focused clarification question, the next user reply is rewritten back onto the original target ask and the normal execution path resumes.
79254
* **Macro Then Micro**: Routing gates prepare the Ask frame first. The inner native ReAct loop then executes inside that frame without re-running the gates on every retry.

CHANGELOG.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
# Changelog
22

3+
## SOP V2 build 54 (Upcoming)
4+
- **Gate 1 Advanced KB Routing**: Major enhancements to specialized focused routing for knowledge base queries.
5+
- **Root Category Navigation**: Query `omni:<kb>` to display all root categories with item counts and subcategory information.
6+
- Example: `omni:sop` shows all top-level categories in the SOP knowledge base
7+
- Provides directory-style exploration without needing to know category names upfront
8+
- Navigation hints included for deeper exploration (e.g., "Navigate: omni:sop:language")
9+
- **Pagination**: 20 categories per page with `:page:<number>` or `/page/<number>` syntax
10+
- `omni:sop:page:2` or `omni:sop/page/2` - View page 2 of root categories
11+
- `omni:sop:language:page:3` or `omni:sop/language/page/3` - View page 3 of subcategories
12+
- Supports both `:` and `/` as separators (matches user's query style)
13+
- Shows page info: "(Page 2 of 5, showing 21-40 of 87)"
14+
- Navigation hints: "Previous: omni:sop:page:1 | Next: omni:sop:page:3"
15+
- LLM filtering suggestion for large sets (>40 categories)
16+
- **`:llm <instruction>` Meta-Token**: Added support for explicit LLM post-processing instructions using `:llm` suffix (e.g., `omni:sop:operations:performance:llm summarize top 3`).
17+
- **Clean Query Separation**: The `:llm` meta-token is automatically stripped from the KB search query and treated as post-retrieval guidance.
18+
- **TaskContextClassification Fields**: Added `CleanQuery` and `LLMInstruction` fields to properly separate user intent from meta-commands.
19+
- **Three-Way Routing**: Intelligent decision-making based on result count and `:llm` presence:
20+
- `:llm` present → LLM processes with instruction (highest priority)
21+
- 1-5 matches → Direct display (no LLM)
22+
- 6+ matches → Automatic LLM summarization
23+
- **Flexible Hierarchy Support**: Full support for any-depth category paths (e.g., `omni:kb:cat1:subcat1.1:subsubcat1.1.1:...`).
24+
- **Subcategory Navigation**: When a category path has no direct items (and no `:llm` instruction), automatically returns child categories with item counts and descriptions as navigation hints.
25+
- **Enhanced Parsing**: New `stripLLMInstruction()` function ensures consistent meta-token extraction across all query patterns.
26+
- **Architecture Improvements**:
27+
- `getSubcategories()` function for root and path-level category display
28+
- `buildKBEnrichedQuery()` now uses clean queries without meta-tokens for proper LLM context
29+
- `trySpecializedFocusedRouting()` handles root navigation, flexible hierarchy, and meta-token parsing
30+
- Comprehensive test coverage for all routing patterns and hierarchy depths
31+
- **Roadmap - Quoted Text Search**: Proposed support for combined category + text queries (e.g., `omni:sop:language bindings "java tutorial"`).
32+
- **Documentation Updates**: Updated `AI_COPILOT.md`, `AI_COPILOT_USAGE.md`, and `IMPLEMENTATION.md` with comprehensive routing guides including root category navigation.
33+
334
## SOP V2 build 53 (Upcoming)
435
- **Schema Format Enhancement**: Introduced flat schema format for better LLM understanding and correlation with Store Relations.
536
- **New Fields**: Added `FlatSchema`, `KeyFields`, and `ValueFields` to `StoreInfo` for improved schema representation.

IMPLEMENTATION.md

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,83 @@ If critical tool manuals are fragmented and left to probabilistic retrieval, a d
244244

245245
### Three-Gate Routing Architecture
246246

247-
Gate 1: Focused prefix routing.
247+
Gate 1: Focused prefix routing with KB specialization.
248+
249+
**Input patterns:**
250+
- Root category display: `omni:<kb>` (e.g., `omni:sop`)
251+
- Root with paging: `omni:<kb>:page:<number>` or `omni:<kb>/page/<number>` (e.g., `omni:sop:page:2` or `omni:sop/page/2`)
252+
- Category path with paging: `omni:<kb>:<path>:page:<number>` (e.g., `omni:sop:language:page:3` or `omni:sop/language/page/3`)
253+
- Explicit namespace: `omni:stores:users`
254+
- Deep hierarchical KB paths: `omni:sop:operations:performance:caching`
255+
- KB paths with LLM instruction: `omni:sop:language bindings:c#:llm summarize`
256+
- Flexible depth support: `omni:<kb>:cat1:subcat1.1:subsubcat1.1.1`
257+
258+
**Processing flow:**
259+
1. **Pattern recognition**: Detects `omni:` prefix routing queries via `looksLikeSpecializedRoutingQuery()`
260+
2. **KB extraction**: Identifies target KB name (defaults to "sop" if not specified)
261+
3. **Page extraction**: Uses `extractPageNumber()` to parse and remove `:page:<number>` suffix (defaults to 1)
262+
4. **Root navigation check**: If query is just `omni:<kb>` (no category path), retrieve and display root categories
263+
5. **Meta-token parsing**: Uses `stripLLMInstruction()` to extract and separate the `:llm <instruction>` suffix
264+
6. **Category path resolution**: Normalizes colon-separated paths to forward slashes for internal routing
265+
7. **Search orchestration**: Delegates to `searchKnowledgeBase()` with clean query (meta-token stripped)
266+
8. **Subcategory fallback**: If no items found at path (and no `:llm`), calls `getSubcategories()` with page number
267+
9. **Pagination display**: Shows page info, navigation hints (Previous/Next), and LLM filtering suggestion for large sets
268+
269+
**Key architecture principles:**
270+
- **Clean query separation**: The `:llm` meta-token is treated as post-retrieval guidance, not part of the search query
271+
- **Hierarchical flexibility**: Supports any depth of category nesting (1 to N levels)
272+
- **Intelligent fallback**: Direct category path lookup → semantic category matching → text search fallback
273+
- **Subcategory navigation**: When a category has no items, returns child categories as navigation hints
274+
275+
**Three-way routing decision:**
276+
1. **Case 1 (1-5 matches)**: Direct display, bypass LLM processing
277+
2. **Case 2 (`:llm` present)**: LLM processes matches according to user instruction
278+
3. **Case 3 (6+ matches)**: Automatic LLM summarization to reduce cognitive load
279+
280+
**TaskContextClassification fields:**
281+
```go
282+
type TaskContextClassification struct {
283+
CleanQuery string // Query without :llm meta-token
284+
LLMInstruction string // Extracted instruction from :llm suffix
285+
KBSearchResults string // Retrieved KB matches
286+
KBMatchCount int // Number of matches found
287+
DirectDisplay bool // Whether to bypass LLM (Case 1)
288+
}
289+
```
248290

249-
- Input shape: explicit namespace such as omni:stores:users, plus deep slash-path KB prompts such as `sop:/a/b/c` or `a/b/c/d`.
250-
- Action: parse hard constraints and classify only the missing parts (mainly layers and CRUD intent). Deep path-style KB prompts now short-circuit into focused KB retrieval rather than drifting into the generic discovery loop.
251-
- Result: deterministic route with low token overhead, and a clean path for direct category-path answers.
291+
**Implementation reference:**
292+
- `trySpecializedFocusedRouting()` in `ai/agent/classifier.go`: Main routing logic
293+
- `stripLLMInstruction()` in `ai/agent/copilottools.search.go`: Meta-token parsing
294+
- `searchKnowledgeBase()` in `ai/agent/copilottools.search.go`: KB search orchestration
295+
- `buildKBEnrichedQuery()` in `ai/agent/copilot.go`: LLM context assembly
296+
297+
**Example query flow:**
298+
```
299+
Input: omni:sop:operations:performance:caching:llm summarize top 3
300+
Parse: kb_name = "sop"
301+
category_path = "operations/performance/caching"
302+
clean_query = "operations:performance:caching"
303+
llm_instruction = "summarize top 3"
304+
Execute: KB.Search(category_path) → 8 matches
305+
Route: Case 2 (LLM instruction present)
306+
Output: buildKBEnrichedQuery(clean_query, results, instruction)
307+
→ LLM receives: query + 8 matches + "summarize top 3"
308+
```
309+
310+
**Roadmap - Quoted text search:**
311+
Future support for explicit text queries within categories:
312+
```
313+
omni:sop:language bindings "java tutorial"
314+
→ category_path = "language bindings"
315+
→ search_text = "java tutorial"
316+
317+
omni:sop:operations "caching strategies":llm summarize
318+
→ Combined category + text + LLM instruction
319+
```
252320

253-
Optional follow-on instruction format:
321+
**Action:** Parse hard constraints, extract meta-tokens, and classify missing parts (layers, CRUD intent). Deep path-style KB prompts now short-circuit into focused KB retrieval with proper meta-token handling.
254322

255-
- If the user adds an explicit `:LLM <instruction>` suffix, for example `a/b/c:LLM extract Apple company from the matches`, the ask remains grounded in KB retrieval but the model is invited to synthesize or narrow the returned candidates after the path lookup.
323+
**Result:** Deterministic route with low token overhead, clean separation of query vs. instruction, and intelligent LLM delegation based on result count.
256324

257325
Gate 2: MRU continuity or switch routing.
258326

0 commit comments

Comments
 (0)