Files
sproutclaw-web/backend/internal/services/slash_dispatch.go
2026-06-13 20:16:09 +08:00

58 lines
1.3 KiB
Go

package services
import (
"strings"
)
// supportedWebUICommands is the whitelist of commands the WebUI handles.
var supportedWebUICommands = map[string]bool{
"compact": true,
"new": true,
"reload": true,
"clone": true,
"name": true,
"model": true,
"session": true,
"export": true,
"copy": true,
}
// SlashDispatchResult holds the parsed result of a slash command attempt.
type SlashDispatchResult struct {
Handled bool
Command string
Args string
// For unsupported commands, return a friendly message
Message string
}
// TrySlashDispatch checks if the message is a slash command the WebUI supports.
func TrySlashDispatch(message string) SlashDispatchResult {
if !strings.HasPrefix(message, "/") {
return SlashDispatchResult{}
}
// strip leading slash and split
trimmed := strings.TrimPrefix(message, "/")
parts := strings.SplitN(trimmed, " ", 2)
cmd := strings.ToLower(parts[0])
args := ""
if len(parts) > 1 {
args = strings.TrimSpace(parts[1])
}
if !supportedWebUICommands[cmd] {
return SlashDispatchResult{
Handled: true,
Command: cmd,
Args: args,
Message: "该命令在 WebUI 模式下暂不支持,请在终端中使用",
}
}
return SlashDispatchResult{
Handled: true,
Command: cmd,
Args: args,
}
}