command

package
v0.0.0-...-0f1d74b Latest Latest
Warning

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

Go to latest
Published: Feb 5, 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 CommandFunc

type CommandFunc func() *cobra.Command

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

type ExecutionContext

type ExecutionContext struct {
	RequestSnapshot botcore.RequestSnapshot
	// contains filtered or unexported fields
}

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

func FromContext

func FromContext(ctx context.Context) *ExecutionContext

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

func (*ExecutionContext) Response

func (ctx *ExecutionContext) Response(msg any) error

Response 发送主动回复消息。 Parameters:

  • msg: 平台消息负载

Returns:

  • error: 发送失败时返回

func (*ExecutionContext) ResponseMarkdown

func (ctx *ExecutionContext) ResponseMarkdown(content string) error

ResponseMarkdown 发送 Markdown 主动回复。 Parameters:

  • content: Markdown 文本内容

Returns:

  • error: 发送失败时返回

func (*ExecutionContext) ResponseTemplateCard

func (ctx *ExecutionContext) ResponseTemplateCard(card any) error

ResponseTemplateCard 发送模板卡片主动回复。 Parameters:

  • card: 模板卡片负载

Returns:

  • error: 发送失败时返回

func (*ExecutionContext) SendNoResponse

func (ctx *ExecutionContext) SendNoResponse()

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

func (*ExecutionContext) SendPayload

func (ctx *ExecutionContext) SendPayload(payload any)

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

type Manager

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

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

func NewManager

func NewManager(factory CommandFunc, opts ...ManagerOption) *Manager

NewManager 绑定命令构建函数,返回实现 PipelineInvoker 的管理器。

func (*Manager) Trigger

func (m *Manager) Trigger(pipelineCtx botcore.PipelineContext) <-chan botcore.StreamChunk

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

type ManagerOption

type ManagerOption func(*Manager)

ManagerOption 自定义 Manager 行为。

func WithLogger

func WithLogger(l *log.Logger) ManagerOption

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

func WithResponser

func WithResponser(r botcore.Responser) ManagerOption

WithResponser 注入主动消息发送器(当 PipelineContext.Responser 为空时作为兜底)。

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 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