-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathseed-index.ts
More file actions
112 lines (95 loc) · 3.92 KB
/
seed-index.ts
File metadata and controls
112 lines (95 loc) · 3.92 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
// CLI: trigger full re-index locally
//
// Usage:
// npx tsx scripts/seed-index.ts # All sources
// npx tsx scripts/seed-index.ts --source=docs # Just the "docs" source
// npx tsx scripts/seed-index.ts --source=code # Just the "code" source
import { initializeSchema, getPool } from '../src/db/client.js';
import { getConfig, getServerConfig } from '../src/config.js';
import { createEmbeddingProvider } from '../src/indexing/embeddings.js';
import { getProvider } from '../src/indexing/providers/index.js';
import { IndexingPipeline } from '../src/indexing/pipeline.js';
// ---------------------------------------------------------------------------
// Arg parsing
// ---------------------------------------------------------------------------
const args = process.argv.slice(2);
let sourceName: string | undefined;
for (const arg of args) {
if (arg.startsWith('--source=')) {
sourceName = arg.slice('--source='.length);
} else if (arg === '--help' || arg === '-h') {
console.log(`Usage: npx tsx scripts/seed-index.ts [options]
Options:
--source=<name> Index only the named source (default: all sources)
-h, --help Show this help message
`);
process.exit(0);
} else {
console.error(`Unknown argument: ${arg}`);
process.exit(1);
}
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
async function main(): Promise<void> {
const overallStart = Date.now();
const config = getConfig();
const serverConfig = getServerConfig();
// Determine which sources to index
let sources = serverConfig.sources;
if (sourceName) {
sources = sources.filter(s => s.name === sourceName);
if (sources.length === 0) {
const available = serverConfig.sources.map(s => s.name).join(', ');
console.error(`Error: source "${sourceName}" not found in config. Available sources: ${available}`);
process.exit(1);
}
}
console.log('=== seed-index ===');
console.log(`Mode: ${sourceName ? `source=${sourceName}` : `all (${sources.map(s => s.name).join(', ')})`}`);
console.log('');
// Initialize database schema
console.log('Initializing database schema...');
await initializeSchema();
console.log('Schema initialized.\n');
// Create embedding provider from YAML config
const embeddingProvider = createEmbeddingProvider(
serverConfig.embedding,
config.openaiApiKey,
);
// Index each source
for (const sourceConfig of sources) {
const start = Date.now();
console.log(`--- Indexing source: ${sourceConfig.name} (${sourceConfig.type}) ---`);
const provider = getProvider(sourceConfig.type)(sourceConfig, {
cloneDir: config.cloneDir,
githubToken: config.githubToken || undefined,
});
const pipeline = new IndexingPipeline(embeddingProvider, sourceConfig);
const result = await provider.fullAcquire();
if (result.items.length > 0) {
await pipeline.indexItems(result.items, result.stateToken);
}
const elapsed = ((Date.now() - start) / 1000).toFixed(1);
console.log(`Source "${sourceConfig.name}" indexed ${result.items.length} items in ${elapsed}s\n`);
}
const totalElapsed = ((Date.now() - overallStart) / 1000).toFixed(1);
console.log(`=== Done in ${totalElapsed}s ===`);
}
main()
.catch((err) => {
console.error('Fatal error:', err);
process.exit(1);
})
.finally(async () => {
try {
const pool = getPool();
await pool.end();
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
if (!msg.includes('DATABASE_URL')) {
console.warn('[seed-index] Error closing pool:', msg);
}
}
});