@ff-labs/fff-bun 0.1.0-nightly.00750c2

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/src/types.ts ADDED
@@ -0,0 +1,404 @@
1
+ /**
2
+ * Result type for all operations - follows the Result pattern
3
+ */
4
+ export type Result<T> = { ok: true; value: T } | { ok: false; error: string };
5
+
6
+ /**
7
+ * Helper to create a successful result
8
+ */
9
+ export function ok<T>(value: T): Result<T> {
10
+ return { ok: true, value };
11
+ }
12
+
13
+ /**
14
+ * Helper to create an error result
15
+ */
16
+ export function err<T>(error: string): Result<T> {
17
+ return { ok: false, error };
18
+ }
19
+
20
+ /**
21
+ * Initialization options for the file finder
22
+ */
23
+ export interface InitOptions {
24
+ /** Base directory to index (required) */
25
+ basePath: string;
26
+ /** Path to frecency database (optional, omit to skip frecency initialization) */
27
+ frecencyDbPath?: string;
28
+ /** Path to query history database (optional, omit to skip query tracker initialization) */
29
+ historyDbPath?: string;
30
+ /** Use unsafe no-lock mode for databases (optional, defaults to false) */
31
+ useUnsafeNoLock?: boolean;
32
+ /**
33
+ * Pre-populate mmap caches for all files after the initial scan completes.
34
+ * When enabled, the first grep search will be as fast as subsequent ones
35
+ * at the cost of a longer scan time and higher initial memory usage.
36
+ * (default: false)
37
+ */
38
+ warmupMmapCache?: boolean;
39
+ }
40
+
41
+ /**
42
+ * Search options for fuzzy file search
43
+ */
44
+ export interface SearchOptions {
45
+ /** Maximum threads for parallel search (0 = auto) */
46
+ maxThreads?: number;
47
+ /** Current file path (for deprioritization in results) */
48
+ currentFile?: string;
49
+ /** Combo boost score multiplier (default: 100) */
50
+ comboBoostMultiplier?: number;
51
+ /** Minimum combo count for boost (default: 3) */
52
+ minComboCount?: number;
53
+ /** Page index for pagination (default: 0) */
54
+ pageIndex?: number;
55
+ /** Page size for pagination (default: 100) */
56
+ pageSize?: number;
57
+ }
58
+
59
+ /**
60
+ * A file item in search results
61
+ */
62
+ export interface FileItem {
63
+ /** Absolute path to the file */
64
+ path: string;
65
+ /** Path relative to the indexed directory */
66
+ relativePath: string;
67
+ /** File name only */
68
+ fileName: string;
69
+ /** File size in bytes */
70
+ size: number;
71
+ /** Last modified timestamp (Unix seconds) */
72
+ modified: number;
73
+ /** Frecency score based on access patterns */
74
+ accessFrecencyScore: number;
75
+ /** Frecency score based on modification time */
76
+ modificationFrecencyScore: number;
77
+ /** Combined frecency score */
78
+ totalFrecencyScore: number;
79
+ /** Git status: 'clean', 'modified', 'untracked', 'staged_new', etc. */
80
+ gitStatus: string;
81
+ }
82
+
83
+ /**
84
+ * Score breakdown for a search result
85
+ */
86
+ export interface Score {
87
+ /** Total combined score */
88
+ total: number;
89
+ /** Base fuzzy match score */
90
+ baseScore: number;
91
+ /** Bonus for filename match */
92
+ filenameBonus: number;
93
+ /** Bonus for special filenames (index.ts, main.rs, etc.) */
94
+ specialFilenameBonus: number;
95
+ /** Boost from frecency */
96
+ frecencyBoost: number;
97
+ /** Penalty for distance in path */
98
+ distancePenalty: number;
99
+ /** Penalty if this is the current file */
100
+ currentFilePenalty: number;
101
+ /** Boost from query history combo matching */
102
+ comboMatchBoost: number;
103
+ /** Whether this was an exact match */
104
+ exactMatch: boolean;
105
+ /** Type of match: 'fuzzy', 'exact', 'prefix', etc. */
106
+ matchType: string;
107
+ }
108
+
109
+ /**
110
+ * Location in file (from query like "file.ts:42")
111
+ */
112
+ export type Location =
113
+ | { type: "line"; line: number }
114
+ | { type: "position"; line: number; col: number }
115
+ | {
116
+ type: "range";
117
+ start: { line: number; col: number };
118
+ end: { line: number; col: number };
119
+ };
120
+
121
+ /**
122
+ * Search result from fuzzy file search
123
+ */
124
+ export interface SearchResult {
125
+ /** Matched file items */
126
+ items: FileItem[];
127
+ /** Corresponding scores for each item */
128
+ scores: Score[];
129
+ /** Total number of files that matched */
130
+ totalMatched: number;
131
+ /** Total number of indexed files */
132
+ totalFiles: number;
133
+ /** Location parsed from query (e.g., "file.ts:42:10") */
134
+ location?: Location;
135
+ }
136
+
137
+ /**
138
+ * Scan progress information
139
+ */
140
+ export interface ScanProgress {
141
+ /** Number of files scanned so far */
142
+ scannedFilesCount: number;
143
+ /** Whether a scan is currently in progress */
144
+ isScanning: boolean;
145
+ }
146
+
147
+ /**
148
+ * Database health information
149
+ */
150
+ export interface DbHealth {
151
+ /** Path to the database */
152
+ path: string;
153
+ /** Size of the database on disk in bytes */
154
+ diskSize: number;
155
+ }
156
+
157
+ /**
158
+ * Health check result
159
+ */
160
+ export interface HealthCheck {
161
+ /** Library version */
162
+ version: string;
163
+ /** Git integration status */
164
+ git: {
165
+ /** Whether git2 library is available */
166
+ available: boolean;
167
+ /** Whether a git repository was found */
168
+ repositoryFound: boolean;
169
+ /** Git working directory path */
170
+ workdir?: string;
171
+ /** libgit2 version string */
172
+ libgit2Version: string;
173
+ /** Error message if git detection failed */
174
+ error?: string;
175
+ };
176
+ /** File picker status */
177
+ filePicker: {
178
+ /** Whether the file picker is initialized */
179
+ initialized: boolean;
180
+ /** Base path being indexed */
181
+ basePath?: string;
182
+ /** Whether a scan is in progress */
183
+ isScanning?: boolean;
184
+ /** Number of indexed files */
185
+ indexedFiles?: number;
186
+ /** Error message if there's an issue */
187
+ error?: string;
188
+ };
189
+ /** Frecency database status */
190
+ frecency: {
191
+ /** Whether frecency tracking is initialized */
192
+ initialized: boolean;
193
+ /** Database health information */
194
+ dbHealthcheck?: DbHealth;
195
+ /** Error message if there's an issue */
196
+ error?: string;
197
+ };
198
+ /** Query tracker status */
199
+ queryTracker: {
200
+ /** Whether query tracking is initialized */
201
+ initialized: boolean;
202
+ /** Database health information */
203
+ dbHealthcheck?: DbHealth;
204
+ /** Error message if there's an issue */
205
+ error?: string;
206
+ };
207
+ }
208
+
209
+ /**
210
+ * Internal: Options format sent to Rust FFI
211
+ * @internal
212
+ */
213
+ export interface InitOptionsInternal {
214
+ base_path: string;
215
+ frecency_db_path?: string;
216
+ history_db_path?: string;
217
+ use_unsafe_no_lock: boolean;
218
+ warmup_mmap_cache: boolean;
219
+ }
220
+
221
+ /**
222
+ * Internal: Search options format sent to Rust FFI
223
+ * @internal
224
+ */
225
+ export interface SearchOptionsInternal {
226
+ max_threads?: number;
227
+ current_file?: string;
228
+ combo_boost_multiplier?: number;
229
+ min_combo_count?: number;
230
+ page_index?: number;
231
+ page_size?: number;
232
+ }
233
+
234
+ /**
235
+ * Convert public InitOptions to internal format
236
+ * @internal
237
+ */
238
+ export function toInternalInitOptions(opts: InitOptions): InitOptionsInternal {
239
+ return {
240
+ base_path: opts.basePath,
241
+ frecency_db_path: opts.frecencyDbPath,
242
+ history_db_path: opts.historyDbPath,
243
+ use_unsafe_no_lock: opts.useUnsafeNoLock ?? false,
244
+ warmup_mmap_cache: opts.warmupMmapCache ?? false,
245
+ };
246
+ }
247
+
248
+ /**
249
+ * Convert public SearchOptions to internal format
250
+ * @internal
251
+ */
252
+ export function toInternalSearchOptions(
253
+ opts?: SearchOptions,
254
+ ): SearchOptionsInternal {
255
+ return {
256
+ max_threads: opts?.maxThreads,
257
+ current_file: opts?.currentFile,
258
+ combo_boost_multiplier: opts?.comboBoostMultiplier,
259
+ min_combo_count: opts?.minComboCount,
260
+ page_index: opts?.pageIndex,
261
+ page_size: opts?.pageSize,
262
+ };
263
+ }
264
+
265
+
266
+ /**
267
+ * Grep search mode
268
+ */
269
+ export type GrepMode = "plain" | "regex" | "fuzzy";
270
+
271
+ /**
272
+ * Opaque pagination cursor for grep results.
273
+ * Pass this to `GrepOptions.cursor` to fetch the next page.
274
+ * Do not construct or modify this — use the `nextCursor` from a previous `GrepResult`.
275
+ */
276
+ export interface GrepCursor {
277
+ /** @internal */
278
+ readonly __brand: "GrepCursor";
279
+ /** @internal */
280
+ readonly _offset: number;
281
+ }
282
+
283
+ /**
284
+ * @internal Create a GrepCursor from a raw file offset.
285
+ */
286
+ export function createGrepCursor(offset: number): GrepCursor {
287
+ return { __brand: "GrepCursor" as const, _offset: offset };
288
+ }
289
+
290
+ /**
291
+ * Options for live grep (content search)
292
+ *
293
+ * Files are searched sequentially in frecency order (most recently/frequently
294
+ * accessed first). The engine returns a `nextCursor` for fetching the next page.
295
+ */
296
+ export interface GrepOptions {
297
+ /** Maximum file size to search in bytes. Files larger than this are skipped. (default: 10MB) */
298
+ maxFileSize?: number;
299
+ /** Maximum matching lines to collect from a single file (default: 200) */
300
+ maxMatchesPerFile?: number;
301
+ /** Smart case: case-insensitive when the query is all lowercase, case-sensitive otherwise (default: true) */
302
+ smartCase?: boolean;
303
+ /**
304
+ * Pagination cursor from a previous `GrepResult.nextCursor`.
305
+ * Omit (or pass `null`) for the first page.
306
+ */
307
+ cursor?: GrepCursor | null;
308
+ /** Search mode (default: "plain") */
309
+ mode?: GrepMode;
310
+ /**
311
+ * Maximum wall-clock time in milliseconds to spend searching before returning
312
+ * partial results. 0 = unlimited. (default: 0)
313
+ */
314
+ timeBudgetMs?: number;
315
+ }
316
+
317
+ /**
318
+ * A single grep match with file and line information
319
+ */
320
+ export interface GrepMatch {
321
+ /** Absolute path to the file */
322
+ path: string;
323
+ /** Path relative to the indexed directory */
324
+ relativePath: string;
325
+ /** File name only */
326
+ fileName: string;
327
+ /** Git status */
328
+ gitStatus: string;
329
+ /** File size in bytes */
330
+ size: number;
331
+ /** Last modified timestamp (Unix seconds) */
332
+ modified: number;
333
+ /** Whether the file is binary */
334
+ isBinary: boolean;
335
+ /** Combined frecency score */
336
+ totalFrecencyScore: number;
337
+ /** Access-based frecency score */
338
+ accessFrecencyScore: number;
339
+ /** Modification-based frecency score */
340
+ modificationFrecencyScore: number;
341
+ /** 1-based line number of the match */
342
+ lineNumber: number;
343
+ /** 0-based byte column of first match start */
344
+ col: number;
345
+ /** Absolute byte offset of the matched line from file start */
346
+ byteOffset: number;
347
+ /** The matched line text (may be truncated) */
348
+ lineContent: string;
349
+ /** Byte offset pairs [start, end] within lineContent for highlighting */
350
+ matchRanges: [number, number][];
351
+ /** Fuzzy match score (only in fuzzy mode) */
352
+ fuzzyScore?: number;
353
+ }
354
+
355
+ /**
356
+ * Result from a grep search
357
+ */
358
+ export interface GrepResult {
359
+ /** Matched items with file and line information. At most `max_matches_per_file`. */
360
+ items: GrepMatch[];
361
+ /** Total number of matches collected (always equal to items.length). */
362
+ totalMatched: number;
363
+ /** Number of files actually opened and searched in this call */
364
+ totalFilesSearched: number;
365
+ /** Total number of indexed files (before any filtering) */
366
+ totalFiles: number;
367
+ /** Number of files eligible for search after filtering out binary files, oversized files, and constraint mismatches */
368
+ filteredFileCount: number;
369
+ /**
370
+ * Cursor for the next page, or `null` if all eligible files have been searched.
371
+ * Pass this as `GrepOptions.cursor` to continue from where this call left off.
372
+ */
373
+ nextCursor: GrepCursor | null;
374
+ /** When regex mode fails to compile the pattern, the engine falls back to literal matching and this field contains the compilation error */
375
+ regexFallbackError?: string;
376
+ }
377
+
378
+ /**
379
+ * Internal: Grep options format sent to Rust FFI
380
+ * @internal
381
+ */
382
+ export interface GrepOptionsInternal {
383
+ max_file_size?: number;
384
+ max_matches_per_file?: number;
385
+ smart_case?: boolean;
386
+ file_offset?: number;
387
+ mode?: string;
388
+ time_budget_ms?: number;
389
+ }
390
+
391
+ /**
392
+ * Convert public GrepOptions to internal format
393
+ * @internal
394
+ */
395
+ export function toInternalGrepOptions(opts?: GrepOptions): GrepOptionsInternal {
396
+ return {
397
+ max_file_size: opts?.maxFileSize,
398
+ max_matches_per_file: opts?.maxMatchesPerFile,
399
+ smart_case: opts?.smartCase,
400
+ file_offset: opts?.cursor?._offset ?? 0,
401
+ mode: opts?.mode,
402
+ time_budget_ms: opts?.timeBudgetMs,
403
+ };
404
+ }