-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutils.ts
More file actions
278 lines (239 loc) · 8.13 KB
/
Copy pathutils.ts
File metadata and controls
278 lines (239 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { FileContext, FileNode } from "./types";
import JSZip from "jszip";
import * as XLSX from "xlsx";
import yaml from "js-yaml";
// 5. PDF.js is loaded dynamically to avoid Node-specific dependencies like 'canvas' during build
let pdfLib: any = null;
const getPdfLib = async () => {
if (pdfLib) return pdfLib;
const lib = await import("pdfjs-dist");
pdfLib = lib.default || lib;
pdfLib.GlobalWorkerOptions.workerSrc = 'https://esm.sh/pdfjs-dist@3.11.174/build/pdf.worker.min.js';
return pdfLib;
};
/**
* Converts a flat list of git paths into a nested FileNode tree.
*/
export const buildFileTree = (items: any[]): FileNode[] => {
const root: FileNode[] = [];
const map: { [key: string]: FileNode } = {};
// Sort: Directories first, then files.
items.sort((a, b) => {
if (a.type === b.type) return a.path.localeCompare(b.path);
return a.type === 'tree' ? -1 : 1;
});
items.forEach(item => {
const parts = item.path.split('/');
const fileName = parts.pop();
const parentPath = parts.join('/');
const node: FileNode = {
name: fileName,
path: item.path,
type: item.type,
children: item.type === 'tree' ? [] : undefined
};
map[item.path] = node;
if (parts.length === 0) {
root.push(node);
} else {
// Find parent (assuming parent folders always come before children in git tree response,
// but if not, this simple logic relies on structure. GitHub recursive tree usually works well.)
// To be robust, we might need to create implicit parents, but GitHub API usually guarantees tree existence.
if (map[parentPath]) {
map[parentPath].children?.push(node);
} else {
// Fallback if parent not found in order (should be rare with recursive=1)
root.push(node);
}
}
});
return root;
};
/**
* Main entry point to read a file (or extract multiple if archive).
*/
export const readFile = async (file: File): Promise<FileContext[]> => {
const buffer = await file.arrayBuffer();
const fileType = determineFileType(new Uint8Array(buffer), file.name);
// 1. Handle ZIP Archives (Recursive)
if (fileType.mime === 'application/zip') {
return handleZipArchive(buffer);
}
// 2. Handle PDF
if (fileType.mime === 'application/pdf') {
const text = await handlePdf(buffer);
return [{
id: genId(),
name: file.name,
type: 'text/plain', // Converted to text for the LLM
content: text,
category: 'other'
}];
}
// 3. Handle Excel
if (fileType.mime === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ||
fileType.mime === 'application/vnd.ms-excel') {
const text = handleExcel(buffer);
return [{
id: genId(),
name: file.name + " (Processed)",
type: 'text/csv',
content: text,
category: 'other'
}];
}
// 4. Handle Image
if (fileType.mime.startsWith('image/')) {
const base64 = arrayBufferToBase64(buffer);
return [{
id: genId(),
name: file.name,
type: fileType.mime,
content: base64,
category: 'image'
}];
}
// 5. Handle Text / Code / YAML / JSON
const textContent = new TextDecoder("utf-8").decode(buffer);
if (fileType.ext === 'yaml' || fileType.ext === 'yml') {
try {
const obj = yaml.load(textContent);
return [{
id: genId(),
name: file.name,
type: 'application/json',
content: JSON.stringify(obj, null, 2),
category: 'code'
}];
} catch (e) {
// Fallback to raw text if parsing fails
}
}
// Default: Treat as Text/Code
return [{
id: genId(),
name: file.name,
type: fileType.mime || 'text/plain',
content: textContent,
category: 'code'
}];
};
// --- Parsers ---
async function handleZipArchive(buffer: ArrayBuffer): Promise<FileContext[]> {
try {
const zip = await JSZip.loadAsync(buffer);
const results: FileContext[] = [];
const entries = Object.keys(zip.files);
// Limit to prevent crashing browser with massive repos
const limitedEntries = entries.slice(0, 50);
for (const filename of limitedEntries) {
const entry = zip.files[filename];
if (entry.dir) continue;
const fileData = await entry.async("arraybuffer");
// Recursively identify type for each file in zip
const type = determineFileType(new Uint8Array(fileData), filename);
// We mostly care about code/text in zips for this app
if (type.mime.startsWith('image/')) {
// Skip images in zips to save tokens/memory for now, or uncomment to support
continue;
}
const text = new TextDecoder("utf-8").decode(fileData);
// Basic binary check to avoid adding garbage
if (isBinary(text)) continue;
results.push({
id: genId(),
name: filename,
type: 'text/plain',
content: text,
category: 'code'
});
}
return results;
} catch (error) {
console.error("Error parsing ZIP:", error);
return [];
}
}
async function handlePdf(buffer: ArrayBuffer): Promise<string> {
try {
const pdf = await getPdfLib();
const loadingTask = pdf.getDocument({ data: buffer });
const pdfDoc = await loadingTask.promise;
let fullText = "";
// Limit pages for token sanity
const maxPages = Math.min(pdfDoc.numPages, 10);
for (let i = 1; i <= maxPages; i++) {
const page = await pdfDoc.getPage(i);
const textContent = await page.getTextContent();
const pageText = textContent.items.map((item: any) => item.str).join(" ");
fullText += `--- Page ${i} ---\n${pageText}\n\n`;
}
return fullText;
} catch (e) {
console.error("PDF Parse Error", e);
return "Error parsing PDF file.";
}
}
function handleExcel(buffer: ArrayBuffer): string {
try {
const workbook = XLSX.read(buffer, { type: 'array' });
let result = "";
workbook.SheetNames.slice(0, 3).forEach(sheetName => {
const sheet = workbook.Sheets[sheetName];
const csv = XLSX.utils.sheet_to_csv(sheet);
result += `--- Sheet: ${sheetName} ---\n${csv}\n\n`;
});
return result;
} catch (e) {
console.error("Excel Parse Error", e);
return "Error parsing Excel file.";
}
}
// --- Helpers ---
function determineFileType(header: Uint8Array, filename: string): { mime: string, ext: string } {
const hex = Array.from(header.subarray(0, 4)).map(b => b.toString(16).padStart(2, '0')).join('').toUpperCase();
const ext = filename.split('.').pop()?.toLowerCase() || '';
// Magic Numbers
if (hex.startsWith('504B0304')) return { mime: 'application/zip', ext: 'zip' };
if (hex.startsWith('25504446')) return { mime: 'application/pdf', ext: 'pdf' };
if (hex.startsWith('D0CF11E0')) return { mime: 'application/vnd.ms-excel', ext: 'xls' }; // Old Excel
if (hex.startsWith('504B0304') && (ext === 'xlsx' || ext === 'docx')) {
// DOCX/XLSX are technically zips, relies on extension distinction here
return { mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', ext: 'xlsx' };
}
// Images
if (hex.startsWith('FFD8FF')) return { mime: 'image/jpeg', ext: 'jpg' };
if (hex.startsWith('89504E47')) return { mime: 'image/png', ext: 'png' };
if (hex.startsWith('47494638')) return { mime: 'image/gif', ext: 'gif' };
// Default fallback to extension or text
return { mime: '', ext };
}
function isBinary(text: string): boolean {
// Simple heuristic: if we see too many nulls or non-printables in first 100 chars
for (let i = 0; i < Math.min(text.length, 100); i++) {
const code = text.charCodeAt(i);
if (code === 0 || (code < 32 && code !== 9 && code !== 10 && code !== 13)) {
return true;
}
}
return false;
}
function arrayBufferToBase64(buffer: ArrayBuffer): string {
let binary = '';
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
/**
* Rough estimate of tokens (4 chars per token)
*/
export const estimateTokens = (text: string): number => {
if (!text) return 0;
return Math.ceil(text.length / 4);
};
function genId() {
return Math.random().toString(36).substring(7);
}