@sanqianx/project-knowledge 4.6.1
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 +20 -0
- package/.claude-plugin/marketplace.json +17 -0
- package/CHANGELOG.md +780 -0
- package/LICENSE +201 -0
- package/NOTICE +5 -0
- package/README.md +266 -0
- package/_site/README.md +30 -0
- package/_site/_test/ai-profile-resolver-test.js +137 -0
- package/_site/_test/ai-profile-test.js +75 -0
- package/_site/_test/ai-vendor-presets-test.js +91 -0
- package/_site/_test/automation-queue-test.js +14 -0
- package/_site/_test/automation-ui-test.js +40 -0
- package/_site/_test/background-task-registry-test.js +43 -0
- package/_site/_test/baseline-schema-test.js +91 -0
- package/_site/_test/bin-cli-test.js +218 -0
- package/_site/_test/bridge-adapter-test.js +95 -0
- package/_site/_test/bridge-consumer-service-test.js +310 -0
- package/_site/_test/cdp-browser-failure-test.js +100 -0
- package/_site/_test/chat-claudecodeui-match-test.js +92 -0
- package/_site/_test/claude-executable-discovery-test.js +90 -0
- package/_site/_test/claude-session-lifecycle-sweep-test.js +252 -0
- package/_site/_test/claude-workbench-test.js +170 -0
- package/_site/_test/client-capture-restart-e2e-test.js +189 -0
- package/_site/_test/codex-conversation-projection-test.js +59 -0
- package/_site/_test/codex-notify-fanout-test.js +45 -0
- package/_site/_test/commit-boundary-freeze-test.js +147 -0
- package/_site/_test/commit-conversation-binding-test.js +99 -0
- package/_site/_test/commit-evidence-test.js +110 -0
- package/_site/_test/context-pack-test.js +198 -0
- package/_site/_test/conversation-api-test.js +119 -0
- package/_site/_test/conversation-store-test.js +136 -0
- package/_site/_test/cross-repo-isolation-e2e-test.js +187 -0
- package/_site/_test/data-dir-migration-test.js +77 -0
- package/_site/_test/desktop-browser-compat-test.js +28 -0
- package/_site/_test/desktop-hook-runtime-regression-test.js +204 -0
- package/_site/_test/embedding-config-test.js +32 -0
- package/_site/_test/explicit-commit-processor-test.js +37 -0
- package/_site/_test/fixtures/fake-browser-exit.js +10 -0
- package/_site/_test/fixtures/fake-browser-hang.js +10 -0
- package/_site/_test/fixtures/make-git-repos.js +89 -0
- package/_site/_test/folder-picker-output-test.js +19 -0
- package/_site/_test/full-integration-e2e-test.js +165 -0
- package/_site/_test/git-validation-test.js +81 -0
- package/_site/_test/hook-runtime-endpoint-test.js +88 -0
- package/_site/_test/hook-status-repair-api-test.js +152 -0
- package/_site/_test/hook-trigger-test.js +85 -0
- package/_site/_test/hook-worktree-test.js +42 -0
- package/_site/_test/import-preflight-api-test.js +233 -0
- package/_site/_test/import-ui-flow-test.js +171 -0
- package/_site/_test/index-builder-test.js +41 -0
- package/_site/_test/index-writer-concurrency-test.js +135 -0
- package/_site/_test/integration-adapters-test.js +243 -0
- package/_site/_test/integration-capture-setup-test.js +108 -0
- package/_site/_test/integration-surface-coverage-test.js +165 -0
- package/_site/_test/kb-framework-test.js +57 -0
- package/_site/_test/kbpath-follow-test.js +63 -0
- package/_site/_test/knowledge-db-test.js +144 -0
- package/_site/_test/knowledge-language-control-test.js +170 -0
- package/_site/_test/knowledge-maintenance-test.js +40 -0
- package/_site/_test/knowledge-migration-test.js +75 -0
- package/_site/_test/knowledge-promotion-recovery-test.js +260 -0
- package/_site/_test/knowledge-query-test.js +54 -0
- package/_site/_test/knowledge-retrieval-service-test.js +77 -0
- package/_site/_test/knowledge-scopes-test.js +37 -0
- package/_site/_test/knowledge-storage-location-test.js +28 -0
- package/_site/_test/knowledge-storage-startup-test.js +52 -0
- package/_site/_test/knowledge-store-logs-supervision-test.js +89 -0
- package/_site/_test/legacy-claude-session-alias-test.js +106 -0
- package/_site/_test/legacy-forward-compat-test.js +257 -0
- package/_site/_test/legacy-project-upgrade-e2e-test.js +363 -0
- package/_site/_test/legacy-routes-removed-test.js +47 -0
- package/_site/_test/legacy-workbench-exclusion-test.js +92 -0
- package/_site/_test/log-redaction-test.js +58 -0
- package/_site/_test/logging-api-test.js +89 -0
- package/_site/_test/logging-sse-no-gap-test.js +88 -0
- package/_site/_test/logging-ui-test.js +104 -0
- package/_site/_test/markdown-delta-overlay-test.js +85 -0
- package/_site/_test/markdown-maintenance-api-test.js +75 -0
- package/_site/_test/markdown-maintenance-test.js +11 -0
- package/_site/_test/mcp-server-test.js +149 -0
- package/_site/_test/model-context-windows-test.js +89 -0
- package/_site/_test/module-bridge-eventbridge-test.js +193 -0
- package/_site/_test/non-release-ci-test.js +34 -0
- package/_site/_test/offline-boundary-isolation-test.js +32 -0
- package/_site/_test/orphaned-claim-recovery-test.js +80 -0
- package/_site/_test/p0-data-migration-characterization-test.js +37 -0
- package/_site/_test/p0-e2e-gate-test.js +347 -0
- package/_site/_test/package-boundary-test.js +42 -0
- package/_site/_test/package-startup-test.js +56 -0
- package/_site/_test/packaged-ui-smoke-test.js +79 -0
- package/_site/_test/path-consistency-test.js +151 -0
- package/_site/_test/pending-sweep-test.js +9 -0
- package/_site/_test/post-commit-automation-test.js +87 -0
- package/_site/_test/pr-consumer-contract-test.js +55 -0
- package/_site/_test/project-control-panel-task14-test.js +63 -0
- package/_site/_test/project-delete-recovery-test.js +64 -0
- package/_site/_test/project-goal-editor-test.js +145 -0
- package/_site/_test/project-layout-v2-migration-test.js +127 -0
- package/_site/_test/project-lifecycle-transaction-test.js +106 -0
- package/_site/_test/project-remove-running-guard-test.js +79 -0
- package/_site/_test/project-store-test.js +90 -0
- package/_site/_test/prompt-settings-test.js +115 -0
- package/_site/_test/protected-architecture-gate-test.js +123 -0
- package/_site/_test/refactor-characterization-test.js +36 -0
- package/_site/_test/release-version-sync-test.js +98 -0
- package/_site/_test/requirement-binding-test.js +113 -0
- package/_site/_test/requirement-recorder-test.js +173 -0
- package/_site/_test/run-all-tests.js +156 -0
- package/_site/_test/runtime-endpoint-test.js +64 -0
- package/_site/_test/scanner-test.js +83 -0
- package/_site/_test/server-runtime-migration-safety-test.js +27 -0
- package/_site/_test/server-security-test.js +96 -0
- package/_site/_test/sessions-stream-test.js +92 -0
- package/_site/_test/shared-contracts-test.js +58 -0
- package/_site/_test/simple-import-test.js +97 -0
- package/_site/_test/startup-analysis-disabled-test.js +16 -0
- package/_site/_test/storage-foundation-test.js +78 -0
- package/_site/_test/structured-logger-test.js +101 -0
- package/_site/_test/task15-20-ui-flow-test.js +148 -0
- package/_site/_test/team-knowledge-store-sparse-checkout-test.js +196 -0
- package/_site/_test/team-knowledge-store-test.js +131 -0
- package/_site/_test/tracking-start-test.js +143 -0
- package/_site/_test/ui-i18n-toggle-test.js +114 -0
- package/_site/_test/ui-smoke-test.js +73 -0
- package/_site/_test/v4122-upgrade-data-contract-test.js +56 -0
- package/_site/_test/workbench-permission-test.js +118 -0
- package/_site/_test/workspace-ui-contract-test.js +43 -0
- package/_site/lib/ai-profile-resolver.js +78 -0
- package/_site/lib/ai-vendor-presets.js +83 -0
- package/_site/lib/ai-workspace.js +104 -0
- package/_site/lib/atomic-file.js +204 -0
- package/_site/lib/automation-config.js +140 -0
- package/_site/lib/bridge-adapter.js +256 -0
- package/_site/lib/bridge-consumer-service.js +402 -0
- package/_site/lib/claude-cli-runner.js +1599 -0
- package/_site/lib/commit-conversation-binder.js +246 -0
- package/_site/lib/commit-processing-ledger.js +27 -0
- package/_site/lib/commit-prompt.js +202 -0
- package/_site/lib/commit-reconciler.js +444 -0
- package/_site/lib/context-pack-builder.js +299 -0
- package/_site/lib/contracts.js +279 -0
- package/_site/lib/conversation-exclusions.js +204 -0
- package/_site/lib/conversation-query-service.js +161 -0
- package/_site/lib/conversation-store.js +385 -0
- package/_site/lib/data-dir.js +81 -0
- package/_site/lib/data-state-classifier.js +40 -0
- package/_site/lib/embedding-service.js +85 -0
- package/_site/lib/evidence-bundle.js +228 -0
- package/_site/lib/folder-picker-output.js +24 -0
- package/_site/lib/git-runner.js +62 -0
- package/_site/lib/github-team-store.js +1175 -0
- package/_site/lib/hook-manager.js +202 -0
- package/_site/lib/index-builder.js +157 -0
- package/_site/lib/index-service.js +173 -0
- package/_site/lib/integration-installer.js +907 -0
- package/_site/lib/kb-framework.js +191 -0
- package/_site/lib/kb-validator.js +124 -0
- package/_site/lib/knowledge-db.js +363 -0
- package/_site/lib/knowledge-promotion.js +435 -0
- package/_site/lib/knowledge-retrieval-service.js +401 -0
- package/_site/lib/knowledge-schema.js +121 -0
- package/_site/lib/knowledge-tool-runtime.js +359 -0
- package/_site/lib/legacy-data-manifest.js +26 -0
- package/_site/lib/llm-client.js +173 -0
- package/_site/lib/markdown-knowledge-indexer.js +230 -0
- package/_site/lib/migration-service.js +442 -0
- package/_site/lib/model-context-windows.js +82 -0
- package/_site/lib/module-bridge.js +496 -0
- package/_site/lib/post-commit-automation.js +141 -0
- package/_site/lib/project-lifecycle-service.js +511 -0
- package/_site/lib/project-registry-store.js +129 -0
- package/_site/lib/project-store.js +262 -0
- package/_site/lib/prompt-registry.js +148 -0
- package/_site/lib/requirement-adapters.js +10 -0
- package/_site/lib/requirement-binder.js +112 -0
- package/_site/lib/requirement-recorder.js +278 -0
- package/_site/lib/runtime-endpoint.js +160 -0
- package/_site/lib/scanner.js +298 -0
- package/_site/lib/server-app.js +1520 -0
- package/_site/lib/settings-store.js +113 -0
- package/_site/lib/storage-layout.js +162 -0
- package/_site/lib/structured-logger.js +574 -0
- package/_site/scripts/codex-notify-fanout.cjs +51 -0
- package/_site/scripts/folder-picker.ps1 +156 -0
- package/_site/scripts/hook-trigger.js +156 -0
- package/_site/scripts/sync-release-version.js +158 -0
- package/_site/server.js +12 -0
- package/_site/start.bat +26 -0
- package/_site/stop.bat +11 -0
- package/bin/project-knowledge-integrations.js +241 -0
- package/bin/project-knowledge-kb.js +65 -0
- package/bin/project-knowledge-mcp.js +195 -0
- package/bin/project-knowledge.js +439 -0
- package/claude-prompts.json +21 -0
- package/docs/README.zh-CN.md +175 -0
- package/docs/assets/architecture.svg +182 -0
- package/docs/assets/dashboard.png +0 -0
- package/docs/assets/draft-review.png +0 -0
- package/docs/assets/logo.svg +50 -0
- package/docs/assets/runs-drafts.png +0 -0
- package/docs/assets/settings.png +0 -0
- package/docs/project-registry-schema.md +33 -0
- package/docs/team-knowledge-mode-a-plan.md +73 -0
- package/docs/testing-strategy.md +33 -0
- package/package.json +100 -0
- package/plugins/project-knowledge/.claude-plugin/plugin.json +19 -0
- package/plugins/project-knowledge/.codex-plugin/plugin.json +38 -0
- package/plugins/project-knowledge/.mcp.json +14 -0
- package/plugins/project-knowledge/opencode/project-knowledge.md +3 -0
- package/plugins/project-knowledge/skills/project-knowledge/SKILL.md +28 -0
- package/plugins/project-knowledge/skills/project-knowledge/agents/openai.yaml +11 -0
- package/templates/change.md +30 -0
- package/templates/project-readme.md +52 -0
- package/ui/app.css +58 -0
- package/ui/app.js +607 -0
- package/ui/favicon.svg +38 -0
- package/ui/i18n.js +146 -0
- package/ui/index.html +162 -0
|
@@ -0,0 +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 };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// The complete v4.1.x runtime asset inventory. Both relocation from an
|
|
2
|
+
// installed package and layout-v2 migration consume this manifest.
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
const LEGACY_ASSETS = Object.freeze([
|
|
6
|
+
{ source: 'projects.json', target: 'projects.json', kind: 'file', authority: 'user' },
|
|
7
|
+
{ source: 'ai-profiles.json', target: 'ai-profiles.json', kind: 'file', authority: 'user' },
|
|
8
|
+
{ source: 'knowledge-store.json', target: 'knowledge-store.json', kind: 'file', authority: 'user' },
|
|
9
|
+
{ source: 'embedding-config.json', target: 'embedding-config.json', kind: 'file', authority: 'user' },
|
|
10
|
+
{ source: 'logging.json', target: 'logging.json', kind: 'file', authority: 'user' },
|
|
11
|
+
{ source: 'claude-prompts.json', target: 'claude-prompts.json', kind: 'file', authority: 'user' },
|
|
12
|
+
{ source: 'github-team.json', target: 'github-team.json', kind: 'file', authority: 'user' },
|
|
13
|
+
{ source: 'team-git-providers.json', target: 'team-git-providers.json', kind: 'file', authority: 'user' },
|
|
14
|
+
{ source: 'knowledge-scopes.json', target: 'knowledge-scopes.json', kind: 'file', authority: 'user' },
|
|
15
|
+
{ source: '.jobs-log.json', target: '.jobs-log.json', kind: 'file', authority: 'history' },
|
|
16
|
+
{ source: '.hook-trigger-errors.log', target: '.hook-trigger-errors.log', kind: 'file', authority: 'history' },
|
|
17
|
+
{ source: 'projects', target: 'projects', kind: 'dir', authority: 'user' },
|
|
18
|
+
{ source: 'logs', target: 'logs', kind: 'dir', authority: 'history' },
|
|
19
|
+
{ source: 'models', target: 'models', kind: 'dir', authority: 'cache-but-expensive' },
|
|
20
|
+
{ source: path.join('_site', '_ai'), target: '_ai', kind: 'dir', authority: 'history' },
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
function getLegacyAsset(source) { return LEGACY_ASSETS.find(asset => asset.source === source) || null; }
|
|
24
|
+
function assetPath(root, asset, side = 'source') { return path.join(root, asset[side] || asset.source); }
|
|
25
|
+
|
|
26
|
+
module.exports = { LEGACY_ASSETS, getLegacyAsset, assetPath };
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// _site/lib/llm-client.js
|
|
2
|
+
//
|
|
3
|
+
// Tiny Anthropic Messages API client. Zero npm deps; supports HTTPS and HTTP
|
|
4
|
+
// Anthropic-compatible endpoints. Connection settings are resolved from an AI
|
|
5
|
+
// profile first, then environment variables as fallback:
|
|
6
|
+
//
|
|
7
|
+
// profile.baseUrl or ANTHROPIC_BASE_URL
|
|
8
|
+
// profile.apiKey or ANTHROPIC_AUTH_TOKEN
|
|
9
|
+
// profile.mainModel or ANTHROPIC_MODEL
|
|
10
|
+
// profile.timeoutMs or API_TIMEOUT_MS
|
|
11
|
+
//
|
|
12
|
+
// This lets the local UI persist model settings in ai-profiles.json while still
|
|
13
|
+
// keeping env vars useful for scripts and temporary overrides.
|
|
14
|
+
|
|
15
|
+
const fs = require('fs');
|
|
16
|
+
const path = require('path');
|
|
17
|
+
const http = require('http');
|
|
18
|
+
const https = require('https');
|
|
19
|
+
const { URL } = require('url');
|
|
20
|
+
|
|
21
|
+
const DEFAULT_BASE = 'https://api.anthropic.com';
|
|
22
|
+
const DEFAULT_VERSION = '2023-06-01';
|
|
23
|
+
const DEFAULT_MODEL = 'claude-haiku-4-5';
|
|
24
|
+
const { getDataDir } = require('./data-dir');
|
|
25
|
+
const DATA_DIR = getDataDir();
|
|
26
|
+
const AI_PROFILES_PATH = path.join(DATA_DIR, 'ai-profiles.json');
|
|
27
|
+
|
|
28
|
+
function readProfilesFile() {
|
|
29
|
+
try {
|
|
30
|
+
const cfg = JSON.parse(fs.readFileSync(AI_PROFILES_PATH, 'utf-8'));
|
|
31
|
+
return Array.isArray(cfg.profiles) ? cfg.profiles : [];
|
|
32
|
+
} catch {
|
|
33
|
+
return [];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function findProfile(profileId) {
|
|
38
|
+
if (!profileId) return null;
|
|
39
|
+
return readProfilesFile().find(profile => profile && profile.id === profileId) || null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function readConfig(options = {}) {
|
|
43
|
+
const profileId = typeof options === 'string' ? options : options.profileId;
|
|
44
|
+
const inlineProfile = options && typeof options === 'object' && options.profile && typeof options.profile === 'object'
|
|
45
|
+
? options.profile
|
|
46
|
+
: null;
|
|
47
|
+
const profile = inlineProfile || findProfile(profileId) || {};
|
|
48
|
+
const timeoutMs = Number(profile.timeoutMs || process.env.API_TIMEOUT_MS || 60_000);
|
|
49
|
+
return {
|
|
50
|
+
profileId: profile.id || profileId || null,
|
|
51
|
+
baseUrl: profile.baseUrl || profile.apiBaseUrl || profile.anthropicBaseUrl || process.env.ANTHROPIC_BASE_URL || DEFAULT_BASE,
|
|
52
|
+
apiKey: profile.apiKey || profile.authToken || profile.anthropicAuthToken || process.env.ANTHROPIC_AUTH_TOKEN || '',
|
|
53
|
+
model: profile.mainModel || profile.model || process.env.ANTHROPIC_MODEL || DEFAULT_MODEL,
|
|
54
|
+
version: DEFAULT_VERSION,
|
|
55
|
+
timeoutMs: Number.isFinite(timeoutMs) && timeoutMs > 0 ? timeoutMs : 60_000,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function buildRequestPath(base, p) {
|
|
60
|
+
if (!p.startsWith('/')) return p;
|
|
61
|
+
const basePath = base.pathname.endsWith('/') ? base.pathname.slice(0, -1) : base.pathname;
|
|
62
|
+
return basePath + p;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function postJson({ baseUrl, path: p, body, apiKey, version, timeoutMs = 60_000 }) {
|
|
66
|
+
return new Promise((resolve, reject) => {
|
|
67
|
+
try {
|
|
68
|
+
const base = new URL(baseUrl);
|
|
69
|
+
const transport = base.protocol === 'http:' ? http : https;
|
|
70
|
+
const data = JSON.stringify(body);
|
|
71
|
+
const req = transport.request({
|
|
72
|
+
host: base.hostname,
|
|
73
|
+
port: base.port || (base.protocol === 'http:' ? 80 : 443),
|
|
74
|
+
path: buildRequestPath(base, p),
|
|
75
|
+
method: 'POST',
|
|
76
|
+
headers: {
|
|
77
|
+
'Content-Type': 'application/json',
|
|
78
|
+
'x-api-key': apiKey,
|
|
79
|
+
'anthropic-version': version,
|
|
80
|
+
'Content-Length': Buffer.byteLength(data),
|
|
81
|
+
},
|
|
82
|
+
timeout: timeoutMs,
|
|
83
|
+
}, res => {
|
|
84
|
+
const chunks = [];
|
|
85
|
+
res.on('data', c => chunks.push(c));
|
|
86
|
+
res.on('end', () => {
|
|
87
|
+
const text = Buffer.concat(chunks).toString('utf-8');
|
|
88
|
+
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
89
|
+
return reject(new Error(`HTTP ${res.statusCode}: ${text.slice(0, 500)}`));
|
|
90
|
+
}
|
|
91
|
+
try { resolve(JSON.parse(text)); }
|
|
92
|
+
catch (e) { reject(new Error(`bad JSON: ${e.message}; body head: ${text.slice(0, 200)}`)); }
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
req.on('error', reject);
|
|
96
|
+
req.on('timeout', () => req.destroy(new Error(`timeout after ${timeoutMs}ms`)));
|
|
97
|
+
req.write(data);
|
|
98
|
+
req.end();
|
|
99
|
+
} catch (e) {
|
|
100
|
+
reject(new Error(`bad baseUrl: ${baseUrl}: ${e.message}`));
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function completeText({ system, user, maxTokens = 512, model, profileId, profile }) {
|
|
106
|
+
const cfg = readConfig({ profileId, profile });
|
|
107
|
+
if (!cfg.apiKey) {
|
|
108
|
+
const id = profileId ? ` for profile ${profileId}` : '';
|
|
109
|
+
throw new Error(`API key not set${id}`);
|
|
110
|
+
}
|
|
111
|
+
const body = {
|
|
112
|
+
model: model || cfg.model,
|
|
113
|
+
max_tokens: maxTokens,
|
|
114
|
+
messages: [{ role: 'user', content: user }],
|
|
115
|
+
};
|
|
116
|
+
if (system) body.system = system;
|
|
117
|
+
|
|
118
|
+
const raw = await postJson({
|
|
119
|
+
baseUrl: cfg.baseUrl,
|
|
120
|
+
path: '/v1/messages',
|
|
121
|
+
body,
|
|
122
|
+
apiKey: cfg.apiKey,
|
|
123
|
+
version: cfg.version,
|
|
124
|
+
timeoutMs: cfg.timeoutMs,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const text = (raw.content || [])
|
|
128
|
+
.filter(block => block && block.type === 'text' && typeof block.text === 'string')
|
|
129
|
+
.map(block => block.text)
|
|
130
|
+
.join('\n')
|
|
131
|
+
.trim();
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
text,
|
|
135
|
+
raw,
|
|
136
|
+
usage: raw.usage || null,
|
|
137
|
+
model: raw.model || body.model,
|
|
138
|
+
profileId: cfg.profileId,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function completeJson({ system, user, schema, maxTokens = 2048, model, profileId, profile }) {
|
|
143
|
+
const schemaHint = schema ? `\n\nThe output MUST be a JSON object matching this schema:\n${schema}\n` : '';
|
|
144
|
+
const finalUser = `${user}${schemaHint}\n\nRespond with ONLY the JSON object, no prose, no markdown fences.`;
|
|
145
|
+
const result = await completeText({ system, user: finalUser, maxTokens, model, profileId, profile });
|
|
146
|
+
const text = result.text;
|
|
147
|
+
|
|
148
|
+
let parsed = null;
|
|
149
|
+
let parseError = null;
|
|
150
|
+
const candidates = [
|
|
151
|
+
text,
|
|
152
|
+
text.replace(/^```(?:json)?\s*/i, '').replace(/```\s*$/, ''),
|
|
153
|
+
];
|
|
154
|
+
for (const candidate of candidates) {
|
|
155
|
+
if (!candidate) continue;
|
|
156
|
+
try {
|
|
157
|
+
parsed = JSON.parse(candidate);
|
|
158
|
+
parseError = null;
|
|
159
|
+
break;
|
|
160
|
+
} catch (e) {
|
|
161
|
+
parseError = e;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return { ...result, parsed, parseError };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
module.exports = {
|
|
169
|
+
completeJson,
|
|
170
|
+
completeText,
|
|
171
|
+
readConfig,
|
|
172
|
+
findProfile,
|
|
173
|
+
};
|