@juspay/neurolink 13.10.5 → 13.12.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.
- package/CHANGELOG.md +3 -3
- package/dist/agent/agent.js +7 -6
- package/dist/browser/neurolink.min.js +439 -439
- package/dist/cli/loop/optionsSchema.d.ts +1 -1
- package/dist/core/nativeGenerateLoop.js +241 -3
- package/dist/neurolink.d.ts +7 -0
- package/dist/neurolink.js +21 -0
- package/dist/providers/anthropic/client.d.ts +17 -0
- package/dist/providers/anthropic/client.js +155 -6
- package/dist/providers/anthropic/loopAdapter.js +86 -41
- package/dist/providers/anthropic/threads.d.ts +80 -0
- package/dist/providers/anthropic/threads.js +442 -0
- package/dist/server/routes/claudeProxyRoutes.js +19 -1
- package/dist/types/generate.d.ts +26 -0
- package/dist/types/loopEngine.d.ts +68 -0
- package/dist/types/stream.d.ts +25 -0
- package/docs-site/static/search-index.json +2 -0
- package/package.json +8 -6
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
import { resolveDeferredTool } from "../../tools/toolDiscovery.js";
|
|
25
25
|
import { stringifyAnthropicToolOutput } from "./toolOutput.js";
|
|
26
26
|
import { stringifyFinalResultInput } from "./structuredOutput.js";
|
|
27
|
+
import { settleThread, threadRecovery } from "./threads.js";
|
|
27
28
|
import { composeAbortSignalsScoped, createTimeoutController, } from "../../utils/timeout.js";
|
|
28
29
|
import { sanitizeAnthropicMessagesForTrace } from "../../utils/anthropicTraceSanitizer.js";
|
|
29
30
|
import { NeuroLinkError } from "../../utils/errorHandling.js";
|
|
@@ -49,7 +50,7 @@ function mapAnthropicFinishReason(rawStopReason, hadToolCallsAtCap) {
|
|
|
49
50
|
* `executeStep` above it is now only the per-request deadline wiring, which is
|
|
50
51
|
* the part a reader looking for "what bounds this request" needs to find.
|
|
51
52
|
*/
|
|
52
|
-
async function readAnthropicStep({ params, client, channel, stepSignal, requestTimeoutSignal, noteObservedPromptTokens, finalResultToolName, onTerminalResult, requireTerminalEvent, }) {
|
|
53
|
+
async function readAnthropicStep({ params, client, channel, stepSignal, requestTimeoutSignal, noteObservedPromptTokens, finalResultToolName, onTerminalResult, requireTerminalEvent, threads, requestHeaders, }) {
|
|
53
54
|
// Single assertion, not a double: `messages.create` returns a union of
|
|
54
55
|
// Message and Stream, and Stream<RawMessageStreamEvent> already IS an
|
|
55
56
|
// AsyncIterable of that event, so the two types overlap and the
|
|
@@ -59,7 +60,10 @@ async function readAnthropicStep({ params, client, channel, stepSignal, requestT
|
|
|
59
60
|
// runAgenticLoop already derives the engine signal from the caller's
|
|
60
61
|
// abortSignal, so preferring config's would ignore engine-initiated
|
|
61
62
|
// cancellation entirely.
|
|
62
|
-
{
|
|
63
|
+
{
|
|
64
|
+
signal: stepSignal,
|
|
65
|
+
...(requestHeaders ? { headers: requestHeaders } : {}),
|
|
66
|
+
}));
|
|
63
67
|
const textByIndex = new Map();
|
|
64
68
|
const toolByIndex = new Map();
|
|
65
69
|
const thinkingByIndex = new Map();
|
|
@@ -88,6 +92,7 @@ async function readAnthropicStep({ params, client, channel, stepSignal, requestT
|
|
|
88
92
|
// content blocks that DID arrive are syntactically complete.
|
|
89
93
|
let sawTerminalEvent = false;
|
|
90
94
|
let responseModel;
|
|
95
|
+
let messageId;
|
|
91
96
|
for await (const rawEvent of events) {
|
|
92
97
|
if (stepSignal.aborted) {
|
|
93
98
|
break;
|
|
@@ -98,6 +103,7 @@ async function readAnthropicStep({ params, client, channel, stepSignal, requestT
|
|
|
98
103
|
// asserted.
|
|
99
104
|
const event = rawEvent;
|
|
100
105
|
if (event.type === "message_start") {
|
|
106
|
+
messageId = event.message?.id ?? messageId;
|
|
101
107
|
responseModel = event.message?.model ?? responseModel;
|
|
102
108
|
const usage = event.message?.usage;
|
|
103
109
|
inputTokens += usage?.input_tokens ?? 0;
|
|
@@ -232,6 +238,13 @@ async function readAnthropicStep({ params, client, channel, stepSignal, requestT
|
|
|
232
238
|
context: { provider: "anthropic", rawStopReason },
|
|
233
239
|
});
|
|
234
240
|
}
|
|
241
|
+
if (threads) {
|
|
242
|
+
settleThread(threads, {
|
|
243
|
+
sawTerminalEvent,
|
|
244
|
+
messageId,
|
|
245
|
+
stopReason: rawStopReason,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
235
248
|
// JSON.parse happily yields null, an array or a primitive, and an
|
|
236
249
|
// assertion converts none of them. Both `tool_use.input` and
|
|
237
250
|
// AgenticLoopToolCall.args require an object, so anything else becomes
|
|
@@ -414,46 +427,78 @@ export function createAnthropicLoopAdapter(config) {
|
|
|
414
427
|
},
|
|
415
428
|
async executeStep(request, channel, signal) {
|
|
416
429
|
const params = request.raw;
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
:
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
430
|
+
const runStep = async (stepParams, threadPlan) => {
|
|
431
|
+
// Per-REQUEST deadline, armed fresh for this step and disposed the moment
|
|
432
|
+
// it settles. Being armed per step is what makes it unresettable from a
|
|
433
|
+
// step boundary: the boundary runs between two steps, when no request
|
|
434
|
+
// deadline is running at all.
|
|
435
|
+
//
|
|
436
|
+
// Scoped composition rather than AbortSignal.any: the engine signal lives
|
|
437
|
+
// for the whole turn, and `any` keeps its registration on that signal
|
|
438
|
+
// until the derived one is collected, so composing per step accumulates
|
|
439
|
+
// listeners for the turn's whole length.
|
|
440
|
+
const requestTimeout = config.requestTimeoutMs
|
|
441
|
+
? createTimeoutController(config.requestTimeoutMs, "anthropic", "stream")
|
|
442
|
+
: null;
|
|
443
|
+
const composed = composeAbortSignalsScoped(signal, requestTimeout?.controller.signal);
|
|
444
|
+
try {
|
|
445
|
+
return await readAnthropicStep({
|
|
446
|
+
params: stepParams,
|
|
447
|
+
...(threadPlan
|
|
448
|
+
? { threads: config.threads, requestHeaders: threadPlan.headers }
|
|
449
|
+
: {}),
|
|
450
|
+
client: config.client,
|
|
451
|
+
channel,
|
|
452
|
+
// The engine's signal, not config's: runAgenticLoop already derives
|
|
453
|
+
// it from the caller's abortSignal, so preferring config's would
|
|
454
|
+
// ignore engine-initiated cancellation entirely.
|
|
455
|
+
stepSignal: composed.signal ?? signal,
|
|
456
|
+
requestTimeoutSignal: requestTimeout?.controller.signal,
|
|
457
|
+
...(config.noteObservedPromptTokens
|
|
458
|
+
? { noteObservedPromptTokens: config.noteObservedPromptTokens }
|
|
459
|
+
: {}),
|
|
460
|
+
...(config.finalResultToolName
|
|
461
|
+
? { finalResultToolName: config.finalResultToolName }
|
|
462
|
+
: {}),
|
|
463
|
+
...(config.onTerminalResult
|
|
464
|
+
? { onTerminalResult: config.onTerminalResult }
|
|
465
|
+
: {}),
|
|
466
|
+
...(config.requireTerminalEvent
|
|
467
|
+
? { requireTerminalEvent: config.requireTerminalEvent }
|
|
468
|
+
: {}),
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
finally {
|
|
472
|
+
composed.dispose();
|
|
473
|
+
requestTimeout?.cleanup();
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
const threads = config.threads;
|
|
477
|
+
if (!threads?.enabled) {
|
|
478
|
+
return runStep(params);
|
|
453
479
|
}
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
480
|
+
// A thread step goes out as a create or a continue. When the API cannot
|
|
481
|
+
// continue it, or cannot thread the request at all, the same step is
|
|
482
|
+
// sent once more in full; any other failure just ends the thread, so the
|
|
483
|
+
// next step starts a new one.
|
|
484
|
+
let plan = threads.plan(params);
|
|
485
|
+
for (let attempt = 0;; attempt++) {
|
|
486
|
+
try {
|
|
487
|
+
return await runStep(plan.params, plan);
|
|
488
|
+
}
|
|
489
|
+
catch (error) {
|
|
490
|
+
threads.reset();
|
|
491
|
+
const recovery = attempt === 0 ? threadRecovery(error) : undefined;
|
|
492
|
+
if (recovery === "recreate") {
|
|
493
|
+
plan = threads.plan(params);
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
if (recovery === "unthreaded") {
|
|
497
|
+
threads.disable();
|
|
498
|
+
return runStep(params);
|
|
499
|
+
}
|
|
500
|
+
throw error;
|
|
501
|
+
}
|
|
457
502
|
}
|
|
458
503
|
},
|
|
459
504
|
appendPlanningNudge(conversation, text) {
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message threads for the native Anthropic loop (beta, `message-threads-2026-08-12`).
|
|
3
|
+
*
|
|
4
|
+
* Without a thread every step of a turn resends the whole conversation. With
|
|
5
|
+
* one, the first step sends it with `thread: {type: "create"}` and each later
|
|
6
|
+
* step sends only the messages since the last response, plus the previous
|
|
7
|
+
* response id. The API keeps the conversation in memory for a few minutes and
|
|
8
|
+
* is best-effort about it, so the caller stays the source of truth: this
|
|
9
|
+
* module only decides, per step, whether the thread can be continued, and
|
|
10
|
+
* otherwise plans a `create` that sends everything again.
|
|
11
|
+
*
|
|
12
|
+
* A thread can be continued only when nothing the API fixed at creation has
|
|
13
|
+
* changed (model, tools, system, tool choice, thinking, output config and the
|
|
14
|
+
* beta set) and the messages already sent are untouched. Anything that
|
|
15
|
+
* rewrites history, such as a context reclaim, therefore starts a new thread
|
|
16
|
+
* by itself.
|
|
17
|
+
*/
|
|
18
|
+
import type { AnthropicThreadCarry, AnthropicThreadSession } from "../../types/index.js";
|
|
19
|
+
export declare const MESSAGE_THREADS_BETA = "message-threads-2026-08-12";
|
|
20
|
+
/**
|
|
21
|
+
* Record how a step's response ended. The API stores a thread turn only when
|
|
22
|
+
* the response ended normally; a refusal, a cut-off or an early end is resent
|
|
23
|
+
* in full by the next step.
|
|
24
|
+
*/
|
|
25
|
+
export declare function settleThread(session: AnthropicThreadSession, response: {
|
|
26
|
+
sawTerminalEvent: boolean;
|
|
27
|
+
messageId: string | undefined;
|
|
28
|
+
stopReason: string | undefined;
|
|
29
|
+
}): void;
|
|
30
|
+
/**
|
|
31
|
+
* Start the thread state for one turn.
|
|
32
|
+
*
|
|
33
|
+
* @param betaHeader the `anthropic-beta` value the client already sends; the
|
|
34
|
+
* thread beta is added to it, identically on every request of the thread.
|
|
35
|
+
*/
|
|
36
|
+
export declare function createAnthropicThreadSession(betaHeader: string | undefined, carried?: AnthropicThreadCarry): AnthropicThreadSession;
|
|
37
|
+
/** The API's `error.details.error_code` for a thread request that was refused, if there is one. */
|
|
38
|
+
export declare function threadErrorCode(error: unknown): string | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* What to do after a thread request fails: send everything again as a new
|
|
41
|
+
* thread, send it without a thread at all, or neither (an ordinary error).
|
|
42
|
+
*/
|
|
43
|
+
export declare function threadRecovery(error: unknown): "recreate" | "unthreaded" | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* The threads a NeuroLink instance has left behind for its sessions' next
|
|
46
|
+
* calls. In memory and per process only: another process simply creates a new
|
|
47
|
+
* thread, which is always correct. An entry is taken out by the call that
|
|
48
|
+
* continues it, because a response id can be continued once, so two concurrent
|
|
49
|
+
* calls of one session cannot both continue the same thread.
|
|
50
|
+
*/
|
|
51
|
+
export declare class AnthropicThreadStore {
|
|
52
|
+
private readonly entries;
|
|
53
|
+
private globalEpoch;
|
|
54
|
+
private readonly sessionEpochs;
|
|
55
|
+
private sweepTimer;
|
|
56
|
+
/**
|
|
57
|
+
* The identity of a thread's owner: who asked, in which session, with which
|
|
58
|
+
* credential and endpoint. A thread belongs to the key that created it, so
|
|
59
|
+
* the credential is part of the key (hashed; the key itself is never kept).
|
|
60
|
+
*/
|
|
61
|
+
static keyOf(parts: {
|
|
62
|
+
userId?: string;
|
|
63
|
+
sessionId: string;
|
|
64
|
+
credential: string;
|
|
65
|
+
baseURL?: string;
|
|
66
|
+
}): string;
|
|
67
|
+
take(key: string, now?: number): AnthropicThreadCarry | undefined;
|
|
68
|
+
/** The epoch a call must hand back to `put` for its thread to be kept. */
|
|
69
|
+
epochOf(sessionId: string): string;
|
|
70
|
+
put(key: string, sessionId: string, carry: AnthropicThreadCarry, epoch: string, now?: number): void;
|
|
71
|
+
/** Drop every entry that has expired. */
|
|
72
|
+
sweep(now?: number): void;
|
|
73
|
+
private armSweep;
|
|
74
|
+
/**
|
|
75
|
+
* Forget a session's thread, or every thread when no session is named, and
|
|
76
|
+
* fence any call still running from leaving one afterwards.
|
|
77
|
+
*/
|
|
78
|
+
forget(sessionId?: string): void;
|
|
79
|
+
get size(): number;
|
|
80
|
+
}
|