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