spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / mods / agents-md / hooks / files / unseen-files.ts
40 lines · 1.2 KBRaw
1import type { InstructionFile } from 'claude-code'
2
3import Frames from '../frames'
4import { isProjectOwn } from './is-project-own.js'
5
6/**
7 * The candidate files the engine has not already loaded, and no candidate
8 * twice.
9 *
10 * Leaves out one whose path is among the handed files (a CLAUDE.md that
11 * `@`-imports AGENTS.md) or whose text equals a handed project file's (a
12 * CLAUDE.md symlinked to AGENTS.md); other kinds' text does not count.
13 *
14 * @param candidates the AGENTS.md instruction files of a walk
15 * @param handed the files the engine handed the hook
16 * @returns the candidates worth adding, in order
17 */
18export function unseenFiles(
19 candidates: readonly InstructionFile[],
20 handed: readonly InstructionFile[],
21): InstructionFile[] {
22 const paths = new Set(handed.map(file => Frames.normalSpellingOf(file.path)))
23 const texts = new Set(
24 handed.filter(isProjectOwn).map(file => file.content.trim()),
25 )
26 const kept: InstructionFile[] = []
27
28 for (const file of candidates) {
29 const path = Frames.normalSpellingOf(file.path)
30 const text = file.content.trim()
31 const isSeen = paths.has(path) || (text !== '' && texts.has(text))
32
33 if (!isSeen) {
34 paths.add(path)
35 kept.push(file)
36 }
37 }
38
39 return kept
40}