@sanqianx/project-knowledge 4.7.0 → 5.0.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/.agents/plugins/marketplace.json +17 -17
- package/.claude-plugin/marketplace.json +1 -1
- package/CHANGELOG.md +836 -799
- package/README.md +58 -41
- package/_modules/knowledge-engine/README.md +91 -0
- package/_modules/knowledge-engine/package.json +38 -0
- package/_modules/knowledge-engine/src/analysis-contract.js +25 -0
- package/_modules/knowledge-engine/src/analysis-worker.js +54 -0
- package/_modules/knowledge-engine/src/bin.js +52 -0
- package/_modules/knowledge-engine/src/bridge-client.js +24 -0
- package/_modules/knowledge-engine/src/coordinator.js +263 -0
- package/_modules/knowledge-engine/src/headless-analyzer.js +75 -0
- package/_modules/knowledge-engine/src/memories.js +85 -0
- package/_modules/knowledge-engine/src/retrieval.js +116 -0
- package/_modules/knowledge-engine/src/runtime/README.md +7 -0
- package/_modules/knowledge-engine/src/runtime/atomic-file.js +204 -0
- package/_modules/knowledge-engine/src/runtime/automation-config.js +140 -0
- package/_modules/knowledge-engine/src/runtime/commit-conversation-binder.js +249 -0
- package/_modules/knowledge-engine/src/runtime/commit-processing-ledger.js +26 -0
- package/_modules/knowledge-engine/src/runtime/commit-prompt.js +202 -0
- package/_modules/knowledge-engine/src/runtime/commit-reconciler.js +432 -0
- package/_modules/knowledge-engine/src/runtime/content-hash.js +4 -0
- package/_modules/knowledge-engine/src/runtime/contracts.js +279 -0
- package/_modules/knowledge-engine/src/runtime/conversation-exclusions.js +204 -0
- package/_modules/knowledge-engine/src/runtime/conversation-store.js +387 -0
- package/_modules/knowledge-engine/src/runtime/evidence-bundle.js +228 -0
- package/_modules/knowledge-engine/src/runtime/git-runner.js +62 -0
- package/_modules/knowledge-engine/src/runtime/knowledge-promotion.js +444 -0
- package/_modules/knowledge-engine/src/runtime/knowledge-retrieval-service.js +402 -0
- package/_modules/knowledge-engine/src/runtime/layout.js +44 -0
- package/_modules/knowledge-engine/src/runtime/markdown-knowledge-indexer.js +230 -0
- package/_modules/knowledge-engine/src/runtime/scanner.js +298 -0
- package/_modules/knowledge-engine/src/server.js +154 -0
- package/_modules/knowledge-engine/src/store.js +236 -0
- package/_modules/knowledge-engine/ui/app.css +172 -0
- package/_modules/knowledge-engine/ui/app.js +297 -0
- package/_modules/knowledge-engine/ui/index.html +95 -0
- package/_site/README.md +30 -30
- package/_site/_test/ai-profile-resolver-test.js +137 -137
- package/_site/_test/automation-queue-test.js +14 -14
- package/_site/_test/automation-ui-test.js +40 -40
- package/_site/_test/background-task-registry-test.js +43 -43
- package/_site/_test/baseline-schema-test.js +91 -91
- package/_site/_test/chat-claudecodeui-match-test.js +92 -92
- package/_site/_test/claude-executable-discovery-test.js +62 -62
- package/_site/_test/claude-workbench-test.js +170 -170
- package/_site/_test/codex-conversation-projection-test.js +2 -1
- package/_site/_test/commit-conversation-binding-test.js +95 -95
- package/_site/_test/commit-evidence-test.js +110 -110
- package/_site/_test/conversation-api-test.js +109 -109
- package/_site/_test/conversation-store-test.js +136 -136
- package/_site/_test/desktop-browser-compat-test.js +28 -28
- package/_site/_test/desktop-hook-runtime-regression-test.js +204 -204
- package/_site/_test/explicit-commit-processor-test.js +37 -37
- package/_site/_test/fixtures/make-git-repos.js +12 -1
- package/_site/_test/folder-picker-output-test.js +19 -19
- package/_site/_test/fresh-hook-reimport-test.js +53 -0
- package/_site/_test/fresh-product-data-test.js +42 -0
- package/_site/_test/full-integration-e2e-test.js +165 -165
- package/_site/_test/git-validation-test.js +2 -2
- package/_site/_test/hook-runtime-endpoint-test.js +88 -88
- package/_site/_test/hook-status-repair-api-test.js +152 -152
- package/_site/_test/hook-trigger-test.js +85 -85
- package/_site/_test/import-preflight-api-test.js +234 -233
- package/_site/_test/index-writer-concurrency-test.js +135 -135
- package/_site/_test/integration-adapters-test.js +171 -171
- package/_site/_test/integration-surface-coverage-test.js +164 -164
- package/_site/_test/knowledge-language-control-test.js +170 -170
- package/_site/_test/knowledge-promotion-recovery-test.js +260 -260
- package/_site/_test/knowledge-query-test.js +54 -54
- package/_site/_test/knowledge-retrieval-service-test.js +77 -77
- package/_site/_test/knowledge-storage-startup-test.js +52 -52
- package/_site/_test/knowledge-store-logs-supervision-test.js +89 -89
- package/_site/_test/logging-api-test.js +89 -89
- package/_site/_test/logging-sse-no-gap-test.js +88 -88
- package/_site/_test/logging-ui-test.js +106 -104
- package/_site/_test/markdown-delta-overlay-test.js +85 -85
- package/_site/_test/markdown-maintenance-api-test.js +75 -75
- package/_site/_test/mcp-server-test.js +149 -149
- package/_site/_test/module-artifact-integrity-test.js +27 -0
- package/_site/_test/module-boundary-test.js +30 -0
- package/_site/_test/module-bridge-eventbridge-test.js +7 -0
- package/_site/_test/module-model-configuration-test.js +44 -0
- package/_site/_test/module-process-lifecycle-test.js +24 -0
- package/_site/_test/module-stream-proxy-test.js +34 -0
- package/_site/_test/non-release-ci-test.js +34 -34
- package/_site/_test/offline-boundary-isolation-test.js +32 -32
- package/_site/_test/packaged-ui-smoke-test.js +11 -9
- package/_site/_test/path-consistency-test.js +151 -151
- package/_site/_test/pending-sweep-test.js +9 -9
- package/_site/_test/post-commit-automation-test.js +87 -87
- package/_site/_test/product-diagnostics-ui-test.js +75 -0
- package/_site/_test/product-import-ui-test.js +92 -0
- package/_site/_test/project-delete-recovery-test.js +64 -64
- package/_site/_test/project-goal-editor-test.js +144 -144
- package/_site/_test/project-lifecycle-transaction-test.js +106 -106
- package/_site/_test/prompt-settings-test.js +115 -115
- package/_site/_test/protected-architecture-gate-test.js +122 -122
- package/_site/_test/refactor-characterization-test.js +36 -36
- package/_site/_test/release-version-sync-test.js +1 -1
- package/_site/_test/requirement-recorder-test.js +173 -173
- package/_site/_test/run-all-tests.js +159 -156
- package/_site/_test/shared-contracts-test.js +58 -58
- package/_site/_test/startup-analysis-disabled-test.js +10 -10
- package/_site/_test/storage-foundation-test.js +78 -78
- package/_site/_test/structured-logger-test.js +101 -101
- package/_site/_test/tracking-start-test.js +109 -108
- package/_site/_test/workbench-permission-test.js +117 -117
- package/_site/_test/workspace-ui-contract-test.js +28 -43
- package/_site/lib/ai-profile-resolver.js +78 -78
- package/_site/lib/ai-workspace.js +101 -101
- package/_site/lib/automation-config.js +140 -140
- package/_site/lib/bridge-adapter.js +1 -0
- package/_site/lib/bridge-consumer-service.js +402 -402
- package/_site/lib/claude-cli-runner.js +1510 -1510
- package/_site/lib/commit-conversation-binder.js +234 -234
- package/_site/lib/commit-processing-ledger.js +27 -27
- package/_site/lib/commit-prompt.js +202 -202
- package/_site/lib/commit-reconciler.js +406 -406
- package/_site/lib/contracts.js +284 -279
- package/_site/lib/conversation-query-service.js +144 -144
- package/_site/lib/conversation-store.js +385 -385
- package/_site/lib/data-dir.js +6 -74
- package/_site/lib/engine-connection.js +67 -0
- package/_site/lib/evidence-bundle.js +228 -228
- package/_site/lib/folder-picker-output.js +21 -21
- package/_site/lib/git-runner.js +62 -62
- package/_site/lib/github-team-store.js +1172 -1172
- package/_site/lib/hook-manager.js +203 -202
- package/_site/lib/index-service.js +173 -173
- package/_site/lib/integration-installer.js +908 -907
- package/_site/lib/kb-framework.js +176 -176
- package/_site/lib/kb-validator.js +124 -124
- package/_site/lib/knowledge-promotion.js +435 -435
- package/_site/lib/knowledge-retrieval-service.js +401 -401
- package/_site/lib/knowledge-tool-runtime.js +359 -359
- package/_site/lib/llm-client.js +161 -161
- package/_site/lib/markdown-knowledge-indexer.js +230 -230
- package/_site/lib/module-bridge.js +191 -36
- package/_site/lib/post-commit-automation.js +87 -87
- package/_site/lib/product-data.js +24 -0
- package/_site/lib/project-lifecycle-service.js +535 -497
- package/_site/lib/project-store.js +268 -262
- package/_site/lib/requirement-recorder.js +278 -278
- package/_site/lib/runtime-endpoint.js +155 -155
- package/_site/lib/scanner.js +298 -298
- package/_site/lib/server-app.js +1579 -1520
- package/_site/lib/settings-store.js +133 -113
- package/_site/lib/storage-layout.js +162 -162
- package/_site/lib/structured-logger.js +574 -574
- package/_site/scripts/folder-picker.ps1 +156 -156
- package/_site/scripts/hook-trigger.js +157 -156
- package/_site/scripts/install-module-candidates.js +69 -0
- package/_site/scripts/pack-module-candidates.js +72 -0
- package/_site/scripts/sync-release-version.js +2 -2
- package/_site/scripts/vendor-modules.js +47 -0
- package/_site/scripts/verify-module-runtime.js +45 -0
- package/_site/scripts/verify-product-runtime.js +34 -0
- package/bin/project-knowledge-mcp.js +194 -194
- package/docs/README.zh-CN.md +42 -29
- package/docs/fresh-v5-start.md +28 -0
- package/docs/project-registry-schema.md +22 -22
- package/docs/testing-strategy.md +33 -33
- package/module-runtime-manifest.json +220 -0
- package/package.json +14 -9
- package/plugins/project-knowledge/.claude-plugin/plugin.json +1 -1
- package/plugins/project-knowledge/.codex-plugin/plugin.json +1 -1
- package/plugins/project-knowledge/.mcp.json +1 -1
- package/plugins/project-knowledge/opencode/project-knowledge.md +3 -3
- package/plugins/project-knowledge/skills/project-knowledge/SKILL.md +28 -28
- package/templates/project-readme.md +34 -34
- package/ui/favicon.svg +38 -38
- package/ui/index.html +52 -149
- package/ui/product.css +9 -0
- package/ui/product.js +412 -0
- package/vendor-manifest.json +45 -0
- package/_site/_test/data-dir-migration-test.js +0 -77
- package/_site/_test/import-ui-flow-test.js +0 -171
- package/_site/_test/knowledge-migration-test.js +0 -75
- package/_site/_test/legacy-forward-compat-test.js +0 -257
- package/_site/_test/legacy-project-upgrade-e2e-test.js +0 -363
- package/_site/_test/p0-data-migration-characterization-test.js +0 -37
- package/_site/_test/p0-e2e-gate-test.js +0 -347
- package/_site/_test/project-control-panel-task14-test.js +0 -63
- package/_site/_test/project-layout-v2-migration-test.js +0 -127
- package/_site/_test/server-runtime-migration-safety-test.js +0 -27
- package/_site/_test/task15-20-ui-flow-test.js +0 -148
- package/_site/_test/ui-i18n-toggle-test.js +0 -114
- package/_site/_test/ui-smoke-test.js +0 -73
- package/_site/_test/v4122-upgrade-data-contract-test.js +0 -56
- package/_site/lib/data-state-classifier.js +0 -40
- package/_site/lib/legacy-data-manifest.js +0 -26
- package/_site/lib/migration-service.js +0 -442
- package/ui/app.css +0 -58
- package/ui/app.js +0 -607
- package/ui/i18n.js +0 -146
|
@@ -1,359 +1,359 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const { spawnSync } = require('child_process');
|
|
4
|
-
const { StorageLayout } = require('./storage-layout');
|
|
5
|
-
const { SettingsStore } = require('./settings-store');
|
|
6
|
-
const { ProjectRegistryStore } = require('./project-registry-store');
|
|
7
|
-
const { ProjectStore } = require('./project-store');
|
|
8
|
-
const { RequirementRecorder } = require('./requirement-recorder');
|
|
9
|
-
const { KnowledgeDatabase } = require('./knowledge-db');
|
|
10
|
-
const { LocalEmbeddingService } = require('./embedding-service');
|
|
11
|
-
const { EMBEDDING_DIMENSIONS } = require('./knowledge-schema');
|
|
12
|
-
const { DomainError } = require('./contracts');
|
|
13
|
-
const { KnowledgeRetrievalService } = require('./knowledge-retrieval-service');
|
|
14
|
-
const { ConversationStore } = require('./conversation-store');
|
|
15
|
-
|
|
16
|
-
function gitRoot(candidate) {
|
|
17
|
-
const resolved = path.resolve(candidate || process.cwd());
|
|
18
|
-
const cwd = fs.existsSync(resolved) && fs.statSync(resolved).isFile() ? path.dirname(resolved) : resolved;
|
|
19
|
-
const result = spawnSync('git', ['-C', cwd, 'rev-parse', '--show-toplevel'], {
|
|
20
|
-
encoding: 'utf8', windowsHide: true, env: { ...process.env, GIT_TERMINAL_PROMPT: '0' },
|
|
21
|
-
});
|
|
22
|
-
return result.status === 0 && String(result.stdout || '').trim()
|
|
23
|
-
? path.resolve(String(result.stdout).trim())
|
|
24
|
-
: cwd;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function safeLimit(value, fallback = 8, max = 30) {
|
|
28
|
-
const parsed = Number(value);
|
|
29
|
-
return Number.isFinite(parsed) ? Math.max(1, Math.min(Math.floor(parsed), max)) : fallback;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function markdownFiles(knowledgePath) {
|
|
33
|
-
const roots = [
|
|
34
|
-
path.join(knowledgePath, 'README.md'),
|
|
35
|
-
path.join(knowledgePath, 'GOAL.md'),
|
|
36
|
-
path.join(knowledgePath, 'ARCHITECTURE.md'),
|
|
37
|
-
path.join(knowledgePath, 'modules'),
|
|
38
|
-
path.join(knowledgePath, 'changes'),
|
|
39
|
-
];
|
|
40
|
-
const files = [];
|
|
41
|
-
const walk = current => {
|
|
42
|
-
if (!fs.existsSync(current)) return;
|
|
43
|
-
const stat = fs.lstatSync(current);
|
|
44
|
-
if (stat.isSymbolicLink()) return;
|
|
45
|
-
if (stat.isFile()) {
|
|
46
|
-
if (/\.md$/i.test(current) && path.basename(current).toLowerCase() !== '00-index.md') files.push(path.resolve(current));
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
if (!stat.isDirectory()) return;
|
|
50
|
-
for (const entry of fs.readdirSync(current)) walk(path.join(current, entry));
|
|
51
|
-
};
|
|
52
|
-
roots.forEach(walk);
|
|
53
|
-
return [...new Set(files)].sort();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function queryTokens(query) {
|
|
57
|
-
const normalized = String(query || '').trim().toLowerCase();
|
|
58
|
-
const words = normalized.match(/[\p{L}\p{N}_-]+/gu) || [];
|
|
59
|
-
for (const segment of normalized.match(/[\p{Script=Han}]+/gu) || []) {
|
|
60
|
-
for (let index = 0; index < segment.length - 1; index += 1) words.push(segment.slice(index, index + 2));
|
|
61
|
-
}
|
|
62
|
-
return [...new Set(words.filter(token => token.length > 1))];
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function markdownTitle(text, fallback) {
|
|
66
|
-
const match = /^#\s+(.+)$/m.exec(String(text || ''));
|
|
67
|
-
return match ? match[1].trim() : fallback;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function markdownExcerpt(text, tokens, max = 900) {
|
|
71
|
-
const normalized = String(text || '').replace(/\r\n/g, '\n');
|
|
72
|
-
const lowered = normalized.toLowerCase();
|
|
73
|
-
let first = -1;
|
|
74
|
-
for (const token of tokens) {
|
|
75
|
-
const hit = lowered.indexOf(token);
|
|
76
|
-
if (hit >= 0 && (first < 0 || hit < first)) first = hit;
|
|
77
|
-
}
|
|
78
|
-
const start = Math.max(0, first < 0 ? 0 : first - 180);
|
|
79
|
-
const excerpt = normalized.slice(start, start + max).trim();
|
|
80
|
-
return `${start > 0 ? '…' : ''}${excerpt}${start + max < normalized.length ? '…' : ''}`;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function publicIndexResult(row, scope) {
|
|
84
|
-
const { vector, tags_json, source_paths_json, routes_json, symbols_json, ...safe } = row;
|
|
85
|
-
return {
|
|
86
|
-
...safe,
|
|
87
|
-
chunk_text: markdownExcerpt(row.chunk_text, [], 900),
|
|
88
|
-
scope_reason: scope.reason,
|
|
89
|
-
scope_project_id: scope.projectId,
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
class KnowledgeToolRuntime {
|
|
94
|
-
constructor(options = {}) {
|
|
95
|
-
this.layout = options.layout || new StorageLayout({ dataDir: options.dataDir });
|
|
96
|
-
this.cwd = path.resolve(options.cwd || process.cwd());
|
|
97
|
-
this.settingsStore = options.settingsStore || new SettingsStore({ layout: this.layout });
|
|
98
|
-
this.registryStore = options.registryStore || new ProjectRegistryStore({ layout: this.layout });
|
|
99
|
-
this.projectStore = options.projectStore || new ProjectStore({ layout: this.layout });
|
|
100
|
-
this.database = null;
|
|
101
|
-
this.embedder = null;
|
|
102
|
-
this.conversationStore = options.conversationStore || new ConversationStore({
|
|
103
|
-
layout: this.layout,
|
|
104
|
-
projectStore: this.projectStore,
|
|
105
|
-
logger: options.logger,
|
|
106
|
-
});
|
|
107
|
-
this.requirementRecorder = options.requirementRecorder || new RequirementRecorder({
|
|
108
|
-
layout: this.layout,
|
|
109
|
-
registryStore: this.registryStore,
|
|
110
|
-
projectStore: this.projectStore,
|
|
111
|
-
conversationStore: this.conversationStore,
|
|
112
|
-
logger: options.logger,
|
|
113
|
-
});
|
|
114
|
-
this.retrievalService = options.retrievalService || new KnowledgeRetrievalService({
|
|
115
|
-
layout: this.layout,
|
|
116
|
-
registryStore: this.registryStore,
|
|
117
|
-
projectStore: this.projectStore,
|
|
118
|
-
databaseProvider: () => this.createDatabase(),
|
|
119
|
-
embedderProvider: () => this.createEmbedder(),
|
|
120
|
-
logger: options.logger,
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
projectAlias(projectId, config) {
|
|
125
|
-
return String(config.legacyExtensions && config.legacyExtensions.slug || projectId);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
resolveRequestedProject(requested) {
|
|
129
|
-
const ids = this.registryStore.listIds();
|
|
130
|
-
if (ids.includes(requested)) return requested;
|
|
131
|
-
const matches = ids.filter(projectId => this.projectAlias(projectId, this.projectStore.readConfig(projectId)) === requested);
|
|
132
|
-
if (matches.length > 1) throw new DomainError('PROJECT_AMBIGUOUS', 'Multiple projects use this compatibility alias.', { status: 409 });
|
|
133
|
-
if (!matches.length) throw new DomainError('PROJECT_NOT_FOUND', 'The requested project is not registered.', { status: 404 });
|
|
134
|
-
return matches[0];
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
resolveProject(input = {}) {
|
|
138
|
-
const requested = String(input.projectId || input.projectSlug || input.project || '').trim();
|
|
139
|
-
let projectId;
|
|
140
|
-
let resolvedRepoPath = '';
|
|
141
|
-
if (requested) {
|
|
142
|
-
projectId = this.resolveRequestedProject(requested);
|
|
143
|
-
} else {
|
|
144
|
-
resolvedRepoPath = gitRoot(input.repoPath || input.path || this.cwd);
|
|
145
|
-
const matches = this.registryStore.listIds().filter(candidate => {
|
|
146
|
-
const config = this.projectStore.readConfig(candidate);
|
|
147
|
-
return config.enabled !== false && this.layout.pathsEqual(config.repoPath, resolvedRepoPath);
|
|
148
|
-
});
|
|
149
|
-
if (!matches.length) throw new DomainError('PROJECT_NOT_FOUND', 'No enabled project is registered for this Git root.', { status: 404 });
|
|
150
|
-
if (matches.length > 1) throw new DomainError('PROJECT_AMBIGUOUS', 'Multiple projects are registered for this Git root.', { status: 409 });
|
|
151
|
-
[projectId] = matches;
|
|
152
|
-
}
|
|
153
|
-
const config = this.projectStore.readConfig(projectId);
|
|
154
|
-
if (config.enabled === false) throw new DomainError('PROJECT_NOT_FOUND', 'The requested project is disabled.', { status: 404 });
|
|
155
|
-
const state = this.projectStore.readState(projectId);
|
|
156
|
-
return {
|
|
157
|
-
ok: true,
|
|
158
|
-
projectId,
|
|
159
|
-
projectSlug: this.projectAlias(projectId, config),
|
|
160
|
-
displayName: config.displayName,
|
|
161
|
-
repoPath: path.resolve(resolvedRepoPath || config.repoPath),
|
|
162
|
-
knowledgePath: path.resolve(config.knowledgePath),
|
|
163
|
-
kbPath: path.resolve(config.knowledgePath),
|
|
164
|
-
indexPath: this.layout.getIndexPath(),
|
|
165
|
-
primarySpaceId: `project:${projectId}`,
|
|
166
|
-
relatedProjectIds: Array.isArray(config.relatedProjectIds) ? [...config.relatedProjectIds] : [],
|
|
167
|
-
index: { ...state.index },
|
|
168
|
-
readOnly: true,
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
scopes(project) {
|
|
173
|
-
const scopes = [{ projectId: project.projectId, spaceId: project.primarySpaceId, weight: 1, reason: 'primary' }];
|
|
174
|
-
for (const projectId of project.relatedProjectIds) {
|
|
175
|
-
if (!this.registryStore.readDisplaySnapshot(projectId)) continue;
|
|
176
|
-
const config = this.projectStore.readConfig(projectId);
|
|
177
|
-
if (config.enabled === false) continue;
|
|
178
|
-
scopes.push({ projectId, spaceId: `project:${projectId}`, weight: 0.88, reason: 'related' });
|
|
179
|
-
}
|
|
180
|
-
return scopes;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
indexAvailability(project) {
|
|
184
|
-
if (project.index.dirty) return { usable: false, status: 'dirty', reason: 'index-dirty' };
|
|
185
|
-
const indexPath = this.layout.getIndexPath();
|
|
186
|
-
if (!fs.existsSync(indexPath)) return { usable: false, status: 'unavailable', reason: 'index-missing' };
|
|
187
|
-
return { usable: true, status: 'ok', reason: '' };
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
createEmbedder() {
|
|
191
|
-
if (this.embedder) return this.embedder;
|
|
192
|
-
if (process.env.KB_EMBEDDING_FAKE === '1') {
|
|
193
|
-
this.embedder = {
|
|
194
|
-
embedQuery: async text => {
|
|
195
|
-
const vector = new Array(EMBEDDING_DIMENSIONS).fill(0);
|
|
196
|
-
for (let index = 0; index < String(text).length; index += 1) vector[String(text).charCodeAt(index) % vector.length] += 1;
|
|
197
|
-
const norm = Math.sqrt(vector.reduce((sum, item) => sum + item * item, 0)) || 1;
|
|
198
|
-
return vector.map(item => item / norm);
|
|
199
|
-
},
|
|
200
|
-
};
|
|
201
|
-
return this.embedder;
|
|
202
|
-
}
|
|
203
|
-
const settings = this.settingsStore.read({ allowMissing: true });
|
|
204
|
-
this.embedder = new LocalEmbeddingService({
|
|
205
|
-
modelId: settings.embedding.modelId,
|
|
206
|
-
cacheDir: this.layout.getCachePath('models'),
|
|
207
|
-
remoteHost: settings.embedding.remoteHost,
|
|
208
|
-
localModelPath: settings.embedding.localModelPath,
|
|
209
|
-
localFilesOnly: settings.embedding.localFilesOnly === true,
|
|
210
|
-
});
|
|
211
|
-
return this.embedder;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
createDatabase() {
|
|
215
|
-
if (!this.database) {
|
|
216
|
-
this.database = new KnowledgeDatabase({
|
|
217
|
-
dbPath: this.layout.getIndexPath(),
|
|
218
|
-
maintenancePath: this.layout.getRuntimePath('index-maintenance.json'),
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
return this.database;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
async searchIndex(project, input) {
|
|
225
|
-
const query = String(input.query || '').trim();
|
|
226
|
-
const scopes = this.scopes(project);
|
|
227
|
-
const vector = await this.createEmbedder().embedQuery(query);
|
|
228
|
-
const limit = safeLimit(input.limit);
|
|
229
|
-
const rows = await this.createDatabase().hybridSearch({
|
|
230
|
-
text: query,
|
|
231
|
-
vector,
|
|
232
|
-
spaceIds: scopes.map(scope => scope.spaceId),
|
|
233
|
-
limit: Math.min(limit * 4, 100),
|
|
234
|
-
candidates: Math.min(limit * 8, 200),
|
|
235
|
-
});
|
|
236
|
-
const bySpace = new Map(scopes.map(scope => [scope.spaceId, scope]));
|
|
237
|
-
const results = rows.map(row => {
|
|
238
|
-
const scope = bySpace.get(row.space_id) || scopes[0];
|
|
239
|
-
return { ...publicIndexResult(row, scope), relevance_score: Number(row.relevance_score || 0) * scope.weight };
|
|
240
|
-
}).sort((left, right) => right.relevance_score - left.relevance_score).slice(0, limit);
|
|
241
|
-
return { ok: true, projectId: project.projectId, projectSlug: project.projectSlug, query, backend: 'lancedb', source: 'derived-index', health: { index: 'ok' }, results };
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
searchMarkdown(project, input = {}, health = { index: 'unavailable' }) {
|
|
245
|
-
const query = String(input.query || '').trim();
|
|
246
|
-
if (!query) throw new DomainError('INVALID_ARGUMENT', 'query is required.');
|
|
247
|
-
const tokens = queryTokens(query);
|
|
248
|
-
const loweredQuery = query.toLowerCase();
|
|
249
|
-
const sources = this.scopes(project).map(scope => {
|
|
250
|
-
const config = scope.projectId === project.projectId ? null : this.projectStore.readConfig(scope.projectId);
|
|
251
|
-
return { scope, knowledgePath: config ? path.resolve(config.knowledgePath) : project.knowledgePath };
|
|
252
|
-
});
|
|
253
|
-
const results = sources.flatMap(source => markdownFiles(source.knowledgePath).map(file => {
|
|
254
|
-
const text = fs.readFileSync(file, 'utf8');
|
|
255
|
-
const lowered = text.toLowerCase();
|
|
256
|
-
let score = lowered.includes(loweredQuery) ? 20 : 0;
|
|
257
|
-
for (const token of tokens) {
|
|
258
|
-
let offset = 0;
|
|
259
|
-
let matches = 0;
|
|
260
|
-
while ((offset = lowered.indexOf(token, offset)) >= 0 && matches < 20) {
|
|
261
|
-
matches += 1;
|
|
262
|
-
offset += token.length;
|
|
263
|
-
}
|
|
264
|
-
score += matches;
|
|
265
|
-
}
|
|
266
|
-
if (!score) return null;
|
|
267
|
-
const entryId = path.relative(source.knowledgePath, file).replace(/\\/g, '/');
|
|
268
|
-
return {
|
|
269
|
-
entry_id: entryId,
|
|
270
|
-
title: markdownTitle(text, path.basename(file, '.md')),
|
|
271
|
-
chunk_text: markdownExcerpt(text, tokens),
|
|
272
|
-
relevance_score: score * source.scope.weight,
|
|
273
|
-
space_id: source.scope.spaceId,
|
|
274
|
-
scope_project_id: source.scope.projectId,
|
|
275
|
-
};
|
|
276
|
-
})).filter(Boolean).sort((left, right) => right.relevance_score - left.relevance_score).slice(0, safeLimit(input.limit));
|
|
277
|
-
return { ok: true, projectId: project.projectId, projectSlug: project.projectSlug, query, backend: 'markdown', source: 'markdown-fallback', health, results };
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
async search(input = {}) {
|
|
281
|
-
const project = this.resolveProject(input);
|
|
282
|
-
return this.retrievalService.search({ ...input, project });
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
async ask(input = {}) {
|
|
286
|
-
const searched = await this.search(input);
|
|
287
|
-
const citations = searched.results.slice(0, 5).map((result, index) => ({
|
|
288
|
-
index: index + 1,
|
|
289
|
-
projectId: result.scope_project_id || searched.projectId,
|
|
290
|
-
entryId: result.entry_id,
|
|
291
|
-
title: result.title,
|
|
292
|
-
text: result.chunk_text,
|
|
293
|
-
}));
|
|
294
|
-
return {
|
|
295
|
-
...searched,
|
|
296
|
-
answer: citations.length ? citations.map(item => `${item.index}. ${item.title}: ${item.text}`).join('\n') : 'No relevant knowledge was found.',
|
|
297
|
-
citations,
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
async get(input = {}) {
|
|
302
|
-
const project = this.resolveProject(input);
|
|
303
|
-
const entryId = String(input.entryId || input.entry || '').trim().replace(/\\/g, '/');
|
|
304
|
-
if (!entryId) throw new DomainError('INVALID_ARGUMENT', 'entry is required.');
|
|
305
|
-
const target = path.resolve(project.knowledgePath, ...entryId.split('/'));
|
|
306
|
-
if (!/\.md$/i.test(target) || !this.layout.isPathInside(project.knowledgePath, target, { realpath: true })) {
|
|
307
|
-
throw new DomainError('PATH_OUTSIDE_ROOT', 'entry is outside the project knowledge base.', { status: 403 });
|
|
308
|
-
}
|
|
309
|
-
if (!fs.existsSync(target) || !fs.statSync(target).isFile() || fs.lstatSync(target).isSymbolicLink()) {
|
|
310
|
-
throw new DomainError('PROJECT_NOT_FOUND', 'Knowledge entry was not found.', { status: 404 });
|
|
311
|
-
}
|
|
312
|
-
return {
|
|
313
|
-
ok: true,
|
|
314
|
-
projectId: project.projectId,
|
|
315
|
-
projectSlug: project.projectSlug,
|
|
316
|
-
entryId: path.relative(project.knowledgePath, target).replace(/\\/g, '/'),
|
|
317
|
-
source: 'markdown',
|
|
318
|
-
chunks: [{ chunk_text: fs.readFileSync(target, 'utf8'), entry_id: entryId, space_id: project.primarySpaceId }],
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
async history(input = {}) {
|
|
323
|
-
const project = this.resolveProject(input);
|
|
324
|
-
const changesRoot = path.join(project.knowledgePath, 'changes');
|
|
325
|
-
const results = markdownFiles(project.knowledgePath).filter(file => this.layout.isPathInside(changesRoot, file, { realpath: true })).map(file => {
|
|
326
|
-
const text = fs.readFileSync(file, 'utf8');
|
|
327
|
-
return {
|
|
328
|
-
entry_id: path.relative(project.knowledgePath, file).replace(/\\/g, '/'),
|
|
329
|
-
title: markdownTitle(text, path.basename(file, '.md')),
|
|
330
|
-
chunk_text: markdownExcerpt(text, [], 900),
|
|
331
|
-
updated_at: fs.statSync(file).mtime.toISOString(),
|
|
332
|
-
};
|
|
333
|
-
}).sort((left, right) => right.updated_at.localeCompare(left.updated_at)).slice(0, safeLimit(input.limit, 20, 100));
|
|
334
|
-
return { ok: true, projectId: project.projectId, projectSlug: project.projectSlug, backend: 'markdown', source: 'markdown', results };
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
async recordRequirement(input = {}) {
|
|
338
|
-
const record = await this.requirementRecorder.recordRequirement(input);
|
|
339
|
-
return {
|
|
340
|
-
ok: true,
|
|
341
|
-
projectId: record.projectId,
|
|
342
|
-
requirementId: record.id,
|
|
343
|
-
requirementHash: record.requirementHash,
|
|
344
|
-
recordedAt: record.ts,
|
|
345
|
-
branch: record.branch,
|
|
346
|
-
headAtRecord: record.headAtRecord,
|
|
347
|
-
turnId: record.turnId,
|
|
348
|
-
};
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
async close() {
|
|
352
|
-
const database = this.database;
|
|
353
|
-
this.database = null;
|
|
354
|
-
this.embedder = null;
|
|
355
|
-
if (database) await database.close();
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
module.exports = { KnowledgeToolRuntime, gitRoot, markdownFiles, queryTokens };
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { spawnSync } = require('child_process');
|
|
4
|
+
const { StorageLayout } = require('./storage-layout');
|
|
5
|
+
const { SettingsStore } = require('./settings-store');
|
|
6
|
+
const { ProjectRegistryStore } = require('./project-registry-store');
|
|
7
|
+
const { ProjectStore } = require('./project-store');
|
|
8
|
+
const { RequirementRecorder } = require('./requirement-recorder');
|
|
9
|
+
const { KnowledgeDatabase } = require('./knowledge-db');
|
|
10
|
+
const { LocalEmbeddingService } = require('./embedding-service');
|
|
11
|
+
const { EMBEDDING_DIMENSIONS } = require('./knowledge-schema');
|
|
12
|
+
const { DomainError } = require('./contracts');
|
|
13
|
+
const { KnowledgeRetrievalService } = require('./knowledge-retrieval-service');
|
|
14
|
+
const { ConversationStore } = require('./conversation-store');
|
|
15
|
+
|
|
16
|
+
function gitRoot(candidate) {
|
|
17
|
+
const resolved = path.resolve(candidate || process.cwd());
|
|
18
|
+
const cwd = fs.existsSync(resolved) && fs.statSync(resolved).isFile() ? path.dirname(resolved) : resolved;
|
|
19
|
+
const result = spawnSync('git', ['-C', cwd, 'rev-parse', '--show-toplevel'], {
|
|
20
|
+
encoding: 'utf8', windowsHide: true, env: { ...process.env, GIT_TERMINAL_PROMPT: '0' },
|
|
21
|
+
});
|
|
22
|
+
return result.status === 0 && String(result.stdout || '').trim()
|
|
23
|
+
? path.resolve(String(result.stdout).trim())
|
|
24
|
+
: cwd;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function safeLimit(value, fallback = 8, max = 30) {
|
|
28
|
+
const parsed = Number(value);
|
|
29
|
+
return Number.isFinite(parsed) ? Math.max(1, Math.min(Math.floor(parsed), max)) : fallback;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function markdownFiles(knowledgePath) {
|
|
33
|
+
const roots = [
|
|
34
|
+
path.join(knowledgePath, 'README.md'),
|
|
35
|
+
path.join(knowledgePath, 'GOAL.md'),
|
|
36
|
+
path.join(knowledgePath, 'ARCHITECTURE.md'),
|
|
37
|
+
path.join(knowledgePath, 'modules'),
|
|
38
|
+
path.join(knowledgePath, 'changes'),
|
|
39
|
+
];
|
|
40
|
+
const files = [];
|
|
41
|
+
const walk = current => {
|
|
42
|
+
if (!fs.existsSync(current)) return;
|
|
43
|
+
const stat = fs.lstatSync(current);
|
|
44
|
+
if (stat.isSymbolicLink()) return;
|
|
45
|
+
if (stat.isFile()) {
|
|
46
|
+
if (/\.md$/i.test(current) && path.basename(current).toLowerCase() !== '00-index.md') files.push(path.resolve(current));
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (!stat.isDirectory()) return;
|
|
50
|
+
for (const entry of fs.readdirSync(current)) walk(path.join(current, entry));
|
|
51
|
+
};
|
|
52
|
+
roots.forEach(walk);
|
|
53
|
+
return [...new Set(files)].sort();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function queryTokens(query) {
|
|
57
|
+
const normalized = String(query || '').trim().toLowerCase();
|
|
58
|
+
const words = normalized.match(/[\p{L}\p{N}_-]+/gu) || [];
|
|
59
|
+
for (const segment of normalized.match(/[\p{Script=Han}]+/gu) || []) {
|
|
60
|
+
for (let index = 0; index < segment.length - 1; index += 1) words.push(segment.slice(index, index + 2));
|
|
61
|
+
}
|
|
62
|
+
return [...new Set(words.filter(token => token.length > 1))];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function markdownTitle(text, fallback) {
|
|
66
|
+
const match = /^#\s+(.+)$/m.exec(String(text || ''));
|
|
67
|
+
return match ? match[1].trim() : fallback;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function markdownExcerpt(text, tokens, max = 900) {
|
|
71
|
+
const normalized = String(text || '').replace(/\r\n/g, '\n');
|
|
72
|
+
const lowered = normalized.toLowerCase();
|
|
73
|
+
let first = -1;
|
|
74
|
+
for (const token of tokens) {
|
|
75
|
+
const hit = lowered.indexOf(token);
|
|
76
|
+
if (hit >= 0 && (first < 0 || hit < first)) first = hit;
|
|
77
|
+
}
|
|
78
|
+
const start = Math.max(0, first < 0 ? 0 : first - 180);
|
|
79
|
+
const excerpt = normalized.slice(start, start + max).trim();
|
|
80
|
+
return `${start > 0 ? '…' : ''}${excerpt}${start + max < normalized.length ? '…' : ''}`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function publicIndexResult(row, scope) {
|
|
84
|
+
const { vector, tags_json, source_paths_json, routes_json, symbols_json, ...safe } = row;
|
|
85
|
+
return {
|
|
86
|
+
...safe,
|
|
87
|
+
chunk_text: markdownExcerpt(row.chunk_text, [], 900),
|
|
88
|
+
scope_reason: scope.reason,
|
|
89
|
+
scope_project_id: scope.projectId,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
class KnowledgeToolRuntime {
|
|
94
|
+
constructor(options = {}) {
|
|
95
|
+
this.layout = options.layout || new StorageLayout({ dataDir: options.dataDir });
|
|
96
|
+
this.cwd = path.resolve(options.cwd || process.cwd());
|
|
97
|
+
this.settingsStore = options.settingsStore || new SettingsStore({ layout: this.layout });
|
|
98
|
+
this.registryStore = options.registryStore || new ProjectRegistryStore({ layout: this.layout });
|
|
99
|
+
this.projectStore = options.projectStore || new ProjectStore({ layout: this.layout });
|
|
100
|
+
this.database = null;
|
|
101
|
+
this.embedder = null;
|
|
102
|
+
this.conversationStore = options.conversationStore || new ConversationStore({
|
|
103
|
+
layout: this.layout,
|
|
104
|
+
projectStore: this.projectStore,
|
|
105
|
+
logger: options.logger,
|
|
106
|
+
});
|
|
107
|
+
this.requirementRecorder = options.requirementRecorder || new RequirementRecorder({
|
|
108
|
+
layout: this.layout,
|
|
109
|
+
registryStore: this.registryStore,
|
|
110
|
+
projectStore: this.projectStore,
|
|
111
|
+
conversationStore: this.conversationStore,
|
|
112
|
+
logger: options.logger,
|
|
113
|
+
});
|
|
114
|
+
this.retrievalService = options.retrievalService || new KnowledgeRetrievalService({
|
|
115
|
+
layout: this.layout,
|
|
116
|
+
registryStore: this.registryStore,
|
|
117
|
+
projectStore: this.projectStore,
|
|
118
|
+
databaseProvider: () => this.createDatabase(),
|
|
119
|
+
embedderProvider: () => this.createEmbedder(),
|
|
120
|
+
logger: options.logger,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
projectAlias(projectId, config) {
|
|
125
|
+
return String(config.legacyExtensions && config.legacyExtensions.slug || projectId);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
resolveRequestedProject(requested) {
|
|
129
|
+
const ids = this.registryStore.listIds();
|
|
130
|
+
if (ids.includes(requested)) return requested;
|
|
131
|
+
const matches = ids.filter(projectId => this.projectAlias(projectId, this.projectStore.readConfig(projectId)) === requested);
|
|
132
|
+
if (matches.length > 1) throw new DomainError('PROJECT_AMBIGUOUS', 'Multiple projects use this compatibility alias.', { status: 409 });
|
|
133
|
+
if (!matches.length) throw new DomainError('PROJECT_NOT_FOUND', 'The requested project is not registered.', { status: 404 });
|
|
134
|
+
return matches[0];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
resolveProject(input = {}) {
|
|
138
|
+
const requested = String(input.projectId || input.projectSlug || input.project || '').trim();
|
|
139
|
+
let projectId;
|
|
140
|
+
let resolvedRepoPath = '';
|
|
141
|
+
if (requested) {
|
|
142
|
+
projectId = this.resolveRequestedProject(requested);
|
|
143
|
+
} else {
|
|
144
|
+
resolvedRepoPath = gitRoot(input.repoPath || input.path || this.cwd);
|
|
145
|
+
const matches = this.registryStore.listIds().filter(candidate => {
|
|
146
|
+
const config = this.projectStore.readConfig(candidate);
|
|
147
|
+
return config.enabled !== false && this.layout.pathsEqual(config.repoPath, resolvedRepoPath);
|
|
148
|
+
});
|
|
149
|
+
if (!matches.length) throw new DomainError('PROJECT_NOT_FOUND', 'No enabled project is registered for this Git root.', { status: 404 });
|
|
150
|
+
if (matches.length > 1) throw new DomainError('PROJECT_AMBIGUOUS', 'Multiple projects are registered for this Git root.', { status: 409 });
|
|
151
|
+
[projectId] = matches;
|
|
152
|
+
}
|
|
153
|
+
const config = this.projectStore.readConfig(projectId);
|
|
154
|
+
if (config.enabled === false) throw new DomainError('PROJECT_NOT_FOUND', 'The requested project is disabled.', { status: 404 });
|
|
155
|
+
const state = this.projectStore.readState(projectId);
|
|
156
|
+
return {
|
|
157
|
+
ok: true,
|
|
158
|
+
projectId,
|
|
159
|
+
projectSlug: this.projectAlias(projectId, config),
|
|
160
|
+
displayName: config.displayName,
|
|
161
|
+
repoPath: path.resolve(resolvedRepoPath || config.repoPath),
|
|
162
|
+
knowledgePath: path.resolve(config.knowledgePath),
|
|
163
|
+
kbPath: path.resolve(config.knowledgePath),
|
|
164
|
+
indexPath: this.layout.getIndexPath(),
|
|
165
|
+
primarySpaceId: `project:${projectId}`,
|
|
166
|
+
relatedProjectIds: Array.isArray(config.relatedProjectIds) ? [...config.relatedProjectIds] : [],
|
|
167
|
+
index: { ...state.index },
|
|
168
|
+
readOnly: true,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
scopes(project) {
|
|
173
|
+
const scopes = [{ projectId: project.projectId, spaceId: project.primarySpaceId, weight: 1, reason: 'primary' }];
|
|
174
|
+
for (const projectId of project.relatedProjectIds) {
|
|
175
|
+
if (!this.registryStore.readDisplaySnapshot(projectId)) continue;
|
|
176
|
+
const config = this.projectStore.readConfig(projectId);
|
|
177
|
+
if (config.enabled === false) continue;
|
|
178
|
+
scopes.push({ projectId, spaceId: `project:${projectId}`, weight: 0.88, reason: 'related' });
|
|
179
|
+
}
|
|
180
|
+
return scopes;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
indexAvailability(project) {
|
|
184
|
+
if (project.index.dirty) return { usable: false, status: 'dirty', reason: 'index-dirty' };
|
|
185
|
+
const indexPath = this.layout.getIndexPath();
|
|
186
|
+
if (!fs.existsSync(indexPath)) return { usable: false, status: 'unavailable', reason: 'index-missing' };
|
|
187
|
+
return { usable: true, status: 'ok', reason: '' };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
createEmbedder() {
|
|
191
|
+
if (this.embedder) return this.embedder;
|
|
192
|
+
if (process.env.KB_EMBEDDING_FAKE === '1') {
|
|
193
|
+
this.embedder = {
|
|
194
|
+
embedQuery: async text => {
|
|
195
|
+
const vector = new Array(EMBEDDING_DIMENSIONS).fill(0);
|
|
196
|
+
for (let index = 0; index < String(text).length; index += 1) vector[String(text).charCodeAt(index) % vector.length] += 1;
|
|
197
|
+
const norm = Math.sqrt(vector.reduce((sum, item) => sum + item * item, 0)) || 1;
|
|
198
|
+
return vector.map(item => item / norm);
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
return this.embedder;
|
|
202
|
+
}
|
|
203
|
+
const settings = this.settingsStore.read({ allowMissing: true });
|
|
204
|
+
this.embedder = new LocalEmbeddingService({
|
|
205
|
+
modelId: settings.embedding.modelId,
|
|
206
|
+
cacheDir: this.layout.getCachePath('models'),
|
|
207
|
+
remoteHost: settings.embedding.remoteHost,
|
|
208
|
+
localModelPath: settings.embedding.localModelPath,
|
|
209
|
+
localFilesOnly: settings.embedding.localFilesOnly === true,
|
|
210
|
+
});
|
|
211
|
+
return this.embedder;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
createDatabase() {
|
|
215
|
+
if (!this.database) {
|
|
216
|
+
this.database = new KnowledgeDatabase({
|
|
217
|
+
dbPath: this.layout.getIndexPath(),
|
|
218
|
+
maintenancePath: this.layout.getRuntimePath('index-maintenance.json'),
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
return this.database;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async searchIndex(project, input) {
|
|
225
|
+
const query = String(input.query || '').trim();
|
|
226
|
+
const scopes = this.scopes(project);
|
|
227
|
+
const vector = await this.createEmbedder().embedQuery(query);
|
|
228
|
+
const limit = safeLimit(input.limit);
|
|
229
|
+
const rows = await this.createDatabase().hybridSearch({
|
|
230
|
+
text: query,
|
|
231
|
+
vector,
|
|
232
|
+
spaceIds: scopes.map(scope => scope.spaceId),
|
|
233
|
+
limit: Math.min(limit * 4, 100),
|
|
234
|
+
candidates: Math.min(limit * 8, 200),
|
|
235
|
+
});
|
|
236
|
+
const bySpace = new Map(scopes.map(scope => [scope.spaceId, scope]));
|
|
237
|
+
const results = rows.map(row => {
|
|
238
|
+
const scope = bySpace.get(row.space_id) || scopes[0];
|
|
239
|
+
return { ...publicIndexResult(row, scope), relevance_score: Number(row.relevance_score || 0) * scope.weight };
|
|
240
|
+
}).sort((left, right) => right.relevance_score - left.relevance_score).slice(0, limit);
|
|
241
|
+
return { ok: true, projectId: project.projectId, projectSlug: project.projectSlug, query, backend: 'lancedb', source: 'derived-index', health: { index: 'ok' }, results };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
searchMarkdown(project, input = {}, health = { index: 'unavailable' }) {
|
|
245
|
+
const query = String(input.query || '').trim();
|
|
246
|
+
if (!query) throw new DomainError('INVALID_ARGUMENT', 'query is required.');
|
|
247
|
+
const tokens = queryTokens(query);
|
|
248
|
+
const loweredQuery = query.toLowerCase();
|
|
249
|
+
const sources = this.scopes(project).map(scope => {
|
|
250
|
+
const config = scope.projectId === project.projectId ? null : this.projectStore.readConfig(scope.projectId);
|
|
251
|
+
return { scope, knowledgePath: config ? path.resolve(config.knowledgePath) : project.knowledgePath };
|
|
252
|
+
});
|
|
253
|
+
const results = sources.flatMap(source => markdownFiles(source.knowledgePath).map(file => {
|
|
254
|
+
const text = fs.readFileSync(file, 'utf8');
|
|
255
|
+
const lowered = text.toLowerCase();
|
|
256
|
+
let score = lowered.includes(loweredQuery) ? 20 : 0;
|
|
257
|
+
for (const token of tokens) {
|
|
258
|
+
let offset = 0;
|
|
259
|
+
let matches = 0;
|
|
260
|
+
while ((offset = lowered.indexOf(token, offset)) >= 0 && matches < 20) {
|
|
261
|
+
matches += 1;
|
|
262
|
+
offset += token.length;
|
|
263
|
+
}
|
|
264
|
+
score += matches;
|
|
265
|
+
}
|
|
266
|
+
if (!score) return null;
|
|
267
|
+
const entryId = path.relative(source.knowledgePath, file).replace(/\\/g, '/');
|
|
268
|
+
return {
|
|
269
|
+
entry_id: entryId,
|
|
270
|
+
title: markdownTitle(text, path.basename(file, '.md')),
|
|
271
|
+
chunk_text: markdownExcerpt(text, tokens),
|
|
272
|
+
relevance_score: score * source.scope.weight,
|
|
273
|
+
space_id: source.scope.spaceId,
|
|
274
|
+
scope_project_id: source.scope.projectId,
|
|
275
|
+
};
|
|
276
|
+
})).filter(Boolean).sort((left, right) => right.relevance_score - left.relevance_score).slice(0, safeLimit(input.limit));
|
|
277
|
+
return { ok: true, projectId: project.projectId, projectSlug: project.projectSlug, query, backend: 'markdown', source: 'markdown-fallback', health, results };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
async search(input = {}) {
|
|
281
|
+
const project = this.resolveProject(input);
|
|
282
|
+
return this.retrievalService.search({ ...input, project });
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async ask(input = {}) {
|
|
286
|
+
const searched = await this.search(input);
|
|
287
|
+
const citations = searched.results.slice(0, 5).map((result, index) => ({
|
|
288
|
+
index: index + 1,
|
|
289
|
+
projectId: result.scope_project_id || searched.projectId,
|
|
290
|
+
entryId: result.entry_id,
|
|
291
|
+
title: result.title,
|
|
292
|
+
text: result.chunk_text,
|
|
293
|
+
}));
|
|
294
|
+
return {
|
|
295
|
+
...searched,
|
|
296
|
+
answer: citations.length ? citations.map(item => `${item.index}. ${item.title}: ${item.text}`).join('\n') : 'No relevant knowledge was found.',
|
|
297
|
+
citations,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
async get(input = {}) {
|
|
302
|
+
const project = this.resolveProject(input);
|
|
303
|
+
const entryId = String(input.entryId || input.entry || '').trim().replace(/\\/g, '/');
|
|
304
|
+
if (!entryId) throw new DomainError('INVALID_ARGUMENT', 'entry is required.');
|
|
305
|
+
const target = path.resolve(project.knowledgePath, ...entryId.split('/'));
|
|
306
|
+
if (!/\.md$/i.test(target) || !this.layout.isPathInside(project.knowledgePath, target, { realpath: true })) {
|
|
307
|
+
throw new DomainError('PATH_OUTSIDE_ROOT', 'entry is outside the project knowledge base.', { status: 403 });
|
|
308
|
+
}
|
|
309
|
+
if (!fs.existsSync(target) || !fs.statSync(target).isFile() || fs.lstatSync(target).isSymbolicLink()) {
|
|
310
|
+
throw new DomainError('PROJECT_NOT_FOUND', 'Knowledge entry was not found.', { status: 404 });
|
|
311
|
+
}
|
|
312
|
+
return {
|
|
313
|
+
ok: true,
|
|
314
|
+
projectId: project.projectId,
|
|
315
|
+
projectSlug: project.projectSlug,
|
|
316
|
+
entryId: path.relative(project.knowledgePath, target).replace(/\\/g, '/'),
|
|
317
|
+
source: 'markdown',
|
|
318
|
+
chunks: [{ chunk_text: fs.readFileSync(target, 'utf8'), entry_id: entryId, space_id: project.primarySpaceId }],
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
async history(input = {}) {
|
|
323
|
+
const project = this.resolveProject(input);
|
|
324
|
+
const changesRoot = path.join(project.knowledgePath, 'changes');
|
|
325
|
+
const results = markdownFiles(project.knowledgePath).filter(file => this.layout.isPathInside(changesRoot, file, { realpath: true })).map(file => {
|
|
326
|
+
const text = fs.readFileSync(file, 'utf8');
|
|
327
|
+
return {
|
|
328
|
+
entry_id: path.relative(project.knowledgePath, file).replace(/\\/g, '/'),
|
|
329
|
+
title: markdownTitle(text, path.basename(file, '.md')),
|
|
330
|
+
chunk_text: markdownExcerpt(text, [], 900),
|
|
331
|
+
updated_at: fs.statSync(file).mtime.toISOString(),
|
|
332
|
+
};
|
|
333
|
+
}).sort((left, right) => right.updated_at.localeCompare(left.updated_at)).slice(0, safeLimit(input.limit, 20, 100));
|
|
334
|
+
return { ok: true, projectId: project.projectId, projectSlug: project.projectSlug, backend: 'markdown', source: 'markdown', results };
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
async recordRequirement(input = {}) {
|
|
338
|
+
const record = await this.requirementRecorder.recordRequirement(input);
|
|
339
|
+
return {
|
|
340
|
+
ok: true,
|
|
341
|
+
projectId: record.projectId,
|
|
342
|
+
requirementId: record.id,
|
|
343
|
+
requirementHash: record.requirementHash,
|
|
344
|
+
recordedAt: record.ts,
|
|
345
|
+
branch: record.branch,
|
|
346
|
+
headAtRecord: record.headAtRecord,
|
|
347
|
+
turnId: record.turnId,
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async close() {
|
|
352
|
+
const database = this.database;
|
|
353
|
+
this.database = null;
|
|
354
|
+
this.embedder = null;
|
|
355
|
+
if (database) await database.close();
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
module.exports = { KnowledgeToolRuntime, gitRoot, markdownFiles, queryTokens };
|