@juspay/neurolink 9.80.0 → 9.80.1

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.
@@ -1,8 +1,10 @@
1
1
  import type { ZodType } from "zod";
2
2
  import { AIProviderName } from "../constants/enums.js";
3
3
  import { BaseProvider } from "../core/baseProvider.js";
4
- import type { EnhancedGenerateResult, TextGenerationOptions, StreamOptions, StreamResult } from "../types/index.js";
4
+ import type { EnhancedGenerateResult, TextGenerationOptions, StreamOptions, StreamResult, VertexAnthropicMessage, ChatMessage, MinimalChatMessage } from "../types/index.js";
5
5
  import type { Schema, LanguageModel } from "../types/index.js";
6
+ export declare function buildAnthropicHistoryMessages(conversationMessages: Array<ChatMessage | MinimalChatMessage>): VertexAnthropicMessage[];
7
+ export declare function appendUserMessage(messages: VertexAnthropicMessage[], content: VertexAnthropicMessage["content"]): void;
6
8
  /**
7
9
  * Recursively strip JSON-schema fields that Vertex Gemini's function-call AND
8
10
  * `responseSchema` (structured output) validators reject with 400
@@ -44,6 +44,144 @@ const hasAnthropicSupport = () => {
44
44
  // Actual availability is checked at runtime when creating the client
45
45
  return true;
46
46
  };
47
+ // Parse stored rows into ordered segments, grouping tool_call/tool_result by (turn, step).
48
+ function collectHistorySegments(conversationMessages) {
49
+ const segments = [];
50
+ const stepMap = new Map();
51
+ let turnCounter = 0;
52
+ const getStep = (stepIndex) => {
53
+ const key = `${turnCounter}:${stepIndex ?? "x"}`;
54
+ let step = stepMap.get(key);
55
+ if (!step) {
56
+ step = { type: "tool_step", callParts: [], resultParts: [] };
57
+ stepMap.set(key, step);
58
+ segments.push(step);
59
+ }
60
+ return step;
61
+ };
62
+ for (const msg of conversationMessages) {
63
+ if (msg.role === "tool_call") {
64
+ getStep(msg.metadata?.stepIndex).callParts.push({
65
+ name: msg.tool || "unknown",
66
+ input: msg.args || {},
67
+ });
68
+ continue;
69
+ }
70
+ if (msg.role === "tool_result") {
71
+ getStep(msg.metadata?.stepIndex).resultParts.push(msg.content && msg.content.length > 0 ? msg.content : "(no output)");
72
+ continue;
73
+ }
74
+ const role = msg.role === "assistant"
75
+ ? "assistant"
76
+ : msg.role === "user"
77
+ ? "user"
78
+ : null;
79
+ if (!role || !msg.content || msg.content.trim().length === 0) {
80
+ continue;
81
+ }
82
+ // New turn → fresh step namespace for any following tool rows.
83
+ turnCounter++;
84
+ segments.push({ type: "regular", role, parts: [msg.content] });
85
+ }
86
+ return segments;
87
+ }
88
+ // Append blocks to the trailing turn if it shares the role, else start a new turn.
89
+ function pushTurn(messages, role, blocks) {
90
+ const last = messages[messages.length - 1];
91
+ if (last && last.role === role && Array.isArray(last.content)) {
92
+ last.content.push(...blocks);
93
+ }
94
+ else {
95
+ messages.push({ role, content: [...blocks] });
96
+ }
97
+ }
98
+ // Pair a tool step's calls/results by position; unbalanced extras are dropped
99
+ // because Anthropic 400s on an orphaned tool_use/tool_result reference.
100
+ function pairToolStep(step, ordinal) {
101
+ const calls = step.callParts;
102
+ const resultStrings = step.resultParts;
103
+ const paired = Math.min(calls.length, resultStrings.length);
104
+ if (paired !== calls.length || paired !== resultStrings.length) {
105
+ logger.debug("[GoogleVertex] Unbalanced tool step in Claude history replay", {
106
+ calls: calls.length,
107
+ results: resultStrings.length,
108
+ paired,
109
+ });
110
+ }
111
+ const uses = [];
112
+ const results = [];
113
+ for (let i = 0; i < paired; i++) {
114
+ const id = `hist_${ordinal}_${i}`;
115
+ uses.push({
116
+ type: "tool_use",
117
+ id,
118
+ name: calls[i].name,
119
+ input: calls[i].input,
120
+ });
121
+ results.push({
122
+ type: "tool_result",
123
+ tool_use_id: id,
124
+ content: resultStrings[i],
125
+ });
126
+ }
127
+ return { uses, results };
128
+ }
129
+ // Drop leading turns until the first is a real user turn — Anthropic requires it
130
+ // (covers a leading assistant/tool step and an orphaned tool_result-only turn).
131
+ function trimLeadingNonUser(messages) {
132
+ const isToolResultOnly = (m) => Array.isArray(m.content) &&
133
+ m.content.length > 0 &&
134
+ m.content.every((block) => block.type === "tool_result");
135
+ while (messages.length > 0 &&
136
+ (messages[0].role !== "user" || isToolResultOnly(messages[0]))) {
137
+ messages.shift();
138
+ }
139
+ }
140
+ // Rebuild Anthropic history with paired tool_use/tool_result turns from stored rows.
141
+ export function buildAnthropicHistoryMessages(conversationMessages) {
142
+ const messages = [];
143
+ let stepOrdinal = 0;
144
+ for (const seg of collectHistorySegments(conversationMessages)) {
145
+ if (seg.type === "regular") {
146
+ pushTurn(messages, seg.role === "assistant" ? "assistant" : "user", [
147
+ { type: "text", text: String(seg.parts[0] ?? "") },
148
+ ]);
149
+ continue;
150
+ }
151
+ const { uses, results } = pairToolStep(seg, stepOrdinal);
152
+ if (uses.length === 0) {
153
+ continue;
154
+ }
155
+ stepOrdinal++;
156
+ pushTurn(messages, "assistant", uses);
157
+ pushTurn(messages, "user", results);
158
+ }
159
+ trimLeadingNonUser(messages);
160
+ if (logger.shouldLog("debug")) {
161
+ const blocks = messages.flatMap((m) => Array.isArray(m.content) ? m.content : []);
162
+ logger.debug("[GoogleVertex] Replayed Claude history with tool turns", {
163
+ historyMessages: messages.length,
164
+ toolUseBlocks: blocks.filter((b) => b.type === "tool_use").length,
165
+ toolResultBlocks: blocks.filter((b) => b.type === "tool_result").length,
166
+ });
167
+ }
168
+ return messages;
169
+ }
170
+ // Append the live user turn, merging into a trailing user turn to avoid back-to-back user messages.
171
+ export function appendUserMessage(messages, content) {
172
+ const last = messages[messages.length - 1];
173
+ if (last && last.role === "user") {
174
+ const head = Array.isArray(last.content)
175
+ ? last.content
176
+ : [{ type: "text", text: last.content }];
177
+ const tail = Array.isArray(content)
178
+ ? content
179
+ : [{ type: "text", text: content }];
180
+ last.content = [...head, ...tail];
181
+ return;
182
+ }
183
+ messages.push({ role: "user", content });
184
+ }
47
185
  /**
48
186
  * Recursively strip JSON-schema fields that Vertex Gemini's function-call AND
49
187
  * `responseSchema` (structured output) validators reject with 400
@@ -2287,18 +2425,14 @@ export class GoogleVertexProvider extends BaseProvider {
2287
2425
  });
2288
2426
  // Build messages from input
2289
2427
  const messages = [];
2290
- // Add conversation history if present.
2291
- //
2292
- // Intentionally text-only. Anthropic's API rejects messages where a
2293
- // tool_use_id reference appears without its matching tool_use in the
2294
- // same turn — so synthesising tool_use / tool_result blocks from
2295
- // stored ChatMessages risks emitting orphaned references that fail
2296
- // validation. Tool rows are still persisted to Redis (chat-history
2297
- // UI renders them) but they don't re-enter the model's context on
2298
- // subsequent turns.
2428
+ // Replay conversationMessages (with tool turns), else the legacy text-only conversationHistory.
2299
2429
  if (options.conversationMessages &&
2300
2430
  options.conversationMessages.length > 0) {
2301
- for (const msg of options.conversationMessages) {
2431
+ messages.push(...buildAnthropicHistoryMessages(options.conversationMessages));
2432
+ }
2433
+ else if (options.conversationHistory &&
2434
+ options.conversationHistory.length > 0) {
2435
+ for (const msg of options.conversationHistory) {
2302
2436
  if (msg.role === "user" || msg.role === "assistant") {
2303
2437
  messages.push({
2304
2438
  role: msg.role,
@@ -2430,13 +2564,10 @@ export class GoogleVertexProvider extends BaseProvider {
2430
2564
  type: "text",
2431
2565
  text: multimodalInput.text,
2432
2566
  });
2433
- // Add the user message with appropriate content format
2434
- messages.push({
2435
- role: "user",
2436
- content: userContentParts.length === 1 && userContentParts[0].type === "text"
2437
- ? multimodalInput.text
2438
- : userContentParts,
2439
- });
2567
+ // Append the live user turn (merges into a trailing user turn if present).
2568
+ appendUserMessage(messages, userContentParts.length === 1 && userContentParts[0].type === "text"
2569
+ ? multimodalInput.text
2570
+ : userContentParts);
2440
2571
  // Convert tools to Anthropic format if present
2441
2572
  let tools;
2442
2573
  const executeMap = new Map();
@@ -3056,20 +3187,14 @@ export class GoogleVertexProvider extends BaseProvider {
3056
3187
  // Build messages from input
3057
3188
  const messages = [];
3058
3189
  const inputText = options.prompt || options.input?.text || "Please respond.";
3059
- // Add conversation history if present. Prefer `conversationMessages`
3060
- // (what NeuroLink core injects today via MessageBuilder) and fall back
3061
- // to the legacy `conversationHistory` field for callers that still use
3062
- // the older surface. The Vertex Claude STREAM path already follows this
3063
- // priority — keeping the GENERATE path on `conversationHistory` only
3064
- // would silently drop multi-turn context for memory/loop sessions.
3065
- // Intentionally text-only: see the stream sibling for the rationale —
3066
- // synthesising tool_use / tool_result blocks from stored ChatMessages
3067
- // risks emitting orphaned references that Anthropic's API rejects.
3068
- const historyMessages = options.conversationMessages && options.conversationMessages.length > 0
3069
- ? options.conversationMessages
3070
- : options.conversationHistory;
3071
- if (historyMessages && historyMessages.length > 0) {
3072
- for (const msg of historyMessages) {
3190
+ // Replay conversationMessages (with tool turns), else the legacy text-only conversationHistory.
3191
+ if (options.conversationMessages &&
3192
+ options.conversationMessages.length > 0) {
3193
+ messages.push(...buildAnthropicHistoryMessages(options.conversationMessages));
3194
+ }
3195
+ else if (options.conversationHistory &&
3196
+ options.conversationHistory.length > 0) {
3197
+ for (const msg of options.conversationHistory) {
3073
3198
  if (msg.role === "user" || msg.role === "assistant") {
3074
3199
  messages.push({
3075
3200
  role: msg.role,
@@ -3201,13 +3326,10 @@ export class GoogleVertexProvider extends BaseProvider {
3201
3326
  type: "text",
3202
3327
  text: inputText,
3203
3328
  });
3204
- // Add the user message with appropriate content format
3205
- messages.push({
3206
- role: "user",
3207
- content: userContentParts.length === 1 && userContentParts[0].type === "text"
3208
- ? inputText
3209
- : userContentParts,
3210
- });
3329
+ // Append the live user turn (merges into a trailing user turn if present).
3330
+ appendUserMessage(messages, userContentParts.length === 1 && userContentParts[0].type === "text"
3331
+ ? inputText
3332
+ : userContentParts);
3211
3333
  // Convert tools to Anthropic format if present
3212
3334
  let tools;
3213
3335
  const executeMap = new Map();
@@ -1,8 +1,10 @@
1
1
  import type { ZodType } from "zod";
2
2
  import { AIProviderName } from "../constants/enums.js";
3
3
  import { BaseProvider } from "../core/baseProvider.js";
4
- import type { EnhancedGenerateResult, TextGenerationOptions, StreamOptions, StreamResult } from "../types/index.js";
4
+ import type { EnhancedGenerateResult, TextGenerationOptions, StreamOptions, StreamResult, VertexAnthropicMessage, ChatMessage, MinimalChatMessage } from "../types/index.js";
5
5
  import type { Schema, LanguageModel } from "../types/index.js";
6
+ export declare function buildAnthropicHistoryMessages(conversationMessages: Array<ChatMessage | MinimalChatMessage>): VertexAnthropicMessage[];
7
+ export declare function appendUserMessage(messages: VertexAnthropicMessage[], content: VertexAnthropicMessage["content"]): void;
6
8
  /**
7
9
  * Recursively strip JSON-schema fields that Vertex Gemini's function-call AND
8
10
  * `responseSchema` (structured output) validators reject with 400
@@ -44,6 +44,144 @@ const hasAnthropicSupport = () => {
44
44
  // Actual availability is checked at runtime when creating the client
45
45
  return true;
46
46
  };
47
+ // Parse stored rows into ordered segments, grouping tool_call/tool_result by (turn, step).
48
+ function collectHistorySegments(conversationMessages) {
49
+ const segments = [];
50
+ const stepMap = new Map();
51
+ let turnCounter = 0;
52
+ const getStep = (stepIndex) => {
53
+ const key = `${turnCounter}:${stepIndex ?? "x"}`;
54
+ let step = stepMap.get(key);
55
+ if (!step) {
56
+ step = { type: "tool_step", callParts: [], resultParts: [] };
57
+ stepMap.set(key, step);
58
+ segments.push(step);
59
+ }
60
+ return step;
61
+ };
62
+ for (const msg of conversationMessages) {
63
+ if (msg.role === "tool_call") {
64
+ getStep(msg.metadata?.stepIndex).callParts.push({
65
+ name: msg.tool || "unknown",
66
+ input: msg.args || {},
67
+ });
68
+ continue;
69
+ }
70
+ if (msg.role === "tool_result") {
71
+ getStep(msg.metadata?.stepIndex).resultParts.push(msg.content && msg.content.length > 0 ? msg.content : "(no output)");
72
+ continue;
73
+ }
74
+ const role = msg.role === "assistant"
75
+ ? "assistant"
76
+ : msg.role === "user"
77
+ ? "user"
78
+ : null;
79
+ if (!role || !msg.content || msg.content.trim().length === 0) {
80
+ continue;
81
+ }
82
+ // New turn → fresh step namespace for any following tool rows.
83
+ turnCounter++;
84
+ segments.push({ type: "regular", role, parts: [msg.content] });
85
+ }
86
+ return segments;
87
+ }
88
+ // Append blocks to the trailing turn if it shares the role, else start a new turn.
89
+ function pushTurn(messages, role, blocks) {
90
+ const last = messages[messages.length - 1];
91
+ if (last && last.role === role && Array.isArray(last.content)) {
92
+ last.content.push(...blocks);
93
+ }
94
+ else {
95
+ messages.push({ role, content: [...blocks] });
96
+ }
97
+ }
98
+ // Pair a tool step's calls/results by position; unbalanced extras are dropped
99
+ // because Anthropic 400s on an orphaned tool_use/tool_result reference.
100
+ function pairToolStep(step, ordinal) {
101
+ const calls = step.callParts;
102
+ const resultStrings = step.resultParts;
103
+ const paired = Math.min(calls.length, resultStrings.length);
104
+ if (paired !== calls.length || paired !== resultStrings.length) {
105
+ logger.debug("[GoogleVertex] Unbalanced tool step in Claude history replay", {
106
+ calls: calls.length,
107
+ results: resultStrings.length,
108
+ paired,
109
+ });
110
+ }
111
+ const uses = [];
112
+ const results = [];
113
+ for (let i = 0; i < paired; i++) {
114
+ const id = `hist_${ordinal}_${i}`;
115
+ uses.push({
116
+ type: "tool_use",
117
+ id,
118
+ name: calls[i].name,
119
+ input: calls[i].input,
120
+ });
121
+ results.push({
122
+ type: "tool_result",
123
+ tool_use_id: id,
124
+ content: resultStrings[i],
125
+ });
126
+ }
127
+ return { uses, results };
128
+ }
129
+ // Drop leading turns until the first is a real user turn — Anthropic requires it
130
+ // (covers a leading assistant/tool step and an orphaned tool_result-only turn).
131
+ function trimLeadingNonUser(messages) {
132
+ const isToolResultOnly = (m) => Array.isArray(m.content) &&
133
+ m.content.length > 0 &&
134
+ m.content.every((block) => block.type === "tool_result");
135
+ while (messages.length > 0 &&
136
+ (messages[0].role !== "user" || isToolResultOnly(messages[0]))) {
137
+ messages.shift();
138
+ }
139
+ }
140
+ // Rebuild Anthropic history with paired tool_use/tool_result turns from stored rows.
141
+ export function buildAnthropicHistoryMessages(conversationMessages) {
142
+ const messages = [];
143
+ let stepOrdinal = 0;
144
+ for (const seg of collectHistorySegments(conversationMessages)) {
145
+ if (seg.type === "regular") {
146
+ pushTurn(messages, seg.role === "assistant" ? "assistant" : "user", [
147
+ { type: "text", text: String(seg.parts[0] ?? "") },
148
+ ]);
149
+ continue;
150
+ }
151
+ const { uses, results } = pairToolStep(seg, stepOrdinal);
152
+ if (uses.length === 0) {
153
+ continue;
154
+ }
155
+ stepOrdinal++;
156
+ pushTurn(messages, "assistant", uses);
157
+ pushTurn(messages, "user", results);
158
+ }
159
+ trimLeadingNonUser(messages);
160
+ if (logger.shouldLog("debug")) {
161
+ const blocks = messages.flatMap((m) => Array.isArray(m.content) ? m.content : []);
162
+ logger.debug("[GoogleVertex] Replayed Claude history with tool turns", {
163
+ historyMessages: messages.length,
164
+ toolUseBlocks: blocks.filter((b) => b.type === "tool_use").length,
165
+ toolResultBlocks: blocks.filter((b) => b.type === "tool_result").length,
166
+ });
167
+ }
168
+ return messages;
169
+ }
170
+ // Append the live user turn, merging into a trailing user turn to avoid back-to-back user messages.
171
+ export function appendUserMessage(messages, content) {
172
+ const last = messages[messages.length - 1];
173
+ if (last && last.role === "user") {
174
+ const head = Array.isArray(last.content)
175
+ ? last.content
176
+ : [{ type: "text", text: last.content }];
177
+ const tail = Array.isArray(content)
178
+ ? content
179
+ : [{ type: "text", text: content }];
180
+ last.content = [...head, ...tail];
181
+ return;
182
+ }
183
+ messages.push({ role: "user", content });
184
+ }
47
185
  /**
48
186
  * Recursively strip JSON-schema fields that Vertex Gemini's function-call AND
49
187
  * `responseSchema` (structured output) validators reject with 400
@@ -2287,18 +2425,14 @@ export class GoogleVertexProvider extends BaseProvider {
2287
2425
  });
2288
2426
  // Build messages from input
2289
2427
  const messages = [];
2290
- // Add conversation history if present.
2291
- //
2292
- // Intentionally text-only. Anthropic's API rejects messages where a
2293
- // tool_use_id reference appears without its matching tool_use in the
2294
- // same turn — so synthesising tool_use / tool_result blocks from
2295
- // stored ChatMessages risks emitting orphaned references that fail
2296
- // validation. Tool rows are still persisted to Redis (chat-history
2297
- // UI renders them) but they don't re-enter the model's context on
2298
- // subsequent turns.
2428
+ // Replay conversationMessages (with tool turns), else the legacy text-only conversationHistory.
2299
2429
  if (options.conversationMessages &&
2300
2430
  options.conversationMessages.length > 0) {
2301
- for (const msg of options.conversationMessages) {
2431
+ messages.push(...buildAnthropicHistoryMessages(options.conversationMessages));
2432
+ }
2433
+ else if (options.conversationHistory &&
2434
+ options.conversationHistory.length > 0) {
2435
+ for (const msg of options.conversationHistory) {
2302
2436
  if (msg.role === "user" || msg.role === "assistant") {
2303
2437
  messages.push({
2304
2438
  role: msg.role,
@@ -2430,13 +2564,10 @@ export class GoogleVertexProvider extends BaseProvider {
2430
2564
  type: "text",
2431
2565
  text: multimodalInput.text,
2432
2566
  });
2433
- // Add the user message with appropriate content format
2434
- messages.push({
2435
- role: "user",
2436
- content: userContentParts.length === 1 && userContentParts[0].type === "text"
2437
- ? multimodalInput.text
2438
- : userContentParts,
2439
- });
2567
+ // Append the live user turn (merges into a trailing user turn if present).
2568
+ appendUserMessage(messages, userContentParts.length === 1 && userContentParts[0].type === "text"
2569
+ ? multimodalInput.text
2570
+ : userContentParts);
2440
2571
  // Convert tools to Anthropic format if present
2441
2572
  let tools;
2442
2573
  const executeMap = new Map();
@@ -3056,20 +3187,14 @@ export class GoogleVertexProvider extends BaseProvider {
3056
3187
  // Build messages from input
3057
3188
  const messages = [];
3058
3189
  const inputText = options.prompt || options.input?.text || "Please respond.";
3059
- // Add conversation history if present. Prefer `conversationMessages`
3060
- // (what NeuroLink core injects today via MessageBuilder) and fall back
3061
- // to the legacy `conversationHistory` field for callers that still use
3062
- // the older surface. The Vertex Claude STREAM path already follows this
3063
- // priority — keeping the GENERATE path on `conversationHistory` only
3064
- // would silently drop multi-turn context for memory/loop sessions.
3065
- // Intentionally text-only: see the stream sibling for the rationale —
3066
- // synthesising tool_use / tool_result blocks from stored ChatMessages
3067
- // risks emitting orphaned references that Anthropic's API rejects.
3068
- const historyMessages = options.conversationMessages && options.conversationMessages.length > 0
3069
- ? options.conversationMessages
3070
- : options.conversationHistory;
3071
- if (historyMessages && historyMessages.length > 0) {
3072
- for (const msg of historyMessages) {
3190
+ // Replay conversationMessages (with tool turns), else the legacy text-only conversationHistory.
3191
+ if (options.conversationMessages &&
3192
+ options.conversationMessages.length > 0) {
3193
+ messages.push(...buildAnthropicHistoryMessages(options.conversationMessages));
3194
+ }
3195
+ else if (options.conversationHistory &&
3196
+ options.conversationHistory.length > 0) {
3197
+ for (const msg of options.conversationHistory) {
3073
3198
  if (msg.role === "user" || msg.role === "assistant") {
3074
3199
  messages.push({
3075
3200
  role: msg.role,
@@ -3201,13 +3326,10 @@ export class GoogleVertexProvider extends BaseProvider {
3201
3326
  type: "text",
3202
3327
  text: inputText,
3203
3328
  });
3204
- // Add the user message with appropriate content format
3205
- messages.push({
3206
- role: "user",
3207
- content: userContentParts.length === 1 && userContentParts[0].type === "text"
3208
- ? inputText
3209
- : userContentParts,
3210
- });
3329
+ // Append the live user turn (merges into a trailing user turn if present).
3330
+ appendUserMessage(messages, userContentParts.length === 1 && userContentParts[0].type === "text"
3331
+ ? inputText
3332
+ : userContentParts);
3211
3333
  // Convert tools to Anthropic format if present
3212
3334
  let tools;
3213
3335
  const executeMap = new Map();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juspay/neurolink",
3
- "version": "9.80.0",
3
+ "version": "9.80.1",
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": {