aiecs 1.5.1__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 (302) hide show
  1. aiecs/__init__.py +72 -0
  2. aiecs/__main__.py +41 -0
  3. aiecs/aiecs_client.py +469 -0
  4. aiecs/application/__init__.py +10 -0
  5. aiecs/application/executors/__init__.py +10 -0
  6. aiecs/application/executors/operation_executor.py +363 -0
  7. aiecs/application/knowledge_graph/__init__.py +7 -0
  8. aiecs/application/knowledge_graph/builder/__init__.py +37 -0
  9. aiecs/application/knowledge_graph/builder/document_builder.py +375 -0
  10. aiecs/application/knowledge_graph/builder/graph_builder.py +356 -0
  11. aiecs/application/knowledge_graph/builder/schema_mapping.py +531 -0
  12. aiecs/application/knowledge_graph/builder/structured_pipeline.py +443 -0
  13. aiecs/application/knowledge_graph/builder/text_chunker.py +319 -0
  14. aiecs/application/knowledge_graph/extractors/__init__.py +27 -0
  15. aiecs/application/knowledge_graph/extractors/base.py +100 -0
  16. aiecs/application/knowledge_graph/extractors/llm_entity_extractor.py +327 -0
  17. aiecs/application/knowledge_graph/extractors/llm_relation_extractor.py +349 -0
  18. aiecs/application/knowledge_graph/extractors/ner_entity_extractor.py +244 -0
  19. aiecs/application/knowledge_graph/fusion/__init__.py +23 -0
  20. aiecs/application/knowledge_graph/fusion/entity_deduplicator.py +387 -0
  21. aiecs/application/knowledge_graph/fusion/entity_linker.py +343 -0
  22. aiecs/application/knowledge_graph/fusion/knowledge_fusion.py +580 -0
  23. aiecs/application/knowledge_graph/fusion/relation_deduplicator.py +189 -0
  24. aiecs/application/knowledge_graph/pattern_matching/__init__.py +21 -0
  25. aiecs/application/knowledge_graph/pattern_matching/pattern_matcher.py +344 -0
  26. aiecs/application/knowledge_graph/pattern_matching/query_executor.py +378 -0
  27. aiecs/application/knowledge_graph/profiling/__init__.py +12 -0
  28. aiecs/application/knowledge_graph/profiling/query_plan_visualizer.py +199 -0
  29. aiecs/application/knowledge_graph/profiling/query_profiler.py +223 -0
  30. aiecs/application/knowledge_graph/reasoning/__init__.py +27 -0
  31. aiecs/application/knowledge_graph/reasoning/evidence_synthesis.py +347 -0
  32. aiecs/application/knowledge_graph/reasoning/inference_engine.py +504 -0
  33. aiecs/application/knowledge_graph/reasoning/logic_form_parser.py +167 -0
  34. aiecs/application/knowledge_graph/reasoning/logic_parser/__init__.py +79 -0
  35. aiecs/application/knowledge_graph/reasoning/logic_parser/ast_builder.py +513 -0
  36. aiecs/application/knowledge_graph/reasoning/logic_parser/ast_nodes.py +630 -0
  37. aiecs/application/knowledge_graph/reasoning/logic_parser/ast_validator.py +654 -0
  38. aiecs/application/knowledge_graph/reasoning/logic_parser/error_handler.py +477 -0
  39. aiecs/application/knowledge_graph/reasoning/logic_parser/parser.py +390 -0
  40. aiecs/application/knowledge_graph/reasoning/logic_parser/query_context.py +217 -0
  41. aiecs/application/knowledge_graph/reasoning/logic_query_integration.py +169 -0
  42. aiecs/application/knowledge_graph/reasoning/query_planner.py +872 -0
  43. aiecs/application/knowledge_graph/reasoning/reasoning_engine.py +554 -0
  44. aiecs/application/knowledge_graph/retrieval/__init__.py +19 -0
  45. aiecs/application/knowledge_graph/retrieval/retrieval_strategies.py +596 -0
  46. aiecs/application/knowledge_graph/search/__init__.py +59 -0
  47. aiecs/application/knowledge_graph/search/hybrid_search.py +423 -0
  48. aiecs/application/knowledge_graph/search/reranker.py +295 -0
  49. aiecs/application/knowledge_graph/search/reranker_strategies.py +553 -0
  50. aiecs/application/knowledge_graph/search/text_similarity.py +398 -0
  51. aiecs/application/knowledge_graph/traversal/__init__.py +15 -0
  52. aiecs/application/knowledge_graph/traversal/enhanced_traversal.py +329 -0
  53. aiecs/application/knowledge_graph/traversal/path_scorer.py +269 -0
  54. aiecs/application/knowledge_graph/validators/__init__.py +13 -0
  55. aiecs/application/knowledge_graph/validators/relation_validator.py +189 -0
  56. aiecs/application/knowledge_graph/visualization/__init__.py +11 -0
  57. aiecs/application/knowledge_graph/visualization/graph_visualizer.py +321 -0
  58. aiecs/common/__init__.py +9 -0
  59. aiecs/common/knowledge_graph/__init__.py +17 -0
  60. aiecs/common/knowledge_graph/runnable.py +484 -0
  61. aiecs/config/__init__.py +16 -0
  62. aiecs/config/config.py +498 -0
  63. aiecs/config/graph_config.py +137 -0
  64. aiecs/config/registry.py +23 -0
  65. aiecs/core/__init__.py +46 -0
  66. aiecs/core/interface/__init__.py +34 -0
  67. aiecs/core/interface/execution_interface.py +152 -0
  68. aiecs/core/interface/storage_interface.py +171 -0
  69. aiecs/domain/__init__.py +289 -0
  70. aiecs/domain/agent/__init__.py +189 -0
  71. aiecs/domain/agent/base_agent.py +697 -0
  72. aiecs/domain/agent/exceptions.py +103 -0
  73. aiecs/domain/agent/graph_aware_mixin.py +559 -0
  74. aiecs/domain/agent/hybrid_agent.py +490 -0
  75. aiecs/domain/agent/integration/__init__.py +26 -0
  76. aiecs/domain/agent/integration/context_compressor.py +222 -0
  77. aiecs/domain/agent/integration/context_engine_adapter.py +252 -0
  78. aiecs/domain/agent/integration/retry_policy.py +219 -0
  79. aiecs/domain/agent/integration/role_config.py +213 -0
  80. aiecs/domain/agent/knowledge_aware_agent.py +646 -0
  81. aiecs/domain/agent/lifecycle.py +296 -0
  82. aiecs/domain/agent/llm_agent.py +300 -0
  83. aiecs/domain/agent/memory/__init__.py +12 -0
  84. aiecs/domain/agent/memory/conversation.py +197 -0
  85. aiecs/domain/agent/migration/__init__.py +14 -0
  86. aiecs/domain/agent/migration/conversion.py +160 -0
  87. aiecs/domain/agent/migration/legacy_wrapper.py +90 -0
  88. aiecs/domain/agent/models.py +317 -0
  89. aiecs/domain/agent/observability.py +407 -0
  90. aiecs/domain/agent/persistence.py +289 -0
  91. aiecs/domain/agent/prompts/__init__.py +29 -0
  92. aiecs/domain/agent/prompts/builder.py +161 -0
  93. aiecs/domain/agent/prompts/formatters.py +189 -0
  94. aiecs/domain/agent/prompts/template.py +255 -0
  95. aiecs/domain/agent/registry.py +260 -0
  96. aiecs/domain/agent/tool_agent.py +257 -0
  97. aiecs/domain/agent/tools/__init__.py +12 -0
  98. aiecs/domain/agent/tools/schema_generator.py +221 -0
  99. aiecs/domain/community/__init__.py +155 -0
  100. aiecs/domain/community/agent_adapter.py +477 -0
  101. aiecs/domain/community/analytics.py +481 -0
  102. aiecs/domain/community/collaborative_workflow.py +642 -0
  103. aiecs/domain/community/communication_hub.py +645 -0
  104. aiecs/domain/community/community_builder.py +320 -0
  105. aiecs/domain/community/community_integration.py +800 -0
  106. aiecs/domain/community/community_manager.py +813 -0
  107. aiecs/domain/community/decision_engine.py +879 -0
  108. aiecs/domain/community/exceptions.py +225 -0
  109. aiecs/domain/community/models/__init__.py +33 -0
  110. aiecs/domain/community/models/community_models.py +268 -0
  111. aiecs/domain/community/resource_manager.py +457 -0
  112. aiecs/domain/community/shared_context_manager.py +603 -0
  113. aiecs/domain/context/__init__.py +58 -0
  114. aiecs/domain/context/context_engine.py +989 -0
  115. aiecs/domain/context/conversation_models.py +354 -0
  116. aiecs/domain/context/graph_memory.py +467 -0
  117. aiecs/domain/execution/__init__.py +12 -0
  118. aiecs/domain/execution/model.py +57 -0
  119. aiecs/domain/knowledge_graph/__init__.py +19 -0
  120. aiecs/domain/knowledge_graph/models/__init__.py +52 -0
  121. aiecs/domain/knowledge_graph/models/entity.py +130 -0
  122. aiecs/domain/knowledge_graph/models/evidence.py +194 -0
  123. aiecs/domain/knowledge_graph/models/inference_rule.py +186 -0
  124. aiecs/domain/knowledge_graph/models/path.py +179 -0
  125. aiecs/domain/knowledge_graph/models/path_pattern.py +173 -0
  126. aiecs/domain/knowledge_graph/models/query.py +272 -0
  127. aiecs/domain/knowledge_graph/models/query_plan.py +187 -0
  128. aiecs/domain/knowledge_graph/models/relation.py +136 -0
  129. aiecs/domain/knowledge_graph/schema/__init__.py +23 -0
  130. aiecs/domain/knowledge_graph/schema/entity_type.py +135 -0
  131. aiecs/domain/knowledge_graph/schema/graph_schema.py +271 -0
  132. aiecs/domain/knowledge_graph/schema/property_schema.py +155 -0
  133. aiecs/domain/knowledge_graph/schema/relation_type.py +171 -0
  134. aiecs/domain/knowledge_graph/schema/schema_manager.py +496 -0
  135. aiecs/domain/knowledge_graph/schema/type_enums.py +205 -0
  136. aiecs/domain/task/__init__.py +13 -0
  137. aiecs/domain/task/dsl_processor.py +613 -0
  138. aiecs/domain/task/model.py +62 -0
  139. aiecs/domain/task/task_context.py +268 -0
  140. aiecs/infrastructure/__init__.py +24 -0
  141. aiecs/infrastructure/graph_storage/__init__.py +11 -0
  142. aiecs/infrastructure/graph_storage/base.py +601 -0
  143. aiecs/infrastructure/graph_storage/batch_operations.py +449 -0
  144. aiecs/infrastructure/graph_storage/cache.py +429 -0
  145. aiecs/infrastructure/graph_storage/distributed.py +226 -0
  146. aiecs/infrastructure/graph_storage/error_handling.py +390 -0
  147. aiecs/infrastructure/graph_storage/graceful_degradation.py +306 -0
  148. aiecs/infrastructure/graph_storage/health_checks.py +378 -0
  149. aiecs/infrastructure/graph_storage/in_memory.py +514 -0
  150. aiecs/infrastructure/graph_storage/index_optimization.py +483 -0
  151. aiecs/infrastructure/graph_storage/lazy_loading.py +410 -0
  152. aiecs/infrastructure/graph_storage/metrics.py +357 -0
  153. aiecs/infrastructure/graph_storage/migration.py +413 -0
  154. aiecs/infrastructure/graph_storage/pagination.py +471 -0
  155. aiecs/infrastructure/graph_storage/performance_monitoring.py +466 -0
  156. aiecs/infrastructure/graph_storage/postgres.py +871 -0
  157. aiecs/infrastructure/graph_storage/query_optimizer.py +635 -0
  158. aiecs/infrastructure/graph_storage/schema_cache.py +290 -0
  159. aiecs/infrastructure/graph_storage/sqlite.py +623 -0
  160. aiecs/infrastructure/graph_storage/streaming.py +495 -0
  161. aiecs/infrastructure/messaging/__init__.py +13 -0
  162. aiecs/infrastructure/messaging/celery_task_manager.py +383 -0
  163. aiecs/infrastructure/messaging/websocket_manager.py +298 -0
  164. aiecs/infrastructure/monitoring/__init__.py +34 -0
  165. aiecs/infrastructure/monitoring/executor_metrics.py +174 -0
  166. aiecs/infrastructure/monitoring/global_metrics_manager.py +213 -0
  167. aiecs/infrastructure/monitoring/structured_logger.py +48 -0
  168. aiecs/infrastructure/monitoring/tracing_manager.py +410 -0
  169. aiecs/infrastructure/persistence/__init__.py +24 -0
  170. aiecs/infrastructure/persistence/context_engine_client.py +187 -0
  171. aiecs/infrastructure/persistence/database_manager.py +333 -0
  172. aiecs/infrastructure/persistence/file_storage.py +754 -0
  173. aiecs/infrastructure/persistence/redis_client.py +220 -0
  174. aiecs/llm/__init__.py +86 -0
  175. aiecs/llm/callbacks/__init__.py +11 -0
  176. aiecs/llm/callbacks/custom_callbacks.py +264 -0
  177. aiecs/llm/client_factory.py +420 -0
  178. aiecs/llm/clients/__init__.py +33 -0
  179. aiecs/llm/clients/base_client.py +193 -0
  180. aiecs/llm/clients/googleai_client.py +181 -0
  181. aiecs/llm/clients/openai_client.py +131 -0
  182. aiecs/llm/clients/vertex_client.py +437 -0
  183. aiecs/llm/clients/xai_client.py +184 -0
  184. aiecs/llm/config/__init__.py +51 -0
  185. aiecs/llm/config/config_loader.py +275 -0
  186. aiecs/llm/config/config_validator.py +236 -0
  187. aiecs/llm/config/model_config.py +151 -0
  188. aiecs/llm/utils/__init__.py +10 -0
  189. aiecs/llm/utils/validate_config.py +91 -0
  190. aiecs/main.py +363 -0
  191. aiecs/scripts/__init__.py +3 -0
  192. aiecs/scripts/aid/VERSION_MANAGEMENT.md +97 -0
  193. aiecs/scripts/aid/__init__.py +19 -0
  194. aiecs/scripts/aid/version_manager.py +215 -0
  195. aiecs/scripts/dependance_check/DEPENDENCY_SYSTEM_SUMMARY.md +242 -0
  196. aiecs/scripts/dependance_check/README_DEPENDENCY_CHECKER.md +310 -0
  197. aiecs/scripts/dependance_check/__init__.py +17 -0
  198. aiecs/scripts/dependance_check/dependency_checker.py +938 -0
  199. aiecs/scripts/dependance_check/dependency_fixer.py +391 -0
  200. aiecs/scripts/dependance_check/download_nlp_data.py +396 -0
  201. aiecs/scripts/dependance_check/quick_dependency_check.py +270 -0
  202. aiecs/scripts/dependance_check/setup_nlp_data.sh +217 -0
  203. aiecs/scripts/dependance_patch/__init__.py +7 -0
  204. aiecs/scripts/dependance_patch/fix_weasel/README_WEASEL_PATCH.md +126 -0
  205. aiecs/scripts/dependance_patch/fix_weasel/__init__.py +11 -0
  206. aiecs/scripts/dependance_patch/fix_weasel/fix_weasel_validator.py +128 -0
  207. aiecs/scripts/dependance_patch/fix_weasel/fix_weasel_validator.sh +82 -0
  208. aiecs/scripts/dependance_patch/fix_weasel/patch_weasel_library.sh +188 -0
  209. aiecs/scripts/dependance_patch/fix_weasel/run_weasel_patch.sh +41 -0
  210. aiecs/scripts/tools_develop/README.md +449 -0
  211. aiecs/scripts/tools_develop/TOOL_AUTO_DISCOVERY.md +234 -0
  212. aiecs/scripts/tools_develop/__init__.py +21 -0
  213. aiecs/scripts/tools_develop/check_type_annotations.py +259 -0
  214. aiecs/scripts/tools_develop/validate_tool_schemas.py +422 -0
  215. aiecs/scripts/tools_develop/verify_tools.py +356 -0
  216. aiecs/tasks/__init__.py +1 -0
  217. aiecs/tasks/worker.py +172 -0
  218. aiecs/tools/__init__.py +299 -0
  219. aiecs/tools/apisource/__init__.py +99 -0
  220. aiecs/tools/apisource/intelligence/__init__.py +19 -0
  221. aiecs/tools/apisource/intelligence/data_fusion.py +381 -0
  222. aiecs/tools/apisource/intelligence/query_analyzer.py +413 -0
  223. aiecs/tools/apisource/intelligence/search_enhancer.py +388 -0
  224. aiecs/tools/apisource/monitoring/__init__.py +9 -0
  225. aiecs/tools/apisource/monitoring/metrics.py +303 -0
  226. aiecs/tools/apisource/providers/__init__.py +115 -0
  227. aiecs/tools/apisource/providers/base.py +664 -0
  228. aiecs/tools/apisource/providers/census.py +401 -0
  229. aiecs/tools/apisource/providers/fred.py +564 -0
  230. aiecs/tools/apisource/providers/newsapi.py +412 -0
  231. aiecs/tools/apisource/providers/worldbank.py +357 -0
  232. aiecs/tools/apisource/reliability/__init__.py +12 -0
  233. aiecs/tools/apisource/reliability/error_handler.py +375 -0
  234. aiecs/tools/apisource/reliability/fallback_strategy.py +391 -0
  235. aiecs/tools/apisource/tool.py +850 -0
  236. aiecs/tools/apisource/utils/__init__.py +9 -0
  237. aiecs/tools/apisource/utils/validators.py +338 -0
  238. aiecs/tools/base_tool.py +201 -0
  239. aiecs/tools/docs/__init__.py +121 -0
  240. aiecs/tools/docs/ai_document_orchestrator.py +599 -0
  241. aiecs/tools/docs/ai_document_writer_orchestrator.py +2403 -0
  242. aiecs/tools/docs/content_insertion_tool.py +1333 -0
  243. aiecs/tools/docs/document_creator_tool.py +1317 -0
  244. aiecs/tools/docs/document_layout_tool.py +1166 -0
  245. aiecs/tools/docs/document_parser_tool.py +994 -0
  246. aiecs/tools/docs/document_writer_tool.py +1818 -0
  247. aiecs/tools/knowledge_graph/__init__.py +17 -0
  248. aiecs/tools/knowledge_graph/graph_reasoning_tool.py +734 -0
  249. aiecs/tools/knowledge_graph/graph_search_tool.py +923 -0
  250. aiecs/tools/knowledge_graph/kg_builder_tool.py +476 -0
  251. aiecs/tools/langchain_adapter.py +542 -0
  252. aiecs/tools/schema_generator.py +275 -0
  253. aiecs/tools/search_tool/__init__.py +100 -0
  254. aiecs/tools/search_tool/analyzers.py +589 -0
  255. aiecs/tools/search_tool/cache.py +260 -0
  256. aiecs/tools/search_tool/constants.py +128 -0
  257. aiecs/tools/search_tool/context.py +216 -0
  258. aiecs/tools/search_tool/core.py +749 -0
  259. aiecs/tools/search_tool/deduplicator.py +123 -0
  260. aiecs/tools/search_tool/error_handler.py +271 -0
  261. aiecs/tools/search_tool/metrics.py +371 -0
  262. aiecs/tools/search_tool/rate_limiter.py +178 -0
  263. aiecs/tools/search_tool/schemas.py +277 -0
  264. aiecs/tools/statistics/__init__.py +80 -0
  265. aiecs/tools/statistics/ai_data_analysis_orchestrator.py +643 -0
  266. aiecs/tools/statistics/ai_insight_generator_tool.py +505 -0
  267. aiecs/tools/statistics/ai_report_orchestrator_tool.py +694 -0
  268. aiecs/tools/statistics/data_loader_tool.py +564 -0
  269. aiecs/tools/statistics/data_profiler_tool.py +658 -0
  270. aiecs/tools/statistics/data_transformer_tool.py +573 -0
  271. aiecs/tools/statistics/data_visualizer_tool.py +495 -0
  272. aiecs/tools/statistics/model_trainer_tool.py +487 -0
  273. aiecs/tools/statistics/statistical_analyzer_tool.py +459 -0
  274. aiecs/tools/task_tools/__init__.py +86 -0
  275. aiecs/tools/task_tools/chart_tool.py +732 -0
  276. aiecs/tools/task_tools/classfire_tool.py +922 -0
  277. aiecs/tools/task_tools/image_tool.py +447 -0
  278. aiecs/tools/task_tools/office_tool.py +684 -0
  279. aiecs/tools/task_tools/pandas_tool.py +635 -0
  280. aiecs/tools/task_tools/report_tool.py +635 -0
  281. aiecs/tools/task_tools/research_tool.py +392 -0
  282. aiecs/tools/task_tools/scraper_tool.py +715 -0
  283. aiecs/tools/task_tools/stats_tool.py +688 -0
  284. aiecs/tools/temp_file_manager.py +130 -0
  285. aiecs/tools/tool_executor/__init__.py +37 -0
  286. aiecs/tools/tool_executor/tool_executor.py +881 -0
  287. aiecs/utils/LLM_output_structor.py +445 -0
  288. aiecs/utils/__init__.py +34 -0
  289. aiecs/utils/base_callback.py +47 -0
  290. aiecs/utils/cache_provider.py +695 -0
  291. aiecs/utils/execution_utils.py +184 -0
  292. aiecs/utils/logging.py +1 -0
  293. aiecs/utils/prompt_loader.py +14 -0
  294. aiecs/utils/token_usage_repository.py +323 -0
  295. aiecs/ws/__init__.py +0 -0
  296. aiecs/ws/socket_server.py +52 -0
  297. aiecs-1.5.1.dist-info/METADATA +608 -0
  298. aiecs-1.5.1.dist-info/RECORD +302 -0
  299. aiecs-1.5.1.dist-info/WHEEL +5 -0
  300. aiecs-1.5.1.dist-info/entry_points.txt +10 -0
  301. aiecs-1.5.1.dist-info/licenses/LICENSE +225 -0
  302. aiecs-1.5.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,2403 @@
1
+ import os
2
+ import asyncio
3
+ import logging
4
+ import tempfile
5
+ from typing import Dict, Any, List, Optional
6
+ from enum import Enum
7
+ from datetime import datetime
8
+
9
+ from pydantic import BaseModel, Field
10
+ from pydantic_settings import BaseSettings, SettingsConfigDict
11
+
12
+ from aiecs.tools.base_tool import BaseTool
13
+ from aiecs.tools import register_tool
14
+
15
+
16
+ class ContentGenerationMode(str, Enum):
17
+ """AI content generation modes"""
18
+
19
+ GENERATE = "generate" # 生成全新内容
20
+ ENHANCE = "enhance" # 增强现有内容
21
+ REWRITE = "rewrite" # 重写内容
22
+ TRANSLATE = "translate" # 翻译内容
23
+ CONVERT_FORMAT = "convert_format" # 格式转换
24
+ TEMPLATE_FILL = "template_fill" # 模板填充
25
+ FORMAT_CONTENT = "format_content" # 格式化内容
26
+ EDIT_CONTENT = "edit_content" # 编辑内容
27
+
28
+
29
+ class AIEditOperation(str, Enum):
30
+ """AI-driven editing operations"""
31
+
32
+ SMART_FORMAT = "smart_format" # AI智能格式化
33
+ STYLE_ENHANCE = "style_enhance" # 样式增强
34
+ CONTENT_RESTRUCTURE = "content_restructure" # 内容重构
35
+ INTELLIGENT_HIGHLIGHT = "intelligent_highlight" # 智能高亮
36
+ AUTO_BOLD_KEYWORDS = "auto_bold_keywords" # 自动加粗关键词
37
+ SMART_PARAGRAPH = "smart_paragraph" # 智能段落优化
38
+ AI_PROOFREADING = "ai_proofreading" # AI校对
39
+
40
+
41
+ class WriteStrategy(str, Enum):
42
+ """Document writing strategies"""
43
+
44
+ IMMEDIATE = "immediate" # 立即写入
45
+ REVIEW = "review" # 审核后写入
46
+ DRAFT = "draft" # 保存为草稿
47
+ STAGED = "staged" # 分阶段写入
48
+
49
+
50
+ class AIProvider(str, Enum):
51
+ """Supported AI providers"""
52
+
53
+ OPENAI = "openai"
54
+ VERTEX_AI = "vertex_ai"
55
+ XAI = "xai"
56
+ LOCAL = "local"
57
+
58
+
59
+ class AIDocumentWriterOrchestratorError(Exception):
60
+ """Base exception for AI Document Writer Orchestrator errors"""
61
+
62
+
63
+ class ContentGenerationError(AIDocumentWriterOrchestratorError):
64
+ """Raised when content generation fails"""
65
+
66
+
67
+ class WriteOrchestrationError(AIDocumentWriterOrchestratorError):
68
+ """Raised when write orchestration fails"""
69
+
70
+
71
+ @register_tool("ai_document_writer_orchestrator")
72
+ class AIDocumentWriterOrchestrator(BaseTool):
73
+ """
74
+ AI-powered document writing orchestrator that:
75
+ 1. Coordinates AI content generation with document writing
76
+ 2. Manages complex writing workflows
77
+ 3. Provides intelligent content enhancement and formatting
78
+ 4. Handles review and approval processes
79
+ 5. Supports template-based document generation
80
+
81
+ Integrates with:
82
+ - DocumentWriterTool for document writing operations
83
+ - Various AI providers for content generation
84
+ - Existing AIECS infrastructure
85
+ """
86
+
87
+ # Configuration schema
88
+ class Config(BaseSettings):
89
+ """Configuration for the AI document writer orchestrator tool
90
+
91
+ Automatically reads from environment variables with AI_DOC_WRITER_ prefix.
92
+ """
93
+
94
+ model_config = SettingsConfigDict(env_prefix="AI_DOC_WRITER_")
95
+
96
+ default_ai_provider: str = Field(default="openai", description="Default AI provider to use")
97
+ max_content_length: int = Field(
98
+ default=50000,
99
+ description="Maximum content length for AI generation",
100
+ )
101
+ max_concurrent_writes: int = Field(
102
+ default=5, description="Maximum concurrent write operations"
103
+ )
104
+ default_temperature: float = Field(
105
+ default=0.3, description="Default temperature for AI model"
106
+ )
107
+ max_tokens: int = Field(default=4000, description="Maximum tokens for AI response")
108
+ timeout: int = Field(default=60, description="Timeout in seconds for AI operations")
109
+ enable_draft_mode: bool = Field(default=True, description="Whether to enable draft mode")
110
+ enable_content_review: bool = Field(
111
+ default=True, description="Whether to enable content review"
112
+ )
113
+ auto_backup_on_ai_write: bool = Field(
114
+ default=True,
115
+ description="Whether to automatically backup before AI writes",
116
+ )
117
+ temp_dir: str = Field(
118
+ default=tempfile.gettempdir(),
119
+ description="Temporary directory for processing",
120
+ )
121
+
122
+ def __init__(self, config: Optional[Dict] = None):
123
+ """Initialize AI Document Writer Orchestrator with settings"""
124
+ super().__init__(config)
125
+
126
+ # Parse configuration
127
+ self.config = self.Config(**(config or {}))
128
+
129
+ self.logger = logging.getLogger(__name__)
130
+
131
+ # Initialize document writer
132
+ self._init_document_writer()
133
+
134
+ # Initialize document creation tools
135
+ self._init_document_creation_tools()
136
+
137
+ # Initialize AI providers
138
+ self._init_ai_providers()
139
+
140
+ # Initialize content generation templates
141
+ self._init_content_templates()
142
+
143
+ def _init_document_writer(self):
144
+ """Initialize document writer tool"""
145
+ try:
146
+ from aiecs.tools.docs.document_writer_tool import (
147
+ DocumentWriterTool,
148
+ )
149
+
150
+ self.document_writer = DocumentWriterTool()
151
+ except ImportError:
152
+ self.logger.error("DocumentWriterTool not available")
153
+ self.document_writer = None
154
+
155
+ def _init_document_creation_tools(self):
156
+ """Initialize document creation and layout tools"""
157
+ self.creation_tools = {}
158
+
159
+ # Initialize DocumentCreatorTool
160
+ try:
161
+ from aiecs.tools.docs.document_creator_tool import (
162
+ DocumentCreatorTool,
163
+ DocumentFormat,
164
+ DocumentType,
165
+ TemplateType,
166
+ )
167
+
168
+ self.creation_tools["creator"] = DocumentCreatorTool()
169
+ # Store classes for later use
170
+ self.DocumentFormat = DocumentFormat
171
+ self.DocumentType = DocumentType
172
+ self.TemplateType = TemplateType
173
+ self.logger.info("DocumentCreatorTool initialized successfully")
174
+ except ImportError:
175
+ self.logger.warning("DocumentCreatorTool not available")
176
+
177
+ # Initialize DocumentLayoutTool
178
+ try:
179
+ from aiecs.tools.docs.document_layout_tool import (
180
+ DocumentLayoutTool,
181
+ )
182
+
183
+ self.creation_tools["layout"] = DocumentLayoutTool()
184
+ self.logger.info("DocumentLayoutTool initialized successfully")
185
+ except ImportError:
186
+ self.logger.warning("DocumentLayoutTool not available")
187
+
188
+ # Initialize ContentInsertionTool
189
+ try:
190
+ from aiecs.tools.docs.content_insertion_tool import (
191
+ ContentInsertionTool,
192
+ )
193
+
194
+ self.creation_tools["content"] = ContentInsertionTool()
195
+ self.logger.info("ContentInsertionTool initialized successfully")
196
+ except ImportError:
197
+ self.logger.warning("ContentInsertionTool not available")
198
+
199
+ def _init_ai_providers(self):
200
+ """Initialize AI providers"""
201
+ self.ai_providers = {}
202
+
203
+ try:
204
+ # Initialize AIECS client for AI operations
205
+ from aiecs import AIECS
206
+
207
+ self.aiecs_client = AIECS()
208
+ self.ai_providers["aiecs"] = self.aiecs_client
209
+ except ImportError:
210
+ self.logger.warning("AIECS client not available")
211
+ self.aiecs_client = None
212
+
213
+ def _init_content_templates(self):
214
+ """Initialize content generation templates"""
215
+ self.content_templates = {
216
+ ContentGenerationMode.GENERATE: {
217
+ "system_prompt": "You are an expert content writer. Generate high-quality, well-structured content based on the given requirements.",
218
+ "user_prompt_template": "Generate content for: {content_type}\n\nRequirements:\n{requirements}\n\nTarget audience: {audience}\n\nPlease provide well-structured, engaging content that meets these requirements.",
219
+ },
220
+ ContentGenerationMode.ENHANCE: {
221
+ "system_prompt": "You are an expert content editor. Enhance and improve existing content while maintaining its core message.",
222
+ "user_prompt_template": "Enhance the following content:\n\n{existing_content}\n\nImprovement goals:\n{enhancement_goals}\n\nPlease provide an enhanced version that is more engaging, clear, and effective.",
223
+ },
224
+ ContentGenerationMode.REWRITE: {
225
+ "system_prompt": "You are an expert content rewriter. Rewrite content to improve clarity, style, and effectiveness.",
226
+ "user_prompt_template": "Rewrite the following content:\n\n{existing_content}\n\nRewriting goals:\n{rewrite_goals}\n\nTarget style: {target_style}\n\nPlease provide a completely rewritten version that maintains the core information but improves presentation.",
227
+ },
228
+ ContentGenerationMode.TRANSLATE: {
229
+ "system_prompt": "You are an expert translator. Provide accurate, natural translations that preserve meaning and context.",
230
+ "user_prompt_template": "Translate the following content to {target_language}:\n\n{content}\n\nPlease provide a natural, accurate translation that preserves the original meaning and tone.",
231
+ },
232
+ ContentGenerationMode.CONVERT_FORMAT: {
233
+ "system_prompt": "You are an expert document formatter. Convert content between different formats while preserving structure and meaning.",
234
+ "user_prompt_template": "Convert the following content from {source_format} to {target_format}:\n\n{content}\n\nPlease maintain the structure and ensure the converted format is properly formatted and readable.",
235
+ },
236
+ ContentGenerationMode.TEMPLATE_FILL: {
237
+ "system_prompt": "You are an expert template processor. Fill templates with appropriate content based on provided data.",
238
+ "user_prompt_template": "Fill the following template with the provided data:\n\nTemplate:\n{template}\n\nData:\n{data}\n\nPlease generate complete, coherent content that properly fills all template sections.",
239
+ },
240
+ }
241
+
242
+ # Schema definitions
243
+ class AIWriteDocumentSchema(BaseModel):
244
+ """Schema for ai_write_document operation"""
245
+
246
+ target_path: str = Field(description="Target file path")
247
+ content_requirements: str = Field(description="Content requirements and specifications")
248
+ generation_mode: ContentGenerationMode = Field(description="Content generation mode")
249
+ document_format: str = Field(description="Target document format")
250
+ write_strategy: WriteStrategy = Field(
251
+ default=WriteStrategy.IMMEDIATE, description="Write strategy"
252
+ )
253
+ ai_provider: Optional[AIProvider] = Field(default=None, description="AI provider to use")
254
+ generation_params: Optional[Dict[str, Any]] = Field(
255
+ default=None, description="AI generation parameters"
256
+ )
257
+ write_params: Optional[Dict[str, Any]] = Field(
258
+ default=None, description="Document write parameters"
259
+ )
260
+
261
+ class EnhanceDocumentSchema(BaseModel):
262
+ """Schema for enhance_document operation"""
263
+
264
+ source_path: str = Field(description="Source document path")
265
+ target_path: Optional[str] = Field(default=None, description="Target path (if different)")
266
+ enhancement_goals: str = Field(description="Enhancement goals and requirements")
267
+ ai_provider: Optional[AIProvider] = Field(default=None, description="AI provider to use")
268
+ preserve_format: bool = Field(default=True, description="Preserve original format")
269
+
270
+ class BatchWriteSchema(BaseModel):
271
+ """Schema for batch_ai_write operation"""
272
+
273
+ write_requests: List[Dict[str, Any]] = Field(description="List of write requests")
274
+ coordination_strategy: str = Field(default="parallel", description="Coordination strategy")
275
+ max_concurrent: Optional[int] = Field(
276
+ default=None, description="Maximum concurrent operations"
277
+ )
278
+
279
+ class AIEditDocumentSchema(BaseModel):
280
+ """Schema for ai_edit_document operation"""
281
+
282
+ target_path: str = Field(description="Target document path")
283
+ edit_operation: AIEditOperation = Field(description="AI editing operation to perform")
284
+ edit_instructions: str = Field(description="Specific editing instructions")
285
+ ai_provider: Optional[AIProvider] = Field(default=None, description="AI provider to use")
286
+ preserve_structure: bool = Field(default=True, description="Preserve document structure")
287
+ format_options: Optional[Dict[str, Any]] = Field(
288
+ default=None, description="Format-specific options"
289
+ )
290
+
291
+ class SmartFormatSchema(BaseModel):
292
+ """Schema for smart_format_document operation"""
293
+
294
+ target_path: str = Field(description="Target document path")
295
+ format_goals: str = Field(description="Formatting goals and requirements")
296
+ target_format: str = Field(description="Target document format")
297
+ style_preferences: Optional[Dict[str, Any]] = Field(
298
+ default=None, description="Style preferences"
299
+ )
300
+
301
+ class ContentAnalysisSchema(BaseModel):
302
+ """Schema for analyze_document_content operation"""
303
+
304
+ source_path: str = Field(description="Source document path")
305
+ analysis_type: str = Field(description="Type of analysis to perform")
306
+ analysis_params: Optional[Dict[str, Any]] = Field(
307
+ default=None, description="Analysis parameters"
308
+ )
309
+
310
+ class CreateRichDocumentSchema(BaseModel):
311
+ """Schema for create_rich_document operation"""
312
+
313
+ document_template: str = Field(description="Document template type")
314
+ content_plan: Dict[str, Any] = Field(description="Content planning configuration")
315
+ layout_config: Optional[Dict[str, Any]] = Field(
316
+ default=None, description="Layout configuration"
317
+ )
318
+ output_path: Optional[str] = Field(default=None, description="Custom output path")
319
+ ai_assistance: bool = Field(
320
+ default=True,
321
+ description="Use AI assistance for content generation",
322
+ )
323
+
324
+ class GenerateDocumentWithChartsSchema(BaseModel):
325
+ """Schema for generate_document_with_charts operation"""
326
+
327
+ requirements: str = Field(description="Document requirements and specifications")
328
+ data_sources: List[Dict[str, Any]] = Field(description="Data sources for charts and tables")
329
+ document_type: str = Field(description="Type of document to generate")
330
+ include_analysis: bool = Field(default=True, description="Include data analysis sections")
331
+ chart_preferences: Optional[Dict[str, Any]] = Field(
332
+ default=None, description="Chart style preferences"
333
+ )
334
+
335
+ class OptimizeDocumentLayoutSchema(BaseModel):
336
+ """Schema for optimize_document_layout operation"""
337
+
338
+ document_path: str = Field(description="Path to document to optimize")
339
+ optimization_goals: List[str] = Field(description="Layout optimization goals")
340
+ preserve_content: bool = Field(default=True, description="Preserve existing content")
341
+ layout_style: Optional[str] = Field(default=None, description="Target layout style")
342
+
343
+ class BatchContentInsertionSchema(BaseModel):
344
+ """Schema for batch_content_insertion operation"""
345
+
346
+ document_path: str = Field(description="Target document path")
347
+ content_plan: List[Dict[str, Any]] = Field(description="Content insertion plan")
348
+ insertion_strategy: str = Field(default="sequential", description="Insertion strategy")
349
+ ai_optimization: bool = Field(default=True, description="Use AI for content optimization")
350
+
351
+ def ai_write_document(
352
+ self,
353
+ target_path: str,
354
+ content_requirements: str,
355
+ generation_mode: ContentGenerationMode,
356
+ document_format: str = "txt",
357
+ write_strategy: WriteStrategy = WriteStrategy.IMMEDIATE,
358
+ ai_provider: Optional[AIProvider] = None,
359
+ generation_params: Optional[Dict[str, Any]] = None,
360
+ write_params: Optional[Dict[str, Any]] = None,
361
+ ) -> Dict[str, Any]:
362
+ """
363
+ Generate content using AI and write to document
364
+
365
+ Args:
366
+ target_path: Target file path
367
+ content_requirements: Content requirements and specifications
368
+ generation_mode: Content generation mode
369
+ document_format: Target document format
370
+ write_strategy: Write strategy (immediate, review, draft, staged)
371
+ ai_provider: AI provider to use
372
+ generation_params: AI generation parameters
373
+ write_params: Document write parameters
374
+
375
+ Returns:
376
+ Dict containing generation and write results
377
+ """
378
+ try:
379
+ start_time = datetime.now()
380
+ # Use microsecond precision for unique IDs
381
+ operation_id = f"ai_write_{int(start_time.timestamp() * 1000000)}"
382
+
383
+ self.logger.info(f"Starting AI write operation {operation_id}: {target_path}")
384
+
385
+ # Step 1: Generate content using AI
386
+ ai_result = self._generate_content_with_ai(
387
+ content_requirements,
388
+ generation_mode,
389
+ document_format,
390
+ ai_provider or self.config.default_ai_provider,
391
+ generation_params or {},
392
+ )
393
+
394
+ # Step 2: Process generated content
395
+ processed_content = self._process_generated_content(
396
+ ai_result["generated_content"],
397
+ document_format,
398
+ generation_mode,
399
+ )
400
+
401
+ # Step 3: Handle write strategy
402
+ write_result = self._execute_write_strategy(
403
+ target_path,
404
+ processed_content,
405
+ document_format,
406
+ write_strategy,
407
+ write_params or {},
408
+ )
409
+
410
+ # Step 4: Post-processing
411
+ post_process_result = self._post_process_ai_write(
412
+ operation_id,
413
+ target_path,
414
+ ai_result,
415
+ write_result,
416
+ write_strategy,
417
+ )
418
+
419
+ result = {
420
+ "operation_id": operation_id,
421
+ "target_path": target_path,
422
+ "generation_mode": generation_mode,
423
+ "document_format": document_format,
424
+ "write_strategy": write_strategy,
425
+ "ai_provider": ai_provider or self.config.default_ai_provider,
426
+ "ai_result": ai_result,
427
+ "write_result": write_result,
428
+ "post_process_result": post_process_result,
429
+ "processing_metadata": {
430
+ "start_time": start_time.isoformat(),
431
+ "end_time": datetime.now().isoformat(),
432
+ "duration": (datetime.now() - start_time).total_seconds(),
433
+ },
434
+ }
435
+
436
+ self.logger.info(f"AI write operation {operation_id} completed successfully")
437
+ return result
438
+
439
+ except Exception as e:
440
+ raise WriteOrchestrationError(f"AI write operation failed: {str(e)}")
441
+
442
+ async def ai_write_document_async(
443
+ self,
444
+ target_path: str,
445
+ content_requirements: str,
446
+ generation_mode: ContentGenerationMode,
447
+ document_format: str = "txt",
448
+ write_strategy: WriteStrategy = WriteStrategy.IMMEDIATE,
449
+ ai_provider: Optional[AIProvider] = None,
450
+ generation_params: Optional[Dict[str, Any]] = None,
451
+ write_params: Optional[Dict[str, Any]] = None,
452
+ ) -> Dict[str, Any]:
453
+ """Async version of ai_write_document"""
454
+ return await asyncio.to_thread(
455
+ self.ai_write_document,
456
+ target_path=target_path,
457
+ content_requirements=content_requirements,
458
+ generation_mode=generation_mode,
459
+ document_format=document_format,
460
+ write_strategy=write_strategy,
461
+ ai_provider=ai_provider,
462
+ generation_params=generation_params,
463
+ write_params=write_params,
464
+ )
465
+
466
+ def enhance_document(
467
+ self,
468
+ source_path: str,
469
+ enhancement_goals: str,
470
+ target_path: Optional[str] = None,
471
+ ai_provider: Optional[AIProvider] = None,
472
+ preserve_format: bool = True,
473
+ ) -> Dict[str, Any]:
474
+ """
475
+ Enhance existing document using AI
476
+
477
+ Args:
478
+ source_path: Source document path
479
+ enhancement_goals: Enhancement goals and requirements
480
+ target_path: Target path (if different from source)
481
+ ai_provider: AI provider to use
482
+ preserve_format: Preserve original document format
483
+
484
+ Returns:
485
+ Dict containing enhancement results
486
+ """
487
+ try:
488
+ start_time = datetime.now()
489
+
490
+ # Step 1: Read existing document
491
+ existing_content = self._read_existing_document(source_path)
492
+
493
+ # Step 2: Generate enhanced content
494
+ ai_result = self._enhance_content_with_ai(
495
+ existing_content,
496
+ enhancement_goals,
497
+ ai_provider or self.config.default_ai_provider,
498
+ )
499
+
500
+ # Step 3: Write enhanced content
501
+ target = target_path or source_path
502
+ write_mode = "overwrite" if target == source_path else "create"
503
+
504
+ if self.config.auto_backup_on_ai_write and target == source_path:
505
+ write_mode = "backup_write"
506
+
507
+ write_result = self.document_writer.write_document(
508
+ target_path=target,
509
+ content=ai_result["enhanced_content"],
510
+ format=existing_content["format"],
511
+ mode=write_mode,
512
+ )
513
+
514
+ result = {
515
+ "source_path": source_path,
516
+ "target_path": target,
517
+ "enhancement_goals": enhancement_goals,
518
+ "preserve_format": preserve_format,
519
+ "original_content": existing_content,
520
+ "ai_result": ai_result,
521
+ "write_result": write_result,
522
+ "processing_metadata": {
523
+ "start_time": start_time.isoformat(),
524
+ "end_time": datetime.now().isoformat(),
525
+ "duration": (datetime.now() - start_time).total_seconds(),
526
+ },
527
+ }
528
+
529
+ return result
530
+
531
+ except Exception as e:
532
+ raise WriteOrchestrationError(f"Document enhancement failed: {str(e)}")
533
+
534
+ def batch_ai_write(
535
+ self,
536
+ write_requests: List[Dict[str, Any]],
537
+ coordination_strategy: str = "parallel",
538
+ max_concurrent: Optional[int] = None,
539
+ ) -> Dict[str, Any]:
540
+ """
541
+ Batch AI write operations with coordination
542
+
543
+ Args:
544
+ write_requests: List of write request dictionaries
545
+ coordination_strategy: Coordination strategy (parallel, sequential, smart)
546
+ max_concurrent: Maximum concurrent operations
547
+
548
+ Returns:
549
+ Dict containing batch processing results
550
+ """
551
+ try:
552
+ start_time = datetime.now()
553
+ batch_id = f"batch_ai_write_{int(start_time.timestamp())}"
554
+ max_concurrent = max_concurrent or self.config.max_concurrent_writes
555
+
556
+ self.logger.info(f"Starting batch AI write {batch_id}: {len(write_requests)} requests")
557
+
558
+ if coordination_strategy == "parallel":
559
+ results = asyncio.run(self._batch_write_parallel(write_requests, max_concurrent))
560
+ elif coordination_strategy == "sequential":
561
+ results = self._batch_write_sequential(write_requests)
562
+ elif coordination_strategy == "smart":
563
+ results = asyncio.run(self._batch_write_smart(write_requests, max_concurrent))
564
+ else:
565
+ raise ValueError(f"Unknown coordination strategy: {coordination_strategy}")
566
+
567
+ batch_result = {
568
+ "batch_id": batch_id,
569
+ "coordination_strategy": coordination_strategy,
570
+ "total_requests": len(write_requests),
571
+ "successful_requests": len([r for r in results if r.get("status") == "success"]),
572
+ "failed_requests": len([r for r in results if r.get("status") == "error"]),
573
+ "results": results,
574
+ "batch_metadata": {
575
+ "start_time": start_time.isoformat(),
576
+ "end_time": datetime.now().isoformat(),
577
+ "duration": (datetime.now() - start_time).total_seconds(),
578
+ },
579
+ }
580
+
581
+ return batch_result
582
+
583
+ except Exception as e:
584
+ raise WriteOrchestrationError(f"Batch AI write failed: {str(e)}")
585
+
586
+ def ai_edit_document(
587
+ self,
588
+ target_path: str,
589
+ edit_operation: AIEditOperation,
590
+ edit_instructions: str,
591
+ ai_provider: Optional[AIProvider] = None,
592
+ preserve_structure: bool = True,
593
+ format_options: Optional[Dict[str, Any]] = None,
594
+ ) -> Dict[str, Any]:
595
+ """
596
+ Perform AI-driven editing operations on documents
597
+
598
+ Args:
599
+ target_path: Target document path
600
+ edit_operation: AI editing operation to perform
601
+ edit_instructions: Specific editing instructions
602
+ ai_provider: AI provider to use
603
+ preserve_structure: Preserve document structure
604
+ format_options: Format-specific options
605
+
606
+ Returns:
607
+ Dict containing editing results
608
+ """
609
+ try:
610
+ start_time = datetime.now()
611
+ operation_id = f"ai_edit_{int(start_time.timestamp())}"
612
+
613
+ self.logger.info(
614
+ f"Starting AI edit operation {operation_id}: {edit_operation} on {target_path}"
615
+ )
616
+
617
+ if not self.document_writer:
618
+ raise WriteOrchestrationError("DocumentWriterTool not available")
619
+
620
+ # Step 1: Read current document content
621
+ current_content = self._read_document_for_editing(target_path)
622
+
623
+ # Step 2: Analyze content for editing
624
+ analysis_result = self._analyze_document_for_editing(
625
+ current_content, edit_operation, edit_instructions
626
+ )
627
+
628
+ # Step 3: Generate editing instructions using AI
629
+ ai_edit_plan = self._generate_ai_edit_plan(
630
+ current_content,
631
+ edit_operation,
632
+ edit_instructions,
633
+ analysis_result,
634
+ ai_provider or self.config.default_ai_provider,
635
+ )
636
+
637
+ # Step 4: Execute editing operations
638
+ edit_results = self._execute_ai_editing_plan(target_path, ai_edit_plan, format_options)
639
+
640
+ # Step 5: Post-process and validate
641
+ validation_result = self._validate_ai_editing_result(
642
+ target_path, current_content, edit_results, preserve_structure
643
+ )
644
+
645
+ result = {
646
+ "operation_id": operation_id,
647
+ "target_path": target_path,
648
+ "edit_operation": edit_operation,
649
+ "edit_instructions": edit_instructions,
650
+ "preserve_structure": preserve_structure,
651
+ "analysis_result": analysis_result,
652
+ "ai_edit_plan": ai_edit_plan,
653
+ "edit_results": edit_results,
654
+ "validation_result": validation_result,
655
+ "processing_metadata": {
656
+ "start_time": start_time.isoformat(),
657
+ "end_time": datetime.now().isoformat(),
658
+ "duration": (datetime.now() - start_time).total_seconds(),
659
+ },
660
+ }
661
+
662
+ self.logger.info(f"AI edit operation {operation_id} completed successfully")
663
+ return result
664
+
665
+ except Exception as e:
666
+ raise WriteOrchestrationError(f"AI edit operation failed: {str(e)}")
667
+
668
+ def smart_format_document(
669
+ self,
670
+ target_path: str,
671
+ format_goals: str,
672
+ target_format: str,
673
+ style_preferences: Optional[Dict[str, Any]] = None,
674
+ ) -> Dict[str, Any]:
675
+ """
676
+ Intelligently format document using AI analysis
677
+
678
+ Args:
679
+ target_path: Target document path
680
+ format_goals: Formatting goals and requirements
681
+ target_format: Target document format
682
+ style_preferences: Style preferences
683
+
684
+ Returns:
685
+ Dict containing formatting results
686
+ """
687
+ try:
688
+ start_time = datetime.now()
689
+
690
+ if not self.document_writer:
691
+ raise WriteOrchestrationError("DocumentWriterTool not available")
692
+
693
+ # Step 1: Analyze document structure
694
+ structure_analysis = self._analyze_document_structure(target_path, target_format)
695
+
696
+ # Step 2: Generate smart formatting plan
697
+ format_plan = self._generate_smart_format_plan(
698
+ structure_analysis,
699
+ format_goals,
700
+ target_format,
701
+ style_preferences,
702
+ )
703
+
704
+ # Step 3: Execute formatting operations
705
+ format_results = self._execute_smart_formatting(target_path, format_plan, target_format)
706
+
707
+ result = {
708
+ "target_path": target_path,
709
+ "format_goals": format_goals,
710
+ "target_format": target_format,
711
+ "structure_analysis": structure_analysis,
712
+ "format_plan": format_plan,
713
+ "format_results": format_results,
714
+ "processing_metadata": {
715
+ "start_time": start_time.isoformat(),
716
+ "end_time": datetime.now().isoformat(),
717
+ "duration": (datetime.now() - start_time).total_seconds(),
718
+ },
719
+ }
720
+
721
+ return result
722
+
723
+ except Exception as e:
724
+ raise WriteOrchestrationError(f"Smart formatting failed: {str(e)}")
725
+
726
+ def analyze_document_content(
727
+ self,
728
+ source_path: str,
729
+ analysis_type: str,
730
+ analysis_params: Optional[Dict[str, Any]] = None,
731
+ ) -> Dict[str, Any]:
732
+ """
733
+ Perform AI-driven content analysis
734
+
735
+ Args:
736
+ source_path: Source document path
737
+ analysis_type: Type of analysis to perform
738
+ analysis_params: Analysis parameters
739
+
740
+ Returns:
741
+ Dict containing analysis results
742
+ """
743
+ try:
744
+ if not self.document_writer:
745
+ raise WriteOrchestrationError("DocumentWriterTool not available")
746
+
747
+ # Read document content
748
+ content = self._read_document_for_editing(source_path)
749
+
750
+ # Perform analysis based on type
751
+ if analysis_type == "structure":
752
+ format_param = analysis_params.get("format", "txt") if analysis_params else "txt"
753
+ result = self._analyze_document_structure(source_path, format_param)
754
+ elif analysis_type == "readability":
755
+ result = self._analyze_readability(content, analysis_params)
756
+ elif analysis_type == "keywords":
757
+ result = self._analyze_keywords(content, analysis_params)
758
+ elif analysis_type == "formatting_issues":
759
+ result = self._analyze_formatting_issues(content, analysis_params)
760
+ elif analysis_type == "content_quality":
761
+ result = self._analyze_content_quality(content, analysis_params)
762
+ else:
763
+ raise ValueError(f"Unsupported analysis type: {analysis_type}")
764
+
765
+ return {
766
+ "source_path": source_path,
767
+ "analysis_type": analysis_type,
768
+ "analysis_result": result,
769
+ "content_metadata": {
770
+ "content_length": len(content),
771
+ "analysis_timestamp": datetime.now().isoformat(),
772
+ },
773
+ }
774
+
775
+ except Exception as e:
776
+ raise WriteOrchestrationError(f"Document analysis failed: {str(e)}")
777
+
778
+ # Helper methods for AI editing operations
779
+ def _read_document_for_editing(self, file_path: str) -> str:
780
+ """Read document content for editing operations"""
781
+ try:
782
+ with open(file_path, "r", encoding="utf-8") as f:
783
+ return f.read()
784
+ except Exception as e:
785
+ raise WriteOrchestrationError(f"Cannot read document {file_path}: {str(e)}")
786
+
787
+ def _analyze_document_for_editing(
788
+ self, content: str, operation: AIEditOperation, instructions: str
789
+ ) -> Dict[str, Any]:
790
+ """Analyze document content for editing operations"""
791
+ analysis = {
792
+ "content_length": len(content),
793
+ "line_count": len(content.split("\n")),
794
+ "word_count": len(content.split()),
795
+ "operation": operation,
796
+ "instructions": instructions,
797
+ }
798
+
799
+ # Specific analysis based on operation type
800
+ if operation == AIEditOperation.SMART_FORMAT:
801
+ analysis["formatting_issues"] = self._detect_formatting_issues(content)
802
+ elif operation == AIEditOperation.AUTO_BOLD_KEYWORDS:
803
+ analysis["potential_keywords"] = self._extract_potential_keywords(content)
804
+ elif operation == AIEditOperation.INTELLIGENT_HIGHLIGHT:
805
+ analysis["important_sections"] = self._identify_important_sections(content)
806
+ elif operation == AIEditOperation.CONTENT_RESTRUCTURE:
807
+ analysis["structure_analysis"] = self._analyze_content_structure(content)
808
+
809
+ return analysis
810
+
811
+ def _generate_ai_edit_plan(
812
+ self,
813
+ content: str,
814
+ operation: AIEditOperation,
815
+ instructions: str,
816
+ analysis: Dict[str, Any],
817
+ ai_provider: AIProvider,
818
+ ) -> Dict[str, Any]:
819
+ """Generate AI editing plan"""
820
+ try:
821
+ if not self.aiecs_client:
822
+ # Fallback to rule-based editing plan
823
+ return self._generate_fallback_edit_plan(content, operation, instructions, analysis)
824
+
825
+ # Prepare AI prompt for editing plan
826
+ system_prompt = f"""You are an expert document editor. Create a detailed editing plan based on the operation type and user instructions.
827
+
828
+ Operation: {operation}
829
+ Analysis: {analysis}
830
+
831
+ Provide a structured editing plan with specific actions, positions, and formatting details."""
832
+
833
+ user_prompt = f"""Content to edit:
834
+ {content[:2000]}...
835
+
836
+ Instructions: {instructions}
837
+
838
+ Please provide a detailed editing plan with:
839
+ 1. Specific edit operations
840
+ 2. Text positions (line numbers, character offsets)
841
+ 3. Format options
842
+ 4. Expected outcomes"""
843
+
844
+ # Generate editing plan using AI
845
+ ai_response = self._call_ai_provider(
846
+ ai_provider,
847
+ system_prompt,
848
+ user_prompt,
849
+ {"max_tokens": 2000, "temperature": 0.3},
850
+ )
851
+
852
+ # Parse AI response into structured plan
853
+ edit_plan = self._parse_ai_edit_response(ai_response, operation)
854
+
855
+ return edit_plan
856
+
857
+ except Exception as e:
858
+ self.logger.warning(f"AI edit plan generation failed: {e}, using fallback")
859
+ return self._generate_fallback_edit_plan(content, operation, instructions, analysis)
860
+
861
+ def _execute_ai_editing_plan(
862
+ self,
863
+ target_path: str,
864
+ edit_plan: Dict[str, Any],
865
+ format_options: Optional[Dict[str, Any]],
866
+ ) -> List[Dict[str, Any]]:
867
+ """Execute the AI-generated editing plan"""
868
+ edit_results = []
869
+
870
+ try:
871
+ for edit_action in edit_plan.get("edit_actions", []):
872
+ operation_type = edit_action.get("operation")
873
+
874
+ if operation_type in [
875
+ "bold",
876
+ "italic",
877
+ "underline",
878
+ "strikethrough",
879
+ "highlight",
880
+ ]:
881
+ # Text formatting operations
882
+ result = self.document_writer.edit_document(
883
+ target_path=target_path,
884
+ operation=operation_type,
885
+ selection=edit_action.get("selection"),
886
+ format_options=format_options or edit_action.get("format_options", {}),
887
+ )
888
+ elif operation_type == "find_replace":
889
+ # Find and replace operations
890
+ result = self.document_writer.find_replace(
891
+ target_path=target_path,
892
+ find_text=edit_action.get("find_text"),
893
+ replace_text=edit_action.get("replace_text"),
894
+ replace_all=edit_action.get("replace_all", False),
895
+ case_sensitive=edit_action.get("case_sensitive", True),
896
+ regex_mode=edit_action.get("regex_mode", False),
897
+ )
898
+ elif operation_type == "format_text":
899
+ # Format specific text
900
+ result = self.document_writer.format_text(
901
+ target_path=target_path,
902
+ text_to_format=edit_action.get("text_to_format"),
903
+ format_type=edit_action.get("format_type"),
904
+ format_options=format_options or edit_action.get("format_options", {}),
905
+ )
906
+ else:
907
+ # General edit operations
908
+ result = self.document_writer.edit_document(
909
+ target_path=target_path,
910
+ operation=edit_action.get("operation"),
911
+ content=edit_action.get("content"),
912
+ position=edit_action.get("position"),
913
+ selection=edit_action.get("selection"),
914
+ format_options=format_options or edit_action.get("format_options", {}),
915
+ )
916
+
917
+ edit_results.append({"action": edit_action, "result": result, "success": True})
918
+
919
+ except Exception as e:
920
+ edit_results.append({"action": edit_action, "error": str(e), "success": False})
921
+
922
+ return edit_results
923
+
924
+ def _validate_ai_editing_result(
925
+ self,
926
+ target_path: str,
927
+ original_content: str,
928
+ edit_results: List[Dict[str, Any]],
929
+ preserve_structure: bool,
930
+ ) -> Dict[str, Any]:
931
+ """Validate AI editing results"""
932
+ try:
933
+ # Read edited content
934
+ edited_content = self._read_document_for_editing(target_path)
935
+
936
+ validation = {
937
+ "original_length": len(original_content),
938
+ "edited_length": len(edited_content),
939
+ "successful_operations": sum(1 for r in edit_results if r.get("success")),
940
+ "failed_operations": sum(1 for r in edit_results if not r.get("success")),
941
+ "content_changed": original_content != edited_content,
942
+ "structure_preserved": True, # TODO: Implement structure validation
943
+ }
944
+
945
+ if preserve_structure:
946
+ validation["structure_check"] = self._check_structure_preservation(
947
+ original_content, edited_content
948
+ )
949
+
950
+ return validation
951
+
952
+ except Exception as e:
953
+ return {"validation_error": str(e)}
954
+
955
+ # Additional helper methods for specific operations
956
+ def _generate_fallback_edit_plan(
957
+ self,
958
+ content: str,
959
+ operation: AIEditOperation,
960
+ instructions: str,
961
+ analysis: Dict[str, Any],
962
+ ) -> Dict[str, Any]:
963
+ """Generate fallback editing plan when AI is not available"""
964
+ plan = {"edit_actions": []}
965
+
966
+ if operation == AIEditOperation.AUTO_BOLD_KEYWORDS:
967
+ # Auto-bold common keywords
968
+ keywords = ["重要", "关键", "注意", "警告", "错误", "成功"]
969
+ for keyword in keywords:
970
+ if keyword in content:
971
+ plan["edit_actions"].append(
972
+ {
973
+ "operation": "format_text",
974
+ "text_to_format": keyword,
975
+ "format_type": "bold",
976
+ "format_options": {"format_type": "markdown"},
977
+ }
978
+ )
979
+
980
+ elif operation == AIEditOperation.SMART_FORMAT:
981
+ # Basic formatting improvements
982
+ plan["edit_actions"].append(
983
+ {
984
+ "operation": "find_replace",
985
+ "find_text": " ",
986
+ "replace_text": " ",
987
+ "replace_all": True,
988
+ "description": "Remove double spaces",
989
+ }
990
+ )
991
+
992
+ return plan
993
+
994
+ def _detect_formatting_issues(self, content: str) -> List[str]:
995
+ """Detect common formatting issues"""
996
+ issues = []
997
+
998
+ if " " in content:
999
+ issues.append("Multiple consecutive spaces")
1000
+ if "\n\n\n" in content:
1001
+ issues.append("Multiple consecutive line breaks")
1002
+ if content.count("**") % 2 != 0:
1003
+ issues.append("Unmatched bold markdown markers")
1004
+
1005
+ return issues
1006
+
1007
+ def _extract_potential_keywords(self, content: str) -> List[str]:
1008
+ """Extract potential keywords for bolding"""
1009
+ # Simple keyword extraction - could be enhanced with NLP
1010
+ import re
1011
+
1012
+ words = re.findall(r"\b[A-Z][a-z]+\b", content) # Capitalized words
1013
+ return list(set(words))[:10] # Top 10 unique words
1014
+
1015
+ def _identify_important_sections(self, content: str) -> List[Dict[str, Any]]:
1016
+ """Identify sections that might need highlighting"""
1017
+ sections = []
1018
+ lines = content.split("\n")
1019
+
1020
+ for i, line in enumerate(lines):
1021
+ if any(keyword in line.lower() for keyword in ["重要", "注意", "警告", "关键"]):
1022
+ sections.append(
1023
+ {
1024
+ "line": i,
1025
+ "content": line,
1026
+ "reason": "Contains important keywords",
1027
+ }
1028
+ )
1029
+
1030
+ return sections
1031
+
1032
+ def _analyze_content_structure(self, content: str) -> Dict[str, Any]:
1033
+ """Analyze content structure"""
1034
+ lines = content.split("\n")
1035
+ return {
1036
+ "total_lines": len(lines),
1037
+ "empty_lines": sum(1 for line in lines if not line.strip()),
1038
+ "header_lines": sum(1 for line in lines if line.startswith("#")),
1039
+ "list_items": sum(1 for line in lines if line.strip().startswith(("-", "*", "+"))),
1040
+ "paragraphs": len(
1041
+ [line for line in lines if line.strip() and not line.startswith("#")]
1042
+ ),
1043
+ }
1044
+
1045
+ def _parse_ai_edit_response(
1046
+ self, ai_response: str, operation: AIEditOperation
1047
+ ) -> Dict[str, Any]:
1048
+ """Parse AI response into structured editing plan"""
1049
+ # This is a simplified parser - could be enhanced with more
1050
+ # sophisticated parsing
1051
+ plan = {"edit_actions": []}
1052
+
1053
+ # Try to extract structured actions from AI response
1054
+ # For now, return a basic plan
1055
+ plan["ai_response"] = ai_response
1056
+ plan["operation"] = operation
1057
+
1058
+ return plan
1059
+
1060
+ def _check_structure_preservation(self, original: str, edited: str) -> Dict[str, Any]:
1061
+ """Check if document structure is preserved after editing"""
1062
+ original_structure = self._analyze_content_structure(original)
1063
+ edited_structure = self._analyze_content_structure(edited)
1064
+
1065
+ return {
1066
+ "headers_preserved": original_structure["header_lines"]
1067
+ == edited_structure["header_lines"],
1068
+ "structure_similarity": self._calculate_structure_similarity(
1069
+ original_structure, edited_structure
1070
+ ),
1071
+ }
1072
+
1073
+ def _calculate_structure_similarity(self, struct1: Dict, struct2: Dict) -> float:
1074
+ """Calculate similarity between two document structures"""
1075
+ # Simple similarity calculation
1076
+ if struct1["total_lines"] == 0:
1077
+ return 1.0 if struct2["total_lines"] == 0 else 0.0
1078
+
1079
+ similarity = 1.0 - abs(struct1["total_lines"] - struct2["total_lines"]) / max(
1080
+ struct1["total_lines"], struct2["total_lines"]
1081
+ )
1082
+ return max(0.0, similarity)
1083
+
1084
+ def _analyze_document_structure(self, file_path: str, format_type: str) -> Dict[str, Any]:
1085
+ """Analyze document structure for formatting"""
1086
+ content = self._read_document_for_editing(file_path)
1087
+ return self._analyze_content_structure(content)
1088
+
1089
+ def _generate_smart_format_plan(
1090
+ self,
1091
+ structure: Dict[str, Any],
1092
+ goals: str,
1093
+ target_format: str,
1094
+ style_prefs: Optional[Dict[str, Any]],
1095
+ ) -> Dict[str, Any]:
1096
+ """Generate smart formatting plan"""
1097
+ return {
1098
+ "format_actions": [],
1099
+ "structure_analysis": structure,
1100
+ "goals": goals,
1101
+ "target_format": target_format,
1102
+ "style_preferences": style_prefs or {},
1103
+ }
1104
+
1105
+ def _execute_smart_formatting(
1106
+ self, target_path: str, plan: Dict[str, Any], target_format: str
1107
+ ) -> Dict[str, Any]:
1108
+ """Execute smart formatting plan"""
1109
+ return {
1110
+ "target_path": target_path,
1111
+ "plan_executed": plan,
1112
+ "target_format": target_format,
1113
+ "formatting_completed": True,
1114
+ }
1115
+
1116
+ def _analyze_readability(
1117
+ self, content: str, params: Optional[Dict[str, Any]]
1118
+ ) -> Dict[str, Any]:
1119
+ """Analyze content readability"""
1120
+ words = content.split()
1121
+ sentences = content.split(".")
1122
+
1123
+ return {
1124
+ "word_count": len(words),
1125
+ "sentence_count": len(sentences),
1126
+ "avg_words_per_sentence": len(words) / max(len(sentences), 1),
1127
+ "readability_score": "good", # Simplified
1128
+ }
1129
+
1130
+ def _analyze_keywords(self, content: str, params: Optional[Dict[str, Any]]) -> Dict[str, Any]:
1131
+ """Analyze content keywords"""
1132
+ words = content.lower().split()
1133
+ word_freq = {}
1134
+ for word in words:
1135
+ word_freq[word] = word_freq.get(word, 0) + 1
1136
+
1137
+ # Get top keywords
1138
+ top_keywords = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)[:10]
1139
+
1140
+ return {
1141
+ "total_words": len(words),
1142
+ "unique_words": len(word_freq),
1143
+ "top_keywords": top_keywords,
1144
+ }
1145
+
1146
+ def _analyze_formatting_issues(
1147
+ self, content: str, params: Optional[Dict[str, Any]]
1148
+ ) -> Dict[str, Any]:
1149
+ """Analyze formatting issues in content"""
1150
+ issues = self._detect_formatting_issues(content)
1151
+
1152
+ return {
1153
+ "issues_found": len(issues),
1154
+ "issue_list": issues,
1155
+ "content_length": len(content),
1156
+ }
1157
+
1158
+ def _analyze_content_quality(
1159
+ self, content: str, params: Optional[Dict[str, Any]]
1160
+ ) -> Dict[str, Any]:
1161
+ """Analyze overall content quality"""
1162
+ return {
1163
+ "content_length": len(content),
1164
+ "structure_score": 0.8, # Simplified scoring
1165
+ "readability_score": 0.7,
1166
+ "formatting_score": 0.9,
1167
+ "overall_quality": 0.8,
1168
+ }
1169
+
1170
+ def create_rich_document(
1171
+ self,
1172
+ document_template: str,
1173
+ content_plan: Dict[str, Any],
1174
+ layout_config: Optional[Dict[str, Any]] = None,
1175
+ output_path: Optional[str] = None,
1176
+ ai_assistance: bool = True,
1177
+ ) -> Dict[str, Any]:
1178
+ """
1179
+ Create rich document with comprehensive content and layout
1180
+
1181
+ Args:
1182
+ document_template: Document template type
1183
+ content_plan: Content planning configuration
1184
+ layout_config: Layout configuration
1185
+ output_path: Custom output path
1186
+ ai_assistance: Use AI assistance for content generation
1187
+
1188
+ Returns:
1189
+ Dict containing rich document creation results
1190
+ """
1191
+ try:
1192
+ start_time = datetime.now()
1193
+ operation_id = f"create_rich_{int(start_time.timestamp())}"
1194
+
1195
+ self.logger.info(f"Starting rich document creation {operation_id}")
1196
+
1197
+ # Check tool availability
1198
+ creator = self.creation_tools.get("creator")
1199
+ layout_tool = self.creation_tools.get("layout")
1200
+ content_tool = self.creation_tools.get("content")
1201
+
1202
+ if not creator:
1203
+ raise WriteOrchestrationError("DocumentCreatorTool not available")
1204
+
1205
+ # Step 1: Create document from template
1206
+ document_metadata = content_plan.get("metadata", {})
1207
+ document_format_str = content_plan.get("format", "markdown")
1208
+
1209
+ # Convert string to DocumentFormat enum
1210
+ try:
1211
+ document_format = self.DocumentFormat(document_format_str)
1212
+ except (ValueError, AttributeError):
1213
+ # Fallback if DocumentFormat not available
1214
+ from aiecs.tools.docs.document_creator_tool import (
1215
+ DocumentFormat,
1216
+ )
1217
+
1218
+ document_format = DocumentFormat.MARKDOWN
1219
+
1220
+ # Get enum classes
1221
+ try:
1222
+ DocumentType = self.DocumentType
1223
+ TemplateType = self.TemplateType
1224
+ except AttributeError:
1225
+ from aiecs.tools.docs.document_creator_tool import (
1226
+ DocumentType,
1227
+ TemplateType,
1228
+ )
1229
+
1230
+ # Parse document_type with fallback
1231
+ doc_type_str = content_plan.get("document_type", "custom")
1232
+ try:
1233
+ doc_type = DocumentType(doc_type_str)
1234
+ except ValueError:
1235
+ # Try to find a matching type or use a sensible default
1236
+ if "technical" in doc_type_str.lower():
1237
+ doc_type = DocumentType.TECHNICAL
1238
+ elif "report" in doc_type_str.lower():
1239
+ doc_type = DocumentType.REPORT
1240
+ elif "article" in doc_type_str.lower():
1241
+ doc_type = DocumentType.ARTICLE
1242
+ else:
1243
+ doc_type = DocumentType.TECHNICAL # Default fallback
1244
+ self.logger.warning(
1245
+ f"Unknown document type '{doc_type_str}', using {doc_type.value}"
1246
+ )
1247
+
1248
+ # Parse template_type with fallback
1249
+ try:
1250
+ tmpl_type = TemplateType(document_template)
1251
+ except ValueError:
1252
+ # Default to basic template
1253
+ tmpl_type = TemplateType.BASIC
1254
+ self.logger.warning(f"Unknown template type '{document_template}', using basic")
1255
+
1256
+ creation_result = creator.create_document(
1257
+ document_type=doc_type,
1258
+ template_type=tmpl_type,
1259
+ output_format=document_format,
1260
+ metadata=document_metadata,
1261
+ output_path=output_path,
1262
+ )
1263
+
1264
+ document_path = creation_result["output_path"]
1265
+
1266
+ # Step 2: Setup document structure
1267
+ if content_plan.get("sections"):
1268
+ creator.setup_document_structure(
1269
+ document_path=document_path,
1270
+ sections=content_plan["sections"],
1271
+ generate_toc=content_plan.get("generate_toc", True),
1272
+ numbering_style=content_plan.get("numbering_style"),
1273
+ )
1274
+
1275
+ # Step 3: Apply layout configuration
1276
+ if layout_tool and layout_config:
1277
+ layout_tool.set_page_layout(document_path=document_path, **layout_config)
1278
+
1279
+ # Setup headers/footers if specified
1280
+ if layout_config.get("headers_footers"):
1281
+ layout_tool.setup_headers_footers(
1282
+ document_path=document_path,
1283
+ **layout_config["headers_footers"],
1284
+ )
1285
+
1286
+ # Step 4: Generate and insert content with AI assistance
1287
+ content_results = []
1288
+ if ai_assistance and content_plan.get("content_items"):
1289
+ content_results = self._generate_and_insert_content_items(
1290
+ document_path, content_plan["content_items"]
1291
+ )
1292
+
1293
+ # Step 5: Insert complex content (charts, tables, images)
1294
+ insertion_results = []
1295
+ if content_tool and content_plan.get("insertions"):
1296
+ insertion_results = self._batch_insert_complex_content(
1297
+ document_path, content_plan["insertions"], content_tool
1298
+ )
1299
+
1300
+ # Step 6: Final optimization
1301
+ if ai_assistance:
1302
+ self._optimize_rich_document(
1303
+ document_path, content_plan.get("optimization_goals", [])
1304
+ )
1305
+
1306
+ result = {
1307
+ "operation_id": operation_id,
1308
+ "document_path": document_path,
1309
+ "document_template": document_template,
1310
+ "content_plan": content_plan,
1311
+ "layout_config": layout_config,
1312
+ "creation_result": creation_result,
1313
+ "content_results": content_results,
1314
+ "insertion_results": insertion_results,
1315
+ "ai_assistance_used": ai_assistance,
1316
+ "processing_metadata": {
1317
+ "start_time": start_time.isoformat(),
1318
+ "end_time": datetime.now().isoformat(),
1319
+ "duration": (datetime.now() - start_time).total_seconds(),
1320
+ },
1321
+ }
1322
+
1323
+ self.logger.info(f"Rich document creation {operation_id} completed successfully")
1324
+ return result
1325
+
1326
+ except Exception as e:
1327
+ raise WriteOrchestrationError(f"Rich document creation failed: {str(e)}")
1328
+
1329
+ def generate_document_with_charts(
1330
+ self,
1331
+ requirements: str,
1332
+ data_sources: List[Dict[str, Any]],
1333
+ document_type: str,
1334
+ include_analysis: bool = True,
1335
+ chart_preferences: Optional[Dict[str, Any]] = None,
1336
+ ) -> Dict[str, Any]:
1337
+ """
1338
+ Generate document with AI-driven charts and data visualization
1339
+
1340
+ Args:
1341
+ requirements: Document requirements and specifications
1342
+ data_sources: Data sources for charts and tables
1343
+ document_type: Type of document to generate
1344
+ include_analysis: Include data analysis sections
1345
+ chart_preferences: Chart style preferences
1346
+
1347
+ Returns:
1348
+ Dict containing document generation results
1349
+ """
1350
+ try:
1351
+ start_time = datetime.now()
1352
+ operation_id = f"gen_charts_{int(start_time.timestamp())}"
1353
+
1354
+ self.logger.info(f"Starting document generation with charts {operation_id}")
1355
+
1356
+ # Step 1: Analyze data sources and generate content plan
1357
+ content_plan = self._analyze_data_and_create_plan(
1358
+ data_sources, requirements, document_type, include_analysis
1359
+ )
1360
+
1361
+ # Step 2: Generate charts from data sources
1362
+ chart_results = self._generate_charts_from_data(data_sources, chart_preferences)
1363
+
1364
+ # Step 3: Create document with integrated charts
1365
+ rich_doc_result = self.create_rich_document(
1366
+ document_template=self._select_template_for_data_document(document_type),
1367
+ content_plan=content_plan,
1368
+ ai_assistance=True,
1369
+ )
1370
+
1371
+ # Step 4: Insert generated charts
1372
+ chart_insertion_results = self._insert_generated_charts(
1373
+ rich_doc_result["document_path"], chart_results, content_plan
1374
+ )
1375
+
1376
+ # Step 5: Generate AI analysis content
1377
+ if include_analysis:
1378
+ self._generate_ai_analysis_content(
1379
+ rich_doc_result["document_path"],
1380
+ data_sources,
1381
+ chart_results,
1382
+ )
1383
+
1384
+ result = {
1385
+ "operation_id": operation_id,
1386
+ "document_path": rich_doc_result["document_path"],
1387
+ "requirements": requirements,
1388
+ "data_sources": data_sources,
1389
+ "document_type": document_type,
1390
+ "content_plan": content_plan,
1391
+ "chart_results": chart_results,
1392
+ "rich_doc_result": rich_doc_result,
1393
+ "chart_insertion_results": chart_insertion_results,
1394
+ "include_analysis": include_analysis,
1395
+ "processing_metadata": {
1396
+ "start_time": start_time.isoformat(),
1397
+ "end_time": datetime.now().isoformat(),
1398
+ "duration": (datetime.now() - start_time).total_seconds(),
1399
+ },
1400
+ }
1401
+
1402
+ self.logger.info(
1403
+ f"Document with charts generation {operation_id} completed successfully"
1404
+ )
1405
+ return result
1406
+
1407
+ except Exception as e:
1408
+ raise WriteOrchestrationError(f"Document with charts generation failed: {str(e)}")
1409
+
1410
+ def optimize_document_layout(
1411
+ self,
1412
+ document_path: str,
1413
+ optimization_goals: List[str],
1414
+ preserve_content: bool = True,
1415
+ layout_style: Optional[str] = None,
1416
+ ) -> Dict[str, Any]:
1417
+ """
1418
+ Optimize document layout using AI analysis
1419
+
1420
+ Args:
1421
+ document_path: Path to document to optimize
1422
+ optimization_goals: Layout optimization goals
1423
+ preserve_content: Preserve existing content
1424
+ layout_style: Target layout style
1425
+
1426
+ Returns:
1427
+ Dict containing layout optimization results
1428
+ """
1429
+ try:
1430
+ start_time = datetime.now()
1431
+ operation_id = f"optimize_layout_{int(start_time.timestamp())}"
1432
+
1433
+ self.logger.info(f"Starting layout optimization {operation_id} for: {document_path}")
1434
+
1435
+ layout_tool = self.creation_tools.get("layout")
1436
+ if not layout_tool:
1437
+ raise WriteOrchestrationError("DocumentLayoutTool not available")
1438
+
1439
+ # Step 1: Analyze current document content
1440
+ content_analysis = self.analyze_document_content(
1441
+ source_path=document_path, analysis_type="structure"
1442
+ )
1443
+
1444
+ # Step 2: Generate optimization plan
1445
+ optimization_plan = self._generate_layout_optimization_plan(
1446
+ document_path,
1447
+ content_analysis,
1448
+ optimization_goals,
1449
+ layout_style,
1450
+ )
1451
+
1452
+ # Step 3: Apply optimizations
1453
+ optimization_results = layout_tool.optimize_layout_for_content(
1454
+ document_path=document_path,
1455
+ content_analysis=content_analysis["analysis_result"],
1456
+ optimization_goals=optimization_goals,
1457
+ )
1458
+
1459
+ # Step 4: Validate optimization results
1460
+ if preserve_content:
1461
+ self._validate_content_preservation(document_path, content_analysis)
1462
+
1463
+ result = {
1464
+ "operation_id": operation_id,
1465
+ "document_path": document_path,
1466
+ "optimization_goals": optimization_goals,
1467
+ "layout_style": layout_style,
1468
+ "content_analysis": content_analysis,
1469
+ "optimization_plan": optimization_plan,
1470
+ "optimization_results": optimization_results,
1471
+ "preserve_content": preserve_content,
1472
+ "processing_metadata": {
1473
+ "start_time": start_time.isoformat(),
1474
+ "end_time": datetime.now().isoformat(),
1475
+ "duration": (datetime.now() - start_time).total_seconds(),
1476
+ },
1477
+ }
1478
+
1479
+ self.logger.info(f"Layout optimization {operation_id} completed successfully")
1480
+ return result
1481
+
1482
+ except Exception as e:
1483
+ raise WriteOrchestrationError(f"Layout optimization failed: {str(e)}")
1484
+
1485
+ def batch_content_insertion(
1486
+ self,
1487
+ document_path: str,
1488
+ content_plan: List[Dict[str, Any]],
1489
+ insertion_strategy: str = "sequential",
1490
+ ai_optimization: bool = True,
1491
+ ) -> Dict[str, Any]:
1492
+ """
1493
+ Batch insertion of multiple content types with AI coordination
1494
+
1495
+ Args:
1496
+ document_path: Target document path
1497
+ content_plan: Content insertion plan
1498
+ insertion_strategy: Insertion strategy (sequential, parallel, optimized)
1499
+ ai_optimization: Use AI for content optimization
1500
+
1501
+ Returns:
1502
+ Dict containing batch insertion results
1503
+ """
1504
+ try:
1505
+ start_time = datetime.now()
1506
+ operation_id = f"batch_insert_{int(start_time.timestamp())}"
1507
+
1508
+ self.logger.info(
1509
+ f"Starting batch content insertion {operation_id} for: {document_path}"
1510
+ )
1511
+
1512
+ content_tool = self.creation_tools.get("content")
1513
+ if not content_tool:
1514
+ raise WriteOrchestrationError("ContentInsertionTool not available")
1515
+
1516
+ # Step 1: Optimize insertion order if AI optimization is enabled
1517
+ if ai_optimization:
1518
+ optimized_plan = self._optimize_content_insertion_plan(document_path, content_plan)
1519
+ else:
1520
+ optimized_plan = content_plan
1521
+
1522
+ # Step 2: Execute insertions based on strategy
1523
+ if insertion_strategy == "sequential":
1524
+ insertion_results = self._execute_sequential_insertions(
1525
+ document_path, optimized_plan, content_tool
1526
+ )
1527
+ elif insertion_strategy == "parallel":
1528
+ insertion_results = self._execute_parallel_insertions(
1529
+ document_path, optimized_plan, content_tool
1530
+ )
1531
+ else: # optimized
1532
+ insertion_results = self._execute_optimized_insertions(
1533
+ document_path, optimized_plan, content_tool
1534
+ )
1535
+
1536
+ # Step 3: Post-insertion optimization
1537
+ if ai_optimization:
1538
+ self._post_insertion_optimization(document_path, insertion_results)
1539
+
1540
+ result = {
1541
+ "operation_id": operation_id,
1542
+ "document_path": document_path,
1543
+ "content_plan": content_plan,
1544
+ "optimized_plan": optimized_plan,
1545
+ "insertion_strategy": insertion_strategy,
1546
+ "ai_optimization": ai_optimization,
1547
+ "insertion_results": insertion_results,
1548
+ "processing_metadata": {
1549
+ "start_time": start_time.isoformat(),
1550
+ "end_time": datetime.now().isoformat(),
1551
+ "duration": (datetime.now() - start_time).total_seconds(),
1552
+ },
1553
+ }
1554
+
1555
+ self.logger.info(f"Batch content insertion {operation_id} completed successfully")
1556
+ return result
1557
+
1558
+ except Exception as e:
1559
+ raise WriteOrchestrationError(f"Batch content insertion failed: {str(e)}")
1560
+
1561
+ # Helper methods for new functionality
1562
+ def _generate_and_insert_content_items(
1563
+ self, document_path: str, content_items: List[Dict[str, Any]]
1564
+ ) -> List[Dict[str, Any]]:
1565
+ """Generate and insert content items with AI assistance"""
1566
+ results = []
1567
+
1568
+ for item in content_items:
1569
+ try:
1570
+ # Generate content based on type
1571
+ if item.get("type") == "ai_generated":
1572
+ generated_content = self._generate_content_with_ai(
1573
+ item.get("requirements", ""),
1574
+ ContentGenerationMode.GENERATE,
1575
+ "markdown",
1576
+ self.config.default_ai_provider,
1577
+ item.get("generation_params", {}),
1578
+ )
1579
+
1580
+ # Insert generated content
1581
+ if self.document_writer:
1582
+ write_result = self.document_writer.write_document(
1583
+ target_path=document_path,
1584
+ content=generated_content["generated_content"],
1585
+ format="markdown",
1586
+ mode="append",
1587
+ )
1588
+
1589
+ results.append(
1590
+ {
1591
+ "item": item,
1592
+ "generated_content": generated_content,
1593
+ "write_result": write_result,
1594
+ "success": True,
1595
+ }
1596
+ )
1597
+
1598
+ except Exception as e:
1599
+ results.append({"item": item, "error": str(e), "success": False})
1600
+ self.logger.warning(f"Failed to generate/insert content item: {e}")
1601
+
1602
+ return results
1603
+
1604
+ def _batch_insert_complex_content(
1605
+ self,
1606
+ document_path: str,
1607
+ insertions: List[Dict[str, Any]],
1608
+ content_tool,
1609
+ ) -> List[Dict[str, Any]]:
1610
+ """Batch insert complex content using ContentInsertionTool"""
1611
+ try:
1612
+ # Use the content tool's batch insertion capability
1613
+ return content_tool.batch_insert_content(
1614
+ document_path=document_path, content_items=insertions
1615
+ )
1616
+ except Exception as e:
1617
+ self.logger.warning(f"Batch insertion failed: {e}")
1618
+ return []
1619
+
1620
+ def _optimize_rich_document(
1621
+ self, document_path: str, optimization_goals: List[str]
1622
+ ) -> Dict[str, Any]:
1623
+ """Optimize rich document based on goals"""
1624
+ try:
1625
+ if "layout" in self.creation_tools:
1626
+ return self.creation_tools["layout"].optimize_layout_for_content(
1627
+ document_path=document_path,
1628
+ content_analysis={"content_length": 0}, # Simplified
1629
+ optimization_goals=optimization_goals,
1630
+ )
1631
+ except Exception as e:
1632
+ self.logger.warning(f"Document optimization failed: {e}")
1633
+
1634
+ return {"optimization_applied": False}
1635
+
1636
+ def _analyze_data_and_create_plan(
1637
+ self,
1638
+ data_sources: List[Dict[str, Any]],
1639
+ requirements: str,
1640
+ document_type: str,
1641
+ include_analysis: bool,
1642
+ ) -> Dict[str, Any]:
1643
+ """Analyze data sources and create content plan"""
1644
+ plan = {
1645
+ "document_type": document_type,
1646
+ "format": "markdown",
1647
+ "metadata": {
1648
+ "title": f"{document_type.title()} Report",
1649
+ "author": "AI Document Generator",
1650
+ "date": datetime.now().strftime("%Y-%m-%d"),
1651
+ },
1652
+ "sections": [
1653
+ {"title": "Executive Summary", "level": 2, "required": True},
1654
+ {"title": "Data Overview", "level": 2, "required": True},
1655
+ {"title": "Visualizations", "level": 2, "required": True},
1656
+ ],
1657
+ "generate_toc": True,
1658
+ "insertions": [],
1659
+ }
1660
+
1661
+ if include_analysis:
1662
+ plan["sections"].append({"title": "Analysis", "level": 2, "required": True})
1663
+ plan["sections"].append({"title": "Insights", "level": 2, "required": True})
1664
+
1665
+ # Add chart insertions for each data source
1666
+ for i, data_source in enumerate(data_sources):
1667
+ plan["insertions"].append(
1668
+ {
1669
+ "content_type": "chart",
1670
+ "chart_data": data_source.get("data", {}),
1671
+ "chart_type": data_source.get("chart_type", "bar"),
1672
+ "position": {"marker": f"<!-- CHART_{i+1} -->"},
1673
+ "caption": f"Chart {i+1}: {data_source.get('title', 'Data Visualization')}",
1674
+ }
1675
+ )
1676
+
1677
+ return plan
1678
+
1679
+ def _generate_charts_from_data(
1680
+ self,
1681
+ data_sources: List[Dict[str, Any]],
1682
+ preferences: Optional[Dict[str, Any]],
1683
+ ) -> List[Dict[str, Any]]:
1684
+ """Generate charts from data sources"""
1685
+ results = []
1686
+
1687
+ if "content" not in self.creation_tools:
1688
+ return results
1689
+
1690
+ content_tool = self.creation_tools["content"]
1691
+
1692
+ for data_source in data_sources:
1693
+ try:
1694
+ chart_result = content_tool._generate_chart(
1695
+ chart_data=data_source.get("data", {}),
1696
+ chart_type=data_source.get("chart_type", "bar"),
1697
+ config=preferences,
1698
+ )
1699
+ results.append(
1700
+ {
1701
+ "data_source": data_source,
1702
+ "chart_result": chart_result,
1703
+ "success": True,
1704
+ }
1705
+ )
1706
+ except Exception as e:
1707
+ results.append(
1708
+ {
1709
+ "data_source": data_source,
1710
+ "error": str(e),
1711
+ "success": False,
1712
+ }
1713
+ )
1714
+
1715
+ return results
1716
+
1717
+ def _select_template_for_data_document(self, document_type: str) -> str:
1718
+ """Select appropriate template for data document"""
1719
+ type_template_map = {
1720
+ "report": "business_report",
1721
+ "analysis": "technical_doc",
1722
+ "presentation": "presentation",
1723
+ "academic": "academic_paper",
1724
+ }
1725
+ return type_template_map.get(document_type, "business_report")
1726
+
1727
+ def _insert_generated_charts(
1728
+ self,
1729
+ document_path: str,
1730
+ chart_results: List[Dict[str, Any]],
1731
+ content_plan: Dict[str, Any],
1732
+ ) -> List[Dict[str, Any]]:
1733
+ """Insert generated charts into document"""
1734
+ results = []
1735
+
1736
+ if "content" not in self.creation_tools:
1737
+ return results
1738
+
1739
+ content_tool = self.creation_tools["content"]
1740
+
1741
+ for i, chart_result in enumerate(chart_results):
1742
+ if chart_result.get("success"):
1743
+ try:
1744
+ insertion_result = content_tool.insert_chart(
1745
+ document_path=document_path,
1746
+ chart_data=chart_result["data_source"].get("data", {}),
1747
+ chart_type=chart_result["data_source"].get("chart_type", "bar"),
1748
+ position={"marker": f"<!-- CHART_{i+1} -->"},
1749
+ caption=f"Chart {i+1}: {chart_result['data_source'].get('title', 'Data Visualization')}",
1750
+ )
1751
+ results.append(insertion_result)
1752
+ except Exception as e:
1753
+ self.logger.warning(f"Failed to insert chart {i+1}: {e}")
1754
+
1755
+ return results
1756
+
1757
+ def _generate_ai_analysis_content(
1758
+ self,
1759
+ document_path: str,
1760
+ data_sources: List[Dict[str, Any]],
1761
+ chart_results: List[Dict[str, Any]],
1762
+ ) -> Dict[str, Any]:
1763
+ """Generate AI-driven analysis content"""
1764
+ try:
1765
+ # Generate analysis based on data
1766
+ analysis_prompt = f"""
1767
+ Analyze the following data sources and provide insights:
1768
+ Data Sources: {len(data_sources)} datasets
1769
+ Charts Generated: {len([r for r in chart_results if r.get('success')])}
1770
+
1771
+ Please provide:
1772
+ 1. Key findings from the data
1773
+ 2. Trends and patterns observed
1774
+ 3. Recommendations based on the analysis
1775
+ """
1776
+
1777
+ analysis_result = self._generate_content_with_ai(
1778
+ analysis_prompt,
1779
+ ContentGenerationMode.GENERATE,
1780
+ "markdown",
1781
+ self.config.default_ai_provider,
1782
+ {},
1783
+ )
1784
+
1785
+ # Insert analysis content into document
1786
+ if self.document_writer:
1787
+ write_result = self.document_writer.write_document(
1788
+ target_path=document_path,
1789
+ content=analysis_result["generated_content"],
1790
+ format="markdown",
1791
+ mode="append",
1792
+ )
1793
+
1794
+ return {
1795
+ "analysis_generated": True,
1796
+ "analysis_content": analysis_result,
1797
+ "write_result": write_result,
1798
+ }
1799
+
1800
+ except Exception as e:
1801
+ self.logger.warning(f"Failed to generate AI analysis: {e}")
1802
+
1803
+ return {"analysis_generated": False}
1804
+
1805
+ def _generate_layout_optimization_plan(
1806
+ self,
1807
+ document_path: str,
1808
+ content_analysis: Dict[str, Any],
1809
+ optimization_goals: List[str],
1810
+ layout_style: Optional[str],
1811
+ ) -> Dict[str, Any]:
1812
+ """Generate layout optimization plan"""
1813
+ return {
1814
+ "document_path": document_path,
1815
+ "optimization_goals": optimization_goals,
1816
+ "layout_style": layout_style,
1817
+ "content_analysis": content_analysis,
1818
+ "recommended_actions": [
1819
+ "Optimize spacing",
1820
+ "Improve typography",
1821
+ "Enhance readability",
1822
+ ],
1823
+ }
1824
+
1825
+ def _validate_content_preservation(
1826
+ self, document_path: str, original_analysis: Dict[str, Any]
1827
+ ) -> Dict[str, Any]:
1828
+ """Validate that content was preserved during optimization"""
1829
+ try:
1830
+ # Re-analyze document after optimization
1831
+ new_analysis = self.analyze_document_content(
1832
+ source_path=document_path, analysis_type="structure"
1833
+ )
1834
+
1835
+ # Compare analyses
1836
+ original_length = original_analysis.get("analysis_result", {}).get("content_length", 0)
1837
+ new_length = new_analysis.get("analysis_result", {}).get("content_length", 0)
1838
+
1839
+ content_preserved = abs(original_length - new_length) / max(original_length, 1) < 0.1
1840
+
1841
+ return {
1842
+ "content_preserved": content_preserved,
1843
+ "original_length": original_length,
1844
+ "new_length": new_length,
1845
+ "difference_ratio": abs(original_length - new_length) / max(original_length, 1),
1846
+ }
1847
+
1848
+ except Exception as e:
1849
+ return {"validation_error": str(e)}
1850
+
1851
+ def _optimize_content_insertion_plan(
1852
+ self, document_path: str, content_plan: List[Dict[str, Any]]
1853
+ ) -> List[Dict[str, Any]]:
1854
+ """Optimize content insertion plan using AI"""
1855
+ # For now, return original plan
1856
+ # In a full implementation, this would use AI to optimize the order
1857
+ return content_plan
1858
+
1859
+ def _execute_sequential_insertions(
1860
+ self, document_path: str, plan: List[Dict[str, Any]], content_tool
1861
+ ) -> Dict[str, Any]:
1862
+ """Execute content insertions sequentially"""
1863
+ return content_tool.batch_insert_content(document_path=document_path, content_items=plan)
1864
+
1865
+ def _execute_parallel_insertions(
1866
+ self, document_path: str, plan: List[Dict[str, Any]], content_tool
1867
+ ) -> Dict[str, Any]:
1868
+ """Execute content insertions in parallel (simplified to sequential for now)"""
1869
+ return self._execute_sequential_insertions(document_path, plan, content_tool)
1870
+
1871
+ def _execute_optimized_insertions(
1872
+ self, document_path: str, plan: List[Dict[str, Any]], content_tool
1873
+ ) -> Dict[str, Any]:
1874
+ """Execute optimized content insertions"""
1875
+ return self._execute_sequential_insertions(document_path, plan, content_tool)
1876
+
1877
+ def _post_insertion_optimization(
1878
+ self, document_path: str, insertion_results: Dict[str, Any]
1879
+ ) -> Dict[str, Any]:
1880
+ """Perform post-insertion optimization"""
1881
+ return {
1882
+ "optimization_performed": True,
1883
+ "document_path": document_path,
1884
+ "insertion_results": insertion_results,
1885
+ }
1886
+
1887
+ def create_content_template(
1888
+ self,
1889
+ template_name: str,
1890
+ template_content: str,
1891
+ template_variables: List[str],
1892
+ metadata: Optional[Dict[str, Any]] = None,
1893
+ ) -> Dict[str, Any]:
1894
+ """
1895
+ Create reusable content template
1896
+
1897
+ Args:
1898
+ template_name: Name of the template
1899
+ template_content: Template content with variables
1900
+ template_variables: List of template variables
1901
+ metadata: Additional template metadata
1902
+
1903
+ Returns:
1904
+ Dict containing template information
1905
+ """
1906
+ template_info = {
1907
+ "name": template_name,
1908
+ "content": template_content,
1909
+ "variables": template_variables,
1910
+ "metadata": metadata or {},
1911
+ "created_at": datetime.now().isoformat(),
1912
+ "version": "1.0",
1913
+ }
1914
+
1915
+ # Save template
1916
+ temp_dir = tempfile.gettempdir()
1917
+ template_file = os.path.join(temp_dir, f"template_{template_name}.json")
1918
+ with open(template_file, "w") as f:
1919
+ import json
1920
+
1921
+ json.dump(template_info, f, indent=2)
1922
+
1923
+ return template_info
1924
+
1925
+ def use_content_template(
1926
+ self,
1927
+ template_name: str,
1928
+ template_data: Dict[str, Any],
1929
+ target_path: str,
1930
+ ai_enhancement: bool = True,
1931
+ ) -> Dict[str, Any]:
1932
+ """
1933
+ Use content template to generate document
1934
+
1935
+ Args:
1936
+ template_name: Name of the template to use
1937
+ template_data: Data to fill template variables
1938
+ target_path: Target document path
1939
+ ai_enhancement: Whether to enhance with AI
1940
+
1941
+ Returns:
1942
+ Dict containing template usage results
1943
+ """
1944
+ try:
1945
+ # Load template
1946
+ template_file = os.path.join(self.config.temp_dir, f"template_{template_name}.json")
1947
+ with open(template_file, "r") as f:
1948
+ import json
1949
+
1950
+ template_info = json.load(f)
1951
+
1952
+ # Fill template
1953
+ filled_content = self._fill_template(template_info["content"], template_data)
1954
+
1955
+ # Enhance with AI if requested
1956
+ if ai_enhancement:
1957
+ ai_result = self._generate_content_with_ai(
1958
+ f"Template: {template_name}",
1959
+ ContentGenerationMode.TEMPLATE_FILL,
1960
+ "txt",
1961
+ self.config.default_ai_provider,
1962
+ {
1963
+ "template": template_info["content"],
1964
+ "data": template_data,
1965
+ },
1966
+ )
1967
+ filled_content = ai_result["generated_content"]
1968
+
1969
+ # Write document
1970
+ write_result = self.document_writer.write_document(
1971
+ target_path=target_path,
1972
+ content=filled_content,
1973
+ format="txt",
1974
+ mode="create",
1975
+ )
1976
+
1977
+ return {
1978
+ "template_name": template_name,
1979
+ "template_data": template_data,
1980
+ "target_path": target_path,
1981
+ "ai_enhancement": ai_enhancement,
1982
+ "filled_content": filled_content,
1983
+ "write_result": write_result,
1984
+ }
1985
+
1986
+ except Exception as e:
1987
+ raise WriteOrchestrationError(f"Template usage failed: {str(e)}")
1988
+
1989
+ def _generate_content_with_ai(
1990
+ self,
1991
+ requirements: str,
1992
+ generation_mode: ContentGenerationMode,
1993
+ document_format: str,
1994
+ ai_provider: AIProvider,
1995
+ generation_params: Dict[str, Any],
1996
+ ) -> Dict[str, Any]:
1997
+ """Generate content using AI based on requirements"""
1998
+
1999
+ try:
2000
+ # Get content generation template
2001
+ template = self.content_templates.get(generation_mode)
2002
+ if not template:
2003
+ raise ContentGenerationError(
2004
+ f"No template found for generation mode: {generation_mode}"
2005
+ )
2006
+
2007
+ # Prepare AI prompt
2008
+ prompt_params = {
2009
+ "content_type": document_format,
2010
+ "requirements": requirements,
2011
+ "audience": generation_params.get("audience", "general"),
2012
+ **generation_params,
2013
+ }
2014
+
2015
+ prompt = self._format_content_prompt(template, prompt_params)
2016
+
2017
+ # Call AI provider
2018
+ ai_response = self._call_ai_provider(prompt, ai_provider, generation_params)
2019
+
2020
+ return {
2021
+ "generation_mode": generation_mode,
2022
+ "requirements": requirements,
2023
+ "prompt_used": prompt,
2024
+ "generated_content": ai_response,
2025
+ "ai_provider": ai_provider,
2026
+ "generation_params": generation_params,
2027
+ }
2028
+
2029
+ except Exception as e:
2030
+ raise ContentGenerationError(f"AI content generation failed: {str(e)}")
2031
+
2032
+ def _enhance_content_with_ai(
2033
+ self,
2034
+ existing_content: Dict[str, Any],
2035
+ enhancement_goals: str,
2036
+ ai_provider: AIProvider,
2037
+ ) -> Dict[str, Any]:
2038
+ """Enhance existing content using AI"""
2039
+
2040
+ try:
2041
+ template = self.content_templates[ContentGenerationMode.ENHANCE]
2042
+
2043
+ prompt_params = {
2044
+ "existing_content": existing_content["content"],
2045
+ "enhancement_goals": enhancement_goals,
2046
+ }
2047
+
2048
+ prompt = self._format_content_prompt(template, prompt_params)
2049
+ ai_response = self._call_ai_provider(prompt, ai_provider, {})
2050
+
2051
+ return {
2052
+ "original_content": existing_content["content"],
2053
+ "enhancement_goals": enhancement_goals,
2054
+ "enhanced_content": ai_response,
2055
+ "ai_provider": ai_provider,
2056
+ }
2057
+
2058
+ except Exception as e:
2059
+ raise ContentGenerationError(f"AI content enhancement failed: {str(e)}")
2060
+
2061
+ def _process_generated_content(
2062
+ self,
2063
+ content: str,
2064
+ document_format: str,
2065
+ generation_mode: ContentGenerationMode,
2066
+ ) -> str:
2067
+ """Process generated content for specific format"""
2068
+
2069
+ # Format-specific processing
2070
+ if document_format.lower() == "markdown":
2071
+ # Ensure proper markdown formatting
2072
+ content = self._ensure_markdown_formatting(content)
2073
+ elif document_format.lower() == "html":
2074
+ # Ensure proper HTML structure
2075
+ content = self._ensure_html_structure(content)
2076
+ elif document_format.lower() == "json":
2077
+ # Validate and format JSON
2078
+ content = self._ensure_json_format(content)
2079
+
2080
+ return content
2081
+
2082
+ def _execute_write_strategy(
2083
+ self,
2084
+ target_path: str,
2085
+ content: str,
2086
+ document_format: str,
2087
+ write_strategy: WriteStrategy,
2088
+ write_params: Dict[str, Any],
2089
+ ) -> Dict[str, Any]:
2090
+ """Execute write strategy"""
2091
+
2092
+ if not self.document_writer:
2093
+ raise WriteOrchestrationError("DocumentWriterTool not available")
2094
+
2095
+ if write_strategy == WriteStrategy.IMMEDIATE:
2096
+ # Write immediately
2097
+ return self.document_writer.write_document(
2098
+ target_path=target_path,
2099
+ content=content,
2100
+ format=document_format,
2101
+ mode="create",
2102
+ **write_params,
2103
+ )
2104
+
2105
+ elif write_strategy == WriteStrategy.DRAFT:
2106
+ # Save as draft
2107
+ draft_path = f"{target_path}.draft"
2108
+ return self.document_writer.write_document(
2109
+ target_path=draft_path,
2110
+ content=content,
2111
+ format=document_format,
2112
+ mode="create",
2113
+ **write_params,
2114
+ )
2115
+
2116
+ elif write_strategy == WriteStrategy.REVIEW:
2117
+ # Save for review
2118
+ review_path = f"{target_path}.review"
2119
+ return self.document_writer.write_document(
2120
+ target_path=review_path,
2121
+ content=content,
2122
+ format=document_format,
2123
+ mode="create",
2124
+ **write_params,
2125
+ )
2126
+
2127
+ elif write_strategy == WriteStrategy.STAGED:
2128
+ # Staged write (implement custom logic)
2129
+ return self._execute_staged_write(target_path, content, document_format, write_params)
2130
+
2131
+ else:
2132
+ raise ValueError(f"Unknown write strategy: {write_strategy}")
2133
+
2134
+ def _execute_staged_write(
2135
+ self,
2136
+ target_path: str,
2137
+ content: str,
2138
+ document_format: str,
2139
+ write_params: Dict[str, Any],
2140
+ ) -> Dict[str, Any]:
2141
+ """Execute staged write operation"""
2142
+
2143
+ # Split content into stages (simplified implementation)
2144
+ content_parts = content.split("\n\n") # Split by paragraphs
2145
+ stage_results = []
2146
+
2147
+ for i, part in enumerate(content_parts):
2148
+ stage_path = f"{target_path}.stage_{i+1}"
2149
+ stage_result = self.document_writer.write_document(
2150
+ target_path=stage_path,
2151
+ content=part,
2152
+ format=document_format,
2153
+ mode="create",
2154
+ **write_params,
2155
+ )
2156
+ stage_results.append(stage_result)
2157
+
2158
+ return {
2159
+ "strategy": "staged",
2160
+ "total_stages": len(content_parts),
2161
+ "stage_results": stage_results,
2162
+ }
2163
+
2164
+ def _read_existing_document(self, source_path: str) -> Dict[str, Any]:
2165
+ """Read existing document for enhancement"""
2166
+
2167
+ try:
2168
+ # Try to use document parser for reading
2169
+ try:
2170
+ from aiecs.tools.docs.document_parser_tool import (
2171
+ DocumentParserTool,
2172
+ )
2173
+
2174
+ parser = DocumentParserTool()
2175
+ parse_result = parser.parse_document(source_path)
2176
+
2177
+ return {
2178
+ "content": parse_result["content"],
2179
+ "format": parse_result["document_type"],
2180
+ "metadata": parse_result.get("metadata", {}),
2181
+ }
2182
+
2183
+ except ImportError:
2184
+ # Fallback to simple file reading
2185
+ with open(source_path, "r", encoding="utf-8") as f:
2186
+ content = f.read()
2187
+
2188
+ file_ext = os.path.splitext(source_path)[1].lower()
2189
+ return {
2190
+ "content": content,
2191
+ "format": file_ext.lstrip(".") or "txt",
2192
+ "metadata": {},
2193
+ }
2194
+
2195
+ except Exception as e:
2196
+ raise WriteOrchestrationError(f"Failed to read existing document: {str(e)}")
2197
+
2198
+ def _call_ai_provider(
2199
+ self, prompt: str, ai_provider: AIProvider, params: Dict[str, Any]
2200
+ ) -> str:
2201
+ """Call AI provider with prompt"""
2202
+
2203
+ try:
2204
+ if self.aiecs_client:
2205
+ # Use AIECS client for AI operations
2206
+ from aiecs.domain.task.task_context import TaskContext
2207
+
2208
+ task_context = TaskContext(
2209
+ data={
2210
+ "user_id": "test_user",
2211
+ "chat_id": f"content_gen_{datetime.now().timestamp()}",
2212
+ "metadata": params,
2213
+ "aiPreference": params.get("ai_provider", "default"),
2214
+ }
2215
+ )
2216
+
2217
+ result = self.aiecs_client.process_task(task_context)
2218
+ return result.get("response", "")
2219
+ else:
2220
+ # Fallback to mock response
2221
+ return self._generate_mock_content(prompt, params)
2222
+
2223
+ except Exception as e:
2224
+ raise ContentGenerationError(f"AI provider call failed: {str(e)}")
2225
+
2226
+ def _generate_mock_content(self, prompt: str, params: Dict[str, Any]) -> str:
2227
+ """Generate mock content for testing"""
2228
+ self.logger.warning(
2229
+ "Using mock content generation - implement actual AI provider integration"
2230
+ )
2231
+
2232
+ # Generate simple mock content based on prompt
2233
+ if "requirements" in params:
2234
+ return f"Generated content based on: {params['requirements']}\n\nThis is mock content for testing purposes."
2235
+ else:
2236
+ return f"Mock generated content for prompt: {prompt[:100]}..."
2237
+
2238
+ def _format_content_prompt(self, template: Dict[str, str], params: Dict[str, Any]) -> str:
2239
+ """Format content generation prompt using template"""
2240
+
2241
+ user_prompt = template["user_prompt_template"]
2242
+
2243
+ # Replace placeholders
2244
+ for key, value in params.items():
2245
+ placeholder = f"{{{key}}}"
2246
+ if placeholder in user_prompt:
2247
+ user_prompt = user_prompt.replace(placeholder, str(value))
2248
+
2249
+ return user_prompt
2250
+
2251
+ def _fill_template(self, template_content: str, template_data: Dict[str, Any]) -> str:
2252
+ """Fill template with provided data"""
2253
+
2254
+ filled_content = template_content
2255
+ for key, value in template_data.items():
2256
+ placeholder = f"{{{key}}}"
2257
+ filled_content = filled_content.replace(placeholder, str(value))
2258
+
2259
+ return filled_content
2260
+
2261
+ # Content formatting helpers
2262
+ def _ensure_markdown_formatting(self, content: str) -> str:
2263
+ """Ensure proper markdown formatting"""
2264
+ # Add basic markdown formatting if missing
2265
+ lines = content.split("\n")
2266
+ formatted_lines = []
2267
+
2268
+ for line in lines:
2269
+ line = line.strip()
2270
+ if (
2271
+ line
2272
+ and not line.startswith("#")
2273
+ and not line.startswith("-")
2274
+ and not line.startswith("*")
2275
+ ):
2276
+ # Add paragraph spacing
2277
+ formatted_lines.append(line + "\n")
2278
+ else:
2279
+ formatted_lines.append(line)
2280
+
2281
+ return "\n".join(formatted_lines)
2282
+
2283
+ def _ensure_html_structure(self, content: str) -> str:
2284
+ """Ensure proper HTML structure"""
2285
+ if not content.strip().startswith("<html"):
2286
+ content = f"<html><body>{content}</body></html>"
2287
+ return content
2288
+
2289
+ def _ensure_json_format(self, content: str) -> str:
2290
+ """Ensure proper JSON format"""
2291
+ try:
2292
+ import json
2293
+
2294
+ # Try to parse and reformat
2295
+ parsed = json.loads(content)
2296
+ return json.dumps(parsed, indent=2, ensure_ascii=False)
2297
+ except json.JSONDecodeError:
2298
+ # Wrap in basic JSON structure
2299
+ return json.dumps({"content": content}, indent=2, ensure_ascii=False)
2300
+
2301
+ # Batch processing methods
2302
+ async def _batch_write_parallel(
2303
+ self, write_requests: List[Dict[str, Any]], max_concurrent: int
2304
+ ) -> List[Dict[str, Any]]:
2305
+ """Process write requests in parallel"""
2306
+
2307
+ semaphore = asyncio.Semaphore(max_concurrent)
2308
+
2309
+ async def process_single_request(
2310
+ request: Dict[str, Any],
2311
+ ) -> Dict[str, Any]:
2312
+ async with semaphore:
2313
+ try:
2314
+ result = await self.ai_write_document_async(**request)
2315
+ return {
2316
+ "status": "success",
2317
+ "request": request,
2318
+ "result": result,
2319
+ }
2320
+ except Exception as e:
2321
+ return {
2322
+ "status": "error",
2323
+ "request": request,
2324
+ "error": str(e),
2325
+ }
2326
+
2327
+ tasks = [process_single_request(req) for req in write_requests]
2328
+ return await asyncio.gather(*tasks)
2329
+
2330
+ def _batch_write_sequential(self, write_requests: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
2331
+ """Process write requests sequentially"""
2332
+
2333
+ results = []
2334
+ for request in write_requests:
2335
+ try:
2336
+ result = self.ai_write_document(**request)
2337
+ results.append({"status": "success", "request": request, "result": result})
2338
+ except Exception as e:
2339
+ results.append({"status": "error", "request": request, "error": str(e)})
2340
+
2341
+ return results
2342
+
2343
+ async def _batch_write_smart(
2344
+ self, write_requests: List[Dict[str, Any]], max_concurrent: int
2345
+ ) -> List[Dict[str, Any]]:
2346
+ """Smart batch processing with dependency awareness"""
2347
+
2348
+ # Analyze dependencies (simplified implementation)
2349
+ independent_requests = []
2350
+ dependent_requests = []
2351
+
2352
+ for request in write_requests:
2353
+ # Check if request depends on others (simplified logic)
2354
+ if any(req.get("target_path") == request.get("source_path") for req in write_requests):
2355
+ dependent_requests.append(request)
2356
+ else:
2357
+ independent_requests.append(request)
2358
+
2359
+ # Process independent requests in parallel
2360
+ results = []
2361
+ if independent_requests:
2362
+ parallel_results = await self._batch_write_parallel(
2363
+ independent_requests, max_concurrent
2364
+ )
2365
+ results.extend(parallel_results)
2366
+
2367
+ # Process dependent requests sequentially
2368
+ if dependent_requests:
2369
+ sequential_results = self._batch_write_sequential(dependent_requests)
2370
+ results.extend(sequential_results)
2371
+
2372
+ return results
2373
+
2374
+ def _post_process_ai_write(
2375
+ self,
2376
+ operation_id: str,
2377
+ target_path: str,
2378
+ ai_result: Dict[str, Any],
2379
+ write_result: Dict[str, Any],
2380
+ write_strategy: WriteStrategy,
2381
+ ) -> Dict[str, Any]:
2382
+ """Post-process AI write operation"""
2383
+
2384
+ post_process_info = {
2385
+ "operation_id": operation_id,
2386
+ "target_path": target_path,
2387
+ "write_strategy": write_strategy,
2388
+ "content_length": len(ai_result.get("generated_content", "")),
2389
+ "write_success": write_result.get("write_result", {}).get("path") is not None,
2390
+ "timestamp": datetime.now().isoformat(),
2391
+ }
2392
+
2393
+ # Log operation
2394
+ try:
2395
+ log_file = os.path.join(self.config.temp_dir, "ai_write_operations.log")
2396
+ with open(log_file, "a") as f:
2397
+ import json
2398
+
2399
+ f.write(json.dumps(post_process_info) + "\n")
2400
+ except Exception as e:
2401
+ self.logger.warning(f"Operation logging failed: {e}")
2402
+
2403
+ return post_process_info