oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package provider
2
3import (
4 "bytes"
5 "encoding/json"
6)
7
8// NativeRequest retains the exact request and correlated file item. It carries
9// evidence, never a policy update or a replacement command.
10type NativeRequest struct {
11 AdditionalPermissions json.RawMessage `json:"additional_permissions,omitempty"`
12 NetworkApprovalContext json.RawMessage `json:"network_approval_context,omitempty"`
13 ApprovalID string `json:"approval_id,omitempty"`
14 RequestID string `json:"request_id"`
15 ItemID string `json:"item_id"`
16 TurnID string `json:"turn_id"`
17 SessionID string `json:"session_id"`
18 Request json.RawMessage `json:"request"`
19 ToolCall json.RawMessage `json:"tool_call,omitempty"`
20}
21
22func NativeApproval(ev Event) (NativeRequest, error) {
23 n := NativeRequest{RequestID: ev.RequestID, Request: bytes.Clone(ev.Payload)}
24 v, err := parseNative(ev.Payload)
25 if err != nil || string(v.ID) != ev.RequestID {
26 return n, ErrPolicy
27 }
28 if v.Method == "execCommandApproval" || v.Method == "applyPatchApproval" {
29 return legacyNativeApproval(ev, v)
30 }
31 var p struct {
32 ThreadID, TurnID, ItemID, Command, Cwd, Kind, GrantRoot, ApprovalID string
33 AdditionalPermissions, NetworkApprovalContext json.RawMessage
34 }
35 if json.Unmarshal(v.Params, &p) != nil || p.ThreadID == "" || p.ThreadID != ev.SessionID || p.GrantRoot != "" || p.TurnID == "" || (ev.ProviderTurnID != "" && ev.ProviderTurnID != p.TurnID) || p.ItemID == "" {
36 return n, ErrPolicy
37 }
38 n.ItemID, n.TurnID, n.SessionID = p.ItemID, p.TurnID, p.ThreadID
39 n.AdditionalPermissions, n.NetworkApprovalContext, n.ApprovalID = bytes.Clone(p.AdditionalPermissions), bytes.Clone(p.NetworkApprovalContext), p.ApprovalID
40 if len(p.NetworkApprovalContext) > 0 && string(p.NetworkApprovalContext) != "null" {
41 return n, ErrPolicy
42 }
43 var extra struct{ Network json.RawMessage }
44 if len(p.AdditionalPermissions) > 0 && json.Unmarshal(p.AdditionalPermissions, &extra) != nil {
45 return n, ErrPolicy
46 }
47 if len(extra.Network) > 0 && string(extra.Network) != "null" {
48 return n, ErrPolicy
49 }
50 switch v.Method {
51 case "item/commandExecution/requestApproval":
52 if p.Command == "" || p.Cwd == "" || (p.Kind != "" && p.Kind != "command") {
53 return n, ErrPolicy
54 }
55 case "item/fileChange/requestApproval":
56 call, err := parseNative(ev.ToolCall)
57 if err != nil || call.Method != "item/started" {
58 return n, ErrPolicy
59 }
60 var c struct {
61 ThreadID, TurnID string
62 Item struct {
63 ID, Type, Status string
64 Changes []struct {
65 Path string
66 Diff *string
67 Kind struct {
68 Type string
69 MovePath *string `json:"move_path"`
70 }
71 }
72 }
73 }
74 if json.Unmarshal(call.Params, &c) != nil || c.ThreadID != p.ThreadID || c.TurnID != p.TurnID || c.Item.ID != p.ItemID || c.Item.Type != "fileChange" || c.Item.Status != "inProgress" || len(c.Item.Changes) == 0 {
75 return n, ErrPolicy
76 }
77 for _, change := range c.Item.Changes {
78 if change.Path == "" || change.Diff == nil {
79 return n, ErrPolicy
80 }
81 switch change.Kind.Type {
82 case "add", "delete", "update":
83 default:
84 return n, ErrPolicy
85 }
86 }
87 n.ToolCall = bytes.Clone(ev.ToolCall)
88 default:
89 return n, ErrPolicy
90 }
91 return n, nil
92}
93
94func legacyNativeApproval(ev Event, v nativeEnvelope) (NativeRequest, error) {
95 n := NativeRequest{RequestID: ev.RequestID, Request: bytes.Clone(ev.Payload), TurnID: ev.ProviderTurnID}
96 var p struct {
97 ConversationID, CallID, Cwd, GrantRoot string
98 Command []string
99 FileChanges map[string]struct {
100 Type string
101 Content, UnifiedDiff *string
102 MovePath *string `json:"move_path"`
103 }
104 }
105 // Legacy update diffs use snake case.
106 var changes struct{ FileChanges map[string]json.RawMessage }
107 if json.Unmarshal(v.Params, &p) != nil || p.ConversationID != ev.SessionID || p.ConversationID == "" || p.CallID == "" || p.GrantRoot != "" || ev.ProviderTurnID == "" {
108 return n, ErrPolicy
109 }
110 n.SessionID, n.ItemID = p.ConversationID, p.CallID
111 if v.Method == "execCommandApproval" {
112 if len(p.Command) == 0 || p.Command[0] == "" || p.Cwd == "" {
113 return n, ErrPolicy
114 }
115 } else {
116 if len(p.FileChanges) == 0 || json.Unmarshal(v.Params, &changes) != nil {
117 return n, ErrPolicy
118 }
119 for path, change := range p.FileChanges {
120 if path == "" {
121 return n, ErrPolicy
122 }
123 switch change.Type {
124 case "add", "delete":
125 if change.Content == nil {
126 return n, ErrPolicy
127 }
128 case "update":
129 var c struct {
130 Diff *string `json:"unified_diff"`
131 }
132 if json.Unmarshal(changes.FileChanges[path], &c) != nil || c.Diff == nil {
133 return n, ErrPolicy
134 }
135 default:
136 return n, ErrPolicy
137 }
138 }
139 }
140 return n, nil
141}
142