This repository has been archived by the owner on Aug 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added event system (CORE-5263) (#28)
* wip: added EventsManager * fix: ambiguous exports * refactor: refactoring trace types * feat: added all interface methods * refactor: refactored to use common handler signature * style: linting * style: renaming classes * test: added tests for EventManager * test: unit tests for maximum coverage * style: linting errors Co-authored-by: Zhiheng Lu <[email protected]>
- Loading branch information
Showing
33 changed files
with
641 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { GeneralTrace as DBGeneralTrace, SpeakTrace, TraceType } from '@voiceflow/general-types'; | ||
import { SpeakType } from '@voiceflow/general-types/build/nodes/speak'; | ||
import htmlParse from 'html-parse-stringify'; | ||
|
||
import { DBResponseContext } from './types'; | ||
|
||
/** | ||
* WORK-AROUND function to deal with bug where enabling `tts` will cause the Audio Step's audio | ||
* file url to not generate. Better solution is to decouple the TTS and audio handlers in our | ||
* backend and remove `parseAudioStepSrc` and `adaptResponseContext` | ||
*/ | ||
export const parseAudioStepSrc = (trace: DBGeneralTrace): DBGeneralTrace => { | ||
if (trace.type !== TraceType.SPEAK) { | ||
return trace; | ||
} | ||
|
||
const node = htmlParse.parse(trace.payload.message)[0]; | ||
|
||
if (!node || node.name !== 'audio') { | ||
return { | ||
...trace, | ||
payload: { | ||
...trace.payload, | ||
type: SpeakType.MESSAGE, | ||
}, | ||
} as SpeakTrace; | ||
} | ||
|
||
const audioSrc = node.attrs.src; | ||
|
||
return { | ||
...trace, | ||
payload: { | ||
...trace.payload, | ||
type: SpeakType.AUDIO, | ||
src: audioSrc, | ||
}, | ||
}; | ||
}; | ||
|
||
/** | ||
* WORK-AROUND function, see `parseAudioStepSrc` above | ||
*/ | ||
export const adaptResponseContext = (context: DBResponseContext): DBResponseContext => ({ | ||
...context, | ||
trace: context.trace.map(parseAudioStepSrc), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { GeneralTrace as DBGeneralTrace, TraceType as DBTraceType } from '@voiceflow/general-types'; | ||
import { SpeakType } from '@voiceflow/general-types/build/nodes/speak'; | ||
|
||
import { GeneralTrace, ResponseContext, TraceType } from '@/lib/types'; | ||
|
||
export const extractAudioStep = (context: Omit<ResponseContext, 'trace'> & { trace: DBGeneralTrace[] }) => ({ | ||
...context, | ||
trace: context.trace.map((trace) => { | ||
if (trace.type !== DBTraceType.SPEAK) { | ||
return (trace as unknown) as GeneralTrace; | ||
} | ||
const { type, ...payload } = trace.payload; | ||
|
||
return ({ | ||
type: type === SpeakType.MESSAGE ? TraceType.SPEAK : TraceType.AUDIO, | ||
payload, | ||
} as unknown) as GeneralTrace; | ||
}), | ||
}); | ||
|
||
export default extractAudioStep; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,2 @@ | ||
import { GeneralTrace, SpeakTrace, TraceType } from '@voiceflow/general-types'; | ||
import { SpeakType } from '@voiceflow/general-types/build/nodes/speak'; | ||
import htmlParse from 'html-parse-stringify'; | ||
|
||
import { ResponseContext } from '../../types'; | ||
|
||
/** | ||
* WORK-AROUND function to deal with bug where enabling `tts` will cause the Audio Step's audio | ||
* file url to not generate. Better solution is to decouple the TTS and audio handlers in our | ||
* backend and remove `parseAudioStepSrc` and `adaptResponseContext` | ||
*/ | ||
export const parseAudioStepSrc = (trace: GeneralTrace): GeneralTrace => { | ||
if (trace.type !== TraceType.SPEAK) { | ||
return trace; | ||
} | ||
|
||
const node = htmlParse.parse(trace.payload.message)[0]; | ||
|
||
if (!node || node.name !== 'audio') { | ||
return { | ||
...trace, | ||
payload: { | ||
...trace.payload, | ||
type: SpeakType.MESSAGE, | ||
}, | ||
} as SpeakTrace; | ||
} | ||
|
||
const audioSrc = node.attrs.src; | ||
|
||
return { | ||
...trace, | ||
payload: { | ||
...trace.payload, | ||
type: SpeakType.AUDIO, | ||
src: audioSrc, | ||
}, | ||
}; | ||
}; | ||
|
||
/** | ||
* WORK-AROUND function, see `parseAudioStepSrc` above | ||
*/ | ||
export const adaptResponseContext = (context: ResponseContext) => ({ | ||
...context, | ||
trace: context.trace.map(parseAudioStepSrc), | ||
}); | ||
export { adaptResponseContext } from './addAudioSrc'; | ||
export { extractAudioStep } from './extractAudioStep'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { GeneralTrace as DBGeneralTrace } from '@voiceflow/general-types'; | ||
|
||
import { ResponseContext } from '@/lib/types'; | ||
|
||
export type DBResponseContext = Omit<ResponseContext, 'trace'> & { | ||
trace: DBGeneralTrace[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { GeneralTrace, TraceMap, TraceType } from '@/lib/types'; | ||
|
||
import Context from '../Context'; | ||
|
||
export type TraceEventHandler<T extends TraceType, V extends Record<string, any>> = (object: TraceMap[T], context: Context<V>) => void; | ||
|
||
export type GeneralTraceEventHandler<V extends Record<string, any>> = (object: GeneralTrace, context: Context<V>) => void; | ||
|
||
type _Map<T extends Record<string, any>, K extends TraceType = TraceType> = Map<K, Array<TraceEventHandler<K, T>>>; | ||
|
||
export class EventManager<V extends Record<string, any>> { | ||
private specHandlers: _Map<V>; | ||
|
||
private genHandlers: GeneralTraceEventHandler<V>[]; | ||
|
||
constructor() { | ||
this.specHandlers = new Map(); | ||
const traceTypeVals = Object.keys(TraceType).map((type) => type.toLowerCase()) as TraceType[]; | ||
traceTypeVals.forEach((traceType) => this.specHandlers.set(traceType, [])); | ||
|
||
this.genHandlers = []; | ||
} | ||
|
||
on<T extends TraceType>(event: T, handler: TraceEventHandler<T, V>) { | ||
const handlerList = this.specHandlers.get(event)! as TraceEventHandler<T, V>[]; | ||
handlerList.push(handler); | ||
} | ||
|
||
onAny(handler: GeneralTraceEventHandler<V>) { | ||
this.genHandlers.push(handler); | ||
} | ||
|
||
handle<T extends TraceType>(trace: TraceMap[T], context: Context<V>) { | ||
this.specHandlers.get(trace.type)!.forEach((handler: TraceEventHandler<T, V>) => handler(trace, context)); | ||
|
||
this.genHandlers.forEach((handler: GeneralTraceEventHandler<V>) => handler(trace, context)); | ||
} | ||
} | ||
|
||
export default EventManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { AudioTrace, AudioTraceHandler } from '@/lib/types'; | ||
|
||
export const invokeAudioHandler = (trace: AudioTrace, handler: AudioTraceHandler) => { | ||
const { | ||
payload: { message, src }, | ||
} = trace; | ||
return handler(message, src); | ||
}; | ||
export default invokeAudioHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { BlockTrace } from '@voiceflow/general-types'; | ||
|
||
export type BlockTraceHandler = (blockID: BlockTrace['payload']['blockID']) => any; | ||
import { BlockTrace, BlockTraceHandler } from '@/lib/types'; | ||
|
||
export const invokeBlockHandler = (trace: BlockTrace, handler: BlockTraceHandler) => handler(trace.payload.blockID); | ||
export default invokeBlockHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { ChoiceTrace } from '@voiceflow/general-types'; | ||
|
||
export type ChoiceTraceHandler = (choices: ChoiceTrace['payload']['choices']) => any; | ||
import { ChoiceTrace, ChoiceTraceHandler } from '@/lib/types'; | ||
|
||
export const invokeChoiceHandler = (trace: ChoiceTrace, handler: ChoiceTraceHandler) => handler(trace.payload.choices); | ||
export default invokeChoiceHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { DebugTrace } from '@voiceflow/general-types'; | ||
|
||
export type DebugTraceHandler = (message: DebugTrace['payload']['message']) => any; | ||
import { DebugTrace, DebugTraceHandler } from '@/lib/types'; | ||
|
||
export const invokeDebugHandler = (trace: DebugTrace, handler: DebugTraceHandler) => handler(trace.payload.message); | ||
export default invokeDebugHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { ExitTrace } from '@voiceflow/general-types'; | ||
import { EndTrace, EndTraceHandler } from '@/lib/types'; | ||
|
||
export type EndTraceHandler = () => any; | ||
|
||
export const invokeEndHandler = (_: ExitTrace, handler: EndTraceHandler) => handler(); | ||
export const invokeEndHandler = (_: EndTrace, handler: EndTraceHandler) => handler(); | ||
export default invokeEndHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { FlowTrace } from '@voiceflow/general-types'; | ||
|
||
export type FlowTraceHandler = (diagramID: FlowTrace['payload']['diagramID']) => any; | ||
import { FlowTrace, FlowTraceHandler } from '@/lib/types'; | ||
|
||
export const invokeFlowHandler = (trace: FlowTrace, handler: FlowTraceHandler) => handler(trace.payload.diagramID); | ||
export default invokeFlowHandler; |
Oops, something went wrong.