dislop 0.9.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +3 -3
- package/.claude-plugin/plugin.json +2 -2
- package/.codex-plugin/plugin.json +7 -8
- package/.cursor-plugin/plugin.json +2 -3
- package/.pre-commit-hooks.yaml +5 -12
- package/PRODUCT.md +19 -57
- package/README.md +360 -252
- package/README.zh-CN.md +390 -240
- package/action.yml +30 -39
- package/bin/dislop-mcp.mjs +3 -4
- package/bin/dislop.mjs +4 -4582
- package/gemini-extension.json +2 -2
- package/llms-install.md +68 -0
- package/package.json +17 -55
- package/packs/common/artifact-qa/pack.json +5 -0
- package/packs/common/artifact-qa/prompts/rules.md +11 -0
- package/packs/common/artifact-qa/scripts/index.mjs +173 -0
- package/packs/common/slide-deck/pack.json +6 -0
- package/packs/common/slide-deck/prompts/rules.md +11 -0
- package/packs/common/slide-deck/scripts/index.mjs +62 -0
- package/packs/common/web-ui/pack.json +6 -0
- package/packs/common/web-ui/prompts/rules.md +11 -0
- package/packs/common/web-ui/scripts/index.mjs +80 -0
- package/packs/en/artifact-qa/pack.json +6 -0
- package/packs/en/artifact-qa/prompts/rules.md +14 -0
- package/packs/en/artifact-qa/scripts/index.mjs +90 -0
- package/packs/en/prose/pack.json +7 -0
- package/packs/en/prose/prompts/rules.md +26 -0
- package/packs/en/prose/scripts/index.mjs +290 -0
- package/packs/en/slide-deck/pack.json +7 -0
- package/packs/en/slide-deck/prompts/rules.md +17 -0
- package/packs/en/slide-deck/scripts/index.mjs +81 -0
- package/packs/en/web-ui/pack.json +7 -0
- package/packs/en/web-ui/prompts/rules.md +20 -0
- package/packs/en/web-ui/scripts/index.mjs +103 -0
- package/packs/zh/artifact-qa/pack.json +6 -0
- package/packs/zh/artifact-qa/prompts/rules.md +14 -0
- package/packs/zh/artifact-qa/scripts/index.mjs +89 -0
- package/packs/zh/prose/pack.json +7 -0
- package/packs/zh/prose/prompts/rules.md +26 -0
- package/packs/zh/prose/scripts/index.mjs +305 -0
- package/packs/zh/slide-deck/pack.json +7 -0
- package/packs/zh/slide-deck/prompts/rules.md +17 -0
- package/packs/zh/slide-deck/scripts/index.mjs +75 -0
- package/packs/zh/web-ui/pack.json +7 -0
- package/packs/zh/web-ui/prompts/rules.md +20 -0
- package/packs/zh/web-ui/scripts/index.mjs +98 -0
- package/plugin.json +5 -6
- package/server.json +8 -2
- package/skills/deslop-docs/SKILL.md +10 -8
- package/skills/deslop-slides-docx/SKILL.md +7 -8
- package/skills/deslop-verify/SKILL.md +7 -8
- package/skills/deslop-web-ui/SKILL.md +7 -8
- package/skills/dislop/SKILL.md +16 -28
- package/skills/dislop/references/essay.md +1 -1
- package/skills/dislop/references/essay.zh-CN.md +1 -1
- package/skills/dislop/references/office.md +1 -1
- package/skills/dislop/references/office.zh-CN.md +1 -1
- package/skills/dislop/references/web-ui.md +1 -1
- package/skills/dislop/references/web-ui.zh-CN.md +1 -1
- package/skills/dislop/references/work-docs.md +1 -1
- package/skills/dislop/references/work-docs.zh-CN.md +1 -1
- package/src/cli-main.mjs +210 -0
- package/src/commands/check-fix.mjs +169 -0
- package/src/commands/doctor.mjs +110 -0
- package/src/commands/init.mjs +49 -0
- package/src/commands/login.mjs +82 -0
- package/src/commands/model.mjs +130 -0
- package/src/commands/packs.mjs +44 -0
- package/src/commands/verify.mjs +133 -0
- package/src/commands/voice.mjs +84 -0
- package/src/kernel/config.mjs +232 -0
- package/src/kernel/document-model.mjs +1412 -0
- package/src/kernel/fact-gate.mjs +615 -0
- package/src/kernel/file-discovery.mjs +139 -0
- package/src/{binary-node.mjs → kernel/ooxml-binary.mjs} +532 -893
- package/src/kernel/pack-loader.mjs +715 -0
- package/src/kernel/patcher.mjs +341 -0
- package/src/kernel/pipeline.mjs +267 -0
- package/src/kernel/prompt-assembler.mjs +307 -0
- package/src/kernel/voice-profile.mjs +177 -0
- package/src/mcp/stdio-server.mjs +372 -0
- package/src/runners/auto-fix.mjs +59 -0
- package/src/runners/interactive-tty.mjs +461 -0
- package/src/runners/reporters.mjs +256 -0
- package/src/runtime/chunker.mjs +87 -0
- package/src/runtime/json-extract.mjs +144 -0
- package/src/runtime/privacy-gate.mjs +97 -0
- package/src/runtime/providers/anthropic.mjs +65 -0
- package/src/runtime/providers/cloud.mjs +136 -0
- package/src/runtime/providers/gemini.mjs +91 -0
- package/src/runtime/providers/local-model.mjs +139 -0
- package/src/runtime/providers/openai-compat.mjs +130 -0
- package/src/runtime/router.mjs +315 -0
- package/src/sdk/index.mjs +137 -0
- package/models/manifest.json +0 -90
- package/models/pocket-slop-v1.json +0 -1
- package/packs/business-brief/pack.json +0 -937
- package/packs/core/pack.json +0 -209
- package/packs/deck-and-ui/pack.json +0 -539
- package/packs/engineering-spec/pack.json +0 -144
- package/packs/executive-deck/pack.json +0 -98
- package/packs/literary-essay/pack.json +0 -344
- package/packs/regulated-comms/pack.json +0 -99
- package/patterns/ai-tic-rubric.json +0 -252
- package/patterns/banks/composite-fusion.json +0 -48
- package/patterns/banks/discourse-rhythm.json +0 -95
- package/patterns/banks/lexical-signals.json +0 -92
- package/patterns/banks/syntactic-frames.json +0 -52
- package/patterns/banks/visual-deck.json +0 -44
- package/patterns/dimensions/artifacts.json +0 -121
- package/patterns/dimensions/discourse.json +0 -438
- package/patterns/dimensions/lexical.json +0 -198
- package/patterns/dimensions/rhythm.json +0 -258
- package/patterns/dimensions/syntactic.json +0 -285
- package/patterns/dimensions/visual-and-deck.json +0 -530
- package/patterns/taxonomy.json +0 -112
- package/src/core.mjs +0 -5497
- package/src/dislop/review-picker.vim +0 -114
- package/src/local-model.mjs +0 -997
- package/src/pattern-engine.mjs +0 -1003
- package/src/pocket-model.mjs +0 -729
- package/src/review-apply.mjs +0 -151
- package/src/review-client.mjs +0 -1073
|
@@ -0,0 +1,715 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
5
|
+
import {
|
|
6
|
+
VALID_ACTION_TYPES,
|
|
7
|
+
VALID_DEFECT_SEVERITIES,
|
|
8
|
+
VALID_FIX_SAFETIES,
|
|
9
|
+
VALID_SUGGESTION_PRIORITIES,
|
|
10
|
+
definePack,
|
|
11
|
+
definePackScripts,
|
|
12
|
+
} from "../sdk/index.mjs";
|
|
13
|
+
|
|
14
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
15
|
+
export const BUILTIN_PACKS_ROOT = path.resolve(__dirname, "..", "..", "packs");
|
|
16
|
+
|
|
17
|
+
const SHORTHAND_ALIASES = Object.freeze({
|
|
18
|
+
"prose-en": "en/prose",
|
|
19
|
+
"prose-zh": "zh/prose",
|
|
20
|
+
"artifact-qa-en": "en/artifact-qa",
|
|
21
|
+
"artifact-qa-zh": "zh/artifact-qa",
|
|
22
|
+
"web-ui-en": "en/web-ui",
|
|
23
|
+
"web-ui-zh": "zh/web-ui",
|
|
24
|
+
"slide-deck-en": "en/slide-deck",
|
|
25
|
+
"slide-deck-zh": "zh/slide-deck",
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
function parseInlineArray(raw) {
|
|
29
|
+
const inner = raw.trim().replace(/^\[/, "").replace(/\]$/, "").trim();
|
|
30
|
+
if (!inner) return [];
|
|
31
|
+
return inner
|
|
32
|
+
.split(",")
|
|
33
|
+
.map((s) => s.trim().replace(/^["']|["']$/g, ""))
|
|
34
|
+
.filter(Boolean);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function unquoteYamlScalar(raw) {
|
|
38
|
+
return String(raw || "")
|
|
39
|
+
.trim()
|
|
40
|
+
.replace(/^["']|["']$/g, "");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Parse a Markdown prompt file (`prompts/*.md` or `<name>.pack.md`) into `{ meta, roleHint, rules }`.
|
|
45
|
+
*/
|
|
46
|
+
export function parseMarkdownPromptContent(markdownText, sourcePath = "rules.md") {
|
|
47
|
+
const normalized = String(markdownText || "").replace(/\r\n/g, "\n");
|
|
48
|
+
const fmMatch = normalized.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
|
|
49
|
+
|
|
50
|
+
let fmLines = [];
|
|
51
|
+
let body = normalized.trim();
|
|
52
|
+
if (fmMatch) {
|
|
53
|
+
fmLines = fmMatch[1].split("\n");
|
|
54
|
+
body = fmMatch[2].trim();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let name = path
|
|
58
|
+
.basename(sourcePath)
|
|
59
|
+
.replace(/\.pack\.md$/i, "")
|
|
60
|
+
.replace(/\.md$/i, "");
|
|
61
|
+
let version = "0.1.0";
|
|
62
|
+
let description = "";
|
|
63
|
+
let roleHint = "";
|
|
64
|
+
let languages;
|
|
65
|
+
let formats;
|
|
66
|
+
let modalities;
|
|
67
|
+
const frontmatterRules = [];
|
|
68
|
+
|
|
69
|
+
let inRulesList = false;
|
|
70
|
+
let currentRule = null;
|
|
71
|
+
|
|
72
|
+
for (const rawLine of fmLines) {
|
|
73
|
+
const line = rawLine.replace(/#.*$/, "").trimEnd();
|
|
74
|
+
if (!line.trim()) continue;
|
|
75
|
+
|
|
76
|
+
if (inRulesList) {
|
|
77
|
+
const itemStart = line.match(/^\s*-\s+([a-zA-Z0-9_-]+)\s*:\s*(.+)$/);
|
|
78
|
+
if (itemStart) {
|
|
79
|
+
if (currentRule) frontmatterRules.push(currentRule);
|
|
80
|
+
currentRule = { [itemStart[1]]: unquoteYamlScalar(itemStart[2]) };
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const itemProp = line.match(/^\s{2,}([a-zA-Z0-9_-]+)\s*:\s*(.+)$/);
|
|
84
|
+
if (itemProp && currentRule) {
|
|
85
|
+
currentRule[itemProp[1]] = unquoteYamlScalar(itemProp[2]);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (!/^\s/.test(line)) {
|
|
89
|
+
if (currentRule) {
|
|
90
|
+
frontmatterRules.push(currentRule);
|
|
91
|
+
currentRule = null;
|
|
92
|
+
}
|
|
93
|
+
inRulesList = false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const kv = line.match(/^([a-zA-Z0-9_-]+)\s*:\s*(.*)$/);
|
|
98
|
+
if (!kv) continue;
|
|
99
|
+
const [, key, val] = kv;
|
|
100
|
+
if (key === "rules" && !val.trim()) {
|
|
101
|
+
inRulesList = true;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (key === "name" && val.trim()) name = unquoteYamlScalar(val);
|
|
105
|
+
else if (key === "version" && val.trim()) version = unquoteYamlScalar(val);
|
|
106
|
+
else if (key === "description" && val.trim()) description = unquoteYamlScalar(val);
|
|
107
|
+
else if (key === "roleHint" && val.trim()) roleHint = unquoteYamlScalar(val);
|
|
108
|
+
else if (key === "languages") languages = parseInlineArray(val);
|
|
109
|
+
else if (key === "formats") formats = parseInlineArray(val);
|
|
110
|
+
else if (key === "modalities") modalities = parseInlineArray(val);
|
|
111
|
+
}
|
|
112
|
+
if (currentRule) frontmatterRules.push(currentRule);
|
|
113
|
+
|
|
114
|
+
const bodyMap = new Map();
|
|
115
|
+
const bodyOrdered = [];
|
|
116
|
+
const ruleLineRe = /^(?:\d+\.|[-*+])\s+\*\*([a-zA-Z0-9._-]+)\*\*\s*:\s*(.+)$/gm;
|
|
117
|
+
let m;
|
|
118
|
+
while ((m = ruleLineRe.exec(body)) !== null) {
|
|
119
|
+
const id = m[1].trim();
|
|
120
|
+
const instruction = m[2].trim();
|
|
121
|
+
bodyMap.set(id, instruction);
|
|
122
|
+
bodyOrdered.push({ id, instruction });
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const compiledRules = [];
|
|
126
|
+
if (frontmatterRules.length > 0) {
|
|
127
|
+
for (const r of frontmatterRules) {
|
|
128
|
+
const ruleId = r.id || r.ruleId;
|
|
129
|
+
if (!ruleId) continue;
|
|
130
|
+
const priority = VALID_SUGGESTION_PRIORITIES.includes(r.priority) ? r.priority : "moderate";
|
|
131
|
+
const instruction = r.instruction || bodyMap.get(ruleId) || body;
|
|
132
|
+
compiledRules.push({
|
|
133
|
+
ruleId,
|
|
134
|
+
defaultPriority: priority,
|
|
135
|
+
instruction,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
} else if (bodyOrdered.length > 0) {
|
|
139
|
+
for (const r of bodyOrdered) {
|
|
140
|
+
compiledRules.push({
|
|
141
|
+
ruleId: r.id,
|
|
142
|
+
defaultPriority: "moderate",
|
|
143
|
+
instruction: r.instruction,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
} else if (body) {
|
|
147
|
+
compiledRules.push({
|
|
148
|
+
ruleId: "general",
|
|
149
|
+
defaultPriority: "moderate",
|
|
150
|
+
instruction: body,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
meta: { name, version, description, languages, formats, modalities },
|
|
156
|
+
roleHint,
|
|
157
|
+
rules: compiledRules,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Compile a standalone single-file `<name>.pack.md` into a Pack handle.
|
|
163
|
+
*/
|
|
164
|
+
export function compileMarkdownPack(markdownText, sourcePath = "custom.pack.md") {
|
|
165
|
+
const normalized = String(markdownText || "").replace(/\r\n/g, "\n");
|
|
166
|
+
if (!/^---\n[\s\S]*?\n---/.test(normalized)) {
|
|
167
|
+
throw new Error(`Invalid Markdown Pack '${sourcePath}': missing YAML frontmatter block (--- ... ---)`);
|
|
168
|
+
}
|
|
169
|
+
const parsed = parseMarkdownPromptContent(normalized, sourcePath);
|
|
170
|
+
const pack = definePack({
|
|
171
|
+
name: parsed.meta.name,
|
|
172
|
+
version: parsed.meta.version,
|
|
173
|
+
description:
|
|
174
|
+
parsed.meta.description || `Declarative Markdown Prompt Pack (${path.basename(sourcePath)})`,
|
|
175
|
+
...(parsed.meta.languages ? { languages: parsed.meta.languages } : {}),
|
|
176
|
+
...(parsed.meta.formats ? { formats: parsed.meta.formats } : {}),
|
|
177
|
+
...(parsed.meta.modalities ? { modalities: parsed.meta.modalities } : {}),
|
|
178
|
+
resolvePrompt(doc) {
|
|
179
|
+
return {
|
|
180
|
+
...(parsed.roleHint ? { roleHint: interpolateRoleHint(parsed.roleHint, doc) } : {}),
|
|
181
|
+
rules: parsed.rules,
|
|
182
|
+
};
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
return wrapEagerPackAsLazyHandle(pack);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Interpolate lightweight document telemetry placeholders (`{{uiSummary}}`, `{{deckSummary}}`)
|
|
190
|
+
* inside Markdown prompt `roleHint` strings so `prompts/*.md` stays 100% pure Markdown.
|
|
191
|
+
*/
|
|
192
|
+
export function interpolateRoleHint(template, doc) {
|
|
193
|
+
if (!template || !template.includes("{{")) return template;
|
|
194
|
+
return template
|
|
195
|
+
.replace(/\{\{\s*uiSummary\s*\}\}/g, () => {
|
|
196
|
+
const ui = doc?.artifacts?.uiSignals || {};
|
|
197
|
+
return `UI telemetry: pillBadges=${(ui.pillBadges || []).length}, gradients=${(ui.purplePinkGradients || []).length}, links=${(doc?.artifacts?.links || []).length}.`;
|
|
198
|
+
})
|
|
199
|
+
.replace(/\{\{\s*deckSummary\s*\}\}/g, () => {
|
|
200
|
+
const slides = doc?.artifacts?.deckSignals?.slides || [];
|
|
201
|
+
if (slides.length === 0) return "";
|
|
202
|
+
return `Deck structure (${slides.length} slides): ${slides
|
|
203
|
+
.slice(0, 15)
|
|
204
|
+
.map((s) => `S${s.index}("${String(s.title || "").slice(0, 40)}", words=${s.wordCount}, bullets=${s.bulletCount})`)
|
|
205
|
+
.join("; ")}.`;
|
|
206
|
+
})
|
|
207
|
+
.replace(/\{\{\s*slideCount\s*\}\}/g, () => {
|
|
208
|
+
const c = doc?.artifacts?.deckSignals?.slideCount;
|
|
209
|
+
return c ? String(c) : "0";
|
|
210
|
+
})
|
|
211
|
+
.trim();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function listFilesWithExt(dirPath, extRegex) {
|
|
215
|
+
if (!fs.existsSync(dirPath) || !fs.statSync(dirPath).isDirectory()) return [];
|
|
216
|
+
return fs
|
|
217
|
+
.readdirSync(dirPath, { withFileTypes: true })
|
|
218
|
+
.filter((e) => e.isFile() && extRegex.test(e.name))
|
|
219
|
+
.map((e) => path.join(dirPath, e.name))
|
|
220
|
+
.sort();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Create a lazy stage-gated Pack Handle for a separated Pack directory:
|
|
225
|
+
* `<packDir>/pack.json`
|
|
226
|
+
* `<packDir>/scripts/*.mjs` (imported ONLY when deterministic checks or verifyFix run)
|
|
227
|
+
* `<packDir>/prompts/*.md` (read & compiled ONLY when an LLM route is active)
|
|
228
|
+
*/
|
|
229
|
+
export function createDirectoryPackHandle(packDir) {
|
|
230
|
+
const manifestPath = path.join(packDir, "pack.json");
|
|
231
|
+
if (!fs.existsSync(manifestPath)) {
|
|
232
|
+
throw new Error(`Pack directory '${packDir}' is missing pack.json`);
|
|
233
|
+
}
|
|
234
|
+
const rawManifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
235
|
+
const name = String(rawManifest.name || "").trim();
|
|
236
|
+
if (!name) {
|
|
237
|
+
throw new Error(`Invalid pack.json in '${packDir}': missing 'name'`);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const version = String(rawManifest.version || "1.0.0").trim();
|
|
241
|
+
const description = String(rawManifest.description || "").trim();
|
|
242
|
+
const languages = Array.isArray(rawManifest.languages)
|
|
243
|
+
? Object.freeze(rawManifest.languages.map((l) => String(l).trim().toLowerCase()).filter(Boolean))
|
|
244
|
+
: null;
|
|
245
|
+
const formats = Array.isArray(rawManifest.formats)
|
|
246
|
+
? Object.freeze(
|
|
247
|
+
rawManifest.formats.map((f) => String(f).trim().toLowerCase().replace(/^\./, "")).filter(Boolean),
|
|
248
|
+
)
|
|
249
|
+
: null;
|
|
250
|
+
const modalities = Array.isArray(rawManifest.modalities)
|
|
251
|
+
? Object.freeze(rawManifest.modalities.map((m) => String(m).trim().toLowerCase()).filter(Boolean))
|
|
252
|
+
: null;
|
|
253
|
+
|
|
254
|
+
const scriptsDir = path.join(packDir, "scripts");
|
|
255
|
+
const promptsDir = path.join(packDir, "prompts");
|
|
256
|
+
|
|
257
|
+
const scriptFiles = listFilesWithExt(scriptsDir, /\.(?:mjs|js)$/i);
|
|
258
|
+
const promptFiles = listFilesWithExt(promptsDir, /\.md$/i);
|
|
259
|
+
|
|
260
|
+
const loadStats = {
|
|
261
|
+
scriptsLoaded: false,
|
|
262
|
+
promptsLoaded: false,
|
|
263
|
+
scriptFileCount: scriptFiles.length,
|
|
264
|
+
promptFileCount: promptFiles.length,
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
let cachedScriptsPromise = null;
|
|
268
|
+
let cachedPromptsPromise = null;
|
|
269
|
+
|
|
270
|
+
return Object.freeze({
|
|
271
|
+
__dislopPack: true,
|
|
272
|
+
__lazyPackHandle: true,
|
|
273
|
+
packDir,
|
|
274
|
+
name,
|
|
275
|
+
version,
|
|
276
|
+
description,
|
|
277
|
+
languages,
|
|
278
|
+
formats,
|
|
279
|
+
modalities,
|
|
280
|
+
hasScripts: scriptFiles.length > 0,
|
|
281
|
+
hasPrompts: promptFiles.length > 0,
|
|
282
|
+
loadStats,
|
|
283
|
+
|
|
284
|
+
async loadScripts() {
|
|
285
|
+
if (scriptFiles.length === 0) return [];
|
|
286
|
+
if (!cachedScriptsPromise) {
|
|
287
|
+
cachedScriptsPromise = (async () => {
|
|
288
|
+
const modules = [];
|
|
289
|
+
for (const sf of scriptFiles) {
|
|
290
|
+
const mod = await import(pathToFileURL(sf).href);
|
|
291
|
+
const exported = mod.default || mod;
|
|
292
|
+
const normalized = exported?.__dislopPackScripts
|
|
293
|
+
? exported
|
|
294
|
+
: definePackScripts({
|
|
295
|
+
appliesTo: exported?.appliesTo || mod.appliesTo,
|
|
296
|
+
check: exported?.check || mod.check,
|
|
297
|
+
verifyFix: exported?.verifyFix || mod.verifyFix,
|
|
298
|
+
});
|
|
299
|
+
modules.push(normalized);
|
|
300
|
+
}
|
|
301
|
+
loadStats.scriptsLoaded = true;
|
|
302
|
+
return modules;
|
|
303
|
+
})();
|
|
304
|
+
}
|
|
305
|
+
return cachedScriptsPromise;
|
|
306
|
+
},
|
|
307
|
+
|
|
308
|
+
async loadPrompts() {
|
|
309
|
+
if (promptFiles.length === 0) return [];
|
|
310
|
+
if (!cachedPromptsPromise) {
|
|
311
|
+
cachedPromptsPromise = (async () => {
|
|
312
|
+
const parsedPrompts = promptFiles.map((pf) => {
|
|
313
|
+
const raw = fs.readFileSync(pf, "utf8");
|
|
314
|
+
return parseMarkdownPromptContent(raw, pf);
|
|
315
|
+
});
|
|
316
|
+
loadStats.promptsLoaded = true;
|
|
317
|
+
return parsedPrompts;
|
|
318
|
+
})();
|
|
319
|
+
}
|
|
320
|
+
return cachedPromptsPromise;
|
|
321
|
+
},
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Wrap an in-memory programmatic `definePack()` object in the same `LazyPackHandle` interface.
|
|
327
|
+
*/
|
|
328
|
+
export function wrapEagerPackAsLazyHandle(pack) {
|
|
329
|
+
if (pack?.__lazyPackHandle) return pack;
|
|
330
|
+
const validated = pack?.__dislopPack ? pack : definePack(pack);
|
|
331
|
+
|
|
332
|
+
const loadStats = {
|
|
333
|
+
scriptsLoaded: false,
|
|
334
|
+
promptsLoaded: false,
|
|
335
|
+
scriptFileCount: validated.check || validated.verifyFix ? 1 : 0,
|
|
336
|
+
promptFileCount: validated.resolvePrompt ? 1 : 0,
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
return Object.freeze({
|
|
340
|
+
__dislopPack: true,
|
|
341
|
+
__lazyPackHandle: true,
|
|
342
|
+
name: validated.name,
|
|
343
|
+
version: validated.version,
|
|
344
|
+
description: validated.description,
|
|
345
|
+
languages: validated.languages,
|
|
346
|
+
formats: validated.formats,
|
|
347
|
+
modalities: validated.modalities,
|
|
348
|
+
appliesTo: validated.appliesTo || null,
|
|
349
|
+
hasScripts: Boolean(validated.check || validated.verifyFix),
|
|
350
|
+
hasPrompts: Boolean(validated.resolvePrompt),
|
|
351
|
+
loadStats,
|
|
352
|
+
|
|
353
|
+
async loadScripts() {
|
|
354
|
+
loadStats.scriptsLoaded = true;
|
|
355
|
+
if (!validated.check && !validated.verifyFix && !validated.appliesTo) return [];
|
|
356
|
+
return [
|
|
357
|
+
{
|
|
358
|
+
appliesTo: validated.appliesTo || null,
|
|
359
|
+
check: validated.check || null,
|
|
360
|
+
verifyFix: validated.verifyFix || null,
|
|
361
|
+
},
|
|
362
|
+
];
|
|
363
|
+
},
|
|
364
|
+
|
|
365
|
+
async loadPrompts(doc, ctx) {
|
|
366
|
+
loadStats.promptsLoaded = true;
|
|
367
|
+
if (typeof validated.resolvePrompt !== "function") return [];
|
|
368
|
+
const res = validated.resolvePrompt(doc, ctx);
|
|
369
|
+
if (!res || !Array.isArray(res.rules)) return [];
|
|
370
|
+
return [{ roleHint: res.roleHint || "", rules: res.rules }];
|
|
371
|
+
},
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function isSeparatedPackDirectory(dirPath) {
|
|
376
|
+
return fs.existsSync(path.join(dirPath, "pack.json"));
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Enumerate all built-in Packs under `packs/<scope-or-lang>/<scenario>/` by reading `pack.json` only.
|
|
381
|
+
*/
|
|
382
|
+
export function listBuiltinPackIds(packsRoot = BUILTIN_PACKS_ROOT) {
|
|
383
|
+
if (!fs.existsSync(packsRoot)) return [];
|
|
384
|
+
const results = [];
|
|
385
|
+
for (const level1 of fs.readdirSync(packsRoot, { withFileTypes: true })) {
|
|
386
|
+
if (!level1.isDirectory()) continue;
|
|
387
|
+
const level1Dir = path.join(packsRoot, level1.name);
|
|
388
|
+
for (const level2 of fs.readdirSync(level1Dir, { withFileTypes: true })) {
|
|
389
|
+
if (!level2.isDirectory()) continue;
|
|
390
|
+
const packDir = path.join(level1Dir, level2.name);
|
|
391
|
+
if (
|
|
392
|
+
isSeparatedPackDirectory(packDir) ||
|
|
393
|
+
fs.existsSync(path.join(packDir, "index.mjs")) ||
|
|
394
|
+
fs.existsSync(path.join(packDir, "pack.md"))
|
|
395
|
+
) {
|
|
396
|
+
results.push(`${level1.name}/${level2.name}`);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return results.sort();
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
async function loadFromFilesystemEntry(targetPath) {
|
|
404
|
+
const stat = fs.statSync(targetPath);
|
|
405
|
+
if (stat.isDirectory()) {
|
|
406
|
+
if (isSeparatedPackDirectory(targetPath)) {
|
|
407
|
+
return createDirectoryPackHandle(targetPath);
|
|
408
|
+
}
|
|
409
|
+
const candidates = ["index.mjs", "index.js", "pack.md"].map((f) => path.join(targetPath, f));
|
|
410
|
+
const found = candidates.find((p) => fs.existsSync(p));
|
|
411
|
+
if (!found) {
|
|
412
|
+
throw new Error(`Pack directory '${targetPath}' is missing pack.json, index.mjs, or pack.md`);
|
|
413
|
+
}
|
|
414
|
+
return loadFromFilesystemEntry(found);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (/\.md$/i.test(targetPath)) {
|
|
418
|
+
return compileMarkdownPack(fs.readFileSync(targetPath, "utf8"), targetPath);
|
|
419
|
+
}
|
|
420
|
+
if (path.basename(targetPath) === "pack.json") {
|
|
421
|
+
return createDirectoryPackHandle(path.dirname(targetPath));
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const mod = await import(pathToFileURL(targetPath).href);
|
|
425
|
+
const candidate = mod.default || mod.pack;
|
|
426
|
+
if (!candidate || typeof candidate !== "object") {
|
|
427
|
+
throw new Error(`Pack module '${targetPath}' does not export a default definePack() object`);
|
|
428
|
+
}
|
|
429
|
+
return wrapEagerPackAsLazyHandle(candidate);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Resolve a single Pack specifier into a `LazyPackHandle` WITHOUT importing `scripts/` or reading `prompts/`
|
|
434
|
+
* when the pack is a separated Pack directory (`pack.json`).
|
|
435
|
+
*/
|
|
436
|
+
export async function loadSinglePack(
|
|
437
|
+
specifier,
|
|
438
|
+
{ docLanguage = "en", cwd = process.cwd(), packsRoot = BUILTIN_PACKS_ROOT } = {},
|
|
439
|
+
) {
|
|
440
|
+
if (specifier && typeof specifier === "object") {
|
|
441
|
+
return wrapEagerPackAsLazyHandle(specifier);
|
|
442
|
+
}
|
|
443
|
+
const raw = String(specifier || "").trim();
|
|
444
|
+
if (!raw) {
|
|
445
|
+
throw new Error("Empty Pack specifier");
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
const looksLikePath =
|
|
449
|
+
raw.startsWith("./") ||
|
|
450
|
+
raw.startsWith("../") ||
|
|
451
|
+
path.isAbsolute(raw) ||
|
|
452
|
+
/\.(?:mjs|js|md|json)$/i.test(raw);
|
|
453
|
+
if (looksLikePath) {
|
|
454
|
+
const abs = path.resolve(cwd, raw);
|
|
455
|
+
if (!fs.existsSync(abs)) {
|
|
456
|
+
throw new Error(`Pack path not found: '${raw}' (resolved to '${abs}')`);
|
|
457
|
+
}
|
|
458
|
+
return loadFromFilesystemEntry(abs);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
let normalizedId = SHORTHAND_ALIASES[raw] || raw;
|
|
462
|
+
if (!normalizedId.includes("/")) {
|
|
463
|
+
const langDir = path.join(packsRoot, docLanguage, normalizedId);
|
|
464
|
+
const commonDir = path.join(packsRoot, "common", normalizedId);
|
|
465
|
+
if (fs.existsSync(langDir)) {
|
|
466
|
+
normalizedId = `${docLanguage}/${normalizedId}`;
|
|
467
|
+
} else if (fs.existsSync(commonDir)) {
|
|
468
|
+
normalizedId = `common/${normalizedId}`;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const builtinDir = path.join(packsRoot, ...normalizedId.split("/"));
|
|
473
|
+
if (fs.existsSync(builtinDir) && fs.statSync(builtinDir).isDirectory()) {
|
|
474
|
+
return loadFromFilesystemEntry(builtinDir);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// Installed npm package fallback
|
|
478
|
+
try {
|
|
479
|
+
const req = createRequire(path.join(cwd, "package.json"));
|
|
480
|
+
const pkgEntry = req.resolve(raw);
|
|
481
|
+
const pkgDir = path.dirname(pkgEntry);
|
|
482
|
+
if (isSeparatedPackDirectory(pkgDir)) {
|
|
483
|
+
return createDirectoryPackHandle(pkgDir);
|
|
484
|
+
}
|
|
485
|
+
return loadFromFilesystemEntry(pkgEntry);
|
|
486
|
+
} catch {
|
|
487
|
+
throw new Error(
|
|
488
|
+
`Unknown Pack '${raw}'. Built-in packs: ${listBuiltinPackIds(packsRoot).join(", ")}`,
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function builtinPackExists(packsRoot, id) {
|
|
494
|
+
const dir = path.join(packsRoot, ...id.split("/"));
|
|
495
|
+
return (
|
|
496
|
+
isSeparatedPackDirectory(dir) ||
|
|
497
|
+
fs.existsSync(path.join(dir, "index.mjs")) ||
|
|
498
|
+
fs.existsSync(path.join(dir, "pack.md"))
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Determine default Pack specifiers for a `DocumentModel` when no custom `--pack` was given.
|
|
504
|
+
*/
|
|
505
|
+
export function defaultPackSpecifiersForDoc(doc, packsRoot = BUILTIN_PACKS_ROOT) {
|
|
506
|
+
const lang = doc.language === "zh" ? "zh" : "en";
|
|
507
|
+
const specs = ["common/artifact-qa"];
|
|
508
|
+
|
|
509
|
+
const pushIfBuiltinExists = (id) => {
|
|
510
|
+
if (builtinPackExists(packsRoot, id)) specs.push(id);
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
if (doc.segments.length > 0) {
|
|
514
|
+
pushIfBuiltinExists(`${lang}/artifact-qa`);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
if (["html", "css", "jsx", "tsx", "svg"].includes(doc.format)) {
|
|
518
|
+
specs.push("common/web-ui");
|
|
519
|
+
if (doc.segments.length > 0) {
|
|
520
|
+
pushIfBuiltinExists(`${lang}/web-ui`);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
if (["pptx", "pdf", "png", "jpg", "jpeg", "webp", "gif"].includes(doc.format)) {
|
|
525
|
+
specs.push("common/slide-deck");
|
|
526
|
+
if (doc.segments.length > 0) {
|
|
527
|
+
pushIfBuiltinExists(`${lang}/slide-deck`);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
if (doc.segments.length > 0) {
|
|
532
|
+
pushIfBuiltinExists(`${lang}/prose`);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
return specs;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function expandScenarioShorthands(rawSpecs, docLanguage, packsRoot) {
|
|
539
|
+
const expanded = [];
|
|
540
|
+
for (const spec of rawSpecs) {
|
|
541
|
+
if (typeof spec !== "string") {
|
|
542
|
+
expanded.push(spec);
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
const trimmed = spec.trim();
|
|
546
|
+
const looksLikePath =
|
|
547
|
+
trimmed.startsWith("./") ||
|
|
548
|
+
trimmed.startsWith("../") ||
|
|
549
|
+
path.isAbsolute(trimmed) ||
|
|
550
|
+
/\.(?:mjs|js|md|json)$/i.test(trimmed);
|
|
551
|
+
|
|
552
|
+
if (looksLikePath || trimmed.includes("/") || SHORTHAND_ALIASES[trimmed]) {
|
|
553
|
+
expanded.push(trimmed);
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
const commonId = `common/${trimmed}`;
|
|
558
|
+
const langId = `${docLanguage}/${trimmed}`;
|
|
559
|
+
const hasCommon = builtinPackExists(packsRoot, commonId);
|
|
560
|
+
const hasLang = builtinPackExists(packsRoot, langId);
|
|
561
|
+
|
|
562
|
+
if (hasCommon && hasLang) {
|
|
563
|
+
expanded.push(commonId, langId);
|
|
564
|
+
} else {
|
|
565
|
+
expanded.push(trimmed);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
return expanded;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
export function packMetadataAppliesToDoc(pack, doc) {
|
|
572
|
+
if (pack.languages && pack.languages.length > 0 && !pack.languages.includes(doc.language)) {
|
|
573
|
+
return false;
|
|
574
|
+
}
|
|
575
|
+
if (pack.formats && pack.formats.length > 0 && !pack.formats.includes(doc.format)) {
|
|
576
|
+
return false;
|
|
577
|
+
}
|
|
578
|
+
if (pack.modalities && pack.modalities.length > 0 && !pack.modalities.includes(doc.modality)) {
|
|
579
|
+
return false;
|
|
580
|
+
}
|
|
581
|
+
if (typeof pack.appliesTo === "function") {
|
|
582
|
+
return Boolean(pack.appliesTo(doc));
|
|
583
|
+
}
|
|
584
|
+
return true;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Resolve and filter active Packs for `doc` using `pack.json` metadata ONLY
|
|
589
|
+
* (zero `scripts/` imports and zero `prompts/` reads during admission!).
|
|
590
|
+
*/
|
|
591
|
+
export async function resolveActivePacks(
|
|
592
|
+
doc,
|
|
593
|
+
{ packSpecifiers = null, cwd = process.cwd(), packsRoot = BUILTIN_PACKS_ROOT } = {},
|
|
594
|
+
) {
|
|
595
|
+
const requested =
|
|
596
|
+
packSpecifiers && packSpecifiers.length > 0
|
|
597
|
+
? expandScenarioShorthands(packSpecifiers, doc.language, packsRoot)
|
|
598
|
+
: defaultPackSpecifiersForDoc(doc, packsRoot);
|
|
599
|
+
|
|
600
|
+
const loaded = [];
|
|
601
|
+
const seenNames = new Set();
|
|
602
|
+
for (const spec of requested) {
|
|
603
|
+
const pack = await loadSinglePack(spec, { docLanguage: doc.language, cwd, packsRoot });
|
|
604
|
+
if (seenNames.has(pack.name)) continue;
|
|
605
|
+
seenNames.add(pack.name);
|
|
606
|
+
if (packMetadataAppliesToDoc(pack, doc)) {
|
|
607
|
+
loaded.push(pack);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
return loaded;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
export function qualifyRuleId(packName, ruleId) {
|
|
614
|
+
const cleanRule = String(ruleId || "rule").trim();
|
|
615
|
+
if (cleanRule.startsWith(`${packName}/`)) return cleanRule;
|
|
616
|
+
return `${packName}/${cleanRule}`;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* Lazily import `scripts/` ONLY for admitted Packs that declare `hasScripts`,
|
|
621
|
+
* and execute `check(doc, ctx)` in parallel. Never touches `prompts/`.
|
|
622
|
+
*/
|
|
623
|
+
export async function runDeterministicPackChecks(
|
|
624
|
+
doc,
|
|
625
|
+
activePacks,
|
|
626
|
+
{ diagnosticLang = doc.language, cwd = process.cwd() } = {},
|
|
627
|
+
) {
|
|
628
|
+
const items = [];
|
|
629
|
+
|
|
630
|
+
await Promise.all(
|
|
631
|
+
activePacks.map(async (pack) => {
|
|
632
|
+
if (!pack.hasScripts) return;
|
|
633
|
+
const scriptModules = await pack.loadScripts();
|
|
634
|
+
if (scriptModules.length === 0) return;
|
|
635
|
+
|
|
636
|
+
const localItems = [];
|
|
637
|
+
const ctx = Object.freeze({
|
|
638
|
+
diagnosticLang,
|
|
639
|
+
cwd,
|
|
640
|
+
reportDefect(input) {
|
|
641
|
+
if (!input || typeof input !== "object") return;
|
|
642
|
+
const severity = VALID_DEFECT_SEVERITIES.includes(input.severity) ? input.severity : "error";
|
|
643
|
+
const fixSafety = VALID_FIX_SAFETIES.includes(input.fixSafety) ? input.fixSafety : "manual";
|
|
644
|
+
const suggestedReplacement = String(input.suggestedReplacement ?? "");
|
|
645
|
+
const actionType = VALID_ACTION_TYPES.includes(input.actionType)
|
|
646
|
+
? input.actionType
|
|
647
|
+
: fixSafety !== "manual" && suggestedReplacement === ""
|
|
648
|
+
? "delete"
|
|
649
|
+
: suggestedReplacement
|
|
650
|
+
? "replace"
|
|
651
|
+
: "prompt";
|
|
652
|
+
|
|
653
|
+
const seg = input.targetId ? doc.segments.find((s) => s.id === input.targetId) : null;
|
|
654
|
+
localItems.push({
|
|
655
|
+
kind: "defect",
|
|
656
|
+
pack: pack.name,
|
|
657
|
+
ruleId: qualifyRuleId(pack.name, input.ruleId),
|
|
658
|
+
targetId: input.targetId || null,
|
|
659
|
+
...(input.line ?? seg?.line ? { line: input.line ?? seg?.line } : {}),
|
|
660
|
+
severity,
|
|
661
|
+
fixSafety,
|
|
662
|
+
actionType,
|
|
663
|
+
originalQuote: String(input.originalQuote ?? seg?.text ?? ""),
|
|
664
|
+
suggestedReplacement,
|
|
665
|
+
reason: String(input.reason || "Objective artifact defect."),
|
|
666
|
+
...(input.rewritePrompt ? { rewritePrompt: String(input.rewritePrompt) } : {}),
|
|
667
|
+
...(input.locator ? { _locator: input.locator } : {}),
|
|
668
|
+
});
|
|
669
|
+
},
|
|
670
|
+
reportSuggestion(input) {
|
|
671
|
+
if (!input || typeof input !== "object") return;
|
|
672
|
+
const priority = VALID_SUGGESTION_PRIORITIES.includes(input.priority)
|
|
673
|
+
? input.priority
|
|
674
|
+
: "moderate";
|
|
675
|
+
const fixSafety = VALID_FIX_SAFETIES.includes(input.fixSafety) ? input.fixSafety : "review";
|
|
676
|
+
const suggestedReplacement = String(input.suggestedReplacement ?? "");
|
|
677
|
+
const actionType = VALID_ACTION_TYPES.includes(input.actionType)
|
|
678
|
+
? input.actionType
|
|
679
|
+
: fixSafety !== "manual" && suggestedReplacement === ""
|
|
680
|
+
? "delete"
|
|
681
|
+
: suggestedReplacement
|
|
682
|
+
? "replace"
|
|
683
|
+
: "prompt";
|
|
684
|
+
|
|
685
|
+
const seg = input.targetId ? doc.segments.find((s) => s.id === input.targetId) : null;
|
|
686
|
+
localItems.push({
|
|
687
|
+
kind: "suggestion",
|
|
688
|
+
pack: pack.name,
|
|
689
|
+
ruleId: qualifyRuleId(pack.name, input.ruleId),
|
|
690
|
+
targetId: input.targetId || null,
|
|
691
|
+
...(input.line ?? seg?.line ? { line: input.line ?? seg?.line } : {}),
|
|
692
|
+
priority,
|
|
693
|
+
fixSafety,
|
|
694
|
+
actionType,
|
|
695
|
+
originalQuote: String(input.originalQuote ?? seg?.text ?? ""),
|
|
696
|
+
suggestedReplacement,
|
|
697
|
+
reason: String(input.reason || "Editorial improvement suggestion."),
|
|
698
|
+
...(input.rewritePrompt ? { rewritePrompt: String(input.rewritePrompt) } : {}),
|
|
699
|
+
...(input.locator ? { _locator: input.locator } : {}),
|
|
700
|
+
});
|
|
701
|
+
},
|
|
702
|
+
});
|
|
703
|
+
|
|
704
|
+
for (const sm of scriptModules) {
|
|
705
|
+
if (typeof sm.appliesTo === "function" && !sm.appliesTo(doc)) continue;
|
|
706
|
+
if (typeof sm.check === "function") {
|
|
707
|
+
await sm.check(doc, ctx);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
items.push(...localItems);
|
|
711
|
+
}),
|
|
712
|
+
);
|
|
713
|
+
|
|
714
|
+
return items;
|
|
715
|
+
}
|