1- import { Effect , Schema } from "effect"
1+ import { Effect , Schema , SchemaGetter } from "effect"
22import { Route } from "../route/client.js"
33import { Auth } from "../route/auth.js"
44import { Endpoint } from "../route/endpoint.js"
@@ -148,6 +148,24 @@ const Interaction = Schema.StructWithRest(
148148 } ) ,
149149 [ Schema . Record ( Schema . String , Schema . Unknown ) ] ,
150150)
151+ const KNOWN_EVENT_TYPES = new Set ( [
152+ "step.start" ,
153+ "step.delta" ,
154+ "step.stop" ,
155+ "interaction.created" ,
156+ "interaction.status_update" ,
157+ "interaction.completed" ,
158+ "error" ,
159+ ] )
160+ // Google may add stream event types. Unknown types are ignored, while a malformed known type still fails decoding.
161+ const UnknownEvent = Schema . Struct ( {
162+ event_type : Schema . String . check ( Schema . makeFilter ( ( type ) => ! KNOWN_EVENT_TYPES . has ( type ) ) ) ,
163+ } ) . pipe (
164+ Schema . decodeTo ( Schema . Struct ( { event_type : Schema . Literal ( "unknown" ) } ) , {
165+ decode : SchemaGetter . transform ( ( ) => ( { event_type : "unknown" as const } ) ) ,
166+ encode : SchemaGetter . forbidden ( ( ) => "Unknown Google Interactions stream events are decode-only" ) ,
167+ } ) ,
168+ )
151169const Event = Schema . Union ( [
152170 Schema . Struct ( { event_type : Schema . Literal ( "step.start" ) , index : Schema . Number , step : OutputStep } ) ,
153171 Schema . Struct ( { event_type : Schema . Literal ( "step.delta" ) , index : Schema . Number , delta : Delta } ) ,
@@ -163,6 +181,7 @@ const Event = Schema.Union([
163181 } ) ,
164182 Schema . Struct ( { event_type : Schema . Literal ( "interaction.completed" ) , interaction : Interaction } ) ,
165183 Schema . Struct ( { event_type : Schema . Literal ( "error" ) , error : Schema . Unknown } ) ,
184+ UnknownEvent ,
166185] )
167186type Event = typeof Event . Type
168187
@@ -475,6 +494,7 @@ const step = Effect.fnUntraced(function* (state: ParserState, event: Event) {
475494 return yield * onStop ( state , event . index )
476495 case "interaction.created" :
477496 case "interaction.status_update" :
497+ case "unknown" :
478498 return [ state , [ ] ] satisfies StepResult
479499 case "error" :
480500 return yield * new AIError ( {
0 commit comments