toolnet-memory 0.2.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/.env.example +28 -0
- package/README.md +147 -0
- package/bin/toolnet-memory +415 -0
- package/dist/packages/cli/commands.js +1 -0
- package/dist/packages/cli/index.js +1 -0
- package/dist/packages/sdk/client.js +78 -0
- package/dist/packages/sdk/index.js +1 -0
- package/dist/packages/sdk/types.js +1 -0
- package/dist/src/api/index.js +1 -0
- package/dist/src/api/routes/health.js +1 -0
- package/dist/src/api/routes/index.js +1 -0
- package/dist/src/api/routes/memory.js +1 -0
- package/dist/src/api/routes/projects.js +1 -0
- package/dist/src/api/routes/search.js +1 -0
- package/dist/src/api/server.js +1 -0
- package/dist/src/capture/activity-capture.js +21 -0
- package/dist/src/capture/command-capture.js +12 -0
- package/dist/src/capture/error-capture.js +14 -0
- package/dist/src/capture/event-deduplicator.js +28 -0
- package/dist/src/capture/event-queue.js +20 -0
- package/dist/src/capture/file-capture.js +23 -0
- package/dist/src/capture/index.js +8 -0
- package/dist/src/capture/session-capture.js +18 -0
- package/dist/src/capture/tool-capture.js +13 -0
- package/dist/src/code-intelligence/analysis/analysis-engine.js +26 -0
- package/dist/src/code-intelligence/analysis/dead-code-analyzer.js +127 -0
- package/dist/src/code-intelligence/analysis/dependency-analyzer.js +66 -0
- package/dist/src/code-intelligence/analysis/index.js +4 -0
- package/dist/src/code-intelligence/analysis/types.js +1 -0
- package/dist/src/code-intelligence/architecture/architecture-engine.js +48 -0
- package/dist/src/code-intelligence/architecture/clusters.js +322 -0
- package/dist/src/code-intelligence/architecture/entry-points.js +150 -0
- package/dist/src/code-intelligence/architecture/hotspots.js +136 -0
- package/dist/src/code-intelligence/architecture/index.js +6 -0
- package/dist/src/code-intelligence/architecture/layers.js +197 -0
- package/dist/src/code-intelligence/architecture/types.js +1 -0
- package/dist/src/code-intelligence/chunks/index.js +2 -0
- package/dist/src/code-intelligence/chunks/smart-chunker.js +125 -0
- package/dist/src/code-intelligence/chunks/types.js +1 -0
- package/dist/src/code-intelligence/git/git-diff.js +109 -0
- package/dist/src/code-intelligence/git/index.js +1 -0
- package/dist/src/code-intelligence/graph/architecture.js +26 -0
- package/dist/src/code-intelligence/graph/graph-builder.js +213 -0
- package/dist/src/code-intelligence/graph/graph-store.js +57 -0
- package/dist/src/code-intelligence/graph/index.js +1 -0
- package/dist/src/code-intelligence/graph/relationships.js +1 -0
- package/dist/src/code-intelligence/graph/trace.js +65 -0
- package/dist/src/code-intelligence/impact/blast-radius.js +160 -0
- package/dist/src/code-intelligence/impact/change-mapper.js +69 -0
- package/dist/src/code-intelligence/impact/edit-tool-detector.js +92 -0
- package/dist/src/code-intelligence/impact/impact-analyzer.js +60 -0
- package/dist/src/code-intelligence/impact/impact-guard.js +33 -0
- package/dist/src/code-intelligence/impact/index.js +5 -0
- package/dist/src/code-intelligence/incremental/file-hash.js +8 -0
- package/dist/src/code-intelligence/incremental/incremental-indexer.js +106 -0
- package/dist/src/code-intelligence/incremental/manifest-builder.js +19 -0
- package/dist/src/code-intelligence/incremental/manifest.js +31 -0
- package/dist/src/code-intelligence/index.js +23 -0
- package/dist/src/code-intelligence/indexer/file-watcher.js +1 -0
- package/dist/src/code-intelligence/indexer/incremental-indexer.js +1 -0
- package/dist/src/code-intelligence/indexer/index.js +1 -0
- package/dist/src/code-intelligence/indexer/repository-indexer.js +31 -0
- package/dist/src/code-intelligence/indexer/repository-scanner.js +50 -0
- package/dist/src/code-intelligence/parsers/index.js +1 -0
- package/dist/src/code-intelligence/parsers/tree-sitter/index.js +1 -0
- package/dist/src/code-intelligence/parsers/tree-sitter/languages.js +1 -0
- package/dist/src/code-intelligence/parsers/tree-sitter/parser.js +1 -0
- package/dist/src/code-intelligence/parsers/typescript-parser.js +190 -0
- package/dist/src/code-intelligence/query/graph-query-engine.js +275 -0
- package/dist/src/code-intelligence/query/index.js +2 -0
- package/dist/src/code-intelligence/query/types.js +1 -0
- package/dist/src/code-intelligence/resolution/index.js +2 -0
- package/dist/src/code-intelligence/resolution/types.js +1 -0
- package/dist/src/code-intelligence/resolution/typescript-resolver.js +316 -0
- package/dist/src/code-intelligence/rich/index.js +1 -0
- package/dist/src/code-intelligence/rich/rich-graph-enricher.js +463 -0
- package/dist/src/code-intelligence/semantic/index.js +2 -0
- package/dist/src/code-intelligence/semantic/semantic-code-engine.js +213 -0
- package/dist/src/code-intelligence/semantic/source-priority.js +22 -0
- package/dist/src/code-intelligence/symbols/index.js +1 -0
- package/dist/src/code-intelligence/symbols/reference-resolver.js +21 -0
- package/dist/src/code-intelligence/symbols/symbol-extractor.js +1 -0
- package/dist/src/code-intelligence/test-analysis.js +72 -0
- package/dist/src/code-intelligence/test-architecture.js +67 -0
- package/dist/src/code-intelligence/test-impact.js +47 -0
- package/dist/src/code-intelligence/test-incremental.js +24 -0
- package/dist/src/code-intelligence/test-index.js +39 -0
- package/dist/src/code-intelligence/test-resolution.js +54 -0
- package/dist/src/code-intelligence/test-rich-graph.js +48 -0
- package/dist/src/code-intelligence/test-semantic.js +57 -0
- package/dist/src/code-intelligence/test-visualization.js +36 -0
- package/dist/src/code-intelligence/types.js +1 -0
- package/dist/src/code-intelligence/visualization/index.js +2 -0
- package/dist/src/code-intelligence/visualization/types.js +1 -0
- package/dist/src/code-intelligence/visualization/visualization-builder.js +104 -0
- package/dist/src/core/config.js +66 -0
- package/dist/src/core/global-env.js +70 -0
- package/dist/src/core/importance.js +30 -0
- package/dist/src/core/index.js +5 -0
- package/dist/src/core/memory-engine.js +183 -0
- package/dist/src/core/project-manager.js +243 -0
- package/dist/src/core/types.js +1 -0
- package/dist/src/embeddings/batch-embedder.js +1 -0
- package/dist/src/embeddings/cache.js +1 -0
- package/dist/src/embeddings/embedder.js +51 -0
- package/dist/src/embeddings/index.js +4 -0
- package/dist/src/embeddings/local.js +37 -0
- package/dist/src/embeddings/provider.js +1 -0
- package/dist/src/embeddings/remote.js +76 -0
- package/dist/src/embeddings/test-connection.js +16 -0
- package/dist/src/hooks/after-edit.js +3 -0
- package/dist/src/hooks/after-tool.js +3 -0
- package/dist/src/hooks/auto-context.js +68 -0
- package/dist/src/hooks/auto-impact-guard.js +94 -0
- package/dist/src/hooks/auto-retrieval.js +27 -0
- package/dist/src/hooks/before-agent.js +18 -0
- package/dist/src/hooks/before-compact.js +3 -0
- package/dist/src/hooks/before-edit.js +20 -0
- package/dist/src/hooks/index.js +11 -0
- package/dist/src/hooks/runtime.js +110 -0
- package/dist/src/hooks/session-end.js +3 -0
- package/dist/src/hooks/session-start.js +3 -0
- package/dist/src/index.js +22 -0
- package/dist/src/mcp/bootstrap.js +68 -0
- package/dist/src/mcp/context.js +1 -0
- package/dist/src/mcp/index.js +3 -0
- package/dist/src/mcp/server.js +45 -0
- package/dist/src/mcp/tools/analyze-impact.js +41 -0
- package/dist/src/mcp/tools/dead-code.js +55 -0
- package/dist/src/mcp/tools/find-callers.js +24 -0
- package/dist/src/mcp/tools/find-dependencies.js +57 -0
- package/dist/src/mcp/tools/find-symbol.js +7 -0
- package/dist/src/mcp/tools/get-architecture.js +11 -0
- package/dist/src/mcp/tools/graph-dependents.js +24 -0
- package/dist/src/mcp/tools/graph-neighborhood.js +58 -0
- package/dist/src/mcp/tools/graph-path.js +43 -0
- package/dist/src/mcp/tools/graph-query-utils.js +32 -0
- package/dist/src/mcp/tools/impact-guard.js +34 -0
- package/dist/src/mcp/tools/index-repository.js +1 -0
- package/dist/src/mcp/tools/index.js +20 -0
- package/dist/src/mcp/tools/memory-decisions.js +1 -0
- package/dist/src/mcp/tools/memory-forget.js +14 -0
- package/dist/src/mcp/tools/memory-recent.js +1 -0
- package/dist/src/mcp/tools/memory-remember.js +33 -0
- package/dist/src/mcp/tools/memory-rules.js +1 -0
- package/dist/src/mcp/tools/memory-search.js +18 -0
- package/dist/src/mcp/tools/memory-todos.js +1 -0
- package/dist/src/mcp/tools/project-context.js +25 -0
- package/dist/src/mcp/tools/search-code.js +21 -0
- package/dist/src/mcp/tools/semantic-code-search.js +33 -0
- package/dist/src/mcp/tools/snapshot-create.js +13 -0
- package/dist/src/mcp/tools/snapshot-list.js +7 -0
- package/dist/src/mcp/tools/snapshot-restore.js +13 -0
- package/dist/src/mcp/tools/trace-calls.js +37 -0
- package/dist/src/memory/activity.js +5 -0
- package/dist/src/memory/conflict-detector.js +105 -0
- package/dist/src/memory/consolidator.js +104 -0
- package/dist/src/memory/decay.js +59 -0
- package/dist/src/memory/decisions.js +3 -0
- package/dist/src/memory/deduplicate.js +25 -0
- package/dist/src/memory/index.js +11 -0
- package/dist/src/memory/maintenance.js +16 -0
- package/dist/src/memory/remember.js +3 -0
- package/dist/src/memory/rules.js +3 -0
- package/dist/src/memory/search.js +3 -0
- package/dist/src/memory/summaries.js +1 -0
- package/dist/src/memory/todos.js +3 -0
- package/dist/src/processor/decision-extractor.js +13 -0
- package/dist/src/processor/importance-scorer.js +22 -0
- package/dist/src/processor/index.js +7 -0
- package/dist/src/processor/memory-extractor.js +59 -0
- package/dist/src/processor/memory-processor.js +105 -0
- package/dist/src/processor/rule-extractor.js +13 -0
- package/dist/src/processor/summarizer.js +71 -0
- package/dist/src/processor/todo-extractor.js +12 -0
- package/dist/src/production/config-check.js +34 -0
- package/dist/src/production/doctor.js +58 -0
- package/dist/src/production/full-index.js +114 -0
- package/dist/src/production/health.js +58 -0
- package/dist/src/production/index-pipeline.js +151 -0
- package/dist/src/production/index.js +4 -0
- package/dist/src/production/project-lock.js +95 -0
- package/dist/src/production/project-status.js +16 -0
- package/dist/src/production/recovery.js +15 -0
- package/dist/src/production/snapshot-cli.js +58 -0
- package/dist/src/project-manual/cli.js +77 -0
- package/dist/src/project-manual/index.js +2 -0
- package/dist/src/project-manual/manager.js +147 -0
- package/dist/src/project-manual/types.js +1 -0
- package/dist/src/retrieval/bm25/index.js +1 -0
- package/dist/src/retrieval/bm25/search.js +1 -0
- package/dist/src/retrieval/bm25.js +30 -0
- package/dist/src/retrieval/context-budget.js +1 -0
- package/dist/src/retrieval/context-builder.js +22 -0
- package/dist/src/retrieval/graph/index.js +1 -0
- package/dist/src/retrieval/graph/search.js +1 -0
- package/dist/src/retrieval/hybrid/hybrid-search.js +1 -0
- package/dist/src/retrieval/hybrid/index.js +1 -0
- package/dist/src/retrieval/hybrid-search.js +68 -0
- package/dist/src/retrieval/importance.js +16 -0
- package/dist/src/retrieval/index.js +12 -0
- package/dist/src/retrieval/query-analyzer.js +32 -0
- package/dist/src/retrieval/query-classifier.js +1 -0
- package/dist/src/retrieval/query-expansion.js +1 -0
- package/dist/src/retrieval/recency.js +12 -0
- package/dist/src/retrieval/reranker/index.js +1 -0
- package/dist/src/retrieval/reranker/reranker.js +1 -0
- package/dist/src/retrieval/retrieval-engine.js +17 -0
- package/dist/src/retrieval/tokenizer.js +9 -0
- package/dist/src/retrieval/type-priority.js +10 -0
- package/dist/src/retrieval/types.js +1 -0
- package/dist/src/retrieval/vector/index.js +4 -0
- package/dist/src/retrieval/vector/metadata-filter.js +1 -0
- package/dist/src/retrieval/vector/persistence.js +67 -0
- package/dist/src/retrieval/vector/search.js +12 -0
- package/dist/src/retrieval/vector/test-persistence.js +36 -0
- package/dist/src/retrieval/vector/upsert.js +40 -0
- package/dist/src/retrieval/vector/vector-store.js +89 -0
- package/dist/src/retrieval/vector-hybrid-engine.js +91 -0
- package/dist/src/runtime/bootstrap.js +28 -0
- package/dist/src/runtime/graceful.js +17 -0
- package/dist/src/runtime/index.js +3 -0
- package/dist/src/runtime/toolnet-memory-runtime.js +156 -0
- package/dist/src/security/encryption.js +1 -0
- package/dist/src/security/file-filter.js +30 -0
- package/dist/src/security/index.js +3 -0
- package/dist/src/security/sanitizer.js +49 -0
- package/dist/src/security/secret-scanner.js +56 -0
- package/dist/src/session/agy/adapter.js +159 -0
- package/dist/src/session/agy/cli.js +87 -0
- package/dist/src/session/agy/hook-installer.js +62 -0
- package/dist/src/session/agy/hook.js +167 -0
- package/dist/src/session/agy/index.js +5 -0
- package/dist/src/session/agy/project-resolver.js +27 -0
- package/dist/src/session/agy/recovery.js +112 -0
- package/dist/src/session/agy/transcript.js +191 -0
- package/dist/src/session/codex/adapter.js +138 -0
- package/dist/src/session/codex/cli.js +236 -0
- package/dist/src/session/codex/context-hook-installer.js +70 -0
- package/dist/src/session/codex/context-hook.js +69 -0
- package/dist/src/session/codex/discovery.js +138 -0
- package/dist/src/session/codex/index.js +6 -0
- package/dist/src/session/codex/notify-installer.js +135 -0
- package/dist/src/session/codex/project-resolver.js +25 -0
- package/dist/src/session/codex/recovery.js +27 -0
- package/dist/src/session/codex/rollout.js +186 -0
- package/dist/src/session/core.js +239 -0
- package/dist/src/session/identity.js +57 -0
- package/dist/src/session/index.js +9 -0
- package/dist/src/session/learner/cli.js +62 -0
- package/dist/src/session/learner/extractor.js +461 -0
- package/dist/src/session/learner/index.js +4 -0
- package/dist/src/session/learner/journal.js +159 -0
- package/dist/src/session/learner/learner.js +124 -0
- package/dist/src/session/learner/types.js +1 -0
- package/dist/src/session/opencode/adapter.js +553 -0
- package/dist/src/session/opencode/cli.js +121 -0
- package/dist/src/session/opencode/index.js +2 -0
- package/dist/src/session/opencode/plugin-installer.js +230 -0
- package/dist/src/session/store.js +242 -0
- package/dist/src/session/types.js +1 -0
- package/dist/src/session/utils.js +47 -0
- package/dist/src/session/wal.js +303 -0
- package/dist/src/snapshot/index.js +2 -0
- package/dist/src/snapshot/manager.js +130 -0
- package/dist/src/snapshot/test-snapshot.js +28 -0
- package/dist/src/snapshot/types.js +1 -0
- package/dist/src/storage/architecture-store.js +37 -0
- package/dist/src/storage/cleanup-health.js +20 -0
- package/dist/src/storage/code-analysis-store.js +35 -0
- package/dist/src/storage/code-chunk-store.js +25 -0
- package/dist/src/storage/code-graph-store.js +24 -0
- package/dist/src/storage/code-manifest-store.js +24 -0
- package/dist/src/storage/code-vector-store.js +25 -0
- package/dist/src/storage/github/client.js +1 -0
- package/dist/src/storage/github/index.js +1 -0
- package/dist/src/storage/google-drive/client.js +1 -0
- package/dist/src/storage/google-drive/index.js +1 -0
- package/dist/src/storage/huggingface/client.js +116 -0
- package/dist/src/storage/huggingface/index.js +1 -0
- package/dist/src/storage/index.js +16 -0
- package/dist/src/storage/list-bucket-raw.js +14 -0
- package/dist/src/storage/list-bucket-tree.js +38 -0
- package/dist/src/storage/local/client.js +99 -0
- package/dist/src/storage/local/index.js +1 -0
- package/dist/src/storage/memory-store.js +30 -0
- package/dist/src/storage/project/cleanup-layout-v1.js +72 -0
- package/dist/src/storage/project/folder.js +15 -0
- package/dist/src/storage/project/index.js +3 -0
- package/dist/src/storage/project/layout.js +74 -0
- package/dist/src/storage/project/migrate-layout-v2.js +101 -0
- package/dist/src/storage/project/migrate.js +73 -0
- package/dist/src/storage/project/scoped-provider.js +165 -0
- package/dist/src/storage/provider.js +26 -0
- package/dist/src/storage/retrying-provider.js +23 -0
- package/dist/src/storage/test-connection.js +32 -0
- package/dist/src/storage/type-resolution-store.js +26 -0
- package/dist/src/storage/types.js +1 -0
- package/dist/src/storage/vector-store.js +30 -0
- package/dist/src/storage/visualization-store.js +25 -0
- package/dist/src/sync/compressor.js +1 -0
- package/dist/src/sync/conflict-resolver.js +1 -0
- package/dist/src/sync/downloader.js +1 -0
- package/dist/src/sync/index.js +1 -0
- package/dist/src/sync/sync-engine.js +1 -0
- package/dist/src/sync/upload-queue.js +1 -0
- package/dist/src/utils/retry.js +29 -0
- package/dist/src/visualization/server.js +270 -0
- package/dist/src/work-continuity/brief-cache.js +107 -0
- package/dist/src/work-continuity/brief.js +349 -0
- package/dist/src/work-continuity/cli.js +96 -0
- package/dist/src/work-continuity/context-cli.js +106 -0
- package/dist/src/work-continuity/context-runtime-cli.js +88 -0
- package/dist/src/work-continuity/extractor.js +339 -0
- package/dist/src/work-continuity/handoff.js +185 -0
- package/dist/src/work-continuity/index.js +14 -0
- package/dist/src/work-continuity/injection.js +84 -0
- package/dist/src/work-continuity/journal.js +49 -0
- package/dist/src/work-continuity/learner.js +127 -0
- package/dist/src/work-continuity/reducer.js +316 -0
- package/dist/src/work-continuity/semantic-extractor.js +360 -0
- package/dist/src/work-continuity/semantic-journal.js +55 -0
- package/dist/src/work-continuity/semantic-learner.js +125 -0
- package/dist/src/work-continuity/semantic-reducer.js +268 -0
- package/dist/src/work-continuity/semantic-types.js +1 -0
- package/dist/src/work-continuity/types.js +1 -0
- package/dist/visualization/public/index.html +2896 -0
- package/dist/visualization/public/vendor/3d-force-graph.min.js +5 -0
- package/package.json +69 -0
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
import { createHash, } from "node:crypto";
|
|
2
|
+
import { dirname, relative, resolve, } from "node:path";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
function normalize(value) {
|
|
5
|
+
return value
|
|
6
|
+
.replaceAll("\\", "/")
|
|
7
|
+
.replace(/^\.\//, "");
|
|
8
|
+
}
|
|
9
|
+
function id(...parts) {
|
|
10
|
+
return createHash("sha256")
|
|
11
|
+
.update(parts.join(":"))
|
|
12
|
+
.digest("hex")
|
|
13
|
+
.slice(0, 24);
|
|
14
|
+
}
|
|
15
|
+
function lineOf(source, node) {
|
|
16
|
+
return source
|
|
17
|
+
.getLineAndCharacterOfPosition(node.getStart(source)).line + 1;
|
|
18
|
+
}
|
|
19
|
+
const ROUTE_METHODS = new Set([
|
|
20
|
+
"get",
|
|
21
|
+
"post",
|
|
22
|
+
"put",
|
|
23
|
+
"patch",
|
|
24
|
+
"delete",
|
|
25
|
+
"options",
|
|
26
|
+
"head",
|
|
27
|
+
]);
|
|
28
|
+
const ASSIGNMENTS = new Set([
|
|
29
|
+
ts.SyntaxKind.EqualsToken,
|
|
30
|
+
ts.SyntaxKind.PlusEqualsToken,
|
|
31
|
+
ts.SyntaxKind.MinusEqualsToken,
|
|
32
|
+
ts.SyntaxKind.AsteriskEqualsToken,
|
|
33
|
+
ts.SyntaxKind.SlashEqualsToken,
|
|
34
|
+
ts.SyntaxKind.PercentEqualsToken,
|
|
35
|
+
ts.SyntaxKind.QuestionQuestionEqualsToken,
|
|
36
|
+
ts.SyntaxKind.AmpersandAmpersandEqualsToken,
|
|
37
|
+
ts.SyntaxKind.BarBarEqualsToken,
|
|
38
|
+
]);
|
|
39
|
+
export class RichGraphEnricher {
|
|
40
|
+
graph;
|
|
41
|
+
constructor(graph) {
|
|
42
|
+
this.graph = graph;
|
|
43
|
+
}
|
|
44
|
+
enrich(projectId, rootPath, resolution) {
|
|
45
|
+
const stats = {
|
|
46
|
+
routes: 0,
|
|
47
|
+
tests: 0,
|
|
48
|
+
usesType: 0,
|
|
49
|
+
writes: 0,
|
|
50
|
+
callReferences: 0,
|
|
51
|
+
properties: 0,
|
|
52
|
+
};
|
|
53
|
+
const config = this.loadConfig(rootPath);
|
|
54
|
+
const program = ts.createProgram({
|
|
55
|
+
rootNames: config.fileNames,
|
|
56
|
+
options: config.options,
|
|
57
|
+
});
|
|
58
|
+
const checker = program.getTypeChecker();
|
|
59
|
+
for (const source of program.getSourceFiles()) {
|
|
60
|
+
if (source.isDeclarationFile) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const filePath = normalize(relative(rootPath, resolve(source.fileName)));
|
|
64
|
+
if (filePath.startsWith("../") ||
|
|
65
|
+
filePath.includes("node_modules/")) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const fileNode = this.fileNode(projectId, filePath);
|
|
69
|
+
if (!fileNode) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const visit = (node) => {
|
|
73
|
+
if (ts.isCallExpression(node)) {
|
|
74
|
+
if (this.addRoute(projectId, rootPath, source, fileNode, node, checker)) {
|
|
75
|
+
stats.routes++;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (ts.isTypeReferenceNode(node)) {
|
|
79
|
+
if (this.addTypeUse(projectId, rootPath, source, node, checker)) {
|
|
80
|
+
stats.usesType++;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (ts.isBinaryExpression(node) &&
|
|
84
|
+
ASSIGNMENTS.has(node.operatorToken.kind)) {
|
|
85
|
+
const result = this.addWrite(projectId, rootPath, source, node.left, checker);
|
|
86
|
+
if (result.added) {
|
|
87
|
+
stats.writes++;
|
|
88
|
+
if (result.propertyCreated) {
|
|
89
|
+
stats.properties++;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if ((ts.isPrefixUnaryExpression(node) ||
|
|
94
|
+
ts.isPostfixUnaryExpression(node)) &&
|
|
95
|
+
(node.operator ===
|
|
96
|
+
ts.SyntaxKind.PlusPlusToken ||
|
|
97
|
+
node.operator ===
|
|
98
|
+
ts.SyntaxKind.MinusMinusToken)) {
|
|
99
|
+
const result = this.addWrite(projectId, rootPath, source, node.operand, checker);
|
|
100
|
+
if (result.added) {
|
|
101
|
+
stats.writes++;
|
|
102
|
+
if (result.propertyCreated) {
|
|
103
|
+
stats.properties++;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
ts.forEachChild(node, visit);
|
|
108
|
+
};
|
|
109
|
+
visit(source);
|
|
110
|
+
}
|
|
111
|
+
stats.tests +=
|
|
112
|
+
this.addTestEdges(projectId);
|
|
113
|
+
if (resolution) {
|
|
114
|
+
stats.callReferences +=
|
|
115
|
+
this.addResolvedCalls(projectId, resolution);
|
|
116
|
+
}
|
|
117
|
+
return stats;
|
|
118
|
+
}
|
|
119
|
+
addRoute(projectId, rootPath, source, fileNode, node, checker) {
|
|
120
|
+
if (!ts.isPropertyAccessExpression(node.expression)) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
const method = node.expression
|
|
124
|
+
.name.text
|
|
125
|
+
.toLowerCase();
|
|
126
|
+
if (!ROUTE_METHODS.has(method)) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
const first = node.arguments[0];
|
|
130
|
+
if (!first ||
|
|
131
|
+
!(ts.isStringLiteral(first) ||
|
|
132
|
+
ts.isNoSubstitutionTemplateLiteral(first))) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
const path = first.text;
|
|
136
|
+
if (!path.startsWith("/")) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
const sourceLine = lineOf(source, node);
|
|
140
|
+
const routeId = id(projectId, source.fileName, sourceLine, method, path);
|
|
141
|
+
if (!this.graph.getSymbol(routeId)) {
|
|
142
|
+
this.graph.addSymbol({
|
|
143
|
+
id: routeId,
|
|
144
|
+
projectId,
|
|
145
|
+
name: `${method.toUpperCase()} ${path}`,
|
|
146
|
+
qualifiedName: `${method.toUpperCase()} ${path}`,
|
|
147
|
+
type: "route",
|
|
148
|
+
filePath: fileNode.filePath,
|
|
149
|
+
startLine: sourceLine,
|
|
150
|
+
endLine: sourceLine,
|
|
151
|
+
});
|
|
152
|
+
this.graph.addEdge(this.edge(projectId, fileNode.id, "DEFINES", routeId));
|
|
153
|
+
}
|
|
154
|
+
const handler = node.arguments.at(-1);
|
|
155
|
+
if (handler) {
|
|
156
|
+
const target = this.resolveNodeSymbol(projectId, rootPath, handler, checker);
|
|
157
|
+
if (target) {
|
|
158
|
+
this.graph.addEdge(this.edge(projectId, routeId, "ROUTE", target.id, {
|
|
159
|
+
method: method.toUpperCase(),
|
|
160
|
+
path,
|
|
161
|
+
}));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
addTypeUse(projectId, rootPath, source, node, checker) {
|
|
167
|
+
const target = this.resolveNodeSymbol(projectId, rootPath, node.typeName, checker);
|
|
168
|
+
if (!target) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
const sourceSymbol = this.containingSymbol(projectId, normalize(relative(rootPath, resolve(source.fileName))), lineOf(source, node));
|
|
172
|
+
if (!sourceSymbol ||
|
|
173
|
+
sourceSymbol.id ===
|
|
174
|
+
target.id) {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
this.graph.addEdge(this.edge(projectId, sourceSymbol.id, "USES_TYPE", target.id));
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
addWrite(projectId, rootPath, source, targetNode, checker) {
|
|
181
|
+
const symbol = this.resolveTsSymbol(targetNode, checker);
|
|
182
|
+
const declaration = symbol
|
|
183
|
+
?.valueDeclaration ??
|
|
184
|
+
symbol
|
|
185
|
+
?.declarations?.[0];
|
|
186
|
+
if (!declaration) {
|
|
187
|
+
return {
|
|
188
|
+
added: false,
|
|
189
|
+
propertyCreated: false,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
const declarationSource = declaration
|
|
193
|
+
.getSourceFile();
|
|
194
|
+
if (declarationSource
|
|
195
|
+
.isDeclarationFile) {
|
|
196
|
+
return {
|
|
197
|
+
added: false,
|
|
198
|
+
propertyCreated: false,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
const targetFile = normalize(relative(rootPath, resolve(declarationSource
|
|
202
|
+
.fileName)));
|
|
203
|
+
if (targetFile.startsWith("../") ||
|
|
204
|
+
targetFile.includes("node_modules/")) {
|
|
205
|
+
return {
|
|
206
|
+
added: false,
|
|
207
|
+
propertyCreated: false,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
const targetLine = lineOf(declarationSource, declaration);
|
|
211
|
+
let target = this.findSymbolAt(projectId, targetFile, targetLine);
|
|
212
|
+
let propertyCreated = false;
|
|
213
|
+
if (!target ||
|
|
214
|
+
target.type === "file") {
|
|
215
|
+
const name = symbol?.getName() ??
|
|
216
|
+
targetNode.getText(source);
|
|
217
|
+
const propertyId = id(projectId, targetFile, "property", name, targetLine);
|
|
218
|
+
target =
|
|
219
|
+
this.graph.getSymbol(propertyId);
|
|
220
|
+
if (!target) {
|
|
221
|
+
target = {
|
|
222
|
+
id: propertyId,
|
|
223
|
+
projectId,
|
|
224
|
+
name,
|
|
225
|
+
qualifiedName: name,
|
|
226
|
+
type: "property",
|
|
227
|
+
filePath: targetFile,
|
|
228
|
+
startLine: targetLine,
|
|
229
|
+
endLine: targetLine,
|
|
230
|
+
};
|
|
231
|
+
this.graph.addSymbol(target);
|
|
232
|
+
const targetFileNode = this.fileNode(projectId, targetFile);
|
|
233
|
+
if (targetFileNode) {
|
|
234
|
+
this.graph.addEdge(this.edge(projectId, targetFileNode.id, "DEFINES", target.id));
|
|
235
|
+
}
|
|
236
|
+
propertyCreated =
|
|
237
|
+
true;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (!target) {
|
|
241
|
+
return {
|
|
242
|
+
added: false,
|
|
243
|
+
propertyCreated,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
const sourceFile = normalize(relative(rootPath, resolve(source.fileName)));
|
|
247
|
+
const writer = this.containingSymbol(projectId, sourceFile, lineOf(source, targetNode));
|
|
248
|
+
if (!writer ||
|
|
249
|
+
writer.id === target.id) {
|
|
250
|
+
return {
|
|
251
|
+
added: false,
|
|
252
|
+
propertyCreated,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
this.graph.addEdge(this.edge(projectId, writer.id, "WRITES", target.id, {
|
|
256
|
+
expression: targetNode.getText(source),
|
|
257
|
+
}));
|
|
258
|
+
return {
|
|
259
|
+
added: true,
|
|
260
|
+
propertyCreated,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
addTestEdges(projectId) {
|
|
264
|
+
const edges = this.graph.allEdges(projectId);
|
|
265
|
+
let added = 0;
|
|
266
|
+
for (const edge of edges) {
|
|
267
|
+
if (edge.type !==
|
|
268
|
+
"IMPORTS") {
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
const from = this.graph.getSymbol(edge.from);
|
|
272
|
+
const to = this.graph.getSymbol(edge.to);
|
|
273
|
+
if (!from ||
|
|
274
|
+
!to ||
|
|
275
|
+
!this.isTestFile(from.filePath) ||
|
|
276
|
+
this.isTestFile(to.filePath)) {
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
this.graph.addEdge(this.edge(projectId, from.id, "TESTS", to.id));
|
|
280
|
+
added++;
|
|
281
|
+
}
|
|
282
|
+
return added;
|
|
283
|
+
}
|
|
284
|
+
addResolvedCalls(projectId, resolution) {
|
|
285
|
+
let added = 0;
|
|
286
|
+
for (const item of resolution.resolutions) {
|
|
287
|
+
if (item.kind !== "CALL" ||
|
|
288
|
+
!item.targetSymbolId) {
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
const target = this.graph.getSymbol(item.targetSymbolId);
|
|
292
|
+
if (!target) {
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
const source = this.containingSymbol(projectId, item.sourceFile, item.sourceLine);
|
|
296
|
+
if (!source ||
|
|
297
|
+
source.id === target.id) {
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
/*
|
|
301
|
+
* Nếu CALLS hiện tại đã đúng target
|
|
302
|
+
* thì không tạo edge trùng.
|
|
303
|
+
*/
|
|
304
|
+
const exactAlreadyExists = this.graph
|
|
305
|
+
.allEdges(projectId)
|
|
306
|
+
.some((edge) => edge.type ===
|
|
307
|
+
"CALLS" &&
|
|
308
|
+
edge.from ===
|
|
309
|
+
source.id &&
|
|
310
|
+
edge.to ===
|
|
311
|
+
target.id);
|
|
312
|
+
if (exactAlreadyExists) {
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
this.graph.addEdge(this.edge(projectId, source.id, "CALL_REFERENCE", target.id, {
|
|
316
|
+
expression: item.expression,
|
|
317
|
+
confidence: item.confidence,
|
|
318
|
+
resolver: item.resolver,
|
|
319
|
+
}));
|
|
320
|
+
added++;
|
|
321
|
+
}
|
|
322
|
+
return added;
|
|
323
|
+
}
|
|
324
|
+
resolveNodeSymbol(projectId, rootPath, node, checker) {
|
|
325
|
+
const symbol = this.resolveTsSymbol(node, checker);
|
|
326
|
+
const declaration = symbol
|
|
327
|
+
?.valueDeclaration ??
|
|
328
|
+
symbol
|
|
329
|
+
?.declarations?.[0];
|
|
330
|
+
if (!declaration) {
|
|
331
|
+
return undefined;
|
|
332
|
+
}
|
|
333
|
+
const targetSource = declaration
|
|
334
|
+
.getSourceFile();
|
|
335
|
+
if (targetSource
|
|
336
|
+
.isDeclarationFile) {
|
|
337
|
+
return undefined;
|
|
338
|
+
}
|
|
339
|
+
const filePath = normalize(relative(rootPath, resolve(targetSource
|
|
340
|
+
.fileName)));
|
|
341
|
+
if (filePath.startsWith("../") ||
|
|
342
|
+
filePath.includes("node_modules/")) {
|
|
343
|
+
return undefined;
|
|
344
|
+
}
|
|
345
|
+
return this.findSymbolAt(projectId, filePath, lineOf(targetSource, declaration), symbol?.getName());
|
|
346
|
+
}
|
|
347
|
+
resolveTsSymbol(node, checker) {
|
|
348
|
+
let symbol = checker.getSymbolAtLocation(node);
|
|
349
|
+
if (symbol &&
|
|
350
|
+
(symbol.flags &
|
|
351
|
+
ts.SymbolFlags.Alias)) {
|
|
352
|
+
try {
|
|
353
|
+
symbol =
|
|
354
|
+
checker.getAliasedSymbol(symbol);
|
|
355
|
+
}
|
|
356
|
+
catch {
|
|
357
|
+
// keep original
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return symbol;
|
|
361
|
+
}
|
|
362
|
+
containingSymbol(projectId, filePath, line) {
|
|
363
|
+
return this.graph
|
|
364
|
+
.allSymbols(projectId)
|
|
365
|
+
.filter((symbol) => normalize(symbol.filePath) ===
|
|
366
|
+
normalize(filePath) &&
|
|
367
|
+
typeof symbol.startLine ===
|
|
368
|
+
"number" &&
|
|
369
|
+
typeof symbol.endLine ===
|
|
370
|
+
"number" &&
|
|
371
|
+
symbol.startLine <= line &&
|
|
372
|
+
symbol.endLine >= line)
|
|
373
|
+
.sort((a, b) => ((a.endLine ?? 0) -
|
|
374
|
+
(a.startLine ?? 0)) -
|
|
375
|
+
((b.endLine ?? 0) -
|
|
376
|
+
(b.startLine ?? 0)))[0];
|
|
377
|
+
}
|
|
378
|
+
findSymbolAt(projectId, filePath, line, name) {
|
|
379
|
+
const candidates = this.graph
|
|
380
|
+
.allSymbols(projectId)
|
|
381
|
+
.filter((symbol) => normalize(symbol.filePath) ===
|
|
382
|
+
normalize(filePath) &&
|
|
383
|
+
typeof symbol.startLine ===
|
|
384
|
+
"number" &&
|
|
385
|
+
typeof symbol.endLine ===
|
|
386
|
+
"number" &&
|
|
387
|
+
symbol.startLine <= line &&
|
|
388
|
+
symbol.endLine >= line)
|
|
389
|
+
.sort((a, b) => ((a.endLine ?? 0) -
|
|
390
|
+
(a.startLine ?? 0)) -
|
|
391
|
+
((b.endLine ?? 0) -
|
|
392
|
+
(b.startLine ?? 0)));
|
|
393
|
+
if (name) {
|
|
394
|
+
const named = candidates.find((symbol) => symbol.name === name ||
|
|
395
|
+
(symbol.qualifiedName
|
|
396
|
+
?.endsWith(`.${name}`) ?? false));
|
|
397
|
+
if (named) {
|
|
398
|
+
return named;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return candidates[0];
|
|
402
|
+
}
|
|
403
|
+
fileNode(projectId, filePath) {
|
|
404
|
+
return this.graph
|
|
405
|
+
.allSymbols(projectId)
|
|
406
|
+
.find((symbol) => symbol.type ===
|
|
407
|
+
"file" &&
|
|
408
|
+
normalize(symbol.filePath) ===
|
|
409
|
+
normalize(filePath));
|
|
410
|
+
}
|
|
411
|
+
isTestFile(filePath) {
|
|
412
|
+
const path = normalize(filePath).toLowerCase();
|
|
413
|
+
return (path.startsWith("tests/") ||
|
|
414
|
+
path.includes("/tests/") ||
|
|
415
|
+
path.includes("__tests__") ||
|
|
416
|
+
/\.(test|spec)\.[^.]+$/
|
|
417
|
+
.test(path));
|
|
418
|
+
}
|
|
419
|
+
edge(projectId, from, type, to, metadata) {
|
|
420
|
+
return {
|
|
421
|
+
id: id(projectId, from, type, to, JSON.stringify(metadata ?? {})),
|
|
422
|
+
projectId,
|
|
423
|
+
from,
|
|
424
|
+
to,
|
|
425
|
+
type,
|
|
426
|
+
metadata,
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
loadConfig(rootPath) {
|
|
430
|
+
const configPath = ts.findConfigFile(rootPath, ts.sys.fileExists, "tsconfig.json");
|
|
431
|
+
if (configPath) {
|
|
432
|
+
const raw = ts.readConfigFile(configPath, ts.sys.readFile);
|
|
433
|
+
return ts.parseJsonConfigFileContent(raw.config, ts.sys, dirname(configPath));
|
|
434
|
+
}
|
|
435
|
+
return {
|
|
436
|
+
fileNames: ts.sys.readDirectory(rootPath, [
|
|
437
|
+
".ts",
|
|
438
|
+
".tsx",
|
|
439
|
+
".js",
|
|
440
|
+
".jsx",
|
|
441
|
+
".mts",
|
|
442
|
+
".cts",
|
|
443
|
+
".mjs",
|
|
444
|
+
".cjs",
|
|
445
|
+
], [
|
|
446
|
+
"node_modules",
|
|
447
|
+
".git",
|
|
448
|
+
"dist",
|
|
449
|
+
"build",
|
|
450
|
+
"coverage",
|
|
451
|
+
]),
|
|
452
|
+
options: {
|
|
453
|
+
allowJs: true,
|
|
454
|
+
noEmit: true,
|
|
455
|
+
skipLibCheck: true,
|
|
456
|
+
target: ts.ScriptTarget.ES2022,
|
|
457
|
+
module: ts.ModuleKind.NodeNext,
|
|
458
|
+
moduleResolution: ts.ModuleResolutionKind
|
|
459
|
+
.NodeNext,
|
|
460
|
+
},
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { PersistentCodeChunkStore, } from "../../storage/code-chunk-store.js";
|
|
2
|
+
import { PersistentCodeVectorStore, } from "../../storage/code-vector-store.js";
|
|
3
|
+
import { SmartCodeChunker, } from "../chunks/smart-chunker.js";
|
|
4
|
+
import { VectorStore, } from "../../retrieval/vector/vector-store.js";
|
|
5
|
+
import { tokenize, } from "../../retrieval/tokenizer.js";
|
|
6
|
+
import { codeSourceWeight, } from "./source-priority.js";
|
|
7
|
+
export class SemanticCodeEngine {
|
|
8
|
+
options;
|
|
9
|
+
vectors = new VectorStore();
|
|
10
|
+
chunks = new Map();
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.options = options;
|
|
13
|
+
}
|
|
14
|
+
async initialize() {
|
|
15
|
+
const { projectId, rootPath, storage, embeddings, graph, model, } = this.options;
|
|
16
|
+
const chunkStore = new PersistentCodeChunkStore(storage);
|
|
17
|
+
const vectorStore = new PersistentCodeVectorStore(storage);
|
|
18
|
+
const chunks = await new SmartCodeChunker()
|
|
19
|
+
.build(projectId, rootPath, graph.allSymbols(projectId));
|
|
20
|
+
this.chunks.clear();
|
|
21
|
+
for (const chunk of chunks) {
|
|
22
|
+
this.chunks.set(chunk.id, chunk);
|
|
23
|
+
}
|
|
24
|
+
const previousChunks = await chunkStore.load(projectId);
|
|
25
|
+
const chunksChanged = !previousChunks ||
|
|
26
|
+
previousChunks.chunks.length !==
|
|
27
|
+
chunks.length ||
|
|
28
|
+
previousChunks.chunks.some((old) => !this.chunks.has(old.id));
|
|
29
|
+
if (chunksChanged) {
|
|
30
|
+
await chunkStore.save({
|
|
31
|
+
version: 1,
|
|
32
|
+
projectId,
|
|
33
|
+
updatedAt: new Date()
|
|
34
|
+
.toISOString(),
|
|
35
|
+
chunks,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
const previousVectors = await vectorStore.load(projectId);
|
|
39
|
+
let vectorsLoaded = 0;
|
|
40
|
+
if (previousVectors &&
|
|
41
|
+
previousVectors.model ===
|
|
42
|
+
model) {
|
|
43
|
+
vectorsLoaded =
|
|
44
|
+
this.vectors.importRecords(previousVectors.records);
|
|
45
|
+
}
|
|
46
|
+
const validIds = new Set(chunks.map((chunk) => chunk.id));
|
|
47
|
+
let vectorsRemoved = 0;
|
|
48
|
+
for (const record of this.vectors
|
|
49
|
+
.exportProject(projectId)) {
|
|
50
|
+
if (!validIds.has(record.id)) {
|
|
51
|
+
if (this.vectors.remove(record.id)) {
|
|
52
|
+
vectorsRemoved++;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const pending = chunks.filter((chunk) => {
|
|
57
|
+
const existing = this.vectors.get(chunk.id);
|
|
58
|
+
if (!existing) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
return (existing.metadata
|
|
62
|
+
?.contentHash !==
|
|
63
|
+
chunk.contentHash);
|
|
64
|
+
});
|
|
65
|
+
let vectorsIndexed = 0;
|
|
66
|
+
/*
|
|
67
|
+
* Batch nhỏ để tránh request embedding quá lớn.
|
|
68
|
+
*/
|
|
69
|
+
const batchSize = 8;
|
|
70
|
+
let targetDimensions = this.vectors
|
|
71
|
+
.exportProject(projectId)[0]
|
|
72
|
+
?.vector.length;
|
|
73
|
+
for (let offset = 0; offset < pending.length; offset += batchSize) {
|
|
74
|
+
const batch = pending.slice(offset, offset +
|
|
75
|
+
batchSize);
|
|
76
|
+
const texts = batch.map((chunk) => [
|
|
77
|
+
`file: ${chunk.filePath}`,
|
|
78
|
+
chunk.symbolName
|
|
79
|
+
? `symbol: ${chunk.symbolName}`
|
|
80
|
+
: "",
|
|
81
|
+
chunk.symbolType
|
|
82
|
+
? `type: ${chunk.symbolType}`
|
|
83
|
+
: "",
|
|
84
|
+
chunk.content,
|
|
85
|
+
]
|
|
86
|
+
.filter(Boolean)
|
|
87
|
+
.join("\n"));
|
|
88
|
+
const vectors = await embeddings
|
|
89
|
+
.embedMany(texts);
|
|
90
|
+
for (let i = 0; i < batch.length; i++) {
|
|
91
|
+
const chunk = batch[i];
|
|
92
|
+
const vector = vectors[i];
|
|
93
|
+
if (!chunk ||
|
|
94
|
+
!vector ||
|
|
95
|
+
vector.length === 0) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (targetDimensions ===
|
|
99
|
+
undefined) {
|
|
100
|
+
targetDimensions =
|
|
101
|
+
vector.length;
|
|
102
|
+
}
|
|
103
|
+
/*
|
|
104
|
+
* Không cho trộn vector 384d/128d
|
|
105
|
+
* nếu provider fallback giữa chừng.
|
|
106
|
+
*/
|
|
107
|
+
if (vector.length !==
|
|
108
|
+
targetDimensions) {
|
|
109
|
+
console.warn(`[code-semantic] skip dimension mismatch: ${vector.length} != ${targetDimensions}`);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
this.vectors.upsert({
|
|
113
|
+
id: chunk.id,
|
|
114
|
+
projectId,
|
|
115
|
+
vector,
|
|
116
|
+
metadata: {
|
|
117
|
+
contentHash: chunk.contentHash,
|
|
118
|
+
filePath: chunk.filePath,
|
|
119
|
+
symbolName: chunk.symbolName,
|
|
120
|
+
symbolType: chunk.symbolType,
|
|
121
|
+
startLine: chunk.startLine,
|
|
122
|
+
endLine: chunk.endLine,
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
vectorsIndexed++;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const records = this.vectors
|
|
129
|
+
.exportProject(projectId);
|
|
130
|
+
const shouldSaveVectors = vectorsIndexed > 0 ||
|
|
131
|
+
vectorsRemoved > 0 ||
|
|
132
|
+
!previousVectors ||
|
|
133
|
+
previousVectors.model !==
|
|
134
|
+
model;
|
|
135
|
+
if (shouldSaveVectors) {
|
|
136
|
+
await vectorStore.save({
|
|
137
|
+
version: 1,
|
|
138
|
+
projectId,
|
|
139
|
+
model,
|
|
140
|
+
dimensions: records[0]
|
|
141
|
+
?.vector.length ??
|
|
142
|
+
targetDimensions ??
|
|
143
|
+
0,
|
|
144
|
+
updatedAt: new Date()
|
|
145
|
+
.toISOString(),
|
|
146
|
+
records,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
chunks: chunks.length,
|
|
151
|
+
vectorsLoaded,
|
|
152
|
+
vectorsIndexed,
|
|
153
|
+
vectorsRemoved,
|
|
154
|
+
totalVectors: records.length,
|
|
155
|
+
savedChunks: chunksChanged,
|
|
156
|
+
savedVectors: shouldSaveVectors,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
async search(query, limit = 8) {
|
|
160
|
+
const queryVector = await this.options
|
|
161
|
+
.embeddings
|
|
162
|
+
.embed(query);
|
|
163
|
+
const vectorResults = this.vectors.search(this.options.projectId, queryVector, Math.max(limit * 4, 20));
|
|
164
|
+
const queryTokens = new Set(tokenize(query));
|
|
165
|
+
const output = [];
|
|
166
|
+
for (const result of vectorResults) {
|
|
167
|
+
const chunk = this.chunks.get(result.id);
|
|
168
|
+
if (!chunk) {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
const documentTokens = new Set(tokenize([
|
|
172
|
+
chunk.filePath,
|
|
173
|
+
chunk.symbolName ??
|
|
174
|
+
"",
|
|
175
|
+
chunk.symbolType ??
|
|
176
|
+
"",
|
|
177
|
+
chunk.content,
|
|
178
|
+
].join(" ")));
|
|
179
|
+
let matches = 0;
|
|
180
|
+
for (const token of queryTokens) {
|
|
181
|
+
if (documentTokens.has(token)) {
|
|
182
|
+
matches++;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const lexicalScore = queryTokens.size === 0
|
|
186
|
+
? 0
|
|
187
|
+
: matches /
|
|
188
|
+
queryTokens.size;
|
|
189
|
+
const vectorScore = Math.max(0, result.score);
|
|
190
|
+
/*
|
|
191
|
+
* Semantic quan trọng hơn lexical.
|
|
192
|
+
*/
|
|
193
|
+
const sourceWeight = codeSourceWeight(chunk.filePath, query);
|
|
194
|
+
const score = (vectorScore *
|
|
195
|
+
0.75 +
|
|
196
|
+
lexicalScore *
|
|
197
|
+
0.25) *
|
|
198
|
+
sourceWeight;
|
|
199
|
+
output.push({
|
|
200
|
+
chunk,
|
|
201
|
+
score,
|
|
202
|
+
vectorScore,
|
|
203
|
+
lexicalScore,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
return output
|
|
207
|
+
.sort((a, b) => b.score - a.score)
|
|
208
|
+
.slice(0, limit);
|
|
209
|
+
}
|
|
210
|
+
size() {
|
|
211
|
+
return this.chunks.size;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function codeSourceWeight(filePath, query) {
|
|
2
|
+
const path = filePath
|
|
3
|
+
.toLowerCase()
|
|
4
|
+
.replaceAll("\\", "/");
|
|
5
|
+
const q = query.toLowerCase();
|
|
6
|
+
const testIntent = /\b(test|tests|spec|fixture|mock)\b/
|
|
7
|
+
.test(q);
|
|
8
|
+
const isTest = path.startsWith("tests/") ||
|
|
9
|
+
path.includes("/tests/") ||
|
|
10
|
+
path.includes("__tests__") ||
|
|
11
|
+
/\.(test|spec)\.[^.]+$/
|
|
12
|
+
.test(path);
|
|
13
|
+
if (isTest &&
|
|
14
|
+
!testIntent) {
|
|
15
|
+
return 0.35;
|
|
16
|
+
}
|
|
17
|
+
if (path.includes("/generated/") ||
|
|
18
|
+
path.includes(".generated.")) {
|
|
19
|
+
return 0.25;
|
|
20
|
+
}
|
|
21
|
+
return 1;
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|