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,596 @@
1
+ /**
2
+ * Enhanced LLM Service
3
+ *
4
+ * 在原有 LLMService 基础上添加:
5
+ * - 重试机制(指数退避)
6
+ * - Token 使用记录
7
+ * - 成本估算
8
+ * - 性能监控
9
+ */
10
+
11
+ import axios, { AxiosError } from 'axios';
12
+ import { config } from '../../config/index.js';
13
+ import { createLogger } from '../../infrastructure/logging/logger.js';
14
+
15
+ const logger = createLogger('LLM:Enhanced');
16
+
17
+ /**
18
+ * Chat 消息
19
+ */
20
+ export interface ChatMessage {
21
+ role: 'system' | 'user' | 'assistant';
22
+ content: string;
23
+ }
24
+
25
+ /**
26
+ * Chat 请求参数
27
+ */
28
+ export interface ChatRequest {
29
+ messages: ChatMessage[];
30
+ model?: string;
31
+ maxTokens?: number;
32
+ temperature?: number;
33
+ stream?: boolean;
34
+ taskId?: string; // 任务 ID(用于 Token 记录)
35
+ stepName?: string; // 步骤名称(用于 Token 记录)
36
+ }
37
+
38
+ /**
39
+ * Chat 响应
40
+ */
41
+ export interface ChatResponse {
42
+ id: string;
43
+ object: string;
44
+ created: number;
45
+ model: string;
46
+ choices: Array<{
47
+ index: number;
48
+ message: {
49
+ role: string;
50
+ content: string;
51
+ };
52
+ finishReason: string | null;
53
+ }>;
54
+ usage: {
55
+ promptTokens: number;
56
+ completionTokens: number;
57
+ totalTokens: number;
58
+ };
59
+ }
60
+
61
+ /**
62
+ * Token 使用统计
63
+ */
64
+ export interface TokenUsage {
65
+ promptTokens: number;
66
+ completionTokens: number;
67
+ totalTokens: number;
68
+ }
69
+
70
+ /**
71
+ * 重试配置
72
+ */
73
+ export interface RetryConfig {
74
+ maxRetries: number; // 最大重试次数
75
+ initialDelay: number; // 初始延迟(毫秒)
76
+ maxDelay: number; // 最大延迟(毫秒)
77
+ backoffMultiplier: number; // 退避乘数
78
+ }
79
+
80
+ /**
81
+ * 默认重试配置
82
+ */
83
+ const DEFAULT_RETRY_CONFIG: RetryConfig = {
84
+ maxRetries: 3,
85
+ initialDelay: 1000, // 1 秒
86
+ maxDelay: 10000, // 10 秒
87
+ backoffMultiplier: 2, // 指数退避
88
+ };
89
+
90
+ /**
91
+ * 成本配置(元/1k tokens)
92
+ */
93
+ const COST_CONFIG: Record<string, Record<string, { costPer1kTokensIn: number; costPer1kTokensOut: number }>> = {
94
+ deepseek: {
95
+ 'deepseek-chat': {
96
+ costPer1kTokensIn: 0.001,
97
+ costPer1kTokensOut: 0.002,
98
+ },
99
+ },
100
+ };
101
+
102
+ /**
103
+ * 增强的 LLM 服务类
104
+ */
105
+ export class EnhancedLLMService {
106
+ private baseURL: string;
107
+ private apiKey: string;
108
+ private modelName: string;
109
+ private maxTokens: number;
110
+ private temperature: number;
111
+ private retryConfig: RetryConfig;
112
+ private tokenUsageRepo: TempTokenUsageRepository;
113
+
114
+ constructor(retryConfig?: Partial<RetryConfig>) {
115
+ this.baseURL = config.llm.baseURL;
116
+ this.apiKey = config.llm.apiKey;
117
+ this.modelName = config.llm.modelName;
118
+ this.maxTokens = config.llm.maxTokens;
119
+ this.temperature = config.llm.temperature;
120
+ this.retryConfig = { ...DEFAULT_RETRY_CONFIG, ...retryConfig };
121
+ this.tokenUsageRepo = new TempTokenUsageRepository();
122
+ }
123
+
124
+ /**
125
+ * 聊天对话(带重试和 Token 记录)
126
+ */
127
+ async chat(request: ChatRequest): Promise<{
128
+ content: string;
129
+ usage: TokenUsage;
130
+ cost: number;
131
+ }> {
132
+ const startTime = Date.now();
133
+
134
+ try {
135
+ logger.debug('Sending chat request with retry', {
136
+ model: request.model || this.modelName,
137
+ messagesCount: request.messages.length,
138
+ maxRetries: this.retryConfig.maxRetries,
139
+ stream: request.stream || false,
140
+ });
141
+
142
+ // 带重试的请求
143
+ const response = await this.chatWithRetry(request);
144
+
145
+ // 提取数据
146
+ const choice = response.choices[0];
147
+ if (!choice) {
148
+ throw new Error('No choice in response');
149
+ }
150
+ const content = choice.message.content;
151
+ const usage = response.usage;
152
+
153
+ // 计算 Token 成本
154
+ const cost = this.calculateCost(
155
+ usage.promptTokens,
156
+ usage.completionTokens,
157
+ this.modelName
158
+ );
159
+
160
+ // 记录 Token 使用
161
+ if (request.taskId && request.stepName) {
162
+ await this.recordTokenUsage({
163
+ taskId: request.taskId,
164
+ stepName: request.stepName,
165
+ tokensIn: usage.promptTokens,
166
+ tokensOut: usage.completionTokens,
167
+ totalTokens: usage.totalTokens,
168
+ costPer1kTokensIn: COST_CONFIG.deepseek?.[this.modelName]?.costPer1kTokensIn || 0,
169
+ costPer1kTokensOut: COST_CONFIG.deepseek?.[this.modelName]?.costPer1kTokensOut || 0,
170
+ totalCost: cost,
171
+ duration: Date.now() - startTime,
172
+ });
173
+ }
174
+
175
+ logger.info('Chat request completed', {
176
+ promptTokens: usage.promptTokens,
177
+ completionTokens: usage.completionTokens,
178
+ totalTokens: usage.totalTokens,
179
+ cost,
180
+ finishReason: choice.finishReason,
181
+ duration: Date.now() - startTime,
182
+ stream: request.stream || false,
183
+ });
184
+
185
+ return {
186
+ content,
187
+ usage: {
188
+ promptTokens: usage.promptTokens,
189
+ completionTokens: usage.completionTokens,
190
+ totalTokens: usage.totalTokens,
191
+ },
192
+ cost,
193
+ };
194
+ } catch (error) {
195
+ logger.error('LLM API request failed after retries', {
196
+ error: error instanceof Error ? error.message : String(error),
197
+ duration: Date.now() - startTime,
198
+ stream: request.stream || false,
199
+ });
200
+ throw error;
201
+ }
202
+ }
203
+
204
+ /**
205
+ * 带重试的聊天请求
206
+ */
207
+ private async chatWithRetry(request: ChatRequest): Promise<ChatResponse> {
208
+ let lastError: Error | null = null;
209
+
210
+ for (let attempt = 0; attempt <= this.retryConfig.maxRetries; attempt++) {
211
+ try {
212
+ if (attempt > 0) {
213
+ const delay = this.calculateRetryDelay(attempt);
214
+ logger.info(`Retrying chat request (attempt ${attempt + 1}/${this.retryConfig.maxRetries + 1})`, {
215
+ delay,
216
+ });
217
+ await this.sleep(delay);
218
+ }
219
+
220
+ return await this.chatRequest(request);
221
+ } catch (error) {
222
+ lastError = error instanceof Error ? error : new Error(String(error));
223
+
224
+ // 判断是否应该重试
225
+ if (!this.shouldRetry(lastError, attempt)) {
226
+ throw lastError;
227
+ }
228
+
229
+ logger.warn('Chat request failed, will retry', {
230
+ attempt: attempt + 1,
231
+ error: lastError.message,
232
+ });
233
+ }
234
+ }
235
+
236
+ throw lastError;
237
+ }
238
+
239
+ /**
240
+ * 单次聊天请求
241
+ */
242
+ private async chatRequest(request: ChatRequest): Promise<ChatResponse> {
243
+ const maxTokens = typeof (request.maxTokens || this.maxTokens) === 'number'
244
+ ? request.maxTokens || this.maxTokens
245
+ : parseInt(String(request.maxTokens || this.maxTokens));
246
+
247
+ const temperature = typeof (request.temperature ?? this.temperature) === 'number'
248
+ ? request.temperature ?? this.temperature
249
+ : parseFloat(String(request.temperature ?? this.temperature));
250
+
251
+ const useStream = request.stream || false;
252
+
253
+ // 流式请求
254
+ if (useStream) {
255
+ return await this.chatRequestStream(
256
+ request,
257
+ maxTokens,
258
+ temperature
259
+ );
260
+ }
261
+
262
+ // 非流式请求
263
+ const response = await axios.post<ChatResponse>(
264
+ `${this.baseURL}/v1/chat/completions`,
265
+ {
266
+ model: request.model || this.modelName,
267
+ messages: request.messages,
268
+ max_tokens: maxTokens,
269
+ temperature: temperature,
270
+ stream: false,
271
+ },
272
+ {
273
+ headers: {
274
+ 'Authorization': `Bearer ${this.apiKey}`,
275
+ 'Content-Type': 'application/json',
276
+ },
277
+ timeout: 60000, // 60 秒超时
278
+ }
279
+ );
280
+
281
+ return response.data;
282
+ }
283
+
284
+ /**
285
+ * 流式聊天请求
286
+ */
287
+ private async chatRequestStream(
288
+ request: ChatRequest,
289
+ maxTokens: number,
290
+ temperature: number
291
+ ): Promise<ChatResponse> {
292
+ logger.debug('Starting stream request', {
293
+ model: request.model || this.modelName,
294
+ });
295
+
296
+ const response = await axios.post(
297
+ `${this.baseURL}/v1/chat/completions`,
298
+ {
299
+ model: request.model || this.modelName,
300
+ messages: request.messages,
301
+ max_tokens: maxTokens,
302
+ temperature: temperature,
303
+ stream: true,
304
+ },
305
+ {
306
+ headers: {
307
+ 'Authorization': `Bearer ${this.apiKey}`,
308
+ 'Content-Type': 'application/json',
309
+ },
310
+ responseType: 'stream',
311
+ timeout: 120000, // 流式请求超时时间更长
312
+ }
313
+ );
314
+
315
+ // 收集流式数据
316
+ let fullContent = '';
317
+ let finishReason: string | null = null;
318
+ let promptTokens = 0;
319
+ let completionTokens = 0;
320
+ let totalTokens = 0;
321
+
322
+ return new Promise((resolve, reject) => {
323
+ const chunks: Buffer[] = [];
324
+
325
+ response.data.on('data', (chunk: Buffer) => {
326
+ chunks.push(chunk);
327
+
328
+ // 解析 SSE 数据
329
+ const chunkStr = chunk.toString('utf-8');
330
+ const lines = chunkStr.split('\n').filter(line => line.trim() !== '');
331
+
332
+ for (const line of lines) {
333
+ if (line.startsWith('data: ')) {
334
+ const data = line.slice(6);
335
+
336
+ if (data === '[DONE]') {
337
+ continue;
338
+ }
339
+
340
+ try {
341
+ const parsed = JSON.parse(data);
342
+ const delta = parsed.choices?.[0]?.delta;
343
+
344
+ if (delta?.content) {
345
+ fullContent += delta.content;
346
+ }
347
+
348
+ if (parsed.choices?.[0]?.finish_reason) {
349
+ finishReason = parsed.choices[0].finish_reason;
350
+ }
351
+
352
+ // 从最后一个 chunk 获取 usage 信息
353
+ if (parsed.usage) {
354
+ promptTokens = parsed.usage.prompt_tokens || 0;
355
+ completionTokens = parsed.usage.completion_tokens || 0;
356
+ totalTokens = parsed.usage.total_tokens || 0;
357
+ }
358
+ } catch (error) {
359
+ // 忽略解析错误(可能是空行或其他格式)
360
+ }
361
+ }
362
+ }
363
+ });
364
+
365
+ response.data.on('end', () => {
366
+ logger.debug('Stream request completed', {
367
+ contentLength: fullContent.length,
368
+ finishReason,
369
+ });
370
+
371
+ // 构造标准响应格式
372
+ resolve({
373
+ id: `chatcmpl-${Date.now()}`,
374
+ object: 'chat.completion',
375
+ created: Math.floor(Date.now() / 1000),
376
+ model: request.model || this.modelName,
377
+ choices: [{
378
+ index: 0,
379
+ message: {
380
+ role: 'assistant',
381
+ content: fullContent,
382
+ },
383
+ finishReason,
384
+ }],
385
+ usage: {
386
+ promptTokens,
387
+ completionTokens,
388
+ totalTokens,
389
+ },
390
+ });
391
+ });
392
+
393
+ response.data.on('error', (error: Error) => {
394
+ logger.error('Stream request error', { error: error.message });
395
+ reject(error);
396
+ });
397
+ });
398
+ }
399
+
400
+ /**
401
+ * 判断是否应该重试
402
+ */
403
+ private shouldRetry(error: Error, attempt: number): boolean {
404
+ // 超过最大重试次数
405
+ if (attempt >= this.retryConfig.maxRetries) {
406
+ return false;
407
+ }
408
+
409
+ // 网络错误或超时
410
+ if (axios.isAxiosError(error)) {
411
+ const axiosError = error as AxiosError;
412
+ const isRetryable =
413
+ !axiosError.response || // 无响应(网络错误)
414
+ axiosError.response.status >= 500 || // 服务器错误
415
+ axiosError.response.status === 429; // 限流
416
+
417
+ return isRetryable;
418
+ }
419
+
420
+ // 其他错误不重试
421
+ return false;
422
+ }
423
+
424
+ /**
425
+ * 计算重试延迟(指数退避)
426
+ */
427
+ private calculateRetryDelay(attempt: number): number {
428
+ const delay = Math.min(
429
+ this.retryConfig.initialDelay * Math.pow(this.retryConfig.backoffMultiplier, attempt),
430
+ this.retryConfig.maxDelay
431
+ );
432
+ return delay;
433
+ }
434
+
435
+ /**
436
+ * 计算 Token 成本
437
+ */
438
+ private calculateCost(
439
+ tokensIn: number,
440
+ tokensOut: number,
441
+ modelName: string = this.modelName
442
+ ): number {
443
+ const config = COST_CONFIG.deepseek?.[modelName];
444
+ if (!config) {
445
+ logger.warn(`No cost config found for model: ${modelName}`);
446
+ return 0;
447
+ }
448
+
449
+ const costIn = (tokensIn / 1000) * config.costPer1kTokensIn;
450
+ const costOut = (tokensOut / 1000) * config.costPer1kTokensOut;
451
+
452
+ return Number((costIn + costOut).toFixed(6));
453
+ }
454
+
455
+ /**
456
+ * 记录 Token 使用到数据库
457
+ */
458
+ private async recordTokenUsage(params: {
459
+ taskId: string;
460
+ stepName: string;
461
+ tokensIn: number;
462
+ tokensOut: number;
463
+ totalTokens: number;
464
+ costPer1kTokensIn: number;
465
+ costPer1kTokensOut: number;
466
+ totalCost: number;
467
+ duration: number;
468
+ }): Promise<void> {
469
+ try {
470
+ await this.tokenUsageRepo.create({
471
+ taskId: params.taskId,
472
+ traceId: this.generateTraceId(),
473
+ stepName: params.stepName,
474
+ apiName: 'deepseek',
475
+ modelName: this.modelName,
476
+ tokensIn: params.tokensIn,
477
+ tokensOut: params.tokensOut,
478
+ totalTokens: params.totalTokens,
479
+ costPer1kTokensIn: params.costPer1kTokensIn,
480
+ costPer1kTokensOut: params.costPer1kTokensOut,
481
+ totalCost: params.totalCost,
482
+ metadata: {
483
+ duration: params.duration,
484
+ temperature: this.temperature,
485
+ maxTokens: this.maxTokens,
486
+ },
487
+ });
488
+
489
+ logger.debug('Token usage recorded', {
490
+ taskId: params.taskId,
491
+ stepName: params.stepName,
492
+ totalTokens: params.totalTokens,
493
+ cost: params.totalCost,
494
+ });
495
+ } catch (error) {
496
+ logger.error('Failed to record token usage', {
497
+ error: error instanceof Error ? error.message : String(error),
498
+ });
499
+ // 不抛出错误,避免影响主流程
500
+ }
501
+ }
502
+
503
+ /**
504
+ * 生成 Trace ID(用于链路追踪)
505
+ */
506
+ private generateTraceId(): string {
507
+ return `trace_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;
508
+ }
509
+
510
+ /**
511
+ * 延迟函数
512
+ */
513
+ private sleep(ms: number): Promise<void> {
514
+ return new Promise((resolve) => setTimeout(resolve, ms));
515
+ }
516
+
517
+ /**
518
+ * 生成文本(简化接口)
519
+ */
520
+ async generateText(
521
+ prompt: string,
522
+ systemPrompt?: string,
523
+ options?: {
524
+ taskId?: string;
525
+ stepName?: string;
526
+ }
527
+ ): Promise<string> {
528
+ const messages: ChatMessage[] = [];
529
+
530
+ if (systemPrompt) {
531
+ messages.push({ role: 'system', content: systemPrompt });
532
+ }
533
+
534
+ messages.push({ role: 'user', content: prompt });
535
+
536
+ const result = await this.chat({
537
+ messages,
538
+ taskId: options?.taskId,
539
+ stepName: options?.stepName,
540
+ });
541
+
542
+ return result.content;
543
+ }
544
+
545
+ /**
546
+ * 健康检查
547
+ */
548
+ async healthCheck(): Promise<boolean> {
549
+ try {
550
+ await this.chat({
551
+ messages: [{ role: 'user', content: 'test' }],
552
+ maxTokens: 1,
553
+ });
554
+
555
+ logger.info('LLM service health check passed');
556
+ return true;
557
+ } catch (error) {
558
+ logger.error('LLM service health check failed', error as Error);
559
+ return false;
560
+ }
561
+ }
562
+
563
+ /**
564
+ * 计算文本的 Token 数量(估算)
565
+ */
566
+ estimateTokens(text: string): number {
567
+ // 粗略估算:英文约 4 字符/token,中文约 1.5 字符/token
568
+ const chineseChars = (text.match(/[\u4e00-\u9fa5]/g) || []).length;
569
+ const englishChars = text.length - chineseChars;
570
+
571
+ return Math.ceil(chineseChars / 1.5 + englishChars / 4);
572
+ }
573
+
574
+ /**
575
+ * 估算成本(未调用 API)
576
+ */
577
+ estimateCost(tokensIn: number, tokensOut: number): number {
578
+ return this.calculateCost(tokensIn, tokensOut, this.modelName);
579
+ }
580
+ }
581
+
582
+ /**
583
+ * Token Usage Repository(临时实现,后续使用真实的 Repository)
584
+ */
585
+ class TempTokenUsageRepository {
586
+ async create(params: any): Promise<void> {
587
+ // TODO: 实现真实的数据库保存
588
+ // 目前只记录日志
589
+ logger.debug('Token usage would be saved to database', params);
590
+ }
591
+ }
592
+
593
+ /**
594
+ * 增强的 LLM 服务单例
595
+ */
596
+ export const enhancedLLMService = new EnhancedLLMService();