Help us shape the future of Go! Take the 2026 Go Developer Survey

command

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package command implements the platform-agnostic slash-command layer shared by IM (Lark / WeChat) and the Web chat page.

The three-layer model:

  1. Definition (this file): command name, aliases, description. Pure data.
  2. Execution (this package's Execute): takes (name, args, context), returns a structured Result describing what should happen and what to show the user. Knows nothing about IMJobMapping, URLs, or SSE.
  3. Platform adapter (outside this package): each platform translates the Result into its own state changes — IM writes to IMJobMapping and sends a text reply; Web updates URL + currentWorkspace / currentJob state and pushes a `command_system_message` SSE event.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsKnown

func IsKnown(text string) bool

IsKnown reports whether text is a recognized slash command (matching a canonical name or alias). Unknown slash text (e.g. a typo like /hlep, or a path like /etc/hosts) returns false so callers treat it as a regular message. Mirrors the frontend's isKnownCommand in utils/commands.ts.

func IsReadOnly

func IsReadOnly(text string) bool

IsReadOnly reports whether a known command only reads state and can safely run while an Agent turn is active. Navigation and mutation commands are rejected by the Web adapter during a running turn rather than delayed.

func Parse

func Parse(text string) (cmd, args string)

Parse splits raw text into (command, args). Returns empty command if the text doesn't begin with '/'.

func ResolveName

func ResolveName(raw string) string

ResolveName normalizes a raw command name (including aliases) to a canonical definition name. Returns empty string if no match — callers can then treat the text as a regular message. Matching is case-insensitive.

Types

type Action

type Action struct {
	Type        ActionType `json:"type"`
	WorkspaceID string     `json:"workspaceId,omitempty"`
	JobID       string     `json:"jobId,omitempty"`
}

Action describes the intended state change. Fields are populated only when relevant for Type; empty fields mean "no change / not applicable".

type ActionType

type ActionType string

ActionType describes the side effect a command wants the caller to apply. Adapters pattern-match on this and update their own state accordingly.

const (
	// ActionNone — purely display; no state change.
	ActionNone ActionType = ""
	// ActionSwitchWorkspace — change the current workspace context. IM updates
	// IMJobMapping; Web updates URL + currentWorkspace and then runs its
	// "reuse-or-create Job" logic for the chat page.
	ActionSwitchWorkspace ActionType = "switch_workspace"
	// ActionBindJob — switch to the given Job. IM rebinds the mapping; Web
	// reloads its chat-page state (URL, SSE, messages, etc.).
	ActionBindJob ActionType = "bind_job"
	// ActionNewJob — create a new Job in the current workspace and switch to
	// it. Adapters carry out the create+switch.
	ActionNewJob ActionType = "new_job"
)

type Definition

type Definition struct {
	Name        string
	Aliases     []string
	Description string
	Usage       string
}

Definition is per-command metadata. Duplicate-free — aliases share the same Definition.

func Definitions

func Definitions() []Definition

Definitions returns the full list of built-in commands, sorted by primary name. Used by /help and by the Web-side completion overlay.

The result is a shared, already-sorted slice — callers are read-only. IsKnown / ResolveName run on every chat-page message, so caching avoids rebuilding and re-sorting on each call.

type ExecCtx

type ExecCtx struct {
	Ctx              context.Context
	WorkspaceService workspace.Service
	JobService       job.Service

	// CurrentWorkspaceID / CurrentJobID are the current page/chat context at
	// the time the command was invoked. Empty means "not set".
	CurrentWorkspaceID string
	CurrentJobID       string

	// Platform optionally narrows output / behavior (IM keeps legacy hints
	// like "下一条消息将创建新的对话" and refuses cross-workspace /job use).
	// Empty defaults to PlatformWeb.
	Platform Platform
}

ExecCtx carries everything a command needs to run, independent of platform. Fields that don't apply to the caller should be left zero (e.g. Web doesn't need IM fields, IM may not set JobID if no mapping).

type Message

type Message struct {
	Text    string      `json:"text"`
	Present PresentType `json:"present,omitempty"`
}

Message is what the user sees after the command runs.

type Platform

type Platform string

Platform identifies the calling surface. Used to keep legacy IM output text intact while Web gets its cleaner equivalents. Also gates behavior differences like /job use cross-workspace: Web supports cross-ws binding (URL + currentWorkspace are updated together by the adapter); IM keeps the old "same workspace only" semantics since an IM chat is pinned to one workspace at a time via IMJobMapping.

const (
	PlatformWeb Platform = "web"
	PlatformIM  Platform = "im"
)

type PresentType

type PresentType string

PresentType tells the adapter how to render Result.Message.

const (
	// PresentInline — render as an in-chat system message (default).
	PresentInline PresentType = "inline"
	// PresentToast — render as a transient UI toast (Web only; IM treats it
	// as inline).
	PresentToast PresentType = "toast"
)

type Result

type Result struct {
	Action  Action  `json:"action"`
	Message Message `json:"message"`
}

Result is the structured output of a command.

func Execute

func Execute(cmd, args string, ec *ExecCtx) (*Result, bool)

Execute dispatches a command by its canonical name. Returns (nil, false) when the name is not recognized — the caller can fall through to its regular-message path.

Jump to

Keyboard shortcuts

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