brsh 3.2.0 → 3.3.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/test/wbt.test.js CHANGED
@@ -440,14 +440,28 @@ function createMockWllama() {
440
440
  return new Uint8Array([0x47, 0x47, 0x55, 0x46, 0x03, 0x00, 0x00, 0x00]);
441
441
  }
442
442
 
443
- async createChatCompletion(opts) {
444
- const messages = opts.messages || [];
443
+ createChatCompletion(opts) {
444
+ const messages = (opts && opts.messages) || [];
445
445
  const sysMsg = messages.find(m => m.role === 'system');
446
446
  const userMsgs = messages.filter(m => m.role === 'user').map(m => m.content);
447
447
  const prefix = sysMsg ? `[sys:${sysMsg.content}] ` : '';
448
448
  const gpuTag = this._loadParams.n_gpu_layers === 0 ? 'CPU' : 'WebGPU';
449
449
  const reply = `${prefix}Reply(${gpuTag},turns=${userMsgs.length}): ${userMsgs[userMsgs.length - 1] || ''}`;
450
- return {
450
+
451
+ if (opts && opts.stream) {
452
+ return (async function* () {
453
+ const parts = reply.split(' ');
454
+ for (let i = 0; i < parts.length; i++) {
455
+ const token = (i === 0 ? '' : ' ') + parts[i];
456
+ yield {
457
+ id: 'chatcmpl-wllama',
458
+ choices: [{ delta: { content: token } }]
459
+ };
460
+ }
461
+ })();
462
+ }
463
+
464
+ return Promise.resolve({
451
465
  id: 'chatcmpl-wllama',
452
466
  object: 'chat.completion',
453
467
  choices: [
@@ -457,21 +471,35 @@ function createMockWllama() {
457
471
  finish_reason: 'stop'
458
472
  }
459
473
  ]
460
- };
474
+ });
461
475
  }
462
476
 
463
- async createCompletion(opts) {
464
- return {
477
+ createCompletion(opts) {
478
+ const reply = `${opts.prompt} [completed]`;
479
+ if (opts && opts.stream) {
480
+ return (async function* () {
481
+ const parts = reply.split(' ');
482
+ for (let i = 0; i < parts.length; i++) {
483
+ const token = (i === 0 ? '' : ' ') + parts[i];
484
+ yield {
485
+ id: 'cmpl-wllama',
486
+ choices: [{ text: token }]
487
+ };
488
+ }
489
+ })();
490
+ }
491
+
492
+ return Promise.resolve({
465
493
  id: 'cmpl-wllama',
466
494
  object: 'text_completion',
467
495
  choices: [
468
496
  {
469
497
  index: 0,
470
- text: `${opts.prompt} [completed]`,
498
+ text: reply,
471
499
  finish_reason: 'stop'
472
500
  }
473
501
  ]
474
- };
502
+ });
475
503
  }
476
504
 
477
505
  async createEmbedding(opts) {
@@ -1108,6 +1136,62 @@ describe('WBT Wllama v3 WASM & WebGPU package (`wllama`, `llama`, `llama-cli`)',
1108
1136
  expect(rawRes.output.join('\n')).toContain('Explain WebGPU in one sentence. [completed]');
1109
1137
  });
1110
1138
 
1139
+ it('streams tokens in real time as they are generated and supports --no-stream', async () => {
1140
+ const streamedTokens = [];
1141
+ const rawChunks = [];
1142
+ const onToken = (token) => streamedTokens.push(token);
1143
+ const onRaw = (chunk) => rawChunks.push(chunk);
1144
+
1145
+ shell.on('token', onToken);
1146
+ shell.on('stdOutRaw', onRaw);
1147
+
1148
+ const res = await run(
1149
+ shell,
1150
+ 'wllama -m /home/tinyllama.gguf -p "Stream me"'
1151
+ );
1152
+
1153
+ shell.removeListener('token', onToken);
1154
+ shell.removeListener('stdOutRaw', onRaw);
1155
+
1156
+ expect(res.exitCode).toBe(0);
1157
+ expect(res.output.join('\n')).toContain('Reply(WebGPU,turns=1): Stream me');
1158
+ // Verified tokens were streamed incrementally
1159
+ expect(streamedTokens.length).toBeGreaterThan(1);
1160
+ expect(streamedTokens.join('')).toContain('Stream me');
1161
+ expect(rawChunks.length).toBeGreaterThan(1);
1162
+
1163
+ // Test with --no-stream flag
1164
+ const noStreamTokens = [];
1165
+ const onNoStreamToken = (token) => noStreamTokens.push(token);
1166
+ shell.on('token', onNoStreamToken);
1167
+
1168
+ const noStreamRes = await run(
1169
+ shell,
1170
+ 'wllama -m /home/tinyllama.gguf --no-stream -p "No stream"'
1171
+ );
1172
+
1173
+ shell.removeListener('token', onNoStreamToken);
1174
+
1175
+ expect(noStreamRes.exitCode).toBe(0);
1176
+ expect(noStreamRes.output.join('\n')).toContain('Reply(WebGPU,turns=1): No stream');
1177
+ expect(noStreamTokens.length).toBe(0);
1178
+
1179
+ // Test status indication (loading model, running inference)
1180
+ const statusUpdates = [];
1181
+ const onStatus = (chunk) => statusUpdates.push(chunk);
1182
+ shell.stderr.on('data', onStatus);
1183
+
1184
+ await run(
1185
+ shell,
1186
+ 'wllama -m /home/tinyllama.gguf -p "Status test"'
1187
+ );
1188
+
1189
+ shell.stderr.removeListener('data', onStatus);
1190
+ const combinedStatus = statusUpdates.join('');
1191
+ expect(combinedStatus).toMatch(/Loading model|Loaded model/);
1192
+ expect(combinedStatus).toContain('Running inference');
1193
+ });
1194
+
1111
1195
  it('supports Hugging Face model loading (--hf / hf:), embeddings (wllama embed), info, and bench', async () => {
1112
1196
  const hfRes = await run(
1113
1197
  shell,