Files
writer-work-flow/apps/api/ww_api/services/credentials.py
Yaojia Wang 765dbdfbd4 feat: M4 文风 + M5 生成/多provider/Skill + Kimi Code 订阅接入 + 本地联调修复
M4(文风): style-auditor 双轨(提取指纹/漂移第四审)+ jobs 长任务框架(zombie reaper) + 回炉 refine + GET /style read-back。
M5(生成+扩展): worldbuilder/character-gen(入库 continuity 409 gate + partition_writes 白名单 + schema→JSONB 形变);
  网关多 provider 回退链/熔断/能力降级(Anthropic/Gemini 适配器);Skill registry + 表权限沙箱 + 规则;
  前端 角色生成器/世界观/Codex/规则页/技能库/⌘K 命令面板。
K1(Kimi Code 订阅接入): OAuth device-flow(kimi-code)+ 静态 Console key(kimi-code-key)两路径;
  coding 端点 KimiCLI 伪造头(实测 UA allow-list 门禁,缺则 403)+ JSON 模式结构化(thinking ⊥ tool_choice)。
本地联调修复: CORS 中间件;assemble 注入 premise+「写第N章」指令(修空 prompt 400);
  GET /outline·/draft read-back + 大纲/工作台/审稿页重载;写页 client/server 常量边界 + notFound 健壮化;
  字数 toLocaleString locale 水合;审稿页终稿从已存草稿 seed(修 accept 422)。
门禁: backend ruff/mypy(157)/alembic 无漂移/pytest 451 · frontend lint/tsc/vitest/build。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-20 10:39:58 +02:00

237 lines
8.3 KiB
Python
Raw 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.

"""凭据存储与提供商探测的接口 + SQLAlchemy/网关实现ARCH §4.7)。
路由依赖这里的 **接口**Protocol测试注入内存替身运行时用 SQLAlchemy/网关实现。
单用户原型:`owner_id` 用固定 stub见 `STUB_OWNER_ID`),多租户化时改为按认证主体取。
"""
from __future__ import annotations
import uuid
from dataclasses import dataclass
from typing import Protocol
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from ww_db.models import ProviderCredential, TierRouting
from ww_llm_gateway.adapters.base import Capabilities
# 单用户 stub owner与网关 Scope.user_id 的 stub 约定一致UUID(int=1))。
# 多租户化时此常量由认证主体替换(见 ARCH §4.7 隔离)。
STUB_OWNER_ID = uuid.UUID(int=1)
# 凭据认证类型(`provider_credentials.auth_type`,见 C2 扩 K1.1)。
AUTH_TYPE_API_KEY = "api_key"
AUTH_TYPE_OAUTH = "oauth"
@dataclass(frozen=True)
class StoredCredential:
"""存储层视图:含密文,绝不出 API 边界(路由仅取 provider 并掩码)。
一行二选一:`auth_type="api_key"` → `api_key_enc` 有值、`oauth_enc=None`
`auth_type="oauth"`Kimi Code device-flowK1.3)→ `oauth_enc` 有值、`api_key_enc=None`
(持 Fernet 加密的 `{access_token,refresh_token,expires_at}` JSON 包)。
"""
provider: str
api_key_enc: bytes | None
auth_type: str = AUTH_TYPE_API_KEY
oauth_enc: bytes | None = None
@dataclass(frozen=True)
class StoredRouting:
tier: str
provider: str
model: str
fallback: list[str]
class CredentialStore(Protocol):
"""凭据 + 档位路由的读写接口(按 owner_id 隔离)。"""
async def list_credentials(self, owner_id: uuid.UUID) -> list[StoredCredential]: ...
async def list_routing(self) -> list[StoredRouting]: ...
async def get_credential(
self, owner_id: uuid.UUID, provider: str
) -> StoredCredential | None: ...
async def upsert_credential(
self, owner_id: uuid.UUID, provider: str, api_key_enc: bytes
) -> None: ...
async def upsert_oauth_credential(
self, owner_id: uuid.UUID, provider: str, oauth_enc: bytes
) -> None: ...
async def delete_credential(self, owner_id: uuid.UUID, provider: str) -> bool: ...
async def upsert_routing(self, routing: StoredRouting) -> None: ...
class ProviderProbe(Protocol):
"""最小连通探测:验证 Key + 返回能力矩阵。测试注入假探测,绝不联网。"""
async def probe(self, owner_id: uuid.UUID, provider: str) -> Capabilities: ...
class SqlCredentialStore:
"""SQLAlchemy 实现:写 `provider_credentials` / `tier_routing`,幂等 upsert。"""
def __init__(self, session: AsyncSession) -> None:
self._session = session
async def list_credentials(self, owner_id: uuid.UUID) -> list[StoredCredential]:
rows = (
await self._session.execute(
select(ProviderCredential).where(ProviderCredential.owner_id == owner_id)
)
).scalars()
return [
StoredCredential(
provider=r.provider,
api_key_enc=r.api_key_enc,
auth_type=r.auth_type,
oauth_enc=r.oauth_enc,
)
for r in rows
]
async def list_routing(self) -> list[StoredRouting]:
rows = (await self._session.execute(select(TierRouting))).scalars()
return [
StoredRouting(
tier=r.tier, provider=r.provider, model=r.model, fallback=list(r.fallback)
)
for r in rows
]
async def get_credential(self, owner_id: uuid.UUID, provider: str) -> StoredCredential | None:
row = (
await self._session.execute(
select(ProviderCredential).where(
ProviderCredential.owner_id == owner_id,
ProviderCredential.provider == provider,
)
)
).scalar_one_or_none()
if row is None:
return None
return StoredCredential(
provider=row.provider,
api_key_enc=row.api_key_enc,
auth_type=row.auth_type,
oauth_enc=row.oauth_enc,
)
async def upsert_credential(
self, owner_id: uuid.UUID, provider: str, api_key_enc: bytes
) -> None:
# 显式 read-modify-write唯一约束含可空 project_idPG ON CONFLICT
# 在 NULL 上不去重NULLS DISTINCT故不用 on_conflict。
existing = (
await self._session.execute(
select(ProviderCredential).where(
ProviderCredential.owner_id == owner_id,
ProviderCredential.project_id.is_(None),
ProviderCredential.provider == provider,
)
)
).scalar_one_or_none()
if existing is None:
self._session.add(
ProviderCredential(
owner_id=owner_id,
project_id=None,
provider=provider,
api_key_enc=api_key_enc,
auth_type=AUTH_TYPE_API_KEY,
oauth_enc=None,
)
)
else:
existing.api_key_enc = api_key_enc
existing.auth_type = AUTH_TYPE_API_KEY
existing.oauth_enc = None
await self._session.commit()
async def upsert_oauth_credential(
self, owner_id: uuid.UUID, provider: str, oauth_enc: bytes
) -> None:
"""写/更新 OAuth 凭据行Kimi Code device-flowK1.3)。
`auth_type="oauth"`、`oauth_enc=<Fernet 加密 token 包>`、`api_key_enc=None`。
显式 read-modify-write同 `upsert_credential`:含可空 project_id 的唯一约束不能用
PG `ON CONFLICT`,见 memory/gotchas。明文 token 绝不进此层(已加密)。
"""
existing = (
await self._session.execute(
select(ProviderCredential).where(
ProviderCredential.owner_id == owner_id,
ProviderCredential.project_id.is_(None),
ProviderCredential.provider == provider,
)
)
).scalar_one_or_none()
if existing is None:
self._session.add(
ProviderCredential(
owner_id=owner_id,
project_id=None,
provider=provider,
api_key_enc=None,
auth_type=AUTH_TYPE_OAUTH,
oauth_enc=oauth_enc,
)
)
else:
existing.api_key_enc = None
existing.auth_type = AUTH_TYPE_OAUTH
existing.oauth_enc = oauth_enc
await self._session.commit()
async def delete_credential(self, owner_id: uuid.UUID, provider: str) -> bool:
"""删除凭据行OAuth disconnect / 撤销)。返回是否删到行。"""
existing = (
await self._session.execute(
select(ProviderCredential).where(
ProviderCredential.owner_id == owner_id,
ProviderCredential.project_id.is_(None),
ProviderCredential.provider == provider,
)
)
).scalar_one_or_none()
if existing is None:
return False
await self._session.delete(existing)
await self._session.commit()
return True
async def upsert_routing(self, routing: StoredRouting) -> None:
existing = (
await self._session.execute(
select(TierRouting).where(
TierRouting.project_id.is_(None),
TierRouting.tier == routing.tier,
)
)
).scalar_one_or_none()
if existing is None:
self._session.add(
TierRouting(
project_id=None,
tier=routing.tier,
provider=routing.provider,
model=routing.model,
fallback=routing.fallback,
)
)
else:
existing.provider = routing.provider
existing.model = routing.model
existing.fallback = routing.fallback
await self._session.commit()