image.domain\agent_kit\agent_parser.mbt

100% · 32 / 32 points · 77 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/// # Agent 解析器 Agent Parser
  3. 3///
  4. 4/// 指令解析与验证
  5. 5/// 描述 Instruction parsing and validation
  6. 6// Agent-Kit 解析器 — 自然语言 → 意图序列
  7. 7// nl_parse: 将自然语言查询解析为操作意图序列
  8. 8
  9. 9///|
  10. 10pub fn nl_parse(query : String) -> Array[Intent] {
  11. 11● let intents : Array[Intent] = []
  12. 12● let words = split_words(query)
  13. 13 for word in words {
  14. 14● let intent = match_word(word)
  15. 15 if intent is Some(i) {
  16. 16● let mut found = false
  17. 17 for existing in intents {
  18. 18● if existing == i {
  19. 19● found = true
  20. 20 break
  21. 21 }
  22. 22 }
  23. 23 if !found {
  24. 24● intents.push(i)
  25. 25 }
  26. 26 }
  27. 27 }
  28. 28● if intents.length() == 0 {
  29. 29● intents.push(Identity)
  30. 30 }
  31. 31 intents
  32. 32}
  33. 33
  34. 34///|
  35. 35fn split_words(query : String) -> Array[String] {
  36. 36● let words : Array[String] = []
  37. 37● for part in query.split(" ") {
  38. 38● let trimmed = part.trim().to_owned()
  39. 39● if trimmed.length() > 0 {
  40. 40● words.push(trimmed.to_lower())
  41. 41 }
  42. 42 }
  43. 43 words
  44. 44}
  45. 45
  46. 46///|
  47. 47fn match_word(word : String) -> Intent? {
  48. 48● let candidates = [
  49. 49 Denoise,
  50. 50 EdgeDetect,
  51. 51 Blur,
  52. 52 Sharpen,
  53. 53 Grayscale,
  54. 54 Rotate,
  55. 55 FlipH,
  56. 56 FlipV,
  57. 57 Identity,
  58. 58 ]
  59. 59 for candidate in candidates {
  60. 60● if candidate.matches(word) {
  61. 61● return Some(candidate)
  62. 62 }
  63. 63 }
  64. 64 None
  65. 65}
  66. 66
  67. 67///|
  68. 68pub fn nl_describe(intents : Array[Intent]) -> String {
  69. 69● if intents.length() == 0 {
  70. 70● return "empty"
  71. 71 }
  72. 72 let parts : Array[String] = []
  73. 73 for intent in intents {
  74. 74● parts.push(intent.to_string())
  75. 75 }
  76. 76● parts.join(" → ")
  77. 77}