Skip to content

Extension 系统

Spaceflow 的所有功能命令都以 Extension 形式组织。Extension 分为内置命令外部扩展两类,但都使用相同的 defineExtension() API 定义。

Extension 类型

内置命令

内置在 @spaceflow/core 中,无需安装即可使用:

命令说明
install安装 Extension
uninstall卸载 Extension
update更新 Extension
build构建 Extension
dev开发模式
create创建 Extension 模板
list列出已安装 Extension
clear清理缓存
commitAI 智能提交
setup初始化配置
schema生成 JSON Schema
mcpMCP 服务
runx运行外部命令

外部扩展

独立的 npm 包,需要通过 spaceflow install 安装:

Extension包名说明
review@spaceflow/reviewAI 代码审查
publish@spaceflow/publish版本发布
scripts@spaceflow/scripts自定义脚本执行
shell@spaceflow/shellShell 命令执行
review-summary@spaceflow/review-summary审查总结

Extension 生命周期

text
install → 注册到配置文件 → CLI 启动时 dynamic import → ExtensionLoader 注册 → Commander 构建 → 可用
  1. 安装spaceflow install <package> 下载依赖并注册
  2. 注册 — 写入配置文件的 dependencies 字段
  3. 加载 — CLI 壳子生成 .spaceflow/bin/index.js,动态 import 所有扩展
  4. 注册ExtensionLoader 接收 ExtensionDefinition,注册命令和服务
  5. 构建exec() 函数将所有命令添加到 Commander.js 程序
  6. 执行 — 用户通过 spaceflow <command> 调用

定义 Extension

使用 defineExtension() 函数定义扩展,这是一个纯函数式 API:

typescript
import { defineExtension } from "@spaceflow/core";

export default defineExtension({
  name: "hello",
  version: "1.0.0",
  description: "示例扩展",
  configKey: "hello",
  commands: [
    {
      name: "hello",
      description: "打招呼命令",
      arguments: "[name]",
      options: [
        {
          flags: "-g, --greeting <greeting>",
          description: "自定义问候语",
          default: "Hello",
        },
      ],
      run: async (args, options, ctx) => {
        const name = args[0] || "World";
        ctx.output.info(`${options.greeting}, ${name}!`);
      },
    },
  ],
});

ExtensionDefinition

字段类型必填说明
namestring扩展名称
commandsCommandDefinition[]命令列表
versionstring版本号
descriptionstring扩展描述
configKeystring对应配置文件中的配置路径
configSchema() => ZodSchema配置 Schema 工厂函数
configDependenciesstring[]依赖的其他扩展配置 key
toolsMcpToolDefinition[]MCP 工具列表
resourcesMcpResourceDefinition[]MCP 资源列表
servicesServiceDefinition[]服务定义列表
onInit(ctx) => Promise初始化钩子(所有服务注册完毕后调用)
onDestroy(ctx) => Promise销毁钩子(CLI 退出前调用)

CommandDefinition

字段类型必填说明
namestring命令名称
descriptionstring命令描述
run(args, opts, ctx) => Promise执行函数
aliasesstring[]命令别名
argumentsstring位置参数,如 "<name>""[file]"
argsDescriptionRecord<string, string>参数描述映射
optionsOptionDefinition[]命令选项
subcommandsCommandDefinition[]子命令

SpaceflowContext

命令的 run 函数接收 SpaceflowContext 作为第三个参数,提供运行时服务访问:

typescript
interface SpaceflowContext {
  readonly cwd: string; // 当前工作目录(优先 SPACEFLOW_CWD 环境变量)
  readonly config: IConfigReader; // 配置读取
  readonly output: IOutputService; // 输出服务(info/success/warn/error/debug)
  readonly storage: IStorageService; // 存储服务
  getService<T>(key: string): T; // 获取已注册的服务
  hasService(key: string): boolean; // 检查服务是否存在
  registerService(key: string, service: unknown): void; // 注册服务
}

package.json 中的 spaceflow 配置

Extension 的 package.json 中通过 spaceflow 字段声明导出类型:

json
{
  "spaceflow": {
    "type": "flow",
    "entry": "."
  }
}

多导出格式:

json
{
  "spaceflow": {
    "exports": {
      "review": { "type": "flow", "entry": "." },
      "review-rules": { "type": "skill", "entry": "./skills" },
      "review-mcp": { "type": "mcp", "entry": "." }
    }
  }
}

导出类型:

类型说明安装行为
flowCLI 子命令(默认)注册为 spaceflow <command>
skill技能文件复制到编辑器的 skills/ 目录
command编辑器命令生成 .md 文件到编辑器的 commands/ 目录
mcpMCP 工具spaceflow mcp 统一聚合

管理命令

bash
# 安装
spaceflow install @spaceflow/review

# 卸载
spaceflow uninstall @spaceflow/review

# 更新
spaceflow update

# 列出
spaceflow list

开发 Extension

请参考 扩展开发指南 了解如何创建自定义 Extension。

基于 MIT 许可发布