MCPcopy Create free account
hub / github.com/cocoindex-io/cocoindex-code / process_file

Function process_file

src/cocoindex_code/indexer.py:35–91  ·  view source on GitHub ↗

Process a single file: chunk, embed, and store.

(
    file: localfs.File,
    table: sqlite.TableTarget[CodeChunk],
)

Source from the content-addressed store, hash-verified

33
34@coco.fn(memo=True)
35async def process_file(
36 file: localfs.File,
37 table: sqlite.TableTarget[CodeChunk],
38) -> None:
39 """Process a single file: chunk, embed, and store."""
40 embedder = coco.use_context(EMBEDDER)
41 indexing_params = coco.use_context(INDEXING_EMBED_PARAMS)
42
43 try:
44 content = await file.read_text()
45 except UnicodeDecodeError:
46 return
47
48 if not content.strip():
49 return
50
51 suffix = file.file_path.path.suffix
52 project_root = coco.use_context(CODEBASE_DIR)
53 ps = load_project_settings(project_root)
54 ext_lang_map = {f".{lo.ext}": lo.lang for lo in ps.language_overrides}
55 language = (
56 ext_lang_map.get(suffix)
57 or detect_code_language(filename=file.file_path.path.name)
58 or "text"
59 )
60
61 chunker_registry = coco.use_context(CHUNKER_REGISTRY)
62 chunker = chunker_registry.get(suffix)
63 if chunker is not None:
64 language_override, chunks = chunker(Path(file.file_path.path), content)
65 if language_override is not None:
66 language = language_override
67 else:
68 chunks = splitter.split(
69 content,
70 chunk_size=CHUNK_SIZE,
71 min_chunk_size=MIN_CHUNK_SIZE,
72 chunk_overlap=CHUNK_OVERLAP,
73 language=language,
74 )
75
76 id_gen = IdGenerator()
77
78 async def process(chunk: Chunk) -> None:
79 table.declare_row(
80 row=CodeChunk(
81 id=await id_gen.next_id(chunk.text),
82 file_path=file.file_path.path.as_posix(),
83 language=language,
84 content=chunk.text,
85 start_line=chunk.start.line,
86 end_line=chunk.end.line,
87 embedding=await embedder.embed(chunk.text, **indexing_params),
88 )
89 )
90
91 await coco.map(process, chunks)
92

Callers

nothing calls this directly

Calls 1

load_project_settingsFunction · 0.85

Tested by

no test coverage detected