Skip to content

Commit 33124c2

Browse files
35C4n0rCopilothugodutka
authored
feat: update support for codex (#41)
Co-authored-by: Copilot <[email protected]> Co-authored-by: Hugo Dutka <[email protected]>
1 parent 75269dd commit 33124c2

File tree

23 files changed

+192
-422
lines changed

23 files changed

+192
-422
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ You can use AgentAPI:
5454

5555
Run an HTTP server that lets you control an agent. If you'd like to start an agent with additional arguments, pass the full agent command after the `--` flag.
5656

57+
> [!NOTE]
58+
> When using Codex, always specify the agent type explicitly (`agentapi server --type=codex -- codex`), or message formatting may break.
59+
5760
```bash
5861
agentapi server -- claude --allowedTools "Bash(git*) Edit Replace"
5962
```

cmd/server/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ func runServer(ctx context.Context, logger *slog.Logger, argsToPass []string) er
7878
}
7979
if termHeight < 10 {
8080
return xerrors.Errorf("term height must be at least 10")
81+
} else if agentType == AgentTypeCodex && termHeight > 930 {
82+
logger.Warn(fmt.Sprintf("Term height is set to %d which may cause issues with Codex. Setting it to 930 instead.", termHeight))
83+
termHeight = 930 // codex has a bug where the TUI distorts the screen if the height is too large, see: https://github.com/openai/codex/issues/1608
8184
}
8285

8386
var process *termexec.Process

lib/msgfmt/message_box.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,41 @@ func removeMessageBox(msg string) string {
5151

5252
return strings.Join(lines, "\n")
5353
}
54+
55+
func removeCodexMessageBox(msg string) string {
56+
lines := strings.Split(msg, "\n")
57+
messageBoxEndIdx := -1
58+
messageBoxStartIdx := -1
59+
60+
for i := len(lines) - 1; i >= 0; i-- {
61+
if messageBoxEndIdx == -1 {
62+
if strings.Contains(lines[i], "╰────────") && strings.Contains(lines[i], "───────╯") {
63+
messageBoxEndIdx = i
64+
}
65+
} else {
66+
// We reached the start of the message box (we don't want to show this line), also exit the loop
67+
if strings.Contains(lines[i], "╭") && strings.Contains(lines[i], "───────╮") {
68+
// We only want this to be i in case the top of the box is visible
69+
messageBoxStartIdx = i
70+
break
71+
}
72+
73+
// We are in between the start and end of the message box, so remove the │ from the start and end of the line, let the trimEmptyLines handle the rest
74+
if strings.HasPrefix(lines[i], "│") {
75+
lines[i] = strings.TrimPrefix(lines[i], "│")
76+
}
77+
if strings.HasSuffix(lines[i], "│") {
78+
lines[i] = strings.TrimSuffix(lines[i], "│")
79+
lines[i] = strings.TrimRight(lines[i], " \t")
80+
}
81+
}
82+
}
83+
84+
// If we didn't find messageBoxEndIdx, set it to the end of the lines
85+
if messageBoxEndIdx == -1 {
86+
messageBoxEndIdx = len(lines)
87+
}
88+
89+
return strings.Join(lines[messageBoxStartIdx+1:messageBoxEndIdx], "\n")
90+
91+
}

lib/msgfmt/msgfmt.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ func formatGenericMessage(message string, userInput string) string {
216216
return message
217217
}
218218

219+
func formatCodexMessage(message string, userInput string) string {
220+
message = RemoveUserInput(message, userInput)
221+
message = removeMessageBox(message)
222+
message = removeCodexMessageBox(message)
223+
message = trimEmptyLines(message)
224+
return message
225+
}
226+
219227
func FormatAgentMessage(agentType AgentType, message string, userInput string) string {
220228
switch agentType {
221229
case AgentTypeClaude:
@@ -225,7 +233,7 @@ func FormatAgentMessage(agentType AgentType, message string, userInput string) s
225233
case AgentTypeAider:
226234
return formatGenericMessage(message, userInput)
227235
case AgentTypeCodex:
228-
return formatGenericMessage(message, userInput)
236+
return formatCodexMessage(message, userInput)
229237
case AgentTypeGemini:
230238
return formatGenericMessage(message, userInput)
231239
case AgentTypeCustom:
Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
thinking for 9s
1+
Shell Command
2+
~/Documents/work/agentapi$ git rev-parse --show-toplevel
23

3-
command
4+
Allow command?
45

5-
$ git rev-parse --show-toplevel
6-
7-
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────╮
8-
│Shell Command │
9-
│ │
10-
│$ git rev-parse --show-toplevel │
11-
│ │
12-
│Allow command? │
13-
│ │
14-
│ ❯ Yes (y) │
15-
│ Yes, always approve this exact command for this session (a) │
16-
│ Edit or give feedback (e) │
17-
│ No, and keep going (n) │
18-
│ No, and stop for now (esc) │
19-
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────╯
6+
▶ Yes (y)
7+
Yes, always approve this exact command for this session (a)
8+
Edit or give feedback (e)
9+
No, and keep going (n)
10+
No, and stop for now (esc)
Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
user
2-
what repository are you in?
3-
4-
thinking for 9s
5-
6-
command
7-
8-
$ git rev-parse --show-toplevel
9-
10-
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────╮
11-
│Shell Command │
12-
│ │
13-
│$ git rev-parse --show-toplevel │
14-
│ │
15-
│Allow command? │
16-
│ │
17-
│ ❯ Yes (y) │
18-
│ Yes, always approve this exact command for this session (a) │
19-
│ Edit or give feedback (e) │
20-
│ No, and keep going (n) │
21-
│ No, and stop for now (esc) │
22-
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────╯
1+
╭Messages (tab to focus)──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
2+
│OpenAI Codex v0.10.0 (research preview) │
3+
│ │
4+
│codex session 790ba389-ab4d-47be-bb16-9cc7f36bcf2b │
5+
│workdir: /Users/jkmr/Documents/work/agentapi │
6+
│model: codex-mini-latest │
7+
│provider: openai │
8+
│approval: untrusted │
9+
│sandbox: read-only │
10+
│reasoning effort: medium │
11+
│reasoning summaries: auto │
12+
│ │
13+
│user │
14+
│what repository are you in? │
15+
│ │
16+
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
17+
╭Review───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
18+
│Shell Command │
19+
│~/Documents/work/agentapi$ git rev-parse --show-toplevel │
20+
│ │
21+
│Allow command? │
22+
│ │
23+
│ ▶ Yes (y) │
24+
│ Yes, always approve this exact command for this session (a) │
25+
│ Edit or give feedback (e) │
26+
│ No, and keep going (n) │
27+
│ No, and stop for now (esc) │
28+
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
╭──────────────────────────────────────────────────────────────╮
2-
│ ● OpenAI Codex (research preview) v0.1.2504161551 │
3-
╰──────────────────────────────────────────────────────────────╯
4-
╭──────────────────────────────────────────────────────────────╮
5-
│ localhost session: c4bf4c89c3fb483c935bdff223394646 │
6-
│ ↳ workdir: ~/dev/agentapi │
7-
│ ↳ model: o4-mini │
8-
│ ↳ approval: suggest │
9-
╰──────────────────────────────────────────────────────────────╯
1+
OpenAI Codex v0.10.0 (research preview)
102

11-
system
12-
Warning: model "o4-mini" is not in the list of available models returned by OpenAI.
3+
codex session 56576d81-529d-42f3-843b-78d870054a75
4+
workdir: /Users/jkmr/Documents/work/agentapi
5+
model: codex-mini-latest
6+
provider: openai
7+
approval: untrusted
8+
sandbox: read-only
9+
reasoning effort: medium
10+
reasoning summaries: auto

0 commit comments

Comments
 (0)