@huaqiu/component-gen-server 0.3.6
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/LICENSE +21 -0
- package/lib/index.d.mts +205 -0
- package/lib/index.mjs +636 -0
- package/lib/standalone.mjs +754 -0
- package/package.json +47 -0
- package/src/backend.ts +33 -0
- package/src/history.ts +135 -0
- package/src/index.ts +48 -0
- package/src/jobs.ts +279 -0
- package/src/routes.ts +227 -0
- package/src/standalone.ts +172 -0
- package/src/types.ts +101 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 深圳华秋智联股份有限公司
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/lib/index.d.mts
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
//#region src/types.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* `@huaqiu/component-gen-server` — shared domain types.
|
|
5
|
+
*
|
|
6
|
+
* These mirror `@huaqiu/component-gen-app/src/ports.ts` (structurally
|
|
7
|
+
* identical, JSON interop) but are owned by the server as the authority.
|
|
8
|
+
*/
|
|
9
|
+
type ComponentGenPage = 'symbol' | 'footprint';
|
|
10
|
+
type JobKind = 'symbol' | 'extract-footprint' | 'generate-footprint';
|
|
11
|
+
type JobStatus = 'queued' | 'running' | 'needs_confirmation' | 'completed' | 'failed' | 'cancelled';
|
|
12
|
+
interface JobInput {
|
|
13
|
+
imageDataUrl?: string;
|
|
14
|
+
instruction?: string;
|
|
15
|
+
packageType?: string;
|
|
16
|
+
dimensions?: Record<string, number>;
|
|
17
|
+
fileName?: string;
|
|
18
|
+
edited?: Record<string, boolean>;
|
|
19
|
+
}
|
|
20
|
+
interface JobState {
|
|
21
|
+
id: string;
|
|
22
|
+
kind: JobKind;
|
|
23
|
+
status: JobStatus;
|
|
24
|
+
progress?: string;
|
|
25
|
+
result?: Record<string, unknown>;
|
|
26
|
+
dimensions?: Record<string, unknown>;
|
|
27
|
+
pkgType?: string | null;
|
|
28
|
+
fileName?: string | null;
|
|
29
|
+
error?: string;
|
|
30
|
+
createdAt: string;
|
|
31
|
+
updatedAt: string;
|
|
32
|
+
}
|
|
33
|
+
type JobEvent = {
|
|
34
|
+
type: 'progress';
|
|
35
|
+
message: string;
|
|
36
|
+
at: string;
|
|
37
|
+
} | {
|
|
38
|
+
type: 'needs_confirmation';
|
|
39
|
+
dimensions: Record<string, unknown>;
|
|
40
|
+
pkgType?: string | null;
|
|
41
|
+
fileName?: string | null;
|
|
42
|
+
at: string;
|
|
43
|
+
} | {
|
|
44
|
+
type: 'completed';
|
|
45
|
+
job: JobState;
|
|
46
|
+
at: string;
|
|
47
|
+
} | {
|
|
48
|
+
type: 'failed';
|
|
49
|
+
error: string;
|
|
50
|
+
result?: Record<string, unknown>;
|
|
51
|
+
at: string;
|
|
52
|
+
} | {
|
|
53
|
+
type: 'cancelled';
|
|
54
|
+
at: string;
|
|
55
|
+
};
|
|
56
|
+
interface StartJobRequest {
|
|
57
|
+
kind: JobKind;
|
|
58
|
+
input: JobInput;
|
|
59
|
+
}
|
|
60
|
+
interface HistoryQuery {
|
|
61
|
+
limit?: number;
|
|
62
|
+
cursor?: string | null;
|
|
63
|
+
}
|
|
64
|
+
interface HistoryPage {
|
|
65
|
+
entries: HistoryEntry[];
|
|
66
|
+
nextCursor?: string | null;
|
|
67
|
+
}
|
|
68
|
+
interface HistoryEntry {
|
|
69
|
+
id: string;
|
|
70
|
+
kind: 'symbol' | 'footprint';
|
|
71
|
+
createdAt: string;
|
|
72
|
+
status: 'generated' | 'failed' | 'cancelled';
|
|
73
|
+
input: {
|
|
74
|
+
imageId?: string;
|
|
75
|
+
instruction?: string;
|
|
76
|
+
packageType?: string;
|
|
77
|
+
dimensions?: Record<string, number>;
|
|
78
|
+
};
|
|
79
|
+
edited?: Record<string, boolean>;
|
|
80
|
+
result?: {
|
|
81
|
+
artifactId: string;
|
|
82
|
+
filename: string;
|
|
83
|
+
fileUrl?: string;
|
|
84
|
+
size?: number;
|
|
85
|
+
};
|
|
86
|
+
error?: string;
|
|
87
|
+
}
|
|
88
|
+
interface HistoryPatch {
|
|
89
|
+
status?: HistoryEntry['status'];
|
|
90
|
+
result?: HistoryEntry['result'];
|
|
91
|
+
error?: string;
|
|
92
|
+
edited?: Record<string, boolean>;
|
|
93
|
+
}
|
|
94
|
+
interface ComponentGenConfig {
|
|
95
|
+
hostMode: boolean;
|
|
96
|
+
capabilities: {
|
|
97
|
+
symbol: boolean;
|
|
98
|
+
footprint: boolean;
|
|
99
|
+
};
|
|
100
|
+
limits: {
|
|
101
|
+
imageBytes: number;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
declare const COMPONENT_GEN_ROUTE_PREFIX = "/api/v1/huaqiu/component-gen";
|
|
105
|
+
declare const MAX_IMAGE_BYTES: number;
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/backend.d.ts
|
|
108
|
+
interface GenerationExec {
|
|
109
|
+
signal?: AbortSignal;
|
|
110
|
+
}
|
|
111
|
+
interface ComponentGenBackend {
|
|
112
|
+
generateSymbol(args: {
|
|
113
|
+
imageDataUrl: string;
|
|
114
|
+
instruction?: string;
|
|
115
|
+
}, exec: GenerationExec): Promise<Record<string, unknown>>;
|
|
116
|
+
extractFootprint(args: {
|
|
117
|
+
imageDataUrl: string;
|
|
118
|
+
packageType?: string;
|
|
119
|
+
instruction?: string;
|
|
120
|
+
}, exec: GenerationExec): Promise<Record<string, unknown>>;
|
|
121
|
+
generateFootprint(args: {
|
|
122
|
+
packageType: string;
|
|
123
|
+
fileName?: string;
|
|
124
|
+
dimensions: Record<string, number>;
|
|
125
|
+
}, exec: GenerationExec): Promise<Record<string, unknown>>;
|
|
126
|
+
}
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region src/history.d.ts
|
|
129
|
+
/** `data:image/...;base64,....` → { mime, bytes } | null. */
|
|
130
|
+
declare function parseDataUrl(dataUrl: string): {
|
|
131
|
+
mime: string;
|
|
132
|
+
bytes: Buffer;
|
|
133
|
+
} | null;
|
|
134
|
+
declare class HistoryStore {
|
|
135
|
+
private readonly dir;
|
|
136
|
+
private readonly file;
|
|
137
|
+
private entries;
|
|
138
|
+
constructor(dir: string);
|
|
139
|
+
/** All entries, newest first. */
|
|
140
|
+
private sorted;
|
|
141
|
+
append(entry: HistoryEntry): Promise<HistoryEntry>;
|
|
142
|
+
list(query: HistoryQuery): Promise<HistoryPage>;
|
|
143
|
+
get(id: string): Promise<HistoryEntry | null>;
|
|
144
|
+
patch(id: string, patch: HistoryPatch): Promise<HistoryEntry | null>;
|
|
145
|
+
delete(id: string): Promise<void>;
|
|
146
|
+
saveImage(imageId: string, dataUrl: string): Promise<void>;
|
|
147
|
+
readImage(imageId: string): Promise<{
|
|
148
|
+
bytes: Uint8Array;
|
|
149
|
+
mime: string;
|
|
150
|
+
} | null>;
|
|
151
|
+
}
|
|
152
|
+
declare function newHistoryId(): string;
|
|
153
|
+
declare function newImageId(): string;
|
|
154
|
+
//#endregion
|
|
155
|
+
//#region src/routes.d.ts
|
|
156
|
+
interface ComponentGenHandlerDeps {
|
|
157
|
+
backend: ComponentGenBackend;
|
|
158
|
+
history: HistoryStore;
|
|
159
|
+
hostMode?: boolean;
|
|
160
|
+
getAccessToken?: () => Promise<string | null>;
|
|
161
|
+
}
|
|
162
|
+
type ComponentGenHandler = (req: IncomingMessage, res: ServerResponse) => Promise<void> | void;
|
|
163
|
+
declare function createComponentGenHandler(deps: ComponentGenHandlerDeps): ComponentGenHandler;
|
|
164
|
+
//#endregion
|
|
165
|
+
//#region src/jobs.d.ts
|
|
166
|
+
interface JobMeta {
|
|
167
|
+
/** stored input thumbnail id (points into the history store's inputs/). */
|
|
168
|
+
imageId?: string;
|
|
169
|
+
}
|
|
170
|
+
declare class JobStore {
|
|
171
|
+
private readonly jobs;
|
|
172
|
+
private readonly listeners;
|
|
173
|
+
create(req: StartJobRequest, meta: JobMeta): JobState;
|
|
174
|
+
get(id: string): JobState | undefined;
|
|
175
|
+
abort(id: string): boolean;
|
|
176
|
+
signal(id: string): AbortSignal | undefined;
|
|
177
|
+
subscribe(id: string, cb: (e: JobEvent) => void): (() => void) | null;
|
|
178
|
+
private emit;
|
|
179
|
+
/** Update job state (public — the runner writes progress/status). */
|
|
180
|
+
update(id: string, patch: Partial<JobState>, event?: JobEvent): JobState;
|
|
181
|
+
/** Update + emit the canonical event for a terminal state. */
|
|
182
|
+
settle(id: string, patch: Partial<JobState>): JobState;
|
|
183
|
+
remove(id: string): void;
|
|
184
|
+
}
|
|
185
|
+
interface RunOutcome {
|
|
186
|
+
state: JobState;
|
|
187
|
+
/** whether a history entry was appended. */
|
|
188
|
+
recorded: boolean;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Run one generation to a terminal state. Returns the final JobState.
|
|
192
|
+
* History recording happens here so entry and state cannot drift.
|
|
193
|
+
*/
|
|
194
|
+
declare function runGeneration(store: JobStore, backend: ComponentGenBackend, history: HistoryStore, id: string, req: StartJobRequest, meta: JobMeta, onProgress?: (message: string) => void): Promise<RunOutcome>;
|
|
195
|
+
//#endregion
|
|
196
|
+
//#region src/index.d.ts
|
|
197
|
+
interface ComponentGenWebRoute {
|
|
198
|
+
kind: 'prefix';
|
|
199
|
+
path: string;
|
|
200
|
+
handler: ReturnType<typeof createComponentGenHandler>;
|
|
201
|
+
}
|
|
202
|
+
/** Build the DSH `webServer.register(...)` route object. */
|
|
203
|
+
declare function createComponentGenRoutes(deps: ComponentGenHandlerDeps): ComponentGenWebRoute;
|
|
204
|
+
//#endregion
|
|
205
|
+
export { COMPONENT_GEN_ROUTE_PREFIX, type ComponentGenBackend, type ComponentGenConfig, type ComponentGenHandler, type ComponentGenHandlerDeps, type ComponentGenPage, ComponentGenWebRoute, type GenerationExec, type HistoryEntry, type HistoryPage, type HistoryPatch, type HistoryQuery, HistoryStore, type JobEvent, type JobInput, type JobKind, type JobMeta, type JobState, JobStore, MAX_IMAGE_BYTES, type RunOutcome, type StartJobRequest, createComponentGenHandler, createComponentGenRoutes, newHistoryId, newImageId, parseDataUrl, runGeneration };
|