1import { runPromptSources } from '../../../serverF/aiF/aiSourcesAI.mts'2import { sanitizeAiContent } from '../../../serverF/aiF/sanitizeAiContentAI.mts'3import { getReqGameActionCtx } from '../gameState/gameActionCtxF.mts'4import { finishedTrailsToImgUrl } from '../finishedTrailsToBufferAI.mjs'5import { getGamePlayerStatesPaperStacks } from './updatePlayerStatesF.mts'6import { getIsAssignedStack } from './actionUtilsF.mjs'7import { userMsgImg } from '../../../serverF/aiF/userMsgImgF.mts'8import { maybeNotifError } from '../../../serverF/throwErrorF/maybeNotifErrF.mts'9import { isTestGame } from './isTestGameAI.mjs'11export const aiDescribeDraw = async () => {12 const {existingGame, finishedTrails, assignedStackName} = getReqGameActionCtx()13 if (!getIsAssignedStack().isCorrectStack) return15 // Skip AI for test games - let agents describe drawings manually16 if (isTestGame({existingGame})) return18 if (existingGame.settings && !existingGame.settings.aiDescriptEnabled) return20 const {paperStacks} = getGamePlayerStatesPaperStacks({existingGame})22 const stack = paperStacks[assignedStackName]24 const {length: noResp} = stack.responses25 if (!(noResp >= 2 && noResp <= 3)) return27 // console.log('WOULDRUNAIDESCRIBE-------------')28 // return30 const imageUrlStr = finishedTrailsToImgUrl({finishedTrails})32 const model = 'openai/gpt-4.1'33 // const model = 'meta-llama/llama-3.2-11b-vision-instruct'35 const aiMessages = [36 {role: 'user', content: `We are playing telestrations. In <10 words please describe the following drawing:`},37 userMsgImg({imageUrlStr}),38 ]40 let resp41 try {42 const r = await runPromptSources({ modelIds: [model], opts: { max_tokens: 100, aiMessages }, context: 'aiDescribeDraw' })43 resp = r.result44 } catch (err) { // openRouterErr shouldn't break game45 await maybeNotifError({err})46 return false47 }49 if (!resp) return false50 let {content} = resp.choices[0].message51 content = sanitizeAiContent(content)52 const {costUsd} = resp54 return {model, content, costUsd}55}