feat: 添加自然语言触发支持
- 支持'保存到知识库'/'订阅到知识库'触发词 - 自动检测链接并处理 - 更新SKILL.md文档
This commit is contained in:
@@ -4,6 +4,10 @@ Auto-trigger handler for obsidian-kb skill.
|
||||
|
||||
Detects video/article links in user messages and automatically processes them.
|
||||
Supports Bilibili, YouTube, and Zhihu.
|
||||
|
||||
## 触发词
|
||||
- "保存到知识库" + 链接 → 添加到收件箱并处理
|
||||
- "订阅到知识库" + 链接 → 添加订阅
|
||||
"""
|
||||
|
||||
import re
|
||||
@@ -41,6 +45,12 @@ PATTERNS = {
|
||||
'zhihu_answer': re.compile(r'https?://(?:www\.)?zhihu\.com/question/\d+/answer/(\d+)'),
|
||||
}
|
||||
|
||||
# 触发词模式
|
||||
TRIGGER_PATTERNS = {
|
||||
'save': re.compile(r'(保存到知识库|加入知识库|添加到知识库)', re.IGNORECASE),
|
||||
'subscribe': re.compile(r'(订阅到知识库|订阅知识库|关注到知识库)', re.IGNORECASE),
|
||||
}
|
||||
|
||||
|
||||
def detect_links(text: str) -> List[Dict[str, str]]:
|
||||
"""
|
||||
@@ -196,6 +206,96 @@ def get_action_prompt(detected: List[Dict[str, str]]) -> str:
|
||||
return f"是否将上述 {count} 个内容添加到收件箱?(y/n)"
|
||||
|
||||
|
||||
def detect_triggers(text: str) -> Optional[str]:
|
||||
"""
|
||||
检测用户消息中的触发词。
|
||||
|
||||
Args:
|
||||
text: 用户消息
|
||||
|
||||
Returns:
|
||||
'save' | 'subscribe' | None
|
||||
"""
|
||||
# 检测"保存到知识库"
|
||||
if TRIGGER_PATTERNS['save'].search(text):
|
||||
return 'save'
|
||||
|
||||
# 检测"订阅到知识库"
|
||||
if TRIGGER_PATTERNS['subscribe'].search(text):
|
||||
return 'subscribe'
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def handle_save_to_kb(detected: List[Dict[str, str]]) -> str:
|
||||
"""
|
||||
处理"保存到知识库"命令:将链接添加到收件箱并处理。
|
||||
|
||||
Args:
|
||||
detected: 检测到的链接列表
|
||||
|
||||
Returns:
|
||||
处理结果消息
|
||||
"""
|
||||
if not detected:
|
||||
return "❌ 未检测到任何链接,请提供要保存的链接"
|
||||
|
||||
results = []
|
||||
for item in detected:
|
||||
if item['is_short']:
|
||||
results.append(f"⚠️ 短链接无法处理: {item['url']}")
|
||||
continue
|
||||
|
||||
# 调用保存处理
|
||||
success, msg = process_link(item['url'], item['platform'], auto_process=True)
|
||||
results.append(msg)
|
||||
|
||||
return "\n".join(results)
|
||||
|
||||
|
||||
def handle_subscribe_to_kb(detected: List[Dict[str, str]]) -> str:
|
||||
"""
|
||||
处理"订阅到知识库"命令:将链接添加到订阅列表。
|
||||
|
||||
Args:
|
||||
detected: 检测到的链接列表
|
||||
|
||||
Returns:
|
||||
处理结果消息
|
||||
"""
|
||||
if not detected:
|
||||
return "❌ 未检测到任何链接,请提供要订阅的链接"
|
||||
|
||||
results = []
|
||||
for item in detected:
|
||||
if item['platform'] != 'bilibili':
|
||||
results.append(f"⚠️ 暂不支持订阅 {item['platform']}: {item['url']}")
|
||||
continue
|
||||
|
||||
if item['is_short']:
|
||||
results.append(f"⚠️ 短链接无法订阅: {item['url']}")
|
||||
continue
|
||||
|
||||
try:
|
||||
# 调用订阅命令
|
||||
import subprocess
|
||||
cmd = [
|
||||
sys.executable,
|
||||
os.path.join(os.path.dirname(__file__), 'kb_workflow.py'),
|
||||
'--trigger', 'subscriptions',
|
||||
'--add', 'bilibili', 'video', item['id'], item.get('url', '')
|
||||
]
|
||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||
if result.returncode == 0:
|
||||
results.append(f"✅ 已订阅: {item['id']}")
|
||||
else:
|
||||
results.append(f"❌ 订阅失败: {result.stderr}")
|
||||
except Exception as e:
|
||||
results.append(f"❌ 订阅失败: {e}")
|
||||
|
||||
return "\n".join(results)
|
||||
|
||||
|
||||
def should_auto_process(text: str) -> bool:
|
||||
"""
|
||||
Check if user wants to auto-process based on their message.
|
||||
@@ -326,21 +426,45 @@ def handle_message(text: str) -> Optional[str]:
|
||||
"""
|
||||
Main entry point: handle incoming message and return response.
|
||||
|
||||
## 触发条件
|
||||
- 检测到"保存到知识库"/"订阅到知识库"触发词 + 链接 → 自动处理
|
||||
- 仅检测到链接 → 询问用户是否处理
|
||||
|
||||
Args:
|
||||
text: User's message text
|
||||
|
||||
Returns:
|
||||
Response message, or None if no action needed
|
||||
"""
|
||||
# First, check for short links and provide help
|
||||
has_short, short_msg = detect_and_format(text)
|
||||
# 首先检测触发词
|
||||
trigger = detect_triggers(text)
|
||||
|
||||
# Detect all links (including short ones)
|
||||
# 检测链接
|
||||
detected = detect_links(text)
|
||||
|
||||
if not detected:
|
||||
# 如果没有链接,也没有触发词,返回 None
|
||||
if not detected and not trigger:
|
||||
return None
|
||||
|
||||
# 如果有触发词(save/subscribe),但没有链接
|
||||
if trigger and not detected:
|
||||
if trigger == 'save':
|
||||
return "请提供要保存到知识库的链接"
|
||||
elif trigger == 'subscribe':
|
||||
return "请提供要订阅到知识库的链接"
|
||||
|
||||
# 处理"保存到知识库"触发
|
||||
if trigger == 'save':
|
||||
return handle_save_to_kb(detected)
|
||||
|
||||
# 处理"订阅到知识库"触发
|
||||
if trigger == 'subscribe':
|
||||
return handle_subscribe_to_kb(detected)
|
||||
|
||||
# 以下是原有的仅链接检测逻辑
|
||||
# First, check for short links and provide help
|
||||
has_short, short_msg = detect_and_format(text)
|
||||
|
||||
# If we have short links, show help message first
|
||||
if has_short:
|
||||
return short_msg
|
||||
|
||||
Reference in New Issue
Block a user