@zvec/zvec-grep 0.1.4 → 0.1.6
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/README.md +86 -162
- package/README_CN.md +80 -162
- package/dist/authorization/index.d.ts +7 -0
- package/dist/authorization/index.js +7 -0
- package/dist/authorization/index.js.map +1 -0
- package/dist/authorization/manager.d.ts +8 -0
- package/dist/authorization/manager.js +19 -0
- package/dist/authorization/manager.js.map +1 -0
- package/dist/authorization/operation.d.ts +7 -0
- package/dist/authorization/operation.js +50 -0
- package/dist/authorization/operation.js.map +1 -0
- package/dist/authorization/planner.d.ts +21 -0
- package/dist/authorization/planner.js +97 -0
- package/dist/authorization/planner.js.map +1 -0
- package/dist/authorization/prompt.d.ts +11 -0
- package/dist/authorization/prompt.js +62 -0
- package/dist/authorization/prompt.js.map +1 -0
- package/dist/authorization/store.d.ts +20 -0
- package/dist/authorization/store.js +251 -0
- package/dist/authorization/store.js.map +1 -0
- package/dist/authorization/target.d.ts +15 -0
- package/dist/authorization/target.js +49 -0
- package/dist/authorization/target.js.map +1 -0
- package/dist/authorization/types.d.ts +61 -0
- package/dist/authorization/types.js +2 -0
- package/dist/authorization/types.js.map +1 -0
- package/dist/cli/args.d.ts +3 -0
- package/dist/cli/args.js +749 -201
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/auth.d.ts +21 -0
- package/dist/cli/auth.js +212 -0
- package/dist/cli/auth.js.map +1 -0
- package/dist/cli/commands.d.ts +3 -1
- package/dist/cli/commands.js +529 -180
- package/dist/cli/commands.js.map +1 -1
- package/dist/cli/errors.js +7 -6
- package/dist/cli/errors.js.map +1 -1
- package/dist/cli/format/context.d.ts +1 -0
- package/dist/cli/format/context.js +270 -70
- package/dist/cli/format/context.js.map +1 -1
- package/dist/cli/format/debug.js +14 -16
- package/dist/cli/format/debug.js.map +1 -1
- package/dist/cli/format/highlight.js.map +1 -1
- package/dist/cli/format/progress.d.ts +4 -0
- package/dist/cli/format/progress.js +222 -44
- package/dist/cli/format/progress.js.map +1 -1
- package/dist/cli/format/range.js.map +1 -1
- package/dist/cli/format/status.d.ts +77 -6
- package/dist/cli/format/status.js +343 -114
- package/dist/cli/format/status.js.map +1 -1
- package/dist/cli/help.d.ts +1 -1
- package/dist/cli/help.js +368 -84
- package/dist/cli/help.js.map +1 -1
- package/dist/cli/index.js +4 -4
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/install.d.ts +4 -0
- package/dist/cli/install.js +845 -0
- package/dist/cli/install.js.map +1 -0
- package/dist/cli/managed-rg.d.ts +10 -0
- package/dist/cli/managed-rg.js +329 -0
- package/dist/cli/managed-rg.js.map +1 -0
- package/dist/cli/types.d.ts +40 -13
- package/dist/cli/types.js.map +1 -1
- package/dist/cli/version.js +3 -1
- package/dist/cli/version.js.map +1 -1
- package/dist/client/daemon-client.d.ts +19 -0
- package/dist/client/daemon-client.js +180 -0
- package/dist/client/daemon-client.js.map +1 -0
- package/dist/client/mode-router.d.ts +9 -0
- package/dist/client/mode-router.js +28 -0
- package/dist/client/mode-router.js.map +1 -0
- package/dist/client/search-policy.d.ts +11 -0
- package/dist/client/search-policy.js +15 -0
- package/dist/client/search-policy.js.map +1 -0
- package/dist/daemon/backend.d.ts +76 -0
- package/dist/daemon/backend.js +1049 -0
- package/dist/daemon/backend.js.map +1 -0
- package/dist/daemon/change-set.d.ts +25 -0
- package/dist/daemon/change-set.js +101 -0
- package/dist/daemon/change-set.js.map +1 -0
- package/dist/daemon/config.d.ts +24 -0
- package/dist/daemon/config.js +88 -0
- package/dist/daemon/config.js.map +1 -0
- package/dist/daemon/errors.d.ts +5 -0
- package/dist/daemon/errors.js +11 -0
- package/dist/daemon/errors.js.map +1 -0
- package/dist/daemon/http-server.d.ts +30 -0
- package/dist/daemon/http-server.js +287 -0
- package/dist/daemon/http-server.js.map +1 -0
- package/dist/daemon/index-coordinator.d.ts +23 -0
- package/dist/daemon/index-coordinator.js +68 -0
- package/dist/daemon/index-coordinator.js.map +1 -0
- package/dist/daemon/job-scheduler.d.ts +70 -0
- package/dist/daemon/job-scheduler.js +366 -0
- package/dist/daemon/job-scheduler.js.map +1 -0
- package/dist/daemon/logger.d.ts +9 -0
- package/dist/daemon/logger.js +53 -0
- package/dist/daemon/logger.js.map +1 -0
- package/dist/daemon/model-pool.d.ts +45 -0
- package/dist/daemon/model-pool.js +187 -0
- package/dist/daemon/model-pool.js.map +1 -0
- package/dist/daemon/root-lease.d.ts +14 -0
- package/dist/daemon/root-lease.js +188 -0
- package/dist/daemon/root-lease.js.map +1 -0
- package/dist/daemon/root-runtime.d.ts +78 -0
- package/dist/daemon/root-runtime.js +333 -0
- package/dist/daemon/root-runtime.js.map +1 -0
- package/dist/daemon/runtime-manager.d.ts +45 -0
- package/dist/daemon/runtime-manager.js +243 -0
- package/dist/daemon/runtime-manager.js.map +1 -0
- package/dist/daemon/runtime.d.ts +12 -0
- package/dist/daemon/runtime.js +102 -0
- package/dist/daemon/runtime.js.map +1 -0
- package/dist/daemon/server-controller.d.ts +39 -0
- package/dist/daemon/server-controller.js +203 -0
- package/dist/daemon/server-controller.js.map +1 -0
- package/dist/daemon/watch-manager.d.ts +50 -0
- package/dist/daemon/watch-manager.js +293 -0
- package/dist/daemon/watch-manager.js.map +1 -0
- package/dist/daemon/workspace-read-session-cache.d.ts +31 -0
- package/dist/daemon/workspace-read-session-cache.js +115 -0
- package/dist/daemon/workspace-read-session-cache.js.map +1 -0
- package/dist/engine/config.d.ts +50 -0
- package/dist/engine/config.js +321 -0
- package/dist/engine/config.js.map +1 -0
- package/dist/engine/{errors/index.d.ts → errors.d.ts} +6 -1
- package/dist/engine/{errors/details.js → errors.js} +17 -4
- package/dist/engine/errors.js.map +1 -0
- package/dist/engine/extraction/code/adapter.d.ts +1 -1
- package/dist/engine/extraction/code/adapter.js.map +1 -1
- package/dist/engine/extraction/code/extractor.d.ts +5 -11
- package/dist/engine/extraction/code/extractor.js +184 -109
- package/dist/engine/extraction/code/extractor.js.map +1 -1
- package/dist/engine/extraction/code/families/c-family.js +13 -7
- package/dist/engine/extraction/code/families/c-family.js.map +1 -1
- package/dist/engine/extraction/code/families/js-ts.d.ts +1 -1
- package/dist/engine/extraction/code/families/js-ts.js +24 -18
- package/dist/engine/extraction/code/families/js-ts.js.map +1 -1
- package/dist/engine/extraction/code/families/metadata.d.ts +1 -1
- package/dist/engine/extraction/code/families/metadata.js +11 -8
- package/dist/engine/extraction/code/families/metadata.js.map +1 -1
- package/dist/engine/extraction/code/families/name-field.js +5 -5
- package/dist/engine/extraction/code/families/name-field.js.map +1 -1
- package/dist/engine/extraction/code/languages/c.js +1 -4
- package/dist/engine/extraction/code/languages/c.js.map +1 -1
- package/dist/engine/extraction/code/languages/cpp.js.map +1 -1
- package/dist/engine/extraction/code/languages/go.js +1 -3
- package/dist/engine/extraction/code/languages/go.js.map +1 -1
- package/dist/engine/extraction/code/languages/java.js.map +1 -1
- package/dist/engine/extraction/code/languages/javascript.js +1 -3
- package/dist/engine/extraction/code/languages/javascript.js.map +1 -1
- package/dist/engine/extraction/code/languages/python.js +5 -6
- package/dist/engine/extraction/code/languages/python.js.map +1 -1
- package/dist/engine/extraction/code/languages/rust.js +1 -5
- package/dist/engine/extraction/code/languages/rust.js.map +1 -1
- package/dist/engine/extraction/code/languages/typescript.js.map +1 -1
- package/dist/engine/extraction/code/tree-sitter/grammar.js.map +1 -0
- package/dist/engine/extraction/{tree-sitter → code/tree-sitter}/nodes.js +2 -1
- package/dist/engine/extraction/code/tree-sitter/nodes.js.map +1 -0
- package/dist/engine/extraction/code/tree-sitter/parser.js.map +1 -0
- package/dist/engine/extraction/ids.js.map +1 -1
- package/dist/engine/extraction/image/extractor.d.ts +5 -0
- package/dist/engine/extraction/image/extractor.js +35 -0
- package/dist/engine/extraction/image/extractor.js.map +1 -0
- package/dist/engine/extraction/index.d.ts +3 -44
- package/dist/engine/extraction/index.js +1 -280
- package/dist/engine/extraction/index.js.map +1 -1
- package/dist/engine/extraction/markdown/extractor.d.ts +5 -11
- package/dist/engine/extraction/markdown/extractor.js +37 -16
- package/dist/engine/extraction/markdown/extractor.js.map +1 -1
- package/dist/engine/extraction/runtime.d.ts +4 -0
- package/dist/engine/extraction/runtime.js +26 -0
- package/dist/engine/extraction/runtime.js.map +1 -0
- package/dist/engine/extraction/source.d.ts +18 -0
- package/dist/engine/extraction/source.js +22 -0
- package/dist/engine/extraction/source.js.map +1 -0
- package/dist/engine/extraction/text/extractor.d.ts +7 -0
- package/dist/engine/extraction/text/extractor.js +168 -0
- package/dist/engine/extraction/text/extractor.js.map +1 -0
- package/dist/engine/extraction/types.d.ts +4 -0
- package/dist/engine/{models → extraction}/types.js.map +1 -1
- package/dist/engine/{files/file-type.d.ts → file-type.d.ts} +1 -1
- package/dist/engine/file-type.js.map +1 -0
- package/dist/engine/index-status.d.ts +9 -0
- package/dist/engine/index-status.js +38 -0
- package/dist/engine/index-status.js.map +1 -0
- package/dist/engine/manifest.d.ts +12 -0
- package/dist/engine/manifest.js +120 -0
- package/dist/engine/manifest.js.map +1 -0
- package/dist/engine/models/{providers/llama-cpp/embedding.d.ts → backends/llama-cpp.d.ts} +25 -20
- package/dist/engine/models/{providers/llama-cpp/embedding.js → backends/llama-cpp.js} +165 -113
- package/dist/engine/models/backends/llama-cpp.js.map +1 -0
- package/dist/engine/models/backends/model2vec.d.ts +57 -0
- package/dist/engine/models/backends/model2vec.js +332 -0
- package/dist/engine/models/backends/model2vec.js.map +1 -0
- package/dist/engine/models/backends/qwen.d.ts +44 -0
- package/dist/engine/models/{providers/qwen/embedding.js → backends/qwen.js} +168 -102
- package/dist/engine/models/backends/qwen.js.map +1 -0
- package/dist/engine/models/backends/transformers-js.d.ts +87 -0
- package/dist/engine/models/backends/transformers-js.js +316 -0
- package/dist/engine/models/backends/transformers-js.js.map +1 -0
- package/dist/engine/models/catalog.d.ts +188 -23
- package/dist/engine/models/catalog.js +172 -41
- package/dist/engine/models/catalog.js.map +1 -1
- package/dist/engine/models/download-progress.d.ts +15 -0
- package/dist/engine/models/download-progress.js +48 -0
- package/dist/engine/models/download-progress.js.map +1 -0
- package/dist/engine/models/embeddings.d.ts +58 -18
- package/dist/engine/models/embeddings.js +53 -29
- package/dist/engine/models/embeddings.js.map +1 -1
- package/dist/engine/models/factory.d.ts +2 -5
- package/dist/engine/models/factory.js +41 -56
- package/dist/engine/models/factory.js.map +1 -1
- package/dist/engine/models/index.d.ts +4 -6
- package/dist/engine/models/index.js +4 -4
- package/dist/engine/models/index.js.map +1 -1
- package/dist/engine/models/ranking.d.ts +7 -3
- package/dist/engine/models/resolution.d.ts +8 -0
- package/dist/engine/models/resolution.js +22 -0
- package/dist/engine/models/resolution.js.map +1 -0
- package/dist/engine/pipeline/indexing/index.d.ts +9 -7
- package/dist/engine/pipeline/indexing/index.js +399 -147
- package/dist/engine/pipeline/indexing/index.js.map +1 -1
- package/dist/engine/pipeline/indexing/root-paths.js +16 -19
- package/dist/engine/pipeline/indexing/root-paths.js.map +1 -1
- package/dist/engine/pipeline/indexing/scanner/index.d.ts +6 -1
- package/dist/engine/pipeline/indexing/scanner/index.js +318 -66
- package/dist/engine/pipeline/indexing/scanner/index.js.map +1 -1
- package/dist/engine/pipeline/search/index.d.ts +6 -6
- package/dist/engine/pipeline/search/index.js +76 -34
- package/dist/engine/pipeline/search/index.js.map +1 -1
- package/dist/engine/service/index.d.ts +3 -2
- package/dist/engine/service/index.js +1 -1
- package/dist/engine/service/index.js.map +1 -1
- package/dist/engine/service/lexical.d.ts +14 -15
- package/dist/engine/service/lexical.js +78 -119
- package/dist/engine/service/lexical.js.map +1 -1
- package/dist/engine/service/root.d.ts +13 -12
- package/dist/engine/service/root.js +35 -24
- package/dist/engine/service/root.js.map +1 -1
- package/dist/engine/service/structure-enrichment.js +29 -25
- package/dist/engine/service/structure-enrichment.js.map +1 -1
- package/dist/engine/service/types.d.ts +61 -35
- package/dist/engine/service/workspace-index.d.ts +24 -0
- package/dist/engine/service/workspace-index.js +151 -0
- package/dist/engine/service/workspace-index.js.map +1 -0
- package/dist/engine/service/zvec-grep.d.ts +12 -1
- package/dist/engine/service/zvec-grep.js +878 -544
- package/dist/engine/service/zvec-grep.js.map +1 -1
- package/dist/engine/storage/index.d.ts +8 -4
- package/dist/engine/storage/index.js +1 -1
- package/dist/engine/storage/index.js.map +1 -1
- package/dist/engine/storage/zvec.d.ts +10 -33
- package/dist/engine/storage/zvec.js +110 -49
- package/dist/engine/storage/zvec.js.map +1 -1
- package/dist/engine/types.d.ts +29 -23
- package/dist/engine/types.js +1 -1
- package/dist/engine/types.js.map +1 -1
- package/dist/engine/utils/daemon-lease.d.ts +17 -0
- package/dist/engine/utils/daemon-lease.js +175 -0
- package/dist/engine/utils/daemon-lease.js.map +1 -0
- package/dist/engine/utils/file-selection.d.ts +10 -0
- package/dist/engine/utils/file-selection.js +157 -0
- package/dist/engine/utils/file-selection.js.map +1 -0
- package/dist/engine/utils/glob.d.ts +3 -0
- package/dist/engine/utils/glob.js +116 -18
- package/dist/engine/utils/glob.js.map +1 -1
- package/dist/engine/utils/hash.js.map +1 -1
- package/dist/engine/utils/json.d.ts +6 -2
- package/dist/engine/utils/json.js +50 -12
- package/dist/engine/utils/json.js.map +1 -1
- package/dist/engine/utils/lock.js +7 -7
- package/dist/engine/utils/lock.js.map +1 -1
- package/dist/engine/utils/path.js +2 -2
- package/dist/engine/utils/path.js.map +1 -1
- package/dist/engine/utils/timing.js.map +1 -1
- package/dist/index-progress.d.ts +10 -0
- package/dist/index-progress.js +159 -0
- package/dist/index-progress.js.map +1 -0
- package/dist/index.d.ts +6 -6
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/mcp/http-transport.d.ts +27 -0
- package/dist/mcp/http-transport.js +182 -0
- package/dist/mcp/http-transport.js.map +1 -0
- package/dist/mcp/input-normalization.d.ts +38 -0
- package/dist/mcp/input-normalization.js +95 -0
- package/dist/mcp/input-normalization.js.map +1 -0
- package/dist/mcp/progress-heartbeat.d.ts +17 -0
- package/dist/mcp/progress-heartbeat.js +46 -0
- package/dist/mcp/progress-heartbeat.js.map +1 -0
- package/dist/mcp/request-metadata.d.ts +2 -0
- package/dist/mcp/request-metadata.js +11 -0
- package/dist/mcp/request-metadata.js.map +1 -0
- package/dist/mcp/request-state.d.ts +40 -0
- package/dist/mcp/request-state.js +210 -0
- package/dist/mcp/request-state.js.map +1 -0
- package/dist/mcp/result-format.d.ts +13 -0
- package/dist/mcp/result-format.js +75 -0
- package/dist/mcp/result-format.js.map +1 -0
- package/dist/mcp/schemas.d.ts +430 -0
- package/dist/mcp/schemas.js +447 -0
- package/dist/mcp/schemas.js.map +1 -0
- package/dist/mcp/tools.d.ts +150 -0
- package/dist/mcp/tools.js +448 -0
- package/dist/mcp/tools.js.map +1 -0
- package/dist/mcp/toolset.d.ts +5 -0
- package/dist/mcp/toolset.js +12 -0
- package/dist/mcp/toolset.js.map +1 -0
- package/dist/observability/trace-context.d.ts +11 -0
- package/dist/observability/trace-context.js +138 -0
- package/dist/observability/trace-context.js.map +1 -0
- package/docs/01-agents.md +111 -0
- package/docs/02-cli.md +229 -0
- package/docs/03-mcp.md +167 -0
- package/docs/04-pipeline.md +218 -0
- package/docs/05-architecture.md +86 -0
- package/docs/06-server.md +210 -0
- package/docs/07-embedding.md +179 -0
- package/docs/08-roadmap.md +85 -0
- package/docs/README.md +53 -0
- package/package.json +43 -7
- package/skills/zvec-grep/SKILL.md +43 -123
- package/skills/zvec-grep/agents/openai.yaml +2 -2
- package/skills/zvec-grep/references/cli-fallback.md +85 -0
- package/dist/engine/collection/index.d.ts +0 -55
- package/dist/engine/collection/index.js +0 -710
- package/dist/engine/collection/index.js.map +0 -1
- package/dist/engine/errors/details.d.ts +0 -6
- package/dist/engine/errors/details.js.map +0 -1
- package/dist/engine/errors/index.js +0 -16
- package/dist/engine/errors/index.js.map +0 -1
- package/dist/engine/extraction/tree-sitter/grammar.js.map +0 -1
- package/dist/engine/extraction/tree-sitter/nodes.js.map +0 -1
- package/dist/engine/extraction/tree-sitter/parser.js.map +0 -1
- package/dist/engine/files/file-type.js.map +0 -1
- package/dist/engine/models/providers/index.d.ts +0 -2
- package/dist/engine/models/providers/index.js +0 -3
- package/dist/engine/models/providers/index.js.map +0 -1
- package/dist/engine/models/providers/llama-cpp/embedding.js.map +0 -1
- package/dist/engine/models/providers/llama-cpp/index.d.ts +0 -1
- package/dist/engine/models/providers/llama-cpp/index.js +0 -2
- package/dist/engine/models/providers/llama-cpp/index.js.map +0 -1
- package/dist/engine/models/providers/qwen/embedding.d.ts +0 -42
- package/dist/engine/models/providers/qwen/embedding.js.map +0 -1
- package/dist/engine/models/providers/qwen/index.d.ts +0 -1
- package/dist/engine/models/providers/qwen/index.js +0 -2
- package/dist/engine/models/providers/qwen/index.js.map +0 -1
- package/dist/engine/models/types.d.ts +0 -30
- /package/dist/engine/extraction/{tree-sitter → code/tree-sitter}/grammar.d.ts +0 -0
- /package/dist/engine/extraction/{tree-sitter → code/tree-sitter}/grammar.js +0 -0
- /package/dist/engine/extraction/{tree-sitter → code/tree-sitter}/nodes.d.ts +0 -0
- /package/dist/engine/extraction/{tree-sitter → code/tree-sitter}/parser.d.ts +0 -0
- /package/dist/engine/extraction/{tree-sitter → code/tree-sitter}/parser.js +0 -0
- /package/dist/engine/{models → extraction}/types.js +0 -0
- /package/dist/engine/{files/file-type.js → file-type.js} +0 -0
|
@@ -1,710 +0,0 @@
|
|
|
1
|
-
import { ZVecCollectionSchema, ZVecCreateAndOpen, ZVecDataType, ZVecIndexType, ZVecOpen, } from "@zvec/zvec";
|
|
2
|
-
import { randomUUID } from "node:crypto";
|
|
3
|
-
import { existsSync, mkdirSync, rmSync } from "node:fs";
|
|
4
|
-
import { basename, dirname, join } from "node:path";
|
|
5
|
-
import { collectionDetail, detail, EngineError, errorDetails, } from "../errors/index.js";
|
|
6
|
-
import { getCollectionIndexStatus, indexCollection, } from "../pipeline/indexing/index.js";
|
|
7
|
-
import { fileBelongsToRootPath, validateRootPaths, } from "../pipeline/indexing/root-paths.js";
|
|
8
|
-
import { diagnoseEntitySearch, diagnoseFileSearch, searchPlanCollection, } from "../pipeline/search/index.js";
|
|
9
|
-
import { assertZvecStatus, initializeZvec, openZvecCollection, quoteFilterString, readNullableNumberFieldFromFields, readNullableStringFieldFromFields, readNumberFieldFromFields, readStringFieldFromFields, ZvecCollectionStorage, ZvecFileMetaStore, } from "../storage/zvec.js";
|
|
10
|
-
import { CURRENT_INDEX_VERSION } from "../types.js";
|
|
11
|
-
import { defaultHome, normalizePath } from "../utils/path.js";
|
|
12
|
-
const COLLECTIONS_ZVEC = "collections.zvec";
|
|
13
|
-
const FILES_ZVEC = "files.zvec";
|
|
14
|
-
export class Collection {
|
|
15
|
-
info;
|
|
16
|
-
embeddingModel;
|
|
17
|
-
readOnly;
|
|
18
|
-
fileStorePath;
|
|
19
|
-
storage;
|
|
20
|
-
embedding;
|
|
21
|
-
constructor(info, embeddingModel, readOnly = false, fileStorePath = join(info.path, FILES_ZVEC)) {
|
|
22
|
-
this.info = info;
|
|
23
|
-
this.embeddingModel = embeddingModel;
|
|
24
|
-
this.readOnly = readOnly;
|
|
25
|
-
this.fileStorePath = fileStorePath;
|
|
26
|
-
this.embedding = requireIndexedEmbedding(info, "open");
|
|
27
|
-
this.validateIndexVersion();
|
|
28
|
-
if (this.embeddingModel) {
|
|
29
|
-
this.validateEmbeddingSchema(this.embeddingModel);
|
|
30
|
-
}
|
|
31
|
-
this.storage = new ZvecCollectionStorage(info.path, this.embedding, this.fileStorePath, info.id, readOnly);
|
|
32
|
-
}
|
|
33
|
-
get id() {
|
|
34
|
-
return this.info.id;
|
|
35
|
-
}
|
|
36
|
-
get name() {
|
|
37
|
-
return this.info.name;
|
|
38
|
-
}
|
|
39
|
-
index(options = {}) {
|
|
40
|
-
if (this.readOnly) {
|
|
41
|
-
throw new EngineError("Cannot index a read-only collection", {
|
|
42
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.READ_ONLY",
|
|
43
|
-
context: collectionOperationDetails(this.name, "index"),
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
const embeddingModel = this.requireEmbeddingModel("index");
|
|
47
|
-
return indexCollection({
|
|
48
|
-
collection: this.info,
|
|
49
|
-
embeddingModel,
|
|
50
|
-
storage: this.storage,
|
|
51
|
-
embeddingConcurrency: options.embeddingConcurrency,
|
|
52
|
-
onProgress: options.onProgress,
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
status() {
|
|
56
|
-
return getCollectionIndexStatus(this.info, this.storage.listFiles());
|
|
57
|
-
}
|
|
58
|
-
searchPlan(plan) {
|
|
59
|
-
return searchPlanCollection(plan, {
|
|
60
|
-
collection: this.info,
|
|
61
|
-
embeddingModel: this.embeddingModel,
|
|
62
|
-
storage: this.storage,
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
diagnoseFile(absolutePath) {
|
|
66
|
-
const path = normalizePath(absolutePath);
|
|
67
|
-
const file = this.storage.getFileByPath(path);
|
|
68
|
-
const matchedRootPath = this.info.rootPaths.find((rootPath) => fileBelongsToRootPath(path, rootPath));
|
|
69
|
-
return {
|
|
70
|
-
collectionId: this.id,
|
|
71
|
-
collectionName: this.name,
|
|
72
|
-
absolutePath: path,
|
|
73
|
-
belongsToCollection: matchedRootPath !== undefined,
|
|
74
|
-
matchedRootPath: matchedRootPath?.absolutePath,
|
|
75
|
-
file: file ?? undefined,
|
|
76
|
-
entityCount: file?.indexStatus?.entityCount ?? 0,
|
|
77
|
-
reason: file
|
|
78
|
-
? undefined
|
|
79
|
-
: matchedRootPath
|
|
80
|
-
? "File belongs to this collection but has not been indexed"
|
|
81
|
-
: "File is outside this collection roots or excluded by root patterns",
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
diagnoseEntitySearch(query, entityId) {
|
|
85
|
-
const embeddingModel = this.requireEmbeddingModel("diagnose");
|
|
86
|
-
return diagnoseEntitySearch(query, entityId, {
|
|
87
|
-
collection: this.info,
|
|
88
|
-
embeddingModel,
|
|
89
|
-
storage: this.storage,
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
diagnoseFileSearch(query, absolutePath) {
|
|
93
|
-
const embeddingModel = this.requireEmbeddingModel("diagnose");
|
|
94
|
-
return diagnoseFileSearch(query, absolutePath, {
|
|
95
|
-
collection: this.info,
|
|
96
|
-
embeddingModel,
|
|
97
|
-
storage: this.storage,
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
getFile(absolutePath) {
|
|
101
|
-
return this.storage.getFileByPath(absolutePath);
|
|
102
|
-
}
|
|
103
|
-
listEntitiesByFile(fileId, options = {}) {
|
|
104
|
-
return this.storage.listEntitiesByFile(fileId, options);
|
|
105
|
-
}
|
|
106
|
-
getEntity(entityId) {
|
|
107
|
-
return this.storage.getEntity(entityId);
|
|
108
|
-
}
|
|
109
|
-
close() {
|
|
110
|
-
this.storage.close();
|
|
111
|
-
}
|
|
112
|
-
validateIndexVersion() {
|
|
113
|
-
if (this.info.indexVersion !== CURRENT_INDEX_VERSION) {
|
|
114
|
-
throw new EngineError("Collection index version is not supported", {
|
|
115
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.INDEX_VERSION_MISMATCH",
|
|
116
|
-
context: errorDetails([
|
|
117
|
-
collectionDetail(this.name),
|
|
118
|
-
detail("expected", CURRENT_INDEX_VERSION),
|
|
119
|
-
detail("actual", this.info.indexVersion),
|
|
120
|
-
]),
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
validateEmbeddingSchema(current) {
|
|
125
|
-
this.validateIndexVersion();
|
|
126
|
-
const expected = this.embedding;
|
|
127
|
-
if (expected.provider !== current.ref.provider) {
|
|
128
|
-
throw new EngineError("Collection embedding provider does not match current model", {
|
|
129
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.EMBEDDING_PROVIDER_MISMATCH",
|
|
130
|
-
context: collectionMismatchDetails(this.name, expected.provider, current.ref.provider),
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
if (expected.model !== current.ref.model) {
|
|
134
|
-
throw new EngineError("Collection embedding model does not match current model", {
|
|
135
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.EMBEDDING_MODEL_MISMATCH",
|
|
136
|
-
context: collectionMismatchDetails(this.name, expected.model, current.ref.model),
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
if (expected.dimension !== current.dimension) {
|
|
140
|
-
throw new EngineError("Collection embedding dimension does not match current model", {
|
|
141
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.EMBEDDING_DIMENSION_MISMATCH",
|
|
142
|
-
context: collectionMismatchDetails(this.name, expected.dimension, current.dimension),
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
if (expected.metric !== current.metric) {
|
|
146
|
-
throw new EngineError("Collection embedding metric does not match current model", {
|
|
147
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.EMBEDDING_METRIC_MISMATCH",
|
|
148
|
-
context: collectionMismatchDetails(this.name, expected.metric, current.metric),
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
requireEmbeddingModel(operation) {
|
|
153
|
-
if (!this.embeddingModel) {
|
|
154
|
-
throw new EngineError("Collection operation requires an embedding model", {
|
|
155
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.EMBEDDING_MODEL_REQUIRED",
|
|
156
|
-
context: collectionOperationDetails(this.name, operation),
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
return this.embeddingModel;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
function collectionOperationDetails(name, operation) {
|
|
163
|
-
return errorDetails([
|
|
164
|
-
collectionDetail(name),
|
|
165
|
-
detail("operation", operation),
|
|
166
|
-
]);
|
|
167
|
-
}
|
|
168
|
-
function collectionMismatchDetails(name, expected, actual) {
|
|
169
|
-
return errorDetails([
|
|
170
|
-
collectionDetail(name),
|
|
171
|
-
detail("expected", expected),
|
|
172
|
-
detail("actual", actual),
|
|
173
|
-
]);
|
|
174
|
-
}
|
|
175
|
-
export function isCollectionIndexed(info) {
|
|
176
|
-
return info?.embedding !== null
|
|
177
|
-
&& info?.embedding !== undefined
|
|
178
|
-
&& info.indexVersion !== null
|
|
179
|
-
&& info.indexVersion !== undefined;
|
|
180
|
-
}
|
|
181
|
-
function requireIndexedEmbedding(info, operation) {
|
|
182
|
-
if (isCollectionIndexed(info)) {
|
|
183
|
-
return info.embedding;
|
|
184
|
-
}
|
|
185
|
-
throw new EngineError("Collection index has not been built", {
|
|
186
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.INDEX_MISSING",
|
|
187
|
-
context: errorDetails([
|
|
188
|
-
collectionDetail(info.name),
|
|
189
|
-
detail("operation", operation),
|
|
190
|
-
detail("hint", "Run zg --index to build this index."),
|
|
191
|
-
]),
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
export class CollectionRegistry {
|
|
195
|
-
home;
|
|
196
|
-
embeddingModel;
|
|
197
|
-
readOnly;
|
|
198
|
-
meta;
|
|
199
|
-
collections = new Map();
|
|
200
|
-
constructor(home = defaultHome(), embeddingModel, readOnly = false) {
|
|
201
|
-
this.home = home;
|
|
202
|
-
this.embeddingModel = embeddingModel;
|
|
203
|
-
this.readOnly = readOnly;
|
|
204
|
-
if (!readOnly) {
|
|
205
|
-
mkdirSync(home, { recursive: true });
|
|
206
|
-
}
|
|
207
|
-
this.meta = new ZvecCollectionsMetaStore(collectionsMetaPath(home), readOnly);
|
|
208
|
-
}
|
|
209
|
-
list() {
|
|
210
|
-
return this.meta.list();
|
|
211
|
-
}
|
|
212
|
-
get(name) {
|
|
213
|
-
return this.meta.getByName(name);
|
|
214
|
-
}
|
|
215
|
-
has(name) {
|
|
216
|
-
return this.get(name) !== null;
|
|
217
|
-
}
|
|
218
|
-
remove(name) {
|
|
219
|
-
if (this.readOnly) {
|
|
220
|
-
throw new EngineError("Cannot remove a collection from a read-only registry", {
|
|
221
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.READ_ONLY",
|
|
222
|
-
context: collectionOperationDetails(name, "remove"),
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
const info = this.get(name);
|
|
226
|
-
if (!info) {
|
|
227
|
-
return false;
|
|
228
|
-
}
|
|
229
|
-
const cached = this.collections.get(name);
|
|
230
|
-
if (cached) {
|
|
231
|
-
cached.close();
|
|
232
|
-
this.collections.delete(name);
|
|
233
|
-
}
|
|
234
|
-
this.meta.remove(info.id);
|
|
235
|
-
const files = new ZvecFileMetaStore(filesMetaPath(this.home), false);
|
|
236
|
-
try {
|
|
237
|
-
files.deleteCollection(info.id);
|
|
238
|
-
}
|
|
239
|
-
finally {
|
|
240
|
-
files.close();
|
|
241
|
-
}
|
|
242
|
-
rmSync(info.path, { recursive: true, force: true });
|
|
243
|
-
return true;
|
|
244
|
-
}
|
|
245
|
-
rename(name, nextName) {
|
|
246
|
-
if (this.readOnly) {
|
|
247
|
-
throw new EngineError("Cannot rename a collection from a read-only registry", {
|
|
248
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.READ_ONLY",
|
|
249
|
-
context: collectionOperationDetails(name, "rename"),
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
const normalizedNextName = nextName.trim();
|
|
253
|
-
if (normalizedNextName.length === 0) {
|
|
254
|
-
throw new EngineError("Collection rename requires a non-empty name", {
|
|
255
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.EMPTY_NAME",
|
|
256
|
-
context: collectionOperationDetails(name, "rename"),
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
const info = this.get(name);
|
|
260
|
-
if (!info) {
|
|
261
|
-
return null;
|
|
262
|
-
}
|
|
263
|
-
if (this.has(normalizedNextName)) {
|
|
264
|
-
throw new EngineError("Collection already exists", {
|
|
265
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.ALREADY_EXISTS",
|
|
266
|
-
context: errorDetails([collectionDetail(normalizedNextName)]),
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
const cached = this.collections.get(name);
|
|
270
|
-
if (cached) {
|
|
271
|
-
cached.close();
|
|
272
|
-
this.collections.delete(name);
|
|
273
|
-
}
|
|
274
|
-
const updated = {
|
|
275
|
-
...info,
|
|
276
|
-
name: normalizedNextName,
|
|
277
|
-
updatedTime: Date.now(),
|
|
278
|
-
};
|
|
279
|
-
this.meta.upsert(updated);
|
|
280
|
-
return updated;
|
|
281
|
-
}
|
|
282
|
-
async status(name) {
|
|
283
|
-
const info = this.get(name);
|
|
284
|
-
if (!info || !isCollectionIndexed(info) || info.indexPolicy === "disabled") {
|
|
285
|
-
return null;
|
|
286
|
-
}
|
|
287
|
-
return this.open(name).status();
|
|
288
|
-
}
|
|
289
|
-
create(name, rootPaths, path) {
|
|
290
|
-
if (this.readOnly) {
|
|
291
|
-
throw new EngineError("Cannot create a collection in a read-only registry", {
|
|
292
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.READ_ONLY",
|
|
293
|
-
context: collectionOperationDetails(name, "create"),
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
if (!this.embeddingModel) {
|
|
297
|
-
throw new EngineError("Creating a collection requires an embedding model", {
|
|
298
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.EMBEDDING_MODEL_REQUIRED",
|
|
299
|
-
context: collectionOperationDetails(name, "create"),
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
if (this.has(name)) {
|
|
303
|
-
throw new EngineError("Collection already exists", {
|
|
304
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.ALREADY_EXISTS",
|
|
305
|
-
context: errorDetails([collectionDetail(name)]),
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
const id = randomUUID();
|
|
309
|
-
const collectionPath = normalizePath(path ?? join(this.home, "collections", id));
|
|
310
|
-
const now = Date.now();
|
|
311
|
-
const normalizedRootPaths = validateRootPaths(rootPaths);
|
|
312
|
-
const info = {
|
|
313
|
-
id,
|
|
314
|
-
name,
|
|
315
|
-
path: collectionPath,
|
|
316
|
-
rootPaths: normalizedRootPaths,
|
|
317
|
-
indexPolicy: "enabled",
|
|
318
|
-
embedding: currentEmbeddingSchema(this.embeddingModel),
|
|
319
|
-
indexVersion: CURRENT_INDEX_VERSION,
|
|
320
|
-
createdTime: now,
|
|
321
|
-
updatedTime: now,
|
|
322
|
-
};
|
|
323
|
-
mkdirSync(collectionPath, { recursive: true });
|
|
324
|
-
this.meta.upsert(info);
|
|
325
|
-
return info;
|
|
326
|
-
}
|
|
327
|
-
prepareIndex(name, rootPaths, path) {
|
|
328
|
-
if (this.readOnly) {
|
|
329
|
-
throw new EngineError("Cannot prepare a collection in a read-only registry", {
|
|
330
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.READ_ONLY",
|
|
331
|
-
context: collectionOperationDetails(name, "prepareIndex"),
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
if (!this.embeddingModel) {
|
|
335
|
-
throw new EngineError("Preparing a collection requires an embedding model", {
|
|
336
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.EMBEDDING_MODEL_REQUIRED",
|
|
337
|
-
context: collectionOperationDetails(name, "prepareIndex"),
|
|
338
|
-
});
|
|
339
|
-
}
|
|
340
|
-
const existing = this.get(name);
|
|
341
|
-
if (!existing) {
|
|
342
|
-
return this.create(name, rootPaths, path);
|
|
343
|
-
}
|
|
344
|
-
const normalizedRootPaths = validateRootPaths(rootPaths);
|
|
345
|
-
const collectionPath = normalizePath(path ?? existing.path);
|
|
346
|
-
const cached = this.collections.get(name);
|
|
347
|
-
if (cached) {
|
|
348
|
-
cached.close();
|
|
349
|
-
this.collections.delete(name);
|
|
350
|
-
}
|
|
351
|
-
const updated = {
|
|
352
|
-
...existing,
|
|
353
|
-
path: collectionPath,
|
|
354
|
-
rootPaths: normalizedRootPaths,
|
|
355
|
-
indexPolicy: "enabled",
|
|
356
|
-
embedding: currentEmbeddingSchema(this.embeddingModel),
|
|
357
|
-
indexVersion: CURRENT_INDEX_VERSION,
|
|
358
|
-
updatedTime: Date.now(),
|
|
359
|
-
};
|
|
360
|
-
mkdirSync(collectionPath, { recursive: true });
|
|
361
|
-
this.meta.upsert(updated);
|
|
362
|
-
return updated;
|
|
363
|
-
}
|
|
364
|
-
disableIndex(name, rootPaths, path) {
|
|
365
|
-
if (this.readOnly) {
|
|
366
|
-
throw new EngineError("Cannot disable a collection index in a read-only registry", {
|
|
367
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.READ_ONLY",
|
|
368
|
-
context: collectionOperationDetails(name, "disableIndex"),
|
|
369
|
-
});
|
|
370
|
-
}
|
|
371
|
-
const existing = this.get(name);
|
|
372
|
-
const normalizedRootPaths = validateRootPaths(rootPaths);
|
|
373
|
-
const now = Date.now();
|
|
374
|
-
if (existing) {
|
|
375
|
-
const cached = this.collections.get(name);
|
|
376
|
-
if (cached) {
|
|
377
|
-
cached.close();
|
|
378
|
-
this.collections.delete(name);
|
|
379
|
-
}
|
|
380
|
-
const updated = {
|
|
381
|
-
...existing,
|
|
382
|
-
rootPaths: normalizedRootPaths,
|
|
383
|
-
path: normalizePath(path ?? existing.path),
|
|
384
|
-
indexPolicy: "disabled",
|
|
385
|
-
embedding: null,
|
|
386
|
-
indexVersion: null,
|
|
387
|
-
updatedTime: now,
|
|
388
|
-
};
|
|
389
|
-
this.meta.upsert(updated);
|
|
390
|
-
return updated;
|
|
391
|
-
}
|
|
392
|
-
const collectionPath = normalizePath(path ?? join(this.home, "collections", randomUUID()));
|
|
393
|
-
const info = {
|
|
394
|
-
id: randomUUID(),
|
|
395
|
-
name,
|
|
396
|
-
path: collectionPath,
|
|
397
|
-
rootPaths: normalizedRootPaths,
|
|
398
|
-
indexPolicy: "disabled",
|
|
399
|
-
embedding: null,
|
|
400
|
-
indexVersion: null,
|
|
401
|
-
createdTime: now,
|
|
402
|
-
updatedTime: now,
|
|
403
|
-
};
|
|
404
|
-
this.meta.upsert(info);
|
|
405
|
-
return info;
|
|
406
|
-
}
|
|
407
|
-
updateRootPaths(name, rootPaths) {
|
|
408
|
-
if (this.readOnly) {
|
|
409
|
-
throw new EngineError("Cannot update a collection from a read-only registry", {
|
|
410
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.READ_ONLY",
|
|
411
|
-
context: collectionOperationDetails(name, "updateRootPaths"),
|
|
412
|
-
});
|
|
413
|
-
}
|
|
414
|
-
const info = this.get(name);
|
|
415
|
-
if (!info) {
|
|
416
|
-
return null;
|
|
417
|
-
}
|
|
418
|
-
const normalizedRootPaths = validateRootPaths(rootPaths);
|
|
419
|
-
if (rootPathsEqual(info.rootPaths, normalizedRootPaths)) {
|
|
420
|
-
return info;
|
|
421
|
-
}
|
|
422
|
-
const cached = this.collections.get(name);
|
|
423
|
-
if (cached) {
|
|
424
|
-
cached.close();
|
|
425
|
-
this.collections.delete(name);
|
|
426
|
-
}
|
|
427
|
-
const updated = {
|
|
428
|
-
...info,
|
|
429
|
-
rootPaths: normalizedRootPaths,
|
|
430
|
-
updatedTime: Date.now(),
|
|
431
|
-
};
|
|
432
|
-
this.meta.upsert(updated);
|
|
433
|
-
return updated;
|
|
434
|
-
}
|
|
435
|
-
open(name) {
|
|
436
|
-
const cached = this.collections.get(name);
|
|
437
|
-
if (cached) {
|
|
438
|
-
return cached;
|
|
439
|
-
}
|
|
440
|
-
const info = this.get(name);
|
|
441
|
-
if (!info) {
|
|
442
|
-
throw new EngineError("Collection not found", {
|
|
443
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.NOT_FOUND",
|
|
444
|
-
context: errorDetails([collectionDetail(name)]),
|
|
445
|
-
});
|
|
446
|
-
}
|
|
447
|
-
if (info.indexPolicy === "disabled") {
|
|
448
|
-
throw new EngineError("Collection index is disabled", {
|
|
449
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.INDEX_DISABLED",
|
|
450
|
-
context: errorDetails([
|
|
451
|
-
collectionDetail(name),
|
|
452
|
-
detail("hint", "Run zg --index to enable and build this index, or use zg --rg for no-index search."),
|
|
453
|
-
]),
|
|
454
|
-
});
|
|
455
|
-
}
|
|
456
|
-
if (!isCollectionIndexed(info)) {
|
|
457
|
-
throw new EngineError("Collection index has not been built", {
|
|
458
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.INDEX_MISSING",
|
|
459
|
-
context: errorDetails([
|
|
460
|
-
collectionDetail(name),
|
|
461
|
-
detail("hint", "Run zg --index to build this index."),
|
|
462
|
-
]),
|
|
463
|
-
});
|
|
464
|
-
}
|
|
465
|
-
const collection = new Collection(info, this.embeddingModel, this.readOnly, filesMetaPath(this.home));
|
|
466
|
-
this.collections.set(name, collection);
|
|
467
|
-
return collection;
|
|
468
|
-
}
|
|
469
|
-
close() {
|
|
470
|
-
for (const collection of this.collections.values()) {
|
|
471
|
-
collection.close();
|
|
472
|
-
}
|
|
473
|
-
this.collections.clear();
|
|
474
|
-
this.meta.close();
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
class ZvecCollectionsMetaStore {
|
|
478
|
-
path;
|
|
479
|
-
readOnly;
|
|
480
|
-
collection;
|
|
481
|
-
needsOptimize = false;
|
|
482
|
-
constructor(path, readOnly = false) {
|
|
483
|
-
this.path = path;
|
|
484
|
-
this.readOnly = readOnly;
|
|
485
|
-
initializeZvec();
|
|
486
|
-
if (!readOnly) {
|
|
487
|
-
mkdirSync(homeDirectoryForMetaPath(path), { recursive: true });
|
|
488
|
-
}
|
|
489
|
-
if (existsSync(path)) {
|
|
490
|
-
this.collection = openZvecCollection(path, readOnly, "open", () => ZVecOpen(path, { readOnly }));
|
|
491
|
-
}
|
|
492
|
-
else if (readOnly) {
|
|
493
|
-
this.collection = null;
|
|
494
|
-
}
|
|
495
|
-
else {
|
|
496
|
-
this.collection = openZvecCollection(path, readOnly, "create", () => ZVecCreateAndOpen(path, createCollectionsSchema()));
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
list() {
|
|
500
|
-
if (!this.collection) {
|
|
501
|
-
return [];
|
|
502
|
-
}
|
|
503
|
-
const topk = Math.max(this.collection.stats.docCount, 1);
|
|
504
|
-
return this.collection.querySync({ topk, includeVector: false })
|
|
505
|
-
.map((doc) => docToCollectionInfo(doc))
|
|
506
|
-
.sort((left, right) => left.name.localeCompare(right.name));
|
|
507
|
-
}
|
|
508
|
-
getByName(name) {
|
|
509
|
-
if (!this.collection) {
|
|
510
|
-
return null;
|
|
511
|
-
}
|
|
512
|
-
const [doc] = this.collection.querySync({
|
|
513
|
-
filter: `name = ${quoteFilterString(name)}`,
|
|
514
|
-
topk: 1,
|
|
515
|
-
includeVector: false,
|
|
516
|
-
});
|
|
517
|
-
return doc ? docToCollectionInfo(doc) : null;
|
|
518
|
-
}
|
|
519
|
-
upsert(info) {
|
|
520
|
-
this.assertWritable("upsert");
|
|
521
|
-
const collection = this.requireCollection();
|
|
522
|
-
const deleteStatus = collection.deleteSync(info.id);
|
|
523
|
-
assertZvecStatusOrNotFound(deleteStatus, "collection metadata replace", info.name);
|
|
524
|
-
const status = collection.upsertSync(collectionInfoToDoc(info));
|
|
525
|
-
assertZvecStatus(status, "collection metadata upsert", info.name);
|
|
526
|
-
this.needsOptimize = true;
|
|
527
|
-
}
|
|
528
|
-
remove(collectionId) {
|
|
529
|
-
this.assertWritable("remove");
|
|
530
|
-
const status = this.requireCollection().deleteByFilterSync(`collection_id = ${quoteFilterString(collectionId)}`);
|
|
531
|
-
assertZvecStatus(status, "collection metadata delete", collectionId);
|
|
532
|
-
this.needsOptimize = true;
|
|
533
|
-
}
|
|
534
|
-
close() {
|
|
535
|
-
if (!this.collection) {
|
|
536
|
-
return;
|
|
537
|
-
}
|
|
538
|
-
if (this.needsOptimize) {
|
|
539
|
-
this.collection.optimizeSync();
|
|
540
|
-
this.needsOptimize = false;
|
|
541
|
-
}
|
|
542
|
-
this.collection.closeSync();
|
|
543
|
-
}
|
|
544
|
-
requireCollection() {
|
|
545
|
-
if (!this.collection) {
|
|
546
|
-
throw new EngineError("zvec collection metadata storage is unavailable", {
|
|
547
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.META_UNAVAILABLE",
|
|
548
|
-
context: `path=${this.path}`,
|
|
549
|
-
});
|
|
550
|
-
}
|
|
551
|
-
return this.collection;
|
|
552
|
-
}
|
|
553
|
-
assertWritable(operation) {
|
|
554
|
-
if (this.readOnly) {
|
|
555
|
-
throw new EngineError("Cannot update read-only collection metadata storage", {
|
|
556
|
-
code: "ZVEC_GREP.ENGINE.COLLECTION.READ_ONLY",
|
|
557
|
-
context: `path=${this.path} operation=${operation}`,
|
|
558
|
-
});
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
function assertZvecStatusOrNotFound(status, operation, context) {
|
|
563
|
-
if (status.ok || status.code === "ZVEC_NOT_FOUND") {
|
|
564
|
-
return;
|
|
565
|
-
}
|
|
566
|
-
assertZvecStatus(status, operation, context);
|
|
567
|
-
}
|
|
568
|
-
function createCollectionsSchema() {
|
|
569
|
-
return new ZVecCollectionSchema({
|
|
570
|
-
name: "zvec_grep_collections",
|
|
571
|
-
fields: [
|
|
572
|
-
indexedStringField("collection_id"),
|
|
573
|
-
indexedStringField("name"),
|
|
574
|
-
indexedStringField("path"),
|
|
575
|
-
stringField("root_paths_json"),
|
|
576
|
-
indexedStringField("index_policy", true),
|
|
577
|
-
indexedStringField("embedding_provider", true),
|
|
578
|
-
indexedStringField("embedding_model", true),
|
|
579
|
-
{
|
|
580
|
-
name: "embedding_dimension",
|
|
581
|
-
dataType: ZVecDataType.INT32,
|
|
582
|
-
nullable: true,
|
|
583
|
-
},
|
|
584
|
-
indexedStringField("embedding_metric", true),
|
|
585
|
-
{
|
|
586
|
-
name: "index_version",
|
|
587
|
-
dataType: ZVecDataType.INT32,
|
|
588
|
-
nullable: true,
|
|
589
|
-
},
|
|
590
|
-
{
|
|
591
|
-
name: "created_time",
|
|
592
|
-
dataType: ZVecDataType.INT64,
|
|
593
|
-
nullable: false,
|
|
594
|
-
},
|
|
595
|
-
{
|
|
596
|
-
name: "updated_time",
|
|
597
|
-
dataType: ZVecDataType.INT64,
|
|
598
|
-
nullable: false,
|
|
599
|
-
},
|
|
600
|
-
],
|
|
601
|
-
});
|
|
602
|
-
}
|
|
603
|
-
function collectionInfoToDoc(info) {
|
|
604
|
-
const fields = {
|
|
605
|
-
collection_id: info.id,
|
|
606
|
-
name: info.name,
|
|
607
|
-
path: info.path,
|
|
608
|
-
root_paths_json: JSON.stringify(info.rootPaths),
|
|
609
|
-
created_time: info.createdTime,
|
|
610
|
-
updated_time: info.updatedTime,
|
|
611
|
-
};
|
|
612
|
-
if (info.indexPolicy !== undefined) {
|
|
613
|
-
fields.index_policy = info.indexPolicy;
|
|
614
|
-
}
|
|
615
|
-
if (info.embedding) {
|
|
616
|
-
fields.embedding_provider = info.embedding.provider;
|
|
617
|
-
fields.embedding_model = info.embedding.model;
|
|
618
|
-
fields.embedding_dimension = info.embedding.dimension;
|
|
619
|
-
fields.embedding_metric = info.embedding.metric;
|
|
620
|
-
}
|
|
621
|
-
if (info.indexVersion !== null && info.indexVersion !== undefined) {
|
|
622
|
-
fields.index_version = info.indexVersion;
|
|
623
|
-
}
|
|
624
|
-
return {
|
|
625
|
-
id: info.id,
|
|
626
|
-
fields,
|
|
627
|
-
};
|
|
628
|
-
}
|
|
629
|
-
function docToCollectionInfo(doc) {
|
|
630
|
-
const fields = doc.fields;
|
|
631
|
-
const embedding = collectionEmbeddingFromFields(fields);
|
|
632
|
-
return {
|
|
633
|
-
id: readStringFieldFromFields(fields, "collection_id"),
|
|
634
|
-
name: readStringFieldFromFields(fields, "name"),
|
|
635
|
-
path: normalizePath(readStringFieldFromFields(fields, "path")),
|
|
636
|
-
rootPaths: parseRootPaths(readStringFieldFromFields(fields, "root_paths_json")),
|
|
637
|
-
indexPolicy: collectionIndexPolicyFromFields(fields),
|
|
638
|
-
embedding,
|
|
639
|
-
indexVersion: readNullableNumberFieldFromFields(fields, "index_version"),
|
|
640
|
-
createdTime: readNumberFieldFromFields(fields, "created_time"),
|
|
641
|
-
updatedTime: readNumberFieldFromFields(fields, "updated_time"),
|
|
642
|
-
};
|
|
643
|
-
}
|
|
644
|
-
function collectionEmbeddingFromFields(fields) {
|
|
645
|
-
const provider = readNullableStringFieldFromFields(fields, "embedding_provider");
|
|
646
|
-
const model = readNullableStringFieldFromFields(fields, "embedding_model");
|
|
647
|
-
const dimension = readNullableNumberFieldFromFields(fields, "embedding_dimension");
|
|
648
|
-
const metric = readNullableStringFieldFromFields(fields, "embedding_metric");
|
|
649
|
-
if (!provider || !model || !dimension || !metric) {
|
|
650
|
-
return null;
|
|
651
|
-
}
|
|
652
|
-
return {
|
|
653
|
-
provider,
|
|
654
|
-
model,
|
|
655
|
-
dimension,
|
|
656
|
-
metric,
|
|
657
|
-
};
|
|
658
|
-
}
|
|
659
|
-
function collectionIndexPolicyFromFields(fields) {
|
|
660
|
-
const policy = readNullableStringFieldFromFields(fields, "index_policy");
|
|
661
|
-
return policy === "enabled" || policy === "disabled" ? policy : undefined;
|
|
662
|
-
}
|
|
663
|
-
function parseRootPaths(value) {
|
|
664
|
-
try {
|
|
665
|
-
const parsed = JSON.parse(value);
|
|
666
|
-
return Array.isArray(parsed) ? validateRootPaths(parsed) : [];
|
|
667
|
-
}
|
|
668
|
-
catch {
|
|
669
|
-
return [];
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
function collectionsMetaPath(home) {
|
|
673
|
-
return join(home, COLLECTIONS_ZVEC);
|
|
674
|
-
}
|
|
675
|
-
function filesMetaPath(home) {
|
|
676
|
-
return join(home, FILES_ZVEC);
|
|
677
|
-
}
|
|
678
|
-
function homeDirectoryForMetaPath(path) {
|
|
679
|
-
return dirname(path);
|
|
680
|
-
}
|
|
681
|
-
function indexedStringField(name, nullable = false) {
|
|
682
|
-
return {
|
|
683
|
-
name,
|
|
684
|
-
dataType: ZVecDataType.STRING,
|
|
685
|
-
nullable,
|
|
686
|
-
indexParams: { indexType: ZVecIndexType.INVERT },
|
|
687
|
-
};
|
|
688
|
-
}
|
|
689
|
-
function stringField(name, nullable = false) {
|
|
690
|
-
return {
|
|
691
|
-
name,
|
|
692
|
-
dataType: ZVecDataType.STRING,
|
|
693
|
-
nullable,
|
|
694
|
-
};
|
|
695
|
-
}
|
|
696
|
-
export function defaultCollectionName(path) {
|
|
697
|
-
return basename(normalizePath(path));
|
|
698
|
-
}
|
|
699
|
-
function currentEmbeddingSchema(model) {
|
|
700
|
-
return {
|
|
701
|
-
provider: model.ref.provider,
|
|
702
|
-
model: model.ref.model,
|
|
703
|
-
dimension: model.dimension,
|
|
704
|
-
metric: model.metric,
|
|
705
|
-
};
|
|
706
|
-
}
|
|
707
|
-
function rootPathsEqual(left, right) {
|
|
708
|
-
return JSON.stringify(left) === JSON.stringify(right);
|
|
709
|
-
}
|
|
710
|
-
//# sourceMappingURL=index.js.map
|