pipeline-sdlc/wwwroot/api/set_agent_model.dspy

45 lines
2.1 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# set_agent_model.dspy — 保存个人选择的默认模型
# 2026-09-04 收敛:模型存在性/机构归属校验统一走模型治理模块
# pipeline_llm.selection.resolve_model_name不再各模块自查表
# llm_id 兼容模型 id / vendor_model_id / 注册名,解析成功后按模型 id 持久化。
dbname = get_module_dbname('pipeline-sdlc')
uid = await get_user()
llm_id = (params_kw or {}).get('llm_id', '')
if not uid:
return json.dumps({"success": False, "error": "请先登录"}, ensure_ascii=False)
if not llm_id:
return json.dumps({"success": False, "error": "缺少 llm_id"}, ensure_ascii=False)
org_id = await get_userorgid() or '0'
# 统一解析:解析得到注册名即代表存在且属于本机构(系统级不过滤)
# capabilities='chat':个人默认模型是会话 agent 用的只接受对话能力t2t/i2t/m2t
name = await llm_resolve_model_name(llm_id, org_id=org_id, capabilities='chat')
if not name:
return json.dumps({"success": False, "error": "模型不存在、不属于本机构或不是对话模型(t2t/i2t/m2t)"}, ensure_ascii=False)
# 反查模型 id 持久化(个人默认选择按 id 存,与下拉 value 一致)
_model_id = llm_id
async with DBPools().sqlorContext(dbname) as sor:
_r = await sor.sqlExe(
"SELECT id FROM llm_model WHERE status='active' "
"AND (id=${m}$ OR vendor_model_id=${m}$ OR name=${m}$) LIMIT 1", {"m": llm_id})
await sor.sqlExe("COMMIT", {})
if _r:
_model_id = getattr(_r[0], 'id', '') or llm_id
# upsert 到 pipeline_agent_settingsuser_id 唯一键)
await sor.sqlExe(
"UPDATE pipeline_agent_settings SET default_llm_id=${lid}$ WHERE user_id=${uid}$",
{"lid": _model_id, "uid": uid})
exists = await sor.sqlExe(
"SELECT 1 FROM pipeline_agent_settings WHERE user_id=${uid}$", {"uid": uid})
if not exists:
await sor.C('pipeline_agent_settings', {
'id': getID(), 'user_id': uid, 'default_llm_id': _model_id,
})
return json.dumps({"success": True, "llm_id": _model_id, "name": name}, ensure_ascii=False)