1
1
import { AppSchema } from './types/schema'
2
- import { tokenize } from './tokenize'
3
2
import { BOT_REPLACE , SELF_REPLACE } from './prompt'
4
3
5
4
export type ImagePromptOpts = {
@@ -21,7 +20,7 @@ export async function createAppearancePrompt(
21
20
22
21
const prefix = ''
23
22
const max = getMaxImageContext ( user )
24
- let size = await tokenize ( prefix )
23
+ let size = prefix . length
25
24
26
25
const { persona } = avatar
27
26
@@ -37,7 +36,7 @@ export async function createAppearancePrompt(
37
36
if ( ! value ) continue
38
37
39
38
for ( const visual of value ) {
40
- size += await tokenize ( visual )
39
+ size += visual . length
41
40
if ( size > max ) break
42
41
visuals . push ( visual )
43
42
}
@@ -71,7 +70,7 @@ export async function createImagePrompt(opts: ImagePromptOpts) {
71
70
72
71
for ( const index of indexes . reverse ( ) ) {
73
72
const line = msg . slice ( index )
74
- const size = await tokenize ( line )
73
+ const size = line . length
75
74
tokens += size
76
75
77
76
if ( tokens > maxTokens ) {
@@ -85,7 +84,7 @@ export async function createImagePrompt(opts: ImagePromptOpts) {
85
84
if ( tokens > maxTokens ) break
86
85
87
86
const handle = userId ? opts . members . find ( ( pr ) => pr . userId === userId ) ?. handle : opts . char . name
88
- tokens += await tokenize ( handle + ':' )
87
+ tokens += ( handle || '' ) . length
89
88
90
89
if ( tokens > maxTokens ) {
91
90
lines . push ( last . trim ( ) )
@@ -135,3 +134,13 @@ function tokenizeMessage(line: string) {
135
134
136
135
return matches
137
136
}
137
+
138
+ export function fixImagePrompt ( prompt : string ) {
139
+ return prompt
140
+ . replace ( / + / g, ' ' )
141
+ . replace ( / , + / g, ',' )
142
+ . split ( ',' )
143
+ . filter ( ( t ) => ! ! t . trim ( ) )
144
+ . map ( ( t ) => t . trim ( ) )
145
+ . join ( ', ' )
146
+ }
0 commit comments