UNPKG

autosnippet

Version:

Extract code patterns into a knowledge base for AI coding assistants

124 lines (123 loc) 4.87 kB
/** * SignalCollector — AI 驱动的后台行为分析与 Skill 推荐引擎 * * 在 `asd ui` 运行时作为后台守护进程运行,周期性收集多维度信号并 * 通过 AgentFactory(统一 Agent 系统)进行深度分析,生成 Skill 推荐。 * * 三种工作模式: * - off — 不收集,不推荐 * - suggest — 收集信号 → AI 分析 → 推送推荐(默认) * - auto — 收集信号 → AI 分析 → 推送推荐 + AI 自动创建 Skill * * 核心架构: * 每次 tick → 收集 6 维度信号 → 构造分析 prompt → AgentFactory.createChat() * → Agent 执行(可调用 suggest_skills / create_skill 等工具) * → 解析 AI 响应(suggestions + nextIntervalMinutes + summary) * → 推送建议 → 动态调整下次执行间隔 * * 6 大信号维度: * 1. Guard 冲突信号 — 当前错误/冲突检测 * 2. 对话记忆信号 — 用户近期对话主题 * 3. Recipe 健康信号 — 模板使用情况与质量 * 4. Candidate 堆积信号 — 待处理候选 Skill 分析 * 5. 操作日志信号 — 近期用户操作模式 * 6. 代码变更信号 — 项目 git diff 分析 * * 设计原则: * 1. 静默 — 不打断用户,后台运行,所有错误降级 * 2. 增量 — 只分析上次快照以来的新数据 * 3. 去重 — 同一推荐仅推送一次 * 4. AI 驱动 — 所有分析决策由 AgentRuntime 完成 * 5. 自适应 — AI 根据信号密度动态调整执行频率 * * 前提条件: * 需要可用的 AI Provider * * 生命周期: * new SignalCollector(opts) → instance.start() → ... → instance.stop() */ import type { AuditRepositoryImpl } from '../../repository/audit/AuditRepository.js'; import type { KnowledgeRepositoryImpl } from '../../repository/knowledge/KnowledgeRepository.impl.js'; interface SignalCollectorOpts { projectRoot: string; knowledgeRepo?: KnowledgeRepositoryImpl | null; auditRepo?: AuditRepositoryImpl | null; agentFactory?: AgentFactoryLike | null; container?: ContainerLike | null; signalBus?: import('../../infrastructure/signal/SignalBus.js').SignalBus | null; mode?: string; intervalMs?: number; onSuggestions?: ((suggestions: Record<string, unknown>[]) => void) | null; } interface AgentResult { reply?: string; text?: string; toolCalls?: Array<{ tool: string; params?: Record<string, unknown>; }>; [key: string]: unknown; } interface AgentFactoryLike { createChat(opts: Record<string, unknown>): { execute(msg: unknown): Promise<AgentResult>; }; } interface ContainerService { name?: string; extractJSON?: (text: string, open: string, close: string) => Record<string, unknown> | null; [key: string]: unknown; } interface ContainerLike { get(name: string): ContainerService | null; singletons?: Record<string, unknown>; } export declare class SignalCollector { #private; /** * @param opts.projectRoot 用户项目根目录 * @param [opts.database] better-sqlite3 实例 * @param [opts.agentFactory] AgentFactory 实例 * @param [opts.container] ServiceContainer 实例 * @param [opts.signalBus] SignalBus 实例(实时信号订阅) * @param [opts.mode] 'off' | 'suggest' | 'auto' * @param [opts.intervalMs] 初始收集间隔(毫秒),后续由 AI 动态调整 * @param [opts.onSuggestions] 新建议回调 (suggestions[]) => void */ constructor({ projectRoot, knowledgeRepo, auditRepo, agentFactory, container, signalBus, mode, intervalMs, onSuggestions, }: SignalCollectorOpts); start(): void; stop(): void; /** * 外部事件推送入口(由 FileWatcher / Guard / CLI 等调用) * * 事件会经过 EventAggregator 聚合后触发提前分析。 * @param key 事件类型(如 'file_change', 'guard_violation', 'candidate_submit') * @param event 事件数据 */ pushEvent(key: string, event: Record<string, unknown>): void; collect(): Promise<{ suggestions: Record<string, unknown>[]; stats: Record<string, unknown> | null; } | { suggestions: any; stats: Record<string, unknown>; } | null>; getSnapshot(): { lastRun: string | null; totalRuns: number; pushedNames: string[]; lastResult: Record<string, unknown> | null; lastAiSummary: string; autoCreated: Array<{ name: string; createdAt: string; }>; pendingSuggestions: Record<string, unknown>[]; }; getMode(): string; /** 从 pendingSuggestions 中移除已创建的 Skill */ removePendingSuggestion(name: string): void; setMode(mode: string): void; resetPushed(): void; } export default SignalCollector;