neatlogs 1.1.13 → 1.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -6023,7 +6023,7 @@ function _resetMastraCache() {
6023
6023
  }
6024
6024
 
6025
6025
  // src/version.ts
6026
- var __version__ = "1.1.13";
6026
+ var __version__ = "1.1.14";
6027
6027
 
6028
6028
  // src/init.ts
6029
6029
  var logger13 = getLogger();
@@ -6912,29 +6912,25 @@ function tracedResponsesCreate(original) {
6912
6912
  return function(opts, ...rest) {
6913
6913
  const tracer = getProviderTracer(TRACER_NAME3);
6914
6914
  const model = opts?.model ?? "";
6915
+ const isStream = opts?.stream === true;
6915
6916
  const span2 = tracer.startSpan("openai.responses.create", {
6916
6917
  attributes: {
6917
6918
  "neatlogs.span.kind": "LLM",
6918
6919
  "neatlogs.llm.provider": "openai",
6919
6920
  "neatlogs.llm.system": "openai",
6920
6921
  "neatlogs.llm.model_name": model,
6922
+ "neatlogs.llm.is_streaming": isStream,
6921
6923
  "input.value": safeStringify3(opts?.input ?? "")
6922
6924
  }
6923
6925
  }, getNeatlogsParentContext());
6926
+ setInvocationParams(span2, opts);
6924
6927
  const promise = withNeatlogsSpan(span2, () => original(opts, ...rest));
6925
6928
  return promise.then(
6926
6929
  (response) => {
6927
- if (response?.output_text) {
6928
- span2.setAttribute("neatlogs.llm.output_messages.0.role", "assistant");
6929
- span2.setAttribute("neatlogs.llm.output_messages.0.content", response.output_text);
6930
- }
6931
- if (response?.model) span2.setAttribute("neatlogs.llm.model_name", response.model);
6932
- if (response?.usage) {
6933
- if (response.usage.input_tokens != null) span2.setAttribute("neatlogs.llm.token_count.prompt", response.usage.input_tokens);
6934
- if (response.usage.output_tokens != null) span2.setAttribute("neatlogs.llm.token_count.completion", response.usage.output_tokens);
6930
+ if (isStream) {
6931
+ return wrapResponsesAsyncIterableStream(response, span2);
6935
6932
  }
6936
- span2.setStatus({ code: import_api10.SpanStatusCode.OK });
6937
- span2.end();
6933
+ finalizeResponsesResponse(span2, response);
6938
6934
  return response;
6939
6935
  },
6940
6936
  (err) => {
@@ -6944,6 +6940,110 @@ function tracedResponsesCreate(original) {
6944
6940
  );
6945
6941
  };
6946
6942
  }
6943
+ function wrapResponsesAsyncIterableStream(stream, span2) {
6944
+ const originalAsyncIterator = stream?.[Symbol.asyncIterator]?.bind(stream);
6945
+ if (!originalAsyncIterator) {
6946
+ finalizeResponsesResponse(span2, stream);
6947
+ return stream;
6948
+ }
6949
+ const textParts = [];
6950
+ let completedResponse;
6951
+ let finalized = false;
6952
+ const finalize = () => {
6953
+ if (finalized) return;
6954
+ finalized = true;
6955
+ finalizeResponsesResponse(span2, completedResponse, textParts.join(""));
6956
+ };
6957
+ return new Proxy(stream, {
6958
+ get(target, prop) {
6959
+ if (prop === Symbol.asyncIterator) {
6960
+ return () => {
6961
+ const iterator = originalAsyncIterator();
6962
+ return {
6963
+ async next() {
6964
+ try {
6965
+ const result = await iterator.next();
6966
+ if (result.done) {
6967
+ finalize();
6968
+ return result;
6969
+ }
6970
+ const event = result.value;
6971
+ if (event?.type === "response.output_text.delta" && typeof event.delta === "string") {
6972
+ textParts.push(event.delta);
6973
+ } else if (event?.type === "response.completed") {
6974
+ completedResponse = event.response;
6975
+ }
6976
+ return result;
6977
+ } catch (err) {
6978
+ if (!finalized) {
6979
+ finalized = true;
6980
+ recordError(span2, err);
6981
+ }
6982
+ throw err;
6983
+ }
6984
+ },
6985
+ async return(value2) {
6986
+ try {
6987
+ return await (iterator.return?.(value2) ?? { done: true, value: value2 });
6988
+ } finally {
6989
+ finalize();
6990
+ }
6991
+ },
6992
+ async throw(err) {
6993
+ if (!finalized) {
6994
+ finalized = true;
6995
+ recordError(span2, err);
6996
+ }
6997
+ if (iterator.throw) return iterator.throw(err);
6998
+ throw err;
6999
+ }
7000
+ };
7001
+ };
7002
+ }
7003
+ const value = Reflect.get(target, prop, target);
7004
+ return typeof value === "function" ? value.bind(target) : value;
7005
+ }
7006
+ });
7007
+ }
7008
+ function finalizeResponsesResponse(span2, response, streamedText = "") {
7009
+ const text = response?.output_text || streamedText || extractResponsesOutputText(response?.output);
7010
+ if (text) {
7011
+ span2.setAttribute("neatlogs.llm.output_messages.0.role", "assistant");
7012
+ span2.setAttribute("neatlogs.llm.output_messages.0.content", text);
7013
+ span2.setAttribute("output.value", text);
7014
+ } else if (response?.output) {
7015
+ span2.setAttribute("output.value", safeStringify3(response.output));
7016
+ }
7017
+ if (response?.model) span2.setAttribute("neatlogs.llm.model_name", response.model);
7018
+ if (response?.status) span2.setAttribute("neatlogs.llm.finish_reason", response.status);
7019
+ const usage = response?.usage;
7020
+ if (usage) {
7021
+ if (usage.input_tokens != null) span2.setAttribute("neatlogs.llm.token_count.prompt", usage.input_tokens);
7022
+ if (usage.output_tokens != null) span2.setAttribute("neatlogs.llm.token_count.completion", usage.output_tokens);
7023
+ if (usage.total_tokens != null) span2.setAttribute("neatlogs.llm.token_count.total", usage.total_tokens);
7024
+ if (usage.input_tokens_details?.cached_tokens != null) {
7025
+ span2.setAttribute("neatlogs.llm.token_count.cache_read", usage.input_tokens_details.cached_tokens);
7026
+ }
7027
+ if (usage.output_tokens_details?.reasoning_tokens != null) {
7028
+ span2.setAttribute("neatlogs.llm.token_count.reasoning", usage.output_tokens_details.reasoning_tokens);
7029
+ }
7030
+ }
7031
+ span2.setStatus({ code: import_api10.SpanStatusCode.OK });
7032
+ span2.end();
7033
+ }
7034
+ function extractResponsesOutputText(output) {
7035
+ if (!Array.isArray(output)) return "";
7036
+ const parts = [];
7037
+ for (const item of output) {
7038
+ if (item?.type !== "message" || !Array.isArray(item.content)) continue;
7039
+ for (const content of item.content) {
7040
+ if (content?.type === "output_text" && typeof content.text === "string") {
7041
+ parts.push(content.text);
7042
+ }
7043
+ }
7044
+ }
7045
+ return parts.join("");
7046
+ }
6947
7047
  function wrapAsyncIterableStream(stream, span2) {
6948
7048
  const chunks = [];
6949
7049
  const originalAsyncIterator = stream[Symbol.asyncIterator]?.bind(stream);