foxclaw 0.1.0 โ 0.1.2
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 +11 -10
- package/package.json +46 -38
- package/python/.dockerignore +35 -0
- package/python/.env.example +165 -0
- package/python/.gitattributes +46 -0
- package/python/.github/workflows/ci.yml +122 -0
- package/python/.github/workflows/release.yml +183 -0
- package/python/Dockerfile +64 -0
- package/python/LICENSE +21 -0
- package/python/MANIFEST.in +14 -0
- package/python/README.md +192 -0
- package/python/README_CN.md +324 -0
- package/python/bench/bm25_audit.py +199 -0
- package/python/bench/bm25_regression.py +178 -0
- package/python/bench/bm25_stress.py +159 -0
- package/python/bench/cd_audit.py +238 -0
- package/python/bench/chaos.py +142 -0
- package/python/bench/concurrency.py +103 -0
- package/python/bench/e2e_real_models.py +95 -0
- package/python/bench/llm_retry_test.py +248 -0
- package/python/bench/load_test.py +196 -0
- package/python/bench/vector_ivf_upgrade.py +134 -0
- package/python/docs/CHANNELS.md +195 -0
- package/python/docs/GOVERNANCE.md +164 -0
- package/python/docs/RELEASE.md +109 -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 +152 -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 +400 -0
- package/python/examples/governance_middleware_demo.py +77 -0
- package/python/examples/langgraph_governance_demo.py +108 -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 +526 -0
- package/python/foxclaw/agents/__init__.py +12 -0
- package/python/foxclaw/agents/agents.py +35 -0
- package/python/foxclaw/agents/base.py +865 -0
- package/python/foxclaw/agents/impl/__init__.py +1 -0
- package/python/foxclaw/agents/impl/_helpers.py +78 -0
- package/python/foxclaw/agents/impl/finance.py +248 -0
- package/python/foxclaw/agents/impl/office.py +367 -0
- package/python/foxclaw/agents/impl/supervisor.py +659 -0
- package/python/foxclaw/agents/mixins/_context.py +151 -0
- package/python/foxclaw/agents/mixins/augment.py +149 -0
- package/python/foxclaw/agents/mixins/llm.py +66 -0
- package/python/foxclaw/agents/mixins/memory.py +35 -0
- package/python/foxclaw/agents/mixins/preference.py +81 -0
- package/python/foxclaw/agents/mixins/prompt.py +77 -0
- package/python/foxclaw/agents/mixins/skillbox.py +58 -0
- package/python/foxclaw/agents/mixins/tool_exec.py +443 -0
- package/python/foxclaw/api/__init__.py +283 -0
- package/python/foxclaw/api/app.py +123 -0
- package/python/foxclaw/api/middleware.py +317 -0
- package/python/foxclaw/api/routes/ahs.py +417 -0
- package/python/foxclaw/api/routes/attachments.py +117 -0
- package/python/foxclaw/api/routes/browser.py +39 -0
- package/python/foxclaw/api/routes/domain.py +314 -0
- package/python/foxclaw/api/routes/knowledge.py +163 -0
- package/python/foxclaw/api/routes/llm_admin.py +461 -0
- package/python/foxclaw/api/routes/memory.py +89 -0
- package/python/foxclaw/api/routes/orchestration.py +41 -0
- package/python/foxclaw/api/routes/recovery.py +43 -0
- package/python/foxclaw/api/routes/review.py +275 -0
- package/python/foxclaw/api/routes/search.py +37 -0
- package/python/foxclaw/api/routes/system.py +327 -0
- package/python/foxclaw/api/routes/task.py +281 -0
- package/python/foxclaw/api/routes/ws.py +68 -0
- package/python/foxclaw/api/spine.py +55 -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 +54 -0
- package/python/foxclaw/browser/action/action_types.py +45 -0
- package/python/foxclaw/browser/action/action_verifier.py +68 -0
- package/python/foxclaw/browser/action/url_policy.py +147 -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 +166 -0
- package/python/foxclaw/browser/perception/__init__.py +1 -0
- package/python/foxclaw/browser/perception/dom_extractor.py +43 -0
- package/python/foxclaw/browser/perception/element_indexer.py +57 -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 +148 -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 +67 -0
- package/python/foxclaw/channels/__init__.py +140 -0
- package/python/foxclaw/channels/discord.py +184 -0
- package/python/foxclaw/channels/dispatcher.py +204 -0
- package/python/foxclaw/channels/feishu.py +225 -0
- package/python/foxclaw/channels/openclaw_bridge.py +156 -0
- package/python/foxclaw/channels/signing.py +187 -0
- package/python/foxclaw/channels/telegram.py +121 -0
- package/python/foxclaw/channels/weixin.py +225 -0
- package/python/foxclaw/cli.py +154 -0
- package/python/foxclaw/cognitive/__init__.py +4 -0
- package/python/foxclaw/cognitive/reasoner.py +82 -0
- package/python/foxclaw/cognitive/state.py +147 -0
- package/python/foxclaw/core/__init__.py +9 -0
- package/python/foxclaw/core/audit_chain.py +133 -0
- package/python/foxclaw/core/audit_sink.py +100 -0
- package/python/foxclaw/core/compaction.py +283 -0
- package/python/foxclaw/core/config.py +65 -0
- package/python/foxclaw/core/confirm_nonce.py +157 -0
- package/python/foxclaw/core/dispatch.py +27 -0
- package/python/foxclaw/core/envfile.py +48 -0
- package/python/foxclaw/core/failure_policy.py +151 -0
- package/python/foxclaw/core/gateway.py +85 -0
- package/python/foxclaw/core/governance_audit.py +175 -0
- package/python/foxclaw/core/idempotency.py +289 -0
- package/python/foxclaw/core/injection_guard.py +221 -0
- package/python/foxclaw/core/memory_adapter.py +516 -0
- package/python/foxclaw/core/metrics.py +89 -0
- package/python/foxclaw/core/model_router.py +1097 -0
- package/python/foxclaw/core/paths.py +103 -0
- package/python/foxclaw/core/recovery.py +362 -0
- package/python/foxclaw/core/scheduler.py +144 -0
- package/python/foxclaw/core/secrets.py +134 -0
- package/python/foxclaw/core/sessions.py +157 -0
- package/python/foxclaw/core/settings.py +430 -0
- package/python/foxclaw/core/shared_write.py +198 -0
- package/python/foxclaw/core/steering_queue.py +153 -0
- package/python/foxclaw/core/system_bus.py +84 -0
- package/python/foxclaw/core/thought_chain.py +318 -0
- package/python/foxclaw/core/token_meter.py +235 -0
- package/python/foxclaw/core/tools.py +172 -0
- package/python/foxclaw/core/tracing.py +390 -0
- package/python/foxclaw/core/truth_pipeline.py +114 -0
- package/python/foxclaw/core/vec_recall_adapter.py +503 -0
- package/python/foxclaw/core/verify.py +181 -0
- package/python/foxclaw/core/web_search.py +242 -0
- package/python/foxclaw/finance/__init__.py +15 -0
- package/python/foxclaw/finance/evidence.py +153 -0
- package/python/foxclaw/finance/fx.py +100 -0
- package/python/foxclaw/finance/graph.py +188 -0
- package/python/foxclaw/finance/models.py +150 -0
- package/python/foxclaw/finance/nlparse.py +575 -0
- package/python/foxclaw/finance/policy.py +329 -0
- package/python/foxclaw/finance/reasoner.py +129 -0
- package/python/foxclaw/finance/reasoning.py +340 -0
- package/python/foxclaw/finance/reconcile.py +180 -0
- package/python/foxclaw/finance/runtime.py +527 -0
- package/python/foxclaw/governance/__init__.py +24 -0
- package/python/foxclaw/governance/middleware.py +184 -0
- package/python/foxclaw/harness/__init__.py +17 -0
- package/python/foxclaw/harness/_base.py +72 -0
- package/python/foxclaw/harness/_policy.py +359 -0
- package/python/foxclaw/harness/drift.py +89 -0
- package/python/foxclaw/harness/event_bus.py +31 -0
- package/python/foxclaw/harness/harness.py +446 -0
- package/python/foxclaw/harness/loop_detection.py +152 -0
- package/python/foxclaw/itinerary/__init__.py +10 -0
- package/python/foxclaw/itinerary/amap_client.py +117 -0
- package/python/foxclaw/itinerary/models.py +80 -0
- package/python/foxclaw/itinerary/reasoner.py +101 -0
- package/python/foxclaw/itinerary/reasoning.py +174 -0
- package/python/foxclaw/itinerary/runtime.py +148 -0
- package/python/foxclaw/kernel/__init__.py +13 -0
- package/python/foxclaw/kernel/capabilities.py +73 -0
- package/python/foxclaw/kernel/policy_gate.py +114 -0
- package/python/foxclaw/kernel/process_manager.py +205 -0
- package/python/foxclaw/kernel/scheduler.py +131 -0
- package/python/foxclaw/kernel/sentinel.py +453 -0
- package/python/foxclaw/kernel/sentinel_client.py +246 -0
- package/python/foxclaw/kernel/spine.py +97 -0
- package/python/foxclaw/knowledge/__init__.py +376 -0
- package/python/foxclaw/knowledge/truth_crystallizer.py +92 -0
- package/python/foxclaw/learning/__init__.py +12 -0
- package/python/foxclaw/learning/knowledge_graph.py +149 -0
- package/python/foxclaw/learning/models.py +93 -0
- package/python/foxclaw/learning/reasoner.py +70 -0
- package/python/foxclaw/learning/runtime.py +363 -0
- package/python/foxclaw/learning/spaced_repetition.py +96 -0
- package/python/foxclaw/mcp_server.py +189 -0
- package/python/foxclaw/orchestration/__init__.py +184 -0
- package/python/foxclaw/pipeline/__init__.py +23 -0
- package/python/foxclaw/pipeline/cognitive_pipeline.py +481 -0
- package/python/foxclaw/pipeline/supervisor_stages.py +230 -0
- package/python/foxclaw/planning/__init__.py +17 -0
- package/python/foxclaw/planning/llm_planner.py +232 -0
- package/python/foxclaw/planning/planner.py +99 -0
- package/python/foxclaw/search/__init__.py +24 -0
- package/python/foxclaw/search/agent_port.py +194 -0
- package/python/foxclaw/search/evidence.py +113 -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 +73 -0
- package/python/foxclaw/search/models.py +129 -0
- package/python/foxclaw/search/planner.py +44 -0
- package/python/foxclaw/search/retriever.py +39 -0
- package/python/foxclaw/search/runtime.py +134 -0
- package/python/foxclaw/search/strategy.py +44 -0
- package/python/foxclaw/skills/__init__.py +613 -0
- package/python/foxclaw/skills/hooks_spec.py +73 -0
- package/python/foxclaw/skills/lifecycle.py +278 -0
- package/python/foxclaw/skills/markdown_skill.py +174 -0
- package/python/foxclaw/skills/markdown_store.py +39 -0
- package/python/foxclaw/skills/mcp_client.py +190 -0
- package/python/foxclaw/skills/mcp_store.py +43 -0
- package/python/foxclaw/skills/permission.py +180 -0
- package/python/foxclaw/skills/pipeline.py +386 -0
- package/python/foxclaw/skills/policy.py +80 -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 +32 -0
- package/python/frontend/deck/chat_model_picker.js +537 -0
- package/python/frontend/deck/domain.js +296 -0
- package/python/frontend/deck/main.js +1950 -0
- package/python/frontend/deck/markdown.js +48 -0
- package/python/frontend/deck/settings.js +324 -0
- package/python/frontend/deck/state.js +6 -0
- package/python/frontend/deck/util.js +6 -0
- package/python/frontend/deck/visuals.js +212 -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 +1637 -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-BD7VPbMV.js +33 -0
- package/python/frontend/v3/assets/index-CAvnYWGH.css +1 -0
- package/python/frontend/v3/index.html +15 -0
- package/python/frontend/widget_runtime.js +295 -0
- package/python/frontend/widgets.js +696 -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 +62 -0
- package/python/frontend_src/src/api.js +89 -0
- package/python/frontend_src/src/components/ChatDock.vue +377 -0
- package/python/frontend_src/src/components/ComposerMenu.vue +75 -0
- package/python/frontend_src/src/components/DocViewer.vue +171 -0
- package/python/frontend_src/src/components/Inspector.vue +241 -0
- package/python/frontend_src/src/components/LeftRail.vue +394 -0
- package/python/frontend_src/src/components/MenuPopover.vue +130 -0
- package/python/frontend_src/src/components/MessageStream.vue +401 -0
- package/python/frontend_src/src/components/ModelPicker.vue +355 -0
- package/python/frontend_src/src/components/QuickIngest.vue +118 -0
- package/python/frontend_src/src/components/Stage.vue +226 -0
- package/python/frontend_src/src/components/ToolMenu.vue +144 -0
- package/python/frontend_src/src/components/TopBar.vue +90 -0
- package/python/frontend_src/src/components/panels/ActivityPanel.vue +84 -0
- package/python/frontend_src/src/components/panels/AgentsPanel.vue +86 -0
- package/python/frontend_src/src/components/panels/KnowledgePanel.vue +441 -0
- package/python/frontend_src/src/components/panels/MemoryPanel.vue +118 -0
- package/python/frontend_src/src/components/panels/PanelBox.vue +51 -0
- package/python/frontend_src/src/components/panels/ReviewPanel.vue +308 -0
- package/python/frontend_src/src/components/panels/SettingsPanel.vue +254 -0
- package/python/frontend_src/src/components/panels/TasksPanel.vue +74 -0
- package/python/frontend_src/src/components/panels/ToolsPanel.vue +640 -0
- package/python/frontend_src/src/components/panels/UsagePanel.vue +105 -0
- package/python/frontend_src/src/lib/fmt.js +48 -0
- package/python/frontend_src/src/lib/md.js +204 -0
- package/python/frontend_src/src/main.js +7 -0
- package/python/frontend_src/src/stores/app.js +106 -0
- package/python/frontend_src/src/stores/chat.js +484 -0
- package/python/frontend_src/src/stores/model.js +104 -0
- package/python/frontend_src/src/stores/panels.js +330 -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 +46 -0
- package/python/pyproject.toml +265 -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 +76 -0
- package/python/tests/run_all.py +263 -0
- package/python/tests/test_agent_lifecycle.py +360 -0
- package/python/tests/test_ahs_p2.py +414 -0
- package/python/tests/test_anthropic_tools.py +206 -0
- package/python/tests/test_api.py +87 -0
- package/python/tests/test_api_auth.py +83 -0
- package/python/tests/test_api_host_guard.py +69 -0
- package/python/tests/test_attachments.py +167 -0
- package/python/tests/test_audit_chain.py +121 -0
- package/python/tests/test_audit_sink.py +136 -0
- package/python/tests/test_browser.py +153 -0
- package/python/tests/test_browser_request_guard.py +184 -0
- package/python/tests/test_capabilities.py +158 -0
- package/python/tests/test_channel_signing.py +192 -0
- package/python/tests/test_channel_trust.py +109 -0
- package/python/tests/test_channels.py +184 -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 +65 -0
- package/python/tests/test_confirm_nonce.py +197 -0
- package/python/tests/test_context_compaction_wiring.py +130 -0
- package/python/tests/test_dispatch.py +95 -0
- package/python/tests/test_domain_evals.py +403 -0
- package/python/tests/test_domain_import.py +78 -0
- package/python/tests/test_domain_reasoners.py +148 -0
- package/python/tests/test_e2e.py +178 -0
- package/python/tests/test_emb_cache.py +107 -0
- package/python/tests/test_embedding_singleton.py +89 -0
- package/python/tests/test_envfile.py +83 -0
- package/python/tests/test_evals_smoke.py +114 -0
- package/python/tests/test_event_spine.py +182 -0
- package/python/tests/test_evidence_semantic.py +101 -0
- package/python/tests/test_evidence_sse.py +97 -0
- package/python/tests/test_file_ingest.py +146 -0
- package/python/tests/test_finance.py +153 -0
- package/python/tests/test_finance_ledger.py +252 -0
- package/python/tests/test_finance_persist.py +178 -0
- package/python/tests/test_gateway.py +144 -0
- package/python/tests/test_governance.py +468 -0
- package/python/tests/test_governance_audit.py +359 -0
- package/python/tests/test_governance_middleware.py +143 -0
- package/python/tests/test_hkc.py +286 -0
- package/python/tests/test_hkc_index_build_manager.py +339 -0
- package/python/tests/test_hkc_manifest_reuse.py +317 -0
- package/python/tests/test_hkc_search_event_sync.py +533 -0
- package/python/tests/test_import_path_guard.py +161 -0
- package/python/tests/test_injection_guard.py +211 -0
- package/python/tests/test_integration.py +141 -0
- package/python/tests/test_itinerary.py +103 -0
- package/python/tests/test_kee_semantic.py +89 -0
- package/python/tests/test_kernel.py +222 -0
- package/python/tests/test_learning.py +110 -0
- package/python/tests/test_llm_admin.py +160 -0
- package/python/tests/test_llm_planner.py +366 -0
- package/python/tests/test_markdown_skills.py +73 -0
- package/python/tests/test_markdown_skills_openclaw.py +71 -0
- package/python/tests/test_mcp_connectors.py +109 -0
- package/python/tests/test_mcp_guardian_discovery.py +91 -0
- package/python/tests/test_mcp_no_memory.py +43 -0
- package/python/tests/test_mcp_policy_gate.py +77 -0
- package/python/tests/test_mcp_server.py +95 -0
- package/python/tests/test_memory_recall.py +95 -0
- package/python/tests/test_metrics.py +66 -0
- package/python/tests/test_multi_chain.py +102 -0
- package/python/tests/test_multimodal.py +135 -0
- package/python/tests/test_negotiation.py +146 -0
- package/python/tests/test_orchestration.py +119 -0
- package/python/tests/test_orchestration_decoupled.py +109 -0
- package/python/tests/test_p3_p5_hardening.py +338 -0
- package/python/tests/test_parallel_dispatch.py +215 -0
- package/python/tests/test_pipeline.py +533 -0
- package/python/tests/test_plan_visible.py +86 -0
- package/python/tests/test_planner.py +95 -0
- package/python/tests/test_policy_gate.py +109 -0
- package/python/tests/test_reasoner.py +185 -0
- package/python/tests/test_release_check.py +263 -0
- package/python/tests/test_resume_checkpoint.py +156 -0
- package/python/tests/test_routing.py +95 -0
- package/python/tests/test_scheduler.py +141 -0
- package/python/tests/test_search.py +204 -0
- package/python/tests/test_secrets.py +159 -0
- package/python/tests/test_semantic.py +78 -0
- package/python/tests/test_sessions.py +91 -0
- package/python/tests/test_settings.py +281 -0
- package/python/tests/test_shadow_learning.py +330 -0
- package/python/tests/test_shared_write.py +207 -0
- package/python/tests/test_skill_taint_gate.py +154 -0
- package/python/tests/test_skills.py +133 -0
- package/python/tests/test_stats_and_events.py +165 -0
- package/python/tests/test_streaming.py +60 -0
- package/python/tests/test_supervisor_pipeline.py +177 -0
- package/python/tests/test_supervisor_triage.py +108 -0
- package/python/tests/test_taint.py +117 -0
- package/python/tests/test_task_stream.py +85 -0
- package/python/tests/test_thinking_sse.py +67 -0
- package/python/tests/test_thinking_stream.py +76 -0
- package/python/tests/test_tool_center.py +129 -0
- package/python/tests/test_tool_parallel.py +113 -0
- package/python/tests/test_tool_params.py +103 -0
- package/python/tests/test_tool_use.py +242 -0
- package/python/tests/test_toplevel_concurrency.py +117 -0
- package/python/tests/test_tracing.py +290 -0
- package/python/tests/test_truth_crystallizer.py +127 -0
- package/python/tests/test_truth_flow.py +93 -0
- package/python/tests/test_url_policy.py +53 -0
- package/python/tests/test_verify_evidence.py +225 -0
- package/python/tests/test_version_sync.py +71 -0
- package/python/tests/test_web_trigger.py +99 -0
- package/python/tools/check_faiss_type.py +31 -0
- package/python/tools/check_log.ps1 +46 -0
- package/python/tools/ivf_status.py +72 -0
- package/python/tools/mypy_baseline.txt +5 -0
- package/python/tools/mypy_ratchet.py +97 -0
- package/python/tools/pack.py +178 -0
- package/python/tools/release_check.py +349 -0
- package/python/tools/restart_foxclaw.ps1 +74 -0
- package/python/tools/shared_write_audit.py +204 -0
- package/python/tools/size_baseline.txt +28 -0
- package/python/tools/size_check.py +102 -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,88 @@
|
|
|
1
|
+
"""
|
|
2
|
+
hkc-api / routes / search.py
|
|
3
|
+
ๆ็ดข่ทฏ็ฑ๏ผๅ
ณ้ฎ่ฏ / ๅ้ / ๅพ่ฐฑ / ๆททๅๆฃ็ดขใ
|
|
4
|
+
"""
|
|
5
|
+
from fastapi import APIRouter, HTTPException, Depends
|
|
6
|
+
|
|
7
|
+
from ..container import HKCContainer, get_container
|
|
8
|
+
from ..schemas import (
|
|
9
|
+
SearchRequest, SearchResponse, SearchHit, NeighborsRequest,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
router = APIRouter(prefix="/search", tags=["search"])
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@router.post("", response_model=SearchResponse)
|
|
16
|
+
def search(
|
|
17
|
+
req: SearchRequest,
|
|
18
|
+
c: HKCContainer = Depends(get_container),
|
|
19
|
+
):
|
|
20
|
+
"""
|
|
21
|
+
็ปไธๆ็ดขๆฅๅฃใ
|
|
22
|
+
mode: bm25 | vector | graph | hybrid๏ผ้ป่ฎค๏ผ
|
|
23
|
+
"""
|
|
24
|
+
if req.mode not in ("bm25", "vector", "graph", "hybrid"):
|
|
25
|
+
raise HTTPException(
|
|
26
|
+
status_code=400,
|
|
27
|
+
detail=f"ๆ ๆ mode: {req.mode}๏ผๆฏๆ bm25/vector/graph/hybrid"
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
results = c.search.search(
|
|
31
|
+
query = req.query,
|
|
32
|
+
mode = req.mode,
|
|
33
|
+
top_k = req.top_k,
|
|
34
|
+
domain = req.domain,
|
|
35
|
+
ku_types = req.ku_types,
|
|
36
|
+
)
|
|
37
|
+
return SearchResponse(
|
|
38
|
+
query = req.query,
|
|
39
|
+
mode = req.mode,
|
|
40
|
+
hits = [
|
|
41
|
+
SearchHit(
|
|
42
|
+
ku_id=r.ku_id, score=r.score, mode=r.mode,
|
|
43
|
+
name=r.name, summary=r.summary, ku_type=r.ku_type,
|
|
44
|
+
)
|
|
45
|
+
for r in results
|
|
46
|
+
],
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@router.post("/neighbors")
|
|
51
|
+
def search_neighbors(
|
|
52
|
+
req: NeighborsRequest,
|
|
53
|
+
c: HKCContainer = Depends(get_container),
|
|
54
|
+
):
|
|
55
|
+
"""ไปๆๅฎ KU ๅบๅๅๅพ่ฐฑ้ปๅฑ
ๅฑๅผใ"""
|
|
56
|
+
ku = c.graph_store.get(req.ku_id)
|
|
57
|
+
if not ku:
|
|
58
|
+
raise HTTPException(status_code=404, detail=f"KU ไธๅญๅจ: {req.ku_id}")
|
|
59
|
+
|
|
60
|
+
results = c.search.search_neighbors(
|
|
61
|
+
req.ku_id, max_depth=req.max_depth,
|
|
62
|
+
top_k=req.top_k, rel_types=req.rel_types,
|
|
63
|
+
)
|
|
64
|
+
return {
|
|
65
|
+
"ku_id": req.ku_id,
|
|
66
|
+
"hits": [
|
|
67
|
+
{"ku_id": r.ku_id, "score": r.score,
|
|
68
|
+
"name": r.name, "summary": r.summary}
|
|
69
|
+
for r in results
|
|
70
|
+
],
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@router.get("/path")
|
|
75
|
+
def find_path(
|
|
76
|
+
from_id: str,
|
|
77
|
+
to_id: str,
|
|
78
|
+
c: HKCContainer = Depends(get_container),
|
|
79
|
+
):
|
|
80
|
+
"""ๆฅๆพไธคไธช KU ไน้ด็ๆ็ญ่ทฏๅพใ"""
|
|
81
|
+
path = c.search.find_path(from_id, to_id)
|
|
82
|
+
return {
|
|
83
|
+
"from": from_id,
|
|
84
|
+
"to": to_id,
|
|
85
|
+
"path": path,
|
|
86
|
+
"reachable": len(path) > 0,
|
|
87
|
+
"hops": max(0, len(path) - 1),
|
|
88
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"""
|
|
2
|
+
hkc-api / schemas.py
|
|
3
|
+
API ่ฏทๆฑ / ๅๅบๆจกๅ๏ผPydantic๏ผใ
|
|
4
|
+
"""
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
from pydantic import BaseModel, Field
|
|
7
|
+
from typing import Optional, Any
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# โโ Ingest โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
11
|
+
|
|
12
|
+
class IngestTextRequest(BaseModel):
|
|
13
|
+
text: str
|
|
14
|
+
source: str = "inline"
|
|
15
|
+
source_title: str = ""
|
|
16
|
+
source_year: int = 0
|
|
17
|
+
domain: str = ""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class IngestURLRequest(BaseModel):
|
|
21
|
+
url: str
|
|
22
|
+
source_title: str = ""
|
|
23
|
+
source_year: int = 0
|
|
24
|
+
domain: str = ""
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class CrystallizeRequest(BaseModel):
|
|
28
|
+
"""ๅค้จ็ณป็ป็ป Crystallizer ้ๆๅฑๆจ้็็ฅ่ฏๅ้(่พน็่ฝฌๆขๅ
ฅๅฃ)ใ"""
|
|
29
|
+
content: str
|
|
30
|
+
title: str = ""
|
|
31
|
+
domain: str = ""
|
|
32
|
+
evidence_type: str = "document" # memory | meeting | research | document | reflection ...
|
|
33
|
+
source_id: str = ""
|
|
34
|
+
agent: str = ""
|
|
35
|
+
event_refs: list[str] = []
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class IngestResponse(BaseModel):
|
|
39
|
+
ku_count: int
|
|
40
|
+
ku_ids: list[str]
|
|
41
|
+
counts: dict[str, int] = Field(default_factory=dict)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# โโ KU โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
45
|
+
|
|
46
|
+
class KUResponse(BaseModel):
|
|
47
|
+
ku_id: str
|
|
48
|
+
ku_type: str
|
|
49
|
+
name: str
|
|
50
|
+
summary: str
|
|
51
|
+
domain: str
|
|
52
|
+
confidence: float
|
|
53
|
+
status: str
|
|
54
|
+
tags: list[str] = Field(default_factory=list)
|
|
55
|
+
extra: dict[str, Any] = Field(default_factory=dict)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class GraphResponse(BaseModel):
|
|
59
|
+
"""ๅ
จ้็ฅ่ฏๅพ่ฐฑ๏ผๆๆ KU + ๆๆๅ
ณ็ณป + ็ป่ฎกใๅ็ซฏๆ็ไธๆฌกๆๅ็จใ"""
|
|
60
|
+
kus: list[KUResponse]
|
|
61
|
+
relations: list[dict[str, Any]] = Field(default_factory=list)
|
|
62
|
+
stats: dict[str, Any] = Field(default_factory=dict)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
# โโ Search โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
66
|
+
|
|
67
|
+
class SearchRequest(BaseModel):
|
|
68
|
+
query: str
|
|
69
|
+
mode: str = "hybrid" # bm25 | vector | graph | hybrid
|
|
70
|
+
top_k: int = 10
|
|
71
|
+
domain: str = ""
|
|
72
|
+
ku_types: Optional[list[str]] = None
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class SearchHit(BaseModel):
|
|
76
|
+
ku_id: str
|
|
77
|
+
score: float
|
|
78
|
+
mode: str
|
|
79
|
+
name: str
|
|
80
|
+
summary: str
|
|
81
|
+
ku_type: str = ""
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class SearchResponse(BaseModel):
|
|
85
|
+
query: str
|
|
86
|
+
mode: str
|
|
87
|
+
hits: list[SearchHit]
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class NeighborsRequest(BaseModel):
|
|
91
|
+
ku_id: str
|
|
92
|
+
max_depth: int = 2
|
|
93
|
+
top_k: int = 20
|
|
94
|
+
rel_types: Optional[list[str]] = None
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
# โโ Ability โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
98
|
+
|
|
99
|
+
class AbilityCompileRequest(BaseModel):
|
|
100
|
+
ability_key: str
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class CoverageReportResponse(BaseModel):
|
|
104
|
+
ability_key: str
|
|
105
|
+
display_name: str = "" # ่ฝๅๆพ็คบๅ(ๅฏไธญๆ),ไพๅ็ซฏๅ่กจๅฑ็คบ
|
|
106
|
+
domains: list[str] = [] # ่ฏฅ่ฝๅๅไธ่ฆ็ๅน้
็้ขๅๅ่กจ
|
|
107
|
+
can_compile: bool
|
|
108
|
+
coverage: dict[str, float]
|
|
109
|
+
missing_skills: list[str]
|
|
110
|
+
ku_count: int
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class AbilityResponse(BaseModel):
|
|
114
|
+
ability_key: str
|
|
115
|
+
display_name: str
|
|
116
|
+
domain: str
|
|
117
|
+
coverage: dict[str, float]
|
|
118
|
+
skills: list[dict]
|
|
119
|
+
workflows: list[dict]
|
|
120
|
+
version: str
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
# โโ Conflict โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
124
|
+
|
|
125
|
+
class ConflictResponse(BaseModel):
|
|
126
|
+
conflict_id: str
|
|
127
|
+
claim_a_id: str
|
|
128
|
+
claim_b_id: str
|
|
129
|
+
domain: str
|
|
130
|
+
status: str
|
|
131
|
+
resolution_strategy: str
|
|
132
|
+
resolution_note: str
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
class ConflictResolveRequest(BaseModel):
|
|
136
|
+
winner_id: str
|
|
137
|
+
note: str = ""
|
|
138
|
+
resolved_by: str = "human"
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
# โโ Stats โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
142
|
+
|
|
143
|
+
class StatsResponse(BaseModel):
|
|
144
|
+
assembled: bool
|
|
145
|
+
data_dir: str = "" # ๅ็ซฏๅฎ้
ไฝฟ็จ็ๆฐๆฎ็ฎๅฝ(็ปๅฏน่ทฏๅพ),ไพๅ็ซฏๆพ็คบ/ๆๆฅ"่ฟ้็ฎๅฝ"
|
|
146
|
+
embedding: str = "" # ็ๆ็ๅ้ๅ็ซฏๅ(Stub ๆ ่ฏญไน / Local / TEI)
|
|
147
|
+
total_kus: int = 0
|
|
148
|
+
ku_counts: dict[str, int] = Field(default_factory=dict)
|
|
149
|
+
total_relations: int = 0
|
|
150
|
+
open_conflicts: int = 0
|
|
151
|
+
bm25_size: int = 0
|
|
152
|
+
vector_size: int = 0
|
|
153
|
+
abilities: list[str] = Field(default_factory=list)
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"""
|
|
2
|
+
hkc-api / synthesis.py
|
|
3
|
+
็ปผๅ้กตใ็ฌฌ 2 ๆกฃ ยท LLM ็ปผ่ฟฐใโโๆไธไธช่็นๅจๅพ้็้ปๅ๏ผ็จ LLM ็ป็ปๆ่ฟ่ดฏ็ไธญๆ่ฏๆกใ
|
|
4
|
+
|
|
5
|
+
โ
Principle 07 ่พน็๏ผๅกๅฟ
ๅฎไฝ๏ผ๏ผ
|
|
6
|
+
็ปผ่ฟฐๆฏ**ๆดพ็ๅช่ฏป่งๅพ**๏ผไธๆฏๆฐ็ฅ่ฏใๅฎโโ
|
|
7
|
+
- ไธๅปบ KUใไธๅ็ฅ่ฏๅพใไธ่ตฐ KEE๏ผ
|
|
8
|
+
- ็ผๅญๅญๅจ**็ฌ็ซ็ synthesis.db**๏ผ็ฉ็ไธไธ hkc.db ็ฅ่ฏๅบๅๅผ๏ผ่กจๆ"่ฟไธๆฏ็ฅ่ฏ"๏ผ๏ผ
|
|
9
|
+
- ๅชไพๆฎๅพ้ๅทฒๆ็็ฅ่ฏๆๆ้ๆฐ็ป็ป่กจ่พพ๏ผPrompt ๆ็กฎ็ฆๆญขๆๆฐ/ๅผๅ
ฅๅค้จ็ฅ่ฏใ
|
|
10
|
+
ๅฎไธ 3D ๆ็ไธๆ ท๏ผๅชๆฏ็ฅ่ฏๅพ็ไธ็งๆธฒๆ่งๅพใ
|
|
11
|
+
"""
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import hashlib
|
|
15
|
+
import sqlite3
|
|
16
|
+
import logging
|
|
17
|
+
import threading
|
|
18
|
+
from datetime import datetime, timezone
|
|
19
|
+
|
|
20
|
+
logger = logging.getLogger(__name__)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
_SYS_PROMPT = """ไฝ ๆฏไธฅ่ฐจ็็ฅ่ฏ็ผ่พใไธ้ข็ปไฝ ็ๆฏๆไธช็ฅ่ฏ่็นๅๅ
ถๅจ็ฅ่ฏๅบ้็ๅ
ณ่ๆๆใ
|
|
24
|
+
่ฏทๆๅฎไปฌ็ป็ปๆไธ็ฏ่ฟ่ดฏใๅ็กฎใๅฏ่ฏป็ไธญๆ wiki ๅผ่ฏๆกใ
|
|
25
|
+
|
|
26
|
+
ไธฅๆ ผ่งๅ๏ผ
|
|
27
|
+
- ๅชไพๆฎ็ปๅฎๆๆ๏ผ็ปไธๆๆฐใ็ปไธๅผๅ
ฅๆๆไนๅค็็ฅ่ฏ๏ผๆๆๆฒกๆๅฐ็ๅฐฑไธๅใ
|
|
28
|
+
- ่ฟๆฏๅฏน**ๅทฒๆ็ฅ่ฏ็้ๆฐ็ป็ป่กจ่พพ**๏ผไธๆฏๅ้ ๆฐ็ฅ่ฏ๏ผไนไธ่ฆไธไธๆๆ็ธๆ็็ป่ฎบใ
|
|
29
|
+
- ็จ็ฎ็ญๅฐๆ ้ข + ๆฎต่ฝ๏ผๅผๅคดไธๅฅ่ฏๅฎไน/ๆฆ่ฟฐ่ฏฅ่็น๏ผ็ถๅๅฑๅผๅ
ณ่ไบๅฎใ่ง็นไธ่ๆฏใ
|
|
30
|
+
- ่ฅๆๆ็จๅฐ๏ผๅฐฑๅฆๅฎๅ็ฎ็ญๅ ๅฅ๏ผไธ่ฆๅๅญๆฐใ
|
|
31
|
+
- ็ดๆฅ่พๅบ่ฏๆกๆญฃๆ๏ผไธ่ฆ่ฏด"ๆ นๆฎๆๆ"ไน็ฑป็ๅ
่ฏๆฏใ"""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class SynthesisStore:
|
|
35
|
+
"""็ปผ่ฟฐ็ผๅญ๏ผ็ฌ็ซ SQLite๏ผไธ็ฅ่ฏๅบ็ฉ็้็ฆป๏ผใ"""
|
|
36
|
+
|
|
37
|
+
def __init__(self, path: str):
|
|
38
|
+
self.path = path
|
|
39
|
+
# check_same_thread=False + ๆพๅผ้๏ผFastAPI ๅๆญฅ่ทฏ็ฑ่ทๅจ็บฟ็จๆฑ ้๏ผ
|
|
40
|
+
# ๅนถๅ่ฏทๆฑไผไปๅคไธช็บฟ็จๅ
ฑ็จ่ฟไธไธช่ฟๆฅ๏ผไธๅ ้ไผ "recursive use of cursors"/
|
|
41
|
+
# "database is locked"ใไธ SQLiteGraphStore ็ๆขๆๆจกๅผไฟๆไธ่ดใ
|
|
42
|
+
self._lock = threading.Lock()
|
|
43
|
+
self._db = sqlite3.connect(path, check_same_thread=False)
|
|
44
|
+
with self._lock:
|
|
45
|
+
self._db.execute("""
|
|
46
|
+
CREATE TABLE IF NOT EXISTS synthesis (
|
|
47
|
+
ku_id TEXT PRIMARY KEY,
|
|
48
|
+
content TEXT NOT NULL,
|
|
49
|
+
model TEXT,
|
|
50
|
+
generated_at TEXT,
|
|
51
|
+
context_hash TEXT
|
|
52
|
+
)
|
|
53
|
+
""")
|
|
54
|
+
self._db.commit()
|
|
55
|
+
|
|
56
|
+
def get(self, ku_id: str) -> dict | None:
|
|
57
|
+
with self._lock:
|
|
58
|
+
row = self._db.execute(
|
|
59
|
+
"SELECT ku_id, content, model, generated_at, context_hash "
|
|
60
|
+
"FROM synthesis WHERE ku_id=?", (ku_id,)
|
|
61
|
+
).fetchone()
|
|
62
|
+
if not row:
|
|
63
|
+
return None
|
|
64
|
+
return {"ku_id": row[0], "content": row[1], "model": row[2],
|
|
65
|
+
"generated_at": row[3], "context_hash": row[4]}
|
|
66
|
+
|
|
67
|
+
def put(self, ku_id: str, content: str, model: str, context_hash: str):
|
|
68
|
+
with self._lock:
|
|
69
|
+
self._db.execute(
|
|
70
|
+
"INSERT INTO synthesis(ku_id, content, model, generated_at, context_hash) "
|
|
71
|
+
"VALUES(?,?,?,?,?) ON CONFLICT(ku_id) DO UPDATE SET "
|
|
72
|
+
"content=excluded.content, model=excluded.model, "
|
|
73
|
+
"generated_at=excluded.generated_at, context_hash=excluded.context_hash",
|
|
74
|
+
(ku_id, content, model,
|
|
75
|
+
datetime.now(timezone.utc).isoformat(), context_hash),
|
|
76
|
+
)
|
|
77
|
+
self._db.commit()
|
|
78
|
+
|
|
79
|
+
def delete(self, ku_id: str):
|
|
80
|
+
with self._lock:
|
|
81
|
+
self._db.execute("DELETE FROM synthesis WHERE ku_id=?", (ku_id,))
|
|
82
|
+
self._db.commit()
|
|
83
|
+
|
|
84
|
+
def close(self):
|
|
85
|
+
with self._lock:
|
|
86
|
+
self._db.close()
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class SynthesisService:
|
|
90
|
+
"""ๆ้็ๆ + ็ผๅญ่็น็ปผ่ฟฐใ็ๆ่ตฐๅทฒ้
็ฝฎ็ไปปๆ LLM๏ผๆจกๅๆ ๅ
ณ๏ผใ"""
|
|
91
|
+
|
|
92
|
+
def __init__(self, graph_store, search, kde, cache_path: str):
|
|
93
|
+
self.store = graph_store
|
|
94
|
+
self.search = search
|
|
95
|
+
self.kde = kde # ๅจๆๅ kde.extractor,ๅฐ้่ฟ่กๆถๆข/้้
LLM
|
|
96
|
+
self.cache = SynthesisStore(cache_path)
|
|
97
|
+
|
|
98
|
+
@property
|
|
99
|
+
def extractor(self):
|
|
100
|
+
return self.kde.extractor
|
|
101
|
+
|
|
102
|
+
# โโ ้ปๅๆๆๆถ้๏ผๅค็จๅพ้ปๅฑ
+ ๆฃ็ดข"ๆๅ"๏ผๅๆ็๏ผโโโโโโโโโโ
|
|
103
|
+
def _gather_context(self, ku) -> tuple[str, str]:
|
|
104
|
+
"""่ฟๅ (ๆผๅฅฝ็ๆๆๆๆฌ, ๆๆๆ็บน)ใๆๆๅช่ฏป๏ผไธๆนๅจไปปไฝ็ฅ่ฏใ"""
|
|
105
|
+
ex = ku.to_dict().get("extra", {})
|
|
106
|
+
lines: list[str] = []
|
|
107
|
+
head = f"่็น๏ผ{ku.name}๏ผ{ku.ku_type.value}๏ผ"
|
|
108
|
+
if ku.domain:
|
|
109
|
+
head += f"ใ้ขๅ๏ผ{ku.domain}"
|
|
110
|
+
lines.append(head)
|
|
111
|
+
stmt = ex.get("statement")
|
|
112
|
+
if stmt:
|
|
113
|
+
lines.append(f"้่ฟฐ๏ผ{stmt}")
|
|
114
|
+
if ku.summary and ku.summary != stmt:
|
|
115
|
+
lines.append(f"ๆฆ่ฟฐ๏ผ{ku.summary}")
|
|
116
|
+
src = (ex.get("source_text") or "").strip()
|
|
117
|
+
if src:
|
|
118
|
+
lines.append(f"ๅๆ็ๆฎต๏ผ{src[:800]}")
|
|
119
|
+
|
|
120
|
+
seen = {ku.ku_id}
|
|
121
|
+
related: list[str] = []
|
|
122
|
+
# 1) ๅพ่ฐฑ้ปๅฑ
๏ผๆ็ดๆฅๅ
ณ็ณป็่็น๏ผ
|
|
123
|
+
try:
|
|
124
|
+
for h in self.search.search_neighbors(ku.ku_id, max_depth=2, top_k=15):
|
|
125
|
+
if h.ku_id in seen:
|
|
126
|
+
continue
|
|
127
|
+
seen.add(h.ku_id)
|
|
128
|
+
related.append(f"- {h.name}๏ผ{(h.summary or '').strip()[:160]}")
|
|
129
|
+
except Exception as e:
|
|
130
|
+
logger.warning("็ปผ่ฟฐ้ปๅฑ
ๆถ้ๅคฑ่ดฅ %s: %s", ku.ku_id, e)
|
|
131
|
+
# 2) ๆฃ็ดข"ๆๅๆฌ่็นๅๅญ"็ไบๅฎ/่ง็น๏ผๅพ้ๅธธๅช่ฟๅฐไนฆใไธ็ดๆฅ่ฟๅฎไฝ๏ผ
|
|
132
|
+
try:
|
|
133
|
+
for h in self.search.search(ku.name, top_k=12):
|
|
134
|
+
if h.ku_id in seen:
|
|
135
|
+
continue
|
|
136
|
+
seen.add(h.ku_id)
|
|
137
|
+
related.append(f"- {h.name}๏ผ{(h.summary or '').strip()[:160]}")
|
|
138
|
+
except Exception as e:
|
|
139
|
+
logger.warning("็ปผ่ฟฐๆฃ็ดขๆถ้ๅคฑ่ดฅ %s: %s", ku.ku_id, e)
|
|
140
|
+
|
|
141
|
+
if related:
|
|
142
|
+
lines.append("\nๅ
ณ่ๆๆ๏ผ")
|
|
143
|
+
lines.extend(related[:24]) # ไธ็๏ผๆง prompt ไฝ็งฏ
|
|
144
|
+
|
|
145
|
+
text = "\n".join(lines)
|
|
146
|
+
digest = hashlib.sha1(text.encode("utf-8")).hexdigest()
|
|
147
|
+
return text, digest
|
|
148
|
+
|
|
149
|
+
def get_cached(self, ku_id: str) -> dict | None:
|
|
150
|
+
return self.cache.get(ku_id)
|
|
151
|
+
|
|
152
|
+
def generate(self, ku_id: str, force: bool = False) -> dict:
|
|
153
|
+
"""
|
|
154
|
+
่ฟๅ {ku_id, content, model, generated_at, cached, stale}ใ
|
|
155
|
+
- ๆ็ผๅญไธ้ force๏ผ็ดๆฅ่ฟๅ็ผๅญ๏ผstale ๆ ่ฎฐๆๆๆฏๅฆๅทฒๅ๏ผไพๅ็ซฏๆ็คบ"ๅฏ้ๆฐ็ๆ"๏ผใ
|
|
156
|
+
- force ๆๆ ็ผๅญ๏ผ่ฐ LLM ็ๆๅนถๅ็ผๅญใ
|
|
157
|
+
LLM ๆช้
็ฝฎ/่ฐ็จๅคฑ่ดฅไผๆๅผๅธธ๏ผ็ฑ่ทฏ็ฑ่ฝฌๆๆ็กฎ้่ฏฏใ
|
|
158
|
+
"""
|
|
159
|
+
ku = self.store.get(ku_id)
|
|
160
|
+
if not ku:
|
|
161
|
+
raise KeyError(ku_id)
|
|
162
|
+
|
|
163
|
+
context, digest = self._gather_context(ku)
|
|
164
|
+
cached = self.cache.get(ku_id)
|
|
165
|
+
if cached and not force:
|
|
166
|
+
cached["cached"] = True
|
|
167
|
+
cached["stale"] = (cached.get("context_hash") != digest)
|
|
168
|
+
return cached
|
|
169
|
+
|
|
170
|
+
content = self.extractor.complete(
|
|
171
|
+
system=_SYS_PROMPT,
|
|
172
|
+
user=context,
|
|
173
|
+
max_tokens=1500,
|
|
174
|
+
)
|
|
175
|
+
model = getattr(self.extractor, "model", "")
|
|
176
|
+
self.cache.put(ku_id, content, model, digest)
|
|
177
|
+
return {"ku_id": ku_id, "content": content, "model": model,
|
|
178
|
+
"generated_at": datetime.now(timezone.utc).isoformat(),
|
|
179
|
+
"context_hash": digest, "cached": False, "stale": False}
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"""
|
|
2
|
+
hkc-core / graph / base.py
|
|
3
|
+
GraphStore ๆฝ่ฑกๆฅๅฃใ
|
|
4
|
+
v1 ๅฎ็ฐ๏ผSQLiteGraphStore
|
|
5
|
+
v2 ๅฏๆฟๆข๏ผNeo4jGraphStore
|
|
6
|
+
ไธๅฑไปฃ็ ๅชไพ่ต่ฟไธชๆฅๅฃ๏ผๅบๅฑๅฎ็ฐ้ๆถๅฏๆขใ
|
|
7
|
+
"""
|
|
8
|
+
from abc import ABC, abstractmethod
|
|
9
|
+
from typing import Optional
|
|
10
|
+
from ..models.ku import KU, Relation, ConflictCard, ClaimKU
|
|
11
|
+
from ..models.enums import KUType, ClaimStatus
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class GraphStore(ABC):
|
|
15
|
+
|
|
16
|
+
# โโ ๅไธช KU โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
17
|
+
|
|
18
|
+
@abstractmethod
|
|
19
|
+
def get(self, ku_id: str) -> Optional[KU]:
|
|
20
|
+
"""ๆ ID ่ทๅ KU๏ผไธๅญๅจ่ฟๅ Noneใ"""
|
|
21
|
+
...
|
|
22
|
+
|
|
23
|
+
@abstractmethod
|
|
24
|
+
def put(self, ku: KU) -> None:
|
|
25
|
+
"""ๅๅ
ฅๆๆดๆฐๅไธช KUใ"""
|
|
26
|
+
...
|
|
27
|
+
|
|
28
|
+
@abstractmethod
|
|
29
|
+
def delete(self, ku_id: str) -> None:
|
|
30
|
+
"""่ฝฏๅ ้ค๏ผๆ status ๆนไธบ 'deleted'ใ"""
|
|
31
|
+
...
|
|
32
|
+
|
|
33
|
+
# โโ ๅฑ้จๅๅ
ฅ๏ผ้ป่ฎคๅฎ็ฐ้ๅไธบๆด่ก put๏ผโโโโโโโโโ
|
|
34
|
+
#
|
|
35
|
+
# put() ๆฏๆด่กๅใๅฝไธคไธชๅญ็ณป็ปๅ่ชๅชๅ
ณๅฟ KU ็ไธๅ้จๅๆถ(ๅคไน ็ถๆๅจ
|
|
36
|
+
# extra["review"],KEE ๆผๅๆน sources / confidence / supports),ๆด่ก่ฏปๆนๅไผ
|
|
37
|
+
# ไบ็ธ่ฆ็ โโ ่ฐๅๅ่ฐ่ตข,ๅไธไธช็ๆนๅจ้้ปไธขๅคฑใๅญๅจๅฎ็ฐๅบ่ฆ็่ฟไธคไธชๆนๆณ,
|
|
38
|
+
# ๅชๅๅฑไบ่ชๅทฑ็้ฃๅ ๅ/้ฃๅ ไธช้ฎใ
|
|
39
|
+
|
|
40
|
+
def patch_ku(
|
|
41
|
+
self,
|
|
42
|
+
ku_id: str,
|
|
43
|
+
columns: Optional[dict] = None,
|
|
44
|
+
extra: Optional[dict] = None,
|
|
45
|
+
) -> int:
|
|
46
|
+
"""ๅชๅๆๅฎๅ + ๅๅนถ extra ็ๆๅฎ้ฎใ่ฟๅๅๅฝฑๅ่กๆฐ(0 = KU ไธๅญๅจ)ใ
|
|
47
|
+
้ป่ฎคๅฎ็ฐ:get โ ๆน โ put(็ญไปท็ๆด่กๅ,ไธ่งฃๅณ่ฆ็้ฎ้ข,ไป
ไฟ่ฏๆฅๅฃๅฏ็จ)ใ"""
|
|
48
|
+
ku = self.get(ku_id)
|
|
49
|
+
if ku is None:
|
|
50
|
+
return 0
|
|
51
|
+
for k, v in (columns or {}).items():
|
|
52
|
+
setattr(ku, k, v)
|
|
53
|
+
if extra:
|
|
54
|
+
ku.extra = {**(ku.extra or {}), **extra}
|
|
55
|
+
self.put(ku)
|
|
56
|
+
return 1
|
|
57
|
+
|
|
58
|
+
def update_extra(self, ku_id: str, fn) -> Optional[dict]:
|
|
59
|
+
"""้ๅ
ใ่ฏป extra โ fn(extra) โ ๅๅใใ่ฟๅๅๅ็ extra;None = KU ไธๅญๅจใ
|
|
60
|
+
้ป่ฎคๅฎ็ฐไธๅ ้,ไป
ไฟ่ฏๆฅๅฃๅฏ็จใ"""
|
|
61
|
+
ku = self.get(ku_id)
|
|
62
|
+
if ku is None:
|
|
63
|
+
return None
|
|
64
|
+
new_extra = fn({**(ku.extra or {})})
|
|
65
|
+
if new_extra is None:
|
|
66
|
+
return ku.extra
|
|
67
|
+
ku.extra = new_extra
|
|
68
|
+
self.put(ku)
|
|
69
|
+
return new_extra
|
|
70
|
+
|
|
71
|
+
# โโ ๆน้ๆไฝ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
72
|
+
|
|
73
|
+
@abstractmethod
|
|
74
|
+
def batch_write(self, kus: list[KU]) -> None:
|
|
75
|
+
"""ๆน้ๅๅ
ฅ๏ผๅๅญๆไฝใ"""
|
|
76
|
+
...
|
|
77
|
+
|
|
78
|
+
# โโ ๆฅ่ฏข โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
79
|
+
|
|
80
|
+
@abstractmethod
|
|
81
|
+
def query_by_type(
|
|
82
|
+
self,
|
|
83
|
+
ku_type: KUType,
|
|
84
|
+
domain: str = "",
|
|
85
|
+
status: str = "active",
|
|
86
|
+
limit: int = 100,
|
|
87
|
+
) -> list[KU]:
|
|
88
|
+
...
|
|
89
|
+
|
|
90
|
+
@abstractmethod
|
|
91
|
+
def query_by_domain(self, domain: str, limit: int = 200) -> list[KU]:
|
|
92
|
+
...
|
|
93
|
+
|
|
94
|
+
@abstractmethod
|
|
95
|
+
def search_by_name(self, name: str, fuzzy: bool = True) -> list[KU]:
|
|
96
|
+
...
|
|
97
|
+
|
|
98
|
+
# โโ ๅ
ณ็ณป โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
99
|
+
|
|
100
|
+
@abstractmethod
|
|
101
|
+
def add_relation(self, rel: Relation) -> None:
|
|
102
|
+
...
|
|
103
|
+
|
|
104
|
+
@abstractmethod
|
|
105
|
+
def delete_relation(self, rel_id: str) -> None:
|
|
106
|
+
"""ๅ ้คไธๆกๅ
ณ็ณป(SQLite ่กจ + ๅพ็ปๆๅๆญฅ็งป้ค)ใ"""
|
|
107
|
+
...
|
|
108
|
+
|
|
109
|
+
@abstractmethod
|
|
110
|
+
def redirect_relations(self, from_ku_id: str, to_ku_id: str) -> int:
|
|
111
|
+
"""
|
|
112
|
+
ๆๆๆๆๅ/ๆฅ่ช from_ku_id ็ๅ
ณ็ณป,้ๅฎๅๅฐ to_ku_idใ
|
|
113
|
+
็จไบ็ฅ่ฏ็บงๅป้:้ๅค KU ่ขซๅๅนถๅ
ฅ canonical KU ๅ,
|
|
114
|
+
ๅ
ถๅ
ณ็ณปไธๅบๆฌ็ฉบ,่ๅบ่ฝฌ็งปๅฐ canonical KU ไธใ
|
|
115
|
+
่ฟๅ้ๅฎๅ็ๅ
ณ็ณปๆฐใ่ช็ฏ(้ๅฎๅๅ from==to)ไผ่ขซไธขๅผใ
|
|
116
|
+
"""
|
|
117
|
+
...
|
|
118
|
+
|
|
119
|
+
@abstractmethod
|
|
120
|
+
def get_relations(
|
|
121
|
+
self,
|
|
122
|
+
ku_id: str,
|
|
123
|
+
rel_type: str = "",
|
|
124
|
+
direction: str = "both", # "out" | "in" | "both"
|
|
125
|
+
) -> list[Relation]:
|
|
126
|
+
...
|
|
127
|
+
|
|
128
|
+
@abstractmethod
|
|
129
|
+
def get_all_relations(self, limit: int = 5000) -> list[Relation]:
|
|
130
|
+
"""ๅ
จ้ๆๅๆๆๅ
ณ็ณป๏ผ็จไบไธๆฌกๆงๆๅปบๅ
จๅพ๏ผๅฆๅ็ซฏๆ็๏ผใ"""
|
|
131
|
+
...
|
|
132
|
+
|
|
133
|
+
@abstractmethod
|
|
134
|
+
def neighbors(
|
|
135
|
+
self,
|
|
136
|
+
ku_id: str,
|
|
137
|
+
rel_type: str = "",
|
|
138
|
+
depth: int = 1,
|
|
139
|
+
) -> list[KU]:
|
|
140
|
+
...
|
|
141
|
+
|
|
142
|
+
@abstractmethod
|
|
143
|
+
def shortest_path(self, from_id: str, to_id: str) -> list[str]:
|
|
144
|
+
"""่ฟๅ KU ID ่ทฏๅพๅ่กจ๏ผไธๅฏ่พพ่ฟๅ็ฉบๅ่กจใ"""
|
|
145
|
+
...
|
|
146
|
+
|
|
147
|
+
# โโ Conflict Cards โโโโโโโโโโโโโโโโโโโโโโโโ
|
|
148
|
+
|
|
149
|
+
@abstractmethod
|
|
150
|
+
def save_conflict(self, card: ConflictCard) -> None:
|
|
151
|
+
...
|
|
152
|
+
|
|
153
|
+
@abstractmethod
|
|
154
|
+
def get_conflict(self, conflict_id: str) -> Optional[ConflictCard]:
|
|
155
|
+
...
|
|
156
|
+
|
|
157
|
+
@abstractmethod
|
|
158
|
+
def list_conflicts(
|
|
159
|
+
self,
|
|
160
|
+
status: str = "open",
|
|
161
|
+
domain: str = "",
|
|
162
|
+
) -> list[ConflictCard]:
|
|
163
|
+
...
|
|
164
|
+
|
|
165
|
+
# โโ Claim ไธ็จ โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
166
|
+
|
|
167
|
+
def query_all(self, limit: int = 2000) -> list[KU]:
|
|
168
|
+
"""ๅ
จ้ๆฅ่ฏขๆๆ้ๅ ้ค KU๏ผ้ป่ฎคๅฎ็ฐ๏ผ้ๅๆๆ็ฑปๅใ"""
|
|
169
|
+
from ..models.enums import KUType
|
|
170
|
+
result = []
|
|
171
|
+
for ku_type in KUType:
|
|
172
|
+
result.extend(self.query_by_type(ku_type, status='active', limit=limit))
|
|
173
|
+
return result
|
|
174
|
+
|
|
175
|
+
@abstractmethod
|
|
176
|
+
def find_claims_by_domain(
|
|
177
|
+
self,
|
|
178
|
+
domain: str,
|
|
179
|
+
status: ClaimStatus = None,
|
|
180
|
+
) -> list[ClaimKU]:
|
|
181
|
+
...
|
|
182
|
+
|
|
183
|
+
# โโ ็ป่ฎก โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
184
|
+
|
|
185
|
+
@abstractmethod
|
|
186
|
+
def stats(self) -> dict:
|
|
187
|
+
"""่ฟๅๅ็ฑปๅ KU ๆฐ้ใๅ
ณ็ณปๆฐ้็ญๅบ็ก็ป่ฎกใ"""
|
|
188
|
+
...
|