Skip to content

Commit

Permalink
Setup variables on initialization, not just randomization.
Browse files Browse the repository at this point in the history
They're not synchronized across manual updates (should they be?)
  • Loading branch information
DrJKL committed Sep 4, 2023
1 parent f1e8358 commit 23e9a89
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down
65 changes: 57 additions & 8 deletions src/common/prompt_modification.tsx
Original file line number Diff line number Diff line change
@@ -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 }
Expand All @@ -29,30 +35,40 @@ export function variantSelectionReducer(
return draft;
}

export async function fillOutWildcards(
export async function fillOutWildcardsAndVariables(
allResults: ParseResult,
): Promise<ParseResult> {
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<Prompt>): Promise<void> {
async function fillOutChunkInPlace(
prompt: Draft<Prompt>,
variableMap: VariableMap,
): Promise<void> {
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);
}
}
}
Expand All @@ -70,14 +86,47 @@ async function fillOutWildcardInPlace(wildcard: Draft<Wildcard>) {
),
);
const parsedEntries = await Promise.all(
wildcardEntries.map((e) => parsePrompt(e)).map(fillOutWildcards),
wildcardEntries
.map((e) => parsePrompt(e))
.map(fillOutWildcardsAndVariables),
);

wildcard.variants = [
...parsedEntries.map(([[parseResult]]) => createDraft(parseResult)),
];
}

export function populateVariable(
variable: Draft<Variable>,
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<ParseResult>,
path: number[],
Expand Down
31 changes: 3 additions & 28 deletions src/common/random/randomize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Prompt[]> }
>;
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 23e9a89

Please sign in to comment.