Skip to content

Commit

Permalink
fix(core): fix appel issues, close #23
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 26, 2023
1 parent 5cfd229 commit f2bcb73
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export const schema: Schema<Config> = Schema.intersect([
])

export const name = 'dialogue'
export const using = ['database'] as const
export const using = ['database']

export function apply(ctx: Context, config: Config) {
ctx.i18n.define('zh-CN', require('./locales/zh-CN'))
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default function apply(ctx: Context, config: Dialogue.Config) {
}
const { original, parsed, appellative } = options.regexp
? { original: segment.unescape(question), parsed: question, appellative: false }
: ctx.dialogue.stripQuestion(question)
: ctx.root.dialogue.stripQuestion(question)
defineProperty(options, 'appellative', appellative)
defineProperty(options, 'original', original)
args[0] = parsed
Expand Down Expand Up @@ -87,7 +87,7 @@ export default function apply(ctx: Context, config: Dialogue.Config) {
sess.guild = session.guild
sess.argv = session.argv
return sess.withScope('commands.teach.messages', () => {
return session.argv.options.target ? analyze(session) : create(session)
return session.argv.options.target ? analyze(sess) : create(sess)
})
}

Expand Down Expand Up @@ -172,9 +172,10 @@ export default function apply(ctx: Context, config: Dialogue.Config) {

ctx.before('dialogue/modify', async (session) => {
const { args } = session.argv
if (!args[1] || !ctx.assets) return
const assets = ctx.get('assets')
if (!args[1] || !assets) return
try {
args[1] = await ctx.assets.transform(args[1])
args[1] = await assets.transform(args[1])
} catch (error) {
ctx.logger('teach').warn(error)
return session.text('.upload-failed')
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ export async function triggerDialogue(ctx: Context, session: Session, next: Next

if (ctx.bail('dialogue/receive', state)) return next()
const logger = ctx.logger('dialogue')
logger.debug('[receive]', session.messageId, session.content)
logger.debug('[receive]', session.content)

// fetch matched dialogues
const dialogues = state.dialogues = await ctx.dialogue.get(state.test)
const dialogues = state.dialogues = await ctx.root.dialogue.get(state.test)

// pick dialogue
let dialogue: Dialogue
Expand Down Expand Up @@ -271,11 +271,12 @@ export default function receiver(ctx: Context, config: Dialogue.Config) {

ctx.on('dialogue/receive', ({ session, test }) => {
if (session.content.includes('<image ')) return true
const { original, parsed, appellative, activated } = ctx.dialogue.stripQuestion(session.content)
const { appel, content } = session.stripped
const { original, parsed, appellative, activated } = ctx.root.dialogue.stripQuestion(content)
test.question = parsed
test.original = original
test.activated = activated
test.appellative = appellative
test.appellative = appellative || appel
})

// predict the user fields involved
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ declare module '.' {

export default function apply(ctx: Context) {
ctx.command('dialogue.stats').action(async ({ session }) => {
const stats = await ctx.dialogue.stats()
const stats = await ctx.root.dialogue.stats()
return session.text('.output', stats)
})

Expand Down Expand Up @@ -72,10 +72,10 @@ export default function apply(ctx: Context) {
const { answer } = dialogue
// TODO extract dialogue command
if (!answer.startsWith('%{dialogue ')) continue
const { original, parsed } = ctx.dialogue.stripQuestion(answer.slice(11, -1).trimStart())
const { original, parsed } = ctx.root.dialogue.stripQuestion(answer.slice(11, -1).trimStart())
if (parsed in options.questionMap) continue
// TODO multiple tests in one query
const dialogues = options.questionMap[parsed] = await ctx.dialogue.get({
const dialogues = options.questionMap[parsed] = await ctx.root.dialogue.get({
...test,
regexp: null,
question: parsed,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export async function analyze(session: Dialogue.Session) {
await app.serial('dialogue/after-modify', session)
}

return sendResult(session)
return await sendResult(session)
})
}

Expand Down
5 changes: 4 additions & 1 deletion packages/core/tests/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { App } from 'koishi'
import { App, Logger } from 'koishi'
import * as dialogue from 'koishi-plugin-dialogue'
import memory from '@koishijs/plugin-database-memory'
import mock from '@koishijs/plugin-mock'

export default function (config: dialogue.Config = {}) {
const logger = new Logger('dialogue')
const app = new App({
nickname: ['koishi', 'satori'],
})
Expand Down Expand Up @@ -31,6 +32,7 @@ export default function (config: dialogue.Config = {}) {

async function start() {
if (app.events.isActive) return
logger.level = 3
await app.start()
await app.mock.initUser(u2id, 2)
await app.mock.initUser(u3id, 3)
Expand All @@ -43,6 +45,7 @@ export default function (config: dialogue.Config = {}) {
if (!app.events.isActive) return
await app.database.dropAll()
await app.stop()
logger.level = 2
}

before(start)
Expand Down
12 changes: 6 additions & 6 deletions packages/core/tests/receiver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import createEnvironment from '.'
import * as jest from 'jest-mock'

describe('koishi-plugin-dialogue (appellative)', () => {
const { u3g1 } = createEnvironment({})
const { app, u3g1 } = createEnvironment({})

let clock: InstalledClock
const randomReal = jest.spyOn(Random, 'real')
Expand All @@ -27,8 +27,7 @@ describe('koishi-plugin-dialogue (appellative)', () => {
await u3g1.shouldReply('koishi, f o o', 'bar')
// should strip punctuations
await u3g1.shouldReply('satori, foo?!', 'bar')
// TODO support at-trigger
// await u3g1.shouldReply(`<at id="${app.selfId}"/> foo`, 'bar')
await u3g1.shouldReply(`<at id="${app.mock.bots[0].selfId}"/> foo`, 'bar')
await u3g1.shouldReply('#1', '问答 1 的详细信息:\n问题:koishi,foo\n回答:bar\n触发权重:p=0, P=1')
await u3g1.shouldReply('## foo', '问题“foo”的回答如下:\n1. [p=0, P=1] bar')
})
Expand All @@ -50,6 +49,7 @@ describe('koishi-plugin-dialogue (appellative)', () => {
await u3g1.shouldReply('koishi, fooo', 'baz')
await u3g1.shouldReply('#4 -p 0.5 -P 1', '问答 4 已成功修改。')
await u3g1.shouldReply('koishi, fooo', 'baz')
await u3g1.shouldReply(`<at id="${app.mock.bots[0].selfId}"/> fooo`, 'baz')
})

it('unescape semgent (#309)', async () => {
Expand All @@ -64,11 +64,11 @@ describe('koishi-plugin-dialogue (interpolate)', () => {
const { app, u3g1, start, stop } = createEnvironment({})
app.command('bar').action(() => 'hello')
app.command('baz').action(async ({ session }) => {
await session.sendQueued('hello')
await session!.sendQueued('hello')
})
app.command('report [text]').action(async ({ session }, text) => {
await session.sendQueued(text)
await session.sendQueued('end')
await session!.sendQueued(text)
await session!.sendQueued('end')
})

await start()
Expand Down

0 comments on commit f2bcb73

Please sign in to comment.