@kangyishuai/dsh-advisor 0.3.0 → 0.5.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 +45 -37
- package/README.zh-CN.md +44 -36
- package/lib/budget.js +10 -4
- package/lib/catalog.js +35 -8
- package/lib/client.js +276 -28
- package/lib/guard.js +22 -8
- package/lib/ledger.js +19 -4
- package/lib/money.js +74 -0
- package/lib/service.js +127 -15
- package/lib/settings.js +45 -4
- package/locale/en.json +22 -4
- package/locale/zh.json +22 -4
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -108,7 +108,6 @@ window.__ModuleLoader__.load({
|
|
|
108
108
|
"settings.budget.maxCallsPerTurn": "每回合最多调用",
|
|
109
109
|
"settings.budget.maxCallsPerSession": "每会话最多调用",
|
|
110
110
|
"settings.budget.maxCallsPerHour": "每小时最多调用",
|
|
111
|
-
"settings.budget.maxCostPerDayUsd": "每日预算(USD)",
|
|
112
111
|
"settings.budget.priceInputPerMTok": "输入单价(USD / 百万 token)",
|
|
113
112
|
"settings.budget.priceOutputPerMTok": "输出单价(USD / 百万 token)",
|
|
114
113
|
"settings.budget.range": "默认 {default} · 范围 {min}–{max}",
|
|
@@ -247,7 +246,6 @@ window.__ModuleLoader__.load({
|
|
|
247
246
|
"settings.budget.maxCallsPerTurn": "Max calls per turn",
|
|
248
247
|
"settings.budget.maxCallsPerSession": "Max calls per session",
|
|
249
248
|
"settings.budget.maxCallsPerHour": "Max calls per hour",
|
|
250
|
-
"settings.budget.maxCostPerDayUsd": "Daily budget (USD)",
|
|
251
249
|
"settings.budget.priceInputPerMTok": "Input price (USD / 1M tokens)",
|
|
252
250
|
"settings.budget.priceOutputPerMTok": "Output price (USD / 1M tokens)",
|
|
253
251
|
"settings.budget.range": "default {default} · range {min}–{max}",
|
|
@@ -603,16 +601,20 @@ window.__ModuleLoader__.load({
|
|
|
603
601
|
maxCallsPerTurn: 1,
|
|
604
602
|
maxCallsPerSession: 32,
|
|
605
603
|
maxCallsPerHour: 12,
|
|
606
|
-
|
|
604
|
+
currency: "USD",
|
|
605
|
+
usdToCny: 7.2,
|
|
606
|
+
maxCostPerDay: 0,
|
|
607
607
|
priceInputPerMTok: 0,
|
|
608
608
|
priceOutputPerMTok: 0,
|
|
609
|
+
priceCacheReadPerMTok: 0,
|
|
610
|
+
priceCacheWritePerMTok: 0,
|
|
609
611
|
privacyScope: "summary",
|
|
610
612
|
redact: true,
|
|
611
613
|
dedupe: true,
|
|
612
614
|
sessionCap: 24,
|
|
613
615
|
allowedModels: [],
|
|
614
616
|
workspaceMaxCallsPerDay: 200,
|
|
615
|
-
|
|
617
|
+
workspaceMaxCostPerDay: 0,
|
|
616
618
|
postAnswer: "off",
|
|
617
619
|
advisorTools: false,
|
|
618
620
|
tracePrompt: "preview",
|
|
@@ -625,21 +627,27 @@ window.__ModuleLoader__.load({
|
|
|
625
627
|
maxCallsPerTurn: [0, 8],
|
|
626
628
|
maxCallsPerSession: [0, 1000],
|
|
627
629
|
maxCallsPerHour: [0, 500],
|
|
628
|
-
|
|
630
|
+
maxCostPerDay: [0, 10000],
|
|
629
631
|
priceInputPerMTok: [0, 1000],
|
|
630
632
|
priceOutputPerMTok: [0, 1000],
|
|
633
|
+
priceCacheReadPerMTok: [0, 1000],
|
|
634
|
+
priceCacheWritePerMTok: [0, 1000],
|
|
631
635
|
workspaceMaxCallsPerDay: [0, 100000],
|
|
632
|
-
|
|
636
|
+
workspaceMaxCostPerDay: [0, 10000],
|
|
637
|
+
usdToCny: [0.1, 100],
|
|
633
638
|
sessionCap: [0, 200],
|
|
634
639
|
maxInputChars: [4000, 400000],
|
|
635
640
|
};
|
|
636
641
|
|
|
637
642
|
/** 金额字段的小数位;与 settings.js 的 MONEY_LIMITS 一致。 @type {Record<string, number>} */
|
|
638
643
|
let SETTINGS_MONEY_FIELDS = {
|
|
639
|
-
|
|
640
|
-
|
|
644
|
+
maxCostPerDay: 2,
|
|
645
|
+
workspaceMaxCostPerDay: 2,
|
|
641
646
|
priceInputPerMTok: 4,
|
|
642
647
|
priceOutputPerMTok: 4,
|
|
648
|
+
priceCacheReadPerMTok: 4,
|
|
649
|
+
priceCacheWritePerMTok: 4,
|
|
650
|
+
usdToCny: 4,
|
|
643
651
|
};
|
|
644
652
|
|
|
645
653
|
/** 所有整数输入字段(temperature 单独处理:可为空、可小数)。 */
|
|
@@ -653,9 +661,12 @@ window.__ModuleLoader__.load({
|
|
|
653
661
|
"maxCallsPerSession",
|
|
654
662
|
"maxCallsPerHour",
|
|
655
663
|
"workspaceMaxCallsPerDay",
|
|
656
|
-
"
|
|
664
|
+
"workspaceMaxCostPerDay",
|
|
657
665
|
"priceInputPerMTok",
|
|
658
666
|
"priceOutputPerMTok",
|
|
667
|
+
"priceCacheReadPerMTok",
|
|
668
|
+
"priceCacheWritePerMTok",
|
|
669
|
+
"usdToCny",
|
|
659
670
|
"maxTokens",
|
|
660
671
|
"temperature",
|
|
661
672
|
"maxInputChars",
|
|
@@ -672,9 +683,9 @@ window.__ModuleLoader__.load({
|
|
|
672
683
|
"maxCallsPerTurn",
|
|
673
684
|
"maxCallsPerSession",
|
|
674
685
|
"maxCallsPerHour",
|
|
675
|
-
"
|
|
686
|
+
"maxCostPerDay",
|
|
676
687
|
"workspaceMaxCallsPerDay",
|
|
677
|
-
"
|
|
688
|
+
"workspaceMaxCostPerDay",
|
|
678
689
|
];
|
|
679
690
|
|
|
680
691
|
/** 模式与发送范围的白名单,与 settings.js 一致。 */
|
|
@@ -698,11 +709,15 @@ window.__ModuleLoader__.load({
|
|
|
698
709
|
"maxCallsPerTurn",
|
|
699
710
|
"maxCallsPerSession",
|
|
700
711
|
"maxCallsPerHour",
|
|
701
|
-
"
|
|
712
|
+
"currency",
|
|
713
|
+
"usdToCny",
|
|
714
|
+
"maxCostPerDay",
|
|
702
715
|
"priceInputPerMTok",
|
|
703
716
|
"priceOutputPerMTok",
|
|
717
|
+
"priceCacheReadPerMTok",
|
|
718
|
+
"priceCacheWritePerMTok",
|
|
704
719
|
"workspaceMaxCallsPerDay",
|
|
705
|
-
"
|
|
720
|
+
"workspaceMaxCostPerDay",
|
|
706
721
|
"privacyScope",
|
|
707
722
|
"redact",
|
|
708
723
|
"dedupe",
|
|
@@ -814,9 +829,16 @@ window.__ModuleLoader__.load({
|
|
|
814
829
|
postAnswer: input.postAnswer === "card" ? "card" : "off",
|
|
815
830
|
advisorTools: input.advisorTools === true,
|
|
816
831
|
tracePrompt: input.tracePrompt === "off" ? "off" : "preview",
|
|
832
|
+
currency: input.currency === "CNY" ? "CNY" : "USD",
|
|
833
|
+
usdToCny: 7.2,
|
|
817
834
|
allowedModels: normalizeStringList(input.allowedModels),
|
|
818
835
|
confirmedHosts: normalizeStringList(input.confirmedHosts),
|
|
819
836
|
};
|
|
837
|
+
let rawRate = Number(input.usdToCny);
|
|
838
|
+
out.usdToCny =
|
|
839
|
+
Number.isFinite(rawRate) && rawRate > 0
|
|
840
|
+
? clampMoney(rawRate, SETTINGS_DEFAULTS.usdToCny, 0.1, 100, 4)
|
|
841
|
+
: SETTINGS_DEFAULTS.usdToCny;
|
|
820
842
|
for (let index = 0; index < SETTINGS_INT_FIELDS.length; index += 1) {
|
|
821
843
|
let key = SETTINGS_INT_FIELDS[index];
|
|
822
844
|
out[key] = clampInt(
|
|
@@ -827,6 +849,7 @@ window.__ModuleLoader__.load({
|
|
|
827
849
|
);
|
|
828
850
|
}
|
|
829
851
|
for (let key of Object.keys(SETTINGS_MONEY_FIELDS)) {
|
|
852
|
+
if (key === "usdToCny") continue;
|
|
830
853
|
out[key] = clampMoney(
|
|
831
854
|
input[key],
|
|
832
855
|
SETTINGS_DEFAULTS[key],
|
|
@@ -864,6 +887,82 @@ window.__ModuleLoader__.load({
|
|
|
864
887
|
return n;
|
|
865
888
|
}
|
|
866
889
|
|
|
890
|
+
/**
|
|
891
|
+
* 币种规整:未知值按 USD。
|
|
892
|
+
* @param {any} value
|
|
893
|
+
* @returns {"USD"|"CNY"}
|
|
894
|
+
*/
|
|
895
|
+
function currencyOf(value) {
|
|
896
|
+
return value === "CNY" ? "CNY" : "USD";
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
/**
|
|
900
|
+
* USD → 设置币种金额;汇率非法时回落 7.2。
|
|
901
|
+
* @param {any} amountUsd
|
|
902
|
+
* @param {any} currency
|
|
903
|
+
* @param {any} rate
|
|
904
|
+
* @returns {number}
|
|
905
|
+
*/
|
|
906
|
+
function fromUsd(amountUsd, currency, rate) {
|
|
907
|
+
let n = Number(amountUsd);
|
|
908
|
+
if (!Number.isFinite(n)) return 0;
|
|
909
|
+
if (currencyOf(currency) === "USD") return n;
|
|
910
|
+
let r = Number(rate);
|
|
911
|
+
if (!Number.isFinite(r) || r <= 0) r = 7.2;
|
|
912
|
+
return n * r;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
/**
|
|
916
|
+
* 金额显示:USD 用 $,CNY 用 ¥。
|
|
917
|
+
* @param {any} amount
|
|
918
|
+
* @param {any} currency
|
|
919
|
+
* @param {number} [digits]
|
|
920
|
+
* @returns {string}
|
|
921
|
+
*/
|
|
922
|
+
function formatMoney(amount, currency, digits) {
|
|
923
|
+
let n = Number(amount);
|
|
924
|
+
let value = Number.isFinite(n) ? n : 0;
|
|
925
|
+
let places =
|
|
926
|
+
typeof digits === "number" && Number.isFinite(digits)
|
|
927
|
+
? Math.max(0, Math.min(8, Math.round(digits)))
|
|
928
|
+
: 4;
|
|
929
|
+
return `${currencyOf(currency) === "CNY" ? "¥" : "$"}${value.toFixed(places)}`;
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
/**
|
|
933
|
+
* 把草稿里的金额字段从当前币种换到目标币种,保持真实价值不变。
|
|
934
|
+
* 汇率是 1 USD = ? CNY;切换时用当前草稿里的汇率。
|
|
935
|
+
* @param {any} source
|
|
936
|
+
* @param {any} nextCurrency
|
|
937
|
+
* @returns {any}
|
|
938
|
+
*/
|
|
939
|
+
function convertCurrency(source, nextCurrency) {
|
|
940
|
+
let from = currencyOf(source.currency);
|
|
941
|
+
let to = currencyOf(nextCurrency);
|
|
942
|
+
let next = Object.assign({}, source, { currency: to });
|
|
943
|
+
if (from === to) return next;
|
|
944
|
+
let rate = Number(source.usdToCny);
|
|
945
|
+
if (!Number.isFinite(rate) || rate <= 0) rate = 7.2;
|
|
946
|
+
let factor = to === "CNY" ? rate : 1 / rate;
|
|
947
|
+
/** @type {Record<string, number>} */
|
|
948
|
+
let fields = {
|
|
949
|
+
maxCostPerDay: 2,
|
|
950
|
+
workspaceMaxCostPerDay: 2,
|
|
951
|
+
priceInputPerMTok: 4,
|
|
952
|
+
priceOutputPerMTok: 4,
|
|
953
|
+
priceCacheReadPerMTok: 4,
|
|
954
|
+
priceCacheWritePerMTok: 4,
|
|
955
|
+
};
|
|
956
|
+
for (let key of Object.keys(fields)) {
|
|
957
|
+
let n = Number(source[key]);
|
|
958
|
+
if (!Number.isFinite(n)) n = 0;
|
|
959
|
+
let digits = fields[key];
|
|
960
|
+
let scale = 10 ** digits;
|
|
961
|
+
next[key] = Math.round(n * factor * scale) / scale;
|
|
962
|
+
}
|
|
963
|
+
return next;
|
|
964
|
+
}
|
|
965
|
+
|
|
867
966
|
/**
|
|
868
967
|
* 校验 `advisor/options`:settings 必须可用,其余字段缺失时降级为空。
|
|
869
968
|
* 畸形一律 null(调用方显示内联错误 + 重试)。
|
|
@@ -908,6 +1007,8 @@ window.__ModuleLoader__.load({
|
|
|
908
1007
|
calls: countOf(totalsRaw.calls),
|
|
909
1008
|
inputTokens: countOf(totalsRaw.inputTokens),
|
|
910
1009
|
outputTokens: countOf(totalsRaw.outputTokens),
|
|
1010
|
+
cacheReadTokens: countOf(totalsRaw.cacheReadTokens),
|
|
1011
|
+
cacheWriteTokens: countOf(totalsRaw.cacheWriteTokens),
|
|
911
1012
|
costUsd: moneyOf(totalsRaw.costUsd),
|
|
912
1013
|
},
|
|
913
1014
|
pool: {
|
|
@@ -919,8 +1020,11 @@ window.__ModuleLoader__.load({
|
|
|
919
1020
|
priceRaw.source === "builtin" || priceRaw.source === "settings"
|
|
920
1021
|
? priceRaw.source
|
|
921
1022
|
: "none",
|
|
1023
|
+
cacheSource: priceRaw.cacheSource === "settings" ? "settings" : "none",
|
|
922
1024
|
input: moneyOf(priceRaw.input),
|
|
923
1025
|
output: moneyOf(priceRaw.output),
|
|
1026
|
+
cacheRead: moneyOf(priceRaw.cacheRead),
|
|
1027
|
+
cacheWrite: moneyOf(priceRaw.cacheWrite),
|
|
924
1028
|
},
|
|
925
1029
|
counters: {
|
|
926
1030
|
calls: countOf(countersRaw.calls),
|
|
@@ -2078,7 +2182,17 @@ window.__ModuleLoader__.load({
|
|
|
2078
2182
|
let settings = normalizeSettingsView(
|
|
2079
2183
|
outcome.settings === null ? committed : outcome.settings,
|
|
2080
2184
|
);
|
|
2081
|
-
|
|
2185
|
+
// 价格来源/缓存未计价提示要跟着刚保存的设置走,不必等下一次手动重新加载。
|
|
2186
|
+
let cacheSource =
|
|
2187
|
+
settings.priceCacheReadPerMTok > 0 || settings.priceCacheWritePerMTok > 0
|
|
2188
|
+
? "settings"
|
|
2189
|
+
: "none";
|
|
2190
|
+
setSnap(
|
|
2191
|
+
Object.assign({}, snap, {
|
|
2192
|
+
settings: settings,
|
|
2193
|
+
price: Object.assign({}, snap.price, { cacheSource: cacheSource }),
|
|
2194
|
+
}),
|
|
2195
|
+
);
|
|
2082
2196
|
setDraft(settings);
|
|
2083
2197
|
setTexts(textsOf(settings));
|
|
2084
2198
|
setNotice(t("settings.saved"));
|
|
@@ -2320,6 +2434,49 @@ window.__ModuleLoader__.load({
|
|
|
2320
2434
|
);
|
|
2321
2435
|
}
|
|
2322
2436
|
|
|
2437
|
+
/** 切换币种:按当前汇率折算金额,保持真实价值不变。 @param {string} value */
|
|
2438
|
+
function chooseCurrency(value) {
|
|
2439
|
+
if (!draft) return;
|
|
2440
|
+
let nextCurrency = currencyOf(value);
|
|
2441
|
+
if (nextCurrency === draft.currency) return;
|
|
2442
|
+
let next = convertCurrency(draft, nextCurrency);
|
|
2443
|
+
setDraft(next);
|
|
2444
|
+
setTexts(textsOf(next));
|
|
2445
|
+
setError(null);
|
|
2446
|
+
setNotice(
|
|
2447
|
+
t("settings.currency.converted")
|
|
2448
|
+
.replace("{currency}", nextCurrency)
|
|
2449
|
+
.replace("{rate}", String(next.usdToCny)),
|
|
2450
|
+
);
|
|
2451
|
+
}
|
|
2452
|
+
|
|
2453
|
+
/** 金额字段的币种单位。 @returns {string} */
|
|
2454
|
+
function currencyLabel() {
|
|
2455
|
+
return draft !== null && draft.currency === "CNY"
|
|
2456
|
+
? t("settings.unit.cny")
|
|
2457
|
+
: t("settings.unit.usd");
|
|
2458
|
+
}
|
|
2459
|
+
|
|
2460
|
+
/** 单价的币种单位。 @returns {string} */
|
|
2461
|
+
function priceUnit() {
|
|
2462
|
+
return draft !== null && draft.currency === "CNY"
|
|
2463
|
+
? t("settings.unit.cnyPerMTok")
|
|
2464
|
+
: t("settings.unit.usdPerMTok");
|
|
2465
|
+
}
|
|
2466
|
+
|
|
2467
|
+
/** 币种一行。 @returns {any} */
|
|
2468
|
+
function currencyField() {
|
|
2469
|
+
return selectField(
|
|
2470
|
+
"currency",
|
|
2471
|
+
"settings.currency",
|
|
2472
|
+
["USD", "CNY"],
|
|
2473
|
+
"settings.currency.",
|
|
2474
|
+
draft.currency,
|
|
2475
|
+
chooseCurrency,
|
|
2476
|
+
{ hint: t("settings.currencyHint") },
|
|
2477
|
+
);
|
|
2478
|
+
}
|
|
2479
|
+
|
|
2323
2480
|
/**
|
|
2324
2481
|
* @param {string} key
|
|
2325
2482
|
* @param {boolean} on
|
|
@@ -3039,7 +3196,10 @@ window.__ModuleLoader__.load({
|
|
|
3039
3196
|
|
|
3040
3197
|
let sep = t("settings.status.sep");
|
|
3041
3198
|
let totals = snap.totals;
|
|
3042
|
-
let priced = snap.
|
|
3199
|
+
let priced = snap.price.source !== "none" || snap.price.cacheSource !== "none";
|
|
3200
|
+
let cacheTotal =
|
|
3201
|
+
(Number(totals.cacheReadTokens) || 0) + (Number(totals.cacheWriteTokens) || 0);
|
|
3202
|
+
let cacheUnpriced = cacheTotal > 0 && snap.price.cacheSource === "none";
|
|
3043
3203
|
let lastText =
|
|
3044
3204
|
snap.last === null
|
|
3045
3205
|
? t("settings.status.none")
|
|
@@ -3049,9 +3209,23 @@ window.__ModuleLoader__.load({
|
|
|
3049
3209
|
t("settings.status.tokens")
|
|
3050
3210
|
.replace("{input}", formatCount(totals.inputTokens))
|
|
3051
3211
|
.replace("{output}", formatCount(totals.outputTokens)),
|
|
3212
|
+
...(cacheTotal > 0
|
|
3213
|
+
? [
|
|
3214
|
+
t("settings.status.cache")
|
|
3215
|
+
.replace("{read}", formatCount(totals.cacheReadTokens))
|
|
3216
|
+
.replace("{write}", formatCount(totals.cacheWriteTokens)),
|
|
3217
|
+
]
|
|
3218
|
+
: []),
|
|
3219
|
+
...(cacheUnpriced ? [t("settings.status.cacheUnpriced")] : []),
|
|
3052
3220
|
t("settings.status.cost") +
|
|
3053
3221
|
sep +
|
|
3054
|
-
(priced
|
|
3222
|
+
(priced
|
|
3223
|
+
? `≈${formatMoney(
|
|
3224
|
+
fromUsd(totals.costUsd, snap.settings.currency, snap.settings.usdToCny),
|
|
3225
|
+
snap.settings.currency,
|
|
3226
|
+
4,
|
|
3227
|
+
)}`
|
|
3228
|
+
: t("settings.status.costUnset")),
|
|
3055
3229
|
t("settings.status.last") + sep + lastText,
|
|
3056
3230
|
t("settings.status.skip") +
|
|
3057
3231
|
sep +
|
|
@@ -3087,9 +3261,20 @@ window.__ModuleLoader__.load({
|
|
|
3087
3261
|
}
|
|
3088
3262
|
|
|
3089
3263
|
let priceSourceText = t(`settings.budget.priceSource.${snap.price.source}`);
|
|
3264
|
+
let cachePriceSourceText =
|
|
3265
|
+
t("settings.budget.cachePriceSource") +
|
|
3266
|
+
sep +
|
|
3267
|
+
t(`settings.budget.cachePriceSource.${snap.price.cacheSource}`);
|
|
3090
3268
|
let poolLine = t("settings.budget.poolLine")
|
|
3091
3269
|
.replace("{calls}", formatCount(snap.pool.calls))
|
|
3092
|
-
.replace(
|
|
3270
|
+
.replace(
|
|
3271
|
+
"{cost}",
|
|
3272
|
+
formatMoney(
|
|
3273
|
+
fromUsd(snap.pool.costUsd, snap.settings.currency, snap.settings.usdToCny),
|
|
3274
|
+
snap.settings.currency,
|
|
3275
|
+
4,
|
|
3276
|
+
),
|
|
3277
|
+
);
|
|
3093
3278
|
|
|
3094
3279
|
// 基础设置:只放开启前必须做的决定,以及每日预算与发送范围。
|
|
3095
3280
|
let basicBody = [
|
|
@@ -3097,9 +3282,10 @@ window.__ModuleLoader__.load({
|
|
|
3097
3282
|
subgroup(
|
|
3098
3283
|
"settings.group.spend",
|
|
3099
3284
|
[
|
|
3100
|
-
|
|
3285
|
+
currencyField(),
|
|
3286
|
+
numberField("maxCostPerDay", "settings.budget.maxCostPerDay", {
|
|
3101
3287
|
hint: t("settings.basic.budgetHint"),
|
|
3102
|
-
unit:
|
|
3288
|
+
unit: currencyLabel(),
|
|
3103
3289
|
}),
|
|
3104
3290
|
],
|
|
3105
3291
|
"spend",
|
|
@@ -3151,9 +3337,9 @@ window.__ModuleLoader__.load({
|
|
|
3151
3337
|
help: t("settings.budget.workspaceHint"),
|
|
3152
3338
|
unit: t("settings.unit.calls"),
|
|
3153
3339
|
}),
|
|
3154
|
-
numberField("
|
|
3340
|
+
numberField("workspaceMaxCostPerDay", "settings.budget.workspaceMaxCostPerDay", {
|
|
3155
3341
|
help: t("settings.budget.workspaceHint"),
|
|
3156
|
-
unit:
|
|
3342
|
+
unit: currencyLabel(),
|
|
3157
3343
|
}),
|
|
3158
3344
|
h(
|
|
3159
3345
|
"div",
|
|
@@ -3166,15 +3352,32 @@ window.__ModuleLoader__.load({
|
|
|
3166
3352
|
subgroup(
|
|
3167
3353
|
"settings.group.money",
|
|
3168
3354
|
[
|
|
3355
|
+
numberField("usdToCny", "settings.usdToCny", {
|
|
3356
|
+
help: t("settings.usdToCnyHint"),
|
|
3357
|
+
unit: t("settings.unit.cnyPerUsd"),
|
|
3358
|
+
}),
|
|
3169
3359
|
numberField("priceInputPerMTok", "settings.budget.priceInputPerMTok", {
|
|
3170
3360
|
help: t("settings.budget.priceHint"),
|
|
3171
|
-
unit:
|
|
3361
|
+
unit: priceUnit(),
|
|
3172
3362
|
}),
|
|
3173
3363
|
numberField("priceOutputPerMTok", "settings.budget.priceOutputPerMTok", {
|
|
3174
3364
|
help: t("settings.budget.priceHint"),
|
|
3175
|
-
unit:
|
|
3365
|
+
unit: priceUnit(),
|
|
3366
|
+
}),
|
|
3367
|
+
numberField("priceCacheReadPerMTok", "settings.budget.priceCacheReadPerMTok", {
|
|
3368
|
+
help: t("settings.budget.cachePriceHint"),
|
|
3369
|
+
unit: priceUnit(),
|
|
3370
|
+
}),
|
|
3371
|
+
numberField("priceCacheWritePerMTok", "settings.budget.priceCacheWritePerMTok", {
|
|
3372
|
+
help: t("settings.budget.cachePriceHint"),
|
|
3373
|
+
unit: priceUnit(),
|
|
3176
3374
|
}),
|
|
3177
3375
|
h("div", { className: "dsh-advisor-readout", key: "priceSource" }, priceSourceText),
|
|
3376
|
+
h(
|
|
3377
|
+
"div",
|
|
3378
|
+
{ className: "dsh-advisor-readout", key: "cachePriceSource" },
|
|
3379
|
+
cachePriceSourceText,
|
|
3380
|
+
),
|
|
3178
3381
|
],
|
|
3179
3382
|
"money",
|
|
3180
3383
|
),
|
|
@@ -3498,7 +3701,6 @@ window.__ModuleLoader__.load({
|
|
|
3498
3701
|
"settings.advisorToolsHint":
|
|
3499
3702
|
"允许顾问读取工作目录里的文件来核实结论(最多 5 个、只读);会多一次模型调用。",
|
|
3500
3703
|
"settings.budget.workspaceMaxCallsPerDay": "工作区每日调用上限",
|
|
3501
|
-
"settings.budget.workspaceMaxCostPerDayUsd": "工作区每日预算",
|
|
3502
3704
|
"settings.budget.workspaceHint": "工作区级,跨会话累计、按本地日重置",
|
|
3503
3705
|
"settings.budget.poolLine": "工作区今日:{calls} 次 · ≈{cost}",
|
|
3504
3706
|
"settings.budget.priceSource.builtin": "单价来源:内置参考价",
|
|
@@ -3526,6 +3728,29 @@ window.__ModuleLoader__.load({
|
|
|
3526
3728
|
"settings.tracePrompt.hint":
|
|
3527
3729
|
"在会话记录里保留本次实际发送文本的首尾各 2,000 字符(脱敏后);关掉后只记元数据。发送前脱敏关闭时,预览也会包含原文。",
|
|
3528
3730
|
"settings.help.defaultPreview": "默认:首尾预览",
|
|
3731
|
+
"settings.currency": "币种",
|
|
3732
|
+
"settings.currency.USD": "美元(USD)",
|
|
3733
|
+
"settings.currency.CNY": "人民币(CNY)",
|
|
3734
|
+
"settings.currencyHint":
|
|
3735
|
+
"金额和单价按此币种填写;切换时会按汇率折算金额,保持真实价值不变。",
|
|
3736
|
+
"settings.currency.converted": "已按 1 USD = {rate} CNY 折算金额(尚未保存)。",
|
|
3737
|
+
"settings.usdToCny": "人民币汇率",
|
|
3738
|
+
"settings.usdToCnyHint":
|
|
3739
|
+
"1 USD = ? CNY;只在币种为人民币时用于换算。插件不联网取汇率,请手动维护。",
|
|
3740
|
+
"settings.unit.cny": "元",
|
|
3741
|
+
"settings.unit.cnyPerMTok": "元 / 百万 token",
|
|
3742
|
+
"settings.unit.cnyPerUsd": "元 / USD",
|
|
3743
|
+
"settings.budget.maxCostPerDay": "每日预算",
|
|
3744
|
+
"settings.budget.workspaceMaxCostPerDay": "工作区每日预算",
|
|
3745
|
+
"settings.budget.priceCacheReadPerMTok": "缓存读取单价",
|
|
3746
|
+
"settings.budget.priceCacheWritePerMTok": "缓存写入单价",
|
|
3747
|
+
"settings.budget.cachePriceHint":
|
|
3748
|
+
"缓存读取/写入的输入 token 单价(按当前币种 / 百万 token);0 = 不计缓存部分。宿主只提供缓存读/写 token,没有缓存输出 token。",
|
|
3749
|
+
"settings.budget.cachePriceSource": "缓存单价",
|
|
3750
|
+
"settings.budget.cachePriceSource.settings": "用户设置",
|
|
3751
|
+
"settings.budget.cachePriceSource.none": "未配置(缓存 token 不计)",
|
|
3752
|
+
"settings.status.cache": "缓存 token:{read} read / {write} write",
|
|
3753
|
+
"settings.status.cacheUnpriced": "账本里有缓存 token,但缓存单价为 0:缓存部分没有计价。",
|
|
3529
3754
|
"tail.trace.preview": "查看发送内容",
|
|
3530
3755
|
"tail.trace.previewHide": "收起发送内容",
|
|
3531
3756
|
"tail.trace.previewTruncated": "实际发送(脱敏后,首尾各 2,000 字符;共 {chars} 字符)",
|
|
@@ -3546,7 +3771,6 @@ window.__ModuleLoader__.load({
|
|
|
3546
3771
|
"settings.budget.maxCallsPerTurn": "每回合调用上限",
|
|
3547
3772
|
"settings.budget.maxCallsPerSession": "每会话调用上限",
|
|
3548
3773
|
"settings.budget.maxCallsPerHour": "每小时调用上限",
|
|
3549
|
-
"settings.budget.maxCostPerDayUsd": "每日预算",
|
|
3550
3774
|
"settings.budget.priceInputPerMTok": "输入单价",
|
|
3551
3775
|
"settings.budget.priceOutputPerMTok": "输出单价",
|
|
3552
3776
|
"settings.scope.off": "不发送",
|
|
@@ -3592,7 +3816,8 @@ window.__ModuleLoader__.load({
|
|
|
3592
3816
|
"settings.group.delivery": "投递与行为",
|
|
3593
3817
|
"settings.group.more": "更多",
|
|
3594
3818
|
"settings.group.status": "状态与诊断",
|
|
3595
|
-
"settings.basic.budgetHint":
|
|
3819
|
+
"settings.basic.budgetHint":
|
|
3820
|
+
"0 = 不限;默认 0,范围 0–10000(按当前币种)。切换币种会按汇率折算;更细的限额在高级设置里。",
|
|
3596
3821
|
"settings.budget.maxCostHint": "每天最多花多少钱;0 = 不限。只有填了单价才会计算金额。",
|
|
3597
3822
|
"settings.advanced.maxTokensHint": "单次回答最多生成多少 token;越大越贵。",
|
|
3598
3823
|
"settings.advanced.maxInputCharsHint":
|
|
@@ -3625,7 +3850,6 @@ window.__ModuleLoader__.load({
|
|
|
3625
3850
|
"settings.advisorToolsHint":
|
|
3626
3851
|
"Lets the advisor read files in the working directory to verify conclusions (up to 5 files, read-only); costs one extra model call.",
|
|
3627
3852
|
"settings.budget.workspaceMaxCallsPerDay": "Workspace calls per day",
|
|
3628
|
-
"settings.budget.workspaceMaxCostPerDayUsd": "Workspace daily budget",
|
|
3629
3853
|
"settings.budget.workspaceHint":
|
|
3630
3854
|
"Workspace-wide: shared across sessions, resets on the local day",
|
|
3631
3855
|
"settings.budget.poolLine": "Workspace today: {calls} calls · ≈{cost}",
|
|
@@ -3656,6 +3880,30 @@ window.__ModuleLoader__.load({
|
|
|
3656
3880
|
"settings.tracePrompt.hint":
|
|
3657
3881
|
"Stores the first and last 2,000 characters of the text actually sent (after redaction) in the session record; off stores metadata only. If redaction is off, the preview contains raw text.",
|
|
3658
3882
|
"settings.help.defaultPreview": "Default: head/tail preview",
|
|
3883
|
+
"settings.currency": "Currency",
|
|
3884
|
+
"settings.currency.USD": "US dollars (USD)",
|
|
3885
|
+
"settings.currency.CNY": "Chinese yuan (CNY)",
|
|
3886
|
+
"settings.currencyHint":
|
|
3887
|
+
"Amounts and prices are entered in this currency; switching converts them at the configured rate so the real value stays the same.",
|
|
3888
|
+
"settings.currency.converted": "Converted amounts at 1 USD = {rate} CNY (not saved yet).",
|
|
3889
|
+
"settings.usdToCny": "CNY per USD",
|
|
3890
|
+
"settings.usdToCnyHint":
|
|
3891
|
+
"1 USD = ? CNY; only used when the currency is CNY. The plugin never fetches rates; maintain it manually.",
|
|
3892
|
+
"settings.unit.cny": "CNY",
|
|
3893
|
+
"settings.unit.cnyPerMTok": "CNY / 1M tokens",
|
|
3894
|
+
"settings.unit.cnyPerUsd": "CNY / USD",
|
|
3895
|
+
"settings.budget.maxCostPerDay": "Daily budget",
|
|
3896
|
+
"settings.budget.workspaceMaxCostPerDay": "Workspace daily budget",
|
|
3897
|
+
"settings.budget.priceCacheReadPerMTok": "Cache-read price",
|
|
3898
|
+
"settings.budget.priceCacheWritePerMTok": "Cache-write price",
|
|
3899
|
+
"settings.budget.cachePriceHint":
|
|
3900
|
+
"Input-token price for cache reads/writes (selected currency per 1M tokens); 0 = cache tokens are not priced. The host exposes cache-read/write tokens only; there is no cache-output token.",
|
|
3901
|
+
"settings.budget.cachePriceSource": "Cache prices",
|
|
3902
|
+
"settings.budget.cachePriceSource.settings": "user settings",
|
|
3903
|
+
"settings.budget.cachePriceSource.none": "unset (cache tokens not priced)",
|
|
3904
|
+
"settings.status.cache": "Cache tokens: {read} read / {write} write",
|
|
3905
|
+
"settings.status.cacheUnpriced":
|
|
3906
|
+
"The ledger has cache tokens, but cache prices are 0: that part is not priced.",
|
|
3659
3907
|
"tail.trace.preview": "View sent content",
|
|
3660
3908
|
"tail.trace.previewHide": "Hide sent content",
|
|
3661
3909
|
"tail.trace.previewTruncated": "Sent (redacted; first/last 2,000 of {chars} chars)",
|
|
@@ -3679,7 +3927,6 @@ window.__ModuleLoader__.load({
|
|
|
3679
3927
|
"settings.budget.maxCallsPerTurn": "Calls per turn",
|
|
3680
3928
|
"settings.budget.maxCallsPerSession": "Calls per session",
|
|
3681
3929
|
"settings.budget.maxCallsPerHour": "Calls per hour",
|
|
3682
|
-
"settings.budget.maxCostPerDayUsd": "Daily budget",
|
|
3683
3930
|
"settings.budget.priceInputPerMTok": "Input price",
|
|
3684
3931
|
"settings.budget.priceOutputPerMTok": "Output price",
|
|
3685
3932
|
"settings.scope.off": "None",
|
|
@@ -4306,6 +4553,7 @@ window.__ModuleLoader__.load({
|
|
|
4306
4553
|
clampFloat: clampFloat,
|
|
4307
4554
|
clampMoney: clampMoney,
|
|
4308
4555
|
formatMoneyInput: formatMoneyInput,
|
|
4556
|
+
convertCurrency: convertCurrency,
|
|
4309
4557
|
normalizeSettingsView: normalizeSettingsView,
|
|
4310
4558
|
normalizeOptions: normalizeOptions,
|
|
4311
4559
|
normalizeModels: normalizeModels,
|
package/lib/guard.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
// 纯函数守卫:预算、去重、免疫窗口、配对。不 import 宿主包,单测可直接跑。
|
|
3
3
|
// 设计原则:所有判断都可以用「整数 + 时间戳」表达,不依赖会话对象的形状。
|
|
4
4
|
|
|
5
|
+
import { toUsd } from "./money.js";
|
|
6
|
+
|
|
5
7
|
/** 去重指纹的默认窗口:24 小时。 */
|
|
6
8
|
export const DEDUPE_WINDOW_MS = 24 * 60 * 60 * 1000;
|
|
7
9
|
/** 去重表上限(有界 FIFO)。 */
|
|
@@ -111,14 +113,13 @@ export function checkBudget(input) {
|
|
|
111
113
|
return { ok: false, code: "hour_limit", limit: settings.maxCallsPerHour, used };
|
|
112
114
|
}
|
|
113
115
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
) {
|
|
116
|
+
// 设置里的限额按 settings.currency 填写;比较前先换算成 USD。
|
|
117
|
+
const maxCostUsd = toUsd(settings.maxCostPerDay, settings.currency, settings.usdToCny);
|
|
118
|
+
if (maxCostUsd > 0 && counters.dayCostUsd + estimatedCostUsd > maxCostUsd) {
|
|
118
119
|
return {
|
|
119
120
|
ok: false,
|
|
120
121
|
code: "day_budget",
|
|
121
|
-
limit:
|
|
122
|
+
limit: maxCostUsd,
|
|
122
123
|
used: counters.dayCostUsd,
|
|
123
124
|
};
|
|
124
125
|
}
|
|
@@ -127,14 +128,27 @@ export function checkBudget(input) {
|
|
|
127
128
|
|
|
128
129
|
/**
|
|
129
130
|
* 估算一次调用的美元成本。单价为 0 时返回 0(= 不算钱,只记 token)。
|
|
130
|
-
* @param {{ inputTokens: number, outputTokens: number, priceInputPerMTok
|
|
131
|
+
* @param {{ inputTokens: number, outputTokens: number, cacheReadTokens?: number, cacheWriteTokens?: number, priceInputPerMTok?: number, priceOutputPerMTok?: number, priceCacheReadPerMTok?: number, priceCacheWritePerMTok?: number }} input
|
|
131
132
|
* @returns {number}
|
|
132
133
|
*/
|
|
133
134
|
export function estimateCostUsd(input) {
|
|
134
|
-
const {
|
|
135
|
+
const {
|
|
136
|
+
inputTokens,
|
|
137
|
+
outputTokens,
|
|
138
|
+
cacheReadTokens = 0,
|
|
139
|
+
cacheWriteTokens = 0,
|
|
140
|
+
priceInputPerMTok = 0,
|
|
141
|
+
priceOutputPerMTok = 0,
|
|
142
|
+
priceCacheReadPerMTok = 0,
|
|
143
|
+
priceCacheWritePerMTok = 0,
|
|
144
|
+
} = input;
|
|
135
145
|
const inCost = (Math.max(0, inputTokens) / 1_000_000) * Math.max(0, priceInputPerMTok);
|
|
136
146
|
const outCost = (Math.max(0, outputTokens) / 1_000_000) * Math.max(0, priceOutputPerMTok);
|
|
137
|
-
|
|
147
|
+
const cacheReadCost =
|
|
148
|
+
(Math.max(0, cacheReadTokens) / 1_000_000) * Math.max(0, priceCacheReadPerMTok);
|
|
149
|
+
const cacheWriteCost =
|
|
150
|
+
(Math.max(0, cacheWriteTokens) / 1_000_000) * Math.max(0, priceCacheWritePerMTok);
|
|
151
|
+
return inCost + outCost + cacheReadCost + cacheWriteCost;
|
|
138
152
|
}
|
|
139
153
|
|
|
140
154
|
/**
|
package/lib/ledger.js
CHANGED
|
@@ -28,6 +28,7 @@ export const LEDGER_MAX_BYTES = 2 * 1024 * 1024;
|
|
|
28
28
|
* @property {number} inputTokens
|
|
29
29
|
* @property {number} outputTokens
|
|
30
30
|
* @property {number} cacheReadTokens
|
|
31
|
+
* @property {number} [cacheWriteTokens]
|
|
31
32
|
* @property {number} costUsd
|
|
32
33
|
* @property {number} ms
|
|
33
34
|
* @property {string} [code]
|
|
@@ -62,7 +63,7 @@ export function dayKey(date = new Date()) {
|
|
|
62
63
|
* counters: (sessionId: string, now?: number) => SessionCounters,
|
|
63
64
|
* noteCall: (sessionId: string, input: { turn: number, costUsd: number, at: number }) => void,
|
|
64
65
|
* resetTurn: (sessionId: string, turn: number) => void,
|
|
65
|
-
* totals: () => { calls: number, inputTokens: number, outputTokens: number, costUsd: number, byTrigger: Record<string, number> },
|
|
66
|
+
* totals: () => { calls: number, inputTokens: number, outputTokens: number, cacheReadTokens: number, cacheWriteTokens: number, costUsd: number, byTrigger: Record<string, number> },
|
|
66
67
|
* recent: (limit?: number) => LedgerEntry[],
|
|
67
68
|
* ready: () => Promise<void>,
|
|
68
69
|
* recordAdvice: (entry: any) => Promise<void>,
|
|
@@ -78,8 +79,16 @@ export function createLedger(options) {
|
|
|
78
79
|
const countersBySession = new Map();
|
|
79
80
|
/** @type {LedgerEntry[]} */
|
|
80
81
|
const recent = [];
|
|
81
|
-
/** @type {{ calls: number, inputTokens: number, outputTokens: number, costUsd: number, byTrigger: Record<string, number> }} */
|
|
82
|
-
const totals = {
|
|
82
|
+
/** @type {{ calls: number, inputTokens: number, outputTokens: number, cacheReadTokens: number, cacheWriteTokens: number, costUsd: number, byTrigger: Record<string, number> }} */
|
|
83
|
+
const totals = {
|
|
84
|
+
calls: 0,
|
|
85
|
+
inputTokens: 0,
|
|
86
|
+
outputTokens: 0,
|
|
87
|
+
cacheReadTokens: 0,
|
|
88
|
+
cacheWriteTokens: 0,
|
|
89
|
+
costUsd: 0,
|
|
90
|
+
byTrigger: {},
|
|
91
|
+
};
|
|
83
92
|
let loaded = false;
|
|
84
93
|
|
|
85
94
|
/** 读历史账本,恢复「今日花费」与累计数(只读尾部 2MB,避免大文件拖慢启动)。 */
|
|
@@ -104,9 +113,13 @@ export function createLedger(options) {
|
|
|
104
113
|
const cost = Number(entry.costUsd ?? 0) || 0;
|
|
105
114
|
const inputTokens = Number(entry.inputTokens ?? 0) || 0;
|
|
106
115
|
const outputTokens = Number(entry.outputTokens ?? 0) || 0;
|
|
116
|
+
const cacheReadTokens = Number(entry.cacheReadTokens ?? 0) || 0;
|
|
117
|
+
const cacheWriteTokens = Number(entry.cacheWriteTokens ?? 0) || 0;
|
|
107
118
|
totals.calls += 1;
|
|
108
119
|
totals.inputTokens += inputTokens;
|
|
109
120
|
totals.outputTokens += outputTokens;
|
|
121
|
+
totals.cacheReadTokens += cacheReadTokens;
|
|
122
|
+
totals.cacheWriteTokens += cacheWriteTokens;
|
|
110
123
|
totals.costUsd += cost;
|
|
111
124
|
const trigger = typeof entry.trigger === "string" ? entry.trigger : "tool";
|
|
112
125
|
totals.byTrigger[trigger] = (totals.byTrigger[trigger] ?? 0) + 1;
|
|
@@ -178,6 +191,8 @@ export function createLedger(options) {
|
|
|
178
191
|
totals.calls += 1;
|
|
179
192
|
totals.inputTokens += entry.inputTokens;
|
|
180
193
|
totals.outputTokens += entry.outputTokens;
|
|
194
|
+
totals.cacheReadTokens += entry.cacheReadTokens ?? 0;
|
|
195
|
+
totals.cacheWriteTokens += entry.cacheWriteTokens ?? 0;
|
|
181
196
|
totals.costUsd += entry.costUsd;
|
|
182
197
|
totals.byTrigger[entry.trigger] = (totals.byTrigger[entry.trigger] ?? 0) + 1;
|
|
183
198
|
|
|
@@ -232,7 +247,7 @@ export function createLedger(options) {
|
|
|
232
247
|
}
|
|
233
248
|
},
|
|
234
249
|
|
|
235
|
-
/** @returns {{ calls: number, inputTokens: number, outputTokens: number, costUsd: number, byTrigger: Record<string, number> }} */
|
|
250
|
+
/** @returns {{ calls: number, inputTokens: number, outputTokens: number, cacheReadTokens: number, cacheWriteTokens: number, costUsd: number, byTrigger: Record<string, number> }} */
|
|
236
251
|
totals() {
|
|
237
252
|
return { ...totals, byTrigger: { ...totals.byTrigger } };
|
|
238
253
|
},
|