From 63a7155645e717634c7b77ad34661c879360537a Mon Sep 17 00:00:00 2001 From: zxstty Date: Thu, 24 Apr 2025 11:15:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B5=81=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E5=AE=8C=E6=AF=95=E7=8A=B6=E6=80=81=E6=9C=AA=E5=8F=8A=E6=97=B6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=9A=84=E9=97=AE=E9=A2=98;=E5=AE=8C?= =?UTF-8?q?=E5=96=84=E8=8A=82=E7=82=B9&=E6=AD=A5=E9=AA=A4=E7=9A=84?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E5=8F=82=E6=95=B0=E7=9A=84=E5=B1=95=E7=8E=B0?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/manager/flow.py | 4 ++-- apps/routers/chat.py | 4 ++-- apps/scheduler/slot/slot.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/apps/manager/flow.py b/apps/manager/flow.py index 38dc1823..41742a87 100644 --- a/apps/manager/flow.py +++ b/apps/manager/flow.py @@ -90,7 +90,7 @@ class FlowManager: # TODO: 由于现在没有动态表单,所以暂时使用Slot的create_empty_slot方法 parameters = { "input_parameters": Slot(params_schema).create_empty_slot(), - "output_parameters": output_schema, + "output_parameters": Slot(output_schema).extract_type_desc_from_schema(), } except Exception: logger.exception("[FlowManager] generate_from_schema 失败") @@ -267,7 +267,7 @@ class FlowManager: output_parameters = {} parameters = { "input_parameters": input_parameters, - "output_parameters": output_parameters, + "output_parameters": Slot(output_parameters).extract_type_desc_from_schema(), } node_item = NodeItem( stepId=node_id, diff --git a/apps/routers/chat.py b/apps/routers/chat.py index 727c9730..756e9938 100644 --- a/apps/routers/chat.py +++ b/apps/routers/chat.py @@ -103,8 +103,6 @@ async def chat_generator(post_body: RequestData, user_sub: str, session_id: str) # 创建新Record,存入数据库 await save_data(task_id, user_sub, post_body, scheduler.used_docs) - yield "data: [DONE]\n\n" - if post_body.app and post_body.app.flow_id: await FlowManager.update_flow_debug_by_app_and_flow_id( post_body.app.app_id, @@ -112,6 +110,8 @@ async def chat_generator(post_body: RequestData, user_sub: str, session_id: str) debug=True, ) + yield "data: [DONE]\n\n" + except Exception: logger.exception("[Chat] 生成答案失败") yield "data: [ERROR]\n\n" diff --git a/apps/scheduler/slot/slot.py b/apps/scheduler/slot/slot.py index cb230469..46a4f656 100644 --- a/apps/scheduler/slot/slot.py +++ b/apps/scheduler/slot/slot.py @@ -193,6 +193,36 @@ class Slot: return _generate_example(self._schema) + def extract_type_desc_from_schema(self) -> dict[str, str]: + """从JSON Schema中提取类型描述""" + + def _extract_type_desc(schema_node: dict[str, Any]) -> None: + if "type" not in schema_node and "anyOf" not in schema_node: + return None + data = {"type": schema_node.get("type", ""), "description": schema_node.get("description", "")} + if "anyOf" in schema_node: + data["type"] = "anyOf" + # 处理类型为 object 的节点 + if "anyOf" in schema_node: + data["items"] = {} + type_index = 0 + for sub_schema in schema_node["anyOf"]: + sub_result = _extract_type_desc(sub_schema) + type_index += 1 + if sub_result: + data["items"]["type_"+str(type_index)] = sub_result + if schema_node.get("type", "") == "object": + data["items"] = {} + for key, val in schema_node.get("properties", {}).items(): + data["items"][key] = _extract_type_desc(val) + + # 处理类型为 array 的节点 + if schema_node.get("type", "") == "array": + items_schema = schema_node.get("items", {}) + data["items"] = _extract_type_desc(items_schema) + return data + return _extract_type_desc(self._schema) + def _flatten_schema(self, schema: dict[str, Any]) -> tuple[dict[str, Any], list[str]]: """将JSON Schema扁平化""" result = {} -- Gitee