koishi-plugin-rina-currency 0.0.1 → 0.0.2

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/config.d.ts CHANGED
@@ -23,6 +23,8 @@ export interface Config {
23
23
  defaultBank: BankId;
24
24
  defaultRateType: RateType;
25
25
  requestTimeout: number;
26
+ requestAttempts: number;
27
+ requestRetryDelay: number;
26
28
  collectIntervalMinutes: number;
27
29
  collectOnStart: boolean;
28
30
  chartWidth: number;
package/lib/index.js CHANGED
@@ -72,6 +72,8 @@ var Config = import_koishi.Schema.object({
72
72
  defaultBank: BankSchema.default("boc").description("购汇成本与单银行趋势的默认参考银行"),
73
73
  defaultRateType: RateTypeSchema.default("spotSell").description("默认牌价类型"),
74
74
  requestTimeout: import_koishi.Schema.number().min(1e3).max(6e4).default(15e3).description("银行接口请求超时(毫秒)"),
75
+ requestAttempts: import_koishi.Schema.number().min(1).max(10).default(3).description("每个银行接口请求的总尝试次数(含首次请求)"),
76
+ requestRetryDelay: import_koishi.Schema.number().min(100).max(3e4).default(1500).description("请求失败后的首次重试等待时间(毫秒),后续等待时间线性递增"),
75
77
  collectIntervalMinutes: import_koishi.Schema.number().min(10).max(1440).default(60).description("牌价采集间隔(分钟)"),
76
78
  collectOnStart: import_koishi.Schema.boolean().default(true).description("插件启动后立即采集一次"),
77
79
  chartWidth: import_koishi.Schema.number().min(600).max(2e3).default(1200).description("趋势图宽度"),
@@ -392,18 +394,28 @@ __name(extendModel, "extendModel");
392
394
  // src/providers/common.ts
393
395
  var BOC_SOURCE_URL = "https://www.boc.cn/sourcedb/whpj/index.html";
394
396
  var ICBC_SOURCE_URL = "http://papi.icbc.com.cn/exchanges/ns/getLatest";
395
- async function fetchText(url, timeoutMs, init = {}) {
396
- const controller = new AbortController();
397
- const timer = setTimeout(() => controller.abort(), timeoutMs);
398
- try {
399
- const response = await fetch(url, { ...init, signal: controller.signal });
400
- if (!response.ok) {
401
- throw new Error(`${response.status} ${response.statusText}`);
397
+ async function fetchText(url, timeoutMs, init = {}, retry = { attempts: 1, retryDelay: 0 }) {
398
+ let lastError;
399
+ for (let attempt = 1; attempt <= retry.attempts; attempt++) {
400
+ const controller = new AbortController();
401
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
402
+ try {
403
+ const response = await fetch(url, { ...init, signal: controller.signal });
404
+ if (!response.ok) {
405
+ throw new Error(`${response.status} ${response.statusText}`);
406
+ }
407
+ return await response.text();
408
+ } catch (error) {
409
+ lastError = error;
410
+ } finally {
411
+ clearTimeout(timer);
412
+ }
413
+ if (attempt < retry.attempts) {
414
+ await new Promise((resolve) => setTimeout(resolve, retry.retryDelay * attempt));
402
415
  }
403
- return await response.text();
404
- } finally {
405
- clearTimeout(timer);
406
416
  }
417
+ const reason = lastError instanceof Error ? `${lastError.name}: ${lastError.message}` : String(lastError);
418
+ throw new Error(`请求 ${url} 失败,已尝试 ${retry.attempts} 次:${reason}`);
407
419
  }
408
420
  __name(fetchText, "fetchText");
409
421
  function asRate(value, field) {
@@ -453,8 +465,10 @@ function parseBocJpy(html, collectedAt = /* @__PURE__ */ new Date()) {
453
465
  }
454
466
  __name(parseBocJpy, "parseBocJpy");
455
467
  var BocProvider = class {
456
- constructor(timeoutMs) {
468
+ constructor(timeoutMs, attempts, retryDelay) {
457
469
  this.timeoutMs = timeoutMs;
470
+ this.attempts = attempts;
471
+ this.retryDelay = retryDelay;
458
472
  }
459
473
  static {
460
474
  __name(this, "BocProvider");
@@ -463,7 +477,7 @@ var BocProvider = class {
463
477
  async fetch() {
464
478
  const html = await fetchText(BOC_SOURCE_URL, this.timeoutMs, {
465
479
  headers: { "user-agent": "koishi-plugin-rina-currency/0.1" }
466
- });
480
+ }, { attempts: this.attempts, retryDelay: this.retryDelay });
467
481
  return parseBocJpy(html);
468
482
  }
469
483
  };
@@ -489,8 +503,10 @@ function parseIcbcJpy(payload, collectedAt = /* @__PURE__ */ new Date()) {
489
503
  }
490
504
  __name(parseIcbcJpy, "parseIcbcJpy");
491
505
  var IcbcProvider = class {
492
- constructor(timeoutMs) {
506
+ constructor(timeoutMs, attempts, retryDelay) {
493
507
  this.timeoutMs = timeoutMs;
508
+ this.attempts = attempts;
509
+ this.retryDelay = retryDelay;
494
510
  }
495
511
  static {
496
512
  __name(this, "IcbcProvider");
@@ -504,7 +520,7 @@ var IcbcProvider = class {
504
520
  "user-agent": "koishi-plugin-rina-currency/0.1"
505
521
  },
506
522
  body: "{}"
507
- });
523
+ }, { attempts: this.attempts, retryDelay: this.retryDelay });
508
524
  return parseIcbcJpy(JSON.parse(body));
509
525
  }
510
526
  };
@@ -716,8 +732,8 @@ function apply(ctx, config) {
716
732
  extendModel(ctx);
717
733
  const logger = ctx.logger(name);
718
734
  const service = new RateService(ctx, logger, [
719
- new BocProvider(config.requestTimeout),
720
- new IcbcProvider(config.requestTimeout)
735
+ new BocProvider(config.requestTimeout, config.requestAttempts, config.requestRetryDelay),
736
+ new IcbcProvider(config.requestTimeout, config.requestAttempts, config.requestRetryDelay)
721
737
  ]);
722
738
  registerCommands(ctx, service, config);
723
739
  new Scheduler(ctx, logger, service, config).start();
@@ -4,7 +4,9 @@ import { ExchangeRateQuote } from '../types';
4
4
  export declare function parseBocJpy(html: string, collectedAt?: Date): ExchangeRateQuote;
5
5
  export declare class BocProvider implements RateProvider {
6
6
  private timeoutMs;
7
+ private attempts;
8
+ private retryDelay;
7
9
  readonly id: "boc";
8
- constructor(timeoutMs: number);
10
+ constructor(timeoutMs: number, attempts: number, retryDelay: number);
9
11
  fetch(): Promise<ExchangeRateQuote>;
10
12
  }
@@ -5,6 +5,10 @@ export interface RateProvider {
5
5
  readonly id: ExchangeRateQuote['bank'];
6
6
  fetch(): Promise<ExchangeRateQuote>;
7
7
  }
8
- export declare function fetchText(url: string, timeoutMs: number, init?: RequestInit): Promise<string>;
8
+ export interface RetryOptions {
9
+ attempts: number;
10
+ retryDelay: number;
11
+ }
12
+ export declare function fetchText(url: string, timeoutMs: number, init?: RequestInit, retry?: RetryOptions): Promise<string>;
9
13
  export declare function asRate(value: unknown, field: string): number;
10
14
  export declare function chinaTime(date: string, time?: string): Date;
@@ -16,8 +16,10 @@ interface IcbcResponse {
16
16
  export declare function parseIcbcJpy(payload: IcbcResponse, collectedAt?: Date): ExchangeRateQuote;
17
17
  export declare class IcbcProvider implements RateProvider {
18
18
  private timeoutMs;
19
+ private attempts;
20
+ private retryDelay;
19
21
  readonly id: "icbc";
20
- constructor(timeoutMs: number);
22
+ constructor(timeoutMs: number, attempts: number, retryDelay: number);
21
23
  fetch(): Promise<ExchangeRateQuote>;
22
24
  }
23
25
  export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-rina-currency",
3
3
  "description": "面向购汇决策的日元汇率插件,基于中行和工行数据",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
package/readme.md CHANGED
@@ -46,6 +46,8 @@ plugins:
46
46
  rina-currency:
47
47
  defaultBank: boc
48
48
  defaultRateType: spotSell
49
+ requestAttempts: 3
50
+ requestRetryDelay: 1500
49
51
  collectIntervalMinutes: 60
50
52
  broadcasts:
51
53
  - platform: onebot
@@ -64,6 +66,8 @@ plugins:
64
66
  channelId: '987654'
65
67
  ```
66
68
 
69
+ 每个来源请求默认总共尝试 3 次(包含首次请求),失败后分别等待 1.5 秒和 3 秒再试;可通过 `requestAttempts` 和 `requestRetryDelay` 调整。连续失败时,最终日志会包含来源地址、尝试次数和最后一次错误。
70
+
67
71
  定时播报按北京时间执行。阈值提醒按相邻真实采样点的绝对涨跌幅触发:`changePercent: 0.5` 表示上涨或下跌至少 0.5%。比较周期由 `collectIntervalMinutes` 自然决定;同方向只提醒一次,波动回到阈值内或反向越过阈值后才会重新提醒。
68
72
 
69
73
  ## 开发