attune-ai 2.0.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.
- attune/__init__.py +358 -0
- attune/adaptive/__init__.py +13 -0
- attune/adaptive/task_complexity.py +127 -0
- attune/agent_monitoring.py +414 -0
- attune/cache/__init__.py +117 -0
- attune/cache/base.py +166 -0
- attune/cache/dependency_manager.py +256 -0
- attune/cache/hash_only.py +251 -0
- attune/cache/hybrid.py +457 -0
- attune/cache/storage.py +285 -0
- attune/cache_monitor.py +356 -0
- attune/cache_stats.py +298 -0
- attune/cli/__init__.py +152 -0
- attune/cli/__main__.py +12 -0
- attune/cli/commands/__init__.py +1 -0
- attune/cli/commands/batch.py +264 -0
- attune/cli/commands/cache.py +248 -0
- attune/cli/commands/help.py +331 -0
- attune/cli/commands/info.py +140 -0
- attune/cli/commands/inspect.py +436 -0
- attune/cli/commands/inspection.py +57 -0
- attune/cli/commands/memory.py +48 -0
- attune/cli/commands/metrics.py +92 -0
- attune/cli/commands/orchestrate.py +184 -0
- attune/cli/commands/patterns.py +207 -0
- attune/cli/commands/profiling.py +202 -0
- attune/cli/commands/provider.py +98 -0
- attune/cli/commands/routing.py +285 -0
- attune/cli/commands/setup.py +96 -0
- attune/cli/commands/status.py +235 -0
- attune/cli/commands/sync.py +166 -0
- attune/cli/commands/tier.py +121 -0
- attune/cli/commands/utilities.py +114 -0
- attune/cli/commands/workflow.py +579 -0
- attune/cli/core.py +32 -0
- attune/cli/parsers/__init__.py +68 -0
- attune/cli/parsers/batch.py +118 -0
- attune/cli/parsers/cache.py +65 -0
- attune/cli/parsers/help.py +41 -0
- attune/cli/parsers/info.py +26 -0
- attune/cli/parsers/inspect.py +66 -0
- attune/cli/parsers/metrics.py +42 -0
- attune/cli/parsers/orchestrate.py +61 -0
- attune/cli/parsers/patterns.py +54 -0
- attune/cli/parsers/provider.py +40 -0
- attune/cli/parsers/routing.py +110 -0
- attune/cli/parsers/setup.py +42 -0
- attune/cli/parsers/status.py +47 -0
- attune/cli/parsers/sync.py +31 -0
- attune/cli/parsers/tier.py +33 -0
- attune/cli/parsers/workflow.py +77 -0
- attune/cli/utils/__init__.py +1 -0
- attune/cli/utils/data.py +242 -0
- attune/cli/utils/helpers.py +68 -0
- attune/cli_legacy.py +3957 -0
- attune/cli_minimal.py +1159 -0
- attune/cli_router.py +437 -0
- attune/cli_unified.py +814 -0
- attune/config/__init__.py +66 -0
- attune/config/xml_config.py +286 -0
- attune/config.py +545 -0
- attune/coordination.py +870 -0
- attune/core.py +1511 -0
- attune/core_modules/__init__.py +15 -0
- attune/cost_tracker.py +626 -0
- attune/dashboard/__init__.py +41 -0
- attune/dashboard/app.py +512 -0
- attune/dashboard/simple_server.py +435 -0
- attune/dashboard/standalone_server.py +547 -0
- attune/discovery.py +306 -0
- attune/emergence.py +306 -0
- attune/exceptions.py +123 -0
- attune/feedback_loops.py +373 -0
- attune/hot_reload/README.md +473 -0
- attune/hot_reload/__init__.py +62 -0
- attune/hot_reload/config.py +83 -0
- attune/hot_reload/integration.py +229 -0
- attune/hot_reload/reloader.py +298 -0
- attune/hot_reload/watcher.py +183 -0
- attune/hot_reload/websocket.py +177 -0
- attune/levels.py +577 -0
- attune/leverage_points.py +441 -0
- attune/logging_config.py +261 -0
- attune/mcp/__init__.py +10 -0
- attune/mcp/server.py +506 -0
- attune/memory/__init__.py +237 -0
- attune/memory/claude_memory.py +469 -0
- attune/memory/config.py +224 -0
- attune/memory/control_panel.py +1290 -0
- attune/memory/control_panel_support.py +145 -0
- attune/memory/cross_session.py +845 -0
- attune/memory/edges.py +179 -0
- attune/memory/encryption.py +159 -0
- attune/memory/file_session.py +770 -0
- attune/memory/graph.py +570 -0
- attune/memory/long_term.py +913 -0
- attune/memory/long_term_types.py +99 -0
- attune/memory/mixins/__init__.py +25 -0
- attune/memory/mixins/backend_init_mixin.py +249 -0
- attune/memory/mixins/capabilities_mixin.py +208 -0
- attune/memory/mixins/handoff_mixin.py +208 -0
- attune/memory/mixins/lifecycle_mixin.py +49 -0
- attune/memory/mixins/long_term_mixin.py +352 -0
- attune/memory/mixins/promotion_mixin.py +109 -0
- attune/memory/mixins/short_term_mixin.py +182 -0
- attune/memory/nodes.py +179 -0
- attune/memory/redis_bootstrap.py +540 -0
- attune/memory/security/__init__.py +31 -0
- attune/memory/security/audit_logger.py +932 -0
- attune/memory/security/pii_scrubber.py +640 -0
- attune/memory/security/secrets_detector.py +678 -0
- attune/memory/short_term.py +2192 -0
- attune/memory/simple_storage.py +302 -0
- attune/memory/storage/__init__.py +15 -0
- attune/memory/storage_backend.py +167 -0
- attune/memory/summary_index.py +583 -0
- attune/memory/types.py +446 -0
- attune/memory/unified.py +182 -0
- attune/meta_workflows/__init__.py +74 -0
- attune/meta_workflows/agent_creator.py +248 -0
- attune/meta_workflows/builtin_templates.py +567 -0
- attune/meta_workflows/cli_commands/__init__.py +56 -0
- attune/meta_workflows/cli_commands/agent_commands.py +321 -0
- attune/meta_workflows/cli_commands/analytics_commands.py +442 -0
- attune/meta_workflows/cli_commands/config_commands.py +232 -0
- attune/meta_workflows/cli_commands/memory_commands.py +182 -0
- attune/meta_workflows/cli_commands/template_commands.py +354 -0
- attune/meta_workflows/cli_commands/workflow_commands.py +382 -0
- attune/meta_workflows/cli_meta_workflows.py +59 -0
- attune/meta_workflows/form_engine.py +292 -0
- attune/meta_workflows/intent_detector.py +409 -0
- attune/meta_workflows/models.py +569 -0
- attune/meta_workflows/pattern_learner.py +738 -0
- attune/meta_workflows/plan_generator.py +384 -0
- attune/meta_workflows/session_context.py +397 -0
- attune/meta_workflows/template_registry.py +229 -0
- attune/meta_workflows/workflow.py +984 -0
- attune/metrics/__init__.py +12 -0
- attune/metrics/collector.py +31 -0
- attune/metrics/prompt_metrics.py +194 -0
- attune/models/__init__.py +172 -0
- attune/models/__main__.py +13 -0
- attune/models/adaptive_routing.py +437 -0
- attune/models/auth_cli.py +444 -0
- attune/models/auth_strategy.py +450 -0
- attune/models/cli.py +655 -0
- attune/models/empathy_executor.py +354 -0
- attune/models/executor.py +257 -0
- attune/models/fallback.py +762 -0
- attune/models/provider_config.py +282 -0
- attune/models/registry.py +472 -0
- attune/models/tasks.py +359 -0
- attune/models/telemetry/__init__.py +71 -0
- attune/models/telemetry/analytics.py +594 -0
- attune/models/telemetry/backend.py +196 -0
- attune/models/telemetry/data_models.py +431 -0
- attune/models/telemetry/storage.py +489 -0
- attune/models/token_estimator.py +420 -0
- attune/models/validation.py +280 -0
- attune/monitoring/__init__.py +52 -0
- attune/monitoring/alerts.py +946 -0
- attune/monitoring/alerts_cli.py +448 -0
- attune/monitoring/multi_backend.py +271 -0
- attune/monitoring/otel_backend.py +362 -0
- attune/optimization/__init__.py +19 -0
- attune/optimization/context_optimizer.py +272 -0
- attune/orchestration/__init__.py +67 -0
- attune/orchestration/agent_templates.py +707 -0
- attune/orchestration/config_store.py +499 -0
- attune/orchestration/execution_strategies.py +2111 -0
- attune/orchestration/meta_orchestrator.py +1168 -0
- attune/orchestration/pattern_learner.py +696 -0
- attune/orchestration/real_tools.py +931 -0
- attune/pattern_cache.py +187 -0
- attune/pattern_library.py +542 -0
- attune/patterns/debugging/all_patterns.json +81 -0
- attune/patterns/debugging/workflow_20260107_1770825e.json +77 -0
- attune/patterns/refactoring_memory.json +89 -0
- attune/persistence.py +564 -0
- attune/platform_utils.py +265 -0
- attune/plugins/__init__.py +28 -0
- attune/plugins/base.py +361 -0
- attune/plugins/registry.py +268 -0
- attune/project_index/__init__.py +32 -0
- attune/project_index/cli.py +335 -0
- attune/project_index/index.py +667 -0
- attune/project_index/models.py +504 -0
- attune/project_index/reports.py +474 -0
- attune/project_index/scanner.py +777 -0
- attune/project_index/scanner_parallel.py +291 -0
- attune/prompts/__init__.py +61 -0
- attune/prompts/config.py +77 -0
- attune/prompts/context.py +177 -0
- attune/prompts/parser.py +285 -0
- attune/prompts/registry.py +313 -0
- attune/prompts/templates.py +208 -0
- attune/redis_config.py +302 -0
- attune/redis_memory.py +799 -0
- attune/resilience/__init__.py +56 -0
- attune/resilience/circuit_breaker.py +256 -0
- attune/resilience/fallback.py +179 -0
- attune/resilience/health.py +300 -0
- attune/resilience/retry.py +209 -0
- attune/resilience/timeout.py +135 -0
- attune/routing/__init__.py +43 -0
- attune/routing/chain_executor.py +433 -0
- attune/routing/classifier.py +217 -0
- attune/routing/smart_router.py +234 -0
- attune/routing/workflow_registry.py +343 -0
- attune/scaffolding/README.md +589 -0
- attune/scaffolding/__init__.py +35 -0
- attune/scaffolding/__main__.py +14 -0
- attune/scaffolding/cli.py +240 -0
- attune/scaffolding/templates/base_wizard.py.jinja2 +121 -0
- attune/scaffolding/templates/coach_wizard.py.jinja2 +321 -0
- attune/scaffolding/templates/domain_wizard.py.jinja2 +408 -0
- attune/scaffolding/templates/linear_flow_wizard.py.jinja2 +203 -0
- attune/socratic/__init__.py +256 -0
- attune/socratic/ab_testing.py +958 -0
- attune/socratic/blueprint.py +533 -0
- attune/socratic/cli.py +703 -0
- attune/socratic/collaboration.py +1114 -0
- attune/socratic/domain_templates.py +924 -0
- attune/socratic/embeddings.py +738 -0
- attune/socratic/engine.py +794 -0
- attune/socratic/explainer.py +682 -0
- attune/socratic/feedback.py +772 -0
- attune/socratic/forms.py +629 -0
- attune/socratic/generator.py +732 -0
- attune/socratic/llm_analyzer.py +637 -0
- attune/socratic/mcp_server.py +702 -0
- attune/socratic/session.py +312 -0
- attune/socratic/storage.py +667 -0
- attune/socratic/success.py +730 -0
- attune/socratic/visual_editor.py +860 -0
- attune/socratic/web_ui.py +958 -0
- attune/telemetry/__init__.py +39 -0
- attune/telemetry/agent_coordination.py +475 -0
- attune/telemetry/agent_tracking.py +367 -0
- attune/telemetry/approval_gates.py +545 -0
- attune/telemetry/cli.py +1231 -0
- attune/telemetry/commands/__init__.py +14 -0
- attune/telemetry/commands/dashboard_commands.py +696 -0
- attune/telemetry/event_streaming.py +409 -0
- attune/telemetry/feedback_loop.py +567 -0
- attune/telemetry/usage_tracker.py +591 -0
- attune/templates.py +754 -0
- attune/test_generator/__init__.py +38 -0
- attune/test_generator/__main__.py +14 -0
- attune/test_generator/cli.py +234 -0
- attune/test_generator/generator.py +355 -0
- attune/test_generator/risk_analyzer.py +216 -0
- attune/test_generator/templates/unit_test.py.jinja2 +272 -0
- attune/tier_recommender.py +384 -0
- attune/tools.py +183 -0
- attune/trust/__init__.py +28 -0
- attune/trust/circuit_breaker.py +579 -0
- attune/trust_building.py +527 -0
- attune/validation/__init__.py +19 -0
- attune/validation/xml_validator.py +281 -0
- attune/vscode_bridge.py +173 -0
- attune/workflow_commands.py +780 -0
- attune/workflow_patterns/__init__.py +33 -0
- attune/workflow_patterns/behavior.py +249 -0
- attune/workflow_patterns/core.py +76 -0
- attune/workflow_patterns/output.py +99 -0
- attune/workflow_patterns/registry.py +255 -0
- attune/workflow_patterns/structural.py +288 -0
- attune/workflows/__init__.py +539 -0
- attune/workflows/autonomous_test_gen.py +1268 -0
- attune/workflows/base.py +2667 -0
- attune/workflows/batch_processing.py +342 -0
- attune/workflows/bug_predict.py +1084 -0
- attune/workflows/builder.py +273 -0
- attune/workflows/caching.py +253 -0
- attune/workflows/code_review.py +1048 -0
- attune/workflows/code_review_adapters.py +312 -0
- attune/workflows/code_review_pipeline.py +722 -0
- attune/workflows/config.py +645 -0
- attune/workflows/dependency_check.py +644 -0
- attune/workflows/document_gen/__init__.py +25 -0
- attune/workflows/document_gen/config.py +30 -0
- attune/workflows/document_gen/report_formatter.py +162 -0
- attune/workflows/document_gen/workflow.py +1426 -0
- attune/workflows/document_manager.py +216 -0
- attune/workflows/document_manager_README.md +134 -0
- attune/workflows/documentation_orchestrator.py +1205 -0
- attune/workflows/history.py +510 -0
- attune/workflows/keyboard_shortcuts/__init__.py +39 -0
- attune/workflows/keyboard_shortcuts/generators.py +391 -0
- attune/workflows/keyboard_shortcuts/parsers.py +416 -0
- attune/workflows/keyboard_shortcuts/prompts.py +295 -0
- attune/workflows/keyboard_shortcuts/schema.py +193 -0
- attune/workflows/keyboard_shortcuts/workflow.py +509 -0
- attune/workflows/llm_base.py +363 -0
- attune/workflows/manage_docs.py +87 -0
- attune/workflows/manage_docs_README.md +134 -0
- attune/workflows/manage_documentation.py +821 -0
- attune/workflows/new_sample_workflow1.py +149 -0
- attune/workflows/new_sample_workflow1_README.md +150 -0
- attune/workflows/orchestrated_health_check.py +849 -0
- attune/workflows/orchestrated_release_prep.py +600 -0
- attune/workflows/output.py +413 -0
- attune/workflows/perf_audit.py +863 -0
- attune/workflows/pr_review.py +762 -0
- attune/workflows/progress.py +785 -0
- attune/workflows/progress_server.py +322 -0
- attune/workflows/progressive/README 2.md +454 -0
- attune/workflows/progressive/README.md +454 -0
- attune/workflows/progressive/__init__.py +82 -0
- attune/workflows/progressive/cli.py +219 -0
- attune/workflows/progressive/core.py +488 -0
- attune/workflows/progressive/orchestrator.py +723 -0
- attune/workflows/progressive/reports.py +520 -0
- attune/workflows/progressive/telemetry.py +274 -0
- attune/workflows/progressive/test_gen.py +495 -0
- attune/workflows/progressive/workflow.py +589 -0
- attune/workflows/refactor_plan.py +694 -0
- attune/workflows/release_prep.py +895 -0
- attune/workflows/release_prep_crew.py +969 -0
- attune/workflows/research_synthesis.py +404 -0
- attune/workflows/routing.py +168 -0
- attune/workflows/secure_release.py +593 -0
- attune/workflows/security_adapters.py +297 -0
- attune/workflows/security_audit.py +1329 -0
- attune/workflows/security_audit_phase3.py +355 -0
- attune/workflows/seo_optimization.py +633 -0
- attune/workflows/step_config.py +234 -0
- attune/workflows/telemetry_mixin.py +269 -0
- attune/workflows/test5.py +125 -0
- attune/workflows/test5_README.md +158 -0
- attune/workflows/test_coverage_boost_crew.py +849 -0
- attune/workflows/test_gen/__init__.py +52 -0
- attune/workflows/test_gen/ast_analyzer.py +249 -0
- attune/workflows/test_gen/config.py +88 -0
- attune/workflows/test_gen/data_models.py +38 -0
- attune/workflows/test_gen/report_formatter.py +289 -0
- attune/workflows/test_gen/test_templates.py +381 -0
- attune/workflows/test_gen/workflow.py +655 -0
- attune/workflows/test_gen.py +54 -0
- attune/workflows/test_gen_behavioral.py +477 -0
- attune/workflows/test_gen_parallel.py +341 -0
- attune/workflows/test_lifecycle.py +526 -0
- attune/workflows/test_maintenance.py +627 -0
- attune/workflows/test_maintenance_cli.py +590 -0
- attune/workflows/test_maintenance_crew.py +840 -0
- attune/workflows/test_runner.py +622 -0
- attune/workflows/tier_tracking.py +531 -0
- attune/workflows/xml_enhanced_crew.py +285 -0
- attune_ai-2.0.0.dist-info/METADATA +1026 -0
- attune_ai-2.0.0.dist-info/RECORD +457 -0
- attune_ai-2.0.0.dist-info/WHEEL +5 -0
- attune_ai-2.0.0.dist-info/entry_points.txt +26 -0
- attune_ai-2.0.0.dist-info/licenses/LICENSE +201 -0
- attune_ai-2.0.0.dist-info/licenses/LICENSE_CHANGE_ANNOUNCEMENT.md +101 -0
- attune_ai-2.0.0.dist-info/top_level.txt +5 -0
- attune_healthcare/__init__.py +13 -0
- attune_healthcare/monitors/__init__.py +9 -0
- attune_healthcare/monitors/clinical_protocol_monitor.py +315 -0
- attune_healthcare/monitors/monitoring/__init__.py +44 -0
- attune_healthcare/monitors/monitoring/protocol_checker.py +300 -0
- attune_healthcare/monitors/monitoring/protocol_loader.py +214 -0
- attune_healthcare/monitors/monitoring/sensor_parsers.py +306 -0
- attune_healthcare/monitors/monitoring/trajectory_analyzer.py +389 -0
- attune_llm/README.md +553 -0
- attune_llm/__init__.py +28 -0
- attune_llm/agent_factory/__init__.py +53 -0
- attune_llm/agent_factory/adapters/__init__.py +85 -0
- attune_llm/agent_factory/adapters/autogen_adapter.py +312 -0
- attune_llm/agent_factory/adapters/crewai_adapter.py +483 -0
- attune_llm/agent_factory/adapters/haystack_adapter.py +298 -0
- attune_llm/agent_factory/adapters/langchain_adapter.py +362 -0
- attune_llm/agent_factory/adapters/langgraph_adapter.py +333 -0
- attune_llm/agent_factory/adapters/native.py +228 -0
- attune_llm/agent_factory/adapters/wizard_adapter.py +423 -0
- attune_llm/agent_factory/base.py +305 -0
- attune_llm/agent_factory/crews/__init__.py +67 -0
- attune_llm/agent_factory/crews/code_review.py +1113 -0
- attune_llm/agent_factory/crews/health_check.py +1262 -0
- attune_llm/agent_factory/crews/refactoring.py +1128 -0
- attune_llm/agent_factory/crews/security_audit.py +1018 -0
- attune_llm/agent_factory/decorators.py +287 -0
- attune_llm/agent_factory/factory.py +558 -0
- attune_llm/agent_factory/framework.py +193 -0
- attune_llm/agent_factory/memory_integration.py +328 -0
- attune_llm/agent_factory/resilient.py +320 -0
- attune_llm/agents_md/__init__.py +22 -0
- attune_llm/agents_md/loader.py +218 -0
- attune_llm/agents_md/parser.py +271 -0
- attune_llm/agents_md/registry.py +307 -0
- attune_llm/claude_memory.py +466 -0
- attune_llm/cli/__init__.py +8 -0
- attune_llm/cli/sync_claude.py +487 -0
- attune_llm/code_health.py +1313 -0
- attune_llm/commands/__init__.py +51 -0
- attune_llm/commands/context.py +375 -0
- attune_llm/commands/loader.py +301 -0
- attune_llm/commands/models.py +231 -0
- attune_llm/commands/parser.py +371 -0
- attune_llm/commands/registry.py +429 -0
- attune_llm/config/__init__.py +29 -0
- attune_llm/config/unified.py +291 -0
- attune_llm/context/__init__.py +22 -0
- attune_llm/context/compaction.py +455 -0
- attune_llm/context/manager.py +434 -0
- attune_llm/contextual_patterns.py +361 -0
- attune_llm/core.py +907 -0
- attune_llm/git_pattern_extractor.py +435 -0
- attune_llm/hooks/__init__.py +24 -0
- attune_llm/hooks/config.py +306 -0
- attune_llm/hooks/executor.py +289 -0
- attune_llm/hooks/registry.py +302 -0
- attune_llm/hooks/scripts/__init__.py +39 -0
- attune_llm/hooks/scripts/evaluate_session.py +201 -0
- attune_llm/hooks/scripts/first_time_init.py +285 -0
- attune_llm/hooks/scripts/pre_compact.py +207 -0
- attune_llm/hooks/scripts/session_end.py +183 -0
- attune_llm/hooks/scripts/session_start.py +163 -0
- attune_llm/hooks/scripts/suggest_compact.py +225 -0
- attune_llm/learning/__init__.py +30 -0
- attune_llm/learning/evaluator.py +438 -0
- attune_llm/learning/extractor.py +514 -0
- attune_llm/learning/storage.py +560 -0
- attune_llm/levels.py +227 -0
- attune_llm/pattern_confidence.py +414 -0
- attune_llm/pattern_resolver.py +272 -0
- attune_llm/pattern_summary.py +350 -0
- attune_llm/providers.py +967 -0
- attune_llm/routing/__init__.py +32 -0
- attune_llm/routing/model_router.py +362 -0
- attune_llm/security/IMPLEMENTATION_SUMMARY.md +413 -0
- attune_llm/security/PHASE2_COMPLETE.md +384 -0
- attune_llm/security/PHASE2_SECRETS_DETECTOR_COMPLETE.md +271 -0
- attune_llm/security/QUICK_REFERENCE.md +316 -0
- attune_llm/security/README.md +262 -0
- attune_llm/security/__init__.py +62 -0
- attune_llm/security/audit_logger.py +929 -0
- attune_llm/security/audit_logger_example.py +152 -0
- attune_llm/security/pii_scrubber.py +640 -0
- attune_llm/security/secrets_detector.py +678 -0
- attune_llm/security/secrets_detector_example.py +304 -0
- attune_llm/security/secure_memdocs.py +1192 -0
- attune_llm/security/secure_memdocs_example.py +278 -0
- attune_llm/session_status.py +745 -0
- attune_llm/state.py +246 -0
- attune_llm/utils/__init__.py +5 -0
- attune_llm/utils/tokens.py +349 -0
- attune_software/SOFTWARE_PLUGIN_README.md +57 -0
- attune_software/__init__.py +13 -0
- attune_software/cli/__init__.py +120 -0
- attune_software/cli/inspect.py +362 -0
- attune_software/cli.py +574 -0
- attune_software/plugin.py +188 -0
- workflow_scaffolding/__init__.py +11 -0
- workflow_scaffolding/__main__.py +12 -0
- workflow_scaffolding/cli.py +206 -0
- workflow_scaffolding/generator.py +265 -0
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
"""Token Estimation Service
|
|
2
|
+
|
|
3
|
+
Pre-flight token estimation for cost prediction using tiktoken.
|
|
4
|
+
Provides accurate token counts before workflow execution.
|
|
5
|
+
|
|
6
|
+
Copyright 2025 Smart AI Memory, LLC
|
|
7
|
+
Licensed under Fair Source 0.9
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import functools
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
from attune.config import _validate_file_path
|
|
16
|
+
|
|
17
|
+
# Try to import tiktoken, fall back to heuristic if not available
|
|
18
|
+
try:
|
|
19
|
+
import tiktoken
|
|
20
|
+
|
|
21
|
+
TIKTOKEN_AVAILABLE = True
|
|
22
|
+
except ImportError:
|
|
23
|
+
TIKTOKEN_AVAILABLE = False
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# Heuristic fallback: ~4 tokens per word, ~0.25 tokens per character
|
|
27
|
+
TOKENS_PER_CHAR_HEURISTIC = 0.25
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@functools.lru_cache(maxsize=4)
|
|
31
|
+
def _get_encoding(model_id: str) -> Any:
|
|
32
|
+
"""Get tiktoken encoding for a model, with caching."""
|
|
33
|
+
if not TIKTOKEN_AVAILABLE:
|
|
34
|
+
return None
|
|
35
|
+
|
|
36
|
+
# Map model IDs to encoding names
|
|
37
|
+
if "claude" in model_id.lower() or "anthropic" in model_id.lower():
|
|
38
|
+
# Claude uses cl100k_base-like encoding
|
|
39
|
+
return tiktoken.get_encoding("cl100k_base")
|
|
40
|
+
if "gpt-4" in model_id.lower() or "gpt-3.5" in model_id.lower() or "o1" in model_id.lower():
|
|
41
|
+
return tiktoken.encoding_for_model("gpt-4")
|
|
42
|
+
# Default to cl100k_base for unknown models
|
|
43
|
+
return tiktoken.get_encoding("cl100k_base")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def estimate_tokens(text: str, model_id: str = "claude-sonnet-4-5-20250514") -> int:
|
|
47
|
+
"""Estimate token count for text using accurate token counting.
|
|
48
|
+
|
|
49
|
+
Uses empathy_llm_toolkit's token counter which leverages tiktoken for fast,
|
|
50
|
+
accurate local counting (~98% accurate). Falls back to heuristic if unavailable.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
text: The text to count tokens for
|
|
54
|
+
model_id: The model ID to use for encoding selection
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
Accurate token count
|
|
58
|
+
|
|
59
|
+
Raises:
|
|
60
|
+
ValueError: If model_id is empty
|
|
61
|
+
|
|
62
|
+
"""
|
|
63
|
+
# Pattern 1: String ID validation
|
|
64
|
+
if not model_id or not model_id.strip():
|
|
65
|
+
raise ValueError("model_id cannot be empty")
|
|
66
|
+
|
|
67
|
+
if not text:
|
|
68
|
+
return 0
|
|
69
|
+
|
|
70
|
+
# Use new accurate token counting from attune_llm
|
|
71
|
+
try:
|
|
72
|
+
from attune_llm.utils.tokens import count_tokens
|
|
73
|
+
|
|
74
|
+
return count_tokens(text, model=model_id, use_api=False)
|
|
75
|
+
except ImportError:
|
|
76
|
+
# Fallback to tiktoken if toolkit not available
|
|
77
|
+
if TIKTOKEN_AVAILABLE:
|
|
78
|
+
try:
|
|
79
|
+
encoding = _get_encoding(model_id)
|
|
80
|
+
if encoding:
|
|
81
|
+
return len(encoding.encode(text))
|
|
82
|
+
except Exception:
|
|
83
|
+
pass # Fall through to heuristic
|
|
84
|
+
|
|
85
|
+
# Last resort: heuristic fallback
|
|
86
|
+
return max(1, int(len(text) * TOKENS_PER_CHAR_HEURISTIC))
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def estimate_workflow_cost(
|
|
90
|
+
workflow_name: str,
|
|
91
|
+
input_text: str,
|
|
92
|
+
provider: str = "anthropic",
|
|
93
|
+
target_path: str | None = None,
|
|
94
|
+
) -> dict[str, Any]:
|
|
95
|
+
"""Estimate total workflow cost before execution.
|
|
96
|
+
|
|
97
|
+
Analyzes workflow stages and estimates token usage and cost for each,
|
|
98
|
+
providing a cost range for the full workflow.
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
workflow_name: Name of the workflow (e.g., "security-audit", "test-gen")
|
|
102
|
+
input_text: The input text/code to be processed
|
|
103
|
+
provider: LLM provider (anthropic, openai, ollama, hybrid)
|
|
104
|
+
target_path: Optional path for file-based workflows
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
Dictionary with cost estimates:
|
|
108
|
+
{
|
|
109
|
+
"workflow": str,
|
|
110
|
+
"provider": str,
|
|
111
|
+
"input_tokens": int,
|
|
112
|
+
"stages": [...],
|
|
113
|
+
"total_min": float,
|
|
114
|
+
"total_max": float,
|
|
115
|
+
"display": str,
|
|
116
|
+
"risk": "low" | "medium" | "high"
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
Raises:
|
|
120
|
+
ValueError: If workflow_name or provider is empty
|
|
121
|
+
|
|
122
|
+
"""
|
|
123
|
+
from .registry import get_model, get_supported_providers
|
|
124
|
+
|
|
125
|
+
# Pattern 1: String ID validation
|
|
126
|
+
if not workflow_name or not workflow_name.strip():
|
|
127
|
+
raise ValueError("workflow_name cannot be empty")
|
|
128
|
+
if not provider or not provider.strip():
|
|
129
|
+
raise ValueError("provider cannot be empty")
|
|
130
|
+
|
|
131
|
+
# Validate provider
|
|
132
|
+
if provider not in get_supported_providers():
|
|
133
|
+
provider = "anthropic" # Default fallback
|
|
134
|
+
|
|
135
|
+
# Workflow stage configurations by workflow name
|
|
136
|
+
# Based on actual workflow implementations
|
|
137
|
+
WORKFLOW_STAGES = {
|
|
138
|
+
"security-audit": [
|
|
139
|
+
{"name": "identify_vulnerabilities", "tier": "capable"},
|
|
140
|
+
{"name": "analyze_risk", "tier": "capable"},
|
|
141
|
+
{"name": "generate_report", "tier": "cheap"},
|
|
142
|
+
],
|
|
143
|
+
"code-review": [
|
|
144
|
+
{"name": "analyze_code", "tier": "capable"},
|
|
145
|
+
{"name": "generate_feedback", "tier": "capable"},
|
|
146
|
+
{"name": "summarize", "tier": "cheap"},
|
|
147
|
+
],
|
|
148
|
+
"test-gen": [
|
|
149
|
+
{"name": "analyze_code", "tier": "capable"},
|
|
150
|
+
{"name": "generate_tests", "tier": "capable"},
|
|
151
|
+
{"name": "review_tests", "tier": "cheap"},
|
|
152
|
+
],
|
|
153
|
+
"doc-gen": [
|
|
154
|
+
{"name": "analyze_structure", "tier": "cheap"},
|
|
155
|
+
{"name": "generate_documentation", "tier": "capable"},
|
|
156
|
+
],
|
|
157
|
+
"bug-predict": [
|
|
158
|
+
{"name": "analyze_patterns", "tier": "capable"},
|
|
159
|
+
{"name": "predict_risks", "tier": "capable"},
|
|
160
|
+
],
|
|
161
|
+
"refactor-plan": [
|
|
162
|
+
{"name": "identify_issues", "tier": "capable"},
|
|
163
|
+
{"name": "plan_refactoring", "tier": "capable"},
|
|
164
|
+
{"name": "review_plan", "tier": "cheap"},
|
|
165
|
+
],
|
|
166
|
+
"perf-audit": [
|
|
167
|
+
{"name": "analyze_performance", "tier": "capable"},
|
|
168
|
+
{"name": "identify_bottlenecks", "tier": "capable"},
|
|
169
|
+
{"name": "generate_recommendations", "tier": "cheap"},
|
|
170
|
+
],
|
|
171
|
+
"health-check": [
|
|
172
|
+
{"name": "scan_codebase", "tier": "cheap"},
|
|
173
|
+
{"name": "analyze_health", "tier": "capable"},
|
|
174
|
+
{"name": "generate_report", "tier": "cheap"},
|
|
175
|
+
],
|
|
176
|
+
"pr-review": [
|
|
177
|
+
{"name": "analyze_diff", "tier": "capable"},
|
|
178
|
+
{"name": "check_quality", "tier": "capable"},
|
|
179
|
+
{"name": "generate_review", "tier": "capable"},
|
|
180
|
+
],
|
|
181
|
+
"pro-review": [
|
|
182
|
+
{"name": "deep_analysis", "tier": "premium"},
|
|
183
|
+
{"name": "generate_insights", "tier": "capable"},
|
|
184
|
+
],
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
# Get stage configuration for this workflow
|
|
188
|
+
stages_config = WORKFLOW_STAGES.get(
|
|
189
|
+
workflow_name,
|
|
190
|
+
[
|
|
191
|
+
{"name": "analyze", "tier": "capable"},
|
|
192
|
+
{"name": "generate", "tier": "capable"},
|
|
193
|
+
{"name": "review", "tier": "cheap"},
|
|
194
|
+
],
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
# Estimate input tokens
|
|
198
|
+
input_tokens = estimate_tokens(input_text)
|
|
199
|
+
|
|
200
|
+
# If we have a target path, estimate additional content
|
|
201
|
+
if target_path:
|
|
202
|
+
try:
|
|
203
|
+
import os
|
|
204
|
+
|
|
205
|
+
# Validate path to prevent path traversal attacks
|
|
206
|
+
validated_target = _validate_file_path(target_path)
|
|
207
|
+
|
|
208
|
+
if os.path.isfile(validated_target):
|
|
209
|
+
with open(validated_target, encoding="utf-8", errors="ignore") as f:
|
|
210
|
+
file_content = f.read()
|
|
211
|
+
input_tokens += estimate_tokens(file_content)
|
|
212
|
+
elif os.path.isdir(validated_target):
|
|
213
|
+
# Estimate based on directory size (rough heuristic)
|
|
214
|
+
total_chars = 0
|
|
215
|
+
for root, _, files in os.walk(validated_target):
|
|
216
|
+
for file in files[:50]: # Limit to first 50 files
|
|
217
|
+
if file.endswith((".py", ".js", ".ts", ".tsx", ".jsx")):
|
|
218
|
+
try:
|
|
219
|
+
filepath = os.path.join(root, file)
|
|
220
|
+
validated_filepath = _validate_file_path(filepath)
|
|
221
|
+
with open(
|
|
222
|
+
validated_filepath, encoding="utf-8", errors="ignore"
|
|
223
|
+
) as f:
|
|
224
|
+
total_chars += len(f.read())
|
|
225
|
+
except (ValueError, OSError):
|
|
226
|
+
pass
|
|
227
|
+
input_tokens += int(total_chars * TOKENS_PER_CHAR_HEURISTIC)
|
|
228
|
+
except (ValueError, OSError):
|
|
229
|
+
pass # Keep original estimate
|
|
230
|
+
|
|
231
|
+
# Output multipliers by stage type
|
|
232
|
+
output_multipliers = {
|
|
233
|
+
"identify": 0.3,
|
|
234
|
+
"analyze": 0.8,
|
|
235
|
+
"generate": 2.0,
|
|
236
|
+
"review": 0.5,
|
|
237
|
+
"summarize": 0.3,
|
|
238
|
+
"fix": 1.5,
|
|
239
|
+
"test": 1.5,
|
|
240
|
+
"document": 1.0,
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
estimates = []
|
|
244
|
+
total_min = 0.0
|
|
245
|
+
total_max = 0.0
|
|
246
|
+
|
|
247
|
+
for stage in stages_config:
|
|
248
|
+
stage_name = stage.get("name", "unknown")
|
|
249
|
+
tier = stage.get("tier", "capable")
|
|
250
|
+
|
|
251
|
+
# Get model for this tier
|
|
252
|
+
try:
|
|
253
|
+
model_info = get_model(provider, tier)
|
|
254
|
+
except Exception:
|
|
255
|
+
# Fallback to capable tier
|
|
256
|
+
model_info = get_model(provider, "capable")
|
|
257
|
+
|
|
258
|
+
if model_info is None:
|
|
259
|
+
# Skip stage if no model available
|
|
260
|
+
continue
|
|
261
|
+
|
|
262
|
+
# Estimate output tokens based on stage type
|
|
263
|
+
multiplier = 1.0
|
|
264
|
+
for stage_type, mult in output_multipliers.items():
|
|
265
|
+
if stage_type in stage_name.lower():
|
|
266
|
+
multiplier = mult
|
|
267
|
+
break
|
|
268
|
+
|
|
269
|
+
est_output = int(input_tokens * multiplier)
|
|
270
|
+
|
|
271
|
+
# Calculate cost
|
|
272
|
+
cost = (input_tokens / 1_000_000) * model_info.input_cost_per_million + (
|
|
273
|
+
est_output / 1_000_000
|
|
274
|
+
) * model_info.output_cost_per_million
|
|
275
|
+
|
|
276
|
+
estimates.append(
|
|
277
|
+
{
|
|
278
|
+
"stage": stage_name,
|
|
279
|
+
"tier": tier,
|
|
280
|
+
"model": model_info.id,
|
|
281
|
+
"estimated_input_tokens": input_tokens,
|
|
282
|
+
"estimated_output_tokens": est_output,
|
|
283
|
+
"estimated_cost": round(cost, 6),
|
|
284
|
+
},
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
# Accumulate with variance (80% - 120%)
|
|
288
|
+
total_min += cost * 0.8
|
|
289
|
+
total_max += cost * 1.2
|
|
290
|
+
|
|
291
|
+
# Determine risk level
|
|
292
|
+
if total_max > 1.0:
|
|
293
|
+
risk = "high"
|
|
294
|
+
elif total_max > 0.10:
|
|
295
|
+
risk = "medium"
|
|
296
|
+
else:
|
|
297
|
+
risk = "low"
|
|
298
|
+
|
|
299
|
+
return {
|
|
300
|
+
"workflow": workflow_name,
|
|
301
|
+
"provider": provider,
|
|
302
|
+
"input_tokens": input_tokens,
|
|
303
|
+
"stages": estimates,
|
|
304
|
+
"total_min": round(total_min, 4),
|
|
305
|
+
"total_max": round(total_max, 4),
|
|
306
|
+
"display": f"${total_min:.3f} - ${total_max:.3f}",
|
|
307
|
+
"risk": risk,
|
|
308
|
+
"tiktoken_available": TIKTOKEN_AVAILABLE,
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
def estimate_single_call_cost(
|
|
313
|
+
text: str,
|
|
314
|
+
task_type: str,
|
|
315
|
+
provider: str = "anthropic",
|
|
316
|
+
) -> dict[str, Any]:
|
|
317
|
+
"""Estimate cost for a single LLM call.
|
|
318
|
+
|
|
319
|
+
Args:
|
|
320
|
+
text: Input text
|
|
321
|
+
task_type: Type of task (e.g., "summarize", "generate_code")
|
|
322
|
+
provider: LLM provider
|
|
323
|
+
|
|
324
|
+
Returns:
|
|
325
|
+
Cost estimate dictionary
|
|
326
|
+
|
|
327
|
+
Raises:
|
|
328
|
+
ValueError: If task_type or provider is empty
|
|
329
|
+
|
|
330
|
+
"""
|
|
331
|
+
from .registry import get_model
|
|
332
|
+
from .tasks import get_tier_for_task
|
|
333
|
+
|
|
334
|
+
# Pattern 1: String ID validation
|
|
335
|
+
if not task_type or not task_type.strip():
|
|
336
|
+
raise ValueError("task_type cannot be empty")
|
|
337
|
+
if not provider or not provider.strip():
|
|
338
|
+
raise ValueError("provider cannot be empty")
|
|
339
|
+
|
|
340
|
+
input_tokens = estimate_tokens(text)
|
|
341
|
+
|
|
342
|
+
# Get tier for task
|
|
343
|
+
tier = get_tier_for_task(task_type)
|
|
344
|
+
model_info = get_model(provider, tier.value)
|
|
345
|
+
|
|
346
|
+
if model_info is None:
|
|
347
|
+
# Return a fallback estimate if no model available
|
|
348
|
+
return {
|
|
349
|
+
"task_type": task_type,
|
|
350
|
+
"tier": tier.value,
|
|
351
|
+
"model": "unknown",
|
|
352
|
+
"provider": provider,
|
|
353
|
+
"input_tokens": input_tokens,
|
|
354
|
+
"estimated_output_tokens": input_tokens,
|
|
355
|
+
"estimated_cost": 0.0,
|
|
356
|
+
"display": "$0.0000",
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
# Estimate output based on task type
|
|
360
|
+
output_multipliers = {
|
|
361
|
+
"summarize": 0.3,
|
|
362
|
+
"classify": 0.1,
|
|
363
|
+
"generate_code": 1.5,
|
|
364
|
+
"fix_bug": 1.2,
|
|
365
|
+
"review": 0.5,
|
|
366
|
+
"document": 0.8,
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
multiplier = output_multipliers.get(task_type, 1.0)
|
|
370
|
+
est_output = int(input_tokens * multiplier)
|
|
371
|
+
|
|
372
|
+
cost = (input_tokens / 1_000_000) * model_info.input_cost_per_million + (
|
|
373
|
+
est_output / 1_000_000
|
|
374
|
+
) * model_info.output_cost_per_million
|
|
375
|
+
|
|
376
|
+
return {
|
|
377
|
+
"task_type": task_type,
|
|
378
|
+
"tier": tier.value,
|
|
379
|
+
"model": model_info.id,
|
|
380
|
+
"provider": provider,
|
|
381
|
+
"input_tokens": input_tokens,
|
|
382
|
+
"estimated_output_tokens": est_output,
|
|
383
|
+
"estimated_cost": round(cost, 6),
|
|
384
|
+
"display": f"${cost:.4f}",
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
# CLI support
|
|
389
|
+
if __name__ == "__main__":
|
|
390
|
+
import argparse
|
|
391
|
+
import json
|
|
392
|
+
import sys
|
|
393
|
+
|
|
394
|
+
parser = argparse.ArgumentParser(description="Estimate workflow costs")
|
|
395
|
+
parser.add_argument("workflow", help="Workflow name (e.g., security-audit)")
|
|
396
|
+
parser.add_argument("--input", "-i", help="Input text or file path")
|
|
397
|
+
parser.add_argument("--provider", "-p", default="anthropic", help="LLM provider")
|
|
398
|
+
parser.add_argument("--target", "-t", help="Target path for file-based workflows")
|
|
399
|
+
|
|
400
|
+
args = parser.parse_args()
|
|
401
|
+
|
|
402
|
+
# Read input
|
|
403
|
+
input_text = ""
|
|
404
|
+
if args.input:
|
|
405
|
+
try:
|
|
406
|
+
validated_input = _validate_file_path(args.input)
|
|
407
|
+
with open(validated_input) as f:
|
|
408
|
+
input_text = f.read()
|
|
409
|
+
except (FileNotFoundError, ValueError):
|
|
410
|
+
input_text = args.input
|
|
411
|
+
|
|
412
|
+
result = estimate_workflow_cost(
|
|
413
|
+
workflow_name=args.workflow,
|
|
414
|
+
input_text=input_text,
|
|
415
|
+
provider=args.provider,
|
|
416
|
+
target_path=args.target,
|
|
417
|
+
)
|
|
418
|
+
|
|
419
|
+
print(json.dumps(result, indent=2))
|
|
420
|
+
sys.exit(0)
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
"""Configuration Validation for Multi-Model Workflows
|
|
2
|
+
|
|
3
|
+
Provides schema validation for workflow configurations:
|
|
4
|
+
- Required field validation
|
|
5
|
+
- Type checking
|
|
6
|
+
- Value range validation
|
|
7
|
+
- Provider/tier existence checks
|
|
8
|
+
|
|
9
|
+
Copyright 2025 Smart-AI-Memory
|
|
10
|
+
Licensed under Fair Source License 0.9
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from dataclasses import dataclass, field
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from attune.config import _validate_file_path
|
|
17
|
+
|
|
18
|
+
from .registry import MODEL_REGISTRY, ModelTier
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class ValidationError:
|
|
23
|
+
"""A single validation error."""
|
|
24
|
+
|
|
25
|
+
path: str
|
|
26
|
+
message: str
|
|
27
|
+
severity: str = "error" # "error" | "warning"
|
|
28
|
+
|
|
29
|
+
def __str__(self) -> str:
|
|
30
|
+
return f"[{self.severity.upper()}] {self.path}: {self.message}"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass
|
|
34
|
+
class ValidationResult:
|
|
35
|
+
"""Result of configuration validation."""
|
|
36
|
+
|
|
37
|
+
valid: bool
|
|
38
|
+
errors: list[ValidationError] = field(default_factory=list)
|
|
39
|
+
warnings: list[ValidationError] = field(default_factory=list)
|
|
40
|
+
|
|
41
|
+
def add_error(self, path: str, message: str) -> None:
|
|
42
|
+
"""Add an error."""
|
|
43
|
+
self.errors.append(ValidationError(path, message, "error"))
|
|
44
|
+
self.valid = False
|
|
45
|
+
|
|
46
|
+
def add_warning(self, path: str, message: str) -> None:
|
|
47
|
+
"""Add a warning."""
|
|
48
|
+
self.warnings.append(ValidationError(path, message, "warning"))
|
|
49
|
+
|
|
50
|
+
def __str__(self) -> str:
|
|
51
|
+
lines = []
|
|
52
|
+
if self.valid:
|
|
53
|
+
lines.append("Configuration is valid")
|
|
54
|
+
else:
|
|
55
|
+
lines.append("Configuration has errors")
|
|
56
|
+
|
|
57
|
+
for error in self.errors:
|
|
58
|
+
lines.append(f" {error}")
|
|
59
|
+
for warning in self.warnings:
|
|
60
|
+
lines.append(f" {warning}")
|
|
61
|
+
|
|
62
|
+
return "\n".join(lines)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class ConfigValidator:
|
|
66
|
+
"""Validator for multi-model workflow configurations.
|
|
67
|
+
|
|
68
|
+
Validates:
|
|
69
|
+
- Provider names exist in registry
|
|
70
|
+
- Tier names are valid
|
|
71
|
+
- Required fields are present
|
|
72
|
+
- Numeric values are in valid ranges
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
# Valid provider names from registry
|
|
76
|
+
VALID_PROVIDERS = set(MODEL_REGISTRY.keys())
|
|
77
|
+
|
|
78
|
+
# Valid tier names
|
|
79
|
+
VALID_TIERS = {tier.value for tier in ModelTier}
|
|
80
|
+
|
|
81
|
+
# Schema for workflow config
|
|
82
|
+
WORKFLOW_SCHEMA = {
|
|
83
|
+
"name": {"type": str, "required": True},
|
|
84
|
+
"description": {"type": str, "required": False},
|
|
85
|
+
"default_provider": {"type": str, "required": False},
|
|
86
|
+
"stages": {"type": list, "required": False},
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
# Schema for stage config
|
|
90
|
+
STAGE_SCHEMA = {
|
|
91
|
+
"name": {"type": str, "required": True},
|
|
92
|
+
"tier": {"type": str, "required": True},
|
|
93
|
+
"provider": {"type": str, "required": False},
|
|
94
|
+
"timeout_ms": {"type": int, "required": False, "min": 0, "max": 600000},
|
|
95
|
+
"max_retries": {"type": int, "required": False, "min": 0, "max": 10},
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
def validate_workflow_config(self, config: dict[str, Any]) -> ValidationResult:
|
|
99
|
+
"""Validate a workflow configuration dictionary.
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
config: Workflow configuration dict
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
ValidationResult with any errors or warnings
|
|
106
|
+
|
|
107
|
+
"""
|
|
108
|
+
result = ValidationResult(valid=True)
|
|
109
|
+
|
|
110
|
+
# Check required fields
|
|
111
|
+
for field_name, spec in self.WORKFLOW_SCHEMA.items():
|
|
112
|
+
if spec.get("required") and field_name not in config:
|
|
113
|
+
result.add_error(field_name, f"Required field '{field_name}' is missing")
|
|
114
|
+
|
|
115
|
+
# Validate types
|
|
116
|
+
for field_name, value in config.items():
|
|
117
|
+
if field_name in self.WORKFLOW_SCHEMA:
|
|
118
|
+
spec = self.WORKFLOW_SCHEMA[field_name]
|
|
119
|
+
expected_type = spec.get("type")
|
|
120
|
+
if expected_type is not None:
|
|
121
|
+
# Cast to type for isinstance check
|
|
122
|
+
type_cls = (
|
|
123
|
+
expected_type if isinstance(expected_type, type) else type(expected_type)
|
|
124
|
+
)
|
|
125
|
+
if not isinstance(value, type_cls):
|
|
126
|
+
type_name = getattr(type_cls, "__name__", str(type_cls))
|
|
127
|
+
result.add_error(
|
|
128
|
+
field_name,
|
|
129
|
+
f"Expected {type_name}, got {type(value).__name__}",
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
# Validate default_provider
|
|
133
|
+
if "default_provider" in config:
|
|
134
|
+
provider = config["default_provider"]
|
|
135
|
+
if provider not in self.VALID_PROVIDERS:
|
|
136
|
+
result.add_error(
|
|
137
|
+
"default_provider",
|
|
138
|
+
f"Unknown provider '{provider}'. Valid: {sorted(self.VALID_PROVIDERS)}",
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
# Validate stages
|
|
142
|
+
if "stages" in config and isinstance(config["stages"], list):
|
|
143
|
+
for i, stage in enumerate(config["stages"]):
|
|
144
|
+
stage_path = f"stages[{i}]"
|
|
145
|
+
self._validate_stage(stage, stage_path, result)
|
|
146
|
+
|
|
147
|
+
return result
|
|
148
|
+
|
|
149
|
+
def _validate_stage(self, stage: dict[str, Any], path: str, result: ValidationResult) -> None:
|
|
150
|
+
"""Validate a single stage configuration."""
|
|
151
|
+
if not isinstance(stage, dict):
|
|
152
|
+
result.add_error(path, "Stage must be a dictionary")
|
|
153
|
+
return
|
|
154
|
+
|
|
155
|
+
# Check required fields
|
|
156
|
+
for field_name, spec in self.STAGE_SCHEMA.items():
|
|
157
|
+
if spec.get("required") and field_name not in stage:
|
|
158
|
+
result.add_error(f"{path}.{field_name}", "Required field is missing")
|
|
159
|
+
|
|
160
|
+
# Validate tier
|
|
161
|
+
if "tier" in stage:
|
|
162
|
+
tier = stage["tier"]
|
|
163
|
+
if tier not in self.VALID_TIERS:
|
|
164
|
+
result.add_error(
|
|
165
|
+
f"{path}.tier",
|
|
166
|
+
f"Unknown tier '{tier}'. Valid: {sorted(self.VALID_TIERS)}",
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
# Validate provider
|
|
170
|
+
if "provider" in stage:
|
|
171
|
+
provider = stage["provider"]
|
|
172
|
+
if provider not in self.VALID_PROVIDERS:
|
|
173
|
+
result.add_error(
|
|
174
|
+
f"{path}.provider",
|
|
175
|
+
f"Unknown provider '{provider}'. Valid: {sorted(self.VALID_PROVIDERS)}",
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
# Validate numeric ranges
|
|
179
|
+
for field_name in ["timeout_ms", "max_retries"]:
|
|
180
|
+
if field_name in stage:
|
|
181
|
+
value = stage[field_name]
|
|
182
|
+
spec = self.STAGE_SCHEMA[field_name]
|
|
183
|
+
|
|
184
|
+
if not isinstance(value, int):
|
|
185
|
+
result.add_error(
|
|
186
|
+
f"{path}.{field_name}",
|
|
187
|
+
f"Expected integer, got {type(value).__name__}",
|
|
188
|
+
)
|
|
189
|
+
else:
|
|
190
|
+
min_val = spec.get("min")
|
|
191
|
+
max_val = spec.get("max")
|
|
192
|
+
if isinstance(min_val, int | float) and value < min_val:
|
|
193
|
+
result.add_error(
|
|
194
|
+
f"{path}.{field_name}",
|
|
195
|
+
f"Value {value} below minimum {min_val}",
|
|
196
|
+
)
|
|
197
|
+
if isinstance(max_val, int | float) and value > max_val:
|
|
198
|
+
result.add_error(
|
|
199
|
+
f"{path}.{field_name}",
|
|
200
|
+
f"Value {value} above maximum {max_val}",
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
def validate_provider_tier(self, provider: str, tier: str) -> ValidationResult:
|
|
204
|
+
"""Validate that a provider/tier combination exists.
|
|
205
|
+
|
|
206
|
+
Args:
|
|
207
|
+
provider: Provider name
|
|
208
|
+
tier: Tier name
|
|
209
|
+
|
|
210
|
+
Returns:
|
|
211
|
+
ValidationResult
|
|
212
|
+
|
|
213
|
+
"""
|
|
214
|
+
result = ValidationResult(valid=True)
|
|
215
|
+
|
|
216
|
+
if provider not in self.VALID_PROVIDERS:
|
|
217
|
+
result.add_error("provider", f"Unknown provider '{provider}'")
|
|
218
|
+
return result
|
|
219
|
+
|
|
220
|
+
if tier not in self.VALID_TIERS:
|
|
221
|
+
result.add_error("tier", f"Unknown tier '{tier}'")
|
|
222
|
+
return result
|
|
223
|
+
|
|
224
|
+
# Check if combination exists in registry
|
|
225
|
+
if tier not in MODEL_REGISTRY.get(provider, {}):
|
|
226
|
+
result.add_warning(
|
|
227
|
+
"provider_tier",
|
|
228
|
+
f"Provider '{provider}' may not have tier '{tier}' configured",
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
return result
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def validate_config(config: dict[str, Any]) -> ValidationResult:
|
|
235
|
+
"""Convenience function to validate a workflow config.
|
|
236
|
+
|
|
237
|
+
Args:
|
|
238
|
+
config: Configuration dictionary
|
|
239
|
+
|
|
240
|
+
Returns:
|
|
241
|
+
ValidationResult
|
|
242
|
+
|
|
243
|
+
"""
|
|
244
|
+
validator = ConfigValidator()
|
|
245
|
+
return validator.validate_workflow_config(config)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def validate_yaml_file(file_path: str) -> ValidationResult:
|
|
249
|
+
"""Validate a YAML configuration file.
|
|
250
|
+
|
|
251
|
+
Args:
|
|
252
|
+
file_path: Path to YAML file
|
|
253
|
+
|
|
254
|
+
Returns:
|
|
255
|
+
ValidationResult
|
|
256
|
+
|
|
257
|
+
"""
|
|
258
|
+
import yaml
|
|
259
|
+
|
|
260
|
+
result = ValidationResult(valid=True)
|
|
261
|
+
|
|
262
|
+
try:
|
|
263
|
+
validated_path = _validate_file_path(str(file_path))
|
|
264
|
+
with open(validated_path) as f:
|
|
265
|
+
config = yaml.safe_load(f)
|
|
266
|
+
except FileNotFoundError:
|
|
267
|
+
result.add_error("file", f"File not found: {file_path}")
|
|
268
|
+
return result
|
|
269
|
+
except ValueError as e:
|
|
270
|
+
result.add_error("file", f"Invalid file path: {e}")
|
|
271
|
+
return result
|
|
272
|
+
except yaml.YAMLError as e:
|
|
273
|
+
result.add_error("yaml", f"Invalid YAML: {e}")
|
|
274
|
+
return result
|
|
275
|
+
|
|
276
|
+
if config is None:
|
|
277
|
+
result.add_error("file", "Empty configuration file")
|
|
278
|
+
return result
|
|
279
|
+
|
|
280
|
+
return validate_config(config)
|