codetree-claude 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.codetreerc.default.json +41 -0
- package/README.md +171 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +185 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +131 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +111 -0
- package/dist/config.js.map +1 -0
- package/dist/hook/hook-client.d.ts +6 -0
- package/dist/hook/hook-client.d.ts.map +1 -0
- package/dist/hook/hook-client.js +48 -0
- package/dist/hook/hook-client.js.map +1 -0
- package/dist/hook/pre-tool-use.bundled.js +162 -0
- package/dist/hook/pre-tool-use.d.ts +14 -0
- package/dist/hook/pre-tool-use.d.ts.map +1 -0
- package/dist/hook/pre-tool-use.js +140 -0
- package/dist/hook/pre-tool-use.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/indexer/extractor-registry.d.ts +10 -0
- package/dist/indexer/extractor-registry.d.ts.map +1 -0
- package/dist/indexer/extractor-registry.js +39 -0
- package/dist/indexer/extractor-registry.js.map +1 -0
- package/dist/indexer/extractors/generic.d.ts +11 -0
- package/dist/indexer/extractors/generic.d.ts.map +1 -0
- package/dist/indexer/extractors/generic.js +12 -0
- package/dist/indexer/extractors/generic.js.map +1 -0
- package/dist/indexer/extractors/go.d.ts +10 -0
- package/dist/indexer/extractors/go.d.ts.map +1 -0
- package/dist/indexer/extractors/go.js +119 -0
- package/dist/indexer/extractors/go.js.map +1 -0
- package/dist/indexer/extractors/java.d.ts +10 -0
- package/dist/indexer/extractors/java.d.ts.map +1 -0
- package/dist/indexer/extractors/java.js +89 -0
- package/dist/indexer/extractors/java.js.map +1 -0
- package/dist/indexer/extractors/javascript.d.ts +11 -0
- package/dist/indexer/extractors/javascript.d.ts.map +1 -0
- package/dist/indexer/extractors/javascript.js +190 -0
- package/dist/indexer/extractors/javascript.js.map +1 -0
- package/dist/indexer/extractors/python.d.ts +11 -0
- package/dist/indexer/extractors/python.d.ts.map +1 -0
- package/dist/indexer/extractors/python.js +115 -0
- package/dist/indexer/extractors/python.js.map +1 -0
- package/dist/indexer/extractors/types.d.ts +26 -0
- package/dist/indexer/extractors/types.d.ts.map +1 -0
- package/dist/indexer/extractors/types.js +36 -0
- package/dist/indexer/extractors/types.js.map +1 -0
- package/dist/indexer/hasher.d.ts +11 -0
- package/dist/indexer/hasher.d.ts.map +1 -0
- package/dist/indexer/hasher.js +41 -0
- package/dist/indexer/hasher.js.map +1 -0
- package/dist/indexer/ignore.d.ts +15 -0
- package/dist/indexer/ignore.d.ts.map +1 -0
- package/dist/indexer/ignore.js +63 -0
- package/dist/indexer/ignore.js.map +1 -0
- package/dist/indexer/indexer.d.ts +64 -0
- package/dist/indexer/indexer.d.ts.map +1 -0
- package/dist/indexer/indexer.js +295 -0
- package/dist/indexer/indexer.js.map +1 -0
- package/dist/indexer/watcher.d.ts +16 -0
- package/dist/indexer/watcher.d.ts.map +1 -0
- package/dist/indexer/watcher.js +79 -0
- package/dist/indexer/watcher.js.map +1 -0
- package/dist/server/ipc.d.ts +38 -0
- package/dist/server/ipc.d.ts.map +1 -0
- package/dist/server/ipc.js +212 -0
- package/dist/server/ipc.js.map +1 -0
- package/dist/server/mcp-server.d.ts +2 -0
- package/dist/server/mcp-server.d.ts.map +1 -0
- package/dist/server/mcp-server.js +133 -0
- package/dist/server/mcp-server.js.map +1 -0
- package/dist/server/tools/codetree-find-refs.d.ts +46 -0
- package/dist/server/tools/codetree-find-refs.d.ts.map +1 -0
- package/dist/server/tools/codetree-find-refs.js +83 -0
- package/dist/server/tools/codetree-find-refs.js.map +1 -0
- package/dist/server/tools/codetree-read.d.ts +46 -0
- package/dist/server/tools/codetree-read.d.ts.map +1 -0
- package/dist/server/tools/codetree-read.js +82 -0
- package/dist/server/tools/codetree-read.js.map +1 -0
- package/dist/server/tools/codetree-search.d.ts +59 -0
- package/dist/server/tools/codetree-search.d.ts.map +1 -0
- package/dist/server/tools/codetree-search.js +92 -0
- package/dist/server/tools/codetree-search.js.map +1 -0
- package/dist/server/tools/codetree-structure.d.ts +55 -0
- package/dist/server/tools/codetree-structure.d.ts.map +1 -0
- package/dist/server/tools/codetree-structure.js +137 -0
- package/dist/server/tools/codetree-structure.js.map +1 -0
- package/dist/server/tools/codetree-summary.d.ts +36 -0
- package/dist/server/tools/codetree-summary.d.ts.map +1 -0
- package/dist/server/tools/codetree-summary.js +94 -0
- package/dist/server/tools/codetree-summary.js.map +1 -0
- package/dist/setup/install.d.ts +26 -0
- package/dist/setup/install.d.ts.map +1 -0
- package/dist/setup/install.js +127 -0
- package/dist/setup/install.js.map +1 -0
- package/dist/storage/cache.d.ts +29 -0
- package/dist/storage/cache.d.ts.map +1 -0
- package/dist/storage/cache.js +54 -0
- package/dist/storage/cache.js.map +1 -0
- package/dist/storage/database.d.ts +52 -0
- package/dist/storage/database.d.ts.map +1 -0
- package/dist/storage/database.js +346 -0
- package/dist/storage/database.js.map +1 -0
- package/package.json +60 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { createServer } from "node:http";
|
|
2
|
+
import { writeFileSync, mkdirSync, existsSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { normalizePath } from "../config.js";
|
|
5
|
+
import { relative } from "node:path";
|
|
6
|
+
/**
|
|
7
|
+
* Lightweight HTTP server for hook->MCP server IPC.
|
|
8
|
+
* The PreToolUse hook queries this to check cache status before deciding to allow/deny.
|
|
9
|
+
*/
|
|
10
|
+
export class IpcServer {
|
|
11
|
+
server = null;
|
|
12
|
+
port = 0;
|
|
13
|
+
config;
|
|
14
|
+
indexer;
|
|
15
|
+
db;
|
|
16
|
+
// Telemetry
|
|
17
|
+
stats = {
|
|
18
|
+
cacheHits: 0,
|
|
19
|
+
cacheMisses: 0,
|
|
20
|
+
hookQueries: 0,
|
|
21
|
+
tokensSaved: 0, // Rough estimate: chars that weren't re-read
|
|
22
|
+
};
|
|
23
|
+
constructor(config, indexer, db) {
|
|
24
|
+
this.config = config;
|
|
25
|
+
this.indexer = indexer;
|
|
26
|
+
this.db = db;
|
|
27
|
+
}
|
|
28
|
+
async start() {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
this.server = createServer((req, res) => this.handleRequest(req, res));
|
|
31
|
+
this.server.listen(this.config.ipc.port, this.config.ipc.host, () => {
|
|
32
|
+
const addr = this.server.address();
|
|
33
|
+
if (addr && typeof addr === "object") {
|
|
34
|
+
this.port = addr.port;
|
|
35
|
+
// Write port file so the hook can find us
|
|
36
|
+
this.writePortFile();
|
|
37
|
+
resolve(this.port);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
reject(new Error("Failed to get server address"));
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
this.server.on("error", reject);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
getPort() {
|
|
47
|
+
return this.port;
|
|
48
|
+
}
|
|
49
|
+
stop() {
|
|
50
|
+
if (this.server) {
|
|
51
|
+
this.server.close();
|
|
52
|
+
this.server = null;
|
|
53
|
+
}
|
|
54
|
+
this.persistStats();
|
|
55
|
+
}
|
|
56
|
+
writePortFile() {
|
|
57
|
+
const portFilePath = join(this.config.projectRoot, ".codetree", "ipc.port");
|
|
58
|
+
const dir = dirname(portFilePath);
|
|
59
|
+
if (!existsSync(dir))
|
|
60
|
+
mkdirSync(dir, { recursive: true });
|
|
61
|
+
writeFileSync(portFilePath, String(this.port), "utf-8");
|
|
62
|
+
}
|
|
63
|
+
handleRequest(req, res) {
|
|
64
|
+
const url = new URL(req.url || "/", `http://${req.headers.host}`);
|
|
65
|
+
res.setHeader("Content-Type", "application/json");
|
|
66
|
+
switch (url.pathname) {
|
|
67
|
+
case "/check":
|
|
68
|
+
this.handleCheck(url, res);
|
|
69
|
+
break;
|
|
70
|
+
case "/check-search":
|
|
71
|
+
this.handleCheckSearch(url, res);
|
|
72
|
+
break;
|
|
73
|
+
case "/check-glob":
|
|
74
|
+
this.handleCheckGlob(url, res);
|
|
75
|
+
break;
|
|
76
|
+
case "/status":
|
|
77
|
+
this.handleStatus(res);
|
|
78
|
+
break;
|
|
79
|
+
case "/stats":
|
|
80
|
+
this.handleStats(res);
|
|
81
|
+
break;
|
|
82
|
+
default:
|
|
83
|
+
res.writeHead(404);
|
|
84
|
+
res.end(JSON.stringify({ error: "Not found" }));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Check if a file Read can be served from cache.
|
|
89
|
+
* Returns { cached: true, tool: "codetree_read", path: "..." } or { cached: false }
|
|
90
|
+
*/
|
|
91
|
+
handleCheck(url, res) {
|
|
92
|
+
this.stats.hookQueries++;
|
|
93
|
+
const filePath = url.searchParams.get("path");
|
|
94
|
+
if (!filePath) {
|
|
95
|
+
res.writeHead(400);
|
|
96
|
+
res.end(JSON.stringify({ error: "Missing path parameter" }));
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const relPath = normalizePath(relative(this.config.projectRoot, filePath));
|
|
100
|
+
if (this.indexer.isCacheValid(filePath)) {
|
|
101
|
+
this.stats.cacheHits++;
|
|
102
|
+
const record = this.db.getFile(relPath);
|
|
103
|
+
if (record) {
|
|
104
|
+
this.stats.tokensSaved += Math.ceil(record.size / 4); // ~4 chars per token
|
|
105
|
+
}
|
|
106
|
+
res.writeHead(200);
|
|
107
|
+
res.end(JSON.stringify({
|
|
108
|
+
cached: true,
|
|
109
|
+
tool: "codetree_read",
|
|
110
|
+
path: relPath,
|
|
111
|
+
size: record?.size,
|
|
112
|
+
lineCount: record?.line_count,
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
this.stats.cacheMisses++;
|
|
117
|
+
res.writeHead(200);
|
|
118
|
+
res.end(JSON.stringify({ cached: false }));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Check if a Grep search can be answered from the index.
|
|
123
|
+
*/
|
|
124
|
+
handleCheckSearch(url, res) {
|
|
125
|
+
this.stats.hookQueries++;
|
|
126
|
+
const pattern = url.searchParams.get("pattern");
|
|
127
|
+
const searchPath = url.searchParams.get("path");
|
|
128
|
+
if (!pattern) {
|
|
129
|
+
res.writeHead(400);
|
|
130
|
+
res.end(JSON.stringify({ error: "Missing pattern parameter" }));
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
// We can answer symbol searches from the index
|
|
134
|
+
const symbols = this.db.searchSymbols(pattern, undefined, 20);
|
|
135
|
+
if (symbols.length > 0) {
|
|
136
|
+
this.stats.cacheHits++;
|
|
137
|
+
res.writeHead(200);
|
|
138
|
+
res.end(JSON.stringify({
|
|
139
|
+
cached: true,
|
|
140
|
+
tool: "codetree_search",
|
|
141
|
+
resultCount: symbols.length,
|
|
142
|
+
hint: `Found ${symbols.length} symbols matching "${pattern}". Use codetree_search for full results.`,
|
|
143
|
+
}));
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
this.stats.cacheMisses++;
|
|
147
|
+
res.writeHead(200);
|
|
148
|
+
res.end(JSON.stringify({ cached: false }));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Check if a Glob can be answered from the file tree.
|
|
153
|
+
*/
|
|
154
|
+
handleCheckGlob(url, res) {
|
|
155
|
+
this.stats.hookQueries++;
|
|
156
|
+
const pattern = url.searchParams.get("pattern");
|
|
157
|
+
if (!pattern) {
|
|
158
|
+
res.writeHead(400);
|
|
159
|
+
res.end(JSON.stringify({ error: "Missing pattern parameter" }));
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
// We have the full file tree if indexing is complete
|
|
163
|
+
if (this.indexer.isReady()) {
|
|
164
|
+
this.stats.cacheHits++;
|
|
165
|
+
res.writeHead(200);
|
|
166
|
+
res.end(JSON.stringify({
|
|
167
|
+
cached: true,
|
|
168
|
+
tool: "codetree_structure",
|
|
169
|
+
hint: `Project structure is indexed. Use codetree_structure to browse files matching "${pattern}".`,
|
|
170
|
+
}));
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
this.stats.cacheMisses++;
|
|
174
|
+
res.writeHead(200);
|
|
175
|
+
res.end(JSON.stringify({ cached: false }));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
handleStatus(res) {
|
|
179
|
+
res.writeHead(200);
|
|
180
|
+
res.end(JSON.stringify({
|
|
181
|
+
ready: this.indexer.isReady(),
|
|
182
|
+
indexing: this.indexer.isIndexing(),
|
|
183
|
+
fileCount: this.db.getFileCount(),
|
|
184
|
+
port: this.port,
|
|
185
|
+
}));
|
|
186
|
+
}
|
|
187
|
+
handleStats(res) {
|
|
188
|
+
res.writeHead(200);
|
|
189
|
+
res.end(JSON.stringify({
|
|
190
|
+
...this.stats,
|
|
191
|
+
estimatedTokensSaved: this.stats.tokensSaved,
|
|
192
|
+
hitRate: this.stats.hookQueries > 0
|
|
193
|
+
? this.stats.cacheHits / this.stats.hookQueries
|
|
194
|
+
: 0,
|
|
195
|
+
}));
|
|
196
|
+
}
|
|
197
|
+
persistStats() {
|
|
198
|
+
if (!this.config.telemetry.enabled)
|
|
199
|
+
return;
|
|
200
|
+
try {
|
|
201
|
+
const statsPath = this.config.telemetry.statsFile;
|
|
202
|
+
const dir = dirname(statsPath);
|
|
203
|
+
if (!existsSync(dir))
|
|
204
|
+
mkdirSync(dir, { recursive: true });
|
|
205
|
+
writeFileSync(statsPath, JSON.stringify(this.stats, null, 2), "utf-8");
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
// Non-critical, ignore
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
//# sourceMappingURL=ipc.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ipc.js","sourceRoot":"","sources":["../../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W;YAC5C,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,WAAW,GAAG,CAAC;gBACjC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW;gBAC/C,CAAC,CAAC,CAAC;SACN,CAAC,CAAC,CAAC;IACN,CAAC;IAEO,YAAY;QAClB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO;YAAE,OAAO;QAC3C,IAAI,CAAC;YACH,MAAM,SAAS,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC;YAClD,MAAM,GAAG,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;YAC/B,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC;gBAAE,SAAS,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;YAC1D,aAAa,CAAC,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;QACzE,CAAC;QAAC,MAAM,CAAC;YACP,uBAAuB;QACzB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-server.d.ts","sourceRoot":"","sources":["../../src/server/mcp-server.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
2
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
3
|
+
import { CallToolRequestSchema, ListToolsRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
|
|
4
|
+
import { loadConfig } from "../config.js";
|
|
5
|
+
import { Database } from "../storage/database.js";
|
|
6
|
+
import { ContentCache } from "../storage/cache.js";
|
|
7
|
+
import { Indexer } from "../indexer/indexer.js";
|
|
8
|
+
import { FileWatcher } from "../indexer/watcher.js";
|
|
9
|
+
import { IpcServer } from "./ipc.js";
|
|
10
|
+
import { createReadTool } from "./tools/codetree-read.js";
|
|
11
|
+
import { createSearchTool } from "./tools/codetree-search.js";
|
|
12
|
+
import { createStructureTool } from "./tools/codetree-structure.js";
|
|
13
|
+
import { createFindRefsTool } from "./tools/codetree-find-refs.js";
|
|
14
|
+
import { createSummaryTool } from "./tools/codetree-summary.js";
|
|
15
|
+
async function main() {
|
|
16
|
+
// Load config
|
|
17
|
+
const projectRoot = process.env.CODETREE_PROJECT_ROOT || process.cwd();
|
|
18
|
+
const config = loadConfig(projectRoot);
|
|
19
|
+
// Initialize components
|
|
20
|
+
const db = new Database(config);
|
|
21
|
+
await db.init();
|
|
22
|
+
const cache = new ContentCache(config.cache.memoryLimitMB);
|
|
23
|
+
const indexer = new Indexer(config, db, cache);
|
|
24
|
+
await indexer.init();
|
|
25
|
+
// Start IPC server for hook communication
|
|
26
|
+
const ipc = new IpcServer(config, indexer, db);
|
|
27
|
+
const ipcPort = await ipc.start();
|
|
28
|
+
logToStderr(`[codetree] IPC server listening on port ${ipcPort}`);
|
|
29
|
+
// Start file watcher
|
|
30
|
+
const watcher = new FileWatcher(config, indexer);
|
|
31
|
+
if (config.watch.enabled) {
|
|
32
|
+
watcher.start();
|
|
33
|
+
logToStderr("[codetree] File watcher started");
|
|
34
|
+
}
|
|
35
|
+
// Run initial index in background
|
|
36
|
+
logToStderr("[codetree] Starting initial index scan...");
|
|
37
|
+
indexer.fullScan({
|
|
38
|
+
onProgress: (indexed, total) => {
|
|
39
|
+
if (indexed % 500 === 0) {
|
|
40
|
+
logToStderr(`[codetree] Indexed ${indexed}/${total} files...`);
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
onError: (path, err) => {
|
|
44
|
+
logToStderr(`[codetree] Error indexing ${path}: ${err.message}`);
|
|
45
|
+
},
|
|
46
|
+
onComplete: (stats) => {
|
|
47
|
+
logToStderr(`[codetree] Index complete: ${stats.indexedFiles}/${stats.totalFiles} files, ` +
|
|
48
|
+
`${stats.symbolCount} symbols, ${stats.indexTimeMs}ms`);
|
|
49
|
+
},
|
|
50
|
+
}).catch((err) => {
|
|
51
|
+
logToStderr(`[codetree] Index scan failed: ${err.message}`);
|
|
52
|
+
});
|
|
53
|
+
// Create MCP tool handlers
|
|
54
|
+
const readTool = createReadTool(config, indexer, db);
|
|
55
|
+
const searchTool = createSearchTool(config, indexer, db);
|
|
56
|
+
const structureTool = createStructureTool(config, db);
|
|
57
|
+
const findRefsTool = createFindRefsTool(config, indexer, db);
|
|
58
|
+
const summaryTool = createSummaryTool(config, db);
|
|
59
|
+
const tools = [readTool, searchTool, structureTool, findRefsTool, summaryTool];
|
|
60
|
+
const toolMap = new Map(tools.map((t) => [t.name, t]));
|
|
61
|
+
// Create MCP server
|
|
62
|
+
const server = new Server({
|
|
63
|
+
name: "codetree",
|
|
64
|
+
version: "1.0.0",
|
|
65
|
+
}, {
|
|
66
|
+
capabilities: {
|
|
67
|
+
tools: {},
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
// List tools
|
|
71
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
72
|
+
tools: tools.map((t) => ({
|
|
73
|
+
name: t.name,
|
|
74
|
+
description: t.description,
|
|
75
|
+
inputSchema: t.inputSchema,
|
|
76
|
+
})),
|
|
77
|
+
}));
|
|
78
|
+
// Handle tool calls
|
|
79
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
80
|
+
const { name, arguments: args } = request.params;
|
|
81
|
+
const tool = toolMap.get(name);
|
|
82
|
+
if (!tool) {
|
|
83
|
+
return {
|
|
84
|
+
content: [{ type: "text", text: `Unknown tool: ${name}` }],
|
|
85
|
+
isError: true,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
const result = await tool.handler(args);
|
|
90
|
+
return {
|
|
91
|
+
content: [
|
|
92
|
+
{
|
|
93
|
+
type: "text",
|
|
94
|
+
text: JSON.stringify(result, null, 2),
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
catch (err) {
|
|
100
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
101
|
+
return {
|
|
102
|
+
content: [{ type: "text", text: `Error: ${message}` }],
|
|
103
|
+
isError: true,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
// Connect via stdio transport
|
|
108
|
+
const transport = new StdioServerTransport();
|
|
109
|
+
await server.connect(transport);
|
|
110
|
+
logToStderr("[codetree] MCP server connected via stdio");
|
|
111
|
+
// Graceful shutdown
|
|
112
|
+
const shutdown = () => {
|
|
113
|
+
logToStderr("[codetree] Shutting down...");
|
|
114
|
+
watcher.stop();
|
|
115
|
+
ipc.stop();
|
|
116
|
+
db.close();
|
|
117
|
+
process.exit(0);
|
|
118
|
+
};
|
|
119
|
+
process.on("SIGINT", shutdown);
|
|
120
|
+
process.on("SIGTERM", shutdown);
|
|
121
|
+
process.on("SIGHUP", shutdown);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Log to stderr so it doesn't interfere with MCP stdio protocol on stdout.
|
|
125
|
+
*/
|
|
126
|
+
function logToStderr(msg) {
|
|
127
|
+
process.stderr.write(msg + "\n");
|
|
128
|
+
}
|
|
129
|
+
main().catch((err) => {
|
|
130
|
+
process.stderr.write(`[codetree] Fatal error: ${err.message}\n`);
|
|
131
|
+
process.exit(1);
|
|
132
|
+
});
|
|
133
|
+
//# sourceMappingURL=mcp-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-server.js","sourceRoot":"","sources":["../../src/server/mcp-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EACL,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,oCAAoC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QAAQ,EAAE,QAAQ,CAAC,CAAC;IAC/B,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAChC,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;AACjC,CAAC;AAED;;GAEG;AACH,SAAS,WAAW,CAAC,GAAW;IAC9B,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC;AACnC,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;IACnB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,2BAA2B,GAAG,CAAC,OAAO,IAAI,CAAC,CAAC;IACjE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { CodeTreeConfig } from "../../config.js";
|
|
2
|
+
import type { Indexer } from "../../indexer/indexer.js";
|
|
3
|
+
import type { Database } from "../../storage/database.js";
|
|
4
|
+
export interface FindRefsParams {
|
|
5
|
+
symbol: string;
|
|
6
|
+
kind?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface FindRefsResult {
|
|
9
|
+
definitions: Array<{
|
|
10
|
+
file_path: string;
|
|
11
|
+
line_start: number | null;
|
|
12
|
+
signature: string | null;
|
|
13
|
+
kind: string;
|
|
14
|
+
exported: boolean;
|
|
15
|
+
}>;
|
|
16
|
+
dependents: Array<{
|
|
17
|
+
source_path: string;
|
|
18
|
+
import_spec: string;
|
|
19
|
+
}>;
|
|
20
|
+
content_references: Array<{
|
|
21
|
+
file_path: string;
|
|
22
|
+
line_number: number;
|
|
23
|
+
line_content: string;
|
|
24
|
+
}>;
|
|
25
|
+
totalReferences: number;
|
|
26
|
+
}
|
|
27
|
+
export declare function createFindRefsTool(config: CodeTreeConfig, indexer: Indexer, db: Database): {
|
|
28
|
+
name: string;
|
|
29
|
+
description: string;
|
|
30
|
+
inputSchema: {
|
|
31
|
+
type: "object";
|
|
32
|
+
properties: {
|
|
33
|
+
symbol: {
|
|
34
|
+
type: string;
|
|
35
|
+
description: string;
|
|
36
|
+
};
|
|
37
|
+
kind: {
|
|
38
|
+
type: string;
|
|
39
|
+
description: string;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
required: string[];
|
|
43
|
+
};
|
|
44
|
+
handler: (params: FindRefsParams) => Promise<FindRefsResult>;
|
|
45
|
+
};
|
|
46
|
+
//# sourceMappingURL=codetree-find-refs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codetree-find-refs.d.ts","sourceRoot":"","sources":["../../../src/server/tools/codetree-find-refs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAE1D,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,cAAc;IAC7B,WAAW,EAAE,KAAK,CAAC;QACjB,SAAS,EAAE,MAAM,CAAC;QAClB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;QAC1B,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;QACzB,IAAI,EAAE,MAAM,CAAC;QACb,QAAQ,EAAE,OAAO,CAAC;KACnB,CAAC,CAAC;IACH,UAAU,EAAE,KAAK,CAAC;QAChB,WAAW,EAAE,MAAM,CAAC;QACpB,WAAW,EAAE,MAAM,CAAC;KACrB,CAAC,CAAC;IACH,kBAAkB,EAAE,KAAK,CAAC;QACxB,SAAS,EAAE,MAAM,CAAC;QAClB,WAAW,EAAE,MAAM,CAAC;QACpB,YAAY,EAAE,MAAM,CAAC;KACtB,CAAC,CAAC;IACH,eAAe,EAAE,MAAM,CAAC;CACzB;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,cAAc,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,QAAQ;;;;;;;;;;;;;;;;;sBAqB7D,cAAc,KAAG,OAAO,CAAC,cAAc,CAAC;EA4DnE"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
export function createFindRefsTool(config, indexer, db) {
|
|
2
|
+
return {
|
|
3
|
+
name: "codetree_find_refs",
|
|
4
|
+
description: "Find all references to a symbol across the codebase. Shows: where it's defined, " +
|
|
5
|
+
"which files import/depend on the defining file, and content occurrences. " +
|
|
6
|
+
"Use this to understand impact before refactoring.",
|
|
7
|
+
inputSchema: {
|
|
8
|
+
type: "object",
|
|
9
|
+
properties: {
|
|
10
|
+
symbol: {
|
|
11
|
+
type: "string",
|
|
12
|
+
description: "Symbol name to find references for (e.g., 'UserService', 'handleAuth', 'ApiConfig')",
|
|
13
|
+
},
|
|
14
|
+
kind: {
|
|
15
|
+
type: "string",
|
|
16
|
+
description: "Filter definitions by kind: function, class, interface, type, variable",
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
required: ["symbol"],
|
|
20
|
+
},
|
|
21
|
+
handler: async (params) => {
|
|
22
|
+
// Find definitions in symbol index
|
|
23
|
+
const definitions = db.searchSymbols(params.symbol, params.kind, 20)
|
|
24
|
+
.filter((s) => s.name === params.symbol || s.name.endsWith(`.${params.symbol}`));
|
|
25
|
+
// Find files that depend on the defining files
|
|
26
|
+
const dependentSet = new Map();
|
|
27
|
+
for (const def of definitions) {
|
|
28
|
+
const deps = db.findDependents(def.file_path);
|
|
29
|
+
for (const dep of deps) {
|
|
30
|
+
if (!dependentSet.has(dep.source_path)) {
|
|
31
|
+
dependentSet.set(dep.source_path, dep.target_spec);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// Content search for the symbol name across all cached files
|
|
36
|
+
const contentRefs = [];
|
|
37
|
+
const files = db.getAllFiles();
|
|
38
|
+
const symbolRegex = new RegExp(`\\b${escapeRegex(params.symbol)}\\b`, "g");
|
|
39
|
+
for (const file of files) {
|
|
40
|
+
// Skip definition files (already captured above)
|
|
41
|
+
if (definitions.some((d) => d.file_path === file.path))
|
|
42
|
+
continue;
|
|
43
|
+
const content = indexer.readCached(file.path);
|
|
44
|
+
if (!content)
|
|
45
|
+
continue;
|
|
46
|
+
const lines = content.split("\n");
|
|
47
|
+
for (let i = 0; i < lines.length; i++) {
|
|
48
|
+
if (symbolRegex.test(lines[i])) {
|
|
49
|
+
contentRefs.push({
|
|
50
|
+
file_path: file.path,
|
|
51
|
+
line_number: i + 1,
|
|
52
|
+
line_content: lines[i].trim().slice(0, 200),
|
|
53
|
+
});
|
|
54
|
+
if (contentRefs.length >= 30)
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
symbolRegex.lastIndex = 0;
|
|
58
|
+
}
|
|
59
|
+
if (contentRefs.length >= 30)
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
definitions: definitions.map((d) => ({
|
|
64
|
+
file_path: d.file_path,
|
|
65
|
+
line_start: d.line_start,
|
|
66
|
+
signature: d.signature,
|
|
67
|
+
kind: d.kind,
|
|
68
|
+
exported: d.exported,
|
|
69
|
+
})),
|
|
70
|
+
dependents: Array.from(dependentSet.entries()).map(([source, spec]) => ({
|
|
71
|
+
source_path: source,
|
|
72
|
+
import_spec: spec,
|
|
73
|
+
})),
|
|
74
|
+
content_references: contentRefs,
|
|
75
|
+
totalReferences: definitions.length + dependentSet.size + contentRefs.length,
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function escapeRegex(str) {
|
|
81
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=codetree-find-refs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+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WAAW,CAAC,MAAM,IAAI,EAAE;oBAAE,MAAM;YACtC,CAAC;YAED,OAAO;gBACL,WAAW,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;oBACnC,SAAS,EAAE,CAAC,CAAC,SAAS;oBACtB,UAAU,EAAE,CAAC,CAAC,UAAU;oBACxB,SAAS,EAAE,CAAC,CAAC,SAAS;oBACtB,IAAI,EAAE,CAAC,CAAC,IAAI;oBACZ,QAAQ,EAAE,CAAC,CAAC,QAAQ;iBACrB,CAAC,CAAC;gBACH,UAAU,EAAE,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;oBACtE,WAAW,EAAE,MAAM;oBACnB,WAAW,EAAE,IAAI;iBAClB,CAAC,CAAC;gBACH,kBAAkB,EAAE,WAAW;gBAC/B,eAAe,EAAE,WAAW,CAAC,MAAM,GAAG,YAAY,CAAC,IAAI,GAAG,WAAW,CAAC,MAAM;aAC7E,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC;AAED,SAAS,WAAW,CAAC,GAAW;IAC9B,OAAO,GAAG,CAAC,OAAO,CAAC,qBAAqB,EAAE,MAAM,CAAC,CAAC;AACpD,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type CodeTreeConfig } from "../../config.js";
|
|
2
|
+
import type { Indexer } from "../../indexer/indexer.js";
|
|
3
|
+
import type { Database } from "../../storage/database.js";
|
|
4
|
+
export interface ReadParams {
|
|
5
|
+
file_path: string;
|
|
6
|
+
offset?: number;
|
|
7
|
+
limit?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface ReadResult {
|
|
10
|
+
content: string;
|
|
11
|
+
source: "memory_cache" | "disk_cache" | "direct_read";
|
|
12
|
+
path: string;
|
|
13
|
+
lineCount: number;
|
|
14
|
+
size: number;
|
|
15
|
+
language: string | null;
|
|
16
|
+
symbols: Array<{
|
|
17
|
+
name: string;
|
|
18
|
+
kind: string;
|
|
19
|
+
line: number | null;
|
|
20
|
+
signature: string | null;
|
|
21
|
+
}>;
|
|
22
|
+
}
|
|
23
|
+
export declare function createReadTool(config: CodeTreeConfig, indexer: Indexer, db: Database): {
|
|
24
|
+
name: string;
|
|
25
|
+
description: string;
|
|
26
|
+
inputSchema: {
|
|
27
|
+
type: "object";
|
|
28
|
+
properties: {
|
|
29
|
+
file_path: {
|
|
30
|
+
type: string;
|
|
31
|
+
description: string;
|
|
32
|
+
};
|
|
33
|
+
offset: {
|
|
34
|
+
type: string;
|
|
35
|
+
description: string;
|
|
36
|
+
};
|
|
37
|
+
limit: {
|
|
38
|
+
type: string;
|
|
39
|
+
description: string;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
required: string[];
|
|
43
|
+
};
|
|
44
|
+
handler: (params: ReadParams) => Promise<ReadResult>;
|
|
45
|
+
};
|
|
46
|
+
//# sourceMappingURL=codetree-read.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codetree-read.d.ts","sourceRoot":"","sources":["../../../src/server/tools/codetree-read.ts"],"names":[],"mappings":"AAEA,OAAO,EAAiB,KAAK,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACrE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAE1D,MAAM,WAAW,UAAU;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,cAAc,GAAG,YAAY,GAAG,aAAa,CAAC;IACtD,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC,CAAC;CAC/F;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,cAAc,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,QAAQ;;;;;;;;;;;;;;;;;;;;;sBAyBzD,UAAU,KAAG,OAAO,CAAC,UAAU,CAAC;EAyD3D"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { resolve, relative } from "node:path";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { normalizePath } from "../../config.js";
|
|
4
|
+
export function createReadTool(config, indexer, db) {
|
|
5
|
+
return {
|
|
6
|
+
name: "codetree_read",
|
|
7
|
+
description: "Read a file from the CodeTree cache. Much faster and more token-efficient than the native Read tool. " +
|
|
8
|
+
"Returns the file content along with metadata (symbols, language, line count). " +
|
|
9
|
+
"Use this instead of Read when CodeTree has cached the file.",
|
|
10
|
+
inputSchema: {
|
|
11
|
+
type: "object",
|
|
12
|
+
properties: {
|
|
13
|
+
file_path: {
|
|
14
|
+
type: "string",
|
|
15
|
+
description: "Absolute or project-relative path to the file",
|
|
16
|
+
},
|
|
17
|
+
offset: {
|
|
18
|
+
type: "number",
|
|
19
|
+
description: "Line number to start reading from (0-indexed). Default: 0",
|
|
20
|
+
},
|
|
21
|
+
limit: {
|
|
22
|
+
type: "number",
|
|
23
|
+
description: "Number of lines to read. Default: all lines",
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
required: ["file_path"],
|
|
27
|
+
},
|
|
28
|
+
handler: async (params) => {
|
|
29
|
+
// Resolve the path
|
|
30
|
+
let absPath;
|
|
31
|
+
let relPath;
|
|
32
|
+
if (params.file_path.startsWith("/") || params.file_path.match(/^[A-Za-z]:/)) {
|
|
33
|
+
absPath = resolve(params.file_path);
|
|
34
|
+
relPath = normalizePath(relative(config.projectRoot, absPath));
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
relPath = normalizePath(params.file_path);
|
|
38
|
+
absPath = resolve(config.projectRoot, relPath);
|
|
39
|
+
}
|
|
40
|
+
// Try cache first
|
|
41
|
+
let content = indexer.readCached(relPath);
|
|
42
|
+
let source = "memory_cache";
|
|
43
|
+
if (!content) {
|
|
44
|
+
// Direct read as fallback
|
|
45
|
+
try {
|
|
46
|
+
content = readFileSync(absPath, "utf-8");
|
|
47
|
+
source = "direct_read";
|
|
48
|
+
// Trigger background indexing of this file
|
|
49
|
+
indexer.indexFile(absPath);
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
throw new Error(`Failed to read file: ${absPath}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// Apply offset/limit
|
|
56
|
+
if (params.offset !== undefined || params.limit !== undefined) {
|
|
57
|
+
const lines = content.split("\n");
|
|
58
|
+
const start = params.offset ?? 0;
|
|
59
|
+
const end = params.limit ? start + params.limit : lines.length;
|
|
60
|
+
content = lines.slice(start, end).join("\n");
|
|
61
|
+
}
|
|
62
|
+
// Get metadata
|
|
63
|
+
const fileRecord = db.getFile(relPath);
|
|
64
|
+
const symbols = db.getSymbolsForFile(relPath);
|
|
65
|
+
return {
|
|
66
|
+
content,
|
|
67
|
+
source,
|
|
68
|
+
path: relPath,
|
|
69
|
+
lineCount: fileRecord?.line_count ?? content.split("\n").length,
|
|
70
|
+
size: fileRecord?.size ?? Buffer.byteLength(content, "utf-8"),
|
|
71
|
+
language: fileRecord?.language ?? null,
|
|
72
|
+
symbols: symbols.map((s) => ({
|
|
73
|
+
name: s.name,
|
|
74
|
+
kind: s.kind,
|
|
75
|
+
line: s.line_start,
|
|
76
|
+
signature: s.signature,
|
|
77
|
+
})),
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=codetree-read.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codetree-read.js","sourceRoot":"","sources":["../../../src/server/tools/codetree-read.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,aAAa,EAAuB,MAAM,iBAAiB,CAAC;AAoBrE,MAAM,UAAU,cAAc,CAAC,MAAsB,EAAE,OAAgB,EAAE,EAAY;IACnF,OAAO;QACL,IAAI,EAAE,eAAe;QACrB,WAAW,EACT,uGAAuG;YACvG,gFAAgF;YAChF,6DAA6D;QAC/D,WAAW,EAAE;YACX,IAAI,EAAE,QAAiB;YACvB,UAAU,EAAE;gBACV,SAAS,EAAE;oBACT,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,+CAA+C;iBAC7D;gBACD,MAAM,EAAE;oBACN,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,2DAA2D;iBACzE;gBACD,KAAK,EAAE;oBACL,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,6CAA6C;iBAC3D;aACF;YACD,QAAQ,EAAE,CAAC,WAAW,CAAC;SACxB;QACD,OAAO,EAAE,KAAK,EAAE,MAAkB,EAAuB,EAAE;YACzD,mBAAmB;YACnB,IAAI,OAAe,CAAC;YACpB,IAAI,OAAe,CAAC;YAEpB,IAAI,MAAM,CAAC,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,CAAC;gBAC7E,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;gBACpC,OAAO,GAAG,aAAa,CAAC,QAAQ,CAAC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