@huyooo/ai-search 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,508 @@
1
+ import { I as IndexProgress, F as FileType, a as IndexedDocument, b as IndexStats } from './index-B6UR8lRu.js';
2
+ export { f as BackupInfo, B as BatchOperationResult, C as ChunkOptions, h as DEFAULT_CONFIG, D as DocumentSearch, E as ExportInfo, H as HealthCheckResult, g as IndexError, j as IndexingPipeline, P as PipelineConfig, k as PipelineStats, S as SearchConfig, c as SearchOptions, q as SearchPluginInstance, p as SearchPluginOptions, d as SearchResult, l as SkipCheckCallback, T as TextChunk, e as WatchEvent, W as WatchOptions, i as createIndexingPipeline, m as getChunkStats, o as getSearchPlugin, n as searchPlugin, s as splitText } from './index-B6UR8lRu.js';
3
+ export { SearchElectronBridge, SearchElectronBridgeOptions, createSearchElectronBridge } from './bridge/electron.js';
4
+ export { Tool, ToolContext, ToolPlugin } from '@huyooo/ai-chat-core';
5
+ import 'electron';
6
+
7
+ /**
8
+ * 全局进度监听器
9
+ *
10
+ * 所有 DocumentSearch.indexDirectory 调用都会触发这些监听器
11
+ * 用于 Electron 桥接等场景,无需包装方法,解决时序问题
12
+ *
13
+ * 使用 globalThis 确保在 monorepo 多包场景下只有一个实例
14
+ */
15
+
16
+ /** 全局进度监听器类型 */
17
+ type GlobalProgressListener = (progress: IndexProgress) => void;
18
+ /** 添加全局进度监听器 */
19
+ declare function addGlobalProgressListener(listener: GlobalProgressListener): void;
20
+ /** 移除全局进度监听器 */
21
+ declare function removeGlobalProgressListener(listener: GlobalProgressListener): void;
22
+
23
+ /**
24
+ * 文件扩展名规则
25
+ */
26
+ interface ExtensionRules {
27
+ /** 支持的文档扩展名列表 */
28
+ allowed: string[];
29
+ /** 排除的扩展名列表(即使在其他地方允许) */
30
+ excluded: string[];
31
+ }
32
+ /**
33
+ * 目录排除规则
34
+ */
35
+ interface DirectoryRules {
36
+ /** 排除的目录名(不区分大小写) */
37
+ excludedNames: string[];
38
+ /** 排除的目录路径(完整路径或相对路径) */
39
+ excludedPaths: string[];
40
+ /** Windows 系统目录 */
41
+ windowsSystemDirs: string[];
42
+ /** 构建和缓存目录 */
43
+ buildDirs: string[];
44
+ /** 资源目录(通常包含图标等,不是文档) */
45
+ resourceDirs: string[];
46
+ }
47
+ /**
48
+ * 文件排除规则
49
+ */
50
+ interface FileRules {
51
+ /** 是否排除隐藏文件(以 . 开头) */
52
+ excludeHidden: boolean;
53
+ /** 排除的文件名模式(支持通配符) */
54
+ excludedPatterns: string[];
55
+ /** 排除的路径包含模式(路径中包含这些字符串的文件将被排除) */
56
+ excludedPathContains: string[];
57
+ }
58
+ /**
59
+ * 路径匹配规则
60
+ */
61
+ interface PathRules {
62
+ /** Windows 平台特殊规则 */
63
+ windows: {
64
+ /** 是否只扫描用户目录 */
65
+ onlyUserDirs: boolean;
66
+ /** 排除的系统盘根目录下的目录 */
67
+ excludedRootDirs: string[];
68
+ };
69
+ }
70
+ /**
71
+ * 扫描规则配置
72
+ */
73
+ interface ScanRules {
74
+ extensions: ExtensionRules;
75
+ directories: DirectoryRules;
76
+ files: FileRules;
77
+ paths: PathRules;
78
+ }
79
+ /**
80
+ * 默认文件扩展名规则
81
+ */
82
+ declare const DEFAULT_EXTENSION_RULES: ExtensionRules;
83
+ /**
84
+ * 默认目录排除规则
85
+ * 注意:所有以 . 开头的隐藏目录会自动被排除(在 shouldExcludeDirectory 中处理)
86
+ * 这里只列出非隐藏的目录名
87
+ */
88
+ declare const DEFAULT_DIRECTORY_RULES: DirectoryRules;
89
+ /**
90
+ * 默认文件排除规则
91
+ * 注意:所有以 . 开头的隐藏文件会自动被排除(在 shouldExcludeFile 中处理)
92
+ */
93
+ declare const DEFAULT_FILE_RULES: FileRules;
94
+ /**
95
+ * 默认路径规则
96
+ */
97
+ declare const DEFAULT_PATH_RULES: PathRules;
98
+ /**
99
+ * 默认扫描规则
100
+ */
101
+ declare const DEFAULT_SCAN_RULES: ScanRules;
102
+ /**
103
+ * 规则管理器类
104
+ * 提供统一的规则检查和匹配接口
105
+ */
106
+ declare class ScanRulesManager {
107
+ private rules;
108
+ private excludedDirNamesSet;
109
+ private allowedExtensionsSet;
110
+ private excludedExtensionsSet;
111
+ constructor(rules?: ScanRules);
112
+ /**
113
+ * 检查文件扩展名是否允许
114
+ */
115
+ isExtensionAllowed(ext: string): boolean;
116
+ /**
117
+ * 检查目录是否应该被排除(用于 fdir 的 exclude 方法)
118
+ * 这是性能优化的关键:在进入目录之前就排除,避免扫描目录内的文件
119
+ */
120
+ shouldExcludeDirectory(dirName: string): boolean;
121
+ /**
122
+ * 检查文件是否应该被排除
123
+ * 注意:隐藏目录已经在 shouldExcludeDirectory 中被排除,不会进入这里
124
+ * 这里主要处理隐藏文件和路径模式匹配
125
+ */
126
+ shouldExcludeFile(filePath: string, fileName: string): boolean;
127
+ /**
128
+ * Windows 平台路径排除检查
129
+ */
130
+ private shouldExcludeWindowsPath;
131
+ /**
132
+ * 获取所有排除的目录名(用于 fdir exclude)
133
+ */
134
+ getExcludedDirectoryNames(): Set<string>;
135
+ /**
136
+ * 更新规则(允许运行时修改)
137
+ */
138
+ updateRules(newRules: Partial<ScanRules>): void;
139
+ /**
140
+ * 获取当前规则配置
141
+ */
142
+ getRules(): Readonly<ScanRules>;
143
+ }
144
+ /**
145
+ * 创建规则管理器实例
146
+ */
147
+ declare function createRulesManager(customRules?: Partial<ScanRules>): ScanRulesManager;
148
+
149
+ interface ScanConfig {
150
+ /** 扫描目录 */
151
+ directories: string[];
152
+ /** 排除目录(用户自定义,会合并到规则管理器中) */
153
+ excludeDirs?: string[];
154
+ /** 支持的扩展名(用户自定义,会合并到规则管理器中) */
155
+ extensions?: string[];
156
+ /** 最大文件大小 */
157
+ maxFileSize: number;
158
+ /** 扫描进度回调 */
159
+ onProgress?: (progress: {
160
+ scanned: number;
161
+ currentDir?: string;
162
+ }) => void;
163
+ /** 自定义规则(可选,用于覆盖默认规则) */
164
+ customRules?: Partial<ScanRules>;
165
+ }
166
+ /**
167
+ * 扫描目录获取所有支持的文档文件
168
+ */
169
+ declare function scanDirectories(directories: string[], config?: Partial<ScanConfig>): Promise<string[]>;
170
+ /**
171
+ * 获取默认扫描目录(跨平台,只扫描用户目录,不扫描系统盘)
172
+ */
173
+ declare function getDefaultDirectories(): string[];
174
+
175
+ /**
176
+ * 格式化文件大小
177
+ */
178
+ declare function formatSize(bytes: number): string;
179
+ /**
180
+ * 格式化日期
181
+ */
182
+ declare function formatDate(date: Date): string;
183
+ /**
184
+ * 根据扩展名获取文件类型
185
+ */
186
+ declare function getFileType(filePath: string): FileType;
187
+ /**
188
+ * 提取文本摘要(用于搜索结果预览)
189
+ * 优先显示匹配查询词附近的内容
190
+ */
191
+ declare function extractSnippet(content: string, query: string, maxLength?: number): string;
192
+
193
+ interface ParsedDocument {
194
+ /** 提取的文本内容 */
195
+ content: string;
196
+ /** 文档标题(如果能提取) */
197
+ title?: string;
198
+ /** 元数据 */
199
+ metadata?: Record<string, unknown>;
200
+ }
201
+ /**
202
+ * 根据文件扩展名选择解析器
203
+ */
204
+ declare function parseDocument(filePath: string): Promise<ParsedDocument>;
205
+ /**
206
+ * 检查文件是否支持解析
207
+ */
208
+ declare function isSupportedDocument(filePath: string): boolean;
209
+ /**
210
+ * 获取文件类型
211
+ */
212
+ declare function getDocumentType(filePath: string): string;
213
+
214
+ interface VectorRecord {
215
+ id: string;
216
+ vector: number[];
217
+ }
218
+ interface VectorSearchResult {
219
+ id: string;
220
+ distance: number;
221
+ }
222
+ declare class VectorStore {
223
+ private db;
224
+ private table;
225
+ private dbPath;
226
+ private tableName;
227
+ private dimension;
228
+ constructor(dbPath: string, tableName?: string, dimension?: number);
229
+ /**
230
+ * 初始化连接
231
+ */
232
+ init(): Promise<void>;
233
+ /**
234
+ * 添加向量
235
+ */
236
+ add(records: VectorRecord[]): Promise<void>;
237
+ /**
238
+ * 更新向量(删除后重新添加)
239
+ */
240
+ update(record: VectorRecord): Promise<void>;
241
+ /**
242
+ * 根据 ID 获取向量
243
+ */
244
+ getById(id: string): Promise<number[] | null>;
245
+ /**
246
+ * 删除向量
247
+ */
248
+ delete(id: string): Promise<void>;
249
+ /**
250
+ * 搜索最相似的向量
251
+ */
252
+ search(queryVector: number[], limit?: number): Promise<VectorSearchResult[]>;
253
+ /**
254
+ * 获取记录数量
255
+ */
256
+ count(): Promise<number>;
257
+ /**
258
+ * 检查 ID 是否存在
259
+ */
260
+ exists(id: string): Promise<boolean>;
261
+ /**
262
+ * 关闭连接(清理资源)
263
+ * 注意:LanceDB 连接会在对象销毁时自动清理,但可以显式重置引用
264
+ */
265
+ close(): void;
266
+ }
267
+
268
+ interface FullTextRecord {
269
+ id: string;
270
+ title: string;
271
+ content: string;
272
+ }
273
+ interface FullTextSearchResult {
274
+ id: string;
275
+ score: number;
276
+ }
277
+ declare class FullTextIndex {
278
+ private index;
279
+ private indexPath;
280
+ private docCount;
281
+ constructor(dataDir: string);
282
+ private createIndex;
283
+ /**
284
+ * 初始化
285
+ */
286
+ init(): Promise<void>;
287
+ /**
288
+ * 添加文档
289
+ */
290
+ add(record: FullTextRecord): void;
291
+ /**
292
+ * 更新文档
293
+ */
294
+ update(record: FullTextRecord): void;
295
+ /**
296
+ * 删除文档
297
+ */
298
+ remove(id: string): void;
299
+ /**
300
+ * 搜索
301
+ */
302
+ search(query: string, limit?: number): FullTextSearchResult[];
303
+ /**
304
+ * 获取文档数量
305
+ */
306
+ getDocCount(): number;
307
+ /**
308
+ * 根据 ID 获取文档内容
309
+ */
310
+ getContent(id: string): string | null;
311
+ /**
312
+ * 保存索引
313
+ */
314
+ save(): Promise<void>;
315
+ /**
316
+ * 加载索引
317
+ */
318
+ load(): Promise<void>;
319
+ /**
320
+ * 清空索引
321
+ */
322
+ clear(): void;
323
+ }
324
+
325
+ declare class MetaStore {
326
+ private db;
327
+ private dbPath;
328
+ constructor(dataDir: string);
329
+ /**
330
+ * 初始化数据库表
331
+ */
332
+ private init;
333
+ /**
334
+ * 添加或更新文档
335
+ */
336
+ upsert(doc: IndexedDocument): void;
337
+ /**
338
+ * 根据 ID 获取文档
339
+ */
340
+ getById(id: string): IndexedDocument | null;
341
+ /**
342
+ * 根据路径获取文档
343
+ */
344
+ getByPath(filePath: string): IndexedDocument | null;
345
+ /**
346
+ * 根据路径和修改时间获取文档(更精确的唯一性检查)
347
+ */
348
+ getByPathAndTime(filePath: string, modifiedTime: Date): IndexedDocument | null;
349
+ /**
350
+ * 根据内容哈希获取文档(用于检测重复内容)
351
+ */
352
+ getByHash(contentHash: string): IndexedDocument | null;
353
+ /**
354
+ * 根据内容哈希获取所有相同内容的文档路径
355
+ */
356
+ getPathsByHash(contentHash: string): string[];
357
+ /**
358
+ * 根据 ID 列表获取文档
359
+ */
360
+ getByIds(ids: string[]): IndexedDocument[];
361
+ /**
362
+ * 删除文档
363
+ */
364
+ delete(id: string): void;
365
+ /**
366
+ * 根据路径删除文档
367
+ */
368
+ deleteByPath(filePath: string): void;
369
+ /**
370
+ * 获取所有文档
371
+ */
372
+ getAll(): IndexedDocument[];
373
+ /**
374
+ * 获取所有文档路径和哈希(用于增量更新)
375
+ */
376
+ getAllPathsAndHashes(): Map<string, {
377
+ id: string;
378
+ hash: string;
379
+ modifiedAt: Date;
380
+ }>;
381
+ /**
382
+ * 获取统计信息
383
+ */
384
+ getStats(): IndexStats;
385
+ /**
386
+ * 清空所有数据
387
+ */
388
+ clear(): void;
389
+ /**
390
+ * 关闭数据库连接
391
+ */
392
+ close(): void;
393
+ /**
394
+ * 数据行转换为文档对象
395
+ */
396
+ private rowToDocument;
397
+ }
398
+
399
+ /**
400
+ * 文本向量化模块
401
+ * 使用豆包 doubao-embedding-vision-250615 多模态向量化模型
402
+ *
403
+ * 支持:
404
+ * - 文本向量化(支持 instructions 指令)
405
+ * - 图片向量化(URL 或 Base64)
406
+ * - 视频向量化
407
+ * - 多模态混合输入
408
+ */
409
+ /** Embedding 选项 */
410
+ interface EmbedOptions {
411
+ /** 最大文本长度(超出截断) */
412
+ maxLength?: number;
413
+ /** 指令(用于指导模型理解输入意图) */
414
+ instructions?: string;
415
+ }
416
+ /**
417
+ * 默认指令配置
418
+ * 用于区分文档索引和查询检索
419
+ */
420
+ declare const DEFAULT_INSTRUCTIONS: {
421
+ /** 文档索引指令 */
422
+ document: string;
423
+ /** 查询检索指令 */
424
+ query: string;
425
+ };
426
+ /**
427
+ * 初始化 Embedding 模型
428
+ * @param apiKey 豆包 API Key(ARK_API_KEY)
429
+ * @param modelName 模型名称(可选)
430
+ * @param dimensions 向量维度(可选,默认 1024)
431
+ */
432
+ declare function initEmbedder(apiKey?: string, modelName?: string, dimensions?: number): Promise<void>;
433
+ declare function embed(text: string, options?: EmbedOptions): Promise<number[]>;
434
+ /**
435
+ * 生成文档向量(用于索引)
436
+ * 自动使用文档指令
437
+ */
438
+ declare function embedDocument(text: string, maxLength?: number): Promise<number[]>;
439
+ /**
440
+ * 生成查询向量(用于搜索)
441
+ * 自动使用查询指令
442
+ */
443
+ declare function embedQuery(text: string, maxLength?: number): Promise<number[]>;
444
+ /**
445
+ * 批量生成文档向量(串行处理,避免并发限制)
446
+ * @param texts 文本数组
447
+ * @param maxLength 最大文本长度
448
+ * @returns 向量数组的数组
449
+ */
450
+ declare function embedBatch(texts: string[], maxLength?: number): Promise<number[][]>;
451
+ /**
452
+ * 并发批量生成文档向量(适合大量数据)
453
+ * @param texts 文本数组
454
+ * @param concurrency 并发数(默认 5)
455
+ * @param maxLength 最大文本长度
456
+ * @returns 向量数组的数组
457
+ */
458
+ declare function embedBatchConcurrent(texts: string[], concurrency?: number, maxLength?: number): Promise<number[][]>;
459
+ /**
460
+ * 生成图片向量
461
+ * @param imageUrl 图片 URL 或 Base64 编码(格式: data:image/jpeg;base64,xxx)
462
+ * @returns 向量数组
463
+ */
464
+ declare function embedImage(imageUrl: string): Promise<number[]>;
465
+ /**
466
+ * 生成视频向量
467
+ * @param videoUrl 视频 URL 或 Base64 编码
468
+ * @returns 向量数组
469
+ */
470
+ declare function embedVideo(videoUrl: string): Promise<number[]>;
471
+ /**
472
+ * 多模态混合向量化
473
+ * @param inputs 混合输入数组
474
+ * @returns 向量数组
475
+ */
476
+ declare function embedMultimodal(inputs: Array<{
477
+ type: 'text';
478
+ text: string;
479
+ } | {
480
+ type: 'image_url';
481
+ image_url: {
482
+ url: string;
483
+ };
484
+ } | {
485
+ type: 'video_url';
486
+ video_url: {
487
+ url: string;
488
+ };
489
+ }>): Promise<number[]>;
490
+ /**
491
+ * 获取向量维度
492
+ */
493
+ declare function getEmbeddingDimension(): number;
494
+ /**
495
+ * 设置向量维度
496
+ * @param dimension 新的维度值
497
+ */
498
+ declare function setEmbeddingDimension(dimension: number): void;
499
+ /**
500
+ * 释放资源(保持 API 兼容)
501
+ */
502
+ declare function disposeEmbedder(): void;
503
+ /**
504
+ * 检查是否已初始化
505
+ */
506
+ declare function isEmbedderInitialized(): boolean;
507
+
508
+ export { DEFAULT_DIRECTORY_RULES, DEFAULT_EXTENSION_RULES, DEFAULT_FILE_RULES, DEFAULT_INSTRUCTIONS, DEFAULT_PATH_RULES, DEFAULT_SCAN_RULES, type DirectoryRules, type EmbedOptions, type ExtensionRules, type FileRules, FileType, FullTextIndex, type GlobalProgressListener, IndexProgress, IndexStats, IndexedDocument, MetaStore, type PathRules, type ScanRules, ScanRulesManager, VectorStore, addGlobalProgressListener, createRulesManager, disposeEmbedder, embed, embedBatch, embedBatchConcurrent, embedDocument, embedImage, embedMultimodal, embedQuery, embedVideo, extractSnippet, formatDate, formatSize, getDefaultDirectories, getDocumentType, getEmbeddingDimension, getFileType, initEmbedder, isEmbedderInitialized, isSupportedDocument, parseDocument, removeGlobalProgressListener, scanDirectories, setEmbeddingDimension };
package/dist/index.js ADDED
@@ -0,0 +1,99 @@
1
+ import {
2
+ SearchElectronBridge,
3
+ createSearchElectronBridge
4
+ } from "./chunk-GAT4F5NK.js";
5
+ import {
6
+ DEFAULT_CONFIG,
7
+ DEFAULT_DIRECTORY_RULES,
8
+ DEFAULT_EXTENSION_RULES,
9
+ DEFAULT_FILE_RULES,
10
+ DEFAULT_INSTRUCTIONS,
11
+ DEFAULT_PATH_RULES,
12
+ DEFAULT_SCAN_RULES,
13
+ DocumentSearch,
14
+ FileType,
15
+ FullTextIndex,
16
+ IndexingPipeline,
17
+ MetaStore,
18
+ ScanRulesManager,
19
+ VectorStore,
20
+ addGlobalProgressListener,
21
+ createIndexingPipeline,
22
+ createRulesManager,
23
+ disposeEmbedder,
24
+ embed,
25
+ embedBatch,
26
+ embedBatchConcurrent,
27
+ embedDocument,
28
+ embedImage,
29
+ embedMultimodal,
30
+ embedQuery,
31
+ embedVideo,
32
+ extractSnippet,
33
+ formatDate,
34
+ formatSize,
35
+ getChunkStats,
36
+ getDefaultDirectories,
37
+ getDocumentType,
38
+ getEmbeddingDimension,
39
+ getFileType,
40
+ getSearchPlugin,
41
+ initEmbedder,
42
+ isEmbedderInitialized,
43
+ isSupportedDocument,
44
+ parseDocument,
45
+ removeGlobalProgressListener,
46
+ scanDirectories,
47
+ searchPlugin,
48
+ setEmbeddingDimension,
49
+ splitText
50
+ } from "./chunk-YJIIX54F.js";
51
+ export {
52
+ DEFAULT_CONFIG,
53
+ DEFAULT_DIRECTORY_RULES,
54
+ DEFAULT_EXTENSION_RULES,
55
+ DEFAULT_FILE_RULES,
56
+ DEFAULT_INSTRUCTIONS,
57
+ DEFAULT_PATH_RULES,
58
+ DEFAULT_SCAN_RULES,
59
+ DocumentSearch,
60
+ FileType,
61
+ FullTextIndex,
62
+ IndexingPipeline,
63
+ MetaStore,
64
+ ScanRulesManager,
65
+ SearchElectronBridge,
66
+ VectorStore,
67
+ addGlobalProgressListener,
68
+ createIndexingPipeline,
69
+ createRulesManager,
70
+ createSearchElectronBridge,
71
+ disposeEmbedder,
72
+ embed,
73
+ embedBatch,
74
+ embedBatchConcurrent,
75
+ embedDocument,
76
+ embedImage,
77
+ embedMultimodal,
78
+ embedQuery,
79
+ embedVideo,
80
+ extractSnippet,
81
+ formatDate,
82
+ formatSize,
83
+ getChunkStats,
84
+ getDefaultDirectories,
85
+ getDocumentType,
86
+ getEmbeddingDimension,
87
+ getFileType,
88
+ getSearchPlugin,
89
+ initEmbedder,
90
+ isEmbedderInitialized,
91
+ isSupportedDocument,
92
+ parseDocument,
93
+ removeGlobalProgressListener,
94
+ scanDirectories,
95
+ searchPlugin,
96
+ setEmbeddingDimension,
97
+ splitText
98
+ };
99
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,2 @@
1
+ export { q as SearchPluginInstance, p as SearchPluginOptions, o as getSearchPlugin, n as searchPlugin } from '../index-B6UR8lRu.js';
2
+ export { SideEffect, Tool, ToolContext, ToolPlugin, ToolResult } from '@huyooo/ai-chat-core';
@@ -0,0 +1,9 @@
1
+ import {
2
+ getSearchPlugin,
3
+ searchPlugin
4
+ } from "../chunk-YJIIX54F.js";
5
+ export {
6
+ getSearchPlugin,
7
+ searchPlugin
8
+ };
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}