image.domain\agent_kit\agent_compiler.mbt

66.7% · 28 / 42 points · 53 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 Compiler
  3. 3///
  4. 4/// 意图 → 调用链编译
  5. 5/// 描述 Intent → call chain compilation
  6. 6// Agent-Kit 编译器 — 意图序列 → ImgIR 管道
  7. 7// compile: 将意图序列编译为 ImgIR IRPipeline
  8. 8
  9. 9///|
  10. 10pub fn compile(intents : Array[Intent]) -> @imgir.IRPipeline {
  11. 11● let mut pipeline = @imgir.IRPipeline::new()
  12. 12 for intent in intents {
  13. 13● pipeline = apply_intent(pipeline, intent)
  14. 14 }
  15. 15 pipeline
  16. 16}
  17. 17
  18. 18///|
  19. 19fn apply_intent(
  20. 20 pipeline : @imgir.IRPipeline,
  21. 21 intent : Intent,
  22. 22) -> @imgir.IRPipeline {
  23. 23● match intent {
  24. 24● Denoise => pipeline.gaussian_blur(2, 1.0F)
  25. 25● EdgeDetect => pipeline.to_scalar_field().field_gradient()
  26. 26○ Blur => pipeline.box_blur(2)
  27. 27● Sharpen => pipeline.sharpen(0.5F)
  28. 28● Grayscale => pipeline.to_gray()
  29. 29○ Resize(w, h) => pipeline.resize(w, h)
  30. 30● Rotate => pipeline.rotate_90()
  31. 31● FlipH => pipeline.flip_h()
  32. 32○ FlipV => pipeline.flip_v()
  33. 33 Threshold(lo, hi) =>
  34. 34○ pipeline.to_scalar_field().field_clamp(lo, hi).scalar_field_to_image()
  35. 35○ Mask(t) => pipeline.to_scalar_field().field_mask(t).scalar_field_to_image()
  36. 36● Identity => pipeline
  37. 37 }
  38. 38}
  39. 39
  40. 40///|
  41. 41pub fn nl_to_pipeline(query : String) -> @imgir.IRPipeline {
  42. 42● let intents = nl_parse(query)
  43. 43● compile(intents)
  44. 44}
  45. 45
  46. 46///|
  47. 47pub fn nl_to_description(query : String) -> String {
  48. 48● let intents = nl_parse(query)
  49. 49● let intent_desc = nl_describe(intents)
  50. 50● let pipeline = compile(intents)
  51. 51● let pipeline_desc = @imgir.ir_describe(pipeline)
  52. 52 "intents: \{intent_desc} | pipeline: \{pipeline_desc}"
  53. 53}