sang-ai-kit 0.1.1 → 0.2.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 +9 -8
- package/generated/sang-ai-utilities.css +120 -1
- package/package.json +3 -1
- package/src/AiAssistant.tsx +20 -33
- package/src/AiSettings.tsx +6 -3
- package/src/ModelControls.tsx +3 -11
- package/src/SessionList.tsx +4 -1
- package/src/SkillSettings.tsx +4 -2
- package/src/TokenUsageBar.tsx +4 -1
- package/src/assistant.css +2125 -1712
- package/src/assistantTypes.ts +7 -2
- package/src/background-removal/BackgroundRemovalSettings.tsx +502 -0
- package/src/background-removal/ColorMattingEditor.tsx +649 -0
- package/src/background-removal/background-removal.css +294 -0
- package/src/background-removal/color-matting.css +838 -0
- package/src/background-removal/colorMatting.ts +117 -0
- package/src/background-removal/colorMatting.worker.ts +21 -0
- package/src/background-removal/index.ts +92 -0
- package/src/background-removal/inference.worker.ts +117 -0
- package/src/background-removal/models.ts +65 -0
- package/src/background-removal/processing.ts +61 -0
- package/src/background-removal/resources.ts +114 -0
- package/src/background-removal/status.ts +23 -0
- package/src/background-removal/storage.ts +56 -0
- package/src/background-removal/tool.ts +70 -0
- package/src/background-removal/types.ts +38 -0
- package/src/background-removal/useResources.ts +38 -0
- package/src/builtInToolDefinitions.ts +68 -0
- package/src/builtInTools.ts +66 -22
- package/src/bun-env.d.ts +25 -0
- package/src/conversationUtils.ts +71 -1
- package/src/endpointConnection.ts +2 -2
- package/src/features.ts +3 -1
- package/src/imageToolHost.ts +86 -0
- package/src/index.ts +15 -6
- package/src/modelConfig.ts +49 -7
- package/src/openai.ts +56 -30
- package/src/responses.ts +14 -0
- package/src/settingsTypes.ts +6 -8
- package/src/skills.ts +1 -1
- package/src/style.css +111 -67
- package/src/useAssistant.ts +5 -38
- package/src/useResolvedTheme.ts +2 -1
package/src/assistantTypes.ts
CHANGED
|
@@ -34,12 +34,17 @@ export interface ToolEvent extends SessionTool {
|
|
|
34
34
|
result?: string;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
export type AssistantActivityEvent =
|
|
38
|
+
| { id: string; type: 'text'; text: string }
|
|
39
|
+
| { id: string; type: 'reasoning'; text: string }
|
|
40
|
+
| { id: string; type: 'tool'; toolId: string };
|
|
41
|
+
|
|
37
42
|
export interface ChatEntry {
|
|
38
43
|
id: string;
|
|
39
44
|
role: 'user' | 'assistant';
|
|
40
45
|
content: string;
|
|
41
46
|
responseItemCount?: number;
|
|
42
|
-
activity?:
|
|
47
|
+
activity?: AssistantActivityEvent[];
|
|
43
48
|
tools?: ToolEvent[];
|
|
44
49
|
continuable?: boolean;
|
|
45
50
|
images?: SessionImage[];
|
|
@@ -125,7 +130,7 @@ export interface AssistantViewProps {
|
|
|
125
130
|
quickSnippets: string[];
|
|
126
131
|
features?: Partial<AiFeatures>;
|
|
127
132
|
onFork?: (event: AssistantForkEvent) => void;
|
|
128
|
-
active: { endpoint
|
|
133
|
+
active: { endpoint?: unknown; model: { label?: string; supportsVision: boolean } };
|
|
129
134
|
imageToolsEnabled?: boolean;
|
|
130
135
|
toolLabels?: Record<string, [string, string]>;
|
|
131
136
|
loadImage: (sessionId: string, imageId: string) => Promise<Blob | null>;
|
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Check,
|
|
4
|
+
CheckCircle2,
|
|
5
|
+
Download,
|
|
6
|
+
ExternalLink,
|
|
7
|
+
LoaderCircle,
|
|
8
|
+
Scissors,
|
|
9
|
+
ShieldCheck,
|
|
10
|
+
Trash2,
|
|
11
|
+
Upload,
|
|
12
|
+
X,
|
|
13
|
+
Zap,
|
|
14
|
+
Layers,
|
|
15
|
+
Cpu,
|
|
16
|
+
FileCode,
|
|
17
|
+
AlertCircle
|
|
18
|
+
} from 'lucide-react';
|
|
19
|
+
import { MODELS, type ModelId } from './models.ts';
|
|
20
|
+
import { installModel, verifyModel } from './index.ts';
|
|
21
|
+
import { downloadModel, importRuntime, installRuntime, loadResources, RUNTIME_ASSETS, RUNTIME_BASE } from './resources.ts';
|
|
22
|
+
import { deleteModel, deleteRuntime, readSettings, RUNTIME_VERSION, saveSettings } from './storage.ts';
|
|
23
|
+
import { errorText, progressText } from './status.ts';
|
|
24
|
+
import { useResources } from './useResources.ts';
|
|
25
|
+
import type { DeviceMode, Progress } from './types.ts';
|
|
26
|
+
import './background-removal.css';
|
|
27
|
+
|
|
28
|
+
export default function BackgroundRemovalSettings({ english }: { english: boolean }) {
|
|
29
|
+
const state = useResources();
|
|
30
|
+
const [busy, setBusy] = useState(false);
|
|
31
|
+
const [progress, setProgress] = useState<Progress>();
|
|
32
|
+
const [error, setError] = useState('');
|
|
33
|
+
const [success, setSuccess] = useState('');
|
|
34
|
+
const controller = useRef<AbortController | null>(null);
|
|
35
|
+
const runtimeInput = useRef<HTMLInputElement>(null);
|
|
36
|
+
const mounted = useRef(true);
|
|
37
|
+
const text = (zh: string, en: string) => english ? en : zh;
|
|
38
|
+
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
mounted.current = true;
|
|
41
|
+
return () => { mounted.current = false; controller.current?.abort(); };
|
|
42
|
+
}, []);
|
|
43
|
+
|
|
44
|
+
const perform = async (operation: (signal: AbortSignal, report: (value: Progress) => void) => Promise<unknown>) => {
|
|
45
|
+
if (controller.current) return;
|
|
46
|
+
const abort = new AbortController();
|
|
47
|
+
controller.current = abort;
|
|
48
|
+
setBusy(true);
|
|
49
|
+
setError('');
|
|
50
|
+
setSuccess('');
|
|
51
|
+
setProgress(undefined);
|
|
52
|
+
try {
|
|
53
|
+
await operation(abort.signal, value => { if (mounted.current) setProgress(value); });
|
|
54
|
+
if (mounted.current && !abort.signal.aborted) setSuccess(text('已完成', 'Completed'));
|
|
55
|
+
} catch (reason) {
|
|
56
|
+
if (mounted.current && !abort.signal.aborted) setError(errorText(reason, english));
|
|
57
|
+
} finally {
|
|
58
|
+
controller.current = null;
|
|
59
|
+
if (mounted.current) { setBusy(false); setProgress(undefined); }
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const update = (patch: { modelId?: ModelId; device?: DeviceMode }) => perform(async signal => {
|
|
64
|
+
const settings = await readSettings();
|
|
65
|
+
signal.throwIfAborted();
|
|
66
|
+
await saveSettings({ ...settings, ...patch });
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const acceptModel = async (id: ModelId, blob: Blob, name: string, signal: AbortSignal, report: (value: Progress) => void) => {
|
|
70
|
+
await installModel(id, blob, name, state.settings.device, { signal, onProgress: report });
|
|
71
|
+
signal.throwIfAborted();
|
|
72
|
+
await saveSettings({ ...await readSettings(), modelId: id });
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const progressPercent = progress?.total && progress?.loaded
|
|
76
|
+
? Math.min(100, Math.round((progress.loaded / progress.total) * 100))
|
|
77
|
+
: undefined;
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<section className="cf-removal-settings space-y-6 text-slate-200" aria-label={text('抠图设置', 'Background removal settings')}>
|
|
81
|
+
{/* 顶部标题与说明卡片 */}
|
|
82
|
+
<div className="cf-removal-banner p-5 rounded-2xl border border-blue-500/25 bg-gradient-to-r from-blue-950/30 via-slate-900/60 to-indigo-950/20 shadow-lg shadow-blue-950/10 space-y-3">
|
|
83
|
+
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3">
|
|
84
|
+
<div className="cf-removal-heading flex items-center gap-3 !mb-0">
|
|
85
|
+
<div className="w-10 h-10 rounded-xl bg-gradient-to-br from-blue-500/20 to-indigo-500/10 border border-blue-500/30 text-blue-400 flex items-center justify-center shadow-inner shrink-0">
|
|
86
|
+
<Scissors size={18} />
|
|
87
|
+
</div>
|
|
88
|
+
<div>
|
|
89
|
+
<h3 className="text-base font-bold text-white tracking-tight flex items-center gap-2 flex-wrap">
|
|
90
|
+
<span>{text('本地离线智能抠图', 'Local AI Background Removal')}</span>
|
|
91
|
+
<span className="text-[10px] px-2 py-0.5 rounded-full bg-blue-500/15 text-blue-300 font-mono border border-blue-500/30">
|
|
92
|
+
ONNX Web
|
|
93
|
+
</span>
|
|
94
|
+
</h3>
|
|
95
|
+
<p className="text-xs text-slate-400 mt-0.5">
|
|
96
|
+
{text(
|
|
97
|
+
'首次抠图自动下载缺失模型和运行时,推理在浏览器本地执行',
|
|
98
|
+
'Missing models and runtime download automatically on first use; images are processed locally.'
|
|
99
|
+
)}
|
|
100
|
+
</p>
|
|
101
|
+
</div>
|
|
102
|
+
</div>
|
|
103
|
+
|
|
104
|
+
<div className="flex items-center gap-2 flex-wrap shrink-0">
|
|
105
|
+
<span className="cf-removal-pill text-[11px] px-2.5 py-1 rounded-lg bg-slate-900/80 border border-slate-800 text-slate-300 flex items-center gap-1.5 shadow-sm">
|
|
106
|
+
<ShieldCheck size={13} className="text-emerald-400" />
|
|
107
|
+
<span>{text('本地隐私安全', 'Local Privacy')}</span>
|
|
108
|
+
</span>
|
|
109
|
+
<span className="cf-removal-pill text-[11px] px-2.5 py-1 rounded-lg bg-slate-900/80 border border-slate-800 text-slate-300 flex items-center gap-1.5 shadow-sm">
|
|
110
|
+
<Zap size={13} className="text-amber-400" />
|
|
111
|
+
<span>{text('WebGPU / WASM 加速', 'Hardware Accelerated')}</span>
|
|
112
|
+
</span>
|
|
113
|
+
</div>
|
|
114
|
+
</div>
|
|
115
|
+
</div>
|
|
116
|
+
|
|
117
|
+
{/* 推理与计算设备参数卡片 */}
|
|
118
|
+
<div className="cf-removal-card p-5 rounded-2xl bg-slate-900/40 border border-slate-800/80 space-y-4 shadow-sm">
|
|
119
|
+
<div className="flex items-center gap-2 pb-2 border-b border-slate-800/80">
|
|
120
|
+
<Cpu size={16} className="text-blue-400" />
|
|
121
|
+
<h4 className="text-xs font-bold text-white uppercase tracking-wider">
|
|
122
|
+
{text('推理参数与执行设备', 'Inference & Compute Settings')}
|
|
123
|
+
</h4>
|
|
124
|
+
</div>
|
|
125
|
+
|
|
126
|
+
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
127
|
+
<div className="space-y-1.5">
|
|
128
|
+
<label htmlFor="removal-active-model" className="block text-xs font-semibold text-slate-300">
|
|
129
|
+
{text('当前生效模型', 'Active Model')}
|
|
130
|
+
</label>
|
|
131
|
+
<div className="relative">
|
|
132
|
+
<select
|
|
133
|
+
id="removal-active-model"
|
|
134
|
+
disabled={busy || state.loading}
|
|
135
|
+
value={state.settings.modelId}
|
|
136
|
+
onChange={event => void update({ modelId: event.target.value as ModelId })}
|
|
137
|
+
className="w-full bg-slate-950/80 border border-slate-800 focus:border-blue-500/80 focus:ring-1 focus:ring-blue-500/30 rounded-xl px-3.5 py-2.5 text-xs text-slate-100 outline-none transition-all cursor-pointer"
|
|
138
|
+
>
|
|
139
|
+
{MODELS.map(model => <option key={model.id} value={model.id}>
|
|
140
|
+
{model.name} · {((state.models[model.id]?.blob.size ?? model.size) / 1_000_000).toFixed(1)} MB
|
|
141
|
+
{!state.models[model.id] ? text(' [使用时自动下载]', ' [Auto-download on use]') : ''}
|
|
142
|
+
</option>)}
|
|
143
|
+
</select>
|
|
144
|
+
</div>
|
|
145
|
+
<p className="text-[11px] text-slate-500 leading-relaxed">
|
|
146
|
+
{state.settings.modelId === 'modnet'
|
|
147
|
+
? text('人像专用,默认 FP32,512×512 输入,固定 CPU/WASM 稳定模式。', 'Portrait matting, FP32 by default, 512×512 input, fixed CPU/WASM stable mode.')
|
|
148
|
+
: state.settings.modelId === 'birefnet'
|
|
149
|
+
? text('BiRefNet 轻量 512 本地版;旧模型需重新下载。', 'Local BiRefNet Lite 512; older cached models must be downloaded again.')
|
|
150
|
+
: text('快速轻量,日常人像、商品电商与扁平图标首选。', 'Fast and compact, great for everyday portraits, merchandise, and icons.')}
|
|
151
|
+
</p>
|
|
152
|
+
</div>
|
|
153
|
+
|
|
154
|
+
<div className="space-y-1.5">
|
|
155
|
+
<label htmlFor="removal-device" className="block text-xs font-semibold text-slate-300">
|
|
156
|
+
{text('计算加速设备', 'Compute Device')}
|
|
157
|
+
</label>
|
|
158
|
+
<div className="relative">
|
|
159
|
+
<select
|
|
160
|
+
id="removal-device"
|
|
161
|
+
disabled={busy || state.loading}
|
|
162
|
+
value={state.settings.device}
|
|
163
|
+
onChange={event => void update({ device: event.target.value as DeviceMode })}
|
|
164
|
+
className="w-full bg-slate-950/80 border border-slate-800 focus:border-blue-500/80 focus:ring-1 focus:ring-blue-500/30 rounded-xl px-3.5 py-2.5 text-xs text-slate-100 outline-none transition-all cursor-pointer"
|
|
165
|
+
>
|
|
166
|
+
<option value="auto">{text('自动(GPU 优先,不兼容时自动回退)', 'Auto (GPU preferred with CPU fallback)')}</option>
|
|
167
|
+
<option value="webgpu">WebGPU ({text('显卡硬件加速,速度最快', 'Hardware GPU acceleration, fastest')})</option>
|
|
168
|
+
<option value="wasm">CPU / WASM SIMD ({text('通用 CPU 单线程兼容模式', 'Universal single-threaded CPU')})</option>
|
|
169
|
+
</select>
|
|
170
|
+
</div>
|
|
171
|
+
<p className="text-[11px] text-slate-500 leading-relaxed">
|
|
172
|
+
{state.settings.modelId === 'modnet'
|
|
173
|
+
? text('MODNet 已固定使用 CPU/WASM,避免当前 WebGPU 输出异常;此设备选项仍保留给其他模型。', 'MODNet always uses CPU/WASM to avoid incorrect WebGPU output; this device preference still applies to other models.')
|
|
174
|
+
: text('推荐 Chrome 113+ 或 Edge 启用 WebGPU 获得极速推理体验。', 'Chrome 113+ or Edge with WebGPU recommended for maximum performance.')}
|
|
175
|
+
</p>
|
|
176
|
+
</div>
|
|
177
|
+
</div>
|
|
178
|
+
</div>
|
|
179
|
+
|
|
180
|
+
{/* 运行时核心引擎卡片 */}
|
|
181
|
+
<div className="cf-removal-runtime cf-removal-card !m-0 p-5 rounded-2xl bg-slate-900/40 border border-slate-800/80 space-y-4 shadow-sm">
|
|
182
|
+
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 pb-3 border-b border-slate-800/80">
|
|
183
|
+
<div className="space-y-1">
|
|
184
|
+
<div className="flex items-center gap-2 flex-wrap">
|
|
185
|
+
<strong className="text-sm font-bold text-white tracking-tight">
|
|
186
|
+
ONNX Runtime Web {RUNTIME_VERSION}
|
|
187
|
+
</strong>
|
|
188
|
+
<span className={`text-xs px-2.5 py-0.5 rounded-full font-semibold border ${
|
|
189
|
+
state.runtime
|
|
190
|
+
? 'bg-emerald-500/15 text-emerald-300 border-emerald-500/30 cf-removal-ready'
|
|
191
|
+
: 'bg-amber-500/15 text-amber-300 border-amber-500/30'
|
|
192
|
+
}`}>
|
|
193
|
+
{state.runtime
|
|
194
|
+
? text('✓ 已就绪 · 已持久化缓存', '✓ Cached · Offline ready')
|
|
195
|
+
: text('未安装 · 约 24 MB 运行库', 'Not installed · ~24 MB')}
|
|
196
|
+
</span>
|
|
197
|
+
</div>
|
|
198
|
+
<p className="text-xs text-slate-400">
|
|
199
|
+
{text(
|
|
200
|
+
'在浏览器运行神经网络的基础执行环境(包含 WASM SIMD 与 WebGPU 算子库)。',
|
|
201
|
+
'Core runtime for executing deep learning models in browser (WASM SIMD and WebGPU shaders).'
|
|
202
|
+
)}
|
|
203
|
+
</p>
|
|
204
|
+
</div>
|
|
205
|
+
|
|
206
|
+
<div className="cf-removal-actions flex items-center gap-2 flex-wrap shrink-0">
|
|
207
|
+
{!state.runtime && (
|
|
208
|
+
<button
|
|
209
|
+
type="button"
|
|
210
|
+
disabled={busy}
|
|
211
|
+
onClick={() => void perform((signal, report) => installRuntime(signal, report))}
|
|
212
|
+
className="px-3.5 py-2 rounded-xl text-xs font-bold bg-gradient-to-r from-blue-600 to-indigo-600 hover:from-blue-500 hover:to-indigo-500 text-white shadow-md shadow-blue-900/20 flex items-center gap-1.5 transition-all active:scale-[0.98] cursor-pointer"
|
|
213
|
+
>
|
|
214
|
+
<Download size={14} />
|
|
215
|
+
<span>{text('一键下载运行时', 'Download runtime')}</span>
|
|
216
|
+
</button>
|
|
217
|
+
)}
|
|
218
|
+
<button
|
|
219
|
+
type="button"
|
|
220
|
+
disabled={busy}
|
|
221
|
+
onClick={() => runtimeInput.current?.click()}
|
|
222
|
+
title={text('导入同版本运行时文件(.mjs / .wasm)', 'Import trusted runtime files of the same version')}
|
|
223
|
+
className="cf-removal-secondary-btn px-3.5 py-2 rounded-xl text-xs font-semibold bg-slate-800 hover:bg-slate-700 text-slate-200 border border-slate-700/80 flex items-center gap-1.5 transition-all cursor-pointer"
|
|
224
|
+
>
|
|
225
|
+
<Upload size={14} />
|
|
226
|
+
<span>{text('导入运行时', 'Import runtime')}</span>
|
|
227
|
+
</button>
|
|
228
|
+
{state.runtime && (
|
|
229
|
+
<button
|
|
230
|
+
type="button"
|
|
231
|
+
disabled={busy}
|
|
232
|
+
title={text('清除运行时缓存', 'Clear runtime cache')}
|
|
233
|
+
aria-label={text('清除运行时缓存', 'Clear runtime cache')}
|
|
234
|
+
onClick={() => void perform(() => deleteRuntime())}
|
|
235
|
+
className="px-3 py-2 rounded-xl text-xs font-semibold bg-rose-500/10 hover:bg-rose-500/20 text-rose-400 border border-rose-500/30 flex items-center gap-1.5 transition-colors cursor-pointer"
|
|
236
|
+
>
|
|
237
|
+
<Trash2 size={14} />
|
|
238
|
+
<span>{text('清除缓存', 'Clear')}</span>
|
|
239
|
+
</button>
|
|
240
|
+
)}
|
|
241
|
+
</div>
|
|
242
|
+
</div>
|
|
243
|
+
|
|
244
|
+
{/* 离线运行时文件清单 */}
|
|
245
|
+
<details className="group rounded-xl border border-slate-800/80 bg-slate-950/40 p-3 transition-all">
|
|
246
|
+
<summary className="cursor-pointer text-xs font-semibold text-slate-400 group-open:text-slate-200 flex items-center gap-2 select-none">
|
|
247
|
+
<FileCode size={13} className="text-blue-400" />
|
|
248
|
+
<span>{text('离线部署核心文件清单 (CDN / 局域网离线包)', 'Offline runtime asset files (CDN / Intranet pack)')}</span>
|
|
249
|
+
</summary>
|
|
250
|
+
<div className="mt-3 pt-3 border-t border-slate-800/60 grid grid-cols-1 sm:grid-cols-3 gap-2">
|
|
251
|
+
{Object.values(RUNTIME_ASSETS).map(name => (
|
|
252
|
+
<a
|
|
253
|
+
key={name}
|
|
254
|
+
href={RUNTIME_BASE + name}
|
|
255
|
+
target="_blank"
|
|
256
|
+
rel="noreferrer"
|
|
257
|
+
className="p-2 rounded-lg bg-slate-900/60 hover:bg-slate-800/80 border border-slate-800 text-[11px] text-blue-300 hover:text-blue-200 flex items-center justify-between gap-1 transition-colors group/link"
|
|
258
|
+
>
|
|
259
|
+
<span className="truncate font-mono">{name}</span>
|
|
260
|
+
<ExternalLink size={12} className="opacity-60 group-hover/link:opacity-100 shrink-0" />
|
|
261
|
+
</a>
|
|
262
|
+
))}
|
|
263
|
+
</div>
|
|
264
|
+
</details>
|
|
265
|
+
</div>
|
|
266
|
+
|
|
267
|
+
{/* 抠图模型库 */}
|
|
268
|
+
<div className="space-y-3">
|
|
269
|
+
<div className="flex items-center justify-between mt-3">
|
|
270
|
+
<div className="flex items-center gap-2">
|
|
271
|
+
<Layers size={16} className="text-blue-400" />
|
|
272
|
+
<h4 className="text-xs font-bold text-white uppercase tracking-wider">
|
|
273
|
+
{text('视觉抠图模型库', 'Vision Matting Models')}
|
|
274
|
+
</h4>
|
|
275
|
+
</div>
|
|
276
|
+
<span className="text-[11px] text-slate-400">
|
|
277
|
+
{text('在线下载并验证 ONNX 模型', 'Download and verify ONNX models')}
|
|
278
|
+
</span>
|
|
279
|
+
</div>
|
|
280
|
+
|
|
281
|
+
<div className="cf-removal-models grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
282
|
+
{MODELS.map(model => {
|
|
283
|
+
const stored = state.models[model.id];
|
|
284
|
+
const available = !!stored;
|
|
285
|
+
const isActive = state.settings.modelId === model.id;
|
|
286
|
+
|
|
287
|
+
return (
|
|
288
|
+
<article
|
|
289
|
+
key={model.id}
|
|
290
|
+
className={`cf-removal-model !m-0 p-5 rounded-2xl border transition-all relative flex flex-col justify-between ${
|
|
291
|
+
isActive
|
|
292
|
+
? 'is-active bg-gradient-to-b from-blue-950/25 to-slate-900/70 border-blue-500/50 shadow-lg shadow-blue-950/20'
|
|
293
|
+
: 'bg-slate-900/40 border-slate-800/80 hover:border-slate-700/80'
|
|
294
|
+
}`}
|
|
295
|
+
>
|
|
296
|
+
<div>
|
|
297
|
+
<div className="flex items-center justify-between gap-2 mb-2">
|
|
298
|
+
<div className="cf-removal-model-title flex items-center gap-2">
|
|
299
|
+
<strong className="text-sm font-bold text-white">{model.name}</strong>
|
|
300
|
+
<span className="text-[10px] px-2 py-0.5 rounded-full font-medium bg-slate-800 text-slate-300 border border-slate-700/60">
|
|
301
|
+
{model.id === 'modnet' ? text('人像专用', 'Portraits') : model.id === 'u2netp' ? text('轻量通用', 'Lightweight') : text('本地 512', 'Local 512')}
|
|
302
|
+
</span>
|
|
303
|
+
</div>
|
|
304
|
+
|
|
305
|
+
{isActive && (
|
|
306
|
+
<span className="text-[10px] font-bold px-2 py-0.5 rounded-full bg-blue-500/20 text-blue-400 border border-blue-500/40 flex items-center gap-1 shadow-sm">
|
|
307
|
+
<Check size={11} />
|
|
308
|
+
{text('当前生效', 'Active')}
|
|
309
|
+
</span>
|
|
310
|
+
)}
|
|
311
|
+
</div>
|
|
312
|
+
|
|
313
|
+
<p className="text-xs text-slate-400 leading-relaxed mb-3">
|
|
314
|
+
{model.id === 'modnet'
|
|
315
|
+
? text('MODNet 人像抠图,512 方形输入,直接输出 alpha。固定 CPU/WASM 稳定模式。', 'MODNet portrait matting with 512 square input and direct alpha. CPU/WASM stable mode.')
|
|
316
|
+
: model.id === 'u2netp'
|
|
317
|
+
? text('轻巧极速,适合常规商品、人物与扁平图标。', 'Fast and compact, ideal for products, portraits, and flat graphics.')
|
|
318
|
+
: text('BiRefNet Lite,轻量版的可实现高效抠图,适合普通主体和复杂边缘处理。', 'BiRefNet Lite. Lightweight version for efficient matting, suitable for ordinary subjects and complex edges.')}
|
|
319
|
+
</p>
|
|
320
|
+
|
|
321
|
+
<div className="cf-removal-meta flex flex-wrap gap-2 text-[11px] text-slate-400 mb-3">
|
|
322
|
+
<span className="px-2 py-0.5 rounded bg-slate-950/60 border border-slate-800 font-mono">
|
|
323
|
+
{((stored?.blob.size || model.size) / 1_000_000).toFixed(2)} MB
|
|
324
|
+
</span>
|
|
325
|
+
<span className="px-2 py-0.5 rounded bg-slate-950/60 border border-slate-800 font-mono">
|
|
326
|
+
{model.inputSize} × {model.inputSize} px
|
|
327
|
+
</span>
|
|
328
|
+
<span className={`px-2 py-0.5 rounded font-medium border ${
|
|
329
|
+
stored
|
|
330
|
+
? 'bg-emerald-500/10 text-emerald-400 border-emerald-500/30'
|
|
331
|
+
: 'bg-slate-800 text-slate-400 border-slate-700/50'
|
|
332
|
+
}`}>
|
|
333
|
+
{stored
|
|
334
|
+
? text('已验证', 'Verified')
|
|
335
|
+
: text('未安装', 'Not installed')}
|
|
336
|
+
</span>
|
|
337
|
+
{stored && (
|
|
338
|
+
<span className="px-2 py-0.5 rounded bg-indigo-500/10 text-indigo-300 border border-indigo-500/30 font-medium">
|
|
339
|
+
{!model.forceCpu && stored.provider === 'webgpu' ? 'GPU 加速' : 'CPU WASM'}
|
|
340
|
+
</span>
|
|
341
|
+
)}
|
|
342
|
+
</div>
|
|
343
|
+
|
|
344
|
+
{stored && (
|
|
345
|
+
<div className="cf-removal-filename text-[11px] text-slate-400 bg-slate-950/60 border border-slate-800/80 rounded-lg p-2 font-mono truncate mb-3" title={stored.name}>
|
|
346
|
+
<span className="text-slate-500 mr-1.5">{text('文件:', 'File:')}</span>
|
|
347
|
+
{stored.name}
|
|
348
|
+
</div>
|
|
349
|
+
)}
|
|
350
|
+
</div>
|
|
351
|
+
|
|
352
|
+
<div className="pt-3 border-t border-slate-800/80">
|
|
353
|
+
<div className="cf-removal-actions flex items-center gap-2 flex-wrap">
|
|
354
|
+
{!model.variants && (
|
|
355
|
+
<button type="button" disabled={busy}
|
|
356
|
+
onClick={() => void perform(async (signal, report) => {
|
|
357
|
+
const blob = await downloadModel(model.id, signal, report);
|
|
358
|
+
await acceptModel(model.id, blob, `${model.id}.onnx`, signal, report);
|
|
359
|
+
})}
|
|
360
|
+
className="px-3 py-1.5 rounded-lg text-xs font-bold bg-blue-600 hover:bg-blue-500 text-white flex items-center gap-1.5 shadow-sm transition-all cursor-pointer">
|
|
361
|
+
<Download size={13} /> {text('下载模型', 'Download model')}
|
|
362
|
+
</button>
|
|
363
|
+
)}
|
|
364
|
+
{model.variants?.map(variant => (
|
|
365
|
+
<button
|
|
366
|
+
key={variant.precision}
|
|
367
|
+
type="button"
|
|
368
|
+
disabled={busy}
|
|
369
|
+
onClick={() => void perform(async (signal, report) => {
|
|
370
|
+
const blob = await downloadModel(model.id, signal, report, variant.precision);
|
|
371
|
+
const name = new URL(variant.url).pathname.split('/').at(-1) || `${model.id}_${variant.precision}.onnx`;
|
|
372
|
+
await acceptModel(model.id, blob, name, signal, report);
|
|
373
|
+
})}
|
|
374
|
+
className="px-3 py-1.5 rounded-lg text-xs font-bold bg-blue-600 hover:bg-blue-500 text-white flex items-center gap-1.5 shadow-sm transition-all cursor-pointer"
|
|
375
|
+
>
|
|
376
|
+
<Download size={13} />
|
|
377
|
+
<span>{text('下载', 'Download')} {variant.precision.toUpperCase()} · {(variant.size / 1_000_000).toFixed(1)} MB</span>
|
|
378
|
+
</button>
|
|
379
|
+
))}
|
|
380
|
+
|
|
381
|
+
{available && (
|
|
382
|
+
<button
|
|
383
|
+
type="button"
|
|
384
|
+
disabled={busy}
|
|
385
|
+
onClick={() => void perform(async (signal, report) => {
|
|
386
|
+
const resources = await loadResources(model.id, signal);
|
|
387
|
+
const provider = await verifyModel(model.id, resources.model, state.settings.device, { signal, onProgress: report });
|
|
388
|
+
report({ stage: provider });
|
|
389
|
+
})}
|
|
390
|
+
className="cf-removal-secondary-btn px-3 py-1.5 rounded-lg text-xs font-medium bg-slate-800 hover:bg-slate-700 text-slate-200 border border-slate-700/80 flex items-center gap-1.5 transition-colors cursor-pointer"
|
|
391
|
+
>
|
|
392
|
+
<CheckCircle2 size={13} />
|
|
393
|
+
<span>{text('验证推理', 'Verify')}</span>
|
|
394
|
+
</button>
|
|
395
|
+
)}
|
|
396
|
+
|
|
397
|
+
{stored && (
|
|
398
|
+
<button
|
|
399
|
+
type="button"
|
|
400
|
+
disabled={busy}
|
|
401
|
+
aria-label={text(`删除 ${model.name} 模型`, `Delete ${model.name} model`)}
|
|
402
|
+
title={text('删除模型', 'Delete model')}
|
|
403
|
+
onClick={() => void perform(async () => {
|
|
404
|
+
if (state.settings.modelId === model.id) await saveSettings({ ...state.settings, modelId: 'u2netp' });
|
|
405
|
+
await deleteModel(model.id);
|
|
406
|
+
})}
|
|
407
|
+
className="p-1.5 rounded-lg bg-rose-500/10 hover:bg-rose-500/20 text-rose-400 border border-rose-500/30 transition-colors cursor-pointer"
|
|
408
|
+
>
|
|
409
|
+
<Trash2 size={13} />
|
|
410
|
+
</button>
|
|
411
|
+
)}
|
|
412
|
+
|
|
413
|
+
<a
|
|
414
|
+
href={model.sourceUrl ?? model.url}
|
|
415
|
+
target="_blank"
|
|
416
|
+
rel="noreferrer"
|
|
417
|
+
title={text('查看源权重', 'Open source model page')}
|
|
418
|
+
className="cf-removal-secondary-btn p-1.5 rounded-lg bg-slate-800 hover:bg-slate-700 text-slate-400 hover:text-white border border-slate-700/80 transition-colors"
|
|
419
|
+
>
|
|
420
|
+
<ExternalLink size={13} />
|
|
421
|
+
</a>
|
|
422
|
+
|
|
423
|
+
{!isActive && available && (
|
|
424
|
+
<button
|
|
425
|
+
type="button"
|
|
426
|
+
disabled={busy}
|
|
427
|
+
onClick={() => void update({ modelId: model.id })}
|
|
428
|
+
className="ml-auto text-xs text-blue-400 hover:text-blue-300 font-semibold cursor-pointer underline-offset-4 hover:underline"
|
|
429
|
+
>
|
|
430
|
+
{text('设为默认', 'Set as Active')}
|
|
431
|
+
</button>
|
|
432
|
+
)}
|
|
433
|
+
</div>
|
|
434
|
+
</div>
|
|
435
|
+
</article>
|
|
436
|
+
);
|
|
437
|
+
})}
|
|
438
|
+
</div>
|
|
439
|
+
</div>
|
|
440
|
+
|
|
441
|
+
{/* 隐藏的本地文件输入 */}
|
|
442
|
+
<input
|
|
443
|
+
ref={runtimeInput}
|
|
444
|
+
type="file"
|
|
445
|
+
accept=".mjs,.wasm"
|
|
446
|
+
multiple
|
|
447
|
+
hidden
|
|
448
|
+
onChange={event => {
|
|
449
|
+
const files = Array.from(event.target.files || []);
|
|
450
|
+
event.target.value = '';
|
|
451
|
+
if (files.length) void perform(() => importRuntime(files));
|
|
452
|
+
}}
|
|
453
|
+
/>
|
|
454
|
+
|
|
455
|
+
{/* 忙碌状态浮层 / 进度卡片 */}
|
|
456
|
+
{busy && (
|
|
457
|
+
<div role="status" className="cf-removal-status p-4 rounded-xl bg-blue-950/40 border border-blue-500/30 shadow-lg flex flex-col gap-2.5">
|
|
458
|
+
<div className="flex items-center justify-between gap-3 w-full">
|
|
459
|
+
<div className="flex items-center gap-2 min-w-0">
|
|
460
|
+
<LoaderCircle size={15} className="animate-spin text-blue-400 shrink-0" />
|
|
461
|
+
<span className="text-xs font-semibold text-blue-200 truncate">
|
|
462
|
+
{progressText(progress, english) || text('正在处理中...', 'Processing...')}
|
|
463
|
+
</span>
|
|
464
|
+
</div>
|
|
465
|
+
<button
|
|
466
|
+
type="button"
|
|
467
|
+
onClick={() => controller.current?.abort()}
|
|
468
|
+
title={text('取消', 'Cancel')}
|
|
469
|
+
aria-label={text('取消', 'Cancel')}
|
|
470
|
+
className="cf-removal-secondary-btn p-1.5 rounded-lg bg-slate-800 hover:bg-slate-700 text-slate-400 hover:text-white transition-colors cursor-pointer shrink-0"
|
|
471
|
+
>
|
|
472
|
+
<X size={14} />
|
|
473
|
+
</button>
|
|
474
|
+
</div>
|
|
475
|
+
|
|
476
|
+
<div className="w-full bg-slate-900 rounded-full h-1.5 overflow-hidden">
|
|
477
|
+
<div
|
|
478
|
+
className={`h-full bg-gradient-to-r from-blue-500 to-indigo-500 rounded-full transition-all duration-300 ${progressPercent === undefined ? 'w-full animate-pulse' : ''}`}
|
|
479
|
+
style={{ width: progressPercent !== undefined ? `${progressPercent}%` : '100%' }}
|
|
480
|
+
/>
|
|
481
|
+
</div>
|
|
482
|
+
</div>
|
|
483
|
+
)}
|
|
484
|
+
|
|
485
|
+
{/* 错误提示框 */}
|
|
486
|
+
{(error || state.error) && (
|
|
487
|
+
<div role="alert" className="cf-removal-error p-3.5 rounded-xl bg-rose-500/10 border border-rose-500/30 text-rose-300 text-xs font-medium flex items-center gap-2 animate-in fade-in">
|
|
488
|
+
<AlertCircle size={16} className="text-rose-400 shrink-0" />
|
|
489
|
+
<span className="break-all">{error || state.error}</span>
|
|
490
|
+
</div>
|
|
491
|
+
)}
|
|
492
|
+
|
|
493
|
+
{/* 成功状态提示 */}
|
|
494
|
+
{!busy && success && (
|
|
495
|
+
<div role="status" className="cf-removal-ready p-3.5 rounded-xl bg-emerald-500/10 border border-emerald-500/30 text-emerald-300 text-xs font-medium flex items-center gap-2 animate-in fade-in">
|
|
496
|
+
<CheckCircle2 size={16} className="text-emerald-400 shrink-0" />
|
|
497
|
+
<span>{success}</span>
|
|
498
|
+
</div>
|
|
499
|
+
)}
|
|
500
|
+
</section>
|
|
501
|
+
);
|
|
502
|
+
}
|