@fish-under-sea/magpie-dragonapi 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/service.mjs CHANGED
@@ -222,6 +222,10 @@ export async function startService({ port = 3517, dir = '.', site, catalog, stat
222
222
  return { st, cat, cur, built, status: site_ }
223
223
  }
224
224
 
225
+ // 页面「查看」列的记忆放在服务进程里:沙箱 iframe 没有 localStorage,
226
+ // 刷新页面后还能从同一个服务把选择拿回来。
227
+ const uiPrefs = { logCols: null, keysCols: null }
228
+
225
229
  async function handler(req, res) {
226
230
  const url = new URL(req.url || '/', 'http://127.0.0.1')
227
231
  const path = url.pathname
@@ -276,6 +280,25 @@ export async function startService({ port = 3517, dir = '.', site, catalog, stat
276
280
  return
277
281
  }
278
282
 
283
+ if (path === '/api/prefs') {
284
+ if (req.method === 'GET') {
285
+ json(res, 200, { ok: true, ...uiPrefs })
286
+ return
287
+ }
288
+ if (req.method === 'PUT' || req.method === 'POST') {
289
+ let body = {}
290
+ try { body = (await readBody(req)) || {} } catch { throw new BadRequest('列偏好必须是 JSON') }
291
+ if (!body || typeof body !== 'object') throw new BadRequest('列偏好必须是 JSON 对象')
292
+ for (const field of ['logCols', 'keysCols']) {
293
+ if (Array.isArray(body[field])) {
294
+ uiPrefs[field] = body[field].map((v) => String(v)).filter(Boolean).slice(0, 32)
295
+ }
296
+ }
297
+ json(res, 200, { ok: true, ...uiPrefs })
298
+ return
299
+ }
300
+ }
301
+
279
302
  if (path === '/api/state' && req.method === 'GET') {
280
303
  const { st, cat, cur, built, status: site_ } = await stateOf()
281
304
  const [accounts, userResult] = await Promise.all([
package/lib/usage.mjs CHANGED
@@ -5,6 +5,7 @@
5
5
  // 也是 New API 部署,日志形状一致。
6
6
 
7
7
  import { text } from './site.mjs'
8
+ import { chatCoeffs } from './pricing.mjs'
8
9
 
9
10
  /** num 只接受真正的数字,字符串数字也认;其它一律 undefined。 */
10
11
  function num(v) {
@@ -65,13 +66,28 @@ function firstToken(row) {
65
66
  */
66
67
  function detailOf(other, quota) {
67
68
  if (!other || typeof other !== 'object') return ''
68
- const tier = pick(other?.tier, other?.billing_tier, other?.group)
69
- const inPrice = num(other?.model_ratio) ?? num(other?.input_price)
70
- const outPrice = num(other?.completion_ratio) ?? num(other?.output_price)
69
+ const tier = pick(other?.matched_tier, other?.tier, other?.billing_tier)
70
+ // 表达式计费(tiered_expr):价格在 expr_b64 里,model_ratio / completion_ratio
71
+ // 是 0 元占位,照它们拼只会得到「0 / 0」。解开表达式取 p / c 系数才是官网
72
+ // 详情列那串「$2 / $10/M」。
73
+ let input = num(other?.input_price) ?? num(other?.model_price_input)
74
+ let output = num(other?.output_price) ?? num(other?.model_price_output)
75
+ if ((input === undefined || output === undefined) && typeof other?.expr_b64 === 'string' && other.expr_b64) {
76
+ try {
77
+ const expr = Buffer.from(other.expr_b64, 'base64').toString('utf8')
78
+ const c = chatCoeffs(expr)
79
+ if (c) {
80
+ if (input === undefined) input = c.input
81
+ if (output === undefined) output = c.output
82
+ }
83
+ } catch { /* 表达式坏了就退到 tier */ }
84
+ }
71
85
  const parts = []
72
- if (tier) parts.push(tier)
73
- if (inPrice !== undefined || outPrice !== undefined) {
74
- parts.push(`${inPrice ?? '—'} / ${outPrice ?? '—'}`)
86
+ if (tier) parts.push(String(tier))
87
+ if (Number.isFinite(input) || Number.isFinite(output)) {
88
+ const inText = Number.isFinite(input) ? `$${input}` : '—'
89
+ const outText = Number.isFinite(output) ? `$${output}` : '—'
90
+ parts.push(`${inText} / ${outText}/M`)
75
91
  }
76
92
  if (!parts.length && quota !== undefined) return ''
77
93
  return parts.join(' · ')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fish-under-sea/magpie-dragonapi",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "DragonAPI for magpie: a provider plugin whose models, groups and prices come from the site's public /api/pricing, with balance and per-call usage from an sk- key, plus a local page that lists the market (read-only), shows usage and can mint new keys for a group.",
5
5
  "type": "module",
6
6
  "main": "index.mjs",
package/ui/app.js CHANGED
@@ -266,6 +266,8 @@ const api = {
266
266
  revealKey: (id) => request(`/api/keys/${encodeURIComponent(id)}/reveal`, { method: 'POST' }),
267
267
  bindInfo: () => request('/api/bind-info'),
268
268
  health: () => request('/api/health'),
269
+ prefs: () => request('/api/prefs'),
270
+ savePrefs: (body) => request('/api/prefs', { method: 'PUT', body }),
269
271
  };
270
272
 
271
273
  // ── 提示条:所有失败都必须在界面上说话 ────────────────────
@@ -1865,7 +1867,7 @@ const LOG_COLS = [
1865
1867
  const logFilter = { model: '', group: '', kind: '', tokenName: '', requestId: '', upRequestId: '' };
1866
1868
  const logView = {
1867
1869
  page: 1, pageSize: 20, more: false, private: false, colsOpen: false,
1868
- cols: new Set(['time', 'key', 'model', 'flow', 'tokens', 'cost', 'duration', 'detail']),
1870
+ cols: new Set(['time', 'key', 'model', 'flow', 'tokens', 'cost', 'duration']),
1869
1871
  };
1870
1872
 
1871
1873
  /** secs 秒数带单位:4.5 → 4.5s。 */
@@ -2010,6 +2012,7 @@ function renderLogs() {
2010
2012
  ev.stopPropagation();
2011
2013
  if (logView.cols.has(c.id)) { if (logView.cols.size > 1) logView.cols.delete(c.id); }
2012
2014
  else logView.cols.add(c.id);
2015
+ saveColPrefs();
2013
2016
  renderLogs();
2014
2017
  },
2015
2018
  }, c.label))),
@@ -2943,6 +2946,33 @@ const KEY_COLS = [
2943
2946
  ];
2944
2947
  const keysView = new Set(['name', 'status', 'key', 'group', 'time', 'expire']);
2945
2948
 
2949
+ // 「查看」列的偏好记在服务进程里:沙箱 iframe 没有 localStorage,
2950
+ // 只存浏览器刷新就丢;服务活着就能跨刷新恢复。
2951
+ const colPrefs = { loaded: false, timer: 0 };
2952
+ function applyColPrefs(p) {
2953
+ const logIds = new Set(LOG_COLS.map((c) => c.id));
2954
+ const keyIds = new Set(KEY_COLS.map((c) => c.id));
2955
+ if (Array.isArray(p?.logCols)) {
2956
+ const saved = p.logCols.filter((id) => logIds.has(id));
2957
+ if (saved.length) logView.cols = new Set(saved);
2958
+ }
2959
+ if (Array.isArray(p?.keysCols)) {
2960
+ const saved = p.keysCols.filter((id) => keyIds.has(id));
2961
+ if (saved.length) keysView.clear(), saved.forEach((id) => keysView.add(id));
2962
+ }
2963
+ }
2964
+ async function loadColPrefs() {
2965
+ if (colPrefs.loaded) return;
2966
+ try { applyColPrefs(await api.prefs()); } catch { /* 服务不支持就用默认列 */ }
2967
+ colPrefs.loaded = true;
2968
+ }
2969
+ function saveColPrefs() {
2970
+ clearTimeout(colPrefs.timer);
2971
+ colPrefs.timer = setTimeout(() => {
2972
+ api.savePrefs({ logCols: [...logView.cols], keysCols: [...keysView] }).catch(() => {});
2973
+ }, 150);
2974
+ }
2975
+
2946
2976
  const keysFilter = { name: '', key: '', status: '' };
2947
2977
  const keysSel = new Set();
2948
2978
 
@@ -3125,7 +3155,11 @@ function renderKeys() {
3125
3155
  const cells = {
3126
3156
  name: h('td', { class: 'keys-name', text: k.name || '未命名' }),
3127
3157
  status: h('td', null, h('span', { class: `keys-status ${status.cls.includes('ok') ? 'on' : 'off'}`, text: status.label })),
3128
- key: h('td', null, h('span', { class: 'keys-key' }, k.masked || '—', copyButton(k.masked || '', '⧉'))),
3158
+ key: h('td', null, h('span', { class: 'keys-key' }, k.masked || '—',
3159
+ h('button', {
3160
+ class: 'btn ghost', type: 'button', title: '复制完整密钥',
3161
+ onclick: () => copyKeySecret(k),
3162
+ }, '⧉'))),
3129
3163
  quota: h('td', { class: 'keys-quota', title: Number.isFinite(k.used) ? `已用 ${fmtInt(k.used)} quota` : null },
3130
3164
  k.unlimited ? '无限制' : moneyRaw(k.remain)),
3131
3165
  group: h('td', null, k.group
@@ -3175,6 +3209,7 @@ function renderKeys() {
3175
3209
  ev.stopPropagation();
3176
3210
  if (keysView.has(c.id)) { if (keysView.size > 1) keysView.delete(c.id); }
3177
3211
  else keysView.add(c.id);
3212
+ saveColPrefs();
3178
3213
  renderKeys();
3179
3214
  },
3180
3215
  }, h('span', { class: 'keys-check-mark', text: keysView.has(c.id) ? '✓' : '' }), c.label)));
@@ -4278,6 +4313,8 @@ async function boot() {
4278
4313
  bindPatControls();
4279
4314
  bindDrawer();
4280
4315
 
4316
+ await loadColPrefs();
4317
+
4281
4318
  const params = new URLSearchParams(location.search);
4282
4319
  const wanted = params.get('tab');
4283
4320
  // 密钥已并入账户页;旧的 ?tab=keys 链接仍要能打开(映射到 wallet),
package/ui/style.css CHANGED
@@ -115,7 +115,7 @@ a { color: var(--accent); }
115
115
  .app {
116
116
  max-width: 1400px;
117
117
  margin: 0 auto;
118
- padding: 18px 22px 24px;
118
+ padding: 18px 22px 8px;
119
119
  /* 外壳定高不滚:顶部(页头/标签/提示)与页脚钉住,主内容区自己滚 */
120
120
  height: 100%;
121
121
  display: flex;
@@ -3114,7 +3114,7 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3114
3114
  .logs-tools .btn { border-radius: 999px; }
3115
3115
 
3116
3116
  /* 钱包左栏的账户主体不再自己滚:内容已居中放下,滚动条只在密钥卡里 */
3117
- #panel-wallet .wallet-balance #account-body { overflow: hidden; }
3117
+ #panel-wallet .wallet-balance #account-body { overflow: hidden; }
3118
3118
 
3119
3119
  /* ── API 密钥:照官网表格重做 ─────────────────────────────── */
3120
3120
  .keys-title-actions { display: flex; align-items: center; gap: 8px; margin-left: auto; }
@@ -3160,7 +3160,7 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3160
3160
  border-radius: 999px; border: 1px solid transparent; background: transparent; color: var(--muted); cursor: pointer;
3161
3161
  }
3162
3162
  .keys-act:hover { background: var(--card-hover); color: var(--fg); }
3163
- .keys-act.danger:hover { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, transparent); }
3163
+ .keys-act.danger:hover { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, transparent); }
3164
3164
 
3165
3165
  /* ── 日志头部:直接照 NearToken in Magpie 的 .lg-* 设计(覆盖上面的胶囊尝试) ── */
3166
3166
  .logs-toolbar { padding: 8px; gap: 8px; border: 1px solid var(--line); border-radius: 12px; background: rgba(255,255,255,.02); }
@@ -3186,7 +3186,7 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3186
3186
  .logs-tools .btn { height: 34px; padding: 0 8px; border-color: transparent; background: transparent; color: var(--muted); border-radius: 8px; }
3187
3187
  .logs-tools .btn:hover { background: rgba(255,255,255,.06); color: var(--fg); }
3188
3188
  .logs-tools .btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; padding: 0 14px; }
3189
- .logs-tools a.btn { text-decoration: none; display: inline-flex; align-items: center; }
3189
+ .logs-tools a.btn { text-decoration: none; display: inline-flex; align-items: center; }
3190
3190
 
3191
3191
  /* API 密钥:查看列菜单 + 更小的复制图标 */
3192
3192
  .keys-view-menu { margin-left: auto; }
@@ -3196,11 +3196,11 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3196
3196
  .keys-key .btn {
3197
3197
  width: 22px; height: 22px; min-height: 0; padding: 0; font-size: 12px;
3198
3198
  line-height: 1; border-radius: 6px; display: inline-flex; align-items: center; justify-content: center;
3199
- }
3199
+ }
3200
3200
 
3201
3201
  /* 密钥「查看」菜单:选中用勾选,不用整行高亮 */
3202
3202
  #keys-view-pop [role="option"][aria-selected="true"] { color: var(--fg); background: transparent; }
3203
- #keys-view-pop [role="option"]:hover { background: var(--card-hover); }
3203
+ #keys-view-pop [role="option"]:hover { background: var(--card-hover); }
3204
3204
 
3205
3205
  /* 日志头部:胶囊化 + 排布(最新要求);表格不许左右滚 */
3206
3206
  .logs-filters { display: flex; flex-wrap: wrap; gap: 8px; }
@@ -3219,7 +3219,7 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3219
3219
  .logs-body { overflow-x: hidden; }
3220
3220
  .lg-table { min-width: 0; width: 100%; }
3221
3221
  .lg-row { grid-template-columns: var(--lg-cols); gap: 0 10px; }
3222
- .lg-row > * { min-width: 0; }
3222
+ .lg-row > * { min-width: 0; }
3223
3223
 
3224
3224
  /* 半透明表头 / 弹层:加不透明底 + 背景模糊,避免滚动内容透出来 */
3225
3225
  .lg-head,
@@ -3239,7 +3239,7 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3239
3239
  .keys-titlebar {
3240
3240
  backdrop-filter: blur(8px);
3241
3241
  -webkit-backdrop-filter: blur(8px);
3242
- }
3242
+ }
3243
3243
 
3244
3244
  /* 表头再压实一点:接近不透明 + 模糊,滚动行完全不透出 */
3245
3245
  .lg-head,
@@ -3247,7 +3247,7 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3247
3247
  background: color-mix(in srgb, var(--bg) 98%, transparent);
3248
3248
  backdrop-filter: blur(12px);
3249
3249
  -webkit-backdrop-filter: blur(12px);
3250
- }
3250
+ }
3251
3251
 
3252
3252
  /* 日志顶部(照站点源码的 primaryFilters 结构):一行、扁平胶囊、32px */
3253
3253
  .logs-toolbar {
@@ -3278,7 +3278,7 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3278
3278
  border: 1px solid transparent; background: transparent; color: var(--muted);
3279
3279
  display: inline-flex; align-items: center; gap: 4px;
3280
3280
  }
3281
- .logs-expand:hover { background: rgba(255,255,255,.06); color: var(--fg); }
3281
+ .logs-expand:hover { background: rgba(255,255,255,.06); color: var(--fg); }
3282
3282
 
3283
3283
  /* 收紧日志顶部上下留白 */
3284
3284
  .logs-toolbar { margin-bottom: 0; }
@@ -3305,10 +3305,10 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3305
3305
  .keys-filterbar .btn,
3306
3306
  .keys-filterbar .sel-trigger { background: var(--card); border: 1px solid var(--line); padding: 0 14px; }
3307
3307
  #keys-api-addr { background: transparent; border: 1px solid var(--line); padding: 0 14px; }
3308
- #key-create-open { padding: 0 16px; }
3308
+ #key-create-open { padding: 0 16px; }
3309
3309
 
3310
3310
  .logs-card { gap: 0; }
3311
-
3311
+
3312
3312
 
3313
3313
  /* 弹层:完全不透明并压住表格,避免内容透出 */
3314
3314
  .menu-pop, .sel-pop {
@@ -3334,7 +3334,7 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3334
3334
  background: var(--accent) !important;
3335
3335
  border-color: var(--accent) !important;
3336
3336
  color: #fff;
3337
- }
3337
+ }
3338
3338
 
3339
3339
  /* 密钥复制按钮:只留图标,去掉框选 */
3340
3340
  .keys-key .btn {
@@ -3342,7 +3342,7 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3342
3342
  border: 0 !important; background: transparent !important; box-shadow: none;
3343
3343
  color: var(--muted); border-radius: 0;
3344
3344
  }
3345
- .keys-key .btn:hover { color: var(--fg); background: transparent !important; }
3345
+ .keys-key .btn:hover { color: var(--fg); background: transparent !important; }
3346
3346
 
3347
3347
  /* 密钥行操作:禁用/编辑/更多 */
3348
3348
  .keys-actions { display: inline-flex; align-items: center; gap: 2px; }
@@ -3358,10 +3358,10 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3358
3358
  }
3359
3359
  .keys-more-pop [role="option"]:hover, .keys-more-del:hover { background: var(--card-hover); }
3360
3360
  .kmi-ico { color: var(--muted); }
3361
- .keys-more-del { color: var(--bad); }
3361
+ .keys-more-del { color: var(--bad); }
3362
3362
 
3363
3363
  /* 密钥行「更多」菜单向上弹,避免被卡片底边裁掉 */
3364
- .keys-more-pop { top: auto; bottom: calc(100% + 6px); }
3364
+ .keys-more-pop { top: auto; bottom: calc(100% + 6px); }
3365
3365
 
3366
3366
  /* 展开按钮:胶囊;展开行三个输入也胶囊扁平 */
3367
3367
  .logs-expand { background: rgba(255,255,255,.05); color: var(--fg); border: 1px solid transparent; }
@@ -3375,7 +3375,7 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3375
3375
  .logs-filters-adv .logs-input {
3376
3376
  flex: 1 1 0; height: 32px; border-radius: 999px; border: 1px solid transparent;
3377
3377
  background: rgba(255,255,255,.05); padding: 0 12px; font-size: 11.5px;
3378
- }
3378
+ }
3379
3379
 
3380
3380
  /* 钱包标题即入口:无框,只加箭头;金额放大填满左栏 */
3381
3381
  .wallet-title { margin: 0; }
@@ -3391,12 +3391,12 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3391
3391
  .wallet-balance .acct-hero { align-items: center; }
3392
3392
  .wallet-balance .acct-num { font-size: 46px; line-height: 1.02; }
3393
3393
  .wallet-balance .acct-cur { font-size: 24px; }
3394
- .wallet-jump { display: none; }
3394
+ .wallet-jump { display: none; }
3395
3395
 
3396
3396
  /* 每页行数下拉向上弹,避免被卡片底边裁掉 */
3397
3397
  .lg-pager .sel-pop { top: auto; bottom: calc(100% + 6px); }
3398
3398
  /* 日志表头不跟随滚动(不再 sticky) */
3399
- .lg-head { position: static; backdrop-filter: none; -webkit-backdrop-filter: none; }
3399
+ .lg-head { position: static; backdrop-filter: none; -webkit-backdrop-filter: none; }
3400
3400
 
3401
3401
  /* 日志:筛选区(绿框)+ 表格区(红框)各自成块,和官网一致 */
3402
3402
  .logs-head-box {
@@ -3414,7 +3414,7 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3414
3414
  .logs-body .lg-table { border: 0; border-radius: 0; }
3415
3415
  .lg-head { background: color-mix(in srgb, var(--card) 75%, transparent); }
3416
3416
  .lg-row { padding: 10px 12px; }
3417
- .logs-pager { margin-top: 10px; }
3417
+ .logs-pager { margin-top: 10px; }
3418
3418
 
3419
3419
  /* 钱包左栏:左对齐,内容从顶部排,sub-plus 贴底与右侧卡片齐 */
3420
3420
  .wallet-balance #account-body {
@@ -3423,24 +3423,24 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3423
3423
  .wallet-balance .acct-card { height: 100%; justify-content: flex-start; align-items: stretch; gap: 12px; text-align: left; }
3424
3424
  .wallet-balance .acct-hero { justify-content: flex-start; }
3425
3425
  .wallet-balance .acct-money { justify-content: flex-start; }
3426
- .wallet-balance .acct-foot { margin-top: auto; }
3426
+ .wallet-balance .acct-foot { margin-top: auto; }
3427
3427
 
3428
3428
  /* 推荐计划:同步时间下移,与左侧副标题小字同一行 */
3429
- .rf-sync { align-self: flex-end; padding-bottom: 1px; }
3429
+ .rf-sync { align-self: flex-end; padding-bottom: 1px; }
3430
3430
 
3431
3431
  /* 钱包左栏留白:标题下留空隙,数字下移填掉底部空白 */
3432
3432
  .wallet-balance #account-body { margin-top: 10px; }
3433
- .wallet-balance .acct-hero { margin-top: 12px; }
3433
+ .wallet-balance .acct-hero { margin-top: 12px; }
3434
3434
 
3435
3435
  /* 展开后三行之间留足空隙,不要挤在一起 */
3436
3436
  .logs-head-box .logs-filters-adv { margin-top: 14px; }
3437
- .logs-head-box .logs-chips-bar { margin-top: 16px; }
3437
+ .logs-head-box .logs-chips-bar { margin-top: 16px; }
3438
3438
 
3439
3439
  /* 展开区用 flex 纵向排列,统一 12px 间距,避免行间重叠 */
3440
3440
  .logs-head-box { display: flex; flex-direction: column; gap: 12px; }
3441
3441
  .logs-head-box .logs-toolbar,
3442
3442
  .logs-head-box .logs-filters-adv,
3443
- .logs-head-box .logs-chips-bar { margin: 0; }
3443
+ .logs-head-box .logs-chips-bar { margin: 0; }
3444
3444
 
3445
3445
  /* 访问令牌:顶部两卡完整显示,不在卡片内部滚 */
3446
3446
  #panel-pat { overflow-y: auto; }
@@ -3458,4 +3458,4 @@ input.keys-input:focus-visible, select.keys-select:focus-visible {
3458
3458
  .banners .banner {
3459
3459
  pointer-events: auto; background: var(--card);
3460
3460
  box-shadow: 0 10px 30px rgba(0, 0, 0, .45);
3461
- }
3461
+ }