-
Notifications
You must be signed in to change notification settings - Fork 16
Add optional moeflow-companion service #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
a4bd396
4fcf8a4
e399d85
f6f82ff
8cdce06
0a36d0c
507fa21
31930fe
d4c89d9
492b055
74e4002
d0e7ab9
4517128
d1f8f83
cfa7415
f6285fa
219c384
c8279e3
b673879
88b2938
6744e82
d6fa56a
0ddc876
a8c6700
5eb7668
4538da3
83ac745
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,3 +28,4 @@ yarn-error.log* | |
| .vscode/* | ||
| .idea | ||
| stats.html | ||
| /public/moeflow-runtime-config.json | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "comment": "Runtime configuration overrides. See src/configs.tsx", | ||
| "moeflowCompanion": { | ||
| "gradioUrl": "http://localhost:7860" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { useState, useRef } from 'react'; | ||
| import { Client } from '@gradio/client'; | ||
| import { useAsyncEffect } from '@jokester/ts-commonutil/lib/react/hook/use-async-effect'; | ||
| import { useSelector } from 'react-redux'; | ||
| import { AppState } from '@/store'; | ||
| import { createDebugLogger } from '@/utils/debug-logger'; | ||
|
|
||
| export const moeflowCompanionServiceState = { | ||
| disabled: 'disabled', | ||
| connecting: 'connecting', | ||
| connected: 'connected', | ||
| disconnected: 'disconnected', | ||
| } as const; | ||
|
|
||
|
Comment on lines
+9
to
+15
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Strengthen typing for state and public API Use a literal union for service state and annotate hook return for consumers. export const moeflowCompanionServiceState = {
disabled: 'disabled',
connecting: 'connecting',
connected: 'connected',
disconnected: 'disconnected',
} as const;
+export type MoeflowCompanionServiceState =
+ (typeof moeflowCompanionServiceState)[keyof typeof moeflowCompanionServiceState];
+
const logger = createDebugLogger('service:moeflow_companion');
-export function useMoeflowCompanion() {
+export function useMoeflowCompanion(): readonly [
+ MoeflowCompanionServiceState,
+ Client | null
+] {
const clientRef = useRef<Client | null>(null);
- const [clientState, setClientState] = useState<string>(
+ const [clientState, setClientState] = useState<MoeflowCompanionServiceState>(
moeflowCompanionServiceState.disabled,
);
@@
- return [clientState, clientRef.current] as const;
+ return [clientState, clientRef.current] as const;Also applies to: 17-21, 46-46 🤖 Prompt for AI Agents |
||
| const logger = createDebugLogger('service:moeflow_companion'); | ||
|
|
||
| export function useMoeflowCompanion() { | ||
| const clientRef = useRef<Client | null>(null); | ||
| const [clientState, setClientState] = useState<string>( | ||
| moeflowCompanionServiceState.disabled, | ||
| ); | ||
| const serviceConf = useSelector( | ||
| (s: AppState) => s.site.runtimeConfig.moeflowCompanion, | ||
| ); | ||
|
Comment on lines
+32
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Prevent possible runtime crash when runtimeConfig is undefined; depend only on gradioUrl Selector currently assumes Apply: - const serviceConf = useSelector(
- (s: AppState) => s.site.runtimeConfig.moeflowCompanion,
- );
+ const gradioUrl = useSelector(
+ (s: AppState) => s.site.runtimeConfig?.moeflowCompanion?.gradioUrl,
+ );
@@
- if (!serviceConf?.gradioUrl) {
+ if (!gradioUrl) {
clientRef.current = null;
setClientState(moeflowCompanionServiceState.disabled);
return;
}
try {
- const client = await Client.connect(serviceConf.gradioUrl);
+ const client = await Client.connect(gradioUrl);
clientRef.current = client;
setClientState(moeflowCompanionServiceState.connected);
released.then(() => client.close());
} catch (e) {
- logger('error connecting', e, serviceConf.gradioUrl);
+ logger('error connecting', e, gradioUrl);
clientRef.current = null;
setClientState(moeflowCompanionServiceState.disconnected);
}
@@
- [serviceConf],
+ [gradioUrl],Also applies to: 28-32, 34-41, 44-45 |
||
|
|
||
| useAsyncEffect( | ||
| async (_, released) => { | ||
| if (!serviceConf?.gradioUrl) { | ||
| clientRef.current = null; | ||
| setClientState(moeflowCompanionServiceState.disabled); | ||
| return; | ||
| } | ||
| try { | ||
| const client = await Client.connect(serviceConf.gradioUrl); | ||
| clientRef.current = client; | ||
| setClientState(moeflowCompanionServiceState.connected); | ||
| released.then(() => client.close()); | ||
| } catch (e) { | ||
| logger('error connecting', e, serviceConf.gradioUrl); | ||
| clientRef.current = null; | ||
| setClientState(moeflowCompanionServiceState.disconnected); | ||
| } | ||
| }, | ||
| [serviceConf], | ||
| ); | ||
| return [clientState, clientRef.current] as const; | ||
| } | ||
|
|
||
| export async function x(client: Client) {} | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,7 +16,7 @@ function* setCurrentProjectSetWorker( | |||||||||||||||||||||
| ) { | ||||||||||||||||||||||
| // 清空当前 projectSet | ||||||||||||||||||||||
| yield put(clearCurrentProjectSet()); | ||||||||||||||||||||||
| const projectSets = yield select( | ||||||||||||||||||||||
| const projectSets: UserProjectSet[] = yield select( | ||||||||||||||||||||||
| (state: AppState) => state.projectSet.projectSets, | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| const projectSet = projectSets.find( | ||||||||||||||||||||||
|
|
@@ -35,7 +35,7 @@ function* setCurrentProjectSetWorker( | |||||||||||||||||||||
| configs: { cancelToken }, | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| yield put(setCurrentProjectSet(toLowerCamelCase(result.data))); | ||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||
| } catch (error: any) { | ||||||||||||||||||||||
| error.default(); | ||||||||||||||||||||||
|
Comment on lines
+38
to
39
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Don’t widen to any; guard error.default() to prevent crash paths Calling default on unknown errors can throw. Use unknown and guard. - } catch (error: any) {
- error.default();
+ } catch (error: unknown) {
+ if (typeof (error as any)?.default === 'function') {
+ (error as any).default();
+ } else {
+ // TODO: route to centralized error handler
+ console.error(error);
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||
| if (yield cancelled()) { | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.