🌳
pt0/gamesapp/serverF/libF/aiTelePromptF.mts
7import * as _ from 'lodash-es'
11import type { GameStateJson } from "../gamesDbF.d.ts"
13const memPromptsA: {model: string; content: string}[] = []
15const isTooLong = (content: string) => {
16 const numWords = content.split(' ').length
17 const numChars = content.length
18 return numWords > 8 || numChars > 50
21export const getRandModel = async () => {
22 const configuredModels = _.values(telePromptModelsH) as string[]
23 const validatedModelsA = await getValidatedModels(configuredModels)
24 throwIf(() => validatedModelsA.length === 0, {configuredModels})
25 return _.sample(validatedModelsA)
28export const aiTelePrompt = async () => {
29 // Skip AI for test games to avoid API credits
30 const {existingGame} = gameActionCtx.getStore() || {}
31 if (existingGame && isTestGame({existingGame})) {
32 return {model: 'test-placeholder', content: 'test prompt for drawing'}
33 }
35 const db = getGamesDb()
36 const rows = await db.selectFrom('dbgames')
37 .where('created_at', 'is not', null)
38 .select(['game_state'])
39 .execute()
40 const statesA = _.map(rows, 'game_state') as GameStateJson[]
42 const dbPromptsA = _.compact(_.flatten(_.map(statesA, ({gameActionHistA}) => {
43 return _.map(gameActionHistA, ({playerAction, pregenFncRet}) => {
44 if (playerAction == 'giveMyselfAiPrompt') return pregenFncRet
45 })
46 })))
48 while (true) {
49 const model = await getRandModel()
50 const aiMessages = [
51 {
52 role: 'user',
53 content: `We are playing telestrations, in a few words write ONE original prompt that's slightly hard to draw`,
54 },
55 ..._.flatMap(_.uniq(_.compact(_.map(_.union(dbPromptsA, memPromptsA), 'content'))), (content) => [
56 {role: 'assistant', content},
57 aiUsrMsg((() => {
58 if (isTooLong(content)) return 'That was too long, do not apologize. Another please'
59 if (content.length <= 3) return 'Too short, do not apologize. Another please'
60 return 'Another please'
61 })()),
62 ])
63 ]
65 const { result: respH } = await runPromptSources({ modelIds: [model!], opts: { aiMessages }, context: 'aiTelePrompt' })
66 if (!respH) throw new Error('aiTelePrompt all sources exhausted')
67 let {content} = respH.choices[0].message
68 const {costUsd} = respH
69 content = sanitizeAiContent(content)
71 memPromptsA.push({model: model!, content})
72 if (!isTooLong(content)) {
73 return {content, model, costUsd}
74 }
75 }