@juspay/neurolink 13.4.9 → 13.5.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/README.md +14 -4
- package/dist/browser/neurolink.min.js +419 -419
- package/dist/cli/commands/decide.js +25 -5
- package/dist/factories/providerDescriptors.d.ts +4 -3
- package/dist/factories/providerDescriptors.js +6 -4
- package/dist/hitl/hitlManager.d.ts +1 -0
- package/dist/hitl/hitlManager.js +2 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/neurolink.d.ts +4 -2
- package/dist/neurolink.js +36 -10
- package/dist/providers/catalog/provider-catalog.schema.json +64 -3
- package/dist/providers/catalog/schema.js +81 -1
- package/dist/providers/catalog/xor.json +30 -10
- package/dist/providers/configuredDecision.d.ts +8 -1
- package/dist/providers/configuredDecision.js +22 -0
- package/dist/providers/decide/dialects/openaiDecisions.js +8 -0
- package/dist/providers/decide/dialects/systemOne.js +28 -2
- package/dist/providers/decide/guards.d.ts +6 -0
- package/dist/providers/decide/guards.js +22 -0
- package/dist/providers/systemOneDecision.d.ts +30 -7
- package/dist/providers/systemOneDecision.js +71 -14
- package/dist/types/cli.d.ts +2 -0
- package/dist/types/decision.d.ts +117 -10
- package/dist/types/providerCatalog.d.ts +12 -1
- package/dist/types/providers.d.ts +2 -0
- package/dist/utils/decisionAnswers.d.ts +7 -1
- package/dist/utils/decisionAnswers.js +43 -2
- package/dist/utils/decisionLimits.d.ts +13 -1
- package/dist/utils/decisionLimits.js +52 -1
- package/dist/utils/decisionMedia.d.ts +4 -2
- package/dist/utils/decisionMedia.js +216 -14
- package/docs-site/static/search-index.json +24 -21
- package/package.json +1 -1
|
@@ -7,11 +7,13 @@ const MAX_ECHOED_PATH_CHARS = 200;
|
|
|
7
7
|
const DATA_URL = {
|
|
8
8
|
image: /^data:image\/[a-z0-9.+-]+;base64,/i,
|
|
9
9
|
video: /^data:video\/[a-z0-9.+-]+;base64,/i,
|
|
10
|
+
audio: /^data:audio\/[a-z0-9.+-]+;base64,/i,
|
|
10
11
|
};
|
|
11
12
|
const WEBM_HEADER = [0x1a, 0x45, 0xdf, 0xa3];
|
|
12
13
|
const NOT_RECOGNISED = {
|
|
13
14
|
image: "is not a recognised image format",
|
|
14
15
|
video: "is not a recognised video format (MP4, MOV or WebM)",
|
|
16
|
+
audio: "is not a recognised audio format (WAV, FLAC, Ogg or MP3)",
|
|
15
17
|
};
|
|
16
18
|
/**
|
|
17
19
|
* MP4 and MOV share the ISO-BMFF box. An AVIF or HEIC image shares it too, and
|
|
@@ -26,6 +28,153 @@ function sniffVideoMimeType(buffer) {
|
|
|
26
28
|
? "video/webm"
|
|
27
29
|
: null;
|
|
28
30
|
}
|
|
31
|
+
const ascii = (buffer, start, end) => buffer.subarray(start, end).toString("latin1");
|
|
32
|
+
/**
|
|
33
|
+
* The audio containers a decision model is known to decode. MP4 audio (M4A,
|
|
34
|
+
* AAC) is told apart so it can be refused by name: it was measured decoding to
|
|
35
|
+
* silence, with the request still answered as if it had been heard.
|
|
36
|
+
*/
|
|
37
|
+
function sniffAudioFormat(buffer) {
|
|
38
|
+
if (ascii(buffer, 0, 4) === "RIFF" && ascii(buffer, 8, 12) === "WAVE") {
|
|
39
|
+
return "wav";
|
|
40
|
+
}
|
|
41
|
+
if (ascii(buffer, 0, 4) === "fLaC") {
|
|
42
|
+
return "flac";
|
|
43
|
+
}
|
|
44
|
+
if (ascii(buffer, 0, 4) === "OggS") {
|
|
45
|
+
return "ogg";
|
|
46
|
+
}
|
|
47
|
+
if (hasFtypBoxSignature(buffer)) {
|
|
48
|
+
return "aac";
|
|
49
|
+
}
|
|
50
|
+
if (ascii(buffer, 0, 3) === "ID3") {
|
|
51
|
+
return "mp3";
|
|
52
|
+
}
|
|
53
|
+
// An MPEG frame sync. Layer bits 00 mark an ADTS (AAC) stream instead.
|
|
54
|
+
if (buffer.length >= 2 && buffer[0] === 0xff && (buffer[1] & 0xe0) === 0xe0) {
|
|
55
|
+
return (buffer[1] & 0x06) === 0 ? "aac" : "mp3";
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
/** A WAV clip's length from its `fmt ` byte rate and `data` size, or undefined. */
|
|
60
|
+
function wavSeconds(buffer) {
|
|
61
|
+
let byteRate;
|
|
62
|
+
let offset = 12;
|
|
63
|
+
while (offset + 8 <= buffer.length) {
|
|
64
|
+
const id = ascii(buffer, offset, offset + 4);
|
|
65
|
+
const size = buffer.readUInt32LE(offset + 4);
|
|
66
|
+
const body = offset + 8;
|
|
67
|
+
if (id === "fmt " && body + 12 <= buffer.length) {
|
|
68
|
+
byteRate = buffer.readUInt32LE(body + 8);
|
|
69
|
+
}
|
|
70
|
+
else if (id === "data") {
|
|
71
|
+
// A streamed WAV leaves the size unset; what follows is the data.
|
|
72
|
+
const dataBytes = size === 0 || size === 0xffffffff
|
|
73
|
+
? buffer.length - body
|
|
74
|
+
: Math.min(size, buffer.length - body);
|
|
75
|
+
return byteRate ? dataBytes / byteRate : undefined;
|
|
76
|
+
}
|
|
77
|
+
offset = body + size + (size % 2);
|
|
78
|
+
}
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
/** A FLAC clip's length from its STREAMINFO block, or undefined when unset. */
|
|
82
|
+
function flacSeconds(buffer) {
|
|
83
|
+
// "fLaC", a 4-byte block header, then STREAMINFO.
|
|
84
|
+
if (buffer.length < 26 || (buffer[4] & 0x7f) !== 0) {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
const info = 8;
|
|
88
|
+
const sampleRate = (buffer[info + 10] << 12) |
|
|
89
|
+
(buffer[info + 11] << 4) |
|
|
90
|
+
(buffer[info + 12] >> 4);
|
|
91
|
+
const totalSamples = (buffer[info + 13] & 0x0f) * 2 ** 32 + buffer.readUInt32BE(info + 14);
|
|
92
|
+
return sampleRate > 0 && totalSamples > 0
|
|
93
|
+
? totalSamples / sampleRate
|
|
94
|
+
: undefined;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* An Ogg clip's length: the granule position of its last page over the stream's
|
|
98
|
+
* sample rate. Opus counts at 48 kHz from its pre-skip; Vorbis at the rate in
|
|
99
|
+
* its identification header. Undefined for any other codec, or a stream whose
|
|
100
|
+
* pages cannot be read.
|
|
101
|
+
*/
|
|
102
|
+
function oggSeconds(buffer) {
|
|
103
|
+
// The first page carries the codec's identification packet.
|
|
104
|
+
const segments = buffer[26];
|
|
105
|
+
if (buffer.length < 28 || segments === undefined) {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
const packet = 27 + segments;
|
|
109
|
+
let rate;
|
|
110
|
+
let preSkip = 0;
|
|
111
|
+
if (ascii(buffer, packet, packet + 8) === "OpusHead") {
|
|
112
|
+
rate = 48000;
|
|
113
|
+
preSkip =
|
|
114
|
+
buffer.length >= packet + 12 ? buffer.readUInt16LE(packet + 10) : 0;
|
|
115
|
+
}
|
|
116
|
+
else if (ascii(buffer, packet, packet + 7) === "\x01vorbis") {
|
|
117
|
+
rate =
|
|
118
|
+
buffer.length >= packet + 16
|
|
119
|
+
? buffer.readUInt32LE(packet + 12)
|
|
120
|
+
: undefined;
|
|
121
|
+
}
|
|
122
|
+
const last = buffer.lastIndexOf("OggS");
|
|
123
|
+
if (!rate ||
|
|
124
|
+
last < 0 ||
|
|
125
|
+
last + 14 > buffer.length ||
|
|
126
|
+
buffer[last + 4] !== 0) {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
const granule = buffer.readBigInt64LE(last + 6);
|
|
130
|
+
if (granule <= 0n) {
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
return Math.max(0, Number(granule) - preSkip) / rate;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The clip in wire form, or why it cannot be sent. A clip whose length can be
|
|
137
|
+
* read (WAV, FLAC, Ogg Opus or Vorbis) and that runs past what the model hears is
|
|
138
|
+
* refused; the model would answer from its start alone. An MP3's length is not
|
|
139
|
+
* read: it needs every frame header walked.
|
|
140
|
+
*/
|
|
141
|
+
function encodeAudio(buffer, limits, label) {
|
|
142
|
+
const format = sniffAudioFormat(buffer);
|
|
143
|
+
if (format === null) {
|
|
144
|
+
return { status: "error", message: `${label} ${NOT_RECOGNISED.audio}.` };
|
|
145
|
+
}
|
|
146
|
+
if (format === "aac") {
|
|
147
|
+
return {
|
|
148
|
+
status: "error",
|
|
149
|
+
message: `${label} is AAC or M4A audio, which the model does not decode. Convert it to WAV, FLAC, Ogg or MP3.`,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
const seconds = format === "wav"
|
|
153
|
+
? wavSeconds(buffer)
|
|
154
|
+
: format === "flac"
|
|
155
|
+
? flacSeconds(buffer)
|
|
156
|
+
: format === "ogg"
|
|
157
|
+
? oggSeconds(buffer)
|
|
158
|
+
: undefined;
|
|
159
|
+
// Every WAV the model can open has both chunks; the server answers one
|
|
160
|
+
// without them with an engine error, which reads as a fault worth retrying.
|
|
161
|
+
if (format === "wav" && seconds === undefined) {
|
|
162
|
+
return {
|
|
163
|
+
status: "error",
|
|
164
|
+
message: `${label} is a WAV file without a complete fmt and data chunk.`,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
if (seconds !== undefined && seconds > limits.maxSeconds) {
|
|
168
|
+
return {
|
|
169
|
+
status: "error",
|
|
170
|
+
message: `${label} runs ${seconds.toFixed(1)} seconds; the model hears at most ${limits.maxSeconds}. Trim it, or split it across requests.`,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
status: "ok",
|
|
175
|
+
audio: { data: buffer.toString("base64"), format },
|
|
176
|
+
};
|
|
177
|
+
}
|
|
29
178
|
function encode(buffer, kind, label) {
|
|
30
179
|
const mime = kind === "image" ? sniffImageMimeType(buffer) : sniffVideoMimeType(buffer);
|
|
31
180
|
return mime === null
|
|
@@ -73,14 +222,15 @@ async function readMediaFile(path, label, maxBytes) {
|
|
|
73
222
|
}
|
|
74
223
|
}
|
|
75
224
|
/**
|
|
76
|
-
*
|
|
225
|
+
* A source resolved for one kind of media, with every refusal the three kinds
|
|
226
|
+
* share. Nothing here echoes a `data:` URL or a remote URL: either can carry a
|
|
77
227
|
* credential or megabytes of base64, and neither belongs in an error message.
|
|
78
228
|
*/
|
|
79
|
-
async function
|
|
229
|
+
async function readMediaSource(source, kind, label, maxBytes) {
|
|
80
230
|
if (typeof source !== "string") {
|
|
81
231
|
return source.length === 0
|
|
82
232
|
? { status: "error", message: `${label} is an empty Buffer.` }
|
|
83
|
-
:
|
|
233
|
+
: { status: "buffer", buffer: source };
|
|
84
234
|
}
|
|
85
235
|
const value = source.trim();
|
|
86
236
|
if (value === "") {
|
|
@@ -94,7 +244,7 @@ async function toDataUrl(source, kind, label, maxBytes) {
|
|
|
94
244
|
}
|
|
95
245
|
if (/^data:/i.test(value)) {
|
|
96
246
|
return DATA_URL[kind].test(value)
|
|
97
|
-
? { status: "
|
|
247
|
+
? { status: "dataUrl", dataUrl: value }
|
|
98
248
|
: {
|
|
99
249
|
status: "error",
|
|
100
250
|
message: `${label} is a data: URL that is not base64 ${kind} content.`,
|
|
@@ -107,24 +257,59 @@ async function toDataUrl(source, kind, label, maxBytes) {
|
|
|
107
257
|
};
|
|
108
258
|
}
|
|
109
259
|
const file = await readMediaFile(value, label, maxBytes);
|
|
110
|
-
return file.status === "ok"
|
|
260
|
+
return file.status === "ok"
|
|
261
|
+
? { status: "buffer", buffer: file.buffer }
|
|
262
|
+
: file;
|
|
263
|
+
}
|
|
264
|
+
/** An image or video as a `data:` URL: bytes are identified first, a URL of the right kind is sent as given. */
|
|
265
|
+
async function toDataUrl(source, kind, label, maxBytes) {
|
|
266
|
+
const read = await readMediaSource(source, kind, label, maxBytes);
|
|
267
|
+
if (read.status === "buffer") {
|
|
268
|
+
return encode(read.buffer, kind, label);
|
|
269
|
+
}
|
|
270
|
+
return read.status === "dataUrl"
|
|
271
|
+
? { status: "ok", dataUrl: read.dataUrl }
|
|
272
|
+
: read;
|
|
273
|
+
}
|
|
274
|
+
/** An audio clip as bytes: a `data:audio/…` URL is decoded, since the wire takes raw base64 and the container is sniffed. */
|
|
275
|
+
async function readAudioSource(source, label, maxBytes) {
|
|
276
|
+
const read = await readMediaSource(source, "audio", label, maxBytes);
|
|
277
|
+
if (read.status === "dataUrl") {
|
|
278
|
+
const base64 = read.dataUrl.slice(read.dataUrl.indexOf(",") + 1);
|
|
279
|
+
return { status: "ok", buffer: Buffer.from(base64, "base64") };
|
|
280
|
+
}
|
|
281
|
+
return read.status === "buffer"
|
|
282
|
+
? { status: "ok", buffer: read.buffer }
|
|
283
|
+
: read;
|
|
111
284
|
}
|
|
112
285
|
/**
|
|
113
|
-
* Turn the caller's images and video into `data:` URLs,
|
|
114
|
-
* Runs before any request, so every
|
|
286
|
+
* Turn the caller's images and video into `data:` URLs, and an audio clip into
|
|
287
|
+
* `{ data, format }`, or say why not. Runs before any request, so every
|
|
288
|
+
* refusal costs nothing and none is retried.
|
|
115
289
|
*/
|
|
116
290
|
export async function prepareDecisionMedia(request, limits, vendor, alternatives) {
|
|
117
291
|
const images = request.images ?? [];
|
|
118
|
-
if (images.length === 0 &&
|
|
292
|
+
if (images.length === 0 &&
|
|
293
|
+
request.video === undefined &&
|
|
294
|
+
request.audio === undefined) {
|
|
119
295
|
return { status: "prepared", media: undefined };
|
|
120
296
|
}
|
|
297
|
+
const use = alternatives.length > 0
|
|
298
|
+
? ` Use a decision provider that does: ${alternatives.join(", ")}.`
|
|
299
|
+
: "";
|
|
121
300
|
if (!limits) {
|
|
122
|
-
const
|
|
123
|
-
?
|
|
124
|
-
: "";
|
|
301
|
+
const what = images.length === 0 && request.video === undefined
|
|
302
|
+
? "audio"
|
|
303
|
+
: "images or video";
|
|
125
304
|
return {
|
|
126
305
|
status: "refused",
|
|
127
|
-
message: `${vendor} does not accept
|
|
306
|
+
message: `${vendor} does not accept ${what}.${use}`,
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
if (request.audio !== undefined && limits.audio === undefined) {
|
|
310
|
+
return {
|
|
311
|
+
status: "refused",
|
|
312
|
+
message: `${vendor} does not accept audio.${use}`,
|
|
128
313
|
};
|
|
129
314
|
}
|
|
130
315
|
if (images.length > limits.maxImages) {
|
|
@@ -152,9 +337,26 @@ export async function prepareDecisionMedia(request, limits, vendor, alternatives
|
|
|
152
337
|
}
|
|
153
338
|
video = converted.dataUrl;
|
|
154
339
|
}
|
|
155
|
-
|
|
340
|
+
let audio;
|
|
341
|
+
if (request.audio !== undefined && limits.audio !== undefined) {
|
|
342
|
+
const read = await readAudioSource(request.audio, "The audio", limits.maxRequestBytes);
|
|
343
|
+
if (read.status === "error") {
|
|
344
|
+
return { status: "refused", message: read.message };
|
|
345
|
+
}
|
|
346
|
+
const encoded = encodeAudio(read.buffer, limits.audio, "The audio");
|
|
347
|
+
if (encoded.status === "error") {
|
|
348
|
+
return { status: "refused", message: encoded.message };
|
|
349
|
+
}
|
|
350
|
+
audio = encoded.audio;
|
|
351
|
+
}
|
|
352
|
+
const bytes = prepared.reduce((sum, url) => sum + url.length, (video?.length ?? 0) + (audio?.data.length ?? 0));
|
|
156
353
|
return {
|
|
157
354
|
status: "prepared",
|
|
158
|
-
media: {
|
|
355
|
+
media: {
|
|
356
|
+
images: prepared,
|
|
357
|
+
...(video ? { video } : {}),
|
|
358
|
+
...(audio ? { audio } : {}),
|
|
359
|
+
bytes,
|
|
360
|
+
},
|
|
159
361
|
};
|
|
160
362
|
}
|