command

package
v0.0.0-...-d113ed5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCommandNotFound 表示输入命令在注册表中不存在。
	ErrCommandNotFound = errors.New("command not found")
	// ErrCommandRequired 表示未提供任何命令关键字。
	ErrCommandRequired = errors.New("command required")
)

定义命令解析与分发阶段的通用错误,便于统一处理提示文案。

Functions

func WithExecutionContext

func WithExecutionContext(ctx context.Context, execCtx *ExecutionContext) context.Context

WithExecutionContext 将 ExecutionContext 注入到标准 context.Context 中。

Types

type CommandFactory

type CommandFactory func() *cobra.Command

CommandFactory 定义创建 Cobra 命令树的工厂函数类型。 在 HTTP 服务中,每个请求必须拥有独立的命令对象实例,以避免 Flag 解析的并发冲突。

type ContextValues

type ContextValues map[string]string

ContextValues 存储命令执行过程中的上下文扩展字段。

type ConversationStore

type ConversationStore interface {
	Load(key string) (ContextValues, error)
	Save(key string, values ContextValues) error
}

ConversationStore 定义上下文存取接口,便于替换实现。

type ExecutionContext

type ExecutionContext struct {
	RequestSnapshot botcore.RequestSnapshot
	Values          ContextValues
	Store           ConversationStore
	// contains filtered or unexported fields
}

ExecutionContext 为命令 handler 提供必要的环境信息。

func FromContext

func FromContext(ctx context.Context) *ExecutionContext

FromContext 从标准 context.Context 中提取 ExecutionContext。

func (*ExecutionContext) ConversationKey

func (ctx *ExecutionContext) ConversationKey() string

ConversationKey 返回当前上下文在存储中的唯一 key。

func (*ExecutionContext) Responder

func (ctx *ExecutionContext) Responder() Responder

Responder 返回主动消息发送器。

func (*ExecutionContext) SetNoResponse

func (ctx *ExecutionContext) SetNoResponse()

SetNoResponse 立即发送静默信号。 Bot 层收到此信号后将直接返回 HTTP 200 OK 空包。

func (*ExecutionContext) SetResponsePayload

func (ctx *ExecutionContext) SetResponsePayload(payload any)

SetResponsePayload 立即发送非流式响应对象。

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager 实现 PipelineInvoker,负责串联解析、构建 Cobra 命令树并执行。

func NewManager

func NewManager(factory CommandFactory, store ConversationStore, opts ...ManagerOption) *Manager

NewManager 绑定命令工厂与存储,返回实现 PipelineInvoker 的管理器。

func (*Manager) Trigger

func (m *Manager) Trigger(update botcore.RequestSnapshot) <-chan botcore.StreamChunk

Trigger 满足 botcore.PipelineInvoker,为每个请求构建独立的命令树并执行。

type ManagerOption

type ManagerOption func(*Manager)

ManagerOption 自定义 Manager 行为。

func WithLogger

func WithLogger(l *log.Logger) ManagerOption

WithLogger 注入自定义日志记录器。

func WithResponder

func WithResponder(r Responder) ManagerOption

WithResponder 注入主动消息发送器。

type MemoryStore

type MemoryStore struct {
	// contains filtered or unexported fields
}

MemoryStore 提供简单的基于内存的上下文存储实现。 仅用于命令执行期的上下文键值(非聊天历史);进程重启即丢失。

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore 创建内存存储实例。

func (*MemoryStore) Load

func (s *MemoryStore) Load(key string) (ContextValues, error)

Load 返回指定 key 的上下文副本。

func (*MemoryStore) Save

func (s *MemoryStore) Save(key string, values ContextValues) error

Save 合并并存储上下文增量。

type ParseResult

type ParseResult struct {
	IsCommand   bool     // 是否检测到命令前缀
	Tokens      []string // 解析后的命令及参数 token(包含命令本身)
	Raw         string   // 原始输入文本
	ArgumentRaw string   // 去除命令后的原始参数串
}

ParseResult 承载文本命令解析后的结构化结果。

type Parser

type Parser struct {
	Prefix string // 命令前缀,默认 "/"
}

Parser 解析企业微信文本内容,判定是否命令并拆分 token。

func NewParser

func NewParser() Parser

NewParser 创建带默认前缀的解析器。

func (Parser) Parse

func (p Parser) Parse(text string) ParseResult

Parse 将文本拆解为命令 token。规则参考 Telegram Message.IsCommand。

type Responder

type Responder interface {
	Send(responseURL string, msg any) error
	SendMarkdown(responseURL, content string) error
	SendTemplateCard(responseURL string, card any) error
}

Responder 定义命令执行过程中的主动发送能力。

type StreamWriter

type StreamWriter struct {
	Ch chan<- botcore.StreamChunk
}

StreamWriter 实现 io.Writer 接口,将输出重定向到 StreamChunk 通道。 这允许 Cobra 命令像操作 stdout 一样直接打印,而结果会被流式传输给用户。

func NewStreamWriter

func NewStreamWriter(ch chan<- botcore.StreamChunk) *StreamWriter

NewStreamWriter 创建一个新的 StreamWriter。

func (*StreamWriter) Write

func (w *StreamWriter) Write(p []byte) (n int, err error)

Write 将字节切片转换为 StreamChunk 发送。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL