Files
multica/server/internal/integrations/lark/fresh_command.go
Wilson-G b92e4a53fb DH-106 为飞书接入补上 /new 会话指令 (MUL-3503) (#4396)
Lark/飞书入站消息新增 /new 首行指令,解析为 force_fresh_session,复用既有 daemon 会话续接门控。

Co-authored-by: Wilson-G <Wilson-G@users.noreply.github.com>
2026-06-24 14:16:22 +08:00

67 lines
1.6 KiB
Go

package lark
import "strings"
const (
newCommandPrefix = "/new"
)
// FreshSessionCommand is the normalized fresh-start directive extracted from a
// Lark inbound message. Body is the user prompt with the directive removed.
type FreshSessionCommand struct {
Body string
}
// parseFreshSessionCommand extracts a first-line /new command from a message
// body. Matching follows the /issue command rules: case-sensitive,
// token-bounded, and only the first non-empty line can be a command. That
// means /new and /issue are mutually exclusive on the same first line.
func parseFreshSessionCommand(body string) (*FreshSessionCommand, bool) {
lines := strings.Split(body, "\n")
firstIdx := -1
for i, line := range lines {
if strings.TrimSpace(line) != "" {
firstIdx = i
break
}
}
if firstIdx == -1 {
return nil, false
}
first := lines[firstIdx]
trimmed := strings.TrimLeft(first, " \t")
prefix, ok := matchedFreshPrefix(trimmed)
if !ok {
return nil, false
}
rest := trimmed[len(prefix):]
if rest != "" {
r0 := rest[0]
if r0 != ' ' && r0 != '\t' {
return nil, false
}
}
bodyParts := make([]string, 0, 2)
if firstLineBody := strings.TrimSpace(rest); firstLineBody != "" {
bodyParts = append(bodyParts, firstLineBody)
}
if firstIdx+1 < len(lines) {
bodyParts = append(bodyParts, strings.Join(lines[firstIdx+1:], "\n"))
}
stripped := strings.TrimRight(strings.Join(bodyParts, "\n"), " \t\n")
return &FreshSessionCommand{Body: stripped}, true
}
func matchedFreshPrefix(line string) (string, bool) {
switch {
case strings.HasPrefix(line, newCommandPrefix):
return newCommandPrefix, true
default:
return "", false
}
}