koishi-plugin-chat-patch 1.0.18 → 1.1.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.
@@ -2,7 +2,7 @@ import { FileManager } from './file-manager'
2
2
  import { MessageHandler } from './message-handler'
3
3
  import { Context, h, Logger } from 'koishi'
4
4
  import { Config } from './config'
5
-
5
+ import { } from '@koishijs/plugin-console'
6
6
  import { URL, pathToFileURL } from 'node:url'
7
7
 
8
8
  export class ApiHandlers {
@@ -18,6 +18,17 @@ export class ApiHandlers {
18
18
  }
19
19
 
20
20
  registerApiHandlers() {
21
+ this.ctx.console.addListener('clear-all-indexeddb-data' as any, async () => {
22
+ try {
23
+ this.logInfo('收到清空 IndexedDB 数据请求')
24
+ // 这个 API 主要用于前端调用,后端不直接操作 IndexedDB
25
+ return { success: true, message: '可以清空 IndexedDB' }
26
+ } catch (error: any) {
27
+ this.logger.error('清空 IndexedDB 数据失败:', error)
28
+ return { success: false, error: error?.message || String(error) }
29
+ }
30
+ })
31
+
21
32
  // 获取所有聊天数据的 API
22
33
  this.ctx.console.addListener('get-chat-data' as any, async () => {
23
34
  try {
@@ -50,23 +61,39 @@ export class ApiHandlers {
50
61
  this.ctx.console.addListener('get-history-messages' as any, async (requestData: {
51
62
  selfId: string
52
63
  channelId: string
64
+ limit?: number
65
+ offset?: number
53
66
  }) => {
54
67
  try {
55
68
  const data = this.fileManager.readChatDataFromFile()
56
69
  const channelKey = `${requestData.selfId}:${requestData.channelId}`
57
- const messages = data.messages[channelKey] || []
58
-
59
- const sortedMessages = messages.sort((a, b) => a.timestamp - b.timestamp)
70
+ let messages = data.messages[channelKey] || []
71
+
72
+ // 按时间戳降序排列(最新的消息在前面)
73
+ const sortedMessages = messages.sort((a, b) => b.timestamp - a.timestamp)
74
+
75
+ // 如果提供了分页参数,则进行分页处理
76
+ if (requestData.limit !== undefined) {
77
+ const limit = requestData.limit
78
+ const offset = requestData.offset || 0
79
+ messages = sortedMessages.slice(offset, offset + limit)
80
+ // 重新按时间戳升序排列,以保持消息显示顺序
81
+ messages = messages.sort((a, b) => a.timestamp - b.timestamp)
82
+ } else {
83
+ // 默认返回所有消息,按时间戳升序排列
84
+ messages = sortedMessages.sort((a, b) => a.timestamp - b.timestamp)
85
+ }
60
86
 
61
- this.logInfo('获取历史消息:', channelKey, '共', sortedMessages.length, '条消息')
87
+ this.logInfo('获取历史消息:', channelKey, '共', messages.length, '条消息')
62
88
 
63
89
  return {
64
90
  success: true,
65
- messages: sortedMessages
91
+ messages: messages,
92
+ total: sortedMessages.length
66
93
  }
67
94
  } catch (error: any) {
68
95
  this.logger.error('获取历史消息失败:', error)
69
- return { success: false, error: error?.message || String(error), messages: [] }
96
+ return { success: false, error: error?.message || String(error), messages: [], total: 0 }
70
97
  }
71
98
  })
72
99
 
@@ -451,7 +478,8 @@ export class ApiHandlers {
451
478
  keepTempImages: this.config.keepTempImages,
452
479
  loggerinfo: this.config.loggerinfo,
453
480
  blockedPlatforms: this.config.blockedPlatforms || [],
454
- chatContainerHeight: this.config.chatContainerHeight
481
+ chatContainerHeight: this.config.chatContainerHeight,
482
+ clearIndexedDBOnStart: this.config.clearIndexedDBOnStart
455
483
  }
456
484
  }
457
485
  } catch (error: any) {
package/src/config.ts CHANGED
@@ -2,6 +2,7 @@ import { Schema } from 'koishi'
2
2
 
3
3
  export interface Config {
4
4
  loggerinfo: boolean
5
+ clearIndexedDBOnStart: boolean
5
6
  maxMessagesPerChannel: number
6
7
  keepMessagesOnClear: number
7
8
  keepTempImages: number
@@ -40,6 +41,10 @@ export const Config: Schema<Config> = Schema.intersect([
40
41
 
41
42
  Schema.object({
42
43
  chatContainerHeight: Schema.number().default(80).description('手机端使用的视口高度(防止文本输入框被挡住)').min(50).max(100),
43
- loggerinfo: Schema.boolean().default(false).description('日志调试模式').experimental(),
44
44
  }).description('进阶设置'),
45
+
46
+ Schema.object({
47
+ clearIndexedDBOnStart: Schema.boolean().default(true).description('启动时强制清空IndexedDB缓存(适用于紧急情况,防止浏览器卡死)'),
48
+ loggerinfo: Schema.boolean().default(false).description('日志调试模式').experimental(),
49
+ }).description('开发者选项'),
45
50
  ])
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- import { } from '@koishijs/plugin-console'
2
+ import { Console } from '@koishijs/console'
3
3
  import { Context } from 'koishi'
4
4
  import path from 'node:path'
5
5
 
@@ -16,6 +16,12 @@ export const inject = {
16
16
  required: ['console']
17
17
  }
18
18
 
19
+ declare module 'koishi' {
20
+ interface Context {
21
+ console: Console
22
+ }
23
+ }
24
+
19
25
  export const usage = `
20
26
 
21
27
  ---
@@ -3,6 +3,7 @@ import { Context, Session, h, Logger } from 'koishi'
3
3
  import { FileManager } from './file-manager'
4
4
  import { Config } from './config'
5
5
  import { Utils } from './utils'
6
+ import { } from '@koishijs/plugin-console'
6
7
 
7
8
  export class MessageHandler {
8
9
  private logger: Logger