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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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
 * Filesystem browser & search API — supports directory browsing and file search
 * for the DirectoryPicker component and @-triggered file search popup.
 */

import * as path from 'path'
import * as fs from 'fs'
import * as os from 'os'
import ignore from 'ignore'
import { getGlobalConfig } from '../../utils/config.js'
import { execFileNoThrowWithCwd } from '../../utils/execFileNoThrow.js'
import { findGitRoot, gitExe } from '../../utils/git.js'
import { ripGrep } from '../../utils/ripgrep.js'
import { getInitialSettings } from '../../utils/settings/settings.js'
import { isWithinRegisteredFilesystemRoot } from '../services/filesystemAccessRoots.js'

type FilesystemEntry = {
  name: string
  path: string
  isDirectory: boolean
  relativePath?: string
}

type ScoredFilesystemEntry = FilesystemEntry & {
  score: number
}

const FILE_SEARCH_TIMEOUT_MS = 10_000

const IMAGE_MIME_TYPES: Record<string, string> = {
  '.png': 'image/png',
  '.jpg': 'image/jpeg',
  '.jpeg': 'image/jpeg',
  '.gif': 'image/gif',
  '.webp': 'image/webp',
  '.svg': 'image/svg+xml',
  '.bmp': 'image/bmp',
  '.ico': 'image/x-icon',
  '.avif': 'image/avif',
}

function isWithinRoot(targetPath: string, rootPath: string): boolean {
  const target = normalizeComparablePath(targetPath)
  const root = normalizeComparablePath(rootPath)
  return target === root || target.startsWith(`${root}${path.sep}`)
}

function normalizeComparablePath(filePath: string): string {
  const resolved = path.resolve(filePath)
  return process.platform === 'win32' ? resolved.toLowerCase() : resolved
}

function isAllowedFilesystemPath(targetPath: string): boolean {
  const resolvedPath = path.resolve(targetPath)
  const homeDir = path.resolve(os.homedir())

  if (isWithinRoot(resolvedPath, homeDir) || isWithinRoot(resolvedPath, '/tmp')) {
    return true
  }

  if (isWithinRegisteredFilesystemRoot(resolvedPath)) {
    return true
  }

  // macOS reports /tmp as /private/tmp via native folder pickers and realpath().
  if (process.platform === 'darwin' && isWithinRoot(resolvedPath, '/private/tmp')) {
    return true
  }

  return false
}

export async function handleFilesystemRoute(pathname: string, url: URL): Promise<Response> {
  if (pathname === '/api/filesystem/browse') {
    return handleBrowse(url)
  }

  if (pathname === '/api/filesystem/file') {
    return handleServeFile(url)
  }

  return new Response(JSON.stringify({ error: 'Not found' }), { status: 404 })
}

async function handleServeFile(url: URL): Promise<Response> {
  const filePath = url.searchParams.get('path')
  if (!filePath) {
    return json({ error: 'Missing path parameter' }, 400)
  }

  const resolvedPath = path.resolve(filePath)

  if (!isAllowedFilesystemPath(resolvedPath)) {
    return json({ error: 'Access denied: path outside allowed directory' }, 403)
  }

  const ext = path.extname(resolvedPath).toLowerCase()
  const mimeType = IMAGE_MIME_TYPES[ext]

  if (!mimeType) {
    return json({ error: 'Unsupported file type' }, 400)
  }

  try {
    const stat = fs.statSync(resolvedPath)
    if (!stat.isFile()) {
      return json({ error: 'Not a file' }, 400)
    }
    // Limit to 50MB
    if (stat.size > 50 * 1024 * 1024) {
      return json({ error: 'File too large' }, 400)
    }

    const data = fs.readFileSync(resolvedPath)
    return new Response(data, {
      status: 200,
      headers: {
        'Content-Type': mimeType,
        'Content-Length': String(stat.size),
        'Cache-Control': 'private, max-age=3600',
      },
    })
  } catch {
    return json({ error: 'File not found' }, 404)
  }
}

async function handleBrowse(url: URL): Promise<Response> {
  const targetPath = url.searchParams.get('path') || os.homedir() || '/'
  const resolvedPath = path.resolve(targetPath)

  if (!isAllowedFilesystemPath(resolvedPath)) {
    return json({ error: 'Access denied: path outside allowed directory' }, 403)
  }

  const searchQuery = url.searchParams.get('search') || ''
  const includeFiles = url.searchParams.get('includeFiles') === 'true'
  const maxResults = Math.min(parseInt(url.searchParams.get('maxResults') || '200', 10), 200)

  try {
    const stat = fs.statSync(resolvedPath)
    if (!stat.isDirectory()) {
      return json({ error: 'Not a directory', path: resolvedPath }, 400)
    }

    if (searchQuery) {
      const results = await searchFilesystemEntries(resolvedPath, searchQuery, {
        includeFiles,
        maxResults,
      })

      return json({
        currentPath: resolvedPath,
        parentPath: path.dirname(resolvedPath),
        entries: results,
        query: searchQuery,
      })
    }

    const entries = fs.readdirSync(resolvedPath, { withFileTypes: true })

    // Browse mode: show all directories (and optionally files)
    const filtered = entries.filter((e) => {
      if (e.name.startsWith('.')) return false
      if (e.isDirectory()) return true
      return includeFiles
    })

    const entries_list = filtered
      .map((e) => ({
        name: e.name,
        path: path.join(resolvedPath, e.name),
        isDirectory: e.isDirectory(),
        relativePath: e.name,
      }))
      .sort((a, b) => {
        if (a.isDirectory !== b.isDirectory) return a.isDirectory ? -1 : 1
        return a.name.localeCompare(b.name)
      })

    return json({
      currentPath: resolvedPath,
      parentPath: path.dirname(resolvedPath),
      entries: entries_list,
    })
  } catch (err) {
    return json({ error: `Cannot read directory: ${err}`, path: resolvedPath }, 500)
  }
}

async function searchFilesystemEntries(
  rootPath: string,
  searchQuery: string,
  options: { includeFiles: boolean; maxResults: number },
): Promise<FilesystemEntry[]> {
  const normalizedQuery = normalizeSearchText(searchQuery)
  if (!normalizedQuery) return []

  const candidates = await getSearchCandidates(rootPath, options.includeFiles)
  const results = candidates
    .map((entry): ScoredFilesystemEntry | null => {
      const relativePath = entry.relativePath ?? entry.name
      const score = scoreFilesystemEntry(entry.name, relativePath, normalizedQuery, entry.isDirectory)
      return score > 0 ? { ...entry, score } : null
    })
    .filter((entry): entry is ScoredFilesystemEntry => entry !== null)

  return results
    .sort((a, b) => {
      if (a.score !== b.score) return b.score - a.score
      if (a.isDirectory !== b.isDirectory) return a.isDirectory ? -1 : 1
      const aPath = a.relativePath ?? a.name
      const bPath = b.relativePath ?? b.name
      const aDepth = pathDepth(aPath)
      const bDepth = pathDepth(bPath)
      if (aDepth !== bDepth) return aDepth - bDepth
      return (a.relativePath ?? a.name).localeCompare(b.relativePath ?? b.name)
    })
    .slice(0, options.maxResults)
    .map(({ score: _score, ...entry }) => entry)
}

async function getSearchCandidates(rootPath: string, includeFiles: boolean): Promise<FilesystemEntry[]> {
  const files = await getProjectSearchFiles(rootPath)
  const entries = new Map<string, FilesystemEntry>()

  for (const filePath of files) {
    const normalizedFile = normalizeRelativePath(filePath)
    if (!normalizedFile || !isRelativeInsideRoot(normalizedFile)) continue

    let currentDir = path.posix.dirname(normalizedFile)
    while (currentDir !== '.') {
      addCandidate(entries, rootPath, currentDir, true)
      const parent = path.posix.dirname(currentDir)
      if (parent === currentDir) break
      currentDir = parent
    }

    if (includeFiles) {
      addCandidate(entries, rootPath, normalizedFile, false)
    }
  }

  return [...entries.values()]
}

function addCandidate(entries: Map<string, FilesystemEntry>, rootPath: string, relativePath: string, isDirectory: boolean): void {
  if (entries.has(relativePath)) return
  entries.set(relativePath, {
    name: path.posix.basename(relativePath),
    path: path.join(rootPath, ...relativePath.split('/')),
    isDirectory,
    relativePath,
  })
}

async function getProjectSearchFiles(rootPath: string): Promise<string[]> {
  const respectGitignore = shouldRespectGitignore()
  const gitFiles = await getFilesUsingGit(rootPath, respectGitignore)
  if (gitFiles !== null) {
    return gitFiles
  }

  return getFilesUsingRipgrep(rootPath, respectGitignore)
}

function shouldRespectGitignore(): boolean {
  const projectSettings = getInitialSettings()
  const globalConfig = getGlobalConfig()
  return projectSettings.respectGitignore ?? globalConfig.respectGitignore ?? true
}

async function getFilesUsingGit(rootPath: string, respectGitignore: boolean): Promise<string[] | null> {
  const repoRoot = findGitRoot(rootPath)
  if (!repoRoot) return null

  const trackedResult = await execFileNoThrowWithCwd(
    gitExe(),
    ['-c', 'core.quotepath=false', 'ls-files', '--recurse-submodules'],
    { timeout: FILE_SEARCH_TIMEOUT_MS, cwd: repoRoot },
  )
  if (trackedResult.code !== 0) return null

  const untrackedArgs = respectGitignore
    ? ['-c', 'core.quotepath=false', 'ls-files', '--others', '--exclude-standard']
    : ['-c', 'core.quotepath=false', 'ls-files', '--others']
  const untrackedResult = await execFileNoThrowWithCwd(gitExe(), untrackedArgs, {
    timeout: FILE_SEARCH_TIMEOUT_MS,
    cwd: repoRoot,
  })

  const files = [
    ...lines(trackedResult.stdout),
    ...(untrackedResult.code === 0 ? lines(untrackedResult.stdout) : []),
  ]
  let normalized = files
    .map(filePath => normalizeGitPath(filePath, repoRoot, rootPath))
    .filter((filePath): filePath is string => filePath !== null)

  const ignorePatterns = loadSearchIgnorePatterns(repoRoot, rootPath, false)
  if (ignorePatterns) {
    normalized = ignorePatterns.filter(normalized)
  }

  return [...new Set(normalized)]
}

async function getFilesUsingRipgrep(rootPath: string, respectGitignore: boolean): Promise<string[]> {
  const rgArgs = [
    '--files',
    '--follow',
    '--hidden',
    '--glob',
    '!.git/',
    '--glob',
    '!.svn/',
    '--glob',
    '!.hg/',
    '--glob',
    '!.bzr/',
    '--glob',
    '!.jj/',
    '--glob',
    '!.sl/',
  ]
  if (!respectGitignore) {
    rgArgs.push('--no-ignore-vcs')
  }

  const files = await ripGrep(rgArgs, rootPath, AbortSignal.timeout(FILE_SEARCH_TIMEOUT_MS))
  let normalized = files
    .map(filePath => normalizeRipgrepPath(filePath, rootPath))
    .filter((filePath): filePath is string => filePath !== null)

  const ignorePatterns = loadSearchIgnorePatterns(rootPath, rootPath, true)
  if (ignorePatterns) {
    normalized = ignorePatterns.filter(normalized)
  }

  return normalized
}

function normalizeGitPath(filePath: string, repoRoot: string, rootPath: string): string | null {
  const relativePath = path.relative(rootPath, path.join(repoRoot, filePath))
  const normalized = normalizeRelativePath(relativePath)
  return isRelativeInsideRoot(normalized) ? normalized : null
}

function normalizeRipgrepPath(filePath: string, rootPath: string): string | null {
  const relativePath = path.isAbsolute(filePath) ? path.relative(rootPath, filePath) : filePath
  const normalized = normalizeRelativePath(relativePath)
  return isRelativeInsideRoot(normalized) ? normalized : null
}

function normalizeRelativePath(filePath: string): string {
  return filePath.replace(/\\/g, '/').replace(/^\.\/+/, '')
}

function isRelativeInsideRoot(filePath: string): boolean {
  return !!filePath && filePath !== '.' && !filePath.startsWith('../') && !path.isAbsolute(filePath)
}

function lines(output: string): string[] {
  return output.trim().split('\n').map(line => line.trim()).filter(Boolean)
}

function loadSearchIgnorePatterns(repoRoot: string, rootPath: string, includeGitignore: boolean): ReturnType<typeof ignore> | null {
  const ig = ignore()
  let hasPatterns = false
  const ignoreFiles = includeGitignore ? ['.gitignore', '.ignore', '.rgignore'] : ['.ignore', '.rgignore']
  const paths = [...new Set([repoRoot, rootPath])].flatMap(dir => ignoreFiles.map(fileName => path.join(dir, fileName)))

  for (const ignorePath of paths) {
    try {
      ig.add(fs.readFileSync(ignorePath, 'utf8'))
      hasPatterns = true
    } catch {
      // Missing or unreadable ignore files should not break suggestions.
    }
  }

  return hasPatterns ? ig : null
}

function normalizeSearchText(value: string): string {
  return value
    .trim()
    .replace(/\\/g, '/')
    .replace(/^@+/, '')
    .replace(/^\.\/+/, '')
    .replace(/\/+$/, '')
    .toLowerCase()
}

function scoreFilesystemEntry(name: string, relativePath: string, query: string, isDirectory: boolean): number {
  const normalizedName = normalizeSearchText(name)
  const normalizedPath = normalizeSearchText(relativePath)
  const pathNoExtension = normalizedPath.replace(/\.[^/.]+$/, '')
  const pathPrefix = `${query}/`
  const baseBoost = isDirectory ? 4 : 0
  const depthPenalty = Math.min(relativePath.split('/').length - 1, 8) * 2

  if (normalizedPath === query) return 150 + baseBoost - depthPenalty
  if (pathNoExtension === query) return 144 + baseBoost - depthPenalty
  if (normalizedPath.startsWith(pathPrefix)) return 136 + baseBoost - depthPenalty
  if (normalizedPath.startsWith(query)) return 112 + baseBoost - depthPenalty
  if (normalizedName === query) return 96 + baseBoost - depthPenalty
  if (normalizedName.startsWith(query)) return 88 + baseBoost - depthPenalty
  if (normalizedName.includes(query)) return 72 + baseBoost - depthPenalty
  if (normalizedPath.includes(query)) return 60 + baseBoost - depthPenalty

  const nameFuzzy = fuzzyScore(normalizedName, query)
  if (nameFuzzy > 0) return 44 + nameFuzzy + baseBoost - depthPenalty

  const pathFuzzy = fuzzyScore(normalizedPath, query)
  if (pathFuzzy > 0) return 28 + pathFuzzy + baseBoost - depthPenalty

  return 0
}

function pathDepth(relativePath: string): number {
  return relativePath.split('/').length
}

function fuzzyScore(value: string, query: string): number {
  let queryIndex = 0
  let runLength = 0
  let score = 0

  for (let valueIndex = 0; valueIndex < value.length && queryIndex < query.length; valueIndex += 1) {
    if (value[valueIndex] !== query[queryIndex]) {
      runLength = 0
      continue
    }

    const boundaryBoost = valueIndex === 0 || ['/', '-', '_', '.', ' '].includes(value[valueIndex - 1] ?? '')
      ? 3
      : 0
    runLength += 1
    score += 2 + boundaryBoost + Math.min(runLength, 4)
    queryIndex += 1
  }

  return queryIndex === query.length ? score : 0
}

function json(data: unknown, status = 200): Response {
  return new Response(JSON.stringify(data), {
    status,
    headers: { 'Content-Type': 'application/json' },
  })
}