接入浏览器子产品
把任意第一方 Dispike 应用接入统一身份、计划与推理。
如何把一个新的浏览器子产品 (任何 *.dispike.com 子域应用) 接入 Dispike 身份 + 推理网关。
适用范围:本文仅覆盖 浏览器 子产品 (Chromium / Firefox / Safari)。Electron 桌面应用见 桌面集成。
前置
- dispike.com 子域 —— 必须服务于
https://<app>.dispike.com。其他域需要在 Dispike Web 应用中配置XCT_CORS_EXTRA_ORIGINS并经过安全评审。 - HTTPS 生产 —— 会话 Cookie 带
Secure,生产 http 域不会附带。 - 用户已有 Dispike 账户 —— 首次访问自动跳转到
dispike.com/login;我们不在子产品提供注册表单。
第一步 —— 拉取身份包络
async function getDispikeIdentity() {
const res = await fetch('https://dispike.com/api/me/litellm-key', {
credentials: 'include',
});
if (res.status === 401) {
window.location.href = 'https://dispike.com/login?return=' +
encodeURIComponent(window.location.href);
return null;
}
return res.json() as Promise<{
key: string;
plan: string;
models: string[];
api_base: string;
}>;
}
一次调用拿到 Bearer、用户计划、可用模型、网关地址。
第二步 —— 发起推理
const id = await getDispikeIdentity();
if (!id) return;
const res = await fetch(`${id.api_base}/chat/completions`, {
method: 'POST',
headers: {
Authorization: `Bearer ${id.key}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: id.models[0],
messages: [{ role: 'user', content: '你好' }],
}),
});
第三步 —— 响应计划变更
长会话请在 focus 事件或每 5 分钟刷新一次身份包络。降级后 chat/completions 会对已禁用模型返回 403,请优雅刷新 models 并更新 UI。
常见错误
- 忘加
credentials: 'include':浏览器不带 Cookie,永远401。 - 写死
api_base:永远读包络。我们会迁移网关域名。 - 缓存 Bearer:它是短期的 (吊销或计划变更会失效)。
401时重新拉。 - 直接调用 Stripe / GoTrue:不要。子产品不持有任何共享密钥。
Last updated: