koishi-plugin-newapi-status 0.0.0-stage → 0.2.0

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 (50) hide show
  1. package/LICENSE +31 -0
  2. package/lib/api/index.d.ts +10 -0
  3. package/lib/api/index.js +26 -0
  4. package/lib/api/models.d.ts +9 -0
  5. package/lib/api/models.js +20 -0
  6. package/lib/api/network.d.ts +20 -0
  7. package/lib/api/network.js +42 -0
  8. package/lib/api/notice.d.ts +8 -0
  9. package/lib/api/notice.js +17 -0
  10. package/lib/api/perf.d.ts +31 -0
  11. package/lib/api/perf.js +52 -0
  12. package/lib/api/pricing.d.ts +9 -0
  13. package/lib/api/pricing.js +24 -0
  14. package/lib/api/ratio.d.ts +10 -0
  15. package/lib/api/ratio.js +29 -0
  16. package/lib/api/relay.d.ts +43 -0
  17. package/lib/api/relay.js +178 -0
  18. package/lib/api/shared.d.ts +60 -0
  19. package/lib/api/shared.js +123 -0
  20. package/lib/api/status.d.ts +12 -0
  21. package/lib/api/status.js +13 -0
  22. package/lib/api/uptime.d.ts +9 -0
  23. package/lib/api/uptime.js +18 -0
  24. package/lib/config.d.ts +59 -0
  25. package/lib/config.js +112 -0
  26. package/lib/format.d.ts +76 -0
  27. package/lib/format.js +242 -0
  28. package/lib/index.d.ts +11 -0
  29. package/lib/index.js +567 -0
  30. package/lib/log.d.ts +39 -0
  31. package/lib/log.js +39 -0
  32. package/lib/monitor.d.ts +39 -0
  33. package/lib/monitor.js +186 -0
  34. package/lib/render/ChannelCard.d.ts +6 -0
  35. package/lib/render/ChannelCard.js +12 -0
  36. package/lib/render/StatusCard.d.ts +6 -0
  37. package/lib/render/StatusCard.js +18 -0
  38. package/lib/render/index.d.ts +12 -0
  39. package/lib/render/index.js +71 -0
  40. package/lib/render/styles.d.ts +23 -0
  41. package/lib/render/styles.js +192 -0
  42. package/lib/text.d.ts +10 -0
  43. package/lib/text.js +107 -0
  44. package/lib/types.d.ts +201 -0
  45. package/lib/types.js +6 -0
  46. package/lib/view.d.ts +149 -0
  47. package/lib/view.js +360 -0
  48. package/package.json +73 -4
  49. package/readme.md +225 -0
  50. package/README.md +0 -3
package/lib/text.js ADDED
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.renderCardText = renderCardText;
4
+ exports.blocksToText = blocksToText;
5
+ exports.renderListText = renderListText;
6
+ exports.renderHelp = renderHelp;
7
+ exports.renderError = renderError;
8
+ /**
9
+ * 没有 puppeteer 时的纯文本输出。
10
+ * 与卡片共用同一份 CardView / ListView,信息不缩水,只换排版。
11
+ */
12
+ const DOT = {
13
+ ok: '●',
14
+ warn: '◐',
15
+ fail: '✕',
16
+ unknown: '○',
17
+ };
18
+ const BLOCK = {
19
+ ok: '🟩',
20
+ warn: '🟨',
21
+ fail: '🟥',
22
+ unknown: '⬜',
23
+ };
24
+ function line(char = '─', length = 18) {
25
+ return char.repeat(length);
26
+ }
27
+ function renderCardText(card) {
28
+ const out = [];
29
+ out.push(`【${card.siteName}】${card.sub}`);
30
+ if (card.badges.length) {
31
+ out.push(card.badges.map(badge => `[${badge.text}]`).join(' '));
32
+ }
33
+ out.push(line());
34
+ for (const metric of card.metrics) {
35
+ const level = metric.level && metric.level !== 'ok' ? ` ${DOT[metric.level]}` : '';
36
+ out.push(`${metric.key}:${metric.value}${level}${metric.sub ? `(${metric.sub})` : ''}`);
37
+ }
38
+ for (const section of card.sections) {
39
+ out.push('');
40
+ out.push(`· ${section.title}${section.note ? ` · ${section.note}` : ''}`);
41
+ if (section.rows?.length) {
42
+ for (const row of section.rows) {
43
+ const level = row.level ? `${DOT[row.level]} ` : '';
44
+ out.push(` ${level}${row.name} → ${row.value}`);
45
+ }
46
+ }
47
+ if (section.blocks?.length) {
48
+ out.push(` ${blocksToText(section.blocks)}`);
49
+ if (section.axis?.length)
50
+ out.push(` ${section.axis.join(' → ')}`);
51
+ }
52
+ if (section.chips?.length) {
53
+ for (const chip of section.chips)
54
+ out.push(` - ${chip}`);
55
+ }
56
+ if (!section.rows?.length && !section.blocks?.length && !section.chips?.length && section.empty) {
57
+ out.push(` ${section.empty}`);
58
+ }
59
+ }
60
+ if (card.notice)
61
+ out.push(`\n公告:${card.notice.body}`);
62
+ out.push(`\n${card.footerLeft} · ${card.footerRight}`);
63
+ return out.join('\n');
64
+ }
65
+ function blocksToText(blocks) {
66
+ return blocks.map(block => BLOCK[block.level] ?? BLOCK.unknown).join('');
67
+ }
68
+ function renderListText(list) {
69
+ const out = [];
70
+ out.push(`【${list.title}】${list.subtitle ? ` ${list.subtitle}` : ''}`);
71
+ if (list.note)
72
+ out.push(`${list.note}`);
73
+ out.push(line());
74
+ if (!list.rows.length) {
75
+ out.push(list.empty || '暂无数据。');
76
+ }
77
+ else {
78
+ const limit = list.limit && list.limit > 0 ? list.limit : 24;
79
+ const rows = list.rows.slice(0, limit);
80
+ for (const row of rows) {
81
+ const level = row.level ? `${DOT[row.level]} ` : '';
82
+ const tag = row.tag ? ` [${row.tag}]` : '';
83
+ if (row.wrap) {
84
+ // 长文本(公告正文):名字单独一行,正文换行缩进,避免一坨超长单行
85
+ out.push(`${level}${row.name}`);
86
+ out.push(` ${row.value || ''}`);
87
+ continue;
88
+ }
89
+ out.push(`${level}${row.name}${tag}${row.value ? ` ${row.value}` : ''}`);
90
+ }
91
+ if (list.rows.length > rows.length) {
92
+ out.push(`… 还有 ${list.rows.length - rows.length} 条未显示,用关键词筛选`);
93
+ }
94
+ }
95
+ out.push(`\n${list.footerLeft} · ${list.footerRight}`);
96
+ return out.join('\n');
97
+ }
98
+ /** 指令列表(/newapi help)用文本表格,不渲染卡片。 */
99
+ function renderHelp(prefix, items) {
100
+ const out = [`New API 状态查询 · 指令前缀 ${prefix}`, line()];
101
+ for (const item of items)
102
+ out.push(`${item.usage}\n ${item.desc}`);
103
+ return out.join('\n');
104
+ }
105
+ function renderError(title, detail) {
106
+ return [`✕ ${title}`, detail].filter(Boolean).join('\n');
107
+ }
package/lib/types.d.ts ADDED
@@ -0,0 +1,201 @@
1
+ /**
2
+ * New API 接口返回结构。
3
+ * 全部字段均来自 QuantumNous/new-api 源码(controller/、pkg/perf_metrics/types.go),未做猜测。
4
+ */
5
+ export interface ApiEnvelope<T> {
6
+ success: boolean;
7
+ message?: string;
8
+ data: T;
9
+ }
10
+ /** GET /api/status */
11
+ export interface SiteStatus {
12
+ version: string;
13
+ /** unix 秒 */
14
+ start_time: number;
15
+ system_name?: string;
16
+ logo?: string;
17
+ server_address?: string;
18
+ docs_link?: string;
19
+ quota_per_unit?: number;
20
+ display_in_currency?: boolean;
21
+ quota_display_type?: string;
22
+ usd_exchange_rate?: number;
23
+ price?: number;
24
+ api_info_enabled?: boolean;
25
+ uptime_kuma_enabled?: boolean;
26
+ announcements_enabled?: boolean;
27
+ faq_enabled?: boolean;
28
+ register_enabled?: boolean;
29
+ self_use_mode_enabled?: boolean;
30
+ demo_site_enabled?: boolean;
31
+ /**
32
+ * 公告列表,仅在 announcements_enabled 时下发。
33
+ * 老版本是 JSON 数组字符串(console_setting.Announcements),
34
+ * v1.0.0-rc 之后直接返回对象数组 { content, extra, id, publishDate, type }。
35
+ */
36
+ announcements?: string | SiteAnnouncement[];
37
+ /** 站点线路(只在 api_info_enabled 时下发),用于 /newapi network */
38
+ api_info?: SiteRoute[];
39
+ setup?: boolean;
40
+ }
41
+ /** /api/status 的 data.api_info 元素 */
42
+ export interface SiteRoute {
43
+ /** 仅用于展示的分类色(red / yellow / green ...),不是健康状态 */
44
+ color?: string;
45
+ description?: string;
46
+ id?: number;
47
+ /** 线路名,例如 China / Global */
48
+ route: string;
49
+ /** 该线路的接入地址 */
50
+ url: string;
51
+ }
52
+ /** /api/status 里 announcements 的对象形态(v1.0.0-rc+) */
53
+ export interface SiteAnnouncement {
54
+ content: string;
55
+ extra?: string;
56
+ id?: number;
57
+ publishDate?: string;
58
+ type?: string;
59
+ }
60
+ /** GET /api/pricing */
61
+ export interface ModelPricing {
62
+ model_name: string;
63
+ description?: string;
64
+ icon?: string;
65
+ tags?: string[];
66
+ vendor_id?: number;
67
+ quota_type: number;
68
+ model_ratio: number;
69
+ model_price: number;
70
+ owner_by?: string;
71
+ completion_ratio: number;
72
+ cache_ratio?: number;
73
+ create_cache_ratio?: number;
74
+ image_ratio?: number;
75
+ audio_ratio?: number;
76
+ audio_completion_ratio?: number;
77
+ enable_groups: string[];
78
+ supported_endpoint_types?: string[];
79
+ billing_mode?: string;
80
+ billing_expr?: string;
81
+ pricing_version?: string;
82
+ ordinary_pricing?: boolean;
83
+ }
84
+ export interface PricingVendor {
85
+ id: number;
86
+ name: string;
87
+ description?: string;
88
+ icon?: string;
89
+ }
90
+ export interface PricingResponse {
91
+ data: ModelPricing[];
92
+ vendors: PricingVendor[];
93
+ group_ratio: Record<string, number>;
94
+ usable_group: Record<string, string>;
95
+ supported_endpoint?: Record<string, string[]>;
96
+ auto_groups?: string[];
97
+ pricing_version?: string;
98
+ }
99
+ /**
100
+ * GET /api/ratio_config
101
+ * 全站倍率字典。model 名 → 倍率/价格,字段是 map 而不是数组。
102
+ */
103
+ export interface RatioConfig {
104
+ model_ratio: Record<string, number>;
105
+ completion_ratio: Record<string, number>;
106
+ cache_ratio: Record<string, number>;
107
+ create_cache_ratio: Record<string, number>;
108
+ model_price: Record<string, number>;
109
+ /** 动态计费模式标识 */
110
+ billing_mode: Record<string, string>;
111
+ /** 动态计费表达式 */
112
+ billing_expr: Record<string, string>;
113
+ }
114
+ /** GET /v1/models(relay 端点,用 sk- 调用令牌) */
115
+ export interface RelayModelList {
116
+ object?: string;
117
+ data?: Array<{
118
+ id?: string;
119
+ object?: string;
120
+ }>;
121
+ }
122
+ /** GET /api/models 的 data:channelType(int) -> 模型名列表 */
123
+ export type ModelsByChannel = Record<string, string[]>;
124
+ /** GET /api/uptime/status */
125
+ export interface UptimeMonitor {
126
+ name: string;
127
+ uptime: number;
128
+ /** 1 = up,0 = down */
129
+ status: number;
130
+ group?: string;
131
+ }
132
+ export interface UptimeGroup {
133
+ categoryName: string;
134
+ monitors: UptimeMonitor[];
135
+ }
136
+ /** GET /api/perf-metrics */
137
+ export interface PerfSummary {
138
+ avg_latency_ms: number;
139
+ success_rate: number;
140
+ avg_tps: number;
141
+ }
142
+ export interface PerfBucketPoint {
143
+ ts: number;
144
+ avg_ttft_ms: number;
145
+ avg_latency_ms: number;
146
+ success_rate: number;
147
+ avg_tps: number;
148
+ }
149
+ export interface PerfGroupResult {
150
+ group: string;
151
+ avg_ttft_ms: number;
152
+ avg_latency_ms: number;
153
+ success_rate: number;
154
+ avg_tps: number;
155
+ series: PerfBucketPoint[];
156
+ }
157
+ export interface PerfQueryResult {
158
+ model_name: string;
159
+ series_schema?: string;
160
+ summary?: PerfSummary;
161
+ series: PerfBucketPoint[];
162
+ window_start: number;
163
+ window_end: number;
164
+ groups?: PerfGroupResult[];
165
+ }
166
+ /** GET /api/perf-metrics/summary */
167
+ export interface SuccessRatePoint {
168
+ ts: number;
169
+ success_rate: number;
170
+ }
171
+ export interface PerfModelSummary {
172
+ model_name: string;
173
+ avg_latency_ms: number;
174
+ success_rate: number;
175
+ avg_tps: number;
176
+ recent_success_series?: SuccessRatePoint[];
177
+ request_count?: number;
178
+ }
179
+ export interface PerfSummaryAllResult {
180
+ summary?: PerfSummary;
181
+ window_start: number;
182
+ window_end: number;
183
+ models: PerfModelSummary[];
184
+ recent_success_series?: Record<string, SuccessRatePoint[]>;
185
+ }
186
+ /** relay 测试(POST /v1/chat/completions)返回的最小字段 */
187
+ export interface ChatCompletionResponse {
188
+ id?: string;
189
+ model?: string;
190
+ choices?: Array<{
191
+ message?: {
192
+ content?: string;
193
+ };
194
+ finish_reason?: string;
195
+ }>;
196
+ usage?: {
197
+ prompt_tokens?: number;
198
+ completion_tokens?: number;
199
+ total_tokens?: number;
200
+ };
201
+ }
package/lib/types.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ /**
3
+ * New API 接口返回结构。
4
+ * 全部字段均来自 QuantumNous/new-api 源码(controller/、pkg/perf_metrics/types.go),未做猜测。
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
package/lib/view.d.ts ADDED
@@ -0,0 +1,149 @@
1
+ import { Level } from './format';
2
+ import type { RouteProbe } from './api/network';
3
+ import type { ModelPricing, PerfBucketPoint, PerfQueryResult, PricingResponse, SiteStatus, UptimeGroup } from './types';
4
+ /**
5
+ * 卡片 / 列表的视图模型。图片渲染与纯文本渲染共用这一份数据,
6
+ * 保证「有没有 puppeteer」只影响排版,不影响口径。
7
+ */
8
+ export type Tone = 'ok' | 'warn' | 'fail' | 'plain' | 'unknown';
9
+ export interface CardBadge {
10
+ text: string;
11
+ tone: Tone;
12
+ }
13
+ export interface CardMetric {
14
+ key: string;
15
+ value: string;
16
+ sub?: string;
17
+ level?: Level;
18
+ }
19
+ export interface CardBlock {
20
+ level: Level;
21
+ label?: string;
22
+ }
23
+ export interface CardRow {
24
+ name: string;
25
+ ratio?: number;
26
+ value: string;
27
+ level?: Level;
28
+ }
29
+ export interface CardSection {
30
+ title: string;
31
+ note?: string;
32
+ rows?: CardRow[];
33
+ blocks?: CardBlock[];
34
+ axis?: string[];
35
+ legend?: boolean;
36
+ chips?: string[];
37
+ empty?: string;
38
+ }
39
+ export interface CardView {
40
+ siteName: string;
41
+ sub: string;
42
+ badges: CardBadge[];
43
+ metrics: CardMetric[];
44
+ sections: CardSection[];
45
+ notice?: {
46
+ tag: string;
47
+ body: string;
48
+ };
49
+ footerLeft: string;
50
+ footerRight: string;
51
+ }
52
+ export interface ListRowView {
53
+ name: string;
54
+ tag?: string;
55
+ value?: string;
56
+ level?: Level;
57
+ /** 长文本(公告正文等):value 换行显示,不右对齐、不省略 */
58
+ wrap?: boolean;
59
+ }
60
+ export interface ListView {
61
+ title: string;
62
+ subtitle?: string;
63
+ note?: string;
64
+ rows: ListRowView[];
65
+ empty?: string;
66
+ /** 超过该条数时折叠:只渲染前 N 行,并给一行「还有 M 条」提示 */
67
+ limit?: number;
68
+ footerLeft: string;
69
+ footerRight: string;
70
+ }
71
+ export interface ViewContext {
72
+ pluginName: string;
73
+ updatedAt?: number;
74
+ }
75
+ export declare function latencyMetric(key: string, ms: number | null | undefined, sub?: string): CardMetric;
76
+ /**
77
+ * 站点级色块:把多模型的每小时成功率按小时取平均。
78
+ * 注意真实站点(v1.0.0-rc.51)的 /api/perf-metrics/summary **不返回顶层
79
+ * recent_success_series**,只把它挂在 models[].recent_success_series 上,
80
+ * 所以这里接受两种形态:{ 模型: 点[] } 或 models[] 数组。
81
+ */
82
+ export declare function aggregateSuccessSeries(input?: Record<string, {
83
+ ts: number;
84
+ success_rate: number;
85
+ }[]> | Array<{
86
+ recent_success_series?: {
87
+ ts: number;
88
+ success_rate: number;
89
+ }[];
90
+ }>): CardBlock[];
91
+ export declare function seriesToBlocks(series: PerfBucketPoint[] | undefined): CardBlock[];
92
+ export declare function buildStatusCard(input: {
93
+ status: SiteStatus | null;
94
+ baseUrl: string;
95
+ latencyMs: number;
96
+ reachable: boolean;
97
+ pricing?: PricingResponse | null;
98
+ successSeries?: Record<string, {
99
+ ts: number;
100
+ success_rate: number;
101
+ }[]>;
102
+ /** /api/perf-metrics/summary 的 models[](真实站点把成功率序列挂在这里) */
103
+ perfModels?: Array<{
104
+ recent_success_series?: {
105
+ ts: number;
106
+ success_rate: number;
107
+ }[];
108
+ }>;
109
+ /** 参与色块聚合的模型数,用于文案 */
110
+ perfModelCount?: number;
111
+ announcements?: string[];
112
+ fallbackName?: string;
113
+ /** 成功率色块的统计窗口(小时),用于空态文案 */
114
+ seriesHours?: number;
115
+ }, view: ViewContext): CardView;
116
+ export declare function buildModelStatusCard(input: {
117
+ model: string;
118
+ group?: string;
119
+ hours: number;
120
+ result: PerfQueryResult;
121
+ }, view: ViewContext): CardView;
122
+ export declare function hostOf(baseUrl: string): string;
123
+ /** 公告来源:优先 /api/status 的 announcements,退回 /api/notice */
124
+ export declare function collectAnnouncements(status: SiteStatus | null, notice: string | null, limit: number): string[];
125
+ export interface PricingModelItem {
126
+ model_name: string;
127
+ vendor?: string;
128
+ groups: string[];
129
+ quotaType: number;
130
+ ratio: number;
131
+ completionRatio: number;
132
+ pricing: ModelPricing;
133
+ }
134
+ /** 把 /api/pricing 归一化成便于展示的模型条目 */
135
+ export declare function pricingModels(pricing: PricingResponse | null): PricingModelItem[];
136
+ /** 模型列表 / 搜索结果 */
137
+ export declare function buildModelList(models: PricingModelItem[], options: {
138
+ title: string;
139
+ subtitle?: string;
140
+ note?: string;
141
+ keyword?: string;
142
+ }, view: ViewContext): ListView;
143
+ /** Uptime Kuma 监控列表 */
144
+ export declare function buildUptimeList(groups: UptimeGroup[], name: string | undefined, view: ViewContext): ListView;
145
+ /**
146
+ * /api/status 的 api_info 线路列表(多数站点比 Uptime Kuma 更有内容)。
147
+ * 每条线路单独探一次 /api/status,得到真实可达性与延迟。
148
+ */
149
+ export declare function buildRouteList(probes: RouteProbe[], name: string | undefined, view: ViewContext): ListView;