chore: gptd data typing and tests fixing
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user