@brglng/pi-currency-cost 0.2.0 → 0.4.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.
package/README.md CHANGED
@@ -17,18 +17,18 @@ pi install npm:@brglng/pi-currency-cost
17
17
 
18
18
  ### Rates in Pi's settings.json
19
19
 
20
- Add the `pi-currency-cost` namespace to Pi's global `<agentDir>/settings.json`
20
+ Add the `currency-cost` namespace to Pi's global `<agentDir>/settings.json`
21
21
  (default: `~/.pi/agent/settings.json`). Use strict JSON (no comments or
22
22
  trailing commas). The extension ignores this namespace in project settings
23
23
  and in `models.json`.
24
24
 
25
25
  ```json
26
26
  {
27
- "pi-currency-cost": {
27
+ "currency-cost": {
28
28
  "currencies": {
29
29
  "CNY": { "usdRate": 0.147 }
30
30
  },
31
- "currencyExchangeRateSource": { "type": "frankfurter" }
31
+ "exchangeRateSource": { "type": "frankfurter" }
32
32
  }
33
33
  }
34
34
  ```
@@ -36,6 +36,12 @@ and in `models.json`.
36
36
  Valid rates and rate-source settings here take precedence over the merged
37
37
  legacy configuration.
38
38
 
39
+ If your `settings.json` uses `pi-currency-cost`, rename it to `currency-cost`.
40
+ The old namespace is ignored.
41
+
42
+ Upgrading from 0.2.0: rename `currencyExchangeRateSource` to
43
+ `exchangeRateSource` in `settings.json`. The old key is ignored.
44
+
39
45
  ### Provider and model currencies in Pi's models.json
40
46
 
41
47
  Declare `currency` beside the provider or model definition in Pi's global
package/README.zh.md CHANGED
@@ -16,23 +16,29 @@ pi install npm:@brglng/pi-currency-cost
16
16
 
17
17
  ### 在 Pi 的 settings.json 中配置汇率
18
18
 
19
- 将 `pi-currency-cost` 命名空间添加到 Pi 的全局 `<agentDir>/settings.json`
19
+ 将 `currency-cost` 命名空间添加到 Pi 的全局 `<agentDir>/settings.json`
20
20
  (默认为 `~/.pi/agent/settings.json`)。请使用严格的 JSON,不要添加注释或
21
21
  尾随逗号。扩展会忽略项目设置和 `models.json` 中的此命名空间。
22
22
 
23
23
  ```json
24
24
  {
25
- "pi-currency-cost": {
25
+ "currency-cost": {
26
26
  "currencies": {
27
27
  "CNY": { "usdRate": 0.147 }
28
28
  },
29
- "currencyExchangeRateSource": { "type": "frankfurter" }
29
+ "exchangeRateSource": { "type": "frankfurter" }
30
30
  }
31
31
  }
32
32
  ```
33
33
 
34
34
  此处有效的汇率及汇率来源设置优先于合并后的旧版配置。
35
35
 
36
+ 如果 `settings.json` 中仍使用 `pi-currency-cost`,请将其改为 `currency-cost`。
37
+ 扩展会忽略旧命名空间。
38
+
39
+ 从 0.2.0 升级时,请在 `settings.json` 中将 `currencyExchangeRateSource`
40
+ 改为 `exchangeRateSource`。扩展会忽略旧键。
41
+
36
42
  ### 在 Pi 的 models.json 中声明 Provider 和模型的币种
37
43
 
38
44
  在 Pi 的全局 `<agentDir>/models.json` 中,将 `currency` 写在对应的 Provider
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brglng/pi-currency-cost",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Convert configured provider/model usage costs to USD for Pi sessions",
5
5
  "type": "module",
6
6
  "exports": {
package/src/config.ts CHANGED
@@ -1,13 +1,12 @@
1
1
  import { readFile } from "node:fs/promises";
2
2
  import { join } from "node:path";
3
- import { EXTENSION_ID } from "./config-paths";
4
3
  import {
5
4
  getModelsConfigPath,
6
5
  isPositiveFinite,
7
6
  isRecord,
8
7
  readModelsConfig,
9
8
  } from "./models-config";
10
- import { readSettingsConfig } from "./settings-config";
9
+ import { readSettingsConfig, SETTINGS_KEY } from "./settings-config";
11
10
 
12
11
  /**
13
12
  * One currency entry. `usdRate` is the sole conversion rate (USD per one
@@ -462,7 +461,7 @@ export async function loadConfigWithSources(agentDir: string, cwd: string) {
462
461
  }),
463
462
  readSettingsConfig(agentDir).catch(() => {
464
463
  inlineWarnings.push(
465
- "Could not read a valid settings.json; ignoring pi-currency-cost settings",
464
+ `Could not read a valid settings.json; ignoring ${SETTINGS_KEY} settings`,
466
465
  );
467
466
  return undefined;
468
467
  }),
@@ -473,7 +472,7 @@ export async function loadConfigWithSources(agentDir: string, cwd: string) {
473
472
  global.config,
474
473
  );
475
474
  const inlineRateKeys = new Map<string, string>();
476
- const settings = settingsRaw?.[EXTENSION_ID];
475
+ const settings = settingsRaw?.[SETTINGS_KEY];
477
476
 
478
477
  if (isRecord(settings)) {
479
478
  if (isRecord(settings.currencies)) {
@@ -499,12 +498,12 @@ export async function loadConfigWithSources(agentDir: string, cwd: string) {
499
498
  );
500
499
  }
501
500
 
502
- const source = settings.currencyExchangeRateSource;
501
+ const source = settings.exchangeRateSource;
503
502
  if (isRecord(source)) {
504
503
  const type = source.type;
505
504
  if (type !== undefined && type !== "boc" && type !== "frankfurter") {
506
505
  inlineWarnings.push(
507
- 'settings.json: currencyExchangeRateSource.type must be "boc" or "frankfurter"; keeping previous',
506
+ 'settings.json: exchangeRateSource.type must be "boc" or "frankfurter"; keeping previous',
508
507
  );
509
508
  }
510
509
  config.rateSource = {
@@ -516,19 +515,19 @@ export async function loadConfigWithSources(agentDir: string, cwd: string) {
516
515
  source,
517
516
  config.rateSource.timeoutMs,
518
517
  inlineWarnings,
519
- "settings.json: currencyExchangeRateSource",
518
+ "settings.json: exchangeRateSource",
520
519
  ),
521
520
  };
522
521
  } else if (source === "boc" || source === "frankfurter") {
523
522
  config.rateSource = { ...config.rateSource, type: source };
524
523
  } else if (source !== undefined) {
525
524
  inlineWarnings.push(
526
- "settings.json: invalid currencyExchangeRateSource; keeping previous",
525
+ "settings.json: invalid exchangeRateSource; keeping previous",
527
526
  );
528
527
  }
529
528
  } else if (settings !== undefined) {
530
529
  inlineWarnings.push(
531
- "settings.json: pi-currency-cost must be an object; ignoring",
530
+ `settings.json: ${SETTINGS_KEY} must be an object; ignoring`,
532
531
  );
533
532
  }
534
533
 
@@ -2,6 +2,8 @@ import { readFile } from "node:fs/promises";
2
2
  import { join } from "node:path";
3
3
  import { isRecord } from "./models-config";
4
4
 
5
+ export const SETTINGS_KEY = "currency-cost";
6
+
5
7
  export function getSettingsConfigPath(agentDir: string): string {
6
8
  return join(agentDir, "settings.json");
7
9
  }
package/src/store.ts CHANGED
@@ -14,9 +14,8 @@ import {
14
14
  import { dirname, join } from "node:path";
15
15
  import { applyEdits, modify, type Node, parseTree } from "jsonc-parser";
16
16
  import { normalizeCurrencyCode } from "./config";
17
- import { EXTENSION_ID } from "./config-paths";
18
17
  import { isPositiveFinite, isRecord } from "./models-config";
19
- import { parseSettingsConfig } from "./settings-config";
18
+ import { parseSettingsConfig, SETTINGS_KEY } from "./settings-config";
20
19
 
21
20
  /**
22
21
  * Read a raw config JSON file. Returns undefined when the file does not
@@ -128,7 +127,7 @@ export async function updateInlineRate(
128
127
  if (!info.isFile()) throw new Error();
129
128
  const text = await readFile(target, "utf8");
130
129
  const raw = parseSettingsConfig(text);
131
- const settings = raw[EXTENSION_ID];
130
+ const settings = raw[SETTINGS_KEY];
132
131
  const currencies = isRecord(settings) ? settings.currencies : undefined;
133
132
  if (!isRecord(currencies)) throw new Error();
134
133
  const effective = Object.entries(currencies).findLast(
@@ -143,7 +142,7 @@ export async function updateInlineRate(
143
142
  const source = text.slice(bomLength);
144
143
  const tree = parseTree(source);
145
144
  const entry = propertyValue(
146
- propertyValue(propertyValue(tree, EXTENSION_ID), "currencies"),
145
+ propertyValue(propertyValue(tree, SETTINGS_KEY), "currencies"),
147
146
  key,
148
147
  );
149
148
  const rate = propertyValue(entry, "usdRate");