-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e89d236
commit 9b45bc2
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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('set_preset', async (session, context) => { | ||
const { command } = context | ||
|
||
if (command !== 'set_preset') { | ||
return ChainMiddlewareRunStatus.SKIPPED | ||
} | ||
|
||
const presetName = context.options.setPreset | ||
|
||
const presetService = ctx.chathub.preset | ||
|
||
const preset = await presetService.getPreset(presetName) | ||
|
||
if (preset.messages.length > 1) { | ||
await context.send( | ||
`不支持修改 ${presetName} 预设!该预设自定义了多条消息,属于复杂预设,无法使用此命令修改,请自行前往控制面板里的资源管理器编辑预设文件。` | ||
) | ||
|
||
return ChainMiddlewareRunStatus.STOP | ||
} | ||
|
||
await context.send('请发送你的预设内容。') | ||
|
||
const result = await session.prompt(1000 * 30) | ||
|
||
if (!result) { | ||
context.message = `添加预设超时,已取消添加预设: ${presetName}` | ||
return ChainMiddlewareRunStatus.STOP | ||
} | ||
|
||
const presetObject = load(preset.rawText) as RawPreset | ||
|
||
presetObject.prompts[0].content = result | ||
|
||
await fs.writeFile(preset.path, dump(presetObject)) | ||
|
||
context.message = `预设修改成功,预设名称为: ${presetName}。 请调用预设列表命令查看。` | ||
|
||
return ChainMiddlewareRunStatus.STOP | ||
}) | ||
.after('lifecycle-handle_command') | ||
} | ||
|
||
declare module '../chains/chain' { | ||
interface ChainMiddlewareName { | ||
set_preset: string | ||
} | ||
|
||
interface ChainMiddlewareContextOptions { | ||
setPreset?: string | ||
} | ||
} |