feat: Add templates for autonomous actions, orchestrators, sequential phases, and skill documentation

- Introduced a comprehensive template for autonomous actions, detailing structure, execution, and error handling.
- Added an orchestrator template to manage state and decision logic for autonomous actions.
- Created a sequential phase template to outline execution steps and objectives for structured workflows.
- Developed a skill documentation template to standardize the generation of skill entry files.
- Implemented a Python script to compare search results between hybrid and cascade methods, analyzing ranking changes.
This commit is contained in:
catlog22
2026-01-03 15:58:31 +08:00
parent ac23fe5b5a
commit 9922d455da
17 changed files with 4625 additions and 14 deletions

View File

@@ -0,0 +1,396 @@
# Execution Modes Specification
两种 Skill 执行模式的详细规范定义。
---
## 模式概览
| 特性 | Sequential (顺序) | Autonomous (自主) |
|------|-------------------|-------------------|
| 执行顺序 | 固定(数字前缀) | 动态(编排器决策) |
| 阶段依赖 | 强依赖 | 弱依赖/无依赖 |
| 状态管理 | 隐式(阶段产出) | 显式(状态文件) |
| 适用场景 | 流水线任务 | 交互式任务 |
| 复杂度 | 低 | 中-高 |
| 可扩展性 | 插入子阶段 | 添加新动作 |
---
## Mode 1: Sequential (顺序模式)
### 定义
阶段按固定顺序线性执行,每个阶段的输出作为下一阶段的输入。
### 目录结构
```
phases/
├── 01-{first-step}.md
├── 02-{second-step}.md
├── 02.5-{sub-step}.md # 可选:子阶段
├── 03-{third-step}.md
└── ...
```
### 执行流程
```
┌─────────┐ ┌─────────┐ ┌─────────┐
│Phase 01 │────▶│Phase 02 │────▶│Phase 03 │────▶ ...
└─────────┘ └─────────┘ └─────────┘
│ │ │
▼ ▼ ▼
output1.json output2.md output3.md
```
### Phase 文件规范
```markdown
# Phase N: {阶段名称}
{一句话描述}
## Objective
{详细目标}
## Input
- 依赖: {上一阶段产出}
- 配置: {配置文件}
## Execution Steps
### Step 1: {步骤}
{执行代码或说明}
### Step 2: {步骤}
{执行代码或说明}
## Output
- **File**: `{输出文件}`
- **Format**: {JSON/Markdown}
## Next Phase
→ [Phase N+1: xxx](0N+1-xxx.md)
```
### 适用场景
- **文档生成**: 收集 → 分析 → 组装 → 优化
- **代码分析**: 扫描 → 解析 → 报告
- **数据处理**: 提取 → 转换 → 加载
### 优点
- 逻辑清晰,易于理解
- 调试简单,可逐阶段验证
- 输出可预测
### 缺点
- 灵活性低
- 难以处理分支逻辑
- 用户交互受限
---
## Mode 2: Autonomous (自主模式)
### 定义
无固定执行顺序,由编排器 (Orchestrator) 根据当前状态动态选择下一个动作。
### 目录结构
```
phases/
├── orchestrator.md # 编排器:核心决策逻辑
├── state-schema.md # 状态结构定义
└── actions/ # 独立动作(无顺序)
├── action-{a}.md
├── action-{b}.md
├── action-{c}.md
└── ...
```
### 核心组件
#### 1. Orchestrator (编排器)
```markdown
# Orchestrator
## Role
根据当前状态选择并执行下一个动作。
## State Reading
读取状态文件: `{workDir}/state.json`
## Decision Logic
```javascript
function selectNextAction(state) {
// 1. 检查终止条件
if (state.status === 'completed') return null;
if (state.error_count > MAX_RETRIES) return 'action-abort';
// 2. 根据状态选择动作
if (!state.initialized) return 'action-init';
if (state.pending_items.length > 0) return 'action-process';
if (state.needs_review) return 'action-review';
// 3. 默认动作
return 'action-complete';
}
```
## Execution Loop
```
while (true) {
state = readState();
action = selectNextAction(state);
if (!action) break;
result = executeAction(action, state);
updateState(result);
}
```
```
#### 2. State Schema (状态结构)
```markdown
# State Schema
## 状态文件
位置: `{workDir}/state.json`
## 结构定义
```typescript
interface SkillState {
// 元信息
skill_name: string;
started_at: string;
updated_at: string;
// 执行状态
status: 'pending' | 'running' | 'completed' | 'failed';
current_action: string | null;
completed_actions: string[];
// 业务数据
context: Record<string, any>;
pending_items: any[];
results: Record<string, any>;
// 错误追踪
errors: Array<{
action: string;
message: string;
timestamp: string;
}>;
error_count: number;
}
```
## 初始状态
```json
{
"skill_name": "{skill-name}",
"started_at": "{ISO8601}",
"updated_at": "{ISO8601}",
"status": "pending",
"current_action": null,
"completed_actions": [],
"context": {},
"pending_items": [],
"results": {},
"errors": [],
"error_count": 0
}
```
```
#### 3. Action (动作)
```markdown
# Action: {action-name}
## Purpose
{动作目的}
## Preconditions
- [ ] 条件1
- [ ] 条件2
## Execution
{执行逻辑}
## State Updates
```javascript
return {
completed_actions: [...state.completed_actions, 'action-name'],
results: {
...state.results,
action_name: { /* 结果 */ }
},
// 其他状态更新
};
```
## Next Actions (Hints)
- 成功时: `action-{next}`
- 失败时: `action-retry``action-abort`
```
### 执行流程
```
┌─────────────────────────────────────────────────────────────────┐
│ Orchestrator Loop │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ Read │────▶│ Select │────▶│ Execute │ │ │
│ │ │ State │ │ Action │ │ Action │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ │ │
│ │ ▲ │ │ │
│ │ │ ▼ │ │
│ │ │ ┌──────────┐ │ │
│ │ └───────────│ Update │◀────────────────────────┘ │
│ │ │ State │ │
│ │ └──────────┘ │
│ │ │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌──────────────────┐ │
│ │ Termination? │ │
│ │ - completed │ │
│ │ - max_retries │ │
│ │ - user_abort │ │
│ └──────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
```
### 动作目录 (Action Catalog)
在 `specs/action-catalog.md` 中定义:
```markdown
# Action Catalog
## Available Actions
| Action | Purpose | Preconditions | Effects |
|--------|---------|---------------|---------|
| action-init | 初始化状态 | status=pending | status=running |
| action-process | 处理待办项 | pending_items.length>0 | pending_items-- |
| action-review | 用户审核 | needs_review=true | needs_review=false |
| action-complete | 完成任务 | pending_items.length=0 | status=completed |
| action-abort | 中止任务 | error_count>MAX | status=failed |
## Action Dependencies Graph
```mermaid
graph TD
INIT[action-init] --> PROCESS[action-process]
PROCESS --> PROCESS
PROCESS --> REVIEW[action-review]
REVIEW --> PROCESS
REVIEW --> COMPLETE[action-complete]
PROCESS --> ABORT[action-abort]
```
```
### 适用场景
- **交互式任务**: 问答、对话、表单填写
- **状态机任务**: Issue 管理、工作流审批
- **探索式任务**: 调试、诊断、搜索
### 优点
- 高度灵活,适应动态需求
- 支持复杂分支逻辑
- 易于扩展新动作
### 缺点
- 复杂度高
- 状态管理开销
- 调试难度大
---
## 模式选择指南
### 决策流程
```
用户需求分析
┌────────────────────────────┐
│ 阶段间是否有强依赖关系? │
└────────────────────────────┘
├── 是 → Sequential
└── 否 → 继续判断
┌────────────────────────────┐
│ 是否需要动态响应用户意图? │
└────────────────────────────┘
├── 是 → Autonomous
└── 否 → Sequential
```
### 快速判断表
| 问题 | Sequential | Autonomous |
|------|------------|------------|
| 输出结构是否固定? | ✓ | ✗ |
| 是否需要用户多轮交互? | ✗ | ✓ |
| 阶段是否可以跳过/重复? | ✗ | ✓ |
| 是否有复杂分支逻辑? | ✗ | ✓ |
| 调试是否需要简单? | ✓ | ✗ |
---
## 混合模式
某些复杂 Skill 可能需要混合使用两种模式:
```
phases/
├── 01-init.md # Sequential: 初始化
├── 02-orchestrator.md # Autonomous: 核心交互循环
│ └── actions/
│ ├── action-a.md
│ └── action-b.md
└── 03-finalize.md # Sequential: 收尾
```
**适用场景**:
- 初始化和收尾固定,中间交互灵活
- 多阶段任务,某阶段需要动态决策

View File

@@ -0,0 +1,344 @@
# Skill Requirements Specification
新 Skill 创建的需求收集规范。
---
## 必需信息
### 1. 基本信息
| 字段 | 类型 | 必需 | 说明 |
|------|------|------|------|
| `skill_name` | string | ✓ | Skill 标识符(小写-连字符) |
| `display_name` | string | ✓ | 显示名称 |
| `description` | string | ✓ | 一句话描述 |
| `triggers` | string[] | ✓ | 触发关键词列表 |
### 2. 执行模式
| 字段 | 类型 | 必需 | 说明 |
|------|------|------|------|
| `execution_mode` | enum | ✓ | `sequential` \| `autonomous` |
| `phase_count` | number | 条件 | Sequential 模式下的阶段数 |
| `action_count` | number | 条件 | Autonomous 模式下的动作数 |
### 3. 工具依赖
| 字段 | 类型 | 必需 | 说明 |
|------|------|------|------|
| `allowed_tools` | string[] | ✓ | 允许使用的工具列表 |
| `mcp_tools` | string[] | 可选 | 需要的 MCP 工具 |
### 4. 输出配置
| 字段 | 类型 | 必需 | 说明 |
|------|------|------|------|
| `output_format` | enum | ✓ | `markdown` \| `html` \| `json` |
| `output_location` | string | ✓ | 输出目录模式 |
---
## 配置文件结构
```typescript
interface SkillConfig {
// 基本信息
skill_name: string; // "my-skill"
display_name: string; // "My Skill"
description: string; // "一句话描述"
triggers: string[]; // ["keyword1", "keyword2"]
// 执行模式
execution_mode: 'sequential' | 'autonomous';
// Sequential 模式配置
sequential_config?: {
phases: Array<{
id: string; // "01-init"
name: string; // "Initialization"
description: string; // "收集初始配置"
input: string[]; // 输入依赖
output: string; // 输出文件
}>;
};
// Autonomous 模式配置
autonomous_config?: {
state_schema: {
fields: Array<{
name: string;
type: string;
description: string;
}>;
};
actions: Array<{
id: string; // "action-init"
name: string; // "Initialize"
description: string; // "初始化状态"
preconditions: string[]; // 前置条件
effects: string[]; // 执行效果
}>;
termination_conditions: string[];
};
// 工具依赖
allowed_tools: string[]; // ["Task", "Read", "Write", ...]
mcp_tools?: string[]; // ["mcp__chrome__*"]
// 输出配置
output: {
format: 'markdown' | 'html' | 'json';
location: string; // ".workflow/.scratchpad/{skill}-{timestamp}"
filename_pattern: string; // "{name}-output.{ext}"
};
// 质量配置
quality?: {
dimensions: string[]; // ["completeness", "consistency", ...]
pass_threshold: number; // 80
};
// 元数据
created_at: string;
version: string;
}
```
---
## 需求收集问题
### Phase 1: 基本信息
```javascript
AskUserQuestion({
questions: [
{
question: "Skill 的名称是什么?(英文,小写-连字符格式)",
header: "Skill 名称",
multiSelect: false,
options: [
{ label: "自动生成", description: "根据描述自动生成名称" },
{ label: "手动输入", description: "输入自定义名称" }
]
},
{
question: "Skill 的主要用途是什么?",
header: "用途类型",
multiSelect: false,
options: [
{ label: "文档生成", description: "生成 Markdown/HTML 文档" },
{ label: "代码分析", description: "分析代码结构、质量、安全" },
{ label: "交互管理", description: "管理 Issue、任务、工作流" },
{ label: "数据处理", description: "ETL、转换、报告生成" },
{ label: "自定义", description: "其他用途" }
]
}
]
});
```
### Phase 2: 执行模式
```javascript
AskUserQuestion({
questions: [
{
question: "选择执行模式:",
header: "执行模式",
multiSelect: false,
options: [
{
label: "Sequential (顺序)",
description: "阶段按固定顺序执行,适合流水线任务(推荐)"
},
{
label: "Autonomous (自主)",
description: "动态选择执行路径,适合交互式任务"
},
{
label: "Hybrid (混合)",
description: "初始化和收尾固定,中间交互灵活"
}
]
}
]
});
```
### Phase 3: 阶段/动作定义
#### Sequential 模式
```javascript
AskUserQuestion({
questions: [
{
question: "需要多少个执行阶段?",
header: "阶段数量",
multiSelect: false,
options: [
{ label: "3 阶段", description: "简单: 收集 → 处理 → 输出" },
{ label: "5 阶段", description: "标准: 收集 → 探索 → 分析 → 组装 → 验证" },
{ label: "7 阶段", description: "完整: 包含并行处理和迭代优化" },
{ label: "自定义", description: "手动指定阶段" }
]
}
]
});
```
#### Autonomous 模式
```javascript
AskUserQuestion({
questions: [
{
question: "核心动作有哪些?",
header: "动作定义",
multiSelect: true,
options: [
{ label: "初始化 (init)", description: "设置初始状态" },
{ label: "列表 (list)", description: "显示当前项目" },
{ label: "创建 (create)", description: "创建新项目" },
{ label: "编辑 (edit)", description: "修改现有项目" },
{ label: "删除 (delete)", description: "删除项目" },
{ label: "完成 (complete)", description: "完成任务" }
]
}
]
});
```
### Phase 4: 工具依赖
```javascript
AskUserQuestion({
questions: [
{
question: "需要哪些工具?",
header: "工具选择",
multiSelect: true,
options: [
{ label: "基础工具", description: "Task, Read, Write, Glob, Grep, Bash" },
{ label: "用户交互", description: "AskUserQuestion" },
{ label: "Chrome 截图", description: "mcp__chrome__*" },
{ label: "外部搜索", description: "mcp__exa__search" }
]
}
]
});
```
---
## 验证规则
### 名称验证
```javascript
function validateSkillName(name) {
const rules = [
{ test: /^[a-z][a-z0-9-]*$/, msg: "必须以小写字母开头,只包含小写字母、数字、连字符" },
{ test: /^.{3,30}$/, msg: "长度 3-30 字符" },
{ test: /^(?!.*--)/, msg: "不能有连续连字符" },
{ test: /[^-]$/, msg: "不能以连字符结尾" }
];
for (const rule of rules) {
if (!rule.test.test(name)) {
return { valid: false, error: rule.msg };
}
}
return { valid: true };
}
```
### 配置验证
```javascript
function validateSkillConfig(config) {
const errors = [];
// 必需字段
if (!config.skill_name) errors.push("缺少 skill_name");
if (!config.description) errors.push("缺少 description");
if (!config.execution_mode) errors.push("缺少 execution_mode");
// 模式特定验证
if (config.execution_mode === 'sequential') {
if (!config.sequential_config?.phases?.length) {
errors.push("Sequential 模式需要定义 phases");
}
} else if (config.execution_mode === 'autonomous') {
if (!config.autonomous_config?.actions?.length) {
errors.push("Autonomous 模式需要定义 actions");
}
}
return { valid: errors.length === 0, errors };
}
```
---
## 示例配置
### Sequential 模式示例
```json
{
"skill_name": "api-docs-generator",
"display_name": "API Docs Generator",
"description": "Generate API documentation from source code",
"triggers": ["generate api docs", "api documentation"],
"execution_mode": "sequential",
"sequential_config": {
"phases": [
{ "id": "01-scan", "name": "Code Scanning", "output": "endpoints.json" },
{ "id": "02-parse", "name": "Schema Parsing", "output": "schemas.json" },
{ "id": "03-generate", "name": "Doc Generation", "output": "api-docs.md" }
]
},
"allowed_tools": ["Task", "Read", "Write", "Glob", "Grep", "Bash"],
"output": {
"format": "markdown",
"location": ".workflow/.scratchpad/api-docs-{timestamp}",
"filename_pattern": "{name}-api-docs.md"
}
}
```
### Autonomous 模式示例
```json
{
"skill_name": "task-manager",
"display_name": "Task Manager",
"description": "Interactive task management with CRUD operations",
"triggers": ["manage tasks", "task list", "create task"],
"execution_mode": "autonomous",
"autonomous_config": {
"state_schema": {
"fields": [
{ "name": "tasks", "type": "Task[]", "description": "任务列表" },
{ "name": "current_view", "type": "string", "description": "当前视图" }
]
},
"actions": [
{ "id": "action-list", "name": "List Tasks", "preconditions": [], "effects": ["显示任务列表"] },
{ "id": "action-create", "name": "Create Task", "preconditions": [], "effects": ["添加新任务"] },
{ "id": "action-edit", "name": "Edit Task", "preconditions": ["task_selected"], "effects": ["更新任务"] },
{ "id": "action-delete", "name": "Delete Task", "preconditions": ["task_selected"], "effects": ["删除任务"] }
],
"termination_conditions": ["user_exit", "error_limit"]
},
"allowed_tools": ["Task", "AskUserQuestion", "Read", "Write"],
"output": {
"format": "json",
"location": ".workflow/.scratchpad/tasks",
"filename_pattern": "tasks.json"
}
}
```