A TypeScript conversation framework. This implements a set of interfaces for transmitting the state of a constructed conversation between a client and a server. It provides concrete classes that can parse that information and turn it into useful control flow operations. The framework is usually used for hybrid offline/online conversations with 'bots'.
The conversation framework has a few concepts:
The unit of work is a {@link IFormation}. Each and every formation will contain a single {@link IDiscourse}, which is a interaction with the user, and a {@link ILink} that provides the identification value of the next formation to process. As the formations are processed, a {@link IContext} is persisted to acculate data between discourse interactions. The links can use the built up context to make decisions on what formation to move to next. A {@link ICache} provides an in-memory store of recently fetched formations. The cache uses a {@link IProvider} that can retrieve missing messages. The pseudo-code for processing a conversation tree is as follows:
formation = cache.fetch(startId)
context = {}
do {
context = processDiscourse(formation.discourse, context);
id, context = processLink(formation.link, context);
formation = cache.fetch(id);
} until(theEndOfTime)
A conversation tree will always have closed loops by design. It is up to content creators to produce sensible flows of discourse to perform with the user. The library provides utility functions to implement the above pseudo-code but most usage will derive from the {@link Processor} class:
class Processor extends FormationProcessor<string> {
// Override the abstract discourse processing functions
protected abstract statement(
discourse: IDiscourseStatementImmutable<Data>,
{...context}: IContextImmutable
): IContextImmutable {
// Process the discourse data. This is generic and the discourse nodes can transmit any type of specified data
console.log(discourse.data);
// We add to and forward on the context data. This can be used to build up result from discourse interactions
// Eventually a link can process the context to determine the next formation
return { addToContext: 1, ...context };
}
// ... other processing functions
}
function loop() {
const processor = new Processor({ cache, start: 'some-starting-guid });
for await (const id of processor) {
console.log(id);
}
}
loop();