diff --git a/pipeline_service/agent_loop_v2.py b/pipeline_service/agent_loop_v2.py index 70797e6..e0905f0 100644 --- a/pipeline_service/agent_loop_v2.py +++ b/pipeline_service/agent_loop_v2.py @@ -287,7 +287,21 @@ class AgentExecutor: async def _init_components(self): """懒加载各组件""" - # 1. 加载 org_id + workspace_dir + pipeline_id(从项目上下文,需先于 skill 加载) + # 1. 加载 org_id(用户机构优先,fallback 项目机构)+ workspace_dir + pipeline_id + # 个人选择 llm 按「用户机构」隔离(default_llm_id 指向本机构 llm),故 org_id 须取 users.orgid; + # 项目机构仅作 fallback(无 user_id 的测试场景)。 + if self.user_id: + try: + from sqlor.dbpools import DBPools + db = DBPools() + async with db.sqlorContext("pipeline") as sor: + recs = await sor.sqlExe( + "SELECT orgid FROM users WHERE id=${uid}$", {"uid": self.user_id}) + if recs: + self.org_id = getattr(recs[0], 'orgid', '') or '' + except Exception: + pass + if self.project_id: try: from sqlor.dbpools import DBPools @@ -297,7 +311,8 @@ class AgentExecutor: "SELECT org_id, workspace_dir, pipeline_id FROM sd_projects WHERE id=${pid}$", {"pid": self.project_id}) if recs: - self.org_id = getattr(recs[0], 'org_id', '') or '' + if not self.org_id: + self.org_id = getattr(recs[0], 'org_id', '') or '' ws = getattr(recs[0], 'workspace_dir', '') or '' if ws: self.workspace_dir = ws diff --git a/pipeline_service/gateway.py b/pipeline_service/gateway.py index 2e7a2c9..f2675f5 100644 --- a/pipeline_service/gateway.py +++ b/pipeline_service/gateway.py @@ -71,8 +71,11 @@ class Gateway: # ── 上下文解析 ── async def resolve_project(self, user_id: str) -> dict: - """解析用户当前项目 → {pid, pipeline_id, name}。""" - ctx = {"pid": "", "pipeline_id": "", "name": ""} + """解析用户当前项目 → {pid, pipeline_id, name, default_llm_id, default_llm_name}。 + + 同时读出个人选择的模型(default_llm_id → llm.name),供 run_message 覆盖 config.model_name。 + """ + ctx = {"pid": "", "pipeline_id": "", "name": "", "default_llm_id": "", "default_llm_name": ""} if not user_id: return ctx try: @@ -80,10 +83,15 @@ class Gateway: db = DBPools() async with db.sqlorContext("pipeline") as sor: recs = await sor.sqlExe( - "SELECT current_project_id FROM pipeline_agent_settings WHERE user_id=${u}$", + "SELECT s.current_project_id, s.default_llm_id, l.name as llm_name " + "FROM pipeline_agent_settings s " + "LEFT JOIN llm l ON l.id = s.default_llm_id AND l.status='active' " + "WHERE s.user_id=${u}$", {"u": user_id}) if recs: ctx["pid"] = getattr(recs[0], "current_project_id", "") or "" + ctx["default_llm_id"] = getattr(recs[0], "default_llm_id", "") or "" + ctx["default_llm_name"] = getattr(recs[0], "llm_name", "") or "" if ctx["pid"]: proj = await sor.sqlExe( "SELECT name, pipeline_id FROM sd_projects WHERE id=${p}$", @@ -138,6 +146,9 @@ class Gateway: # 3. 加载产线能力 + 创建 executor(generic 时不装产线能力) config = await load_agent_config( pipeline_id=ctx["pipeline_id"], project_id=ctx["pid"], generic=generic) + # 个人选择的模型优先(default_llm_id → llm.name,覆盖产线 default_model) + if ctx.get("default_llm_name"): + config.model_name = ctx["default_llm_name"] executor = AgentExecutor( config=config, project_id=ctx["pid"], user_id=user_id, role=role)