fix: shared app uses release snapshot config instead of draft in draft_run and get_agent_config

This commit is contained in:
wxy
2026-03-18 16:38:22 +08:00
parent 56c98648f9
commit f042f44501
2 changed files with 94 additions and 12 deletions

View File

@@ -559,18 +559,29 @@ async def draft_run(
service._check_agent_config(app_id)
# 2. 获取 Agent 配置
stmt = select(AgentConfig).where(AgentConfig.app_id == app_id)
agent_cfg = db.scalars(stmt).first()
if not agent_cfg:
raise BusinessException("Agent 配置不存在", BizCode.AGENT_CONFIG_MISSING)
# 共享应用:从最新发布版本读配置快照,而非草稿
is_shared = app.workspace_id != workspace_id
if is_shared:
if not app.current_release_id:
raise BusinessException("该应用尚未发布,无法使用", BizCode.AGENT_CONFIG_MISSING)
release = db.get(AppRelease, app.current_release_id)
if not release:
raise BusinessException("发布版本不存在", BizCode.AGENT_CONFIG_MISSING)
agent_cfg = service._agent_config_from_release(release)
model_config = db.get(ModelConfig, release.default_model_config_id) if release.default_model_config_id else None
else:
stmt = select(AgentConfig).where(AgentConfig.app_id == app_id)
agent_cfg = db.scalars(stmt).first()
if not agent_cfg:
raise BusinessException("Agent 配置不存在", BizCode.AGENT_CONFIG_MISSING)
# 3. 获取模型配置
model_config = None
if agent_cfg.default_model_config_id:
model_config = db.get(ModelConfig, agent_cfg.default_model_config_id)
if not model_config:
from app.core.exceptions import ResourceNotFoundException
raise ResourceNotFoundException("模型配置", str(agent_cfg.default_model_config_id))
# 3. 获取模型配置
model_config = None
if agent_cfg.default_model_config_id:
model_config = db.get(ModelConfig, agent_cfg.default_model_config_id)
if not model_config:
from app.core.exceptions import ResourceNotFoundException
raise ResourceNotFoundException("模型配置", str(agent_cfg.default_model_config_id))
# 流式返回
if payload.stream:
@@ -726,7 +737,18 @@ async def draft_run(
msg="多 Agent 任务执行成功"
)
elif app.type == AppType.WORKFLOW: # 工作流
config = workflow_service.check_config(app_id)
# 共享应用:从最新发布版本读配置快照,而非草稿
is_shared = app.workspace_id != workspace_id
if is_shared:
if not app.current_release_id:
raise BusinessException("该应用尚未发布,无法使用", BizCode.AGENT_CONFIG_MISSING)
from app.models import AppRelease
release = db.get(AppRelease, app.current_release_id)
if not release:
raise BusinessException("发布版本不存在", BizCode.AGENT_CONFIG_MISSING)
config = service._workflow_config_from_release(release)
else:
config = workflow_service.check_config(app_id)
# 3. 流式返回
if payload.stream:
logger.debug(

View File

@@ -1426,6 +1426,47 @@ class AppService:
logger.info("Agent 配置更新成功", extra={"app_id": str(app_id)})
return agent_cfg
def _agent_config_from_release(self, release: "AppRelease") -> "AgentConfig":
"""从发布版本快照重建 AgentConfig 对象(不入库,仅用于运行)"""
cfg = release.config or {}
now = release.created_at or datetime.datetime.now()
agent_cfg = AgentConfig(
id=uuid.uuid4(),
app_id=release.app_id,
system_prompt=cfg.get("system_prompt", ""),
default_model_config_id=release.default_model_config_id,
model_parameters=cfg.get("model_parameters"),
knowledge_retrieval=cfg.get("knowledge_retrieval"),
memory=cfg.get("memory", {}),
variables=cfg.get("variables", []),
tools=cfg.get("tools", []),
skills=cfg.get("skills", {}),
features=cfg.get("features", {}),
is_active=True,
created_at=now,
updated_at=now,
)
return agent_cfg
def _workflow_config_from_release(self, release: "AppRelease") -> "WorkflowConfig":
"""从发布版本快照重建 WorkflowConfig 对象(不入库,仅用于运行)"""
cfg = release.config or {}
now = release.created_at or datetime.datetime.now()
from app.models.workflow_model import WorkflowConfig as WorkflowConfigModel
wf_cfg = WorkflowConfigModel(
id=uuid.uuid4(),
app_id=release.app_id,
nodes=cfg.get("nodes", []),
edges=cfg.get("edges", []),
variables=cfg.get("variables", []),
execution_config=cfg.get("execution_config", {}),
triggers=cfg.get("triggers", []),
is_active=True,
created_at=now,
updated_at=now,
)
return wf_cfg
def get_agent_config(
self,
*,
@@ -1457,6 +1498,15 @@ class AppService:
# 只读操作,允许访问共享应用
self._validate_app_accessible(app, workspace_id)
# 共享应用:返回最新发布版本的配置快照,而非草稿
if workspace_id and app.workspace_id != workspace_id:
if not app.current_release_id:
raise BusinessException("该应用尚未发布,无法使用", BizCode.AGENT_CONFIG_MISSING)
release = self.db.get(AppRelease, app.current_release_id)
if not release:
raise BusinessException("发布版本不存在", BizCode.AGENT_CONFIG_MISSING)
return self._agent_config_from_release(release)
stmt = select(AgentConfig).where(
AgentConfig.app_id == app_id,
AgentConfig.is_active.is_(True)
@@ -1555,6 +1605,16 @@ class AppService:
# 只读操作,允许访问共享应用
self._validate_app_accessible(app, workspace_id)
# 共享应用:返回最新发布版本的配置快照,而非草稿
if workspace_id and app.workspace_id != workspace_id:
if not app.current_release_id:
raise BusinessException("该应用尚未发布,无法使用", BizCode.CONFIG_MISSING)
release = self.db.get(AppRelease, app.current_release_id)
if not release:
raise BusinessException("发布版本不存在", BizCode.CONFIG_MISSING)
return self._workflow_config_from_release(release)
repo = WorkflowConfigRepository(self.db)
config = repo.get_by_app_id(app_id)
if config: