@h-ai/vecdb 0.1.0-alpha9 → 1.0.0-beta.2

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/dist/index.d.ts CHANGED
@@ -5,49 +5,98 @@ import { HaiResult } from '@h-ai/core';
5
5
  /**
6
6
  * @h-ai/vecdb — 向量数据库配置 Schema
7
7
  *
8
- * 本文件定义向量数据库模块的错误码常量、Zod Schema 和配置类型。
9
- * 支持 LanceDB(默认)、pgvector、Qdrant 三种后端。
8
+ * 本文件定义向量数据库模块的 Zod Schema 和配置类型;错误码见 vecdb-types。
9
+ * 支持 LanceDB(默认)、pgvector、Qdrant、Chroma 四种后端。
10
10
  * @module vecdb-config
11
11
  */
12
12
 
13
13
  /**
14
- * 向量数据库类型枚举
14
+ * 向量数据库类型
15
+ * @example
16
+ * ```ts
17
+ * import type { VecdbType } from '@h-ai/vecdb'
15
18
  *
16
- * 支持的向量数据库类型:
17
- * - `lancedb` — LanceDB 嵌入式向量数据库(默认)
18
- * - `pgvector` — PostgreSQL + pgvector 扩展
19
- * - `qdrant` — Qdrant 向量搜索引擎
19
+ * const value: VecdbType = 'qdrant'
20
+ * void value
21
+ * ```
20
22
  */
21
- declare const VecdbTypeSchema: z.ZodEnum<{
22
- lancedb: "lancedb";
23
- pgvector: "pgvector";
24
- qdrant: "qdrant";
25
- }>;
26
- /** 向量数据库类型 */
27
- type VecdbType = z.infer<typeof VecdbTypeSchema>;
23
+ type VecdbType = 'lancedb' | 'pgvector' | 'qdrant' | 'chroma';
28
24
  /**
29
25
  * 距离度量类型
30
26
  *
31
27
  * - `cosine` — 余弦相似度(默认,适用于文本嵌入)
32
28
  * - `euclidean` — 欧氏距离(L2)
33
29
  * - `dot` — 内积(点积)
30
+ * @example
31
+ * ```ts
32
+ * import { DistanceMetricSchema } from '@h-ai/vecdb'
33
+ *
34
+ * const value = DistanceMetricSchema.parse('cosine')
35
+ * void value
36
+ * ```
34
37
  */
35
38
  declare const DistanceMetricSchema: z.ZodDefault<z.ZodEnum<{
36
39
  cosine: "cosine";
37
40
  euclidean: "euclidean";
38
41
  dot: "dot";
39
42
  }>>;
40
- /** 距离度量类型 */
43
+ /**
44
+ * 距离度量类型
45
+ * @example
46
+ * ```ts
47
+ * import type { DistanceMetric } from '@h-ai/vecdb'
48
+ *
49
+ * const value: DistanceMetric = 'cosine'
50
+ * void value
51
+ * ```
52
+ */
41
53
  type DistanceMetric = z.infer<typeof DistanceMetricSchema>;
42
54
  /**
43
- * LanceDB 配置 Schema
55
+ * 操作日志配置;read/write 默认关闭,长度和级别由 Schema 校验。
56
+ * @example
57
+ * ```ts
58
+ * import { VecdbOperationLogConfigSchema } from '@h-ai/vecdb'
44
59
  *
60
+ * const value = VecdbOperationLogConfigSchema.parse({ read: false, write: true, maxLength: 500, level: 'debug' })
61
+ * void value
62
+ * ```
63
+ */
64
+ declare const VecdbOperationLogConfigSchema: z.ZodObject<{
65
+ read: z.ZodDefault<z.ZodBoolean>;
66
+ write: z.ZodDefault<z.ZodBoolean>;
67
+ maxLength: z.ZodDefault<z.ZodNumber>;
68
+ level: z.ZodDefault<z.ZodEnum<{
69
+ info: "info";
70
+ debug: "debug";
71
+ trace: "trace";
72
+ }>>;
73
+ }, z.core.$strip>;
74
+ /**
75
+ * 已解析的操作日志配置,单位和默认值与 Schema 保持一致。
45
76
  * @example
46
77
  * ```ts
47
- * { type: 'lancedb', path: './data/vecdb' }
78
+ * import { VecdbOperationLogConfigSchema, type VecdbOperationLogConfig } from '@h-ai/vecdb'
79
+ *
80
+ * const value: VecdbOperationLogConfig = VecdbOperationLogConfigSchema.parse({})
81
+ * void value
48
82
  * ```
49
83
  */
84
+ type VecdbOperationLogConfig = z.infer<typeof VecdbOperationLogConfigSchema>;
85
+ /**
86
+ * LanceDB 配置 Schema
87
+ *
88
+ */
50
89
  declare const LancedbConfigSchema: z.ZodObject<{
90
+ operationLog: z.ZodOptional<z.ZodObject<{
91
+ read: z.ZodDefault<z.ZodBoolean>;
92
+ write: z.ZodDefault<z.ZodBoolean>;
93
+ maxLength: z.ZodDefault<z.ZodNumber>;
94
+ level: z.ZodDefault<z.ZodEnum<{
95
+ info: "info";
96
+ debug: "debug";
97
+ trace: "trace";
98
+ }>>;
99
+ }, z.core.$strip>>;
51
100
  type: z.ZodLiteral<"lancedb">;
52
101
  path: z.ZodString;
53
102
  metric: z.ZodOptional<z.ZodDefault<z.ZodEnum<{
@@ -56,38 +105,35 @@ declare const LancedbConfigSchema: z.ZodObject<{
56
105
  dot: "dot";
57
106
  }>>>;
58
107
  }, z.core.$strip>;
59
- /** LanceDB 配置类型 */
60
- type LancedbConfig = z.infer<typeof LancedbConfigSchema>;
61
108
  /**
62
- * pgvector 索引类型
109
+ * LanceDB 配置类型
110
+ * @example
111
+ * ```ts
112
+ * import { VecdbConfigSchema, type LancedbConfig } from '@h-ai/vecdb'
63
113
  *
64
- * - `ivfflat` — IVFFlat 索引(适合中等规模数据)
65
- * - `hnsw` — HNSW 索引(适合大规模数据,检索速度更快)
114
+ * const parsed = VecdbConfigSchema.parse({ type: 'lancedb', path: './data/vecdb' })
115
+ * if (parsed.type === 'lancedb') {
116
+ * const config: LancedbConfig = parsed
117
+ * void config
118
+ * }
119
+ * ```
66
120
  */
67
- declare const PgvectorIndexTypeSchema: z.ZodDefault<z.ZodEnum<{
68
- ivfflat: "ivfflat";
69
- hnsw: "hnsw";
70
- }>>;
121
+ type LancedbConfig = z.infer<typeof LancedbConfigSchema>;
71
122
  /**
72
123
  * pgvector 配置 Schema
73
124
  *
74
- * @example
75
- * ```ts
76
- * // 使用连接字符串
77
- * { type: 'pgvector', url: 'postgres://user:pass@localhost:5432/mydb' }
78
- *
79
- * // 使用分开的字段
80
- * {
81
- * type: 'pgvector',
82
- * host: 'localhost',
83
- * port: 5432,
84
- * database: 'mydb',
85
- * user: 'admin',
86
- * password: 'secret'
87
- * }
88
- * ```
89
125
  */
90
126
  declare const PgvectorConfigSchema: z.ZodObject<{
127
+ operationLog: z.ZodOptional<z.ZodObject<{
128
+ read: z.ZodDefault<z.ZodBoolean>;
129
+ write: z.ZodDefault<z.ZodBoolean>;
130
+ maxLength: z.ZodDefault<z.ZodNumber>;
131
+ level: z.ZodDefault<z.ZodEnum<{
132
+ info: "info";
133
+ debug: "debug";
134
+ trace: "trace";
135
+ }>>;
136
+ }, z.core.$strip>>;
91
137
  type: z.ZodLiteral<"pgvector">;
92
138
  url: z.ZodOptional<z.ZodString>;
93
139
  host: z.ZodDefault<z.ZodString>;
@@ -106,17 +152,35 @@ declare const PgvectorConfigSchema: z.ZodObject<{
106
152
  }>>>;
107
153
  tablePrefix: z.ZodDefault<z.ZodString>;
108
154
  }, z.core.$strip>;
109
- /** pgvector 配置类型 */
110
- type PgvectorConfig = z.infer<typeof PgvectorConfigSchema>;
111
155
  /**
112
- * Qdrant 配置 Schema
113
- *
156
+ * pgvector 配置类型
114
157
  * @example
115
158
  * ```ts
116
- * { type: 'qdrant', url: 'http://localhost:6333' }
159
+ * import { VecdbConfigSchema, type PgvectorConfig } from '@h-ai/vecdb'
160
+ *
161
+ * const parsed = VecdbConfigSchema.parse({ type: 'pgvector', host: 'db', database: 'shop' })
162
+ * if (parsed.type === 'pgvector') {
163
+ * const config: PgvectorConfig = parsed
164
+ * void config
165
+ * }
117
166
  * ```
118
167
  */
168
+ type PgvectorConfig = z.infer<typeof PgvectorConfigSchema>;
169
+ /**
170
+ * Qdrant 配置 Schema
171
+ *
172
+ */
119
173
  declare const QdrantConfigSchema: z.ZodObject<{
174
+ operationLog: z.ZodOptional<z.ZodObject<{
175
+ read: z.ZodDefault<z.ZodBoolean>;
176
+ write: z.ZodDefault<z.ZodBoolean>;
177
+ maxLength: z.ZodDefault<z.ZodNumber>;
178
+ level: z.ZodDefault<z.ZodEnum<{
179
+ info: "info";
180
+ debug: "debug";
181
+ trace: "trace";
182
+ }>>;
183
+ }, z.core.$strip>>;
120
184
  type: z.ZodLiteral<"qdrant">;
121
185
  url: z.ZodDefault<z.ZodString>;
122
186
  apiKey: z.ZodOptional<z.ZodString>;
@@ -126,14 +190,91 @@ declare const QdrantConfigSchema: z.ZodObject<{
126
190
  dot: "dot";
127
191
  }>>>;
128
192
  }, z.core.$strip>;
129
- /** Qdrant 配置类型 */
193
+ /**
194
+ * Qdrant 配置类型
195
+ * @example
196
+ * ```ts
197
+ * import { VecdbConfigSchema, type QdrantConfig } from '@h-ai/vecdb'
198
+ *
199
+ * const parsed = VecdbConfigSchema.parse({ type: 'qdrant', url: 'http://localhost:6333' })
200
+ * if (parsed.type === 'qdrant') {
201
+ * const config: QdrantConfig = parsed
202
+ * void config
203
+ * }
204
+ * ```
205
+ */
130
206
  type QdrantConfig = z.infer<typeof QdrantConfigSchema>;
207
+ /**
208
+ * Chroma 配置 Schema
209
+ *
210
+ * Chroma 在 Node 端只有 HTTP 客户端(无进程内嵌入式)。本 Provider 支持两种模式:
211
+ * - **嵌入式**:提供 `path` 且不提供 `url` 时,init 自动拉起本地 `chroma run` 服务
212
+ * (持久化到 `path`),close 时关闭;数据落地本地目录,无需手动启动服务。
213
+ * - **直连**:提供 `url`(或已运行的 host:port)时,直接连接已有 Chroma 服务,不拉起进程。
214
+ *
215
+ */
216
+ declare const ChromaConfigSchema: z.ZodObject<{
217
+ operationLog: z.ZodOptional<z.ZodObject<{
218
+ read: z.ZodDefault<z.ZodBoolean>;
219
+ write: z.ZodDefault<z.ZodBoolean>;
220
+ maxLength: z.ZodDefault<z.ZodNumber>;
221
+ level: z.ZodDefault<z.ZodEnum<{
222
+ info: "info";
223
+ debug: "debug";
224
+ trace: "trace";
225
+ }>>;
226
+ }, z.core.$strip>>;
227
+ type: z.ZodLiteral<"chroma">;
228
+ path: z.ZodOptional<z.ZodString>;
229
+ url: z.ZodOptional<z.ZodString>;
230
+ host: z.ZodDefault<z.ZodString>;
231
+ port: z.ZodDefault<z.ZodNumber>;
232
+ apiKey: z.ZodOptional<z.ZodString>;
233
+ serverCommand: z.ZodDefault<z.ZodString>;
234
+ startupTimeout: z.ZodDefault<z.ZodNumber>;
235
+ metric: z.ZodOptional<z.ZodDefault<z.ZodEnum<{
236
+ cosine: "cosine";
237
+ euclidean: "euclidean";
238
+ dot: "dot";
239
+ }>>>;
240
+ }, z.core.$strip>;
241
+ /**
242
+ * Chroma 配置类型
243
+ * @example
244
+ * ```ts
245
+ * import { VecdbConfigSchema, type ChromaConfig } from '@h-ai/vecdb'
246
+ *
247
+ * const parsed = VecdbConfigSchema.parse({ type: 'chroma', url: 'http://localhost:8000' })
248
+ * if (parsed.type === 'chroma') {
249
+ * const config: ChromaConfig = parsed
250
+ * void config
251
+ * }
252
+ * ```
253
+ */
254
+ type ChromaConfig = z.infer<typeof ChromaConfigSchema>;
131
255
  /**
132
256
  * 统一向量数据库配置 Schema(判别联合体)
133
257
  *
134
258
  * 根据 `type` 字段区分不同向量数据库类型的配置。
259
+ * @example
260
+ * ```ts
261
+ * import { VecdbConfigSchema } from '@h-ai/vecdb'
262
+ *
263
+ * const value = VecdbConfigSchema.parse({ type: 'qdrant', url: 'http://localhost:6333' })
264
+ * void value
265
+ * ```
135
266
  */
136
267
  declare const VecdbConfigSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
268
+ operationLog: z.ZodOptional<z.ZodObject<{
269
+ read: z.ZodDefault<z.ZodBoolean>;
270
+ write: z.ZodDefault<z.ZodBoolean>;
271
+ maxLength: z.ZodDefault<z.ZodNumber>;
272
+ level: z.ZodDefault<z.ZodEnum<{
273
+ info: "info";
274
+ debug: "debug";
275
+ trace: "trace";
276
+ }>>;
277
+ }, z.core.$strip>>;
137
278
  type: z.ZodLiteral<"lancedb">;
138
279
  path: z.ZodString;
139
280
  metric: z.ZodOptional<z.ZodDefault<z.ZodEnum<{
@@ -142,6 +283,16 @@ declare const VecdbConfigSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
142
283
  dot: "dot";
143
284
  }>>>;
144
285
  }, z.core.$strip>, z.ZodObject<{
286
+ operationLog: z.ZodOptional<z.ZodObject<{
287
+ read: z.ZodDefault<z.ZodBoolean>;
288
+ write: z.ZodDefault<z.ZodBoolean>;
289
+ maxLength: z.ZodDefault<z.ZodNumber>;
290
+ level: z.ZodDefault<z.ZodEnum<{
291
+ info: "info";
292
+ debug: "debug";
293
+ trace: "trace";
294
+ }>>;
295
+ }, z.core.$strip>>;
145
296
  type: z.ZodLiteral<"pgvector">;
146
297
  url: z.ZodOptional<z.ZodString>;
147
298
  host: z.ZodDefault<z.ZodString>;
@@ -160,6 +311,16 @@ declare const VecdbConfigSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
160
311
  }>>>;
161
312
  tablePrefix: z.ZodDefault<z.ZodString>;
162
313
  }, z.core.$strip>, z.ZodObject<{
314
+ operationLog: z.ZodOptional<z.ZodObject<{
315
+ read: z.ZodDefault<z.ZodBoolean>;
316
+ write: z.ZodDefault<z.ZodBoolean>;
317
+ maxLength: z.ZodDefault<z.ZodNumber>;
318
+ level: z.ZodDefault<z.ZodEnum<{
319
+ info: "info";
320
+ debug: "debug";
321
+ trace: "trace";
322
+ }>>;
323
+ }, z.core.$strip>>;
163
324
  type: z.ZodLiteral<"qdrant">;
164
325
  url: z.ZodDefault<z.ZodString>;
165
326
  apiKey: z.ZodOptional<z.ZodString>;
@@ -168,19 +329,68 @@ declare const VecdbConfigSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
168
329
  euclidean: "euclidean";
169
330
  dot: "dot";
170
331
  }>>>;
332
+ }, z.core.$strip>, z.ZodObject<{
333
+ operationLog: z.ZodOptional<z.ZodObject<{
334
+ read: z.ZodDefault<z.ZodBoolean>;
335
+ write: z.ZodDefault<z.ZodBoolean>;
336
+ maxLength: z.ZodDefault<z.ZodNumber>;
337
+ level: z.ZodDefault<z.ZodEnum<{
338
+ info: "info";
339
+ debug: "debug";
340
+ trace: "trace";
341
+ }>>;
342
+ }, z.core.$strip>>;
343
+ type: z.ZodLiteral<"chroma">;
344
+ path: z.ZodOptional<z.ZodString>;
345
+ url: z.ZodOptional<z.ZodString>;
346
+ host: z.ZodDefault<z.ZodString>;
347
+ port: z.ZodDefault<z.ZodNumber>;
348
+ apiKey: z.ZodOptional<z.ZodString>;
349
+ serverCommand: z.ZodDefault<z.ZodString>;
350
+ startupTimeout: z.ZodDefault<z.ZodNumber>;
351
+ metric: z.ZodOptional<z.ZodDefault<z.ZodEnum<{
352
+ cosine: "cosine";
353
+ euclidean: "euclidean";
354
+ dot: "dot";
355
+ }>>>;
171
356
  }, z.core.$strip>], "type">;
172
- /** 向量数据库配置类型(判别联合体) */
357
+ /**
358
+ * 向量数据库配置类型(判别联合体)
359
+ * @example
360
+ * ```ts
361
+ * import { VecdbConfigSchema, type VecdbConfig } from '@h-ai/vecdb'
362
+ *
363
+ * const parsed = VecdbConfigSchema.parse({ type: 'qdrant', url: 'http://localhost:6333' })
364
+ * const config: VecdbConfig = parsed
365
+ * void config
366
+ * ```
367
+ */
173
368
  type VecdbConfig = z.infer<typeof VecdbConfigSchema>;
174
369
  /**
175
370
  * 向量数据库配置输入类型(用于 init 等入口)
176
371
  *
177
372
  * 说明:Zod 的 default 会让输入端字段可省略,但输出端字段为必填。
178
373
  * 因此对外 API(如 vecdb.init)更适合接收 VecdbConfigInput。
374
+ * @example
375
+ * ```ts
376
+ * import type { VecdbConfigInput } from '@h-ai/vecdb'
377
+ *
378
+ * const value: VecdbConfigInput = { type: 'qdrant', url: 'http://localhost:6333' }
379
+ * void value
380
+ * ```
179
381
  */
180
382
  type VecdbConfigInput = z.input<typeof VecdbConfigSchema>;
181
383
 
182
384
  /**
183
385
  * Vecdb 模块标准错误定义对象。
386
+ * @example
387
+ * ```ts
388
+ * import { HaiVecdbError } from '@h-ai/vecdb'
389
+ * import { err } from '@h-ai/core'
390
+ *
391
+ * const result = err(HaiVecdbError.COLLECTION_NOT_FOUND, 'Collection missing')
392
+ * if (!result.success) void result.error.code
393
+ * ```
184
394
  */
185
395
  declare const HaiVecdbError: {
186
396
  readonly CONNECTION_FAILED: _h_ai_core.HaiErrorDef;
@@ -202,15 +412,12 @@ declare const HaiVecdbError: {
202
412
  * 向量文档接口
203
413
  *
204
414
  * 表示一条存入向量数据库的记录,包含向量、文本内容和元数据。
205
- *
206
415
  * @example
207
416
  * ```ts
208
- * const doc: VectorDocument = {
209
- * id: 'doc-001',
210
- * vector: [0.1, 0.2, 0.3, ...],
211
- * content: '这是文档内容',
212
- * metadata: { source: 'wiki', page: 1 },
213
- * }
417
+ * import type { VectorDocument } from '@h-ai/vecdb'
418
+ *
419
+ * const value: VectorDocument = { id: 'doc_1', vector: [0.1, 0.2, 0.3], content: 'Example text', metadata: { tenant: 'shop' } }
420
+ * void value
214
421
  * ```
215
422
  */
216
423
  interface VectorDocument {
@@ -225,20 +432,24 @@ interface VectorDocument {
225
432
  }
226
433
  /**
227
434
  * 向量搜索选项
228
- *
229
435
  * @example
230
436
  * ```ts
231
- * const options: VectorSearchOptions = {
232
- * topK: 10,
233
- * filter: { source: 'wiki' },
234
- * minScore: 0.7,
235
- * }
437
+ * import type { VectorSearchOptions } from '@h-ai/vecdb'
438
+ *
439
+ * const value: VectorSearchOptions = { topK: 5, filter: { tenant: 'shop' }, minScore: 0.7 }
440
+ * void value
236
441
  * ```
237
442
  */
238
443
  interface VectorSearchOptions {
239
444
  /** 返回的最大结果数(默认 10) */
240
445
  topK?: number;
241
- /** 元数据过滤条件(键值对,精确匹配) */
446
+ /**
447
+ * 元数据过滤,具体能力以 Provider 为准。
448
+ * LanceDB:顶层字面量键的 AND 精确匹配,仅支持 string、有限 number、boolean、null;
449
+ * 类型严格区分,null 不匹配缺失字段;空对象不筛选,不支持对象/数组/undefined。
450
+ * 条件在 topK 前执行,不支持的值返回 CONFIG_ERROR。其它后端不保证相同 null/复杂值语义。
451
+ * @example { tenant: 'acme', active: true, page: 1, removedAt: null }
452
+ */
242
453
  filter?: Record<string, unknown>;
243
454
  /** 最低相似度阈值(0-1,低于此值的结果将被过滤) */
244
455
  minScore?: number;
@@ -247,15 +458,12 @@ interface VectorSearchOptions {
247
458
  * 向量搜索结果
248
459
  *
249
460
  * 包含匹配的文档和相似度得分。
250
- *
251
461
  * @example
252
462
  * ```ts
253
- * const result: VectorSearchResult = {
254
- * id: 'doc-001',
255
- * score: 0.95,
256
- * content: '匹配的文档内容',
257
- * metadata: { source: 'wiki' },
258
- * }
463
+ * import type { VectorSearchResult } from '@h-ai/vecdb'
464
+ *
465
+ * const value: VectorSearchResult = { id: 'doc_1', score: 0.95, content: 'Example text', metadata: { tenant: 'shop' } }
466
+ * void value
259
467
  * ```
260
468
  */
261
469
  interface VectorSearchResult {
@@ -274,15 +482,12 @@ interface VectorSearchResult {
274
482
  * 集合信息接口
275
483
  *
276
484
  * 描述一个向量集合的元信息。
277
- *
278
485
  * @example
279
486
  * ```ts
280
- * const info: CollectionInfo = {
281
- * name: 'docs',
282
- * dimension: 1536,
283
- * metric: 'cosine',
284
- * count: 42,
285
- * }
487
+ * import type { CollectionInfo } from '@h-ai/vecdb'
488
+ *
489
+ * const value: CollectionInfo = { name: 'docs', dimension: 3, metric: 'cosine', count: 1 }
490
+ * void value
286
491
  * ```
287
492
  */
288
493
  interface CollectionInfo {
@@ -299,14 +504,15 @@ interface CollectionInfo {
299
504
  * 集合管理操作接口
300
505
  *
301
506
  * 通过 `vecdb.collection` 访问,管理向量集合的创建、删除和查询。
302
- *
303
507
  * @example
304
508
  * ```ts
305
- * // 创建集合
306
- * await vecdb.collection.create('my-docs', { dimension: 1536 })
509
+ * import type { CollectionOperations } from '@h-ai/vecdb'
307
510
  *
308
- * // 列出集合
309
- * const result = await vecdb.collection.list()
511
+ * async function read(operations: CollectionOperations) {
512
+ * const result = await operations.list()
513
+ * if (!result.success) return result.error.message
514
+ * return result.data
515
+ * }
310
516
  * ```
311
517
  */
312
518
  interface CollectionOperations {
@@ -348,13 +554,12 @@ interface CollectionOperations {
348
554
  }
349
555
  /**
350
556
  * 集合创建选项
351
- *
352
557
  * @example
353
558
  * ```ts
354
- * const options: CollectionCreateOptions = {
355
- * dimension: 1536,
356
- * metric: 'cosine',
357
- * }
559
+ * import type { CollectionCreateOptions } from '@h-ai/vecdb'
560
+ *
561
+ * const value: CollectionCreateOptions = { dimension: 3, metric: 'cosine' }
562
+ * void value
358
563
  * ```
359
564
  */
360
565
  interface CollectionCreateOptions {
@@ -367,17 +572,15 @@ interface CollectionCreateOptions {
367
572
  * 向量操作接口
368
573
  *
369
574
  * 通过 `vecdb.vector` 访问,管理向量文档的增删改查和搜索。
370
- *
371
575
  * @example
372
576
  * ```ts
373
- * // 插入向量
374
- * await vecdb.vector.insert('my-docs', [
375
- * { id: 'doc-1', vector: [...], content: '文档1' },
376
- * { id: 'doc-2', vector: [...], content: '文档2' },
377
- * ])
378
- *
379
- * // 搜索向量
380
- * const result = await vecdb.vector.search('my-docs', queryVector, { topK: 5 })
577
+ * import type { VectorOperations } from '@h-ai/vecdb'
578
+ *
579
+ * async function read(operations: VectorOperations) {
580
+ * const result = await operations.search('docs', [0.1, 0.2, 0.3], { topK: 5 })
581
+ * if (!result.success) return result.error.message
582
+ * return result.data
583
+ * }
381
584
  * ```
382
585
  */
383
586
  interface VectorOperations {
@@ -386,15 +589,19 @@ interface VectorOperations {
386
589
  *
387
590
  * @param collection - 集合名称
388
591
  * @param documents - 文档列表
389
- * @returns 成功返回 ok(undefined)
592
+ * @returns 成功返回 ok(undefined);LanceDB 已有 ID 保留不覆盖,同批重复 ID 返回 CONFIG_ERROR。
593
+ * Qdrant 成功前等待服务端应用写入。
594
+ * @example await vecdb.vector.insert('docs', [{ id: 'doc-1', vector: [1, 0] }])
390
595
  */
391
596
  insert: (collection: string, documents: VectorDocument[]) => Promise<HaiResult<void>>;
392
597
  /**
393
- * 更新向量文档(按 id 匹配,整体替换)
598
+ * 插入或更新向量文档(按 id 匹配,整体替换)
394
599
  *
395
600
  * @param collection - 集合名称
396
- * @param documents - 待更新的文档(id 必须已存在)
397
- * @returns 成功返回 ok(undefined)
601
+ * @param documents - 待写入文档;已有 ID 更新,不存在的 ID 插入
602
+ * @returns 成功返回 ok(undefined);LanceDB 缺少原子主键能力或同批重复 ID 返回 CONFIG_ERROR。
603
+ * Qdrant 成功前等待服务端应用写入。
604
+ * @example await vecdb.vector.upsert('docs', [{ id: 'doc-1', vector: [1, 0], content: 'updated' }])
398
605
  */
399
606
  upsert: (collection: string, documents: VectorDocument[]) => Promise<HaiResult<void>>;
400
607
  /**
@@ -403,6 +610,7 @@ interface VectorOperations {
403
610
  * @param collection - 集合名称
404
611
  * @param ids - 文档 ID 列表
405
612
  * @returns 成功返回 ok(undefined)
613
+ * Qdrant 成功前等待服务端应用删除。
406
614
  */
407
615
  delete: (collection: string, ids: string[]) => Promise<HaiResult<void>>;
408
616
  /**
@@ -412,6 +620,7 @@ interface VectorOperations {
412
620
  * @param vector - 查询向量
413
621
  * @param options - 搜索选项
414
622
  * @returns 搜索结果列表(按相似度降序排列)
623
+ * @example await vecdb.vector.search('docs', [1, 0], { topK: 5, filter: { tenant: 'acme', active: true } })
415
624
  */
416
625
  search: (collection: string, vector: number[], options?: VectorSearchOptions) => Promise<HaiResult<VectorSearchResult[]>>;
417
626
  /**
@@ -419,30 +628,26 @@ interface VectorOperations {
419
628
  *
420
629
  * @param collection - 集合名称
421
630
  * @returns 文档数量
631
+ * Qdrant 使用精确 count,不依赖集合信息的近似统计。
422
632
  */
423
633
  count: (collection: string) => Promise<HaiResult<number>>;
424
634
  }
425
635
  /**
426
636
  * 向量数据库服务接口(通过 `vecdb` 对象访问)
427
- *
428
637
  * @example
429
638
  * ```ts
430
639
  * import { vecdb } from '@h-ai/vecdb'
431
640
  *
432
- * // 初始化(LanceDB)
433
- * await vecdb.init({ type: 'lancedb', path: './data/vecdb' })
434
- *
435
- * // 创建集合
436
- * await vecdb.collection.create('docs', { dimension: 1536 })
437
- *
438
- * // 插入向量
439
- * await vecdb.vector.insert('docs', [{ id: '1', vector: [...], content: '...' }])
440
- *
441
- * // 搜索
442
- * const result = await vecdb.vector.search('docs', queryVector, { topK: 5 })
443
- *
444
- * // 关闭
445
- * await vecdb.close()
641
+ * const initialized = await vecdb.init({ type: 'qdrant', url: 'http://localhost:6333' })
642
+ * if (!initialized.success) throw new Error(initialized.error.message)
643
+ * try {
644
+ * const result = await vecdb.collection.list()
645
+ * if (!result.success) throw new Error(result.error.message)
646
+ * void result.data
647
+ * } finally {
648
+ * const closed = await vecdb.close()
649
+ * if (!closed.success) throw new Error(closed.error.message)
650
+ * }
446
651
  * ```
447
652
  */
448
653
  interface VecdbFunctions {
@@ -450,8 +655,15 @@ interface VecdbFunctions {
450
655
  init: (config: VecdbConfigInput) => Promise<HaiResult<void>>;
451
656
  /** 关闭连接 */
452
657
  close: () => Promise<HaiResult<void>>;
453
- /** 当前配置(未初始化时为 null) */
658
+ /** 当前脱敏配置快照(未初始化时为 null) */
454
659
  readonly config: VecdbConfig | null;
660
+ /**
661
+ * 获取底层后端的原始连接信息(含凭证,未初始化时为 null)
662
+ *
663
+ * 供需要直连同一向量库的外部引擎(如 mem0 OSS)复用后端。返回值包含未脱敏的凭证,
664
+ * 仅供同进程内集成使用,禁止写入日志或返回给外部调用方。
665
+ */
666
+ readonly rawConnection: VecdbConnection | null;
455
667
  /** 是否已初始化 */
456
668
  readonly isInitialized: boolean;
457
669
  /** 集合管理操作 */
@@ -459,6 +671,40 @@ interface VecdbFunctions {
459
671
  /** 向量操作 */
460
672
  readonly vector: VectorOperations;
461
673
  }
674
+ /**
675
+ * 向量库原始连接信息
676
+ *
677
+ * 由 `vecdb.rawConnection` 暴露,字段按后端类型填充。包含凭证,仅供同进程内集成使用。
678
+ * @example
679
+ * ```ts
680
+ * import type { VecdbConnection } from '@h-ai/vecdb'
681
+ *
682
+ * const value: VecdbConnection = { type: 'qdrant', url: 'http://localhost:6333' }
683
+ * void value
684
+ * ```
685
+ */
686
+ interface VecdbConnection {
687
+ /** 后端类型 */
688
+ type: VecdbConfig['type'];
689
+ /** 服务 URL(qdrant、chroma;Chroma 内嵌模式含实际本地端口) */
690
+ url?: string;
691
+ /** API Key / Bearer token(qdrant、chroma) */
692
+ apiKey?: string;
693
+ /** 主机(pgvector、chroma) */
694
+ host?: string;
695
+ /** 端口(pgvector、chroma) */
696
+ port?: number;
697
+ /** 数据库名(pgvector) */
698
+ database?: string;
699
+ /** 用户名(pgvector) */
700
+ user?: string;
701
+ /** 密码(pgvector) */
702
+ password?: string;
703
+ /** 连接串(pgvector) */
704
+ connectionString?: string;
705
+ /** 本地持久化路径(lancedb 或内嵌 chroma) */
706
+ path?: string;
707
+ }
462
708
 
463
709
  /**
464
710
  * @h-ai/vecdb — 向量数据库服务主入口
@@ -477,29 +723,22 @@ interface VecdbFunctions {
477
723
  * - `vecdb.vector` - 向量操作(插入/搜索/删除)
478
724
  * - `vecdb.config` - 当前配置
479
725
  * - `vecdb.isInitialized` - 初始化状态
480
- *
481
726
  * @example
482
727
  * ```ts
483
728
  * import { vecdb } from '@h-ai/vecdb'
484
729
  *
485
- * // 初始化(LanceDB)
486
- * await vecdb.init({ type: 'lancedb', path: './data/vecdb' })
487
- *
488
- * // 集合操作
489
- * await vecdb.collection.create('docs', { dimension: 1536 })
490
- *
491
- * // 向量操作
492
- * await vecdb.vector.insert('docs', [
493
- * { id: '1', vector: [...], content: '文档内容' },
494
- * ])
495
- *
496
- * // 搜索
497
- * const result = await vecdb.vector.search('docs', queryVector, { topK: 5 })
498
- *
499
- * // 关闭
500
- * await vecdb.close()
730
+ * const initialized = await vecdb.init({ type: 'qdrant', url: 'http://localhost:6333' })
731
+ * if (!initialized.success) throw new Error(initialized.error.message)
732
+ * try {
733
+ * const result = await vecdb.collection.list()
734
+ * if (!result.success) throw new Error(result.error.message)
735
+ * void result.data
736
+ * } finally {
737
+ * const closed = await vecdb.close()
738
+ * if (!closed.success) throw new Error(closed.error.message)
739
+ * }
501
740
  * ```
502
741
  */
503
742
  declare const vecdb: VecdbFunctions;
504
743
 
505
- export { type CollectionCreateOptions, type CollectionInfo, type CollectionOperations, type DistanceMetric, DistanceMetricSchema, HaiVecdbError, type LancedbConfig, LancedbConfigSchema, type PgvectorConfig, PgvectorConfigSchema, PgvectorIndexTypeSchema, type QdrantConfig, QdrantConfigSchema, type VecdbConfig, type VecdbConfigInput, VecdbConfigSchema, type VecdbFunctions, type VecdbType, VecdbTypeSchema, type VectorDocument, type VectorOperations, type VectorSearchOptions, type VectorSearchResult, vecdb };
744
+ export { type ChromaConfig, type CollectionCreateOptions, type CollectionInfo, type CollectionOperations, type DistanceMetric, DistanceMetricSchema, HaiVecdbError, type LancedbConfig, type PgvectorConfig, type QdrantConfig, type VecdbConfig, type VecdbConfigInput, VecdbConfigSchema, type VecdbConnection, type VecdbFunctions, type VecdbOperationLogConfig, VecdbOperationLogConfigSchema, type VecdbType, type VectorDocument, type VectorOperations, type VectorSearchOptions, type VectorSearchResult, vecdb };