-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.ts
More file actions
132 lines (111 loc) · 3.7 KB
/
index.ts
File metadata and controls
132 lines (111 loc) · 3.7 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import type { Plugin } from "@opencode-ai/plugin";
import * as path from "path";
import { fileURLToPath } from "url";
import { parseConfig } from "./config/schema.js";
import { loadMergedConfig } from "./config/merger.js";
import { createWatcherWithIndexer } from "./watcher/index.js";
import {
codebase_search,
codebase_peek,
index_codebase,
index_status,
index_health_check,
index_metrics,
index_logs,
find_similar,
call_graph,
implementation_lookup,
add_knowledge_base,
list_knowledge_bases,
remove_knowledge_base,
getSharedIndexer,
initializeTools,
} from "./tools/index.js";
import { loadCommandsFromDirectory } from "./commands/loader.js";
import { RoutingHintController } from "./routing-hints.js";
import { hasProjectMarker } from "./utils/files.js";
import type { CombinedWatcher } from "./watcher/index.js";
let activeWatcher: CombinedWatcher | null = null;
function replaceActiveWatcher(nextWatcher: CombinedWatcher | null): void {
activeWatcher?.stop();
activeWatcher = nextWatcher;
}
function getCommandsDir(): string {
let currentDir = process.cwd();
if (typeof import.meta !== "undefined" && import.meta.url) {
currentDir = path.dirname(fileURLToPath(import.meta.url));
}
return path.join(currentDir, "..", "commands");
}
const plugin: Plugin = async ({ directory }) => {
try {
const projectRoot = directory;
const rawConfig = loadMergedConfig(projectRoot);
const config = parseConfig(rawConfig);
initializeTools(projectRoot, config);
const indexer = getSharedIndexer();
const routingHints = config.search.routingHints
? new RoutingHintController(() => indexer.getStatus())
: null;
const isValidProject = !config.indexing.requireProjectMarker || hasProjectMarker(projectRoot);
if (!isValidProject) {
console.warn(
`[codebase-index] Skipping file watching and auto-indexing: no project marker found in "${projectRoot}". ` +
`Set "indexing.requireProjectMarker": false in config to override.`
);
}
if (config.indexing.autoIndex && isValidProject) {
indexer.initialize().then(() => {
indexer.index().catch(() => {});
}).catch(() => {});
}
if (config.indexing.watchFiles && isValidProject) {
replaceActiveWatcher(createWatcherWithIndexer(getSharedIndexer, projectRoot, config));
} else {
replaceActiveWatcher(null);
}
return {
tool: {
codebase_search,
codebase_peek,
index_codebase,
index_status,
index_health_check,
index_metrics,
index_logs,
find_similar,
call_graph,
implementation_lookup,
add_knowledge_base,
list_knowledge_bases,
remove_knowledge_base,
},
async "chat.message"(input, output) {
routingHints?.observeUserMessage(input.sessionID, output.parts);
},
async "experimental.chat.system.transform"(input, output) {
const hints = await routingHints?.getSystemHints(input.sessionID) ?? [];
output.system.push(...hints);
},
async "tool.execute.after"(input) {
routingHints?.markToolUsed(input.sessionID, input.tool);
},
async config(cfg) {
cfg.command = cfg.command ?? {};
const commandsDir = getCommandsDir();
const commands = loadCommandsFromDirectory(commandsDir);
for (const [name, definition] of commands) {
cfg.command[name] = definition;
}
},
};
} catch (error) {
console.error("[codebase-index] Failed to initialize plugin:", error);
// Return a plugin with no tools to prevent opencode from crashing
return {
tool: undefined,
async config() {},
};
}
};
export default plugin;