empathy-framework 5.3.0__py3-none-any.whl → 5.4.0__py3-none-any.whl

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 (458) hide show
  1. empathy_framework-5.4.0.dist-info/METADATA +47 -0
  2. empathy_framework-5.4.0.dist-info/RECORD +8 -0
  3. {empathy_framework-5.3.0.dist-info → empathy_framework-5.4.0.dist-info}/top_level.txt +0 -1
  4. empathy_healthcare_plugin/__init__.py +12 -11
  5. empathy_llm_toolkit/__init__.py +12 -26
  6. empathy_os/__init__.py +12 -356
  7. empathy_software_plugin/__init__.py +12 -11
  8. empathy_framework-5.3.0.dist-info/METADATA +0 -1026
  9. empathy_framework-5.3.0.dist-info/RECORD +0 -456
  10. empathy_framework-5.3.0.dist-info/entry_points.txt +0 -26
  11. empathy_framework-5.3.0.dist-info/licenses/LICENSE +0 -201
  12. empathy_framework-5.3.0.dist-info/licenses/LICENSE_CHANGE_ANNOUNCEMENT.md +0 -101
  13. empathy_healthcare_plugin/monitors/__init__.py +0 -9
  14. empathy_healthcare_plugin/monitors/clinical_protocol_monitor.py +0 -315
  15. empathy_healthcare_plugin/monitors/monitoring/__init__.py +0 -44
  16. empathy_healthcare_plugin/monitors/monitoring/protocol_checker.py +0 -300
  17. empathy_healthcare_plugin/monitors/monitoring/protocol_loader.py +0 -214
  18. empathy_healthcare_plugin/monitors/monitoring/sensor_parsers.py +0 -306
  19. empathy_healthcare_plugin/monitors/monitoring/trajectory_analyzer.py +0 -389
  20. empathy_healthcare_plugin/protocols/cardiac.json +0 -93
  21. empathy_healthcare_plugin/protocols/post_operative.json +0 -92
  22. empathy_healthcare_plugin/protocols/respiratory.json +0 -92
  23. empathy_healthcare_plugin/protocols/sepsis.json +0 -141
  24. empathy_llm_toolkit/README.md +0 -553
  25. empathy_llm_toolkit/agent_factory/__init__.py +0 -53
  26. empathy_llm_toolkit/agent_factory/adapters/__init__.py +0 -85
  27. empathy_llm_toolkit/agent_factory/adapters/autogen_adapter.py +0 -312
  28. empathy_llm_toolkit/agent_factory/adapters/crewai_adapter.py +0 -483
  29. empathy_llm_toolkit/agent_factory/adapters/haystack_adapter.py +0 -298
  30. empathy_llm_toolkit/agent_factory/adapters/langchain_adapter.py +0 -362
  31. empathy_llm_toolkit/agent_factory/adapters/langgraph_adapter.py +0 -333
  32. empathy_llm_toolkit/agent_factory/adapters/native.py +0 -228
  33. empathy_llm_toolkit/agent_factory/adapters/wizard_adapter.py +0 -423
  34. empathy_llm_toolkit/agent_factory/base.py +0 -305
  35. empathy_llm_toolkit/agent_factory/crews/__init__.py +0 -67
  36. empathy_llm_toolkit/agent_factory/crews/code_review.py +0 -1113
  37. empathy_llm_toolkit/agent_factory/crews/health_check.py +0 -1262
  38. empathy_llm_toolkit/agent_factory/crews/refactoring.py +0 -1128
  39. empathy_llm_toolkit/agent_factory/crews/security_audit.py +0 -1018
  40. empathy_llm_toolkit/agent_factory/decorators.py +0 -287
  41. empathy_llm_toolkit/agent_factory/factory.py +0 -558
  42. empathy_llm_toolkit/agent_factory/framework.py +0 -193
  43. empathy_llm_toolkit/agent_factory/memory_integration.py +0 -328
  44. empathy_llm_toolkit/agent_factory/resilient.py +0 -320
  45. empathy_llm_toolkit/agents_md/__init__.py +0 -22
  46. empathy_llm_toolkit/agents_md/loader.py +0 -218
  47. empathy_llm_toolkit/agents_md/parser.py +0 -271
  48. empathy_llm_toolkit/agents_md/registry.py +0 -307
  49. empathy_llm_toolkit/claude_memory.py +0 -466
  50. empathy_llm_toolkit/cli/__init__.py +0 -8
  51. empathy_llm_toolkit/cli/sync_claude.py +0 -487
  52. empathy_llm_toolkit/code_health.py +0 -1313
  53. empathy_llm_toolkit/commands/__init__.py +0 -51
  54. empathy_llm_toolkit/commands/context.py +0 -375
  55. empathy_llm_toolkit/commands/loader.py +0 -301
  56. empathy_llm_toolkit/commands/models.py +0 -231
  57. empathy_llm_toolkit/commands/parser.py +0 -371
  58. empathy_llm_toolkit/commands/registry.py +0 -429
  59. empathy_llm_toolkit/config/__init__.py +0 -29
  60. empathy_llm_toolkit/config/unified.py +0 -291
  61. empathy_llm_toolkit/context/__init__.py +0 -22
  62. empathy_llm_toolkit/context/compaction.py +0 -455
  63. empathy_llm_toolkit/context/manager.py +0 -434
  64. empathy_llm_toolkit/contextual_patterns.py +0 -361
  65. empathy_llm_toolkit/core.py +0 -907
  66. empathy_llm_toolkit/git_pattern_extractor.py +0 -435
  67. empathy_llm_toolkit/hooks/__init__.py +0 -24
  68. empathy_llm_toolkit/hooks/config.py +0 -306
  69. empathy_llm_toolkit/hooks/executor.py +0 -289
  70. empathy_llm_toolkit/hooks/registry.py +0 -302
  71. empathy_llm_toolkit/hooks/scripts/__init__.py +0 -39
  72. empathy_llm_toolkit/hooks/scripts/evaluate_session.py +0 -201
  73. empathy_llm_toolkit/hooks/scripts/first_time_init.py +0 -285
  74. empathy_llm_toolkit/hooks/scripts/pre_compact.py +0 -207
  75. empathy_llm_toolkit/hooks/scripts/session_end.py +0 -183
  76. empathy_llm_toolkit/hooks/scripts/session_start.py +0 -163
  77. empathy_llm_toolkit/hooks/scripts/suggest_compact.py +0 -225
  78. empathy_llm_toolkit/learning/__init__.py +0 -30
  79. empathy_llm_toolkit/learning/evaluator.py +0 -438
  80. empathy_llm_toolkit/learning/extractor.py +0 -514
  81. empathy_llm_toolkit/learning/storage.py +0 -560
  82. empathy_llm_toolkit/levels.py +0 -227
  83. empathy_llm_toolkit/pattern_confidence.py +0 -414
  84. empathy_llm_toolkit/pattern_resolver.py +0 -272
  85. empathy_llm_toolkit/pattern_summary.py +0 -350
  86. empathy_llm_toolkit/providers.py +0 -967
  87. empathy_llm_toolkit/routing/__init__.py +0 -32
  88. empathy_llm_toolkit/routing/model_router.py +0 -362
  89. empathy_llm_toolkit/security/IMPLEMENTATION_SUMMARY.md +0 -413
  90. empathy_llm_toolkit/security/PHASE2_COMPLETE.md +0 -384
  91. empathy_llm_toolkit/security/PHASE2_SECRETS_DETECTOR_COMPLETE.md +0 -271
  92. empathy_llm_toolkit/security/QUICK_REFERENCE.md +0 -316
  93. empathy_llm_toolkit/security/README.md +0 -262
  94. empathy_llm_toolkit/security/__init__.py +0 -62
  95. empathy_llm_toolkit/security/audit_logger.py +0 -929
  96. empathy_llm_toolkit/security/audit_logger_example.py +0 -152
  97. empathy_llm_toolkit/security/pii_scrubber.py +0 -640
  98. empathy_llm_toolkit/security/secrets_detector.py +0 -678
  99. empathy_llm_toolkit/security/secrets_detector_example.py +0 -304
  100. empathy_llm_toolkit/security/secure_memdocs.py +0 -1192
  101. empathy_llm_toolkit/security/secure_memdocs_example.py +0 -278
  102. empathy_llm_toolkit/session_status.py +0 -745
  103. empathy_llm_toolkit/state.py +0 -246
  104. empathy_llm_toolkit/utils/__init__.py +0 -5
  105. empathy_llm_toolkit/utils/tokens.py +0 -349
  106. empathy_os/adaptive/__init__.py +0 -13
  107. empathy_os/adaptive/task_complexity.py +0 -127
  108. empathy_os/agent_monitoring.py +0 -414
  109. empathy_os/cache/__init__.py +0 -117
  110. empathy_os/cache/base.py +0 -166
  111. empathy_os/cache/dependency_manager.py +0 -256
  112. empathy_os/cache/hash_only.py +0 -251
  113. empathy_os/cache/hybrid.py +0 -457
  114. empathy_os/cache/storage.py +0 -285
  115. empathy_os/cache_monitor.py +0 -356
  116. empathy_os/cache_stats.py +0 -298
  117. empathy_os/cli/__init__.py +0 -152
  118. empathy_os/cli/__main__.py +0 -12
  119. empathy_os/cli/commands/__init__.py +0 -1
  120. empathy_os/cli/commands/batch.py +0 -264
  121. empathy_os/cli/commands/cache.py +0 -248
  122. empathy_os/cli/commands/help.py +0 -331
  123. empathy_os/cli/commands/info.py +0 -140
  124. empathy_os/cli/commands/inspect.py +0 -436
  125. empathy_os/cli/commands/inspection.py +0 -57
  126. empathy_os/cli/commands/memory.py +0 -48
  127. empathy_os/cli/commands/metrics.py +0 -92
  128. empathy_os/cli/commands/orchestrate.py +0 -184
  129. empathy_os/cli/commands/patterns.py +0 -207
  130. empathy_os/cli/commands/profiling.py +0 -202
  131. empathy_os/cli/commands/provider.py +0 -98
  132. empathy_os/cli/commands/routing.py +0 -285
  133. empathy_os/cli/commands/setup.py +0 -96
  134. empathy_os/cli/commands/status.py +0 -235
  135. empathy_os/cli/commands/sync.py +0 -166
  136. empathy_os/cli/commands/tier.py +0 -121
  137. empathy_os/cli/commands/utilities.py +0 -114
  138. empathy_os/cli/commands/workflow.py +0 -579
  139. empathy_os/cli/core.py +0 -32
  140. empathy_os/cli/parsers/__init__.py +0 -68
  141. empathy_os/cli/parsers/batch.py +0 -118
  142. empathy_os/cli/parsers/cache.py +0 -65
  143. empathy_os/cli/parsers/help.py +0 -41
  144. empathy_os/cli/parsers/info.py +0 -26
  145. empathy_os/cli/parsers/inspect.py +0 -66
  146. empathy_os/cli/parsers/metrics.py +0 -42
  147. empathy_os/cli/parsers/orchestrate.py +0 -61
  148. empathy_os/cli/parsers/patterns.py +0 -54
  149. empathy_os/cli/parsers/provider.py +0 -40
  150. empathy_os/cli/parsers/routing.py +0 -110
  151. empathy_os/cli/parsers/setup.py +0 -42
  152. empathy_os/cli/parsers/status.py +0 -47
  153. empathy_os/cli/parsers/sync.py +0 -31
  154. empathy_os/cli/parsers/tier.py +0 -33
  155. empathy_os/cli/parsers/workflow.py +0 -77
  156. empathy_os/cli/utils/__init__.py +0 -1
  157. empathy_os/cli/utils/data.py +0 -242
  158. empathy_os/cli/utils/helpers.py +0 -68
  159. empathy_os/cli_legacy.py +0 -3957
  160. empathy_os/cli_minimal.py +0 -1159
  161. empathy_os/cli_router.py +0 -437
  162. empathy_os/cli_unified.py +0 -814
  163. empathy_os/config/__init__.py +0 -66
  164. empathy_os/config/xml_config.py +0 -286
  165. empathy_os/config.py +0 -545
  166. empathy_os/coordination.py +0 -870
  167. empathy_os/core.py +0 -1511
  168. empathy_os/core_modules/__init__.py +0 -15
  169. empathy_os/cost_tracker.py +0 -626
  170. empathy_os/dashboard/__init__.py +0 -41
  171. empathy_os/dashboard/app.py +0 -512
  172. empathy_os/dashboard/simple_server.py +0 -435
  173. empathy_os/dashboard/standalone_server.py +0 -547
  174. empathy_os/discovery.py +0 -306
  175. empathy_os/emergence.py +0 -306
  176. empathy_os/exceptions.py +0 -123
  177. empathy_os/feedback_loops.py +0 -373
  178. empathy_os/hot_reload/README.md +0 -473
  179. empathy_os/hot_reload/__init__.py +0 -62
  180. empathy_os/hot_reload/config.py +0 -83
  181. empathy_os/hot_reload/integration.py +0 -229
  182. empathy_os/hot_reload/reloader.py +0 -298
  183. empathy_os/hot_reload/watcher.py +0 -183
  184. empathy_os/hot_reload/websocket.py +0 -177
  185. empathy_os/levels.py +0 -577
  186. empathy_os/leverage_points.py +0 -441
  187. empathy_os/logging_config.py +0 -261
  188. empathy_os/mcp/__init__.py +0 -10
  189. empathy_os/mcp/server.py +0 -506
  190. empathy_os/memory/__init__.py +0 -237
  191. empathy_os/memory/claude_memory.py +0 -469
  192. empathy_os/memory/config.py +0 -224
  193. empathy_os/memory/control_panel.py +0 -1290
  194. empathy_os/memory/control_panel_support.py +0 -145
  195. empathy_os/memory/cross_session.py +0 -845
  196. empathy_os/memory/edges.py +0 -179
  197. empathy_os/memory/encryption.py +0 -159
  198. empathy_os/memory/file_session.py +0 -770
  199. empathy_os/memory/graph.py +0 -570
  200. empathy_os/memory/long_term.py +0 -913
  201. empathy_os/memory/long_term_types.py +0 -99
  202. empathy_os/memory/mixins/__init__.py +0 -25
  203. empathy_os/memory/mixins/backend_init_mixin.py +0 -249
  204. empathy_os/memory/mixins/capabilities_mixin.py +0 -208
  205. empathy_os/memory/mixins/handoff_mixin.py +0 -208
  206. empathy_os/memory/mixins/lifecycle_mixin.py +0 -49
  207. empathy_os/memory/mixins/long_term_mixin.py +0 -352
  208. empathy_os/memory/mixins/promotion_mixin.py +0 -109
  209. empathy_os/memory/mixins/short_term_mixin.py +0 -182
  210. empathy_os/memory/nodes.py +0 -179
  211. empathy_os/memory/redis_bootstrap.py +0 -540
  212. empathy_os/memory/security/__init__.py +0 -31
  213. empathy_os/memory/security/audit_logger.py +0 -932
  214. empathy_os/memory/security/pii_scrubber.py +0 -640
  215. empathy_os/memory/security/secrets_detector.py +0 -678
  216. empathy_os/memory/short_term.py +0 -2192
  217. empathy_os/memory/simple_storage.py +0 -302
  218. empathy_os/memory/storage/__init__.py +0 -15
  219. empathy_os/memory/storage_backend.py +0 -167
  220. empathy_os/memory/summary_index.py +0 -583
  221. empathy_os/memory/types.py +0 -446
  222. empathy_os/memory/unified.py +0 -182
  223. empathy_os/meta_workflows/__init__.py +0 -74
  224. empathy_os/meta_workflows/agent_creator.py +0 -248
  225. empathy_os/meta_workflows/builtin_templates.py +0 -567
  226. empathy_os/meta_workflows/cli_commands/__init__.py +0 -56
  227. empathy_os/meta_workflows/cli_commands/agent_commands.py +0 -321
  228. empathy_os/meta_workflows/cli_commands/analytics_commands.py +0 -442
  229. empathy_os/meta_workflows/cli_commands/config_commands.py +0 -232
  230. empathy_os/meta_workflows/cli_commands/memory_commands.py +0 -182
  231. empathy_os/meta_workflows/cli_commands/template_commands.py +0 -354
  232. empathy_os/meta_workflows/cli_commands/workflow_commands.py +0 -382
  233. empathy_os/meta_workflows/cli_meta_workflows.py +0 -59
  234. empathy_os/meta_workflows/form_engine.py +0 -292
  235. empathy_os/meta_workflows/intent_detector.py +0 -409
  236. empathy_os/meta_workflows/models.py +0 -569
  237. empathy_os/meta_workflows/pattern_learner.py +0 -738
  238. empathy_os/meta_workflows/plan_generator.py +0 -384
  239. empathy_os/meta_workflows/session_context.py +0 -397
  240. empathy_os/meta_workflows/template_registry.py +0 -229
  241. empathy_os/meta_workflows/workflow.py +0 -984
  242. empathy_os/metrics/__init__.py +0 -12
  243. empathy_os/metrics/collector.py +0 -31
  244. empathy_os/metrics/prompt_metrics.py +0 -194
  245. empathy_os/models/__init__.py +0 -172
  246. empathy_os/models/__main__.py +0 -13
  247. empathy_os/models/adaptive_routing.py +0 -437
  248. empathy_os/models/auth_cli.py +0 -444
  249. empathy_os/models/auth_strategy.py +0 -450
  250. empathy_os/models/cli.py +0 -655
  251. empathy_os/models/empathy_executor.py +0 -354
  252. empathy_os/models/executor.py +0 -257
  253. empathy_os/models/fallback.py +0 -762
  254. empathy_os/models/provider_config.py +0 -282
  255. empathy_os/models/registry.py +0 -472
  256. empathy_os/models/tasks.py +0 -359
  257. empathy_os/models/telemetry/__init__.py +0 -71
  258. empathy_os/models/telemetry/analytics.py +0 -594
  259. empathy_os/models/telemetry/backend.py +0 -196
  260. empathy_os/models/telemetry/data_models.py +0 -431
  261. empathy_os/models/telemetry/storage.py +0 -489
  262. empathy_os/models/token_estimator.py +0 -420
  263. empathy_os/models/validation.py +0 -280
  264. empathy_os/monitoring/__init__.py +0 -52
  265. empathy_os/monitoring/alerts.py +0 -946
  266. empathy_os/monitoring/alerts_cli.py +0 -448
  267. empathy_os/monitoring/multi_backend.py +0 -271
  268. empathy_os/monitoring/otel_backend.py +0 -362
  269. empathy_os/optimization/__init__.py +0 -19
  270. empathy_os/optimization/context_optimizer.py +0 -272
  271. empathy_os/orchestration/__init__.py +0 -67
  272. empathy_os/orchestration/agent_templates.py +0 -707
  273. empathy_os/orchestration/config_store.py +0 -499
  274. empathy_os/orchestration/execution_strategies.py +0 -2111
  275. empathy_os/orchestration/meta_orchestrator.py +0 -1168
  276. empathy_os/orchestration/pattern_learner.py +0 -696
  277. empathy_os/orchestration/real_tools.py +0 -931
  278. empathy_os/pattern_cache.py +0 -187
  279. empathy_os/pattern_library.py +0 -542
  280. empathy_os/patterns/debugging/all_patterns.json +0 -81
  281. empathy_os/patterns/debugging/workflow_20260107_1770825e.json +0 -77
  282. empathy_os/patterns/refactoring_memory.json +0 -89
  283. empathy_os/persistence.py +0 -564
  284. empathy_os/platform_utils.py +0 -265
  285. empathy_os/plugins/__init__.py +0 -28
  286. empathy_os/plugins/base.py +0 -361
  287. empathy_os/plugins/registry.py +0 -268
  288. empathy_os/project_index/__init__.py +0 -32
  289. empathy_os/project_index/cli.py +0 -335
  290. empathy_os/project_index/index.py +0 -667
  291. empathy_os/project_index/models.py +0 -504
  292. empathy_os/project_index/reports.py +0 -474
  293. empathy_os/project_index/scanner.py +0 -777
  294. empathy_os/project_index/scanner_parallel.py +0 -291
  295. empathy_os/prompts/__init__.py +0 -61
  296. empathy_os/prompts/config.py +0 -77
  297. empathy_os/prompts/context.py +0 -177
  298. empathy_os/prompts/parser.py +0 -285
  299. empathy_os/prompts/registry.py +0 -313
  300. empathy_os/prompts/templates.py +0 -208
  301. empathy_os/redis_config.py +0 -302
  302. empathy_os/redis_memory.py +0 -799
  303. empathy_os/resilience/__init__.py +0 -56
  304. empathy_os/resilience/circuit_breaker.py +0 -256
  305. empathy_os/resilience/fallback.py +0 -179
  306. empathy_os/resilience/health.py +0 -300
  307. empathy_os/resilience/retry.py +0 -209
  308. empathy_os/resilience/timeout.py +0 -135
  309. empathy_os/routing/__init__.py +0 -43
  310. empathy_os/routing/chain_executor.py +0 -433
  311. empathy_os/routing/classifier.py +0 -217
  312. empathy_os/routing/smart_router.py +0 -234
  313. empathy_os/routing/workflow_registry.py +0 -343
  314. empathy_os/scaffolding/README.md +0 -589
  315. empathy_os/scaffolding/__init__.py +0 -35
  316. empathy_os/scaffolding/__main__.py +0 -14
  317. empathy_os/scaffolding/cli.py +0 -240
  318. empathy_os/socratic/__init__.py +0 -256
  319. empathy_os/socratic/ab_testing.py +0 -958
  320. empathy_os/socratic/blueprint.py +0 -533
  321. empathy_os/socratic/cli.py +0 -703
  322. empathy_os/socratic/collaboration.py +0 -1114
  323. empathy_os/socratic/domain_templates.py +0 -924
  324. empathy_os/socratic/embeddings.py +0 -738
  325. empathy_os/socratic/engine.py +0 -794
  326. empathy_os/socratic/explainer.py +0 -682
  327. empathy_os/socratic/feedback.py +0 -772
  328. empathy_os/socratic/forms.py +0 -629
  329. empathy_os/socratic/generator.py +0 -732
  330. empathy_os/socratic/llm_analyzer.py +0 -637
  331. empathy_os/socratic/mcp_server.py +0 -702
  332. empathy_os/socratic/session.py +0 -312
  333. empathy_os/socratic/storage.py +0 -667
  334. empathy_os/socratic/success.py +0 -730
  335. empathy_os/socratic/visual_editor.py +0 -860
  336. empathy_os/socratic/web_ui.py +0 -958
  337. empathy_os/telemetry/__init__.py +0 -39
  338. empathy_os/telemetry/agent_coordination.py +0 -475
  339. empathy_os/telemetry/agent_tracking.py +0 -367
  340. empathy_os/telemetry/approval_gates.py +0 -545
  341. empathy_os/telemetry/cli.py +0 -1231
  342. empathy_os/telemetry/commands/__init__.py +0 -14
  343. empathy_os/telemetry/commands/dashboard_commands.py +0 -696
  344. empathy_os/telemetry/event_streaming.py +0 -409
  345. empathy_os/telemetry/feedback_loop.py +0 -567
  346. empathy_os/telemetry/usage_tracker.py +0 -591
  347. empathy_os/templates.py +0 -754
  348. empathy_os/test_generator/__init__.py +0 -38
  349. empathy_os/test_generator/__main__.py +0 -14
  350. empathy_os/test_generator/cli.py +0 -234
  351. empathy_os/test_generator/generator.py +0 -355
  352. empathy_os/test_generator/risk_analyzer.py +0 -216
  353. empathy_os/tier_recommender.py +0 -384
  354. empathy_os/tools.py +0 -183
  355. empathy_os/trust/__init__.py +0 -28
  356. empathy_os/trust/circuit_breaker.py +0 -579
  357. empathy_os/trust_building.py +0 -527
  358. empathy_os/validation/__init__.py +0 -19
  359. empathy_os/validation/xml_validator.py +0 -281
  360. empathy_os/vscode_bridge.py +0 -173
  361. empathy_os/workflow_commands.py +0 -780
  362. empathy_os/workflow_patterns/__init__.py +0 -33
  363. empathy_os/workflow_patterns/behavior.py +0 -249
  364. empathy_os/workflow_patterns/core.py +0 -76
  365. empathy_os/workflow_patterns/output.py +0 -99
  366. empathy_os/workflow_patterns/registry.py +0 -255
  367. empathy_os/workflow_patterns/structural.py +0 -288
  368. empathy_os/workflows/__init__.py +0 -539
  369. empathy_os/workflows/autonomous_test_gen.py +0 -1268
  370. empathy_os/workflows/base.py +0 -2667
  371. empathy_os/workflows/batch_processing.py +0 -342
  372. empathy_os/workflows/bug_predict.py +0 -1084
  373. empathy_os/workflows/builder.py +0 -273
  374. empathy_os/workflows/caching.py +0 -253
  375. empathy_os/workflows/code_review.py +0 -1048
  376. empathy_os/workflows/code_review_adapters.py +0 -312
  377. empathy_os/workflows/code_review_pipeline.py +0 -722
  378. empathy_os/workflows/config.py +0 -645
  379. empathy_os/workflows/dependency_check.py +0 -644
  380. empathy_os/workflows/document_gen/__init__.py +0 -25
  381. empathy_os/workflows/document_gen/config.py +0 -30
  382. empathy_os/workflows/document_gen/report_formatter.py +0 -162
  383. empathy_os/workflows/document_gen/workflow.py +0 -1426
  384. empathy_os/workflows/document_manager.py +0 -216
  385. empathy_os/workflows/document_manager_README.md +0 -134
  386. empathy_os/workflows/documentation_orchestrator.py +0 -1205
  387. empathy_os/workflows/history.py +0 -510
  388. empathy_os/workflows/keyboard_shortcuts/__init__.py +0 -39
  389. empathy_os/workflows/keyboard_shortcuts/generators.py +0 -391
  390. empathy_os/workflows/keyboard_shortcuts/parsers.py +0 -416
  391. empathy_os/workflows/keyboard_shortcuts/prompts.py +0 -295
  392. empathy_os/workflows/keyboard_shortcuts/schema.py +0 -193
  393. empathy_os/workflows/keyboard_shortcuts/workflow.py +0 -509
  394. empathy_os/workflows/llm_base.py +0 -363
  395. empathy_os/workflows/manage_docs.py +0 -87
  396. empathy_os/workflows/manage_docs_README.md +0 -134
  397. empathy_os/workflows/manage_documentation.py +0 -821
  398. empathy_os/workflows/new_sample_workflow1.py +0 -149
  399. empathy_os/workflows/new_sample_workflow1_README.md +0 -150
  400. empathy_os/workflows/orchestrated_health_check.py +0 -849
  401. empathy_os/workflows/orchestrated_release_prep.py +0 -600
  402. empathy_os/workflows/output.py +0 -413
  403. empathy_os/workflows/perf_audit.py +0 -863
  404. empathy_os/workflows/pr_review.py +0 -762
  405. empathy_os/workflows/progress.py +0 -785
  406. empathy_os/workflows/progress_server.py +0 -322
  407. empathy_os/workflows/progressive/README 2.md +0 -454
  408. empathy_os/workflows/progressive/README.md +0 -454
  409. empathy_os/workflows/progressive/__init__.py +0 -82
  410. empathy_os/workflows/progressive/cli.py +0 -219
  411. empathy_os/workflows/progressive/core.py +0 -488
  412. empathy_os/workflows/progressive/orchestrator.py +0 -723
  413. empathy_os/workflows/progressive/reports.py +0 -520
  414. empathy_os/workflows/progressive/telemetry.py +0 -274
  415. empathy_os/workflows/progressive/test_gen.py +0 -495
  416. empathy_os/workflows/progressive/workflow.py +0 -589
  417. empathy_os/workflows/refactor_plan.py +0 -694
  418. empathy_os/workflows/release_prep.py +0 -895
  419. empathy_os/workflows/release_prep_crew.py +0 -969
  420. empathy_os/workflows/research_synthesis.py +0 -404
  421. empathy_os/workflows/routing.py +0 -168
  422. empathy_os/workflows/secure_release.py +0 -593
  423. empathy_os/workflows/security_adapters.py +0 -297
  424. empathy_os/workflows/security_audit.py +0 -1329
  425. empathy_os/workflows/security_audit_phase3.py +0 -355
  426. empathy_os/workflows/seo_optimization.py +0 -633
  427. empathy_os/workflows/step_config.py +0 -234
  428. empathy_os/workflows/telemetry_mixin.py +0 -269
  429. empathy_os/workflows/test5.py +0 -125
  430. empathy_os/workflows/test5_README.md +0 -158
  431. empathy_os/workflows/test_coverage_boost_crew.py +0 -849
  432. empathy_os/workflows/test_gen/__init__.py +0 -52
  433. empathy_os/workflows/test_gen/ast_analyzer.py +0 -249
  434. empathy_os/workflows/test_gen/config.py +0 -88
  435. empathy_os/workflows/test_gen/data_models.py +0 -38
  436. empathy_os/workflows/test_gen/report_formatter.py +0 -289
  437. empathy_os/workflows/test_gen/test_templates.py +0 -381
  438. empathy_os/workflows/test_gen/workflow.py +0 -655
  439. empathy_os/workflows/test_gen.py +0 -54
  440. empathy_os/workflows/test_gen_behavioral.py +0 -477
  441. empathy_os/workflows/test_gen_parallel.py +0 -341
  442. empathy_os/workflows/test_lifecycle.py +0 -526
  443. empathy_os/workflows/test_maintenance.py +0 -627
  444. empathy_os/workflows/test_maintenance_cli.py +0 -590
  445. empathy_os/workflows/test_maintenance_crew.py +0 -840
  446. empathy_os/workflows/test_runner.py +0 -622
  447. empathy_os/workflows/tier_tracking.py +0 -531
  448. empathy_os/workflows/xml_enhanced_crew.py +0 -285
  449. empathy_software_plugin/SOFTWARE_PLUGIN_README.md +0 -57
  450. empathy_software_plugin/cli/__init__.py +0 -120
  451. empathy_software_plugin/cli/inspect.py +0 -362
  452. empathy_software_plugin/cli.py +0 -574
  453. empathy_software_plugin/plugin.py +0 -188
  454. workflow_scaffolding/__init__.py +0 -11
  455. workflow_scaffolding/__main__.py +0 -12
  456. workflow_scaffolding/cli.py +0 -206
  457. workflow_scaffolding/generator.py +0 -265
  458. {empathy_framework-5.3.0.dist-info → empathy_framework-5.4.0.dist-info}/WHEEL +0 -0
@@ -1,1426 +0,0 @@
1
- """Document Generation Workflow.
2
-
3
- Main workflow orchestration for documentation generation.
4
-
5
- Copyright 2025 Smart-AI-Memory
6
- Licensed under Fair Source License 0.9
7
- """
8
-
9
- import logging
10
- from datetime import datetime
11
- from pathlib import Path
12
- from typing import Any
13
-
14
- from empathy_os.config import _validate_file_path
15
-
16
- from ..base import BaseWorkflow, ModelTier
17
- from .config import DOC_GEN_STEPS, TOKEN_COSTS
18
- from .report_formatter import format_doc_gen_report
19
-
20
- logger = logging.getLogger(__name__)
21
-
22
-
23
- class DocumentGenerationWorkflow(BaseWorkflow):
24
- """Multi-tier document generation workflow.
25
-
26
- Uses cheap models for outlining, capable models for content
27
- generation, and premium models for final polish and consistency
28
- review.
29
-
30
- Usage:
31
- workflow = DocumentGenerationWorkflow()
32
- result = await workflow.execute(
33
- source_code="...",
34
- doc_type="api_reference",
35
- audience="developers"
36
- )
37
- """
38
-
39
- name = "doc-gen"
40
- description = "Cost-optimized documentation generation pipeline"
41
- stages = ["outline", "write", "polish"]
42
- tier_map = {
43
- "outline": ModelTier.CHEAP,
44
- "write": ModelTier.CAPABLE,
45
- "polish": ModelTier.PREMIUM,
46
- }
47
-
48
- def __init__(
49
- self,
50
- skip_polish_threshold: int = 1000,
51
- max_sections: int = 10,
52
- max_write_tokens: int | None = None, # Auto-scaled if None
53
- section_focus: list[str] | None = None,
54
- chunked_generation: bool = True,
55
- sections_per_chunk: int = 3,
56
- max_cost: float = 5.0, # Cost guardrail in USD
57
- cost_warning_threshold: float = 0.8, # Warn at 80% of max_cost
58
- graceful_degradation: bool = True, # Return partial results on error
59
- export_path: str | Path | None = None, # Export docs to file (e.g., "docs/generated")
60
- max_display_chars: int = 45000, # Max chars before chunking output
61
- enable_auth_strategy: bool = True, # Enable intelligent auth routing
62
- **kwargs: Any,
63
- ):
64
- """Initialize workflow with enterprise-safe defaults.
65
-
66
- Args:
67
- skip_polish_threshold: Skip premium polish for docs under this
68
- token count (they're already good enough).
69
- max_sections: Maximum number of sections to generate.
70
- max_write_tokens: Maximum tokens for content generation.
71
- If None, auto-scales based on section count (recommended).
72
- section_focus: Optional list of specific sections to generate
73
- (e.g., ["Testing Guide", "API Reference"]).
74
- chunked_generation: If True, generates large docs in chunks to avoid
75
- truncation (default True).
76
- sections_per_chunk: Number of sections to generate per chunk (default 3).
77
- max_cost: Maximum cost in USD before stopping (default $5).
78
- Set to 0 to disable cost limits.
79
- cost_warning_threshold: Percentage of max_cost to trigger warning (default 0.8).
80
- graceful_degradation: If True, return partial results on errors
81
- instead of failing completely (default True).
82
- export_path: Optional directory to export generated docs (e.g., "docs/generated").
83
- If provided, documentation will be saved to a file automatically.
84
- max_display_chars: Maximum characters before splitting output into chunks
85
- for display (default 45000). Helps avoid terminal/UI truncation.
86
- enable_auth_strategy: If True, use intelligent subscription vs API routing
87
- based on module size (default True).
88
-
89
- """
90
- super().__init__(**kwargs)
91
- self.skip_polish_threshold = skip_polish_threshold
92
- self.max_sections = max_sections
93
- self._user_max_write_tokens = max_write_tokens # Store user preference
94
- self.max_write_tokens = max_write_tokens or 16000 # Will be auto-scaled
95
- self.section_focus = section_focus
96
- self.chunked_generation = chunked_generation
97
- self.sections_per_chunk = sections_per_chunk
98
- self.max_cost = max_cost
99
- self.cost_warning_threshold = cost_warning_threshold
100
- self.graceful_degradation = graceful_degradation
101
- self.export_path = Path(export_path) if export_path else None
102
- self.max_display_chars = max_display_chars
103
- self.enable_auth_strategy = enable_auth_strategy
104
- self._total_content_tokens: int = 0
105
- self._accumulated_cost: float = 0.0
106
- self._cost_warning_issued: bool = False
107
- self._partial_results: dict = {}
108
- self._auth_mode_used: str | None = None # Track which auth was recommended
109
-
110
- def _estimate_cost(self, tier: ModelTier, input_tokens: int, output_tokens: int) -> float:
111
- """Estimate cost for a given tier and token counts."""
112
- costs = TOKEN_COSTS.get(tier, TOKEN_COSTS[ModelTier.CAPABLE])
113
- input_cost = (input_tokens / 1000) * costs["input"]
114
- output_cost = (output_tokens / 1000) * costs["output"]
115
- return input_cost + output_cost
116
-
117
- def _track_cost(
118
- self,
119
- tier: ModelTier,
120
- input_tokens: int,
121
- output_tokens: int,
122
- ) -> tuple[float, bool]:
123
- """Track accumulated cost and check against limits.
124
-
125
- Returns:
126
- Tuple of (cost_for_this_call, should_stop)
127
-
128
- """
129
- cost = self._estimate_cost(tier, input_tokens, output_tokens)
130
- self._accumulated_cost += cost
131
-
132
- # Check warning threshold
133
- if (
134
- self.max_cost > 0
135
- and not self._cost_warning_issued
136
- and self._accumulated_cost >= self.max_cost * self.cost_warning_threshold
137
- ):
138
- self._cost_warning_issued = True
139
- logger.warning(
140
- f"Doc-gen cost approaching limit: ${self._accumulated_cost:.2f} "
141
- f"of ${self.max_cost:.2f} ({self.cost_warning_threshold * 100:.0f}% threshold)",
142
- )
143
-
144
- # Check if we should stop
145
- should_stop = self.max_cost > 0 and self._accumulated_cost >= self.max_cost
146
- if should_stop:
147
- logger.warning(
148
- f"Doc-gen cost limit reached: ${self._accumulated_cost:.2f} >= ${self.max_cost:.2f}",
149
- )
150
-
151
- return cost, should_stop
152
-
153
- def _auto_scale_tokens(self, section_count: int) -> int:
154
- """Auto-scale max_write_tokens based on section count.
155
-
156
- Enterprise projects may have 20+ sections requiring more tokens.
157
- """
158
- if self._user_max_write_tokens is not None:
159
- return self._user_max_write_tokens # User override
160
-
161
- # Base: 2000 tokens per section, minimum 16000, maximum 64000
162
- scaled = max(16000, min(64000, section_count * 2000))
163
- logger.info(f"Auto-scaled max_write_tokens to {scaled} for {section_count} sections")
164
- return scaled
165
-
166
- def _export_document(
167
- self,
168
- document: str,
169
- doc_type: str,
170
- report: str | None = None,
171
- ) -> tuple[Path | None, Path | None]:
172
- """Export generated documentation to file.
173
-
174
- Args:
175
- document: The generated documentation content
176
- doc_type: Document type for naming
177
- report: Optional report to save alongside document
178
-
179
- Returns:
180
- Tuple of (doc_path, report_path) or (None, None) if export disabled
181
-
182
- """
183
- if not self.export_path:
184
- return None, None
185
-
186
- # Create export directory
187
- self.export_path.mkdir(parents=True, exist_ok=True)
188
-
189
- # Generate filename with timestamp
190
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
191
- safe_doc_type = doc_type.replace(" ", "_").replace("/", "-").lower()
192
- doc_filename = f"{safe_doc_type}_{timestamp}.md"
193
- report_filename = f"{safe_doc_type}_{timestamp}_report.txt"
194
-
195
- doc_path = self.export_path / doc_filename
196
- report_path = self.export_path / report_filename if report else None
197
-
198
- # Write document
199
- try:
200
- validated_doc_path = _validate_file_path(str(doc_path))
201
- validated_doc_path.write_text(document, encoding="utf-8")
202
- logger.info(f"Documentation exported to: {validated_doc_path}")
203
-
204
- # Write report if provided
205
- if report and report_path:
206
- validated_report_path = _validate_file_path(str(report_path))
207
- validated_report_path.write_text(report, encoding="utf-8")
208
- logger.info(f"Report exported to: {validated_report_path}")
209
-
210
- return validated_doc_path, validated_report_path if report else None
211
- except (OSError, ValueError) as e:
212
- logger.error(f"Failed to export documentation: {e}")
213
- return None, None
214
-
215
- def _chunk_output_for_display(self, content: str, chunk_prefix: str = "PART") -> list[str]:
216
- """Split large output into displayable chunks.
217
-
218
- Args:
219
- content: The content to chunk
220
- chunk_prefix: Prefix for chunk headers
221
-
222
- Returns:
223
- List of content chunks, each under max_display_chars
224
-
225
- """
226
- if len(content) <= self.max_display_chars:
227
- return [content]
228
-
229
- chunks = []
230
- # Try to split on section boundaries (## headers)
231
- import re
232
-
233
- sections = re.split(r"(?=^## )", content, flags=re.MULTILINE)
234
-
235
- current_chunk = ""
236
- chunk_num = 1
237
-
238
- for section in sections:
239
- # If adding this section would exceed limit, save current chunk
240
- if current_chunk and len(current_chunk) + len(section) > self.max_display_chars:
241
- chunks.append(
242
- f"{'=' * 60}\n{chunk_prefix} {chunk_num} of {{total}}\n{'=' * 60}\n\n"
243
- + current_chunk,
244
- )
245
- chunk_num += 1
246
- current_chunk = section
247
- else:
248
- current_chunk += section
249
-
250
- # Add final chunk
251
- if current_chunk:
252
- chunks.append(
253
- f"{'=' * 60}\n{chunk_prefix} {chunk_num} of {{total}}\n{'=' * 60}\n\n"
254
- + current_chunk,
255
- )
256
-
257
- # Update total count in all chunks
258
- total = len(chunks)
259
- chunks = [chunk.format(total=total) for chunk in chunks]
260
-
261
- return chunks
262
-
263
- def should_skip_stage(self, stage_name: str, input_data: Any) -> tuple[bool, str | None]:
264
- """Skip polish for short documents."""
265
- if stage_name == "polish":
266
- if self._total_content_tokens < self.skip_polish_threshold:
267
- self.tier_map["polish"] = ModelTier.CAPABLE
268
- return False, None
269
- return False, None
270
-
271
- async def run_stage(
272
- self,
273
- stage_name: str,
274
- tier: ModelTier,
275
- input_data: Any,
276
- ) -> tuple[Any, int, int]:
277
- """Execute a document generation stage."""
278
- if stage_name == "outline":
279
- return await self._outline(input_data, tier)
280
- if stage_name == "write":
281
- return await self._write(input_data, tier)
282
- if stage_name == "polish":
283
- return await self._polish(input_data, tier)
284
- raise ValueError(f"Unknown stage: {stage_name}")
285
-
286
- async def _outline(self, input_data: dict, tier: ModelTier) -> tuple[dict, int, int]:
287
- """Generate document outline from source."""
288
- from pathlib import Path
289
-
290
- source_code = input_data.get("source_code", "")
291
- target = input_data.get("target", "")
292
- doc_type = input_data.get("doc_type", "general")
293
- audience = input_data.get("audience", "developers")
294
-
295
- # Use target if source_code not provided
296
- content_to_document = source_code or target
297
-
298
- # If target looks like a file path and source_code wasn't provided, read the file
299
- if not source_code and target:
300
- target_path = Path(target)
301
- if target_path.exists() and target_path.is_file():
302
- try:
303
- content_to_document = target_path.read_text(encoding="utf-8")
304
- # Prepend file info for context
305
- content_to_document = f"# File: {target}\n\n{content_to_document}"
306
- except Exception as e:
307
- # If we can't read the file, log and use the path as-is
308
- import logging
309
-
310
- logging.getLogger(__name__).warning(f"Could not read file {target}: {e}")
311
- elif target_path.suffix in (
312
- ".py",
313
- ".js",
314
- ".ts",
315
- ".tsx",
316
- ".java",
317
- ".go",
318
- ".rs",
319
- ".md",
320
- ".txt",
321
- ):
322
- # Looks like a file path but doesn't exist - warn
323
- import logging
324
-
325
- logging.getLogger(__name__).warning(
326
- f"Target appears to be a file path but doesn't exist: {target}",
327
- )
328
-
329
- # === AUTH STRATEGY INTEGRATION ===
330
- # Detect module size and recommend auth mode (first stage only)
331
- if self.enable_auth_strategy:
332
- try:
333
- from empathy_os.models import (
334
- count_lines_of_code,
335
- get_auth_strategy,
336
- get_module_size_category,
337
- )
338
-
339
- # Calculate module size
340
- module_lines = 0
341
- if target and Path(target).exists():
342
- module_lines = count_lines_of_code(target)
343
- elif content_to_document:
344
- # Count from source code content
345
- module_lines = len(
346
- [
347
- line
348
- for line in content_to_document.split("\n")
349
- if line.strip() and not line.strip().startswith("#")
350
- ]
351
- )
352
-
353
- if module_lines > 0:
354
- # Get auth strategy (first-time setup if needed)
355
- strategy = get_auth_strategy()
356
-
357
- # Get recommended auth mode
358
- recommended_mode = strategy.get_recommended_mode(module_lines)
359
- self._auth_mode_used = recommended_mode.value
360
-
361
- # Get size category
362
- size_category = get_module_size_category(module_lines)
363
-
364
- # Log recommendation
365
- logger.info(
366
- f"Module: {target or 'source'} ({module_lines} LOC, {size_category})"
367
- )
368
- logger.info(f"Recommended auth mode: {recommended_mode.value}")
369
-
370
- # Get cost estimate
371
- cost_estimate = strategy.estimate_cost(module_lines, recommended_mode)
372
-
373
- if recommended_mode.value == "subscription":
374
- logger.info(
375
- f"Cost: {cost_estimate['quota_cost']} "
376
- f"(fits in {cost_estimate['fits_in_context']} context)"
377
- )
378
- else: # API
379
- logger.info(
380
- f"Cost: ~${cost_estimate['monetary_cost']:.4f} "
381
- f"(1M context window)"
382
- )
383
-
384
- except Exception as e:
385
- # Don't fail workflow if auth strategy fails
386
- logger.warning(f"Auth strategy detection failed: {e}")
387
-
388
- system = """You are an expert technical writer specializing in API Reference documentation.
389
-
390
- IMPORTANT: This is API REFERENCE documentation, not a tutorial. Focus on documenting EVERY function/class with structured Args/Returns/Raises format.
391
-
392
- Create a detailed, structured outline for API Reference documentation:
393
-
394
- 1. **Logical Section Structure** (emphasize API reference sections):
395
- - Overview/Introduction (brief)
396
- - Quick Start (1 complete example)
397
- - API Reference - Functions (one subsection per function with Args/Returns/Raises)
398
- - API Reference - Classes (one subsection per class with Args/Returns/Raises for methods)
399
- - Usage Examples (showing how to combine multiple functions)
400
- - Additional reference sections as needed
401
-
402
- 2. **For Each Section**:
403
- - Clear purpose and what readers will learn
404
- - Specific topics to cover
405
- - Types of examples to include (with actual code)
406
-
407
- 3. **Key Requirements**:
408
- - Include sections for real, copy-paste ready code examples
409
- - Plan for comprehensive API documentation with all parameters
410
- - Include edge cases and error handling examples
411
- - Add best practices and common patterns
412
-
413
- Format as a numbered list with section titles and detailed descriptions."""
414
-
415
- user_message = f"""Create a comprehensive documentation outline:
416
-
417
- Document Type: {doc_type}
418
- Target Audience: {audience}
419
-
420
- IMPORTANT: This documentation should be production-ready with:
421
- - Real, executable code examples (not placeholders)
422
- - Complete API reference with parameter types and descriptions
423
- - Usage guides showing common patterns
424
- - Edge case handling and error scenarios
425
- - Best practices for the target audience
426
-
427
- Content to document:
428
- {content_to_document[:4000]}
429
-
430
- Generate an outline that covers all these aspects comprehensively."""
431
-
432
- response, input_tokens, output_tokens = await self._call_llm(
433
- tier,
434
- system,
435
- user_message,
436
- max_tokens=1000,
437
- )
438
-
439
- return (
440
- {
441
- "outline": response,
442
- "doc_type": doc_type,
443
- "audience": audience,
444
- "content_to_document": content_to_document,
445
- },
446
- input_tokens,
447
- output_tokens,
448
- )
449
-
450
- def _parse_outline_sections(self, outline: str) -> list[str]:
451
- """Parse top-level section titles from the outline.
452
-
453
- Only matches main sections like "1. Introduction", "2. Setup", etc.
454
- Ignores sub-sections like "2.1 Prerequisites" or nested items.
455
- """
456
- import re
457
-
458
- sections = []
459
- # Match only top-level sections: digit followed by period and space/letter
460
- # e.g., "1. Introduction" but NOT "1.1 Sub-section" or "2.1.3 Deep"
461
- top_level_pattern = re.compile(r"^(\d+)\.\s+([A-Za-z].*)")
462
-
463
- for line in outline.split("\n"):
464
- stripped = line.strip()
465
- match = top_level_pattern.match(stripped)
466
- if match:
467
- # section_num = match.group(1) - not needed, only extracting title
468
- title = match.group(2).strip()
469
- # Remove any trailing description after " - "
470
- if " - " in title:
471
- title = title.split(" - ")[0].strip()
472
- sections.append(title)
473
-
474
- return sections
475
-
476
- async def _write(self, input_data: dict, tier: ModelTier) -> tuple[dict, int, int]:
477
- """Write content based on the outline."""
478
- outline = input_data.get("outline", "")
479
- doc_type = input_data.get("doc_type", "general")
480
- audience = input_data.get("audience", "developers")
481
- content_to_document = input_data.get("content_to_document", "")
482
-
483
- # Parse sections from outline
484
- sections = self._parse_outline_sections(outline)
485
-
486
- # Auto-scale tokens based on section count
487
- self.max_write_tokens = self._auto_scale_tokens(len(sections))
488
-
489
- # Use chunked generation for large outlines (more than sections_per_chunk * 2)
490
- use_chunking = (
491
- self.chunked_generation
492
- and len(sections) > self.sections_per_chunk * 2
493
- and not self.section_focus # Don't chunk if already focused
494
- )
495
-
496
- if use_chunking:
497
- return await self._write_chunked(
498
- sections,
499
- outline,
500
- doc_type,
501
- audience,
502
- content_to_document,
503
- tier,
504
- )
505
-
506
- # Handle section_focus for targeted generation
507
- section_instruction = ""
508
- if self.section_focus:
509
- sections_list = ", ".join(self.section_focus)
510
- section_instruction = f"""
511
- IMPORTANT: Focus ONLY on generating these specific sections:
512
- {sections_list}
513
-
514
- Generate comprehensive, detailed content for each of these sections."""
515
-
516
- system = f"""You are an expert technical writer creating comprehensive developer documentation.
517
-
518
- YOUR TASK HAS TWO CRITICAL PHASES - YOU MUST COMPLETE BOTH:
519
-
520
- ═══════════════════════════════════════════════════════════════
521
- PHASE 1: Write Comprehensive Documentation
522
- ═══════════════════════════════════════════════════════════════
523
-
524
- Write clear, helpful documentation with:
525
- - Overview and introduction explaining what this code does
526
- - Real, executable code examples (NOT placeholders - use actual code from source)
527
- - Usage guides showing how to use the code in real scenarios
528
- - Best practices and common patterns
529
- - Step-by-step instructions where helpful
530
- - Tables, diagrams, and visual aids as appropriate
531
- - Clear explanations appropriate for {audience}
532
-
533
- Do this naturally - write the kind of documentation that helps developers understand and use the code effectively.
534
-
535
- ═══════════════════════════════════════════════════════════════
536
- PHASE 2: Add Structured API Reference Sections (MANDATORY)
537
- ═══════════════════════════════════════════════════════════════
538
-
539
- After writing the comprehensive documentation above, you MUST add structured API reference sections for EVERY function and class method.
540
-
541
- For EACH function/method in the source code, add this EXACT structure:
542
-
543
- ---
544
- ### `function_name()`
545
-
546
- **Function Signature:**
547
- ```python
548
- def function_name(param1: type, param2: type = default) -> return_type
549
- ```
550
-
551
- **Description:**
552
- [Brief description of what the function does - 1-2 sentences]
553
-
554
- **Args:**
555
- - `param1` (`type`): Clear description of this parameter
556
- - `param2` (`type`, optional): Description. Defaults to `default`.
557
-
558
- **Returns:**
559
- - `return_type`: Description of the return value
560
-
561
- **Raises:**
562
- - `ExceptionType`: Description of when and why this exception occurs
563
- - `AnotherException`: Another exception case
564
-
565
- **Example:**
566
- ```python
567
- from module import function_name
568
-
569
- # Show real usage with actual code
570
- result = function_name(actual_value, param2=123)
571
- print(result)
572
- ```
573
- ---
574
-
575
- CRITICAL RULES FOR PHASE 2:
576
- - Include **Args:** header for ALL functions (write "None" if no parameters)
577
- - Include **Returns:** header for ALL functions (write "None" if void/no return)
578
- - Include **Raises:** header for ALL functions (write "None" if no exceptions)
579
- - Use backticks for code: `param_name` (`type`)
580
- - Document EVERY public function and method you see in the source code
581
-
582
- {section_instruction}
583
-
584
- ═══════════════════════════════════════════════════════════════
585
- REMINDER: BOTH PHASES ARE MANDATORY
586
- ═══════════════════════════════════════════════════════════════
587
-
588
- 1. Write comprehensive documentation (Phase 1) - what you do naturally
589
- 2. Add structured API reference sections (Phase 2) - for every function/method
590
-
591
- Do NOT skip Phase 2 after completing Phase 1. Both phases are required for complete documentation."""
592
-
593
- user_message = f"""Write comprehensive, production-ready documentation in TWO PHASES:
594
-
595
- Document Type: {doc_type}
596
- Target Audience: {audience}
597
-
598
- Outline to follow:
599
- {outline}
600
-
601
- Source code to document (extract actual class names, function signatures, parameters):
602
- {content_to_document[:5000]}
603
-
604
- ═══════════════════════════════════════════════════════════════
605
- YOUR TASK:
606
- ═══════════════════════════════════════════════════════════════
607
-
608
- PHASE 1: Write comprehensive documentation
609
- - Use the outline above as your guide
610
- - Include real, executable code examples from the source
611
- - Show usage patterns, best practices, common workflows
612
- - Write clear explanations that help developers understand the code
613
-
614
- PHASE 2: Add structured API reference sections
615
- - For EACH function/method in the source code, add:
616
- - Function signature
617
- - Description
618
- - **Args:** section (every parameter with type and description)
619
- - **Returns:** section (return type and description)
620
- - **Raises:** section (exceptions that can occur)
621
- - Example code snippet
622
-
623
- ═══════════════════════════════════════════════════════════════
624
- IMPORTANT: Complete BOTH phases. Don't stop after Phase 1.
625
- ═══════════════════════════════════════════════════════════════
626
-
627
- Generate the complete documentation now, ensuring both comprehensive content AND structured API reference sections."""
628
-
629
- response, input_tokens, output_tokens = await self._call_llm(
630
- tier,
631
- system,
632
- user_message,
633
- max_tokens=self.max_write_tokens,
634
- )
635
-
636
- self._total_content_tokens = output_tokens
637
-
638
- return (
639
- {
640
- "draft_document": response,
641
- "doc_type": doc_type,
642
- "audience": audience,
643
- "outline": outline,
644
- "chunked": False,
645
- "source_code": content_to_document, # Pass through for API reference generation
646
- },
647
- input_tokens,
648
- output_tokens,
649
- )
650
-
651
- async def _write_chunked(
652
- self,
653
- sections: list[str],
654
- outline: str,
655
- doc_type: str,
656
- audience: str,
657
- content_to_document: str,
658
- tier: ModelTier,
659
- ) -> tuple[dict, int, int]:
660
- """Generate documentation in chunks to avoid truncation.
661
-
662
- Enterprise-safe: includes cost tracking and graceful degradation.
663
- """
664
- all_content: list[str] = []
665
- total_input_tokens: int = 0
666
- total_output_tokens: int = 0
667
- stopped_early: bool = False
668
- error_message: str | None = None
669
-
670
- # Split sections into chunks
671
- chunks = []
672
- for i in range(0, len(sections), self.sections_per_chunk):
673
- chunks.append(sections[i : i + self.sections_per_chunk])
674
-
675
- logger.info(f"Generating documentation in {len(chunks)} chunks")
676
-
677
- for chunk_idx, chunk_sections in enumerate(chunks):
678
- sections_list = ", ".join(chunk_sections)
679
-
680
- # Build context about what came before
681
- previous_context = ""
682
- if chunk_idx > 0 and all_content:
683
- # Include last 500 chars of previous content for continuity
684
- previous_context = f"""
685
- Previous sections already written (for context/continuity):
686
- ...{all_content[-1][-500:]}
687
-
688
- Continue with the next sections, maintaining consistent style and terminology."""
689
-
690
- system = f"""You are an expert technical writer creating comprehensive developer documentation.
691
-
692
- Write ONLY these sections (part {chunk_idx + 1} of {len(chunks)}): {sections_list}
693
-
694
- YOUR TASK FOR THESE SECTIONS (TWO PHASES):
695
-
696
- ═══════════════════════════════════════════════════════════════
697
- PHASE 1: Comprehensive Content
698
- ═══════════════════════════════════════════════════════════════
699
- - Write clear explanations and overviews
700
- - Include real, executable code examples (extract from source)
701
- - Show usage patterns and workflows
702
- - Add best practices and common patterns
703
- - Professional language for {audience}
704
-
705
- ═══════════════════════════════════════════════════════════════
706
- PHASE 2: Structured API Reference
707
- ═══════════════════════════════════════════════════════════════
708
- For EACH function/method in these sections, add:
709
-
710
- ### `function_name()`
711
-
712
- **Function Signature:**
713
- ```python
714
- def function_name(params) -> return_type
715
- ```
716
-
717
- **Description:**
718
- [Brief description]
719
-
720
- **Args:**
721
- - `param` (`type`): Description
722
-
723
- **Returns:**
724
- - `type`: Description
725
-
726
- **Raises:**
727
- - `Exception`: When it occurs
728
-
729
- **Example:**
730
- ```python
731
- # Real usage example
732
- ```
733
-
734
- ═══════════════════════════════════════════════════════════════
735
- Complete BOTH phases for these sections.
736
- ═══════════════════════════════════════════════════════════════"""
737
-
738
- user_message = f"""Write comprehensive documentation for these sections in TWO PHASES:
739
-
740
- Sections to write: {sections_list}
741
-
742
- Document Type: {doc_type}
743
- Target Audience: {audience}
744
-
745
- Source code (extract actual functions/classes from here):
746
- {content_to_document[:3000]}
747
-
748
- Full outline (for context):
749
- {outline}
750
- {previous_context}
751
-
752
- PHASE 1: Write comprehensive content with real code examples
753
- PHASE 2: Add structured API reference sections with **Args:**, **Returns:**, **Raises:**
754
-
755
- Generate complete sections now, ensuring both phases are complete."""
756
-
757
- try:
758
- response, input_tokens, output_tokens = await self._call_llm(
759
- tier,
760
- system,
761
- user_message,
762
- max_tokens=self.max_write_tokens // len(chunks) + 2000,
763
- )
764
-
765
- # Track cost and check limits
766
- _, should_stop = self._track_cost(tier, input_tokens, output_tokens)
767
-
768
- all_content.append(response)
769
- total_input_tokens += input_tokens
770
- total_output_tokens += output_tokens
771
-
772
- logger.info(
773
- f"Chunk {chunk_idx + 1}/{len(chunks)} complete: "
774
- f"{len(response)} chars, {output_tokens} tokens, "
775
- f"cost so far: ${self._accumulated_cost:.2f}",
776
- )
777
-
778
- # Check cost limit
779
- if should_stop:
780
- stopped_early = True
781
- remaining = len(chunks) - chunk_idx - 1
782
- error_message = (
783
- f"Cost limit reached (${self._accumulated_cost:.2f}). "
784
- f"Stopped after {chunk_idx + 1}/{len(chunks)} chunks. "
785
- f"{remaining} chunks not generated."
786
- )
787
- logger.warning(error_message)
788
- break
789
-
790
- except Exception as e:
791
- error_message = f"Error generating chunk {chunk_idx + 1}: {e}"
792
- logger.error(error_message)
793
- if not self.graceful_degradation:
794
- raise
795
- stopped_early = True
796
- break
797
-
798
- # Combine all chunks
799
- combined_document = "\n\n".join(all_content)
800
- self._total_content_tokens = total_output_tokens
801
-
802
- # Store partial results for graceful degradation
803
- self._partial_results = {
804
- "draft_document": combined_document,
805
- "sections_completed": len(all_content),
806
- "sections_total": len(chunks),
807
- }
808
-
809
- result = {
810
- "draft_document": combined_document,
811
- "doc_type": doc_type,
812
- "audience": audience,
813
- "outline": outline,
814
- "chunked": True,
815
- "chunk_count": len(chunks),
816
- "chunks_completed": len(all_content),
817
- "stopped_early": stopped_early,
818
- "accumulated_cost": self._accumulated_cost,
819
- "source_code": content_to_document, # Pass through for API reference generation
820
- }
821
-
822
- if error_message:
823
- result["warning"] = error_message
824
-
825
- return (result, total_input_tokens, total_output_tokens)
826
-
827
- async def _polish(self, input_data: dict, tier: ModelTier) -> tuple[dict, int, int]:
828
- """Final review and consistency polish using LLM.
829
-
830
- Enterprise-safe: chunks large documents to avoid truncation.
831
- Supports XML-enhanced prompts when enabled in workflow config.
832
- """
833
- draft_document = input_data.get("draft_document", "")
834
- doc_type = input_data.get("doc_type", "general")
835
- audience = input_data.get("audience", "developers")
836
-
837
- # Check if document is too large and needs chunked polishing
838
- # Rough estimate: 4 chars per token, 10k tokens threshold for chunking
839
- estimated_tokens = len(draft_document) // 4
840
- needs_chunked_polish = estimated_tokens > 10000
841
-
842
- if needs_chunked_polish:
843
- logger.info(
844
- f"Large document detected (~{estimated_tokens} tokens). "
845
- "Using chunked polish for enterprise safety.",
846
- )
847
- return await self._polish_chunked(input_data, tier)
848
-
849
- # Build input payload for prompt
850
- input_payload = f"""Document Type: {doc_type}
851
- Target Audience: {audience}
852
-
853
- Draft:
854
- {draft_document}"""
855
-
856
- # Check if XML prompts are enabled
857
- if self._is_xml_enabled():
858
- # Use XML-enhanced prompt
859
- user_message = self._render_xml_prompt(
860
- role="senior technical editor",
861
- goal="Polish and improve the documentation for consistency and quality",
862
- instructions=[
863
- "Standardize terminology and formatting",
864
- "Improve clarity and flow",
865
- "Add missing cross-references",
866
- "Fix grammatical issues",
867
- "Identify gaps and add helpful notes",
868
- "Ensure examples are complete and accurate",
869
- ],
870
- constraints=[
871
- "Maintain the original structure and intent",
872
- "Keep content appropriate for the target audience",
873
- "Preserve code examples while improving explanations",
874
- ],
875
- input_type="documentation_draft",
876
- input_payload=input_payload,
877
- extra={
878
- "doc_type": doc_type,
879
- "audience": audience,
880
- },
881
- )
882
- system = None # XML prompt includes all context
883
- else:
884
- # Use legacy plain text prompts
885
- system = """You are a senior technical editor specializing in developer documentation.
886
-
887
- Polish and improve this documentation. The writer was asked to complete TWO PHASES:
888
- - Phase 1: Comprehensive content with real examples
889
- - Phase 2: Structured API reference sections with **Args:**, **Returns:**, **Raises:**
890
-
891
- Your job is to verify BOTH phases are complete and polish to production quality.
892
-
893
- ═══════════════════════════════════════════════════════════════
894
- CRITICAL: Verify Phase 2 Completion
895
- ═══════════════════════════════════════════════════════════════
896
-
897
- 1. **Check for Missing API Reference Sections**:
898
- - Scan the entire document for all functions and methods
899
- - EVERY function MUST have these sections:
900
- - **Args:** (write "None" if no parameters)
901
- - **Returns:** (write "None" if void)
902
- - **Raises:** (write "None" if no exceptions)
903
- - If ANY function is missing these sections, ADD them now
904
- - Format: **Args:**, **Returns:**, **Raises:** (bold headers with colons)
905
-
906
- 2. **Polish API Reference Sections**:
907
- - Verify all parameters have types in backticks: `param` (`type`)
908
- - Ensure return values are clearly described
909
- - Check exception documentation is complete
910
- - Validate code examples in each function section
911
-
912
- 3. **Polish General Content**:
913
- - Verify code examples are complete and runnable
914
- - Ensure proper imports and setup code
915
- - Replace any placeholders with real code
916
- - Standardize terminology throughout
917
- - Fix formatting inconsistencies
918
- - Improve clarity and flow
919
- - Add cross-references between sections
920
-
921
- 4. **Production Readiness**:
922
- - Remove any TODO or placeholder comments
923
- - Ensure professional tone
924
- - Add helpful notes, tips, and warnings
925
- - Verify edge cases are covered
926
-
927
- ═══════════════════════════════════════════════════════════════
928
- Return the complete, polished document. Add a brief "## Polish Notes" section at the end summarizing improvements made."""
929
-
930
- user_message = f"""Polish this documentation to production quality.
931
-
932
- The writer was asked to complete TWO PHASES:
933
- 1. Comprehensive content with real examples
934
- 2. Structured API reference with **Args:**, **Returns:**, **Raises:** for every function
935
-
936
- Verify BOTH phases are complete, then polish:
937
-
938
- {input_payload}
939
-
940
- ═══════════════════════════════════════════════════════════════
941
- YOUR TASKS:
942
- ═══════════════════════════════════════════════════════════════
943
-
944
- 1. SCAN for missing API reference sections
945
- - Find every function/method in the document
946
- - Check if it has **Args:**, **Returns:**, **Raises:** sections
947
- - ADD these sections if missing (use "None" if no parameters/returns/exceptions)
948
-
949
- 2. POLISH existing content
950
- - Verify code examples are complete and runnable
951
- - Ensure terminology is consistent
952
- - Fix formatting issues
953
- - Improve clarity and flow
954
-
955
- 3. VALIDATE production readiness
956
- - Remove TODOs and placeholders
957
- - Add warnings and best practices
958
- - Ensure professional tone
959
-
960
- Return the complete, polished documentation with all API reference sections present."""
961
-
962
- # Calculate polish tokens based on draft size (at least as much as write stage)
963
- polish_max_tokens = max(self.max_write_tokens, 20000)
964
-
965
- # Try executor-based execution first (Phase 3 pattern)
966
- if self._executor is not None or self._api_key:
967
- try:
968
- step = DOC_GEN_STEPS["polish"]
969
- # Override step max_tokens with dynamic value
970
- step.max_tokens = polish_max_tokens
971
- response, input_tokens, output_tokens, cost = await self.run_step_with_executor(
972
- step=step,
973
- prompt=user_message,
974
- system=system,
975
- )
976
- except Exception:
977
- # Fall back to legacy _call_llm if executor fails
978
- response, input_tokens, output_tokens = await self._call_llm(
979
- tier,
980
- system or "",
981
- user_message,
982
- max_tokens=polish_max_tokens,
983
- )
984
- else:
985
- # Legacy path for backward compatibility
986
- response, input_tokens, output_tokens = await self._call_llm(
987
- tier,
988
- system or "",
989
- user_message,
990
- max_tokens=polish_max_tokens,
991
- )
992
-
993
- # Parse XML response if enforcement is enabled
994
- parsed_data = self._parse_xml_response(response)
995
-
996
- # Add structured API reference sections (Step 4: Post-processing)
997
- source_code = input_data.get("source_code", "")
998
- if source_code:
999
- logger.info("Adding structured API reference sections to polished document...")
1000
- response = await self._add_api_reference_sections(
1001
- narrative_doc=response,
1002
- source_code=source_code,
1003
- tier=ModelTier.CHEAP, # Use cheap tier for structured extraction
1004
- )
1005
- else:
1006
- logger.warning("No source code available for API reference generation")
1007
-
1008
- result = {
1009
- "document": response,
1010
- "doc_type": doc_type,
1011
- "audience": audience,
1012
- "model_tier_used": tier.value,
1013
- "accumulated_cost": self._accumulated_cost, # Track total cost
1014
- "auth_mode_used": self._auth_mode_used, # Track recommended auth mode
1015
- }
1016
-
1017
- # Merge parsed XML data if available
1018
- if parsed_data.get("xml_parsed"):
1019
- result.update(
1020
- {
1021
- "xml_parsed": True,
1022
- "summary": parsed_data.get("summary"),
1023
- "findings": parsed_data.get("findings", []),
1024
- "checklist": parsed_data.get("checklist", []),
1025
- },
1026
- )
1027
-
1028
- # Add formatted report for human readability
1029
- result["formatted_report"] = format_doc_gen_report(result, input_data)
1030
-
1031
- # Export documentation if export_path is configured
1032
- doc_path, report_path = self._export_document(
1033
- document=response,
1034
- doc_type=doc_type,
1035
- report=result["formatted_report"],
1036
- )
1037
- if doc_path:
1038
- result["export_path"] = str(doc_path)
1039
- result["report_path"] = str(report_path) if report_path else None
1040
- logger.info(f"Documentation saved to: {doc_path}")
1041
-
1042
- # Chunk output for display if needed
1043
- output_chunks = self._chunk_output_for_display(
1044
- result["formatted_report"],
1045
- chunk_prefix="DOC OUTPUT",
1046
- )
1047
- if len(output_chunks) > 1:
1048
- result["output_chunks"] = output_chunks
1049
- result["output_chunk_count"] = len(output_chunks)
1050
- logger.info(
1051
- f"Report split into {len(output_chunks)} chunks for display "
1052
- f"(total {len(result['formatted_report'])} chars)",
1053
- )
1054
-
1055
- return (result, input_tokens, output_tokens)
1056
-
1057
- async def _polish_chunked(self, input_data: dict, tier: ModelTier) -> tuple[dict, int, int]:
1058
- """Polish large documents in chunks to avoid truncation.
1059
-
1060
- Splits the document by section headers and polishes each chunk separately,
1061
- then combines the results.
1062
- """
1063
- import re
1064
-
1065
- draft_document = input_data.get("draft_document", "")
1066
- doc_type = input_data.get("doc_type", "general")
1067
- audience = input_data.get("audience", "developers")
1068
-
1069
- # Split document by major section headers (## headers)
1070
- sections = re.split(r"(?=^## )", draft_document, flags=re.MULTILINE)
1071
- sections = [s.strip() for s in sections if s.strip()]
1072
-
1073
- if len(sections) <= 1:
1074
- # If we can't split by sections, split by character count
1075
- chunk_size = 15000 # ~3750 tokens per chunk
1076
- sections = [
1077
- draft_document[i : i + chunk_size]
1078
- for i in range(0, len(draft_document), chunk_size)
1079
- ]
1080
-
1081
- logger.info(f"Polishing document in {len(sections)} chunks")
1082
-
1083
- polished_chunks: list[str] = []
1084
- total_input_tokens: int = 0
1085
- total_output_tokens: int = 0
1086
-
1087
- for chunk_idx, section in enumerate(sections):
1088
- system = """You are a senior technical editor specializing in developer documentation.
1089
-
1090
- Polish this section to production quality. The writer was asked to complete TWO PHASES:
1091
- 1. Comprehensive content with real examples
1092
- 2. Structured API reference with **Args:**, **Returns:**, **Raises:** for every function
1093
-
1094
- Verify both phases are complete in this section:
1095
-
1096
- ═══════════════════════════════════════════════════════════════
1097
- CRITICAL: Check for Missing API Reference Format
1098
- ═══════════════════════════════════════════════════════════════
1099
-
1100
- 1. **Scan for functions/methods in this section**
1101
- - If any function is missing **Args:**, **Returns:**, **Raises:** sections, ADD them
1102
- - Format: **Args:**, **Returns:**, **Raises:** (bold headers with colons)
1103
- - Write "None" if no parameters/returns/exceptions
1104
-
1105
- 2. **Polish API Documentation**:
1106
- - Verify parameters documented with types in backticks
1107
- - Ensure return values and exceptions are clear
1108
- - Validate code examples are complete
1109
-
1110
- 3. **Polish General Content**:
1111
- - Ensure all examples are runnable with proper imports
1112
- - Standardize terminology and formatting
1113
- - Fix grammatical issues
1114
- - Remove TODOs and placeholders
1115
-
1116
- Return ONLY the polished section. Do not add commentary about changes."""
1117
-
1118
- user_message = f"""Polish this section to production quality (part {chunk_idx + 1} of {len(sections)}):
1119
-
1120
- Document Type: {doc_type}
1121
- Target Audience: {audience}
1122
-
1123
- Section to polish:
1124
- {section}
1125
-
1126
- Check if all functions have **Args:**, **Returns:**, **Raises:** sections - add if missing.
1127
- Make all code examples complete and executable."""
1128
-
1129
- try:
1130
- response, input_tokens, output_tokens = await self._call_llm(
1131
- tier,
1132
- system,
1133
- user_message,
1134
- max_tokens=8000,
1135
- )
1136
-
1137
- # Track cost
1138
- _, should_stop = self._track_cost(tier, input_tokens, output_tokens)
1139
-
1140
- polished_chunks.append(response)
1141
- total_input_tokens += input_tokens
1142
- total_output_tokens += output_tokens
1143
-
1144
- logger.info(
1145
- f"Polish chunk {chunk_idx + 1}/{len(sections)} complete, "
1146
- f"cost so far: ${self._accumulated_cost:.2f}",
1147
- )
1148
-
1149
- if should_stop:
1150
- logger.warning(
1151
- f"Cost limit reached during polish. "
1152
- f"Returning {len(polished_chunks)}/{len(sections)} polished chunks.",
1153
- )
1154
- # Add remaining sections unpolished
1155
- polished_chunks.extend(sections[chunk_idx + 1 :])
1156
- break
1157
-
1158
- except Exception as e:
1159
- logger.error(f"Error polishing chunk {chunk_idx + 1}: {e}")
1160
- if self.graceful_degradation:
1161
- # Keep original section on error
1162
- polished_chunks.append(section)
1163
- else:
1164
- raise
1165
-
1166
- # Combine polished chunks
1167
- polished_document = "\n\n".join(polished_chunks)
1168
-
1169
- # Add structured API reference sections (Step 4: Post-processing)
1170
- source_code = input_data.get("source_code", "")
1171
- if source_code:
1172
- logger.info("Adding structured API reference sections to chunked polished document...")
1173
- polished_document = await self._add_api_reference_sections(
1174
- narrative_doc=polished_document,
1175
- source_code=source_code,
1176
- tier=ModelTier.CHEAP, # Use cheap tier for structured extraction
1177
- )
1178
- else:
1179
- logger.warning("No source code available for API reference generation")
1180
-
1181
- result = {
1182
- "document": polished_document,
1183
- "doc_type": doc_type,
1184
- "audience": audience,
1185
- "model_tier_used": tier.value,
1186
- "polish_chunked": True,
1187
- "polish_chunks": len(sections),
1188
- "accumulated_cost": self._accumulated_cost,
1189
- }
1190
-
1191
- # Add formatted report
1192
- result["formatted_report"] = format_doc_gen_report(result, input_data)
1193
-
1194
- # Export documentation if export_path is configured
1195
- doc_path, report_path = self._export_document(
1196
- document=polished_document,
1197
- doc_type=doc_type,
1198
- report=result["formatted_report"],
1199
- )
1200
- if doc_path:
1201
- result["export_path"] = str(doc_path)
1202
- result["report_path"] = str(report_path) if report_path else None
1203
- logger.info(f"Documentation saved to: {doc_path}")
1204
-
1205
- # Chunk output for display if needed
1206
- output_chunks = self._chunk_output_for_display(
1207
- result["formatted_report"],
1208
- chunk_prefix="DOC OUTPUT",
1209
- )
1210
- if len(output_chunks) > 1:
1211
- result["output_chunks"] = output_chunks
1212
- result["output_chunk_count"] = len(output_chunks)
1213
- logger.info(
1214
- f"Report split into {len(output_chunks)} chunks for display "
1215
- f"(total {len(result['formatted_report'])} chars)",
1216
- )
1217
-
1218
- return (result, total_input_tokens, total_output_tokens)
1219
-
1220
- def _extract_functions_from_source(self, source_code: str) -> list[dict]:
1221
- """Extract function information from source code using AST.
1222
-
1223
- Args:
1224
- source_code: Python source code to parse
1225
-
1226
- Returns:
1227
- List of dicts with function information (name, args, returns, docstring)
1228
- """
1229
- import ast
1230
-
1231
- functions = []
1232
-
1233
- try:
1234
- tree = ast.parse(source_code)
1235
- except SyntaxError as e:
1236
- logger.warning(f"Failed to parse source code: {e}")
1237
- return functions
1238
-
1239
- for node in ast.walk(tree):
1240
- # Extract top-level functions and class methods
1241
- if isinstance(node, ast.FunctionDef):
1242
- # Skip private functions (starting with _)
1243
- if node.name.startswith("_"):
1244
- continue
1245
-
1246
- # Extract function signature
1247
- args_list = []
1248
- for arg in node.args.args:
1249
- arg_name = arg.arg
1250
- # Get type annotation if available
1251
- arg_type = ast.unparse(arg.annotation) if arg.annotation else "Any"
1252
- args_list.append({"name": arg_name, "type": arg_type})
1253
-
1254
- # Extract return type
1255
- return_type = ast.unparse(node.returns) if node.returns else "Any"
1256
-
1257
- # Extract docstring
1258
- docstring = ast.get_docstring(node) or ""
1259
-
1260
- functions.append({
1261
- "name": node.name,
1262
- "args": args_list,
1263
- "return_type": return_type,
1264
- "docstring": docstring,
1265
- "lineno": node.lineno,
1266
- })
1267
-
1268
- return functions
1269
-
1270
- async def _generate_api_section_for_function(
1271
- self,
1272
- func_info: dict,
1273
- tier: ModelTier,
1274
- ) -> str:
1275
- """Generate structured API reference section for a single function.
1276
-
1277
- This is a focused prompt that ONLY asks for Args/Returns/Raises format,
1278
- not narrative documentation.
1279
-
1280
- Args:
1281
- func_info: Function information from AST extraction
1282
- tier: Model tier to use for generation
1283
-
1284
- Returns:
1285
- Markdown formatted API reference section
1286
- """
1287
- func_name = func_info["name"]
1288
- args_list = func_info["args"]
1289
- return_type = func_info["return_type"]
1290
- docstring = func_info["docstring"]
1291
-
1292
- # Build function signature
1293
- args_str = ", ".join([f"{arg['name']}: {arg['type']}" for arg in args_list])
1294
- signature = f"def {func_name}({args_str}) -> {return_type}"
1295
-
1296
- system = """You are an API documentation generator. Output ONLY structured API reference sections in the EXACT format specified below.
1297
-
1298
- CRITICAL: Do NOT write explanatory text, questions, or narrative. Output ONLY the formatted section.
1299
-
1300
- REQUIRED FORMAT (copy this structure EXACTLY, replace bracketed content):
1301
-
1302
- ### `function_name()`
1303
-
1304
- **Function Signature:**
1305
- ```python
1306
- def function_name(param: type) -> return_type
1307
- ```
1308
-
1309
- **Description:**
1310
- Brief 1-2 sentence description.
1311
-
1312
- **Args:**
1313
- - `param_name` (`type`): Parameter description
1314
-
1315
- **Returns:**
1316
- - `return_type`: Return value description
1317
-
1318
- **Raises:**
1319
- - `ExceptionType`: When this exception occurs
1320
-
1321
- IMPORTANT:
1322
- - Use "**Args:**" (NOT "Parameters" or "params")
1323
- - Write "None" if no Args/Returns/Raises
1324
- - NO conversational text - just the formatted section"""
1325
-
1326
- user_message = f"""Generate API reference section using EXACT format specified in system prompt.
1327
-
1328
- Function:
1329
- ```python
1330
- {signature}
1331
- ```
1332
-
1333
- Docstring:
1334
- {docstring if docstring else "No docstring"}
1335
-
1336
- Output the formatted section EXACTLY as shown in system prompt. Use **Args:** (not Parameters). NO conversational text."""
1337
-
1338
- try:
1339
- response, input_tokens, output_tokens = await self._call_llm(
1340
- tier,
1341
- system,
1342
- user_message,
1343
- max_tokens=1000, # Small response - just the structured section
1344
- )
1345
-
1346
- # Track cost
1347
- self._track_cost(tier, input_tokens, output_tokens)
1348
-
1349
- return response
1350
-
1351
- except Exception as e:
1352
- logger.error(f"Failed to generate API section for {func_name}: {e}")
1353
- # Return minimal fallback
1354
- return f"""### `{func_name}()`
1355
-
1356
- **Function Signature:**
1357
- ```python
1358
- {signature}
1359
- ```
1360
-
1361
- **Description:**
1362
- {docstring.split('.')[0] if docstring else "No description available."}
1363
-
1364
- **Args:**
1365
- None
1366
-
1367
- **Returns:**
1368
- - `{return_type}`: Return value
1369
-
1370
- **Raises:**
1371
- None
1372
- """
1373
-
1374
- async def _add_api_reference_sections(
1375
- self,
1376
- narrative_doc: str,
1377
- source_code: str,
1378
- tier: ModelTier,
1379
- ) -> str:
1380
- """Add structured API reference sections to narrative documentation.
1381
-
1382
- This is Step 4 of the pipeline: after outline, write, and polish,
1383
- we add structured API reference sections extracted from source code.
1384
-
1385
- Args:
1386
- narrative_doc: The polished narrative documentation
1387
- source_code: Original source code to extract functions from
1388
- tier: Model tier to use for API section generation
1389
-
1390
- Returns:
1391
- Complete documentation with API reference appendix
1392
- """
1393
- logger.info("Adding structured API reference sections...")
1394
-
1395
- # Extract functions from source code
1396
- functions = self._extract_functions_from_source(source_code)
1397
-
1398
- if not functions:
1399
- logger.warning("No public functions found in source code")
1400
- return narrative_doc
1401
-
1402
- logger.info(f"Found {len(functions)} public functions to document")
1403
-
1404
- # Generate API section for each function
1405
- api_sections = []
1406
- for func_info in functions:
1407
- func_name = func_info["name"]
1408
- logger.debug(f"Generating API reference for {func_name}()")
1409
-
1410
- api_section = await self._generate_api_section_for_function(
1411
- func_info, tier
1412
- )
1413
- api_sections.append(api_section)
1414
-
1415
- # Append API reference section to narrative doc
1416
- full_doc = narrative_doc
1417
- full_doc += "\n\n---\n\n"
1418
- full_doc += "## API Reference\n\n"
1419
- full_doc += "Complete structured reference for all public functions:\n\n"
1420
- full_doc += "\n\n".join(api_sections)
1421
-
1422
- logger.info(f"Added {len(api_sections)} API reference sections")
1423
-
1424
- return full_doc
1425
-
1426
-