specweave 0.22.14 → 0.23.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.
Files changed (211) hide show
  1. package/CLAUDE.md +178 -1
  2. package/dist/src/cli/commands/import-external.d.ts +22 -0
  3. package/dist/src/cli/commands/import-external.d.ts.map +1 -0
  4. package/dist/src/cli/commands/import-external.js +282 -0
  5. package/dist/src/cli/commands/import-external.js.map +1 -0
  6. package/dist/src/cli/commands/init.d.ts.map +1 -1
  7. package/dist/src/cli/commands/init.js +46 -0
  8. package/dist/src/cli/commands/init.js.map +1 -1
  9. package/dist/src/cli/helpers/github-repo-selector.d.ts +59 -0
  10. package/dist/src/cli/helpers/github-repo-selector.d.ts.map +1 -0
  11. package/dist/src/cli/helpers/github-repo-selector.js +265 -0
  12. package/dist/src/cli/helpers/github-repo-selector.js.map +1 -0
  13. package/dist/src/cli/helpers/issue-tracker/index.d.ts.map +1 -1
  14. package/dist/src/cli/helpers/issue-tracker/index.js +5 -17
  15. package/dist/src/cli/helpers/issue-tracker/index.js.map +1 -1
  16. package/dist/src/config/types.d.ts +16 -16
  17. package/dist/src/core/increment/ac-status-manager.d.ts.map +1 -1
  18. package/dist/src/core/increment/ac-status-manager.js +4 -2
  19. package/dist/src/core/increment/ac-status-manager.js.map +1 -1
  20. package/dist/src/core/increment/completion-validator.d.ts +30 -1
  21. package/dist/src/core/increment/completion-validator.d.ts.map +1 -1
  22. package/dist/src/core/increment/completion-validator.js +151 -3
  23. package/dist/src/core/increment/completion-validator.js.map +1 -1
  24. package/dist/src/core/increment/increment-archiver.d.ts +25 -0
  25. package/dist/src/core/increment/increment-archiver.d.ts.map +1 -1
  26. package/dist/src/core/increment/increment-archiver.js +130 -3
  27. package/dist/src/core/increment/increment-archiver.js.map +1 -1
  28. package/dist/src/core/living-docs/feature-archiver.d.ts +37 -0
  29. package/dist/src/core/living-docs/feature-archiver.d.ts.map +1 -1
  30. package/dist/src/core/living-docs/feature-archiver.js +262 -18
  31. package/dist/src/core/living-docs/feature-archiver.js.map +1 -1
  32. package/dist/src/core/living-docs/feature-id-manager.d.ts +17 -0
  33. package/dist/src/core/living-docs/feature-id-manager.d.ts.map +1 -1
  34. package/dist/src/core/living-docs/feature-id-manager.js +25 -0
  35. package/dist/src/core/living-docs/feature-id-manager.js.map +1 -1
  36. package/dist/src/core/living-docs/living-docs-sync.d.ts +14 -0
  37. package/dist/src/core/living-docs/living-docs-sync.d.ts.map +1 -1
  38. package/dist/src/core/living-docs/living-docs-sync.js +46 -0
  39. package/dist/src/core/living-docs/living-docs-sync.js.map +1 -1
  40. package/dist/src/core/repo-structure/repo-id-generator.d.ts +20 -0
  41. package/dist/src/core/repo-structure/repo-id-generator.d.ts.map +1 -1
  42. package/dist/src/core/repo-structure/repo-id-generator.js +44 -0
  43. package/dist/src/core/repo-structure/repo-id-generator.js.map +1 -1
  44. package/dist/src/core/repo-structure/repo-structure-manager.d.ts.map +1 -1
  45. package/dist/src/core/repo-structure/repo-structure-manager.js +5 -2
  46. package/dist/src/core/repo-structure/repo-structure-manager.js.map +1 -1
  47. package/dist/src/core/sync/sync-event-logger.d.ts +15 -1
  48. package/dist/src/core/sync/sync-event-logger.d.ts.map +1 -1
  49. package/dist/src/core/sync/sync-event-logger.js +39 -1
  50. package/dist/src/core/sync/sync-event-logger.js.map +1 -1
  51. package/dist/src/core/types/sync-config-validator.d.ts +57 -0
  52. package/dist/src/core/types/sync-config-validator.d.ts.map +1 -0
  53. package/dist/src/core/types/sync-config-validator.js +116 -0
  54. package/dist/src/core/types/sync-config-validator.js.map +1 -0
  55. package/dist/src/importers/duplicate-detector.d.ts +107 -0
  56. package/dist/src/importers/duplicate-detector.d.ts.map +1 -0
  57. package/dist/src/importers/duplicate-detector.js +189 -0
  58. package/dist/src/importers/duplicate-detector.js.map +1 -0
  59. package/dist/src/importers/import-coordinator.d.ts +15 -0
  60. package/dist/src/importers/import-coordinator.d.ts.map +1 -1
  61. package/dist/src/importers/import-coordinator.js +43 -1
  62. package/dist/src/importers/import-coordinator.js.map +1 -1
  63. package/dist/src/importers/item-converter.d.ts +5 -0
  64. package/dist/src/importers/item-converter.d.ts.map +1 -1
  65. package/dist/src/importers/item-converter.js +27 -2
  66. package/dist/src/importers/item-converter.js.map +1 -1
  67. package/dist/src/importers/rate-limiter.d.ts +128 -0
  68. package/dist/src/importers/rate-limiter.d.ts.map +1 -0
  69. package/dist/src/importers/rate-limiter.js +200 -0
  70. package/dist/src/importers/rate-limiter.js.map +1 -0
  71. package/dist/src/init/compliance/types.d.ts +2 -2
  72. package/dist/src/integrations/ado/ado-client.d.ts +6 -0
  73. package/dist/src/integrations/ado/ado-client.d.ts.map +1 -1
  74. package/dist/src/integrations/ado/ado-client.js +23 -0
  75. package/dist/src/integrations/ado/ado-client.js.map +1 -1
  76. package/dist/src/integrations/jira/jira-client.d.ts +6 -0
  77. package/dist/src/integrations/jira/jira-client.d.ts.map +1 -1
  78. package/dist/src/integrations/jira/jira-client.js +38 -0
  79. package/dist/src/integrations/jira/jira-client.js.map +1 -1
  80. package/dist/src/sync/external-item-sync-service.d.ts +150 -0
  81. package/dist/src/sync/external-item-sync-service.d.ts.map +1 -0
  82. package/dist/src/sync/external-item-sync-service.js +241 -0
  83. package/dist/src/sync/external-item-sync-service.js.map +1 -0
  84. package/dist/src/sync/format-preservation-sync.d.ts +90 -0
  85. package/dist/src/sync/format-preservation-sync.d.ts.map +1 -0
  86. package/dist/src/sync/format-preservation-sync.js +173 -0
  87. package/dist/src/sync/format-preservation-sync.js.map +1 -0
  88. package/dist/src/sync/index.d.ts +8 -0
  89. package/dist/src/sync/index.d.ts.map +1 -0
  90. package/dist/src/sync/index.js +6 -0
  91. package/dist/src/sync/index.js.map +1 -0
  92. package/dist/src/sync/sync-coordinator.d.ts +49 -0
  93. package/dist/src/sync/sync-coordinator.d.ts.map +1 -0
  94. package/dist/src/sync/sync-coordinator.js +248 -0
  95. package/dist/src/sync/sync-coordinator.js.map +1 -0
  96. package/dist/src/sync/sync-metadata.d.ts +75 -0
  97. package/dist/src/sync/sync-metadata.d.ts.map +1 -0
  98. package/dist/src/sync/sync-metadata.js +100 -0
  99. package/dist/src/sync/sync-metadata.js.map +1 -0
  100. package/dist/src/types/living-docs-us-file.d.ts +63 -0
  101. package/dist/src/types/living-docs-us-file.d.ts.map +1 -0
  102. package/dist/src/types/living-docs-us-file.js +27 -0
  103. package/dist/src/types/living-docs-us-file.js.map +1 -0
  104. package/dist/src/validators/format-preservation-validator.d.ts +127 -0
  105. package/dist/src/validators/format-preservation-validator.d.ts.map +1 -0
  106. package/dist/src/validators/format-preservation-validator.js +187 -0
  107. package/dist/src/validators/format-preservation-validator.js.map +1 -0
  108. package/package.json +3 -2
  109. package/plugins/specweave/.claude-plugin/plugin.json +20 -0
  110. package/plugins/specweave/commands/specweave-archive-features.md +11 -1
  111. package/plugins/specweave/commands/specweave-archive.md +51 -15
  112. package/plugins/specweave/commands/specweave-import-docs.md +88 -278
  113. package/plugins/specweave/commands/specweave-import-external.md +407 -0
  114. package/plugins/specweave/hooks/post-edit-spec.sh +94 -0
  115. package/plugins/specweave/hooks/post-increment-completion.sh +0 -0
  116. package/plugins/specweave/hooks/post-spec-update.sh +0 -0
  117. package/plugins/specweave/hooks/post-task-completion.sh +13 -3
  118. package/plugins/specweave/hooks/post-write-spec.sh +91 -0
  119. package/plugins/specweave/lib/hooks/auto-transition.js +1 -1
  120. package/plugins/specweave/lib/hooks/auto-transition.js.bak +50 -0
  121. package/plugins/specweave/lib/hooks/auto-transition.ts +1 -1
  122. package/plugins/specweave/lib/hooks/auto-transition.ts.bak +84 -0
  123. package/plugins/specweave/lib/hooks/git-diff-analyzer.d.js.bak +0 -0
  124. package/plugins/specweave/lib/hooks/git-diff-analyzer.d.ts.bak +89 -0
  125. package/plugins/specweave/lib/hooks/git-diff-analyzer.js.bak +142 -0
  126. package/plugins/specweave/lib/hooks/git-diff-analyzer.ts.bak +269 -0
  127. package/plugins/specweave/lib/hooks/invoke-translator-skill.d.js.bak +0 -0
  128. package/plugins/specweave/lib/hooks/invoke-translator-skill.d.ts.bak +60 -0
  129. package/plugins/specweave/lib/hooks/invoke-translator-skill.js +1 -1
  130. package/plugins/specweave/lib/hooks/invoke-translator-skill.js.bak +155 -0
  131. package/plugins/specweave/lib/hooks/invoke-translator-skill.ts +1 -1
  132. package/plugins/specweave/lib/hooks/invoke-translator-skill.ts.bak +264 -0
  133. package/plugins/specweave/lib/hooks/prepare-reflection-context.d.js.bak +0 -0
  134. package/plugins/specweave/lib/hooks/prepare-reflection-context.d.ts.bak +42 -0
  135. package/plugins/specweave/lib/hooks/prepare-reflection-context.js.bak +110 -0
  136. package/plugins/specweave/lib/hooks/prepare-reflection-context.ts.bak +178 -0
  137. package/plugins/specweave/lib/hooks/reflection-config-loader.d.js.bak +0 -0
  138. package/plugins/specweave/lib/hooks/reflection-config-loader.d.ts.bak +45 -0
  139. package/plugins/specweave/lib/hooks/reflection-config-loader.js.bak +92 -0
  140. package/plugins/specweave/lib/hooks/reflection-config-loader.ts.bak +156 -0
  141. package/plugins/specweave/lib/hooks/reflection-parser.d.js.bak +0 -0
  142. package/plugins/specweave/lib/hooks/reflection-parser.d.ts.bak +33 -0
  143. package/plugins/specweave/lib/hooks/reflection-parser.js.bak +301 -0
  144. package/plugins/specweave/lib/hooks/reflection-parser.ts.bak +484 -0
  145. package/plugins/specweave/lib/hooks/reflection-prompt-builder.d.js.bak +0 -0
  146. package/plugins/specweave/lib/hooks/reflection-prompt-builder.d.ts.bak +56 -0
  147. package/plugins/specweave/lib/hooks/reflection-prompt-builder.js.bak +182 -0
  148. package/plugins/specweave/lib/hooks/reflection-prompt-builder.ts.bak +306 -0
  149. package/plugins/specweave/lib/hooks/reflection-storage.d.js.bak +0 -0
  150. package/plugins/specweave/lib/hooks/reflection-storage.d.ts.bak +64 -0
  151. package/plugins/specweave/lib/hooks/reflection-storage.js.bak +231 -0
  152. package/plugins/specweave/lib/hooks/reflection-storage.ts.bak +369 -0
  153. package/plugins/specweave/lib/hooks/run-self-reflection.d.js.bak +0 -0
  154. package/plugins/specweave/lib/hooks/run-self-reflection.d.ts.bak +43 -0
  155. package/plugins/specweave/lib/hooks/run-self-reflection.js.bak +132 -0
  156. package/plugins/specweave/lib/hooks/run-self-reflection.ts.bak +258 -0
  157. package/plugins/specweave/lib/hooks/sync-cache.js.bak +294 -0
  158. package/plugins/specweave/lib/hooks/sync-living-docs.d.js.bak +1 -0
  159. package/plugins/specweave/lib/hooks/sync-living-docs.d.ts.bak +27 -0
  160. package/plugins/specweave/lib/hooks/sync-living-docs.js +35 -1
  161. package/plugins/specweave/lib/hooks/sync-living-docs.js.bak +339 -0
  162. package/plugins/specweave/lib/hooks/sync-us-tasks.js +179 -3
  163. package/plugins/specweave/lib/hooks/sync-us-tasks.js.bak +476 -0
  164. package/plugins/specweave/lib/hooks/translate-file.d.js.bak +0 -0
  165. package/plugins/specweave/lib/hooks/translate-file.d.ts.bak +59 -0
  166. package/plugins/specweave/lib/hooks/translate-file.js +1 -1
  167. package/plugins/specweave/lib/hooks/translate-file.js.bak +289 -0
  168. package/plugins/specweave/lib/hooks/translate-file.ts +1 -1
  169. package/plugins/specweave/lib/hooks/translate-file.ts.bak +428 -0
  170. package/plugins/specweave/lib/hooks/translate-living-docs.d.js.bak +0 -0
  171. package/plugins/specweave/lib/hooks/translate-living-docs.d.ts.bak +13 -0
  172. package/plugins/specweave/lib/hooks/translate-living-docs.js.bak +119 -0
  173. package/plugins/specweave/lib/hooks/translate-living-docs.ts.bak +224 -0
  174. package/plugins/specweave/lib/hooks/update-ac-status.js +1 -1
  175. package/plugins/specweave/lib/hooks/update-ac-status.js.bak +51 -0
  176. package/plugins/specweave/lib/hooks/update-ac-status.ts +1 -1
  177. package/plugins/specweave/lib/hooks/update-ac-status.ts.bak +103 -0
  178. package/plugins/specweave/lib/hooks/update-tasks-md.d.js.bak +1 -0
  179. package/plugins/specweave/lib/hooks/update-tasks-md.d.ts.bak +29 -0
  180. package/plugins/specweave/lib/hooks/update-tasks-md.js.bak +296 -0
  181. package/plugins/specweave/lib/hooks/update-tasks-md.ts.bak +489 -0
  182. package/plugins/specweave/lib/vendor/core/increment/ac-status-manager.d.ts +115 -0
  183. package/plugins/specweave/lib/vendor/core/increment/ac-status-manager.js +345 -0
  184. package/plugins/specweave/lib/vendor/core/increment/ac-status-manager.js.map +1 -0
  185. package/plugins/specweave/lib/vendor/core/increment/active-increment-manager.d.ts +106 -0
  186. package/plugins/specweave/lib/vendor/core/increment/active-increment-manager.js +220 -0
  187. package/plugins/specweave/lib/vendor/core/increment/active-increment-manager.js.map +1 -0
  188. package/plugins/specweave/lib/vendor/core/increment/auto-transition-manager.d.ts +60 -0
  189. package/plugins/specweave/lib/vendor/core/increment/auto-transition-manager.js +192 -0
  190. package/plugins/specweave/lib/vendor/core/increment/auto-transition-manager.js.map +1 -0
  191. package/plugins/specweave/lib/vendor/core/increment/duplicate-detector.d.ts +52 -0
  192. package/plugins/specweave/lib/vendor/core/increment/duplicate-detector.js +276 -0
  193. package/plugins/specweave/lib/vendor/core/increment/duplicate-detector.js.map +1 -0
  194. package/plugins/specweave/lib/vendor/core/increment/metadata-manager.d.ts +163 -0
  195. package/plugins/specweave/lib/vendor/core/increment/metadata-manager.js +541 -0
  196. package/plugins/specweave/lib/vendor/core/increment/metadata-manager.js.map +1 -0
  197. package/plugins/specweave/lib/vendor/core/types/increment-metadata.d.ts +157 -0
  198. package/plugins/specweave/lib/vendor/core/types/increment-metadata.js +191 -0
  199. package/plugins/specweave/lib/vendor/core/types/increment-metadata.js.map +1 -0
  200. package/plugins/specweave/lib/vendor/generators/spec/task-parser.d.ts +95 -0
  201. package/plugins/specweave/lib/vendor/generators/spec/task-parser.js +301 -0
  202. package/plugins/specweave/lib/vendor/generators/spec/task-parser.js.map +1 -0
  203. package/plugins/specweave/lib/vendor/utils/logger.d.ts +48 -0
  204. package/plugins/specweave/lib/vendor/utils/logger.js +53 -0
  205. package/plugins/specweave/lib/vendor/utils/logger.js.map +1 -0
  206. package/plugins/specweave/lib/vendor/utils/translation.d.ts +187 -0
  207. package/plugins/specweave/lib/vendor/utils/translation.js +414 -0
  208. package/plugins/specweave/lib/vendor/utils/translation.js.map +1 -0
  209. package/plugins/specweave-github/commands/specweave-github-update-user-story.md +1 -1
  210. package/plugins/specweave-github/skills/github-issue-standard/SKILL.md +1 -1
  211. package/plugins/specweave-release/hooks/.specweave/logs/dora-tracking.log +531 -0
@@ -0,0 +1,155 @@
1
+ import fs from "fs-extra";
2
+ import {
3
+ detectLanguage,
4
+ prepareTranslation,
5
+ postProcessTranslation,
6
+ getLanguageName
7
+ } from "../../../../dist/src/utils/translation.js";
8
+ async function invokeTranslatorSkill(content, sourceLang, targetLang = "en") {
9
+ try {
10
+ const prepared = prepareTranslation(content, sourceLang, targetLang);
11
+ const result = {
12
+ success: true,
13
+ sourceLanguage: sourceLang,
14
+ targetLanguage: targetLang,
15
+ originalContent: content,
16
+ translatedContent: await performTranslation(prepared.prompt, prepared.preserved)
17
+ };
18
+ return result;
19
+ } catch (error) {
20
+ const errorMessage = error instanceof Error ? error.message : String(error);
21
+ return {
22
+ success: false,
23
+ sourceLanguage: sourceLang,
24
+ targetLanguage: targetLang,
25
+ originalContent: content,
26
+ error: errorMessage
27
+ };
28
+ }
29
+ }
30
+ async function performTranslation(prompt, preserved) {
31
+ const contentMatch = prompt.match(/SOURCE DOCUMENT[^\n]*:\n---\n([\s\S]*?)\n---/);
32
+ const contentToTranslate = contentMatch ? contentMatch[1] : "";
33
+ const apiKey = process.env.ANTHROPIC_API_KEY;
34
+ if (apiKey) {
35
+ console.log(`
36
+ \u{1F916} Translating via Anthropic API (Haiku model)...`);
37
+ try {
38
+ const Anthropic = await import("@anthropic-ai/sdk").then((m) => m.default);
39
+ const anthropic = new Anthropic({
40
+ apiKey
41
+ });
42
+ const message = await anthropic.messages.create({
43
+ model: "claude-3-haiku-20240307",
44
+ max_tokens: 8e3,
45
+ messages: [
46
+ {
47
+ role: "user",
48
+ content: prompt
49
+ }
50
+ ]
51
+ });
52
+ const translatedContent = message.content[0].type === "text" ? message.content[0].text : contentToTranslate;
53
+ console.log(`\u2705 Translation complete via API`);
54
+ console.log(` Input tokens: ${message.usage.input_tokens}`);
55
+ console.log(` Output tokens: ${message.usage.output_tokens}`);
56
+ console.log(` Cost: ~$${((message.usage.input_tokens * 0.25 + message.usage.output_tokens * 1.25) / 1e6).toFixed(4)}
57
+ `);
58
+ return postProcessTranslation(translatedContent, preserved);
59
+ } catch (error) {
60
+ console.error(`
61
+ \u274C API translation failed: ${error.message}`);
62
+ console.error(` Falling back to manual translation instructions
63
+ `);
64
+ }
65
+ }
66
+ const isInteractive = process.env.CLAUDE_CODE_SESSION === "true";
67
+ if (isInteractive) {
68
+ console.log("\n" + "=".repeat(80));
69
+ console.log("\u{1F310} TRANSLATION REQUEST (translator skill will auto-activate)");
70
+ console.log("=".repeat(80));
71
+ console.log(prompt);
72
+ console.log("=".repeat(80));
73
+ console.log("\u{1F4A1} Tip: Set ANTHROPIC_API_KEY for fully automatic translation\n");
74
+ return postProcessTranslation(
75
+ `<!-- \u26A0\uFE0F TRANSLATION REQUESTED - Awaiting translator skill activation -->
76
+
77
+ ${contentToTranslate}`,
78
+ preserved
79
+ );
80
+ } else {
81
+ console.error("\n\u26A0\uFE0F AUTO-TRANSLATION REQUIRES ONE OF:");
82
+ console.error(" Option A (Recommended): Set ANTHROPIC_API_KEY environment variable");
83
+ console.error(" Option B: Run /specweave:translate <file-path>");
84
+ console.error(" Option C: Manually translate the content\n");
85
+ return postProcessTranslation(
86
+ `<!-- \u26A0\uFE0F AUTO-TRANSLATION PENDING -->
87
+ <!-- Set ANTHROPIC_API_KEY for automatic translation -->
88
+ <!-- Or run: /specweave:translate to complete -->
89
+ <!-- Original content below -->
90
+
91
+ ${contentToTranslate}`,
92
+ preserved
93
+ );
94
+ }
95
+ }
96
+ async function translateFile(filePath, targetLang = "en") {
97
+ if (!await fs.pathExists(filePath)) {
98
+ throw new Error(`File not found: ${filePath}`);
99
+ }
100
+ const content = await fs.readFile(filePath, "utf-8");
101
+ const detection = detectLanguage(content);
102
+ const sourceLang = detection.language;
103
+ if (sourceLang === targetLang) {
104
+ return {
105
+ success: true,
106
+ filePath,
107
+ sourceLanguage: sourceLang,
108
+ targetLanguage: targetLang,
109
+ originalContent: content,
110
+ translatedContent: content
111
+ };
112
+ }
113
+ const result = await invokeTranslatorSkill(content, sourceLang, targetLang);
114
+ if (result.success && result.translatedContent) {
115
+ await fs.writeFile(filePath, result.translatedContent, "utf-8");
116
+ console.log(`\u2705 Translated: ${filePath} (${getLanguageName(sourceLang)} \u2192 ${getLanguageName(targetLang)})`);
117
+ }
118
+ return {
119
+ ...result,
120
+ filePath
121
+ };
122
+ }
123
+ async function batchTranslateFiles(filePaths, targetLang = "en") {
124
+ const results = [];
125
+ console.log(`
126
+ \u{1F310} Batch translating ${filePaths.length} file(s) to ${getLanguageName(targetLang)}...
127
+ `);
128
+ for (const filePath of filePaths) {
129
+ try {
130
+ const result = await translateFile(filePath, targetLang);
131
+ results.push(result);
132
+ } catch (error) {
133
+ const errorMessage = error instanceof Error ? error.message : String(error);
134
+ console.error(`\u274C Failed to translate ${filePath}: ${errorMessage}`);
135
+ results.push({
136
+ success: false,
137
+ filePath,
138
+ sourceLanguage: "unknown",
139
+ targetLanguage: targetLang,
140
+ originalContent: "",
141
+ error: errorMessage
142
+ });
143
+ }
144
+ }
145
+ const successful = results.filter((r) => r.success).length;
146
+ console.log(`
147
+ \u{1F4CA} Batch translation complete: ${successful}/${filePaths.length} files translated
148
+ `);
149
+ return results;
150
+ }
151
+ export {
152
+ batchTranslateFiles,
153
+ invokeTranslatorSkill,
154
+ translateFile
155
+ };
@@ -16,7 +16,7 @@ import {
16
16
  postProcessTranslation,
17
17
  getLanguageName,
18
18
  type SupportedLanguage,
19
- } from '../../../../dist/src/utils/translation.js';
19
+ } from '../vendor/utils/translation.js';
20
20
 
21
21
  /**
22
22
  * Translation result
@@ -0,0 +1,264 @@
1
+ /**
2
+ * Translator Skill Invocation Utility
3
+ *
4
+ * Provides programmatic invocation of the translator skill for automated translation.
5
+ * Used by hooks and CLI scripts to translate content without manual intervention.
6
+ *
7
+ * @see plugins/specweave/skills/translator/SKILL.md
8
+ * @see plugins/specweave/commands/translate.md
9
+ */
10
+
11
+ import fs from 'fs-extra';
12
+ import path from 'path';
13
+ import {
14
+ detectLanguage,
15
+ prepareTranslation,
16
+ postProcessTranslation,
17
+ getLanguageName,
18
+ type SupportedLanguage,
19
+ } from '../../../../dist/src/utils/translation.js';
20
+
21
+ /**
22
+ * Translation result
23
+ */
24
+ export interface TranslationResult {
25
+ success: boolean;
26
+ sourceLanguage: SupportedLanguage;
27
+ targetLanguage: SupportedLanguage;
28
+ originalContent: string;
29
+ translatedContent?: string;
30
+ error?: string;
31
+ }
32
+
33
+ /**
34
+ * Invokes translator skill to translate content
35
+ *
36
+ * This function integrates with the translator skill by:
37
+ * 1. Preparing translation prompt
38
+ * 2. Writing prompt to a temp file
39
+ * 3. Outputting instructions for Claude to process
40
+ * 4. Returning the translated content
41
+ *
42
+ * In an automated context (hooks), this provides clear instructions.
43
+ * In an interactive context, the translator skill can auto-activate.
44
+ *
45
+ * @param content - Content to translate
46
+ * @param sourceLang - Source language
47
+ * @param targetLang - Target language
48
+ * @returns Translation result
49
+ */
50
+ export async function invokeTranslatorSkill(
51
+ content: string,
52
+ sourceLang: SupportedLanguage,
53
+ targetLang: SupportedLanguage = 'en'
54
+ ): Promise<TranslationResult> {
55
+ try {
56
+ // Prepare translation
57
+ const prepared = prepareTranslation(content, sourceLang, targetLang);
58
+
59
+ // For now, we use a practical approach:
60
+ // Output the translation request clearly so it can be processed
61
+
62
+ const result: TranslationResult = {
63
+ success: true,
64
+ sourceLanguage: sourceLang,
65
+ targetLanguage: targetLang,
66
+ originalContent: content,
67
+ translatedContent: await performTranslation(prepared.prompt, prepared.preserved),
68
+ };
69
+
70
+ return result;
71
+ } catch (error) {
72
+ const errorMessage = error instanceof Error ? error.message : String(error);
73
+ return {
74
+ success: false,
75
+ sourceLanguage: sourceLang,
76
+ targetLanguage: targetLang,
77
+ originalContent: content,
78
+ error: errorMessage,
79
+ };
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Performs the actual translation using Anthropic API
85
+ *
86
+ * PRODUCTION IMPLEMENTATION:
87
+ * 1. Checks for ANTHROPIC_API_KEY in environment
88
+ * 2. If available: Uses Anthropic API directly (fully automatic)
89
+ * 3. If not available: Provides clear instructions for manual translation
90
+ *
91
+ * @param prompt - Translation prompt
92
+ * @param preserved - Preserved content structure
93
+ * @returns Translated content
94
+ */
95
+ async function performTranslation(
96
+ prompt: string,
97
+ preserved: any
98
+ ): Promise<string> {
99
+ // Extract content for processing
100
+ const contentMatch = prompt.match(/SOURCE DOCUMENT[^\n]*:\n---\n([\s\S]*?)\n---/);
101
+ const contentToTranslate = contentMatch ? contentMatch[1] : '';
102
+
103
+ // Check if ANTHROPIC_API_KEY is available
104
+ const apiKey = process.env.ANTHROPIC_API_KEY;
105
+
106
+ if (apiKey) {
107
+ // Fully automatic translation using Anthropic API
108
+ console.log(`\n🤖 Translating via Anthropic API (Haiku model)...`);
109
+
110
+ try {
111
+ // Dynamic import of Anthropic SDK (allows graceful fallback if not installed)
112
+ const Anthropic = await import('@anthropic-ai/sdk').then(m => m.default);
113
+
114
+ const anthropic = new Anthropic({
115
+ apiKey,
116
+ });
117
+
118
+ const message = await anthropic.messages.create({
119
+ model: 'claude-3-haiku-20240307',
120
+ max_tokens: 8000,
121
+ messages: [
122
+ {
123
+ role: 'user',
124
+ content: prompt,
125
+ },
126
+ ],
127
+ });
128
+
129
+ // Extract translated content from response
130
+ const translatedContent = message.content[0].type === 'text'
131
+ ? message.content[0].text
132
+ : contentToTranslate;
133
+
134
+ console.log(`✅ Translation complete via API`);
135
+ console.log(` Input tokens: ${message.usage.input_tokens}`);
136
+ console.log(` Output tokens: ${message.usage.output_tokens}`);
137
+ console.log(` Cost: ~$${((message.usage.input_tokens * 0.25 + message.usage.output_tokens * 1.25) / 1000000).toFixed(4)}\n`);
138
+
139
+ return postProcessTranslation(translatedContent, preserved);
140
+ } catch (error: any) {
141
+ console.error(`\n❌ API translation failed: ${error.message}`);
142
+ console.error(` Falling back to manual translation instructions\n`);
143
+ // Fall through to manual instructions
144
+ }
145
+ }
146
+
147
+ // Fallback: Manual translation instructions
148
+ const isInteractive = process.env.CLAUDE_CODE_SESSION === 'true';
149
+
150
+ if (isInteractive) {
151
+ // In Claude Code environment, output translation request
152
+ console.log('\n' + '='.repeat(80));
153
+ console.log('🌐 TRANSLATION REQUEST (translator skill will auto-activate)');
154
+ console.log('='.repeat(80));
155
+ console.log(prompt);
156
+ console.log('='.repeat(80));
157
+ console.log('💡 Tip: Set ANTHROPIC_API_KEY for fully automatic translation\n');
158
+
159
+ return postProcessTranslation(
160
+ `<!-- ⚠️ TRANSLATION REQUESTED - Awaiting translator skill activation -->\n\n${contentToTranslate}`,
161
+ preserved
162
+ );
163
+ } else {
164
+ // Non-interactive environment - provide clear instructions
165
+ console.error('\n⚠️ AUTO-TRANSLATION REQUIRES ONE OF:');
166
+ console.error(' Option A (Recommended): Set ANTHROPIC_API_KEY environment variable');
167
+ console.error(' Option B: Run /specweave:translate <file-path>');
168
+ console.error(' Option C: Manually translate the content\n');
169
+
170
+ return postProcessTranslation(
171
+ `<!-- ⚠️ AUTO-TRANSLATION PENDING -->\n<!-- Set ANTHROPIC_API_KEY for automatic translation -->\n<!-- Or run: /specweave:translate to complete -->\n<!-- Original content below -->\n\n${contentToTranslate}`,
172
+ preserved
173
+ );
174
+ }
175
+ }
176
+
177
+ /**
178
+ * Translate a file using translator skill
179
+ *
180
+ * @param filePath - Path to file to translate
181
+ * @param targetLang - Target language
182
+ * @returns Translation result
183
+ */
184
+ export async function translateFile(
185
+ filePath: string,
186
+ targetLang: SupportedLanguage = 'en'
187
+ ): Promise<TranslationResult & { filePath: string }> {
188
+ // Read file
189
+ if (!await fs.pathExists(filePath)) {
190
+ throw new Error(`File not found: ${filePath}`);
191
+ }
192
+
193
+ const content = await fs.readFile(filePath, 'utf-8');
194
+
195
+ // Detect source language
196
+ const detection = detectLanguage(content);
197
+ const sourceLang = detection.language;
198
+
199
+ // Check if already in target language
200
+ if (sourceLang === targetLang) {
201
+ return {
202
+ success: true,
203
+ filePath,
204
+ sourceLanguage: sourceLang,
205
+ targetLanguage: targetLang,
206
+ originalContent: content,
207
+ translatedContent: content,
208
+ };
209
+ }
210
+
211
+ // Translate
212
+ const result = await invokeTranslatorSkill(content, sourceLang, targetLang);
213
+
214
+ // Write back if successful
215
+ if (result.success && result.translatedContent) {
216
+ await fs.writeFile(filePath, result.translatedContent, 'utf-8');
217
+ console.log(`✅ Translated: ${filePath} (${getLanguageName(sourceLang)} → ${getLanguageName(targetLang)})`);
218
+ }
219
+
220
+ return {
221
+ ...result,
222
+ filePath,
223
+ };
224
+ }
225
+
226
+ /**
227
+ * Batch translate multiple files
228
+ *
229
+ * @param filePaths - Array of file paths
230
+ * @param targetLang - Target language
231
+ * @returns Array of translation results
232
+ */
233
+ export async function batchTranslateFiles(
234
+ filePaths: string[],
235
+ targetLang: SupportedLanguage = 'en'
236
+ ): Promise<Array<TranslationResult & { filePath: string }>> {
237
+ const results: Array<TranslationResult & { filePath: string }> = [];
238
+
239
+ console.log(`\n🌐 Batch translating ${filePaths.length} file(s) to ${getLanguageName(targetLang)}...\n`);
240
+
241
+ for (const filePath of filePaths) {
242
+ try {
243
+ const result = await translateFile(filePath, targetLang);
244
+ results.push(result);
245
+ } catch (error) {
246
+ const errorMessage = error instanceof Error ? error.message : String(error);
247
+ console.error(`❌ Failed to translate ${filePath}: ${errorMessage}`);
248
+ results.push({
249
+ success: false,
250
+ filePath,
251
+ sourceLanguage: 'unknown',
252
+ targetLanguage: targetLang,
253
+ originalContent: '',
254
+ error: errorMessage,
255
+ });
256
+ }
257
+ }
258
+
259
+ // Summary
260
+ const successful = results.filter(r => r.success).length;
261
+ console.log(`\n📊 Batch translation complete: ${successful}/${filePaths.length} files translated\n`);
262
+
263
+ return results;
264
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Prepare Reflection Context
3
+ *
4
+ * Prepares context for reflection and saves to temp file
5
+ * Used by post-task-completion hook to prepare for reflection invocation
6
+ *
7
+ * @module prepare-reflection-context
8
+ */
9
+ /**
10
+ * Prepare reflection context and save to temp file
11
+ * This allows the hook to prepare data without actually invoking the agent
12
+ *
13
+ * @param incrementId Increment identifier
14
+ * @param taskId Task identifier
15
+ * @param projectRoot Project root directory (optional)
16
+ * @returns Path to saved context file or null if reflection should be skipped
17
+ */
18
+ export declare function prepareReflectionContext(incrementId: string, taskId: string, projectRoot?: string): string | null;
19
+ /**
20
+ * Check if reflection context exists for an increment
21
+ *
22
+ * @param incrementId Increment identifier
23
+ * @param projectRoot Project root directory (optional)
24
+ * @returns True if context file exists
25
+ */
26
+ export declare function hasReflectionContext(incrementId: string, projectRoot?: string): boolean;
27
+ /**
28
+ * Read reflection context from file
29
+ *
30
+ * @param incrementId Increment identifier
31
+ * @param projectRoot Project root directory (optional)
32
+ * @returns Context data or null if not found
33
+ */
34
+ export declare function readReflectionContext(incrementId: string, projectRoot?: string): any | null;
35
+ /**
36
+ * Clear reflection context after reflection completes
37
+ *
38
+ * @param incrementId Increment identifier
39
+ * @param projectRoot Project root directory (optional)
40
+ */
41
+ export declare function clearReflectionContext(incrementId: string, projectRoot?: string): void;
42
+ //# sourceMappingURL=prepare-reflection-context.d.ts.map
@@ -0,0 +1,110 @@
1
+ import fs from "fs-extra";
2
+ import path from "path";
3
+ import { createReflectionContext } from "./run-self-reflection";
4
+ import { getModifiedFilesSummary } from "./git-diff-analyzer";
5
+ function prepareReflectionContext(incrementId, taskId, projectRoot) {
6
+ try {
7
+ const context = createReflectionContext(incrementId, taskId, projectRoot);
8
+ if (!context.config.enabled || context.modifiedFiles.length === 0) {
9
+ return null;
10
+ }
11
+ const rootDir = projectRoot || process.cwd();
12
+ const tempDir = path.join(rootDir, ".specweave", "increments", incrementId, "logs", "reflections", ".temp");
13
+ fs.mkdirpSync(tempDir);
14
+ const contextFile = path.join(tempDir, "reflection-context.json");
15
+ const fileStats = getModifiedFilesSummary(context.modifiedFiles);
16
+ const contextData = {
17
+ incrementId: context.incrementId,
18
+ taskId: context.taskId,
19
+ modifiedFiles: context.modifiedFiles.map((f) => ({
20
+ file: f.file,
21
+ linesAdded: f.linesAdded,
22
+ linesRemoved: f.linesRemoved
23
+ // Exclude diff content to save space
24
+ })),
25
+ fileSummary: {
26
+ count: fileStats.count,
27
+ linesAdded: fileStats.linesAdded,
28
+ linesRemoved: fileStats.linesRemoved
29
+ },
30
+ config: context.config,
31
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
32
+ };
33
+ fs.writeJsonSync(contextFile, contextData, { spaces: 2 });
34
+ return contextFile;
35
+ } catch (error) {
36
+ console.error(`Failed to prepare reflection context: ${error.message}`);
37
+ return null;
38
+ }
39
+ }
40
+ function hasReflectionContext(incrementId, projectRoot) {
41
+ const rootDir = projectRoot || process.cwd();
42
+ const contextFile = path.join(
43
+ rootDir,
44
+ ".specweave",
45
+ "increments",
46
+ incrementId,
47
+ "logs",
48
+ "reflections",
49
+ ".temp",
50
+ "reflection-context.json"
51
+ );
52
+ return fs.existsSync(contextFile);
53
+ }
54
+ function readReflectionContext(incrementId, projectRoot) {
55
+ const rootDir = projectRoot || process.cwd();
56
+ const contextFile = path.join(
57
+ rootDir,
58
+ ".specweave",
59
+ "increments",
60
+ incrementId,
61
+ "logs",
62
+ "reflections",
63
+ ".temp",
64
+ "reflection-context.json"
65
+ );
66
+ if (!fs.existsSync(contextFile)) {
67
+ return null;
68
+ }
69
+ try {
70
+ return fs.readJsonSync(contextFile);
71
+ } catch {
72
+ return null;
73
+ }
74
+ }
75
+ function clearReflectionContext(incrementId, projectRoot) {
76
+ const rootDir = projectRoot || process.cwd();
77
+ const tempDir = path.join(
78
+ rootDir,
79
+ ".specweave",
80
+ "increments",
81
+ incrementId,
82
+ "logs",
83
+ "reflections",
84
+ ".temp"
85
+ );
86
+ if (fs.existsSync(tempDir)) {
87
+ fs.removeSync(tempDir);
88
+ }
89
+ }
90
+ if (require.main === module) {
91
+ const incrementId = process.argv[2];
92
+ const taskId = process.argv[3];
93
+ if (!incrementId || !taskId) {
94
+ console.error("Usage: node prepare-reflection-context.js <increment-id> <task-id>");
95
+ process.exit(1);
96
+ }
97
+ const contextFile = prepareReflectionContext(incrementId, taskId);
98
+ if (contextFile) {
99
+ console.log(`Reflection context prepared: ${contextFile}`);
100
+ console.log("\u2728 Reflection ready. Run /specweave:reflect to analyze your work.");
101
+ } else {
102
+ console.log("Reflection skipped (disabled or no changes).");
103
+ }
104
+ }
105
+ export {
106
+ clearReflectionContext,
107
+ hasReflectionContext,
108
+ prepareReflectionContext,
109
+ readReflectionContext
110
+ };