llm-content-creator 0.1.0

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 (438) hide show
  1. package/README.md +309 -0
  2. package/dist/application/workflow/SyncExecutor.d.ts +75 -0
  3. package/dist/application/workflow/SyncExecutor.d.ts.map +1 -0
  4. package/dist/application/workflow/SyncExecutor.js +370 -0
  5. package/dist/application/workflow/SyncExecutor.js.map +1 -0
  6. package/dist/application/workflow/types.d.ts +46 -0
  7. package/dist/application/workflow/types.d.ts.map +1 -0
  8. package/dist/application/workflow/types.js +7 -0
  9. package/dist/application/workflow/types.js.map +1 -0
  10. package/dist/config/index.d.ts +173 -0
  11. package/dist/config/index.d.ts.map +1 -0
  12. package/dist/config/index.js +288 -0
  13. package/dist/config/index.js.map +1 -0
  14. package/dist/domain/entities/QualityCheck.d.ts +181 -0
  15. package/dist/domain/entities/QualityCheck.d.ts.map +1 -0
  16. package/dist/domain/entities/QualityCheck.js +39 -0
  17. package/dist/domain/entities/QualityCheck.js.map +1 -0
  18. package/dist/domain/entities/Result.d.ts +103 -0
  19. package/dist/domain/entities/Result.d.ts.map +1 -0
  20. package/dist/domain/entities/Result.js +15 -0
  21. package/dist/domain/entities/Result.js.map +1 -0
  22. package/dist/domain/entities/Task.d.ts +130 -0
  23. package/dist/domain/entities/Task.d.ts.map +1 -0
  24. package/dist/domain/entities/Task.js +64 -0
  25. package/dist/domain/entities/Task.js.map +1 -0
  26. package/dist/domain/entities/TaskStep.d.ts +160 -0
  27. package/dist/domain/entities/TaskStep.d.ts.map +1 -0
  28. package/dist/domain/entities/TaskStep.js +30 -0
  29. package/dist/domain/entities/TaskStep.js.map +1 -0
  30. package/dist/domain/entities/TokenUsage.d.ts +70 -0
  31. package/dist/domain/entities/TokenUsage.d.ts.map +1 -0
  32. package/dist/domain/entities/TokenUsage.js +42 -0
  33. package/dist/domain/entities/TokenUsage.js.map +1 -0
  34. package/dist/domain/entities/index.d.ts +11 -0
  35. package/dist/domain/entities/index.d.ts.map +1 -0
  36. package/dist/domain/entities/index.js +16 -0
  37. package/dist/domain/entities/index.js.map +1 -0
  38. package/dist/domain/repositories/QualityCheckRepository.d.ts +49 -0
  39. package/dist/domain/repositories/QualityCheckRepository.d.ts.map +1 -0
  40. package/dist/domain/repositories/QualityCheckRepository.js +5 -0
  41. package/dist/domain/repositories/QualityCheckRepository.js.map +1 -0
  42. package/dist/domain/repositories/ResultRepository.d.ts +43 -0
  43. package/dist/domain/repositories/ResultRepository.d.ts.map +1 -0
  44. package/dist/domain/repositories/ResultRepository.js +5 -0
  45. package/dist/domain/repositories/ResultRepository.js.map +1 -0
  46. package/dist/domain/repositories/TaskRepository.d.ts +240 -0
  47. package/dist/domain/repositories/TaskRepository.d.ts.map +1 -0
  48. package/dist/domain/repositories/TaskRepository.js +7 -0
  49. package/dist/domain/repositories/TaskRepository.js.map +1 -0
  50. package/dist/domain/workflow/CheckpointManager.d.ts +94 -0
  51. package/dist/domain/workflow/CheckpointManager.d.ts.map +1 -0
  52. package/dist/domain/workflow/CheckpointManager.js +224 -0
  53. package/dist/domain/workflow/CheckpointManager.js.map +1 -0
  54. package/dist/domain/workflow/ContentCreatorGraph.d.ts +17 -0
  55. package/dist/domain/workflow/ContentCreatorGraph.d.ts.map +1 -0
  56. package/dist/domain/workflow/ContentCreatorGraph.js +381 -0
  57. package/dist/domain/workflow/ContentCreatorGraph.js.map +1 -0
  58. package/dist/domain/workflow/State.d.ts +172 -0
  59. package/dist/domain/workflow/State.d.ts.map +1 -0
  60. package/dist/domain/workflow/State.js +184 -0
  61. package/dist/domain/workflow/State.js.map +1 -0
  62. package/dist/domain/workflow/index.d.ts +11 -0
  63. package/dist/domain/workflow/index.d.ts.map +1 -0
  64. package/dist/domain/workflow/index.js +15 -0
  65. package/dist/domain/workflow/index.js.map +1 -0
  66. package/dist/domain/workflow/nodes/BaseNode.d.ts +134 -0
  67. package/dist/domain/workflow/nodes/BaseNode.d.ts.map +1 -0
  68. package/dist/domain/workflow/nodes/BaseNode.js +253 -0
  69. package/dist/domain/workflow/nodes/BaseNode.js.map +1 -0
  70. package/dist/domain/workflow/nodes/CheckImageNode.d.ts +43 -0
  71. package/dist/domain/workflow/nodes/CheckImageNode.d.ts.map +1 -0
  72. package/dist/domain/workflow/nodes/CheckImageNode.js +254 -0
  73. package/dist/domain/workflow/nodes/CheckImageNode.js.map +1 -0
  74. package/dist/domain/workflow/nodes/CheckTextNode.d.ts +66 -0
  75. package/dist/domain/workflow/nodes/CheckTextNode.d.ts.map +1 -0
  76. package/dist/domain/workflow/nodes/CheckTextNode.js +530 -0
  77. package/dist/domain/workflow/nodes/CheckTextNode.js.map +1 -0
  78. package/dist/domain/workflow/nodes/GenerateImageNode.d.ts +44 -0
  79. package/dist/domain/workflow/nodes/GenerateImageNode.d.ts.map +1 -0
  80. package/dist/domain/workflow/nodes/GenerateImageNode.js +272 -0
  81. package/dist/domain/workflow/nodes/GenerateImageNode.js.map +1 -0
  82. package/dist/domain/workflow/nodes/OrganizeNode.d.ts +49 -0
  83. package/dist/domain/workflow/nodes/OrganizeNode.d.ts.map +1 -0
  84. package/dist/domain/workflow/nodes/OrganizeNode.js +241 -0
  85. package/dist/domain/workflow/nodes/OrganizeNode.js.map +1 -0
  86. package/dist/domain/workflow/nodes/SearchNode.d.ts +48 -0
  87. package/dist/domain/workflow/nodes/SearchNode.d.ts.map +1 -0
  88. package/dist/domain/workflow/nodes/SearchNode.js +151 -0
  89. package/dist/domain/workflow/nodes/SearchNode.js.map +1 -0
  90. package/dist/domain/workflow/nodes/WriteNode.d.ts +68 -0
  91. package/dist/domain/workflow/nodes/WriteNode.d.ts.map +1 -0
  92. package/dist/domain/workflow/nodes/WriteNode.js +431 -0
  93. package/dist/domain/workflow/nodes/WriteNode.js.map +1 -0
  94. package/dist/domain/workflow/nodes/config/index.js +287 -0
  95. package/dist/domain/workflow/nodes/domain/entities/Task.js +68 -0
  96. package/dist/domain/workflow/nodes/domain/workflow/State.js +200 -0
  97. package/dist/domain/workflow/nodes/domain/workflow/nodes/BaseNode.js +328 -0
  98. package/dist/domain/workflow/nodes/domain/workflow/nodes/CheckTextNode.js +500 -0
  99. package/dist/domain/workflow/nodes/index.d.ts +13 -0
  100. package/dist/domain/workflow/nodes/index.d.ts.map +1 -0
  101. package/dist/domain/workflow/nodes/index.js +13 -0
  102. package/dist/domain/workflow/nodes/index.js.map +1 -0
  103. package/dist/domain/workflow/nodes/infrastructure/logging/logger.js +275 -0
  104. package/dist/domain/workflow/nodes/services/llm/EnhancedLLMService.js +559 -0
  105. package/dist/index.d.ts +24 -0
  106. package/dist/index.d.ts.map +1 -0
  107. package/dist/index.js +34 -0
  108. package/dist/index.js.map +1 -0
  109. package/dist/infrastructure/cache/CacheService.d.ts +139 -0
  110. package/dist/infrastructure/cache/CacheService.d.ts.map +1 -0
  111. package/dist/infrastructure/cache/CacheService.js +419 -0
  112. package/dist/infrastructure/cache/CacheService.js.map +1 -0
  113. package/dist/infrastructure/cache/index.d.ts +5 -0
  114. package/dist/infrastructure/cache/index.d.ts.map +1 -0
  115. package/dist/infrastructure/cache/index.js +6 -0
  116. package/dist/infrastructure/cache/index.js.map +1 -0
  117. package/dist/infrastructure/database/BaseRepository.d.ts +98 -0
  118. package/dist/infrastructure/database/BaseRepository.d.ts.map +1 -0
  119. package/dist/infrastructure/database/BaseRepository.js +178 -0
  120. package/dist/infrastructure/database/BaseRepository.js.map +1 -0
  121. package/dist/infrastructure/database/MemoryTaskRepository.d.ts +77 -0
  122. package/dist/infrastructure/database/MemoryTaskRepository.d.ts.map +1 -0
  123. package/dist/infrastructure/database/MemoryTaskRepository.js +309 -0
  124. package/dist/infrastructure/database/MemoryTaskRepository.js.map +1 -0
  125. package/dist/infrastructure/database/PostgresQualityCheckRepository.d.ts +36 -0
  126. package/dist/infrastructure/database/PostgresQualityCheckRepository.d.ts.map +1 -0
  127. package/dist/infrastructure/database/PostgresQualityCheckRepository.js +89 -0
  128. package/dist/infrastructure/database/PostgresQualityCheckRepository.js.map +1 -0
  129. package/dist/infrastructure/database/PostgresTaskRepository.d.ts +94 -0
  130. package/dist/infrastructure/database/PostgresTaskRepository.d.ts.map +1 -0
  131. package/dist/infrastructure/database/PostgresTaskRepository.js +364 -0
  132. package/dist/infrastructure/database/PostgresTaskRepository.js.map +1 -0
  133. package/dist/infrastructure/database/ResultRepository.d.ts +41 -0
  134. package/dist/infrastructure/database/ResultRepository.d.ts.map +1 -0
  135. package/dist/infrastructure/database/ResultRepository.js +86 -0
  136. package/dist/infrastructure/database/ResultRepository.js.map +1 -0
  137. package/dist/infrastructure/database/SQLiteTaskRepository.d.ts +101 -0
  138. package/dist/infrastructure/database/SQLiteTaskRepository.d.ts.map +1 -0
  139. package/dist/infrastructure/database/SQLiteTaskRepository.js +548 -0
  140. package/dist/infrastructure/database/SQLiteTaskRepository.js.map +1 -0
  141. package/dist/infrastructure/database/index.d.ts +32 -0
  142. package/dist/infrastructure/database/index.d.ts.map +1 -0
  143. package/dist/infrastructure/database/index.js +72 -0
  144. package/dist/infrastructure/database/index.js.map +1 -0
  145. package/dist/infrastructure/logging/logger.d.ts +69 -0
  146. package/dist/infrastructure/logging/logger.d.ts.map +1 -0
  147. package/dist/infrastructure/logging/logger.js +169 -0
  148. package/dist/infrastructure/logging/logger.js.map +1 -0
  149. package/dist/infrastructure/monitoring/LoggingService.d.ts +109 -0
  150. package/dist/infrastructure/monitoring/LoggingService.d.ts.map +1 -0
  151. package/dist/infrastructure/monitoring/LoggingService.js +198 -0
  152. package/dist/infrastructure/monitoring/LoggingService.js.map +1 -0
  153. package/dist/infrastructure/monitoring/MetricsService.d.ts +112 -0
  154. package/dist/infrastructure/monitoring/MetricsService.d.ts.map +1 -0
  155. package/dist/infrastructure/monitoring/MetricsService.js +362 -0
  156. package/dist/infrastructure/monitoring/MetricsService.js.map +1 -0
  157. package/dist/infrastructure/monitoring/SentryService.d.ts +108 -0
  158. package/dist/infrastructure/monitoring/SentryService.d.ts.map +1 -0
  159. package/dist/infrastructure/monitoring/SentryService.js +282 -0
  160. package/dist/infrastructure/monitoring/SentryService.js.map +1 -0
  161. package/dist/infrastructure/monitoring/index.d.ts +7 -0
  162. package/dist/infrastructure/monitoring/index.d.ts.map +1 -0
  163. package/dist/infrastructure/monitoring/index.js +10 -0
  164. package/dist/infrastructure/monitoring/index.js.map +1 -0
  165. package/dist/infrastructure/queue/TaskQueue.d.ts +110 -0
  166. package/dist/infrastructure/queue/TaskQueue.d.ts.map +1 -0
  167. package/dist/infrastructure/queue/TaskQueue.js +363 -0
  168. package/dist/infrastructure/queue/TaskQueue.js.map +1 -0
  169. package/dist/infrastructure/queue/index.d.ts +5 -0
  170. package/dist/infrastructure/queue/index.d.ts.map +1 -0
  171. package/dist/infrastructure/queue/index.js +5 -0
  172. package/dist/infrastructure/queue/index.js.map +1 -0
  173. package/dist/infrastructure/redis/connection.d.ts +61 -0
  174. package/dist/infrastructure/redis/connection.d.ts.map +1 -0
  175. package/dist/infrastructure/redis/connection.js +184 -0
  176. package/dist/infrastructure/redis/connection.js.map +1 -0
  177. package/dist/infrastructure/redis/index.d.ts +5 -0
  178. package/dist/infrastructure/redis/index.d.ts.map +1 -0
  179. package/dist/infrastructure/redis/index.js +5 -0
  180. package/dist/infrastructure/redis/index.js.map +1 -0
  181. package/dist/infrastructure/security/ApiKeyService.d.ts +103 -0
  182. package/dist/infrastructure/security/ApiKeyService.d.ts.map +1 -0
  183. package/dist/infrastructure/security/ApiKeyService.js +250 -0
  184. package/dist/infrastructure/security/ApiKeyService.js.map +1 -0
  185. package/dist/infrastructure/security/QuotaService.d.ts +87 -0
  186. package/dist/infrastructure/security/QuotaService.d.ts.map +1 -0
  187. package/dist/infrastructure/security/QuotaService.js +303 -0
  188. package/dist/infrastructure/security/QuotaService.js.map +1 -0
  189. package/dist/infrastructure/security/RateLimiter.d.ts +104 -0
  190. package/dist/infrastructure/security/RateLimiter.d.ts.map +1 -0
  191. package/dist/infrastructure/security/RateLimiter.js +331 -0
  192. package/dist/infrastructure/security/RateLimiter.js.map +1 -0
  193. package/dist/infrastructure/security/index.d.ts +7 -0
  194. package/dist/infrastructure/security/index.d.ts.map +1 -0
  195. package/dist/infrastructure/security/index.js +10 -0
  196. package/dist/infrastructure/security/index.js.map +1 -0
  197. package/dist/monitoring/index.d.ts +5 -0
  198. package/dist/monitoring/index.d.ts.map +1 -0
  199. package/dist/monitoring/index.js +5 -0
  200. package/dist/monitoring/index.js.map +1 -0
  201. package/dist/monitoring/server.d.ts +14 -0
  202. package/dist/monitoring/server.d.ts.map +1 -0
  203. package/dist/monitoring/server.js +99 -0
  204. package/dist/monitoring/server.js.map +1 -0
  205. package/dist/presentation/cli/commands/cancel.d.ts +8 -0
  206. package/dist/presentation/cli/commands/cancel.d.ts.map +1 -0
  207. package/dist/presentation/cli/commands/cancel.js +57 -0
  208. package/dist/presentation/cli/commands/cancel.js.map +1 -0
  209. package/dist/presentation/cli/commands/create.d.ts +8 -0
  210. package/dist/presentation/cli/commands/create.d.ts.map +1 -0
  211. package/dist/presentation/cli/commands/create.js +368 -0
  212. package/dist/presentation/cli/commands/create.js.map +1 -0
  213. package/dist/presentation/cli/commands/result.d.ts +8 -0
  214. package/dist/presentation/cli/commands/result.d.ts.map +1 -0
  215. package/dist/presentation/cli/commands/result.js +121 -0
  216. package/dist/presentation/cli/commands/result.js.map +1 -0
  217. package/dist/presentation/cli/commands/status.d.ts +8 -0
  218. package/dist/presentation/cli/commands/status.d.ts.map +1 -0
  219. package/dist/presentation/cli/commands/status.js +92 -0
  220. package/dist/presentation/cli/commands/status.js.map +1 -0
  221. package/dist/presentation/cli/index.d.ts +8 -0
  222. package/dist/presentation/cli/index.d.ts.map +1 -0
  223. package/dist/presentation/cli/index.js +32 -0
  224. package/dist/presentation/cli/index.js.map +1 -0
  225. package/dist/presentation/cli/utils/cleanup.d.ts +14 -0
  226. package/dist/presentation/cli/utils/cleanup.d.ts.map +1 -0
  227. package/dist/presentation/cli/utils/cleanup.js +62 -0
  228. package/dist/presentation/cli/utils/cleanup.js.map +1 -0
  229. package/dist/presentation/cli/utils/formatter.d.ts +28 -0
  230. package/dist/presentation/cli/utils/formatter.d.ts.map +1 -0
  231. package/dist/presentation/cli/utils/formatter.js +68 -0
  232. package/dist/presentation/cli/utils/formatter.js.map +1 -0
  233. package/dist/presentation/cli.d.ts +7 -0
  234. package/dist/presentation/cli.d.ts.map +1 -0
  235. package/dist/presentation/cli.js +8 -0
  236. package/dist/presentation/cli.js.map +1 -0
  237. package/dist/presentation/monitor-cli.d.ts +8 -0
  238. package/dist/presentation/monitor-cli.d.ts.map +1 -0
  239. package/dist/presentation/monitor-cli.js +44 -0
  240. package/dist/presentation/monitor-cli.js.map +1 -0
  241. package/dist/presentation/worker-cli.d.ts +8 -0
  242. package/dist/presentation/worker-cli.d.ts.map +1 -0
  243. package/dist/presentation/worker-cli.js +51 -0
  244. package/dist/presentation/worker-cli.js.map +1 -0
  245. package/dist/schedulers/TaskScheduler.d.ts +99 -0
  246. package/dist/schedulers/TaskScheduler.d.ts.map +1 -0
  247. package/dist/schedulers/TaskScheduler.js +233 -0
  248. package/dist/schedulers/TaskScheduler.js.map +1 -0
  249. package/dist/schedulers/index.d.ts +5 -0
  250. package/dist/schedulers/index.d.ts.map +1 -0
  251. package/dist/schedulers/index.js +5 -0
  252. package/dist/schedulers/index.js.map +1 -0
  253. package/dist/services/image/ImageService.d.ts +68 -0
  254. package/dist/services/image/ImageService.d.ts.map +1 -0
  255. package/dist/services/image/ImageService.js +166 -0
  256. package/dist/services/image/ImageService.js.map +1 -0
  257. package/dist/services/index.d.ts +8 -0
  258. package/dist/services/index.d.ts.map +1 -0
  259. package/dist/services/index.js +12 -0
  260. package/dist/services/index.js.map +1 -0
  261. package/dist/services/llm/EnhancedLLMService.d.ts +148 -0
  262. package/dist/services/llm/EnhancedLLMService.d.ts.map +1 -0
  263. package/dist/services/llm/EnhancedLLMService.js +425 -0
  264. package/dist/services/llm/EnhancedLLMService.js.map +1 -0
  265. package/dist/services/llm/LLMService.d.ts +103 -0
  266. package/dist/services/llm/LLMService.d.ts.map +1 -0
  267. package/dist/services/llm/LLMService.js +212 -0
  268. package/dist/services/llm/LLMService.js.map +1 -0
  269. package/dist/services/quality/HardRuleChecker.d.ts +143 -0
  270. package/dist/services/quality/HardRuleChecker.d.ts.map +1 -0
  271. package/dist/services/quality/HardRuleChecker.js +353 -0
  272. package/dist/services/quality/HardRuleChecker.js.map +1 -0
  273. package/dist/services/quality/LLMEvaluator.d.ts +105 -0
  274. package/dist/services/quality/LLMEvaluator.d.ts.map +1 -0
  275. package/dist/services/quality/LLMEvaluator.js +312 -0
  276. package/dist/services/quality/LLMEvaluator.js.map +1 -0
  277. package/dist/services/quality/QualityCheckService.d.ts +112 -0
  278. package/dist/services/quality/QualityCheckService.d.ts.map +1 -0
  279. package/dist/services/quality/QualityCheckService.js +342 -0
  280. package/dist/services/quality/QualityCheckService.js.map +1 -0
  281. package/dist/services/quality/QualityService.d.ts +75 -0
  282. package/dist/services/quality/QualityService.d.ts.map +1 -0
  283. package/dist/services/quality/QualityService.js +360 -0
  284. package/dist/services/quality/QualityService.js.map +1 -0
  285. package/dist/services/quality/index.d.ts +7 -0
  286. package/dist/services/quality/index.d.ts.map +1 -0
  287. package/dist/services/quality/index.js +10 -0
  288. package/dist/services/quality/index.js.map +1 -0
  289. package/dist/services/search/SearchService.d.ts +79 -0
  290. package/dist/services/search/SearchService.d.ts.map +1 -0
  291. package/dist/services/search/SearchService.js +193 -0
  292. package/dist/services/search/SearchService.js.map +1 -0
  293. package/dist/workers/TaskWorker.d.ts +61 -0
  294. package/dist/workers/TaskWorker.d.ts.map +1 -0
  295. package/dist/workers/TaskWorker.js +256 -0
  296. package/dist/workers/TaskWorker.js.map +1 -0
  297. package/dist/workers/index.d.ts +5 -0
  298. package/dist/workers/index.d.ts.map +1 -0
  299. package/dist/workers/index.js +5 -0
  300. package/dist/workers/index.js.map +1 -0
  301. package/docs/DOCUMENTATION-ANALYSIS.md +190 -0
  302. package/docs/README.md +145 -0
  303. package/docs/SOURCE-CODE-ANALYSIS.md +1107 -0
  304. package/docs/architecture-complete.md +5524 -0
  305. package/docs/archive/implementation/implementation-analysis/README.md +244 -0
  306. package/docs/archive/implementation/implementation-analysis/implementation-analysis-context.md +483 -0
  307. package/docs/archive/implementation/implementation-analysis/implementation-analysis-plan.md +1242 -0
  308. package/docs/archive/implementation/implementation-analysis/implementation-analysis-tasks.md +777 -0
  309. package/docs/archive/phases/phase-1/phase-1-completion-summary.md +284 -0
  310. package/docs/archive/phases/phase-1/phase-1-implementation-guide.md +1380 -0
  311. package/docs/archive/phases/phase-2/phase-2a/phase-2a-completion-summary.md +443 -0
  312. package/docs/archive/phases/phase-2/phase-2b/phase-2b-completion-report.md +430 -0
  313. package/docs/archive/phases/phase-2/phase-2b/phase-2b-completion-summary.md +592 -0
  314. package/docs/archive/phases/phase-2/phase-2b/phase-2b-final-summary.md +371 -0
  315. package/docs/archive/phases/phase-2/phase-2b/phase-2b-preparation-complete.md +343 -0
  316. package/docs/archive/phases/phase-2/phase-2b/phase-2b-preparation.md +945 -0
  317. package/docs/archive/phases/phase-2/phase-2b/phase-2b-progress-update.md +366 -0
  318. package/docs/archive/phases/phase-3/phase-3-completion-summary.md +354 -0
  319. package/docs/archive/phases/phase-3/phase-3-development-plan.md +878 -0
  320. package/docs/archive/phases/phase-3/phase-3-quick-start.md +324 -0
  321. package/docs/archive/phases/phase-4/phase-4-completion-summary.md +708 -0
  322. package/docs/archive/phases/phase-4/phase-4-development-plan.md +740 -0
  323. package/docs/archive/phases/phase-4/phase-4-quick-start.md +632 -0
  324. package/docs/archive/phases/phase-4/phase-4-session-3-security-testing.md +484 -0
  325. package/docs/archive/phases/phase-4/phase-4-session-4-unit-tests.md +550 -0
  326. package/docs/archive/phases/phase-4/phase-4-session-5-security-tests.md +564 -0
  327. package/docs/archive/phases/phase-4/phase-4-session-6-cache-integration.md +456 -0
  328. package/docs/archive/phases/phase-4/phase-4-session-7-test-fixes.md +348 -0
  329. package/docs/archive/phases/phase-4/phase-4-session-8-taskqueue-fixes.md +323 -0
  330. package/docs/archive/phases/phase-4/phase-4-session-summary-continued.md +373 -0
  331. package/docs/archive/phases/phase-4/phase-4-session-summary.md +595 -0
  332. package/docs/archive/reports/progress-reports/PHASE_0_PROGRESS.md +242 -0
  333. package/docs/archive/reports/progress-reports/PHASE_0_SUMMARY.md +262 -0
  334. package/docs/archive/reports/progress-reports/PHASE_1_2_ISSUES.md +399 -0
  335. package/docs/archive/reports/progress-reports/PHASE_1_PROGRESS.md +388 -0
  336. package/docs/archive/reports/progress-reports/PHASE_3_PREPARATION.md +574 -0
  337. package/docs/archive/reports/progress-reports/current-progress-update.md +294 -0
  338. package/docs/archive/reports/progress-reports/final-summary.md +215 -0
  339. package/docs/archive/reports/progress-reports/implementation-summary.md +287 -0
  340. package/docs/archive/reports/progress-reports/project-progress-report.md +440 -0
  341. package/docs/archive/reports/progress-reports/project-progress.md +386 -0
  342. package/docs/archive/reports/test-reports/TEST-COVERAGE-REPORT.md +441 -0
  343. package/docs/archive/reports/test-reports/e2e-test-report.md +293 -0
  344. package/docs/archive/reports/test-reports/final-test-report.md +367 -0
  345. package/docs/archive/reports/test-reports/real-env-test-report.md +391 -0
  346. package/docs/archive/reports/test-reports/test-completion-summary.md +356 -0
  347. package/docs/archive/reports/test-reports/test-report.md +371 -0
  348. package/docs/archive/sessions/session-2-summary.md +429 -0
  349. package/docs/archive/sessions/session-3-summary.md +395 -0
  350. package/docs/archive/sessions/session-summary.md +370 -0
  351. package/docs/config-system-update.md +239 -0
  352. package/docs/database-refactoring-PLAN.md +199 -0
  353. package/docs/database-refactoring-SUMMARY.md +384 -0
  354. package/docs/quality-check-architecture.md +1030 -0
  355. package/docs/quick-start.md +388 -0
  356. package/docs/references/bullmq-quick-reference.md +525 -0
  357. package/docs/references/monitoring-optimization-guide.md +871 -0
  358. package/docs/references/performance-optimization-guide.md +933 -0
  359. package/docs/storage-guide.md +612 -0
  360. package/docs/test-implementation-PLAN.md +223 -0
  361. package/docs/test-implementation-SUMMARY.md +194 -0
  362. package/docs/user-guide.md +719 -0
  363. package/docs/workflow-architecture.md +549 -0
  364. package/package.json +126 -0
  365. package/src/application/workflow/SyncExecutor.ts +444 -0
  366. package/src/application/workflow/types.ts +57 -0
  367. package/src/config/index.ts +352 -0
  368. package/src/domain/entities/QualityCheck.ts +202 -0
  369. package/src/domain/entities/Result.ts +130 -0
  370. package/src/domain/entities/Task.ts +178 -0
  371. package/src/domain/entities/TaskStep.ts +188 -0
  372. package/src/domain/entities/TokenUsage.ts +119 -0
  373. package/src/domain/entities/index.ts +20 -0
  374. package/src/domain/repositories/QualityCheckRepository.ts +52 -0
  375. package/src/domain/repositories/ResultRepository.ts +47 -0
  376. package/src/domain/repositories/TaskRepository.ts +271 -0
  377. package/src/domain/workflow/CheckpointManager.ts +283 -0
  378. package/src/domain/workflow/ContentCreatorGraph.ts +446 -0
  379. package/src/domain/workflow/State.ts +321 -0
  380. package/src/domain/workflow/index.ts +18 -0
  381. package/src/domain/workflow/nodes/BaseNode.ts +325 -0
  382. package/src/domain/workflow/nodes/CheckImageNode.ts +325 -0
  383. package/src/domain/workflow/nodes/CheckTextNode.ts +709 -0
  384. package/src/domain/workflow/nodes/GenerateImageNode.ts +342 -0
  385. package/src/domain/workflow/nodes/OrganizeNode.ts +304 -0
  386. package/src/domain/workflow/nodes/SearchNode.ts +192 -0
  387. package/src/domain/workflow/nodes/WriteNode.ts +505 -0
  388. package/src/domain/workflow/nodes/index.ts +13 -0
  389. package/src/index.ts +43 -0
  390. package/src/infrastructure/cache/CacheService.ts +483 -0
  391. package/src/infrastructure/cache/index.ts +6 -0
  392. package/src/infrastructure/database/BaseRepository.ts +214 -0
  393. package/src/infrastructure/database/MemoryTaskRepository.ts +377 -0
  394. package/src/infrastructure/database/PostgresQualityCheckRepository.ts +115 -0
  395. package/src/infrastructure/database/PostgresTaskRepository.ts +424 -0
  396. package/src/infrastructure/database/ResultRepository.ts +113 -0
  397. package/src/infrastructure/database/SQLiteTaskRepository.ts +651 -0
  398. package/src/infrastructure/database/index.ts +83 -0
  399. package/src/infrastructure/logging/logger.ts +231 -0
  400. package/src/infrastructure/monitoring/LoggingService.ts +292 -0
  401. package/src/infrastructure/monitoring/MetricsService.ts +468 -0
  402. package/src/infrastructure/monitoring/SentryService.ts +345 -0
  403. package/src/infrastructure/monitoring/index.ts +12 -0
  404. package/src/infrastructure/queue/TaskQueue.ts +429 -0
  405. package/src/infrastructure/queue/index.ts +5 -0
  406. package/src/infrastructure/redis/connection.ts +215 -0
  407. package/src/infrastructure/redis/index.ts +5 -0
  408. package/src/infrastructure/security/ApiKeyService.ts +340 -0
  409. package/src/infrastructure/security/QuotaService.ts +411 -0
  410. package/src/infrastructure/security/RateLimiter.ts +417 -0
  411. package/src/infrastructure/security/index.ts +12 -0
  412. package/src/monitoring/index.ts +5 -0
  413. package/src/monitoring/server.ts +109 -0
  414. package/src/presentation/cli/commands/cancel.ts +64 -0
  415. package/src/presentation/cli/commands/create.ts +400 -0
  416. package/src/presentation/cli/commands/result.ts +136 -0
  417. package/src/presentation/cli/commands/status.ts +102 -0
  418. package/src/presentation/cli/index.ts +39 -0
  419. package/src/presentation/cli/utils/cleanup.ts +65 -0
  420. package/src/presentation/cli/utils/formatter.ts +74 -0
  421. package/src/presentation/cli.ts +8 -0
  422. package/src/presentation/monitor-cli.ts +52 -0
  423. package/src/presentation/worker-cli.ts +62 -0
  424. package/src/schedulers/TaskScheduler.ts +314 -0
  425. package/src/schedulers/index.ts +11 -0
  426. package/src/services/image/ImageService.ts +221 -0
  427. package/src/services/index.ts +15 -0
  428. package/src/services/llm/EnhancedLLMService.ts +596 -0
  429. package/src/services/llm/LLMService.ts +310 -0
  430. package/src/services/quality/HardRuleChecker.ts +509 -0
  431. package/src/services/quality/LLMEvaluator.ts +400 -0
  432. package/src/services/quality/QualityCheckService.ts +473 -0
  433. package/src/services/quality/QualityService.ts +445 -0
  434. package/src/services/quality/index.ts +12 -0
  435. package/src/services/search/SearchService.ts +266 -0
  436. package/src/types/global.d.ts +17 -0
  437. package/src/workers/TaskWorker.ts +320 -0
  438. package/src/workers/index.ts +5 -0
@@ -0,0 +1,1107 @@
1
+ # Content Creator 源码分析报告
2
+
3
+ **分析日期**: 2026-01-22
4
+ **项目版本**: v1.0
5
+ **代码行数**: 约 10,000+ 行
6
+ **技术栈**: TypeScript + Node.js + LangGraph
7
+
8
+ ---
9
+
10
+ ## 📋 目录
11
+
12
+ - [一、项目架构概览](#一项目架构概览)
13
+ - [二、目录结构分析](#二目录结构分析)
14
+ - [三、核心设计模式](#三核心设计模式)
15
+ - [四、各层实现分析](#四各层实现分析)
16
+ - [五、关键技术实现](#五关键技术实现)
17
+ - [六、代码质量评估](#六代码质量评估)
18
+ - [七、性能与可扩展性](#七性能与可扩展性)
19
+ - [八、建议与改进](#八建议与改进)
20
+
21
+ ---
22
+
23
+ ## 一、项目架构概览
24
+
25
+ ### 1.1 架构风格
26
+
27
+ 项目采用 **DDD(领域驱动设计)** + **分层架构** + **工作流引擎** 的混合架构:
28
+
29
+ ```
30
+ ┌─────────────────────────────────────────┐
31
+ │ Presentation Layer │
32
+ │ (CLI / Worker / Monitor) │
33
+ └─────────────────┬───────────────────────┘
34
+
35
+ ┌─────────────────▼───────────────────────┐
36
+ │ Application Layer │
37
+ │ (SyncExecutor / TaskWorker) │
38
+ └─────────────────┬───────────────────────┘
39
+
40
+ ┌─────────────────▼───────────────────────┐
41
+ │ Domain Layer │
42
+ │ (Entities / Workflow / Repositories) │
43
+ └─────────────────┬───────────────────────┘
44
+
45
+ ┌─────────────────▼───────────────────────┐
46
+ │ Infrastructure Layer │
47
+ │ (Database / Cache / Queue / Services) │
48
+ └─────────────────────────────────────────┘
49
+ ```
50
+
51
+ ### 1.2 核心组件
52
+
53
+ | 组件 | 职责 | 关键文件 |
54
+ |------|------|----------|
55
+ | **工作流引擎** | LangGraph 状态机 | `ContentCreatorGraph.ts` |
56
+ | **任务执行器** | 同步/异步任务执行 | `SyncExecutor.ts` / `TaskWorker.ts` |
57
+ | **节点系统** | 工作流节点 | `nodes/BaseNode.ts` |
58
+ | **状态管理** | 工作流状态 | `State.ts` |
59
+ | **数据持久化** | PostgreSQL 仓储 | `PostgresTaskRepository.ts` |
60
+ | **外部服务** | LLM/搜索/图片 | `services/` |
61
+
62
+ ---
63
+
64
+ ## 二、目录结构分析
65
+
66
+ ### 2.1 整体结构
67
+
68
+ ```
69
+ src/
70
+ ├── domain/ # 领域层(核心业务逻辑)
71
+ │ ├── entities/ # 实体(Task, Result, QualityCheck)
72
+ │ ├── repositories/ # 仓储接口
73
+ │ └── workflow/ # 工作流(LangGraph)
74
+ │ ├── nodes/ # 工作流节点
75
+ │ ├── State.ts # 状态定义
76
+ │ └── ContentCreatorGraph.ts
77
+ ├── application/ # 应用层(用例编排)
78
+ │ └── workflow/
79
+ │ ├── SyncExecutor.ts
80
+ │ └── types.ts
81
+ ├── infrastructure/ # 基础设施层
82
+ │ ├── database/ # 数据库实现
83
+ │ ├── cache/ # 缓存服务
84
+ │ ├── queue/ # 任务队列
85
+ │ ├── monitoring/ # 监控服务
86
+ │ ├── logging/ # 日志服务
87
+ │ └── security/ # 安全服务
88
+ ├── services/ # 服务层(外部集成)
89
+ │ ├── llm/ # LLM 服务
90
+ │ ├── search/ # 搜索服务
91
+ │ ├── image/ # 图片生成
92
+ │ ├── quality/ # 质量检查
93
+ │ └── token/ # Token 统计
94
+ ├── workers/ # Worker 实现
95
+ │ └── TaskWorker.ts
96
+ ├── schedulers/ # 调度器
97
+ │ └── TaskScheduler.ts
98
+ ├── presentation/ # 表现层
99
+ │ ├── cli/ # CLI 命令
100
+ │ └── monitor-cli.ts
101
+ └── config/ # 配置
102
+ └── index.ts
103
+ ```
104
+
105
+ ### 2.2 设计优点
106
+
107
+ ✅ **清晰的分层**:严格的层次边界,职责分明
108
+ ✅ **依赖倒置**:领域层不依赖基础设施层
109
+ ✅ **模块化**:每个模块职责单一,高内聚低耦合
110
+ ✅ **可测试性**:接口抽象,易于 mock 和测试
111
+
112
+ ### 2.3 潜在问题
113
+
114
+ ⚠️ **循环依赖风险**:`services/` 和 `infrastructure/` 边界模糊
115
+ ⚠️ **类型导出混乱**:部分类型在多处定义,容易造成不一致
116
+
117
+ ---
118
+
119
+ ## 三、核心设计模式
120
+
121
+ ### 3.1 仓储模式(Repository Pattern)
122
+
123
+ **定义位置**: `domain/repositories/`
124
+ **实现位置**: `infrastructure/database/`
125
+
126
+ ```typescript
127
+ // 领域层定义接口
128
+ interface ITaskRepository {
129
+ create(input: CreateTaskInput): Promise<Task>;
130
+ findById(taskId: string): Promise<Task | null>;
131
+ update(taskId: string, params: UpdateTaskParams): Promise<Task>;
132
+ }
133
+
134
+ // 基础设施层实现
135
+ class PostgresTaskRepository implements ITaskRepository {
136
+ async create(input: CreateTaskInput): Promise<Task> {
137
+ // PostgreSQL 实现
138
+ }
139
+ }
140
+ ```
141
+
142
+ **优点**:
143
+ - ✅ 领域层与数据访问解耦
144
+ - ✅ 易于切换数据库(PostgreSQL → MySQL)
145
+ - ✅ 便于单元测试(使用 Mock Repository)
146
+
147
+ ### 3.2 工作流节点模式(Workflow Node Pattern)
148
+
149
+ **定义位置**: `domain/workflow/nodes/BaseNode.ts`
150
+
151
+ ```typescript
152
+ abstract class BaseNode {
153
+ async execute(state: WorkflowState): Promise<NodeResult> {
154
+ // 1. 验证状态
155
+ this.validateState(state);
156
+
157
+ // 2. 执行节点逻辑(带超时)
158
+ const stateUpdate = await this.executeWithTimeout(state);
159
+
160
+ // 3. 返回结果
161
+ return { success: true, stateUpdate, duration };
162
+ }
163
+
164
+ protected abstract executeLogic(state: WorkflowState): Promise<Partial<WorkflowState>>;
165
+ }
166
+ ```
167
+
168
+ **具体节点**:
169
+ - `SearchNode` - 搜索节点
170
+ - `OrganizeNode` - 整理节点
171
+ - `WriteNode` - 写作节点
172
+ - `CheckTextNode` - 文本质检节点
173
+ - `GenerateImageNode` - 配图生成节点
174
+ - `CheckImageNode` - 配图质检节点
175
+
176
+ **优点**:
177
+ - ✅ 统一的节点执行流程
178
+ - ✅ 内置错误处理和超时控制
179
+ - ✅ 易于扩展新节点
180
+
181
+ ### 3.3 状态机模式(State Machine Pattern)
182
+
183
+ **定义位置**: `domain/workflow/ContentCreatorGraph.ts`
184
+
185
+ ```typescript
186
+ // LangGraph StateGraph
187
+ const graph = new StateGraph<WorkflowState>({
188
+ channels: {
189
+ taskId: { default: () => '' },
190
+ topic: { default: () => '' },
191
+ articleContent: { default: () => undefined },
192
+ // ...
193
+ }
194
+ });
195
+
196
+ // 添加节点
197
+ graph.addNode('search', searchNode);
198
+ graph.addNode('write', writeNode);
199
+
200
+ // 添加边
201
+ graph.addEdge('search', 'organize');
202
+ graph.addEdge('organize', 'write');
203
+
204
+ // 添加条件边(路由)
205
+ graph.addConditionalEdges('checkText', routeAfterCheckText, {
206
+ write: 'write', // 质检失败,重试写作
207
+ generate_image: 'generate_image', // 质检通过,生成配图
208
+ });
209
+ ```
210
+
211
+ **工作流流程**:
212
+ ```
213
+ search → organize → write → checkText
214
+
215
+ ┌─────────┴─────────┐
216
+ ↓ ↓
217
+ write generate_image
218
+ (重试写作) ↓
219
+ checkImage
220
+
221
+ ┌─────┴─────┐
222
+ ↓ ↓
223
+ generate_image __end__
224
+ (重试生成) (完成)
225
+ ```
226
+
227
+ ### 3.4 工厂模式(Factory Pattern)
228
+
229
+ **定义位置**: `infrastructure/database/index.ts`
230
+
231
+ ```typescript
232
+ export function createTaskRepository(): ITaskRepository {
233
+ const databaseType = config.database.type;
234
+
235
+ switch (databaseType) {
236
+ case 'postgres':
237
+ return new PostgresTaskRepository(pool);
238
+ case 'sqlite':
239
+ return new SQLiteTaskRepository(dbPath);
240
+ case 'memory':
241
+ return new MemoryTaskRepository();
242
+ default:
243
+ throw new Error(`Unsupported database type: ${databaseType}`);
244
+ }
245
+ }
246
+ ```
247
+
248
+ ### 3.5 策略模式(Strategy Pattern)
249
+
250
+ **定义位置**: `services/quality/`
251
+
252
+ ```typescript
253
+ interface QualityCheckStrategy {
254
+ check(content: string, constraints: any): Promise<QualityReport>;
255
+ }
256
+
257
+ // 硬规则检查策略
258
+ class HardRuleChecker implements QualityCheckStrategy {
259
+ async check(content: string, constraints: any): Promise<QualityReport> {
260
+ // 检查字数、关键词、结构等
261
+ }
262
+ }
263
+
264
+ // LLM 评估策略
265
+ class LLMEvaluator implements QualityCheckStrategy {
266
+ async check(content: string, constraints: any): Promise<QualityReport> {
267
+ // 调用 LLM 进行质量评估
268
+ }
269
+ }
270
+
271
+ // 质检服务组合策略
272
+ class QualityCheckService {
273
+ async check(content: string): Promise<QualityReport> {
274
+ const hardRuleResult = await hardRuleChecker.check(content);
275
+ if (!hardRuleResult.passed) return hardRuleResult;
276
+
277
+ return await llmEvaluator.check(content);
278
+ }
279
+ }
280
+ ```
281
+
282
+ ---
283
+
284
+ ## 四、各层实现分析
285
+
286
+ ### 4.1 领域层(Domain Layer)
287
+
288
+ #### 核心实体
289
+
290
+ **Task 实体** (`domain/entities/Task.ts`)
291
+
292
+ ```typescript
293
+ interface Task {
294
+ // 基础字段
295
+ id: string;
296
+ taskId: string;
297
+ status: TaskStatus;
298
+ mode: ExecutionMode;
299
+
300
+ // 业务字段
301
+ topic: string;
302
+ requirements: string;
303
+ hardConstraints?: {
304
+ minWords?: number;
305
+ maxWords?: number;
306
+ keywords?: string[];
307
+ };
308
+
309
+ // 并发控制
310
+ workerId?: string;
311
+ version: number; // 乐观锁
312
+
313
+ // 重试计数
314
+ textRetryCount: number;
315
+ imageRetryCount: number;
316
+
317
+ // 崩溃恢复
318
+ stateSnapshot?: object;
319
+ }
320
+ ```
321
+
322
+ **设计亮点**:
323
+ - ✅ 乐观锁并发控制(`version` 字段)
324
+ - ✅ 多 Worker 抢占机制(`workerId` 字段)
325
+ - ✅ 崩溃恢复支持(`stateSnapshot` 字段)
326
+ - ✅ 幂等性支持(`idempotencyKey` 字段)
327
+
328
+ #### 工作流状态
329
+
330
+ **WorkflowState** (`domain/workflow/State.ts`)
331
+
332
+ ```typescript
333
+ interface WorkflowState {
334
+ // 输入参数
335
+ taskId: string;
336
+ mode: ExecutionMode;
337
+ topic: string;
338
+ requirements: string;
339
+ hardConstraints: { ... };
340
+
341
+ // 流程数据(各节点累积)
342
+ searchQuery?: string;
343
+ searchResults?: SearchResultItem[];
344
+ organizedInfo?: OrganizedInfo;
345
+ articleContent?: string;
346
+ images?: GeneratedImage[];
347
+ textQualityReport?: QualityReport;
348
+ imageQualityReport?: QualityReport;
349
+
350
+ // 控制数据
351
+ currentStep: string;
352
+ textRetryCount: number;
353
+ imageRetryCount: number;
354
+
355
+ // 版本控制
356
+ version: number;
357
+ }
358
+ ```
359
+
360
+ **特点**:
361
+ - ✅ 不可变性(每次返回 `Partial<State>`)
362
+ - ✅ 可序列化(支持 JSON 序列化)
363
+ - ✅ 版本控制(乐观锁)
364
+
365
+ ### 4.2 应用层(Application Layer)
366
+
367
+ #### SyncExecutor(同步执行器)
368
+
369
+ **位置**: `application/workflow/SyncExecutor.ts`
370
+
371
+ ```typescript
372
+ class SyncExecutor {
373
+ async execute(params: CreateTaskParams): Promise<ExecutionResult> {
374
+ // 1. 创建任务记录
375
+ const task = await this.createTask(taskId, params);
376
+
377
+ // 2. 创建初始工作流状态
378
+ const initialState = createInitialState({ ... });
379
+
380
+ // 3. 创建工作流图
381
+ const graph = createSimpleContentCreatorGraph();
382
+
383
+ // 4. 执行工作流
384
+ const finalState = await graph.invoke(initialState);
385
+
386
+ // 5. 保存结果
387
+ await this.saveResults(finalState);
388
+
389
+ // 6. 更新任务状态
390
+ await this.updateTaskStatus(taskId, TaskStatus.COMPLETED);
391
+
392
+ return { taskId, success: true, result: finalState };
393
+ }
394
+ }
395
+ ```
396
+
397
+ **关键特性**:
398
+ - ✅ 端到端任务生命周期管理
399
+ - ✅ 错误处理和重试
400
+ - ✅ 结果自动保存
401
+ - ✅ 进度回调支持
402
+
403
+ ### 4.3 基础设施层(Infrastructure Layer)
404
+
405
+ #### 数据库实现
406
+
407
+ **PostgresTaskRepository** (`infrastructure/database/PostgresTaskRepository.ts`)
408
+
409
+ **核心功能**:
410
+
411
+ 1. **创建任务(幂等性)**
412
+ ```typescript
413
+ async create(input: CreateTaskInput): Promise<Task> {
414
+ const taskId = input.idempotencyKey || uuidv4();
415
+
416
+ const query = `
417
+ INSERT INTO tasks (...)
418
+ VALUES (...)
419
+ ON CONFLICT (task_id) DO UPDATE SET
420
+ user_id = EXCLUDED.user_id,
421
+ ...
422
+ RETURNING *
423
+ `;
424
+ // 使用 ON CONFLICT 实现幂等性
425
+ }
426
+ ```
427
+
428
+ 2. **Worker 抢占(乐观锁)**
429
+ ```typescript
430
+ async claimForProcessing(
431
+ taskId: string,
432
+ workerId: string
433
+ ): Promise<boolean> {
434
+ const query = `
435
+ UPDATE tasks
436
+ SET
437
+ status = 'running',
438
+ worker_id = $2,
439
+ claimed_at = CURRENT_TIMESTAMP,
440
+ version = version + 1
441
+ WHERE task_id = $1
442
+ AND status = 'pending'
443
+ AND version = $3
444
+ RETURNING *
445
+ `;
446
+ // WHERE version = $3 实现乐观锁
447
+ }
448
+ ```
449
+
450
+ **设计亮点**:
451
+ - ✅ 幂等性保障(`ON CONFLICT`)
452
+ - ✅ 乐观锁并发控制(`version` 字段)
453
+ - ✅ Worker 抢占机制(`WHERE status = 'pending'`)
454
+ - ✅ 软删除(`deleted_at` 字段)
455
+
456
+ #### 缓存服务
457
+
458
+ **CacheService** (`infrastructure/cache/CacheService.ts`)
459
+
460
+ ```typescript
461
+ class CacheService {
462
+ async get<T>(key: string): Promise<T | null>;
463
+ async set<T>(key: string, value: T, ttl?: number): Promise<void>;
464
+ async del(key: string): Promise<void>;
465
+ }
466
+ ```
467
+
468
+ **用途**:
469
+ - LLM 响应缓存
470
+ - 搜索结果缓存
471
+ - API 限流计数
472
+
473
+ #### 任务队列
474
+
475
+ **TaskQueue** (`infrastructure/queue/TaskQueue.ts`)
476
+
477
+ ```typescript
478
+ class TaskQueue {
479
+ async addTask(task: Task): Promise<void>;
480
+ async getTaskStats(): Promise<QueueStats>;
481
+ }
482
+ ```
483
+
484
+ **基于 BullMQ**:
485
+ - Redis 作为后端存储
486
+ - 支持任务优先级
487
+ - 支持任务延迟
488
+ - 支持重试策略
489
+
490
+ ### 4.4 服务层(Services Layer)
491
+
492
+ #### LLM 服务
493
+
494
+ **LLMService** (`services/llm/LLMService.ts`)
495
+
496
+ ```typescript
497
+ class LLMService {
498
+ async chat(request: ChatRequest): Promise<ChatResponse> {
499
+ // 1. 生成缓存键
500
+ const cacheKey = this.generateCacheKey(request);
501
+
502
+ // 2. 检查缓存
503
+ if (this.enableCache) {
504
+ const cached = await cacheService.get(cacheKey);
505
+ if (cached) return cached;
506
+ }
507
+
508
+ // 3. 调用 DeepSeek API
509
+ const response = await axios.post(...);
510
+
511
+ // 4. 缓存结果
512
+ if (this.enableCache) {
513
+ await cacheService.set(cacheKey, response, ttl);
514
+ }
515
+
516
+ // 5. 记录 Token 使用
517
+ await metricsService.recordTokenUsage(...);
518
+
519
+ return response;
520
+ }
521
+ }
522
+ ```
523
+
524
+ **特点**:
525
+ - ✅ 缓存支持(减少 API 调用)
526
+ - ✅ Token 使用统计
527
+ - ✅ 错误重试
528
+
529
+ #### 搜索服务
530
+
531
+ **SearchService** (`services/search/SearchService.ts`)
532
+
533
+ ```typescript
534
+ class SearchService {
535
+ async searchWithAnswer(
536
+ query: string,
537
+ maxResults: number
538
+ ): Promise<SearchResponse> {
539
+ // 调用 Tavily API
540
+ }
541
+ }
542
+ ```
543
+
544
+ #### 质量检查服务
545
+
546
+ **QualityCheckService** (`services/quality/QualityCheckService.ts`)
547
+
548
+ **双层质检**:
549
+ 1. **硬规则检查**(`HardRuleChecker`)
550
+ - 字数检查
551
+ - 关键词检查
552
+ - 结构检查(标题、引言、正文、结论)
553
+
554
+ 2. **LLM 评估**(`LLMEvaluator`)
555
+ - 相关性评分
556
+ - 连贯性评分
557
+ - 完整性评分
558
+ - 可读性评分
559
+
560
+ ```typescript
561
+ async check(content: string): Promise<QualityReport> {
562
+ // 1. 硬规则检查
563
+ const hardRuleResult = await this.hardRuleChecker.check(content);
564
+ if (!hardRuleResult.passed) {
565
+ return hardRuleResult; // 快速失败
566
+ }
567
+
568
+ // 2. LLM 评估
569
+ const llmResult = await this.llmEvaluator.check(content);
570
+
571
+ return {
572
+ ...hardRuleResult,
573
+ ...llmResult,
574
+ passed: llmResult.passed,
575
+ };
576
+ }
577
+ ```
578
+
579
+ ---
580
+
581
+ ## 五、关键技术实现
582
+
583
+ ### 5.1 并发控制
584
+
585
+ #### 乐观锁(Optimistic Locking)
586
+
587
+ **实现位置**: `PostgresTaskRepository.ts`
588
+
589
+ ```typescript
590
+ async update(
591
+ taskId: string,
592
+ params: UpdateTaskParams
593
+ ): Promise<Task> {
594
+ const query = `
595
+ UPDATE tasks
596
+ SET
597
+ status = $2,
598
+ worker_id = $3,
599
+ version = version + 1
600
+ WHERE task_id = $1
601
+ AND version = $4
602
+ RETURNING *
603
+ `;
604
+
605
+ const result = await this.query(query, [
606
+ taskId,
607
+ params.status,
608
+ params.workerId,
609
+ params.version, // 当前版本号
610
+ ]);
611
+
612
+ if (result.rows.length === 0) {
613
+ throw new Error('Optimistic lock conflict: version mismatch');
614
+ }
615
+
616
+ return this.mapToTask(result.rows[0]);
617
+ }
618
+ ```
619
+
620
+ **原理**:
621
+ - 更新时检查 `version` 字段
622
+ - 只有 `version` 匹配时才更新成功
623
+ - 更新成功后 `version` 自动 +1
624
+
625
+ **优点**:
626
+ - ✅ 无锁竞争,性能高
627
+ - ✅ 适合读多写少场景
628
+ - ✅ 避免死锁
629
+
630
+ #### Worker 抢占机制
631
+
632
+ **实现位置**: `TaskWorker.ts`
633
+
634
+ ```typescript
635
+ async processJob(job: Job<TaskJobData>): Promise<void> {
636
+ // 1. 抢占任务(使用乐观锁)
637
+ const claimed = await this.repository.claimForProcessing(
638
+ data.taskId,
639
+ this.workerId
640
+ );
641
+
642
+ if (!claimed) {
643
+ throw new Error(`Failed to claim task ${data.taskId}`);
644
+ }
645
+
646
+ // 2. 执行工作流
647
+ const result = await this.executeWorkflow(data);
648
+
649
+ // 3. 更新任务状态
650
+ await this.repository.update(data.taskId, {
651
+ status: 'completed',
652
+ version: task.version + 1,
653
+ });
654
+ }
655
+ ```
656
+
657
+ **抢占逻辑**:
658
+ ```sql
659
+ UPDATE tasks
660
+ SET
661
+ status = 'running',
662
+ worker_id = $2,
663
+ claimed_at = CURRENT_TIMESTAMP
664
+ WHERE task_id = $1
665
+ AND status = 'pending' -- 只有 pending 状态才能抢占
666
+ AND version = $3 -- 乐观锁
667
+ RETURNING *
668
+ ```
669
+
670
+ **保证**:
671
+ - 同一时刻只有一个 Worker 能抢占成功
672
+ - 其他 Worker 会收到 `claimed = false`
673
+
674
+ ### 5.2 崩溃恢复
675
+
676
+ #### State 快照
677
+
678
+ **实现位置**: `CheckpointManager.ts`
679
+
680
+ ```typescript
681
+ class CheckpointManager {
682
+ async saveCheckpoint(
683
+ taskId: string,
684
+ nodeName: string,
685
+ state: WorkflowState
686
+ ): Promise<void> {
687
+ // 1. 序列化状态
688
+ const snapshot = StateSnapshotManager.createCheckpoint(state);
689
+
690
+ // 2. 保存到数据库
691
+ await this.taskRepo.update(taskId, {
692
+ stateSnapshot: snapshot,
693
+ currentStep: nodeName,
694
+ });
695
+ }
696
+
697
+ async loadCheckpoint(taskId: string): Promise<WorkflowState | null> {
698
+ // 1. 从数据库加载
699
+ const task = await this.taskRepo.findById(taskId);
700
+ if (!task?.stateSnapshot) return null;
701
+
702
+ // 2. 反序列化
703
+ return StateSnapshotManager.deserialize(task.stateSnapshot);
704
+ }
705
+ }
706
+ ```
707
+
708
+ **恢复流程**:
709
+ ```typescript
710
+ // Worker 重启后
711
+ const task = await taskRepo.findById(taskId);
712
+
713
+ if (task.status === 'running' && task.stateSnapshot) {
714
+ // 从检查点恢复
715
+ const state = StateSnapshotManager.deserialize(task.stateSnapshot);
716
+
717
+ // 继续执行
718
+ const graph = createContentCreatorGraph();
719
+ const result = await graph.invoke(state, {
720
+ checkpoint: task.currentStep,
721
+ });
722
+ }
723
+ ```
724
+
725
+ ### 5.3 质量检查循环
726
+
727
+ **实现位置**: `ContentCreatorGraph.ts`
728
+
729
+ ```typescript
730
+ // 文本质检后的路由
731
+ function routeAfterCheckText(state: WorkflowState): string {
732
+ // 如果质检通过,生成配图
733
+ if (state.textQualityReport?.passed) {
734
+ return 'generate_image';
735
+ }
736
+
737
+ // 如果质检失败但重试次数未满,重试写作
738
+ if (state.textRetryCount < 3) {
739
+ return 'write';
740
+ }
741
+
742
+ // 重试次数已满,抛出错误
743
+ throw new Error('Text quality check failed after 3 attempts');
744
+ }
745
+
746
+ // 添加条件边
747
+ graph.addConditionalEdges(
748
+ 'checkText',
749
+ routeAfterCheckText,
750
+ {
751
+ write: 'write',
752
+ generate_image: 'generate_image',
753
+ }
754
+ );
755
+ ```
756
+
757
+ **重试机制**:
758
+ - 文章质检最多重试 3 次
759
+ - 配图质检最多重试 2 次
760
+ - 每次重试会保留上一版本内容(`previousContent`)
761
+
762
+ ### 5.4 Token 监控
763
+
764
+ **实现位置**: `LLMService.ts`
765
+
766
+ ```typescript
767
+ async chat(request: ChatRequest): Promise<ChatResponse> {
768
+ const startTime = Date.now();
769
+
770
+ // 调用 API
771
+ const response = await axios.post(...);
772
+
773
+ const duration = Date.now() - startTime;
774
+
775
+ // 记录 Token 使用
776
+ await metricsService.recordTokenUsage({
777
+ taskId: request.metadata?.taskId,
778
+ apiName: 'deepseek',
779
+ modelName: response.model,
780
+ promptTokens: response.usage.promptTokens,
781
+ completionTokens: response.usage.completionTokens,
782
+ totalTokens: response.usage.totalTokens,
783
+ duration,
784
+ });
785
+
786
+ return response;
787
+ }
788
+ ```
789
+
790
+ **监控指标**:
791
+ - Token 使用量(按任务、按模型)
792
+ - API 调用次数
793
+ - API 响应时间
794
+ - API 错误率
795
+
796
+ ---
797
+
798
+ ## 六、代码质量评估
799
+
800
+ ### 6.1 优点
801
+
802
+ | 维度 | 评分 | 说明 |
803
+ |------|------|------|
804
+ | **架构设计** | ⭐⭐⭐⭐⭐ | 清晰的分层,依赖倒置,高内聚低耦合 |
805
+ | **代码规范** | ⭐⭐⭐⭐ | TypeScript 类型完整,注释清晰 |
806
+ | **错误处理** | ⭐⭐⭐⭐ | 统一的错误处理,重试机制完善 |
807
+ | **可测试性** | ⭐⭐⭐⭐⭐ | 接口抽象,易于 mock 和测试 |
808
+ | **可扩展性** | ⭐⭐⭐⭐⭐ | 节点模式易于扩展,策略模式灵活 |
809
+ | **性能优化** | ⭐⭐⭐⭐ | 缓存、异步、并发控制到位 |
810
+ | **文档完整性** | ⭐⭐⭐⭐ | 代码注释完整,有架构文档 |
811
+
812
+ ### 6.2 可改进之处
813
+
814
+ #### 1. 类型定义分散
815
+
816
+ **问题**:同一类型在多处定义
817
+
818
+ ```typescript
819
+ // domain/entities/Task.ts
820
+ export interface Result { ... }
821
+
822
+ // domain/entities/Result.ts
823
+ export interface Result { ... } // 重复定义
824
+ ```
825
+
826
+ **建议**:统一类型定义位置
827
+ ```typescript
828
+ // domain/types/index.ts
829
+ export * from './task-types';
830
+ export * from './result-types';
831
+ export * from './quality-types';
832
+ ```
833
+
834
+ #### 2. 错误处理不够细化
835
+
836
+ **问题**:所有错误都用 `Error` 抛出
837
+
838
+ ```typescript
839
+ throw new Error('Task not found');
840
+ ```
841
+
842
+ **建议**:定义领域错误类型
843
+ ```typescript
844
+ export class TaskNotFoundError extends Error {
845
+ constructor(taskId: string) {
846
+ super(`Task not found: ${taskId}`);
847
+ this.name = 'TaskNotFoundError';
848
+ }
849
+ }
850
+
851
+ throw new TaskNotFoundError(taskId);
852
+ ```
853
+
854
+ #### 3. 日志记录不够结构化
855
+
856
+ **问题**:日志参数使用对象
857
+
858
+ ```typescript
859
+ logger.info('Task created', { taskId, topic, mode });
860
+ ```
861
+
862
+ **建议**:使用结构化日志字段
863
+ ```typescript
864
+ logger.info('Task created', {
865
+ event: 'task.created',
866
+ task_id: taskId,
867
+ topic,
868
+ mode,
869
+ });
870
+ ```
871
+
872
+ #### 4. 缺少输入验证
873
+
874
+ **问题**:直接使用外部输入
875
+
876
+ ```typescript
877
+ async createTask(params: CreateTaskParams): Promise<Task> {
878
+ // 没有验证 params.topic 是否为空
879
+ const task = await this.repo.create({ ...params });
880
+ }
881
+ ```
882
+
883
+ **建议**:使用 Zod 验证
884
+ ```typescript
885
+ import { z } from 'zod';
886
+
887
+ const CreateTaskSchema = z.object({
888
+ topic: z.string().min(1).max(200),
889
+ requirements: z.string().min(1),
890
+ mode: z.enum(['sync', 'async']),
891
+ });
892
+
893
+ async createTask(params: CreateTaskParams): Promise<Task> {
894
+ const validated = CreateTaskSchema.parse(params);
895
+ // ...
896
+ }
897
+ ```
898
+
899
+ ---
900
+
901
+ ## 七、性能与可扩展性
902
+
903
+ ### 7.1 性能优化
904
+
905
+ #### 1. 数据库连接池
906
+
907
+ ```typescript
908
+ // PostgreSQL 连接池配置
909
+ const pool = new Pool({
910
+ host: config.database.host,
911
+ port: config.database.port,
912
+ database: config.database.name,
913
+ user: config.database.user,
914
+ password: config.database.password,
915
+ max: 20, // 最大连接数
916
+ idleTimeoutMillis: 30000,
917
+ connectionTimeoutMillis: 2000,
918
+ });
919
+ ```
920
+
921
+ #### 2. Redis 缓存
922
+
923
+ ```typescript
924
+ // 缓存 LLM 响应
925
+ async get<T>(key: string): Promise<T | null> {
926
+ const cached = await redis.get(key);
927
+ return cached ? JSON.parse(cached) : null;
928
+ }
929
+
930
+ async set<T>(key: string, value: T, ttl = 3600): Promise<void> {
931
+ await redis.setex(key, ttl, JSON.stringify(value));
932
+ }
933
+ ```
934
+
935
+ #### 3. 任务队列限流
936
+
937
+ ```typescript
938
+ // Worker 限流配置
939
+ const worker = new Worker('content-creator-tasks', processor, {
940
+ connection: redisConnection,
941
+ concurrency: 2, // 每个 Worker 并发 2 个任务
942
+ limiter: {
943
+ max: 10, // 每秒最多处理 10 个任务
944
+ duration: 1000,
945
+ },
946
+ });
947
+ ```
948
+
949
+ ### 7.2 可扩展性
950
+
951
+ #### 水平扩展
952
+
953
+ **多 Worker 部署**:
954
+ ```
955
+ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
956
+ │ Worker 1 │ │ Worker 2 │ │ Worker 3 │
957
+ │ (并发: 2) │ │ (并发: 2) │ │ (并发: 2) │
958
+ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘
959
+ │ │ │
960
+ └───────────────────┼───────────────────┘
961
+
962
+ ┌────────▼────────┐
963
+ │ BullMQ Queue │
964
+ │ (Redis Backend)│
965
+ └─────────────────┘
966
+ ```
967
+
968
+ **总并发**: 3 Workers × 2 = 6 个任务同时执行
969
+
970
+ #### 垂直扩展
971
+
972
+ **数据库优化**:
973
+ - PostgreSQL 主从复制
974
+ - 读写分离
975
+ - 连接池调优
976
+
977
+ **缓存优化**:
978
+ - Redis Cluster
979
+ - 多级缓存(本地 + Redis)
980
+
981
+ ---
982
+
983
+ ## 八、建议与改进
984
+
985
+ ### 8.1 短期改进(1-2 周)
986
+
987
+ 1. **添加输入验证**
988
+ - 使用 Zod 验证所有外部输入
989
+ - 添加参数边界检查
990
+
991
+ 2. **完善错误类型**
992
+ - 定义领域错误类型
993
+ - 区分业务错误和系统错误
994
+
995
+ 3. **添加单元测试**
996
+ - 提高测试覆盖率到 80%+
997
+ - 重点测试核心业务逻辑
998
+
999
+ 4. **优化日志格式**
1000
+ - 使用结构化日志
1001
+ - 添加 Trace ID 支持链路追踪
1002
+
1003
+ ### 8.2 中期改进(1-2 月)
1004
+
1005
+ 1. **性能监控**
1006
+ - 集成 Prometheus + Grafana
1007
+ - 添加性能指标仪表板
1008
+
1009
+ 2. **API 限流**
1010
+ - 实现用户级限流
1011
+ - 防止 API 滥用
1012
+
1013
+ 3. **任务优先级**
1014
+ - 支持任务优先级队列
1015
+ - VIP 任务优先处理
1016
+
1017
+ 4. **分布式追踪**
1018
+ - 集成 Jaeger / Zipkin
1019
+ - 全链路性能分析
1020
+
1021
+ ### 8.3 长期改进(3-6 月)
1022
+
1023
+ 1. **微服务拆分**
1024
+ - 工作流服务独立部署
1025
+ - 质检服务独立部署
1026
+
1027
+ 2. **数据库分片**
1028
+ - 按用户 ID 分片
1029
+ - 提高并发能力
1030
+
1031
+ 3. **多区域部署**
1032
+ - 跨区域容灾
1033
+ - 就近访问
1034
+
1035
+ 4. **AI 模型优化**
1036
+ - 模型蒸馏
1037
+ - 降低成本
1038
+
1039
+ ---
1040
+
1041
+ ## 九、总结
1042
+
1043
+ ### 9.1 项目亮点
1044
+
1045
+ ✅ **架构设计优秀**:清晰的分层,依赖倒置,高内聚低耦合
1046
+ ✅ **并发控制完善**:乐观锁 + Worker 抢占机制
1047
+ ✅ **容错能力强**:崩溃恢复 + 重试机制
1048
+ ✅ **可扩展性好**:节点模式 + 策略模式
1049
+ ✅ **代码质量高**:TypeScript 类型完整,注释清晰
1050
+
1051
+ ### 9.2 核心技术
1052
+
1053
+ | 技术 | 用途 | 评价 |
1054
+ |------|------|------|
1055
+ | **LangGraph** | 工作流引擎 | ⭐⭐⭐⭐⭐ 优秀的选择 |
1056
+ | **BullMQ** | 任务队列 | ⭐⭐⭐⭐ 功能完善 |
1057
+ | **PostgreSQL** | 数据持久化 | ⭐⭐⭐⭐⭐ 生产级选择 |
1058
+ | **Redis** | 缓存 + 队列后端 | ⭐⭐⭐⭐⭐ 性能优秀 |
1059
+ | **TypeScript** | 类型安全 | ⭐⭐⭐⭐⭐ 必备工具 |
1060
+
1061
+ ### 9.3 代码质量总评
1062
+
1063
+ | 维度 | 评分 |
1064
+ |------|------|
1065
+ | 架构设计 | ⭐⭐⭐⭐⭐ (5/5) |
1066
+ | 代码规范 | ⭐⭐⭐⭐ (4/5) |
1067
+ | 错误处理 | ⭐⭐⭐⭐ (4/5) |
1068
+ | 可测试性 | ⭐⭐⭐⭐⭐ (5/5) |
1069
+ | 可扩展性 | ⭐⭐⭐⭐⭐ (5/5) |
1070
+ | 性能优化 | ⭐⭐⭐⭐ (4/5) |
1071
+ | 文档完整性 | ⭐⭐⭐⭐ (4/5) |
1072
+ | **总评** | **⭐⭐⭐⭐⭐ (4.6/5)** |
1073
+
1074
+ ---
1075
+
1076
+ ## 附录
1077
+
1078
+ ### A. 关键文件索引
1079
+
1080
+ | 模块 | 核心文件 |
1081
+ |------|----------|
1082
+ | 领域实体 | `domain/entities/Task.ts` |
1083
+ | 工作流状态 | `domain/workflow/State.ts` |
1084
+ | 工作流图 | `domain/workflow/ContentCreatorGraph.ts` |
1085
+ | 节点基类 | `domain/workflow/nodes/BaseNode.ts` |
1086
+ | 同步执行器 | `application/workflow/SyncExecutor.ts` |
1087
+ | 异步 Worker | `workers/TaskWorker.ts` |
1088
+ | 数据库仓储 | `infrastructure/database/PostgresTaskRepository.ts` |
1089
+ | LLM 服务 | `services/llm/LLMService.ts` |
1090
+ | 质检服务 | `services/quality/QualityCheckService.ts` |
1091
+
1092
+ ### B. 设计模式总结
1093
+
1094
+ | 模式 | 使用位置 | 目的 |
1095
+ |------|----------|------|
1096
+ | 仓储模式 | `domain/repositories/` | 数据访问解耦 |
1097
+ | 工厂模式 | `infrastructure/database/` | 对象创建 |
1098
+ | 策略模式 | `services/quality/` | 算法封装 |
1099
+ | 状态机模式 | `domain/workflow/` | 工作流控制 |
1100
+ | 单例模式 | `services/` | 共享实例 |
1101
+ | 观察者模式 | `application/` | 进度通知 |
1102
+
1103
+ ---
1104
+
1105
+ **文档版本**: v1.0
1106
+ **最后更新**: 2026-01-22
1107
+ **作者**: Claude Code Analysis