@cortexkit/opencode-magic-context 0.36.1 → 0.38.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 +2 -0
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/project-security.d.ts +2 -1
- package/dist/config/project-security.d.ts.map +1 -1
- package/dist/config/schema/magic-context.d.ts +10 -0
- package/dist/config/schema/magic-context.d.ts.map +1 -1
- package/dist/{conflict-warning-hook-mt0ttc4x.js → conflict-warning-hook-an4fxhwf.js} +3 -3
- package/dist/{derive-budgets-88mxk2w1.js → derive-budgets-da4yg3tz.js} +3 -2
- package/dist/features/magic-context/context-authority.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/classify-prompt.d.ts +5 -1
- package/dist/features/magic-context/dreamer/classify-prompt.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/classify.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/evaluate-smart-notes.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/manifest-parser.d.ts +8 -0
- package/dist/features/magic-context/dreamer/manifest-parser.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/map-memories-prompt.d.ts +7 -1
- package/dist/features/magic-context/dreamer/map-memories-prompt.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/map-memories.d.ts +18 -0
- package/dist/features/magic-context/dreamer/map-memories.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/storage-dream-runs.d.ts +5 -0
- package/dist/features/magic-context/dreamer/storage-dream-runs.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/task-executor.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/verify-prompt.d.ts +5 -1
- package/dist/features/magic-context/dreamer/verify-prompt.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/verify.d.ts.map +1 -1
- package/dist/features/magic-context/fail-closed-block.d.ts +6 -1
- package/dist/features/magic-context/fail-closed-block.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding-openai.d.ts +3 -1
- package/dist/features/magic-context/memory/embedding-openai.d.ts.map +1 -1
- package/dist/features/magic-context/memory/project-identity.d.ts +6 -0
- package/dist/features/magic-context/memory/project-identity.d.ts.map +1 -1
- package/dist/features/magic-context/migrations.d.ts.map +1 -1
- package/dist/features/magic-context/overflow-detection.d.ts.map +1 -1
- package/dist/features/magic-context/reclaim-protection.d.ts +13 -0
- package/dist/features/magic-context/reclaim-protection.d.ts.map +1 -0
- package/dist/features/magic-context/smart-notes/runner.d.ts.map +1 -1
- package/dist/features/magic-context/smart-notes/wake-plane.d.ts +21 -0
- package/dist/features/magic-context/smart-notes/wake-plane.d.ts.map +1 -0
- package/dist/features/magic-context/storage-db.d.ts +19 -3
- package/dist/features/magic-context/storage-db.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta-persisted.d.ts +23 -0
- package/dist/features/magic-context/storage-meta-persisted.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta-session.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta-shared.d.ts.map +1 -1
- package/dist/features/magic-context/storage-primers.d.ts +11 -0
- package/dist/features/magic-context/storage-primers.d.ts.map +1 -1
- package/dist/features/magic-context/storage-schema-helpers.d.ts.map +1 -1
- package/dist/features/magic-context/storage-tags.d.ts +13 -5
- package/dist/features/magic-context/storage-tags.d.ts.map +1 -1
- package/dist/features/magic-context/user-memory/storage-user-memory.d.ts +7 -0
- package/dist/features/magic-context/user-memory/storage-user-memory.d.ts.map +1 -1
- package/dist/features/magic-context/window-report-ledger.d.ts +70 -0
- package/dist/features/magic-context/window-report-ledger.d.ts.map +1 -0
- package/dist/hooks/auto-update-checker/checker.d.ts +8 -1
- package/dist/hooks/auto-update-checker/checker.d.ts.map +1 -1
- package/dist/hooks/auto-update-checker/index.d.ts.map +1 -1
- package/dist/hooks/magic-context/channel2-cycle.d.ts +16 -0
- package/dist/hooks/magic-context/channel2-cycle.d.ts.map +1 -0
- package/dist/hooks/magic-context/channel2-delivery.d.ts +3 -9
- package/dist/hooks/magic-context/channel2-delivery.d.ts.map +1 -1
- package/dist/hooks/magic-context/command-handler.d.ts +2 -0
- package/dist/hooks/magic-context/command-handler.d.ts.map +1 -1
- package/dist/hooks/magic-context/ctx-reduce-nudge.d.ts +33 -103
- package/dist/hooks/magic-context/ctx-reduce-nudge.d.ts.map +1 -1
- package/dist/hooks/magic-context/emergency-drop.d.ts.map +1 -1
- package/dist/hooks/magic-context/event-handler.d.ts.map +1 -1
- package/dist/hooks/magic-context/event-resolvers.d.ts +4 -0
- package/dist/hooks/magic-context/event-resolvers.d.ts.map +1 -1
- package/dist/hooks/magic-context/execute-status.d.ts +3 -1
- package/dist/hooks/magic-context/execute-status.d.ts.map +1 -1
- package/dist/hooks/magic-context/fold-execution-gate.d.ts +2 -0
- package/dist/hooks/magic-context/fold-execution-gate.d.ts.map +1 -0
- package/dist/hooks/magic-context/hook-handlers.d.ts.map +1 -1
- package/dist/hooks/magic-context/hook.d.ts.map +1 -1
- package/dist/hooks/magic-context/inject-compartments.d.ts.map +1 -1
- package/dist/hooks/magic-context/live-session-state.d.ts +3 -0
- package/dist/hooks/magic-context/live-session-state.d.ts.map +1 -1
- package/dist/hooks/magic-context/lkg-replay.d.ts.map +1 -1
- package/dist/hooks/magic-context/lkg-slot.d.ts +20 -0
- package/dist/hooks/magic-context/lkg-slot.d.ts.map +1 -1
- package/dist/hooks/magic-context/module-state-sync.d.ts +8 -0
- package/dist/hooks/magic-context/module-state-sync.d.ts.map +1 -1
- package/dist/hooks/magic-context/module-transport.d.ts.map +1 -1
- package/dist/hooks/magic-context/module-wire.d.ts +6 -1
- package/dist/hooks/magic-context/module-wire.d.ts.map +1 -1
- package/dist/hooks/magic-context/rust-mode-transform.d.ts +21 -2
- package/dist/hooks/magic-context/rust-mode-transform.d.ts.map +1 -1
- package/dist/hooks/magic-context/strip-content.d.ts +27 -0
- package/dist/hooks/magic-context/strip-content.d.ts.map +1 -1
- package/dist/hooks/magic-context/supersession-reclaim.d.ts.map +1 -1
- package/dist/hooks/magic-context/system-prompt-hash.d.ts.map +1 -1
- package/dist/hooks/magic-context/tail-hygiene-walk.d.ts +78 -0
- package/dist/hooks/magic-context/tail-hygiene-walk.d.ts.map +1 -0
- package/dist/hooks/magic-context/tool-reclaim.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform-postprocess-phase.d.ts +14 -3
- package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform.d.ts +10 -0
- package/dist/hooks/magic-context/transform.d.ts.map +1 -1
- package/dist/hooks/magic-context/wrapup-orchestrator.d.ts.map +1 -1
- package/dist/{index-nsx2n9eg.js → index-4cz5nfxf.js} +3 -3
- package/dist/{index-f1tnb3vm.js → index-8pqfcghm.js} +21 -9
- package/dist/index-9zhhvr4w.js +733 -0
- package/dist/{index-zp6x6s6a.js → index-cae89eas.js} +25 -6
- package/dist/index-cgyfn1s2.js +53 -0
- package/dist/{index-3zqcdwqr.js → index-crke4z5j.js} +7 -4
- package/dist/{index-1skzp4dc.js → index-mmvhrqqb.js} +1991 -274
- package/dist/{index-tsgs7td8.js → index-pck9vadr.js} +1 -1
- package/dist/{index-3tnkymva.js → index-sw8gnq34.js} +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1714 -988
- package/dist/plugin/hooks/create-session-hooks.d.ts +3 -0
- package/dist/plugin/hooks/create-session-hooks.d.ts.map +1 -1
- package/dist/plugin/messages-transform.d.ts.map +1 -1
- package/dist/plugin/rpc-handlers.d.ts +2 -0
- package/dist/plugin/rpc-handlers.d.ts.map +1 -1
- package/dist/{recomp-orchestrator-a0g9b52q.js → recomp-orchestrator-jsde4ygw.js} +7 -6
- package/dist/{safe-notification-target-c11fdda2.js → safe-notification-target-b693f9x3.js} +2 -2
- package/dist/{send-session-notification-bgtf4853.js → send-session-notification-dxske5zm.js} +3 -3
- package/dist/shared/conflict-detector.d.ts +63 -0
- package/dist/shared/conflict-detector.d.ts.map +1 -1
- package/dist/shared/harness-provider-map.d.ts +8 -0
- package/dist/shared/harness-provider-map.d.ts.map +1 -1
- package/dist/shared/logger.d.ts +8 -0
- package/dist/shared/logger.d.ts.map +1 -1
- package/dist/shared/models-dev-cache.d.ts +7 -0
- package/dist/shared/models-dev-cache.d.ts.map +1 -1
- package/dist/shared/prompt-surface-runtime.d.ts.map +1 -1
- package/dist/shared/prompt-surface.d.ts +2 -0
- package/dist/shared/prompt-surface.d.ts.map +1 -1
- package/dist/shared/rpc-client.d.ts.map +1 -1
- package/dist/shared/rpc-server.d.ts.map +1 -1
- package/dist/shared/rpc-types.d.ts +29 -0
- package/dist/shared/rpc-types.d.ts.map +1 -1
- package/dist/shared/rpc-utils.d.ts +28 -9
- package/dist/shared/rpc-utils.d.ts.map +1 -1
- package/dist/shared/tail-hygiene-status.d.ts +15 -0
- package/dist/shared/tail-hygiene-status.d.ts.map +1 -0
- package/dist/shared/window-geometry.d.ts +90 -0
- package/dist/shared/window-geometry.d.ts.map +1 -0
- package/dist/{storage-meta-session-df9tstzr.js → storage-meta-session-mk4gsnke.js} +4 -3
- package/dist/tools/ctx-memory/tools.d.ts.map +1 -1
- package/dist/tools/ctx-note/tools.d.ts.map +1 -1
- package/dist/tui/data/context-db.d.ts.map +1 -1
- package/dist/tui-compiled/data/context-db.d.ts.map +1 -1
- package/package.json +3 -1
- package/src/config/index.ts +2 -0
- package/src/config/latch-permanence-guard.test.ts +218 -0
- package/src/config/project-security.ts +10 -1
- package/src/config/schema/magic-context.ts +12 -2
- package/src/shared/__fixtures__/window-overlay.v1.json +337 -0
- package/src/shared/conflict-detector.test.ts +176 -1
- package/src/shared/conflict-detector.ts +126 -3
- package/src/shared/harness-provider-map.test.ts +20 -0
- package/src/shared/harness-provider-map.ts +19 -0
- package/src/shared/logger.test.ts +119 -0
- package/src/shared/logger.ts +33 -15
- package/src/shared/models-dev-cache.test.ts +42 -0
- package/src/shared/models-dev-cache.ts +62 -9
- package/src/shared/prompt-surface-runtime.ts +5 -3
- package/src/shared/prompt-surface.test.ts +38 -0
- package/src/shared/prompt-surface.ts +15 -2
- package/src/shared/rpc-client.ts +4 -2
- package/src/shared/rpc-server.ts +1 -0
- package/src/shared/rpc-types.ts +30 -0
- package/src/shared/rpc-utils.test.ts +162 -13
- package/src/shared/rpc-utils.ts +224 -31
- package/src/shared/tail-hygiene-status.ts +57 -0
- package/src/shared/window-geometry.test.ts +342 -0
- package/src/shared/window-geometry.ts +626 -0
- package/src/tui/data/context-db.ts +5 -0
- package/src/tui/index.tsx +33 -1
- package/src/tui/slots/sidebar-content.tsx +17 -5
- package/src/tui-compiled/data/context-db.ts +5 -0
- package/src/tui-compiled/index.tsx +359 -273
- package/src/tui-compiled/slots/sidebar-content.tsx +316 -287
- package/dist/index-wgvxkqpx.js +0 -342
package/dist/index-wgvxkqpx.js
DELETED
|
@@ -1,342 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
sessionLog
|
|
3
|
-
} from "./index-f1tnb3vm.js";
|
|
4
|
-
import {
|
|
5
|
-
getHarness,
|
|
6
|
-
getMagicContextStorageDir
|
|
7
|
-
} from "./index-mmrhhk9a.js";
|
|
8
|
-
|
|
9
|
-
// src/shared/models-dev-cache.ts
|
|
10
|
-
import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
11
|
-
import { join } from "node:path";
|
|
12
|
-
|
|
13
|
-
// src/shared/harness-provider-map.ts
|
|
14
|
-
var PI_TO_CANONICAL_PROVIDER = {
|
|
15
|
-
"openai-codex": "openai",
|
|
16
|
-
"google-antigravity": "google"
|
|
17
|
-
};
|
|
18
|
-
function remapProviderPrefix(ref, map) {
|
|
19
|
-
if (typeof ref !== "string")
|
|
20
|
-
return ref;
|
|
21
|
-
const slash = ref.indexOf("/");
|
|
22
|
-
if (slash <= 0)
|
|
23
|
-
return ref;
|
|
24
|
-
const provider = ref.slice(0, slash);
|
|
25
|
-
if (!Object.hasOwn(map, provider))
|
|
26
|
-
return ref;
|
|
27
|
-
return `${map[provider]}${ref.slice(slash)}`;
|
|
28
|
-
}
|
|
29
|
-
function piModelRefToCanonical(ref) {
|
|
30
|
-
return remapProviderPrefix(ref, PI_TO_CANONICAL_PROVIDER);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// src/shared/storage-permissions.ts
|
|
34
|
-
var enforcePrivateStoragePermissions = true;
|
|
35
|
-
function setStoragePrivatePermissionEnforcement(enforce) {
|
|
36
|
-
enforcePrivateStoragePermissions = enforce;
|
|
37
|
-
}
|
|
38
|
-
function shouldEnforcePrivateStoragePermissions() {
|
|
39
|
-
return enforcePrivateStoragePermissions;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// src/shared/models-dev-cache.ts
|
|
43
|
-
var MIN_SANE_LIMIT = 20000;
|
|
44
|
-
var MAX_SANE_LIMIT = 3000000;
|
|
45
|
-
function isSaneLimit(limit) {
|
|
46
|
-
return typeof limit === "number" && limit >= MIN_SANE_LIMIT && limit <= MAX_SANE_LIMIT;
|
|
47
|
-
}
|
|
48
|
-
var SEPARATE_OUTPUT_QUOTA_PROVIDERS = new Set(["google", "google-antigravity"]);
|
|
49
|
-
var MIN_PLAUSIBLE_CONTEXT_LIMIT = 1024;
|
|
50
|
-
var OUTPUT_RESERVE_CAP_RATIO = 0.25;
|
|
51
|
-
var outputReserveConfig;
|
|
52
|
-
var reserveClampLogSeen = new Set;
|
|
53
|
-
var apiCache = null;
|
|
54
|
-
var apiLoadedAt = 0;
|
|
55
|
-
var persistSeedLoaded = false;
|
|
56
|
-
function persistFilePath() {
|
|
57
|
-
return join(getMagicContextStorageDir(), `model-context-limits-${getHarness()}.json`);
|
|
58
|
-
}
|
|
59
|
-
function loadPersistedApiCacheOnce() {
|
|
60
|
-
if (persistSeedLoaded || apiCache !== null)
|
|
61
|
-
return;
|
|
62
|
-
persistSeedLoaded = true;
|
|
63
|
-
try {
|
|
64
|
-
const raw = readFileSync(persistFilePath(), "utf-8");
|
|
65
|
-
const obj = JSON.parse(raw);
|
|
66
|
-
const map = new Map;
|
|
67
|
-
for (const [key, persisted] of Object.entries(obj)) {
|
|
68
|
-
const limit = typeof persisted === "number" ? persisted : persisted.limit;
|
|
69
|
-
const contextLimit = typeof persisted === "number" ? undefined : persisted.contextLimit;
|
|
70
|
-
const inputLimit = typeof persisted === "number" ? undefined : persisted.inputLimit;
|
|
71
|
-
const outputLimit = typeof persisted === "number" ? undefined : persisted.outputLimit;
|
|
72
|
-
const vision = typeof persisted === "number" ? false : persisted.vision === true;
|
|
73
|
-
if (isSaneLimit(contextLimit) || isSaneLimit(limit)) {
|
|
74
|
-
map.set(key, {
|
|
75
|
-
limit: isSaneLimit(limit) ? limit : undefined,
|
|
76
|
-
contextLimit: isSaneLimit(contextLimit) ? contextLimit : undefined,
|
|
77
|
-
inputLimit: isSaneLimit(inputLimit) ? inputLimit : undefined,
|
|
78
|
-
outputLimit: isFinitePositive(outputLimit) ? outputLimit : undefined,
|
|
79
|
-
vision
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
if (map.size > 0) {
|
|
84
|
-
apiCache = map;
|
|
85
|
-
sessionLog("global", `models-dev-cache: seeded ${map.size} entries from persisted cache (cold start)`);
|
|
86
|
-
}
|
|
87
|
-
} catch {}
|
|
88
|
-
}
|
|
89
|
-
function persistApiCache() {
|
|
90
|
-
if (!apiCache)
|
|
91
|
-
return;
|
|
92
|
-
const obj = {};
|
|
93
|
-
for (const [key, value] of apiCache) {
|
|
94
|
-
if (isSaneLimit(value.limit)) {
|
|
95
|
-
obj[key] = {
|
|
96
|
-
limit: value.limit,
|
|
97
|
-
contextLimit: isSaneLimit(value.contextLimit) ? value.contextLimit : undefined,
|
|
98
|
-
inputLimit: isSaneLimit(value.inputLimit) ? value.inputLimit : undefined,
|
|
99
|
-
outputLimit: isFinitePositive(value.outputLimit) ? value.outputLimit : undefined,
|
|
100
|
-
vision: value.vision === true
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
try {
|
|
105
|
-
const dir = getMagicContextStorageDir();
|
|
106
|
-
mkdirSync(dir, { recursive: true });
|
|
107
|
-
const target = persistFilePath();
|
|
108
|
-
const tmp = `${target}.${process.pid}.tmp`;
|
|
109
|
-
if (shouldEnforcePrivateStoragePermissions()) {
|
|
110
|
-
writeFileSync(tmp, JSON.stringify(obj), { encoding: "utf-8", mode: 384 });
|
|
111
|
-
} else {
|
|
112
|
-
writeFileSync(tmp, JSON.stringify(obj), { encoding: "utf-8" });
|
|
113
|
-
}
|
|
114
|
-
renameSync(tmp, target);
|
|
115
|
-
} catch {}
|
|
116
|
-
}
|
|
117
|
-
function isFinitePositive(value) {
|
|
118
|
-
return typeof value === "number" && Number.isFinite(value) && value > 0;
|
|
119
|
-
}
|
|
120
|
-
function modelKeyLookupOrder(providerID, modelID) {
|
|
121
|
-
const full = `${providerID}/${modelID}`;
|
|
122
|
-
const canonicalFull = piModelRefToCanonical(full);
|
|
123
|
-
const candidates = [full, canonicalFull, modelID];
|
|
124
|
-
const colon = modelID.lastIndexOf(":");
|
|
125
|
-
if (colon > 0) {
|
|
126
|
-
const bareModel = modelID.slice(0, colon);
|
|
127
|
-
const providerBare = `${providerID}/${bareModel}`;
|
|
128
|
-
candidates.push(providerBare, piModelRefToCanonical(providerBare), bareModel);
|
|
129
|
-
}
|
|
130
|
-
return [...new Set(candidates)];
|
|
131
|
-
}
|
|
132
|
-
function configuredOutputReserve(config, providerID, modelID) {
|
|
133
|
-
if (typeof config === "number")
|
|
134
|
-
return Number.isFinite(config) && config >= 0 ? config : undefined;
|
|
135
|
-
if (!config)
|
|
136
|
-
return;
|
|
137
|
-
for (const candidate of modelKeyLookupOrder(providerID, modelID)) {
|
|
138
|
-
const value = config[candidate];
|
|
139
|
-
if (typeof value === "number" && Number.isFinite(value) && value >= 0)
|
|
140
|
-
return value;
|
|
141
|
-
}
|
|
142
|
-
return Number.isFinite(config.default) && config.default >= 0 ? config.default : undefined;
|
|
143
|
-
}
|
|
144
|
-
function logReserveClampOnce(key, message) {
|
|
145
|
-
if (reserveClampLogSeen.has(key))
|
|
146
|
-
return;
|
|
147
|
-
reserveClampLogSeen.add(key);
|
|
148
|
-
sessionLog("global", `models-dev-cache: ${message}`);
|
|
149
|
-
}
|
|
150
|
-
function setOutputReserveConfig(config) {
|
|
151
|
-
outputReserveConfig = config;
|
|
152
|
-
}
|
|
153
|
-
function resolveLimit(limit, providerID, modelID, reserveConfig = outputReserveConfig) {
|
|
154
|
-
if (!limit)
|
|
155
|
-
return;
|
|
156
|
-
const context = isFinitePositive(limit.context) ? limit.context : undefined;
|
|
157
|
-
const input = isFinitePositive(limit.input) ? limit.input : undefined;
|
|
158
|
-
if (input !== undefined && (context === undefined || input < context))
|
|
159
|
-
return input;
|
|
160
|
-
if (context === undefined)
|
|
161
|
-
return;
|
|
162
|
-
const configuredReserve = configuredOutputReserve(reserveConfig, providerID, modelID);
|
|
163
|
-
let reserve;
|
|
164
|
-
if (configuredReserve !== undefined) {
|
|
165
|
-
reserve = configuredReserve;
|
|
166
|
-
} else if (SEPARATE_OUTPUT_QUOTA_PROVIDERS.has(providerID)) {
|
|
167
|
-
reserve = 0;
|
|
168
|
-
} else {
|
|
169
|
-
const output = isFinitePositive(limit.output) ? limit.output : 0;
|
|
170
|
-
const cap = context * OUTPUT_RESERVE_CAP_RATIO;
|
|
171
|
-
reserve = Math.min(output, cap);
|
|
172
|
-
if (output > cap) {
|
|
173
|
-
logReserveClampOnce(`cap|${providerID}/${modelID}|${context}|${output}`, `output reserve capped at 25% for ${providerID}/${modelID}: ${output} → ${cap}`);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
const floor = Math.max(MIN_PLAUSIBLE_CONTEXT_LIMIT, context * 0.5);
|
|
177
|
-
const maxReserve = Math.max(0, context - floor);
|
|
178
|
-
if (reserve > maxReserve) {
|
|
179
|
-
logReserveClampOnce(`floor|${providerID}/${modelID}|${context}|${reserve}`, `output reserve clamped for ${providerID}/${modelID}: ${reserve} → ${maxReserve} (usable floor ${floor})`);
|
|
180
|
-
reserve = maxReserve;
|
|
181
|
-
}
|
|
182
|
-
return Math.floor(context - reserve);
|
|
183
|
-
}
|
|
184
|
-
function setCachedModelMetadata(cache, key, model) {
|
|
185
|
-
const contextLimit = model?.limit?.context;
|
|
186
|
-
const inputLimit = model?.limit?.input;
|
|
187
|
-
const outputLimit = model?.limit?.output;
|
|
188
|
-
const rawLimit = isSaneLimit(contextLimit) ? contextLimit : isSaneLimit(inputLimit) ? inputLimit : undefined;
|
|
189
|
-
if (rawLimit === undefined)
|
|
190
|
-
return;
|
|
191
|
-
const values = [model?.capabilities, model?.modalities, model?.input, model?.attachment];
|
|
192
|
-
const vision = values.some((value2) => JSON.stringify(value2 ?? "").toLowerCase().includes("image") || JSON.stringify(value2 ?? "").toLowerCase().includes("vision"));
|
|
193
|
-
const value = {
|
|
194
|
-
limit: rawLimit,
|
|
195
|
-
contextLimit: isSaneLimit(contextLimit) ? contextLimit : undefined,
|
|
196
|
-
inputLimit: isSaneLimit(inputLimit) ? inputLimit : undefined,
|
|
197
|
-
outputLimit: isFinitePositive(outputLimit) ? outputLimit : undefined,
|
|
198
|
-
vision
|
|
199
|
-
};
|
|
200
|
-
cache.set(key, value);
|
|
201
|
-
const modes = model?.experimental?.modes;
|
|
202
|
-
if (modes && typeof modes === "object") {
|
|
203
|
-
for (const mode of Object.keys(modes)) {
|
|
204
|
-
cache.set(`${key}-${mode}`, value);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
async function refreshModelLimitsFromApi(client, options) {
|
|
209
|
-
const attempts = Math.max(1, (options?.retries ?? 0) + 1);
|
|
210
|
-
const delayMs = options?.retryDelayMs ?? 1000;
|
|
211
|
-
for (let attempt = 1;attempt <= attempts; attempt++) {
|
|
212
|
-
const ok = await refreshModelLimitsOnce(client);
|
|
213
|
-
if (ok)
|
|
214
|
-
return;
|
|
215
|
-
if (attempt < attempts) {
|
|
216
|
-
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
var authRewarmDone = false;
|
|
221
|
-
async function refreshModelLimitsAfterAuthOnce(client) {
|
|
222
|
-
if (authRewarmDone)
|
|
223
|
-
return;
|
|
224
|
-
authRewarmDone = true;
|
|
225
|
-
const ok = await refreshModelLimitsOnce(client);
|
|
226
|
-
if (!ok)
|
|
227
|
-
authRewarmDone = false;
|
|
228
|
-
}
|
|
229
|
-
async function refreshModelLimitsOnce(client) {
|
|
230
|
-
try {
|
|
231
|
-
const result = await client.config.providers();
|
|
232
|
-
const data = result.data;
|
|
233
|
-
const providers = data?.providers;
|
|
234
|
-
if (!Array.isArray(providers) || providers.length === 0) {
|
|
235
|
-
sessionLog("global", "models-dev-cache: API refresh returned no providers payload (will retry if attempts remain)");
|
|
236
|
-
return false;
|
|
237
|
-
}
|
|
238
|
-
const map = new Map;
|
|
239
|
-
for (const entry of providers) {
|
|
240
|
-
const p = entry;
|
|
241
|
-
if (!p?.id || !p.models || typeof p.models !== "object")
|
|
242
|
-
continue;
|
|
243
|
-
for (const [modelId, model] of Object.entries(p.models)) {
|
|
244
|
-
setCachedModelMetadata(map, `${p.id}/${modelId}`, model);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
const previousSize = apiCache?.size ?? null;
|
|
248
|
-
apiCache = map;
|
|
249
|
-
apiLoadedAt = Date.now();
|
|
250
|
-
persistApiCache();
|
|
251
|
-
if (previousSize === null) {
|
|
252
|
-
sessionLog("global", `models-dev-cache: API layer loaded ${map.size} model metadata entries`);
|
|
253
|
-
} else if (previousSize !== map.size) {
|
|
254
|
-
sessionLog("global", `models-dev-cache: API layer loaded ${map.size} model metadata entries (was ${previousSize})`);
|
|
255
|
-
}
|
|
256
|
-
return true;
|
|
257
|
-
} catch (error) {
|
|
258
|
-
sessionLog("global", "models-dev-cache: API refresh failed:", error instanceof Error ? error.message : String(error));
|
|
259
|
-
return false;
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
function getSdkContextLimit(providerID, modelID, detectedContextLimit, options) {
|
|
263
|
-
loadPersistedApiCacheOnce();
|
|
264
|
-
const metadata = lookupMetadataWithTagFallback(apiCache, providerID, modelID);
|
|
265
|
-
if (!metadata)
|
|
266
|
-
return;
|
|
267
|
-
const rawContext = metadata.contextLimit ?? metadata.limit;
|
|
268
|
-
const promptOnlyDetected = options?.detectedLimitProvenance === "prompt_only" && isFinitePositive(detectedContextLimit) ? detectedContextLimit : undefined;
|
|
269
|
-
const context = promptOnlyDetected === undefined && isFinitePositive(detectedContextLimit) && isFinitePositive(rawContext) ? Math.min(rawContext, detectedContextLimit) : promptOnlyDetected === undefined && isFinitePositive(detectedContextLimit) ? detectedContextLimit : rawContext;
|
|
270
|
-
const inputCandidates = [metadata.inputLimit, promptOnlyDetected].filter(isFinitePositive);
|
|
271
|
-
const input = inputCandidates.length > 0 ? Math.min(...inputCandidates) : undefined;
|
|
272
|
-
return resolveLimit({
|
|
273
|
-
context,
|
|
274
|
-
input,
|
|
275
|
-
output: metadata.outputLimit
|
|
276
|
-
}, providerID, modelID, options?.reservation === "none" ? 0 : undefined);
|
|
277
|
-
}
|
|
278
|
-
function modelSupportsVision(providerID, modelID) {
|
|
279
|
-
loadPersistedApiCacheOnce();
|
|
280
|
-
if (!apiCache)
|
|
281
|
-
return false;
|
|
282
|
-
const exact = apiCache.get(`${providerID}/${modelID}`);
|
|
283
|
-
if (exact?.vision === true)
|
|
284
|
-
return true;
|
|
285
|
-
const colon = modelID.lastIndexOf(":");
|
|
286
|
-
return colon > 0 ? apiCache.get(`${providerID}/${modelID.slice(0, colon)}`)?.vision === true : false;
|
|
287
|
-
}
|
|
288
|
-
function lookupMetadataWithTagFallback(cache, providerID, modelID) {
|
|
289
|
-
if (!cache)
|
|
290
|
-
return;
|
|
291
|
-
const exact = cache.get(`${providerID}/${modelID}`);
|
|
292
|
-
if (exact)
|
|
293
|
-
return exact;
|
|
294
|
-
const colonIdx = modelID.lastIndexOf(":");
|
|
295
|
-
if (colonIdx > 0) {
|
|
296
|
-
return cache.get(`${providerID}/${modelID.slice(0, colonIdx)}`);
|
|
297
|
-
}
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
// src/hooks/magic-context/derive-budgets.ts
|
|
302
|
-
var TRIGGER_BUDGET_PERCENTAGE = 0.05;
|
|
303
|
-
var TRIGGER_BUDGET_MIN = 5000;
|
|
304
|
-
var TRIGGER_BUDGET_MAX = 50000;
|
|
305
|
-
var HISTORIAN_CHUNK_PERCENTAGE = 0.25;
|
|
306
|
-
var HISTORIAN_CHUNK_MIN = 8000;
|
|
307
|
-
var HISTORIAN_CHUNK_MAX = 50000;
|
|
308
|
-
var DEFAULT_HISTORIAN_CONTEXT_FALLBACK = 128000;
|
|
309
|
-
function deriveTriggerBudget(mainContextLimit, executeThresholdPercentage) {
|
|
310
|
-
if (!Number.isFinite(mainContextLimit) || mainContextLimit <= 0) {
|
|
311
|
-
return TRIGGER_BUDGET_MIN;
|
|
312
|
-
}
|
|
313
|
-
const thresholdFraction = Math.max(0, executeThresholdPercentage) / 100;
|
|
314
|
-
const usable = mainContextLimit * thresholdFraction;
|
|
315
|
-
const derived = Math.round(usable * TRIGGER_BUDGET_PERCENTAGE);
|
|
316
|
-
return Math.max(TRIGGER_BUDGET_MIN, Math.min(TRIGGER_BUDGET_MAX, derived));
|
|
317
|
-
}
|
|
318
|
-
function deriveHistorianChunkTokens(historianContextLimit) {
|
|
319
|
-
if (!Number.isFinite(historianContextLimit) || historianContextLimit <= 0) {
|
|
320
|
-
return HISTORIAN_CHUNK_MIN;
|
|
321
|
-
}
|
|
322
|
-
const derived = Math.round(historianContextLimit * HISTORIAN_CHUNK_PERCENTAGE);
|
|
323
|
-
return Math.max(HISTORIAN_CHUNK_MIN, Math.min(HISTORIAN_CHUNK_MAX, derived));
|
|
324
|
-
}
|
|
325
|
-
function resolveHistorianContextLimit(historianModelOverride) {
|
|
326
|
-
if (typeof historianModelOverride === "string" && historianModelOverride.includes("/")) {
|
|
327
|
-
const [providerID, ...rest] = historianModelOverride.split("/");
|
|
328
|
-
const modelID = rest.join("/");
|
|
329
|
-
if (providerID && modelID) {
|
|
330
|
-
const limit = getSdkContextLimit(providerID, modelID);
|
|
331
|
-
if (typeof limit === "number" && limit > 0)
|
|
332
|
-
return limit;
|
|
333
|
-
}
|
|
334
|
-
return DEFAULT_HISTORIAN_CONTEXT_FALLBACK;
|
|
335
|
-
}
|
|
336
|
-
if (typeof historianModelOverride === "string" && historianModelOverride.trim() !== "") {
|
|
337
|
-
console.warn(`[magic-context] historian.model "${historianModelOverride}" lacks provider prefix ("provider/model-id"); using the default context limit for chunk-budget derivation.`);
|
|
338
|
-
}
|
|
339
|
-
return DEFAULT_HISTORIAN_CONTEXT_FALLBACK;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
export { piModelRefToCanonical, setStoragePrivatePermissionEnforcement, shouldEnforcePrivateStoragePermissions, isSaneLimit, setOutputReserveConfig, refreshModelLimitsFromApi, refreshModelLimitsAfterAuthOnce, getSdkContextLimit, modelSupportsVision, deriveTriggerBudget, deriveHistorianChunkTokens, resolveHistorianContextLimit };
|