@aigne/doc-smith 0.2.4 → 0.2.6

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