Skip to content

feat: make workspaceAgentAddChatContext use the chatd state machine#26112

Open
hugodutka wants to merge 1 commit into
hugodutka/chatd-refactor-5from
hugodutka/hugodutka/chatd-refactor-6
Open

feat: make workspaceAgentAddChatContext use the chatd state machine#26112
hugodutka wants to merge 1 commit into
hugodutka/chatd-refactor-5from
hugodutka/hugodutka/chatd-refactor-6

Conversation

@hugodutka
Copy link
Copy Markdown
Contributor

@hugodutka hugodutka commented Jun 5, 2026

PR 6 of the chatd refactor. Originally not included in the RFC - I wasn't aware that workspaceAgentAddChatContext existed. It seems to be an internal endpoint that the workspace agent can call to inject additional context into the chat. I don't think it's used in production today, but I refactored it either way.

Previous PR in the stack: #26111

@hugodutka hugodutka force-pushed the hugodutka/chatd-refactor-5 branch from 8fa644f to aef0c15 Compare June 5, 2026 18:05
@hugodutka hugodutka force-pushed the hugodutka/hugodutka/chatd-refactor-6 branch from efdf24f to 1627cf8 Compare June 5, 2026 18:05
@hugodutka hugodutka changed the title pr 6 feat: make workspaceAgentAddChatContext use the chatd state machine Jun 8, 2026
@hugodutka hugodutka marked this pull request as ready for review June 8, 2026 12:38
@hugodutka hugodutka requested review from johnstcn and mafredri and removed request for mafredri June 8, 2026 12:38
@hugodutka hugodutka force-pushed the hugodutka/hugodutka/chatd-refactor-6 branch from 1627cf8 to d0f134b Compare June 8, 2026 13:07
@hugodutka hugodutka force-pushed the hugodutka/chatd-refactor-5 branch from aef0c15 to 6c84888 Compare June 8, 2026 13:07
Comment thread coderd/workspaceagents.go
Comment on lines +2606 to +2634
if errors.Is(err, chatstate.ErrMessageQueueFull) {
var queueFull *chatstate.MessageQueueFullError
detail := ""
if errors.As(err, &queueFull) {
detail = fmt.Sprintf("Maximum %d messages can be queued.", queueFull.Max)
}
httpapi.Write(ctx, rw, http.StatusTooManyRequests, codersdk.Response{
Message: "Message queue is full.",
Detail: detail,
})
return
}
if errors.Is(err, chatstate.ErrInvalidState) {
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{
Message: "Chat is in an invalid state.",
})
return
}
if errors.Is(err, chatstate.ErrTransitionNotAllowed) {
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{
Message: "Chat is not in a state that accepts new context.",
Detail: err.Error(),
})
return
}
if errors.Is(err, chatstate.ErrChatNotFound) {
writeAgentChatError(ctx, rw, errChatNotFound)
return
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion, non-blocking: maybe rewrite this as a switch?

Comment thread coderd/workspaceagents.go
Comment on lines +2856 to +2872
func agentChatContextStateMessage(
content pqtype.NullRawMessage,
modelConfigID uuid.UUID,
ownerID uuid.UUID,
apiKeyID string,
) chatstate.Message {
return chatstate.Message{
Role: database.ChatMessageRoleUser,
Content: content,
Visibility: database.ChatMessageVisibilityBoth,
ModelConfigID: uuid.NullUUID{UUID: modelConfigID, Valid: modelConfigID != uuid.Nil},
CreatedBy: uuid.NullUUID{UUID: ownerID, Valid: ownerID != uuid.Nil},
ContentVersion: chatprompt.CurrentContentVersion,
APIKeyID: sql.NullString{String: apiKeyID, Valid: apiKeyID != ""},
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion, non-blocking: this is only used in one place, just inline it for clarity.

Comment thread coderd/workspaceagents.go
Comment on lines +2587 to +2589
if len(sendResult.InsertedMessages) == 0 {
return nil
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an error case? We're trying to send a message, but the result was that nothing was sent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants