autosnippet
Version:
Extract code patterns into a knowledge base for AI coding assistants
62 lines (61 loc) • 1.75 kB
JavaScript
export class Reasoning {
alternatives;
confidence;
qualitySignals;
sources;
whyStandard;
constructor(props = {}) {
/** 为什么遵循标准 */
this.whyStandard = props.whyStandard ?? '';
/** 来源列表 */
this.sources = props.sources || [];
/** 置信度 0-1 */
this.confidence = props.confidence ?? 0.7;
/** 质量信号 */
this.qualitySignals = props.qualitySignals ?? {};
/** 备选方案 */
this.alternatives = props.alternatives || [];
}
/** 从任意输入构造 Reasoning */
static from(input) {
if (input instanceof Reasoning) {
return input;
}
if (!input) {
return new Reasoning();
}
if (typeof input === 'string') {
try {
input = JSON.parse(input);
}
catch {
return new Reasoning();
}
}
return new Reasoning(input);
}
/** 验证推理信息的完整性 */
isValid() {
return !!(this.whyStandard?.trim() &&
Array.isArray(this.sources) &&
this.sources.length > 0 &&
typeof this.confidence === 'number' &&
this.confidence >= 0 &&
this.confidence <= 1);
}
/** 转换为 JSON */
toJSON() {
return {
whyStandard: this.whyStandard,
sources: this.sources,
confidence: this.confidence,
qualitySignals: this.qualitySignals,
alternatives: this.alternatives,
};
}
/** 从 wire format 创建 */
static fromJSON(data) {
return new Reasoning(data);
}
}
export default Reasoning;