Install
openclaw skills install jueduilunhui-siyuan-noteClawHub Security found sensitive or high-impact capabilities. Review the scan results before using.
思源笔记读写技能,提供与思源笔记(SiYuan Note)进行交互的能力,包括读取和写入笔记内容
openclaw skills install jueduilunhui-siyuan-note这个技能提供了与思源笔记(SiYuan Note)进行交互的能力,包括读取和写入笔记内容。基于思源笔记Chrome扩展最佳实践优化,支持真正的独立文档创建、文章剪藏、模板渲染等高级功能。
当用户提到"查询思源笔记"、"写入思源笔记"、"创建思源笔记"或类似请求时,使用此技能。
当用户提到以下关键词时激活此技能:
基于思源笔记Chrome扩展(siyuan-note/siyuan-chrome)的实践,增加了以下高级功能:
使用createDocWithMd API创建真正的独立文档,不再依赖在现有文档中追加内容:
from siyuan_note_enhanced import create_enhanced_client
client = create_enhanced_client()
doc_id = client.create_document(
"其他",
"我的文档",
"# 标题\n\n文档内容",
tags=["标签1", "标签2"]
)
# 返回: 独立文档ID,如 "20260217205120-3wcoxib"
# 文件: myDocument.sy
优势:
类似Chrome扩展的剪藏功能,支持完整的文章剪藏:
client.clip_article(
notebook_name="其他",
title="文章标题",
url="https://example.com/article",
content="文章的Markdown内容",
excerpt="文章摘要",
site_name="网站名称",
tags=["技术", "笔记"]
)
特性:
基于Chrome扩展的模板渲染引擎:
# 基本变量替换
template = "标题: ${title}\n时间: ${date}"
data = {'title': '测试', 'date': '2026-02-17'}
result = client.render_template(template, data)
# 结果: "标题: 测试\n时间: 2026-02-17"
# 条件表达式
template = "${show ? '显示' : '隐藏'}"
result = client.render_template(template, {'show': True})
# 结果: "显示"
# 嵌套属性
template = "用户: ${user.name}"
data = {'user': {'name': '张三'}}
result = client.render_template(template, data)
# 结果: "用户: 张三"
支持的功能:
${variable}${condition ? true : false}${user.name}${firstName + lastName}配置通过环境变量读取:
SIYUAN_API_URL - 思源笔记API地址,例如:http://localhost:6806SIYUAN_API_TOKEN - 你的API token(从思源笔记设置中获取)也可以使用配置文件,默认位置:~/.openclaw/workspace/siyuan-openchat-sync/config.json
{
"siyuan": {
"api_url": "http://localhost:6806",
"token": "your-api-token-here"
},
"sync": {
"notebook_name": "其他",
"document_name": "openchat",
"auto_sync": true
}
}
from siyuan_note import SiYuanNote
siyuan = SiYuanNote()
from siyuan_note_enhanced import create_enhanced_client
client = create_enhanced_client()
| 功能 | 基础版 | 增强版 |
|---|---|---|
| 独立文档创建 | ❌ 在现有文档中追加 | ✅ 创建真正的.sy文件 |
| 文章剪藏 | ❌ 不支持 | ✅ 完整的剪藏功能 |
| 模板渲染 | ❌ 基础格式化 | ✅ 强大的模板系统 |
| 错误处理 | ✅ 基础处理 | ✅ 增强的错误处理 |
| Chrome扩展兼容 | ❌ 不兼容 | ✅ 基于最佳实践 |
| 向后兼容 | ✅ 完全兼容 | ✅ 包含所有功能 |
from siyuan_note import SiYuanNote
siyuan = SiYuanNote()
if siyuan.test_connection():
print("连接成功")
else:
print("连接失败")
# 获取所有笔记本
notebooks = siyuan.get_notebooks()
print(f"找到 {len(notebooks)} 个笔记本")
# 获取指定笔记本的文档
notebook_id = siyuan.get_notebook_id("其他")
documents = siyuan.get_documents(notebook_id)
# 搜索特定内容
results = siyuan.search_content("关键词")
# 同步对话到思源笔记
conversation_data = {
'summary': '对话摘要',
'messages': [
{'role': 'user', 'content': '用户消息'},
{'role': 'assistant', 'content': '助手回复'}
],
'conclusion': '对话总结'
}
success = siyuan.sync_conversation(conversation_data)
if success:
print("同步成功")
else:
print("同步失败")
# 创建新文档
new_doc_id = siyuan.create_document(
notebook_name="其他",
document_name="新文档",
content="# 新文档\n\n这是新创建的文档内容。"
)
# 从环境变量自动读取(推荐)
from siyuan_note import SiYuanNote
siyuan = SiYuanNote()
# 或手动指定
siyuan = SiYuanNote(
api_url="http://localhost:6806",
token="your-api-token-here"
)
test_connection() - 测试API连接get_notebooks() - 获取所有笔记本get_notebook_id(notebook_name) - 获取笔记本IDget_documents(notebook_id) - 获取笔记本中的文档get_document_content(document_id) - 获取文档内容search_content(query, notebook_name=None) - 搜索内容create_document(notebook_name, document_name, content) - 创建新文档append_to_document(document_id, content) - 向文档追加内容sync_conversation(conversation_data, notebook_name="其他", document_name="openchat") - 同步对话load_config() - 加载配置save_config() - 保存配置update_config(new_config) - 更新配置conversation_data = {
'summary': '对话摘要',
'messages': [
{
'role': 'user', # 或 'assistant'
'content': '消息内容',
'timestamp': '可选时间戳'
}
],
'conclusion': '对话总结',
'metadata': {
'source': 'OpenChat',
'version': '1.0'
}
}
#!/usr/bin/env python3
"""
快速同步当前OpenChat对话到思源笔记
"""
import sys
import json
from siyuan_note import SiYuanNote
def main():
# 从命令行参数获取对话数据
if len(sys.argv) < 2:
print("用法: python sync_now.py '对话JSON数据'")
return
try:
conversation_data = json.loads(sys.argv[1])
except:
print("错误: 无效的JSON数据")
return
# 同步到思源笔记
siyuan = SiYuanNote()
success = siyuan.sync_conversation(conversation_data)
if success:
print("✅ 对话已同步到思源笔记")
else:
print("❌ 同步失败")
if __name__ == "__main__":
main()
#!/usr/bin/env python3
"""
批量同步历史对话到思源笔记
"""
import os
import json
from siyuan_note import SiYuanNote
def sync_history_conversations(history_dir="./conversations"):
siyuan = SiYuanNote()
if not os.path.exists(history_dir):
print(f"目录不存在: {history_dir}")
return
# 遍历所有JSON文件
for filename in os.listdir(history_dir):
if filename.endswith('.json'):
filepath = os.path.join(history_dir, filename)
try:
with open(filepath, 'r', encoding='utf-8') as f:
data = json.load(f)
# 提取对话数据
conversation_data = data.get('conversation', data)
print(f"同步: {filename}")
success = siyuan.sync_conversation(conversation_data)
if success:
print(f"✅ {filename} 同步成功")
else:
print(f"❌ {filename} 同步失败")
except Exception as e:
print(f"❌ 处理 {filename} 时出错: {e}")
if __name__ == "__main__":
sync_history_conversations()
连接失败
权限问题
同步失败
import logging
logging.basicConfig(level=logging.DEBUG)
siyuan = SiYuanNote()
siyuan.test_connection()
将此技能集成到OpenClaw的tools中,可以通过以下方式调用:
# 在OpenClaw会话中
from skills.siyuan_note.siyuan_note import SiYuanNote
# 初始化
siyuan = SiYuanNote()
# 查询思源笔记
if user_request == "查询思源笔记":
notebooks = siyuan.get_notebooks()
response = f"找到 {len(notebooks)} 个笔记本"
# 写入思源笔记
elif user_request == "写入思源笔记":
success = siyuan.sync_conversation(current_conversation)
response = "已同步到思源笔记" if success else "同步失败"
可以设置定时任务,自动将OpenClaw对话同步到思源笔记:
# 在heartbeat或cron任务中
def auto_sync_to_siyuan():
siyuan = SiYuanNote()
# 获取未同步的对话
unsynced_conversations = get_unsynced_conversations()
for conv in unsynced_conversations:
siyuan.sync_conversation(conv)
mark_as_synced(conv)
.env.example - 环境变量配置示例README.md - 完整文档CHANGELOG.md - 更新日志