| 1 | package codex |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "reflect" |
| 7 | "strings" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 10 | ) |
| 11 | |
| 12 | // The thin runner drives the pinned app-server itself (one long-lived |
| 13 | // coordinator process per person). These are the launcher's own checks, |
| 14 | // exported so both paths hold the same policy. |
| 15 | |
| 16 | // ValidateThinPolicy re-derives a launch's policy from its manifest and |
| 17 | // refuses anything the rendering would not produce, or a binary that is |
| 18 | // not the pinned one (digest and code-mode host digest; tests pass their |
| 19 | // fake's). |
| 20 | func ValidateThinPolicy(b provider.Binding, p provider.Policy, digest, hostDigest string) error { |
| 21 | l := &Launcher{digest: digest, hostDigest: hostDigest} |
| 22 | _, err := l.validatePolicy(b, p) |
| 23 | return err |
| 24 | } |
| 25 | |
| 26 | // InitializeParams is the client's initialize request. |
| 27 | func InitializeParams() json.RawMessage { |
| 28 | return marshal(map[string]any{"clientInfo": map[string]any{"name": "equipo", "version": "2"}, "capabilities": map[string]any{"experimentalApi": true}}) |
| 29 | } |
| 30 | |
| 31 | // CheckInitialize accepts only the pinned version answering with its home |
| 32 | // at /home/runner (the runtime copy, never the source home). |
| 33 | func CheckInitialize(raw json.RawMessage) error { |
| 34 | var v struct{ UserAgent, CodexHome string } |
| 35 | if json.Unmarshal(raw, &v) != nil || !strings.HasPrefix(v.UserAgent, "equipo/"+Version+" ") || v.CodexHome != "/home/runner" { |
| 36 | return provider.ErrVersion |
| 37 | } |
| 38 | return nil |
| 39 | } |
| 40 | |
| 41 | // CheckConfig validates config/read's effective configuration against |
| 42 | // the policy, as the launcher does before every thread. |
| 43 | func CheckConfig(raw json.RawMessage, p provider.Policy) error { return validateConfig(raw, p) } |
| 44 | |
| 45 | // ThreadParams are thread/start (id empty) or thread/resume parameters |
| 46 | // from the policy's thread policy. |
| 47 | func ThreadParams(p provider.Policy, id string) json.RawMessage { |
| 48 | method := "thread/start" |
| 49 | if id != "" { |
| 50 | method = "thread/resume" |
| 51 | } |
| 52 | params := threadParams(p.ThreadPolicy, method, id) |
| 53 | return marshal(params) |
| 54 | } |
| 55 | |
| 56 | // CheckThread accepts a thread/start or thread/resume answer only when |
| 57 | // the thread runs the policy's model, provider, cwd, approvals and |
| 58 | // permission profile; it returns the thread id. |
| 59 | func CheckThread(raw json.RawMessage, model, id string) (string, error) { |
| 60 | var v struct { |
| 61 | Thread struct{ ID string } |
| 62 | Model, ModelProvider, Cwd, ApprovalPolicy, ApprovalsReviewer string |
| 63 | ActivePermissionProfile *struct { |
| 64 | ID string |
| 65 | Extends json.RawMessage |
| 66 | } |
| 67 | RuntimeWorkspaceRoots []string |
| 68 | } |
| 69 | if json.Unmarshal(raw, &v) != nil || v.Thread.ID == "" || (id != "" && v.Thread.ID != id) || v.Model != model || v.ModelProvider != "openai" || v.Cwd != "/workspace" || v.ApprovalPolicy != "on-request" || v.ApprovalsReviewer != "user" || v.ActivePermissionProfile == nil || v.ActivePermissionProfile.ID != "equipo" || !bytes.Equal(v.ActivePermissionProfile.Extends, []byte("null")) || !reflect.DeepEqual(v.RuntimeWorkspaceRoots, []string{"/workspace"}) { |
| 70 | return "", provider.ErrPolicy |
| 71 | } |
| 72 | return v.Thread.ID, nil |
| 73 | } |
| 74 | |
| 75 | // TurnParams is turn/start for one user message on a thread. |
| 76 | func TurnParams(p provider.Policy, thread, text string) json.RawMessage { |
| 77 | var params map[string]json.RawMessage |
| 78 | _ = json.Unmarshal(p.TurnPolicy, ¶ms) |
| 79 | if params == nil { |
| 80 | params = map[string]json.RawMessage{} |
| 81 | } |
| 82 | params["threadId"] = marshal(thread) |
| 83 | params["input"] = marshal([]any{map[string]any{"type": "text", "text": text}}) |
| 84 | return marshal(params) |
| 85 | } |
| 86 | |
| 87 | // TurnParamsWithImages is turn/start with the text, then each image as a |
| 88 | // localImage input by its path inside the namespace (the pinned |
| 89 | // app-server's UserInput, from its generated JSON schema). |
| 90 | func TurnParamsWithImages(p provider.Policy, thread, text string, images []string) json.RawMessage { |
| 91 | if len(images) == 0 { |
| 92 | return TurnParams(p, thread, text) |
| 93 | } |
| 94 | var params map[string]json.RawMessage |
| 95 | _ = json.Unmarshal(p.TurnPolicy, ¶ms) |
| 96 | if params == nil { |
| 97 | params = map[string]json.RawMessage{} |
| 98 | } |
| 99 | input := []any{map[string]any{"type": "text", "text": text}} |
| 100 | for _, path := range images { |
| 101 | input = append(input, map[string]any{"type": "localImage", "path": path}) |
| 102 | } |
| 103 | params["threadId"] = marshal(thread) |
| 104 | params["input"] = marshal(input) |
| 105 | return marshal(params) |
| 106 | } |
| 107 | |
| 108 | // ToolApproval reads an mcpServer/elicitation/request for an MCP tool |
| 109 | // call: the server and the tool's arguments. ok is false for any other |
| 110 | // kind of elicitation (which is declined). |
| 111 | func ToolApproval(params json.RawMessage) (server string, args json.RawMessage, ok bool) { |
| 112 | var p struct { |
| 113 | ServerName, Mode string |
| 114 | Meta struct { |
| 115 | Kind string `json:"codex_approval_kind"` |
| 116 | Arguments json.RawMessage `json:"tool_params"` |
| 117 | } `json:"_meta"` |
| 118 | } |
| 119 | if json.Unmarshal(params, &p) != nil || p.Mode != "form" || p.Meta.Kind != "mcp_tool_call" { |
| 120 | return "", nil, false |
| 121 | } |
| 122 | return p.ServerName, p.Meta.Arguments, true |
| 123 | } |
| 124 | |
| 125 | // SameArguments compares two JSON tool argument documents. |
| 126 | func SameArguments(a, b json.RawMessage) bool { return sameJSON(a, b) } |
| 127 | |