@tbox-claw/bridge-sdk 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -1
- package/dist/index.d.mts +15 -1
- package/dist/index.d.ts +15 -1
- package/docs/api/loginSuccess.md +151 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -95,6 +95,12 @@ interface BridgeResponse<T = unknown> {
|
|
|
95
95
|
|-----|------|:--------------:|
|
|
96
96
|
| [`getGatewayStatus()`](./docs/api/getGatewayStatus.md) | 查询 Gateway 运行状态,判断对话类 API 是否可用 | `1.2.1` |
|
|
97
97
|
|
|
98
|
+
### 自定义登录
|
|
99
|
+
|
|
100
|
+
| API | 说明 | 最小客户端版本 |
|
|
101
|
+
|-----|------|:-------:|
|
|
102
|
+
| [`loginSuccess(params)`](./docs/api/loginSuccess.md) | 三方登录页面调用,将 OAuth 授权码传给桌面端完成登录 | `1.2.5` |
|
|
103
|
+
|
|
98
104
|
---
|
|
99
105
|
|
|
100
106
|
## 工具函数
|
|
@@ -184,13 +190,14 @@ const user = await bridge.getUserInfo(); // 完整类型推导
|
|
|
184
190
|
import type {
|
|
185
191
|
TboxClawBridge, // Bridge 接口定义
|
|
186
192
|
BridgeResponse, // { ok, data?, error? }
|
|
187
|
-
UserInfo, // { userId, name, avatar }
|
|
193
|
+
UserInfo, // { userId, name, avatar, loginType?, extra? }
|
|
188
194
|
ClientInfo, // { platform, osVersion, clientVersion }
|
|
189
195
|
SendNotificationOptions, // { title, body, silent? }
|
|
190
196
|
SendChatMessageOptions, // { message, sessionKey? }
|
|
191
197
|
GetChatHistoryOptions, // { sessionKey?, limit? }
|
|
192
198
|
ChatMessage, // { role, content, timestamp?, id? }
|
|
193
199
|
GatewayStatusInfo, // { state, ready }
|
|
200
|
+
LoginSuccessParams, // { authCode }
|
|
194
201
|
} from '@tbox-claw/bridge-sdk';
|
|
195
202
|
```
|
|
196
203
|
|
package/dist/index.d.mts
CHANGED
|
@@ -64,6 +64,11 @@ interface ChatMessage {
|
|
|
64
64
|
/** Message unique identifier */
|
|
65
65
|
id?: string;
|
|
66
66
|
}
|
|
67
|
+
/** Parameters for loginSuccess — third-party login page provides the OAuth auth code */
|
|
68
|
+
interface LoginSuccessParams {
|
|
69
|
+
/** OAuth authorization code obtained from the SSO provider (e.g. Alipay auth_code) */
|
|
70
|
+
authCode: string;
|
|
71
|
+
}
|
|
67
72
|
/** Gateway runtime status */
|
|
68
73
|
interface GatewayStatusInfo {
|
|
69
74
|
/** Current lifecycle state */
|
|
@@ -87,6 +92,15 @@ interface TboxClawBridge {
|
|
|
87
92
|
getChatHistory(options?: GetChatHistoryOptions): Promise<BridgeResponse<ChatMessage[]>>;
|
|
88
93
|
/** Check whether the Gateway is running and ready */
|
|
89
94
|
getGatewayStatus(): Promise<BridgeResponse<GatewayStatusInfo>>;
|
|
95
|
+
/**
|
|
96
|
+
* Notify the desktop app that login has completed successfully.
|
|
97
|
+
* Call this after the third-party OAuth flow finishes with the obtained auth code.
|
|
98
|
+
* The desktop app will exchange the auth code for a session via the backend oauth/login API.
|
|
99
|
+
* Returns user info on success.
|
|
100
|
+
*
|
|
101
|
+
* @param params - Must include the OAuth authorization code (authCode)
|
|
102
|
+
*/
|
|
103
|
+
loginSuccess(params: LoginSuccessParams): Promise<BridgeResponse<UserInfo>>;
|
|
90
104
|
}
|
|
91
105
|
|
|
92
106
|
/** Check if currently running inside TboxClaw embedded environment */
|
|
@@ -99,4 +113,4 @@ declare function getBridge(): TboxClawBridge;
|
|
|
99
113
|
*/
|
|
100
114
|
declare function safeBridgeCall<T>(apiFn: (bridge: TboxClawBridge) => Promise<T>, fallback: T): Promise<T>;
|
|
101
115
|
|
|
102
|
-
export { type BridgeResponse, type ChatMessage, type ClientInfo, type GatewayStatusInfo, type GetChatHistoryOptions, type SendChatMessageOptions, type SendNotificationOptions, type TboxClawBridge, type UserInfo, getBridge, isTboxClawEnv, safeBridgeCall };
|
|
116
|
+
export { type BridgeResponse, type ChatMessage, type ClientInfo, type GatewayStatusInfo, type GetChatHistoryOptions, type LoginSuccessParams, type SendChatMessageOptions, type SendNotificationOptions, type TboxClawBridge, type UserInfo, getBridge, isTboxClawEnv, safeBridgeCall };
|
package/dist/index.d.ts
CHANGED
|
@@ -64,6 +64,11 @@ interface ChatMessage {
|
|
|
64
64
|
/** Message unique identifier */
|
|
65
65
|
id?: string;
|
|
66
66
|
}
|
|
67
|
+
/** Parameters for loginSuccess — third-party login page provides the OAuth auth code */
|
|
68
|
+
interface LoginSuccessParams {
|
|
69
|
+
/** OAuth authorization code obtained from the SSO provider (e.g. Alipay auth_code) */
|
|
70
|
+
authCode: string;
|
|
71
|
+
}
|
|
67
72
|
/** Gateway runtime status */
|
|
68
73
|
interface GatewayStatusInfo {
|
|
69
74
|
/** Current lifecycle state */
|
|
@@ -87,6 +92,15 @@ interface TboxClawBridge {
|
|
|
87
92
|
getChatHistory(options?: GetChatHistoryOptions): Promise<BridgeResponse<ChatMessage[]>>;
|
|
88
93
|
/** Check whether the Gateway is running and ready */
|
|
89
94
|
getGatewayStatus(): Promise<BridgeResponse<GatewayStatusInfo>>;
|
|
95
|
+
/**
|
|
96
|
+
* Notify the desktop app that login has completed successfully.
|
|
97
|
+
* Call this after the third-party OAuth flow finishes with the obtained auth code.
|
|
98
|
+
* The desktop app will exchange the auth code for a session via the backend oauth/login API.
|
|
99
|
+
* Returns user info on success.
|
|
100
|
+
*
|
|
101
|
+
* @param params - Must include the OAuth authorization code (authCode)
|
|
102
|
+
*/
|
|
103
|
+
loginSuccess(params: LoginSuccessParams): Promise<BridgeResponse<UserInfo>>;
|
|
90
104
|
}
|
|
91
105
|
|
|
92
106
|
/** Check if currently running inside TboxClaw embedded environment */
|
|
@@ -99,4 +113,4 @@ declare function getBridge(): TboxClawBridge;
|
|
|
99
113
|
*/
|
|
100
114
|
declare function safeBridgeCall<T>(apiFn: (bridge: TboxClawBridge) => Promise<T>, fallback: T): Promise<T>;
|
|
101
115
|
|
|
102
|
-
export { type BridgeResponse, type ChatMessage, type ClientInfo, type GatewayStatusInfo, type GetChatHistoryOptions, type SendChatMessageOptions, type SendNotificationOptions, type TboxClawBridge, type UserInfo, getBridge, isTboxClawEnv, safeBridgeCall };
|
|
116
|
+
export { type BridgeResponse, type ChatMessage, type ClientInfo, type GatewayStatusInfo, type GetChatHistoryOptions, type LoginSuccessParams, type SendChatMessageOptions, type SendNotificationOptions, type TboxClawBridge, type UserInfo, getBridge, isTboxClawEnv, safeBridgeCall };
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
## loginSuccess(params)
|
|
2
|
+
|
|
3
|
+
通知桌面端第三方 OAuth 登录已完成。三方登录页面在完成 SSO 授权后,将获取到的 `authCode` 传入此 API,桌面端会自动用该授权码换取会话并完成登录。
|
|
4
|
+
|
|
5
|
+
| 属性 | 值 |
|
|
6
|
+
|------|-----------------------------------------------|
|
|
7
|
+
| **最小客户端版本** | `1.2.5` |
|
|
8
|
+
| **分类** | 自定义登录 |
|
|
9
|
+
| **需要权限** | `loginSuccess` 需在页面 `bridge.allowedApis` 白名单中 |
|
|
10
|
+
| **适用场景** | 仅限 `WebContentsView` 模式的自定义登录页面 |
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
### 签名
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
loginSuccess(params: LoginSuccessParams): Promise<BridgeResponse<UserInfo>>
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### 参数
|
|
21
|
+
|
|
22
|
+
**`LoginSuccessParams` 类型定义**:
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
interface LoginSuccessParams {
|
|
26
|
+
/** OAuth 授权码,由 SSO 提供方返回(如支付宝的 auth_code) */
|
|
27
|
+
authCode: string;
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
| 参数 | 类型 | 必填 | 说明 |
|
|
32
|
+
|------|------|:----:|------|
|
|
33
|
+
| `authCode` | `string` | ✅ | SSO 授权码 |
|
|
34
|
+
|
|
35
|
+
### 返回值
|
|
36
|
+
|
|
37
|
+
`Promise<BridgeResponse<UserInfo>>`
|
|
38
|
+
|
|
39
|
+
登录成功后返回用户信息。
|
|
40
|
+
|
|
41
|
+
**`UserInfo` 类型定义**:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
interface UserInfo {
|
|
45
|
+
/** 用户 ID */
|
|
46
|
+
userId: string;
|
|
47
|
+
/** 用户昵称 */
|
|
48
|
+
name: string;
|
|
49
|
+
/** 头像 URL */
|
|
50
|
+
avatar: string;
|
|
51
|
+
/** 登录渠道(如 'tuma'、'alipay') */
|
|
52
|
+
loginType?: string;
|
|
53
|
+
/** 服务端扩展信息 */
|
|
54
|
+
extra?: Record<string, unknown>;
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### 示例
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import { getBridge } from '@tbox-claw/bridge-sdk';
|
|
62
|
+
|
|
63
|
+
const bridge = getBridge();
|
|
64
|
+
|
|
65
|
+
// 假设已从 SSO 回调中获取到 authCode
|
|
66
|
+
const authCode = 'abc123def456';
|
|
67
|
+
|
|
68
|
+
const result = await bridge.loginSuccess({ authCode });
|
|
69
|
+
|
|
70
|
+
if (result.ok) {
|
|
71
|
+
console.log('登录成功!');
|
|
72
|
+
console.log('用户:', result.data.name);
|
|
73
|
+
console.log('头像:', result.data.avatar);
|
|
74
|
+
} else {
|
|
75
|
+
console.error('登录失败:', result.error);
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### 典型用法(WebContentsView 自定义登录页面)
|
|
80
|
+
|
|
81
|
+
```html
|
|
82
|
+
<script>
|
|
83
|
+
// 在 SSO 重定向回来后,从 URL 中提取 auth_code
|
|
84
|
+
const params = new URLSearchParams(window.location.search);
|
|
85
|
+
const authCode = params.get('auth_code');
|
|
86
|
+
|
|
87
|
+
if (authCode && window.tboxClawBridge) {
|
|
88
|
+
window.tboxClawBridge.loginSuccess({ authCode })
|
|
89
|
+
.then(result => {
|
|
90
|
+
if (result.ok) {
|
|
91
|
+
// 登录成功,桌面端会自动关闭登录页面并跳转到主界面
|
|
92
|
+
console.log('登录成功', result.data);
|
|
93
|
+
} else {
|
|
94
|
+
alert('登录失败: ' + result.error);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
</script>
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 流程说明
|
|
102
|
+
|
|
103
|
+
1. 桌面端打开自定义登录页面(WebContentsView 模式)
|
|
104
|
+
2. 用户在登录页面完成 SSO 授权,页面获取到 `authCode`
|
|
105
|
+
3. 登录页面调用 `tboxClawBridge.loginSuccess({ authCode })`
|
|
106
|
+
4. 桌面端在后台执行:
|
|
107
|
+
- 用 `authCode` 调用后端 OAuth 接口换取 session
|
|
108
|
+
- 验证 session 有效性
|
|
109
|
+
- 持久化登录状态
|
|
110
|
+
5. 返回用户信息,桌面端自动关闭登录页面
|
|
111
|
+
|
|
112
|
+
### 错误场景
|
|
113
|
+
|
|
114
|
+
| error | 说明 |
|
|
115
|
+
|-------|------|
|
|
116
|
+
| `authCode is required` | 未提供 `authCode` 参数 |
|
|
117
|
+
| `OAuth login failed: ...` | 授权码换取 session 失败(码无效或已过期) |
|
|
118
|
+
| `Session verification failed: ...` | session 验证失败 |
|
|
119
|
+
| `API "loginSuccess" not allowed for page "xxx"` | 页面未配置该 API 的调用权限 |
|
|
120
|
+
|
|
121
|
+
### 返回示例
|
|
122
|
+
|
|
123
|
+
**成功**:
|
|
124
|
+
|
|
125
|
+
```json
|
|
126
|
+
{
|
|
127
|
+
"ok": true,
|
|
128
|
+
"data": {
|
|
129
|
+
"userId": "12345",
|
|
130
|
+
"name": "张三",
|
|
131
|
+
"avatar": "https://example.com/avatar.png",
|
|
132
|
+
"loginType": "tuma",
|
|
133
|
+
"extra": {}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
**失败(授权码无效)**:
|
|
139
|
+
|
|
140
|
+
```json
|
|
141
|
+
{
|
|
142
|
+
"ok": false,
|
|
143
|
+
"error": "OAuth login failed: invalid auth code"
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### 注意事项
|
|
148
|
+
|
|
149
|
+
- 此 API **仅在 `WebContentsView` 模式的自定义登录页面中可用**。`webview` 模式的登录页面通过 URL 重定向拦截自动提取 authCode,不需要调用此 API。
|
|
150
|
+
- 登录成功后,桌面端会自动关闭登录视图,无需登录页面手动处理。
|
|
151
|
+
- 每个 `authCode` 只能使用一次,重复调用会返回错误。
|