Refactor form update handling in API endpoints and models
- Introduced a new function to parse JSON values in endpoints.py for improved data handling. - Updated extract_form_update_from_flow_nodes to return structured data instead of strings. - Changed formUpdate field in ProcessResponse_chat model to use Any type with a default empty dictionary for better flexibility in handling updates.
This commit is contained in:
@@ -71,10 +71,26 @@ def extract_state_and_content(data1: str) -> dict | None:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def extract_form_update_from_flow_nodes(nodes) -> str:
|
def parse_json_value(value):
|
||||||
|
"""Parse JSON string values when possible."""
|
||||||
|
parsed = value
|
||||||
|
for _ in range(3):
|
||||||
|
if not isinstance(parsed, str):
|
||||||
|
return parsed
|
||||||
|
parsed = parsed.strip()
|
||||||
|
if not parsed:
|
||||||
|
return {}
|
||||||
|
try:
|
||||||
|
parsed = json.loads(parsed)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
return parsed
|
||||||
|
return parsed
|
||||||
|
|
||||||
|
|
||||||
|
def extract_form_update_from_flow_nodes(nodes):
|
||||||
"""Extract form update data from the configured FastGPT content-extract node."""
|
"""Extract form update data from the configured FastGPT content-extract node."""
|
||||||
if not isinstance(nodes, list):
|
if not isinstance(nodes, list):
|
||||||
return ""
|
return {}
|
||||||
|
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
if not isinstance(node, dict):
|
if not isinstance(node, dict):
|
||||||
@@ -84,16 +100,14 @@ def extract_form_update_from_flow_nodes(nodes) -> str:
|
|||||||
|
|
||||||
extract_result = node.get("extractResult", {})
|
extract_result = node.get("extractResult", {})
|
||||||
if not isinstance(extract_result, dict):
|
if not isinstance(extract_result, dict):
|
||||||
return ""
|
return {}
|
||||||
|
|
||||||
form_update = extract_result.get("formUpdate", "")
|
form_update = extract_result.get("formUpdate") or extract_result.get("form") or ""
|
||||||
if isinstance(form_update, str):
|
if not form_update:
|
||||||
return form_update
|
return {}
|
||||||
if form_update:
|
return parse_json_value(form_update)
|
||||||
return json.dumps(form_update, ensure_ascii=False)
|
|
||||||
return ""
|
|
||||||
|
|
||||||
return ""
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def format_set_info_input(payload: dict, include_input_info: bool) -> str:
|
def format_set_info_input(payload: dict, include_input_info: bool) -> str:
|
||||||
@@ -180,8 +194,8 @@ async def chat(
|
|||||||
def flush_text_delta(text: str):
|
def flush_text_delta(text: str):
|
||||||
return create_sse_event("text_delta", {"text": text})
|
return create_sse_event("text_delta", {"text": text})
|
||||||
|
|
||||||
def flush_form_update(form_update: str):
|
def flush_form_update(form_update):
|
||||||
return create_sse_event("formUpdate", {"formUpdate": form_update})
|
return create_sse_event("formUpdate", form_update)
|
||||||
|
|
||||||
async for event in aiter_stream_events(response):
|
async for event in aiter_stream_events(response):
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from typing import Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
class ProcessRequest_chat(BaseModel):
|
class ProcessRequest_chat(BaseModel):
|
||||||
sessionId: str = Field(..., max_length=64)
|
sessionId: str = Field(..., max_length=64)
|
||||||
@@ -11,7 +11,7 @@ class ProcessResponse_chat(BaseModel):
|
|||||||
sessionId: str = Field(..., max_length=64)
|
sessionId: str = Field(..., max_length=64)
|
||||||
timeStamp: str = Field(..., max_length=32)
|
timeStamp: str = Field(..., max_length=32)
|
||||||
outputText: str = Field(...)
|
outputText: str = Field(...)
|
||||||
formUpdate: str = Field(default="")
|
formUpdate: Any = Field(default_factory=dict)
|
||||||
nextStage: str = Field(..., max_length=32)
|
nextStage: str = Field(..., max_length=32)
|
||||||
nextStageCode: str = Field(..., max_length=4)
|
nextStageCode: str = Field(..., max_length=4)
|
||||||
code: str = Field(..., max_length=4)
|
code: str = Field(..., max_length=4)
|
||||||
|
|||||||
Reference in New Issue
Block a user