Files
WeKnora/internal/im/command.go
nullkey f05f9b0e11 feat(im): add slash-command system with /help, /info, /search, /stop, /clear
Introduce a pluggable command framework for IM channels. Includes
Command interface, CommandRegistry for registration and dispatch,
and five built-in commands: /help (list commands), /info (inspect
knowledge bases), /search (search knowledge), /stop (cancel running
QA), and /clear (reset conversation context).
2026-03-20 15:49:59 +08:00

67 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package im
import (
"context"
"github.com/Tencent/WeKnora/internal/types"
)
// CommandAction represents a service-level side effect that a command requests.
// Using an enum keeps commands free of service/DB dependencies—they declare intent,
// the Service executes it.
type CommandAction int
const (
// ActionNone means no side effect beyond sending the reply.
ActionNone CommandAction = iota
// ActionClear soft-deletes the current ChannelSession and clears the LLM
// context so the next message creates a completely fresh conversation.
ActionClear
// ActionStop cancels the in-flight QA request for this user+chat.
ActionStop
)
// CommandResult is the output produced by a Command.Execute call.
type CommandResult struct {
// Content is the Markdown reply sent back to the user.
Content string
// Action requests a service-level side effect (reset, clear, …).
Action CommandAction
}
// CommandContext carries all runtime data a command needs during execution.
// Services are NOT here; inject them into command structs at construction time.
type CommandContext struct {
// Incoming is the raw IM message that triggered the command.
Incoming *IncomingMessage
// Session is the IM channel session for this user×chat combination.
Session *ChannelSession
// TenantID is the tenant that owns this bot deployment.
TenantID uint64
// AgentName is the display name of the bound agent (empty if none).
AgentName string
// CustomAgent is the bound agent configuration. Commands that need to
// inspect agent-level settings (e.g. /search reading KBSelectionMode)
// can access it directly. May be nil when no agent is bound.
CustomAgent *types.CustomAgent
// ChannelOutputMode is the channel-level output mode configured by the admin
// ("stream" or "full").
ChannelOutputMode string
}
// Command is the interface every IM slash-command must implement.
//
// Design rules:
// - Dependencies (DB, services) are injected at construction time.
// - Validation errors (bad args, entity not found) are returned as a
// CommandResult with a helpful message, NOT as an error.
// - error is reserved for infrastructure failures (DB errors, network, …).
type Command interface {
// Name is the primary token used after "/" (e.g. "kb", "mode").
Name() string
// Description is the one-line summary shown in /help output.
Description() string
// Execute runs the command and returns a reply to send to the user.
Execute(ctx context.Context, cmdCtx *CommandContext, args []string) (*CommandResult, error)
}