feat(codex): retain all tool items and expose bounded command output

This commit is contained in:
2026-10-20 18:07:00 +01:00
parent 9cdfd9d9ab
commit 83fef10331
6 changed files with 529 additions and 76 deletions
+76 -76
View File
@@ -54,6 +54,8 @@ type Chat struct {
subs map[string]map[chan protocol.ChatEventFrame]struct{}
forms map[string]pendingForm
approvals map[string]pendingApproval
commands map[string]map[string]commandSnapshot
commandOrder uint64
turns map[string]string
emitted map[string]bool
closed bool
@@ -347,12 +349,6 @@ func (c *Chat) PromptWithMessageIDModel(ctx context.Context, id, text, messageID
return err
}
// Shells is empty: Codex background terminals are not exposed through the chat
// surface yet.
func (c *Chat) Shells(context.Context, string) ([]protocol.ChatShell, error) {
return nil, nil
}
// ActivateSkill is an OpenCode-only chat concept.
func (c *Chat) ActivateSkill(context.Context, string, string) error {
return errors.New("Codex chat has no out-of-band skill activation")
@@ -451,27 +447,45 @@ func (c *Chat) Sessions(ctx context.Context) ([]protocol.ChatSession, error) {
// Messages reads a thread's turns and flattens them into chat messages.
func (c *Chat) Messages(ctx context.Context, id string, limit int, cursor string) (protocol.ChatHistoryPage, error) {
_ = limit
_ = cursor
raw, err := c.call(ctx, "thread/read", map[string]any{"threadId": id, "includeTurns": true})
if limit <= 0 {
limit = 50
}
if limit > 100 {
limit = 100
}
params := map[string]any{"threadId": id, "limit": limit, "sortDirection": "desc", "itemsView": "full"}
if cursor != "" {
params["cursor"] = cursor
}
raw, err := c.call(ctx, "thread/turns/list", params)
if err != nil {
return protocol.ChatHistoryPage{}, err
}
var result struct {
Thread thread `json:"thread"`
Data []turn `json:"data"`
NextCursor *string `json:"nextCursor"`
}
if err := json.Unmarshal(raw, &result); err != nil {
return protocol.ChatHistoryPage{}, err
}
if result.Data == nil {
return protocol.ChatHistoryPage{}, errors.New("invalid Codex history page")
}
messages := []protocol.ChatMessage{}
for _, turn := range result.Thread.Turns {
for index := len(result.Data) - 1; index >= 0; index-- {
turn := result.Data[index]
for _, item := range turn.Items {
if msg, ok := item.chatMessage(id); ok {
messages = append(messages, msg)
}
}
}
return protocol.ChatHistoryPage{Messages: messages}, nil
page := protocol.ChatHistoryPage{Messages: messages}
if result.NextCursor != nil {
page.NextCursor = *result.NextCursor
page.HasMore = page.NextCursor != ""
}
return page, nil
}
// Answer replies to a pending requestUserInput request.
@@ -657,71 +671,38 @@ func (c *Chat) handleNotification(msg rpcMessage) {
c.markEmitted(params.ItemID)
c.emit(textFrame(params.ThreadID, params.ItemID, protocol.ChatEventReasoning, params.Delta))
}
case "item/commandExecution/outputDelta":
c.commandDelta(params.ThreadID, params.ItemID, params.Delta)
}
}
func (c *Chat) handleItem(threadID, itemID string, raw json.RawMessage, completed bool) {
var item struct {
ID string `json:"id"`
Type string `json:"type"`
Text string `json:"text"`
Status string `json:"status"`
Command string `json:"command"`
Query string `json:"query"`
Name string `json:"name"`
Input any `json:"input"`
Error string `json:"error"`
}
if err := json.Unmarshal(raw, &item); err != nil {
var record item
if err := json.Unmarshal(raw, &record); err != nil {
return
}
if item.ID == "" {
item.ID = itemID
if record.ID == "" {
record.ID = itemID
}
switch item.Type {
switch record.Type {
case "agentMessage":
if completed && item.Text != "" && !c.wasEmitted(item.ID) {
c.emit(textFrame(threadID, item.ID, protocol.ChatEventText, item.Text))
if completed && record.Text != "" && !c.wasEmitted(record.ID) {
c.emit(textFrame(threadID, record.ID, protocol.ChatEventText, record.Text))
}
case "reasoning":
if completed && item.Text != "" && !c.wasEmitted(item.ID) {
c.emit(textFrame(threadID, item.ID, protocol.ChatEventReasoning, item.Text))
text := record.reasoningText()
if completed && text != "" && !c.wasEmitted(record.ID) {
c.emit(textFrame(threadID, record.ID, protocol.ChatEventReasoning, text))
}
case "commandExecution", "fileChange", "mcpToolCall", "webSearch":
status := protocol.ChatToolRunning
if completed {
if strings.EqualFold(item.Status, "failed") || item.Error != "" {
status = protocol.ChatToolError
} else {
status = protocol.ChatToolCompleted
}
}
name := item.Type
var input any
switch item.Type {
case "commandExecution":
name = "shell"
input = map[string]any{"command": item.Command}
case "webSearch":
name = "websearch"
input = map[string]any{"query": item.Query}
default:
if item.Name != "" {
name = item.Name
}
input = item.Input
}
encoded, _ := json.Marshal(input)
state := &protocol.ChatToolState{Status: status, Input: encoded}
if item.Text != "" {
state.Content = []protocol.ChatToolContent{{Type: "text", Text: item.Text}}
}
if item.Error != "" {
state.Error = &protocol.ChatError{Message: item.Error}
case "userMessage":
return
default:
if record.Type == "commandExecution" {
c.recordCommand(threadID, record, true)
}
part := record.toolPart(completed)
c.emit(protocol.ChatEventFrame{
SessionID: threadID, MessageID: item.ID, Kind: protocol.ChatEventTool,
Part: &protocol.ChatPart{Type: protocol.ChatPartTool, ID: item.ID, Name: name, State: state},
SessionID: threadID, MessageID: record.ID, Kind: protocol.ChatEventTool, Part: &part,
})
}
}
@@ -769,17 +750,30 @@ type turn struct {
}
type item struct {
ID string `json:"id"`
ClientID string `json:"clientId"`
Type string `json:"type"`
Text string `json:"text"`
Content json.RawMessage `json:"content"`
Status string `json:"status"`
Command string `json:"command"`
Query string `json:"query"`
Name string `json:"name"`
Input any `json:"input"`
Error string `json:"error"`
Raw json.RawMessage `json:"-"`
ID string `json:"id"`
ClientID string `json:"clientId"`
Type string `json:"type"`
Text string `json:"text"`
Content json.RawMessage `json:"content"`
Status string `json:"status"`
Command string `json:"command"`
Query string `json:"query"`
Name string `json:"name"`
Input any `json:"input"`
Error json.RawMessage `json:"error"`
Arguments json.RawMessage `json:"arguments"`
Result json.RawMessage `json:"result"`
Output json.RawMessage `json:"output"`
ContentItems json.RawMessage `json:"contentItems"`
Changes json.RawMessage `json:"changes"`
Tool string `json:"tool"`
Namespace string `json:"namespace"`
Server string `json:"server"`
AggregatedOutput string `json:"aggregatedOutput"`
ExitCode *int `json:"exitCode"`
DurationMS *int64 `json:"durationMs"`
Summary []string `json:"summary"`
}
func (t thread) chatSession() protocol.ChatSession {
@@ -817,8 +811,14 @@ func (i item) chatMessage(sessionID string) (protocol.ChatMessage, bool) {
if i.Text != "" {
msg.Parts = append(msg.Parts, protocol.ChatPart{Type: protocol.ChatPartText, Text: i.Text})
}
case "reasoning":
msg.Role = protocol.ChatRoleAssistant
if text := i.reasoningText(); text != "" {
msg.Parts = append(msg.Parts, protocol.ChatPart{Type: protocol.ChatPartReasoning, Text: text})
}
default:
return protocol.ChatMessage{}, false
msg.Role = protocol.ChatRoleAssistant
msg.Parts = []protocol.ChatPart{i.toolPart(!strings.EqualFold(i.Status, "inProgress"))}
}
return msg, len(msg.Parts) > 0
}