From 8bc963bc3efa2180294a16a6bb95c2f126649376 Mon Sep 17 00:00:00 2001 From: PaienNate <68044286+PaienNate@users.noreply.github.com> Date: Sun, 12 Jan 2025 13:56:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=E5=B8=AE=E5=8A=A9?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E6=97=A0=E6=B3=95=E5=8A=A0=E8=BD=BD=E6=8C=87?= =?UTF-8?q?=E4=BB=A4=E7=9B=B8=E5=85=B3=E5=B8=AE=E5=8A=A9=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=E5=B9=B6=E8=A1=A5=E5=85=85?= =?UTF-8?q?=E9=83=A8=E5=88=86=E6=97=A5=E5=BF=97=20(#1192)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/helpdoc.go | 1 - dice/builtin_commands.go | 2 - dice/dice_help.go | 85 ++++++++++++++++++++++++++++------------ dice/dice_manager.go | 11 +++--- dice/docengine/bleve.go | 4 +- 5 files changed, 67 insertions(+), 36 deletions(-) diff --git a/api/helpdoc.go b/api/helpdoc.go index df4bd9eb..4838b83e 100644 --- a/api/helpdoc.go +++ b/api/helpdoc.go @@ -47,7 +47,6 @@ func helpDocReload(c echo.Context) error { dm.Help.Close() dm.InitHelp() - dm.AddHelpWithDice(dm.Dice[0]) return Success(&c, Response{}) } return Error(&c, "帮助文档正在重新装载", Response{}) diff --git a/dice/builtin_commands.go b/dice/builtin_commands.go index e1199ef7..06379c7f 100644 --- a/dice/builtin_commands.go +++ b/dice/builtin_commands.go @@ -442,7 +442,6 @@ func (d *Dice) registerCoreCommands() { dm.Help.Close() dm.InitHelp() - dm.AddHelpWithDice(dm.Dice[0]) ReplyToSender(ctx, msg, "帮助文档已经重新装载") } else { ReplyToSender(ctx, msg, "帮助文档正在重新装载,请稍后...") @@ -1122,7 +1121,6 @@ func (d *Dice) registerCoreCommands() { dm.IsHelpReloading = true dm.Help.Close() dm.InitHelp() - dm.AddHelpWithDice(dice) ReplyToSender(ctx, msg, "帮助文档已重载") } else { ReplyToSender(ctx, msg, "帮助文档正在重新装载") diff --git a/dice/dice_help.go b/dice/dice_help.go index a26378b0..a80c0ee8 100644 --- a/dice/dice_help.go +++ b/dice/dice_help.go @@ -57,7 +57,6 @@ func (e HelpTextItems) Len() int { type HelpManager struct { CurID uint64 - Parent *DiceManager EngineType EngineType LoadingFn string HelpDocTree []*HelpDoc @@ -122,7 +121,7 @@ func (m *HelpManager) Close() { _ = os.RemoveAll("./_help_cache") } -func (m *HelpManager) Load() { +func (m *HelpManager) Load(internalCmdMap CmdMapCls, extList []*ExtInfo) { m.loadSearchEngine() _ = m.AddItem(docengine.HelpTextItem{ @@ -184,7 +183,7 @@ func (m *HelpManager) Load() { totalEntries := len(entries) for i, entry := range entries { progress := float64(i+1) / float64(totalEntries) * 100 - log.Infof("处理帮助文档组[文件夹]: 当前帮助文档加载总进度: %s %.2f%% (%d/%d)", entry.Name(), progress, i+1, totalEntries) + log.Infof("[帮助文档] 处理用户定义帮助文档组[文件夹]: 当前帮助文档加载进度: %s %.2f%% (%d/%d)", entry.Name(), progress, i+1, totalEntries) if strings.HasPrefix(entry.Name(), ".") { continue } @@ -219,7 +218,29 @@ func (m *HelpManager) Load() { }) m.HelpDocTree = append(m.HelpDocTree, &child) } - _ = m.AddItemApply(true) + err = m.AddItemApply(false) + if err != nil { + log.Errorf("加载用户自定义帮助文档出现异常!: %v", err) + } + log.Infof("[帮助文档] 用户定义的帮助文档组已加载完成!") + log.Infof("[帮助文档] 正在处理指令相关(含插件)帮助文档组") + err = m.addInternalCmdHelp(internalCmdMap) + if err != nil { + log.Errorf("加载内置指令帮助文档出现异常: %v", err) + } + err = m.AddItemApply(false) + if err != nil { + log.Errorf("加载内置指令帮助文档出现异常: %v", err) + } + err = m.addExternalCmdHelp(extList) + if err != nil { + log.Errorf("加载插件指令帮助文档出现异常: %v", err) + } + err = m.AddItemApply(true) + if err != nil { + log.Errorf("加载插件指令帮助文档出现异常: %v", err) + } + log.Infof("[帮助文档] 指令相关(含插件)帮助文档组已加载完成!") m.CurID = m.searchEngine.GetTotalID() elapsed := time.Since(start) // 计算执行时间 log.Infof("帮助文档加载完毕,共耗费时间: %s 共计加载条目:%d\n", elapsed, m.CurID) @@ -409,35 +430,51 @@ out: return synonymCount, nil } -func (dm *DiceManager) AddHelpWithDice(dice *Dice) { - m := dm.Help - - addCmdMap := func(packageName string, cmdMap CmdMapCls) { - for k, v := range cmdMap { - content := v.Help - if content == "" { - content = v.ShortHelp - } - _ = m.AddItem(docengine.HelpTextItem{ - Group: HelpBuiltinGroup, - Title: k, - Content: content, - PackageName: packageName, - }) +func (m *HelpManager) addCmdMap(packageName string, cmdMap CmdMapCls) error { + for k, v := range cmdMap { + content := v.Help + if content == "" { + content = v.ShortHelp } + err := m.AddItem(docengine.HelpTextItem{ + Group: HelpBuiltinGroup, + Title: k, + Content: content, + PackageName: packageName, + }) + if err != nil { + log.Errorf("AddCmdMapItem err:%v", err) + return err + } + } + return nil +} + +func (m *HelpManager) addInternalCmdHelp(cmdMap CmdMapCls) error { + err := m.addCmdMap("核心指令", cmdMap) + if err != nil { + return err } + return nil +} - addCmdMap("核心指令", dice.CmdMap) - for _, i := range dice.ExtList { - _ = m.AddItem(docengine.HelpTextItem{ +func (m *HelpManager) addExternalCmdHelp(ext []*ExtInfo) error { + for _, i := range ext { + err := m.AddItem(docengine.HelpTextItem{ Group: HelpBuiltinGroup, Title: i.Name, Content: i.GetDescText(i), PackageName: "扩展模块", }) - addCmdMap(i.Name, i.CmdMap) + if err != nil { + return err + } + err = m.addCmdMap(i.Name, i.CmdMap) + if err != nil { + return err + } } - _ = m.AddItemApply(false) + return nil } func (m *HelpManager) AddItem(item docengine.HelpTextItem) error { diff --git a/dice/dice_manager.go b/dice/dice_manager.go index 3d0704de..b5dc0b85 100644 --- a/dice/dice_manager.go +++ b/dice/dice_manager.go @@ -112,9 +112,12 @@ func (dm *DiceManager) InitHelp() { dm.IsHelpReloading = true _ = os.MkdirAll("./data/helpdoc", 0755) dm.Help = new(HelpManager) - dm.Help.Parent = dm dm.Help.EngineType = EngineType(dm.HelpDocEngineType) - dm.Help.Load() + if len(dm.Dice) == 0 { + log.Fatalf("Dice实例不存在!") + return + } + dm.Help.Load(dm.Dice[0].CmdMap, dm.Dice[0].ExtList) dm.IsHelpReloading = false } @@ -255,12 +258,8 @@ func (dm *DiceManager) InitDice() { } } }() - // 加载帮助 dm.InitHelp() - if len(dm.Dice) >= 1 { - dm.AddHelpWithDice(dm.Dice[0]) - } }() dm.ResetAutoBackup() diff --git a/dice/docengine/bleve.go b/dice/docengine/bleve.go index 4692989d..492f9f0c 100644 --- a/dice/docengine/bleve.go +++ b/dice/docengine/bleve.go @@ -120,7 +120,6 @@ func (d *BleveSearchEngine) AddItem(item HelpTextItem) (string, error) { // AddItemApply 这里认为是真正执行插入文档的逻辑 // 由于现在已经将执行函数改为了可按文件执行,所以可以按文件进行Apply,这应当不会有太大的量级。 // end代表是否是最后一次执行,一般用在所有的数据都处理完之后,关闭逻辑的时候使用,如bleve batch重复利用后最后销毁 -// TODO: 似乎很奇怪,这家伙貌似不会回收内存的吗? func (d *BleveSearchEngine) AddItemApply(end bool) error { if d.batch != nil { // 执行batch @@ -130,11 +129,10 @@ func (d *BleveSearchEngine) AddItemApply(end bool) error { } // 如果是最后一批 if end { - // 销毁batch d.batch.Reset() d.batch = nil } else { - // 否则重置batch + // 否则仅重置batch d.batch.Reset() } return err