aiecs 1.0.8__py3-none-any.whl → 1.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of aiecs might be problematic. Click here for more details.

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