Skip to content

Commit 1864797

Browse files
author
福晋
committed
chore: remove temp placeholder absolute paths
1 parent 7f82427 commit 1864797

7 files changed

Lines changed: 23 additions & 23 deletions

File tree

src/context.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,10 @@ export class Context {
220220
})
221221
);
222222

223-
// Internal type that extends Document with load-phase metadata
224-
// not stored in the Document interface itself.
223+
// Internal type that extends Document with load-phase metadata.
224+
// id is required here — Context.load() always assigns it via pathToId.
225225
interface LoadedDoc extends Document {
226+
id: string; // override optional Document.id → required
226227
contentHash?: string;
227228
sourceFilePath?: string;
228229
}

src/loaders/chunker.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,15 @@ export interface ChunkingOptions {
4444
strategy?: 'markdown' | 'fixed' | 'auto';
4545
}
4646

47+
/** A document with a guaranteed ID — used by chunkers.
48+
* Context.load() always assigns id via pathToId before chunking. */
49+
export interface ChunkableDocument extends Document {
50+
id: string;
51+
}
52+
4753
/** Chunker interface — custom chunkers can implement this. */
4854
export interface Chunker {
49-
chunk(doc: Document): Chunk[];
55+
chunk(doc: ChunkableDocument): Chunk[];
5056
}
5157

5258
// ---------------------------------------------------------------------------
@@ -77,7 +83,7 @@ export class MarkdownChunker implements Chunker {
7783
this.chunkOverlap = opts.chunkOverlap;
7884
}
7985

80-
chunk(doc: Document): Chunk[] {
86+
chunk(doc: ChunkableDocument): Chunk[] {
8187
return chunkMarkdown(doc, this.maxChunkSize, this.chunkOverlap);
8288
}
8389
}
@@ -87,7 +93,7 @@ export class MarkdownChunker implements Chunker {
8793
* sections that exceed maxChunkSize with a fixed-size sliding window.
8894
*/
8995
function chunkMarkdown(
90-
doc: Document,
96+
doc: ChunkableDocument,
9197
maxChunkSize: number,
9298
overlap: number,
9399
): Chunk[] {
@@ -205,7 +211,7 @@ export class FixedSizeChunker implements Chunker {
205211
this.chunkOverlap = opts.chunkOverlap;
206212
}
207213

208-
chunk(doc: Document): Chunk[] {
214+
chunk(doc: ChunkableDocument): Chunk[] {
209215
const parts = fixedSizeSplit(doc.content, this.maxChunkSize, this.chunkOverlap);
210216
return parts.map((content, i) => ({
211217
content,
@@ -505,7 +511,7 @@ class AutoChunker implements Chunker {
505511
this.fixed = new FixedSizeChunker(options);
506512
}
507513

508-
chunk(doc: Document): Chunk[] {
514+
chunk(doc: ChunkableDocument): Chunk[] {
509515
// If the document content contains markdown headings (## or ###), use
510516
// heading-aware chunking; otherwise fall back to fixed-size splits.
511517
if (/^#{1,3}\s+\S/m.test(doc.content)) {

src/loaders/json.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ export class JsonLoader implements Loader {
1111
const content = await fs.readFile(filePath, 'utf-8');
1212
const data = JSON.parse(content);
1313

14-
// ID is a temporary placeholder — Context.load() derives the canonical
15-
// hash-based ID from the path relative to basePath (cross-machine consistency).
1614
return {
17-
id: filePath,
1815
content: typeof data === 'string' ? data : JSON.stringify(data, null, 2),
1916
};
2017
}

src/loaders/markdown.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ export class MarkdownLoader implements Loader {
1212
const content = await fs.readFile(filePath, 'utf-8');
1313
const { data: meta, content: body } = matter(content);
1414

15-
// ID is a temporary placeholder — Context.load() derives the canonical
16-
// hash-based ID from the path relative to basePath (cross-machine consistency).
1715
return {
18-
id: filePath,
1916
content: body.trim(),
2017
meta,
2118
};

src/loaders/text.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ export class TextLoader implements Loader {
1010
async load(filePath: string): Promise<Document> {
1111
const content = await fs.readFile(filePath, 'utf-8');
1212

13-
// ID is a temporary placeholder — Context.load() derives the canonical
14-
// hash-based ID from the path relative to basePath (cross-machine consistency).
1513
return {
16-
id: filePath,
1714
content: content.trim(),
1815
};
1916
}

src/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
* Document structure
33
*/
44
export interface Document {
5-
/** Document unique identifier (default: file path) */
6-
id: string;
5+
/** Document unique identifier — assigned by Context.load() (hash-based).
6+
* Not set by Loaders; Context.load() derives it from the file path
7+
* relative to basePath for cross-machine consistency. */
8+
id?: string;
79
/** Document content */
810
content: string;
911
/** Markdown front-matter metadata */

test/loaders.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ describe('Loaders', () => {
1313
const filePath = path.join(FIXTURES_DIR, 'getting-started.md');
1414
const doc = await loader.load(filePath);
1515

16-
// Loader returns the file path as a temporary ID — the canonical
17-
// hash-based ID is assigned by Context.load() for cross-machine consistency.
18-
expect(doc.id).toBe(filePath);
16+
// Loader does not assign id — the canonical hash-based ID is
17+
// assigned by Context.load() for cross-machine consistency.
18+
expect(doc.id).toBeUndefined();
1919
expect(doc.content).toContain('Getting Started');
2020
expect(doc.content).toContain('npm install');
2121
expect(doc.meta).toEqual({
@@ -38,7 +38,7 @@ describe('Loaders', () => {
3838
const filePath = path.join(FIXTURES_DIR, 'api.json');
3939
const doc = await loader.load(filePath);
4040

41-
expect(doc.id).toBe(filePath);
41+
expect(doc.id).toBeUndefined();
4242
expect(doc.content).toContain('API Reference');
4343
expect(doc.content).toContain('/users');
4444
});
@@ -56,7 +56,7 @@ describe('Loaders', () => {
5656
const filePath = path.join(FIXTURES_DIR, 'notes.txt');
5757
const doc = await loader.load(filePath);
5858

59-
expect(doc.id).toBe(filePath);
59+
expect(doc.id).toBeUndefined();
6060
expect(doc.content).toContain('notes');
6161
});
6262

0 commit comments

Comments
 (0)