neatlogs 1.1.4 → 1.1.6

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/index.cjs CHANGED
@@ -66,7 +66,8 @@ __export(index_exports, {
66
66
  trace: () => trace2,
67
67
  traceToolAnthropic: () => traceTool2,
68
68
  traceToolAzureOpenAI: () => traceTool3,
69
- traceToolBedrock: () => traceTool5,
69
+ traceToolBedrock: () => traceTool6,
70
+ traceToolGoogleGenAI: () => traceTool5,
70
71
  traceToolOpenAI: () => traceTool,
71
72
  traceToolVertexAI: () => traceTool4,
72
73
  updatePrompt: () => updatePrompt,
@@ -76,6 +77,8 @@ __export(index_exports, {
76
77
  wrapBedrock: () => wrapBedrock,
77
78
  wrapCallModel: () => wrapCallModel,
78
79
  wrapClaudeAgentSDK: () => wrapClaudeAgentSDK,
80
+ wrapGoogleGenAI: () => wrapGoogleGenAI,
81
+ wrapGoogleGenAIChat: () => wrapGoogleGenAIChat,
79
82
  wrapMastra: () => wrapMastra,
80
83
  wrapOpenAI: () => wrapOpenAI,
81
84
  wrapOpenRouterAgent: () => wrapOpenRouterAgent,
@@ -5820,7 +5823,7 @@ async function removeTag(name, tag) {
5820
5823
  }
5821
5824
 
5822
5825
  // src/version.ts
5823
- var __version__ = "1.1.4";
5826
+ var __version__ = "1.1.6";
5824
5827
 
5825
5828
  // src/init.ts
5826
5829
  var logger13 = getLogger();
@@ -6313,6 +6316,19 @@ function setOutputValue(span2, result) {
6313
6316
  span2.setAttribute("output.value", stringified);
6314
6317
  }
6315
6318
  }
6319
+ function setStreamOutputValue(span2, event) {
6320
+ if (!event || typeof event !== "object") return;
6321
+ const e = event;
6322
+ if (typeof e.text === "string" && e.text) {
6323
+ span2.setAttribute("output.value", e.text);
6324
+ } else if ("object" in e && e.object !== void 0) {
6325
+ const stringified = safeStringify2(e.object);
6326
+ if (stringified) span2.setAttribute("output.value", stringified);
6327
+ }
6328
+ if (e.finishReason) {
6329
+ span2.setAttribute("gen_ai.finish_reason", String(e.finishReason));
6330
+ }
6331
+ }
6316
6332
  var WRAPPED_FUNCTIONS = [
6317
6333
  "generateText",
6318
6334
  "streamText",
@@ -6328,7 +6344,7 @@ function wrapAISDK(aiModule) {
6328
6344
  const original = aiModule[name];
6329
6345
  if (typeof original !== "function") continue;
6330
6346
  if (name === "streamText" || name === "streamObject") {
6331
- wrapped[name] = createSyncWrapper(name, original);
6347
+ wrapped[name] = createStreamWrapper(name, original);
6332
6348
  } else {
6333
6349
  wrapped[name] = createAsyncWrapper(name, original);
6334
6350
  }
@@ -6381,20 +6397,46 @@ function createAsyncWrapper(name, original) {
6381
6397
  );
6382
6398
  };
6383
6399
  }
6384
- function createSyncWrapper(name, original) {
6385
- return function wrappedSyncFn(opts) {
6400
+ function createStreamWrapper(name, original) {
6401
+ return function wrappedStreamFn(opts) {
6386
6402
  const tracer = import_api9.trace.getTracer(TRACER_NAME2);
6387
6403
  return tracer.startActiveSpan(`ai.${name}`, { attributes: { "openinference.span.kind": rootSpanKind(name) } }, getParentContext(), (span2) => {
6404
+ let spanEnded = false;
6405
+ const endOnce = () => {
6406
+ if (spanEnded) return;
6407
+ spanEnded = true;
6408
+ span2.end();
6409
+ };
6388
6410
  try {
6389
6411
  setInputValue(span2, opts);
6390
6412
  const merged = mergeTelemetry(opts);
6391
- const result = original(merged);
6392
- return result;
6413
+ const userOnFinish = opts?.onFinish;
6414
+ const userOnError = opts?.onError;
6415
+ const wrappedOpts = {
6416
+ ...merged,
6417
+ onFinish: async (event) => {
6418
+ try {
6419
+ setStreamOutputValue(span2, event);
6420
+ } finally {
6421
+ endOnce();
6422
+ }
6423
+ if (typeof userOnFinish === "function") {
6424
+ return userOnFinish(event);
6425
+ }
6426
+ },
6427
+ onError: (event) => {
6428
+ recordSpanError(span2, (event && event.error) ?? event);
6429
+ endOnce();
6430
+ if (typeof userOnError === "function") {
6431
+ return userOnError(event);
6432
+ }
6433
+ }
6434
+ };
6435
+ return original(wrappedOpts);
6393
6436
  } catch (err) {
6394
6437
  recordSpanError(span2, err);
6438
+ endOnce();
6395
6439
  throw err;
6396
- } finally {
6397
- span2.end();
6398
6440
  }
6399
6441
  });
6400
6442
  };
@@ -7881,10 +7923,14 @@ function recordError4(span2, err) {
7881
7923
  span2.end();
7882
7924
  }
7883
7925
 
7884
- // src/bedrock.ts
7926
+ // src/google-genai.ts
7885
7927
  var import_api15 = require("@opentelemetry/api");
7886
- var TRACER_NAME7 = "neatlogs.bedrock";
7887
- var PROVIDER3 = "bedrock";
7928
+ var TRACER_NAME7 = "neatlogs.google_genai";
7929
+ var PROVIDER3 = "google";
7930
+ var SYSTEM3 = "google_genai";
7931
+ function wrapGoogleGenAI(client) {
7932
+ return wrapNamespace5(client, []);
7933
+ }
7888
7934
  function traceTool5(name, fn) {
7889
7935
  return async function tracedTool(args) {
7890
7936
  const tracer = getProviderTracer(TRACER_NAME7);
@@ -7914,6 +7960,394 @@ function traceTool5(name, fn) {
7914
7960
  );
7915
7961
  };
7916
7962
  }
7963
+ function wrapNamespace5(target, path3) {
7964
+ return new Proxy(target, {
7965
+ get(obj, prop, receiver) {
7966
+ const value = Reflect.get(obj, prop, receiver);
7967
+ if (typeof prop === "symbol" || String(prop).startsWith("_")) return value;
7968
+ const currentPath = [...path3, String(prop)];
7969
+ const pathStr = currentPath.join(".");
7970
+ if (pathStr === "models.generateContent" && typeof value === "function") {
7971
+ return tracedGenerateContent2(value.bind(obj), false);
7972
+ }
7973
+ if (pathStr === "models.generateContentStream" && typeof value === "function") {
7974
+ return tracedGenerateContent2(value.bind(obj), true);
7975
+ }
7976
+ if (pathStr === "models.embedContent" && typeof value === "function") {
7977
+ return tracedEmbedContent2(value.bind(obj));
7978
+ }
7979
+ if (pathStr === "models.countTokens" && typeof value === "function") {
7980
+ return tracedCountTokens2(value.bind(obj));
7981
+ }
7982
+ if (value && typeof value === "object" && !Array.isArray(value) && isNamespace5(currentPath)) {
7983
+ return wrapNamespace5(value, currentPath);
7984
+ }
7985
+ return value;
7986
+ }
7987
+ });
7988
+ }
7989
+ function isNamespace5(path3) {
7990
+ if (path3.length > 2) return false;
7991
+ return ["models", "chats"].includes(path3[path3.length - 1]);
7992
+ }
7993
+ function wrapGoogleGenAIChat(chat) {
7994
+ const c = chat;
7995
+ if (!c || c._neatlogsGoogleGenAIPatched) return chat;
7996
+ if (typeof c.sendMessage === "function") {
7997
+ const orig = c.sendMessage.bind(c);
7998
+ c.sendMessage = (params, ...rest) => tracedChatSend2(orig, c, params, rest, false);
7999
+ }
8000
+ if (typeof c.sendMessageStream === "function") {
8001
+ const orig = c.sendMessageStream.bind(c);
8002
+ c.sendMessageStream = (params, ...rest) => tracedChatSend2(orig, c, params, rest, true);
8003
+ }
8004
+ try {
8005
+ Object.defineProperty(c, "_neatlogsGoogleGenAIPatched", { value: true, enumerable: false, configurable: true });
8006
+ } catch {
8007
+ c._neatlogsGoogleGenAIPatched = true;
8008
+ }
8009
+ return chat;
8010
+ }
8011
+ function tracedChatSend2(original, chat, params, rest, isStream) {
8012
+ const tracer = getProviderTracer(TRACER_NAME7);
8013
+ const model = chat?.model ?? chat?.modelVersion ?? "";
8014
+ const span2 = tracer.startSpan("google_genai.chat.send_message", {
8015
+ attributes: {
8016
+ "neatlogs.span.kind": "LLM",
8017
+ "neatlogs.llm.provider": PROVIDER3,
8018
+ "neatlogs.llm.system": SYSTEM3,
8019
+ "neatlogs.llm.model_name": model,
8020
+ "neatlogs.llm.is_streaming": isStream
8021
+ }
8022
+ }, import_api15.context.active());
8023
+ const message = params?.message ?? params;
8024
+ span2.setAttribute("neatlogs.llm.input_messages.0.role", "user");
8025
+ span2.setAttribute(
8026
+ "neatlogs.llm.input_messages.0.content",
8027
+ typeof message === "string" ? message : safeStringify7(message)
8028
+ );
8029
+ const ctx = import_api15.trace.setSpan(import_api15.context.active(), span2);
8030
+ const result = import_api15.context.with(ctx, () => original(params, ...rest));
8031
+ return Promise.resolve(result).then(
8032
+ (response) => {
8033
+ if (isStream) return wrapStream2(response, span2);
8034
+ finalizeResponse2(span2, response);
8035
+ return response;
8036
+ },
8037
+ (err) => {
8038
+ recordError5(span2, err);
8039
+ throw err;
8040
+ }
8041
+ );
8042
+ }
8043
+ function tracedEmbedContent2(original) {
8044
+ return function(opts, ...rest) {
8045
+ const tracer = getProviderTracer(TRACER_NAME7);
8046
+ const span2 = tracer.startSpan("google_genai.models.embed_content", {
8047
+ attributes: {
8048
+ "neatlogs.span.kind": "EMBEDDING",
8049
+ "neatlogs.llm.provider": PROVIDER3,
8050
+ "neatlogs.embedding.model_name": opts?.model ?? "",
8051
+ "neatlogs.embedding.text": safeStringify7(opts?.contents ?? "")
8052
+ }
8053
+ }, import_api15.context.active());
8054
+ const ctx = import_api15.trace.setSpan(import_api15.context.active(), span2);
8055
+ const result = import_api15.context.with(ctx, () => original(opts, ...rest));
8056
+ return Promise.resolve(result).then(
8057
+ (response) => {
8058
+ const embeddings = response?.embeddings;
8059
+ if (Array.isArray(embeddings)) {
8060
+ span2.setAttribute("neatlogs.embedding.count", embeddings.length);
8061
+ const vals = embeddings[0]?.values;
8062
+ if (Array.isArray(vals)) span2.setAttribute("neatlogs.embedding.dimensions", vals.length);
8063
+ }
8064
+ span2.setStatus({ code: import_api15.SpanStatusCode.OK });
8065
+ span2.end();
8066
+ return response;
8067
+ },
8068
+ (err) => {
8069
+ recordError5(span2, err);
8070
+ throw err;
8071
+ }
8072
+ );
8073
+ };
8074
+ }
8075
+ function tracedCountTokens2(original) {
8076
+ return function(opts, ...rest) {
8077
+ const tracer = getProviderTracer(TRACER_NAME7);
8078
+ const span2 = tracer.startSpan("google_genai.models.count_tokens", {
8079
+ attributes: {
8080
+ "neatlogs.span.kind": "LLM",
8081
+ "neatlogs.llm.provider": PROVIDER3,
8082
+ "neatlogs.llm.task": "count_tokens",
8083
+ "neatlogs.llm.model_name": opts?.model ?? ""
8084
+ }
8085
+ }, import_api15.context.active());
8086
+ const ctx = import_api15.trace.setSpan(import_api15.context.active(), span2);
8087
+ const result = import_api15.context.with(ctx, () => original(opts, ...rest));
8088
+ return Promise.resolve(result).then(
8089
+ (response) => {
8090
+ if (response?.totalTokens != null) span2.setAttribute("neatlogs.llm.token_count.prompt", response.totalTokens);
8091
+ span2.setStatus({ code: import_api15.SpanStatusCode.OK });
8092
+ span2.end();
8093
+ return response;
8094
+ },
8095
+ (err) => {
8096
+ recordError5(span2, err);
8097
+ throw err;
8098
+ }
8099
+ );
8100
+ };
8101
+ }
8102
+ function tracedGenerateContent2(original, isStream) {
8103
+ return function(opts, ...rest) {
8104
+ const tracer = getProviderTracer(TRACER_NAME7);
8105
+ const model = opts?.model ?? "";
8106
+ const span2 = tracer.startSpan("google_genai.models.generate_content", {
8107
+ attributes: {
8108
+ "neatlogs.span.kind": "LLM",
8109
+ "neatlogs.llm.provider": PROVIDER3,
8110
+ "neatlogs.llm.system": SYSTEM3,
8111
+ "neatlogs.llm.model_name": model,
8112
+ "neatlogs.llm.is_streaming": isStream
8113
+ }
8114
+ }, import_api15.context.active());
8115
+ setInputAttributes2(span2, opts);
8116
+ const ctx = import_api15.trace.setSpan(import_api15.context.active(), span2);
8117
+ const result = import_api15.context.with(ctx, () => original(opts, ...rest));
8118
+ return Promise.resolve(result).then(
8119
+ (response) => {
8120
+ if (isStream) {
8121
+ return wrapStream2(response, span2);
8122
+ }
8123
+ finalizeResponse2(span2, response);
8124
+ return response;
8125
+ },
8126
+ (err) => {
8127
+ recordError5(span2, err);
8128
+ throw err;
8129
+ }
8130
+ );
8131
+ };
8132
+ }
8133
+ function setInputAttributes2(span2, opts) {
8134
+ let idx = 0;
8135
+ const config = opts?.config;
8136
+ const systemInstruction = config?.systemInstruction ?? config?.system_instruction;
8137
+ if (systemInstruction) {
8138
+ span2.setAttribute(`neatlogs.llm.input_messages.${idx}.role`, "system");
8139
+ span2.setAttribute(
8140
+ `neatlogs.llm.input_messages.${idx}.content`,
8141
+ typeof systemInstruction === "string" ? systemInstruction : safeStringify7(systemInstruction)
8142
+ );
8143
+ idx++;
8144
+ }
8145
+ const contents = opts?.contents;
8146
+ if (typeof contents === "string") {
8147
+ span2.setAttribute(`neatlogs.llm.input_messages.${idx}.role`, "user");
8148
+ span2.setAttribute(`neatlogs.llm.input_messages.${idx}.content`, contents);
8149
+ } else if (Array.isArray(contents)) {
8150
+ for (const item of contents) {
8151
+ if (typeof item === "string") {
8152
+ span2.setAttribute(`neatlogs.llm.input_messages.${idx}.role`, "user");
8153
+ span2.setAttribute(`neatlogs.llm.input_messages.${idx}.content`, item);
8154
+ idx++;
8155
+ } else if (item && typeof item === "object") {
8156
+ const role = item.role ?? "user";
8157
+ span2.setAttribute(`neatlogs.llm.input_messages.${idx}.role`, role);
8158
+ const parts = item.parts ?? [];
8159
+ const textParts = [];
8160
+ for (const part of parts) {
8161
+ if (typeof part === "string") textParts.push(part);
8162
+ else if (part?.text) textParts.push(part.text);
8163
+ }
8164
+ span2.setAttribute(
8165
+ `neatlogs.llm.input_messages.${idx}.content`,
8166
+ textParts.length ? textParts.join("\n") : safeStringify7(parts)
8167
+ );
8168
+ idx++;
8169
+ }
8170
+ }
8171
+ } else if (contents) {
8172
+ span2.setAttribute(`neatlogs.llm.input_messages.${idx}.role`, "user");
8173
+ span2.setAttribute(`neatlogs.llm.input_messages.${idx}.content`, safeStringify7(contents));
8174
+ }
8175
+ const tools = config?.tools;
8176
+ if (Array.isArray(tools)) {
8177
+ let t = 0;
8178
+ for (const tool of tools) {
8179
+ const decls = tool?.functionDeclarations ?? tool?.function_declarations ?? [];
8180
+ for (const fn of decls) {
8181
+ span2.setAttribute(`neatlogs.llm.tools.${t}.name`, fn?.name ?? "");
8182
+ if (fn?.description) span2.setAttribute(`neatlogs.llm.tools.${t}.description`, fn.description);
8183
+ if (fn?.parameters) span2.setAttribute(`neatlogs.llm.tools.${t}.input_schema`, safeStringify7(fn.parameters));
8184
+ t++;
8185
+ }
8186
+ }
8187
+ }
8188
+ if (config) {
8189
+ if (config.temperature != null) span2.setAttribute("neatlogs.llm.temperature", config.temperature);
8190
+ if (config.topP != null) span2.setAttribute("neatlogs.llm.top_p", config.topP);
8191
+ if (config.topK != null) span2.setAttribute("neatlogs.llm.top_k", config.topK);
8192
+ const maxTokens = config.maxOutputTokens ?? config.max_output_tokens;
8193
+ if (maxTokens != null) span2.setAttribute("neatlogs.llm.max_tokens", maxTokens);
8194
+ const params = {};
8195
+ if (config.temperature != null) params.temperature = config.temperature;
8196
+ if (config.topP != null) params.top_p = config.topP;
8197
+ if (config.topK != null) params.top_k = config.topK;
8198
+ if (maxTokens != null) params.max_tokens = maxTokens;
8199
+ if (config.frequencyPenalty != null) params.frequency_penalty = config.frequencyPenalty;
8200
+ if (config.presencePenalty != null) params.presence_penalty = config.presencePenalty;
8201
+ if (Object.keys(params).length > 0) {
8202
+ span2.setAttribute("neatlogs.llm.invocation_parameters", JSON.stringify(params));
8203
+ }
8204
+ }
8205
+ }
8206
+ function wrapStream2(stream, span2) {
8207
+ const chunks = [];
8208
+ const originalAsyncIterator = stream?.[Symbol.asyncIterator]?.bind(stream);
8209
+ if (!originalAsyncIterator) {
8210
+ finalizeStreamChunks4(span2, chunks);
8211
+ return stream;
8212
+ }
8213
+ const wrapped = Object.create(Object.getPrototypeOf(stream));
8214
+ Object.assign(wrapped, stream);
8215
+ wrapped[Symbol.asyncIterator] = function() {
8216
+ const iterator = originalAsyncIterator();
8217
+ return {
8218
+ async next() {
8219
+ try {
8220
+ const result = await iterator.next();
8221
+ if (result.done) {
8222
+ finalizeStreamChunks4(span2, chunks);
8223
+ return result;
8224
+ }
8225
+ chunks.push(result.value);
8226
+ return result;
8227
+ } catch (err) {
8228
+ recordError5(span2, err);
8229
+ throw err;
8230
+ }
8231
+ },
8232
+ async return(value) {
8233
+ finalizeStreamChunks4(span2, chunks);
8234
+ return iterator.return?.(value) ?? { done: true, value: void 0 };
8235
+ },
8236
+ async throw(err) {
8237
+ recordError5(span2, err);
8238
+ return iterator.throw?.(err) ?? { done: true, value: void 0 };
8239
+ }
8240
+ };
8241
+ };
8242
+ return wrapped;
8243
+ }
8244
+ function finalizeStreamChunks4(span2, chunks) {
8245
+ const textParts = [];
8246
+ let finishReason = "";
8247
+ let usage = null;
8248
+ for (const chunk of chunks) {
8249
+ for (const candidate of chunk?.candidates ?? []) {
8250
+ for (const part of candidate?.content?.parts ?? []) {
8251
+ if (part?.text && !part?.thought) textParts.push(part.text);
8252
+ }
8253
+ if (candidate?.finishReason) finishReason = candidate.finishReason;
8254
+ }
8255
+ if (chunk?.usageMetadata) usage = chunk.usageMetadata;
8256
+ }
8257
+ const fullText = textParts.join("");
8258
+ if (fullText) {
8259
+ span2.setAttribute("neatlogs.llm.output_messages.0.role", "assistant");
8260
+ span2.setAttribute("neatlogs.llm.output_messages.0.content", fullText);
8261
+ }
8262
+ if (finishReason) span2.setAttribute("neatlogs.llm.finish_reason", String(finishReason));
8263
+ setUsage2(span2, usage);
8264
+ span2.setStatus({ code: import_api15.SpanStatusCode.OK });
8265
+ span2.end();
8266
+ }
8267
+ function finalizeResponse2(span2, response) {
8268
+ const textParts = [];
8269
+ let toolIdx = 0;
8270
+ for (const candidate of response?.candidates ?? []) {
8271
+ for (const part of candidate?.content?.parts ?? []) {
8272
+ if (part?.text && !part?.thought) {
8273
+ textParts.push(part.text);
8274
+ } else if (part?.thought && part?.text) {
8275
+ span2.setAttribute("neatlogs.llm.output_messages.0.thinking", part.text);
8276
+ } else if (part?.functionCall) {
8277
+ const fc = part.functionCall;
8278
+ span2.setAttribute(`neatlogs.llm.tool_calls.${toolIdx}.name`, fc?.name ?? "");
8279
+ span2.setAttribute(`neatlogs.llm.tool_calls.${toolIdx}.arguments`, safeStringify7(fc?.args ?? {}));
8280
+ toolIdx++;
8281
+ }
8282
+ }
8283
+ if (candidate?.finishReason) span2.setAttribute("neatlogs.llm.finish_reason", String(candidate.finishReason));
8284
+ }
8285
+ if (textParts.length) {
8286
+ span2.setAttribute("neatlogs.llm.output_messages.0.role", "assistant");
8287
+ span2.setAttribute("neatlogs.llm.output_messages.0.content", textParts.join(""));
8288
+ }
8289
+ setUsage2(span2, response?.usageMetadata);
8290
+ span2.setStatus({ code: import_api15.SpanStatusCode.OK });
8291
+ span2.end();
8292
+ }
8293
+ function setUsage2(span2, usage) {
8294
+ if (!usage) return;
8295
+ if (usage.promptTokenCount != null) span2.setAttribute("neatlogs.llm.token_count.prompt", usage.promptTokenCount);
8296
+ if (usage.candidatesTokenCount != null) span2.setAttribute("neatlogs.llm.token_count.completion", usage.candidatesTokenCount);
8297
+ if (usage.totalTokenCount != null) span2.setAttribute("neatlogs.llm.token_count.total", usage.totalTokenCount);
8298
+ if (usage.cachedContentTokenCount != null) span2.setAttribute("neatlogs.llm.token_count.cache_read", usage.cachedContentTokenCount);
8299
+ if (usage.thoughtsTokenCount != null) span2.setAttribute("neatlogs.llm.token_count.reasoning", usage.thoughtsTokenCount);
8300
+ }
8301
+ function safeStringify7(value) {
8302
+ try {
8303
+ return typeof value === "string" ? value : JSON.stringify(value);
8304
+ } catch {
8305
+ return "";
8306
+ }
8307
+ }
8308
+ function recordError5(span2, err) {
8309
+ if (err instanceof Error) {
8310
+ span2.setStatus({ code: import_api15.SpanStatusCode.ERROR, message: err.message });
8311
+ span2.recordException(err);
8312
+ } else {
8313
+ span2.setStatus({ code: import_api15.SpanStatusCode.ERROR, message: String(err) });
8314
+ }
8315
+ span2.end();
8316
+ }
8317
+
8318
+ // src/bedrock.ts
8319
+ var import_api16 = require("@opentelemetry/api");
8320
+ var TRACER_NAME8 = "neatlogs.bedrock";
8321
+ var PROVIDER4 = "bedrock";
8322
+ function traceTool6(name, fn) {
8323
+ return async function tracedTool(args) {
8324
+ const tracer = getProviderTracer(TRACER_NAME8);
8325
+ return tracer.startActiveSpan(
8326
+ `tool.${name}`,
8327
+ {
8328
+ attributes: {
8329
+ "neatlogs.span.kind": "TOOL",
8330
+ "neatlogs.tool.name": name,
8331
+ "input.value": safeStringify8(args)
8332
+ }
8333
+ },
8334
+ import_api16.context.active(),
8335
+ async (span2) => {
8336
+ try {
8337
+ const result = await fn(args);
8338
+ span2.setAttribute("output.value", safeStringify8(result));
8339
+ span2.setStatus({ code: import_api16.SpanStatusCode.OK });
8340
+ return result;
8341
+ } catch (err) {
8342
+ recordError6(span2, err);
8343
+ throw err;
8344
+ } finally {
8345
+ span2.end();
8346
+ }
8347
+ }
8348
+ );
8349
+ };
8350
+ }
7917
8351
  function wrapBedrock(client) {
7918
8352
  const c = client;
7919
8353
  if (c._neatlogsBedrockPatched) return client;
@@ -7948,15 +8382,15 @@ function vendorFromModel(modelId) {
7948
8382
  return vendor || "bedrock";
7949
8383
  }
7950
8384
  function startSpan(name, modelId, isStream) {
7951
- return getProviderTracer(TRACER_NAME7).startSpan(name, {
8385
+ return getProviderTracer(TRACER_NAME8).startSpan(name, {
7952
8386
  attributes: {
7953
8387
  "neatlogs.span.kind": "LLM",
7954
- "neatlogs.llm.provider": PROVIDER3,
8388
+ "neatlogs.llm.provider": PROVIDER4,
7955
8389
  "neatlogs.llm.system": vendorFromModel(modelId),
7956
8390
  "neatlogs.llm.model_name": String(modelId ?? ""),
7957
8391
  "neatlogs.llm.is_streaming": isStream
7958
8392
  }
7959
- }, import_api15.context.active());
8393
+ }, import_api16.context.active());
7960
8394
  }
7961
8395
  function tracedConverse(originalSend, command, input, rest, isStream) {
7962
8396
  const span2 = startSpan(
@@ -7965,8 +8399,8 @@ function tracedConverse(originalSend, command, input, rest, isStream) {
7965
8399
  isStream
7966
8400
  );
7967
8401
  setConverseInput(span2, input);
7968
- const ctx = import_api15.trace.setSpan(import_api15.context.active(), span2);
7969
- const promise = import_api15.context.with(ctx, () => originalSend(command, ...rest));
8402
+ const ctx = import_api16.trace.setSpan(import_api16.context.active(), span2);
8403
+ const promise = import_api16.context.with(ctx, () => originalSend(command, ...rest));
7970
8404
  return promise.then(
7971
8405
  (response) => {
7972
8406
  if (isStream) {
@@ -7976,7 +8410,7 @@ function tracedConverse(originalSend, command, input, rest, isStream) {
7976
8410
  return response;
7977
8411
  },
7978
8412
  (err) => {
7979
- recordError5(span2, err);
8413
+ recordError6(span2, err);
7980
8414
  throw err;
7981
8415
  }
7982
8416
  );
@@ -8005,19 +8439,19 @@ function setConverseInput(span2, input) {
8005
8439
  const spec = tools[i]?.toolSpec ?? {};
8006
8440
  if (spec.name) span2.setAttribute(`neatlogs.llm.tools.${i}.name`, spec.name);
8007
8441
  if (spec.description) span2.setAttribute(`neatlogs.llm.tools.${i}.description`, spec.description);
8008
- if (spec.inputSchema) span2.setAttribute(`neatlogs.llm.tools.${i}.input_schema`, safeStringify7(spec.inputSchema));
8442
+ if (spec.inputSchema) span2.setAttribute(`neatlogs.llm.tools.${i}.input_schema`, safeStringify8(spec.inputSchema));
8009
8443
  }
8010
8444
  }
8011
8445
  function converseBlocksToText(content) {
8012
8446
  if (typeof content === "string") return content;
8013
- if (!Array.isArray(content)) return safeStringify7(content);
8447
+ if (!Array.isArray(content)) return safeStringify8(content);
8014
8448
  const parts = [];
8015
8449
  for (const block of content) {
8016
8450
  if (block && typeof block === "object") {
8017
8451
  if ("text" in block) parts.push(String(block.text));
8018
- else if ("toolResult" in block) parts.push(safeStringify7(block.toolResult));
8019
- else if ("toolUse" in block) parts.push(safeStringify7(block.toolUse));
8020
- else parts.push(safeStringify7(block));
8452
+ else if ("toolResult" in block) parts.push(safeStringify8(block.toolResult));
8453
+ else if ("toolUse" in block) parts.push(safeStringify8(block.toolUse));
8454
+ else parts.push(safeStringify8(block));
8021
8455
  } else {
8022
8456
  parts.push(String(block));
8023
8457
  }
@@ -8036,7 +8470,7 @@ function finalizeConverse(span2, response) {
8036
8470
  const tu = block.toolUse;
8037
8471
  span2.setAttribute(`neatlogs.llm.tool_calls.${toolIdx}.id`, String(tu?.toolUseId ?? ""));
8038
8472
  span2.setAttribute(`neatlogs.llm.tool_calls.${toolIdx}.name`, String(tu?.name ?? ""));
8039
- span2.setAttribute(`neatlogs.llm.tool_calls.${toolIdx}.arguments`, safeStringify7(tu?.input ?? {}));
8473
+ span2.setAttribute(`neatlogs.llm.tool_calls.${toolIdx}.arguments`, safeStringify8(tu?.input ?? {}));
8040
8474
  toolIdx++;
8041
8475
  }
8042
8476
  }
@@ -8046,7 +8480,7 @@ function finalizeConverse(span2, response) {
8046
8480
  }
8047
8481
  if (response?.stopReason) span2.setAttribute("neatlogs.llm.finish_reason", String(response.stopReason));
8048
8482
  setConverseUsage(span2, response?.usage);
8049
- span2.setStatus({ code: import_api15.SpanStatusCode.OK });
8483
+ span2.setStatus({ code: import_api16.SpanStatusCode.OK });
8050
8484
  span2.end();
8051
8485
  }
8052
8486
  function setConverseUsage(span2, usage) {
@@ -8060,7 +8494,7 @@ function setConverseUsage(span2, usage) {
8060
8494
  function wrapConverseStream(response, span2) {
8061
8495
  const stream = response?.stream;
8062
8496
  if (!stream || typeof stream[Symbol.asyncIterator] !== "function") {
8063
- span2.setStatus({ code: import_api15.SpanStatusCode.OK });
8497
+ span2.setStatus({ code: import_api16.SpanStatusCode.OK });
8064
8498
  span2.end();
8065
8499
  return response;
8066
8500
  }
@@ -8099,7 +8533,7 @@ function wrapConverseStream(response, span2) {
8099
8533
  if (ev?.metadata?.usage) usage = ev.metadata.usage;
8100
8534
  return result;
8101
8535
  } catch (err) {
8102
- recordError5(span2, err);
8536
+ recordError6(span2, err);
8103
8537
  throw err;
8104
8538
  }
8105
8539
  },
@@ -8128,7 +8562,7 @@ function finalizeConverseStream(span2, textParts, toolCalls, finishReason, usage
8128
8562
  }
8129
8563
  if (finishReason) span2.setAttribute("neatlogs.llm.finish_reason", String(finishReason));
8130
8564
  setConverseUsage(span2, usage);
8131
- span2.setStatus({ code: import_api15.SpanStatusCode.OK });
8565
+ span2.setStatus({ code: import_api16.SpanStatusCode.OK });
8132
8566
  span2.end();
8133
8567
  }
8134
8568
  function isEmbeddingModel(modelId) {
@@ -8140,16 +8574,16 @@ function tracedInvokeModel(originalSend, command, input, rest, isStream) {
8140
8574
  const bodyIn = decodeBody(input?.body);
8141
8575
  let span2;
8142
8576
  if (isEmbedding) {
8143
- span2 = getProviderTracer(TRACER_NAME7).startSpan("bedrock.invoke_model", {
8577
+ span2 = getProviderTracer(TRACER_NAME8).startSpan("bedrock.invoke_model", {
8144
8578
  attributes: {
8145
8579
  "neatlogs.span.kind": "EMBEDDING",
8146
- "neatlogs.llm.provider": PROVIDER3,
8580
+ "neatlogs.llm.provider": PROVIDER4,
8147
8581
  "neatlogs.embedding.model_name": String(input?.modelId ?? "")
8148
8582
  }
8149
- }, import_api15.context.active());
8583
+ }, import_api16.context.active());
8150
8584
  const text = bodyIn?.inputText ?? bodyIn?.texts ?? bodyIn?.input_text;
8151
8585
  if (text) {
8152
- span2.setAttribute("neatlogs.embedding.text", typeof text === "string" ? text : safeStringify7(text));
8586
+ span2.setAttribute("neatlogs.embedding.text", typeof text === "string" ? text : safeStringify8(text));
8153
8587
  }
8154
8588
  } else {
8155
8589
  span2 = startSpan(
@@ -8159,8 +8593,8 @@ function tracedInvokeModel(originalSend, command, input, rest, isStream) {
8159
8593
  );
8160
8594
  setInvokeInput(span2, vendor, bodyIn);
8161
8595
  }
8162
- const ctx = import_api15.trace.setSpan(import_api15.context.active(), span2);
8163
- const promise = import_api15.context.with(ctx, () => originalSend(command, ...rest));
8596
+ const ctx = import_api16.trace.setSpan(import_api16.context.active(), span2);
8597
+ const promise = import_api16.context.with(ctx, () => originalSend(command, ...rest));
8164
8598
  return promise.then(
8165
8599
  (response) => {
8166
8600
  if (isStream) {
@@ -8173,13 +8607,13 @@ function tracedInvokeModel(originalSend, command, input, rest, isStream) {
8173
8607
  finalizeInvoke(span2, vendor, decodeBody(response?.body));
8174
8608
  }
8175
8609
  } catch {
8176
- span2.setStatus({ code: import_api15.SpanStatusCode.OK });
8610
+ span2.setStatus({ code: import_api16.SpanStatusCode.OK });
8177
8611
  span2.end();
8178
8612
  }
8179
8613
  return response;
8180
8614
  },
8181
8615
  (err) => {
8182
- recordError5(span2, err);
8616
+ recordError6(span2, err);
8183
8617
  throw err;
8184
8618
  }
8185
8619
  );
@@ -8198,7 +8632,7 @@ function finalizeInvokeEmbedding(span2, body) {
8198
8632
  span2.setAttribute("neatlogs.llm.token_count.prompt", body.inputTextTokenCount);
8199
8633
  span2.setAttribute("neatlogs.embedding.token_count", body.inputTextTokenCount);
8200
8634
  }
8201
- span2.setStatus({ code: import_api15.SpanStatusCode.OK });
8635
+ span2.setStatus({ code: import_api16.SpanStatusCode.OK });
8202
8636
  span2.end();
8203
8637
  }
8204
8638
  function decodeBody(body) {
@@ -8220,7 +8654,7 @@ function setInvokeInput(span2, vendor, body) {
8220
8654
  span2.setAttribute(`neatlogs.llm.input_messages.${idx}.role`, "system");
8221
8655
  span2.setAttribute(
8222
8656
  `neatlogs.llm.input_messages.${idx}.content`,
8223
- typeof body.system === "string" ? body.system : safeStringify7(body.system)
8657
+ typeof body.system === "string" ? body.system : safeStringify8(body.system)
8224
8658
  );
8225
8659
  idx++;
8226
8660
  }
@@ -8229,7 +8663,7 @@ function setInvokeInput(span2, vendor, body) {
8229
8663
  span2.setAttribute(`neatlogs.llm.input_messages.${idx}.role`, msg?.role ?? "user");
8230
8664
  span2.setAttribute(
8231
8665
  `neatlogs.llm.input_messages.${idx}.content`,
8232
- typeof msg?.content === "string" ? msg.content : safeStringify7(msg?.content)
8666
+ typeof msg?.content === "string" ? msg.content : safeStringify8(msg?.content)
8233
8667
  );
8234
8668
  idx++;
8235
8669
  }
@@ -8264,7 +8698,7 @@ function finalizeInvoke(span2, vendor, body) {
8264
8698
  if (b?.type === "tool_use") {
8265
8699
  span2.setAttribute(`neatlogs.llm.tool_calls.${toolIdx}.id`, String(b.id ?? ""));
8266
8700
  span2.setAttribute(`neatlogs.llm.tool_calls.${toolIdx}.name`, String(b.name ?? ""));
8267
- span2.setAttribute(`neatlogs.llm.tool_calls.${toolIdx}.arguments`, safeStringify7(b.input ?? {}));
8701
+ span2.setAttribute(`neatlogs.llm.tool_calls.${toolIdx}.arguments`, safeStringify8(b.input ?? {}));
8268
8702
  toolIdx++;
8269
8703
  }
8270
8704
  }
@@ -8306,13 +8740,13 @@ function finalizeInvoke(span2, vendor, body) {
8306
8740
  span2.setAttribute("neatlogs.llm.token_count.total", promptTokens + completionTokens);
8307
8741
  }
8308
8742
  if (finishReason) span2.setAttribute("neatlogs.llm.finish_reason", String(finishReason));
8309
- span2.setStatus({ code: import_api15.SpanStatusCode.OK });
8743
+ span2.setStatus({ code: import_api16.SpanStatusCode.OK });
8310
8744
  span2.end();
8311
8745
  }
8312
8746
  function wrapInvokeStream(response, span2, vendor) {
8313
8747
  const body = response?.body;
8314
8748
  if (!body || typeof body[Symbol.asyncIterator] !== "function") {
8315
- span2.setStatus({ code: import_api15.SpanStatusCode.OK });
8749
+ span2.setStatus({ code: import_api16.SpanStatusCode.OK });
8316
8750
  span2.end();
8317
8751
  return response;
8318
8752
  }
@@ -8330,7 +8764,7 @@ function wrapInvokeStream(response, span2, vendor) {
8330
8764
  if (promptTokens != null) span2.setAttribute("neatlogs.llm.token_count.prompt", promptTokens);
8331
8765
  if (completionTokens != null) span2.setAttribute("neatlogs.llm.token_count.completion", completionTokens);
8332
8766
  if (finishReason) span2.setAttribute("neatlogs.llm.finish_reason", String(finishReason));
8333
- span2.setStatus({ code: import_api15.SpanStatusCode.OK });
8767
+ span2.setStatus({ code: import_api16.SpanStatusCode.OK });
8334
8768
  span2.end();
8335
8769
  };
8336
8770
  const wrappedBody = {
@@ -8360,7 +8794,7 @@ function wrapInvokeStream(response, span2, vendor) {
8360
8794
  }
8361
8795
  return result;
8362
8796
  } catch (err) {
8363
- recordError5(span2, err);
8797
+ recordError6(span2, err);
8364
8798
  throw err;
8365
8799
  }
8366
8800
  },
@@ -8374,26 +8808,26 @@ function wrapInvokeStream(response, span2, vendor) {
8374
8808
  response.body = wrappedBody;
8375
8809
  return response;
8376
8810
  }
8377
- function safeStringify7(value) {
8811
+ function safeStringify8(value) {
8378
8812
  try {
8379
8813
  return typeof value === "string" ? value : JSON.stringify(value);
8380
8814
  } catch {
8381
8815
  return "";
8382
8816
  }
8383
8817
  }
8384
- function recordError5(span2, err) {
8818
+ function recordError6(span2, err) {
8385
8819
  if (err instanceof Error) {
8386
- span2.setStatus({ code: import_api15.SpanStatusCode.ERROR, message: err.message });
8820
+ span2.setStatus({ code: import_api16.SpanStatusCode.ERROR, message: err.message });
8387
8821
  span2.recordException(err);
8388
8822
  } else {
8389
- span2.setStatus({ code: import_api15.SpanStatusCode.ERROR, message: String(err) });
8823
+ span2.setStatus({ code: import_api16.SpanStatusCode.ERROR, message: String(err) });
8390
8824
  }
8391
8825
  span2.end();
8392
8826
  }
8393
8827
 
8394
8828
  // src/langchain.ts
8395
- var import_api16 = require("@opentelemetry/api");
8396
- var TRACER_NAME8 = "neatlogs.langchain";
8829
+ var import_api17 = require("@opentelemetry/api");
8830
+ var TRACER_NAME9 = "neatlogs.langchain";
8397
8831
  function langchainHandler(opts) {
8398
8832
  return new NeatlogsCallbackHandler(opts);
8399
8833
  }
@@ -8417,10 +8851,10 @@ var NeatlogsCallbackHandler = class {
8417
8851
  _startCtx(parentRunId, runId, kind) {
8418
8852
  const parentSpan = parentRunId ? this._spans.get(parentRunId) : void 0;
8419
8853
  if (parentSpan) {
8420
- return import_api16.trace.setSpan(import_api16.context.active(), parentSpan);
8854
+ return import_api17.trace.setSpan(import_api17.context.active(), parentSpan);
8421
8855
  }
8422
- const tracer = import_api16.trace.getTracer(TRACER_NAME8);
8423
- const { root, ctx } = maybeOpenAutoRoot(tracer, kind, import_api16.context.active());
8856
+ const tracer = import_api17.trace.getTracer(TRACER_NAME9);
8857
+ const { root, ctx } = maybeOpenAutoRoot(tracer, kind, import_api17.context.active());
8424
8858
  if (root) this._autoRoots.set(runId, root);
8425
8859
  return ctx;
8426
8860
  }
@@ -8433,7 +8867,7 @@ var NeatlogsCallbackHandler = class {
8433
8867
  }
8434
8868
  // --- Chain/Graph callbacks ---
8435
8869
  async handleChainStart(serialized, inputs, runId, parentRunId, tags, metadata) {
8436
- const tracer = import_api16.trace.getTracer(TRACER_NAME8);
8870
+ const tracer = import_api17.trace.getTracer(TRACER_NAME9);
8437
8871
  const name = serialized?.name ?? serialized?.id?.at(-1) ?? "chain";
8438
8872
  const parentCtx = this._startCtx(parentRunId, runId, "chain");
8439
8873
  const attrs = {
@@ -8441,7 +8875,7 @@ var NeatlogsCallbackHandler = class {
8441
8875
  "neatlogs.chain.name": name
8442
8876
  };
8443
8877
  if (this._workflowName) attrs["neatlogs.workflow.name"] = this._workflowName;
8444
- if (inputs) attrs["input.value"] = safeStringify8(inputs);
8878
+ if (inputs) attrs["input.value"] = safeStringify9(inputs);
8445
8879
  if (tags?.length) attrs["neatlogs.tags"] = tags.join(",");
8446
8880
  const span2 = tracer.startSpan(`langchain.chain.${name}`, { attributes: attrs }, parentCtx);
8447
8881
  this._spans.set(runId, span2);
@@ -8449,8 +8883,8 @@ var NeatlogsCallbackHandler = class {
8449
8883
  async handleChainEnd(outputs, runId) {
8450
8884
  const span2 = this._spans.get(runId);
8451
8885
  if (!span2) return;
8452
- if (outputs) span2.setAttribute("output.value", safeStringify8(outputs));
8453
- span2.setStatus({ code: import_api16.SpanStatusCode.OK });
8886
+ if (outputs) span2.setAttribute("output.value", safeStringify9(outputs));
8887
+ span2.setStatus({ code: import_api17.SpanStatusCode.OK });
8454
8888
  span2.end();
8455
8889
  this._spans.delete(runId);
8456
8890
  this._endAutoRoot(runId);
@@ -8458,7 +8892,7 @@ var NeatlogsCallbackHandler = class {
8458
8892
  async handleChainError(error, runId) {
8459
8893
  const span2 = this._spans.get(runId);
8460
8894
  if (!span2) return;
8461
- span2.setStatus({ code: import_api16.SpanStatusCode.ERROR, message: error.message });
8895
+ span2.setStatus({ code: import_api17.SpanStatusCode.ERROR, message: error.message });
8462
8896
  span2.recordException(error);
8463
8897
  span2.end();
8464
8898
  this._spans.delete(runId);
@@ -8466,7 +8900,7 @@ var NeatlogsCallbackHandler = class {
8466
8900
  }
8467
8901
  // --- LLM callbacks ---
8468
8902
  async handleLLMStart(serialized, prompts, runId, parentRunId, extraParams) {
8469
- const tracer = import_api16.trace.getTracer(TRACER_NAME8);
8903
+ const tracer = import_api17.trace.getTracer(TRACER_NAME9);
8470
8904
  const model = serialized?.kwargs?.model_name ?? serialized?.kwargs?.model ?? serialized?.name ?? "";
8471
8905
  const parentCtx = this._startCtx(parentRunId, runId, "llm");
8472
8906
  const attrs = {
@@ -8488,7 +8922,7 @@ var NeatlogsCallbackHandler = class {
8488
8922
  this._spans.set(runId, span2);
8489
8923
  }
8490
8924
  async handleChatModelStart(serialized, messages, runId, parentRunId, extraParams) {
8491
- const tracer = import_api16.trace.getTracer(TRACER_NAME8);
8925
+ const tracer = import_api17.trace.getTracer(TRACER_NAME9);
8492
8926
  const model = serialized?.kwargs?.model_name ?? serialized?.kwargs?.model ?? serialized?.name ?? "";
8493
8927
  const parentCtx = this._startCtx(parentRunId, runId, "llm");
8494
8928
  const attrs = {
@@ -8500,7 +8934,7 @@ var NeatlogsCallbackHandler = class {
8500
8934
  for (let i = 0; i < flatMessages.length; i++) {
8501
8935
  const msg = flatMessages[i];
8502
8936
  const role = msg?.role ?? msg?._getType?.() ?? msg?.constructor?.name ?? "unknown";
8503
- const content = typeof msg?.content === "string" ? msg.content : safeStringify8(msg?.content);
8937
+ const content = typeof msg?.content === "string" ? msg.content : safeStringify9(msg?.content);
8504
8938
  attrs[`neatlogs.llm.input_messages.${i}.role`] = mapRole(role);
8505
8939
  attrs[`neatlogs.llm.input_messages.${i}.content`] = content;
8506
8940
  }
@@ -8531,7 +8965,7 @@ var NeatlogsCallbackHandler = class {
8531
8965
  const tc = toolCalls[k];
8532
8966
  span2.setAttribute(`neatlogs.llm.tool_calls.${k}.id`, tc.id ?? "");
8533
8967
  span2.setAttribute(`neatlogs.llm.tool_calls.${k}.name`, tc.name ?? tc.function?.name ?? "");
8534
- span2.setAttribute(`neatlogs.llm.tool_calls.${k}.arguments`, tc.args ? safeStringify8(tc.args) : tc.function?.arguments ?? "");
8968
+ span2.setAttribute(`neatlogs.llm.tool_calls.${k}.arguments`, tc.args ? safeStringify9(tc.args) : tc.function?.arguments ?? "");
8535
8969
  }
8536
8970
  }
8537
8971
  }
@@ -8548,7 +8982,7 @@ var NeatlogsCallbackHandler = class {
8548
8982
  span2.setAttribute("neatlogs.llm.token_count.total", usage.totalTokens ?? usage.total_tokens);
8549
8983
  }
8550
8984
  }
8551
- span2.setStatus({ code: import_api16.SpanStatusCode.OK });
8985
+ span2.setStatus({ code: import_api17.SpanStatusCode.OK });
8552
8986
  span2.end();
8553
8987
  this._spans.delete(runId);
8554
8988
  this._endAutoRoot(runId);
@@ -8558,7 +8992,7 @@ var NeatlogsCallbackHandler = class {
8558
8992
  async handleLLMError(error, runId) {
8559
8993
  const span2 = this._spans.get(runId);
8560
8994
  if (!span2) return;
8561
- span2.setStatus({ code: import_api16.SpanStatusCode.ERROR, message: error.message });
8995
+ span2.setStatus({ code: import_api17.SpanStatusCode.ERROR, message: error.message });
8562
8996
  span2.recordException(error);
8563
8997
  span2.end();
8564
8998
  this._spans.delete(runId);
@@ -8566,7 +9000,7 @@ var NeatlogsCallbackHandler = class {
8566
9000
  }
8567
9001
  // --- Tool callbacks ---
8568
9002
  async handleToolStart(serialized, input, runId, parentRunId, _tags, _metadata, runName, toolCallId) {
8569
- const tracer = import_api16.trace.getTracer(TRACER_NAME8);
9003
+ const tracer = import_api17.trace.getTracer(TRACER_NAME9);
8570
9004
  const name = serialized?.name || runName || (Array.isArray(serialized?.id) ? serialized.id[serialized.id.length - 1] : void 0) || "tool";
8571
9005
  const parentCtx = this._startCtx(parentRunId, runId, "tool");
8572
9006
  const attrs = {
@@ -8582,7 +9016,7 @@ var NeatlogsCallbackHandler = class {
8582
9016
  const span2 = this._spans.get(runId);
8583
9017
  if (!span2) return;
8584
9018
  span2.setAttribute("output.value", String(output));
8585
- span2.setStatus({ code: import_api16.SpanStatusCode.OK });
9019
+ span2.setStatus({ code: import_api17.SpanStatusCode.OK });
8586
9020
  span2.end();
8587
9021
  this._spans.delete(runId);
8588
9022
  this._endAutoRoot(runId);
@@ -8590,7 +9024,7 @@ var NeatlogsCallbackHandler = class {
8590
9024
  async handleToolError(error, runId) {
8591
9025
  const span2 = this._spans.get(runId);
8592
9026
  if (!span2) return;
8593
- span2.setStatus({ code: import_api16.SpanStatusCode.ERROR, message: error.message });
9027
+ span2.setStatus({ code: import_api17.SpanStatusCode.ERROR, message: error.message });
8594
9028
  span2.recordException(error);
8595
9029
  span2.end();
8596
9030
  this._spans.delete(runId);
@@ -8598,7 +9032,7 @@ var NeatlogsCallbackHandler = class {
8598
9032
  }
8599
9033
  // --- Retriever callbacks ---
8600
9034
  async handleRetrieverStart(serialized, query, runId, parentRunId) {
8601
- const tracer = import_api16.trace.getTracer(TRACER_NAME8);
9035
+ const tracer = import_api17.trace.getTracer(TRACER_NAME9);
8602
9036
  const name = serialized?.name ?? "retriever";
8603
9037
  const parentCtx = this._startCtx(parentRunId, runId, "retriever");
8604
9038
  const attrs = {
@@ -8621,7 +9055,7 @@ var NeatlogsCallbackHandler = class {
8621
9055
  }
8622
9056
  }
8623
9057
  }
8624
- span2.setStatus({ code: import_api16.SpanStatusCode.OK });
9058
+ span2.setStatus({ code: import_api17.SpanStatusCode.OK });
8625
9059
  span2.end();
8626
9060
  this._spans.delete(runId);
8627
9061
  this._endAutoRoot(runId);
@@ -8629,7 +9063,7 @@ var NeatlogsCallbackHandler = class {
8629
9063
  async handleRetrieverError(error, runId) {
8630
9064
  const span2 = this._spans.get(runId);
8631
9065
  if (!span2) return;
8632
- span2.setStatus({ code: import_api16.SpanStatusCode.ERROR, message: error.message });
9066
+ span2.setStatus({ code: import_api17.SpanStatusCode.ERROR, message: error.message });
8633
9067
  span2.recordException(error);
8634
9068
  span2.end();
8635
9069
  this._spans.delete(runId);
@@ -8654,7 +9088,7 @@ function mapRole(role) {
8654
9088
  if (r === "tool" || r === "toolmessage") return "tool";
8655
9089
  return role;
8656
9090
  }
8657
- function safeStringify8(value) {
9091
+ function safeStringify9(value) {
8658
9092
  try {
8659
9093
  return typeof value === "string" ? value : JSON.stringify(value);
8660
9094
  } catch {
@@ -8730,7 +9164,7 @@ function setOutput(span2, isTool, out) {
8730
9164
  } else {
8731
9165
  span2.setAttribute("neatlogs.llm.output_messages.0.role", "assistant");
8732
9166
  span2.setAttribute("neatlogs.llm.output_messages.0.content", out);
8733
- span2.setAttribute("neatlogs.llm.output", safeStringify9({ role: "assistant", content: out }));
9167
+ span2.setAttribute("neatlogs.llm.output", safeStringify10({ role: "assistant", content: out }));
8734
9168
  }
8735
9169
  }
8736
9170
  function appendInputMessage(span2, role, content) {
@@ -8741,7 +9175,7 @@ function appendInputMessage(span2, role, content) {
8741
9175
  const existing = span2.__neatlogs_in_msgs ?? [];
8742
9176
  existing.push({ role, content });
8743
9177
  span2.__neatlogs_in_msgs = existing;
8744
- const blob = safeStringify9({ messages: existing });
9178
+ const blob = safeStringify10({ messages: existing });
8745
9179
  span2.setAttribute("input.value", blob);
8746
9180
  span2.setAttribute("neatlogs.llm.input", blob);
8747
9181
  }
@@ -8784,15 +9218,15 @@ function flattenBlocks(val) {
8784
9218
  const o = item;
8785
9219
  if (typeof o.text === "string") out.push(o.text);
8786
9220
  else if (typeof o.content === "string") out.push(o.content);
8787
- else if (o.toolUse) out.push(`${o.toolUse.name ?? "tool"}(${safeStringify9(o.toolUse.input ?? {})})`);
9221
+ else if (o.toolUse) out.push(`${o.toolUse.name ?? "tool"}(${safeStringify10(o.toolUse.input ?? {})})`);
8788
9222
  else if (o.toolResult) out.push(flattenBlocks(o.toolResult.content ?? o.toolResult));
8789
9223
  else if (Array.isArray(o.parts)) out.push(flattenBlocks(o.parts));
8790
9224
  else if (Array.isArray(o.content)) out.push(flattenBlocks(o.content));
8791
- else out.push(safeStringify9(o));
9225
+ else out.push(safeStringify10(o));
8792
9226
  }
8793
9227
  return out.filter(Boolean).join("\n");
8794
9228
  }
8795
- function safeStringify9(value) {
9229
+ function safeStringify10(value) {
8796
9230
  if (typeof value === "string") return value;
8797
9231
  try {
8798
9232
  return JSON.stringify(value) ?? "";
@@ -8802,8 +9236,8 @@ function safeStringify9(value) {
8802
9236
  }
8803
9237
 
8804
9238
  // src/openai-agents.ts
8805
- var import_api17 = require("@opentelemetry/api");
8806
- var TRACER_NAME9 = "neatlogs.openai_agents";
9239
+ var import_api18 = require("@opentelemetry/api");
9240
+ var TRACER_NAME10 = "neatlogs.openai_agents";
8807
9241
  function openaiAgentsProcessor() {
8808
9242
  return new NeatlogsTraceProcessor();
8809
9243
  }
@@ -8812,13 +9246,13 @@ var NeatlogsTraceProcessor = class {
8812
9246
  _startTimes = /* @__PURE__ */ new Map();
8813
9247
  // The @openai/agents SDK passes a Trace object: { traceId, name, groupId, metadata }.
8814
9248
  onTraceStart(traceData) {
8815
- const tracer = import_api17.trace.getTracer(TRACER_NAME9);
9249
+ const tracer = import_api18.trace.getTracer(TRACER_NAME10);
8816
9250
  const attrs = { "neatlogs.span.kind": "WORKFLOW" };
8817
9251
  const workflowName = traceData?.name ?? traceData?.workflow_name;
8818
9252
  if (workflowName) attrs["neatlogs.workflow.name"] = workflowName;
8819
9253
  const traceId = traceData?.traceId ?? traceData?.trace_id;
8820
9254
  if (traceId) attrs["neatlogs.agent.trace_id"] = String(traceId);
8821
- const span2 = tracer.startSpan("openai_agents.trace", { attributes: attrs }, import_api17.context.active());
9255
+ const span2 = tracer.startSpan("openai_agents.trace", { attributes: attrs }, import_api18.context.active());
8822
9256
  const key = String(traceId ?? `trace_${Date.now()}`);
8823
9257
  this._spans.set(key, span2);
8824
9258
  this._startTimes.set(key, Date.now());
@@ -8831,7 +9265,7 @@ var NeatlogsTraceProcessor = class {
8831
9265
  if (startTime) {
8832
9266
  span2.setAttribute("neatlogs.metrics.duration_ms", Date.now() - startTime);
8833
9267
  }
8834
- span2.setStatus({ code: import_api17.SpanStatusCode.OK });
9268
+ span2.setStatus({ code: import_api18.SpanStatusCode.OK });
8835
9269
  span2.end();
8836
9270
  this._spans.delete(key);
8837
9271
  this._startTimes.delete(key);
@@ -8839,13 +9273,13 @@ var NeatlogsTraceProcessor = class {
8839
9273
  // The SDK passes a Span object: { type, spanId, traceId, parentId?, spanData: {...} }.
8840
9274
  // The meaningful payload (type, name, input, output, usage, ...) lives in `spanData`.
8841
9275
  onSpanStart(span2) {
8842
- const tracer = import_api17.trace.getTracer(TRACER_NAME9);
9276
+ const tracer = import_api18.trace.getTracer(TRACER_NAME10);
8843
9277
  const data = span2?.spanData ?? span2 ?? {};
8844
9278
  const spanType = data?.type ?? data?.span_type ?? span2?.type ?? "";
8845
9279
  const spanId = String(span2?.spanId ?? span2?.span_id ?? data?.span_id ?? `span_${Date.now()}`);
8846
9280
  const parentKey = String(span2?.parentId ?? span2?.traceId ?? span2?.trace_id ?? "");
8847
9281
  const parentSpan = this._spans.get(parentKey);
8848
- const parentCtx = parentSpan ? import_api17.trace.setSpan(import_api17.context.active(), parentSpan) : import_api17.context.active();
9282
+ const parentCtx = parentSpan ? import_api18.trace.setSpan(import_api18.context.active(), parentSpan) : import_api18.context.active();
8849
9283
  let otelSpan;
8850
9284
  if (spanType === "agent" || spanType === "agent_run") {
8851
9285
  const agentName = data?.name ?? data?.agent_name ?? "agent";
@@ -8871,7 +9305,7 @@ var NeatlogsTraceProcessor = class {
8871
9305
  const role = typeof msg === "object" ? msg.role ?? "" : "";
8872
9306
  const content = typeof msg === "object" ? msg.content ?? "" : String(msg);
8873
9307
  if (role) attrs[`neatlogs.llm.input_messages.${i}.role`] = role;
8874
- if (content) attrs[`neatlogs.llm.input_messages.${i}.content`] = typeof content === "string" ? content : safeStringify10(content);
9308
+ if (content) attrs[`neatlogs.llm.input_messages.${i}.content`] = typeof content === "string" ? content : safeStringify11(content);
8875
9309
  }
8876
9310
  }
8877
9311
  otelSpan = tracer.startSpan("openai_agents.generation", { attributes: attrs }, parentCtx);
@@ -8883,7 +9317,7 @@ var NeatlogsTraceProcessor = class {
8883
9317
  };
8884
9318
  const toolInput = data?.input ?? data?.arguments;
8885
9319
  if (toolInput !== void 0) {
8886
- attrs["input.value"] = typeof toolInput === "string" ? toolInput : safeStringify10(toolInput);
9320
+ attrs["input.value"] = typeof toolInput === "string" ? toolInput : safeStringify11(toolInput);
8887
9321
  }
8888
9322
  otelSpan = tracer.startSpan(`openai_agents.tool.${toolName}`, { attributes: attrs }, parentCtx);
8889
9323
  } else if (spanType === "handoff") {
@@ -8920,13 +9354,13 @@ var NeatlogsTraceProcessor = class {
8920
9354
  for (let i = 0; i < toolCalls.length; i++) {
8921
9355
  const tc = toolCalls[i];
8922
9356
  otelSpan.setAttribute(`neatlogs.llm.tool_calls.${i}.name`, tc.name ?? "");
8923
- otelSpan.setAttribute(`neatlogs.llm.tool_calls.${i}.arguments`, typeof tc.arguments === "string" ? tc.arguments : safeStringify10(tc.arguments ?? {}));
9357
+ otelSpan.setAttribute(`neatlogs.llm.tool_calls.${i}.arguments`, typeof tc.arguments === "string" ? tc.arguments : safeStringify11(tc.arguments ?? {}));
8924
9358
  if (tc.callId ?? tc.id) otelSpan.setAttribute(`neatlogs.llm.tool_calls.${i}.id`, tc.callId ?? tc.id);
8925
9359
  }
8926
9360
  } else if (outputItems?.content) {
8927
9361
  otelSpan.setAttribute("neatlogs.llm.output_messages.0.role", "assistant");
8928
9362
  const c = outputItems.content;
8929
- otelSpan.setAttribute("neatlogs.llm.output_messages.0.content", typeof c === "string" ? c : safeStringify10(c));
9363
+ otelSpan.setAttribute("neatlogs.llm.output_messages.0.content", typeof c === "string" ? c : safeStringify11(c));
8930
9364
  }
8931
9365
  const usage = data?.usage ?? resp?.usage;
8932
9366
  if (usage) {
@@ -8943,24 +9377,24 @@ var NeatlogsTraceProcessor = class {
8943
9377
  } else if (spanType === "function" || spanType === "tool" || spanType === "tool_call") {
8944
9378
  const output = data?.output ?? data?.result;
8945
9379
  if (output != null) {
8946
- otelSpan.setAttribute("output.value", typeof output === "string" ? output : safeStringify10(output));
9380
+ otelSpan.setAttribute("output.value", typeof output === "string" ? output : safeStringify11(output));
8947
9381
  }
8948
9382
  } else if (spanType === "agent" || spanType === "agent_run") {
8949
9383
  const output = data?.output;
8950
9384
  if (output != null) {
8951
- otelSpan.setAttribute("output.value", typeof output === "string" ? output : safeStringify10(output));
9385
+ otelSpan.setAttribute("output.value", typeof output === "string" ? output : safeStringify11(output));
8952
9386
  }
8953
9387
  }
8954
9388
  const error = data?.error ?? span2?.error;
8955
9389
  if (error) {
8956
9390
  if (error instanceof Error) {
8957
- otelSpan.setStatus({ code: import_api17.SpanStatusCode.ERROR, message: error.message });
9391
+ otelSpan.setStatus({ code: import_api18.SpanStatusCode.ERROR, message: error.message });
8958
9392
  otelSpan.recordException(error);
8959
9393
  } else {
8960
- otelSpan.setStatus({ code: import_api17.SpanStatusCode.ERROR, message: String(error) });
9394
+ otelSpan.setStatus({ code: import_api18.SpanStatusCode.ERROR, message: String(error) });
8961
9395
  }
8962
9396
  } else {
8963
- otelSpan.setStatus({ code: import_api17.SpanStatusCode.OK });
9397
+ otelSpan.setStatus({ code: import_api18.SpanStatusCode.OK });
8964
9398
  }
8965
9399
  if (startTime) {
8966
9400
  otelSpan.setAttribute("neatlogs.llm.metrics.duration_ms", Date.now() - startTime);
@@ -8971,7 +9405,7 @@ var NeatlogsTraceProcessor = class {
8971
9405
  }
8972
9406
  shutdown() {
8973
9407
  for (const [, span2] of this._spans) {
8974
- span2.setStatus({ code: import_api17.SpanStatusCode.ERROR, message: "Processor shutdown before span completed" });
9408
+ span2.setStatus({ code: import_api18.SpanStatusCode.ERROR, message: "Processor shutdown before span completed" });
8975
9409
  span2.end();
8976
9410
  }
8977
9411
  this._spans.clear();
@@ -8980,7 +9414,7 @@ var NeatlogsTraceProcessor = class {
8980
9414
  forceFlush() {
8981
9415
  }
8982
9416
  };
8983
- function safeStringify10(value) {
9417
+ function safeStringify11(value) {
8984
9418
  try {
8985
9419
  return typeof value === "string" ? value : JSON.stringify(value);
8986
9420
  } catch {
@@ -8989,8 +9423,8 @@ function safeStringify10(value) {
8989
9423
  }
8990
9424
 
8991
9425
  // src/mastra-wrap.ts
8992
- var import_api18 = require("@opentelemetry/api");
8993
- var TRACER_NAME10 = "neatlogs.mastra";
9426
+ var import_api19 = require("@opentelemetry/api");
9427
+ var TRACER_NAME11 = "neatlogs.mastra";
8994
9428
  var PATCH_FLAG2 = "_neatlogs_patched";
8995
9429
  function wrapMastra(entity) {
8996
9430
  if (!entity || entity[PATCH_FLAG2]) return entity;
@@ -9056,14 +9490,14 @@ function patchAgentMethod(agent, method, streaming) {
9056
9490
  }
9057
9491
  if (streaming) attrs["neatlogs.llm.is_streaming"] = true;
9058
9492
  if (streaming) {
9059
- const tracer = import_api18.trace.getTracer(TRACER_NAME10);
9060
- const span2 = tracer.startSpan(`mastra.agent.${agentName}`, { attributes: attrs }, import_api18.context.active());
9061
- const ctx = import_api18.trace.setSpan(import_api18.context.active(), span2);
9493
+ const tracer = import_api19.trace.getTracer(TRACER_NAME11);
9494
+ const span2 = tracer.startSpan(`mastra.agent.${agentName}`, { attributes: attrs }, import_api19.context.active());
9495
+ const ctx = import_api19.trace.setSpan(import_api19.context.active(), span2);
9062
9496
  try {
9063
- const result = await import_api18.context.with(ctx, () => orig(input, opts));
9497
+ const result = await import_api19.context.with(ctx, () => orig(input, opts));
9064
9498
  return wrapStreamingOutput(result, span2, ctx);
9065
9499
  } catch (err) {
9066
- recordError6(span2, err);
9500
+ recordError7(span2, err);
9067
9501
  span2.end();
9068
9502
  throw err;
9069
9503
  }
@@ -9106,7 +9540,7 @@ function patchModelInPlace(model) {
9106
9540
  if (provider) attrs["neatlogs.llm.provider"] = provider;
9107
9541
  if (isStream) attrs["neatlogs.llm.is_streaming"] = true;
9108
9542
  const promptInput = callOpts?.prompt ?? callOpts?.messages;
9109
- if (promptInput !== void 0) attrs["input.value"] = safeStringify11(promptInput);
9543
+ if (promptInput !== void 0) attrs["input.value"] = safeStringify12(promptInput);
9110
9544
  captureInvocationParams(attrs, callOpts);
9111
9545
  return withActiveSpan(`mastra.llm.${modelId || "model"}.${fn}`, attrs, async (span2) => {
9112
9546
  const result = await orig(callOpts);
@@ -9140,12 +9574,12 @@ function patchToolExecute(tool, key) {
9140
9574
  const attrs = {
9141
9575
  "neatlogs.span.kind": "TOOL",
9142
9576
  "neatlogs.tool.name": toolName,
9143
- "input.value": safeStringify11(params)
9577
+ "input.value": safeStringify12(params)
9144
9578
  };
9145
9579
  if (tool.description) attrs["neatlogs.tool.description"] = String(tool.description);
9146
9580
  return withActiveSpan(`mastra.tool.${toolName}`, attrs, async (span2) => {
9147
9581
  const result = await orig(params, options);
9148
- span2.setAttribute("output.value", safeStringify11(result));
9582
+ span2.setAttribute("output.value", safeStringify12(result));
9149
9583
  return result;
9150
9584
  });
9151
9585
  };
@@ -9171,13 +9605,13 @@ function patchRunMethod(run, method, workflowName) {
9171
9605
  "neatlogs.workflow.name": workflowName
9172
9606
  };
9173
9607
  if (startOpts?.inputData !== void 0) {
9174
- attrs["input.value"] = safeStringify11(startOpts.inputData);
9608
+ attrs["input.value"] = safeStringify12(startOpts.inputData);
9175
9609
  }
9176
9610
  return withActiveSpan(`mastra.workflow.${workflowName}`, attrs, async (span2) => {
9177
9611
  const result = await orig(startOpts);
9178
- if (result?.status) span2.setAttribute("neatlogs.metadata", safeStringify11({ status: result.status }));
9612
+ if (result?.status) span2.setAttribute("neatlogs.metadata", safeStringify12({ status: result.status }));
9179
9613
  if (result?.result !== void 0) {
9180
- span2.setAttribute("output.value", safeStringify11(result.result));
9614
+ span2.setAttribute("output.value", safeStringify12(result.result));
9181
9615
  }
9182
9616
  return result;
9183
9617
  });
@@ -9198,14 +9632,14 @@ function patchVectorOp(vector, op, kind) {
9198
9632
  "neatlogs.span.kind": kind,
9199
9633
  "neatlogs.db.system": dbName,
9200
9634
  "neatlogs.db.operation": op,
9201
- "input.value": safeStringify11(params)
9635
+ "input.value": safeStringify12(params)
9202
9636
  };
9203
9637
  const indexName = params?.indexName;
9204
9638
  if (indexName) attrs["neatlogs.vectordb.index_name"] = String(indexName);
9205
9639
  if (kind === "RETRIEVER" && params?.topK != null) attrs["neatlogs.retriever.top_k"] = params.topK;
9206
9640
  return withActiveSpan(`mastra.vector.${op}`, attrs, async (span2) => {
9207
9641
  const result = await orig(params);
9208
- span2.setAttribute("output.value", safeStringify11(result));
9642
+ span2.setAttribute("output.value", safeStringify12(result));
9209
9643
  return result;
9210
9644
  });
9211
9645
  };
@@ -9219,11 +9653,11 @@ function patchMemory(memory) {
9219
9653
  const attrs = {
9220
9654
  "neatlogs.span.kind": "CHAIN",
9221
9655
  "neatlogs.db.operation": op,
9222
- "input.value": safeStringify11(args?.[0])
9656
+ "input.value": safeStringify12(args?.[0])
9223
9657
  };
9224
9658
  return withActiveSpan(`mastra.memory.${op}`, attrs, async (span2) => {
9225
9659
  const result = await orig(...args);
9226
- span2.setAttribute("output.value", safeStringify11(result));
9660
+ span2.setAttribute("output.value", safeStringify12(result));
9227
9661
  return result;
9228
9662
  });
9229
9663
  };
@@ -9264,7 +9698,7 @@ function wrapRootMastra(mastra) {
9264
9698
  }
9265
9699
  function wrapStreamingOutput(output, span2, ctx) {
9266
9700
  if (!output) {
9267
- span2.setStatus({ code: import_api18.SpanStatusCode.OK });
9701
+ span2.setStatus({ code: import_api19.SpanStatusCode.OK });
9268
9702
  span2.end();
9269
9703
  return output;
9270
9704
  }
@@ -9272,8 +9706,8 @@ function wrapStreamingOutput(output, span2, ctx) {
9272
9706
  const endOnce = (err) => {
9273
9707
  if (ended) return;
9274
9708
  ended = true;
9275
- if (err) recordError6(span2, err);
9276
- else span2.setStatus({ code: import_api18.SpanStatusCode.OK });
9709
+ if (err) recordError7(span2, err);
9710
+ else span2.setStatus({ code: import_api19.SpanStatusCode.OK });
9277
9711
  span2.end();
9278
9712
  };
9279
9713
  if (output.text && typeof output.text.then === "function") {
@@ -9364,7 +9798,7 @@ function rebindStreamContext(output, ctx) {
9364
9798
  const origIterator = value[Symbol.asyncIterator].bind(value);
9365
9799
  return {
9366
9800
  [Symbol.asyncIterator]() {
9367
- return import_api18.context.with(ctx, () => origIterator());
9801
+ return import_api19.context.with(ctx, () => origIterator());
9368
9802
  }
9369
9803
  };
9370
9804
  }
@@ -9406,7 +9840,7 @@ function finalizeModelResult(span2, result) {
9406
9840
  const tc = toolCalls[i];
9407
9841
  setToolCall(span2, i, tc.toolName, tc.input ?? tc.args, tc.toolCallId);
9408
9842
  }
9409
- span2.setAttribute("output.value", safeStringify11(result.content));
9843
+ span2.setAttribute("output.value", safeStringify12(result.content));
9410
9844
  }
9411
9845
  if (result.usage) recordUsage(span2, result.usage);
9412
9846
  span2.setAttribute("neatlogs.llm.stop_reason", normalizeFinishReason(result.finishReason));
@@ -9414,7 +9848,7 @@ function finalizeModelResult(span2, result) {
9414
9848
  function normalizeFinishReason(fr) {
9415
9849
  if (fr == null) return "";
9416
9850
  if (typeof fr === "string") return fr;
9417
- return String(fr.unified ?? fr.reason ?? fr.type ?? safeStringify11(fr));
9851
+ return String(fr.unified ?? fr.reason ?? fr.type ?? safeStringify12(fr));
9418
9852
  }
9419
9853
  function tokenValue(v) {
9420
9854
  if (v == null) return void 0;
@@ -9443,22 +9877,22 @@ function captureInvocationParams(attrs, callOpts) {
9443
9877
  }
9444
9878
  }
9445
9879
  if (Array.isArray(callOpts.stopSequences) && callOpts.stopSequences.length) {
9446
- attrs["neatlogs.llm.stop_sequences"] = safeStringify11(callOpts.stopSequences);
9880
+ attrs["neatlogs.llm.stop_sequences"] = safeStringify12(callOpts.stopSequences);
9447
9881
  invocation.stopSequences = callOpts.stopSequences;
9448
9882
  }
9449
9883
  if (Array.isArray(callOpts.tools)) {
9450
9884
  for (let i = 0; i < callOpts.tools.length; i++) {
9451
- attrs[`neatlogs.llm.tools.${i}`] = safeStringify11(callOpts.tools[i]);
9885
+ attrs[`neatlogs.llm.tools.${i}`] = safeStringify12(callOpts.tools[i]);
9452
9886
  }
9453
9887
  invocation.toolChoice = callOpts.toolChoice;
9454
9888
  }
9455
9889
  if (Object.keys(invocation).length) {
9456
- attrs["neatlogs.llm.invocation_parameters"] = safeStringify11(invocation);
9890
+ attrs["neatlogs.llm.invocation_parameters"] = safeStringify12(invocation);
9457
9891
  }
9458
9892
  }
9459
9893
  function setToolCall(span2, i, name, args, id) {
9460
9894
  span2.setAttribute(`neatlogs.llm.tool_calls.${i}.name`, name ?? "");
9461
- span2.setAttribute(`neatlogs.llm.tool_calls.${i}.arguments`, safeStringify11(args ?? {}));
9895
+ span2.setAttribute(`neatlogs.llm.tool_calls.${i}.arguments`, safeStringify12(args ?? {}));
9462
9896
  if (id) span2.setAttribute(`neatlogs.llm.tool_calls.${i}.id`, id);
9463
9897
  }
9464
9898
  function recordUsage(span2, usage) {
@@ -9474,16 +9908,16 @@ function recordUsage(span2, usage) {
9474
9908
  }
9475
9909
  }
9476
9910
  function withActiveSpan(name, attrs, fn) {
9477
- const tracer = import_api18.trace.getTracer(TRACER_NAME10);
9478
- const span2 = tracer.startSpan(name, { attributes: attrs }, import_api18.context.active());
9479
- const ctx = import_api18.trace.setSpan(import_api18.context.active(), span2);
9480
- return import_api18.context.with(ctx, async () => {
9911
+ const tracer = import_api19.trace.getTracer(TRACER_NAME11);
9912
+ const span2 = tracer.startSpan(name, { attributes: attrs }, import_api19.context.active());
9913
+ const ctx = import_api19.trace.setSpan(import_api19.context.active(), span2);
9914
+ return import_api19.context.with(ctx, async () => {
9481
9915
  try {
9482
9916
  const result = await fn(span2);
9483
- span2.setStatus({ code: import_api18.SpanStatusCode.OK });
9917
+ span2.setStatus({ code: import_api19.SpanStatusCode.OK });
9484
9918
  return result;
9485
9919
  } catch (err) {
9486
- recordError6(span2, err);
9920
+ recordError7(span2, err);
9487
9921
  throw err;
9488
9922
  } finally {
9489
9923
  span2.end();
@@ -9503,9 +9937,9 @@ function extractModelId(model) {
9503
9937
  return model.modelId ?? model.name ?? "";
9504
9938
  }
9505
9939
  function toInputValue(input) {
9506
- return typeof input === "string" ? input : safeStringify11(input);
9940
+ return typeof input === "string" ? input : safeStringify12(input);
9507
9941
  }
9508
- function safeStringify11(value) {
9942
+ function safeStringify12(value) {
9509
9943
  if (typeof value === "string") return value;
9510
9944
  try {
9511
9945
  return JSON.stringify(value) ?? "";
@@ -9513,25 +9947,25 @@ function safeStringify11(value) {
9513
9947
  return "";
9514
9948
  }
9515
9949
  }
9516
- function recordError6(span2, err) {
9950
+ function recordError7(span2, err) {
9517
9951
  if (err instanceof Error) {
9518
- span2.setStatus({ code: import_api18.SpanStatusCode.ERROR, message: err.message });
9952
+ span2.setStatus({ code: import_api19.SpanStatusCode.ERROR, message: err.message });
9519
9953
  span2.recordException(err);
9520
9954
  } else {
9521
- span2.setStatus({ code: import_api18.SpanStatusCode.ERROR, message: String(err) });
9955
+ span2.setStatus({ code: import_api19.SpanStatusCode.ERROR, message: String(err) });
9522
9956
  }
9523
9957
  }
9524
9958
 
9525
9959
  // src/pi-agent.ts
9526
- var import_api19 = require("@opentelemetry/api");
9527
- var TRACER_NAME11 = "neatlogs.pi-agent";
9960
+ var import_api20 = require("@opentelemetry/api");
9961
+ var TRACER_NAME12 = "neatlogs.pi-agent";
9528
9962
  var PATCH_FLAG3 = "_neatlogs_patched";
9529
9963
  function piAgentHooks(agent) {
9530
9964
  if (!agent || agent[PATCH_FLAG3]) return agent;
9531
9965
  const a = agent;
9532
9966
  if (typeof a.subscribe !== "function") return agent;
9533
9967
  const state = { toolSpans: /* @__PURE__ */ new Map(), inputMessages: [] };
9534
- const tracer = import_api19.trace.getTracer(TRACER_NAME11);
9968
+ const tracer = import_api20.trace.getTracer(TRACER_NAME12);
9535
9969
  a.subscribe((event) => {
9536
9970
  try {
9537
9971
  handleEvent(tracer, state, event);
@@ -9547,10 +9981,10 @@ function handleEvent(tracer, state, event) {
9547
9981
  const span2 = tracer.startSpan(
9548
9982
  "pi_agent.run",
9549
9983
  { attributes: { "neatlogs.span.kind": "AGENT" } },
9550
- import_api19.context.active()
9984
+ import_api20.context.active()
9551
9985
  );
9552
9986
  state.agentSpan = span2;
9553
- state.agentCtx = import_api19.trace.setSpan(import_api19.context.active(), span2);
9987
+ state.agentCtx = import_api20.trace.setSpan(import_api20.context.active(), span2);
9554
9988
  state.inputMessages = [];
9555
9989
  break;
9556
9990
  }
@@ -9569,14 +10003,14 @@ function handleEvent(tracer, state, event) {
9569
10003
  break;
9570
10004
  }
9571
10005
  case "tool_execution_start": {
9572
- const parent = state.agentCtx ?? import_api19.context.active();
10006
+ const parent = state.agentCtx ?? import_api20.context.active();
9573
10007
  const span2 = tracer.startSpan(
9574
10008
  `pi_agent.tool.${event.toolName ?? "tool"}`,
9575
10009
  {
9576
10010
  attributes: {
9577
10011
  "neatlogs.span.kind": "TOOL",
9578
10012
  ...event.toolName ? { "neatlogs.tool.name": String(event.toolName) } : {},
9579
- ...event.args !== void 0 ? { "input.value": safeStringify12(event.args) } : {}
10013
+ ...event.args !== void 0 ? { "input.value": safeStringify13(event.args) } : {}
9580
10014
  }
9581
10015
  },
9582
10016
  parent
@@ -9588,13 +10022,13 @@ function handleEvent(tracer, state, event) {
9588
10022
  const span2 = event.toolCallId ? state.toolSpans.get(event.toolCallId) : void 0;
9589
10023
  if (!span2) return;
9590
10024
  if (event.result !== void 0) {
9591
- span2.setAttribute("output.value", safeStringify12(event.result));
10025
+ span2.setAttribute("output.value", safeStringify13(event.result));
9592
10026
  }
9593
10027
  if (event.isError) {
9594
- span2.setStatus({ code: import_api19.SpanStatusCode.ERROR });
10028
+ span2.setStatus({ code: import_api20.SpanStatusCode.ERROR });
9595
10029
  span2.setAttribute("neatlogs.tool.is_error", true);
9596
10030
  } else {
9597
- span2.setStatus({ code: import_api19.SpanStatusCode.OK });
10031
+ span2.setStatus({ code: import_api20.SpanStatusCode.OK });
9598
10032
  }
9599
10033
  span2.end();
9600
10034
  if (event.toolCallId) state.toolSpans.delete(event.toolCallId);
@@ -9613,7 +10047,7 @@ function handleEvent(tracer, state, event) {
9613
10047
  if (firstUser) state.agentSpan.setAttribute("input.value", firstUser.content);
9614
10048
  const finalText = lastAssistantText(event.messages);
9615
10049
  if (finalText) state.agentSpan.setAttribute("output.value", finalText);
9616
- state.agentSpan.setStatus({ code: import_api19.SpanStatusCode.OK });
10050
+ state.agentSpan.setStatus({ code: import_api20.SpanStatusCode.OK });
9617
10051
  state.agentSpan.end();
9618
10052
  state.agentSpan = void 0;
9619
10053
  state.agentCtx = void 0;
@@ -9635,11 +10069,11 @@ function emitLlmSpan(tracer, state, msg) {
9635
10069
  attrs[`neatlogs.llm.input_messages.${i}.role`] = m.role;
9636
10070
  attrs[`neatlogs.llm.input_messages.${i}.content`] = m.content;
9637
10071
  });
9638
- attrs["neatlogs.llm.input"] = safeStringify12({ messages: inMsgs });
9639
- attrs["input.value"] = safeStringify12({ messages: inMsgs });
10072
+ attrs["neatlogs.llm.input"] = safeStringify13({ messages: inMsgs });
10073
+ attrs["input.value"] = safeStringify13({ messages: inMsgs });
9640
10074
  }
9641
10075
  const { text, toolCalls } = splitAssistantContent(msg.content);
9642
- const outText = text || toolCalls.map((tc) => `${tc.name}(${safeStringify12(tc.arguments)})`).join("\n");
10076
+ const outText = text || toolCalls.map((tc) => `${tc.name}(${safeStringify13(tc.arguments)})`).join("\n");
9643
10077
  if (outText || toolCalls.length) {
9644
10078
  attrs["neatlogs.llm.output_messages.0.role"] = "assistant";
9645
10079
  attrs["neatlogs.llm.output_messages.0.content"] = outText || "";
@@ -9649,11 +10083,11 @@ function emitLlmSpan(tracer, state, msg) {
9649
10083
  toolCalls.forEach((tc, j) => {
9650
10084
  if (tc.name) attrs[`neatlogs.llm.tool_calls.${j}.name`] = tc.name;
9651
10085
  if (tc.arguments !== void 0)
9652
- attrs[`neatlogs.llm.tool_calls.${j}.arguments`] = safeStringify12(tc.arguments);
10086
+ attrs[`neatlogs.llm.tool_calls.${j}.arguments`] = safeStringify13(tc.arguments);
9653
10087
  if (tc.id) attrs[`neatlogs.llm.tool_calls.${j}.id`] = String(tc.id);
9654
10088
  });
9655
10089
  }
9656
- attrs["neatlogs.llm.output"] = safeStringify12(outBlob);
10090
+ attrs["neatlogs.llm.output"] = safeStringify13(outBlob);
9657
10091
  attrs["output.value"] = outText || "";
9658
10092
  }
9659
10093
  const usage = msg.usage;
@@ -9665,13 +10099,13 @@ function emitLlmSpan(tracer, state, msg) {
9665
10099
  if (usage.cacheRead) attrs["neatlogs.llm.token_count.cache_read"] = usage.cacheRead;
9666
10100
  if (usage.cacheWrite) attrs["neatlogs.llm.token_count.cache_write"] = usage.cacheWrite;
9667
10101
  }
9668
- const parent = state.agentCtx ?? import_api19.context.active();
10102
+ const parent = state.agentCtx ?? import_api20.context.active();
9669
10103
  const span2 = tracer.startSpan(
9670
10104
  `pi_agent.llm.${msg.model || "model"}`,
9671
10105
  { attributes: attrs },
9672
10106
  parent
9673
10107
  );
9674
- span2.setStatus({ code: import_api19.SpanStatusCode.OK });
10108
+ span2.setStatus({ code: import_api20.SpanStatusCode.OK });
9675
10109
  span2.end();
9676
10110
  }
9677
10111
  function splitAssistantContent(content) {
@@ -9699,7 +10133,7 @@ function messageText(msg) {
9699
10133
  if (typeof block === "string") parts.push(block);
9700
10134
  else if (block && typeof block === "object") {
9701
10135
  if (typeof block.text === "string") parts.push(block.text);
9702
- else if (block.type === "toolCall") parts.push(`${block.name ?? "tool"}(${safeStringify12(block.arguments)})`);
10136
+ else if (block.type === "toolCall") parts.push(`${block.name ?? "tool"}(${safeStringify13(block.arguments)})`);
9703
10137
  }
9704
10138
  }
9705
10139
  return parts.join("");
@@ -9722,7 +10156,7 @@ function markPatched2(e) {
9722
10156
  e[PATCH_FLAG3] = true;
9723
10157
  }
9724
10158
  }
9725
- function safeStringify12(value) {
10159
+ function safeStringify13(value) {
9726
10160
  if (typeof value === "string") return value;
9727
10161
  try {
9728
10162
  return JSON.stringify(value) ?? "";
@@ -9732,8 +10166,8 @@ function safeStringify12(value) {
9732
10166
  }
9733
10167
 
9734
10168
  // src/claude-agent-sdk.ts
9735
- var import_api20 = require("@opentelemetry/api");
9736
- var TRACER_NAME12 = "neatlogs.claude_agent_sdk";
10169
+ var import_api21 = require("@opentelemetry/api");
10170
+ var TRACER_NAME13 = "neatlogs.claude_agent_sdk";
9737
10171
  var ROOT_SCOPE = "__root__";
9738
10172
  function wrapClaudeAgentSDK(sdk, options = {}) {
9739
10173
  if (!sdk || typeof sdk.query !== "function") return sdk;
@@ -9753,13 +10187,13 @@ function wrapClaudeAgentSDK(sdk, options = {}) {
9753
10187
  }
9754
10188
  function wrapQuery(original, options) {
9755
10189
  return function(params, ...rest) {
9756
- const tracer = import_api20.trace.getTracer(TRACER_NAME12);
10190
+ const tracer = import_api21.trace.getTracer(TRACER_NAME13);
9757
10191
  const workflowName = options.workflowName ?? "claude_agent.query";
9758
10192
  const promptRef = { text: "" };
9759
10193
  const agentSpan = tracer.startSpan(
9760
10194
  "claude_agent.query",
9761
10195
  { attributes: { "neatlogs.span.kind": "AGENT", "neatlogs.workflow.name": workflowName } },
9762
- import_api20.context.active()
10196
+ import_api21.context.active()
9763
10197
  );
9764
10198
  const promptText = extractPromptText(params?.prompt);
9765
10199
  if (promptText) {
@@ -9768,8 +10202,8 @@ function wrapQuery(original, options) {
9768
10202
  } else if (params && isAsyncIterable(params.prompt)) {
9769
10203
  params = { ...params, prompt: tapPromptStream(params.prompt, promptRef) };
9770
10204
  }
9771
- const agentCtx = import_api20.trace.setSpan(import_api20.context.active(), agentSpan);
9772
- const queryObj = import_api20.context.with(agentCtx, () => original(params, ...rest));
10205
+ const agentCtx = import_api21.trace.setSpan(import_api21.context.active(), agentSpan);
10206
+ const queryObj = import_api21.context.with(agentCtx, () => original(params, ...rest));
9773
10207
  return instrumentQueryIterable(queryObj, agentSpan, agentCtx, tracer, promptRef);
9774
10208
  };
9775
10209
  }
@@ -9788,7 +10222,7 @@ async function* tapPromptStream(prompt, ref) {
9788
10222
  function instrumentQueryIterable(queryObj, agentSpan, agentCtx, tracer, promptRef) {
9789
10223
  const originalAsyncIterator = queryObj?.[Symbol.asyncIterator]?.bind(queryObj);
9790
10224
  if (!originalAsyncIterator) {
9791
- agentSpan.setStatus({ code: import_api20.SpanStatusCode.OK });
10225
+ agentSpan.setStatus({ code: import_api21.SpanStatusCode.OK });
9792
10226
  agentSpan.end();
9793
10227
  return queryObj;
9794
10228
  }
@@ -9823,9 +10257,9 @@ function instrumentQueryIterable(queryObj, agentSpan, agentCtx, tracer, promptRe
9823
10257
  }
9824
10258
  if (rootScope.finalText) agentSpan.setAttribute("output.value", rootScope.finalText);
9825
10259
  if (status === "error") {
9826
- recordError7(agentSpan, err);
10260
+ recordError8(agentSpan, err);
9827
10261
  } else {
9828
- agentSpan.setStatus({ code: import_api20.SpanStatusCode.OK });
10262
+ agentSpan.setStatus({ code: import_api21.SpanStatusCode.OK });
9829
10263
  agentSpan.end();
9830
10264
  }
9831
10265
  };
@@ -9836,7 +10270,7 @@ function instrumentQueryIterable(queryObj, agentSpan, agentCtx, tracer, promptRe
9836
10270
  return {
9837
10271
  async next() {
9838
10272
  try {
9839
- const result = await import_api20.context.with(agentCtx, () => iterator.next());
10273
+ const result = await import_api21.context.with(agentCtx, () => iterator.next());
9840
10274
  if (result.done) {
9841
10275
  finalizeAgent("ok");
9842
10276
  return result;
@@ -9867,7 +10301,7 @@ function instrumentQueryIterable(queryObj, agentSpan, agentCtx, tracer, promptRe
9867
10301
  function closeScope(scope, status) {
9868
10302
  try {
9869
10303
  if (scope.finalText) scope.span.setAttribute("output.value", scope.finalText);
9870
- scope.span.setStatus({ code: status === "ok" ? import_api20.SpanStatusCode.OK : import_api20.SpanStatusCode.ERROR });
10304
+ scope.span.setStatus({ code: status === "ok" ? import_api21.SpanStatusCode.OK : import_api21.SpanStatusCode.ERROR });
9871
10305
  scope.span.end();
9872
10306
  } catch {
9873
10307
  }
@@ -9882,7 +10316,7 @@ function getScope(tracer, state, msg) {
9882
10316
  flushAssistantTurn(tracer, root, state);
9883
10317
  }
9884
10318
  const parentToolSpan = state.toolSpans.get(parentId);
9885
- const parentCtx = parentToolSpan ? import_api20.trace.setSpan(import_api20.context.active(), parentToolSpan) : state.scopes.get(ROOT_SCOPE).ctx;
10319
+ const parentCtx = parentToolSpan ? import_api21.trace.setSpan(import_api21.context.active(), parentToolSpan) : state.scopes.get(ROOT_SCOPE).ctx;
9886
10320
  const subType = msg?.subagent_type ? String(msg.subagent_type) : "subagent";
9887
10321
  const attrs = {
9888
10322
  "neatlogs.span.kind": "AGENT",
@@ -9892,7 +10326,7 @@ function getScope(tracer, state, msg) {
9892
10326
  const span2 = tracer.startSpan(`claude_agent.subagent.${subType}`, { attributes: attrs }, parentCtx);
9893
10327
  const scope = {
9894
10328
  span: span2,
9895
- ctx: import_api20.trace.setSpan(import_api20.context.active(), span2),
10329
+ ctx: import_api21.trace.setSpan(import_api21.context.active(), span2),
9896
10330
  inputMessages: msg?.task_description ? [{ role: "user", content: String(msg.task_description) }] : [],
9897
10331
  assistantBuffer: null,
9898
10332
  finalText: "",
@@ -9947,7 +10381,7 @@ function handleMessage(tracer, state, msg, finalizeAgent) {
9947
10381
  rootScope.span.setAttribute("neatlogs.conversation.id", String(msg.session_id));
9948
10382
  }
9949
10383
  const usage = msg.usage;
9950
- if (usage) setUsage2(rootScope.span, usage);
10384
+ if (usage) setUsage3(rootScope.span, usage);
9951
10385
  if (msg.total_cost_usd != null) rootScope.span.setAttribute("neatlogs.agent.cost_usd", msg.total_cost_usd);
9952
10386
  if (msg.num_turns != null) rootScope.span.setAttribute("neatlogs.agent.num_turns", msg.num_turns);
9953
10387
  if (msg.is_error) rootScope.span.setAttribute("neatlogs.agent.is_error", true);
@@ -9996,10 +10430,10 @@ function flushAssistantTurn(tracer, scope, state) {
9996
10430
  attrs[`neatlogs.llm.input_messages.${i}.content`] = m.content;
9997
10431
  });
9998
10432
  if (scope.inputMessages.length) {
9999
- attrs["input.value"] = safeStringify13({ messages: scope.inputMessages });
10433
+ attrs["input.value"] = safeStringify14({ messages: scope.inputMessages });
10000
10434
  }
10001
10435
  const outText = buf.textParts.join("");
10002
- const outValue = outText || buf.toolCalls.map((tc) => `${tc.name}(${safeStringify13(tc.input ?? {})})`).join("\n");
10436
+ const outValue = outText || buf.toolCalls.map((tc) => `${tc.name}(${safeStringify14(tc.input ?? {})})`).join("\n");
10003
10437
  if (outValue) {
10004
10438
  attrs["neatlogs.llm.output_messages.0.role"] = "assistant";
10005
10439
  attrs["neatlogs.llm.output_messages.0.content"] = outValue;
@@ -10011,17 +10445,17 @@ function flushAssistantTurn(tracer, scope, state) {
10011
10445
  buf.toolCalls.forEach((tc, j) => {
10012
10446
  attrs[`neatlogs.llm.tool_calls.${j}.id`] = tc.id;
10013
10447
  attrs[`neatlogs.llm.tool_calls.${j}.name`] = tc.name;
10014
- attrs[`neatlogs.llm.tool_calls.${j}.arguments`] = safeStringify13(tc.input ?? {});
10448
+ attrs[`neatlogs.llm.tool_calls.${j}.arguments`] = safeStringify14(tc.input ?? {});
10015
10449
  });
10016
10450
  if (buf.stopReason) attrs["neatlogs.llm.finish_reason"] = String(buf.stopReason);
10017
10451
  const span2 = tracer.startSpan(`claude_agent.llm.${model || "model"}`, { attributes: attrs }, scope.ctx);
10018
- if (buf.usage) setUsage2(span2, buf.usage);
10019
- span2.setStatus({ code: import_api20.SpanStatusCode.OK });
10452
+ if (buf.usage) setUsage3(span2, buf.usage);
10453
+ span2.setStatus({ code: import_api21.SpanStatusCode.OK });
10020
10454
  span2.end();
10021
10455
  if (outText) scope.finalText = outText;
10022
10456
  const turnParts = [];
10023
10457
  if (outText) turnParts.push(outText);
10024
- for (const tc of buf.toolCalls) turnParts.push(`[tool_call ${tc.name} ${safeStringify13(tc.input ?? {})}]`);
10458
+ for (const tc of buf.toolCalls) turnParts.push(`[tool_call ${tc.name} ${safeStringify14(tc.input ?? {})}]`);
10025
10459
  if (turnParts.length) scope.inputMessages.push({ role: "assistant", content: turnParts.join("\n") });
10026
10460
  for (const tc of buf.toolCalls) {
10027
10461
  const toolSpan = tracer.startSpan(
@@ -10031,7 +10465,7 @@ function flushAssistantTurn(tracer, scope, state) {
10031
10465
  "neatlogs.span.kind": "TOOL",
10032
10466
  "neatlogs.tool.name": String(tc.name ?? ""),
10033
10467
  ...tc.id ? { "neatlogs.tool_call.id": String(tc.id) } : {},
10034
- "input.value": safeStringify13(tc.input ?? {})
10468
+ "input.value": safeStringify14(tc.input ?? {})
10035
10469
  }
10036
10470
  },
10037
10471
  scope.ctx
@@ -10045,16 +10479,16 @@ function closeToolSpansFromUser(state, scope, msg) {
10045
10479
  if (block?.type !== "tool_result") continue;
10046
10480
  const id = block.tool_use_id ?? "";
10047
10481
  const out = block.content;
10048
- const outText = typeof out === "string" ? out : safeStringify13(out);
10482
+ const outText = typeof out === "string" ? out : safeStringify14(out);
10049
10483
  if (outText) scope.inputMessages.push({ role: "tool", content: outText });
10050
10484
  const span2 = state.toolSpans.get(id);
10051
10485
  if (!span2) continue;
10052
10486
  span2.setAttribute("output.value", outText);
10053
10487
  if (block.is_error) {
10054
- span2.setStatus({ code: import_api20.SpanStatusCode.ERROR });
10488
+ span2.setStatus({ code: import_api21.SpanStatusCode.ERROR });
10055
10489
  span2.setAttribute("neatlogs.tool.is_error", true);
10056
10490
  } else {
10057
- span2.setStatus({ code: import_api20.SpanStatusCode.OK });
10491
+ span2.setStatus({ code: import_api21.SpanStatusCode.OK });
10058
10492
  }
10059
10493
  span2.end();
10060
10494
  state.toolSpans.delete(id);
@@ -10073,7 +10507,7 @@ function userMessageText(msg) {
10073
10507
  }
10074
10508
  return parts.join("\n");
10075
10509
  }
10076
- function setUsage2(span2, usage) {
10510
+ function setUsage3(span2, usage) {
10077
10511
  if (!usage) return;
10078
10512
  if (usage.input_tokens != null) span2.setAttribute("neatlogs.llm.token_count.prompt", usage.input_tokens);
10079
10513
  if (usage.output_tokens != null) span2.setAttribute("neatlogs.llm.token_count.completion", usage.output_tokens);
@@ -10091,7 +10525,7 @@ function extractPromptText(prompt) {
10091
10525
  if (typeof prompt === "string") return prompt;
10092
10526
  return "";
10093
10527
  }
10094
- function safeStringify13(value) {
10528
+ function safeStringify14(value) {
10095
10529
  if (typeof value === "string") return value;
10096
10530
  try {
10097
10531
  return JSON.stringify(value) ?? "";
@@ -10099,20 +10533,20 @@ function safeStringify13(value) {
10099
10533
  return "";
10100
10534
  }
10101
10535
  }
10102
- function recordError7(span2, err) {
10536
+ function recordError8(span2, err) {
10103
10537
  if (err instanceof Error) {
10104
- span2.setStatus({ code: import_api20.SpanStatusCode.ERROR, message: err.message });
10538
+ span2.setStatus({ code: import_api21.SpanStatusCode.ERROR, message: err.message });
10105
10539
  span2.recordException(err);
10106
10540
  } else {
10107
- span2.setStatus({ code: import_api20.SpanStatusCode.ERROR, message: String(err) });
10541
+ span2.setStatus({ code: import_api21.SpanStatusCode.ERROR, message: String(err) });
10108
10542
  }
10109
10543
  span2.end();
10110
10544
  }
10111
10545
 
10112
10546
  // src/openrouter-agent.ts
10113
- var import_api21 = require("@opentelemetry/api");
10114
- var TRACER_NAME13 = "neatlogs.openrouter_agent";
10115
- var PROVIDER4 = "openrouter";
10547
+ var import_api22 = require("@opentelemetry/api");
10548
+ var TRACER_NAME14 = "neatlogs.openrouter_agent";
10549
+ var PROVIDER5 = "openrouter";
10116
10550
  function wrapOpenRouterAgent(client) {
10117
10551
  const c = client;
10118
10552
  if (!c || c._neatlogsWrapped) return client;
@@ -10129,30 +10563,30 @@ function wrapOpenRouterAgent(client) {
10129
10563
  function wrapCallModel(callModel) {
10130
10564
  return function(clientArg, opts, ...rest) {
10131
10565
  const span2 = startLlmSpan(opts);
10132
- const ctx = import_api21.trace.setSpan(import_api21.context.active(), span2);
10133
- const result = import_api21.context.with(ctx, () => callModel.call(this, clientArg, opts, ...rest));
10566
+ const ctx = import_api22.trace.setSpan(import_api22.context.active(), span2);
10567
+ const result = import_api22.context.with(ctx, () => callModel.call(this, clientArg, opts, ...rest));
10134
10568
  return instrumentModelResult(result, span2);
10135
10569
  };
10136
10570
  }
10137
10571
  function tracedCallModel(original) {
10138
10572
  return function(opts, ...rest) {
10139
10573
  const span2 = startLlmSpan(opts);
10140
- const ctx = import_api21.trace.setSpan(import_api21.context.active(), span2);
10141
- const result = import_api21.context.with(ctx, () => original(opts, ...rest));
10574
+ const ctx = import_api22.trace.setSpan(import_api22.context.active(), span2);
10575
+ const result = import_api22.context.with(ctx, () => original(opts, ...rest));
10142
10576
  return instrumentModelResult(result, span2);
10143
10577
  };
10144
10578
  }
10145
10579
  function startLlmSpan(opts) {
10146
- const tracer = getProviderTracer(TRACER_NAME13);
10580
+ const tracer = getProviderTracer(TRACER_NAME14);
10147
10581
  const model = opts?.model ?? "";
10148
10582
  const span2 = tracer.startSpan("openrouter.call_model", {
10149
10583
  attributes: {
10150
10584
  "neatlogs.span.kind": "LLM",
10151
- "neatlogs.llm.provider": PROVIDER4,
10152
- "neatlogs.llm.system": PROVIDER4,
10585
+ "neatlogs.llm.provider": PROVIDER5,
10586
+ "neatlogs.llm.system": PROVIDER5,
10153
10587
  "neatlogs.llm.model_name": model
10154
10588
  }
10155
- }, import_api21.context.active());
10589
+ }, import_api22.context.active());
10156
10590
  const messages = Array.isArray(opts?.messages) ? opts.messages : Array.isArray(opts?.input) ? opts.input : [];
10157
10591
  if (messages.length) {
10158
10592
  messages.forEach((msg, i) => {
@@ -10160,10 +10594,10 @@ function startLlmSpan(opts) {
10160
10594
  const content = msg?.content;
10161
10595
  span2.setAttribute(
10162
10596
  `neatlogs.llm.input_messages.${i}.content`,
10163
- typeof content === "string" ? content : safeStringify14(content)
10597
+ typeof content === "string" ? content : safeStringify15(content)
10164
10598
  );
10165
10599
  });
10166
- span2.setAttribute("input.value", safeStringify14({ messages }));
10600
+ span2.setAttribute("input.value", safeStringify15({ messages }));
10167
10601
  } else if (typeof opts?.input === "string") {
10168
10602
  span2.setAttribute("neatlogs.llm.input_messages.0.role", "user");
10169
10603
  span2.setAttribute("neatlogs.llm.input_messages.0.content", opts.input);
@@ -10204,7 +10638,7 @@ function startLlmSpan(opts) {
10204
10638
  }
10205
10639
  function instrumentModelResult(result, span2) {
10206
10640
  if (!result || typeof result !== "object" && typeof result !== "function") {
10207
- span2.setStatus({ code: import_api21.SpanStatusCode.OK });
10641
+ span2.setStatus({ code: import_api22.SpanStatusCode.OK });
10208
10642
  span2.end();
10209
10643
  return result;
10210
10644
  }
@@ -10215,14 +10649,14 @@ function instrumentModelResult(result, span2) {
10215
10649
  try {
10216
10650
  finalizeLlm(span2, resolved);
10217
10651
  } catch {
10218
- span2.setStatus({ code: import_api21.SpanStatusCode.OK });
10652
+ span2.setStatus({ code: import_api22.SpanStatusCode.OK });
10219
10653
  span2.end();
10220
10654
  }
10221
10655
  };
10222
10656
  const finalizeError = (err) => {
10223
10657
  if (finalized) return;
10224
10658
  finalized = true;
10225
- recordError8(span2, err);
10659
+ recordError9(span2, err);
10226
10660
  };
10227
10661
  return new Proxy(result, {
10228
10662
  get(obj, prop, receiver) {
@@ -10308,7 +10742,7 @@ function finalizeLlm(span2, result) {
10308
10742
  span2.setAttribute(`neatlogs.llm.tool_calls.${j}.id`, tc?.id ?? "");
10309
10743
  span2.setAttribute(`neatlogs.llm.tool_calls.${j}.name`, tc?.function?.name ?? tc?.name ?? "");
10310
10744
  const args = tc?.function?.arguments ?? tc?.arguments;
10311
- span2.setAttribute(`neatlogs.llm.tool_calls.${j}.arguments`, typeof args === "string" ? args : safeStringify14(args ?? {}));
10745
+ span2.setAttribute(`neatlogs.llm.tool_calls.${j}.arguments`, typeof args === "string" ? args : safeStringify15(args ?? {}));
10312
10746
  });
10313
10747
  }
10314
10748
  const model = result?.model ?? result?.response?.model;
@@ -10318,7 +10752,7 @@ function finalizeLlm(span2, result) {
10318
10752
  const finishReason = result?.finishReason ?? result?.finish_reason ?? result?.choices?.[0]?.finish_reason;
10319
10753
  if (finishReason) span2.setAttribute("neatlogs.llm.finish_reason", String(finishReason));
10320
10754
  setOpenResponsesUsage(span2, result);
10321
- span2.setStatus({ code: import_api21.SpanStatusCode.OK });
10755
+ span2.setStatus({ code: import_api22.SpanStatusCode.OK });
10322
10756
  span2.end();
10323
10757
  }
10324
10758
  function extractOpenResponsesText(result) {
@@ -10334,7 +10768,7 @@ function extractOpenResponsesText(result) {
10334
10768
  }
10335
10769
  return parts.length ? parts.join("") : void 0;
10336
10770
  }
10337
- function safeStringify14(value) {
10771
+ function safeStringify15(value) {
10338
10772
  if (typeof value === "string") return value;
10339
10773
  try {
10340
10774
  return JSON.stringify(value) ?? "";
@@ -10342,12 +10776,12 @@ function safeStringify14(value) {
10342
10776
  return "";
10343
10777
  }
10344
10778
  }
10345
- function recordError8(span2, err) {
10779
+ function recordError9(span2, err) {
10346
10780
  if (err instanceof Error) {
10347
- span2.setStatus({ code: import_api21.SpanStatusCode.ERROR, message: err.message });
10781
+ span2.setStatus({ code: import_api22.SpanStatusCode.ERROR, message: err.message });
10348
10782
  span2.recordException(err);
10349
10783
  } else {
10350
- span2.setStatus({ code: import_api21.SpanStatusCode.ERROR, message: String(err) });
10784
+ span2.setStatus({ code: import_api22.SpanStatusCode.ERROR, message: String(err) });
10351
10785
  }
10352
10786
  span2.end();
10353
10787
  }
@@ -10504,7 +10938,7 @@ var protoRoot = import_protobufjs.default.Root.fromJSON(OTLP_PROTO_JSON);
10504
10938
  var ExportTraceServiceRequest = protoRoot.lookupType(
10505
10939
  "opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest"
10506
10940
  );
10507
- var SpanStatusCode16 = { UNSET: 0, OK: 1, ERROR: 2 };
10941
+ var SpanStatusCode17 = { UNSET: 0, OK: 1, ERROR: 2 };
10508
10942
  function randomBytes(length) {
10509
10943
  const out = new Uint8Array(length);
10510
10944
  const crypto = globalThis.crypto;
@@ -10711,7 +11145,7 @@ var NeatlogsOpencodePlugin = async (_ctx) => {
10711
11145
  startTimeUnixNano: st.rootSpan.startNano,
10712
11146
  endTimeUnixNano: nowNanoString(),
10713
11147
  attributes: attrs,
10714
- status: { code: SpanStatusCode16.OK }
11148
+ status: { code: SpanStatusCode17.OK }
10715
11149
  });
10716
11150
  const m = nowNanoString();
10717
11151
  shipper.enqueue({
@@ -10798,18 +11232,18 @@ var NeatlogsOpencodePlugin = async (_ctx) => {
10798
11232
  { key: "neatlogs.conversation.id", value: { stringValue: sessionID } }
10799
11233
  ];
10800
11234
  if (start.args !== void 0) {
10801
- setIO(attrs, "TOOL", safeStringify15(start.args), void 0);
10802
- push(attrs, attrStr("neatlogs.tool.input", safeStringify15(start.args)));
11235
+ setIO(attrs, "TOOL", safeStringify16(start.args), void 0);
11236
+ push(attrs, attrStr("neatlogs.tool.input", safeStringify16(start.args)));
10803
11237
  }
10804
11238
  if (result?.title) push(attrs, attrStr("neatlogs.tool.title", String(result.title)));
10805
11239
  const out = result?.output ?? result?.result;
10806
11240
  if (out !== void 0) {
10807
- const o = typeof out === "string" ? out : safeStringify15(out);
11241
+ const o = typeof out === "string" ? out : safeStringify16(out);
10808
11242
  setIO(attrs, "TOOL", void 0, o);
10809
11243
  push(attrs, attrStr("neatlogs.tool.output", o));
10810
11244
  }
10811
11245
  if (result?.metadata !== void 0) {
10812
- push(attrs, attrStr("neatlogs.tool.metadata", safeStringify15(result.metadata)));
11246
+ push(attrs, attrStr("neatlogs.tool.metadata", safeStringify16(result.metadata)));
10813
11247
  }
10814
11248
  shipper.enqueue({
10815
11249
  traceId: st.traceId,
@@ -10820,7 +11254,7 @@ var NeatlogsOpencodePlugin = async (_ctx) => {
10820
11254
  startTimeUnixNano: start.startNano,
10821
11255
  endTimeUnixNano: nowNanoString(),
10822
11256
  attributes: attrs,
10823
- status: { code: SpanStatusCode16.OK }
11257
+ status: { code: SpanStatusCode17.OK }
10824
11258
  });
10825
11259
  } catch {
10826
11260
  }
@@ -10907,10 +11341,10 @@ function emitLlmSpan2(shipper, st, info, sessionID) {
10907
11341
  if (toolCalls.length) {
10908
11342
  toolCalls.forEach((tc, j) => {
10909
11343
  push(attrs, attrStr(`neatlogs.llm.tool_calls.${j}.name`, tc.name));
10910
- push(attrs, attrStr(`neatlogs.llm.tool_calls.${j}.arguments`, safeStringify15(tc.input ?? {})));
11344
+ push(attrs, attrStr(`neatlogs.llm.tool_calls.${j}.arguments`, safeStringify16(tc.input ?? {})));
10911
11345
  });
10912
11346
  if (!outText) {
10913
- const rendered = toolCalls.map((tc) => `\u2192 ${tc.name}(${safeStringify15(tc.input ?? {})})`).join("\n");
11347
+ const rendered = toolCalls.map((tc) => `\u2192 ${tc.name}(${safeStringify16(tc.input ?? {})})`).join("\n");
10914
11348
  push(attrs, attrStr("neatlogs.llm.output_messages.0.role", "assistant"));
10915
11349
  push(attrs, attrStr("neatlogs.llm.output_messages.0.content", rendered));
10916
11350
  setIO(attrs, "LLM", void 0, rendered);
@@ -10939,7 +11373,7 @@ function emitLlmSpan2(shipper, st, info, sessionID) {
10939
11373
  startTimeUnixNano: startNano,
10940
11374
  endTimeUnixNano: nowNanoString(),
10941
11375
  attributes: attrs,
10942
- status: { code: SpanStatusCode16.OK }
11376
+ status: { code: SpanStatusCode17.OK }
10943
11377
  });
10944
11378
  if (info?.id) {
10945
11379
  st.outputParts.delete(String(info.id));
@@ -10975,7 +11409,7 @@ function messageText2(info) {
10975
11409
  }
10976
11410
  return out.join("");
10977
11411
  }
10978
- function safeStringify15(value) {
11412
+ function safeStringify16(value) {
10979
11413
  if (typeof value === "string") return value;
10980
11414
  try {
10981
11415
  return JSON.stringify(value) ?? "";
@@ -11072,6 +11506,7 @@ function bindTemplates(llm, systemTpl, userTpl, compiledVars) {
11072
11506
  traceToolAnthropic,
11073
11507
  traceToolAzureOpenAI,
11074
11508
  traceToolBedrock,
11509
+ traceToolGoogleGenAI,
11075
11510
  traceToolOpenAI,
11076
11511
  traceToolVertexAI,
11077
11512
  updatePrompt,
@@ -11081,6 +11516,8 @@ function bindTemplates(llm, systemTpl, userTpl, compiledVars) {
11081
11516
  wrapBedrock,
11082
11517
  wrapCallModel,
11083
11518
  wrapClaudeAgentSDK,
11519
+ wrapGoogleGenAI,
11520
+ wrapGoogleGenAIChat,
11084
11521
  wrapMastra,
11085
11522
  wrapOpenAI,
11086
11523
  wrapOpenRouterAgent,