|
|
@@ -222,7 +222,7 @@ export class GameService implements OnModuleInit {
|
|
|
this.eventsGateway.emitEvent(id, event)
|
|
|
}
|
|
|
|
|
|
- async genCharactor(background: string, num: number) {
|
|
|
+ async genCharactor(type: string, background: string, num: number) {
|
|
|
const parser = StructuredOutputParser.fromZodSchema(
|
|
|
z.array(
|
|
|
z.object({
|
|
|
@@ -237,7 +237,7 @@ export class GameService implements OnModuleInit {
|
|
|
)
|
|
|
const formatInstructions = parser.getFormatInstructions()
|
|
|
const prompt = new PromptTemplate({
|
|
|
- template: await this.promptService.getPromptByName(PromptName.GenCharacter),
|
|
|
+ template: await this.promptService.getPromptByName(PromptName.GenCharacter, type),
|
|
|
inputVariables: ['num', 'background'],
|
|
|
partialVariables: { format_instructions: formatInstructions }
|
|
|
})
|
|
|
@@ -350,7 +350,7 @@ export class GameService implements OnModuleInit {
|
|
|
gameState.plot = initGame.plot
|
|
|
} else {
|
|
|
const prompt = new PromptTemplate({
|
|
|
- template: await this.promptService.getPromptByName(PromptName.FirstPlot),
|
|
|
+ template: await this.promptService.getPromptByName(PromptName.FirstPlot, game.type),
|
|
|
inputVariables: ['background', 'charactors', 'datetime']
|
|
|
})
|
|
|
const input = await prompt.format({
|
|
|
@@ -414,7 +414,7 @@ export class GameService implements OnModuleInit {
|
|
|
|
|
|
let summary = this.formatSummary(history)
|
|
|
let prompt = new PromptTemplate({
|
|
|
- template: await this.promptService.getPromptByName(PromptName.NextPlot),
|
|
|
+ template: await this.promptService.getPromptByName(PromptName.NextPlot, game.type),
|
|
|
inputVariables: ['background', 'charactors', 'datetime', 'summary', 'choice', 'death', 'newCharactor']
|
|
|
})
|
|
|
const formatedCharactors = this.formatCharactors(charactors)
|
|
|
@@ -487,7 +487,10 @@ export class GameService implements OnModuleInit {
|
|
|
const delay = voteDelay > 0 ? setTimeout(voteDelay * 1000) : Promise.resolve()
|
|
|
|
|
|
options = (
|
|
|
- await Promise.all([this.createOptions(charactors, `${summary}\n\n${formatedDatatime}\n${plot}`), delay])
|
|
|
+ await Promise.all([
|
|
|
+ this.createOptions(game.type, charactors, `${summary}\n\n${formatedDatatime}\n${plot}`),
|
|
|
+ delay
|
|
|
+ ])
|
|
|
)[0]
|
|
|
this.send(`${id}`, {
|
|
|
type: 'options',
|
|
|
@@ -498,7 +501,7 @@ export class GameService implements OnModuleInit {
|
|
|
}
|
|
|
|
|
|
const [newSummary, nextChoice] = await Promise.all([
|
|
|
- this.refineSummary(history, this.formatDatetime(date, time) + '\n' + plot),
|
|
|
+ this.refineSummary(game.type, history, this.formatDatetime(date, time) + '\n' + plot),
|
|
|
willEnd
|
|
|
? Promise.resolve(null)
|
|
|
: this.makeVotes(id, options, new Date(), {
|
|
|
@@ -569,7 +572,7 @@ export class GameService implements OnModuleInit {
|
|
|
|
|
|
let summary = this.formatSummary(history)
|
|
|
let prompt = new PromptTemplate({
|
|
|
- template: await this.promptService.getPromptByName(PromptName.Ending),
|
|
|
+ template: await this.promptService.getPromptByName(PromptName.Ending, game.type),
|
|
|
inputVariables: ['background', 'summary']
|
|
|
})
|
|
|
const formatedDatatime = this.formatDatetime(date, time)
|
|
|
@@ -594,7 +597,7 @@ export class GameService implements OnModuleInit {
|
|
|
plot,
|
|
|
options: [],
|
|
|
charactors: [...lastState.charactors],
|
|
|
- summary: await this.refineSummary(history, this.formatDatetime(date, time) + '\n' + plot),
|
|
|
+ summary: await this.refineSummary(game.type, history, this.formatDatetime(date, time) + '\n' + plot),
|
|
|
ending: true
|
|
|
})
|
|
|
)
|
|
|
@@ -751,7 +754,7 @@ export class GameService implements OnModuleInit {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async createOptions(charactors: Charactor[], summary: string) {
|
|
|
+ async createOptions(type: string, charactors: Charactor[], summary: string) {
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
try {
|
|
|
if (i > 0) await setTimeout(1000)
|
|
|
@@ -770,7 +773,7 @@ export class GameService implements OnModuleInit {
|
|
|
)
|
|
|
const formatInstructions = parser.getFormatInstructions()
|
|
|
const prompt = new PromptTemplate({
|
|
|
- template: await this.promptService.getPromptByName(PromptName.GenChoice),
|
|
|
+ template: await this.promptService.getPromptByName(PromptName.GenChoice, type),
|
|
|
inputVariables: ['charactors', 'summary'],
|
|
|
partialVariables: { format_instructions: formatInstructions }
|
|
|
})
|
|
|
@@ -794,19 +797,16 @@ export class GameService implements OnModuleInit {
|
|
|
|
|
|
async createCharactorFromDanmu(id: number, date: Date) {
|
|
|
this.logger.log(`从弹幕中创建角色`)
|
|
|
- const danmu = await this.danmuService.pickCharactor(id, date)
|
|
|
- if (danmu) {
|
|
|
+ const game = await this.findById(id)
|
|
|
+ const c = await this.danmuService.pickCharactor(id, date)
|
|
|
+ if (c) {
|
|
|
try {
|
|
|
- const addCharactor = danmu.content.replace(/^我是/, '')
|
|
|
- const danmuUser = await this.danmuService.findDanmuUser(danmu.danmuUserId)
|
|
|
- if (danmuUser) {
|
|
|
- const charactor = await this.createNewCharactor(id, addCharactor)
|
|
|
- this.logger.log(`已创建角色: ${JSON.stringify(charactor)}`)
|
|
|
- charactor.name = danmuUser.name
|
|
|
- charactor.avatar = danmuUser.avatar
|
|
|
- charactor.danmuUserId = danmu.danmuUserId
|
|
|
- return charactor
|
|
|
- }
|
|
|
+ const charactor = await this.createNewCharactor(game.type, c.content, game.background)
|
|
|
+ this.logger.log(`已创建角色: ${JSON.stringify(charactor)}`)
|
|
|
+ charactor.name = c.name
|
|
|
+ charactor.avatar = c.avatar
|
|
|
+ charactor.danmuUserId = c.danmuUserId
|
|
|
+ return charactor
|
|
|
} catch (error) {
|
|
|
this.logger.error(error)
|
|
|
}
|
|
|
@@ -832,12 +832,12 @@ export class GameService implements OnModuleInit {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async refineSummary(history: GameState[], newPlot: string) {
|
|
|
+ async refineSummary(type: string, history: GameState[], newPlot: string) {
|
|
|
let i = history.map((i) => !!i.summary).lastIndexOf(true)
|
|
|
let input
|
|
|
if (i < 0) {
|
|
|
const prompt = new PromptTemplate({
|
|
|
- template: await this.promptService.getPromptByName(PromptName.Summarize),
|
|
|
+ template: await this.promptService.getPromptByName(PromptName.Summarize, type),
|
|
|
inputVariables: ['text']
|
|
|
})
|
|
|
input = await prompt.format({
|
|
|
@@ -848,7 +848,7 @@ export class GameService implements OnModuleInit {
|
|
|
})
|
|
|
} else {
|
|
|
const prompt = new PromptTemplate({
|
|
|
- template: await this.promptService.getPromptByName(PromptName.Refine),
|
|
|
+ template: await this.promptService.getPromptByName(PromptName.Refine, type),
|
|
|
inputVariables: ['existing_answer', 'text']
|
|
|
})
|
|
|
input = await prompt.format({
|
|
|
@@ -865,14 +865,10 @@ export class GameService implements OnModuleInit {
|
|
|
return response.content
|
|
|
}
|
|
|
|
|
|
- async createNewCharactor(gameId: number, base: string) {
|
|
|
+ async createNewCharactor(type: string, base: string, background: string) {
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
try {
|
|
|
if (i > 0) await setTimeout(1000)
|
|
|
- const game = await this.gameRepository.findOneBy({ id: gameId })
|
|
|
- if (!game) {
|
|
|
- throw new NotFoundException(`game #${gameId} not found`)
|
|
|
- }
|
|
|
const parser = StructuredOutputParser.fromZodSchema(
|
|
|
z.object({
|
|
|
name: z.string().describe('角色名称'),
|
|
|
@@ -885,13 +881,13 @@ export class GameService implements OnModuleInit {
|
|
|
)
|
|
|
const formatInstructions = parser.getFormatInstructions()
|
|
|
const prompt = new PromptTemplate({
|
|
|
- template: await this.promptService.getPromptByName(PromptName.NewCharacter),
|
|
|
+ template: await this.promptService.getPromptByName(PromptName.NewCharacter, type),
|
|
|
inputVariables: ['base', 'background'],
|
|
|
partialVariables: { format_instructions: formatInstructions }
|
|
|
})
|
|
|
const input = await prompt.format({
|
|
|
base,
|
|
|
- background: game.background
|
|
|
+ background
|
|
|
})
|
|
|
const response = await this.callLLM([
|
|
|
new SystemMessage('你是一个富有想象力的写作助手,你的任务是帮我想象一个小说里的角色。'),
|
|
|
@@ -906,7 +902,7 @@ export class GameService implements OnModuleInit {
|
|
|
throw new InternalServerErrorException('无法生成新角色')
|
|
|
}
|
|
|
|
|
|
- async modifyHp(charactors: Charactor[], history: GameState[], newPlot: string) {
|
|
|
+ async modifyHp(type: string, charactors: Charactor[], history: GameState[], newPlot: string) {
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
try {
|
|
|
if (i > 0) await setTimeout(1000)
|
|
|
@@ -920,7 +916,7 @@ export class GameService implements OnModuleInit {
|
|
|
)
|
|
|
const formatInstructions = parser.getFormatInstructions()
|
|
|
const prompt = new PromptTemplate({
|
|
|
- template: await this.promptService.getPromptByName(PromptName.ModifyHp),
|
|
|
+ template: await this.promptService.getPromptByName(PromptName.ModifyHp, type),
|
|
|
inputVariables: ['charactors', 'summary', 'text'],
|
|
|
partialVariables: { format_instructions: formatInstructions }
|
|
|
})
|