mirror of
https://github.com/multica-ai/multica.git
synced 2026-07-13 05:16:29 +02:00
Lark/飞书入站消息新增 /new 首行指令,解析为 force_fresh_session,复用既有 daemon 会话续接门控。 Co-authored-by: Wilson-G <Wilson-G@users.noreply.github.com>
67 lines
1.6 KiB
Go
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
|
|
}
|
|
}
|