-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodex.go
More file actions
181 lines (167 loc) · 4.73 KB
/
Copy pathcodex.go
File metadata and controls
181 lines (167 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"encoding/json"
"fmt"
"regexp"
"strings"
"time"
)
func codexHookArgs(id, token string) []string {
// Hook callbacks carry the token as a query param, same as the UI.
q := ""
if token != "" {
q = "?t=" + token
}
hook := func(path string, timeoutSec int) string {
cmd := fmt.Sprintf(`curl -sS --max-time %d -X POST http://127.0.0.1:$PULSE_PORT/hooks/%s/%s%s -H "Content-Type: application/json" --data-binary @-`, timeoutSec, id, path, q)
q, _ := json.Marshal(cmd)
return fmt.Sprintf(`[{hooks=[{type="command",command=%s,timeout=%d}]}]`, q, timeoutSec)
}
return []string{
"-c", "hooks.SessionStart=" + hook("session-start", 30),
"-c", "hooks.PermissionRequest=" + hook("permission", 900),
"-c", "hooks.Stop=" + hook("stop", 30),
"--dangerously-bypass-hook-trust",
}
}
func parseCodexLine(lineNo int, raw []byte) parsed {
var l struct {
Type string `json:"type"`
Payload json.RawMessage `json:"payload"`
}
if json.Unmarshal(raw, &l) != nil {
return parsed{}
}
if l.Type == "turn_context" {
var tc struct {
Model string `json:"model"`
}
json.Unmarshal(l.Payload, &tc)
return parsed{model: tc.Model}
}
if l.Type != "response_item" {
return parsed{}
}
var p struct {
Type string `json:"type"`
Role string `json:"role"`
Content []struct {
Text string `json:"text"`
} `json:"content"`
Name string `json:"name"`
Arguments string `json:"arguments"`
CallID string `json:"call_id"`
Output string `json:"output"`
Summary []struct {
Text string `json:"text"`
} `json:"summary"`
}
if json.Unmarshal(l.Payload, &p) != nil {
return parsed{}
}
line := lineNo * 10
switch p.Type {
case "message":
if p.Role != "user" && p.Role != "assistant" {
return parsed{}
}
var text strings.Builder
for _, b := range p.Content {
text.WriteString(b.Text)
}
s := truncate(stripANSI(text.String()), maxText)
if s == "" || strings.HasPrefix(s, "<environment_context>") {
return parsed{}
}
return parsed{msgs: []Message{{Line: line, Role: p.Role, Kind: "text", Text: s}}}
case "reasoning":
var text strings.Builder
for _, s := range p.Summary {
text.WriteString(s.Text)
}
if text.Len() == 0 {
return parsed{}
}
return parsed{msgs: []Message{{Line: line, Role: "assistant", Kind: "thinking", Text: truncate(text.String(), maxText)}}}
case "function_call":
return parsed{msgs: []Message{{Line: line, Role: "assistant", Kind: "tool_use", Name: p.Name, Text: truncate(prettyJSON(json.RawMessage(p.Arguments)), maxText)}}}
case "function_call_output":
out := truncate(stripANSI(p.Output), maxText)
if out == "" {
return parsed{}
}
return parsed{msgs: []Message{{Line: line, Role: "assistant", Kind: "tool_result", Text: out, resultFor: p.CallID}}}
}
return parsed{}
}
func codexStatusLine(pane string) string {
lines := strings.Split(strings.TrimRight(pane, "\n"), "\n")
for i := len(lines) - 1; i >= 0 && i > len(lines)-6; i-- {
if strings.TrimSpace(lines[i]) != "" {
return lines[i]
}
}
return ""
}
var codexPlanModeRE = regexp.MustCompile(`Plan mode\s*$`)
func parseCodexMode(pane string) string {
if codexPlanModeRE.MatchString(codexStatusLine(pane)) {
return "plan"
}
return "default"
}
var codexModelEffortRE = regexp.MustCompile(`^\s*(\S+)\s+(low|medium|high|xhigh)\s*·`)
func parseCodexModelEffort(pane string) (model, effort string) {
m := codexModelEffortRE.FindStringSubmatch(codexStatusLine(pane))
if m == nil {
return "", ""
}
return m[1], m[2]
}
func codexSetMode(session, target string) {
if parseCodexMode(tmuxCapture(session)) != target {
tmuxSendKey(session, "BTab")
}
}
var codexMenuItemRE = regexp.MustCompile(`(?m)^.\s*(\d+)\.\s+(\S+)`)
func codexMenuIndex(pane, needle string) string {
needle = strings.ToLower(needle)
for _, m := range codexMenuItemRE.FindAllStringSubmatch(pane, -1) {
if strings.Contains(strings.ToLower(m[2]), needle) {
return m[1]
}
}
return ""
}
func codexSetModel(session, modelID string) {
tmuxSendText(session, "/model")
time.Sleep(700 * time.Millisecond)
idx := codexMenuIndex(tmuxCapture(session), modelID)
if idx == "" {
tmuxSendKey(session, "Escape")
return
}
tmuxSendKey(session, idx)
time.Sleep(500 * time.Millisecond)
tmuxSendKey(session, "Enter")
}
func codexEffortNeedle(level string) string {
if level == "xhigh" {
return "Extra"
}
return level
}
func codexSetEffort(session, level string) {
tmuxSendText(session, "/model")
time.Sleep(700 * time.Millisecond)
tmuxSendKey(session, "Enter")
time.Sleep(500 * time.Millisecond)
idx := codexMenuIndex(tmuxCapture(session), codexEffortNeedle(level))
if idx == "" {
tmuxSendKey(session, "Escape")
return
}
tmuxSendKey(session, idx)
time.Sleep(300 * time.Millisecond)
tmuxSendKey(session, "Enter")
}