@juspay/neurolink 10.10.4 → 10.10.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.
Files changed (35) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/browser/neurolink.min.js +392 -391
  3. package/dist/context/anthropicLoopGuard.d.ts +46 -0
  4. package/dist/context/anthropicLoopGuard.js +167 -0
  5. package/dist/context/loopGuardCore.d.ts +43 -0
  6. package/dist/context/loopGuardCore.js +145 -0
  7. package/dist/context/openaiCompatLoopGuard.d.ts +42 -0
  8. package/dist/context/openaiCompatLoopGuard.js +172 -0
  9. package/dist/context/toolOutputLimits.d.ts +11 -0
  10. package/dist/context/toolOutputLimits.js +16 -0
  11. package/dist/core/conversationMemoryManager.d.ts +25 -0
  12. package/dist/core/conversationMemoryManager.js +71 -0
  13. package/dist/lib/context/anthropicLoopGuard.d.ts +46 -0
  14. package/dist/lib/context/anthropicLoopGuard.js +168 -0
  15. package/dist/lib/context/loopGuardCore.d.ts +43 -0
  16. package/dist/lib/context/loopGuardCore.js +146 -0
  17. package/dist/lib/context/openaiCompatLoopGuard.d.ts +42 -0
  18. package/dist/lib/context/openaiCompatLoopGuard.js +173 -0
  19. package/dist/lib/context/toolOutputLimits.d.ts +11 -0
  20. package/dist/lib/context/toolOutputLimits.js +16 -0
  21. package/dist/lib/core/conversationMemoryManager.d.ts +25 -0
  22. package/dist/lib/core/conversationMemoryManager.js +71 -0
  23. package/dist/lib/neurolink.d.ts +8 -2
  24. package/dist/lib/neurolink.js +18 -11
  25. package/dist/lib/providers/anthropic/client.js +98 -0
  26. package/dist/lib/providers/openaiChatCompletionsBase.js +38 -1
  27. package/dist/lib/types/context.d.ts +73 -0
  28. package/dist/lib/types/conversationMemoryInterface.d.ts +22 -0
  29. package/dist/neurolink.d.ts +8 -2
  30. package/dist/neurolink.js +18 -11
  31. package/dist/providers/anthropic/client.js +98 -0
  32. package/dist/providers/openaiChatCompletionsBase.js +38 -1
  33. package/dist/types/context.d.ts +73 -0
  34. package/dist/types/conversationMemoryInterface.d.ts +22 -0
  35. package/package.json +5 -1
@@ -412,6 +412,79 @@ export type RepairResult = {
412
412
  orphanedCallsFixed: number;
413
413
  orphanedResultsFixed: number;
414
414
  };
415
+ /**
416
+ * Provider-neutral view of ONE message in an agent loop's history.
417
+ *
418
+ * Every native provider loop keeps its history in a different shape (AI-SDK
419
+ * `ModelMessage`, OpenAI-compatible `{role,tool_calls}`, Gemini `contents`
420
+ * parts, Anthropic content blocks). The reclaim POLICY is identical across all
421
+ * of them, so adapters map their own shape onto this view, ask the core what to
422
+ * do, and apply the answer themselves.
423
+ */
424
+ export type LoopGuardEntry = {
425
+ /** `toolCall` and `toolResult` form the batches the policy keeps intact. */
426
+ kind: "other" | "toolCall" | "toolResult";
427
+ /** Estimated tokens this entry currently costs. */
428
+ tokens: number;
429
+ /**
430
+ * Tokens this entry would cost with its payload replaced by a head/tail
431
+ * preview. Omitted when the entry cannot usefully shrink — which is exactly
432
+ * the case that forces the policy to drop batches instead.
433
+ */
434
+ previewTokens?: number;
435
+ };
436
+ /** What the caller should do to reclaim budget. Indices refer to the input array. */
437
+ export type LoopGuardPlan = {
438
+ /** False when the loop is under threshold and nothing should change. */
439
+ fire: boolean;
440
+ /** Entries whose payload should be replaced by a preview. */
441
+ truncate: number[];
442
+ /** Entries to remove entirely — always whole batches, never a partial pair. */
443
+ drop: number[];
444
+ /** Estimated total after applying the plan, including fixed overhead. */
445
+ projectedTokens: number;
446
+ };
447
+ /**
448
+ * Structural view of one Anthropic content block, loose enough to accept the
449
+ * official SDK's `ContentBlockParam` union and NeuroLink's own
450
+ * `VertexAnthropicMessage` blocks without a cast at either call site.
451
+ */
452
+ export type AnthropicGuardBlock = {
453
+ type: string;
454
+ /** Payload of a `tool_result` block. Other block kinds carry other fields. */
455
+ content?: unknown;
456
+ /** Text of a `text` block. */
457
+ text?: string;
458
+ };
459
+ /**
460
+ * Structural view of one Anthropic-shaped message, as used by both the direct
461
+ * Anthropic loop and the native Vertex+Claude path. Tool calls ride as
462
+ * `tool_use` blocks on an assistant message; their answers ride as
463
+ * `tool_result` blocks on the following user message.
464
+ */
465
+ export type AnthropicGuardMessage = {
466
+ /**
467
+ * `system` is included because the installed `@anthropic-ai/sdk` widens
468
+ * `MessageParam["role"]` to accept it; narrowing here would make the SDK's
469
+ * own array unassignable at the call site.
470
+ */
471
+ role: "user" | "assistant" | "system";
472
+ content: string | AnthropicGuardBlock[];
473
+ };
474
+ /** Tuning for {@link planLoopGuardReclaim}. */
475
+ export type LoopGuardPolicy = {
476
+ availableInputTokens: number;
477
+ /** System prompt + tool definitions — rides outside the message array. */
478
+ fixedOverheadTokens: number;
479
+ /** Fraction of the window at which the guard fires. */
480
+ thresholdRatio?: number;
481
+ /** Fraction of the window the guard reclaims down to once it fires. */
482
+ lowWaterRatio?: number;
483
+ /** Newest entries the guard must never modify. */
484
+ protectedTailCount?: number;
485
+ /** Observed/estimated token ratio, used to tighten both marks. */
486
+ calibration?: number;
487
+ };
415
488
  /**
416
489
  * One contiguous tool batch: the run of `tool_call` messages emitted by a
417
490
  * single agent step, plus the run of `tool_result` messages that follows it.
@@ -32,6 +32,28 @@ export type IConversationMemoryManager = {
32
32
  getSessionMessages(sessionId: string, userId?: string): Promise<ChatMessage[]>;
33
33
  /** Replace the entire messages array for a session */
34
34
  setSessionMessages(sessionId: string, messages: ChatMessage[], userId?: string): Promise<void>;
35
+ /**
36
+ * Persist a step's tool calls and results as `tool_call` / `tool_result`
37
+ * messages on the session.
38
+ *
39
+ * Declared on the interface so every backend can implement it. Previously
40
+ * only the Redis manager had it, and the caller reached it by casting — so
41
+ * on in-memory storage tool activity never became messages at all, and the
42
+ * compaction, pruning and pair-repair paths saw a different history shape
43
+ * depending on `STORAGE_TYPE`.
44
+ */
45
+ storeToolExecution?(sessionId: string, userId: string | undefined, toolCalls: Array<{
46
+ toolCallId?: string;
47
+ toolName?: string;
48
+ args?: Record<string, unknown>;
49
+ [key: string]: unknown;
50
+ }>, toolResults: Array<{
51
+ toolCallId?: string;
52
+ output?: unknown;
53
+ result?: unknown;
54
+ error?: string;
55
+ [key: string]: unknown;
56
+ }>, currentTime?: Date): Promise<void>;
35
57
  /** Close/shutdown the memory manager and release resources (e.g., Redis connections) */
36
58
  close?(): Promise<void>;
37
59
  };
@@ -1787,8 +1787,14 @@ export declare class NeuroLink {
1787
1787
  [key: string]: unknown;
1788
1788
  }>, currentTime?: Date): Promise<void>;
1789
1789
  /**
1790
- * Check if tool execution storage is available
1791
- * @returns boolean indicating if Redis storage is configured and available
1790
+ * Check if tool execution storage is available.
1791
+ *
1792
+ * Now capability-based rather than Redis-specific: any configured memory
1793
+ * backend implementing `storeToolExecution` qualifies. The old check
1794
+ * required `STORAGE_TYPE === "redis"` AND a Redis manager by class name, so
1795
+ * in-memory sessions reported false and silently skipped tool persistence.
1796
+ *
1797
+ * @returns whether the active memory backend can persist tool executions
1792
1798
  */
1793
1799
  isToolExecutionStorageAvailable(): boolean;
1794
1800
  /**
package/dist/neurolink.js CHANGED
@@ -11129,11 +11129,16 @@ Current user's request: ${currentInput}`;
11129
11129
  });
11130
11130
  return;
11131
11131
  }
11132
- // Type guard to ensure it's Redis conversation memory manager
11133
- const redisMemory = this
11134
- .conversationMemory;
11132
+ // Any backend that implements storeToolExecution no longer a Redis cast.
11133
+ // The in-memory manager implements it too, so tool activity becomes
11134
+ // tool_call/tool_result messages regardless of STORAGE_TYPE.
11135
+ const memory = this.conversationMemory;
11136
+ if (!memory?.storeToolExecution) {
11137
+ logger.debug("Tool execution storage not supported by this memory backend");
11138
+ return;
11139
+ }
11135
11140
  try {
11136
- await redisMemory.storeToolExecution(sessionId, userId, toolCalls, toolResults, currentTime);
11141
+ await memory.storeToolExecution(sessionId, userId, toolCalls, toolResults, currentTime);
11137
11142
  }
11138
11143
  catch (error) {
11139
11144
  logger.warn("Failed to store tool executions", {
@@ -11145,15 +11150,17 @@ Current user's request: ${currentInput}`;
11145
11150
  }
11146
11151
  }
11147
11152
  /**
11148
- * Check if tool execution storage is available
11149
- * @returns boolean indicating if Redis storage is configured and available
11153
+ * Check if tool execution storage is available.
11154
+ *
11155
+ * Now capability-based rather than Redis-specific: any configured memory
11156
+ * backend implementing `storeToolExecution` qualifies. The old check
11157
+ * required `STORAGE_TYPE === "redis"` AND a Redis manager by class name, so
11158
+ * in-memory sessions reported false and silently skipped tool persistence.
11159
+ *
11160
+ * @returns whether the active memory backend can persist tool executions
11150
11161
  */
11151
11162
  isToolExecutionStorageAvailable() {
11152
- const isRedisStorage = process.env.STORAGE_TYPE === "redis";
11153
- const hasRedisConversationMemory = this.conversationMemory &&
11154
- this.conversationMemory.constructor.name ===
11155
- "RedisConversationMemoryManager";
11156
- return !!(isRedisStorage && hasRedisConversationMemory);
11163
+ return typeof this.conversationMemory?.storeToolExecution === "function";
11157
11164
  }
11158
11165
  /**
11159
11166
  * Get the raw messages array for a session.
@@ -14,6 +14,9 @@ import { createProxyFetch } from "../../proxy/proxyFetch.js";
14
14
  import { getCapturedLimitSnapshot, getCapturedResponseHeaders, logClaudeLimitSnapshot, runInLimitCaptureScope, setLimitSpanAttributes, withLimitCapture, wrapFetchWithLimitCapture, } from "./rateLimitCapture.js";
15
15
  import { AuthenticationError, NetworkError, ProviderError, RateLimitError, } from "../../types/index.js";
16
16
  import { logger } from "../../utils/logger.js";
17
+ import { ANTHROPIC_ELISION_NOTE, planAnthropicLoopReclaim, previewAnthropicToolResultText, } from "../../context/anthropicLoopGuard.js";
18
+ import { getAvailableInputTokens } from "../../constants/contextWindows.js";
19
+ import { estimateTokens } from "../../utils/tokenEstimation.js";
17
20
  import { redactUrlCredentials } from "../../utils/logSanitize.js";
18
21
  import { ANTHROPIC_MAX_CACHE_BREAKPOINTS, applyAnthropicHistoryCacheBreakpoints, countAnthropicCacheMarkers, } from "../../utils/anthropicCacheBreakpoints.js";
19
22
  import { calculateCost } from "../../utils/pricing.js";
@@ -1509,8 +1512,30 @@ export class AnthropicProvider extends BaseProvider {
1509
1512
  // and stay fully incremental.
1510
1513
  let bufferedText = "";
1511
1514
  let finalResultText;
1515
+ /** System prompt + tool definitions: they ride outside `messages`. */
1516
+ const estimateAnthropicFixedOverhead = (system, tools) => {
1517
+ const text = (value) => {
1518
+ if (typeof value === "string") {
1519
+ return value;
1520
+ }
1521
+ try {
1522
+ return JSON.stringify(value) ?? "";
1523
+ }
1524
+ catch {
1525
+ return "";
1526
+ }
1527
+ };
1528
+ return (estimateTokens(text(system), "anthropic") +
1529
+ estimateTokens(text(tools), "anthropic"));
1530
+ };
1512
1531
  const runLoop = async () => {
1513
1532
  const conversation = payload.messages.slice();
1533
+ // The provider's REAL prompt-token count for the previous step,
1534
+ // calibrating the guard's char-based estimate for free, paired with the
1535
+ // guard's own estimate for that same request — a ratio between counts of
1536
+ // two different payloads would be meaningless.
1537
+ let lastObservedPromptTokens;
1538
+ let lastSentEstimate;
1514
1539
  for (let step = 0; step < maxSteps; step++) {
1515
1540
  // Mid-turn discovery sync: search_tools (tools.discovery) hydrates
1516
1541
  // new tools into toolsRecord between steps; Claude only calls tools
@@ -1524,6 +1549,71 @@ export class AnthropicProvider extends BaseProvider {
1524
1549
  logger.info(`[Anthropic] ${Object.keys(hydrated).length} tool(s) hydrated mid-turn via discovery: ${Object.keys(hydrated).join(", ")}`);
1525
1550
  }
1526
1551
  }
1552
+ // In-turn context guard. This loop appends an assistant tool_use
1553
+ // message plus a user tool_result message every step — growth the
1554
+ // pre-dispatch budget check never sees. Without it a long agentic run
1555
+ // overflows the window mid-loop and loses every completed step.
1556
+ // Returns undefined while the request still fits, leaving the history
1557
+ // byte-identical so the rolling cache prefix below stays valid.
1558
+ const reclaim = planAnthropicLoopReclaim({
1559
+ conversation,
1560
+ availableInputTokens: getAvailableInputTokens("anthropic", modelId, options.maxTokens ?? undefined),
1561
+ fixedOverheadTokens: estimateAnthropicFixedOverhead(payload.system, anthropicTools),
1562
+ provider: "anthropic",
1563
+ observedPromptTokens: lastObservedPromptTokens,
1564
+ // Both halves of the calibration ratio must describe the same
1565
+ // request: the tokens the provider reported, and this guard's own
1566
+ // estimate for what was sent to earn them.
1567
+ previousSentEstimate: lastSentEstimate,
1568
+ onSentEstimate: (tokens) => {
1569
+ lastSentEstimate = tokens;
1570
+ },
1571
+ });
1572
+ if (reclaim) {
1573
+ // Applied HERE, in the loop's own concrete types: the guard decides,
1574
+ // the caller mutates. Dropping an assistant tool_use message together
1575
+ // with its user tool_result message is what keeps blocks paired.
1576
+ const dropSet = new Set(reclaim.drop);
1577
+ const truncateSet = new Set(reclaim.truncate);
1578
+ const rebuilt = [];
1579
+ for (let i = 0; i < conversation.length; i++) {
1580
+ if (dropSet.has(i)) {
1581
+ continue;
1582
+ }
1583
+ const message = conversation[i];
1584
+ if (truncateSet.has(i) && Array.isArray(message.content)) {
1585
+ rebuilt.push({
1586
+ ...message,
1587
+ content: message.content.map((block) => block.type === "tool_result"
1588
+ ? {
1589
+ ...block,
1590
+ content: previewAnthropicToolResultText(typeof block.content === "string"
1591
+ ? block.content
1592
+ : (JSON.stringify(block.content) ?? "")),
1593
+ }
1594
+ : block),
1595
+ });
1596
+ continue;
1597
+ }
1598
+ rebuilt.push(message);
1599
+ }
1600
+ if (dropSet.size > 0) {
1601
+ // Anthropic requires user/assistant alternation around tool blocks;
1602
+ // the note is a user turn placed immediately before the first
1603
+ // surviving assistant tool_use turn, which preserves it.
1604
+ let noteIndex = rebuilt.findIndex((m) => Array.isArray(m.content) &&
1605
+ m.content.some((b) => b.type === "tool_use" || b.type === "tool_result"));
1606
+ if (noteIndex < 0) {
1607
+ noteIndex = Math.min(1, rebuilt.length);
1608
+ }
1609
+ rebuilt.splice(noteIndex, 0, {
1610
+ role: "user",
1611
+ content: [{ type: "text", text: ANTHROPIC_ELISION_NOTE }],
1612
+ });
1613
+ }
1614
+ conversation.length = 0;
1615
+ conversation.push(...rebuilt);
1616
+ }
1527
1617
  // Prompt-cache parity with the native Vertex+Claude path — rolling
1528
1618
  // history breakpoints, re-applied per step so the stable prefix
1529
1619
  // stays byte-identical while the breakpoint follows the growing
@@ -1587,6 +1677,14 @@ export class AnthropicProvider extends BaseProvider {
1587
1677
  totalCacheRead += event.message.usage.cache_read_input_tokens ?? 0;
1588
1678
  totalCacheWrite +=
1589
1679
  event.message.usage.cache_creation_input_tokens ?? 0;
1680
+ // Calibration signal for the in-turn guard: the FULL prompt size,
1681
+ // which on this path means uncached input plus both cache tiers.
1682
+ // Using input_tokens alone would read a cache-hit step as tiny and
1683
+ // let the guard drift far under the real cost.
1684
+ lastObservedPromptTokens =
1685
+ (event.message.usage.input_tokens ?? 0) +
1686
+ (event.message.usage.cache_read_input_tokens ?? 0) +
1687
+ (event.message.usage.cache_creation_input_tokens ?? 0);
1590
1688
  }
1591
1689
  else if (event.type === "content_block_start") {
1592
1690
  blockTypes.set(event.index, event.content_block.type);
@@ -17,7 +17,8 @@
17
17
  * Nothing here imports from "ai" or "@ai-sdk/*". The base class is a
18
18
  * direct HTTP client + multi-step tool-execution loop driven by SSE.
19
19
  */
20
- import { getRuntimeContextWindow, getRuntimeOutputCeiling, registerRuntimeContextWindow, } from "../constants/contextWindows.js";
20
+ import { getAvailableInputTokens, getRuntimeContextWindow, getRuntimeOutputCeiling, registerRuntimeContextWindow, } from "../constants/contextWindows.js";
21
+ import { guardOpenAICompatConversation } from "../context/openaiCompatLoopGuard.js";
21
22
  import { isContextOverflowError, parseProviderOverflowDetails, } from "../context/errorDetection.js";
22
23
  import { ContextBudgetExceededError } from "../context/errors.js";
23
24
  import { BaseProvider } from "../core/baseProvider.js";
@@ -770,6 +771,12 @@ export class OpenAIChatCompletionsProvider extends BaseProvider {
770
771
  // May grow mid-turn: hydrated tools with wire-unsafe names need
771
772
  // reverse-mapping even when the initial name set required none.
772
773
  let effectiveToolNameFromWire = toolNameFromWire;
774
+ // The provider's REAL prompt-token count for the previous step, used to
775
+ // calibrate the guard's char-based estimate for free, paired with the
776
+ // guard's own estimate for that same request — a ratio between counts of
777
+ // two different payloads would be meaningless.
778
+ let lastObservedPromptTokens;
779
+ let lastSentEstimate;
773
780
  for (let step = 0; step < maxSteps; step++) {
774
781
  // Mid-turn discovery sync: search_tools (tools.discovery) hydrates
775
782
  // new tools into toolsRecord between steps. Dispatch already re-reads
@@ -795,6 +802,35 @@ export class OpenAIChatCompletionsProvider extends BaseProvider {
795
802
  logger.info(`${this.providerName}: ${Object.keys(hydrated).length} tool(s) hydrated mid-turn via discovery: ${Object.keys(hydrated).join(", ")}`);
796
803
  }
797
804
  }
805
+ // In-turn context guard. This loop appends an assistant tool-call
806
+ // message plus one tool message per result on every step — growth the
807
+ // pre-dispatch budget check never sees. Without this, a long agentic
808
+ // run walks into a provider "context length exceeded" and loses every
809
+ // completed step. Shares its reclaim policy with the other provider
810
+ // loops via loopGuardCore; returns undefined (leaving `conversation`
811
+ // byte-identical) whenever the request still fits, so a loop that fits
812
+ // never pays a prompt-cache invalidation.
813
+ const guarded = guardOpenAICompatConversation({
814
+ conversation,
815
+ availableInputTokens: getAvailableInputTokens(this.providerName, modelId, options.maxTokens ?? undefined),
816
+ // Tool definitions ride outside the message array. Passing an empty
817
+ // message list yields the tools-only overhead, and reuses the same
818
+ // estimator the wire path already trusts.
819
+ fixedOverheadTokens: estimateWireTokens([], openAITools, this.providerName),
820
+ provider: this.providerName,
821
+ observedPromptTokens: lastObservedPromptTokens,
822
+ // Both halves of the calibration ratio must describe the same
823
+ // request: the tokens the provider reported, and this guard's own
824
+ // estimate for what was sent to earn them.
825
+ previousSentEstimate: lastSentEstimate,
826
+ onSentEstimate: (tokens) => {
827
+ lastSentEstimate = tokens;
828
+ },
829
+ });
830
+ if (guarded) {
831
+ conversation.length = 0;
832
+ conversation.push(...guarded);
833
+ }
798
834
  const stepResult = await this.streamOneStep({
799
835
  modelId,
800
836
  url,
@@ -806,6 +842,7 @@ export class OpenAIChatCompletionsProvider extends BaseProvider {
806
842
  openAIToolChoice,
807
843
  pushChunk,
808
844
  });
845
+ lastObservedPromptTokens = stepResult.usage?.prompt_tokens;
809
846
  stepFinish = stepResult.finishReason;
810
847
  if (stepResult.usage) {
811
848
  stepUsage = mergeUsage(stepUsage, stepResult.usage);
@@ -412,6 +412,79 @@ export type RepairResult = {
412
412
  orphanedCallsFixed: number;
413
413
  orphanedResultsFixed: number;
414
414
  };
415
+ /**
416
+ * Provider-neutral view of ONE message in an agent loop's history.
417
+ *
418
+ * Every native provider loop keeps its history in a different shape (AI-SDK
419
+ * `ModelMessage`, OpenAI-compatible `{role,tool_calls}`, Gemini `contents`
420
+ * parts, Anthropic content blocks). The reclaim POLICY is identical across all
421
+ * of them, so adapters map their own shape onto this view, ask the core what to
422
+ * do, and apply the answer themselves.
423
+ */
424
+ export type LoopGuardEntry = {
425
+ /** `toolCall` and `toolResult` form the batches the policy keeps intact. */
426
+ kind: "other" | "toolCall" | "toolResult";
427
+ /** Estimated tokens this entry currently costs. */
428
+ tokens: number;
429
+ /**
430
+ * Tokens this entry would cost with its payload replaced by a head/tail
431
+ * preview. Omitted when the entry cannot usefully shrink — which is exactly
432
+ * the case that forces the policy to drop batches instead.
433
+ */
434
+ previewTokens?: number;
435
+ };
436
+ /** What the caller should do to reclaim budget. Indices refer to the input array. */
437
+ export type LoopGuardPlan = {
438
+ /** False when the loop is under threshold and nothing should change. */
439
+ fire: boolean;
440
+ /** Entries whose payload should be replaced by a preview. */
441
+ truncate: number[];
442
+ /** Entries to remove entirely — always whole batches, never a partial pair. */
443
+ drop: number[];
444
+ /** Estimated total after applying the plan, including fixed overhead. */
445
+ projectedTokens: number;
446
+ };
447
+ /**
448
+ * Structural view of one Anthropic content block, loose enough to accept the
449
+ * official SDK's `ContentBlockParam` union and NeuroLink's own
450
+ * `VertexAnthropicMessage` blocks without a cast at either call site.
451
+ */
452
+ export type AnthropicGuardBlock = {
453
+ type: string;
454
+ /** Payload of a `tool_result` block. Other block kinds carry other fields. */
455
+ content?: unknown;
456
+ /** Text of a `text` block. */
457
+ text?: string;
458
+ };
459
+ /**
460
+ * Structural view of one Anthropic-shaped message, as used by both the direct
461
+ * Anthropic loop and the native Vertex+Claude path. Tool calls ride as
462
+ * `tool_use` blocks on an assistant message; their answers ride as
463
+ * `tool_result` blocks on the following user message.
464
+ */
465
+ export type AnthropicGuardMessage = {
466
+ /**
467
+ * `system` is included because the installed `@anthropic-ai/sdk` widens
468
+ * `MessageParam["role"]` to accept it; narrowing here would make the SDK's
469
+ * own array unassignable at the call site.
470
+ */
471
+ role: "user" | "assistant" | "system";
472
+ content: string | AnthropicGuardBlock[];
473
+ };
474
+ /** Tuning for {@link planLoopGuardReclaim}. */
475
+ export type LoopGuardPolicy = {
476
+ availableInputTokens: number;
477
+ /** System prompt + tool definitions — rides outside the message array. */
478
+ fixedOverheadTokens: number;
479
+ /** Fraction of the window at which the guard fires. */
480
+ thresholdRatio?: number;
481
+ /** Fraction of the window the guard reclaims down to once it fires. */
482
+ lowWaterRatio?: number;
483
+ /** Newest entries the guard must never modify. */
484
+ protectedTailCount?: number;
485
+ /** Observed/estimated token ratio, used to tighten both marks. */
486
+ calibration?: number;
487
+ };
415
488
  /**
416
489
  * One contiguous tool batch: the run of `tool_call` messages emitted by a
417
490
  * single agent step, plus the run of `tool_result` messages that follows it.
@@ -32,6 +32,28 @@ export type IConversationMemoryManager = {
32
32
  getSessionMessages(sessionId: string, userId?: string): Promise<ChatMessage[]>;
33
33
  /** Replace the entire messages array for a session */
34
34
  setSessionMessages(sessionId: string, messages: ChatMessage[], userId?: string): Promise<void>;
35
+ /**
36
+ * Persist a step's tool calls and results as `tool_call` / `tool_result`
37
+ * messages on the session.
38
+ *
39
+ * Declared on the interface so every backend can implement it. Previously
40
+ * only the Redis manager had it, and the caller reached it by casting — so
41
+ * on in-memory storage tool activity never became messages at all, and the
42
+ * compaction, pruning and pair-repair paths saw a different history shape
43
+ * depending on `STORAGE_TYPE`.
44
+ */
45
+ storeToolExecution?(sessionId: string, userId: string | undefined, toolCalls: Array<{
46
+ toolCallId?: string;
47
+ toolName?: string;
48
+ args?: Record<string, unknown>;
49
+ [key: string]: unknown;
50
+ }>, toolResults: Array<{
51
+ toolCallId?: string;
52
+ output?: unknown;
53
+ result?: unknown;
54
+ error?: string;
55
+ [key: string]: unknown;
56
+ }>, currentTime?: Date): Promise<void>;
35
57
  /** Close/shutdown the memory manager and release resources (e.g., Redis connections) */
36
58
  close?(): Promise<void>;
37
59
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juspay/neurolink",
3
- "version": "10.10.4",
3
+ "version": "10.10.6",
4
4
  "packageManager": "pnpm@10.15.1",
5
5
  "description": "TypeScript AI SDK with 24+ LLM providers behind one consistent API. MCP-native (connect any MCP server), voice TTS/STT/realtime, RAG, agents, memory, context compaction. OpenAI · Anthropic · Gemini · Bedrock · Azure · Ollama · DeepSeek · NVIDIA NIM and more.",
6
6
  "author": {
@@ -85,6 +85,10 @@
85
85
  "test:tool-pairing": "npx tsx test/continuous-test-suite-tool-pairing.ts",
86
86
  "test:token-accounting": "npx tsx test/continuous-test-suite-token-accounting.ts",
87
87
  "test:step-guard": "npx tsx test/continuous-test-suite-step-guard.ts",
88
+ "test:loop-guard-core": "npx tsx test/continuous-test-suite-loop-guard-core.ts",
89
+ "test:openai-compat-guard": "npx tsx test/continuous-test-suite-openai-compat-guard.ts",
90
+ "test:anthropic-guard": "npx tsx test/continuous-test-suite-anthropic-guard.ts",
91
+ "test:tool-storage-parity": "npx tsx test/continuous-test-suite-tool-storage-parity.ts",
88
92
  "test:middleware": "npx tsx test/continuous-test-suite-middleware.ts",
89
93
  "test:observability": "npx tsx test/continuous-test-suite-observability.ts",
90
94
  "test:ppt": "npx tsx test/continuous-test-suite-ppt.ts",