mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
53 lines
1.9 KiB
Go
53 lines
1.9 KiB
Go
package chatd
|
|
|
|
import (
|
|
"github.com/google/uuid"
|
|
"github.com/sqlc-dev/pqtype"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatprompt"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatstate"
|
|
)
|
|
|
|
// newChatMachine constructs a chat-scoped state machine handle bound to
|
|
// the server's database and pubsub.
|
|
func (p *Server) newChatMachine(chatID uuid.UUID) *chatstate.ChatMachine {
|
|
return chatstate.NewChatMachine(p.db, p.pubsub, chatID, chatstate.Options{})
|
|
}
|
|
|
|
// systemMessage builds a chatstate.Message representing a system
|
|
// prompt entry for the initial-history slice of CreateChat.
|
|
func systemMessage(rawContent pqtype.NullRawMessage, modelConfigID uuid.UUID) chatstate.Message {
|
|
return chatstate.Message{
|
|
Role: database.ChatMessageRoleSystem,
|
|
Content: rawContent,
|
|
Visibility: database.ChatMessageVisibilityModel,
|
|
ModelConfigID: uuid.NullUUID{UUID: modelConfigID, Valid: modelConfigID != uuid.Nil},
|
|
ContentVersion: chatprompt.CurrentContentVersion,
|
|
}
|
|
}
|
|
|
|
// userMessage builds a chatstate.Message representing a user message
|
|
// for CreateChat, SendMessage, or EditMessage.
|
|
func userMessage(rawContent pqtype.NullRawMessage, modelConfigID, createdBy uuid.UUID) chatstate.Message {
|
|
return chatstate.Message{
|
|
Role: database.ChatMessageRoleUser,
|
|
Content: rawContent,
|
|
Visibility: database.ChatMessageVisibilityBoth,
|
|
ModelConfigID: uuid.NullUUID{UUID: modelConfigID, Valid: modelConfigID != uuid.Nil},
|
|
CreatedBy: uuid.NullUUID{UUID: createdBy, Valid: createdBy != uuid.Nil},
|
|
ContentVersion: chatprompt.CurrentContentVersion,
|
|
}
|
|
}
|
|
|
|
// busyBehaviorToChatState converts the public busy-behavior enum used
|
|
// by the server API to the chatstate variant.
|
|
func busyBehaviorToChatState(b SendMessageBusyBehavior) chatstate.BusyBehavior {
|
|
switch b {
|
|
case SendMessageBusyBehaviorInterrupt:
|
|
return chatstate.BusyBehaviorInterrupt
|
|
default:
|
|
return chatstate.BusyBehaviorQueue
|
|
}
|
|
}
|