-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* wip: handle no matches * chore: comments * refactor: new handler * wip: add full speak capabitilies * chore: comments * test: fix old * test: new unit * chore: rename test * feat: handling a randomized reprompt (#59) * feat: handling a randomized reprompt * feat: unit tests for no match randomize * feat: added randomize property to test object * Update noMatch.unit.ts refactor: removed randomize from one unit test to test undefined case instead
- Loading branch information
1 parent
e63819b
commit d093344
Showing
7 changed files
with
248 additions
and
20 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
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,38 @@ | ||
import { Context, Store } from '@voiceflow/client'; | ||
import _ from 'lodash'; | ||
|
||
import { S } from '@/lib/constants'; | ||
|
||
import { regexVariables, sanitizeVariables } from '../utils'; | ||
|
||
type Block = { | ||
blockID: string; | ||
noMatches?: string[]; | ||
randomize?: boolean; | ||
}; | ||
|
||
export const NoMatchHandler = () => ({ | ||
canHandle: (block: Block, context: Context) => { | ||
return Array.isArray(block.noMatches) && block.noMatches.length > (context.storage.get(S.NO_MATCHES_COUNTER) ?? 0); | ||
}, | ||
handle: (block: Block, context: Context, variables: Store) => { | ||
context.storage.produce((draft) => { | ||
draft[S.NO_MATCHES_COUNTER] = draft[S.NO_MATCHES_COUNTER] ? draft[S.NO_MATCHES_COUNTER] + 1 : 1; | ||
}); | ||
|
||
const speak = (block.randomize ? _.sample(block.noMatches) : block.noMatches?.[context.storage.get(S.NO_MATCHES_COUNTER) - 1]) || ''; | ||
|
||
const sanitizedVars = sanitizeVariables(variables.getState()); | ||
// replaces var values | ||
const output = regexVariables(speak, sanitizedVars); | ||
|
||
context.storage.produce((draft) => { | ||
draft[S.OUTPUT] += output; | ||
}); | ||
context.trace.speak(output); | ||
|
||
return block.blockID; | ||
}, | ||
}); | ||
|
||
export default () => NoMatchHandler(); |
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
Oops, something went wrong.