從 Local REST API 到 Agent SDK,從 Notion 雙向同步到本地 Gemma-4 寫作引擎——完整的代碼範例、自動化流程與架構設計。 From Local REST API to Agent SDK, from bidirectional Notion sync to a local Gemma-4 writing engine — complete code examples, automation pipelines, and architecture diagrams.
git config --global core.hooksPath ~/.config/git/hooks
然後將 post-commit 腳本放入 ~/.config/git/hooks/post-commit,
即可對所有 git 倉庫生效,無需每個倉庫單獨設定。
Run git config --global core.hooksPath ~/.config/git/hooks
then place the post-commit script in ~/.config/git/hooks/post-commit.
This applies to every git repository without per-repo setup.
http://192.168.1.235:1234/v1,
使用 OpenAI 相容格式。所有 OpenAI SDK 客戶端只需修改 baseURL 即可接入。
LM Studio runs at http://192.168.1.235:1234/v1 with an OpenAI-compatible API. Any OpenAI SDK client only needs to change baseURL to connect.
| 方法Method | 用途Purpose | 參數Params | 類型Type |
|---|---|---|---|
write(prompt, options) |
根據提示生成文章/段落支援 formal / casual / academic / creative 語氣 Generate articles or paragraphs from a promptSupports formal / casual / academic / creative tone | tone maxTokens language |
LOCAL |
translate(text, targetLang) |
翻譯文字到目標語言技術術語保留原文並附翻譯 Translate text to target languageTechnical terms are preserved with translation | targetLang sourceLang |
LOCAL |
summarize(text, options) |
生成文字摘要支援 paragraph / bullets / tldr 格式 Generate text summariesSupports paragraph / bullets / tldr formats | length format |
LOCAL |
improve(text, instruction) |
潤色改進文字提升流暢度、清晰度,保持原意 Polish and improve textEnhances fluency and clarity while preserving intent | instruction |
LOCAL |
writeStream(prompt) |
串流生成長文AsyncGenerator,適合即時顯示 Stream-generate long contentAsyncGenerator — ideal for real-time display | prompt |
LOCAL |
healthCheck() |
檢查 LM Studio 連線狀態確認 gemma-4-e4b 模型已載入 Check LM Studio connectionConfirms gemma-4-e4b model is loaded | — | UTILS |
translateNote(path, lang) |
AI 翻譯整份 Obsidian 筆記輸出至 {原名}-{語言}.md AI-translate an entire Obsidian noteOutputs to {original-name}-{lang}.md | notePath targetLang |
INTEGRATION |
summarizeFolder(folder, output) |
批次摘要整個資料夾生成統一的摘要索引文件 Batch-summarise an entire folderGenerates a unified summary index file | folder outputNote |
INTEGRATION |
import Anthropic from '@anthropic-ai/sdk';
import { OpenAI } from 'openai';
// ── 連接兩個 AI 層 / Connect both AI layers ──────────────
const claude = new Anthropic(); // Agent SDK (claude-sonnet-4-6)
const gemma = new OpenAI({ // 本地 Gemma-4 / Local Gemma-4
baseURL: 'http://192.168.1.235:1234/v1',
apiKey: 'lm-studio',
});
// ── Agentic Loop ─────────────────────────────────────────
async function runAgent(task: string) {
const messages = [{ role: 'user', content: task }];
while (true) {
const res = await claude.messages.create({
model: 'claude-sonnet-4-6',
max_tokens: 8096,
tools: obsidianTools,
messages,
});
messages.push({ role: 'assistant', content: res.content });
if (res.stop_reason === 'end_turn') break;
// 執行工具呼叫 / Execute tool calls
const results = await executeTools(res.content);
messages.push({ role: 'user', content: results });
}
}
// ── Gemma-4 寫作 SDK / Writing SDK ───────────────────────
async function summarize(text: string) {
const r = await gemma.chat.completions.create({
model: 'google/gemma-4-e4b',
messages: [
{ role: 'system', content: '用繁體中文生成100字內摘要' },
{ role: 'user', content: text },
],
temperature: 0.3,
});
return r.choices[0].message.content;
}
#!/bin/bash
# 每次 git commit 後自動更新 Obsidian Dev Logs
# Auto-update Obsidian Dev Logs after every git commit
COMMIT_HASH=$(git rev-parse HEAD)
COMMIT_MSG=$(git log -1 --pretty=%B)
REPO=$(basename $(git rev-parse --show-toplevel))
CHANGED=$(git diff-tree --no-commit-id -r --name-only HEAD)
# 1. 寫入 Obsidian Dev Logs(REST API)/ Write via REST API
curl -sk -X POST \
"https://127.0.0.1:27124/vault/Dev%20Logs%2F${REPO}.md" \
-H "Authorization: Bearer ${OBSIDIAN_API_KEY}" \
-H "Content-Type: text/markdown" \
--data-raw "
## $(date '+%Y-%m-%d %H:%M') | ${COMMIT_HASH:0:7}
**${COMMIT_MSG}**
$(echo "$CHANGED" | awk '{print "- " \$0}')
"
# 2. 背景觸發 Agent SDK / Trigger Agent SDK in background
node ~/.obsidian-agent/post-commit.js \
--repo "${REPO}" --hash "${COMMIT_HASH:0:7}" &
echo "📝 Obsidian dev log updated"