# llm_token_create.dspy — 签发短期 LLM 代理 token(登录用户,机构隔离) # # 用途:给运行环境(browser-use 子进程 / 沙箱脚本 / 被开发的 AI 应用)发一把 # 短期钥匙,代替下发真实模型 api_key。真 key 永不出服务进程。 user_id = await get_user() if not user_id: return json.dumps({"success": False, "error": "未登录"}, ensure_ascii=False) p = params_kw or {} project_id = (p.get('project_id', '') or '').strip() task_id = (p.get('task_id', '') or '').strip() model_name = (p.get('model_name', '') or '').strip() purpose = (p.get('purpose', '') or '').strip() ttl_hours = p.get('ttl_hours', 8) max_calls = p.get('max_calls', 500) # 机构隔离:token 的 org_id 一律取当前登录用户所属机构,不接受前端传入 org_id = await get_userorgid() if not org_id: org_id = '0' # 指定了 project_id 时校验 owner(防越权给别人的项目签发 token) if project_id: ok_owner, msg_owner = await check_project_owner(project_id, user_id) if not ok_owner: return json.dumps({"success": False, "error": msg_owner or "仅项目 owner 可签发"}, ensure_ascii=False) ok, token = await create_llm_token(org_id, project_id=project_id, task_id=task_id, model_name=model_name, purpose=purpose, ttl_hours=ttl_hours, max_calls=max_calls, created_by=user_id) if ok: return json.dumps({"success": True, "token": token, "org_id": org_id, "base_url": entire_url('/pipeline_core/api/llm_v1')}, ensure_ascii=False) return json.dumps({"success": False, "error": token}, ensure_ascii=False)