transcribe-cpp 0.2.4 → 0.3.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.
package/README.md CHANGED
@@ -46,6 +46,17 @@ and streams.
46
46
  const result = await model.transcribe(pcm, { pnc: "off", itn: "on" });
47
47
  ```
48
48
 
49
+ ### Prompting
50
+
51
+ `vocabulary` (custom terms), `prompt` (context, or the instruction under
52
+ `task: "instruct"`) and `prefix` (text the model continues from) take effect
53
+ where `model.supports()` reports `"vocabulary"`, `"context_prompt"`,
54
+ `"instruct"` or `"transcript_prefix"`.
55
+
56
+ ```ts
57
+ const result = await model.transcribe(pcm, { vocabulary: ["Kubernetes", "gRPC"] });
58
+ ```
59
+
49
60
  ### Streaming
50
61
 
51
62
  ```ts
@@ -1,4 +1,4 @@
1
- export declare const PUBLIC_HEADER_HASH = "7df72bf9e667b8c2";
1
+ export declare const PUBLIC_HEADER_HASH = "57b1af43650f195d";
2
2
  export declare const TRANSCRIBE_OK = 0;
3
3
  export declare const TRANSCRIBE_ERR_INVALID_ARG = 1;
4
4
  export declare const TRANSCRIBE_ERR_NOT_IMPLEMENTED = 2;
@@ -18,6 +18,7 @@ export declare const TRANSCRIBE_ERR_UNSUPPORTED_PNC = 15;
18
18
  export declare const TRANSCRIBE_ERR_UNSUPPORTED_ITN = 16;
19
19
  export declare const TRANSCRIBE_ERR_INPUT_TOO_LONG = 17;
20
20
  export declare const TRANSCRIBE_ERR_OUTPUT_TRUNCATED = 18;
21
+ export declare const TRANSCRIBE_ERR_OUTPUT_REPETITION = 19;
21
22
  export declare const TRANSCRIBE_ABI_MODEL_LOAD_PARAMS = 0;
22
23
  export declare const TRANSCRIBE_ABI_SESSION_PARAMS = 1;
23
24
  export declare const TRANSCRIBE_ABI_RUN_PARAMS = 2;
@@ -33,6 +34,7 @@ export declare const TRANSCRIBE_ABI_SESSION_LIMITS = 11;
33
34
  export declare const TRANSCRIBE_ABI_EXT = 12;
34
35
  export declare const TRANSCRIBE_ABI_DEVICE_INFO = 13;
35
36
  export declare const TRANSCRIBE_ABI_SPEAKER_SEGMENT = 14;
37
+ export declare const TRANSCRIBE_ABI_BACKEND_INIT_PARAMS = 15;
36
38
  export declare const TRANSCRIBE_LOG_LEVEL_NONE = 0;
37
39
  export declare const TRANSCRIBE_LOG_LEVEL_INFO = 1;
38
40
  export declare const TRANSCRIBE_LOG_LEVEL_WARN = 2;
@@ -41,6 +43,7 @@ export declare const TRANSCRIBE_LOG_LEVEL_DEBUG = 4;
41
43
  export declare const TRANSCRIBE_LOG_LEVEL_CONT = 5;
42
44
  export declare const TRANSCRIBE_TASK_TRANSCRIBE = 0;
43
45
  export declare const TRANSCRIBE_TASK_TRANSLATE = 1;
46
+ export declare const TRANSCRIBE_TASK_INSTRUCT = 2;
44
47
  export declare const TRANSCRIBE_TIMESTAMPS_NONE = 0;
45
48
  export declare const TRANSCRIBE_TIMESTAMPS_AUTO = 1;
46
49
  export declare const TRANSCRIBE_TIMESTAMPS_SEGMENT = 2;
@@ -78,6 +81,10 @@ export declare const TRANSCRIBE_FEATURE_CANCELLATION = 3;
78
81
  export declare const TRANSCRIBE_FEATURE_PNC = 4;
79
82
  export declare const TRANSCRIBE_FEATURE_ITN = 5;
80
83
  export declare const TRANSCRIBE_FEATURE_DIARIZATION = 6;
84
+ export declare const TRANSCRIBE_FEATURE_VOCABULARY = 7;
85
+ export declare const TRANSCRIBE_FEATURE_CONTEXT_PROMPT = 8;
86
+ export declare const TRANSCRIBE_FEATURE_INSTRUCT = 9;
87
+ export declare const TRANSCRIBE_FEATURE_TRANSCRIPT_PREFIX = 10;
81
88
  export declare const TRANSCRIBE_STREAM_IDLE = 0;
82
89
  export declare const TRANSCRIBE_STREAM_ACTIVE = 1;
83
90
  export declare const TRANSCRIBE_STREAM_FINISHED = 2;
@@ -91,6 +98,13 @@ export declare const TRANSCRIBE_SORTFORMER_PRESET_HIGH_LATENCY = 2;
91
98
  export declare const TRANSCRIBE_SORTFORMER_PRESET_LOW_LATENCY = 3;
92
99
  export declare const TRANSCRIBE_WHISPER_PROMPT_FIRST_SEGMENT = 0;
93
100
  export declare const TRANSCRIBE_WHISPER_PROMPT_ALL_SEGMENTS = 1;
101
+ export declare const TRANSCRIBE_BACKEND_MASK_ALL = 4294967295;
102
+ export declare const TRANSCRIBE_BACKEND_MASK_CPU = 1;
103
+ export declare const TRANSCRIBE_BACKEND_MASK_CUDA = 8;
104
+ export declare const TRANSCRIBE_BACKEND_MASK_METAL = 2;
105
+ export declare const TRANSCRIBE_BACKEND_MASK_OTHER = 2147483648;
106
+ export declare const TRANSCRIBE_BACKEND_MASK_ROCM = 16;
107
+ export declare const TRANSCRIBE_BACKEND_MASK_VULKAN = 4;
94
108
  export declare const TRANSCRIBE_EXT_KIND_MOONSHINE_STREAMING_STREAM = 1414746957;
95
109
  export declare const TRANSCRIBE_EXT_KIND_PARAKEET_BUFFERED_STREAM = 1396853584;
96
110
  export declare const TRANSCRIBE_EXT_KIND_PARAKEET_STREAM = 1414744912;
@@ -10,7 +10,7 @@
10
10
  // Stable digest of the ABI surface (structs, enums, macros, layout,
11
11
  // prototypes), computed by the Python oracle and pinned here so a header
12
12
  // ABI change turns this binding's drift check red for conscious review.
13
- export const PUBLIC_HEADER_HASH = "7df72bf9e667b8c2";
13
+ export const PUBLIC_HEADER_HASH = "57b1af43650f195d";
14
14
  // === enum constants ===
15
15
  export const TRANSCRIBE_OK = 0;
16
16
  export const TRANSCRIBE_ERR_INVALID_ARG = 1;
@@ -31,6 +31,7 @@ export const TRANSCRIBE_ERR_UNSUPPORTED_PNC = 15;
31
31
  export const TRANSCRIBE_ERR_UNSUPPORTED_ITN = 16;
32
32
  export const TRANSCRIBE_ERR_INPUT_TOO_LONG = 17;
33
33
  export const TRANSCRIBE_ERR_OUTPUT_TRUNCATED = 18;
34
+ export const TRANSCRIBE_ERR_OUTPUT_REPETITION = 19;
34
35
  export const TRANSCRIBE_ABI_MODEL_LOAD_PARAMS = 0;
35
36
  export const TRANSCRIBE_ABI_SESSION_PARAMS = 1;
36
37
  export const TRANSCRIBE_ABI_RUN_PARAMS = 2;
@@ -46,6 +47,7 @@ export const TRANSCRIBE_ABI_SESSION_LIMITS = 11;
46
47
  export const TRANSCRIBE_ABI_EXT = 12;
47
48
  export const TRANSCRIBE_ABI_DEVICE_INFO = 13;
48
49
  export const TRANSCRIBE_ABI_SPEAKER_SEGMENT = 14;
50
+ export const TRANSCRIBE_ABI_BACKEND_INIT_PARAMS = 15;
49
51
  export const TRANSCRIBE_LOG_LEVEL_NONE = 0;
50
52
  export const TRANSCRIBE_LOG_LEVEL_INFO = 1;
51
53
  export const TRANSCRIBE_LOG_LEVEL_WARN = 2;
@@ -54,6 +56,7 @@ export const TRANSCRIBE_LOG_LEVEL_DEBUG = 4;
54
56
  export const TRANSCRIBE_LOG_LEVEL_CONT = 5;
55
57
  export const TRANSCRIBE_TASK_TRANSCRIBE = 0;
56
58
  export const TRANSCRIBE_TASK_TRANSLATE = 1;
59
+ export const TRANSCRIBE_TASK_INSTRUCT = 2;
57
60
  export const TRANSCRIBE_TIMESTAMPS_NONE = 0;
58
61
  export const TRANSCRIBE_TIMESTAMPS_AUTO = 1;
59
62
  export const TRANSCRIBE_TIMESTAMPS_SEGMENT = 2;
@@ -91,6 +94,10 @@ export const TRANSCRIBE_FEATURE_CANCELLATION = 3;
91
94
  export const TRANSCRIBE_FEATURE_PNC = 4;
92
95
  export const TRANSCRIBE_FEATURE_ITN = 5;
93
96
  export const TRANSCRIBE_FEATURE_DIARIZATION = 6;
97
+ export const TRANSCRIBE_FEATURE_VOCABULARY = 7;
98
+ export const TRANSCRIBE_FEATURE_CONTEXT_PROMPT = 8;
99
+ export const TRANSCRIBE_FEATURE_INSTRUCT = 9;
100
+ export const TRANSCRIBE_FEATURE_TRANSCRIPT_PREFIX = 10;
94
101
  export const TRANSCRIBE_STREAM_IDLE = 0;
95
102
  export const TRANSCRIBE_STREAM_ACTIVE = 1;
96
103
  export const TRANSCRIBE_STREAM_FINISHED = 2;
@@ -105,6 +112,13 @@ export const TRANSCRIBE_SORTFORMER_PRESET_LOW_LATENCY = 3;
105
112
  export const TRANSCRIBE_WHISPER_PROMPT_FIRST_SEGMENT = 0;
106
113
  export const TRANSCRIBE_WHISPER_PROMPT_ALL_SEGMENTS = 1;
107
114
  // === macro constants (integer object-like macros) ===
115
+ export const TRANSCRIBE_BACKEND_MASK_ALL = 4294967295;
116
+ export const TRANSCRIBE_BACKEND_MASK_CPU = 1;
117
+ export const TRANSCRIBE_BACKEND_MASK_CUDA = 8;
118
+ export const TRANSCRIBE_BACKEND_MASK_METAL = 2;
119
+ export const TRANSCRIBE_BACKEND_MASK_OTHER = 2147483648;
120
+ export const TRANSCRIBE_BACKEND_MASK_ROCM = 16;
121
+ export const TRANSCRIBE_BACKEND_MASK_VULKAN = 4;
108
122
  export const TRANSCRIBE_EXT_KIND_MOONSHINE_STREAMING_STREAM = 1414746957;
109
123
  export const TRANSCRIBE_EXT_KIND_PARAKEET_BUFFERED_STREAM = 1396853584;
110
124
  export const TRANSCRIBE_EXT_KIND_PARAKEET_STREAM = 1414744912;
@@ -113,10 +127,11 @@ export const TRANSCRIBE_EXT_KIND_VOXTRAL_REALTIME_STREAM = 1414746710;
113
127
  export const TRANSCRIBE_EXT_KIND_WHISPER_RUN = 1314015319;
114
128
  export const STRUCT_LAYOUT = {
115
129
  'transcribe_ext': { size: 16, align: 8, offsets: { 'size': 0, 'kind': 8 } },
130
+ 'transcribe_backend_init_params': { size: 24, align: 8, offsets: { 'struct_size': 0, 'artifact_dir': 8, 'allowed_backends': 16 } },
116
131
  'transcribe_device_info': { size: 64, align: 8, offsets: { 'struct_size': 0, 'name': 8, 'description': 16, 'kind': 24, 'device_id': 32, 'memory_total': 40, 'memory_free': 48, 'device_type': 56 } },
117
132
  'transcribe_model_load_params': { size: 24, align: 8, offsets: { 'struct_size': 0, 'backend': 8, 'device': 16 } },
118
133
  'transcribe_session_params': { size: 24, align: 8, offsets: { 'struct_size': 0, 'n_threads': 8, 'kv_type': 12, 'n_ctx': 16 } },
119
- 'transcribe_run_params': { size: 72, align: 8, offsets: { 'struct_size': 0, 'task': 8, 'timestamps': 12, 'pnc': 16, 'itn': 20, 'diarize': 24, 'language': 32, 'target_language': 40, 'keep_special_tags': 48, 'family': 56, 'spec_k_drafts': 64 } },
134
+ 'transcribe_run_params': { size: 104, align: 8, offsets: { 'struct_size': 0, 'task': 8, 'timestamps': 12, 'pnc': 16, 'itn': 20, 'diarize': 24, 'language': 32, 'target_language': 40, 'keep_special_tags': 48, 'family': 56, 'spec_k_drafts': 64, 'vocabulary': 72, 'n_vocabulary': 80, 'prompt': 88, 'prefix': 96 } },
120
135
  'transcribe_capabilities': { size: 56, align: 8, offsets: { 'struct_size': 0, 'native_sample_rate': 8, 'n_languages': 12, 'languages': 16, 'max_timestamp_kind': 24, 'supports_language_detect': 28, 'supports_translate': 29, 'supports_streaming': 30, 'supports_spec_decode': 31, 'max_audio_ms': 32, 'n_translate_target_languages': 40, 'translate_target_languages': 48 } },
121
136
  'transcribe_session_limits': { size: 32, align: 8, offsets: { 'struct_size': 0, 'effective_n_ctx': 8, 'effective_max_audio_ms': 16, 'max_kv_bytes': 24 } },
122
137
  'transcribe_stream_params': { size: 24, align: 8, offsets: { 'struct_size': 0, 'family': 8, 'commit_policy': 16, 'stable_prefix_agreement_n': 20 } },
@@ -137,6 +152,7 @@ export const STRUCT_LAYOUT = {
137
152
  };
138
153
  export const ABI_STRUCT_IDS = {
139
154
  'transcribe_ext': 12,
155
+ 'transcribe_backend_init_params': 15,
140
156
  'transcribe_device_info': 13,
141
157
  'transcribe_model_load_params': 0,
142
158
  'transcribe_session_params': 1,
@@ -156,10 +172,11 @@ export const ABI_STRUCT_IDS = {
156
172
  export function defineTypes(koffi) {
157
173
  const T = {};
158
174
  T['transcribe_ext'] = koffi.struct({ size: 'uint64_t', kind: 'uint32_t' });
175
+ T['transcribe_backend_init_params'] = koffi.struct({ struct_size: 'uint64_t', artifact_dir: 'char *', allowed_backends: 'uint32_t' });
159
176
  T['transcribe_device_info'] = koffi.struct({ struct_size: 'uint64_t', name: 'char *', description: 'char *', kind: 'char *', device_id: 'char *', memory_total: 'uint64_t', memory_free: 'uint64_t', device_type: 'int' });
160
177
  T['transcribe_model_load_params'] = koffi.struct({ struct_size: 'uint64_t', backend: 'int', device: 'void *' });
161
178
  T['transcribe_session_params'] = koffi.struct({ struct_size: 'uint64_t', n_threads: 'int', kv_type: 'int', n_ctx: 'int32_t' });
162
- T['transcribe_run_params'] = koffi.struct({ struct_size: 'uint64_t', task: 'int', timestamps: 'int', pnc: 'int', itn: 'int', diarize: 'int', language: 'char *', target_language: 'char *', keep_special_tags: 'bool', family: 'void *', spec_k_drafts: 'int32_t' });
179
+ T['transcribe_run_params'] = koffi.struct({ struct_size: 'uint64_t', task: 'int', timestamps: 'int', pnc: 'int', itn: 'int', diarize: 'int', language: 'char *', target_language: 'char *', keep_special_tags: 'bool', family: 'void *', spec_k_drafts: 'int32_t', vocabulary: 'void *', n_vocabulary: 'int32_t', prompt: 'char *', prefix: 'char *' });
163
180
  T['transcribe_capabilities'] = koffi.struct({ struct_size: 'uint64_t', native_sample_rate: 'int32_t', n_languages: 'int', languages: 'void *', max_timestamp_kind: 'int', supports_language_detect: 'bool', supports_translate: 'bool', supports_streaming: 'bool', supports_spec_decode: 'bool', max_audio_ms: 'int64_t', n_translate_target_languages: 'int', translate_target_languages: 'void *' });
164
181
  T['transcribe_session_limits'] = koffi.struct({ struct_size: 'uint64_t', effective_n_ctx: 'int32_t', effective_max_audio_ms: 'int64_t', max_kv_bytes: 'int64_t' });
165
182
  T['transcribe_stream_params'] = koffi.struct({ struct_size: 'uint64_t', family: 'void *', commit_policy: 'int', stable_prefix_agreement_n: 'uint32_t' });
@@ -182,7 +199,9 @@ export function defineTypes(koffi) {
182
199
  export const FUNCTION_SIGNATURES = {
183
200
  'transcribe_abi_struct_align': { ret: 'size_t', args: ['transcribe_abi_struct'] },
184
201
  'transcribe_abi_struct_size': { ret: 'size_t', args: ['transcribe_abi_struct'] },
202
+ 'transcribe_allowed_backends': { ret: 'uint32_t', args: [] },
185
203
  'transcribe_backend_available': { ret: '_Bool', args: ['transcribe_backend_request'] },
204
+ 'transcribe_backend_init_params_init': { ret: 'void', args: ['struct transcribe_backend_init_params *'] },
186
205
  'transcribe_batch_detected_language': { ret: 'const char *', args: ['const struct transcribe_session *', 'int'] },
187
206
  'transcribe_batch_full_text': { ret: 'const char *', args: ['const struct transcribe_session *', 'int'] },
188
207
  'transcribe_batch_get_segment': { ret: 'transcribe_status', args: ['const struct transcribe_session *', 'int', 'int', 'struct transcribe_segment *'] },
@@ -217,6 +236,7 @@ export const FUNCTION_SIGNATURES = {
217
236
  'transcribe_get_word': { ret: 'transcribe_status', args: ['const struct transcribe_session *', 'int', 'struct transcribe_word *'] },
218
237
  'transcribe_init_backends': { ret: 'transcribe_status', args: ['const char *'] },
219
238
  'transcribe_init_backends_default': { ret: 'transcribe_status', args: [] },
239
+ 'transcribe_init_backends_ex': { ret: 'transcribe_status', args: ['const struct transcribe_backend_init_params *'] },
220
240
  'transcribe_log_set': { ret: 'void', args: ['transcribe_log_callback', 'void *'] },
221
241
  'transcribe_model_accepts_ext_kind': { ret: '_Bool', args: ['const struct transcribe_model *', 'transcribe_ext_slot', 'uint32_t'] },
222
242
  'transcribe_model_arch_string': { ret: 'const char *', args: ['const struct transcribe_model *'] },
package/dist/errors.d.ts CHANGED
@@ -5,7 +5,7 @@ export declare class TranscribeError extends Error {
5
5
  readonly status: number;
6
6
  /** Set on per-utterance failures from a batch run. */
7
7
  utteranceIndex?: number;
8
- /** Any partial transcript recovered before the error (set on Aborted / OutputTruncated). */
8
+ /** Any partial transcript recovered before the error (set on Aborted / OutputTruncated / OutputRepetition). */
9
9
  partialResult?: TranscriptionResult;
10
10
  constructor(message: string, status?: number);
11
11
  }
@@ -43,6 +43,13 @@ export declare class Aborted extends TranscribeError {
43
43
  /** Raised when decode hits the context/generation cap; carries the partial in `partialResult`. */
44
44
  export declare class OutputTruncated extends TranscribeError {
45
45
  }
46
+ /**
47
+ * Raised when decode is stopped because the output began repeating itself; carries
48
+ * the partial (repeats dropped) in `partialResult`. Extends OutputTruncated, so a
49
+ * handler for incomplete transcripts catches both.
50
+ */
51
+ export declare class OutputRepetition extends OutputTruncated {
52
+ }
46
53
  /** Build (do not throw) the mapped exception for a status. */
47
54
  export declare function exceptionForStatus(status: number, statusString: string, context?: string): TranscribeError;
48
55
  /** Throw the mapped exception unless the status is OK. */
package/dist/errors.js CHANGED
@@ -5,7 +5,7 @@ export class TranscribeError extends Error {
5
5
  status;
6
6
  /** Set on per-utterance failures from a batch run. */
7
7
  utteranceIndex;
8
- /** Any partial transcript recovered before the error (set on Aborted / OutputTruncated). */
8
+ /** Any partial transcript recovered before the error (set on Aborted / OutputTruncated / OutputRepetition). */
9
9
  partialResult;
10
10
  constructor(message, status = g.TRANSCRIBE_OK) {
11
11
  super(message);
@@ -47,6 +47,13 @@ export class Aborted extends TranscribeError {
47
47
  /** Raised when decode hits the context/generation cap; carries the partial in `partialResult`. */
48
48
  export class OutputTruncated extends TranscribeError {
49
49
  }
50
+ /**
51
+ * Raised when decode is stopped because the output began repeating itself; carries
52
+ * the partial (repeats dropped) in `partialResult`. Extends OutputTruncated, so a
53
+ * handler for incomplete transcripts catches both.
54
+ */
55
+ export class OutputRepetition extends OutputTruncated {
56
+ }
50
57
  const STATUS_TO_EXC = {
51
58
  [g.TRANSCRIBE_ERR_INVALID_ARG]: InvalidArgument,
52
59
  [g.TRANSCRIBE_ERR_NOT_IMPLEMENTED]: NotImplementedByModel,
@@ -66,6 +73,7 @@ const STATUS_TO_EXC = {
66
73
  [g.TRANSCRIBE_ERR_UNSUPPORTED_ITN]: UnsupportedRequest,
67
74
  [g.TRANSCRIBE_ERR_INPUT_TOO_LONG]: InputTooLong,
68
75
  [g.TRANSCRIBE_ERR_OUTPUT_TRUNCATED]: OutputTruncated,
76
+ [g.TRANSCRIBE_ERR_OUTPUT_REPETITION]: OutputRepetition,
69
77
  };
70
78
  /** Build (do not throw) the mapped exception for a status. */
71
79
  export function exceptionForStatus(status, statusString, context = "") {
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@
8
8
  import { native, setLogHandler, } from "./native.js";
9
9
  import { resolveLibrary } from "./loader.js";
10
10
  import * as g from "./_generated.js";
11
- import { Aborted, Busy, exceptionForStatus, InvalidArgument, ModelLoadError, NotImplementedByModel, OutputTruncated, TranscribeError, UnsupportedRequest, } from "./errors.js";
11
+ import { Aborted, Busy, exceptionForStatus, InvalidArgument, ModelLoadError, NotImplementedByModel, OutputRepetition, OutputTruncated, TranscribeError, UnsupportedRequest, } from "./errors.js";
12
12
  export * from "./types.js";
13
13
  export * from "./errors.js";
14
14
  export { setLogHandler };
@@ -30,6 +30,7 @@ const KV_TYPES = {
30
30
  const TASKS = {
31
31
  transcribe: g.TRANSCRIBE_TASK_TRANSCRIBE,
32
32
  translate: g.TRANSCRIBE_TASK_TRANSLATE,
33
+ instruct: g.TRANSCRIBE_TASK_INSTRUCT,
33
34
  };
34
35
  const TIMESTAMPS = {
35
36
  none: g.TRANSCRIBE_TIMESTAMPS_NONE,
@@ -62,6 +63,10 @@ const FEATURES = {
62
63
  pnc: g.TRANSCRIBE_FEATURE_PNC,
63
64
  itn: g.TRANSCRIBE_FEATURE_ITN,
64
65
  diarization: g.TRANSCRIBE_FEATURE_DIARIZATION,
66
+ vocabulary: g.TRANSCRIBE_FEATURE_VOCABULARY,
67
+ context_prompt: g.TRANSCRIBE_FEATURE_CONTEXT_PROMPT,
68
+ instruct: g.TRANSCRIBE_FEATURE_INSTRUCT,
69
+ transcript_prefix: g.TRANSCRIBE_FEATURE_TRANSCRIPT_PREFIX,
65
70
  };
66
71
  // ---- helpers ---------------------------------------------------------------
67
72
  function lookup(map, key, what) {
@@ -438,7 +443,7 @@ const FAMILY = {
438
443
  type: "transcribe_whisper_run_ext",
439
444
  init: "whisperRunExtInit",
440
445
  map: (o) => ({
441
- initial_prompt: o.initialPrompt,
446
+ initial_prompt: o.initialPrompt === undefined ? undefined : cstr(o.initialPrompt, "initialPrompt"),
442
447
  condition_on_prev_tokens: o.conditionOnPrevTokens,
443
448
  temperature: o.temperature,
444
449
  temperature_inc: o.temperatureInc,
@@ -525,6 +530,20 @@ function buildFamily(n, modelHandle, family, slot) {
525
530
  n.koffi.encode(buf, n.T[reg.type], ext);
526
531
  return buf;
527
532
  }
533
+ /** A string option as passed to C, which would silently cut it at a NUL. */
534
+ function cstr(value, name) {
535
+ if (value.includes("\0"))
536
+ throw new InvalidArgument(`${name} contains a NUL character`);
537
+ return value;
538
+ }
539
+ /** Free what #buildRunParams allocated; call once the native call returns. */
540
+ function freeRunParams(n, p) {
541
+ if (p.vocabulary) {
542
+ n.koffi.free(p.vocabulary);
543
+ p.vocabulary = null;
544
+ p.n_vocabulary = 0;
545
+ }
546
+ }
528
547
  function toStreamUpdate(u) {
529
548
  return {
530
549
  resultChanged: u.result_changed,
@@ -639,7 +658,8 @@ export class Session {
639
658
  cancel?.();
640
659
  }
641
660
  if (status === g.TRANSCRIBE_ERR_ABORTED ||
642
- status === g.TRANSCRIBE_ERR_OUTPUT_TRUNCATED) {
661
+ status === g.TRANSCRIBE_ERR_OUTPUT_TRUNCATED ||
662
+ status === g.TRANSCRIBE_ERR_OUTPUT_REPETITION) {
643
663
  const partial = {
644
664
  ...materialize(n, singleAccessors(n, h)),
645
665
  aborted: F.wasAborted(h),
@@ -647,7 +667,9 @@ export class Session {
647
667
  };
648
668
  const exc = status === g.TRANSCRIBE_ERR_ABORTED
649
669
  ? new Aborted(`run aborted`, status)
650
- : new OutputTruncated(`run output truncated`, status);
670
+ : status === g.TRANSCRIBE_ERR_OUTPUT_REPETITION
671
+ ? new OutputRepetition(`run stopped: output began repeating`, status)
672
+ : new OutputTruncated(`run output truncated`, status);
651
673
  exc.partialResult = partial;
652
674
  throw exc;
653
675
  }
@@ -657,7 +679,7 @@ export class Session {
657
679
  aborted: F.wasAborted(h),
658
680
  truncated: F.wasTruncated(h),
659
681
  };
660
- });
682
+ }).finally(() => freeRunParams(n, p));
661
683
  }
662
684
  #buildRunParams(opts) {
663
685
  const n = this.#n;
@@ -672,15 +694,33 @@ export class Session {
672
694
  p.itn = lookup(ITN, opts.itn ?? "default", "itn");
673
695
  p.diarize = lookup(DIARIZE, opts.diarize ?? "default", "diarize");
674
696
  if (opts.language !== undefined)
675
- p.language = opts.language;
697
+ p.language = cstr(opts.language, "language");
676
698
  if (opts.targetLanguage !== undefined)
677
- p.target_language = opts.targetLanguage;
699
+ p.target_language = cstr(opts.targetLanguage, "targetLanguage");
678
700
  if (opts.keepSpecialTags !== undefined)
679
701
  p.keep_special_tags = opts.keepSpecialTags;
680
702
  if (opts.specKDrafts !== undefined)
681
703
  p.spec_k_drafts = opts.specKDrafts;
682
704
  if (opts.family)
683
705
  p.family = buildFamily(n, this.#model.handle, opts.family, "run");
706
+ if (opts.vocabulary !== undefined) {
707
+ const terms = opts.vocabulary;
708
+ if (!Array.isArray(terms) || !terms.every((t) => typeof t === "string"))
709
+ throw new InvalidArgument("vocabulary must be an array of strings");
710
+ if (terms.length > 0) {
711
+ // Freed by freeRunParams after the call; the library copies the terms.
712
+ terms.forEach((t) => cstr(t, "vocabulary"));
713
+ const type = n.koffi.array("char *", terms.length);
714
+ const arr = n.koffi.alloc(type, 1);
715
+ n.koffi.encode(arr, type, terms);
716
+ p.vocabulary = arr;
717
+ p.n_vocabulary = terms.length;
718
+ }
719
+ }
720
+ if (opts.prompt !== undefined)
721
+ p.prompt = cstr(opts.prompt, "prompt");
722
+ if (opts.prefix !== undefined)
723
+ p.prefix = cstr(opts.prefix, "prefix");
684
724
  return p;
685
725
  }
686
726
  /**
@@ -734,18 +774,20 @@ export class Session {
734
774
  const error = exceptionForStatus(st, F.statusString(st), `utterance ${i}`);
735
775
  error.utteranceIndex = i;
736
776
  if (st === g.TRANSCRIBE_ERR_ABORTED ||
737
- st === g.TRANSCRIBE_ERR_OUTPUT_TRUNCATED) {
777
+ st === g.TRANSCRIBE_ERR_OUTPUT_TRUNCATED ||
778
+ st === g.TRANSCRIBE_ERR_OUTPUT_REPETITION) {
738
779
  error.partialResult = {
739
780
  ...materialize(n, batchAccessors(n, h, i)),
740
781
  aborted: st === g.TRANSCRIBE_ERR_ABORTED,
741
- truncated: st === g.TRANSCRIBE_ERR_OUTPUT_TRUNCATED,
782
+ truncated: st === g.TRANSCRIBE_ERR_OUTPUT_TRUNCATED ||
783
+ st === g.TRANSCRIBE_ERR_OUTPUT_REPETITION,
742
784
  };
743
785
  }
744
786
  out.push({ ok: false, error });
745
787
  }
746
788
  }
747
789
  return out;
748
- });
790
+ }).finally(() => freeRunParams(n, p));
749
791
  }
750
792
  /** Begin a streaming session. The returned Stream owns the begin params. */
751
793
  async stream(opts = {}) {
@@ -762,6 +804,8 @@ export class Session {
762
804
  diarize: opts.diarize,
763
805
  keepSpecialTags: opts.keepSpecialTags,
764
806
  specKDrafts: -1,
807
+ vocabulary: opts.vocabulary,
808
+ prompt: opts.prompt,
765
809
  });
766
810
  const sp = {};
767
811
  F.streamParamsInit(sp);
@@ -788,7 +832,7 @@ export class Session {
788
832
  throw new TranscribeError("session control is missing");
789
833
  control.replaceCurrentStream(stream);
790
834
  return stream;
791
- });
835
+ }).finally(() => freeRunParams(n, rp)); // begin copied the prompting strings
792
836
  }
793
837
  /**
794
838
  * Wire an AbortSignal to a native abort callback for one run. The callback is
package/dist/types.d.ts CHANGED
@@ -2,12 +2,14 @@
2
2
  import type { TranscribeError } from "./errors.js";
3
3
  export type Backend = "auto" | "cpu" | "cpu_accel" | "cuda" | "rocm" | "vulkan" | "metal";
4
4
  export type KvType = "auto" | "f32" | "f16";
5
- export type Task = "transcribe" | "translate";
5
+ /** "instruct": `prompt` replaces the task instruction and the output is free
6
+ * text ("instruct" feature; offline only). */
7
+ export type Task = "transcribe" | "translate" | "instruct";
6
8
  export type TimestampKind = "none" | "auto" | "segment" | "word" | "token";
7
9
  export type Pnc = "default" | "off" | "on";
8
10
  export type Itn = "default" | "off" | "on";
9
11
  export type Diarize = "default" | "off" | "on";
10
- export type Feature = "initial_prompt" | "temperature_fallback" | "long_form" | "cancellation" | "pnc" | "itn" | "diarization";
12
+ export type Feature = "initial_prompt" | "temperature_fallback" | "long_form" | "cancellation" | "pnc" | "itn" | "diarization" | "vocabulary" | "context_prompt" | "instruct" | "transcript_prefix";
11
13
  /** Mono float32 PCM at the model's native sample rate (16 kHz for v1). */
12
14
  export type PcmLike = Float32Array | number[] | ArrayBuffer | Buffer;
13
15
  export interface Segment {
@@ -144,6 +146,16 @@ export interface TranscribeOptions {
144
146
  signal?: AbortSignal;
145
147
  /** A run-slot family extension (e.g. whisper). */
146
148
  family?: FamilyExtension;
149
+ /** Custom terms in priority order, formatted per family ("vocabulary"
150
+ * feature; ignored with a warning elsewhere). */
151
+ vocabulary?: readonly string[];
152
+ /** Context text under transcribe/translate ("context_prompt" feature); the
153
+ * required instruction under task "instruct". */
154
+ prompt?: string;
155
+ /** Transcript text the model continues from ("transcript_prefix" feature;
156
+ * an error elsewhere, and in runBatch). `text` holds only the continuation;
157
+ * `rawText` leads with the prefix. */
158
+ prefix?: string;
147
159
  }
148
160
  /** One result of a batch run: success carries the transcript, failure the error.
149
161
  * On failure, `error.utteranceIndex` is set, and `error.partialResult` carries any
@@ -187,6 +199,10 @@ export interface StreamOptions {
187
199
  keepSpecialTags?: boolean;
188
200
  commitPolicy?: CommitPolicy;
189
201
  stablePrefixAgreementN?: number;
202
+ /** Custom terms in priority order (see TranscribeOptions.vocabulary). */
203
+ vocabulary?: readonly string[];
204
+ /** Context text (see TranscribeOptions.prompt). */
205
+ prompt?: string;
190
206
  /** A stream-slot family extension (moonshine, parakeet, voxtral). */
191
207
  family?: FamilyExtension;
192
208
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "transcribe-cpp",
3
- "version": "0.2.4",
3
+ "version": "0.3.1",
4
4
  "description": "TypeScript/Node.js bindings for transcribe.cpp — a C/C++ speech-to-text library built on ggml",
5
5
  "type": "module",
6
6
  "exports": {
@@ -51,11 +51,11 @@
51
51
  "koffi": "^3.0.2"
52
52
  },
53
53
  "optionalDependencies": {
54
- "@transcribe-cpp/darwin-arm64-metal": "0.2.4",
55
- "@transcribe-cpp/darwin-x64-cpu": "0.2.4",
56
- "@transcribe-cpp/linux-x64-cpu-vulkan": "0.2.4",
57
- "@transcribe-cpp/linux-arm64-cpu-vulkan": "0.2.4",
58
- "@transcribe-cpp/win32-x64-cpu-vulkan": "0.2.4"
54
+ "@transcribe-cpp/darwin-arm64-metal": "0.3.1",
55
+ "@transcribe-cpp/darwin-x64-cpu": "0.3.1",
56
+ "@transcribe-cpp/linux-x64-cpu-vulkan": "0.3.1",
57
+ "@transcribe-cpp/linux-arm64-cpu-vulkan": "0.3.1",
58
+ "@transcribe-cpp/win32-x64-cpu-vulkan": "0.3.1"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@types/node": "^22.0.0",