| 1 | package planflow |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "unicode/utf8" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 8 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 9 | ) |
| 10 | |
| 11 | var ErrPromptTooLarge = runnerproto.ErrPromptTooLarge |
| 12 | |
| 13 | const MaxPromptBytes = 24 << 10 |
| 14 | |
| 15 | // LegacyStep preserves only the original fixed bug protocol. |
| 16 | func LegacyStep(key string) runnerproto.StepBrief { |
| 17 | action := key |
| 18 | if key == "verify" { |
| 19 | action = "checks" |
| 20 | } |
| 21 | specialist := "engineering" |
| 22 | if key == "review" || key == "verify" { |
| 23 | specialist = "review" |
| 24 | } |
| 25 | return runnerproto.StepBrief{PlanRevision: 1, StepKey: key, Action: action, SpecialistKey: specialist, SpecialistRevision: 1} |
| 26 | } |
| 27 | |
| 28 | type promptFile struct { |
| 29 | Path string `json:"path"` |
| 30 | Text string `json:"text"` |
| 31 | } |
| 32 | type turnPrompt struct { |
| 33 | RenderAvailable bool `json:"render_available,omitempty"` |
| 34 | Action string |
| 35 | Sources []runnerproto.SourceSnapshot `json:"sources,omitempty"` |
| 36 | Step string |
| 37 | Instructions string `json:"instructions"` |
| 38 | StandingInstructions string `json:"standing_instructions"` |
| 39 | Target team.TargetBinding `json:"target"` |
| 40 | ExpectedSymptom string `json:"expected_symptom,omitempty"` |
| 41 | Files []promptFile `json:"reproduction_files,omitempty"` |
| 42 | Tests []team.TestIdentity `json:"reproduction_tests,omitempty"` |
| 43 | CandidateCommit string `json:"candidate_commit,omitempty"` |
| 44 | } |
| 45 | |
| 46 | func Prompt(assignment runnerproto.Assignment, brief runnerproto.RunnerBrief, artifacts []team.Artifact) (string, error) { |
| 47 | p := turnPrompt{RenderAvailable: brief.RenderRecipe != nil, Action: brief.Step.Action, Sources: brief.Sources, Step: assignment.StepKey, Instructions: brief.Instructions, StandingInstructions: brief.StandingInstructions, Target: brief.Target} |
| 48 | if brief.Target.Kind == team.TargetProjectRead && p.Action == "scope" { |
| 49 | p.Action = "recommend" |
| 50 | } |
| 51 | for _, a := range artifacts { |
| 52 | switch a.Role { |
| 53 | case team.RoleReproduction, team.RoleAcceptance: |
| 54 | var r runnerproto.Reproduction |
| 55 | if err := json.Unmarshal(a.Payload, &r); err != nil { |
| 56 | return "", err |
| 57 | } |
| 58 | p.ExpectedSymptom, p.Tests = r.ExpectedSymptom, r.TestNames |
| 59 | for _, f := range r.Files { |
| 60 | if !utf8.Valid(f.Bytes) { |
| 61 | return "", runnerproto.ErrInvalid |
| 62 | } |
| 63 | p.Files = append(p.Files, promptFile{f.Path, string(f.Bytes)}) |
| 64 | } |
| 65 | case team.RoleCandidate: |
| 66 | var candidate team.GitCandidate |
| 67 | if err := json.Unmarshal(a.Payload, &candidate); err != nil { |
| 68 | return "", err |
| 69 | } |
| 70 | p.CandidateCommit = candidate.Commit |
| 71 | } |
| 72 | } |
| 73 | raw, err := json.Marshal(p) |
| 74 | if err != nil { |
| 75 | return "", err |
| 76 | } |
| 77 | if len(raw) > MaxPromptBytes { |
| 78 | return "", ErrPromptTooLarge |
| 79 | } |
| 80 | return string(raw), nil |
| 81 | } |
| 82 | |