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,646 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict, List, Optional, Set
|
|
4
|
+
|
|
5
|
+
import time
|
|
6
|
+
import unicodedata
|
|
7
|
+
|
|
8
|
+
from ...utils import profile_policy
|
|
9
|
+
from ...utils.feedback_policy import (
|
|
10
|
+
feedback_cfg_profile_force_refresh_on_read,
|
|
11
|
+
feedback_cfg_profile_refresh_enabled,
|
|
12
|
+
feedback_cfg_reconcile_batch_size,
|
|
13
|
+
)
|
|
14
|
+
from ...utils.metadata import coerce_metadata_dict
|
|
15
|
+
from ...utils.profile_evidence import profile_evidence_type_from_source, profile_relation_content
|
|
16
|
+
from ...utils.runtime_payloads import tokens
|
|
17
|
+
from .base import KernelServiceBase
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class MemoryProfileAdminService(KernelServiceBase):
|
|
21
|
+
@staticmethod
|
|
22
|
+
def _profile_relevance_grams(value: str) -> Set[str]:
|
|
23
|
+
normalized = unicodedata.normalize("NFKC", value).casefold()
|
|
24
|
+
token = "".join(char for char in normalized if char.isalnum())
|
|
25
|
+
ignored = {"用户", "这个", "那个", "自己", "喜欢"}
|
|
26
|
+
return {token[index : index + 2] for index in range(len(token) - 1)} - ignored
|
|
27
|
+
|
|
28
|
+
def _uncertain_profile_candidates(self, person_id: str, context_text: str) -> Dict[str, Any]:
|
|
29
|
+
"""从最近的未确认事实中选择当前消息相关的候选。"""
|
|
30
|
+
|
|
31
|
+
assert self.metadata_store is not None
|
|
32
|
+
count = self.metadata_store.count_uncertain_person_fact_claims(person_id)
|
|
33
|
+
claims = self.metadata_store.list_uncertain_person_fact_claims(person_id, limit=512) if context_text else []
|
|
34
|
+
query_grams = self._profile_relevance_grams(context_text)
|
|
35
|
+
ranked = []
|
|
36
|
+
if query_grams:
|
|
37
|
+
for claim in claims:
|
|
38
|
+
overlap = query_grams & self._profile_relevance_grams(str(claim.get("value_text", "")))
|
|
39
|
+
if overlap:
|
|
40
|
+
ranked.append((len(overlap), float(claim.get("last_confirmed_at", 0)), claim))
|
|
41
|
+
ranked.sort(key=lambda item: (-item[0], -item[1], str(item[2].get("claim_id", ""))))
|
|
42
|
+
return {
|
|
43
|
+
"uncertain_fact_count": count,
|
|
44
|
+
"uncertain_candidates": [
|
|
45
|
+
{"claim_id": str(claim.get("claim_id", "")), "text": str(claim.get("value_text", ""))}
|
|
46
|
+
for _, _, claim in ranked[:2]
|
|
47
|
+
],
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
def _mark_person_active(self, person_id: str) -> None:
|
|
51
|
+
token = str(person_id or "").strip()
|
|
52
|
+
if not token:
|
|
53
|
+
return
|
|
54
|
+
self._active_person_timestamps[token] = time.time()
|
|
55
|
+
|
|
56
|
+
def _enqueue_person_profile_refresh(self, person_id: str, *, reason: str = "") -> bool:
|
|
57
|
+
if self.metadata_store is None or not bool(self._cfg("person_profile.enabled", True)):
|
|
58
|
+
return False
|
|
59
|
+
payload = self.metadata_store.enqueue_person_profile_refresh(
|
|
60
|
+
person_id=person_id,
|
|
61
|
+
reason=str(reason or "").strip() or "memory_ingest",
|
|
62
|
+
)
|
|
63
|
+
return isinstance(payload, dict)
|
|
64
|
+
|
|
65
|
+
def _has_pending_person_profile_refresh(self, person_id: str) -> bool:
|
|
66
|
+
if self.metadata_store is None:
|
|
67
|
+
return False
|
|
68
|
+
request = self.metadata_store.get_person_profile_refresh_request(person_id)
|
|
69
|
+
if not isinstance(request, dict):
|
|
70
|
+
return False
|
|
71
|
+
status = str(request.get("status", "") or "").strip().lower()
|
|
72
|
+
if status in {"pending", "running"}:
|
|
73
|
+
return True
|
|
74
|
+
if status != "failed":
|
|
75
|
+
return False
|
|
76
|
+
return int(request.get("retry_count", 0) or 0) < profile_policy.person_profile_refresh_max_retry(self._cfg)
|
|
77
|
+
|
|
78
|
+
@staticmethod
|
|
79
|
+
def _empty_person_profile_response(*, person_id: str = "", person_name: str = "") -> Dict[str, Any]:
|
|
80
|
+
return {
|
|
81
|
+
"summary": "",
|
|
82
|
+
"traits": [],
|
|
83
|
+
"evidence": [],
|
|
84
|
+
"person_id": str(person_id or "").strip(),
|
|
85
|
+
"person_name": str(person_name or "").strip(),
|
|
86
|
+
"profile_source": "",
|
|
87
|
+
"has_manual_override": False,
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async def _query_person_profile_with_feedback_refresh(
|
|
91
|
+
self,
|
|
92
|
+
*,
|
|
93
|
+
person_id: str = "",
|
|
94
|
+
person_keyword: str = "",
|
|
95
|
+
limit: int = 10,
|
|
96
|
+
force_refresh: bool = False,
|
|
97
|
+
source_note: str,
|
|
98
|
+
) -> Dict[str, Any]:
|
|
99
|
+
assert self.metadata_store is not None
|
|
100
|
+
assert self.person_profile_service is not None
|
|
101
|
+
|
|
102
|
+
pid = str(person_id or "").strip()
|
|
103
|
+
if not pid and person_keyword:
|
|
104
|
+
pid = self.person_profile_service.resolve_person_id(str(person_keyword or "").strip())
|
|
105
|
+
|
|
106
|
+
dirty_request = self.metadata_store.get_person_profile_refresh_request(pid) if pid else None
|
|
107
|
+
should_force_refresh = bool(force_refresh)
|
|
108
|
+
if (
|
|
109
|
+
pid
|
|
110
|
+
and feedback_cfg_profile_refresh_enabled()
|
|
111
|
+
and feedback_cfg_profile_force_refresh_on_read()
|
|
112
|
+
and isinstance(dirty_request, dict)
|
|
113
|
+
and str(dirty_request.get("status", "") or "").strip().lower() in {"pending", "running", "failed"}
|
|
114
|
+
):
|
|
115
|
+
should_force_refresh = True
|
|
116
|
+
|
|
117
|
+
profile = await self.person_profile_service.query_person_profile(
|
|
118
|
+
person_id=pid,
|
|
119
|
+
person_keyword=str(person_keyword or "").strip(),
|
|
120
|
+
top_k=max(1, int(limit or 10)),
|
|
121
|
+
force_refresh=should_force_refresh,
|
|
122
|
+
source_note=source_note,
|
|
123
|
+
)
|
|
124
|
+
payload = profile if isinstance(profile, dict) else {"success": False, "error": "invalid profile payload"}
|
|
125
|
+
if dirty_request:
|
|
126
|
+
payload["feedback_refresh_request"] = dirty_request
|
|
127
|
+
if should_force_refresh and dirty_request and not bool(payload.get("success")):
|
|
128
|
+
payload.setdefault("error", "feedback_refresh_failed")
|
|
129
|
+
payload["feedback_refresh_failed"] = True
|
|
130
|
+
return payload
|
|
131
|
+
|
|
132
|
+
def _build_person_profile_response(
|
|
133
|
+
self,
|
|
134
|
+
profile: Dict[str, Any],
|
|
135
|
+
*,
|
|
136
|
+
requested_person_id: str,
|
|
137
|
+
limit: int,
|
|
138
|
+
) -> Dict[str, Any]:
|
|
139
|
+
assert self.metadata_store is not None
|
|
140
|
+
if not bool(profile.get("success")):
|
|
141
|
+
return self._empty_person_profile_response(
|
|
142
|
+
person_id=str(profile.get("person_id", "") or requested_person_id),
|
|
143
|
+
person_name=str(profile.get("person_name", "") or ""),
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
evidence: List[Dict[str, Any]] = []
|
|
147
|
+
evidence_limit = max(1, int(limit or 10))
|
|
148
|
+
for hash_value in profile.get("evidence_ids", [])[:evidence_limit]:
|
|
149
|
+
paragraph = self.metadata_store.get_paragraph(hash_value)
|
|
150
|
+
if paragraph is not None:
|
|
151
|
+
evidence.append(
|
|
152
|
+
{
|
|
153
|
+
"hash": hash_value,
|
|
154
|
+
"content": str(paragraph.get("content", "") or "")[:220],
|
|
155
|
+
"metadata": paragraph.get("metadata", {}) or {},
|
|
156
|
+
"type": "paragraph",
|
|
157
|
+
}
|
|
158
|
+
)
|
|
159
|
+
continue
|
|
160
|
+
|
|
161
|
+
relation = self.metadata_store.get_relation(hash_value)
|
|
162
|
+
if relation is not None:
|
|
163
|
+
evidence.append(
|
|
164
|
+
{
|
|
165
|
+
"hash": hash_value,
|
|
166
|
+
"content": " ".join(
|
|
167
|
+
[
|
|
168
|
+
str(relation.get("subject", "") or "").strip(),
|
|
169
|
+
str(relation.get("predicate", "") or "").strip(),
|
|
170
|
+
str(relation.get("object", "") or "").strip(),
|
|
171
|
+
]
|
|
172
|
+
).strip(),
|
|
173
|
+
"metadata": {
|
|
174
|
+
"confidence": relation.get("confidence"),
|
|
175
|
+
"source_paragraph": relation.get("source_paragraph"),
|
|
176
|
+
},
|
|
177
|
+
"type": "relation",
|
|
178
|
+
}
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
evidence = self._filter_user_visible_hits(evidence)
|
|
182
|
+
text = str(profile.get("profile_text", "") or "").strip()
|
|
183
|
+
traits = [line.strip("- ").strip() for line in text.splitlines() if line.strip()][:8]
|
|
184
|
+
return {
|
|
185
|
+
"summary": text,
|
|
186
|
+
"traits": traits,
|
|
187
|
+
"evidence": evidence,
|
|
188
|
+
"person_id": str(profile.get("person_id", "") or requested_person_id),
|
|
189
|
+
"person_name": str(profile.get("person_name", "") or ""),
|
|
190
|
+
"profile_source": str(profile.get("profile_source", "") or "auto_snapshot"),
|
|
191
|
+
"has_manual_override": bool(profile.get("has_manual_override", False)),
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async def get_person_profile(self, *, person_id: str, chat_id: str = "", limit: int = 10) -> Dict[str, Any]:
|
|
195
|
+
del chat_id
|
|
196
|
+
await self.initialize()
|
|
197
|
+
assert self.metadata_store is not None
|
|
198
|
+
assert self.person_profile_service is not None
|
|
199
|
+
self._mark_person_active(person_id)
|
|
200
|
+
profile = await self._query_person_profile_with_feedback_refresh(
|
|
201
|
+
person_id=person_id,
|
|
202
|
+
limit=max(4, int(limit or 10)),
|
|
203
|
+
source_note="sdk_memory_kernel.get_person_profile",
|
|
204
|
+
)
|
|
205
|
+
return self._build_person_profile_response(profile, requested_person_id=person_id, limit=limit)
|
|
206
|
+
|
|
207
|
+
async def refresh_person_profile(
|
|
208
|
+
self, person_id: str, limit: int = 10, *, mark_active: bool = True
|
|
209
|
+
) -> Dict[str, Any]:
|
|
210
|
+
await self.initialize()
|
|
211
|
+
assert self.person_profile_service
|
|
212
|
+
if mark_active:
|
|
213
|
+
self._mark_person_active(person_id)
|
|
214
|
+
profile = await self.person_profile_service.query_person_profile(
|
|
215
|
+
person_id=person_id,
|
|
216
|
+
top_k=max(4, int(limit or 10)),
|
|
217
|
+
force_refresh=True,
|
|
218
|
+
source_note="sdk_memory_kernel.refresh_person_profile",
|
|
219
|
+
)
|
|
220
|
+
return profile if isinstance(profile, dict) else {}
|
|
221
|
+
|
|
222
|
+
async def memory_profile_admin(self, *, action: str, **kwargs) -> Dict[str, Any]:
|
|
223
|
+
await self.initialize()
|
|
224
|
+
assert self.metadata_store is not None
|
|
225
|
+
assert self.person_profile_service is not None
|
|
226
|
+
|
|
227
|
+
act = str(action or "").strip().lower()
|
|
228
|
+
if act == "query":
|
|
229
|
+
profile = await self._query_person_profile_with_feedback_refresh(
|
|
230
|
+
person_id=str(kwargs.get("person_id", "") or "").strip(),
|
|
231
|
+
person_keyword=str(kwargs.get("person_keyword", "") or kwargs.get("keyword", "") or "").strip(),
|
|
232
|
+
limit=max(1, int(kwargs.get("limit", kwargs.get("top_k", 12)) or 12)),
|
|
233
|
+
force_refresh=bool(kwargs.get("force_refresh", False)),
|
|
234
|
+
source_note="sdk_memory_kernel.memory_profile_admin.query",
|
|
235
|
+
)
|
|
236
|
+
if not isinstance(profile, dict):
|
|
237
|
+
return {"success": False, "error": "invalid profile payload"}
|
|
238
|
+
person_id = str(profile.get("person_id", "") or "").strip()
|
|
239
|
+
if bool(profile.get("success")) and person_id:
|
|
240
|
+
profile.update(
|
|
241
|
+
self._uncertain_profile_candidates(
|
|
242
|
+
person_id,
|
|
243
|
+
str(kwargs.get("context_text", "") or ""),
|
|
244
|
+
)
|
|
245
|
+
)
|
|
246
|
+
if bool(profile.get("has_manual_override")):
|
|
247
|
+
profile["uncertain_candidates"] = []
|
|
248
|
+
return profile
|
|
249
|
+
|
|
250
|
+
if act == "evidence":
|
|
251
|
+
return await self._profile_evidence_admin(
|
|
252
|
+
person_id=str(kwargs.get("person_id", "") or "").strip(),
|
|
253
|
+
person_keyword=str(kwargs.get("person_keyword", "") or kwargs.get("keyword", "") or "").strip(),
|
|
254
|
+
limit=max(1, int(kwargs.get("limit", kwargs.get("top_k", 12)) or 12)),
|
|
255
|
+
force_refresh=bool(kwargs.get("force_refresh", False)),
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
if act == "correct_evidence":
|
|
259
|
+
return await self._profile_correct_evidence_admin(
|
|
260
|
+
person_id=str(kwargs.get("person_id", "") or "").strip(),
|
|
261
|
+
person_keyword=str(kwargs.get("person_keyword", "") or kwargs.get("keyword", "") or "").strip(),
|
|
262
|
+
evidence_type=str(kwargs.get("evidence_type", "") or "").strip(),
|
|
263
|
+
hash_value=str(kwargs.get("hash", "") or kwargs.get("hash_value", "") or "").strip(),
|
|
264
|
+
requested_by=str(kwargs.get("requested_by", "") or "webui").strip(),
|
|
265
|
+
reason=str(kwargs.get("reason", "") or "profile_evidence_correction").strip(),
|
|
266
|
+
refresh=bool(kwargs.get("refresh", True)),
|
|
267
|
+
limit=max(1, int(kwargs.get("limit", kwargs.get("top_k", 12)) or 12)),
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
if act == "status":
|
|
271
|
+
summary = self.metadata_store.get_person_profile_refresh_summary(
|
|
272
|
+
failed_limit=max(1, int(kwargs.get("limit", 20) or 20))
|
|
273
|
+
)
|
|
274
|
+
return {"success": True, **summary}
|
|
275
|
+
|
|
276
|
+
if act == "process_pending":
|
|
277
|
+
result = await self._process_feedback_profile_refresh_batch(
|
|
278
|
+
limit=max(
|
|
279
|
+
1,
|
|
280
|
+
int(
|
|
281
|
+
kwargs.get("limit", feedback_cfg_reconcile_batch_size()) or feedback_cfg_reconcile_batch_size()
|
|
282
|
+
),
|
|
283
|
+
)
|
|
284
|
+
)
|
|
285
|
+
return {"success": True, **result}
|
|
286
|
+
|
|
287
|
+
if act == "list":
|
|
288
|
+
limit = max(1, int(kwargs.get("limit", 50) or 50))
|
|
289
|
+
rows = self.metadata_store.query(
|
|
290
|
+
"""
|
|
291
|
+
SELECT s.person_id, s.profile_version, s.profile_text, s.updated_at, s.expires_at, s.source_note
|
|
292
|
+
FROM person_profile_snapshots s
|
|
293
|
+
JOIN (
|
|
294
|
+
SELECT person_id, MAX(profile_version) AS max_version
|
|
295
|
+
FROM person_profile_snapshots
|
|
296
|
+
GROUP BY person_id
|
|
297
|
+
) latest
|
|
298
|
+
ON latest.person_id = s.person_id
|
|
299
|
+
AND latest.max_version = s.profile_version
|
|
300
|
+
ORDER BY s.updated_at DESC
|
|
301
|
+
LIMIT ?
|
|
302
|
+
""",
|
|
303
|
+
(limit,),
|
|
304
|
+
)
|
|
305
|
+
items = []
|
|
306
|
+
for row in rows:
|
|
307
|
+
person_id = str(row.get("person_id", "") or "").strip()
|
|
308
|
+
override = self.metadata_store.get_person_profile_override(person_id)
|
|
309
|
+
items.append(
|
|
310
|
+
{
|
|
311
|
+
"person_id": person_id,
|
|
312
|
+
"profile_version": int(row.get("profile_version", 0) or 0),
|
|
313
|
+
"profile_text": str(row.get("profile_text", "") or ""),
|
|
314
|
+
"updated_at": row.get("updated_at"),
|
|
315
|
+
"expires_at": row.get("expires_at"),
|
|
316
|
+
"source_note": str(row.get("source_note", "") or ""),
|
|
317
|
+
"has_manual_override": bool(override),
|
|
318
|
+
"manual_override": override,
|
|
319
|
+
}
|
|
320
|
+
)
|
|
321
|
+
return {"success": True, "items": items, "count": len(items)}
|
|
322
|
+
|
|
323
|
+
if act == "get_aliases":
|
|
324
|
+
person_id = str(kwargs.get("person_id", "") or "").strip()
|
|
325
|
+
if not person_id:
|
|
326
|
+
return {"success": False, "error": "person_id 不能为空"}
|
|
327
|
+
return {"success": True, **self.person_profile_service.get_person_alias_details(person_id)}
|
|
328
|
+
|
|
329
|
+
if act == "set_aliases":
|
|
330
|
+
person_id = str(kwargs.get("person_id", "") or "").strip()
|
|
331
|
+
aliases = kwargs.get("aliases")
|
|
332
|
+
if not isinstance(aliases, list):
|
|
333
|
+
return {"success": False, "error": "aliases 必须是字符串列表"}
|
|
334
|
+
try:
|
|
335
|
+
with self.metadata_store.transaction(immediate=True) as conn:
|
|
336
|
+
override = self.metadata_store.set_person_profile_alias_override(
|
|
337
|
+
person_id=person_id,
|
|
338
|
+
aliases=aliases,
|
|
339
|
+
updated_by=str(kwargs.get("updated_by", "") or ""),
|
|
340
|
+
source=str(kwargs.get("source", "") or "memory_profile_admin"),
|
|
341
|
+
conn=conn,
|
|
342
|
+
)
|
|
343
|
+
refresh_request = None
|
|
344
|
+
if bool(self._cfg("person_profile.enabled", True)):
|
|
345
|
+
refresh_request = self.metadata_store.enqueue_person_profile_refresh(
|
|
346
|
+
person_id=person_id,
|
|
347
|
+
reason="person_aliases_updated",
|
|
348
|
+
conn=conn,
|
|
349
|
+
)
|
|
350
|
+
except ValueError as error:
|
|
351
|
+
return {"success": False, "error": str(error)}
|
|
352
|
+
return {
|
|
353
|
+
"success": True,
|
|
354
|
+
"override": override,
|
|
355
|
+
"refresh_queued": refresh_request is not None,
|
|
356
|
+
**self.person_profile_service.get_person_alias_details(person_id),
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if act == "delete_aliases":
|
|
360
|
+
person_id = str(kwargs.get("person_id", "") or "").strip()
|
|
361
|
+
if not person_id:
|
|
362
|
+
return {"success": False, "error": "person_id 不能为空"}
|
|
363
|
+
with self.metadata_store.transaction(immediate=True) as conn:
|
|
364
|
+
deleted = self.metadata_store.delete_person_profile_alias_override(person_id, conn=conn)
|
|
365
|
+
refresh_request = None
|
|
366
|
+
if bool(self._cfg("person_profile.enabled", True)):
|
|
367
|
+
refresh_request = self.metadata_store.enqueue_person_profile_refresh(
|
|
368
|
+
person_id=person_id,
|
|
369
|
+
reason="person_aliases_override_deleted",
|
|
370
|
+
conn=conn,
|
|
371
|
+
)
|
|
372
|
+
return {
|
|
373
|
+
"success": True,
|
|
374
|
+
"deleted": deleted,
|
|
375
|
+
"refresh_queued": refresh_request is not None,
|
|
376
|
+
**self.person_profile_service.get_person_alias_details(person_id),
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if act == "set_override":
|
|
380
|
+
person_id = str(kwargs.get("person_id", "") or "").strip()
|
|
381
|
+
override = self.metadata_store.set_person_profile_override(
|
|
382
|
+
person_id=person_id,
|
|
383
|
+
override_text=str(kwargs.get("override_text", "") or kwargs.get("text", "") or ""),
|
|
384
|
+
updated_by=str(kwargs.get("updated_by", "") or ""),
|
|
385
|
+
source=str(kwargs.get("source", "") or "memory_profile_admin"),
|
|
386
|
+
)
|
|
387
|
+
return {"success": True, "override": override}
|
|
388
|
+
|
|
389
|
+
if act == "delete_override":
|
|
390
|
+
person_id = str(kwargs.get("person_id", "") or "").strip()
|
|
391
|
+
deleted = self.metadata_store.delete_person_profile_override(person_id)
|
|
392
|
+
return {"success": bool(deleted), "deleted": bool(deleted), "person_id": person_id}
|
|
393
|
+
|
|
394
|
+
return {"success": False, "error": f"不支持的 profile action: {act}"}
|
|
395
|
+
|
|
396
|
+
@staticmethod
|
|
397
|
+
def _profile_evidence_type_from_source(source: str, metadata: Optional[Dict[str, Any]] = None) -> str:
|
|
398
|
+
return profile_evidence_type_from_source(source, metadata)
|
|
399
|
+
|
|
400
|
+
@staticmethod
|
|
401
|
+
def _profile_relation_content(relation: Dict[str, Any]) -> str:
|
|
402
|
+
return profile_relation_content(relation)
|
|
403
|
+
|
|
404
|
+
def _build_profile_relation_evidence_item(self, relation: Dict[str, Any], *, index: int) -> Dict[str, Any]:
|
|
405
|
+
relation_hash = str(relation.get("hash", "") or "").strip()
|
|
406
|
+
metadata = coerce_metadata_dict(relation.get("metadata"))
|
|
407
|
+
return {
|
|
408
|
+
"evidence_key": f"relation:{relation_hash or index}",
|
|
409
|
+
"evidence_type": "relation",
|
|
410
|
+
"hash": relation_hash,
|
|
411
|
+
"content": self._profile_relation_content(relation),
|
|
412
|
+
"source": str(relation.get("source_paragraph", "") or metadata.get("source", "") or "").strip(),
|
|
413
|
+
"source_type": "relation",
|
|
414
|
+
"metadata": metadata,
|
|
415
|
+
"score": None,
|
|
416
|
+
"confidence": relation.get("confidence"),
|
|
417
|
+
"correction_mode": "delete_relation",
|
|
418
|
+
"deletable": bool(relation_hash),
|
|
419
|
+
"not_deletable_reason": "" if relation_hash else "缺少关系 hash",
|
|
420
|
+
"raw": relation,
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
def _build_profile_paragraph_evidence_item(
|
|
424
|
+
self,
|
|
425
|
+
item: Dict[str, Any],
|
|
426
|
+
*,
|
|
427
|
+
index: int,
|
|
428
|
+
fallback_hash: str = "",
|
|
429
|
+
) -> Dict[str, Any]:
|
|
430
|
+
hash_value = str(item.get("hash", "") or fallback_hash or "").strip()
|
|
431
|
+
metadata = coerce_metadata_dict(item.get("metadata"))
|
|
432
|
+
source = str(item.get("source", "") or metadata.get("source", "") or "").strip()
|
|
433
|
+
content = str(item.get("content", "") or "").strip()
|
|
434
|
+
source_type = self._profile_evidence_type_from_source(source, metadata)
|
|
435
|
+
is_deleted = False
|
|
436
|
+
if hash_value:
|
|
437
|
+
try:
|
|
438
|
+
paragraph = self.metadata_store.get_paragraph(hash_value) if self.metadata_store else None
|
|
439
|
+
except Exception:
|
|
440
|
+
paragraph = None
|
|
441
|
+
if isinstance(paragraph, dict):
|
|
442
|
+
paragraph_metadata = coerce_metadata_dict(paragraph.get("metadata"))
|
|
443
|
+
metadata = {**paragraph_metadata, **metadata}
|
|
444
|
+
source = source or str(paragraph.get("source", "") or "").strip()
|
|
445
|
+
content = content or str(paragraph.get("content", "") or "").strip()
|
|
446
|
+
source_type = self._profile_evidence_type_from_source(source, metadata)
|
|
447
|
+
is_deleted = bool(paragraph.get("is_deleted", 0))
|
|
448
|
+
return {
|
|
449
|
+
"evidence_key": f"{source_type}:{hash_value or index}",
|
|
450
|
+
"evidence_type": source_type,
|
|
451
|
+
"hash": hash_value,
|
|
452
|
+
"content": self._trim_text(content, 260),
|
|
453
|
+
"source": source,
|
|
454
|
+
"source_type": source_type,
|
|
455
|
+
"metadata": metadata,
|
|
456
|
+
"score": item.get("score"),
|
|
457
|
+
"confidence": None,
|
|
458
|
+
"correction_mode": "delete_paragraph",
|
|
459
|
+
"deletable": bool(hash_value) and not is_deleted,
|
|
460
|
+
"not_deletable_reason": ""
|
|
461
|
+
if hash_value and not is_deleted
|
|
462
|
+
else ("证据已删除" if is_deleted else "缺少段落 hash"),
|
|
463
|
+
"raw": item,
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
def _build_profile_evidence_items(self, profile: Dict[str, Any]) -> List[Dict[str, Any]]:
|
|
467
|
+
assert self.metadata_store is not None
|
|
468
|
+
evidence: List[Dict[str, Any]] = []
|
|
469
|
+
seen: set[tuple[str, str]] = set()
|
|
470
|
+
|
|
471
|
+
def append(item: Dict[str, Any]) -> None:
|
|
472
|
+
evidence_type = str(item.get("evidence_type", "") or "").strip()
|
|
473
|
+
hash_value = str(item.get("hash", "") or "").strip()
|
|
474
|
+
key = (evidence_type, hash_value or str(item.get("evidence_key", "") or ""))
|
|
475
|
+
if not key[0] or key in seen:
|
|
476
|
+
return
|
|
477
|
+
seen.add(key)
|
|
478
|
+
evidence.append(item)
|
|
479
|
+
|
|
480
|
+
for index, relation in enumerate(profile.get("relation_edges") or [], start=1):
|
|
481
|
+
if isinstance(relation, dict):
|
|
482
|
+
append(self._build_profile_relation_evidence_item(relation, index=index))
|
|
483
|
+
|
|
484
|
+
for index, item in enumerate(profile.get("vector_evidence") or [], start=1):
|
|
485
|
+
if isinstance(item, dict):
|
|
486
|
+
append(self._build_profile_paragraph_evidence_item(item, index=index))
|
|
487
|
+
|
|
488
|
+
for index, hash_value in enumerate(tokens(profile.get("evidence_ids")), start=1):
|
|
489
|
+
if any(str(item.get("hash", "") or "").strip() == hash_value for item in evidence):
|
|
490
|
+
continue
|
|
491
|
+
paragraph = self.metadata_store.get_paragraph(hash_value)
|
|
492
|
+
if isinstance(paragraph, dict):
|
|
493
|
+
append(
|
|
494
|
+
self._build_profile_paragraph_evidence_item(
|
|
495
|
+
{
|
|
496
|
+
"hash": hash_value,
|
|
497
|
+
"content": str(paragraph.get("content", "") or ""),
|
|
498
|
+
"source": str(paragraph.get("source", "") or ""),
|
|
499
|
+
"metadata": coerce_metadata_dict(paragraph.get("metadata")),
|
|
500
|
+
},
|
|
501
|
+
index=index,
|
|
502
|
+
)
|
|
503
|
+
)
|
|
504
|
+
continue
|
|
505
|
+
relation = self.metadata_store.get_relation(hash_value)
|
|
506
|
+
if isinstance(relation, dict):
|
|
507
|
+
append(self._build_profile_relation_evidence_item(relation, index=index))
|
|
508
|
+
|
|
509
|
+
return evidence
|
|
510
|
+
|
|
511
|
+
def _profile_evidence_response(
|
|
512
|
+
self, profile: Dict[str, Any], *, requested_person_id: str, limit: int
|
|
513
|
+
) -> Dict[str, Any]:
|
|
514
|
+
if not bool(profile.get("success")):
|
|
515
|
+
return {
|
|
516
|
+
"success": False,
|
|
517
|
+
"error": str(profile.get("error", "") or "人物画像查询失败"),
|
|
518
|
+
"person_id": str(profile.get("person_id", "") or requested_person_id),
|
|
519
|
+
"evidence": [],
|
|
520
|
+
}
|
|
521
|
+
evidence = self._build_profile_evidence_items(profile)
|
|
522
|
+
return {
|
|
523
|
+
"success": True,
|
|
524
|
+
"person_id": str(profile.get("person_id", "") or requested_person_id),
|
|
525
|
+
"person_name": str(profile.get("person_name", "") or ""),
|
|
526
|
+
"profile_text": str(profile.get("profile_text", "") or ""),
|
|
527
|
+
"auto_profile_text": str(profile.get("auto_profile_text", "") or profile.get("profile_text", "") or ""),
|
|
528
|
+
"profile_version": profile.get("profile_version"),
|
|
529
|
+
"updated_at": profile.get("updated_at"),
|
|
530
|
+
"expires_at": profile.get("expires_at"),
|
|
531
|
+
"profile_source": str(profile.get("profile_source", "") or "auto_snapshot"),
|
|
532
|
+
"has_manual_override": bool(profile.get("has_manual_override", False)),
|
|
533
|
+
"manual_override_text": str(profile.get("manual_override_text", "") or ""),
|
|
534
|
+
"evidence": evidence[: max(1, int(limit or 12))],
|
|
535
|
+
"evidence_count": len(evidence),
|
|
536
|
+
"uncertain_fact_count": self.metadata_store.count_uncertain_person_fact_claims(
|
|
537
|
+
str(profile.get("person_id", "") or requested_person_id)
|
|
538
|
+
),
|
|
539
|
+
"raw_profile": profile,
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
async def _profile_evidence_admin(
|
|
543
|
+
self,
|
|
544
|
+
*,
|
|
545
|
+
person_id: str = "",
|
|
546
|
+
person_keyword: str = "",
|
|
547
|
+
limit: int = 12,
|
|
548
|
+
force_refresh: bool = False,
|
|
549
|
+
) -> Dict[str, Any]:
|
|
550
|
+
profile = await self._query_person_profile_with_feedback_refresh(
|
|
551
|
+
person_id=person_id,
|
|
552
|
+
person_keyword=person_keyword,
|
|
553
|
+
limit=max(1, int(limit or 12)),
|
|
554
|
+
force_refresh=force_refresh,
|
|
555
|
+
source_note="sdk_memory_kernel.memory_profile_admin.evidence",
|
|
556
|
+
)
|
|
557
|
+
requested_person_id = (
|
|
558
|
+
str(profile.get("person_id", "") or person_id or "").strip() if isinstance(profile, dict) else person_id
|
|
559
|
+
)
|
|
560
|
+
return self._profile_evidence_response(
|
|
561
|
+
profile if isinstance(profile, dict) else {}, requested_person_id=requested_person_id, limit=limit
|
|
562
|
+
)
|
|
563
|
+
|
|
564
|
+
async def _profile_correct_evidence_admin(
|
|
565
|
+
self,
|
|
566
|
+
*,
|
|
567
|
+
person_id: str = "",
|
|
568
|
+
person_keyword: str = "",
|
|
569
|
+
evidence_type: str,
|
|
570
|
+
hash_value: str,
|
|
571
|
+
requested_by: str = "webui",
|
|
572
|
+
reason: str = "profile_evidence_correction",
|
|
573
|
+
refresh: bool = True,
|
|
574
|
+
limit: int = 12,
|
|
575
|
+
) -> Dict[str, Any]:
|
|
576
|
+
normalized_type = str(evidence_type or "").strip().lower()
|
|
577
|
+
normalized_hash = str(hash_value or "").strip()
|
|
578
|
+
if normalized_type not in {"relation", "paragraph", "person_fact", "chat_summary"}:
|
|
579
|
+
return {"success": False, "error": "不支持的画像证据类型"}
|
|
580
|
+
if not normalized_hash:
|
|
581
|
+
return {"success": False, "error": "画像证据 hash 不能为空"}
|
|
582
|
+
|
|
583
|
+
evidence_payload = await self._profile_evidence_admin(
|
|
584
|
+
person_id=person_id,
|
|
585
|
+
person_keyword=person_keyword,
|
|
586
|
+
limit=max(50, int(limit or 12)),
|
|
587
|
+
force_refresh=False,
|
|
588
|
+
)
|
|
589
|
+
if not bool(evidence_payload.get("success")):
|
|
590
|
+
return evidence_payload
|
|
591
|
+
matched = None
|
|
592
|
+
for item in evidence_payload.get("evidence") or []:
|
|
593
|
+
if not isinstance(item, dict):
|
|
594
|
+
continue
|
|
595
|
+
if str(item.get("hash", "") or "").strip() != normalized_hash:
|
|
596
|
+
continue
|
|
597
|
+
item_type = str(item.get("evidence_type", "") or "").strip().lower()
|
|
598
|
+
if normalized_type == item_type or (
|
|
599
|
+
normalized_type == "paragraph" and item_type in {"person_fact", "chat_summary"}
|
|
600
|
+
):
|
|
601
|
+
matched = item
|
|
602
|
+
break
|
|
603
|
+
if matched is None:
|
|
604
|
+
return {"success": False, "error": "当前画像证据中未找到目标 hash"}
|
|
605
|
+
if not bool(matched.get("deletable", False)):
|
|
606
|
+
return {
|
|
607
|
+
"success": False,
|
|
608
|
+
"error": str(matched.get("not_deletable_reason", "") or "该画像证据不可纠错"),
|
|
609
|
+
"evidence": matched,
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
delete_mode = "relation" if normalized_type == "relation" else "paragraph"
|
|
613
|
+
delete_result = await self._execute_delete_action(
|
|
614
|
+
mode=delete_mode,
|
|
615
|
+
selector={"hashes": [normalized_hash]},
|
|
616
|
+
requested_by=requested_by or "webui",
|
|
617
|
+
reason=reason or "profile_evidence_correction",
|
|
618
|
+
)
|
|
619
|
+
if bool(delete_result.get("success")):
|
|
620
|
+
await self._invalidate_import_manifest_for_sources(delete_result)
|
|
621
|
+
|
|
622
|
+
refreshed_profile: Dict[str, Any] = {}
|
|
623
|
+
refreshed_evidence: Dict[str, Any] = {}
|
|
624
|
+
if refresh and bool(delete_result.get("success")):
|
|
625
|
+
refreshed_profile = await self.person_profile_service.query_person_profile(
|
|
626
|
+
person_id=str(evidence_payload.get("person_id", "") or person_id),
|
|
627
|
+
top_k=max(4, int(limit or 12)),
|
|
628
|
+
force_refresh=True,
|
|
629
|
+
source_note="sdk_memory_kernel.memory_profile_admin.correct_evidence",
|
|
630
|
+
)
|
|
631
|
+
refreshed_evidence = self._profile_evidence_response(
|
|
632
|
+
refreshed_profile if isinstance(refreshed_profile, dict) else {},
|
|
633
|
+
requested_person_id=str(evidence_payload.get("person_id", "") or person_id),
|
|
634
|
+
limit=limit,
|
|
635
|
+
)
|
|
636
|
+
|
|
637
|
+
return {
|
|
638
|
+
"success": bool(delete_result.get("success")),
|
|
639
|
+
"person_id": str(evidence_payload.get("person_id", "") or person_id),
|
|
640
|
+
"evidence": matched,
|
|
641
|
+
"delete_result": delete_result,
|
|
642
|
+
"operation_id": str(delete_result.get("operation_id", "") or ""),
|
|
643
|
+
"refreshed_profile": refreshed_profile,
|
|
644
|
+
"refreshed_evidence": refreshed_evidence,
|
|
645
|
+
"error": str(delete_result.get("error", "") or ""),
|
|
646
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, Callable, Coroutine, Dict
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
|
|
7
|
+
from .base import KernelServiceBase
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class MemoryRequestDedupService(KernelServiceBase):
|
|
11
|
+
async def execute_request_with_dedup(
|
|
12
|
+
self,
|
|
13
|
+
request_key: str,
|
|
14
|
+
executor: Callable[[], Coroutine[Any, Any, Dict[str, Any]]],
|
|
15
|
+
) -> tuple[bool, Dict[str, Any]]:
|
|
16
|
+
token = str(request_key or "").strip()
|
|
17
|
+
if not token:
|
|
18
|
+
return False, await executor()
|
|
19
|
+
|
|
20
|
+
existing = self._request_dedup_tasks.get(token)
|
|
21
|
+
if existing is not None:
|
|
22
|
+
return True, await asyncio.shield(existing)
|
|
23
|
+
|
|
24
|
+
task = asyncio.create_task(executor())
|
|
25
|
+
self._request_dedup_tasks[token] = task
|
|
26
|
+
|
|
27
|
+
def _remove_completed_task(completed: asyncio.Task) -> None:
|
|
28
|
+
current = self._request_dedup_tasks.get(token)
|
|
29
|
+
if current is completed:
|
|
30
|
+
self._request_dedup_tasks.pop(token, None)
|
|
31
|
+
|
|
32
|
+
task.add_done_callback(_remove_completed_task)
|
|
33
|
+
payload = await asyncio.shield(task)
|
|
34
|
+
return False, payload
|