chore: gptd data typing and tests fixing

This commit is contained in:
Björn Otgaar
2026-01-13 12:03:49 +01:00
parent 79d889c10e
commit 108fdeeedc
2 changed files with 76 additions and 35 deletions

View File

@@ -66,17 +66,26 @@ export async function playExperiment(): Promise<void> {
}
/**
* Types for the experiment stream messages
*/
export type PhaseUpdate = { type: 'phase_update'; id: string };
export type GoalUpdate = { type: 'goal_update'; id: string };
export type TriggerUpdate = { type: 'trigger_update'; id: string; achieved: boolean };
export type CondNormsStateUpdate = { type: 'cond_norms_state_update'; norms: { id: string; active: boolean }[] };
export type ExperimentStreamData = PhaseUpdate | GoalUpdate | TriggerUpdate | CondNormsStateUpdate | Record<string, unknown>;
/**
* A hook that listens to the experiment stream and logs data to the console.
* It does not render anything.
*/
export function useExperimentLogger(onUpdate?: (data: any) => void) {
export function useExperimentLogger(onUpdate?: (data: ExperimentStreamData) => void) {
useEffect(() => {
const eventSource = new EventSource(`${API_BASE}/experiment_stream`);
eventSource.onmessage = (event) => {
try {
const parsedData = JSON.parse(event.data);
const parsedData = JSON.parse(event.data) as ExperimentStreamData;
if (onUpdate) {
console.log(event.data);
onUpdate(parsedData);