MaiBot 1.3.8__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.
- maibot-1.3.8.dist-info/METADATA +245 -0
- maibot-1.3.8.dist-info/RECORD +716 -0
- maibot-1.3.8.dist-info/WHEEL +5 -0
- maibot-1.3.8.dist-info/entry_points.txt +2 -0
- maibot-1.3.8.dist-info/licenses/LICENSE +674 -0
- maibot-1.3.8.dist-info/top_level.txt +1 -0
- src/A_memorix/LICENSE +661 -0
- src/A_memorix/LICENSE-MAIBOT-GPL.md +22 -0
- src/A_memorix/__init__.py +5 -0
- src/A_memorix/config_schema.json +1640 -0
- src/A_memorix/core/__init__.py +83 -0
- src/A_memorix/core/embedding/__init__.py +17 -0
- src/A_memorix/core/embedding/api_adapter.py +650 -0
- src/A_memorix/core/embedding/manager.py +506 -0
- src/A_memorix/core/embedding/presets.py +68 -0
- src/A_memorix/core/image/__init__.py +6 -0
- src/A_memorix/core/image/asset_store.py +141 -0
- src/A_memorix/core/image/component_paths.py +36 -0
- src/A_memorix/core/image/embedder.py +129 -0
- src/A_memorix/core/image/fingerprints.py +36 -0
- src/A_memorix/core/image/runtime.py +900 -0
- src/A_memorix/core/retrieval/__init__.py +66 -0
- src/A_memorix/core/retrieval/dual_path.py +3272 -0
- src/A_memorix/core/retrieval/graph_relation_recall.py +285 -0
- src/A_memorix/core/retrieval/pagerank.py +471 -0
- src/A_memorix/core/retrieval/posterior_graph.py +787 -0
- src/A_memorix/core/retrieval/score_calibration.py +180 -0
- src/A_memorix/core/retrieval/sparse_bm25.py +717 -0
- src/A_memorix/core/retrieval/threshold.py +404 -0
- src/A_memorix/core/runtime/__init__.py +26 -0
- src/A_memorix/core/runtime/admin_contracts.py +293 -0
- src/A_memorix/core/runtime/kernel_compat.py +269 -0
- src/A_memorix/core/runtime/lifecycle_orchestrator.py +368 -0
- src/A_memorix/core/runtime/models.py +27 -0
- src/A_memorix/core/runtime/runtime_facade.py +141 -0
- src/A_memorix/core/runtime/runtime_writer_lock.py +105 -0
- src/A_memorix/core/runtime/sdk_memory_kernel.py +1907 -0
- src/A_memorix/core/runtime/search_runtime_initializer.py +290 -0
- src/A_memorix/core/runtime/services/__init__.py +63 -0
- src/A_memorix/core/runtime/services/background_task_service.py +477 -0
- src/A_memorix/core/runtime/services/base.py +28 -0
- src/A_memorix/core/runtime/services/bundle_admin_service.py +2200 -0
- src/A_memorix/core/runtime/services/chat_filter_service.py +88 -0
- src/A_memorix/core/runtime/services/correction_admin_service.py +1274 -0
- src/A_memorix/core/runtime/services/delete_admin_service.py +2018 -0
- src/A_memorix/core/runtime/services/dual_vector_migration_service.py +109 -0
- src/A_memorix/core/runtime/services/dual_vector_state_service.py +503 -0
- src/A_memorix/core/runtime/services/embedding_state_service.py +515 -0
- src/A_memorix/core/runtime/services/episode_admin_service.py +104 -0
- src/A_memorix/core/runtime/services/fact_admin_service.py +199 -0
- src/A_memorix/core/runtime/services/feedback_correction_service.py +1523 -0
- src/A_memorix/core/runtime/services/graph_admin_service.py +1521 -0
- src/A_memorix/core/runtime/services/image_service.py +158 -0
- src/A_memorix/core/runtime/services/import_tuning_admin_service.py +158 -0
- src/A_memorix/core/runtime/services/ingest_service.py +787 -0
- src/A_memorix/core/runtime/services/memory_maintenance_service.py +404 -0
- src/A_memorix/core/runtime/services/memory_search_service.py +373 -0
- src/A_memorix/core/runtime/services/profile_admin_service.py +646 -0
- src/A_memorix/core/runtime/services/request_dedup_service.py +34 -0
- src/A_memorix/core/runtime/services/runtime_config_service.py +94 -0
- src/A_memorix/core/runtime/services/runtime_dependency_service.py +105 -0
- src/A_memorix/core/runtime/services/runtime_lifecycle_service.py +409 -0
- src/A_memorix/core/runtime/services/search_hit_processing_service.py +891 -0
- src/A_memorix/core/runtime/services/source_admin_service.py +52 -0
- src/A_memorix/core/runtime/services/stats_service.py +35 -0
- src/A_memorix/core/runtime/services/summary_service.py +50 -0
- src/A_memorix/core/runtime/services/v5_admin_service.py +326 -0
- src/A_memorix/core/runtime/services/vector_delete_service.py +30 -0
- src/A_memorix/core/runtime/services/vector_rebuild_checkpoint.py +65 -0
- src/A_memorix/core/runtime/services/vector_recovery_service.py +460 -0
- src/A_memorix/core/runtime/services/vector_runtime_service.py +1417 -0
- src/A_memorix/core/runtime/services/vector_space_service.py +251 -0
- src/A_memorix/core/storage/__init__.py +62 -0
- src/A_memorix/core/storage/format_migration.py +676 -0
- src/A_memorix/core/storage/graph_store.py +1733 -0
- src/A_memorix/core/storage/knowledge_types.py +183 -0
- src/A_memorix/core/storage/metadata_cleanup.py +913 -0
- src/A_memorix/core/storage/metadata_episode.py +1659 -0
- src/A_memorix/core/storage/metadata_fact.py +1410 -0
- src/A_memorix/core/storage/metadata_feedback.py +745 -0
- src/A_memorix/core/storage/metadata_fts.py +937 -0
- src/A_memorix/core/storage/metadata_image.py +1076 -0
- src/A_memorix/core/storage/metadata_profile.py +782 -0
- src/A_memorix/core/storage/metadata_schema.py +2503 -0
- src/A_memorix/core/storage/metadata_store.py +4565 -0
- src/A_memorix/core/storage/sqlite_connection.py +176 -0
- src/A_memorix/core/storage/tokenizer_runtime.py +9 -0
- src/A_memorix/core/storage/transaction.py +42 -0
- src/A_memorix/core/storage/type_detection.py +137 -0
- src/A_memorix/core/storage/value_coercion.py +10 -0
- src/A_memorix/core/storage/vector_store.py +2851 -0
- src/A_memorix/core/strategies/__init__.py +0 -0
- src/A_memorix/core/strategies/base.py +92 -0
- src/A_memorix/core/strategies/chat_log.py +80 -0
- src/A_memorix/core/strategies/factual.py +104 -0
- src/A_memorix/core/strategies/narrative.py +148 -0
- src/A_memorix/core/strategies/quote.py +47 -0
- src/A_memorix/core/utils/__init__.py +33 -0
- src/A_memorix/core/utils/aggregate_query_service.py +359 -0
- src/A_memorix/core/utils/episode_retrieval_service.py +222 -0
- src/A_memorix/core/utils/episode_segmentation_service.py +322 -0
- src/A_memorix/core/utils/episode_service.py +646 -0
- src/A_memorix/core/utils/feedback_policy.py +149 -0
- src/A_memorix/core/utils/hash.py +128 -0
- src/A_memorix/core/utils/import_payloads.py +222 -0
- src/A_memorix/core/utils/io.py +85 -0
- src/A_memorix/core/utils/matcher.py +175 -0
- src/A_memorix/core/utils/memory_lifecycle_policy.py +336 -0
- src/A_memorix/core/utils/metadata.py +11 -0
- src/A_memorix/core/utils/model_routing.py +239 -0
- src/A_memorix/core/utils/monitor.py +188 -0
- src/A_memorix/core/utils/path_fallback_service.py +182 -0
- src/A_memorix/core/utils/person_profile_service.py +1277 -0
- src/A_memorix/core/utils/plugin_id_policy.py +26 -0
- src/A_memorix/core/utils/profile_evidence.py +25 -0
- src/A_memorix/core/utils/profile_policy.py +45 -0
- src/A_memorix/core/utils/profile_text.py +204 -0
- src/A_memorix/core/utils/quantization.py +343 -0
- src/A_memorix/core/utils/relation_query.py +121 -0
- src/A_memorix/core/utils/relation_write_service.py +504 -0
- src/A_memorix/core/utils/retrieval_tuning_manager.py +2369 -0
- src/A_memorix/core/utils/runtime_payloads.py +132 -0
- src/A_memorix/core/utils/runtime_self_check.py +303 -0
- src/A_memorix/core/utils/search_execution_service.py +445 -0
- src/A_memorix/core/utils/search_postprocess.py +91 -0
- src/A_memorix/core/utils/summary_importer.py +991 -0
- src/A_memorix/core/utils/time_parser.py +156 -0
- src/A_memorix/core/utils/web_import_manager.py +4511 -0
- src/A_memorix/host_service.py +1018 -0
- src/A_memorix/paths.py +58 -0
- src/A_memorix/plugin.py +321 -0
- src/A_memorix/runtime_registry.py +30 -0
- src/A_memorix/scripts/_bootstrap.py +22 -0
- src/A_memorix/scripts/audit_vector_consistency.py +205 -0
- src/A_memorix/scripts/backfill_relation_vectors.py +265 -0
- src/A_memorix/scripts/backfill_temporal_metadata.py +67 -0
- src/A_memorix/scripts/convert_lpmm.py +537 -0
- src/A_memorix/scripts/import_lpmm_json.py +189 -0
- src/A_memorix/scripts/migrate_chat_history.py +101 -0
- src/A_memorix/scripts/migrate_maibot_memory.py +1955 -0
- src/A_memorix/scripts/migrate_person_memory_points.py +113 -0
- src/A_memorix/scripts/process_knowledge.py +831 -0
- src/A_memorix/scripts/rebuild_episodes.py +207 -0
- src/A_memorix/scripts/release_vnext_migrate.py +875 -0
- src/A_memorix/scripts/runtime_self_check.py +144 -0
- src/__init__.py +14 -0
- src/_resources/EULA.md +135 -0
- src/_resources/PRIVACY.md +60 -0
- src/_resources/changelogs/changelog.md +918 -0
- src/_resources/locales/en-US/config.json +39 -0
- src/_resources/locales/en-US/core.json +8 -0
- src/_resources/locales/en-US/prompts.json +8 -0
- src/_resources/locales/en-US/startup.json +98 -0
- src/_resources/locales/ja-JP/config.json +39 -0
- src/_resources/locales/ja-JP/core.json +8 -0
- src/_resources/locales/ja-JP/prompts.json +8 -0
- src/_resources/locales/ja-JP/startup.json +98 -0
- src/_resources/locales/ko/config.json +39 -0
- src/_resources/locales/ko/core.json +8 -0
- src/_resources/locales/ko/prompts.json +8 -0
- src/_resources/locales/ko/startup.json +98 -0
- src/_resources/locales/zh-CN/config.json +39 -0
- src/_resources/locales/zh-CN/core.json +8 -0
- src/_resources/locales/zh-CN/prompts.json +8 -0
- src/_resources/locales/zh-CN/startup.json +98 -0
- src/_resources/prompts/en-US/.meta.toml +164 -0
- src/_resources/prompts/en-US/a_memorix_chat_summary.prompt +45 -0
- src/_resources/prompts/en-US/emoji_content_analysis.prompt +3 -0
- src/_resources/prompts/en-US/emoji_content_filtration.prompt +6 -0
- src/_resources/prompts/en-US/emoji_replace.prompt +12 -0
- src/_resources/prompts/en-US/emoji_selection.prompt +6 -0
- src/_resources/prompts/en-US/expression_evaluation.prompt +14 -0
- src/_resources/prompts/en-US/heuristic_memory_impression.prompt +15 -0
- src/_resources/prompts/en-US/image_description.prompt +1 -0
- src/_resources/prompts/en-US/jargon_compare_inference.prompt +14 -0
- src/_resources/prompts/en-US/jargon_inference_content_only.prompt +11 -0
- src/_resources/prompts/en-US/jargon_inference_with_context.prompt +19 -0
- src/_resources/prompts/en-US/learn_jargon.prompt +31 -0
- src/_resources/prompts/en-US/learn_style.prompt +28 -0
- src/_resources/prompts/en-US/maisaka_chat.prompt +28 -0
- src/_resources/prompts/en-US/maisaka_chat_focus.prompt +39 -0
- src/_resources/prompts/en-US/maisaka_chat_optimized.prompt +19 -0
- src/_resources/prompts/en-US/maisaka_replyer.prompt +10 -0
- src/_resources/prompts/en-US/memory_fuzzy_modify_plan.prompt +45 -0
- src/_resources/prompts/en-US/mid_term_memory_summary.prompt +10 -0
- src/_resources/prompts/en-US/reply_attachments.prompt +3 -0
- src/_resources/prompts/en-US/retro_private_replyer.prompt +13 -0
- src/_resources/prompts/en-US/retro_private_replyer_self.prompt +13 -0
- src/_resources/prompts/en-US/retro_replyer.prompt +17 -0
- src/_resources/prompts/en-US/retro_replyer_light.prompt +17 -0
- src/_resources/prompts/en-US/webui_ai_search.prompt +24 -0
- src/_resources/prompts/en-US/webui_ai_search_correction.prompt +1 -0
- src/_resources/prompts/en-US/webui_ai_search_evidence.prompt +3 -0
- src/_resources/prompts/en-US/webui_ai_search_final.prompt +1 -0
- src/_resources/prompts/ja-JP/.meta.toml +164 -0
- src/_resources/prompts/ja-JP/a_memorix_chat_summary.prompt +45 -0
- src/_resources/prompts/ja-JP/emoji_content_analysis.prompt +3 -0
- src/_resources/prompts/ja-JP/emoji_content_filtration.prompt +6 -0
- src/_resources/prompts/ja-JP/emoji_replace.prompt +12 -0
- src/_resources/prompts/ja-JP/emoji_selection.prompt +6 -0
- src/_resources/prompts/ja-JP/expression_evaluation.prompt +14 -0
- src/_resources/prompts/ja-JP/heuristic_memory_impression.prompt +15 -0
- src/_resources/prompts/ja-JP/image_description.prompt +1 -0
- src/_resources/prompts/ja-JP/jargon_compare_inference.prompt +14 -0
- src/_resources/prompts/ja-JP/jargon_inference_content_only.prompt +11 -0
- src/_resources/prompts/ja-JP/jargon_inference_with_context.prompt +19 -0
- src/_resources/prompts/ja-JP/learn_jargon.prompt +31 -0
- src/_resources/prompts/ja-JP/learn_style.prompt +28 -0
- src/_resources/prompts/ja-JP/maisaka_chat.prompt +28 -0
- src/_resources/prompts/ja-JP/maisaka_chat_focus.prompt +39 -0
- src/_resources/prompts/ja-JP/maisaka_chat_optimized.prompt +19 -0
- src/_resources/prompts/ja-JP/maisaka_replyer.prompt +10 -0
- src/_resources/prompts/ja-JP/memory_fuzzy_modify_plan.prompt +45 -0
- src/_resources/prompts/ja-JP/mid_term_memory_summary.prompt +10 -0
- src/_resources/prompts/ja-JP/reply_attachments.prompt +3 -0
- src/_resources/prompts/ja-JP/retro_private_replyer.prompt +13 -0
- src/_resources/prompts/ja-JP/retro_private_replyer_self.prompt +13 -0
- src/_resources/prompts/ja-JP/retro_replyer.prompt +17 -0
- src/_resources/prompts/ja-JP/retro_replyer_light.prompt +17 -0
- src/_resources/prompts/ja-JP/webui_ai_search.prompt +24 -0
- src/_resources/prompts/ja-JP/webui_ai_search_correction.prompt +1 -0
- src/_resources/prompts/ja-JP/webui_ai_search_evidence.prompt +3 -0
- src/_resources/prompts/ja-JP/webui_ai_search_final.prompt +1 -0
- src/_resources/prompts/ko/.meta.toml +4 -0
- src/_resources/prompts/ko/reply_attachments.prompt +3 -0
- src/_resources/prompts/zh-CN/.meta.toml +159 -0
- src/_resources/prompts/zh-CN/a_memorix_chat_summary.prompt +59 -0
- src/_resources/prompts/zh-CN/emoji_content_analysis.prompt +3 -0
- src/_resources/prompts/zh-CN/emoji_content_filtration.prompt +6 -0
- src/_resources/prompts/zh-CN/emoji_replace.prompt +12 -0
- src/_resources/prompts/zh-CN/emoji_selection.prompt +6 -0
- src/_resources/prompts/zh-CN/expression_evaluation.prompt +14 -0
- src/_resources/prompts/zh-CN/heuristic_memory_impression.prompt +15 -0
- src/_resources/prompts/zh-CN/image_description.prompt +1 -0
- src/_resources/prompts/zh-CN/jargon_compare_inference.prompt +14 -0
- src/_resources/prompts/zh-CN/jargon_inference_content_only.prompt +11 -0
- src/_resources/prompts/zh-CN/jargon_inference_with_context.prompt +19 -0
- src/_resources/prompts/zh-CN/learn_jargon.prompt +31 -0
- src/_resources/prompts/zh-CN/learn_style.prompt +28 -0
- src/_resources/prompts/zh-CN/maisaka_chat.prompt +28 -0
- src/_resources/prompts/zh-CN/maisaka_chat_focus.prompt +39 -0
- src/_resources/prompts/zh-CN/maisaka_chat_optimized.prompt +19 -0
- src/_resources/prompts/zh-CN/maisaka_replyer.prompt +6 -0
- src/_resources/prompts/zh-CN/memory_fuzzy_modify_plan.prompt +45 -0
- src/_resources/prompts/zh-CN/mid_term_memory_summary.prompt +10 -0
- src/_resources/prompts/zh-CN/reply_attachments.prompt +3 -0
- src/_resources/prompts/zh-CN/retro_private_replyer.prompt +13 -0
- src/_resources/prompts/zh-CN/retro_private_replyer_self.prompt +13 -0
- src/_resources/prompts/zh-CN/retro_replyer.prompt +17 -0
- src/_resources/prompts/zh-CN/retro_replyer_light.prompt +17 -0
- src/_resources/prompts/zh-CN/webui_ai_search.prompt +24 -0
- src/_resources/prompts/zh-CN/webui_ai_search_correction.prompt +1 -0
- src/_resources/prompts/zh-CN/webui_ai_search_evidence.prompt +3 -0
- src/_resources/prompts/zh-CN/webui_ai_search_final.prompt +1 -0
- src/_resources/pyproject.toml +156 -0
- src/chat/__init__.py +13 -0
- src/chat/heart_flow/heartFC_utils.py +29 -0
- src/chat/heart_flow/heartflow_manager.py +138 -0
- src/chat/heart_flow/heartflow_message_processor.py +104 -0
- src/chat/image_system/image_cache_cleanup.py +300 -0
- src/chat/image_system/image_manager.py +516 -0
- src/chat/message_receive/__init__.py +5 -0
- src/chat/message_receive/bot.py +893 -0
- src/chat/message_receive/chat_manager.py +540 -0
- src/chat/message_receive/image_receive_compressor.py +135 -0
- src/chat/message_receive/message.py +546 -0
- src/chat/message_receive/uni_message_sender.py +332 -0
- src/chat/replyer/expression_vector_index.py +3176 -0
- src/chat/replyer/maisaka_expression_selector.py +678 -0
- src/chat/replyer/maisaka_generator.py +25 -0
- src/chat/replyer/maisaka_generator_base.py +1457 -0
- src/chat/replyer/replyer_manager.py +62 -0
- src/chat/replyer/retro_prompt.py +512 -0
- src/chat/utils/llm_sentence_splitter.py +151 -0
- src/chat/utils/statistic.py +3170 -0
- src/chat/utils/typo_generator.py +495 -0
- src/chat/utils/utils.py +922 -0
- src/cli/__init__.py +3 -0
- src/cli/bot_console.py +281 -0
- src/cli/console.py +17 -0
- src/cli/input_reader.py +55 -0
- src/cli/maisaka_cli.py +122 -0
- src/cli/maisaka_cli_sender.py +28 -0
- src/cli/tool.py +62 -0
- src/common/__init__.py +1 -0
- src/common/data_models/__init__.py +28 -0
- src/common/data_models/chat_session_data_model.py +73 -0
- src/common/data_models/chat_target_info_data_model.py +73 -0
- src/common/data_models/embedding_service_data_models.py +31 -0
- src/common/data_models/expression_data_model.py +83 -0
- src/common/data_models/image_data_model.py +247 -0
- src/common/data_models/jargon_data_model.py +101 -0
- src/common/data_models/llm_data_model.py +23 -0
- src/common/data_models/llm_service_data_models.py +298 -0
- src/common/data_models/mai_message_data_model.py +226 -0
- src/common/data_models/message_component_data_model.py +560 -0
- src/common/data_models/person_info_data_model.py +177 -0
- src/common/data_models/reply_generation_data_models.py +275 -0
- src/common/data_models/tool_record_data_model.py +51 -0
- src/common/database/__init__.py +0 -0
- src/common/database/database.py +240 -0
- src/common/database/database_model.py +477 -0
- src/common/database/migrations/__init__.py +161 -0
- src/common/database/migrations/behavior_generic_tags.py +532 -0
- src/common/database/migrations/bootstrap.py +171 -0
- src/common/database/migrations/builtin.py +1966 -0
- src/common/database/migrations/exceptions.py +33 -0
- src/common/database/migrations/frozen_v2_schema.py +282 -0
- src/common/database/migrations/legacy_v1_to_v2.py +1300 -0
- src/common/database/migrations/manager.py +209 -0
- src/common/database/migrations/models.py +305 -0
- src/common/database/migrations/planner.py +108 -0
- src/common/database/migrations/progress.py +319 -0
- src/common/database/migrations/registry.py +98 -0
- src/common/database/migrations/resolver.py +135 -0
- src/common/database/migrations/schema.py +98 -0
- src/common/database/migrations/v10_to_v11.py +60 -0
- src/common/database/migrations/v11_to_v12.py +92 -0
- src/common/database/migrations/v12_to_v13.py +42 -0
- src/common/database/migrations/v13_to_v14.py +55 -0
- src/common/database/migrations/v14_to_v15.py +44 -0
- src/common/database/migrations/v15_to_v16.py +39 -0
- src/common/database/migrations/v16_to_v17.py +66 -0
- src/common/database/migrations/v17_to_v18.py +64 -0
- src/common/database/migrations/v18_to_v19.py +355 -0
- src/common/database/migrations/v19_to_v20.py +169 -0
- src/common/database/migrations/v20_to_v21.py +149 -0
- src/common/database/migrations/v21_to_v22.py +343 -0
- src/common/database/migrations/v22_to_v23.py +114 -0
- src/common/database/migrations/v23_to_v24.py +465 -0
- src/common/database/migrations/v24_to_v25.py +96 -0
- src/common/database/migrations/v25_to_v26.py +32 -0
- src/common/database/migrations/v26_to_v27.py +86 -0
- src/common/database/migrations/v27_to_v28.py +185 -0
- src/common/database/migrations/v28_to_v29.py +80 -0
- src/common/database/migrations/v29_to_v30.py +217 -0
- src/common/database/migrations/v2_to_v3.py +269 -0
- src/common/database/migrations/v30_to_v31.py +323 -0
- src/common/database/migrations/v31_to_v32.py +250 -0
- src/common/database/migrations/v32_to_v33.py +84 -0
- src/common/database/migrations/v33_to_v34.py +119 -0
- src/common/database/migrations/v34_to_v35.py +68 -0
- src/common/database/migrations/v35_to_v36.py +35 -0
- src/common/database/migrations/v36_to_v37.py +54 -0
- src/common/database/migrations/v37_to_v38.py +95 -0
- src/common/database/migrations/v38_to_v39.py +85 -0
- src/common/database/migrations/v39_to_v40.py +53 -0
- src/common/database/migrations/v3_to_v4.py +155 -0
- src/common/database/migrations/v40_to_v41.py +23 -0
- src/common/database/migrations/v41_to_v42.py +44 -0
- src/common/database/migrations/v42_to_v43.py +29 -0
- src/common/database/migrations/v4_to_v5.py +77 -0
- src/common/database/migrations/v5_to_v6.py +51 -0
- src/common/database/migrations/v6_to_v7.py +164 -0
- src/common/database/migrations/v7_to_v8.py +73 -0
- src/common/database/migrations/v8_to_v9.py +313 -0
- src/common/database/migrations/v9_to_v10.py +53 -0
- src/common/database/migrations/version_store.py +57 -0
- src/common/embedding_identity.py +43 -0
- src/common/event_loop_watchdog.py +72 -0
- src/common/i18n/__init__.py +70 -0
- src/common/i18n/exceptions.py +18 -0
- src/common/i18n/formatting.py +29 -0
- src/common/i18n/loaders.py +162 -0
- src/common/i18n/manager.py +201 -0
- src/common/logger.py +1009 -0
- src/common/logger_color_and_mapping.py +430 -0
- src/common/message_repository.py +287 -0
- src/common/message_server/__init__.py +19 -0
- src/common/message_server/api.py +190 -0
- src/common/message_server/server.py +130 -0
- src/common/operation_timing.py +52 -0
- src/common/paths.py +32 -0
- src/common/process_runner.py +142 -0
- src/common/prompt_i18n.py +416 -0
- src/common/remote.py +288 -0
- src/common/reply_effect_fingerprint.py +119 -0
- src/common/reply_effect_record_codec.py +40 -0
- src/common/runtime_loop.py +31 -0
- src/common/self_update.py +580 -0
- src/common/shutdown.py +55 -0
- src/common/update_notice.py +351 -0
- src/common/utils/image_path.py +39 -0
- src/common/utils/math_utils.py +122 -0
- src/common/utils/port_checker.py +97 -0
- src/common/utils/system_utils.py +11 -0
- src/common/utils/utils_action.py +32 -0
- src/common/utils/utils_config.py +613 -0
- src/common/utils/utils_file.py +56 -0
- src/common/utils/utils_image.py +257 -0
- src/common/utils/utils_message.py +871 -0
- src/common/utils/utils_person.py +55 -0
- src/common/utils/utils_session.py +42 -0
- src/common/utils/utils_voice.py +42 -0
- src/common/version.py +33 -0
- src/config/__init__.py +0 -0
- src/config/config.py +927 -0
- src/config/config_base.py +308 -0
- src/config/config_upgrade_hooks.py +675 -0
- src/config/config_utils.py +166 -0
- src/config/default_model_config.py +131 -0
- src/config/file_watcher.py +238 -0
- src/config/legacy_migration.py +424 -0
- src/config/legacy_upgrade_confirmation.py +225 -0
- src/config/model_configs.py +548 -0
- src/config/official_configs.py +6166 -0
- src/config/startup_bindings.py +201 -0
- src/core/__init__.py +6 -0
- src/core/announcement_manager.py +120 -0
- src/core/config_types.py +273 -0
- src/core/event_bus.py +225 -0
- src/core/local_operator.py +98 -0
- src/core/tooling.py +420 -0
- src/core/types.py +423 -0
- src/emoji_system/emoji_cache_cleanup.py +326 -0
- src/emoji_system/emoji_manager.py +1314 -0
- src/emoji_system/maisaka_tool.py +286 -0
- src/learners/expression_learner.py +927 -0
- src/learners/expression_review_store.py +216 -0
- src/learners/expression_style_utils.py +32 -0
- src/learners/expression_utils.py +299 -0
- src/learners/jargon_learner.py +873 -0
- src/learners/jargon_miner.py +885 -0
- src/learners/weighted_sampling.py +75 -0
- src/llm_models/LICENSE +21 -0
- src/llm_models/__init__.py +0 -0
- src/llm_models/exceptions.py +140 -0
- src/llm_models/generation_diagnostics.py +155 -0
- src/llm_models/image_normalizer.py +128 -0
- src/llm_models/model_client/__init__.py +29 -0
- src/llm_models/model_client/adapter_base.py +362 -0
- src/llm_models/model_client/base_client.py +678 -0
- src/llm_models/model_client/embedding_rate_limit.py +55 -0
- src/llm_models/model_client/gemini_client.py +1379 -0
- src/llm_models/model_client/image_embedding_protocols.py +397 -0
- src/llm_models/model_client/openai_client.py +2200 -0
- src/llm_models/model_client/openai_responses_client.py +817 -0
- src/llm_models/model_client/plugin_client.py +307 -0
- src/llm_models/openai_compat.py +165 -0
- src/llm_models/payload_content/__init__.py +51 -0
- src/llm_models/payload_content/context_item.py +699 -0
- src/llm_models/payload_content/context_protocol.py +243 -0
- src/llm_models/payload_content/native_tool.py +18 -0
- src/llm_models/payload_content/resp_format.py +239 -0
- src/llm_models/payload_content/tool_option.py +547 -0
- src/llm_models/request_snapshot.py +1311 -0
- src/llm_models/utils.py +276 -0
- src/llm_models/utils_model.py +1604 -0
- src/main.py +300 -0
- src/maisaka/attention_drift.py +116 -0
- src/maisaka/builtin_tool/__init__.py +236 -0
- src/maisaka/builtin_tool/context.py +530 -0
- src/maisaka/builtin_tool/fetch_history.py +62 -0
- src/maisaka/builtin_tool/provider.py +74 -0
- src/maisaka/builtin_tool/query_image_memory.py +103 -0
- src/maisaka/builtin_tool/query_memory.py +335 -0
- src/maisaka/builtin_tool/query_person_profile.py +152 -0
- src/maisaka/builtin_tool/read_tool_result.py +142 -0
- src/maisaka/builtin_tool/reply.py +695 -0
- src/maisaka/builtin_tool/send_emoji.py +543 -0
- src/maisaka/builtin_tool/show_emoji_list.py +76 -0
- src/maisaka/builtin_tool/subagent.py +549 -0
- src/maisaka/builtin_tool/switch_chat.py +78 -0
- src/maisaka/builtin_tool/tool_search.py +92 -0
- src/maisaka/builtin_tool/view_forward_message.py +153 -0
- src/maisaka/builtin_tool/wait.py +77 -0
- src/maisaka/chat_loop_service.py +1579 -0
- src/maisaka/context/__init__.py +1 -0
- src/maisaka/context/clear_context.py +42 -0
- src/maisaka/context/emoji_candidates.py +36 -0
- src/maisaka/context/history.py +384 -0
- src/maisaka/context/image_attachment.py +52 -0
- src/maisaka/context/message_adapter.py +126 -0
- src/maisaka/context/message_id_alias.py +52 -0
- src/maisaka/context/messages.py +825 -0
- src/maisaka/context/planner_messages.py +199 -0
- src/maisaka/context/post_processor.py +204 -0
- src/maisaka/context/usage.py +141 -0
- src/maisaka/display/__init__.py +25 -0
- src/maisaka/display/display_utils.py +144 -0
- src/maisaka/display/preview_path_utils.py +64 -0
- src/maisaka/display/prompt_cli_renderer.py +1110 -0
- src/maisaka/display/prompt_preview_logger.py +603 -0
- src/maisaka/display/runtime_mixin.py +709 -0
- src/maisaka/display/stage_status_board.py +129 -0
- src/maisaka/focus/__init__.py +10 -0
- src/maisaka/focus/manager.py +442 -0
- src/maisaka/focus/runtime_mixin.py +753 -0
- src/maisaka/jargon_context_matcher.py +363 -0
- src/maisaka/memory/__init__.py +1 -0
- src/maisaka/memory/heuristic_injector.py +393 -0
- src/maisaka/memory/image_injector.py +81 -0
- src/maisaka/memory/mid_term.py +995 -0
- src/maisaka/memory/person_profile.py +280 -0
- src/maisaka/mode_policy.py +28 -0
- src/maisaka/monitor/__init__.py +1 -0
- src/maisaka/monitor/event_store.py +351 -0
- src/maisaka/monitor/events.py +795 -0
- src/maisaka/monitor/message_payload.py +172 -0
- src/maisaka/monitor/planner_delta.py +77 -0
- src/maisaka/monitor/tool_media.py +58 -0
- src/maisaka/planner_reply_guard.py +69 -0
- src/maisaka/reasoning_engine.py +2186 -0
- src/maisaka/reply_effect/__init__.py +18 -0
- src/maisaka/reply_effect/image_utils.py +100 -0
- src/maisaka/reply_effect/judge.py +382 -0
- src/maisaka/reply_effect/models.py +185 -0
- src/maisaka/reply_effect/path_utils.py +24 -0
- src/maisaka/reply_effect/quote_utils.py +32 -0
- src/maisaka/reply_effect/scoring.py +205 -0
- src/maisaka/reply_effect/storage.py +380 -0
- src/maisaka/reply_effect/tracker.py +568 -0
- src/maisaka/runtime.py +2218 -0
- src/maisaka/subagent_tasks.py +171 -0
- src/maisaka/tool_result_condenser.py +143 -0
- src/maisaka/tool_result_store.py +57 -0
- src/maisaka/turn_trigger/__init__.py +9 -0
- src/maisaka/turn_trigger/dynamic_gate.py +211 -0
- src/maisaka/turn_trigger/gates.py +252 -0
- src/maisaka/turn_trigger/idle_backoff.py +93 -0
- src/maisaka/turn_trigger/reply_likelihood.py +88 -0
- src/maisaka/turn_trigger/scheduler.py +113 -0
- src/maisaka/utils/__init__.py +1 -0
- src/maisaka/utils/tool_post_execution.py +76 -0
- src/maisaka/utils/tool_record_payload.py +155 -0
- src/maisaka/visual/__init__.py +1 -0
- src/maisaka/visual/chat_history_refresher.py +303 -0
- src/maisaka/visual/message_limiter.py +69 -0
- src/maisaka/visual/mode_utils.py +33 -0
- src/manager/async_task_manager.py +193 -0
- src/manager/local_store_manager.py +126 -0
- src/mcp_module/__init__.py +9 -0
- src/mcp_module/config.py +174 -0
- src/mcp_module/connection.py +726 -0
- src/mcp_module/hooks.py +20 -0
- src/mcp_module/host_llm_bridge.py +654 -0
- src/mcp_module/manager.py +680 -0
- src/mcp_module/models.py +437 -0
- src/mcp_module/provider.py +62 -0
- src/mcp_module/service.py +308 -0
- src/mcp_module/stdio_filter.py +177 -0
- src/person_info/__init__.py +0 -0
- src/person_info/person_info.py +600 -0
- src/platform_io/__init__.py +39 -0
- src/platform_io/adapter_policy.py +839 -0
- src/platform_io/avatar.py +197 -0
- src/platform_io/dedupe.py +133 -0
- src/platform_io/drivers/__init__.py +11 -0
- src/platform_io/drivers/base.py +104 -0
- src/platform_io/drivers/legacy_driver.py +92 -0
- src/platform_io/drivers/plugin_driver.py +213 -0
- src/platform_io/manager.py +716 -0
- src/platform_io/outbound_tracker.py +286 -0
- src/platform_io/registry.py +70 -0
- src/platform_io/route_key_factory.py +174 -0
- src/platform_io/routing.py +141 -0
- src/platform_io/types.py +265 -0
- src/plugin_runtime/__init__.py +60 -0
- src/plugin_runtime/avatar_provider.py +64 -0
- src/plugin_runtime/capabilities/__init__.py +11 -0
- src/plugin_runtime/capabilities/components.py +977 -0
- src/plugin_runtime/capabilities/core.py +868 -0
- src/plugin_runtime/capabilities/data.py +1415 -0
- src/plugin_runtime/capabilities/registry.py +116 -0
- src/plugin_runtime/capabilities/render.py +131 -0
- src/plugin_runtime/compat_policy.py +47 -0
- src/plugin_runtime/component_query.py +1119 -0
- src/plugin_runtime/dependency_pipeline.py +547 -0
- src/plugin_runtime/docker_layout_migration.py +275 -0
- src/plugin_runtime/hook_catalog.py +52 -0
- src/plugin_runtime/hook_payloads.py +197 -0
- src/plugin_runtime/hook_schema_utils.py +31 -0
- src/plugin_runtime/host/__init__.py +1 -0
- src/plugin_runtime/host/api_registry.py +353 -0
- src/plugin_runtime/host/authorization.py +67 -0
- src/plugin_runtime/host/capability_service.py +91 -0
- src/plugin_runtime/host/circuit_breaker.py +243 -0
- src/plugin_runtime/host/component_registry.py +1336 -0
- src/plugin_runtime/host/component_timeout.py +32 -0
- src/plugin_runtime/host/event_dispatcher.py +228 -0
- src/plugin_runtime/host/hook_dispatcher.py +711 -0
- src/plugin_runtime/host/hook_spec_registry.py +190 -0
- src/plugin_runtime/host/logger_bridge.py +64 -0
- src/plugin_runtime/host/message_gateway.py +116 -0
- src/plugin_runtime/host/message_utils.py +572 -0
- src/plugin_runtime/host/reply_extension_schema.py +65 -0
- src/plugin_runtime/host/reply_extensions.py +204 -0
- src/plugin_runtime/host/rpc_server.py +509 -0
- src/plugin_runtime/host/supervisor.py +2269 -0
- src/plugin_runtime/integration.py +2063 -0
- src/plugin_runtime/local_sdk.py +147 -0
- src/plugin_runtime/protocol/__init__.py +1 -0
- src/plugin_runtime/protocol/codec.py +71 -0
- src/plugin_runtime/protocol/envelope.py +600 -0
- src/plugin_runtime/protocol/errors.py +77 -0
- src/plugin_runtime/runner/__init__.py +1 -0
- src/plugin_runtime/runner/log_handler.py +239 -0
- src/plugin_runtime/runner/manifest_validator.py +1517 -0
- src/plugin_runtime/runner/plugin_loader.py +711 -0
- src/plugin_runtime/runner/plugin_paths.py +59 -0
- src/plugin_runtime/runner/rpc_client.py +336 -0
- src/plugin_runtime/runner/runner_main.py +2670 -0
- src/plugin_runtime/tool_provider.py +58 -0
- src/plugin_runtime/transport/__init__.py +1 -0
- src/plugin_runtime/transport/base.py +116 -0
- src/plugin_runtime/transport/factory.py +57 -0
- src/plugin_runtime/transport/named_pipe.py +206 -0
- src/plugin_runtime/transport/tcp.py +62 -0
- src/plugin_runtime/transport/uds.py +133 -0
- src/plugin_runtime/update_compatibility_notice.py +303 -0
- src/plugin_runtime/upload_store.py +173 -0
- src/plugin_runtime/webui_schema.py +571 -0
- src/plugins/__init__.py +1 -0
- src/plugins/built_in/plugin_management/_manifest.json +48 -0
- src/plugins/built_in/plugin_management/plugin.py +665 -0
- src/prompt/prompt_manager.py +440 -0
- src/runtime.py +688 -0
- src/services/__init__.py +7 -0
- src/services/bot_account_service.py +327 -0
- src/services/database_service.py +203 -0
- src/services/embedding_service.py +271 -0
- src/services/expression_vector_service.py +106 -0
- src/services/html_render_service.py +945 -0
- src/services/image_path_maintenance_service.py +454 -0
- src/services/image_writeback_journal.py +98 -0
- src/services/llm_cache_stats.py +1521 -0
- src/services/llm_service.py +799 -0
- src/services/memory_flow_service.py +1019 -0
- src/services/memory_service.py +528 -0
- src/services/message_service.py +295 -0
- src/services/message_word_frequency_service.py +595 -0
- src/services/news_service.py +123 -0
- src/services/person_fact_reverification.py +90 -0
- src/services/person_fact_verifier.py +79 -0
- src/services/send_service.py +1483 -0
- src/services/service_task_resolver.py +59 -0
- src/services/statistics_aggregation_service.py +349 -0
- src/services/statistics_service.py +850 -0
- src/services/telemetry_stats_service.py +294 -0
- src/services/tool_record_cleanup_service.py +205 -0
- src/webui/app.py +374 -0
- src/webui/config_schema.py +481 -0
- src/webui/core/__init__.py +32 -0
- src/webui/core/auth.py +153 -0
- src/webui/core/rate_limiter.py +247 -0
- src/webui/core/security.py +361 -0
- src/webui/dependencies.py +70 -0
- src/webui/logs_ws.py +195 -0
- src/webui/middleware/__init__.py +17 -0
- src/webui/middleware/anti_crawler.py +824 -0
- src/webui/middleware/compression.py +88 -0
- src/webui/routers/__init__.py +33 -0
- src/webui/routers/avatar.py +250 -0
- src/webui/routers/bot_accounts.py +103 -0
- src/webui/routers/chat/__init__.py +18 -0
- src/webui/routers/chat/routes.py +1715 -0
- src/webui/routers/chat/serializers.py +248 -0
- src/webui/routers/chat/service.py +1489 -0
- src/webui/routers/config.py +1964 -0
- src/webui/routers/data_transfer.py +769 -0
- src/webui/routers/emoji/__init__.py +3 -0
- src/webui/routers/emoji/routes.py +1230 -0
- src/webui/routers/emoji/schemas.py +182 -0
- src/webui/routers/emoji/support.py +143 -0
- src/webui/routers/expression.py +2298 -0
- src/webui/routers/jargon.py +1319 -0
- src/webui/routers/mcp.py +99 -0
- src/webui/routers/memory.py +5589 -0
- src/webui/routers/model.py +910 -0
- src/webui/routers/news.py +23 -0
- src/webui/routers/person.py +427 -0
- src/webui/routers/plugin/__init__.py +31 -0
- src/webui/routers/plugin/catalog.py +218 -0
- src/webui/routers/plugin/config_routes.py +592 -0
- src/webui/routers/plugin/icon_routes.py +71 -0
- src/webui/routers/plugin/management.py +908 -0
- src/webui/routers/plugin/marketplace.py +116 -0
- src/webui/routers/plugin/progress.py +172 -0
- src/webui/routers/plugin/release_install.py +313 -0
- src/webui/routers/plugin/releases.py +181 -0
- src/webui/routers/plugin/runtime_routes.py +243 -0
- src/webui/routers/plugin/schemas.py +131 -0
- src/webui/routers/plugin/stats_proxy.py +127 -0
- src/webui/routers/plugin/support.py +366 -0
- src/webui/routers/plugin/webui_extensions.py +346 -0
- src/webui/routers/plugin/zip_install.py +125 -0
- src/webui/routers/reasoning_process.py +2326 -0
- src/webui/routers/reply_effects.py +926 -0
- src/webui/routers/search.py +249 -0
- src/webui/routers/self_update.py +194 -0
- src/webui/routers/statistics.py +61 -0
- src/webui/routers/system.py +1790 -0
- src/webui/routers/user_emoji.py +168 -0
- src/webui/routers/websocket/__init__.py +7 -0
- src/webui/routers/websocket/auth.py +104 -0
- src/webui/routers/websocket/manager.py +373 -0
- src/webui/routers/websocket/plugin_runtime.py +54 -0
- src/webui/routers/websocket/unified.py +761 -0
- src/webui/routes.py +416 -0
- src/webui/schemas/__init__.py +18 -0
- src/webui/schemas/news.py +24 -0
- src/webui/schemas/statistics.py +173 -0
- src/webui/services/__init__.py +1 -0
- src/webui/services/ai_search_agent.py +509 -0
- src/webui/services/ai_search_documents.py +329 -0
- src/webui/services/ai_search_grounding.py +103 -0
- src/webui/services/ai_search_local_config.py +129 -0
- src/webui/services/ai_search_models.py +98 -0
- src/webui/services/git_mirror_service.py +1024 -0
- src/webui/services/plugin_market_service.py +56 -0
- src/webui/utils/__init__.py +1 -0
- src/webui/utils/http_client.py +27 -0
- src/webui/utils/network_security.py +133 -0
- src/webui/utils/toml_utils.py +124 -0
- src/webui/version_compatibility.py +180 -0
- src/webui/webui_server.py +422 -0
|
@@ -0,0 +1,711 @@
|
|
|
1
|
+
"""命名 Hook 分发系统。
|
|
2
|
+
|
|
3
|
+
主程序可以在任意执行点触发一个命名 Hook,Host 会收集所有订阅该 Hook 的
|
|
4
|
+
插件处理器,并按照固定的全局顺序调度执行。
|
|
5
|
+
|
|
6
|
+
排序规则如下:
|
|
7
|
+
|
|
8
|
+
1. `blocking` 先于 `observe`
|
|
9
|
+
2. `early` 先于 `normal` 先于 `late`
|
|
10
|
+
3. 内置插件先于第三方插件
|
|
11
|
+
4. `plugin_id`
|
|
12
|
+
5. `handler_name`
|
|
13
|
+
|
|
14
|
+
其中:
|
|
15
|
+
|
|
16
|
+
- `blocking` 处理器串行执行,可修改 `kwargs`,也可中止本次 Hook 调用。
|
|
17
|
+
- `observe` 处理器后台并发执行,只允许旁路观察,不参与主流程控制。
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from dataclasses import dataclass, field
|
|
21
|
+
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Sequence, Set
|
|
22
|
+
|
|
23
|
+
import asyncio
|
|
24
|
+
import contextlib
|
|
25
|
+
|
|
26
|
+
from src.common.logger import get_logger
|
|
27
|
+
from src.common.shutdown import is_shutdown_requested
|
|
28
|
+
from src.config.config import global_config
|
|
29
|
+
from src.plugin_runtime.protocol.errors import ErrorCode, RPCError
|
|
30
|
+
|
|
31
|
+
from .circuit_breaker import get_plugin_circuit_breaker
|
|
32
|
+
from .hook_spec_registry import HookSpec, HookSpecRegistry
|
|
33
|
+
|
|
34
|
+
if TYPE_CHECKING:
|
|
35
|
+
from .component_registry import HookHandlerEntry
|
|
36
|
+
from .supervisor import PluginRunnerSupervisor
|
|
37
|
+
|
|
38
|
+
logger = get_logger("plugin_runtime.host.hook_dispatcher")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass(slots=True)
|
|
42
|
+
class HookHandlerExecutionResult:
|
|
43
|
+
"""单个 HookHandler 的执行结果。
|
|
44
|
+
|
|
45
|
+
Attributes:
|
|
46
|
+
handler_name: 完整处理器名称,格式通常为 `plugin_id.component_name`。
|
|
47
|
+
plugin_id: 处理器所属插件 ID。
|
|
48
|
+
success: 本次调用是否成功。
|
|
49
|
+
action: 当前处理器要求的控制动作,仅支持 `continue` 或 `abort`。
|
|
50
|
+
modified_kwargs: 处理器返回的修改后参数字典。
|
|
51
|
+
custom_result: 处理器返回的附加结果。
|
|
52
|
+
error_message: 失败时的错误描述。
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
handler_name: str
|
|
56
|
+
plugin_id: str
|
|
57
|
+
success: bool = True
|
|
58
|
+
action: str = "continue"
|
|
59
|
+
modified_kwargs: Optional[Dict[str, Any]] = None
|
|
60
|
+
custom_result: Any = None
|
|
61
|
+
error_message: str = ""
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass(slots=True)
|
|
65
|
+
class HookDispatchResult:
|
|
66
|
+
"""一次命名 Hook 调用的聚合结果。
|
|
67
|
+
|
|
68
|
+
Attributes:
|
|
69
|
+
hook_name: 本次调用的 Hook 名称。
|
|
70
|
+
kwargs: 经阻塞处理器串行处理后的最终参数字典。
|
|
71
|
+
aborted: 是否被某个处理器中止。
|
|
72
|
+
stopped_by: 若被中止,记录触发中止的完整处理器名称。
|
|
73
|
+
custom_results: 阻塞处理器返回的附加结果列表。
|
|
74
|
+
errors: 本次调用中记录到的错误信息列表。
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
hook_name: str
|
|
78
|
+
kwargs: Dict[str, Any] = field(default_factory=dict)
|
|
79
|
+
aborted: bool = False
|
|
80
|
+
stopped_by: Optional[str] = None
|
|
81
|
+
custom_results: List[Any] = field(default_factory=list)
|
|
82
|
+
errors: List[str] = field(default_factory=list)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@dataclass(slots=True)
|
|
86
|
+
class _HookInvocationTarget:
|
|
87
|
+
"""内部使用的 Hook 调度目标。
|
|
88
|
+
|
|
89
|
+
Attributes:
|
|
90
|
+
supervisor: 负责该处理器的 Supervisor。
|
|
91
|
+
entry: Hook 处理器条目。
|
|
92
|
+
source_rank: 插件来源权重,内置插件为 `0`,第三方插件为 `1`。
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
supervisor: "PluginRunnerSupervisor"
|
|
96
|
+
entry: "HookHandlerEntry"
|
|
97
|
+
source_rank: int
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class HookDispatcher:
|
|
101
|
+
"""命名 Hook 分发器。"""
|
|
102
|
+
|
|
103
|
+
def __init__(
|
|
104
|
+
self,
|
|
105
|
+
supervisors_provider: Optional[Callable[[], Sequence["PluginRunnerSupervisor"]]] = None,
|
|
106
|
+
hook_spec_registry: Optional[HookSpecRegistry] = None,
|
|
107
|
+
) -> None:
|
|
108
|
+
"""初始化 Hook 分发器。
|
|
109
|
+
|
|
110
|
+
Args:
|
|
111
|
+
supervisors_provider: 可选的 Supervisor 提供器。若调用 `invoke_hook()`
|
|
112
|
+
时未显式传入 `supervisors`,则使用该回调获取目标 Supervisor 列表。
|
|
113
|
+
hook_spec_registry: 可选的 Hook 规格注册中心;留空时使用独立注册中心。
|
|
114
|
+
"""
|
|
115
|
+
|
|
116
|
+
self._background_tasks: Set[asyncio.Task[Any]] = set()
|
|
117
|
+
self._supervisors_provider = supervisors_provider
|
|
118
|
+
self._hook_spec_registry = hook_spec_registry or HookSpecRegistry()
|
|
119
|
+
|
|
120
|
+
async def stop(self) -> None:
|
|
121
|
+
"""停止分发器并取消所有未完成的观察任务。"""
|
|
122
|
+
|
|
123
|
+
for task in self._background_tasks:
|
|
124
|
+
task.cancel()
|
|
125
|
+
await asyncio.gather(*self._background_tasks, return_exceptions=True)
|
|
126
|
+
self._background_tasks.clear()
|
|
127
|
+
|
|
128
|
+
def register_hook_spec(self, spec: HookSpec) -> None:
|
|
129
|
+
"""注册单个命名 Hook 规格。
|
|
130
|
+
|
|
131
|
+
Args:
|
|
132
|
+
spec: 需要注册的 Hook 规格。
|
|
133
|
+
"""
|
|
134
|
+
|
|
135
|
+
self._hook_spec_registry.register_hook_spec(spec)
|
|
136
|
+
|
|
137
|
+
def register_hook_specs(self, specs: Sequence[HookSpec]) -> None:
|
|
138
|
+
"""批量注册命名 Hook 规格。
|
|
139
|
+
|
|
140
|
+
Args:
|
|
141
|
+
specs: 需要注册的 Hook 规格序列。
|
|
142
|
+
"""
|
|
143
|
+
|
|
144
|
+
for spec in specs:
|
|
145
|
+
self.register_hook_spec(spec)
|
|
146
|
+
|
|
147
|
+
def get_hook_spec(self, hook_name: str) -> HookSpec:
|
|
148
|
+
"""获取指定 Hook 的规格定义。
|
|
149
|
+
|
|
150
|
+
Args:
|
|
151
|
+
hook_name: Hook 名称。
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
HookSpec: 若未显式注册,则返回按系统默认值生成的运行时规格。
|
|
155
|
+
"""
|
|
156
|
+
|
|
157
|
+
normalized_name = self._normalize_hook_name(hook_name)
|
|
158
|
+
registered_spec = self._hook_spec_registry.get_hook_spec(normalized_name)
|
|
159
|
+
if registered_spec is not None:
|
|
160
|
+
return registered_spec
|
|
161
|
+
|
|
162
|
+
return HookSpec(
|
|
163
|
+
name=normalized_name,
|
|
164
|
+
parameters_schema={},
|
|
165
|
+
default_timeout_ms=self._get_default_timeout_ms(),
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
def unregister_hook_spec(self, hook_name: str) -> bool:
|
|
169
|
+
"""注销指定命名 Hook 规格。
|
|
170
|
+
|
|
171
|
+
Args:
|
|
172
|
+
hook_name: 目标 Hook 名称。
|
|
173
|
+
|
|
174
|
+
Returns:
|
|
175
|
+
bool: 是否成功注销。
|
|
176
|
+
"""
|
|
177
|
+
|
|
178
|
+
return self._hook_spec_registry.unregister_hook_spec(hook_name)
|
|
179
|
+
|
|
180
|
+
def list_hook_specs(self) -> List[HookSpec]:
|
|
181
|
+
"""返回当前全部显式注册的 Hook 规格。
|
|
182
|
+
|
|
183
|
+
Returns:
|
|
184
|
+
List[HookSpec]: 已注册 Hook 规格列表。
|
|
185
|
+
"""
|
|
186
|
+
|
|
187
|
+
return self._hook_spec_registry.list_hook_specs()
|
|
188
|
+
|
|
189
|
+
async def invoke_hook(
|
|
190
|
+
self,
|
|
191
|
+
hook_name: str,
|
|
192
|
+
supervisors: Optional[Sequence["PluginRunnerSupervisor"]] = None,
|
|
193
|
+
**kwargs: Any,
|
|
194
|
+
) -> HookDispatchResult:
|
|
195
|
+
"""触发一次命名 Hook 调用。
|
|
196
|
+
|
|
197
|
+
Args:
|
|
198
|
+
hook_name: 本次触发的 Hook 名称。
|
|
199
|
+
supervisors: 当前运行时中所有可参与分发的 Supervisor;留空时使用绑定的提供器。
|
|
200
|
+
**kwargs: 传递给 Hook 处理器的关键字参数。
|
|
201
|
+
|
|
202
|
+
Returns:
|
|
203
|
+
HookDispatchResult: 聚合后的 Hook 调用结果。
|
|
204
|
+
"""
|
|
205
|
+
|
|
206
|
+
normalized_hook_name = self._normalize_hook_name(hook_name)
|
|
207
|
+
current_kwargs: Dict[str, Any] = dict(kwargs)
|
|
208
|
+
dispatch_result = HookDispatchResult(hook_name=normalized_hook_name, kwargs=dict(current_kwargs))
|
|
209
|
+
if is_shutdown_requested():
|
|
210
|
+
return dispatch_result
|
|
211
|
+
|
|
212
|
+
resolved_supervisors = list(supervisors) if supervisors is not None else list(self._resolve_supervisors())
|
|
213
|
+
hook_spec = self.get_hook_spec(normalized_hook_name)
|
|
214
|
+
invocation_targets = self._collect_invocation_targets(normalized_hook_name, resolved_supervisors)
|
|
215
|
+
|
|
216
|
+
if not invocation_targets:
|
|
217
|
+
return dispatch_result
|
|
218
|
+
|
|
219
|
+
for target in invocation_targets:
|
|
220
|
+
if is_shutdown_requested():
|
|
221
|
+
return dispatch_result
|
|
222
|
+
|
|
223
|
+
if target.entry.is_observe:
|
|
224
|
+
self._schedule_observe_handler(
|
|
225
|
+
hook_name=normalized_hook_name,
|
|
226
|
+
hook_spec=hook_spec,
|
|
227
|
+
target=target,
|
|
228
|
+
kwargs=current_kwargs,
|
|
229
|
+
)
|
|
230
|
+
continue
|
|
231
|
+
|
|
232
|
+
if not hook_spec.allow_blocking:
|
|
233
|
+
error_message = (
|
|
234
|
+
f"Hook {normalized_hook_name} 不允许 blocking 处理器,"
|
|
235
|
+
f"已跳过 {target.entry.full_name}"
|
|
236
|
+
)
|
|
237
|
+
logger.warning(error_message)
|
|
238
|
+
dispatch_result.errors.append(error_message)
|
|
239
|
+
continue
|
|
240
|
+
|
|
241
|
+
execution_result = await self._invoke_handler(
|
|
242
|
+
hook_name=normalized_hook_name,
|
|
243
|
+
hook_spec=hook_spec,
|
|
244
|
+
target=target,
|
|
245
|
+
kwargs=current_kwargs,
|
|
246
|
+
)
|
|
247
|
+
self._merge_blocking_result(
|
|
248
|
+
hook_spec=hook_spec,
|
|
249
|
+
target=target,
|
|
250
|
+
execution_result=execution_result,
|
|
251
|
+
dispatch_result=dispatch_result,
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
current_kwargs = dict(dispatch_result.kwargs)
|
|
255
|
+
if dispatch_result.aborted:
|
|
256
|
+
break
|
|
257
|
+
|
|
258
|
+
return dispatch_result
|
|
259
|
+
|
|
260
|
+
def _resolve_supervisors(self) -> Sequence["PluginRunnerSupervisor"]:
|
|
261
|
+
"""解析当前调用应使用的 Supervisor 列表。
|
|
262
|
+
|
|
263
|
+
Returns:
|
|
264
|
+
Sequence[PluginRunnerSupervisor]: 可参与本次 Hook 调度的 Supervisor 序列。
|
|
265
|
+
|
|
266
|
+
Raises:
|
|
267
|
+
ValueError: 当未传入 `supervisors` 且分发器也未绑定提供器时抛出。
|
|
268
|
+
"""
|
|
269
|
+
|
|
270
|
+
if self._supervisors_provider is None:
|
|
271
|
+
raise ValueError("当前 HookDispatcher 未绑定 supervisors_provider,请显式传入 supervisors")
|
|
272
|
+
return self._supervisors_provider()
|
|
273
|
+
|
|
274
|
+
def _collect_invocation_targets(
|
|
275
|
+
self,
|
|
276
|
+
hook_name: str,
|
|
277
|
+
supervisors: Sequence["PluginRunnerSupervisor"],
|
|
278
|
+
) -> List[_HookInvocationTarget]:
|
|
279
|
+
"""收集并排序本次 Hook 调用的全部处理器目标。
|
|
280
|
+
|
|
281
|
+
Args:
|
|
282
|
+
hook_name: 目标 Hook 名称。
|
|
283
|
+
supervisors: 当前参与调度的 Supervisor 序列。
|
|
284
|
+
|
|
285
|
+
Returns:
|
|
286
|
+
List[_HookInvocationTarget]: 已完成全局排序的处理器目标列表。
|
|
287
|
+
"""
|
|
288
|
+
|
|
289
|
+
invocation_targets: List[_HookInvocationTarget] = []
|
|
290
|
+
for supervisor in supervisors:
|
|
291
|
+
source_rank = self._get_supervisor_source_rank(supervisor)
|
|
292
|
+
for entry in supervisor.component_registry.get_hook_handlers(hook_name):
|
|
293
|
+
invocation_targets.append(
|
|
294
|
+
_HookInvocationTarget(
|
|
295
|
+
supervisor=supervisor,
|
|
296
|
+
entry=entry,
|
|
297
|
+
source_rank=source_rank,
|
|
298
|
+
)
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
invocation_targets.sort(key=self._build_sort_key)
|
|
302
|
+
return invocation_targets
|
|
303
|
+
|
|
304
|
+
@staticmethod
|
|
305
|
+
def _build_sort_key(target: _HookInvocationTarget) -> tuple[int, int, int, str, str]:
|
|
306
|
+
"""构造 Hook 处理器的全局排序键。
|
|
307
|
+
|
|
308
|
+
Args:
|
|
309
|
+
target: 待排序的处理器目标。
|
|
310
|
+
|
|
311
|
+
Returns:
|
|
312
|
+
tuple[int, int, int, str, str]: 全局排序键。
|
|
313
|
+
"""
|
|
314
|
+
|
|
315
|
+
return (
|
|
316
|
+
HookDispatcher._get_mode_rank(target.entry.mode),
|
|
317
|
+
HookDispatcher._get_order_rank(target.entry.order),
|
|
318
|
+
target.source_rank,
|
|
319
|
+
target.entry.plugin_id,
|
|
320
|
+
target.entry.name,
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
@staticmethod
|
|
324
|
+
def _get_default_timeout_ms() -> int:
|
|
325
|
+
"""读取系统级默认 Hook 超时。
|
|
326
|
+
|
|
327
|
+
Returns:
|
|
328
|
+
int: 默认超时毫秒数。
|
|
329
|
+
"""
|
|
330
|
+
|
|
331
|
+
timeout_seconds = float(global_config.plugin_runtime.hook_blocking_timeout_sec or 60.0)
|
|
332
|
+
return max(int(timeout_seconds * 1000), 1)
|
|
333
|
+
|
|
334
|
+
@staticmethod
|
|
335
|
+
def _get_mode_rank(mode: str) -> int:
|
|
336
|
+
"""返回 Hook 模式的排序权重。
|
|
337
|
+
|
|
338
|
+
Args:
|
|
339
|
+
mode: Hook 模式。
|
|
340
|
+
|
|
341
|
+
Returns:
|
|
342
|
+
int: 越小表示越靠前。
|
|
343
|
+
"""
|
|
344
|
+
|
|
345
|
+
return {"blocking": 0, "observe": 1}.get(mode, 99)
|
|
346
|
+
|
|
347
|
+
@staticmethod
|
|
348
|
+
def _get_order_rank(order: str) -> int:
|
|
349
|
+
"""返回 Hook 顺序槽位的排序权重。
|
|
350
|
+
|
|
351
|
+
Args:
|
|
352
|
+
order: Hook 顺序槽位。
|
|
353
|
+
|
|
354
|
+
Returns:
|
|
355
|
+
int: 越小表示越靠前。
|
|
356
|
+
"""
|
|
357
|
+
|
|
358
|
+
return {"early": 0, "normal": 1, "late": 2}.get(order, 99)
|
|
359
|
+
|
|
360
|
+
@staticmethod
|
|
361
|
+
def _get_supervisor_source_rank(supervisor: "PluginRunnerSupervisor") -> int:
|
|
362
|
+
"""返回 Supervisor 的来源排序权重。
|
|
363
|
+
|
|
364
|
+
Args:
|
|
365
|
+
supervisor: 目标 Supervisor。
|
|
366
|
+
|
|
367
|
+
Returns:
|
|
368
|
+
int: 内置插件返回 `0`,第三方插件返回 `1`。
|
|
369
|
+
"""
|
|
370
|
+
|
|
371
|
+
return 0 if supervisor.group_name == "builtin" else 1
|
|
372
|
+
|
|
373
|
+
@staticmethod
|
|
374
|
+
def _normalize_hook_name(hook_name: str) -> str:
|
|
375
|
+
"""规范化命名 Hook 名称。
|
|
376
|
+
|
|
377
|
+
Args:
|
|
378
|
+
hook_name: 原始 Hook 名称。
|
|
379
|
+
|
|
380
|
+
Returns:
|
|
381
|
+
str: 规范化后的 Hook 名称。
|
|
382
|
+
|
|
383
|
+
Raises:
|
|
384
|
+
ValueError: 当 Hook 名称为空时抛出。
|
|
385
|
+
"""
|
|
386
|
+
|
|
387
|
+
normalized_name = str(hook_name or "").strip()
|
|
388
|
+
if not normalized_name:
|
|
389
|
+
raise ValueError("Hook 名称不能为空")
|
|
390
|
+
return normalized_name
|
|
391
|
+
|
|
392
|
+
def _resolve_timeout_ms(self, hook_spec: HookSpec, target: _HookInvocationTarget) -> int:
|
|
393
|
+
"""计算单个处理器的实际超时。
|
|
394
|
+
|
|
395
|
+
Args:
|
|
396
|
+
hook_spec: 当前 Hook 的规格定义。
|
|
397
|
+
target: 当前执行目标。
|
|
398
|
+
|
|
399
|
+
Returns:
|
|
400
|
+
int: 最终生效的超时毫秒数。
|
|
401
|
+
"""
|
|
402
|
+
|
|
403
|
+
if target.entry.timeout_ms > 0:
|
|
404
|
+
return target.entry.timeout_ms
|
|
405
|
+
if hook_spec.default_timeout_ms > 0:
|
|
406
|
+
return hook_spec.default_timeout_ms
|
|
407
|
+
return self._get_default_timeout_ms()
|
|
408
|
+
|
|
409
|
+
async def _invoke_handler(
|
|
410
|
+
self,
|
|
411
|
+
hook_name: str,
|
|
412
|
+
hook_spec: HookSpec,
|
|
413
|
+
target: _HookInvocationTarget,
|
|
414
|
+
kwargs: Dict[str, Any],
|
|
415
|
+
) -> HookHandlerExecutionResult:
|
|
416
|
+
"""执行单个 Hook 处理器。
|
|
417
|
+
|
|
418
|
+
Args:
|
|
419
|
+
hook_name: 当前 Hook 名称。
|
|
420
|
+
hook_spec: 当前 Hook 规格。
|
|
421
|
+
target: 当前执行目标。
|
|
422
|
+
kwargs: 当前参数字典。
|
|
423
|
+
|
|
424
|
+
Returns:
|
|
425
|
+
HookHandlerExecutionResult: 处理器执行结果。
|
|
426
|
+
"""
|
|
427
|
+
|
|
428
|
+
timeout_ms = self._resolve_timeout_ms(hook_spec, target)
|
|
429
|
+
request_args: Dict[str, Any] = {"hook_name": hook_name, **dict(kwargs)}
|
|
430
|
+
if is_shutdown_requested():
|
|
431
|
+
return HookHandlerExecutionResult(
|
|
432
|
+
handler_name=target.entry.full_name,
|
|
433
|
+
plugin_id=target.entry.plugin_id,
|
|
434
|
+
)
|
|
435
|
+
|
|
436
|
+
circuit_permit = get_plugin_circuit_breaker().try_acquire(
|
|
437
|
+
target.entry.plugin_id,
|
|
438
|
+
target.entry.full_name,
|
|
439
|
+
f"hook:{hook_name}",
|
|
440
|
+
)
|
|
441
|
+
if not circuit_permit.allowed:
|
|
442
|
+
return HookHandlerExecutionResult(
|
|
443
|
+
handler_name=target.entry.full_name,
|
|
444
|
+
plugin_id=target.entry.plugin_id,
|
|
445
|
+
)
|
|
446
|
+
|
|
447
|
+
try:
|
|
448
|
+
response_envelope = await asyncio.wait_for(
|
|
449
|
+
target.supervisor.invoke_plugin(
|
|
450
|
+
"plugin.invoke_hook",
|
|
451
|
+
target.entry.plugin_id,
|
|
452
|
+
target.entry.name,
|
|
453
|
+
request_args,
|
|
454
|
+
timeout_ms=timeout_ms,
|
|
455
|
+
),
|
|
456
|
+
timeout=max(timeout_ms / 1000.0, 0.001),
|
|
457
|
+
)
|
|
458
|
+
except asyncio.TimeoutError:
|
|
459
|
+
get_plugin_circuit_breaker().record_failure(circuit_permit, f"HookHandler 超时 {timeout_ms}ms")
|
|
460
|
+
error_message = (
|
|
461
|
+
f"HookHandler {target.entry.full_name} 执行超时,已超过 {timeout_ms}ms"
|
|
462
|
+
)
|
|
463
|
+
logger.error(error_message)
|
|
464
|
+
return HookHandlerExecutionResult(
|
|
465
|
+
handler_name=target.entry.full_name,
|
|
466
|
+
plugin_id=target.entry.plugin_id,
|
|
467
|
+
success=False,
|
|
468
|
+
error_message=error_message,
|
|
469
|
+
)
|
|
470
|
+
except RPCError as exc:
|
|
471
|
+
if self._should_record_circuit_failure(exc):
|
|
472
|
+
get_plugin_circuit_breaker().record_failure(circuit_permit, str(exc))
|
|
473
|
+
error_message = f"HookHandler {target.entry.full_name} 执行失败: {exc}"
|
|
474
|
+
logger.error(error_message, exc_info=True)
|
|
475
|
+
return HookHandlerExecutionResult(
|
|
476
|
+
handler_name=target.entry.full_name,
|
|
477
|
+
plugin_id=target.entry.plugin_id,
|
|
478
|
+
success=False,
|
|
479
|
+
error_message=error_message,
|
|
480
|
+
)
|
|
481
|
+
except Exception as exc:
|
|
482
|
+
error_message = f"HookHandler {target.entry.full_name} 执行失败: {exc}"
|
|
483
|
+
logger.error(error_message, exc_info=True)
|
|
484
|
+
return HookHandlerExecutionResult(
|
|
485
|
+
handler_name=target.entry.full_name,
|
|
486
|
+
plugin_id=target.entry.plugin_id,
|
|
487
|
+
success=False,
|
|
488
|
+
error_message=error_message,
|
|
489
|
+
)
|
|
490
|
+
|
|
491
|
+
get_plugin_circuit_breaker().record_success(circuit_permit)
|
|
492
|
+
response_payload = response_envelope.payload
|
|
493
|
+
if not isinstance(response_payload, dict):
|
|
494
|
+
return HookHandlerExecutionResult(
|
|
495
|
+
handler_name=target.entry.full_name,
|
|
496
|
+
plugin_id=target.entry.plugin_id,
|
|
497
|
+
custom_result=response_payload,
|
|
498
|
+
)
|
|
499
|
+
|
|
500
|
+
return HookHandlerExecutionResult(
|
|
501
|
+
handler_name=target.entry.full_name,
|
|
502
|
+
plugin_id=target.entry.plugin_id,
|
|
503
|
+
success=bool(response_payload.get("success", True)),
|
|
504
|
+
action=self._normalize_action(response_payload.get("action", "continue")),
|
|
505
|
+
modified_kwargs=self._extract_modified_kwargs(response_payload.get("modified_kwargs")),
|
|
506
|
+
custom_result=response_payload.get("custom_result"),
|
|
507
|
+
error_message=str(response_payload.get("error_message", "") or ""),
|
|
508
|
+
)
|
|
509
|
+
|
|
510
|
+
@staticmethod
|
|
511
|
+
def _should_record_circuit_failure(exc: RPCError) -> bool:
|
|
512
|
+
"""判断 RPC 异常是否应计入插件熔断。"""
|
|
513
|
+
|
|
514
|
+
return exc.code in {ErrorCode.E_TIMEOUT, ErrorCode.E_PLUGIN_CRASHED}
|
|
515
|
+
|
|
516
|
+
@staticmethod
|
|
517
|
+
def _extract_modified_kwargs(raw_value: Any) -> Optional[Dict[str, Any]]:
|
|
518
|
+
"""提取并校验处理器返回的 `modified_kwargs`。
|
|
519
|
+
|
|
520
|
+
Args:
|
|
521
|
+
raw_value: 原始返回值。
|
|
522
|
+
|
|
523
|
+
Returns:
|
|
524
|
+
Optional[Dict[str, Any]]: 合法时返回字典,否则返回 `None`。
|
|
525
|
+
"""
|
|
526
|
+
|
|
527
|
+
if raw_value is None:
|
|
528
|
+
return None
|
|
529
|
+
if isinstance(raw_value, dict):
|
|
530
|
+
return dict(raw_value)
|
|
531
|
+
logger.warning("HookHandler 返回的 modified_kwargs 不是字典,已忽略")
|
|
532
|
+
return None
|
|
533
|
+
|
|
534
|
+
@staticmethod
|
|
535
|
+
def _normalize_action(raw_value: Any) -> str:
|
|
536
|
+
"""规范化处理器动作返回值。
|
|
537
|
+
|
|
538
|
+
Args:
|
|
539
|
+
raw_value: 原始动作值。
|
|
540
|
+
|
|
541
|
+
Returns:
|
|
542
|
+
str: 规范化后的动作值,仅支持 `continue` 或 `abort`。
|
|
543
|
+
"""
|
|
544
|
+
|
|
545
|
+
normalized_value = str(raw_value or "").strip().lower() or "continue"
|
|
546
|
+
if normalized_value not in {"continue", "abort"}:
|
|
547
|
+
logger.warning(f"未知的 Hook action: {raw_value},已按 continue 处理")
|
|
548
|
+
return "continue"
|
|
549
|
+
return normalized_value
|
|
550
|
+
|
|
551
|
+
def _merge_blocking_result(
|
|
552
|
+
self,
|
|
553
|
+
hook_spec: HookSpec,
|
|
554
|
+
target: _HookInvocationTarget,
|
|
555
|
+
execution_result: HookHandlerExecutionResult,
|
|
556
|
+
dispatch_result: HookDispatchResult,
|
|
557
|
+
) -> None:
|
|
558
|
+
"""合并阻塞处理器结果到聚合结果。
|
|
559
|
+
|
|
560
|
+
Args:
|
|
561
|
+
hook_spec: 当前 Hook 规格。
|
|
562
|
+
target: 当前执行目标。
|
|
563
|
+
execution_result: 当前处理器执行结果。
|
|
564
|
+
dispatch_result: 当前聚合结果对象。
|
|
565
|
+
"""
|
|
566
|
+
|
|
567
|
+
if execution_result.custom_result is not None:
|
|
568
|
+
dispatch_result.custom_results.append(execution_result.custom_result)
|
|
569
|
+
|
|
570
|
+
if not execution_result.success:
|
|
571
|
+
error_message = execution_result.error_message or f"HookHandler {target.entry.full_name} 执行失败"
|
|
572
|
+
dispatch_result.errors.append(error_message)
|
|
573
|
+
self._apply_error_policy(target, hook_spec, dispatch_result, error_message)
|
|
574
|
+
return
|
|
575
|
+
|
|
576
|
+
if execution_result.modified_kwargs is not None:
|
|
577
|
+
if hook_spec.allow_kwargs_mutation:
|
|
578
|
+
dispatch_result.kwargs = dict(execution_result.modified_kwargs)
|
|
579
|
+
else:
|
|
580
|
+
error_message = (
|
|
581
|
+
f"Hook {dispatch_result.hook_name} 不允许修改 kwargs,"
|
|
582
|
+
f"已忽略 {target.entry.full_name} 的 modified_kwargs"
|
|
583
|
+
)
|
|
584
|
+
logger.warning(error_message)
|
|
585
|
+
dispatch_result.errors.append(error_message)
|
|
586
|
+
|
|
587
|
+
if execution_result.action == "abort":
|
|
588
|
+
if hook_spec.allow_abort:
|
|
589
|
+
dispatch_result.aborted = True
|
|
590
|
+
dispatch_result.stopped_by = target.entry.full_name
|
|
591
|
+
logger.info(f"HookHandler {target.entry.full_name} 中止了 Hook {dispatch_result.hook_name}")
|
|
592
|
+
else:
|
|
593
|
+
error_message = (
|
|
594
|
+
f"Hook {dispatch_result.hook_name} 不允许 abort,"
|
|
595
|
+
f"已忽略 {target.entry.full_name} 的 abort 请求"
|
|
596
|
+
)
|
|
597
|
+
logger.warning(error_message)
|
|
598
|
+
dispatch_result.errors.append(error_message)
|
|
599
|
+
|
|
600
|
+
def _apply_error_policy(
|
|
601
|
+
self,
|
|
602
|
+
target: _HookInvocationTarget,
|
|
603
|
+
hook_spec: HookSpec,
|
|
604
|
+
dispatch_result: HookDispatchResult,
|
|
605
|
+
error_message: str,
|
|
606
|
+
) -> None:
|
|
607
|
+
"""根据错误策略处理阻塞处理器失败。
|
|
608
|
+
|
|
609
|
+
Args:
|
|
610
|
+
target: 触发错误的处理器目标。
|
|
611
|
+
hook_spec: 当前 Hook 规格。
|
|
612
|
+
dispatch_result: 当前聚合结果对象。
|
|
613
|
+
error_message: 需要记录的错误描述。
|
|
614
|
+
"""
|
|
615
|
+
|
|
616
|
+
if target.entry.error_policy != "abort":
|
|
617
|
+
return
|
|
618
|
+
if not hook_spec.allow_abort:
|
|
619
|
+
logger.warning(
|
|
620
|
+
f"Hook {dispatch_result.hook_name} 禁止 abort,"
|
|
621
|
+
f"已将 {target.entry.full_name} 的错误策略按 skip 处理"
|
|
622
|
+
)
|
|
623
|
+
return
|
|
624
|
+
|
|
625
|
+
dispatch_result.aborted = True
|
|
626
|
+
dispatch_result.stopped_by = target.entry.full_name
|
|
627
|
+
logger.warning(
|
|
628
|
+
f"HookHandler {target.entry.full_name} 因错误策略 abort "
|
|
629
|
+
f"中止了 Hook {dispatch_result.hook_name}: {error_message}"
|
|
630
|
+
)
|
|
631
|
+
|
|
632
|
+
def _schedule_observe_handler(
|
|
633
|
+
self,
|
|
634
|
+
hook_name: str,
|
|
635
|
+
hook_spec: HookSpec,
|
|
636
|
+
target: _HookInvocationTarget,
|
|
637
|
+
kwargs: Dict[str, Any],
|
|
638
|
+
) -> None:
|
|
639
|
+
"""后台调度观察型处理器。
|
|
640
|
+
|
|
641
|
+
Args:
|
|
642
|
+
hook_name: 当前 Hook 名称。
|
|
643
|
+
hook_spec: 当前 Hook 规格。
|
|
644
|
+
target: 当前观察型处理器目标。
|
|
645
|
+
kwargs: 调用参数快照。
|
|
646
|
+
"""
|
|
647
|
+
|
|
648
|
+
if not hook_spec.allow_observe:
|
|
649
|
+
logger.warning(f"Hook {hook_name} 不允许 observe 处理器,已跳过 {target.entry.full_name}")
|
|
650
|
+
return
|
|
651
|
+
if is_shutdown_requested():
|
|
652
|
+
return
|
|
653
|
+
|
|
654
|
+
task = asyncio.create_task(
|
|
655
|
+
self._run_observe_handler(
|
|
656
|
+
hook_name=hook_name,
|
|
657
|
+
hook_spec=hook_spec,
|
|
658
|
+
target=target,
|
|
659
|
+
kwargs=dict(kwargs),
|
|
660
|
+
)
|
|
661
|
+
)
|
|
662
|
+
self._background_tasks.add(task)
|
|
663
|
+
task.add_done_callback(self._handle_background_task_done)
|
|
664
|
+
|
|
665
|
+
async def _run_observe_handler(
|
|
666
|
+
self,
|
|
667
|
+
hook_name: str,
|
|
668
|
+
hook_spec: HookSpec,
|
|
669
|
+
target: _HookInvocationTarget,
|
|
670
|
+
kwargs: Dict[str, Any],
|
|
671
|
+
) -> None:
|
|
672
|
+
"""执行观察型处理器并吞掉控制流副作用。
|
|
673
|
+
|
|
674
|
+
Args:
|
|
675
|
+
hook_name: 当前 Hook 名称。
|
|
676
|
+
hook_spec: 当前 Hook 规格。
|
|
677
|
+
target: 当前观察型处理器目标。
|
|
678
|
+
kwargs: 调用参数快照。
|
|
679
|
+
"""
|
|
680
|
+
|
|
681
|
+
execution_result = await self._invoke_handler(
|
|
682
|
+
hook_name=hook_name,
|
|
683
|
+
hook_spec=hook_spec,
|
|
684
|
+
target=target,
|
|
685
|
+
kwargs=kwargs,
|
|
686
|
+
)
|
|
687
|
+
|
|
688
|
+
if not execution_result.success:
|
|
689
|
+
logger.warning(
|
|
690
|
+
f"观察型 HookHandler {target.entry.full_name} 执行失败: "
|
|
691
|
+
f"{execution_result.error_message or '未知错误'}"
|
|
692
|
+
)
|
|
693
|
+
return
|
|
694
|
+
|
|
695
|
+
if execution_result.modified_kwargs is not None:
|
|
696
|
+
logger.warning(f"观察型 HookHandler {target.entry.full_name} 返回了 modified_kwargs,已忽略")
|
|
697
|
+
if execution_result.action == "abort":
|
|
698
|
+
logger.warning(f"观察型 HookHandler {target.entry.full_name} 请求 abort,已忽略")
|
|
699
|
+
|
|
700
|
+
def _handle_background_task_done(self, task: asyncio.Task[Any]) -> None:
|
|
701
|
+
"""处理观察任务完成回调。
|
|
702
|
+
|
|
703
|
+
Args:
|
|
704
|
+
task: 已完成的后台任务。
|
|
705
|
+
"""
|
|
706
|
+
|
|
707
|
+
self._background_tasks.discard(task)
|
|
708
|
+
with contextlib.suppress(asyncio.CancelledError):
|
|
709
|
+
exception = task.exception()
|
|
710
|
+
if exception is not None:
|
|
711
|
+
logger.error(f"观察型 Hook 后台任务执行失败: {exception}")
|