foxclaw 0.1.0 → 0.1.3
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.
- package/README.md +54 -29
- package/bin/foxclaw.cjs +51 -0
- package/index.js +4 -10
- package/package.json +46 -38
- package/python/.dockerignore +35 -0
- package/python/.env.example +173 -0
- package/python/.gitattributes +46 -0
- package/python/.github/workflows/ci.yml +167 -0
- package/python/.github/workflows/release.yml +183 -0
- package/python/CHANGELOG.md +57 -0
- package/python/CHANGELOG_CN.md +57 -0
- package/python/Dockerfile +64 -0
- package/python/LICENSE +21 -0
- package/python/MANIFEST.in +14 -0
- package/python/README.md +201 -0
- package/python/README_CN.md +336 -0
- package/python/bench/bm25_audit.py +186 -0
- package/python/bench/bm25_regression.py +177 -0
- package/python/bench/bm25_stress.py +155 -0
- package/python/bench/cd_audit.py +215 -0
- package/python/bench/chaos.py +140 -0
- package/python/bench/concurrency.py +103 -0
- package/python/bench/e2e_real_models.py +94 -0
- package/python/bench/llm_retry_test.py +241 -0
- package/python/bench/load_test.py +193 -0
- package/python/bench/vector_ivf_upgrade.py +128 -0
- package/python/docs/CHANNELS.md +195 -0
- package/python/docs/GOVERNANCE.md +164 -0
- package/python/docs/RELEASE.md +110 -0
- package/python/docs/RUN.md +75 -0
- package/python/docs/RUN_CN.md +121 -0
- package/python/docs//351/205/215/347/275/256/346/214/207/345/215/227.md +152 -0
- package/python/evals/__init__.py +0 -0
- package/python/evals/governance/README.md +61 -0
- package/python/evals/governance/__init__.py +0 -0
- package/python/evals/governance/cases.py +115 -0
- package/python/evals/governance/compare_embeddings.py +94 -0
- package/python/evals/governance/convergence.svg +11 -0
- package/python/evals/governance/report.json +371 -0
- package/python/evals/governance/run_evals.py +368 -0
- package/python/examples/governance_middleware_demo.py +72 -0
- package/python/examples/langgraph_governance_demo.py +104 -0
- package/python/examples/sample_itinerary.csv +6 -0
- package/python/examples/sample_knowledge.csv +9 -0
- package/python/examples/sample_transactions.csv +14 -0
- package/python/foxclaw/__init__.py +408 -0
- package/python/foxclaw/agents/__init__.py +12 -0
- package/python/foxclaw/agents/agents.py +35 -0
- package/python/foxclaw/agents/base.py +794 -0
- package/python/foxclaw/agents/impl/__init__.py +1 -0
- package/python/foxclaw/agents/impl/_helpers.py +70 -0
- package/python/foxclaw/agents/impl/finance.py +231 -0
- package/python/foxclaw/agents/impl/office.py +350 -0
- package/python/foxclaw/agents/impl/supervisor.py +592 -0
- package/python/foxclaw/agents/mixins/_context.py +128 -0
- package/python/foxclaw/agents/mixins/augment.py +143 -0
- package/python/foxclaw/agents/mixins/llm.py +67 -0
- package/python/foxclaw/agents/mixins/memory.py +41 -0
- package/python/foxclaw/agents/mixins/preference.py +131 -0
- package/python/foxclaw/agents/mixins/prompt.py +71 -0
- package/python/foxclaw/agents/mixins/skillbox.py +58 -0
- package/python/foxclaw/agents/mixins/tool_exec.py +417 -0
- package/python/foxclaw/api/__init__.py +224 -0
- package/python/foxclaw/api/_state.py +43 -0
- package/python/foxclaw/api/bootstrap.py +96 -0
- package/python/foxclaw/api/middleware.py +294 -0
- package/python/foxclaw/api/routes/ahs.py +404 -0
- package/python/foxclaw/api/routes/attachments.py +116 -0
- package/python/foxclaw/api/routes/browser.py +35 -0
- package/python/foxclaw/api/routes/domain.py +306 -0
- package/python/foxclaw/api/routes/knowledge.py +145 -0
- package/python/foxclaw/api/routes/llm_admin.py +449 -0
- package/python/foxclaw/api/routes/memory.py +105 -0
- package/python/foxclaw/api/routes/orchestration.py +39 -0
- package/python/foxclaw/api/routes/recovery.py +44 -0
- package/python/foxclaw/api/routes/review.py +266 -0
- package/python/foxclaw/api/routes/search.py +33 -0
- package/python/foxclaw/api/routes/system.py +311 -0
- package/python/foxclaw/api/routes/task.py +263 -0
- package/python/foxclaw/api/routes/ws.py +64 -0
- package/python/foxclaw/api/spine.py +52 -0
- package/python/foxclaw/browser/__init__.py +13 -0
- package/python/foxclaw/browser/action/__init__.py +1 -0
- package/python/foxclaw/browser/action/action_executor.py +50 -0
- package/python/foxclaw/browser/action/action_types.py +44 -0
- package/python/foxclaw/browser/action/action_verifier.py +68 -0
- package/python/foxclaw/browser/action/url_policy.py +143 -0
- package/python/foxclaw/browser/adapter/__init__.py +1 -0
- package/python/foxclaw/browser/adapter/base_adapter.py +23 -0
- package/python/foxclaw/browser/adapter/web_adapter.py +46 -0
- package/python/foxclaw/browser/driver/__init__.py +1 -0
- package/python/foxclaw/browser/driver/base_driver.py +61 -0
- package/python/foxclaw/browser/driver/playwright_driver.py +170 -0
- package/python/foxclaw/browser/perception/__init__.py +1 -0
- package/python/foxclaw/browser/perception/dom_extractor.py +42 -0
- package/python/foxclaw/browser/perception/element_indexer.py +56 -0
- package/python/foxclaw/browser/perception/semantic_reducer.py +33 -0
- package/python/foxclaw/browser/perception/snapshot.py +29 -0
- package/python/foxclaw/browser/service.py +144 -0
- package/python/foxclaw/browser/session/__init__.py +1 -0
- package/python/foxclaw/browser/session/session_manager.py +57 -0
- package/python/foxclaw/capability/__init__.py +12 -0
- package/python/foxclaw/capability/embedding_service.py +65 -0
- package/python/foxclaw/channels/__init__.py +138 -0
- package/python/foxclaw/channels/discord.py +175 -0
- package/python/foxclaw/channels/dispatcher.py +179 -0
- package/python/foxclaw/channels/feishu.py +215 -0
- package/python/foxclaw/channels/openclaw_bridge.py +151 -0
- package/python/foxclaw/channels/signing.py +187 -0
- package/python/foxclaw/channels/telegram.py +120 -0
- package/python/foxclaw/channels/weixin.py +215 -0
- package/python/foxclaw/cli.py +183 -0
- package/python/foxclaw/cognitive/__init__.py +4 -0
- package/python/foxclaw/cognitive/reasoner.py +81 -0
- package/python/foxclaw/cognitive/state.py +141 -0
- package/python/foxclaw/core/__init__.py +9 -0
- package/python/foxclaw/core/audit_chain.py +131 -0
- package/python/foxclaw/core/audit_sink.py +100 -0
- package/python/foxclaw/core/compaction.py +265 -0
- package/python/foxclaw/core/config.py +65 -0
- package/python/foxclaw/core/confirm_nonce.py +151 -0
- package/python/foxclaw/core/dispatch.py +27 -0
- package/python/foxclaw/core/envfile.py +48 -0
- package/python/foxclaw/core/failure_policy.py +142 -0
- package/python/foxclaw/core/gateway.py +81 -0
- package/python/foxclaw/core/governance_audit.py +171 -0
- package/python/foxclaw/core/idempotency.py +282 -0
- package/python/foxclaw/core/injection_guard.py +202 -0
- package/python/foxclaw/core/memory_adapter.py +489 -0
- package/python/foxclaw/core/metrics.py +88 -0
- package/python/foxclaw/core/model_router.py +985 -0
- package/python/foxclaw/core/paths.py +103 -0
- package/python/foxclaw/core/recovery.py +349 -0
- package/python/foxclaw/core/scheduler.py +141 -0
- package/python/foxclaw/core/secrets.py +131 -0
- package/python/foxclaw/core/sessions.py +155 -0
- package/python/foxclaw/core/settings.py +414 -0
- package/python/foxclaw/core/shared_write.py +186 -0
- package/python/foxclaw/core/steering_queue.py +154 -0
- package/python/foxclaw/core/system_bus.py +84 -0
- package/python/foxclaw/core/thought_chain.py +316 -0
- package/python/foxclaw/core/token_meter.py +231 -0
- package/python/foxclaw/core/tools.py +164 -0
- package/python/foxclaw/core/tracing.py +375 -0
- package/python/foxclaw/core/truth_pipeline.py +110 -0
- package/python/foxclaw/core/update_check.py +149 -0
- package/python/foxclaw/core/vec_recall_adapter.py +477 -0
- package/python/foxclaw/core/verify.py +173 -0
- package/python/foxclaw/core/web_search.py +218 -0
- package/python/foxclaw/finance/__init__.py +15 -0
- package/python/foxclaw/finance/evidence.py +149 -0
- package/python/foxclaw/finance/fx.py +98 -0
- package/python/foxclaw/finance/graph.py +179 -0
- package/python/foxclaw/finance/models.py +146 -0
- package/python/foxclaw/finance/nlparse.py +515 -0
- package/python/foxclaw/finance/policy.py +301 -0
- package/python/foxclaw/finance/reasoner.py +121 -0
- package/python/foxclaw/finance/reasoning.py +319 -0
- package/python/foxclaw/finance/reconcile.py +175 -0
- package/python/foxclaw/finance/runtime.py +500 -0
- package/python/foxclaw/governance/__init__.py +24 -0
- package/python/foxclaw/governance/middleware.py +176 -0
- package/python/foxclaw/harness/__init__.py +17 -0
- package/python/foxclaw/harness/_base.py +66 -0
- package/python/foxclaw/harness/_policy.py +340 -0
- package/python/foxclaw/harness/drift.py +77 -0
- package/python/foxclaw/harness/event_bus.py +28 -0
- package/python/foxclaw/harness/harness.py +358 -0
- package/python/foxclaw/harness/loop_detection.py +145 -0
- package/python/foxclaw/itinerary/__init__.py +10 -0
- package/python/foxclaw/itinerary/amap_client.py +114 -0
- package/python/foxclaw/itinerary/models.py +80 -0
- package/python/foxclaw/itinerary/reasoner.py +96 -0
- package/python/foxclaw/itinerary/reasoning.py +168 -0
- package/python/foxclaw/itinerary/runtime.py +144 -0
- package/python/foxclaw/kernel/__init__.py +13 -0
- package/python/foxclaw/kernel/capabilities.py +72 -0
- package/python/foxclaw/kernel/policy_gate.py +108 -0
- package/python/foxclaw/kernel/process_manager.py +186 -0
- package/python/foxclaw/kernel/scheduler.py +125 -0
- package/python/foxclaw/kernel/sentinel.py +443 -0
- package/python/foxclaw/kernel/sentinel_client.py +243 -0
- package/python/foxclaw/kernel/spine.py +97 -0
- package/python/foxclaw/knowledge/__init__.py +360 -0
- package/python/foxclaw/knowledge/truth_crystallizer.py +89 -0
- package/python/foxclaw/learning/__init__.py +12 -0
- package/python/foxclaw/learning/knowledge_graph.py +144 -0
- package/python/foxclaw/learning/models.py +93 -0
- package/python/foxclaw/learning/reasoner.py +67 -0
- package/python/foxclaw/learning/runtime.py +351 -0
- package/python/foxclaw/learning/spaced_repetition.py +91 -0
- package/python/foxclaw/mcp_server.py +188 -0
- package/python/foxclaw/orchestration/__init__.py +167 -0
- package/python/foxclaw/pipeline/__init__.py +23 -0
- package/python/foxclaw/pipeline/cognitive_pipeline.py +429 -0
- package/python/foxclaw/pipeline/stage_meta.py +21 -0
- package/python/foxclaw/pipeline/supervisor_stages.py +201 -0
- package/python/foxclaw/planning/__init__.py +17 -0
- package/python/foxclaw/planning/llm_planner.py +232 -0
- package/python/foxclaw/planning/planner.py +94 -0
- package/python/foxclaw/search/__init__.py +24 -0
- package/python/foxclaw/search/agent_port.py +190 -0
- package/python/foxclaw/search/evidence.py +109 -0
- package/python/foxclaw/search/external_search.py +50 -0
- package/python/foxclaw/search/hkc_read_port.py +60 -0
- package/python/foxclaw/search/internal_search.py +71 -0
- package/python/foxclaw/search/models.py +127 -0
- package/python/foxclaw/search/planner.py +44 -0
- package/python/foxclaw/search/retriever.py +39 -0
- package/python/foxclaw/search/runtime.py +127 -0
- package/python/foxclaw/search/strategy.py +44 -0
- package/python/foxclaw/skills/__init__.py +559 -0
- package/python/foxclaw/skills/archive.py +111 -0
- package/python/foxclaw/skills/fetch.py +226 -0
- package/python/foxclaw/skills/hooks_spec.py +69 -0
- package/python/foxclaw/skills/install_cmd.py +216 -0
- package/python/foxclaw/skills/lifecycle.py +252 -0
- package/python/foxclaw/skills/markdown_skill.py +175 -0
- package/python/foxclaw/skills/markdown_store.py +39 -0
- package/python/foxclaw/skills/mcp_client.py +184 -0
- package/python/foxclaw/skills/mcp_store.py +40 -0
- package/python/foxclaw/skills/permission.py +145 -0
- package/python/foxclaw/skills/pipeline.py +354 -0
- package/python/foxclaw/skills/policy.py +74 -0
- package/python/foxclaw/skills/review.py +72 -0
- package/python/foxclaw/skills/store.py +57 -0
- package/python/frontend/assets/core_bg.jpg +0 -0
- package/python/frontend/deck/auth.js +25 -0
- package/python/frontend/deck/chat_model_picker.js +483 -0
- package/python/frontend/deck/domain.js +276 -0
- package/python/frontend/deck/main.js +1806 -0
- package/python/frontend/deck/markdown.js +44 -0
- package/python/frontend/deck/settings.js +307 -0
- package/python/frontend/deck/state.js +5 -0
- package/python/frontend/deck/util.js +4 -0
- package/python/frontend/deck/visuals.js +190 -0
- package/python/frontend/deck.css +855 -0
- package/python/frontend/deck.html +686 -0
- package/python/frontend/deck_v2.html +341 -0
- package/python/frontend/planet/index.html +266 -0
- package/python/frontend/planet/planet.css +579 -0
- package/python/frontend/planet/planet.js +1436 -0
- package/python/frontend/planet/vendor/three.core.min.js +6 -0
- package/python/frontend/planet/vendor/three.module.min.js +6 -0
- package/python/frontend/v3/assets/index-CQSBdxk4.js +34 -0
- package/python/frontend/v3/assets/index-D5nIsjms.css +1 -0
- package/python/frontend/v3/index.html +15 -0
- package/python/frontend/widget_runtime.js +252 -0
- package/python/frontend/widgets.js +639 -0
- package/python/frontend_src/index.html +14 -0
- package/python/frontend_src/package-lock.json +1376 -0
- package/python/frontend_src/package.json +19 -0
- package/python/frontend_src/src/App.vue +58 -0
- package/python/frontend_src/src/api.js +82 -0
- package/python/frontend_src/src/components/ChatDock.vue +365 -0
- package/python/frontend_src/src/components/ComposerMenu.vue +75 -0
- package/python/frontend_src/src/components/DocViewer.vue +162 -0
- package/python/frontend_src/src/components/Inspector.vue +235 -0
- package/python/frontend_src/src/components/LeftRail.vue +415 -0
- package/python/frontend_src/src/components/MenuPopover.vue +128 -0
- package/python/frontend_src/src/components/MessageStream.vue +389 -0
- package/python/frontend_src/src/components/ModelPicker.vue +350 -0
- package/python/frontend_src/src/components/QuickIngest.vue +112 -0
- package/python/frontend_src/src/components/Stage.vue +272 -0
- package/python/frontend_src/src/components/ToolMenu.vue +141 -0
- package/python/frontend_src/src/components/TopBar.vue +118 -0
- package/python/frontend_src/src/components/panels/ActivityPanel.vue +82 -0
- package/python/frontend_src/src/components/panels/AgentsPanel.vue +83 -0
- package/python/frontend_src/src/components/panels/KnowledgePanel.vue +425 -0
- package/python/frontend_src/src/components/panels/MemoryPanel.vue +117 -0
- package/python/frontend_src/src/components/panels/PanelBox.vue +50 -0
- package/python/frontend_src/src/components/panels/ReviewPanel.vue +292 -0
- package/python/frontend_src/src/components/panels/SettingsPanel.vue +358 -0
- package/python/frontend_src/src/components/panels/TasksPanel.vue +73 -0
- package/python/frontend_src/src/components/panels/ToolsPanel.vue +746 -0
- package/python/frontend_src/src/components/panels/UsagePanel.vue +105 -0
- package/python/frontend_src/src/lib/fmt.js +44 -0
- package/python/frontend_src/src/lib/i18n.js +53 -0
- package/python/frontend_src/src/lib/locales/en.js +173 -0
- package/python/frontend_src/src/lib/locales/zh.js +174 -0
- package/python/frontend_src/src/lib/md.js +190 -0
- package/python/frontend_src/src/main.js +9 -0
- package/python/frontend_src/src/stores/app.js +91 -0
- package/python/frontend_src/src/stores/chat.js +450 -0
- package/python/frontend_src/src/stores/model.js +96 -0
- package/python/frontend_src/src/stores/panels.js +289 -0
- package/python/frontend_src/src/stores/profile.js +31 -0
- package/python/frontend_src/src/stores/system.js +25 -0
- package/python/frontend_src/src/styles/base.css +138 -0
- package/python/frontend_src/src/styles/panel.css +117 -0
- package/python/frontend_src/src/styles/tokens.css +78 -0
- package/python/frontend_src/vite.config.js +39 -0
- package/python/pyproject.toml +268 -0
- package/python/requirements.txt +311 -0
- package/python/skills/README.md +123 -0
- package/python/skills/execution-contract/SKILL.md +30 -0
- package/python/skills/general-assistant/SKILL.md +34 -0
- package/python/skills/planner/SKILL.md +33 -0
- package/python/skills/schedule-itinerary/SKILL.md +35 -0
- package/python/skills/skill-composition/SKILL.md +31 -0
- package/python/skills/skill-guard/SKILL.md +67 -0
- package/python/skills/skill-memory/SKILL.md +50 -0
- package/python/skills/skill-router/SKILL.md +35 -0
- package/python/tests/conftest.py +54 -0
- package/python/tests/run_all.py +200 -0
- package/python/tests/test_agent_lifecycle.py +326 -0
- package/python/tests/test_ahs_p2.py +393 -0
- package/python/tests/test_anthropic_tools.py +192 -0
- package/python/tests/test_api.py +97 -0
- package/python/tests/test_api_auth.py +79 -0
- package/python/tests/test_api_host_guard.py +69 -0
- package/python/tests/test_attachments.py +151 -0
- package/python/tests/test_audit_chain.py +116 -0
- package/python/tests/test_audit_sink.py +130 -0
- package/python/tests/test_browser.py +149 -0
- package/python/tests/test_browser_request_guard.py +180 -0
- package/python/tests/test_capabilities.py +152 -0
- package/python/tests/test_channel_signing.py +185 -0
- package/python/tests/test_channel_trust.py +109 -0
- package/python/tests/test_channels.py +174 -0
- package/python/tests/test_cli.py +46 -0
- package/python/tests/test_compaction_toolpair.py +119 -0
- package/python/tests/test_config_layering.py +61 -0
- package/python/tests/test_confirm_nonce.py +186 -0
- package/python/tests/test_context_compaction_wiring.py +124 -0
- package/python/tests/test_dispatch.py +89 -0
- package/python/tests/test_domain_evals.py +334 -0
- package/python/tests/test_domain_import.py +72 -0
- package/python/tests/test_domain_reasoners.py +134 -0
- package/python/tests/test_e2e.py +161 -0
- package/python/tests/test_emb_cache.py +97 -0
- package/python/tests/test_embedding_singleton.py +87 -0
- package/python/tests/test_envfile.py +83 -0
- package/python/tests/test_evals_smoke.py +90 -0
- package/python/tests/test_event_spine.py +174 -0
- package/python/tests/test_evidence_semantic.py +88 -0
- package/python/tests/test_evidence_sse.py +94 -0
- package/python/tests/test_file_ingest.py +135 -0
- package/python/tests/test_finance.py +144 -0
- package/python/tests/test_finance_ledger.py +245 -0
- package/python/tests/test_finance_persist.py +174 -0
- package/python/tests/test_gateway.py +125 -0
- package/python/tests/test_governance.py +421 -0
- package/python/tests/test_governance_audit.py +324 -0
- package/python/tests/test_governance_middleware.py +137 -0
- package/python/tests/test_hkc.py +268 -0
- package/python/tests/test_hkc_index_build_manager.py +306 -0
- package/python/tests/test_hkc_manifest_reuse.py +294 -0
- package/python/tests/test_hkc_search_event_sync.py +482 -0
- package/python/tests/test_import_path_guard.py +155 -0
- package/python/tests/test_injection_guard.py +193 -0
- package/python/tests/test_integration.py +117 -0
- package/python/tests/test_itinerary.py +100 -0
- package/python/tests/test_kee_semantic.py +84 -0
- package/python/tests/test_kernel.py +212 -0
- package/python/tests/test_learning.py +101 -0
- package/python/tests/test_llm_admin.py +145 -0
- package/python/tests/test_llm_planner.py +344 -0
- package/python/tests/test_markdown_skills.py +62 -0
- package/python/tests/test_markdown_skills_openclaw.py +66 -0
- package/python/tests/test_mcp_connectors.py +97 -0
- package/python/tests/test_mcp_guardian_discovery.py +87 -0
- package/python/tests/test_mcp_no_memory.py +43 -0
- package/python/tests/test_mcp_policy_gate.py +76 -0
- package/python/tests/test_mcp_server.py +92 -0
- package/python/tests/test_memory_recall.py +92 -0
- package/python/tests/test_metrics.py +58 -0
- package/python/tests/test_multi_chain.py +90 -0
- package/python/tests/test_multimodal.py +122 -0
- package/python/tests/test_negotiation.py +136 -0
- package/python/tests/test_orchestration.py +109 -0
- package/python/tests/test_orchestration_decoupled.py +103 -0
- package/python/tests/test_p3_p5_hardening.py +324 -0
- package/python/tests/test_parallel_dispatch.py +207 -0
- package/python/tests/test_pipeline.py +492 -0
- package/python/tests/test_plan_visible.py +86 -0
- package/python/tests/test_planner.py +88 -0
- package/python/tests/test_policy_gate.py +108 -0
- package/python/tests/test_profile.py +67 -0
- package/python/tests/test_readme_claims.py +163 -0
- package/python/tests/test_reasoner.py +170 -0
- package/python/tests/test_release_check.py +252 -0
- package/python/tests/test_resume_checkpoint.py +148 -0
- package/python/tests/test_routing.py +85 -0
- package/python/tests/test_scheduler.py +138 -0
- package/python/tests/test_search.py +194 -0
- package/python/tests/test_secrets.py +153 -0
- package/python/tests/test_semantic.py +69 -0
- package/python/tests/test_sessions.py +90 -0
- package/python/tests/test_settings.py +265 -0
- package/python/tests/test_shadow_learning.py +290 -0
- package/python/tests/test_shared_write.py +200 -0
- package/python/tests/test_skill_taint_gate.py +145 -0
- package/python/tests/test_skills.py +117 -0
- package/python/tests/test_skills_fetch.py +282 -0
- package/python/tests/test_stats_and_events.py +162 -0
- package/python/tests/test_streaming.py +55 -0
- package/python/tests/test_supervisor_pipeline.py +176 -0
- package/python/tests/test_supervisor_triage.py +102 -0
- package/python/tests/test_taint.py +112 -0
- package/python/tests/test_task_stream.py +74 -0
- package/python/tests/test_thinking_sse.py +66 -0
- package/python/tests/test_thinking_stream.py +76 -0
- package/python/tests/test_tool_center.py +113 -0
- package/python/tests/test_tool_parallel.py +113 -0
- package/python/tests/test_tool_params.py +99 -0
- package/python/tests/test_tool_use.py +229 -0
- package/python/tests/test_toplevel_concurrency.py +116 -0
- package/python/tests/test_tracing.py +256 -0
- package/python/tests/test_truth_crystallizer.py +125 -0
- package/python/tests/test_truth_flow.py +88 -0
- package/python/tests/test_update_check.py +95 -0
- package/python/tests/test_url_policy.py +53 -0
- package/python/tests/test_verify_evidence.py +212 -0
- package/python/tests/test_version_sync.py +100 -0
- package/python/tests/test_web_trigger.py +82 -0
- package/python/tools/check_faiss_type.py +30 -0
- package/python/tools/check_log.ps1 +38 -0
- package/python/tools/ivf_status.py +72 -0
- package/python/tools/mypy_baseline.txt +13 -0
- package/python/tools/mypy_ratchet.py +143 -0
- package/python/tools/pack.py +173 -0
- package/python/tools/release_check.py +349 -0
- package/python/tools/restart_foxclaw.ps1 +71 -0
- package/python/tools/shared_write_audit.py +190 -0
- package/python/tools/size_baseline.txt +28 -0
- package/python/tools/size_check.py +102 -0
- package/python/tools/strip_comments.py +228 -0
- package/python/tools/verify_env.bat +2 -0
- package/python/tools/verify_env.py +25 -0
- package/python/vendor/HMR_LICENSE +37 -0
- package/python/vendor/HMR_README.md +65 -0
- package/python/vendor/hkc/LICENSE +37 -0
- package/python/vendor/hkc/hkc_ace/__init__.py +0 -0
- package/python/vendor/hkc/hkc_ace/ace.py +457 -0
- package/python/vendor/hkc/hkc_ace/skill_taxonomy.json +465 -0
- package/python/vendor/hkc/hkc_api/__init__.py +0 -0
- package/python/vendor/hkc/hkc_api/container.py +416 -0
- package/python/vendor/hkc/hkc_api/ingest_jobs.py +103 -0
- package/python/vendor/hkc/hkc_api/main.py +157 -0
- package/python/vendor/hkc/hkc_api/routes/__init__.py +0 -0
- package/python/vendor/hkc/hkc_api/routes/ability.py +151 -0
- package/python/vendor/hkc/hkc_api/routes/config.py +51 -0
- package/python/vendor/hkc/hkc_api/routes/knowledge.py +630 -0
- package/python/vendor/hkc/hkc_api/routes/search.py +88 -0
- package/python/vendor/hkc/hkc_api/schemas.py +153 -0
- package/python/vendor/hkc/hkc_api/synthesis.py +179 -0
- package/python/vendor/hkc/hkc_core/__init__.py +0 -0
- package/python/vendor/hkc/hkc_core/graph/__init__.py +0 -0
- package/python/vendor/hkc/hkc_core/graph/base.py +188 -0
- package/python/vendor/hkc/hkc_core/graph/sqlite_store.py +789 -0
- package/python/vendor/hkc/hkc_core/models/__init__.py +0 -0
- package/python/vendor/hkc/hkc_core/models/enums.py +65 -0
- package/python/vendor/hkc/hkc_core/models/ku.py +307 -0
- package/python/vendor/hkc/hkc_core/utils/__init__.py +0 -0
- package/python/vendor/hkc/hkc_core/utils/id_gen.py +113 -0
- package/python/vendor/hkc/hkc_crystallizer/__init__.py +44 -0
- package/python/vendor/hkc/hkc_crystallizer/adapters.py +94 -0
- package/python/vendor/hkc/hkc_crystallizer/candidate.py +145 -0
- package/python/vendor/hkc/hkc_crystallizer/crystallizer.py +179 -0
- package/python/vendor/hkc/hkc_crystallizer/ingress.py +90 -0
- package/python/vendor/hkc/hkc_kde/__init__.py +0 -0
- package/python/vendor/hkc/hkc_kde/chunker.py +197 -0
- package/python/vendor/hkc/hkc_kde/extractor.py +623 -0
- package/python/vendor/hkc/hkc_kde/kde.py +298 -0
- package/python/vendor/hkc/hkc_kde/loaders/__init__.py +0 -0
- package/python/vendor/hkc/hkc_kde/loaders/ebook_loader.py +153 -0
- package/python/vendor/hkc/hkc_kde/loaders/office_loader.py +194 -0
- package/python/vendor/hkc/hkc_kde/loaders/pdf_loader.py +162 -0
- package/python/vendor/hkc/hkc_kde/loaders/text_loader.py +157 -0
- package/python/vendor/hkc/hkc_kde/loaders/web_loader.py +91 -0
- package/python/vendor/hkc/hkc_kde/models.py +74 -0
- package/python/vendor/hkc/hkc_kde/packager.py +329 -0
- package/python/vendor/hkc/hkc_kde/relation_disc.py +151 -0
- package/python/vendor/hkc/hkc_kee/__init__.py +0 -0
- package/python/vendor/hkc/hkc_kee/dedup.py +227 -0
- package/python/vendor/hkc/hkc_kee/fingerprint.py +106 -0
- package/python/vendor/hkc/hkc_kee/kee.py +541 -0
- package/python/vendor/hkc/hkc_kep/__init__.py +0 -0
- package/python/vendor/hkc/hkc_kep/event_bus.py +205 -0
- package/python/vendor/hkc/hkc_sdk/__init__.py +63 -0
- package/python/vendor/hkc/hkc_sdk/base.py +151 -0
- package/python/vendor/hkc/hkc_sdk/direct_client.py +153 -0
- package/python/vendor/hkc/hkc_sdk/exceptions.py +39 -0
- package/python/vendor/hkc/hkc_sdk/http_client.py +166 -0
- package/python/vendor/hkc/hkc_sdk/models.py +171 -0
- package/python/vendor/hkc/hkc_search/__init__.py +0 -0
- package/python/vendor/hkc/hkc_search/bm25.py +325 -0
- package/python/vendor/hkc/hkc_search/embedding_backends.py +374 -0
- package/python/vendor/hkc/hkc_search/graph_search.py +132 -0
- package/python/vendor/hkc/hkc_search/hybrid.py +331 -0
- package/python/vendor/hkc/hkc_search/index_build_manager.py +378 -0
- package/python/vendor/hkc/hkc_search/vector_search.py +484 -0
- package/python/vendor/hmr/__init__.py +0 -0
- package/python/vendor/hmr/core/__init__.py +0 -0
- package/python/vendor/hmr/core/hmr.py +684 -0
- package/python/vendor/hmr/core/models.py +310 -0
- package/python/vendor/hmr/engines/__init__.py +0 -0
- package/python/vendor/hmr/engines/evolution.py +710 -0
- package/python/vendor/hmr/engines/jit_compiler.py +472 -0
- package/python/vendor/hmr/engines/lifecycle.py +264 -0
- package/python/vendor/hmr/engines/policy.py +625 -0
- package/python/vendor/hmr/engines/recall.py +260 -0
- package/python/vendor/hmr/engines/runtime_state.py +158 -0
- package/python/vendor/hmr/engines/scheduler.py +323 -0
- package/python/vendor/hmr/engines/semantic.py +150 -0
- package/python/vendor/hmr/engines/temporal.py +261 -0
- package/python/vendor/hmr/engines/thought_chain.py +635 -0
- package/python/vendor/hmr/graph/__init__.py +0 -0
- package/python/vendor/hmr/graph/cwg.py +338 -0
- package/python/vendor/hmr/graph/memory_graph.py +530 -0
- package/python/vendor/hmr/storage/__init__.py +0 -0
- package/python/vendor/hmr/storage/memory_fs.py +300 -0
- package/python/vendor/hmr/storage/vector_store.py +520 -0
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
"""
|
|
2
|
+
hkc-kde / extractor.py
|
|
3
|
+
知识提取器:把 Chunk 文本交给 LLM,提取结构化 KU。
|
|
4
|
+
|
|
5
|
+
核心规则:
|
|
6
|
+
- Fact :可被客观验证,confidence 固定 1.0
|
|
7
|
+
- Claim :依赖理论或存在对立观点,给出 0.0-1.0 置信度
|
|
8
|
+
- Entity:真实存在的人 / 机构 / 产品
|
|
9
|
+
- Concept:抽象知识单元
|
|
10
|
+
|
|
11
|
+
v1:调用 Anthropic API(claude-sonnet-4-6)
|
|
12
|
+
v2:可替换为本地模型,接口不变
|
|
13
|
+
"""
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import json
|
|
17
|
+
import logging
|
|
18
|
+
import os
|
|
19
|
+
import re
|
|
20
|
+
import time
|
|
21
|
+
from typing import Optional
|
|
22
|
+
|
|
23
|
+
from .chunker import Chunk
|
|
24
|
+
from .models import RawExtraction, ExtractionResult
|
|
25
|
+
|
|
26
|
+
logger = logging.getLogger(__name__)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
# ── System Prompt ─────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
_SYSTEM_PROMPT = """\
|
|
32
|
+
你是知识提取专家。从用户提供的文本中提取结构化知识。
|
|
33
|
+
|
|
34
|
+
## 严格规则
|
|
35
|
+
|
|
36
|
+
**Fact vs Claim 必须区分:**
|
|
37
|
+
- Fact:可被客观验证、不随时间/理论变化的陈述(人物生卒年、历史事件、测量值)
|
|
38
|
+
→ confidence 固定 1.0,不需要估算
|
|
39
|
+
- Claim:依赖理论框架、存在对立观点、或需要解释的观点/结论
|
|
40
|
+
→ 给出 0.0-1.0 的置信度,0.5 表示不确定
|
|
41
|
+
|
|
42
|
+
**输出格式:严格 JSON,不要任何解释文字、不要 Markdown 代码块**
|
|
43
|
+
|
|
44
|
+
## 输出 Schema
|
|
45
|
+
|
|
46
|
+
{
|
|
47
|
+
"facts": [
|
|
48
|
+
{"statement": "陈述原文", "source_hint": "来源线索(可选)"}
|
|
49
|
+
],
|
|
50
|
+
"claims": [
|
|
51
|
+
{"statement": "观点陈述", "confidence": 0.0-1.0, "domain": "领域"}
|
|
52
|
+
],
|
|
53
|
+
"entities": [
|
|
54
|
+
{"name": "实体名", "type": "Person|Organization|Product|Event|Place", "aliases": []}
|
|
55
|
+
],
|
|
56
|
+
"concepts": [
|
|
57
|
+
{"name": "概念名", "domain": "领域", "definition": "简短定义"}
|
|
58
|
+
]
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
## 判断示例
|
|
62
|
+
|
|
63
|
+
Fact(✓): "巴菲特生于1930年" / "GDP增长率为3.2%" / "该公司成立于1994年"
|
|
64
|
+
Claim(✓): "价值投资长期跑赢市场" / "高脂饮食有益代谢健康" / "这项技术将颠覆行业"
|
|
65
|
+
|
|
66
|
+
每类最多提取 5 个最重要的条目。如果文本中没有某类知识,返回空列表。\
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
# 批量抽取提示词(opt-in,HKC_EXTRACT_BATCH>1 时用):一次调用处理多段,
|
|
71
|
+
# 省掉逐段模式里每段重复一遍系统提示词的 token 开销。
|
|
72
|
+
_BATCH_SYSTEM_PROMPT = """\
|
|
73
|
+
你是知识提取专家。用户会提供 N 段文本,每段用 <chunk id="0">...</chunk> 这样的标签包裹,id 从 0 递增到 N-1。
|
|
74
|
+
对**每一段分别**提取结构化知识,输出一个 JSON 对象:顶层 key 是段的 id(字符串),value 是该段的知识。
|
|
75
|
+
|
|
76
|
+
{
|
|
77
|
+
"0": { "facts": [...], "claims": [...], "entities": [...], "concepts": [...] },
|
|
78
|
+
"1": { ... }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
每段独立提取、互不混淆。每类最多提取 5 个最重要的条目;某段没有某类知识就返回空列表。
|
|
82
|
+
|
|
83
|
+
## 严格规则
|
|
84
|
+
- Fact vs Claim 必须区分:Fact 可客观验证(confidence 固定 1.0);Claim 依赖理论/有对立观点(给 0.0-1.0 置信度)。
|
|
85
|
+
- 每个 value 的 Schema:
|
|
86
|
+
facts: [{"statement": "陈述原文", "source_hint": "来源线索(可选)"}]
|
|
87
|
+
claims: [{"statement": "观点陈述", "confidence": 0.0-1.0, "domain": "领域"}]
|
|
88
|
+
entities: [{"name": "实体名", "type": "Person|Organization|Product|Event|Place", "aliases": []}]
|
|
89
|
+
concepts: [{"name": "概念名", "domain": "领域", "definition": "简短定义"}]
|
|
90
|
+
|
|
91
|
+
严格只输出 JSON,不要任何解释文字、不要 Markdown 代码块。\
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class Extractor:
|
|
96
|
+
|
|
97
|
+
def __init__(self, api_key: Optional[str] = None, model: Optional[str] = None,
|
|
98
|
+
provider: str = "anthropic", base_url: Optional[str] = None):
|
|
99
|
+
"""
|
|
100
|
+
provider: 'anthropic'(默认) | 'deepseek' | 'openai' | 'openai-compatible'
|
|
101
|
+
- anthropic: 用 anthropic SDK,model 默认 claude-sonnet-4-6
|
|
102
|
+
- deepseek : 用 openai SDK(兼容),base_url=https://api.deepseek.com,model 默认 deepseek-chat
|
|
103
|
+
- openai / openai-compatible: 用 openai SDK,可自定义 base_url
|
|
104
|
+
"""
|
|
105
|
+
self._apply_config(provider, api_key, model, base_url)
|
|
106
|
+
self._client = None
|
|
107
|
+
# 最后一次抽取异常:全段失败时由 _run_pipeline 抛出,供上层显示真实原因
|
|
108
|
+
self._last_error = None
|
|
109
|
+
|
|
110
|
+
def _apply_config(self, provider, api_key, model, base_url):
|
|
111
|
+
"""应用 LLM 配置(init 和运行时 reconfigure 共用)。"""
|
|
112
|
+
self.provider = (provider or "anthropic").lower()
|
|
113
|
+
self.api_key = api_key
|
|
114
|
+
self.base_url = base_url
|
|
115
|
+
# 各 provider 的默认 model
|
|
116
|
+
if model:
|
|
117
|
+
self.model = model
|
|
118
|
+
elif self.provider == "deepseek":
|
|
119
|
+
self.model = "deepseek-chat"
|
|
120
|
+
elif self.provider in ("openai", "openai-compatible"):
|
|
121
|
+
self.model = "gpt-4o-mini"
|
|
122
|
+
else:
|
|
123
|
+
self.model = "claude-sonnet-4-6"
|
|
124
|
+
# deepseek 的默认 base_url
|
|
125
|
+
if self.provider == "deepseek" and not self.base_url:
|
|
126
|
+
self.base_url = "https://api.deepseek.com"
|
|
127
|
+
|
|
128
|
+
def reconfigure(self, provider=None, api_key=None, model=None, base_url=None):
|
|
129
|
+
"""运行时重设 LLM 配置(界面配置用),清掉缓存的 client。"""
|
|
130
|
+
self._apply_config(provider, api_key, model, base_url)
|
|
131
|
+
self._client = None
|
|
132
|
+
|
|
133
|
+
def config_summary(self) -> dict:
|
|
134
|
+
"""返回当前配置(api_key 脱敏),供界面展示。"""
|
|
135
|
+
k = self.api_key or ""
|
|
136
|
+
masked = (k[:6] + "…" + k[-4:]) if len(k) > 12 else ("已设置" if k else "")
|
|
137
|
+
return {
|
|
138
|
+
"provider": self.provider,
|
|
139
|
+
"model": self.model,
|
|
140
|
+
"base_url": self.base_url or "",
|
|
141
|
+
"api_key_masked": masked,
|
|
142
|
+
"has_key": bool(k),
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
def _get_client(self):
|
|
146
|
+
if self._client is not None:
|
|
147
|
+
return self._client
|
|
148
|
+
if self.provider == "anthropic":
|
|
149
|
+
try:
|
|
150
|
+
import anthropic
|
|
151
|
+
self._client = anthropic.Anthropic(api_key=self.api_key)
|
|
152
|
+
except ImportError:
|
|
153
|
+
raise ImportError("anthropic SDK 未安装,请运行: pip install anthropic")
|
|
154
|
+
else:
|
|
155
|
+
# deepseek / openai / openai-compatible 都用 openai SDK
|
|
156
|
+
try:
|
|
157
|
+
from openai import OpenAI
|
|
158
|
+
self._client = OpenAI(api_key=self.api_key, base_url=self.base_url)
|
|
159
|
+
except ImportError:
|
|
160
|
+
raise ImportError("openai SDK 未安装,请运行: pip install openai")
|
|
161
|
+
return self._client
|
|
162
|
+
|
|
163
|
+
def extract(self, chunks: list[Chunk], progress_cb=None) -> ExtractionResult:
|
|
164
|
+
"""
|
|
165
|
+
批量提取:每个 Chunk 单独调用 LLM,汇总为 ExtractionResult。
|
|
166
|
+
|
|
167
|
+
progress_cb: 可选 (processed, total) -> None 回调,每处理完一个 chunk 触发一次,
|
|
168
|
+
供异步摄入上报真进度。
|
|
169
|
+
"""
|
|
170
|
+
if not chunks:
|
|
171
|
+
return ExtractionResult(doc_id="", source="", items=[])
|
|
172
|
+
|
|
173
|
+
doc_id = chunks[0].doc_id
|
|
174
|
+
source = doc_id # ExtractionResult.source 存 doc_id,真实路径由 Packager 从外部传入
|
|
175
|
+
items: list[RawExtraction] = []
|
|
176
|
+
fail_count = 0
|
|
177
|
+
total = len(chunks)
|
|
178
|
+
|
|
179
|
+
# 并发抽取:每段一次 LLM 调用是 I/O 密集,串行 500 段要 40+ 分钟。用有限并发
|
|
180
|
+
# (默认 4,可经 HKC_EXTRACT_CONCURRENCY 调)把整本书从几十分钟压到几分钟。
|
|
181
|
+
# 并发度要温和,避免触发各 provider 的限流(429);429 已有 _retry_llm 兜底
|
|
182
|
+
# (指数退避 + Retry-After header),并发可适度放松但仍建议 4-8 区间。
|
|
183
|
+
import os
|
|
184
|
+
try:
|
|
185
|
+
workers = max(1, int(os.getenv("HKC_EXTRACT_CONCURRENCY", "4")))
|
|
186
|
+
except ValueError:
|
|
187
|
+
workers = 4
|
|
188
|
+
try:
|
|
189
|
+
llm_timeout = float(os.getenv("HKC_LLM_TIMEOUT", "60"))
|
|
190
|
+
except ValueError:
|
|
191
|
+
llm_timeout = 60.0
|
|
192
|
+
# 批量抽取(opt-in):一次 LLM 调用处理多段,省掉逐段模式里每段重复一遍系统提示词的 token。
|
|
193
|
+
# 默认 1 = 逐段抽取(行为不变);设 HKC_EXTRACT_BATCH=N 则 N 段一次调用。
|
|
194
|
+
try:
|
|
195
|
+
batch_size = max(1, int(os.getenv("HKC_EXTRACT_BATCH", "1")))
|
|
196
|
+
except ValueError:
|
|
197
|
+
batch_size = 1
|
|
198
|
+
if batch_size > 1:
|
|
199
|
+
return self._extract_batched(chunks, workers, llm_timeout, batch_size, progress_cb)
|
|
200
|
+
workers = min(workers, total)
|
|
201
|
+
done = 0
|
|
202
|
+
|
|
203
|
+
def _tick(raw):
|
|
204
|
+
nonlocal fail_count, done
|
|
205
|
+
if raw:
|
|
206
|
+
items.append(raw)
|
|
207
|
+
else:
|
|
208
|
+
fail_count += 1
|
|
209
|
+
done += 1
|
|
210
|
+
logger.info("提取 Chunk %d/%d (doc=%s)", done, total, doc_id)
|
|
211
|
+
if progress_cb is not None:
|
|
212
|
+
try:
|
|
213
|
+
progress_cb(done, total)
|
|
214
|
+
except Exception:
|
|
215
|
+
pass # 进度上报失败不影响摄入本身
|
|
216
|
+
|
|
217
|
+
if workers <= 1:
|
|
218
|
+
for chunk in chunks:
|
|
219
|
+
_tick(self._extract_chunk(chunk))
|
|
220
|
+
else:
|
|
221
|
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
222
|
+
with ThreadPoolExecutor(max_workers=workers, thread_name_prefix="extract") as pool:
|
|
223
|
+
futures = [pool.submit(self._extract_chunk, ch, llm_timeout) for ch in chunks]
|
|
224
|
+
for fut in as_completed(futures):
|
|
225
|
+
try:
|
|
226
|
+
raw = fut.result()
|
|
227
|
+
except Exception as e:
|
|
228
|
+
logger.error("Chunk 提取线程异常: %s", e)
|
|
229
|
+
raw = None
|
|
230
|
+
_tick(raw)
|
|
231
|
+
|
|
232
|
+
# 全部 chunk 都抽取失败 → 分两种情况,不能一刀切:
|
|
233
|
+
# 1) 根本没配 key:这是**设计内的降级**(无 LLM 时知识库照常读写,只是抽不出 KU,
|
|
234
|
+
# 文件摄入/格式门等链路仍要能跑通),返回空结果,不抛。
|
|
235
|
+
# 2) 配了 key 却全失败(401/超时/模型不存在):这才是真故障。必须抛,否则 API 层
|
|
236
|
+
# 返回 ku_count=0 + 200,用户以为录进去了,实际库是空的(复习永远 0 卡)。
|
|
237
|
+
if fail_count == len(chunks) and len(chunks) > 0:
|
|
238
|
+
reason = self._last_error
|
|
239
|
+
logger.error(
|
|
240
|
+
"知识抽取全部失败 (%d/%d chunks)。provider=%s, model=%s, has_key=%s。最后错误: %s",
|
|
241
|
+
fail_count, len(chunks), self.provider, self.model, bool(self.api_key), reason,
|
|
242
|
+
)
|
|
243
|
+
if self.api_key:
|
|
244
|
+
raise RuntimeError(
|
|
245
|
+
f"知识抽取失败:所有 {len(chunks)} 段都没能抽出内容。"
|
|
246
|
+
f"当前 provider={self.provider} model={self.model}。"
|
|
247
|
+
f"最后错误: {reason or '未知(模型返回为空或解析失败)'}"
|
|
248
|
+
)
|
|
249
|
+
logger.warning("未配置 API Key —— 按降级处理,本次不产出知识单元。")
|
|
250
|
+
|
|
251
|
+
return ExtractionResult(doc_id=doc_id, source=source, items=items)
|
|
252
|
+
|
|
253
|
+
def _retry_llm(self, func, *args, max_retries: int = 3, base_delay: float = 1.0, max_delay: float = 8.0, **kwargs):
|
|
254
|
+
"""
|
|
255
|
+
通用 LLM retry 包装:处理 429 RateLimit / 5xx / Timeout,指数退避 + Retry-After。
|
|
256
|
+
|
|
257
|
+
用法:
|
|
258
|
+
resp = self._retry_llm(
|
|
259
|
+
client.messages.create,
|
|
260
|
+
model=..., max_tokens=..., timeout=..., system=..., messages=[...],
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
可重试的异常:
|
|
264
|
+
- status_code == 429 (限流,anthropic/openai SDK 都会设置)
|
|
265
|
+
- 500 <= status_code < 600 (服务端错误)
|
|
266
|
+
- ConnectionError / TimeoutError (网络层)
|
|
267
|
+
|
|
268
|
+
退避策略:
|
|
269
|
+
- 优先读取异常 headers['Retry-After'] (秒),作为本次等待时间
|
|
270
|
+
- 否则用 base_delay,每次 * 2 直到 max_delay
|
|
271
|
+
- 最多 max_retries+1 次尝试 (默认 4 次 = 1 + 3 retry)
|
|
272
|
+
"""
|
|
273
|
+
delay = base_delay
|
|
274
|
+
for attempt in range(max_retries + 1):
|
|
275
|
+
try:
|
|
276
|
+
return func(*args, **kwargs)
|
|
277
|
+
except Exception as e:
|
|
278
|
+
status = getattr(e, 'status_code', None)
|
|
279
|
+
if status is None:
|
|
280
|
+
status = getattr(e, 'status', None)
|
|
281
|
+
is_429 = (status == 429) or 'RateLimit' in type(e).__name__
|
|
282
|
+
is_5xx = isinstance(status, int) and 500 <= status < 600
|
|
283
|
+
is_conn = isinstance(e, (ConnectionError, TimeoutError)) or 'Timeout' in type(e).__name__
|
|
284
|
+
retryable = is_429 or is_5xx or is_conn
|
|
285
|
+
if not retryable or attempt == max_retries:
|
|
286
|
+
raise
|
|
287
|
+
# 取 Retry-After header
|
|
288
|
+
wait = delay
|
|
289
|
+
headers = getattr(e, 'headers', None)
|
|
290
|
+
if headers and 'Retry-After' in headers:
|
|
291
|
+
try:
|
|
292
|
+
wait = float(headers['Retry-After'])
|
|
293
|
+
except (TypeError, ValueError):
|
|
294
|
+
pass
|
|
295
|
+
wait = min(wait, max_delay)
|
|
296
|
+
logger.warning(
|
|
297
|
+
"LLM %s (status=%s, attempt %d/%d) retry after %.1fs: %s",
|
|
298
|
+
type(e).__name__, status, attempt + 1, max_retries, wait, e,
|
|
299
|
+
)
|
|
300
|
+
time.sleep(wait)
|
|
301
|
+
delay = min(delay * 2, max_delay)
|
|
302
|
+
|
|
303
|
+
def complete(self, system: str, user: str, max_tokens: int = 1500, timeout: float = 60.0) -> str:
|
|
304
|
+
"""
|
|
305
|
+
通用单轮补全(非抽取):给定 system+user,返回模型文本。
|
|
306
|
+
供「综合页 LLM 综述」等派生视图复用同一套 provider 配置,保持模型无关。
|
|
307
|
+
与 _extract_chunk 一样兼容 anthropic 与 openai 系两条路径。
|
|
308
|
+
"""
|
|
309
|
+
client = self._get_client()
|
|
310
|
+
if self.provider == "anthropic":
|
|
311
|
+
resp = self._retry_llm(
|
|
312
|
+
client.messages.create,
|
|
313
|
+
model=self.model, max_tokens=max_tokens, timeout=timeout,
|
|
314
|
+
system=system,
|
|
315
|
+
messages=[{"role": "user", "content": user}],
|
|
316
|
+
)
|
|
317
|
+
return resp.content[0].text.strip()
|
|
318
|
+
resp = self._retry_llm(
|
|
319
|
+
client.chat.completions.create,
|
|
320
|
+
model=self.model, max_tokens=max_tokens, timeout=timeout,
|
|
321
|
+
messages=[{"role": "system", "content": system},
|
|
322
|
+
{"role": "user", "content": user}],
|
|
323
|
+
)
|
|
324
|
+
return resp.choices[0].message.content.strip()
|
|
325
|
+
|
|
326
|
+
def _extract_chunk(self, chunk: Chunk, timeout: float = 60.0) -> Optional[RawExtraction]:
|
|
327
|
+
"""
|
|
328
|
+
单个 Chunk 的提取。
|
|
329
|
+
LLM 调用失败时记录日志并返回 None,不中断整个流程。
|
|
330
|
+
"""
|
|
331
|
+
try:
|
|
332
|
+
client = self._get_client()
|
|
333
|
+
if self.provider == "anthropic":
|
|
334
|
+
resp = self._retry_llm(
|
|
335
|
+
client.messages.create,
|
|
336
|
+
model = self.model,
|
|
337
|
+
max_tokens = 2048,
|
|
338
|
+
timeout = timeout,
|
|
339
|
+
system = _SYSTEM_PROMPT,
|
|
340
|
+
messages = [{"role": "user", "content": chunk.content}],
|
|
341
|
+
)
|
|
342
|
+
raw_text = resp.content[0].text.strip()
|
|
343
|
+
else:
|
|
344
|
+
# OpenAI 兼容格式(DeepSeek / OpenAI):system 作为一条 message
|
|
345
|
+
resp = self._retry_llm(
|
|
346
|
+
client.chat.completions.create,
|
|
347
|
+
model = self.model,
|
|
348
|
+
max_tokens = 2048,
|
|
349
|
+
timeout = timeout,
|
|
350
|
+
messages = [
|
|
351
|
+
{"role": "system", "content": _SYSTEM_PROMPT},
|
|
352
|
+
{"role": "user", "content": chunk.content},
|
|
353
|
+
],
|
|
354
|
+
)
|
|
355
|
+
raw_text = resp.choices[0].message.content.strip()
|
|
356
|
+
raw = self._parse_response(raw_text, chunk.source_hint)
|
|
357
|
+
if raw:
|
|
358
|
+
# 把本 chunk 的原文段落挂到每条抽取结果上,供 packager 存进 KU,
|
|
359
|
+
# 前端点击知识节点时可显示"原文段落"(见 KU.source_text)。
|
|
360
|
+
src = chunk.content
|
|
361
|
+
for d in (raw.facts + raw.claims + raw.concepts + raw.entities):
|
|
362
|
+
d["_src"] = src
|
|
363
|
+
return raw
|
|
364
|
+
|
|
365
|
+
except Exception as e:
|
|
366
|
+
logger.error("Chunk 提取失败 (chunk_id=%s): %s", chunk.chunk_id, e)
|
|
367
|
+
# 留给 _run_pipeline:全部失败时把真实原因(401/超时/模型不存在)带出去,
|
|
368
|
+
# 而不是只回一句"0 条"。
|
|
369
|
+
self._last_error = e
|
|
370
|
+
return None
|
|
371
|
+
|
|
372
|
+
def _extract_batched(self, chunks, workers, llm_timeout, batch_size, progress_cb):
|
|
373
|
+
"""批量抽取:把 chunks 按 batch_size 分组,每组一次 LLM 调用。返回 ExtractionResult。"""
|
|
374
|
+
total = len(chunks)
|
|
375
|
+
groups = [chunks[i:i + batch_size] for i in range(0, total, batch_size)]
|
|
376
|
+
n_groups = len(groups)
|
|
377
|
+
results = [None] * n_groups # 每组对齐结果 list[Optional[RawExtraction]]
|
|
378
|
+
done = 0
|
|
379
|
+
|
|
380
|
+
def _collect(gi, raws):
|
|
381
|
+
nonlocal done
|
|
382
|
+
results[gi] = raws
|
|
383
|
+
done += len(groups[gi])
|
|
384
|
+
if progress_cb is not None:
|
|
385
|
+
try:
|
|
386
|
+
progress_cb(min(done, total), total)
|
|
387
|
+
except Exception:
|
|
388
|
+
pass
|
|
389
|
+
|
|
390
|
+
if workers <= 1 or n_groups == 1:
|
|
391
|
+
for gi, g in enumerate(groups):
|
|
392
|
+
_collect(gi, self._extract_batch(g, llm_timeout))
|
|
393
|
+
else:
|
|
394
|
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
395
|
+
with ThreadPoolExecutor(max_workers=min(workers, n_groups),
|
|
396
|
+
thread_name_prefix="extract") as pool:
|
|
397
|
+
futs = {pool.submit(self._extract_batch, g, llm_timeout): gi
|
|
398
|
+
for gi, g in enumerate(groups)}
|
|
399
|
+
for fut in as_completed(futs):
|
|
400
|
+
gi = futs[fut]
|
|
401
|
+
try:
|
|
402
|
+
raws = fut.result()
|
|
403
|
+
except Exception as e:
|
|
404
|
+
logger.error("批量抽取线程异常 (group=%d): %s", gi, e)
|
|
405
|
+
raws = [None] * len(groups[gi])
|
|
406
|
+
_collect(gi, raws)
|
|
407
|
+
|
|
408
|
+
items = []
|
|
409
|
+
fail_count = 0
|
|
410
|
+
for gi in range(n_groups):
|
|
411
|
+
for r in (results[gi] or []):
|
|
412
|
+
if r is None:
|
|
413
|
+
fail_count += 1
|
|
414
|
+
else:
|
|
415
|
+
items.append(r)
|
|
416
|
+
|
|
417
|
+
# 全部失败 → 大概率 LLM 配置问题(与逐段路径同一诊断口径)
|
|
418
|
+
if fail_count == total and total > 0:
|
|
419
|
+
logger.error(
|
|
420
|
+
"知识抽取全部失败 (%d/%d chunks)。provider=%s, model=%s。"
|
|
421
|
+
"若上方有「JSON 解析失败」,说明输出被截断:调大 HKC_EXTRACT_MAX_TOKENS "
|
|
422
|
+
"或调小 HKC_EXTRACT_BATCH;否则检查 API Key / 网络。",
|
|
423
|
+
fail_count, total, self.provider, self.model,
|
|
424
|
+
)
|
|
425
|
+
|
|
426
|
+
doc_id = chunks[0].doc_id
|
|
427
|
+
return ExtractionResult(doc_id=doc_id, source=doc_id, items=items)
|
|
428
|
+
|
|
429
|
+
@staticmethod
|
|
430
|
+
def _batch_max_tokens(n: int) -> int:
|
|
431
|
+
"""单批输出上限:随段数放大(每段最多 5×4 条),硬顶可配。
|
|
432
|
+
|
|
433
|
+
旧公式是 1024*n,批 4 段正好 4096 —— 恰好撞在 deepseek-chat 的单次输出
|
|
434
|
+
硬顶上。内容密的中文文档会在 JSON 写到一半被截断,整批 4 段全部丢弃,
|
|
435
|
+
表象就是「摄入完成但 0 条知识」。这里给足余量并允许用
|
|
436
|
+
HKC_EXTRACT_MAX_TOKENS 覆盖(上限 8192)。
|
|
437
|
+
"""
|
|
438
|
+
try:
|
|
439
|
+
cap = max(2048, int(os.getenv("HKC_EXTRACT_MAX_TOKENS", "8192")))
|
|
440
|
+
except ValueError:
|
|
441
|
+
cap = 8192
|
|
442
|
+
return max(2048, min(cap, 1280 * n))
|
|
443
|
+
|
|
444
|
+
def _extract_batch(self, chunks, timeout, max_tokens: Optional[int] = None):
|
|
445
|
+
"""一次 LLM 调用抽取一组 chunks,返回与 chunks 对齐的 list[Optional[RawExtraction]]。"""
|
|
446
|
+
n = len(chunks)
|
|
447
|
+
parts = [f'<chunk id="{i}">\n{ch.content}\n</chunk>' for i, ch in enumerate(chunks)]
|
|
448
|
+
user = "\n\n".join(parts)
|
|
449
|
+
if max_tokens is None:
|
|
450
|
+
max_tokens = self._batch_max_tokens(n)
|
|
451
|
+
try:
|
|
452
|
+
client = self._get_client()
|
|
453
|
+
if self.provider == "anthropic":
|
|
454
|
+
resp = self._retry_llm(
|
|
455
|
+
client.messages.create,
|
|
456
|
+
model = self.model,
|
|
457
|
+
max_tokens = max_tokens,
|
|
458
|
+
timeout = timeout,
|
|
459
|
+
system = _BATCH_SYSTEM_PROMPT,
|
|
460
|
+
messages = [{"role": "user", "content": user}],
|
|
461
|
+
)
|
|
462
|
+
raw_text = resp.content[0].text.strip()
|
|
463
|
+
else:
|
|
464
|
+
resp = self._retry_llm(
|
|
465
|
+
client.chat.completions.create,
|
|
466
|
+
model = self.model,
|
|
467
|
+
max_tokens = max_tokens,
|
|
468
|
+
timeout = timeout,
|
|
469
|
+
messages = [
|
|
470
|
+
{"role": "system", "content": _BATCH_SYSTEM_PROMPT},
|
|
471
|
+
{"role": "user", "content": user},
|
|
472
|
+
],
|
|
473
|
+
)
|
|
474
|
+
raw_text = resp.choices[0].message.content.strip()
|
|
475
|
+
except Exception as e:
|
|
476
|
+
logger.error("批量提取失败 (首 chunk=%s): %s",
|
|
477
|
+
chunks[0].chunk_id if chunks else "?", e)
|
|
478
|
+
return [None] * n
|
|
479
|
+
|
|
480
|
+
parsed = self._parse_batch_response(raw_text, n)
|
|
481
|
+
# 整批解析失败(JSON 被截断 / 空响应):拆半重试,不把整批丢掉。
|
|
482
|
+
# 逐层对半拆到单段为止;单段仍失败,就放大输出上限再试一次。
|
|
483
|
+
if n > 1 and all(p is None for p in parsed):
|
|
484
|
+
mid = n // 2
|
|
485
|
+
logger.warning("批量抽取整批解析失败,拆分为 %d + %d 重试", mid, n - mid)
|
|
486
|
+
return (self._extract_batch(chunks[:mid], timeout)
|
|
487
|
+
+ self._extract_batch(chunks[mid:], timeout))
|
|
488
|
+
if n == 1 and parsed[0] is None:
|
|
489
|
+
bigger = min(8192, max_tokens * 2)
|
|
490
|
+
if bigger > max_tokens:
|
|
491
|
+
logger.warning("单段抽取解析失败,放大 max_tokens %d → %d 重试",
|
|
492
|
+
max_tokens, bigger)
|
|
493
|
+
return self._extract_batch(chunks, timeout, bigger)
|
|
494
|
+
for i, raw in enumerate(parsed):
|
|
495
|
+
if raw is None:
|
|
496
|
+
continue
|
|
497
|
+
raw.source_hint = chunks[i].source_hint
|
|
498
|
+
src = chunks[i].content
|
|
499
|
+
for d in (raw.facts + raw.claims + raw.concepts + raw.entities):
|
|
500
|
+
d["_src"] = src
|
|
501
|
+
return parsed
|
|
502
|
+
|
|
503
|
+
@staticmethod
|
|
504
|
+
def _strip_fences(text: str) -> str:
|
|
505
|
+
"""去掉 LLM 偶尔包裹的 Markdown 代码块围栏。"""
|
|
506
|
+
text = re.sub(r'^```(?:json)?\s*', '', text, flags=re.MULTILINE)
|
|
507
|
+
text = re.sub(r'\s*```$', '', text, flags=re.MULTILINE)
|
|
508
|
+
return text.strip()
|
|
509
|
+
|
|
510
|
+
def _raw_from_dict(self, data, source_hint: str) -> Optional[RawExtraction]:
|
|
511
|
+
"""把单个 chunk 的 JSON dict 校验并规范化为 RawExtraction(空则 None)。"""
|
|
512
|
+
if not isinstance(data, dict):
|
|
513
|
+
return None
|
|
514
|
+
facts = self._validate_facts(data.get("facts", []))
|
|
515
|
+
claims = self._validate_claims(data.get("claims", []))
|
|
516
|
+
entities = self._validate_entities(data.get("entities", []))
|
|
517
|
+
concepts = self._validate_concepts(data.get("concepts", []))
|
|
518
|
+
|
|
519
|
+
if not any([facts, claims, entities, concepts]):
|
|
520
|
+
return None
|
|
521
|
+
|
|
522
|
+
return RawExtraction(
|
|
523
|
+
facts = facts,
|
|
524
|
+
claims = claims,
|
|
525
|
+
entities = entities,
|
|
526
|
+
concepts = concepts,
|
|
527
|
+
source_hint = source_hint,
|
|
528
|
+
)
|
|
529
|
+
|
|
530
|
+
def _parse_response(self, text: str, source_hint: str) -> Optional[RawExtraction]:
|
|
531
|
+
"""
|
|
532
|
+
解析单段 LLM 返回的 JSON。
|
|
533
|
+
容错处理:去掉可能的 Markdown 代码块包裹。
|
|
534
|
+
"""
|
|
535
|
+
text = self._strip_fences(text)
|
|
536
|
+
try:
|
|
537
|
+
data = json.loads(text)
|
|
538
|
+
except json.JSONDecodeError as e:
|
|
539
|
+
logger.warning("JSON 解析失败: %s\n原文: %.200s", e, text)
|
|
540
|
+
return None
|
|
541
|
+
return self._raw_from_dict(data, source_hint)
|
|
542
|
+
|
|
543
|
+
def _parse_batch_response(self, text: str, n: int) -> list[Optional[RawExtraction]]:
|
|
544
|
+
"""解析批量 LLM 返回的 JSON:顶层 key 为段 id(字符串 "0".."n-1")。"""
|
|
545
|
+
text = self._strip_fences(text)
|
|
546
|
+
try:
|
|
547
|
+
data = json.loads(text)
|
|
548
|
+
except json.JSONDecodeError as e:
|
|
549
|
+
logger.warning("批量 JSON 解析失败: %s\n原文: %.200s", e, text)
|
|
550
|
+
return [None] * n
|
|
551
|
+
if not isinstance(data, dict):
|
|
552
|
+
return [None] * n
|
|
553
|
+
return [self._raw_from_dict(data.get(str(i)), "") for i in range(n)]
|
|
554
|
+
|
|
555
|
+
# ── 校验方法:确保字段存在且类型正确 ────────────────────
|
|
556
|
+
|
|
557
|
+
def _validate_facts(self, items: list) -> list[dict]:
|
|
558
|
+
result = []
|
|
559
|
+
for item in items[:5]:
|
|
560
|
+
if not isinstance(item, dict):
|
|
561
|
+
continue
|
|
562
|
+
stmt = str(item.get("statement", "")).strip()
|
|
563
|
+
if stmt:
|
|
564
|
+
result.append({
|
|
565
|
+
"statement": stmt,
|
|
566
|
+
"source_hint": str(item.get("source_hint", "")),
|
|
567
|
+
})
|
|
568
|
+
return result
|
|
569
|
+
|
|
570
|
+
def _validate_claims(self, items: list) -> list[dict]:
|
|
571
|
+
result = []
|
|
572
|
+
for item in items[:5]:
|
|
573
|
+
if not isinstance(item, dict):
|
|
574
|
+
continue
|
|
575
|
+
stmt = str(item.get("statement", "")).strip()
|
|
576
|
+
if not stmt:
|
|
577
|
+
continue
|
|
578
|
+
conf = item.get("confidence", 0.5)
|
|
579
|
+
try:
|
|
580
|
+
conf = float(conf)
|
|
581
|
+
conf = max(0.0, min(1.0, conf))
|
|
582
|
+
except (TypeError, ValueError):
|
|
583
|
+
conf = 0.5
|
|
584
|
+
result.append({
|
|
585
|
+
"statement": stmt,
|
|
586
|
+
"confidence": conf,
|
|
587
|
+
"domain": str(item.get("domain", "")),
|
|
588
|
+
})
|
|
589
|
+
return result
|
|
590
|
+
|
|
591
|
+
def _validate_entities(self, items: list) -> list[dict]:
|
|
592
|
+
valid_types = {"Person", "Organization", "Product", "Event", "Place"}
|
|
593
|
+
result = []
|
|
594
|
+
for item in items[:5]:
|
|
595
|
+
if not isinstance(item, dict):
|
|
596
|
+
continue
|
|
597
|
+
name = str(item.get("name", "")).strip()
|
|
598
|
+
if not name:
|
|
599
|
+
continue
|
|
600
|
+
entity_type = item.get("type", "Person")
|
|
601
|
+
if entity_type not in valid_types:
|
|
602
|
+
entity_type = "Person"
|
|
603
|
+
result.append({
|
|
604
|
+
"name": name,
|
|
605
|
+
"type": entity_type,
|
|
606
|
+
"aliases": list(item.get("aliases", [])),
|
|
607
|
+
})
|
|
608
|
+
return result
|
|
609
|
+
|
|
610
|
+
def _validate_concepts(self, items: list) -> list[dict]:
|
|
611
|
+
result = []
|
|
612
|
+
for item in items[:5]:
|
|
613
|
+
if not isinstance(item, dict):
|
|
614
|
+
continue
|
|
615
|
+
name = str(item.get("name", "")).strip()
|
|
616
|
+
if not name:
|
|
617
|
+
continue
|
|
618
|
+
result.append({
|
|
619
|
+
"name": name,
|
|
620
|
+
"domain": str(item.get("domain", "")),
|
|
621
|
+
"definition": str(item.get("definition", "")),
|
|
622
|
+
})
|
|
623
|
+
return result
|