fix: wire ai platform run and codex chat

This commit is contained in:
2026-06-13 11:45:03 +08:00
parent af879e4607
commit 9632c1b4a6
7 changed files with 526 additions and 24 deletions
@@ -44,6 +44,75 @@ from ai_platform.services.agent_import_export import (
logger = logging.getLogger(__name__)
CODEX_AGENT_PROMPT = """
你是 ai-agent-admin 内置的 Codex 协作助手。你的目标是帮助用户拆解工程任务、解释代码、规划实现、生成可执行步骤,并在需要时提示用户进入智能体、流程编排、模型配置和执行历史等模块完成操作。
回答要求:
- 直接、具体、可执行。
- 优先给出最小可落地方案,再说明关键风险。
- 涉及代码或配置时,明确文件、接口、命令或字段。
- 对不确定事实要说明需要验证,不要编造。
""".strip()
async def _resolve_default_chat_model_id(db: AsyncSession) -> Optional[str]:
result = await db.execute(
select(LLMModel)
.where(
LLMModel.is_deleted == False,
LLMModel.is_active == True,
LLMModel.model_type == "chat",
)
.order_by(LLMModel.sort.desc(), LLMModel.sys_create_datetime.desc())
)
model = result.scalars().first()
return str(model.id) if model else None
async def _ensure_codex_agent(db: AsyncSession) -> Agent:
model_id = await _resolve_default_chat_model_id(db)
if not model_id:
raise HTTPException(
status_code=400,
detail="Codex 智能体需要先配置一个启用的 chat 模型",
)
agent = Agent(
name="Codex",
code="codex",
description="内置通用 AI 编程协作助手",
mode="autonomous",
status="published",
is_global=True,
is_public=True,
model_id=model_id,
temperature=0.3,
top_p=1.0,
max_tokens=4096,
max_iterations=6,
enable_memory=True,
memory_window=10,
enable_streaming=True,
persona={
"role": "AI 编程协作助手",
"skills": ["代码理解", "任务拆解", "方案设计", "问题排查"],
"constraints": ["不编造事实", "优先输出可执行步骤"],
},
system_prompt=CODEX_AGENT_PROMPT,
welcome_message="我是 Codex 协作助手,可以帮你拆解任务、解释代码、规划实现和排查问题。",
suggested_questions=[
"帮我分析当前 AI Agent Admin 的下一步实现重点",
"解释一下智能体和流程编排如何协作",
"帮我设计一个 OC-69 多智能体协作流程",
],
sort=100,
)
db.add(agent)
await db.commit()
await db.refresh(agent)
return agent
router = APIRouter(prefix="/agent", tags=["AI-智能体"])
@@ -171,6 +240,8 @@ async def get_agent_by_code(code: str, db: AsyncSession = Depends(get_db)):
select(Agent).where(Agent.code == code, Agent.is_deleted == False)
)
agent = result.scalar_one_or_none()
if not agent and code == "codex":
agent = await _ensure_codex_agent(db)
if not agent:
raise HTTPException(status_code=404, detail="智能体不存在")