All files / src/rules/converters dedup-prompt.ts

100% Statements 118/118
100% Branches 1/1
100% Functions 1/1
100% Lines 118/118

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 1192x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x  
import type { CustomRule } from '../../types';
import { getLanguage, type Language } from '../../i18n/messages';
import { buildKnownRulesSection } from './known-rules';
 
export function buildDedupOnlyPrompt(
  yamlContent: string,
  existingRules: CustomRule[],
): { system: string; user: string } {
  const lang = getLanguage();
  const s = PROMPTS[lang];
 
  const system = [
    s.role,
    s.taskTitle,
    s.taskLines.join('\n'),
    s.rulesTitle,
    s.rulesLines.join('\n'),
    s.constraintTitle,
    s.constraintLines.join('\n'),
    buildKnownRulesSection(existingRules),
  ].join('\n\n');
 
  const user = s.userPrefix + '\n\n' + yamlContent;
  return { system, user };
}
 
const PROMPTS: Record<Language, {
  role: string;
  taskTitle: string;
  taskLines: string[];
  rulesTitle: string;
  rulesLines: string[];
  constraintTitle: string;
  constraintLines: string[];
  userPrefix: string;
}> = {
  'zh-CN': {
    role: '你是规则去重判定助手。你只输出 YAML,不输出任何解释。',
    taskTitle: '## 任务',
    taskLines: [
      '下面是已标准化的规则 YAML。你只负责对照"内置静态分析规则与已导入的自定义规则"为每条规则标注去重字段。',
      '为每条规则补充以下字段(如果无重复则标注 none):',
      '- duplicateOf: 重复的规则 ID(如 eslint/no-console、custom/my-rule)',
      '- duplicateLevel: exact(完全相同)/ overlap(部分重叠)/ none(无重复)',
      '- duplicateReason: 仅 overlap 时必填,简要说明重叠原因',
    ],
    rulesTitle: '## 判定规则',
    rulesLines: [
      '1. exact: id 完全相同,或 description + message 语义完全一致',
      '2. overlap: 检测目标/场景部分重叠,但并非完全相同',
      '3. none: 与现有规则无冲突',
    ],
    constraintTitle: '## ⚠️ 严格约束(必须遵守)',
    constraintLines: [
      '1. 严禁修改任何已有字段的值(id、severity、description、message、languages、excludeLanguages)',
      '2. 严禁添加新规则,严禁删除或合并规则',
      '3. 规则数量必须与输入完全一致,顺序必须与输入完全一致',
      '4. 你只允许添加三个字段: duplicateOf、duplicateLevel、duplicateReason',
      '5. 如果某条规则与现有规则无任何重复,设置 duplicateLevel: none 即可,不需要补充 duplicateOf',
      '6. 输出纯 YAML,不要用 markdown 代码块包裹',
    ],
    userPrefix: '## 待去重的规则 YAML',
  },
  'en': {
    role: 'You are a rule deduplication assistant. Output YAML only, no explanations.',
    taskTitle: '## Task',
    taskLines: [
      'Below is standardized rule YAML. Only annotate dedup fields by comparing against the "built-in static analysis rules and existing custom rules".',
      'For each rule, add (mark none if no conflict):',
      '- duplicateOf: duplicated rule ID (e.g. eslint/no-console, custom/my-rule)',
      '- duplicateLevel: exact / overlap / none',
      '- duplicateReason: required only for overlap',
    ],
    rulesTitle: '## Judgement Rules',
    rulesLines: [
      '1. exact: identical id, or semantically identical description+message',
      '2. overlap: partially overlapping target/scenario',
      '3. none: no conflict with existing rules',
    ],
    constraintTitle: '## ⚠️ Strict Constraints (MUST follow)',
    constraintLines: [
      '1. Do NOT modify any existing field values (id, severity, description, message, languages, excludeLanguages)',
      '2. Do NOT add, delete, or merge rules',
      '3. Rule count and order must exactly match the input',
      '4. Only add three fields: duplicateOf, duplicateLevel, duplicateReason',
      '5. If a rule has no duplication, set duplicateLevel: none without duplicateOf',
      '6. Output pure YAML, do NOT wrap in markdown code fences',
    ],
    userPrefix: '## YAML to deduplicate',
  },
  'ja': {
    role: 'あなたはルール重複判定アシスタントです。YAML のみ出力し、説明は不要です。',
    taskTitle: '## タスク',
    taskLines: [
      '以下は標準化されたルール YAML です。組み込みの静的解析ルールと既存カスタムルールに照合し、重複フィールドのみ注釈してください。',
      '各ルールに以下を追加(重複がない場合は none と表記):',
      '- duplicateOf: 重複ルール ID(例: eslint/no-console, custom/my-rule)',
      '- duplicateLevel: exact / overlap / none',
      '- duplicateReason: overlap 時のみ必須',
    ],
    rulesTitle: '## 判定ルール',
    rulesLines: [
      '1. exact: ID が同一、または description+message が意味的に完全一致',
      '2. overlap: 検出対象/シナリオが部分重複',
      '3. none: 既存ルールと競合なし',
    ],
    constraintTitle: '## ⚠️ 厳格な制約(必ず遵守)',
    constraintLines: [
      '1. 既存フィールド(id, severity, description, message, languages, excludeLanguages)の値を一切変更しない',
      '2. ルールの追加、削除、統合を一切行わない',
      '3. ルールの数と順序は入力と完全に一致させる',
      '4. 追加できるフィールドは duplicateOf, duplicateLevel, duplicateReason のみ',
      '5. 重複がないルールは duplicateLevel: none とし、duplicateOf は付けない',
      '6. 純粋な YAML を出力し、markdown コードブロックで囲まない',
    ],
    userPrefix: '## 重複排除対象の YAML',
  },
};