feat: introduce ChatMessageContent model for enhanced message handling

- Added ChatMessageContent model to encapsulate message content types and action statuses, improving the structure of chat messages.
- Updated ChatMessage model to include a slice of ChatMessageContent, allowing for multiple content items per message.
- Implemented GetContent method to handle content retrieval and formatting based on content type, enhancing message processing capabilities.
- Ensured alignment with existing coding standards and added necessary comments for clarity and maintainability.
This commit is contained in:
Marvin Zhang
2025-04-08 17:58:43 +08:00
parent c4291e7a93
commit 37307d7c01
2 changed files with 95 additions and 8 deletions

View File

@@ -7,12 +7,52 @@ import (
type ChatMessage struct {
any `collection:"chat_messages"`
BaseModel `bson:",inline"`
ConversationId primitive.ObjectID `json:"conversation_id" bson:"conversation_id" description:"Conversation ID"`
Role string `json:"role" bson:"role" description:"Message role (user/assistant/system)"`
Content string `json:"content" bson:"content" description:"Message content"`
Tokens int `json:"tokens" bson:"tokens" description:"Number of tokens in the message"`
Model string `json:"model" bson:"model" description:"AI model used"`
Metadata map[string]any `json:"metadata,omitempty" bson:"metadata,omitempty" description:"Additional metadata"`
Status string `json:"status" bson:"status" description:"Message status (pending/completed/failed)"`
Error string `json:"error,omitempty" bson:"error,omitempty" description:"Error message if failed"`
ConversationId primitive.ObjectID `json:"conversation_id" bson:"conversation_id" description:"Conversation ID"`
Role string `json:"role" bson:"role" description:"Message role (user/assistant/system)"`
Content string `json:"content,omitempty" bson:"content,omitempty" description:"Message content"`
IsAgent bool `json:"is_agent,omitempty" bson:"is_agent,omitempty" description:"Is agent"`
ContentIds []primitive.ObjectID `json:"content_ids,omitempty" bson:"content_ids,omitempty" description:"Content IDs"`
Contents []ChatMessageContent `json:"contents,omitempty" bson:"-" description:"Contents"`
Tokens int `json:"tokens" bson:"tokens" description:"Number of tokens in the message"`
Model string `json:"model" bson:"model" description:"AI model used"`
Metadata map[string]any `json:"metadata,omitempty" bson:"metadata,omitempty" description:"Additional metadata"`
Status string `json:"status" bson:"status" description:"Message status (pending/completed/failed)"`
Error string `json:"error,omitempty" bson:"error,omitempty" description:"Error message if failed"`
}
func (m *ChatMessage) GetContent() string {
// If the message has a single content item, return it directly
if m.Content != "" {
return m.Content
}
// If the message has multiple content items, concatenate them
var result string
for _, content := range m.Contents {
switch content.Type {
case ChatMessageContentTypeText:
result += content.Content
case ChatMessageContentTypeAction:
// Format action content with status
actionInfo := "[" + content.Action
if content.ActionStatus != "" {
actionInfo += " - " + string(content.ActionStatus)
}
actionInfo += "]"
if content.Content != "" {
result += actionInfo + ": " + content.Content
} else {
result += actionInfo
}
default:
// For any unrecognized type, just add the content
result += content.Content
}
// Add newline between content items
result += "\n\n"
}
return result
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2025. Core Digital Limited
* 版权所有 (c) 2025. 重庆科锐数研科技有限公司 (Core Digital Limited)
* All rights reserved. 保留所有权利。
*
* 该软件由 重庆科锐数研科技有限公司 (Core Digital Limited) 开发,未经明确书面许可,任何人不得使用、复制、修改或分发该软件的任何部分。
* This software is developed by Core Digital Limited. No one is permitted to use, copy, modify, or distribute this software without explicit written permission.
*
* 许可证:
* 该软件仅供授权使用。授权用户有权在授权范围内使用、复制、修改和分发该软件。
* License:
* This software is for authorized use only. Authorized users are permitted to use, copy, modify, and distribute this software within the scope of their authorization.
*
* 免责声明:
* 该软件按"原样"提供,不附带任何明示或暗示的担保,包括但不限于对适销性和适用于特定目的的担保。在任何情况下,版权持有者或其许可方对因使用该软件而产生的任何损害或其他责任概不负责。
* Disclaimer:
* This software is provided "as is," without any express or implied warranties, including but not limited to warranties of merchantability and fitness for a particular purpose. In no event shall the copyright holder or its licensors be liable for any damages or other liability arising from the use of this software.
*/
package models
import "go.mongodb.org/mongo-driver/bson/primitive"
type ChatMessageContentType string
const (
ChatMessageContentTypeText ChatMessageContentType = "text"
ChatMessageContentTypeAction ChatMessageContentType = "action"
)
type ChatMessageContentActionStatus string
const (
ChatMessageContentActionStatusPending ChatMessageContentActionStatus = "pending"
ChatMessageContentActionStatusSuccess ChatMessageContentActionStatus = "success"
ChatMessageContentActionStatusFailed ChatMessageContentActionStatus = "failed"
)
type ChatMessageContent struct {
any `collection:"chat_message_contents"`
BaseModel `bson:",inline"`
MessageId primitive.ObjectID `json:"message_id" bson:"message_id" description:"Message ID"`
Content string `json:"content" bson:"content" description:"Message content"`
Type ChatMessageContentType `json:"type" bson:"type" description:"Message type (text/action)"`
Action string `json:"action,omitempty" bson:"action,omitempty" description:"Action name"`
ActionStatus ChatMessageContentActionStatus `json:"action_status,omitempty" bson:"action_status,omitempty" description:"Action status"`
}