@zapier/kitcore 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -270,13 +270,39 @@ function buildRegistry({
270
270
  }
271
271
 
272
272
  // src/utils/build-hooks.ts
273
+ var isolated = /* @__PURE__ */ new WeakSet();
274
+ function isolate(observer) {
275
+ if (!observer) return void 0;
276
+ if (isolated.has(observer)) return observer;
277
+ const wrapped = (ctx) => {
278
+ try {
279
+ observer(ctx);
280
+ } catch (error) {
281
+ console.error(
282
+ "[core] A method-lifecycle observer threw and was ignored. Observers are fire-and-forget and must not throw.",
283
+ error
284
+ );
285
+ }
286
+ };
287
+ isolated.add(wrapped);
288
+ return wrapped;
289
+ }
273
290
  function composeVoid(existing, added) {
291
+ const wrappedExisting = isolate(existing);
292
+ const wrappedAdded = isolate(added);
293
+ if (!wrappedExisting) return wrappedAdded;
294
+ if (!wrappedAdded) return wrappedExisting;
295
+ const composed = (ctx) => {
296
+ wrappedExisting(ctx);
297
+ wrappedAdded(ctx);
298
+ };
299
+ isolated.add(composed);
300
+ return composed;
301
+ }
302
+ function composeAnnotators(existing, added) {
274
303
  if (!existing) return added;
275
304
  if (!added) return existing;
276
- return (ctx) => {
277
- existing(ctx);
278
- added(ctx);
279
- };
305
+ return (ctx) => ({ ...existing(ctx), ...added(ctx) });
280
306
  }
281
307
  function buildHooks(existing, added) {
282
308
  const result = {};
@@ -284,6 +310,8 @@ function buildHooks(existing, added) {
284
310
  if (start2) result.onMethodStart = start2;
285
311
  const end = composeVoid(existing.onMethodEnd, added.onMethodEnd);
286
312
  if (end) result.onMethodEnd = end;
313
+ const annotator = composeAnnotators(existing.annotator, added.annotator);
314
+ if (annotator) result.annotator = annotator;
287
315
  return result;
288
316
  }
289
317
 
@@ -756,11 +784,14 @@ function generateCallId() {
756
784
  }
757
785
  return null;
758
786
  }
759
- function rootCallContext() {
787
+ function rootCallContext({
788
+ callOrigin = "surface"
789
+ } = {}) {
760
790
  return {
761
791
  callId: generateCallId(),
762
792
  depth: 0,
763
793
  annotations: {},
794
+ callOrigin,
764
795
  [CALL_CONTEXT_BRAND]: true
765
796
  };
766
797
  }
@@ -769,6 +800,7 @@ function childCallContext(parent) {
769
800
  callId: parent.callId,
770
801
  depth: parent.depth + 1,
771
802
  annotations: {},
803
+ callOrigin: parent.callOrigin,
772
804
  [CALL_CONTEXT_BRAND]: true
773
805
  };
774
806
  }
@@ -794,6 +826,28 @@ var INTERNAL_CALL = Symbol("kitcore.internalCall");
794
826
  function resolveCallContext(secondArg) {
795
827
  return isCallContext(secondArg) ? secondArg : rootCallContext();
796
828
  }
829
+ var hookAnnotatorReentrancy = 0;
830
+ function applyAnnotations({
831
+ context,
832
+ methodName,
833
+ input,
834
+ hookAnnotator,
835
+ methodAnnotator
836
+ }) {
837
+ if (hookAnnotator && !isInsideObserver() && context.depth === 0 && context.callOrigin !== "internal" && hookAnnotatorReentrancy === 0) {
838
+ hookAnnotatorReentrancy++;
839
+ try {
840
+ Object.assign(context.annotations, hookAnnotator({ methodName, input }));
841
+ } catch {
842
+ } finally {
843
+ hookAnnotatorReentrancy--;
844
+ }
845
+ }
846
+ try {
847
+ Object.assign(context.annotations, methodAnnotator?.(input));
848
+ } catch {
849
+ }
850
+ }
797
851
  function signalDeprecation(context, methodName, getDeprecation) {
798
852
  if (isInsideObserver()) return;
799
853
  const deprecation = getDeprecation?.();
@@ -819,7 +873,7 @@ function normalizeError(error, adaptError) {
819
873
  );
820
874
  }
821
875
  function createFunction(coreFn, options) {
822
- const { sdk, schema, name, getDeprecation } = options;
876
+ const { sdk, schema, name, annotator, getDeprecation } = options;
823
877
  const functionName = name || coreFn.name;
824
878
  const namedFunctions = {
825
879
  [functionName]: async function(callOptions) {
@@ -833,14 +887,26 @@ function createFunction(coreFn, options) {
833
887
  const normalizedOptions = callOptions ?? {};
834
888
  const args = [normalizedOptions];
835
889
  const depth = Math.max(context.depth, getCurrentDepth());
836
- const hooks = isInsideObserver() ? void 0 : sdk.context.hooks;
890
+ const insideObserver = isInsideObserver();
891
+ const hooks = insideObserver ? void 0 : sdk.context.hooks;
837
892
  const adaptError = resolveCoreOptions(sdk.context)?.adaptError;
838
- hooks?.onMethodStart?.({
893
+ applyAnnotations({
894
+ context,
895
+ methodName: functionName,
896
+ input: normalizedOptions,
897
+ hookAnnotator: hooks?.annotator,
898
+ methodAnnotator: annotator
899
+ });
900
+ const hookBase = {
839
901
  methodName: functionName,
840
902
  args,
841
903
  isPaginated: false,
842
- depth
843
- });
904
+ depth,
905
+ callId: context.callId,
906
+ callOrigin: context.callOrigin,
907
+ annotations: context.annotations
908
+ };
909
+ hooks?.onMethodStart?.({ ...hookBase });
844
910
  try {
845
911
  let result;
846
912
  if (schema) {
@@ -862,20 +928,14 @@ function createFunction(coreFn, options) {
862
928
  result = await coreFn(normalizedOptions, context);
863
929
  }
864
930
  hooks?.onMethodEnd?.({
865
- methodName: functionName,
866
- args,
867
- isPaginated: false,
868
- depth,
931
+ ...hookBase,
869
932
  durationMs: Date.now() - startTime
870
933
  });
871
934
  return result;
872
935
  } catch (error) {
873
936
  const normalizedError = normalizeError(error, adaptError);
874
937
  hooks?.onMethodEnd?.({
875
- methodName: functionName,
876
- args,
877
- isPaginated: false,
878
- depth,
938
+ ...hookBase,
879
939
  durationMs: Date.now() - startTime,
880
940
  error: normalizedError
881
941
  });
@@ -887,7 +947,7 @@ function createFunction(coreFn, options) {
887
947
  return namedFunctions[functionName];
888
948
  }
889
949
  function createRawFunction(coreFn, options) {
890
- const { sdk, name, schema, positional, getDeprecation } = options;
950
+ const { sdk, name, schema, positional, annotator, getDeprecation } = options;
891
951
  return function(rawInput) {
892
952
  const internal = arguments[1];
893
953
  const context = resolveCallContext(internal);
@@ -897,23 +957,32 @@ function createRawFunction(coreFn, options) {
897
957
  return runInMethodScope(() => {
898
958
  const startTime = Date.now();
899
959
  const depth = Math.max(context.depth, getCurrentDepth());
900
- const hooks = isInsideObserver() ? void 0 : sdk.context.hooks;
960
+ const insideObserver = isInsideObserver();
961
+ const hooks = insideObserver ? void 0 : sdk.context.hooks;
901
962
  const adaptError = resolveCoreOptions(sdk.context)?.adaptError;
902
963
  const input = schema ? rawInput ?? {} : rawInput;
964
+ applyAnnotations({
965
+ context,
966
+ methodName: name,
967
+ input,
968
+ hookAnnotator: hooks?.annotator,
969
+ methodAnnotator: annotator
970
+ });
903
971
  const record = input;
904
972
  const args = positional ? positional.filter((key) => record?.[key] !== void 0).map((key) => record?.[key]) : [input];
905
- hooks?.onMethodStart?.({
973
+ const hookBase = {
906
974
  methodName: name,
907
975
  args,
908
976
  isPaginated: false,
909
- depth
910
- });
977
+ depth,
978
+ callId: context.callId,
979
+ callOrigin: context.callOrigin,
980
+ annotations: context.annotations
981
+ };
982
+ hooks?.onMethodStart?.({ ...hookBase });
911
983
  const fireEnd = (error) => {
912
984
  hooks?.onMethodEnd?.({
913
- methodName: name,
914
- args,
915
- isPaginated: false,
916
- depth,
985
+ ...hookBase,
917
986
  durationMs: Date.now() - startTime,
918
987
  ...error ? { error } : {}
919
988
  });
@@ -980,7 +1049,15 @@ function createPageFunction(coreFn, {
980
1049
  return namedFunctions[functionName];
981
1050
  }
982
1051
  function createPaginatedFunction(coreFn, options) {
983
- const { sdk, schema, name, defaultPageSize, adaptPage, getDeprecation } = options;
1052
+ const {
1053
+ sdk,
1054
+ schema,
1055
+ name,
1056
+ defaultPageSize,
1057
+ adaptPage,
1058
+ annotator,
1059
+ getDeprecation
1060
+ } = options;
984
1061
  const pageFunction = createPageFunction(coreFn, { sdk, adaptPage });
985
1062
  const functionName = name || coreFn.name;
986
1063
  const namedFunctions = {
@@ -995,14 +1072,26 @@ function createPaginatedFunction(coreFn, options) {
995
1072
  const normalizedOptions = callOptions ?? {};
996
1073
  const args = [normalizedOptions];
997
1074
  const depth = Math.max(context.depth, getCurrentDepth());
998
- const hooks = isInsideObserver() ? void 0 : sdk.context.hooks;
1075
+ const insideObserver = isInsideObserver();
1076
+ const hooks = insideObserver ? void 0 : sdk.context.hooks;
999
1077
  const adaptError = resolveCoreOptions(sdk.context)?.adaptError;
1000
- hooks?.onMethodStart?.({
1078
+ applyAnnotations({
1079
+ context,
1080
+ methodName: functionName,
1081
+ input: normalizedOptions,
1082
+ hookAnnotator: hooks?.annotator,
1083
+ methodAnnotator: annotator
1084
+ });
1085
+ const hookBase = {
1001
1086
  methodName: functionName,
1002
1087
  args,
1003
1088
  isPaginated: true,
1004
- depth
1005
- });
1089
+ depth,
1090
+ callId: context.callId,
1091
+ callOrigin: context.callOrigin,
1092
+ annotations: context.annotations
1093
+ };
1094
+ hooks?.onMethodStart?.({ ...hookBase });
1006
1095
  try {
1007
1096
  const validatedOptions = {
1008
1097
  ...normalizedOptions,
@@ -1025,24 +1114,21 @@ function createPaginatedFunction(coreFn, options) {
1025
1114
  return result.value;
1026
1115
  });
1027
1116
  if (hooks?.onMethodEnd) {
1028
- firstPagePromise.then(() => {
1029
- hooks.onMethodEnd({
1030
- methodName: functionName,
1031
- args,
1032
- isPaginated: true,
1033
- depth,
1034
- durationMs: Date.now() - startTime
1035
- });
1036
- }).catch((error) => {
1037
- hooks.onMethodEnd({
1038
- methodName: functionName,
1039
- args,
1040
- isPaginated: true,
1041
- depth,
1042
- durationMs: Date.now() - startTime,
1043
- error: error instanceof Error ? error : new Error(String(error))
1044
- });
1045
- });
1117
+ firstPagePromise.then(
1118
+ () => {
1119
+ hooks.onMethodEnd({
1120
+ ...hookBase,
1121
+ durationMs: Date.now() - startTime
1122
+ });
1123
+ },
1124
+ (error) => {
1125
+ hooks.onMethodEnd({
1126
+ ...hookBase,
1127
+ durationMs: Date.now() - startTime,
1128
+ error: error instanceof Error ? error : new Error(String(error))
1129
+ });
1130
+ }
1131
+ );
1046
1132
  }
1047
1133
  const pageStream = async function* () {
1048
1134
  yield await firstPagePromise;
@@ -1076,10 +1162,7 @@ function createPaginatedFunction(coreFn, options) {
1076
1162
  } catch (error) {
1077
1163
  const normalizedError = normalizeError(error, adaptError);
1078
1164
  hooks?.onMethodEnd?.({
1079
- methodName: functionName,
1080
- args,
1081
- isPaginated: true,
1082
- depth,
1165
+ ...hookBase,
1083
1166
  durationMs: Date.now() - startTime,
1084
1167
  error: normalizedError
1085
1168
  });
@@ -1504,6 +1587,7 @@ function defineMethod(config) {
1504
1587
  meta: collectLeafMeta(config),
1505
1588
  resolvers: config.resolvers,
1506
1589
  formatter: config.formatter,
1590
+ annotator: config.annotator,
1507
1591
  output: config.output,
1508
1592
  positional: config.positional,
1509
1593
  setup: config.setup,
@@ -1663,7 +1747,8 @@ function defineHook(config) {
1663
1747
  setup: config.setup,
1664
1748
  dispose: config.dispose,
1665
1749
  wrap: config.wrap,
1666
- observe: config.observe
1750
+ observe: config.observe,
1751
+ annotator: config.annotator
1667
1752
  };
1668
1753
  }
1669
1754
  function declarePlugin(config) {
@@ -2102,7 +2187,14 @@ function collectPlugins(root, materialized = /* @__PURE__ */ new Set(), configur
2102
2187
  }
2103
2188
  return byId;
2104
2189
  }
2105
- function bindValue(target, key, entry, callType = "surface", ctx) {
2190
+ function bindValue({
2191
+ target,
2192
+ key,
2193
+ entry,
2194
+ bindMode = "surface",
2195
+ ctx,
2196
+ frameworkOrigin = false
2197
+ }) {
2106
2198
  if (entry.pluginType === "property" && entry.getValue) {
2107
2199
  Object.defineProperty(target, key, {
2108
2200
  get: entry.getValue,
@@ -2110,7 +2202,7 @@ function bindValue(target, key, entry, callType = "surface", ctx) {
2110
2202
  configurable: true
2111
2203
  });
2112
2204
  } else {
2113
- const value = callType === "internal" && entry.pluginType === "method" ? entry.bindInternal?.(ctx) ?? entry.internalValue ?? entry.value : entry.value;
2205
+ const value = bindMode === "internal" && entry.pluginType === "method" ? entry.bindInternal?.({ ctx, frameworkOrigin }) ?? entry.internalValue ?? entry.value : entry.value;
2114
2206
  Object.defineProperty(target, key, {
2115
2207
  value,
2116
2208
  writable: true,
@@ -2128,7 +2220,12 @@ function buildSurface(context, ...maps) {
2128
2220
  sdk[CONTEXT] = context;
2129
2221
  return sdk;
2130
2222
  }
2131
- function buildImports(plugins, importBindings, ctx) {
2223
+ function buildImports({
2224
+ plugins,
2225
+ importBindings,
2226
+ ctx,
2227
+ frameworkOrigin = false
2228
+ }) {
2132
2229
  const imports = {};
2133
2230
  for (const { binding, id, optional } of importBindings) {
2134
2231
  const entry = plugins[id];
@@ -2141,10 +2238,31 @@ function buildImports(plugins, importBindings, ctx) {
2141
2238
  });
2142
2239
  continue;
2143
2240
  }
2144
- bindValue(imports, binding, entry, "internal", ctx);
2241
+ bindValue({
2242
+ target: imports,
2243
+ key: binding,
2244
+ entry,
2245
+ bindMode: "internal",
2246
+ ctx,
2247
+ frameworkOrigin
2248
+ });
2145
2249
  }
2146
2250
  return imports;
2147
2251
  }
2252
+ function bindInternalTwin({
2253
+ ctx,
2254
+ frameworkOrigin,
2255
+ withContext,
2256
+ internalValue
2257
+ }) {
2258
+ if (ctx) {
2259
+ return (...args) => withContext(childCallContext(ctx))(...args);
2260
+ }
2261
+ if (frameworkOrigin) {
2262
+ return (...args) => withContext(rootCallContext({ callOrigin: "internal" }))(...args);
2263
+ }
2264
+ return internalValue;
2265
+ }
2148
2266
  function mirrorLegacyRootKeys(context, rootKeys, meta) {
2149
2267
  const exports2 = {};
2150
2268
  for (const [name, value] of Object.entries(rootKeys)) {
@@ -2208,7 +2326,11 @@ function bindResolver(resolver, plugins) {
2208
2326
  case "info":
2209
2327
  return { type: "info", text: resolver.text };
2210
2328
  case "object": {
2211
- const imports = buildImports(plugins, resolver.importBindings);
2329
+ const imports = buildImports({
2330
+ plugins,
2331
+ importBindings: resolver.importBindings,
2332
+ frameworkOrigin: true
2333
+ });
2212
2334
  const bound = {
2213
2335
  type: "object",
2214
2336
  requireParameters: resolver.requireParameters
@@ -2249,7 +2371,11 @@ function bindResolver(resolver, plugins) {
2249
2371
  return bound;
2250
2372
  }
2251
2373
  case "dynamic": {
2252
- const imports = buildImports(plugins, resolver.importBindings);
2374
+ const imports = buildImports({
2375
+ plugins,
2376
+ importBindings: resolver.importBindings,
2377
+ frameworkOrigin: true
2378
+ });
2253
2379
  const {
2254
2380
  getContext: getContext2,
2255
2381
  listItems,
@@ -2304,7 +2430,11 @@ function bindDefinitions(definitions, plugins) {
2304
2430
  return out;
2305
2431
  }
2306
2432
  function bindFormatter(formatter, plugins) {
2307
- const imports = buildImports(plugins, formatter.importBindings);
2433
+ const imports = buildImports({
2434
+ plugins,
2435
+ importBindings: formatter.importBindings,
2436
+ frameworkOrigin: true
2437
+ });
2308
2438
  const bound = { format: formatter.format };
2309
2439
  const { getContext: getContext2 } = formatter;
2310
2440
  if (getContext2)
@@ -2392,17 +2522,32 @@ function buildMethodEntries(descriptors, context, states) {
2392
2522
  // Replaced below; never called.
2393
2523
  value: () => void 0
2394
2524
  };
2395
- const callRun = (input, ctx) => descriptor.run({
2396
- imports: buildImports(plugins, descriptor.importBindings, ctx),
2397
- state: states.get(id),
2398
- input
2399
- });
2525
+ const callRun = (input, ctx) => {
2526
+ const callContext = ctx ?? rootCallContext();
2527
+ return descriptor.run({
2528
+ imports: buildImports({
2529
+ plugins,
2530
+ importBindings: descriptor.importBindings,
2531
+ ctx: callContext
2532
+ }),
2533
+ state: states.get(id),
2534
+ input,
2535
+ callContext,
2536
+ annotate: (metadata) => {
2537
+ Object.assign(callContext.annotations, metadata);
2538
+ }
2539
+ });
2540
+ };
2400
2541
  const fold = (coreFn) => (input, ctx) => {
2401
2542
  let next = (i) => coreFn(i, ctx);
2402
2543
  for (const wrap of entry.chain) {
2403
2544
  const inner = next;
2404
2545
  next = (i) => wrap.run({
2405
- imports: buildImports(plugins, wrap.owner.importBindings, ctx),
2546
+ imports: buildImports({
2547
+ plugins,
2548
+ importBindings: wrap.owner.importBindings,
2549
+ ctx
2550
+ }),
2406
2551
  next: inner,
2407
2552
  input: i,
2408
2553
  // Overwritten by the chain item's own closure with the owning
@@ -2413,6 +2558,8 @@ function buildMethodEntries(descriptors, context, states) {
2413
2558
  return next(input);
2414
2559
  };
2415
2560
  const sdk = { context };
2561
+ const methodAnnotator = descriptor.annotator;
2562
+ const boundAnnotator = methodAnnotator ? (input) => methodAnnotator({ input }) : void 0;
2416
2563
  if (out.type === "list") {
2417
2564
  entry.value = createPaginatedFunction(
2418
2565
  fold(callRun),
@@ -2422,6 +2569,7 @@ function buildMethodEntries(descriptors, context, states) {
2422
2569
  name: descriptor.name,
2423
2570
  defaultPageSize: out.defaultPageSize,
2424
2571
  adaptPage: out.adaptPage,
2572
+ annotator: boundAnnotator,
2425
2573
  getDeprecation: () => entry.meta?.deprecation
2426
2574
  }
2427
2575
  );
@@ -2433,6 +2581,7 @@ function buildMethodEntries(descriptors, context, states) {
2433
2581
  sdk,
2434
2582
  schema: descriptor.inputSchema,
2435
2583
  name: descriptor.name,
2584
+ annotator: boundAnnotator,
2436
2585
  getDeprecation: () => entry.meta?.deprecation
2437
2586
  }
2438
2587
  );
@@ -2444,6 +2593,7 @@ function buildMethodEntries(descriptors, context, states) {
2444
2593
  name: descriptor.name,
2445
2594
  schema: descriptor.skipInputValidation ? void 0 : descriptor.inputSchema,
2446
2595
  positional: descriptor.positional,
2596
+ annotator: boundAnnotator,
2447
2597
  // The boundary reads the deprecation LIVE off the entry, so a
2448
2598
  // deprecation merged after build (defineMethodOverride, addPlugin)
2449
2599
  // fires too.
@@ -2464,12 +2614,22 @@ function buildMethodEntries(descriptors, context, states) {
2464
2614
  const internalValue = (...args) => canonicalValue(pack(args), INTERNAL_CALL);
2465
2615
  entry.value = (...args) => canonicalValue(pack(args));
2466
2616
  entry.internalValue = internalValue;
2467
- entry.bindInternal = (ctx) => ctx ? (...args) => canonicalValue(pack(args), childCallContext(ctx)) : internalValue;
2617
+ entry.bindInternal = (opts) => bindInternalTwin({
2618
+ ...opts,
2619
+ withContext: (context2) => {
2620
+ return (...args) => canonicalValue(pack(args), context2);
2621
+ },
2622
+ internalValue
2623
+ });
2468
2624
  entry.positional = names;
2469
2625
  } else {
2470
2626
  const internalValue = (input) => canonicalValue(input, INTERNAL_CALL);
2471
2627
  entry.internalValue = internalValue;
2472
- entry.bindInternal = (ctx) => ctx ? (input) => canonicalValue(input, childCallContext(ctx)) : internalValue;
2628
+ entry.bindInternal = (opts) => bindInternalTwin({
2629
+ ...opts,
2630
+ withContext: (context2) => (input) => canonicalValue(input, context2),
2631
+ internalValue
2632
+ });
2473
2633
  }
2474
2634
  plugins[id] = entry;
2475
2635
  }
@@ -2495,8 +2655,14 @@ function buildEagerArtifacts(descriptors, context, states) {
2495
2655
  if (!dispose) return;
2496
2656
  context.disposers?.push({
2497
2657
  id,
2658
+ // Teardown is framework-internal: an SDK method a `dispose` calls runs
2659
+ // on an internal-origin root (dropped from telemetry).
2498
2660
  dispose: (input) => dispose({
2499
- imports: buildImports(plugins, descriptor.importBindings),
2661
+ imports: buildImports({
2662
+ plugins,
2663
+ importBindings: descriptor.importBindings,
2664
+ frameworkOrigin: true
2665
+ }),
2500
2666
  state: states.get(id),
2501
2667
  input
2502
2668
  })
@@ -2506,7 +2672,10 @@ function buildEagerArtifacts(descriptors, context, states) {
2506
2672
  states.set(
2507
2673
  id,
2508
2674
  descriptor.setup ? descriptor.setup({
2509
- imports: buildImports(plugins, descriptor.importBindings)
2675
+ imports: buildImports({
2676
+ plugins,
2677
+ importBindings: descriptor.importBindings
2678
+ })
2510
2679
  }) : void 0
2511
2680
  );
2512
2681
  recordDisposer();
@@ -2518,14 +2687,20 @@ function buildEagerArtifacts(descriptors, context, states) {
2518
2687
  states.set(
2519
2688
  id,
2520
2689
  descriptor.setup ? descriptor.setup({
2521
- imports: buildImports(plugins, descriptor.importBindings)
2690
+ imports: buildImports({
2691
+ plugins,
2692
+ importBindings: descriptor.importBindings
2693
+ })
2522
2694
  }) : void 0
2523
2695
  );
2524
2696
  } else {
2525
2697
  states.set(
2526
2698
  id,
2527
2699
  descriptor.setup ? descriptor.setup({
2528
- imports: buildImports(plugins, descriptor.importBindings)
2700
+ imports: buildImports({
2701
+ plugins,
2702
+ importBindings: descriptor.importBindings
2703
+ })
2529
2704
  }) : void 0
2530
2705
  );
2531
2706
  if (descriptor.privileged) {
@@ -2543,7 +2718,7 @@ function buildEagerArtifacts(descriptors, context, states) {
2543
2718
  pluginType: "property",
2544
2719
  name: descriptor.name,
2545
2720
  getValue: () => get({
2546
- imports: buildImports(plugins, importBindings),
2721
+ imports: buildImports({ plugins, importBindings }),
2547
2722
  state: states.get(id)
2548
2723
  }),
2549
2724
  meta: descriptor.meta,
@@ -2579,7 +2754,7 @@ function resolvePlugin(sdk, ref) {
2579
2754
  return entry.getValue();
2580
2755
  }
2581
2756
  if (entry.pluginType === "method" && entry.internalValue) {
2582
- return entry.internalValue;
2757
+ return entry.bindInternal?.({ frameworkOrigin: true }) ?? entry.internalValue;
2583
2758
  }
2584
2759
  return entry.value;
2585
2760
  }
@@ -2614,7 +2789,7 @@ function resolveAggregates(descriptors, context) {
2614
2789
  if (descriptor.pluginType !== "aggregate") continue;
2615
2790
  const exports2 = {};
2616
2791
  for (const [binding, child] of Object.entries(descriptor.exports)) {
2617
- bindValue(exports2, binding, plugins[child.id]);
2792
+ bindValue({ target: exports2, key: binding, entry: plugins[child.id] });
2618
2793
  }
2619
2794
  plugins[id] = { pluginType: "aggregate", name: descriptor.name, exports: exports2 };
2620
2795
  }
@@ -2657,23 +2832,39 @@ function assembleHooks(descriptors, context, states) {
2657
2832
  const plugins = context.plugins;
2658
2833
  for (const id of topoOrder(descriptors)) {
2659
2834
  const descriptor = descriptors.get(id);
2660
- if (!descriptor || descriptor.pluginType !== "hook" || !descriptor.observe) {
2835
+ if (!descriptor || descriptor.pluginType !== "hook" || !descriptor.observe && !descriptor.annotator) {
2661
2836
  continue;
2662
2837
  }
2663
- const { observe } = descriptor;
2664
- const imports = buildImports(plugins, descriptor.importBindings);
2838
+ const { observe, annotator } = descriptor;
2665
2839
  const state = states.get(id);
2666
2840
  const contributed = {};
2667
- if (observe.onMethodStart) {
2668
- const onStart = observe.onMethodStart;
2669
- contributed.onMethodStart = (input) => {
2670
- runIsolatedObserver(() => onStart({ imports, input, state }));
2671
- };
2841
+ if (observe?.onMethodStart || observe?.onMethodEnd) {
2842
+ const imports = buildImports({
2843
+ plugins,
2844
+ importBindings: descriptor.importBindings,
2845
+ frameworkOrigin: true
2846
+ });
2847
+ if (observe.onMethodStart) {
2848
+ const onStart = observe.onMethodStart;
2849
+ contributed.onMethodStart = (input) => {
2850
+ runIsolatedObserver(() => onStart({ imports, input, state }));
2851
+ };
2852
+ }
2853
+ if (observe.onMethodEnd) {
2854
+ const onEnd = observe.onMethodEnd;
2855
+ contributed.onMethodEnd = (input) => {
2856
+ runIsolatedObserver(() => onEnd({ imports, input, state }));
2857
+ };
2858
+ }
2672
2859
  }
2673
- if (observe.onMethodEnd) {
2674
- const onEnd = observe.onMethodEnd;
2675
- contributed.onMethodEnd = (input) => {
2676
- runIsolatedObserver(() => onEnd({ imports, input, state }));
2860
+ if (annotator) {
2861
+ const annotatorFn = annotator;
2862
+ contributed.annotator = ({ methodName, input }) => {
2863
+ try {
2864
+ return annotatorFn({ methodName, input, state });
2865
+ } catch {
2866
+ return {};
2867
+ }
2677
2868
  };
2678
2869
  }
2679
2870
  context.hooks = buildHooks(context.hooks, contributed);
@@ -2707,7 +2898,11 @@ function createSdk(root, options) {
2707
2898
  pluginSurface = plugins2[plugin.id].exports;
2708
2899
  } else {
2709
2900
  pluginSurface = {};
2710
- bindValue(pluginSurface, plugin.name, plugins2[plugin.id]);
2901
+ bindValue({
2902
+ target: pluginSurface,
2903
+ key: plugin.name,
2904
+ entry: plugins2[plugin.id]
2905
+ });
2711
2906
  }
2712
2907
  for (const key of Object.keys(legacyExports)) context.surface[key] = key;
2713
2908
  if (plugin.pluginType === "aggregate") {
@@ -2724,7 +2919,7 @@ function createSdk(root, options) {
2724
2919
  if (root.pluginType === "method" || root.pluginType === "property") {
2725
2920
  context.surface[root.name] = root.id;
2726
2921
  const sdk = buildSurface(context);
2727
- bindValue(sdk, root.name, plugins[root.id]);
2922
+ bindValue({ target: sdk, key: root.name, entry: plugins[root.id] });
2728
2923
  return sdk;
2729
2924
  }
2730
2925
  if (root.pluginType === "aggregate")
@@ -2756,7 +2951,7 @@ function addModelPlugin(sdk, plugin, options = {}) {
2756
2951
  context.surface[binding] = child.id;
2757
2952
  }
2758
2953
  } else {
2759
- bindValue(sdk, plugin.name, entry);
2954
+ bindValue({ target: sdk, key: plugin.name, entry });
2760
2955
  context.surface[plugin.name] = plugin.id;
2761
2956
  }
2762
2957
  }