dsh-desktop-enhance 0.9.6 → 0.9.8

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
@@ -85,6 +85,10 @@ Web 版(浏览器里打开的那种)**不提供插件查找的默认快捷
85
85
  改坏了有「恢复默认」一键还原。与出厂模板**一字不差**时会存成空串(也就是「用默认」),所以恢复默认之后配置是干净的。
86
86
 
87
87
 
88
+ ### 图片 token 估价
89
+
90
+ 默认补齐未声明图片价格的模型路由,让上下文计量与自动压缩保留预算计入图片视觉 tokens;已有适配器定价优先。没有独立界面开关,可通过插件 Config 的 `imagePricing: false` 停用。估价不改 provider usage,也不保证压后固定占比。安装此 Host 变更后需完整重启应用;规则与限制见[图片 token 估价](docs/features/image-pricing.md)。
91
+
88
92
  ### 每步耗时
89
93
 
90
94
  进程里每一步**几点开始的、花了多久**,直接跟在这一行的文字后面(`14:32 · 23s`):
package/lib/index.d.ts CHANGED
@@ -7,6 +7,13 @@ interface RequestEvent {
7
7
  readonly data: unknown;
8
8
  }
9
9
  //#endregion
10
+ //#region src/image-pricing/host.d.ts
11
+ declare function imageTokensForModel(model: string, width: number, height: number): number | null;
12
+ declare function readImagePricingEnabled(config: unknown): boolean;
13
+ declare function installImagePricing(ctx: {
14
+ get(name: string): unknown;
15
+ }, enabled?: () => boolean): () => void;
16
+ //#endregion
10
17
  //#region src/shared/session-state.d.ts
11
18
  interface SessionPolicyDelta {
12
19
  readonly allow: readonly string[];
@@ -736,6 +743,7 @@ export declare const Config: Schema<Schemastery.ObjectS<NoInfer<{
736
743
  disabledSkills: Schema<string, string, "volatile-defined">;
737
744
  contextManager: Schema<boolean, boolean, "volatile-defined">;
738
745
  skillRecovery: Schema<boolean, boolean, "volatile-defined">;
746
+ imagePricing: Schema<boolean, boolean, "volatile-defined">;
739
747
  diagnosticsRetentionDays: Schema<number, number, "defined">;
740
748
  diagnosticsMaxMiB: Schema<number, number, "defined">;
741
749
  }>>, Schemastery.ObjectT<NoInfer<{
@@ -759,11 +767,15 @@ export declare const Config: Schema<Schemastery.ObjectS<NoInfer<{
759
767
  disabledSkills: Schema<string, string, "volatile-defined">;
760
768
  contextManager: Schema<boolean, boolean, "volatile-defined">;
761
769
  skillRecovery: Schema<boolean, boolean, "volatile-defined">;
770
+ imagePricing: Schema<boolean, boolean, "volatile-defined">;
762
771
  diagnosticsRetentionDays: Schema<number, number, "defined">;
763
772
  diagnosticsMaxMiB: Schema<number, number, "defined">;
764
773
  }>>, "plain">;
765
774
  export declare function apply(ctx: ToolsHostContext, config?: unknown): void;
766
775
  export declare const __test: {
776
+ imageTokensForModel: typeof imageTokensForModel;
777
+ installImagePricing: typeof installImagePricing;
778
+ readImagePricingEnabled: typeof readImagePricingEnabled;
767
779
  createUpdateEngine: typeof createUpdateEngine;
768
780
  productionDeps: typeof productionDeps;
769
781
  readInstalledVersion: typeof readInstalledVersion;
package/lib/index.js CHANGED
@@ -19,6 +19,145 @@ function applyPolicyDelta(global, delta) {
19
19
  return effective;
20
20
  }
21
21
  //#endregion
22
+ //#region src/image-pricing/host.ts
23
+ const PATCH_SIZE = 14;
24
+ const DOWNSAMPLE_RATIO = 3;
25
+ const MAX_IMAGE_TOKENS = 1024;
26
+ const MIN_PIXELS = 295936;
27
+ const CELL_SIZE = 42;
28
+ const intDiv = (value, divisor) => Math.floor(value / divisor);
29
+ const ceilDiv = (value, divisor) => Math.floor((value + divisor - 1) / divisor);
30
+ const gridCells = (paddedLength) => ceilDiv(intDiv(paddedLength, PATCH_SIZE), DOWNSAMPLE_RATIO);
31
+ const gridTokens = (height, width) => height * (width + 1) + 2;
32
+ function solveResizeRatio(height, width) {
33
+ const aspect = height / width;
34
+ const idealGridWidth = Math.sqrt(1022 / aspect + .25) - .5;
35
+ const idealGridHeight = idealGridWidth * aspect;
36
+ let bestHeight;
37
+ let bestWidth;
38
+ if (idealGridWidth < 1) {
39
+ bestWidth = CELL_SIZE;
40
+ bestHeight = intDiv(1022, 2) * CELL_SIZE;
41
+ } else if (idealGridHeight < 1) {
42
+ bestWidth = 1021 * CELL_SIZE;
43
+ bestHeight = CELL_SIZE;
44
+ } else {
45
+ const scale = Math.min(Math.trunc(idealGridWidth) * CELL_SIZE / width, Math.trunc(idealGridHeight) * CELL_SIZE / height);
46
+ bestWidth = Math.trunc(width * scale / PATCH_SIZE) * PATCH_SIZE;
47
+ bestHeight = Math.trunc(height * scale / PATCH_SIZE) * PATCH_SIZE;
48
+ }
49
+ return {
50
+ bestHeight,
51
+ bestWidth,
52
+ gridHeight: gridCells(bestHeight),
53
+ gridWidth: gridCells(bestWidth)
54
+ };
55
+ }
56
+ function resizeOnce(width, height) {
57
+ let scaledWidth = width;
58
+ let scaledHeight = height;
59
+ const pixels = width * height;
60
+ if (pixels < MIN_PIXELS && pixels > 0) {
61
+ const scale = Math.sqrt(MIN_PIXELS / pixels);
62
+ scaledWidth = Math.trunc(width * scale);
63
+ scaledHeight = Math.trunc(height * scale);
64
+ }
65
+ const paddedWidth = ceilDiv(scaledWidth, PATCH_SIZE) * PATCH_SIZE;
66
+ const paddedHeight = ceilDiv(scaledHeight, PATCH_SIZE) * PATCH_SIZE;
67
+ const direct = {
68
+ bestHeight: paddedHeight,
69
+ bestWidth: paddedWidth,
70
+ gridHeight: gridCells(paddedHeight),
71
+ gridWidth: gridCells(paddedWidth)
72
+ };
73
+ if (gridTokens(direct.gridHeight, direct.gridWidth) <= MAX_IMAGE_TOKENS) return direct;
74
+ const solved = solveResizeRatio(scaledHeight, scaledWidth);
75
+ if (gridTokens(solved.gridHeight, solved.gridWidth) > MAX_IMAGE_TOKENS) throw new Error(`deepseek image tokens: no grid fits the token budget for ${width}x${height}`);
76
+ return solved;
77
+ }
78
+ function deepSeekImageTokens(width, height) {
79
+ let result = resizeOnce(width, height);
80
+ for (let iteration = 1; iteration < 10; iteration += 1) {
81
+ const next = resizeOnce(result.bestWidth, result.bestHeight);
82
+ if (next.gridHeight === result.gridHeight && next.gridWidth === result.gridWidth && next.bestHeight === result.bestHeight && next.bestWidth === result.bestWidth) return gridTokens(result.gridHeight, result.gridWidth);
83
+ result = next;
84
+ }
85
+ throw new Error(`deepseek image tokens: resize did not converge for ${width}x${height}`);
86
+ }
87
+ const IMAGE_RULES = [
88
+ [/deepseek/i, deepSeekImageTokens],
89
+ [/qwen/i, (width, height) => Math.ceil(width / 32) * Math.ceil(height / 32)],
90
+ [/seed|doubao/i, (width, height) => Math.min(Math.round(width * height / 1764), 1280)],
91
+ [/^gemini/i, (width, height) => Math.ceil(width / 768) * Math.ceil(height / 768) * 258],
92
+ [/^gpt-6/i, (width, height) => Math.ceil(Math.ceil(width / 32) * Math.ceil(height / 32) * 1.2)]
93
+ ];
94
+ const ruleForModel = (model) => IMAGE_RULES.find(([pattern]) => pattern.test(model))?.[1];
95
+ function imageTokensForModel(model, width, height) {
96
+ const rule = ruleForModel(model);
97
+ if (!rule) return null;
98
+ return tokensForDimensions(rule, width, height);
99
+ }
100
+ function tokensForDimensions(rule, width, height) {
101
+ if (!Number.isSafeInteger(width) || width <= 0 || !Number.isSafeInteger(height) || height <= 0) throw new RangeError(`image pricing: invalid dimensions ${width}x${height}`);
102
+ return rule(width, height);
103
+ }
104
+ function readImagePricingEnabled(config) {
105
+ if (config === null || typeof config !== "object") return true;
106
+ const raw = config.imagePricing;
107
+ if (typeof raw === "boolean") return raw;
108
+ if (raw !== null && typeof raw === "object" && typeof raw.get === "function") try {
109
+ const value = raw.get();
110
+ return typeof value === "boolean" ? value : true;
111
+ } catch {
112
+ return true;
113
+ }
114
+ return true;
115
+ }
116
+ const installedPricing = new WeakSet();
117
+ function installImagePricing(ctx, enabled = () => true) {
118
+ const llm = ctx.get("llm");
119
+ if (!llm) return () => {};
120
+ const previous = Object.getOwnPropertyDescriptor(llm, "imageRequestPricing");
121
+ if (previous && installedPricing.has(previous.value)) return () => {};
122
+ const basePricing = llm.imageRequestPricing;
123
+ const replacement = function(provider, model) {
124
+ const declared = basePricing.call(this, provider, model);
125
+ if (declared !== void 0 || !installedPricing.has(replacement) || !enabled()) return declared;
126
+ const rule = ruleForModel(model);
127
+ if (!rule) return void 0;
128
+ return { priceImages: (images) => images.map((image) => {
129
+ if (image.offloaded === true) return {
130
+ visualTokens: 0,
131
+ text: ""
132
+ };
133
+ let target = {
134
+ width: image.attachment.width,
135
+ height: image.attachment.height
136
+ };
137
+ try {
138
+ target = llm.imageRequestTarget(provider, model, image.attachment);
139
+ } catch {}
140
+ return {
141
+ visualTokens: tokensForDimensions(rule, target.width, target.height),
142
+ text: ""
143
+ };
144
+ }) };
145
+ };
146
+ Object.defineProperty(llm, "imageRequestPricing", {
147
+ value: replacement,
148
+ configurable: true,
149
+ writable: true,
150
+ enumerable: previous?.enumerable ?? false
151
+ });
152
+ installedPricing.add(replacement);
153
+ return () => {
154
+ installedPricing.delete(replacement);
155
+ if (Object.getOwnPropertyDescriptor(llm, "imageRequestPricing")?.value !== replacement) return;
156
+ if (previous) Object.defineProperty(llm, "imageRequestPricing", previous);
157
+ else delete llm.imageRequestPricing;
158
+ };
159
+ }
160
+ //#endregion
22
161
  //#region src/shared/route.ts
23
162
  const SUGGEST_ROUTE = "/api/desktop-enhance/suggest";
24
163
  const SUGGEST_MODELS_ROUTE = "/api/desktop-enhance/suggest-models";
@@ -5770,6 +5909,7 @@ const Config = Schema.object({
5770
5909
  disabledSkills: Schema.string().default("").volatile(),
5771
5910
  contextManager: Schema.boolean().default(true).volatile(),
5772
5911
  skillRecovery: Schema.boolean().default(true).volatile(),
5912
+ imagePricing: Schema.boolean().default(true).volatile(),
5773
5913
  diagnosticsRetentionDays: Schema.number().min(1).max(30).step(1).default(7),
5774
5914
  diagnosticsMaxMiB: Schema.number().min(4).max(256).step(1).default(64)
5775
5915
  });
@@ -5781,6 +5921,9 @@ function apply(ctx, config) {
5781
5921
  ctx.inject(["sessionProjections"], (serviceCtx) => {
5782
5922
  serviceCtx.get("sessionProjections").register(sessionModelProjection);
5783
5923
  });
5924
+ ctx.inject(["llm"], (serviceCtx) => {
5925
+ serviceCtx.effect(() => installImagePricing(serviceCtx, () => readImagePricingEnabled(config)), "desktop-enhance: image pricing");
5926
+ });
5784
5927
  ctx.inject(["connection"], (serviceCtx) => {
5785
5928
  try {
5786
5929
  serviceCtx.effect(() => registerSuggestRoute(serviceCtx), "desktop-enhance: suggest route");
@@ -5920,6 +6063,9 @@ function apply(ctx, config) {
5920
6063
  });
5921
6064
  }
5922
6065
  const __test = {
6066
+ imageTokensForModel,
6067
+ installImagePricing,
6068
+ readImagePricingEnabled,
5923
6069
  createUpdateEngine,
5924
6070
  productionDeps,
5925
6071
  readInstalledVersion,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-desktop-enhance",
3
- "version": "0.9.6",
3
+ "version": "0.9.8",
4
4
  "description": "DSH Desktop 界面增强:页内查找(Cmd+F)、轮次编号、上下文压缩行常显、建议、每步耗时、折叠文件夹状态计数、会话信息(含上下文注入 C1 视图)、工具管理、Skill 管理、独立远程接入与手机体验,以及智能建议、会话能力和远程接入的持久诊断日志。",
5
5
  "type": "module",
6
6
  "icon": "./icon.svg",