march-cli 0.1.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/bin/march.mjs +13 -0
- package/package.json +36 -0
- package/src/agent/command-exec-tool.mjs +91 -0
- package/src/agent/context-stats-tool.mjs +57 -0
- package/src/agent/editing/diff-apply.mjs +28 -0
- package/src/agent/editing/diff-format.mjs +57 -0
- package/src/agent/file-edit-tool.mjs +276 -0
- package/src/agent/find-tool.mjs +112 -0
- package/src/agent/model-payload-dumper.mjs +201 -0
- package/src/agent/pi-session/pi-session-sidecar-failure.mjs +10 -0
- package/src/agent/provider/payload-messages.mjs +138 -0
- package/src/agent/read-file-tool.mjs +112 -0
- package/src/agent/runner/fast-model.mjs +36 -0
- package/src/agent/runner/runner-cleanup.mjs +12 -0
- package/src/agent/runner/runner-init.mjs +15 -0
- package/src/agent/runner/runner-session-state.mjs +40 -0
- package/src/agent/runner.mjs +266 -0
- package/src/agent/runtime/runner-runtime-host.mjs +73 -0
- package/src/agent/runtime/runtime-factory.mjs +42 -0
- package/src/agent/runtime/runtime-host.mjs +34 -0
- package/src/agent/session/session-auto-name.mjs +41 -0
- package/src/agent/session/session-binding.mjs +12 -0
- package/src/agent/session/session-options.mjs +46 -0
- package/src/agent/tool-names.mjs +1 -0
- package/src/agent/tool-result.mjs +3 -0
- package/src/agent/tools.mjs +54 -0
- package/src/agent/turn/turn-events.mjs +64 -0
- package/src/agent/turn/turn-runner.mjs +103 -0
- package/src/auth/login-command.mjs +90 -0
- package/src/auth/storage.mjs +33 -0
- package/src/cli/args.mjs +71 -0
- package/src/cli/commands/copy-command.mjs +73 -0
- package/src/cli/commands/export-command.mjs +206 -0
- package/src/cli/commands/extensions-command.mjs +53 -0
- package/src/cli/commands/help-command.mjs +7 -0
- package/src/cli/commands/model-command.mjs +110 -0
- package/src/cli/commands/paste-image-command.mjs +43 -0
- package/src/cli/commands/provider-command.mjs +55 -0
- package/src/cli/commands/status-command.mjs +157 -0
- package/src/cli/commands/thinking-command.mjs +80 -0
- package/src/cli/fallback-ui.mjs +156 -0
- package/src/cli/input/attachment-tokens.mjs +20 -0
- package/src/cli/input/autocomplete.mjs +106 -0
- package/src/cli/input/external-editor.mjs +39 -0
- package/src/cli/input/history-store.mjs +35 -0
- package/src/cli/input/image-clipboard.mjs +55 -0
- package/src/cli/input/keybinding-dispatch.mjs +76 -0
- package/src/cli/input/keybindings.mjs +96 -0
- package/src/cli/input/mode-state.mjs +43 -0
- package/src/cli/input/prompt-templates.mjs +84 -0
- package/src/cli/input/select-with-keyboard.mjs +67 -0
- package/src/cli/permissions.mjs +103 -0
- package/src/cli/repl-commands.mjs +86 -0
- package/src/cli/repl-loop.mjs +157 -0
- package/src/cli/selector-list.mjs +21 -0
- package/src/cli/session/pi-session-switch-command.mjs +41 -0
- package/src/cli/session/session-command.mjs +23 -0
- package/src/cli/session/session-list-command.mjs +68 -0
- package/src/cli/session/session-name-command.mjs +26 -0
- package/src/cli/session/session-source-command.mjs +89 -0
- package/src/cli/session/session-switch-command.mjs +1 -0
- package/src/cli/shell/shell-command.mjs +55 -0
- package/src/cli/shell/shell-drawer-controls.mjs +33 -0
- package/src/cli/shell/shell-drawer.mjs +192 -0
- package/src/cli/shell/shell-split-layout.mjs +70 -0
- package/src/cli/slash-commands.mjs +176 -0
- package/src/cli/startup/startup-banner.mjs +17 -0
- package/src/cli/startup/startup-session.mjs +51 -0
- package/src/cli/status-line-updater.mjs +74 -0
- package/src/cli/tool-output.mjs +9 -0
- package/src/cli/tui/editor/external-editor-runner.mjs +24 -0
- package/src/cli/tui/input/mouse-selection-controller.mjs +89 -0
- package/src/cli/tui/input/mouse-tracking.mjs +20 -0
- package/src/cli/tui/layout/main-pane-layout.mjs +38 -0
- package/src/cli/tui/layout/safe-render-boundary.mjs +46 -0
- package/src/cli/tui/markdown-renderer.mjs +279 -0
- package/src/cli/tui/output/scroll-state.mjs +79 -0
- package/src/cli/tui/output/tool-card-renderer.mjs +59 -0
- package/src/cli/tui/output-buffer.mjs +297 -0
- package/src/cli/tui/permission-request-ui.mjs +18 -0
- package/src/cli/tui/recall-rendering.mjs +25 -0
- package/src/cli/tui/select/editor-select-list.mjs +111 -0
- package/src/cli/tui/selection-screen.mjs +212 -0
- package/src/cli/tui/status/retry-status.mjs +72 -0
- package/src/cli/tui/status/spinner-status.mjs +42 -0
- package/src/cli/tui/status/status-bar.mjs +88 -0
- package/src/cli/tui/syntax/highlighting.mjs +277 -0
- package/src/cli/tui/syntax/languages.mjs +91 -0
- package/src/cli/tui/syntax/tree-sitter/bash.highlights.scm +261 -0
- package/src/cli/tui/syntax/tree-sitter/c.highlights.scm +341 -0
- package/src/cli/tui/syntax/tree-sitter/cpp.highlights.scm +268 -0
- package/src/cli/tui/syntax/tree-sitter/csharp.highlights.scm +577 -0
- package/src/cli/tui/syntax/tree-sitter/css.highlights.scm +109 -0
- package/src/cli/tui/syntax/tree-sitter/diff.highlights.scm +49 -0
- package/src/cli/tui/syntax/tree-sitter/go.highlights.scm +254 -0
- package/src/cli/tui/syntax/tree-sitter/html.highlights.scm +13 -0
- package/src/cli/tui/syntax/tree-sitter/java.highlights.scm +330 -0
- package/src/cli/tui/syntax/tree-sitter/json.highlights.scm +38 -0
- package/src/cli/tui/syntax/tree-sitter/php.highlights.scm +203 -0
- package/src/cli/tui/syntax/tree-sitter/python.highlights.scm +137 -0
- package/src/cli/tui/syntax/tree-sitter/ruby.highlights.scm +309 -0
- package/src/cli/tui/syntax/tree-sitter/rust.highlights.scm +531 -0
- package/src/cli/tui/syntax/tree-sitter/toml.highlights.scm +39 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-bash.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-c-sharp.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-c.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-cpp.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-css.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-diff.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-go.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-html.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-java.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-json.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-php.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-python.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-ruby.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-rust.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-toml.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-tsx.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-typescript.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tree-sitter-yaml.wasm +0 -0
- package/src/cli/tui/syntax/tree-sitter/tsx.highlights.scm +35 -0
- package/src/cli/tui/syntax/tree-sitter/typescript.highlights.scm +35 -0
- package/src/cli/tui/syntax/tree-sitter/yaml.highlights.scm +99 -0
- package/src/cli/tui/tool-rendering.mjs +194 -0
- package/src/cli/tui/tui-diff-rendering.mjs +157 -0
- package/src/cli/tui/tui-handlers.mjs +110 -0
- package/src/cli/tui/tui-input-controller.mjs +61 -0
- package/src/cli/tui/ui-theme.mjs +148 -0
- package/src/cli/ui.mjs +299 -0
- package/src/config/config-json.mjs +73 -0
- package/src/config/dotenv.mjs +20 -0
- package/src/config/features.mjs +75 -0
- package/src/config/loader.mjs +109 -0
- package/src/config/settings-command.mjs +97 -0
- package/src/context/diagnostics.mjs +70 -0
- package/src/context/engine.mjs +148 -0
- package/src/context/injections.mjs +26 -0
- package/src/context/project-context.mjs +20 -0
- package/src/context/session-status.mjs +15 -0
- package/src/context/shell-layers.mjs +23 -0
- package/src/context/system-core/base.md +60 -0
- package/src/context/system-core/prompts/deepseek-v4-pro.md +3 -0
- package/src/context/system-core/prompts/default.md +3 -0
- package/src/context/system-core.mjs +35 -0
- package/src/debug/model-context-dumper.mjs +52 -0
- package/src/extensions/discovery.mjs +40 -0
- package/src/extensions/lifecycle-adapter.mjs +210 -0
- package/src/extensions/lifecycle-manifest.mjs +69 -0
- package/src/image-gen/index.mjs +7 -0
- package/src/image-gen/provider.mjs +231 -0
- package/src/image-gen/tool.mjs +84 -0
- package/src/lsp/client.mjs +204 -0
- package/src/lsp/diagnostic-store.mjs +39 -0
- package/src/lsp/servers.mjs +212 -0
- package/src/lsp/service.mjs +65 -0
- package/src/main.mjs +294 -0
- package/src/mcp/client.mjs +195 -0
- package/src/mcp/config.mjs +130 -0
- package/src/mcp/index.mjs +48 -0
- package/src/mcp/tools.mjs +98 -0
- package/src/memory/database.mjs +219 -0
- package/src/memory/glossary.mjs +124 -0
- package/src/memory/graph/graph-cascades.mjs +109 -0
- package/src/memory/graph/graph-diagnostics.mjs +73 -0
- package/src/memory/graph/graph-path-removal.mjs +50 -0
- package/src/memory/graph/graph-path-utils.mjs +17 -0
- package/src/memory/graph/graph-primitives.mjs +103 -0
- package/src/memory/graph/graph-read.mjs +159 -0
- package/src/memory/graph.mjs +282 -0
- package/src/memory/markdown/markdown-delete.mjs +23 -0
- package/src/memory/markdown/markdown-format.mjs +128 -0
- package/src/memory/markdown/markdown-recall.mjs +28 -0
- package/src/memory/markdown/ripgrep.mjs +16 -0
- package/src/memory/markdown/sqlite-index.mjs +87 -0
- package/src/memory/markdown-store.mjs +286 -0
- package/src/memory/markdown-tools.mjs +103 -0
- package/src/memory/search.mjs +142 -0
- package/src/memory/snapshot.mjs +86 -0
- package/src/memory/system-views.mjs +120 -0
- package/src/memory/tools.mjs +282 -0
- package/src/notification/desktop-notifier.mjs +85 -0
- package/src/platform/open-file.mjs +28 -0
- package/src/provider/config-command.mjs +129 -0
- package/src/provider/presets.mjs +72 -0
- package/src/session/attachment-display.mjs +16 -0
- package/src/session/attachment-references.mjs +65 -0
- package/src/session/attachments.mjs +140 -0
- package/src/session/persist.mjs +1 -0
- package/src/session/pi-manager.mjs +34 -0
- package/src/session/session-utils.mjs +16 -0
- package/src/session/sidecar-sync.mjs +19 -0
- package/src/session/sidecar.mjs +68 -0
- package/src/session/transcript.mjs +83 -0
- package/src/session/tree.mjs +42 -0
- package/src/shell/cli-runtime.mjs +11 -0
- package/src/shell/hints.mjs +12 -0
- package/src/shell/node-pty-adapter.mjs +81 -0
- package/src/shell/runtime-state.mjs +126 -0
- package/src/shell/runtime.mjs +244 -0
- package/src/shell/screen-buffer.mjs +136 -0
- package/src/shell/tool-read.mjs +74 -0
- package/src/shell/tools.mjs +299 -0
- package/src/supergrok/actions/image-generate.mjs +60 -0
- package/src/supergrok/actions/search.mjs +78 -0
- package/src/supergrok/auth.mjs +36 -0
- package/src/supergrok/constants.mjs +18 -0
- package/src/supergrok/oauth-provider.mjs +278 -0
- package/src/supergrok/provider.mjs +36 -0
- package/src/supergrok/response.mjs +76 -0
- package/src/supergrok/tool.mjs +61 -0
- package/src/text/ansi.mjs +3 -0
- package/src/web/config-command.mjs +43 -0
- package/src/web/fetch.mjs +78 -0
- package/src/web/presets.mjs +16 -0
- package/src/web/search.mjs +83 -0
- package/src/web/tools.mjs +107 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { ROOT_NODE_UUID } from "../database.mjs";
|
|
2
|
+
|
|
3
|
+
export function ensureNode(db, nodeUuid) {
|
|
4
|
+
const existing = db.prepare("SELECT uuid FROM nodes WHERE uuid = ?").get(nodeUuid);
|
|
5
|
+
if (!existing) {
|
|
6
|
+
db.prepare("INSERT INTO nodes (uuid) VALUES (?)").run(nodeUuid);
|
|
7
|
+
}
|
|
8
|
+
return nodeUuid;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function insertMemory(db, nodeUuid, content, deprecated = false) {
|
|
12
|
+
const result = db.prepare(
|
|
13
|
+
"INSERT INTO memories (node_uuid, content, deprecated) VALUES (?, ?, ?)"
|
|
14
|
+
).run(nodeUuid, content, deprecated ? 1 : 0);
|
|
15
|
+
return { id: Number(result.lastInsertRowid), node_uuid: nodeUuid, content, deprecated: deprecated ? 1 : 0 };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function getOrCreateEdge(db, parentUuid, childUuid, name, priority = 0, disclosure = null) {
|
|
19
|
+
const existing = db.prepare(
|
|
20
|
+
"SELECT id, parent_uuid, child_uuid, name, priority, disclosure FROM edges WHERE parent_uuid = ? AND child_uuid = ?"
|
|
21
|
+
).get(parentUuid, childUuid);
|
|
22
|
+
|
|
23
|
+
if (existing) return { edge: existing, created: false };
|
|
24
|
+
|
|
25
|
+
const result = db.prepare(
|
|
26
|
+
"INSERT INTO edges (parent_uuid, child_uuid, name, priority, disclosure) VALUES (?, ?, ?, ?, ?)"
|
|
27
|
+
).run(parentUuid, childUuid, name, priority, disclosure);
|
|
28
|
+
const edge = {
|
|
29
|
+
id: Number(result.lastInsertRowid), parent_uuid: parentUuid, child_uuid: childUuid,
|
|
30
|
+
name, priority, disclosure,
|
|
31
|
+
};
|
|
32
|
+
return { edge, created: true };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function insertPath(db, namespace, domain, path, edgeId, nodeUuid) {
|
|
36
|
+
db.prepare(
|
|
37
|
+
"INSERT OR IGNORE INTO paths (namespace, domain, path, edge_id, node_uuid) VALUES (?, ?, ?, ?, ?)"
|
|
38
|
+
).run(namespace, domain, path, edgeId, nodeUuid);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function resolveGraphPath(db, path, domain = "core", namespace = "") {
|
|
42
|
+
if (path === "") {
|
|
43
|
+
return { node_uuid: ROOT_NODE_UUID, edge: null, path_obj: null };
|
|
44
|
+
}
|
|
45
|
+
const row = db.prepare(`
|
|
46
|
+
SELECT p.namespace, p.domain, p.path, p.edge_id, p.node_uuid,
|
|
47
|
+
e.parent_uuid, e.child_uuid, e.name, e.priority, e.disclosure
|
|
48
|
+
FROM paths p
|
|
49
|
+
JOIN edges e ON p.edge_id = e.id
|
|
50
|
+
WHERE p.namespace = ? AND p.domain = ? AND p.path = ?
|
|
51
|
+
`).get(namespace, domain, path);
|
|
52
|
+
|
|
53
|
+
if (!row) return null;
|
|
54
|
+
return {
|
|
55
|
+
path_obj: { namespace: row.namespace, domain: row.domain, path: row.path, edge_id: row.edge_id, node_uuid: row.node_uuid },
|
|
56
|
+
edge: { id: row.edge_id, parent_uuid: row.parent_uuid, child_uuid: row.child_uuid, name: row.name, priority: row.priority, disclosure: row.disclosure },
|
|
57
|
+
node_uuid: row.node_uuid,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function countPathsForEdge(db, edgeId) {
|
|
62
|
+
const row = db.prepare("SELECT COUNT(*) AS cnt FROM paths WHERE edge_id = ?").get(edgeId);
|
|
63
|
+
return row.cnt;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function countMemoriesForNode(db, nodeUuid) {
|
|
67
|
+
const row = db.prepare("SELECT COUNT(*) AS cnt FROM memories WHERE node_uuid = ?").get(nodeUuid);
|
|
68
|
+
return row.cnt;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function getNextChildNumber(db, parentUuid, namespace) {
|
|
72
|
+
const rows = db.prepare(`
|
|
73
|
+
SELECT e.name FROM edges e
|
|
74
|
+
JOIN paths p ON p.edge_id = e.id
|
|
75
|
+
WHERE e.parent_uuid = ? AND p.namespace = ?
|
|
76
|
+
`).all(parentUuid, namespace);
|
|
77
|
+
let maxNum = 0;
|
|
78
|
+
for (const row of rows) {
|
|
79
|
+
const num = parseInt(row.name, 10);
|
|
80
|
+
if (!Number.isNaN(num) && num > maxNum) maxNum = num;
|
|
81
|
+
}
|
|
82
|
+
return maxNum + 1;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function wouldCreateCycle(db, parentUuid, childUuid) {
|
|
86
|
+
if (parentUuid === ROOT_NODE_UUID) return false;
|
|
87
|
+
if (parentUuid === childUuid) return true;
|
|
88
|
+
|
|
89
|
+
const visited = new Set([childUuid]);
|
|
90
|
+
const queue = [childUuid];
|
|
91
|
+
while (queue.length > 0) {
|
|
92
|
+
const current = queue.shift();
|
|
93
|
+
const rows = db.prepare("SELECT child_uuid FROM edges WHERE parent_uuid = ?").all(current);
|
|
94
|
+
for (const row of rows) {
|
|
95
|
+
if (row.child_uuid === parentUuid) return true;
|
|
96
|
+
if (!visited.has(row.child_uuid)) {
|
|
97
|
+
visited.add(row.child_uuid);
|
|
98
|
+
queue.push(row.child_uuid);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { ROOT_NODE_UUID } from "../database.mjs";
|
|
2
|
+
|
|
3
|
+
export function getMemoryByPath(db, path, domain = "core", namespace = "") {
|
|
4
|
+
if (path === "") {
|
|
5
|
+
return {
|
|
6
|
+
id: 0, node_uuid: ROOT_NODE_UUID,
|
|
7
|
+
content: `Root node for domain '${domain}'.`,
|
|
8
|
+
priority: 0, disclosure: null, deprecated: false,
|
|
9
|
+
created_at: null, domain, path: "", alias_count: 0,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const row = db.prepare(`
|
|
14
|
+
SELECT m.id, m.node_uuid, m.content, m.deprecated, m.created_at,
|
|
15
|
+
e.priority, e.disclosure, p.domain, p.path
|
|
16
|
+
FROM paths p
|
|
17
|
+
JOIN edges e ON p.edge_id = e.id
|
|
18
|
+
JOIN memories m ON m.node_uuid = e.child_uuid AND m.deprecated = 0
|
|
19
|
+
WHERE p.namespace = ? AND p.domain = ? AND p.path = ?
|
|
20
|
+
ORDER BY m.created_at DESC
|
|
21
|
+
LIMIT 1
|
|
22
|
+
`).get(namespace, domain, path);
|
|
23
|
+
|
|
24
|
+
if (!row) return null;
|
|
25
|
+
|
|
26
|
+
const totalPaths = countIncomingPaths(db, row.node_uuid, namespace);
|
|
27
|
+
const aliasCount = Math.max(0, totalPaths - 1);
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
id: row.id, node_uuid: row.node_uuid, content: row.content,
|
|
31
|
+
priority: row.priority, disclosure: row.disclosure,
|
|
32
|
+
deprecated: !!row.deprecated, created_at: row.created_at,
|
|
33
|
+
domain: row.domain, path: row.path, alias_count: aliasCount,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function getChildren(db, nodeUuid = ROOT_NODE_UUID, contextDomain = null, contextPath = null, namespace = "") {
|
|
38
|
+
const rows = db.prepare(`
|
|
39
|
+
SELECT DISTINCT e.id AS edge_id, e.child_uuid, e.name, e.priority, e.disclosure,
|
|
40
|
+
m.content, m.id AS memory_id
|
|
41
|
+
FROM edges e
|
|
42
|
+
JOIN paths p ON p.edge_id = e.id
|
|
43
|
+
JOIN memories m ON m.node_uuid = e.child_uuid AND m.deprecated = 0
|
|
44
|
+
WHERE e.parent_uuid = ? AND p.namespace = ?
|
|
45
|
+
ORDER BY e.priority ASC, e.name
|
|
46
|
+
`).all(nodeUuid, namespace);
|
|
47
|
+
|
|
48
|
+
const childUuids = [...new Set(rows.map(r => r.child_uuid))];
|
|
49
|
+
const edgeIds = [...new Set(rows.map(r => r.edge_id))];
|
|
50
|
+
|
|
51
|
+
const approxChildrenMap = {};
|
|
52
|
+
if (childUuids.length > 0) {
|
|
53
|
+
const placeholders = childUuids.map(() => "?").join(",");
|
|
54
|
+
const counts = db.prepare(`
|
|
55
|
+
SELECT e.parent_uuid, COUNT(DISTINCT e.id)
|
|
56
|
+
FROM edges e
|
|
57
|
+
JOIN paths p ON p.edge_id = e.id
|
|
58
|
+
WHERE e.parent_uuid IN (${placeholders}) AND p.namespace = ?
|
|
59
|
+
GROUP BY e.parent_uuid
|
|
60
|
+
`).all(...childUuids, namespace);
|
|
61
|
+
for (const c of counts) {
|
|
62
|
+
approxChildrenMap[c[0]] = c[1];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const pathsByEdgeId = {};
|
|
67
|
+
if (edgeIds.length > 0) {
|
|
68
|
+
const placeholders = edgeIds.map(() => "?").join(",");
|
|
69
|
+
const pathRows = db.prepare(`
|
|
70
|
+
SELECT * FROM paths WHERE namespace = ? AND edge_id IN (${placeholders})
|
|
71
|
+
`).all(namespace, ...edgeIds);
|
|
72
|
+
for (const p of pathRows) {
|
|
73
|
+
if (!pathsByEdgeId[p.edge_id]) pathsByEdgeId[p.edge_id] = [];
|
|
74
|
+
pathsByEdgeId[p.edge_id].push(p);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const prefix = contextPath ? `${contextPath}/` : null;
|
|
79
|
+
const seen = new Set();
|
|
80
|
+
const children = [];
|
|
81
|
+
|
|
82
|
+
for (const row of rows) {
|
|
83
|
+
if (seen.has(row.child_uuid)) continue;
|
|
84
|
+
seen.add(row.child_uuid);
|
|
85
|
+
|
|
86
|
+
const allPaths = pathsByEdgeId[row.edge_id] ?? [];
|
|
87
|
+
if (nodeUuid === ROOT_NODE_UUID && contextDomain) {
|
|
88
|
+
if (!allPaths.some(p => p.domain === contextDomain)) continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const pathObj = pickBestPath(allPaths, contextDomain, prefix);
|
|
92
|
+
if (!pathObj) continue;
|
|
93
|
+
|
|
94
|
+
children.push({
|
|
95
|
+
node_uuid: row.child_uuid,
|
|
96
|
+
edge_id: row.edge_id,
|
|
97
|
+
name: row.name,
|
|
98
|
+
domain: pathObj.domain,
|
|
99
|
+
path: pathObj.path,
|
|
100
|
+
content_snippet: (row.content ?? "").slice(0, 100) + ((row.content ?? "").length > 100 ? "..." : ""),
|
|
101
|
+
priority: row.priority,
|
|
102
|
+
disclosure: row.disclosure,
|
|
103
|
+
approx_children_count: approxChildrenMap[row.child_uuid] ?? 0,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return children;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function getRecentMemories(db, limit = 10, namespace = "") {
|
|
111
|
+
const rows = db.prepare(`
|
|
112
|
+
SELECT m.id AS memory_id, m.created_at, e.priority, e.disclosure, p.domain, p.path
|
|
113
|
+
FROM paths p
|
|
114
|
+
JOIN edges e ON p.edge_id = e.id
|
|
115
|
+
JOIN memories m ON m.node_uuid = e.child_uuid AND m.deprecated = 0
|
|
116
|
+
WHERE p.namespace = ?
|
|
117
|
+
ORDER BY m.created_at DESC
|
|
118
|
+
`).all(namespace);
|
|
119
|
+
|
|
120
|
+
const seen = new Set();
|
|
121
|
+
const memories = [];
|
|
122
|
+
for (const row of rows) {
|
|
123
|
+
if (seen.has(row.memory_id)) continue;
|
|
124
|
+
seen.add(row.memory_id);
|
|
125
|
+
memories.push({
|
|
126
|
+
memory_id: row.memory_id,
|
|
127
|
+
uri: `${row.domain}://${row.path}`,
|
|
128
|
+
priority: row.priority,
|
|
129
|
+
disclosure: row.disclosure,
|
|
130
|
+
created_at: row.created_at,
|
|
131
|
+
});
|
|
132
|
+
if (memories.length >= limit) break;
|
|
133
|
+
}
|
|
134
|
+
return memories;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function countIncomingPaths(db, nodeUuid, namespace = "") {
|
|
138
|
+
const row = db.prepare(
|
|
139
|
+
"SELECT COUNT(*) AS cnt FROM paths WHERE node_uuid = ? AND namespace = ?"
|
|
140
|
+
).get(nodeUuid, namespace);
|
|
141
|
+
return row.cnt;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function pickBestPath(paths, contextDomain, prefix) {
|
|
145
|
+
if (paths.length === 0) return null;
|
|
146
|
+
if (paths.length === 1) return paths[0];
|
|
147
|
+
|
|
148
|
+
if (contextDomain && prefix) {
|
|
149
|
+
for (const p of paths) {
|
|
150
|
+
if (p.domain === contextDomain && p.path.startsWith(prefix)) return p;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (contextDomain) {
|
|
154
|
+
for (const p of paths) {
|
|
155
|
+
if (p.domain === contextDomain) return p;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return paths[0];
|
|
159
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { ROOT_NODE_UUID } from "./database.mjs";
|
|
3
|
+
import {
|
|
4
|
+
getChildren,
|
|
5
|
+
getMemoryByPath,
|
|
6
|
+
getRecentMemories,
|
|
7
|
+
} from "./graph/graph-read.mjs";
|
|
8
|
+
import { getGraphDiagnostics } from "./graph/graph-diagnostics.mjs";
|
|
9
|
+
import {
|
|
10
|
+
cascadeCreatePaths,
|
|
11
|
+
cascadeDeleteNode,
|
|
12
|
+
deprecateNodeMemories,
|
|
13
|
+
} from "./graph/graph-cascades.mjs";
|
|
14
|
+
import { removeGraphPath } from "./graph/graph-path-removal.mjs";
|
|
15
|
+
import {
|
|
16
|
+
graphUri,
|
|
17
|
+
leafName,
|
|
18
|
+
pathExists,
|
|
19
|
+
} from "./graph/graph-path-utils.mjs";
|
|
20
|
+
import {
|
|
21
|
+
ensureNode,
|
|
22
|
+
getNextChildNumber,
|
|
23
|
+
getOrCreateEdge,
|
|
24
|
+
insertMemory,
|
|
25
|
+
insertPath,
|
|
26
|
+
resolveGraphPath,
|
|
27
|
+
wouldCreateCycle,
|
|
28
|
+
} from "./graph/graph-primitives.mjs";
|
|
29
|
+
|
|
30
|
+
export class GraphService {
|
|
31
|
+
constructor(db, { changesetStore = null, searchIndexer = null, namespace = "" } = {}) {
|
|
32
|
+
this.db = db;
|
|
33
|
+
this.changesetStore = changesetStore;
|
|
34
|
+
this.searchIndexer = searchIndexer;
|
|
35
|
+
this.namespace = namespace;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Resolve namespace: explicit arg takes precedence, then instance default.
|
|
40
|
+
*/
|
|
41
|
+
#ns(explicit = undefined) {
|
|
42
|
+
return explicit !== undefined ? explicit : this.namespace;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
46
|
+
// Read Operations
|
|
47
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
48
|
+
|
|
49
|
+
getMemoryByPath(path, domain = "core", namespace = "") {
|
|
50
|
+
return getMemoryByPath(this.db, path, domain, namespace);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
getChildren(nodeUuid = ROOT_NODE_UUID, contextDomain = null, contextPath = null, namespace = "") {
|
|
54
|
+
return getChildren(this.db, nodeUuid, contextDomain, contextPath, this.#ns(namespace));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
getRecentMemories(limit = 10, namespace = "") {
|
|
58
|
+
return getRecentMemories(this.db, limit, namespace);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
resolvePath(path, domain = "core", namespace = "") {
|
|
62
|
+
return resolveGraphPath(this.db, path, domain, namespace);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
getPathsForNode(nodeUuid, namespace = "") {
|
|
66
|
+
return this.db.prepare(
|
|
67
|
+
"SELECT * FROM paths WHERE node_uuid = ? AND namespace = ?"
|
|
68
|
+
).all(nodeUuid, namespace);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
getCurrentMemory(nodeUuid) {
|
|
72
|
+
return this.db.prepare(
|
|
73
|
+
"SELECT * FROM memories WHERE node_uuid = ? AND deprecated = 0 ORDER BY id DESC LIMIT 1"
|
|
74
|
+
).get(nodeUuid);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
touchNode(nodeUuid) {
|
|
78
|
+
this.db.prepare(
|
|
79
|
+
"UPDATE nodes SET last_accessed_at = datetime('now') WHERE uuid = ?"
|
|
80
|
+
).run(nodeUuid);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
getDiagnostics(namespace = "", daysStale = 30, maxChildren = 10) {
|
|
84
|
+
return getGraphDiagnostics(this.db, namespace, daysStale, maxChildren);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
cascadeDeleteNode(nodeUuid) {
|
|
88
|
+
return cascadeDeleteNode(this.db, nodeUuid);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
#createEdgeWithPaths(parentUuid, childUuid, name, domain, path, priority = 0, disclosure = null, namespace = "") {
|
|
92
|
+
const { edge, created } = getOrCreateEdge(this.db, parentUuid, childUuid, name, priority, disclosure);
|
|
93
|
+
insertPath(this.db, namespace, domain, path, edge.id, childUuid);
|
|
94
|
+
cascadeCreatePaths(this.db, childUuid, domain, path, namespace);
|
|
95
|
+
return { edge, edge_id: edge.id, edge_created: created };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
99
|
+
// Public Write API
|
|
100
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
101
|
+
|
|
102
|
+
createMemory(parentPath, content, priority, { title = null, disclosure = null, domain = "core", namespace = "" } = {}) {
|
|
103
|
+
const parentUuid = parentPath === ""
|
|
104
|
+
? ROOT_NODE_UUID
|
|
105
|
+
: resolveGraphPath(this.db, parentPath, domain, namespace)?.node_uuid;
|
|
106
|
+
|
|
107
|
+
if (!parentUuid && parentPath !== "") {
|
|
108
|
+
throw new Error(`Parent '${domain}://${parentPath}' does not exist.`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const finalPath = title
|
|
112
|
+
? (parentPath ? `${parentPath}/${title}` : title)
|
|
113
|
+
: `${parentPath ? parentPath + "/" : ""}${getNextChildNumber(this.db, parentUuid, this.#ns(namespace))}`;
|
|
114
|
+
|
|
115
|
+
if (pathExists(this.db, namespace, domain, finalPath)) throw new Error(`Path '${graphUri(domain, finalPath)}' already exists`);
|
|
116
|
+
|
|
117
|
+
const newUuid = randomUUID();
|
|
118
|
+
ensureNode(this.db, newUuid);
|
|
119
|
+
const memory = insertMemory(this.db, newUuid, content);
|
|
120
|
+
const edgeName = title ?? leafName(finalPath);
|
|
121
|
+
|
|
122
|
+
const created = this.#createEdgeWithPaths(parentUuid, newUuid, edgeName, domain, finalPath, priority, disclosure, namespace);
|
|
123
|
+
|
|
124
|
+
if (this.changesetStore) {
|
|
125
|
+
this.changesetStore.record({ memoryId: memory.id, nodeUuid: newUuid, beforeContent: null, afterContent: content });
|
|
126
|
+
}
|
|
127
|
+
if (this.searchIndexer) {
|
|
128
|
+
this.searchIndexer.index(newUuid, content, namespace);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
id: memory.id, node_uuid: newUuid, domain, path: finalPath,
|
|
133
|
+
uri: graphUri(domain, finalPath), priority,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
updateMemory(path, { content = null, priority = null, disclosure = null, domain = "core", namespace = "" } = {}) {
|
|
138
|
+
if (path === "") throw new Error("Cannot update the root node.");
|
|
139
|
+
if (content === null && priority === null && disclosure === null) {
|
|
140
|
+
throw new Error("At least one of content, priority, or disclosure must be set.");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const resolved = resolveGraphPath(this.db, path, domain, namespace);
|
|
144
|
+
if (!resolved || !resolved.edge) {
|
|
145
|
+
throw new Error(`Path '${graphUri(domain, path)}' not found`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const { edge, node_uuid: nodeUuid } = resolved;
|
|
149
|
+
let oldMemoryId = null;
|
|
150
|
+
let newMemoryId = null;
|
|
151
|
+
|
|
152
|
+
// Get current memory
|
|
153
|
+
const currentMem = this.db.prepare(
|
|
154
|
+
"SELECT id FROM memories WHERE node_uuid = ? AND deprecated = 0 ORDER BY created_at DESC LIMIT 1"
|
|
155
|
+
).get(nodeUuid);
|
|
156
|
+
oldMemoryId = currentMem?.id ?? null;
|
|
157
|
+
|
|
158
|
+
// Update edge metadata
|
|
159
|
+
if (priority !== null) {
|
|
160
|
+
this.db.prepare("UPDATE edges SET priority = ? WHERE id = ?").run(priority, edge.id);
|
|
161
|
+
}
|
|
162
|
+
if (disclosure !== null) {
|
|
163
|
+
this.db.prepare("UPDATE edges SET disclosure = ? WHERE id = ?").run(disclosure, edge.id);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Content change → new memory version
|
|
167
|
+
if (content !== null) {
|
|
168
|
+
const newMem = insertMemory(this.db, nodeUuid, content, false);
|
|
169
|
+
newMemoryId = newMem.id;
|
|
170
|
+
deprecateNodeMemories(this.db, nodeUuid, newMemoryId);
|
|
171
|
+
this.db.prepare(
|
|
172
|
+
"UPDATE memories SET deprecated = 0, migrated_to = NULL WHERE id = ?"
|
|
173
|
+
).run(newMemoryId);
|
|
174
|
+
|
|
175
|
+
// Record changeset + re-index
|
|
176
|
+
if (this.changesetStore) {
|
|
177
|
+
const oldContent = oldMemoryId
|
|
178
|
+
? this.db.prepare("SELECT content FROM memories WHERE id = ?").get(oldMemoryId)?.content ?? null
|
|
179
|
+
: null;
|
|
180
|
+
this.changesetStore.record({ memoryId: newMemoryId, nodeUuid, beforeContent: oldContent, afterContent: content });
|
|
181
|
+
}
|
|
182
|
+
if (this.searchIndexer) {
|
|
183
|
+
this.searchIndexer.index(nodeUuid, content, namespace);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
domain, path, uri: graphUri(domain, path),
|
|
189
|
+
old_memory_id: oldMemoryId, new_memory_id: newMemoryId ?? oldMemoryId, node_uuid: nodeUuid,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
rollbackToMemory(targetMemoryId) {
|
|
194
|
+
const target = this.db.prepare("SELECT * FROM memories WHERE id = ?").get(targetMemoryId);
|
|
195
|
+
if (!target) throw new Error(`Memory ID ${targetMemoryId} not found`);
|
|
196
|
+
|
|
197
|
+
if (!target.deprecated) {
|
|
198
|
+
return { restored_memory_id: targetMemoryId, was_already_active: true };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
deprecateNodeMemories(this.db, target.node_uuid, targetMemoryId);
|
|
202
|
+
this.db.prepare(
|
|
203
|
+
"UPDATE memories SET deprecated = 0, migrated_to = NULL WHERE id = ?"
|
|
204
|
+
).run(targetMemoryId);
|
|
205
|
+
|
|
206
|
+
return { restored_memory_id: targetMemoryId, node_uuid: target.node_uuid };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
addPath(newPath, targetPath, { newDomain = "core", targetDomain = "core", priority = 0, disclosure = null, namespace = "" } = {}) {
|
|
210
|
+
if (newPath === "") throw new Error("Cannot create alias at root path.");
|
|
211
|
+
|
|
212
|
+
const target = resolveGraphPath(this.db, targetPath, targetDomain, namespace);
|
|
213
|
+
if (!target) throw new Error(`Target '${graphUri(targetDomain, targetPath)}' not found`);
|
|
214
|
+
|
|
215
|
+
const parentUuid = newPath.includes("/")
|
|
216
|
+
? resolveGraphPath(this.db, newPath.substring(0, newPath.lastIndexOf("/")), newDomain, namespace)?.node_uuid ?? ROOT_NODE_UUID
|
|
217
|
+
: ROOT_NODE_UUID;
|
|
218
|
+
|
|
219
|
+
if (pathExists(this.db, namespace, newDomain, newPath)) throw new Error(`Path '${graphUri(newDomain, newPath)}' already exists`);
|
|
220
|
+
|
|
221
|
+
if (wouldCreateCycle(this.db, parentUuid, target.node_uuid)) {
|
|
222
|
+
throw new Error(`Cannot create alias: would create a cycle.`);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const result = this.#createEdgeWithPaths(
|
|
226
|
+
parentUuid, target.node_uuid,
|
|
227
|
+
leafName(newPath), newDomain, newPath,
|
|
228
|
+
priority, disclosure ?? target.edge?.disclosure, namespace,
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
new_uri: graphUri(newDomain, newPath),
|
|
233
|
+
target_uri: graphUri(targetDomain, targetPath),
|
|
234
|
+
node_uuid: target.node_uuid,
|
|
235
|
+
edge_id: result.edge_id, edge_created: result.edge_created,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
removePath(path, domain = "core", namespace = "") {
|
|
240
|
+
return removeGraphPath(this.db, path, domain, namespace);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
restorePath(path, domain, nodeUuid, { parentUuid = null, priority = 0, disclosure = null, namespace = "" } = {}) {
|
|
244
|
+
if (path === "") throw new Error("Cannot restore root path.");
|
|
245
|
+
|
|
246
|
+
// Check node exists and has an active memory
|
|
247
|
+
const node = this.db.prepare("SELECT uuid FROM nodes WHERE uuid = ?").get(nodeUuid);
|
|
248
|
+
if (!node) throw new Error(`Node '${nodeUuid}' not found`);
|
|
249
|
+
|
|
250
|
+
const activeMem = this.db.prepare(
|
|
251
|
+
"SELECT id FROM memories WHERE node_uuid = ? AND deprecated = 0"
|
|
252
|
+
).get(nodeUuid);
|
|
253
|
+
if (!activeMem) {
|
|
254
|
+
const latest = this.db.prepare(
|
|
255
|
+
"SELECT id FROM memories WHERE node_uuid = ? ORDER BY created_at DESC LIMIT 1"
|
|
256
|
+
).get(nodeUuid);
|
|
257
|
+
if (!latest) throw new Error(`Node '${nodeUuid}' has no memory versions`);
|
|
258
|
+
this.db.prepare(
|
|
259
|
+
"UPDATE memories SET deprecated = 0, migrated_to = NULL WHERE id = ?"
|
|
260
|
+
).run(latest.id);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (pathExists(this.db, namespace, domain, path)) throw new Error(`Path '${graphUri(domain, path)}' already exists`);
|
|
264
|
+
|
|
265
|
+
if (!parentUuid) {
|
|
266
|
+
if (path.includes("/")) {
|
|
267
|
+
const parentPath = path.substring(0, path.lastIndexOf("/"));
|
|
268
|
+
const parent = resolveGraphPath(this.db, parentPath, domain, namespace);
|
|
269
|
+
parentUuid = parent?.node_uuid ?? ROOT_NODE_UUID;
|
|
270
|
+
} else {
|
|
271
|
+
parentUuid = ROOT_NODE_UUID;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const edgeName = leafName(path);
|
|
276
|
+
const { edge } = getOrCreateEdge(this.db, parentUuid, nodeUuid, edgeName, priority, disclosure);
|
|
277
|
+
insertPath(this.db, namespace, domain, path, edge.id, nodeUuid);
|
|
278
|
+
|
|
279
|
+
return { uri: graphUri(domain, path), node_uuid: nodeUuid };
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { formatMemoryMarkdown, normalizeTags, parseMemoryMarkdown } from "./markdown-format.mjs";
|
|
3
|
+
|
|
4
|
+
export function softDeleteMemoryFile({ path, entry = null, now = () => new Date() } = {}) {
|
|
5
|
+
if (!existsSync(path)) throw new Error(`memory not found: ${path}`);
|
|
6
|
+
const parsed = parseMemoryMarkdown(readFileSync(path, "utf8"));
|
|
7
|
+
const id = String(parsed.frontmatter.id ?? entry?.id ?? "");
|
|
8
|
+
if (!id) throw new Error(`memory file is missing id: ${path}`);
|
|
9
|
+
if (String(parsed.frontmatter.status ?? "active") === "deleted") {
|
|
10
|
+
return { id, path, status: "deleted", alreadyDeleted: true };
|
|
11
|
+
}
|
|
12
|
+
writeFileSync(path, formatMemoryMarkdown({
|
|
13
|
+
frontmatter: {
|
|
14
|
+
...parsed.frontmatter,
|
|
15
|
+
id,
|
|
16
|
+
tags: normalizeTags(parsed.frontmatter.tags ?? entry?.tags ?? []),
|
|
17
|
+
status: "deleted",
|
|
18
|
+
updated_at: now().toISOString(),
|
|
19
|
+
},
|
|
20
|
+
body: parsed.body,
|
|
21
|
+
}), "utf8");
|
|
22
|
+
return { id, path, status: "deleted", alreadyDeleted: false };
|
|
23
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { existsSync, readdirSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
export function parseMemoryMarkdown(content) {
|
|
6
|
+
if (!content.startsWith("---\n")) return { frontmatter: {}, body: content, title: extractTitle(content) };
|
|
7
|
+
const end = content.indexOf("\n---", 4);
|
|
8
|
+
if (end === -1) throw new Error("unterminated frontmatter");
|
|
9
|
+
const frontmatter = parseFrontmatter(content.slice(4, end));
|
|
10
|
+
const body = content.slice(content.indexOf("\n", end + 1) + 1);
|
|
11
|
+
return { frontmatter, body, title: extractTitle(body) };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function formatMemoryMarkdown({ frontmatter, body }) {
|
|
15
|
+
const lines = ["---"];
|
|
16
|
+
for (const key of ["id", "name", "description", "status", "created_at", "updated_at"]) {
|
|
17
|
+
if (frontmatter[key] != null) lines.push(`${key}: ${formatYamlScalar(frontmatter[key])}`);
|
|
18
|
+
}
|
|
19
|
+
lines.push("tags:");
|
|
20
|
+
for (const tag of frontmatter.tags ?? []) lines.push(` - ${formatYamlScalar(tag)}`);
|
|
21
|
+
lines.push("---", "", String(body ?? "").trimEnd(), "");
|
|
22
|
+
return lines.join("\n");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function normalizeTags(tags) {
|
|
26
|
+
const raw = Array.isArray(tags) ? tags : [tags];
|
|
27
|
+
const out = [];
|
|
28
|
+
for (const tag of raw) {
|
|
29
|
+
const value = normalizeTag(tag);
|
|
30
|
+
if (value && !out.includes(value)) out.push(value);
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function expandTags(tags) {
|
|
36
|
+
const terms = [];
|
|
37
|
+
for (const tag of tags) {
|
|
38
|
+
terms.push(tag);
|
|
39
|
+
for (const part of tag.split(/[\/_-]+/)) {
|
|
40
|
+
if (part) terms.push(part);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return [...new Set(terms.map(normalizeText).filter(Boolean))];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function quoteFtsTerm(term) {
|
|
47
|
+
return `"${String(term).replace(/"/g, '""')}"`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function normalizeText(text) {
|
|
51
|
+
return String(text ?? "").trim().toLowerCase();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function generateMemoryId() {
|
|
55
|
+
return `mem_${randomUUID().replace(/-/g, "").slice(0, 16)}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function slugify(value) {
|
|
59
|
+
return String(value ?? "memory")
|
|
60
|
+
.trim()
|
|
61
|
+
.toLowerCase()
|
|
62
|
+
.replace(/[^a-z0-9\u4e00-\u9fff]+/g, "-")
|
|
63
|
+
.replace(/^-+|-+$/g, "")
|
|
64
|
+
.slice(0, 80) || "memory";
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function walkMarkdownFiles(root) {
|
|
68
|
+
const out = [];
|
|
69
|
+
if (!existsSync(root)) return out;
|
|
70
|
+
const walk = (dir) => {
|
|
71
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
72
|
+
const path = join(dir, entry.name);
|
|
73
|
+
if (entry.isDirectory()) walk(path);
|
|
74
|
+
else if (entry.isFile() && entry.name.toLowerCase().endsWith(".md")) out.push(path);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
walk(root);
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function normalizeTag(tag) {
|
|
82
|
+
return String(tag ?? "")
|
|
83
|
+
.trim()
|
|
84
|
+
.replace(/\s+/g, "-")
|
|
85
|
+
.replace(/\/{2,}/g, "/")
|
|
86
|
+
.toLowerCase();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function parseFrontmatter(text) {
|
|
90
|
+
const result = {};
|
|
91
|
+
const lines = text.split(/\r?\n/);
|
|
92
|
+
for (let i = 0; i < lines.length; i++) {
|
|
93
|
+
const line = lines[i];
|
|
94
|
+
const match = line.match(/^([A-Za-z0-9_-]+):\s*(.*)$/);
|
|
95
|
+
if (!match) continue;
|
|
96
|
+
const key = match[1];
|
|
97
|
+
const value = match[2];
|
|
98
|
+
if (value === "") {
|
|
99
|
+
const items = [];
|
|
100
|
+
while (i + 1 < lines.length && /^\s+-\s+/.test(lines[i + 1])) {
|
|
101
|
+
i += 1;
|
|
102
|
+
items.push(unquoteYaml(lines[i].replace(/^\s+-\s+/, "")));
|
|
103
|
+
}
|
|
104
|
+
result[key] = items;
|
|
105
|
+
} else {
|
|
106
|
+
result[key] = unquoteYaml(value);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function unquoteYaml(value) {
|
|
113
|
+
const trimmed = String(value ?? "").trim();
|
|
114
|
+
if ((trimmed.startsWith('"') && trimmed.endsWith('"')) || (trimmed.startsWith("'") && trimmed.endsWith("'"))) {
|
|
115
|
+
return trimmed.slice(1, -1);
|
|
116
|
+
}
|
|
117
|
+
return trimmed;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function formatYamlScalar(value) {
|
|
121
|
+
const text = String(value ?? "");
|
|
122
|
+
if (/[:#\n]|^\s|\s$/.test(text)) return JSON.stringify(text);
|
|
123
|
+
return text;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function extractTitle(body) {
|
|
127
|
+
return body.split(/\r?\n/).find((line) => line.startsWith("# "))?.slice(2).trim() ?? "";
|
|
128
|
+
}
|