@inkdropapp/mcp-server 1.8.0 → 1.10.0-beta.0
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/.github/workflows/actions.lock +10 -0
- package/.github/workflows/ci.yml +65 -0
- package/.github/workflows/publish.yml +10 -1
- package/.oxfmtrc.json +2 -1
- package/README.md +16 -6
- package/dist/client/agent-identity.js +45 -0
- package/dist/client/agent-session.js +66 -0
- package/dist/client/local-server.js +64 -0
- package/dist/features/files/image-type.js +31 -0
- package/dist/features/files/tools.js +191 -0
- package/dist/features/notebooks/tools.js +95 -0
- package/dist/features/notes/note-anchor.js +58 -0
- package/dist/features/notes/note-schemas.js +38 -0
- package/dist/features/notes/note-update.js +16 -0
- package/dist/features/notes/resource.js +26 -0
- package/dist/features/notes/text-edits.js +36 -0
- package/dist/features/notes/tools.js +405 -0
- package/dist/features/tags/tools.js +82 -0
- package/dist/index.js +19 -826
- package/dist/utils/document-fields.js +18 -0
- package/mise.toml +5 -0
- package/oxlint.config.ts +50 -0
- package/package.json +22 -14
- package/pnpm-workspace.yaml +6 -0
- package/src/client/agent-identity.ts +70 -0
- package/src/client/agent-session.ts +75 -0
- package/src/client/local-server.ts +73 -0
- package/src/features/files/image-type.ts +35 -0
- package/src/features/files/tools.ts +230 -0
- package/src/features/notebooks/tools.ts +133 -0
- package/src/features/notes/note-anchor.ts +85 -0
- package/src/features/notes/note-schemas.ts +60 -0
- package/src/features/notes/note-update.ts +27 -0
- package/src/features/notes/resource.ts +34 -0
- package/src/features/notes/text-edits.ts +48 -0
- package/src/features/notes/tools.ts +499 -0
- package/src/features/tags/tools.ts +109 -0
- package/src/index.ts +17 -1049
- package/src/utils/document-fields.ts +21 -0
- package/tests/client/agent-identity.test.ts +98 -0
- package/tests/features/notes/note-anchor.test.ts +125 -0
- package/tests/features/notes/note-schemas.test.ts +27 -0
- package/tests/utils/document-fields.test.ts +41 -0
- package/tsconfig.json +1 -1
- package/dist/api.js +0 -47
- package/dist/utils.js +0 -100
- package/src/api.ts +0 -45
- package/src/utils.ts +0 -112
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Builds the ID schema for one kind of Inkdrop document.
|
|
5
|
+
*
|
|
6
|
+
* @param idPrefix - The prefix every ID of that kind starts with, e.g. `note:`
|
|
7
|
+
* @returns A schema that accepts an ID with that prefix, within the length Inkdrop allows
|
|
8
|
+
*/
|
|
9
|
+
export function documentIdSchema(idPrefix: string) {
|
|
10
|
+
return z
|
|
11
|
+
.string()
|
|
12
|
+
.min(6)
|
|
13
|
+
.max(128)
|
|
14
|
+
.regex(new RegExp(`^${idPrefix}`))
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** A CouchDB revision token of a document. */
|
|
18
|
+
export const revisionSchema = z.string()
|
|
19
|
+
|
|
20
|
+
/** A point in time as a Unix timestamp in milliseconds. */
|
|
21
|
+
export const timestampSchema = z.number().int().nonnegative()
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
|
|
3
|
+
import { AGENT_HEADERS } from 'inkdrop-consts'
|
|
4
|
+
|
|
5
|
+
import { buildAgentHeaders, workspaceNameFromRoot } from '../../src/client/agent-identity'
|
|
6
|
+
|
|
7
|
+
const SESSION_ID = '3f0c1c3e-6f0a-4d0e-9f5b-0a4f2d6f7b11'
|
|
8
|
+
|
|
9
|
+
describe('buildAgentHeaders', () => {
|
|
10
|
+
test('sends only the session ID before the client is known', () => {
|
|
11
|
+
expect(buildAgentHeaders({ sessionId: SESSION_ID })).toEqual({
|
|
12
|
+
[AGENT_HEADERS.session]: SESSION_ID
|
|
13
|
+
})
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test('sends the client name, title, version and the workspace', () => {
|
|
17
|
+
const headers = buildAgentHeaders({
|
|
18
|
+
sessionId: SESSION_ID,
|
|
19
|
+
client: { name: 'claude-code', title: 'Claude Code', version: '2.1.0' },
|
|
20
|
+
workspace: 'inkdrop'
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
expect(headers).toEqual({
|
|
24
|
+
[AGENT_HEADERS.session]: SESSION_ID,
|
|
25
|
+
[AGENT_HEADERS.clientName]: 'claude-code',
|
|
26
|
+
[AGENT_HEADERS.clientTitle]: 'Claude%20Code',
|
|
27
|
+
[AGENT_HEADERS.clientVersion]: '2.1.0',
|
|
28
|
+
[AGENT_HEADERS.workspace]: 'inkdrop'
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test('percent-encodes values that a header cannot carry as they are', () => {
|
|
33
|
+
const headers = buildAgentHeaders({
|
|
34
|
+
sessionId: SESSION_ID,
|
|
35
|
+
client: { name: 'client', version: '1' },
|
|
36
|
+
workspace: 'ノート\r\nInjected: 1'
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
expect(headers[AGENT_HEADERS.workspace]).toBe(encodeURIComponent('ノート\r\nInjected: 1'))
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('leaves out an empty value', () => {
|
|
43
|
+
const headers = buildAgentHeaders({
|
|
44
|
+
sessionId: SESSION_ID,
|
|
45
|
+
client: { name: 'client', title: '', version: '1' }
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
expect(AGENT_HEADERS.clientTitle in headers).toBe(false)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('cuts a long value', () => {
|
|
52
|
+
const headers = buildAgentHeaders({
|
|
53
|
+
sessionId: SESSION_ID,
|
|
54
|
+
client: { name: 'a'.repeat(500), version: '1' }
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
expect(headers[AGENT_HEADERS.clientName]).toBe('a'.repeat(128))
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('cuts a long value in the middle of an emoji without throwing', () => {
|
|
61
|
+
const headers = buildAgentHeaders({
|
|
62
|
+
sessionId: SESSION_ID,
|
|
63
|
+
workspace: `${'a'.repeat(127)}😀`
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
expect(headers[AGENT_HEADERS.workspace]).toBe(
|
|
67
|
+
`${'a'.repeat(127)}${encodeURIComponent('\uFFFD')}`
|
|
68
|
+
)
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
describe('workspaceNameFromRoot', () => {
|
|
73
|
+
test('prefers the name the client gives the root', () => {
|
|
74
|
+
expect(workspaceNameFromRoot({ uri: 'file:///Users/me/dev/inkdrop', name: 'Inkdrop' })).toBe(
|
|
75
|
+
'Inkdrop'
|
|
76
|
+
)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('falls back to the last folder of a file URI', () => {
|
|
80
|
+
expect(workspaceNameFromRoot({ uri: 'file:///Users/me/dev/inkdrop' })).toBe('inkdrop')
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('ignores a trailing slash', () => {
|
|
84
|
+
expect(workspaceNameFromRoot({ uri: 'file:///Users/me/dev/inkdrop/' })).toBe('inkdrop')
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('decodes an encoded folder name', () => {
|
|
88
|
+
expect(workspaceNameFromRoot({ uri: 'file:///Users/me/my%20notes' })).toBe('my notes')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('returns undefined for a URI with no folder', () => {
|
|
92
|
+
expect(workspaceNameFromRoot({ uri: 'file:///' })).toBeUndefined()
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('returns undefined for a malformed URI', () => {
|
|
96
|
+
expect(workspaceNameFromRoot({ uri: 'not a uri' })).toBeUndefined()
|
|
97
|
+
})
|
|
98
|
+
})
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
describeNoteLines,
|
|
5
|
+
extractNoteLines,
|
|
6
|
+
parseNoteReference
|
|
7
|
+
} from '../../../src/features/notes/note-anchor'
|
|
8
|
+
|
|
9
|
+
describe('parseNoteReference', () => {
|
|
10
|
+
test('keeps a prefixed note ID', () => {
|
|
11
|
+
expect(parseNoteReference('note:abc123')).toEqual({ noteId: 'note:abc123' })
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
test('adds the prefix to a bare note ID', () => {
|
|
15
|
+
expect(parseNoteReference('abc123')).toEqual({ noteId: 'note:abc123' })
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
test('reads the note ID from a note URI', () => {
|
|
19
|
+
expect(parseNoteReference('inkdrop://note/abc123')).toEqual({ noteId: 'note:abc123' })
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test('reads a single-line anchor', () => {
|
|
23
|
+
expect(parseNoteReference('inkdrop://note/abc123#L5')).toEqual({
|
|
24
|
+
noteId: 'note:abc123',
|
|
25
|
+
anchor: { type: 'lines', startLine: 5, endLine: 5 }
|
|
26
|
+
})
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test('reads a line-range anchor', () => {
|
|
30
|
+
expect(parseNoteReference('inkdrop://note/abc123#L5-L8')).toEqual({
|
|
31
|
+
noteId: 'note:abc123',
|
|
32
|
+
anchor: { type: 'lines', startLine: 5, endLine: 8 }
|
|
33
|
+
})
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test('reads a heading anchor', () => {
|
|
37
|
+
expect(parseNoteReference('inkdrop://note/abc123#setup-guide')).toEqual({
|
|
38
|
+
noteId: 'note:abc123',
|
|
39
|
+
anchor: { type: 'heading', slug: 'setup-guide' }
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('reads the legacy note URI form', () => {
|
|
44
|
+
expect(parseNoteReference('inkdrop://note:abc123#L2')).toEqual({
|
|
45
|
+
noteId: 'note:abc123',
|
|
46
|
+
anchor: { type: 'lines', startLine: 2, endLine: 2 }
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('rejects an Inkdrop URI that is not a note', () => {
|
|
51
|
+
expect(() => parseNoteReference('inkdrop://file:abc123')).toThrow(/not a note URI/)
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
describe('extractNoteLines', () => {
|
|
56
|
+
const body = 'one\ntwo\nthree\nfour\nfive'
|
|
57
|
+
|
|
58
|
+
test('returns a single line', () => {
|
|
59
|
+
expect(extractNoteLines(body, { startLine: 2, endLine: 2 })).toEqual({
|
|
60
|
+
text: 'two',
|
|
61
|
+
startLine: 2,
|
|
62
|
+
endLine: 2,
|
|
63
|
+
totalLines: 5
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('returns an inclusive range', () => {
|
|
68
|
+
expect(extractNoteLines(body, { startLine: 2, endLine: 4 })).toEqual({
|
|
69
|
+
text: 'two\nthree\nfour',
|
|
70
|
+
startLine: 2,
|
|
71
|
+
endLine: 4,
|
|
72
|
+
totalLines: 5
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('clamps a range that runs past the end', () => {
|
|
77
|
+
expect(extractNoteLines(body, { startLine: 4, endLine: 99 })).toEqual({
|
|
78
|
+
text: 'four\nfive',
|
|
79
|
+
startLine: 4,
|
|
80
|
+
endLine: 5,
|
|
81
|
+
totalLines: 5
|
|
82
|
+
})
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('lands on the last line when the whole range is past the end', () => {
|
|
86
|
+
expect(extractNoteLines(body, { startLine: 50, endLine: 60 })).toEqual({
|
|
87
|
+
text: 'five',
|
|
88
|
+
startLine: 5,
|
|
89
|
+
endLine: 5,
|
|
90
|
+
totalLines: 5
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test('counts CRLF and CR line breaks as one line break', () => {
|
|
95
|
+
expect(extractNoteLines('one\r\ntwo\rthree', { startLine: 2, endLine: 3 })).toEqual({
|
|
96
|
+
text: 'two\nthree',
|
|
97
|
+
startLine: 2,
|
|
98
|
+
endLine: 3,
|
|
99
|
+
totalLines: 3
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
test('treats an empty body as one empty line', () => {
|
|
104
|
+
expect(extractNoteLines('', { startLine: 1, endLine: 3 })).toEqual({
|
|
105
|
+
text: '',
|
|
106
|
+
startLine: 1,
|
|
107
|
+
endLine: 1,
|
|
108
|
+
totalLines: 1
|
|
109
|
+
})
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
describe('describeNoteLines', () => {
|
|
114
|
+
test('names a single line', () => {
|
|
115
|
+
expect(
|
|
116
|
+
describeNoteLines({ text: 'five', startLine: 5, endLine: 5, totalLines: 20 })
|
|
117
|
+
).toStartWith("`body` holds only line 5 of this note's 20 lines")
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
test('names a line range', () => {
|
|
121
|
+
expect(
|
|
122
|
+
describeNoteLines({ text: 'a\nb', startLine: 8, endLine: 9, totalLines: 20 })
|
|
123
|
+
).toStartWith("`body` holds only lines 8-9 of this note's 20 lines")
|
|
124
|
+
})
|
|
125
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
|
|
3
|
+
import { noteTitleSchema, sourceTemplateIdSchema } from '../../../src/features/notes/note-schemas'
|
|
4
|
+
|
|
5
|
+
describe('sourceTemplateIdSchema', () => {
|
|
6
|
+
test('accepts a note ID', () => {
|
|
7
|
+
expect(sourceTemplateIdSchema.parse('note:abc123')).toBe('note:abc123')
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
test('rejects an ID of another document type', () => {
|
|
11
|
+
expect(sourceTemplateIdSchema.safeParse('book:abc123').success).toBe(false)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
test('rejects a note link', () => {
|
|
15
|
+
expect(sourceTemplateIdSchema.safeParse('inkdrop://note/abc123').success).toBe(false)
|
|
16
|
+
})
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
describe('noteTitleSchema', () => {
|
|
20
|
+
test('accepts a title as long as inkdrop-model allows', () => {
|
|
21
|
+
expect(noteTitleSchema.safeParse('a'.repeat(256)).success).toBe(true)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test('rejects a title longer than inkdrop-model allows', () => {
|
|
25
|
+
expect(noteTitleSchema.safeParse('a'.repeat(257)).success).toBe(false)
|
|
26
|
+
})
|
|
27
|
+
})
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
|
|
3
|
+
import { documentIdSchema, timestampSchema } from '../../src/utils/document-fields'
|
|
4
|
+
|
|
5
|
+
describe('documentIdSchema', () => {
|
|
6
|
+
const tagIdSchema = documentIdSchema('tag:')
|
|
7
|
+
|
|
8
|
+
test('accepts an ID with the prefix', () => {
|
|
9
|
+
expect(tagIdSchema.parse('tag:abc123')).toBe('tag:abc123')
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
test('rejects an ID of another document type', () => {
|
|
13
|
+
expect(tagIdSchema.safeParse('note:abc123').success).toBe(false)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test('rejects an ID that has the prefix in the middle', () => {
|
|
17
|
+
expect(tagIdSchema.safeParse('my-tag:abc123').success).toBe(false)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test('rejects an ID shorter than 6 characters', () => {
|
|
21
|
+
expect(tagIdSchema.safeParse('tag:a').success).toBe(false)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test('rejects an ID longer than 128 characters', () => {
|
|
25
|
+
expect(tagIdSchema.safeParse(`tag:${'a'.repeat(125)}`).success).toBe(false)
|
|
26
|
+
})
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
describe('timestampSchema', () => {
|
|
30
|
+
test('accepts a Unix timestamp in milliseconds', () => {
|
|
31
|
+
expect(timestampSchema.parse(1_700_000_000_000)).toBe(1_700_000_000_000)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test('rejects a negative number', () => {
|
|
35
|
+
expect(timestampSchema.safeParse(-1).success).toBe(false)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
test('rejects a fractional number', () => {
|
|
39
|
+
expect(timestampSchema.safeParse(1.5).success).toBe(false)
|
|
40
|
+
})
|
|
41
|
+
})
|
package/tsconfig.json
CHANGED
package/dist/api.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.buildUrl = buildUrl;
|
|
4
|
-
exports.fetchJSON = fetchJSON;
|
|
5
|
-
exports.postJSON = postJSON;
|
|
6
|
-
const config = {
|
|
7
|
-
baseUrl: process.env.INKDROP_LOCAL_SERVER_URL,
|
|
8
|
-
username: process.env.INKDROP_LOCAL_USERNAME,
|
|
9
|
-
password: process.env.INKDROP_LOCAL_PASSWORD
|
|
10
|
-
};
|
|
11
|
-
function getAuthHeaders() {
|
|
12
|
-
const token = Buffer.from(`${config.username}:${config.password}`).toString('base64');
|
|
13
|
-
return {
|
|
14
|
-
Authorization: `Basic ${token}`
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
function buildUrl(path, qs = {}) {
|
|
18
|
-
const url = new URL(path, config.baseUrl);
|
|
19
|
-
for (const [k, v] of Object.entries(qs)) {
|
|
20
|
-
if (v !== undefined && v !== null)
|
|
21
|
-
url.searchParams.set(k, String(v));
|
|
22
|
-
}
|
|
23
|
-
return url.toString();
|
|
24
|
-
}
|
|
25
|
-
async function fetchJSON(path, qs = {}) {
|
|
26
|
-
const url = buildUrl(path, qs);
|
|
27
|
-
const resp = await fetch(url, { headers: getAuthHeaders() });
|
|
28
|
-
if (!resp.ok) {
|
|
29
|
-
throw new Error(`Fetch error [${resp.status}] ${resp.statusText}`);
|
|
30
|
-
}
|
|
31
|
-
return (await resp.json());
|
|
32
|
-
}
|
|
33
|
-
async function postJSON(path, body) {
|
|
34
|
-
const url = buildUrl(path);
|
|
35
|
-
const resp = await fetch(url, {
|
|
36
|
-
method: 'POST',
|
|
37
|
-
headers: {
|
|
38
|
-
...getAuthHeaders(),
|
|
39
|
-
'Content-Type': 'application/json'
|
|
40
|
-
},
|
|
41
|
-
body: JSON.stringify(body)
|
|
42
|
-
});
|
|
43
|
-
if (!resp.ok) {
|
|
44
|
-
throw new Error(`Fetch error [${resp.status}] ${resp.statusText}`);
|
|
45
|
-
}
|
|
46
|
-
return (await resp.json());
|
|
47
|
-
}
|
package/dist/utils.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getNoteUri = getNoteUri;
|
|
4
|
-
exports.getFileUri = getFileUri;
|
|
5
|
-
exports.inferImageContentType = inferImageContentType;
|
|
6
|
-
exports.applyTextEdits = applyTextEdits;
|
|
7
|
-
exports.isViewableImageType = isViewableImageType;
|
|
8
|
-
const node_path_1 = require("node:path");
|
|
9
|
-
const inkdrop_model_1 = require("inkdrop-model");
|
|
10
|
-
/**
|
|
11
|
-
* Builds the canonical Inkdrop note URI for a note ID.
|
|
12
|
-
*
|
|
13
|
-
* The `note:` prefix is stripped so the result matches how note links are stored
|
|
14
|
-
* in note bodies (`note:foobar` → `inkdrop://note/foobar`). Backlink searches rely
|
|
15
|
-
* on this exact form to find notes that link to a given note.
|
|
16
|
-
*
|
|
17
|
-
* @param noteId - The note document ID, with or without the `note:` prefix
|
|
18
|
-
* @returns The `inkdrop://note/<id>` URI
|
|
19
|
-
*/
|
|
20
|
-
function getNoteUri(noteId) {
|
|
21
|
-
return `inkdrop://note/${noteId.replace(/^note:/, '')}`;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Builds the Inkdrop attachment URI for a file ID.
|
|
25
|
-
*
|
|
26
|
-
* Unlike note links, attachment URIs keep the `file:` prefix and use no slash
|
|
27
|
-
* (`file:foobar` → `inkdrop://file:foobar`). This matches how the desktop app
|
|
28
|
-
* renders an attachment into a note body, so the result can be embedded as-is.
|
|
29
|
-
*
|
|
30
|
-
* @param fileId - The file document ID, with or without the `file:` prefix
|
|
31
|
-
* @returns The `inkdrop://file:<id>` URI
|
|
32
|
-
*/
|
|
33
|
-
function getFileUri(fileId) {
|
|
34
|
-
return `inkdrop://${fileId.startsWith('file:') ? fileId : `file:${fileId}`}`;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Infers the attachment content type from a file name or path extension.
|
|
38
|
-
*
|
|
39
|
-
* `SUPPORTED_IMAGE_MIME_TYPES` is keyed by MIME subtype, which doubles as the file
|
|
40
|
-
* extension for every supported type except SVG, whose subtype is `svg+xml`.
|
|
41
|
-
*
|
|
42
|
-
* @returns The matching image MIME type, or `undefined` when the extension is
|
|
43
|
-
* missing or not a supported attachment type
|
|
44
|
-
*/
|
|
45
|
-
function inferImageContentType(filePath) {
|
|
46
|
-
const ext = (0, node_path_1.extname)(filePath).toLowerCase().replace(/^\./, '');
|
|
47
|
-
return inkdrop_model_1.SUPPORTED_IMAGE_MIME_TYPES[ext === 'svg' ? 'svg+xml' : ext];
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Applies exact string replacements in order, each against the result of the
|
|
51
|
-
* previous one, so a later edit can match text an earlier edit produced.
|
|
52
|
-
*
|
|
53
|
-
* Every `old_string` must match exactly one location at the time it is applied.
|
|
54
|
-
* Processing stops at the first edit that does not, so callers never see a
|
|
55
|
-
* partially patched text.
|
|
56
|
-
*
|
|
57
|
-
* @param text - The text to patch
|
|
58
|
-
* @param edits - The replacements to perform
|
|
59
|
-
* @returns The patched text, or an error naming the 1-based position of the failing edit
|
|
60
|
-
*/
|
|
61
|
-
function applyTextEdits(text, edits) {
|
|
62
|
-
let patchedText = text;
|
|
63
|
-
for (const [editIndex, { old_string, new_string }] of edits.entries()) {
|
|
64
|
-
const editLabel = `edit ${editIndex + 1} of ${edits.length}`;
|
|
65
|
-
const firstMatchIndex = patchedText.indexOf(old_string);
|
|
66
|
-
if (firstMatchIndex === -1) {
|
|
67
|
-
return {
|
|
68
|
-
ok: false,
|
|
69
|
-
error: `\`old_string\` of ${editLabel} was not found in the note body.`
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
if (patchedText.indexOf(old_string, firstMatchIndex + 1) !== -1) {
|
|
73
|
-
return {
|
|
74
|
-
ok: false,
|
|
75
|
-
error: `\`old_string\` of ${editLabel} matches multiple locations in the note body. Provide more surrounding context to make it unique.`
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
patchedText =
|
|
79
|
-
patchedText.slice(0, firstMatchIndex) +
|
|
80
|
-
new_string +
|
|
81
|
-
patchedText.slice(firstMatchIndex + old_string.length);
|
|
82
|
-
}
|
|
83
|
-
return { ok: true, text: patchedText };
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Content types the MCP `image` content block can carry. Inkdrop also accepts
|
|
87
|
-
* SVG and HEIC/HEIF attachments, which have to be written to disk instead.
|
|
88
|
-
*/
|
|
89
|
-
const VIEWABLE_IMAGE_TYPES = new Set([
|
|
90
|
-
'image/png',
|
|
91
|
-
'image/jpeg',
|
|
92
|
-
'image/jpg',
|
|
93
|
-
'image/gif'
|
|
94
|
-
]);
|
|
95
|
-
/**
|
|
96
|
-
* Whether an attachment can be returned as an MCP `image` content block.
|
|
97
|
-
*/
|
|
98
|
-
function isViewableImageType(contentType) {
|
|
99
|
-
return contentType !== undefined && VIEWABLE_IMAGE_TYPES.has(contentType);
|
|
100
|
-
}
|
package/src/api.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
const config = {
|
|
2
|
-
baseUrl: process.env.INKDROP_LOCAL_SERVER_URL,
|
|
3
|
-
username: process.env.INKDROP_LOCAL_USERNAME,
|
|
4
|
-
password: process.env.INKDROP_LOCAL_PASSWORD
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
function getAuthHeaders() {
|
|
8
|
-
const token = Buffer.from(`${config.username}:${config.password}`).toString('base64')
|
|
9
|
-
return {
|
|
10
|
-
Authorization: `Basic ${token}`
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function buildUrl(path: string, qs: Record<string, any> = {}): string {
|
|
15
|
-
const url = new URL(path, config.baseUrl)
|
|
16
|
-
for (const [k, v] of Object.entries(qs)) {
|
|
17
|
-
if (v !== undefined && v !== null) url.searchParams.set(k, String(v))
|
|
18
|
-
}
|
|
19
|
-
return url.toString()
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export async function fetchJSON<T = any>(path: string, qs: Record<string, any> = {}): Promise<T> {
|
|
23
|
-
const url = buildUrl(path, qs)
|
|
24
|
-
const resp = await fetch(url, { headers: getAuthHeaders() })
|
|
25
|
-
if (!resp.ok) {
|
|
26
|
-
throw new Error(`Fetch error [${resp.status}] ${resp.statusText}`)
|
|
27
|
-
}
|
|
28
|
-
return (await resp.json()) as T
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export async function postJSON<T = any>(path: string, body: unknown): Promise<T> {
|
|
32
|
-
const url = buildUrl(path)
|
|
33
|
-
const resp = await fetch(url, {
|
|
34
|
-
method: 'POST',
|
|
35
|
-
headers: {
|
|
36
|
-
...getAuthHeaders(),
|
|
37
|
-
'Content-Type': 'application/json'
|
|
38
|
-
},
|
|
39
|
-
body: JSON.stringify(body)
|
|
40
|
-
})
|
|
41
|
-
if (!resp.ok) {
|
|
42
|
-
throw new Error(`Fetch error [${resp.status}] ${resp.statusText}`)
|
|
43
|
-
}
|
|
44
|
-
return (await resp.json()) as T
|
|
45
|
-
}
|
package/src/utils.ts
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { extname } from 'node:path'
|
|
2
|
-
|
|
3
|
-
import { SUPPORTED_IMAGE_MIME_TYPES, type ImageFileType } from 'inkdrop-model'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Builds the canonical Inkdrop note URI for a note ID.
|
|
7
|
-
*
|
|
8
|
-
* The `note:` prefix is stripped so the result matches how note links are stored
|
|
9
|
-
* in note bodies (`note:foobar` → `inkdrop://note/foobar`). Backlink searches rely
|
|
10
|
-
* on this exact form to find notes that link to a given note.
|
|
11
|
-
*
|
|
12
|
-
* @param noteId - The note document ID, with or without the `note:` prefix
|
|
13
|
-
* @returns The `inkdrop://note/<id>` URI
|
|
14
|
-
*/
|
|
15
|
-
export function getNoteUri(noteId: string): string {
|
|
16
|
-
return `inkdrop://note/${noteId.replace(/^note:/, '')}`
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Builds the Inkdrop attachment URI for a file ID.
|
|
21
|
-
*
|
|
22
|
-
* Unlike note links, attachment URIs keep the `file:` prefix and use no slash
|
|
23
|
-
* (`file:foobar` → `inkdrop://file:foobar`). This matches how the desktop app
|
|
24
|
-
* renders an attachment into a note body, so the result can be embedded as-is.
|
|
25
|
-
*
|
|
26
|
-
* @param fileId - The file document ID, with or without the `file:` prefix
|
|
27
|
-
* @returns The `inkdrop://file:<id>` URI
|
|
28
|
-
*/
|
|
29
|
-
export function getFileUri(fileId: string): string {
|
|
30
|
-
return `inkdrop://${fileId.startsWith('file:') ? fileId : `file:${fileId}`}`
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Infers the attachment content type from a file name or path extension.
|
|
35
|
-
*
|
|
36
|
-
* `SUPPORTED_IMAGE_MIME_TYPES` is keyed by MIME subtype, which doubles as the file
|
|
37
|
-
* extension for every supported type except SVG, whose subtype is `svg+xml`.
|
|
38
|
-
*
|
|
39
|
-
* @returns The matching image MIME type, or `undefined` when the extension is
|
|
40
|
-
* missing or not a supported attachment type
|
|
41
|
-
*/
|
|
42
|
-
export function inferImageContentType(filePath: string): ImageFileType | undefined {
|
|
43
|
-
const ext = extname(filePath).toLowerCase().replace(/^\./, '')
|
|
44
|
-
return SUPPORTED_IMAGE_MIME_TYPES[ext === 'svg' ? 'svg+xml' : ext]
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export type TextEdit = {
|
|
48
|
-
old_string: string
|
|
49
|
-
new_string: string
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export type ApplyTextEditsResult = { ok: true; text: string } | { ok: false; error: string }
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Applies exact string replacements in order, each against the result of the
|
|
56
|
-
* previous one, so a later edit can match text an earlier edit produced.
|
|
57
|
-
*
|
|
58
|
-
* Every `old_string` must match exactly one location at the time it is applied.
|
|
59
|
-
* Processing stops at the first edit that does not, so callers never see a
|
|
60
|
-
* partially patched text.
|
|
61
|
-
*
|
|
62
|
-
* @param text - The text to patch
|
|
63
|
-
* @param edits - The replacements to perform
|
|
64
|
-
* @returns The patched text, or an error naming the 1-based position of the failing edit
|
|
65
|
-
*/
|
|
66
|
-
export function applyTextEdits(text: string, edits: TextEdit[]): ApplyTextEditsResult {
|
|
67
|
-
let patchedText = text
|
|
68
|
-
|
|
69
|
-
for (const [editIndex, { old_string, new_string }] of edits.entries()) {
|
|
70
|
-
const editLabel = `edit ${editIndex + 1} of ${edits.length}`
|
|
71
|
-
|
|
72
|
-
const firstMatchIndex = patchedText.indexOf(old_string)
|
|
73
|
-
if (firstMatchIndex === -1) {
|
|
74
|
-
return {
|
|
75
|
-
ok: false,
|
|
76
|
-
error: `\`old_string\` of ${editLabel} was not found in the note body.`
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (patchedText.indexOf(old_string, firstMatchIndex + 1) !== -1) {
|
|
81
|
-
return {
|
|
82
|
-
ok: false,
|
|
83
|
-
error: `\`old_string\` of ${editLabel} matches multiple locations in the note body. Provide more surrounding context to make it unique.`
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
patchedText =
|
|
88
|
-
patchedText.slice(0, firstMatchIndex) +
|
|
89
|
-
new_string +
|
|
90
|
-
patchedText.slice(firstMatchIndex + old_string.length)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return { ok: true, text: patchedText }
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Content types the MCP `image` content block can carry. Inkdrop also accepts
|
|
98
|
-
* SVG and HEIC/HEIF attachments, which have to be written to disk instead.
|
|
99
|
-
*/
|
|
100
|
-
const VIEWABLE_IMAGE_TYPES = new Set<ImageFileType>([
|
|
101
|
-
'image/png',
|
|
102
|
-
'image/jpeg',
|
|
103
|
-
'image/jpg',
|
|
104
|
-
'image/gif'
|
|
105
|
-
])
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Whether an attachment can be returned as an MCP `image` content block.
|
|
109
|
-
*/
|
|
110
|
-
export function isViewableImageType(contentType: ImageFileType | undefined): boolean {
|
|
111
|
-
return contentType !== undefined && VIEWABLE_IMAGE_TYPES.has(contentType)
|
|
112
|
-
}
|