@adriansteffan/reactive 0.2.6 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +796 -229
- package/bin/add.js +56 -0
- package/bin/deploy.js +48 -0
- package/bin/download.js +28 -0
- package/bin/ghsetup.js +21 -0
- package/bin/native.js +81 -0
- package/bin/setup.js +11 -17
- package/bin/study-deploy.js +60 -0
- package/bin/undeploy.js +15 -0
- package/extras/transcription/compose.yaml +32 -0
- package/{template → extras}/transcription/transcribe.py +4 -9
- package/package.json +62 -64
- package/src/bun-env.d.ts +1 -0
- package/src/bun-plugin.ts +22 -0
- package/src/components/canvasblock.tsx +209 -537
- package/src/components/checkdevice.tsx +64 -84
- package/src/components/fullscreenblock.tsx +130 -0
- package/src/components/icons.tsx +50 -0
- package/src/components/index.ts +9 -11
- package/src/components/microphonecheck.tsx +76 -38
- package/src/components/plaininput.tsx +48 -41
- package/src/components/prolificending.tsx +64 -28
- package/src/components/quest.tsx +89 -87
- package/src/components/{randomdotkinetogram.tsx → randomdotkinematogram.tsx} +205 -267
- package/src/components/{voicerecorder.tsx → recorder.tsx} +81 -106
- package/src/components/requestfilepermission.tsx +63 -0
- package/src/components/stimulus.tsx +373 -0
- package/src/components/storeui.tsx +94 -148
- package/src/components/text.tsx +61 -59
- package/src/components/tutorial.tsx +45 -47
- package/src/components/upload.tsx +133 -309
- package/src/components/voicerecording.tsx +90 -94
- package/src/core/backend.ts +20 -0
- package/src/core/bytecode.ts +178 -0
- package/src/core/columns.ts +50 -0
- package/src/core/component.ts +238 -0
- package/src/core/development.ts +32 -0
- package/src/core/experiment.ts +263 -0
- package/src/core/files.ts +291 -0
- package/src/core/params.ts +166 -0
- package/src/core/platform.ts +29 -0
- package/src/core/rows.ts +106 -0
- package/src/core/simulation.ts +158 -0
- package/src/core/slides.ts +178 -0
- package/src/core/store.ts +22 -0
- package/src/core/timeline.ts +399 -0
- package/src/fonts/AtkinsonHyperlegible-Bold.woff2 +0 -0
- package/src/fonts/AtkinsonHyperlegible-BoldItalic.woff2 +0 -0
- package/src/fonts/AtkinsonHyperlegible-Italic.woff2 +0 -0
- package/src/fonts/AtkinsonHyperlegible-Regular.woff2 +0 -0
- package/src/index.css +5 -7
- package/src/mod.tsx +31 -17
- package/src/native.ts +127 -0
- package/src/quest.ts +2 -0
- package/src/runner/contexts.ts +35 -0
- package/src/runner/devpanel.tsx +68 -0
- package/src/runner/experimentrunner.tsx +210 -0
- package/src/runner/fullscreenguard.tsx +93 -0
- package/src/runner/paraminput.tsx +86 -0
- package/src/runner/progressbar.tsx +20 -0
- package/src/runner/settingsscreen.tsx +517 -0
- package/src/{utils → runner}/theme.ts +9 -8
- package/src/server.ts +93 -0
- package/src/tutorial.ts +3 -0
- package/src/utils/array.ts +30 -62
- package/src/utils/canvas.ts +66 -0
- package/src/utils/ddm.ts +47 -70
- package/src/utils/distributions.ts +352 -61
- package/src/utils/fullscreen.ts +38 -0
- package/src/utils/ids.ts +11 -0
- package/src/utils/keys.ts +46 -0
- package/src/utils/llm.ts +15 -15
- package/src/utils/time.ts +12 -0
- package/src/utils/tts.ts +24 -24
- package/template/.dockerignore +3 -1
- package/template/.env.example +5 -0
- package/template/.github/workflows/deploy.yml +25 -0
- package/template/.gitignore +14 -0
- package/template/Dockerfile +9 -14
- package/template/README.md +74 -214
- package/template/biome.json +34 -0
- package/template/build.ts +20 -0
- package/template/bunfig.toml +3 -0
- package/template/docker-compose.yaml +4 -33
- package/template/index.html +2 -2
- package/template/package.json +16 -89
- package/template/public/.gitkeep +0 -0
- package/template/server.ts +32 -0
- package/template/simulate.ts +4 -44
- package/template/src/Experiment.tsx +76 -128
- package/template/src/bun-env.d.ts +1 -0
- package/template/src/index.css +1 -9
- package/template/src/main.tsx +25 -10
- package/template/tsconfig.json +4 -6
- package/.claude/settings.local.json +0 -61
- package/.eslintrc.cjs +0 -18
- package/.playwright-mcp/console-2026-04-07T14-22-59-060Z.log +0 -1
- package/.prettierrc +0 -5
- package/Dockerfile +0 -20
- package/dist/mod-HDegGPrf.js +0 -87434
- package/dist/mod.d.ts +0 -946
- package/dist/reactive.es.js +0 -87
- package/dist/reactive.umd.js +0 -10255
- package/dist/style.css +0 -5
- package/dist/web-7BbR1Ad7.js +0 -435
- package/dist/web-CATfZO1Q.js +0 -12
- package/planned.md +0 -103
- package/postcss.config.js +0 -6
- package/raw-loader-hooks.mjs +0 -25
- package/raw-loader.mjs +0 -5
- package/rdk_doc.md +0 -341
- package/src/components/enterfullscreen.tsx +0 -230
- package/src/components/exitfullscreen.tsx +0 -113
- package/src/components/experimentprovider.tsx +0 -69
- package/src/components/experimentrunner.tsx +0 -497
- package/src/components/mobilefilepermission.tsx +0 -68
- package/src/components/settingsscreen.tsx +0 -770
- package/src/types/array.d.ts +0 -6
- package/src/utils/audiodevice.ts +0 -4
- package/src/utils/bytecode.ts +0 -261
- package/src/utils/common.ts +0 -291
- package/src/utils/ddm.test.ts +0 -233
- package/src/utils/distributions.test.ts +0 -209
- package/src/utils/nodecompat.ts +0 -11
- package/src/utils/overlays.ts +0 -19
- package/src/utils/request.ts +0 -25
- package/src/utils/simulation.ts +0 -299
- package/src/utils/upload.ts +0 -275
- package/src/vite-env.d.ts +0 -1
- package/template/.env.template +0 -2
- package/template/.eslintrc.cjs +0 -18
- package/template/.prettierrc +0 -5
- package/template/backend/package-lock.json +0 -2544
- package/template/backend/package.json +0 -31
- package/template/backend/src/backend.ts +0 -131
- package/template/backend/tsconfig.json +0 -110
- package/template/capacitor.config.ts +0 -15
- package/template/electron/main.js +0 -131
- package/template/electron/preload.js +0 -9
- package/template/electron.vite.config.js +0 -45
- package/template/package-lock.json +0 -12467
- package/template/postcss.config.js +0 -5
- package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-Bold.ttf +0 -0
- package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-BoldItalic.ttf +0 -0
- package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-Italic.ttf +0 -0
- package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-Regular.ttf +0 -0
- package/template/shared.vite.config.js +0 -39
- package/template/src/vite-env.d.ts +0 -2
- package/template/tsconfig.node.json +0 -12
- package/template/vite.config.js +0 -11
- package/tsconfig.json +0 -30
- package/tsconfig.node.json +0 -12
- package/vite.config.ts +0 -39
- /package/{template → extras}/transcription/Dockerfile.cpu +0 -0
- /package/{template → extras}/transcription/Dockerfile.gpu +0 -0
- /package/{template/public/Atkinson_Hyperlegible → src/fonts}/OFL.txt +0 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BLOCK,
|
|
3
|
+
type BlockSpec,
|
|
4
|
+
type BranchPath,
|
|
5
|
+
type Bytecode,
|
|
6
|
+
compileTimeline,
|
|
7
|
+
isBlock,
|
|
8
|
+
} from './bytecode';
|
|
9
|
+
import type {
|
|
10
|
+
RecordedOf,
|
|
11
|
+
RuntimeComponent,
|
|
12
|
+
SimulatorOverrides,
|
|
13
|
+
SimulatorReplacement,
|
|
14
|
+
} from './component';
|
|
15
|
+
import { isReservedTrialName, type ReservedTrialName, sessionPrefixes } from './columns';
|
|
16
|
+
import { type TrialCsv, trialCsv } from './files';
|
|
17
|
+
import type { ExperimentData } from './rows';
|
|
18
|
+
import type { SlideData, SlideTimeline } from './slides';
|
|
19
|
+
import type { Store, StoreUpdate } from './store';
|
|
20
|
+
|
|
21
|
+
// --- Context ---
|
|
22
|
+
|
|
23
|
+
export interface TimelineContext {
|
|
24
|
+
/** Everything recorded so far: the session, and the rows of the trials that have ended. Components read them with `getAll` and `getLatest`. */
|
|
25
|
+
data: ExperimentData;
|
|
26
|
+
store: Store;
|
|
27
|
+
/** The rows of the slides recorded so far by the CanvasBlock this runs in; empty outside a CanvasBlock. */
|
|
28
|
+
slides: SlideData[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function createTimelineContext(data: ExperimentData, store: Store): TimelineContext {
|
|
32
|
+
return { data, store, slides: [] };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A value, or a function that computes it from the context when the trial runs. */
|
|
36
|
+
export type ContextArg<T> = T | ((context: TimelineContext) => T);
|
|
37
|
+
|
|
38
|
+
export function resolve<T>(value: ContextArg<T>, context: TimelineContext): T {
|
|
39
|
+
return typeof value === 'function' ? (value as (context: TimelineContext) => T)(context) : value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// --- Trials ---
|
|
43
|
+
|
|
44
|
+
const TRIAL: unique symbol = Symbol('reactive.trial');
|
|
45
|
+
|
|
46
|
+
interface TrialSpec {
|
|
47
|
+
component: RuntimeComponent;
|
|
48
|
+
name?: string;
|
|
49
|
+
props: ContextArg<Record<string, any>>;
|
|
50
|
+
// Layers of metadata, merged in order when the trial runs
|
|
51
|
+
metadata?: ContextArg<Record<string, any>>[];
|
|
52
|
+
csv?: TrialCsv;
|
|
53
|
+
session?: boolean;
|
|
54
|
+
collectRefreshRate?: boolean;
|
|
55
|
+
hideParams?: readonly string[] | boolean;
|
|
56
|
+
simulators?: Record<string, SimulatorReplacement>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** A trial name, or the reason the types reject it: its session CSV columns would take the library's. */
|
|
60
|
+
export type TrialName<N extends string> = N extends ReservedTrialName
|
|
61
|
+
? `The trial name '${N}' would put its session CSV columns under url_ or session_, which the library keeps for itself`
|
|
62
|
+
: N;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* A trial in the timeline, of component `C`, with default simulators `D` and params `H`. Every method
|
|
66
|
+
* returns a new trial and leaves this one unchanged; passing `undefined` leaves a setting as it is.
|
|
67
|
+
*/
|
|
68
|
+
export interface Trial<C = any, D = any, H extends string = string> {
|
|
69
|
+
readonly [TRIAL]: TrialSpec;
|
|
70
|
+
// Carries the types for inference; never set
|
|
71
|
+
readonly __types?: { component: C; simulators: D; settings: H };
|
|
72
|
+
/**
|
|
73
|
+
* Extra values for this trial's rows, merged into those set before: under `metadata` in the data,
|
|
74
|
+
* and as `meta_` columns in the CSVs.
|
|
75
|
+
*/
|
|
76
|
+
metadata(values: ContextArg<Record<string, any>> | undefined): Trial<C, D, H>;
|
|
77
|
+
/**
|
|
78
|
+
* Renames the CSV files of this trial's rows, which keep their component's flatteners: a name for
|
|
79
|
+
* a component with one file, and for one with several, a map from its files to new names or to
|
|
80
|
+
* `null`. `null` keeps the rows out of the CSVs.
|
|
81
|
+
*/
|
|
82
|
+
csv(files: TrialCsv | undefined): Trial<C, D, H>;
|
|
83
|
+
/**
|
|
84
|
+
* Puts this trial's columns in the session CSV, or with `false` keeps them out, whatever its
|
|
85
|
+
* component does. A response that is a list has no columns there.
|
|
86
|
+
*/
|
|
87
|
+
session(enabled: (RecordedOf<C> extends readonly unknown[] ? false : boolean) | undefined): Trial<C, D, H>;
|
|
88
|
+
/** Measures the screen's refresh rate while this trial runs and stores it for later canvas trials. */
|
|
89
|
+
collectRefreshRate(enabled?: boolean): Trial<C, D, H>;
|
|
90
|
+
/** Hides the component's params from the settings screen: all of them, or the ones named. */
|
|
91
|
+
hideParams(params: readonly H[] | boolean | undefined): Trial<C, D, H>;
|
|
92
|
+
/** Replaces some of the component's simulators for this trial, each with one that returns what it returns. */
|
|
93
|
+
simulators(simulators: SimulatorOverrides<C, D> | undefined): Trial<C, D, H>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function isTrial(item: unknown): item is Trial {
|
|
97
|
+
return typeof item === 'object' && item !== null && TRIAL in item;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function makeTrial(spec: TrialSpec): Trial {
|
|
101
|
+
const trial = Object.create(trialMethods);
|
|
102
|
+
trial[TRIAL] = Object.freeze(spec);
|
|
103
|
+
return Object.freeze(trial);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function withSpec(trial: Trial, changes: Partial<TrialSpec>): Trial {
|
|
107
|
+
return makeTrial({ ...trial[TRIAL], ...changes });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const trialMethods = {
|
|
111
|
+
metadata(this: Trial, values: ContextArg<Record<string, any>> | undefined) {
|
|
112
|
+
return values === undefined ? this : withSpec(this, { metadata: [...(this[TRIAL].metadata ?? []), values] });
|
|
113
|
+
},
|
|
114
|
+
csv(this: Trial, csv: TrialCsv | undefined) {
|
|
115
|
+
return csv === undefined ? this : withSpec(this, { csv });
|
|
116
|
+
},
|
|
117
|
+
session(this: Trial, enabled: boolean | undefined) {
|
|
118
|
+
return enabled === undefined ? this : withSpec(this, { session: enabled });
|
|
119
|
+
},
|
|
120
|
+
// Turns on when called without an argument, and stays as it is when passed undefined
|
|
121
|
+
collectRefreshRate(this: Trial, ...args: [boolean?]) {
|
|
122
|
+
if (args.length === 0) return withSpec(this, { collectRefreshRate: true });
|
|
123
|
+
return args[0] === undefined ? this : withSpec(this, { collectRefreshRate: args[0] });
|
|
124
|
+
},
|
|
125
|
+
hideParams(this: Trial, hideParams: readonly string[] | boolean | undefined) {
|
|
126
|
+
return hideParams === undefined ? this : withSpec(this, { hideParams });
|
|
127
|
+
},
|
|
128
|
+
simulators(this: Trial, simulators: Record<string, SimulatorReplacement> | undefined) {
|
|
129
|
+
return simulators === undefined
|
|
130
|
+
? this
|
|
131
|
+
: withSpec(this, { simulators: { ...this[TRIAL].simulators, ...simulators } });
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/** What the runner and the simulation read for a trial. */
|
|
136
|
+
export interface TrialContent extends Omit<TrialSpec, 'props' | 'metadata'> {
|
|
137
|
+
type: string;
|
|
138
|
+
/** Its own name, or for an unnamed trial in the session CSV, its component's, numbered from the second on. */
|
|
139
|
+
name?: string;
|
|
140
|
+
props: ContextArg<Record<string, any>>;
|
|
141
|
+
metadata?: ContextArg<Record<string, any>>;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Layers merged in order: an object when all of them are, else a function of the context
|
|
145
|
+
const merge = (layers: ContextArg<Record<string, any>>[]): ContextArg<Record<string, any>> =>
|
|
146
|
+
layers.every((layer) => typeof layer !== 'function')
|
|
147
|
+
? Object.assign({}, ...(layers as Record<string, any>[]))
|
|
148
|
+
: (context: TimelineContext) => Object.assign({}, ...layers.map((layer) => resolve(layer, context)));
|
|
149
|
+
|
|
150
|
+
function trialContent(spec: TrialSpec): TrialContent {
|
|
151
|
+
const { component, props, metadata, ...settings } = spec;
|
|
152
|
+
return {
|
|
153
|
+
component,
|
|
154
|
+
type: component.reactive.name,
|
|
155
|
+
props,
|
|
156
|
+
...(metadata ? { metadata: merge(metadata) } : {}),
|
|
157
|
+
...settings,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// --- Blocks ---
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* A block of control flow. `Holds` is what its items are, trials or slides, which keeps it to the
|
|
165
|
+
* timelines of that kind; a block without items, such as updateStore, fits both.
|
|
166
|
+
*/
|
|
167
|
+
export interface Block<Holds extends 'trials' | 'slides' = never> {
|
|
168
|
+
readonly [BLOCK]: BlockSpec;
|
|
169
|
+
readonly __holds?: Holds;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// What a list of items holds: trials, slides, or never for a list that could be either, such as an empty one
|
|
173
|
+
type HoldsOf<T> = T extends Timeline ? (T extends SlideTimeline ? never : 'trials') : 'slides';
|
|
174
|
+
// The items that a block holding `H` takes
|
|
175
|
+
type ItemsFor<H> = [H] extends [never] ? Timeline | SlideTimeline : H extends 'trials' ? Timeline : SlideTimeline;
|
|
176
|
+
|
|
177
|
+
export interface IfBlock<Holds extends 'trials' | 'slides' = never> extends Block<Holds> {
|
|
178
|
+
/** Items to run when the condition is false, of the same kind as the block's. */
|
|
179
|
+
else(items: undefined): Block<Holds>;
|
|
180
|
+
else<const E extends ItemsFor<Holds>>(items: E | undefined): Block<Holds | HoldsOf<E>>;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** A list of trials and blocks. Nested lists are flattened, and `false`, `null` and `undefined` are skipped. */
|
|
184
|
+
export type Timeline = readonly TimelineEntry[];
|
|
185
|
+
export type TimelineEntry = Trial<any, any, any> | Block<'trials'> | Timeline | false | null | undefined;
|
|
186
|
+
|
|
187
|
+
const block = (spec: BlockSpec): Block => Object.freeze({ [BLOCK]: spec });
|
|
188
|
+
|
|
189
|
+
/** Runs the items if the condition is true when the block is reached. In a CanvasBlock, the items are slides. */
|
|
190
|
+
export function ifBlock<const T extends Timeline | SlideTimeline>(
|
|
191
|
+
cond: (context: TimelineContext) => boolean,
|
|
192
|
+
items: T,
|
|
193
|
+
): IfBlock<HoldsOf<T>> {
|
|
194
|
+
const spec = { kind: 'if', cond, items } as const;
|
|
195
|
+
return Object.freeze({
|
|
196
|
+
[BLOCK]: spec,
|
|
197
|
+
else: (elseItems: Timeline | SlideTimeline | undefined) =>
|
|
198
|
+
elseItems === undefined ? block(spec) : block({ ...spec, else: elseItems }),
|
|
199
|
+
}) as IfBlock<HoldsOf<T>>;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Runs the items again and again while the condition is true. In a CanvasBlock, the items are slides. */
|
|
203
|
+
export function whileBlock<const T extends Timeline | SlideTimeline>(
|
|
204
|
+
cond: (context: TimelineContext) => boolean,
|
|
205
|
+
items: T,
|
|
206
|
+
): Block<HoldsOf<T>> {
|
|
207
|
+
return block({ kind: 'while', cond, body: items });
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Runs the items in fullscreen, which the run leaves once it is outside them; fullscreenBlock puts its screen first. */
|
|
211
|
+
export function fullscreenSpan(items: Timeline): Block<'trials'> {
|
|
212
|
+
return block({ kind: 'fullscreen', items }) as Block<'trials'>;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Merges the returned values into the store. */
|
|
216
|
+
export function updateStore(update: (context: TimelineContext) => StoreUpdate): Block {
|
|
217
|
+
return block({ kind: 'updateStore', update });
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** A position that `ifGoto` can jump to. */
|
|
221
|
+
export function marker(id: string): Block {
|
|
222
|
+
return block({ kind: 'marker', id });
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Jumps to the marker if the condition is true. */
|
|
226
|
+
export function ifGoto(cond: (context: TimelineContext) => boolean, markerId: string): Block {
|
|
227
|
+
return block({ kind: 'ifGoto', cond, marker: markerId });
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const blockNames: Record<BlockSpec['kind'], string> = {
|
|
231
|
+
if: 'ifBlock',
|
|
232
|
+
while: 'whileBlock',
|
|
233
|
+
updateStore: 'updateStore',
|
|
234
|
+
marker: 'marker',
|
|
235
|
+
ifGoto: 'ifGoto',
|
|
236
|
+
fullscreen: 'fullscreenBlock',
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/** The items of a list, with nested lists flattened and `false`, `null` and `undefined` skipped. */
|
|
240
|
+
export function itemsOf(list: readonly unknown[]): unknown[] {
|
|
241
|
+
return list.flatMap((item) =>
|
|
242
|
+
Array.isArray(item) ? itemsOf(item) : item === false || item === null || item === undefined ? [] : [item],
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// The items a block holds, the else arm's after an ifBlock's own
|
|
247
|
+
function blockItems(spec: BlockSpec): { item: unknown; arm?: 'else' }[] {
|
|
248
|
+
if (spec.kind === 'if') {
|
|
249
|
+
return [...itemsOf(spec.items).map((item) => ({ item })), ...itemsOf(spec.else ?? []).map((item) => ({ item, arm: 'else' as const }))];
|
|
250
|
+
}
|
|
251
|
+
if (spec.kind === 'while') return itemsOf(spec.body).map((item) => ({ item }));
|
|
252
|
+
return spec.kind === 'fullscreen' ? itemsOf(spec.items).map((item) => ({ item })) : [];
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** A trial or block as the settings screen lists it, with the items of a block. */
|
|
256
|
+
export interface EntryInfo {
|
|
257
|
+
/** Its place in the timeline: its position, after those of the blocks it sits in (`4.2`). */
|
|
258
|
+
position: string;
|
|
259
|
+
/** What a link's `excludeSubset` names it by: a trial's name, or else its position. */
|
|
260
|
+
id: string;
|
|
261
|
+
/** The component of a trial, or the kind of a block. */
|
|
262
|
+
type: string;
|
|
263
|
+
/** A trial's name, or a marker's id. */
|
|
264
|
+
name?: string;
|
|
265
|
+
/** Set for the items of an ifBlock's else arm. */
|
|
266
|
+
arm?: 'else';
|
|
267
|
+
children?: EntryInfo[];
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function describe(item: unknown, position: string, arm?: 'else'): EntryInfo {
|
|
271
|
+
const at = { position, id: position, ...(arm ? { arm } : {}) };
|
|
272
|
+
if (isTrial(item)) {
|
|
273
|
+
const { name } = item[TRIAL];
|
|
274
|
+
return { ...at, id: name ?? position, type: item[TRIAL].component.reactive.name, ...(name !== undefined ? { name } : {}) };
|
|
275
|
+
}
|
|
276
|
+
if (!isBlock(item)) return { ...at, type: typeof item };
|
|
277
|
+
const spec = item[BLOCK];
|
|
278
|
+
if (spec.kind === 'marker') return { ...at, type: 'marker', name: spec.id };
|
|
279
|
+
const children = blockItems(spec).map((child, i) => describe(child.item, `${position}.${i + 1}`, child.arm));
|
|
280
|
+
return { ...at, type: blockNames[spec.kind], ...(children.length > 0 ? { children } : {}) };
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** The timeline's entries as the settings screen lists them, with the items of blocks under them. */
|
|
284
|
+
export function describeTimeline(timeline: Timeline): EntryInfo[] {
|
|
285
|
+
return itemsOf(timeline).map((item, i) => describe(item, `${i + 1}`));
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// The markers in an entry, also those inside its blocks
|
|
289
|
+
function markersIn(entry: unknown): Block[] {
|
|
290
|
+
if (!isBlock(entry)) return [];
|
|
291
|
+
const spec = entry[BLOCK];
|
|
292
|
+
return spec.kind === 'marker' ? [entry] : blockItems(spec).flatMap((child) => markersIn(child.item));
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* The timeline without the entries that `excluded` names, by name or by position, also inside
|
|
297
|
+
* blocks. An entry left out leaves its markers in its place, so that every ifGoto keeps its target,
|
|
298
|
+
* and a block left without any of its items goes as well, so that no loop runs on without them.
|
|
299
|
+
*/
|
|
300
|
+
export function excludeEntries(timeline: Timeline, excluded: ReadonlySet<string>): Timeline {
|
|
301
|
+
const select = (item: unknown, position: string): unknown[] => {
|
|
302
|
+
const name = isTrial(item) ? item[TRIAL].name : undefined;
|
|
303
|
+
if (excluded.has(position) || (name !== undefined && excluded.has(name))) return markersIn(item);
|
|
304
|
+
if (!isBlock(item)) return [item];
|
|
305
|
+
const spec = item[BLOCK];
|
|
306
|
+
if (spec.kind !== 'if' && spec.kind !== 'while' && spec.kind !== 'fullscreen') return [item];
|
|
307
|
+
const kept = blockItems(spec).map((child, i) => ({ ...child, kept: select(child.item, `${position}.${i + 1}`) }));
|
|
308
|
+
if (kept.every((child) => child.kept.length === 1 && child.kept[0] === child.item)) return [item];
|
|
309
|
+
const items = kept.flatMap((child) => child.kept);
|
|
310
|
+
if (items.every((kept) => isBlock(kept) && kept[BLOCK].kind === 'marker')) return items;
|
|
311
|
+
const arm = (which?: 'else') => kept.filter((child) => child.arm === which).flatMap((child) => child.kept);
|
|
312
|
+
if (spec.kind === 'while') return [block({ ...spec, body: arm() })];
|
|
313
|
+
if (spec.kind === 'fullscreen') return [block({ ...spec, items: arm() })];
|
|
314
|
+
return [block({ ...spec, items: arm(), ...(spec.else ? { else: arm('else') } : {}) })];
|
|
315
|
+
};
|
|
316
|
+
return itemsOf(timeline).flatMap((item, i) => select(item, `${i + 1}`)) as Timeline;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// --- Compiling ---
|
|
320
|
+
|
|
321
|
+
export interface CompiledTimeline {
|
|
322
|
+
bytecode: Bytecode<TrialContent>;
|
|
323
|
+
/** The named trials, which the session records for components' `getAll` and `getLatest`. */
|
|
324
|
+
namedTrials: { type: string; name: string }[];
|
|
325
|
+
/** The components of the trials, by name. */
|
|
326
|
+
components: ReadonlyMap<string, RuntimeComponent>;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// Two positions are exclusive when they sit in different branches of the same ifBlock
|
|
330
|
+
function exclusive(a: BranchPath, b: BranchPath): boolean {
|
|
331
|
+
const index = a.findIndex((step, i) => step.block !== b[i]?.block || step.arm !== b[i]?.arm);
|
|
332
|
+
if (index === -1 || index >= b.length) return false;
|
|
333
|
+
return a[index].block === b[index].block;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function compileExperimentTimeline(timeline: Timeline): CompiledTimeline {
|
|
337
|
+
const components = new Map<string, RuntimeComponent>();
|
|
338
|
+
// Trials by name, whatever their component
|
|
339
|
+
const named = new Map<string, { trial: Trial; branch: BranchPath }[]>();
|
|
340
|
+
// How many unnamed trials of each component go to the session CSV
|
|
341
|
+
const unnamedInSession = new Map<string, number>();
|
|
342
|
+
const namedTrials: { type: string; name: string }[] = [];
|
|
343
|
+
|
|
344
|
+
const bytecode = compileTimeline<TrialContent>(timeline, (item, branch) => {
|
|
345
|
+
if (!isTrial(item)) {
|
|
346
|
+
throw new Error(
|
|
347
|
+
`The timeline can only hold trials, blocks and lists of them, but it holds ${JSON.stringify(item) ?? String(item)}.`,
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
const spec = item[TRIAL];
|
|
351
|
+
const type = spec.component.reactive.name;
|
|
352
|
+
|
|
353
|
+
const known = components.get(type);
|
|
354
|
+
if (known && known !== spec.component) {
|
|
355
|
+
throw new Error(`Two different components in the timeline are named '${type}'.`);
|
|
356
|
+
}
|
|
357
|
+
components.set(type, spec.component);
|
|
358
|
+
|
|
359
|
+
if (spec.name !== undefined && isReservedTrialName(spec.name)) {
|
|
360
|
+
throw new Error(
|
|
361
|
+
`A trial cannot be named '${spec.name}': its columns in the session CSV would start with ${sessionPrefixes.join(' or ')}, which the library keeps for itself.`,
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// The session CSV's columns start with the trial's name, so an unnamed trial there gets one
|
|
366
|
+
let name = spec.name;
|
|
367
|
+
// Checks the trial's files when the experiment loads
|
|
368
|
+
trialCsv(type, spec.component.reactive.csv, spec.csv);
|
|
369
|
+
if (name === undefined && (spec.session ?? spec.component.reactive.session !== undefined)) {
|
|
370
|
+
const count = (unnamedInSession.get(type) ?? 0) + 1;
|
|
371
|
+
unnamedInSession.set(type, count);
|
|
372
|
+
name = count === 1 ? type : `${type}_${count}`;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (name !== undefined) {
|
|
376
|
+
const earlier = named.get(name) ?? [];
|
|
377
|
+
named.set(name, [...earlier, { trial: item, branch }]);
|
|
378
|
+
const clash = earlier.find((other) => other.trial !== item && !exclusive(other.branch, branch))?.trial;
|
|
379
|
+
if (clash && (spec.name === undefined || clash[TRIAL].name === undefined)) {
|
|
380
|
+
const unnamedType = spec.name === undefined ? type : clash[TRIAL].component.reactive.name;
|
|
381
|
+
throw new Error(
|
|
382
|
+
`'${name}' is the name an unnamed ${unnamedType} trial gets for its columns in the session CSV, so no other trial can have it. Name one of them differently.`,
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
if (clash) {
|
|
386
|
+
throw new Error(
|
|
387
|
+
`Two different trials are named '${name}' (${clash[TRIAL].component.reactive.name} and ${type}), so their data could not be told apart.`,
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
if (spec.name !== undefined && !namedTrials.some((trial) => trial.type === type && trial.name === name)) {
|
|
391
|
+
namedTrials.push({ type, name });
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return trialContent({ ...spec, name });
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
return { bytecode, namedTrials, components };
|
|
399
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/src/index.css
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
@
|
|
2
|
-
|
|
3
|
-
@source '../dist/**/*.{js,ts,jsx,tsx}';
|
|
1
|
+
@source './';
|
|
4
2
|
|
|
5
3
|
@theme {
|
|
6
4
|
--font-*: initial;
|
|
@@ -59,28 +57,28 @@
|
|
|
59
57
|
|
|
60
58
|
@font-face {
|
|
61
59
|
font-family: 'Atkinson Hyperlegible';
|
|
62
|
-
src: url('/
|
|
60
|
+
src: url('./fonts/AtkinsonHyperlegible-Regular.woff2') format('woff2');
|
|
63
61
|
font-weight: 400;
|
|
64
62
|
font-style: normal;
|
|
65
63
|
}
|
|
66
64
|
|
|
67
65
|
@font-face {
|
|
68
66
|
font-family: 'Atkinson Hyperlegible';
|
|
69
|
-
src: url('/
|
|
67
|
+
src: url('./fonts/AtkinsonHyperlegible-Bold.woff2') format('woff2');
|
|
70
68
|
font-weight: 700;
|
|
71
69
|
font-style: normal;
|
|
72
70
|
}
|
|
73
71
|
|
|
74
72
|
@font-face {
|
|
75
73
|
font-family: 'Atkinson Hyperlegible';
|
|
76
|
-
src: url('/
|
|
74
|
+
src: url('./fonts/AtkinsonHyperlegible-Italic.woff2') format('woff2');
|
|
77
75
|
font-weight: 400;
|
|
78
76
|
font-style: italic;
|
|
79
77
|
}
|
|
80
78
|
|
|
81
79
|
@font-face {
|
|
82
80
|
font-family: 'Atkinson Hyperlegible';
|
|
83
|
-
src: url('/
|
|
81
|
+
src: url('./fonts/AtkinsonHyperlegible-BoldItalic.woff2') format('woff2');
|
|
84
82
|
font-weight: 700;
|
|
85
83
|
font-style: italic;
|
|
86
84
|
}
|
package/src/mod.tsx
CHANGED
|
@@ -1,25 +1,39 @@
|
|
|
1
|
-
|
|
1
|
+
export type { BaseComponentProps } from './core/component';
|
|
2
|
+
export type { ExperimentConfig } from './core/experiment';
|
|
3
|
+
export type { FileUpload } from './core/files';
|
|
4
|
+
export type { ParamSpec } from './core/params';
|
|
5
|
+
export type { Platform } from './core/platform';
|
|
6
|
+
export type { ExperimentData, SessionData, TrialData } from './core/rows';
|
|
7
|
+
export { canvasCountdown } from './components/canvasblock';
|
|
8
|
+
export type { Slide, SlideData, SlideFunction, SlideResponse, SlideTimeline } from './core/slides';
|
|
9
|
+
export { getPlatform } from './core/platform';
|
|
10
|
+
export { now } from './utils/time';
|
|
2
11
|
|
|
3
|
-
export
|
|
12
|
+
export { defineComponent } from './core/component';
|
|
13
|
+
export { ifBlock, ifGoto, marker, updateStore, whileBlock } from './core/timeline';
|
|
14
|
+
export type { TimelineContext, Timeline, Trial } from './core/timeline';
|
|
15
|
+
export type { DefinedComponent, OwnProps, ParticipantState, RecordedOf, SimulateFunction, SimulateResult, Simulator, SimulatorResult } from './core/component';
|
|
16
|
+
export type { ExperimentStore } from './core/store';
|
|
17
|
+
export { defineExperiment } from './core/experiment';
|
|
18
|
+
export { useParams } from './runner/contexts';
|
|
19
|
+
export type { Experiment } from './core/experiment';
|
|
20
|
+
export type { ParamValue, ParamValues } from './core/params';
|
|
4
21
|
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export {
|
|
9
|
-
export {
|
|
10
|
-
export type {
|
|
22
|
+
export { chunk, mean, sample, shuffle, sum } from './utils/array';
|
|
23
|
+
export { noopSimulate, runSimulation } from './core/simulation';
|
|
24
|
+
export { getBackendUrl } from './core/backend';
|
|
25
|
+
export type { RunSimulationConfig } from './core/simulation';
|
|
26
|
+
export type { Flatten, TrialRow } from './core/files';
|
|
27
|
+
export type { ResponseKey } from './utils/keys';
|
|
11
28
|
export { prolificId, alphanumericId } from './utils/ids';
|
|
12
|
-
export { normal, poisson, uniform,
|
|
13
|
-
export { useTheme,
|
|
14
|
-
export {
|
|
15
|
-
export { textToWebmBase64, probeAudioDurationMs } from './utils/tts';
|
|
16
|
-
export { DUMMY_AUDIO_BASE64 } from './components/voicerecording';
|
|
29
|
+
export { normal, poisson, uniform, sobol, halton, sampleParticipants } from './utils/distributions';
|
|
30
|
+
export { useTheme, t } from './runner/theme';
|
|
31
|
+
export { useAudioDeviceId } from './runner/contexts';
|
|
17
32
|
export { invokeLLM } from './utils/llm';
|
|
18
33
|
export type { LLMConfig, LLMSchema } from './utils/llm';
|
|
19
|
-
export type { Theme } from './
|
|
20
|
-
export type { DimensionSpec, UniformSpec, NormalSpec, PoissonSpec, IntegerSpec, DiscreteSpec, SamplingMethod } from './utils/distributions';
|
|
34
|
+
export type { Theme } from './runner/theme';
|
|
35
|
+
export type { DimensionSpec, TraitSpec, UniformSpec, NormalSpec, PoissonSpec, IntegerSpec, DiscreteSpec, SamplingMethod } from './utils/distributions';
|
|
21
36
|
export { simulateDDMTrial, mapDDMChoice } from './utils/ddm';
|
|
22
|
-
export type { DDMTrialParams, DDMTrialResult, ParameterDef,
|
|
37
|
+
export type { DDMTrialParams, DDMTrialResult, ParameterDef, BoundaryDef, CollapseDef, PostStimStrategyDef, ContinueStrategy, CollapseStrategy, SnapshotStrategy } from './utils/ddm';
|
|
23
38
|
export * from './components';
|
|
24
39
|
|
|
25
|
-
export * from 'react-toastify';
|
package/src/native.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { Capacitor } from '@capacitor/core';
|
|
2
|
+
import { Filesystem, Directory, Encoding } from '@capacitor/filesystem';
|
|
3
|
+
import { StatusBar } from '@capacitor/status-bar';
|
|
4
|
+
import { ImmersiveMode } from '@adriansteffan/immersive-mode';
|
|
5
|
+
import { FileBackend, registerNative } from './core/platform';
|
|
6
|
+
|
|
7
|
+
const isAndroid = Capacitor.getPlatform() === 'android';
|
|
8
|
+
|
|
9
|
+
function createFileBackend(parentFolder?: string): FileBackend {
|
|
10
|
+
const getPath = (path: string): string => {
|
|
11
|
+
if (!parentFolder) {
|
|
12
|
+
return path;
|
|
13
|
+
}
|
|
14
|
+
return path ? `${parentFolder}/${path}` : parentFolder;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
directoryExists: async (path: string): Promise<boolean> => {
|
|
19
|
+
try {
|
|
20
|
+
if (parentFolder) {
|
|
21
|
+
try {
|
|
22
|
+
await Filesystem.readdir({
|
|
23
|
+
path: parentFolder,
|
|
24
|
+
directory: Directory.Documents,
|
|
25
|
+
});
|
|
26
|
+
} catch {
|
|
27
|
+
// Parent folder doesn't exist, so subpath doesn't exist either
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const fullPath = getPath(path);
|
|
33
|
+
const result = await Filesystem.readdir({
|
|
34
|
+
path: fullPath,
|
|
35
|
+
directory: Directory.Documents,
|
|
36
|
+
});
|
|
37
|
+
return result.files.length >= 0;
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
createDirectory: async (path: string): Promise<void> => {
|
|
44
|
+
try {
|
|
45
|
+
if (parentFolder) {
|
|
46
|
+
try {
|
|
47
|
+
await Filesystem.mkdir({
|
|
48
|
+
path: parentFolder,
|
|
49
|
+
directory: Directory.Documents,
|
|
50
|
+
recursive: false,
|
|
51
|
+
});
|
|
52
|
+
} catch {
|
|
53
|
+
// Parent directory might already exist, that's fine
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const fullPath = getPath(path);
|
|
58
|
+
await Filesystem.mkdir({
|
|
59
|
+
path: fullPath,
|
|
60
|
+
directory: Directory.Documents,
|
|
61
|
+
recursive: true,
|
|
62
|
+
});
|
|
63
|
+
} catch (e) {
|
|
64
|
+
console.error('Error creating directory:', e);
|
|
65
|
+
throw e;
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
saveFile: async (filename: string, content: string, directory: string): Promise<void> => {
|
|
70
|
+
const fullDirectory = getPath(directory);
|
|
71
|
+
|
|
72
|
+
await Filesystem.writeFile({
|
|
73
|
+
path: `${fullDirectory}/${filename}`,
|
|
74
|
+
data: content,
|
|
75
|
+
directory: Directory.Documents,
|
|
76
|
+
encoding: 'utf8' as Encoding,
|
|
77
|
+
});
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Only Android asks for storage permission, so a test write tells whether it was granted
|
|
83
|
+
async function canWriteFiles(): Promise<boolean> {
|
|
84
|
+
if (!isAndroid) return true;
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
await Filesystem.writeFile({
|
|
88
|
+
path: 'permission_check.txt',
|
|
89
|
+
data: 'Testing permissions',
|
|
90
|
+
directory: Directory.Documents,
|
|
91
|
+
encoding: Encoding.UTF8,
|
|
92
|
+
});
|
|
93
|
+
} catch (error) {
|
|
94
|
+
console.error('Permission denied or error:', error);
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
await Filesystem.deleteFile({
|
|
100
|
+
path: 'permission_check.txt',
|
|
101
|
+
directory: Directory.Documents,
|
|
102
|
+
});
|
|
103
|
+
} catch (e) {
|
|
104
|
+
console.log('Cleanup error:', e);
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// In a regular browser, e.g. during development, the experiment keeps behaving like the web version
|
|
110
|
+
if (Capacitor.isNativePlatform()) {
|
|
111
|
+
registerNative({
|
|
112
|
+
createFileBackend,
|
|
113
|
+
canWriteFiles,
|
|
114
|
+
enterFullscreen: () => {
|
|
115
|
+
if (isAndroid) {
|
|
116
|
+
StatusBar.hide();
|
|
117
|
+
ImmersiveMode.enable();
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
exitFullscreen: () => {
|
|
121
|
+
if (isAndroid) {
|
|
122
|
+
StatusBar.show();
|
|
123
|
+
ImmersiveMode.disable();
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
}
|
package/src/quest.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
import { report } from '../core/development';
|
|
3
|
+
import type { CheckedParams, ParamSpec, ParamValues } from '../core/params';
|
|
4
|
+
import type { CompiledTimeline } from '../core/timeline';
|
|
5
|
+
|
|
6
|
+
// The run's params, or undefined for a component rendered outside a run
|
|
7
|
+
const ParamsContext = createContext<Record<string, unknown> | undefined>(undefined);
|
|
8
|
+
export const ParamsProvider = ParamsContext.Provider;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The values of a component's own params, typed like the experiment's. Declare them `as const`
|
|
12
|
+
* to type a param with `values` as one of them, and pass the same params to `defineComponent`.
|
|
13
|
+
*/
|
|
14
|
+
export function useParams<const P extends Record<string, ParamSpec>>(params: CheckedParams<P>): ParamValues<P> {
|
|
15
|
+
const values = useContext(ParamsContext);
|
|
16
|
+
for (const name of Object.keys(params)) {
|
|
17
|
+
if (values && !(name in values)) {
|
|
18
|
+
report(
|
|
19
|
+
`useParams reads the param '${name}', which neither the experiment nor a component in the timeline declares, so it has no value and the settings screen does not list it. Pass the same params to the component's defineComponent.`,
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return Object.fromEntries(Object.keys(params).map((name) => [name, values?.[name]])) as ParamValues<P>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The microphone chosen in MicrophoneCheck, which recordings use. */
|
|
27
|
+
export const AudioDeviceContext = createContext<string | undefined>(undefined);
|
|
28
|
+
export const useAudioDeviceId = () => useContext(AudioDeviceContext);
|
|
29
|
+
|
|
30
|
+
/** The compiled timeline of the run, whose components Upload builds the CSV files from. */
|
|
31
|
+
export const CompiledTimelineContext = createContext<CompiledTimeline>({
|
|
32
|
+
bytecode: { instructions: [], markers: {}, fullscreen: [] },
|
|
33
|
+
namedTrials: [],
|
|
34
|
+
components: new Map(),
|
|
35
|
+
});
|