weflow-cli 1.5.0 → 1.6.1
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/ARCHITECTURE.md +105 -0
- package/CHANGELOG.md +121 -0
- package/CONTRIBUTING.md +45 -0
- package/LICENSE +21 -21
- package/OPERATIONS.md +272 -0
- package/README.en.md +281 -0
- package/README.md +334 -149
- package/SECURITY.md +47 -0
- package/bin/weflow-cli.ts +4050 -581
- package/dist/bin/weflow-cli.js +4310 -588
- package/dist/bin/weflow-cli.js.map +1 -1
- package/dist/mcp-server/index.js +178 -236
- package/dist/mcp-server/index.js.map +1 -1
- package/dist/src/core/dbPathService.d.ts +15 -1
- package/dist/src/core/dbPathService.d.ts.map +1 -1
- package/dist/src/core/dbPathService.js +177 -19
- package/dist/src/core/dbPathService.js.map +1 -1
- package/dist/src/core/keyService.d.ts +8 -0
- package/dist/src/core/keyService.d.ts.map +1 -1
- package/dist/src/core/keyService.js +127 -22
- package/dist/src/core/keyService.js.map +1 -1
- package/dist/src/core/ntCore.d.ts +59 -1
- package/dist/src/core/ntCore.d.ts.map +1 -1
- package/dist/src/core/ntCore.js +201 -65
- package/dist/src/core/ntCore.js.map +1 -1
- package/dist/src/core/sqlcipherCore.d.ts.map +1 -1
- package/dist/src/core/sqlcipherCore.js +18 -1
- package/dist/src/core/sqlcipherCore.js.map +1 -1
- package/dist/src/core/wcdbCore.d.ts.map +1 -1
- package/dist/src/core/wcdbCore.js +3 -6
- package/dist/src/core/wcdbCore.js.map +1 -1
- package/dist/src/services/assistantDaemon.d.ts +19 -0
- package/dist/src/services/assistantDaemon.d.ts.map +1 -0
- package/dist/src/services/assistantDaemon.js +117 -0
- package/dist/src/services/assistantDaemon.js.map +1 -0
- package/dist/src/services/assistantMemory.d.ts +36 -0
- package/dist/src/services/assistantMemory.d.ts.map +1 -0
- package/dist/src/services/assistantMemory.js +182 -0
- package/dist/src/services/assistantMemory.js.map +1 -0
- package/dist/src/services/assistantPrivacy.d.ts +25 -0
- package/dist/src/services/assistantPrivacy.d.ts.map +1 -0
- package/dist/src/services/assistantPrivacy.js +77 -0
- package/dist/src/services/assistantPrivacy.js.map +1 -0
- package/dist/src/services/assistantRouting.d.ts +18 -0
- package/dist/src/services/assistantRouting.d.ts.map +1 -0
- package/dist/src/services/assistantRouting.js +68 -0
- package/dist/src/services/assistantRouting.js.map +1 -0
- package/dist/src/services/assistantService.d.ts +37 -0
- package/dist/src/services/assistantService.d.ts.map +1 -0
- package/dist/src/services/assistantService.js +284 -0
- package/dist/src/services/assistantService.js.map +1 -0
- package/dist/src/services/assistantTools.d.ts +24 -0
- package/dist/src/services/assistantTools.d.ts.map +1 -0
- package/dist/src/services/assistantTools.js +582 -0
- package/dist/src/services/assistantTools.js.map +1 -0
- package/dist/src/services/chatService.d.ts +44 -0
- package/dist/src/services/chatService.d.ts.map +1 -1
- package/dist/src/services/chatService.js +105 -4
- package/dist/src/services/chatService.js.map +1 -1
- package/dist/src/services/configService.d.ts +55 -0
- package/dist/src/services/configService.d.ts.map +1 -1
- package/dist/src/services/configService.js +147 -6
- package/dist/src/services/configService.js.map +1 -1
- package/dist/src/services/evidenceService.d.ts +30 -0
- package/dist/src/services/evidenceService.d.ts.map +1 -0
- package/dist/src/services/evidenceService.js +98 -0
- package/dist/src/services/evidenceService.js.map +1 -0
- package/dist/src/services/exportService.d.ts +13 -20
- package/dist/src/services/exportService.d.ts.map +1 -1
- package/dist/src/services/exportService.js +113 -47
- package/dist/src/services/exportService.js.map +1 -1
- package/dist/src/services/initKeyService.d.ts +19 -0
- package/dist/src/services/initKeyService.d.ts.map +1 -0
- package/dist/src/services/initKeyService.js +80 -0
- package/dist/src/services/initKeyService.js.map +1 -0
- package/dist/src/services/messageContract.d.ts +23 -0
- package/dist/src/services/messageContract.d.ts.map +1 -0
- package/dist/src/services/messageContract.js +49 -0
- package/dist/src/services/messageContract.js.map +1 -0
- package/dist/src/services/messageQuery.d.ts +7 -0
- package/dist/src/services/messageQuery.d.ts.map +1 -0
- package/dist/src/services/messageQuery.js +30 -0
- package/dist/src/services/messageQuery.js.map +1 -0
- package/dist/src/services/wechat-formatter.js +14 -14
- package/dist/src/services/wechatMessageService.d.ts.map +1 -1
- package/dist/src/services/wechatMessageService.js +31 -3
- package/dist/src/services/wechatMessageService.js.map +1 -1
- package/dist/src/services/whitelistService.d.ts +52 -4
- package/dist/src/services/whitelistService.d.ts.map +1 -1
- package/dist/src/services/whitelistService.js +183 -17
- package/dist/src/services/whitelistService.js.map +1 -1
- package/dist/src/types.d.ts +20 -0
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/utils/dateRange.d.ts +15 -0
- package/dist/src/utils/dateRange.d.ts.map +1 -0
- package/dist/src/utils/dateRange.js +48 -0
- package/dist/src/utils/dateRange.js.map +1 -0
- package/dist/src/utils/mcpSecurity.d.ts +5 -0
- package/dist/src/utils/mcpSecurity.d.ts.map +1 -0
- package/dist/src/utils/mcpSecurity.js +39 -0
- package/dist/src/utils/mcpSecurity.js.map +1 -0
- package/dist/src/utils/packageRoot.d.ts +2 -0
- package/dist/src/utils/packageRoot.d.ts.map +1 -0
- package/dist/src/utils/packageRoot.js +16 -0
- package/dist/src/utils/packageRoot.js.map +1 -0
- package/dist/src/utils/python.d.ts +2 -0
- package/dist/src/utils/python.d.ts.map +1 -0
- package/dist/src/utils/python.js +72 -0
- package/dist/src/utils/python.js.map +1 -0
- package/dist/src/utils/pythonProcessEnv.d.ts +3 -0
- package/dist/src/utils/pythonProcessEnv.d.ts.map +1 -0
- package/dist/src/utils/pythonProcessEnv.js +46 -0
- package/dist/src/utils/pythonProcessEnv.js.map +1 -0
- package/dist/src/utils/talkerUtils.d.ts +4 -1
- package/dist/src/utils/talkerUtils.d.ts.map +1 -1
- package/dist/src/utils/talkerUtils.js +9 -9
- package/dist/src/utils/talkerUtils.js.map +1 -1
- package/dist/src/utils/wechatEmoji.d.ts +5 -0
- package/dist/src/utils/wechatEmoji.d.ts.map +1 -0
- package/dist/src/utils/wechatEmoji.js +35 -0
- package/dist/src/utils/wechatEmoji.js.map +1 -0
- package/docs/AI_INTERFACE.md +189 -0
- package/docs/BRANCHES.md +87 -0
- package/docs/DATA_CONTRACT.md +74 -0
- package/docs/DECISIONS.md +270 -0
- package/docs/DEEPSEEK_V4_FLASH/346/226/275/345/267/245/346/226/207/346/241/243.md +234 -0
- package/docs/EVIDENCE_GUIDE.md +96 -0
- package/docs/MCP.md +95 -0
- package/docs/PARTNERS.md +41 -0
- package/docs/PROJECT_STATE.md +96 -0
- package/docs/RELEASING.md +124 -0
- package/docs/ROADMAP.md +191 -0
- package/docs/SETUP.md +103 -0
- package/docs/images/weflow-architecture-gpt-image-2.png +0 -0
- package/docs/images/weflow-architecture.png +0 -0
- package/docs/images/weflow-architecture.svg +65 -0
- package/mcp-server/index.ts +169 -269
- package/package.json +81 -72
- package/requirements-3x.txt +4 -0
- package/requirements-voice.txt +15 -0
- package/requirements.txt +9 -0
- package/resources/js/marked.min.js +69 -0
- package/resources/wechat-emoji/666.png +0 -0
- package/resources/wechat-emoji/Awesome.png +0 -0
- package/resources/wechat-emoji/Concerned.png +0 -0
- package/resources/wechat-emoji/Cry.png +0 -0
- package/resources/wechat-emoji/Emm.png +0 -0
- package/resources/wechat-emoji/Facepalm.png +0 -0
- package/resources/wechat-emoji/Grin.png +0 -0
- package/resources/wechat-emoji/OK.png +0 -0
- package/resources/wechat-emoji/Respect.png +0 -0
- package/resources/wechat-emoji/Sick.png +0 -0
- package/resources/wechat-emoji/Sleep.png +0 -0
- package/resources/wechat-emoji/Smile.png +0 -0
- package/resources/wechat-emoji//344/272/262/344/272/262.png +0 -0
- package/resources/wechat-emoji//344/276/277/344/276/277.png +0 -0
- package/resources/wechat-emoji//345/201/267/347/254/221.png +0 -0
- package/resources/wechat-emoji//345/202/262/346/205/242.png +0 -0
- package/resources/wechat-emoji//345/206/215/350/247/201.png +0 -0
- package/resources/wechat-emoji//345/207/213/350/260/242.png +0 -0
- package/resources/wechat-emoji//345/212/240/346/262/271.png +0 -0
- package/resources/wechat-emoji//345/213/276/345/274/225.png +0 -0
- package/resources/wechat-emoji//345/217/221/345/221/206.png +0 -0
- package/resources/wechat-emoji//345/217/221/346/200/222.png +0 -0
- package/resources/wechat-emoji//345/217/221/346/212/226.png +0 -0
- package/resources/wechat-emoji//345/217/257/346/200/234.png +0 -0
- package/resources/wechat-emoji//345/217/263/345/223/274/345/223/274.png +0 -0
- package/resources/wechat-emoji//345/217/271/346/260/224.png +0 -0
- package/resources/wechat-emoji//345/220/203/347/223/234.png +0 -0
- package/resources/wechat-emoji//345/220/210/345/215/201.png +0 -0
- package/resources/wechat-emoji//345/220/220.png +0 -0
- package/resources/wechat-emoji//345/221/262/347/211/231.png +0 -0
- package/resources/wechat-emoji//345/222/222/351/252/202.png +0 -0
- package/resources/wechat-emoji//345/222/226/345/225/241.png +0 -0
- package/resources/wechat-emoji//345/223/207.png +0 -0
- package/resources/wechat-emoji//345/225/244/351/205/222.png +0 -0
- package/resources/wechat-emoji//345/230/230.png +0 -0
- package/resources/wechat-emoji//345/230/264/345/224/207.png +0 -0
- package/resources/wechat-emoji//345/230/277/345/223/210.png +0 -0
- package/resources/wechat-emoji//345/233/247.png +0 -0
- package/resources/wechat-emoji//345/233/260.png +0 -0
- package/resources/wechat-emoji//345/235/217/347/254/221.png +0 -0
- package/resources/wechat-emoji//345/244/247/345/223/255.png +0 -0
- package/resources/wechat-emoji//345/244/251/345/225/212.png +0 -0
- package/resources/wechat-emoji//345/244/252/351/230/263.png +0 -0
- package/resources/wechat-emoji//345/244/261/346/234/233.png +0 -0
- package/resources/wechat-emoji//345/245/270/347/254/221.png +0 -0
- package/resources/wechat-emoji//345/245/275/347/232/204.png +0 -0
- package/resources/wechat-emoji//345/247/224/345/261/210.png +0 -0
- package/resources/wechat-emoji//345/256/263/347/276/236.png +0 -0
- package/resources/wechat-emoji//345/260/264/345/260/254.png +0 -0
- package/resources/wechat-emoji//345/272/206/347/245/235.png +0 -0
- package/resources/wechat-emoji//345/274/261.png +0 -0
- package/resources/wechat-emoji//345/274/272.png +0 -0
- package/resources/wechat-emoji//345/276/227/346/204/217.png +0 -0
- package/resources/wechat-emoji//345/276/256/347/254/221.png +0 -0
- package/resources/wechat-emoji//345/277/203/347/242/216.png +0 -0
- package/resources/wechat-emoji//345/277/253/345/223/255/344/272/206.png +0 -0
- package/resources/wechat-emoji//346/201/220/346/203/247.png +0 -0
- package/resources/wechat-emoji//346/202/240/351/227/262.png +0 -0
- package/resources/wechat-emoji//346/203/212/346/201/220.png +0 -0
- package/resources/wechat-emoji//346/203/212/350/256/266.png +0 -0
- package/resources/wechat-emoji//346/204/211/345/277/253.png +0 -0
- package/resources/wechat-emoji//346/206/250/347/254/221.png +0 -0
- package/resources/wechat-emoji//346/211/223/350/204/270.png +0 -0
- package/resources/wechat-emoji//346/212/223/347/213/202.png +0 -0
- package/resources/wechat-emoji//346/212/240/351/274/273.png +0 -0
- package/resources/wechat-emoji//346/212/261/346/213/263.png +0 -0
- package/resources/wechat-emoji//346/213/245/346/212/261.png +0 -0
- package/resources/wechat-emoji//346/213/263/345/244/264.png +0 -0
- package/resources/wechat-emoji//346/215/202/350/204/270.png +0 -0
- package/resources/wechat-emoji//346/217/241/346/211/213.png +0 -0
- package/resources/wechat-emoji//346/222/207/345/230/264.png +0 -0
- package/resources/wechat-emoji//346/223/246/346/261/227.png +0 -0
- package/resources/wechat-emoji//346/225/262/346/211/223.png +0 -0
- package/resources/wechat-emoji//346/227/240/350/257/255.png +0 -0
- package/resources/wechat-emoji//346/227/272/346/237/264.png +0 -0
- package/resources/wechat-emoji//346/231/225.png +0 -0
- package/resources/wechat-emoji//346/234/210/344/272/256.png +0 -0
- package/resources/wechat-emoji//346/234/272/346/231/272.png +0 -0
- package/resources/wechat-emoji//346/261/227.png +0 -0
- package/resources/wechat-emoji//346/265/201/346/263/252.png +0 -0
- package/resources/wechat-emoji//347/202/270/345/274/271.png +0 -0
- package/resources/wechat-emoji//347/203/237/350/212/261.png +0 -0
- package/resources/wechat-emoji//347/210/206/347/253/271.png +0 -0
- package/resources/wechat-emoji//347/210/261/345/277/203.png +0 -0
- package/resources/wechat-emoji//347/214/252/345/244/264.png +0 -0
- package/resources/wechat-emoji//347/216/253/347/221/260.png +0 -0
- package/resources/wechat-emoji//347/224/237/347/227/205.png +0 -0
- package/resources/wechat-emoji//347/226/221/351/227/256.png +0 -0
- package/resources/wechat-emoji//347/231/274.png +0 -0
- package/resources/wechat-emoji//347/231/275/347/234/274.png +0 -0
- package/resources/wechat-emoji//347/232/261/347/234/211.png +0 -0
- package/resources/wechat-emoji//347/235/241.png +0 -0
- package/resources/wechat-emoji//347/240/264/346/266/225/344/270/272/347/254/221.png +0 -0
- package/resources/wechat-emoji//347/244/274/347/211/251.png +0 -0
- package/resources/wechat-emoji//347/244/276/344/274/232/347/244/276/344/274/232.png +0 -0
- package/resources/wechat-emoji//347/246/217.png +0 -0
- package/resources/wechat-emoji//347/254/221/350/204/270.png +0 -0
- package/resources/wechat-emoji//347/272/242/345/214/205.png +0 -0
- package/resources/wechat-emoji//347/277/273/347/231/275/347/234/274.png +0 -0
- package/resources/wechat-emoji//350/200/266.png +0 -0
- package/resources/wechat-emoji//350/203/234/345/210/251.png +0 -0
- package/resources/wechat-emoji//350/204/270/347/272/242.png +0 -0
- package/resources/wechat-emoji//350/211/262.png +0 -0
- package/resources/wechat-emoji//350/213/246/346/266/251.png +0 -0
- package/resources/wechat-emoji//350/217/234/345/210/200.png +0 -0
- package/resources/wechat-emoji//350/233/213/347/263/225.png +0 -0
- package/resources/wechat-emoji//350/241/260.png +0 -0
- package/resources/wechat-emoji//350/243/202/345/274/200.png +0 -0
- package/resources/wechat-emoji//350/256/251/346/210/221/347/234/213/347/234/213.png +0 -0
- package/resources/wechat-emoji//350/260/203/347/232/256.png +0 -0
- package/resources/wechat-emoji//350/267/263/350/267/263.png +0 -0
- package/resources/wechat-emoji//350/275/254/345/234/210.png +0 -0
- package/resources/wechat-emoji//351/204/231/350/247/206.png +0 -0
- package/resources/wechat-emoji//351/227/255/345/230/264.png +0 -0
- package/resources/wechat-emoji//351/230/264/351/231/251.png +0 -0
- package/resources/wechat-emoji//351/232/276/350/277/207.png +0 -0
- package/resources/wechat-emoji//351/252/267/351/253/205.png +0 -0
- package/resources/wechat-emoji//351/274/223/346/216/214.png +0 -0
- package/scripts/_batch_link_sources.py +73 -73
- package/scripts/_utils.py +29 -0
- package/scripts/annual_report.py +796 -796
- package/scripts/auto_tag.py +128 -128
- package/scripts/biz_daily.py +210 -53
- package/scripts/chat_report.py +10 -0
- package/scripts/chat_stats.py +702 -702
- package/scripts/classify_daily.py +17 -11
- package/scripts/create_reading_notes.py +370 -370
- package/scripts/daily_stats.py +132 -0
- package/scripts/enrich_backlinks.py +163 -163
- package/scripts/export_chat_html.py +1704 -107
- package/scripts/extract_todos.py +445 -404
- package/scripts/fav_server.py +119 -12
- package/scripts/fix_topics.py +111 -111
- package/scripts/generate_ai_report.py +1 -1
- package/scripts/generate_html.py +597 -277
- package/scripts/inspection_records.py +354 -0
- package/scripts/mcp_bridge.py +398 -398
- package/scripts/nt_decrypt.py +702 -48
- package/scripts/pipeline.py +27 -19
- package/scripts/promote_all.py +312 -310
- package/scripts/promote_ideas.py +299 -297
- package/scripts/rag_chat.py +194 -191
- package/scripts/semantic_search.py +435 -431
- package/scripts/sync_fav.py +201 -146
- package/scripts/sync_weread.py +192 -192
- package/scripts/vault_rag.py +140 -139
- package/scripts/vault_search.py +141 -141
- package/scripts/wechat_emoji.py +132 -0
- package/scripts/wechat_emoticon.py +433 -0
- package/scripts/wechat_image.py +258 -0
- package/scripts/wechat_voice.py +327 -0
- package/src/core/dbPathService.ts +163 -18
- package/src/core/keyService.ts +137 -25
- package/src/core/ntCore.ts +215 -62
- package/src/core/sqlcipherCore.ts +17 -1
- package/src/core/wcdbCore.ts +3 -7
- package/src/services/assistantDaemon.ts +111 -0
- package/src/services/assistantMemory.ts +193 -0
- package/src/services/assistantPrivacy.ts +87 -0
- package/src/services/assistantRouting.ts +92 -0
- package/src/services/assistantService.ts +295 -0
- package/src/services/assistantTools.ts +549 -0
- package/src/services/chatService.ts +133 -4
- package/src/services/configService.ts +194 -6
- package/src/services/evidenceService.ts +129 -0
- package/src/services/exportService.ts +113 -48
- package/src/services/initKeyService.ts +83 -0
- package/src/services/messageContract.ts +77 -0
- package/src/services/messageQuery.ts +38 -0
- package/src/services/wechat-formatter.ts +348 -348
- package/src/services/wechatMessageService.ts +25 -3
- package/src/services/wereadService.ts +308 -308
- package/src/services/whitelistService.ts +209 -22
- package/src/types.ts +20 -0
- package/src/utils/dateRange.ts +58 -0
- package/src/utils/mcpSecurity.ts +39 -0
- package/src/utils/packageRoot.ts +14 -0
- package/src/utils/python.ts +73 -0
- package/src/utils/pythonProcessEnv.ts +45 -0
- package/src/utils/talkerUtils.ts +14 -2
- package/dist/src/utils/errors.d.ts +0 -25
- package/dist/src/utils/errors.d.ts.map +0 -1
- package/dist/src/utils/errors.js +0 -43
- package/dist/src/utils/errors.js.map +0 -1
- package/dist/src/utils/pythonRunner.d.ts +0 -27
- package/dist/src/utils/pythonRunner.d.ts.map +0 -1
- package/dist/src/utils/pythonRunner.js +0 -62
- package/dist/src/utils/pythonRunner.js.map +0 -1
- package/src/utils/errors.ts +0 -42
- package/src/utils/pythonRunner.ts +0 -81
package/scripts/vault_rag.py
CHANGED
|
@@ -1,139 +1,140 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
3
|
-
Vault RAG 对话 — 基于 Vault 知识库(文章+概念+笔记)的问答。
|
|
4
|
-
|
|
5
|
-
用法:
|
|
6
|
-
python scripts/vault_rag.py "深度学习在遥感中的应用"
|
|
7
|
-
python scripts/vault_rag.py "最近读了什么文章" --json
|
|
8
|
-
"""
|
|
9
|
-
|
|
10
|
-
import sys, os, json, argparse, re
|
|
11
|
-
from pathlib import Path
|
|
12
|
-
|
|
13
|
-
SCRIPTS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
14
|
-
PROJECT_ROOT = os.path.dirname(SCRIPTS_DIR)
|
|
15
|
-
DEFAULT_VAULT = os.path.join(PROJECT_ROOT, 'output', 'wechat-vault')
|
|
16
|
-
DEFAULT_BIZ = os.path.join(PROJECT_ROOT, 'output', 'biz-daily')
|
|
17
|
-
|
|
18
|
-
sys.path.insert(0, SCRIPTS_DIR)
|
|
19
|
-
from _utils import call_deepseek
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
def collect_context(vault: str, biz_daily: str, question: str, top_k: int) -> list[dict]:
|
|
23
|
-
"""从 Vault + biz-daily 检索相关上下文。"""
|
|
24
|
-
results = []
|
|
25
|
-
search_terms = question.lower().split()
|
|
26
|
-
|
|
27
|
-
# 1. 概念页(权重最高)
|
|
28
|
-
concepts_dir = Path(vault) / 'Wiki' / 'Concepts'
|
|
29
|
-
if concepts_dir.is_dir():
|
|
30
|
-
for md in concepts_dir.glob('*.md'):
|
|
31
|
-
try:
|
|
32
|
-
text = md.read_text(encoding='utf-8')[:2000]
|
|
33
|
-
except Exception:
|
|
34
|
-
continue
|
|
35
|
-
score = sum(text.lower().count(t) for t in search_terms)
|
|
36
|
-
if score > 0:
|
|
37
|
-
results.append({'source': 'concept', 'title': md.stem, 'content': text, 'score': score})
|
|
38
|
-
|
|
39
|
-
# 2. 阅读笔记
|
|
40
|
-
notes_dir = Path(vault) / 'Notes'
|
|
41
|
-
if notes_dir.is_dir():
|
|
42
|
-
for md in notes_dir.rglob('*.md'):
|
|
43
|
-
try:
|
|
44
|
-
text = md.read_text(encoding='utf-8')[:1500]
|
|
45
|
-
except Exception:
|
|
46
|
-
continue
|
|
47
|
-
score = sum(text.lower().count(t) for t in search_terms)
|
|
48
|
-
# 标题匹配加分
|
|
49
|
-
if any(t in md.stem.lower() for t in search_terms):
|
|
50
|
-
score += 15
|
|
51
|
-
if score > 0:
|
|
52
|
-
results.append({'source': 'note', 'title': md.stem[:60], 'content': text, 'score': score})
|
|
53
|
-
|
|
54
|
-
# 3. 最新文章
|
|
55
|
-
biz_path = Path(biz_daily)
|
|
56
|
-
for date_dir in sorted(biz_path.glob('20*'), reverse=True)[:7]: # 最近 7 天
|
|
57
|
-
for md in date_dir.rglob('*.md'):
|
|
58
|
-
if md.name == 'README.md' or 'README' in md.name:
|
|
59
|
-
continue
|
|
60
|
-
try:
|
|
61
|
-
text = md.read_text(encoding='utf-8')[:1000]
|
|
62
|
-
except Exception:
|
|
63
|
-
continue
|
|
64
|
-
score = sum(text.lower().count(t) for t in search_terms)
|
|
65
|
-
if score > 0:
|
|
66
|
-
results.append({'source': 'article', 'title': md.stem[:60], 'content': text, 'score': score})
|
|
67
|
-
|
|
68
|
-
results.sort(key=lambda x: x['score'], reverse=True)
|
|
69
|
-
return results[:top_k]
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
def build_prompt(question: str, context: list[dict]) -> str:
|
|
73
|
-
sources_text = ''
|
|
74
|
-
for i, ctx in enumerate(context):
|
|
75
|
-
label = {'concept': '概念', 'note': '笔记', 'article': '文章'}.get(ctx['source'], ctx['source'])
|
|
76
|
-
sources_text += f'\n[{i+1}] ({label}) {ctx["title"]}\n{ctx["content"][:600]}\n'
|
|
77
|
-
|
|
78
|
-
return f'''你是个人知识助手,基于用户笔记库回答问题。
|
|
79
|
-
|
|
80
|
-
知识库内容:
|
|
81
|
-
{sources_text}
|
|
82
|
-
|
|
83
|
-
用户问题:{question}
|
|
84
|
-
|
|
85
|
-
请综合知识库内容回答。如果知识库中没有相关信息,如实说明。'''
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
def main():
|
|
89
|
-
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
|
|
90
|
-
parser = argparse.ArgumentParser(description='Vault RAG 对话')
|
|
91
|
-
parser.add_argument('question', nargs='?', default='', help='问题')
|
|
92
|
-
parser.add_argument('--top-k', default='8', help='检索条数')
|
|
93
|
-
parser.add_argument('--vault', default=DEFAULT_VAULT, help='Vault 路径')
|
|
94
|
-
parser.add_argument('--biz-daily', default=DEFAULT_BIZ, help='biz-daily 路径')
|
|
95
|
-
parser.add_argument('--api-key', default='', help='DeepSeek API key')
|
|
96
|
-
parser.add_argument('--json', action='store_true', help='JSON 输出')
|
|
97
|
-
args = parser.parse_args()
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
print(
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Vault RAG 对话 — 基于 Vault 知识库(文章+概念+笔记)的问答。
|
|
4
|
+
|
|
5
|
+
用法:
|
|
6
|
+
python scripts/vault_rag.py "深度学习在遥感中的应用"
|
|
7
|
+
python scripts/vault_rag.py "最近读了什么文章" --json
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import sys, os, json, argparse, re
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
SCRIPTS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
14
|
+
PROJECT_ROOT = os.path.dirname(SCRIPTS_DIR)
|
|
15
|
+
DEFAULT_VAULT = os.path.join(PROJECT_ROOT, 'output', 'wechat-vault')
|
|
16
|
+
DEFAULT_BIZ = os.path.join(PROJECT_ROOT, 'output', 'biz-daily')
|
|
17
|
+
|
|
18
|
+
sys.path.insert(0, SCRIPTS_DIR)
|
|
19
|
+
from _utils import call_deepseek
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def collect_context(vault: str, biz_daily: str, question: str, top_k: int) -> list[dict]:
|
|
23
|
+
"""从 Vault + biz-daily 检索相关上下文。"""
|
|
24
|
+
results = []
|
|
25
|
+
search_terms = question.lower().split()
|
|
26
|
+
|
|
27
|
+
# 1. 概念页(权重最高)
|
|
28
|
+
concepts_dir = Path(vault) / 'Wiki' / 'Concepts'
|
|
29
|
+
if concepts_dir.is_dir():
|
|
30
|
+
for md in concepts_dir.glob('*.md'):
|
|
31
|
+
try:
|
|
32
|
+
text = md.read_text(encoding='utf-8')[:2000]
|
|
33
|
+
except Exception:
|
|
34
|
+
continue
|
|
35
|
+
score = sum(text.lower().count(t) for t in search_terms)
|
|
36
|
+
if score > 0:
|
|
37
|
+
results.append({'source': 'concept', 'title': md.stem, 'content': text, 'score': score})
|
|
38
|
+
|
|
39
|
+
# 2. 阅读笔记
|
|
40
|
+
notes_dir = Path(vault) / 'Notes'
|
|
41
|
+
if notes_dir.is_dir():
|
|
42
|
+
for md in notes_dir.rglob('*.md'):
|
|
43
|
+
try:
|
|
44
|
+
text = md.read_text(encoding='utf-8')[:1500]
|
|
45
|
+
except Exception:
|
|
46
|
+
continue
|
|
47
|
+
score = sum(text.lower().count(t) for t in search_terms)
|
|
48
|
+
# 标题匹配加分
|
|
49
|
+
if any(t in md.stem.lower() for t in search_terms):
|
|
50
|
+
score += 15
|
|
51
|
+
if score > 0:
|
|
52
|
+
results.append({'source': 'note', 'title': md.stem[:60], 'content': text, 'score': score})
|
|
53
|
+
|
|
54
|
+
# 3. 最新文章
|
|
55
|
+
biz_path = Path(biz_daily)
|
|
56
|
+
for date_dir in sorted(biz_path.glob('20*'), reverse=True)[:7]: # 最近 7 天
|
|
57
|
+
for md in date_dir.rglob('*.md'):
|
|
58
|
+
if md.name == 'README.md' or 'README' in md.name:
|
|
59
|
+
continue
|
|
60
|
+
try:
|
|
61
|
+
text = md.read_text(encoding='utf-8')[:1000]
|
|
62
|
+
except Exception:
|
|
63
|
+
continue
|
|
64
|
+
score = sum(text.lower().count(t) for t in search_terms)
|
|
65
|
+
if score > 0:
|
|
66
|
+
results.append({'source': 'article', 'title': md.stem[:60], 'content': text, 'score': score})
|
|
67
|
+
|
|
68
|
+
results.sort(key=lambda x: x['score'], reverse=True)
|
|
69
|
+
return results[:top_k]
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def build_prompt(question: str, context: list[dict]) -> str:
|
|
73
|
+
sources_text = ''
|
|
74
|
+
for i, ctx in enumerate(context):
|
|
75
|
+
label = {'concept': '概念', 'note': '笔记', 'article': '文章'}.get(ctx['source'], ctx['source'])
|
|
76
|
+
sources_text += f'\n[{i+1}] ({label}) {ctx["title"]}\n{ctx["content"][:600]}\n'
|
|
77
|
+
|
|
78
|
+
return f'''你是个人知识助手,基于用户笔记库回答问题。
|
|
79
|
+
|
|
80
|
+
知识库内容:
|
|
81
|
+
{sources_text}
|
|
82
|
+
|
|
83
|
+
用户问题:{question}
|
|
84
|
+
|
|
85
|
+
请综合知识库内容回答。如果知识库中没有相关信息,如实说明。'''
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def main():
|
|
89
|
+
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
|
|
90
|
+
parser = argparse.ArgumentParser(description='Vault RAG 对话')
|
|
91
|
+
parser.add_argument('question', nargs='?', default='', help='问题')
|
|
92
|
+
parser.add_argument('--top-k', default='8', help='检索条数')
|
|
93
|
+
parser.add_argument('--vault', default=DEFAULT_VAULT, help='Vault 路径')
|
|
94
|
+
parser.add_argument('--biz-daily', default=DEFAULT_BIZ, help='biz-daily 路径')
|
|
95
|
+
parser.add_argument('--api-key', default='', help='DeepSeek API key')
|
|
96
|
+
parser.add_argument('--json', action='store_true', help='JSON 输出')
|
|
97
|
+
args = parser.parse_args()
|
|
98
|
+
|
|
99
|
+
question = args.question or os.environ.get('WEFLOW_VAULT_QUESTION', '')
|
|
100
|
+
if not question:
|
|
101
|
+
print('请提供问题')
|
|
102
|
+
sys.exit(1)
|
|
103
|
+
|
|
104
|
+
api_key = args.api_key or os.environ.get('DEEPSEEK_API_KEY', '')
|
|
105
|
+
if not api_key:
|
|
106
|
+
print('[ERROR] 缺少 API Key')
|
|
107
|
+
sys.exit(1)
|
|
108
|
+
|
|
109
|
+
top_k = int(args.top_k)
|
|
110
|
+
context = collect_context(args.vault, args.biz_daily, question, top_k)
|
|
111
|
+
|
|
112
|
+
if args.json:
|
|
113
|
+
output = {'question': question, 'sources': [{'source': c['source'], 'title': c['title'], 'score': c['score']} for c in context]}
|
|
114
|
+
if context:
|
|
115
|
+
prompt = build_prompt(question, context)
|
|
116
|
+
output['answer'] = call_deepseek(prompt, api_key, max_tokens=800)
|
|
117
|
+
else:
|
|
118
|
+
output['answer'] = '知识库中没有相关信息'
|
|
119
|
+
print(json.dumps(output, ensure_ascii=False, indent=2))
|
|
120
|
+
return
|
|
121
|
+
|
|
122
|
+
print(f'🔍 "{question}"\n')
|
|
123
|
+
if not context:
|
|
124
|
+
print('未找到相关内容')
|
|
125
|
+
return
|
|
126
|
+
|
|
127
|
+
print(f'检索到 {len(context)} 条相关内容:\n')
|
|
128
|
+
for i, c in enumerate(context):
|
|
129
|
+
icon = {'concept': '🧠', 'note': '📝', 'article': '📄'}.get(c['source'], '📎')
|
|
130
|
+
print(f' {i+1}. {icon} [{c["source"]}] {c["title"]}')
|
|
131
|
+
|
|
132
|
+
prompt = build_prompt(question, context)
|
|
133
|
+
print(f'\n{"="*50}')
|
|
134
|
+
answer = call_deepseek(prompt, api_key, max_tokens=800)
|
|
135
|
+
print(answer)
|
|
136
|
+
print(f'{"="*50}')
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
if __name__ == '__main__':
|
|
140
|
+
main()
|
package/scripts/vault_search.py
CHANGED
|
@@ -1,141 +1,141 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
3
|
-
Vault 全局搜索 — 关键词 + AI 排序,覆盖文章/概念/笔记。
|
|
4
|
-
|
|
5
|
-
用法:
|
|
6
|
-
python scripts/vault_search.py "遥感反演" --top-k 10
|
|
7
|
-
python scripts/vault_search.py "空气污染" --type article --days 30
|
|
8
|
-
"""
|
|
9
|
-
|
|
10
|
-
import sys, os, json, argparse, re
|
|
11
|
-
from pathlib import Path
|
|
12
|
-
from datetime import datetime, timedelta
|
|
13
|
-
|
|
14
|
-
SCRIPTS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
15
|
-
PROJECT_ROOT = os.path.dirname(SCRIPTS_DIR)
|
|
16
|
-
DEFAULT_VAULT = os.path.join(PROJECT_ROOT, 'output', 'wechat-vault')
|
|
17
|
-
DEFAULT_BIZ = os.path.join(PROJECT_ROOT, 'output', 'biz-daily')
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def search_text(query: str, files: list[tuple], top_k: int) -> list[dict]:
|
|
21
|
-
"""关键词搜索 + 简单排序。"""
|
|
22
|
-
terms = query.lower().split()
|
|
23
|
-
scored = []
|
|
24
|
-
for filepath, category, meta in files:
|
|
25
|
-
try:
|
|
26
|
-
text = filepath.read_text(encoding='utf-8')[:2000]
|
|
27
|
-
except Exception:
|
|
28
|
-
continue
|
|
29
|
-
text_lower = text.lower()
|
|
30
|
-
# 计分:标题匹配 > 关键词频次
|
|
31
|
-
score = 0
|
|
32
|
-
title = meta.get('title', '')
|
|
33
|
-
if any(t in title.lower() for t in terms):
|
|
34
|
-
score += 10
|
|
35
|
-
for t in terms:
|
|
36
|
-
score += text_lower.count(t)
|
|
37
|
-
if score > 0:
|
|
38
|
-
scored.append({'file': str(filepath), 'category': category, 'title': title,
|
|
39
|
-
'score': score, 'snippet': text[:300]})
|
|
40
|
-
scored.sort(key=lambda x: x['score'], reverse=True)
|
|
41
|
-
return scored[:top_k]
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
def collect_files(vault: str, biz_daily: str, search_type: str, days: int) -> list[tuple]:
|
|
45
|
-
"""收集可搜索文件。"""
|
|
46
|
-
files = []
|
|
47
|
-
cutoff = datetime.now() - timedelta(days=days)
|
|
48
|
-
|
|
49
|
-
def parse_fm(text):
|
|
50
|
-
if not text.startswith('---'):
|
|
51
|
-
return {}
|
|
52
|
-
end = text.find('---', 3)
|
|
53
|
-
if end == -1:
|
|
54
|
-
return {}
|
|
55
|
-
fm = {}
|
|
56
|
-
for line in text[3:end].strip().split('\n'):
|
|
57
|
-
if ':' in line:
|
|
58
|
-
k, _, v = line.partition(':')
|
|
59
|
-
fm[k.strip()] = v.strip().strip('"').strip("'")
|
|
60
|
-
return fm
|
|
61
|
-
|
|
62
|
-
if search_type in ('all', 'article'):
|
|
63
|
-
# biz-daily 文章
|
|
64
|
-
biz_dir = Path(biz_daily)
|
|
65
|
-
for date_dir in sorted(biz_dir.glob('20*'), reverse=True):
|
|
66
|
-
try:
|
|
67
|
-
dir_date = datetime.strptime(date_dir.name, '%Y-%m-%d')
|
|
68
|
-
if dir_date < cutoff:
|
|
69
|
-
continue
|
|
70
|
-
except ValueError:
|
|
71
|
-
continue
|
|
72
|
-
for md in date_dir.rglob('*.md'):
|
|
73
|
-
if md.name == 'README.md':
|
|
74
|
-
continue
|
|
75
|
-
try:
|
|
76
|
-
fm = parse_fm(md.read_text(encoding='utf-8'))
|
|
77
|
-
except Exception:
|
|
78
|
-
fm = {}
|
|
79
|
-
files.append((md, 'article', fm))
|
|
80
|
-
|
|
81
|
-
if search_type in ('all', 'concept'):
|
|
82
|
-
vault_path = Path(vault)
|
|
83
|
-
concepts_dir = vault_path / 'Wiki' / 'Concepts'
|
|
84
|
-
if concepts_dir.is_dir():
|
|
85
|
-
for md in concepts_dir.glob('*.md'):
|
|
86
|
-
try:
|
|
87
|
-
fm = parse_fm(md.read_text(encoding='utf-8'))
|
|
88
|
-
except Exception:
|
|
89
|
-
fm = {}
|
|
90
|
-
files.append((md, 'concept', fm))
|
|
91
|
-
|
|
92
|
-
if search_type in ('all', 'note'):
|
|
93
|
-
vault_path = Path(vault)
|
|
94
|
-
notes_dir = vault_path / 'Notes'
|
|
95
|
-
if notes_dir.is_dir():
|
|
96
|
-
for md in notes_dir.rglob('*.md'):
|
|
97
|
-
try:
|
|
98
|
-
fm = parse_fm(md.read_text(encoding='utf-8'))
|
|
99
|
-
except Exception:
|
|
100
|
-
fm = {}
|
|
101
|
-
files.append((md, 'note', fm))
|
|
102
|
-
|
|
103
|
-
return files
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
def main():
|
|
107
|
-
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
|
|
108
|
-
parser = argparse.ArgumentParser(description='Vault 全局搜索')
|
|
109
|
-
parser.add_argument('query', nargs='?', default='', help='搜索关键词')
|
|
110
|
-
parser.add_argument('--top-k', default='10', help='返回数量')
|
|
111
|
-
parser.add_argument('--type', default='all', choices=['all', 'article', 'concept', 'note'])
|
|
112
|
-
parser.add_argument('--days', type=int, default=90, help='搜索天数范围')
|
|
113
|
-
parser.add_argument('--vault', default=DEFAULT_VAULT, help='Vault 路径')
|
|
114
|
-
parser.add_argument('--biz-daily', default=DEFAULT_BIZ, help='biz-daily 路径')
|
|
115
|
-
parser.add_argument('--json', action='store_true', help='JSON 输出')
|
|
116
|
-
args = parser.parse_args()
|
|
117
|
-
|
|
118
|
-
if not args.query:
|
|
119
|
-
print('请提供搜索关键词')
|
|
120
|
-
sys.exit(1)
|
|
121
|
-
|
|
122
|
-
files = collect_files(args.vault, args.biz_daily, args.type, args.days)
|
|
123
|
-
results = search_text(args.query, files, int(args.top_k))
|
|
124
|
-
|
|
125
|
-
if args.json:
|
|
126
|
-
print(json.dumps(results, ensure_ascii=False, indent=2))
|
|
127
|
-
return
|
|
128
|
-
|
|
129
|
-
print(f'🔍 搜索 "{args.query}" 找到 {len(results)} 条 (类型: {args.type}, {args.days}天内)\n')
|
|
130
|
-
for i, r in enumerate(results):
|
|
131
|
-
icon = {'article': '📄', 'concept': '🧠', 'note': '📝'}.get(r['category'], '📎')
|
|
132
|
-
num = str(i + 1).rjust(2)
|
|
133
|
-
print(f'{num}. {icon} {r["title"] or r["file"].split("/")[-1]}')
|
|
134
|
-
print(f' {r["category"]} | 相关度: {r["score"]}')
|
|
135
|
-
snippet = re.sub(r'\s+', ' ', r['snippet'][:120])
|
|
136
|
-
print(f' {snippet}')
|
|
137
|
-
print()
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if __name__ == '__main__':
|
|
141
|
-
main()
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Vault 全局搜索 — 关键词 + AI 排序,覆盖文章/概念/笔记。
|
|
4
|
+
|
|
5
|
+
用法:
|
|
6
|
+
python scripts/vault_search.py "遥感反演" --top-k 10
|
|
7
|
+
python scripts/vault_search.py "空气污染" --type article --days 30
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import sys, os, json, argparse, re
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from datetime import datetime, timedelta
|
|
13
|
+
|
|
14
|
+
SCRIPTS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
15
|
+
PROJECT_ROOT = os.path.dirname(SCRIPTS_DIR)
|
|
16
|
+
DEFAULT_VAULT = os.path.join(PROJECT_ROOT, 'output', 'wechat-vault')
|
|
17
|
+
DEFAULT_BIZ = os.path.join(PROJECT_ROOT, 'output', 'biz-daily')
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def search_text(query: str, files: list[tuple], top_k: int) -> list[dict]:
|
|
21
|
+
"""关键词搜索 + 简单排序。"""
|
|
22
|
+
terms = query.lower().split()
|
|
23
|
+
scored = []
|
|
24
|
+
for filepath, category, meta in files:
|
|
25
|
+
try:
|
|
26
|
+
text = filepath.read_text(encoding='utf-8')[:2000]
|
|
27
|
+
except Exception:
|
|
28
|
+
continue
|
|
29
|
+
text_lower = text.lower()
|
|
30
|
+
# 计分:标题匹配 > 关键词频次
|
|
31
|
+
score = 0
|
|
32
|
+
title = meta.get('title', '')
|
|
33
|
+
if any(t in title.lower() for t in terms):
|
|
34
|
+
score += 10
|
|
35
|
+
for t in terms:
|
|
36
|
+
score += text_lower.count(t)
|
|
37
|
+
if score > 0:
|
|
38
|
+
scored.append({'file': str(filepath), 'category': category, 'title': title,
|
|
39
|
+
'score': score, 'snippet': text[:300]})
|
|
40
|
+
scored.sort(key=lambda x: x['score'], reverse=True)
|
|
41
|
+
return scored[:top_k]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def collect_files(vault: str, biz_daily: str, search_type: str, days: int) -> list[tuple]:
|
|
45
|
+
"""收集可搜索文件。"""
|
|
46
|
+
files = []
|
|
47
|
+
cutoff = datetime.now() - timedelta(days=days)
|
|
48
|
+
|
|
49
|
+
def parse_fm(text):
|
|
50
|
+
if not text.startswith('---'):
|
|
51
|
+
return {}
|
|
52
|
+
end = text.find('---', 3)
|
|
53
|
+
if end == -1:
|
|
54
|
+
return {}
|
|
55
|
+
fm = {}
|
|
56
|
+
for line in text[3:end].strip().split('\n'):
|
|
57
|
+
if ':' in line:
|
|
58
|
+
k, _, v = line.partition(':')
|
|
59
|
+
fm[k.strip()] = v.strip().strip('"').strip("'")
|
|
60
|
+
return fm
|
|
61
|
+
|
|
62
|
+
if search_type in ('all', 'article'):
|
|
63
|
+
# biz-daily 文章
|
|
64
|
+
biz_dir = Path(biz_daily)
|
|
65
|
+
for date_dir in sorted(biz_dir.glob('20*'), reverse=True):
|
|
66
|
+
try:
|
|
67
|
+
dir_date = datetime.strptime(date_dir.name, '%Y-%m-%d')
|
|
68
|
+
if dir_date < cutoff:
|
|
69
|
+
continue
|
|
70
|
+
except ValueError:
|
|
71
|
+
continue
|
|
72
|
+
for md in date_dir.rglob('*.md'):
|
|
73
|
+
if md.name == 'README.md':
|
|
74
|
+
continue
|
|
75
|
+
try:
|
|
76
|
+
fm = parse_fm(md.read_text(encoding='utf-8'))
|
|
77
|
+
except Exception:
|
|
78
|
+
fm = {}
|
|
79
|
+
files.append((md, 'article', fm))
|
|
80
|
+
|
|
81
|
+
if search_type in ('all', 'concept'):
|
|
82
|
+
vault_path = Path(vault)
|
|
83
|
+
concepts_dir = vault_path / 'Wiki' / 'Concepts'
|
|
84
|
+
if concepts_dir.is_dir():
|
|
85
|
+
for md in concepts_dir.glob('*.md'):
|
|
86
|
+
try:
|
|
87
|
+
fm = parse_fm(md.read_text(encoding='utf-8'))
|
|
88
|
+
except Exception:
|
|
89
|
+
fm = {}
|
|
90
|
+
files.append((md, 'concept', fm))
|
|
91
|
+
|
|
92
|
+
if search_type in ('all', 'note'):
|
|
93
|
+
vault_path = Path(vault)
|
|
94
|
+
notes_dir = vault_path / 'Notes'
|
|
95
|
+
if notes_dir.is_dir():
|
|
96
|
+
for md in notes_dir.rglob('*.md'):
|
|
97
|
+
try:
|
|
98
|
+
fm = parse_fm(md.read_text(encoding='utf-8'))
|
|
99
|
+
except Exception:
|
|
100
|
+
fm = {}
|
|
101
|
+
files.append((md, 'note', fm))
|
|
102
|
+
|
|
103
|
+
return files
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def main():
|
|
107
|
+
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
|
|
108
|
+
parser = argparse.ArgumentParser(description='Vault 全局搜索')
|
|
109
|
+
parser.add_argument('query', nargs='?', default='', help='搜索关键词')
|
|
110
|
+
parser.add_argument('--top-k', default='10', help='返回数量')
|
|
111
|
+
parser.add_argument('--type', default='all', choices=['all', 'article', 'concept', 'note'])
|
|
112
|
+
parser.add_argument('--days', type=int, default=90, help='搜索天数范围')
|
|
113
|
+
parser.add_argument('--vault', default=DEFAULT_VAULT, help='Vault 路径')
|
|
114
|
+
parser.add_argument('--biz-daily', default=DEFAULT_BIZ, help='biz-daily 路径')
|
|
115
|
+
parser.add_argument('--json', action='store_true', help='JSON 输出')
|
|
116
|
+
args = parser.parse_args()
|
|
117
|
+
|
|
118
|
+
if not args.query:
|
|
119
|
+
print('请提供搜索关键词')
|
|
120
|
+
sys.exit(1)
|
|
121
|
+
|
|
122
|
+
files = collect_files(args.vault, args.biz_daily, args.type, args.days)
|
|
123
|
+
results = search_text(args.query, files, int(args.top_k))
|
|
124
|
+
|
|
125
|
+
if args.json:
|
|
126
|
+
print(json.dumps(results, ensure_ascii=False, indent=2))
|
|
127
|
+
return
|
|
128
|
+
|
|
129
|
+
print(f'🔍 搜索 "{args.query}" 找到 {len(results)} 条 (类型: {args.type}, {args.days}天内)\n')
|
|
130
|
+
for i, r in enumerate(results):
|
|
131
|
+
icon = {'article': '📄', 'concept': '🧠', 'note': '📝'}.get(r['category'], '📎')
|
|
132
|
+
num = str(i + 1).rjust(2)
|
|
133
|
+
print(f'{num}. {icon} {r["title"] or r["file"].split("/")[-1]}')
|
|
134
|
+
print(f' {r["category"]} | 相关度: {r["score"]}')
|
|
135
|
+
snippet = re.sub(r'\s+', ' ', r['snippet'][:120])
|
|
136
|
+
print(f' {snippet}')
|
|
137
|
+
print()
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
if __name__ == '__main__':
|
|
141
|
+
main()
|