@juspay/neurolink 9.70.6 → 9.71.0

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.
@@ -49,6 +49,73 @@ function parseOrRepair(candidate) {
49
49
  return undefined;
50
50
  }
51
51
  }
52
+ /** Bounds the recursive nested-string unwrap against pathological inputs. */
53
+ const MAX_NESTED_UNWRAP_DEPTH = 6;
54
+ /**
55
+ * Recursively replace any string-valued field whose content is itself a JSON
56
+ * object/array with the parsed value. Models sometimes double-encode a NESTED
57
+ * field — e.g. `{ "attachment": "{\"k\":1}" }` instead of
58
+ * `{ "attachment": { "k": 1 } }` — which fails schema validation even though the
59
+ * intended object is right there. (`coerceJsonToSchema` already unwraps a
60
+ * stringified TOP-LEVEL object; this handles the nested case.)
61
+ *
62
+ * A parsed string is NOT re-descended into: its own string fields (e.g. an
63
+ * attachment's `content`) are the model's intended values and must be left
64
+ * alone. Recursion only walks already-structural objects/arrays to find
65
+ * stringified fields anywhere in the tree. Returns a NEW value (never mutates
66
+ * the input) plus whether anything changed, so the caller can skip a redundant
67
+ * re-validation when nothing was unwrapped. Callers MUST re-validate the result
68
+ * against the schema — that gate is what keeps an over-eager unwrap (a field
69
+ * that should stay a string) from being accepted.
70
+ */
71
+ function deepUnwrapJsonStrings(value, depth = 0) {
72
+ if (depth > MAX_NESTED_UNWRAP_DEPTH) {
73
+ return { value, changed: false };
74
+ }
75
+ if (typeof value === "string") {
76
+ const s = value.trim();
77
+ const looksJson = (s.startsWith("{") && s.endsWith("}")) ||
78
+ (s.startsWith("[") && s.endsWith("]"));
79
+ if (looksJson) {
80
+ try {
81
+ const parsed = JSON.parse(s);
82
+ if (parsed !== null && typeof parsed === "object") {
83
+ // Parsed one stringified layer. Do NOT descend into `parsed` — its
84
+ // own string fields are intended values, not double-encodings.
85
+ return { value: parsed, changed: true };
86
+ }
87
+ }
88
+ catch {
89
+ // not JSON — leave the string as-is
90
+ }
91
+ }
92
+ return { value, changed: false };
93
+ }
94
+ if (Array.isArray(value)) {
95
+ let changed = false;
96
+ const out = value.map((item) => {
97
+ const r = deepUnwrapJsonStrings(item, depth + 1);
98
+ if (r.changed) {
99
+ changed = true;
100
+ }
101
+ return r.value;
102
+ });
103
+ return { value: changed ? out : value, changed };
104
+ }
105
+ if (value !== null && typeof value === "object") {
106
+ let changed = false;
107
+ const out = {};
108
+ for (const [k, v] of Object.entries(value)) {
109
+ const r = deepUnwrapJsonStrings(v, depth + 1);
110
+ if (r.changed) {
111
+ changed = true;
112
+ }
113
+ out[k] = r.value;
114
+ }
115
+ return { value: changed ? out : value, changed };
116
+ }
117
+ return { value, changed: false };
118
+ }
52
119
  /**
53
120
  * Try to produce canonical JSON from `text`. Returns null when no JSON object
54
121
  * could be recovered (caller should then keep the raw text).
@@ -147,6 +214,24 @@ export function coerceJsonToSchema(text, schema) {
147
214
  if (safeParseable.safeParse(outcome.value).success) {
148
215
  schemaValid.push(record);
149
216
  }
217
+ else {
218
+ // The model may have double-encoded a NESTED field as a JSON string
219
+ // (e.g. `{"attachment":"{...}"}` instead of `{"attachment":{...}}`),
220
+ // which fails validation even though the intended object is present.
221
+ // Unwrap stringified object/array fields and re-validate before giving
222
+ // up — the safeParse gate rejects any over-eager unwrap.
223
+ const unwrapped = deepUnwrapJsonStrings(outcome.value);
224
+ if (unwrapped.changed &&
225
+ unwrapped.value !== null &&
226
+ typeof unwrapped.value === "object" &&
227
+ safeParseable.safeParse(unwrapped.value).success) {
228
+ schemaValid.push({
229
+ value: unwrapped.value,
230
+ repaired: true,
231
+ truncated: candidate.truncated,
232
+ });
233
+ }
234
+ }
150
235
  }
151
236
  // Among schema-valid candidates prefer the MOST COMPLETE one. With nullable
152
237
  // fields a lean object (e.g. `{summary, attachment: null}`) validates
@@ -8,3 +8,19 @@
8
8
  * must happen here and propagate to all three surfaces.
9
9
  */
10
10
  export declare function extractMcpErrorText(raw: unknown): string;
11
+ /**
12
+ * MCP tools signal failure by RETURNING `{ isError: true, ... }`, not throwing,
13
+ * so execute()'s try/catch never sees it. Returns a capped status message for
14
+ * failures (undefined for success) for the caller to set the span error level.
15
+ *
16
+ * Generic over input shape: accepts either a result object or a JSON-stringified
17
+ * envelope (different providers hand back different shapes), mirroring
18
+ * `extractMcpErrorText`. A non-JSON string has no `isError` field, so it is
19
+ * correctly treated as "not an error" (→ undefined).
20
+ *
21
+ * Layered on `extractMcpErrorText`: this adds the `isError === true` gate and
22
+ * the human-readable "MCP tool returned isError: …" prefix, while the shared
23
+ * helper owns the content parsing and the 500-char cap. When `isError` is set
24
+ * but no readable text is present, falls back to a generic message.
25
+ */
26
+ export declare function extractMcpToolErrorMessage(result: unknown): string | undefined;
@@ -33,3 +33,39 @@ export function extractMcpErrorText(raw) {
33
33
  .map((c) => c.text);
34
34
  return texts.join(" ").substring(0, 500);
35
35
  }
36
+ /**
37
+ * MCP tools signal failure by RETURNING `{ isError: true, ... }`, not throwing,
38
+ * so execute()'s try/catch never sees it. Returns a capped status message for
39
+ * failures (undefined for success) for the caller to set the span error level.
40
+ *
41
+ * Generic over input shape: accepts either a result object or a JSON-stringified
42
+ * envelope (different providers hand back different shapes), mirroring
43
+ * `extractMcpErrorText`. A non-JSON string has no `isError` field, so it is
44
+ * correctly treated as "not an error" (→ undefined).
45
+ *
46
+ * Layered on `extractMcpErrorText`: this adds the `isError === true` gate and
47
+ * the human-readable "MCP tool returned isError: …" prefix, while the shared
48
+ * helper owns the content parsing and the 500-char cap. When `isError` is set
49
+ * but no readable text is present, falls back to a generic message.
50
+ */
51
+ export function extractMcpToolErrorMessage(result) {
52
+ let resultObj = result;
53
+ if (typeof resultObj === "string") {
54
+ try {
55
+ resultObj = JSON.parse(resultObj);
56
+ }
57
+ catch {
58
+ return undefined;
59
+ }
60
+ }
61
+ if (!resultObj || typeof resultObj !== "object") {
62
+ return undefined;
63
+ }
64
+ if (resultObj.isError !== true) {
65
+ return undefined;
66
+ }
67
+ const text = extractMcpErrorText(resultObj);
68
+ return text
69
+ ? `MCP tool returned isError: ${text}`
70
+ : "MCP tool returned isError: true";
71
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juspay/neurolink",
3
- "version": "9.70.6",
3
+ "version": "9.71.0",
4
4
  "packageManager": "pnpm@10.15.1",
5
5
  "description": "Universal AI Development Platform with working MCP integration, multi-provider support, voice (TTS/STT/realtime), and professional CLI. 58+ external MCP servers discoverable, multimodal file processing, RAG pipelines. Build, test, and deploy AI applications with 21+ providers: OpenAI, Anthropic, Google AI Studio, Google Vertex, AWS Bedrock, Azure OpenAI, Mistral, LiteLLM, SageMaker, Hugging Face, Ollama, OpenAI-compatible, OpenRouter, DeepSeek, NVIDIA NIM, LM Studio, llama.cpp, plus voice (OpenAI TTS, ElevenLabs, Deepgram, Azure Speech).",
6
6
  "author": {
@@ -104,6 +104,7 @@
104
104
  "test:log-sanitize": "npx tsx test/continuous-test-suite-log-sanitize.ts",
105
105
  "test:sse-client": "npx tsx test/continuous-test-suite-sse-client.ts",
106
106
  "test:stream-span": "npx tsx test/continuous-test-suite-stream-span.ts",
107
+ "test:vertex-langfuse-spans": "npx tsx test/continuous-test-suite-vertex-langfuse-spans.ts",
107
108
  "test:credentials": "npx tsx test/continuous-test-suite-credentials.ts",
108
109
  "test:dynamic": "npx tsx test/continuous-test-suite-dynamic.ts",
109
110
  "test:proxy": "npx tsx test/continuous-test-suite-proxy.ts",
@@ -418,7 +419,7 @@
418
419
  "@changesets/changelog-github": "^0.6.0",
419
420
  "@changesets/cli": "^2.29.8",
420
421
  "@eslint/js": "^10.0.1",
421
- "@juspay/hippocampus": "^0.1.6",
422
+ "@juspay/hippocampus": ">=0.1.7",
422
423
  "@opentelemetry/api": "^1.9.0",
423
424
  "@opentelemetry/sdk-trace-base": "^2.6.0",
424
425
  "@opentelemetry/sdk-trace-node": "^2.6.0",