@cortexkit/pi-magic-context 0.44.4 → 0.46.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/dist/embedding-worker.js +47 -0
- package/dist/emscripten-module-0g8f45e6.js +602 -0
- package/dist/emscripten-module-0pekdjxz.js +1532 -0
- package/dist/emscripten-module-e67xwsp3.js +503 -0
- package/dist/emscripten-module-pj1vyb44.js +4559 -0
- package/dist/ffi-3cx1cxpk.js +78 -0
- package/dist/ffi-7bqqe4cd.js +91 -0
- package/dist/ffi-7e6gryj5.js +91 -0
- package/dist/ffi-bpvkebwr.js +78 -0
- package/dist/historian-calibration-extension.js +7 -4
- package/dist/index-266qrgss.js +417 -0
- package/dist/index-2p1e8706.js +30 -0
- package/dist/index-4vfrjwf6.js +7737 -0
- package/dist/{index-59dmj4zs.js → index-7n8gb6j3.js} +165 -3
- package/dist/index-901ekbga.js +244 -0
- package/dist/{module-asyncify-2EFITU5U-rkqjhjxd.js → index-g20jdh0p.js} +114 -105
- package/dist/{index-5fw91enm.js → index-n3g5mwa8.js} +1 -1
- package/dist/index-phh7e055.js +290 -0
- package/dist/index-q1yc6bs0.js +104 -0
- package/dist/index-sg8zbjs0.js +8937 -0
- package/dist/index-td885aah.js +576 -0
- package/dist/index-vf38qhee.js +150 -0
- package/dist/{index-5am7amy4.js → index-vhzmjpsk.js} +10220 -25155
- package/dist/{index-n8kg008j.js → index-w76x3af7.js} +28 -18
- package/dist/index-wv28nczk.js +464 -0
- package/dist/{index-npagr1cy.js → index-xvw8zptx.js} +4021 -3973
- package/dist/index.js +6942 -2942
- package/dist/{message-time-backfill-5yrp95cx.js → message-time-backfill-4688z1k2.js} +19 -4
- package/dist/migration-worker.js +56 -0
- package/dist/module-ES6BEMUI-3cet1pyp.js +12 -0
- package/dist/module-asyncify-2EFITU5U-c2razzd9.js +6 -0
- package/dist/{read-session-chunk-w493f72c.js → read-session-chunk-tc6yn2pe.js} +32 -5
- package/dist/rpc-notifications-gm1mdpwh.js +16 -0
- package/dist/{safe-notification-target-y3hg2m7p.js → safe-notification-target-6cn3z99t.js} +2 -2
- package/dist/subagent-entry.js +15 -6
- package/package.json +7 -6
- package/dist/index-97e5fzy3.js +0 -16
- package/dist/index-b3eqj1g6.js +0 -51
- package/dist/index-rn7gafjr.js +0 -8
- package/dist/index-v6t0tg85.js +0 -13
- package/dist/rpc-notifications-jxy30tgz.js +0 -8
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import {
|
|
2
|
+
log
|
|
3
|
+
} from "./index-7n8gb6j3.js";
|
|
4
|
+
|
|
5
|
+
// ../plugin/src/shared/internal-initiator-marker.ts
|
|
6
|
+
var OMO_INTERNAL_INITIATOR_MARKER = "<!-- OMO_INTERNAL_INITIATOR -->";
|
|
7
|
+
|
|
8
|
+
// ../plugin/src/shared/system-directive.ts
|
|
9
|
+
var SYSTEM_DIRECTIVE_PREFIX = "[SYSTEM DIRECTIVE: MAGIC-CONTEXT";
|
|
10
|
+
function isSystemDirective(text) {
|
|
11
|
+
return text.trimStart().startsWith(SYSTEM_DIRECTIVE_PREFIX);
|
|
12
|
+
}
|
|
13
|
+
function removeSystemReminders(text) {
|
|
14
|
+
return text.replace(/<system-reminder>[\s\S]*?<\/system-reminder>/gi, "").trim();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// ../plugin/src/hooks/magic-context/read-session-formatting.ts
|
|
18
|
+
import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
19
|
+
import { createRequire } from "node:module";
|
|
20
|
+
import { homedir } from "node:os";
|
|
21
|
+
import { dirname, join, resolve } from "node:path";
|
|
22
|
+
import { pathToFileURL } from "node:url";
|
|
23
|
+
|
|
24
|
+
// ../plugin/src/shared/commit-detection.ts
|
|
25
|
+
var HASH_HEX = "[0-9a-f]{7,12}";
|
|
26
|
+
var COMMIT_HASH_TEST_PATTERN = new RegExp(`\\b${HASH_HEX}\\b`, "i");
|
|
27
|
+
var COMMIT_VERB_PATTERN = /\b(?:commit(?:ted|ting|s)?|cherry-?pick(?:ed|ing|s)?|merge[ds]?|merging|rebas(?:e|ed|es|ing))\b/i;
|
|
28
|
+
function textMentionsRecentCommit(text) {
|
|
29
|
+
return COMMIT_HASH_TEST_PATTERN.test(text) && COMMIT_VERB_PATTERN.test(text);
|
|
30
|
+
}
|
|
31
|
+
function createCommitHashExtractPattern() {
|
|
32
|
+
return new RegExp(`\`?\\b(${HASH_HEX})\\b\`?`, "gi");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ../plugin/src/hooks/magic-context/read-session-formatting.ts
|
|
36
|
+
var MAX_COMMITS_PER_BLOCK = 5;
|
|
37
|
+
function hasMeaningfulUserText2(parts) {
|
|
38
|
+
for (const part of parts) {
|
|
39
|
+
if (part === null || typeof part !== "object")
|
|
40
|
+
continue;
|
|
41
|
+
const candidate = part;
|
|
42
|
+
if (candidate.type !== "text" || typeof candidate.text !== "string")
|
|
43
|
+
continue;
|
|
44
|
+
if (candidate.ignored === true)
|
|
45
|
+
continue;
|
|
46
|
+
const cleaned = removeSystemReminders(candidate.text).replace(OMO_INTERNAL_INITIATOR_MARKER, "").trim();
|
|
47
|
+
if (!cleaned)
|
|
48
|
+
continue;
|
|
49
|
+
if (isSystemDirective(cleaned))
|
|
50
|
+
continue;
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
function extractTexts2(parts) {
|
|
56
|
+
const texts = [];
|
|
57
|
+
for (const part of parts) {
|
|
58
|
+
if (part === null || typeof part !== "object")
|
|
59
|
+
continue;
|
|
60
|
+
const p = part;
|
|
61
|
+
if (p.type === "text" && typeof p.text === "string" && p.text.trim().length > 0) {
|
|
62
|
+
texts.push(p.text.trim());
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return texts;
|
|
66
|
+
}
|
|
67
|
+
function extractToolResultBodyTokens(parts) {
|
|
68
|
+
let tokens = 0;
|
|
69
|
+
for (const part of parts) {
|
|
70
|
+
if (part === null || typeof part !== "object")
|
|
71
|
+
continue;
|
|
72
|
+
const p = part;
|
|
73
|
+
if (p.type !== "tool")
|
|
74
|
+
continue;
|
|
75
|
+
const state = p.state;
|
|
76
|
+
if (!state || typeof state !== "object")
|
|
77
|
+
continue;
|
|
78
|
+
const body = state.output ?? state.error;
|
|
79
|
+
if (body === undefined)
|
|
80
|
+
continue;
|
|
81
|
+
const text = typeof body === "string" ? body : JSON.stringify(body);
|
|
82
|
+
tokens += Math.ceil(text.length / 4);
|
|
83
|
+
}
|
|
84
|
+
return tokens;
|
|
85
|
+
}
|
|
86
|
+
function extractToolCallSummaries(parts) {
|
|
87
|
+
const summaries = [];
|
|
88
|
+
for (const part of parts) {
|
|
89
|
+
if (part === null || typeof part !== "object")
|
|
90
|
+
continue;
|
|
91
|
+
const p = part;
|
|
92
|
+
if (p.type !== "tool" || typeof p.tool !== "string")
|
|
93
|
+
continue;
|
|
94
|
+
const state = p.state;
|
|
95
|
+
if (!state || typeof state !== "object")
|
|
96
|
+
continue;
|
|
97
|
+
const input = state.input;
|
|
98
|
+
const metadata = state.metadata;
|
|
99
|
+
const description = input && typeof input.description === "string" && input.description || metadata && typeof metadata.description === "string" && metadata.description;
|
|
100
|
+
if (description) {
|
|
101
|
+
summaries.push(`TC: ${description}`);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const toolName = p.tool;
|
|
105
|
+
const keyArg = extractKeyArg(toolName, input);
|
|
106
|
+
summaries.push(keyArg ? `TC: ${toolName}(${keyArg})` : `TC: ${toolName}`);
|
|
107
|
+
}
|
|
108
|
+
return summaries;
|
|
109
|
+
}
|
|
110
|
+
function extractKeyArg(_toolName, input) {
|
|
111
|
+
if (!input)
|
|
112
|
+
return null;
|
|
113
|
+
if (typeof input.filePath === "string")
|
|
114
|
+
return truncateArg(input.filePath);
|
|
115
|
+
if (typeof input.path === "string")
|
|
116
|
+
return truncateArg(input.path);
|
|
117
|
+
if (typeof input.pattern === "string")
|
|
118
|
+
return truncateArg(input.pattern);
|
|
119
|
+
if (typeof input.query === "string")
|
|
120
|
+
return truncateArg(input.query);
|
|
121
|
+
if (typeof input.symbol === "string")
|
|
122
|
+
return input.symbol;
|
|
123
|
+
if (typeof input.module === "string")
|
|
124
|
+
return input.module;
|
|
125
|
+
if (typeof input.action === "string")
|
|
126
|
+
return input.action;
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
function truncateArg(value, maxLen = 60) {
|
|
130
|
+
if (value.length <= maxLen)
|
|
131
|
+
return value;
|
|
132
|
+
return `${value.slice(0, maxLen)}…`;
|
|
133
|
+
}
|
|
134
|
+
var TOKENIZER_PACKAGE_DIRS = [
|
|
135
|
+
["@cortexkit", "opencode-magic-context"],
|
|
136
|
+
["@cortexkit", "pi-magic-context"]
|
|
137
|
+
];
|
|
138
|
+
var tokenizer;
|
|
139
|
+
var tokenizerLoadAttempted = false;
|
|
140
|
+
var tokenizerLoadPromise;
|
|
141
|
+
var tokenizerWarningSent = false;
|
|
142
|
+
var tokenizerEncodingPath;
|
|
143
|
+
var tokenizerSerializedTableBytes;
|
|
144
|
+
function tokenizerPackageRoots() {
|
|
145
|
+
const cwd = process.cwd();
|
|
146
|
+
const openCodeCache = join(process.env.XDG_CACHE_HOME ?? join(homedir(), ".cache"), "opencode");
|
|
147
|
+
const roots = [cwd, openCodeCache];
|
|
148
|
+
const candidates = [];
|
|
149
|
+
for (const root of roots) {
|
|
150
|
+
for (const packageDir of TOKENIZER_PACKAGE_DIRS) {
|
|
151
|
+
candidates.push(join(root, "node_modules", ...packageDir, "node_modules", "ai-tokenizer"));
|
|
152
|
+
}
|
|
153
|
+
candidates.push(join(root, "node_modules", "ai-tokenizer"));
|
|
154
|
+
}
|
|
155
|
+
let ancestor = process.argv[1] ? dirname(resolve(process.argv[1])) : cwd;
|
|
156
|
+
while (true) {
|
|
157
|
+
candidates.push(join(ancestor, "node_modules", "ai-tokenizer"));
|
|
158
|
+
const parent = dirname(ancestor);
|
|
159
|
+
if (parent === ancestor)
|
|
160
|
+
break;
|
|
161
|
+
ancestor = parent;
|
|
162
|
+
}
|
|
163
|
+
return [...new Set(candidates)];
|
|
164
|
+
}
|
|
165
|
+
function packageImportTarget(value) {
|
|
166
|
+
if (typeof value === "string")
|
|
167
|
+
return value;
|
|
168
|
+
if (!value || typeof value !== "object")
|
|
169
|
+
return;
|
|
170
|
+
const conditions = value;
|
|
171
|
+
return packageImportTarget(conditions.import) ?? packageImportTarget(conditions.default);
|
|
172
|
+
}
|
|
173
|
+
function findTokenizerImportPaths() {
|
|
174
|
+
for (const packageRoot of tokenizerPackageRoots()) {
|
|
175
|
+
const packageJsonPath = join(packageRoot, "package.json");
|
|
176
|
+
if (!existsSync(packageJsonPath))
|
|
177
|
+
continue;
|
|
178
|
+
try {
|
|
179
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8"));
|
|
180
|
+
const tokenizerTarget = packageImportTarget(packageJson.exports?.["."]) ?? (typeof packageJson.module === "string" ? packageJson.module : undefined) ?? (typeof packageJson.main === "string" ? packageJson.main : undefined);
|
|
181
|
+
const encodingTarget = packageImportTarget(packageJson.exports?.["./encoding/claude"]);
|
|
182
|
+
if (!tokenizerTarget || !encodingTarget)
|
|
183
|
+
continue;
|
|
184
|
+
return {
|
|
185
|
+
tokenizerPath: realpathSync(join(packageRoot, tokenizerTarget)),
|
|
186
|
+
encodingPath: realpathSync(join(packageRoot, encodingTarget))
|
|
187
|
+
};
|
|
188
|
+
} catch {}
|
|
189
|
+
}
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
function constructTokenizer(tokenizerModule, claudeEncoding) {
|
|
193
|
+
const typedModule = tokenizerModule;
|
|
194
|
+
const Tokenizer = typedModule.default ?? typedModule.Tokenizer;
|
|
195
|
+
if (!Tokenizer) {
|
|
196
|
+
throw new Error("ai-tokenizer does not expose a Tokenizer constructor");
|
|
197
|
+
}
|
|
198
|
+
return new Tokenizer(claudeEncoding);
|
|
199
|
+
}
|
|
200
|
+
function loadTokenizer() {
|
|
201
|
+
const requireFromThisModule = createRequire(import.meta.url);
|
|
202
|
+
const encodingSpecifier = "ai-tokenizer/encoding/" + "claude";
|
|
203
|
+
tokenizerEncodingPath = requireFromThisModule.resolve(encodingSpecifier);
|
|
204
|
+
tokenizerSerializedTableBytes = undefined;
|
|
205
|
+
return constructTokenizer(requireFromThisModule("ai-" + "tokenizer"), requireFromThisModule(encodingSpecifier));
|
|
206
|
+
}
|
|
207
|
+
async function loadTokenizerFromInstalledPackage() {
|
|
208
|
+
const installedPaths = findTokenizerImportPaths();
|
|
209
|
+
if (!installedPaths) {
|
|
210
|
+
throw new Error("ai-tokenizer was not found under the project, runtime, or OpenCode cache node_modules roots");
|
|
211
|
+
}
|
|
212
|
+
const [tokenizerModule, claudeEncoding] = await Promise.all([
|
|
213
|
+
import(pathToFileURL(installedPaths.tokenizerPath).href),
|
|
214
|
+
import(pathToFileURL(installedPaths.encodingPath).href)
|
|
215
|
+
]);
|
|
216
|
+
tokenizerEncodingPath = installedPaths.encodingPath;
|
|
217
|
+
tokenizerSerializedTableBytes = undefined;
|
|
218
|
+
return constructTokenizer(tokenizerModule, claudeEncoding);
|
|
219
|
+
}
|
|
220
|
+
function warnTokenizerFallback(error) {
|
|
221
|
+
if (tokenizerWarningSent)
|
|
222
|
+
return;
|
|
223
|
+
tokenizerWarningSent = true;
|
|
224
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
225
|
+
log("[magic-context] ai-tokenizer is unavailable; using approximate character-based token counts for this process. Token budgets, persisted per-message counts, and protected-tail/compartment boundaries may be less accurate until restart:", reason);
|
|
226
|
+
}
|
|
227
|
+
async function preloadTokenizer() {
|
|
228
|
+
if (tokenizer)
|
|
229
|
+
return true;
|
|
230
|
+
if (tokenizerLoadAttempted)
|
|
231
|
+
return false;
|
|
232
|
+
if (tokenizerLoadPromise)
|
|
233
|
+
return tokenizerLoadPromise;
|
|
234
|
+
tokenizerLoadPromise = (async () => {
|
|
235
|
+
try {
|
|
236
|
+
try {
|
|
237
|
+
tokenizer = loadTokenizer();
|
|
238
|
+
} catch {
|
|
239
|
+
tokenizer = await loadTokenizerFromInstalledPackage();
|
|
240
|
+
}
|
|
241
|
+
tokenizerLoadAttempted = true;
|
|
242
|
+
return true;
|
|
243
|
+
} catch (error) {
|
|
244
|
+
tokenizerLoadAttempted = true;
|
|
245
|
+
warnTokenizerFallback(error);
|
|
246
|
+
return false;
|
|
247
|
+
} finally {
|
|
248
|
+
tokenizerLoadPromise = undefined;
|
|
249
|
+
}
|
|
250
|
+
})();
|
|
251
|
+
return tokenizerLoadPromise;
|
|
252
|
+
}
|
|
253
|
+
function getTokenizer() {
|
|
254
|
+
if (tokenizer || tokenizerLoadAttempted)
|
|
255
|
+
return tokenizer;
|
|
256
|
+
tokenizerLoadAttempted = true;
|
|
257
|
+
try {
|
|
258
|
+
tokenizer = loadTokenizer();
|
|
259
|
+
} catch (error) {
|
|
260
|
+
warnTokenizerFallback(error);
|
|
261
|
+
}
|
|
262
|
+
return tokenizer;
|
|
263
|
+
}
|
|
264
|
+
function estimateTokensHeuristically(text) {
|
|
265
|
+
return Math.ceil(text.length / 3.5);
|
|
266
|
+
}
|
|
267
|
+
var TOKEN_ESTIMATOR_SAMPLE = 'Coverage check: const windows = chunk(text, 0x1f, 512); // ok? "naïve" café 日本語 <EOT> 3.14159 -> done.';
|
|
268
|
+
var estimatorFingerprintTokenizer;
|
|
269
|
+
var estimatorFingerprint = "";
|
|
270
|
+
function getTokenEstimatorFingerprint() {
|
|
271
|
+
const activeTokenizer = getTokenizer();
|
|
272
|
+
if (!activeTokenizer) {
|
|
273
|
+
return `heuristic:${estimateTokensHeuristically(TOKEN_ESTIMATOR_SAMPLE)}`;
|
|
274
|
+
}
|
|
275
|
+
if (estimatorFingerprintTokenizer === activeTokenizer)
|
|
276
|
+
return estimatorFingerprint;
|
|
277
|
+
let fingerprint;
|
|
278
|
+
try {
|
|
279
|
+
fingerprint = `tokenizer:${activeTokenizer.encode(TOKEN_ESTIMATOR_SAMPLE, "all").length}`;
|
|
280
|
+
} catch {
|
|
281
|
+
fingerprint = "tokenizer:sample-unencodable";
|
|
282
|
+
}
|
|
283
|
+
estimatorFingerprintTokenizer = activeTokenizer;
|
|
284
|
+
estimatorFingerprint = fingerprint;
|
|
285
|
+
return fingerprint;
|
|
286
|
+
}
|
|
287
|
+
function hasTokenizerForFit() {
|
|
288
|
+
return getTokenizer() !== undefined;
|
|
289
|
+
}
|
|
290
|
+
function tokenCountUsesByteBound(text) {
|
|
291
|
+
return text.length > 1024 * 1024 || /[\p{L}\p{N}]{16385}/u.test(text);
|
|
292
|
+
}
|
|
293
|
+
function estimateTokens(text) {
|
|
294
|
+
if (!text)
|
|
295
|
+
return 0;
|
|
296
|
+
const activeTokenizer = getTokenizer();
|
|
297
|
+
if (!activeTokenizer)
|
|
298
|
+
return estimateTokensHeuristically(text);
|
|
299
|
+
try {
|
|
300
|
+
return activeTokenizer.encode(text, "all").length;
|
|
301
|
+
} catch (error) {
|
|
302
|
+
tokenizer = undefined;
|
|
303
|
+
tokenizerLoadAttempted = true;
|
|
304
|
+
warnTokenizerFallback(error);
|
|
305
|
+
return estimateTokensHeuristically(text);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function createTokenCountMemo(maxEntries, maxBytes) {
|
|
309
|
+
const counts = new Map;
|
|
310
|
+
let bytes = 0;
|
|
311
|
+
return (text) => {
|
|
312
|
+
const cached = counts.get(text);
|
|
313
|
+
if (cached && cached.tokenizer === getTokenizer())
|
|
314
|
+
return cached.tokens;
|
|
315
|
+
const tokens = estimateTokens(text);
|
|
316
|
+
if (cached) {
|
|
317
|
+
bytes -= text.length * 2;
|
|
318
|
+
counts.delete(text);
|
|
319
|
+
}
|
|
320
|
+
if (text.length * 2 <= maxBytes) {
|
|
321
|
+
while (counts.size >= maxEntries || bytes + text.length * 2 > maxBytes) {
|
|
322
|
+
const oldest = counts.keys().next().value;
|
|
323
|
+
if (oldest === undefined)
|
|
324
|
+
break;
|
|
325
|
+
bytes -= oldest.length * 2;
|
|
326
|
+
counts.delete(oldest);
|
|
327
|
+
}
|
|
328
|
+
counts.set(text, { tokenizer: getTokenizer(), tokens });
|
|
329
|
+
bytes += text.length * 2;
|
|
330
|
+
}
|
|
331
|
+
return tokens;
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
var promptTokenCounts = new Map;
|
|
335
|
+
var promptTokenCountBytes = 0;
|
|
336
|
+
var PROMPT_TOKEN_COUNT_BYTES = 8 * 1024 * 1024;
|
|
337
|
+
function estimateFixedPromptTokens(text) {
|
|
338
|
+
const activeTokenizer = getTokenizer();
|
|
339
|
+
const cached = promptTokenCounts.get(text);
|
|
340
|
+
if (cached && cached.tokenizer === activeTokenizer) {
|
|
341
|
+
promptTokenCounts.delete(text);
|
|
342
|
+
promptTokenCounts.set(text, cached);
|
|
343
|
+
return cached.tokens;
|
|
344
|
+
}
|
|
345
|
+
const tokens = estimateTokens(text);
|
|
346
|
+
if (cached) {
|
|
347
|
+
promptTokenCountBytes -= text.length * 2;
|
|
348
|
+
promptTokenCounts.delete(text);
|
|
349
|
+
}
|
|
350
|
+
if (text.length * 2 <= PROMPT_TOKEN_COUNT_BYTES) {
|
|
351
|
+
while (promptTokenCounts.size >= 64 || promptTokenCountBytes + text.length * 2 > PROMPT_TOKEN_COUNT_BYTES) {
|
|
352
|
+
const oldest = promptTokenCounts.keys().next().value;
|
|
353
|
+
if (oldest === undefined)
|
|
354
|
+
break;
|
|
355
|
+
promptTokenCountBytes -= oldest.length * 2;
|
|
356
|
+
promptTokenCounts.delete(oldest);
|
|
357
|
+
}
|
|
358
|
+
promptTokenCounts.set(text, { tokenizer: getTokenizer(), tokens });
|
|
359
|
+
promptTokenCountBytes += text.length * 2;
|
|
360
|
+
}
|
|
361
|
+
return tokens;
|
|
362
|
+
}
|
|
363
|
+
function normalizeText(text) {
|
|
364
|
+
return text.replace(/\s+/g, " ").trim();
|
|
365
|
+
}
|
|
366
|
+
function compactRole(role) {
|
|
367
|
+
if (role === "assistant")
|
|
368
|
+
return "A";
|
|
369
|
+
if (role === "user")
|
|
370
|
+
return "U";
|
|
371
|
+
return role.slice(0, 1).toUpperCase() || "M";
|
|
372
|
+
}
|
|
373
|
+
function formatBlock(block) {
|
|
374
|
+
const range = block.startOrdinal === block.endOrdinal ? `[${block.startOrdinal}]` : `[${block.startOrdinal}-${block.endOrdinal}]`;
|
|
375
|
+
const commitSuffix = block.commitHashes.length > 0 ? ` commits: ${block.commitHashes.join(", ")}` : "";
|
|
376
|
+
return `${range} ${block.role}:${commitSuffix} ${block.parts.join(" / ")}`;
|
|
377
|
+
}
|
|
378
|
+
function extractCommitHashes(text) {
|
|
379
|
+
const hashes = [];
|
|
380
|
+
const seen = new Set;
|
|
381
|
+
for (const match of text.matchAll(createCommitHashExtractPattern())) {
|
|
382
|
+
const hash = match[1]?.toLowerCase();
|
|
383
|
+
if (!hash || seen.has(hash))
|
|
384
|
+
continue;
|
|
385
|
+
seen.add(hash);
|
|
386
|
+
hashes.push(hash);
|
|
387
|
+
if (hashes.length >= MAX_COMMITS_PER_BLOCK)
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
390
|
+
return hashes;
|
|
391
|
+
}
|
|
392
|
+
function compactTextForSummary(text, role) {
|
|
393
|
+
const commitHashes = role === "assistant" ? extractCommitHashes(text) : [];
|
|
394
|
+
if (commitHashes.length === 0 || !COMMIT_VERB_PATTERN.test(text)) {
|
|
395
|
+
return { text, commitHashes };
|
|
396
|
+
}
|
|
397
|
+
const withoutHashes = text.replace(createCommitHashExtractPattern(), "").replace(/\(\s*\)/g, "").replace(/\s+,/g, ",").replace(/,\s*,+/g, ", ").replace(/\s{2,}/g, " ").replace(/\s+([,.;:])/g, "$1").trim();
|
|
398
|
+
return {
|
|
399
|
+
text: withoutHashes.length > 0 ? withoutHashes : text,
|
|
400
|
+
commitHashes
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
function mergeCommitHashes(existing, next) {
|
|
404
|
+
if (next.length === 0)
|
|
405
|
+
return existing;
|
|
406
|
+
const merged = [...existing];
|
|
407
|
+
for (const hash of next) {
|
|
408
|
+
if (merged.includes(hash))
|
|
409
|
+
continue;
|
|
410
|
+
merged.push(hash);
|
|
411
|
+
if (merged.length >= MAX_COMMITS_PER_BLOCK)
|
|
412
|
+
break;
|
|
413
|
+
}
|
|
414
|
+
return merged;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export { textMentionsRecentCommit, OMO_INTERNAL_INITIATOR_MARKER, isSystemDirective, removeSystemReminders, hasMeaningfulUserText2, extractTexts2, extractToolResultBodyTokens, extractToolCallSummaries, preloadTokenizer, getTokenEstimatorFingerprint, hasTokenizerForFit, tokenCountUsesByteBound, estimateTokens, createTokenCountMemo, estimateFixedPromptTokens, normalizeText, compactRole, formatBlock, compactTextForSummary, mergeCommitHashes };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {
|
|
2
|
+
log
|
|
3
|
+
} from "./index-7n8gb6j3.js";
|
|
4
|
+
|
|
5
|
+
// ../plugin/src/shared/write-transaction-timing.ts
|
|
6
|
+
var SLOW_WRITE_TRANSACTION_THRESHOLD_MS = 1000;
|
|
7
|
+
function logSlowWriteTransaction(site, startedAt, thresholdMs = SLOW_WRITE_TRANSACTION_THRESHOLD_MS, completedAtMs = performance.now(), steps) {
|
|
8
|
+
try {
|
|
9
|
+
const durationMs = completedAtMs - startedAt;
|
|
10
|
+
if (durationMs < thresholdMs)
|
|
11
|
+
return;
|
|
12
|
+
const detail = steps ? Object.entries(steps).map(([name, ms]) => `${name}=${ms.toFixed(1)}ms`).join(" ") : "";
|
|
13
|
+
log(`[magic-context] slow write transaction: site=${site} held=${durationMs.toFixed(1)}ms${detail ? ` ${detail}` : ""}`);
|
|
14
|
+
} catch {}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class WriteTransactionSteps {
|
|
18
|
+
durations = {};
|
|
19
|
+
last = performance.now();
|
|
20
|
+
mark(name) {
|
|
21
|
+
const now = performance.now();
|
|
22
|
+
this.durations[name] = now - this.last;
|
|
23
|
+
this.last = now;
|
|
24
|
+
}
|
|
25
|
+
reset() {
|
|
26
|
+
this.last = performance.now();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { logSlowWriteTransaction, WriteTransactionSteps };
|