dsh-tokenbank 0.1.61 → 0.1.63

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.
Files changed (3) hide show
  1. package/lib/client.js +140 -70
  2. package/lib/index.js +12 -0
  3. package/package.json +2 -2
package/lib/client.js CHANGED
@@ -47,6 +47,9 @@ window.__ModuleLoader__.load({
47
47
  rechargeCancel: '取消',
48
48
  composerEntry: 'TokenBank 账户',
49
49
  officialSite: 'TokenBank 官网',
50
+ claimFree: '领取免费TOKEN',
51
+ freeToken: '免费模型',
52
+ modelLinked: '已接入 {n} 个模型,去切换',
50
53
  loading: '正在连接 TokenBank…',
51
54
  balance: '余额',
52
55
  totalEarned: '累计收益',
@@ -139,6 +142,9 @@ window.__ModuleLoader__.load({
139
142
  rechargeCancel: 'Cancel',
140
143
  composerEntry: 'TokenBank account',
141
144
  officialSite: 'TokenBank website',
145
+ claimFree: 'Claim free TOKEN',
146
+ freeToken: 'FREE',
147
+ modelLinked: 'Linked {n} models, tap to switch',
142
148
  loading: 'Connecting to TokenBank…',
143
149
  balance: 'Balance',
144
150
  totalEarned: 'Total earned',
@@ -323,34 +329,6 @@ window.__ModuleLoader__.load({
323
329
  }
324
330
 
325
331
  /** 输入框工具行、模型选择器左侧的紧凑入口按钮。 */
326
- function ComposerEntry() {
327
- var snap = useTbStore()
328
- React.useEffect(function () {
329
- var stopped = false
330
- api('status').then(function (data) {
331
- if (!stopped) tbStore.set({ loggedIn: Boolean(data.loggedIn), portalUrl: data.portalUrl })
332
- }).catch(function () {})
333
- return function () { stopped = true }
334
- }, [])
335
- var onClick = function (e) {
336
- var r = e.currentTarget.getBoundingClientRect()
337
- tbStore.set({ authOpen: true, authMode: 'register', anchor: { top: r.top, right: r.right, bottom: r.bottom, left: r.left } })
338
- }
339
- return h('button', {
340
- type: 'button',
341
- title: pageT('composerEntry'), 'aria-label': pageT('composerEntry'),
342
- onClick: onClick,
343
- style: {
344
- display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 4,
345
- height: 29, minWidth: 29, padding: '0 2px', borderRadius: 7,
346
- border: 'none', background: 'transparent',
347
- cursor: 'pointer', position: 'relative',
348
- },
349
- },
350
- h(TokenBankLogo, { size: 18 }),
351
- )
352
- }
353
-
354
332
  /** shell.overlay 轻提示:portal 到 document.body,压过设置页抽屉层级。 */
355
333
  function NotifyOverlay() {
356
334
  var snap = useTbStore()
@@ -612,6 +590,7 @@ window.__ModuleLoader__.load({
612
590
  var snap = useTbStore()
613
591
  var st = React.useState(null)
614
592
  var boxRef = React.useRef(null)
593
+ var sLogoutArm = React.useState(false) // 两段式退出确认(参考弹窗:首次点变红描边,再点才退出)
615
594
  React.useEffect(function () {
616
595
  var stopped = false
617
596
  api('status').then(function (data) {
@@ -622,49 +601,133 @@ window.__ModuleLoader__.load({
622
601
  }, 60000)
623
602
  return function () { stopped = true; clearInterval(timer) }
624
603
  }, [])
625
- var openPanel = function (mode) {
626
- var el = boxRef.current
627
- var r = el ? el.getBoundingClientRect() : { top: window.innerHeight - 80, right: window.innerWidth - 16, bottom: window.innerHeight, left: window.innerWidth - 272 }
628
- tbStore.set({ authOpen: true, authMode: mode || snap.authMode || 'login', anchor: { top: r.top, right: r.right, bottom: r.bottom, left: r.left } })
604
+ React.useEffect(function () {
605
+ var stopped = false
606
+ api('status').then(function (d) {
607
+ if (stopped) return
608
+ st[1](d)
609
+ tbStore.set({ loggedIn: Boolean(d.loggedIn), portalUrl: d.portalUrl })
610
+ }).catch(function () {})
611
+ return function () { stopped = true }
612
+ }, [snap.loggedIn, snap.authOpen])
613
+ var openGlobal = function (mode, opts) {
614
+ tbStore.set({ authOpen: true, authMode: mode || snap.authMode || 'login', anchor: null, startRecharge: Boolean(opts && opts.recharge) })
615
+ }
616
+ var flash = React.useState(null) // {text, models} —「已接入 n 个模型,点击去切换」
617
+ var addModels = async function () {
618
+ if (flash[0]) return
619
+ try {
620
+ var d0 = st[0]
621
+ if (!d0 || !d0.accessKey) throw new Error(pageT('loading'))
622
+ var models = await api('gateway-models', { accessKey: d0.accessKey })
623
+ var list = Array.isArray(models) ? models : (models.models || models.list || [])
624
+ await addProviderToModels(runtimeCtx, d0.baseURL || d0.portalUrl || portal, { accessKey: d0.accessKey, list: list, protocol: models.protocol })
625
+ flash[1]({ text: pageT('modelLinked').replace('{n}', String(list.length)), models: true })
626
+ window.setTimeout(function () { flash[1](null) }, 3000)
627
+ } catch (err) {
628
+ tbStore.set({ notify: { kind: 'error', title: String(err.message || err), detail: '' } })
629
+ }
629
630
  }
630
631
  var data = st[0]
632
+ var portal = (data && data.portalUrl) || 'https://bank.skillpie.cn'
631
633
  var loggedIn = snap.loggedIn
632
634
  if (!runtimeCtx) return null
633
635
  var smallBtn = Object.assign({}, buttonStyle, { height: 24, padding: '0 8px', fontSize: 12 })
634
636
  return h('div', {
635
637
  ref: boxRef,
636
638
  style: {
637
- margin: '4px 10px 8px', display: 'flex', flexDirection: 'column', gap: 6,
639
+ margin: '4px 10px 8px', display: 'flex', flexDirection: 'column', gap: 6, width: 'calc(100% - 20px)', minWidth: 0, maxWidth: '100%', overflow: 'hidden', boxSizing: 'border-box', alignSelf: 'stretch'
638
640
  },
639
641
  },
640
642
  h('div', { style: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 6 } },
641
- h('span', { className: 'tb-wordmark', style: { fontFamily: "'Frizon','FrizonLocal',sans-serif", fontSize: 14, letterSpacing: 0.5, color: '#ff4f1f' } }, 'TOKENBANK'),
642
- h('a', { href: (data && data.portalUrl) || 'https://bank.skillpie.cn', target: '_blank', rel: 'noopener noreferrer', title: pageT('officialSite'), style: { display: 'inline-flex', color: 'var(--dsw-alias-label-secondary, #888)', textDecoration: 'none' } },
643
- h('svg', { viewBox: '0 0 24 24', width: 13, height: 13, fill: 'none', stroke: 'currentColor', 'stroke-width': 2, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' },
644
- h('path', { d: 'M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6' }),
645
- h('path', { d: 'M15 3h6v6' }),
646
- h('path', { d: 'M10 14L21 3' })))),
647
- loggedIn ? h('div', { style: { display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', gap: 6 } },
648
- h('span', { style: { fontSize: 12, fontWeight: 600, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } },
643
+ h('div', { style: { display: 'inline-flex', alignItems: 'center', gap: 4 } },
644
+ h('span', {
645
+ className: 'tb-wordmark',
646
+ style: { fontFamily: "'Frizon','FrizonLocal',sans-serif", fontSize: 14, letterSpacing: 0.5, color: '#ff4f1f', position: 'relative', top: -1, cursor: 'pointer' },
647
+ onClick: function () { openGlobal('register') },
648
+ }, 'TOKENBANK'),
649
+ h('a', { href: (data && data.portalUrl) || 'https://bank.skillpie.cn', target: '_blank', rel: 'noopener noreferrer', title: pageT('officialSite'), style: { display: 'inline-flex', color: 'var(--dsw-alias-label-secondary, #888)', textDecoration: 'none' } },
650
+ h('svg', { viewBox: '0 0 24 24', width: 12, height: 12, fill: 'none', stroke: 'currentColor', 'stroke-width': 2, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' },
651
+ h('path', { d: 'M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6' }),
652
+ h('path', { d: 'M15 3h6v6' }),
653
+ h('path', { d: 'M10 14L21 3' })))),
654
+ loggedIn
655
+ ? h('button', {
656
+ type: 'button', title: pageT('logout'), 'aria-label': pageT('logout'),
657
+ style: sLogoutArm[0]
658
+ ? Object.assign({}, buttonStyle, { height: 20, padding: '0 6px', fontSize: 11, color: '#d03050', borderColor: '#d03050', borderWidth: 1, borderStyle: 'solid' })
659
+ : { border: 'none', background: 'transparent', cursor: 'pointer', padding: 2, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', color: '#d03050' },
660
+ onClick: async function () {
661
+ if (!sLogoutArm[0]) { sLogoutArm[1](true); return }
662
+ await api('logout').catch(function () {})
663
+ sLogoutArm[1](false)
664
+ st[1](null)
665
+ tbStore.set({ loggedIn: false })
666
+ },
667
+ },
668
+ h('svg', { viewBox: '0 0 24 24', width: 13, height: 13, fill: 'none', stroke: 'currentColor', 'stroke-width': 2, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' },
669
+ h('path', { d: 'M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4' }),
670
+ h('path', { d: 'M16 17l5-5-5-5' }),
671
+ h('path', { d: 'M21 12H9' })))
672
+ : h('button', {
673
+ type: 'button',
674
+ style: { fontSize: 10, lineHeight: 1, padding: '3px 7px', borderRadius: 999, border: 'none', cursor: 'pointer', color: '#fff', background: '#ff4f1f', fontWeight: 600, letterSpacing: 0.3 },
675
+ onClick: function () { openGlobal('register') },
676
+ }, pageT('freeToken')),
677
+ loggedIn ? h('div', { style: { display: 'flex', alignItems: 'baseline', gap: 6, minWidth: 0, maxWidth: '100%', overflow: 'hidden' } },
678
+ h('span', { style: { fontSize: 12, fontWeight: 600, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', minWidth: 0 } },
649
679
  (data && data.user && (data.user.display_name || data.user.name)) || data.email || ''),
650
- h('span', { style: { fontSize: 11, color: 'var(--dsw-alias-label-secondary, #888)', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, data.email || ''),
680
+ h('span', { style: { fontSize: 11, color: 'var(--dsw-alias-label-secondary, #888)', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', flex: 1, minWidth: 0 } }, data.email || ''),
681
+ h('span', { style: { fontSize: 14, fontWeight: 700, fontVariantNumeric: 'tabular-nums', flexShrink: 0 } },
682
+ data && data.account && typeof data.account.balance_nano_usd === 'number'
683
+ ? '$' + (data.account.balance_nano_usd / 1e9).toFixed(2) : '—'),
651
684
  ) : null,
652
685
  loggedIn
653
- ? h('div', { style: { display: 'flex', alignItems: 'center', gap: 6 } },
654
- h('span', { style: { fontSize: 14, fontWeight: 700, fontVariantNumeric: 'tabular-nums', flex: 1 } },
655
- data && data.account && typeof data.account.balance_nano_usd === 'number'
656
- ? '$' + (data.account.balance_nano_usd / 1e9).toFixed(2) : '—'),
657
- h('button', { type: 'button', style: Object.assign({}, smallBtn, { borderColor: 'var(--dsw-alias-border-l2, #c9ccd1)' }),
658
- onClick: function () { openPanel('login') } }, pageT('depositManage')),
659
- h('button', { type: 'button', style: Object.assign({}, primaryButtonStyle, { height: 24, padding: '0 8px', fontSize: 12 }),
660
- onClick: function () { openPanel('login') } }, pageT('recharge')))
661
- : h('div', { style: { fontSize: 12, color: 'var(--dsw-alias-label-secondary, #888)' } }, pageT('balance') + ': —'),
686
+ ? h('div', { style: { display: 'flex', alignItems: 'center', gap: 6, minWidth: 0, maxWidth: '100%', overflow: 'hidden' } },
687
+ h('button', { type: 'button', style: Object.assign({}, smallBtn, { borderColor: 'var(--dsw-alias-border-l2, #c9ccd1)', whiteSpace: 'nowrap', flexShrink: 0 }),
688
+ onClick: function () { openSettingsPage() } }, pageT('depositManage')),
689
+ h('button', { type: 'button', style: Object.assign({}, primaryButtonStyle, { height: 24, padding: '0 8px', fontSize: 12, whiteSpace: 'nowrap', flexShrink: 0 }),
690
+ onClick: function () { openGlobal('login', { recharge: true }) } }, pageT('recharge')))
691
+ : null,
662
692
  loggedIn
663
- ? h('button', { type: 'button', style: Object.assign({}, primaryButtonStyle, { height: 28, justifyContent: 'center', width: '100%' }),
664
- onClick: function () { openPanel('login') } }, pageT('addFreeModels'))
665
- : h('button', { type: 'button', style: Object.assign({}, primaryButtonStyle, { height: 28, justifyContent: 'center', width: '100%' }),
666
- onClick: function () { openPanel('register') } }, pageT('register')),
667
- )
693
+ ? flash[0] ? h('button', { type: 'button', tabIndex: -1,
694
+ style: Object.assign({}, primaryButtonStyle, { height: 28, justifyContent: 'center', width: '100%', cursor: 'pointer', opacity: 0.9 }),
695
+ onClick: flash[0].models ? function () {
696
+ flash[1](null)
697
+ window.setTimeout(function () {
698
+ var triggers = document.querySelectorAll('button[aria-haspopup="menu"]')
699
+ for (var q = 0; q < triggers.length; q++) {
700
+ var lb = triggers[q].getAttribute('aria-label') || ''
701
+ if (/选择模型|Select model/.test(lb)) { triggers[q].click(); return }
702
+ }
703
+ }, 80)
704
+ } : undefined,
705
+ }, flash[0].text) : h('button', { type: 'button', style: Object.assign({}, primaryButtonStyle, { height: 28, justifyContent: 'center', width: '100%' }),
706
+ onClick: addModels }, pageT('addFreeModels'))
707
+ : null,
708
+ ))
709
+ }
710
+
711
+ /** 按邮箱域名猜 webmail 首页;未知域名兜底 mail.<domain>。 */
712
+ function mailHomeUrl(emailAddr) {
713
+ var domain = String(emailAddr || '').split('@')[1]
714
+ if (!domain) return null
715
+ var d = domain.toLowerCase()
716
+ var table = [
717
+ [/^(qq|foxmail)\.com$/, 'https://mail.qq.com'],
718
+ [/^163\.com$/, 'https://mail.163.com'],
719
+ [/^126\.com$/, 'https://mail.126.com'],
720
+ [/^gmail\.com$/, 'https://mail.google.com'],
721
+ [/^(outlook|hotmail|live)\.(com|cn)$/, 'https://outlook.live.com'],
722
+ [/^icloud\.com$/, 'https://www.icloud.com/mail'],
723
+ [/^sina\.com$/, 'https://mail.sina.com.cn'],
724
+ [/^sohu\.com$/, 'https://mail.sohu.com'],
725
+ [/^aliyun\.com$/, 'https://mail.aliyun.com'],
726
+ [/^yeah\.net$/, 'https://mail.yeah.net'],
727
+ [/^139\.com$/, 'https://mail.10086.cn'],
728
+ ]
729
+ for (var i = 0; i < table.length; i++) if (table[i][0].test(d)) return table[i][1]
730
+ return 'https://mail.' + d
668
731
  }
669
732
 
670
733
  /** shell.overlay 账户/注册弹窗:登录态展示余额与快捷操作,未登录展示表单。 */
@@ -678,7 +741,8 @@ window.__ModuleLoader__.load({
678
741
  var checkin = React.useState({ busy: false, done: false })
679
742
  var depTip = React.useState(null) // 说明板块:hover 哪个按钮就显示哪段说明('deposit'|'recharge'|'models')
680
743
  React.useEffect(function () {
681
- if (!snap.authOpen) logoutArm[1](false)
744
+ if (!snap.authOpen) { logoutArm[1](false); return }
745
+ rechargeOpen[1](Boolean(snap.startRecharge)) // 打开时同步「直达充值」标记(侧栏充值入口)
682
746
  }, [snap.authOpen])
683
747
  React.useEffect(function () {
684
748
  if (!snap.authOpen) return
@@ -701,16 +765,18 @@ window.__ModuleLoader__.load({
701
765
  var close = function () { tbStore.set({ authOpen: false }) }
702
766
  var data = st[0]
703
767
  var portal = (data && data.portalUrl) || 'https://bank.skillpie.cn'
704
- var anchor = snap.anchor || { right: window.innerWidth - 16, top: window.innerHeight - 80 }
705
- var panelWidth = 256
768
+ var anchor = snap.anchor || null // null = 全局居中弹窗(侧栏入口)
769
+ var panelWidth = 320
706
770
  return h('div', { style: { position: 'fixed', inset: 0, zIndex: 1999 } },
707
- h('div', { style: { position: 'absolute', inset: 0 }, onClick: close }),
771
+ h('div', { style: { position: 'absolute', inset: 0, background: 'rgba(0,0,0,0.35)', backdropFilter: 'blur(2px)', WebkitBackdropFilter: 'blur(2px)' }, onClick: close }),
708
772
  h('div', {
709
773
  style: {
710
774
  position: 'fixed', width: panelWidth, maxWidth: 'calc(100vw - 16px)',
711
- right: Math.max(8, window.innerWidth - anchor.right),
712
- bottom: window.innerHeight - anchor.top + 8,
713
- maxHeight: 420, overflow: 'auto',
775
+ left: anchor ? undefined : '50%', top: anchor ? undefined : '50%',
776
+ transform: anchor ? undefined : 'translate(-50%, -50%)',
777
+ right: anchor ? Math.max(8, window.innerWidth - anchor.right) : undefined,
778
+ bottom: anchor ? window.innerHeight - anchor.top + 8 : undefined,
779
+ maxHeight: 520, overflow: 'auto',
714
780
  /* 与模型选择器菜单(MenuSurface)完全一致的材质 */
715
781
  background: 'var(--dsw-menu-surface-fill)',
716
782
  backdropFilter: 'var(--dsw-menu-backdrop-filter)',
@@ -880,8 +946,9 @@ window.__ModuleLoader__.load({
880
946
  onAuthed: async function () {
881
947
  tbStore.set({ authOpen: false })
882
948
  try {
883
- if (!runtimeCtx.remote || !runtimeCtx.remote.settings) return
884
949
  var fresh = await api('status')
950
+ if (fresh) tbStore.set({ loggedIn: Boolean(fresh.loggedIn), portalUrl: fresh.portalUrl })
951
+ if (!runtimeCtx.remote || !runtimeCtx.remote.settings) return
885
952
  if (fresh && fresh.accessKey) {
886
953
  var models = await api('gateway-models', { accessKey: fresh.accessKey })
887
954
  var list = Array.isArray(models) ? models : (models.models || models.list || [])
@@ -1281,6 +1348,10 @@ window.__ModuleLoader__.load({
1281
1348
  try {
1282
1349
  await api('send-code', { email: email[0] })
1283
1350
  sent[1](true)
1351
+ var mailUrl = mailHomeUrl(email[0])
1352
+ if (mailUrl) api('open-external', { url: mailUrl }).catch(function (e) {
1353
+ tbStore.set({ notify: { kind: 'error', title: 'open ' + mailUrl + ' failed: ' + String(e.message || e), detail: '' } })
1354
+ })
1284
1355
  } catch (err) { error[1](String(err.message || err)) }
1285
1356
  busy[1](false)
1286
1357
  },
@@ -1400,14 +1471,13 @@ window.__ModuleLoader__.load({
1400
1471
  /* ── 模块②③④:选择器入口 + 弹层 + 设置页托管/状态 ── */
1401
1472
  runtimeCtx = ctx
1402
1473
 
1403
- // 输入框工具行、模型选择器左侧的紧凑入口按钮(弹账户/注册窗)
1404
- ctx.slots.inject('conversation.input.right', function () {
1474
+ // 侧栏底部长驻块(sidebar.footer.action 是 list 槽,渲染为追加项)
1475
+ ctx.slots.inject('sidebar.footer.action', function () {
1405
1476
  return ctx.slots.register({
1406
- name: 'conversation.input.right',
1407
- id: 'dsh-tokenbank-entry',
1408
- order: 0,
1477
+ name: 'sidebar.footer.action',
1478
+ id: 'dsh-tokenbank-card',
1409
1479
  locale: NS,
1410
- }, ComposerEntry)
1480
+ }, SidebarCard)
1411
1481
  })
1412
1482
 
1413
1483
  // 同一个槽只 inject 一次:多次 inject 会互相覆盖(之前 auth 注册被
package/lib/index.js CHANGED
@@ -337,6 +337,18 @@ async function dispatch(ctx, state, cfg, body) {
337
337
  const identity = await requireLive()
338
338
  return { ok: true, order: await tb.createRechargeOrder(baseUrl, identity.sessionToken, body.body ?? {}) }
339
339
  }
340
+ case 'open-external': {
341
+ // 用系统默认浏览器打开外部 URL(仅 http/https,给发送验证码后跳邮箱首页用)
342
+ const raw = String(body.url ?? '')
343
+ let u
344
+ try { u = new URL(raw) } catch { throw new Error('invalid url') }
345
+ if (u.protocol !== 'https:' && u.protocol !== 'http:') throw new Error('invalid url')
346
+ const opener = process.platform === 'darwin' ? 'open' : process.platform === 'linux' ? 'xdg-open' : 'cmd'
347
+ const args = process.platform === 'win32' ? ['/c', 'start', '', u.toString()] : [u.toString()]
348
+ const { execFile } = await import('node:child_process')
349
+ await new Promise((resolve, reject) => execFile(opener, args, (err) => (err ? reject(err) : resolve())))
350
+ return { ok: true }
351
+ }
340
352
  case 'recharge-open-pay': {
341
353
  // WebView 拦新窗口且拦 blob 跳转:落地 HTML 到临时文件,用系统默认浏览器打开
342
354
  const identity = await requireLive()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-tokenbank",
3
- "version": "0.1.61",
3
+ "version": "0.1.63",
4
4
  "description": "DSH 免费模型:一键注册 TokenBank 领免费 APIKey 送 $5 体验金,免费 GLM 模型直接进模型列表;自带余额查询、API Key 密钥托管(临时额度转永久)、每日签到、充值入口(支付宝),获取免费模型无需自建中转。致敬 dsh-our-free-model。官网 https://bank.skillpie.cn",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -48,4 +48,4 @@
48
48
  "publishConfig": {
49
49
  "access": "public"
50
50
  }
51
- }
51
+ }