Skip to content

Commit 0b1d0a5

Browse files
committed
✨ feat: model 统一使用 blob url
1 parent b629c6e commit 0b1d0a5

File tree

14 files changed

+28
-38
lines changed

14 files changed

+28
-38
lines changed

src/features/AgentViewer/ToolBar/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Aperture, Axis3D, Grid3x3, Orbit, Power, SwitchCamera } from 'lucide-re
44
import React from 'react';
55
import { useTranslation } from 'react-i18next';
66

7-
import { Viewer } from '@/features/vrmViewer/viewer';
7+
import { Viewer } from '@/libs/vrmViewer/viewer';
88

99
interface ToolBarProps {
1010
className?: string;

src/features/AgentViewer/index.tsx

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import PageLoading from '@/components/PageLoading';
77
import { useLoadModel } from '@/hooks/useLoadModel';
88
import { useGlobalStore } from '@/store/global';
99
import { Agent } from '@/types/agent';
10-
import { getModelPathByAgentId } from '@/utils/file';
11-
import storage from '@/utils/storage';
1210

1311
import ToolBar from './ToolBar';
1412
import { useStyles } from './style';
@@ -28,24 +26,14 @@ function AgentViewer(props: Props) {
2826
const viewer = useGlobalStore((s) => s.viewer);
2927
const { t } = useTranslation('features');
3028

31-
const { downloading, percent, fetchModelBlob } = useLoadModel();
29+
const { downloading, percent, fetchModelUrl } = useLoadModel();
3230

3331
const canvasRef = useCallback(
3432
(canvas: HTMLCanvasElement) => {
3533
if (canvas) {
3634
viewer.setup(canvas);
37-
const modelPath = getModelPathByAgentId(agent.agentId);
38-
storage.getItem(modelPath).then((blob) => {
39-
if (!blob) {
40-
viewer.unloadVRM();
41-
fetchModelBlob(agent.agentId, agent.meta.model!).then((res) => {
42-
if (res) {
43-
const modelUrl = URL.createObjectURL(res);
44-
viewer.loadVrm(modelUrl);
45-
}
46-
});
47-
} else {
48-
const modelUrl = URL.createObjectURL(blob as Blob);
35+
fetchModelUrl(agent.agentId, agent.meta.model!).then((modelUrl) => {
36+
if (modelUrl) {
4937
viewer.loadVrm(modelUrl);
5038
}
5139
});

src/features/messages/speakCharacter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
import { Viewer } from '@/libs/vrmViewer/viewer';
12
import { speechApi } from '@/services/tts';
23
import { Screenplay } from '@/types/touch';
34
import { wait } from '@/utils/wait';
45

5-
import { Viewer } from '../vrmViewer/viewer';
6-
76
const createSpeakCharacter = () => {
87
let lastTime = 0;
98
let prevFetchPromise: Promise<unknown> = Promise.resolve();

src/hooks/useLoadModel.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,34 @@ export const useLoadModel = () => {
88
const [downloading, setDownloading] = useState(false);
99
const [percent, setPercent] = useState(0);
1010

11-
const fetchModelBlob = async (agentId: string, modelUrl: string) => {
12-
setDownloading(true);
13-
setPercent(0);
14-
try {
15-
const blob = await fetchWithProgress(modelUrl, {
16-
onProgress: (loaded, total) => {
17-
setPercent(Math.ceil((loaded / total) * 100));
18-
},
19-
});
20-
const modelPath = getModelPathByAgentId(agentId);
21-
await storage.setItem(modelPath, blob);
11+
const fetchModelUrl = async (agentId: string, remoteModelUrl: string) => {
12+
const localModelPath = getModelPathByAgentId(agentId);
13+
let modelBlob = await storage.getItem(localModelPath);
2214

23-
return blob;
15+
try {
16+
if (!modelBlob) {
17+
setDownloading(true);
18+
setPercent(0);
19+
const blob = await fetchWithProgress(remoteModelUrl, {
20+
onProgress: (loaded, total) => {
21+
setPercent(Math.ceil((loaded / total) * 100));
22+
},
23+
});
24+
const modelPath = getModelPathByAgentId(agentId);
25+
await storage.setItem(modelPath, blob);
26+
}
2427
} catch (e) {
2528
console.error(e);
2629
return null;
2730
} finally {
2831
setDownloading(false);
2932
}
33+
return URL.createObjectURL(modelBlob);
3034
};
3135

3236
return {
3337
downloading,
3438
percent,
35-
fetchModelBlob,
39+
fetchModelUrl,
3640
};
3741
};
File renamed without changes.

0 commit comments

Comments
 (0)