omnius 1.0.149 → 1.0.151

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/dist/index.js CHANGED
@@ -11820,6 +11820,99 @@ async function _ensureHelia() {
11820
11820
  var _natsConn = null;
11821
11821
  var _natsCodec = null;
11822
11822
  var _tokensByRequest = {};
11823
+ var _sponsorLimits = null;
11824
+ var _sponsorActiveRequests = 0;
11825
+ var _sponsorRequestWindow = [];
11826
+ var _sponsorDailyTokensUsed = 0;
11827
+ var _sponsorDailyResetAt = Date.now() + 86400000;
11828
+ var _sponsorBlockedRequests = 0;
11829
+ var _sponsorTokenRateSamples = [];
11830
+
11831
+ function _sponsorPrune(now) {
11832
+ while (_sponsorRequestWindow.length > 0 && _sponsorRequestWindow[0] < now - 60000) _sponsorRequestWindow.shift();
11833
+ while (_sponsorTokenRateSamples.length > 0 && _sponsorTokenRateSamples[0].at < now - 10000) _sponsorTokenRateSamples.shift();
11834
+ if (_sponsorDailyResetAt <= now) {
11835
+ _sponsorDailyTokensUsed = 0;
11836
+ _sponsorDailyResetAt = now + 86400000;
11837
+ }
11838
+ }
11839
+
11840
+ function _sponsorTokenRate(now) {
11841
+ _sponsorPrune(now || Date.now());
11842
+ if (_sponsorTokenRateSamples.length === 0) return 0;
11843
+ var tokens = 0;
11844
+ for (var i = 0; i < _sponsorTokenRateSamples.length; i++) tokens += _sponsorTokenRateSamples[i].tokens || 0;
11845
+ var spanMs = Math.max(1000, (now || Date.now()) - _sponsorTokenRateSamples[0].at);
11846
+ return Math.round((tokens / (spanMs / 1000)) * 10) / 10;
11847
+ }
11848
+
11849
+ function _sponsorRecordTokenRate(tokens) {
11850
+ var n = Number(tokens) || 0;
11851
+ if (n <= 0) return _sponsorTokenRate(Date.now());
11852
+ var now = Date.now();
11853
+ _sponsorTokenRateSamples.push({ at: now, tokens: Math.floor(n) });
11854
+ return _sponsorTokenRate(now);
11855
+ }
11856
+
11857
+ function _sponsorEstimateTokens(text) {
11858
+ var len = String(text || '').length;
11859
+ return len > 0 ? Math.max(1, Math.ceil(len / 4)) : 0;
11860
+ }
11861
+
11862
+ function _sponsorAdmit(model) {
11863
+ if (!_sponsorLimits) return { ok: true };
11864
+ var now = Date.now();
11865
+ _sponsorPrune(now);
11866
+ if (_sponsorLimits.maxConcurrent <= 0 || _sponsorLimits.maxRequestsPerMinute <= 0 || _sponsorLimits.maxTokensPerDay <= 0) {
11867
+ _sponsorBlockedRequests++;
11868
+ return { ok: false, reason: 'Sponsored endpoint is paused or has no quota configured.' };
11869
+ }
11870
+ if (_sponsorLimits.allowedModels !== 'all' && _sponsorLimits.allowedModels.indexOf(model) === -1) {
11871
+ _sponsorBlockedRequests++;
11872
+ return { ok: false, reason: 'Model not allowed: ' + model };
11873
+ }
11874
+ if (_sponsorActiveRequests >= _sponsorLimits.maxConcurrent) {
11875
+ _sponsorBlockedRequests++;
11876
+ return { ok: false, reason: 'Too many concurrent requests (' + _sponsorActiveRequests + '/' + _sponsorLimits.maxConcurrent + '). Try again shortly.' };
11877
+ }
11878
+ if (_sponsorRequestWindow.length >= _sponsorLimits.maxRequestsPerMinute) {
11879
+ _sponsorBlockedRequests++;
11880
+ return { ok: false, reason: 'Rate limited (' + _sponsorLimits.maxRequestsPerMinute + ' req/min).' };
11881
+ }
11882
+ if (_sponsorDailyTokensUsed >= _sponsorLimits.maxTokensPerDay) {
11883
+ _sponsorBlockedRequests++;
11884
+ return { ok: false, reason: 'Daily token budget exhausted.' };
11885
+ }
11886
+ _sponsorActiveRequests++;
11887
+ _sponsorRequestWindow.push(now);
11888
+ return { ok: true };
11889
+ }
11890
+
11891
+ function _sponsorRelease() {
11892
+ if (_sponsorActiveRequests > 0) _sponsorActiveRequests--;
11893
+ }
11894
+
11895
+ function _sponsorRecordUsage(inputTokens, outputTokens, includeRate) {
11896
+ var input = Math.max(0, Math.floor(Number(inputTokens) || 0));
11897
+ var output = Math.max(0, Math.floor(Number(outputTokens) || 0));
11898
+ _sponsorPrune(Date.now());
11899
+ _sponsorDailyTokensUsed += input + output;
11900
+ if (includeRate !== false) _sponsorRecordTokenRate(output);
11901
+ }
11902
+
11903
+ function _sponsorGatewaySnapshot() {
11904
+ var now = Date.now();
11905
+ _sponsorPrune(now);
11906
+ return {
11907
+ activeConnections: _sponsorActiveRequests,
11908
+ requestsInWindow: _sponsorRequestWindow.length,
11909
+ dailyTokensUsed: _sponsorDailyTokensUsed,
11910
+ dailyResetAt: _sponsorDailyResetAt,
11911
+ blockedRequests: _sponsorBlockedRequests,
11912
+ tokensPerSecond: _sponsorTokenRate(now),
11913
+ limits: _sponsorLimits,
11914
+ };
11915
+ }
11823
11916
 
11824
11917
  // ── Nexus voice subsystem (PCM + ASR) ─────────────────────────────────
11825
11918
  // Subscribes to nexus.rooms.audio, filters by the rooms this daemon has
@@ -12032,10 +12125,22 @@ async function _collectSysMetrics() {
12032
12125
  var totalMem = os.totalmem();
12033
12126
  var freeMem = os.freemem();
12034
12127
  var usedMem = totalMem - freeMem;
12035
- var cpuModel = '';
12036
- try { var cpuArr = os.cpus(); if (cpuArr.length > 0) cpuModel = cpuArr[0].model || ''; } catch {}
12037
- var gpuInfo = { available: false, name: '', utilization: 0, vramUsedMB: 0, vramTotalMB: 0, vramUtilization: 0 };
12038
- try {
12128
+ var cpuModel = '';
12129
+ try { var cpuArr = os.cpus(); if (cpuArr.length > 0) cpuModel = cpuArr[0].model || ''; } catch {}
12130
+ var gpuInfo = { available: false, name: '', utilization: 0, vramUsedMB: 0, vramTotalMB: 0, vramUtilization: 0 };
12131
+ var diskInfo = { path: process.cwd(), totalGB: 0, freeGB: 0, usedGB: 0, utilization: -1 };
12132
+ try {
12133
+ var fs = await import('node:fs');
12134
+ var st = fs.statfsSync(process.cwd());
12135
+ var totalBytes = Number(st.blocks) * Number(st.bsize);
12136
+ var freeBytes = Number(st.bavail) * Number(st.bsize);
12137
+ var usedBytes = Math.max(0, totalBytes - freeBytes);
12138
+ diskInfo.totalGB = Math.round((totalBytes / (1024*1024*1024)) * 10) / 10;
12139
+ diskInfo.freeGB = Math.round((freeBytes / (1024*1024*1024)) * 10) / 10;
12140
+ diskInfo.usedGB = Math.round((usedBytes / (1024*1024*1024)) * 10) / 10;
12141
+ diskInfo.utilization = totalBytes > 0 ? Math.round((usedBytes / totalBytes) * 100) : -1;
12142
+ } catch {}
12143
+ try {
12039
12144
  var cp = await import('node:child_process');
12040
12145
  var smiOut = cp.execSync('nvidia-smi --query-gpu=utilization.gpu,memory.used,memory.total,name --format=csv,noheader,nounits 2>/dev/null', { encoding: 'utf8', timeout: 3000 });
12041
12146
  var smiLine = smiOut.trim().split('\\n')[0];
@@ -12050,9 +12155,10 @@ async function _collectSysMetrics() {
12050
12155
  }
12051
12156
  } catch {}
12052
12157
  _sysMetricsCache = {
12053
- cpu: { utilization: Math.min(100, Math.round((loads[0] / cores) * 100)), cores: cores, model: cpuModel },
12054
- memory: { utilization: Math.round((usedMem / totalMem) * 100), totalGB: Math.round((totalMem / (1024*1024*1024)) * 10) / 10, usedGB: Math.round((usedMem / (1024*1024*1024)) * 10) / 10 },
12055
- gpu: gpuInfo,
12158
+ cpu: { utilization: Math.min(100, Math.round((loads[0] / cores) * 100)), cores: cores, model: cpuModel },
12159
+ memory: { utilization: Math.round((usedMem / totalMem) * 100), totalGB: Math.round((totalMem / (1024*1024*1024)) * 10) / 10, usedGB: Math.round((usedMem / (1024*1024*1024)) * 10) / 10 },
12160
+ disk: diskInfo,
12161
+ gpu: gpuInfo,
12056
12162
  timestamp: new Date().toISOString(),
12057
12163
  };
12058
12164
  _sysMetricsCacheTs = now;
@@ -12235,7 +12341,9 @@ async function handleCmd(cmd) {
12235
12341
  });
12236
12342
  }
12237
12343
  } catch {}
12238
- var sponsorData = {
12344
+ var sponsorLimitsArg = {};
12345
+ try { sponsorLimitsArg = args.limits ? JSON.parse(args.limits) : {}; } catch {}
12346
+ var sponsorData = {
12239
12347
  type: 'sponsor.announce',
12240
12348
  peerId: (connected ? nexus.peerId : 'unknown') || 'unknown',
12241
12349
  libp2pPeerId: (connected ? nexus.peerId : '') || '',
@@ -12244,10 +12352,11 @@ async function handleCmd(cmd) {
12244
12352
  modelDetails: _saModelDetails, // NX-07: per-model capacity
12245
12353
  tunnelUrl: args.tunnel_url || null,
12246
12354
  authKey: args.auth_key || '',
12247
- limits: {
12248
- maxRequestsPerMinute: parseInt(args.rpm || '60', 10),
12249
- maxTokensPerDay: parseInt(args.tpd || '100000', 10),
12250
- },
12355
+ limits: {
12356
+ maxRequestsPerMinute: parseInt(args.rpm || sponsorLimitsArg.maxRequestsPerMinute || '60', 10),
12357
+ maxTokensPerDay: parseInt(args.tpd || sponsorLimitsArg.maxTokensPerDay || '100000', 10),
12358
+ maxConcurrent: parseInt(args.max_concurrent || sponsorLimitsArg.maxConcurrent || '1', 10),
12359
+ },
12251
12360
  banner: args.banner || null,
12252
12361
  message: args.message || '',
12253
12362
  linkUrl: args.link_url || '',
@@ -12262,13 +12371,14 @@ async function handleCmd(cmd) {
12262
12371
 
12263
12372
  // Persist to KV-backed sponsor directory (omnius.nexus worker)
12264
12373
  try {
12265
- var kvResp = await fetch('https://omnius.nexus/api/v1/sponsors', {
12266
- method: 'POST',
12267
- headers: { 'Content-Type': 'application/json' },
12268
- body: JSON.stringify(sponsorData),
12269
- });
12270
- var kvResult = await kvResp.json();
12271
- dlog('sponsor_announce: KV persist ' + (kvResult.persisted ? 'OK' : 'skipped: ' + kvResult.reason));
12374
+ var kvResp = await fetch('https://omnius.nexus/api/v1/sponsors', {
12375
+ method: 'POST',
12376
+ headers: { 'Content-Type': 'application/json' },
12377
+ body: JSON.stringify(sponsorData),
12378
+ });
12379
+ var kvText = await kvResp.text();
12380
+ var kvResult = kvText.trim() ? JSON.parse(kvText) : { persisted: false, reason: 'empty directory response' };
12381
+ dlog('sponsor_announce: KV persist ' + (kvResult.persisted ? 'OK' : 'skipped: ' + kvResult.reason));
12272
12382
  } catch (kvErr) {
12273
12383
  dlog('sponsor_announce: KV persist failed: ' + (kvErr.message || kvErr));
12274
12384
  }
@@ -12388,11 +12498,12 @@ async function handleCmd(cmd) {
12388
12498
 
12389
12499
  // ── Source 1: KV-backed persistent directory (MOST RELIABLE) ──
12390
12500
  // Query the omnius.nexus worker for persisted sponsor listings
12391
- try {
12392
- var kvResp = await fetch('https://omnius.nexus/api/v1/sponsors', { signal: AbortSignal.timeout(5000) });
12393
- if (kvResp.ok) {
12394
- var kvData = await kvResp.json();
12395
- var kvSponsors = kvData.sponsors || [];
12501
+ try {
12502
+ var kvResp = await fetch('https://omnius.nexus/api/v1/sponsors', { signal: AbortSignal.timeout(5000) });
12503
+ if (kvResp.ok) {
12504
+ var kvText = await kvResp.text();
12505
+ var kvData = kvText.trim() ? JSON.parse(kvText) : { sponsors: [] };
12506
+ var kvSponsors = kvData.sponsors || [];
12396
12507
  for (var ki = 0; ki < kvSponsors.length; ki++) {
12397
12508
  var kvSp = kvSponsors[ki];
12398
12509
  if (kvSp.status === 'active') {
@@ -13433,6 +13544,28 @@ async function handleCmd(cmd) {
13433
13544
  dlog('expose: auth key configured (' + exposeAuthKey.length + ' chars)');
13434
13545
  }
13435
13546
 
13547
+ var sponsorMaxConcurrent = parseInt(args.max_concurrent || '0', 10);
13548
+ var sponsorMaxRequestsPerMinute = parseInt(args.max_requests_per_minute || '0', 10);
13549
+ var sponsorMaxTokensPerDay = parseInt(args.max_tokens_per_day || '0', 10);
13550
+ var sponsorAllowedModelsArg = args.allowed_models || '';
13551
+ if (sponsorMaxConcurrent > 0 || sponsorMaxRequestsPerMinute > 0 || sponsorMaxTokensPerDay > 0) {
13552
+ var sponsorDailyUsed = parseInt(args.daily_tokens_used || '0', 10);
13553
+ var sponsorDailyResetAt = parseInt(args.daily_tokens_reset_at || '0', 10);
13554
+ _sponsorDailyTokensUsed = Number.isFinite(sponsorDailyUsed) && sponsorDailyUsed > 0 ? sponsorDailyUsed : 0;
13555
+ _sponsorDailyResetAt = Number.isFinite(sponsorDailyResetAt) && sponsorDailyResetAt > Date.now() ? sponsorDailyResetAt : Date.now() + 86400000;
13556
+ _sponsorLimits = {
13557
+ maxConcurrent: sponsorMaxConcurrent > 0 ? sponsorMaxConcurrent : 1,
13558
+ maxRequestsPerMinute: sponsorMaxRequestsPerMinute > 0 ? sponsorMaxRequestsPerMinute : 1,
13559
+ maxTokensPerDay: sponsorMaxTokensPerDay > 0 ? sponsorMaxTokensPerDay : 1,
13560
+ allowedModels: sponsorAllowedModelsArg && sponsorAllowedModelsArg !== 'all'
13561
+ ? sponsorAllowedModelsArg.split(',').filter(Boolean)
13562
+ : 'all',
13563
+ };
13564
+ dlog('expose: sponsor limits active concurrent=' + _sponsorLimits.maxConcurrent + ' rpm=' + _sponsorLimits.maxRequestsPerMinute + ' tpd=' + _sponsorLimits.maxTokensPerDay);
13565
+ } else {
13566
+ _sponsorLimits = null;
13567
+ }
13568
+
13436
13569
  // Passthrough mode: forward from a remote /endpoint (Chutes, Groq, etc.)
13437
13570
  var isPassthrough = args.passthrough === 'true';
13438
13571
  var endpointAuth = args.endpoint_auth || '';
@@ -13500,6 +13633,11 @@ async function handleCmd(cmd) {
13500
13633
  }
13501
13634
  }
13502
13635
 
13636
+ if (_sponsorLimits && _sponsorLimits.allowedModels !== 'all') {
13637
+ models = models.filter(function(m) { return _sponsorLimits.allowedModels.indexOf(m.name) !== -1; });
13638
+ dlog('expose: sponsor allowlist filtered models to ' + models.length);
13639
+ }
13640
+
13503
13641
  if (models.length === 0) {
13504
13642
  writeResp(id, { ok: false, output: isPassthrough ? 'No models found on upstream endpoint.' : 'No models found on Ollama. Pull a model first.' });
13505
13643
  return;
@@ -13646,12 +13784,46 @@ async function handleCmd(cmd) {
13646
13784
  dlog('expose: auth OK for ' + capName);
13647
13785
  }
13648
13786
 
13649
- // Forward to Ollama — supports both flat prompt and structured messages
13650
- try {
13651
- var parsedReq = null;
13652
- try { parsedReq = JSON.parse(prompt); } catch (pe) { dlog('expose: JSON parse error: ' + (pe.message || pe)); }
13653
-
13654
- var genResp, genData, output, inputTokens, outputTokens, responsePayload;
13787
+ // Forward to Ollama — supports both flat prompt and structured messages
13788
+ var sponsorAdmissionOpen = false;
13789
+ try {
13790
+ var parsedReq = null;
13791
+ try { parsedReq = JSON.parse(prompt); } catch (pe) { dlog('expose: JSON parse error: ' + (pe.message || pe)); }
13792
+
13793
+ var sponsorAdmission = _sponsorAdmit(model.name);
13794
+ if (!sponsorAdmission.ok) {
13795
+ dlog('expose: sponsor limit rejected ' + capName + ' from ' + (request.from || 'unknown') + ': ' + sponsorAdmission.reason);
13796
+ await swrite({
13797
+ type: 'invoke.event', version: 1,
13798
+ requestId: request.requestId, seq: 0,
13799
+ event: 'error', data: sponsorAdmission.reason,
13800
+ });
13801
+ await swrite({
13802
+ type: 'invoke.done', version: 1,
13803
+ requestId: request.requestId,
13804
+ usage: { inputBytes: 0, outputBytes: 0 },
13805
+ });
13806
+ try {
13807
+ appendFileSync(meteringFile, JSON.stringify({
13808
+ timestamp: Date.now(),
13809
+ peerId: request.from || 'unknown',
13810
+ service: capName,
13811
+ capability: capName,
13812
+ model: model.name,
13813
+ direction: 'inbound',
13814
+ blocked: true,
13815
+ reason: sponsorAdmission.reason,
13816
+ inputTokens: 0,
13817
+ outputTokens: 0,
13818
+ tokens: 0,
13819
+ }) + '\\n');
13820
+ } catch {}
13821
+ try { stream.close(); } catch {}
13822
+ return;
13823
+ }
13824
+ sponsorAdmissionOpen = true;
13825
+
13826
+ var genResp, genData, output, inputTokens, outputTokens, responsePayload;
13655
13827
 
13656
13828
  // Detect if requester wants streaming (outputMode from invoke.open)
13657
13829
  var wantsStream = request.outputMode === 'stream';
@@ -13708,9 +13880,10 @@ async function handleCmd(cmd) {
13708
13880
  var sseObj = JSON.parse(sseData);
13709
13881
  var sseDelta = (sseObj.choices && sseObj.choices[0] && sseObj.choices[0].delta) || {};
13710
13882
  var sseToken = sseDelta.content || '';
13711
- if (sseToken) {
13712
- sseContent += sseToken;
13713
- await swrite({
13883
+ if (sseToken) {
13884
+ sseContent += sseToken;
13885
+ _sponsorRecordTokenRate(_sponsorEstimateTokens(sseToken));
13886
+ await swrite({
13714
13887
  type: 'invoke.event', version: 1,
13715
13888
  requestId: request.requestId, seq: sseSeq++,
13716
13889
  event: 'token', data: sseToken,
@@ -13839,12 +14012,14 @@ async function handleCmd(cmd) {
13839
14012
 
13840
14013
  // Attach system metrics to response — clients get CPU/GPU/RAM
13841
14014
  // for free without a separate invoke_capability round-trip
13842
- try {
13843
- var _sm = await _collectSysMetrics();
13844
- if (_sm) responsePayload.system = _sm;
13845
- } catch {}
14015
+ try {
14016
+ var _sm = await _collectSysMetrics();
14017
+ if (_sm) responsePayload.system = Object.assign({}, _sm, { gateway: _sponsorGatewaySnapshot() });
14018
+ } catch {}
13846
14019
 
13847
- // Stream result back
14020
+ _sponsorRecordUsage(inputTokens, outputTokens, !wantsStream);
14021
+
14022
+ // Stream result back
13848
14023
  await swrite({
13849
14024
  type: 'invoke.event', version: 1,
13850
14025
  requestId: request.requestId, seq: 0,
@@ -13882,10 +14057,12 @@ async function handleCmd(cmd) {
13882
14057
  await swrite({
13883
14058
  type: 'invoke.done', version: 1,
13884
14059
  requestId: request.requestId,
13885
- usage: { inputBytes: 0, outputBytes: 0 },
13886
- });
13887
- }
13888
- try { stream.close(); } catch {}
14060
+ usage: { inputBytes: 0, outputBytes: 0 },
14061
+ });
14062
+ } finally {
14063
+ if (sponsorAdmissionOpen) _sponsorRelease();
14064
+ }
14065
+ try { stream.close(); } catch {}
13889
14066
  }, capOpts);
13890
14067
  }
13891
14068
  }
@@ -13948,10 +14125,22 @@ async function handleCmd(cmd) {
13948
14125
  var os = await import('node:os');
13949
14126
  var loads = os.loadavg();
13950
14127
  var cores = os.cpus().length;
13951
- var totalMem = os.totalmem();
13952
- var freeMem = os.freemem();
13953
- var usedMem = totalMem - freeMem;
13954
- var gpuInfo = { available: false, name: '', utilization: 0, vramUsedMB: 0, vramTotalMB: 0, vramUtilization: 0 };
14128
+ var totalMem = os.totalmem();
14129
+ var freeMem = os.freemem();
14130
+ var usedMem = totalMem - freeMem;
14131
+ var gpuInfo = { available: false, name: '', utilization: 0, vramUsedMB: 0, vramTotalMB: 0, vramUtilization: 0 };
14132
+ var diskInfo = { path: process.cwd(), totalGB: 0, freeGB: 0, usedGB: 0, utilization: -1 };
14133
+ try {
14134
+ var fs = await import('node:fs');
14135
+ var st = fs.statfsSync(process.cwd());
14136
+ var totalBytes = Number(st.blocks) * Number(st.bsize);
14137
+ var freeBytes = Number(st.bavail) * Number(st.bsize);
14138
+ var usedBytes = Math.max(0, totalBytes - freeBytes);
14139
+ diskInfo.totalGB = Math.round((totalBytes / (1024*1024*1024)) * 10) / 10;
14140
+ diskInfo.freeGB = Math.round((freeBytes / (1024*1024*1024)) * 10) / 10;
14141
+ diskInfo.usedGB = Math.round((usedBytes / (1024*1024*1024)) * 10) / 10;
14142
+ diskInfo.utilization = totalBytes > 0 ? Math.round((usedBytes / totalBytes) * 100) : -1;
14143
+ } catch {}
13955
14144
  try {
13956
14145
  var cp = await import('node:child_process');
13957
14146
  var smiOut = cp.execSync('nvidia-smi --query-gpu=utilization.gpu,memory.used,memory.total,name --format=csv,noheader,nounits 2>/dev/null', { encoding: 'utf8', timeout: 3000 });
@@ -13968,12 +14157,14 @@ async function handleCmd(cmd) {
13968
14157
  } catch (ge) { /* no GPU */ }
13969
14158
  var cpuModel = '';
13970
14159
  try { var cpuInfoArr = os.cpus(); if (cpuInfoArr.length > 0) cpuModel = cpuInfoArr[0].model || ''; } catch {}
13971
- var metricsPayload = {
13972
- cpu: { utilization: Math.min(100, Math.round((loads[0] / cores) * 100)), cores: cores, model: cpuModel },
13973
- memory: { utilization: Math.round((usedMem / totalMem) * 100), totalGB: Math.round((totalMem / (1024*1024*1024)) * 10) / 10, usedGB: Math.round((usedMem / (1024*1024*1024)) * 10) / 10 },
13974
- gpu: gpuInfo,
13975
- timestamp: new Date().toISOString(),
13976
- };
14160
+ var metricsPayload = {
14161
+ cpu: { utilization: Math.min(100, Math.round((loads[0] / cores) * 100)), cores: cores, model: cpuModel },
14162
+ memory: { utilization: Math.round((usedMem / totalMem) * 100), totalGB: Math.round((totalMem / (1024*1024*1024)) * 10) / 10, usedGB: Math.round((usedMem / (1024*1024*1024)) * 10) / 10 },
14163
+ disk: diskInfo,
14164
+ gpu: gpuInfo,
14165
+ gateway: _sponsorGatewaySnapshot(),
14166
+ timestamp: new Date().toISOString(),
14167
+ };
13977
14168
  await smWrite({ type: 'invoke.event', version: 1, requestId: request.requestId, seq: 0, event: 'result', data: JSON.stringify(metricsPayload) });
13978
14169
  await smWrite({ type: 'invoke.done', version: 1, requestId: request.requestId, usage: { inputBytes: 0, outputBytes: JSON.stringify(metricsPayload).length } });
13979
14170
  } catch (me) {
@@ -18106,7 +18297,8 @@ ${earlyError ? "\n" + earlyError : ""}${earlyOutput ? "\n" + earlyOutput : ""}`;
18106
18297
  model,
18107
18298
  target_peer: args.target_peer,
18108
18299
  temperature: args.temperature,
18109
- max_tokens: args.max_tokens
18300
+ max_tokens: args.max_tokens,
18301
+ auth_key: args.auth_key
18110
18302
  };
18111
18303
  if (args.messages) {
18112
18304
  daemonArgs.messages = args.messages;
@@ -571791,6 +571983,95 @@ function safeNonNegativeInt(value2) {
571791
571983
  const n2 = Number(value2);
571792
571984
  return Number.isFinite(n2) && n2 > 0 ? Math.floor(n2) : 0;
571793
571985
  }
571986
+ function currentTokenRate(samples, now = Date.now()) {
571987
+ while (samples.length > 0 && samples[0].at < now - 1e4) samples.shift();
571988
+ if (samples.length === 0) return 0;
571989
+ const tokens = samples.reduce((sum, sample) => sum + sample.tokens, 0);
571990
+ const spanMs = Math.max(1e3, now - samples[0].at);
571991
+ return Math.round(tokens / (spanMs / 1e3) * 10) / 10;
571992
+ }
571993
+ function recordTokenRate(samples, tokens, now = Date.now()) {
571994
+ const count = safeNonNegativeInt(tokens);
571995
+ if (count > 0) samples.push({ at: now, tokens: count });
571996
+ return currentTokenRate(samples, now);
571997
+ }
571998
+ function estimateTextTokens(text) {
571999
+ const visible = Array.from(text).length;
572000
+ return visible > 0 ? Math.max(1, Math.ceil(visible / 4)) : 0;
572001
+ }
572002
+ function estimateStreamingOutputTokens(chunkText) {
572003
+ let contentChars = 0;
572004
+ for (const line of chunkText.split("\n")) {
572005
+ const trimmed = line.trim();
572006
+ if (!trimmed.startsWith("data:")) continue;
572007
+ const payload = trimmed.slice(5).trim();
572008
+ if (!payload || payload === "[DONE]") continue;
572009
+ try {
572010
+ const parsed = JSON.parse(payload);
572011
+ const choice = Array.isArray(parsed?.choices) ? parsed.choices[0] : null;
572012
+ const deltaContent = choice?.delta?.content;
572013
+ const messageContent = choice?.message?.content;
572014
+ const responseContent = parsed?.response;
572015
+ const text = typeof deltaContent === "string" ? deltaContent : typeof messageContent === "string" ? messageContent : typeof responseContent === "string" ? responseContent : "";
572016
+ contentChars += Array.from(text).length;
572017
+ } catch {
572018
+ contentChars += Array.from(payload).length;
572019
+ }
572020
+ }
572021
+ if (contentChars > 0) return Math.max(1, Math.ceil(contentChars / 4));
572022
+ return estimateTextTokens(chunkText.trim());
572023
+ }
572024
+ function serializeSponsorModels(stats) {
572025
+ const byModel = /* @__PURE__ */ new Map();
572026
+ for (const [model, requests] of stats.modelUsage.entries()) {
572027
+ if (INTERNAL_CAPABILITIES.has(model)) continue;
572028
+ byModel.set(model, {
572029
+ model,
572030
+ requests,
572031
+ tokensIn: 0,
572032
+ tokensOut: 0,
572033
+ lastUsed: 0
572034
+ });
572035
+ }
572036
+ for (const user of stats.users.values()) {
572037
+ for (const [model, meter] of user.models.entries()) {
572038
+ if (INTERNAL_CAPABILITIES.has(model)) continue;
572039
+ const existing = byModel.get(model) ?? {
572040
+ model,
572041
+ requests: 0,
572042
+ tokensIn: 0,
572043
+ tokensOut: 0,
572044
+ lastUsed: 0
572045
+ };
572046
+ if (!stats.modelUsage.has(model)) existing.requests += meter.requests;
572047
+ existing.tokensIn += meter.tokensIn;
572048
+ existing.tokensOut += meter.tokensOut;
572049
+ existing.lastUsed = Math.max(existing.lastUsed, meter.lastUsed);
572050
+ byModel.set(model, existing);
572051
+ }
572052
+ }
572053
+ return Array.from(byModel.values()).sort(
572054
+ (a2, b) => (b.tokensIn + b.tokensOut || b.requests) - (a2.tokensIn + a2.tokensOut || a2.requests)
572055
+ );
572056
+ }
572057
+ function serializeSponsorPeers(stats) {
572058
+ return Array.from(stats.users.values()).map((user) => ({
572059
+ peer: user.ip,
572060
+ firstSeen: user.firstSeen,
572061
+ lastSeen: user.lastSeen,
572062
+ requests: user.requests,
572063
+ activeRequests: user.activeRequests,
572064
+ tokensIn: user.tokensIn,
572065
+ tokensOut: user.tokensOut,
572066
+ models: Array.from(user.models.entries()).filter(([model]) => !INTERNAL_CAPABILITIES.has(model)).map(([model, meter]) => ({
572067
+ model,
572068
+ requests: meter.requests,
572069
+ tokensIn: meter.tokensIn,
572070
+ tokensOut: meter.tokensOut,
572071
+ lastUsed: meter.lastUsed
572072
+ })).sort((a2, b) => b.tokensIn + b.tokensOut - (a2.tokensIn + a2.tokensOut))
572073
+ })).sort((a2, b) => (b.tokensIn + b.tokensOut || b.requests) - (a2.tokensIn + a2.tokensOut || a2.requests));
572074
+ }
571794
572075
  function nextSponsorDailyReset(now = Date.now()) {
571795
572076
  return now + SPONSOR_DAILY_WINDOW_MS;
571796
572077
  }
@@ -572054,6 +572335,7 @@ var init_expose = __esm({
572054
572335
  /** Sponsor rate limits (set via setSponsorLimits) */
572055
572336
  _sponsorLimits = null;
572056
572337
  _sponsorBlockedRequests = 0;
572338
+ _tokenRateSamples = [];
572057
572339
  _authKey;
572058
572340
  _targetUrl;
572059
572341
  _kind;
@@ -572067,6 +572349,7 @@ var init_expose = __esm({
572067
572349
  errors: 0,
572068
572350
  totalTokensIn: 0,
572069
572351
  totalTokensOut: 0,
572352
+ tokensPerSecond: 0,
572070
572353
  startedAt: Date.now(),
572071
572354
  modelUsage: /* @__PURE__ */ new Map(),
572072
572355
  users: /* @__PURE__ */ new Map(),
@@ -572157,6 +572440,7 @@ var init_expose = __esm({
572157
572440
  };
572158
572441
  }
572159
572442
  refreshSponsorUsageStats(now = Date.now()) {
572443
+ this._stats.tokensPerSecond = currentTokenRate(this._tokenRateSamples, now);
572160
572444
  if (!this._sponsorLimits) {
572161
572445
  this._stats.sponsorUsage = null;
572162
572446
  return;
@@ -572166,6 +572450,10 @@ var init_expose = __esm({
572166
572450
  this._stats.sponsorUsage = {
572167
572451
  enabled: true,
572168
572452
  transport: "tunnel",
572453
+ totalRequests: this._stats.totalRequests,
572454
+ totalTokensIn: this._stats.totalTokensIn,
572455
+ totalTokensOut: this._stats.totalTokensOut,
572456
+ tokensPerSecond: this._stats.tokensPerSecond,
572169
572457
  dailyTokensUsed: this._dailyTokensUsed,
572170
572458
  dailyTokensLimit: this._sponsorLimits.maxTokensPerDay,
572171
572459
  dailyResetAt: this._dailyTokensResetAt,
@@ -572175,9 +572463,15 @@ var init_expose = __esm({
572175
572463
  activeConnections: this._stats.activeConnections,
572176
572464
  maxConcurrent: this._sponsorLimits.maxConcurrent,
572177
572465
  blockedRequests: this._sponsorBlockedRequests,
572178
- allowedModels: this._sponsorLimits.allowedModels === "all" ? "all" : [...this._sponsorLimits.allowedModels]
572466
+ allowedModels: this._sponsorLimits.allowedModels === "all" ? "all" : [...this._sponsorLimits.allowedModels],
572467
+ peers: serializeSponsorPeers(this._stats),
572468
+ models: serializeSponsorModels(this._stats)
572179
572469
  };
572180
572470
  }
572471
+ recordServedTokens(tokens, now = Date.now()) {
572472
+ this._stats.tokensPerSecond = recordTokenRate(this._tokenRateSamples, tokens, now);
572473
+ this.refreshSponsorUsageStats(now);
572474
+ }
572181
572475
  markSponsorBlocked() {
572182
572476
  this._sponsorBlockedRequests++;
572183
572477
  this.refreshSponsorUsageStats();
@@ -572391,8 +572685,11 @@ var init_expose = __esm({
572391
572685
  errors: this._stats.errors,
572392
572686
  totalTokensIn: this._stats.totalTokensIn,
572393
572687
  totalTokensOut: this._stats.totalTokensOut,
572688
+ tokensPerSecond: currentTokenRate(this._tokenRateSamples),
572394
572689
  uptimeSeconds: Math.floor((Date.now() - this._stats.startedAt) / 1e3),
572395
572690
  modelUsage: Object.fromEntries(this._stats.modelUsage),
572691
+ models: serializeSponsorModels(this._stats),
572692
+ peers: serializeSponsorPeers(this._stats),
572396
572693
  users: Array.from(this._stats.users.values()).map((u) => ({
572397
572694
  ip: u.ip,
572398
572695
  requests: u.requests,
@@ -572415,6 +572712,7 @@ var init_expose = __esm({
572415
572712
  return;
572416
572713
  }
572417
572714
  const userIp = req2.headers["cf-connecting-ip"] ?? req2.headers["x-forwarded-for"]?.split(",")[0]?.trim() ?? req2.socket.remoteAddress ?? "unknown";
572715
+ const requestStartedAt = Date.now();
572418
572716
  let user = this._stats.users.get(userIp);
572419
572717
  if (!user) {
572420
572718
  user = {
@@ -572564,6 +572862,9 @@ var init_expose = __esm({
572564
572862
  }
572565
572863
  };
572566
572864
  let responseTail = "";
572865
+ let requestTokensIn = 0;
572866
+ let requestTokensOut = 0;
572867
+ let sawStreamTokenEstimate = false;
572567
572868
  const finalizeRequest = () => {
572568
572869
  user.activeRequests = Math.max(0, user.activeRequests - 1);
572569
572870
  this._stats.activeConnections = Math.max(0, this._stats.activeConnections - 1);
@@ -572573,9 +572874,12 @@ var init_expose = __esm({
572573
572874
  if (promptEval || evalCount) {
572574
572875
  const tIn2 = parseInt(promptEval?.[1] ?? "0", 10);
572575
572876
  const tOut2 = parseInt(evalCount?.[1] ?? "0", 10);
572877
+ requestTokensIn = tIn2;
572878
+ requestTokensOut = tOut2;
572576
572879
  this._stats.totalTokensIn += tIn2;
572577
572880
  this._stats.totalTokensOut += tOut2;
572578
572881
  this.trackTokenUsage(tIn2, tOut2);
572882
+ if (!isStreaming || !sawStreamTokenEstimate) this.recordServedTokens(tOut2);
572579
572883
  user.tokensIn += tIn2;
572580
572884
  user.tokensOut += tOut2;
572581
572885
  if (requestModel) {
@@ -572591,9 +572895,12 @@ var init_expose = __esm({
572591
572895
  if (promptTokens || completionTokens) {
572592
572896
  const tIn2 = parseInt(promptTokens?.[1] ?? "0", 10);
572593
572897
  const tOut2 = parseInt(completionTokens?.[1] ?? "0", 10);
572898
+ requestTokensIn = tIn2;
572899
+ requestTokensOut = tOut2;
572594
572900
  this._stats.totalTokensIn += tIn2;
572595
572901
  this._stats.totalTokensOut += tOut2;
572596
572902
  this.trackTokenUsage(tIn2, tOut2);
572903
+ if (!isStreaming || !sawStreamTokenEstimate) this.recordServedTokens(tOut2);
572597
572904
  user.tokensIn += tIn2;
572598
572905
  user.tokensOut += tOut2;
572599
572906
  if (requestModel) {
@@ -572607,8 +572914,8 @@ var init_expose = __esm({
572607
572914
  }
572608
572915
  } catch {
572609
572916
  }
572610
- const tIn = user.tokensIn;
572611
- const tOut = user.tokensOut;
572917
+ const tIn = requestTokensIn;
572918
+ const tOut = requestTokensOut;
572612
572919
  if (tIn > 0 || tOut > 0) {
572613
572920
  const receipt = {
572614
572921
  job_id: `job-${Date.now().toString(36)}`,
@@ -572616,7 +572923,7 @@ var init_expose = __esm({
572616
572923
  consumer_ip: userIp,
572617
572924
  model: requestModel,
572618
572925
  usage_final: { input_tokens: tIn, output_tokens: tOut },
572619
- latency_ms: Date.now() - (user.lastSeen || Date.now()),
572926
+ latency_ms: Date.now() - requestStartedAt,
572620
572927
  quality_flags: [],
572621
572928
  // COHERE revenue split: 70% provider, 10% commons, 8% memory, 7% relay, 5% reserve
572622
572929
  provider_reward: (tIn + tOut) * 1e-7 * 0.7,
@@ -572656,6 +572963,11 @@ var init_expose = __esm({
572656
572963
  }
572657
572964
  if (isStreaming) {
572658
572965
  this.emit("token_flash");
572966
+ const estimatedTokens = estimateStreamingOutputTokens(text);
572967
+ if (estimatedTokens > 0) {
572968
+ sawStreamTokenEstimate = true;
572969
+ this.recordServedTokens(estimatedTokens);
572970
+ }
572659
572971
  this.emit("stream_data", { content: text, model: requestModel, peer: userIp });
572660
572972
  }
572661
572973
  });
@@ -572953,6 +573265,7 @@ ${this.formatConnectionInfo()}`);
572953
573265
  lines.push(` ${c3.cyan("Errors".padEnd(18))} ${s2.errors}`);
572954
573266
  lines.push(` ${c3.cyan("Tokens in".padEnd(18))} ${fmtTokens(s2.totalTokensIn)}`);
572955
573267
  lines.push(` ${c3.cyan("Tokens out".padEnd(18))} ${fmtTokens(s2.totalTokensOut)}`);
573268
+ lines.push(` ${c3.cyan("Tokens/sec".padEnd(18))} ${s2.tokensPerSecond.toFixed(s2.tokensPerSecond >= 10 ? 0 : 1)} t/s`);
572956
573269
  if (s2.budgetTokensTotal > 0) {
572957
573270
  const pct = Math.round(s2.budgetTokensRemaining / s2.budgetTokensTotal * 100);
572958
573271
  const budgetColor = pct > 50 ? c3.green : pct > 20 ? c3.yellow : c3.red;
@@ -573042,8 +573355,10 @@ ${this.formatConnectionInfo()}`);
573042
573355
  _loadbalance = false;
573043
573356
  _endpointAuth;
573044
573357
  _sponsorLimits = null;
573358
+ _lastPushedSponsorLimitsKey = "";
573045
573359
  _sponsorBlockedRequests = 0;
573046
573360
  _sponsorRequestWindow = [];
573361
+ _tokenRateSamples = [];
573047
573362
  _dailyTokensUsed = 0;
573048
573363
  _dailyTokensResetAt = 0;
573049
573364
  _pollTimer = null;
@@ -573059,6 +573374,7 @@ ${this.formatConnectionInfo()}`);
573059
573374
  errors: 0,
573060
573375
  totalTokensIn: 0,
573061
573376
  totalTokensOut: 0,
573377
+ tokensPerSecond: 0,
573062
573378
  startedAt: Date.now(),
573063
573379
  modelUsage: /* @__PURE__ */ new Map(),
573064
573380
  users: /* @__PURE__ */ new Map(),
@@ -573115,6 +573431,7 @@ ${this.formatConnectionInfo()}`);
573115
573431
  this.ensureSponsorDailyWindow();
573116
573432
  this.refreshSponsorUsageStats();
573117
573433
  this.emitStats();
573434
+ if (this._stats.status === "active") this.pushSponsorLimitsToDaemon();
573118
573435
  }
573119
573436
  getSponsorUsageSnapshot() {
573120
573437
  this.refreshSponsorUsageStats();
@@ -573174,6 +573491,7 @@ ${this.formatConnectionInfo()}`);
573174
573491
  this.refreshSponsorUsageStats();
573175
573492
  }
573176
573493
  refreshSponsorUsageStats(now = Date.now()) {
573494
+ this._stats.tokensPerSecond = currentTokenRate(this._tokenRateSamples, now);
573177
573495
  if (!this._sponsorLimits) {
573178
573496
  this._stats.sponsorUsage = null;
573179
573497
  return;
@@ -573183,6 +573501,10 @@ ${this.formatConnectionInfo()}`);
573183
573501
  this._stats.sponsorUsage = {
573184
573502
  enabled: true,
573185
573503
  transport: "libp2p",
573504
+ totalRequests: this._stats.totalRequests,
573505
+ totalTokensIn: this._stats.totalTokensIn,
573506
+ totalTokensOut: this._stats.totalTokensOut,
573507
+ tokensPerSecond: this._stats.tokensPerSecond,
573186
573508
  dailyTokensUsed: this._dailyTokensUsed,
573187
573509
  dailyTokensLimit: this._sponsorLimits.maxTokensPerDay,
573188
573510
  dailyResetAt: this._dailyTokensResetAt,
@@ -573192,9 +573514,47 @@ ${this.formatConnectionInfo()}`);
573192
573514
  activeConnections: this._stats.activeConnections,
573193
573515
  maxConcurrent: this._sponsorLimits.maxConcurrent,
573194
573516
  blockedRequests: this._sponsorBlockedRequests,
573195
- allowedModels: this._sponsorLimits.allowedModels === "all" ? "all" : [...this._sponsorLimits.allowedModels]
573517
+ allowedModels: this._sponsorLimits.allowedModels === "all" ? "all" : [...this._sponsorLimits.allowedModels],
573518
+ peers: serializeSponsorPeers(this._stats),
573519
+ models: serializeSponsorModels(this._stats)
573196
573520
  };
573197
573521
  }
573522
+ recordServedTokens(tokens, now = Date.now()) {
573523
+ this._stats.tokensPerSecond = recordTokenRate(this._tokenRateSamples, tokens, now);
573524
+ this.refreshSponsorUsageStats(now);
573525
+ }
573526
+ buildExposeArgs() {
573527
+ const exposeArgs = {
573528
+ action: "expose",
573529
+ ollama_url: this._targetUrl,
573530
+ margin: String(this._margin),
573531
+ auth_key: this._authKey
573532
+ };
573533
+ if (this._passthrough) {
573534
+ exposeArgs.passthrough = "true";
573535
+ if (this._endpointAuth) exposeArgs.endpoint_auth = this._endpointAuth;
573536
+ }
573537
+ if (this._sponsorLimits) {
573538
+ exposeArgs.max_concurrent = String(this._sponsorLimits.maxConcurrent);
573539
+ exposeArgs.max_requests_per_minute = String(this._sponsorLimits.maxRequestsPerMinute);
573540
+ exposeArgs.max_tokens_per_day = String(this._sponsorLimits.maxTokensPerDay);
573541
+ exposeArgs.daily_tokens_used = String(this._dailyTokensUsed);
573542
+ exposeArgs.daily_tokens_reset_at = String(this._dailyTokensResetAt);
573543
+ exposeArgs.allowed_models = this._sponsorLimits.allowedModels === "all" ? "all" : this._sponsorLimits.allowedModels.join(",");
573544
+ }
573545
+ return exposeArgs;
573546
+ }
573547
+ pushSponsorLimitsToDaemon() {
573548
+ if (!this._nexusTool || this._stats.status !== "active") return;
573549
+ const exposeArgs = this.buildExposeArgs();
573550
+ const key = JSON.stringify(exposeArgs);
573551
+ if (key === this._lastPushedSponsorLimitsKey) return;
573552
+ this._lastPushedSponsorLimitsKey = key;
573553
+ void this._nexusTool.execute(exposeArgs).catch((err) => {
573554
+ this._lastPushedSponsorLimitsKey = "";
573555
+ this._onError?.(`P2P sponsor limit update failed: ${err instanceof Error ? err.message : String(err)}`);
573556
+ });
573557
+ }
573198
573558
  async start() {
573199
573559
  this._onInfo?.("Connecting to nexus P2P network...");
573200
573560
  const connectResult = await this._nexusTool.execute({
@@ -573207,18 +573567,7 @@ ${this.formatConnectionInfo()}`);
573207
573567
  }
573208
573568
  await new Promise((r2) => setTimeout(r2, 500));
573209
573569
  this._onInfo?.(this._passthrough ? `Registering passthrough capabilities (→ ${this._targetUrl})...` : "Registering inference capabilities...");
573210
- const exposeArgs = {
573211
- action: "expose",
573212
- ollama_url: this._targetUrl,
573213
- margin: String(this._margin),
573214
- auth_key: this._authKey
573215
- };
573216
- if (this._passthrough) {
573217
- exposeArgs.passthrough = "true";
573218
- if (this._endpointAuth) {
573219
- exposeArgs.endpoint_auth = this._endpointAuth;
573220
- }
573221
- }
573570
+ const exposeArgs = this.buildExposeArgs();
573222
573571
  let exposeResult = await this._nexusTool.execute(exposeArgs);
573223
573572
  if (!exposeResult.success && exposeResult.error?.includes("not running")) {
573224
573573
  await new Promise((r2) => setTimeout(r2, 1500));
@@ -573355,7 +573704,8 @@ ${this.formatConnectionInfo()}`);
573355
573704
  const invocCount = files.length;
573356
573705
  const newRequests = invocCount - this._prevInvocCount;
573357
573706
  if (newRequests > 0) {
573358
- this._stats.activeConnections = Math.max(1, newRequests);
573707
+ const activeLimit2 = this._sponsorLimits?.maxConcurrent ?? 10;
573708
+ this._stats.activeConnections = Math.min(Math.max(1, newRequests), activeLimit2);
573359
573709
  this._stats.totalRequests = invocCount;
573360
573710
  this._prevInvocCount = invocCount;
573361
573711
  this.emitStats();
@@ -573380,7 +573730,9 @@ ${this.formatConnectionInfo()}`);
573380
573730
  }
573381
573731
  const inFlightEstimate = Math.max(0, invocCount - meteringLines);
573382
573732
  const prevActive = this._stats.activeConnections;
573383
- this._stats.activeConnections = Math.max(recentActive, Math.min(inFlightEstimate, 10));
573733
+ const activeEstimate = Math.max(recentActive, Math.min(inFlightEstimate, 10));
573734
+ const activeLimit = this._sponsorLimits?.maxConcurrent ?? 10;
573735
+ this._stats.activeConnections = Math.min(activeEstimate, activeLimit);
573384
573736
  if (this._stats.activeConnections !== prevActive) this.emitStats();
573385
573737
  if (this._stats.activeConnections > 0 && !this._tokenFlashTimer) {
573386
573738
  this.emit("token_flash");
@@ -573432,6 +573784,13 @@ ${this.formatConnectionInfo()}`);
573432
573784
  if (!line.trim()) continue;
573433
573785
  try {
573434
573786
  const record = JSON.parse(line);
573787
+ if (record.blocked) {
573788
+ this._sponsorBlockedRequests++;
573789
+ this._stats.errors++;
573790
+ this.recordSponsorRequest();
573791
+ this.refreshSponsorUsageStats();
573792
+ continue;
573793
+ }
573435
573794
  let tokIn = 0;
573436
573795
  let tokOut = 0;
573437
573796
  if (typeof record.inputTokens === "number" && typeof record.outputTokens === "number") {
@@ -573452,6 +573811,7 @@ ${this.formatConnectionInfo()}`);
573452
573811
  }
573453
573812
  this._stats.totalTokensIn += tokIn;
573454
573813
  this._stats.totalTokensOut += tokOut;
573814
+ this.recordServedTokens(tokOut);
573455
573815
  this.recordSponsorRequest();
573456
573816
  this.trackTokenUsage(tokIn, tokOut);
573457
573817
  const peerId = record.from || record.peerId || "unknown";
@@ -573565,6 +573925,7 @@ ${this.formatConnectionInfo()}`);
573565
573925
  lines.push(` ${c3.cyan("Errors".padEnd(18))} ${s2.errors}`);
573566
573926
  lines.push(` ${c3.cyan("Tokens in".padEnd(18))} ${fmtTokens(s2.totalTokensIn)}`);
573567
573927
  lines.push(` ${c3.cyan("Tokens out".padEnd(18))} ${fmtTokens(s2.totalTokensOut)}`);
573928
+ lines.push(` ${c3.cyan("Tokens/sec".padEnd(18))} ${s2.tokensPerSecond.toFixed(s2.tokensPerSecond >= 10 ? 0 : 1)} t/s`);
573568
573929
  if (s2.budgetTokensTotal > 0) {
573569
573930
  const pct = Math.round(s2.budgetTokensRemaining / s2.budgetTokensTotal * 100);
573570
573931
  const budgetColor = pct > 50 ? c3.green : pct > 20 ? c3.yellow : c3.red;
@@ -578973,6 +579334,7 @@ var init_status_bar = __esm({
578973
579334
  setExposeStatus(status) {
578974
579335
  this._expose = status;
578975
579336
  this._exposePeerCount = status.activeConnections;
579337
+ this.setRemoteTokensPerSecond(status.tokensPerSecond);
578976
579338
  if (this.active) this.renderFooterPreserveCursor();
578977
579339
  }
578978
579340
  /** Clear expose gateway status */
@@ -579008,27 +579370,41 @@ var init_status_bar = __esm({
579008
579370
  }
579009
579371
  /** Unified system metrics collector (local or remote) */
579010
579372
  _metricsCollector = new SystemMetricsCollector();
579011
- /** Cached unified metrics snapshot — updated by collector callback */
579373
+ /** Cached local metrics snapshot — updated by collector callback */
579012
579374
  _unifiedMetrics = null;
579375
+ _localUnifiedMetrics = null;
579376
+ _remoteUnifiedMetrics = null;
579377
+ _remoteMetricsUpdatedAt = 0;
579013
579378
  /** Legacy remote metrics polling timer (for peer/HTTP polling) */
579014
579379
  _remoteMetricsTimer = null;
579380
+ setRemoteTokensPerSecond(rate) {
579381
+ const n2 = typeof rate === "number" ? rate : Number(rate);
579382
+ if (!Number.isFinite(n2) || n2 < 0) return;
579383
+ this._tokensPerSecond = n2;
579384
+ if (n2 > this._peakTokensPerSecond) this._peakTokensPerSecond = n2;
579385
+ }
579386
+ getDisplayMetrics() {
579387
+ const local = this._localUnifiedMetrics ?? (this._unifiedMetrics?.source === "local" ? this._unifiedMetrics : null) ?? getInstantSnapshot();
579388
+ const remote = this._remoteUnifiedMetrics;
579389
+ const remoteFresh = remote && Date.now() - this._remoteMetricsUpdatedAt < 6e4;
579390
+ if (!remoteFresh) return local;
579391
+ const showRemote = Math.floor(Date.now() / 5e3) % 2 === 1;
579392
+ if (!showRemote) return local;
579393
+ return {
579394
+ ...remote,
579395
+ network: local.network
579396
+ };
579397
+ }
579015
579398
  /** Update remote host system metrics (from polling /v1/system/metrics) */
579016
579399
  setRemoteMetrics(metrics2) {
579017
- if (!this._metricsCollector.isActive || this._metricsCollector.source !== "remote") {
579018
- this._metricsCollector.startRemote((m2) => {
579019
- this._unifiedMetrics = m2;
579020
- this._gpuName = m2.gpuName || "";
579021
- this._vramTotal = m2.vramTotalMB || 0;
579022
- this._vramUsed = m2.vramUsedMB || 0;
579023
- if (this.active) this.renderFooterPreserveCursor();
579024
- });
579025
- }
579026
- this._metricsCollector.pushRemoteMetrics({
579400
+ const hardware = {
579027
579401
  cpuUtil: metrics2.cpuUtil,
579028
579402
  cpuCores: metrics2.cpuCores ?? 0,
579029
579403
  cpuModel: metrics2.cpuModel ?? "",
579030
579404
  gpuUtil: metrics2.gpuUtil,
579405
+ gpuCount: metrics2.gpuUtil >= 0 || metrics2.gpuName ? 1 : 0,
579031
579406
  gpuName: metrics2.gpuName,
579407
+ gpuDevices: [],
579032
579408
  vramUtil: metrics2.vramUtil,
579033
579409
  vramUsedMB: metrics2.vramUsedMB ?? 0,
579034
579410
  vramTotalMB: metrics2.vramTotalMB ?? 0,
@@ -579039,17 +579415,30 @@ var init_status_bar = __esm({
579039
579415
  diskUsedGB: metrics2.diskUsedGB ?? 0,
579040
579416
  diskTotalGB: metrics2.diskTotalGB ?? 0,
579041
579417
  diskFreeGB: metrics2.diskFreeGB ?? 0,
579042
- diskPath: metrics2.diskPath ?? ""
579043
- });
579418
+ diskPath: metrics2.diskPath ?? "",
579419
+ ollamaPool: null
579420
+ };
579421
+ const local = this._localUnifiedMetrics ?? this._unifiedMetrics ?? getInstantSnapshot();
579422
+ this._remoteUnifiedMetrics = {
579423
+ source: "remote",
579424
+ hardware,
579425
+ network: local.network
579426
+ };
579427
+ this._remoteMetricsUpdatedAt = Date.now();
579428
+ this.setRemoteTokensPerSecond(metrics2.tokensPerSecond);
579429
+ if (this.active) this.renderFooterPreserveCursor();
579044
579430
  }
579045
579431
  /** Clear remote metrics and switch back to local collection */
579046
579432
  clearRemoteMetrics() {
579047
- this._unifiedMetrics = null;
579433
+ this._remoteUnifiedMetrics = null;
579434
+ this._remoteMetricsUpdatedAt = 0;
579048
579435
  if (this._remoteMetricsTimer) {
579049
579436
  clearInterval(this._remoteMetricsTimer);
579050
579437
  this._remoteMetricsTimer = null;
579051
579438
  }
579052
- this.startLocalMetrics();
579439
+ if (!this._metricsCollector.isActive || this._metricsCollector.source !== "local") {
579440
+ this.startLocalMetrics();
579441
+ }
579053
579442
  if (this.active) this.renderFooterPreserveCursor();
579054
579443
  }
579055
579444
  /**
@@ -579058,7 +579447,14 @@ var init_status_bar = __esm({
579058
579447
  */
579059
579448
  startLocalMetrics(intervalMs = 2e3) {
579060
579449
  this._metricsCollector.startLocal((m2) => {
579450
+ this._localUnifiedMetrics = m2;
579061
579451
  this._unifiedMetrics = m2;
579452
+ if (this._remoteUnifiedMetrics) {
579453
+ this._remoteUnifiedMetrics = {
579454
+ ...this._remoteUnifiedMetrics,
579455
+ network: m2.network
579456
+ };
579457
+ }
579062
579458
  if (this.active) this.renderFooterPreserveCursor();
579063
579459
  }, intervalMs);
579064
579460
  try {
@@ -579095,6 +579491,9 @@ var init_status_bar = __esm({
579095
579491
  stopAllMetrics() {
579096
579492
  this._metricsCollector.stop();
579097
579493
  this._unifiedMetrics = null;
579494
+ this._localUnifiedMetrics = null;
579495
+ this._remoteUnifiedMetrics = null;
579496
+ this._remoteMetricsUpdatedAt = 0;
579098
579497
  if (this._remoteMetricsTimer) {
579099
579498
  clearInterval(this._remoteMetricsTimer);
579100
579499
  this._remoteMetricsTimer = null;
@@ -579106,10 +579505,6 @@ var init_status_bar = __esm({
579106
579505
  */
579107
579506
  startRemoteMetricsPolling(endpointUrl, authKey) {
579108
579507
  this.stopRemoteMetricsPolling();
579109
- this._metricsCollector.startRemote((m2) => {
579110
- this._unifiedMetrics = m2;
579111
- if (this.active) this.renderFooterPreserveCursor();
579112
- });
579113
579508
  const poll = async () => {
579114
579509
  try {
579115
579510
  const url = new URL("/v1/system/metrics", endpointUrl);
@@ -579131,7 +579526,8 @@ var init_status_bar = __esm({
579131
579526
  diskUsedGB: data.disk?.usedGB ?? 0,
579132
579527
  diskTotalGB: data.disk?.totalGB ?? 0,
579133
579528
  diskFreeGB: data.disk?.freeGB ?? 0,
579134
- diskPath: data.disk?.path ?? ""
579529
+ diskPath: data.disk?.path ?? "",
579530
+ tokensPerSecond: data.gateway?.tokensPerSecond
579135
579531
  });
579136
579532
  }
579137
579533
  } catch {
@@ -579147,10 +579543,6 @@ var init_status_bar = __esm({
579147
579543
  */
579148
579544
  startPeerMetricsPolling(sendCommand, peerId, authKey, nexusDir) {
579149
579545
  this.stopRemoteMetricsPolling();
579150
- this._metricsCollector.startRemote((m2) => {
579151
- this._unifiedMetrics = m2;
579152
- if (this.active) this.renderFooterPreserveCursor();
579153
- });
579154
579546
  let pollAttempt = 0;
579155
579547
  const extractMetrics = (raw) => {
579156
579548
  let obj = raw;
@@ -579217,6 +579609,7 @@ var init_status_bar = __esm({
579217
579609
  const cached = JSON.parse(raw);
579218
579610
  if (cached && cached.ts && Date.now() - cached.ts < 6e4) {
579219
579611
  const m2 = cached.data;
579612
+ this.setRemoteTokensPerSecond(m2?.gateway?.tokensPerSecond);
579220
579613
  if (m2?.cpu) {
579221
579614
  lastPeerMetricsDebug = `ok: cpu=${m2.cpu?.utilization}%`;
579222
579615
  this.setRemoteMetrics({
@@ -579235,7 +579628,8 @@ var init_status_bar = __esm({
579235
579628
  diskTotalGB: m2.disk?.totalGB ?? 0,
579236
579629
  diskUsedGB: m2.disk?.usedGB ?? 0,
579237
579630
  diskFreeGB: m2.disk?.freeGB ?? 0,
579238
- diskPath: m2.disk?.path ?? ""
579631
+ diskPath: m2.disk?.path ?? "",
579632
+ tokensPerSecond: m2.gateway?.tokensPerSecond
579239
579633
  });
579240
579634
  return;
579241
579635
  }
@@ -579263,7 +579657,11 @@ var init_status_bar = __esm({
579263
579657
  clearInterval(this._remoteMetricsTimer);
579264
579658
  this._remoteMetricsTimer = null;
579265
579659
  }
579266
- this.startLocalMetrics();
579660
+ this._remoteUnifiedMetrics = null;
579661
+ this._remoteMetricsUpdatedAt = 0;
579662
+ if (!this._metricsCollector.isActive || this._metricsCollector.source !== "local") {
579663
+ this.startLocalMetrics();
579664
+ }
579267
579665
  }
579268
579666
  /** Update token metrics from a token_usage event */
579269
579667
  updateMetrics(update2) {
@@ -579432,7 +579830,7 @@ var init_status_bar = __esm({
579432
579830
  // 10: █ full block
579433
579831
  ];
579434
579832
  if (this._focusFrame < 4) return DENSITY2[10];
579435
- const m2 = this._unifiedMetrics ?? getInstantSnapshot();
579833
+ const m2 = this.getDisplayMetrics();
579436
579834
  const cpu = m2.hardware.cpuUtil;
579437
579835
  const gpu = m2.hardware.gpuUtil;
579438
579836
  const mem = m2.hardware.memUtil;
@@ -580898,7 +581296,7 @@ ${CONTENT_BG_SEQ}`);
580898
581296
  });
580899
581297
  }
580900
581298
  {
580901
- const um = this._unifiedMetrics ?? getInstantSnapshot();
581299
+ const um = this.getDisplayMetrics();
580902
581300
  const rm4 = um.hardware;
580903
581301
  const isLocal = um.source === "local";
580904
581302
  const srcTag = isLocal ? pastel2(120, "L") : pastel2(117, "R");
@@ -581091,23 +581489,57 @@ ${CONTENT_BG_SEQ}`);
581091
581489
  const w = termWidth ?? getTermWidth();
581092
581490
  return Math.max(1, w - this.promptWidth - 2);
581093
581491
  }
581492
+ wrapPlainInputText(text, availWidth) {
581493
+ const width = Math.max(1, availWidth);
581494
+ const rawLines = [];
581495
+ const charPositions = [];
581496
+ const pushWrappedSegment = (segment, segmentStart2) => {
581497
+ if (segment.length === 0) {
581498
+ charPositions.push(segmentStart2);
581499
+ rawLines.push("");
581500
+ return;
581501
+ }
581502
+ let offset = 0;
581503
+ while (offset < segment.length) {
581504
+ const remaining = segment.slice(offset);
581505
+ if (remaining.length <= width) {
581506
+ charPositions.push(segmentStart2 + offset);
581507
+ rawLines.push(remaining);
581508
+ break;
581509
+ }
581510
+ let breakAt = width;
581511
+ const lastSpace = remaining.lastIndexOf(" ", width);
581512
+ if (lastSpace > 0 && lastSpace >= width * 0.3) {
581513
+ breakAt = lastSpace + 1;
581514
+ }
581515
+ charPositions.push(segmentStart2 + offset);
581516
+ rawLines.push(remaining.slice(0, breakAt));
581517
+ offset += breakAt;
581518
+ }
581519
+ };
581520
+ if (text.length === 0) {
581521
+ pushWrappedSegment("", 0);
581522
+ return { rawLines, charPositions };
581523
+ }
581524
+ let segmentStart = 0;
581525
+ while (segmentStart <= text.length) {
581526
+ const newlineAt = text.indexOf("\n", segmentStart);
581527
+ const segmentEnd = newlineAt === -1 ? text.length : newlineAt;
581528
+ pushWrappedSegment(text.slice(segmentStart, segmentEnd), segmentStart);
581529
+ if (newlineAt === -1) break;
581530
+ segmentStart = newlineAt + 1;
581531
+ if (segmentStart === text.length) {
581532
+ pushWrappedSegment("", segmentStart);
581533
+ break;
581534
+ }
581535
+ }
581536
+ return { rawLines, charPositions };
581537
+ }
581094
581538
  computeInputLineCount(termWidth) {
581095
581539
  if (!this.inputStateProvider) return 1;
581096
581540
  const availWidth = this.inputTextWidth(termWidth);
581097
581541
  const { line } = this.inputStateProvider();
581098
- if (line.length <= availWidth) return 1;
581099
- let count = 0;
581100
- let remaining = line;
581101
- while (remaining.length > 0) {
581102
- count++;
581103
- if (remaining.length <= availWidth) break;
581104
- let breakAt = availWidth;
581105
- const lastSpace = remaining.lastIndexOf(" ", availWidth);
581106
- if (lastSpace > 0 && lastSpace >= availWidth * 0.3)
581107
- breakAt = lastSpace + 1;
581108
- remaining = remaining.slice(breakAt);
581109
- }
581110
- return Math.max(1, count);
581542
+ return Math.max(1, this.wrapPlainInputText(line, availWidth).rawLines.length);
581111
581543
  }
581112
581544
  /** Update _currentFooterHeight based on current input + suggestions. Returns true if height changed. */
581113
581545
  updateFooterHeight(termWidth) {
@@ -581167,8 +581599,9 @@ ${CONTENT_BG_SEQ}`);
581167
581599
  const inputState = this.inputStateProvider?.();
581168
581600
  const fullLine = inputState?.line ?? "";
581169
581601
  const cursorPos = inputState?.cursor ?? 0;
581602
+ const hasExplicitLineBreak = fullLine.includes("\n");
581170
581603
  const ghost = this.getGhostText(fullLine, cursorPos);
581171
- if (fullLine.length <= availWidth) {
581604
+ if (!hasExplicitLineBreak && fullLine.length <= availWidth) {
581172
581605
  let displayLine;
581173
581606
  if (ghost) {
581174
581607
  displayLine = fullLine + `\x1B[7m\x1B[38;5;${TEXT_DIM}m${ghost[0]}\x1B[0m${PANEL_BG_SEQ}\x1B[38;5;${TEXT_DIM}m${ghost.slice(1)}\x1B[0m${PANEL_BG_SEQ}`;
@@ -581183,28 +581616,7 @@ ${CONTENT_BG_SEQ}`);
581183
581616
  cursorCol: Math.min(Math.max(1, termWidth - 1), this.promptWidth + cursorPos + 2)
581184
581617
  };
581185
581618
  }
581186
- const rawLines = [];
581187
- let remaining = fullLine;
581188
- const charPositions = [];
581189
- while (remaining.length > 0) {
581190
- if (remaining.length <= availWidth) {
581191
- charPositions.push(fullLine.length - remaining.length);
581192
- rawLines.push(remaining);
581193
- break;
581194
- }
581195
- let breakAt = availWidth;
581196
- const lastSpace = remaining.lastIndexOf(" ", availWidth);
581197
- if (lastSpace > 0 && lastSpace >= availWidth * 0.3) {
581198
- breakAt = lastSpace + 1;
581199
- }
581200
- charPositions.push(fullLine.length - remaining.length);
581201
- rawLines.push(remaining.slice(0, breakAt));
581202
- remaining = remaining.slice(breakAt);
581203
- }
581204
- if (rawLines.length === 0) {
581205
- rawLines.push("");
581206
- charPositions.push(0);
581207
- }
581619
+ const { rawLines, charPositions } = this.wrapPlainInputText(fullLine, availWidth);
581208
581620
  let cursorLineIdx = rawLines.length - 1;
581209
581621
  let cursorColInLine = cursorPos;
581210
581622
  for (let i2 = 0; i2 < charPositions.length; i2++) {
@@ -590531,6 +590943,15 @@ __export(sponsor_wizard_exports, {
590531
590943
  });
590532
590944
  import { existsSync as existsSync104, readFileSync as readFileSync83, writeFileSync as writeFileSync52, mkdirSync as mkdirSync57 } from "node:fs";
590533
590945
  import { join as join118 } from "node:path";
590946
+ function fmtTokens2(n2) {
590947
+ if (n2 < 1e3) return String(Math.max(0, Math.floor(n2)));
590948
+ if (n2 < 1e6) return `${(n2 / 1e3).toFixed(1)}K`;
590949
+ return `${(n2 / 1e6).toFixed(1)}M`;
590950
+ }
590951
+ function fmtTps(n2) {
590952
+ if (!Number.isFinite(n2) || n2 <= 0) return "0";
590953
+ return n2 >= 10 ? n2.toFixed(0) : n2.toFixed(1);
590954
+ }
590534
590955
  function sponsorDir(projectDir2) {
590535
590956
  return join118(projectDir2, ".omnius", "sponsor");
590536
590957
  }
@@ -591312,7 +591733,13 @@ async function showSponsorDashboard(config, projectDir2, rl, availableRows, spon
591312
591733
  const dailyTokensLimit = sponsorUsage?.dailyTokensLimit || config.rateLimits.maxTokensPerDay;
591313
591734
  const requestsPerMinuteLimit = sponsorUsage?.requestsPerMinuteLimit || config.rateLimits.maxRequestsPerMinute;
591314
591735
  const maxConcurrent = sponsorUsage?.maxConcurrent || config.rateLimits.maxConcurrent;
591736
+ const topModels = (sponsorUsage?.models ?? []).slice(0, 5);
591737
+ const topPeers = (sponsorUsage?.peers ?? []).slice(0, 5);
591315
591738
  const usageItems = [
591739
+ {
591740
+ key: "info_usage_totals",
591741
+ label: ` Totals: ${sponsorUsage?.totalRequests ?? 0} req · in ${fmtTokens2(sponsorUsage?.totalTokensIn ?? 0)} · out ${fmtTokens2(sponsorUsage?.totalTokensOut ?? 0)} · ${fmtTps(sponsorUsage?.tokensPerSecond ?? 0)} t/s`
591742
+ },
591316
591743
  {
591317
591744
  key: "info_usage_daily",
591318
591745
  label: ` ${formatUsageBar({
@@ -591344,12 +591771,31 @@ async function showSponsorDashboard(config, projectDir2, rl, availableRows, spon
591344
591771
  label: ` Blocked: ${sponsorUsage?.blockedRequests ?? 0}`
591345
591772
  }
591346
591773
  ];
591774
+ if (topModels.length > 0) {
591775
+ usageItems.push({ key: "info_usage_models_hdr", label: " Models" });
591776
+ for (const [idx, model] of topModels.entries()) {
591777
+ usageItems.push({
591778
+ key: `info_usage_model_${idx}`,
591779
+ label: ` ${model.model}: ${model.requests} req · ${fmtTokens2(model.tokensIn + model.tokensOut)} tok`
591780
+ });
591781
+ }
591782
+ }
591783
+ if (topPeers.length > 0) {
591784
+ usageItems.push({ key: "info_usage_peers_hdr", label: " Peers" });
591785
+ for (const [idx, peer] of topPeers.entries()) {
591786
+ const active = peer.activeRequests > 0 ? ` · ${peer.activeRequests} active` : "";
591787
+ usageItems.push({
591788
+ key: `info_usage_peer_${idx}`,
591789
+ label: ` ${peer.peer}: ${peer.requests} req · ${fmtTokens2(peer.tokensIn + peer.tokensOut)} tok${active}`
591790
+ });
591791
+ }
591792
+ }
591347
591793
  const items = [
591348
591794
  { key: "hdr", label: "Sponsor Dashboard" },
591349
591795
  { key: "info_status", label: ` Status: ${isPaused ? "● PAUSED" : "● ACTIVE"}` },
591350
591796
  { key: "info_ep", label: ` Endpoints: ${enabledEps.map((e2) => e2.label).join(", ")}` },
591351
591797
  { key: "info_transport", label: ` Transport: ${[config.transport.cloudflared ? "Cloudflared" : "", config.transport.libp2p ? "libp2p" : ""].filter(Boolean).join(" + ")}` },
591352
- { key: "info_limits", label: ` Limits: ${config.rateLimits.maxRequestsPerMinute} req/min, ${config.rateLimits.maxTokensPerDay.toLocaleString()} tokens/day` },
591798
+ { key: "info_limits", label: ` Limits: ${config.rateLimits.maxRequestsPerMinute} req/min, ${config.rateLimits.maxTokensPerDay.toLocaleString()} tokens/day, ${config.rateLimits.maxConcurrent} concurrent` },
591353
591799
  { key: "info_usage_hdr", label: " Usage" },
591354
591800
  ...usageItems,
591355
591801
  { key: "sep", label: "" },
@@ -591361,7 +591807,7 @@ async function showSponsorDashboard(config, projectDir2, rl, availableRows, spon
591361
591807
  items,
591362
591808
  title: "Sponsor Dashboard",
591363
591809
  rl,
591364
- skipKeys: ["hdr", "sep", "info_status", "info_ep", "info_transport", "info_limits", "info_usage_hdr", "info_usage_daily", "info_usage_rpm", "info_usage_concurrent", "info_usage_blocked"],
591810
+ skipKeys: items.map((item) => item.key).filter((key) => !["modify", "pause", "resume", "remove"].includes(key)),
591365
591811
  availableRows
591366
591812
  });
591367
591813
  if (!result.confirmed) return "close";
@@ -595626,6 +596072,18 @@ import {
595626
596072
  writeSync as writeSync2
595627
596073
  } from "node:fs";
595628
596074
  import { relative as relative11, join as join120 } from "node:path";
596075
+ async function parseJsonResponse(resp, source) {
596076
+ const body = await resp.text();
596077
+ const trimmed = body.trim();
596078
+ if (!trimmed) {
596079
+ throw new Error(`${source} returned an empty response${resp.status ? ` (HTTP ${resp.status})` : ""}`);
596080
+ }
596081
+ try {
596082
+ return JSON.parse(trimmed);
596083
+ } catch {
596084
+ throw new Error(`${source} returned malformed JSON${resp.status ? ` (HTTP ${resp.status})` : ""}`);
596085
+ }
596086
+ }
595629
596087
  async function _immediateReregister(newUrl) {
595630
596088
  if (!_lastRegisteredSponsorPayload) return;
595631
596089
  _lastRegisteredSponsorPayload.tunnelUrl = newUrl;
@@ -600471,7 +600929,8 @@ sleep 1
600471
600929
  authKey: tunnelGw?.authKey || "",
600472
600930
  limits: {
600473
600931
  maxRequestsPerMinute: config.rateLimits.maxRequestsPerMinute,
600474
- maxTokensPerDay: config.rateLimits.maxTokensPerDay
600932
+ maxTokensPerDay: config.rateLimits.maxTokensPerDay,
600933
+ maxConcurrent: config.rateLimits.maxConcurrent
600475
600934
  },
600476
600935
  banner: "none",
600477
600936
  message: config.header.message || sponsorName,
@@ -600495,7 +600954,10 @@ sleep 1
600495
600954
  signal: AbortSignal.timeout(1e4)
600496
600955
  }
600497
600956
  );
600498
- const kvResult = await kvResp.json();
600957
+ const kvResult = await parseJsonResponse(
600958
+ kvResp,
600959
+ "Sponsor directory"
600960
+ );
600499
600961
  if (kvResult.persisted) {
600500
600962
  _spLog("KV registration: persisted");
600501
600963
  renderInfo(
@@ -605784,7 +606246,7 @@ async function handleSponsoredEndpoint(ctx3, local) {
605784
606246
  signal: AbortSignal.timeout(5e3)
605785
606247
  });
605786
606248
  if (kvResp.ok) {
605787
- const kvData = await kvResp.json();
606249
+ const kvData = await parseJsonResponse(kvResp, "Sponsor directory");
605788
606250
  const kvSponsors = (kvData.sponsors || []).filter(
605789
606251
  (s2) => s2.status === "active"
605790
606252
  );
@@ -616317,7 +616779,7 @@ function fmtDuration(ms) {
616317
616779
  const rm4 = m2 % 60;
616318
616780
  return `${h}h ${rm4}m`;
616319
616781
  }
616320
- function fmtTokens2(n2) {
616782
+ function fmtTokens3(n2) {
616321
616783
  if (n2 < 1e3) return String(n2);
616322
616784
  if (n2 < 1e6) return `${(n2 / 1e3).toFixed(1)}K`;
616323
616785
  return `${(n2 / 1e6).toFixed(2)}M`;
@@ -616346,8 +616808,8 @@ function buildMetricEntries(snap, scope) {
616346
616808
  entries.push({
616347
616809
  icon: "",
616348
616810
  label: "Tokens",
616349
- value: fmtTokens2(snap.totalTokens),
616350
- detail: `Prompt: ${fmtTokens2(snap.totalPromptTokens)} · Completion: ${fmtTokens2(snap.totalCompletionTokens)}`,
616811
+ value: fmtTokens3(snap.totalTokens),
616812
+ detail: `Prompt: ${fmtTokens3(snap.totalPromptTokens)} · Completion: ${fmtTokens3(snap.totalCompletionTokens)}`,
616351
616813
  category: "inference"
616352
616814
  });
616353
616815
  entries.push({
@@ -616383,14 +616845,14 @@ function buildMetricEntries(snap, scope) {
616383
616845
  entries.push({
616384
616846
  icon: "",
616385
616847
  label: "Context Window",
616386
- value: fmtTokens2(snap.contextWindowSize),
616848
+ value: fmtTokens3(snap.contextWindowSize),
616387
616849
  category: "context"
616388
616850
  });
616389
616851
  entries.push({
616390
616852
  icon: "",
616391
616853
  label: "Context Used",
616392
- value: fmtTokens2(snap.estimatedContextTokens),
616393
- detail: `Utilization: ${fmtPct(snap.contextUtilizationPct)} · Peak: ${fmtTokens2(snap.peakContextTokens)}`,
616854
+ value: fmtTokens3(snap.estimatedContextTokens),
616855
+ detail: `Utilization: ${fmtPct(snap.contextUtilizationPct)} · Peak: ${fmtTokens3(snap.peakContextTokens)}`,
616394
616856
  category: "context"
616395
616857
  });
616396
616858
  entries.push({
@@ -633038,27 +633500,48 @@ var init_direct_input = __esm({
633038
633500
  * and rawLines (text of each line). Matches wrapInput logic in status-bar.ts.
633039
633501
  */
633040
633502
  _computeWrappedLines(availWidth) {
633503
+ const width = Math.max(1, availWidth);
633041
633504
  const rawLines = [];
633042
633505
  const charPositions = [];
633043
- let remaining = this.line;
633044
- while (remaining.length > 0) {
633045
- if (remaining.length <= availWidth) {
633046
- charPositions.push(this.line.length - remaining.length);
633047
- rawLines.push(remaining);
633048
- break;
633506
+ const pushWrappedSegment = (segment, segmentStart2) => {
633507
+ if (segment.length === 0) {
633508
+ charPositions.push(segmentStart2);
633509
+ rawLines.push("");
633510
+ return;
633049
633511
  }
633050
- let breakAt = availWidth;
633051
- const lastSpace = remaining.lastIndexOf(" ", availWidth);
633052
- if (lastSpace > 0 && lastSpace >= availWidth * 0.3) {
633053
- breakAt = lastSpace + 1;
633512
+ let offset = 0;
633513
+ while (offset < segment.length) {
633514
+ const remaining = segment.slice(offset);
633515
+ if (remaining.length <= width) {
633516
+ charPositions.push(segmentStart2 + offset);
633517
+ rawLines.push(remaining);
633518
+ break;
633519
+ }
633520
+ let breakAt = width;
633521
+ const lastSpace = remaining.lastIndexOf(" ", width);
633522
+ if (lastSpace > 0 && lastSpace >= width * 0.3) {
633523
+ breakAt = lastSpace + 1;
633524
+ }
633525
+ charPositions.push(segmentStart2 + offset);
633526
+ rawLines.push(remaining.slice(0, breakAt));
633527
+ offset += breakAt;
633528
+ }
633529
+ };
633530
+ if (this.line.length === 0) {
633531
+ pushWrappedSegment("", 0);
633532
+ return { charPositions, rawLines };
633533
+ }
633534
+ let segmentStart = 0;
633535
+ while (segmentStart <= this.line.length) {
633536
+ const newlineAt = this.line.indexOf("\n", segmentStart);
633537
+ const segmentEnd = newlineAt === -1 ? this.line.length : newlineAt;
633538
+ pushWrappedSegment(this.line.slice(segmentStart, segmentEnd), segmentStart);
633539
+ if (newlineAt === -1) break;
633540
+ segmentStart = newlineAt + 1;
633541
+ if (segmentStart === this.line.length) {
633542
+ pushWrappedSegment("", segmentStart);
633543
+ break;
633054
633544
  }
633055
- charPositions.push(this.line.length - remaining.length);
633056
- rawLines.push(remaining.slice(0, breakAt));
633057
- remaining = remaining.slice(breakAt);
633058
- }
633059
- if (rawLines.length === 0) {
633060
- rawLines.push("");
633061
- charPositions.push(0);
633062
633545
  }
633063
633546
  return { charPositions, rawLines };
633064
633547
  }
@@ -633244,6 +633727,10 @@ var init_direct_input = __esm({
633244
633727
  this.cursor = this.line.length;
633245
633728
  return;
633246
633729
  case "~":
633730
+ if (this._isShiftEnterCSI(params)) {
633731
+ this._insertText("\n");
633732
+ return;
633733
+ }
633247
633734
  if (params === "3") {
633248
633735
  if (this.cursor < this.line.length) {
633249
633736
  this.line = this.line.slice(0, this.cursor) + this.line.slice(this.cursor + 1);
@@ -633265,6 +633752,10 @@ var init_direct_input = __esm({
633265
633752
  const modifiers = parseInt(parts[1] ?? "1");
633266
633753
  const hasCtrl = modifiers - 1 & 4;
633267
633754
  const hasShift = modifiers - 1 & 1;
633755
+ if (hasShift && (codepoint === 10 || codepoint === 13)) {
633756
+ this._insertText("\n");
633757
+ return;
633758
+ }
633268
633759
  if (hasCtrl && hasShift) {
633269
633760
  if (codepoint === 67) {
633270
633761
  this.emit("ctrl-shift-c");
@@ -633283,6 +633774,22 @@ var init_direct_input = __esm({
633283
633774
  }
633284
633775
  }
633285
633776
  }
633777
+ _isShiftEnterCSI(params) {
633778
+ const parts = params.split(";").map((part) => parseInt(part, 10));
633779
+ if (parts.length === 2) {
633780
+ const [codepoint, modifiers] = parts;
633781
+ return (codepoint === 10 || codepoint === 13) && modifiers === 2;
633782
+ }
633783
+ if (parts.length === 3) {
633784
+ const [prefix, modifiers, codepoint] = parts;
633785
+ return prefix === 27 && modifiers === 2 && (codepoint === 10 || codepoint === 13);
633786
+ }
633787
+ return false;
633788
+ }
633789
+ _insertText(text) {
633790
+ this.line = this.line.slice(0, this.cursor) + text + this.line.slice(this.cursor);
633791
+ this.cursor += text.length;
633792
+ }
633286
633793
  /** Handle SS3 sequence: \x1BO {final} (some terminals use this for arrows/Home/End) */
633287
633794
  _handleSS3(final2) {
633288
633795
  switch (final2) {
@@ -661913,7 +662420,8 @@ Log: ${nexusLogPath}`)
661913
662420
  status: stats.status,
661914
662421
  totalRequests: stats.totalRequests,
661915
662422
  activeConnections: stats.activeConnections,
661916
- modelUsage: stats.modelUsage
662423
+ modelUsage: stats.modelUsage,
662424
+ tokensPerSecond: stats.tokensPerSecond
661917
662425
  });
661918
662426
  });
661919
662427
  reconnected.on("token_flash", () => statusBar.flashExposeToken());
@@ -661956,7 +662464,8 @@ Log: ${nexusLogPath}`)
661956
662464
  status: stats.status,
661957
662465
  totalRequests: stats.totalRequests,
661958
662466
  activeConnections: stats.activeConnections,
661959
- modelUsage: stats.modelUsage
662467
+ modelUsage: stats.modelUsage,
662468
+ tokensPerSecond: stats.tokensPerSecond
661960
662469
  });
661961
662470
  });
661962
662471
  reconnectedP2P.on("token_flash", () => statusBar.flashExposeToken());
@@ -662062,7 +662571,8 @@ Log: ${nexusLogPath}`)
662062
662571
  { signal: AbortSignal.timeout(8e3) }
662063
662572
  );
662064
662573
  if (spResp.ok) {
662065
- const spData = await spResp.json();
662574
+ const spText = await spResp.text();
662575
+ const spData = spText.trim() ? JSON.parse(spText) : { sponsors: [] };
662066
662576
  let active = (spData.sponsors || []).filter(
662067
662577
  (s2) => s2.status === "active" && s2.tunnelUrl
662068
662578
  );
@@ -663848,7 +664358,8 @@ Respond concisely and safely. Remember: you are talking to the general public.`;
663848
664358
  status: stats.status,
663849
664359
  totalRequests: stats.totalRequests,
663850
664360
  activeConnections: stats.activeConnections,
663851
- modelUsage: stats.modelUsage
664361
+ modelUsage: stats.modelUsage,
664362
+ tokensPerSecond: stats.tokensPerSecond
663852
664363
  });
663853
664364
  });
663854
664365
  newP2P.on("token_flash", () => statusBar.flashExposeToken());
@@ -663889,7 +664400,8 @@ Respond concisely and safely. Remember: you are talking to the general public.`;
663889
664400
  status: stats.status,
663890
664401
  totalRequests: stats.totalRequests,
663891
664402
  activeConnections: stats.activeConnections,
663892
- modelUsage: stats.modelUsage
664403
+ modelUsage: stats.modelUsage,
664404
+ tokensPerSecond: stats.tokensPerSecond
663893
664405
  });
663894
664406
  });
663895
664407
  newTunnel.on("token_flash", () => statusBar.flashExposeToken());