@kangyishuai/dsh-advisor 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 +53 -29
- package/README.zh-CN.md +56 -30
- package/lib/budget.js +10 -4
- package/lib/catalog.js +14 -3
- package/lib/client.js +1543 -462
- package/lib/guard.js +6 -5
- package/lib/money.js +74 -0
- package/lib/service.js +81 -7
- package/lib/settings.js +85 -13
- package/lib/trace.js +31 -0
- package/locale/en.json +111 -34
- package/locale/zh.json +117 -40
- package/package.json +1 -1
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
|
}
|
package/lib/money.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 币种与金额换算。
|
|
3
|
+
//
|
|
4
|
+
// 账本、预算池、guard 的内部单位始终是 **USD**;`currency`/`usdToCny` 只决定
|
|
5
|
+
// 「设置里的金额按什么币种填写/展示」以及「显示时怎么把 USD 折算回去」。
|
|
6
|
+
// 汇率是手动配置的,插件不联网取价。
|
|
7
|
+
|
|
8
|
+
/** 支持设置的币种。 */
|
|
9
|
+
export const CURRENCIES = Object.freeze(/** @type {const} */ (["USD", "CNY"]));
|
|
10
|
+
|
|
11
|
+
/** 默认汇率:1 USD = 7.2 CNY(只是初始值,请按实际汇率修改)。 */
|
|
12
|
+
export const DEFAULT_USD_TO_CNY = 7.2;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 规整币种:未知值按 USD。
|
|
16
|
+
* @param {unknown} value
|
|
17
|
+
* @returns {"USD"|"CNY"}
|
|
18
|
+
*/
|
|
19
|
+
export function normalizeCurrency(value) {
|
|
20
|
+
return value === "CNY" ? "CNY" : "USD";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 规整汇率:非有限/非正数回落到默认,夹在 0.1–100。
|
|
25
|
+
* @param {unknown} value
|
|
26
|
+
* @returns {number}
|
|
27
|
+
*/
|
|
28
|
+
export function normalizeRate(value) {
|
|
29
|
+
const n = Number(value);
|
|
30
|
+
if (!Number.isFinite(n) || n <= 0) return DEFAULT_USD_TO_CNY;
|
|
31
|
+
return Math.min(100, Math.max(0.1, n));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 设置币种金额 → USD。`rate` 是 1 USD 等于多少 CNY。
|
|
36
|
+
* @param {unknown} amount
|
|
37
|
+
* @param {unknown} currency
|
|
38
|
+
* @param {unknown} rate
|
|
39
|
+
* @returns {number}
|
|
40
|
+
*/
|
|
41
|
+
export function toUsd(amount, currency, rate) {
|
|
42
|
+
const n = Number(amount);
|
|
43
|
+
if (!Number.isFinite(n)) return 0;
|
|
44
|
+
if (normalizeCurrency(currency) === "USD") return n;
|
|
45
|
+
return n / normalizeRate(rate);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* USD → 设置币种金额。
|
|
50
|
+
* @param {unknown} amountUsd
|
|
51
|
+
* @param {unknown} currency
|
|
52
|
+
* @param {unknown} rate
|
|
53
|
+
* @returns {number}
|
|
54
|
+
*/
|
|
55
|
+
export function fromUsd(amountUsd, currency, rate) {
|
|
56
|
+
const n = Number(amountUsd);
|
|
57
|
+
if (!Number.isFinite(n)) return 0;
|
|
58
|
+
if (normalizeCurrency(currency) === "USD") return n;
|
|
59
|
+
return n * normalizeRate(rate);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 金额显示:USD 用 $,CNY 用 ¥。
|
|
64
|
+
* @param {unknown} amount
|
|
65
|
+
* @param {unknown} currency
|
|
66
|
+
* @param {number} [digits]
|
|
67
|
+
* @returns {string}
|
|
68
|
+
*/
|
|
69
|
+
export function formatMoney(amount, currency, digits = 4) {
|
|
70
|
+
const n = Number(amount);
|
|
71
|
+
const value = Number.isFinite(n) ? n : 0;
|
|
72
|
+
const places = Number.isFinite(digits) ? Math.max(0, Math.min(8, Math.round(digits))) : 4;
|
|
73
|
+
return `${normalizeCurrency(currency) === "CNY" ? "¥" : "$"}${value.toFixed(places)}`;
|
|
74
|
+
}
|
package/lib/service.js
CHANGED
|
@@ -29,10 +29,18 @@ import {
|
|
|
29
29
|
rememberFingerprint,
|
|
30
30
|
} from "./guard.js";
|
|
31
31
|
import { createLedger } from "./ledger.js";
|
|
32
|
+
import { formatMoney, fromUsd } from "./money.js";
|
|
32
33
|
import { parseAdvice, summarizeAdvice, TOOL_DESCRIPTION } from "./prompt.js";
|
|
33
34
|
import { pairingCheck, resolveRoute } from "./route.js";
|
|
34
|
-
import {
|
|
35
|
+
import {
|
|
36
|
+
isConfigured,
|
|
37
|
+
loadSettings,
|
|
38
|
+
mergeSettings,
|
|
39
|
+
saveSettings,
|
|
40
|
+
settingsPath,
|
|
41
|
+
} from "./settings.js";
|
|
35
42
|
import { renderToolOutput, TOOL_NAME, TOOL_OUTPUT_SCHEMA, TOOL_PARAMETERS } from "./tool-schema.js";
|
|
43
|
+
import { buildTracePreview } from "./trace.js";
|
|
36
44
|
import { buildTranscript } from "./transcript.js";
|
|
37
45
|
import { runEvidencePass } from "./twopass.js";
|
|
38
46
|
|
|
@@ -423,7 +431,15 @@ export class AdvisorService extends TypertRemoteService {
|
|
|
423
431
|
turn_limit: `本回合已达上限(${budget.used}/${budget.limit})`,
|
|
424
432
|
session_limit: `本会话已达上限(${budget.used}/${budget.limit})`,
|
|
425
433
|
hour_limit: `本小时已达上限(${budget.used}/${budget.limit})`,
|
|
426
|
-
day_budget:
|
|
434
|
+
day_budget: `今日预算已用完(${formatMoney(
|
|
435
|
+
fromUsd(budget.used, settings.currency, settings.usdToCny),
|
|
436
|
+
settings.currency,
|
|
437
|
+
4,
|
|
438
|
+
)}/${formatMoney(
|
|
439
|
+
fromUsd(budget.limit, settings.currency, settings.usdToCny),
|
|
440
|
+
settings.currency,
|
|
441
|
+
2,
|
|
442
|
+
)})`,
|
|
427
443
|
};
|
|
428
444
|
const reason = reasons[budget.code] ?? "预算不足";
|
|
429
445
|
if (sessionId !== "") this.lastSkip.set(sessionId, reason);
|
|
@@ -447,7 +463,15 @@ export class AdvisorService extends TypertRemoteService {
|
|
|
447
463
|
const reason =
|
|
448
464
|
poolCheck.code === "workspace_calls"
|
|
449
465
|
? `工作区今日调用已达上限(${poolCheck.used}/${poolCheck.limit})`
|
|
450
|
-
:
|
|
466
|
+
: `工作区今日预算已用完(${formatMoney(
|
|
467
|
+
fromUsd(poolCheck.used, settings.currency, settings.usdToCny),
|
|
468
|
+
settings.currency,
|
|
469
|
+
4,
|
|
470
|
+
)}/${formatMoney(
|
|
471
|
+
fromUsd(poolCheck.limit, settings.currency, settings.usdToCny),
|
|
472
|
+
settings.currency,
|
|
473
|
+
2,
|
|
474
|
+
)})`;
|
|
451
475
|
if (sessionId !== "") this.lastSkip.set(sessionId, reason);
|
|
452
476
|
this.counters.skipped += 1;
|
|
453
477
|
return {
|
|
@@ -471,6 +495,9 @@ export class AdvisorService extends TypertRemoteService {
|
|
|
471
495
|
});
|
|
472
496
|
if (transcript.text.trim() === "") return skip("会话里还没有可发送的内容", "empty_transcript");
|
|
473
497
|
|
|
498
|
+
// 只存首尾预览;tracePrompt=off 时连预览都不落盘(requestChars 仍记)。
|
|
499
|
+
const trace = settings.tracePrompt === "preview" ? buildTracePreview(transcript.text) : null;
|
|
500
|
+
|
|
474
501
|
const controller = new AbortController();
|
|
475
502
|
if (sessionId !== "") this.inflight.set(sessionId, controller);
|
|
476
503
|
const signal =
|
|
@@ -518,7 +545,15 @@ export class AdvisorService extends TypertRemoteService {
|
|
|
518
545
|
: 0;
|
|
519
546
|
|
|
520
547
|
const usageLine = call.ok
|
|
521
|
-
? `${formatTokens(call.usage?.inputTokens ?? 0)} in / ${formatTokens(call.usage?.outputTokens ?? 0)} out / ${formatMs(call.ms)}${
|
|
548
|
+
? `${formatTokens(call.usage?.inputTokens ?? 0)} in / ${formatTokens(call.usage?.outputTokens ?? 0)} out / ${formatMs(call.ms)}${
|
|
549
|
+
costUsd > 0
|
|
550
|
+
? ` / ≈${formatMoney(
|
|
551
|
+
fromUsd(costUsd, settings.currency, settings.usdToCny),
|
|
552
|
+
settings.currency,
|
|
553
|
+
4,
|
|
554
|
+
)}`
|
|
555
|
+
: ""
|
|
556
|
+
}`
|
|
522
557
|
: `${formatMs(Date.now() - started)}`;
|
|
523
558
|
|
|
524
559
|
const baseResult = {
|
|
@@ -744,6 +779,13 @@ export class AdvisorService extends TypertRemoteService {
|
|
|
744
779
|
model: result.model,
|
|
745
780
|
usage: result.usage,
|
|
746
781
|
trigger,
|
|
782
|
+
requestChars: transcript.text.length,
|
|
783
|
+
...(trace === null
|
|
784
|
+
? {}
|
|
785
|
+
: {
|
|
786
|
+
requestPreview: trace.preview,
|
|
787
|
+
requestPreviewTruncated: trace.truncated,
|
|
788
|
+
}),
|
|
747
789
|
anchors: result.anchors,
|
|
748
790
|
...(verification === null ? {} : { verification }),
|
|
749
791
|
});
|
|
@@ -1096,7 +1138,20 @@ export class AdvisorService extends TypertRemoteService {
|
|
|
1096
1138
|
`脱敏命中:${transcript.stats.redactions.length === 0 ? "无" : transcript.stats.redactions.join(" / ")}`,
|
|
1097
1139
|
`白名单:${allow.reason}`,
|
|
1098
1140
|
`跨 provider:${crossProvider ? `是(需确认${confirmed ? ",已确认" : ",尚未确认 → /advisor confirm"})` : "否"}`,
|
|
1099
|
-
|
|
1141
|
+
`币种:${settings.currency}${settings.currency === "CNY" ? `(1 USD = ${settings.usdToCny} CNY)` : ""}`,
|
|
1142
|
+
`单价:${
|
|
1143
|
+
price.source === "none"
|
|
1144
|
+
? "未配置(只记 token)"
|
|
1145
|
+
: `输入 ${formatMoney(
|
|
1146
|
+
fromUsd(price.input, settings.currency, settings.usdToCny),
|
|
1147
|
+
settings.currency,
|
|
1148
|
+
2,
|
|
1149
|
+
)}/M · 输出 ${formatMoney(
|
|
1150
|
+
fromUsd(price.output, settings.currency, settings.usdToCny),
|
|
1151
|
+
settings.currency,
|
|
1152
|
+
2,
|
|
1153
|
+
)}/M(${price.source === "builtin" ? "内置参考价(USD 折算)" : "用户设置"})`
|
|
1154
|
+
}`,
|
|
1100
1155
|
`工作区今日:${pool.calls} 次(上限 ${settings.workspaceMaxCallsPerDay || "∞"})`,
|
|
1101
1156
|
``,
|
|
1102
1157
|
`—— 正文前 400 字符 ——`,
|
|
@@ -1345,7 +1400,9 @@ export class AdvisorService extends TypertRemoteService {
|
|
|
1345
1400
|
maxCallsPerTurn: settings.maxCallsPerTurn,
|
|
1346
1401
|
maxCallsPerSession: settings.maxCallsPerSession,
|
|
1347
1402
|
maxCallsPerHour: settings.maxCallsPerHour,
|
|
1348
|
-
|
|
1403
|
+
maxCostPerDay: settings.maxCostPerDay,
|
|
1404
|
+
currency: settings.currency,
|
|
1405
|
+
usdToCny: settings.usdToCny,
|
|
1349
1406
|
},
|
|
1350
1407
|
totals,
|
|
1351
1408
|
last: this.lastResult,
|
|
@@ -1377,6 +1434,7 @@ export class AdvisorService extends TypertRemoteService {
|
|
|
1377
1434
|
last: this.lastResult,
|
|
1378
1435
|
lastSkip: sessionId === "" ? null : (this.lastSkip.get(sessionId) ?? null),
|
|
1379
1436
|
ledgerPath: this.ledger.path,
|
|
1437
|
+
settingsPath: settingsPath(this.dataRoot),
|
|
1380
1438
|
pool: await this.pool.read(),
|
|
1381
1439
|
price: effectivePrice(settings, settings.model),
|
|
1382
1440
|
dataRoot: this.dataRoot,
|
|
@@ -1518,6 +1576,10 @@ export class AdvisorService extends TypertRemoteService {
|
|
|
1518
1576
|
model: String(data?.model ?? ""),
|
|
1519
1577
|
usage: String(data?.usage ?? ""),
|
|
1520
1578
|
at: Number(data?.at ?? 0) || 0,
|
|
1579
|
+
trigger: String(data?.trigger ?? ""),
|
|
1580
|
+
requestChars: Number(data?.requestChars ?? 0) || 0,
|
|
1581
|
+
requestPreview: String(data?.requestPreview ?? ""),
|
|
1582
|
+
requestPreviewTruncated: data?.requestPreviewTruncated === true,
|
|
1521
1583
|
anchors: data?.anchors ?? null,
|
|
1522
1584
|
})),
|
|
1523
1585
|
};
|
|
@@ -1673,7 +1735,19 @@ export class AdvisorService extends TypertRemoteService {
|
|
|
1673
1735
|
: {
|
|
1674
1736
|
id: "budget",
|
|
1675
1737
|
status: "warn",
|
|
1676
|
-
message: `预算受限:${budget.code}(${
|
|
1738
|
+
message: `预算受限:${budget.code}(${
|
|
1739
|
+
budget.code === "day_budget"
|
|
1740
|
+
? `${formatMoney(
|
|
1741
|
+
fromUsd(budget.used, settings.currency, settings.usdToCny),
|
|
1742
|
+
settings.currency,
|
|
1743
|
+
4,
|
|
1744
|
+
)}/${formatMoney(
|
|
1745
|
+
fromUsd(budget.limit, settings.currency, settings.usdToCny),
|
|
1746
|
+
settings.currency,
|
|
1747
|
+
2,
|
|
1748
|
+
)}`
|
|
1749
|
+
: `${budget.used}/${budget.limit}`
|
|
1750
|
+
})`,
|
|
1677
1751
|
},
|
|
1678
1752
|
);
|
|
1679
1753
|
}
|
package/lib/settings.js
CHANGED
|
@@ -5,12 +5,17 @@
|
|
|
5
5
|
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
6
6
|
import { join } from "node:path";
|
|
7
7
|
|
|
8
|
+
import { fromUsd, normalizeCurrency } from "./money.js";
|
|
9
|
+
|
|
8
10
|
/** 设置文件名(位于数据根下)。 */
|
|
9
11
|
export const SETTINGS_FILE = "advisor.settings.json";
|
|
10
12
|
|
|
11
13
|
/** 三种模式:关闭 / 影子(只记账不回灌)/ 顾问(正常徵询)。 */
|
|
12
14
|
export const MODES = Object.freeze(/** @type {const} */ (["off", "shadow", "advisory"]));
|
|
13
15
|
|
|
16
|
+
/** 「记录发送内容」的取值:首尾预览 / 不记录。 */
|
|
17
|
+
export const TRACE_PROMPT_MODES = Object.freeze(/** @type {const} */ (["preview", "off"]));
|
|
18
|
+
|
|
14
19
|
/**
|
|
15
20
|
* 默认设置:**默认关闭**。一个会花钱、会把会话内容发出去的插件必须显式 opt-in。
|
|
16
21
|
* - provider/model 为空时永不发起调用(对齐 Claude「配置 advisor 才生效」)。
|
|
@@ -26,7 +31,9 @@ export const DEFAULT_SETTINGS = Object.freeze({
|
|
|
26
31
|
maxCallsPerTurn: 1,
|
|
27
32
|
maxCallsPerSession: 32,
|
|
28
33
|
maxCallsPerHour: 12,
|
|
29
|
-
|
|
34
|
+
currency: "USD",
|
|
35
|
+
usdToCny: 7.2,
|
|
36
|
+
maxCostPerDay: 0,
|
|
30
37
|
priceInputPerMTok: 0,
|
|
31
38
|
priceOutputPerMTok: 0,
|
|
32
39
|
privacyScope: "summary",
|
|
@@ -39,30 +46,41 @@ export const DEFAULT_SETTINGS = Object.freeze({
|
|
|
39
46
|
confirmedHosts: Object.freeze([]),
|
|
40
47
|
pairingEnforcement: "warn",
|
|
41
48
|
workspaceMaxCallsPerDay: 200,
|
|
42
|
-
|
|
49
|
+
workspaceMaxCostPerDay: 0,
|
|
43
50
|
postAnswer: "off",
|
|
44
51
|
advisorTools: false,
|
|
52
|
+
tracePrompt: "preview",
|
|
45
53
|
timeoutMs: 90000,
|
|
46
54
|
maxInputChars: 60000,
|
|
47
55
|
});
|
|
48
56
|
|
|
49
|
-
/**
|
|
50
|
-
const
|
|
57
|
+
/** 整数类数值字段的夹紧区间。`0` 在预算类字段里表示「不限」。 */
|
|
58
|
+
const INT_LIMITS = Object.freeze({
|
|
51
59
|
maxTokens: [256, 32000],
|
|
52
60
|
maxCallsPerTurn: [0, 8],
|
|
53
61
|
maxCallsPerSession: [0, 1000],
|
|
54
62
|
maxCallsPerHour: [0, 500],
|
|
55
|
-
maxCostPerDayUsd: [0, 10000],
|
|
56
|
-
priceInputPerMTok: [0, 1000],
|
|
57
|
-
priceOutputPerMTok: [0, 1000],
|
|
58
63
|
immuneTurns: [0, 20],
|
|
59
64
|
sessionCap: [0, 200],
|
|
60
65
|
workspaceMaxCallsPerDay: [0, 100000],
|
|
61
|
-
workspaceMaxCostPerDayUsd: [0, 10000],
|
|
62
66
|
timeoutMs: [5000, 600000],
|
|
63
67
|
maxInputChars: [4000, 400000],
|
|
64
68
|
});
|
|
65
69
|
|
|
70
|
+
/**
|
|
71
|
+
* 金额类字段:区间 + 小数位。预算按分(2 位),单价按万分之一(4 位)。
|
|
72
|
+
* 金额不能走整数夹紧——0.5 会被取整成 1,0.15 会变成 0。
|
|
73
|
+
*/
|
|
74
|
+
const MONEY_LIMITS = Object.freeze({
|
|
75
|
+
maxCostPerDay: { min: 0, max: 10000, digits: 2 },
|
|
76
|
+
workspaceMaxCostPerDay: { min: 0, max: 10000, digits: 2 },
|
|
77
|
+
priceInputPerMTok: { min: 0, max: 1000, digits: 4 },
|
|
78
|
+
priceOutputPerMTok: { min: 0, max: 1000, digits: 4 },
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
/** 汇率区间:1 USD = 0.1–100 CNY,保留 4 位小数。 */
|
|
82
|
+
const RATE_LIMIT = Object.freeze({ min: 0.1, max: 100, digits: 4 });
|
|
83
|
+
|
|
66
84
|
/** 字符串字段的最大长度,防止有人在设置里塞进一整段提示词。 */
|
|
67
85
|
const MAX_STRING = 300;
|
|
68
86
|
|
|
@@ -76,19 +94,36 @@ export function settingsPath(home) {
|
|
|
76
94
|
}
|
|
77
95
|
|
|
78
96
|
/**
|
|
79
|
-
*
|
|
97
|
+
* 把数字夹紧到区间并取整(计数字段用)。
|
|
80
98
|
* @param {unknown} value
|
|
81
99
|
* @param {number} fallback
|
|
82
100
|
* @param {number} min
|
|
83
101
|
* @param {number} max
|
|
84
102
|
* @returns {number}
|
|
85
103
|
*/
|
|
86
|
-
function
|
|
104
|
+
function clampInt(value, fallback, min, max) {
|
|
87
105
|
const n = typeof value === "number" ? value : Number(value);
|
|
88
106
|
if (!Number.isFinite(n)) return fallback;
|
|
89
107
|
return Math.min(max, Math.max(min, Math.round(n)));
|
|
90
108
|
}
|
|
91
109
|
|
|
110
|
+
/**
|
|
111
|
+
* 把金额夹紧到区间并按固定小数位收口(金额字段用)。
|
|
112
|
+
* @param {unknown} value
|
|
113
|
+
* @param {number} fallback
|
|
114
|
+
* @param {number} min
|
|
115
|
+
* @param {number} max
|
|
116
|
+
* @param {number} digits
|
|
117
|
+
* @returns {number}
|
|
118
|
+
*/
|
|
119
|
+
function clampMoney(value, fallback, min, max, digits) {
|
|
120
|
+
const n = typeof value === "number" ? value : Number(value);
|
|
121
|
+
if (!Number.isFinite(n)) return fallback;
|
|
122
|
+
const bounded = Math.min(max, Math.max(min, n));
|
|
123
|
+
const factor = 10 ** digits;
|
|
124
|
+
return Math.round(bounded * factor) / factor;
|
|
125
|
+
}
|
|
126
|
+
|
|
92
127
|
/**
|
|
93
128
|
* 夹紧一个可空的数字(`temperature` 用:`null` 表示「不传,让 adapter 决定」)。
|
|
94
129
|
* @param {unknown} value
|
|
@@ -152,6 +187,19 @@ export function normalizeSettings(raw) {
|
|
|
152
187
|
/** @type {any} */
|
|
153
188
|
const out = { ...DEFAULT_SETTINGS };
|
|
154
189
|
|
|
190
|
+
out.currency = normalizeCurrency(input.currency);
|
|
191
|
+
const rawRate = Number(input.usdToCny);
|
|
192
|
+
out.usdToCny =
|
|
193
|
+
Number.isFinite(rawRate) && rawRate > 0
|
|
194
|
+
? clampMoney(
|
|
195
|
+
rawRate,
|
|
196
|
+
DEFAULT_SETTINGS.usdToCny,
|
|
197
|
+
RATE_LIMIT.min,
|
|
198
|
+
RATE_LIMIT.max,
|
|
199
|
+
RATE_LIMIT.digits,
|
|
200
|
+
)
|
|
201
|
+
: DEFAULT_SETTINGS.usdToCny;
|
|
202
|
+
|
|
155
203
|
if (typeof input.mode === "string" && MODES.includes(input.mode)) out.mode = input.mode;
|
|
156
204
|
|
|
157
205
|
for (const key of ["provider", "model", "reasoningEffort"]) {
|
|
@@ -174,9 +222,30 @@ export function normalizeSettings(raw) {
|
|
|
174
222
|
if (typeof input[key] === "boolean") out[key] = input[key];
|
|
175
223
|
}
|
|
176
224
|
|
|
177
|
-
for (const key of /** @type {(keyof typeof
|
|
178
|
-
const [min, max] =
|
|
179
|
-
out[key] =
|
|
225
|
+
for (const key of /** @type {(keyof typeof INT_LIMITS)[]} */ (Object.keys(INT_LIMITS))) {
|
|
226
|
+
const [min, max] = INT_LIMITS[key];
|
|
227
|
+
out[key] = clampInt(input[key], DEFAULT_SETTINGS[key], min, max);
|
|
228
|
+
}
|
|
229
|
+
for (const key of /** @type {(keyof typeof MONEY_LIMITS)[]} */ (Object.keys(MONEY_LIMITS))) {
|
|
230
|
+
if (key === "maxCostPerDay" || key === "workspaceMaxCostPerDay") continue;
|
|
231
|
+
const spec = MONEY_LIMITS[key];
|
|
232
|
+
out[key] = clampMoney(input[key], DEFAULT_SETTINGS[key], spec.min, spec.max, spec.digits);
|
|
233
|
+
}
|
|
234
|
+
// 旧版 *Usd 键按 USD 迁移到当前币种;新键存在时优先。
|
|
235
|
+
/** @type {Array<[keyof typeof MONEY_LIMITS, string]>} */
|
|
236
|
+
const MIGRATE_MONEY = [
|
|
237
|
+
["maxCostPerDay", "maxCostPerDayUsd"],
|
|
238
|
+
["workspaceMaxCostPerDay", "workspaceMaxCostPerDayUsd"],
|
|
239
|
+
];
|
|
240
|
+
for (const [key, oldKey] of MIGRATE_MONEY) {
|
|
241
|
+
const spec = MONEY_LIMITS[key];
|
|
242
|
+
const raw =
|
|
243
|
+
input[key] !== undefined
|
|
244
|
+
? input[key]
|
|
245
|
+
: input[oldKey] === undefined
|
|
246
|
+
? undefined
|
|
247
|
+
: fromUsd(Number(input[oldKey]) || 0, out.currency, out.usdToCny);
|
|
248
|
+
out[key] = clampMoney(raw, DEFAULT_SETTINGS[key], spec.min, spec.max, spec.digits);
|
|
180
249
|
}
|
|
181
250
|
out.temperature = nullableNumber(input.temperature, 0, 2);
|
|
182
251
|
out.tiers = normalizeTiers(input.tiers);
|
|
@@ -186,6 +255,9 @@ export function normalizeSettings(raw) {
|
|
|
186
255
|
out.postAnswer = input.postAnswer;
|
|
187
256
|
}
|
|
188
257
|
if (typeof input.advisorTools === "boolean") out.advisorTools = input.advisorTools;
|
|
258
|
+
if (typeof input.tracePrompt === "string" && TRACE_PROMPT_MODES.includes(input.tracePrompt)) {
|
|
259
|
+
out.tracePrompt = input.tracePrompt;
|
|
260
|
+
}
|
|
189
261
|
|
|
190
262
|
return out;
|
|
191
263
|
}
|
package/lib/trace.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 「查看发送内容」的预览:只存本次实际发送文本的首尾片段。
|
|
3
|
+
//
|
|
4
|
+
// 为什么不存全文:会话记录本身已经包含对话,再复制一份完整 transcript 会放大隐私面和会话体积。
|
|
5
|
+
// 首尾各 2,000 字符足以确认「发了什么」,又不会把整个工作区上下文写进记录。
|
|
6
|
+
// 预览是在 buildTranscript 之后取的,所以已经是脱敏、按 maxInputChars 截断后的文本。
|
|
7
|
+
|
|
8
|
+
/** 首尾各保留的字符数。 */
|
|
9
|
+
export const TRACE_PREVIEW_EDGE = 2000;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 生成待存的首尾预览。
|
|
13
|
+
* - 总长度不超过 `edge * 2`:原样返回(短会话不需要省略)。
|
|
14
|
+
* - 超过:保留前 `edge` 与后 `edge` 字符,中间用 `…` 标记;`chars` 始终是完整长度。
|
|
15
|
+
* @param {unknown} text
|
|
16
|
+
* @param {number} [edge]
|
|
17
|
+
* @returns {{ preview: string, chars: number, truncated: boolean }}
|
|
18
|
+
*/
|
|
19
|
+
export function buildTracePreview(text, edge = TRACE_PREVIEW_EDGE) {
|
|
20
|
+
const value = String(text ?? "");
|
|
21
|
+
const chars = value.length;
|
|
22
|
+
const limit = Math.max(1, Math.floor(Number.isFinite(edge) ? edge : TRACE_PREVIEW_EDGE));
|
|
23
|
+
if (chars <= limit * 2) {
|
|
24
|
+
return { preview: value, chars, truncated: false };
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
preview: `${value.slice(0, limit)}\n\n…\n\n${value.slice(-limit)}`,
|
|
28
|
+
chars,
|
|
29
|
+
truncated: true,
|
|
30
|
+
};
|
|
31
|
+
}
|