@cortexkit/opencode-magic-context 0.34.2 → 0.35.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/README.md +3 -1
- package/dist/agents/magic-context-prompt.d.ts +2 -1
- package/dist/agents/magic-context-prompt.d.ts.map +1 -1
- package/dist/config/index.d.ts +3 -0
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/project-security.d.ts +3 -0
- package/dist/config/project-security.d.ts.map +1 -1
- package/dist/config/schema/magic-context.d.ts +42 -1
- package/dist/config/schema/magic-context.d.ts.map +1 -1
- package/dist/{conflict-warning-hook-fvpfvt0w.js → conflict-warning-hook-p2pmhvn7.js} +2 -1
- package/dist/features/magic-context/context-authority.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/provider-output-failure.d.ts +15 -0
- package/dist/features/magic-context/dreamer/provider-output-failure.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/task-executor.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/verify.d.ts.map +1 -1
- package/dist/features/magic-context/mural/compress-cues.d.ts +6 -2
- package/dist/features/magic-context/mural/compress-cues.d.ts.map +1 -1
- package/dist/features/magic-context/mural/storage-mural-cues.d.ts +4 -4
- package/dist/hooks/magic-context/compartment-runner-historian.d.ts +2 -14
- package/dist/hooks/magic-context/compartment-runner-historian.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-runner-incremental.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-runner-validation.d.ts +16 -2
- package/dist/hooks/magic-context/compartment-runner-validation.d.ts.map +1 -1
- package/dist/hooks/magic-context/event-resolvers.d.ts.map +1 -1
- package/dist/hooks/magic-context/hook.d.ts +10 -3
- package/dist/hooks/magic-context/hook.d.ts.map +1 -1
- package/dist/hooks/magic-context/module-state-sync.d.ts +1 -1
- package/dist/hooks/magic-context/module-state-sync.d.ts.map +1 -1
- package/dist/hooks/magic-context/module-transport.d.ts +2 -1
- package/dist/hooks/magic-context/module-transport.d.ts.map +1 -1
- package/dist/hooks/magic-context/protected-tail-boundary.d.ts.map +1 -1
- package/dist/hooks/magic-context/read-session-chunk.d.ts +5 -0
- package/dist/hooks/magic-context/read-session-chunk.d.ts.map +1 -1
- package/dist/hooks/magic-context/read-session-true-raw-tokens.d.ts +2 -0
- package/dist/hooks/magic-context/read-session-true-raw-tokens.d.ts.map +1 -1
- package/dist/hooks/magic-context/rust-mode-transform.d.ts +6 -0
- package/dist/hooks/magic-context/rust-mode-transform.d.ts.map +1 -1
- package/dist/hooks/magic-context/system-prompt-hash.d.ts +19 -5
- package/dist/hooks/magic-context/system-prompt-hash.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform.d.ts +3 -0
- package/dist/hooks/magic-context/transform.d.ts.map +1 -1
- package/dist/index-0c9dmw9m.js +116 -0
- package/dist/{index-z70pxrq1.js → index-4anesz49.js} +726 -611
- package/dist/{index-dtas91s5.js → index-8aaadh98.js} +7 -119
- package/dist/{index-b563sga5.js → index-h8mk7vpa.js} +541 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +796 -809
- package/dist/plugin/hooks/create-session-hooks.d.ts +12 -0
- package/dist/plugin/hooks/create-session-hooks.d.ts.map +1 -1
- package/dist/plugin/rpc-handlers.d.ts +1 -0
- package/dist/plugin/rpc-handlers.d.ts.map +1 -1
- package/dist/plugin/tool-registry.d.ts +4 -0
- package/dist/plugin/tool-registry.d.ts.map +1 -1
- package/dist/{recomp-orchestrator-6cd3nrt7.js → recomp-orchestrator-zc915ceq.js} +3 -2
- package/dist/shared/index.d.ts +2 -0
- package/dist/shared/index.d.ts.map +1 -1
- package/dist/shared/prompt-surface-runtime.d.ts +43 -0
- package/dist/shared/prompt-surface-runtime.d.ts.map +1 -0
- package/dist/shared/prompt-surface.d.ts +49 -0
- package/dist/shared/prompt-surface.d.ts.map +1 -0
- package/dist/{storage-meta-session-xt41zbw4.js → storage-meta-session-53cjs6mr.js} +1 -1
- package/dist/tools/ctx-expand/tools.d.ts +1 -0
- package/dist/tools/ctx-expand/tools.d.ts.map +1 -1
- package/dist/tools/ctx-memory/tools.d.ts +1 -0
- package/dist/tools/ctx-memory/tools.d.ts.map +1 -1
- package/dist/tools/ctx-note/tools.d.ts +1 -0
- package/dist/tools/ctx-note/tools.d.ts.map +1 -1
- package/dist/tools/ctx-reduce/tools.d.ts +1 -0
- package/dist/tools/ctx-reduce/tools.d.ts.map +1 -1
- package/dist/tools/ctx-search/tools.d.ts +1 -0
- package/dist/tools/ctx-search/tools.d.ts.map +1 -1
- package/dist/tools/light-descriptions.d.ts +6 -0
- package/dist/tools/light-descriptions.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/agents/magic-context-prompt.test.ts +144 -0
- package/src/agents/magic-context-prompt.ts +53 -2
- package/src/config/index.test.ts +53 -0
- package/src/config/index.ts +4 -0
- package/src/config/project-security.test.ts +20 -0
- package/src/config/project-security.ts +23 -0
- package/src/config/schema/magic-context.test.ts +58 -0
- package/src/config/schema/magic-context.ts +61 -1
- package/src/shared/index.ts +2 -0
- package/src/shared/prompt-surface-a1-golden.md +458 -0
- package/src/shared/prompt-surface-runtime.test.ts +141 -0
- package/src/shared/prompt-surface-runtime.ts +250 -0
- package/src/shared/prompt-surface.test.ts +109 -0
- package/src/shared/prompt-surface.ts +138 -0
|
@@ -132986,6 +132986,543 @@ function readRawSessionMessageByIdFromDb(db, sessionId, messageId) {
|
|
|
132986
132986
|
};
|
|
132987
132987
|
}
|
|
132988
132988
|
|
|
132989
|
+
// src/shared/record-type-guard.ts
|
|
132990
|
+
function isRecord(value) {
|
|
132991
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
132992
|
+
}
|
|
132993
|
+
|
|
132994
|
+
// src/shared/stable-json.ts
|
|
132995
|
+
function stableStringify(value, seen = new WeakSet) {
|
|
132996
|
+
if (value === undefined)
|
|
132997
|
+
return "undefined";
|
|
132998
|
+
if (value === null || typeof value !== "object")
|
|
132999
|
+
return JSON.stringify(value) ?? String(value);
|
|
133000
|
+
if (seen.has(value))
|
|
133001
|
+
return '"[Circular]"';
|
|
133002
|
+
seen.add(value);
|
|
133003
|
+
if (Array.isArray(value)) {
|
|
133004
|
+
return `[${value.map((item) => stableStringify(item, seen)).join(",")}]`;
|
|
133005
|
+
}
|
|
133006
|
+
const entries = Object.entries(value).sort(([a], [b]) => {
|
|
133007
|
+
if (a < b)
|
|
133008
|
+
return -1;
|
|
133009
|
+
if (a > b)
|
|
133010
|
+
return 1;
|
|
133011
|
+
return 0;
|
|
133012
|
+
});
|
|
133013
|
+
return `{${entries.map(([key, child]) => `${JSON.stringify(key)}:${stableStringify(child, seen)}`).join(",")}}`;
|
|
133014
|
+
}
|
|
133015
|
+
|
|
133016
|
+
// src/hooks/magic-context/read-session-true-raw-tokens.ts
|
|
133017
|
+
function completedToolArcCrossesBoundary(invOrdinal, resOrdinal, boundary) {
|
|
133018
|
+
return invOrdinal < boundary && boundary <= resOrdinal;
|
|
133019
|
+
}
|
|
133020
|
+
var MAX_MESSAGE_CACHE_ENTRIES = 1e5;
|
|
133021
|
+
var MAX_MESSAGE_CACHE_KEY_BYTES = 64 * 1024 * 1024;
|
|
133022
|
+
var FNV1A_32_OFFSET = 2166136261;
|
|
133023
|
+
var FNV1A_32_PRIME = 16777619;
|
|
133024
|
+
var messageEstimateCache = new Map;
|
|
133025
|
+
var messageEstimateCacheBytes = 0;
|
|
133026
|
+
var EMPTY_BREAKDOWN = {
|
|
133027
|
+
text: 0,
|
|
133028
|
+
reasoning: 0,
|
|
133029
|
+
toolInput: 0,
|
|
133030
|
+
toolOutput: 0,
|
|
133031
|
+
image: 0,
|
|
133032
|
+
other: 0,
|
|
133033
|
+
total: 0
|
|
133034
|
+
};
|
|
133035
|
+
function addBreakdown(target, kind, value) {
|
|
133036
|
+
if (kind === "total")
|
|
133037
|
+
return;
|
|
133038
|
+
const safeValue = Number.isFinite(value) && value > 0 ? Math.round(value) : 0;
|
|
133039
|
+
target[kind] += safeValue;
|
|
133040
|
+
target.total += safeValue;
|
|
133041
|
+
}
|
|
133042
|
+
function estimateStructured(value) {
|
|
133043
|
+
if (typeof value === "string")
|
|
133044
|
+
return estimateTokens(value);
|
|
133045
|
+
if (value === undefined || value === null)
|
|
133046
|
+
return 0;
|
|
133047
|
+
return estimateTokens(stableStringify(value));
|
|
133048
|
+
}
|
|
133049
|
+
function firstStringField(record, fields) {
|
|
133050
|
+
for (const field of fields) {
|
|
133051
|
+
const value = record[field];
|
|
133052
|
+
if (typeof value === "string" && value.length > 0)
|
|
133053
|
+
return value;
|
|
133054
|
+
}
|
|
133055
|
+
return null;
|
|
133056
|
+
}
|
|
133057
|
+
function stringValue(value) {
|
|
133058
|
+
if (typeof value === "string")
|
|
133059
|
+
return value;
|
|
133060
|
+
if (value === undefined || value === null)
|
|
133061
|
+
return "";
|
|
133062
|
+
return stableStringify(value);
|
|
133063
|
+
}
|
|
133064
|
+
function textFromToolResultContent(content) {
|
|
133065
|
+
if (typeof content === "string")
|
|
133066
|
+
return content;
|
|
133067
|
+
if (Array.isArray(content)) {
|
|
133068
|
+
const pieces = [];
|
|
133069
|
+
for (const entry of content) {
|
|
133070
|
+
if (typeof entry === "string") {
|
|
133071
|
+
pieces.push(entry);
|
|
133072
|
+
} else if (isRecord(entry)) {
|
|
133073
|
+
const text = firstStringField(entry, ["text", "content", "value"]);
|
|
133074
|
+
pieces.push(text ?? stableStringify(entry));
|
|
133075
|
+
} else if (entry !== null && entry !== undefined) {
|
|
133076
|
+
pieces.push(String(entry));
|
|
133077
|
+
}
|
|
133078
|
+
}
|
|
133079
|
+
return pieces.join(`
|
|
133080
|
+
`);
|
|
133081
|
+
}
|
|
133082
|
+
return stringValue(content);
|
|
133083
|
+
}
|
|
133084
|
+
function looksImageLike(part) {
|
|
133085
|
+
const type = typeof part.type === "string" ? part.type.toLowerCase() : "";
|
|
133086
|
+
const mime = typeof part.mime === "string" ? part.mime.toLowerCase() : "";
|
|
133087
|
+
const mediaType = typeof part.mediaType === "string" ? part.mediaType.toLowerCase() : "";
|
|
133088
|
+
return type.includes("image") || mime.startsWith("image/") || mediaType.startsWith("image/") || part.image_url !== undefined || part.imageUrl !== undefined || part.image !== undefined;
|
|
133089
|
+
}
|
|
133090
|
+
function defaultImageTokenHeuristic(part) {
|
|
133091
|
+
if (isRecord(part)) {
|
|
133092
|
+
const width = part.width;
|
|
133093
|
+
const height = part.height;
|
|
133094
|
+
if (typeof width === "number" && typeof height === "number" && width > 0 && height > 0) {
|
|
133095
|
+
return Math.max(256, Math.min(4096, Math.ceil(width * height / 750)));
|
|
133096
|
+
}
|
|
133097
|
+
}
|
|
133098
|
+
return 1024;
|
|
133099
|
+
}
|
|
133100
|
+
function partType(part) {
|
|
133101
|
+
return typeof part.type === "string" ? part.type : "";
|
|
133102
|
+
}
|
|
133103
|
+
function hasOwn(record, key) {
|
|
133104
|
+
return Object.hasOwn(record, key);
|
|
133105
|
+
}
|
|
133106
|
+
function recursiveByteLength(value) {
|
|
133107
|
+
if (value === null || value === undefined)
|
|
133108
|
+
return 0;
|
|
133109
|
+
if (typeof value === "string")
|
|
133110
|
+
return value.length;
|
|
133111
|
+
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
|
|
133112
|
+
return String(value).length;
|
|
133113
|
+
}
|
|
133114
|
+
if (Array.isArray(value)) {
|
|
133115
|
+
return value.reduce((sum, item) => sum + recursiveByteLength(item), value.length);
|
|
133116
|
+
}
|
|
133117
|
+
if (isRecord(value)) {
|
|
133118
|
+
let total = Object.keys(value).length;
|
|
133119
|
+
for (const [key, child] of Object.entries(value)) {
|
|
133120
|
+
total += key.length + recursiveByteLength(child);
|
|
133121
|
+
}
|
|
133122
|
+
return total;
|
|
133123
|
+
}
|
|
133124
|
+
return String(value).length;
|
|
133125
|
+
}
|
|
133126
|
+
function updateFnv1a32(hash, text) {
|
|
133127
|
+
let next = hash;
|
|
133128
|
+
for (let index = 0;index < text.length; index += 1) {
|
|
133129
|
+
next ^= text.charCodeAt(index);
|
|
133130
|
+
next = Math.imul(next, FNV1A_32_PRIME) >>> 0;
|
|
133131
|
+
}
|
|
133132
|
+
return next;
|
|
133133
|
+
}
|
|
133134
|
+
function contentStringsHash(fields) {
|
|
133135
|
+
let hash = FNV1A_32_OFFSET;
|
|
133136
|
+
for (const field of fields) {
|
|
133137
|
+
hash = updateFnv1a32(hash, `${field.length}:`);
|
|
133138
|
+
hash = updateFnv1a32(hash, field);
|
|
133139
|
+
hash = updateFnv1a32(hash, "\x00");
|
|
133140
|
+
}
|
|
133141
|
+
return hash.toString(16).padStart(8, "0");
|
|
133142
|
+
}
|
|
133143
|
+
function rawPartVersion(part) {
|
|
133144
|
+
return part.__magicContextPartUpdatedAt ?? part.updated_at ?? part.updatedAt ?? part.version ?? part.revision ?? "";
|
|
133145
|
+
}
|
|
133146
|
+
function callIdFromPart(part) {
|
|
133147
|
+
const direct = firstStringField(part, ["callID", "callId", "toolCallId", "tool_call_id", "id"]);
|
|
133148
|
+
if (direct)
|
|
133149
|
+
return direct;
|
|
133150
|
+
const state = isRecord(part.state) ? part.state : null;
|
|
133151
|
+
return state ? firstStringField(state, ["callID", "callId", "toolCallId", "tool_call_id", "id"]) ?? "" : "";
|
|
133152
|
+
}
|
|
133153
|
+
function toolSignalFromPart(part) {
|
|
133154
|
+
if (!isRecord(part))
|
|
133155
|
+
return null;
|
|
133156
|
+
const type = partType(part);
|
|
133157
|
+
const state = isRecord(part.state) ? part.state : null;
|
|
133158
|
+
const callId = callIdFromPart(part);
|
|
133159
|
+
if (!callId && type !== "tool")
|
|
133160
|
+
return null;
|
|
133161
|
+
if (type === "tool") {
|
|
133162
|
+
const hasInput = state !== null && hasOwn(state, "input");
|
|
133163
|
+
const outputKey = state ? hasOwn(state, "output") ? "output" : hasOwn(state, "error") ? "error" : hasOwn(state, "result") ? "result" : null : null;
|
|
133164
|
+
const hasOutput = outputKey !== null;
|
|
133165
|
+
const outputValue = outputKey && state ? state[outputKey] : undefined;
|
|
133166
|
+
const providerExecuted = part.providerExecuted === true;
|
|
133167
|
+
const openInvocation = !providerExecuted && !hasOutput;
|
|
133168
|
+
return {
|
|
133169
|
+
callId,
|
|
133170
|
+
hasInput: hasInput || openInvocation,
|
|
133171
|
+
hasOutput,
|
|
133172
|
+
inputText: hasInput && state ? stringValue(state.input) : "",
|
|
133173
|
+
outputText: hasOutput ? stringValue(outputValue) : ""
|
|
133174
|
+
};
|
|
133175
|
+
}
|
|
133176
|
+
if (type === "tool-invocation") {
|
|
133177
|
+
const args = part.args ?? part.input;
|
|
133178
|
+
return {
|
|
133179
|
+
callId,
|
|
133180
|
+
hasInput: args !== undefined,
|
|
133181
|
+
hasOutput: false,
|
|
133182
|
+
inputText: args !== undefined ? stringValue(args) : "",
|
|
133183
|
+
outputText: ""
|
|
133184
|
+
};
|
|
133185
|
+
}
|
|
133186
|
+
if (type === "tool_use") {
|
|
133187
|
+
const input = part.input;
|
|
133188
|
+
return {
|
|
133189
|
+
callId,
|
|
133190
|
+
hasInput: input !== undefined,
|
|
133191
|
+
hasOutput: false,
|
|
133192
|
+
inputText: input !== undefined ? stringValue(input) : "",
|
|
133193
|
+
outputText: ""
|
|
133194
|
+
};
|
|
133195
|
+
}
|
|
133196
|
+
if (type === "tool_result") {
|
|
133197
|
+
const content = part.content ?? part.output ?? part.result;
|
|
133198
|
+
return {
|
|
133199
|
+
callId,
|
|
133200
|
+
hasInput: false,
|
|
133201
|
+
hasOutput: content !== undefined,
|
|
133202
|
+
inputText: "",
|
|
133203
|
+
outputText: content !== undefined ? textFromToolResultContent(content) : ""
|
|
133204
|
+
};
|
|
133205
|
+
}
|
|
133206
|
+
return null;
|
|
133207
|
+
}
|
|
133208
|
+
function partCheapFingerprint(part) {
|
|
133209
|
+
if (!isRecord(part))
|
|
133210
|
+
return `${typeof part}:${recursiveByteLength(part)}`;
|
|
133211
|
+
const version = rawPartVersion(part);
|
|
133212
|
+
const type = typeof part.type === "string" ? part.type : "";
|
|
133213
|
+
return `${type}:${String(version)}:${recursiveByteLength(part)}`;
|
|
133214
|
+
}
|
|
133215
|
+
function messageCacheKey(message, options) {
|
|
133216
|
+
const namespace = "cacheNamespace" in options ? options.cacheNamespace : "estimate";
|
|
133217
|
+
const cheapFingerprint = message.parts.map(partCheapFingerprint).join("|");
|
|
133218
|
+
return [
|
|
133219
|
+
namespace,
|
|
133220
|
+
options.providerShapeVersion,
|
|
133221
|
+
message.id || `ordinal:${message.ordinal}`,
|
|
133222
|
+
message.role,
|
|
133223
|
+
message.parts.length,
|
|
133224
|
+
cheapFingerprint
|
|
133225
|
+
].join("\x00");
|
|
133226
|
+
}
|
|
133227
|
+
function setCachedEstimate(key, breakdown) {
|
|
133228
|
+
const keyEstimateBytes = key.length * 2 + 64;
|
|
133229
|
+
const existing = messageEstimateCache.get(key);
|
|
133230
|
+
if (existing)
|
|
133231
|
+
messageEstimateCacheBytes -= existing.keyEstimateBytes;
|
|
133232
|
+
messageEstimateCache.set(key, { breakdown, keyEstimateBytes });
|
|
133233
|
+
messageEstimateCacheBytes += keyEstimateBytes;
|
|
133234
|
+
while (messageEstimateCache.size > MAX_MESSAGE_CACHE_ENTRIES || messageEstimateCacheBytes > MAX_MESSAGE_CACHE_KEY_BYTES) {
|
|
133235
|
+
const first = messageEstimateCache.keys().next().value;
|
|
133236
|
+
if (typeof first !== "string")
|
|
133237
|
+
break;
|
|
133238
|
+
const removed = messageEstimateCache.get(first);
|
|
133239
|
+
if (removed)
|
|
133240
|
+
messageEstimateCacheBytes -= removed.keyEstimateBytes;
|
|
133241
|
+
messageEstimateCache.delete(first);
|
|
133242
|
+
}
|
|
133243
|
+
}
|
|
133244
|
+
function cloneBreakdown(value) {
|
|
133245
|
+
return { ...value };
|
|
133246
|
+
}
|
|
133247
|
+
function estimateNonToolPart(part, options, breakdown) {
|
|
133248
|
+
if (!isRecord(part)) {
|
|
133249
|
+
if (part !== null && part !== undefined)
|
|
133250
|
+
addBreakdown(breakdown, "other", estimateStructured(part));
|
|
133251
|
+
return true;
|
|
133252
|
+
}
|
|
133253
|
+
const type = partType(part);
|
|
133254
|
+
if (type === "step-start" || type === "step-finish" || type === "meta" && Object.keys(part).length <= 1) {
|
|
133255
|
+
return true;
|
|
133256
|
+
}
|
|
133257
|
+
if (type === "text") {
|
|
133258
|
+
const text = firstStringField(part, ["text", "content"]);
|
|
133259
|
+
if (text)
|
|
133260
|
+
addBreakdown(breakdown, "text", estimateTokens(text));
|
|
133261
|
+
return true;
|
|
133262
|
+
}
|
|
133263
|
+
if (type === "reasoning" || type === "thinking" || type === "redacted_thinking") {
|
|
133264
|
+
const text = firstStringField(part, ["thinking", "text", "content", "reasoning"]);
|
|
133265
|
+
if (text) {
|
|
133266
|
+
addBreakdown(breakdown, "reasoning", estimateTokens(text));
|
|
133267
|
+
} else {
|
|
133268
|
+
addBreakdown(breakdown, "other", estimateStructured(part));
|
|
133269
|
+
}
|
|
133270
|
+
return true;
|
|
133271
|
+
}
|
|
133272
|
+
const reasoningText = firstStringField(part, ["thinking", "reasoning"]);
|
|
133273
|
+
if (reasoningText && type.length === 0) {
|
|
133274
|
+
addBreakdown(breakdown, "reasoning", estimateTokens(reasoningText));
|
|
133275
|
+
return true;
|
|
133276
|
+
}
|
|
133277
|
+
if (looksImageLike(part)) {
|
|
133278
|
+
addBreakdown(breakdown, "image", options.imageTokenHeuristic?.(part) ?? defaultImageTokenHeuristic(part));
|
|
133279
|
+
const altText = firstStringField(part, ["alt", "text", "description"]);
|
|
133280
|
+
if (altText)
|
|
133281
|
+
addBreakdown(breakdown, "text", estimateTokens(altText));
|
|
133282
|
+
return true;
|
|
133283
|
+
}
|
|
133284
|
+
if (type.includes("file") || type === "source") {
|
|
133285
|
+
const content = firstStringField(part, ["content", "text", "source"]);
|
|
133286
|
+
if (content)
|
|
133287
|
+
addBreakdown(breakdown, "text", estimateTokens(content));
|
|
133288
|
+
else
|
|
133289
|
+
addBreakdown(breakdown, "other", estimateStructured(part));
|
|
133290
|
+
return true;
|
|
133291
|
+
}
|
|
133292
|
+
return false;
|
|
133293
|
+
}
|
|
133294
|
+
function estimateTrueRawMessageTokens(message, options) {
|
|
133295
|
+
const breakdown = cloneBreakdown(EMPTY_BREAKDOWN);
|
|
133296
|
+
const countedInput = new Set;
|
|
133297
|
+
const countedOutput = new Set;
|
|
133298
|
+
let ordinalToolIndex = 0;
|
|
133299
|
+
for (const part of message.parts) {
|
|
133300
|
+
const signal = toolSignalFromPart(part);
|
|
133301
|
+
if (signal) {
|
|
133302
|
+
const localKey = `${signal.callId || "tool"}:${message.ordinal}:${ordinalToolIndex}`;
|
|
133303
|
+
ordinalToolIndex += 1;
|
|
133304
|
+
if (signal.hasInput) {
|
|
133305
|
+
const key = `${signal.callId}:input:${message.ordinal}`;
|
|
133306
|
+
if (!countedInput.has(key)) {
|
|
133307
|
+
countedInput.add(key);
|
|
133308
|
+
addBreakdown(breakdown, "toolInput", estimateTokens(signal.inputText));
|
|
133309
|
+
}
|
|
133310
|
+
}
|
|
133311
|
+
if (signal.hasOutput) {
|
|
133312
|
+
const key = `${signal.callId}:output:${message.ordinal}:${localKey}`;
|
|
133313
|
+
if (!countedOutput.has(key)) {
|
|
133314
|
+
countedOutput.add(key);
|
|
133315
|
+
addBreakdown(breakdown, "toolOutput", estimateTokens(signal.outputText));
|
|
133316
|
+
}
|
|
133317
|
+
}
|
|
133318
|
+
continue;
|
|
133319
|
+
}
|
|
133320
|
+
if (!estimateNonToolPart(part, options, breakdown)) {
|
|
133321
|
+
addBreakdown(breakdown, "other", estimateStructured(part));
|
|
133322
|
+
}
|
|
133323
|
+
}
|
|
133324
|
+
return breakdown;
|
|
133325
|
+
}
|
|
133326
|
+
function buildToolArcs(messages) {
|
|
133327
|
+
const openQueues = new Map;
|
|
133328
|
+
const arcs = [];
|
|
133329
|
+
for (const message of messages) {
|
|
133330
|
+
for (const part of message.parts) {
|
|
133331
|
+
const signal = toolSignalFromPart(part);
|
|
133332
|
+
if (!signal || signal.callId.length === 0)
|
|
133333
|
+
continue;
|
|
133334
|
+
if (signal.hasInput && signal.hasOutput) {
|
|
133335
|
+
arcs.push({
|
|
133336
|
+
callId: signal.callId,
|
|
133337
|
+
invOrdinal: message.ordinal,
|
|
133338
|
+
resOrdinal: message.ordinal
|
|
133339
|
+
});
|
|
133340
|
+
continue;
|
|
133341
|
+
}
|
|
133342
|
+
if (signal.hasInput) {
|
|
133343
|
+
const queue = openQueues.get(signal.callId) ?? [];
|
|
133344
|
+
queue.push(message.ordinal);
|
|
133345
|
+
openQueues.set(signal.callId, queue);
|
|
133346
|
+
continue;
|
|
133347
|
+
}
|
|
133348
|
+
if (signal.hasOutput) {
|
|
133349
|
+
const queue = openQueues.get(signal.callId) ?? [];
|
|
133350
|
+
const invOrdinal = queue.shift();
|
|
133351
|
+
if (queue.length === 0)
|
|
133352
|
+
openQueues.delete(signal.callId);
|
|
133353
|
+
else
|
|
133354
|
+
openQueues.set(signal.callId, queue);
|
|
133355
|
+
if (invOrdinal !== undefined) {
|
|
133356
|
+
arcs.push({ callId: signal.callId, invOrdinal, resOrdinal: message.ordinal });
|
|
133357
|
+
}
|
|
133358
|
+
}
|
|
133359
|
+
}
|
|
133360
|
+
}
|
|
133361
|
+
for (const [callId, queue] of openQueues) {
|
|
133362
|
+
for (const invOrdinal of queue) {
|
|
133363
|
+
arcs.push({ callId, invOrdinal, resOrdinal: null });
|
|
133364
|
+
}
|
|
133365
|
+
}
|
|
133366
|
+
return arcs.sort((a, b) => a.invOrdinal - b.invOrdinal || (a.resOrdinal ?? Number.MAX_SAFE_INTEGER) - (b.resOrdinal ?? Number.MAX_SAFE_INTEGER));
|
|
133367
|
+
}
|
|
133368
|
+
function fenceBoundaryForToolArcs(candidate, arcs, lastCompartmentEndOrdinal, recentOpenArcCutoff) {
|
|
133369
|
+
let boundary = candidate;
|
|
133370
|
+
for (const arc of arcs) {
|
|
133371
|
+
if (arc.resOrdinal !== null) {
|
|
133372
|
+
if (completedToolArcCrossesBoundary(arc.invOrdinal, arc.resOrdinal, boundary)) {
|
|
133373
|
+
boundary = arc.resOrdinal + 1;
|
|
133374
|
+
}
|
|
133375
|
+
continue;
|
|
133376
|
+
}
|
|
133377
|
+
if (arc.invOrdinal < recentOpenArcCutoff)
|
|
133378
|
+
continue;
|
|
133379
|
+
if (arc.invOrdinal >= lastCompartmentEndOrdinal + 1 && arc.invOrdinal < boundary) {
|
|
133380
|
+
return arc.invOrdinal;
|
|
133381
|
+
}
|
|
133382
|
+
if (arc.invOrdinal >= boundary) {
|
|
133383
|
+
return arc.invOrdinal;
|
|
133384
|
+
}
|
|
133385
|
+
}
|
|
133386
|
+
return boundary;
|
|
133387
|
+
}
|
|
133388
|
+
function tokenForMessage(message, options) {
|
|
133389
|
+
const key = messageCacheKey(message, options);
|
|
133390
|
+
const cached = messageEstimateCache.get(key);
|
|
133391
|
+
if (cached)
|
|
133392
|
+
return cloneBreakdown(cached.breakdown);
|
|
133393
|
+
const breakdown = estimateTrueRawMessageTokens(message, options);
|
|
133394
|
+
setCachedEstimate(key, breakdown);
|
|
133395
|
+
return cloneBreakdown(breakdown);
|
|
133396
|
+
}
|
|
133397
|
+
function buildTrueRawTokenIndex(sessionId, messages, options) {
|
|
133398
|
+
const ordered = [...messages].sort((a, b) => a.ordinal - b.ordinal);
|
|
133399
|
+
const sliceCount = ordered.length;
|
|
133400
|
+
const firstOrdinal = ordered.length > 0 ? ordered[0].ordinal : 1;
|
|
133401
|
+
const terminalOrdinal = ordered.length > 0 ? ordered[ordered.length - 1].ordinal : 0;
|
|
133402
|
+
const rawMessageCount = Math.max(sliceCount, terminalOrdinal, options.absoluteMessageCount ?? sliceCount);
|
|
133403
|
+
const ordinalSpan = terminalOrdinal >= firstOrdinal ? terminalOrdinal - firstOrdinal + 1 : 0;
|
|
133404
|
+
const tokensByOrdinal = new Map;
|
|
133405
|
+
const idsByOrdinal = new Map;
|
|
133406
|
+
const prefix = new Array(ordinalSpan + 1).fill(0);
|
|
133407
|
+
for (const message of ordered) {
|
|
133408
|
+
const stored = options.storedTotalForMessage?.(message);
|
|
133409
|
+
const total = stored !== undefined && stored !== null ? stored : tokenForMessage(message, options).total;
|
|
133410
|
+
tokensByOrdinal.set(message.ordinal, total);
|
|
133411
|
+
idsByOrdinal.set(message.ordinal, message.id);
|
|
133412
|
+
const relative = message.ordinal - firstOrdinal + 1;
|
|
133413
|
+
if (relative >= 1 && relative <= ordinalSpan) {
|
|
133414
|
+
prefix[relative] = total;
|
|
133415
|
+
}
|
|
133416
|
+
}
|
|
133417
|
+
for (let k = 1;k <= ordinalSpan; k += 1) {
|
|
133418
|
+
prefix[k] += prefix[k - 1];
|
|
133419
|
+
}
|
|
133420
|
+
const ordinalToIndex = (ordinal) => Math.max(0, Math.min(ordinalSpan, ordinal - firstOrdinal));
|
|
133421
|
+
return {
|
|
133422
|
+
sessionId,
|
|
133423
|
+
providerShapeVersion: options.providerShapeVersion,
|
|
133424
|
+
rawMessageCount,
|
|
133425
|
+
tokenForOrdinal(ordinal) {
|
|
133426
|
+
return tokensByOrdinal.get(ordinal) ?? 0;
|
|
133427
|
+
},
|
|
133428
|
+
messageIdAtOrdinal(ordinal) {
|
|
133429
|
+
return idsByOrdinal.get(ordinal) ?? null;
|
|
133430
|
+
},
|
|
133431
|
+
suffixTokensFromOrdinal(ordinal) {
|
|
133432
|
+
if (ordinal <= firstOrdinal)
|
|
133433
|
+
return prefix[ordinalSpan];
|
|
133434
|
+
if (ordinal > terminalOrdinal)
|
|
133435
|
+
return 0;
|
|
133436
|
+
return prefix[ordinalSpan] - prefix[ordinalToIndex(ordinal)];
|
|
133437
|
+
},
|
|
133438
|
+
rangeTokens(startInclusive, endExclusive) {
|
|
133439
|
+
const start = Math.max(firstOrdinal, startInclusive);
|
|
133440
|
+
const end = Math.max(start, Math.min(terminalOrdinal + 1, endExclusive));
|
|
133441
|
+
return prefix[end - firstOrdinal] - prefix[start - firstOrdinal];
|
|
133442
|
+
},
|
|
133443
|
+
findSuffixStartForTokens(tokens) {
|
|
133444
|
+
if (!Number.isFinite(tokens) || tokens <= 0)
|
|
133445
|
+
return terminalOrdinal + 1;
|
|
133446
|
+
const target = Math.max(0, Math.floor(tokens));
|
|
133447
|
+
const total = prefix[ordinalSpan];
|
|
133448
|
+
if (total < target)
|
|
133449
|
+
return firstOrdinal;
|
|
133450
|
+
const cut = total - target;
|
|
133451
|
+
let lo = 0;
|
|
133452
|
+
let hi = ordinalSpan;
|
|
133453
|
+
let best = 0;
|
|
133454
|
+
while (lo <= hi) {
|
|
133455
|
+
const mid = lo + hi >> 1;
|
|
133456
|
+
if (prefix[mid] <= cut) {
|
|
133457
|
+
best = mid;
|
|
133458
|
+
lo = mid + 1;
|
|
133459
|
+
} else {
|
|
133460
|
+
hi = mid - 1;
|
|
133461
|
+
}
|
|
133462
|
+
}
|
|
133463
|
+
return firstOrdinal + best;
|
|
133464
|
+
},
|
|
133465
|
+
findHeadEndForCap(startInclusive, endExclusive, capTokens) {
|
|
133466
|
+
const start = Math.max(firstOrdinal, Math.min(terminalOrdinal + 1, startInclusive));
|
|
133467
|
+
const end = Math.max(start, Math.min(terminalOrdinal + 1, endExclusive));
|
|
133468
|
+
if (!Number.isFinite(capTokens) || capTokens <= 0)
|
|
133469
|
+
return start;
|
|
133470
|
+
const startIndex = start - firstOrdinal;
|
|
133471
|
+
const endIndex = end - firstOrdinal;
|
|
133472
|
+
const cut = prefix[startIndex] + Math.floor(capTokens);
|
|
133473
|
+
let lo = startIndex + 1;
|
|
133474
|
+
let hi = endIndex;
|
|
133475
|
+
let bestEndIndex = startIndex;
|
|
133476
|
+
while (lo <= hi) {
|
|
133477
|
+
const mid = lo + hi >> 1;
|
|
133478
|
+
if (prefix[mid] <= cut) {
|
|
133479
|
+
bestEndIndex = mid;
|
|
133480
|
+
lo = mid + 1;
|
|
133481
|
+
} else {
|
|
133482
|
+
hi = mid - 1;
|
|
133483
|
+
}
|
|
133484
|
+
}
|
|
133485
|
+
let bestEnd = firstOrdinal + bestEndIndex;
|
|
133486
|
+
if (bestEnd === start && start < end)
|
|
133487
|
+
bestEnd = start + 1;
|
|
133488
|
+
return Math.min(bestEnd, end);
|
|
133489
|
+
}
|
|
133490
|
+
};
|
|
133491
|
+
}
|
|
133492
|
+
function partContentFingerprint(part) {
|
|
133493
|
+
if (!isRecord(part))
|
|
133494
|
+
return `${typeof part}:${recursiveByteLength(part)}`;
|
|
133495
|
+
const tool = toolSignalFromPart(part);
|
|
133496
|
+
if (tool) {
|
|
133497
|
+
return contentStringsHash([tool.inputText, tool.outputText]);
|
|
133498
|
+
}
|
|
133499
|
+
const text = firstStringField(part, ["text", "thinking", "reasoning", "content", "url"]) ?? "";
|
|
133500
|
+
return contentStringsHash([text]);
|
|
133501
|
+
}
|
|
133502
|
+
function computeRawRangeFingerprint(messages, startInclusive, endExclusive) {
|
|
133503
|
+
const pieces = [];
|
|
133504
|
+
for (const message of messages) {
|
|
133505
|
+
if (message.ordinal < startInclusive || message.ordinal >= endExclusive)
|
|
133506
|
+
continue;
|
|
133507
|
+
const partFingerprint = message.parts.map(partContentFingerprint).join(",");
|
|
133508
|
+
pieces.push(`${message.ordinal}:${message.id}:${message.parts.length}:${partFingerprint}`);
|
|
133509
|
+
}
|
|
133510
|
+
return pieces.join("|");
|
|
133511
|
+
}
|
|
133512
|
+
function invalidateTrueRawTokenCache(args) {
|
|
133513
|
+
const sessionNeedle = args.sessionId ? `${args.sessionId}` : null;
|
|
133514
|
+
const messageNeedle = args.messageId ? `\x00${args.messageId}\x00` : null;
|
|
133515
|
+
for (const [key, value] of messageEstimateCache) {
|
|
133516
|
+
const sessionMatches = sessionNeedle === null || key.includes(sessionNeedle);
|
|
133517
|
+
const messageMatches = messageNeedle === null || key.includes(messageNeedle);
|
|
133518
|
+
if (sessionMatches && messageMatches) {
|
|
133519
|
+
messageEstimateCache.delete(key);
|
|
133520
|
+
messageEstimateCacheBytes -= value.keyEstimateBytes;
|
|
133521
|
+
}
|
|
133522
|
+
}
|
|
133523
|
+
args.reason;
|
|
133524
|
+
}
|
|
133525
|
+
|
|
132989
133526
|
// src/hooks/magic-context/tag-content-primitives.ts
|
|
132990
133527
|
var encoder = new TextEncoder;
|
|
132991
133528
|
var TAG_PREFIX_REGEX = /^(?:§\d+§\s*)+/;
|
|
@@ -133079,11 +133616,6 @@ function buildFileSourceContent(parts) {
|
|
|
133079
133616
|
return content.length > 0 ? content : null;
|
|
133080
133617
|
}
|
|
133081
133618
|
|
|
133082
|
-
// src/shared/record-type-guard.ts
|
|
133083
|
-
function isRecord(value) {
|
|
133084
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
133085
|
-
}
|
|
133086
|
-
|
|
133087
133619
|
// src/hooks/magic-context/edit-marker.ts
|
|
133088
133620
|
var TRUNCATION_SENTINEL = "...[truncated]";
|
|
133089
133621
|
var EDIT_REGION_HINT_LEN = 40;
|
|
@@ -133848,6 +134380,7 @@ function readSessionChunk(sessionId, tokenBudget, offset = 1, eligibleEndOrdinal
|
|
|
133848
134380
|
toolOnlyRanges.push({ start: range.start, end: range.end });
|
|
133849
134381
|
}
|
|
133850
134382
|
}
|
|
134383
|
+
const completedToolArcs = buildToolArcs(messages).flatMap((arc) => arc.resOrdinal === null ? [] : [{ start: arc.invOrdinal, end: arc.resOrdinal }]);
|
|
133851
134384
|
return {
|
|
133852
134385
|
startIndex: startOrdinal,
|
|
133853
134386
|
endIndex: lastOrdinal,
|
|
@@ -133860,7 +134393,8 @@ function readSessionChunk(sessionId, tokenBudget, offset = 1, eligibleEndOrdinal
|
|
|
133860
134393
|
`),
|
|
133861
134394
|
lines: lineMeta,
|
|
133862
134395
|
commitClusterCount: commitClusters,
|
|
133863
|
-
toolOnlyRanges
|
|
134396
|
+
toolOnlyRanges,
|
|
134397
|
+
completedToolArcs
|
|
133864
134398
|
};
|
|
133865
134399
|
}
|
|
133866
134400
|
|
|
@@ -134781,4 +135315,4 @@ function clearSession(db, sessionId) {
|
|
|
134781
135315
|
})();
|
|
134782
135316
|
}
|
|
134783
135317
|
|
|
134784
|
-
export { textMentionsRecentCommit, hasMeaningfulUserText, extractTexts, extractToolCallSummaries, estimateTokens, normalizeText, clearCompressionDepth, clearCompressionDepthRange, ensureSessionMetaRow, persistCachedM0, clearCachedM0M1, isRawCompactionSummaryInfo, extractInMemoryMessageViews, buildInMemoryTailRawMessages, stripPersistedAssistantText, byteSize, stripTagPrefix, peelLeadingMcTagNotation, prependTag, isThinkingPart, isTextPart, isToolPartWithOutput, isFilePart, buildFileSourceContent,
|
|
135318
|
+
export { textMentionsRecentCommit, hasMeaningfulUserText, extractTexts, extractToolCallSummaries, estimateTokens, normalizeText, stableStringify, clearCompressionDepth, clearCompressionDepthRange, ensureSessionMetaRow, persistCachedM0, clearCachedM0M1, isRawCompactionSummaryInfo, extractInMemoryMessageViews, buildInMemoryTailRawMessages, isRecord, completedToolArcCrossesBoundary, estimateTrueRawMessageTokens, buildToolArcs, fenceBoundaryForToolArcs, buildTrueRawTokenIndex, computeRawRangeFingerprint, invalidateTrueRawTokenCache, stripPersistedAssistantText, byteSize, stripTagPrefix, peelLeadingMcTagNotation, prependTag, isThinkingPart, isTextPart, isToolPartWithOutput, isFilePart, buildFileSourceContent, isEditTool, partHasCompletedResult, extractToolCallObservation, ToolMutationBatch, createToolDropTarget, setRawMessageProvider, cleanUserText, withRawSessionMessageCache, readRawSessionMessages, primeTailRawMessageCache, getCachedAbsoluteMessageCount, primeInMemoryTailRawMessageCache, readRawSessionMessageOrdinalPage, getRawSessionStoredMessageCount, readRawSessionMessagePartsById, readRawSessionMessageOrdinalById, readRawSessionMessageById, getRawSessionMessageCount, getRawSessionTagKeysThrough, getLegacyProtectedTailStartOrdinal, readSessionChunk, getMessageIndexSourceIdentity, isMessageIndexSourceCurrent, getLastIndexedOrdinal, getIndexedMessageCorpusSize, getMessageIndexReconciliationStartOrdinal, isMessageIndexReconciledThrough, deleteIndexedMessage, clearIndexedMessages, indexSingleMessage, indexMessagesAfterOrdinal, sweepOrphanedOpenCodeMessageIndexes, getOrCreateSessionMeta, updateSessionMeta, advanceToolReclaimWatermark, markSessionCleanupPending, retryPendingSessionCleanups, clearSession };
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,YAAY,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAyxBvE,QAAA,MAAM,MAAM,EAAE,YAGb,CAAC;AAEF,eAAe,MAAM,CAAC"}
|