@fish-under-sea/magpie-dragonapi 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  把 [DragonAPI](https://newapi.dragon3api.com/) 接进 magpie:模型广场、余额、用量,外加一个能按分组建钥匙的本地界面。
4
4
 
5
- > 当前版本 **0.1.3**。只做供应商插件 + 只读看板 + 密钥管理,不做「页面里点充值」。
5
+ > 当前版本 **0.1.4**。只做供应商插件 + 只读看板 + 密钥管理,不做「页面里点充值」。
6
6
 
7
7
  ## 它是什么
8
8
 
@@ -25,7 +25,7 @@
25
25
  | **命令行** | `magpie plugin add @fish-under-sea/magpie-dragonapi` |
26
26
 
27
27
  npm 拉不到时(内网、镜像抽风):到 [Releases](https://github.com/Fish-under-sea/magpie-dragonapi/releases) 下
28
- `fish-under-sea-magpie-dragonapi-0.1.3.tgz`,解包成目录,再 `magpie plugin add <那个目录>` ——
28
+ `fish-under-sea-magpie-dragonapi-0.1.4.tgz`,解包成目录,再 `magpie plugin add <那个目录>` ——
29
29
  `plugin add` 认 `path`,和从文件夹加插件是同一条路。
30
30
 
31
31
  装好后:Settings → Plugins 里出现 **DragonAPI** 行(带一张页面),供应商列表里出现 **DragonAPI**。
@@ -88,7 +88,7 @@ NearToken 那套「页面里点充值、点连接」依赖两个本站没有的
88
88
 
89
89
  | 页 | 内容 |
90
90
  | --- | --- |
91
- | **模型广场** | 122 个模型 / 195 行「分组 × 模型」,按分组、类别、关键词筛选。**纯展示,没有启用/停用开关**——本站做不到 NearToken 那种按分组启停,所以这里不给假开关 |
91
+ | **模型广场** | 左侧分组筛选(14 组,带倍率与数量)、右侧双列卡片网格:每张卡给出名称/厂商/说明/计费方式/输入·输出·缓存价/分组倍率/端点数。**纯展示,没有启用/停用开关**——本站做不到 NearToken 那种按分组启停,所以这里不给假开关,也不伪造健康度数据 |
92
92
  | **余额与用量** | 每个账号的余额与已用;按天/周/月汇总;逐条流水(模型、token、费用、吞吐、首字延迟) |
93
93
  | **密钥** | 列出现有钥匙(打码)、按分组建新钥匙、建完显示一次完整明文并提供复制、删除、查看完整明文 |
94
94
  | **访问令牌** | 填入/清除 PAT,说明需要哪些权限 |
package/index.mjs CHANGED
@@ -119,8 +119,16 @@ export default async function dragonapi(input, options) {
119
119
  void input
120
120
  const ctx = makeContext(input, options)
121
121
 
122
- // 先把站点参数与目录拉起来:冷启动时宿主很快就要模型表。
123
- await Promise.all([ctx.status.get({}), ctx.catalog.get({})])
122
+ // 预热目录与站点参数,但**绝不等它**。
123
+ //
124
+ // 宿主的插件加载有 20 秒上限(实测报错:the plugin's load didn't finish in 20s)。
125
+ // 之前这里 `await Promise.all([...])` 等站点,网络一慢就把整个插件的加载拖死,
126
+ // 表现是插件行直接标「没有加载成功」——而服务其实已经起来了,页面还能开,
127
+ // 只是宿主判定它加载失败。这两件事的因果很容易看错。
128
+ //
129
+ // 预热只是省掉第一次 models() 的等待,失败了也不影响任何功能:
130
+ // models() 自己会取,而且它本来就允许慢。
131
+ void Promise.allSettled([ctx.status.get({}), ctx.catalog.get({})])
124
132
 
125
133
  // 本地服务:页面的真正宿主。起不来也不能让供应商消失,所以失败只记一笔。
126
134
  let service = null
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fish-under-sea/magpie-dragonapi",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
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
@@ -549,6 +549,7 @@ function renderCatalog() {
549
549
  const groupMap = new Map(groups.map((g) => [g.id, g]));
550
550
 
551
551
  renderGroupChips(groups, models);
552
+ renderKindChips(models);
552
553
 
553
554
  const counts = catalogCounts(data);
554
555
  const query = filters.q.trim().toLowerCase();
@@ -556,31 +557,67 @@ function renderCatalog() {
556
557
  if (filters.group && m.group !== filters.group) return false;
557
558
  if (filters.kind && m.kind !== filters.kind) return false;
558
559
  if (!query) return true;
559
- const hay = [m.name, m.id, m.group, m.vendor, m.description, m.tags].filter(Boolean).join(' ').toLowerCase();
560
+ const hay = [m.name, m.id, m.group, m.vendor, m.description, m.tags, ...(m.endpoints || [])]
561
+ .filter(Boolean).join(' ').toLowerCase();
560
562
  return hay.includes(query);
561
563
  });
562
564
 
563
- meta.replaceChildren(
564
- h('span', { class: 'stat' }, h('b', { text: fmtInt(counts.models) }), '个模型'),
565
- h('span', { class: 'stat' }, h('b', { text: fmtInt(counts.rows) }), '行'),
566
- h('span', { class: 'stat' }, h('b', { text: fmtInt(counts.groups) }), '个分组'),
567
- h('span', { class: 'spacer' }),
568
- h('span', {
569
- class: 'muted',
570
- text: `当前显示 ${fmtInt(rows.length)} 行 · 来源 ${SOURCE_LABEL[data.source] || data.source || '未知'} · 更新于 ${fmtAt(data.at)}`,
571
- }),
572
- );
565
+ if (meta) {
566
+ meta.replaceChildren(
567
+ h('b', { text: fmtInt(rows.length) }),
568
+ h('span', { text: ` 个模型 · 全站 ${fmtInt(counts.models)} 个 / ${fmtInt(counts.rows)} 行 / ${fmtInt(counts.groups)} 个分组` }),
569
+ h('span', { class: 'spacer' }),
570
+ h('span', {
571
+ class: 'muted',
572
+ text: `来源 ${SOURCE_LABEL[data.source] || data.source || '未知'} · 更新于 ${fmtAt(data.at)}`,
573
+ }),
574
+ );
575
+ }
573
576
 
574
577
  if (!rows.length) {
575
578
  body.replaceChildren(h('div', { class: 'empty', text: '没有符合条件的模型。放宽分组或搜索词再试。' }));
576
579
  return;
577
580
  }
578
581
 
579
- const tableRows = rows.map((m) => catalogRow(m, groupMap));
580
- body.replaceChildren(tableEl(
581
- ['模型名', '分组', '倍率', '输入价', '输出价', '缓存价', '端点', '说明'],
582
- tableRows,
583
- ));
582
+ body.replaceChildren(...rows.map((m) => modelCard(m, groupMap)));
583
+ }
584
+
585
+ /** 类型筛选:分组之外的第二维,直接做成 chip,比下拉更省一次点击。 */
586
+ function renderKindChips(models) {
587
+ const host = $('#kind-chips');
588
+ if (!host) return;
589
+
590
+ const counts = new Map();
591
+ for (const m of models) {
592
+ const k = m.kind || 'chat';
593
+ counts.set(k, (counts.get(k) || 0) + 1);
594
+ }
595
+
596
+ const order = ['chat', 'image', 'video', 'audio', 'embedding'];
597
+ const kinds = [...counts.keys()].sort((a, b) => {
598
+ const ia = order.indexOf(a);
599
+ const ib = order.indexOf(b);
600
+ return (ia < 0 ? 99 : ia) - (ib < 0 ? 99 : ib);
601
+ });
602
+
603
+ const chips = [h('button', {
604
+ class: 'market-chip', type: 'button',
605
+ 'aria-pressed': String(filters.kind === ''),
606
+ onclick: () => { filters.kind = ''; renderCatalog(); },
607
+ }, '全部')];
608
+
609
+ for (const k of kinds) {
610
+ chips.push(h('button', {
611
+ class: 'market-chip', type: 'button',
612
+ 'aria-pressed': String(filters.kind === k),
613
+ onclick: () => {
614
+ filters.kind = filters.kind === k ? '' : k;
615
+ renderCatalog();
616
+ },
617
+ }, KIND_LABEL[k] || k, h('span', { class: 'chip-count', text: String(counts.get(k) || 0) })));
618
+ }
619
+
620
+ host.replaceChildren(...chips);
584
621
  }
585
622
 
586
623
  function renderGroupChips(groups, models) {
@@ -592,112 +629,136 @@ function renderGroupChips(groups, models) {
592
629
 
593
630
  const chips = [
594
631
  h('button', {
595
- class: 'chip', type: 'button',
632
+ class: 'market-chip', type: 'button',
596
633
  'aria-pressed': String(filters.group === ''),
597
634
  onclick: () => { filters.group = ''; renderCatalog(); },
598
635
  }, '全部', h('span', { class: 'chip-count', text: String(models.length) })),
599
636
  ];
600
637
 
601
638
  for (const g of groups) {
639
+ const ratio = g.ratio != null && Number(g.ratio) > 0 ? `×${fmtRatio(g.ratio)}` : '';
602
640
  chips.push(h('button', {
603
- class: 'chip', type: 'button',
641
+ class: 'market-chip', type: 'button',
604
642
  'aria-pressed': String(filters.group === g.id),
605
643
  title: [
606
- g.auto ? 'auto 分组:费率取决于实际命中的通道' : (g.ratio != null ? `分组倍率 ${g.ratio}×` : ''),
644
+ g.auto ? '该分组费率取决于实际命中的通道,站点没有给固定倍率' : (ratio ? `分组倍率 ${ratio}` : ''),
607
645
  g.note || '',
608
646
  ].filter(Boolean).join(' · '),
609
647
  onclick: () => {
610
648
  filters.group = filters.group === g.id ? '' : g.id;
611
649
  renderCatalog();
612
650
  },
613
- }, g.id, h('span', { class: 'chip-count', text: String(counts.get(g.id) || g.models || 0) })));
651
+ },
652
+ h('span', { class: 'chip-name', text: g.id }),
653
+ ratio ? h('span', { class: 'ratio', text: ratio }) : null,
654
+ h('span', { class: 'chip-count', text: String(counts.get(g.id) || g.models || 0) })));
614
655
  }
615
656
 
616
657
  host.replaceChildren(...chips);
617
658
  }
618
659
 
619
- function catalogRow(m, groupMap) {
660
+ /**
661
+ * 一张模型卡。
662
+ *
663
+ * 结构照 NearToken 的模型广场:名称/厂商 → 描述 → 计费方式 → 价格 → 分组
664
+ * → 端点 → 详情。等高靠三件事:外层 `.model-grid` 的 `align-items: stretch`、
665
+ * 卡片自身 `display:flex; flex-direction:column`、以及尾部 `margin-top:auto`
666
+ * 把「详情」按到底。任何一项缺失,同一行卡片就会参差。
667
+ *
668
+ * 刻意没照搬的两处:
669
+ * 1. NearToken 的卡片有「已启用/未启用」开关。我们按既定要求做成纯展示,
670
+ * 不给假开关——本站做不到按模型启停。
671
+ * 2. 它有实时健康度(延迟、吞吐、成功率条)。我们没有这些数据,
672
+ * 编一个出来比留白更糟,所以那个位置换成上下文长度与计费档位。
673
+ */
674
+ function modelCard(m, groupMap) {
620
675
  const info = groupMap.get(m.group);
621
676
  const isAuto = m.group === 'auto' || (info && info.auto === true);
622
677
  const pricing = m.pricing || {};
623
678
  const priceKind = pricing.kind || 'unknown';
624
679
  const ratio = m.groupRatio != null ? m.groupRatio : (info ? info.ratio : null);
680
+ const kind = m.kind || 'chat';
681
+ const endpoints = Array.isArray(m.endpoints) ? m.endpoints : [];
625
682
 
626
- const nameCell = h('td', { class: 'col-name' },
627
- h('button', {
628
- class: 'name-btn', type: 'button',
629
- title: '查看这个模型的计费表达式与分档',
630
- onclick: () => openModelDrawer(m),
631
- }, m.name || m.id || '未命名'),
632
- m.vendor ? h('span', { class: 'vendor', text: m.vendor }) : null,
633
- );
683
+ // 计费方式的说法要跟价格表一致:按次/按秒只给一列,别硬凑出「输出价 —」。
684
+ let billing = { cls: 'flat', label: '倍率计费' };
685
+ if (isAuto) billing = { cls: 'auto', label: '以实际命中通道为准' };
686
+ else if (priceKind === 'unknown') billing = { cls: 'unknown', label: '价格未知' };
687
+ else if (priceKind === 'percall') {
688
+ billing = { cls: 'call', label: /\/秒/.test(pricing.input || '') ? '按秒计费' : '按次计费' };
689
+ } else if (priceKind === 'tiered') billing = { cls: 'dyn', label: '分档计费' };
634
690
 
635
- const cells = [
636
- nameCell,
637
- { text: m.group || '—', cls: 'mono' },
638
- {
639
- text: ratio == null ? '—' : fmtRatio(ratio),
640
- cls: 'num',
641
- title: isAuto ? 'auto 分组没有固定倍率,按实际命中通道计费' : (ratio != null ? `分组倍率 ${ratio}×` : '站点没有给出倍率'),
642
- },
643
- ];
644
-
645
- // 价格三列:auto 与 unknown 都用一格跨三列,避免三格重复同一句提示。
691
+ let priceCells;
646
692
  if (isAuto) {
647
- cells.push({ node: h('td', {
648
- colspan: 3, class: 'muted price-merged',
649
- title: 'auto 分组没有固定倍率,实际费率取决于命中的通道',
650
- }, '以实际命中通道为准') });
693
+ priceCells = [h('div', { class: 'wide' }, h('span', { text: '单价' }), h('b', { class: 'muted', text: '—' }))];
651
694
  } else if (priceKind === 'unknown') {
652
- cells.push({ node: h('td', {
653
- colspan: 3, class: 'unknown price-merged',
654
- title: pricing.note || '站点没有给出可解析的价格',
655
- }, '价格未知') });
695
+ priceCells = [h('div', { class: 'wide' }, h('span', { text: '单价' }), h('b', { class: 'unknown', text: '价格未知' }))];
696
+ } else if (priceKind === 'percall') {
697
+ priceCells = [h('div', { class: 'wide' }, h('span', { text: '单价' }), h('b', { text: pricing.input || '—' }))];
656
698
  } else {
657
- cells.push(
658
- { text: pricing.input || '—', cls: 'num', title: pricing.note || null },
659
- { text: pricing.output || '—', cls: 'num', title: pricing.note || null },
660
- { text: pricing.cache || '—', cls: 'num', title: pricing.note || null },
661
- );
662
- }
699
+ priceCells = [
700
+ h('div', null, h('span', { text: '输入' }), h('b', { text: pricing.input || '—' })),
701
+ h('div', null, h('span', { text: '输出' }), h('b', { text: pricing.output || '—' })),
702
+ h('div', null, h('span', { text: '缓存读取' }), h('b', { text: pricing.cache || '—' })),
703
+ ];
704
+ }
705
+
706
+ const desc = m.description || '';
707
+ const titleBits = [
708
+ desc,
709
+ m.tags ? `标签:${m.tags}` : '',
710
+ pricing.note || '',
711
+ m.context ? `上下文 ${fmtInt(m.context)}` : '',
712
+ `模型 id:${m.id || ''}`,
713
+ ].filter(Boolean).join('\n');
714
+
715
+ return h('article', { class: `mc mc-${kind}`, title: titleBits },
716
+ h('div', { class: 'mc-head' },
717
+ h('div', { class: 'grow' },
718
+ h('button', {
719
+ class: 'mc-name', type: 'button',
720
+ title: '查看这个模型的计费表达式与分档',
721
+ onclick: () => openModelDrawer(m),
722
+ }, m.name || m.id || '未命名'),
723
+ m.vendor ? h('div', { class: 'mc-vendor', text: m.vendor }) : null),
724
+ h('span', { class: 'mc-kind', text: KIND_LABEL[kind] || kind })),
663
725
 
664
- const endpoints = Array.isArray(m.endpoints) ? m.endpoints : [];
665
- cells.push({
666
- node: h('td', null, endpoints.length
667
- ? h('span', { class: 'ep-list' }, endpoints.map((e) => h('span', { class: 'ep', text: e })))
668
- : h('span', { class: 'muted', text: '—' })),
669
- });
726
+ h('p', { class: desc ? 'mc-desc' : 'mc-desc is-empty', text: desc || '站点没有提供说明' }),
670
727
 
671
- const desc = m.description || m.tags || '';
672
- cells.push({
673
- text: desc || '—',
674
- cls: 'col-desc',
675
- title: [desc, m.tags ? `tags: ${m.tags}` : '', pricing.note || '', m.context ? `上下文 ${fmtInt(m.context)}` : '']
676
- .filter(Boolean).join('\n'),
677
- });
728
+ h('div', { class: `mc-billing ${billing.cls}`, text: billing.label }),
729
+
730
+ h('div', { class: priceKind === 'percall' || priceKind === 'unknown' || isAuto ? 'mc-prices one' : 'mc-prices' }, priceCells),
678
731
 
679
- return cells;
732
+ h('div', { class: 'mc-line' },
733
+ h('span', { class: 'k', text: '分组' }),
734
+ h('span', { class: 'mc-group', text: m.group || '—' }),
735
+ ratio != null && Number(ratio) > 0 ? h('span', { class: 'mc-ratio', text: `×${fmtRatio(ratio)}` }) : null),
736
+
737
+ h('div', { class: 'mc-line' },
738
+ h('span', { class: 'k', text: '端点' }),
739
+ endpoints.length
740
+ ? h('span', { class: 'mc-ends' }, endpoints.map((e) => h('span', { class: 'mc-end', text: e })))
741
+ : h('span', { class: 'muted', text: '—' })),
742
+
743
+ h('div', { class: 'mc-foot' },
744
+ h('button', { class: 'link', type: 'button', onclick: () => openModelDrawer(m) }, '详情')));
680
745
  }
681
746
 
682
747
  function bindCatalogControls() {
683
748
  const search = $('#catalog-search');
684
- const kind = $('#catalog-kind');
685
749
  const reset = $('#catalog-reset');
686
750
 
687
751
  search.addEventListener('input', () => {
688
752
  filters.q = search.value;
689
753
  if (cache.catalog) renderCatalog();
690
754
  });
691
- kind.addEventListener('change', () => {
692
- filters.kind = kind.value;
693
- if (cache.catalog) renderCatalog();
694
- });
755
+
756
+ // 类型筛选现在是 aside 里的 chip(renderKindChips),不再是一个 select。
695
757
  reset.addEventListener('click', () => {
696
758
  filters.group = '';
697
759
  filters.kind = '';
698
760
  filters.q = '';
699
761
  search.value = '';
700
- kind.value = '';
701
762
  if (cache.catalog) renderCatalog();
702
763
  search.focus();
703
764
  });
@@ -922,11 +983,25 @@ async function loadKeys(force = false) {
922
983
  try {
923
984
  const data = await api.keys();
924
985
  cache.keys = data;
986
+ cache.keysNeedPat = false;
925
987
  clearBanner('keys');
926
988
  renderKeys();
927
989
  return data;
928
990
  } catch (err) {
929
991
  const msg = describeError(err);
992
+ // 「还没设 PAT」是刚装好时的正常状态,不是错误。这里不该甩红条,
993
+ // 该直接告诉他下一步做什么——否则第一次打开就像插件坏了。
994
+ const needPat = err instanceof ApiError && err.status === 400 && /访问令牌/.test(msg);
995
+ if (needPat) {
996
+ cache.keysNeedPat = true;
997
+ cache.keysError = '';
998
+ clearBanner('keys');
999
+ renderKeys();
1000
+ renderKeyGroupOptions();
1001
+ return null;
1002
+ }
1003
+ cache.keysNeedPat = false;
1004
+ cache.keysError = msg;
930
1005
  banner('error', `读取钥匙列表失败:${msg}`, { key: 'keys' });
931
1006
  regionError($('#keys-body'), `钥匙列表没读到:${msg}`, () => loadKeys(true));
932
1007
  renderKeyGroupOptions();
@@ -988,6 +1063,17 @@ function renderKeys() {
988
1063
  }
989
1064
 
990
1065
  if (!cache.keys) {
1066
+ if (cache.keysNeedPat) {
1067
+ // 首次打开、还没设 PAT:给一条通往「访问令牌」页的路,而不是报错。
1068
+ box.replaceChildren(h('div', { class: 'empty guide' },
1069
+ h('p', { text: '还没设置访问令牌(PAT),所以读不到钥匙列表,也不能建钥匙。' }),
1070
+ h('p', { class: 'muted', text: 'PAT 只能在浏览器里登录控制台后创建:头像 → 安全 → 访问令牌,勾上 profile:read、api_key:read、api_key:write。' }),
1071
+ h('button', {
1072
+ class: 'btn primary', type: 'button',
1073
+ onclick: () => activateTab('pat', { focus: true }),
1074
+ }, '去设置访问令牌')));
1075
+ return;
1076
+ }
991
1077
  if (!loading.keys) regionLoading(box, '正在读取钥匙列表');
992
1078
  return;
993
1079
  }