Skip to content

Commit

Permalink
feat(core): add restart command and middleware for ChatLuna service
Browse files Browse the repository at this point in the history
  • Loading branch information
dingyi222666 committed Dec 28, 2024
1 parent 05f0149 commit 4c5cbad
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 4 deletions.
1 change: 0 additions & 1 deletion packages/core/src/chains/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ export async function getTemplateConversationRoom(

ctx.scope.parent.scope.update(config, true)


// throw new ChatLunaError(ChatLunaErrorCode.INIT_ROOM)
}

Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/commands/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ export function apply(ctx: Context, config: Config, chain: ChatChain) {
await chain.receiveCommand(session, 'wipe')
}
)

ctx.command('chatluna.restart').action(async ({ options, session }) => {
await chain.receiveCommand(session, 'restart')
})
}
5 changes: 5 additions & 0 deletions packages/core/src/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ commands:
incorrect_input: 'Incorrect input. Deletion cancelled.'
success: 'Data deleted. Restarting to apply changes.'

restart:
description: Restart the ChatLuna.
messages:
success: Successfully restarted ChatLuna.

auth:
description: ChatLuna authentication commands.
list:
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ commands:
incorrect_input: '你的输入不正确,已取消删除。'
success: '已删除相关数据,即将重启完成更改。'

restart:
description: 重启 ChatLuna 服务。
messages:
success: '已成功重启 ChatLuna 插件。'

auth:
description: ChatLuna 鉴权相关指令。
list:
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { apply as render_message } from './middlewares/render_message'
import { apply as request_model } from './middlewares/request_model'
import { apply as resolve_model } from './middlewares/resolve_model'
import { apply as resolve_room } from './middlewares/resolve_room'
import { apply as restart } from './middlewares/restart'
import { apply as rollback_chat } from './middlewares/rollback_chat'
import { apply as room_info } from './middlewares/room_info'
import { apply as room_permission } from './middlewares/room_permission'
Expand Down Expand Up @@ -111,6 +112,7 @@ export async function middleware(ctx: Context, config: Config) {
request_model,
resolve_model,
resolve_room,
restart,
rollback_chat,
room_info,
room_permission,
Expand Down
25 changes: 25 additions & 0 deletions packages/core/src/middlewares/restart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Context } from 'koishi'
import { Config } from '../config'
import { ChainMiddlewareRunStatus, ChatChain } from '../chains/chain'

export function apply(ctx: Context, config: Config, chain: ChatChain) {
chain
.middleware('restart', async (session, context) => {
const { command } = context

if (command !== 'restart') return ChainMiddlewareRunStatus.SKIPPED

ctx.scope.parent.scope.update(config, true)

context.message = session.text('.success')

return ChainMiddlewareRunStatus.STOP
})
.before('black_list')
}

declare module '../chains/chain' {
interface ChainMiddlewareName {
restart: never
}
}
1 change: 0 additions & 1 deletion packages/core/src/middlewares/set_default_embeddings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export function apply(ctx: Context, config: Config, chain: ChatChain) {

ctx.scope.parent.scope.update(config, true)


return ChainMiddlewareRunStatus.STOP
})
.after('lifecycle-handle_command')
Expand Down
3 changes: 1 addition & 2 deletions packages/long-memory/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { Context } from 'koishi'
import { Config } from 'koishi-plugin-chatluna-long-memory'
// import start
import { apply as config } from './plugins/config'
import { apply as memory } from './plugins/memory'
// import end
import { apply as memory } from './plugins/memory' // import end

export async function plugins(ctx: Context, parent: Config) {
type Plugin = (ctx: Context, config: Config) => PromiseLike<void> | void
Expand Down

0 comments on commit 4c5cbad

Please sign in to comment.