Skip to content

Commit

Permalink
feat(core): add clone preset
Browse files Browse the repository at this point in the history
  • Loading branch information
dingyi222666 committed Sep 23, 2023
1 parent 02c35f5 commit e89d236
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 11 deletions.
15 changes: 15 additions & 0 deletions packages/core/src/commands/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ export function apply(ctx: Context, config: Config, chain: ChatChain) {
}
)

ctx.command(
'chathub.preset.clone <originPreset:string> [newPresetName:string]',
'克隆预设',
{
authority: 3
}
).action(async ({ session }, preset, newPreset) => {
await chain.receiveCommand(session, 'clone_preset', {
clonePreset: {
name: preset,
newName: newPreset ?? preset + '(1)'
}
})
})

ctx.command('chathub.preset.delete <preset:string>', '删除一个预设', {
authority: 3
}).action(async ({ session }, preset) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/llm-core/prompt/preset_prompt_parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export function formatPresetTemplateString(
})
}

interface RawPreset {
export interface RawPreset {
keywords: string[]
prompts: {
role: 'user' | 'system' | 'assistant'
Expand Down
74 changes: 74 additions & 0 deletions packages/core/src/middlewares/clone_preset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Context } from 'koishi'
import { Config } from '../config'
import { ChainMiddlewareRunStatus, ChatChain } from '../chains/chain'
import fs from 'fs/promises'
import { dump, load } from 'js-yaml'
import { RawPreset } from '../llm-core/prompt'

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

if (command !== 'clone_preset') {
return ChainMiddlewareRunStatus.SKIPPED
}

const { newName, name } = context.options.clonePreset

const presetService = ctx.chathub.preset

const oldPreset = await presetService.getPreset(name)

try {
await presetService.getPreset(newName)

await context.send(
'该预设关键词已经和其他预设关键词冲突,请更换其他关键词重试哦'
)

return ChainMiddlewareRunStatus.STOP
} catch (e) {}

await context.send(
`你确定要克隆预设 ${name} 吗?如果你确定要克隆,请输入 Y 来确认。`
)

const result = await session.prompt(1000 * 30)

if (result == null) {
context.message = '操作超时未确认,已自动取消。'
return ChainMiddlewareRunStatus.STOP
} else if (result !== 'Y') {
context.message = '已为你取消操作。'
return ChainMiddlewareRunStatus.STOP
}

const loaded = load(oldPreset.rawText) as RawPreset

loaded.keywords.push(newName)

await fs.writeFile(
presetService.resolvePresetDir() + `/${newName}_clone.yml`,
dump(loaded)
)

context.message = `预设克隆成功,预设名称为: ${newName}。 请调用预设列表命令查看。`

return ChainMiddlewareRunStatus.STOP
})
.after('lifecycle-handle_command')
}

declare module '../chains/chain' {
interface ChainMiddlewareName {
clone_preset: string
}

interface ChainMiddlewareContextOptions {
clonePreset?: {
name: string
newName: string
}
}
}
4 changes: 3 additions & 1 deletion packages/core/src/middlewares/delete_preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function apply(ctx: Context, config: Config, chain: ChatChain) {
return ChainMiddlewareRunStatus.STOP
}
} catch (e) {
logger.error(e)
await context.send(
'找不到该预设!请检查你是否输入了正确的预设?'
)
Expand Down Expand Up @@ -69,9 +70,10 @@ export function apply(ctx: Context, config: Config, chain: ChatChain) {

for (const room of roomList) {
room.preset = defaultPreset.triggerKeyword[0]
await ctx.database.upsert('chathub_room', [room])
}

await ctx.database.upsert('chathub_room', roomList)

context.message = `已删除预设: ${presetName},即将自动重启完成更改。`

ctx.scope.update(config, true)
Expand Down
9 changes: 0 additions & 9 deletions packages/core/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,6 @@ export class Preset {
await this.loadAllPreset()
}

/* const cached = await this.cache.get('default-preset')
if (cached) {
try {
return this.getPreset(cached)
} catch {
logger.warn(`default preset ${cached} not found, reset default preset`)
}
} */

const preset = this._presets.find((preset) =>
preset.triggerKeyword.includes('chatgpt')
)
Expand Down

0 comments on commit e89d236

Please sign in to comment.