@mastra/core 0.0.0-commonjs-20250227130920
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +180 -0
- package/dist/agent/index.cjs +1865 -0
- package/dist/agent/index.d.cts +15 -0
- package/dist/agent/index.d.ts +15 -0
- package/dist/agent/index.js +1 -0
- package/dist/base-D90KQ4XI.d.ts +139 -0
- package/dist/base-hs9NDAZ2.d.cts +139 -0
- package/dist/base-nKCMCNrM.d.ts +920 -0
- package/dist/base-nhesrHv3.d.cts +920 -0
- package/dist/base.cjs +138 -0
- package/dist/base.d.cts +6 -0
- package/dist/base.d.ts +6 -0
- package/dist/base.js +1 -0
- package/dist/bundler/index.cjs +158 -0
- package/dist/bundler/index.d.cts +28 -0
- package/dist/bundler/index.d.ts +28 -0
- package/dist/bundler/index.js +1 -0
- package/dist/chunk-33GSTUNK.js +620 -0
- package/dist/chunk-4YRYBCOZ.js +10 -0
- package/dist/chunk-55NFNRKO.js +10 -0
- package/dist/chunk-5XPCMNGW.js +215 -0
- package/dist/chunk-B3M27AMP.js +1479 -0
- package/dist/chunk-BB4KXGBU.js +83 -0
- package/dist/chunk-C6A6W6XS.js +77 -0
- package/dist/chunk-HQ55LN2U.js +318 -0
- package/dist/chunk-KNVTCZW7.js +416 -0
- package/dist/chunk-LH47WVJL.js +61 -0
- package/dist/chunk-NGD2HQYW.js +346 -0
- package/dist/chunk-NUDAZEOG.js +35 -0
- package/dist/chunk-OZ4XVJ6F.js +49 -0
- package/dist/chunk-PHMSPCTC.js +145 -0
- package/dist/chunk-PNZK456O.js +88 -0
- package/dist/chunk-QAAJAHDB.js +37 -0
- package/dist/chunk-RG66XEJT.js +8 -0
- package/dist/chunk-SIFBBGY6.js +190 -0
- package/dist/chunk-SVEAENO7.js +22 -0
- package/dist/chunk-SY5244IR.js +1499 -0
- package/dist/chunk-W5HVJX45.js +402 -0
- package/dist/chunk-WIBGG4X6.js +173 -0
- package/dist/chunk-ZDWFBE5L.js +1 -0
- package/dist/chunk-ZINPRHAN.js +22 -0
- package/dist/deployer/index.cjs +165 -0
- package/dist/deployer/index.d.cts +19 -0
- package/dist/deployer/index.d.ts +19 -0
- package/dist/deployer/index.js +1 -0
- package/dist/eval/index.cjs +110 -0
- package/dist/eval/index.d.cts +28 -0
- package/dist/eval/index.d.ts +28 -0
- package/dist/eval/index.js +1 -0
- package/dist/filter/index.cjs +192 -0
- package/dist/filter/index.d.cts +90 -0
- package/dist/filter/index.d.ts +90 -0
- package/dist/filter/index.js +1 -0
- package/dist/hooks/index.cjs +87 -0
- package/dist/hooks/index.d.cts +33 -0
- package/dist/hooks/index.d.ts +33 -0
- package/dist/hooks/index.js +1 -0
- package/dist/index-mKY1XrpK.d.cts +90 -0
- package/dist/index-mKY1XrpK.d.ts +90 -0
- package/dist/index.cjs +6844 -0
- package/dist/index.d.cts +97 -0
- package/dist/index.d.ts +97 -0
- package/dist/index.js +119 -0
- package/dist/integration/index.cjs +113 -0
- package/dist/integration/index.d.cts +52 -0
- package/dist/integration/index.d.ts +52 -0
- package/dist/integration/index.js +1 -0
- package/dist/llm/index.cjs +2 -0
- package/dist/llm/index.d.cts +15 -0
- package/dist/llm/index.d.ts +15 -0
- package/dist/llm/index.js +1 -0
- package/dist/logger/index.cjs +159 -0
- package/dist/logger/index.d.cts +3 -0
- package/dist/logger/index.d.ts +3 -0
- package/dist/logger/index.js +1 -0
- package/dist/mastra/index.cjs +1741 -0
- package/dist/mastra/index.d.cts +67 -0
- package/dist/mastra/index.d.ts +67 -0
- package/dist/mastra/index.js +1 -0
- package/dist/memory/index.cjs +1907 -0
- package/dist/memory/index.d.cts +15 -0
- package/dist/memory/index.d.ts +15 -0
- package/dist/memory/index.js +1 -0
- package/dist/relevance/index.cjs +1927 -0
- package/dist/relevance/index.d.cts +21 -0
- package/dist/relevance/index.d.ts +21 -0
- package/dist/relevance/index.js +1 -0
- package/dist/storage/index.cjs +361 -0
- package/dist/storage/index.d.cts +15 -0
- package/dist/storage/index.d.ts +15 -0
- package/dist/storage/index.js +2 -0
- package/dist/storage/libsql/index.cjs +770 -0
- package/dist/storage/libsql/index.d.cts +81 -0
- package/dist/storage/libsql/index.d.ts +81 -0
- package/dist/storage/libsql/index.js +1 -0
- package/dist/telemetry/index.cjs +413 -0
- package/dist/telemetry/index.d.cts +51 -0
- package/dist/telemetry/index.d.ts +51 -0
- package/dist/telemetry/index.js +1 -0
- package/dist/telemetry/otel-vendor.cjs +52 -0
- package/dist/telemetry/otel-vendor.d.cts +7 -0
- package/dist/telemetry/otel-vendor.d.ts +7 -0
- package/dist/telemetry/otel-vendor.js +7 -0
- package/dist/tools/index.cjs +25 -0
- package/dist/tools/index.d.cts +29 -0
- package/dist/tools/index.d.ts +29 -0
- package/dist/tools/index.js +1 -0
- package/dist/tts/index.cjs +328 -0
- package/dist/tts/index.d.cts +28 -0
- package/dist/tts/index.d.ts +28 -0
- package/dist/tts/index.js +1 -0
- package/dist/types-m9RryK9a.d.cts +14 -0
- package/dist/types-m9RryK9a.d.ts +14 -0
- package/dist/utils.cjs +179 -0
- package/dist/utils.d.cts +26 -0
- package/dist/utils.d.ts +26 -0
- package/dist/utils.js +1 -0
- package/dist/vector/index.cjs +145 -0
- package/dist/vector/index.d.cts +30 -0
- package/dist/vector/index.d.ts +30 -0
- package/dist/vector/index.js +1 -0
- package/dist/vector/libsql/index.cjs +951 -0
- package/dist/vector/libsql/index.d.cts +29 -0
- package/dist/vector/libsql/index.d.ts +29 -0
- package/dist/vector/libsql/index.js +1 -0
- package/dist/voice/index.cjs +369 -0
- package/dist/voice/index.d.cts +67 -0
- package/dist/voice/index.d.ts +67 -0
- package/dist/voice/index.js +76 -0
- package/dist/workflow-DqQ4pON_.d.cts +84 -0
- package/dist/workflow-Ng_F_Zaf.d.ts +84 -0
- package/dist/workflows/index.cjs +1628 -0
- package/dist/workflows/index.d.cts +48 -0
- package/dist/workflows/index.d.ts +48 -0
- package/dist/workflows/index.js +1 -0
- package/package.json +162 -0
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import { TABLE_TRACES } from './chunk-RG66XEJT.js';
|
|
2
|
+
import { trace, context, propagation, 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
|
+
|
|
14
|
+
// src/telemetry/telemetry.decorators.ts
|
|
15
|
+
function withSpan(options) {
|
|
16
|
+
return function(_target, propertyKey, descriptor) {
|
|
17
|
+
if (!descriptor || typeof descriptor === "number") return;
|
|
18
|
+
const originalMethod = descriptor.value;
|
|
19
|
+
const methodName = String(propertyKey);
|
|
20
|
+
descriptor.value = function(...args) {
|
|
21
|
+
if (options?.skipIfNoTelemetry && !hasActiveTelemetry(options?.tracerName)) {
|
|
22
|
+
return originalMethod.apply(this, args);
|
|
23
|
+
}
|
|
24
|
+
const tracer = trace.getTracer(options?.tracerName ?? "default-tracer");
|
|
25
|
+
let spanName;
|
|
26
|
+
let spanKind;
|
|
27
|
+
if (typeof options === "string") {
|
|
28
|
+
spanName = options;
|
|
29
|
+
} else if (options) {
|
|
30
|
+
spanName = options.spanName || methodName;
|
|
31
|
+
spanKind = options.spanKind;
|
|
32
|
+
} else {
|
|
33
|
+
spanName = methodName;
|
|
34
|
+
}
|
|
35
|
+
const span = tracer.startSpan(spanName, { kind: spanKind });
|
|
36
|
+
let ctx = trace.setSpan(context.active(), span);
|
|
37
|
+
args.forEach((arg, index) => {
|
|
38
|
+
try {
|
|
39
|
+
span.setAttribute(`${spanName}.argument.${index}`, JSON.stringify(arg));
|
|
40
|
+
} catch {
|
|
41
|
+
span.setAttribute(`${spanName}.argument.${index}`, "[Not Serializable]");
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
const currentBaggage = propagation.getBaggage(ctx);
|
|
45
|
+
if (currentBaggage?.componentName) {
|
|
46
|
+
span.setAttribute("componentName", currentBaggage?.componentName);
|
|
47
|
+
span.setAttribute("runId", currentBaggage?.runId);
|
|
48
|
+
} else if (this && this.name) {
|
|
49
|
+
span.setAttribute("componentName", this.name);
|
|
50
|
+
span.setAttribute("runId", this.runId);
|
|
51
|
+
ctx = propagation.setBaggage(ctx, { componentName: this.name, runId: this.runId });
|
|
52
|
+
}
|
|
53
|
+
let result;
|
|
54
|
+
try {
|
|
55
|
+
result = context.with(ctx, () => originalMethod.apply(this, args));
|
|
56
|
+
if (result instanceof Promise) {
|
|
57
|
+
return result.then((resolvedValue) => {
|
|
58
|
+
try {
|
|
59
|
+
span.setAttribute(`${spanName}.result`, JSON.stringify(resolvedValue));
|
|
60
|
+
} catch {
|
|
61
|
+
span.setAttribute(`${spanName}.result`, "[Not Serializable]");
|
|
62
|
+
}
|
|
63
|
+
return resolvedValue;
|
|
64
|
+
}).finally(() => span.end());
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
span.setAttribute(`${spanName}.result`, JSON.stringify(result));
|
|
68
|
+
} catch {
|
|
69
|
+
span.setAttribute(`${spanName}.result`, "[Not Serializable]");
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
} catch (error) {
|
|
73
|
+
span.setStatus({
|
|
74
|
+
code: SpanStatusCode.ERROR,
|
|
75
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
76
|
+
});
|
|
77
|
+
if (error instanceof Error) {
|
|
78
|
+
span.recordException(error);
|
|
79
|
+
}
|
|
80
|
+
throw error;
|
|
81
|
+
} finally {
|
|
82
|
+
if (!(result instanceof Promise)) {
|
|
83
|
+
span.end();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
return descriptor;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function InstrumentClass(options) {
|
|
91
|
+
return function(target) {
|
|
92
|
+
const methods = Object.getOwnPropertyNames(target.prototype);
|
|
93
|
+
methods.forEach((method) => {
|
|
94
|
+
if (options?.excludeMethods?.includes(method) || method === "constructor") return;
|
|
95
|
+
if (options?.methodFilter && !options.methodFilter(method)) return;
|
|
96
|
+
const descriptor = Object.getOwnPropertyDescriptor(target.prototype, method);
|
|
97
|
+
if (descriptor && typeof descriptor.value === "function") {
|
|
98
|
+
Object.defineProperty(
|
|
99
|
+
target.prototype,
|
|
100
|
+
method,
|
|
101
|
+
withSpan({
|
|
102
|
+
spanName: options?.prefix ? `${options.prefix}.${method}` : method,
|
|
103
|
+
skipIfNoTelemetry: true,
|
|
104
|
+
spanKind: options?.spanKind || SpanKind.INTERNAL,
|
|
105
|
+
tracerName: options?.tracerName
|
|
106
|
+
})(target, method, descriptor)
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
return target;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
var OTLPTraceExporter = class {
|
|
114
|
+
storage;
|
|
115
|
+
queue = [];
|
|
116
|
+
serializer;
|
|
117
|
+
logger;
|
|
118
|
+
activeFlush = void 0;
|
|
119
|
+
constructor({ logger, storage }) {
|
|
120
|
+
this.storage = storage;
|
|
121
|
+
this.serializer = JsonTraceSerializer;
|
|
122
|
+
this.logger = logger;
|
|
123
|
+
}
|
|
124
|
+
export(internalRepresentation, resultCallback) {
|
|
125
|
+
const serializedRequest = this.serializer.serializeRequest(internalRepresentation);
|
|
126
|
+
const payload = JSON.parse(Buffer.from(serializedRequest.buffer, "utf8"));
|
|
127
|
+
const items = payload?.resourceSpans?.[0]?.scopeSpans;
|
|
128
|
+
this.logger.debug(`Exporting telemetry: ${items.length} scope spans to be processed [trace batch]`);
|
|
129
|
+
this.queue.push({ data: items, resultCallback });
|
|
130
|
+
if (!this.activeFlush) {
|
|
131
|
+
this.activeFlush = this.flush();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
shutdown() {
|
|
135
|
+
return this.forceFlush();
|
|
136
|
+
}
|
|
137
|
+
flush() {
|
|
138
|
+
const now = /* @__PURE__ */ new Date();
|
|
139
|
+
const items = this.queue.shift();
|
|
140
|
+
if (!items) return Promise.resolve();
|
|
141
|
+
const allSpans = items.data.reduce((acc, scopedSpans) => {
|
|
142
|
+
const { scope, spans } = scopedSpans;
|
|
143
|
+
for (const span of spans) {
|
|
144
|
+
const {
|
|
145
|
+
spanId,
|
|
146
|
+
parentSpanId,
|
|
147
|
+
traceId,
|
|
148
|
+
name,
|
|
149
|
+
kind,
|
|
150
|
+
attributes,
|
|
151
|
+
status,
|
|
152
|
+
events,
|
|
153
|
+
links,
|
|
154
|
+
startTimeUnixNano,
|
|
155
|
+
endTimeUnixNano,
|
|
156
|
+
...rest
|
|
157
|
+
} = span;
|
|
158
|
+
const startTime = Number(BigInt(startTimeUnixNano) / 1000n);
|
|
159
|
+
const endTime = Number(BigInt(endTimeUnixNano) / 1000n);
|
|
160
|
+
acc.push({
|
|
161
|
+
id: spanId,
|
|
162
|
+
parentSpanId,
|
|
163
|
+
traceId,
|
|
164
|
+
name,
|
|
165
|
+
scope: scope.name,
|
|
166
|
+
kind,
|
|
167
|
+
status: JSON.stringify(status),
|
|
168
|
+
events: JSON.stringify(events),
|
|
169
|
+
links: JSON.stringify(links),
|
|
170
|
+
attributes: JSON.stringify(
|
|
171
|
+
attributes.reduce((acc2, attr) => {
|
|
172
|
+
const valueKey = Object.keys(attr.value)[0];
|
|
173
|
+
if (valueKey) {
|
|
174
|
+
acc2[attr.key] = attr.value[valueKey];
|
|
175
|
+
}
|
|
176
|
+
return acc2;
|
|
177
|
+
}, {})
|
|
178
|
+
),
|
|
179
|
+
startTime,
|
|
180
|
+
endTime,
|
|
181
|
+
other: JSON.stringify(rest),
|
|
182
|
+
createdAt: now
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return acc;
|
|
186
|
+
}, []);
|
|
187
|
+
return this.storage.__batchInsert({
|
|
188
|
+
tableName: TABLE_TRACES,
|
|
189
|
+
records: allSpans
|
|
190
|
+
}).then(() => {
|
|
191
|
+
items.resultCallback({
|
|
192
|
+
code: ExportResultCode.SUCCESS
|
|
193
|
+
});
|
|
194
|
+
}).catch((e) => {
|
|
195
|
+
this.logger.error("span err:" + e?.message);
|
|
196
|
+
items.resultCallback({
|
|
197
|
+
code: ExportResultCode.FAILED,
|
|
198
|
+
error: e
|
|
199
|
+
});
|
|
200
|
+
}).finally(() => {
|
|
201
|
+
this.activeFlush = void 0;
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
async forceFlush() {
|
|
205
|
+
if (!this.queue.length) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
await this.activeFlush;
|
|
209
|
+
while (this.queue.length) {
|
|
210
|
+
await this.flush();
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
__setLogger(logger) {
|
|
214
|
+
this.logger = logger;
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
var Telemetry = class _Telemetry {
|
|
218
|
+
tracer = trace.getTracer("default");
|
|
219
|
+
name = "default-service";
|
|
220
|
+
constructor(config) {
|
|
221
|
+
this.name = config.serviceName ?? "default-service";
|
|
222
|
+
this.tracer = trace.getTracer(this.name);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* @deprecated This method does not do anything
|
|
226
|
+
*/
|
|
227
|
+
async shutdown() {
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Initialize telemetry with the given configuration
|
|
231
|
+
* @param config - Optional telemetry configuration object
|
|
232
|
+
* @returns Telemetry instance that can be used for tracing
|
|
233
|
+
*/
|
|
234
|
+
static init(config = {}) {
|
|
235
|
+
try {
|
|
236
|
+
if (!global.__TELEMETRY__) {
|
|
237
|
+
global.__TELEMETRY__ = new _Telemetry(config);
|
|
238
|
+
}
|
|
239
|
+
return global.__TELEMETRY__;
|
|
240
|
+
} catch (error) {
|
|
241
|
+
console.error("Failed to initialize telemetry:", error);
|
|
242
|
+
throw error;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Get the global telemetry instance
|
|
247
|
+
* @throws {Error} If telemetry has not been initialized
|
|
248
|
+
* @returns {Telemetry} The global telemetry instance
|
|
249
|
+
*/
|
|
250
|
+
static get() {
|
|
251
|
+
if (!global.__TELEMETRY__) {
|
|
252
|
+
throw new Error("Telemetry not initialized");
|
|
253
|
+
}
|
|
254
|
+
return global.__TELEMETRY__;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Wraps a class instance with telemetry tracing
|
|
258
|
+
* @param instance The class instance to wrap
|
|
259
|
+
* @param options Optional configuration for tracing
|
|
260
|
+
* @returns Wrapped instance with all methods traced
|
|
261
|
+
*/
|
|
262
|
+
traceClass(instance, options = {}) {
|
|
263
|
+
const { skipIfNoTelemetry = true } = options;
|
|
264
|
+
if (skipIfNoTelemetry && !hasActiveTelemetry()) {
|
|
265
|
+
return instance;
|
|
266
|
+
}
|
|
267
|
+
const { spanNamePrefix = instance.constructor.name.toLowerCase(), attributes = {}, excludeMethods = [] } = options;
|
|
268
|
+
return new Proxy(instance, {
|
|
269
|
+
get: (target, prop) => {
|
|
270
|
+
const value = target[prop];
|
|
271
|
+
if (typeof value === "function" && prop !== "constructor" && !prop.toString().startsWith("_") && !excludeMethods.includes(prop.toString())) {
|
|
272
|
+
return this.traceMethod(value.bind(target), {
|
|
273
|
+
spanName: `${spanNamePrefix}.${prop.toString()}`,
|
|
274
|
+
attributes: {
|
|
275
|
+
...attributes,
|
|
276
|
+
[`${spanNamePrefix}.name`]: target.constructor.name,
|
|
277
|
+
[`${spanNamePrefix}.method.name`]: prop.toString()
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
return value;
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* method to trace individual methods with proper context
|
|
287
|
+
* @param method The method to trace
|
|
288
|
+
* @param context Additional context for the trace
|
|
289
|
+
* @returns Wrapped method with tracing
|
|
290
|
+
*/
|
|
291
|
+
traceMethod(method, context3) {
|
|
292
|
+
let ctx = context.active();
|
|
293
|
+
const { skipIfNoTelemetry = true } = context3;
|
|
294
|
+
if (skipIfNoTelemetry && !hasActiveTelemetry()) {
|
|
295
|
+
return method;
|
|
296
|
+
}
|
|
297
|
+
return (...args) => {
|
|
298
|
+
const span = this.tracer.startSpan(context3.spanName);
|
|
299
|
+
function handleError(error) {
|
|
300
|
+
span.recordException(error);
|
|
301
|
+
span.setStatus({
|
|
302
|
+
code: SpanStatusCode.ERROR,
|
|
303
|
+
message: error.message
|
|
304
|
+
});
|
|
305
|
+
span.end();
|
|
306
|
+
throw error;
|
|
307
|
+
}
|
|
308
|
+
try {
|
|
309
|
+
let recordResult2 = function(res) {
|
|
310
|
+
try {
|
|
311
|
+
span.setAttribute(`${context3.spanName}.result`, JSON.stringify(res));
|
|
312
|
+
} catch {
|
|
313
|
+
span.setAttribute(`${context3.spanName}.result`, "[Not Serializable]");
|
|
314
|
+
}
|
|
315
|
+
span.end();
|
|
316
|
+
return res;
|
|
317
|
+
};
|
|
318
|
+
if (context3.attributes) {
|
|
319
|
+
span.setAttributes(context3.attributes);
|
|
320
|
+
}
|
|
321
|
+
if (context3.attributes?.componentName) {
|
|
322
|
+
ctx = propagation.setBaggage(ctx, {
|
|
323
|
+
// @ts-ignore
|
|
324
|
+
componentName: context3.attributes.componentName,
|
|
325
|
+
runId: context3.attributes.runId
|
|
326
|
+
});
|
|
327
|
+
} else {
|
|
328
|
+
const currentBaggage = propagation.getBaggage(ctx);
|
|
329
|
+
if (currentBaggage?.componentName) {
|
|
330
|
+
span.setAttribute("componentName", currentBaggage?.componentName);
|
|
331
|
+
span.setAttribute("runId", currentBaggage?.runId);
|
|
332
|
+
} else if (this && this.name) {
|
|
333
|
+
span.setAttribute("componentName", this.name);
|
|
334
|
+
span.setAttribute("runId", this.runId);
|
|
335
|
+
ctx = propagation.setBaggage(ctx, { componentName: this.name, runId: this.runId });
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
args.forEach((arg, index) => {
|
|
339
|
+
try {
|
|
340
|
+
span.setAttribute(`${context3.spanName}.argument.${index}`, JSON.stringify(arg));
|
|
341
|
+
} catch {
|
|
342
|
+
span.setAttribute(`${context3.spanName}.argument.${index}`, "[Not Serializable]");
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
let result;
|
|
346
|
+
context.with(trace.setSpan(ctx, span), () => {
|
|
347
|
+
result = method(...args);
|
|
348
|
+
});
|
|
349
|
+
if (result instanceof Promise) {
|
|
350
|
+
return result.then(recordResult2).catch(handleError);
|
|
351
|
+
} else {
|
|
352
|
+
return recordResult2(result);
|
|
353
|
+
}
|
|
354
|
+
} catch (error) {
|
|
355
|
+
handleError(error);
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
getBaggageTracer() {
|
|
360
|
+
return new BaggageTracer(this.tracer);
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
var BaggageTracer = class {
|
|
364
|
+
_tracer;
|
|
365
|
+
constructor(tracer) {
|
|
366
|
+
this._tracer = tracer;
|
|
367
|
+
}
|
|
368
|
+
startSpan(name, options = {}, ctx) {
|
|
369
|
+
ctx = ctx ?? context.active();
|
|
370
|
+
const span = this._tracer.startSpan(name, options, ctx);
|
|
371
|
+
const currentBaggage = propagation.getBaggage(ctx);
|
|
372
|
+
span.setAttribute("componentName", currentBaggage?.componentName);
|
|
373
|
+
span.setAttribute("runId", currentBaggage?.runId);
|
|
374
|
+
return span;
|
|
375
|
+
}
|
|
376
|
+
startActiveSpan(name, optionsOrFn, ctxOrFn, fn) {
|
|
377
|
+
if (typeof optionsOrFn === "function") {
|
|
378
|
+
const wrappedFn2 = (span) => {
|
|
379
|
+
const currentBaggage = propagation.getBaggage(context.active());
|
|
380
|
+
span.setAttribute("componentName", currentBaggage?.componentName);
|
|
381
|
+
return optionsOrFn(span);
|
|
382
|
+
};
|
|
383
|
+
return this._tracer.startActiveSpan(name, {}, context.active(), wrappedFn2);
|
|
384
|
+
}
|
|
385
|
+
if (typeof ctxOrFn === "function") {
|
|
386
|
+
const wrappedFn2 = (span) => {
|
|
387
|
+
const currentBaggage = propagation.getBaggage(context.active());
|
|
388
|
+
span.setAttribute("componentName", currentBaggage?.componentName);
|
|
389
|
+
return ctxOrFn(span);
|
|
390
|
+
};
|
|
391
|
+
return this._tracer.startActiveSpan(name, optionsOrFn, context.active(), wrappedFn2);
|
|
392
|
+
}
|
|
393
|
+
const wrappedFn = (span) => {
|
|
394
|
+
const currentBaggage = propagation.getBaggage(ctxOrFn ?? context.active());
|
|
395
|
+
span.setAttribute("componentName", currentBaggage?.componentName);
|
|
396
|
+
return fn(span);
|
|
397
|
+
};
|
|
398
|
+
return this._tracer.startActiveSpan(name, optionsOrFn, ctxOrFn, wrappedFn);
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
export { InstrumentClass, OTLPTraceExporter, Telemetry, hasActiveTelemetry, withSpan };
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
// src/utils.ts
|
|
4
|
+
var delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
5
|
+
function jsonSchemaPropertiesToTSTypes(value) {
|
|
6
|
+
if (!value.type) {
|
|
7
|
+
return z.object({});
|
|
8
|
+
}
|
|
9
|
+
let zodType;
|
|
10
|
+
switch (value.type) {
|
|
11
|
+
case "string":
|
|
12
|
+
zodType = z.string().describe((value.description || "") + (value.examples ? `
|
|
13
|
+
Examples: ${value.examples.join(", ")}` : ""));
|
|
14
|
+
break;
|
|
15
|
+
case "number":
|
|
16
|
+
zodType = z.number().describe((value.description || "") + (value.examples ? `
|
|
17
|
+
Examples: ${value.examples.join(", ")}` : ""));
|
|
18
|
+
break;
|
|
19
|
+
case "integer":
|
|
20
|
+
zodType = z.number().int().describe((value.description || "") + (value.examples ? `
|
|
21
|
+
Examples: ${value.examples.join(", ")}` : ""));
|
|
22
|
+
break;
|
|
23
|
+
case "boolean":
|
|
24
|
+
zodType = z.boolean().describe((value.description || "") + (value.examples ? `
|
|
25
|
+
Examples: ${value.examples.join(", ")}` : ""));
|
|
26
|
+
break;
|
|
27
|
+
case "array":
|
|
28
|
+
zodType = z.array(jsonSchemaPropertiesToTSTypes(value.items)).describe((value.description || "") + (value.examples ? `
|
|
29
|
+
Examples: ${value.examples.join(", ")}` : ""));
|
|
30
|
+
break;
|
|
31
|
+
case "object":
|
|
32
|
+
zodType = jsonSchemaToModel(value).describe(
|
|
33
|
+
(value.description || "") + (value.examples ? `
|
|
34
|
+
Examples: ${value.examples.join(", ")}` : "")
|
|
35
|
+
);
|
|
36
|
+
break;
|
|
37
|
+
case "null":
|
|
38
|
+
zodType = z.null().describe(value.description || "");
|
|
39
|
+
break;
|
|
40
|
+
default:
|
|
41
|
+
throw new Error(`Unsupported JSON schema type: ${value.type}`);
|
|
42
|
+
}
|
|
43
|
+
return zodType;
|
|
44
|
+
}
|
|
45
|
+
function jsonSchemaToModel(jsonSchema) {
|
|
46
|
+
const properties = jsonSchema.properties;
|
|
47
|
+
const requiredFields = jsonSchema.required || [];
|
|
48
|
+
if (!properties) {
|
|
49
|
+
return z.object({});
|
|
50
|
+
}
|
|
51
|
+
const zodSchema = {};
|
|
52
|
+
for (const [key, _] of Object.entries(properties)) {
|
|
53
|
+
const value = _;
|
|
54
|
+
let zodType;
|
|
55
|
+
if (value.anyOf) {
|
|
56
|
+
const anyOfTypes = value.anyOf.map((schema) => jsonSchemaPropertiesToTSTypes(schema));
|
|
57
|
+
zodType = z.union(anyOfTypes).describe((value.description || "") + (value.examples ? `
|
|
58
|
+
Examples: ${value.examples.join(", ")}` : ""));
|
|
59
|
+
} else if (value.allOf) {
|
|
60
|
+
const allOfTypes = value.allOf.map((schema) => jsonSchemaPropertiesToTSTypes(schema));
|
|
61
|
+
zodType = z.intersection(
|
|
62
|
+
allOfTypes[0],
|
|
63
|
+
allOfTypes.slice(1).reduce((acc, schema) => acc.and(schema), allOfTypes[0])
|
|
64
|
+
).describe((value.description || "") + (value.examples ? `
|
|
65
|
+
Examples: ${value.examples.join(", ")}` : ""));
|
|
66
|
+
} else {
|
|
67
|
+
if (!value.type) {
|
|
68
|
+
value.type = "string";
|
|
69
|
+
}
|
|
70
|
+
zodType = jsonSchemaPropertiesToTSTypes(value);
|
|
71
|
+
}
|
|
72
|
+
if (value.description) {
|
|
73
|
+
zodType = zodType.describe(value.description);
|
|
74
|
+
}
|
|
75
|
+
if (requiredFields.includes(key)) {
|
|
76
|
+
zodSchema[key] = zodType;
|
|
77
|
+
} else {
|
|
78
|
+
zodSchema[key] = zodType.nullable();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return z.object(zodSchema);
|
|
82
|
+
}
|
|
83
|
+
function deepMerge(target, source) {
|
|
84
|
+
const output = { ...target };
|
|
85
|
+
if (!source) return output;
|
|
86
|
+
Object.keys(source).forEach((key) => {
|
|
87
|
+
const targetValue = output[key];
|
|
88
|
+
const sourceValue = source[key];
|
|
89
|
+
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
|
|
90
|
+
output[key] = sourceValue;
|
|
91
|
+
} else if (sourceValue instanceof Object && targetValue instanceof Object && !Array.isArray(sourceValue) && !Array.isArray(targetValue)) {
|
|
92
|
+
output[key] = deepMerge(targetValue, sourceValue);
|
|
93
|
+
} else if (sourceValue !== void 0) {
|
|
94
|
+
output[key] = sourceValue;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
return output;
|
|
98
|
+
}
|
|
99
|
+
async function* maskStreamTags(stream, tag, options = {}) {
|
|
100
|
+
const { onStart, onEnd, onMask } = options;
|
|
101
|
+
const openTag = `<${tag}>`;
|
|
102
|
+
const closeTag = `</${tag}>`;
|
|
103
|
+
let buffer = "";
|
|
104
|
+
let fullContent = "";
|
|
105
|
+
let isMasking = false;
|
|
106
|
+
let isBuffering = false;
|
|
107
|
+
const trimOutsideDelimiter = (text, delimiter, trim) => {
|
|
108
|
+
if (!text.includes(delimiter)) {
|
|
109
|
+
return text;
|
|
110
|
+
}
|
|
111
|
+
const parts = text.split(delimiter);
|
|
112
|
+
if (trim === `before-start`) {
|
|
113
|
+
return `${delimiter}${parts[1]}`;
|
|
114
|
+
}
|
|
115
|
+
return `${parts[0]}${delimiter}`;
|
|
116
|
+
};
|
|
117
|
+
const startsWith = (text, pattern) => {
|
|
118
|
+
if (pattern.includes(openTag.substring(0, 3))) {
|
|
119
|
+
pattern = trimOutsideDelimiter(pattern, `<`, `before-start`);
|
|
120
|
+
}
|
|
121
|
+
return text.trim().startsWith(pattern.trim());
|
|
122
|
+
};
|
|
123
|
+
for await (const chunk of stream) {
|
|
124
|
+
fullContent += chunk;
|
|
125
|
+
if (isBuffering) buffer += chunk;
|
|
126
|
+
const chunkHasTag = startsWith(chunk, openTag);
|
|
127
|
+
const bufferHasTag = !chunkHasTag && isBuffering && startsWith(openTag, buffer);
|
|
128
|
+
let toYieldBeforeMaskedStartTag = ``;
|
|
129
|
+
if (!isMasking && (chunkHasTag || bufferHasTag)) {
|
|
130
|
+
isMasking = true;
|
|
131
|
+
isBuffering = false;
|
|
132
|
+
const taggedTextToMask = trimOutsideDelimiter(buffer, `<`, `before-start`);
|
|
133
|
+
if (taggedTextToMask !== buffer.trim()) {
|
|
134
|
+
toYieldBeforeMaskedStartTag = buffer.replace(taggedTextToMask, ``);
|
|
135
|
+
}
|
|
136
|
+
buffer = "";
|
|
137
|
+
onStart?.();
|
|
138
|
+
}
|
|
139
|
+
if (!isMasking && !isBuffering && startsWith(openTag, chunk) && chunk.trim() !== "") {
|
|
140
|
+
isBuffering = true;
|
|
141
|
+
buffer += chunk;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (isBuffering && buffer && !startsWith(openTag, buffer)) {
|
|
145
|
+
yield buffer;
|
|
146
|
+
buffer = "";
|
|
147
|
+
isBuffering = false;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (isMasking && fullContent.includes(closeTag)) {
|
|
151
|
+
onMask?.(chunk);
|
|
152
|
+
onEnd?.();
|
|
153
|
+
isMasking = false;
|
|
154
|
+
const lastFullContent = fullContent;
|
|
155
|
+
fullContent = ``;
|
|
156
|
+
const textUntilEndTag = trimOutsideDelimiter(lastFullContent, closeTag, "after-end");
|
|
157
|
+
if (textUntilEndTag !== lastFullContent) {
|
|
158
|
+
yield lastFullContent.replace(textUntilEndTag, ``);
|
|
159
|
+
}
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (isMasking) {
|
|
163
|
+
onMask?.(chunk);
|
|
164
|
+
if (toYieldBeforeMaskedStartTag) {
|
|
165
|
+
yield toYieldBeforeMaskedStartTag;
|
|
166
|
+
}
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
yield chunk;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export { deepMerge, delay, jsonSchemaPropertiesToTSTypes, jsonSchemaToModel, maskStreamTags };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// src/tools/tool.ts
|
|
2
|
+
var Tool = class {
|
|
3
|
+
id;
|
|
4
|
+
description;
|
|
5
|
+
inputSchema;
|
|
6
|
+
outputSchema;
|
|
7
|
+
execute;
|
|
8
|
+
mastra;
|
|
9
|
+
constructor(opts) {
|
|
10
|
+
this.id = opts.id;
|
|
11
|
+
this.description = opts.description;
|
|
12
|
+
this.inputSchema = opts.inputSchema;
|
|
13
|
+
this.outputSchema = opts.outputSchema;
|
|
14
|
+
this.execute = opts.execute;
|
|
15
|
+
this.mastra = opts.mastra;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
function createTool(opts) {
|
|
19
|
+
return new Tool(opts);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { Tool, createTool };
|