light-chain-open-ui 1.0.0 → 1.0.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/README.md +233 -0
- package/dist/{button-Bvgi18zf.js → button-CREPu5oG.js} +270 -2256
- package/dist/button-DE8_GX7q.cjs +1101 -0
- package/dist/client-C5AgfBeT.js +1592 -0
- package/dist/client-Dt9_oZey.cjs +1 -0
- package/dist/constants-BTXokn3H.cjs +1 -0
- package/dist/constants-DmzJOR2G.js +399 -0
- package/dist/contants.cjs +1 -0
- package/dist/contants.mjs +4 -0
- package/dist/core-C5cKUOSx.js +1 -0
- package/dist/core-DkseUrte.cjs +1 -0
- package/dist/lightchain-ui.cjs +1 -1
- package/dist/lightchain-ui.mjs +5 -2
- package/dist/options.cjs +1 -0
- package/dist/options.mjs +2 -0
- package/dist/react.cjs +1 -1
- package/dist/react.mjs +13 -12
- package/dist/types/contants.d.ts +2 -0
- package/dist/types/options.d.ts +2 -0
- package/dist/types/types.d.ts +4 -0
- package/dist/types.cjs +0 -0
- package/dist/types.mjs +0 -0
- package/package.json +18 -1
- package/dist/button-DyttCdLa.cjs +0 -1101
package/README.md
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
# light-chain-open-ui
|
|
2
|
+
|
|
3
|
+
LightChain Open UI 是一组用于发起 AI 任务 workflow 的 Web Components、React 组件和前端任务客户端。
|
|
4
|
+
|
|
5
|
+
它适合两种接入方式:
|
|
6
|
+
|
|
7
|
+
- 使用 `lc-task-workflow` / `LcTaskWorkflowReact` 自动收集表单、校验字段、提交任务和轮询结果。
|
|
8
|
+
- 使用 `LightChainClient` 手动提交多个任务,并让同一个客户端合并轮询。
|
|
9
|
+
|
|
10
|
+
## 安装
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install light-chain-open-ui
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
React 项目需要同时安装 React:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install react react-dom
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## 导入入口
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import 'light-chain-open-ui'
|
|
26
|
+
import { LightChainClient } from 'light-chain-open-ui'
|
|
27
|
+
import { LcTaskWorkflowReact } from 'light-chain-open-ui/react'
|
|
28
|
+
import { OPTIONS_ASPECT_RATIO } from 'light-chain-open-ui/options'
|
|
29
|
+
import { TASK_WORKFLOW_TYPES } from 'light-chain-open-ui/contants'
|
|
30
|
+
import type { TaskWorkflowType } from 'light-chain-open-ui/types'
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
入口说明:
|
|
34
|
+
|
|
35
|
+
- `light-chain-open-ui`:注册 Web Components,并导出 `LightChainClient` 等核心能力。
|
|
36
|
+
- `light-chain-open-ui/react`:导出 React 封装组件。
|
|
37
|
+
- `light-chain-open-ui/options`:导出可选值参数,例如 `OPTIONS_ASPECT_RATIO`、`OPTIONS_GENDER`。
|
|
38
|
+
- `light-chain-open-ui/contants`:导出常量,例如 `TASK_WORKFLOW_TYPES`、`WORKFLOW_DEFAULTS`。
|
|
39
|
+
- `light-chain-open-ui/types`:导出 TypeScript 类型。
|
|
40
|
+
|
|
41
|
+
## React 自动任务
|
|
42
|
+
|
|
43
|
+
自动任务适合单个独立任务。组件内部会创建任务客户端,收集子组件字段并完成提交和轮询。
|
|
44
|
+
|
|
45
|
+
```tsx
|
|
46
|
+
import {
|
|
47
|
+
LcButtonReact,
|
|
48
|
+
LcImageUploaderReact,
|
|
49
|
+
LcPromptInputReact,
|
|
50
|
+
LcTaskWorkflowReact,
|
|
51
|
+
} from 'light-chain-open-ui/react'
|
|
52
|
+
import { TASK_WORKFLOW_TYPES } from 'light-chain-open-ui/contants'
|
|
53
|
+
|
|
54
|
+
export function FixFaceForm() {
|
|
55
|
+
return (
|
|
56
|
+
<LcTaskWorkflowReact
|
|
57
|
+
type={TASK_WORKFLOW_TYPES.FIX_FACE}
|
|
58
|
+
customRequest={customRequest}
|
|
59
|
+
customUploadRequest={customUploadRequest}
|
|
60
|
+
onSubmit={(event) => console.log(event.detail.taskId)}
|
|
61
|
+
onSuccess={(event) => console.log(event.detail.images)}
|
|
62
|
+
onError={(event) => console.error(event.detail.message)}
|
|
63
|
+
>
|
|
64
|
+
<LcImageUploaderReact name="imgUrl" label="模特图" required />
|
|
65
|
+
<LcPromptInputReact name="prompt" label="修复要求" maxLength={500} />
|
|
66
|
+
<LcButtonReact type="submit">开始生成</LcButtonReact>
|
|
67
|
+
</LcTaskWorkflowReact>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## 原生/Vue 自动任务
|
|
73
|
+
|
|
74
|
+
Web Components 通过属性和 DOM property 配置。函数类型参数需要通过 property 赋值。
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import 'light-chain-open-ui'
|
|
78
|
+
import { TASK_WORKFLOW_TYPES } from 'light-chain-open-ui/contants'
|
|
79
|
+
|
|
80
|
+
const workflow = document.querySelector('lc-task-workflow')
|
|
81
|
+
|
|
82
|
+
workflow.type = TASK_WORKFLOW_TYPES.FIX_FACE
|
|
83
|
+
workflow.customRequest = customRequest
|
|
84
|
+
workflow.customUploadRequest = customUploadRequest
|
|
85
|
+
|
|
86
|
+
workflow.addEventListener('success', (event) => {
|
|
87
|
+
console.log(event.detail.images)
|
|
88
|
+
})
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
```html
|
|
92
|
+
<lc-task-workflow>
|
|
93
|
+
<lc-image-uploader name="imgUrl" label="模特图" required></lc-image-uploader>
|
|
94
|
+
<lc-prompt-input name="prompt" label="修复要求"></lc-prompt-input>
|
|
95
|
+
<lc-button type="submit">开始生成</lc-button>
|
|
96
|
+
</lc-task-workflow>
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## 手动任务客户端
|
|
100
|
+
|
|
101
|
+
手动模式适合批量任务或跨页面共享任务状态。`LightChainClient` 建议只创建一次,多个 `submit(type, formData)` 会基于同一个客户端合并轮询。
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
import { LightChainClient } from 'light-chain-open-ui'
|
|
105
|
+
import { TASK_WORKFLOW_TYPES } from 'light-chain-open-ui/contants'
|
|
106
|
+
|
|
107
|
+
const client = new LightChainClient({
|
|
108
|
+
customRequest,
|
|
109
|
+
customUploadRequest,
|
|
110
|
+
onSubmit: ({ taskId }) => console.log('submitted', taskId),
|
|
111
|
+
onPoll: ({ taskId, progress }) => console.log('polling', taskId, progress),
|
|
112
|
+
onSuccess: ({ taskId, images }) => console.log('success', taskId, images),
|
|
113
|
+
onError: ({ message }) => console.error(message),
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
await client.submit(TASK_WORKFLOW_TYPES.FIX_FACE, {
|
|
117
|
+
imgUrl: 'https://example.com/model.png',
|
|
118
|
+
prompt: '修复面部细节',
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
await client.submit(TASK_WORKFLOW_TYPES.SR, {
|
|
122
|
+
imgUrl: 'https://example.com/image.png',
|
|
123
|
+
scale: '2',
|
|
124
|
+
})
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## 自定义请求
|
|
128
|
+
|
|
129
|
+
`customRequest` 用于接入业务后端。组件会根据 `type` 自动生成提交地址,例如 `/task/submit/FixFace`。
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
const customRequest = async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
133
|
+
const endpoint = typeof input === 'string' ? input : input.toString()
|
|
134
|
+
const url = new URL(endpoint, 'https://api.example.com')
|
|
135
|
+
|
|
136
|
+
return fetch(url.toString(), {
|
|
137
|
+
...init,
|
|
138
|
+
headers: {
|
|
139
|
+
...init?.headers,
|
|
140
|
+
'client-secret': getClientSecret(),
|
|
141
|
+
},
|
|
142
|
+
})
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
`customUploadRequest` 用于把 `File` 上传成 URL,再提交给任务接口。
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
const customUploadRequest = async ({ formData }) => {
|
|
150
|
+
const response = await fetch('/upload', {
|
|
151
|
+
method: 'POST',
|
|
152
|
+
body: formData,
|
|
153
|
+
})
|
|
154
|
+
const data = await response.json()
|
|
155
|
+
return data.url
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## 枚举参数
|
|
160
|
+
|
|
161
|
+
枚举选择器可以直接使用 `light-chain-open-ui/options` 导出的参数。
|
|
162
|
+
|
|
163
|
+
```tsx
|
|
164
|
+
import { LcEnumSelectorReact } from 'light-chain-open-ui/react'
|
|
165
|
+
import { OPTIONS_ASPECT_RATIO } from 'light-chain-open-ui/options'
|
|
166
|
+
|
|
167
|
+
<LcEnumSelectorReact
|
|
168
|
+
name="aspectRatio"
|
|
169
|
+
label="宽高比"
|
|
170
|
+
mode="select"
|
|
171
|
+
options={JSON.stringify(OPTIONS_ASPECT_RATIO)}
|
|
172
|
+
/>
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## 主题
|
|
176
|
+
|
|
177
|
+
默认样式会随组件一起工作。需要切换暗色主题时,引入主题样式并在页面上添加主题标记。
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
import 'light-chain-open-ui/src/styles/themes/default.css'
|
|
181
|
+
import 'light-chain-open-ui/src/styles/themes/dark.css'
|
|
182
|
+
|
|
183
|
+
document.documentElement.dataset.theme = 'dark'
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## 后端接入约定
|
|
187
|
+
|
|
188
|
+
前端提交任务后,业务后端应立即调用外部 AI 接口、落库任务记录,并返回任务 ID。前端随后通过轮询接口获取任务状态。
|
|
189
|
+
|
|
190
|
+
推荐接口:
|
|
191
|
+
|
|
192
|
+
- `POST /task/submit/{type}`:根据 `type` 调用对应外部接口,创建任务记录,返回 `{ code, success, data: { taskId } }`。
|
|
193
|
+
- `GET /task/progress?taskIds=a,b,c`:批量查询任务状态,返回 `{ code, success, data: TaskProgressResult[] }`。
|
|
194
|
+
|
|
195
|
+
响应示例:
|
|
196
|
+
|
|
197
|
+
```json
|
|
198
|
+
{
|
|
199
|
+
"code": 200,
|
|
200
|
+
"success": true,
|
|
201
|
+
"data": {
|
|
202
|
+
"taskId": "task_123"
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
进度结果示例:
|
|
208
|
+
|
|
209
|
+
```json
|
|
210
|
+
{
|
|
211
|
+
"code": 200,
|
|
212
|
+
"success": true,
|
|
213
|
+
"data": [
|
|
214
|
+
{
|
|
215
|
+
"aiTaskId": "task_123",
|
|
216
|
+
"aiTaskStatus": "done",
|
|
217
|
+
"queuePos": null,
|
|
218
|
+
"taskProgress": 1,
|
|
219
|
+
"aiTaskResult": null,
|
|
220
|
+
"imgInfo": "https://example.com/result.png"
|
|
221
|
+
}
|
|
222
|
+
]
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## 打包与发布
|
|
227
|
+
|
|
228
|
+
```bash
|
|
229
|
+
npm run build
|
|
230
|
+
npm publish --access public
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
如果 npm 开启了双因素认证,需要使用支持 publish 的 automation token,或在发布时输入 OTP。
|