neatlogs 1.0.4 → 1.0.5
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/anthropic.cjs +373 -0
- package/dist/anthropic.cjs.map +1 -0
- package/dist/anthropic.d.ts +27 -0
- package/dist/anthropic.mjs +347 -0
- package/dist/anthropic.mjs.map +1 -0
- package/dist/index.cjs +1664 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.mjs +1655 -6
- package/dist/index.mjs.map +1 -1
- package/dist/langchain.cjs +269 -0
- package/dist/langchain.cjs.map +1 -0
- package/dist/langchain.d.ts +16 -0
- package/dist/langchain.mjs +244 -0
- package/dist/langchain.mjs.map +1 -0
- package/dist/mastra-wrap.cjs +597 -0
- package/dist/mastra-wrap.cjs.map +1 -0
- package/dist/mastra-wrap.d.ts +38 -0
- package/dist/mastra-wrap.mjs +571 -0
- package/dist/mastra-wrap.mjs.map +1 -0
- package/dist/openai-agents.cjs +215 -0
- package/dist/openai-agents.cjs.map +1 -0
- package/dist/openai-agents.d.ts +13 -0
- package/dist/openai-agents.mjs +190 -0
- package/dist/openai-agents.mjs.map +1 -0
- package/dist/openai.cjs +342 -0
- package/dist/openai.cjs.map +1 -0
- package/dist/openai.d.ts +26 -0
- package/dist/openai.mjs +316 -0
- package/dist/openai.mjs.map +1 -0
- package/dist/strands.cjs +44 -0
- package/dist/strands.cjs.map +1 -0
- package/dist/strands.d.ts +27 -0
- package/dist/strands.mjs +19 -0
- package/dist/strands.mjs.map +1 -0
- package/package.json +63 -3
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/mastra-wrap.ts
|
|
21
|
+
var mastra_wrap_exports = {};
|
|
22
|
+
__export(mastra_wrap_exports, {
|
|
23
|
+
wrapMastra: () => wrapMastra,
|
|
24
|
+
wrapMastraRerank: () => wrapMastraRerank
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(mastra_wrap_exports);
|
|
27
|
+
var import_api = require("@opentelemetry/api");
|
|
28
|
+
var TRACER_NAME = "neatlogs.mastra";
|
|
29
|
+
var PATCH_FLAG = "_neatlogs_patched";
|
|
30
|
+
function wrapMastra(entity) {
|
|
31
|
+
if (!entity || entity[PATCH_FLAG]) return entity;
|
|
32
|
+
const e = entity;
|
|
33
|
+
const className = entity.constructor?.name ?? "";
|
|
34
|
+
if (isRootMastra(e)) {
|
|
35
|
+
const proxied = wrapRootMastra(e);
|
|
36
|
+
markPatched(proxied);
|
|
37
|
+
return proxied;
|
|
38
|
+
}
|
|
39
|
+
if (isAgent(e, className)) {
|
|
40
|
+
patchAgent(e);
|
|
41
|
+
} else if (isWorkflow(e, className)) {
|
|
42
|
+
patchWorkflow(e);
|
|
43
|
+
} else if (isVector(e, className)) {
|
|
44
|
+
patchVector(e);
|
|
45
|
+
} else if (isMemory(e, className)) {
|
|
46
|
+
patchMemory(e);
|
|
47
|
+
} else if (isDocument(e, className)) {
|
|
48
|
+
patchDocument(e);
|
|
49
|
+
}
|
|
50
|
+
markPatched(e);
|
|
51
|
+
return entity;
|
|
52
|
+
}
|
|
53
|
+
function wrapMastraRerank(rerankFn) {
|
|
54
|
+
return (async function tracedRerank(...args) {
|
|
55
|
+
return withSpan("mastra.rerank", { "neatlogs.span.kind": "RERANKER" }, (span) => {
|
|
56
|
+
const results = args?.[0];
|
|
57
|
+
const query = args?.[1];
|
|
58
|
+
const options = args?.[3];
|
|
59
|
+
if (typeof query === "string") {
|
|
60
|
+
span.setAttribute("neatlogs.reranker.query", query.slice(0, 1e4));
|
|
61
|
+
span.setAttribute("input.value", query.slice(0, 1e4));
|
|
62
|
+
}
|
|
63
|
+
if (Array.isArray(results)) span.setAttribute("neatlogs.reranker.input_documents", safeStringify(results).slice(0, 1e4));
|
|
64
|
+
if (options?.topK != null) span.setAttribute("neatlogs.reranker.top_k", options.topK);
|
|
65
|
+
return Promise.resolve(rerankFn(...args)).then((result) => {
|
|
66
|
+
span.setAttribute("neatlogs.reranker.output_documents", safeStringify(result).slice(0, 1e4));
|
|
67
|
+
span.setAttribute("output.value", safeStringify(result).slice(0, 1e4));
|
|
68
|
+
return result;
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function isRootMastra(e) {
|
|
74
|
+
return typeof e.getAgent === "function" && typeof e.getWorkflow === "function";
|
|
75
|
+
}
|
|
76
|
+
function isAgent(e, className) {
|
|
77
|
+
return className === "Agent" || typeof e.generate === "function" || typeof e.stream === "function";
|
|
78
|
+
}
|
|
79
|
+
function isWorkflow(e, className) {
|
|
80
|
+
return className === "Workflow" || typeof e.createRun === "function";
|
|
81
|
+
}
|
|
82
|
+
function isVector(e, className) {
|
|
83
|
+
return /Vector/.test(className) || typeof e.query === "function" && typeof e.upsert === "function";
|
|
84
|
+
}
|
|
85
|
+
function isMemory(e, className) {
|
|
86
|
+
return /Memory/.test(className) || typeof e.recall === "function" && typeof e.saveMessages === "function";
|
|
87
|
+
}
|
|
88
|
+
function isDocument(e, className) {
|
|
89
|
+
return className === "MDocument" || typeof e.chunk === "function" && typeof e.getDocs === "function";
|
|
90
|
+
}
|
|
91
|
+
function patchAgent(agent) {
|
|
92
|
+
patchAgentMethod(agent, "generate", false);
|
|
93
|
+
patchAgentMethod(agent, "stream", true);
|
|
94
|
+
}
|
|
95
|
+
function patchAgentMethod(agent, method, streaming) {
|
|
96
|
+
if (typeof agent[method] !== "function") return;
|
|
97
|
+
const orig = agent[method].bind(agent);
|
|
98
|
+
agent[method] = async function tracedAgentMethod(input, opts) {
|
|
99
|
+
const agentName = agent.name ?? agent.id ?? "mastra_agent";
|
|
100
|
+
const model = extractModelId(agent.model);
|
|
101
|
+
installAgentLlmHook(agent);
|
|
102
|
+
installAgentToolHooks(agent);
|
|
103
|
+
const attrs = {
|
|
104
|
+
"neatlogs.span.kind": "AGENT",
|
|
105
|
+
"neatlogs.agent.name": agentName,
|
|
106
|
+
"input.value": toInputValue(input)
|
|
107
|
+
};
|
|
108
|
+
if (model) attrs["neatlogs.llm.model_name"] = model;
|
|
109
|
+
if (agent.instructions && typeof agent.instructions === "string") {
|
|
110
|
+
attrs["neatlogs.llm.system_prompt"] = agent.instructions.slice(0, 5e3);
|
|
111
|
+
}
|
|
112
|
+
if (streaming) attrs["neatlogs.llm.is_streaming"] = true;
|
|
113
|
+
if (streaming) {
|
|
114
|
+
const tracer = import_api.trace.getTracer(TRACER_NAME);
|
|
115
|
+
const span = tracer.startSpan(`mastra.agent.${agentName}`, { attributes: attrs }, import_api.context.active());
|
|
116
|
+
const ctx = import_api.trace.setSpan(import_api.context.active(), span);
|
|
117
|
+
try {
|
|
118
|
+
const result = await import_api.context.with(ctx, () => orig(input, opts));
|
|
119
|
+
return wrapStreamingOutput(result, span, ctx);
|
|
120
|
+
} catch (err) {
|
|
121
|
+
recordError(span, err);
|
|
122
|
+
span.end();
|
|
123
|
+
throw err;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return withActiveSpan(`mastra.agent.${agentName}`, attrs, async (span) => {
|
|
127
|
+
const result = await orig(input, opts);
|
|
128
|
+
finalizeAgentResult(span, result);
|
|
129
|
+
return result;
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function installAgentLlmHook(agent) {
|
|
134
|
+
if (agent.__neatlogs_llm_hook || typeof agent.getLLM !== "function") return;
|
|
135
|
+
agent.__neatlogs_llm_hook = true;
|
|
136
|
+
const origGetLLM = agent.getLLM.bind(agent);
|
|
137
|
+
agent.getLLM = function patchedGetLLM(...args) {
|
|
138
|
+
const out = origGetLLM(...args);
|
|
139
|
+
return Promise.resolve(out).then((llm) => {
|
|
140
|
+
try {
|
|
141
|
+
const model = typeof llm?.getModel === "function" ? llm.getModel() : llm;
|
|
142
|
+
patchModelInPlace(model);
|
|
143
|
+
} catch {
|
|
144
|
+
}
|
|
145
|
+
return llm;
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
function patchModelInPlace(model) {
|
|
150
|
+
if (!model || model.__neatlogs_model_patched) return;
|
|
151
|
+
model.__neatlogs_model_patched = true;
|
|
152
|
+
const modelId = model.modelId ?? model.modelName ?? "";
|
|
153
|
+
const provider = model.provider ?? "";
|
|
154
|
+
for (const fn of ["doGenerate", "doStream"]) {
|
|
155
|
+
if (typeof model[fn] !== "function") continue;
|
|
156
|
+
const orig = model[fn].bind(model);
|
|
157
|
+
const isStream = fn === "doStream";
|
|
158
|
+
model[fn] = function tracedModelCall(callOpts) {
|
|
159
|
+
const attrs = { "neatlogs.span.kind": "LLM" };
|
|
160
|
+
if (modelId) attrs["neatlogs.llm.model_name"] = modelId;
|
|
161
|
+
if (provider) attrs["neatlogs.llm.provider"] = provider;
|
|
162
|
+
if (isStream) attrs["neatlogs.llm.is_streaming"] = true;
|
|
163
|
+
const promptInput = callOpts?.prompt ?? callOpts?.messages;
|
|
164
|
+
if (promptInput !== void 0) attrs["input.value"] = safeStringify(promptInput).slice(0, 1e4);
|
|
165
|
+
captureInvocationParams(attrs, callOpts);
|
|
166
|
+
return withActiveSpan(`mastra.llm.${modelId || "model"}.${fn}`, attrs, async (span) => {
|
|
167
|
+
const result = await orig(callOpts);
|
|
168
|
+
if (!isStream) finalizeModelResult(span, result);
|
|
169
|
+
else if (result?.usage) recordUsage(span, result.usage);
|
|
170
|
+
return result;
|
|
171
|
+
});
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
function installAgentToolHooks(agent) {
|
|
176
|
+
if (agent.__neatlogs_tool_hook) return;
|
|
177
|
+
let tools;
|
|
178
|
+
try {
|
|
179
|
+
tools = typeof agent.listTools === "function" ? agent.listTools() : void 0;
|
|
180
|
+
} catch {
|
|
181
|
+
tools = void 0;
|
|
182
|
+
}
|
|
183
|
+
if (!tools || typeof tools !== "object") return;
|
|
184
|
+
agent.__neatlogs_tool_hook = true;
|
|
185
|
+
for (const [key, tool] of Object.entries(tools)) {
|
|
186
|
+
patchToolExecute(tool, key);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function patchToolExecute(tool, key) {
|
|
190
|
+
if (!tool || typeof tool.execute !== "function" || tool.__neatlogs_tool_patched) return;
|
|
191
|
+
tool.__neatlogs_tool_patched = true;
|
|
192
|
+
const toolName = tool.id ?? key;
|
|
193
|
+
const orig = tool.execute.bind(tool);
|
|
194
|
+
tool.execute = function tracedToolExecute(params, options) {
|
|
195
|
+
const attrs = {
|
|
196
|
+
"neatlogs.span.kind": "TOOL",
|
|
197
|
+
"neatlogs.tool.name": toolName,
|
|
198
|
+
"input.value": safeStringify(params).slice(0, 1e4)
|
|
199
|
+
};
|
|
200
|
+
if (tool.description) attrs["neatlogs.tool.description"] = String(tool.description).slice(0, 2e3);
|
|
201
|
+
return withActiveSpan(`mastra.tool.${toolName}`, attrs, async (span) => {
|
|
202
|
+
const result = await orig(params, options);
|
|
203
|
+
span.setAttribute("output.value", safeStringify(result).slice(0, 1e4));
|
|
204
|
+
return result;
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function patchWorkflow(workflow) {
|
|
209
|
+
if (typeof workflow.createRun !== "function") return;
|
|
210
|
+
const origCreateRun = workflow.createRun.bind(workflow);
|
|
211
|
+
const workflowName = workflow.name ?? workflow.id ?? "mastra_workflow";
|
|
212
|
+
workflow.createRun = async function tracedCreateRun(...args) {
|
|
213
|
+
const run = await origCreateRun(...args);
|
|
214
|
+
if (!run) return run;
|
|
215
|
+
patchRunMethod(run, "start", workflowName);
|
|
216
|
+
patchRunMethod(run, "resume", workflowName);
|
|
217
|
+
return run;
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
function patchRunMethod(run, method, workflowName) {
|
|
221
|
+
if (typeof run[method] !== "function") return;
|
|
222
|
+
const orig = run[method].bind(run);
|
|
223
|
+
run[method] = async function tracedRunMethod(startOpts) {
|
|
224
|
+
const attrs = {
|
|
225
|
+
"neatlogs.span.kind": "WORKFLOW",
|
|
226
|
+
"neatlogs.workflow.name": workflowName
|
|
227
|
+
};
|
|
228
|
+
if (startOpts?.inputData !== void 0) {
|
|
229
|
+
attrs["input.value"] = safeStringify(startOpts.inputData).slice(0, 1e4);
|
|
230
|
+
}
|
|
231
|
+
return withActiveSpan(`mastra.workflow.${workflowName}`, attrs, async (span) => {
|
|
232
|
+
const result = await orig(startOpts);
|
|
233
|
+
if (result?.status) span.setAttribute("neatlogs.metadata", safeStringify({ status: result.status }));
|
|
234
|
+
if (result?.result !== void 0) {
|
|
235
|
+
span.setAttribute("output.value", safeStringify(result.result).slice(0, 1e4));
|
|
236
|
+
}
|
|
237
|
+
return result;
|
|
238
|
+
});
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
var VECTOR_READ_OPS = ["query"];
|
|
242
|
+
var VECTOR_WRITE_OPS = ["upsert", "updateVector", "deleteVector", "createIndex", "deleteIndex"];
|
|
243
|
+
function patchVector(vector) {
|
|
244
|
+
for (const op of VECTOR_READ_OPS) patchVectorOp(vector, op, "RETRIEVER");
|
|
245
|
+
for (const op of VECTOR_WRITE_OPS) patchVectorOp(vector, op, "VECTOR_STORE");
|
|
246
|
+
}
|
|
247
|
+
function patchVectorOp(vector, op, kind) {
|
|
248
|
+
if (typeof vector[op] !== "function") return;
|
|
249
|
+
const orig = vector[op].bind(vector);
|
|
250
|
+
const dbName = vector.constructor?.name ?? "vector";
|
|
251
|
+
vector[op] = async function tracedVectorOp(params) {
|
|
252
|
+
const attrs = {
|
|
253
|
+
"neatlogs.span.kind": kind,
|
|
254
|
+
"neatlogs.db.system": dbName,
|
|
255
|
+
"neatlogs.db.operation": op,
|
|
256
|
+
"input.value": safeStringify(params).slice(0, 1e4)
|
|
257
|
+
};
|
|
258
|
+
const indexName = params?.indexName;
|
|
259
|
+
if (indexName) attrs["neatlogs.vectordb.index_name"] = String(indexName);
|
|
260
|
+
if (kind === "RETRIEVER" && params?.topK != null) attrs["neatlogs.retriever.top_k"] = params.topK;
|
|
261
|
+
return withActiveSpan(`mastra.vector.${op}`, attrs, async (span) => {
|
|
262
|
+
const result = await orig(params);
|
|
263
|
+
span.setAttribute("output.value", safeStringify(result).slice(0, 1e4));
|
|
264
|
+
return result;
|
|
265
|
+
});
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
var MEMORY_OPS = ["recall", "saveMessages", "updateWorkingMemory", "deleteMessages"];
|
|
269
|
+
function patchMemory(memory) {
|
|
270
|
+
for (const op of MEMORY_OPS) {
|
|
271
|
+
if (typeof memory[op] !== "function") continue;
|
|
272
|
+
const orig = memory[op].bind(memory);
|
|
273
|
+
memory[op] = async function tracedMemoryOp(...args) {
|
|
274
|
+
const attrs = {
|
|
275
|
+
"neatlogs.span.kind": "CHAIN",
|
|
276
|
+
"neatlogs.db.operation": op,
|
|
277
|
+
"input.value": safeStringify(args?.[0]).slice(0, 1e4)
|
|
278
|
+
};
|
|
279
|
+
return withActiveSpan(`mastra.memory.${op}`, attrs, async (span) => {
|
|
280
|
+
const result = await orig(...args);
|
|
281
|
+
span.setAttribute("output.value", safeStringify(result).slice(0, 8e3));
|
|
282
|
+
return result;
|
|
283
|
+
});
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function patchDocument(doc) {
|
|
288
|
+
if (typeof doc.chunk !== "function") return;
|
|
289
|
+
const orig = doc.chunk.bind(doc);
|
|
290
|
+
doc.chunk = async function tracedChunk(...args) {
|
|
291
|
+
const attrs = {
|
|
292
|
+
"neatlogs.span.kind": "CHAIN",
|
|
293
|
+
"neatlogs.db.operation": "chunk"
|
|
294
|
+
};
|
|
295
|
+
return withActiveSpan("mastra.document.chunk", attrs, async (span) => {
|
|
296
|
+
const result = await orig(...args);
|
|
297
|
+
if (Array.isArray(result)) span.setAttribute("neatlogs.db.documents_count", result.length);
|
|
298
|
+
return result;
|
|
299
|
+
});
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
function wrapRootMastra(mastra) {
|
|
303
|
+
const AGENT_GETTERS = /* @__PURE__ */ new Set(["getAgent", "getAgentById"]);
|
|
304
|
+
const WORKFLOW_GETTERS = /* @__PURE__ */ new Set(["getWorkflow", "getWorkflowById"]);
|
|
305
|
+
return new Proxy(mastra, {
|
|
306
|
+
get(target, prop, receiver) {
|
|
307
|
+
const value = Reflect.get(target, prop, receiver);
|
|
308
|
+
if (typeof value !== "function") return value;
|
|
309
|
+
const name = String(prop);
|
|
310
|
+
if (AGENT_GETTERS.has(name) || WORKFLOW_GETTERS.has(name)) {
|
|
311
|
+
return (...args) => {
|
|
312
|
+
const entity = value.apply(target, args);
|
|
313
|
+
return entity && typeof entity === "object" ? wrapMastra(entity) : entity;
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
return value.bind(target);
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
function wrapStreamingOutput(output, span, ctx) {
|
|
321
|
+
if (!output) {
|
|
322
|
+
span.setStatus({ code: import_api.SpanStatusCode.OK });
|
|
323
|
+
span.end();
|
|
324
|
+
return output;
|
|
325
|
+
}
|
|
326
|
+
let ended = false;
|
|
327
|
+
const endOnce = (err) => {
|
|
328
|
+
if (ended) return;
|
|
329
|
+
ended = true;
|
|
330
|
+
if (err) recordError(span, err);
|
|
331
|
+
else span.setStatus({ code: import_api.SpanStatusCode.OK });
|
|
332
|
+
span.end();
|
|
333
|
+
};
|
|
334
|
+
if (output.text && typeof output.text.then === "function") {
|
|
335
|
+
const finalize = async () => {
|
|
336
|
+
try {
|
|
337
|
+
const [text, usage, finishReason] = await Promise.all([
|
|
338
|
+
Promise.resolve(output.text).catch(() => void 0),
|
|
339
|
+
Promise.resolve(output.usage).catch(() => void 0),
|
|
340
|
+
Promise.resolve(output.finishReason).catch(() => void 0)
|
|
341
|
+
]);
|
|
342
|
+
if (typeof text === "string" && text) {
|
|
343
|
+
span.setAttribute("neatlogs.llm.output_messages.0.role", "assistant");
|
|
344
|
+
span.setAttribute("neatlogs.llm.output_messages.0.content", text.slice(0, 1e4));
|
|
345
|
+
}
|
|
346
|
+
if (usage) recordUsage(span, usage);
|
|
347
|
+
if (finishReason) span.setAttribute("neatlogs.llm.stop_reason", normalizeFinishReason(finishReason));
|
|
348
|
+
endOnce();
|
|
349
|
+
} catch (err) {
|
|
350
|
+
endOnce(err);
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
void finalize();
|
|
354
|
+
return rebindStreamContext(output, ctx);
|
|
355
|
+
}
|
|
356
|
+
if (output[Symbol.asyncIterator]) {
|
|
357
|
+
const origIterator = output[Symbol.asyncIterator].bind(output);
|
|
358
|
+
const textParts = [];
|
|
359
|
+
const wrapped = Object.create(Object.getPrototypeOf(output));
|
|
360
|
+
Object.assign(wrapped, output);
|
|
361
|
+
wrapped[Symbol.asyncIterator] = function() {
|
|
362
|
+
const iterator = origIterator();
|
|
363
|
+
const finish = () => {
|
|
364
|
+
if (textParts.length) {
|
|
365
|
+
span.setAttribute("neatlogs.llm.output_messages.0.role", "assistant");
|
|
366
|
+
span.setAttribute("neatlogs.llm.output_messages.0.content", textParts.join("").slice(0, 1e4));
|
|
367
|
+
}
|
|
368
|
+
endOnce();
|
|
369
|
+
};
|
|
370
|
+
return {
|
|
371
|
+
async next() {
|
|
372
|
+
try {
|
|
373
|
+
const r = await iterator.next();
|
|
374
|
+
if (r.done) {
|
|
375
|
+
finish();
|
|
376
|
+
return r;
|
|
377
|
+
}
|
|
378
|
+
const chunk = r.value;
|
|
379
|
+
if (typeof chunk === "string") textParts.push(chunk);
|
|
380
|
+
else if (chunk?.text) textParts.push(chunk.text);
|
|
381
|
+
else if (chunk?.delta) textParts.push(chunk.delta);
|
|
382
|
+
return r;
|
|
383
|
+
} catch (err) {
|
|
384
|
+
endOnce(err);
|
|
385
|
+
throw err;
|
|
386
|
+
}
|
|
387
|
+
},
|
|
388
|
+
async return(value) {
|
|
389
|
+
finish();
|
|
390
|
+
return iterator.return?.(value) ?? { done: true, value: void 0 };
|
|
391
|
+
},
|
|
392
|
+
async throw(err) {
|
|
393
|
+
endOnce(err);
|
|
394
|
+
return iterator.throw?.(err) ?? { done: true, value: void 0 };
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
};
|
|
398
|
+
return wrapped;
|
|
399
|
+
}
|
|
400
|
+
if (typeof output.then === "function") {
|
|
401
|
+
return output.then((resolved) => {
|
|
402
|
+
finalizeAgentResult(span, resolved);
|
|
403
|
+
endOnce();
|
|
404
|
+
return resolved;
|
|
405
|
+
}).catch((err) => {
|
|
406
|
+
endOnce(err);
|
|
407
|
+
throw err;
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
endOnce();
|
|
411
|
+
return output;
|
|
412
|
+
}
|
|
413
|
+
function rebindStreamContext(output, ctx) {
|
|
414
|
+
const STREAM_PROPS = /* @__PURE__ */ new Set(["textStream", "fullStream", "objectStream", "elementStream"]);
|
|
415
|
+
return new Proxy(output, {
|
|
416
|
+
get(target, prop, receiver) {
|
|
417
|
+
const value = Reflect.get(target, prop, receiver);
|
|
418
|
+
if (typeof prop === "string" && STREAM_PROPS.has(prop) && value && value[Symbol.asyncIterator]) {
|
|
419
|
+
const origIterator = value[Symbol.asyncIterator].bind(value);
|
|
420
|
+
return {
|
|
421
|
+
[Symbol.asyncIterator]() {
|
|
422
|
+
return import_api.context.with(ctx, () => origIterator());
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
if (typeof value === "function") return value.bind(target);
|
|
427
|
+
return value;
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
function finalizeAgentResult(span, result) {
|
|
432
|
+
if (!result) return;
|
|
433
|
+
if (result.text) {
|
|
434
|
+
span.setAttribute("neatlogs.llm.output_messages.0.role", "assistant");
|
|
435
|
+
span.setAttribute("neatlogs.llm.output_messages.0.content", String(result.text).slice(0, 1e4));
|
|
436
|
+
}
|
|
437
|
+
if (Array.isArray(result.toolCalls)) {
|
|
438
|
+
for (let i = 0; i < result.toolCalls.length; i++) {
|
|
439
|
+
const tc = result.toolCalls[i];
|
|
440
|
+
const p = tc.payload ?? tc;
|
|
441
|
+
setToolCall(span, i, p.toolName ?? p.name, p.args ?? p.arguments, p.toolCallId ?? p.id);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
if (result.usage) recordUsage(span, result.usage);
|
|
445
|
+
if (result.finishReason) span.setAttribute("neatlogs.llm.stop_reason", normalizeFinishReason(result.finishReason));
|
|
446
|
+
if (result.model) span.setAttribute("neatlogs.llm.model_name", result.model);
|
|
447
|
+
}
|
|
448
|
+
function finalizeModelResult(span, result) {
|
|
449
|
+
if (!result) return;
|
|
450
|
+
if (typeof result.text === "string" && result.text) {
|
|
451
|
+
span.setAttribute("neatlogs.llm.output_messages.0.role", "assistant");
|
|
452
|
+
span.setAttribute("neatlogs.llm.output_messages.0.content", result.text.slice(0, 1e4));
|
|
453
|
+
} else if (Array.isArray(result.content)) {
|
|
454
|
+
const text = result.content.filter((p) => p?.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
|
|
455
|
+
if (text) {
|
|
456
|
+
span.setAttribute("neatlogs.llm.output_messages.0.role", "assistant");
|
|
457
|
+
span.setAttribute("neatlogs.llm.output_messages.0.content", text.slice(0, 1e4));
|
|
458
|
+
}
|
|
459
|
+
const toolCalls = result.content.filter((p) => p?.type === "tool-call");
|
|
460
|
+
for (let i = 0; i < toolCalls.length; i++) {
|
|
461
|
+
const tc = toolCalls[i];
|
|
462
|
+
setToolCall(span, i, tc.toolName, tc.input ?? tc.args, tc.toolCallId);
|
|
463
|
+
}
|
|
464
|
+
span.setAttribute("output.value", safeStringify(result.content).slice(0, 1e4));
|
|
465
|
+
}
|
|
466
|
+
if (result.usage) recordUsage(span, result.usage);
|
|
467
|
+
span.setAttribute("neatlogs.llm.stop_reason", normalizeFinishReason(result.finishReason));
|
|
468
|
+
}
|
|
469
|
+
function normalizeFinishReason(fr) {
|
|
470
|
+
if (fr == null) return "";
|
|
471
|
+
if (typeof fr === "string") return fr;
|
|
472
|
+
return String(fr.unified ?? fr.reason ?? fr.type ?? safeStringify(fr));
|
|
473
|
+
}
|
|
474
|
+
function tokenValue(v) {
|
|
475
|
+
if (v == null) return void 0;
|
|
476
|
+
if (typeof v === "number") return v;
|
|
477
|
+
if (typeof v === "object" && typeof v.total === "number") return v.total;
|
|
478
|
+
const n = Number(v);
|
|
479
|
+
return Number.isFinite(n) ? n : void 0;
|
|
480
|
+
}
|
|
481
|
+
function captureInvocationParams(attrs, callOpts) {
|
|
482
|
+
if (!callOpts) return;
|
|
483
|
+
const map = [
|
|
484
|
+
["temperature", "neatlogs.llm.temperature"],
|
|
485
|
+
["maxOutputTokens", "neatlogs.llm.max_tokens"],
|
|
486
|
+
["maxTokens", "neatlogs.llm.max_tokens"],
|
|
487
|
+
["topP", "neatlogs.llm.top_p"],
|
|
488
|
+
["topK", "neatlogs.llm.top_k"],
|
|
489
|
+
["frequencyPenalty", "neatlogs.llm.frequency_penalty"],
|
|
490
|
+
["presencePenalty", "neatlogs.llm.presence_penalty"]
|
|
491
|
+
];
|
|
492
|
+
const invocation = {};
|
|
493
|
+
for (const [src, target] of map) {
|
|
494
|
+
const v = callOpts[src];
|
|
495
|
+
if (v != null) {
|
|
496
|
+
attrs[target] = v;
|
|
497
|
+
invocation[src] = v;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
if (Array.isArray(callOpts.stopSequences) && callOpts.stopSequences.length) {
|
|
501
|
+
attrs["neatlogs.llm.stop_sequences"] = safeStringify(callOpts.stopSequences);
|
|
502
|
+
invocation.stopSequences = callOpts.stopSequences;
|
|
503
|
+
}
|
|
504
|
+
if (Array.isArray(callOpts.tools)) {
|
|
505
|
+
for (let i = 0; i < callOpts.tools.length; i++) {
|
|
506
|
+
attrs[`neatlogs.llm.tools.${i}`] = safeStringify(callOpts.tools[i]).slice(0, 4e3);
|
|
507
|
+
}
|
|
508
|
+
invocation.toolChoice = callOpts.toolChoice;
|
|
509
|
+
}
|
|
510
|
+
if (Object.keys(invocation).length) {
|
|
511
|
+
attrs["neatlogs.llm.invocation_parameters"] = safeStringify(invocation).slice(0, 4e3);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
function setToolCall(span, i, name, args, id) {
|
|
515
|
+
span.setAttribute(`neatlogs.llm.tool_calls.${i}.name`, name ?? "");
|
|
516
|
+
span.setAttribute(`neatlogs.llm.tool_calls.${i}.arguments`, safeStringify(args ?? {}));
|
|
517
|
+
if (id) span.setAttribute(`neatlogs.llm.tool_calls.${i}.id`, id);
|
|
518
|
+
}
|
|
519
|
+
function recordUsage(span, usage) {
|
|
520
|
+
if (!usage) return;
|
|
521
|
+
const prompt = tokenValue(usage.promptTokens ?? usage.inputTokens ?? usage.input_tokens);
|
|
522
|
+
const completion = tokenValue(usage.completionTokens ?? usage.outputTokens ?? usage.output_tokens);
|
|
523
|
+
const total = tokenValue(usage.totalTokens);
|
|
524
|
+
if (prompt != null) span.setAttribute("neatlogs.llm.token_count.prompt", prompt);
|
|
525
|
+
if (completion != null) span.setAttribute("neatlogs.llm.token_count.completion", completion);
|
|
526
|
+
if (total != null) span.setAttribute("neatlogs.llm.token_count.total", total);
|
|
527
|
+
else if (prompt != null && completion != null) {
|
|
528
|
+
span.setAttribute("neatlogs.llm.token_count.total", prompt + completion);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
function withActiveSpan(name, attrs, fn) {
|
|
532
|
+
const tracer = import_api.trace.getTracer(TRACER_NAME);
|
|
533
|
+
const span = tracer.startSpan(name, { attributes: attrs }, import_api.context.active());
|
|
534
|
+
const ctx = import_api.trace.setSpan(import_api.context.active(), span);
|
|
535
|
+
return import_api.context.with(ctx, async () => {
|
|
536
|
+
try {
|
|
537
|
+
const result = await fn(span);
|
|
538
|
+
span.setStatus({ code: import_api.SpanStatusCode.OK });
|
|
539
|
+
return result;
|
|
540
|
+
} catch (err) {
|
|
541
|
+
recordError(span, err);
|
|
542
|
+
throw err;
|
|
543
|
+
} finally {
|
|
544
|
+
span.end();
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
function withSpan(name, attrs, fn) {
|
|
549
|
+
const tracer = import_api.trace.getTracer(TRACER_NAME);
|
|
550
|
+
const span = tracer.startSpan(name, { attributes: attrs }, import_api.context.active());
|
|
551
|
+
return Promise.resolve().then(() => fn(span)).then((result) => {
|
|
552
|
+
span.setStatus({ code: import_api.SpanStatusCode.OK });
|
|
553
|
+
span.end();
|
|
554
|
+
return result;
|
|
555
|
+
}).catch((err) => {
|
|
556
|
+
recordError(span, err);
|
|
557
|
+
span.end();
|
|
558
|
+
throw err;
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
function markPatched(e) {
|
|
562
|
+
try {
|
|
563
|
+
Object.defineProperty(e, PATCH_FLAG, { value: true, enumerable: false, configurable: true });
|
|
564
|
+
} catch {
|
|
565
|
+
e[PATCH_FLAG] = true;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
function extractModelId(model) {
|
|
569
|
+
if (!model) return "";
|
|
570
|
+
if (typeof model === "string") return model;
|
|
571
|
+
return model.modelId ?? model.name ?? "";
|
|
572
|
+
}
|
|
573
|
+
function toInputValue(input) {
|
|
574
|
+
return (typeof input === "string" ? input : safeStringify(input)).slice(0, 1e4);
|
|
575
|
+
}
|
|
576
|
+
function safeStringify(value) {
|
|
577
|
+
if (typeof value === "string") return value;
|
|
578
|
+
try {
|
|
579
|
+
return JSON.stringify(value) ?? "";
|
|
580
|
+
} catch {
|
|
581
|
+
return "";
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
function recordError(span, err) {
|
|
585
|
+
if (err instanceof Error) {
|
|
586
|
+
span.setStatus({ code: import_api.SpanStatusCode.ERROR, message: err.message });
|
|
587
|
+
span.recordException(err);
|
|
588
|
+
} else {
|
|
589
|
+
span.setStatus({ code: import_api.SpanStatusCode.ERROR, message: String(err) });
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
593
|
+
0 && (module.exports = {
|
|
594
|
+
wrapMastra,
|
|
595
|
+
wrapMastraRerank
|
|
596
|
+
});
|
|
597
|
+
//# sourceMappingURL=mastra-wrap.cjs.map
|