@mastra/core 0.5.0-alpha.8 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/index.cjs +3 -2100
- package/dist/agent/index.d.cts +1 -1
- package/dist/agent/index.d.ts +1 -1
- package/dist/agent/index.js +1 -1
- package/dist/{base-CTdONy0_.d.cts → base-CIPKleAU.d.cts} +103 -70
- package/dist/{base-DIn_km7X.d.ts → base-C_Oq53qk.d.ts} +103 -70
- package/dist/base.cjs +5 -140
- package/dist/bundler/index.cjs +5 -160
- package/dist/chunk-2W2GYEYQ.cjs +25 -0
- package/dist/chunk-3ASEZT7U.cjs +1586 -0
- package/dist/chunk-43Y7WG5W.cjs +335 -0
- package/dist/{chunk-HBHPTMAC.js → chunk-4Y74D74B.js} +46 -6
- package/dist/chunk-ENT7U27Y.cjs +37 -0
- package/dist/chunk-F5UYWPV4.cjs +14 -0
- package/dist/chunk-FL3GQXQ2.cjs +218 -0
- package/dist/chunk-FRQFWZDN.cjs +2 -0
- package/dist/chunk-GXQRMKSN.cjs +367 -0
- package/dist/chunk-HJPMYDWO.cjs +37 -0
- package/dist/chunk-IIWRJFLQ.cjs +51 -0
- package/dist/chunk-KFQ7Z3PO.cjs +347 -0
- package/dist/{chunk-SWDQYPJS.js → chunk-KP5UAFLN.js} +3 -2
- package/dist/chunk-KPKFLQFR.cjs +12 -0
- package/dist/{chunk-RRJB4TCC.js → chunk-MLFXOST6.js} +1 -1
- package/dist/{chunk-KBSR2LLT.js → chunk-OD7ZMKHY.js} +176 -63
- package/dist/chunk-OTFLHXHZ.cjs +65 -0
- package/dist/chunk-RWTSGWWL.cjs +81 -0
- package/dist/chunk-ST5RMVLG.cjs +87 -0
- package/dist/chunk-SYQ7NK2E.cjs +24 -0
- package/dist/chunk-UZNQG7QO.cjs +1868 -0
- package/dist/chunk-V5ORZPFW.cjs +38 -0
- package/dist/chunk-VA4P7QJT.cjs +443 -0
- package/dist/chunk-WB2HREXE.cjs +166 -0
- package/dist/chunk-WOMOGDGR.cjs +691 -0
- package/dist/chunk-XB2TJ7LX.cjs +408 -0
- package/dist/{chunk-QABMKXI3.js → chunk-XF2FMJYK.js} +1 -1
- package/dist/chunk-XLSROQ26.cjs +91 -0
- package/dist/chunk-YK3XJ52U.cjs +192 -0
- package/dist/{chunk-SF5GHHOQ.js → chunk-YPD6BQIM.js} +121 -93
- package/dist/deployer/index.cjs +5 -167
- package/dist/eval/index.cjs +9 -105
- package/dist/eval/index.d.cts +1 -1
- package/dist/eval/index.d.ts +1 -1
- package/dist/hooks/index.cjs +14 -83
- package/dist/index.cjs +253 -7470
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +7 -7
- package/dist/integration/index.cjs +9 -108
- package/dist/integration/index.d.cts +1 -1
- package/dist/integration/index.d.ts +1 -1
- package/dist/llm/index.d.cts +1 -1
- package/dist/llm/index.d.ts +1 -1
- package/dist/logger/index.cjs +33 -161
- package/dist/mastra/index.cjs +3 -1755
- package/dist/mastra/index.d.cts +1 -1
- package/dist/mastra/index.d.ts +1 -1
- package/dist/mastra/index.js +1 -1
- package/dist/memory/index.cjs +4 -2050
- package/dist/memory/index.d.cts +1 -1
- package/dist/memory/index.d.ts +1 -1
- package/dist/memory/index.js +1 -1
- package/dist/relevance/index.cjs +10 -2161
- package/dist/relevance/index.d.cts +19 -2
- package/dist/relevance/index.d.ts +19 -2
- package/dist/relevance/index.js +1 -1
- package/dist/storage/index.cjs +29 -367
- package/dist/storage/index.d.cts +1 -1
- package/dist/storage/index.d.ts +1 -1
- package/dist/storage/libsql/index.cjs +9 -798
- package/dist/storage/libsql/index.d.cts +1 -1
- package/dist/storage/libsql/index.d.ts +1 -1
- package/dist/telemetry/index.cjs +21 -408
- package/dist/telemetry/index.d.cts +1 -1
- package/dist/telemetry/index.d.ts +1 -1
- package/dist/tools/index.cjs +11 -22
- package/dist/tools/index.d.cts +3 -3
- package/dist/tools/index.d.ts +3 -3
- package/dist/tts/index.cjs +3 -328
- package/dist/utils.cjs +41 -309
- package/dist/utils.d.cts +10 -4
- package/dist/utils.d.ts +10 -4
- package/dist/utils.js +1 -1
- package/dist/vector/filter/index.cjs +7 -189
- package/dist/vector/index.cjs +5 -172
- package/dist/vector/libsql/index.cjs +9 -1047
- package/dist/voice/index.cjs +8 -306
- package/dist/workflows/index.cjs +65 -1925
- package/dist/workflows/index.d.cts +4 -3
- package/dist/workflows/index.d.ts +4 -3
- package/dist/workflows/index.js +1 -1
- package/package.json +27 -27
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkF5UYWPV4_cjs = require('./chunk-F5UYWPV4.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
|
+
|
|
16
|
+
// src/telemetry/telemetry.decorators.ts
|
|
17
|
+
function withSpan(options) {
|
|
18
|
+
return function(_target, propertyKey, descriptor) {
|
|
19
|
+
if (!descriptor || typeof descriptor === "number") return;
|
|
20
|
+
const originalMethod = descriptor.value;
|
|
21
|
+
const methodName = String(propertyKey);
|
|
22
|
+
descriptor.value = function(...args) {
|
|
23
|
+
if (options?.skipIfNoTelemetry && !hasActiveTelemetry(options?.tracerName)) {
|
|
24
|
+
return originalMethod.apply(this, args);
|
|
25
|
+
}
|
|
26
|
+
const tracer = api.trace.getTracer(options?.tracerName ?? "default-tracer");
|
|
27
|
+
let spanName;
|
|
28
|
+
let spanKind;
|
|
29
|
+
if (typeof options === "string") {
|
|
30
|
+
spanName = options;
|
|
31
|
+
} else if (options) {
|
|
32
|
+
spanName = options.spanName || methodName;
|
|
33
|
+
spanKind = options.spanKind;
|
|
34
|
+
} else {
|
|
35
|
+
spanName = methodName;
|
|
36
|
+
}
|
|
37
|
+
const span = tracer.startSpan(spanName, { kind: spanKind });
|
|
38
|
+
let ctx = api.trace.setSpan(api.context.active(), span);
|
|
39
|
+
args.forEach((arg, index) => {
|
|
40
|
+
try {
|
|
41
|
+
span.setAttribute(`${spanName}.argument.${index}`, JSON.stringify(arg));
|
|
42
|
+
} catch {
|
|
43
|
+
span.setAttribute(`${spanName}.argument.${index}`, "[Not Serializable]");
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
const currentBaggage = api.propagation.getBaggage(ctx);
|
|
47
|
+
if (currentBaggage?.componentName) {
|
|
48
|
+
span.setAttribute("componentName", currentBaggage?.componentName);
|
|
49
|
+
span.setAttribute("runId", currentBaggage?.runId);
|
|
50
|
+
} else if (this && this.name) {
|
|
51
|
+
span.setAttribute("componentName", this.name);
|
|
52
|
+
span.setAttribute("runId", this.runId);
|
|
53
|
+
ctx = api.propagation.setBaggage(ctx, { componentName: this.name, runId: this.runId });
|
|
54
|
+
}
|
|
55
|
+
let result;
|
|
56
|
+
try {
|
|
57
|
+
result = api.context.with(ctx, () => originalMethod.apply(this, args));
|
|
58
|
+
if (result instanceof Promise) {
|
|
59
|
+
return result.then((resolvedValue) => {
|
|
60
|
+
try {
|
|
61
|
+
span.setAttribute(`${spanName}.result`, JSON.stringify(resolvedValue));
|
|
62
|
+
} catch {
|
|
63
|
+
span.setAttribute(`${spanName}.result`, "[Not Serializable]");
|
|
64
|
+
}
|
|
65
|
+
return resolvedValue;
|
|
66
|
+
}).finally(() => span.end());
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
span.setAttribute(`${spanName}.result`, JSON.stringify(result));
|
|
70
|
+
} catch {
|
|
71
|
+
span.setAttribute(`${spanName}.result`, "[Not Serializable]");
|
|
72
|
+
}
|
|
73
|
+
return result;
|
|
74
|
+
} catch (error) {
|
|
75
|
+
span.setStatus({
|
|
76
|
+
code: api.SpanStatusCode.ERROR,
|
|
77
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
78
|
+
});
|
|
79
|
+
if (error instanceof Error) {
|
|
80
|
+
span.recordException(error);
|
|
81
|
+
}
|
|
82
|
+
throw error;
|
|
83
|
+
} finally {
|
|
84
|
+
if (!(result instanceof Promise)) {
|
|
85
|
+
span.end();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
return descriptor;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function InstrumentClass(options) {
|
|
93
|
+
return function(target) {
|
|
94
|
+
const methods = Object.getOwnPropertyNames(target.prototype);
|
|
95
|
+
methods.forEach((method) => {
|
|
96
|
+
if (options?.excludeMethods?.includes(method) || method === "constructor") return;
|
|
97
|
+
if (options?.methodFilter && !options.methodFilter(method)) return;
|
|
98
|
+
const descriptor = Object.getOwnPropertyDescriptor(target.prototype, method);
|
|
99
|
+
if (descriptor && typeof descriptor.value === "function") {
|
|
100
|
+
Object.defineProperty(
|
|
101
|
+
target.prototype,
|
|
102
|
+
method,
|
|
103
|
+
withSpan({
|
|
104
|
+
spanName: options?.prefix ? `${options.prefix}.${method}` : method,
|
|
105
|
+
skipIfNoTelemetry: true,
|
|
106
|
+
spanKind: options?.spanKind || api.SpanKind.INTERNAL,
|
|
107
|
+
tracerName: options?.tracerName
|
|
108
|
+
})(target, method, descriptor)
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
return target;
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
var OTLPTraceExporter = class {
|
|
116
|
+
storage;
|
|
117
|
+
queue = [];
|
|
118
|
+
serializer;
|
|
119
|
+
logger;
|
|
120
|
+
activeFlush = void 0;
|
|
121
|
+
constructor({ logger, storage }) {
|
|
122
|
+
this.storage = storage;
|
|
123
|
+
this.serializer = otlpTransformer.JsonTraceSerializer;
|
|
124
|
+
this.logger = logger;
|
|
125
|
+
}
|
|
126
|
+
export(internalRepresentation, resultCallback) {
|
|
127
|
+
const serializedRequest = this.serializer.serializeRequest(internalRepresentation);
|
|
128
|
+
const payload = JSON.parse(Buffer.from(serializedRequest.buffer, "utf8"));
|
|
129
|
+
const items = payload?.resourceSpans?.[0]?.scopeSpans;
|
|
130
|
+
this.logger.debug(`Exporting telemetry: ${items.length} scope spans to be processed [trace batch]`);
|
|
131
|
+
this.queue.push({ data: items, resultCallback });
|
|
132
|
+
if (!this.activeFlush) {
|
|
133
|
+
this.activeFlush = this.flush();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
shutdown() {
|
|
137
|
+
return this.forceFlush();
|
|
138
|
+
}
|
|
139
|
+
flush() {
|
|
140
|
+
const now = /* @__PURE__ */ new Date();
|
|
141
|
+
const items = this.queue.shift();
|
|
142
|
+
if (!items) return Promise.resolve();
|
|
143
|
+
const allSpans = items.data.reduce((acc, scopedSpans) => {
|
|
144
|
+
const { scope, spans } = scopedSpans;
|
|
145
|
+
for (const span of spans) {
|
|
146
|
+
const {
|
|
147
|
+
spanId,
|
|
148
|
+
parentSpanId,
|
|
149
|
+
traceId,
|
|
150
|
+
name,
|
|
151
|
+
kind,
|
|
152
|
+
attributes,
|
|
153
|
+
status,
|
|
154
|
+
events,
|
|
155
|
+
links,
|
|
156
|
+
startTimeUnixNano,
|
|
157
|
+
endTimeUnixNano,
|
|
158
|
+
...rest
|
|
159
|
+
} = span;
|
|
160
|
+
const startTime = Number(BigInt(startTimeUnixNano) / 1000n);
|
|
161
|
+
const endTime = Number(BigInt(endTimeUnixNano) / 1000n);
|
|
162
|
+
acc.push({
|
|
163
|
+
id: spanId,
|
|
164
|
+
parentSpanId,
|
|
165
|
+
traceId,
|
|
166
|
+
name,
|
|
167
|
+
scope: scope.name,
|
|
168
|
+
kind,
|
|
169
|
+
status: JSON.stringify(status),
|
|
170
|
+
events: JSON.stringify(events),
|
|
171
|
+
links: JSON.stringify(links),
|
|
172
|
+
attributes: JSON.stringify(
|
|
173
|
+
attributes.reduce((acc2, attr) => {
|
|
174
|
+
const valueKey = Object.keys(attr.value)[0];
|
|
175
|
+
if (valueKey) {
|
|
176
|
+
acc2[attr.key] = attr.value[valueKey];
|
|
177
|
+
}
|
|
178
|
+
return acc2;
|
|
179
|
+
}, {})
|
|
180
|
+
),
|
|
181
|
+
startTime,
|
|
182
|
+
endTime,
|
|
183
|
+
other: JSON.stringify(rest),
|
|
184
|
+
createdAt: now
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
return acc;
|
|
188
|
+
}, []);
|
|
189
|
+
return this.storage.__batchInsert({
|
|
190
|
+
tableName: chunkF5UYWPV4_cjs.TABLE_TRACES,
|
|
191
|
+
records: allSpans
|
|
192
|
+
}).then(() => {
|
|
193
|
+
items.resultCallback({
|
|
194
|
+
code: core.ExportResultCode.SUCCESS
|
|
195
|
+
});
|
|
196
|
+
}).catch((e) => {
|
|
197
|
+
this.logger.error("span err:" + e?.message);
|
|
198
|
+
items.resultCallback({
|
|
199
|
+
code: core.ExportResultCode.FAILED,
|
|
200
|
+
error: e
|
|
201
|
+
});
|
|
202
|
+
}).finally(() => {
|
|
203
|
+
this.activeFlush = void 0;
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
async forceFlush() {
|
|
207
|
+
if (!this.queue.length) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
await this.activeFlush;
|
|
211
|
+
while (this.queue.length) {
|
|
212
|
+
await this.flush();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
__setLogger(logger) {
|
|
216
|
+
this.logger = logger;
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
var Telemetry = class _Telemetry {
|
|
220
|
+
tracer = api.trace.getTracer("default");
|
|
221
|
+
name = "default-service";
|
|
222
|
+
constructor(config) {
|
|
223
|
+
this.name = config.serviceName ?? "default-service";
|
|
224
|
+
this.tracer = api.trace.getTracer(this.name);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* @deprecated This method does not do anything
|
|
228
|
+
*/
|
|
229
|
+
async shutdown() {
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Initialize telemetry with the given configuration
|
|
233
|
+
* @param config - Optional telemetry configuration object
|
|
234
|
+
* @returns Telemetry instance that can be used for tracing
|
|
235
|
+
*/
|
|
236
|
+
static init(config = {}) {
|
|
237
|
+
try {
|
|
238
|
+
if (!global.__TELEMETRY__) {
|
|
239
|
+
global.__TELEMETRY__ = new _Telemetry(config);
|
|
240
|
+
}
|
|
241
|
+
return global.__TELEMETRY__;
|
|
242
|
+
} catch (error) {
|
|
243
|
+
console.error("Failed to initialize telemetry:", error);
|
|
244
|
+
throw error;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Get the global telemetry instance
|
|
249
|
+
* @throws {Error} If telemetry has not been initialized
|
|
250
|
+
* @returns {Telemetry} The global telemetry instance
|
|
251
|
+
*/
|
|
252
|
+
static get() {
|
|
253
|
+
if (!global.__TELEMETRY__) {
|
|
254
|
+
throw new Error("Telemetry not initialized");
|
|
255
|
+
}
|
|
256
|
+
return global.__TELEMETRY__;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Wraps a class instance with telemetry tracing
|
|
260
|
+
* @param instance The class instance to wrap
|
|
261
|
+
* @param options Optional configuration for tracing
|
|
262
|
+
* @returns Wrapped instance with all methods traced
|
|
263
|
+
*/
|
|
264
|
+
traceClass(instance, options = {}) {
|
|
265
|
+
const { skipIfNoTelemetry = true } = options;
|
|
266
|
+
if (skipIfNoTelemetry && !hasActiveTelemetry()) {
|
|
267
|
+
return instance;
|
|
268
|
+
}
|
|
269
|
+
const { spanNamePrefix = instance.constructor.name.toLowerCase(), attributes = {}, excludeMethods = [] } = options;
|
|
270
|
+
return new Proxy(instance, {
|
|
271
|
+
get: (target, prop) => {
|
|
272
|
+
const value = target[prop];
|
|
273
|
+
if (typeof value === "function" && prop !== "constructor" && !prop.toString().startsWith("_") && !excludeMethods.includes(prop.toString())) {
|
|
274
|
+
return this.traceMethod(value.bind(target), {
|
|
275
|
+
spanName: `${spanNamePrefix}.${prop.toString()}`,
|
|
276
|
+
attributes: {
|
|
277
|
+
...attributes,
|
|
278
|
+
[`${spanNamePrefix}.name`]: target.constructor.name,
|
|
279
|
+
[`${spanNamePrefix}.method.name`]: prop.toString()
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
return value;
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* method to trace individual methods with proper context
|
|
289
|
+
* @param method The method to trace
|
|
290
|
+
* @param context Additional context for the trace
|
|
291
|
+
* @returns Wrapped method with tracing
|
|
292
|
+
*/
|
|
293
|
+
traceMethod(method, context3) {
|
|
294
|
+
let ctx = api.context.active();
|
|
295
|
+
const { skipIfNoTelemetry = true } = context3;
|
|
296
|
+
if (skipIfNoTelemetry && !hasActiveTelemetry()) {
|
|
297
|
+
return method;
|
|
298
|
+
}
|
|
299
|
+
return (...args) => {
|
|
300
|
+
const span = this.tracer.startSpan(context3.spanName);
|
|
301
|
+
function handleError(error) {
|
|
302
|
+
span.recordException(error);
|
|
303
|
+
span.setStatus({
|
|
304
|
+
code: api.SpanStatusCode.ERROR,
|
|
305
|
+
message: error.message
|
|
306
|
+
});
|
|
307
|
+
span.end();
|
|
308
|
+
throw error;
|
|
309
|
+
}
|
|
310
|
+
try {
|
|
311
|
+
let recordResult2 = function(res) {
|
|
312
|
+
try {
|
|
313
|
+
span.setAttribute(`${context3.spanName}.result`, JSON.stringify(res));
|
|
314
|
+
} catch {
|
|
315
|
+
span.setAttribute(`${context3.spanName}.result`, "[Not Serializable]");
|
|
316
|
+
}
|
|
317
|
+
span.end();
|
|
318
|
+
return res;
|
|
319
|
+
};
|
|
320
|
+
if (context3.attributes) {
|
|
321
|
+
span.setAttributes(context3.attributes);
|
|
322
|
+
}
|
|
323
|
+
if (context3.attributes?.componentName) {
|
|
324
|
+
ctx = api.propagation.setBaggage(ctx, {
|
|
325
|
+
// @ts-ignore
|
|
326
|
+
componentName: context3.attributes.componentName,
|
|
327
|
+
runId: context3.attributes.runId
|
|
328
|
+
});
|
|
329
|
+
} else {
|
|
330
|
+
const currentBaggage = api.propagation.getBaggage(ctx);
|
|
331
|
+
if (currentBaggage?.componentName) {
|
|
332
|
+
span.setAttribute("componentName", currentBaggage?.componentName);
|
|
333
|
+
span.setAttribute("runId", currentBaggage?.runId);
|
|
334
|
+
} else if (this && this.name) {
|
|
335
|
+
span.setAttribute("componentName", this.name);
|
|
336
|
+
span.setAttribute("runId", this.runId);
|
|
337
|
+
ctx = api.propagation.setBaggage(ctx, { componentName: this.name, runId: this.runId });
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
args.forEach((arg, index) => {
|
|
341
|
+
try {
|
|
342
|
+
span.setAttribute(`${context3.spanName}.argument.${index}`, JSON.stringify(arg));
|
|
343
|
+
} catch {
|
|
344
|
+
span.setAttribute(`${context3.spanName}.argument.${index}`, "[Not Serializable]");
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
let result;
|
|
348
|
+
api.context.with(api.trace.setSpan(ctx, span), () => {
|
|
349
|
+
result = method(...args);
|
|
350
|
+
});
|
|
351
|
+
if (result instanceof Promise) {
|
|
352
|
+
return result.then(recordResult2).catch(handleError);
|
|
353
|
+
} else {
|
|
354
|
+
return recordResult2(result);
|
|
355
|
+
}
|
|
356
|
+
} catch (error) {
|
|
357
|
+
handleError(error);
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
getBaggageTracer() {
|
|
362
|
+
return new BaggageTracer(this.tracer);
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
var BaggageTracer = class {
|
|
366
|
+
_tracer;
|
|
367
|
+
constructor(tracer) {
|
|
368
|
+
this._tracer = tracer;
|
|
369
|
+
}
|
|
370
|
+
startSpan(name, options = {}, ctx) {
|
|
371
|
+
ctx = ctx ?? api.context.active();
|
|
372
|
+
const span = this._tracer.startSpan(name, options, ctx);
|
|
373
|
+
const currentBaggage = api.propagation.getBaggage(ctx);
|
|
374
|
+
span.setAttribute("componentName", currentBaggage?.componentName);
|
|
375
|
+
span.setAttribute("runId", currentBaggage?.runId);
|
|
376
|
+
return span;
|
|
377
|
+
}
|
|
378
|
+
startActiveSpan(name, optionsOrFn, ctxOrFn, fn) {
|
|
379
|
+
if (typeof optionsOrFn === "function") {
|
|
380
|
+
const wrappedFn2 = (span) => {
|
|
381
|
+
const currentBaggage = api.propagation.getBaggage(api.context.active());
|
|
382
|
+
span.setAttribute("componentName", currentBaggage?.componentName);
|
|
383
|
+
return optionsOrFn(span);
|
|
384
|
+
};
|
|
385
|
+
return this._tracer.startActiveSpan(name, {}, api.context.active(), wrappedFn2);
|
|
386
|
+
}
|
|
387
|
+
if (typeof ctxOrFn === "function") {
|
|
388
|
+
const wrappedFn2 = (span) => {
|
|
389
|
+
const currentBaggage = api.propagation.getBaggage(api.context.active());
|
|
390
|
+
span.setAttribute("componentName", currentBaggage?.componentName);
|
|
391
|
+
return ctxOrFn(span);
|
|
392
|
+
};
|
|
393
|
+
return this._tracer.startActiveSpan(name, optionsOrFn, api.context.active(), wrappedFn2);
|
|
394
|
+
}
|
|
395
|
+
const wrappedFn = (span) => {
|
|
396
|
+
const currentBaggage = api.propagation.getBaggage(ctxOrFn ?? api.context.active());
|
|
397
|
+
span.setAttribute("componentName", currentBaggage?.componentName);
|
|
398
|
+
return fn(span);
|
|
399
|
+
};
|
|
400
|
+
return this._tracer.startActiveSpan(name, optionsOrFn, ctxOrFn, wrappedFn);
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
exports.InstrumentClass = InstrumentClass;
|
|
405
|
+
exports.OTLPTraceExporter = OTLPTraceExporter;
|
|
406
|
+
exports.Telemetry = Telemetry;
|
|
407
|
+
exports.hasActiveTelemetry = hasActiveTelemetry;
|
|
408
|
+
exports.withSpan = withSpan;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunk2W2GYEYQ_cjs = require('./chunk-2W2GYEYQ.cjs');
|
|
4
|
+
var zod = require('zod');
|
|
5
|
+
|
|
6
|
+
// src/integration/integration.ts
|
|
7
|
+
var Integration = class {
|
|
8
|
+
name = "Integration";
|
|
9
|
+
workflows;
|
|
10
|
+
constructor() {
|
|
11
|
+
this.workflows = {};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Workflows
|
|
15
|
+
*/
|
|
16
|
+
registerWorkflow(name, fn) {
|
|
17
|
+
if (this.workflows[name]) {
|
|
18
|
+
throw new Error(`Sync function "${name}" already registered`);
|
|
19
|
+
}
|
|
20
|
+
this.workflows[name] = fn;
|
|
21
|
+
}
|
|
22
|
+
getWorkflows({ serialized }) {
|
|
23
|
+
if (serialized) {
|
|
24
|
+
return Object.entries(this.workflows).reduce((acc, [k, v]) => {
|
|
25
|
+
return {
|
|
26
|
+
...acc,
|
|
27
|
+
[k]: {
|
|
28
|
+
name: v.name
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}, {});
|
|
32
|
+
}
|
|
33
|
+
return this.workflows;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* TOOLS
|
|
37
|
+
*/
|
|
38
|
+
getStaticTools(_params) {
|
|
39
|
+
throw new Error("Method not implemented.");
|
|
40
|
+
}
|
|
41
|
+
async getTools(_params) {
|
|
42
|
+
throw new Error("Method not implemented.");
|
|
43
|
+
}
|
|
44
|
+
async getApiClient() {
|
|
45
|
+
throw new Error("Method not implemented");
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var OpenAPIToolset = class {
|
|
49
|
+
authType = "API_KEY";
|
|
50
|
+
constructor() {
|
|
51
|
+
}
|
|
52
|
+
get toolSchemas() {
|
|
53
|
+
return {};
|
|
54
|
+
}
|
|
55
|
+
get toolDocumentations() {
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
get baseClient() {
|
|
59
|
+
return {};
|
|
60
|
+
}
|
|
61
|
+
async getApiClient() {
|
|
62
|
+
throw new Error("API not implemented");
|
|
63
|
+
}
|
|
64
|
+
_generateIntegrationTools() {
|
|
65
|
+
const { client, ...clientMethods } = this.baseClient;
|
|
66
|
+
const schemas = this.toolSchemas;
|
|
67
|
+
const documentations = this.toolDocumentations;
|
|
68
|
+
const tools = Object.keys(clientMethods).reduce((acc, key) => {
|
|
69
|
+
const comment = documentations[key]?.comment;
|
|
70
|
+
const fallbackComment = `Execute ${key}`;
|
|
71
|
+
const tool = chunk2W2GYEYQ_cjs.createTool({
|
|
72
|
+
id: key,
|
|
73
|
+
inputSchema: schemas[key] || zod.z.object({}),
|
|
74
|
+
description: comment || fallbackComment,
|
|
75
|
+
// documentation: doc || fallbackComment,
|
|
76
|
+
execute: async ({ context }) => {
|
|
77
|
+
const client2 = await this.getApiClient();
|
|
78
|
+
const value = client2[key];
|
|
79
|
+
return value({
|
|
80
|
+
...context
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
return { ...acc, [key]: tool };
|
|
85
|
+
}, {});
|
|
86
|
+
return tools;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
exports.Integration = Integration;
|
|
91
|
+
exports.OpenAPIToolset = OpenAPIToolset;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/vector/filter/base.ts
|
|
4
|
+
var BaseFilterTranslator = class _BaseFilterTranslator {
|
|
5
|
+
/**
|
|
6
|
+
* Operator type checks
|
|
7
|
+
*/
|
|
8
|
+
isOperator(key) {
|
|
9
|
+
return key.startsWith("$");
|
|
10
|
+
}
|
|
11
|
+
static BASIC_OPERATORS = ["$eq", "$ne"];
|
|
12
|
+
static NUMERIC_OPERATORS = ["$gt", "$gte", "$lt", "$lte"];
|
|
13
|
+
static ARRAY_OPERATORS = ["$in", "$nin", "$all", "$elemMatch"];
|
|
14
|
+
static LOGICAL_OPERATORS = ["$and", "$or", "$not", "$nor"];
|
|
15
|
+
static ELEMENT_OPERATORS = ["$exists"];
|
|
16
|
+
static REGEX_OPERATORS = ["$regex", "$options"];
|
|
17
|
+
static DEFAULT_OPERATORS = {
|
|
18
|
+
logical: _BaseFilterTranslator.LOGICAL_OPERATORS,
|
|
19
|
+
basic: _BaseFilterTranslator.BASIC_OPERATORS,
|
|
20
|
+
numeric: _BaseFilterTranslator.NUMERIC_OPERATORS,
|
|
21
|
+
array: _BaseFilterTranslator.ARRAY_OPERATORS,
|
|
22
|
+
element: _BaseFilterTranslator.ELEMENT_OPERATORS,
|
|
23
|
+
regex: _BaseFilterTranslator.REGEX_OPERATORS
|
|
24
|
+
};
|
|
25
|
+
isLogicalOperator(key) {
|
|
26
|
+
return _BaseFilterTranslator.DEFAULT_OPERATORS.logical.includes(key);
|
|
27
|
+
}
|
|
28
|
+
isBasicOperator(key) {
|
|
29
|
+
return _BaseFilterTranslator.DEFAULT_OPERATORS.basic.includes(key);
|
|
30
|
+
}
|
|
31
|
+
isNumericOperator(key) {
|
|
32
|
+
return _BaseFilterTranslator.DEFAULT_OPERATORS.numeric.includes(key);
|
|
33
|
+
}
|
|
34
|
+
isArrayOperator(key) {
|
|
35
|
+
return _BaseFilterTranslator.DEFAULT_OPERATORS.array.includes(key);
|
|
36
|
+
}
|
|
37
|
+
isElementOperator(key) {
|
|
38
|
+
return _BaseFilterTranslator.DEFAULT_OPERATORS.element.includes(key);
|
|
39
|
+
}
|
|
40
|
+
isRegexOperator(key) {
|
|
41
|
+
return _BaseFilterTranslator.DEFAULT_OPERATORS.regex.includes(key);
|
|
42
|
+
}
|
|
43
|
+
isFieldOperator(key) {
|
|
44
|
+
return this.isOperator(key) && !this.isLogicalOperator(key);
|
|
45
|
+
}
|
|
46
|
+
isCustomOperator(key) {
|
|
47
|
+
const support = this.getSupportedOperators();
|
|
48
|
+
return support.custom?.includes(key) ?? false;
|
|
49
|
+
}
|
|
50
|
+
getSupportedOperators() {
|
|
51
|
+
return _BaseFilterTranslator.DEFAULT_OPERATORS;
|
|
52
|
+
}
|
|
53
|
+
isValidOperator(key) {
|
|
54
|
+
const support = this.getSupportedOperators();
|
|
55
|
+
const allSupported = Object.values(support).flat();
|
|
56
|
+
return allSupported.includes(key);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Value normalization for comparison operators
|
|
60
|
+
*/
|
|
61
|
+
normalizeComparisonValue(value) {
|
|
62
|
+
if (value instanceof Date) {
|
|
63
|
+
return value.toISOString();
|
|
64
|
+
}
|
|
65
|
+
if (typeof value === "number" && Object.is(value, -0)) {
|
|
66
|
+
return 0;
|
|
67
|
+
}
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Helper method to simulate $all operator using $and + $eq when needed.
|
|
72
|
+
* Some vector stores don't support $all natively.
|
|
73
|
+
*/
|
|
74
|
+
simulateAllOperator(field, values) {
|
|
75
|
+
return {
|
|
76
|
+
$and: values.map((value) => ({
|
|
77
|
+
[field]: { $in: [this.normalizeComparisonValue(value)] }
|
|
78
|
+
}))
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Utility functions for type checking
|
|
83
|
+
*/
|
|
84
|
+
isPrimitive(value) {
|
|
85
|
+
return value === null || value === void 0 || typeof value === "string" || typeof value === "number" || typeof value === "boolean";
|
|
86
|
+
}
|
|
87
|
+
isRegex(value) {
|
|
88
|
+
return value instanceof RegExp;
|
|
89
|
+
}
|
|
90
|
+
isEmpty(obj) {
|
|
91
|
+
return obj === null || obj === void 0 || typeof obj === "object" && Object.keys(obj).length === 0;
|
|
92
|
+
}
|
|
93
|
+
static ErrorMessages = {
|
|
94
|
+
UNSUPPORTED_OPERATOR: (op) => `Unsupported operator: ${op}`,
|
|
95
|
+
INVALID_LOGICAL_OPERATOR_LOCATION: (op, path) => `Logical operator ${op} cannot be used at field level: ${path}`,
|
|
96
|
+
NOT_REQUIRES_OBJECT: `$not operator requires an object`,
|
|
97
|
+
NOT_CANNOT_BE_EMPTY: `$not operator cannot be empty`,
|
|
98
|
+
INVALID_LOGICAL_OPERATOR_CONTENT: (path) => `Logical operators must contain field conditions, not direct operators: ${path}`,
|
|
99
|
+
INVALID_TOP_LEVEL_OPERATOR: (op) => `Invalid top-level operator: ${op}`,
|
|
100
|
+
ELEM_MATCH_REQUIRES_OBJECT: `$elemMatch requires an object with conditions`
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Helper to handle array value normalization consistently
|
|
104
|
+
*/
|
|
105
|
+
normalizeArrayValues(values) {
|
|
106
|
+
return values.map((value) => this.normalizeComparisonValue(value));
|
|
107
|
+
}
|
|
108
|
+
validateFilter(filter) {
|
|
109
|
+
const validation = this.validateFilterSupport(filter);
|
|
110
|
+
if (!validation.supported) {
|
|
111
|
+
throw new Error(validation.messages.join(", "));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Validates if a filter structure is supported by the specific vector DB
|
|
116
|
+
* and returns detailed validation information.
|
|
117
|
+
*/
|
|
118
|
+
validateFilterSupport(node, path = "") {
|
|
119
|
+
const messages = [];
|
|
120
|
+
if (this.isPrimitive(node) || this.isEmpty(node)) {
|
|
121
|
+
return { supported: true, messages: [] };
|
|
122
|
+
}
|
|
123
|
+
if (Array.isArray(node)) {
|
|
124
|
+
const arrayResults = node.map((item) => this.validateFilterSupport(item, path));
|
|
125
|
+
const arrayMessages = arrayResults.flatMap((r) => r.messages);
|
|
126
|
+
return {
|
|
127
|
+
supported: arrayResults.every((r) => r.supported),
|
|
128
|
+
messages: arrayMessages
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
const nodeObj = node;
|
|
132
|
+
let isSupported = true;
|
|
133
|
+
for (const [key, value] of Object.entries(nodeObj)) {
|
|
134
|
+
const newPath = path ? `${path}.${key}` : key;
|
|
135
|
+
if (this.isOperator(key)) {
|
|
136
|
+
if (!this.isValidOperator(key)) {
|
|
137
|
+
isSupported = false;
|
|
138
|
+
messages.push(_BaseFilterTranslator.ErrorMessages.UNSUPPORTED_OPERATOR(key));
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (!path && !this.isLogicalOperator(key)) {
|
|
142
|
+
isSupported = false;
|
|
143
|
+
messages.push(_BaseFilterTranslator.ErrorMessages.INVALID_TOP_LEVEL_OPERATOR(key));
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (key === "$elemMatch" && (typeof value !== "object" || Array.isArray(value))) {
|
|
147
|
+
isSupported = false;
|
|
148
|
+
messages.push(_BaseFilterTranslator.ErrorMessages.ELEM_MATCH_REQUIRES_OBJECT);
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (this.isLogicalOperator(key)) {
|
|
152
|
+
if (key === "$not") {
|
|
153
|
+
if (Array.isArray(value) || typeof value !== "object") {
|
|
154
|
+
isSupported = false;
|
|
155
|
+
messages.push(_BaseFilterTranslator.ErrorMessages.NOT_REQUIRES_OBJECT);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (this.isEmpty(value)) {
|
|
159
|
+
isSupported = false;
|
|
160
|
+
messages.push(_BaseFilterTranslator.ErrorMessages.NOT_CANNOT_BE_EMPTY);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (path && !this.isLogicalOperator(path.split(".").pop())) {
|
|
166
|
+
isSupported = false;
|
|
167
|
+
messages.push(_BaseFilterTranslator.ErrorMessages.INVALID_LOGICAL_OPERATOR_LOCATION(key, newPath));
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (Array.isArray(value)) {
|
|
171
|
+
const hasDirectOperators = value.some(
|
|
172
|
+
(item) => typeof item === "object" && Object.keys(item).length === 1 && this.isFieldOperator(Object.keys(item)[0])
|
|
173
|
+
);
|
|
174
|
+
if (hasDirectOperators) {
|
|
175
|
+
isSupported = false;
|
|
176
|
+
messages.push(_BaseFilterTranslator.ErrorMessages.INVALID_LOGICAL_OPERATOR_CONTENT(newPath));
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const nestedValidation = this.validateFilterSupport(value, newPath);
|
|
183
|
+
if (!nestedValidation.supported) {
|
|
184
|
+
isSupported = false;
|
|
185
|
+
messages.push(...nestedValidation.messages);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return { supported: isSupported, messages };
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
exports.BaseFilterTranslator = BaseFilterTranslator;
|