dsh-model-think-level 2.0.2 → 2.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/README.md +49 -23
- package/lib/balance.d.ts +44 -0
- package/lib/client/ComposerSlider.d.ts +19 -0
- package/lib/client/ConfigPane.d.ts +47 -0
- package/lib/client/EffortEditor.d.ts +34 -16
- package/lib/client/KeyManager.d.ts +2 -2
- package/lib/client/balance-client.d.ts +34 -0
- package/lib/client/capacity.d.ts +20 -4
- package/lib/client/configured-inputs.d.ts +11 -2
- package/lib/client/events.d.ts +31 -2
- package/lib/client/fingerprint-runner.d.ts +13 -1
- package/lib/client/image-config-client.d.ts +75 -0
- package/lib/client/injection/autofill-run.d.ts +2 -2
- package/lib/client/injection/config-button-seat.d.ts +96 -0
- package/lib/client/injection/create-key-seat.d.ts +3 -3
- package/lib/client/injection/create-keys-pane.d.ts +4 -2
- package/lib/client/injection/editor-tab-bar.d.ts +1 -1
- package/lib/client/injection/editor-tabs.d.ts +1 -1
- package/lib/client/injection/image-card.d.ts +85 -0
- package/lib/client/injection/imagegen-completion.d.ts +43 -0
- package/lib/client/injection/imagegen-entry.d.ts +22 -0
- package/lib/client/injection/key-prefetch.d.ts +44 -0
- package/lib/client/injection/key-reveal.d.ts +2 -3
- package/lib/client/injection/models-page-editor.d.ts +59 -28
- package/lib/client/injection/models-page.d.ts +15 -3
- package/lib/client/injection/provider-balance.d.ts +83 -0
- package/lib/client/injection/provider-endpoints.d.ts +2 -2
- package/lib/client/injection/row-drag.d.ts +7 -0
- package/lib/client/injection/staged-keys.d.ts +24 -12
- package/lib/client/key-manager-client.d.ts +70 -18
- package/lib/client/locales.d.ts +65 -15
- package/lib/client/ops.d.ts +12 -2
- package/lib/client/styles.d.ts +1 -1
- package/lib/client/types.d.ts +21 -3
- package/lib/client.js +721 -78
- package/lib/client.js.map +4 -4
- package/lib/constants.d.ts +58 -0
- package/lib/detection.d.ts +7 -0
- package/lib/fingerprint/estimate.d.ts +16 -0
- package/lib/fingerprint/run.d.ts +24 -0
- package/lib/fingerprint/store.d.ts +2 -2
- package/lib/fingerprint/verdict.d.ts +53 -3
- package/lib/headers-core.d.ts +3 -1
- package/lib/headers-fetch.d.ts +25 -0
- package/lib/image/command.d.ts +84 -0
- package/lib/image/config.d.ts +150 -0
- package/lib/image/generate.d.ts +135 -0
- package/lib/image/legacy-cleanup.d.ts +29 -0
- package/lib/image/sizes.d.ts +28 -0
- package/lib/image/tool.d.ts +107 -0
- package/lib/index.js +3876 -1745
- package/lib/index.js.map +4 -4
- package/lib/key-index.d.ts +37 -7
- package/lib/key-probe.d.ts +39 -99
- package/lib/key-refs.d.ts +43 -19
- package/lib/key-route-core.d.ts +100 -32
- package/lib/key-serves.d.ts +81 -0
- package/lib/knowledge.d.ts +21 -0
- package/lib/listing.d.ts +18 -20
- package/lib/multi-key-listing.d.ts +3 -3
- package/package.json +1 -8
package/lib/constants.d.ts
CHANGED
|
@@ -41,6 +41,18 @@ export declare const PROVIDER_KEY_PATH = "/dsh-model-think-level/provider-key";
|
|
|
41
41
|
* never needs a new Remote face.
|
|
42
42
|
*/
|
|
43
43
|
export declare const KEY_INDEX_PATH = "/dsh-model-think-level/key-index";
|
|
44
|
+
/**
|
|
45
|
+
* Same-origin host route that reads one provider's balance.
|
|
46
|
+
*
|
|
47
|
+
* The URL itself is configured per route in the plugin's key index (the
|
|
48
|
+
* `balances` map) and is queried host-side, because only the host can resolve
|
|
49
|
+
* the route's stored API key for the upstream call and the page would otherwise
|
|
50
|
+
* face CORS on a third-party balance API. The route answers
|
|
51
|
+
* `{ok, configured:false}` when the provider has no balance endpoint at all, so
|
|
52
|
+
* the browser half can tell "not configured" (show nothing) from "configured but
|
|
53
|
+
* unreadable" (show nothing but keep the endpoint).
|
|
54
|
+
*/
|
|
55
|
+
export declare const BALANCE_PATH = "/dsh-model-think-level/balance";
|
|
44
56
|
/**
|
|
45
57
|
* Same-origin host route that reports this plugin's autofill switches. The
|
|
46
58
|
* browser half runs the running auto-fill complement, and a `dsh.client`
|
|
@@ -84,6 +96,19 @@ export declare const UNSET_MARKER = "reasoningEffortsUnset";
|
|
|
84
96
|
* `input` as a decision (the route default applies), not as a gap to fill.
|
|
85
97
|
*/
|
|
86
98
|
export declare const INPUT_UNSET_MARKER = "inputUnset";
|
|
99
|
+
/**
|
|
100
|
+
* Model-level marker declaring the row an IMAGE-GENERATION model (生图).
|
|
101
|
+
*
|
|
102
|
+
* The chat wire's own `input` vocabulary (pi-ai's MODALITY_GATE) only knows
|
|
103
|
+
* text/image — the request modalities a model ACCEPTS — and an image generator
|
|
104
|
+
* is not a request-modality answer at all: it is a different kind of model that
|
|
105
|
+
* the composer's model switcher must hide (its entries cannot serve a chat
|
|
106
|
+
* turn), so it lives beside the declaration as its own marker. Schemastery
|
|
107
|
+
* passes unknown model keys through, so the marker survives official-page saves
|
|
108
|
+
* and restarts like the other three; absence means "not an image generator",
|
|
109
|
+
* and `false`/absent are the same state.
|
|
110
|
+
*/
|
|
111
|
+
export declare const INPUT_IMAGE_GEN_MARKER = "inputImageGen";
|
|
87
112
|
/**
|
|
88
113
|
* Model-level provenance marker written beside every ladder the HOST autofill
|
|
89
114
|
* fills from the knowledge base, carrying the settings revision the fill read.
|
|
@@ -117,5 +142,38 @@ export declare const AUTOFILL_MARKER = "reasoningEffortsAutofilled";
|
|
|
117
142
|
* so a future first-class field can adopt the stored values as-is.
|
|
118
143
|
*/
|
|
119
144
|
export declare const DEFAULT_EFFORT_FIELD = "defaultEffort";
|
|
145
|
+
/**
|
|
146
|
+
* Same-origin host route owning the image-generation config surface: `GET`
|
|
147
|
+
* answers the derived image-model list merged with the stored overrides,
|
|
148
|
+
* `POST` saves default model / default params / output dir / per-model rows.
|
|
149
|
+
* Backing file: `<DSH_HOME>/dsh-model-think-level/image.json` (see
|
|
150
|
+
* {@link IMAGE_CONFIG_FILE}).
|
|
151
|
+
*/
|
|
152
|
+
export declare const IMAGE_CONFIG_PATH = "/dsh-model-think-level/image-config";
|
|
153
|
+
/**
|
|
154
|
+
* Same-origin host route that runs one image generation through the shared
|
|
155
|
+
* core (`src/image/generate.ts`): the explicit entry beside the native
|
|
156
|
+
* `generate_image` tool and the `/生图` host command — used for scripted calls
|
|
157
|
+
* and for callers that want the file paths back.
|
|
158
|
+
*/
|
|
159
|
+
export declare const IMAGE_GENERATE_PATH = "/dsh-model-think-level/image-generate";
|
|
160
|
+
/** File name of the image-generation config inside the plugin's home dir. */
|
|
161
|
+
export declare const IMAGE_CONFIG_FILE = "image.json";
|
|
162
|
+
/**
|
|
163
|
+
* The patch-layer id the RETIRED image-MCP entry carried. No entry is written
|
|
164
|
+
* any more — the native `generate_image` tool replaced the stdio server — but
|
|
165
|
+
* the id and the marker below still matter: the boot-time migration
|
|
166
|
+
* (`src/image/legacy-cleanup.ts`) recognizes and removes a leftover block, so
|
|
167
|
+
* an upgraded profile stops spawning the now-missing `lib/image-mcp.mjs` and
|
|
168
|
+
* stops exposing the duplicate `mcp__dsh-image__generate_image`.
|
|
169
|
+
*/
|
|
170
|
+
export declare const IMAGE_MCP_PATCH_ID = "dsh-image-mcp";
|
|
171
|
+
/**
|
|
172
|
+
* Marker-block name the retired installer used inside `cordis.patch.yml`. The
|
|
173
|
+
* begin/end comment lines are `# >>> <name>:begin` / `# <<< <name>:end`; the
|
|
174
|
+
* legacy cleanup splices exactly that byte range, so third-party marker blocks
|
|
175
|
+
* (the dsh-skill-mcp-panel precedent) survive untouched.
|
|
176
|
+
*/
|
|
177
|
+
export declare const IMAGE_MCP_PATCH_MARK = "dsh-model-think-level:image-mcp";
|
|
120
178
|
/** Locale dictionary namespace for the browser half's copy (not a settings namespace). */
|
|
121
179
|
export declare const STORE_NS = "dsh-model-think-level";
|
package/lib/detection.d.ts
CHANGED
|
@@ -32,6 +32,13 @@ export interface EndpointSignal {
|
|
|
32
32
|
* layer owns the intersection with the core vocabulary.
|
|
33
33
|
*/
|
|
34
34
|
input?: readonly string[];
|
|
35
|
+
/**
|
|
36
|
+
* The listing's output-modality disclosure, lowercased, when it named one
|
|
37
|
+
* (OpenRouter's `architecture.output_modalities` and its flattened copies).
|
|
38
|
+
* This is the disclosure that answers "is this an image generator": a model
|
|
39
|
+
* whose listing outputs images and not text cannot serve a chat turn.
|
|
40
|
+
*/
|
|
41
|
+
output?: readonly string[];
|
|
35
42
|
/** Context length the listing disclosed, when it carries one. */
|
|
36
43
|
contextLength?: number;
|
|
37
44
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { FingerprintRecord } from './verdict.js';
|
|
2
|
+
/** 提示里的时长:秒按 5 秒取整,过了 75 秒改报整分钟。 */
|
|
3
|
+
export type RunEstimate = {
|
|
4
|
+
unit: 'seconds';
|
|
5
|
+
value: number;
|
|
6
|
+
} | {
|
|
7
|
+
unit: 'minutes';
|
|
8
|
+
value: number;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* 估一次完整检测(`FULL_RUN_QUERIES` 题)大概要等多久。
|
|
12
|
+
*
|
|
13
|
+
* @param records - 同一家供应商已落盘的结论,顺序任意,缺 timing 的跳过。
|
|
14
|
+
* @returns 没有任何可参考的耗时时返回 `undefined`。
|
|
15
|
+
*/
|
|
16
|
+
export declare function estimateRun(records: readonly FingerprintRecord[]): RunEstimate | undefined;
|
package/lib/fingerprint/run.d.ts
CHANGED
|
@@ -51,6 +51,19 @@ export interface ProbeOutcome {
|
|
|
51
51
|
error?: string;
|
|
52
52
|
/** Why the stream stopped, when one was reported. */
|
|
53
53
|
finish?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Milliseconds from dispatch start to the first visible text chunk. Absent
|
|
56
|
+
* when no text arrived (rejected, cancelled, empty answer).
|
|
57
|
+
*/
|
|
58
|
+
firstTokenMs?: number;
|
|
59
|
+
/** Total wall time of the challenge, milliseconds. */
|
|
60
|
+
durationMs?: number;
|
|
61
|
+
/**
|
|
62
|
+
* Aborted BY the early exit: two confident answers already closed the run, so
|
|
63
|
+
* this challenge was never needed. Without the bit a caller cannot tell a
|
|
64
|
+
* deliberate skip from a user cancellation — both come back `cancelled`.
|
|
65
|
+
*/
|
|
66
|
+
skipped?: boolean;
|
|
54
67
|
}
|
|
55
68
|
/** Everything one model's fingerprint run learned, ready for the bank. */
|
|
56
69
|
export interface ProbeRunResult {
|
|
@@ -84,8 +97,19 @@ export interface RunProbeOptions {
|
|
|
84
97
|
/**
|
|
85
98
|
* Called as each challenge settles, so a caller can stream progress. Fire
|
|
86
99
|
* order is completion order, which for parallel runs is not `index` order.
|
|
100
|
+
* Challenges aborted by {@link shouldStop} do NOT fire this: they never
|
|
101
|
+
* answered, so they are not progress.
|
|
87
102
|
*/
|
|
88
103
|
onOutcome?: (outcome: ProbeOutcome) => void;
|
|
104
|
+
/**
|
|
105
|
+
* Early-exit hook, called after each challenge settles with every outcome
|
|
106
|
+
* so far. Returning true aborts every challenge still in flight; their
|
|
107
|
+
* outcomes come back cancelled and stay out of `onOutcome` and of any
|
|
108
|
+
* caller-side progress accounting. This is what lets two confident answers
|
|
109
|
+
* close the run before the slowest challenge lands — aborting the tail is
|
|
110
|
+
* the whole point of firing all three at once.
|
|
111
|
+
*/
|
|
112
|
+
shouldStop?: (outcomes: readonly ProbeOutcome[]) => boolean;
|
|
89
113
|
}
|
|
90
114
|
/** Default per-challenge timeout: two minutes is generous for 300 integers. */
|
|
91
115
|
export declare const DEFAULT_PROBE_TIMEOUT_MS = 120000;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type FingerprintRecord, type FingerprintStatus, type FingerprintTopEntry } from './verdict.js';
|
|
2
|
-
export { type FingerprintRecord, type FingerprintStatus, type FingerprintTopEntry };
|
|
1
|
+
import { type FingerprintChallenge, type FingerprintRecord, type FingerprintStatus, type FingerprintTiming, type FingerprintTopEntry } from './verdict.js';
|
|
2
|
+
export { type FingerprintChallenge, type FingerprintRecord, type FingerprintStatus, type FingerprintTiming, type FingerprintTopEntry, };
|
|
3
3
|
export interface FingerprintDocument {
|
|
4
4
|
version: 1;
|
|
5
5
|
/** 银行的 built_at;换银行就整体失效。 */
|
|
@@ -1,12 +1,47 @@
|
|
|
1
1
|
import type { AttributionResult, ResultRow } from './core.js';
|
|
2
2
|
/** 落盘的三种状态。`running` 不落盘,只活在客户端 runner 里。 */
|
|
3
3
|
export type FingerprintStatus = 'identified' | 'doubtful' | 'failed';
|
|
4
|
+
/**
|
|
5
|
+
* 检测的耗时画像(可选):给结果卡一句「慢在哪」。只收统计量 —— 首 token
|
|
6
|
+
* 等待、整次墙钟、按「总时长 − 首 token 等待」折出的吐字速度;原始文本
|
|
7
|
+
* 从不落盘(design §4.2)。
|
|
8
|
+
*/
|
|
9
|
+
export interface FingerprintTiming {
|
|
10
|
+
/** 有效回答里最慢的首 token 等待,毫秒。 */
|
|
11
|
+
firstTokenMs?: number;
|
|
12
|
+
/** 有效回答里最慢的整题耗时(墙钟 ≈ 用户等的时间),毫秒。 */
|
|
13
|
+
durationMs?: number;
|
|
14
|
+
/** 有效回答合计的可见字符 ÷ 合计纯吐字时长,字符/秒。 */
|
|
15
|
+
charsPerSecond?: number;
|
|
16
|
+
}
|
|
4
17
|
/** 结果卡「前三候选」里的一条。 */
|
|
5
18
|
export interface FingerprintTopEntry {
|
|
6
19
|
model: string;
|
|
7
20
|
probability: number;
|
|
8
21
|
similarity: number;
|
|
9
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* 结果卡「逐题」那一行里的一颗药丸:一道题问了多少、答回来多少、算不算数。
|
|
25
|
+
*
|
|
26
|
+
* 为什么落盘:`usedOutputs` 只说「三题里算了几题」,说不出是哪一题不算 ——
|
|
27
|
+
* 而「为什么只回答了部分题目」正是用户看着 `2 / 3 题` 想问的那句话。逐题的
|
|
28
|
+
* 期望数与解析数都是统计量,不涉及作答文本(design §4.2)。
|
|
29
|
+
*/
|
|
30
|
+
export interface FingerprintChallenge {
|
|
31
|
+
/** 题号,从 1 开始,与题干发出顺序一致。 */
|
|
32
|
+
index: number;
|
|
33
|
+
/** 这道题要求几个整数(292–332)。 */
|
|
34
|
+
expected: number;
|
|
35
|
+
/** 解析出来的整数个数;拒答、报错、被掐断都是 0。 */
|
|
36
|
+
parsed: number;
|
|
37
|
+
/** 是否达到了计入门槛(`max(80, ceil(expected * 0.55))`)。 */
|
|
38
|
+
accepted: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* 两题早停时被主动掐掉的那道题。它没答不是因为答坏了 —— 药丸上要说的
|
|
41
|
+
* 是「早停跳过」,不是「未计入」,否则用户会去追一个不存在的失败。
|
|
42
|
+
*/
|
|
43
|
+
skipped?: boolean;
|
|
44
|
+
}
|
|
10
45
|
/**
|
|
11
46
|
* 一个模型的一次检测结论,也是落盘的那一行。
|
|
12
47
|
*
|
|
@@ -27,10 +62,14 @@ export interface FingerprintRecord {
|
|
|
27
62
|
familyPrediction?: string;
|
|
28
63
|
familyProbability?: number;
|
|
29
64
|
top?: FingerprintTopEntry[];
|
|
65
|
+
/** 逐题记账:哪几题算进去了、哪题没算。旧记录没有这个字段。 */
|
|
66
|
+
challenges?: FingerprintChallenge[];
|
|
30
67
|
calibration?: {
|
|
31
68
|
queries: number;
|
|
32
69
|
cvAccuracy: number;
|
|
33
70
|
};
|
|
71
|
+
/** 本次检测的耗时画像,检测失败时没有意义,不落盘。 */
|
|
72
|
+
timing?: FingerprintTiming;
|
|
34
73
|
/** 仅 failed 有值,面向用户的失败原因。 */
|
|
35
74
|
error?: string;
|
|
36
75
|
}
|
|
@@ -43,6 +82,16 @@ export interface FingerprintRecord {
|
|
|
43
82
|
export declare const IDENTIFIED_THRESHOLD = 0.6;
|
|
44
83
|
/** 出一个确定结论需要的完整题数。 */
|
|
45
84
|
export declare const FULL_RUN_QUERIES = 3;
|
|
85
|
+
/**
|
|
86
|
+
* 两题早停判 `identified` 的概率下界。
|
|
87
|
+
*
|
|
88
|
+
* 银行自带 2 题校准:交叉验证准确率 99.7%(646/648),beta≈12 让概率分布
|
|
89
|
+
* 非常尖。0.9 在那套校准下仍是保守门槛 —— 池化概率到不了 0.9 就继续等第三
|
|
90
|
+
* 题,走三题的完整判据,所以早停只会提前、不会放宽。
|
|
91
|
+
*/
|
|
92
|
+
export declare const EARLY_IDENTIFIED_PROBABILITY = 0.9;
|
|
93
|
+
/** 早停最少需要的有效题数(两题)。 */
|
|
94
|
+
export declare const EARLY_IDENTIFIED_QUERIES = 2;
|
|
46
95
|
/**
|
|
47
96
|
* 取概率最高的前几名候选,供结果卡画条形。
|
|
48
97
|
* @param rows - 已按概率降序排好的候选。
|
|
@@ -53,9 +102,10 @@ export declare function topEntriesOf(rows: readonly ResultRow[], limit?: number)
|
|
|
53
102
|
/**
|
|
54
103
|
* 把一次归属结论定级。
|
|
55
104
|
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
105
|
+
* 三道闸:概率必须够高,题数必须够多,多到哪一档就用哪一档的门槛。三题全有
|
|
106
|
+
* 效走 0.6;两题有效走 0.9(早停或第三题意外作废时,银行 2 题校准的交叉验证
|
|
107
|
+
* 准确率 99.7% 撑得起,但门槛相应收紧);1 题的交叉验证准确率只有 96.3%,
|
|
108
|
+
* 撑不起任何确定结论 —— 一律 `doubtful`,哪怕那一题的概率是 0.99。
|
|
59
109
|
*
|
|
60
110
|
* @param attribution - `analyzeGlobalOutputs` 的结果;失败时传 null。
|
|
61
111
|
* @param usedOutputs - 有效题数。
|
package/lib/headers-core.d.ts
CHANGED
|
@@ -82,7 +82,9 @@ export declare function isSendableUserAgent(value: string): boolean;
|
|
|
82
82
|
/**
|
|
83
83
|
* Whether a configured name/value pair can ride a Fetch header. Fetch is the
|
|
84
84
|
* authority for the exact token grammar, so this accepts arbitrary legal names
|
|
85
|
-
* instead of maintaining a brittle allow-list
|
|
85
|
+
* instead of maintaining a brittle allow-list — except the wire-forbidden set
|
|
86
|
+
* above, which `Headers.set()` (an editing API, not a wire validator) happily
|
|
87
|
+
* accepts and the wire then chokes on.
|
|
86
88
|
*/
|
|
87
89
|
export declare function isSendableHeader(name: string, value: string): boolean;
|
|
88
90
|
/** The `headers` dict of one route profile, as string pairs, or undefined. */
|
package/lib/headers-fetch.d.ts
CHANGED
|
@@ -38,6 +38,31 @@
|
|
|
38
38
|
*/
|
|
39
39
|
import { emptyIndex, type HeaderIndex } from './headers-core.js';
|
|
40
40
|
import { type KeyCandidate } from './key-route-core.js';
|
|
41
|
+
/** What a dispatch hook is told about the request it may claim. */
|
|
42
|
+
export interface DispatchContext {
|
|
43
|
+
/** The request URL, as resolved by the wrapper. */
|
|
44
|
+
readonly url: string;
|
|
45
|
+
/** The request method, uppercased (GET when neither side named one). */
|
|
46
|
+
readonly method: string;
|
|
47
|
+
/**
|
|
48
|
+
* Lazily read the request body as text. Cached; a `Request` input is cloned
|
|
49
|
+
* (the caller's stream survives), an `init.body` string is taken directly,
|
|
50
|
+
* and any other shape yields undefined — the hook decides what "unreadable"
|
|
51
|
+
* means (the image dispatch lets those through rather than mis-claiming).
|
|
52
|
+
*/
|
|
53
|
+
readonly bodyText: () => Promise<string | undefined>;
|
|
54
|
+
/** The fetch beneath this wrapper, avoiding recursive interception. */
|
|
55
|
+
readonly send: typeof globalThis.fetch;
|
|
56
|
+
}
|
|
57
|
+
/** A dispatch candidate: return a Response to fully claim the request (it is never sent); undefined passes it through. */
|
|
58
|
+
export type DispatchHook = (context: DispatchContext) => Promise<Response | undefined>;
|
|
59
|
+
/**
|
|
60
|
+
* Register one dispatch hook. Order is registration order; the FIRST hook to
|
|
61
|
+
* return a Response claims the request. @returns the remover (idempotent).
|
|
62
|
+
*/
|
|
63
|
+
export declare function registerDispatchHook(hook: DispatchHook): () => void;
|
|
64
|
+
/** How many dispatch hooks are live (diagnostics; 0 = the seam costs nothing). */
|
|
65
|
+
export declare function dispatchHookCount(): number;
|
|
41
66
|
/** What a key resolver is told about the request it must sign. */
|
|
42
67
|
export interface KeyRouteContext {
|
|
43
68
|
/** The provider route whose profile matched the request URL. */
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `/生图` host command (the explicit entry beside the wire dispatch).
|
|
3
|
+
*
|
|
4
|
+
* Registered on the HOST side on purpose: the client's `commandUi.register`
|
|
5
|
+
* can only run a no-argument action — a command that reads parameters would
|
|
6
|
+
* fall through to `remote.commands.execute` and answer "unknown or malformed
|
|
7
|
+
* command". The host-side `ctx.commands.register` with an `input.hint` is the
|
|
8
|
+
* shape the built-in command plugins use (the dsh-command-goal precedent).
|
|
9
|
+
*
|
|
10
|
+
* The handler is a thin argument parser over the SHARED generation core —
|
|
11
|
+
* the same `generateImage` the dispatch and the `/image-generate` route call.
|
|
12
|
+
*
|
|
13
|
+
* @module dsh-model-think-level/image/command
|
|
14
|
+
*/
|
|
15
|
+
import { generateImage, type GenerateDeps, type GenerateResult } from './generate.js';
|
|
16
|
+
/** The host `commands` service, typed structurally (no new dependency). */
|
|
17
|
+
export interface CommandsServiceLike {
|
|
18
|
+
register(contribution: {
|
|
19
|
+
definitionId: string;
|
|
20
|
+
name: string;
|
|
21
|
+
description: string;
|
|
22
|
+
input?: {
|
|
23
|
+
hint?: string;
|
|
24
|
+
attachments?: boolean;
|
|
25
|
+
};
|
|
26
|
+
handler: (invocation: unknown) => Promise<unknown>;
|
|
27
|
+
}): unknown;
|
|
28
|
+
}
|
|
29
|
+
/** The plugin's command definition id (the built-ins' `@deepseek-ai/<pkg>:<name>` spelling). */
|
|
30
|
+
export declare const IMAGEGEN_DEFINITION_ID = "@deepseek-ai/dsh-model-think-level:imagegen";
|
|
31
|
+
/** The hint the slash menu shows under the command. */
|
|
32
|
+
export declare const IMAGEGEN_HINT = "[<\u6A21\u578B>] [--size 1024x1024] [--quality high] [--n 1] [--\u53C2\u8003\u56FE <\u8DEF\u5F84>] <\u63D0\u793A\u8BCD>";
|
|
33
|
+
/** One parsed invocation. */
|
|
34
|
+
export interface ImagegenArgs {
|
|
35
|
+
readonly model?: string;
|
|
36
|
+
readonly size?: string;
|
|
37
|
+
readonly quality?: string;
|
|
38
|
+
readonly n?: number;
|
|
39
|
+
readonly referenceImages: readonly string[];
|
|
40
|
+
readonly prompt: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Parse one `/生图` argument string. Tolerant on purpose: the model may be
|
|
44
|
+
* omitted (the config default applies), an unknown option is reported (with
|
|
45
|
+
* the accepted spellings — never silently ignored), and everything that is
|
|
46
|
+
* neither option nor model is the prompt.
|
|
47
|
+
* @throws Error naming the mistake and the accepted options.
|
|
48
|
+
*/
|
|
49
|
+
export declare function parseImagegenArgs(text: string, knownModels?: readonly string[]): ImagegenArgs;
|
|
50
|
+
/**
|
|
51
|
+
* What the handler needs: the live model ids (for argument disambiguation),
|
|
52
|
+
* the generation deps, and an optional echo face for the session the command
|
|
53
|
+
* ran in. The echo is best-effort — the command's return value is the
|
|
54
|
+
* contract the host consumes; a session write is attempted only when the
|
|
55
|
+
* invocation carries a session that exposes one.
|
|
56
|
+
*/
|
|
57
|
+
export interface ImagegenDeps {
|
|
58
|
+
readonly modelIds: () => readonly string[];
|
|
59
|
+
readonly generate: typeof generateImage;
|
|
60
|
+
readonly generateDeps: GenerateDeps;
|
|
61
|
+
/** Best-effort session echo (guarded; never throws into the command). */
|
|
62
|
+
readonly echo?: (invocation: unknown, text: string, filePath: string) => void;
|
|
63
|
+
} /** Run one parsed invocation through the shared core and shape the result. */
|
|
64
|
+
export declare function executeImagegen(deps: ImagegenDeps, parsed: ImagegenArgs): Promise<{
|
|
65
|
+
ok: true;
|
|
66
|
+
filePaths: readonly string[];
|
|
67
|
+
mime: string;
|
|
68
|
+
model: string;
|
|
69
|
+
paramsUsed: GenerateResult['paramsUsed'];
|
|
70
|
+
text: string;
|
|
71
|
+
warning?: string;
|
|
72
|
+
} | {
|
|
73
|
+
ok: false;
|
|
74
|
+
error: string;
|
|
75
|
+
}>;
|
|
76
|
+
/**
|
|
77
|
+
* The invocation face the host hands the handler, narrowed to what this
|
|
78
|
+
* command reads. `text` carries the arguments after the command word; the
|
|
79
|
+
* exact field is spelled several ways across host versions, so each common
|
|
80
|
+
* one is tried (in order) and the first non-empty wins.
|
|
81
|
+
*/
|
|
82
|
+
export declare function invocationTextOf(invocation: unknown): string;
|
|
83
|
+
/** Register the command. @returns undefined when the host has no commands service. */
|
|
84
|
+
export declare function registerImagegenCommand(commands: CommandsServiceLike | undefined, deps: ImagegenDeps): void;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The image-generation config: `<DSH_HOME>/dsh-model-think-level/image.json`.
|
|
3
|
+
*
|
|
4
|
+
* The file holds no secrets — credential references only — which is what makes
|
|
5
|
+
* it safe to keep in plain text and to hand to the MCP subprocess through one
|
|
6
|
+
* `DSH_IMAGE_CONFIG` env var. Its `models[]` is a DERIVED SNAPSHOT: the source
|
|
7
|
+
* of truth is the `llm-pi-ai` settings document's `inputImageGen` rows (the
|
|
8
|
+
* same marker the Models page and the auto-fill write), and this file layers
|
|
9
|
+
* the user's per-model overrides (endpoint verdict, param abilities, picked
|
|
10
|
+
* size/quality/n) on top. Syncing keeps every override keyed by model id, so a
|
|
11
|
+
* model that leaves the settings document drops out and one that comes back
|
|
12
|
+
* keeps its overrides.
|
|
13
|
+
*
|
|
14
|
+
* Parsing follows `key-index.ts`: read-never-throw for absent files, defensive
|
|
15
|
+
* field-by-field drops for malformed content, and an atomic temp-then-rename
|
|
16
|
+
* write so a crash mid-write cannot leave a half-parsed config behind.
|
|
17
|
+
*
|
|
18
|
+
* @module dsh-model-think-level/image/config
|
|
19
|
+
*/
|
|
20
|
+
/** Endpoint verdict of one image model. `unverified` is the honest default. */
|
|
21
|
+
export type EndpointStatus = 'unverified' | 'ok' | 'unsupported';
|
|
22
|
+
/** Wire shape the model row speaks. Only OpenAI-compatible images is wired up. */
|
|
23
|
+
export type ImageApi = 'images' | 'none';
|
|
24
|
+
/** The conservative ability table every freshly derived row starts with. */
|
|
25
|
+
export interface ImageParams {
|
|
26
|
+
readonly sizes: readonly string[];
|
|
27
|
+
readonly qualities: readonly string[];
|
|
28
|
+
readonly maxN: number;
|
|
29
|
+
/** How the endpoint wants its answer: b64 inline, an output_format field, or nothing. */
|
|
30
|
+
readonly responseFormat: 'b64_json' | 'output_format' | 'none';
|
|
31
|
+
}
|
|
32
|
+
/** One derived model row, before user overrides. */
|
|
33
|
+
export interface ImageModelSeed {
|
|
34
|
+
readonly id: string;
|
|
35
|
+
readonly route: string;
|
|
36
|
+
readonly modelId: string;
|
|
37
|
+
readonly label: string;
|
|
38
|
+
/** The route profile's `displayName` (the provider name the Models page
|
|
39
|
+
* shows), when the profile has one; absent when the profile does not. */
|
|
40
|
+
readonly providerLabel?: string;
|
|
41
|
+
readonly api: ImageApi;
|
|
42
|
+
readonly baseURL: string;
|
|
43
|
+
readonly keyRef?: string;
|
|
44
|
+
}
|
|
45
|
+
/** One image model as the config stores it: derived facts + user overrides. */
|
|
46
|
+
export interface ImageModel {
|
|
47
|
+
readonly id: string;
|
|
48
|
+
readonly route: string;
|
|
49
|
+
readonly modelId: string;
|
|
50
|
+
readonly label: string;
|
|
51
|
+
readonly providerLabel?: string;
|
|
52
|
+
readonly api: ImageApi;
|
|
53
|
+
readonly baseURL: string;
|
|
54
|
+
readonly keyRef?: string;
|
|
55
|
+
readonly endpoint: {
|
|
56
|
+
readonly kind: 'images';
|
|
57
|
+
readonly status: EndpointStatus;
|
|
58
|
+
readonly checkedAt: string | null;
|
|
59
|
+
};
|
|
60
|
+
readonly params: ImageParams;
|
|
61
|
+
readonly lastGoodParams: ImageParams | null;
|
|
62
|
+
}
|
|
63
|
+
/** The defaults every model row inherits when it does not override. */
|
|
64
|
+
export interface ImageDefaults {
|
|
65
|
+
readonly size: string;
|
|
66
|
+
readonly quality: string;
|
|
67
|
+
readonly n: number;
|
|
68
|
+
readonly timeoutMs: number;
|
|
69
|
+
}
|
|
70
|
+
/** The whole config document, exactly as it sits on disk. */
|
|
71
|
+
export interface ImageConfig {
|
|
72
|
+
readonly version: 2;
|
|
73
|
+
/** When `models[]` was last re-derived from the settings document. */
|
|
74
|
+
readonly syncedAt: string;
|
|
75
|
+
/** `"<route>/<modelId>"` of the default model, when the user picked one. */
|
|
76
|
+
readonly defaultModel: string | null;
|
|
77
|
+
readonly defaults: ImageDefaults;
|
|
78
|
+
readonly outputDir: string;
|
|
79
|
+
readonly models: readonly ImageModel[];
|
|
80
|
+
}
|
|
81
|
+
/** A mutable view of the config the writers build from. */
|
|
82
|
+
export type ImageConfigDraft = {
|
|
83
|
+
-readonly [K in keyof ImageConfig]: ImageConfig[K];
|
|
84
|
+
};
|
|
85
|
+
/** Conservative defaults: nothing here assumes a capability the API may not have. */
|
|
86
|
+
export declare const IMAGE_FALLBACK_PARAMS: ImageParams;
|
|
87
|
+
/** Absolute path of the image config. */
|
|
88
|
+
export declare function imageConfigFilePath(home?: string): string;
|
|
89
|
+
/** A fresh, empty config. */
|
|
90
|
+
export declare function emptyImageConfig(now?: () => Date): ImageConfig;
|
|
91
|
+
/**
|
|
92
|
+
* Parse an already-decoded config document. Never throws and never trusts the
|
|
93
|
+
* input: anything unrecognisable is dropped field by field, so a hand-edited
|
|
94
|
+
* or truncated file costs at most the fields it got wrong. Overrides of model
|
|
95
|
+
* rows this document does not know are kept — the next sync may re-derive them.
|
|
96
|
+
*/
|
|
97
|
+
export declare function parseImageConfig(raw: unknown, now?: () => Date): ImageConfig;
|
|
98
|
+
/** Read the config; an absent or unreadable file means a fresh, empty one. */
|
|
99
|
+
export declare function readImageConfig(file?: string): ImageConfig;
|
|
100
|
+
/** Write the config at one path, atomically (sibling temp file, then rename). */
|
|
101
|
+
export declare function writeImageConfig(document: ImageConfig, file?: string): void;
|
|
102
|
+
/**
|
|
103
|
+
* Derive the image-model seeds from a resolved `llm-pi-ai` providers map: every
|
|
104
|
+
* route's `models[]` row carrying `inputImageGen === true`, with the route's
|
|
105
|
+
* api/baseURL/credential reference (and its displayName, the provider name the
|
|
106
|
+
* list shows) attached. This is the SAME marker the Models
|
|
107
|
+
* page editor and the auto-fill write, so both writers feed this list.
|
|
108
|
+
*
|
|
109
|
+
* A route whose protocol cannot carry an OpenAI-compatible images call still
|
|
110
|
+
* lists its rows — marked `api: 'none'` so the UI can badge "no compatible
|
|
111
|
+
* endpoint" and refuse generation.
|
|
112
|
+
*/
|
|
113
|
+
export declare function deriveImageSeeds(providers: Record<string, Record<string, unknown>>): ImageModelSeed[];
|
|
114
|
+
/**
|
|
115
|
+
* Re-derive `models[]` from the settings document while keeping every stored
|
|
116
|
+
* override keyed by model id. A derived row starts from the conservative
|
|
117
|
+
* ability table; a stored row of the same id hands its overrides (endpoint
|
|
118
|
+
* verdict, params, lastGoodParams) forward. Rows the settings document no
|
|
119
|
+
* longer declares are dropped; defaultModel that points at a dropped model is
|
|
120
|
+
* cleared.
|
|
121
|
+
*
|
|
122
|
+
* The old per-row size/quality/n picks are gone on purpose: the pane's params
|
|
123
|
+
* row is the single place those are chosen, so a leftover pick from an older
|
|
124
|
+
* build must not quietly outrank it. They are simply not carried forward.
|
|
125
|
+
*/
|
|
126
|
+
export declare function syncImageConfig(stored: ImageConfig, providers: Record<string, Record<string, unknown>>, now?: () => Date): ImageConfig;
|
|
127
|
+
/**
|
|
128
|
+
* Resolve the model a generation should use, per the design's priority:
|
|
129
|
+
* an explicit pick (the dialog's current model, or a call's `model` argument)
|
|
130
|
+
* wins; then the config's defaultModel; then the sole candidate when exactly
|
|
131
|
+
* one exists. `undefined` when nothing resolves.
|
|
132
|
+
*/
|
|
133
|
+
export declare function resolveImageModel(config: ImageConfig, explicit?: string | undefined): ImageModel | undefined;
|
|
134
|
+
/**
|
|
135
|
+
* The params one generation uses: the call's explicit values win, then the
|
|
136
|
+
* config defaults (the pane's params row). `n` is additionally clamped to the
|
|
137
|
+
* model's ability table (a row that says maxN 2 never fires 8 requests).
|
|
138
|
+
* `referenceImages` never comes from config — it is per-call.
|
|
139
|
+
*/
|
|
140
|
+
export declare function effectiveParams(config: ImageConfig, model: ImageModel | undefined, request: {
|
|
141
|
+
size?: string;
|
|
142
|
+
quality?: string;
|
|
143
|
+
n?: number;
|
|
144
|
+
}): {
|
|
145
|
+
size: string;
|
|
146
|
+
quality: string;
|
|
147
|
+
n: number;
|
|
148
|
+
};
|
|
149
|
+
/** One synchronous read-modify-write of the config file. */
|
|
150
|
+
export declare function updateImageConfig(mutate: (document: ImageConfig) => ImageConfig, file?: string): ImageConfig;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The image-generation core: ONE implementation of the OpenAI-compatible
|
|
3
|
+
* images API call + disk write, behind three entries (the `generate_image`
|
|
4
|
+
* native tool, the `/image-generate` route, the `/生图` host command).
|
|
5
|
+
* Anything about request construction, param fallback, sanitization or file
|
|
6
|
+
* naming lives here and nowhere else.
|
|
7
|
+
*
|
|
8
|
+
* Endpoint choice: no reference images → `POST {baseURL}/images/generations`
|
|
9
|
+
* (JSON); with reference images → `POST {baseURL}/images/edits` with a
|
|
10
|
+
* hand-built multipart/form-data body (zero dependencies — the boundary is
|
|
11
|
+
* constructed here, not by a library the bundle cannot carry).
|
|
12
|
+
*
|
|
13
|
+
* Failure taxonomy (v3): the GUI used to show an image-only key's `403
|
|
14
|
+
* permission_denied` as "API 密钥无效" because the CHAT wire produced it. This
|
|
15
|
+
* core never talks the chat wire, and its errors are classified — auth vs
|
|
16
|
+
* image-endpoint-only permission vs missing endpoint vs param — so every entry
|
|
17
|
+
* can show the operative sentence instead of the misleading one. Error text is
|
|
18
|
+
* sanitized: the key value can never appear in a message, whatever shape the
|
|
19
|
+
* upstream echoes back.
|
|
20
|
+
*
|
|
21
|
+
* @module dsh-model-think-level/image/generate
|
|
22
|
+
*/
|
|
23
|
+
import { type ImageConfig, type ImageModel } from './config.js';
|
|
24
|
+
/** What a caller asks for. Everything but `prompt` is optional. */
|
|
25
|
+
export interface GenerateRequest {
|
|
26
|
+
/** `"<route>/<modelId>"`, a bare modelId, or the label; default when omitted. */
|
|
27
|
+
readonly model?: string;
|
|
28
|
+
readonly prompt: string;
|
|
29
|
+
/**
|
|
30
|
+
* The caller's cancellation (the user pressed stop while the reply stream
|
|
31
|
+
* was still pending). Aborts the in-flight images request too, so a stop
|
|
32
|
+
* never leaves a paid generation running to completion behind the UI.
|
|
33
|
+
*/
|
|
34
|
+
readonly signal?: AbortSignal;
|
|
35
|
+
readonly size?: string;
|
|
36
|
+
readonly quality?: string;
|
|
37
|
+
readonly n?: number;
|
|
38
|
+
/** Local file paths; any non-empty list switches to the edits endpoint. */
|
|
39
|
+
readonly referenceImages?: readonly string[];
|
|
40
|
+
/**
|
|
41
|
+
* Reference images as already-decoded bytes — the attachment-backed channel:
|
|
42
|
+
* the native tool resolves conversation image IDs through the attachment
|
|
43
|
+
* store and hands the bytes over, so no temp file ever hits the disk. Any
|
|
44
|
+
* entry here (alone or beside `referenceImages`) switches to the edits
|
|
45
|
+
* endpoint.
|
|
46
|
+
*/
|
|
47
|
+
readonly referenceFiles?: readonly {
|
|
48
|
+
readonly name: string;
|
|
49
|
+
readonly mime: string;
|
|
50
|
+
readonly bytes: Uint8Array;
|
|
51
|
+
}[];
|
|
52
|
+
/**
|
|
53
|
+
* Where to land the file: a path ending in `.png` is honored EXACTLY (an
|
|
54
|
+
* existing one gains a `-2` suffix — never overwritten), anything else is
|
|
55
|
+
* created as a directory and receives the slug-timestamp naming. Absent
|
|
56
|
+
* means the config's `outputDir`.
|
|
57
|
+
*/
|
|
58
|
+
readonly outputPath?: string;
|
|
59
|
+
}
|
|
60
|
+
/** One generated image: the b64 payload the wire returned + where it landed. */
|
|
61
|
+
export interface GeneratedImage {
|
|
62
|
+
readonly filePath: string;
|
|
63
|
+
readonly data: string;
|
|
64
|
+
}
|
|
65
|
+
/** What a successful generation reports. */
|
|
66
|
+
export interface GenerateResult {
|
|
67
|
+
/**
|
|
68
|
+
* Every image the wire returned, in order, each already written to disk.
|
|
69
|
+
* The upstream is asked for `paramsUsed.n` images; a relay that answers
|
|
70
|
+
* fewer yields fewer entries — the result never invents one.
|
|
71
|
+
*/
|
|
72
|
+
readonly images: readonly GeneratedImage[];
|
|
73
|
+
readonly mime: string;
|
|
74
|
+
readonly model: string;
|
|
75
|
+
/** The prompt this run answered (the reply's markdown alt text derives from it). */
|
|
76
|
+
readonly prompt: string;
|
|
77
|
+
readonly paramsUsed: {
|
|
78
|
+
readonly size: string;
|
|
79
|
+
readonly quality: string;
|
|
80
|
+
readonly n: number;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* What the fallback chain had to change, in order — the "回退说明" the reply
|
|
84
|
+
* text carries. Empty when the first attempt went through untouched.
|
|
85
|
+
*/
|
|
86
|
+
readonly revisions: readonly string[];
|
|
87
|
+
}
|
|
88
|
+
/** The classified failure `generateImage` throws (as `GenerateError`). */
|
|
89
|
+
export type GenerateFailureKind = 'config' | 'auth' | 'permission' | 'endpoint' | 'param' | 'reference' | 'upstream' | 'timeout' | 'aborted';
|
|
90
|
+
export declare class GenerateError extends Error {
|
|
91
|
+
readonly kind: GenerateFailureKind;
|
|
92
|
+
readonly status?: number;
|
|
93
|
+
constructor(kind: GenerateFailureKind, message: string, status?: number);
|
|
94
|
+
}
|
|
95
|
+
/** The faces this core needs from its host. All injectable for tests. */
|
|
96
|
+
export interface GenerateDeps {
|
|
97
|
+
/** The live config; read fresh per call (the MCP path re-reads the file). */
|
|
98
|
+
readonly config?: () => ImageConfig;
|
|
99
|
+
/** Resolve one credential reference to its value. */
|
|
100
|
+
readonly resolveKey: (ref: string) => Promise<string | undefined>;
|
|
101
|
+
/** The fetch beneath any wrapper — the images call must not be re-intercepted. */
|
|
102
|
+
readonly send: typeof globalThis.fetch;
|
|
103
|
+
/** Base dir for relative `outputDir`s (defaults to the harness home). */
|
|
104
|
+
readonly home?: () => string;
|
|
105
|
+
/** Persist `lastGoodParams` after a successful fallback (host: config file). */
|
|
106
|
+
readonly persistLastGood?: (modelId: string, params: ImageModel['lastGoodParams']) => void;
|
|
107
|
+
/** Now, for file stamps (defaults to Date.now). */
|
|
108
|
+
readonly now?: () => number;
|
|
109
|
+
/** Per-call timeout override (ms). */
|
|
110
|
+
readonly timeoutMs?: number;
|
|
111
|
+
}
|
|
112
|
+
/** The multipart body of an /images/edits call, hand-built (zero deps). */
|
|
113
|
+
export declare function buildMultipart(boundary: string, fields: Readonly<Record<string, string>>, images: readonly {
|
|
114
|
+
readonly name: string;
|
|
115
|
+
readonly bytes: Uint8Array;
|
|
116
|
+
readonly mime: string;
|
|
117
|
+
}[]): {
|
|
118
|
+
body: Uint8Array<ArrayBuffer>;
|
|
119
|
+
contentType: string;
|
|
120
|
+
};
|
|
121
|
+
/** Filesystem slug of a prompt: CJK kept, everything unsafe folded to `-`. */
|
|
122
|
+
export declare function promptSlug(prompt: string): string;
|
|
123
|
+
/**
|
|
124
|
+
* Where this run's file lands. An explicit `.png` path is honored exactly
|
|
125
|
+
* (an existing one gains a numeric suffix — the never-overwrite rule);
|
|
126
|
+
* anything else — an existing directory, a trailing separator, a non-png
|
|
127
|
+
* name — is created as a directory and receives the slug-timestamp naming.
|
|
128
|
+
*/
|
|
129
|
+
export declare function resolveDestination(requested: string | undefined, fallbackDir: string, slug: string, now: () => number): string;
|
|
130
|
+
/**
|
|
131
|
+
* Run one generation. Reads the config fresh, resolves the model per the
|
|
132
|
+
* priority chain, merges params, calls the images endpoint (with one pass of
|
|
133
|
+
* the param fallback chain), decodes and writes the file.
|
|
134
|
+
*/
|
|
135
|
+
export declare function generateImage(deps: GenerateDeps, request: GenerateRequest): Promise<GenerateResult>;
|