Files
syops/backend/seed_shipping_notice_agents.py
T
hugo c18c0394d0 feat(shipping_notice): 完成货代发货通知全流程功能迭代与重构
主要变更:
1.  移除废弃的发货通知字段抽取独立Workflow Agent相关代码
2.  重构Dify服务调用,新增inputs参数支持注入环境变量配置的工具API地址与密钥
3.  新增auto_progress工具API与checkpoint catalog接口,支持流程自动推进
4.  统一字段命名,将column_mapping_json改为map_json并兼容旧有调用
5.  重构库存匹配、解析结果的展示格式,改为标准化多语言Markdown表格
6.  新增ODM ID库存字段支持,优化库存解析与映射逻辑
7.  新增LibreOffice转换隔离Profile配置,避免多实例运行冲突
8.  新增完整环境变量配置、数据库迁移脚本与测试用例
9.  适配前端代码,更新字段命名与聊天跳转逻辑
10. 更新OpenAPI规范适配Dify工具调用,删除已归档的旧部署文档
2026-06-08 16:07:20 +08:00

94 lines
2.9 KiB
Python

"""Seed agents for freight shipping notice Chatflow orchestration."""
import sqlalchemy as sa
from app import create_app, db
from app.models import Agent
from app.services.shipping_notice.agent_resolver import CHAT_AGENT_NAME
app = create_app()
CHAT_DESCRIPTION = "货代发货通知智能体:自然语言驱动解析、库存匹配、Excel 生成与货代上传(Tool 编排)"
def _normalize_url(raw: str | None) -> str | None:
if raw is None:
return None
text = str(raw).strip()
if not text:
return None
if (text.startswith("`") and text.endswith("`")) or (text.startswith('"') and text.endswith('"')):
text = text[1:-1].strip()
return text or None
def _ensure_pg_sequence(table: str, pk: str) -> None:
engine_name = db.engine.dialect.name
if engine_name != "postgresql":
return
if not table.replace("_", "").isalnum() or not pk.replace("_", "").isalnum():
raise ValueError("invalid identifier")
sql = sa.text(
f"SELECT setval(pg_get_serial_sequence(:tbl, :pk), COALESCE((SELECT MAX({pk}) FROM {table}), 0), true)"
)
db.session.execute(sql, {"tbl": table, "pk": pk})
db.session.commit()
def _upsert_agent(
*,
name: str,
description: str,
dify_app_type: str,
dify_app_id: str | None = None,
dify_api_key: str | None = None,
api_base_url: str | None = None,
):
agent = Agent.query.filter_by(name=name).first()
extra_config = {"dify_app_type": dify_app_type}
if not agent:
agent = Agent(
name=name,
description=description,
type="api",
dify_app_id=dify_app_id,
dify_api_key=dify_api_key,
api_base_url=api_base_url,
extra_config=extra_config,
is_active=True,
)
db.session.add(agent)
print(f"Created agent: {name}")
else:
agent.description = description
agent.type = "api"
agent.extra_config = extra_config
if dify_app_id:
agent.dify_app_id = dify_app_id
if dify_api_key:
agent.dify_api_key = dify_api_key
if api_base_url:
agent.api_base_url = api_base_url
agent.is_active = True
print(f"Updated agent: {name}")
return agent
if __name__ == "__main__":
import os
with app.app_context():
_ensure_pg_sequence("agents", "id")
chat_key = os.getenv("SHIPPING_NOTICE_CHAT_DIFY_API_KEY") or os.getenv("DIFY_API_KEY")
chat_base = _normalize_url(os.getenv("SHIPPING_NOTICE_CHAT_DIFY_BASE") or os.getenv("DIFY_API_BASE"))
_upsert_agent(
name=CHAT_AGENT_NAME,
description=CHAT_DESCRIPTION,
dify_app_type="chatflow",
dify_app_id=os.getenv("SHIPPING_NOTICE_CHAT_DIFY_APP_ID"),
dify_api_key=chat_key,
api_base_url=chat_base,
)
db.session.commit()
print("Shipping notice agents seeded.")