feat(cockpit): 会话模型选择持久化——cockpit_chat_v2 传 model_id+session_id、下拉按项目模型选中

This commit is contained in:
ymq 2026-08-28 12:26:24 +08:00
parent f80dc87e98
commit b2844cdef4
2 changed files with 25 additions and 4 deletions

View File

@ -96,6 +96,9 @@ if action == 'send_message':
ctx['pipeline_id'] = getattr(proj_recs[0], 'pipeline_id', '') or ''
# ── 走 gateway 统一入口Web AgentIO 通道)──
# model_id前端模型下拉选中的模型 → gateway 校验后持久化到项目(项目模型一经设置
# 即生效,直到用户再次选择),空 = 沿用项目已设模型。
model_id = (params_kw or {}).get('model_id', '') or ''
from pipeline_service.gateway import get_gateway
gateway = get_gateway()
@ -105,7 +108,7 @@ if action == 'send_message':
yield json.dumps({"reasoning_content": "项目: " + ctx['name'] + "\n"}, ensure_ascii=False) + '\n'
# 运行 gateway输出 content 流式格式,供前端 AgentIO→AgentOut 渲染)
async for chunk in gateway.run_message("web", uid, prompt):
async for chunk in gateway.run_message("web", uid, prompt, model_id=model_id, session_id=session_id):
data = json.loads(chunk)
t = data.get('type', '')

View File

@ -1,11 +1,14 @@
# cockpit_model_options.dspy - Returns active models with capabilities for dropdown
# Used by cockpit model selector
# org_id 多租户隔离:非系统级机构只看到本机构的模型(不含系统级兜底)
# 返回每项带 selected 标记标识个人之前选择的默认模型default_llm_id
# selected 标记 = 当前项目的已设模型sd_projects.default_model产线通用、跨会话持久
# 项目未设模型时回退个人全局选择default_llm_id。前端 UiCode 按 selected 恢复选中项,
# 不再每次重建都回退第一项。
dbname = get_module_dbname('pipeline-sdlc')
uid = await get_user()
org_id = (await get_userorgid()) or ''
session_id = (params_kw or {}).get('session_id', '') or ''
async with DBPools().sqlorContext(dbname) as sor:
sql = "SELECT id, name, provider, model_id, capabilities FROM llm WHERE status='active'"
@ -16,7 +19,22 @@ async with DBPools().sqlorContext(dbname) as sor:
sql += " ORDER BY name"
recs = await sor.sqlExe(sql, params)
# 个人之前选择的默认模型
# 项目级模型(优先级最高):按会话解析当前项目 → sd_projects.default_model。
# session_id 为空/无会话记录时自动回退全局设置,与消息链路的项目解析同一函数,不漂移。
project_model = ''
try:
from pipeline_service.workspace import get_session_project_id
_pid = await get_session_project_id(sor, uid or '', session_id)
if _pid:
_p = await sor.sqlExe(
"SELECT default_model FROM sd_projects WHERE id=${p}$", {"p": _pid})
await sor.sqlExe("COMMIT", {})
if _p:
project_model = getattr(_p[0], 'default_model', '') or ''
except Exception as e:
debug(f'cockpit_model_options project_model error: {e}')
# 个人之前选择的默认模型(项目未设模型时的回退选中项)
current_llm_id = ''
if uid:
_s = await sor.sqlExe(
@ -32,7 +50,7 @@ for r in recs:
'provider': r.provider,
'model_id': r.model_id,
'capabilities': r.capabilities or 'text',
'selected': r.id == current_llm_id,
'selected': (r.name == project_model) if project_model else (r.id == current_llm_id),
})
return rows