@takara-ai/miru-code 0.1.1 → 0.1.3
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/package.json +1 -1
- package/src/embeddings/openai.ts +12 -3
- package/src/mcp/index-cache.ts +160 -21
- package/src/mcp/serve.ts +2 -2
package/package.json
CHANGED
package/src/embeddings/openai.ts
CHANGED
|
@@ -63,14 +63,23 @@ function splitIntoWindows(text: string, maxChars: number): string[] {
|
|
|
63
63
|
return out;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
/** Unpaired UTF-16 surrogates are invalid in JSON and break strict API parsers. */
|
|
67
|
+
function stripLoneSurrogates(text: string): string {
|
|
68
|
+
return text.replace(
|
|
69
|
+
/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g,
|
|
70
|
+
"\uFFFD",
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function sanitizeEmbeddingInput(text: string): string {
|
|
75
|
+
let out = stripLoneSurrogates(text);
|
|
67
76
|
// Some OpenAI-compatible gateways mis-handle backslash escapes in JSON text
|
|
68
77
|
// payloads. Keep a benchmark fallback mode to forcefully neutralize them.
|
|
69
78
|
const mode = process.env.MIRU_EMBED_ESCAPE_MODE ?? process.env.SEMBLE_EMBED_ESCAPE_MODE ?? "quad";
|
|
70
79
|
if (mode === "strip") {
|
|
71
|
-
return
|
|
80
|
+
return out.replaceAll("\\", "/");
|
|
72
81
|
}
|
|
73
|
-
return
|
|
82
|
+
return out.replaceAll("\\", "\\\\\\\\");
|
|
74
83
|
}
|
|
75
84
|
|
|
76
85
|
function isPayloadTooLargeError(err: unknown): boolean {
|
package/src/mcp/index-cache.ts
CHANGED
|
@@ -1,16 +1,73 @@
|
|
|
1
1
|
import { watch } from "node:fs";
|
|
2
2
|
import { resolve } from "node:path";
|
|
3
3
|
import { MiruIndex } from "../miru-index.ts";
|
|
4
|
+
import { envOptionalInt } from "../env.ts";
|
|
4
5
|
import type { ContentType } from "../types.ts";
|
|
5
6
|
import { computeSourceCacheKey, isGitUrl } from "../utils.ts";
|
|
6
7
|
|
|
7
8
|
const CACHE_MAX_SIZE = 10;
|
|
9
|
+
const DEFAULT_REINDEX_DEBOUNCE_MS = 3000;
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
/** Directory names we skip for MCP fs.watch re-index triggers (aligned with file-walker). */
|
|
12
|
+
const WATCH_IGNORED_DIR_NAMES = new Set([
|
|
13
|
+
".git",
|
|
14
|
+
".hg",
|
|
15
|
+
".svn",
|
|
16
|
+
"__pycache__",
|
|
17
|
+
"node_modules",
|
|
18
|
+
".venv",
|
|
19
|
+
"venv",
|
|
20
|
+
".tox",
|
|
21
|
+
".mypy_cache",
|
|
22
|
+
".pytest_cache",
|
|
23
|
+
".ruff_cache",
|
|
24
|
+
".cache",
|
|
25
|
+
".miru",
|
|
26
|
+
".next",
|
|
27
|
+
"dist",
|
|
28
|
+
"build",
|
|
29
|
+
".eggs",
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
class StaleIndexError extends Error {
|
|
33
|
+
constructor() {
|
|
34
|
+
super("Index build superseded by a newer change");
|
|
35
|
+
this.name = "StaleIndexError";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
type CacheEntry = {
|
|
40
|
+
generation: number;
|
|
41
|
+
task: Promise<MiruIndex> | null;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function resolveReindexDebounceMs(): number {
|
|
45
|
+
return envOptionalInt(["MIRU_MCP_REINDEX_DEBOUNCE_MS"], 0) ?? DEFAULT_REINDEX_DEBOUNCE_MS;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function mcpWatchEnabled(): boolean {
|
|
49
|
+
const raw = process.env.MIRU_MCP_WATCH ?? process.env.SEMBLE_MCP_WATCH;
|
|
50
|
+
return raw !== "0" && raw !== "false";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Returns true when a watch event path should not trigger a re-index. */
|
|
54
|
+
export function shouldIgnoreWatchPath(relativePath: string | null | undefined): boolean {
|
|
55
|
+
if (!relativePath) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
const normalized = relativePath.replace(/\\/g, "/");
|
|
59
|
+
for (const segment of normalized.split("/")) {
|
|
60
|
+
if (segment && WATCH_IGNORED_DIR_NAMES.has(segment)) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
10
66
|
|
|
11
67
|
export class IndexCache {
|
|
12
68
|
private readonly content: ContentType[];
|
|
13
|
-
private readonly
|
|
69
|
+
private readonly entries = new Map<string, CacheEntry>();
|
|
70
|
+
private readonly reindexTimers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
14
71
|
private watcher: ReturnType<typeof watch> | null = null;
|
|
15
72
|
|
|
16
73
|
constructor(content: ContentType[] = ["code"]) {
|
|
@@ -18,46 +75,128 @@ export class IndexCache {
|
|
|
18
75
|
}
|
|
19
76
|
|
|
20
77
|
evict(source: string, ref?: string | null): void {
|
|
21
|
-
this.
|
|
78
|
+
this.invalidate(computeSourceCacheKey(source, ref));
|
|
22
79
|
}
|
|
23
80
|
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
81
|
+
private invalidate(cacheKey: string): void {
|
|
82
|
+
const entry = this.entries.get(cacheKey);
|
|
83
|
+
if (entry) {
|
|
84
|
+
entry.generation++;
|
|
85
|
+
entry.task = null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private ensureEntry(cacheKey: string): CacheEntry {
|
|
90
|
+
let entry = this.entries.get(cacheKey);
|
|
91
|
+
if (!entry) {
|
|
92
|
+
if (this.entries.size >= CACHE_MAX_SIZE) {
|
|
93
|
+
const oldest = this.entries.keys().next().value;
|
|
30
94
|
if (oldest) {
|
|
31
|
-
this.
|
|
95
|
+
this.clearEntry(oldest);
|
|
32
96
|
}
|
|
33
97
|
}
|
|
34
|
-
|
|
35
|
-
this.
|
|
98
|
+
entry = { generation: 0, task: null };
|
|
99
|
+
this.entries.set(cacheKey, entry);
|
|
36
100
|
}
|
|
101
|
+
return entry;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private clearEntry(cacheKey: string): void {
|
|
105
|
+
const timer = this.reindexTimers.get(cacheKey);
|
|
106
|
+
if (timer) {
|
|
107
|
+
clearTimeout(timer);
|
|
108
|
+
this.reindexTimers.delete(cacheKey);
|
|
109
|
+
}
|
|
110
|
+
this.entries.delete(cacheKey);
|
|
111
|
+
}
|
|
37
112
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
113
|
+
private startBuild(
|
|
114
|
+
source: string,
|
|
115
|
+
ref: string | null | undefined,
|
|
116
|
+
entry: CacheEntry,
|
|
117
|
+
generationAtStart: number,
|
|
118
|
+
): Promise<MiruIndex> {
|
|
119
|
+
return (async () => {
|
|
120
|
+
const index = await MiruIndex.fromSource(source, this.content, undefined, ref);
|
|
121
|
+
if (entry.generation !== generationAtStart) {
|
|
122
|
+
throw new StaleIndexError();
|
|
123
|
+
}
|
|
124
|
+
if (!isGitUrl(source)) {
|
|
125
|
+
await index.saveToDefaultCache(resolve(source));
|
|
126
|
+
}
|
|
127
|
+
return index;
|
|
128
|
+
})();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async get(source: string, ref?: string | null): Promise<MiruIndex> {
|
|
132
|
+
const cacheKey = computeSourceCacheKey(source, ref);
|
|
133
|
+
|
|
134
|
+
for (;;) {
|
|
135
|
+
const entry = this.ensureEntry(cacheKey);
|
|
136
|
+
const generationAtStart = entry.generation;
|
|
137
|
+
|
|
138
|
+
if (!entry.task) {
|
|
139
|
+
entry.task = this.startBuild(source, ref, entry, generationAtStart);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const task = entry.task;
|
|
143
|
+
try {
|
|
144
|
+
const index = await task;
|
|
145
|
+
if (entry.generation !== generationAtStart) {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
return index;
|
|
149
|
+
} catch (err) {
|
|
150
|
+
if (entry.task === task) {
|
|
151
|
+
entry.task = null;
|
|
152
|
+
}
|
|
153
|
+
if (err instanceof StaleIndexError) {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (this.entries.get(cacheKey) === entry) {
|
|
157
|
+
this.entries.delete(cacheKey);
|
|
158
|
+
}
|
|
159
|
+
throw err;
|
|
43
160
|
}
|
|
44
|
-
throw err;
|
|
45
161
|
}
|
|
46
162
|
}
|
|
47
163
|
|
|
164
|
+
private scheduleReindex(source: string, ref?: string | null): void {
|
|
165
|
+
const cacheKey = computeSourceCacheKey(source, ref);
|
|
166
|
+
const existing = this.reindexTimers.get(cacheKey);
|
|
167
|
+
if (existing) {
|
|
168
|
+
clearTimeout(existing);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const debounceMs = resolveReindexDebounceMs();
|
|
172
|
+
const timer = setTimeout(() => {
|
|
173
|
+
this.reindexTimers.delete(cacheKey);
|
|
174
|
+
this.invalidate(cacheKey);
|
|
175
|
+
void this.get(source, ref).catch(() => undefined);
|
|
176
|
+
}, debounceMs);
|
|
177
|
+
|
|
178
|
+
this.reindexTimers.set(cacheKey, timer);
|
|
179
|
+
}
|
|
180
|
+
|
|
48
181
|
startWatcher(path: string): void {
|
|
49
182
|
const resolved = resolve(path);
|
|
50
183
|
if (this.watcher) {
|
|
51
184
|
this.watcher.close();
|
|
52
185
|
}
|
|
53
186
|
|
|
54
|
-
this.watcher = watch(resolved, { recursive: true }, () => {
|
|
55
|
-
|
|
56
|
-
|
|
187
|
+
this.watcher = watch(resolved, { recursive: true }, (_event, filename) => {
|
|
188
|
+
if (shouldIgnoreWatchPath(filename)) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
this.scheduleReindex(resolved);
|
|
57
192
|
});
|
|
58
193
|
}
|
|
59
194
|
|
|
60
195
|
close(): void {
|
|
196
|
+
for (const timer of this.reindexTimers.values()) {
|
|
197
|
+
clearTimeout(timer);
|
|
198
|
+
}
|
|
199
|
+
this.reindexTimers.clear();
|
|
61
200
|
this.watcher?.close();
|
|
62
201
|
this.watcher = null;
|
|
63
202
|
}
|
package/src/mcp/serve.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { resolve } from "node:path";
|
|
|
2
2
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
3
3
|
import type { ContentType } from "../types.ts";
|
|
4
4
|
import { isGitUrl } from "../utils.ts";
|
|
5
|
-
import { IndexCache } from "./index-cache.ts";
|
|
5
|
+
import { IndexCache, mcpWatchEnabled } from "./index-cache.ts";
|
|
6
6
|
import { createMcpServer } from "./server.ts";
|
|
7
7
|
|
|
8
8
|
export async function serveMcp(options: {
|
|
@@ -14,7 +14,7 @@ export async function serveMcp(options: {
|
|
|
14
14
|
const defaultSource = options.path ?? resolve(process.cwd());
|
|
15
15
|
|
|
16
16
|
void cache.get(defaultSource, options.ref).catch(() => undefined);
|
|
17
|
-
if (!isGitUrl(defaultSource)) {
|
|
17
|
+
if (!isGitUrl(defaultSource) && mcpWatchEnabled()) {
|
|
18
18
|
cache.startWatcher(defaultSource);
|
|
19
19
|
}
|
|
20
20
|
|