Skip to content
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

Access myshell widgets #249

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion web/apps/web/src/app/app/detail/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ const FlowEngineWrapper = observer(
// 退出页面初始化状态
useEffect(() => {
appBuilder.getFlowList({ type: 'workflow' });
appBuilder.getLocalWidgetList();
appBuilder.getMyShellWidgetList();
return () => {
resetState();
};
Expand Down Expand Up @@ -103,7 +105,7 @@ const FlowEngineWrapper = observer(
ref={flowRef}
nodeTypes={nodeTypes}
edgeTypes={edgeTypes}
materialList={materialList}
materialList={appBuilder.materialList}
footerExtra={<ListFooterExtra />}
header={
<div onDoubleClick={e => e.stopPropagation()}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,28 @@ const StandardWidgetConfig: React.FC<CommonWidgetConfigProps> = ({
);

useEffect(() => {
if (values?.widget_class_name && !widgetSchema[values.widget_class_name]) {
getWidgetSchema({ widget_name: values.widget_class_name });
if (
values?.widget_class_name &&
values?.widget_name &&
!widgetSchema[[values.widget_class_name, values.widget_name].join('_')]
) {
getWidgetSchema({
widget_name: values.widget_class_name,
myshell_widget_name: values.widget_name,
});
}
}, [values?.widget_class_name, getWidgetSchema, widgetSchema]);
}, [
values?.widget_class_name,
values?.widget_name,
getWidgetSchema,
widgetSchema,
]);

const schema = useMemo(() => {
const schema = getSchemaByWidget({
...widgetSchema[values?.widget_class_name],
...widgetSchema[
[values?.widget_class_name, values?.widget_name].join('_')
],
});
// Special process ImageCanvasWidget
if (values?.widget_class_name === 'ImageCanvasWidget') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { useDrag, useDrop } from 'react-dnd';
import { materialList } from '@/components/app/constants';
import { TaskList } from '@/components/app/task-list';
import { useAppState } from '@/stores/app/use-app-state';
import { useInjection } from 'inversify-react';
import { AppBuilderModel } from '@/stores/app/models/app-builder.model';

const TaskItem = ({
name,
Expand Down Expand Up @@ -122,6 +124,7 @@ const TasksConfig = ({
onChange: (value: Task[]) => void;
draggable?: boolean;
}) => {
const appBuilder = useInjection<AppBuilderModel>('AppBuilderModel');
const btnRef = useRef<HTMLButtonElement>(null);
const [open, setOpen] = useState(false);
const { getValues } = useFormContext();
Expand Down Expand Up @@ -225,7 +228,7 @@ const TasksConfig = ({
onClick={e => e.stopPropagation()}>
<TaskList
className="rounded-xl"
data={materialList.slice(1)}
data={appBuilder.materialList.slice(1)}
loading={false}
onChange={handleSelect}
/>
Expand Down
63 changes: 63 additions & 0 deletions web/apps/web/src/services/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
GetIntroDisplayResponse,
} from './type';
import { APIFetch } from '../base';
import { MaterialListType } from '@shellagent/flow-engine';
import { z } from 'zod';

// 保存
export const saveApp = (params: SaveAppRequest) => {
Expand Down Expand Up @@ -145,3 +147,64 @@ export const checkIp = (params: CheckIpRequest) => {
body: params,
});
};

export const get_local_widget_list_res = z.object({
widget_list: z.array(
z.object({
title: z.string(),
items: z.array(
z.object({
name: z.string(),
children: z.array(
z.object({
name: z.string(),
display_name: z.string(),
}),
),
}),
),
}),
),
});

export type type_get_local_widget_list_res = z.infer<
typeof get_local_widget_list_res
>;

export const getLocalWidgetList: Fetcher<
type_get_local_widget_list_res,
{}
> = params => {
return APIFetch.get('/api/widget/get_local_widget_list', {
params,
});
};

export const get_myshell_widget_list_res = z.object({
widget_list: z.array(
z.object({
title: z.string(),
items: z.array(
z.object({
name: z.string(),
display_name: z.string(),
type: z.string(),
widget_name: z.string(),
}),
),
}),
),
});

export type type_get_myshell_widget_list = z.infer<
typeof get_myshell_widget_list_res
>;

export const getMyShellWidgetList: Fetcher<
type_get_myshell_widget_list,
{}
> = params => {
return APIFetch.get('/api/widget/get_myshell_widget_list', {
params,
});
};
1 change: 1 addition & 0 deletions web/apps/web/src/services/workflow/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type GetWidgetListResponse = {
// /api/workflow/get_widget_schema
export type GetWidgetSchemaRequest = {
widget_name: string; // 后续需要考虑版本
myshell_widget_name: string;
};

export type GetWidgetSchemaResponse = {
Expand Down
153 changes: 142 additions & 11 deletions web/apps/web/src/stores/app/models/app-builder.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { type IFlow, type ReactFlowInstance } from '@shellagent/flow-engine';
import { CustomKey, CustomEventName } from '@shellagent/pro-config';
import {
type IFlow,
type MaterialListType,
type ReactFlowInstance,
NodeTypeEnum,
WidgetItem,
} from '@shellagent/flow-engine';
import { CustomEventName, CustomKey } from '@shellagent/pro-config';
import {
customSnakeCase,
getRefOptions,
Expand All @@ -13,9 +19,15 @@ import {
import { Issue, IssueTypeEnum } from '@shellagent/shared/type';
import type { FieldValues } from '@shellagent/ui';
import { message } from 'antd';
import { injectable, inject } from 'inversify';
import { inject, injectable } from 'inversify';
import { isEmpty } from 'lodash-es';
import { action, makeObservable, observable, runInAction } from 'mobx';
import {
action,
computed,
makeObservable,
observable,
runInAction,
} from 'mobx';

import { defaultFlow } from '@/components/app/constants';
import { ComfyUIModel } from '@/components/app/plugins/comfyui/comfyui.model';
Expand All @@ -26,32 +38,36 @@ import {
import { AppBuilderChatModel } from '@/components/chat/app-builder-chat.model';
import { SettingsModel } from '@/components/settings/settings.model';
import {
type type_get_local_widget_list_res,
type type_get_myshell_widget_list,
checkIp,
exportApp,
fetchAppVersionList,
fetchAutomata,
fetchFlow,
getIntroDisplay,
getLocalWidgetList,
getMyShellWidgetList,
releaseApp,
exportApp,
checkIp,
saveApp,
getIntroDisplay,
} from '@/services/app';
import type {
GetAppFlowRequest,
GetAppVersionListResponse,
GetAutomataRequest,
} from '@/services/app/type';
import { fetchList as fetchFlowList, editItem } from '@/services/home';
import { editItem, fetchList as fetchFlowList } from '@/services/home';
import type { GetListRequest, GetListResponse } from '@/services/home/type';
import emitter, { EventType } from '@/stores/app/models/emitter';
import {
onDownload,
checkDependency,
LackDependency,
onDownload,
} from '@/stores/app/utils/automata-export';
import {
genNodeData,
genAutomata,
formatReactFlow2Flow,
genAutomata,
genNodeData,
} from '@/stores/app/utils/data-transformer';
import type { Config, Metadata, NodeDataType } from '@/types/app/types';
import { ToastModel } from '@/utils/toast.model';
Expand All @@ -73,6 +89,23 @@ import {

const settingsDisabled = process.env.NEXT_PUBLIC_DISABLE_SETTING === 'yes';

const LocalWigets: MaterialListType = [
{
plain: true,
items: [
{
name: 'State',
display_name: 'State',
type: NodeTypeEnum.state,
},
{
name: 'Note',
display_name: 'Note',
type: NodeTypeEnum.note,
},
],
},
];
@injectable()
export class AppBuilderModel {
nodeData: NodeDataType = {};
Expand Down Expand Up @@ -112,6 +145,76 @@ export class AppBuilderModel {
@observable openExportDialog = false;
@observable openValidationDialog = false;
@observable validateAlert: Issue[] = [];
@observable rawLocalWidgetList: type_get_local_widget_list_res = {
widget_list: [],
};

@observable rawMyShellWidgetList: type_get_myshell_widget_list = {
widget_list: [],
};

@computed get materialList() {
const backendLocalWidgets: MaterialListType =
this.rawLocalWidgetList.widget_list.map(m => {
return {
...m,
plain: true,
items: m.items.reduce<MaterialListType[number]['items']>((acc, i) => {
acc = acc.concat(
i.children.map(c => {
if (c.name === 'ComfyUIWidget') {
return {
...c,
type: NodeTypeEnum.widget,
undraggable: true,
custom: true,
};
}
return {
...c,
type: NodeTypeEnum.widget,
undraggable: true,
};
}),
);
return acc;
}, []),
};
});
const myshellWidgets: MaterialListType =
this.rawMyShellWidgetList.widget_list.map(m => {
return {
...m,
items: m.items.map(i => {
return {
...i,
undraggable: true,
} as WidgetItem;
}),
plain: true,
};
});

const mergedWidgets = [...LocalWigets, ...backendLocalWidgets];

myshellWidgets.forEach(myshellWidget => {
const existingIndex = mergedWidgets.findIndex(
w => w.title === myshellWidget.title,
);
if (existingIndex !== -1) {
mergedWidgets[existingIndex] = {
...mergedWidgets[existingIndex],
items: [
...mergedWidgets[existingIndex].items,
...myshellWidget.items,
],
};
} else {
mergedWidgets.push(myshellWidget);
}
});
return mergedWidgets;
}

copyNodeData: FieldValues = {};

Expand Down Expand Up @@ -687,4 +790,32 @@ export class AppBuilderModel {
this.appEditing = editing;
});
}

@action.bound
async getLocalWidgetList() {
try {
const res = await getLocalWidgetList({});
runInAction(() => {
this.rawLocalWidgetList = res;
});
} catch (e) {
if (e instanceof Error) {
this.emitter.error(e.message);
}
}
}

@action.bound
async getMyShellWidgetList() {
try {
const res = await getMyShellWidgetList({});
runInAction(() => {
this.rawMyShellWidgetList = res;
});
} catch (e) {
if (e instanceof Error) {
this.emitter.error(e.message);
}
}
}
}
2 changes: 1 addition & 1 deletion web/apps/web/src/stores/app/schema-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const SchemaProvider: React.FC<SchemaProviderProps> = ({

useEffect(() => {
if (name && !widgetSchema[name] && type === NodeTypeEnum.widget) {
getWidgetSchema({ widget_name: name });
getWidgetSchema({ widget_name: name, myshell_widget_name: '' });
}
}, [name]);

Expand Down
2 changes: 1 addition & 1 deletion web/apps/web/src/stores/workflow/schema-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const SchemaProvider: React.FC<SchemaProviderProps> = ({
id !== NodeIdEnum.start &&
id !== NodeIdEnum.end
) {
getWidgetSchema({ widget_name: name });
getWidgetSchema({ widget_name: name, myshell_widget_name: '' });
}
}, [name]);

Expand Down
4 changes: 3 additions & 1 deletion web/apps/web/src/stores/workflow/workflow-store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ export const createWorkflowStore = () => {

set(
produce(state => {
state.widgetSchema[params.widget_name] = response;
state.widgetSchema[
[params.widget_name, params.myshell_widget_name].join('_')
] = response;
state.loading.getWidgetSchema[params.widget_name] = false;
}),
);
Expand Down
Loading
Loading