Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | 25x 25x 18x 18x 8x 7x 1x 5x 2x 2x 2x 2x 4x 2x 2x 2x 2x 2x 1x 1x 1x 1x 3x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 7x 7x 20x 20x 5x 15x 20x 3x 1x 2x 2x 2x 2x 2x 9x 2x 2x 11x 4x 3x 3x 3x 3x 2x 2x 3x 3x 2x 2x 2x 2x 2x 2x 3x 1x 1x | /**
* Pipeline - 责任链模式实现
*
* 构建可组合的分析处理管道
* 灵感来源:meta-skill-weaver 状态持久化系统
*
* 设计模式:责任链模式 (Chain of Responsibility)
* - 将处理逻辑拆分为独立的处理器
* - 处理器可以动态组合
* - 支持中途退出和错误处理
*/
// ============================================================
// 处理器基类
// ============================================================
/**
* 处理器基类
* 所有处理器必须继承此类
*/
class Processor {
constructor(name) {
this.name = name;
this.next = null;
}
/**
* 设置下一个处理器
* @param {Processor} processor - 下一个处理器
* @returns {Processor} 当前处理器(支持链式调用)
*/
setNext(processor) {
this.next = processor;
return processor;
}
/**
* 处理请求
* @param {Object} context - 处理上下文
* @returns {Object} 处理结果
*/
handle(context) {
throw new Error('子类必须实现 handle 方法');
}
/**
* 传递给下一个处理器
* @param {Object} context - 处理上下文
* @returns {Object} 最终结果
*/
passToNext(context) {
if (this.next) {
return this.next.handle(context);
}
return context;
}
}
// ============================================================
// 具体处理器实现
// ============================================================
/**
* 问题接收处理器
*/
class ProblemReceiveProcessor extends Processor {
constructor() {
super('problem_receive');
}
handle(context) {
console.log(`[Pipeline] 处理器 ${this.name}: 接收问题`);
context.stage = 'problem_receive';
context.receivedAt = new Date().toISOString();
return this.passToNext(context);
}
}
/**
* 假设识别处理器
*/
class AssumptionIdentifyProcessor extends Processor {
constructor() {
super('assumption_identify');
}
handle(context) {
console.log(`[Pipeline] 处理器 ${this.name}: 识别假设`);
context.stage = 'assumption_identify';
context.assumptions = context.assumptions || [];
return this.passToNext(context);
}
}
/**
* 假设质疑处理器
*/
class AssumptionQuestionProcessor extends Processor {
constructor() {
super('assumption_question');
}
handle(context) {
console.log(`[Pipeline] 处理器 ${this.name}: 质疑假设`);
context.stage = 'assumption_question';
context.questionedAssumptions = context.assumptions?.map(a => ({
...a,
questioned: true,
questionedAt: new Date().toISOString()
})) || [];
return this.passToNext(context);
}
}
/**
* 问题分解处理器
*/
class DecompositionProcessor extends Processor {
constructor() {
super('decomposition');
}
handle(context) {
console.log(`[Pipeline] 处理器 ${this.name}: 分解问题`);
context.stage = 'decomposition';
context.layers = context.layers || [];
return this.passToNext(context);
}
}
/**
* 真理验证处理器
*/
class TruthVerifyProcessor extends Processor {
constructor() {
super('truth_verify');
}
handle(context) {
console.log(`[Pipeline] 处理器 ${this.name}: 验证真理`);
context.stage = 'truth_verify';
context.basicTruths = context.basicTruths || [];
context.verifiedAt = new Date().toISOString();
return this.passToNext(context);
}
}
/**
* 方案重构处理器
*/
class ReconstructProcessor extends Processor {
constructor() {
super('reconstruct');
}
handle(context) {
console.log(`[Pipeline] 处理器 ${this.name}: 重构方案`);
context.stage = 'reconstruct';
context.solution = context.solution || {};
context.reconstructedAt = new Date().toISOString();
return this.passToNext(context);
}
}
/**
* 报告生成处理器
*/
class ReportGenerateProcessor extends Processor {
constructor() {
super('report_generate');
}
handle(context) {
console.log(`[Pipeline] 处理器 ${this.name}: 生成报告`);
context.stage = 'report_generate';
context.report = context.report || { content: '', format: 'markdown' };
context.generatedAt = new Date().toISOString();
return context; // 最后一个处理器,不传递
}
}
// ============================================================
// 管道构建器
// ============================================================
/**
* 分析管道
* 管理处理器链的执行
*/
class AnalysisPipeline {
constructor() {
this.processors = [];
this.head = null;
}
/**
* 添加处理器
* @param {Processor} processor - 处理器实例
* @returns {AnalysisPipeline} 当前管道(支持链式调用)
*/
add(processor) {
this.processors.push(processor);
if (this.processors.length === 1) {
this.head = processor;
} else {
this.processors[this.processors.length - 2].setNext(processor);
}
return this;
}
/**
* 执行管道
* @param {Object} initialContext - 初始上下文
* @returns {Object} 最终结果
*/
execute(initialContext) {
if (!this.head) {
throw new Error('管道为空,无法执行');
}
console.log('[Pipeline] 开始执行分析管道');
const context = {
...initialContext,
pipelineStartedAt: new Date().toISOString(),
processorsExecuted: []
};
try {
const result = this.head.handle(context);
result.pipelineCompletedAt = new Date().toISOString();
result.processorsExecuted = this.processors.map(p => p.name);
console.log('[Pipeline] 分析管道执行完成');
return result;
} catch (error) {
console.error('[Pipeline] 管道执行失败:', error);
throw error;
}
}
/**
* 获取处理器列表
* @returns {Array<string>} 处理器名称列表
*/
getProcessorNames() {
return this.processors.map(p => p.name);
}
/**
* 重置管道
*/
reset() {
this.processors = [];
this.head = null;
}
}
/**
* 管道构建器
* 提供便捷的管道创建方法
*/
class PipelineBuilder {
constructor() {
this.pipeline = new AnalysisPipeline();
}
/**
* 添加问题接收处理器
* @returns {PipelineBuilder} 当前构建器
*/
withProblemReceive() {
this.pipeline.add(new ProblemReceiveProcessor());
return this;
}
/**
* 添加假设识别处理器
* @returns {PipelineBuilder} 当前构建器
*/
withAssumptionIdentify() {
this.pipeline.add(new AssumptionIdentifyProcessor());
return this;
}
/**
* 添加假设质疑处理器
* @returns {PipelineBuilder} 当前构建器
*/
withAssumptionQuestion() {
this.pipeline.add(new AssumptionQuestionProcessor());
return this;
}
/**
* 添加问题分解处理器
* @returns {PipelineBuilder} 当前构建器
*/
withDecomposition() {
this.pipeline.add(new DecompositionProcessor());
return this;
}
/**
* 添加真理验证处理器
* @returns {PipelineBuilder} 当前构建器
*/
withTruthVerify() {
this.pipeline.add(new TruthVerifyProcessor());
return this;
}
/**
* 添加方案重构处理器
* @returns {PipelineBuilder} 当前构建器
*/
withReconstruct() {
this.pipeline.add(new ReconstructProcessor());
return this;
}
/**
* 添加报告生成处理器
* @returns {PipelineBuilder} 当前构建器
*/
withReportGenerate() {
this.pipeline.add(new ReportGenerateProcessor());
return this;
}
/**
* 构建完整管道
* @returns {AnalysisPipeline} 构建的管道
*/
build() {
return this.pipeline;
}
/**
* 构建标准分析管道(包含所有处理器)
* @returns {AnalysisPipeline} 标准管道
*/
buildStandard() {
return new PipelineBuilder()
.withProblemReceive()
.withAssumptionIdentify()
.withAssumptionQuestion()
.withDecomposition()
.withTruthVerify()
.withReconstruct()
.withReportGenerate()
.build();
}
}
module.exports = {
Processor,
ProblemReceiveProcessor,
AssumptionIdentifyProcessor,
AssumptionQuestionProcessor,
DecompositionProcessor,
TruthVerifyProcessor,
ReconstructProcessor,
ReportGenerateProcessor,
AnalysisPipeline,
PipelineBuilder
};
|