| 1 | package runnerproto |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "unicode/utf8" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 9 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 10 | ) |
| 11 | |
| 12 | var ErrPromptTooLarge = errors.New("runner.prompt_too_large") |
| 13 | |
| 14 | const maxTurnPromptBytes = 24 << 10 |
| 15 | |
| 16 | type promptFile struct { |
| 17 | Path string `json:"path"` |
| 18 | Text string `json:"text"` |
| 19 | } |
| 20 | type turnPrompt struct { |
| 21 | Step string |
| 22 | Instructions string `json:"instructions"` |
| 23 | StandingInstructions string `json:"standing_instructions"` |
| 24 | Target team.TargetBinding `json:"target"` |
| 25 | ExpectedSymptom string `json:"expected_symptom,omitempty"` |
| 26 | Files []promptFile `json:"reproduction_files,omitempty"` |
| 27 | Tests []team.TestIdentity `json:"reproduction_tests,omitempty"` |
| 28 | CandidateCommit string `json:"candidate_commit,omitempty"` |
| 29 | } |
| 30 | |
| 31 | func BuildTurnPrompt(step string, brief RunnerBrief, artifacts []team.Artifact) (string, error) { |
| 32 | p := turnPrompt{Step: step, Instructions: brief.Instructions, StandingInstructions: brief.StandingInstructions, Target: brief.Target} |
| 33 | for _, a := range artifacts { |
| 34 | switch a.Role { |
| 35 | case team.RoleReproduction: |
| 36 | var r Reproduction |
| 37 | if err := json.Unmarshal(a.Payload, &r); err != nil { |
| 38 | return "", err |
| 39 | } |
| 40 | p.ExpectedSymptom, p.Tests = r.ExpectedSymptom, r.TestNames |
| 41 | for _, f := range r.Files { |
| 42 | if !utf8.Valid(f.Bytes) { |
| 43 | return "", ErrInvalid |
| 44 | } |
| 45 | p.Files = append(p.Files, promptFile{f.Path, string(f.Bytes)}) |
| 46 | } |
| 47 | case team.RoleCandidate: |
| 48 | var candidate team.GitCandidate |
| 49 | if err := json.Unmarshal(a.Payload, &candidate); err != nil { |
| 50 | return "", err |
| 51 | } |
| 52 | p.CandidateCommit = candidate.Commit |
| 53 | } |
| 54 | } |
| 55 | raw, err := json.Marshal(p) |
| 56 | if err != nil { |
| 57 | return "", err |
| 58 | } |
| 59 | if len(raw) > maxTurnPromptBytes { |
| 60 | return "", ErrPromptTooLarge |
| 61 | } |
| 62 | return string(raw), nil |
| 63 | } |
| 64 | func TurnIntentPayload(providerName, model, prompt string) ([]byte, error) { |
| 65 | quoted, err := json.Marshal(prompt) |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | // Leave room for the agent identity, request ID and bridge frame envelope. |
| 70 | if len(prompt) > maxTurnPromptBytes || len(quoted)+8192 >= provider.MaxFrameBytes { |
| 71 | return nil, ErrPromptTooLarge |
| 72 | } |
| 73 | return promptIntent(providerName, model, prompt) |
| 74 | } |
| 75 | func promptIntent(providerName, model, prompt string) ([]byte, error) { |
| 76 | return json.Marshal(struct { |
| 77 | Provider string `json:"provider"` |
| 78 | Model string `json:"model"` |
| 79 | Hash string `json:"prompt_sha256"` |
| 80 | Bytes int `json:"prompt_bytes"` |
| 81 | }{providerName, model, team.ContentDigest([]byte(prompt)), len(prompt)}) |
| 82 | } |
| 83 | |