bitgpu 0.19.0 → 0.19.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
@@ -53,7 +53,7 @@ weights stream straight from the Hugging Face Hub. This runs as-is:
53
53
  import { createEngine } from 'bitgpu'
54
54
  import { createChat } from 'bitgpu/chat'
55
55
 
56
- const REPO = 'https://cdn.jsdelivr.net/gh/stfurkan/bitgpu@v0.19.0/models/bonsai-1.7b-gguf'
56
+ const REPO = 'https://cdn.jsdelivr.net/gh/stfurkan/bitgpu@v0.19.1/models/bonsai-1.7b-gguf'
57
57
  const TOK = 'https://huggingface.co/onnx-community/Bonsai-1.7B-ONNX/resolve/main'
58
58
  const engine = await createEngine({
59
59
  manifestUrl: `${REPO}/manifest.json`,
package/dist/chat.d.ts CHANGED
@@ -246,6 +246,8 @@ declare class ToolBodyMachineXml implements ToolBody {
246
246
  private elem;
247
247
  private keyBuf;
248
248
  private keyCands;
249
+ private declared;
250
+ private valWs;
249
251
  private reqLeft;
250
252
  private valTail;
251
253
  private valStarted;
@@ -381,9 +383,13 @@ declare class ThinkBudget {
381
383
  private closed;
382
384
  private run;
383
385
  private earlyFired;
386
+ private seen;
384
387
  constructor(openId: number | undefined, closeId: number | undefined, budget: number, startInside: boolean, early?: ThinkEarlyStop | null);
385
388
  advance(id: number): void;
386
- /** Feed one step's candidate logits (descending). Only meaningful inside think. */
389
+ /** Feed one step's candidate logits (descending). Only meaningful inside think. Counted at most
390
+ * once per emitted step: the engine re-invokes the candidate filter (and thus observe) once per
391
+ * 512-token batch when it has to walk the full vocabulary for a forced token, and those
392
+ * batch-local gaps must not eat the early-stop window. */
387
393
  observe(vals: ArrayLike<number>): void;
388
394
  /** The forced token id once the budget is exhausted or early stop fired, else null. */
389
395
  force(): number | null;
package/dist/chat.js CHANGED
@@ -4419,6 +4419,7 @@ var ThinkBudget = class {
4419
4419
  closed = false;
4420
4420
  run = 0;
4421
4421
  earlyFired = false;
4422
+ seen = false;
4422
4423
  constructor(openId, closeId, budget, startInside, early = null) {
4423
4424
  this.openId = openId;
4424
4425
  this.closeId = closeId;
@@ -4438,10 +4439,15 @@ var ThinkBudget = class {
4438
4439
  return;
4439
4440
  }
4440
4441
  this.spent++;
4442
+ this.seen = false;
4441
4443
  }
4442
- /** Feed one step's candidate logits (descending). Only meaningful inside think. */
4444
+ /** Feed one step's candidate logits (descending). Only meaningful inside think. Counted at most
4445
+ * once per emitted step: the engine re-invokes the candidate filter (and thus observe) once per
4446
+ * 512-token batch when it has to walk the full vocabulary for a forced token, and those
4447
+ * batch-local gaps must not eat the early-stop window. */
4443
4448
  observe(vals) {
4444
- if (!this.early || !this.inThink || this.closed || this.earlyFired || vals.length < 2) return;
4449
+ if (!this.early || !this.inThink || this.closed || this.earlyFired || this.seen || vals.length < 2) return;
4450
+ this.seen = true;
4445
4451
  const confident = vals[0] - vals[1] >= this.early.gap;
4446
4452
  this.run = confident ? this.run + 1 : 0;
4447
4453
  if (this.spent >= this.early.minTokens && this.run >= this.early.window) this.earlyFired = true;
@@ -5258,6 +5264,8 @@ var ToolBodyMachineXml = class ToolBodyMachineXml {
5258
5264
  elem = [];
5259
5265
  keyBuf = "";
5260
5266
  keyCands = [];
5267
+ declared = false;
5268
+ valWs = 0;
5261
5269
  reqLeft = [];
5262
5270
  valTail = [];
5263
5271
  valStarted = false;
@@ -5284,6 +5292,8 @@ var ToolBodyMachineXml = class ToolBodyMachineXml {
5284
5292
  m.keyCands = [...this.keyCands];
5285
5293
  m.reqLeft = [...this.reqLeft];
5286
5294
  m.valTail = [...this.valTail];
5295
+ m.declared = this.declared;
5296
+ m.valWs = this.valWs;
5287
5297
  m.valStarted = this.valStarted;
5288
5298
  m.complete = this.complete;
5289
5299
  m.name = this.name;
@@ -5331,6 +5341,7 @@ var ToolBodyMachineXml = class ToolBodyMachineXml {
5331
5341
  this.name = this.nameBuf;
5332
5342
  const spec = this.props.get(this.nameBuf);
5333
5343
  this.keyCands = [...spec?.keys ?? []];
5344
+ this.declared = this.keyCands.length > 0;
5334
5345
  this.reqLeft = [...spec?.required ?? []];
5335
5346
  this.phase = 3;
5336
5347
  this.elem = [];
@@ -5347,7 +5358,9 @@ var ToolBodyMachineXml = class ToolBodyMachineXml {
5347
5358
  return true;
5348
5359
  }
5349
5360
  this.elem.push(b);
5361
+ const canParam = this.keyCands.length > 0 || !this.declared;
5350
5362
  if (litEq(this.elem, X_PARAM)) {
5363
+ if (!canParam) return false;
5351
5364
  this.phase = 4;
5352
5365
  this.keyBuf = "";
5353
5366
  return true;
@@ -5359,7 +5372,7 @@ var ToolBodyMachineXml = class ToolBodyMachineXml {
5359
5372
  this.wsRun = 0;
5360
5373
  return true;
5361
5374
  }
5362
- const pP = litPrefix(this.elem, X_PARAM);
5375
+ const pP = litPrefix(this.elem, X_PARAM) && canParam;
5363
5376
  const pF = litPrefix(this.elem, X_FCLOSE);
5364
5377
  if (pF && !pP && this.reqLeft.length) return false;
5365
5378
  return pP || pF;
@@ -5375,7 +5388,7 @@ var ToolBodyMachineXml = class ToolBodyMachineXml {
5375
5388
  this.valStarted = false;
5376
5389
  const sch = this.props.get(this.name)?.schemas?.get(this.keyBuf);
5377
5390
  const t = sch?.type;
5378
- if (sch && t !== void 0 && t !== "string") {
5391
+ if (sch && (sch.oneOf !== void 0 || t !== void 0 && t !== "string")) {
5379
5392
  this.valMode = "json";
5380
5393
  this.valJson = new JsonMachine(sch);
5381
5394
  } else if (sch?.enum?.length) {
@@ -5401,7 +5414,12 @@ var ToolBodyMachineXml = class ToolBodyMachineXml {
5401
5414
  }
5402
5415
  if (this.valMode === "json") {
5403
5416
  const j = this.valJson;
5404
- if (j.feed(Uint8Array.of(b))) return true;
5417
+ if (j.feed(Uint8Array.of(b))) {
5418
+ if (j.complete && (b === 10 || b === 32 || b === 9 || b === 13)) {
5419
+ if (++this.valWs > 1) return false;
5420
+ } else this.valWs = 0;
5421
+ return true;
5422
+ }
5405
5423
  if (!j.complete || b !== X_PCLOSE[0]) return false;
5406
5424
  this.phase = 6;
5407
5425
  this.closeLit = 1;
@@ -5458,7 +5476,14 @@ function makeToolFilter(table, prep, startInThink = false) {
5458
5476
  schemas
5459
5477
  }];
5460
5478
  }));
5479
+ if (prep.format === "xml") for (const t of prep.tools) for (const [k, s] of Object.entries(t.function.parameters?.properties ?? {})) {
5480
+ const sch = s;
5481
+ if (sch.oneOf === void 0 && (sch.type === void 0 || sch.type === "string") && !sch.enum?.length && (sch.minLength !== void 0 || sch.maxLength !== void 0)) throw new Error(`bitgpu/chat: minLength/maxLength on string parameter '${t.function.name}.${k}' cannot be enforced with the XML tool protocol (values are raw text); use an enum or drop the constraint`);
5482
+ if (sch.enum?.some((v) => v === "")) throw new Error(`bitgpu/chat: enum on parameter '${t.function.name}.${k}' contains an empty string, which the XML tool protocol cannot represent (the value scaffold newline would be ambiguous)`);
5483
+ }
5461
5484
  const newBody = () => prep.format === "xml" ? new ToolBodyMachineXml(cands, props) : new ToolBodyMachine(cands, schemas);
5485
+ const CLOSE_TEXT = ToolBodyMachine.bytesOf("</tool_call>");
5486
+ let bodyTail = "";
5462
5487
  let state = forced ? 1 : 0;
5463
5488
  let body = null;
5464
5489
  let inThink = startInThink;
@@ -5483,6 +5508,7 @@ function makeToolFilter(table, prep, startInThink = false) {
5483
5508
  }
5484
5509
  const bytes = table.bytes(n);
5485
5510
  if (!bytes || bytes.length === 0) continue;
5511
+ if ((bodyTail + String.fromCharCode(...bytes)).includes(CLOSE_TEXT)) continue;
5486
5512
  const fb = m.forcedNext?.() ?? -1;
5487
5513
  if (fb >= 0) {
5488
5514
  if (bytes.length === 1 && bytes[0] === fb) out.push(n);
@@ -5508,6 +5534,7 @@ function makeToolFilter(table, prep, startInThink = false) {
5508
5534
  if (id === ids.open) {
5509
5535
  state = 2;
5510
5536
  body = newBody();
5537
+ bodyTail = "";
5511
5538
  }
5512
5539
  return;
5513
5540
  case 2:
@@ -5518,7 +5545,10 @@ function makeToolFilter(table, prep, startInThink = false) {
5518
5545
  }
5519
5546
  {
5520
5547
  const bytes = table.bytes(id);
5521
- if (bytes) body.feed(bytes);
5548
+ if (bytes) {
5549
+ body.feed(bytes);
5550
+ bodyTail = (bodyTail + String.fromCharCode(...bytes)).slice(1 - CLOSE_TEXT.length);
5551
+ }
5522
5552
  }
5523
5553
  return;
5524
5554
  case 3: return;
@@ -5646,7 +5676,7 @@ function parseToolCallXml(raw, tools) {
5646
5676
  const key = m[1];
5647
5677
  const valText = m[2].replace(/^\n/, "").replace(/\n$/, "");
5648
5678
  const pschema = props[key];
5649
- if (pschema?.type !== void 0 && pschema.type !== "string") try {
5679
+ if (pschema?.oneOf !== void 0 || pschema?.type !== void 0 && pschema.type !== "string") try {
5650
5680
  args[key] = JSON.parse(valText);
5651
5681
  } catch {
5652
5682
  return {