@mastra/core 0.10.12 → 0.10.13

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 (235) hide show
  1. package/package.json +1 -1
  2. package/a2a.d.ts +0 -1
  3. package/agent.d.ts +0 -1
  4. package/base.d.ts +0 -1
  5. package/bundler.d.ts +0 -1
  6. package/deployer.d.ts +0 -1
  7. package/di.d.ts +0 -1
  8. package/dist/a2a/index.cjs +0 -79
  9. package/dist/a2a/index.d.cts +0 -780
  10. package/dist/a2a/index.d.ts +0 -780
  11. package/dist/a2a/index.js +0 -68
  12. package/dist/agent/index.cjs +0 -15
  13. package/dist/agent/index.d.cts +0 -29
  14. package/dist/agent/index.d.ts +0 -29
  15. package/dist/agent/index.js +0 -2
  16. package/dist/base-B_y9sMg0.d.cts +0 -162
  17. package/dist/base-CS5eSXbL.d.cts +0 -4117
  18. package/dist/base-ClrXcCRx.d.ts +0 -162
  19. package/dist/base-DBhKcre4.d.ts +0 -4117
  20. package/dist/base.cjs +0 -10
  21. package/dist/base.d.cts +0 -6
  22. package/dist/base.d.ts +0 -6
  23. package/dist/base.js +0 -1
  24. package/dist/bundler/index.cjs +0 -10
  25. package/dist/bundler/index.d.cts +0 -30
  26. package/dist/bundler/index.d.ts +0 -30
  27. package/dist/bundler/index.js +0 -1
  28. package/dist/chunk-2GRYVZ2O.cjs +0 -244
  29. package/dist/chunk-2HDFKWFU.js +0 -2249
  30. package/dist/chunk-32C7JDIZ.js +0 -1
  31. package/dist/chunk-4UBOJFSL.js +0 -989
  32. package/dist/chunk-4UWPFBC6.js +0 -88
  33. package/dist/chunk-4Z3OU5RY.cjs +0 -31
  34. package/dist/chunk-5HTMDAXP.js +0 -359
  35. package/dist/chunk-5IEKR756.js +0 -53
  36. package/dist/chunk-5YDTZN2X.js +0 -114
  37. package/dist/chunk-6UNGH46J.js +0 -75
  38. package/dist/chunk-6Y4UL5Z6.cjs +0 -94
  39. package/dist/chunk-7F6BQXE2.cjs +0 -425
  40. package/dist/chunk-7H2GET5Z.cjs +0 -668
  41. package/dist/chunk-7HZ6NIAF.cjs +0 -2
  42. package/dist/chunk-7MZNOW6W.cjs +0 -2263
  43. package/dist/chunk-7XQIPES3.js +0 -668
  44. package/dist/chunk-AKYTYALY.js +0 -70
  45. package/dist/chunk-ATXBSEFT.js +0 -22
  46. package/dist/chunk-B6TOBUS6.cjs +0 -80
  47. package/dist/chunk-B7SQOKEC.cjs +0 -91
  48. package/dist/chunk-BB4KXGBU.js +0 -83
  49. package/dist/chunk-BB6DPGIV.cjs +0 -6
  50. package/dist/chunk-C4LMN2IR.js +0 -27
  51. package/dist/chunk-DFFVEKIG.js +0 -407
  52. package/dist/chunk-E7AX3U6M.cjs +0 -659
  53. package/dist/chunk-EWCOOO3H.js +0 -5007
  54. package/dist/chunk-F2WMR75C.cjs +0 -183
  55. package/dist/chunk-FBKJ3652.cjs +0 -5034
  56. package/dist/chunk-FEYYOBBG.cjs +0 -24
  57. package/dist/chunk-FL5SZ2XU.js +0 -181
  58. package/dist/chunk-GH2KM66J.js +0 -37
  59. package/dist/chunk-GWFS5DAR.cjs +0 -37
  60. package/dist/chunk-HNEE7IF4.js +0 -60
  61. package/dist/chunk-HSVOEWAM.cjs +0 -2
  62. package/dist/chunk-J52TXHZV.cjs +0 -73
  63. package/dist/chunk-JNMQKJH4.js +0 -10
  64. package/dist/chunk-JQOMTERC.js +0 -89
  65. package/dist/chunk-LABUWBKX.cjs +0 -71
  66. package/dist/chunk-LXFZUKP3.cjs +0 -34
  67. package/dist/chunk-MP2QBLUJ.cjs +0 -70
  68. package/dist/chunk-MUNFCOMB.cjs +0 -62
  69. package/dist/chunk-NH5WJNNS.js +0 -1
  70. package/dist/chunk-P3Q73CAW.cjs +0 -55
  71. package/dist/chunk-PA2YIVIT.js +0 -61
  72. package/dist/chunk-QFTBW7ZZ.cjs +0 -2
  73. package/dist/chunk-QQ5K5TZE.cjs +0 -619
  74. package/dist/chunk-QUSEDVYI.cjs +0 -991
  75. package/dist/chunk-R4V75T7J.js +0 -1
  76. package/dist/chunk-SGGPJWRQ.js +0 -69
  77. package/dist/chunk-ST5RMVLG.cjs +0 -87
  78. package/dist/chunk-TC2SCOTE.js +0 -605
  79. package/dist/chunk-U64IJDC5.cjs +0 -109
  80. package/dist/chunk-UX3B6S2I.cjs +0 -65
  81. package/dist/chunk-V5D2LIF5.js +0 -68
  82. package/dist/chunk-VG4OPO2R.js +0 -240
  83. package/dist/chunk-WQNOATKB.js +0 -103
  84. package/dist/chunk-Y7D2JLKS.js +0 -4
  85. package/dist/chunk-YJEHXYK5.js +0 -657
  86. package/dist/chunk-YOQP5T77.js +0 -32
  87. package/dist/chunk-ZIZ3CVHN.cjs +0 -120
  88. package/dist/chunk-ZPOUMTTH.cjs +0 -362
  89. package/dist/chunk-ZZLBNB3U.cjs +0 -12
  90. package/dist/deployer/index.cjs +0 -10
  91. package/dist/deployer/index.d.cts +0 -19
  92. package/dist/deployer/index.d.ts +0 -19
  93. package/dist/deployer/index.js +0 -1
  94. package/dist/di/index.cjs +0 -10
  95. package/dist/di/index.d.cts +0 -1
  96. package/dist/di/index.d.ts +0 -1
  97. package/dist/di/index.js +0 -1
  98. package/dist/error/index.cjs +0 -22
  99. package/dist/error/index.d.cts +0 -86
  100. package/dist/error/index.d.ts +0 -86
  101. package/dist/error/index.js +0 -1
  102. package/dist/eval/index.cjs +0 -14
  103. package/dist/eval/index.d.cts +0 -43
  104. package/dist/eval/index.d.ts +0 -43
  105. package/dist/eval/index.js +0 -1
  106. package/dist/hooks/index.cjs +0 -18
  107. package/dist/hooks/index.d.cts +0 -33
  108. package/dist/hooks/index.d.ts +0 -33
  109. package/dist/hooks/index.js +0 -1
  110. package/dist/index.cjs +0 -281
  111. package/dist/index.d.cts +0 -92
  112. package/dist/index.d.ts +0 -92
  113. package/dist/index.js +0 -112
  114. package/dist/integration/index.cjs +0 -14
  115. package/dist/integration/index.d.cts +0 -65
  116. package/dist/integration/index.d.ts +0 -65
  117. package/dist/integration/index.js +0 -1
  118. package/dist/llm/index.cjs +0 -10
  119. package/dist/llm/index.d.cts +0 -29
  120. package/dist/llm/index.d.ts +0 -29
  121. package/dist/llm/index.js +0 -1
  122. package/dist/logger/index.cjs +0 -43
  123. package/dist/logger/index.d.cts +0 -96
  124. package/dist/logger/index.d.ts +0 -96
  125. package/dist/logger/index.js +0 -2
  126. package/dist/logger-B8XXh6ya.d.cts +0 -159
  127. package/dist/logger-Bpa2oLL4.d.ts +0 -159
  128. package/dist/mastra/index.cjs +0 -10
  129. package/dist/mastra/index.d.cts +0 -29
  130. package/dist/mastra/index.d.ts +0 -29
  131. package/dist/mastra/index.js +0 -1
  132. package/dist/mcp/index.cjs +0 -106
  133. package/dist/mcp/index.d.cts +0 -29
  134. package/dist/mcp/index.d.ts +0 -29
  135. package/dist/mcp/index.js +0 -100
  136. package/dist/memory/index.cjs +0 -18
  137. package/dist/memory/index.d.cts +0 -29
  138. package/dist/memory/index.d.ts +0 -29
  139. package/dist/memory/index.js +0 -1
  140. package/dist/network/index.cjs +0 -311
  141. package/dist/network/index.d.cts +0 -29
  142. package/dist/network/index.d.ts +0 -29
  143. package/dist/network/index.js +0 -309
  144. package/dist/network/vNext/index.cjs +0 -873
  145. package/dist/network/vNext/index.d.cts +0 -29
  146. package/dist/network/vNext/index.d.ts +0 -29
  147. package/dist/network/vNext/index.js +0 -871
  148. package/dist/relevance/index.cjs +0 -18
  149. package/dist/relevance/index.d.cts +0 -49
  150. package/dist/relevance/index.d.ts +0 -49
  151. package/dist/relevance/index.js +0 -1
  152. package/dist/runtime-context/index.cjs +0 -10
  153. package/dist/runtime-context/index.d.cts +0 -52
  154. package/dist/runtime-context/index.d.ts +0 -52
  155. package/dist/runtime-context/index.js +0 -1
  156. package/dist/server/index.cjs +0 -62
  157. package/dist/server/index.d.cts +0 -52
  158. package/dist/server/index.d.ts +0 -52
  159. package/dist/server/index.js +0 -59
  160. package/dist/storage/index.cjs +0 -336
  161. package/dist/storage/index.d.cts +0 -149
  162. package/dist/storage/index.d.ts +0 -149
  163. package/dist/storage/index.js +0 -303
  164. package/dist/telemetry/index.cjs +0 -30
  165. package/dist/telemetry/index.d.cts +0 -75
  166. package/dist/telemetry/index.d.ts +0 -75
  167. package/dist/telemetry/index.js +0 -1
  168. package/dist/telemetry/otel-vendor.cjs +0 -103
  169. package/dist/telemetry/otel-vendor.d.cts +0 -20
  170. package/dist/telemetry/otel-vendor.d.ts +0 -20
  171. package/dist/telemetry/otel-vendor.js +0 -57
  172. package/dist/tools/index.cjs +0 -18
  173. package/dist/tools/index.d.cts +0 -41
  174. package/dist/tools/index.d.ts +0 -41
  175. package/dist/tools/index.js +0 -1
  176. package/dist/tts/index.cjs +0 -10
  177. package/dist/tts/index.d.cts +0 -28
  178. package/dist/tts/index.d.ts +0 -28
  179. package/dist/tts/index.js +0 -1
  180. package/dist/types-Bo1uigWx.d.cts +0 -17
  181. package/dist/types-Bo1uigWx.d.ts +0 -17
  182. package/dist/utils.cjs +0 -58
  183. package/dist/utils.d.cts +0 -149
  184. package/dist/utils.d.ts +0 -149
  185. package/dist/utils.js +0 -1
  186. package/dist/vector/filter/index.cjs +0 -192
  187. package/dist/vector/filter/index.d.cts +0 -128
  188. package/dist/vector/filter/index.d.ts +0 -128
  189. package/dist/vector/filter/index.js +0 -190
  190. package/dist/vector/index.cjs +0 -10
  191. package/dist/vector/index.d.cts +0 -77
  192. package/dist/vector/index.d.ts +0 -77
  193. package/dist/vector/index.js +0 -1
  194. package/dist/voice/index.cjs +0 -18
  195. package/dist/voice/index.d.cts +0 -29
  196. package/dist/voice/index.d.ts +0 -29
  197. package/dist/voice/index.js +0 -1
  198. package/dist/workflows/constants.cjs +0 -10
  199. package/dist/workflows/constants.d.cts +0 -3
  200. package/dist/workflows/constants.d.ts +0 -3
  201. package/dist/workflows/constants.js +0 -1
  202. package/dist/workflows/index.cjs +0 -42
  203. package/dist/workflows/index.d.cts +0 -282
  204. package/dist/workflows/index.d.ts +0 -282
  205. package/dist/workflows/index.js +0 -1
  206. package/dist/workflows/legacy/index.cjs +0 -90
  207. package/dist/workflows/legacy/index.d.cts +0 -91
  208. package/dist/workflows/legacy/index.d.ts +0 -91
  209. package/dist/workflows/legacy/index.js +0 -1
  210. package/error.d.ts +0 -1
  211. package/eval.d.ts +0 -1
  212. package/hooks.d.ts +0 -1
  213. package/integration.d.ts +0 -1
  214. package/llm.d.ts +0 -1
  215. package/logger.d.ts +0 -1
  216. package/mastra.d.ts +0 -1
  217. package/mcp.d.ts +0 -1
  218. package/memory.d.ts +0 -1
  219. package/network/vNext.d.ts +0 -1
  220. package/network.d.ts +0 -1
  221. package/relevance.d.ts +0 -1
  222. package/runtime-context.d.ts +0 -1
  223. package/server.d.ts +0 -1
  224. package/storage.d.ts +0 -1
  225. package/telemetry/otel-vendor.d.ts +0 -1
  226. package/telemetry.d.ts +0 -1
  227. package/tools.d.ts +0 -1
  228. package/tts.d.ts +0 -1
  229. package/utils.d.ts +0 -1
  230. package/vector/filter.d.ts +0 -1
  231. package/vector.d.ts +0 -1
  232. package/voice.d.ts +0 -1
  233. package/workflows/_constants.d.ts +0 -1
  234. package/workflows/legacy.d.ts +0 -1
  235. package/workflows.d.ts +0 -1
@@ -1,605 +0,0 @@
1
- import { MastraError } from './chunk-6UNGH46J.js';
2
- import { trace, propagation, context, SpanStatusCode, SpanKind } from '@opentelemetry/api';
3
- import { ExportResultCode } from '@opentelemetry/core';
4
- import { JsonTraceSerializer } from '@opentelemetry/otlp-transformer';
5
-
6
- function hasActiveTelemetry(tracerName = "default-tracer") {
7
- try {
8
- return !!trace.getTracer(tracerName);
9
- } catch {
10
- return false;
11
- }
12
- }
13
- function getBaggageValues(ctx) {
14
- const currentBaggage = propagation.getBaggage(ctx);
15
- const requestId = currentBaggage?.getEntry("http.request_id")?.value;
16
- const componentName = currentBaggage?.getEntry("componentName")?.value;
17
- const runId = currentBaggage?.getEntry("runId")?.value;
18
- return {
19
- requestId,
20
- componentName,
21
- runId
22
- };
23
- }
24
-
25
- // src/telemetry/telemetry.decorators.ts
26
- function withSpan(options) {
27
- return function(_target, propertyKey, descriptor) {
28
- if (!descriptor || typeof descriptor === "number") return;
29
- const originalMethod = descriptor.value;
30
- const methodName = String(propertyKey);
31
- descriptor.value = function(...args) {
32
- if (options?.skipIfNoTelemetry && !hasActiveTelemetry(options?.tracerName)) {
33
- return originalMethod.apply(this, args);
34
- }
35
- const tracer = trace.getTracer(options?.tracerName ?? "default-tracer");
36
- let spanName;
37
- let spanKind;
38
- if (typeof options === "string") {
39
- spanName = options;
40
- } else if (options) {
41
- spanName = options.spanName || methodName;
42
- spanKind = options.spanKind;
43
- } else {
44
- spanName = methodName;
45
- }
46
- const span = tracer.startSpan(spanName, { kind: spanKind });
47
- let ctx = trace.setSpan(context.active(), span);
48
- args.forEach((arg, index) => {
49
- try {
50
- span.setAttribute(`${spanName}.argument.${index}`, JSON.stringify(arg));
51
- } catch {
52
- span.setAttribute(`${spanName}.argument.${index}`, "[Not Serializable]");
53
- }
54
- });
55
- const { requestId, componentName, runId } = getBaggageValues(ctx);
56
- if (requestId) {
57
- span.setAttribute("http.request_id", requestId);
58
- }
59
- if (componentName) {
60
- span.setAttribute("componentName", componentName);
61
- span.setAttribute("runId", runId);
62
- } else if (this && this.name) {
63
- span.setAttribute("componentName", this.name);
64
- span.setAttribute("runId", this.runId);
65
- ctx = propagation.setBaggage(
66
- ctx,
67
- propagation.createBaggage({
68
- // @ts-ignore
69
- componentName: { value: this.name },
70
- // @ts-ignore
71
- runId: { value: this.runId },
72
- // @ts-ignore
73
- "http.request_id": { value: requestId }
74
- })
75
- );
76
- }
77
- let result;
78
- try {
79
- result = context.with(ctx, () => originalMethod.apply(this, args));
80
- if (result instanceof Promise) {
81
- return result.then((resolvedValue) => {
82
- try {
83
- span.setAttribute(`${spanName}.result`, JSON.stringify(resolvedValue));
84
- } catch {
85
- span.setAttribute(`${spanName}.result`, "[Not Serializable]");
86
- }
87
- return resolvedValue;
88
- }).finally(() => span.end());
89
- }
90
- try {
91
- span.setAttribute(`${spanName}.result`, JSON.stringify(result));
92
- } catch {
93
- span.setAttribute(`${spanName}.result`, "[Not Serializable]");
94
- }
95
- return result;
96
- } catch (error) {
97
- span.setStatus({
98
- code: SpanStatusCode.ERROR,
99
- message: error instanceof Error ? error.message : "Unknown error"
100
- });
101
- if (error instanceof Error) {
102
- span.recordException(error);
103
- }
104
- throw error;
105
- } finally {
106
- if (!(result instanceof Promise)) {
107
- span.end();
108
- }
109
- }
110
- };
111
- return descriptor;
112
- };
113
- }
114
- function InstrumentClass(options) {
115
- return function(target) {
116
- const methods = Object.getOwnPropertyNames(target.prototype);
117
- methods.forEach((method) => {
118
- if (options?.excludeMethods?.includes(method) || method === "constructor") return;
119
- if (options?.methodFilter && !options.methodFilter(method)) return;
120
- const descriptor = Object.getOwnPropertyDescriptor(target.prototype, method);
121
- if (descriptor && typeof descriptor.value === "function") {
122
- Object.defineProperty(
123
- target.prototype,
124
- method,
125
- withSpan({
126
- spanName: options?.prefix ? `${options.prefix}.${method}` : method,
127
- skipIfNoTelemetry: true,
128
- spanKind: options?.spanKind || SpanKind.INTERNAL,
129
- tracerName: options?.tracerName
130
- })(target, method, descriptor)
131
- );
132
- }
133
- });
134
- return target;
135
- };
136
- }
137
-
138
- // src/storage/constants.ts
139
- var TABLE_WORKFLOW_SNAPSHOT = "mastra_workflow_snapshot";
140
- var TABLE_EVALS = "mastra_evals";
141
- var TABLE_MESSAGES = "mastra_messages";
142
- var TABLE_THREADS = "mastra_threads";
143
- var TABLE_TRACES = "mastra_traces";
144
- var TABLE_RESOURCES = "mastra_resources";
145
- var TABLE_SCHEMAS = {
146
- [TABLE_WORKFLOW_SNAPSHOT]: {
147
- workflow_name: {
148
- type: "text"
149
- },
150
- run_id: {
151
- type: "text"
152
- },
153
- resourceId: { type: "text", nullable: true },
154
- snapshot: {
155
- type: "text"
156
- },
157
- createdAt: {
158
- type: "timestamp"
159
- },
160
- updatedAt: {
161
- type: "timestamp"
162
- }
163
- },
164
- [TABLE_EVALS]: {
165
- input: {
166
- type: "text"
167
- },
168
- output: {
169
- type: "text"
170
- },
171
- result: {
172
- type: "jsonb"
173
- },
174
- agent_name: {
175
- type: "text"
176
- },
177
- metric_name: {
178
- type: "text"
179
- },
180
- instructions: {
181
- type: "text"
182
- },
183
- test_info: {
184
- type: "jsonb",
185
- nullable: true
186
- },
187
- global_run_id: {
188
- type: "text"
189
- },
190
- run_id: {
191
- type: "text"
192
- },
193
- created_at: {
194
- type: "timestamp"
195
- },
196
- createdAt: {
197
- type: "timestamp",
198
- nullable: true
199
- }
200
- },
201
- [TABLE_THREADS]: {
202
- id: { type: "text", nullable: false, primaryKey: true },
203
- resourceId: { type: "text", nullable: false },
204
- title: { type: "text", nullable: false },
205
- metadata: { type: "text", nullable: true },
206
- createdAt: { type: "timestamp", nullable: false },
207
- updatedAt: { type: "timestamp", nullable: false }
208
- },
209
- [TABLE_MESSAGES]: {
210
- id: { type: "text", nullable: false, primaryKey: true },
211
- thread_id: { type: "text", nullable: false },
212
- content: { type: "text", nullable: false },
213
- role: { type: "text", nullable: false },
214
- type: { type: "text", nullable: false },
215
- createdAt: { type: "timestamp", nullable: false },
216
- resourceId: { type: "text", nullable: true }
217
- },
218
- [TABLE_TRACES]: {
219
- id: { type: "text", nullable: false, primaryKey: true },
220
- parentSpanId: { type: "text", nullable: true },
221
- name: { type: "text", nullable: false },
222
- traceId: { type: "text", nullable: false },
223
- scope: { type: "text", nullable: false },
224
- kind: { type: "integer", nullable: false },
225
- attributes: { type: "jsonb", nullable: true },
226
- status: { type: "jsonb", nullable: true },
227
- events: { type: "jsonb", nullable: true },
228
- links: { type: "jsonb", nullable: true },
229
- other: { type: "text", nullable: true },
230
- startTime: { type: "bigint", nullable: false },
231
- endTime: { type: "bigint", nullable: false },
232
- createdAt: { type: "timestamp", nullable: false }
233
- },
234
- [TABLE_RESOURCES]: {
235
- id: { type: "text", nullable: false, primaryKey: true },
236
- workingMemory: { type: "text", nullable: true },
237
- metadata: { type: "jsonb", nullable: true },
238
- createdAt: { type: "timestamp", nullable: false },
239
- updatedAt: { type: "timestamp", nullable: false }
240
- }
241
- };
242
-
243
- // src/telemetry/storage-exporter.ts
244
- var OTLPTraceExporter = class {
245
- storage;
246
- queue = [];
247
- serializer;
248
- logger;
249
- activeFlush = void 0;
250
- constructor({ logger, storage }) {
251
- this.storage = storage;
252
- this.serializer = JsonTraceSerializer;
253
- this.logger = logger;
254
- }
255
- export(internalRepresentation, resultCallback) {
256
- const serializedRequest = this.serializer.serializeRequest(internalRepresentation);
257
- const payload = JSON.parse(Buffer.from(serializedRequest.buffer, "utf8"));
258
- const items = payload?.resourceSpans?.[0]?.scopeSpans;
259
- this.logger.debug(`Exporting telemetry: ${items.length} scope spans to be processed [trace batch]`);
260
- this.queue.push({ data: items, resultCallback });
261
- if (!this.activeFlush) {
262
- this.activeFlush = this.flush();
263
- }
264
- }
265
- shutdown() {
266
- return this.forceFlush();
267
- }
268
- flush() {
269
- const now = /* @__PURE__ */ new Date();
270
- const items = this.queue.shift();
271
- if (!items) return Promise.resolve();
272
- const allSpans = items.data.reduce((acc, scopedSpans) => {
273
- const { scope, spans } = scopedSpans;
274
- for (const span of spans) {
275
- const {
276
- spanId,
277
- parentSpanId,
278
- traceId,
279
- name,
280
- kind,
281
- attributes,
282
- status,
283
- events,
284
- links,
285
- startTimeUnixNano,
286
- endTimeUnixNano,
287
- ...rest
288
- } = span;
289
- const startTime = Number(BigInt(startTimeUnixNano) / 1000n);
290
- const endTime = Number(BigInt(endTimeUnixNano) / 1000n);
291
- acc.push({
292
- id: spanId,
293
- parentSpanId,
294
- traceId,
295
- name,
296
- scope: scope.name,
297
- kind,
298
- status: JSON.stringify(status),
299
- events: JSON.stringify(events),
300
- links: JSON.stringify(links),
301
- attributes: JSON.stringify(
302
- attributes.reduce((acc2, attr) => {
303
- const valueKey = Object.keys(attr.value)[0];
304
- if (valueKey) {
305
- acc2[attr.key] = attr.value[valueKey];
306
- }
307
- return acc2;
308
- }, {})
309
- ),
310
- startTime,
311
- endTime,
312
- other: JSON.stringify(rest),
313
- createdAt: now
314
- });
315
- }
316
- return acc;
317
- }, []);
318
- return this.storage.batchInsert({
319
- tableName: TABLE_TRACES,
320
- records: allSpans
321
- }).then(() => {
322
- items.resultCallback({
323
- code: ExportResultCode.SUCCESS
324
- });
325
- }).catch((e) => {
326
- const mastraError = new MastraError(
327
- {
328
- id: "OTLP_TRACE_EXPORT_FAILURE",
329
- text: "Failed to export telemetry spans",
330
- domain: "MASTRA_TELEMETRY" /* MASTRA_TELEMETRY */,
331
- category: "SYSTEM" /* SYSTEM */,
332
- details: {
333
- attemptedSpanCount: allSpans.length,
334
- targetTable: TABLE_TRACES,
335
- firstSpanName: allSpans.length > 0 ? allSpans[0].name : "",
336
- firstSpanKind: allSpans.length > 0 ? allSpans[0].kind : "",
337
- firstSpanScope: allSpans.length > 0 ? allSpans[0].scope : ""
338
- }
339
- },
340
- e
341
- );
342
- this.logger.trackException(mastraError);
343
- this.logger.error("span err:" + mastraError.toString());
344
- items.resultCallback({
345
- code: ExportResultCode.FAILED,
346
- error: e
347
- });
348
- }).finally(() => {
349
- this.activeFlush = void 0;
350
- });
351
- }
352
- async forceFlush() {
353
- if (!this.queue.length) {
354
- return;
355
- }
356
- await this.activeFlush;
357
- while (this.queue.length) {
358
- await this.flush();
359
- }
360
- }
361
- __setLogger(logger) {
362
- this.logger = logger;
363
- }
364
- };
365
- var Telemetry = class _Telemetry {
366
- tracer = trace.getTracer("default");
367
- name = "default-service";
368
- constructor(config) {
369
- this.name = config.serviceName ?? "default-service";
370
- this.tracer = trace.getTracer(this.name);
371
- }
372
- /**
373
- * @deprecated This method does not do anything
374
- */
375
- async shutdown() {
376
- }
377
- /**
378
- * Initialize telemetry with the given configuration
379
- * @param config - Optional telemetry configuration object
380
- * @returns Telemetry instance that can be used for tracing
381
- */
382
- static init(config = {}) {
383
- try {
384
- if (!globalThis.__TELEMETRY__) {
385
- globalThis.__TELEMETRY__ = new _Telemetry(config);
386
- }
387
- return globalThis.__TELEMETRY__;
388
- } catch (error) {
389
- const wrappedError = new MastraError(
390
- {
391
- id: "TELEMETRY_INIT_FAILED",
392
- text: "Failed to initialize telemetry",
393
- domain: "MASTRA_TELEMETRY" /* MASTRA_TELEMETRY */,
394
- category: "SYSTEM" /* SYSTEM */
395
- },
396
- error
397
- );
398
- throw wrappedError;
399
- }
400
- }
401
- static getActiveSpan() {
402
- const span = trace.getActiveSpan();
403
- return span;
404
- }
405
- /**
406
- * Get the global telemetry instance
407
- * @throws {Error} If telemetry has not been initialized
408
- * @returns {Telemetry} The global telemetry instance
409
- */
410
- static get() {
411
- if (!globalThis.__TELEMETRY__) {
412
- throw new MastraError({
413
- id: "TELEMETRY_GETTER_FAILED_GLOBAL_TELEMETRY_NOT_INITIALIZED",
414
- text: "Telemetry not initialized",
415
- domain: "MASTRA_TELEMETRY" /* MASTRA_TELEMETRY */,
416
- category: "USER" /* USER */
417
- });
418
- }
419
- return globalThis.__TELEMETRY__;
420
- }
421
- /**
422
- * Wraps a class instance with telemetry tracing
423
- * @param instance The class instance to wrap
424
- * @param options Optional configuration for tracing
425
- * @returns Wrapped instance with all methods traced
426
- */
427
- traceClass(instance, options = {}) {
428
- const { skipIfNoTelemetry = true } = options;
429
- if (skipIfNoTelemetry && !hasActiveTelemetry()) {
430
- return instance;
431
- }
432
- const { spanNamePrefix = instance.constructor.name.toLowerCase(), attributes = {}, excludeMethods = [] } = options;
433
- return new Proxy(instance, {
434
- get: (target, prop) => {
435
- const value = target[prop];
436
- if (typeof value === "function" && prop !== "constructor" && !prop.toString().startsWith("_") && !excludeMethods.includes(prop.toString())) {
437
- return this.traceMethod(value.bind(target), {
438
- spanName: `${spanNamePrefix}.${prop.toString()}`,
439
- attributes: {
440
- ...attributes,
441
- [`${spanNamePrefix}.name`]: target.constructor.name,
442
- [`${spanNamePrefix}.method.name`]: prop.toString()
443
- }
444
- });
445
- }
446
- return value;
447
- }
448
- });
449
- }
450
- static setBaggage(baggage, ctx = context.active()) {
451
- const currentBaggage = Object.fromEntries(propagation.getBaggage(ctx)?.getAllEntries() ?? []);
452
- const newCtx = propagation.setBaggage(
453
- ctx,
454
- propagation.createBaggage({
455
- ...currentBaggage,
456
- ...baggage
457
- })
458
- );
459
- return newCtx;
460
- }
461
- static withContext(ctx, fn) {
462
- return context.with(ctx, fn);
463
- }
464
- /**
465
- * method to trace individual methods with proper context
466
- * @param method The method to trace
467
- * @param context Additional context for the trace
468
- * @returns Wrapped method with tracing
469
- */
470
- traceMethod(method, context3) {
471
- let ctx = context.active();
472
- const { skipIfNoTelemetry = true } = context3;
473
- if (skipIfNoTelemetry && !hasActiveTelemetry()) {
474
- return method;
475
- }
476
- return (...args) => {
477
- const span = this.tracer.startSpan(context3.spanName);
478
- function handleError(error) {
479
- span.recordException(error);
480
- span.setStatus({
481
- code: SpanStatusCode.ERROR,
482
- message: error.message
483
- });
484
- span.end();
485
- throw error;
486
- }
487
- try {
488
- let recordResult2 = function(res) {
489
- try {
490
- span.setAttribute(`${context3.spanName}.result`, JSON.stringify(res));
491
- } catch {
492
- span.setAttribute(`${context3.spanName}.result`, "[Not Serializable]");
493
- }
494
- span.end();
495
- return res;
496
- };
497
- const { requestId, componentName, runId } = getBaggageValues(ctx);
498
- if (context3.attributes) {
499
- span.setAttributes(context3.attributes);
500
- }
501
- if (requestId) {
502
- span.setAttribute("http.request_id", requestId);
503
- }
504
- if (context3.attributes?.componentName) {
505
- ctx = propagation.setBaggage(
506
- ctx,
507
- propagation.createBaggage({
508
- componentName: { value: context3.attributes.componentName },
509
- // @ts-ignore
510
- runId: { value: context3.attributes.runId },
511
- // @ts-ignore
512
- "http.request_id": { value: requestId }
513
- })
514
- );
515
- } else {
516
- if (componentName) {
517
- span.setAttribute("componentName", componentName);
518
- span.setAttribute("runId", runId);
519
- } else if (this && this.name) {
520
- span.setAttribute("componentName", this.name);
521
- span.setAttribute("runId", this.runId);
522
- ctx = propagation.setBaggage(
523
- ctx,
524
- propagation.createBaggage({
525
- componentName: { value: this.name },
526
- // @ts-ignore
527
- runId: { value: this.runId },
528
- // @ts-ignore
529
- "http.request_id": { value: requestId }
530
- })
531
- );
532
- }
533
- }
534
- args.forEach((arg, index) => {
535
- try {
536
- span.setAttribute(`${context3.spanName}.argument.${index}`, JSON.stringify(arg));
537
- } catch {
538
- span.setAttribute(`${context3.spanName}.argument.${index}`, "[Not Serializable]");
539
- }
540
- });
541
- let result;
542
- context.with(trace.setSpan(ctx, span), () => {
543
- result = method(...args);
544
- });
545
- if (result instanceof Promise) {
546
- return result.then(recordResult2).catch(handleError);
547
- } else {
548
- return recordResult2(result);
549
- }
550
- } catch (error) {
551
- handleError(error);
552
- }
553
- };
554
- }
555
- getBaggageTracer() {
556
- return new BaggageTracer(this.tracer);
557
- }
558
- };
559
- var BaggageTracer = class {
560
- _tracer;
561
- constructor(tracer) {
562
- this._tracer = tracer;
563
- }
564
- startSpan(name, options = {}, ctx) {
565
- ctx = ctx ?? context.active();
566
- const span = this._tracer.startSpan(name, options, ctx);
567
- const { componentName, runId, requestId } = getBaggageValues(ctx);
568
- span.setAttribute("componentName", componentName);
569
- span.setAttribute("runId", runId);
570
- span.setAttribute("http.request_id", requestId);
571
- return span;
572
- }
573
- startActiveSpan(name, optionsOrFn, ctxOrFn, fn) {
574
- if (typeof optionsOrFn === "function") {
575
- const wrappedFn2 = (span) => {
576
- const { componentName, runId, requestId } = getBaggageValues(context.active());
577
- span.setAttribute("componentName", componentName);
578
- span.setAttribute("runId", runId);
579
- span.setAttribute("http.request_id", requestId);
580
- return optionsOrFn(span);
581
- };
582
- return this._tracer.startActiveSpan(name, {}, context.active(), wrappedFn2);
583
- }
584
- if (typeof ctxOrFn === "function") {
585
- const wrappedFn2 = (span) => {
586
- const { componentName, runId, requestId } = getBaggageValues(context.active());
587
- span.setAttribute("componentName", componentName);
588
- span.setAttribute("runId", runId);
589
- span.setAttribute("http.request_id", requestId);
590
- return ctxOrFn(span);
591
- };
592
- return this._tracer.startActiveSpan(name, optionsOrFn, context.active(), wrappedFn2);
593
- }
594
- const wrappedFn = (span) => {
595
- const { componentName, runId, requestId } = getBaggageValues(ctxOrFn ?? context.active());
596
- span.setAttribute("componentName", componentName);
597
- span.setAttribute("runId", runId);
598
- span.setAttribute("http.request_id", requestId);
599
- return fn(span);
600
- };
601
- return this._tracer.startActiveSpan(name, optionsOrFn, ctxOrFn, wrappedFn);
602
- }
603
- };
604
-
605
- export { InstrumentClass, OTLPTraceExporter, TABLE_EVALS, TABLE_MESSAGES, TABLE_RESOURCES, TABLE_SCHEMAS, TABLE_THREADS, TABLE_TRACES, TABLE_WORKFLOW_SNAPSHOT, Telemetry, getBaggageValues, hasActiveTelemetry, withSpan };