blazen 0.5.4 → 0.6.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/error-classes.js DELETED
@@ -1,966 +0,0 @@
1
- 'use strict'
2
-
3
- // Typed JS error classes for Blazen.
4
- //
5
- // The Rust binding (see `crates/blazen-node/src/error.rs`) prefixes every
6
- // `napi::Error` message with a `[ClassName]` tag. This file:
7
- // 1. Defines a class hierarchy that mirrors the Python binding's
8
- // `pyo3::create_exception!` tree (the source of truth).
9
- // 2. Exposes `enrichError(err)` which, given a plain `Error` thrown by a
10
- // napi-rs function, parses the leading `[Tag]` prefix and rethrows a
11
- // typed instance whose `message` is the original message minus the
12
- // prefix.
13
- //
14
- // Consumers can do `err instanceof BlazenError`, `err instanceof ProviderError`,
15
- // or check the concrete leaf type (e.g. `LlamaCppModelLoadError`).
16
- //
17
- // The `[ProviderError]` tag is also emitted alongside a sentinel JSON line
18
- // for HTTP/provider errors -- see `error.rs` `PROVIDER_ERROR_SENTINEL`.
19
- // `enrichError` extracts the sentinel JSON (when present), attaches the
20
- // structured fields (`provider`, `status`, `endpoint`, `requestId`,
21
- // `detail`, `retryAfterMs`) onto the resulting `ProviderError` instance,
22
- // and strips the sentinel line from the user-visible message.
23
-
24
- const PROVIDER_ERROR_SENTINEL = '__BLAZEN_PROVIDER_ERROR__'
25
-
26
- // Sentinel + stash for caller-error preservation. The napi side
27
- // formats `__BLAZEN_CALLER_ERROR__ {json} \n [CallerError] msg` when a
28
- // user's tool handler threw. The JSON `ref` is a UUID that maps to an
29
- // entry in `callerErrorStash` (populated by `wrapToolHandlerForCallerErrors`
30
- // below); the stashed value is the ORIGINAL JS Error instance -- so
31
- // `enrichError` can re-throw it verbatim, preserving `instanceof MyError`
32
- // and all custom properties. See `crates/blazen-node/src/error.rs`
33
- // CALLER_ERROR_SENTINEL.
34
- const CALLER_ERROR_SENTINEL = '__BLAZEN_CALLER_ERROR__'
35
-
36
- // Module-level Map keyed by UUID strings. Values are the original
37
- // thrown JS Error instances. Each entry is added by
38
- // `wrapToolHandlerForCallerErrors` and deleted in `enrichError` after
39
- // re-throw (or fall-back). Entries that escape the stash because the
40
- // agent loop produced a non-CallerError outcome are cleaned up by the
41
- // `runAgent` wrapper's `.finally(...)` (see `wrapRunAgentForCallerErrors`
42
- // in index.js).
43
- const callerErrorStash = new Map()
44
-
45
- // Build a fresh UUID string suitable for Map keys. Uses the built-in
46
- // crypto.randomUUID() when available (Node 14.17+); falls back to a
47
- // simple time+random pattern otherwise.
48
- function freshUuid() {
49
- try {
50
- // eslint-disable-next-line global-require
51
- const { randomUUID } = require('crypto')
52
- if (typeof randomUUID === 'function') {
53
- return randomUUID()
54
- }
55
- } catch {
56
- // Fall through.
57
- }
58
- return `caller-${Date.now()}-${Math.random().toString(36).slice(2)}`
59
- }
60
-
61
- // Wrap a user-supplied tool handler so any thrown / rejected error is
62
- // captured as an envelope `{__blazenOk: false, errorRef: uuid, errorName,
63
- // errorMessage}` and stashed for `enrichError` to re-throw. Success
64
- // returns become `{__blazenOk: true, value: <user's return>}`.
65
- //
66
- // Returns a freshly-wrapped function (does NOT mutate the user's
67
- // handler). The wrapped function is what the napi `runAgent` /
68
- // `runAgentWithCallback` Rust side sees.
69
- //
70
- // The wrapped function returns a Promise (always) so the napi
71
- // `Promise<serde_json::Value>` resolution path is uniform regardless of
72
- // whether the user's handler is sync or async.
73
- function wrapToolHandlerForCallerErrors(handler) {
74
- if (typeof handler !== 'function') {
75
- return handler
76
- }
77
- return async function blazenEnvelopeToolHandler(...args) {
78
- try {
79
- const value = await handler.apply(this, args)
80
- return { __blazenOk: true, value }
81
- } catch (error) {
82
- const ref = freshUuid()
83
- callerErrorStash.set(ref, error)
84
- const errorName =
85
- (error && typeof error === 'object' && typeof error.name === 'string')
86
- ? error.name
87
- : undefined
88
- const errorMessage =
89
- (error && typeof error === 'object' && typeof error.message === 'string')
90
- ? error.message
91
- : String(error)
92
- return { __blazenOk: false, errorRef: ref, errorName, errorMessage }
93
- }
94
- }
95
- }
96
-
97
- // ---------------------------------------------------------------------------
98
- // Class hierarchy
99
- // ---------------------------------------------------------------------------
100
-
101
- class BlazenError extends Error {
102
- constructor(message) {
103
- super(message)
104
- this.name = 'BlazenError'
105
- }
106
- }
107
-
108
- class AuthError extends BlazenError {
109
- constructor(message) {
110
- super(message)
111
- this.name = 'AuthError'
112
- }
113
- }
114
-
115
- class RateLimitError extends BlazenError {
116
- constructor(message) {
117
- super(message)
118
- this.name = 'RateLimitError'
119
- }
120
- }
121
-
122
- class TimeoutError extends BlazenError {
123
- constructor(message) {
124
- super(message)
125
- this.name = 'TimeoutError'
126
- }
127
- }
128
-
129
- class ValidationError extends BlazenError {
130
- constructor(message) {
131
- super(message)
132
- this.name = 'ValidationError'
133
- }
134
- }
135
-
136
- class ContentPolicyError extends BlazenError {
137
- constructor(message) {
138
- super(message)
139
- this.name = 'ContentPolicyError'
140
- }
141
- }
142
-
143
- class UnsupportedError extends BlazenError {
144
- constructor(message) {
145
- super(message)
146
- this.name = 'UnsupportedError'
147
- }
148
- }
149
-
150
- class ComputeError extends BlazenError {
151
- constructor(message) {
152
- super(message)
153
- this.name = 'ComputeError'
154
- }
155
- }
156
-
157
- class MediaError extends BlazenError {
158
- constructor(message) {
159
- super(message)
160
- this.name = 'MediaError'
161
- }
162
- }
163
-
164
- class ProviderError extends BlazenError {
165
- constructor(message) {
166
- super(message)
167
- this.name = 'ProviderError'
168
- // Structured fields populated from the sentinel JSON when present.
169
- // Always defined (defaulting to `null`) so consumers can rely on the
170
- // shape without `in` / `hasOwnProperty` checks.
171
- this.provider = null
172
- this.status = null
173
- this.endpoint = null
174
- this.requestId = null
175
- this.detail = null
176
- this.retryAfterMs = null
177
- }
178
- }
179
-
180
- // Per-backend provider subclasses. All extend `ProviderError` so callers
181
- // that don't care which backend failed can keep one `instanceof
182
- // ProviderError` arm.
183
-
184
- class LlamaCppError extends ProviderError {
185
- constructor(message) {
186
- super(message)
187
- this.name = 'LlamaCppError'
188
- }
189
- }
190
- class LlamaCppInvalidOptionsError extends LlamaCppError {
191
- constructor(message) {
192
- super(message)
193
- this.name = 'LlamaCppInvalidOptionsError'
194
- }
195
- }
196
- class LlamaCppModelLoadError extends LlamaCppError {
197
- constructor(message) {
198
- super(message)
199
- this.name = 'LlamaCppModelLoadError'
200
- }
201
- }
202
- class LlamaCppInferenceError extends LlamaCppError {
203
- constructor(message) {
204
- super(message)
205
- this.name = 'LlamaCppInferenceError'
206
- }
207
- }
208
- class LlamaCppEngineNotAvailableError extends LlamaCppError {
209
- constructor(message) {
210
- super(message)
211
- this.name = 'LlamaCppEngineNotAvailableError'
212
- }
213
- }
214
-
215
- class CandleLlmError extends ProviderError {
216
- constructor(message) {
217
- super(message)
218
- this.name = 'CandleLlmError'
219
- }
220
- }
221
- class CandleLlmInvalidOptionsError extends CandleLlmError {
222
- constructor(message) {
223
- super(message)
224
- this.name = 'CandleLlmInvalidOptionsError'
225
- }
226
- }
227
- class CandleLlmModelLoadError extends CandleLlmError {
228
- constructor(message) {
229
- super(message)
230
- this.name = 'CandleLlmModelLoadError'
231
- }
232
- }
233
- class CandleLlmInferenceError extends CandleLlmError {
234
- constructor(message) {
235
- super(message)
236
- this.name = 'CandleLlmInferenceError'
237
- }
238
- }
239
- class CandleLlmEngineNotAvailableError extends CandleLlmError {
240
- constructor(message) {
241
- super(message)
242
- this.name = 'CandleLlmEngineNotAvailableError'
243
- }
244
- }
245
-
246
- class CandleEmbedError extends ProviderError {
247
- constructor(message) {
248
- super(message)
249
- this.name = 'CandleEmbedError'
250
- }
251
- }
252
- class CandleEmbedInvalidOptionsError extends CandleEmbedError {
253
- constructor(message) {
254
- super(message)
255
- this.name = 'CandleEmbedInvalidOptionsError'
256
- }
257
- }
258
- class CandleEmbedModelLoadError extends CandleEmbedError {
259
- constructor(message) {
260
- super(message)
261
- this.name = 'CandleEmbedModelLoadError'
262
- }
263
- }
264
- class CandleEmbedEmbeddingError extends CandleEmbedError {
265
- constructor(message) {
266
- super(message)
267
- this.name = 'CandleEmbedEmbeddingError'
268
- }
269
- }
270
- class CandleEmbedEngineNotAvailableError extends CandleEmbedError {
271
- constructor(message) {
272
- super(message)
273
- this.name = 'CandleEmbedEngineNotAvailableError'
274
- }
275
- }
276
- class CandleEmbedTaskPanickedError extends CandleEmbedError {
277
- constructor(message) {
278
- super(message)
279
- this.name = 'CandleEmbedTaskPanickedError'
280
- }
281
- }
282
-
283
- class MistralRsError extends ProviderError {
284
- constructor(message) {
285
- super(message)
286
- this.name = 'MistralRsError'
287
- }
288
- }
289
- class MistralRsInvalidOptionsError extends MistralRsError {
290
- constructor(message) {
291
- super(message)
292
- this.name = 'MistralRsInvalidOptionsError'
293
- }
294
- }
295
- class MistralRsInitError extends MistralRsError {
296
- constructor(message) {
297
- super(message)
298
- this.name = 'MistralRsInitError'
299
- }
300
- }
301
- class MistralRsInferenceError extends MistralRsError {
302
- constructor(message) {
303
- super(message)
304
- this.name = 'MistralRsInferenceError'
305
- }
306
- }
307
- class MistralRsEngineNotAvailableError extends MistralRsError {
308
- constructor(message) {
309
- super(message)
310
- this.name = 'MistralRsEngineNotAvailableError'
311
- }
312
- }
313
-
314
- class WhisperError extends ProviderError {
315
- constructor(message) {
316
- super(message)
317
- this.name = 'WhisperError'
318
- }
319
- }
320
- class WhisperInvalidOptionsError extends WhisperError {
321
- constructor(message) {
322
- super(message)
323
- this.name = 'WhisperInvalidOptionsError'
324
- }
325
- }
326
- class WhisperModelLoadError extends WhisperError {
327
- constructor(message) {
328
- super(message)
329
- this.name = 'WhisperModelLoadError'
330
- }
331
- }
332
- class WhisperTranscriptionError extends WhisperError {
333
- constructor(message) {
334
- super(message)
335
- this.name = 'WhisperTranscriptionError'
336
- }
337
- }
338
- class WhisperEngineNotAvailableError extends WhisperError {
339
- constructor(message) {
340
- super(message)
341
- this.name = 'WhisperEngineNotAvailableError'
342
- }
343
- }
344
- class WhisperIoError extends WhisperError {
345
- constructor(message) {
346
- super(message)
347
- this.name = 'WhisperIoError'
348
- }
349
- }
350
-
351
- class PiperError extends ProviderError {
352
- constructor(message) {
353
- super(message)
354
- this.name = 'PiperError'
355
- }
356
- }
357
- class PiperInvalidOptionsError extends PiperError {
358
- constructor(message) {
359
- super(message)
360
- this.name = 'PiperInvalidOptionsError'
361
- }
362
- }
363
- class PiperModelLoadError extends PiperError {
364
- constructor(message) {
365
- super(message)
366
- this.name = 'PiperModelLoadError'
367
- }
368
- }
369
- class PiperSynthesisError extends PiperError {
370
- constructor(message) {
371
- super(message)
372
- this.name = 'PiperSynthesisError'
373
- }
374
- }
375
- class PiperEngineNotAvailableError extends PiperError {
376
- constructor(message) {
377
- super(message)
378
- this.name = 'PiperEngineNotAvailableError'
379
- }
380
- }
381
-
382
- class DiffusionError extends ProviderError {
383
- constructor(message) {
384
- super(message)
385
- this.name = 'DiffusionError'
386
- }
387
- }
388
- class DiffusionInvalidOptionsError extends DiffusionError {
389
- constructor(message) {
390
- super(message)
391
- this.name = 'DiffusionInvalidOptionsError'
392
- }
393
- }
394
- class DiffusionModelLoadError extends DiffusionError {
395
- constructor(message) {
396
- super(message)
397
- this.name = 'DiffusionModelLoadError'
398
- }
399
- }
400
- class DiffusionGenerationError extends DiffusionError {
401
- constructor(message) {
402
- super(message)
403
- this.name = 'DiffusionGenerationError'
404
- }
405
- }
406
-
407
- // fastembed (non-musl) and tract (musl) both surface as `Embed*` tags
408
- // from the Rust side. We expose them both as `FastEmbedError` (matching
409
- // the Python binding's name) AND as the underlying `Embed*` tag classes
410
- // so consumers who already check the per-tag class keep working.
411
- class FastEmbedError extends ProviderError {
412
- constructor(message) {
413
- super(message)
414
- this.name = 'FastEmbedError'
415
- }
416
- }
417
- class EmbedUnknownModelError extends FastEmbedError {
418
- constructor(message) {
419
- super(message)
420
- this.name = 'EmbedUnknownModelError'
421
- }
422
- }
423
- class EmbedInitError extends FastEmbedError {
424
- constructor(message) {
425
- super(message)
426
- this.name = 'EmbedInitError'
427
- }
428
- }
429
- class EmbedEmbedError extends FastEmbedError {
430
- constructor(message) {
431
- super(message)
432
- this.name = 'EmbedEmbedError'
433
- }
434
- }
435
- class EmbedMutexPoisonedError extends FastEmbedError {
436
- constructor(message) {
437
- super(message)
438
- this.name = 'EmbedMutexPoisonedError'
439
- }
440
- }
441
- class EmbedTaskPanickedError extends FastEmbedError {
442
- constructor(message) {
443
- super(message)
444
- this.name = 'EmbedTaskPanickedError'
445
- }
446
- }
447
-
448
- class TractError extends ProviderError {
449
- constructor(message) {
450
- super(message)
451
- this.name = 'TractError'
452
- }
453
- }
454
-
455
- // Peer subsystem
456
- class PeerEncodeError extends BlazenError {
457
- constructor(message) {
458
- super(message)
459
- this.name = 'PeerEncodeError'
460
- }
461
- }
462
- class PeerTransportError extends BlazenError {
463
- constructor(message) {
464
- super(message)
465
- this.name = 'PeerTransportError'
466
- }
467
- }
468
- class PeerEnvelopeVersionError extends BlazenError {
469
- constructor(message) {
470
- super(message)
471
- this.name = 'PeerEnvelopeVersionError'
472
- }
473
- }
474
- class PeerWorkflowError extends BlazenError {
475
- constructor(message) {
476
- super(message)
477
- this.name = 'PeerWorkflowError'
478
- }
479
- }
480
- class PeerTlsError extends BlazenError {
481
- constructor(message) {
482
- super(message)
483
- this.name = 'PeerTlsError'
484
- }
485
- }
486
- class PeerUnknownStepError extends BlazenError {
487
- constructor(message) {
488
- super(message)
489
- this.name = 'PeerUnknownStepError'
490
- }
491
- }
492
-
493
- // Persist
494
- class PersistError extends BlazenError {
495
- constructor(message) {
496
- super(message)
497
- this.name = 'PersistError'
498
- }
499
- }
500
-
501
- // Prompts
502
- class PromptError extends BlazenError {
503
- constructor(message) {
504
- super(message)
505
- this.name = 'PromptError'
506
- }
507
- }
508
- class PromptMissingVariableError extends PromptError {
509
- constructor(message) {
510
- super(message)
511
- this.name = 'PromptMissingVariableError'
512
- }
513
- }
514
- class PromptNotFoundError extends PromptError {
515
- constructor(message) {
516
- super(message)
517
- this.name = 'PromptNotFoundError'
518
- }
519
- }
520
- class PromptVersionNotFoundError extends PromptError {
521
- constructor(message) {
522
- super(message)
523
- this.name = 'PromptVersionNotFoundError'
524
- }
525
- }
526
- class PromptIoError extends PromptError {
527
- constructor(message) {
528
- super(message)
529
- this.name = 'PromptIoError'
530
- }
531
- }
532
- class PromptYamlError extends PromptError {
533
- constructor(message) {
534
- super(message)
535
- this.name = 'PromptYamlError'
536
- }
537
- }
538
- class PromptJsonError extends PromptError {
539
- constructor(message) {
540
- super(message)
541
- this.name = 'PromptJsonError'
542
- }
543
- }
544
- class PromptValidationError extends PromptError {
545
- constructor(message) {
546
- super(message)
547
- this.name = 'PromptValidationError'
548
- }
549
- }
550
-
551
- // Memory
552
- class MemoryError extends BlazenError {
553
- constructor(message) {
554
- super(message)
555
- this.name = 'MemoryError'
556
- }
557
- }
558
- class MemoryNoEmbedderError extends MemoryError {
559
- constructor(message) {
560
- super(message)
561
- this.name = 'MemoryNoEmbedderError'
562
- }
563
- }
564
- class MemoryElidError extends MemoryError {
565
- constructor(message) {
566
- super(message)
567
- this.name = 'MemoryElidError'
568
- }
569
- }
570
- class MemoryEmbeddingError extends MemoryError {
571
- constructor(message) {
572
- super(message)
573
- this.name = 'MemoryEmbeddingError'
574
- }
575
- }
576
- class MemoryNotFoundError extends MemoryError {
577
- constructor(message) {
578
- super(message)
579
- this.name = 'MemoryNotFoundError'
580
- }
581
- }
582
- class MemorySerializationError extends MemoryError {
583
- constructor(message) {
584
- super(message)
585
- this.name = 'MemorySerializationError'
586
- }
587
- }
588
- class MemoryIoError extends MemoryError {
589
- constructor(message) {
590
- super(message)
591
- this.name = 'MemoryIoError'
592
- }
593
- }
594
- class MemoryBackendError extends MemoryError {
595
- constructor(message) {
596
- super(message)
597
- this.name = 'MemoryBackendError'
598
- }
599
- }
600
-
601
- // Model cache
602
- class CacheError extends BlazenError {
603
- constructor(message) {
604
- super(message)
605
- this.name = 'CacheError'
606
- }
607
- }
608
- class DownloadError extends CacheError {
609
- constructor(message) {
610
- super(message)
611
- this.name = 'DownloadError'
612
- }
613
- }
614
- class CacheDirError extends CacheError {
615
- constructor(message) {
616
- super(message)
617
- this.name = 'CacheDirError'
618
- }
619
- }
620
- class IoError extends CacheError {
621
- constructor(message) {
622
- super(message)
623
- this.name = 'IoError'
624
- }
625
- }
626
-
627
- // ---------------------------------------------------------------------------
628
- // Tag -> class registry
629
- // ---------------------------------------------------------------------------
630
-
631
- const TAG_TO_CLASS = {
632
- // Core
633
- BlazenError,
634
- AuthError,
635
- RateLimitError,
636
- TimeoutError,
637
- ValidationError,
638
- ContentPolicyError,
639
- UnsupportedError,
640
- ComputeError,
641
- MediaError,
642
- ProviderError,
643
-
644
- // LlamaCpp
645
- LlamaCppError,
646
- LlamaCppInvalidOptionsError,
647
- LlamaCppModelLoadError,
648
- LlamaCppInferenceError,
649
- LlamaCppEngineNotAvailableError,
650
-
651
- // Candle LLM
652
- CandleLlmError,
653
- CandleLlmInvalidOptionsError,
654
- CandleLlmModelLoadError,
655
- CandleLlmInferenceError,
656
- CandleLlmEngineNotAvailableError,
657
-
658
- // Candle Embed
659
- CandleEmbedError,
660
- CandleEmbedInvalidOptionsError,
661
- CandleEmbedModelLoadError,
662
- CandleEmbedEmbeddingError,
663
- CandleEmbedEngineNotAvailableError,
664
- CandleEmbedTaskPanickedError,
665
-
666
- // MistralRs
667
- MistralRsError,
668
- MistralRsInvalidOptionsError,
669
- MistralRsInitError,
670
- MistralRsInferenceError,
671
- MistralRsEngineNotAvailableError,
672
-
673
- // Whisper
674
- WhisperError,
675
- WhisperInvalidOptionsError,
676
- WhisperModelLoadError,
677
- WhisperTranscriptionError,
678
- WhisperEngineNotAvailableError,
679
- WhisperIoError,
680
-
681
- // Piper
682
- PiperError,
683
- PiperInvalidOptionsError,
684
- PiperModelLoadError,
685
- PiperSynthesisError,
686
- PiperEngineNotAvailableError,
687
-
688
- // Diffusion
689
- DiffusionError,
690
- DiffusionInvalidOptionsError,
691
- DiffusionModelLoadError,
692
- DiffusionGenerationError,
693
-
694
- // Fastembed / Tract
695
- FastEmbedError,
696
- EmbedUnknownModelError,
697
- EmbedInitError,
698
- EmbedEmbedError,
699
- EmbedMutexPoisonedError,
700
- EmbedTaskPanickedError,
701
- TractError,
702
-
703
- // Peer
704
- PeerEncodeError,
705
- PeerTransportError,
706
- PeerEnvelopeVersionError,
707
- PeerWorkflowError,
708
- PeerTlsError,
709
- PeerUnknownStepError,
710
-
711
- // Persist
712
- PersistError,
713
-
714
- // Prompts
715
- PromptError,
716
- PromptMissingVariableError,
717
- PromptNotFoundError,
718
- PromptVersionNotFoundError,
719
- PromptIoError,
720
- PromptYamlError,
721
- PromptJsonError,
722
- PromptValidationError,
723
-
724
- // Memory
725
- MemoryError,
726
- MemoryNoEmbedderError,
727
- MemoryElidError,
728
- MemoryEmbeddingError,
729
- MemoryNotFoundError,
730
- MemorySerializationError,
731
- MemoryIoError,
732
- MemoryBackendError,
733
-
734
- // Model cache
735
- CacheError,
736
- DownloadError,
737
- CacheDirError,
738
- IoError,
739
- }
740
-
741
- // Tag prefix regex. Matches `[Tag] rest-of-message`. The tag is restricted
742
- // to ASCII letters/digits to avoid eating bracketed user content.
743
- const TAG_RE = /^\[([A-Za-z][A-Za-z0-9]*)\]\s*([\s\S]*)$/
744
-
745
- /**
746
- * Given an `Error` thrown by a napi-rs function, parse the Rust-side
747
- * `[Tag]` prefix and return a typed instance from the hierarchy above.
748
- *
749
- * If `err` is not an `Error`, or its message is not tagged, or the tag
750
- * is unknown, the original error is returned unchanged.
751
- */
752
- function enrichError(err) {
753
- if (!(err instanceof Error)) {
754
- return err
755
- }
756
- let message = err.message || ''
757
- let providerPayload = null
758
-
759
- // Detect and strip the provider-error sentinel (a JSON line followed by
760
- // the human-readable `[ProviderError] ...` line).
761
- if (message.startsWith(PROVIDER_ERROR_SENTINEL)) {
762
- const newlineIdx = message.indexOf('\n')
763
- if (newlineIdx !== -1) {
764
- const jsonPart = message
765
- .slice(PROVIDER_ERROR_SENTINEL.length, newlineIdx)
766
- .trim()
767
- try {
768
- providerPayload = JSON.parse(jsonPart)
769
- } catch {
770
- providerPayload = null
771
- }
772
- message = message.slice(newlineIdx + 1)
773
- }
774
- }
775
-
776
- // Detect and strip the caller-error sentinel. If the stash has the
777
- // original error, re-throw it verbatim (preserves `instanceof MyError`
778
- // and all custom properties). Otherwise fall back to a generic Error
779
- // carrying the name+message from the sentinel JSON.
780
- if (message.startsWith(CALLER_ERROR_SENTINEL)) {
781
- const newlineIdx = message.indexOf('\n')
782
- let payload = null
783
- if (newlineIdx !== -1) {
784
- const jsonPart = message
785
- .slice(CALLER_ERROR_SENTINEL.length, newlineIdx)
786
- .trim()
787
- try {
788
- payload = JSON.parse(jsonPart)
789
- } catch {
790
- payload = null
791
- }
792
- }
793
- if (payload && typeof payload.ref === 'string') {
794
- const original = callerErrorStash.get(payload.ref)
795
- callerErrorStash.delete(payload.ref)
796
- if (original !== undefined) {
797
- return original
798
- }
799
- }
800
- // Fallback: construct a generic Error with the sentinel-embedded
801
- // name and message. instanceof won't match, but `.name` / `.message`
802
- // still let consumers branch on the error type.
803
- const fallback = new Error((payload && payload.message) || 'caller error')
804
- if (payload && typeof payload.name === 'string') {
805
- fallback.name = payload.name
806
- }
807
- fallback.stack = err.stack
808
- return fallback
809
- }
810
-
811
- const match = TAG_RE.exec(message)
812
- if (!match) {
813
- return err
814
- }
815
- const tag = match[1]
816
- const rest = match[2]
817
- const Cls = TAG_TO_CLASS[tag]
818
- if (!Cls) {
819
- return err
820
- }
821
- const enriched = new Cls(rest)
822
- enriched.stack = err.stack
823
- // Preserve the original napi `code` (e.g. 'GenericFailure') if present.
824
- if (err.code !== undefined) {
825
- enriched.code = err.code
826
- }
827
- if (providerPayload && enriched instanceof ProviderError) {
828
- if (providerPayload.provider !== undefined) {
829
- enriched.provider = providerPayload.provider
830
- }
831
- if (providerPayload.status !== undefined) {
832
- enriched.status = providerPayload.status
833
- }
834
- if (providerPayload.endpoint !== undefined) {
835
- enriched.endpoint = providerPayload.endpoint
836
- }
837
- if (providerPayload.requestId !== undefined) {
838
- enriched.requestId = providerPayload.requestId
839
- }
840
- if (providerPayload.detail !== undefined) {
841
- enriched.detail = providerPayload.detail
842
- }
843
- if (providerPayload.retryAfterMs !== undefined) {
844
- enriched.retryAfterMs = providerPayload.retryAfterMs
845
- }
846
- }
847
- return enriched
848
- }
849
-
850
- module.exports = {
851
- // Core
852
- BlazenError,
853
- AuthError,
854
- RateLimitError,
855
- TimeoutError,
856
- ValidationError,
857
- ContentPolicyError,
858
- UnsupportedError,
859
- ComputeError,
860
- MediaError,
861
- ProviderError,
862
-
863
- // LlamaCpp
864
- LlamaCppError,
865
- LlamaCppInvalidOptionsError,
866
- LlamaCppModelLoadError,
867
- LlamaCppInferenceError,
868
- LlamaCppEngineNotAvailableError,
869
-
870
- // Candle LLM
871
- CandleLlmError,
872
- CandleLlmInvalidOptionsError,
873
- CandleLlmModelLoadError,
874
- CandleLlmInferenceError,
875
- CandleLlmEngineNotAvailableError,
876
-
877
- // Candle Embed
878
- CandleEmbedError,
879
- CandleEmbedInvalidOptionsError,
880
- CandleEmbedModelLoadError,
881
- CandleEmbedEmbeddingError,
882
- CandleEmbedEngineNotAvailableError,
883
- CandleEmbedTaskPanickedError,
884
-
885
- // MistralRs
886
- MistralRsError,
887
- MistralRsInvalidOptionsError,
888
- MistralRsInitError,
889
- MistralRsInferenceError,
890
- MistralRsEngineNotAvailableError,
891
-
892
- // Whisper
893
- WhisperError,
894
- WhisperInvalidOptionsError,
895
- WhisperModelLoadError,
896
- WhisperTranscriptionError,
897
- WhisperEngineNotAvailableError,
898
- WhisperIoError,
899
-
900
- // Piper
901
- PiperError,
902
- PiperInvalidOptionsError,
903
- PiperModelLoadError,
904
- PiperSynthesisError,
905
- PiperEngineNotAvailableError,
906
-
907
- // Diffusion
908
- DiffusionError,
909
- DiffusionInvalidOptionsError,
910
- DiffusionModelLoadError,
911
- DiffusionGenerationError,
912
-
913
- // Fastembed / Tract
914
- FastEmbedError,
915
- EmbedUnknownModelError,
916
- EmbedInitError,
917
- EmbedEmbedError,
918
- EmbedMutexPoisonedError,
919
- EmbedTaskPanickedError,
920
- TractError,
921
-
922
- // Peer
923
- PeerEncodeError,
924
- PeerTransportError,
925
- PeerEnvelopeVersionError,
926
- PeerWorkflowError,
927
- PeerTlsError,
928
- PeerUnknownStepError,
929
-
930
- // Persist
931
- PersistError,
932
-
933
- // Prompts
934
- PromptError,
935
- PromptMissingVariableError,
936
- PromptNotFoundError,
937
- PromptVersionNotFoundError,
938
- PromptIoError,
939
- PromptYamlError,
940
- PromptJsonError,
941
- PromptValidationError,
942
-
943
- // Memory
944
- MemoryError,
945
- MemoryNoEmbedderError,
946
- MemoryElidError,
947
- MemoryEmbeddingError,
948
- MemoryNotFoundError,
949
- MemorySerializationError,
950
- MemoryIoError,
951
- MemoryBackendError,
952
-
953
- // Model cache
954
- CacheError,
955
- DownloadError,
956
- CacheDirError,
957
- IoError,
958
-
959
- // Helper
960
- enrichError,
961
-
962
- // Caller-error preservation
963
- CALLER_ERROR_SENTINEL,
964
- wrapToolHandlerForCallerErrors,
965
- callerErrorStash,
966
- }