@brglng/pi-currency-cost 0.1.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/LICENSE ADDED
@@ -0,0 +1,5 @@
1
+ Mozilla Public License Version 2.0
2
+
3
+ This Source Code Form is subject to the terms of the Mozilla Public
4
+ License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ file, You can obtain one at https://mozilla.org/MPL/2.0/.
package/README.md ADDED
@@ -0,0 +1,151 @@
1
+ # @brglng/pi-currency-cost
2
+
3
+ > For a Chinese version, see [README.zh.md](README.zh.md).
4
+
5
+ A [Pi coding agent](https://pi.dev/) extension that converts configured
6
+ provider/model usage costs to USD on every finalized assistant message, so
7
+ all costs and totals are shown in a single currency even when your providers
8
+ bill in different currencies.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ pi install npm:@brglng/pi-currency-cost
14
+ ```
15
+
16
+ ## Configuration
17
+
18
+ The extension reads two optional JSON files and merges them, with the project
19
+ file taking precedence over the global file:
20
+
21
+ - Global: `~/.pi/agent/extensions/pi-currency-cost/config.json`
22
+ - Project: `<cwd>/.pi/extensions/pi-currency-cost/config.json`
23
+
24
+ ```json
25
+ {
26
+ "currencies": {
27
+ "CNY": { "usdRate": 0.147 }
28
+ },
29
+ "providers": {
30
+ "bailian": { "currency": "CNY" }
31
+ },
32
+ "rateSource": { "type": "frankfurter" }
33
+ }
34
+ ```
35
+
36
+ A ready-to-edit template is provided in
37
+ [`config/config.example.json`](config/config.example.json).
38
+
39
+ ### currencies
40
+
41
+ All rates are **USD per one unit of the source currency** (for example, 0.147
42
+ USD per 1 CNY). Every configured non-USD currency must provide a `usdRate`
43
+ — a positive, finite number — before it can be converted. `usdRate` is the
44
+ only required field; conversion never happens without it:
45
+
46
+ ```json
47
+ {
48
+ "currencies": {
49
+ "CNY": { "usdRate": 0.147 },
50
+ "EUR": { "usdRate": 1.083 }
51
+ }
52
+ }
53
+ ```
54
+
55
+ `usdRate` is the value used for conversion. When a fresh rate is fetched by
56
+ the extension, it is written back into this same field (along with `updatedAt`).
57
+
58
+ ### providers
59
+
60
+ Only the providers listed here are converted; any provider that is not listed
61
+ is left untouched. Each entry names the currency Pi reports that provider's
62
+ usage costs in, with optional per-model overrides on top:
63
+
64
+ ```json
65
+ {
66
+ "providers": {
67
+ "bailian": { "currency": "CNY" },
68
+ "my-gateway": {
69
+ "currency": "EUR",
70
+ "models": {
71
+ "euro-model": { "currency": "EUR" },
72
+ "usd-model": { "currency": "USD" }
73
+ }
74
+ }
75
+ }
76
+ }
77
+ ```
78
+
79
+ A provider whose currency is `USD` (and any provider not listed) is left
80
+ untouched. A model with no override falls back to its provider's currency.
81
+
82
+ ### rateSource
83
+
84
+ The default is Frankfurter, a common reference-rate service:
85
+
86
+ ```json
87
+ {
88
+ "rateSource": {
89
+ "type": "frankfurter"
90
+ }
91
+ }
92
+ ```
93
+
94
+ If you prefer a mainland-China–oriented source, you can use Bank of China
95
+ instead:
96
+
97
+ ```json
98
+ {
99
+ "rateSource": {
100
+ "type": "boc"
101
+ }
102
+ }
103
+ ```
104
+
105
+ Both sources use bounded timeouts.
106
+
107
+ ## Commands
108
+
109
+ - `/currency-cost status` shows the config paths, the active rate source,
110
+ each currency rate (including its update time), and the provider/model
111
+ currency mappings.
112
+ - `/currency-cost refresh` fetches current USD rates for every configured
113
+ non-USD currency and saves them as `usdRate`.
114
+ - `/currency-cost help` lists the available subcommands.
115
+
116
+ ## How conversion works
117
+
118
+ On every finalized assistant message, the extension converts the message's
119
+ cost components — input, output, cache reads, and cache writes — from the
120
+ configured source currency into USD, and recomputes the total from those
121
+ converted components. Session totals, status views, and history all end up in
122
+ USD. A message is converted at most once, so the same cost is never converted
123
+ twice.
124
+
125
+ If a mapped currency has no usable `usdRate`, the message is left in its
126
+ original currency and a warning is shown.
127
+
128
+ ## Automatic updates
129
+
130
+ At the start of each session, the extension fetches the latest rate for every
131
+ configured non-USD currency and saves it back to the same `usdRate` field, so
132
+ conversion always uses the most recent rate the extension can obtain. You can
133
+ also trigger this manually at any time with `/currency-cost refresh`.
134
+
135
+ ## Network and fallback
136
+
137
+ Rate fetches require network access, which cannot be guaranteed in every
138
+ environment. If a rate source is unreachable or times out, the extension does
139
+ not fail: the configured `usdRate` value is kept and stays in effect, and
140
+ startup is never blocked.
141
+
142
+ ## Limitations
143
+
144
+ Exchange rates are estimates, and the resulting USD figures may differ from
145
+ what your provider actually bills. Pricing also changes over time and varies
146
+ by provider, region, and plan, so treat converted costs as an approximation
147
+ rather than an exact invoice.
148
+
149
+ ## License
150
+
151
+ MPL-2.0
package/README.zh.md ADDED
@@ -0,0 +1,140 @@
1
+ # @brglng/pi-currency-cost
2
+
3
+ > 英文版见 [README.md](README.md)。
4
+
5
+ 一个 [Pi coding agent](https://pi.dev/) 扩展:在每条最终确定的 assistant
6
+ 消息上,把已配置 Provider/模型的用量费用换成 USD,让所有费用和合计都以
7
+ 同一种货币显示——即使不同的 Provider 用不同的币种计费。
8
+
9
+ ## 安装
10
+
11
+ ```bash
12
+ pi install npm:@brglng/pi-currency-cost
13
+ ```
14
+
15
+ ## 配置
16
+
17
+ 扩展读取两份可选的 JSON 配置文件并把它们合并,其中项目配置优先于全局配置:
18
+
19
+ - 全局:`~/.pi/agent/extensions/pi-currency-cost/config.json`
20
+ - 项目:`<cwd>/.pi/extensions/pi-currency-cost/config.json`
21
+
22
+ ```json
23
+ {
24
+ "currencies": {
25
+ "CNY": { "usdRate": 0.147 }
26
+ },
27
+ "providers": {
28
+ "bailian": { "currency": "CNY" }
29
+ },
30
+ "rateSource": { "type": "frankfurter" }
31
+ }
32
+ ```
33
+
34
+ 可直接修改的模板见
35
+ [`config/config.example.json`](config/config.example.json)。
36
+
37
+ ### currencies
38
+
39
+ 所有汇率都是“1 单位源币种兑换多少 USD”(例如 1 CNY = 0.147 USD)。每个
40
+ 配置的非 USD 币种都必须提供 `usdRate`——一个正的、有限的数字——才能被
41
+ 换算。`usdRate` 是唯一必填字段,没有它就不会转换:
42
+
43
+ ```json
44
+ {
45
+ "currencies": {
46
+ "CNY": { "usdRate": 0.147 },
47
+ "EUR": { "usdRate": 1.083 }
48
+ }
49
+ }
50
+ ```
51
+
52
+ `usdRate` 就是换算时使用的汇率。当扩展取到新汇率时,会写回这个字段(并
53
+ 同时更新 `updatedAt`)。
54
+
55
+ ### providers
56
+
57
+ 只有列在这里的 Provider 才会被换算;没列出的 Provider 保持原样。每一项声明
58
+ Pi 报告该 Provider 用量费用所用的币种,也可以在此基础上叠加按模型覆盖:
59
+
60
+ ```json
61
+ {
62
+ "providers": {
63
+ "bailian": { "currency": "CNY" },
64
+ "my-gateway": {
65
+ "currency": "EUR",
66
+ "models": {
67
+ "euro-model": { "currency": "EUR" },
68
+ "usd-model": { "currency": "USD" }
69
+ }
70
+ }
71
+ }
72
+ }
73
+ ```
74
+
75
+ 币种为 `USD` 的 Provider(以及没有列出的 Provider)一律不做转换。未设置
76
+ 覆盖的模型使用其 Provider 的币种。
77
+
78
+ ### rateSource
79
+
80
+ 默认是较通用的 Frankfurter 参考汇率服务:
81
+
82
+ ```json
83
+ {
84
+ "rateSource": {
85
+ "type": "frankfurter"
86
+ }
87
+ }
88
+ ```
89
+
90
+ 如果更偏好面向中国大陆环境的来源,也可以选择中国银行:
91
+
92
+ ```json
93
+ {
94
+ "rateSource": {
95
+ "type": "boc"
96
+ }
97
+ }
98
+ ```
99
+
100
+ 两个内置来源都使用有界超时。
101
+
102
+ ## 命令
103
+
104
+ - `/currency-cost status` 显示配置路径、当前汇率来源、每种币种的汇率
105
+ (含更新时间)以及 Provider/模型与币种的对应关系。
106
+ - `/currency-cost refresh` 为所有配置的非 USD 币种获取最新 USD 汇率,并
107
+ 保存为 `usdRate`。
108
+ - `/currency-cost help` 列出所有子命令。
109
+
110
+ ## 换算方式
111
+
112
+ 在每条最终确定的 assistant 消息上,扩展会把消息的成本分量——输入、输出、
113
+ 缓存读、缓存写——从配置的源币种换算成 USD,再把换算后的分量重新求和得到
114
+ 总计。这样会话总计、状态视图和历史记录都会以 USD 显示。每条消息最多换算
115
+ 一次,同一笔费用不会被重复转换。
116
+
117
+ 如果某个映射的币种没有可用的 `usdRate`,该消息保持原币种,并显示一条
118
+ 警告。
119
+
120
+ ## 自动更新
121
+
122
+ 每次会话开始时,扩展会为所有配置的非 USD 币种取最新汇率,并写回同一个
123
+ `usdRate` 字段,这样换算始终使用扩展能取得的最新汇率。你也可以随时用
124
+ `/currency-cost refresh` 手动触发。
125
+
126
+ ## 网络与回退
127
+
128
+ 取汇率需要网络访问,而网络访问无法保证在所有环境中都可用。如果汇率来源
129
+ 无法连接或超时,扩展不会中断:配置的 `usdRate` 保持不变并继续生效,启动
130
+ 也不会被卡住。
131
+
132
+ ## 局限性说明
133
+
134
+ 汇率只是估算值,换算出的 USD 金额可能与 Provider 实际计费不一致;价格
135
+ 本身还会随时间、Provider、地区与套餐变化,因此请把换算后的成本当作一个
136
+ 大致参考,而不是精确账单。
137
+
138
+ ## License
139
+
140
+ MPL-2.0
@@ -0,0 +1,34 @@
1
+ {
2
+ "$schema": "https://raw.githubusercontent.com/brglng/pi-packages/main/packages/pi-currency-cost/schemas/config.schema.json",
3
+ "currencies": {
4
+ "CNY": {
5
+ "usdRate": 0.147
6
+ },
7
+ "EUR": {
8
+ "usdRate": 1.083,
9
+ "updatedAt": 1720000000000
10
+ }
11
+ },
12
+ "providers": {
13
+ "bailian": {
14
+ "currency": "CNY"
15
+ },
16
+ "openrouter": {
17
+ "currency": "USD"
18
+ },
19
+ "my-gateway": {
20
+ "currency": "EUR",
21
+ "models": {
22
+ "euro-model": {
23
+ "currency": "EUR"
24
+ },
25
+ "usd-model": {
26
+ "currency": "USD"
27
+ }
28
+ }
29
+ }
30
+ },
31
+ "rateSource": {
32
+ "type": "frankfurter"
33
+ }
34
+ }
@@ -0,0 +1,255 @@
1
+ import { MessageEndEvent, ExtensionAPI, ExtensionContext, ExtensionCommandContext } from '@earendil-works/pi-coding-agent';
2
+
3
+ /**
4
+ * One currency entry. `usdRate` is the sole conversion rate (USD per one
5
+ * unit of this currency), required for every configured non-USD currency.
6
+ * It is configured by the user and replaced in place (along with
7
+ * `updatedAt`) when a fresh rate is fetched.
8
+ */
9
+ interface CurrencyRateConfig {
10
+ /** USD per one unit of this currency. Required for every configured non-USD currency. */
11
+ usdRate: number;
12
+ /** Epoch milliseconds when `usdRate` was last set or fetched. */
13
+ updatedAt?: number;
14
+ }
15
+ /** Per-model currency override inside a provider mapping. */
16
+ interface ProviderModelCurrency {
17
+ currency?: string;
18
+ }
19
+ /**
20
+ * Provider mapping: the currency Pi-reported usage costs are denominated in.
21
+ * A per-model override wins over the provider-level currency.
22
+ */
23
+ interface ProviderCurrencyConfig {
24
+ currency?: string;
25
+ models?: Record<string, ProviderModelCurrency>;
26
+ }
27
+ /** Bank of China official spot-rate table (mainland-China reachable). */
28
+ interface BocRateSourceConfig {
29
+ type: "boc";
30
+ timeoutMs?: number;
31
+ }
32
+ /**
33
+ * Fixed Frankfurter currency converter endpoint: this extension reads
34
+ * `rates.USD` for `base=<CURRENCY>&symbols=USD` (USD per one source unit).
35
+ * The source is built in and cannot be redirected; only the timeout is
36
+ * configurable.
37
+ */
38
+ interface FrankfurterRateSourceConfig {
39
+ type: "frankfurter";
40
+ timeoutMs?: number;
41
+ }
42
+ type RateSourceConfig = BocRateSourceConfig | FrankfurterRateSourceConfig;
43
+ interface CurrencyCostConfig {
44
+ /** Rates keyed by normalized 3-letter currency code. */
45
+ currencies: Record<string, CurrencyRateConfig>;
46
+ /** Provider → currency mapping. Providers not listed here are never converted. */
47
+ providers: Record<string, ProviderCurrencyConfig>;
48
+ rateSource: RateSourceConfig;
49
+ }
50
+ declare const DEFAULT_CONFIG: CurrencyCostConfig;
51
+ type ConfigWarnings = string[];
52
+ declare const DEFAULT_TIMEOUT_MS = 8000;
53
+ declare const USD_CODE = "USD";
54
+ /** Uppercase a raw currency code and validate the normalized 3-letter form. */
55
+ declare function normalizeCurrencyCode(value: unknown): string | undefined;
56
+ /**
57
+ * Normalize one raw config object (global or project) onto a base config.
58
+ * Project currency/provider entries merge field-by-field over the global base,
59
+ * so a project may refine a global currency entry without repeating it.
60
+ * Invalid values are skipped and reported as warnings.
61
+ */
62
+ declare function normalizeConfig(raw: unknown, base?: CurrencyCostConfig): {
63
+ config: CurrencyCostConfig;
64
+ warnings: ConfigWarnings;
65
+ };
66
+ /**
67
+ * Load and merge the global config (`<agentDir>/extensions/pi-currency-cost/config.json`)
68
+ * and the project config (`<cwd>/.pi/extensions/pi-currency-cost/config.json`).
69
+ * Project values override global values; currency entries and provider
70
+ * mappings are merged per field/key.
71
+ */
72
+ declare function loadConfig(agentDir: string, cwd: string): Promise<{
73
+ config: CurrencyCostConfig;
74
+ warnings: ConfigWarnings;
75
+ }>;
76
+
77
+ declare const EXTENSION_ID = "pi-currency-cost";
78
+ /** Global config directory: `<agentDir>/extensions/pi-currency-cost/`. */
79
+ declare function getGlobalConfigDir(agentDir: string): string;
80
+ /** Global config file: `<agentDir>/extensions/pi-currency-cost/config.json`. */
81
+ declare function getGlobalConfigPath(agentDir: string): string;
82
+ /** Project config file: `<cwd>/.pi/extensions/pi-currency-cost/config.json`. */
83
+ declare function getProjectConfigPath(cwd: string): string;
84
+ /**
85
+ * The config file the extension writes to. The project config is the writable
86
+ * effective config when a project config file exists; otherwise the global
87
+ * config path is used. Only an absent project config (ENOENT) redirects to
88
+ * the global scope: permission or other access failures throw so writes can
89
+ * never silently land in another scope.
90
+ */
91
+ declare function getWritableConfigPath(agentDir: string, cwd: string): Promise<string>;
92
+
93
+ /** Bank of China official spot-rate page (中行外汇牌价). */
94
+ declare const BOC_PAGE_URL = "https://www.boc.cn/sourcedb/whpj/";
95
+ /**
96
+ * Chinese currency names as published in the 货币名称 column of the Bank of
97
+ * China rate table. The table publishes 中行折算价 per 100 units of the
98
+ * foreign currency; CNY itself is the base currency and has no row.
99
+ */
100
+ declare const BOC_CURRENCY_NAMES: Record<string, string>;
101
+ /**
102
+ * Parse the Bank of China HTML table into `Map<code, 中行折算价>` where the
103
+ * value is CNY per 100 units of the currency. Rows whose name is not in the
104
+ * known name map are ignored (including the disclaimer footer row).
105
+ */
106
+ declare function parseBocTable(html: string): Map<string, number>;
107
+ /**
108
+ * USD per one unit of `currency` derived from a parsed BOC table
109
+ * (cross-rate via the USD row, CNY handled as the base currency).
110
+ */
111
+ declare function bocUsdPerUnit(parsed: Map<string, number>, currency: string): number | undefined;
112
+ /**
113
+ * Conservative dot-path read: segments are plain object keys (no array
114
+ * indices, no wildcards, no prototype/inherited keys). Returns undefined
115
+ * for missing or unsafe paths.
116
+ */
117
+ declare function readDotPath(root: unknown, dotPath: string): unknown;
118
+ interface RateFetchError {
119
+ currency: string;
120
+ message: string;
121
+ }
122
+ interface RateFetchResult {
123
+ /** currency -> USD per one unit, for every currency that could be resolved. */
124
+ rates: Map<string, number>;
125
+ errors: RateFetchError[];
126
+ }
127
+ /**
128
+ * Fetch current USD rates for all requested currencies. Frankfurter is queried
129
+ * once per currency; BOC uses one table request for all currencies.
130
+ * Transport-level failures of the whole source throw; per-currency failures
131
+ * are reported in `errors` so the caller can keep the configured `usdRate`
132
+ * and warn.
133
+ */
134
+ declare function fetchUsdRates(rateSource: RateSourceConfig, currencies: string[], signal: AbortSignal | undefined, fetchImpl?: typeof fetch): Promise<RateFetchResult>;
135
+ /**
136
+ * True when a currency entry has a usable positive configured rate.
137
+ * Conversion never happens without one. There is no fallback/current split:
138
+ * the single `usdRate` is both what the user sets and what a fetch replaces.
139
+ */
140
+ declare function hasValidUsdRate(entry: CurrencyRateConfig | undefined): boolean;
141
+ /** Pick the configured rate to convert with (the single `usdRate`). */
142
+ declare function selectUsdRate(entry: CurrencyRateConfig | undefined): number | undefined;
143
+ /** Human-readable description of the configured rate source. */
144
+ declare function rateSourceDescription(rateSource: RateSourceConfig): string;
145
+
146
+ /**
147
+ * Read a raw config JSON file. Returns undefined when the file does not
148
+ * exist; throws when it exists but is not valid JSON (so callers never
149
+ * silently clobber a corrupt user config).
150
+ */
151
+ declare function readRawConfig(path: string): Promise<Record<string, unknown> | undefined>;
152
+ /**
153
+ * Write a config JSON file atomically: write to a sibling temp file, then
154
+ * rename over the target. A failed write leaves the previous file intact and
155
+ * cleans up the temp file.
156
+ */
157
+ declare function writeConfigAtomic(path: string, raw: Record<string, unknown>): Promise<void>;
158
+ interface CurrencyUpdate {
159
+ /** USD per one unit of the currency; replaces the stored `usdRate`. */
160
+ usdRate: number;
161
+ updatedAt: number;
162
+ }
163
+ /**
164
+ * Merge a currency update into the config file at `path`, preserving all
165
+ * unknown root fields, other currency entries, and unknown fields inside the
166
+ * updated currency entry. Returns the updated root object.
167
+ */
168
+ declare function updateWritableConfig(path: string, currency: string, update: CurrencyUpdate): Promise<Record<string, unknown>>;
169
+
170
+ type ConversionWarn = (message: string) => void;
171
+ /**
172
+ * Resolve the source currency for a provider/model pair: the per-model
173
+ * override wins over the provider-level currency; providers that are not
174
+ * mapped at all yield undefined (never converted). Only own keys are read,
175
+ * so inherited prototype entries can never act as providers or models.
176
+ */
177
+ declare function selectCurrency(providers: Record<string, ProviderCurrencyConfig>, provider: string, model: string): string | undefined;
178
+ /**
179
+ * Convert an assistant message's `usage.cost` fields from the configured
180
+ * source currency to USD. Returns a replacement message (same role, same
181
+ * shape) when a conversion applies, or undefined when the message is not
182
+ * covered by the config (unmapped/untouched providers, USD, missing rates).
183
+ *
184
+ * Only the five `usage.cost` fields are touched; the rest of the message and
185
+ * usage objects are preserved by reference. `total` is recomputed as the sum
186
+ * of the converted components, which is how Pi itself derives `cost.total`.
187
+ */
188
+ declare function convertUsageCost(message: MessageEndEvent["message"], config: CurrencyCostConfig, warn?: ConversionWarn | undefined): MessageEndEvent["message"] | undefined;
189
+
190
+ interface Note {
191
+ text: string;
192
+ type: "info" | "warning";
193
+ }
194
+ declare class CurrencyCostRuntime {
195
+ config: CurrencyCostConfig;
196
+ readonly convertedMessages: WeakSet<object>;
197
+ private readonly warned;
198
+ private pendingNotes;
199
+ /** Config load warnings (not yet reported over a session context). */
200
+ constructor(config: CurrencyCostConfig);
201
+ enqueueNote(text: string, type: Note["type"]): void;
202
+ /** Flush pending startup notes through the first session context. */
203
+ flushNotes(ctx: ExtensionContext): void;
204
+ warnOnce(ctx: ExtensionContext, key: string, text: string): void;
205
+ /**
206
+ * Fetch a fresh rate for every configured non-USD currency that has a
207
+ * valid `usdRate`, persist each result back to that same field, and update
208
+ * the in-memory config. Per-currency failures keep the configured `usdRate`
209
+ * (selection uses it unchanged) and are reported; a source-level failure is
210
+ * reported as a warning and never crashes startup. `signal` bounds the
211
+ * whole fetch.
212
+ */
213
+ refreshConfiguredRates(agentDir: string, cwd: string, signal: AbortSignal | undefined, fetchImpl?: typeof fetch): Promise<void>;
214
+ /**
215
+ * Start-of-session fetch of missing current rates, awaited inside
216
+ * `session_start` so its notes are flushed in that lifecycle and the
217
+ * persisted rates update the in-memory config before any message_end.
218
+ * Every failure is caught and reported; startup can never crash here.
219
+ */
220
+ startupFetch(agentDir: string, cwd: string, fetchImpl: typeof fetch, timeoutSignal: AbortSignal): Promise<void>;
221
+ /** Refresh every configured non-USD currency and persist fresh rates. */
222
+ refreshRates(agentDir: string, cwd: string, ctx: ExtensionCommandContext, fetchImpl?: typeof fetch): Promise<void>;
223
+ private persistFetchedRates;
224
+ /**
225
+ * `/currency-cost status`: show config paths, the rate source, the single
226
+ * configured `usdRate` per currency, and provider mappings.
227
+ */
228
+ statusCommand(args: string[], agentDir: string, cwd: string, ctx: ExtensionCommandContext): Promise<void>;
229
+ }
230
+ /**
231
+ * Build the extension runtime. Async: the merged global + project config
232
+ * is loaded before any handler is registered so message_end and the
233
+ * /currency-cost command always observe the effective config — startup never
234
+ * races with config loading.
235
+ */
236
+ declare function createCurrencyCostExtension(pi: ExtensionAPI, options?: {
237
+ agentDir?: string;
238
+ cwd?: string;
239
+ /** Skip the start-of-session rate fetch (used by tests). */
240
+ skipStartupFetch?: boolean;
241
+ fetchImpl?: typeof fetch;
242
+ }): Promise<{
243
+ runtime: CurrencyCostRuntime;
244
+ reload: () => Promise<void>;
245
+ }>;
246
+ /**
247
+ * @brglng/pi-currency-cost extension factory.
248
+ * Loads global + project config, converts mapped provider/model usage costs
249
+ * to USD on every finalized assistant message, and registers the
250
+ * /currency-cost command.
251
+ */
252
+ declare function piCurrencyCostExtension(pi: ExtensionAPI): Promise<void>;
253
+
254
+ export { BOC_CURRENCY_NAMES, BOC_PAGE_URL, DEFAULT_CONFIG, DEFAULT_TIMEOUT_MS, EXTENSION_ID, USD_CODE, bocUsdPerUnit, convertUsageCost, createCurrencyCostExtension, piCurrencyCostExtension as default, fetchUsdRates, getGlobalConfigDir, getGlobalConfigPath, getProjectConfigPath, getWritableConfigPath, hasValidUsdRate, loadConfig, normalizeConfig, normalizeCurrencyCode, parseBocTable, rateSourceDescription, readDotPath, readRawConfig, selectCurrency, selectUsdRate, updateWritableConfig, writeConfigAtomic };
255
+ export type { BocRateSourceConfig, ConfigWarnings, ConversionWarn, CurrencyCostConfig, CurrencyRateConfig, CurrencyUpdate, ProviderCurrencyConfig, ProviderModelCurrency, RateFetchError, RateFetchResult, RateSourceConfig };
package/package.json ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "name": "@brglng/pi-currency-cost",
3
+ "version": "0.1.0",
4
+ "description": "Convert configured provider/model usage costs to USD for Pi sessions",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/public.d.ts",
9
+ "default": "./src/index.ts"
10
+ }
11
+ },
12
+ "files": [
13
+ "src",
14
+ "dist",
15
+ "config/config.example.json",
16
+ "schemas/config.schema.json",
17
+ "README.md",
18
+ "README.zh.md",
19
+ "LICENSE"
20
+ ],
21
+ "keywords": [
22
+ "pi-package",
23
+ "pi",
24
+ "pi-extension",
25
+ "pi-coding-agent",
26
+ "coding-agent",
27
+ "currency",
28
+ "cost",
29
+ "usd",
30
+ "conversion",
31
+ "exchange-rate"
32
+ ],
33
+ "author": {
34
+ "name": "brglng"
35
+ },
36
+ "license": "MPL-2.0",
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/brglng/pi-packages.git",
40
+ "directory": "packages/pi-currency-cost"
41
+ },
42
+ "homepage": "https://github.com/brglng/pi-packages/tree/main/packages/pi-currency-cost#readme",
43
+ "bugs": {
44
+ "url": "https://github.com/brglng/pi-packages/issues"
45
+ },
46
+ "engines": {
47
+ "node": ">=22"
48
+ },
49
+ "publishConfig": {
50
+ "access": "public"
51
+ },
52
+ "pi": {
53
+ "extensions": [
54
+ "./src/index.ts"
55
+ ]
56
+ },
57
+ "peerDependencies": {
58
+ "@earendil-works/pi-coding-agent": ">=0.79.0"
59
+ },
60
+ "devDependencies": {
61
+ "@biomejs/biome": "^2.4.16",
62
+ "@earendil-works/pi-coding-agent": "0.83.0",
63
+ "@types/node": "^22.15.3",
64
+ "rollup": "^4.62.2",
65
+ "rollup-plugin-dts": "^6.4.1",
66
+ "rumdl": "^0.2.10",
67
+ "typescript": "^6.0.3",
68
+ "vitest": "^4.1.8"
69
+ },
70
+ "scripts": {
71
+ "check": "tsc --noEmit",
72
+ "build:types": "rollup -c rollup.dts.config.mjs",
73
+ "test": "vitest run",
74
+ "lint:md": "rumdl check *.md",
75
+ "lint": "biome check . && pnpm run lint:md"
76
+ }
77
+ }