267 lines
6.9 KiB
Markdown
267 lines
6.9 KiB
Markdown
# Obsidian 集成指南
|
||||
|
|
|
|||
|
|
本指南说明如何在 Obsidian 中集成和使用日记整理 Agent。
|
|||
|
|
|
|||
|
|
## 前置条件
|
|||
|
|
|
|||
|
|
1. 已安装 Obsidian
|
|||
|
|
2. 已安装 `Local REST API` 插件并配置 API 密钥
|
|||
|
|
3. 已安装并配置日记整理 Agent
|
|||
|
|
4. 已安装 Obsidian 的 `Templater` 或 `QuickAdd` 插件(用于触发脚本)
|
|||
|
|
|
|||
|
|
## 方案 1: 使用 Templater 插件
|
|||
|
|
|
|||
|
|
### 步骤 1: 安装 Templater 插件
|
|||
|
|
|
|||
|
|
1. 在 Obsidian 中,进入 `设置` > `第三方插件`
|
|||
|
|
2. 关闭 `安全模式`
|
|||
|
|
3. 点击 `浏览社区插件`,搜索 `Templater` 并安装
|
|||
|
|
4. 启用 Templater 插件
|
|||
|
|
|
|||
|
|
### 步骤 2: 创建模板
|
|||
|
|
|
|||
|
|
1. 在 Obsidian 中创建一个新的笔记,例如 `Templates/OrganizeJournal.md`
|
|||
|
|
2. 在笔记中添加以下内容:
|
|||
|
|
|
|||
|
|
```javascript
|
|||
|
|
<%*
|
|||
|
|
// 获取当前笔记的标题(假设为日期格式)
|
|||
|
|
const noteTitle = tp.file.title;
|
|||
|
|
const agentPath = "/path/to/journal_organizer";
|
|||
|
|
|
|||
|
|
// 构建命令
|
|||
|
|
const command = `cd ${agentPath} && /path/to/journal_venv/bin/python -m journal_organizer organize --date ${noteTitle}`;
|
|||
|
|
|
|||
|
|
// 执行命令
|
|||
|
|
try {
|
|||
|
|
const result = await tp.system.exec(command);
|
|||
|
|
const output = JSON.parse(result);
|
|||
|
|
|
|||
|
|
if (output.success) {
|
|||
|
|
tp.obsidian.Notice.show(`✓ 成功整理日记 ${noteTitle}\n已生成 ${output.data.successful_writes} 个文件`);
|
|||
|
|
} else {
|
|||
|
|
tp.obsidian.Notice.show(`✗ 整理失败: ${output.message}`);
|
|||
|
|
}
|
|||
|
|
} catch (error) {
|
|||
|
|
tp.obsidian.Notice.show(`✗ 执行错误: ${error.message}`);
|
|||
|
|
}
|
|||
|
|
%>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
3. 根据您的环境修改 `agentPath` 和虚拟环境路径
|
|||
|
|
|
|||
|
|
### 步骤 3: 创建快捷键
|
|||
|
|
|
|||
|
|
1. 进入 `设置` > `快捷键`
|
|||
|
|
2. 搜索 `Templater: Open Insert Template modal`
|
|||
|
|
3. 为其分配一个快捷键,例如 `Ctrl+Alt+O`
|
|||
|
|
|
|||
|
|
### 步骤 4: 使用
|
|||
|
|
|
|||
|
|
1. 打开或创建一个日记笔记(文件名应为日期格式,如 `2025-12-31.md`)
|
|||
|
|
2. 按下快捷键打开模板选择器
|
|||
|
|
3. 选择 `OrganizeJournal` 模板
|
|||
|
|
4. Agent 将自动执行并整理日记
|
|||
|
|
|
|||
|
|
## 方案 2: 使用 QuickAdd 插件
|
|||
|
|
|
|||
|
|
### 步骤 1: 安装 QuickAdd 插件
|
|||
|
|
|
|||
|
|
1. 在 Obsidian 中进入 `设置` > `第三方插件`
|
|||
|
|
2. 点击 `浏览社区插件`,搜索 `QuickAdd` 并安装
|
|||
|
|
3. 启用 QuickAdd 插件
|
|||
|
|
|
|||
|
|
### 步骤 2: 创建宏
|
|||
|
|
|
|||
|
|
1. 打开 QuickAdd 插件设置
|
|||
|
|
2. 点击 `Manage Macros`
|
|||
|
|
3. 创建一个新的宏,例如 `OrganizeJournal`
|
|||
|
|
4. 在宏中添加以下步骤:
|
|||
|
|
- 类型:`User Script`
|
|||
|
|
- 脚本内容:
|
|||
|
|
|
|||
|
|
```javascript
|
|||
|
|
module.exports = async (params) => {
|
|||
|
|
const { app } = params;
|
|||
|
|
const noteTitle = app.workspace.getActiveFile()?.basename;
|
|||
|
|
|
|||
|
|
if (!noteTitle) {
|
|||
|
|
new Notice("请先打开一个笔记");
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const agentPath = "/path/to/journal_organizer";
|
|||
|
|
const command = `cd ${agentPath} && /path/to/journal_venv/bin/python -m journal_organizer organize --date ${noteTitle}`;
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
const result = await require('child_process').execSync(command, { encoding: 'utf-8' });
|
|||
|
|
const output = JSON.parse(result);
|
|||
|
|
|
|||
|
|
if (output.success) {
|
|||
|
|
new Notice(`✓ 成功整理日记 ${noteTitle}`);
|
|||
|
|
} else {
|
|||
|
|
new Notice(`✗ 整理失败: ${output.message}`);
|
|||
|
|
}
|
|||
|
|
} catch (error) {
|
|||
|
|
new Notice(`✗ 执行错误: ${error.message}`);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 步骤 3: 创建快捷键
|
|||
|
|
|
|||
|
|
1. 在 QuickAdd 设置中,为宏分配一个快捷键
|
|||
|
|
|
|||
|
|
### 步骤 4: 使用
|
|||
|
|
|
|||
|
|
1. 打开一个日记笔记
|
|||
|
|
2. 按下快捷键执行宏
|
|||
|
|
3. Agent 将自动整理日记
|
|||
|
|
|
|||
|
|
## 方案 3: 使用 Shell 命令(高级)
|
|||
|
|
|
|||
|
|
如果您熟悉 Shell 脚本,可以创建一个更复杂的集成方案:
|
|||
|
|
|
|||
|
|
### 创建 Shell 脚本
|
|||
|
|
|
|||
|
|
在 `/home/ubuntu/organize_journal.sh` 中创建以下脚本:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
#!/bin/bash
|
|||
|
|
|
|||
|
|
# 日记整理 Agent 调用脚本
|
|||
|
|
|
|||
|
|
AGENT_PATH="/path/to/journal_organizer"
|
|||
|
|
VENV_PATH="/path/to/journal_venv"
|
|||
|
|
DATE="${1:-$(date +%Y-%m-%d)}"
|
|||
|
|
|
|||
|
|
# 激活虚拟环境并运行 Agent
|
|||
|
|
source "${VENV_PATH}/bin/activate"
|
|||
|
|
cd "${AGENT_PATH}"
|
|||
|
|
|
|||
|
|
# 执行 Agent
|
|||
|
|
python -m journal_organizer organize --date "${DATE}"
|
|||
|
|
|
|||
|
|
# 返回状态
|
|||
|
|
exit $?
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 在 Obsidian 中调用
|
|||
|
|
|
|||
|
|
在 Templater 或 QuickAdd 中使用以下命令:
|
|||
|
|
|
|||
|
|
```javascript
|
|||
|
|
const result = await tp.system.exec("/home/ubuntu/organize_journal.sh 2025-12-31");
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 故障排除
|
|||
|
|
|
|||
|
|
### 问题 1: "找不到 python 模块"
|
|||
|
|
|
|||
|
|
**解决方案**:确保虚拟环境路径正确,并且依赖已安装。
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
/path/to/journal_venv/bin/pip list | grep anthropic
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 问题 2: "API 密钥错误"
|
|||
|
|
|
|||
|
|
**解决方案**:检查配置文件中的 API 密钥是否正确。
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
cat ~/.journal_organizer/config.yaml | grep api_key
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 问题 3: "找不到日记文件"
|
|||
|
|
|
|||
|
|
**解决方案**:确保日记文件名格式正确(YYYY-MM-DD.md),并且在配置的文件夹中。
|
|||
|
|
|
|||
|
|
### 问题 4: "权限被拒绝"
|
|||
|
|
|
|||
|
|
**解决方案**:确保脚本有执行权限。
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
chmod +x /home/ubuntu/organize_journal.sh
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 高级配置
|
|||
|
|
|
|||
|
|
### 定时自动整理
|
|||
|
|
|
|||
|
|
您可以使用系统的 cron 任务来定时运行 Agent:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 编辑 crontab
|
|||
|
|
crontab -e
|
|||
|
|
|
|||
|
|
# 添加以下行,每天晚上 10 点运行
|
|||
|
|
0 22 * * * /home/ubuntu/organize_journal.sh $(date +\%Y-\%m-\%d)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 监听文件变化
|
|||
|
|
|
|||
|
|
使用 `watchdog` 库来监听日记文件的变化,并自动触发整理:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
import time
|
|||
|
|
from watchdog.observers import Observer
|
|||
|
|
from watchdog.events import FileSystemEventHandler
|
|||
|
|
import subprocess
|
|||
|
|
|
|||
|
|
class JournalHandler(FileSystemEventHandler):
|
|||
|
|
def on_modified(self, event):
|
|||
|
|
if event.src_path.endswith('.md'):
|
|||
|
|
# 延迟 5 秒后执行,避免频繁触发
|
|||
|
|
time.sleep(5)
|
|||
|
|
subprocess.run(['/home/ubuntu/organize_journal.sh'])
|
|||
|
|
|
|||
|
|
observer = Observer()
|
|||
|
|
observer.schedule(JournalHandler(), path='/path/to/Daily', recursive=False)
|
|||
|
|
observer.start()
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
while True:
|
|||
|
|
time.sleep(1)
|
|||
|
|
except KeyboardInterrupt:
|
|||
|
|
observer.stop()
|
|||
|
|
observer.join()
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 最佳实践
|
|||
|
|
|
|||
|
|
1. **定期备份**:在整理前备份您的 Obsidian vault,以防万一。
|
|||
|
|
2. **测试配置**:在正式使用前,用一个测试日记进行测试。
|
|||
|
|
3. **监控日志**:定期检查 Agent 的日志文件,了解执行情况。
|
|||
|
|
4. **逐步扩展**:先用基础功能,然后根据需要添加更多的 Skills 和 Commands。
|
|||
|
|
5. **保护密钥**:不要在公开的地方暴露 API 密钥,使用环境变量或安全的配置管理工具。
|
|||
|
|
|
|||
|
|
## 常见场景
|
|||
|
|
|
|||
|
|
### 场景 1: 每天晚上自动整理
|
|||
|
|
|
|||
|
|
使用 cron 任务在每天晚上 10 点自动运行 Agent:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
0 22 * * * /home/ubuntu/organize_journal.sh
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 场景 2: 手动触发整理
|
|||
|
|
|
|||
|
|
在 Obsidian 中创建一个快捷键,按需整理日记。
|
|||
|
|
|
|||
|
|
### 场景 3: 批量整理历史日记
|
|||
|
|
|
|||
|
|
使用以下命令整理指定日期范围内的所有日记:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
for date in {1..31}; do
|
|||
|
|
/home/ubuntu/organize_journal.sh "2025-12-$(printf "%02d" $date)"
|
|||
|
|
done
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 下一步
|
|||
|
|
|
|||
|
|
- 查看 [README.md](README.md) 了解更多关于 Agent 的信息
|
|||
|
|
- 查看 [config.example.yaml](config.example.yaml) 了解配置选项
|
|||
|
|
- 根据需要扩展 Agent 功能,添加新的 Skills 和 Commands
|