@mastra/core 0.10.13 → 0.10.14

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