@juspay/neurolink 13.11.0 → 13.12.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.
@@ -16,6 +16,7 @@
16
16
  * by itself.
17
17
  */
18
18
  import { createHash } from "node:crypto";
19
+ import { CONVERSATION_INSTRUCTIONS } from "../../config/conversationMemory.js";
19
20
  export const MESSAGE_THREADS_BETA = "message-threads-2026-08-12";
20
21
  /** Stop reasons after which the API has stored the response as a continuable turn. */
21
22
  const STORABLE_STOP_REASONS = new Set([
@@ -36,7 +37,7 @@ export function settleThread(session, response) {
36
37
  response.messageId !== undefined &&
37
38
  response.stopReason !== undefined &&
38
39
  STORABLE_STOP_REASONS.has(response.stopReason)) {
39
- session.accept(response.messageId);
40
+ session.accept(response.messageId, response.stopReason);
40
41
  }
41
42
  else {
42
43
  session.reset();
@@ -63,6 +64,73 @@ const withoutCacheControl = (value) => {
63
64
  }
64
65
  return value;
65
66
  };
67
+ // What conversation memory keeps of a message: its role and its text. Memory
68
+ // drops tool_use / tool_result blocks and anything that is not text, so that is
69
+ // all a later call can be compared on.
70
+ const textOfMessage = (message) => {
71
+ const content = message?.content;
72
+ if (typeof content === "string") {
73
+ return content;
74
+ }
75
+ if (!Array.isArray(content)) {
76
+ return "";
77
+ }
78
+ return content
79
+ .map((block) => {
80
+ const b = block;
81
+ if (b?.type === "text" && typeof b.text === "string") {
82
+ return b.text;
83
+ }
84
+ // An attachment (image, document) is part of what the model was given.
85
+ // It is compared by content, so a replaced one is a different message;
86
+ // an attachment memory does not hand back simply never matches, which
87
+ // costs a new thread and nothing else.
88
+ return b === null || b === undefined
89
+ ? ""
90
+ : `\u0000${String(b.type)}:${sha(withoutCacheControl(b))}\u0000`;
91
+ })
92
+ .join("");
93
+ };
94
+ // By default conversation memory replays a turn's tool calls as one
95
+ // `[called <tool> → ok]` line each, in front of the assistant's own text. Those
96
+ // lines describe tool blocks the thread already holds in full, so they are
97
+ // left out of the comparison: what is compared is what the model said.
98
+ const TOOL_MARKER_LINE = /^\[called [^\n]*? → (?:ok|error|unknown)\](?:\n|$)/;
99
+ const withoutToolMarkers = (text) => {
100
+ let rest = text;
101
+ while (TOOL_MARKER_LINE.test(rest)) {
102
+ rest = rest.replace(TOOL_MARKER_LINE, "");
103
+ }
104
+ return rest;
105
+ };
106
+ const memoryHash = (role, text) => sha([role, role === "assistant" ? withoutToolMarkers(text) : text]);
107
+ const memoryHashOf = (message) => memoryHash(message?.role, textOfMessage(message));
108
+ // NeuroLink appends CONVERSATION_INSTRUCTIONS to the system prompt once a
109
+ // conversation has history, so a session's first call and its second carry
110
+ // different system prompts for the same conversation. The note is about
111
+ // history a continued thread already holds, so it is left out of the
112
+ // comparison; a continued call therefore runs without it.
113
+ const withoutConversationNote = (system) => {
114
+ if (typeof system === "string") {
115
+ return system.endsWith(CONVERSATION_INSTRUCTIONS)
116
+ ? system.slice(0, -CONVERSATION_INSTRUCTIONS.length)
117
+ : system;
118
+ }
119
+ if (Array.isArray(system)) {
120
+ return system.map((block) => {
121
+ const text = block?.text;
122
+ return typeof text === "string" &&
123
+ text.endsWith(CONVERSATION_INSTRUCTIONS)
124
+ ? {
125
+ ...block,
126
+ text: text.slice(0, -CONVERSATION_INSTRUCTIONS.length),
127
+ }
128
+ : block;
129
+ });
130
+ }
131
+ return system;
132
+ };
133
+ const TURN_ENDING_STOPS = new Set(["end_turn", "stop_sequence"]);
66
134
  const FIXED_FIELDS = [
67
135
  "model",
68
136
  "system",
@@ -86,7 +154,7 @@ const mergeBetas = (existing) => [
86
154
  * @param betaHeader the `anthropic-beta` value the client already sends; the
87
155
  * thread beta is added to it, identically on every request of the thread.
88
156
  */
89
- export function createAnthropicThreadSession(betaHeader) {
157
+ export function createAnthropicThreadSession(betaHeader, carried) {
90
158
  const headers = { "anthropic-beta": mergeBetas(betaHeader) };
91
159
  let enabled = true;
92
160
  let previousMessageId;
@@ -95,6 +163,18 @@ export function createAnthropicThreadSession(betaHeader) {
95
163
  // the API stored after them is not hashed; it is the API's own copy.
96
164
  let stored;
97
165
  let pending;
166
+ // The call's starting conversation as memory sees it, and how the last
167
+ // stored response ended: together they are what the next call is matched on.
168
+ let startAnchor;
169
+ // The call's starting messages, hashed exactly. A thread can be handed to the
170
+ // next call only while it still holds them unchanged: a reclaim that rewrites
171
+ // earlier history leaves a thread the caller's history no longer matches.
172
+ let startHashes;
173
+ let extendsStart = true;
174
+ let lastStop;
175
+ // A thread left by the previous call of the session, adopted by the first
176
+ // plan only if everything it was created with is still the same.
177
+ let handedOver = carried;
98
178
  return {
99
179
  get enabled() {
100
180
  return enabled;
@@ -105,10 +185,35 @@ export function createAnthropicThreadSession(betaHeader) {
105
185
  betas: headers["anthropic-beta"],
106
186
  };
107
187
  for (const field of FIXED_FIELDS) {
108
- fixed[field] = bag[field];
188
+ fixed[field] =
189
+ field === "system" ? withoutConversationNote(bag[field]) : bag[field];
109
190
  }
110
191
  const fingerprint = sha(fixed);
111
192
  const hashes = params.messages.map((message) => sha(withoutCacheControl(message)));
193
+ const memory = params.messages.map(memoryHashOf);
194
+ startAnchor ??= memory;
195
+ startHashes ??= hashes;
196
+ if (hashes.length < startHashes.length ||
197
+ !startHashes.every((hash, index) => hashes[index] === hash)) {
198
+ extendsStart = false;
199
+ }
200
+ if (handedOver !== undefined) {
201
+ // The previous call ended on an assistant reply that memory now hands
202
+ // back, and this call adds one user message. The thread also holds the
203
+ // tool blocks memory dropped; that difference is the point of the
204
+ // session scope and is accepted, not detected.
205
+ const prior = handedOver;
206
+ handedOver = undefined;
207
+ const last = params.messages[params.messages.length - 1];
208
+ if (params.messages.length >= 3 &&
209
+ last.role === "user" &&
210
+ prior.fingerprint === fingerprint &&
211
+ prior.anchor.length === memory.length - 1 &&
212
+ prior.anchor.every((hash, index) => hash === memory[index])) {
213
+ previousMessageId = prior.previousMessageId;
214
+ stored = { fingerprint, hashes: hashes.slice(0, -2) };
215
+ }
216
+ }
112
217
  pending = { fingerprint, hashes };
113
218
  // The stored reply sits between the messages already sent and the new ones.
114
219
  const held = stored ? stored.hashes.length + 1 : 0;
@@ -140,26 +245,46 @@ export function createAnthropicThreadSession(betaHeader) {
140
245
  headers,
141
246
  };
142
247
  },
143
- accept(messageId) {
248
+ accept(messageId, stopReason) {
144
249
  if (pending === undefined) {
145
250
  previousMessageId = undefined;
146
251
  stored = undefined;
252
+ lastStop = undefined;
147
253
  return;
148
254
  }
149
255
  previousMessageId = messageId;
150
256
  stored = pending;
151
257
  pending = undefined;
258
+ lastStop = stopReason;
152
259
  },
153
260
  reset() {
154
261
  previousMessageId = undefined;
155
262
  stored = undefined;
156
263
  pending = undefined;
264
+ lastStop = undefined;
157
265
  },
158
266
  disable() {
159
267
  enabled = false;
160
268
  previousMessageId = undefined;
161
269
  stored = undefined;
162
270
  pending = undefined;
271
+ lastStop = undefined;
272
+ },
273
+ carry(assistantText) {
274
+ if (!enabled ||
275
+ previousMessageId === undefined ||
276
+ stored === undefined ||
277
+ startAnchor === undefined ||
278
+ !extendsStart ||
279
+ lastStop === undefined ||
280
+ !TURN_ENDING_STOPS.has(lastStop)) {
281
+ return undefined;
282
+ }
283
+ return {
284
+ previousMessageId,
285
+ fingerprint: stored.fingerprint,
286
+ anchor: [...startAnchor, memoryHash("assistant", assistantText)],
287
+ };
163
288
  },
164
289
  };
165
290
  }
@@ -198,3 +323,120 @@ export function threadRecovery(error) {
198
323
  return undefined;
199
324
  }
200
325
  }
326
+ /** How long a thread is kept for the next call of its session. */
327
+ const SESSION_THREAD_TTL_MS = 4 * 60 * 1000; // the API drops one after 5 minutes idle
328
+ const SESSION_THREAD_MAX = 500;
329
+ /**
330
+ * The threads a NeuroLink instance has left behind for its sessions' next
331
+ * calls. In memory and per process only: another process simply creates a new
332
+ * thread, which is always correct. An entry is taken out by the call that
333
+ * continues it, because a response id can be continued once, so two concurrent
334
+ * calls of one session cannot both continue the same thread.
335
+ */
336
+ export class AnthropicThreadStore {
337
+ entries = new Map();
338
+ // Bumped when a session (or everything) is cleared. A call records the epoch
339
+ // when it starts and may leave a thread only if it is unchanged, so a stream
340
+ // that finishes after its conversation was cleared cannot bring the thread
341
+ // back. Coarse by design: a missed reuse costs one new thread.
342
+ globalEpoch = 0;
343
+ sessionEpochs = new Map();
344
+ sweepTimer;
345
+ /**
346
+ * The identity of a thread's owner: who asked, in which session, with which
347
+ * credential and endpoint. A thread belongs to the key that created it, so
348
+ * the credential is part of the key (hashed; the key itself is never kept).
349
+ */
350
+ static keyOf(parts) {
351
+ return sha([
352
+ parts.userId ?? "",
353
+ parts.sessionId,
354
+ parts.credential,
355
+ parts.baseURL ?? "",
356
+ ]);
357
+ }
358
+ take(key, now = Date.now()) {
359
+ const entry = this.entries.get(key);
360
+ if (entry === undefined) {
361
+ return undefined;
362
+ }
363
+ this.entries.delete(key);
364
+ return entry.expiresAt > now ? entry.carry : undefined;
365
+ }
366
+ /** The epoch a call must hand back to `put` for its thread to be kept. */
367
+ epochOf(sessionId) {
368
+ return `${this.globalEpoch}:${this.sessionEpochs.get(sessionId) ?? 0}`;
369
+ }
370
+ put(key, sessionId, carry, epoch, now = Date.now()) {
371
+ if (epoch !== this.epochOf(sessionId)) {
372
+ return;
373
+ }
374
+ this.sweep(now);
375
+ while (this.entries.size >= SESSION_THREAD_MAX) {
376
+ const oldest = this.entries.keys().next().value;
377
+ if (oldest === undefined) {
378
+ break;
379
+ }
380
+ this.entries.delete(oldest);
381
+ }
382
+ this.entries.set(key, {
383
+ sessionId,
384
+ carry,
385
+ expiresAt: now + SESSION_THREAD_TTL_MS,
386
+ });
387
+ this.armSweep();
388
+ }
389
+ /** Drop every entry that has expired. */
390
+ sweep(now = Date.now()) {
391
+ for (const [key, entry] of this.entries) {
392
+ if (entry.expiresAt <= now) {
393
+ this.entries.delete(key);
394
+ }
395
+ }
396
+ }
397
+ // An idle store still lets go of its entries when they expire. The timer is
398
+ // unref'd, so it never keeps a process alive, and it stops once empty.
399
+ armSweep() {
400
+ if (this.sweepTimer !== undefined) {
401
+ return;
402
+ }
403
+ const timer = setTimeout(() => {
404
+ this.sweepTimer = undefined;
405
+ this.sweep();
406
+ if (this.entries.size > 0) {
407
+ this.armSweep();
408
+ }
409
+ }, SESSION_THREAD_TTL_MS + 1000);
410
+ timer.unref?.();
411
+ this.sweepTimer = timer;
412
+ }
413
+ /**
414
+ * Forget a session's thread, or every thread when no session is named, and
415
+ * fence any call still running from leaving one afterwards.
416
+ */
417
+ forget(sessionId) {
418
+ if (sessionId === undefined) {
419
+ this.globalEpoch++;
420
+ this.sessionEpochs.clear();
421
+ this.entries.clear();
422
+ if (this.sweepTimer !== undefined) {
423
+ clearTimeout(this.sweepTimer);
424
+ this.sweepTimer = undefined;
425
+ }
426
+ return;
427
+ }
428
+ if (this.sessionEpochs.size >= SESSION_THREAD_MAX) {
429
+ this.globalEpoch++;
430
+ this.sessionEpochs.clear();
431
+ }
432
+ this.sessionEpochs.set(sessionId, (this.sessionEpochs.get(sessionId) ?? 0) + 1);
433
+ for (const [key, entry] of this.entries) {
434
+ if (entry.sessionId === sessionId) {
435
+ this.entries.delete(key);
436
+ }
437
+ }
438
+ }
439
+ get size() {
440
+ return this.entries.size;
441
+ }
442
+ }
@@ -68,11 +68,11 @@ export declare const providerCatalogJsonSchema: z.ZodObject<{
68
68
  pattern: z.ZodOptional<z.ZodString>;
69
69
  patternStatuses: z.ZodOptional<z.ZodArray<z.ZodNumber>>;
70
70
  class: z.ZodEnum<{
71
- network: "network";
72
71
  provider: "provider";
73
72
  authentication: "authentication";
74
73
  "rate-limit": "rate-limit";
75
74
  "invalid-model": "invalid-model";
75
+ network: "network";
76
76
  }>;
77
77
  message: z.ZodString;
78
78
  }, z.core.$strict>>;
@@ -162,7 +162,7 @@ export declare class SageMakerLanguageModel implements SageMakerAsLanguageModel
162
162
  provider: string;
163
163
  specificationVersion: "v2";
164
164
  endpointName: string;
165
- modelType: "huggingface" | "mistral" | "custom" | "llama" | "claude" | "jumpstart" | undefined;
165
+ modelType: "custom" | "huggingface" | "mistral" | "llama" | "claude" | "jumpstart" | undefined;
166
166
  region: string;
167
167
  };
168
168
  /**
@@ -209,7 +209,7 @@ export declare class SageMakerLanguageModel implements SageMakerAsLanguageModel
209
209
  provider: string;
210
210
  specificationVersion: "v2";
211
211
  endpointName: string;
212
- modelType: "huggingface" | "mistral" | "custom" | "llama" | "claude" | "jumpstart" | undefined;
212
+ modelType: "custom" | "huggingface" | "mistral" | "llama" | "claude" | "jumpstart" | undefined;
213
213
  region: string;
214
214
  };
215
215
  }
@@ -63,7 +63,7 @@ export declare function getProxyOtelLogSnapshot(): {
63
63
  lastFailureAt: string | undefined;
64
64
  highWaterOutstanding: number;
65
65
  failureHistoryEvicted: number;
66
- kind: "metadata" | "bodies" | "indexes" | "diagnostics";
66
+ kind: "metadata" | "indexes" | "bodies" | "diagnostics";
67
67
  capacity: number;
68
68
  byteCapacity: number;
69
69
  retentionMs: number;
@@ -259,8 +259,8 @@ export declare const TranscribeConfigSchema: z.ZodObject<{
259
259
  correction: z.ZodOptional<z.ZodObject<{
260
260
  enabled: z.ZodOptional<z.ZodBoolean>;
261
261
  guard: z.ZodOptional<z.ZodEnum<{
262
- none: "none";
263
262
  decide: "decide";
263
+ none: "none";
264
264
  }>>;
265
265
  decideTimeoutMs: z.ZodOptional<z.ZodNumber>;
266
266
  rewrite: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodLiteral<"auto">, z.ZodObject<{
@@ -268,14 +268,14 @@ export declare const TranscribeConfigSchema: z.ZodObject<{
268
268
  model: z.ZodOptional<z.ZodString>;
269
269
  timeoutMs: z.ZodOptional<z.ZodNumber>;
270
270
  mode: z.ZodOptional<z.ZodEnum<{
271
- auto: "auto";
272
271
  always: "always";
272
+ auto: "auto";
273
273
  }>>;
274
274
  }, z.core.$strip>]>>;
275
275
  perSentence: z.ZodOptional<z.ZodBoolean>;
276
276
  transliterate: z.ZodOptional<z.ZodEnum<{
277
- native: "native";
278
277
  latin: "latin";
278
+ native: "native";
279
279
  }>>;
280
280
  punctuate: z.ZodOptional<z.ZodBoolean>;
281
281
  cleanup: z.ZodOptional<z.ZodEnum<{
@@ -295,8 +295,8 @@ export declare const TranscribeConfigSchema: z.ZodObject<{
295
295
  model: z.ZodOptional<z.ZodString>;
296
296
  when: z.ZodOptional<z.ZodArray<z.ZodEnum<{
297
297
  error: "error";
298
- empty: "empty";
299
298
  unsure: "unsure";
299
+ empty: "empty";
300
300
  }>>>;
301
301
  }, z.core.$strip>>;
302
302
  routing: z.ZodOptional<z.ZodObject<{
@@ -340,8 +340,8 @@ export declare const TranscribeRequestSchema: z.ZodObject<{
340
340
  correction: z.ZodOptional<z.ZodObject<{
341
341
  enabled: z.ZodOptional<z.ZodBoolean>;
342
342
  guard: z.ZodOptional<z.ZodEnum<{
343
- none: "none";
344
343
  decide: "decide";
344
+ none: "none";
345
345
  }>>;
346
346
  decideTimeoutMs: z.ZodOptional<z.ZodNumber>;
347
347
  rewrite: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodLiteral<"auto">, z.ZodObject<{
@@ -349,14 +349,14 @@ export declare const TranscribeRequestSchema: z.ZodObject<{
349
349
  model: z.ZodOptional<z.ZodString>;
350
350
  timeoutMs: z.ZodOptional<z.ZodNumber>;
351
351
  mode: z.ZodOptional<z.ZodEnum<{
352
- auto: "auto";
353
352
  always: "always";
353
+ auto: "auto";
354
354
  }>>;
355
355
  }, z.core.$strip>]>>;
356
356
  perSentence: z.ZodOptional<z.ZodBoolean>;
357
357
  transliterate: z.ZodOptional<z.ZodEnum<{
358
- native: "native";
359
358
  latin: "latin";
359
+ native: "native";
360
360
  }>>;
361
361
  punctuate: z.ZodOptional<z.ZodBoolean>;
362
362
  cleanup: z.ZodOptional<z.ZodEnum<{
@@ -376,8 +376,8 @@ export declare const TranscribeRequestSchema: z.ZodObject<{
376
376
  model: z.ZodOptional<z.ZodString>;
377
377
  when: z.ZodOptional<z.ZodArray<z.ZodEnum<{
378
378
  error: "error";
379
- empty: "empty";
380
379
  unsure: "unsure";
380
+ empty: "empty";
381
381
  }>>>;
382
382
  }, z.core.$strip>>;
383
383
  routing: z.ZodOptional<z.ZodObject<{
@@ -437,8 +437,8 @@ export declare const TranscribeStreamConfigSchema: z.ZodObject<{
437
437
  correction: z.ZodOptional<z.ZodObject<{
438
438
  enabled: z.ZodOptional<z.ZodBoolean>;
439
439
  guard: z.ZodOptional<z.ZodEnum<{
440
- none: "none";
441
440
  decide: "decide";
441
+ none: "none";
442
442
  }>>;
443
443
  decideTimeoutMs: z.ZodOptional<z.ZodNumber>;
444
444
  rewrite: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodLiteral<"auto">, z.ZodObject<{
@@ -446,14 +446,14 @@ export declare const TranscribeStreamConfigSchema: z.ZodObject<{
446
446
  model: z.ZodOptional<z.ZodString>;
447
447
  timeoutMs: z.ZodOptional<z.ZodNumber>;
448
448
  mode: z.ZodOptional<z.ZodEnum<{
449
- auto: "auto";
450
449
  always: "always";
450
+ auto: "auto";
451
451
  }>>;
452
452
  }, z.core.$strip>]>>;
453
453
  perSentence: z.ZodOptional<z.ZodBoolean>;
454
454
  transliterate: z.ZodOptional<z.ZodEnum<{
455
- native: "native";
456
455
  latin: "latin";
456
+ native: "native";
457
457
  }>>;
458
458
  punctuate: z.ZodOptional<z.ZodBoolean>;
459
459
  cleanup: z.ZodOptional<z.ZodEnum<{
@@ -473,8 +473,8 @@ export declare const TranscribeStreamConfigSchema: z.ZodObject<{
473
473
  model: z.ZodOptional<z.ZodString>;
474
474
  when: z.ZodOptional<z.ZodArray<z.ZodEnum<{
475
475
  error: "error";
476
- empty: "empty";
477
476
  unsure: "unsure";
477
+ empty: "empty";
478
478
  }>>>;
479
479
  }, z.core.$strip>>;
480
480
  routing: z.ZodOptional<z.ZodObject<{
@@ -535,7 +535,6 @@ export declare const TranscribeStreamConfigSchema: z.ZodObject<{
535
535
  }, z.core.$strip>>;
536
536
  }, z.core.$strip>;
537
537
  export declare const STTRealtimeProfileSchema: z.ZodObject<{
538
- timeoutMs: z.ZodOptional<z.ZodNumber>;
539
538
  provider: z.ZodOptional<z.ZodString>;
540
539
  output: z.ZodOptional<z.ZodObject<{
541
540
  languages: z.ZodArray<z.ZodString>;
@@ -574,10 +573,9 @@ export declare const STTRealtimeProfileSchema: z.ZodObject<{
574
573
  }, z.core.$strip>>;
575
574
  normalize: z.ZodOptional<z.ZodBoolean>;
576
575
  }, z.core.$strip>>;
577
- prompt: z.ZodOptional<z.ZodString>;
578
576
  language: z.ZodOptional<z.ZodString>;
579
- diarization: z.ZodOptional<z.ZodBoolean>;
580
- wordTimestamps: z.ZodOptional<z.ZodBoolean>;
577
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
578
+ prompt: z.ZodOptional<z.ZodString>;
581
579
  dictionary: z.ZodOptional<z.ZodArray<z.ZodObject<{
582
580
  term: z.ZodString;
583
581
  heardAs: z.ZodOptional<z.ZodArray<z.ZodString>>;
@@ -586,8 +584,8 @@ export declare const STTRealtimeProfileSchema: z.ZodObject<{
586
584
  correction: z.ZodOptional<z.ZodObject<{
587
585
  enabled: z.ZodOptional<z.ZodBoolean>;
588
586
  guard: z.ZodOptional<z.ZodEnum<{
589
- none: "none";
590
587
  decide: "decide";
588
+ none: "none";
591
589
  }>>;
592
590
  decideTimeoutMs: z.ZodOptional<z.ZodNumber>;
593
591
  rewrite: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodLiteral<"auto">, z.ZodObject<{
@@ -595,14 +593,14 @@ export declare const STTRealtimeProfileSchema: z.ZodObject<{
595
593
  model: z.ZodOptional<z.ZodString>;
596
594
  timeoutMs: z.ZodOptional<z.ZodNumber>;
597
595
  mode: z.ZodOptional<z.ZodEnum<{
598
- auto: "auto";
599
596
  always: "always";
597
+ auto: "auto";
600
598
  }>>;
601
599
  }, z.core.$strip>]>>;
602
600
  perSentence: z.ZodOptional<z.ZodBoolean>;
603
601
  transliterate: z.ZodOptional<z.ZodEnum<{
604
- native: "native";
605
602
  latin: "latin";
603
+ native: "native";
606
604
  }>>;
607
605
  punctuate: z.ZodOptional<z.ZodBoolean>;
608
606
  cleanup: z.ZodOptional<z.ZodEnum<{
@@ -622,8 +620,8 @@ export declare const STTRealtimeProfileSchema: z.ZodObject<{
622
620
  model: z.ZodOptional<z.ZodString>;
623
621
  when: z.ZodOptional<z.ZodArray<z.ZodEnum<{
624
622
  error: "error";
625
- empty: "empty";
626
623
  unsure: "unsure";
624
+ empty: "empty";
627
625
  }>>>;
628
626
  }, z.core.$strip>>;
629
627
  routing: z.ZodOptional<z.ZodObject<{
@@ -639,6 +637,8 @@ export declare const STTRealtimeProfileSchema: z.ZodObject<{
639
637
  engine: "engine";
640
638
  utterances: "utterances";
641
639
  }>>;
640
+ diarization: z.ZodOptional<z.ZodBoolean>;
641
+ wordTimestamps: z.ZodOptional<z.ZodBoolean>;
642
642
  completed: z.ZodOptional<z.ZodEnum<{
643
643
  final: "final";
644
644
  corrected: "corrected";
@@ -648,7 +648,6 @@ export declare const STTRealtimeProfileSchema: z.ZodObject<{
648
648
  clientTurnDetection: z.ZodOptional<z.ZodBoolean>;
649
649
  }, z.core.$strip>;
650
650
  export declare const STTRealtimeProfilesSchema: z.ZodRecord<z.ZodString, z.ZodObject<{
651
- timeoutMs: z.ZodOptional<z.ZodNumber>;
652
651
  provider: z.ZodOptional<z.ZodString>;
653
652
  output: z.ZodOptional<z.ZodObject<{
654
653
  languages: z.ZodArray<z.ZodString>;
@@ -687,10 +686,9 @@ export declare const STTRealtimeProfilesSchema: z.ZodRecord<z.ZodString, z.ZodOb
687
686
  }, z.core.$strip>>;
688
687
  normalize: z.ZodOptional<z.ZodBoolean>;
689
688
  }, z.core.$strip>>;
690
- prompt: z.ZodOptional<z.ZodString>;
691
689
  language: z.ZodOptional<z.ZodString>;
692
- diarization: z.ZodOptional<z.ZodBoolean>;
693
- wordTimestamps: z.ZodOptional<z.ZodBoolean>;
690
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
691
+ prompt: z.ZodOptional<z.ZodString>;
694
692
  dictionary: z.ZodOptional<z.ZodArray<z.ZodObject<{
695
693
  term: z.ZodString;
696
694
  heardAs: z.ZodOptional<z.ZodArray<z.ZodString>>;
@@ -699,8 +697,8 @@ export declare const STTRealtimeProfilesSchema: z.ZodRecord<z.ZodString, z.ZodOb
699
697
  correction: z.ZodOptional<z.ZodObject<{
700
698
  enabled: z.ZodOptional<z.ZodBoolean>;
701
699
  guard: z.ZodOptional<z.ZodEnum<{
702
- none: "none";
703
700
  decide: "decide";
701
+ none: "none";
704
702
  }>>;
705
703
  decideTimeoutMs: z.ZodOptional<z.ZodNumber>;
706
704
  rewrite: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodLiteral<"auto">, z.ZodObject<{
@@ -708,14 +706,14 @@ export declare const STTRealtimeProfilesSchema: z.ZodRecord<z.ZodString, z.ZodOb
708
706
  model: z.ZodOptional<z.ZodString>;
709
707
  timeoutMs: z.ZodOptional<z.ZodNumber>;
710
708
  mode: z.ZodOptional<z.ZodEnum<{
711
- auto: "auto";
712
709
  always: "always";
710
+ auto: "auto";
713
711
  }>>;
714
712
  }, z.core.$strip>]>>;
715
713
  perSentence: z.ZodOptional<z.ZodBoolean>;
716
714
  transliterate: z.ZodOptional<z.ZodEnum<{
717
- native: "native";
718
715
  latin: "latin";
716
+ native: "native";
719
717
  }>>;
720
718
  punctuate: z.ZodOptional<z.ZodBoolean>;
721
719
  cleanup: z.ZodOptional<z.ZodEnum<{
@@ -735,8 +733,8 @@ export declare const STTRealtimeProfilesSchema: z.ZodRecord<z.ZodString, z.ZodOb
735
733
  model: z.ZodOptional<z.ZodString>;
736
734
  when: z.ZodOptional<z.ZodArray<z.ZodEnum<{
737
735
  error: "error";
738
- empty: "empty";
739
736
  unsure: "unsure";
737
+ empty: "empty";
740
738
  }>>>;
741
739
  }, z.core.$strip>>;
742
740
  routing: z.ZodOptional<z.ZodObject<{
@@ -752,6 +750,8 @@ export declare const STTRealtimeProfilesSchema: z.ZodRecord<z.ZodString, z.ZodOb
752
750
  engine: "engine";
753
751
  utterances: "utterances";
754
752
  }>>;
753
+ diarization: z.ZodOptional<z.ZodBoolean>;
754
+ wordTimestamps: z.ZodOptional<z.ZodBoolean>;
755
755
  completed: z.ZodOptional<z.ZodEnum<{
756
756
  final: "final";
757
757
  corrected: "corrected";
@@ -30,6 +30,14 @@ import type { ProcessorPipelineConfig } from "./ioProcessor.js";
30
30
  * Supports multimodal content while maintaining backward compatibility
31
31
  */
32
32
  export type GenerateOptions = {
33
+ /**
34
+ * Send this call's steps as an Anthropic message thread (beta); `"session"`
35
+ * also keeps the thread for the session's next call. The same option and the
36
+ * same limits as `StreamOptions.messageThreads`: native Anthropic with
37
+ * API-key auth only, off by default, not zero-data-retention eligible, and no
38
+ * way to delete a thread. See that option for the trade-offs.
39
+ */
40
+ messageThreads?: boolean | "session";
33
41
  /**
34
42
  * Input content for generation. Optional for media-only modes (avatar, music,
35
43
  * video) where all configuration lives in `output`; the SDK synthesises an
@@ -1222,6 +1230,14 @@ export type FactoryEnhancedProvider = EnhancedProvider & {
1222
1230
  * Extended to support video generation mode
1223
1231
  */
1224
1232
  export type TextGenerationOptions = {
1233
+ /**
1234
+ * Send this call's steps as an Anthropic message thread (beta); `"session"`
1235
+ * also keeps the thread for the session's next call. The same option and the
1236
+ * same limits as `StreamOptions.messageThreads`: native Anthropic with
1237
+ * API-key auth only, off by default, not zero-data-retention eligible, and no
1238
+ * way to delete a thread. See that option for the trade-offs.
1239
+ */
1240
+ messageThreads?: boolean | "session";
1225
1241
  prompt?: string;
1226
1242
  /**
1227
1243
  * Fraction of the model's context window at which compaction runs for this
@@ -255,6 +255,23 @@ export type AnthropicThreadPlan = {
255
255
  /** The `anthropic-beta` value for this request: the client's own plus the thread beta. */
256
256
  headers: Record<string, string>;
257
257
  };
258
+ /**
259
+ * What one call hands the next call of the same session so it can continue the
260
+ * thread instead of creating another: the last stored response, the fixed
261
+ * fields the thread was created with, and a hash of each message the call
262
+ * started from (the history conversation memory rebuilt for it).
263
+ */
264
+ export type AnthropicThreadCarry = {
265
+ previousMessageId: string;
266
+ fingerprint: string;
267
+ anchor: string[];
268
+ };
269
+ /** A thread a NeuroLink instance keeps for its session's next call. */
270
+ export type AnthropicStoredThread = {
271
+ sessionId: string;
272
+ carry: AnthropicThreadCarry;
273
+ expiresAt: number;
274
+ };
258
275
  /**
259
276
  * The state of one Anthropic message thread across the steps of a turn.
260
277
  *
@@ -265,9 +282,19 @@ export type AnthropicThreadPlan = {
265
282
  export type AnthropicThreadSession = {
266
283
  /** False once the API has said it cannot thread this request. */
267
284
  readonly enabled: boolean;
285
+ /**
286
+ * What the next call of the same session needs to continue this thread, or
287
+ * undefined when it cannot: the turn did not end on a normal stop, or no
288
+ * response was stored. `assistantText` is the text the turn streamed, which
289
+ * is what conversation memory will hand back as the assistant's reply.
290
+ */
291
+ carry(assistantText: string): AnthropicThreadCarry | undefined;
268
292
  plan(params: Anthropic.Messages.MessageCreateParams & AnthropicSamplingParams): AnthropicThreadPlan;
269
- /** The response was complete and stored: the next step may continue from it. */
270
- accept(messageId: string): void;
293
+ /**
294
+ * The response was complete and stored: the next step may continue from it.
295
+ * `stopReason` says whether it also ended the turn.
296
+ */
297
+ accept(messageId: string, stopReason?: string): void;
271
298
  /** The next plan is a `create`. */
272
299
  reset(): void;
273
300
  /** Stop threading for the rest of the turn. */