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.d.ts CHANGED
@@ -656,6 +656,6 @@ declare function registerCrewaiTask(task: {
656
656
  * SDK version. Kept in sync with package.json by the `version:sync` script
657
657
  * (runs automatically on `prebuild`). Do not edit by hand — bump package.json.
658
658
  */
659
- declare const __version__ = "1.1.13";
659
+ declare const __version__ = "1.1.14";
660
660
 
661
661
  export { type CachedPrompt, type InitOptions, type MaskFunction, PromptApiError, PromptClient, PromptClientError, PromptHandle, type PromptMessage, PromptNotFoundError, PromptTemplate, Span, type SpanKind, type SpanOptions, type TraceContextCarrier, type TraceOptions, UserPromptTemplate, __version__, bindTemplates, createPrompt, deletePrompt, fetchPrompt, flush, getMastraObservability, getPrompt, getSessionConfig, identify, init, injectTraceContext, isDebugEnabled, listPrompts, log, registerCrewaiTask, removeTag, saveAsVersion, setTraceOutput, shutdown, span, trace, updatePrompt };
package/dist/index.mjs CHANGED
@@ -5946,7 +5946,7 @@ function _resetMastraCache() {
5946
5946
  }
5947
5947
 
5948
5948
  // src/version.ts
5949
- var __version__ = "1.1.13";
5949
+ var __version__ = "1.1.14";
5950
5950
 
5951
5951
  // src/init.ts
5952
5952
  var logger13 = getLogger();
@@ -6837,29 +6837,25 @@ function tracedResponsesCreate(original) {
6837
6837
  return function(opts, ...rest) {
6838
6838
  const tracer = getProviderTracer(TRACER_NAME3);
6839
6839
  const model = opts?.model ?? "";
6840
+ const isStream = opts?.stream === true;
6840
6841
  const span2 = tracer.startSpan("openai.responses.create", {
6841
6842
  attributes: {
6842
6843
  "neatlogs.span.kind": "LLM",
6843
6844
  "neatlogs.llm.provider": "openai",
6844
6845
  "neatlogs.llm.system": "openai",
6845
6846
  "neatlogs.llm.model_name": model,
6847
+ "neatlogs.llm.is_streaming": isStream,
6846
6848
  "input.value": safeStringify3(opts?.input ?? "")
6847
6849
  }
6848
6850
  }, getNeatlogsParentContext());
6851
+ setInvocationParams(span2, opts);
6849
6852
  const promise = withNeatlogsSpan(span2, () => original(opts, ...rest));
6850
6853
  return promise.then(
6851
6854
  (response) => {
6852
- if (response?.output_text) {
6853
- span2.setAttribute("neatlogs.llm.output_messages.0.role", "assistant");
6854
- span2.setAttribute("neatlogs.llm.output_messages.0.content", response.output_text);
6855
- }
6856
- if (response?.model) span2.setAttribute("neatlogs.llm.model_name", response.model);
6857
- if (response?.usage) {
6858
- if (response.usage.input_tokens != null) span2.setAttribute("neatlogs.llm.token_count.prompt", response.usage.input_tokens);
6859
- if (response.usage.output_tokens != null) span2.setAttribute("neatlogs.llm.token_count.completion", response.usage.output_tokens);
6855
+ if (isStream) {
6856
+ return wrapResponsesAsyncIterableStream(response, span2);
6860
6857
  }
6861
- span2.setStatus({ code: SpanStatusCode5.OK });
6862
- span2.end();
6858
+ finalizeResponsesResponse(span2, response);
6863
6859
  return response;
6864
6860
  },
6865
6861
  (err) => {
@@ -6869,6 +6865,110 @@ function tracedResponsesCreate(original) {
6869
6865
  );
6870
6866
  };
6871
6867
  }
6868
+ function wrapResponsesAsyncIterableStream(stream, span2) {
6869
+ const originalAsyncIterator = stream?.[Symbol.asyncIterator]?.bind(stream);
6870
+ if (!originalAsyncIterator) {
6871
+ finalizeResponsesResponse(span2, stream);
6872
+ return stream;
6873
+ }
6874
+ const textParts = [];
6875
+ let completedResponse;
6876
+ let finalized = false;
6877
+ const finalize = () => {
6878
+ if (finalized) return;
6879
+ finalized = true;
6880
+ finalizeResponsesResponse(span2, completedResponse, textParts.join(""));
6881
+ };
6882
+ return new Proxy(stream, {
6883
+ get(target, prop) {
6884
+ if (prop === Symbol.asyncIterator) {
6885
+ return () => {
6886
+ const iterator = originalAsyncIterator();
6887
+ return {
6888
+ async next() {
6889
+ try {
6890
+ const result = await iterator.next();
6891
+ if (result.done) {
6892
+ finalize();
6893
+ return result;
6894
+ }
6895
+ const event = result.value;
6896
+ if (event?.type === "response.output_text.delta" && typeof event.delta === "string") {
6897
+ textParts.push(event.delta);
6898
+ } else if (event?.type === "response.completed") {
6899
+ completedResponse = event.response;
6900
+ }
6901
+ return result;
6902
+ } catch (err) {
6903
+ if (!finalized) {
6904
+ finalized = true;
6905
+ recordError(span2, err);
6906
+ }
6907
+ throw err;
6908
+ }
6909
+ },
6910
+ async return(value2) {
6911
+ try {
6912
+ return await (iterator.return?.(value2) ?? { done: true, value: value2 });
6913
+ } finally {
6914
+ finalize();
6915
+ }
6916
+ },
6917
+ async throw(err) {
6918
+ if (!finalized) {
6919
+ finalized = true;
6920
+ recordError(span2, err);
6921
+ }
6922
+ if (iterator.throw) return iterator.throw(err);
6923
+ throw err;
6924
+ }
6925
+ };
6926
+ };
6927
+ }
6928
+ const value = Reflect.get(target, prop, target);
6929
+ return typeof value === "function" ? value.bind(target) : value;
6930
+ }
6931
+ });
6932
+ }
6933
+ function finalizeResponsesResponse(span2, response, streamedText = "") {
6934
+ const text = response?.output_text || streamedText || extractResponsesOutputText(response?.output);
6935
+ if (text) {
6936
+ span2.setAttribute("neatlogs.llm.output_messages.0.role", "assistant");
6937
+ span2.setAttribute("neatlogs.llm.output_messages.0.content", text);
6938
+ span2.setAttribute("output.value", text);
6939
+ } else if (response?.output) {
6940
+ span2.setAttribute("output.value", safeStringify3(response.output));
6941
+ }
6942
+ if (response?.model) span2.setAttribute("neatlogs.llm.model_name", response.model);
6943
+ if (response?.status) span2.setAttribute("neatlogs.llm.finish_reason", response.status);
6944
+ const usage = response?.usage;
6945
+ if (usage) {
6946
+ if (usage.input_tokens != null) span2.setAttribute("neatlogs.llm.token_count.prompt", usage.input_tokens);
6947
+ if (usage.output_tokens != null) span2.setAttribute("neatlogs.llm.token_count.completion", usage.output_tokens);
6948
+ if (usage.total_tokens != null) span2.setAttribute("neatlogs.llm.token_count.total", usage.total_tokens);
6949
+ if (usage.input_tokens_details?.cached_tokens != null) {
6950
+ span2.setAttribute("neatlogs.llm.token_count.cache_read", usage.input_tokens_details.cached_tokens);
6951
+ }
6952
+ if (usage.output_tokens_details?.reasoning_tokens != null) {
6953
+ span2.setAttribute("neatlogs.llm.token_count.reasoning", usage.output_tokens_details.reasoning_tokens);
6954
+ }
6955
+ }
6956
+ span2.setStatus({ code: SpanStatusCode5.OK });
6957
+ span2.end();
6958
+ }
6959
+ function extractResponsesOutputText(output) {
6960
+ if (!Array.isArray(output)) return "";
6961
+ const parts = [];
6962
+ for (const item of output) {
6963
+ if (item?.type !== "message" || !Array.isArray(item.content)) continue;
6964
+ for (const content of item.content) {
6965
+ if (content?.type === "output_text" && typeof content.text === "string") {
6966
+ parts.push(content.text);
6967
+ }
6968
+ }
6969
+ }
6970
+ return parts.join("");
6971
+ }
6872
6972
  function wrapAsyncIterableStream(stream, span2) {
6873
6973
  const chunks = [];
6874
6974
  const originalAsyncIterator = stream[Symbol.asyncIterator]?.bind(stream);