@kernelift/ai-chat 2.1.1 → 2.1.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @kernelift/ai-chat
2
2
 
3
+ ## 2.1.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 修复create-record歧义问题
8
+
9
+ ## 2.1.2
10
+
11
+ ### Patch Changes
12
+
13
+ - fix auto scroll problem
14
+
3
15
  ## 2.1.1
4
16
 
5
17
  ### Patch Changes
package/README.md CHANGED
@@ -142,7 +142,6 @@ ChatContainer (主容器)
142
142
  :input-height="140"
143
143
  @send="handleSend"
144
144
  @bubble-event="handleBubbleEvent"
145
- @create-record="handleCreateRecord"
146
145
  @change-record="handleChangeRecord"
147
146
  @change-theme="handleThemeChange"
148
147
  >
@@ -373,20 +372,39 @@ const handleBubbleEvent = (eventName: BubbleEvent, message: ChatMessage) => {
373
372
  ### 记录管理事件
374
373
 
375
374
  ```typescript
376
- // 创建新记录
377
- const handleCreateRecord = (messages: ChatMessage[]) => {
378
- const newRecord: ChatRecord = {
379
- id: generateId(),
380
- name: messages[0]?.content.slice(0, 20) + '...' || '新对话',
381
- content: messages[0]?.content || '',
382
- type: 'text',
383
- createTime: new Date().toLocaleDateString(),
384
- userId: 'current-user',
385
- extraData: { messages }
386
- };
375
+ // 发送消息(组件会自动传递 needCreateRecord 标志)
376
+ const handleSend = (
377
+ text: string,
378
+ enableThink?: boolean,
379
+ enableNet?: boolean,
380
+ needCreateRecord?: boolean
381
+ ) => {
382
+ // 添加用户消息
383
+ messages.value.push({
384
+ id: Date.now().toString(),
385
+ role: 'user',
386
+ content: text,
387
+ timestamp: Date.now()
388
+ });
389
+
390
+ // 如果需要创建新记录(当前没有激活的记录)
391
+ if (needCreateRecord) {
392
+ const newRecord: ChatRecord = {
393
+ id: generateId(),
394
+ name: text.slice(0, 30) + (text.length > 30 ? '...' : ''),
395
+ content: text,
396
+ type: 'text',
397
+ createTime: new Date().toLocaleDateString(),
398
+ userId: 'current-user',
399
+ extraData: { messages: messages.value }
400
+ };
401
+
402
+ records.value.unshift(newRecord);
403
+ activeRecordId.value = newRecord.id;
404
+ }
387
405
 
388
- records.value.unshift(newRecord);
389
- activeRecordId.value = newRecord.id;
406
+ // 调用 AI API 处理响应
407
+ callAIAPI(text, enableThink, enableNet);
390
408
  };
391
409
 
392
410
  // 切换记录
@@ -660,7 +678,6 @@ const handleStreamResponse = async (question: string, enableThink?: boolean) =>
660
678
  :markdown-class-name="themeMode === 'dark' ? 'prose-invert' : 'prose'"
661
679
  @send="handleSend"
662
680
  @cancel="handleCancel"
663
- @create-record="handleCreateRecord"
664
681
  @change-record="handleChangeRecord"
665
682
  @bubble-event="handleBubbleEvent"
666
683
  @change-theme="(mode) => (themeMode = mode)"
@@ -725,8 +742,13 @@ const recordActions: ChatRecordAction[] = [
725
742
  }
726
743
  ];
727
744
 
728
- // 发送消息
729
- const handleSend = async (text: string, enableThink?: boolean) => {
745
+ // 发送消息(包含记录创建逻辑)
746
+ const handleSend = async (
747
+ text: string,
748
+ enableThink?: boolean,
749
+ enableNet?: boolean,
750
+ needCreateRecord?: boolean
751
+ ) => {
730
752
  inputText.value = '';
731
753
 
732
754
  // 添加用户消息
@@ -738,6 +760,22 @@ const handleSend = async (text: string, enableThink?: boolean) => {
738
760
  isThinking: enableThink
739
761
  });
740
762
 
763
+ // 如果需要创建新记录(当前没有激活的记录)
764
+ if (needCreateRecord) {
765
+ const newRecord: ChatRecord = {
766
+ id: Date.now().toString(),
767
+ name: text.slice(0, 30) + (text.length > 30 ? '...' : ''),
768
+ content: text,
769
+ type: 'text',
770
+ createTime: new Date().toLocaleDateString(),
771
+ userId: 'current-user',
772
+ extraData: { messages: messages.value }
773
+ };
774
+
775
+ records.value.unshift(newRecord);
776
+ activeRecordId.value = newRecord.id;
777
+ }
778
+
741
779
  senderLoading.value = true;
742
780
  generateLoading.value = true;
743
781
 
@@ -820,21 +858,8 @@ const handleBubbleEvent = (event: BubbleEvent, data: ChatMessage) => {
820
858
  }
821
859
  };
822
860
 
823
- // 创建记录
824
- const handleCreateRecord = (msgs: ChatMessage[]) => {
825
- const newRecord: ChatRecord = {
826
- id: Date.now().toString(),
827
- name: msgs[0]?.content.slice(0, 20) + '...' || '新对话',
828
- content: msgs[0]?.content || '',
829
- type: 'text',
830
- createTime: new Date().toLocaleDateString(),
831
- userId: 'current-user',
832
- extraData: { messages: msgs }
833
- };
834
-
835
- records.value.unshift(newRecord);
836
- activeRecordId.value = newRecord.id;
837
- };
861
+ // 注意:记录创建逻辑已集成到 handleSend 函数中
862
+ // needCreateRecord true 时,在发送消息的同时创建新记录
838
863
 
839
864
  // 切换记录
840
865
  const handleChangeRecord = (record?: ChatRecord) => {
@@ -1140,21 +1165,8 @@ onUnmounted(() => {
1140
1165
  controller.value.abort();
1141
1166
  });
1142
1167
 
1143
- function handleCreateRecord(messages: ChatMessage[]) {
1144
- const targetId = Date.now().toString();
1145
- demoRecords.value.push({
1146
- id: targetId,
1147
- name: '新记录',
1148
- content: messages[0]?.content || '',
1149
- type: 'text',
1150
- createTime: new Date(messages[0]?.timestamp || Date.now()).toLocaleDateString(),
1151
- userId: 'demo-user',
1152
- extraData: {
1153
- messages
1154
- }
1155
- });
1156
- activeRecordId.value = targetId;
1157
- }
1168
+ // 记录创建逻辑已整合到 handleSend
1169
+ // 通过 needCreateRecord 参数判断是否需要创建新记录
1158
1170
 
1159
1171
  function handleChangeRecord(record?: ChatRecord) {
1160
1172
  demoMessages.value = record?.extraData?.messages || [];
@@ -1227,7 +1239,6 @@ function handleScrollBottom() {
1227
1239
  :input-height="80"
1228
1240
  @send="handleSend"
1229
1241
  @cancel="handleCancel"
1230
- @create-record="handleCreateRecord"
1231
1242
  @close-workspace="showWorkspace = false"
1232
1243
  @change-record="handleChangeRecord"
1233
1244
  @bubble-event="handleBubbleEvent"
@@ -1393,20 +1404,19 @@ function handleScrollBottom() {
1393
1404
 
1394
1405
  ### Events 事件
1395
1406
 
1396
- | 事件名 | 参数 | 说明 |
1397
- | -------------------- | ------------------------------------------------------------ | -------------- |
1398
- | `send` | `(text: string, enableThink?: boolean, enableNet?: boolean)` | 发送消息 |
1399
- | `cancel` | - | 取消生成 |
1400
- | `clear` | - | 清空聊天 |
1401
- | `create-record` | `(messages: ChatMessage[])` | 创建记录 |
1402
- | `change-record` | `(record?: ChatRecord)` | 切换记录 |
1403
- | `change-collapse` | `(collapse: boolean)` | 折叠状态改变 |
1404
- | `change-theme` | `(theme: 'light' \| 'dark')` | 主题切换 |
1405
- | `change-aside-width` | `(width: number)` | 侧边栏宽度改变 |
1406
- | `click-logo` | - | 点击Logo |
1407
- | `bubble-event` | `(event: BubbleEvent, message: ChatMessage)` | 气泡交互事件 |
1408
- | `close-workspace` | - | 关闭工作区 |
1409
- | `scroll-bottom` | - | 滚动到底部 |
1407
+ | 事件名 | 参数 | 说明 |
1408
+ | -------------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------- |
1409
+ | `send` | `(text: string, enableThink?: boolean, enableNet?: boolean, needCreateRecord?: boolean)` | 发送消息,needCreateRecord 表示是否需要创建新记录 |
1410
+ | `cancel` | - | 取消生成 |
1411
+ | `clear` | - | 清空聊天 |
1412
+ | `change-record` | `(record?: ChatRecord)` | 切换记录 |
1413
+ | `change-collapse` | `(collapse: boolean)` | 折叠状态改变 |
1414
+ | `change-theme` | `(theme: 'light' \| 'dark')` | 主题切换 |
1415
+ | `change-aside-width` | `(width: number)` | 侧边栏宽度改变 |
1416
+ | `click-logo` | - | 点击Logo |
1417
+ | `bubble-event` | `(event: BubbleEvent, message: ChatMessage)` | 气泡交互事件 |
1418
+ | `close-workspace` | - | 关闭工作区 |
1419
+ | `scroll-bottom` | - | 滚动到底部 |
1410
1420
 
1411
1421
  ### Slots 插槽
1412
1422