@aigne/doc-smith 0.2.5 → 0.2.8

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.
Files changed (41) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +1 -0
  3. package/agents/check-detail-result.mjs +13 -139
  4. package/agents/check-detail.mjs +4 -6
  5. package/agents/check-structure-plan.mjs +56 -12
  6. package/agents/detail-generator-and-translate.yaml +7 -1
  7. package/agents/detail-regenerator.yaml +3 -1
  8. package/agents/docs-generator.yaml +2 -1
  9. package/agents/find-item-by-path.mjs +64 -15
  10. package/agents/input-generator.mjs +31 -11
  11. package/agents/language-selector.mjs +89 -0
  12. package/agents/load-config.mjs +2 -2
  13. package/agents/load-sources.mjs +13 -40
  14. package/agents/publish-docs.mjs +47 -161
  15. package/agents/retranslate.yaml +74 -0
  16. package/agents/save-docs.mjs +19 -21
  17. package/agents/save-output.mjs +2 -9
  18. package/agents/save-single-doc.mjs +20 -1
  19. package/agents/schema/structure-plan.yaml +1 -1
  20. package/agents/structure-planning.yaml +6 -0
  21. package/agents/transform-detail-datasources.mjs +2 -5
  22. package/agents/translate.yaml +3 -0
  23. package/aigne.yaml +5 -1
  24. package/biome.json +13 -3
  25. package/docs-mcp/get-docs-structure.mjs +1 -1
  26. package/docs-mcp/read-doc-content.mjs +1 -4
  27. package/package.json +20 -7
  28. package/prompts/check-structure-planning-result.md +4 -7
  29. package/prompts/content-detail-generator.md +1 -2
  30. package/prompts/structure-planning.md +7 -2
  31. package/prompts/translator.md +4 -0
  32. package/tests/check-detail-result.test.mjs +8 -19
  33. package/tests/load-sources.test.mjs +65 -161
  34. package/tests/test-all-validation-cases.mjs +741 -0
  35. package/tests/test-save-docs.mjs +6 -17
  36. package/utils/constants.mjs +1 -2
  37. package/utils/markdown-checker.mjs +453 -0
  38. package/utils/mermaid-validator.mjs +153 -0
  39. package/utils/mermaid-worker-pool.mjs +250 -0
  40. package/utils/mermaid-worker.mjs +233 -0
  41. package/utils/utils.mjs +162 -114
@@ -0,0 +1,250 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Simplified Mermaid Worker Pool
5
+ * Manages worker threads for concurrent mermaid validation
6
+ */
7
+
8
+ import { dirname, join } from "node:path";
9
+ import { fileURLToPath } from "node:url";
10
+ import { Worker } from "node:worker_threads";
11
+
12
+ const __filename = fileURLToPath(import.meta.url);
13
+ const __dirname = dirname(__filename);
14
+
15
+ class SimpleMermaidWorkerPool {
16
+ constructor(options = {}) {
17
+ this.poolSize = options.poolSize || 3;
18
+ this.timeout = options.timeout || 15000; // Reduced timeout
19
+
20
+ this.workers = [];
21
+ this.availableWorkers = [];
22
+ this.requestQueue = [];
23
+ this.nextRequestId = 1;
24
+ this.isShuttingDown = false;
25
+ }
26
+
27
+ /**
28
+ * Initialize worker pool
29
+ */
30
+ async initialize() {
31
+ if (this.workers.length > 0) return; // Already initialized
32
+
33
+ const workerPath = join(__dirname, "mermaid-worker.mjs");
34
+
35
+ for (let i = 0; i < this.poolSize; i++) {
36
+ await this.createWorker(workerPath, i);
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Create a single worker
42
+ */
43
+ async createWorker(workerPath, workerId) {
44
+ return new Promise((resolve, reject) => {
45
+ try {
46
+ const worker = new Worker(workerPath);
47
+ worker.workerId = workerId;
48
+ worker.isAvailable = true;
49
+ worker.currentRequest = null;
50
+
51
+ // Handle worker errors more gracefully
52
+ worker.on("error", (error) => {
53
+ if (worker.currentRequest) {
54
+ worker.currentRequest.reject(new Error(`Worker error: ${error.message}`));
55
+ worker.currentRequest = null;
56
+ }
57
+ });
58
+
59
+ worker.on("exit", (_code) => {
60
+ if (worker.currentRequest) {
61
+ worker.currentRequest.reject(new Error("Worker exited unexpectedly"));
62
+ worker.currentRequest = null;
63
+ }
64
+ });
65
+
66
+ worker.on("message", (data) => {
67
+ this.handleWorkerMessage(worker, data);
68
+ });
69
+
70
+ this.workers.push(worker);
71
+ this.availableWorkers.push(worker);
72
+
73
+ resolve(worker);
74
+ } catch (error) {
75
+ reject(error);
76
+ }
77
+ });
78
+ }
79
+
80
+ /**
81
+ * Handle worker message
82
+ */
83
+ handleWorkerMessage(worker, data) {
84
+ if (!worker.currentRequest) return;
85
+
86
+ const { resolve, reject, timeoutId } = worker.currentRequest;
87
+
88
+ // Clear timeout
89
+ if (timeoutId) {
90
+ clearTimeout(timeoutId);
91
+ }
92
+
93
+ // Reset worker state
94
+ worker.currentRequest = null;
95
+ worker.isAvailable = true;
96
+
97
+ // Move worker back to available pool
98
+ const workerIndex = this.workers.indexOf(worker);
99
+ if (workerIndex > -1 && !this.availableWorkers.includes(worker)) {
100
+ this.availableWorkers.push(worker);
101
+ }
102
+
103
+ // Process queued requests
104
+ this.processQueue();
105
+
106
+ // Handle response
107
+ if (data.error) {
108
+ reject(new Error(data.error));
109
+ } else {
110
+ resolve(data.result);
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Process queued requests
116
+ */
117
+ processQueue() {
118
+ while (this.requestQueue.length > 0 && this.availableWorkers.length > 0) {
119
+ const queuedRequest = this.requestQueue.shift();
120
+ const worker = this.availableWorkers.shift();
121
+
122
+ this.executeRequest(worker, queuedRequest);
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Execute a request on a worker
128
+ */
129
+ executeRequest(worker, request) {
130
+ const { content, resolve, reject } = request;
131
+ const requestId = this.nextRequestId++;
132
+
133
+ // Set timeout
134
+ const timeoutId = setTimeout(() => {
135
+ worker.currentRequest = null;
136
+ worker.isAvailable = true;
137
+ if (!this.availableWorkers.includes(worker)) {
138
+ this.availableWorkers.push(worker);
139
+ }
140
+ reject(new Error(`Validation timeout after ${this.timeout}ms`));
141
+ }, this.timeout);
142
+
143
+ // Store request info
144
+ worker.currentRequest = { resolve, reject, timeoutId };
145
+ worker.isAvailable = false;
146
+
147
+ // Send request
148
+ worker.postMessage({
149
+ id: requestId,
150
+ content: content,
151
+ });
152
+ }
153
+
154
+ /**
155
+ * Validate content using worker pool
156
+ */
157
+ async validate(content) {
158
+ if (this.isShuttingDown) {
159
+ throw new Error("Worker pool is shutting down");
160
+ }
161
+
162
+ // Initialize if needed
163
+ await this.initialize();
164
+
165
+ return new Promise((resolve, reject) => {
166
+ const request = { content, resolve, reject };
167
+
168
+ // If worker available, use it immediately
169
+ if (this.availableWorkers.length > 0) {
170
+ const worker = this.availableWorkers.shift();
171
+ this.executeRequest(worker, request);
172
+ } else {
173
+ // Queue the request
174
+ this.requestQueue.push(request);
175
+ }
176
+ });
177
+ }
178
+
179
+ /**
180
+ * Get pool statistics
181
+ */
182
+ getStats() {
183
+ return {
184
+ poolSize: this.poolSize,
185
+ totalWorkers: this.workers.length,
186
+ availableWorkers: this.availableWorkers.length,
187
+ busyWorkers: this.workers.length - this.availableWorkers.length,
188
+ queuedRequests: this.requestQueue.length,
189
+ isShuttingDown: this.isShuttingDown,
190
+ };
191
+ }
192
+
193
+ /**
194
+ * Shutdown the pool
195
+ */
196
+ async shutdown() {
197
+ if (this.isShuttingDown) return;
198
+
199
+ this.isShuttingDown = true;
200
+
201
+ // Reject all queued requests
202
+ while (this.requestQueue.length > 0) {
203
+ const request = this.requestQueue.shift();
204
+ request.reject(new Error("Worker pool is shutting down"));
205
+ }
206
+
207
+ // Terminate all workers
208
+ const terminationPromises = this.workers.map(async (worker) => {
209
+ try {
210
+ await worker.terminate();
211
+ } catch (_error) {
212
+ // Ignore termination errors
213
+ }
214
+ });
215
+
216
+ await Promise.allSettled(terminationPromises);
217
+
218
+ // Clear arrays
219
+ this.workers.length = 0;
220
+ this.availableWorkers.length = 0;
221
+ }
222
+ }
223
+
224
+ // Global pool instance
225
+ let globalPool = null;
226
+
227
+ /**
228
+ * Get global worker pool
229
+ */
230
+ export function getMermaidWorkerPool(options = {}) {
231
+ if (!globalPool) {
232
+ globalPool = new SimpleMermaidWorkerPool(options);
233
+ }
234
+ return globalPool;
235
+ }
236
+
237
+ /**
238
+ * Shutdown global pool
239
+ */
240
+ export async function shutdownMermaidWorkerPool() {
241
+ if (globalPool) {
242
+ await globalPool.shutdown();
243
+ globalPool = null;
244
+ }
245
+ }
246
+
247
+ // Note: We don't add global process event listeners here to avoid preventing clean exit
248
+ // The application should call shutdownMermaidWorkerPool() explicitly when needed
249
+
250
+ export { SimpleMermaidWorkerPool };
@@ -0,0 +1,233 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Simplified Mermaid Validation Worker
5
+ * Runs in isolated Worker thread to avoid global state conflicts
6
+ */
7
+
8
+ import { parentPort } from "node:worker_threads";
9
+
10
+ /**
11
+ * Validate mermaid syntax using official parser in isolated environment
12
+ */
13
+ async function validateMermaidWithOfficialParser(content) {
14
+ const trimmedContent = content.trim();
15
+ if (!content || !trimmedContent) {
16
+ throw new Error("Empty mermaid diagram");
17
+ }
18
+
19
+ try {
20
+ // Import dependencies
21
+ const { JSDOM } = await import("jsdom");
22
+ const DOMPurifyModule = await import("dompurify");
23
+
24
+ // Create isolated DOM environment
25
+ const { window } = new JSDOM(`<!DOCTYPE html><html><body></body></html>`, {
26
+ pretendToBeVisual: true,
27
+ resources: "usable",
28
+ });
29
+
30
+ // Setup globals (safe in worker - no conflicts)
31
+ global.window = window;
32
+ global.document = window.document;
33
+
34
+ // Only set navigator if it doesn't exist
35
+ if (!global.navigator) {
36
+ global.navigator = {
37
+ userAgent: "node.js",
38
+ platform: "node",
39
+ cookieEnabled: false,
40
+ onLine: true,
41
+ };
42
+ }
43
+
44
+ global.DOMParser = window.DOMParser;
45
+ global.XMLSerializer = window.XMLSerializer;
46
+ global.HTMLElement = window.HTMLElement;
47
+ global.HTMLDivElement = window.HTMLDivElement;
48
+ global.SVGElement = window.SVGElement;
49
+ global.Element = window.Element;
50
+ global.Node = window.Node;
51
+
52
+ // Initialize DOMPurify with the JSDOM window
53
+ const dompurify = DOMPurifyModule.default(window);
54
+
55
+ // Verify DOMPurify is working before proceeding
56
+ if (typeof dompurify.sanitize !== "function") {
57
+ throw new Error("DOMPurify initialization failed - sanitize method not available");
58
+ }
59
+
60
+ // Test DOMPurify functionality
61
+ dompurify.sanitize("<p>test</p>");
62
+
63
+ // Step 5: Comprehensively set up DOMPurify in all possible global locations
64
+ global.DOMPurify = dompurify;
65
+ window.DOMPurify = dompurify;
66
+
67
+ // For ES module interception, we need to ensure DOMPurify is available
68
+ // in all the ways mermaid might try to access it
69
+ if (typeof globalThis !== "undefined") {
70
+ globalThis.DOMPurify = dompurify;
71
+ }
72
+
73
+ // Set up on the global scope itself
74
+ if (typeof self !== "undefined") {
75
+ self.DOMPurify = dompurify;
76
+ }
77
+
78
+ // CRITICAL: Override the DOMPurify constructor/factory to always use our window
79
+ // This is the key to solving the issue: mermaid imports DOMPurify directly
80
+ const originalDOMPurifyFactory = DOMPurifyModule.default;
81
+ try {
82
+ // This might work: intercept the factory function itself
83
+ if (typeof originalDOMPurifyFactory === "function" && !originalDOMPurifyFactory.sanitize) {
84
+ // This means DOMPurify.default is a factory function, not an instance
85
+ // We need to make sure when mermaid calls DOMPurify.sanitize, it works
86
+ const factoryResult = originalDOMPurifyFactory(window);
87
+
88
+ // Copy methods from our working instance to the factory result
89
+ Object.assign(originalDOMPurifyFactory, factoryResult);
90
+ }
91
+ } catch (_factoryError) {
92
+ // If factory modification fails, that's OK - we have other fallbacks
93
+ }
94
+
95
+ // Import and setup mermaid
96
+ const mermaid = await import("mermaid");
97
+
98
+ mermaid.default.initialize({
99
+ startOnLoad: false,
100
+ theme: "default",
101
+ securityLevel: "loose",
102
+ htmlLabels: false,
103
+ });
104
+
105
+ // Parse content
106
+ await mermaid.default.parse(trimmedContent);
107
+
108
+ return true;
109
+ } catch (error) {
110
+ const errorMessage = error.message || String(error);
111
+
112
+ // Keep parse errors as-is for useful info
113
+ if (errorMessage.includes("Parse error")) {
114
+ throw new Error(errorMessage);
115
+ }
116
+
117
+ if (errorMessage.includes("Expecting ")) {
118
+ throw new Error(`Syntax error: ${errorMessage.replace(/^.*Expecting /, "Expected ")}`);
119
+ }
120
+
121
+ if (errorMessage.includes("Lexical error")) {
122
+ throw new Error("Syntax error: invalid characters or tokens");
123
+ }
124
+
125
+ if (errorMessage.includes("No diagram type detected")) {
126
+ throw new Error("Syntax error: invalid or unrecognized diagram type");
127
+ }
128
+
129
+ throw new Error(errorMessage);
130
+ }
131
+ }
132
+
133
+ /**
134
+ * Basic validation fallback
135
+ */
136
+ function validateBasicMermaidSyntax(content) {
137
+ const trimmedContent = content.trim();
138
+
139
+ if (!trimmedContent) {
140
+ throw new Error("Empty mermaid diagram");
141
+ }
142
+
143
+ const validDiagramTypes = [
144
+ "flowchart",
145
+ "graph",
146
+ "sequenceDiagram",
147
+ "classDiagram",
148
+ "stateDiagram",
149
+ "entityRelationshipDiagram",
150
+ "erDiagram",
151
+ "journey",
152
+ "gantt",
153
+ "pie",
154
+ "requirement",
155
+ "gitgraph",
156
+ "mindmap",
157
+ "timeline",
158
+ "quadrantChart",
159
+ ];
160
+
161
+ const firstLine = trimmedContent.split("\n")[0].trim();
162
+ const hasValidType = validDiagramTypes.some((type) => firstLine.includes(type));
163
+
164
+ if (!hasValidType) {
165
+ throw new Error("Invalid or missing diagram type");
166
+ }
167
+
168
+ // Basic bracket matching
169
+ const openBrackets = (content.match(/[[{(]/g) || []).length;
170
+ const closeBrackets = (content.match(/[\]})]/g) || []).length;
171
+
172
+ if (openBrackets !== closeBrackets) {
173
+ throw new Error("Unmatched brackets in diagram");
174
+ }
175
+
176
+ return true;
177
+ }
178
+
179
+ /**
180
+ * Main validation with fallback
181
+ */
182
+ async function validateMermaidSyntax(content) {
183
+ try {
184
+ return await validateMermaidWithOfficialParser(content);
185
+ } catch (officialError) {
186
+ const errorMsg = officialError.message || String(officialError);
187
+
188
+ // Check if it's an environment issue
189
+ if (
190
+ errorMsg.includes("Cannot resolve module") ||
191
+ errorMsg.includes("window is not defined") ||
192
+ errorMsg.includes("canvas") ||
193
+ errorMsg.includes("Web APIs") ||
194
+ errorMsg.includes("getComputedTextLength") ||
195
+ errorMsg.includes("document is not defined") ||
196
+ errorMsg.includes("DOMPurify")
197
+ ) {
198
+ // Fall back to basic validation
199
+ return validateBasicMermaidSyntax(content);
200
+ }
201
+
202
+ // Re-throw syntax errors
203
+ throw officialError;
204
+ }
205
+ }
206
+
207
+ // Worker message handler
208
+ if (parentPort) {
209
+ parentPort.on("message", async (data) => {
210
+ const { id, content } = data;
211
+
212
+ try {
213
+ if (!id || !content) {
214
+ throw new Error("Missing id or content");
215
+ }
216
+
217
+ const result = await validateMermaidSyntax(content);
218
+
219
+ parentPort.postMessage({
220
+ id,
221
+ success: true,
222
+ result,
223
+ });
224
+ } catch (error) {
225
+ parentPort.postMessage({
226
+ id,
227
+ error: error.message || String(error),
228
+ });
229
+ }
230
+ });
231
+ }
232
+
233
+ export { validateMermaidSyntax };