@kweaver-ai/chatkit 0.1.15 → 0.1.17

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.
@@ -18,6 +18,13 @@ export interface ChatKitBaseProps {
18
18
  token?: string;
19
19
  /** 刷新 token 的方法,由集成方传入 */
20
20
  refreshToken?: () => Promise<string>;
21
+ /**
22
+ * 初始用户问题。
23
+ *
24
+ * - 外层在挂载 Assistant / Copilot 等组件时传入
25
+ * - 当组件首次可见且当前没有任何消息时,会自动发送该问题并触发问答
26
+ */
27
+ initialQuestion?: string;
21
28
  }
22
29
  /**
23
30
  * ChatKitBase 组件的状态接口
@@ -56,6 +63,20 @@ export declare abstract class ChatKitBase<P extends ChatKitBaseProps = ChatKitBa
56
63
  */
57
64
  private isInitializing;
58
65
  private hasInitialized;
66
+ /**
67
+ * 会话代数:用于区分「旧会话的流式更新」和「新会话」
68
+ * 每次清空/新建会话时自增,流式处理只在代数未变化时才继续写入 state
69
+ */
70
+ private conversationSeq;
71
+ /**
72
+ * 当前流式请求的 AbortController
73
+ * 子类在发起流式 fetch 时应设置该字段,便于在 handleStop / 新建会话时主动中断连接
74
+ */
75
+ protected currentStreamController?: AbortController;
76
+ /**
77
+ * 是否已经根据 initialQuestion 触发过一次自动发送
78
+ */
79
+ private hasSentInitialQuestion;
59
80
  /**
60
81
  * 流式响应时是否处于「按 chunk 批处理」中,避免同一 chunk 内多行事件触发多次 setState 导致 Maximum update depth exceeded
61
82
  */
@@ -85,6 +106,16 @@ export declare abstract class ChatKitBase<P extends ChatKitBaseProps = ChatKitBa
85
106
  * 仅在组件首次可见时调用,防止重复初始化
86
107
  */
87
108
  private initializeConversation;
109
+ /**
110
+ * 根据 props.initialQuestion 在合适的时机自动触发一次问答
111
+ *
112
+ * 触发条件:
113
+ * - 组件当前处于可见状态(visible !== false)
114
+ * - props.initialQuestion 为非空字符串
115
+ * - 当前还没有任何对话消息(messages.length === 0)
116
+ * - 仅触发一次(hasSentInitialQuestion 为 false)
117
+ */
118
+ private trySendInitialQuestion;
88
119
  /**
89
120
  * 获取开场白和预置问题 (抽象方法,由子类实现)
90
121
  * 该方法需要由子类继承并重写,以适配扣子、Dify 等 LLMOps 平台的接口
@@ -227,6 +258,14 @@ export declare abstract class ChatKitBase<P extends ChatKitBaseProps = ChatKitBa
227
258
  * @param consumeTime 耗时(毫秒),可选
228
259
  */
229
260
  protected appendJson2PlotBlock(messageId: string, chartData: ChartDataSchema, consumeTime?: number): void;
261
+ /**
262
+ * 更新指定消息中最后一个 JSON2Plot 图表块的内容(用于流式工具 json2plot 的组装)
263
+ * 如果不存在 JSON2Plot 块,则在流式更新时创建一个新的图表块
264
+ * @param messageId 消息 ID
265
+ * @param chartData 最新的图表数据 Schema
266
+ * @param consumeTime 耗时(毫秒),可选
267
+ */
268
+ protected updateJson2PlotBlock(messageId: string, chartData: ChartDataSchema, consumeTime?: number): void;
230
269
  /**
231
270
  * 添加 AfSailor 工具类型的消息块
232
271
  * 该方法由子类调用,用于在消息中添加 AfSailor 查询结果
@@ -243,6 +282,14 @@ export declare abstract class ChatKitBase<P extends ChatKitBaseProps = ChatKitBa
243
282
  * @param consumeTime 耗时(毫秒),可选
244
283
  */
245
284
  protected appendDatasourceFilterBlock(messageId: string, result: DatasourceFilterResult, consumeTime?: number): void;
285
+ /**
286
+ * 更新指定消息中最后一个 DatasourceFilter 工具块的内容(用于流式工具 datasource_filter 的组装)
287
+ * 如果不存在 DatasourceFilter 块,则在流式更新时创建一个新的工具块
288
+ * @param messageId 消息 ID
289
+ * @param result 最新的 DatasourceFilterResult
290
+ * @param consumeTime 耗时(毫秒),可选
291
+ */
292
+ protected updateDatasourceFilterBlock(messageId: string, result: DatasourceFilterResult, consumeTime?: number): void;
246
293
  /**
247
294
  * 添加 DatasourceRerank 工具类型的消息块
248
295
  * 该方法由子类调用,用于在消息中添加 DatasourceRerank 查询结果,与 datasource_filter 处理方式一致
@@ -251,6 +298,14 @@ export declare abstract class ChatKitBase<P extends ChatKitBaseProps = ChatKitBa
251
298
  * @param consumeTime 耗时(毫秒),可选
252
299
  */
253
300
  protected appendDatasourceRerankBlock(messageId: string, result: DatasourceRerankResult, consumeTime?: number): void;
301
+ /**
302
+ * 更新指定消息中最后一个 DatasourceRerank 工具块的内容(用于流式工具 datasource_rerank 的组装)
303
+ * 如果不存在 DatasourceRerank 块,则在流式更新时创建一个新的工具块
304
+ * @param messageId 消息 ID
305
+ * @param result 最新的 DatasourceRerankResult
306
+ * @param consumeTime 耗时(毫秒),可选
307
+ */
308
+ protected updateDatasourceRerankBlock(messageId: string, result: DatasourceRerankResult, consumeTime?: number): void;
254
309
  /**
255
310
  * 添加默认工具类型的消息块
256
311
  * 用于那些没有单独渲染逻辑的通用工具
@@ -46,7 +46,7 @@ export declare abstract class AssistantBase<P extends AssistantBaseProps = Assis
46
46
  /**
47
47
  * 处理新建对话
48
48
  */
49
- handleNewChat: () => void;
49
+ handleNewChat: () => Promise<void>;
50
50
  /**
51
51
  * 处理加载指定会话
52
52
  */
@@ -0,0 +1,13 @@
1
+ import React from 'react';
2
+ interface HistoryChunkPlaceholderProps {
3
+ /** 此占位块包含的消息条数,用于展示提示文案 */
4
+ messageCount: number;
5
+ /** 点击展开该历史段 */
6
+ onExpand: () => void;
7
+ }
8
+ /**
9
+ * 历史段占位块
10
+ * - 用于折叠更早的历史记录,减少真实 DOM 数量
11
+ */
12
+ declare const HistoryChunkPlaceholder: React.FC<HistoryChunkPlaceholderProps>;
13
+ export default HistoryChunkPlaceholder;
@@ -8,8 +8,8 @@ interface InputAreaProps {
8
8
  value: string;
9
9
  /** 输入框值变化的回调 */
10
10
  onChange: (value: string) => void;
11
- /** 发送消息的回调 */
12
- onSend: () => void;
11
+ /** 发送消息的回调(支持异步) */
12
+ onSend: () => void | Promise<void>;
13
13
  /** 当前的应用上下文 */
14
14
  context?: ApplicationContext;
15
15
  /** 移除上下文的回调 */
@@ -0,0 +1,14 @@
1
+ import React from 'react';
2
+ interface MessageShellProps {
3
+ messageId: string;
4
+ onHeightChange?: (messageId: string, height: number) => void;
5
+ className?: string;
6
+ children: React.ReactNode;
7
+ }
8
+ /**
9
+ * MessageShell
10
+ * - 包裹单条消息,负责通过 ResizeObserver 上报真实高度
11
+ * - 不关心消息内容与交互本身
12
+ */
13
+ declare const MessageShell: React.FC<MessageShellProps>;
14
+ export default MessageShell;
@@ -0,0 +1,23 @@
1
+ import type React from 'react';
2
+ export interface ScrollAnchor {
3
+ messageId: string;
4
+ offset: number;
5
+ }
6
+ export interface UseAnchoredMessageListOptions {
7
+ /** 消息按从上到下的顺序排列,用于根据高度表计算偏移(仅包含当前真实渲染的消息 ID) */
8
+ getOrderedMessageIds: () => string[];
9
+ /** 是否启用锚点恢复(例如在不贴底时才启用) */
10
+ shouldUseAnchor?: () => boolean;
11
+ }
12
+ export interface UseAnchoredMessageListResult {
13
+ handleMessageHeightChange: (messageId: string, height: number) => void;
14
+ handleScroll: (e: React.UIEvent<HTMLDivElement>) => void;
15
+ registerScrollContainer: (el: HTMLDivElement | null) => void;
16
+ restoreScrollByAnchor: () => void;
17
+ }
18
+ /**
19
+ * 管理消息高度表与滚动锚点的 Hook
20
+ * - 使用 ResizeObserver 上报的高度增量,结合锚点信息,在高度变化或上方插入消息时做滚动修正
21
+ * - 内部使用 requestAnimationFrame 合并一帧内的多次高度变更,避免频繁 reflow
22
+ */
23
+ export declare function useAnchoredMessageList(options: UseAnchoredMessageListOptions): UseAnchoredMessageListResult;
@@ -189,6 +189,14 @@ export declare function DIPBaseMixin<TBase extends Constructor>(Base: TBase): {
189
189
  getWhitelistEntry(action: string, jsonPath: string): {
190
190
  postProcess?: (assistantMessage: AssistantMessage, content: any, messageId: string) => void;
191
191
  } | null;
192
+ /**
193
+ * 在流式结束后,将完整的 AssistantMessage 结构一次性应用到指定 ChatMessage:
194
+ * - 遍历 middle_answer.progress,按顺序调用 appendSkillOrLLMContentToMessage 组装所有工具和 LLM 内容块
195
+ * - 基于 message.ext 同步 messageContext(相关问题、耗时、Token 等),与历史会话逻辑保持一致
196
+ *
197
+ * 作为受保护方法暴露,便于子类在特殊场景下自定义流式结果的应用逻辑。
198
+ */
199
+ applyAssistantMessageFromStream(assistantMessage: AssistantMessage, messageId: string): void;
192
200
  /**
193
201
  * 从 Progress 对象中提取 Web 搜索查询
194
202
  * 根据 OpenAPI 规范,搜索数据在 answer.choices[0].message.tool_calls 中
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kweaver-ai/chatkit",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },