From 23e9a898c554dd427cf9f50ad40d0ea750582f01 Mon Sep 17 00:00:00 2001 From: Alexander Brown Date: Sun, 3 Sep 2023 23:36:01 -0700 Subject: [PATCH] Setup variables on initialization, not just randomization. They're not synchronized across manual updates (should they be?) --- src/App.tsx | 4 +- src/common/prompt_modification.tsx | 65 ++++++++++++++++++++++++++---- src/common/random/randomize.ts | 31 ++------------ 3 files changed, 62 insertions(+), 38 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index f3b3d3a..8b80f65 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -25,7 +25,7 @@ import { useImmer, useImmerReducer } from 'use-immer'; import { getPromptTokens, parsePrompt } from './common/parsing/app_parsing'; import { ModifyPromptAction, - fillOutWildcards, + fillOutWildcardsAndVariables, variantSelectionReducer, } from './common/prompt_modification'; import { nextType } from './common/rendering/RenderType'; @@ -90,7 +90,7 @@ function App() { const results = parsePrompt(promptText); dispatch({ type: 'reset', results }); - fillOutWildcards(results).then((filledOut) => { + fillOutWildcardsAndVariables(results).then((filledOut) => { dispatch({ type: 'reset', results: filledOut }); }); } catch (err: unknown) { diff --git a/src/common/prompt_modification.tsx b/src/common/prompt_modification.tsx index f2003d4..cee3db4 100644 --- a/src/common/prompt_modification.tsx +++ b/src/common/prompt_modification.tsx @@ -1,11 +1,17 @@ import { Draft, createDraft, current, finishDraft } from 'immer'; -import { ParseResult, Prompt, Wildcard } from './rendering/parsed_types'; -import { randomizeAllResults } from './random/randomize'; +import { + ParseResult, + Prompt, + Variable, + Wildcard, +} from './rendering/parsed_types'; +import { VariableMap, randomizeAllResults } from './random/randomize'; import { PRNG } from 'seedrandom'; import { wildcardFiles$ } from '@wildcard-browser/src/lib/wildcards'; import { firstValueFrom } from 'rxjs'; import { filter, map, reduce } from 'rxjs/operators'; import { parsePrompt } from './parsing/app_parsing'; +import { cloneDeep } from 'lodash'; export type ModifyPromptAction = | { type: 'reset'; results: ParseResult } @@ -29,30 +35,40 @@ export function variantSelectionReducer( return draft; } -export async function fillOutWildcards( +export async function fillOutWildcardsAndVariables( allResults: ParseResult, ): Promise { const draft = createDraft(allResults); + const variableMap: VariableMap = {}; await Promise.all( - draft.flat().map(async (prompt) => await fillOutChunkInPlace(prompt)), + draft + .flat() + .map(async (prompt) => await fillOutChunkInPlace(prompt, variableMap)), ); return finishDraft(draft); } -async function fillOutChunkInPlace(prompt: Draft): Promise { +async function fillOutChunkInPlace( + prompt: Draft, + variableMap: VariableMap, +): Promise { for (const chunk of prompt) { switch (chunk.type) { case 'group': - await fillOutChunkInPlace(chunk.chunks); + await fillOutChunkInPlace(chunk.chunks, variableMap); break; case 'variants': await Promise.all( - chunk.variants.map(async (c) => await fillOutChunkInPlace(c)), + chunk.variants.map( + async (c) => await fillOutChunkInPlace(c, variableMap), + ), ); break; case 'wildcard': await fillOutWildcardInPlace(chunk); break; + case 'variable': + await populateVariable(chunk, variableMap); } } } @@ -70,7 +86,9 @@ async function fillOutWildcardInPlace(wildcard: Draft) { ), ); const parsedEntries = await Promise.all( - wildcardEntries.map((e) => parsePrompt(e)).map(fillOutWildcards), + wildcardEntries + .map((e) => parsePrompt(e)) + .map(fillOutWildcardsAndVariables), ); wildcard.variants = [ @@ -78,6 +96,37 @@ async function fillOutWildcardInPlace(wildcard: Draft) { ]; } +export function populateVariable( + variable: Draft, + variableMap: VariableMap, +) { + const { name, flavor, value } = variable; + const currentValue = variableMap[name]; + switch (flavor) { + case 'assignment': + case 'assignmentImmediate': + if (currentValue !== undefined) { + console.warn(`Overriding variable ${name}, was ${currentValue}`); + } + if (value) { + variableMap[name] = { + immediate: flavor === 'assignmentImmediate', + value, + }; + } + break; + case 'access': + if (currentValue) { + variable.value = currentValue.immediate + ? currentValue.value + : cloneDeep(currentValue.value); + } + break; + default: + flavor satisfies 'unknown'; + } +} + function modifySelection( draft: Draft, path: number[], diff --git a/src/common/random/randomize.ts b/src/common/random/randomize.ts index 00724ff..06887c4 100644 --- a/src/common/random/randomize.ts +++ b/src/common/random/randomize.ts @@ -8,9 +8,9 @@ import { Wildcard, } from '../rendering/parsed_types'; import { PRNG } from 'seedrandom'; -import { cloneDeep } from 'lodash'; +import { populateVariable } from '@common/prompt_modification'; -type VariableMap = Record< +export type VariableMap = Record< string, { immediate: boolean; value: Draft } >; @@ -83,32 +83,7 @@ function randomizeOrPopulateVariable( variableMap: VariableMap, prng: PRNG, ) { - console.log('v=', JSON.parse(JSON.stringify(variable))); - const { name, flavor, value } = variable; - const currentValue = variableMap[name]; - switch (flavor) { - case 'assignment': - case 'assignmentImmediate': - if (currentValue !== undefined) { - console.warn(`Overriding variable ${name}, was ${currentValue}`); - } - if (value) { - variableMap[name] = { - immediate: flavor === 'assignmentImmediate', - value, - }; - } - break; - case 'access': - if (currentValue) { - variable.value = currentValue.immediate - ? currentValue.value - : cloneDeep(currentValue.value); - } - break; - default: - flavor satisfies 'unknown'; - } + populateVariable(variable, variableMap); if (variable.value) { variable.value.forEach((v) => randomizePromptInPlace(v, variableMap, prng)); }