@finogeeks2026/chatkit-web 0.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 +194 -0
- package/dist/index.css +10 -0
- package/dist/index.js +25859 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# ChatKit Web SDK
|
|
2
|
+
|
|
3
|
+
面向 Web / H5 的 AI 对话 UI SDK,支持流式对话、多会话管理、主题定制、MCP-UI 渲染、Tool Call 展示等能力。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- **流式对话**:基于 SSE 的 AG-UI 协议,实时展示 AI 回复
|
|
8
|
+
- **多会话管理**:会话列表、历史消息持久化(IndexedDB)
|
|
9
|
+
- **主题系统**:支持浅色 / 深色切换,CSS 变量定制
|
|
10
|
+
- **MCP-UI 渲染**:支持服务端下发的自定义 UI 组件渲染
|
|
11
|
+
- **Tool Call 展示**:工具调用进度与结果卡片
|
|
12
|
+
- **Consent 授权**:敏感操作前的用户同意弹窗
|
|
13
|
+
|
|
14
|
+
## 环境要求
|
|
15
|
+
|
|
16
|
+
- React 18+
|
|
17
|
+
- 现代浏览器(Chrome、Edge、Safari、Firefox)
|
|
18
|
+
|
|
19
|
+
## 安装
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @finogeeks2026/chatkit-web
|
|
23
|
+
# 或
|
|
24
|
+
pnpm add @finogeeks2026/chatkit-web
|
|
25
|
+
# 或
|
|
26
|
+
yarn add @finogeeks2026/chatkit-web
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 快速集成
|
|
30
|
+
|
|
31
|
+
### 方式一:使用 useChat(推荐)
|
|
32
|
+
|
|
33
|
+
最简接入方式,适合完整聊天界面场景。
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
import React from 'react'
|
|
37
|
+
import {
|
|
38
|
+
useChat,
|
|
39
|
+
ChatView,
|
|
40
|
+
ChatHeader,
|
|
41
|
+
HistoryPanel,
|
|
42
|
+
ThemeProvider,
|
|
43
|
+
} from '@finogeeks2026/chatkit-web'
|
|
44
|
+
import '@finogeeks2026/chatkit-web/dist/index.css'
|
|
45
|
+
|
|
46
|
+
function App() {
|
|
47
|
+
const [historyOpen, setHistoryOpen] = useState(false)
|
|
48
|
+
const {
|
|
49
|
+
messages,
|
|
50
|
+
sendMessage,
|
|
51
|
+
isLoading,
|
|
52
|
+
runtime,
|
|
53
|
+
sessions,
|
|
54
|
+
currentSessionId,
|
|
55
|
+
switchSession,
|
|
56
|
+
deleteSession,
|
|
57
|
+
pinSession,
|
|
58
|
+
createSession,
|
|
59
|
+
} = useChat({
|
|
60
|
+
apiUrl: 'https://your-api.example.com/agent',
|
|
61
|
+
getBearerToken: () => yourAuth.getToken() ?? '',
|
|
62
|
+
persist: true, // 启用 IndexedDB 持久化
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<ThemeProvider defaultMode="light">
|
|
67
|
+
<div style={{ height: '100vh', display: 'flex', flexDirection: 'column' }}>
|
|
68
|
+
<ChatHeader
|
|
69
|
+
title="AI Assistant"
|
|
70
|
+
onMenuClick={() => setHistoryOpen(true)}
|
|
71
|
+
onAddClick={() => createSession()}
|
|
72
|
+
/>
|
|
73
|
+
<ChatView
|
|
74
|
+
messages={messages}
|
|
75
|
+
onSend={sendMessage}
|
|
76
|
+
isLoading={isLoading}
|
|
77
|
+
promptStarters={['推荐相关资源', '帮我分析这个问题']}
|
|
78
|
+
runtime={runtime}
|
|
79
|
+
/>
|
|
80
|
+
<HistoryPanel
|
|
81
|
+
open={historyOpen}
|
|
82
|
+
onClose={() => setHistoryOpen(false)}
|
|
83
|
+
sessions={sessions}
|
|
84
|
+
currentSessionId={currentSessionId}
|
|
85
|
+
onSelect={switchSession}
|
|
86
|
+
onDelete={deleteSession}
|
|
87
|
+
onPin={pinSession}
|
|
88
|
+
title="历史对话"
|
|
89
|
+
/>
|
|
90
|
+
</div>
|
|
91
|
+
</ThemeProvider>
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 方式二:使用 ChatKit 门面 API
|
|
97
|
+
|
|
98
|
+
适合需要精细控制运行时和会话生命周期的场景。
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
import { ChatKit } from '@finogeeks2026/chatkit-web'
|
|
102
|
+
|
|
103
|
+
const instance = ChatKit.create({
|
|
104
|
+
apiUrl: 'https://your-api.example.com/agent',
|
|
105
|
+
getBearerToken: () => yourAuth.getToken() ?? '',
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
const conversation = instance.startConversation()
|
|
109
|
+
// 使用 conversation 发送消息、监听事件等
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### 方式三:按需组合组件
|
|
113
|
+
|
|
114
|
+
按需引入组件,自由组合布局。
|
|
115
|
+
|
|
116
|
+
```tsx
|
|
117
|
+
import {
|
|
118
|
+
useChat,
|
|
119
|
+
ChatView,
|
|
120
|
+
MessageList,
|
|
121
|
+
Composer,
|
|
122
|
+
ThemeProvider,
|
|
123
|
+
} from '@finogeeks2026/chatkit-web'
|
|
124
|
+
import '@finogeeks2026/chatkit-web/dist/index.css'
|
|
125
|
+
|
|
126
|
+
// 或更细粒度:MessageList + Composer 自定义布局
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## 配置说明
|
|
130
|
+
|
|
131
|
+
| 配置项 | 类型 | 必填 | 说明 |
|
|
132
|
+
|--------|------|------|------|
|
|
133
|
+
| `apiUrl` | `string` | 是 | 后端 AG-UI 协议接口地址 |
|
|
134
|
+
| `getBearerToken` | `() => string \| Promise<string>` | 否 | 返回鉴权 Token,请求时自动添加到 `Authorization` 头 |
|
|
135
|
+
| `persist` | `boolean` | 否 | 是否启用 IndexedDB 持久化会话和消息 |
|
|
136
|
+
|
|
137
|
+
## 主要导出
|
|
138
|
+
|
|
139
|
+
### Hooks
|
|
140
|
+
|
|
141
|
+
- `useChat`:完整聊天能力(消息、发送、会话管理)
|
|
142
|
+
- `useMessages`:仅消息与发送
|
|
143
|
+
- `useConversations`:仅会话管理
|
|
144
|
+
- `useNetworkStatus`:网络状态
|
|
145
|
+
|
|
146
|
+
### 组件
|
|
147
|
+
|
|
148
|
+
- `ChatView`:聊天主视图(消息列表 + 输入框 + 推荐问题)
|
|
149
|
+
- `ChatHeader`:顶部栏
|
|
150
|
+
- `HistoryPanel`:历史会话侧边栏
|
|
151
|
+
- `MessageList` / `MessageBubble`:消息展示
|
|
152
|
+
- `Composer`:输入框
|
|
153
|
+
- `ConsentDialog`:授权确认弹窗
|
|
154
|
+
- `ThemeProvider` / `useTheme`:主题
|
|
155
|
+
|
|
156
|
+
### 门面
|
|
157
|
+
|
|
158
|
+
- `ChatKit` / `createChatKit`:门面 API
|
|
159
|
+
|
|
160
|
+
## 样式
|
|
161
|
+
|
|
162
|
+
务必引入 SDK 的样式文件:
|
|
163
|
+
|
|
164
|
+
```tsx
|
|
165
|
+
import '@finogeeks2026/chatkit-web/dist/index.css'
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## 主题定制
|
|
169
|
+
|
|
170
|
+
通过 CSS 变量覆盖默认主题:
|
|
171
|
+
|
|
172
|
+
```css
|
|
173
|
+
:root {
|
|
174
|
+
--chatkit-bg-primary: #ffffff;
|
|
175
|
+
--chatkit-bg-secondary: #f5f5f5;
|
|
176
|
+
--chatkit-text-primary: #111111;
|
|
177
|
+
--chatkit-border: #e5e5e5;
|
|
178
|
+
/* 更多变量见 SDK 源码 theme/variables.css */
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
使用 `ThemeProvider` 可切换浅色 / 深色模式。
|
|
183
|
+
|
|
184
|
+
## 在 WebView 中使用
|
|
185
|
+
|
|
186
|
+
SDK 支持在 App 内 WebView 中加载的 H5 页面使用。需确保:
|
|
187
|
+
|
|
188
|
+
- 页面通过 **HTTPS** 加载(安全上下文)
|
|
189
|
+
- WebView 已授予麦克风权限(若使用后续语音功能)
|
|
190
|
+
- `apiUrl` 与鉴权配置正确
|
|
191
|
+
|
|
192
|
+
## License
|
|
193
|
+
|
|
194
|
+
MIT
|
package/dist/index.css
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
:root[data-theme=light],:root:not([data-theme]){--chatkit-bg-primary: #ffffff;--chatkit-bg-secondary: #f5f5f5;--chatkit-bg-tertiary: #fafafa;--chatkit-text-primary: #333333;--chatkit-text-secondary: #666666;--chatkit-text-tertiary: #999999;--chatkit-text-disabled: #cccccc;--chatkit-border: #e0e0e0;--chatkit-border-light: #f0f0f0;--chatkit-border-dark: #d1d5db;--chatkit-user-bubble-bg: #007aff;--chatkit-user-bubble-text: #ffffff;--chatkit-assistant-bubble-bg: #f5f5f5;--chatkit-assistant-bubble-text: #333333;--chatkit-prompt-bg: linear-gradient(to right, #EFF6FF, #FAF5FF);--chatkit-prompt-text: #1976d2;--chatkit-prompt-icon: #1976d2;--chatkit-input-bg: #ffffff;--chatkit-input-border: #d1d5db;--chatkit-input-placeholder: #999999;--chatkit-input-focus-border: #007aff;--chatkit-button-primary: #007aff;--chatkit-button-primary-hover: #0056b3;--chatkit-button-primary-disabled: #cccccc;--chatkit-button-text: #ffffff;--chatkit-error: #ff3b30;--chatkit-warning: #ff9500;--chatkit-success: #34c759;--chatkit-scrollbar-track: #f5f5f5;--chatkit-scrollbar-thumb: #cccccc;--chatkit-scrollbar-thumb-hover: #999999}:root[data-theme=dark]{--chatkit-bg-primary: #1a1a1a;--chatkit-bg-secondary: #2d2d2d;--chatkit-bg-tertiary: #252525;--chatkit-text-primary: #ffffff;--chatkit-text-secondary: #b3b3b3;--chatkit-text-tertiary: #808080;--chatkit-text-disabled: #4d4d4d;--chatkit-border: #404040;--chatkit-border-light: #333333;--chatkit-border-dark: #4d4d4d;--chatkit-user-bubble-bg: #007aff;--chatkit-user-bubble-text: #ffffff;--chatkit-assistant-bubble-bg: #2d2d2d;--chatkit-assistant-bubble-text: #ffffff;--chatkit-assistant-bubble-border: #404040;--chatkit-prompt-bg: #1e3a5f;--chatkit-prompt-text: #90caf9;--chatkit-prompt-icon: #90caf9;--chatkit-input-bg: #2d2d2d;--chatkit-input-border: #404040;--chatkit-input-placeholder: #808080;--chatkit-input-focus-border: #007aff;--chatkit-button-primary: #007aff;--chatkit-button-primary-hover: #0056b3;--chatkit-button-primary-disabled: #4d4d4d;--chatkit-button-text: #ffffff;--chatkit-error: #ff3b30;--chatkit-warning: #ff9500;--chatkit-success: #34c759;--chatkit-scrollbar-track: #2d2d2d;--chatkit-scrollbar-thumb: #4d4d4d;--chatkit-scrollbar-thumb-hover: #666666}pre code.hljs{display:block;overflow-x:auto;padding:1em}code.hljs{padding:3px 5px}/*!
|
|
2
|
+
Theme: GitHub
|
|
3
|
+
Description: Light theme as seen on github.com
|
|
4
|
+
Author: github.com
|
|
5
|
+
Maintainer: @Hirse
|
|
6
|
+
Updated: 2021-05-15
|
|
7
|
+
|
|
8
|
+
Outdated base version: https://github.com/primer/github-syntax-light
|
|
9
|
+
Current colors taken from GitHub's CSS
|
|
10
|
+
*/.hljs{color:#24292e;background:#fff}.hljs-doctag,.hljs-keyword,.hljs-meta .hljs-keyword,.hljs-template-tag,.hljs-template-variable,.hljs-type,.hljs-variable.language_{color:#d73a49}.hljs-title,.hljs-title.class_,.hljs-title.class_.inherited__,.hljs-title.function_{color:#6f42c1}.hljs-attr,.hljs-attribute,.hljs-literal,.hljs-meta,.hljs-number,.hljs-operator,.hljs-selector-attr,.hljs-selector-class,.hljs-selector-id,.hljs-variable{color:#005cc5}.hljs-meta .hljs-string,.hljs-regexp,.hljs-string{color:#032f62}.hljs-built_in,.hljs-symbol{color:#e36209}.hljs-code,.hljs-comment,.hljs-formula{color:#6a737d}.hljs-name,.hljs-quote,.hljs-selector-pseudo,.hljs-selector-tag{color:#22863a}.hljs-subst{color:#24292e}.hljs-section{color:#005cc5;font-weight:700}.hljs-bullet{color:#735c0f}.hljs-emphasis{color:#24292e;font-style:italic}.hljs-strong{color:#24292e;font-weight:700}.hljs-addition{color:#22863a;background-color:#f0fff4}.hljs-deletion{color:#b31d28;background-color:#ffeef0}
|