Skip to content

Commit 54a0b74

Browse files
committed
feat: passdown agent type for safer formatting
1 parent b09f7ff commit 54a0b74

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ agentapi server -- goose
6565
```
6666

6767
> [!NOTE]
68-
> When using Codex, always specify the agent type explicitly (`agentapi server --type=codex -- codex`), or message formatting may break.
68+
> When using Codex, Gemini or CursorCLI, always specify the agent type explicitly (eg: `agentapi server --type=codex -- codex`), or message formatting may break.
6969
7070
An OpenAPI schema is available in [openapi.json](openapi.json).
7171

lib/msgfmt/msgfmt.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func skipTrailingInputBoxLine(lines []string, currentIdx int, markers ...string)
167167
// For instance, if there are any leading or trailing lines with only whitespace,
168168
// and each line of the input in msgRaw is preceded by a character like `>`,
169169
// these lines will not be removed.
170-
func RemoveUserInput(msgRaw string, userInputRaw string) string {
170+
func RemoveUserInput(msgRaw string, userInputRaw string, agentType AgentType) string {
171171
if userInputRaw == "" {
172172
return msgRaw
173173
}
@@ -188,13 +188,17 @@ func RemoveUserInput(msgRaw string, userInputRaw string) string {
188188
lastUserInputLineIdx := msgRuneLineLocations[userInputEndIdx]
189189

190190
// Skip Gemini trailing input box line
191-
if idx, found := skipTrailingInputBoxLine(msgLines, lastUserInputLineIdx, "╯", "╰"); found {
192-
lastUserInputLineIdx = idx
191+
if agentType == AgentTypeGemini {
192+
if idx, found := skipTrailingInputBoxLine(msgLines, lastUserInputLineIdx, "╯", "╰"); found {
193+
lastUserInputLineIdx = idx
194+
}
193195
}
194196

195197
// Skip Cursor trailing input box line
196-
if idx, found := skipTrailingInputBoxLine(msgLines, lastUserInputLineIdx, "┘", "└"); found {
197-
lastUserInputLineIdx = idx
198+
if agentType == AgentTypeCursor {
199+
if idx, found := skipTrailingInputBoxLine(msgLines, lastUserInputLineIdx, "┘", "└"); found {
200+
lastUserInputLineIdx = idx
201+
}
198202
}
199203

200204
return strings.Join(msgLines[lastUserInputLineIdx+1:], "\n")
@@ -234,15 +238,15 @@ const (
234238
AgentTypeCustom AgentType = "custom"
235239
)
236240

237-
func formatGenericMessage(message string, userInput string) string {
238-
message = RemoveUserInput(message, userInput)
241+
func formatGenericMessage(message string, userInput string, agentType AgentType) string {
242+
message = RemoveUserInput(message, userInput, agentType)
239243
message = removeMessageBox(message)
240244
message = trimEmptyLines(message)
241245
return message
242246
}
243247

244248
func formatCodexMessage(message string, userInput string) string {
245-
message = RemoveUserInput(message, userInput)
249+
message = RemoveUserInput(message, userInput, AgentTypeCodex)
246250
message = removeCodexInputBox(message)
247251
message = trimEmptyLines(message)
248252
return message
@@ -251,21 +255,21 @@ func formatCodexMessage(message string, userInput string) string {
251255
func FormatAgentMessage(agentType AgentType, message string, userInput string) string {
252256
switch agentType {
253257
case AgentTypeClaude:
254-
return formatGenericMessage(message, userInput)
258+
return formatGenericMessage(message, userInput, agentType)
255259
case AgentTypeGoose:
256-
return formatGenericMessage(message, userInput)
260+
return formatGenericMessage(message, userInput, agentType)
257261
case AgentTypeAider:
258-
return formatGenericMessage(message, userInput)
262+
return formatGenericMessage(message, userInput, agentType)
259263
case AgentTypeCodex:
260264
return formatCodexMessage(message, userInput)
261265
case AgentTypeGemini:
262-
return formatGenericMessage(message, userInput)
266+
return formatGenericMessage(message, userInput, agentType)
263267
case AgentTypeAmp:
264-
return formatGenericMessage(message, userInput)
268+
return formatGenericMessage(message, userInput, agentType)
265269
case AgentTypeCursor:
266-
return formatGenericMessage(message, userInput)
270+
return formatGenericMessage(message, userInput, agentType)
267271
case AgentTypeCustom:
268-
return formatGenericMessage(message, userInput)
272+
return formatGenericMessage(message, userInput, agentType)
269273
default:
270274
return message
271275
}

0 commit comments

Comments
 (0)