@kweaver-ai/chatkit 0.1.0

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.
Files changed (47) hide show
  1. package/README.md +528 -0
  2. package/dist/chatkit.cjs.js +66 -0
  3. package/dist/chatkit.es.js +2966 -0
  4. package/dist/components/base/ChatKitBase.d.ts +261 -0
  5. package/dist/components/base/assistant/AssistantBase.d.ts +17 -0
  6. package/dist/components/base/assistant/Header.d.ts +20 -0
  7. package/dist/components/base/assistant/InputArea.d.ts +29 -0
  8. package/dist/components/base/assistant/MessageItem.d.ts +15 -0
  9. package/dist/components/base/assistant/MessageList.d.ts +15 -0
  10. package/dist/components/base/assistant/Prologue.d.ts +18 -0
  11. package/dist/components/base/assistant/blocks/MarkdownBlock.d.ts +15 -0
  12. package/dist/components/base/assistant/blocks/TextBlock.d.ts +15 -0
  13. package/dist/components/base/assistant/blocks/ToolBlock.d.ts +16 -0
  14. package/dist/components/base/assistant/blocks/ToolDrawer.d.ts +26 -0
  15. package/dist/components/base/assistant/blocks/WebSearchBlock.d.ts +16 -0
  16. package/dist/components/base/assistant/blocks/index.d.ts +9 -0
  17. package/dist/components/base/copilot/CopilotBase.d.ts +16 -0
  18. package/dist/components/base/copilot/Header.d.ts +22 -0
  19. package/dist/components/base/copilot/InputArea.d.ts +29 -0
  20. package/dist/components/base/copilot/MessageItem.d.ts +15 -0
  21. package/dist/components/base/copilot/MessageList.d.ts +15 -0
  22. package/dist/components/base/copilot/Prologue.d.ts +18 -0
  23. package/dist/components/base/copilot/blocks/MarkdownBlock.d.ts +15 -0
  24. package/dist/components/base/copilot/blocks/TextBlock.d.ts +15 -0
  25. package/dist/components/base/copilot/blocks/ToolBlock.d.ts +16 -0
  26. package/dist/components/base/copilot/blocks/ToolDrawer.d.ts +26 -0
  27. package/dist/components/base/copilot/blocks/WebSearchBlock.d.ts +16 -0
  28. package/dist/components/base/copilot/blocks/index.d.ts +9 -0
  29. package/dist/components/coze/Copilot.d.ts +102 -0
  30. package/dist/components/dip/Assistant.d.ts +22 -0
  31. package/dist/components/dip/Copilot.d.ts +22 -0
  32. package/dist/components/dip/DIPBase.d.ts +310 -0
  33. package/dist/components/icons/ClockIcon.d.ts +6 -0
  34. package/dist/components/icons/CloseIcon.d.ts +6 -0
  35. package/dist/components/icons/SendIcon.d.ts +13 -0
  36. package/dist/components/icons/StopIcon.d.ts +6 -0
  37. package/dist/components/icons/index.d.ts +7 -0
  38. package/dist/icons/assistant.svg +14 -0
  39. package/dist/icons/close.svg +6 -0
  40. package/dist/icons/expand.svg +3 -0
  41. package/dist/icons/more.svg +3 -0
  42. package/dist/icons/new.svg +10 -0
  43. package/dist/icons/send.svg +4 -0
  44. package/dist/index.d.ts +20 -0
  45. package/dist/types/index.d.ts +285 -0
  46. package/dist/utils/mixins.d.ts +18 -0
  47. package/package.json +66 -0
@@ -0,0 +1,285 @@
1
+ /**
2
+ * 角色类型枚举
3
+ * 发送该消息的角色
4
+ */
5
+ export declare enum RoleType {
6
+ /** 用户 */
7
+ USER = "User",
8
+ /** AI 助手 */
9
+ ASSISTANT = "Assistant"
10
+ }
11
+ /**
12
+ * 消息类型枚举
13
+ * 消息的类型
14
+ */
15
+ export declare enum ChatMessageType {
16
+ /** Markdown 文本类型 */
17
+ TEXT = "Text",
18
+ /** JSON 类型 */
19
+ JSON = "JSON",
20
+ /** Widget 组件 */
21
+ WIDGET = "Widget"
22
+ }
23
+ /**
24
+ * 消息块类型枚举
25
+ * 消息块的类型,不同类型的消息块使用不同的组件进行渲染
26
+ */
27
+ export declare enum BlockType {
28
+ /** 文本类型 */
29
+ TEXT = "Text",
30
+ /** Markdown 类型 */
31
+ MARKDOWN = "Markdown",
32
+ /** Web 搜索类型 */
33
+ WEB_SEARCH = "WebSearch",
34
+ /** 工具调用类型 */
35
+ TOOL = "Tool"
36
+ }
37
+ /**
38
+ * 角色接口
39
+ * 定义消息发送者的角色信息
40
+ */
41
+ export interface Role {
42
+ /** 角色的名称:
43
+ * - 如果 type 是 Assistant,则名称为 "AI 助手"
44
+ * - 如果 type 是 User,则名称为用户的昵称/显示名
45
+ */
46
+ name: string;
47
+ /** 发送该消息的角色 */
48
+ type: RoleType;
49
+ /** 角色的头像,可以是 URL、Base64 或 SVG */
50
+ avatar: string;
51
+ }
52
+ /**
53
+ * 消息块基类
54
+ * 一条消息可以由许多不同类型的消息块组成
55
+ */
56
+ export interface ContentBlock<T extends BlockType, K> {
57
+ /** 消息块的类型,不同类型的消息块使用不同的组件进行渲染 */
58
+ type: T;
59
+ /** 消息块的内容 */
60
+ content: K;
61
+ }
62
+ /**
63
+ * 文本类型的消息块
64
+ */
65
+ export interface TextBlock extends ContentBlock<BlockType.TEXT, string> {
66
+ }
67
+ /**
68
+ * Markdown 类型的消息块
69
+ */
70
+ export interface MarkdownBlock extends ContentBlock<BlockType.MARKDOWN, string> {
71
+ }
72
+ /**
73
+ * Web 搜索类型的消息块
74
+ */
75
+ export interface WebSearchBlock extends ContentBlock<BlockType.WEB_SEARCH, WebSearchQuery> {
76
+ }
77
+ /**
78
+ * 工具调用类型的消息块
79
+ */
80
+ export interface ToolBlock extends ContentBlock<BlockType.TOOL, ToolCallData> {
81
+ }
82
+ /**
83
+ * 消息接口
84
+ * 展示在消息区消息列表中的一条消息
85
+ */
86
+ export interface ChatMessage {
87
+ /** 一条消息的 ID */
88
+ messageId: string;
89
+ /** 发送该消息的角色 */
90
+ role: Role;
91
+ /** 该条消息的类型(已废弃,保留用于向后兼容) */
92
+ type?: ChatMessageType;
93
+ /** 该条消息的内容。一条消息可以由许多不同类型的消息块组成 */
94
+ content: Array<TextBlock | MarkdownBlock | WebSearchBlock | ToolBlock>;
95
+ /** 与该消息关联的应用上下文(可选),仅用户消息可能包含此字段 */
96
+ applicationContext?: ApplicationContext;
97
+ }
98
+ /**
99
+ * 应用上下文接口
100
+ * 与用户输入的文本相关的应用上下文
101
+ */
102
+ export interface ApplicationContext {
103
+ /** 显示在输入框上方的应用上下文标题 */
104
+ title: string;
105
+ /** 该应用上下文实际包含的数据 */
106
+ data: any;
107
+ }
108
+ /**
109
+ * EventStream 消息接口
110
+ * 表示从 SSE 接收到的一条流式消息
111
+ */
112
+ export interface EventStreamMessage {
113
+ /** 事件类型 */
114
+ event: string;
115
+ /** 消息数据,通常是 JSON 字符串 */
116
+ data: string;
117
+ }
118
+ /**
119
+ * 开场白信息接口
120
+ * 包含开场白文案和预置问题
121
+ */
122
+ export interface OnboardingInfo {
123
+ /** 开场白文案 */
124
+ prologue: string;
125
+ /** 预置问题列表 */
126
+ predefinedQuestions: Array<string>;
127
+ }
128
+ /**
129
+ * Web 搜索结果接口
130
+ * 单条 Web 搜索的结果
131
+ */
132
+ export interface WebSearchResult {
133
+ /** 搜索结果的内容摘要 */
134
+ content: string;
135
+ /** 搜索结果的来源网站图标 URL */
136
+ icon: string;
137
+ /** 搜索结果的来源地址 */
138
+ link: string;
139
+ /** 搜索结果的来源网站名称 */
140
+ media: string;
141
+ /** 搜索结果的来源文章标题 */
142
+ title: string;
143
+ }
144
+ /**
145
+ * Web 搜索查询接口
146
+ * 调用 Web 搜索的详情
147
+ */
148
+ export interface WebSearchQuery {
149
+ /** 搜索查询 */
150
+ input: string;
151
+ /** Web 搜索结果集合 */
152
+ results: WebSearchResult[];
153
+ }
154
+ /**
155
+ * 代码执行结果接口
156
+ * 代码执行的输入和输出信息
157
+ */
158
+ export interface ExecuteCodeResult {
159
+ /** 执行的代码,以 Markdown 的代码格式显示 */
160
+ input: string;
161
+ /** 代码执行后转为自然语言的输出结果,以 Markdown 格式显示 */
162
+ output: string;
163
+ }
164
+ /**
165
+ * 工具调用数据接口
166
+ * 工具调用的输入和输出信息
167
+ */
168
+ export interface ToolCallData {
169
+ /** 工具名称 */
170
+ name: string;
171
+ /** 工具图标 URL 或 React 元素 */
172
+ icon?: string | React.ReactNode;
173
+ /** 工具标题 */
174
+ title: string;
175
+ /** 工具调用的输入参数 */
176
+ input?: any;
177
+ /** 工具调用的输出结果 */
178
+ output?: any;
179
+ }
180
+ /**
181
+ * 历史会话记录接口
182
+ * 一条历史会话记录
183
+ */
184
+ export interface ConversationHistory {
185
+ /** 会话 ID */
186
+ conversationID: string;
187
+ /** 会话标题 */
188
+ title: string;
189
+ /** 会话创建时间(Unix 时间戳) */
190
+ created_at: number;
191
+ /** 会话最后更新时间(Unix 时间戳) */
192
+ updated_at: number;
193
+ /** 最新会话消息下标 */
194
+ message_index?: number;
195
+ /** 最新已读的会话消息下标 */
196
+ read_message_index?: number;
197
+ }
198
+ /**
199
+ * ChatKit 接口
200
+ * 定义了 ChatKit 的一些抽象方法
201
+ */
202
+ export interface ChatKitInterface {
203
+ /**
204
+ * 获取开场白和预置问题
205
+ * 该方法需要由子类继承并重写,以适配扣子、Dify 等 LLMOps 平台的接口
206
+ * 返回开场白信息结构体
207
+ * 注意:该方法是一个无状态无副作用的函数,不允许修改 state
208
+ * @returns 返回开场白信息,包含开场白文案和预置问题
209
+ */
210
+ getOnboardingInfo(): Promise<OnboardingInfo>;
211
+ /**
212
+ * 新建会话
213
+ * 该方法需要由子类继承并重写,以适配扣子、Dify 等 LLMOps 平台的接口
214
+ * 成功返回会话 ID
215
+ * 注意:该方法是一个无状态无副作用的函数,不允许修改 state
216
+ * @returns 返回新创建的会话 ID
217
+ */
218
+ generateConversation(): Promise<string>;
219
+ /**
220
+ * 向后端发送消息
221
+ * 该方法需要由开发者实现,以适配扣子、Dify等 LLMOps 平台的接口
222
+ * 发送成功后,返回发送的消息结构
223
+ * 注意:该方法是一个无状态无副作用的函数,不允许修改 state
224
+ * @param text 发送给后端的用户输入的文本
225
+ * @param ctx 随用户输入文本一起发送的应用上下文
226
+ * @param conversationID 发送的对话消息所属的会话 ID
227
+ * @returns 返回发送的消息结构
228
+ */
229
+ sendMessage(text: string, ctx: ApplicationContext, conversationID?: string): Promise<ChatMessage>;
230
+ /**
231
+ * 将 API 接口返回的 EventStream 增量解析成完整的 AssistantMessage 对象
232
+ * 当接收到 SSE 消息时触发,该方法需要由子类实现
233
+ * 子类在该方法中应该调用 appendMarkdownBlock() 或 appendWebSearchBlock() 来更新消息内容
234
+ * 注意:该方法应该只处理数据解析逻辑,通过调用 append*Block 方法来更新界面
235
+ * @param eventMessage 接收到的一条 Event Message
236
+ * @param prev 上一次增量更新后的 AssistantMessage 对象
237
+ * @param messageId 当前正在更新的消息 ID,用于调用 append*Block 方法
238
+ * @returns 返回更新后的 AssistantMessage 对象
239
+ */
240
+ reduceAssistantMessage<T = any, K = any>(eventMessage: T, prev: K, messageId: string): K;
241
+ /**
242
+ * 检查是否需要刷新 token
243
+ * 当发生异常时检查是否需要刷新 token。返回 true 表示需要刷新 token,返回 false 表示无需刷新 token。
244
+ * 该方法需要由子类继承并重写,以适配扣子、Dify 等 LLMOps 平台的接口。
245
+ * 注意:该方法是一个无状态无副作用的函数,不允许修改 state。
246
+ * @param status HTTP 状态码
247
+ * @param error 错误响应体
248
+ * @returns 返回是否需要刷新 token
249
+ */
250
+ shouldRefreshToken(status: number, error: any): boolean;
251
+ /**
252
+ * 终止会话
253
+ * 该方法需要由子类继承并重写,以适配扣子、Dify 等 LLMOps 平台的接口。
254
+ * 注意:该方法是一个无状态无副作用的函数,不允许修改 state。
255
+ * @param conversationId 要终止的会话 ID
256
+ * @returns 返回 Promise,成功时 resolve,失败时 reject
257
+ */
258
+ terminateConversation(conversationId: string): Promise<void>;
259
+ /**
260
+ * 获取历史会话列表
261
+ * 该方法需要由子类继承并重写,以适配扣子、Dify 等 LLMOps 平台的接口。
262
+ * 注意:该方法是一个无状态无副作用的函数,不允许修改 state。
263
+ * @param page 分页页码,默认为 1
264
+ * @param size 每页返回条数,默认为 10
265
+ * @returns 返回历史会话列表
266
+ */
267
+ getConversations(page?: number, size?: number): Promise<ConversationHistory[]>;
268
+ /**
269
+ * 获取指定会话 ID 的对话消息列表
270
+ * 该方法需要由子类继承并重写,以适配扣子、Dify 等 LLMOps 平台的接口。
271
+ * 如果对话消息是 AI 助手消息,则需要调用 reduceAssistantMessage() 解析消息。
272
+ * 注意:该方法是一个无状态无副作用的函数,不允许修改 state。
273
+ * @param conversationId 会话 ID
274
+ * @returns 返回对话消息列表
275
+ */
276
+ getConversationMessages(conversationId: string): Promise<ChatMessage[]>;
277
+ /**
278
+ * 删除指定 ID 的会话
279
+ * 该方法需要由子类继承并重写,以适配扣子、Dify 等 LLMOps 平台的接口。
280
+ * 注意:该方法是一个无状态无副作用的函数,不允许修改 state。
281
+ * @param conversationID 会话 ID
282
+ * @returns 返回 Promise,成功时 resolve,失败时 reject
283
+ */
284
+ deleteConversation(conversationID: string): Promise<void>;
285
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Mixin 工具函数
3
+ * 根据 TypeScript 官方文档实现的 mixin 模式
4
+ * @see https://www.typescriptlang.org/docs/handbook/mixins.html
5
+ */
6
+ /**
7
+ * 构造函数类型
8
+ * 表示一个可以被 new 调用的类型
9
+ */
10
+ export type Constructor<T = {}> = new (...args: any[]) => T;
11
+ /**
12
+ * 应用 mixins 到目标类
13
+ * 将多个 mixin 类的属性和方法复制到目标类的原型上
14
+ *
15
+ * @param derivedCtor 目标类的构造函数
16
+ * @param constructors 要混入的类的构造函数数组
17
+ */
18
+ export declare function applyMixins(derivedCtor: any, constructors: any[]): void;
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@kweaver-ai/chatkit",
3
+ "version": "0.1.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "type": "module",
8
+ "description": "AI 对话组件 - ChatKit",
9
+ "main": "dist/chatkit.cjs.js",
10
+ "module": "dist/chatkit.es.js",
11
+ "types": "dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/chatkit.es.js",
16
+ "require": "./dist/chatkit.cjs.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "scripts": {
23
+ "dev": "vite",
24
+ "build": "npm run build:lib && npm run build:types",
25
+ "build:lib": "vite build --config vite.config.lib.ts",
26
+ "build:types": "tsc --project tsconfig.lib.json",
27
+ "preview": "vite preview"
28
+ },
29
+ "keywords": [
30
+ "chat",
31
+ "ai",
32
+ "react",
33
+ "component"
34
+ ],
35
+ "author": "AISHU",
36
+ "license": "MIT",
37
+ "peerDependencies": {
38
+ "react": "^18.0.0 || ^19.0.0",
39
+ "react-dom": "^18.0.0 || ^19.0.0"
40
+ },
41
+ "dependencies": {
42
+ "echarts": "^6.0.0",
43
+ "react-markdown": "^10.1.0",
44
+ "rehype-highlight": "^7.0.2",
45
+ "rehype-katex": "^7.0.1",
46
+ "rehype-raw": "^7.0.0",
47
+ "remark-breaks": "^4.0.0",
48
+ "remark-gfm": "^4.0.1",
49
+ "remark-math": "^6.0.0",
50
+ "sql-formatter-plush": "^1.1.3"
51
+ },
52
+ "devDependencies": {
53
+ "@types/node": "^22.10.2",
54
+ "@types/react": "^18.3.18 || ^19.0.0",
55
+ "@types/react-dom": "^18.3.5 || ^19.0.0",
56
+ "@vitejs/plugin-react": "^4.3.4",
57
+ "autoprefixer": "^10.4.20",
58
+ "postcss": "^8.4.49",
59
+ "react": "^18.3.1",
60
+ "react-dom": "^18.3.1",
61
+ "tailwindcss": "^3.4.17",
62
+ "typescript": "^5.7.2",
63
+ "vite": "^6.0.3",
64
+ "vite-plugin-css-injected-by-js": "^3.5.2"
65
+ }
66
+ }