image.vision\aoci\aoci_router.mbt
100% · 49 / 49 points · 107 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # AOCI 路由器 AOCI Router - 3
/// - 4
/// 自然语言 → 模块定位 → 按需加载 - 5
/// 自然语言→模块路由→按需加载 Natural language → module routing → on-demand loading - 6
// AOCI-Router — 自然语言 → 模块定位 → 按需加载 - 7
// 关键词匹配 + 排序 + Top-K 返回 - 8
- 9
///| - 10
pub fn route(index : SymbolIndex, query : String) -> Array[SymbolCoord] { - 11●
let query_keywords = split_query(query) - 12
let scored : Array[(SymbolCoord, Int32)] = [] - 13
for sym in index.symbols { - 14●
let score = score_symbol(sym, query_keywords) - 15
if score > 0 { - 16●
scored.push((sym, score)) - 17
} - 18
} - 19●
sort_by_score(scored) - 20
} - 21
- 22
///| - 23
pub fn route_top_k( - 24
index : SymbolIndex, - 25
query : String, - 26
k : Int32, - 27
) -> Array[SymbolCoord] { - 28●
let results = route(index, query) - 29
let top : Array[SymbolCoord] = [] - 30●
let limit = if k < results.length() { k } else { results.length() } - 31
for i in 0..<limit { - 32●
top.push(results[i]) - 33
} - 34
top - 35
} - 36
- 37
///| - 38
pub fn route_by_package( - 39
index : SymbolIndex, - 40
pkg : String, - 41
) -> Array[SymbolCoord] { - 42●
index.find_by_package(pkg) - 43
} - 44
- 45
///| - 46
pub fn route_by_kind( - 47
index : SymbolIndex, - 48
kind : SymbolKind, - 49
) -> Array[SymbolCoord] { - 50●
index.find_by_kind(kind) - 51
} - 52
- 53
///| - 54
fn split_query(query : String) -> Array[String] { - 55●
let keywords : Array[String] = [] - 56●
for part in query.split(" ") { - 57●
let trimmed = part.trim().to_owned() - 58●
let lowered = trimmed.to_lower() - 59●
if lowered.length() > 0 { - 60●
keywords.push(lowered) - 61
} - 62
} - 63
keywords - 64
} - 65
- 66
///| - 67
fn score_symbol(sym : SymbolCoord, query_keywords : Array[String]) -> Int32 { - 68●
let mut score = 0 - 69●
let name_lower = sym.name.to_lower() - 70
for qkw in query_keywords { - 71●
if name_lower.contains(qkw) { - 72●
score = score + 10 - 73
} - 74
for skw in sym.keywords { - 75●
if skw.to_lower() == qkw { - 76●
score = score + 5 - 77
} - 78
} - 79●
if sym.doc.to_lower().contains(qkw) { - 80●
score = score + 2 - 81
} - 82●
if sym.signature.to_lower().contains(qkw) { - 83●
score = score + 1 - 84
} - 85
} - 86
score - 87
} - 88
- 89
///| - 90
fn sort_by_score(scored : Array[(SymbolCoord, Int32)]) -> Array[SymbolCoord] { - 91●
let arr = scored - 92●
let n = arr.length() - 93
for i in 0..<n { - 94●
for j in 0..<(n - 1 - i) { - 95●
if arr[j].1 < arr[j + 1].1 { - 96●
let tmp = arr[j] - 97
arr[j] = arr[j + 1] - 98
arr[j + 1] = tmp - 99
} - 100
} - 101
} - 102
let result : Array[SymbolCoord] = [] - 103
for entry in arr { - 104●
result.push(entry.0) - 105
} - 106
result - 107
}