commands

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package commands implements OpenClaw-style slash commands for the harness.

Two kinds exist:

  • Workspace commands: user-authored markdown files discovered from the workspace (mirroring the skills-folder model). A command file is "<dir>/<name>.md", optionally namespaced one level as "<dir>/<ns>/<name>.md" -> "<ns>:<name>". Its YAML-ish frontmatter carries description / argument-hint / model / allowed-tools and the body is a prompt template. Invoking "/<name> <args>" expands the body (substituting $ARGUMENTS and $1..$9) and feeds the result to the model as the turn's user prompt.

  • Reserved built-ins (help, commands, clear): handled by the runner without calling the model. Reserved names cannot be shadowed by workspace files.

Parsing follows OpenClaw: a leading "/", an optional ":" after the name ("/think: high" == "/think high"), and hyphen/underscore-insensitive names.

NOTE: literal OpenClaw registers commands as code handlers; a Go sandbox runtime cannot load handler code from a workspace, so workspace commands are markdown prompt-expansion (the Claude-Code convention) while parsing/dispatch semantics stay faithful to OpenClaw.

Index

Constants

This section is empty.

Variables

View Source
var ReservedNames = func() map[string]string {
	definitions := Definitions(SurfaceRunner)
	out := make(map[string]string, len(definitions))
	for _, definition := range definitions {
		out[CanonicalKey(definition.Name)] = definition.Description
	}
	return out
}()

ReservedNames are built-in command names handled by the runner; workspace files using these names are ignored so built-ins cannot be shadowed. The value is the help-line description.

Functions

func CanonicalBuiltin added in v0.0.2

func CanonicalBuiltin(name string) (canonical string, ok bool)

CanonicalBuiltin resolves a known built-in or alias. Unknown names are returned in canonical-key form with ok=false.

func CanonicalKey

func CanonicalKey(name string) string

CanonicalKey normalizes a command name for lookup: lower-case with underscores folded to hyphens so "/dock-discord" and "/dock_discord" resolve identically. Namespace separators (":") are preserved.

func Expand

func Expand(body, args string) string

Expand substitutes argument tokens in a command body:

$ARGUMENTS      -> the full raw argument string
$1 .. $9        -> whitespace-split positional arguments ("" when absent)

Tokens with no corresponding argument expand to the empty string. $ARGUMENTS is listed first so it is never partially matched by a positional token.

func IsReserved

func IsReserved(name string) bool

IsReserved reports whether name collides with a reserved built-in command.

func Parse

func Parse(line string) (name, args string, ok bool)

Parse splits a chat line into a command name and raw argument string. It returns ok=false when the line is not a slash command. The name is lower-cased and stripped of a trailing ":" ("/think:" -> "think"); args is everything after the first run of whitespace, trimmed. A bare "/", a leading "//", or a "/ foo" (slash then space) is not treated as a command.

Types

type Command

type Command struct {
	Name         string   // canonical, lower-case (e.g. "commit" or "git:commit")
	Description  string   // from frontmatter description (or fallback)
	ArgumentHint string   // from frontmatter argument-hint (display only)
	Model        string   // optional per-turn model override (frontmatter model)
	AllowedTools []string // from frontmatter allowed-tools (display only for now)
	Body         string   // prompt template (frontmatter stripped)
	Path         string   // workspace-relative or absolute operator source path
}

Command is a single workspace command discovered from the filesystem.

func Discover

func Discover(workspaceRoot string, dirs []string) ([]Command, error)

Discover scans each workspace-relative or absolute dir for "<name>.md" command files plus one level of namespace dirs ("<ns>/<name>.md" -> "<ns>:<name>"). Missing dirs are skipped. Commands are de-duplicated by canonical name (first occurrence wins; dirs are scanned in order). Reserved names are skipped.

type CommandDef added in v0.0.2

type CommandDef struct {
	Name         string
	Aliases      []string
	Description  string
	ArgumentHint string
	Owner        Owner
	Surfaces     []Surface
	AliasFor     string
}

CommandDef is the declarative metadata shared by reserved-name checks, help, completion, and command catalogs. Aliases are expanded by Definitions.

func Definitions added in v0.0.2

func Definitions(surface Surface) []CommandDef

Definitions returns detached definitions for a surface, with aliases expanded into independently displayable entries pointing at their canonical command.

type Owner added in v0.0.2

type Owner string

Owner identifies the component that handles a command after metadata-driven discovery. Handlers remain in their owning packages.

const (
	OwnerRunner Owner = "runner"
	OwnerTUI    Owner = "tui"
)

type Registry

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

Registry holds discovered workspace commands, keyed for hyphen/underscore- and case-insensitive lookup. A nil *Registry is usable (empty). First occurrence of a key wins, so callers should pass commands in precedence order.

func New

func New(cmds []Command) *Registry

New builds a Registry from the given commands (deduplicated by canonical key; first occurrence wins).

func (*Registry) Len

func (r *Registry) Len() int

Len reports the number of registered commands.

func (*Registry) List

func (r *Registry) List() []Command

List returns all commands sorted by name.

func (*Registry) Lookup

func (r *Registry) Lookup(name string) (Command, bool)

Lookup resolves a command by name (hyphen/underscore- and case-insensitive).

type Surface added in v0.0.2

type Surface string

Surface identifies a command presentation or dispatch surface.

const (
	SurfaceRunner Surface = "runner"
	SurfaceTUI    Surface = "tui"
)

Jump to

Keyboard shortcuts

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