@juspay/neurolink 13.12.5 → 13.12.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.
@@ -104,8 +104,9 @@ const resolveAgainstSchema = (text, schema) => {
104
104
  * The one place a finish reason becomes the unified spelling. Accepts the wire's
105
105
  * `finish_reason` (`tool_calls`, `content_filter`) and the unified spelling a
106
106
  * middleware's own finish part already carries (`tool-calls`, `content-filter`),
107
- * since both reach the stream path. Anything else, including `stop`, `error` and
108
- * an absent value, reads as `stop`.
107
+ * since both reach the stream path. Anything else, including `stop` and an
108
+ * absent value, reads as `stop`. A wire `error` finish never gets here: the SSE
109
+ * parser throws on it, and on a stream that ended with no terminal marker.
109
110
  */
110
111
  const toUnifiedFinishReason = (reason) => {
111
112
  switch (reason) {
@@ -14,6 +14,8 @@
14
14
  * module is to be the native replacement for the AI SDK's OpenAI wrapper.
15
15
  */
16
16
  import { createParser } from "eventsource-parser";
17
+ import { ErrorCategory, ErrorSeverity } from "../constants/enums.js";
18
+ import { NeuroLinkError } from "../utils/errorHandling.js";
17
19
  import { sanitizeToolName } from "../mcp/toolConverter.js";
18
20
  import { resolveSamplingParams } from "../models/modelRegistry.js";
19
21
  import { convertZodToJsonSchema, normalizeWireToolSchema, } from "../utils/schemaConversion.js";
@@ -725,13 +727,24 @@ export const parseSSEStream = async (body, onTextDelta, onReasoningDelta) => {
725
727
  };
726
728
  const decoder = new TextDecoder();
727
729
  let parseErr;
730
+ // An upstream error frame or `finish_reason: "error"`.
731
+ let streamErr;
732
+ // True once the upstream said it was done: `[DONE]` or any finish_reason.
733
+ let sawTerminal = false;
728
734
  // Key of the tool call the previous delta wrote to (for unindexed deltas).
729
735
  let lastToolCallKey;
730
736
  let nextToolCallKey = 0;
731
737
  const toolCallKeysById = new Map();
732
738
  const handleEvent = (msg) => {
739
+ if (streamErr) {
740
+ return;
741
+ }
733
742
  const data = msg.data;
734
- if (!data || data === "[DONE]") {
743
+ if (data === "[DONE]") {
744
+ sawTerminal = true;
745
+ return;
746
+ }
747
+ if (!data) {
735
748
  return;
736
749
  }
737
750
  let chunk;
@@ -742,6 +755,13 @@ export const parseSSEStream = async (body, onTextDelta, onReasoningDelta) => {
742
755
  parseErr = err instanceof Error ? err : new Error(String(err));
743
756
  return;
744
757
  }
758
+ if (chunk.error) {
759
+ streamErr ??= streamFailure("OPENAI_COMPAT_STREAM_ERROR_FRAME", `Upstream sent an error frame mid-stream: ${String(chunk.error.message ?? JSON.stringify(chunk.error)).slice(0, 500)}`, {
760
+ upstreamError: JSON.stringify(chunk.error).slice(0, 500),
761
+ partialTextLength: result.text.length,
762
+ });
763
+ return;
764
+ }
745
765
  if (chunk.usage) {
746
766
  result.usage = chunk.usage;
747
767
  }
@@ -828,7 +848,12 @@ export const parseSSEStream = async (body, onTextDelta, onReasoningDelta) => {
828
848
  }
829
849
  }
830
850
  }
851
+ if (choice.finish_reason === "error") {
852
+ streamErr ??= streamFailure("OPENAI_COMPAT_STREAM_FINISH_ERROR", 'Upstream ended the stream with finish_reason "error".', { partialTextLength: result.text.length });
853
+ return;
854
+ }
831
855
  if (choice.finish_reason) {
856
+ sawTerminal = true;
832
857
  result.finishReason = choice.finish_reason;
833
858
  }
834
859
  };
@@ -841,6 +866,15 @@ export const parseSSEStream = async (body, onTextDelta, onReasoningDelta) => {
841
866
  break;
842
867
  }
843
868
  parser.feed(decoder.decode(value, { stream: true }));
869
+ if (streamErr) {
870
+ try {
871
+ await reader.cancel();
872
+ }
873
+ catch {
874
+ // Keep the original upstream error when reader cleanup fails.
875
+ }
876
+ break;
877
+ }
844
878
  }
845
879
  parser.feed(decoder.decode());
846
880
  }
@@ -850,8 +884,26 @@ export const parseSSEStream = async (body, onTextDelta, onReasoningDelta) => {
850
884
  if (parseErr) {
851
885
  throw parseErr;
852
886
  }
887
+ if (streamErr) {
888
+ throw streamErr;
889
+ }
890
+ // The body closed without `[DONE]` or a finish_reason: truncated stream.
891
+ // Not retriable (same as ANTHROPIC_STREAM_TRUNCATED).
892
+ if (!sawTerminal) {
893
+ throw streamFailure("OPENAI_COMPAT_STREAM_TRUNCATED", "Stream ended before [DONE] or a finish_reason; the response is incomplete.", { partialTextLength: result.text.length });
894
+ }
853
895
  return result;
854
896
  };
897
+ function streamFailure(code, message, context) {
898
+ return new NeuroLinkError({
899
+ code,
900
+ message,
901
+ category: ErrorCategory.NETWORK,
902
+ severity: ErrorSeverity.HIGH,
903
+ retriable: false,
904
+ context,
905
+ });
906
+ }
855
907
  function extractOpenAICompatErrorMessage(parsed) {
856
908
  const message = parsed?.error?.message;
857
909
  if (typeof message === "string" && message.trim().length > 0) {
@@ -144,7 +144,7 @@ export type OpenAICompatStreamDelta = {
144
144
  export type OpenAICompatStreamChunkChoice = {
145
145
  index: number;
146
146
  delta: OpenAICompatStreamDelta;
147
- finish_reason: "stop" | "length" | "tool_calls" | "function_call" | "content_filter" | null;
147
+ finish_reason: "stop" | "length" | "tool_calls" | "function_call" | "content_filter" | "error" | null;
148
148
  };
149
149
  export type OpenAICompatChatStreamChunk = {
150
150
  id?: string;
@@ -152,6 +152,11 @@ export type OpenAICompatChatStreamChunk = {
152
152
  created?: number;
153
153
  model?: string;
154
154
  choices: OpenAICompatStreamChunkChoice[];
155
+ error?: {
156
+ message?: string;
157
+ type?: string;
158
+ code?: string | number;
159
+ };
155
160
  usage?: OpenAICompatUsage;
156
161
  };
157
162
  export type OpenAICompatErrorMessage = string | ReadonlyArray<{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juspay/neurolink",
3
- "version": "13.12.5",
3
+ "version": "13.12.6",
4
4
  "packageManager": "pnpm@10.34.9+sha512.f49b5a8ea0fdedb44bb3c0c1d6575092a53c2950d95b7ad15df11f17974877a2de3a01c07e934e9b28ce716e51c09a94962c7306d09be2e1e4b18a8c8762fd32",
5
5
  "description": "The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002/call) for routing, tool selection and context budgeting. MCP-native (4 transports), voice TTS/STT/realtime, RAG, agents, memory, compaction, 9 observability exporters. OpenAI · Anthropic · Gemini · Bedrock · Azure · Ollama · TypeSafe Jev and more.",
6
6
  "author": {
@@ -108,6 +108,8 @@
108
108
  "test:multimodal-text-content-fold": "pnpm exec tsx test/continuous-test-suite-multimodal-text-content-fold.ts",
109
109
  "test:proxy-sharing": "pnpm run build && pnpm exec tsx test/continuous-test-suite-proxy-sharing.ts",
110
110
  "test:openai-compat-streaming-retry": "pnpm exec tsx test/continuous-test-suite-openai-compat-streaming-retry.ts",
111
+ "test:openai-compat-stream-truncation": "pnpm exec tsx test/continuous-test-suite-openai-compat-stream-truncation.ts",
112
+ "test:openai-compat-stream-cancel": "pnpm exec tsx test/continuous-test-suite-openai-compat-stream-cancel.ts",
111
113
  "test:anthropic-streaming-retry": "pnpm exec tsx test/continuous-test-suite-anthropic-streaming-retry.ts",
112
114
  "test:anthropic-silent-truncation": "pnpm exec tsx test/continuous-test-suite-anthropic-silent-truncation.ts",
113
115
  "test:anthropic-thinking-disabled": "pnpm exec tsx test/continuous-test-suite-anthropic-thinking-disabled.ts",