feat(utils): add BuildRuntimeMessageText function for message formatting
This commit is contained in:
+1
-1
Submodule docs updated: c694af9a91...9731debd6a
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"cs-agent/internal/ai/runtime/executor"
|
"cs-agent/internal/ai/runtime/executor"
|
||||||
|
"cs-agent/internal/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
@@ -25,6 +26,7 @@ func (s *Service) Run(ctx context.Context, req Request) (*Summary, error) {
|
|||||||
if s == nil || s.runtime == nil || s.prepare == nil {
|
if s == nil || s.runtime == nil || s.prepare == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
req.UserMessage.Content = utils.BuildRuntimeMessageText(req.UserMessage.MessageType, req.UserMessage.Content)
|
||||||
selectedSkill, skillReason, skillTrace, skillErr := s.prepare.selectSkill(ctx, req)
|
selectedSkill, skillReason, skillTrace, skillErr := s.prepare.selectSkill(ctx, req)
|
||||||
req.SelectedSkill = selectedSkill
|
req.SelectedSkill = selectedSkill
|
||||||
req.SkillRouteReason = skillReason
|
req.SkillRouteReason = skillReason
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package adapter
|
package adapter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
|
|
||||||
"cs-agent/internal/models"
|
"cs-agent/internal/models"
|
||||||
"cs-agent/internal/pkg/enums"
|
"cs-agent/internal/pkg/enums"
|
||||||
|
"cs-agent/internal/pkg/utils"
|
||||||
"cs-agent/internal/repositories"
|
"cs-agent/internal/repositories"
|
||||||
|
|
||||||
"github.com/cloudwego/eino/schema"
|
"github.com/cloudwego/eino/schema"
|
||||||
@@ -54,7 +53,7 @@ func BuildSchemaMessage(item *models.Message) *schema.Message {
|
|||||||
if item == nil {
|
if item == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
content := strings.TrimSpace(item.Content)
|
content := utils.BuildRuntimeMessageText(item.MessageType, item.Content)
|
||||||
if content == "" {
|
if content == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,6 +105,26 @@ func BuildHTMLSummary(content string) string {
|
|||||||
return strings.TrimSpace(strings.Join(parts, " "))
|
return strings.TrimSpace(strings.Join(parts, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BuildRuntimeMessageText(messageType enums.IMMessageType, content string) string {
|
||||||
|
content = strings.TrimSpace(content)
|
||||||
|
switch messageType {
|
||||||
|
case enums.IMMessageTypeHTML:
|
||||||
|
return BuildHTMLSummary(content)
|
||||||
|
case enums.IMMessageTypeImage:
|
||||||
|
if content != "" {
|
||||||
|
return "[图片] " + content
|
||||||
|
}
|
||||||
|
return "[图片]"
|
||||||
|
case enums.IMMessageTypeAttachment:
|
||||||
|
if content != "" {
|
||||||
|
return "[附件] " + content
|
||||||
|
}
|
||||||
|
return "[附件]"
|
||||||
|
default:
|
||||||
|
return content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BuildRenderableMessage(item *models.Message) (content, payload string) {
|
func BuildRenderableMessage(item *models.Message) (content, payload string) {
|
||||||
if item == nil {
|
if item == nil {
|
||||||
return "", ""
|
return "", ""
|
||||||
|
|||||||
@@ -71,6 +71,22 @@ func TestBuildMessageHTMLForResponseAddsSignedURL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuildRuntimeMessageTextForHTML(t *testing.T) {
|
||||||
|
got := BuildRuntimeMessageText(enums.IMMessageTypeHTML, `<p>你好</p><p><img data-provider="local" data-storage-key="images/demo.png" alt="demo"></p>`)
|
||||||
|
if got != "你好 [图片]" {
|
||||||
|
t.Fatalf("expected html converted to plain text summary, got: %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildRuntimeMessageTextForAssetMessages(t *testing.T) {
|
||||||
|
if got := BuildRuntimeMessageText(enums.IMMessageTypeImage, "demo.png"); got != "[图片] demo.png" {
|
||||||
|
t.Fatalf("unexpected image runtime text: %q", got)
|
||||||
|
}
|
||||||
|
if got := BuildRuntimeMessageText(enums.IMMessageTypeAttachment, "spec.pdf"); got != "[附件] spec.pdf" {
|
||||||
|
t.Fatalf("unexpected attachment runtime text: %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestBuildRenderableMessageTransformsPayloadAndHTML(t *testing.T) {
|
func TestBuildRenderableMessageTransformsPayloadAndHTML(t *testing.T) {
|
||||||
config.SetCurrent(&config.Config{
|
config.SetCurrent(&config.Config{
|
||||||
Storage: config.StorageConfig{
|
Storage: config.StorageConfig{
|
||||||
|
|||||||
Reference in New Issue
Block a user