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,575 @@
|
|
|
1
|
+
"""自然语言记账解析 —— 把「今天买了一件衣服花了 105 块」变成一笔结构化交易。
|
|
2
|
+
|
|
3
|
+
财务应用的第一线输入几乎全是这种大白话:没有 CSV、没有表单,用户只是随口报备一句。
|
|
4
|
+
这里用**确定性规则**(正则 + 中文数字 + 关键词表)解析,不让 LLM 猜金额 —— 记错一笔钱
|
|
5
|
+
比慢 100 毫秒严重得多,而且金额出错会顺着预算/风控/预测一路错下去。
|
|
6
|
+
|
|
7
|
+
两个刻意的顺序:
|
|
8
|
+
|
|
9
|
+
1. **先剥时间,再找金额**。否则「上个月 5 号买衣服 100」里的 5 会被当成金额。
|
|
10
|
+
2. **先认带货币单位的数字,再退回动词旁的裸数字**。否则「买了 2 件衣服 105」会记成 2 块。
|
|
11
|
+
|
|
12
|
+
时态:
|
|
13
|
+
- 今天/昨天/前天/N 号/上个月 … → 已发生(settled)
|
|
14
|
+
- 明天/后天/大后天/下个月 … → 计划支出(planned)。用户说「明天交房租 3000」是希望
|
|
15
|
+
被记住,不是希望被忽略,但它绝不能算进「本月已花」。
|
|
16
|
+
|
|
17
|
+
币种:「105 块 / 105 元 / 105 人民币 / 105 RMB」→ CNY,「105 美元 / 105 美金 / $105」
|
|
18
|
+
→ USD,另有港币/欧元/日元/英镑等。识别出的原币金额与币种都保留,折算成本位币的
|
|
19
|
+
汇率由 `fx.py` 提供(静态参考价,可改),不联网也不假装实时。
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
import re
|
|
23
|
+
from dataclasses import dataclass, field
|
|
24
|
+
from datetime import datetime, timedelta
|
|
25
|
+
from typing import Optional
|
|
26
|
+
|
|
27
|
+
# ── 币种 ──────────────────────────────────────────────────
|
|
28
|
+
# 别名按长度倒序匹配:"块钱" 必须先于 "块","人民币" 先于 "元",否则会切错。
|
|
29
|
+
CURRENCY_ALIASES = {
|
|
30
|
+
"人民币": "CNY", "块钱": "CNY", "块": "CNY", "元": "CNY", "圆": "CNY",
|
|
31
|
+
"软妹币": "CNY", "rmb": "CNY", "cny": "CNY",
|
|
32
|
+
"美元": "USD", "美金": "USD", "美刀": "USD", "刀": "USD", "usd": "USD",
|
|
33
|
+
"港元": "HKD", "港币": "HKD", "hkd": "HKD",
|
|
34
|
+
"欧元": "EUR", "eur": "EUR",
|
|
35
|
+
"日元": "JPY", "日币": "JPY", "jpy": "JPY",
|
|
36
|
+
"英镑": "GBP", "gbp": "GBP",
|
|
37
|
+
"韩元": "KRW", "krw": "KRW",
|
|
38
|
+
"新币": "SGD", "新加坡元": "SGD", "sgd": "SGD",
|
|
39
|
+
"澳元": "AUD", "aud": "AUD",
|
|
40
|
+
"加元": "CAD", "cad": "CAD",
|
|
41
|
+
}
|
|
42
|
+
_SYMBOLS = {"¥": "CNY", "¥": "CNY", "$": "USD", "€": "EUR", "£": "GBP", "₩": "KRW"}
|
|
43
|
+
_UNIT_ALT = "|".join(re.escape(u) for u in
|
|
44
|
+
sorted(CURRENCY_ALIASES, key=len, reverse=True))
|
|
45
|
+
|
|
46
|
+
# 倍数后缀:1.5k / 2w / 3万 —— 不是币种,只是量级
|
|
47
|
+
_SCALE = {"k": 1000, "千": 1000, "w": 10000, "万": 10000}
|
|
48
|
+
|
|
49
|
+
# ── 中文数字 ──────────────────────────────────────────────
|
|
50
|
+
_CN_DIGITS = {"零": 0, "〇": 0, "一": 1, "二": 2, "两": 2, "三": 3, "四": 4,
|
|
51
|
+
"五": 5, "六": 6, "七": 7, "八": 8, "九": 9}
|
|
52
|
+
_CN_UNITS = {"十": 10, "百": 100, "千": 1000, "万": 10000, "亿": 100000000}
|
|
53
|
+
_CN_NUM = "[零〇一二两三四五六七八九十百千万亿]{1,12}"
|
|
54
|
+
|
|
55
|
+
# ── 量词:数字 + 量词是"几件/2 袋",不是金额,必须先剔除 ─────────
|
|
56
|
+
# 注意「块」不在此表 —— 它是货币单位(105 块),不是量词。
|
|
57
|
+
# 「人」「位」故意不收:会吃掉「105 人民币」的「人」。
|
|
58
|
+
_QUANTIFIERS = ("件|个|只|双|条|包|袋|瓶|杯|斤|公斤|kg|克|张|台|部|本|支|盒|箱|"
|
|
59
|
+
"次|份|碗|串|根|桶|套|顿|晚|小时|分钟|公里|千米|米|升|码|岁|ml")
|
|
60
|
+
_QUANT_PAT = re.compile(rf"(?:\d[\d,.]*|{_CN_NUM})\s*(?:{_QUANTIFIERS})", re.I)
|
|
61
|
+
|
|
62
|
+
# ── 花费/收入动词 ────────────────────────────────────────
|
|
63
|
+
_EXPENSE_VERBS = ("花了", "花费", "花", "付了", "付款", "支付", "买了", "买", "购",
|
|
64
|
+
"消费", "充值", "充了", "交", "缴", "扣", "订", "租", "打车", "吃")
|
|
65
|
+
_INCOME_VERBS = ("收入", "工资", "发了", "发工资", "收到", "收款", "进账", "退款",
|
|
66
|
+
"退回", "报销到账", "报销了", "赚", "奖金", "红包", "分红", "利息", "中奖")
|
|
67
|
+
|
|
68
|
+
_VERB_PAT = re.compile("|".join(sorted(_EXPENSE_VERBS, key=len, reverse=True)))
|
|
69
|
+
_INCOME_PAT = re.compile("|".join(sorted(_INCOME_VERBS, key=len, reverse=True)))
|
|
70
|
+
|
|
71
|
+
# ── 分类关键词(顺序即优先级:订阅要先于娱乐,否则「腾讯视频会员」会归到娱乐)──
|
|
72
|
+
CATEGORY_LABELS = {
|
|
73
|
+
"food": "餐饮食品", "clothing": "服饰", "transport": "交通出行",
|
|
74
|
+
"housing": "居住", "subscription": "订阅服务", "entertainment": "娱乐",
|
|
75
|
+
"medical": "医疗健康", "education": "教育", "digital": "数码家电",
|
|
76
|
+
"pet": "宠物", "travel": "旅行", "income": "收入", "misc": "其他",
|
|
77
|
+
}
|
|
78
|
+
_CATEGORY_RULES = [
|
|
79
|
+
("subscription", ["会员", "订阅", "vip", "netflix", "网飞", "腾讯视频",
|
|
80
|
+
"爱奇艺", "bilibili", "b站大会员", "spotify", "icloud", "wps会员"]),
|
|
81
|
+
("food", ["大米", "米面", "面粉", "菜", "肉", "排骨", "水果", "奶茶", "咖啡",
|
|
82
|
+
"饭", "早餐", "午餐", "晚餐", "夜宵", "外卖", "零食", "超市", "牛奶",
|
|
83
|
+
"鸡蛋", "面包", "饮料", "火锅", "烧烤", "酒", "水饺", "面条", "油盐",
|
|
84
|
+
# 常见餐饮商户:用户报备时往往只说店名
|
|
85
|
+
"星巴克", "瑞幸", "麦当劳", "肯德基", "海底捞", "喜茶", "蜜雪", "食堂"]),
|
|
86
|
+
("clothing", ["衣服", "裤子", "鞋", "外套", "裙子", "衬衫", "t恤", "帽子",
|
|
87
|
+
"包", "袜子", "内衣", "羽绒服", "西装", "围巾", "手套", "牛仔裤"]),
|
|
88
|
+
("transport", ["洗车", "加油", "油费", "打车", "滴滴", "出租车", "公交", "地铁",
|
|
89
|
+
"高铁", "火车", "机票", "停车", "过路费", "保养", "修车", "充电桩"]),
|
|
90
|
+
("housing", ["房租", "水电", "电费", "水费", "物业", "燃气", "煤气", "暖气", "宽带"]),
|
|
91
|
+
("medical", ["药", "医院", "挂号", "体检", "牙", "看病", "门诊", "住院"]),
|
|
92
|
+
("education", ["书", "课程", "学费", "培训", "网课", "考试", "文具", "教材", "报班"]),
|
|
93
|
+
("digital", ["手机", "电脑", "耳机", "充电器", "数据线", "硬盘", "显示器", "键盘", "鼠标"]),
|
|
94
|
+
("pet", ["猫粮", "狗粮", "宠物", "猫砂", "疫苗", "猫", "狗"]),
|
|
95
|
+
("travel", ["旅游", "景区", "门票", "酒店", "民宿", "度假", "签证"]),
|
|
96
|
+
("entertainment", ["电影", "游戏", "ktv", "演出", "演唱会", "剧本杀", "健身", "按摩"]),
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
# ── 时间表达 ─────────────────────────────────────────────
|
|
100
|
+
_DAY_WORDS = [ # 长的先匹配,避免「大后天」被「后天」吃掉
|
|
101
|
+
("大后天", 3), ("大前天", -3), ("后天", 2), ("前天", -2),
|
|
102
|
+
("昨天", -1), ("昨日", -1), ("昨晚", -1), ("昨儿", -1),
|
|
103
|
+
("今天", 0), ("今日", 0), ("今儿", 0), ("刚刚", 0), ("刚才", 0),
|
|
104
|
+
("明天", 1), ("明日", 1), ("明儿", 1),
|
|
105
|
+
]
|
|
106
|
+
_MONTH_WORDS = [ # (词, 月偏移)
|
|
107
|
+
("上上个月", -2), ("上上月", -2), ("上个月", -1), ("上一个月", -1),
|
|
108
|
+
("上月", -1), ("下下个月", 2), ("下下月", 2), ("下个月", 1),
|
|
109
|
+
("下一个月", 1), ("下月", 1), ("这个月", 0), ("本月", 0), ("这月", 0),
|
|
110
|
+
]
|
|
111
|
+
_MONTH_ALT = "|".join(re.escape(w) for w, _ in
|
|
112
|
+
sorted(_MONTH_WORDS, key=lambda x: -len(x[0])))
|
|
113
|
+
_WEEK_WORDS = [("上周", -7), ("上星期", -7), ("上个星期", -7),
|
|
114
|
+
("这周", 0), ("本周", 0), ("这星期", 0),
|
|
115
|
+
("下周", 7), ("下星期", 7), ("下个星期", 7)]
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
# ══════════════════════════════════════════════════════════
|
|
119
|
+
# 解析结果
|
|
120
|
+
# ══════════════════════════════════════════════════════════
|
|
121
|
+
@dataclass
|
|
122
|
+
class ParsedExpense:
|
|
123
|
+
"""一句话里解析出的一笔钱(还没落账)。"""
|
|
124
|
+
text: str # 原始片段
|
|
125
|
+
direction: str = "expense" # expense / income
|
|
126
|
+
amount: float = 0.0 # 原币金额(正数)
|
|
127
|
+
currency: str = "CNY"
|
|
128
|
+
when: datetime = field(default_factory=datetime.now)
|
|
129
|
+
category: str = "misc"
|
|
130
|
+
description: str = "" # 买了什么
|
|
131
|
+
entity: str = "" # 商户/物品(账本里按它归类)
|
|
132
|
+
account: str = "default"
|
|
133
|
+
confidence: float = 0.5
|
|
134
|
+
warnings: list = field(default_factory=list)
|
|
135
|
+
planned: bool = False # 未来时间 → 计划支出,不进"已发生"汇总
|
|
136
|
+
|
|
137
|
+
@property
|
|
138
|
+
def is_planned(self) -> bool:
|
|
139
|
+
return self.planned
|
|
140
|
+
|
|
141
|
+
def to_dict(self) -> dict:
|
|
142
|
+
return {
|
|
143
|
+
"text": self.text, "direction": self.direction,
|
|
144
|
+
"amount": round(self.amount, 2), "currency": self.currency,
|
|
145
|
+
"when": self.when.isoformat(timespec="seconds"),
|
|
146
|
+
"is_planned": self.is_planned, "category": self.category,
|
|
147
|
+
"category_label": CATEGORY_LABELS.get(self.category, self.category),
|
|
148
|
+
"description": self.description, "entity": self.entity,
|
|
149
|
+
"account": self.account, "confidence": round(self.confidence, 2),
|
|
150
|
+
"warnings": self.warnings,
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
# ══════════════════════════════════════════════════════════
|
|
155
|
+
# 中文数字
|
|
156
|
+
# ══════════════════════════════════════════════════════════
|
|
157
|
+
def cn_to_number(s: str) -> Optional[float]:
|
|
158
|
+
"""「一百零五」→105、「两千三」→2300、「十万」→100000。解析不了返回 None。
|
|
159
|
+
|
|
160
|
+
口语省略规则必须处理,否则「两千三」会被读成 2003:
|
|
161
|
+
末尾的裸数字紧跟在单位后 = 下一级单位(千→百、百→十),而「零」之后的裸数字 = 个位。
|
|
162
|
+
"""
|
|
163
|
+
if not s:
|
|
164
|
+
return None
|
|
165
|
+
if s.isdigit():
|
|
166
|
+
return float(s)
|
|
167
|
+
total, section, num, last_unit, zero = 0, 0, 0, 0, False
|
|
168
|
+
for ch in s:
|
|
169
|
+
if ch in _CN_DIGITS:
|
|
170
|
+
num = _CN_DIGITS[ch]
|
|
171
|
+
if num == 0:
|
|
172
|
+
zero = True
|
|
173
|
+
continue
|
|
174
|
+
unit = _CN_UNITS.get(ch)
|
|
175
|
+
if unit is None:
|
|
176
|
+
return None
|
|
177
|
+
if unit >= 10000:
|
|
178
|
+
section = (section + num) * unit
|
|
179
|
+
total += section
|
|
180
|
+
section, num, last_unit = 0, 0, unit
|
|
181
|
+
else:
|
|
182
|
+
if num == 0:
|
|
183
|
+
num = 1
|
|
184
|
+
section += num * unit
|
|
185
|
+
num, last_unit = 0, unit
|
|
186
|
+
if num:
|
|
187
|
+
if zero: # 一百零五
|
|
188
|
+
section += num
|
|
189
|
+
elif last_unit >= 10: # 两千三 → 2300
|
|
190
|
+
section += num * max(1, last_unit // 10)
|
|
191
|
+
else:
|
|
192
|
+
section += num
|
|
193
|
+
return float(total + section)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
# ══════════════════════════════════════════════════════════
|
|
197
|
+
# 时间
|
|
198
|
+
# ══════════════════════════════════════════════════════════
|
|
199
|
+
def _shift_month(now: datetime, months: int, day: Optional[int] = None) -> datetime:
|
|
200
|
+
"""按月份偏移,并把日 clamp 到目标月的合法范围(1 月 31 日 −1 月 → 2 月 28/29)。"""
|
|
201
|
+
y, m = now.year, now.month + months
|
|
202
|
+
y += (m - 1) // 12
|
|
203
|
+
m = (m - 1) % 12 + 1
|
|
204
|
+
d = day if day is not None else now.day
|
|
205
|
+
while d > 28:
|
|
206
|
+
try:
|
|
207
|
+
return datetime(y, m, d, now.hour, now.minute)
|
|
208
|
+
except ValueError:
|
|
209
|
+
d -= 1
|
|
210
|
+
return datetime(y, m, d, now.hour, now.minute)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def _month_days(y: int, m: int) -> int:
|
|
214
|
+
nxt = datetime(y + (m == 12), (m % 12) + 1, 1)
|
|
215
|
+
return (nxt - timedelta(days=1)).day
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def _parse_when(text: str, now: datetime):
|
|
219
|
+
"""识别时间表达。返回 (when, cleaned_text, explicit)。
|
|
220
|
+
|
|
221
|
+
只认第一处时间表达:一句话里出现两个时间点时(「昨天买衣服,明天买菜」)应当在
|
|
222
|
+
上层按标点拆成两句,而不是在这里猜该用哪个。
|
|
223
|
+
"""
|
|
224
|
+
when, explicit, cleaned = None, False, text
|
|
225
|
+
|
|
226
|
+
def _take(m, dt, why="time"):
|
|
227
|
+
nonlocal when, explicit, cleaned
|
|
228
|
+
if when is None:
|
|
229
|
+
when, explicit = dt, True
|
|
230
|
+
cleaned = cleaned[:m.start()] + " " + cleaned[m.end():]
|
|
231
|
+
|
|
232
|
+
# 1) 绝对日期 2026-11-05 / 2026年11月5日
|
|
233
|
+
m = re.search(r"(\d{4})\s*[-/年]\s*(\d{1,2})\s*[-/月]\s*(\d{1,2})\s*[日号]?", cleaned)
|
|
234
|
+
if m:
|
|
235
|
+
try:
|
|
236
|
+
_take(m, datetime(int(m.group(1)), int(m.group(2)), int(m.group(3)),
|
|
237
|
+
now.hour, now.minute))
|
|
238
|
+
except ValueError:
|
|
239
|
+
pass
|
|
240
|
+
|
|
241
|
+
# 2) 月份偏移(可带"5号"):上个月 / 下个月 5 号
|
|
242
|
+
if when is None:
|
|
243
|
+
m = re.search(rf"({_MONTH_ALT})\s*(?:份)?\s*(?:(\d{{1,2}}|[二三四五六七八九十]{{1,3}})\s*[号日])?",
|
|
244
|
+
cleaned)
|
|
245
|
+
if m:
|
|
246
|
+
offset = next(o for w, o in _MONTH_WORDS if w == m.group(1))
|
|
247
|
+
day = None
|
|
248
|
+
if m.group(2):
|
|
249
|
+
day = int(m.group(2)) if m.group(2).isdigit() else int(cn_to_number(m.group(2)) or 0)
|
|
250
|
+
day = day if 1 <= day <= 31 else None
|
|
251
|
+
_take(m, _shift_month(now, offset, day))
|
|
252
|
+
|
|
253
|
+
# 3) N 月 / N 月 M 号(不带"上/下"修饰)
|
|
254
|
+
if when is None:
|
|
255
|
+
m = re.search(rf"(\d{{1,2}}|{_CN_NUM})\s*月份?\s*(?:(\d{{1,2}}|{_CN_NUM})\s*[号日])?",
|
|
256
|
+
cleaned)
|
|
257
|
+
if m:
|
|
258
|
+
mon = int(m.group(1)) if m.group(1).isdigit() else int(cn_to_number(m.group(1)) or 0)
|
|
259
|
+
if 1 <= mon <= 12:
|
|
260
|
+
day = None
|
|
261
|
+
if m.group(2):
|
|
262
|
+
day = int(m.group(2)) if m.group(2).isdigit() else int(cn_to_number(m.group(2)) or 0)
|
|
263
|
+
day = day if 1 <= day <= 31 else None
|
|
264
|
+
# 没写年份的「9 月」在 11 月说出来,指的就是今年 9 月(已过去);
|
|
265
|
+
# 「12 月」在 11 月说,只可能是去年 12 月 —— 未来请说「下个月」。
|
|
266
|
+
year = now.year - 1 if mon > now.month else now.year
|
|
267
|
+
dd = min(day or now.day, _month_days(year, mon))
|
|
268
|
+
_take(m, datetime(year, mon, dd, now.hour, now.minute))
|
|
269
|
+
|
|
270
|
+
# 4) 今天/昨天/前天/大前天/明天/后天/大后天
|
|
271
|
+
if when is None:
|
|
272
|
+
for word, delta in _DAY_WORDS:
|
|
273
|
+
idx = cleaned.find(word)
|
|
274
|
+
if idx >= 0:
|
|
275
|
+
dt = now + timedelta(days=delta)
|
|
276
|
+
when, explicit = dt, True
|
|
277
|
+
cleaned = cleaned[:idx] + " " + cleaned[idx + len(word):]
|
|
278
|
+
break
|
|
279
|
+
|
|
280
|
+
# 5) N 天前 / N 天后 / N 日之内
|
|
281
|
+
if when is None:
|
|
282
|
+
m = re.search(rf"(\d{{1,3}}|{_CN_NUM})\s*天\s*(前|后)", cleaned)
|
|
283
|
+
if m:
|
|
284
|
+
n = int(m.group(1)) if m.group(1).isdigit() else int(cn_to_number(m.group(1)) or 0)
|
|
285
|
+
delta = -n if m.group(2) == "前" else n
|
|
286
|
+
_take(m, now + timedelta(days=delta))
|
|
287
|
+
|
|
288
|
+
# 6) 上周/这周/下周
|
|
289
|
+
if when is None:
|
|
290
|
+
for word, delta in _WEEK_WORDS:
|
|
291
|
+
idx = cleaned.find(word)
|
|
292
|
+
if idx >= 0:
|
|
293
|
+
when, explicit = now + timedelta(days=delta), True
|
|
294
|
+
cleaned = cleaned[:idx] + " " + cleaned[idx + len(word):]
|
|
295
|
+
break
|
|
296
|
+
|
|
297
|
+
# 7) 光秃秃的「5 号」:没过 → 本月;已过 → 上个月(口语里 5 号说的都是已经发生的事)
|
|
298
|
+
if when is None:
|
|
299
|
+
m = re.search(rf"(\d{{1,2}}|{_CN_NUM})\s*[号日]", cleaned)
|
|
300
|
+
if m:
|
|
301
|
+
d = int(m.group(1)) if m.group(1).isdigit() else int(cn_to_number(m.group(1)) or 0)
|
|
302
|
+
if 1 <= d <= 31:
|
|
303
|
+
if d <= now.day:
|
|
304
|
+
dt = _shift_month(now, 0, d)
|
|
305
|
+
else:
|
|
306
|
+
dt = _shift_month(now, -1, d)
|
|
307
|
+
_take(m, dt)
|
|
308
|
+
|
|
309
|
+
return (when or now), cleaned, explicit
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
# ══════════════════════════════════════════════════════════
|
|
313
|
+
# 金额
|
|
314
|
+
# ══════════════════════════════════════════════════════════
|
|
315
|
+
def _to_float(num: str) -> Optional[float]:
|
|
316
|
+
try:
|
|
317
|
+
return float(num.replace(",", ""))
|
|
318
|
+
except ValueError:
|
|
319
|
+
return None
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def _extract_amount(text: str):
|
|
323
|
+
"""找金额。返回 (金额, 币种, [已匹配片段...], 是否带单位, 备注) 或 None。
|
|
324
|
+
|
|
325
|
+
优先级:符号 > 数字+币种 > 数字+量级 > 中文数字+币种/量级 > 动词后的数字 > 唯一的裸数字。
|
|
326
|
+
越往后越不确定,备注里会写明,让用户知道系统在猜。
|
|
327
|
+
"""
|
|
328
|
+
# 1) 符号前置:$105、¥1,050、€30
|
|
329
|
+
m = re.search(r"([¥¥$€£₩])\s*(\d[\d,]*(?:\.\d+)?)", text)
|
|
330
|
+
if m:
|
|
331
|
+
v = _to_float(m.group(2))
|
|
332
|
+
if v is not None:
|
|
333
|
+
return v, _SYMBOLS[m.group(1)], [m.group(0)], True, ""
|
|
334
|
+
|
|
335
|
+
# 2) 数字 + 币种(105 块 / 105 美元 / 105 rmb)
|
|
336
|
+
m = re.search(rf"(\d[\d,]*(?:\.\d+)?)\s*({_UNIT_ALT})", text, re.I)
|
|
337
|
+
if m:
|
|
338
|
+
v = _to_float(m.group(1))
|
|
339
|
+
if v is not None:
|
|
340
|
+
return v, CURRENCY_ALIASES[m.group(2).lower() if m.group(2).isascii()
|
|
341
|
+
else m.group(2)], [m.group(0)], True, ""
|
|
342
|
+
|
|
343
|
+
# 2b) 币种前置:美元 105 / rmb 88
|
|
344
|
+
m = re.search(rf"({_UNIT_ALT})\s*(\d[\d,]*(?:\.\d+)?)", text, re.I)
|
|
345
|
+
if m:
|
|
346
|
+
v = _to_float(m.group(2))
|
|
347
|
+
if v is not None:
|
|
348
|
+
key = m.group(1).lower() if m.group(1).isascii() else m.group(1)
|
|
349
|
+
return v, CURRENCY_ALIASES[key], [m.group(0)], True, ""
|
|
350
|
+
|
|
351
|
+
# 3) 数字 + 量级(1.5k / 2w / 3 万)
|
|
352
|
+
m = re.search(r"(\d[\d,]*(?:\.\d+)?)\s*(千|[kK]|[wW]|万)", text)
|
|
353
|
+
if m:
|
|
354
|
+
v = _to_float(m.group(1))
|
|
355
|
+
if v is not None:
|
|
356
|
+
scale = _SCALE.get(m.group(2).lower(), 1000)
|
|
357
|
+
return v * scale, "CNY", [m.group(0)], False, "按人民币处理(只给了量级)"
|
|
358
|
+
|
|
359
|
+
# 4) 中文数字 + 币种(一百零五块 / 两千美元)
|
|
360
|
+
m = re.search(rf"({_CN_NUM})\s*({_UNIT_ALT})", text, re.I)
|
|
361
|
+
if m:
|
|
362
|
+
v = cn_to_number(m.group(1))
|
|
363
|
+
if v is not None:
|
|
364
|
+
return v, CURRENCY_ALIASES[m.group(2).lower() if m.group(2).isascii()
|
|
365
|
+
else m.group(2)], [m.group(0)], True, ""
|
|
366
|
+
|
|
367
|
+
# 5) 中文数字整串(两千三 / 三千五 / 两万)。
|
|
368
|
+
# 注意必须**整串**交给 cn_to_number:先按 (千|万) 切会得到 "两"+"千" = 2000,
|
|
369
|
+
# 而用户说的是 2300。
|
|
370
|
+
m = re.search(rf"({_CN_NUM})", text)
|
|
371
|
+
if m and len(m.group(1)) >= 2:
|
|
372
|
+
v = cn_to_number(m.group(1))
|
|
373
|
+
if v is not None:
|
|
374
|
+
return v, "CNY", [m.group(0)], False, "按人民币处理(只给了量级)"
|
|
375
|
+
|
|
376
|
+
# 6) 动词旁边的裸数字(花了 105 / 付 30)
|
|
377
|
+
m = re.search(rf"(?:{'|'.join(sorted(_EXPENSE_VERBS, key=len, reverse=True))})"
|
|
378
|
+
rf"\s*(\d[\d,]*(?:\.\d+)?)", text)
|
|
379
|
+
if m:
|
|
380
|
+
v = _to_float(m.group(1))
|
|
381
|
+
if v is not None:
|
|
382
|
+
return v, "CNY", [m.group(0)], False, ""
|
|
383
|
+
m = re.search(rf"(?:{'|'.join(sorted(_EXPENSE_VERBS, key=len, reverse=True))})"
|
|
384
|
+
rf"\s*({_CN_NUM})", text)
|
|
385
|
+
if m:
|
|
386
|
+
v = cn_to_number(m.group(1))
|
|
387
|
+
if v is not None:
|
|
388
|
+
return v, "CNY", [m.group(0)], False, ""
|
|
389
|
+
|
|
390
|
+
# 7) 裸数字:只有一个就用它;有多个取最大(「买 2 件衣服 105」→ 105)。
|
|
391
|
+
# 口语报备省略「元」是常态,单数字不再提示 —— 每笔都挂一句
|
|
392
|
+
# 「按人民币处理」纯属噪音,反而让回执显得不可靠。
|
|
393
|
+
nums = [(v, mm.group(0)) for mm in re.finditer(r"\d[\d,]*(?:\.\d+)?", text)
|
|
394
|
+
if (v := _to_float(mm.group(0))) is not None]
|
|
395
|
+
if len(nums) == 1:
|
|
396
|
+
return nums[0][0], "CNY", [nums[0][1]], False, ""
|
|
397
|
+
if len(nums) > 1:
|
|
398
|
+
v, raw = max(nums, key=lambda x: x[0])
|
|
399
|
+
return v, "CNY", [raw], False, f"句子里有多个数字,按最大的 {v:g} 元记账,请核对"
|
|
400
|
+
return None
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
# ══════════════════════════════════════════════════════════
|
|
404
|
+
# 分类 / 收支方向
|
|
405
|
+
# ══════════════════════════════════════════════════════════
|
|
406
|
+
def _classify(text: str) -> str:
|
|
407
|
+
low = text.lower()
|
|
408
|
+
for cat, words in _CATEGORY_RULES:
|
|
409
|
+
if any(w in low for w in words):
|
|
410
|
+
return cat
|
|
411
|
+
return "misc"
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def _direction(text: str) -> str:
|
|
415
|
+
"""收入还是支出。两者都出现时按支出(「报销到账后退了款」这种复合句交给 LLM 分析)。"""
|
|
416
|
+
has_in = bool(_INCOME_PAT.search(text))
|
|
417
|
+
has_out = bool(_VERB_PAT.search(text))
|
|
418
|
+
if has_in and not has_out:
|
|
419
|
+
return "income"
|
|
420
|
+
return "expense"
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
# 描述清洗:剥掉金额、时间、功能动词与量词,只留下"买的是什么"。
|
|
424
|
+
# 保留词先占位 —— 「打车/洗车/加油/租房」里的动词本身就是消费名,
|
|
425
|
+
# 删掉会变成「车 25」这种读不懂的残句。
|
|
426
|
+
_KEEP_PHRASES = ("打车", "洗车", "加油", "吃饭", "租房", "房租", "停车", "话费", "液化气")
|
|
427
|
+
_STOP_WORDS = (
|
|
428
|
+
"花了", "花费", "付了", "付款", "支付", "买了", "充值", "充了", "消费",
|
|
429
|
+
"收到", "赚了", "退了", "报销了", "发了", "发工资", "进账", "退款", "退回",
|
|
430
|
+
"一共", "总共", "合计", "大概", "差不多",
|
|
431
|
+
"花", "买", "购", "付", "交", "缴", "扣", "订", "租", "退", "收",
|
|
432
|
+
"在", "的", "了", "呢", "啊", "吧", "呀", "哦", "嗯", "要", "去", "再",
|
|
433
|
+
"又", "就", "还", "和", "跟", "给", "把", "被", "我", "你", "他", "她", "它",
|
|
434
|
+
"件", "个", "只", "双", "条", "袋", "瓶", "杯", "张", "台", "部", "本",
|
|
435
|
+
"支", "盒", "箱", "次", "份", "碗", "串", "根", "桶", "套", "顿",
|
|
436
|
+
)
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
def _clean_description(text: str, spans: list) -> str:
|
|
440
|
+
out = text
|
|
441
|
+
for sp in spans:
|
|
442
|
+
out = out.replace(sp, " ")
|
|
443
|
+
# 保护动词性名词
|
|
444
|
+
for i, w in enumerate(_KEEP_PHRASES):
|
|
445
|
+
out = out.replace(w, f"\x00{i}\x00")
|
|
446
|
+
for w in sorted(_STOP_WORDS, key=len, reverse=True):
|
|
447
|
+
out = out.replace(w, " ")
|
|
448
|
+
out = re.sub(r"[,,。;;、!?!?::]+", " ", out)
|
|
449
|
+
out = re.sub(r"\s+", " ", out).strip(" .,:;,。;:")
|
|
450
|
+
for i, w in enumerate(_KEEP_PHRASES):
|
|
451
|
+
out = out.replace(f"\x00{i}\x00", w)
|
|
452
|
+
return re.sub(r"\s+", " ", out).strip()[:40]
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
_SPLIT_PAT = re.compile(r"[,,;;\n]+|(?:然后|另外|还有|以及|再就是|接着)")
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
# ══════════════════════════════════════════════════════════
|
|
459
|
+
# 主入口
|
|
460
|
+
# ══════════════════════════════════════════════════════════
|
|
461
|
+
def parse(text: str, now: datetime = None) -> list:
|
|
462
|
+
"""把一段话解析成 0..N 笔钱。
|
|
463
|
+
|
|
464
|
+
「今天买了一件衣服花了 105 块,还买了 45 块的大米」
|
|
465
|
+
→ [衣服 105 CNY 今天, 大米 45 CNY 今天]
|
|
466
|
+
|
|
467
|
+
拆分规则:按标点/连接词切段,逐段解析;**时间可继承** —— 后一段没写时间时沿用
|
|
468
|
+
前一段的时间(「今天…,还买了…」),这样用户不必每句都重复报时。
|
|
469
|
+
"""
|
|
470
|
+
now = now or datetime.now()
|
|
471
|
+
text = (text or "").strip()
|
|
472
|
+
if not text:
|
|
473
|
+
return []
|
|
474
|
+
|
|
475
|
+
segments = [s.strip() for s in _SPLIT_PAT.split(text) if s and s.strip()]
|
|
476
|
+
|
|
477
|
+
out, inherited_when = [], None
|
|
478
|
+
for seg in segments:
|
|
479
|
+
p = _parse_one(seg, now, inherited_when)
|
|
480
|
+
if p is None:
|
|
481
|
+
continue
|
|
482
|
+
inherited_when = p.when
|
|
483
|
+
out.append(p)
|
|
484
|
+
|
|
485
|
+
# 拆段只解析出 ≤1 笔 → 换用整句再解析一次。中文口语常带逗号
|
|
486
|
+
# (「今天,我买了件衣服,花了 105 块」),拆开会让「衣服」这个分类线索掉在
|
|
487
|
+
# 没有金额的段里,整句解析才拿得到完整语境。
|
|
488
|
+
if len(out) <= 1:
|
|
489
|
+
whole = _parse_one(text, now, None)
|
|
490
|
+
if whole is not None:
|
|
491
|
+
return [whole]
|
|
492
|
+
return out
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
def _parse_one(seg: str, now: datetime, inherited_when: Optional[datetime]):
|
|
496
|
+
original = seg
|
|
497
|
+
when, cleaned, explicit_time = _parse_when(seg, now)
|
|
498
|
+
if not explicit_time and inherited_when is not None:
|
|
499
|
+
when = inherited_when
|
|
500
|
+
|
|
501
|
+
cleaned = _QUANT_PAT.sub(" ", cleaned) # 「一件」「2 袋」不是金额
|
|
502
|
+
got = _extract_amount(cleaned)
|
|
503
|
+
if got is None:
|
|
504
|
+
return None
|
|
505
|
+
amount, currency, spans, has_unit, warn = got
|
|
506
|
+
if amount <= 0 or amount > 1e9:
|
|
507
|
+
return None
|
|
508
|
+
|
|
509
|
+
direction = _direction(original)
|
|
510
|
+
desc = _clean_description(cleaned, spans)
|
|
511
|
+
category = "income" if direction == "income" else _classify(original)
|
|
512
|
+
|
|
513
|
+
# 商户:「在星巴克买了…」
|
|
514
|
+
ent = ""
|
|
515
|
+
m = re.search(r"在\s*([\u4e00-\u9fa5A-Za-z0-9]{1,10}?)\s*(?:买|花|吃|订|充|消费|缴)", original)
|
|
516
|
+
if m:
|
|
517
|
+
ent = m.group(1)
|
|
518
|
+
|
|
519
|
+
warnings = [warn] if warn else []
|
|
520
|
+
conf = 0.45
|
|
521
|
+
if has_unit:
|
|
522
|
+
conf += 0.2
|
|
523
|
+
if explicit_time:
|
|
524
|
+
conf += 0.2
|
|
525
|
+
if category != "misc":
|
|
526
|
+
conf += 0.1
|
|
527
|
+
if _VERB_PAT.search(original) or _INCOME_PAT.search(original):
|
|
528
|
+
conf += 0.05
|
|
529
|
+
|
|
530
|
+
return ParsedExpense(
|
|
531
|
+
text=original, direction=direction, amount=amount, currency=currency,
|
|
532
|
+
when=when, category=category,
|
|
533
|
+
description=desc or CATEGORY_LABELS.get(category, ""),
|
|
534
|
+
entity=ent or (desc[:12] if desc else CATEGORY_LABELS.get(category, "未注明")),
|
|
535
|
+
confidence=min(conf, 0.98), warnings=warnings,
|
|
536
|
+
planned=when.date() > now.date(),
|
|
537
|
+
)
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
# 完成态付钱动词:「买了/花了/交了」说明钱**已经出去了**;
|
|
541
|
+
# 「房租 3000 占收入多少」里的「租」不算 —— 那句在讨论额度,不是在报账。
|
|
542
|
+
_SETTLED_SPEND = ("花了", "买了", "付了", "支付了", "消费了", "充值了",
|
|
543
|
+
"交了", "缴了", "扣了", "刷了", "转了", "付账")
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
def has_settled_spend(text: str) -> bool:
|
|
547
|
+
"""句子里有没有「已经花出去」的完成态动词。"""
|
|
548
|
+
t = text or ""
|
|
549
|
+
return any(w in t for w in _SETTLED_SPEND)
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
def has_payment_verb(text: str) -> bool:
|
|
553
|
+
"""有没有「花/买/付/交/缴/扣」这类明确的付钱动词。
|
|
554
|
+
|
|
555
|
+
用途:区分「我买了件衣服 105」和「衣服预算 800」—— 后者带金额但不是已发生的钱,
|
|
556
|
+
自动记账会把预算额记成一笔支出。
|
|
557
|
+
"""
|
|
558
|
+
return bool(_VERB_PAT.search(text or ""))
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
def has_expense_signal(text: str) -> bool:
|
|
562
|
+
"""这句话像不像「花了钱」的报备 —— 主管路由用,决定要不要交给财务 Agent。
|
|
563
|
+
|
|
564
|
+
判据:能解析出金额,并且有花费动词 / 货币单位 / 明确的消费类目。
|
|
565
|
+
只要求「有金额」会把「今天几号」「3 个月计划」也甩给财务,所以三条要占一条。
|
|
566
|
+
"""
|
|
567
|
+
t = (text or "").strip()
|
|
568
|
+
if not t or len(t) > 120:
|
|
569
|
+
return False
|
|
570
|
+
if not parse(t):
|
|
571
|
+
return False
|
|
572
|
+
has_verb = bool(_VERB_PAT.search(t) or _INCOME_PAT.search(t))
|
|
573
|
+
has_unit = any(k in t.lower() for k in CURRENCY_ALIASES) or any(s in t for s in _SYMBOLS)
|
|
574
|
+
has_cat = _classify(t) != "misc"
|
|
575
|
+
return has_verb or has_unit or has_cat
|