triliumnext-mcp 0.3.10 → 0.3.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +150 -188
- package/build/index.js +7 -2
- package/build/modules/attributeListHandler.js +34 -0
- package/build/modules/attributeListManager.js +147 -0
- package/build/modules/attributeManager.js +36 -2
- package/build/modules/noteHandler.js +52 -8
- package/build/modules/noteManager.js +278 -49
- package/build/modules/searchManager.js +53 -1
- package/build/modules/searchQueryBuilder.js +33 -9
- package/build/modules/toolDefinitions.js +89 -20
- package/build/utils/contentIntegrity.js +173 -0
- package/build/utils/contentProcessor.js +3 -29
- package/build/utils/contentRules.js +266 -0
- package/build/utils/templateMapper.js +113 -0
- package/build/utils/validationUtils.js +7 -11
- package/package.json +1 -1
- package/build/utils/hashUtils.js +0 -95
package/README.md
CHANGED
|
@@ -1,188 +1,150 @@
|
|
|
1
|
-
# TriliumNext Notes' MCP Server
|
|
2
|
-
|
|
3
|
-
⚠️ **DISCLAIMER: This is a prototype for https://github.com/TriliumNext/Notes/issues/705. Suggested only for developer use. Please backup your Trilium notes before using this tool.** ⚠️
|
|
4
|
-
|
|
5
|
-
A model context protocol server for TriliumNext Notes. This server provides tools to interact with your Trilium Notes instance through MCP.
|
|
6
|
-
|
|
7
|
-
## Quick Start
|
|
8
|
-
|
|
9
|
-
Make sure to set up your environment variables first:
|
|
10
|
-
- `TRILIUM_API_URL` (default: http://localhost:8080/etapi)
|
|
11
|
-
- `TRILIUM_API_TOKEN` (required, get this from your Trilium Notes settings)
|
|
12
|
-
- `PERMISSIONS` (optional, default='READ;WRITE', where READ
|
|
13
|
-
- `VERBOSE` (optional, default='false',
|
|
14
|
-
|
|
15
|
-
## Installation
|
|
16
|
-
|
|
17
|
-
### 1. Using with Claude Desktop
|
|
18
|
-
|
|
19
|
-
Add the server config to your Claude Desktop configuration file:
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
"triliumnext-mcp"
|
|
69
|
-
|
|
70
|
-
"
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
"
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
- `
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
-
|
|
125
|
-
-
|
|
126
|
-
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
- "Create a new note called 'Weekly Review' in my journal folder"
|
|
152
|
-
|
|
153
|
-
> 📖 **More Examples**: See [User Query Examples](docs/user-query-examples.md) for comprehensive usage scenarios.
|
|
154
|
-
|
|
155
|
-
## Documentation
|
|
156
|
-
|
|
157
|
-
- [Content Modification Guide](docs/content-modification-guide.md) - Safe content editing with revision control
|
|
158
|
-
- [User Query Examples](docs/user-query-examples.md) - Natural language query examples
|
|
159
|
-
- [Search Query Examples](docs/search-examples/) - Advanced search syntax and filters
|
|
160
|
-
|
|
161
|
-
## Development
|
|
162
|
-
|
|
163
|
-
If you want to contribute or modify the server:
|
|
164
|
-
|
|
165
|
-
```bash
|
|
166
|
-
# Clone the repository
|
|
167
|
-
git clone https://github.com/tan-yong-sheng/triliumnext-mcp.git
|
|
168
|
-
|
|
169
|
-
# Install dependencies
|
|
170
|
-
npm install
|
|
171
|
-
|
|
172
|
-
# Build the server
|
|
173
|
-
npm run build
|
|
174
|
-
|
|
175
|
-
# For development with auto-rebuild
|
|
176
|
-
npm run watch
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
## Contributing
|
|
180
|
-
|
|
181
|
-
Contributions are welcome! If you are looking to improve the server, especially the search functionality, please familiarize yourself with the following resources:
|
|
182
|
-
|
|
183
|
-
- **Trilium Search DSL**: The [official documentation](https://triliumnext.github.io/Docs/Wiki/search.html) provides the foundation for all search queries.
|
|
184
|
-
- **Internal Search Implementation**: Our [Search Query Examples](docs/search-examples/) document details how `search_notes` parameters are translated into Trilium search strings. This is crucial for understanding and extending the current implementation.
|
|
185
|
-
|
|
186
|
-
Please feel free to open an issue or submit a pull request.
|
|
187
|
-
|
|
188
|
-
|
|
1
|
+
# TriliumNext Notes' MCP Server
|
|
2
|
+
|
|
3
|
+
⚠️ **DISCLAIMER: This is a prototype for https://github.com/TriliumNext/Notes/issues/705. Suggested only for developer use. Please backup your Trilium notes before using this tool.** ⚠️
|
|
4
|
+
|
|
5
|
+
A model context protocol server for TriliumNext Notes. This server provides tools to interact with your Trilium Notes instance through MCP.
|
|
6
|
+
|
|
7
|
+
## Quick Start
|
|
8
|
+
|
|
9
|
+
Make sure to set up your environment variables first:
|
|
10
|
+
- `TRILIUM_API_URL` (default: http://localhost:8080/etapi)
|
|
11
|
+
- `TRILIUM_API_TOKEN` (required, get this from your Trilium Notes settings)
|
|
12
|
+
- `PERMISSIONS` (optional, default='READ;WRITE', where READ grants access to `search_notes`, `get_note`, `resolve_note_id`, and `read_attributes`, and WRITE grants access to `create_note`, `update_note`, `delete_note`, and `manage_attributes`)
|
|
13
|
+
- `VERBOSE` (optional, default='false', which if true will print verbose debugging logs)
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
### 1. Using with Claude Desktop
|
|
18
|
+
|
|
19
|
+
Add the server config to your Claude Desktop configuration file:
|
|
20
|
+
|
|
21
|
+
#### For Local Installation (on Windows)
|
|
22
|
+
|
|
23
|
+
```json
|
|
24
|
+
"triliumnext-mcp": {
|
|
25
|
+
"command": "cmd",
|
|
26
|
+
"args": [
|
|
27
|
+
"/k",
|
|
28
|
+
"npx",
|
|
29
|
+
"-y",
|
|
30
|
+
"triliumnext-mcp"
|
|
31
|
+
],
|
|
32
|
+
"env": {
|
|
33
|
+
"TRILIUM_API_URL": "http://localhost:8080/etapi",
|
|
34
|
+
"TRILIUM_API_TOKEN": "<YOUR_TRILIUM_API_TOKEN>",
|
|
35
|
+
"PERMISSIONS": "READ;WRITE"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
#### For Local installation (on Linux)
|
|
41
|
+
|
|
42
|
+
```json
|
|
43
|
+
"triliumnext-mcp": {
|
|
44
|
+
"command": "npx",
|
|
45
|
+
"args": [
|
|
46
|
+
"-y",
|
|
47
|
+
"triliumnext-mcp"
|
|
48
|
+
],
|
|
49
|
+
"env": {
|
|
50
|
+
"TRILIUM_API_URL": "http://localhost:8080/etapi",
|
|
51
|
+
"TRILIUM_API_TOKEN": "<YOUR_TRILIUM_API_TOKEN>",
|
|
52
|
+
"PERMISSIONS": "READ;WRITE"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### For Development (on Windows / Linux)
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
cd /path/to/triliumnext-mcp
|
|
61
|
+
npm run build
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
```json
|
|
65
|
+
"triliumnext-mcp": {
|
|
66
|
+
"command": "node",
|
|
67
|
+
"args": [
|
|
68
|
+
"/path/to/triliumnext-mcp/build/index.js"
|
|
69
|
+
],
|
|
70
|
+
"env": {
|
|
71
|
+
"TRILIUM_API_URL": "http://localhost:8080/etapi",
|
|
72
|
+
"TRILIUM_API_TOKEN": "<YOUR_TRILIUM_API_TOKEN>",
|
|
73
|
+
"PERMISSIONS": "READ;WRITE",
|
|
74
|
+
"VERBOSE": "true"
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Location of the configuration file:
|
|
80
|
+
- Windows: `%APPDATA%/Claude/claude_desktop_config.json`
|
|
81
|
+
- MacOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
|
|
82
|
+
|
|
83
|
+
**Feedback**: Please report issues and test results at [GitHub Issues](https://github.com/TriliumNext/Notes/issues)
|
|
84
|
+
|
|
85
|
+
## Available Tools
|
|
86
|
+
|
|
87
|
+
The server provides the following tools for note management:
|
|
88
|
+
|
|
89
|
+
### Search & Discovery Tools
|
|
90
|
+
|
|
91
|
+
- `search_notes` - Unified search with comprehensive filtering capabilities including keyword search, date ranges, field-specific searches, attribute searches, note properties, template-based searches, note type filtering, MIME type filtering, and hierarchy navigation.
|
|
92
|
+
- `resolve_note_id` - Find a note's ID by its title. Essential for getting a note's ID to use with other tools.
|
|
93
|
+
|
|
94
|
+
### Note Management Tools
|
|
95
|
+
|
|
96
|
+
- `get_note` - Retrieve a note and its content by ID. Can also be used with regex to extract specific patterns from the content.
|
|
97
|
+
- `create_note` - Create a new note. Supports 9 note types and allows creating attributes (labels and relations) in the same step.
|
|
98
|
+
- `update_note` - Updates a note's title or content. Requires a `mode` (`'overwrite'` or `'append'`) to specify the update type and an `expectedHash` to prevent conflicts.
|
|
99
|
+
- `delete_note` - Permanently delete a note (⚠️ cannot be undone).
|
|
100
|
+
|
|
101
|
+
### Attribute Management Tools
|
|
102
|
+
|
|
103
|
+
- `read_attributes` - Read all attributes (labels and relations) for a given note.
|
|
104
|
+
- `manage_attributes` - Create, update, or delete attributes on a note. Supports batch creation.
|
|
105
|
+
|
|
106
|
+
> 📖 **Detailed Usage**: See [Note Management Guide](docs/manage-notes-examples/index.md) for revision control strategy and best practices.
|
|
107
|
+
|
|
108
|
+
## Example Queries
|
|
109
|
+
|
|
110
|
+
### Search & Discovery
|
|
111
|
+
- "Find my most recent 10 notes about 'n8n' since the beginning of 2024"
|
|
112
|
+
- "Show me notes I've edited in the last 7 days"
|
|
113
|
+
- "List all notes under 'n8n Template' folder, including subfolders"
|
|
114
|
+
|
|
115
|
+
### Content Management
|
|
116
|
+
- "Add today's update to my work log" (uses `update_note` with `mode: 'append'`)
|
|
117
|
+
- "Replace this draft with the final version" (uses `update_note` with `mode: 'overwrite'`)
|
|
118
|
+
- "Create a new note called 'Weekly Review' in my journal folder"
|
|
119
|
+
|
|
120
|
+
> 📖 **More Examples**: See [User Query Examples](docs/user-query-examples.md) for comprehensive usage scenarios.
|
|
121
|
+
|
|
122
|
+
## Documentation
|
|
123
|
+
|
|
124
|
+
- [Note Management Guide](docs/manage-notes-examples/index.md) - Safe content editing with revision control
|
|
125
|
+
- [User Query Examples](docs/user-query-examples.md) - Natural language query examples
|
|
126
|
+
- [Search Query Examples](docs/search-examples/) - Advanced search syntax and filters
|
|
127
|
+
|
|
128
|
+
## Development
|
|
129
|
+
|
|
130
|
+
If you want to contribute or modify the server:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
# Clone the repository
|
|
134
|
+
git clone https://github.com/tan-yong-sheng/triliumnext-mcp.git
|
|
135
|
+
|
|
136
|
+
# Install dependencies
|
|
137
|
+
npm install
|
|
138
|
+
|
|
139
|
+
# Build the server
|
|
140
|
+
npm run build
|
|
141
|
+
|
|
142
|
+
# For development with auto-rebuild
|
|
143
|
+
npm run watch
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Contributing
|
|
147
|
+
|
|
148
|
+
Contributions are welcome! If you are looking to improve the server, please familiarize yourself with the official [Trilium Search DSL documentation](https://triliumnext.github.io/Docs/Wiki/search.html) and our internal [Search Query Examples](docs/search-examples/) to understand how search queries are constructed.
|
|
149
|
+
|
|
150
|
+
Please feel free to open an issue or submit a pull request.
|
package/build/index.js
CHANGED
|
@@ -5,10 +5,11 @@ import { CallToolRequestSchema, ErrorCode, ListToolsRequestSchema, McpError, } f
|
|
|
5
5
|
import axios from "axios";
|
|
6
6
|
// Import modular components
|
|
7
7
|
import { generateTools } from "./modules/toolDefinitions.js";
|
|
8
|
-
import { handleCreateNoteRequest, handleUpdateNoteRequest, handleDeleteNoteRequest, handleGetNoteRequest } from "./modules/noteHandler.js";
|
|
8
|
+
import { handleCreateNoteRequest, handleUpdateNoteRequest, handleDeleteNoteRequest, handleGetNoteRequest, handleSearchReplaceNoteRequest } from "./modules/noteHandler.js";
|
|
9
9
|
import { handleSearchNotesRequest } from "./modules/searchHandler.js";
|
|
10
10
|
import { handleResolveNoteRequest } from "./modules/resolveHandler.js";
|
|
11
11
|
import { handleManageAttributes, handleReadAttributes } from "./modules/attributeHandler.js";
|
|
12
|
+
import { handleListAttributesRequest } from "./modules/attributeListHandler.js";
|
|
12
13
|
const TRILIUM_API_URL = process.env.TRILIUM_API_URL;
|
|
13
14
|
const TRILIUM_API_TOKEN = process.env.TRILIUM_API_TOKEN;
|
|
14
15
|
const PERMISSIONS = process.env.PERMISSIONS || "READ;WRITE";
|
|
@@ -23,7 +24,7 @@ class TriliumServer {
|
|
|
23
24
|
this.allowedPermissions = PERMISSIONS.split(';');
|
|
24
25
|
this.server = new Server({
|
|
25
26
|
name: "triliumnext-mcp",
|
|
26
|
-
version: "0.
|
|
27
|
+
version: "0.3.10",
|
|
27
28
|
}, {
|
|
28
29
|
capabilities: {
|
|
29
30
|
tools: {},
|
|
@@ -66,6 +67,8 @@ class TriliumServer {
|
|
|
66
67
|
return await handleDeleteNoteRequest(request.params.arguments, this.axiosInstance, this);
|
|
67
68
|
case "get_note":
|
|
68
69
|
return await handleGetNoteRequest(request.params.arguments, this.axiosInstance, this);
|
|
70
|
+
case "search_and_replace_note":
|
|
71
|
+
return await handleSearchReplaceNoteRequest(request.params.arguments, this.axiosInstance, this);
|
|
69
72
|
// Search and listing operations
|
|
70
73
|
case "search_notes":
|
|
71
74
|
return await handleSearchNotesRequest(request.params.arguments, this.axiosInstance, this);
|
|
@@ -75,6 +78,8 @@ class TriliumServer {
|
|
|
75
78
|
return await handleReadAttributes(request.params.arguments, this.axiosInstance, this);
|
|
76
79
|
case "manage_attributes":
|
|
77
80
|
return await handleManageAttributes(request.params.arguments, this.axiosInstance, this);
|
|
81
|
+
case "list_attributes":
|
|
82
|
+
return await handleListAttributesRequest(request.params.arguments, this.axiosInstance, this);
|
|
78
83
|
default:
|
|
79
84
|
throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}`);
|
|
80
85
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attribute List Handler Module
|
|
3
|
+
* Centralized request handling for attribute listing operations
|
|
4
|
+
*/
|
|
5
|
+
import { McpError, ErrorCode } from "@modelcontextprotocol/sdk/types.js";
|
|
6
|
+
import { handleListAttributes } from "./attributeListManager.js";
|
|
7
|
+
/**
|
|
8
|
+
* Handle list_attributes tool requests
|
|
9
|
+
*/
|
|
10
|
+
export async function handleListAttributesRequest(args, axiosInstance, permissionChecker) {
|
|
11
|
+
if (!permissionChecker.hasPermission("READ")) {
|
|
12
|
+
throw new McpError(ErrorCode.InvalidRequest, "Permission denied: Not authorized to list attributes.");
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
const listOperation = {
|
|
16
|
+
noteId: args.noteId,
|
|
17
|
+
hierarchyLevel: args.hierarchyLevel || 'immediate',
|
|
18
|
+
limit: args.limit || 50
|
|
19
|
+
};
|
|
20
|
+
const result = await handleListAttributes(listOperation, axiosInstance);
|
|
21
|
+
return {
|
|
22
|
+
content: [{
|
|
23
|
+
type: "text",
|
|
24
|
+
text: JSON.stringify(result, null, 2)
|
|
25
|
+
}]
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (error instanceof McpError) {
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
throw new McpError(ErrorCode.InvalidParams, error instanceof Error ? error.message : String(error));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attribute List Management Module
|
|
3
|
+
* Handles attribute listing operations using search_notes internally
|
|
4
|
+
*/
|
|
5
|
+
import { handleSearchNotes } from './searchManager.js';
|
|
6
|
+
import { logVerboseInput, logVerboseOutput } from '../utils/verboseUtils.js';
|
|
7
|
+
/**
|
|
8
|
+
* Handle list attributes operation using search_notes internally
|
|
9
|
+
*/
|
|
10
|
+
export async function handleListAttributes(args, axiosInstance) {
|
|
11
|
+
logVerboseInput('handleListAttributes', args);
|
|
12
|
+
// Build search criteria based on hierarchy navigation
|
|
13
|
+
const searchCriteria = buildHierarchySearchCriteria(args);
|
|
14
|
+
// Use search_notes internally to find notes
|
|
15
|
+
const searchOperation = {
|
|
16
|
+
searchCriteria: searchCriteria,
|
|
17
|
+
limit: args.limit || 100
|
|
18
|
+
};
|
|
19
|
+
const searchResults = await handleSearchNotes(searchOperation, axiosInstance);
|
|
20
|
+
// Extract and organize attributes from search results
|
|
21
|
+
const attributes = extractAttributesFromResults(searchResults.results, args);
|
|
22
|
+
// Generate summary
|
|
23
|
+
const summary = generateAttributeSummary(attributes, args);
|
|
24
|
+
const result = {
|
|
25
|
+
attributes: attributes,
|
|
26
|
+
summary: summary
|
|
27
|
+
};
|
|
28
|
+
logVerboseOutput('handleListAttributes', result);
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Build search criteria for hierarchy navigation
|
|
33
|
+
*/
|
|
34
|
+
function buildHierarchySearchCriteria(args) {
|
|
35
|
+
const criteria = [];
|
|
36
|
+
const hierarchyLevel = args.hierarchyLevel || 'immediate';
|
|
37
|
+
if (hierarchyLevel === 'immediate') {
|
|
38
|
+
// For immediate level, search for direct parents and children
|
|
39
|
+
criteria.push({
|
|
40
|
+
property: 'parents.noteId',
|
|
41
|
+
type: 'noteProperty',
|
|
42
|
+
op: '=',
|
|
43
|
+
value: args.noteId
|
|
44
|
+
});
|
|
45
|
+
criteria.push({
|
|
46
|
+
property: 'children.noteId',
|
|
47
|
+
type: 'noteProperty',
|
|
48
|
+
op: '=',
|
|
49
|
+
value: args.noteId,
|
|
50
|
+
logic: 'OR'
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
// For all levels, search for ancestors and descendants
|
|
55
|
+
criteria.push({
|
|
56
|
+
property: 'ancestors.noteId',
|
|
57
|
+
type: 'noteProperty',
|
|
58
|
+
op: '=',
|
|
59
|
+
value: args.noteId
|
|
60
|
+
});
|
|
61
|
+
criteria.push({
|
|
62
|
+
property: 'descendants.noteId',
|
|
63
|
+
type: 'noteProperty',
|
|
64
|
+
op: '=',
|
|
65
|
+
value: args.noteId,
|
|
66
|
+
logic: 'OR'
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
// Exclude the anchor note itself
|
|
70
|
+
criteria.push({
|
|
71
|
+
property: 'noteId',
|
|
72
|
+
type: 'noteProperty',
|
|
73
|
+
op: '!=',
|
|
74
|
+
value: args.noteId,
|
|
75
|
+
logic: 'AND'
|
|
76
|
+
});
|
|
77
|
+
return criteria;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Extract and organize attributes from search results
|
|
81
|
+
*/
|
|
82
|
+
function extractAttributesFromResults(searchResults, args) {
|
|
83
|
+
const attributes = [];
|
|
84
|
+
for (const note of searchResults) {
|
|
85
|
+
if (note.attributes && Array.isArray(note.attributes)) {
|
|
86
|
+
for (const attribute of note.attributes) {
|
|
87
|
+
const attributeInfo = {
|
|
88
|
+
noteId: note.noteId,
|
|
89
|
+
noteTitle: note.title,
|
|
90
|
+
noteType: note.type,
|
|
91
|
+
attributeId: attribute.attributeId,
|
|
92
|
+
attributeType: attribute.type,
|
|
93
|
+
attributeName: attribute.name,
|
|
94
|
+
attributeValue: attribute.value || '',
|
|
95
|
+
position: attribute.position || 10,
|
|
96
|
+
isInheritable: attribute.isInheritable || false,
|
|
97
|
+
hierarchyPath: buildHierarchyPath(note, args)
|
|
98
|
+
};
|
|
99
|
+
attributes.push(attributeInfo);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return attributes;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Build hierarchy path for attribute context
|
|
107
|
+
*/
|
|
108
|
+
function buildHierarchyPath(note, args) {
|
|
109
|
+
const hierarchyLevel = args.hierarchyLevel || 'immediate';
|
|
110
|
+
const noteTitle = note.title || 'Unknown';
|
|
111
|
+
if (hierarchyLevel === 'immediate') {
|
|
112
|
+
return `Related to ${args.noteId}: ${noteTitle}`;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
return `Hierarchy-connected to ${args.noteId}: ${noteTitle}`;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Generate summary of attributes found
|
|
120
|
+
*/
|
|
121
|
+
function generateAttributeSummary(attributes, args) {
|
|
122
|
+
const totalAttributes = attributes.length;
|
|
123
|
+
if (totalAttributes === 0) {
|
|
124
|
+
return `No attributes found`;
|
|
125
|
+
}
|
|
126
|
+
// Group by attribute type
|
|
127
|
+
const labelCount = attributes.filter(attr => attr.attributeType === 'label').length;
|
|
128
|
+
const relationCount = attributes.filter(attr => attr.attributeType === 'relation').length;
|
|
129
|
+
// Group by note
|
|
130
|
+
const uniqueNotes = new Set(attributes.map(attr => attr.noteId)).size;
|
|
131
|
+
let summary = `Found ${totalAttributes} attributes across ${uniqueNotes} notes`;
|
|
132
|
+
if (labelCount > 0 || relationCount > 0) {
|
|
133
|
+
summary += ` (${labelCount} labels, ${relationCount} relations)`;
|
|
134
|
+
}
|
|
135
|
+
// Add hierarchy context
|
|
136
|
+
const hierarchyLevel = args.hierarchyLevel || 'immediate';
|
|
137
|
+
if (hierarchyLevel === 'immediate') {
|
|
138
|
+
summary += ` in immediate hierarchy (parents and children)`;
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
summary += ` in full hierarchy (ancestors and descendants)`;
|
|
142
|
+
}
|
|
143
|
+
// Add unique attribute names count
|
|
144
|
+
const uniqueAttributeNames = new Set(attributes.map(attr => attr.attributeName)).size;
|
|
145
|
+
summary += ` with ${uniqueAttributeNames} unique attribute types`;
|
|
146
|
+
return summary;
|
|
147
|
+
}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import axios from 'axios';
|
|
6
6
|
import { logVerbose, logVerboseApi, logVerboseAxiosError } from "../utils/verboseUtils.js";
|
|
7
|
+
import { validateAndTranslateTemplate, createTemplateRelationError } from "../utils/templateMapper.js";
|
|
7
8
|
/**
|
|
8
9
|
* Manage note attributes with write operations (create, update, delete)
|
|
9
10
|
* This function provides write-only access to note attributes
|
|
@@ -49,12 +50,30 @@ async function create_single_attribute(noteId, attribute, axiosInstance) {
|
|
|
49
50
|
errors: validation.errors
|
|
50
51
|
};
|
|
51
52
|
}
|
|
53
|
+
// Translate template names to note IDs for template relations
|
|
54
|
+
let processedValue = attribute.value || "";
|
|
55
|
+
if (attribute.type === "relation" && attribute.name === "template" && attribute.value) {
|
|
56
|
+
try {
|
|
57
|
+
processedValue = validateAndTranslateTemplate(attribute.value);
|
|
58
|
+
logVerbose("create_single_attribute", `Translated template relation`, {
|
|
59
|
+
from: attribute.value,
|
|
60
|
+
to: processedValue
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
return {
|
|
65
|
+
success: false,
|
|
66
|
+
message: createTemplateRelationError(attribute.value),
|
|
67
|
+
errors: [error instanceof Error ? error.message : 'Template validation failed']
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
52
71
|
// Prepare attribute data for ETAPI
|
|
53
72
|
const attributeData = {
|
|
54
73
|
noteId: noteId,
|
|
55
74
|
type: attribute.type,
|
|
56
75
|
name: attribute.name,
|
|
57
|
-
value:
|
|
76
|
+
value: processedValue,
|
|
58
77
|
position: attribute.position || 10,
|
|
59
78
|
isInheritable: attribute.isInheritable || false
|
|
60
79
|
};
|
|
@@ -95,11 +114,26 @@ async function create_batch_attributes(noteId, attributes, axiosInstance) {
|
|
|
95
114
|
errors.push(`Validation failed for ${attribute.type} '${attribute.name}': ${validation.errors.join(', ')}`);
|
|
96
115
|
return null;
|
|
97
116
|
}
|
|
117
|
+
// Translate template names to note IDs for template relations
|
|
118
|
+
let processedValue = attribute.value || "";
|
|
119
|
+
if (attribute.type === "relation" && attribute.name === "template" && attribute.value) {
|
|
120
|
+
try {
|
|
121
|
+
processedValue = validateAndTranslateTemplate(attribute.value);
|
|
122
|
+
logVerbose("create_batch_attributes", `Translated template relation`, {
|
|
123
|
+
from: attribute.value,
|
|
124
|
+
to: processedValue
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
errors.push(createTemplateRelationError(attribute.value));
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
98
132
|
const attributeData = {
|
|
99
133
|
noteId: noteId,
|
|
100
134
|
type: attribute.type,
|
|
101
135
|
name: attribute.name,
|
|
102
|
-
value:
|
|
136
|
+
value: processedValue,
|
|
103
137
|
position: attribute.position || 10,
|
|
104
138
|
isInheritable: attribute.isInheritable || false
|
|
105
139
|
};
|