@zapier/kitcore 0.10.1 → 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
@@ -299,12 +299,19 @@ function composeVoid(existing, added) {
299
299
  isolated.add(composed);
300
300
  return composed;
301
301
  }
302
+ function composeAnnotators(existing, added) {
303
+ if (!existing) return added;
304
+ if (!added) return existing;
305
+ return (ctx) => ({ ...existing(ctx), ...added(ctx) });
306
+ }
302
307
  function buildHooks(existing, added) {
303
308
  const result = {};
304
309
  const start2 = composeVoid(existing.onMethodStart, added.onMethodStart);
305
310
  if (start2) result.onMethodStart = start2;
306
311
  const end = composeVoid(existing.onMethodEnd, added.onMethodEnd);
307
312
  if (end) result.onMethodEnd = end;
313
+ const annotator = composeAnnotators(existing.annotator, added.annotator);
314
+ if (annotator) result.annotator = annotator;
308
315
  return result;
309
316
  }
310
317
 
@@ -777,11 +784,14 @@ function generateCallId() {
777
784
  }
778
785
  return null;
779
786
  }
780
- function rootCallContext() {
787
+ function rootCallContext({
788
+ callOrigin = "surface"
789
+ } = {}) {
781
790
  return {
782
791
  callId: generateCallId(),
783
792
  depth: 0,
784
793
  annotations: {},
794
+ callOrigin,
785
795
  [CALL_CONTEXT_BRAND]: true
786
796
  };
787
797
  }
@@ -790,6 +800,7 @@ function childCallContext(parent) {
790
800
  callId: parent.callId,
791
801
  depth: parent.depth + 1,
792
802
  annotations: {},
803
+ callOrigin: parent.callOrigin,
793
804
  [CALL_CONTEXT_BRAND]: true
794
805
  };
795
806
  }
@@ -815,6 +826,28 @@ var INTERNAL_CALL = Symbol("kitcore.internalCall");
815
826
  function resolveCallContext(secondArg) {
816
827
  return isCallContext(secondArg) ? secondArg : rootCallContext();
817
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
+ }
818
851
  function signalDeprecation(context, methodName, getDeprecation) {
819
852
  if (isInsideObserver()) return;
820
853
  const deprecation = getDeprecation?.();
@@ -840,7 +873,7 @@ function normalizeError(error, adaptError) {
840
873
  );
841
874
  }
842
875
  function createFunction(coreFn, options) {
843
- const { sdk, schema, name, getDeprecation } = options;
876
+ const { sdk, schema, name, annotator, getDeprecation } = options;
844
877
  const functionName = name || coreFn.name;
845
878
  const namedFunctions = {
846
879
  [functionName]: async function(callOptions) {
@@ -854,14 +887,26 @@ function createFunction(coreFn, options) {
854
887
  const normalizedOptions = callOptions ?? {};
855
888
  const args = [normalizedOptions];
856
889
  const depth = Math.max(context.depth, getCurrentDepth());
857
- const hooks = isInsideObserver() ? void 0 : sdk.context.hooks;
890
+ const insideObserver = isInsideObserver();
891
+ const hooks = insideObserver ? void 0 : sdk.context.hooks;
858
892
  const adaptError = resolveCoreOptions(sdk.context)?.adaptError;
859
- hooks?.onMethodStart?.({
893
+ applyAnnotations({
894
+ context,
895
+ methodName: functionName,
896
+ input: normalizedOptions,
897
+ hookAnnotator: hooks?.annotator,
898
+ methodAnnotator: annotator
899
+ });
900
+ const hookBase = {
860
901
  methodName: functionName,
861
902
  args,
862
903
  isPaginated: false,
863
- depth
864
- });
904
+ depth,
905
+ callId: context.callId,
906
+ callOrigin: context.callOrigin,
907
+ annotations: context.annotations
908
+ };
909
+ hooks?.onMethodStart?.({ ...hookBase });
865
910
  try {
866
911
  let result;
867
912
  if (schema) {
@@ -883,20 +928,14 @@ function createFunction(coreFn, options) {
883
928
  result = await coreFn(normalizedOptions, context);
884
929
  }
885
930
  hooks?.onMethodEnd?.({
886
- methodName: functionName,
887
- args,
888
- isPaginated: false,
889
- depth,
931
+ ...hookBase,
890
932
  durationMs: Date.now() - startTime
891
933
  });
892
934
  return result;
893
935
  } catch (error) {
894
936
  const normalizedError = normalizeError(error, adaptError);
895
937
  hooks?.onMethodEnd?.({
896
- methodName: functionName,
897
- args,
898
- isPaginated: false,
899
- depth,
938
+ ...hookBase,
900
939
  durationMs: Date.now() - startTime,
901
940
  error: normalizedError
902
941
  });
@@ -908,7 +947,7 @@ function createFunction(coreFn, options) {
908
947
  return namedFunctions[functionName];
909
948
  }
910
949
  function createRawFunction(coreFn, options) {
911
- const { sdk, name, schema, positional, getDeprecation } = options;
950
+ const { sdk, name, schema, positional, annotator, getDeprecation } = options;
912
951
  return function(rawInput) {
913
952
  const internal = arguments[1];
914
953
  const context = resolveCallContext(internal);
@@ -918,23 +957,32 @@ function createRawFunction(coreFn, options) {
918
957
  return runInMethodScope(() => {
919
958
  const startTime = Date.now();
920
959
  const depth = Math.max(context.depth, getCurrentDepth());
921
- const hooks = isInsideObserver() ? void 0 : sdk.context.hooks;
960
+ const insideObserver = isInsideObserver();
961
+ const hooks = insideObserver ? void 0 : sdk.context.hooks;
922
962
  const adaptError = resolveCoreOptions(sdk.context)?.adaptError;
923
963
  const input = schema ? rawInput ?? {} : rawInput;
964
+ applyAnnotations({
965
+ context,
966
+ methodName: name,
967
+ input,
968
+ hookAnnotator: hooks?.annotator,
969
+ methodAnnotator: annotator
970
+ });
924
971
  const record = input;
925
972
  const args = positional ? positional.filter((key) => record?.[key] !== void 0).map((key) => record?.[key]) : [input];
926
- hooks?.onMethodStart?.({
973
+ const hookBase = {
927
974
  methodName: name,
928
975
  args,
929
976
  isPaginated: false,
930
- depth
931
- });
977
+ depth,
978
+ callId: context.callId,
979
+ callOrigin: context.callOrigin,
980
+ annotations: context.annotations
981
+ };
982
+ hooks?.onMethodStart?.({ ...hookBase });
932
983
  const fireEnd = (error) => {
933
984
  hooks?.onMethodEnd?.({
934
- methodName: name,
935
- args,
936
- isPaginated: false,
937
- depth,
985
+ ...hookBase,
938
986
  durationMs: Date.now() - startTime,
939
987
  ...error ? { error } : {}
940
988
  });
@@ -1001,7 +1049,15 @@ function createPageFunction(coreFn, {
1001
1049
  return namedFunctions[functionName];
1002
1050
  }
1003
1051
  function createPaginatedFunction(coreFn, options) {
1004
- 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;
1005
1061
  const pageFunction = createPageFunction(coreFn, { sdk, adaptPage });
1006
1062
  const functionName = name || coreFn.name;
1007
1063
  const namedFunctions = {
@@ -1016,14 +1072,26 @@ function createPaginatedFunction(coreFn, options) {
1016
1072
  const normalizedOptions = callOptions ?? {};
1017
1073
  const args = [normalizedOptions];
1018
1074
  const depth = Math.max(context.depth, getCurrentDepth());
1019
- const hooks = isInsideObserver() ? void 0 : sdk.context.hooks;
1075
+ const insideObserver = isInsideObserver();
1076
+ const hooks = insideObserver ? void 0 : sdk.context.hooks;
1020
1077
  const adaptError = resolveCoreOptions(sdk.context)?.adaptError;
1021
- hooks?.onMethodStart?.({
1078
+ applyAnnotations({
1079
+ context,
1080
+ methodName: functionName,
1081
+ input: normalizedOptions,
1082
+ hookAnnotator: hooks?.annotator,
1083
+ methodAnnotator: annotator
1084
+ });
1085
+ const hookBase = {
1022
1086
  methodName: functionName,
1023
1087
  args,
1024
1088
  isPaginated: true,
1025
- depth
1026
- });
1089
+ depth,
1090
+ callId: context.callId,
1091
+ callOrigin: context.callOrigin,
1092
+ annotations: context.annotations
1093
+ };
1094
+ hooks?.onMethodStart?.({ ...hookBase });
1027
1095
  try {
1028
1096
  const validatedOptions = {
1029
1097
  ...normalizedOptions,
@@ -1049,19 +1117,13 @@ function createPaginatedFunction(coreFn, options) {
1049
1117
  firstPagePromise.then(
1050
1118
  () => {
1051
1119
  hooks.onMethodEnd({
1052
- methodName: functionName,
1053
- args,
1054
- isPaginated: true,
1055
- depth,
1120
+ ...hookBase,
1056
1121
  durationMs: Date.now() - startTime
1057
1122
  });
1058
1123
  },
1059
1124
  (error) => {
1060
1125
  hooks.onMethodEnd({
1061
- methodName: functionName,
1062
- args,
1063
- isPaginated: true,
1064
- depth,
1126
+ ...hookBase,
1065
1127
  durationMs: Date.now() - startTime,
1066
1128
  error: error instanceof Error ? error : new Error(String(error))
1067
1129
  });
@@ -1100,10 +1162,7 @@ function createPaginatedFunction(coreFn, options) {
1100
1162
  } catch (error) {
1101
1163
  const normalizedError = normalizeError(error, adaptError);
1102
1164
  hooks?.onMethodEnd?.({
1103
- methodName: functionName,
1104
- args,
1105
- isPaginated: true,
1106
- depth,
1165
+ ...hookBase,
1107
1166
  durationMs: Date.now() - startTime,
1108
1167
  error: normalizedError
1109
1168
  });
@@ -1528,6 +1587,7 @@ function defineMethod(config) {
1528
1587
  meta: collectLeafMeta(config),
1529
1588
  resolvers: config.resolvers,
1530
1589
  formatter: config.formatter,
1590
+ annotator: config.annotator,
1531
1591
  output: config.output,
1532
1592
  positional: config.positional,
1533
1593
  setup: config.setup,
@@ -1687,7 +1747,8 @@ function defineHook(config) {
1687
1747
  setup: config.setup,
1688
1748
  dispose: config.dispose,
1689
1749
  wrap: config.wrap,
1690
- observe: config.observe
1750
+ observe: config.observe,
1751
+ annotator: config.annotator
1691
1752
  };
1692
1753
  }
1693
1754
  function declarePlugin(config) {
@@ -2126,7 +2187,14 @@ function collectPlugins(root, materialized = /* @__PURE__ */ new Set(), configur
2126
2187
  }
2127
2188
  return byId;
2128
2189
  }
2129
- 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
+ }) {
2130
2198
  if (entry.pluginType === "property" && entry.getValue) {
2131
2199
  Object.defineProperty(target, key, {
2132
2200
  get: entry.getValue,
@@ -2134,7 +2202,7 @@ function bindValue(target, key, entry, callType = "surface", ctx) {
2134
2202
  configurable: true
2135
2203
  });
2136
2204
  } else {
2137
- 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;
2138
2206
  Object.defineProperty(target, key, {
2139
2207
  value,
2140
2208
  writable: true,
@@ -2152,7 +2220,12 @@ function buildSurface(context, ...maps) {
2152
2220
  sdk[CONTEXT] = context;
2153
2221
  return sdk;
2154
2222
  }
2155
- function buildImports(plugins, importBindings, ctx) {
2223
+ function buildImports({
2224
+ plugins,
2225
+ importBindings,
2226
+ ctx,
2227
+ frameworkOrigin = false
2228
+ }) {
2156
2229
  const imports = {};
2157
2230
  for (const { binding, id, optional } of importBindings) {
2158
2231
  const entry = plugins[id];
@@ -2165,10 +2238,31 @@ function buildImports(plugins, importBindings, ctx) {
2165
2238
  });
2166
2239
  continue;
2167
2240
  }
2168
- bindValue(imports, binding, entry, "internal", ctx);
2241
+ bindValue({
2242
+ target: imports,
2243
+ key: binding,
2244
+ entry,
2245
+ bindMode: "internal",
2246
+ ctx,
2247
+ frameworkOrigin
2248
+ });
2169
2249
  }
2170
2250
  return imports;
2171
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
+ }
2172
2266
  function mirrorLegacyRootKeys(context, rootKeys, meta) {
2173
2267
  const exports2 = {};
2174
2268
  for (const [name, value] of Object.entries(rootKeys)) {
@@ -2232,7 +2326,11 @@ function bindResolver(resolver, plugins) {
2232
2326
  case "info":
2233
2327
  return { type: "info", text: resolver.text };
2234
2328
  case "object": {
2235
- const imports = buildImports(plugins, resolver.importBindings);
2329
+ const imports = buildImports({
2330
+ plugins,
2331
+ importBindings: resolver.importBindings,
2332
+ frameworkOrigin: true
2333
+ });
2236
2334
  const bound = {
2237
2335
  type: "object",
2238
2336
  requireParameters: resolver.requireParameters
@@ -2273,7 +2371,11 @@ function bindResolver(resolver, plugins) {
2273
2371
  return bound;
2274
2372
  }
2275
2373
  case "dynamic": {
2276
- const imports = buildImports(plugins, resolver.importBindings);
2374
+ const imports = buildImports({
2375
+ plugins,
2376
+ importBindings: resolver.importBindings,
2377
+ frameworkOrigin: true
2378
+ });
2277
2379
  const {
2278
2380
  getContext: getContext2,
2279
2381
  listItems,
@@ -2328,7 +2430,11 @@ function bindDefinitions(definitions, plugins) {
2328
2430
  return out;
2329
2431
  }
2330
2432
  function bindFormatter(formatter, plugins) {
2331
- const imports = buildImports(plugins, formatter.importBindings);
2433
+ const imports = buildImports({
2434
+ plugins,
2435
+ importBindings: formatter.importBindings,
2436
+ frameworkOrigin: true
2437
+ });
2332
2438
  const bound = { format: formatter.format };
2333
2439
  const { getContext: getContext2 } = formatter;
2334
2440
  if (getContext2)
@@ -2416,17 +2522,32 @@ function buildMethodEntries(descriptors, context, states) {
2416
2522
  // Replaced below; never called.
2417
2523
  value: () => void 0
2418
2524
  };
2419
- const callRun = (input, ctx) => descriptor.run({
2420
- imports: buildImports(plugins, descriptor.importBindings, ctx),
2421
- state: states.get(id),
2422
- input
2423
- });
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
+ };
2424
2541
  const fold = (coreFn) => (input, ctx) => {
2425
2542
  let next = (i) => coreFn(i, ctx);
2426
2543
  for (const wrap of entry.chain) {
2427
2544
  const inner = next;
2428
2545
  next = (i) => wrap.run({
2429
- imports: buildImports(plugins, wrap.owner.importBindings, ctx),
2546
+ imports: buildImports({
2547
+ plugins,
2548
+ importBindings: wrap.owner.importBindings,
2549
+ ctx
2550
+ }),
2430
2551
  next: inner,
2431
2552
  input: i,
2432
2553
  // Overwritten by the chain item's own closure with the owning
@@ -2437,6 +2558,8 @@ function buildMethodEntries(descriptors, context, states) {
2437
2558
  return next(input);
2438
2559
  };
2439
2560
  const sdk = { context };
2561
+ const methodAnnotator = descriptor.annotator;
2562
+ const boundAnnotator = methodAnnotator ? (input) => methodAnnotator({ input }) : void 0;
2440
2563
  if (out.type === "list") {
2441
2564
  entry.value = createPaginatedFunction(
2442
2565
  fold(callRun),
@@ -2446,6 +2569,7 @@ function buildMethodEntries(descriptors, context, states) {
2446
2569
  name: descriptor.name,
2447
2570
  defaultPageSize: out.defaultPageSize,
2448
2571
  adaptPage: out.adaptPage,
2572
+ annotator: boundAnnotator,
2449
2573
  getDeprecation: () => entry.meta?.deprecation
2450
2574
  }
2451
2575
  );
@@ -2457,6 +2581,7 @@ function buildMethodEntries(descriptors, context, states) {
2457
2581
  sdk,
2458
2582
  schema: descriptor.inputSchema,
2459
2583
  name: descriptor.name,
2584
+ annotator: boundAnnotator,
2460
2585
  getDeprecation: () => entry.meta?.deprecation
2461
2586
  }
2462
2587
  );
@@ -2468,6 +2593,7 @@ function buildMethodEntries(descriptors, context, states) {
2468
2593
  name: descriptor.name,
2469
2594
  schema: descriptor.skipInputValidation ? void 0 : descriptor.inputSchema,
2470
2595
  positional: descriptor.positional,
2596
+ annotator: boundAnnotator,
2471
2597
  // The boundary reads the deprecation LIVE off the entry, so a
2472
2598
  // deprecation merged after build (defineMethodOverride, addPlugin)
2473
2599
  // fires too.
@@ -2488,12 +2614,22 @@ function buildMethodEntries(descriptors, context, states) {
2488
2614
  const internalValue = (...args) => canonicalValue(pack(args), INTERNAL_CALL);
2489
2615
  entry.value = (...args) => canonicalValue(pack(args));
2490
2616
  entry.internalValue = internalValue;
2491
- 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
+ });
2492
2624
  entry.positional = names;
2493
2625
  } else {
2494
2626
  const internalValue = (input) => canonicalValue(input, INTERNAL_CALL);
2495
2627
  entry.internalValue = internalValue;
2496
- 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
+ });
2497
2633
  }
2498
2634
  plugins[id] = entry;
2499
2635
  }
@@ -2519,8 +2655,14 @@ function buildEagerArtifacts(descriptors, context, states) {
2519
2655
  if (!dispose) return;
2520
2656
  context.disposers?.push({
2521
2657
  id,
2658
+ // Teardown is framework-internal: an SDK method a `dispose` calls runs
2659
+ // on an internal-origin root (dropped from telemetry).
2522
2660
  dispose: (input) => dispose({
2523
- imports: buildImports(plugins, descriptor.importBindings),
2661
+ imports: buildImports({
2662
+ plugins,
2663
+ importBindings: descriptor.importBindings,
2664
+ frameworkOrigin: true
2665
+ }),
2524
2666
  state: states.get(id),
2525
2667
  input
2526
2668
  })
@@ -2530,7 +2672,10 @@ function buildEagerArtifacts(descriptors, context, states) {
2530
2672
  states.set(
2531
2673
  id,
2532
2674
  descriptor.setup ? descriptor.setup({
2533
- imports: buildImports(plugins, descriptor.importBindings)
2675
+ imports: buildImports({
2676
+ plugins,
2677
+ importBindings: descriptor.importBindings
2678
+ })
2534
2679
  }) : void 0
2535
2680
  );
2536
2681
  recordDisposer();
@@ -2542,14 +2687,20 @@ function buildEagerArtifacts(descriptors, context, states) {
2542
2687
  states.set(
2543
2688
  id,
2544
2689
  descriptor.setup ? descriptor.setup({
2545
- imports: buildImports(plugins, descriptor.importBindings)
2690
+ imports: buildImports({
2691
+ plugins,
2692
+ importBindings: descriptor.importBindings
2693
+ })
2546
2694
  }) : void 0
2547
2695
  );
2548
2696
  } else {
2549
2697
  states.set(
2550
2698
  id,
2551
2699
  descriptor.setup ? descriptor.setup({
2552
- imports: buildImports(plugins, descriptor.importBindings)
2700
+ imports: buildImports({
2701
+ plugins,
2702
+ importBindings: descriptor.importBindings
2703
+ })
2553
2704
  }) : void 0
2554
2705
  );
2555
2706
  if (descriptor.privileged) {
@@ -2567,7 +2718,7 @@ function buildEagerArtifacts(descriptors, context, states) {
2567
2718
  pluginType: "property",
2568
2719
  name: descriptor.name,
2569
2720
  getValue: () => get({
2570
- imports: buildImports(plugins, importBindings),
2721
+ imports: buildImports({ plugins, importBindings }),
2571
2722
  state: states.get(id)
2572
2723
  }),
2573
2724
  meta: descriptor.meta,
@@ -2603,7 +2754,7 @@ function resolvePlugin(sdk, ref) {
2603
2754
  return entry.getValue();
2604
2755
  }
2605
2756
  if (entry.pluginType === "method" && entry.internalValue) {
2606
- return entry.internalValue;
2757
+ return entry.bindInternal?.({ frameworkOrigin: true }) ?? entry.internalValue;
2607
2758
  }
2608
2759
  return entry.value;
2609
2760
  }
@@ -2638,7 +2789,7 @@ function resolveAggregates(descriptors, context) {
2638
2789
  if (descriptor.pluginType !== "aggregate") continue;
2639
2790
  const exports2 = {};
2640
2791
  for (const [binding, child] of Object.entries(descriptor.exports)) {
2641
- bindValue(exports2, binding, plugins[child.id]);
2792
+ bindValue({ target: exports2, key: binding, entry: plugins[child.id] });
2642
2793
  }
2643
2794
  plugins[id] = { pluginType: "aggregate", name: descriptor.name, exports: exports2 };
2644
2795
  }
@@ -2681,23 +2832,39 @@ function assembleHooks(descriptors, context, states) {
2681
2832
  const plugins = context.plugins;
2682
2833
  for (const id of topoOrder(descriptors)) {
2683
2834
  const descriptor = descriptors.get(id);
2684
- if (!descriptor || descriptor.pluginType !== "hook" || !descriptor.observe) {
2835
+ if (!descriptor || descriptor.pluginType !== "hook" || !descriptor.observe && !descriptor.annotator) {
2685
2836
  continue;
2686
2837
  }
2687
- const { observe } = descriptor;
2688
- const imports = buildImports(plugins, descriptor.importBindings);
2838
+ const { observe, annotator } = descriptor;
2689
2839
  const state = states.get(id);
2690
2840
  const contributed = {};
2691
- if (observe.onMethodStart) {
2692
- const onStart = observe.onMethodStart;
2693
- contributed.onMethodStart = (input) => {
2694
- runIsolatedObserver(() => onStart({ imports, input, state }));
2695
- };
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
+ }
2696
2859
  }
2697
- if (observe.onMethodEnd) {
2698
- const onEnd = observe.onMethodEnd;
2699
- contributed.onMethodEnd = (input) => {
2700
- 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
+ }
2701
2868
  };
2702
2869
  }
2703
2870
  context.hooks = buildHooks(context.hooks, contributed);
@@ -2731,7 +2898,11 @@ function createSdk(root, options) {
2731
2898
  pluginSurface = plugins2[plugin.id].exports;
2732
2899
  } else {
2733
2900
  pluginSurface = {};
2734
- bindValue(pluginSurface, plugin.name, plugins2[plugin.id]);
2901
+ bindValue({
2902
+ target: pluginSurface,
2903
+ key: plugin.name,
2904
+ entry: plugins2[plugin.id]
2905
+ });
2735
2906
  }
2736
2907
  for (const key of Object.keys(legacyExports)) context.surface[key] = key;
2737
2908
  if (plugin.pluginType === "aggregate") {
@@ -2748,7 +2919,7 @@ function createSdk(root, options) {
2748
2919
  if (root.pluginType === "method" || root.pluginType === "property") {
2749
2920
  context.surface[root.name] = root.id;
2750
2921
  const sdk = buildSurface(context);
2751
- bindValue(sdk, root.name, plugins[root.id]);
2922
+ bindValue({ target: sdk, key: root.name, entry: plugins[root.id] });
2752
2923
  return sdk;
2753
2924
  }
2754
2925
  if (root.pluginType === "aggregate")
@@ -2780,7 +2951,7 @@ function addModelPlugin(sdk, plugin, options = {}) {
2780
2951
  context.surface[binding] = child.id;
2781
2952
  }
2782
2953
  } else {
2783
- bindValue(sdk, plugin.name, entry);
2954
+ bindValue({ target: sdk, key: plugin.name, entry });
2784
2955
  context.surface[plugin.name] = plugin.id;
2785
2956
  }
2786
2957
  }