@aigne/doc-smith 0.8.5 → 0.8.7

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 (126) hide show
  1. package/.aigne/doc-smith/output/structure-plan.json +1 -5
  2. package/CHANGELOG.md +25 -0
  3. package/README.md +3 -3
  4. package/agents/{chat.yaml → chat/index.yaml} +7 -7
  5. package/agents/generate/check-document-structure.yaml +30 -0
  6. package/agents/{check-structure-plan.mjs → generate/check-need-generate-structure.mjs} +21 -21
  7. package/agents/generate/generate-structure.yaml +58 -0
  8. package/agents/{docs-generator.yaml → generate/index.yaml} +15 -16
  9. package/agents/generate/refine-document-structure.yaml +12 -0
  10. package/agents/{input-generator.mjs → init/index.mjs} +34 -27
  11. package/agents/{manage-prefs.mjs → prefs/index.mjs} +16 -16
  12. package/agents/publish/index.yaml +17 -0
  13. package/agents/{publish-docs.mjs → publish/publish-docs.mjs} +15 -16
  14. package/agents/schema/{structure-plan-result.yaml → document-execution-structure.yaml} +3 -3
  15. package/agents/schema/document-structure.yaml +26 -0
  16. package/agents/{language-selector.mjs → translate/choose-language.mjs} +5 -5
  17. package/agents/{retranslate.yaml → translate/index.yaml} +17 -18
  18. package/agents/translate/translate-document.yaml +32 -0
  19. package/agents/{batch-translate.yaml → translate/translate-multilingual.yaml} +5 -5
  20. package/agents/update/batch-generate-document.yaml +19 -0
  21. package/agents/{check-detail.mjs → update/check-document.mjs} +16 -16
  22. package/agents/{detail-generator-and-translate.yaml → update/generate-and-translate-document.yaml} +23 -23
  23. package/agents/update/generate-document.yaml +50 -0
  24. package/agents/{detail-regenerator.yaml → update/index.yaml} +16 -17
  25. package/agents/{action-success.mjs → utils/action-success.mjs} +2 -2
  26. package/agents/{check-detail-result.mjs → utils/check-detail-result.mjs} +3 -3
  27. package/agents/{check-feedback-refiner.mjs → utils/check-feedback-refiner.mjs} +6 -6
  28. package/agents/{find-items-by-paths.mjs → utils/choose-docs.mjs} +25 -10
  29. package/agents/{docs-fs.yaml → utils/docs-fs-actor.yaml} +3 -1
  30. package/agents/utils/feedback-refiner.yaml +50 -0
  31. package/agents/{find-item-by-path.mjs → utils/find-item-by-path.mjs} +17 -7
  32. package/agents/{find-user-preferences-by-path.mjs → utils/find-user-preferences-by-path.mjs} +1 -1
  33. package/agents/utils/format-document-structure.mjs +25 -0
  34. package/agents/{load-sources.mjs → utils/load-sources.mjs} +41 -28
  35. package/agents/{save-docs.mjs → utils/save-docs.mjs} +16 -16
  36. package/agents/{save-single-doc.mjs → utils/save-single-doc.mjs} +2 -2
  37. package/agents/{transform-detail-datasources.mjs → utils/transform-detail-datasources.mjs} +1 -1
  38. package/aigne.yaml +35 -35
  39. package/docs/cli-reference.md +1 -1
  40. package/docs/features-generate-documentation.md +1 -1
  41. package/docs/features-update-and-refine.md +2 -2
  42. package/docs-mcp/analyze-docs-relevance.yaml +10 -10
  43. package/docs-mcp/docs-search.yaml +5 -3
  44. package/package.json +10 -8
  45. package/prompts/{document → detail/custom}/custom-code-block.md +6 -6
  46. package/prompts/detail/custom/custom-components.md +172 -0
  47. package/prompts/{document → detail}/d2-chart/rules.md +95 -1
  48. package/prompts/{document → detail}/detail-example.md +80 -61
  49. package/prompts/{document/detail-generator.md → detail/document-rules.md} +4 -8
  50. package/prompts/{content-detail-generator.md → detail/generate-document.md} +48 -25
  51. package/prompts/{check-structure-planning-result.md → structure/check-document-structure.md} +23 -17
  52. package/prompts/{document/structure-planning.md → structure/document-rules.md} +0 -2
  53. package/prompts/{structure-planning.md → structure/generate-structure.md} +51 -30
  54. package/prompts/{document → structure}/structure-example.md +2 -2
  55. package/prompts/{document → structure}/structure-getting-started.md +2 -2
  56. package/prompts/translate/glossary.md +6 -0
  57. package/prompts/{translator.md → translate/translate-document.md} +29 -10
  58. package/prompts/{feedback-refiner.md → utils/feedback-refiner.md} +8 -8
  59. package/tests/agents/chat/chat.test.mjs +46 -0
  60. package/tests/agents/generate/check-document-structure.test.mjs +51 -0
  61. package/tests/agents/generate/check-need-generate-structure.test.mjs +292 -0
  62. package/tests/agents/generate/generate-structure.test.mjs +51 -0
  63. package/tests/{input-generator.test.mjs → agents/init/init.test.mjs} +19 -17
  64. package/tests/agents/prefs/prefs.test.mjs +431 -0
  65. package/tests/agents/publish/publish-docs.test.mjs +642 -0
  66. package/tests/agents/translate/choose-language.test.mjs +311 -0
  67. package/tests/agents/translate/translate-document.test.mjs +51 -0
  68. package/tests/agents/update/check-document.test.mjs +523 -0
  69. package/tests/agents/update/generate-document.test.mjs +51 -0
  70. package/tests/agents/utils/action-success.test.mjs +54 -0
  71. package/tests/{check-detail-result.test.mjs → agents/utils/check-detail-result.test.mjs} +98 -98
  72. package/tests/agents/utils/check-feedback-refiner.test.mjs +478 -0
  73. package/tests/agents/utils/choose-docs.test.mjs +417 -0
  74. package/tests/agents/utils/exit.test.mjs +70 -0
  75. package/tests/agents/utils/feedback-refiner.test.mjs +51 -0
  76. package/tests/agents/utils/find-item-by-path.test.mjs +526 -0
  77. package/tests/agents/utils/find-user-preferences-by-path.test.mjs +382 -0
  78. package/tests/agents/utils/format-document-structure.test.mjs +264 -0
  79. package/tests/agents/utils/fs.test.mjs +267 -0
  80. package/tests/{load-sources.test.mjs → agents/utils/load-sources.test.mjs} +153 -25
  81. package/tests/{save-docs.test.mjs → agents/utils/save-docs.test.mjs} +11 -5
  82. package/tests/agents/utils/save-output.test.mjs +315 -0
  83. package/tests/agents/utils/save-single-doc.test.mjs +364 -0
  84. package/tests/agents/utils/transform-detail-datasources.test.mjs +363 -0
  85. package/tests/utils/auth-utils.test.mjs +358 -0
  86. package/tests/utils/blocklet.test.mjs +334 -0
  87. package/tests/{conflict-resolution.test.mjs → utils/conflict-detector.test.mjs} +3 -3
  88. package/tests/utils/constants.test.mjs +295 -0
  89. package/tests/utils/d2-utils.test.mjs +423 -0
  90. package/tests/utils/deploy.test.mjs +365 -0
  91. package/tests/utils/docs-finder-utils.test.mjs +625 -0
  92. package/tests/utils/file-utils.test.mjs +213 -0
  93. package/tests/{kroki-utils.test.mjs → utils/kroki-utils.test.mjs} +2 -2
  94. package/tests/utils/load-config.test.mjs +141 -0
  95. package/tests/{mermaid-validation.test.mjs → utils/mermaid-validator.test.mjs} +2 -2
  96. package/tests/utils/mock-chat-model.mjs +12 -0
  97. package/tests/{preferences-utils.test.mjs → utils/preferences-utils.test.mjs} +1 -1
  98. package/tests/{save-value-to-config.test.mjs → utils/save-value-to-config.test.mjs} +61 -4
  99. package/tests/utils/utils.test.mjs +939 -0
  100. package/utils/auth-utils.mjs +1 -1
  101. package/utils/conflict-detector.mjs +1 -1
  102. package/utils/constants.mjs +5 -3
  103. package/utils/d2-utils.mjs +194 -0
  104. package/utils/deploy.mjs +3 -3
  105. package/utils/docs-finder-utils.mjs +26 -26
  106. package/utils/icon-map.mjs +26 -0
  107. package/{agents → utils}/load-config.mjs +2 -18
  108. package/utils/markdown-checker.mjs +5 -5
  109. package/agents/batch-docs-detail-generator.yaml +0 -19
  110. package/agents/check-structure-planning-result.yaml +0 -30
  111. package/agents/content-detail-generator.yaml +0 -50
  112. package/agents/feedback-refiner.yaml +0 -52
  113. package/agents/format-structure-plan.mjs +0 -25
  114. package/agents/reflective-structure-planner.yaml +0 -12
  115. package/agents/schema/structure-plan.yaml +0 -26
  116. package/agents/structure-planning.yaml +0 -58
  117. package/agents/team-publish-docs.yaml +0 -18
  118. package/agents/translate.yaml +0 -31
  119. package/prompts/document/custom-components.md +0 -104
  120. package/tests/README.md +0 -93
  121. package/tests/utils.test.mjs +0 -2067
  122. /package/agents/{exit.mjs → utils/exit.mjs} +0 -0
  123. /package/agents/{fs.mjs → utils/fs.mjs} +0 -0
  124. /package/agents/{save-output.mjs → utils/save-output.mjs} +0 -0
  125. /package/prompts/{document → detail}/d2-chart/official-examples.md +0 -0
  126. /package/prompts/{document → detail}/jsx/rules.md +0 -0
@@ -0,0 +1,311 @@
1
+ import { afterEach, beforeEach, describe, expect, mock, spyOn, test } from "bun:test";
2
+ import chooseLanguage from "../../../agents/translate/choose-language.mjs";
3
+
4
+ import * as utils from "../../../utils/utils.mjs";
5
+
6
+ describe("choose-language", () => {
7
+ let mockOptions;
8
+
9
+ // Spies for internal utils
10
+ let loadConfigFromFileSpy;
11
+ let saveValueToConfigSpy;
12
+ let consoleSpy;
13
+
14
+ beforeEach(() => {
15
+ mock.restore();
16
+
17
+ mockOptions = {
18
+ prompts: {
19
+ checkbox: mock(async () => ["zh", "ja"]),
20
+ },
21
+ };
22
+
23
+ // Set up spies for internal utils
24
+ loadConfigFromFileSpy = spyOn(utils, "loadConfigFromFile").mockResolvedValue({});
25
+ saveValueToConfigSpy = spyOn(utils, "saveValueToConfig").mockResolvedValue();
26
+ consoleSpy = spyOn(console, "log").mockImplementation(() => {});
27
+
28
+ // Clear prompts mock call history
29
+ mockOptions.prompts.checkbox.mockClear();
30
+ });
31
+
32
+ afterEach(() => {
33
+ // Restore all spies
34
+ loadConfigFromFileSpy?.mockRestore();
35
+ saveValueToConfigSpy?.mockRestore();
36
+ consoleSpy?.mockRestore();
37
+ });
38
+
39
+ // BASIC FUNCTIONALITY TESTS
40
+ test("should return selected languages with provided langs parameter", async () => {
41
+ const selectedDocs = [
42
+ { path: "/doc1", title: "Document 1" },
43
+ { path: "/doc2", title: "Document 2" },
44
+ ];
45
+
46
+ const result = await chooseLanguage(
47
+ {
48
+ langs: ["zh", "ja"],
49
+ locale: "en",
50
+ selectedDocs,
51
+ },
52
+ mockOptions,
53
+ );
54
+
55
+ expect(result.selectedLanguages).toEqual(["zh", "ja"]);
56
+ expect(result.selectedDocs).toHaveLength(2);
57
+ expect(result.selectedDocs[0].translates).toEqual([{ language: "zh" }, { language: "ja" }]);
58
+ expect(mockOptions.prompts.checkbox).not.toHaveBeenCalled();
59
+ });
60
+
61
+ test("should prompt for language selection when no langs provided", async () => {
62
+ const selectedDocs = [{ path: "/doc1", title: "Document 1" }];
63
+
64
+ const result = await chooseLanguage(
65
+ {
66
+ locale: "en",
67
+ selectedDocs,
68
+ },
69
+ mockOptions,
70
+ );
71
+
72
+ expect(mockOptions.prompts.checkbox).toHaveBeenCalledWith(
73
+ expect.objectContaining({
74
+ message: "Select translation languages:",
75
+ choices: expect.any(Array),
76
+ validate: expect.any(Function),
77
+ }),
78
+ );
79
+ expect(result.selectedLanguages).toEqual(["zh", "ja"]);
80
+ });
81
+
82
+ test("should exclude primary language from available choices", async () => {
83
+ const selectedDocs = [{ path: "/doc1", title: "Document 1" }];
84
+
85
+ await chooseLanguage(
86
+ {
87
+ locale: "zh",
88
+ selectedDocs,
89
+ },
90
+ mockOptions,
91
+ );
92
+
93
+ const choices = mockOptions.prompts.checkbox.mock.calls[0][0].choices;
94
+ const zhChoice = choices.find((choice) => choice.value === "zh");
95
+ expect(zhChoice).toBeUndefined();
96
+
97
+ // Should have English available since zh is primary
98
+ const enChoice = choices.find((choice) => choice.value === "en");
99
+ expect(enChoice).toBeDefined();
100
+ });
101
+
102
+ test("should use existing config for primary language when not provided", async () => {
103
+ loadConfigFromFileSpy.mockResolvedValue({ locale: "es" });
104
+ const selectedDocs = [{ path: "/doc1", title: "Document 1" }];
105
+
106
+ await chooseLanguage({ selectedDocs }, mockOptions);
107
+
108
+ const choices = mockOptions.prompts.checkbox.mock.calls[0][0].choices;
109
+ const esChoice = choices.find((choice) => choice.value === "es");
110
+ expect(esChoice).toBeUndefined(); // Spanish should be excluded as primary
111
+ });
112
+
113
+ test("should default to 'en' when no locale in config or parameter", async () => {
114
+ loadConfigFromFileSpy.mockResolvedValue({});
115
+ const selectedDocs = [{ path: "/doc1", title: "Document 1" }];
116
+
117
+ await chooseLanguage({ selectedDocs }, mockOptions);
118
+
119
+ const choices = mockOptions.prompts.checkbox.mock.calls[0][0].choices;
120
+ const enChoice = choices.find((choice) => choice.value === "en");
121
+ expect(enChoice).toBeUndefined(); // English should be excluded as primary
122
+ });
123
+
124
+ // VALIDATION TESTS
125
+ test("should validate checkbox selection requires at least one language", async () => {
126
+ const selectedDocs = [{ path: "/doc1", title: "Document 1" }];
127
+
128
+ await chooseLanguage({ locale: "en", selectedDocs }, mockOptions);
129
+
130
+ const validateFn = mockOptions.prompts.checkbox.mock.calls[0][0].validate;
131
+ expect(validateFn([])).toBe("Please select at least one language");
132
+ expect(validateFn(["zh"])).toBe(true);
133
+ });
134
+
135
+ test("should filter out invalid languages from langs parameter", async () => {
136
+ const selectedDocs = [{ path: "/doc1", title: "Document 1" }];
137
+
138
+ const result = await chooseLanguage(
139
+ {
140
+ langs: ["zh", "invalid-lang", "ja", "another-invalid"],
141
+ locale: "en",
142
+ selectedDocs,
143
+ },
144
+ mockOptions,
145
+ );
146
+
147
+ expect(result.selectedLanguages).toEqual(["zh", "ja"]);
148
+ expect(consoleSpy).not.toHaveBeenCalled(); // Valid languages were found
149
+ });
150
+
151
+ test("should show warning and prompt when all provided langs are invalid", async () => {
152
+ mockOptions.prompts.checkbox.mockResolvedValue(["fr"]);
153
+ const selectedDocs = [{ path: "/doc1", title: "Document 1" }];
154
+
155
+ const result = await chooseLanguage(
156
+ {
157
+ langs: ["invalid1", "invalid2"],
158
+ locale: "en",
159
+ selectedDocs,
160
+ },
161
+ mockOptions,
162
+ );
163
+
164
+ expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Invalid languages provided"));
165
+ expect(mockOptions.prompts.checkbox).toHaveBeenCalled();
166
+ expect(result.selectedLanguages).toEqual(["fr"]);
167
+ });
168
+
169
+ test("should throw error when no languages are selected", async () => {
170
+ mockOptions.prompts.checkbox.mockResolvedValue([]);
171
+ const selectedDocs = [{ path: "/doc1", title: "Document 1" }];
172
+
173
+ await expect(chooseLanguage({ locale: "en", selectedDocs }, mockOptions)).rejects.toThrow(
174
+ "No languages selected for translation",
175
+ );
176
+ });
177
+
178
+ // CONFIG MANAGEMENT TESTS
179
+ test("should save new languages to config", async () => {
180
+ loadConfigFromFileSpy.mockResolvedValue({
181
+ translateLanguages: ["zh"],
182
+ });
183
+ const selectedDocs = [{ path: "/doc1", title: "Document 1" }];
184
+
185
+ await chooseLanguage(
186
+ {
187
+ langs: ["zh", "ja", "fr"], // ja and fr are new
188
+ locale: "en",
189
+ selectedDocs,
190
+ },
191
+ mockOptions,
192
+ );
193
+
194
+ expect(saveValueToConfigSpy).toHaveBeenCalledWith("translateLanguages", ["zh", "ja", "fr"]);
195
+ });
196
+
197
+ test("should not save to config when no new languages", async () => {
198
+ loadConfigFromFileSpy.mockResolvedValue({
199
+ translateLanguages: ["zh", "ja"],
200
+ });
201
+ const selectedDocs = [{ path: "/doc1", title: "Document 1" }];
202
+
203
+ await chooseLanguage(
204
+ {
205
+ langs: ["zh", "ja"], // All existing
206
+ locale: "en",
207
+ selectedDocs,
208
+ },
209
+ mockOptions,
210
+ );
211
+
212
+ expect(saveValueToConfigSpy).not.toHaveBeenCalled();
213
+ });
214
+
215
+ test("should mark previously selected languages as checked in prompt", async () => {
216
+ loadConfigFromFileSpy.mockResolvedValue({
217
+ translateLanguages: ["zh", "ja"],
218
+ });
219
+ const selectedDocs = [{ path: "/doc1", title: "Document 1" }];
220
+
221
+ await chooseLanguage({ locale: "en", selectedDocs }, mockOptions);
222
+
223
+ const choices = mockOptions.prompts.checkbox.mock.calls[0][0].choices;
224
+ const zhChoice = choices.find((choice) => choice.value === "zh");
225
+ const jaChoice = choices.find((choice) => choice.value === "ja");
226
+ const frChoice = choices.find((choice) => choice.value === "fr");
227
+
228
+ expect(zhChoice.checked).toBe(true);
229
+ expect(jaChoice.checked).toBe(true);
230
+ expect(frChoice.checked).toBe(false);
231
+ });
232
+
233
+ // DOCUMENT PROCESSING TESTS
234
+ test("should add translation info to selected documents", async () => {
235
+ const selectedDocs = [
236
+ { path: "/doc1", title: "Document 1", existing: "data" },
237
+ { path: "/doc2", title: "Document 2" },
238
+ ];
239
+
240
+ const result = await chooseLanguage(
241
+ {
242
+ langs: ["zh", "ja"],
243
+ locale: "en",
244
+ selectedDocs,
245
+ },
246
+ mockOptions,
247
+ );
248
+
249
+ expect(result.selectedDocs).toEqual([
250
+ {
251
+ path: "/doc1",
252
+ title: "Document 1",
253
+ existing: "data",
254
+ translates: [{ language: "zh" }, { language: "ja" }],
255
+ },
256
+ {
257
+ path: "/doc2",
258
+ title: "Document 2",
259
+ translates: [{ language: "zh" }, { language: "ja" }],
260
+ },
261
+ ]);
262
+ });
263
+
264
+ // EDGE CASES
265
+ test("should handle empty selectedDocs array", async () => {
266
+ const result = await chooseLanguage(
267
+ {
268
+ langs: ["zh"],
269
+ locale: "en",
270
+ selectedDocs: [],
271
+ },
272
+ mockOptions,
273
+ );
274
+
275
+ expect(result.selectedDocs).toEqual([]);
276
+ expect(result.selectedLanguages).toEqual(["zh"]);
277
+ });
278
+
279
+ test("should handle empty langs array", async () => {
280
+ mockOptions.prompts.checkbox.mockResolvedValue(["fr"]);
281
+ const selectedDocs = [{ path: "/doc1", title: "Document 1" }];
282
+
283
+ const result = await chooseLanguage(
284
+ {
285
+ langs: [],
286
+ locale: "en",
287
+ selectedDocs,
288
+ },
289
+ mockOptions,
290
+ );
291
+
292
+ expect(mockOptions.prompts.checkbox).toHaveBeenCalled();
293
+ expect(result.selectedLanguages).toEqual(["fr"]);
294
+ });
295
+
296
+ test("should handle missing config file", async () => {
297
+ loadConfigFromFileSpy.mockResolvedValue(null);
298
+ const selectedDocs = [{ path: "/doc1", title: "Document 1" }];
299
+
300
+ const result = await chooseLanguage(
301
+ {
302
+ langs: ["zh"],
303
+ locale: "en",
304
+ selectedDocs,
305
+ },
306
+ mockOptions,
307
+ );
308
+
309
+ expect(result.selectedLanguages).toEqual(["zh"]);
310
+ });
311
+ });
@@ -0,0 +1,51 @@
1
+ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2
+ import { join } from "node:path";
3
+ import { AIAgent } from "@aigne/core";
4
+ import { loadAgent } from "@aigne/core/loader/index.js";
5
+ import { loadModel } from "../../utils/mock-chat-model.mjs";
6
+
7
+ describe("translateDocument Agent", () => {
8
+ beforeAll(() => {
9
+ process.env.AIGNE_OBSERVABILITY_DISABLED = "true";
10
+ });
11
+
12
+ afterAll(() => {
13
+ delete process.env.AIGNE_OBSERVABILITY_DISABLED;
14
+ });
15
+ test("should load agent correctly with proper configuration", async () => {
16
+ const agent = await loadAgent(
17
+ join(import.meta.dirname, "../../../agents/translate/translate-document.yaml"),
18
+ {
19
+ model: loadModel,
20
+ },
21
+ );
22
+
23
+ expect(agent).toBeDefined();
24
+
25
+ // Verify agent exists and is correct type
26
+ expect(agent).toBeDefined();
27
+ expect(agent).toBeInstanceOf(AIAgent);
28
+ expect(agent.name).toBe("translateDocument");
29
+ });
30
+
31
+ test("should have instructions loaded from file", async () => {
32
+ const agent = await loadAgent(
33
+ join(import.meta.dirname, "../../../agents/translate/translate-document.yaml"),
34
+ {
35
+ model: loadModel,
36
+ },
37
+ );
38
+
39
+ expect(agent).toBeDefined();
40
+
41
+ // Verify instructions are loaded
42
+ expect(agent.instructions).toBeDefined();
43
+ const instructions = await agent.instructions.build({});
44
+ expect(instructions.messages).toBeDefined();
45
+ expect(instructions.messages.length).toBeGreaterThan(0);
46
+
47
+ // The instructions should contain content from the prompt file
48
+ const systemMessage = instructions.messages.find((m) => m.role === "system");
49
+ expect(systemMessage).toBeDefined();
50
+ });
51
+ });