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,933 @@
1
+ # 性能优化指南
2
+
3
+ **版本**: 1.0
4
+ **日期**: 2026-01-19
5
+ **所属阶段**: 阶段 4
6
+
7
+ ---
8
+
9
+ ## 📋 目录
10
+
11
+ - [概述](#概述)
12
+ - [缓存优化](#缓存优化)
13
+ - [数据库优化](#数据库优化)
14
+ - [LLM 调用优化](#llm-调用优化)
15
+ - [并发优化](#并发优化)
16
+ - [内存优化](#内存优化)
17
+ - [网络优化](#网络优化)
18
+ - [性能测试](#性能测试)
19
+
20
+ ---
21
+
22
+ ## 概述
23
+
24
+ ### 优化目标
25
+
26
+ - ✅ 减少响应时间(端到端 < 5分钟)
27
+ - ✅ 提高吞吐量(日处理 3000+ 任务)
28
+ - ✅ 降低资源使用(内存 < 2GB/Worker)
29
+ - ✅ 减少 API 调用成本(Token 使用)
30
+
31
+ ### 性能瓶颈分析
32
+
33
+ ```
34
+ 典型任务耗时分布(总计 ~120秒)
35
+ ├── 搜索 (2秒) - 1.7%
36
+ ├── 整理 (28秒) - 23.3%
37
+ ├── 写作 (36秒) - 30.0%
38
+ ├── 质检 (114秒) - 95.0% ← 主要瓶颈
39
+ │ ├── LLM 调用 (100秒)
40
+ │ └── 硬规则 (14秒)
41
+ └── 图片生成 (未配置)
42
+ ```
43
+
44
+ **优化重点**: LLM 调用、缓存策略、并发处理
45
+
46
+ ---
47
+
48
+ ## 缓存优化
49
+
50
+ ### 1. Redis 缓存架构
51
+
52
+ ```typescript
53
+ // 文件: src/infrastructure/cache/RedisCache.ts
54
+
55
+ import Redis from 'ioredis';
56
+ import { createLogger } from '../logging/logger.js';
57
+
58
+ const logger = createLogger('RedisCache');
59
+
60
+ export class RedisCache {
61
+ private redis: Redis;
62
+ private defaultTTL = 7 * 24 * 3600; // 7天
63
+
64
+ constructor(redis: Redis) {
65
+ this.redis = redis;
66
+ }
67
+
68
+ async get<T>(key: string): Promise<T | null> {
69
+ try {
70
+ const value = await this.redis.get(key);
71
+ if (!value) return null;
72
+
73
+ return JSON.parse(value) as T;
74
+ } catch (error) {
75
+ logger.error('Cache get failed', { key, error });
76
+ return null;
77
+ }
78
+ }
79
+
80
+ async set(key: string, value: any, ttl?: number): Promise<void> {
81
+ try {
82
+ const serialized = JSON.stringify(value);
83
+ const expiry = ttl || this.defaultTTL;
84
+
85
+ await this.redis.setex(key, expiry, serialized);
86
+
87
+ logger.debug('Cache set', { key, ttl: expiry });
88
+ } catch (error) {
89
+ logger.error('Cache set failed', { key, error });
90
+ }
91
+ }
92
+
93
+ async delete(key: string): Promise<void> {
94
+ try {
95
+ await this.redis.del(key);
96
+ logger.debug('Cache deleted', { key });
97
+ } catch (error) {
98
+ logger.error('Cache delete failed', { key, error });
99
+ }
100
+ }
101
+
102
+ async invalidatePattern(pattern: string): Promise<void> {
103
+ try {
104
+ const keys = await this.redis.keys(pattern);
105
+ if (keys.length > 0) {
106
+ await this.redis.del(...keys);
107
+ logger.info('Cache pattern invalidated', { pattern, count: keys.length });
108
+ }
109
+ } catch (error) {
110
+ logger.error('Cache invalidate failed', { pattern, error });
111
+ }
112
+ }
113
+
114
+ async mget<T>(keys: string[]): Promise<(T | null)[]> {
115
+ try {
116
+ const values = await this.redis.mget(...keys);
117
+ return values.map(v => v ? JSON.parse(v) : null);
118
+ } catch (error) {
119
+ logger.error('Cache mget failed', { error });
120
+ return keys.map(() => null);
121
+ }
122
+ }
123
+
124
+ async mset(keyValuePairs: Record<string, any>, ttl?: number): Promise<void> {
125
+ const pipeline = this.redis.pipeline();
126
+ const expiry = ttl || this.defaultTTL;
127
+
128
+ for (const [key, value] of Object.entries(keyValuePairs)) {
129
+ const serialized = JSON.stringify(value);
130
+ pipeline.setex(key, expiry, serialized);
131
+ }
132
+
133
+ await pipeline.exec();
134
+ }
135
+
136
+ // 缓存统计
137
+ async getStats(pattern: string = 'cache:*'): Promise<CacheStats> {
138
+ const keys = await this.redis.keys(pattern);
139
+ const info = await this.redis.info('stats');
140
+
141
+ return {
142
+ totalKeys: keys.length,
143
+ hits: parseInt(info.keyspace_hits || '0'),
144
+ misses: parseInt(info.keyspace_misses || '0'),
145
+ hitRate: (parseInt(info.keyspace_hits || '0') /
146
+ (parseInt(info.keyspace_hits || '0') + parseInt(info.keyspace_misses || '1')))
147
+ };
148
+ }
149
+ }
150
+ ```
151
+
152
+ ### 2. 缓存策略
153
+
154
+ #### LLM 响应缓存
155
+
156
+ ```typescript
157
+ // 缓存 LLM 生成结果
158
+ export class LLMCacheService {
159
+ private cache: RedisCache;
160
+
161
+ async getCachedResponse(prompt: string): Promise<string | null> {
162
+ const key = this.generateKey('llm', prompt);
163
+ return this.cache.get<string>(key);
164
+ }
165
+
166
+ async setCachedResponse(prompt: string, response: string): Promise<void> {
167
+ const key = this.generateKey('llm', prompt);
168
+ await this.cache.set(key, response, 7 * 24 * 3600); // 7天
169
+ }
170
+
171
+ private generateKey(prefix: string, content: string): string {
172
+ const hash = crypto.createHash('sha256')
173
+ .update(content)
174
+ .digest('hex')
175
+ .substring(0, 16);
176
+ return `${prefix}:${hash}`;
177
+ }
178
+ }
179
+ ```
180
+
181
+ #### 搜索结果缓存
182
+
183
+ ```typescript
184
+ export class SearchCacheService {
185
+ private cache: RedisCache;
186
+
187
+ async getCachedResults(query: string): Promise<SearchResult[] | null> {
188
+ const key = this.generateKey('search', query);
189
+ return this.cache.get<SearchResult[]>(key);
190
+ }
191
+
192
+ async setCachedResults(query: string, results: SearchResult[]): Promise<void> {
193
+ const key = this.generateKey('search', query);
194
+ await this.cache.set(key, results, 24 * 3600); // 1天
195
+ }
196
+ }
197
+ ```
198
+
199
+ #### 质量检查缓存
200
+
201
+ ```typescript
202
+ export class QualityCacheService {
203
+ private cache: RedisCache;
204
+
205
+ async getCachedEvaluation(content: string): Promise<QualityResult | null> {
206
+ const key = this.generateKey('quality', content);
207
+ return this.cache.get<QualityResult>(key);
208
+ }
209
+
210
+ async setCachedEvaluation(content: string, result: QualityResult): Promise<void> {
211
+ const key = this.generateKey('quality', content);
212
+ await this.cache.set(key, result, 3 * 24 * 3600); // 3天
213
+ }
214
+ }
215
+ ```
216
+
217
+ ### 3. 缓存预热
218
+
219
+ ```typescript
220
+ // 在应用启动时预热常用数据
221
+ export class CacheWarmupService {
222
+ async warmup(): Promise<void> {
223
+ logger.info('Starting cache warmup');
224
+
225
+ // 预热常见查询的搜索结果
226
+ const commonQueries = [
227
+ '人工智能',
228
+ '机器学习',
229
+ '深度学习',
230
+ '自然语言处理'
231
+ ];
232
+
233
+ for (const query of commonQueries) {
234
+ // 异步预热,不阻塞启动
235
+ this.warmupSearch(query).catch(err => {
236
+ logger.warn('Cache warmup failed', { query, error: err });
237
+ });
238
+ }
239
+
240
+ logger.info('Cache warmup completed');
241
+ }
242
+
243
+ private async warmupSearch(query: string): Promise<void> {
244
+ const results = await this.searchService.search(query);
245
+ await this.searchCache.setCachedResults(query, results);
246
+ }
247
+ }
248
+ ```
249
+
250
+ ---
251
+
252
+ ## 数据库优化
253
+
254
+ ### 1. 索引优化
255
+
256
+ ```sql
257
+ -- 任务表索引
258
+ CREATE INDEX idx_tasks_status_created ON tasks(status, created_at DESC);
259
+ CREATE INDEX idx_tasks_mode_status ON tasks(mode, status) WHERE status IN ('pending', 'processing');
260
+
261
+ -- 任务步骤表索引
262
+ CREATE INDEX idx_task_steps_task_step_status
263
+ ON task_steps(task_id, step_name, status);
264
+
265
+ -- Token 使用表索引
266
+ CREATE INDEX idx_token_usage_task_id ON token_usage(task_id);
267
+ CREATE INDEX idx_token_usage_created ON token_usage(created_at DESC);
268
+
269
+ -- 质量检查表索引
270
+ CREATE INDEX idx_quality_checks_task_id ON quality_checks(task_id);
271
+ CREATE INDEX idx_quality_checks_score ON quality_checks(score) WHERE score < 7;
272
+ ```
273
+
274
+ ### 2. 查询优化
275
+
276
+ ```typescript
277
+ // 使用连接池
278
+ import { Pool } from 'pg';
279
+
280
+ export class DatabaseService {
281
+ private pool = new Pool({
282
+ host: process.env.POSTGRES_HOST,
283
+ port: parseInt(process.env.POSTGRES_PORT),
284
+ database: process.env.POSTGRES_DB,
285
+ user: process.env.POSTGRES_USER,
286
+ password: process.env.POSTGRES_PASSWORD,
287
+ max: 20, // 最大连接数
288
+ idleTimeoutMillis: 30000,
289
+ connectionTimeoutMillis: 2000,
290
+ });
291
+
292
+ // 批量查询
293
+ async getTasksWithSteps(taskIds: string[]): Promise<TaskWithSteps[]> {
294
+ const client = await this.pool.connect();
295
+
296
+ try {
297
+ // 使用 IN 子查询避免 N+1 问题
298
+ const query = `
299
+ SELECT
300
+ t.*,
301
+ json_agg(
302
+ json_build_object(
303
+ 'step_name', ts.step_name,
304
+ 'status', ts.status,
305
+ 'started_at', ts.started_at,
306
+ 'completed_at', ts.completed_at
307
+ )
308
+ ) as steps
309
+ FROM tasks t
310
+ LEFT JOIN task_steps ts ON t.id = ts.task_id
311
+ WHERE t.id = ANY($1)
312
+ GROUP BY t.id
313
+ `;
314
+
315
+ const result = await client.query(query, [taskIds]);
316
+ return result.rows;
317
+ } finally {
318
+ client.release();
319
+ }
320
+ }
321
+
322
+ // 分页查询
323
+ async getTasksPaginated(page: number, pageSize: number, filters: any): Promise<PaginatedResult> {
324
+ const offset = (page - 1) * pageSize;
325
+ const limit = pageSize;
326
+
327
+ const [tasks, countResult] = await Promise.all([
328
+ this.pool.query(
329
+ 'SELECT * FROM tasks WHERE $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3',
330
+ [filters.whereClause, limit, offset]
331
+ ),
332
+ this.pool.query(
333
+ 'SELECT COUNT(*) FROM tasks WHERE $1',
334
+ [filters.whereClause]
335
+ )
336
+ ]);
337
+
338
+ return {
339
+ data: tasks.rows,
340
+ total: parseInt(countResult.rows[0].count),
341
+ page,
342
+ pageSize,
343
+ totalPages: Math.ceil(parseInt(countResult.rows[0].count) / pageSize)
344
+ };
345
+ }
346
+ }
347
+ ```
348
+
349
+ ### 3. 连接池配置
350
+
351
+ ```typescript
352
+ // 连接池配置
353
+ const poolConfig = {
354
+ // 连接数
355
+ max: 20, // 最大连接数
356
+ min: 5, // 最小空闲连接数
357
+ idle: 10000, // 空闲连接超时(10秒)
358
+
359
+ // 超时
360
+ connectionTimeoutMillis: 2000, // 连接超时
361
+ idleTimeoutMillis: 30000, // 空闲超时
362
+ statementTimeout: 30000, // 查询超时(30秒)
363
+
364
+ // 重试
365
+ retries: 3,
366
+ };
367
+
368
+ // 性能监控
369
+ pool.on('connect', (client) => {
370
+ logger.info('New DB client connected', { totalCount: pool.totalCount });
371
+ });
372
+
373
+ pool.on('error', (err) => {
374
+ logger.error('DB client error', { error: err });
375
+ });
376
+
377
+ pool.on('remove', () => {
378
+ logger.warn('DB client removed');
379
+ });
380
+
381
+ pool.on('wait', (count) => {
382
+ logger.warn('Waiting for available DB connection', { waitingCount: count });
383
+ });
384
+ ```
385
+
386
+ ---
387
+
388
+ ## LLM 调用优化
389
+
390
+ ### 1. 批量处理
391
+
392
+ ```typescript
393
+ // 批量生成多个内容
394
+ export class BatchLLMService {
395
+ async generateBatch(prompts: string[]): Promise<string[]> {
396
+ const batchSize = 5; // 每批处理5个
397
+ const results: string[] = [];
398
+
399
+ for (let i = 0; i < prompts.length; i += batchSize) {
400
+ const batch = prompts.slice(i, i + batchSize);
401
+
402
+ // 并发处理批次
403
+ const batchResults = await Promise.all(
404
+ batch.map(prompt => this.llmService.generate(prompt))
405
+ );
406
+
407
+ results.push(...batchResults);
408
+
409
+ // 批次间延迟,避免速率限制
410
+ if (i + batchSize < prompts.length) {
411
+ await this.delay(1000); // 等待1秒
412
+ }
413
+ }
414
+
415
+ return results;
416
+ }
417
+
418
+ private delay(ms: number): Promise<void> {
419
+ return new Promise(resolve => setTimeout(resolve, ms));
420
+ }
421
+ }
422
+ ```
423
+
424
+ ### 2. Prompt 优化
425
+
426
+ ```typescript
427
+ // 优化 Prompt 以减少 Token 使用
428
+ export class PromptOptimizer {
429
+ optimizePrompt(prompt: string): string {
430
+ // 移除冗余信息
431
+ let optimized = prompt
432
+ .replace(/\s+/g, ' ') // 多个空格压缩为一个
433
+ .trim();
434
+
435
+ // 使用更简洁的表达
436
+ const replacements = [
437
+ ['请', ''], // 移除礼貌用语
438
+ ['帮助我', ''],
439
+ ['我需要你', ''],
440
+ ['你能', ''],
441
+ ['请生成', '生成'],
442
+ ['请提供', '提供']
443
+ ];
444
+
445
+ for (const [from, to] of replacements) {
446
+ optimized = optimized.replace(new RegExp(from, 'g'), to);
447
+ }
448
+
449
+ return optimized;
450
+ }
451
+
452
+ // 估算 Token 数量
453
+ estimateTokens(text: string): number {
454
+ // 中文约 1.5 字符 = 1 token
455
+ // 英文约 4 字符 = 1 token
456
+ const chineseChars = (text.match(/[\u4e00-\u9fa5]/g) || []).length;
457
+ const englishChars = text.length - chineseChars;
458
+
459
+ return Math.ceil(chineseChars / 1.5 + englishChars / 4);
460
+ }
461
+ }
462
+ ```
463
+
464
+ ### 3. 流式响应
465
+
466
+ ```typescript
467
+ // 使用流式响应减少首字延迟
468
+ export class StreamingLLMService {
469
+ async *generateStream(prompt: string): AsyncGenerator<string> {
470
+ const response = await fetch(this.llmBaseUrl, {
471
+ method: 'POST',
472
+ headers: {
473
+ 'Content-Type': 'application/json',
474
+ 'Authorization': `Bearer ${this.apiKey}`
475
+ },
476
+ body: JSON.stringify({
477
+ model: this.model,
478
+ messages: [{ role: 'user', content: prompt }],
479
+ stream: true // 启用流式
480
+ })
481
+ });
482
+
483
+ const reader = response.body!.getReader();
484
+ const decoder = new TextDecoder();
485
+
486
+ while (true) {
487
+ const { done, value } = await reader.read();
488
+ if (done) break;
489
+
490
+ const chunk = decoder.decode(value);
491
+ const lines = chunk.split('\n');
492
+
493
+ for (const line of lines) {
494
+ if (line.startsWith('data: ')) {
495
+ const data = JSON.parse(line.slice(6));
496
+ yield data.choices[0].delta.content;
497
+ }
498
+ }
499
+ }
500
+ }
501
+
502
+ // 使用示例
503
+ async generateWithStreaming(prompt: string): Promise<string> {
504
+ let fullResponse = '';
505
+
506
+ for await (const chunk of this.generateStream(prompt)) {
507
+ fullResponse += chunk;
508
+ // 实时输出到用户
509
+ process.stdout.write(chunk);
510
+ }
511
+
512
+ return fullResponse;
513
+ }
514
+ }
515
+ ```
516
+
517
+ ---
518
+
519
+ ## 并发优化
520
+
521
+ ### 1. Worker 并发控制
522
+
523
+ ```typescript
524
+ // 每个 Worker 的并发数配置
525
+ export class WorkerConcurrencyManager {
526
+ private concurrency: number;
527
+ private runningTasks = 0;
528
+ private queue: Array<() => Promise<any>> = [];
529
+
530
+ constructor(concurrency: number = 2) {
531
+ this.concurrency = concurrency;
532
+ }
533
+
534
+ async execute<T>(task: () => Promise<T>): Promise<T> {
535
+ // 如果已满,等待
536
+ while (this.runningTasks >= this.concurrency) {
537
+ await this.delay(100);
538
+ }
539
+
540
+ this.runningTasks++;
541
+
542
+ try {
543
+ return await task();
544
+ } finally {
545
+ this.runningTasks--;
546
+ }
547
+ }
548
+
549
+ private delay(ms: number): Promise<void> {
550
+ return new Promise(resolve => setTimeout(resolve, ms));
551
+ }
552
+ }
553
+ ```
554
+
555
+ ### 2. 任务分片
556
+
557
+ ```typescript
558
+ // 将大任务拆分为小任务
559
+ export class TaskPartitioner {
560
+ partition(content: string, maxLength: number = 3000): string[] {
561
+ const chunks: string[] = [];
562
+ let currentChunk = '';
563
+
564
+ // 按段落分割
565
+ const paragraphs = content.split('\n\n');
566
+
567
+ for (const paragraph of paragraphs) {
568
+ if (currentChunk.length + paragraph.length > maxLength) {
569
+ if (currentChunk) {
570
+ chunks.push(currentChunk);
571
+ }
572
+ currentChunk = paragraph;
573
+ } else {
574
+ currentChunk += '\n\n' + paragraph;
575
+ }
576
+ }
577
+
578
+ if (currentChunk) {
579
+ chunks.push(currentChunk);
580
+ }
581
+
582
+ return chunks;
583
+ }
584
+ }
585
+ ```
586
+
587
+ ### 3. 并行处理独立任务
588
+
589
+ ```typescript
590
+ // 并行处理多个独立的质量检查
591
+ export class ParallelQualityChecker {
592
+ async checkParallel(content: string): Promise<CheckResult[]> {
593
+ const checkers = [
594
+ new WordCountChecker(),
595
+ new KeywordChecker(),
596
+ new StructureChecker(),
597
+ new ForbiddenWordsChecker()
598
+ ];
599
+
600
+ // 并行执行
601
+ const results = await Promise.all(
602
+ checkers.map(checker =>
603
+ this.executeWithTimeout(checker.check(content), 5000)
604
+ )
605
+ );
606
+
607
+ return results;
608
+ }
609
+
610
+ private async executeWithTimeout<T>(
611
+ fn: Promise<T>,
612
+ timeout: number
613
+ ): Promise<T> {
614
+ return Promise.race([
615
+ fn,
616
+ new Promise<T>((_, reject) =>
617
+ setTimeout(() => reject(new Error('Timeout')), timeout)
618
+ )
619
+ ]);
620
+ }
621
+ }
622
+ ```
623
+
624
+ ---
625
+
626
+ ## 内存优化
627
+
628
+ ### 1. 流式处理
629
+
630
+ ```typescript
631
+ // 使用流避免大文件占用内存
632
+ import { Readable } from 'stream';
633
+
634
+ export class StreamingContentProcessor {
635
+ async processLargeContent(stream: Readable): Promise<void> {
636
+ const chunks: Buffer[] = [];
637
+
638
+ return new Promise((resolve, reject) => {
639
+ stream.on('data', (chunk: Buffer) => {
640
+ chunks.push(chunk);
641
+
642
+ // 限制内存使用
643
+ const totalSize = chunks.reduce((sum, c) => sum + c.length, 0);
644
+ if (totalSize > 10 * 1024 * 1024) { // 10MB
645
+ stream.pause();
646
+ // 处理已接收的数据
647
+ this.processBatch(chunks);
648
+ chunks.length = 0;
649
+ stream.resume();
650
+ }
651
+ });
652
+
653
+ stream.on('end', () => {
654
+ this.processBatch(chunks);
655
+ resolve();
656
+ });
657
+
658
+ stream.on('error', reject);
659
+ });
660
+ }
661
+
662
+ private processBatch(chunks: Buffer[]): void {
663
+ // 处理数据批次
664
+ }
665
+ }
666
+ ```
667
+
668
+ ### 2. 对象池
669
+
670
+ ```typescript
671
+ // 复用昂贵对象
672
+ export class ObjectPool<T> {
673
+ private pool: T[] = [];
674
+ private factory: () => T;
675
+ private reset?: (obj: T) => void;
676
+
677
+ constructor(factory: () => T, reset?: (obj: T) => void, initialSize = 5) {
678
+ this.factory = factory;
679
+ this.reset = reset;
680
+
681
+ for (let i = 0; i < initialSize; i++) {
682
+ this.pool.push(factory());
683
+ }
684
+ }
685
+
686
+ acquire(): T {
687
+ if (this.pool.length > 0) {
688
+ return this.pool.pop()!;
689
+ }
690
+ return this.factory();
691
+ }
692
+
693
+ release(obj: T): void {
694
+ if (this.reset) {
695
+ this.reset(obj);
696
+ }
697
+ this.pool.push(obj);
698
+ }
699
+
700
+ get size(): number {
701
+ return this.pool.length;
702
+ }
703
+ }
704
+
705
+ // 使用示例
706
+ const bufferPool = new ObjectPool(
707
+ () => new Buffer(1024 * 1024), // 1MB buffer
708
+ (buf) => buf.fill(0),
709
+ 5 // 预创建5个
710
+ );
711
+ ```
712
+
713
+ ---
714
+
715
+ ## 网络优化
716
+
717
+ ### 1. HTTP 连接复用
718
+
719
+ ```typescript
720
+ // 使用 HTTP Agent 复用连接
721
+ import { Agent } from 'https';
722
+
723
+ const agent = new Agent({
724
+ keepAlive: true,
725
+ keepAliveMsecs: 60000,
726
+ maxSockets: 100,
727
+ maxFreeSockets: 10,
728
+ timeout: 30000,
729
+ });
730
+
731
+ export class OptimizedHTTPClient {
732
+ async fetch(url: string, options: RequestInit = {}): Promise<Response> {
733
+ return fetch(url, {
734
+ ...options,
735
+ agent, // 复用连接
736
+ });
737
+ }
738
+ }
739
+ ```
740
+
741
+ ### 2. 请求合并
742
+
743
+ ```typescript
744
+ // 合并多个请求
745
+ export class RequestBatcher {
746
+ private requests: Map<string, Promise<any>> = new Map();
747
+ private pendingTimer?: NodeJS.Timeout;
748
+
749
+ async batchRequest<T>(key: string, requestFn: () => Promise<T>): Promise<T> {
750
+ // 如果已有相同请求在等待,返回同一个 Promise
751
+ if (this.requests.has(key)) {
752
+ return this.requests.get(key);
753
+ }
754
+
755
+ // 创建新请求
756
+ const promise = requestFn().finally(() => {
757
+ this.requests.delete(key);
758
+ });
759
+
760
+ this.requests.set(key, promise);
761
+
762
+ return promise;
763
+ }
764
+
765
+ // 延迟执行批量操作
766
+ scheduleBatch(key: string, fn: () => Promise<void>, delay: number = 100) {
767
+ if (this.pendingTimer) {
768
+ clearTimeout(this.pendingTimer);
769
+ }
770
+
771
+ this.pendingTimer = setTimeout(async () => {
772
+ await fn();
773
+ this.pendingTimer = undefined;
774
+ }, delay);
775
+ }
776
+ }
777
+ ```
778
+
779
+ ---
780
+
781
+ ## 性能测试
782
+
783
+ ### 1. 基准测试
784
+
785
+ ```typescript
786
+ // tests/performance/llm.bench.ts
787
+ import { Benchmark } from 'vitest';
788
+
789
+ describe('LLM Performance Benchmarks', () => {
790
+ Benchmark('LLM generation', async (bench) => {
791
+ const llmService = new LLMService();
792
+ const prompt = '测试提示词';
793
+
794
+ await bench('warmup', async () => {
795
+ await llmService.generate(prompt);
796
+ }, { iterations: 10, warmupIterations: 5 });
797
+
798
+ await bench('normal', async () => {
799
+ await llmService.generate(prompt);
800
+ }, { iterations: 100 });
801
+ });
802
+
803
+ Benchmark('Cache performance', async (bench) => {
804
+ const cache = new RedisCache();
805
+
806
+ await bench('set', async () => {
807
+ await cache.set('test-key', { data: 'test' });
808
+ }, { iterations: 1000 });
809
+
810
+ await bench('get', async () => {
811
+ await cache.get('test-key');
812
+ }, { iterations: 1000 });
813
+ });
814
+ });
815
+ ```
816
+
817
+ ### 2. 负载测试
818
+
819
+ ```typescript
820
+ // tests/performance/load.test.ts
821
+ import { describe, it } from 'vitest';
822
+
823
+ describe('Load Tests', () => {
824
+ it('should handle 100 concurrent tasks', async () => {
825
+ const tasks = Array(100).fill(null).map((_, i) => ({
826
+ mode: 'async',
827
+ topic: `测试任务 ${i}`,
828
+ requirements: '测试描述',
829
+ }));
830
+
831
+ const startTime = Date.now();
832
+
833
+ const results = await Promise.all(
834
+ tasks.map(task => scheduler.scheduleTask(task))
835
+ );
836
+
837
+ const duration = Date.now() - startTime;
838
+
839
+ expect(duration).toBeLessThan(5000); // 5秒内完成
840
+ expect(results).toHaveLength(100);
841
+ });
842
+ });
843
+ ```
844
+
845
+ ### 3. 性能分析
846
+
847
+ ```typescript
848
+ // 使用 Node.js profiler
849
+ import { performance } from 'perf_hooks';
850
+
851
+ export class PerformanceProfiler {
852
+ startProfiling(id: string) {
853
+ performance.mark(`${id}-start`);
854
+ }
855
+
856
+ endProfiling(id: string): number {
857
+ performance.mark(`${id}-end`);
858
+ performance.measure(id, `${id}-start`, `${id}-end`);
859
+
860
+ const measure = performance.getEntriesByName(id)[0];
861
+ return measure.duration;
862
+ }
863
+
864
+ // 分析内存使用
865
+ getMemoryUsage(): NodeJS.MemoryUsage {
866
+ return process.memoryUsage();
867
+ }
868
+
869
+ // GC 触发
870
+ forceGC() {
871
+ if (global.gc) {
872
+ global.gc();
873
+ }
874
+ }
875
+ }
876
+
877
+ // 使用示例
878
+ const profiler = new PerformanceProfiler();
879
+
880
+ profiler.startProfiling('task-processing');
881
+ await processTask();
882
+ const duration = profiler.endProfiling('task-processing');
883
+ console.log(`Task took ${duration}ms`);
884
+
885
+ // 内存分析
886
+ const memBefore = profiler.getMemoryUsage();
887
+ // ... 执行操作
888
+ profiler.forceGC();
889
+ const memAfter = profiler.getMemoryUsage();
890
+ console.log(`Memory delta: ${memAfter.heapUsed - memBefore.heapUsed} bytes`);
891
+ ```
892
+
893
+ ---
894
+
895
+ ## 性能指标
896
+
897
+ ### 目标指标
898
+
899
+ | 指标 | 当前 | 目标 | 方法 |
900
+ |------|------|------|------|
901
+ | 单任务延迟 | ~120秒 | <90秒 | LLM缓存、并行处理 |
902
+ | 并发任务数 | 2 | 10 | 增加Worker并发 |
903
+ | 内存占用 | 未知 | <2GB | 流式处理、对象池 |
904
+ | 缓存命中率 | 0% | >60% | 多级缓存 |
905
+ | API 调用次数 | 未知 | -30% | 结果缓存、Prompt优化 |
906
+
907
+ ### 监控指标
908
+
909
+ ```typescript
910
+ // Prometheus 指标
911
+ const performanceMetrics = {
912
+ // 响应时间
913
+ taskDuration_p99: 300, // 99分位 < 5分钟
914
+ llmRequestDuration_p95: 60, // 95分位 < 1分钟
915
+
916
+ // 吞吐量
917
+ tasksPerMinute: 5, // 5个任务/分钟
918
+ tasksPerDay: 7200, // 7200个任务/天(理论值)
919
+
920
+ // 缓存
921
+ cacheHitRate_llm: 0.4, // LLM缓存命中率 > 40%
922
+ cacheHitRate_search: 0.6, // 搜索缓存命中率 > 60%
923
+
924
+ // 资源
925
+ memoryUsage_heapUsed: 1024 * 1024 * 1024, // < 1GB
926
+ cpuUsage_percent: 70, // < 70%
927
+ };
928
+ ```
929
+
930
+ ---
931
+
932
+ **文档生成时间**: 2026-01-19
933
+ **版本**: 1.0