@cortexkit/opencode-magic-context 0.49.0 → 0.49.2
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/{announcement-fp14dw0n.js → announcement-hdbkt8v5.js} +1 -1
- package/dist/auto-search-worker.js +3 -3
- package/dist/config/{mural-model-deprecation.d.ts → deprecated-config.d.ts} +4 -1
- package/dist/config/deprecated-config.d.ts.map +1 -0
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/project-security.d.ts +2 -2
- package/dist/config/schema/magic-context.d.ts +4 -4
- package/dist/config/schema/magic-context.d.ts.map +1 -1
- package/dist/{conflict-warning-hook-kzx4dmk1.js → conflict-warning-hook-asnt6hfc.js} +3 -3
- package/dist/{context-nj5f293n.js → context-tfyhtdcs.js} +111 -45
- package/dist/{derive-budgets-qz5rrr08.js → derive-budgets-j4ry8j8n.js} +1 -1
- package/dist/{embedding-backfill-aetxv1vt.js → embedding-backfill-wyc3ykyf.js} +7 -7
- package/dist/embedding-worker-process.js +30 -0
- package/dist/embedding-worker.js +5 -5
- package/dist/features/magic-context/dreamer/task-registry.d.ts +2 -3
- package/dist/features/magic-context/dreamer/task-registry.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding-worker-client.d.ts +8 -1
- package/dist/features/magic-context/memory/embedding-worker-client.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding-worker-process.d.ts +2 -0
- package/dist/features/magic-context/memory/embedding-worker-process.d.ts.map +1 -0
- package/dist/features/magic-context/memory/onnx-telemetry.d.ts +3 -0
- package/dist/features/magic-context/memory/onnx-telemetry.d.ts.map +1 -0
- package/dist/features/magic-context/overflow-detection.d.ts +1 -2
- package/dist/features/magic-context/overflow-detection.d.ts.map +1 -1
- package/dist/features/magic-context/storage-session-tables.d.ts.map +1 -1
- package/dist/features/magic-context/transform-decision-log.d.ts +1 -1
- package/dist/features/magic-context/transform-decision-log.d.ts.map +1 -1
- package/dist/features/magic-context/user-memory/storage-user-memory.d.ts +1 -0
- package/dist/features/magic-context/user-memory/storage-user-memory.d.ts.map +1 -1
- package/dist/hooks/magic-context/event-handler.d.ts.map +1 -1
- package/dist/hooks/magic-context/final-wire-token-estimate.d.ts +1 -0
- package/dist/hooks/magic-context/final-wire-token-estimate.d.ts.map +1 -1
- package/dist/hooks/magic-context/hook-handlers.d.ts.map +1 -1
- package/dist/hooks/magic-context/hook.d.ts +1 -0
- 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/lkg-measured-request.d.ts +3 -1
- package/dist/hooks/magic-context/lkg-measured-request.d.ts.map +1 -1
- package/dist/hooks/magic-context/lkg-persist.d.ts +8 -14
- package/dist/hooks/magic-context/lkg-persist.d.ts.map +1 -1
- package/dist/hooks/magic-context/lkg-recovery-fence.d.ts +62 -0
- package/dist/hooks/magic-context/lkg-recovery-fence.d.ts.map +1 -0
- package/dist/hooks/magic-context/lkg-recovery-prefix.d.ts +38 -0
- package/dist/hooks/magic-context/lkg-recovery-prefix.d.ts.map +1 -0
- package/dist/hooks/magic-context/lkg-replay.d.ts +3 -1
- package/dist/hooks/magic-context/lkg-replay.d.ts.map +1 -1
- package/dist/hooks/magic-context/lkg-slot.d.ts.map +1 -1
- package/dist/hooks/magic-context/opaque-thinking.d.ts +12 -0
- package/dist/hooks/magic-context/opaque-thinking.d.ts.map +1 -0
- package/dist/hooks/magic-context/pass-outcome.d.ts +2 -2
- package/dist/hooks/magic-context/pending-materialization.d.ts +8 -0
- package/dist/hooks/magic-context/pending-materialization.d.ts.map +1 -0
- package/dist/hooks/magic-context/read-session-db.d.ts +3 -0
- package/dist/hooks/magic-context/read-session-db.d.ts.map +1 -1
- package/dist/hooks/magic-context/recomp-orchestrator.d.ts.map +1 -1
- package/dist/hooks/magic-context/rust-mode-transform.d.ts +1 -0
- package/dist/hooks/magic-context/rust-mode-transform.d.ts.map +1 -1
- package/dist/hooks/magic-context/strip-content.d.ts +5 -2
- package/dist/hooks/magic-context/strip-content.d.ts.map +1 -1
- package/dist/hooks/magic-context/system-date-refresh.d.ts +29 -0
- package/dist/hooks/magic-context/system-date-refresh.d.ts.map +1 -0
- package/dist/hooks/magic-context/system-prompt-dates.d.ts +6 -0
- package/dist/hooks/magic-context/system-prompt-dates.d.ts.map +1 -0
- package/dist/hooks/magic-context/system-prompt-delivery.d.ts +47 -0
- package/dist/hooks/magic-context/system-prompt-delivery.d.ts.map +1 -0
- package/dist/hooks/magic-context/system-prompt-hash.d.ts +4 -0
- package/dist/hooks/magic-context/system-prompt-hash.d.ts.map +1 -1
- package/dist/hooks/magic-context/tag-messages.d.ts +8 -0
- package/dist/hooks/magic-context/tag-messages.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform-postprocess-phase.d.ts +2 -0
- package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform.d.ts +6 -0
- package/dist/hooks/magic-context/transform.d.ts.map +1 -1
- package/dist/{index-9qjqq45k.js → index-0pjvpfkf.js} +21 -1
- package/dist/index-6m7rdv7y.js +733 -0
- package/dist/{index-1r0k9js3.js → index-6tfxmc4n.js} +10 -10
- package/dist/{index-gzcmtmar.js → index-7w18gg78.js} +25 -1
- package/dist/{index-nm6a8xpd.js → index-att4bfan.js} +1 -1
- package/dist/{index-ezmszwbd.js → index-eypn4ba6.js} +7 -4
- package/dist/{index-ha87j7a8.js → index-fdtkt5r1.js} +6 -6
- package/dist/{index-kp5qe690.js → index-fg00p1cg.js} +38 -36
- package/dist/{index-1bc61ans.js → index-fvnzcnww.js} +44 -1
- package/dist/index-gbmqa9x9.js +1170 -0
- package/dist/{index-pxwp2hjk.js → index-gzky5rva.js} +30 -53
- package/dist/{index-kq57tdf0.js → index-k6xvbj2e.js} +255 -1075
- package/dist/{index-0w56v4fm.js → index-mw7c950g.js} +1 -1
- package/dist/{index-1g30axtz.js → index-p0n5ryq8.js} +82 -900
- package/dist/{index-b7mv1jx3.js → index-p1hnwfd8.js} +1 -1
- package/dist/{index-peshmj6h.js → index-r2zcs71c.js} +6 -4
- package/dist/{index-kmpvemph.js → index-rafsaxac.js} +2 -2
- package/dist/{index-stbr4ce6.js → index-s7fc0fkn.js} +1 -1
- package/dist/{index-hj2q50k0.js → index-sfhndpcb.js} +463 -6
- package/dist/index-sv0bb7vg.js +6972 -0
- package/dist/{index-57mrqed8.js → index-w23dh319.js} +17 -258
- package/dist/{index-9rt53q79.js → index-xgrwmj82.js} +1 -1
- package/dist/{index-h2py7207.js → index-xnanvj3w.js} +765 -1255
- package/dist/{index-2jcbj7r1.js → index-zfzdpeq2.js} +2269 -3802
- package/dist/{index-s4j7t92z.js → index-zg8yq4ya.js} +29 -744
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +209 -61
- package/dist/{message-index-bounded-h0mrd4pr.js → message-index-bounded-hqktkhdy.js} +7 -6
- package/dist/migration-worker.js +6 -5
- package/dist/plugin/hooks/create-session-hooks.d.ts +3 -1
- 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.map +1 -1
- package/dist/{project-embedding-registry-rtt0djyb.js → project-embedding-registry-0gv3h6y8.js} +6 -6
- package/dist/raw-ordinal-warmup-worker.js +1 -1
- package/dist/{read-session-chunk-ty89v75e.js → read-session-chunk-5mh0bh8y.js} +5 -4
- package/dist/{recomp-orchestrator-4f1yyd1q.js → recomp-orchestrator-kpkhkjhq.js} +20 -19
- package/dist/{search-pz4f4wge.js → search-p9w39qv4.js} +15 -14
- package/dist/{send-session-notification-38cd5dqt.js → send-session-notification-xffh3bbe.js} +2 -2
- package/dist/shared/models-dev-cache.d.ts.map +1 -1
- package/dist/shared/storage-file-mutex.d.ts +9 -0
- package/dist/shared/storage-file-mutex.d.ts.map +1 -0
- package/dist/shared/storage-permissions.d.ts +6 -0
- package/dist/shared/storage-permissions.d.ts.map +1 -1
- package/dist/shared/window-geometry.d.ts +9 -66
- package/dist/shared/window-geometry.d.ts.map +1 -1
- package/dist/standalone-plugin-marker.d.ts +8 -0
- package/dist/standalone-plugin-marker.d.ts.map +1 -0
- package/dist/tools/ctx-reduce/constants.d.ts +1 -2
- package/dist/tools/ctx-reduce/constants.d.ts.map +1 -1
- package/dist/tools/ctx-reduce/tools.d.ts +1 -1
- package/dist/tools/ctx-reduce/tools.d.ts.map +1 -1
- package/dist/{transform-postprocess-phase-tck4g20s.js → transform-postprocess-phase-481j5a7z.js} +20 -19
- package/dist/v2/auto-search-worker.js +3 -3
- package/dist/v2/{context-crbt6p1y.js → context-gsdtha5v.js} +873 -2122
- package/dist/v2/{derive-budgets-fgej9j46.js → derive-budgets-enckmg7y.js} +2 -2
- package/dist/v2/{embedding-backfill-ph1qfyn3.js → embedding-backfill-dbvjbcmm.js} +8 -9
- package/dist/v2/embedding-worker-process.js +29 -0
- package/dist/v2/embedding-worker.js +3 -4
- package/dist/v2/hooks/context.d.ts.map +1 -1
- package/dist/v2/hooks/copied-tag-notation.d.ts +19 -0
- package/dist/v2/hooks/copied-tag-notation.d.ts.map +1 -0
- package/dist/v2/hooks/payload.d.ts.map +1 -1
- package/dist/v2/hooks/system-prompt-response.d.ts +5 -0
- package/dist/v2/hooks/system-prompt-response.d.ts.map +1 -0
- package/dist/v2/{message-index-bounded-xwqdywxg.js → message-index-bounded-p6v0n8qh.js} +8 -7
- package/dist/v2/migration-worker.js +6 -5
- package/dist/v2/{project-embedding-registry-d6m7tyxr.js → project-embedding-registry-nyq7z8y6.js} +7 -8
- package/dist/v2/raw-ordinal-warmup-worker.js +2 -2
- package/dist/v2/{read-session-chunk-8p5mdryx.js → read-session-chunk-fhyfe4qa.js} +6 -5
- package/dist/v2/{recomp-orchestrator-ckfgt6wq.js → recomp-orchestrator-5xy7w136.js} +22 -22
- package/dist/v2/{search-ym8rehhz.js → search-j7bakcwa.js} +17 -17
- package/dist/v2/{send-session-notification-5m0yapqx.js → send-session-notification-f6v698r5.js} +2 -2
- package/dist/v2/{server-jvxzm1jy.js → server-1fszbabv.js} +28 -28
- package/dist/v2/{server-3j4d00kf.js → server-2htmbqbv.js} +25 -1
- package/dist/v2/{server-c5rrgrcd.js → server-4c7jt8gq.js} +25 -1
- package/dist/v2/{server-ycw9qqcy.js → server-4enr7s6x.js} +1 -1
- package/dist/v2/{server-v4mewb78.js → server-6tcrjfw8.js} +1 -1
- package/dist/v2/{server-x4hscabv.js → server-7spkq1y6.js} +2257 -3746
- package/dist/v2/{server-sa8jvrfc.js → server-7vkj6zhb.js} +29 -744
- package/dist/v2/{server-xwz4dqh9.js → server-8xmq9h3f.js} +30 -53
- package/dist/v2/{server-1tk82krw.js → server-bgzy0phk.js} +463 -6
- package/dist/v2/server-c7n2ftng.js +6860 -0
- package/dist/v2/server-cvgn49tr.js +733 -0
- package/dist/v2/{server-zm570tr2.js → server-cw6521wn.js} +232 -7
- package/dist/v2/{server-pj4pjv02.js → server-czb2rkpk.js} +21 -1
- package/dist/v2/{server-19zjmn02.js → server-d6g4whc2.js} +20 -14
- package/dist/v2/{server-1bc61ans.js → server-fvnzcnww.js} +44 -1
- package/dist/v2/{server-w32kmdez.js → server-kh958yj8.js} +3 -3
- package/dist/v2/{server-q4ph9bwn.js → server-mqvtz7wz.js} +7 -7
- package/dist/v2/{server-2s6ayq3t.js → server-ras2sxnc.js} +133 -13
- package/dist/v2/{server-ss95ky04.js → server-s2st631p.js} +1 -1
- package/dist/v2/{server-2s9qz6n7.js → server-t528t8qm.js} +1 -1
- package/dist/v2/{server-8k2agx29.js → server-vb7pbd9p.js} +17 -258
- package/dist/v2/{server-zp53y8js.js → server-zbxjj2mz.js} +2 -2
- package/dist/v2/server.d.ts.map +1 -1
- package/dist/v2/server.js +149 -5
- package/dist/v2/{storage-tags-8vw924zn.js → storage-tags-5yxrvkaf.js} +1 -1
- package/dist/v2/{wrapup-orchestrator-a7efxaax.js → wrapup-orchestrator-yem5422s.js} +28 -28
- package/dist/{wrapup-orchestrator-barbc6h9.js → wrapup-orchestrator-xvrr27ws.js} +21 -20
- package/package.json +4 -4
- package/src/config/{mural-model-deprecation.ts → deprecated-config.ts} +19 -0
- package/src/config/index.test.ts +15 -37
- package/src/config/index.ts +2 -3
- package/src/config/project-security.ts +3 -3
- package/src/config/schema/magic-context.ts +11 -7
- package/src/features/magic-context/dreamer/task-registry.ts +2 -3
- package/src/shared/models-dev-cache.ts +1 -7
- package/src/shared/sqlite-640-review.test.ts +5 -1
- package/src/shared/storage-file-mutex-review.test.ts +162 -0
- package/src/shared/storage-file-mutex.ts +130 -0
- package/src/shared/storage-permissions.ts +46 -0
- package/src/shared/window-geometry.test.ts +102 -236
- package/src/shared/window-geometry.ts +23 -413
- package/dist/config/mural-model-deprecation.d.ts.map +0 -1
- package/dist/index-2jgwttpt.js +0 -3269
- package/dist/index-mfkzp7qj.js +0 -229
- package/dist/v2/server-4ez8mntn.js +0 -3213
- package/dist/v2/server-mfkzp7qj.js +0 -229
- package/src/shared/__fixtures__/window-overlay.v1.json +0 -337
- /package/dist/{index-2pp04pmg.js → index-69vas84q.js} +0 -0
- /package/dist/v2/{index-2pp04pmg.js → index-69vas84q.js} +0 -0
|
@@ -1,29 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
toolTemplateError
|
|
3
|
-
} from "./index-mfkzp7qj.js";
|
|
4
|
-
import {
|
|
5
|
-
getSynapseLaneIdentity
|
|
6
|
-
} from "./index-vkbqdnkj.js";
|
|
7
|
-
import {
|
|
8
|
-
getHarness2
|
|
9
|
-
} from "./index-pavzsdc6.js";
|
|
10
|
-
import {
|
|
11
|
-
getMagicContextStorageDir
|
|
12
|
-
} from "./index-0sahzkwr.js";
|
|
13
|
-
import {
|
|
14
|
-
importPluginModule,
|
|
15
|
-
log2
|
|
16
|
-
} from "./index-nsjyysj0.js";
|
|
17
|
-
import {
|
|
18
|
-
computeNormalizedHash
|
|
19
|
-
} from "./index-2bvekkfy.js";
|
|
20
|
-
import {
|
|
21
|
-
shouldEnforcePrivateStoragePermissions,
|
|
22
|
-
ensureStorageDirectorySync,
|
|
23
|
-
writeStorageFileAsync,
|
|
24
|
-
writeStorageFileHandleAsync,
|
|
25
|
-
createStorageFileHandleAsync
|
|
26
|
-
} from "./index-1bc61ans.js";
|
|
27
1
|
import {
|
|
28
2
|
DEFAULT_PROTECTED_TOOLS,
|
|
29
3
|
mergeProtectedTools
|
|
@@ -3373,7 +3347,7 @@ function initializeContext(params) {
|
|
|
3373
3347
|
external: params?.external ?? undefined
|
|
3374
3348
|
};
|
|
3375
3349
|
}
|
|
3376
|
-
function
|
|
3350
|
+
function process(schema, ctx, _params = { path: [], schemaPath: [] }) {
|
|
3377
3351
|
var _a;
|
|
3378
3352
|
const def = schema._zod.def;
|
|
3379
3353
|
const seen = ctx.seen.get(schema);
|
|
@@ -3410,7 +3384,7 @@ function process2(schema, ctx, _params = { path: [], schemaPath: [] }) {
|
|
|
3410
3384
|
if (parent) {
|
|
3411
3385
|
if (!result.ref)
|
|
3412
3386
|
result.ref = parent;
|
|
3413
|
-
|
|
3387
|
+
process(parent, ctx, params);
|
|
3414
3388
|
ctx.seen.get(parent).isParent = true;
|
|
3415
3389
|
}
|
|
3416
3390
|
}
|
|
@@ -3693,14 +3667,14 @@ function isTransforming(_schema, _ctx) {
|
|
|
3693
3667
|
}
|
|
3694
3668
|
var createToJSONSchemaMethod = (schema, processors = {}) => (params) => {
|
|
3695
3669
|
const ctx = initializeContext({ ...params, processors });
|
|
3696
|
-
|
|
3670
|
+
process(schema, ctx);
|
|
3697
3671
|
extractDefs(ctx, schema);
|
|
3698
3672
|
return finalize(ctx, schema);
|
|
3699
3673
|
};
|
|
3700
3674
|
var createStandardJSONSchemaMethod = (schema, io, processors = {}) => (params) => {
|
|
3701
3675
|
const { libraryOptions, target } = params ?? {};
|
|
3702
3676
|
const ctx = initializeContext({ ...libraryOptions ?? {}, target, io, processors });
|
|
3703
|
-
|
|
3677
|
+
process(schema, ctx);
|
|
3704
3678
|
extractDefs(ctx, schema);
|
|
3705
3679
|
return finalize(ctx, schema);
|
|
3706
3680
|
};
|
|
@@ -3850,7 +3824,7 @@ var arrayProcessor = (schema, ctx, _json, params) => {
|
|
|
3850
3824
|
if (typeof maximum === "number")
|
|
3851
3825
|
json.maxItems = maximum;
|
|
3852
3826
|
json.type = "array";
|
|
3853
|
-
json.items =
|
|
3827
|
+
json.items = process(def.element, ctx, {
|
|
3854
3828
|
...params,
|
|
3855
3829
|
path: [...params.path, "items"]
|
|
3856
3830
|
});
|
|
@@ -3862,7 +3836,7 @@ var objectProcessor = (schema, ctx, _json, params) => {
|
|
|
3862
3836
|
json.properties = {};
|
|
3863
3837
|
const shape = def.shape;
|
|
3864
3838
|
for (const key in shape) {
|
|
3865
|
-
json.properties[key] =
|
|
3839
|
+
json.properties[key] = process(shape[key], ctx, {
|
|
3866
3840
|
...params,
|
|
3867
3841
|
path: [...params.path, "properties", key]
|
|
3868
3842
|
});
|
|
@@ -3885,7 +3859,7 @@ var objectProcessor = (schema, ctx, _json, params) => {
|
|
|
3885
3859
|
if (ctx.io === "output")
|
|
3886
3860
|
json.additionalProperties = false;
|
|
3887
3861
|
} else if (def.catchall) {
|
|
3888
|
-
json.additionalProperties =
|
|
3862
|
+
json.additionalProperties = process(def.catchall, ctx, {
|
|
3889
3863
|
...params,
|
|
3890
3864
|
path: [...params.path, "additionalProperties"]
|
|
3891
3865
|
});
|
|
@@ -3894,7 +3868,7 @@ var objectProcessor = (schema, ctx, _json, params) => {
|
|
|
3894
3868
|
var unionProcessor = (schema, ctx, json, params) => {
|
|
3895
3869
|
const def = schema._zod.def;
|
|
3896
3870
|
const isExclusive = def.inclusive === false;
|
|
3897
|
-
const options = def.options.map((x, i) =>
|
|
3871
|
+
const options = def.options.map((x, i) => process(x, ctx, {
|
|
3898
3872
|
...params,
|
|
3899
3873
|
path: [...params.path, isExclusive ? "oneOf" : "anyOf", i]
|
|
3900
3874
|
}));
|
|
@@ -3906,11 +3880,11 @@ var unionProcessor = (schema, ctx, json, params) => {
|
|
|
3906
3880
|
};
|
|
3907
3881
|
var intersectionProcessor = (schema, ctx, json, params) => {
|
|
3908
3882
|
const def = schema._zod.def;
|
|
3909
|
-
const a =
|
|
3883
|
+
const a = process(def.left, ctx, {
|
|
3910
3884
|
...params,
|
|
3911
3885
|
path: [...params.path, "allOf", 0]
|
|
3912
3886
|
});
|
|
3913
|
-
const b =
|
|
3887
|
+
const b = process(def.right, ctx, {
|
|
3914
3888
|
...params,
|
|
3915
3889
|
path: [...params.path, "allOf", 1]
|
|
3916
3890
|
});
|
|
@@ -3927,11 +3901,11 @@ var tupleProcessor = (schema, ctx, _json, params) => {
|
|
|
3927
3901
|
json.type = "array";
|
|
3928
3902
|
const prefixPath = ctx.target === "draft-2020-12" ? "prefixItems" : "items";
|
|
3929
3903
|
const restPath = ctx.target === "draft-2020-12" ? "items" : ctx.target === "openapi-3.0" ? "items" : "additionalItems";
|
|
3930
|
-
const prefixItems = def.items.map((x, i) =>
|
|
3904
|
+
const prefixItems = def.items.map((x, i) => process(x, ctx, {
|
|
3931
3905
|
...params,
|
|
3932
3906
|
path: [...params.path, prefixPath, i]
|
|
3933
3907
|
}));
|
|
3934
|
-
const rest = def.rest ?
|
|
3908
|
+
const rest = def.rest ? process(def.rest, ctx, {
|
|
3935
3909
|
...params,
|
|
3936
3910
|
path: [...params.path, restPath, ...ctx.target === "openapi-3.0" ? [def.items.length] : []]
|
|
3937
3911
|
}) : null;
|
|
@@ -3971,7 +3945,7 @@ var recordProcessor = (schema, ctx, _json, params) => {
|
|
|
3971
3945
|
const keyBag = keyType._zod.bag;
|
|
3972
3946
|
const patterns = keyBag?.patterns;
|
|
3973
3947
|
if (def.mode === "loose" && patterns && patterns.size > 0) {
|
|
3974
|
-
const valueSchema =
|
|
3948
|
+
const valueSchema = process(def.valueType, ctx, {
|
|
3975
3949
|
...params,
|
|
3976
3950
|
path: [...params.path, "patternProperties", "*"]
|
|
3977
3951
|
});
|
|
@@ -3981,12 +3955,12 @@ var recordProcessor = (schema, ctx, _json, params) => {
|
|
|
3981
3955
|
}
|
|
3982
3956
|
} else {
|
|
3983
3957
|
if (ctx.target === "draft-07" || ctx.target === "draft-2020-12") {
|
|
3984
|
-
json.propertyNames =
|
|
3958
|
+
json.propertyNames = process(def.keyType, ctx, {
|
|
3985
3959
|
...params,
|
|
3986
3960
|
path: [...params.path, "propertyNames"]
|
|
3987
3961
|
});
|
|
3988
3962
|
}
|
|
3989
|
-
json.additionalProperties =
|
|
3963
|
+
json.additionalProperties = process(def.valueType, ctx, {
|
|
3990
3964
|
...params,
|
|
3991
3965
|
path: [...params.path, "additionalProperties"]
|
|
3992
3966
|
});
|
|
@@ -4001,7 +3975,7 @@ var recordProcessor = (schema, ctx, _json, params) => {
|
|
|
4001
3975
|
};
|
|
4002
3976
|
var nullableProcessor = (schema, ctx, json, params) => {
|
|
4003
3977
|
const def = schema._zod.def;
|
|
4004
|
-
const inner =
|
|
3978
|
+
const inner = process(def.innerType, ctx, params);
|
|
4005
3979
|
const seen = ctx.seen.get(schema);
|
|
4006
3980
|
if (ctx.target === "openapi-3.0") {
|
|
4007
3981
|
seen.ref = def.innerType;
|
|
@@ -4012,20 +3986,20 @@ var nullableProcessor = (schema, ctx, json, params) => {
|
|
|
4012
3986
|
};
|
|
4013
3987
|
var nonoptionalProcessor = (schema, ctx, _json, params) => {
|
|
4014
3988
|
const def = schema._zod.def;
|
|
4015
|
-
|
|
3989
|
+
process(def.innerType, ctx, params);
|
|
4016
3990
|
const seen = ctx.seen.get(schema);
|
|
4017
3991
|
seen.ref = def.innerType;
|
|
4018
3992
|
};
|
|
4019
3993
|
var defaultProcessor = (schema, ctx, json, params) => {
|
|
4020
3994
|
const def = schema._zod.def;
|
|
4021
|
-
|
|
3995
|
+
process(def.innerType, ctx, params);
|
|
4022
3996
|
const seen = ctx.seen.get(schema);
|
|
4023
3997
|
seen.ref = def.innerType;
|
|
4024
3998
|
json.default = JSON.parse(JSON.stringify(def.defaultValue));
|
|
4025
3999
|
};
|
|
4026
4000
|
var prefaultProcessor = (schema, ctx, json, params) => {
|
|
4027
4001
|
const def = schema._zod.def;
|
|
4028
|
-
|
|
4002
|
+
process(def.innerType, ctx, params);
|
|
4029
4003
|
const seen = ctx.seen.get(schema);
|
|
4030
4004
|
seen.ref = def.innerType;
|
|
4031
4005
|
if (ctx.io === "input")
|
|
@@ -4033,7 +4007,7 @@ var prefaultProcessor = (schema, ctx, json, params) => {
|
|
|
4033
4007
|
};
|
|
4034
4008
|
var catchProcessor = (schema, ctx, json, params) => {
|
|
4035
4009
|
const def = schema._zod.def;
|
|
4036
|
-
|
|
4010
|
+
process(def.innerType, ctx, params);
|
|
4037
4011
|
const seen = ctx.seen.get(schema);
|
|
4038
4012
|
seen.ref = def.innerType;
|
|
4039
4013
|
let catchValue;
|
|
@@ -4048,20 +4022,20 @@ var pipeProcessor = (schema, ctx, _json, params) => {
|
|
|
4048
4022
|
const def = schema._zod.def;
|
|
4049
4023
|
const inIsTransform = def.in._zod.traits.has("$ZodTransform");
|
|
4050
4024
|
const innerType = ctx.io === "input" ? inIsTransform ? def.out : def.in : def.out;
|
|
4051
|
-
|
|
4025
|
+
process(innerType, ctx, params);
|
|
4052
4026
|
const seen = ctx.seen.get(schema);
|
|
4053
4027
|
seen.ref = innerType;
|
|
4054
4028
|
};
|
|
4055
4029
|
var readonlyProcessor = (schema, ctx, json, params) => {
|
|
4056
4030
|
const def = schema._zod.def;
|
|
4057
|
-
|
|
4031
|
+
process(def.innerType, ctx, params);
|
|
4058
4032
|
const seen = ctx.seen.get(schema);
|
|
4059
4033
|
seen.ref = def.innerType;
|
|
4060
4034
|
json.readOnly = true;
|
|
4061
4035
|
};
|
|
4062
4036
|
var optionalProcessor = (schema, ctx, _json, params) => {
|
|
4063
4037
|
const def = schema._zod.def;
|
|
4064
|
-
|
|
4038
|
+
process(def.innerType, ctx, params);
|
|
4065
4039
|
const seen = ctx.seen.get(schema);
|
|
4066
4040
|
seen.ref = def.innerType;
|
|
4067
4041
|
};
|
|
@@ -5113,6 +5087,234 @@ function nextDueAtMs(expression, afterMs, consumedScheduledAtMs, maxSearchMs = M
|
|
|
5113
5087
|
return next ? next.getTime() : null;
|
|
5114
5088
|
}
|
|
5115
5089
|
|
|
5090
|
+
// src/shared/historian-tool-template.ts
|
|
5091
|
+
class Parser {
|
|
5092
|
+
source;
|
|
5093
|
+
pos = 0;
|
|
5094
|
+
constructor(source) {
|
|
5095
|
+
this.source = source;
|
|
5096
|
+
}
|
|
5097
|
+
error() {
|
|
5098
|
+
throw new Error(`Invalid tool expansion template at character ${this.pos + 1}`);
|
|
5099
|
+
}
|
|
5100
|
+
take(value) {
|
|
5101
|
+
if (!this.source.startsWith(value, this.pos))
|
|
5102
|
+
return false;
|
|
5103
|
+
this.pos += value.length;
|
|
5104
|
+
return true;
|
|
5105
|
+
}
|
|
5106
|
+
field() {
|
|
5107
|
+
const match = /^[A-Za-z_][A-Za-z0-9_-]*/.exec(this.source.slice(this.pos));
|
|
5108
|
+
if (!match)
|
|
5109
|
+
return this.error();
|
|
5110
|
+
this.pos += match[0].length;
|
|
5111
|
+
return match[0];
|
|
5112
|
+
}
|
|
5113
|
+
quoted() {
|
|
5114
|
+
const start = this.pos;
|
|
5115
|
+
if (!this.take('"'))
|
|
5116
|
+
return this.error();
|
|
5117
|
+
while (this.pos < this.source.length) {
|
|
5118
|
+
if (this.take("\\")) {
|
|
5119
|
+
this.pos++;
|
|
5120
|
+
continue;
|
|
5121
|
+
}
|
|
5122
|
+
if (this.take('"')) {
|
|
5123
|
+
try {
|
|
5124
|
+
return JSON.parse(this.source.slice(start, this.pos));
|
|
5125
|
+
} catch {
|
|
5126
|
+
return this.error();
|
|
5127
|
+
}
|
|
5128
|
+
}
|
|
5129
|
+
this.pos++;
|
|
5130
|
+
}
|
|
5131
|
+
return this.error();
|
|
5132
|
+
}
|
|
5133
|
+
integer() {
|
|
5134
|
+
const match = /^\d+/.exec(this.source.slice(this.pos));
|
|
5135
|
+
if (!match)
|
|
5136
|
+
return this.error();
|
|
5137
|
+
this.pos += match[0].length;
|
|
5138
|
+
const n = Number(match[0]);
|
|
5139
|
+
if (!Number.isSafeInteger(n))
|
|
5140
|
+
return this.error();
|
|
5141
|
+
return n;
|
|
5142
|
+
}
|
|
5143
|
+
expression(relative) {
|
|
5144
|
+
const steps = [];
|
|
5145
|
+
if (!(relative && this.take("."))) {
|
|
5146
|
+
const root = this.field();
|
|
5147
|
+
if (!relative && root !== "input" && root !== "output")
|
|
5148
|
+
return this.error();
|
|
5149
|
+
steps.push({ field: root });
|
|
5150
|
+
}
|
|
5151
|
+
while (true) {
|
|
5152
|
+
if (this.take("[")) {
|
|
5153
|
+
if (this.take("*]."))
|
|
5154
|
+
steps.push({ project: this.field() });
|
|
5155
|
+
else {
|
|
5156
|
+
const index = this.integer();
|
|
5157
|
+
if (!this.take("]"))
|
|
5158
|
+
return this.error();
|
|
5159
|
+
steps.push({ index });
|
|
5160
|
+
}
|
|
5161
|
+
} else if (this.source[this.pos] === "." && !/^\.(each\(|join\(|count(?:\.|\})|truncate\()/.test(this.source.slice(this.pos))) {
|
|
5162
|
+
this.pos++;
|
|
5163
|
+
steps.push({ field: this.field() });
|
|
5164
|
+
if (this.source[this.pos] === "(")
|
|
5165
|
+
return this.error();
|
|
5166
|
+
} else
|
|
5167
|
+
break;
|
|
5168
|
+
}
|
|
5169
|
+
const expression = { steps };
|
|
5170
|
+
if (this.take(".each(")) {
|
|
5171
|
+
if (relative)
|
|
5172
|
+
return this.error();
|
|
5173
|
+
expression.each = parseTemplate(this.quoted(), true);
|
|
5174
|
+
if (!this.take(")"))
|
|
5175
|
+
return this.error();
|
|
5176
|
+
}
|
|
5177
|
+
if (this.take(".join(")) {
|
|
5178
|
+
expression.join = this.quoted();
|
|
5179
|
+
if (!this.take(")"))
|
|
5180
|
+
return this.error();
|
|
5181
|
+
}
|
|
5182
|
+
if (this.take(".count")) {
|
|
5183
|
+
if (expression.each || expression.join !== undefined)
|
|
5184
|
+
return this.error();
|
|
5185
|
+
expression.count = true;
|
|
5186
|
+
}
|
|
5187
|
+
if (this.take(".truncate(")) {
|
|
5188
|
+
expression.cap = this.integer();
|
|
5189
|
+
if (!this.take(")"))
|
|
5190
|
+
return this.error();
|
|
5191
|
+
}
|
|
5192
|
+
if (!this.take("}"))
|
|
5193
|
+
return this.error();
|
|
5194
|
+
return expression;
|
|
5195
|
+
}
|
|
5196
|
+
template(relative) {
|
|
5197
|
+
const nodes = [];
|
|
5198
|
+
while (this.pos < this.source.length) {
|
|
5199
|
+
const next = this.source.indexOf("${", this.pos);
|
|
5200
|
+
if (next < 0) {
|
|
5201
|
+
nodes.push(this.source.slice(this.pos));
|
|
5202
|
+
break;
|
|
5203
|
+
}
|
|
5204
|
+
nodes.push(this.source.slice(this.pos, next));
|
|
5205
|
+
this.pos = next + 2;
|
|
5206
|
+
nodes.push(this.expression(relative));
|
|
5207
|
+
}
|
|
5208
|
+
return nodes;
|
|
5209
|
+
}
|
|
5210
|
+
}
|
|
5211
|
+
function parseTemplate(source, relative = false) {
|
|
5212
|
+
return new Parser(source).template(relative);
|
|
5213
|
+
}
|
|
5214
|
+
function toolTemplateError(source) {
|
|
5215
|
+
try {
|
|
5216
|
+
parseTemplate(source);
|
|
5217
|
+
return;
|
|
5218
|
+
} catch (error) {
|
|
5219
|
+
return error.message;
|
|
5220
|
+
}
|
|
5221
|
+
}
|
|
5222
|
+
var templates = new Map;
|
|
5223
|
+
function compiled(source) {
|
|
5224
|
+
if (!templates.has(source)) {
|
|
5225
|
+
try {
|
|
5226
|
+
templates.set(source, parseTemplate(source));
|
|
5227
|
+
} catch {
|
|
5228
|
+
templates.set(source, null);
|
|
5229
|
+
}
|
|
5230
|
+
}
|
|
5231
|
+
return templates.get(source) ?? null;
|
|
5232
|
+
}
|
|
5233
|
+
function oneLine(value) {
|
|
5234
|
+
return value.replace(/[\r\n\u2028\u2029]+/g, " ");
|
|
5235
|
+
}
|
|
5236
|
+
function truncate(value, cap, legacyPreview = false) {
|
|
5237
|
+
const chars = Array.from(oneLine(value));
|
|
5238
|
+
if (cap === undefined || chars.length <= cap)
|
|
5239
|
+
return chars.join("");
|
|
5240
|
+
if (legacyPreview)
|
|
5241
|
+
return `${chars.slice(0, cap).join("")}…`;
|
|
5242
|
+
let end = 0;
|
|
5243
|
+
for (let i = 0;i < Math.min(cap, chars.length); i++) {
|
|
5244
|
+
if (!".!?。!?".includes(chars[i]))
|
|
5245
|
+
continue;
|
|
5246
|
+
let next = i + 1;
|
|
5247
|
+
while (next < chars.length && `"'”’)]`.includes(chars[next]))
|
|
5248
|
+
next++;
|
|
5249
|
+
if (next <= cap && ("。!?".includes(chars[i]) || next === chars.length || /[ \t]/.test(chars[next])))
|
|
5250
|
+
end = next;
|
|
5251
|
+
}
|
|
5252
|
+
return `${end ? `${chars.slice(0, end).join("")} ` : ""}[… ${chars.length - end} more characters]`;
|
|
5253
|
+
}
|
|
5254
|
+
function scalar(value) {
|
|
5255
|
+
if (value === undefined)
|
|
5256
|
+
return "";
|
|
5257
|
+
return typeof value === "string" ? value : compactJson(value);
|
|
5258
|
+
}
|
|
5259
|
+
function compactJson(value) {
|
|
5260
|
+
if (Array.isArray(value))
|
|
5261
|
+
return `[${value.map((v) => compactJson(v)).join(",")}]`;
|
|
5262
|
+
if (value !== null && typeof value === "object")
|
|
5263
|
+
return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${compactJson(value[key])}`).join(",")}}`;
|
|
5264
|
+
return JSON.stringify(value) ?? "null";
|
|
5265
|
+
}
|
|
5266
|
+
function field(value, key) {
|
|
5267
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) && Object.hasOwn(value, key) ? value[key] : undefined;
|
|
5268
|
+
}
|
|
5269
|
+
function render(nodes, root, legacyPreview) {
|
|
5270
|
+
return nodes.map((node) => {
|
|
5271
|
+
if (typeof node === "string")
|
|
5272
|
+
return node;
|
|
5273
|
+
const cap = node.cap ?? (legacyPreview ? 300 : undefined);
|
|
5274
|
+
let value = root;
|
|
5275
|
+
let list = false;
|
|
5276
|
+
for (const step of node.steps) {
|
|
5277
|
+
if ("field" in step)
|
|
5278
|
+
value = list && Array.isArray(value) ? value.map((item) => field(item, step.field)) : field(value, step.field);
|
|
5279
|
+
else if ("index" in step)
|
|
5280
|
+
value = Array.isArray(value) ? value[step.index] : undefined;
|
|
5281
|
+
else {
|
|
5282
|
+
value = Array.isArray(value) ? value.map((item) => field(item, step.project)) : undefined;
|
|
5283
|
+
list = true;
|
|
5284
|
+
}
|
|
5285
|
+
}
|
|
5286
|
+
if (node.count)
|
|
5287
|
+
return truncate(Array.isArray(value) ? String(value.length) : "", cap, legacyPreview);
|
|
5288
|
+
if (node.each || list || node.join !== undefined) {
|
|
5289
|
+
if (!Array.isArray(value))
|
|
5290
|
+
return "";
|
|
5291
|
+
const elements = value.slice(0, legacyPreview ? 10 : undefined).map((item) => truncate(node.each ? render(node.each, item, legacyPreview) : scalar(item), legacyPreview ? 300 : undefined, legacyPreview));
|
|
5292
|
+
if (legacyPreview && value.length > 10)
|
|
5293
|
+
elements.push(`… +${value.length - 10} more`);
|
|
5294
|
+
return truncate(elements.join(node.join ?? (node.each ? "; " : ", ")), cap, legacyPreview);
|
|
5295
|
+
}
|
|
5296
|
+
return truncate(scalar(value), cap, legacyPreview);
|
|
5297
|
+
}).join("");
|
|
5298
|
+
}
|
|
5299
|
+
function renderToolTemplate(source, input, output, legacyPreview = false) {
|
|
5300
|
+
const nodes = compiled(source);
|
|
5301
|
+
if (!nodes)
|
|
5302
|
+
return null;
|
|
5303
|
+
const text = typeof output === "string" ? output : output === undefined ? "" : compactJson(output);
|
|
5304
|
+
let structured = output;
|
|
5305
|
+
if (typeof output === "string") {
|
|
5306
|
+
try {
|
|
5307
|
+
const parsed = JSON.parse(output);
|
|
5308
|
+
structured = parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : undefined;
|
|
5309
|
+
} catch {
|
|
5310
|
+
structured = undefined;
|
|
5311
|
+
}
|
|
5312
|
+
}
|
|
5313
|
+
const root = { input, output: structured };
|
|
5314
|
+
const bareOutputNodes = nodes.map((node) => typeof node !== "string" && node.steps.length === 1 && ("field" in node.steps[0]) && node.steps[0].field === "output" && !node.each && !node.count && node.join === undefined ? truncate(text ?? "", node.cap ?? (legacyPreview ? 300 : undefined), legacyPreview) : node);
|
|
5315
|
+
return truncate(render(bareOutputNodes, root, legacyPreview), legacyPreview ? 1000 : undefined, legacyPreview);
|
|
5316
|
+
}
|
|
5317
|
+
|
|
5116
5318
|
// src/shared/prompt-surface.ts
|
|
5117
5319
|
function promptSurfaceConfigIdentity(config) {
|
|
5118
5320
|
return JSON.stringify({
|
|
@@ -5521,7 +5723,7 @@ var BaseEmbeddingConfigSchema = object({
|
|
|
5521
5723
|
document_prefix: string().optional().describe("OpenAI-compatible stored-document prefix override, prepended verbatim. Defaults to the model-family recipe (empty for Qwen3/gte/e5 instruct; 'search_document: ' for Nomic). Changing it changes stored vectors and triggers re-embedding. User-level only; project values are ignored."),
|
|
5522
5724
|
truncate: string().optional().describe("Optional truncate mode sent in the embedding request body (e.g. NVIDIA NIM accepts 'NONE' | 'START' | 'END'). Omitted from the request when unset."),
|
|
5523
5725
|
max_input_tokens: number2().int().positive().optional().describe("Optional maximum input tokens for chunk embeddings. Defaults conservatively to 512 when omitted."),
|
|
5524
|
-
local_runtime: _enum(["auto", "native", "wasm"]).default("auto").describe("Local provider only: ONNX runtime selection. 'auto' uses native under Node and uses WASM under Bun versions before 1.4.0, where Bun's NAPI teardown race can panic on quit; native is restored automatically on Bun 1.4.0+. Inference runs in a dedicated worker. On Bun before 1.4.0, even explicit 'native' uses WASM because native worker teardown can crash the process. Set 'wasm' to avoid loading the native addon on newer hosts too."),
|
|
5726
|
+
local_runtime: _enum(["auto", "native", "wasm"]).default("auto").describe("Local provider only: ONNX runtime selection. 'auto' uses native under Node and uses WASM under Bun versions before 1.4.0, where Bun's NAPI teardown race can panic on quit; native is restored automatically on Bun 1.4.0+. Inference runs in a dedicated worker after disabling POSIX telemetry in the native process environment. Compiled Bun hosts and hosts without a verified in-process opt-out use a subprocess instead. Windows native ONNX emits OS-controlled ETW diagnostics, not direct uploads. On Bun before 1.4.0, even explicit 'native' uses WASM because native worker teardown can crash the process. Set 'wasm' to avoid loading the native addon on newer hosts too."),
|
|
5525
5727
|
local_dtype: _enum([
|
|
5526
5728
|
"auto",
|
|
5527
5729
|
"fp32",
|
|
@@ -5630,8 +5832,8 @@ var MagicContextConfigSchema = object({
|
|
|
5630
5832
|
object({ default: number2().min(0) }).catchall(number2().min(0))
|
|
5631
5833
|
]).optional().describe('User-only output-token reservation override. Number or per-model object ({ default: 16384, "provider/model": 8192 }); 0 disables reservation. Takes precedence over every derived source: an explicit value here always wins against catalog output limits, provider window-geometry facts, and the 25%-of-context fallback (usable window = context window minus this reserve). When unset, Magic Context reserves the catalog output limit (capped at 25% of context) for shared-window providers and keeps proven separate-quota Google/Gemini windows unchanged.'),
|
|
5632
5834
|
models: object({
|
|
5633
|
-
window_overlay_path:
|
|
5634
|
-
}).optional().describe("
|
|
5835
|
+
window_overlay_path: unknown().optional().describe("Deprecated: ignored. Fusiform no longer publishes window-overlay data, so this setting no longer changes model window geometry. Remove this key.").meta({ deprecated: true })
|
|
5836
|
+
}).optional().describe("Deprecated user-only Fusiform window-overlay settings; ignored."),
|
|
5635
5837
|
toast_duration_ms: number2().min(0).max(60000).default(5000).describe("TUI toast lifetime in milliseconds for Magic Context notifications. Set to 0 to disable Magic Context toasts entirely (min: 0, max: 60000, default: 5000)"),
|
|
5636
5838
|
execute_threshold_percentage: union([
|
|
5637
5839
|
number2().min(20).max(90, EXECUTE_THRESHOLD_CAP_MESSAGE),
|
|
@@ -5797,1026 +5999,4 @@ function deriveDefaultProtectedTokens(usableSoft) {
|
|
|
5797
5999
|
return Math.min(64000, Math.max(lowerBound, target));
|
|
5798
6000
|
}
|
|
5799
6001
|
|
|
5800
|
-
|
|
5801
|
-
function parseEmbeddingModel(model) {
|
|
5802
|
-
const lastColon = model.lastIndexOf(":");
|
|
5803
|
-
if (lastColon > model.lastIndexOf("/")) {
|
|
5804
|
-
return { base: model.slice(0, lastColon), tag: model.slice(lastColon + 1) };
|
|
5805
|
-
}
|
|
5806
|
-
return { base: model };
|
|
5807
|
-
}
|
|
5808
|
-
function normalizeEmbeddingModelId(model) {
|
|
5809
|
-
return parseEmbeddingModel(model.trim().toLowerCase()).base;
|
|
5810
|
-
}
|
|
5811
|
-
function matchNormalizedEmbeddingModels(a, b) {
|
|
5812
|
-
if (a.length === 0 || b.length === 0)
|
|
5813
|
-
return true;
|
|
5814
|
-
if (a === b)
|
|
5815
|
-
return true;
|
|
5816
|
-
const longer = a.length >= b.length ? a : b;
|
|
5817
|
-
const shorter = a.length >= b.length ? b : a;
|
|
5818
|
-
const isBoundary = (ch) => ch === "-" || ch === "/";
|
|
5819
|
-
if (longer.startsWith(shorter) && isBoundary(longer.charAt(shorter.length)))
|
|
5820
|
-
return true;
|
|
5821
|
-
if (longer.endsWith(shorter) && isBoundary(longer.charAt(longer.length - shorter.length - 1)))
|
|
5822
|
-
return true;
|
|
5823
|
-
return false;
|
|
5824
|
-
}
|
|
5825
|
-
function embeddingModelsMatch(served, requested) {
|
|
5826
|
-
const servedNormalized = served.trim().toLowerCase();
|
|
5827
|
-
const requestedNormalized = requested.trim().toLowerCase();
|
|
5828
|
-
const servedModel = parseEmbeddingModel(servedNormalized);
|
|
5829
|
-
const requestedModel = parseEmbeddingModel(requestedNormalized);
|
|
5830
|
-
if (servedModel.tag !== undefined && requestedModel.tag !== undefined && servedModel.tag !== requestedModel.tag) {
|
|
5831
|
-
return false;
|
|
5832
|
-
}
|
|
5833
|
-
return matchNormalizedEmbeddingModels(servedModel.base, requestedModel.base);
|
|
5834
|
-
}
|
|
5835
|
-
var QWEN3_QUERY_INSTRUCTION = `Instruct: Given a web search query, retrieve relevant passages that answer the query
|
|
5836
|
-
Query: `;
|
|
5837
|
-
var INSTRUCT_WEB_SEARCH_QUERY_PREFIX = QWEN3_QUERY_INSTRUCTION;
|
|
5838
|
-
var EMBEDDING_MODEL_PREFIX_FAMILIES = [
|
|
5839
|
-
{
|
|
5840
|
-
family: "qwen3-embedding",
|
|
5841
|
-
basenamePattern: /^qwen3-embedding(?:-|$)/,
|
|
5842
|
-
prefixes: { queryPrefix: QWEN3_QUERY_INSTRUCTION, documentPrefix: "" }
|
|
5843
|
-
},
|
|
5844
|
-
{
|
|
5845
|
-
family: "gte-qwen-instruct",
|
|
5846
|
-
basenamePattern: /^gte-qwen.*-instruct(?:-|$)/,
|
|
5847
|
-
prefixes: { queryPrefix: INSTRUCT_WEB_SEARCH_QUERY_PREFIX, documentPrefix: "" }
|
|
5848
|
-
},
|
|
5849
|
-
{
|
|
5850
|
-
family: "e5-instruct",
|
|
5851
|
-
basenamePattern: /^(?:multilingual-)?e5-.*-instruct(?:-|$)/,
|
|
5852
|
-
prefixes: { queryPrefix: INSTRUCT_WEB_SEARCH_QUERY_PREFIX, documentPrefix: "" }
|
|
5853
|
-
},
|
|
5854
|
-
{
|
|
5855
|
-
family: "nomic-embed-text",
|
|
5856
|
-
basenamePattern: /^nomic-embed-text(?:-|$)/,
|
|
5857
|
-
prefixes: { queryPrefix: "search_query: ", documentPrefix: "search_document: " }
|
|
5858
|
-
}
|
|
5859
|
-
];
|
|
5860
|
-
function modelBasename(model) {
|
|
5861
|
-
const normalized = normalizeEmbeddingModelId(model);
|
|
5862
|
-
return normalized.slice(normalized.lastIndexOf("/") + 1);
|
|
5863
|
-
}
|
|
5864
|
-
function resolveEmbeddingTextPrefixes(model, queryInstruction, documentPrefix) {
|
|
5865
|
-
const family = EMBEDDING_MODEL_PREFIX_FAMILIES.find(({ basenamePattern }) => basenamePattern.test(modelBasename(model)));
|
|
5866
|
-
return {
|
|
5867
|
-
queryPrefix: queryInstruction === false ? "" : queryInstruction !== undefined ? queryInstruction : family?.prefixes.queryPrefix ?? "",
|
|
5868
|
-
documentPrefix: documentPrefix ?? family?.prefixes.documentPrefix ?? ""
|
|
5869
|
-
};
|
|
5870
|
-
}
|
|
5871
|
-
|
|
5872
|
-
// src/features/magic-context/memory/embedding-identity.ts
|
|
5873
|
-
function normalizeEndpoint(endpoint) {
|
|
5874
|
-
return endpoint?.trim().replace(/\/+$/, "") ?? "";
|
|
5875
|
-
}
|
|
5876
|
-
var LOCAL_EMBEDDING_RUNTIME_FINGERPRINT = "transformers@4.3.0;onnxruntime-node@1.30.0;onnxruntime-web@1.26.0-dev.20260416-b7804b056c";
|
|
5877
|
-
function getEmbeddingProviderIdentity(config) {
|
|
5878
|
-
if (config.provider === "off") {
|
|
5879
|
-
return "embedding-provider:off";
|
|
5880
|
-
}
|
|
5881
|
-
if (config.provider === "synapse") {
|
|
5882
|
-
const resolved = config;
|
|
5883
|
-
if (!resolved.model || !resolved.synapse_fingerprint)
|
|
5884
|
-
return "synapse:v1:pending";
|
|
5885
|
-
return getSynapseLaneIdentity(resolved.model, resolved.synapse_fingerprint);
|
|
5886
|
-
}
|
|
5887
|
-
if (config.provider !== "local" && config.provider !== "openai-compatible") {
|
|
5888
|
-
throw new Error("Unknown embedding provider");
|
|
5889
|
-
}
|
|
5890
|
-
const truncate = config.provider === "openai-compatible" ? config.truncate?.trim() : undefined;
|
|
5891
|
-
const documentPrefix = config.provider === "openai-compatible" ? resolveEmbeddingTextPrefixes(config.model, config.query_instruction, config.document_prefix).documentPrefix : "";
|
|
5892
|
-
const localDtype = config.provider === "local" && config.local_dtype && config.local_dtype !== "fp32" ? config.local_dtype : undefined;
|
|
5893
|
-
const identityInput = config.provider === "openai-compatible" ? {
|
|
5894
|
-
provider: "openai-compatible",
|
|
5895
|
-
model: config.model.trim(),
|
|
5896
|
-
endpoint: normalizeEndpoint(config.endpoint),
|
|
5897
|
-
apiKeyPresent: Boolean(config.api_key?.trim()),
|
|
5898
|
-
inputType: config.input_type?.trim() || "",
|
|
5899
|
-
...documentPrefix ? { documentPrefix } : {},
|
|
5900
|
-
...truncate ? { truncate } : {}
|
|
5901
|
-
} : {
|
|
5902
|
-
provider: "local",
|
|
5903
|
-
model: config.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL,
|
|
5904
|
-
endpoint: "",
|
|
5905
|
-
apiKeyPresent: false,
|
|
5906
|
-
runtimeFingerprint: LOCAL_EMBEDDING_RUNTIME_FINGERPRINT,
|
|
5907
|
-
...localDtype ? { localDtype } : {}
|
|
5908
|
-
};
|
|
5909
|
-
return `embedding-provider:${computeNormalizedHash(JSON.stringify(identityInput))}`;
|
|
5910
|
-
}
|
|
5911
|
-
|
|
5912
|
-
// src/features/magic-context/memory/embedding-local.ts
|
|
5913
|
-
import { readdirSync, statSync } from "node:fs";
|
|
5914
|
-
import { stat, unlink } from "node:fs/promises";
|
|
5915
|
-
import { dirname, join } from "node:path";
|
|
5916
|
-
import { pathToFileURL } from "node:url";
|
|
5917
|
-
import { workerData } from "node:worker_threads";
|
|
5918
|
-
|
|
5919
|
-
// src/features/magic-context/memory/embedding-failure.ts
|
|
5920
|
-
function errorMessage(error) {
|
|
5921
|
-
return error instanceof Error ? error.message : String(error ?? "unknown error");
|
|
5922
|
-
}
|
|
5923
|
-
function nestedMessages(error) {
|
|
5924
|
-
const messages = [errorMessage(error)];
|
|
5925
|
-
if (error && typeof error === "object" && "cause" in error) {
|
|
5926
|
-
const cause = error.cause;
|
|
5927
|
-
if (cause !== undefined)
|
|
5928
|
-
messages.push(...nestedMessages(cause));
|
|
5929
|
-
}
|
|
5930
|
-
return messages;
|
|
5931
|
-
}
|
|
5932
|
-
function safeErrorDetail(message) {
|
|
5933
|
-
return message.replace(/https?:\/\/[^\s"']+/gi, "<remote URL>").replace(/\/(?:Users|home)\/[^/\s]+/g, "~").replace(/\s+/g, " ").trim().slice(0, 240);
|
|
5934
|
-
}
|
|
5935
|
-
function hasAny(messages, patterns) {
|
|
5936
|
-
return messages.some((message) => {
|
|
5937
|
-
const lower = message.toLowerCase();
|
|
5938
|
-
return patterns.some((pattern) => lower.includes(pattern));
|
|
5939
|
-
});
|
|
5940
|
-
}
|
|
5941
|
-
function classifyLocalEmbeddingFailure(error, context = {}) {
|
|
5942
|
-
const primaryMessages = nestedMessages(error);
|
|
5943
|
-
const nativeMessages = context.nativeError === undefined ? [] : nestedMessages(context.nativeError);
|
|
5944
|
-
if (hasAny(primaryMessages, [
|
|
5945
|
-
"mc_embedding_fs_unavailable",
|
|
5946
|
-
"file system cache is not available"
|
|
5947
|
-
]) || context.usesWasm && hasAny(primaryMessages, ["unable to get model file path or buffer"])) {
|
|
5948
|
-
return {
|
|
5949
|
-
class: "local_fs_unavailable",
|
|
5950
|
-
reason: "the WASM model cache cannot access the Node filesystem",
|
|
5951
|
-
retryable: false
|
|
5952
|
-
};
|
|
5953
|
-
}
|
|
5954
|
-
if (hasAny(primaryMessages, [
|
|
5955
|
-
"failed to fetch",
|
|
5956
|
-
"fetch failed",
|
|
5957
|
-
"network error",
|
|
5958
|
-
"enotfound",
|
|
5959
|
-
"econnreset",
|
|
5960
|
-
"econnrefused",
|
|
5961
|
-
"certificate",
|
|
5962
|
-
"unable to load file",
|
|
5963
|
-
"unauthorized access to file",
|
|
5964
|
-
"forbidden access to file",
|
|
5965
|
-
"not found at"
|
|
5966
|
-
]) || primaryMessages.some((message) => /\bHTTP\s+[45]\d\d\b/i.test(message))) {
|
|
5967
|
-
return {
|
|
5968
|
-
class: "local_download_failure",
|
|
5969
|
-
reason: `the embedding model download failed: ${safeErrorDetail(primaryMessages[0])}`,
|
|
5970
|
-
retryable: true
|
|
5971
|
-
};
|
|
5972
|
-
}
|
|
5973
|
-
const bindingMessages = [...primaryMessages, ...nativeMessages];
|
|
5974
|
-
if (hasAny(bindingMessages, [
|
|
5975
|
-
"onnxruntime_binding.node",
|
|
5976
|
-
"cannot find package 'onnxruntime-node'",
|
|
5977
|
-
'cannot find package "onnxruntime-node"',
|
|
5978
|
-
"cannot find module 'onnxruntime-node'",
|
|
5979
|
-
'cannot find module "onnxruntime-node"',
|
|
5980
|
-
"could not resolve: onnxruntime-node"
|
|
5981
|
-
])) {
|
|
5982
|
-
const platform = context.platform ?? process.platform;
|
|
5983
|
-
const arch = context.arch ?? process.arch;
|
|
5984
|
-
return {
|
|
5985
|
-
class: "local_binding_missing",
|
|
5986
|
-
reason: platform === "darwin" && arch === "x64" ? "onnxruntime-node has no darwin/x64 native binding and the WASM fallback could not complete" : `onnxruntime-node has no usable native binding for ${platform}/${arch} and the WASM fallback could not complete`,
|
|
5987
|
-
retryable: false
|
|
5988
|
-
};
|
|
5989
|
-
}
|
|
5990
|
-
return {
|
|
5991
|
-
class: "local_runtime_error",
|
|
5992
|
-
reason: `the local embedding runtime failed: ${safeErrorDetail(primaryMessages[0])}`,
|
|
5993
|
-
retryable: false
|
|
5994
|
-
};
|
|
5995
|
-
}
|
|
5996
|
-
function dominantEmbeddingFailure(failures) {
|
|
5997
|
-
const counts = new Map;
|
|
5998
|
-
for (const failure of failures) {
|
|
5999
|
-
const key = `${failure.class}\x00${failure.reason}`;
|
|
6000
|
-
const current = counts.get(key);
|
|
6001
|
-
if (current)
|
|
6002
|
-
current.count += 1;
|
|
6003
|
-
else
|
|
6004
|
-
counts.set(key, { failure, count: 1 });
|
|
6005
|
-
}
|
|
6006
|
-
let dominant;
|
|
6007
|
-
for (const candidate of counts.values()) {
|
|
6008
|
-
if (!dominant || candidate.count > dominant.count)
|
|
6009
|
-
dominant = candidate;
|
|
6010
|
-
}
|
|
6011
|
-
return dominant?.failure;
|
|
6012
|
-
}
|
|
6013
|
-
|
|
6014
|
-
// src/features/magic-context/memory/embedding-worker-client.ts
|
|
6015
|
-
import { Worker } from "node:worker_threads";
|
|
6016
|
-
class EmbeddingWorkerClient {
|
|
6017
|
-
data;
|
|
6018
|
-
entry;
|
|
6019
|
-
timeoutMs;
|
|
6020
|
-
worker = null;
|
|
6021
|
-
nextId = 1;
|
|
6022
|
-
pending = new Map;
|
|
6023
|
-
disposed = false;
|
|
6024
|
-
constructor(data, entry = new URL(new URL(import.meta.url).pathname.endsWith(".ts") ? "./embedding-worker.ts" : "./embedding-worker.js", import.meta.url), timeoutMs = 5 * 60000) {
|
|
6025
|
-
this.data = data;
|
|
6026
|
-
this.entry = entry;
|
|
6027
|
-
this.timeoutMs = timeoutMs;
|
|
6028
|
-
}
|
|
6029
|
-
start() {
|
|
6030
|
-
if (this.disposed)
|
|
6031
|
-
throw new Error("embedding worker disposed");
|
|
6032
|
-
if (this.worker)
|
|
6033
|
-
return this.worker;
|
|
6034
|
-
const worker = new Worker(this.entry, { workerData: this.data });
|
|
6035
|
-
this.worker = worker;
|
|
6036
|
-
worker.unref();
|
|
6037
|
-
worker.on("message", (reply) => {
|
|
6038
|
-
if (this.worker !== worker)
|
|
6039
|
-
return;
|
|
6040
|
-
const request = this.pending.get(reply.id);
|
|
6041
|
-
if (!request)
|
|
6042
|
-
return;
|
|
6043
|
-
clearTimeout(request.timer);
|
|
6044
|
-
this.pending.delete(reply.id);
|
|
6045
|
-
if (reply.error)
|
|
6046
|
-
request.reject(new Error(reply.error));
|
|
6047
|
-
else
|
|
6048
|
-
request.resolve(reply);
|
|
6049
|
-
if (this.pending.size === 0)
|
|
6050
|
-
worker.unref();
|
|
6051
|
-
});
|
|
6052
|
-
const fail = (error) => {
|
|
6053
|
-
if (this.worker !== worker)
|
|
6054
|
-
return;
|
|
6055
|
-
this.worker = null;
|
|
6056
|
-
for (const request of this.pending.values()) {
|
|
6057
|
-
clearTimeout(request.timer);
|
|
6058
|
-
request.reject(error);
|
|
6059
|
-
}
|
|
6060
|
-
this.pending.clear();
|
|
6061
|
-
log2("[magic-context] embedding worker exited unexpectedly:", error);
|
|
6062
|
-
worker.terminate();
|
|
6063
|
-
};
|
|
6064
|
-
worker.on("error", fail);
|
|
6065
|
-
worker.on("exit", (code) => fail(new Error(`embedding worker exited (${code})`)));
|
|
6066
|
-
return worker;
|
|
6067
|
-
}
|
|
6068
|
-
isRunning() {
|
|
6069
|
-
return this.worker !== null;
|
|
6070
|
-
}
|
|
6071
|
-
request(texts, signal) {
|
|
6072
|
-
if (signal?.aborted)
|
|
6073
|
-
return Promise.reject(new Error("embedding request aborted"));
|
|
6074
|
-
return new Promise((resolve, reject) => {
|
|
6075
|
-
const worker = this.start();
|
|
6076
|
-
const id = this.nextId++;
|
|
6077
|
-
const timer = setTimeout(() => {
|
|
6078
|
-
this.stop(new Error("embedding worker request timed out"));
|
|
6079
|
-
}, this.timeoutMs);
|
|
6080
|
-
const abort = () => {
|
|
6081
|
-
clearTimeout(timer);
|
|
6082
|
-
this.pending.delete(id);
|
|
6083
|
-
signal?.removeEventListener("abort", abort);
|
|
6084
|
-
reject(new Error("embedding request aborted"));
|
|
6085
|
-
if (this.pending.size === 0)
|
|
6086
|
-
worker.unref();
|
|
6087
|
-
};
|
|
6088
|
-
signal?.addEventListener("abort", abort, { once: true });
|
|
6089
|
-
const cleanup = () => signal?.removeEventListener("abort", abort);
|
|
6090
|
-
this.pending.set(id, {
|
|
6091
|
-
timer,
|
|
6092
|
-
resolve: (reply) => {
|
|
6093
|
-
cleanup();
|
|
6094
|
-
resolve(reply);
|
|
6095
|
-
},
|
|
6096
|
-
reject: (error) => {
|
|
6097
|
-
cleanup();
|
|
6098
|
-
reject(error);
|
|
6099
|
-
}
|
|
6100
|
-
});
|
|
6101
|
-
worker.ref();
|
|
6102
|
-
try {
|
|
6103
|
-
worker.postMessage({ id, texts });
|
|
6104
|
-
} catch (error) {
|
|
6105
|
-
this.stop(error instanceof Error ? error : new Error(String(error)));
|
|
6106
|
-
}
|
|
6107
|
-
});
|
|
6108
|
-
}
|
|
6109
|
-
async stop(error) {
|
|
6110
|
-
const worker = this.worker;
|
|
6111
|
-
this.worker = null;
|
|
6112
|
-
for (const request of this.pending.values()) {
|
|
6113
|
-
clearTimeout(request.timer);
|
|
6114
|
-
request.reject(error);
|
|
6115
|
-
}
|
|
6116
|
-
this.pending.clear();
|
|
6117
|
-
await worker?.terminate();
|
|
6118
|
-
}
|
|
6119
|
-
async dispose() {
|
|
6120
|
-
this.disposed = true;
|
|
6121
|
-
await this.stop(new Error("embedding worker disposed"));
|
|
6122
|
-
}
|
|
6123
|
-
}
|
|
6124
|
-
|
|
6125
|
-
// src/features/magic-context/memory/transformers-remote-host.ts
|
|
6126
|
-
var DEFAULT_TRANSFORMERS_REMOTE_HOST = "https://huggingface.co/";
|
|
6127
|
-
function readHuggingFaceEndpoint() {
|
|
6128
|
-
return globalThis.process?.env?.HF_ENDPOINT;
|
|
6129
|
-
}
|
|
6130
|
-
function configureTransformersRemoteHost(env, endpoint = readHuggingFaceEndpoint()) {
|
|
6131
|
-
const normalized = endpoint?.trim().replace(/\/+$/, "");
|
|
6132
|
-
env.remoteHost = normalized ? `${normalized}/` : DEFAULT_TRANSFORMERS_REMOTE_HOST;
|
|
6133
|
-
}
|
|
6134
|
-
|
|
6135
|
-
// src/features/magic-context/memory/embedding-local.ts
|
|
6136
|
-
var BUN_NAPI_TEARDOWN_FIX_VERSION = [1, 4, 0];
|
|
6137
|
-
function currentLocalEmbeddingHost() {
|
|
6138
|
-
const bun = globalThis.Bun;
|
|
6139
|
-
const hasProcess = typeof process !== "undefined";
|
|
6140
|
-
const bunVersion = hasProcess && typeof process.versions?.bun === "string" ? process.versions.bun : undefined;
|
|
6141
|
-
return {
|
|
6142
|
-
isElectron: hasProcess && Boolean(process.versions?.electron),
|
|
6143
|
-
isBun: Boolean(bun) || Boolean(bunVersion),
|
|
6144
|
-
bunVersion,
|
|
6145
|
-
hasNodeFilesystem: hasProcess && (typeof process.versions?.node === "string" || typeof bunVersion === "string")
|
|
6146
|
-
};
|
|
6147
|
-
}
|
|
6148
|
-
function bunHasNapiTeardownFix(version) {
|
|
6149
|
-
const match = version?.match(/^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/);
|
|
6150
|
-
if (!match || match[4])
|
|
6151
|
-
return false;
|
|
6152
|
-
const parsed = [Number(match[1]), Number(match[2]), Number(match[3])];
|
|
6153
|
-
return parsed.some((part) => !Number.isSafeInteger(part)) ? false : parsed[0] > BUN_NAPI_TEARDOWN_FIX_VERSION[0] || parsed[0] === BUN_NAPI_TEARDOWN_FIX_VERSION[0] && (parsed[1] > BUN_NAPI_TEARDOWN_FIX_VERSION[1] || parsed[1] === BUN_NAPI_TEARDOWN_FIX_VERSION[1] && parsed[2] >= BUN_NAPI_TEARDOWN_FIX_VERSION[2]);
|
|
6154
|
-
}
|
|
6155
|
-
function resolveLocalEmbeddingRuntime(preference = "auto", host = currentLocalEmbeddingHost()) {
|
|
6156
|
-
if (preference === "native" || preference === "wasm")
|
|
6157
|
-
return preference;
|
|
6158
|
-
if (host.isElectron)
|
|
6159
|
-
return "electron";
|
|
6160
|
-
if (host.isBun && !bunHasNapiTeardownFix(host.bunVersion))
|
|
6161
|
-
return "wasm";
|
|
6162
|
-
return "native";
|
|
6163
|
-
}
|
|
6164
|
-
function resolveLocalEmbeddingWorkerRuntime(preference = "auto", host = currentLocalEmbeddingHost()) {
|
|
6165
|
-
if (host.isBun && !bunHasNapiTeardownFix(host.bunVersion) && preference === "native")
|
|
6166
|
-
return "wasm";
|
|
6167
|
-
return resolveLocalEmbeddingRuntime(preference, host);
|
|
6168
|
-
}
|
|
6169
|
-
var LOCK_POLL_MS = 150;
|
|
6170
|
-
var STALE_LOCK_MS = 3 * 60000;
|
|
6171
|
-
var MAX_LOCK_WAIT_MS = 5 * 60000;
|
|
6172
|
-
async function acquireModelLoadLock(lockPath) {
|
|
6173
|
-
const waitStart = Date.now();
|
|
6174
|
-
while (true) {
|
|
6175
|
-
try {
|
|
6176
|
-
const handle = await createStorageFileHandleAsync(lockPath, "wx");
|
|
6177
|
-
try {
|
|
6178
|
-
await writeStorageFileHandleAsync(handle, `pid=${process.pid} started=${Date.now()}
|
|
6179
|
-
`);
|
|
6180
|
-
} catch {}
|
|
6181
|
-
await handle.close();
|
|
6182
|
-
return async () => {
|
|
6183
|
-
try {
|
|
6184
|
-
await unlink(lockPath);
|
|
6185
|
-
} catch {}
|
|
6186
|
-
};
|
|
6187
|
-
} catch (error) {
|
|
6188
|
-
const code = error.code;
|
|
6189
|
-
if (code !== "EEXIST" && code !== "EPERM") {
|
|
6190
|
-
throw error;
|
|
6191
|
-
}
|
|
6192
|
-
try {
|
|
6193
|
-
const info = await stat(lockPath);
|
|
6194
|
-
if (Date.now() - info.mtimeMs > STALE_LOCK_MS) {
|
|
6195
|
-
log2(`[magic-context] embedding-load lock stale (>${STALE_LOCK_MS}ms), taking over`);
|
|
6196
|
-
try {
|
|
6197
|
-
await unlink(lockPath);
|
|
6198
|
-
} catch {}
|
|
6199
|
-
continue;
|
|
6200
|
-
}
|
|
6201
|
-
} catch {
|
|
6202
|
-
continue;
|
|
6203
|
-
}
|
|
6204
|
-
if (Date.now() - waitStart > MAX_LOCK_WAIT_MS) {
|
|
6205
|
-
throw new Error(`[magic-context] embedding-load lock wait exceeded ${MAX_LOCK_WAIT_MS}ms; another process is still loading the model. Skipping this init attempt to avoid an unsynchronized native load.`);
|
|
6206
|
-
}
|
|
6207
|
-
await new Promise((resolve) => setTimeout(resolve, LOCK_POLL_MS));
|
|
6208
|
-
}
|
|
6209
|
-
}
|
|
6210
|
-
}
|
|
6211
|
-
function startLockHeartbeat(lockPath) {
|
|
6212
|
-
const HEARTBEAT_MS = Math.floor(STALE_LOCK_MS / 3);
|
|
6213
|
-
const timer = setInterval(() => {
|
|
6214
|
-
writeStorageFileAsync(lockPath, `pid=${process.pid} alive=${Date.now()}
|
|
6215
|
-
`).catch(() => {});
|
|
6216
|
-
}, HEARTBEAT_MS);
|
|
6217
|
-
timer.unref?.();
|
|
6218
|
-
return () => clearInterval(timer);
|
|
6219
|
-
}
|
|
6220
|
-
var ONNX_RUNTIME_WEB_SPECIFIER = "onnxruntime-web";
|
|
6221
|
-
var useInThreadTestRuntime = false;
|
|
6222
|
-
var workerFactoryForTests;
|
|
6223
|
-
var localEmbeddingRuntimeMode = "native";
|
|
6224
|
-
var localEmbeddingProcessFailure = null;
|
|
6225
|
-
var wasmRuntimeInjected = false;
|
|
6226
|
-
var localEmbeddingHostForRuntime = currentLocalEmbeddingHost;
|
|
6227
|
-
var resolveWasmOrtForRuntime = () => {
|
|
6228
|
-
try {
|
|
6229
|
-
return typeof import.meta.resolve === "function" ? import.meta.resolve(ONNX_RUNTIME_WEB_SPECIFIER) : undefined;
|
|
6230
|
-
} catch {
|
|
6231
|
-
return;
|
|
6232
|
-
}
|
|
6233
|
-
};
|
|
6234
|
-
var importWasmOrtModule = async (specifier) => await import(specifier);
|
|
6235
|
-
var importWasmOrtModuleForRuntime = importWasmOrtModule;
|
|
6236
|
-
var importWasmOrtForRuntime = async () => {
|
|
6237
|
-
const { createRequire: createRequireFn } = await import("node:module");
|
|
6238
|
-
const requireFn = createRequireFn(import.meta.url);
|
|
6239
|
-
const ortEntry = requireFn.resolve(ONNX_RUNTIME_WEB_SPECIFIER);
|
|
6240
|
-
const ortSpecifier = resolveWasmOrtForRuntime() ?? ONNX_RUNTIME_WEB_SPECIFIER;
|
|
6241
|
-
return {
|
|
6242
|
-
module: await importWasmOrtModuleForRuntime(ortSpecifier),
|
|
6243
|
-
entryPath: ortEntry
|
|
6244
|
-
};
|
|
6245
|
-
};
|
|
6246
|
-
var importTransformersForRuntime = async () => {
|
|
6247
|
-
return await importPluginModule(() => import("./transformers.node-3zqr6k1w.js"));
|
|
6248
|
-
};
|
|
6249
|
-
var importTransformersWasmFallbackForRuntime = async () => {
|
|
6250
|
-
const webEntry = new URL(new URL(import.meta.url).pathname.endsWith(".ts") ? "../../../../dist/transformers-web.js" : "./transformers-web.js", import.meta.url).href;
|
|
6251
|
-
return await importPluginModule(() => import(webEntry));
|
|
6252
|
-
};
|
|
6253
|
-
var importTransformersNodeWasmFallbackForRuntime = async () => {
|
|
6254
|
-
const nodeWasmEntry = new URL(new URL(import.meta.url).pathname.endsWith(".ts") ? "../../../../dist/transformers-node-wasm.js" : "./transformers-node-wasm.js", import.meta.url).href;
|
|
6255
|
-
return await importPluginModule(() => import(nodeWasmEntry));
|
|
6256
|
-
};
|
|
6257
|
-
var modelCacheDirForRuntime = () => workerData?.magicContextEmbeddingWorker && typeof workerData.modelCacheDir === "string" ? workerData.modelCacheDir : join(getMagicContextStorageDir(), "models");
|
|
6258
|
-
var logForRuntime = log2;
|
|
6259
|
-
var injectWasmOrtForRuntime = injectWasmOrt;
|
|
6260
|
-
var loadedLocalEmbeddingRuntimes = new Map;
|
|
6261
|
-
var nextLocalEmbeddingRuntimeId = 1;
|
|
6262
|
-
function directoryBytes(path) {
|
|
6263
|
-
let total = 0;
|
|
6264
|
-
try {
|
|
6265
|
-
for (const entry of readdirSync(path, { withFileTypes: true })) {
|
|
6266
|
-
const entryPath = join(path, entry.name);
|
|
6267
|
-
if (entry.isDirectory()) {
|
|
6268
|
-
const childBytes = directoryBytes(entryPath);
|
|
6269
|
-
if (childBytes === null)
|
|
6270
|
-
return null;
|
|
6271
|
-
total += childBytes;
|
|
6272
|
-
} else if (entry.isFile()) {
|
|
6273
|
-
total += statSync(entryPath).size;
|
|
6274
|
-
}
|
|
6275
|
-
}
|
|
6276
|
-
return total;
|
|
6277
|
-
} catch (error) {
|
|
6278
|
-
return error.code === "ENOENT" ? 0 : null;
|
|
6279
|
-
}
|
|
6280
|
-
}
|
|
6281
|
-
function getLocalEmbeddingNativeMemoryStats() {
|
|
6282
|
-
const loaded = [...loadedLocalEmbeddingRuntimes.values()];
|
|
6283
|
-
return {
|
|
6284
|
-
loaded: loaded.length > 0,
|
|
6285
|
-
providerCount: loaded.length,
|
|
6286
|
-
models: [...new Set(loaded.map((entry) => entry.model))].sort(),
|
|
6287
|
-
runtimes: [...new Set(loaded.map((entry) => entry.runtime))].sort(),
|
|
6288
|
-
modelCacheBytes: loaded.length > 0 ? directoryBytes(modelCacheDirForRuntime()) : 0,
|
|
6289
|
-
rssDeltaAtLoad: loaded.reduce((sum, entry) => sum + entry.rssDeltaAtLoad, 0),
|
|
6290
|
-
externalDeltaAtLoad: loaded.reduce((sum, entry) => sum + entry.externalDeltaAtLoad, 0),
|
|
6291
|
-
arrayBuffersDeltaAtLoad: loaded.reduce((sum, entry) => sum + entry.arrayBuffersDeltaAtLoad, 0)
|
|
6292
|
-
};
|
|
6293
|
-
}
|
|
6294
|
-
async function ensureWasmOrtInjected() {
|
|
6295
|
-
if (wasmRuntimeInjected)
|
|
6296
|
-
return true;
|
|
6297
|
-
if (!await injectWasmOrtForRuntime())
|
|
6298
|
-
return false;
|
|
6299
|
-
wasmRuntimeInjected = true;
|
|
6300
|
-
return true;
|
|
6301
|
-
}
|
|
6302
|
-
async function injectWasmOrt() {
|
|
6303
|
-
if (wasmRuntimeInjected)
|
|
6304
|
-
return true;
|
|
6305
|
-
try {
|
|
6306
|
-
const { module: ortWeb, entryPath } = await importWasmOrtForRuntime();
|
|
6307
|
-
if (ortWeb.env?.wasm) {
|
|
6308
|
-
ortWeb.env.wasm.numThreads = 1;
|
|
6309
|
-
ortWeb.env.wasm.wasmPaths = `${pathToFileURL(dirname(entryPath)).href}/`;
|
|
6310
|
-
}
|
|
6311
|
-
globalThis[Symbol.for("onnxruntime")] = ortWeb;
|
|
6312
|
-
wasmRuntimeInjected = true;
|
|
6313
|
-
return true;
|
|
6314
|
-
} catch (error) {
|
|
6315
|
-
log2("[magic-context] failed to inject onnxruntime-web:", error instanceof Error ? error.message : String(error));
|
|
6316
|
-
return false;
|
|
6317
|
-
}
|
|
6318
|
-
}
|
|
6319
|
-
function likelyMuslHint() {
|
|
6320
|
-
if (process.platform !== "linux" || typeof process.report?.getReport !== "function")
|
|
6321
|
-
return "";
|
|
6322
|
-
try {
|
|
6323
|
-
const report = process.report.getReport();
|
|
6324
|
-
return typeof report.header?.glibcVersionRuntime === "string" ? "" : " Linux process report has no glibc runtime version (musl likely).";
|
|
6325
|
-
} catch {
|
|
6326
|
-
return "";
|
|
6327
|
-
}
|
|
6328
|
-
}
|
|
6329
|
-
function localEmbeddingRuntimeIsDisabled() {
|
|
6330
|
-
return localEmbeddingRuntimeMode === "disabled";
|
|
6331
|
-
}
|
|
6332
|
-
|
|
6333
|
-
class LocalEmbeddingFallbackError extends Error {
|
|
6334
|
-
nativeError;
|
|
6335
|
-
constructor(nativeError, wasmError) {
|
|
6336
|
-
super("the native local embedding runtime and its WASM fallback both failed", {
|
|
6337
|
-
cause: wasmError
|
|
6338
|
-
});
|
|
6339
|
-
this.name = "LocalEmbeddingFallbackError";
|
|
6340
|
-
this.nativeError = nativeError;
|
|
6341
|
-
}
|
|
6342
|
-
}
|
|
6343
|
-
|
|
6344
|
-
class LocalEmbeddingFsUnavailableError extends Error {
|
|
6345
|
-
code = "MC_EMBEDDING_FS_UNAVAILABLE";
|
|
6346
|
-
constructor() {
|
|
6347
|
-
super("MC_EMBEDDING_FS_UNAVAILABLE: Node WASM bundle has no filesystem cache");
|
|
6348
|
-
this.name = "LocalEmbeddingFsUnavailableError";
|
|
6349
|
-
}
|
|
6350
|
-
}
|
|
6351
|
-
async function importWasmTransformersForHost() {
|
|
6352
|
-
const host = localEmbeddingHostForRuntime();
|
|
6353
|
-
if (host.isElectron) {
|
|
6354
|
-
return importTransformersForRuntime();
|
|
6355
|
-
}
|
|
6356
|
-
return host.hasNodeFilesystem ? importTransformersNodeWasmFallbackForRuntime() : importTransformersWasmFallbackForRuntime();
|
|
6357
|
-
}
|
|
6358
|
-
function disableLocalEmbeddingsAfterRuntimeFailure(detail) {
|
|
6359
|
-
localEmbeddingRuntimeMode = "disabled";
|
|
6360
|
-
logForRuntime(`[magic-context] local embeddings are disabled because both the onnxruntime-node native binding and the onnxruntime-web (WASM) fallback failed to load. Native failure: ${detail}. Run \`npx @cortexkit/magic-context@latest doctor\` for diagnostics; reinstalling repairs missing package files but cannot add a native binding that upstream does not ship. Alternatively, configure an \`openai-compatible\` embedding HTTP endpoint. Existing memories are unaffected.`);
|
|
6361
|
-
}
|
|
6362
|
-
async function loadTransformersForLocalEmbedding(runtimePreference) {
|
|
6363
|
-
if (localEmbeddingRuntimeMode === "disabled") {
|
|
6364
|
-
throw new Error("local embedding runtime is disabled");
|
|
6365
|
-
}
|
|
6366
|
-
const resolvedRuntime = resolveLocalEmbeddingRuntime(runtimePreference, localEmbeddingHostForRuntime());
|
|
6367
|
-
if (resolvedRuntime === "wasm") {
|
|
6368
|
-
if (!await ensureWasmOrtInjected()) {
|
|
6369
|
-
disableLocalEmbeddingsAfterRuntimeFailure("the selected WASM runtime is unavailable");
|
|
6370
|
-
throw new Error("onnxruntime-web failed to load");
|
|
6371
|
-
}
|
|
6372
|
-
return { module: await importWasmTransformersForHost(), usesWasm: true };
|
|
6373
|
-
}
|
|
6374
|
-
if (localEmbeddingRuntimeMode === "wasm") {
|
|
6375
|
-
if (!await ensureWasmOrtInjected()) {
|
|
6376
|
-
disableLocalEmbeddingsAfterRuntimeFailure("the previously selected WASM runtime is unavailable");
|
|
6377
|
-
throw new Error("onnxruntime-web failed to load");
|
|
6378
|
-
}
|
|
6379
|
-
try {
|
|
6380
|
-
return { module: await importWasmTransformersForHost(), usesWasm: true };
|
|
6381
|
-
} catch (wasmError) {
|
|
6382
|
-
disableLocalEmbeddingsAfterRuntimeFailure("the previously selected WASM runtime failed to load");
|
|
6383
|
-
throw wasmError;
|
|
6384
|
-
}
|
|
6385
|
-
}
|
|
6386
|
-
const electron = resolvedRuntime === "electron";
|
|
6387
|
-
if (electron) {
|
|
6388
|
-
const wasInjected = wasmRuntimeInjected;
|
|
6389
|
-
if (await ensureWasmOrtInjected()) {
|
|
6390
|
-
if (!wasInjected) {
|
|
6391
|
-
logForRuntime("[magic-context] Electron detected — using onnxruntime-web (WASM) for embeddings (bypasses onnxruntime-node native load)");
|
|
6392
|
-
}
|
|
6393
|
-
return { module: await importTransformersForRuntime(), usesWasm: true };
|
|
6394
|
-
}
|
|
6395
|
-
}
|
|
6396
|
-
try {
|
|
6397
|
-
return { module: await importTransformersForRuntime(), usesWasm: false };
|
|
6398
|
-
} catch (nativeError) {
|
|
6399
|
-
if (!isNativeRuntimeMissingError(nativeError) || electron) {
|
|
6400
|
-
throw nativeError;
|
|
6401
|
-
}
|
|
6402
|
-
if (!await ensureWasmOrtInjected()) {
|
|
6403
|
-
disableLocalEmbeddingsAfterRuntimeFailure(nativeError instanceof Error ? nativeError.message : String(nativeError));
|
|
6404
|
-
throw nativeError;
|
|
6405
|
-
}
|
|
6406
|
-
localEmbeddingRuntimeMode = "wasm";
|
|
6407
|
-
try {
|
|
6408
|
-
const module = await importWasmTransformersForHost();
|
|
6409
|
-
logForRuntime("[magic-context] onnxruntime-node failed to load; using onnxruntime-web (WASM) for local embeddings. WASM inference is slower than native; a remote `openai-compatible` provider may be faster." + likelyMuslHint());
|
|
6410
|
-
return { module, usesWasm: true };
|
|
6411
|
-
} catch (wasmError) {
|
|
6412
|
-
disableLocalEmbeddingsAfterRuntimeFailure(nativeError instanceof Error ? nativeError.message : String(nativeError));
|
|
6413
|
-
throw new LocalEmbeddingFallbackError(nativeError, wasmError);
|
|
6414
|
-
}
|
|
6415
|
-
}
|
|
6416
|
-
}
|
|
6417
|
-
var DEFAULT_LOCAL_DTYPE = "fp32";
|
|
6418
|
-
async function withQuietConsole(fn) {
|
|
6419
|
-
const origWarn = console.warn;
|
|
6420
|
-
const origError = console.error;
|
|
6421
|
-
const redirect = (...args) => {
|
|
6422
|
-
const message = args.map((a) => typeof a === "string" ? a : String(a)).join(" ");
|
|
6423
|
-
log2(`[transformers] ${message}`);
|
|
6424
|
-
};
|
|
6425
|
-
console.warn = redirect;
|
|
6426
|
-
console.error = redirect;
|
|
6427
|
-
try {
|
|
6428
|
-
return await fn();
|
|
6429
|
-
} finally {
|
|
6430
|
-
console.warn = origWarn;
|
|
6431
|
-
console.error = origError;
|
|
6432
|
-
}
|
|
6433
|
-
}
|
|
6434
|
-
function isNativeRuntimeMissingError(error) {
|
|
6435
|
-
const message = error instanceof Error ? error.message : String(error ?? "");
|
|
6436
|
-
const lower = message.toLowerCase();
|
|
6437
|
-
const code = error?.code;
|
|
6438
|
-
const name = error?.name;
|
|
6439
|
-
if (code === "ERR_DLOPEN_FAILED" && lower.includes("onnxruntime")) {
|
|
6440
|
-
return true;
|
|
6441
|
-
}
|
|
6442
|
-
if (lower.includes('could not load the "sharp" module'))
|
|
6443
|
-
return true;
|
|
6444
|
-
const mentionsNativeRuntime = lower.includes("onnxruntime-node") || lower.includes("onnxruntime_binding");
|
|
6445
|
-
if (!mentionsNativeRuntime)
|
|
6446
|
-
return false;
|
|
6447
|
-
return code === "ERR_MODULE_NOT_FOUND" || name === "ResolveMessage" || lower.includes("cannot find package") || lower.includes("cannot find module") || lower.includes("err_module_not_found");
|
|
6448
|
-
}
|
|
6449
|
-
function isTransientLoadError(error) {
|
|
6450
|
-
const message = error instanceof Error ? error.message : String(error ?? "");
|
|
6451
|
-
if (!message)
|
|
6452
|
-
return false;
|
|
6453
|
-
const lower = message.toLowerCase();
|
|
6454
|
-
return lower.includes("protobuf parsing failed") || lower.includes("unable to get model file path or buffer") || lower.includes("ebusy") || lower.includes("resource busy") || lower.includes("resource temporarily unavailable");
|
|
6455
|
-
}
|
|
6456
|
-
function isArrayLikeNumber(value) {
|
|
6457
|
-
if (typeof value !== "object" || value === null || !("length" in value)) {
|
|
6458
|
-
return false;
|
|
6459
|
-
}
|
|
6460
|
-
const arr = value;
|
|
6461
|
-
if (typeof arr.length !== "number") {
|
|
6462
|
-
return false;
|
|
6463
|
-
}
|
|
6464
|
-
return arr.length === 0 || typeof arr[0] === "number";
|
|
6465
|
-
}
|
|
6466
|
-
function toFloat32Array(values) {
|
|
6467
|
-
return values instanceof Float32Array ? new Float32Array(values) : Float32Array.from(Array.from(values));
|
|
6468
|
-
}
|
|
6469
|
-
function extractBatchEmbeddings(result, expectedCount) {
|
|
6470
|
-
const { data } = result;
|
|
6471
|
-
if (Array.isArray(data) && data.length === expectedCount && data.every((entry) => typeof entry !== "number" && isArrayLikeNumber(entry))) {
|
|
6472
|
-
return data.map((entry) => toFloat32Array(entry));
|
|
6473
|
-
}
|
|
6474
|
-
if (!isArrayLikeNumber(data)) {
|
|
6475
|
-
log2("[magic-context] embedding batch returned unexpected data shape");
|
|
6476
|
-
return Array.from({ length: expectedCount }, () => null);
|
|
6477
|
-
}
|
|
6478
|
-
const flatData = toFloat32Array(data);
|
|
6479
|
-
const dimension = result.dims?.at(-1) ?? flatData.length / expectedCount;
|
|
6480
|
-
if (!Number.isInteger(dimension) || dimension <= 0 || flatData.length !== expectedCount * dimension) {
|
|
6481
|
-
log2("[magic-context] embedding batch returned invalid dimensions");
|
|
6482
|
-
return Array.from({ length: expectedCount }, () => null);
|
|
6483
|
-
}
|
|
6484
|
-
const embeddings = [];
|
|
6485
|
-
for (let index = 0;index < expectedCount; index++) {
|
|
6486
|
-
embeddings.push(flatData.slice(index * dimension, (index + 1) * dimension));
|
|
6487
|
-
}
|
|
6488
|
-
return embeddings;
|
|
6489
|
-
}
|
|
6490
|
-
|
|
6491
|
-
class LocalEmbeddingProvider {
|
|
6492
|
-
modelId;
|
|
6493
|
-
maxInputTokens;
|
|
6494
|
-
memoryStatsId = nextLocalEmbeddingRuntimeId++;
|
|
6495
|
-
model;
|
|
6496
|
-
dtype;
|
|
6497
|
-
runtimePreference;
|
|
6498
|
-
pipeline = null;
|
|
6499
|
-
workerClient;
|
|
6500
|
-
workerLoaded = false;
|
|
6501
|
-
initPromise = null;
|
|
6502
|
-
lastFailureReason = null;
|
|
6503
|
-
usesWasm = false;
|
|
6504
|
-
inFlight = 0;
|
|
6505
|
-
disposing = false;
|
|
6506
|
-
disposePromise = null;
|
|
6507
|
-
inFlightWaiters = [];
|
|
6508
|
-
constructor(model = DEFAULT_LOCAL_EMBEDDING_MODEL, maxInputTokens = 512, dtype = DEFAULT_LOCAL_DTYPE, runtimePreference = "auto") {
|
|
6509
|
-
this.model = model;
|
|
6510
|
-
this.maxInputTokens = maxInputTokens;
|
|
6511
|
-
this.dtype = dtype || DEFAULT_LOCAL_DTYPE;
|
|
6512
|
-
this.runtimePreference = runtimePreference;
|
|
6513
|
-
this.workerClient = !workerData?.magicContextEmbeddingWorker && !useInThreadTestRuntime ? (workerFactoryForTests ?? ((data) => new EmbeddingWorkerClient(data)))({
|
|
6514
|
-
magicContextEmbeddingWorker: true,
|
|
6515
|
-
modelCacheDir: modelCacheDirForRuntime(),
|
|
6516
|
-
enforcePrivateStoragePermissions: shouldEnforcePrivateStoragePermissions(),
|
|
6517
|
-
harness: getHarness2(),
|
|
6518
|
-
model,
|
|
6519
|
-
maxInputTokens,
|
|
6520
|
-
dtype: this.dtype,
|
|
6521
|
-
runtimePreference
|
|
6522
|
-
}) : null;
|
|
6523
|
-
this.modelId = getEmbeddingProviderIdentity({
|
|
6524
|
-
provider: "local",
|
|
6525
|
-
model,
|
|
6526
|
-
local_runtime: runtimePreference,
|
|
6527
|
-
...dtype && dtype !== DEFAULT_LOCAL_DTYPE ? { local_dtype: dtype } : {}
|
|
6528
|
-
});
|
|
6529
|
-
}
|
|
6530
|
-
async initialize() {
|
|
6531
|
-
if (this.workerClient && !this.disposing) {
|
|
6532
|
-
if (this.workerLoaded && this.workerClient.isRunning())
|
|
6533
|
-
return true;
|
|
6534
|
-
try {
|
|
6535
|
-
const reply = await this.workerClient.request();
|
|
6536
|
-
this.recordWorkerReply(reply);
|
|
6537
|
-
return reply.loaded;
|
|
6538
|
-
} catch (error) {
|
|
6539
|
-
this.recordWorkerFailure(error);
|
|
6540
|
-
return false;
|
|
6541
|
-
}
|
|
6542
|
-
}
|
|
6543
|
-
if (this.disposing) {
|
|
6544
|
-
return false;
|
|
6545
|
-
}
|
|
6546
|
-
if (this.pipeline) {
|
|
6547
|
-
return true;
|
|
6548
|
-
}
|
|
6549
|
-
if (localEmbeddingRuntimeMode === "disabled") {
|
|
6550
|
-
this.lastFailureReason = localEmbeddingProcessFailure ?? classifyLocalEmbeddingFailure(new Error("local embedding runtime is disabled"));
|
|
6551
|
-
return false;
|
|
6552
|
-
}
|
|
6553
|
-
if (this.initPromise) {
|
|
6554
|
-
await this.initPromise;
|
|
6555
|
-
return this.pipeline !== null;
|
|
6556
|
-
}
|
|
6557
|
-
const memoryBeforeLoad = process.memoryUsage();
|
|
6558
|
-
this.initPromise = (async () => {
|
|
6559
|
-
try {
|
|
6560
|
-
if (this.disposing) {
|
|
6561
|
-
return;
|
|
6562
|
-
}
|
|
6563
|
-
const { module: transformersModule, usesWasm } = await loadTransformersForLocalEmbedding(this.runtimePreference);
|
|
6564
|
-
this.usesWasm = usesWasm;
|
|
6565
|
-
const env = transformersModule.env;
|
|
6566
|
-
configureTransformersRemoteHost(env);
|
|
6567
|
-
const LogLevel = transformersModule.LogLevel;
|
|
6568
|
-
if (LogLevel && "ERROR" in LogLevel) {
|
|
6569
|
-
env.logLevel = LogLevel.ERROR;
|
|
6570
|
-
}
|
|
6571
|
-
const host = localEmbeddingHostForRuntime();
|
|
6572
|
-
if (usesWasm && host.hasNodeFilesystem && !host.isElectron && (env.useFS !== true || env.useFSCache !== true)) {
|
|
6573
|
-
throw new LocalEmbeddingFsUnavailableError;
|
|
6574
|
-
}
|
|
6575
|
-
const modelCacheDir = modelCacheDirForRuntime();
|
|
6576
|
-
try {
|
|
6577
|
-
ensureStorageDirectorySync(modelCacheDir);
|
|
6578
|
-
env.cacheDir = modelCacheDir;
|
|
6579
|
-
} catch {
|
|
6580
|
-
log2("[magic-context] could not create model cache dir, using library default");
|
|
6581
|
-
}
|
|
6582
|
-
const createPipeline = transformersModule.pipeline;
|
|
6583
|
-
const lockPath = join(modelCacheDir, ".load.lock");
|
|
6584
|
-
const releaseLock = await acquireModelLoadLock(lockPath);
|
|
6585
|
-
const stopHeartbeat = startLockHeartbeat(lockPath);
|
|
6586
|
-
try {
|
|
6587
|
-
const MAX_ATTEMPTS = 3;
|
|
6588
|
-
let lastError;
|
|
6589
|
-
for (let attempt = 1;attempt <= MAX_ATTEMPTS; attempt++) {
|
|
6590
|
-
try {
|
|
6591
|
-
const pipeline = await withQuietConsole(() => createPipeline("feature-extraction", this.model, {
|
|
6592
|
-
dtype: this.dtype,
|
|
6593
|
-
...usesWasm ? { device: "auto" } : {}
|
|
6594
|
-
}));
|
|
6595
|
-
if (this.disposing) {
|
|
6596
|
-
await pipeline.dispose?.();
|
|
6597
|
-
this.pipeline = null;
|
|
6598
|
-
} else {
|
|
6599
|
-
this.pipeline = pipeline;
|
|
6600
|
-
}
|
|
6601
|
-
lastError = undefined;
|
|
6602
|
-
break;
|
|
6603
|
-
} catch (error) {
|
|
6604
|
-
lastError = error;
|
|
6605
|
-
if (!isTransientLoadError(error) || attempt === MAX_ATTEMPTS) {
|
|
6606
|
-
break;
|
|
6607
|
-
}
|
|
6608
|
-
const delayMs = 300 * attempt + Math.floor(Math.random() * 200);
|
|
6609
|
-
log2(`[magic-context] embedding model load attempt ${attempt}/${MAX_ATTEMPTS} failed transiently, retrying in ${delayMs}ms`);
|
|
6610
|
-
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
6611
|
-
}
|
|
6612
|
-
}
|
|
6613
|
-
if (this.pipeline) {
|
|
6614
|
-
this.lastFailureReason = null;
|
|
6615
|
-
localEmbeddingProcessFailure = null;
|
|
6616
|
-
const memoryAfterLoad = process.memoryUsage();
|
|
6617
|
-
loadedLocalEmbeddingRuntimes.set(this.memoryStatsId, {
|
|
6618
|
-
model: this.model,
|
|
6619
|
-
runtime: this.usesWasm ? "wasm" : "native",
|
|
6620
|
-
rssDeltaAtLoad: memoryAfterLoad.rss - memoryBeforeLoad.rss,
|
|
6621
|
-
externalDeltaAtLoad: memoryAfterLoad.external - memoryBeforeLoad.external,
|
|
6622
|
-
arrayBuffersDeltaAtLoad: memoryAfterLoad.arrayBuffers - memoryBeforeLoad.arrayBuffers
|
|
6623
|
-
});
|
|
6624
|
-
log2(`[magic-context] embedding model loaded: ${this.model}`);
|
|
6625
|
-
} else if (this.disposing) {
|
|
6626
|
-
return;
|
|
6627
|
-
} else {
|
|
6628
|
-
throw lastError ?? new Error("unknown embedding load failure");
|
|
6629
|
-
}
|
|
6630
|
-
} finally {
|
|
6631
|
-
stopHeartbeat();
|
|
6632
|
-
await releaseLock();
|
|
6633
|
-
}
|
|
6634
|
-
} catch (error) {
|
|
6635
|
-
const nativeError = error instanceof LocalEmbeddingFallbackError ? error.nativeError : undefined;
|
|
6636
|
-
const failure = classifyLocalEmbeddingFailure(error, {
|
|
6637
|
-
platform: process.platform,
|
|
6638
|
-
arch: process.arch,
|
|
6639
|
-
usesWasm: this.usesWasm,
|
|
6640
|
-
nativeError
|
|
6641
|
-
});
|
|
6642
|
-
this.lastFailureReason = failure;
|
|
6643
|
-
localEmbeddingProcessFailure = failure;
|
|
6644
|
-
if (!localEmbeddingRuntimeIsDisabled() && isNativeRuntimeMissingError(error)) {
|
|
6645
|
-
disableLocalEmbeddingsAfterRuntimeFailure(error instanceof Error ? error.message : String(error));
|
|
6646
|
-
}
|
|
6647
|
-
logForRuntime(`[magic-context] embedding model failed to load (${failure.class}: ${failure.reason}):`, error);
|
|
6648
|
-
this.pipeline = null;
|
|
6649
|
-
} finally {
|
|
6650
|
-
this.initPromise = null;
|
|
6651
|
-
}
|
|
6652
|
-
})();
|
|
6653
|
-
await this.initPromise;
|
|
6654
|
-
return this.pipeline !== null;
|
|
6655
|
-
}
|
|
6656
|
-
waitForInFlightToDrain() {
|
|
6657
|
-
if (this.inFlight === 0) {
|
|
6658
|
-
return Promise.resolve();
|
|
6659
|
-
}
|
|
6660
|
-
return new Promise((resolve) => {
|
|
6661
|
-
this.inFlightWaiters.push(resolve);
|
|
6662
|
-
});
|
|
6663
|
-
}
|
|
6664
|
-
finishInFlight() {
|
|
6665
|
-
this.inFlight = Math.max(0, this.inFlight - 1);
|
|
6666
|
-
if (this.inFlight !== 0)
|
|
6667
|
-
return;
|
|
6668
|
-
const waiters = this.inFlightWaiters.splice(0);
|
|
6669
|
-
for (const waiter of waiters) {
|
|
6670
|
-
waiter();
|
|
6671
|
-
}
|
|
6672
|
-
}
|
|
6673
|
-
async embed(text, signal, _purpose) {
|
|
6674
|
-
if (this.workerClient)
|
|
6675
|
-
return (await this.embedBatch([text], signal, _purpose))[0] ?? null;
|
|
6676
|
-
if (signal?.aborted)
|
|
6677
|
-
return null;
|
|
6678
|
-
if (this.disposing)
|
|
6679
|
-
return null;
|
|
6680
|
-
this.inFlight += 1;
|
|
6681
|
-
try {
|
|
6682
|
-
if (!await this.initialize()) {
|
|
6683
|
-
return null;
|
|
6684
|
-
}
|
|
6685
|
-
const pipeline = this.pipeline;
|
|
6686
|
-
if (!pipeline) {
|
|
6687
|
-
return null;
|
|
6688
|
-
}
|
|
6689
|
-
const result = await withQuietConsole(() => pipeline(text, {
|
|
6690
|
-
pooling: "mean",
|
|
6691
|
-
normalize: true
|
|
6692
|
-
}));
|
|
6693
|
-
const embedding = extractBatchEmbeddings(result, 1)[0] ?? null;
|
|
6694
|
-
if (!embedding) {
|
|
6695
|
-
this.lastFailureReason = classifyLocalEmbeddingFailure(new Error("local embedding pipeline returned no vector"), { usesWasm: this.usesWasm });
|
|
6696
|
-
} else {
|
|
6697
|
-
this.lastFailureReason = null;
|
|
6698
|
-
}
|
|
6699
|
-
return embedding;
|
|
6700
|
-
} catch (error) {
|
|
6701
|
-
const failure = classifyLocalEmbeddingFailure(error, { usesWasm: this.usesWasm });
|
|
6702
|
-
this.lastFailureReason = failure;
|
|
6703
|
-
logForRuntime(`[magic-context] embedding failed (${failure.class}: ${failure.reason}):`, error);
|
|
6704
|
-
return null;
|
|
6705
|
-
} finally {
|
|
6706
|
-
this.finishInFlight();
|
|
6707
|
-
}
|
|
6708
|
-
}
|
|
6709
|
-
async embedBatch(texts, signal, _purpose) {
|
|
6710
|
-
if (this.workerClient) {
|
|
6711
|
-
if (!texts.length)
|
|
6712
|
-
return [];
|
|
6713
|
-
if (signal?.aborted || this.disposing)
|
|
6714
|
-
return texts.map(() => null);
|
|
6715
|
-
try {
|
|
6716
|
-
const reply = await this.workerClient.request(texts, signal);
|
|
6717
|
-
this.recordWorkerReply(reply);
|
|
6718
|
-
return reply.vectors ?? texts.map(() => null);
|
|
6719
|
-
} catch (error) {
|
|
6720
|
-
this.recordWorkerFailure(error);
|
|
6721
|
-
return texts.map(() => null);
|
|
6722
|
-
}
|
|
6723
|
-
}
|
|
6724
|
-
if (texts.length === 0) {
|
|
6725
|
-
return [];
|
|
6726
|
-
}
|
|
6727
|
-
if (signal?.aborted) {
|
|
6728
|
-
return Array.from({ length: texts.length }, () => null);
|
|
6729
|
-
}
|
|
6730
|
-
if (this.disposing) {
|
|
6731
|
-
return Array.from({ length: texts.length }, () => null);
|
|
6732
|
-
}
|
|
6733
|
-
this.inFlight += 1;
|
|
6734
|
-
try {
|
|
6735
|
-
if (!await this.initialize()) {
|
|
6736
|
-
return Array.from({ length: texts.length }, () => null);
|
|
6737
|
-
}
|
|
6738
|
-
const pipeline = this.pipeline;
|
|
6739
|
-
if (!pipeline) {
|
|
6740
|
-
return Array.from({ length: texts.length }, () => null);
|
|
6741
|
-
}
|
|
6742
|
-
const result = await withQuietConsole(() => pipeline(texts, {
|
|
6743
|
-
pooling: "mean",
|
|
6744
|
-
normalize: true
|
|
6745
|
-
}));
|
|
6746
|
-
const embeddings = extractBatchEmbeddings(result, texts.length);
|
|
6747
|
-
if (embeddings.every((embedding) => embedding === null)) {
|
|
6748
|
-
this.lastFailureReason = classifyLocalEmbeddingFailure(new Error("local embedding pipeline returned no vectors"), { usesWasm: this.usesWasm });
|
|
6749
|
-
} else {
|
|
6750
|
-
this.lastFailureReason = null;
|
|
6751
|
-
}
|
|
6752
|
-
return embeddings;
|
|
6753
|
-
} catch (error) {
|
|
6754
|
-
const failure = classifyLocalEmbeddingFailure(error, { usesWasm: this.usesWasm });
|
|
6755
|
-
this.lastFailureReason = failure;
|
|
6756
|
-
logForRuntime(`[magic-context] embedding batch failed (${failure.class}: ${failure.reason}):`, error);
|
|
6757
|
-
return Array.from({ length: texts.length }, () => null);
|
|
6758
|
-
} finally {
|
|
6759
|
-
this.finishInFlight();
|
|
6760
|
-
}
|
|
6761
|
-
}
|
|
6762
|
-
recordWorkerReply(reply) {
|
|
6763
|
-
this.workerLoaded = reply.loaded;
|
|
6764
|
-
this.lastFailureReason = reply.failure ?? null;
|
|
6765
|
-
if (reply.loaded && reply.stats) {
|
|
6766
|
-
loadedLocalEmbeddingRuntimes.set(this.memoryStatsId, {
|
|
6767
|
-
model: this.model,
|
|
6768
|
-
runtime: reply.stats.runtimes[0] ?? "wasm",
|
|
6769
|
-
rssDeltaAtLoad: reply.stats.rssDeltaAtLoad,
|
|
6770
|
-
externalDeltaAtLoad: reply.stats.externalDeltaAtLoad,
|
|
6771
|
-
arrayBuffersDeltaAtLoad: reply.stats.arrayBuffersDeltaAtLoad
|
|
6772
|
-
});
|
|
6773
|
-
} else
|
|
6774
|
-
loadedLocalEmbeddingRuntimes.delete(this.memoryStatsId);
|
|
6775
|
-
}
|
|
6776
|
-
recordWorkerFailure(error) {
|
|
6777
|
-
loadedLocalEmbeddingRuntimes.delete(this.memoryStatsId);
|
|
6778
|
-
this.workerLoaded = false;
|
|
6779
|
-
this.lastFailureReason = classifyLocalEmbeddingFailure(error);
|
|
6780
|
-
logForRuntime("[magic-context] embedding worker failed:", error);
|
|
6781
|
-
}
|
|
6782
|
-
async dispose() {
|
|
6783
|
-
if (this.workerClient) {
|
|
6784
|
-
this.disposing = true;
|
|
6785
|
-
this.workerLoaded = false;
|
|
6786
|
-
loadedLocalEmbeddingRuntimes.delete(this.memoryStatsId);
|
|
6787
|
-
await this.workerClient.dispose();
|
|
6788
|
-
return;
|
|
6789
|
-
}
|
|
6790
|
-
if (this.disposePromise) {
|
|
6791
|
-
return this.disposePromise;
|
|
6792
|
-
}
|
|
6793
|
-
this.disposing = true;
|
|
6794
|
-
this.disposePromise = (async () => {
|
|
6795
|
-
if (this.initPromise) {
|
|
6796
|
-
await this.initPromise;
|
|
6797
|
-
}
|
|
6798
|
-
await this.waitForInFlightToDrain();
|
|
6799
|
-
const pipelineToDispose = this.pipeline;
|
|
6800
|
-
this.pipeline = null;
|
|
6801
|
-
this.initPromise = null;
|
|
6802
|
-
loadedLocalEmbeddingRuntimes.delete(this.memoryStatsId);
|
|
6803
|
-
if (!pipelineToDispose) {
|
|
6804
|
-
return;
|
|
6805
|
-
}
|
|
6806
|
-
try {
|
|
6807
|
-
await pipelineToDispose.dispose?.();
|
|
6808
|
-
} catch (error) {
|
|
6809
|
-
log2("[magic-context] embedding model dispose failed:", error);
|
|
6810
|
-
}
|
|
6811
|
-
})();
|
|
6812
|
-
return this.disposePromise;
|
|
6813
|
-
}
|
|
6814
|
-
isLoaded() {
|
|
6815
|
-
return this.workerClient ? this.workerLoaded && this.workerClient.isRunning() : this.pipeline !== null;
|
|
6816
|
-
}
|
|
6817
|
-
getLastFailureReason() {
|
|
6818
|
-
return this.lastFailureReason;
|
|
6819
|
-
}
|
|
6820
|
-
}
|
|
6821
|
-
|
|
6822
|
-
export { isValidLanguageCode, withContentLanguageDirective, buildPrimaryLanguageDirective, string, unknown, array, object, union, tuple, record, parseCron, nextOccurrence, nextDueAtMs, promptSurfaceConfigIdentity, resolveModelConfigValue, resolvePromptSurface, DEFAULT_EXECUTE_THRESHOLD_PERCENTAGE, DEFAULT_HISTORIAN_TIMEOUT_MS, PROTECTED_TOKENS_MIN, DEFAULT_LOCAL_EMBEDDING_MODEL, PER_HARNESS_MIGRATION_INVENTORY, PER_HARNESS_MODEL_KEYS, ConfigProfilesSchema, DREAM_TASK_PROMOTION_DEFAULTS, MagicContextConfigSchema, LIVE_RELOAD_CONFIG_PATHS, deriveDefaultProtectedTokens, embeddingModelsMatch, resolveEmbeddingTextPrefixes, getEmbeddingProviderIdentity, dominantEmbeddingFailure, resolveLocalEmbeddingWorkerRuntime, getLocalEmbeddingNativeMemoryStats, LocalEmbeddingProvider };
|
|
6002
|
+
export { isValidLanguageCode, withContentLanguageDirective, buildPrimaryLanguageDirective, string, unknown, array, object, union, tuple, record, parseCron, nextOccurrence, nextDueAtMs, renderToolTemplate, promptSurfaceConfigIdentity, resolveModelConfigValue, resolvePromptSurface, DEFAULT_EXECUTE_THRESHOLD_PERCENTAGE, DEFAULT_HISTORIAN_TIMEOUT_MS, PROTECTED_TOKENS_MIN, DEFAULT_LOCAL_EMBEDDING_MODEL, PER_HARNESS_MIGRATION_INVENTORY, PER_HARNESS_MODEL_KEYS, ConfigProfilesSchema, DREAM_TASK_PROMOTION_DEFAULTS, MagicContextConfigSchema, LIVE_RELOAD_CONFIG_PATHS, deriveDefaultProtectedTokens };
|