mirror of
https://github.com/langgenius/dify.git
synced 2026-04-27 14:08:18 +08:00
move workflow_id to app
This commit is contained in:
@ -64,8 +64,8 @@ class AppService:
|
||||
app_template = default_app_templates[app_mode]
|
||||
|
||||
# get model config
|
||||
default_model_config = app_template['model_config']
|
||||
if 'model' in default_model_config:
|
||||
default_model_config = app_template.get('model_config')
|
||||
if default_model_config and 'model' in default_model_config:
|
||||
# get model provider
|
||||
model_manager = ModelManager()
|
||||
|
||||
@ -110,12 +110,15 @@ class AppService:
|
||||
db.session.add(app)
|
||||
db.session.flush()
|
||||
|
||||
app_model_config = AppModelConfig(**default_model_config)
|
||||
app_model_config.app_id = app.id
|
||||
db.session.add(app_model_config)
|
||||
db.session.flush()
|
||||
if default_model_config:
|
||||
app_model_config = AppModelConfig(**default_model_config)
|
||||
app_model_config.app_id = app.id
|
||||
db.session.add(app_model_config)
|
||||
db.session.flush()
|
||||
|
||||
app.app_model_config_id = app_model_config.id
|
||||
app.app_model_config_id = app_model_config.id
|
||||
|
||||
db.session.commit()
|
||||
|
||||
app_was_created.send(app, account=account)
|
||||
|
||||
@ -135,16 +138,22 @@ class AppService:
|
||||
|
||||
app_data = import_data.get('app')
|
||||
model_config_data = import_data.get('model_config')
|
||||
workflow_graph = import_data.get('workflow_graph')
|
||||
workflow = import_data.get('workflow')
|
||||
|
||||
if not app_data or not model_config_data:
|
||||
raise ValueError("Missing app or model_config in data argument")
|
||||
if not app_data:
|
||||
raise ValueError("Missing app in data argument")
|
||||
|
||||
app_mode = AppMode.value_of(app_data.get('mode'))
|
||||
if app_mode in [AppMode.ADVANCED_CHAT, AppMode.WORKFLOW]:
|
||||
if not workflow_graph:
|
||||
raise ValueError("Missing workflow_graph in data argument "
|
||||
"when mode is advanced-chat or workflow")
|
||||
if not workflow:
|
||||
raise ValueError("Missing workflow in data argument "
|
||||
"when app mode is advanced-chat or workflow")
|
||||
elif app_mode in [AppMode.CHAT, AppMode.AGENT_CHAT]:
|
||||
if not model_config_data:
|
||||
raise ValueError("Missing model_config in data argument "
|
||||
"when app mode is chat or agent-chat")
|
||||
else:
|
||||
raise ValueError("Invalid app mode")
|
||||
|
||||
app = App(
|
||||
tenant_id=tenant_id,
|
||||
@ -161,26 +170,32 @@ class AppService:
|
||||
db.session.add(app)
|
||||
db.session.commit()
|
||||
|
||||
if workflow_graph:
|
||||
# init draft workflow
|
||||
workflow_service = WorkflowService()
|
||||
workflow_service.sync_draft_workflow(app, workflow_graph, account)
|
||||
|
||||
app_model_config = AppModelConfig()
|
||||
app_model_config = app_model_config.from_model_config_dict(model_config_data)
|
||||
app_model_config.app_id = app.id
|
||||
|
||||
db.session.add(app_model_config)
|
||||
db.session.commit()
|
||||
|
||||
app.app_model_config_id = app_model_config.id
|
||||
|
||||
app_was_created.send(app, account=account)
|
||||
|
||||
app_model_config_was_updated.send(
|
||||
app,
|
||||
app_model_config=app_model_config
|
||||
)
|
||||
if workflow:
|
||||
# init draft workflow
|
||||
workflow_service = WorkflowService()
|
||||
workflow_service.sync_draft_workflow(
|
||||
app_model=app,
|
||||
graph=workflow.get('graph'),
|
||||
features=workflow.get('features'),
|
||||
account=account
|
||||
)
|
||||
|
||||
if model_config_data:
|
||||
app_model_config = AppModelConfig()
|
||||
app_model_config = app_model_config.from_model_config_dict(model_config_data)
|
||||
app_model_config.app_id = app.id
|
||||
|
||||
db.session.add(app_model_config)
|
||||
db.session.commit()
|
||||
|
||||
app.app_model_config_id = app_model_config.id
|
||||
|
||||
app_model_config_was_updated.send(
|
||||
app,
|
||||
app_model_config=app_model_config
|
||||
)
|
||||
|
||||
return app
|
||||
|
||||
@ -190,7 +205,7 @@ class AppService:
|
||||
:param app: App instance
|
||||
:return:
|
||||
"""
|
||||
app_model_config = app.app_model_config
|
||||
app_mode = AppMode.value_of(app.mode)
|
||||
|
||||
export_data = {
|
||||
"app": {
|
||||
@ -198,16 +213,27 @@ class AppService:
|
||||
"mode": app.mode,
|
||||
"icon": app.icon,
|
||||
"icon_background": app.icon_background
|
||||
},
|
||||
"model_config": app_model_config.to_dict(),
|
||||
}
|
||||
}
|
||||
|
||||
if app_model_config.workflow_id:
|
||||
export_data['workflow_graph'] = json.loads(app_model_config.workflow.graph)
|
||||
if app_mode in [AppMode.ADVANCED_CHAT, AppMode.WORKFLOW]:
|
||||
if app.workflow_id:
|
||||
workflow = app.workflow
|
||||
export_data['workflow'] = {
|
||||
"graph": workflow.graph_dict,
|
||||
"features": workflow.features_dict
|
||||
}
|
||||
else:
|
||||
workflow_service = WorkflowService()
|
||||
workflow = workflow_service.get_draft_workflow(app)
|
||||
export_data['workflow'] = {
|
||||
"graph": workflow.graph_dict,
|
||||
"features": workflow.features_dict
|
||||
}
|
||||
else:
|
||||
workflow_service = WorkflowService()
|
||||
workflow = workflow_service.get_draft_workflow(app)
|
||||
export_data['workflow_graph'] = json.loads(workflow.graph)
|
||||
app_model_config = app.app_model_config
|
||||
|
||||
export_data['model_config'] = app_model_config.to_dict()
|
||||
|
||||
return yaml.dump(export_data)
|
||||
|
||||
|
||||
@ -44,13 +44,10 @@ class WorkflowConverter:
|
||||
:param account: Account
|
||||
:return: new App instance
|
||||
"""
|
||||
# get original app config
|
||||
app_model_config = app_model.app_model_config
|
||||
|
||||
# convert app model config
|
||||
workflow = self.convert_app_model_config_to_workflow(
|
||||
app_model=app_model,
|
||||
app_model_config=app_model_config,
|
||||
app_model_config=app_model.app_model_config,
|
||||
account_id=account.id
|
||||
)
|
||||
|
||||
@ -58,8 +55,9 @@ class WorkflowConverter:
|
||||
new_app = App()
|
||||
new_app.tenant_id = app_model.tenant_id
|
||||
new_app.name = app_model.name + '(workflow)'
|
||||
new_app.mode = AppMode.CHAT.value \
|
||||
new_app.mode = AppMode.ADVANCED_CHAT.value \
|
||||
if app_model.mode == AppMode.CHAT.value else AppMode.WORKFLOW.value
|
||||
new_app.workflow_id = workflow.id
|
||||
new_app.icon = app_model.icon
|
||||
new_app.icon_background = app_model.icon_background
|
||||
new_app.enable_site = app_model.enable_site
|
||||
@ -69,28 +67,6 @@ class WorkflowConverter:
|
||||
new_app.is_demo = False
|
||||
new_app.is_public = app_model.is_public
|
||||
db.session.add(new_app)
|
||||
db.session.flush()
|
||||
|
||||
# create new app model config record
|
||||
new_app_model_config = app_model_config.copy()
|
||||
new_app_model_config.id = None
|
||||
new_app_model_config.app_id = new_app.id
|
||||
new_app_model_config.external_data_tools = ''
|
||||
new_app_model_config.model = ''
|
||||
new_app_model_config.user_input_form = ''
|
||||
new_app_model_config.dataset_query_variable = None
|
||||
new_app_model_config.pre_prompt = None
|
||||
new_app_model_config.agent_mode = ''
|
||||
new_app_model_config.prompt_type = 'simple'
|
||||
new_app_model_config.chat_prompt_config = ''
|
||||
new_app_model_config.completion_prompt_config = ''
|
||||
new_app_model_config.dataset_configs = ''
|
||||
new_app_model_config.workflow_id = workflow.id
|
||||
|
||||
db.session.add(new_app_model_config)
|
||||
db.session.flush()
|
||||
|
||||
new_app.app_model_config_id = new_app_model_config.id
|
||||
db.session.commit()
|
||||
|
||||
app_was_created.send(new_app, account=account)
|
||||
@ -110,11 +86,13 @@ class WorkflowConverter:
|
||||
# get new app mode
|
||||
new_app_mode = self._get_new_app_mode(app_model)
|
||||
|
||||
app_model_config_dict = app_model_config.to_dict()
|
||||
|
||||
# convert app model config
|
||||
application_manager = AppManager()
|
||||
app_orchestration_config_entity = application_manager.convert_from_app_model_config_dict(
|
||||
tenant_id=app_model.tenant_id,
|
||||
app_model_config_dict=app_model_config.to_dict(),
|
||||
app_model_config_dict=app_model_config_dict,
|
||||
skip_check=True
|
||||
)
|
||||
|
||||
@ -177,6 +155,25 @@ class WorkflowConverter:
|
||||
|
||||
graph = self._append_node(graph, end_node)
|
||||
|
||||
# features
|
||||
if new_app_mode == AppMode.ADVANCED_CHAT:
|
||||
features = {
|
||||
"opening_statement": app_model_config_dict.get("opening_statement"),
|
||||
"suggested_questions": app_model_config_dict.get("suggested_questions"),
|
||||
"suggested_questions_after_answer": app_model_config_dict.get("suggested_questions_after_answer"),
|
||||
"speech_to_text": app_model_config_dict.get("speech_to_text"),
|
||||
"text_to_speech": app_model_config_dict.get("text_to_speech"),
|
||||
"file_upload": app_model_config_dict.get("file_upload"),
|
||||
"sensitive_word_avoidance": app_model_config_dict.get("sensitive_word_avoidance"),
|
||||
"retriever_resource": app_model_config_dict.get("retriever_resource"),
|
||||
}
|
||||
else:
|
||||
features = {
|
||||
"text_to_speech": app_model_config_dict.get("text_to_speech"),
|
||||
"file_upload": app_model_config_dict.get("file_upload"),
|
||||
"sensitive_word_avoidance": app_model_config_dict.get("sensitive_word_avoidance"),
|
||||
}
|
||||
|
||||
# create workflow record
|
||||
workflow = Workflow(
|
||||
tenant_id=app_model.tenant_id,
|
||||
@ -184,6 +181,7 @@ class WorkflowConverter:
|
||||
type=WorkflowType.from_app_mode(new_app_mode).value,
|
||||
version='draft',
|
||||
graph=json.dumps(graph),
|
||||
features=json.dumps(features),
|
||||
created_by=account_id,
|
||||
created_at=app_model_config.created_at
|
||||
)
|
||||
|
||||
@ -33,29 +33,31 @@ class WorkflowService:
|
||||
"""
|
||||
Get published workflow
|
||||
"""
|
||||
app_model_config = app_model.app_model_config
|
||||
|
||||
if not app_model_config.workflow_id:
|
||||
if not app_model.workflow_id:
|
||||
return None
|
||||
|
||||
# fetch published workflow by workflow_id
|
||||
workflow = db.session.query(Workflow).filter(
|
||||
Workflow.tenant_id == app_model.tenant_id,
|
||||
Workflow.app_id == app_model.id,
|
||||
Workflow.id == app_model_config.workflow_id
|
||||
Workflow.id == app_model.workflow_id
|
||||
).first()
|
||||
|
||||
# return published workflow
|
||||
return workflow
|
||||
|
||||
|
||||
def sync_draft_workflow(self, app_model: App, graph: dict, account: Account) -> Workflow:
|
||||
def sync_draft_workflow(self, app_model: App,
|
||||
graph: dict,
|
||||
features: dict,
|
||||
account: Account) -> Workflow:
|
||||
"""
|
||||
Sync draft workflow
|
||||
"""
|
||||
# fetch draft workflow by app_model
|
||||
workflow = self.get_draft_workflow(app_model=app_model)
|
||||
|
||||
# TODO validate features
|
||||
|
||||
# create draft workflow if not found
|
||||
if not workflow:
|
||||
workflow = Workflow(
|
||||
@ -64,12 +66,14 @@ class WorkflowService:
|
||||
type=WorkflowType.from_app_mode(app_model.mode).value,
|
||||
version='draft',
|
||||
graph=json.dumps(graph),
|
||||
features=json.dumps(features),
|
||||
created_by=account.id
|
||||
)
|
||||
db.session.add(workflow)
|
||||
# update draft workflow if found
|
||||
else:
|
||||
workflow.graph = json.dumps(graph)
|
||||
workflow.features = json.dumps(features)
|
||||
workflow.updated_by = account.id
|
||||
workflow.updated_at = datetime.utcnow()
|
||||
|
||||
@ -112,28 +116,7 @@ class WorkflowService:
|
||||
db.session.add(workflow)
|
||||
db.session.commit()
|
||||
|
||||
app_model_config = app_model.app_model_config
|
||||
|
||||
# create new app model config record
|
||||
new_app_model_config = app_model_config.copy()
|
||||
new_app_model_config.id = None
|
||||
new_app_model_config.app_id = app_model.id
|
||||
new_app_model_config.external_data_tools = ''
|
||||
new_app_model_config.model = ''
|
||||
new_app_model_config.user_input_form = ''
|
||||
new_app_model_config.dataset_query_variable = None
|
||||
new_app_model_config.pre_prompt = None
|
||||
new_app_model_config.agent_mode = ''
|
||||
new_app_model_config.prompt_type = 'simple'
|
||||
new_app_model_config.chat_prompt_config = ''
|
||||
new_app_model_config.completion_prompt_config = ''
|
||||
new_app_model_config.dataset_configs = ''
|
||||
new_app_model_config.workflow_id = workflow.id
|
||||
|
||||
db.session.add(new_app_model_config)
|
||||
db.session.flush()
|
||||
|
||||
app_model.app_model_config_id = new_app_model_config.id
|
||||
app_model.workflow_id = workflow.id
|
||||
db.session.commit()
|
||||
|
||||
# TODO update app related datasets
|
||||
|
||||
Reference in New Issue
Block a user