autosnippet
Version:
Extract code patterns into a knowledge base for AI coding assistants
114 lines (113 loc) • 3.69 kB
TypeScript
/**
* PipelineStrategy — 顺序多阶段执行策略
*
* 从 strategies.js 提取的独立模块。
* 每个阶段可以有不同的 Capability 和 Budget,
* 阶段间可插入质量门控 (Quality Gate)。
*
* 等价于 Anthropic 的 "Prompt Chaining" + "Evaluator-Optimizer"。
*
* 增强特性 (v3):
* - Gate 支持自定义 evaluator 函数 (三态: pass/retry/degrade)
* - Gate retry: 失败时回退重新执行前一阶段
* - Stage 支持 promptBuilder(context), systemPrompt, onToolCall
* - Per-stage 硬超时保护
* - 阶段隔离 (ContextWindow/ExplorationTracker 状态)
*
* @module PipelineStrategy
*/
import type { AgentMessage } from './AgentMessage.js';
import type { PipelineType } from './context/exploration/ExplorationStrategies.js';
import { Strategy } from './strategies.js';
/** Extended runtime — may carry an optional logger (AgentRuntime provides one) */
interface PipelineRuntime {
id: string;
reactLoop(prompt: string, opts?: Record<string, unknown>): Promise<StageResult>;
logger?: {
info?: (...args: unknown[]) => void;
};
}
/** Result of a single stage execution */
interface StageResult {
reply: string;
toolCalls: Array<Record<string, unknown>>;
tokenUsage: {
input: number;
output: number;
};
iterations: number;
timedOut?: boolean;
[key: string]: unknown;
}
/** Budget configuration for a pipeline stage */
interface StageBudget {
maxIterations?: number;
timeoutMs?: number;
[key: string]: unknown;
}
/** Capability reference: plain string name or object with a name property */
type CapabilityRef = string | {
name: string;
[key: string]: unknown;
};
/** Quality Gate configuration */
interface GateConfig {
evaluator?: (source: unknown, phaseResults: Record<string, unknown>, strategyContext: Record<string, unknown>) => {
action?: string;
pass?: boolean;
reason?: string;
artifact?: unknown;
};
maxRetries?: number;
minEvidenceLength?: number;
minFileRefs?: number;
minToolCalls?: number;
custom?: (source: Record<string, unknown>) => {
pass: boolean;
reason?: string;
};
[key: string]: unknown;
}
/** Pipeline stage definition */
interface PipelineStage {
name: string;
gate?: GateConfig;
capabilities?: CapabilityRef[];
promptBuilder?: (ctx: Record<string, unknown>) => Promise<string> | string;
retryPromptBuilder?: (retryCtx: {
reason?: string;
artifact?: unknown;
}, content: string, phaseResults: Record<string, unknown>) => string;
promptTransform?: (content: string, phaseResults: Record<string, unknown>) => string;
systemPrompt?: string;
onToolCall?: (...args: unknown[]) => unknown;
budget?: StageBudget;
retryBudget?: StageBudget;
skipOnDegrade?: boolean;
skipOnFail?: boolean;
submitToolName?: string;
/** 管线类型标识 — 传递至 ExplorationTracker 用于统一场景判别 */
pipelineType?: PipelineType;
source?: string;
[key: string]: unknown;
}
export declare class PipelineStrategy extends Strategy {
#private;
constructor({ stages, maxRetries, }?: {
stages?: PipelineStage[];
maxRetries?: number;
});
get name(): string;
execute(runtime: PipelineRuntime, message: AgentMessage, opts?: Record<string, unknown>): Promise<{
reply: string;
toolCalls: Record<string, unknown>[];
tokenUsage: {
input: number;
output: number;
};
iterations: number;
phases: Record<string, unknown>;
degraded: boolean;
}>;
}
export {};