global status

This commit is contained in:
josc146 2023-05-23 12:50:53 +08:00
parent 9a3657e6ea
commit 3aaf16b38b
5 changed files with 38 additions and 28 deletions

View File

@ -1,4 +1,4 @@
import commonStore, { ModelStatus } from '../stores/commonStore';
import commonStore, { Status } from '../stores/commonStore';
export const readRoot = async () => {
const port = commonStore.getCurrentModelConfig().apiParameters.apiPort;
@ -36,15 +36,15 @@ export const updateConfig = async (body: any) => {
});
};
export const getStatus = async (timeout?: number): Promise<ModelStatus | undefined> => {
export const getStatus = async (timeout?: number): Promise<Status | undefined> => {
const controller = new AbortController();
if (timeout)
setTimeout(() => controller.abort(), timeout);
const port = commonStore.getCurrentModelConfig().apiParameters.apiPort;
let ret: ModelStatus | undefined;
let ret: Status | undefined;
await fetch(`http://127.0.0.1:${port}/status`, { signal: controller.signal }).then(r => r.json()).then(data => {
ret = data.status;
ret = data;
}).catch(() => {
});
return ret;

View File

@ -42,8 +42,8 @@ export const RunButton: FC<{ onClickRun?: MouseEventHandler, iconMode?: boolean
const navigate = useNavigate();
const onClickMainButton = async () => {
if (commonStore.modelStatus === ModelStatus.Offline) {
commonStore.setModelStatus(ModelStatus.Starting);
if (commonStore.status.modelStatus === ModelStatus.Offline) {
commonStore.setStatus({ modelStatus: ModelStatus.Starting });
const modelConfig = commonStore.getCurrentModelConfig();
let modelName = '';
@ -53,7 +53,7 @@ export const RunButton: FC<{ onClickRun?: MouseEventHandler, iconMode?: boolean
modelPath = `./${manifest.localModelDir}/${modelName}`;
} else {
toast(t('Model Config Exception'), { type: 'error' });
commonStore.setModelStatus(ModelStatus.Offline);
commonStore.setStatus({ modelStatus: ModelStatus.Offline });
return;
}
@ -79,7 +79,7 @@ export const RunButton: FC<{ onClickRun?: MouseEventHandler, iconMode?: boolean
}
});
if (depErrorMsg) {
commonStore.setModelStatus(ModelStatus.Offline);
commonStore.setStatus({ modelStatus: ModelStatus.Offline });
return;
}
commonStore.setDepComplete(true);
@ -100,7 +100,7 @@ export const RunButton: FC<{ onClickRun?: MouseEventHandler, iconMode?: boolean
}
});
commonStore.setModelStatus(ModelStatus.Offline);
commonStore.setStatus({ modelStatus: ModelStatus.Offline });
return;
}
@ -118,7 +118,7 @@ export const RunButton: FC<{ onClickRun?: MouseEventHandler, iconMode?: boolean
.then(r => {
if (r.ok && !loading) {
clearInterval(intervalId);
commonStore.setModelStatus(ModelStatus.Loading);
commonStore.setStatus({ modelStatus: ModelStatus.Loading });
loading = true;
toast(t('Loading Model'), { type: 'info' });
updateConfig({
@ -133,51 +133,51 @@ export const RunButton: FC<{ onClickRun?: MouseEventHandler, iconMode?: boolean
strategy: getStrategy(modelConfig)
}).then((r) => {
if (r.ok) {
commonStore.setModelStatus(ModelStatus.Working);
commonStore.setStatus({ modelStatus: ModelStatus.Working });
toastWithButton(t('Startup Completed'), t('Chat'), () => {
navigate({ pathname: '/chat' });
}, { type: 'success', autoClose: 3000 });
} else if (r.status === 304) {
toast(t('Loading Model'), { type: 'info' });
} else {
commonStore.setModelStatus(ModelStatus.Offline);
commonStore.setStatus({ modelStatus: ModelStatus.Offline });
toast(t('Failed to switch model'), { type: 'error' });
}
}).catch(() => {
commonStore.setModelStatus(ModelStatus.Offline);
commonStore.setStatus({ modelStatus: ModelStatus.Offline });
toast(t('Failed to switch model'), { type: 'error' });
});
}
}).catch(() => {
if (timeoutCount <= 0) {
clearInterval(intervalId);
commonStore.setModelStatus(ModelStatus.Offline);
commonStore.setStatus({ modelStatus: ModelStatus.Offline });
}
});
timeoutCount--;
}, 1000);
} else {
commonStore.setModelStatus(ModelStatus.Offline);
commonStore.setStatus({ modelStatus: ModelStatus.Offline });
exit();
}
};
const onClick = async (e: any) => {
if (commonStore.modelStatus === ModelStatus.Offline)
if (commonStore.status.modelStatus === ModelStatus.Offline)
await onClickRun?.(e);
await onClickMainButton();
};
return (iconMode ?
<ToolTipButton disabled={commonStore.modelStatus === ModelStatus.Starting}
icon={iconModeButtonIcon[commonStore.modelStatus]}
desc={t(mainButtonText[commonStore.modelStatus])}
<ToolTipButton disabled={commonStore.status.modelStatus === ModelStatus.Starting}
icon={iconModeButtonIcon[commonStore.status.modelStatus]}
desc={t(mainButtonText[commonStore.status.modelStatus])}
size="small" onClick={onClick} />
:
<Button disabled={commonStore.modelStatus === ModelStatus.Starting} appearance="primary" size="large"
<Button disabled={commonStore.status.modelStatus === ModelStatus.Starting} appearance="primary" size="large"
onClick={onClick}>
{t(mainButtonText[commonStore.modelStatus])}
{t(mainButtonText[commonStore.status.modelStatus])}
</Button>
);
});

View File

@ -95,7 +95,7 @@ const ChatPanel: FC = observer(() => {
e.stopPropagation();
if (e.type === 'click' || (e.keyCode === 13 && !e.shiftKey)) {
e.preventDefault();
if (commonStore.modelStatus === ModelStatus.Offline) {
if (commonStore.status.modelStatus === ModelStatus.Offline) {
toast(t('Please click the button in the top right corner to start the model'), { type: 'warning' });
return;
}
@ -315,8 +315,8 @@ export const Chat: FC = observer(() => {
<div className="flex flex-col gap-1 p-2 h-full overflow-hidden">
<div className="flex justify-between items-center">
<div className="flex items-center gap-2">
<PresenceBadge status={badgeStatus[commonStore.modelStatus]} />
<Text size={100}>{t('Model Status') + ': ' + t(statusText[commonStore.modelStatus])}</Text>
<PresenceBadge status={badgeStatus[commonStore.status.modelStatus]} />
<Text size={100}>{t('Model Status') + ': ' + t(statusText[commonStore.status.modelStatus])}</Text>
</div>
<div className="flex items-center gap-2">
<ConfigSelector size="small" />

View File

@ -21,7 +21,7 @@ export async function startup() {
getStatus(500).then(status => { // depends on config api port
if (status)
commonStore.setModelStatus(status);
commonStore.setStatus(status);
});
}

View File

@ -18,9 +18,19 @@ export enum ModelStatus {
Working,
}
export type Status = {
modelStatus: ModelStatus;
pid: number;
device_name: string;
}
class CommonStore {
// global
modelStatus: ModelStatus = ModelStatus.Offline;
status: Status = {
modelStatus: ModelStatus.Offline,
pid: 0,
device_name: 'CPU'
};
depComplete: boolean = false;
// home
introduction: IntroductionContent = manifest.introduction;
@ -54,8 +64,8 @@ class CommonStore {
return this.modelConfigs[this.currentModelConfigIndex];
};
setModelStatus = (status: ModelStatus) => {
this.modelStatus = status;
setStatus = (status: Partial<Status>) => {
this.status = { ...this.status, ...status };
};
setCurrentConfigIndex = (index: number, saveConfig: boolean = true) => {