2026-06-19 21:49:28 +08:00
|
|
|
|
package eino
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"encoding/base64"
|
2026-06-19 21:58:17 +08:00
|
|
|
|
|
2026-06-19 21:49:28 +08:00
|
|
|
|
"github.com/cloudwego/eino/compose"
|
|
|
|
|
|
"github.com/cloudwego/eino/schema"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/hhs/camtalk/internal/ai/llm"
|
|
|
|
|
|
"github.com/hhs/camtalk/internal/logger"
|
|
|
|
|
|
"github.com/hhs/camtalk/internal/models"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// NewHistoryLambda 创建历史组装 Lambda 节点。
|
2026-06-19 21:58:17 +08:00
|
|
|
|
// 输入: *STTOutput → 输出: []*schema.Message
|
2026-06-19 21:49:28 +08:00
|
|
|
|
//
|
2026-06-19 21:58:17 +08:00
|
|
|
|
// 从 PipelineState 读取请求元数据(SessionID、Scenario、ImageData 等),
|
2026-06-19 21:49:28 +08:00
|
|
|
|
// 构建系统提示词,组装历史消息和当前用户输入(含多模态图片)。
|
2026-06-19 21:58:17 +08:00
|
|
|
|
func NewHistoryLambda(historyFetcher func(ctx context.Context, sessionID string, limit int) ([]models.Message, error), maxHistory int) *compose.Lambda {
|
|
|
|
|
|
return compose.InvokableLambda(func(ctx context.Context, sttOut STTOutput) ([]*schema.Message, error) {
|
2026-06-19 21:49:28 +08:00
|
|
|
|
log := logger.Log
|
2026-06-19 21:58:17 +08:00
|
|
|
|
|
|
|
|
|
|
// 从 State 读取请求元数据
|
|
|
|
|
|
state := stateFromCtx(ctx)
|
|
|
|
|
|
if state == nil {
|
|
|
|
|
|
return []*schema.Message{}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
state.mu.Lock()
|
|
|
|
|
|
sessionID := state.SessionID
|
|
|
|
|
|
requestID := state.RequestID
|
|
|
|
|
|
imageData := state.ImageData
|
|
|
|
|
|
scenario := state.Scenario
|
|
|
|
|
|
detailLevel := state.DetailLevel
|
|
|
|
|
|
language := sttOut.Language
|
|
|
|
|
|
state.mu.Unlock()
|
2026-06-19 21:49:28 +08:00
|
|
|
|
|
|
|
|
|
|
// 构建系统提示词
|
2026-06-19 21:58:17 +08:00
|
|
|
|
scenarioPrompt := llm.GetScenarioPrompt(scenario, language)
|
|
|
|
|
|
systemPrompt := llm.BuildSystemPrompt(language, detailLevel, scenarioPrompt)
|
2026-06-19 21:49:28 +08:00
|
|
|
|
|
2026-06-19 23:24:01 +08:00
|
|
|
|
// 构建 system message(仅文本,多模态内容只能放在 user 角色)
|
2026-06-19 21:49:28 +08:00
|
|
|
|
systemMsg := &schema.Message{
|
|
|
|
|
|
Role: schema.System,
|
|
|
|
|
|
Content: systemPrompt,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
messages := []*schema.Message{systemMsg}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取并追加历史消息
|
2026-06-19 21:58:17 +08:00
|
|
|
|
if historyFetcher != nil && sessionID != "" {
|
|
|
|
|
|
history, err := historyFetcher(ctx, sessionID, maxHistory)
|
2026-06-19 21:49:28 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Warnw("获取历史消息失败,继续处理", "error", err, "request_id", requestID)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
for _, msg := range history {
|
|
|
|
|
|
messages = append(messages, &schema.Message{
|
|
|
|
|
|
Role: schema.RoleType(msg.Role),
|
|
|
|
|
|
Content: msg.Content,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-19 23:24:01 +08:00
|
|
|
|
// 追加当前用户输入(含图片,多模态内容只能放在 user 角色)
|
|
|
|
|
|
// 注意:不能同时设置 Content 和 UserInputMultiContent,需要统一放到 MultiContent 中
|
|
|
|
|
|
if len(imageData) > 0 {
|
|
|
|
|
|
base64Str := base64.StdEncoding.EncodeToString(imageData)
|
|
|
|
|
|
mimeType := detectImageMimeType(imageData)
|
|
|
|
|
|
parts := []schema.MessageInputPart{
|
|
|
|
|
|
{
|
|
|
|
|
|
Type: schema.ChatMessagePartTypeText,
|
|
|
|
|
|
Text: sttOut.Text,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
Type: schema.ChatMessagePartTypeImageURL,
|
|
|
|
|
|
Image: &schema.MessageInputImage{
|
|
|
|
|
|
MessagePartCommon: schema.MessagePartCommon{
|
|
|
|
|
|
Base64Data: &base64Str,
|
|
|
|
|
|
MIMEType: mimeType,
|
|
|
|
|
|
},
|
|
|
|
|
|
Detail: schema.ImageURLDetailAuto,
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
messages = append(messages, &schema.Message{
|
|
|
|
|
|
Role: schema.User,
|
|
|
|
|
|
UserInputMultiContent: parts,
|
|
|
|
|
|
})
|
|
|
|
|
|
} else {
|
|
|
|
|
|
messages = append(messages, &schema.Message{
|
|
|
|
|
|
Role: schema.User,
|
|
|
|
|
|
Content: sttOut.Text,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2026-06-19 21:49:28 +08:00
|
|
|
|
|
|
|
|
|
|
log.Infow("历史组装完成",
|
|
|
|
|
|
"request_id", requestID,
|
|
|
|
|
|
"message_count", len(messages),
|
2026-06-19 21:58:17 +08:00
|
|
|
|
"has_image", len(imageData) > 0,
|
|
|
|
|
|
"scenario", scenario)
|
2026-06-19 21:49:28 +08:00
|
|
|
|
|
|
|
|
|
|
return messages, nil
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// detectImageMimeType 简单检测图片 MIME 类型。
|
|
|
|
|
|
func detectImageMimeType(data []byte) string {
|
|
|
|
|
|
if len(data) < 4 {
|
|
|
|
|
|
return "image/jpeg"
|
|
|
|
|
|
}
|
|
|
|
|
|
if data[0] == 0xFF && data[1] == 0xD8 && data[2] == 0xFF {
|
|
|
|
|
|
return "image/jpeg"
|
|
|
|
|
|
}
|
|
|
|
|
|
if data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47 {
|
|
|
|
|
|
return "image/png"
|
|
|
|
|
|
}
|
|
|
|
|
|
if data[0] == 0x47 && data[1] == 0x49 && data[2] == 0x46 {
|
|
|
|
|
|
return "image/gif"
|
|
|
|
|
|
}
|
|
|
|
|
|
if data[0] == 0x52 && data[1] == 0x49 && data[2] == 0x46 && data[3] == 0x46 {
|
|
|
|
|
|
return "image/webp"
|
|
|
|
|
|
}
|
2026-06-19 21:58:17 +08:00
|
|
|
|
return "image/jpeg"
|
2026-06-19 21:49:28 +08:00
|
|
|
|
}
|