image.vision\aoci\aoci_router.mbt

100% · 49 / 49 points · 107 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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