Refactored orchestrator for staged file handling, added structured prompt support, adjusted Feishu file handling
This commit is contained in:
@@ -4,6 +4,10 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -34,9 +38,17 @@ type IncomingMessage struct {
|
||||
MessageID string
|
||||
ChatID string
|
||||
UserID string
|
||||
MsgType string
|
||||
Text string
|
||||
FileName string
|
||||
FileKey string
|
||||
FileMime string
|
||||
FileBytes []byte
|
||||
FilePath string
|
||||
}
|
||||
|
||||
const maxFeishuFileBytes = 20 * 1024 * 1024
|
||||
|
||||
func NewBot(appID, appSecret, verifyToken, _ string, _ string, log *logger.Logger) (*Bot, error) {
|
||||
if appID == "" || appSecret == "" {
|
||||
return nil, fmt.Errorf("empty feishu app credentials")
|
||||
@@ -66,7 +78,7 @@ func (b *Bot) Run(ctx context.Context, handler func(context.Context, IncomingMes
|
||||
incoming, ok := parseIncoming(event)
|
||||
if !ok {
|
||||
if b.log != nil {
|
||||
b.log.Debugf("skip non-text or invalid feishu event")
|
||||
b.log.Debugf("skip unsupported or invalid feishu event")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -76,8 +88,11 @@ func (b *Bot) Run(ctx context.Context, handler func(context.Context, IncomingMes
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if incoming.MsgType == "file" {
|
||||
b.enrichFileIncoming(evtCtx, &incoming)
|
||||
}
|
||||
if b.log != nil {
|
||||
b.log.Infof("feishu message received message_id=%s chat_id=%s user_id=%s text=%s", incoming.MessageID, incoming.ChatID, incoming.UserID, incoming.Text)
|
||||
b.log.Infof("feishu message received message_id=%s chat_id=%s user_id=%s msg_type=%s text=%s", incoming.MessageID, incoming.ChatID, incoming.UserID, incoming.MsgType, incoming.Text)
|
||||
}
|
||||
reply, err := handler(evtCtx, incoming)
|
||||
if err != nil {
|
||||
@@ -159,6 +174,175 @@ func extractText(content string) (string, error) {
|
||||
return parsed.Text, nil
|
||||
}
|
||||
|
||||
func extractFileMeta(content string) (fileName string, fileKey string, err error) {
|
||||
var parsed map[string]any
|
||||
if err := json.Unmarshal([]byte(content), &parsed); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
readString := func(keys ...string) string {
|
||||
for _, key := range keys {
|
||||
if v, ok := parsed[key]; ok {
|
||||
s, ok := v.(string)
|
||||
if ok {
|
||||
trimmed := strings.TrimSpace(s)
|
||||
if trimmed != "" {
|
||||
return trimmed
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
fileName = readString("file_name", "fileName", "name", "filename")
|
||||
fileKey = readString("file_key", "fileKey", "key")
|
||||
return fileName, fileKey, nil
|
||||
}
|
||||
|
||||
func buildFileRecognitionText(fileName, fileKey string) string {
|
||||
if strings.TrimSpace(fileName) == "" {
|
||||
fileName = "(unknown)"
|
||||
}
|
||||
if strings.TrimSpace(fileKey) == "" {
|
||||
fileKey = "(unknown)"
|
||||
}
|
||||
|
||||
return strings.Join([]string{
|
||||
"用户发送了一条飞书文件消息。",
|
||||
"文件名: " + fileName,
|
||||
"文件Key: " + fileKey,
|
||||
"系统将先上传该文件到 LLM Provider,再由模型完成文档解析。若上传失败,本次请求将直接中止。",
|
||||
}, "\n")
|
||||
}
|
||||
|
||||
func (b *Bot) enrichFileIncoming(ctx context.Context, incoming *IncomingMessage) {
|
||||
if incoming == nil {
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(incoming.MessageID) == "" || strings.TrimSpace(incoming.FileKey) == "" {
|
||||
incoming.Text = buildFileRecognitionText(incoming.FileName, incoming.FileKey)
|
||||
incoming.Text += "\n\n未找到完整 file_key 或 message_id,暂时无法下载文件内容。"
|
||||
return
|
||||
}
|
||||
|
||||
content, fileName, err := b.downloadFileContent(ctx, incoming.MessageID, incoming.FileKey)
|
||||
if err != nil {
|
||||
if b.log != nil {
|
||||
b.log.Warnf("feishu download file content failed message_id=%s file_key=%s err=%v", incoming.MessageID, incoming.FileKey, err)
|
||||
}
|
||||
incoming.Text = buildFileRecognitionText(incoming.FileName, incoming.FileKey)
|
||||
incoming.Text += "\n\n文件下载失败: " + err.Error()
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(fileName) != "" {
|
||||
incoming.FileName = fileName
|
||||
}
|
||||
incoming.FileBytes = content
|
||||
incoming.FileMime = detectMimeByName(incoming.FileName)
|
||||
localPath, saveErr := saveIncomingFile("files", incoming.FileName, incoming.FileBytes)
|
||||
if saveErr != nil {
|
||||
if b.log != nil {
|
||||
b.log.Warnf("save incoming feishu file failed name=%s err=%v", incoming.FileName, saveErr)
|
||||
}
|
||||
incoming.Text = buildFileRecognitionText(incoming.FileName, incoming.FileKey)
|
||||
incoming.Text += "\n\n文件已下载但本地保存失败: " + saveErr.Error()
|
||||
return
|
||||
}
|
||||
incoming.FilePath = localPath
|
||||
incoming.Text = buildFileRecognitionText(incoming.FileName, incoming.FileKey)
|
||||
incoming.Text += fmt.Sprintf("\n\n文件已下载并保存到本地,路径=%s,大小=%d bytes,mime=%s。", incoming.FilePath, len(content), incoming.FileMime)
|
||||
}
|
||||
|
||||
func (b *Bot) downloadFileContent(ctx context.Context, messageID, fileKey string) ([]byte, string, error) {
|
||||
req := larkim.NewGetMessageResourceReqBuilder().
|
||||
MessageId(messageID).
|
||||
FileKey(fileKey).
|
||||
Type("file").
|
||||
Build()
|
||||
|
||||
resp, err := b.apiClient.Im.MessageResource.Get(ctx, req)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if resp == nil || resp.File == nil {
|
||||
if resp != nil {
|
||||
return nil, "", fmt.Errorf("empty file stream code=%d msg=%s", resp.Code, resp.Msg)
|
||||
}
|
||||
return nil, "", fmt.Errorf("empty file stream")
|
||||
}
|
||||
|
||||
bts, err := io.ReadAll(resp.File)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if len(bts) > maxFeishuFileBytes {
|
||||
return nil, "", fmt.Errorf("file too large: %d bytes, max=%d", len(bts), maxFeishuFileBytes)
|
||||
}
|
||||
return bts, strings.TrimSpace(resp.FileName), nil
|
||||
}
|
||||
|
||||
func detectMimeByName(fileName string) string {
|
||||
ext := strings.ToLower(strings.TrimSpace(filepath.Ext(fileName)))
|
||||
if ext == "" {
|
||||
return "application/octet-stream"
|
||||
}
|
||||
m := strings.TrimSpace(mime.TypeByExtension(ext))
|
||||
if m == "" {
|
||||
return "application/octet-stream"
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func saveIncomingFile(baseDir, fileName string, content []byte) (string, error) {
|
||||
if len(content) == 0 {
|
||||
return "", fmt.Errorf("empty file content")
|
||||
}
|
||||
if strings.TrimSpace(baseDir) == "" {
|
||||
baseDir = "files"
|
||||
}
|
||||
if err := os.MkdirAll(baseDir, 0o755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
safeName := sanitizeFileName(fileName)
|
||||
if safeName == "" {
|
||||
safeName = "upload.bin"
|
||||
}
|
||||
finalName := fmt.Sprintf("%d_%s", time.Now().UnixNano(), safeName)
|
||||
target := filepath.Join(baseDir, finalName)
|
||||
if err := os.WriteFile(target, content, 0o644); err != nil {
|
||||
return "", err
|
||||
}
|
||||
abs, err := filepath.Abs(target)
|
||||
if err != nil {
|
||||
return target, nil
|
||||
}
|
||||
return abs, nil
|
||||
}
|
||||
|
||||
func sanitizeFileName(fileName string) string {
|
||||
name := strings.TrimSpace(filepath.Base(fileName))
|
||||
if name == "" || name == "." || name == ".." {
|
||||
return ""
|
||||
}
|
||||
var b strings.Builder
|
||||
for _, r := range name {
|
||||
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '.' || r == '_' || r == '-' {
|
||||
b.WriteRune(r)
|
||||
continue
|
||||
}
|
||||
b.WriteByte('_')
|
||||
}
|
||||
out := strings.TrimSpace(b.String())
|
||||
if out == "" || out == "." || out == ".." {
|
||||
return ""
|
||||
}
|
||||
if strings.HasPrefix(out, ".") {
|
||||
out = "file" + out
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func parseIncoming(event *larkim.P2MessageReceiveV1) (IncomingMessage, bool) {
|
||||
if event == nil || event.Event == nil || event.Event.Message == nil || event.Event.Sender == nil || event.Event.Sender.SenderId == nil {
|
||||
return IncomingMessage{}, false
|
||||
@@ -168,12 +352,11 @@ func parseIncoming(event *larkim.P2MessageReceiveV1) (IncomingMessage, bool) {
|
||||
}
|
||||
|
||||
msg := event.Event.Message
|
||||
if msg.MessageType == nil || *msg.MessageType != "text" || msg.ChatId == nil || msg.Content == nil || msg.MessageId == nil {
|
||||
if msg.MessageType == nil || msg.ChatId == nil || msg.Content == nil || msg.MessageId == nil {
|
||||
return IncomingMessage{}, false
|
||||
}
|
||||
|
||||
text, err := extractText(*msg.Content)
|
||||
if err != nil {
|
||||
msgType := strings.TrimSpace(*msg.MessageType)
|
||||
if msgType == "" {
|
||||
return IncomingMessage{}, false
|
||||
}
|
||||
|
||||
@@ -186,12 +369,33 @@ func parseIncoming(event *larkim.P2MessageReceiveV1) (IncomingMessage, bool) {
|
||||
userID = *event.Event.Sender.SenderId.UnionId
|
||||
}
|
||||
|
||||
return IncomingMessage{
|
||||
incoming := IncomingMessage{
|
||||
MessageID: *msg.MessageId,
|
||||
ChatID: *msg.ChatId,
|
||||
UserID: userID,
|
||||
Text: text,
|
||||
}, true
|
||||
MsgType: msgType,
|
||||
}
|
||||
|
||||
switch msgType {
|
||||
case "text":
|
||||
text, err := extractText(*msg.Content)
|
||||
if err != nil {
|
||||
return IncomingMessage{}, false
|
||||
}
|
||||
incoming.Text = text
|
||||
return incoming, true
|
||||
case "file":
|
||||
fileName, fileKey, err := extractFileMeta(*msg.Content)
|
||||
if err != nil {
|
||||
return IncomingMessage{}, false
|
||||
}
|
||||
incoming.FileName = fileName
|
||||
incoming.FileKey = fileKey
|
||||
incoming.Text = buildFileRecognitionText(fileName, fileKey)
|
||||
return incoming, true
|
||||
default:
|
||||
return IncomingMessage{}, false
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) shouldProcessMessage(messageID string) bool {
|
||||
|
||||
Reference in New Issue
Block a user