@cortexkit/pi-magic-context 0.49.0 → 0.49.1

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.
Files changed (31) hide show
  1. package/dist/auto-search-worker.js +3 -3
  2. package/dist/{embedding-backfill-b71dvfs8.js → embedding-backfill-scywarbn.js} +7 -7
  3. package/dist/embedding-worker-process.js +29 -0
  4. package/dist/embedding-worker.js +5 -5
  5. package/dist/index-0j0zqb4t.js +712 -0
  6. package/dist/{index-hwqb3s7f.js → index-0rasmwm8.js} +5 -5
  7. package/dist/{index-bejpsj10.js → index-27jh224p.js} +1 -1
  8. package/dist/{index-ehybv5nr.js → index-28vnkyy7.js} +1129 -2990
  9. package/dist/{index-r024r8da.js → index-4gfq5t09.js} +16 -1
  10. package/dist/{index-vf38qhee.js → index-81nthk8j.js} +44 -1
  11. package/dist/{index-yzcjxnt2.js → index-b2anqqad.js} +422 -6
  12. package/dist/{index-1evzceyw.js → index-cyrnv16r.js} +314 -1072
  13. package/dist/index-fqb1bddn.js +4947 -0
  14. package/dist/{index-041ex0cj.js → index-g7hndmqm.js} +20 -34
  15. package/dist/index-gc2749rx.js +1168 -0
  16. package/dist/{index-t7xfqtg6.js → index-p5zesr4e.js} +22 -718
  17. package/dist/{index-yarjn32j.js → index-r874cgmv.js} +38 -36
  18. package/dist/{index-3mgf68pn.js → index-rd53pk0h.js} +1 -1
  19. package/dist/{index-904reqtg.js → index-rdtgp48t.js} +6 -4
  20. package/dist/index.js +1159 -1126
  21. package/dist/{message-index-bounded-qz5dz2se.js → message-index-bounded-110sb4fm.js} +6 -5
  22. package/dist/migration-worker.js +6 -5
  23. package/dist/{project-embedding-registry-ky7wrkr1.js → project-embedding-registry-64t0mw8y.js} +6 -6
  24. package/dist/raw-ordinal-warmup-worker.js +1 -1
  25. package/dist/{read-session-chunk-24sd89tf.js → read-session-chunk-rvmc25n5.js} +3 -2
  26. package/dist/{search-jbh28nwy.js → search-7z3w8pmz.js} +15 -14
  27. package/dist/subagent-entry.js +17 -16
  28. package/package.json +3 -3
  29. package/dist/index-3rea6zd9.js +0 -2396
  30. package/dist/index-bgmv2kaf.js +0 -290
  31. /package/dist/{index-5prbx026.js → index-2pp04pmg.js} +0 -0
@@ -1,33 +1,69 @@
1
- import {
2
- modelRefLookupOrder,
3
- toolTemplateError
4
- } from "./index-bgmv2kaf.js";
5
- import {
6
- getSynapseLaneIdentity
7
- } from "./index-21v1kfs7.js";
8
- import {
9
- getHarness2
10
- } from "./index-0h2mkab2.js";
11
- import {
12
- getMagicContextStorageDir,
13
- importPluginModule,
14
- log2
15
- } from "./index-x3dcnxh0.js";
16
- import {
17
- computeNormalizedHash
18
- } from "./index-9x1f96r8.js";
19
- import {
20
- shouldEnforcePrivateStoragePermissions,
21
- ensureStorageDirectorySync,
22
- writeStorageFileAsync,
23
- writeStorageFileHandleAsync,
24
- createStorageFileHandleAsync
25
- } from "./index-vf38qhee.js";
26
1
  import {
27
2
  DEFAULT_PROTECTED_TOOLS,
28
3
  mergeProtectedTools
29
4
  } from "./index-674f6qs2.js";
30
5
 
6
+ // ../plugin/src/shared/harness-provider-map.ts
7
+ var CANONICAL_TO_PI_PROVIDER = {
8
+ openai: "openai-codex",
9
+ google: "google-antigravity"
10
+ };
11
+ var PI_TO_CANONICAL_PROVIDER = {
12
+ "openai-codex": "openai",
13
+ "google-antigravity": "google"
14
+ };
15
+ var CANONICAL_TO_OMP_PROVIDER = {
16
+ openai: "openai-codex",
17
+ google: "google-antigravity",
18
+ opencode: "opencode-zen"
19
+ };
20
+ var OMP_TO_CANONICAL_PROVIDER = {
21
+ "openai-codex": "openai",
22
+ "google-antigravity": "google",
23
+ "opencode-zen": "opencode"
24
+ };
25
+ var CANONICAL_MODEL_IDENTITY_ALIASES = {
26
+ "github-copilot/gpt-6-astra": "openai/gpt-6-astra"
27
+ };
28
+ function remapProviderPrefix(ref, map) {
29
+ if (typeof ref !== "string")
30
+ return ref;
31
+ const slash = ref.indexOf("/");
32
+ if (slash <= 0)
33
+ return ref;
34
+ const provider = ref.slice(0, slash);
35
+ if (!Object.hasOwn(map, provider))
36
+ return ref;
37
+ return `${map[provider]}${ref.slice(slash)}`;
38
+ }
39
+ function canonicalModelIdentity(ref) {
40
+ const harnessCanonical = piModelRefToCanonical(ompModelRefToCanonical(ref));
41
+ return CANONICAL_MODEL_IDENTITY_ALIASES[harnessCanonical.toLowerCase()] ?? harnessCanonical;
42
+ }
43
+ function piModelRefToCanonical(ref) {
44
+ return remapProviderPrefix(ref, PI_TO_CANONICAL_PROVIDER);
45
+ }
46
+ function resolveModelRefForPi(ref) {
47
+ return remapProviderPrefix(piModelRefToCanonical(ref), CANONICAL_TO_PI_PROVIDER);
48
+ }
49
+ function modelRefLookupOrder(ref) {
50
+ const canonical = piModelRefToCanonical(ompModelRefToCanonical(ref));
51
+ return [
52
+ ...new Set([
53
+ canonical,
54
+ ref,
55
+ resolveModelRefForPi(canonical),
56
+ resolveModelRefForOmp(canonical)
57
+ ])
58
+ ];
59
+ }
60
+ function ompModelRefToCanonical(ref) {
61
+ return remapProviderPrefix(ref, OMP_TO_CANONICAL_PROVIDER);
62
+ }
63
+ function resolveModelRefForOmp(ref) {
64
+ return remapProviderPrefix(ompModelRefToCanonical(ref), CANONICAL_TO_OMP_PROVIDER);
65
+ }
66
+
31
67
  // ../plugin/src/config/schema/magic-context.ts
32
68
  import { homedir } from "node:os";
33
69
 
@@ -3191,7 +3227,7 @@ function initializeContext(params) {
3191
3227
  external: params?.external ?? undefined
3192
3228
  };
3193
3229
  }
3194
- function process2(schema, ctx, _params = { path: [], schemaPath: [] }) {
3230
+ function process(schema, ctx, _params = { path: [], schemaPath: [] }) {
3195
3231
  var _a;
3196
3232
  const def = schema._zod.def;
3197
3233
  const seen = ctx.seen.get(schema);
@@ -3228,7 +3264,7 @@ function process2(schema, ctx, _params = { path: [], schemaPath: [] }) {
3228
3264
  if (parent) {
3229
3265
  if (!result.ref)
3230
3266
  result.ref = parent;
3231
- process2(parent, ctx, params);
3267
+ process(parent, ctx, params);
3232
3268
  ctx.seen.get(parent).isParent = true;
3233
3269
  }
3234
3270
  }
@@ -3511,14 +3547,14 @@ function isTransforming(_schema, _ctx) {
3511
3547
  }
3512
3548
  var createToJSONSchemaMethod = (schema, processors = {}) => (params) => {
3513
3549
  const ctx = initializeContext({ ...params, processors });
3514
- process2(schema, ctx);
3550
+ process(schema, ctx);
3515
3551
  extractDefs(ctx, schema);
3516
3552
  return finalize(ctx, schema);
3517
3553
  };
3518
3554
  var createStandardJSONSchemaMethod = (schema, io, processors = {}) => (params) => {
3519
3555
  const { libraryOptions, target } = params ?? {};
3520
3556
  const ctx = initializeContext({ ...libraryOptions ?? {}, target, io, processors });
3521
- process2(schema, ctx);
3557
+ process(schema, ctx);
3522
3558
  extractDefs(ctx, schema);
3523
3559
  return finalize(ctx, schema);
3524
3560
  };
@@ -3668,7 +3704,7 @@ var arrayProcessor = (schema, ctx, _json, params) => {
3668
3704
  if (typeof maximum === "number")
3669
3705
  json.maxItems = maximum;
3670
3706
  json.type = "array";
3671
- json.items = process2(def.element, ctx, {
3707
+ json.items = process(def.element, ctx, {
3672
3708
  ...params,
3673
3709
  path: [...params.path, "items"]
3674
3710
  });
@@ -3680,7 +3716,7 @@ var objectProcessor = (schema, ctx, _json, params) => {
3680
3716
  json.properties = {};
3681
3717
  const shape = def.shape;
3682
3718
  for (const key in shape) {
3683
- json.properties[key] = process2(shape[key], ctx, {
3719
+ json.properties[key] = process(shape[key], ctx, {
3684
3720
  ...params,
3685
3721
  path: [...params.path, "properties", key]
3686
3722
  });
@@ -3703,7 +3739,7 @@ var objectProcessor = (schema, ctx, _json, params) => {
3703
3739
  if (ctx.io === "output")
3704
3740
  json.additionalProperties = false;
3705
3741
  } else if (def.catchall) {
3706
- json.additionalProperties = process2(def.catchall, ctx, {
3742
+ json.additionalProperties = process(def.catchall, ctx, {
3707
3743
  ...params,
3708
3744
  path: [...params.path, "additionalProperties"]
3709
3745
  });
@@ -3712,7 +3748,7 @@ var objectProcessor = (schema, ctx, _json, params) => {
3712
3748
  var unionProcessor = (schema, ctx, json, params) => {
3713
3749
  const def = schema._zod.def;
3714
3750
  const isExclusive = def.inclusive === false;
3715
- const options = def.options.map((x, i) => process2(x, ctx, {
3751
+ const options = def.options.map((x, i) => process(x, ctx, {
3716
3752
  ...params,
3717
3753
  path: [...params.path, isExclusive ? "oneOf" : "anyOf", i]
3718
3754
  }));
@@ -3724,11 +3760,11 @@ var unionProcessor = (schema, ctx, json, params) => {
3724
3760
  };
3725
3761
  var intersectionProcessor = (schema, ctx, json, params) => {
3726
3762
  const def = schema._zod.def;
3727
- const a = process2(def.left, ctx, {
3763
+ const a = process(def.left, ctx, {
3728
3764
  ...params,
3729
3765
  path: [...params.path, "allOf", 0]
3730
3766
  });
3731
- const b = process2(def.right, ctx, {
3767
+ const b = process(def.right, ctx, {
3732
3768
  ...params,
3733
3769
  path: [...params.path, "allOf", 1]
3734
3770
  });
@@ -3747,7 +3783,7 @@ var recordProcessor = (schema, ctx, _json, params) => {
3747
3783
  const keyBag = keyType._zod.bag;
3748
3784
  const patterns = keyBag?.patterns;
3749
3785
  if (def.mode === "loose" && patterns && patterns.size > 0) {
3750
- const valueSchema = process2(def.valueType, ctx, {
3786
+ const valueSchema = process(def.valueType, ctx, {
3751
3787
  ...params,
3752
3788
  path: [...params.path, "patternProperties", "*"]
3753
3789
  });
@@ -3757,12 +3793,12 @@ var recordProcessor = (schema, ctx, _json, params) => {
3757
3793
  }
3758
3794
  } else {
3759
3795
  if (ctx.target === "draft-07" || ctx.target === "draft-2020-12") {
3760
- json.propertyNames = process2(def.keyType, ctx, {
3796
+ json.propertyNames = process(def.keyType, ctx, {
3761
3797
  ...params,
3762
3798
  path: [...params.path, "propertyNames"]
3763
3799
  });
3764
3800
  }
3765
- json.additionalProperties = process2(def.valueType, ctx, {
3801
+ json.additionalProperties = process(def.valueType, ctx, {
3766
3802
  ...params,
3767
3803
  path: [...params.path, "additionalProperties"]
3768
3804
  });
@@ -3777,7 +3813,7 @@ var recordProcessor = (schema, ctx, _json, params) => {
3777
3813
  };
3778
3814
  var nullableProcessor = (schema, ctx, json, params) => {
3779
3815
  const def = schema._zod.def;
3780
- const inner = process2(def.innerType, ctx, params);
3816
+ const inner = process(def.innerType, ctx, params);
3781
3817
  const seen = ctx.seen.get(schema);
3782
3818
  if (ctx.target === "openapi-3.0") {
3783
3819
  seen.ref = def.innerType;
@@ -3788,20 +3824,20 @@ var nullableProcessor = (schema, ctx, json, params) => {
3788
3824
  };
3789
3825
  var nonoptionalProcessor = (schema, ctx, _json, params) => {
3790
3826
  const def = schema._zod.def;
3791
- process2(def.innerType, ctx, params);
3827
+ process(def.innerType, ctx, params);
3792
3828
  const seen = ctx.seen.get(schema);
3793
3829
  seen.ref = def.innerType;
3794
3830
  };
3795
3831
  var defaultProcessor = (schema, ctx, json, params) => {
3796
3832
  const def = schema._zod.def;
3797
- process2(def.innerType, ctx, params);
3833
+ process(def.innerType, ctx, params);
3798
3834
  const seen = ctx.seen.get(schema);
3799
3835
  seen.ref = def.innerType;
3800
3836
  json.default = JSON.parse(JSON.stringify(def.defaultValue));
3801
3837
  };
3802
3838
  var prefaultProcessor = (schema, ctx, json, params) => {
3803
3839
  const def = schema._zod.def;
3804
- process2(def.innerType, ctx, params);
3840
+ process(def.innerType, ctx, params);
3805
3841
  const seen = ctx.seen.get(schema);
3806
3842
  seen.ref = def.innerType;
3807
3843
  if (ctx.io === "input")
@@ -3809,7 +3845,7 @@ var prefaultProcessor = (schema, ctx, json, params) => {
3809
3845
  };
3810
3846
  var catchProcessor = (schema, ctx, json, params) => {
3811
3847
  const def = schema._zod.def;
3812
- process2(def.innerType, ctx, params);
3848
+ process(def.innerType, ctx, params);
3813
3849
  const seen = ctx.seen.get(schema);
3814
3850
  seen.ref = def.innerType;
3815
3851
  let catchValue;
@@ -3824,20 +3860,20 @@ var pipeProcessor = (schema, ctx, _json, params) => {
3824
3860
  const def = schema._zod.def;
3825
3861
  const inIsTransform = def.in._zod.traits.has("$ZodTransform");
3826
3862
  const innerType = ctx.io === "input" ? inIsTransform ? def.out : def.in : def.out;
3827
- process2(innerType, ctx, params);
3863
+ process(innerType, ctx, params);
3828
3864
  const seen = ctx.seen.get(schema);
3829
3865
  seen.ref = innerType;
3830
3866
  };
3831
3867
  var readonlyProcessor = (schema, ctx, json, params) => {
3832
3868
  const def = schema._zod.def;
3833
- process2(def.innerType, ctx, params);
3869
+ process(def.innerType, ctx, params);
3834
3870
  const seen = ctx.seen.get(schema);
3835
3871
  seen.ref = def.innerType;
3836
3872
  json.readOnly = true;
3837
3873
  };
3838
3874
  var optionalProcessor = (schema, ctx, _json, params) => {
3839
3875
  const def = schema._zod.def;
3840
- process2(def.innerType, ctx, params);
3876
+ process(def.innerType, ctx, params);
3841
3877
  const seen = ctx.seen.get(schema);
3842
3878
  seen.ref = def.innerType;
3843
3879
  };
@@ -4941,6 +4977,234 @@ function nextDueAtMs(expression, afterMs, consumedScheduledAtMs, maxSearchMs = M
4941
4977
  return next ? next.getTime() : null;
4942
4978
  }
4943
4979
 
4980
+ // ../plugin/src/shared/historian-tool-template.ts
4981
+ class Parser {
4982
+ source;
4983
+ pos = 0;
4984
+ constructor(source) {
4985
+ this.source = source;
4986
+ }
4987
+ error() {
4988
+ throw new Error(`Invalid tool expansion template at character ${this.pos + 1}`);
4989
+ }
4990
+ take(value) {
4991
+ if (!this.source.startsWith(value, this.pos))
4992
+ return false;
4993
+ this.pos += value.length;
4994
+ return true;
4995
+ }
4996
+ field() {
4997
+ const match = /^[A-Za-z_][A-Za-z0-9_-]*/.exec(this.source.slice(this.pos));
4998
+ if (!match)
4999
+ return this.error();
5000
+ this.pos += match[0].length;
5001
+ return match[0];
5002
+ }
5003
+ quoted() {
5004
+ const start = this.pos;
5005
+ if (!this.take('"'))
5006
+ return this.error();
5007
+ while (this.pos < this.source.length) {
5008
+ if (this.take("\\")) {
5009
+ this.pos++;
5010
+ continue;
5011
+ }
5012
+ if (this.take('"')) {
5013
+ try {
5014
+ return JSON.parse(this.source.slice(start, this.pos));
5015
+ } catch {
5016
+ return this.error();
5017
+ }
5018
+ }
5019
+ this.pos++;
5020
+ }
5021
+ return this.error();
5022
+ }
5023
+ integer() {
5024
+ const match = /^\d+/.exec(this.source.slice(this.pos));
5025
+ if (!match)
5026
+ return this.error();
5027
+ this.pos += match[0].length;
5028
+ const n = Number(match[0]);
5029
+ if (!Number.isSafeInteger(n))
5030
+ return this.error();
5031
+ return n;
5032
+ }
5033
+ expression(relative) {
5034
+ const steps = [];
5035
+ if (!(relative && this.take("."))) {
5036
+ const root = this.field();
5037
+ if (!relative && root !== "input" && root !== "output")
5038
+ return this.error();
5039
+ steps.push({ field: root });
5040
+ }
5041
+ while (true) {
5042
+ if (this.take("[")) {
5043
+ if (this.take("*]."))
5044
+ steps.push({ project: this.field() });
5045
+ else {
5046
+ const index = this.integer();
5047
+ if (!this.take("]"))
5048
+ return this.error();
5049
+ steps.push({ index });
5050
+ }
5051
+ } else if (this.source[this.pos] === "." && !/^\.(each\(|join\(|count(?:\.|\})|truncate\()/.test(this.source.slice(this.pos))) {
5052
+ this.pos++;
5053
+ steps.push({ field: this.field() });
5054
+ if (this.source[this.pos] === "(")
5055
+ return this.error();
5056
+ } else
5057
+ break;
5058
+ }
5059
+ const expression = { steps };
5060
+ if (this.take(".each(")) {
5061
+ if (relative)
5062
+ return this.error();
5063
+ expression.each = parseTemplate(this.quoted(), true);
5064
+ if (!this.take(")"))
5065
+ return this.error();
5066
+ }
5067
+ if (this.take(".join(")) {
5068
+ expression.join = this.quoted();
5069
+ if (!this.take(")"))
5070
+ return this.error();
5071
+ }
5072
+ if (this.take(".count")) {
5073
+ if (expression.each || expression.join !== undefined)
5074
+ return this.error();
5075
+ expression.count = true;
5076
+ }
5077
+ if (this.take(".truncate(")) {
5078
+ expression.cap = this.integer();
5079
+ if (!this.take(")"))
5080
+ return this.error();
5081
+ }
5082
+ if (!this.take("}"))
5083
+ return this.error();
5084
+ return expression;
5085
+ }
5086
+ template(relative) {
5087
+ const nodes = [];
5088
+ while (this.pos < this.source.length) {
5089
+ const next = this.source.indexOf("${", this.pos);
5090
+ if (next < 0) {
5091
+ nodes.push(this.source.slice(this.pos));
5092
+ break;
5093
+ }
5094
+ nodes.push(this.source.slice(this.pos, next));
5095
+ this.pos = next + 2;
5096
+ nodes.push(this.expression(relative));
5097
+ }
5098
+ return nodes;
5099
+ }
5100
+ }
5101
+ function parseTemplate(source, relative = false) {
5102
+ return new Parser(source).template(relative);
5103
+ }
5104
+ function toolTemplateError(source) {
5105
+ try {
5106
+ parseTemplate(source);
5107
+ return;
5108
+ } catch (error) {
5109
+ return error.message;
5110
+ }
5111
+ }
5112
+ var templates = new Map;
5113
+ function compiled(source) {
5114
+ if (!templates.has(source)) {
5115
+ try {
5116
+ templates.set(source, parseTemplate(source));
5117
+ } catch {
5118
+ templates.set(source, null);
5119
+ }
5120
+ }
5121
+ return templates.get(source) ?? null;
5122
+ }
5123
+ function oneLine(value) {
5124
+ return value.replace(/[\r\n\u2028\u2029]+/g, " ");
5125
+ }
5126
+ function truncate(value, cap, legacyPreview = false) {
5127
+ const chars = Array.from(oneLine(value));
5128
+ if (cap === undefined || chars.length <= cap)
5129
+ return chars.join("");
5130
+ if (legacyPreview)
5131
+ return `${chars.slice(0, cap).join("")}…`;
5132
+ let end = 0;
5133
+ for (let i = 0;i < Math.min(cap, chars.length); i++) {
5134
+ if (!".!?。!?".includes(chars[i]))
5135
+ continue;
5136
+ let next = i + 1;
5137
+ while (next < chars.length && `"'”’)]`.includes(chars[next]))
5138
+ next++;
5139
+ if (next <= cap && ("。!?".includes(chars[i]) || next === chars.length || /[ \t]/.test(chars[next])))
5140
+ end = next;
5141
+ }
5142
+ return `${end ? `${chars.slice(0, end).join("")} ` : ""}[… ${chars.length - end} more characters]`;
5143
+ }
5144
+ function scalar(value) {
5145
+ if (value === undefined)
5146
+ return "";
5147
+ return typeof value === "string" ? value : compactJson(value);
5148
+ }
5149
+ function compactJson(value) {
5150
+ if (Array.isArray(value))
5151
+ return `[${value.map((v) => compactJson(v)).join(",")}]`;
5152
+ if (value !== null && typeof value === "object")
5153
+ return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${compactJson(value[key])}`).join(",")}}`;
5154
+ return JSON.stringify(value) ?? "null";
5155
+ }
5156
+ function field(value, key) {
5157
+ return value !== null && typeof value === "object" && !Array.isArray(value) && Object.hasOwn(value, key) ? value[key] : undefined;
5158
+ }
5159
+ function render(nodes, root, legacyPreview) {
5160
+ return nodes.map((node) => {
5161
+ if (typeof node === "string")
5162
+ return node;
5163
+ const cap = node.cap ?? (legacyPreview ? 300 : undefined);
5164
+ let value = root;
5165
+ let list = false;
5166
+ for (const step of node.steps) {
5167
+ if ("field" in step)
5168
+ value = list && Array.isArray(value) ? value.map((item) => field(item, step.field)) : field(value, step.field);
5169
+ else if ("index" in step)
5170
+ value = Array.isArray(value) ? value[step.index] : undefined;
5171
+ else {
5172
+ value = Array.isArray(value) ? value.map((item) => field(item, step.project)) : undefined;
5173
+ list = true;
5174
+ }
5175
+ }
5176
+ if (node.count)
5177
+ return truncate(Array.isArray(value) ? String(value.length) : "", cap, legacyPreview);
5178
+ if (node.each || list || node.join !== undefined) {
5179
+ if (!Array.isArray(value))
5180
+ return "";
5181
+ const elements = value.slice(0, legacyPreview ? 10 : undefined).map((item) => truncate(node.each ? render(node.each, item, legacyPreview) : scalar(item), legacyPreview ? 300 : undefined, legacyPreview));
5182
+ if (legacyPreview && value.length > 10)
5183
+ elements.push(`… +${value.length - 10} more`);
5184
+ return truncate(elements.join(node.join ?? (node.each ? "; " : ", ")), cap, legacyPreview);
5185
+ }
5186
+ return truncate(scalar(value), cap, legacyPreview);
5187
+ }).join("");
5188
+ }
5189
+ function renderToolTemplate(source, input, output, legacyPreview = false) {
5190
+ const nodes = compiled(source);
5191
+ if (!nodes)
5192
+ return null;
5193
+ const text = typeof output === "string" ? output : output === undefined ? "" : compactJson(output);
5194
+ let structured = output;
5195
+ if (typeof output === "string") {
5196
+ try {
5197
+ const parsed = JSON.parse(output);
5198
+ structured = parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : undefined;
5199
+ } catch {
5200
+ structured = undefined;
5201
+ }
5202
+ }
5203
+ const root = { input, output: structured };
5204
+ 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);
5205
+ return truncate(render(bareOutputNodes, root, legacyPreview), legacyPreview ? 1000 : undefined, legacyPreview);
5206
+ }
5207
+
4944
5208
  // ../plugin/src/shared/prompt-surface.ts
4945
5209
  function isValidPromptSurfaceModelKey(key) {
4946
5210
  if (key.length === 0 || key.trim() !== key)
@@ -5341,7 +5605,7 @@ var BaseEmbeddingConfigSchema = object({
5341
5605
  document_prefix: string2().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."),
5342
5606
  truncate: string2().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."),
5343
5607
  max_input_tokens: number2().int().positive().optional().describe("Optional maximum input tokens for chunk embeddings. Defaults conservatively to 512 when omitted."),
5344
- 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."),
5608
+ 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."),
5345
5609
  local_dtype: _enum([
5346
5610
  "auto",
5347
5611
  "fp32",
@@ -5450,8 +5714,8 @@ var MagicContextConfigSchema = object({
5450
5714
  object({ default: number2().min(0) }).catchall(number2().min(0))
5451
5715
  ]).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.'),
5452
5716
  models: object({
5453
- window_overlay_path: string2().trim().min(1).optional()
5454
- }).optional().describe("User-only Fusiform window-overlay settings. The path defaults to <dataDir>/fusiform/window-overlay.json."),
5717
+ 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 })
5718
+ }).optional().describe("Deprecated user-only Fusiform window-overlay settings; ignored."),
5455
5719
  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)"),
5456
5720
  execute_threshold_percentage: union([
5457
5721
  number2().min(20).max(90, EXECUTE_THRESHOLD_CAP_MESSAGE),
@@ -5617,1026 +5881,4 @@ function deriveDefaultProtectedTokens(usableSoft) {
5617
5881
  return Math.min(64000, Math.max(lowerBound, target));
5618
5882
  }
5619
5883
 
5620
- // ../plugin/src/features/magic-context/memory/embedding-model-match.ts
5621
- function parseEmbeddingModel(model) {
5622
- const lastColon = model.lastIndexOf(":");
5623
- if (lastColon > model.lastIndexOf("/")) {
5624
- return { base: model.slice(0, lastColon), tag: model.slice(lastColon + 1) };
5625
- }
5626
- return { base: model };
5627
- }
5628
- function normalizeEmbeddingModelId(model) {
5629
- return parseEmbeddingModel(model.trim().toLowerCase()).base;
5630
- }
5631
- function matchNormalizedEmbeddingModels(a, b) {
5632
- if (a.length === 0 || b.length === 0)
5633
- return true;
5634
- if (a === b)
5635
- return true;
5636
- const longer = a.length >= b.length ? a : b;
5637
- const shorter = a.length >= b.length ? b : a;
5638
- const isBoundary = (ch) => ch === "-" || ch === "/";
5639
- if (longer.startsWith(shorter) && isBoundary(longer.charAt(shorter.length)))
5640
- return true;
5641
- if (longer.endsWith(shorter) && isBoundary(longer.charAt(longer.length - shorter.length - 1)))
5642
- return true;
5643
- return false;
5644
- }
5645
- function embeddingModelsMatch(served, requested) {
5646
- const servedNormalized = served.trim().toLowerCase();
5647
- const requestedNormalized = requested.trim().toLowerCase();
5648
- const servedModel = parseEmbeddingModel(servedNormalized);
5649
- const requestedModel = parseEmbeddingModel(requestedNormalized);
5650
- if (servedModel.tag !== undefined && requestedModel.tag !== undefined && servedModel.tag !== requestedModel.tag) {
5651
- return false;
5652
- }
5653
- return matchNormalizedEmbeddingModels(servedModel.base, requestedModel.base);
5654
- }
5655
- var QWEN3_QUERY_INSTRUCTION = `Instruct: Given a web search query, retrieve relevant passages that answer the query
5656
- Query: `;
5657
- var INSTRUCT_WEB_SEARCH_QUERY_PREFIX = QWEN3_QUERY_INSTRUCTION;
5658
- var EMBEDDING_MODEL_PREFIX_FAMILIES = [
5659
- {
5660
- family: "qwen3-embedding",
5661
- basenamePattern: /^qwen3-embedding(?:-|$)/,
5662
- prefixes: { queryPrefix: QWEN3_QUERY_INSTRUCTION, documentPrefix: "" }
5663
- },
5664
- {
5665
- family: "gte-qwen-instruct",
5666
- basenamePattern: /^gte-qwen.*-instruct(?:-|$)/,
5667
- prefixes: { queryPrefix: INSTRUCT_WEB_SEARCH_QUERY_PREFIX, documentPrefix: "" }
5668
- },
5669
- {
5670
- family: "e5-instruct",
5671
- basenamePattern: /^(?:multilingual-)?e5-.*-instruct(?:-|$)/,
5672
- prefixes: { queryPrefix: INSTRUCT_WEB_SEARCH_QUERY_PREFIX, documentPrefix: "" }
5673
- },
5674
- {
5675
- family: "nomic-embed-text",
5676
- basenamePattern: /^nomic-embed-text(?:-|$)/,
5677
- prefixes: { queryPrefix: "search_query: ", documentPrefix: "search_document: " }
5678
- }
5679
- ];
5680
- function modelBasename(model) {
5681
- const normalized = normalizeEmbeddingModelId(model);
5682
- return normalized.slice(normalized.lastIndexOf("/") + 1);
5683
- }
5684
- function resolveEmbeddingTextPrefixes(model, queryInstruction, documentPrefix) {
5685
- const family = EMBEDDING_MODEL_PREFIX_FAMILIES.find(({ basenamePattern }) => basenamePattern.test(modelBasename(model)));
5686
- return {
5687
- queryPrefix: queryInstruction === false ? "" : queryInstruction !== undefined ? queryInstruction : family?.prefixes.queryPrefix ?? "",
5688
- documentPrefix: documentPrefix ?? family?.prefixes.documentPrefix ?? ""
5689
- };
5690
- }
5691
-
5692
- // ../plugin/src/features/magic-context/memory/embedding-identity.ts
5693
- function normalizeEndpoint(endpoint) {
5694
- return endpoint?.trim().replace(/\/+$/, "") ?? "";
5695
- }
5696
- var LOCAL_EMBEDDING_RUNTIME_FINGERPRINT = "transformers@4.3.0;onnxruntime-node@1.30.0;onnxruntime-web@1.26.0-dev.20260416-b7804b056c";
5697
- function getEmbeddingProviderIdentity(config) {
5698
- if (config.provider === "off") {
5699
- return "embedding-provider:off";
5700
- }
5701
- if (config.provider === "synapse") {
5702
- const resolved = config;
5703
- if (!resolved.model || !resolved.synapse_fingerprint)
5704
- return "synapse:v1:pending";
5705
- return getSynapseLaneIdentity(resolved.model, resolved.synapse_fingerprint);
5706
- }
5707
- if (config.provider !== "local" && config.provider !== "openai-compatible") {
5708
- throw new Error("Unknown embedding provider");
5709
- }
5710
- const truncate = config.provider === "openai-compatible" ? config.truncate?.trim() : undefined;
5711
- const documentPrefix = config.provider === "openai-compatible" ? resolveEmbeddingTextPrefixes(config.model, config.query_instruction, config.document_prefix).documentPrefix : "";
5712
- const localDtype = config.provider === "local" && config.local_dtype && config.local_dtype !== "fp32" ? config.local_dtype : undefined;
5713
- const identityInput = config.provider === "openai-compatible" ? {
5714
- provider: "openai-compatible",
5715
- model: config.model.trim(),
5716
- endpoint: normalizeEndpoint(config.endpoint),
5717
- apiKeyPresent: Boolean(config.api_key?.trim()),
5718
- inputType: config.input_type?.trim() || "",
5719
- ...documentPrefix ? { documentPrefix } : {},
5720
- ...truncate ? { truncate } : {}
5721
- } : {
5722
- provider: "local",
5723
- model: config.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL,
5724
- endpoint: "",
5725
- apiKeyPresent: false,
5726
- runtimeFingerprint: LOCAL_EMBEDDING_RUNTIME_FINGERPRINT,
5727
- ...localDtype ? { localDtype } : {}
5728
- };
5729
- return `embedding-provider:${computeNormalizedHash(JSON.stringify(identityInput))}`;
5730
- }
5731
-
5732
- // ../plugin/src/features/magic-context/memory/embedding-local.ts
5733
- import { readdirSync, statSync } from "node:fs";
5734
- import { stat, unlink } from "node:fs/promises";
5735
- import { dirname, join } from "node:path";
5736
- import { pathToFileURL } from "node:url";
5737
- import { workerData } from "node:worker_threads";
5738
-
5739
- // ../plugin/src/features/magic-context/memory/embedding-failure.ts
5740
- function errorMessage(error) {
5741
- return error instanceof Error ? error.message : String(error ?? "unknown error");
5742
- }
5743
- function nestedMessages(error) {
5744
- const messages = [errorMessage(error)];
5745
- if (error && typeof error === "object" && "cause" in error) {
5746
- const cause = error.cause;
5747
- if (cause !== undefined)
5748
- messages.push(...nestedMessages(cause));
5749
- }
5750
- return messages;
5751
- }
5752
- function safeErrorDetail(message) {
5753
- return message.replace(/https?:\/\/[^\s"']+/gi, "<remote URL>").replace(/\/(?:Users|home)\/[^/\s]+/g, "~").replace(/\s+/g, " ").trim().slice(0, 240);
5754
- }
5755
- function hasAny(messages, patterns) {
5756
- return messages.some((message) => {
5757
- const lower = message.toLowerCase();
5758
- return patterns.some((pattern) => lower.includes(pattern));
5759
- });
5760
- }
5761
- function classifyLocalEmbeddingFailure(error, context = {}) {
5762
- const primaryMessages = nestedMessages(error);
5763
- const nativeMessages = context.nativeError === undefined ? [] : nestedMessages(context.nativeError);
5764
- if (hasAny(primaryMessages, [
5765
- "mc_embedding_fs_unavailable",
5766
- "file system cache is not available"
5767
- ]) || context.usesWasm && hasAny(primaryMessages, ["unable to get model file path or buffer"])) {
5768
- return {
5769
- class: "local_fs_unavailable",
5770
- reason: "the WASM model cache cannot access the Node filesystem",
5771
- retryable: false
5772
- };
5773
- }
5774
- if (hasAny(primaryMessages, [
5775
- "failed to fetch",
5776
- "fetch failed",
5777
- "network error",
5778
- "enotfound",
5779
- "econnreset",
5780
- "econnrefused",
5781
- "certificate",
5782
- "unable to load file",
5783
- "unauthorized access to file",
5784
- "forbidden access to file",
5785
- "not found at"
5786
- ]) || primaryMessages.some((message) => /\bHTTP\s+[45]\d\d\b/i.test(message))) {
5787
- return {
5788
- class: "local_download_failure",
5789
- reason: `the embedding model download failed: ${safeErrorDetail(primaryMessages[0])}`,
5790
- retryable: true
5791
- };
5792
- }
5793
- const bindingMessages = [...primaryMessages, ...nativeMessages];
5794
- if (hasAny(bindingMessages, [
5795
- "onnxruntime_binding.node",
5796
- "cannot find package 'onnxruntime-node'",
5797
- 'cannot find package "onnxruntime-node"',
5798
- "cannot find module 'onnxruntime-node'",
5799
- 'cannot find module "onnxruntime-node"',
5800
- "could not resolve: onnxruntime-node"
5801
- ])) {
5802
- const platform = context.platform ?? process.platform;
5803
- const arch = context.arch ?? process.arch;
5804
- return {
5805
- class: "local_binding_missing",
5806
- 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`,
5807
- retryable: false
5808
- };
5809
- }
5810
- return {
5811
- class: "local_runtime_error",
5812
- reason: `the local embedding runtime failed: ${safeErrorDetail(primaryMessages[0])}`,
5813
- retryable: false
5814
- };
5815
- }
5816
- function dominantEmbeddingFailure(failures) {
5817
- const counts = new Map;
5818
- for (const failure of failures) {
5819
- const key = `${failure.class}\x00${failure.reason}`;
5820
- const current = counts.get(key);
5821
- if (current)
5822
- current.count += 1;
5823
- else
5824
- counts.set(key, { failure, count: 1 });
5825
- }
5826
- let dominant;
5827
- for (const candidate of counts.values()) {
5828
- if (!dominant || candidate.count > dominant.count)
5829
- dominant = candidate;
5830
- }
5831
- return dominant?.failure;
5832
- }
5833
-
5834
- // ../plugin/src/features/magic-context/memory/embedding-worker-client.ts
5835
- import { Worker } from "node:worker_threads";
5836
- class EmbeddingWorkerClient {
5837
- data;
5838
- entry;
5839
- timeoutMs;
5840
- worker = null;
5841
- nextId = 1;
5842
- pending = new Map;
5843
- disposed = false;
5844
- 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) {
5845
- this.data = data;
5846
- this.entry = entry;
5847
- this.timeoutMs = timeoutMs;
5848
- }
5849
- start() {
5850
- if (this.disposed)
5851
- throw new Error("embedding worker disposed");
5852
- if (this.worker)
5853
- return this.worker;
5854
- const worker = new Worker(this.entry, { workerData: this.data });
5855
- this.worker = worker;
5856
- worker.unref();
5857
- worker.on("message", (reply) => {
5858
- if (this.worker !== worker)
5859
- return;
5860
- const request = this.pending.get(reply.id);
5861
- if (!request)
5862
- return;
5863
- clearTimeout(request.timer);
5864
- this.pending.delete(reply.id);
5865
- if (reply.error)
5866
- request.reject(new Error(reply.error));
5867
- else
5868
- request.resolve(reply);
5869
- if (this.pending.size === 0)
5870
- worker.unref();
5871
- });
5872
- const fail = (error) => {
5873
- if (this.worker !== worker)
5874
- return;
5875
- this.worker = null;
5876
- for (const request of this.pending.values()) {
5877
- clearTimeout(request.timer);
5878
- request.reject(error);
5879
- }
5880
- this.pending.clear();
5881
- log2("[magic-context] embedding worker exited unexpectedly:", error);
5882
- worker.terminate();
5883
- };
5884
- worker.on("error", fail);
5885
- worker.on("exit", (code) => fail(new Error(`embedding worker exited (${code})`)));
5886
- return worker;
5887
- }
5888
- isRunning() {
5889
- return this.worker !== null;
5890
- }
5891
- request(texts, signal) {
5892
- if (signal?.aborted)
5893
- return Promise.reject(new Error("embedding request aborted"));
5894
- return new Promise((resolve, reject) => {
5895
- const worker = this.start();
5896
- const id = this.nextId++;
5897
- const timer = setTimeout(() => {
5898
- this.stop(new Error("embedding worker request timed out"));
5899
- }, this.timeoutMs);
5900
- const abort = () => {
5901
- clearTimeout(timer);
5902
- this.pending.delete(id);
5903
- signal?.removeEventListener("abort", abort);
5904
- reject(new Error("embedding request aborted"));
5905
- if (this.pending.size === 0)
5906
- worker.unref();
5907
- };
5908
- signal?.addEventListener("abort", abort, { once: true });
5909
- const cleanup = () => signal?.removeEventListener("abort", abort);
5910
- this.pending.set(id, {
5911
- timer,
5912
- resolve: (reply) => {
5913
- cleanup();
5914
- resolve(reply);
5915
- },
5916
- reject: (error) => {
5917
- cleanup();
5918
- reject(error);
5919
- }
5920
- });
5921
- worker.ref();
5922
- try {
5923
- worker.postMessage({ id, texts });
5924
- } catch (error) {
5925
- this.stop(error instanceof Error ? error : new Error(String(error)));
5926
- }
5927
- });
5928
- }
5929
- async stop(error) {
5930
- const worker = this.worker;
5931
- this.worker = null;
5932
- for (const request of this.pending.values()) {
5933
- clearTimeout(request.timer);
5934
- request.reject(error);
5935
- }
5936
- this.pending.clear();
5937
- await worker?.terminate();
5938
- }
5939
- async dispose() {
5940
- this.disposed = true;
5941
- await this.stop(new Error("embedding worker disposed"));
5942
- }
5943
- }
5944
-
5945
- // ../plugin/src/features/magic-context/memory/transformers-remote-host.ts
5946
- var DEFAULT_TRANSFORMERS_REMOTE_HOST = "https://huggingface.co/";
5947
- function readHuggingFaceEndpoint() {
5948
- return globalThis.process?.env?.HF_ENDPOINT;
5949
- }
5950
- function configureTransformersRemoteHost(env, endpoint = readHuggingFaceEndpoint()) {
5951
- const normalized = endpoint?.trim().replace(/\/+$/, "");
5952
- env.remoteHost = normalized ? `${normalized}/` : DEFAULT_TRANSFORMERS_REMOTE_HOST;
5953
- }
5954
-
5955
- // ../plugin/src/features/magic-context/memory/embedding-local.ts
5956
- var BUN_NAPI_TEARDOWN_FIX_VERSION = [1, 4, 0];
5957
- function currentLocalEmbeddingHost() {
5958
- const bun = globalThis.Bun;
5959
- const hasProcess = typeof process !== "undefined";
5960
- const bunVersion = hasProcess && typeof process.versions?.bun === "string" ? process.versions.bun : undefined;
5961
- return {
5962
- isElectron: hasProcess && Boolean(process.versions?.electron),
5963
- isBun: Boolean(bun) || Boolean(bunVersion),
5964
- bunVersion,
5965
- hasNodeFilesystem: hasProcess && (typeof process.versions?.node === "string" || typeof bunVersion === "string")
5966
- };
5967
- }
5968
- function bunHasNapiTeardownFix(version) {
5969
- const match = version?.match(/^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/);
5970
- if (!match || match[4])
5971
- return false;
5972
- const parsed = [Number(match[1]), Number(match[2]), Number(match[3])];
5973
- 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]);
5974
- }
5975
- function resolveLocalEmbeddingRuntime(preference = "auto", host = currentLocalEmbeddingHost()) {
5976
- if (preference === "native" || preference === "wasm")
5977
- return preference;
5978
- if (host.isElectron)
5979
- return "electron";
5980
- if (host.isBun && !bunHasNapiTeardownFix(host.bunVersion))
5981
- return "wasm";
5982
- return "native";
5983
- }
5984
- function resolveLocalEmbeddingWorkerRuntime(preference = "auto", host = currentLocalEmbeddingHost()) {
5985
- if (host.isBun && !bunHasNapiTeardownFix(host.bunVersion) && preference === "native")
5986
- return "wasm";
5987
- return resolveLocalEmbeddingRuntime(preference, host);
5988
- }
5989
- var LOCK_POLL_MS = 150;
5990
- var STALE_LOCK_MS = 3 * 60000;
5991
- var MAX_LOCK_WAIT_MS = 5 * 60000;
5992
- async function acquireModelLoadLock(lockPath) {
5993
- const waitStart = Date.now();
5994
- while (true) {
5995
- try {
5996
- const handle = await createStorageFileHandleAsync(lockPath, "wx");
5997
- try {
5998
- await writeStorageFileHandleAsync(handle, `pid=${process.pid} started=${Date.now()}
5999
- `);
6000
- } catch {}
6001
- await handle.close();
6002
- return async () => {
6003
- try {
6004
- await unlink(lockPath);
6005
- } catch {}
6006
- };
6007
- } catch (error) {
6008
- const code = error.code;
6009
- if (code !== "EEXIST" && code !== "EPERM") {
6010
- throw error;
6011
- }
6012
- try {
6013
- const info = await stat(lockPath);
6014
- if (Date.now() - info.mtimeMs > STALE_LOCK_MS) {
6015
- log2(`[magic-context] embedding-load lock stale (>${STALE_LOCK_MS}ms), taking over`);
6016
- try {
6017
- await unlink(lockPath);
6018
- } catch {}
6019
- continue;
6020
- }
6021
- } catch {
6022
- continue;
6023
- }
6024
- if (Date.now() - waitStart > MAX_LOCK_WAIT_MS) {
6025
- 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.`);
6026
- }
6027
- await new Promise((resolve) => setTimeout(resolve, LOCK_POLL_MS));
6028
- }
6029
- }
6030
- }
6031
- function startLockHeartbeat(lockPath) {
6032
- const HEARTBEAT_MS = Math.floor(STALE_LOCK_MS / 3);
6033
- const timer = setInterval(() => {
6034
- writeStorageFileAsync(lockPath, `pid=${process.pid} alive=${Date.now()}
6035
- `).catch(() => {});
6036
- }, HEARTBEAT_MS);
6037
- timer.unref?.();
6038
- return () => clearInterval(timer);
6039
- }
6040
- var ONNX_RUNTIME_WEB_SPECIFIER = "onnxruntime-web";
6041
- var useInThreadTestRuntime = false;
6042
- var workerFactoryForTests;
6043
- var localEmbeddingRuntimeMode = "native";
6044
- var localEmbeddingProcessFailure = null;
6045
- var wasmRuntimeInjected = false;
6046
- var localEmbeddingHostForRuntime = currentLocalEmbeddingHost;
6047
- var resolveWasmOrtForRuntime = () => {
6048
- try {
6049
- return typeof import.meta.resolve === "function" ? import.meta.resolve(ONNX_RUNTIME_WEB_SPECIFIER) : undefined;
6050
- } catch {
6051
- return;
6052
- }
6053
- };
6054
- var importWasmOrtModule = async (specifier) => await import(specifier);
6055
- var importWasmOrtModuleForRuntime = importWasmOrtModule;
6056
- var importWasmOrtForRuntime = async () => {
6057
- const { createRequire: createRequireFn } = await import("node:module");
6058
- const requireFn = createRequireFn(import.meta.url);
6059
- const ortEntry = requireFn.resolve(ONNX_RUNTIME_WEB_SPECIFIER);
6060
- const ortSpecifier = resolveWasmOrtForRuntime() ?? ONNX_RUNTIME_WEB_SPECIFIER;
6061
- return {
6062
- module: await importWasmOrtModuleForRuntime(ortSpecifier),
6063
- entryPath: ortEntry
6064
- };
6065
- };
6066
- var importTransformersForRuntime = async () => {
6067
- return await importPluginModule(() => import("./transformers.node-3zqr6k1w.js"));
6068
- };
6069
- var importTransformersWasmFallbackForRuntime = async () => {
6070
- const webEntry = new URL(new URL(import.meta.url).pathname.endsWith(".ts") ? "../../../../dist/transformers-web.js" : "./transformers-web.js", import.meta.url).href;
6071
- return await importPluginModule(() => import(webEntry));
6072
- };
6073
- var importTransformersNodeWasmFallbackForRuntime = async () => {
6074
- 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;
6075
- return await importPluginModule(() => import(nodeWasmEntry));
6076
- };
6077
- var modelCacheDirForRuntime = () => workerData?.magicContextEmbeddingWorker && typeof workerData.modelCacheDir === "string" ? workerData.modelCacheDir : join(getMagicContextStorageDir(), "models");
6078
- var logForRuntime = log2;
6079
- var injectWasmOrtForRuntime = injectWasmOrt;
6080
- var loadedLocalEmbeddingRuntimes = new Map;
6081
- var nextLocalEmbeddingRuntimeId = 1;
6082
- function directoryBytes(path) {
6083
- let total = 0;
6084
- try {
6085
- for (const entry of readdirSync(path, { withFileTypes: true })) {
6086
- const entryPath = join(path, entry.name);
6087
- if (entry.isDirectory()) {
6088
- const childBytes = directoryBytes(entryPath);
6089
- if (childBytes === null)
6090
- return null;
6091
- total += childBytes;
6092
- } else if (entry.isFile()) {
6093
- total += statSync(entryPath).size;
6094
- }
6095
- }
6096
- return total;
6097
- } catch (error) {
6098
- return error.code === "ENOENT" ? 0 : null;
6099
- }
6100
- }
6101
- function getLocalEmbeddingNativeMemoryStats() {
6102
- const loaded = [...loadedLocalEmbeddingRuntimes.values()];
6103
- return {
6104
- loaded: loaded.length > 0,
6105
- providerCount: loaded.length,
6106
- models: [...new Set(loaded.map((entry) => entry.model))].sort(),
6107
- runtimes: [...new Set(loaded.map((entry) => entry.runtime))].sort(),
6108
- modelCacheBytes: loaded.length > 0 ? directoryBytes(modelCacheDirForRuntime()) : 0,
6109
- rssDeltaAtLoad: loaded.reduce((sum, entry) => sum + entry.rssDeltaAtLoad, 0),
6110
- externalDeltaAtLoad: loaded.reduce((sum, entry) => sum + entry.externalDeltaAtLoad, 0),
6111
- arrayBuffersDeltaAtLoad: loaded.reduce((sum, entry) => sum + entry.arrayBuffersDeltaAtLoad, 0)
6112
- };
6113
- }
6114
- async function ensureWasmOrtInjected() {
6115
- if (wasmRuntimeInjected)
6116
- return true;
6117
- if (!await injectWasmOrtForRuntime())
6118
- return false;
6119
- wasmRuntimeInjected = true;
6120
- return true;
6121
- }
6122
- async function injectWasmOrt() {
6123
- if (wasmRuntimeInjected)
6124
- return true;
6125
- try {
6126
- const { module: ortWeb, entryPath } = await importWasmOrtForRuntime();
6127
- if (ortWeb.env?.wasm) {
6128
- ortWeb.env.wasm.numThreads = 1;
6129
- ortWeb.env.wasm.wasmPaths = `${pathToFileURL(dirname(entryPath)).href}/`;
6130
- }
6131
- globalThis[Symbol.for("onnxruntime")] = ortWeb;
6132
- wasmRuntimeInjected = true;
6133
- return true;
6134
- } catch (error) {
6135
- log2("[magic-context] failed to inject onnxruntime-web:", error instanceof Error ? error.message : String(error));
6136
- return false;
6137
- }
6138
- }
6139
- function likelyMuslHint() {
6140
- if (process.platform !== "linux" || typeof process.report?.getReport !== "function")
6141
- return "";
6142
- try {
6143
- const report = process.report.getReport();
6144
- return typeof report.header?.glibcVersionRuntime === "string" ? "" : " Linux process report has no glibc runtime version (musl likely).";
6145
- } catch {
6146
- return "";
6147
- }
6148
- }
6149
- function localEmbeddingRuntimeIsDisabled() {
6150
- return localEmbeddingRuntimeMode === "disabled";
6151
- }
6152
-
6153
- class LocalEmbeddingFallbackError extends Error {
6154
- nativeError;
6155
- constructor(nativeError, wasmError) {
6156
- super("the native local embedding runtime and its WASM fallback both failed", {
6157
- cause: wasmError
6158
- });
6159
- this.name = "LocalEmbeddingFallbackError";
6160
- this.nativeError = nativeError;
6161
- }
6162
- }
6163
-
6164
- class LocalEmbeddingFsUnavailableError extends Error {
6165
- code = "MC_EMBEDDING_FS_UNAVAILABLE";
6166
- constructor() {
6167
- super("MC_EMBEDDING_FS_UNAVAILABLE: Node WASM bundle has no filesystem cache");
6168
- this.name = "LocalEmbeddingFsUnavailableError";
6169
- }
6170
- }
6171
- async function importWasmTransformersForHost() {
6172
- const host = localEmbeddingHostForRuntime();
6173
- if (host.isElectron) {
6174
- return importTransformersForRuntime();
6175
- }
6176
- return host.hasNodeFilesystem ? importTransformersNodeWasmFallbackForRuntime() : importTransformersWasmFallbackForRuntime();
6177
- }
6178
- function disableLocalEmbeddingsAfterRuntimeFailure(detail) {
6179
- localEmbeddingRuntimeMode = "disabled";
6180
- 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.`);
6181
- }
6182
- async function loadTransformersForLocalEmbedding(runtimePreference) {
6183
- if (localEmbeddingRuntimeMode === "disabled") {
6184
- throw new Error("local embedding runtime is disabled");
6185
- }
6186
- const resolvedRuntime = resolveLocalEmbeddingRuntime(runtimePreference, localEmbeddingHostForRuntime());
6187
- if (resolvedRuntime === "wasm") {
6188
- if (!await ensureWasmOrtInjected()) {
6189
- disableLocalEmbeddingsAfterRuntimeFailure("the selected WASM runtime is unavailable");
6190
- throw new Error("onnxruntime-web failed to load");
6191
- }
6192
- return { module: await importWasmTransformersForHost(), usesWasm: true };
6193
- }
6194
- if (localEmbeddingRuntimeMode === "wasm") {
6195
- if (!await ensureWasmOrtInjected()) {
6196
- disableLocalEmbeddingsAfterRuntimeFailure("the previously selected WASM runtime is unavailable");
6197
- throw new Error("onnxruntime-web failed to load");
6198
- }
6199
- try {
6200
- return { module: await importWasmTransformersForHost(), usesWasm: true };
6201
- } catch (wasmError) {
6202
- disableLocalEmbeddingsAfterRuntimeFailure("the previously selected WASM runtime failed to load");
6203
- throw wasmError;
6204
- }
6205
- }
6206
- const electron = resolvedRuntime === "electron";
6207
- if (electron) {
6208
- const wasInjected = wasmRuntimeInjected;
6209
- if (await ensureWasmOrtInjected()) {
6210
- if (!wasInjected) {
6211
- logForRuntime("[magic-context] Electron detected — using onnxruntime-web (WASM) for embeddings (bypasses onnxruntime-node native load)");
6212
- }
6213
- return { module: await importTransformersForRuntime(), usesWasm: true };
6214
- }
6215
- }
6216
- try {
6217
- return { module: await importTransformersForRuntime(), usesWasm: false };
6218
- } catch (nativeError) {
6219
- if (!isNativeRuntimeMissingError(nativeError) || electron) {
6220
- throw nativeError;
6221
- }
6222
- if (!await ensureWasmOrtInjected()) {
6223
- disableLocalEmbeddingsAfterRuntimeFailure(nativeError instanceof Error ? nativeError.message : String(nativeError));
6224
- throw nativeError;
6225
- }
6226
- localEmbeddingRuntimeMode = "wasm";
6227
- try {
6228
- const module = await importWasmTransformersForHost();
6229
- 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());
6230
- return { module, usesWasm: true };
6231
- } catch (wasmError) {
6232
- disableLocalEmbeddingsAfterRuntimeFailure(nativeError instanceof Error ? nativeError.message : String(nativeError));
6233
- throw new LocalEmbeddingFallbackError(nativeError, wasmError);
6234
- }
6235
- }
6236
- }
6237
- var DEFAULT_LOCAL_DTYPE = "fp32";
6238
- async function withQuietConsole(fn) {
6239
- const origWarn = console.warn;
6240
- const origError = console.error;
6241
- const redirect = (...args) => {
6242
- const message = args.map((a) => typeof a === "string" ? a : String(a)).join(" ");
6243
- log2(`[transformers] ${message}`);
6244
- };
6245
- console.warn = redirect;
6246
- console.error = redirect;
6247
- try {
6248
- return await fn();
6249
- } finally {
6250
- console.warn = origWarn;
6251
- console.error = origError;
6252
- }
6253
- }
6254
- function isNativeRuntimeMissingError(error) {
6255
- const message = error instanceof Error ? error.message : String(error ?? "");
6256
- const lower = message.toLowerCase();
6257
- const code = error?.code;
6258
- const name = error?.name;
6259
- if (code === "ERR_DLOPEN_FAILED" && lower.includes("onnxruntime")) {
6260
- return true;
6261
- }
6262
- if (lower.includes('could not load the "sharp" module'))
6263
- return true;
6264
- const mentionsNativeRuntime = lower.includes("onnxruntime-node") || lower.includes("onnxruntime_binding");
6265
- if (!mentionsNativeRuntime)
6266
- return false;
6267
- return code === "ERR_MODULE_NOT_FOUND" || name === "ResolveMessage" || lower.includes("cannot find package") || lower.includes("cannot find module") || lower.includes("err_module_not_found");
6268
- }
6269
- function isTransientLoadError(error) {
6270
- const message = error instanceof Error ? error.message : String(error ?? "");
6271
- if (!message)
6272
- return false;
6273
- const lower = message.toLowerCase();
6274
- 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");
6275
- }
6276
- function isArrayLikeNumber(value) {
6277
- if (typeof value !== "object" || value === null || !("length" in value)) {
6278
- return false;
6279
- }
6280
- const arr = value;
6281
- if (typeof arr.length !== "number") {
6282
- return false;
6283
- }
6284
- return arr.length === 0 || typeof arr[0] === "number";
6285
- }
6286
- function toFloat32Array(values) {
6287
- return values instanceof Float32Array ? new Float32Array(values) : Float32Array.from(Array.from(values));
6288
- }
6289
- function extractBatchEmbeddings(result, expectedCount) {
6290
- const { data } = result;
6291
- if (Array.isArray(data) && data.length === expectedCount && data.every((entry) => typeof entry !== "number" && isArrayLikeNumber(entry))) {
6292
- return data.map((entry) => toFloat32Array(entry));
6293
- }
6294
- if (!isArrayLikeNumber(data)) {
6295
- log2("[magic-context] embedding batch returned unexpected data shape");
6296
- return Array.from({ length: expectedCount }, () => null);
6297
- }
6298
- const flatData = toFloat32Array(data);
6299
- const dimension = result.dims?.at(-1) ?? flatData.length / expectedCount;
6300
- if (!Number.isInteger(dimension) || dimension <= 0 || flatData.length !== expectedCount * dimension) {
6301
- log2("[magic-context] embedding batch returned invalid dimensions");
6302
- return Array.from({ length: expectedCount }, () => null);
6303
- }
6304
- const embeddings = [];
6305
- for (let index = 0;index < expectedCount; index++) {
6306
- embeddings.push(flatData.slice(index * dimension, (index + 1) * dimension));
6307
- }
6308
- return embeddings;
6309
- }
6310
-
6311
- class LocalEmbeddingProvider {
6312
- modelId;
6313
- maxInputTokens;
6314
- memoryStatsId = nextLocalEmbeddingRuntimeId++;
6315
- model;
6316
- dtype;
6317
- runtimePreference;
6318
- pipeline = null;
6319
- workerClient;
6320
- workerLoaded = false;
6321
- initPromise = null;
6322
- lastFailureReason = null;
6323
- usesWasm = false;
6324
- inFlight = 0;
6325
- disposing = false;
6326
- disposePromise = null;
6327
- inFlightWaiters = [];
6328
- constructor(model = DEFAULT_LOCAL_EMBEDDING_MODEL, maxInputTokens = 512, dtype = DEFAULT_LOCAL_DTYPE, runtimePreference = "auto") {
6329
- this.model = model;
6330
- this.maxInputTokens = maxInputTokens;
6331
- this.dtype = dtype || DEFAULT_LOCAL_DTYPE;
6332
- this.runtimePreference = runtimePreference;
6333
- this.workerClient = !workerData?.magicContextEmbeddingWorker && !useInThreadTestRuntime ? (workerFactoryForTests ?? ((data) => new EmbeddingWorkerClient(data)))({
6334
- magicContextEmbeddingWorker: true,
6335
- modelCacheDir: modelCacheDirForRuntime(),
6336
- enforcePrivateStoragePermissions: shouldEnforcePrivateStoragePermissions(),
6337
- harness: getHarness2(),
6338
- model,
6339
- maxInputTokens,
6340
- dtype: this.dtype,
6341
- runtimePreference
6342
- }) : null;
6343
- this.modelId = getEmbeddingProviderIdentity({
6344
- provider: "local",
6345
- model,
6346
- local_runtime: runtimePreference,
6347
- ...dtype && dtype !== DEFAULT_LOCAL_DTYPE ? { local_dtype: dtype } : {}
6348
- });
6349
- }
6350
- async initialize() {
6351
- if (this.workerClient && !this.disposing) {
6352
- if (this.workerLoaded && this.workerClient.isRunning())
6353
- return true;
6354
- try {
6355
- const reply = await this.workerClient.request();
6356
- this.recordWorkerReply(reply);
6357
- return reply.loaded;
6358
- } catch (error) {
6359
- this.recordWorkerFailure(error);
6360
- return false;
6361
- }
6362
- }
6363
- if (this.disposing) {
6364
- return false;
6365
- }
6366
- if (this.pipeline) {
6367
- return true;
6368
- }
6369
- if (localEmbeddingRuntimeMode === "disabled") {
6370
- this.lastFailureReason = localEmbeddingProcessFailure ?? classifyLocalEmbeddingFailure(new Error("local embedding runtime is disabled"));
6371
- return false;
6372
- }
6373
- if (this.initPromise) {
6374
- await this.initPromise;
6375
- return this.pipeline !== null;
6376
- }
6377
- const memoryBeforeLoad = process.memoryUsage();
6378
- this.initPromise = (async () => {
6379
- try {
6380
- if (this.disposing) {
6381
- return;
6382
- }
6383
- const { module: transformersModule, usesWasm } = await loadTransformersForLocalEmbedding(this.runtimePreference);
6384
- this.usesWasm = usesWasm;
6385
- const env = transformersModule.env;
6386
- configureTransformersRemoteHost(env);
6387
- const LogLevel = transformersModule.LogLevel;
6388
- if (LogLevel && "ERROR" in LogLevel) {
6389
- env.logLevel = LogLevel.ERROR;
6390
- }
6391
- const host = localEmbeddingHostForRuntime();
6392
- if (usesWasm && host.hasNodeFilesystem && !host.isElectron && (env.useFS !== true || env.useFSCache !== true)) {
6393
- throw new LocalEmbeddingFsUnavailableError;
6394
- }
6395
- const modelCacheDir = modelCacheDirForRuntime();
6396
- try {
6397
- ensureStorageDirectorySync(modelCacheDir);
6398
- env.cacheDir = modelCacheDir;
6399
- } catch {
6400
- log2("[magic-context] could not create model cache dir, using library default");
6401
- }
6402
- const createPipeline = transformersModule.pipeline;
6403
- const lockPath = join(modelCacheDir, ".load.lock");
6404
- const releaseLock = await acquireModelLoadLock(lockPath);
6405
- const stopHeartbeat = startLockHeartbeat(lockPath);
6406
- try {
6407
- const MAX_ATTEMPTS = 3;
6408
- let lastError;
6409
- for (let attempt = 1;attempt <= MAX_ATTEMPTS; attempt++) {
6410
- try {
6411
- const pipeline = await withQuietConsole(() => createPipeline("feature-extraction", this.model, {
6412
- dtype: this.dtype,
6413
- ...usesWasm ? { device: "auto" } : {}
6414
- }));
6415
- if (this.disposing) {
6416
- await pipeline.dispose?.();
6417
- this.pipeline = null;
6418
- } else {
6419
- this.pipeline = pipeline;
6420
- }
6421
- lastError = undefined;
6422
- break;
6423
- } catch (error) {
6424
- lastError = error;
6425
- if (!isTransientLoadError(error) || attempt === MAX_ATTEMPTS) {
6426
- break;
6427
- }
6428
- const delayMs = 300 * attempt + Math.floor(Math.random() * 200);
6429
- log2(`[magic-context] embedding model load attempt ${attempt}/${MAX_ATTEMPTS} failed transiently, retrying in ${delayMs}ms`);
6430
- await new Promise((resolve) => setTimeout(resolve, delayMs));
6431
- }
6432
- }
6433
- if (this.pipeline) {
6434
- this.lastFailureReason = null;
6435
- localEmbeddingProcessFailure = null;
6436
- const memoryAfterLoad = process.memoryUsage();
6437
- loadedLocalEmbeddingRuntimes.set(this.memoryStatsId, {
6438
- model: this.model,
6439
- runtime: this.usesWasm ? "wasm" : "native",
6440
- rssDeltaAtLoad: memoryAfterLoad.rss - memoryBeforeLoad.rss,
6441
- externalDeltaAtLoad: memoryAfterLoad.external - memoryBeforeLoad.external,
6442
- arrayBuffersDeltaAtLoad: memoryAfterLoad.arrayBuffers - memoryBeforeLoad.arrayBuffers
6443
- });
6444
- log2(`[magic-context] embedding model loaded: ${this.model}`);
6445
- } else if (this.disposing) {
6446
- return;
6447
- } else {
6448
- throw lastError ?? new Error("unknown embedding load failure");
6449
- }
6450
- } finally {
6451
- stopHeartbeat();
6452
- await releaseLock();
6453
- }
6454
- } catch (error) {
6455
- const nativeError = error instanceof LocalEmbeddingFallbackError ? error.nativeError : undefined;
6456
- const failure = classifyLocalEmbeddingFailure(error, {
6457
- platform: process.platform,
6458
- arch: process.arch,
6459
- usesWasm: this.usesWasm,
6460
- nativeError
6461
- });
6462
- this.lastFailureReason = failure;
6463
- localEmbeddingProcessFailure = failure;
6464
- if (!localEmbeddingRuntimeIsDisabled() && isNativeRuntimeMissingError(error)) {
6465
- disableLocalEmbeddingsAfterRuntimeFailure(error instanceof Error ? error.message : String(error));
6466
- }
6467
- logForRuntime(`[magic-context] embedding model failed to load (${failure.class}: ${failure.reason}):`, error);
6468
- this.pipeline = null;
6469
- } finally {
6470
- this.initPromise = null;
6471
- }
6472
- })();
6473
- await this.initPromise;
6474
- return this.pipeline !== null;
6475
- }
6476
- waitForInFlightToDrain() {
6477
- if (this.inFlight === 0) {
6478
- return Promise.resolve();
6479
- }
6480
- return new Promise((resolve) => {
6481
- this.inFlightWaiters.push(resolve);
6482
- });
6483
- }
6484
- finishInFlight() {
6485
- this.inFlight = Math.max(0, this.inFlight - 1);
6486
- if (this.inFlight !== 0)
6487
- return;
6488
- const waiters = this.inFlightWaiters.splice(0);
6489
- for (const waiter of waiters) {
6490
- waiter();
6491
- }
6492
- }
6493
- async embed(text, signal, _purpose) {
6494
- if (this.workerClient)
6495
- return (await this.embedBatch([text], signal, _purpose))[0] ?? null;
6496
- if (signal?.aborted)
6497
- return null;
6498
- if (this.disposing)
6499
- return null;
6500
- this.inFlight += 1;
6501
- try {
6502
- if (!await this.initialize()) {
6503
- return null;
6504
- }
6505
- const pipeline = this.pipeline;
6506
- if (!pipeline) {
6507
- return null;
6508
- }
6509
- const result = await withQuietConsole(() => pipeline(text, {
6510
- pooling: "mean",
6511
- normalize: true
6512
- }));
6513
- const embedding = extractBatchEmbeddings(result, 1)[0] ?? null;
6514
- if (!embedding) {
6515
- this.lastFailureReason = classifyLocalEmbeddingFailure(new Error("local embedding pipeline returned no vector"), { usesWasm: this.usesWasm });
6516
- } else {
6517
- this.lastFailureReason = null;
6518
- }
6519
- return embedding;
6520
- } catch (error) {
6521
- const failure = classifyLocalEmbeddingFailure(error, { usesWasm: this.usesWasm });
6522
- this.lastFailureReason = failure;
6523
- logForRuntime(`[magic-context] embedding failed (${failure.class}: ${failure.reason}):`, error);
6524
- return null;
6525
- } finally {
6526
- this.finishInFlight();
6527
- }
6528
- }
6529
- async embedBatch(texts, signal, _purpose) {
6530
- if (this.workerClient) {
6531
- if (!texts.length)
6532
- return [];
6533
- if (signal?.aborted || this.disposing)
6534
- return texts.map(() => null);
6535
- try {
6536
- const reply = await this.workerClient.request(texts, signal);
6537
- this.recordWorkerReply(reply);
6538
- return reply.vectors ?? texts.map(() => null);
6539
- } catch (error) {
6540
- this.recordWorkerFailure(error);
6541
- return texts.map(() => null);
6542
- }
6543
- }
6544
- if (texts.length === 0) {
6545
- return [];
6546
- }
6547
- if (signal?.aborted) {
6548
- return Array.from({ length: texts.length }, () => null);
6549
- }
6550
- if (this.disposing) {
6551
- return Array.from({ length: texts.length }, () => null);
6552
- }
6553
- this.inFlight += 1;
6554
- try {
6555
- if (!await this.initialize()) {
6556
- return Array.from({ length: texts.length }, () => null);
6557
- }
6558
- const pipeline = this.pipeline;
6559
- if (!pipeline) {
6560
- return Array.from({ length: texts.length }, () => null);
6561
- }
6562
- const result = await withQuietConsole(() => pipeline(texts, {
6563
- pooling: "mean",
6564
- normalize: true
6565
- }));
6566
- const embeddings = extractBatchEmbeddings(result, texts.length);
6567
- if (embeddings.every((embedding) => embedding === null)) {
6568
- this.lastFailureReason = classifyLocalEmbeddingFailure(new Error("local embedding pipeline returned no vectors"), { usesWasm: this.usesWasm });
6569
- } else {
6570
- this.lastFailureReason = null;
6571
- }
6572
- return embeddings;
6573
- } catch (error) {
6574
- const failure = classifyLocalEmbeddingFailure(error, { usesWasm: this.usesWasm });
6575
- this.lastFailureReason = failure;
6576
- logForRuntime(`[magic-context] embedding batch failed (${failure.class}: ${failure.reason}):`, error);
6577
- return Array.from({ length: texts.length }, () => null);
6578
- } finally {
6579
- this.finishInFlight();
6580
- }
6581
- }
6582
- recordWorkerReply(reply) {
6583
- this.workerLoaded = reply.loaded;
6584
- this.lastFailureReason = reply.failure ?? null;
6585
- if (reply.loaded && reply.stats) {
6586
- loadedLocalEmbeddingRuntimes.set(this.memoryStatsId, {
6587
- model: this.model,
6588
- runtime: reply.stats.runtimes[0] ?? "wasm",
6589
- rssDeltaAtLoad: reply.stats.rssDeltaAtLoad,
6590
- externalDeltaAtLoad: reply.stats.externalDeltaAtLoad,
6591
- arrayBuffersDeltaAtLoad: reply.stats.arrayBuffersDeltaAtLoad
6592
- });
6593
- } else
6594
- loadedLocalEmbeddingRuntimes.delete(this.memoryStatsId);
6595
- }
6596
- recordWorkerFailure(error) {
6597
- loadedLocalEmbeddingRuntimes.delete(this.memoryStatsId);
6598
- this.workerLoaded = false;
6599
- this.lastFailureReason = classifyLocalEmbeddingFailure(error);
6600
- logForRuntime("[magic-context] embedding worker failed:", error);
6601
- }
6602
- async dispose() {
6603
- if (this.workerClient) {
6604
- this.disposing = true;
6605
- this.workerLoaded = false;
6606
- loadedLocalEmbeddingRuntimes.delete(this.memoryStatsId);
6607
- await this.workerClient.dispose();
6608
- return;
6609
- }
6610
- if (this.disposePromise) {
6611
- return this.disposePromise;
6612
- }
6613
- this.disposing = true;
6614
- this.disposePromise = (async () => {
6615
- if (this.initPromise) {
6616
- await this.initPromise;
6617
- }
6618
- await this.waitForInFlightToDrain();
6619
- const pipelineToDispose = this.pipeline;
6620
- this.pipeline = null;
6621
- this.initPromise = null;
6622
- loadedLocalEmbeddingRuntimes.delete(this.memoryStatsId);
6623
- if (!pipelineToDispose) {
6624
- return;
6625
- }
6626
- try {
6627
- await pipelineToDispose.dispose?.();
6628
- } catch (error) {
6629
- log2("[magic-context] embedding model dispose failed:", error);
6630
- }
6631
- })();
6632
- return this.disposePromise;
6633
- }
6634
- isLoaded() {
6635
- return this.workerClient ? this.workerLoaded && this.workerClient.isRunning() : this.pipeline !== null;
6636
- }
6637
- getLastFailureReason() {
6638
- return this.lastFailureReason;
6639
- }
6640
- }
6641
-
6642
- export { isValidLanguageCode, withContentLanguageDirective, buildPrimaryLanguageDirective, parseCron, nextOccurrence, nextDueAtMs, 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 };
5884
+ export { canonicalModelIdentity, piModelRefToCanonical, resolveModelRefForPi, modelRefLookupOrder, ompModelRefToCanonical, resolveModelRefForOmp, isValidLanguageCode, withContentLanguageDirective, buildPrimaryLanguageDirective, parseCron, nextOccurrence, nextDueAtMs, renderToolTemplate, 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 };