@caplab/read 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +189 -0
- package/dist/cjs/errors/structured.d.ts +12 -0
- package/dist/cjs/errors/structured.d.ts.map +1 -0
- package/dist/cjs/errors/structured.js +124 -0
- package/dist/cjs/errors/structured.js.map +1 -0
- package/dist/cjs/factory.d.ts +3 -0
- package/dist/cjs/factory.d.ts.map +1 -0
- package/dist/cjs/factory.js +92 -0
- package/dist/cjs/factory.js.map +1 -0
- package/dist/cjs/fs/binary.d.ts +4 -0
- package/dist/cjs/fs/binary.d.ts.map +1 -0
- package/dist/cjs/fs/binary.js +116 -0
- package/dist/cjs/fs/binary.js.map +1 -0
- package/dist/cjs/fs/directory.d.ts +3 -0
- package/dist/cjs/fs/directory.d.ts.map +1 -0
- package/dist/cjs/fs/directory.js +163 -0
- package/dist/cjs/fs/directory.js.map +1 -0
- package/dist/cjs/fs/reader.d.ts +3 -0
- package/dist/cjs/fs/reader.d.ts.map +1 -0
- package/dist/cjs/fs/reader.js +145 -0
- package/dist/cjs/fs/reader.js.map +1 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +7 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/output/formatter.d.ts +4 -0
- package/dist/cjs/output/formatter.d.ts.map +1 -0
- package/dist/cjs/output/formatter.js +26 -0
- package/dist/cjs/output/formatter.js.map +1 -0
- package/dist/cjs/output/truncation.d.ts +5 -0
- package/dist/cjs/output/truncation.d.ts.map +1 -0
- package/dist/cjs/output/truncation.js +10 -0
- package/dist/cjs/output/truncation.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/path/guard.d.ts +5 -0
- package/dist/cjs/path/guard.d.ts.map +1 -0
- package/dist/cjs/path/guard.js +91 -0
- package/dist/cjs/path/guard.js.map +1 -0
- package/dist/cjs/path/normalizer.d.ts +3 -0
- package/dist/cjs/path/normalizer.d.ts.map +1 -0
- package/dist/cjs/path/normalizer.js +47 -0
- package/dist/cjs/path/normalizer.js.map +1 -0
- package/dist/cjs/path/resolver.d.ts +5 -0
- package/dist/cjs/path/resolver.d.ts.map +1 -0
- package/dist/cjs/path/resolver.js +73 -0
- package/dist/cjs/path/resolver.js.map +1 -0
- package/dist/cjs/read/encoding.d.ts +5 -0
- package/dist/cjs/read/encoding.d.ts.map +1 -0
- package/dist/cjs/read/encoding.js +29 -0
- package/dist/cjs/read/encoding.js.map +1 -0
- package/dist/cjs/read/multi-file.d.ts +4 -0
- package/dist/cjs/read/multi-file.d.ts.map +1 -0
- package/dist/cjs/read/multi-file.js +293 -0
- package/dist/cjs/read/multi-file.js.map +1 -0
- package/dist/cjs/search/adapter.d.ts +3 -0
- package/dist/cjs/search/adapter.d.ts.map +1 -0
- package/dist/cjs/search/adapter.js +34 -0
- package/dist/cjs/search/adapter.js.map +1 -0
- package/dist/cjs/types/internal.d.ts +24 -0
- package/dist/cjs/types/internal.d.ts.map +1 -0
- package/dist/cjs/types/internal.js +4 -0
- package/dist/cjs/types/internal.js.map +1 -0
- package/dist/cjs/types/public.d.ts +87 -0
- package/dist/cjs/types/public.d.ts.map +1 -0
- package/dist/cjs/types/public.js +4 -0
- package/dist/cjs/types/public.js.map +1 -0
- package/dist/errors/structured.d.ts +12 -0
- package/dist/errors/structured.d.ts.map +1 -0
- package/dist/errors/structured.js +79 -0
- package/dist/errors/structured.js.map +1 -0
- package/dist/factory.d.ts +3 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +89 -0
- package/dist/factory.js.map +1 -0
- package/dist/fs/binary.d.ts +4 -0
- package/dist/fs/binary.d.ts.map +1 -0
- package/dist/fs/binary.js +79 -0
- package/dist/fs/binary.js.map +1 -0
- package/dist/fs/directory.d.ts +3 -0
- package/dist/fs/directory.d.ts.map +1 -0
- package/dist/fs/directory.js +127 -0
- package/dist/fs/directory.js.map +1 -0
- package/dist/fs/reader.d.ts +3 -0
- package/dist/fs/reader.d.ts.map +1 -0
- package/dist/fs/reader.js +109 -0
- package/dist/fs/reader.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/output/formatter.d.ts +4 -0
- package/dist/output/formatter.d.ts.map +1 -0
- package/dist/output/formatter.js +22 -0
- package/dist/output/formatter.js.map +1 -0
- package/dist/output/truncation.d.ts +5 -0
- package/dist/output/truncation.d.ts.map +1 -0
- package/dist/output/truncation.js +7 -0
- package/dist/output/truncation.js.map +1 -0
- package/dist/path/guard.d.ts +5 -0
- package/dist/path/guard.d.ts.map +1 -0
- package/dist/path/guard.js +55 -0
- package/dist/path/guard.js.map +1 -0
- package/dist/path/normalizer.d.ts +3 -0
- package/dist/path/normalizer.d.ts.map +1 -0
- package/dist/path/normalizer.js +10 -0
- package/dist/path/normalizer.js.map +1 -0
- package/dist/path/resolver.d.ts +5 -0
- package/dist/path/resolver.d.ts.map +1 -0
- package/dist/path/resolver.js +35 -0
- package/dist/path/resolver.js.map +1 -0
- package/dist/read/encoding.d.ts +5 -0
- package/dist/read/encoding.d.ts.map +1 -0
- package/dist/read/encoding.js +26 -0
- package/dist/read/encoding.js.map +1 -0
- package/dist/read/multi-file.d.ts +4 -0
- package/dist/read/multi-file.d.ts.map +1 -0
- package/dist/read/multi-file.js +257 -0
- package/dist/read/multi-file.js.map +1 -0
- package/dist/search/adapter.d.ts +3 -0
- package/dist/search/adapter.d.ts.map +1 -0
- package/dist/search/adapter.js +31 -0
- package/dist/search/adapter.js.map +1 -0
- package/dist/types/internal.d.ts +24 -0
- package/dist/types/internal.d.ts.map +1 -0
- package/dist/types/internal.js +3 -0
- package/dist/types/internal.js.map +1 -0
- package/dist/types/public.d.ts +87 -0
- package/dist/types/public.d.ts.map +1 -0
- package/dist/types/public.js +3 -0
- package/dist/types/public.js.map +1 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
# @caplab/read
|
|
2
|
+
|
|
3
|
+
A read-only workspace access package for LLM agents and developer tooling with deterministic path handling, budget-aware file operations, and encoding-safe text processing.
|
|
4
|
+
|
|
5
|
+
## Why This Package Exists
|
|
6
|
+
|
|
7
|
+
AI agents and developer tools need safe, predictable file system access. Traditional file APIs can leak outside workspace boundaries, produce unbounded output, or fail silently on encoding issues. `@caplab/read` provides a controlled, read-only interface with explicit budgets and deterministic behavior.
|
|
8
|
+
|
|
9
|
+
## Problems It Solves
|
|
10
|
+
|
|
11
|
+
- **Workspace boundary enforcement**: Prevents reading files outside the designated workspace root
|
|
12
|
+
- **Output budgeting**: Limits bytes, lines, and file counts to prevent unbounded responses
|
|
13
|
+
- **Encoding safety**: Auto-detects and handles UTF-8, UTF-8 BOM, UTF-16LE, UTF-16BE without silent failures
|
|
14
|
+
- **Binary detection**: Identifies binary files and handles them predictably
|
|
15
|
+
- **Deterministic ordering**: Same inputs produce the same outputs across runs
|
|
16
|
+
- **Symlink safety**: Resolves symlinks to prevent boundary escape attacks
|
|
17
|
+
|
|
18
|
+
## What It Intentionally Does Not Do
|
|
19
|
+
|
|
20
|
+
- Write or modify any files (strictly read-only)
|
|
21
|
+
- Execute code or commands
|
|
22
|
+
- Provide write operations or file system mutations
|
|
23
|
+
- Support arbitrary path traversal outside workspace root
|
|
24
|
+
- Handle encodings outside UTF-8/UTF-16 family
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install @caplab/read
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## ESM-Only Usage
|
|
33
|
+
|
|
34
|
+
This package is ESM-only. Use `import` syntax:
|
|
35
|
+
|
|
36
|
+
```javascript
|
|
37
|
+
import { createWorkspaceReader } from "@caplab/read";
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Quick Start
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
import { createWorkspaceReader } from "@caplab/read";
|
|
44
|
+
|
|
45
|
+
const reader = await createWorkspaceReader({
|
|
46
|
+
workspaceRoot: "./my-project",
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// List directory
|
|
50
|
+
const dirResult = await reader.listDirectory("src");
|
|
51
|
+
console.log(dirResult);
|
|
52
|
+
|
|
53
|
+
// Read file
|
|
54
|
+
const fileResult = await reader.readFile("src/index.ts");
|
|
55
|
+
console.log(fileResult);
|
|
56
|
+
|
|
57
|
+
// Search files
|
|
58
|
+
for await (const match of reader.fileSearch("function")) {
|
|
59
|
+
console.log(match);
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## API Overview
|
|
64
|
+
|
|
65
|
+
### `createWorkspaceReader(options)`
|
|
66
|
+
|
|
67
|
+
Creates a configured workspace reader instance. Throws an error if `workspaceRoot` is invalid (does not exist or is not a directory).
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
interface WorkspaceReaderOptions {
|
|
71
|
+
workspaceRoot: string; // Required absolute path
|
|
72
|
+
maxBytes?: number; // Default: 262144 (256 KiB)
|
|
73
|
+
maxLines?: number; // Default: 2000
|
|
74
|
+
maxFiles?: number; // Default: 20
|
|
75
|
+
maxTotalBytes?: number; // Default: 1048576 (1 MiB)
|
|
76
|
+
maxEntries?: number; // Default: 1000
|
|
77
|
+
maxSearchResults?: number; // Default: 200
|
|
78
|
+
maxDepth?: number; // Default: 10
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### `listDirectory(path, options)`
|
|
83
|
+
|
|
84
|
+
Lists files and directories with deterministic ordering. Returns absolute `path` and normalized `relativePath` for each entry.
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
interface ListDirectoryOptions {
|
|
88
|
+
maxDepth?: number; // Default: unlimited
|
|
89
|
+
includeHidden?: boolean; // Default: false
|
|
90
|
+
include?: string[]; // Glob patterns
|
|
91
|
+
exclude?: string[]; // Glob patterns
|
|
92
|
+
maxEntries?: number; // Default: 1000
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### `readFile(path, options)`
|
|
97
|
+
|
|
98
|
+
Reads a single file with byte and line budgets. Returns absolute `path` and normalized `relativePath`. Supported encodings: UTF-8, UTF-8 BOM, UTF-16LE, UTF-16BE.
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
interface ReadFileOptions {
|
|
102
|
+
maxBytes?: number; // Default: 262144
|
|
103
|
+
maxLines?: number; // Default: 2000
|
|
104
|
+
encoding?: "utf-8" | "utf-16le" | "utf-16be" | "auto"; // Default: 'auto'
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### `readMultipleFiles(paths, options)`
|
|
109
|
+
|
|
110
|
+
Reads multiple files with bounded parallelism and deterministic admission. Returns results with absolute `path` and normalized `relativePath` for each file.
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
interface ReadMultipleFilesOptions {
|
|
114
|
+
maxFiles?: number; // Default: 20
|
|
115
|
+
maxBytes?: number; // Default: 262144 (per-file)
|
|
116
|
+
maxTotalBytes?: number; // Default: 1048576
|
|
117
|
+
maxConcurrency?: number; // Default: 5
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### `fileSearch(query, options)`
|
|
122
|
+
|
|
123
|
+
Search files using an adapter over `@caplab/grep-search`. This is not a second search engine—it delegates search behavior to `@caplab/grep-search` with workspace boundary enforcement and budget limits.
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
interface FileSearchOptions {
|
|
127
|
+
regex?: boolean;
|
|
128
|
+
wholeWord?: boolean;
|
|
129
|
+
caseSensitive?: boolean;
|
|
130
|
+
multiline?: boolean;
|
|
131
|
+
extensions?: string[];
|
|
132
|
+
ignore?: string[];
|
|
133
|
+
maxDepth?: number;
|
|
134
|
+
beforeContext?: number;
|
|
135
|
+
afterContext?: number;
|
|
136
|
+
maxResults?: number; // Default: 200
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Default Budgets and Limits
|
|
141
|
+
|
|
142
|
+
- `readFile.maxBytes`: 262144 (256 KiB)
|
|
143
|
+
- `readFile.maxLines`: 2000
|
|
144
|
+
- `readMultipleFiles.maxFiles`: 20
|
|
145
|
+
- `readMultipleFiles.maxTotalBytes`: 1048576 (1 MiB)
|
|
146
|
+
- `listDirectory.maxEntries`: 1000
|
|
147
|
+
- `fileSearch.maxResults`: 200
|
|
148
|
+
|
|
149
|
+
## Error Model
|
|
150
|
+
|
|
151
|
+
Expected operational failures return structured result objects with explicit error codes:
|
|
152
|
+
|
|
153
|
+
- `file_not_found`: File does not exist
|
|
154
|
+
- `permission_denied`: Insufficient permissions
|
|
155
|
+
- `binary_file`: File detected as binary
|
|
156
|
+
- `unsupported_encoding`: Encoding not supported
|
|
157
|
+
- `path_outside_workspace`: Path outside workspace root
|
|
158
|
+
- `skipped_due_to_budget`: Skipped due to maxTotalBytes exhaustion
|
|
159
|
+
- `skipped_due_to_max_files`: Skipped due to maxFiles limit
|
|
160
|
+
|
|
161
|
+
Only invalid API usage or invalid configuration throws exceptions.
|
|
162
|
+
|
|
163
|
+
## Hard Guarantees / Invariants
|
|
164
|
+
|
|
165
|
+
1. **Read-only guarantee**: The package never mutates workspace state
|
|
166
|
+
2. **Deterministic ordering**: Same input produces same output across runs
|
|
167
|
+
3. **Fail-closed boundaries**: Symlink resolution prevents workspace escape
|
|
168
|
+
4. **Encoding safety**: Text decoding is explicit and predictable
|
|
169
|
+
5. **Output budgeting**: Large files and directory trees don't create unbounded output
|
|
170
|
+
6. **Binary safety**: Binary files are detected and handled predictably
|
|
171
|
+
7. **Path consistency**: All operations return absolute `path` and normalized `relativePath`
|
|
172
|
+
|
|
173
|
+
## Relationship to @caplab/grep-search
|
|
174
|
+
|
|
175
|
+
`fileSearch` is an adapter over `@caplab/grep-search`. It:
|
|
176
|
+
|
|
177
|
+
- Delegates search behavior to `@caplab/grep-search`
|
|
178
|
+
- Enforces workspace boundary with `cwd` parameter
|
|
179
|
+
- Applies default `maxResults` budget
|
|
180
|
+
- Forwards search options (regex, caseSensitive, etc.) via allowlist
|
|
181
|
+
- Does not implement a separate search engine
|
|
182
|
+
|
|
183
|
+
## Node Version Requirement
|
|
184
|
+
|
|
185
|
+
Node.js >= 20.0.0
|
|
186
|
+
|
|
187
|
+
## License
|
|
188
|
+
|
|
189
|
+
MIT License - see LICENSE file for details
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FileReadResult, DirectoryResult } from '../types/public.js';
|
|
2
|
+
export declare function createFileNotFoundResult(filePath: string, workspaceRoot: string): FileReadResult;
|
|
3
|
+
export declare function createPermissionDeniedResult(filePath: string, workspaceRoot: string): FileReadResult;
|
|
4
|
+
export declare function createBinaryFileResult(filePath: string, workspaceRoot: string): FileReadResult;
|
|
5
|
+
export declare function createUnsupportedEncodingResult(filePath: string, workspaceRoot: string): FileReadResult;
|
|
6
|
+
export declare function createPathOutsideWorkspaceResult(filePath: string, workspaceRoot: string): FileReadResult;
|
|
7
|
+
export declare function createSkippedDueToBudgetResult(filePath: string, workspaceRoot: string): FileReadResult;
|
|
8
|
+
export declare function createSkippedDueToMaxFilesResult(filePath: string, workspaceRoot: string): FileReadResult;
|
|
9
|
+
export declare function createDirectoryNotFoundResult(dirPath: string): DirectoryResult;
|
|
10
|
+
export declare function createDirectoryPermissionDeniedResult(dirPath: string): DirectoryResult;
|
|
11
|
+
export declare function createDirectoryOutsideWorkspaceResult(dirPath: string): DirectoryResult;
|
|
12
|
+
//# sourceMappingURL=structured.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structured.d.ts","sourceRoot":"","sources":["../../../src/errors/structured.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAG1E,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,cAAc,CAOhG;AAED,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,cAAc,CAOpG;AAED,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,cAAc,CAO9F;AAED,wBAAgB,+BAA+B,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,cAAc,CAOvG;AAED,wBAAgB,gCAAgC,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,cAAc,CAOxG;AAED,wBAAgB,8BAA8B,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,cAAc,CAOtG;AAED,wBAAgB,gCAAgC,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,cAAc,CAOxG;AAED,wBAAgB,6BAA6B,CAAC,OAAO,EAAE,MAAM,GAAG,eAAe,CAM9E;AAED,wBAAgB,qCAAqC,CAAC,OAAO,EAAE,MAAM,GAAG,eAAe,CAMtF;AAED,wBAAgB,qCAAqC,CAAC,OAAO,EAAE,MAAM,GAAG,eAAe,CAMtF"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.createFileNotFoundResult = createFileNotFoundResult;
|
|
37
|
+
exports.createPermissionDeniedResult = createPermissionDeniedResult;
|
|
38
|
+
exports.createBinaryFileResult = createBinaryFileResult;
|
|
39
|
+
exports.createUnsupportedEncodingResult = createUnsupportedEncodingResult;
|
|
40
|
+
exports.createPathOutsideWorkspaceResult = createPathOutsideWorkspaceResult;
|
|
41
|
+
exports.createSkippedDueToBudgetResult = createSkippedDueToBudgetResult;
|
|
42
|
+
exports.createSkippedDueToMaxFilesResult = createSkippedDueToMaxFilesResult;
|
|
43
|
+
exports.createDirectoryNotFoundResult = createDirectoryNotFoundResult;
|
|
44
|
+
exports.createDirectoryPermissionDeniedResult = createDirectoryPermissionDeniedResult;
|
|
45
|
+
exports.createDirectoryOutsideWorkspaceResult = createDirectoryOutsideWorkspaceResult;
|
|
46
|
+
const path = __importStar(require("node:path"));
|
|
47
|
+
function createFileNotFoundResult(filePath, workspaceRoot) {
|
|
48
|
+
return {
|
|
49
|
+
success: false,
|
|
50
|
+
error: 'file_not_found',
|
|
51
|
+
path: filePath,
|
|
52
|
+
relativePath: path.relative(workspaceRoot, filePath),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function createPermissionDeniedResult(filePath, workspaceRoot) {
|
|
56
|
+
return {
|
|
57
|
+
success: false,
|
|
58
|
+
error: 'permission_denied',
|
|
59
|
+
path: filePath,
|
|
60
|
+
relativePath: path.relative(workspaceRoot, filePath),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function createBinaryFileResult(filePath, workspaceRoot) {
|
|
64
|
+
return {
|
|
65
|
+
success: false,
|
|
66
|
+
error: 'binary_file',
|
|
67
|
+
path: filePath,
|
|
68
|
+
relativePath: path.relative(workspaceRoot, filePath),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function createUnsupportedEncodingResult(filePath, workspaceRoot) {
|
|
72
|
+
return {
|
|
73
|
+
success: false,
|
|
74
|
+
error: 'unsupported_encoding',
|
|
75
|
+
path: filePath,
|
|
76
|
+
relativePath: path.relative(workspaceRoot, filePath),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function createPathOutsideWorkspaceResult(filePath, workspaceRoot) {
|
|
80
|
+
return {
|
|
81
|
+
success: false,
|
|
82
|
+
error: 'path_outside_workspace',
|
|
83
|
+
path: filePath,
|
|
84
|
+
relativePath: path.relative(workspaceRoot, filePath),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function createSkippedDueToBudgetResult(filePath, workspaceRoot) {
|
|
88
|
+
return {
|
|
89
|
+
success: false,
|
|
90
|
+
error: 'skipped_due_to_budget',
|
|
91
|
+
path: filePath,
|
|
92
|
+
relativePath: path.relative(workspaceRoot, filePath),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function createSkippedDueToMaxFilesResult(filePath, workspaceRoot) {
|
|
96
|
+
return {
|
|
97
|
+
success: false,
|
|
98
|
+
error: 'skipped_due_to_max_files',
|
|
99
|
+
path: filePath,
|
|
100
|
+
relativePath: path.relative(workspaceRoot, filePath),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function createDirectoryNotFoundResult(dirPath) {
|
|
104
|
+
return {
|
|
105
|
+
success: false,
|
|
106
|
+
error: 'path_not_found',
|
|
107
|
+
path: dirPath,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function createDirectoryPermissionDeniedResult(dirPath) {
|
|
111
|
+
return {
|
|
112
|
+
success: false,
|
|
113
|
+
error: 'permission_denied',
|
|
114
|
+
path: dirPath,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function createDirectoryOutsideWorkspaceResult(dirPath) {
|
|
118
|
+
return {
|
|
119
|
+
success: false,
|
|
120
|
+
error: 'path_outside_workspace',
|
|
121
|
+
path: dirPath,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=structured.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structured.js","sourceRoot":"","sources":["../../../src/errors/structured.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,4DAOC;AAED,oEAOC;AAED,wDAOC;AAED,0EAOC;AAED,4EAOC;AAED,wEAOC;AAED,4EAOC;AAED,sEAMC;AAED,sFAMC;AAED,sFAMC;AAvFD,gDAAkC;AAElC,SAAgB,wBAAwB,CAAC,QAAgB,EAAE,aAAqB;IAC9E,OAAO;QACL,OAAO,EAAE,KAAK;QACd,KAAK,EAAE,gBAAgB;QACvB,IAAI,EAAE,QAAQ;QACd,YAAY,EAAE,IAAI,CAAC,QAAQ,CAAC,aAAa,EAAE,QAAQ,CAAC;KACrD,CAAC;AACJ,CAAC;AAED,SAAgB,4BAA4B,CAAC,QAAgB,EAAE,aAAqB;IAClF,OAAO;QACL,OAAO,EAAE,KAAK;QACd,KAAK,EAAE,mBAAmB;QAC1B,IAAI,EAAE,QAAQ;QACd,YAAY,EAAE,IAAI,CAAC,QAAQ,CAAC,aAAa,EAAE,QAAQ,CAAC;KACrD,CAAC;AACJ,CAAC;AAED,SAAgB,sBAAsB,CAAC,QAAgB,EAAE,aAAqB;IAC5E,OAAO;QACL,OAAO,EAAE,KAAK;QACd,KAAK,EAAE,aAAa;QACpB,IAAI,EAAE,QAAQ;QACd,YAAY,EAAE,IAAI,CA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C;AACJ,CAAC;AAED,SAAgB,qCAAqC,CAAC,OAAe;IACnE,OAAO;QACL,OAAO,EAAE,KAAK;QACd,KAAK,EAAE,wBAAwB;QAC/B,IAAI,EAAE,OAAO;KACd,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../../src/factory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,sBAAsB,EAAsF,MAAM,mBAAmB,CAAC;AASrK,wBAAsB,qBAAqB,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,eAAe,CAAC,CA2FrG"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createWorkspaceReader = createWorkspaceReader;
|
|
4
|
+
const resolver_js_1 = require("./path/resolver.js");
|
|
5
|
+
const guard_js_1 = require("./path/guard.js");
|
|
6
|
+
const directory_js_1 = require("./fs/directory.js");
|
|
7
|
+
const reader_js_1 = require("./fs/reader.js");
|
|
8
|
+
const multi_file_js_1 = require("./read/multi-file.js");
|
|
9
|
+
const adapter_js_1 = require("./search/adapter.js");
|
|
10
|
+
async function createWorkspaceReader(options) {
|
|
11
|
+
const config = await (0, resolver_js_1.validateWorkspaceRoot)(options.workspaceRoot);
|
|
12
|
+
if (!config) {
|
|
13
|
+
throw new Error('Invalid workspace root: path does not exist or is not a directory');
|
|
14
|
+
}
|
|
15
|
+
// Apply overrides from options
|
|
16
|
+
const workspaceConfig = {
|
|
17
|
+
...config,
|
|
18
|
+
maxBytes: options.maxBytes ?? config.maxBytes,
|
|
19
|
+
maxLines: options.maxLines ?? config.maxLines,
|
|
20
|
+
maxFiles: options.maxFiles ?? config.maxFiles,
|
|
21
|
+
maxTotalBytes: options.maxTotalBytes ?? config.maxTotalBytes,
|
|
22
|
+
maxEntries: options.maxEntries ?? config.maxEntries,
|
|
23
|
+
maxSearchResults: options.maxSearchResults ?? config.maxSearchResults,
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
async listDirectory(path, options) {
|
|
27
|
+
const absolutePath = (0, resolver_js_1.resolvePath)(path, workspaceConfig.workspaceRoot);
|
|
28
|
+
const boundaryCheck = await (0, guard_js_1.validatePathBoundary)(absolutePath, workspaceConfig.workspaceRoot);
|
|
29
|
+
if (!boundaryCheck.valid) {
|
|
30
|
+
if (boundaryCheck.error === 'not_found') {
|
|
31
|
+
return {
|
|
32
|
+
success: false,
|
|
33
|
+
error: 'path_not_found',
|
|
34
|
+
path: absolutePath,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
if (boundaryCheck.error === 'permission_denied') {
|
|
38
|
+
return {
|
|
39
|
+
success: false,
|
|
40
|
+
error: 'permission_denied',
|
|
41
|
+
path: absolutePath,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
success: false,
|
|
46
|
+
error: 'path_outside_workspace',
|
|
47
|
+
path: absolutePath,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return (0, directory_js_1.listDirectory)(absolutePath, workspaceConfig.workspaceRoot, options ?? {});
|
|
51
|
+
},
|
|
52
|
+
async readFile(path, options) {
|
|
53
|
+
const absolutePath = (0, resolver_js_1.resolvePath)(path, workspaceConfig.workspaceRoot);
|
|
54
|
+
const boundaryCheck = await (0, guard_js_1.validatePathBoundary)(absolutePath, workspaceConfig.workspaceRoot);
|
|
55
|
+
if (!boundaryCheck.valid) {
|
|
56
|
+
if (boundaryCheck.error === 'not_found') {
|
|
57
|
+
return {
|
|
58
|
+
success: false,
|
|
59
|
+
error: 'file_not_found',
|
|
60
|
+
path: absolutePath,
|
|
61
|
+
relativePath: (0, resolver_js_1.getRelativePath)(absolutePath, workspaceConfig.workspaceRoot),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
if (boundaryCheck.error === 'permission_denied') {
|
|
65
|
+
return {
|
|
66
|
+
success: false,
|
|
67
|
+
error: 'permission_denied',
|
|
68
|
+
path: absolutePath,
|
|
69
|
+
relativePath: (0, resolver_js_1.getRelativePath)(absolutePath, workspaceConfig.workspaceRoot),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
success: false,
|
|
74
|
+
error: 'path_outside_workspace',
|
|
75
|
+
path: absolutePath,
|
|
76
|
+
relativePath: (0, resolver_js_1.getRelativePath)(absolutePath, workspaceConfig.workspaceRoot),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const maxBytes = options?.maxBytes ?? workspaceConfig.maxBytes;
|
|
80
|
+
const maxLines = options?.maxLines ?? workspaceConfig.maxLines;
|
|
81
|
+
const encoding = options?.encoding;
|
|
82
|
+
return (0, reader_js_1.readFileWithBudget)(absolutePath, maxBytes, maxLines, encoding, workspaceConfig.workspaceRoot);
|
|
83
|
+
},
|
|
84
|
+
async readMultipleFiles(paths, options) {
|
|
85
|
+
return (0, multi_file_js_1.readMultipleFiles)(paths, workspaceConfig, options ?? {});
|
|
86
|
+
},
|
|
87
|
+
fileSearch(query, options) {
|
|
88
|
+
return (0, adapter_js_1.fileSearch)(query, workspaceConfig, options ?? {});
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../../src/factory.ts"],"names":[],"mappings":";;AASA,sDA2FC;AAlGD,oDAAyF;AACzF,8CAAuD;AACvD,oDAAuE;AACvE,8CAAoD;AACpD,wDAAyD;AACzD,oDAAmE;AAE5D,KAAK,UAAU,qBAAqB,CAAC,OAA+B;IACzE,MAAM,MAAM,GAAG,MAAM,IAAA,mCAAqB,EAAC,OAAO,CAAC,aAAa,CAAC,CAAC;IAClE,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,KAAK,CAAC,m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AM,QAAQ,GAAG,OAAO,EAAE,QAAQ,CAAC;YAEnC,OAAO,IAAA,8BAAkB,EAAC,YAAY,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,eAAe,CAAC,aAAa,CAAC,CAAC;QACvG,CAAC;QAED,KAAK,CAAC,iBAAiB,CAAC,KAAe,EAAE,OAAkC;YACzE,OAAO,IAAA,iCAAiB,EAAC,KAAK,EAAE,eAAe,EAAE,OAAO,IAAI,EAAE,CAAC,CAAC;QAClE,CAAC;QAED,UAAU,CAAC,KAAa,EAAE,OAA2B;YACnD,OAAO,IAAA,uBAAc,EAAC,KAAK,EAAE,eAAe,EAAE,OAAO,IAAI,EAAE,CAAC,CAAC;QAC/D,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { EncodingDetectionResult } from '../types/internal.js';
|
|
2
|
+
export declare function detectEncoding(filePath: string): Promise<EncodingDetectionResult>;
|
|
3
|
+
export declare function isBinaryFile(filePath: string, encoding: 'utf-8' | 'utf-16le' | 'utf-16be'): Promise<boolean>;
|
|
4
|
+
//# sourceMappingURL=binary.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binary.d.ts","sourceRoot":"","sources":["../../../src/fs/binary.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAIpE,wBAAsB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAYvF;AA2CD,wBAAsB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,GAAG,UAAU,GAAG,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAiClH"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.detectEncoding = detectEncoding;
|
|
37
|
+
exports.isBinaryFile = isBinaryFile;
|
|
38
|
+
const fs = __importStar(require("node:fs/promises"));
|
|
39
|
+
const SNIFF_CHUNK_SIZE = 8192;
|
|
40
|
+
async function detectEncoding(filePath) {
|
|
41
|
+
try {
|
|
42
|
+
const handle = await fs.open(filePath, 'r');
|
|
43
|
+
const buffer = Buffer.alloc(SNIFF_CHUNK_SIZE);
|
|
44
|
+
const { bytesRead } = await handle.read(buffer, 0, SNIFF_CHUNK_SIZE, 0);
|
|
45
|
+
await handle.close();
|
|
46
|
+
const data = buffer.subarray(0, bytesRead);
|
|
47
|
+
return detectEncodingFromBuffer(data);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return { encoding: 'unknown', bom: false };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function detectEncodingFromBuffer(buffer) {
|
|
54
|
+
if (buffer.length < 2) {
|
|
55
|
+
return { encoding: 'utf-8', bom: false };
|
|
56
|
+
}
|
|
57
|
+
// Check for BOM
|
|
58
|
+
if (buffer[0] === 0xEF && buffer[1] === 0xBB && buffer[2] === 0xBF) {
|
|
59
|
+
return { encoding: 'utf-8', bom: true };
|
|
60
|
+
}
|
|
61
|
+
if (buffer[0] === 0xFF && buffer[1] === 0xFE) {
|
|
62
|
+
return { encoding: 'utf-16le', bom: true };
|
|
63
|
+
}
|
|
64
|
+
if (buffer[0] === 0xFE && buffer[1] === 0xFF) {
|
|
65
|
+
return { encoding: 'utf-16be', bom: true };
|
|
66
|
+
}
|
|
67
|
+
// Detect UTF-16 without BOM by looking for null bytes
|
|
68
|
+
let nullByteCount = 0;
|
|
69
|
+
for (let i = 0; i < Math.min(buffer.length, 100); i++) {
|
|
70
|
+
if (buffer[i] === 0) {
|
|
71
|
+
nullByteCount++;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// If we see many null bytes, it's likely UTF-16
|
|
75
|
+
if (nullByteCount > 10) {
|
|
76
|
+
// Determine LE vs BE by looking at pattern
|
|
77
|
+
if (buffer[0] === 0 && buffer[1] !== 0) {
|
|
78
|
+
return { encoding: 'utf-16be', bom: false };
|
|
79
|
+
}
|
|
80
|
+
if (buffer[0] !== 0 && buffer[1] === 0) {
|
|
81
|
+
return { encoding: 'utf-16le', bom: false };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// Default to UTF-8
|
|
85
|
+
return { encoding: 'utf-8', bom: false };
|
|
86
|
+
}
|
|
87
|
+
async function isBinaryFile(filePath, encoding) {
|
|
88
|
+
try {
|
|
89
|
+
const handle = await fs.open(filePath, 'r');
|
|
90
|
+
const buffer = Buffer.alloc(SNIFF_CHUNK_SIZE);
|
|
91
|
+
const { bytesRead } = await handle.read(buffer, 0, SNIFF_CHUNK_SIZE, 0);
|
|
92
|
+
await handle.close();
|
|
93
|
+
const data = buffer.subarray(0, bytesRead);
|
|
94
|
+
if (encoding === 'utf-8' || encoding === 'utf-16le' || encoding === 'utf-16be') {
|
|
95
|
+
// For supported encodings, decode and check for null bytes in decoded text space
|
|
96
|
+
const decoder = new TextDecoder(encoding, { fatal: false });
|
|
97
|
+
const decoded = decoder.decode(data, { stream: true });
|
|
98
|
+
// Check for null bytes in decoded text
|
|
99
|
+
if (decoded.includes('\0')) {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
// For unsupported encodings, use null-byte heuristic on raw bytes
|
|
105
|
+
for (let i = 0; i < data.length; i++) {
|
|
106
|
+
if (data[i] === 0) {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=binary.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binary.js","sourceRoot":"","sources":["../../../src/fs/binary.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKA,wCAYC;AA2CD,oCAiCC;AA7FD,qDAAuC;AAGvC,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAEvB,KAAK,UAAU,cAAc,CAAC,QAAgB;IACnD,IAAI,CAAC;QACH,MAAM,MA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