@mastra/server 1.18.0-alpha.2 → 1.18.0-alpha.4

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 (164) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/_types/@internal_core/dist/index.d.ts +521 -172
  3. package/dist/{chunk-NCCK5NVX.cjs → chunk-2HOVE4YF.cjs} +4 -4
  4. package/dist/{chunk-NCCK5NVX.cjs.map → chunk-2HOVE4YF.cjs.map} +1 -1
  5. package/dist/{chunk-E56FRMZY.js → chunk-3PYG4YYL.js} +3 -3
  6. package/dist/{chunk-E56FRMZY.js.map → chunk-3PYG4YYL.js.map} +1 -1
  7. package/dist/{chunk-YKS5HCGB.js → chunk-3XXKCBEU.js} +4 -4
  8. package/dist/{chunk-YKS5HCGB.js.map → chunk-3XXKCBEU.js.map} +1 -1
  9. package/dist/{chunk-FGZC4JP7.js → chunk-42OVSPQK.js} +575 -34
  10. package/dist/chunk-42OVSPQK.js.map +1 -0
  11. package/dist/{chunk-ULLR3RN3.cjs → chunk-42UIDILO.cjs} +4 -4
  12. package/dist/{chunk-ULLR3RN3.cjs.map → chunk-42UIDILO.cjs.map} +1 -1
  13. package/dist/{chunk-NOBDUHIG.cjs → chunk-4LOHS62D.cjs} +3 -3
  14. package/dist/{chunk-NOBDUHIG.cjs.map → chunk-4LOHS62D.cjs.map} +1 -1
  15. package/dist/{chunk-WIQDQTKG.cjs → chunk-4NTRDFFE.cjs} +6 -6
  16. package/dist/{chunk-WIQDQTKG.cjs.map → chunk-4NTRDFFE.cjs.map} +1 -1
  17. package/dist/{chunk-JL6JURL5.js → chunk-6M5HZQQV.js} +4 -4
  18. package/dist/{chunk-JL6JURL5.js.map → chunk-6M5HZQQV.js.map} +1 -1
  19. package/dist/{chunk-NTZOZIKF.cjs → chunk-7EOHGXMP.cjs} +603 -62
  20. package/dist/chunk-7EOHGXMP.cjs.map +1 -0
  21. package/dist/{chunk-4NRXB7VI.js → chunk-AUSQGJ52.js} +3 -3
  22. package/dist/{chunk-4NRXB7VI.js.map → chunk-AUSQGJ52.js.map} +1 -1
  23. package/dist/{chunk-G2ZZKKQI.js → chunk-B5DA5QVZ.js} +4 -4
  24. package/dist/{chunk-G2ZZKKQI.js.map → chunk-B5DA5QVZ.js.map} +1 -1
  25. package/dist/{chunk-AHOZXQZ5.js → chunk-BX372Y6T.js} +100 -38
  26. package/dist/chunk-BX372Y6T.js.map +1 -0
  27. package/dist/{chunk-6H7FQ4CY.cjs → chunk-CHQVMEZ3.cjs} +7 -4
  28. package/dist/chunk-CHQVMEZ3.cjs.map +1 -0
  29. package/dist/{chunk-4Q2QV27C.js → chunk-CMTOQQZD.js} +7 -4
  30. package/dist/chunk-CMTOQQZD.js.map +1 -0
  31. package/dist/{chunk-SCHPGBMK.js → chunk-DTJJFVTS.js} +5 -5
  32. package/dist/{chunk-SCHPGBMK.js.map → chunk-DTJJFVTS.js.map} +1 -1
  33. package/dist/{chunk-CL24HN6K.cjs → chunk-FQHTXKF6.cjs} +21 -21
  34. package/dist/{chunk-CL24HN6K.cjs.map → chunk-FQHTXKF6.cjs.map} +1 -1
  35. package/dist/{chunk-K7S4XDX6.js → chunk-GIRVVQTS.js} +3 -3
  36. package/dist/{chunk-K7S4XDX6.js.map → chunk-GIRVVQTS.js.map} +1 -1
  37. package/dist/{chunk-ZKXIAID6.js → chunk-HEJ53V64.js} +4 -4
  38. package/dist/{chunk-ZKXIAID6.js.map → chunk-HEJ53V64.js.map} +1 -1
  39. package/dist/{chunk-DJQT32SV.cjs → chunk-HUAXEKGI.cjs} +353 -105
  40. package/dist/chunk-HUAXEKGI.cjs.map +1 -0
  41. package/dist/{chunk-ATZ7V3XS.js → chunk-IFSINB5S.js} +3 -3
  42. package/dist/{chunk-ATZ7V3XS.js.map → chunk-IFSINB5S.js.map} +1 -1
  43. package/dist/{chunk-Z36FXNDQ.cjs → chunk-IOCKHUAA.cjs} +72 -68
  44. package/dist/chunk-IOCKHUAA.cjs.map +1 -0
  45. package/dist/{chunk-HBMIUVV4.cjs → chunk-JD5ZGRBN.cjs} +3 -3
  46. package/dist/{chunk-HBMIUVV4.cjs.map → chunk-JD5ZGRBN.cjs.map} +1 -1
  47. package/dist/{chunk-WUCJRGTK.cjs → chunk-LCA2MLVR.cjs} +3 -3
  48. package/dist/{chunk-WUCJRGTK.cjs.map → chunk-LCA2MLVR.cjs.map} +1 -1
  49. package/dist/{chunk-M6AQTASN.js → chunk-LDIPV66L.js} +3 -3
  50. package/dist/{chunk-M6AQTASN.js.map → chunk-LDIPV66L.js.map} +1 -1
  51. package/dist/{chunk-UFK5THVP.cjs → chunk-N4PIAZEA.cjs} +4 -4
  52. package/dist/{chunk-UFK5THVP.cjs.map → chunk-N4PIAZEA.cjs.map} +1 -1
  53. package/dist/{chunk-HI5EDX3F.js → chunk-NCHR6DGR.js} +3 -3
  54. package/dist/{chunk-HI5EDX3F.js.map → chunk-NCHR6DGR.js.map} +1 -1
  55. package/dist/{chunk-JH576GTI.cjs → chunk-NEB3TPME.cjs} +3 -3
  56. package/dist/{chunk-JH576GTI.cjs.map → chunk-NEB3TPME.cjs.map} +1 -1
  57. package/dist/{chunk-FB5NRM2M.js → chunk-NS5TM6YM.js} +3 -3
  58. package/dist/{chunk-FB5NRM2M.js.map → chunk-NS5TM6YM.js.map} +1 -1
  59. package/dist/{chunk-TYYGY4H4.cjs → chunk-OFI7WQSK.cjs} +4 -4
  60. package/dist/{chunk-TYYGY4H4.cjs.map → chunk-OFI7WQSK.cjs.map} +1 -1
  61. package/dist/{chunk-YEYWCSMW.cjs → chunk-PGPTFBZ3.cjs} +7 -7
  62. package/dist/{chunk-YEYWCSMW.cjs.map → chunk-PGPTFBZ3.cjs.map} +1 -1
  63. package/dist/{chunk-QJZOWORS.js → chunk-PV7IUA6N.js} +3 -3
  64. package/dist/{chunk-QJZOWORS.js.map → chunk-PV7IUA6N.js.map} +1 -1
  65. package/dist/{chunk-YV7FX2SA.cjs → chunk-S5PRNKVT.cjs} +3 -3
  66. package/dist/{chunk-YV7FX2SA.cjs.map → chunk-S5PRNKVT.cjs.map} +1 -1
  67. package/dist/{chunk-6MYRF6QR.js → chunk-SECEXHY4.js} +12 -8
  68. package/dist/chunk-SECEXHY4.js.map +1 -0
  69. package/dist/{chunk-MH2WUZWZ.js → chunk-SEPFGGZQ.js} +4 -4
  70. package/dist/{chunk-MH2WUZWZ.js.map → chunk-SEPFGGZQ.js.map} +1 -1
  71. package/dist/{chunk-4G5KVXN6.cjs → chunk-SIPQHHIE.cjs} +7 -7
  72. package/dist/{chunk-4G5KVXN6.cjs.map → chunk-SIPQHHIE.cjs.map} +1 -1
  73. package/dist/{chunk-3W54ZNYP.cjs → chunk-SUZOB46P.cjs} +4 -4
  74. package/dist/{chunk-3W54ZNYP.cjs.map → chunk-SUZOB46P.cjs.map} +1 -1
  75. package/dist/{chunk-PTTPRMT3.cjs → chunk-TEAYHLYJ.cjs} +100 -38
  76. package/dist/chunk-TEAYHLYJ.cjs.map +1 -0
  77. package/dist/{chunk-MKW43AJ3.js → chunk-TWILU7Q5.js} +4 -4
  78. package/dist/{chunk-MKW43AJ3.js.map → chunk-TWILU7Q5.js.map} +1 -1
  79. package/dist/{chunk-A5J2TY2W.cjs → chunk-U4GMLJ6B.cjs} +3 -3
  80. package/dist/{chunk-A5J2TY2W.cjs.map → chunk-U4GMLJ6B.cjs.map} +1 -1
  81. package/dist/{chunk-3BRCE7JC.cjs → chunk-VDAG64VF.cjs} +5 -5
  82. package/dist/{chunk-3BRCE7JC.cjs.map → chunk-VDAG64VF.cjs.map} +1 -1
  83. package/dist/{chunk-K6RNBFGH.js → chunk-VOCUA2P2.js} +4 -4
  84. package/dist/{chunk-K6RNBFGH.js.map → chunk-VOCUA2P2.js.map} +1 -1
  85. package/dist/{chunk-PVUR75AM.js → chunk-YUTITKH2.js} +353 -105
  86. package/dist/chunk-YUTITKH2.js.map +1 -0
  87. package/dist/{chunk-FPEX6KOJ.cjs → chunk-ZEQ3ZL2T.cjs} +4 -4
  88. package/dist/{chunk-FPEX6KOJ.cjs.map → chunk-ZEQ3ZL2T.cjs.map} +1 -1
  89. package/dist/{chunk-4D7KSU4X.js → chunk-ZL4KCUCN.js} +4 -4
  90. package/dist/{chunk-4D7KSU4X.js.map → chunk-ZL4KCUCN.js.map} +1 -1
  91. package/dist/docs/SKILL.md +1 -1
  92. package/dist/docs/assets/SOURCE_MAP.json +1 -1
  93. package/dist/{observational-memory-UGDENJPE-NVMIXNI4.js → observational-memory-SN7GKMHZ-IWVBFBS6.js} +3 -3
  94. package/dist/{observational-memory-UGDENJPE-NVMIXNI4.js.map → observational-memory-SN7GKMHZ-IWVBFBS6.js.map} +1 -1
  95. package/dist/{observational-memory-UGDENJPE-DM3C7ZXI.cjs → observational-memory-SN7GKMHZ-WOK4TGDH.cjs} +26 -26
  96. package/dist/{observational-memory-UGDENJPE-DM3C7ZXI.cjs.map → observational-memory-SN7GKMHZ-WOK4TGDH.cjs.map} +1 -1
  97. package/dist/server/handlers/a2a.cjs +9 -9
  98. package/dist/server/handlers/a2a.js +1 -1
  99. package/dist/server/handlers/agent-builder.cjs +16 -16
  100. package/dist/server/handlers/agent-builder.js +1 -1
  101. package/dist/server/handlers/agent-versions.cjs +8 -8
  102. package/dist/server/handlers/agent-versions.js +1 -1
  103. package/dist/server/handlers/agents.cjs +33 -33
  104. package/dist/server/handlers/agents.js +1 -1
  105. package/dist/server/handlers/datasets.cjs +26 -26
  106. package/dist/server/handlers/datasets.d.ts +12 -0
  107. package/dist/server/handlers/datasets.d.ts.map +1 -1
  108. package/dist/server/handlers/datasets.js +1 -1
  109. package/dist/server/handlers/logs.cjs +4 -4
  110. package/dist/server/handlers/logs.js +1 -1
  111. package/dist/server/handlers/mcp-client-versions.cjs +8 -8
  112. package/dist/server/handlers/mcp-client-versions.js +1 -1
  113. package/dist/server/handlers/observability-new-endpoints.cjs +19 -19
  114. package/dist/server/handlers/observability-new-endpoints.d.ts +596 -224
  115. package/dist/server/handlers/observability-new-endpoints.d.ts.map +1 -1
  116. package/dist/server/handlers/observability-new-endpoints.js +1 -1
  117. package/dist/server/handlers/observability.cjs +24 -24
  118. package/dist/server/handlers/observability.d.ts +4 -4
  119. package/dist/server/handlers/observability.js +2 -2
  120. package/dist/server/handlers/prompt-block-versions.cjs +8 -8
  121. package/dist/server/handlers/prompt-block-versions.js +1 -1
  122. package/dist/server/handlers/scorer-versions.cjs +8 -8
  123. package/dist/server/handlers/scorer-versions.js +1 -1
  124. package/dist/server/handlers/scores.cjs +7 -7
  125. package/dist/server/handlers/scores.js +1 -1
  126. package/dist/server/handlers/stored-agents.cjs +7 -7
  127. package/dist/server/handlers/stored-agents.js +1 -1
  128. package/dist/server/handlers/stored-mcp-clients.cjs +6 -6
  129. package/dist/server/handlers/stored-mcp-clients.js +1 -1
  130. package/dist/server/handlers/stored-prompt-blocks.cjs +6 -6
  131. package/dist/server/handlers/stored-prompt-blocks.js +1 -1
  132. package/dist/server/handlers/stored-scorers.cjs +6 -6
  133. package/dist/server/handlers/stored-scorers.js +1 -1
  134. package/dist/server/handlers/stored-workspaces.cjs +6 -6
  135. package/dist/server/handlers/stored-workspaces.js +1 -1
  136. package/dist/server/handlers/tools.cjs +6 -6
  137. package/dist/server/handlers/tools.js +1 -1
  138. package/dist/server/handlers/voice.cjs +8 -8
  139. package/dist/server/handlers/voice.js +1 -1
  140. package/dist/server/handlers/workflows.cjs +24 -24
  141. package/dist/server/handlers/workflows.js +1 -1
  142. package/dist/server/handlers.cjs +30 -30
  143. package/dist/server/handlers.js +11 -11
  144. package/dist/server/schemas/datasets.d.ts +4 -0
  145. package/dist/server/schemas/datasets.d.ts.map +1 -1
  146. package/dist/server/schemas/index.cjs +419 -419
  147. package/dist/server/schemas/index.js +8 -8
  148. package/dist/server/server-adapter/index.cjs +205 -205
  149. package/dist/server/server-adapter/index.js +23 -23
  150. package/dist/server/server-adapter/routes/datasets.d.ts +12 -0
  151. package/dist/server/server-adapter/routes/datasets.d.ts.map +1 -1
  152. package/dist/server/server-adapter/routes/observability.d.ts +302 -116
  153. package/dist/server/server-adapter/routes/observability.d.ts.map +1 -1
  154. package/package.json +3 -3
  155. package/dist/chunk-4Q2QV27C.js.map +0 -1
  156. package/dist/chunk-6H7FQ4CY.cjs.map +0 -1
  157. package/dist/chunk-6MYRF6QR.js.map +0 -1
  158. package/dist/chunk-AHOZXQZ5.js.map +0 -1
  159. package/dist/chunk-DJQT32SV.cjs.map +0 -1
  160. package/dist/chunk-FGZC4JP7.js.map +0 -1
  161. package/dist/chunk-NTZOZIKF.cjs.map +0 -1
  162. package/dist/chunk-PTTPRMT3.cjs.map +0 -1
  163. package/dist/chunk-PVUR75AM.js.map +0 -1
  164. package/dist/chunk-Z36FXNDQ.cjs.map +0 -1
@@ -7,6 +7,7 @@ import { appendFileSync, mkdirSync, writeFileSync } from 'fs';
7
7
  import { join } from 'path';
8
8
  import { randomBytes, createHash, randomUUID } from 'crypto';
9
9
  import { Agent } from '@mastra/core/agent';
10
+ import { getOrCreateSpan, EntityType, SpanType, createObservabilityContext } from '@mastra/core/observability';
10
11
  import { AsyncLocalStorage } from 'async_hooks';
11
12
  import { inspect } from 'util';
12
13
 
@@ -1968,7 +1969,8 @@ var ObservationStrategy = class _ObservationStrategy {
1968
1969
  writer,
1969
1970
  abortSignal,
1970
1971
  reflectionHooks,
1971
- requestContext
1972
+ requestContext,
1973
+ observabilityContext: this.opts.observabilityContext
1972
1974
  });
1973
1975
  }
1974
1976
  } catch (error) {
@@ -2109,6 +2111,27 @@ ${threadClose}`;
2109
2111
  const boundary = lastObservedAt ? _ObservationStrategy.createMessageBoundary(lastObservedAt) : "\n\n";
2110
2112
  return `${existingObservations}${boundary}${newThreadSection}`;
2111
2113
  }
2114
+ async indexObservationGroups(observations, threadId, resourceId, observedAt) {
2115
+ if (!resourceId || !this.deps.onIndexObservations) {
2116
+ return;
2117
+ }
2118
+ const groups = parseObservationGroups(observations);
2119
+ if (groups.length === 0) {
2120
+ return;
2121
+ }
2122
+ await Promise.all(
2123
+ groups.map(
2124
+ (group) => this.deps.onIndexObservations({
2125
+ text: group.content,
2126
+ groupId: group.id,
2127
+ range: group.range,
2128
+ threadId,
2129
+ resourceId,
2130
+ observedAt
2131
+ })
2132
+ )
2133
+ );
2134
+ }
2112
2135
  // ── Marker persistence ──────────────────────────────────────
2113
2136
  /**
2114
2137
  * Persist a marker to the last assistant message in storage.
@@ -2242,6 +2265,7 @@ var SyncObservationStrategy = class extends ObservationStrategy {
2242
2265
  const omMeta = thread ? getThreadOMMetadata(thread.metadata) : void 0;
2243
2266
  const result = await this.deps.observer.call(existingObservations, messages, this.opts.abortSignal, {
2244
2267
  requestContext: this.opts.requestContext,
2268
+ observabilityContext: this.opts.observabilityContext,
2245
2269
  priorCurrentTask: omMeta?.currentTask,
2246
2270
  priorSuggestedResponse: omMeta?.suggestedResponse,
2247
2271
  priorThreadTitle: omMeta?.threadTitle
@@ -2291,7 +2315,7 @@ var SyncObservationStrategy = class extends ObservationStrategy {
2291
2315
  };
2292
2316
  }
2293
2317
  async persist(processed) {
2294
- const { record, threadId, messages } = this.opts;
2318
+ const { record, threadId, resourceId, messages } = this.opts;
2295
2319
  const thread = await this.storage.getThreadById({ threadId });
2296
2320
  let threadUpdateMarker;
2297
2321
  if (thread) {
@@ -2328,6 +2352,7 @@ var SyncObservationStrategy = class extends ObservationStrategy {
2328
2352
  lastObservedAt: processed.lastObservedAt,
2329
2353
  observedMessageIds: processed.observedMessageIds
2330
2354
  });
2355
+ await this.indexObservationGroups(processed.observations, threadId, resourceId, processed.lastObservedAt);
2331
2356
  }
2332
2357
  async emitEndMarkers(cycleId, processed) {
2333
2358
  const actualTokensObserved = await this.tokenCounter.countMessagesAsync(this.opts.messages);
@@ -2391,7 +2416,8 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
2391
2416
  async observe(existingObservations, messages) {
2392
2417
  return this.deps.observer.call(existingObservations, messages, void 0, {
2393
2418
  skipContinuationHints: true,
2394
- requestContext: this.opts.requestContext
2419
+ requestContext: this.opts.requestContext,
2420
+ observabilityContext: this.opts.observabilityContext
2395
2421
  });
2396
2422
  }
2397
2423
  async process(output, _existingObservations) {
@@ -2430,7 +2456,7 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
2430
2456
  }
2431
2457
  async persist(processed) {
2432
2458
  if (!processed.observations) return;
2433
- const { record, messages } = this.opts;
2459
+ const { record, threadId, resourceId, messages } = this.opts;
2434
2460
  const messageTokens = await this.tokenCounter.countMessagesAsync(messages);
2435
2461
  await this.storage.updateBufferedObservations({
2436
2462
  id: record.id,
@@ -2447,6 +2473,7 @@ var AsyncBufferObservationStrategy = class extends ObservationStrategy {
2447
2473
  },
2448
2474
  lastBufferedAtTime: processed.lastObservedAt
2449
2475
  });
2476
+ await this.indexObservationGroups(processed.observations, threadId, resourceId, processed.lastObservedAt);
2450
2477
  }
2451
2478
  async emitEndMarkers(_cycleId, processed) {
2452
2479
  if (!processed.observations || !this.opts.writer) return;
@@ -2661,7 +2688,8 @@ var ResourceScopedObservationStrategy = class extends ObservationStrategy {
2661
2688
  batch.threadIds,
2662
2689
  this.opts.abortSignal,
2663
2690
  this.opts.requestContext,
2664
- this.priorMetadataByThread
2691
+ this.priorMetadataByThread,
2692
+ this.opts.observabilityContext
2665
2693
  );
2666
2694
  })
2667
2695
  );
@@ -2745,7 +2773,7 @@ var ResourceScopedObservationStrategy = class extends ObservationStrategy {
2745
2773
  };
2746
2774
  }
2747
2775
  async persist(processed) {
2748
- const { record } = this.opts;
2776
+ const { record, resourceId } = this.opts;
2749
2777
  const threadUpdateMarkers = [];
2750
2778
  if (processed.threadMetadataUpdates) {
2751
2779
  for (const update of processed.threadMetadataUpdates) {
@@ -2789,6 +2817,18 @@ var ResourceScopedObservationStrategy = class extends ObservationStrategy {
2789
2817
  lastObservedAt: processed.lastObservedAt,
2790
2818
  observedMessageIds: processed.observedMessageIds
2791
2819
  });
2820
+ if (resourceId) {
2821
+ await Promise.all(
2822
+ this.observationResults.map(
2823
+ ({ threadId, threadMessages, result }) => this.indexObservationGroups(
2824
+ result.observations,
2825
+ threadId,
2826
+ resourceId,
2827
+ this.getMaxMessageTimestamp(threadMessages)
2828
+ )
2829
+ )
2830
+ );
2831
+ }
2792
2832
  }
2793
2833
  async emitEndMarkers(cycleId, processed) {
2794
2834
  for (const obsResult of this.observationResults) {
@@ -2844,6 +2884,7 @@ ObservationStrategy.create = ((om, opts) => {
2844
2884
  reflector: om.reflector,
2845
2885
  observedMessageIds: om.observedMessageIds,
2846
2886
  obscureThreadIds: om.getObscureThreadIds(),
2887
+ onIndexObservations: om.onIndexObservations,
2847
2888
  emitDebugEvent: (e2) => om.emitDebugEvent(e2)
2848
2889
  };
2849
2890
  if (opts.cycleId) return new AsyncBufferObservationStrategy(deps, opts);
@@ -2912,7 +2953,8 @@ var ObservationStep = class {
2912
2953
  observationTokens: obsTokens,
2913
2954
  threadId,
2914
2955
  writer: this.turn.writer,
2915
- requestContext: this.turn.requestContext
2956
+ requestContext: this.turn.requestContext,
2957
+ observabilityContext: this.turn.observabilityContext
2916
2958
  });
2917
2959
  await this.turn.refreshRecord();
2918
2960
  if (this.turn.record.generationCount > preReflectGeneration) {
@@ -2944,6 +2986,7 @@ var ObservationStep = class {
2944
2986
  record: statusSnapshot.record,
2945
2987
  writer: this.turn.writer,
2946
2988
  requestContext: this.turn.requestContext,
2989
+ observabilityContext: this.turn.observabilityContext,
2947
2990
  beforeBuffer: async (candidates) => {
2948
2991
  if (candidates.length === 0) {
2949
2992
  return;
@@ -3080,7 +3123,8 @@ var ObservationStep = class {
3080
3123
  threadId,
3081
3124
  writer: this.turn.writer,
3082
3125
  messageList,
3083
- requestContext: this.turn.requestContext
3126
+ requestContext: this.turn.requestContext,
3127
+ observabilityContext: this.turn.observabilityContext
3084
3128
  });
3085
3129
  return {
3086
3130
  succeeded: true,
@@ -3094,7 +3138,8 @@ var ObservationStep = class {
3094
3138
  resourceId,
3095
3139
  messages: messageList.get.all.db(),
3096
3140
  requestContext: this.turn.requestContext,
3097
- writer: this.turn.writer
3141
+ writer: this.turn.writer,
3142
+ observabilityContext: this.turn.observabilityContext
3098
3143
  });
3099
3144
  return { succeeded: obsResult.observed, record: obsResult.record };
3100
3145
  }
@@ -3113,6 +3158,8 @@ var ObservationTurn = class {
3113
3158
  writer;
3114
3159
  /** Optional request context for observation calls. */
3115
3160
  requestContext;
3161
+ /** Optional observability context for nested OM spans. */
3162
+ observabilityContext;
3116
3163
  /** Optional processor-provided hooks for turn/step lifecycle integration. */
3117
3164
  hooks;
3118
3165
  constructor(opts) {
@@ -3120,6 +3167,7 @@ var ObservationTurn = class {
3120
3167
  this.threadId = opts.threadId;
3121
3168
  this.resourceId = opts.resourceId;
3122
3169
  this.messageList = opts.messageList;
3170
+ this.observabilityContext = opts.observabilityContext;
3123
3171
  this.hooks = opts.hooks;
3124
3172
  }
3125
3173
  om;
@@ -4360,13 +4408,62 @@ function optimizeObservationsForContext(observations) {
4360
4408
  optimized = optimized.replace(/\n{3,}/g, "\n\n");
4361
4409
  return optimized.trim();
4362
4410
  }
4411
+ var PHASE_CONFIG = {
4412
+ observer: {
4413
+ name: "om.observer",
4414
+ entityName: "Observer"
4415
+ },
4416
+ "observer-multi-thread": {
4417
+ name: "om.observer.multi-thread",
4418
+ entityName: "MultiThreadObserver"
4419
+ },
4420
+ reflector: {
4421
+ name: "om.reflector",
4422
+ entityName: "Reflector"
4423
+ }
4424
+ };
4425
+ async function withOmTracingSpan({
4426
+ phase,
4427
+ model,
4428
+ inputTokens,
4429
+ requestContext,
4430
+ observabilityContext,
4431
+ metadata,
4432
+ callback
4433
+ }) {
4434
+ const config = PHASE_CONFIG[phase];
4435
+ const span = getOrCreateSpan({
4436
+ type: SpanType.GENERIC,
4437
+ name: config.name,
4438
+ entityType: EntityType.OUTPUT_STEP_PROCESSOR,
4439
+ entityName: config.entityName,
4440
+ tracingContext: observabilityContext?.tracingContext ?? observabilityContext?.tracing,
4441
+ attributes: {
4442
+ metadata: {
4443
+ omPhase: phase,
4444
+ omInputTokens: inputTokens,
4445
+ omSelectedModel: typeof model === "string" ? model : "(dynamic-model)",
4446
+ ...metadata
4447
+ }
4448
+ },
4449
+ requestContext
4450
+ });
4451
+ const childObservabilityContext = createObservabilityContext({ currentSpan: span });
4452
+ if (!span) {
4453
+ return callback(childObservabilityContext);
4454
+ }
4455
+ return span.executeInContext(() => callback(childObservabilityContext));
4456
+ }
4363
4457
  var ObserverRunner = class {
4364
4458
  observationConfig;
4365
4459
  observedMessageIds;
4366
- observerAgent;
4460
+ resolveModel;
4461
+ tokenCounter;
4367
4462
  constructor(opts) {
4368
4463
  this.observationConfig = opts.observationConfig;
4369
4464
  this.observedMessageIds = opts.observedMessageIds;
4465
+ this.resolveModel = opts.resolveModel;
4466
+ this.tokenCounter = opts.tokenCounter;
4370
4467
  }
4371
4468
  createAgent(model, isMultiThread = false) {
4372
4469
  return new Agent({
@@ -4380,10 +4477,6 @@ var ObserverRunner = class {
4380
4477
  model
4381
4478
  });
4382
4479
  }
4383
- getAgent(model) {
4384
- this.observerAgent ??= this.createAgent(model);
4385
- return this.observerAgent;
4386
- }
4387
4480
  async withAbortCheck(fn, abortSignal) {
4388
4481
  if (abortSignal?.aborted) {
4389
4482
  throw new Error("The operation was aborted.");
@@ -4398,7 +4491,9 @@ var ObserverRunner = class {
4398
4491
  * Call the Observer agent for a single thread.
4399
4492
  */
4400
4493
  async call(existingObservations, messagesToObserve, abortSignal, options) {
4401
- const agent = options?.model ? this.createAgent(options.model) : this.getAgent(this.observationConfig.model);
4494
+ const inputTokens = this.tokenCounter.countMessages(messagesToObserve);
4495
+ const resolvedModel = options?.model ? { model: options.model } : this.resolveModel(inputTokens);
4496
+ const agent = this.createAgent(resolvedModel.model);
4402
4497
  const observerMessages = [
4403
4498
  {
4404
4499
  role: "user",
@@ -4410,15 +4505,32 @@ var ObserverRunner = class {
4410
4505
  buildObserverHistoryMessage(messagesToObserve)
4411
4506
  ];
4412
4507
  const doGenerate = async () => {
4413
- return this.withAbortCheck(async () => {
4414
- const streamResult = await agent.stream(observerMessages, {
4415
- modelSettings: { ...this.observationConfig.modelSettings },
4416
- providerOptions: this.observationConfig.providerOptions,
4417
- ...abortSignal ? { abortSignal } : {},
4418
- ...options?.requestContext ? { requestContext: options.requestContext } : {}
4419
- });
4420
- return streamResult.getFullOutput();
4421
- }, abortSignal);
4508
+ return withOmTracingSpan({
4509
+ phase: "observer",
4510
+ model: resolvedModel.model,
4511
+ inputTokens,
4512
+ requestContext: options?.requestContext,
4513
+ observabilityContext: options?.observabilityContext,
4514
+ metadata: {
4515
+ omPreviousObserverTokens: this.observationConfig.previousObserverTokens,
4516
+ omThreadTitleEnabled: this.observationConfig.threadTitle,
4517
+ omSkipContinuationHints: options?.skipContinuationHints ?? false,
4518
+ omWasTruncated: options?.wasTruncated ?? false,
4519
+ ...resolvedModel.selectedThreshold !== void 0 ? { omSelectedThreshold: resolvedModel.selectedThreshold } : {},
4520
+ ...resolvedModel.routingStrategy ? { omRoutingStrategy: resolvedModel.routingStrategy } : {},
4521
+ ...resolvedModel.routingThresholds ? { omRoutingThresholds: resolvedModel.routingThresholds } : {}
4522
+ },
4523
+ callback: (childObservabilityContext) => this.withAbortCheck(async () => {
4524
+ const streamResult = await agent.stream(observerMessages, {
4525
+ modelSettings: { ...this.observationConfig.modelSettings },
4526
+ providerOptions: this.observationConfig.providerOptions,
4527
+ ...abortSignal ? { abortSignal } : {},
4528
+ ...options?.requestContext ? { requestContext: options.requestContext } : {},
4529
+ ...childObservabilityContext
4530
+ });
4531
+ return streamResult.getFullOutput();
4532
+ }, abortSignal)
4533
+ });
4422
4534
  };
4423
4535
  let result = await doGenerate();
4424
4536
  let parsed = parseObserverOutput(result.text);
@@ -4443,8 +4555,13 @@ var ObserverRunner = class {
4443
4555
  /**
4444
4556
  * Call the Observer agent for multiple threads in a single batched request.
4445
4557
  */
4446
- async callMultiThread(existingObservations, messagesByThread, threadOrder, abortSignal, requestContext, priorMetadataByThread, model) {
4447
- const agent = this.createAgent(model ?? this.observationConfig.model, true);
4558
+ async callMultiThread(existingObservations, messagesByThread, threadOrder, abortSignal, requestContext, priorMetadataByThread, observabilityContext, model) {
4559
+ const inputTokens = Array.from(messagesByThread.values()).reduce(
4560
+ (total, messages) => total + this.tokenCounter.countMessages(messages),
4561
+ 0
4562
+ );
4563
+ const resolvedModel = model ? { model } : this.resolveModel(inputTokens);
4564
+ const agent = this.createAgent(resolvedModel.model, true);
4448
4565
  const observerMessages = [
4449
4566
  {
4450
4567
  role: "user",
@@ -4464,15 +4581,31 @@ var ObserverRunner = class {
4464
4581
  }
4465
4582
  }
4466
4583
  const doGenerate = async () => {
4467
- return this.withAbortCheck(async () => {
4468
- const streamResult = await agent.stream(observerMessages, {
4469
- modelSettings: { ...this.observationConfig.modelSettings },
4470
- providerOptions: this.observationConfig.providerOptions,
4471
- ...abortSignal ? { abortSignal } : {},
4472
- ...requestContext ? { requestContext } : {}
4473
- });
4474
- return streamResult.getFullOutput();
4475
- }, abortSignal);
4584
+ return withOmTracingSpan({
4585
+ phase: "observer-multi-thread",
4586
+ model: resolvedModel.model,
4587
+ inputTokens,
4588
+ requestContext,
4589
+ observabilityContext,
4590
+ metadata: {
4591
+ omThreadCount: threadOrder.length,
4592
+ omPreviousObserverTokens: this.observationConfig.previousObserverTokens,
4593
+ omThreadTitleEnabled: this.observationConfig.threadTitle,
4594
+ ...resolvedModel.selectedThreshold !== void 0 ? { omSelectedThreshold: resolvedModel.selectedThreshold } : {},
4595
+ ...resolvedModel.routingStrategy ? { omRoutingStrategy: resolvedModel.routingStrategy } : {},
4596
+ ...resolvedModel.routingThresholds ? { omRoutingThresholds: resolvedModel.routingThresholds } : {}
4597
+ },
4598
+ callback: (childObservabilityContext) => this.withAbortCheck(async () => {
4599
+ const streamResult = await agent.stream(observerMessages, {
4600
+ modelSettings: { ...this.observationConfig.modelSettings },
4601
+ providerOptions: this.observationConfig.providerOptions,
4602
+ ...abortSignal ? { abortSignal } : {},
4603
+ ...requestContext ? { requestContext } : {},
4604
+ ...childObservabilityContext
4605
+ });
4606
+ return streamResult.getFullOutput();
4607
+ }, abortSignal)
4608
+ });
4476
4609
  };
4477
4610
  let result = await doGenerate();
4478
4611
  let parsed = parseMultiThreadObserverOutput(result.text);
@@ -4775,6 +4908,7 @@ var ReflectorRunner = class {
4775
4908
  reflectionConfig;
4776
4909
  observationConfig;
4777
4910
  tokenCounter;
4911
+ resolveModel;
4778
4912
  storage;
4779
4913
  scope;
4780
4914
  buffering;
@@ -4786,6 +4920,7 @@ var ReflectorRunner = class {
4786
4920
  this.reflectionConfig = opts.reflectionConfig;
4787
4921
  this.observationConfig = opts.observationConfig;
4788
4922
  this.tokenCounter = opts.tokenCounter;
4923
+ this.resolveModel = opts.resolveModel;
4789
4924
  this.storage = opts.storage;
4790
4925
  this.scope = opts.scope;
4791
4926
  this.buffering = opts.buffering;
@@ -4812,9 +4947,10 @@ var ReflectorRunner = class {
4812
4947
  /**
4813
4948
  * Call the Reflector agent with escalating compression levels.
4814
4949
  */
4815
- async call(observations, manualPrompt, streamContext, observationTokensThreshold, abortSignal, skipContinuationHints, compressionStartLevel, requestContext, model) {
4816
- const agent = this.createAgent(model ?? this.reflectionConfig.model);
4950
+ async call(observations, manualPrompt, streamContext, observationTokensThreshold, abortSignal, skipContinuationHints, compressionStartLevel, requestContext, observabilityContext, model) {
4817
4951
  const originalTokens = this.tokenCounter.countObservations(observations);
4952
+ const resolvedModel = model ? { model } : this.resolveModel(originalTokens);
4953
+ const agent = this.createAgent(resolvedModel.model);
4818
4954
  const targetThreshold = observationTokensThreshold ?? getMaxThreshold(this.reflectionConfig.observationTokens);
4819
4955
  let totalUsage = { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
4820
4956
  const startLevel = compressionStartLevel ?? 0;
@@ -4831,37 +4967,54 @@ var ReflectorRunner = class {
4831
4967
  `[OM:callReflector] ${isRetry ? `retry #${attemptNumber - 1}` : "first attempt"}: level=${currentLevel}, originalTokens=${originalTokens}, targetThreshold=${targetThreshold}, promptLen=${prompt.length}, skipContinuationHints=${skipContinuationHints}`
4832
4968
  );
4833
4969
  let chunkCount = 0;
4834
- const result = await withAbortCheck(async () => {
4835
- const streamResult = await agent.stream(prompt, {
4836
- modelSettings: {
4837
- ...this.reflectionConfig.modelSettings
4838
- },
4839
- providerOptions: this.reflectionConfig.providerOptions,
4840
- ...abortSignal ? { abortSignal } : {},
4841
- ...requestContext ? { requestContext } : {},
4842
- ...attemptNumber === 1 ? {
4843
- onChunk(chunk) {
4844
- chunkCount++;
4845
- if (chunkCount === 1 || chunkCount % 50 === 0) {
4846
- const preview = chunk.type === "text-delta" ? ` text="${chunk.textDelta?.slice(0, 80)}..."` : chunk.type === "tool-call" ? ` tool=${chunk.toolName}` : "";
4847
- omDebug(`[OM:callReflector] chunk#${chunkCount}: type=${chunk.type}${preview}`);
4848
- }
4849
- },
4850
- onFinish(event) {
4851
- omDebug(
4852
- `[OM:callReflector] onFinish: chunks=${chunkCount}, finishReason=${event.finishReason}, inputTokens=${event.usage?.inputTokens}, outputTokens=${event.usage?.outputTokens}, textLen=${event.text?.length}`
4853
- );
4854
- },
4855
- onAbort(event) {
4856
- omDebug(`[OM:callReflector] onAbort: chunks=${chunkCount}, reason=${event?.reason ?? "unknown"}`);
4970
+ const result = await withOmTracingSpan({
4971
+ phase: "reflector",
4972
+ model: resolvedModel.model,
4973
+ inputTokens: originalTokens,
4974
+ requestContext,
4975
+ observabilityContext,
4976
+ metadata: {
4977
+ omCompressionLevel: currentLevel,
4978
+ omCompressionAttempt: attemptNumber,
4979
+ omTargetThreshold: targetThreshold,
4980
+ omSkipContinuationHints: skipContinuationHints ?? false,
4981
+ ...resolvedModel.selectedThreshold !== void 0 ? { omSelectedThreshold: resolvedModel.selectedThreshold } : {},
4982
+ ...resolvedModel.routingStrategy ? { omRoutingStrategy: resolvedModel.routingStrategy } : {},
4983
+ ...resolvedModel.routingThresholds ? { omRoutingThresholds: resolvedModel.routingThresholds } : {}
4984
+ },
4985
+ callback: (childObservabilityContext) => withAbortCheck(async () => {
4986
+ const streamResult = await agent.stream(prompt, {
4987
+ modelSettings: {
4988
+ ...this.reflectionConfig.modelSettings
4857
4989
  },
4858
- onError({ error }) {
4859
- omError(`[OM:callReflector] onError after ${chunkCount} chunks`, error);
4860
- }
4861
- } : {}
4862
- });
4863
- return streamResult.getFullOutput();
4864
- }, abortSignal);
4990
+ providerOptions: this.reflectionConfig.providerOptions,
4991
+ ...abortSignal ? { abortSignal } : {},
4992
+ ...requestContext ? { requestContext } : {},
4993
+ ...childObservabilityContext,
4994
+ ...attemptNumber === 1 ? {
4995
+ onChunk(chunk) {
4996
+ chunkCount++;
4997
+ if (chunkCount === 1 || chunkCount % 50 === 0) {
4998
+ const preview = chunk.type === "text-delta" ? ` text="${chunk.textDelta?.slice(0, 80)}..."` : chunk.type === "tool-call" ? ` tool=${chunk.toolName}` : "";
4999
+ omDebug(`[OM:callReflector] chunk#${chunkCount}: type=${chunk.type}${preview}`);
5000
+ }
5001
+ },
5002
+ onFinish(event) {
5003
+ omDebug(
5004
+ `[OM:callReflector] onFinish: chunks=${chunkCount}, finishReason=${event.finishReason}, inputTokens=${event.usage?.inputTokens}, outputTokens=${event.usage?.outputTokens}, textLen=${event.text?.length}`
5005
+ );
5006
+ },
5007
+ onAbort(event) {
5008
+ omDebug(`[OM:callReflector] onAbort: chunks=${chunkCount}, reason=${event?.reason ?? "unknown"}`);
5009
+ },
5010
+ onError({ error }) {
5011
+ omError(`[OM:callReflector] onError after ${chunkCount} chunks`, error);
5012
+ }
5013
+ } : {}
5014
+ });
5015
+ return streamResult.getFullOutput();
5016
+ }, abortSignal)
5017
+ });
4865
5018
  omDebug(
4866
5019
  `[OM:callReflector] attempt #${attemptNumber} returned: textLen=${result.text?.length}, textPreview="${result.text?.slice(0, 120)}...", inputTokens=${result.usage?.inputTokens ?? result.totalUsage?.inputTokens}, outputTokens=${result.usage?.outputTokens ?? result.totalUsage?.outputTokens}`
4867
5020
  );
@@ -4928,7 +5081,7 @@ var ReflectorRunner = class {
4928
5081
  /**
4929
5082
  * Start an async buffered reflection in the background.
4930
5083
  */
4931
- startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext) {
5084
+ startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext, observabilityContext) {
4932
5085
  const bufferKey = this.buffering.getReflectionBufferKey(lockKey);
4933
5086
  if (this.buffering.isAsyncBufferingInProgress(bufferKey)) {
4934
5087
  return;
@@ -4938,7 +5091,7 @@ var ReflectorRunner = class {
4938
5091
  this.storage.setBufferingReflectionFlag(record.id, true).catch((err) => {
4939
5092
  omError("[OM] Failed to set buffering reflection flag", err);
4940
5093
  });
4941
- const asyncOp = this.doAsyncBufferedReflection(record, bufferKey, writer, requestContext).catch(async (error) => {
5094
+ const asyncOp = this.doAsyncBufferedReflection(record, bufferKey, writer, requestContext, observabilityContext).catch(async (error) => {
4942
5095
  if (writer) {
4943
5096
  const failedMarker = createBufferingFailedMarker({
4944
5097
  cycleId: `reflect-buf-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
@@ -4968,7 +5121,7 @@ var ReflectorRunner = class {
4968
5121
  * Perform async buffered reflection — reflects observations and stores to bufferedReflection.
4969
5122
  * Does NOT create a new generation or update activeObservations.
4970
5123
  */
4971
- async doAsyncBufferedReflection(record, _bufferKey, writer, requestContext) {
5124
+ async doAsyncBufferedReflection(record, _bufferKey, writer, requestContext, observabilityContext) {
4972
5125
  const freshRecord = await this.storage.getObservationalMemory(record.threadId, record.resourceId);
4973
5126
  const currentRecord = freshRecord ?? record;
4974
5127
  const observationTokens = currentRecord.observationTokenCount ?? 0;
@@ -5015,7 +5168,8 @@ var ReflectorRunner = class {
5015
5168
  void 0,
5016
5169
  true,
5017
5170
  compressionStartLevel,
5018
- requestContext
5171
+ requestContext,
5172
+ observabilityContext
5019
5173
  );
5020
5174
  const reflectionTokenCount = this.tokenCounter.countObservations(reflectResult.observations);
5021
5175
  omDebug(
@@ -5131,7 +5285,16 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5131
5285
  * @internal Used by observation strategies. Do not call directly.
5132
5286
  */
5133
5287
  async maybeReflect(opts) {
5134
- const { record, observationTokens, writer, abortSignal, messageList, reflectionHooks, requestContext } = opts;
5288
+ const {
5289
+ record,
5290
+ observationTokens,
5291
+ writer,
5292
+ abortSignal,
5293
+ messageList,
5294
+ reflectionHooks,
5295
+ requestContext,
5296
+ observabilityContext
5297
+ } = opts;
5135
5298
  const lockKey = this.buffering.getLockKey(record.threadId, record.resourceId);
5136
5299
  const reflectThreshold = getMaxThreshold(this.reflectionConfig.observationTokens);
5137
5300
  if (this.buffering.isAsyncReflectionEnabled() && observationTokens < reflectThreshold) {
@@ -5151,7 +5314,14 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5151
5314
  return observationTokens >= activationPoint;
5152
5315
  })();
5153
5316
  if (shouldTrigger) {
5154
- this.startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext);
5317
+ this.startAsyncBufferedReflection(
5318
+ record,
5319
+ observationTokens,
5320
+ lockKey,
5321
+ writer,
5322
+ requestContext,
5323
+ observabilityContext
5324
+ );
5155
5325
  }
5156
5326
  }
5157
5327
  if (observationTokens < reflectThreshold) {
@@ -5178,7 +5348,14 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5178
5348
  omDebug(
5179
5349
  `[OM:reflect] async activation failed, no blockAfter or below it (obsTokens=${observationTokens}, blockAfter=${this.reflectionConfig.blockAfter}) \u2014 starting background reflection`
5180
5350
  );
5181
- this.startAsyncBufferedReflection(record, observationTokens, lockKey, writer, requestContext);
5351
+ this.startAsyncBufferedReflection(
5352
+ record,
5353
+ observationTokens,
5354
+ lockKey,
5355
+ writer,
5356
+ requestContext,
5357
+ observabilityContext
5358
+ );
5182
5359
  return;
5183
5360
  }
5184
5361
  }
@@ -5226,7 +5403,8 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5226
5403
  abortSignal,
5227
5404
  void 0,
5228
5405
  compressionStartLevel,
5229
- requestContext
5406
+ requestContext,
5407
+ observabilityContext
5230
5408
  );
5231
5409
  const reflectionTokenCount = this.tokenCounter.countObservations(reflectResult.observations);
5232
5410
  await this.storage.createReflectionGeneration({
@@ -6512,11 +6690,12 @@ var ObservationalMemory = class _ObservationalMemory {
6512
6690
  storage;
6513
6691
  tokenCounter;
6514
6692
  scope;
6515
- /** Whether retrieval-mode observation groups are enabled (thread scope only). */
6693
+ /** Whether retrieval-mode observation groups are enabled. */
6516
6694
  retrieval;
6517
6695
  observationConfig;
6518
6696
  reflectionConfig;
6519
6697
  onDebugEvent;
6698
+ onIndexObservations;
6520
6699
  /** Observer agent runner — handles LLM calls for extracting observations. */
6521
6700
  observer;
6522
6701
  /** Reflector agent runner — handles LLM calls for compressing observations. */
@@ -6589,7 +6768,8 @@ var ObservationalMemory = class _ObservationalMemory {
6589
6768
  this.shouldObscureThreadIds = config.obscureThreadIds || false;
6590
6769
  this.storage = config.storage;
6591
6770
  this.scope = config.scope ?? "thread";
6592
- this.retrieval = this.scope === "thread" && (config.retrieval ?? false);
6771
+ this.retrieval = Boolean(config.retrieval);
6772
+ this.onIndexObservations = config.onIndexObservations;
6593
6773
  const resolveModel = (m) => m === "default" ? OBSERVATIONAL_MEMORY_DEFAULTS.observation.model : m;
6594
6774
  const observationModel = resolveModel(config.model) ?? resolveModel(config.observation?.model) ?? resolveModel(config.reflection?.model);
6595
6775
  const reflectionModel = resolveModel(config.model) ?? resolveModel(config.reflection?.model) ?? resolveModel(config.observation?.model);
@@ -6683,7 +6863,9 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
6683
6863
  this.messageHistory = new MessageHistory({ storage: this.storage });
6684
6864
  this.observer = new ObserverRunner({
6685
6865
  observationConfig: this.observationConfig,
6686
- observedMessageIds: this.observedMessageIds
6866
+ observedMessageIds: this.observedMessageIds,
6867
+ resolveModel: (inputTokens) => this.resolveObservationModel(inputTokens),
6868
+ tokenCounter: this.tokenCounter
6687
6869
  });
6688
6870
  this.buffering = new BufferingCoordinator({
6689
6871
  observationConfig: this.observationConfig,
@@ -6700,7 +6882,8 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
6700
6882
  emitDebugEvent: (e2) => this.emitDebugEvent(e2),
6701
6883
  persistMarkerToStorage: (m, t2, r) => this.persistMarkerToStorage(m, t2, r),
6702
6884
  persistMarkerToMessage: (m, ml, t2, r) => this.persistMarkerToMessage(m, ml, t2, r),
6703
- getCompressionStartLevel: (rc) => this.getCompressionStartLevel(rc)
6885
+ getCompressionStartLevel: (rc) => this.getCompressionStartLevel(rc),
6886
+ resolveModel: (inputTokens) => this.resolveReflectionModel(inputTokens)
6704
6887
  });
6705
6888
  this.validateBufferConfig();
6706
6889
  omDebug(
@@ -6762,6 +6945,54 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
6762
6945
  }
6763
6946
  return model.provider ? `${model.provider}/${model.modelId}` : model.modelId;
6764
6947
  }
6948
+ resolveObservationModel(inputTokens) {
6949
+ return this.resolveTieredModel(this.observationConfig.model, inputTokens);
6950
+ }
6951
+ resolveReflectionModel(inputTokens) {
6952
+ return this.resolveTieredModel(this.reflectionConfig.model, inputTokens);
6953
+ }
6954
+ resolveTieredModel(model, inputTokens) {
6955
+ if (!(model instanceof ModelByInputTokens)) {
6956
+ return {
6957
+ model
6958
+ };
6959
+ }
6960
+ const thresholds = model.getThresholds();
6961
+ const selectedThreshold = thresholds.find((upTo) => inputTokens <= upTo) ?? thresholds.at(-1);
6962
+ return {
6963
+ model: model.resolve(inputTokens),
6964
+ selectedThreshold,
6965
+ routingStrategy: "model-by-input-tokens",
6966
+ routingThresholds: thresholds.join(",")
6967
+ };
6968
+ }
6969
+ async resolveModelRouting(modelConfig, requestContext) {
6970
+ try {
6971
+ if (modelConfig instanceof ModelByInputTokens) {
6972
+ const routing = await Promise.all(
6973
+ modelConfig.getThresholds().map(async (upTo) => {
6974
+ const resolvedModel = modelConfig.resolve(upTo);
6975
+ const resolved2 = await this.resolveModelContext(resolvedModel, requestContext);
6976
+ return {
6977
+ upTo,
6978
+ model: resolved2?.modelId ? this.formatModelName(resolved2) : "(unknown)"
6979
+ };
6980
+ })
6981
+ );
6982
+ return {
6983
+ model: routing[0]?.model ?? "(unknown)",
6984
+ routing
6985
+ };
6986
+ }
6987
+ const resolved = await this.resolveModelContext(modelConfig, requestContext);
6988
+ return {
6989
+ model: resolved?.modelId ? this.formatModelName(resolved) : "(unknown)"
6990
+ };
6991
+ } catch (error) {
6992
+ omError("[OM] Failed to resolve model config", error);
6993
+ return { model: "(unknown)" };
6994
+ }
6995
+ }
6765
6996
  async resolveModelContext(modelConfig, requestContext, inputTokens) {
6766
6997
  const modelToResolve = this.getModelToResolve(modelConfig, inputTokens);
6767
6998
  if (!modelToResolve) {
@@ -6794,29 +7025,22 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
6794
7025
  * This is async because it needs to resolve the model configs.
6795
7026
  */
6796
7027
  async getResolvedConfig(requestContext) {
6797
- const safeResolveModel = async (modelConfig) => {
6798
- try {
6799
- const resolved = await this.resolveModelContext(modelConfig, requestContext);
6800
- return resolved?.modelId ? this.formatModelName(resolved) : "(unknown)";
6801
- } catch (error) {
6802
- omError("[OM] Failed to resolve model config", error);
6803
- return "(unknown)";
6804
- }
6805
- };
6806
- const [observationModelName, reflectionModelName] = await Promise.all([
6807
- safeResolveModel(this.observationConfig.model),
6808
- safeResolveModel(this.reflectionConfig.model)
7028
+ const [observationResolved, reflectionResolved] = await Promise.all([
7029
+ this.resolveModelRouting(this.observationConfig.model, requestContext),
7030
+ this.resolveModelRouting(this.reflectionConfig.model, requestContext)
6809
7031
  ]);
6810
7032
  return {
6811
7033
  scope: this.scope,
6812
7034
  observation: {
6813
7035
  messageTokens: this.observationConfig.messageTokens,
6814
- model: observationModelName,
6815
- previousObserverTokens: this.observationConfig.previousObserverTokens
7036
+ model: observationResolved.model,
7037
+ previousObserverTokens: this.observationConfig.previousObserverTokens,
7038
+ routing: observationResolved.routing
6816
7039
  },
6817
7040
  reflection: {
6818
7041
  observationTokens: this.reflectionConfig.observationTokens,
6819
- model: reflectionModelName
7042
+ model: reflectionResolved.model,
7043
+ routing: reflectionResolved.routing
6820
7044
  }
6821
7045
  };
6822
7046
  }
@@ -7623,7 +7847,7 @@ ${grouped}` : grouped;
7623
7847
  * @param lockKey - Lock key for this scope
7624
7848
  * @param writer - Optional stream writer for emitting buffering markers
7625
7849
  */
7626
- async startAsyncBufferedObservation(record, threadId, unobservedMessages, lockKey, writer, contextWindowTokens, requestContext) {
7850
+ async startAsyncBufferedObservation(record, threadId, unobservedMessages, lockKey, writer, contextWindowTokens, requestContext, observabilityContext) {
7627
7851
  const bufferKey = this.buffering.getObservationBufferKey(lockKey);
7628
7852
  const currentTokens = contextWindowTokens ?? await this.tokenCounter.countMessagesAsync(unobservedMessages) + (record.pendingMessageTokens ?? 0);
7629
7853
  BufferingCoordinator.lastBufferedBoundary.set(bufferKey, currentTokens);
@@ -7637,7 +7861,8 @@ ${grouped}` : grouped;
7637
7861
  unobservedMessages,
7638
7862
  bufferKey,
7639
7863
  writer,
7640
- requestContext
7864
+ requestContext,
7865
+ observabilityContext
7641
7866
  ).finally(() => {
7642
7867
  BufferingCoordinator.asyncBufferingOps.delete(bufferKey);
7643
7868
  unregisterOp(record.id, "bufferingObservation");
@@ -7651,7 +7876,7 @@ ${grouped}` : grouped;
7651
7876
  * Internal method that waits for existing buffering operation and then runs new buffering.
7652
7877
  * This implements the mutex-wait behavior.
7653
7878
  */
7654
- async runAsyncBufferedObservation(record, threadId, unobservedMessages, bufferKey, writer, requestContext) {
7879
+ async runAsyncBufferedObservation(record, threadId, unobservedMessages, bufferKey, writer, requestContext, observabilityContext) {
7655
7880
  const existingOp = BufferingCoordinator.asyncBufferingOps.get(bufferKey);
7656
7881
  if (existingOp) {
7657
7882
  try {
@@ -7723,7 +7948,8 @@ ${grouped}` : grouped;
7723
7948
  cycleId,
7724
7949
  startedAt,
7725
7950
  writer,
7726
- requestContext
7951
+ requestContext,
7952
+ observabilityContext
7727
7953
  }).run();
7728
7954
  const maxTs = this.getMaxMessageTimestamp(messagesToBuffer);
7729
7955
  const cursor = new Date(maxTs.getTime() + 1);
@@ -8270,7 +8496,7 @@ ${grouped}` : grouped;
8270
8496
  */
8271
8497
  /** @internal Used by ObservationStep. */
8272
8498
  async buffer(opts) {
8273
- const { threadId, resourceId, requestContext } = opts;
8499
+ const { threadId, resourceId, requestContext, observabilityContext } = opts;
8274
8500
  let record = opts.record ?? await this.getOrCreateRecord(threadId, resourceId);
8275
8501
  if (!this.buffering.isAsyncObservationEnabled()) {
8276
8502
  return { buffered: false, record };
@@ -8366,7 +8592,8 @@ ${grouped}` : grouped;
8366
8592
  cycleId,
8367
8593
  startedAt,
8368
8594
  writer,
8369
- requestContext
8595
+ requestContext,
8596
+ observabilityContext
8370
8597
  }).run();
8371
8598
  await this.storage.setBufferingObservationFlag(record.id, false, newTokens).catch(() => {
8372
8599
  });
@@ -8570,7 +8797,8 @@ ${grouped}` : grouped;
8570
8797
  messages: unobservedMessages,
8571
8798
  reflectionHooks,
8572
8799
  requestContext,
8573
- writer: opts.writer
8800
+ writer: opts.writer,
8801
+ observabilityContext: opts.observabilityContext
8574
8802
  }).run();
8575
8803
  observed = true;
8576
8804
  } finally {
@@ -8592,7 +8820,7 @@ ${grouped}` : grouped;
8592
8820
  * );
8593
8821
  * ```
8594
8822
  */
8595
- async reflect(threadId, resourceId, prompt, requestContext) {
8823
+ async reflect(threadId, resourceId, prompt, requestContext, observabilityContext) {
8596
8824
  const record = await this.getOrCreateRecord(threadId, resourceId);
8597
8825
  if (!record.activeObservations) {
8598
8826
  return { reflected: false, record };
@@ -8609,7 +8837,9 @@ ${grouped}` : grouped;
8609
8837
  void 0,
8610
8838
  void 0,
8611
8839
  void 0,
8612
- requestContext
8840
+ requestContext,
8841
+ observabilityContext,
8842
+ void 0
8613
8843
  );
8614
8844
  const reflectionTokenCount = this.tokenCounter.countObservations(reflectResult.observations);
8615
8845
  await this.storage.createReflectionGeneration({
@@ -8716,6 +8946,7 @@ ${grouped}` : grouped;
8716
8946
  threadId: opts.threadId,
8717
8947
  resourceId: opts.resourceId,
8718
8948
  messageList: opts.messageList,
8949
+ observabilityContext: opts.observabilityContext,
8719
8950
  hooks: opts.hooks
8720
8951
  });
8721
8952
  }
@@ -8964,6 +9195,17 @@ function writeProcessInputStepReproCapture(params) {
8964
9195
  params.debug?.(`[OM:repro-capture] failed to write processInputStep capture: ${String(error)}`);
8965
9196
  }
8966
9197
  }
9198
+ function getOmObservabilityContext(args) {
9199
+ if (!args.tracing || !args.tracingContext || !args.loggerVNext || !args.metrics) {
9200
+ return void 0;
9201
+ }
9202
+ return {
9203
+ tracing: args.tracing,
9204
+ tracingContext: args.tracingContext,
9205
+ loggerVNext: args.loggerVNext,
9206
+ metrics: args.metrics
9207
+ };
9208
+ }
8967
9209
  var ObservationalMemoryProcessor = class {
8968
9210
  id = "observational-memory";
8969
9211
  name = "Observational Memory";
@@ -9021,6 +9263,7 @@ var ObservationalMemoryProcessor = class {
9021
9263
  threadId,
9022
9264
  resourceId,
9023
9265
  messageList,
9266
+ observabilityContext: getOmObservabilityContext(args),
9024
9267
  hooks: {
9025
9268
  onBufferChunkSealed: rotateResponseMessageId
9026
9269
  }
@@ -9030,6 +9273,9 @@ var ObservationalMemoryProcessor = class {
9030
9273
  await this.turn.start(this.memory);
9031
9274
  state.__omTurn = this.turn;
9032
9275
  }
9276
+ const observabilityContext = getOmObservabilityContext(args);
9277
+ state.__omObservabilityContext = observabilityContext;
9278
+ this.turn.observabilityContext = observabilityContext;
9033
9279
  {
9034
9280
  const step = this.turn.step(stepNumber);
9035
9281
  let ctx;
@@ -9110,6 +9356,8 @@ var ObservationalMemoryProcessor = class {
9110
9356
  const state = _state ?? {};
9111
9357
  const context = this.engine.getThreadContext(requestContext, messageList);
9112
9358
  if (!context) return messageList;
9359
+ const observabilityContext = getOmObservabilityContext(args);
9360
+ state.__omObservabilityContext = observabilityContext;
9113
9361
  return this.engine.getTokenCounter().runWithModelContext(state.__omActorModelContext, async () => {
9114
9362
  const memoryContext = parseMemoryRequestContext(requestContext);
9115
9363
  if (memoryContext?.memoryConfig?.readOnly) return messageList;
@@ -9157,5 +9405,5 @@ function getObservationsAsOf(activeObservations, asOf) {
9157
9405
  }
9158
9406
 
9159
9407
  export { ModelByInputTokens, OBSERVER_SYSTEM_PROMPT, ObservationalMemory, ObservationalMemoryProcessor, TokenCounter, buildObserverPrompt, buildObserverSystemPrompt, combineObservationGroupRanges, deriveObservationGroupProvenance, e, estimateTokenCount, extractCurrentTask, formatMessagesForObserver, formatToolResultForObserver, getObservationsAsOf, hasCurrentTaskSection, injectAnchorIds, optimizeObservationsForContext, parseAnchorId, parseObservationGroups, parseObserverOutput, reconcileObservationGroupsFromReflection, renderObservationGroupsForReflection, resolveToolResultValue, stripEphemeralAnchorIds, stripObservationGroups, truncateStringByTokens, wrapInObservationGroup };
9160
- //# sourceMappingURL=chunk-PVUR75AM.js.map
9161
- //# sourceMappingURL=chunk-PVUR75AM.js.map
9408
+ //# sourceMappingURL=chunk-YUTITKH2.js.map
9409
+ //# sourceMappingURL=chunk-YUTITKH2.js.map