Logseq AI
通过两个接口与Logseq进行AI驱动的交互:
- MCP服务器 -允许外部AI工具(Windsurf、Claude Desktop)与您的Logseq图进行交互
- 玲音 -Logseq UI中的嵌入式AI助手插件
“无论你去哪里,每个人都是相互联系的。”-莱恩系列实验
先决条件
- Node.js 18+
- 启用了HTTP API服务器的Logseq桌面应用程序(设置→ 特性→ HTTP API服务器)
设置
# Install dependencies
pnpm install
# Build all packages
pnpm build快速开始
使用核心库
import { LogseqClient, LogseqOperations } from "@logseq-ai/core";
// Create client and operations
const client = new LogseqClient({
baseUrl: "http://localhost:12315",
token: "your-api-token" // optional
});
const ops = new LogseqOperations(client);
// Search for pages
const results = await ops.search("meeting notes");
// Get page content as text
const content = await ops.getPageContent("My Notes");
// Create a new block
const block = await ops.createBlock({
pageName: "My Notes",
content: "New idea from AI!"
});
// Create multiple blocks with hierarchy (more efficient for structured content)
const result = await ops.createBlocks("My Notes", [
{
content: "## Project Overview",
children: [
{ content: "Goal: Build a new feature" },
{ content: "Timeline: Q1 2025" },
]
},
{
content: "## Tasks",
children: [
{ content: "Design the API" },
{ content: "Implement backend" },
{ content: "Write tests" },
]
}
]);
console.log(`Created ${result.created} blocks`);
// Run a Datalog query to find TODOs
const todos = await ops.query(`
[:find (pull ?b [*])
:where [?b :block/marker "TODO"]]
`);任务管理
import { LogseqClient, LogseqOperations } from "@logseq-ai/core";
const client = new LogseqClient();
const ops = new LogseqOperations(client);
// Get all active tasks
const tasks = await ops.getTasks();
// Get tasks from a specific page
const projectTasks = await ops.getTasks({ pageName: "Project A" });
// Create a new task with priority and deadline
const task = await ops.createTask({
pageName: "Project A",
content: "Review pull request",
priority: "A",
deadline: "2024-12-15"
});
// Mark task as in progress
await ops.markTask(task.uuid, "DOING");
// Complete the task
await ops.markTask(task.uuid, "DONE");
// Set/change priority
await ops.setTaskPriority(task.uuid, "B");
// Set scheduled date
await ops.setTaskScheduled({ uuid: task.uuid, date: "2024-12-10" });
// Remove deadline
await ops.setTaskDeadline({ uuid: task.uuid, date: null });任务分析
// Get overdue tasks
const overdue = await ops.getOverdueTasks();
console.log(`You have ${overdue.length} overdue tasks`);
// Get tasks due in the next 7 days
const dueSoon = await ops.getTasksDueSoon({ days: 7 });
// Get task statistics
const stats = await ops.getTaskStats();
console.log(`Total: ${stats.total}, Overdue: ${stats.overdue}`);
console.log(`By status:`, stats.byMarker);
// Search tasks by keyword
const reviewTasks = await ops.searchTasks({ query: "review", markers: ["TODO"] });日志操作
// Get or create today's journal
const today = await ops.getToday();
console.log(`Today's journal: ${today.page.name}`);
// Quick capture to today's journal
await ops.appendToToday("Meeting notes: discussed Q1 roadmap");
// Get recent journal entries
const journals = await ops.getRecentJournals({ days: 7, includeContent: true });
journals.forEach(j => console.log(`${j.date}: ${j.content.substring(0, 50)}...`));页面链接和反向链接
// Find pages related to a topic
const links = await ops.findRelatedPages("Project A");
console.log("Pages linking to Project A:", links.backlinks);
console.log("Pages Project A links to:", links.forwardLinks);
// Find blocks referencing a specific block
const backlinks = await ops.getBlockBacklinks("block-uuid-123");
console.log(`Found ${backlinks.backlinks.length} references`);错误处理
import {
LogseqOperations,
LogseqApiError,
LogseqNotFoundError,
isLogseqError
} from "@logseq-ai/core";
try {
await ops.getPageContent("Nonexistent Page");
} catch (error) {
if (error instanceof LogseqNotFoundError) {
console.log(`Page not found: ${error.identifier}`);
} else if (isLogseqError(error)) {
console.log(`Logseq error: ${error.toDetailedString()}`);
}
}包裹
@logseq-ai/core
用于Logseq API交互的共享库。MCP服务器和插件都使用。
主要出口:
LogseqClient-Logseq API的低级HTTP客户端LogseqOperations-具有错误处理功能的高级操作- 错误类别:
LogseqError,LogseqApiError,LogseqConnectionError,LogseqNotFoundError,LogseqValidationError - 类型定义:
Page,Block,SearchResult等等。
@logseq ai/mcp服务器
MCP服务器,将Logseq操作作为AI客户端的工具公开。
特征:
- 34个用于全面Logseq交互的工具
- 带有清晰错误消息的输入验证(由Zod提供支持)
- Logseq API错误的正确错误处理
# Build
pnpm --filter @logseq-ai/mcp-server build
# Run
LOGSEQ_API_TOKEN=your-token pnpm --filter @logseq-ai/mcp-server start
# Test
pnpm --filter @logseq-ai/mcp-server test在Windsurf/Claude桌面中配置
添加到MCP配置中:
{
"mcpServers": {
"logseq": {
"command": "node",
"args": ["/path/to/logseq-ai/packages/mcp-server/dist/index.js"],
"env": {
"LOGSEQ_API_URL": "http://localhost:12315",
"LOGSEQ_API_TOKEN": "your-token"
}
}
}
}可用工具
页面和块操作
| 工具 | 说明 |
|---|---|
search_logseq | 搜索页面和块 |
get_page | 以纯文本形式获取页面内容 |
get_pages | 批量获取多个页面(更高效) |
get_page_with_context | 获取带有反向链接和正向链接的页面 |
list_pages | 列出图表中的所有页面 |
create_page | 创建新页面(可选带块) |
delete_page | 删除页面 |
create_block | 创建单个块 |
create_blocks | 创建具有层次结构的多个块 |
update_block | 更新块的内容 |
delete_block | 删除块 |
query_logseq | 运行数据日志查询 |
update_page_properties | 更新现有页面上的属性 |
图形发现
| 工具 | 说明 |
|---|---|
get_current_graph | 获取当前图形信息 |
get_graph_stats | 获取图表统计信息(按类型、孤立项等分类的页面) |
find_missing_pages | 查找不存在的引用页面 |
find_orphan_pages | 查找没有传入链接的页面 |
find_pages_by_properties | 按属性值查找页面 |
find_related_pages | 查找反向链接和正向链接 |
get_block_backlinks | 查找引用块的块 |
日志操作
| 工具 | 说明 |
|---|---|
get_today | 获取今天的日志页面 |
append_to_today | 为今天的日记添加内容 |
get_recent_journals | 获取最近的日记条目 |
任务管理
| 工具 | 说明 |
|---|---|
get_tasks | 获取待办/正在执行的任务 |
create_task | 创建新任务 |
mark_task | 更改任务状态 |
mark_tasks | 更改多个任务的状态(批处理) |
search_tasks | 按关键字搜索任务 |
get_overdue_tasks | 获取超过截止日期的任务 |
get_tasks_due_soon | 获得N天内到期的任务 |
get_task_stats | 获取任务统计信息 |
set_task_priority | 设置任务优先级(A/B/C) |
set_task_deadline | 设置任务截止日期 |
set_task_scheduled | 设置任务计划日期 |
示例:创建结构化内容
创建包含多个部分的页面时,请使用 create_blocks 为了提高效率:
User: Create a page about Python with sections for Overview, Features, and Links
AI uses:
1. create_page("Python", "type:: #Technology\ntags:: #Programming")
2. create_blocks("Python", [
{
content: "## Overview",
children: [
{ content: "Python is a high-level programming language." }
]
},
{
content: "## Features",
children: [
{ content: "Dynamic typing" },
{ content: "Garbage collection" },
{ content: "Large standard library" }
]
},
{
content: "## Links",
children: " }
]
}
])logseq-行
Lain-Logseq的AI助手插件。
# Build
pnpm --filter logseq-lain build
# Development (watch mode)
pnpm dev:plugin在Logseq中安装
- 在Logseq中启用开发人员模式(设置→ 高级→ 开发者模式)
- 转到插件→ 加载解压缩的插件
- 选择
packages/logseq-plugin目录 - 使用
/lain ask,/lain summarize,/lain expand斜线命令
建筑
看 doc/architecture.md 获取详细的架构文档。
发展
# Build everything
pnpm build
# Run all tests (188 tests)
pnpm test
# Type check all packages
pnpm typecheck
# Lint
pnpm lint许可证
麻省理工学院
