brsh 3.3.0 → 3.4.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/lib/Terminal.js CHANGED
@@ -261,6 +261,10 @@ class Terminal {
261
261
  this._flushTypeQueue();
262
262
  this.parser.reset();
263
263
  this.renderer.flushRender();
264
+ this._mobileInputBuffer = '';
265
+ if (this._mobileInputElement) {
266
+ this._mobileInputElement.value = '';
267
+ }
264
268
  }
265
269
 
266
270
  /**
@@ -528,6 +532,10 @@ class Terminal {
528
532
  this._autoScroll();
529
533
  }
530
534
  const isPureEnter = data === '\r' || data === '\n' || data === '\r\n';
535
+ if (isPureEnter && this._mobileInputElement) {
536
+ this._mobileInputElement.value = '';
537
+ this._mobileInputBuffer = '';
538
+ }
531
539
  this._suppressAnimationUntil = isPureEnter ? 0 : Date.now() + 80;
532
540
  this._isHandlingInput = !isPureEnter;
533
541
  try {
@@ -884,6 +892,11 @@ class Terminal {
884
892
  input.style.zIndex = '-1';
885
893
  input.setAttribute('aria-hidden', 'true');
886
894
  input.setAttribute('tabindex', '-1');
895
+ input.setAttribute('autocapitalize', 'none');
896
+ input.setAttribute('autocorrect', 'off');
897
+ input.setAttribute('autocomplete', 'off');
898
+ input.setAttribute('spellcheck', 'false');
899
+ input.setAttribute('enterkeyhint', 'go');
887
900
  this._el.appendChild(input);
888
901
  this._mobileInputElement = input;
889
902
  this._mobileInputBuffer = '';
@@ -893,28 +906,117 @@ class Terminal {
893
906
  if (e && e.target && e.target.tagName === 'A') return;
894
907
  if (typeof input.focus === 'function') {
895
908
  input.focus({ preventScroll: true });
909
+ if (typeof input.setSelectionRange === 'function') {
910
+ const len = (input.value || '').length;
911
+ input.setSelectionRange(len, len);
912
+ }
896
913
  }
897
914
  };
898
915
  this._el.addEventListener('click', this._onMobileInputClick);
899
916
 
900
- input.addEventListener('keypress', e => {
901
- if (e.key === 'Enter') {
917
+ const applyDiff = () => {
918
+ const oldVal = this._mobileInputBuffer || '';
919
+ const newVal = input.value || '';
920
+ if (oldVal === newVal) return;
921
+
922
+ let prefix = 0;
923
+ while (prefix < oldVal.length && prefix < newVal.length && oldVal[prefix] === newVal[prefix]) {
924
+ prefix++;
925
+ }
926
+
927
+ let suffix = 0;
928
+ while (
929
+ suffix < (oldVal.length - prefix) &&
930
+ suffix < (newVal.length - prefix) &&
931
+ oldVal[oldVal.length - 1 - suffix] === newVal[newVal.length - 1 - suffix]
932
+ ) {
933
+ suffix++;
934
+ }
935
+
936
+ const deletedCount = oldVal.length - prefix - suffix;
937
+ const insertedText = newVal.slice(prefix, newVal.length - suffix);
938
+
939
+ if (deletedCount > 0) {
940
+ for (let i = 0; i < deletedCount; i++) {
941
+ this.sendKey('Backspace');
942
+ }
943
+ }
944
+
945
+ if (insertedText) {
946
+ const normalized = insertedText.replace(/\r?\n/g, '\r');
947
+ this._emitInput(normalized);
948
+ }
949
+
950
+ this._mobileInputBuffer = newVal;
951
+ };
952
+
953
+ const onInput = (e) => {
954
+ if (!e) {
955
+ applyDiff();
956
+ return;
957
+ }
958
+
959
+ const inputType = e.inputType;
960
+ if (inputType === 'insertLineBreak' || inputType === 'insertParagraph') {
961
+ this.sendKey('Enter');
902
962
  input.value = '';
903
- this._mobileInputBuffer = input.value;
963
+ this._mobileInputBuffer = '';
964
+ return;
965
+ }
966
+
967
+ if (inputType === 'deleteContentBackward') {
968
+ this.sendKey('Backspace');
969
+ this._mobileInputBuffer = input.value || '';
970
+ return;
971
+ }
972
+
973
+ if (inputType === 'deleteContentForward') {
974
+ this.sendKey('Delete');
975
+ this._mobileInputBuffer = input.value || '';
976
+ return;
977
+ }
978
+
979
+ if (inputType === 'deleteWordBackward') {
980
+ this.sendKey('Backspace', { alt: true });
981
+ this._mobileInputBuffer = input.value || '';
982
+ return;
983
+ }
984
+
985
+ if (inputType === 'insertText' && typeof e.data === 'string' && e.data.length > 0) {
986
+ const expectedLen = (this._mobileInputBuffer || '').length + e.data.length;
987
+ if (!input.value || input.value.length === expectedLen) {
988
+ const normalized = e.data.replace(/\r?\n/g, '\r');
989
+ this._emitInput(normalized);
990
+ this._mobileInputBuffer = input.value || '';
991
+ return;
992
+ }
993
+ }
994
+
995
+ applyDiff();
996
+ };
997
+
998
+ input.addEventListener('input', onInput);
999
+
1000
+ input.addEventListener('keydown', (e) => {
1001
+ if (e && (e.key === 'Enter' || e.keyCode === 13)) {
1002
+ if (!e.defaultPrevented) {
1003
+ this.sendKey('Enter');
1004
+ input.value = '';
1005
+ this._mobileInputBuffer = '';
1006
+ if (e.preventDefault) e.preventDefault();
1007
+ }
904
1008
  }
905
1009
  });
906
1010
 
907
1011
  input.addEventListener('keyup', (e) => {
908
- if (e.key === 'Unidentified') {
909
- if (input.value.length < this._mobileInputBuffer.length) {
910
- this.sendKey('Backspace');
911
- } else if (input.value.length > this._mobileInputBuffer.length) {
912
- const char = input.value[input.value.length - 1];
913
- this.sendKey(char);
914
- }
915
- this._mobileInputBuffer = input.value;
1012
+ if (e && e.key === 'Unidentified') {
1013
+ applyDiff();
916
1014
  }
917
1015
  });
1016
+
1017
+ input.addEventListener('compositionend', () => {
1018
+ applyDiff();
1019
+ });
918
1020
  }
919
1021
 
920
1022
  _autoScroll() {
@@ -4,7 +4,29 @@ const Command = require('../../Command');
4
4
  const ReplCommand = require('../../ReplCommand');
5
5
  const { readBytesFromFs, writeBytesToFs, loadModule } = require('../utils');
6
6
 
7
- const DEFAULT_MODEL_URL = 'https://huggingface.co/ggml-org/models/resolve/main/tinyllamas/stories260K.gguf';
7
+ /**
8
+ * Pre-configured friendly aliases for lightweight instruct models compatible with Wllama in-browser runtime.
9
+ */
10
+ const MODEL_ALIASES = {
11
+ smol: 'https://huggingface.co/bartowski/SmolLM2-135M-Instruct-GGUF/resolve/main/SmolLM2-135M-Instruct-Q4_K_M.gguf',
12
+ smollm: 'https://huggingface.co/bartowski/SmolLM2-135M-Instruct-GGUF/resolve/main/SmolLM2-135M-Instruct-Q4_K_M.gguf',
13
+ smollm2: 'https://huggingface.co/bartowski/SmolLM2-135M-Instruct-GGUF/resolve/main/SmolLM2-135M-Instruct-Q4_K_M.gguf',
14
+ 'smol-135m': 'https://huggingface.co/bartowski/SmolLM2-135M-Instruct-GGUF/resolve/main/SmolLM2-135M-Instruct-Q4_K_M.gguf',
15
+ 'smol-360m': 'https://huggingface.co/bartowski/SmolLM2-360M-Instruct-GGUF/resolve/main/SmolLM2-360M-Instruct-Q4_K_M.gguf',
16
+ 'smollm-360m': 'https://huggingface.co/bartowski/SmolLM2-360M-Instruct-GGUF/resolve/main/SmolLM2-360M-Instruct-Q4_K_M.gguf',
17
+ 'smollm2-360m': 'https://huggingface.co/bartowski/SmolLM2-360M-Instruct-GGUF/resolve/main/SmolLM2-360M-Instruct-Q4_K_M.gguf',
18
+ 'smol-1.7b': 'https://huggingface.co/bartowski/SmolLM2-1.7B-Instruct-GGUF/resolve/main/SmolLM2-1.7B-Instruct-Q4_K_M.gguf',
19
+ qwen: 'https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/main/qwen2.5-0.5b-instruct-q4_k_m.gguf',
20
+ 'qwen2.5': 'https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/main/qwen2.5-0.5b-instruct-q4_k_m.gguf',
21
+ 'qwen-0.5b': 'https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/main/qwen2.5-0.5b-instruct-q4_k_m.gguf',
22
+ 'qwen2.5-0.5b': 'https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/main/qwen2.5-0.5b-instruct-q4_k_m.gguf',
23
+ 'qwen-1.5b': 'https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct-GGUF/resolve/main/qwen2.5-1.5b-instruct-q4_k_m.gguf',
24
+ 'qwen2.5-1.5b': 'https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct-GGUF/resolve/main/qwen2.5-1.5b-instruct-q4_k_m.gguf',
25
+ stories: 'https://huggingface.co/ggml-org/models/resolve/main/tinyllamas/stories260K.gguf',
26
+ stories15m: 'https://huggingface.co/ggml-org/models/resolve/main/tinyllamas/stories15M.gguf'
27
+ };
28
+
29
+ const DEFAULT_MODEL_URL = MODEL_ALIASES.smol;
8
30
  const DEFAULT_WASM_URL = 'https://cdn.jsdelivr.net/npm/@wllama/wllama@3.8.1/src/wasm/wllama.wasm';
9
31
  const DEFAULT_PERSIST_DIR = '/var/cache/wllama';
10
32
 
@@ -77,7 +99,7 @@ function resolveModelSpec(rawSpec, hfRepo = null, hfFile = null, hfQuant = null)
77
99
  };
78
100
  }
79
101
 
80
- const spec = (rawSpec || '').trim();
102
+ let spec = (rawSpec || '').trim();
81
103
  if (!spec) {
82
104
  return {
83
105
  kind: 'url',
@@ -86,6 +108,11 @@ function resolveModelSpec(rawSpec, hfRepo = null, hfFile = null, hfQuant = null)
86
108
  };
87
109
  }
88
110
 
111
+ const lower = spec.toLowerCase();
112
+ if (MODEL_ALIASES[lower]) {
113
+ spec = MODEL_ALIASES[lower];
114
+ }
115
+
89
116
  if (/^hf:\/\//i.test(spec) || /^hf:/i.test(spec)) {
90
117
  const stripped = spec.replace(/^hf:(\/\/)?/i, '');
91
118
  // Support `hf:owner/repo:Q4_K_M`
@@ -649,8 +676,8 @@ function createWllamaCommand(pkgMeta = {}, wbtConfig = {}, binName = 'wllama') {
649
676
  }
650
677
 
651
678
  if (positional.length > 0) {
652
- // If no `-m` model was provided and first positional token looks like a .gguf path/URL/hf spec (or subcommand is pull)
653
- if (!model && !hfRepo && (subcommand === 'pull' || /\.gguf(\?.*)?$/i.test(positional[0]) || /^hf:/i.test(positional[0]) || /^https?:\/\//i.test(positional[0]))) {
679
+ // If no `-m` model was provided and first positional token looks like an alias, .gguf path, URL, or hf spec (or subcommand is pull)
680
+ if (!model && !hfRepo && (subcommand === 'pull' || MODEL_ALIASES[positional[0].toLowerCase()] || /\.gguf(\?.*)?$/i.test(positional[0]) || /^hf:/i.test(positional[0]) || /^https?:\/\//i.test(positional[0]))) {
654
681
  model = positional.shift();
655
682
  }
656
683
  if (subcommand === 'pull' && positional.length > 0 && !persistPath) {
@@ -1117,7 +1144,7 @@ function createWllamaCommand(pkgMeta = {}, wbtConfig = {}, binName = 'wllama') {
1117
1144
  ' wllama bench Run ggml WebGPU/CPU backend ops test',
1118
1145
  '',
1119
1146
  'Options:',
1120
- ' -m, --model=MODEL Path in brsh filesystem, URL, or hf:owner/repo/file.gguf',
1147
+ ' -m, --model=MODEL Path in brsh fs, URL, HF spec, or alias (default: smol [SmolLM2-135M]; also: qwen)',
1121
1148
  ' --hf=REPO Hugging Face model repository (e.g. Qwen/Qwen2.5-0.5B-Instruct-GGUF)',
1122
1149
  ' --quant=QUANT Hugging Face GGUF quantization (default: Q4_K_M)',
1123
1150
  ' --persist[=PATH] Persist pulled remote model into brsh filesystem (default: /var/cache/wllama/)',
@@ -1284,3 +1311,5 @@ module.exports.getDefaultPersistPath = getDefaultPersistPath;
1284
1311
  module.exports.DEFAULT_PERSIST_DIR = DEFAULT_PERSIST_DIR;
1285
1312
  module.exports.resolveModelSpec = resolveModelSpec;
1286
1313
  module.exports.formatDownloadProgress = formatDownloadProgress;
1314
+ module.exports.DEFAULT_MODEL_URL = DEFAULT_MODEL_URL;
1315
+ module.exports.MODEL_ALIASES = MODEL_ALIASES;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brsh",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "description": "A unix like shell written in javascript",
5
5
  "main": "./dist/shell.min.js",
6
6
  "module": "./dist/shell.es.js",
@@ -882,5 +882,131 @@ describe('Output Typing Animation Suppression', () => {
882
882
  expect(el.children).not.toContain(input);
883
883
  expect(term._mobileInputElement).toBeNull();
884
884
  });
885
+
886
+ it('disables autocapitalize, autocorrect, autocomplete, and spellcheck on mobile input', () => {
887
+ const el = document.createElement('div');
888
+ const term = new Terminal({
889
+ el,
890
+ Shell: false,
891
+ mobileInput: 'click'
892
+ });
893
+
894
+ const input = term._mobileInputElement;
895
+ expect(input.autocapitalize).toBe('none');
896
+ expect(input.autocorrect).toBe('off');
897
+ expect(input.autocomplete).toBe('off');
898
+ expect(input.spellcheck).toBe('false');
899
+ expect(input.enterkeyhint).toBe('go');
900
+ term.destroy();
901
+ });
902
+
903
+ it('handles mobile character typing when characters are prepended due to zero caret', () => {
904
+ const el = document.createElement('div');
905
+ const term = new Terminal({
906
+ el,
907
+ Shell: false,
908
+ mobileInput: 'click'
909
+ });
910
+
911
+ const input = term._mobileInputElement;
912
+ const received = [];
913
+ term.onData((data) => received.push(data));
914
+
915
+ // Type 'h'
916
+ input.value = 'h';
917
+ input.dispatchEvent('input');
918
+
919
+ // Type 'e' (prepended at offset 0 due to zero-size input caret, value becomes 'eh')
920
+ input.value = 'eh';
921
+ input.dispatchEvent('input');
922
+
923
+ // Type 'l' (prepended at offset 0, value becomes 'leh')
924
+ input.value = 'leh';
925
+ input.dispatchEvent('input');
926
+
927
+ // Type 'p' (prepended at offset 0, value becomes 'pleh')
928
+ input.value = 'pleh';
929
+ input.dispatchEvent('input');
930
+
931
+ expect(received).toEqual(['h', 'e', 'l', 'p']);
932
+ term.destroy();
933
+ });
934
+
935
+ it('handles mobile typing via inputType insertText and e.data', () => {
936
+ const el = document.createElement('div');
937
+ const term = new Terminal({
938
+ el,
939
+ Shell: false,
940
+ mobileInput: 'click'
941
+ });
942
+
943
+ const input = term._mobileInputElement;
944
+ const received = [];
945
+ term.onData((data) => received.push(data));
946
+
947
+ input.value = 'a';
948
+ input.dispatchEvent('input', { inputType: 'insertText', data: 'a' });
949
+
950
+ input.value = 'ab';
951
+ input.dispatchEvent('input', { inputType: 'insertText', data: 'b' });
952
+
953
+ input.value = 'abc';
954
+ input.dispatchEvent('input', { inputType: 'insertText', data: 'c' });
955
+
956
+ expect(received).toEqual(['a', 'b', 'c']);
957
+ term.destroy();
958
+ });
959
+
960
+ it('handles mobile backspace and Enter', () => {
961
+ const el = document.createElement('div');
962
+ const term = new Terminal({
963
+ el,
964
+ Shell: false,
965
+ mobileInput: 'click'
966
+ });
967
+
968
+ const input = term._mobileInputElement;
969
+ const received = [];
970
+ term.onData((data) => received.push(data));
971
+
972
+ // Type 'hi'
973
+ input.value = 'hi';
974
+ input.dispatchEvent('input', { inputType: 'insertText', data: 'hi' });
975
+
976
+ // Backspace via inputType
977
+ input.value = 'h';
978
+ input.dispatchEvent('input', { inputType: 'deleteContentBackward' });
979
+
980
+ // Enter via insertLineBreak
981
+ input.dispatchEvent('input', { inputType: 'insertLineBreak' });
982
+
983
+ expect(received).toEqual(['hi', '\x7f', '\r']);
984
+ expect(input.value).toBe('');
985
+ expect(term._mobileInputBuffer).toBe('');
986
+
987
+ term.destroy();
988
+ });
989
+
990
+ it('handles Android keyup with Unidentified via diff', () => {
991
+ const el = document.createElement('div');
992
+ const term = new Terminal({
993
+ el,
994
+ Shell: false,
995
+ mobileInput: 'click'
996
+ });
997
+
998
+ const input = term._mobileInputElement;
999
+ const received = [];
1000
+ term.onData((data) => received.push(data));
1001
+
1002
+ input.value = 'x';
1003
+ input.dispatchEvent('keyup', { key: 'Unidentified' });
1004
+
1005
+ input.value = 'yx'; // prepended 'y'
1006
+ input.dispatchEvent('keyup', { key: 'Unidentified' });
1007
+
1008
+ expect(received).toEqual(['x', 'y']);
1009
+ term.destroy();
1010
+ });
885
1011
  });
886
1012
  });
package/test/wbt.test.js CHANGED
@@ -6,7 +6,7 @@ const require = createRequire(import.meta.url);
6
6
  const path = require('path');
7
7
  const fs = require('fs');
8
8
  const Wbt = require('../lib/wbt/index.js');
9
- const { wllamaInstances } = require('../lib/wbt/packages/wllama.js');
9
+ const { wllamaInstances, DEFAULT_MODEL_URL, MODEL_ALIASES, resolveModelSpec } = require('../lib/wbt/packages/wllama.js');
10
10
 
11
11
  const NANO_WASM_PATH = path.resolve(import.meta.dirname, '../../nano-wasm/dist/nano.wasm');
12
12
  const NANO_WASM_BYTES = (() => {
@@ -1294,6 +1294,39 @@ describe('WBT Wllama v3 WASM & WebGPU package (`wllama`, `llama`, `llama-cli`)',
1294
1294
  expect(modelsRes.output.join('\n')).toContain('/var/cache/wllama/Qwen2.5-0.5B-Instruct-GGUF-Q4_K_M.gguf');
1295
1295
  expect(modelsRes.output.join('\n')).toContain('persisted (fs)');
1296
1296
  });
1297
+
1298
+ it('defaults to SmolLM2 model and supports friendly model aliases like smol and qwen', async () => {
1299
+ // Verify default model URL is SmolLM2-135M rather than stories
1300
+ expect(DEFAULT_MODEL_URL).toContain('SmolLM2-135M-Instruct');
1301
+ expect(DEFAULT_MODEL_URL).not.toContain('stories');
1302
+
1303
+ // Verify resolveModelSpec helper maps empty input to SmolLM2 default
1304
+ const defaultSpec = resolveModelSpec();
1305
+ expect(defaultSpec.url).toBe(DEFAULT_MODEL_URL);
1306
+ expect(defaultSpec.url).toContain('SmolLM2-135M-Instruct-Q4_K_M.gguf');
1307
+
1308
+ // Verify smol and qwen aliases
1309
+ const smolSpec = resolveModelSpec('smol');
1310
+ expect(smolSpec.url).toBe(MODEL_ALIASES.smol);
1311
+ const qwenSpec = resolveModelSpec('qwen');
1312
+ expect(qwenSpec.url).toBe(MODEL_ALIASES.qwen);
1313
+ expect(qwenSpec.url).toContain('qwen2.5-0.5b-instruct-q4_k_m.gguf');
1314
+
1315
+ // Run wllama with no -m flag (uses default SmolLM2 model)
1316
+ const defaultRes = await run(shell, 'wllama -p "Default model prompt"');
1317
+ expect(defaultRes.exitCode).toBe(0);
1318
+ expect(defaultRes.output.join('\n')).toContain('Reply(WebGPU,turns=1): Default model prompt');
1319
+
1320
+ // Run wllama pull using alias 'qwen'
1321
+ const pullQwenRes = await run(shell, 'wllama pull qwen -q');
1322
+ expect(pullQwenRes.exitCode).toBe(0);
1323
+ expect(shell.context.fs.exists('/var/cache/wllama/qwen2.5-0.5b-instruct-q4_k_m.gguf')).toBe(true);
1324
+
1325
+ // Run wllama using alias 'qwen' with -m
1326
+ const qwenRes = await run(shell, 'wllama -m qwen -p "Hello Qwen"');
1327
+ expect(qwenRes.exitCode).toBe(0);
1328
+ expect(qwenRes.output.join('\n')).toContain('Reply(WebGPU,turns=1): Hello Qwen');
1329
+ });
1297
1330
  });
1298
1331
 
1299
1332
  describe('WBT GNU nano WASM package (nano / nano-wasm)', () => {