dsh-image-gen 0.9.3 → 0.10.1
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/lib/client.js +965 -405
- package/lib/index.js +306 -67
- package/lib/types/client/conversation-image-revisions.d.ts +1 -0
- package/lib/types/client/conversation-regenerate.d.ts +1 -0
- package/lib/types/client/gallery-store.d.ts +1 -0
- package/lib/types/client/image-result-node.d.ts +1 -0
- package/lib/types/client/index.d.ts +6 -1
- package/lib/types/client/multi-model-compare.d.ts +3 -3
- package/lib/types/client/provider-pill.d.ts +9 -3
- package/lib/types/client/studio-view.d.ts +1 -0
- package/lib/types/client/tl/tl-trace-bridge.d.ts +55 -0
- package/lib/types/client/tl/trace-playback.d.ts +101 -0
- package/lib/types/config.d.ts +11 -2
- package/lib/types/credentials.d.ts +2 -2
- package/lib/types/generation-timeout.d.ts +5 -0
- package/lib/types/image-tool-context.d.ts +1 -1
- package/lib/types/shared.d.ts +53 -0
- package/lib/types/studio-route.d.ts +1 -0
- package/lib/types/studio.d.ts +1 -1
- package/lib/types/test-route.d.ts +3 -3
- package/lib/types/trace-drawing.d.ts +102 -0
- package/lib/types/trace-route.d.ts +12 -0
- package/lib/types/trace-sketch.d.ts +26 -0
- package/lib/types/trace-store.d.ts +23 -0
- package/lib/types/vendor/drawai/raster.d.ts +43 -0
- package/lib/types/vendor/drawai/trace-image.d.ts +257 -0
- package/package.json +1 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Derive a pencil-sketch raster from a reference image.
|
|
3
|
+
*
|
|
4
|
+
* The tracer can only reproduce what is in the pixels, so "先起稿再上色" needs
|
|
5
|
+
* an actual contour drawing to reproduce. Spending a model call on one would be
|
|
6
|
+
* slow, non-deterministic and — for a picture the model has already been given
|
|
7
|
+
* — redundant. Derive it locally instead: grayscale, Sobel magnitude, an
|
|
8
|
+
* adaptive threshold, then a one-pixel dilation so the lines survive the
|
|
9
|
+
* downsampling the tracer does internally.
|
|
10
|
+
*
|
|
11
|
+
* The result is a black-on-white line drawing: the contour pass an illustrator
|
|
12
|
+
* makes before blocking in colour.
|
|
13
|
+
*/
|
|
14
|
+
/** RGBA8 bitmap, the same shape the tracer consumes. */
|
|
15
|
+
export interface SketchRaster {
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
data: Uint8Array;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Build a line drawing from a raster.
|
|
22
|
+
* @param raster - source pixels (RGBA8).
|
|
23
|
+
* @param maxSide - long side to compute the edges at; defaults to 1200.
|
|
24
|
+
* @returns a black-on-white RGBA8 bitmap of the detected contours.
|
|
25
|
+
*/
|
|
26
|
+
export declare function sketchRaster(raster: SketchRaster, maxSide?: number): SketchRaster;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { TraceShow } from './trace-drawing.js';
|
|
2
|
+
export interface TracePlanStoreOptions {
|
|
3
|
+
/** Directory for restart-surviving copies; omit to stay memory-only. */
|
|
4
|
+
dir?: string | undefined;
|
|
5
|
+
}
|
|
6
|
+
export declare class TracePlanStore {
|
|
7
|
+
private readonly entries;
|
|
8
|
+
private readonly dir;
|
|
9
|
+
constructor(options?: TracePlanStoreOptions);
|
|
10
|
+
/** Store one show and return the id the browser will use. */
|
|
11
|
+
put(show: TraceShow): Promise<string>;
|
|
12
|
+
/** The show behind an id: memory first, then the disk copy, else undefined. */
|
|
13
|
+
get(id: string): Promise<TraceShow | undefined>;
|
|
14
|
+
/** Write the disk copy; failures are logged and otherwise ignored. */
|
|
15
|
+
private persist;
|
|
16
|
+
/** Read one disk copy, dropping it when expired or malformed. */
|
|
17
|
+
private read;
|
|
18
|
+
private remove;
|
|
19
|
+
/** Drop expired entries, then the oldest ones past the cap, in memory and on disk. */
|
|
20
|
+
private evict;
|
|
21
|
+
/** Delete disk copies left behind by a previous host process. */
|
|
22
|
+
private evictDisk;
|
|
23
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vendored from dsh-drawai v0.3.4 - https://github.com/fourzkw/dsh-drawai
|
|
3
|
+
* MIT License, Copyright (c) 2026 fourzkw.
|
|
4
|
+
* Copied verbatim from lib/raster.js apart from this header.
|
|
5
|
+
*
|
|
6
|
+
* PNG decoding is dependency free (node:zlib only). JPEG/GIF/WebP go through an
|
|
7
|
+
* optional sharp install, which this plugin does not rely on.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* 位图解码 —— 把图片文件解成原始 RGBA 像素,供「逐像素矢量化」使用。
|
|
11
|
+
*
|
|
12
|
+
* 两条路:
|
|
13
|
+
* 1. **纯 Node 的 PNG 解码器**(零依赖,只用 node:zlib):PNG 是矢量化最常见的输入
|
|
14
|
+
* (截图、导出的角色图),自己解能保证"宿主装了什么依赖都能用"。
|
|
15
|
+
* 2. **可选 sharp**:非 PNG(JPEG / WebP / GIF…)自己没有解码器,装了 sharp 就用它。
|
|
16
|
+
* sharp 是可选依赖:没装时只报"这个格式解不开",不影响 PNG 那条主路。
|
|
17
|
+
*
|
|
18
|
+
* 解码结果是 `{ width, height, data }`,data 是 **RGBA8**(`width*height*4` 字节),
|
|
19
|
+
* 也就是矢量化的**唯一真相**:后面所有轮廓都从它算,中间不再碰文件。
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* 解一份 PNG 成 RGBA8。
|
|
23
|
+
*
|
|
24
|
+
* 支持:颜色类型 0/2/3/4/6、位深 1/2/4/8/16、Adam7 隔行、tRNS 透明。
|
|
25
|
+
* 不支持(会明确报错):颜色类型 1(已废弃)。
|
|
26
|
+
*/
|
|
27
|
+
export declare function decodePng(bytes: any): {
|
|
28
|
+
width: number;
|
|
29
|
+
height: number;
|
|
30
|
+
data: Buffer<ArrayBuffer>;
|
|
31
|
+
};
|
|
32
|
+
/** 是不是能被本模块解开的图片(按文件头判,不看后缀)。 */
|
|
33
|
+
export declare function looksLikeRaster(bytes: any): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* 解一份图片文件成 RGBA8。
|
|
36
|
+
*
|
|
37
|
+
* PNG 走自带解码器(零依赖、结果确定);其余格式试 sharp,没有就给可执行的建议。
|
|
38
|
+
*/
|
|
39
|
+
export declare function decodeRaster(bytes: any): Promise<{
|
|
40
|
+
width: any;
|
|
41
|
+
height: any;
|
|
42
|
+
data: any;
|
|
43
|
+
}>;
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vendored from dsh-drawai v0.3.4 - https://github.com/fourzkw/dsh-drawai
|
|
3
|
+
* MIT License, Copyright (c) 2026 fourzkw.
|
|
4
|
+
* Copied verbatim from lib/trace-image.js apart from this header.
|
|
5
|
+
*
|
|
6
|
+
* Self-contained raster-to-SVG tracer: it takes RGBA pixels, quantizes with
|
|
7
|
+
* k-means, traces pixel-grid contours, simplifies with RDP, and emits one
|
|
8
|
+
* nonzero path per colour. With includeDrawing it also returns an ordered plan
|
|
9
|
+
* (largest area first) that a canvas can replay step by step.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* 逐像素矢量化 —— 把一张位图变成一段内嵌 SVG 标记(DrawAI 的 svg 内容节点用)。
|
|
13
|
+
*
|
|
14
|
+
* 这里只有**像素进、SVG 出**,不碰文件系统、不碰 ctx:宿主工具与命令行脚本共用同一份。
|
|
15
|
+
*
|
|
16
|
+
* 管线(每一步都能在返回的 metrics 里看到数字):
|
|
17
|
+
* ① 工作图尺寸 = src/scale,再套 grid(宽)/ gridH(高)上限;透明像素合成到 bgColor
|
|
18
|
+
* ② 去背景:由调用方显式传 removeBg true/false(无 auto)。true 时先识纯色底再泛洪,识不到走近白回退
|
|
19
|
+
* ③ 分层 k-means(colorsDark 暗色 + colorsFill 填充)定调色板;暗像素进保护区
|
|
20
|
+
* ④ 3× 超采样归类 → 条件众数(高对比跳过)→ despeckle / minRegionArea(保护区不并)
|
|
21
|
+
* ⑤ 每色按连通域取闭合轮廓:像素**格边**上的有向图 + 左转跟随(区域恒在左侧)
|
|
22
|
+
* ⑥ RDP + 预算阶梯(受保护环不因 minLoopArea 丢弃)→ 每色一条 <path fill-rule="nonzero">
|
|
23
|
+
*
|
|
24
|
+
* 为什么是"格边跟随"而不是 marching squares:标签图在鞍点(对角相接)处 marching squares
|
|
25
|
+
* 要人为定连通性,而格边有向图本身就无歧义 —— 每条边只属于一个区域,走完一圈恰好回到起点。
|
|
26
|
+
* 代价是必须用 4 连通分区(对角相接算两个区域),见下面 connectedRegions 的注释。
|
|
27
|
+
*/
|
|
28
|
+
/** 矢量化的默认参数(宿主工具与命令行共用)。 */
|
|
29
|
+
export declare const TRACE_DEFAULTS: {
|
|
30
|
+
preset: string;
|
|
31
|
+
colors: number;
|
|
32
|
+
colorsDark: number;
|
|
33
|
+
colorsFill: number;
|
|
34
|
+
grid: number;
|
|
35
|
+
gridH: number;
|
|
36
|
+
scale: number;
|
|
37
|
+
epsilon: number;
|
|
38
|
+
precision: number;
|
|
39
|
+
minArea: number;
|
|
40
|
+
minRegionArea: number;
|
|
41
|
+
protectLuma: number;
|
|
42
|
+
majorityContrast: number;
|
|
43
|
+
bgBright: number;
|
|
44
|
+
bgTol: number;
|
|
45
|
+
bgColor: number[];
|
|
46
|
+
bgSolidTol: number;
|
|
47
|
+
bgSolidBorderRatio: number;
|
|
48
|
+
seeds: number;
|
|
49
|
+
maxBytes: number;
|
|
50
|
+
includeLabels: boolean;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* 命名预设:AI 先选场景,再按需覆盖单个字段。
|
|
54
|
+
* 合并顺序:TRACE_DEFAULTS ← preset ← 显式字段。
|
|
55
|
+
*/
|
|
56
|
+
export declare const TRACE_PRESETS: {
|
|
57
|
+
default: {};
|
|
58
|
+
/** 插画 / 线稿:多留暗色、严保细线、轮廓更细 */
|
|
59
|
+
lineart: {
|
|
60
|
+
colorsDark: number;
|
|
61
|
+
protectLuma: number;
|
|
62
|
+
majorityContrast: number;
|
|
63
|
+
minArea: number;
|
|
64
|
+
epsilon: number;
|
|
65
|
+
precision: number;
|
|
66
|
+
};
|
|
67
|
+
/** 照片 / 渐变:少暗色配额、碎块并多一点、轮廓可粗 */
|
|
68
|
+
photo: {
|
|
69
|
+
colors: number;
|
|
70
|
+
colorsDark: number;
|
|
71
|
+
protectLuma: number;
|
|
72
|
+
majorityContrast: number;
|
|
73
|
+
minArea: number;
|
|
74
|
+
epsilon: number;
|
|
75
|
+
};
|
|
76
|
+
/** 图标 / 简单截图:少色 + 降采样,压字符数 */
|
|
77
|
+
simple: {
|
|
78
|
+
colors: number;
|
|
79
|
+
colorsDark: number;
|
|
80
|
+
scale: number;
|
|
81
|
+
grid: number;
|
|
82
|
+
epsilon: number;
|
|
83
|
+
minArea: number;
|
|
84
|
+
protectLuma: number;
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
/** 合法预设名列表(给报错 / 提示用)。 */
|
|
88
|
+
export declare const TRACE_PRESET_NAMES: string[];
|
|
89
|
+
/** 按用户参考图校准:保持同一棱角化色块管线,主要以采样密度区分三档。 */
|
|
90
|
+
export declare const TRACE_FRAGMENT_LEVELS: Readonly<{
|
|
91
|
+
low: Readonly<{
|
|
92
|
+
resolutionRatio: number;
|
|
93
|
+
tolerance: 0.1;
|
|
94
|
+
}>;
|
|
95
|
+
medium: Readonly<{
|
|
96
|
+
resolutionRatio: number;
|
|
97
|
+
tolerance: 0.08;
|
|
98
|
+
}>;
|
|
99
|
+
high: Readonly<{
|
|
100
|
+
resolutionRatio: 1;
|
|
101
|
+
tolerance: 0.04;
|
|
102
|
+
}>;
|
|
103
|
+
}>;
|
|
104
|
+
export declare function traceRasterForToolbar(raster: any, raw?: {}): any;
|
|
105
|
+
/**
|
|
106
|
+
* 合并默认值 + 预设 + 显式覆盖。未知 preset 抛错(让 AI 立刻改,而不是静默当 default)。
|
|
107
|
+
* @returns {{options:object, preset:string}}
|
|
108
|
+
*/
|
|
109
|
+
export declare function resolveTraceOptions(rawOptions: any): {
|
|
110
|
+
options: any;
|
|
111
|
+
preset: any;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* 根据 metrics 生成一句调参建议(给 AI 读,不是给用户的长文)。
|
|
115
|
+
*/
|
|
116
|
+
export declare function hintTraceTune(metrics: any): string;
|
|
117
|
+
/**
|
|
118
|
+
* 把普通 JSON 数据收成 DSH 工具返回值能过关的"无损 JSON":
|
|
119
|
+
* · 丢掉值为 undefined 的自有属性(`{crop:undefined}` 会被 isJsonValue 判死刑,
|
|
120
|
+
* 而 JSON.stringify 却会默默省略 —— 这正是 traceImage 回执曾触发
|
|
121
|
+
* `tool "diagram_apply" returned invalid output: value is not lossless JSON` 的根因)
|
|
122
|
+
* · 把 -0 收成 0(DSH 的 walkJsonValue 同样拒 -0)
|
|
123
|
+
* · 非有限数字 → 0(宁可变钝,也不让整次工具调用被宿主打回)
|
|
124
|
+
*/
|
|
125
|
+
export declare function toLosslessJson(value: any): any;
|
|
126
|
+
/**
|
|
127
|
+
* 压缩给 AI 的结构化调参回执(diagram_apply.trace[])。
|
|
128
|
+
*
|
|
129
|
+
* 可选字段(image / nodeId / crop)**缺就不要挂键** —— 宿主对工具输出走
|
|
130
|
+
* lossless JSON 校验,自有属性值为 undefined 会整次失败。
|
|
131
|
+
*/
|
|
132
|
+
export declare function summarizeTraceForAi(metrics: any, extra: any): any;
|
|
133
|
+
/**
|
|
134
|
+
* 位图 → SVG 标记。
|
|
135
|
+
*
|
|
136
|
+
* @param {{width:number,height:number,data:Buffer|Uint8Array}} raster RGBA8
|
|
137
|
+
* @param {object} [rawOptions] 见 TRACE_DEFAULTS / TRACE_PRESETS(可用 preset)
|
|
138
|
+
* @returns {{svg:string, metrics:object}}
|
|
139
|
+
*/
|
|
140
|
+
export declare function traceRasterToSvg(raster: any, rawOptions: any): {
|
|
141
|
+
labels: {
|
|
142
|
+
width: number;
|
|
143
|
+
height: number;
|
|
144
|
+
colors: number;
|
|
145
|
+
palette: string[];
|
|
146
|
+
labels: string;
|
|
147
|
+
} | null;
|
|
148
|
+
metrics: {
|
|
149
|
+
preset: any;
|
|
150
|
+
source: {
|
|
151
|
+
width: any;
|
|
152
|
+
height: any;
|
|
153
|
+
};
|
|
154
|
+
work: {
|
|
155
|
+
width: number;
|
|
156
|
+
height: number;
|
|
157
|
+
scale: number;
|
|
158
|
+
};
|
|
159
|
+
scaleRequested: number;
|
|
160
|
+
scaleApplied: number;
|
|
161
|
+
grid: number;
|
|
162
|
+
gridH: number;
|
|
163
|
+
bgColor: any[];
|
|
164
|
+
sizeNotes: string;
|
|
165
|
+
colors: number;
|
|
166
|
+
colorsDark: number;
|
|
167
|
+
colorsFill: any;
|
|
168
|
+
darkSamples: number;
|
|
169
|
+
fillSamples: any;
|
|
170
|
+
protectLuma: number;
|
|
171
|
+
protectPixels: number;
|
|
172
|
+
fgRelabeledFromBg: number;
|
|
173
|
+
bgForced: number;
|
|
174
|
+
bgRemoved: boolean;
|
|
175
|
+
bgDetect: {
|
|
176
|
+
hasSolid: boolean;
|
|
177
|
+
mode: string;
|
|
178
|
+
matchRatio: number;
|
|
179
|
+
kind: null;
|
|
180
|
+
color: null;
|
|
181
|
+
};
|
|
182
|
+
majorityContrast: number;
|
|
183
|
+
majoritySkipped: number;
|
|
184
|
+
majorityApplied: number;
|
|
185
|
+
palette: string[];
|
|
186
|
+
backgroundPixels: number;
|
|
187
|
+
foregroundPixels: number;
|
|
188
|
+
despeckle: {
|
|
189
|
+
mergedPixels: number;
|
|
190
|
+
blocksMerged: number;
|
|
191
|
+
blocks: number;
|
|
192
|
+
protectedSkipped: number;
|
|
193
|
+
};
|
|
194
|
+
regionMerge: {
|
|
195
|
+
mergedPixels: number;
|
|
196
|
+
blocksMerged: number;
|
|
197
|
+
blocks: number;
|
|
198
|
+
protectedSkipped: number;
|
|
199
|
+
};
|
|
200
|
+
regions: number;
|
|
201
|
+
regionsProtected: number;
|
|
202
|
+
loops: number;
|
|
203
|
+
paths: number;
|
|
204
|
+
loopsKept: number;
|
|
205
|
+
fragments: number;
|
|
206
|
+
loopsProtectedKept: number;
|
|
207
|
+
drawnPixels: number | null;
|
|
208
|
+
totalPixels: number;
|
|
209
|
+
exactPixelCoverage: boolean;
|
|
210
|
+
labelPartitionComplete: boolean;
|
|
211
|
+
coverageBasis: string;
|
|
212
|
+
loopsDropped: number;
|
|
213
|
+
budgetExceeded: boolean;
|
|
214
|
+
appliedEpsilon: any;
|
|
215
|
+
svgChars: number;
|
|
216
|
+
budget: any;
|
|
217
|
+
attempts: number;
|
|
218
|
+
paletteAreaSum: number;
|
|
219
|
+
optionsApplied: {
|
|
220
|
+
preset: any;
|
|
221
|
+
colors: number;
|
|
222
|
+
colorsDark: number;
|
|
223
|
+
colorsFill: any;
|
|
224
|
+
grid: number;
|
|
225
|
+
gridH: number;
|
|
226
|
+
scale: number;
|
|
227
|
+
epsilon: any;
|
|
228
|
+
precision: any;
|
|
229
|
+
minArea: number;
|
|
230
|
+
minRegionArea: number;
|
|
231
|
+
protectLuma: number;
|
|
232
|
+
majorityContrast: number;
|
|
233
|
+
removeBg: string;
|
|
234
|
+
bgSolidTol: any;
|
|
235
|
+
bgSolidBorderRatio: any;
|
|
236
|
+
};
|
|
237
|
+
};
|
|
238
|
+
drawing?: {
|
|
239
|
+
viewBox: number[];
|
|
240
|
+
fills: string[];
|
|
241
|
+
steps: {
|
|
242
|
+
layer: number;
|
|
243
|
+
d: any;
|
|
244
|
+
area: any;
|
|
245
|
+
}[];
|
|
246
|
+
};
|
|
247
|
+
svg: string;
|
|
248
|
+
};
|
|
249
|
+
/**
|
|
250
|
+
* 把 SVG 标记编码进 drawio 的 `image=` 值(`data:image/svg+xml,<百分号编码>`)。
|
|
251
|
+
*
|
|
252
|
+
* 为什么用百分号编码而不是 base64:编码后的串里一个 `;` 都没有,而 drawio 与本内核的
|
|
253
|
+
* style 解析都是按 `;` 分段 —— base64 形态的 `data:image/svg+xml;base64,` 那个分号是解析器的雷。
|
|
254
|
+
* 这里编得比 JSON 严格一点(`<` `>` `#` `%` `"` 和空格),换来的好处是:不管宿主把它塞进
|
|
255
|
+
* 属性还是直接写进 style,都不会有歧义。
|
|
256
|
+
*/
|
|
257
|
+
export declare function svgToImageValue(svg: any): string;
|
package/package.json
CHANGED