autosnippet
Version:
Extract code patterns into a knowledge base for AI coding assistants
188 lines (187 loc) • 6.56 kB
TypeScript
/**
* Capabilities — 可组合的 Agent 技能模块
*
* 核心思想: Agent 的能力不由"类型"决定,而由加载了哪些 Capability 模块决定。
*
* 每个 Capability 提供:
* 1. promptFragment — 系统提示词片段 (告诉 LLM 它能做什么)
* 2. tools — 工具白名单 (该能力需要哪些工具)
* 3. hooks — 生命周期钩子 (可选的前/后处理)
*
* 组合示例:
* - 用户聊天 = Conversation + CodeAnalysis
* - 冷启动分析 = CodeAnalysis + KnowledgeProduction
* - 飞书远程执行 = Conversation + SystemInteraction
* - 智能全能 = Conversation + CodeAnalysis + KnowledgeProduction + SystemInteraction
*
* 这就是为什么"飞书聊天"和"前端聊天"是同一个概念:
* 它们加载相同的 Capability,只是到达方式 (Transport) 不同。
*
* @module capabilities
*/
/** Context input for buildContext method */
interface ContextInput {
projectBriefing?: string | null;
memoryMode?: string;
[]: unknown;
}
/** Step result from ReAct loop */
interface StepResult {
toolCalls?: Array<{
tool: string;
args: unknown;
result: unknown;
}>;
[]: unknown;
}
/** Memory coordinator interface (subset used by Conversation) */
interface MemoryCoordinator {
buildPromptInjection(mode: string): string | null;
cacheToolResult?(tool: string, args: unknown, result: unknown): void;
}
/** Conversation capability constructor options */
interface ConversationOpts {
memoryCoordinator?: MemoryCoordinator | null;
soulPath?: string;
projectBriefing?: string | null;
}
/** SystemInteraction capability constructor options */
interface SystemInteractionOpts {
projectRoot?: string;
}
/** Capability 基类 — 所有技能模块的抽象接口 */
export declare class Capability {
/** 能力名称 */
get name(): string;
/** 系统提示词片段 */
get promptFragment(): string;
/** 工具白名单 */
get tools(): string[];
/** 构建 prompt 时调用,可注入动态上下文 */
buildContext(_context: unknown): string | null;
/** 每轮 ReAct 步骤前的钩子 */
onBeforeStep(_stepState: unknown): void;
/** 每轮 ReAct 步骤后的钩子 */
onAfterStep(_stepResult: unknown): void;
}
/**
* 对话能力: 多轮问答、知识检索、记忆管理
*
* 核心工具: 知识库搜索、语义搜索
* 用于: 用户聊天、飞书聊天、任何需要对话的场景
*/
export declare class Conversation extends Capability {
/**
* @param [opts.memoryCoordinator] MemoryCoordinator 实例
* @param [opts.soulPath] SOUL.md 路径
* @param [opts.projectBriefing] 项目概况文本
*/
constructor(opts?: ConversationOpts);
get name(): string;
get promptFragment(): string;
get tools(): string[];
buildContext(context: ContextInput): string | null;
onAfterStep(stepResult: StepResult): void;
}
/**
* 代码分析能力: AST 解析、代码搜索、结构理解
*
* 核心工具: AST 工具集 + 文件读取 + 代码搜索
* 用于: 用户聊天中的代码问题、冷启动分析、目标扫描
*/
export declare class CodeAnalysis extends Capability {
get name(): string;
get promptFragment(): string;
get tools(): string[];
}
/**
* 知识生产能力: 将分析结果转化为结构化知识候选
*
* 核心工具: 知识提交 + Guard 检查
* 用于: 冷启动提交、扫描后提交、用户主动创建知识
*/
export declare class KnowledgeProduction extends Capability {
get name(): string;
get promptFragment(): string;
get tools(): string[];
}
/**
* 系统交互能力: 终端命令执行、文件写入、环境探测、项目探索
*
* 核心工具: 终端执行 + 文件写入 + 环境信息 + 项目读取
* 用于: 飞书远程执行、自动化脚本、任何需要操作本地系统的场景
*
* ⚙️ 安全设计 (3 层防护):
* 1. 工具层: run_safe_command / write_project_file 内置硬编码黑名单
* 2. Policy 层: SafetyPolicy.checkCommand() / checkFilePath() 动态拦截
* 3. Runtime 层: reactLoop 工具执行前自动调用 PolicyEngine.validateToolCall()
*
* ⚠️ 该能力通常搭配 SafetyPolicy 使用
*/
export declare class SystemInteraction extends Capability {
/** @param [opts.projectRoot] 项目根目录 (限制操作范围) */
constructor(opts?: SystemInteractionOpts);
get name(): string;
get promptFragment(): string;
get tools(): string[];
}
/**
* 扫描知识生产能力: 将分析结果转化为标准 Recipe
*
* 与冷启动 KnowledgeProduction 的区别:
* - 使用 collect_scan_recipe 工具(内存收集,不入库)
* - 冷启动用 submit_knowledge(直接入库)
* - 字段 schema 完全一致 — 产出质量相同
*
* 用于: scanKnowledge produce 阶段
*/
export declare class ScanProduction extends Capability {
get name(): string;
get promptFragment(): string;
get tools(): string[];
}
/**
* Evolution Analysis — 现有 Recipe 进化决策能力
*
* 用于: evolution preset 的 evolve 阶段
*/
export declare class EvolutionAnalysis extends Capability {
get name(): string;
get promptFragment(): string;
get tools(): string[];
}
/**
* 所有内置 Capability 的注册表
*
* 用于按名称查找和实例化:
* const cap = CapabilityRegistry.create('conversation', { memoryCoordinator });
*/
export declare const CapabilityRegistry: {
_registry: Map<string, typeof Capability>;
/** 按名称创建 Capability 实例 */
create(name: string, opts?: Record<string, unknown>): Capability;
/** 注册自定义 Capability */
register(name: string, cls: typeof Capability): void;
/** 所有注册名 */
readonly names: string[];
};
declare const _default: {
Capability: typeof Capability;
Conversation: typeof Conversation;
CodeAnalysis: typeof CodeAnalysis;
KnowledgeProduction: typeof KnowledgeProduction;
SystemInteraction: typeof SystemInteraction;
EvolutionAnalysis: typeof EvolutionAnalysis;
CapabilityRegistry: {
_registry: Map<string, typeof Capability>;
/** 按名称创建 Capability 实例 */
create(name: string, opts?: Record<string, unknown>): Capability;
/** 注册自定义 Capability */
register(name: string, cls: typeof Capability): void;
/** 所有注册名 */
readonly names: string[];
};
};
export default _default;