Skip to content

Commit

Permalink
feat: better concat for generation that was continued
Browse files Browse the repository at this point in the history
Note: this approach doesn't account for the event when continuation starts mid-word (white space will be inserted anyway). However, statistically chance of this happening is low.
  • Loading branch information
codedealer committed Apr 7, 2024
1 parent b72fb04 commit feac32b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 9 additions & 0 deletions common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ export function trimSentence(text: string) {
return text.slice(0, last + 1)
}

export function concatenateSentence(text: string, next: string) {
if (!text || !next) return text + next
if (text.endsWith('\n') || next.startsWith('\n')) {
return `${text.trimEnd()}\n${next.trimStart()}`
}

return `${text.trimEnd()} ${next.trimStart()}`
}

export function slugify(str: string) {
return str
.toLowerCase()
Expand Down
9 changes: 6 additions & 3 deletions srv/api/chat/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { v4 } from 'uuid'
import { Response } from 'express'
import { publishMany } from '../ws/handle'
import { getScenarioEventType } from '/common/scenario'
import { concatenateSentence } from '/common/util'

type GenRequest = UnwrapBody<typeof genValidator>

Expand Down Expand Up @@ -266,7 +267,7 @@ export const generateMessageV2 = handle(async (req, res) => {
}

if ('partial' in gen) {
const prefix = body.kind === 'continue' ? `${body.continuing.msg}` : ''
const prefix = body.kind === 'continue' ? `${body.continuing.msg} ` : ''
sendMany(members, {
type: 'message-partial',
partial: `${prefix}${gen.partial}`,
Expand Down Expand Up @@ -325,7 +326,8 @@ export const generateMessageV2 = handle(async (req, res) => {
return
}

const responseText = body.kind === 'continue' ? `${body.continuing.msg}${generated}` : generated
const responseText =
body.kind === 'continue' ? concatenateSentence(body.continuing.msg, generated) : generated

const actions: AppSchema.ChatAction[] = []

Expand Down Expand Up @@ -555,7 +557,8 @@ async function handleGuestGenerate(body: GenRequest, req: AppRequest, res: Respo

if (error) return

const responseText = body.kind === 'continue' ? `${body.continuing.msg}${generated}` : generated
const responseText =
body.kind === 'continue' ? concatenateSentence(body.continuing.msg, generated) : generated

const characterId = body.kind === 'self' ? undefined : body.replyAs?._id || body.char?._id
const senderId = body.kind === 'self' ? 'anon' : undefined
Expand Down

0 comments on commit feac32b

Please sign in to comment.