Giter VIP home page Giter VIP logo

fsm's Introduction

FSM

Version MIT License Build Status codecov Go Report Card Join the community on Spectrum

FSM

This package contains a simple interface for a finite-state machine in Go.

What's a Finite-state Machine?

[A finite-state machine] is an abstract machine that can be in exactly one of a finite number of states at any given time. The FSM can change from one state to another in response to some external inputs; the change from one state to another is called a transition. An FSM is defined by a list of its states, its initial state, and the conditions for each transition.

Wikipedia

The FSM library was built as purely against the definition of a formal finite-state machine as possible, so the Wikipedia definition holds true for this library.

Why Use a Finite-state Machine?

FSM was specifically made to build light-weight / generic conversational interfaces (think chatbots), so if that's what your trying to do you'll find this library quite nice.

Building a conversational interface as a finite-state machine will reduce a ton of cognitive overhead, as at any given point you only have to concern yourself with the current step in the conversation.

What is most attractive about FSM is the fact that you can build robust conversational interfaces that run on any platform with a single codebase.

Getting Started

If you're looking to build a chatbot, check out the getting started repository.

This README is more high-level and won't get into the details you'll want to start building.

Supported Platforms

Currently there is support for the following platforms:

Roadmap

We're soon planning to support the following platforms.

These targets libraries are relatively easy to build, so when the next hot platform comes out, your conversational interface can be easily adapted to it!

fsm's People

Contributors

brandonromano avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fsm's Issues

StateMachine -> []BuildState

Currently we're working with this interface:

type StateMachine map[string]BuildState

This is problematic with the current State interface:

type State struct {
	Slug          string
	EntryAction   func() error
	ReentryAction func() error
	Transition    func(interface{}) *State
}

Slug is duplicated here.

I've been avoiding this duplication by generating the map, but I've just realized we should just be doing this inside of the Targets, to remove the cognitive overhead from doing that.

// Build stateMap
stateMap := make(map[string]fsm.BuildState, 0)
for _, buildState := range stateMachine {
	stateMap[buildState(nil, nil).Slug] = buildState
}
  • Update core
  • Update README
  • Update CLI
  • Update Facebook
  • Update Alexa

ReentryAction func() error

  • Update Core
  • Update Facebook
  • Update CLI
  • Update Alexa

In a state, I think it would be beneficial to have a ReentryAction.

In a formal state machine, you simply can say "You can only transition through these options". In the event you can't go through one of the options, the state machine has failed (thus proving your regex / language definition / whatever your state machine is being used for invalid).

In our use case, USERS are inputting their responses. And in all of our targets, they can input ANYTHING at any time.

This is problematic, because we don't just want to say "well, now you're done" and shut off the bot to them, we want to provide them a way back into the bot experience.

This proposal would lead to code that looks something like this:

When the Transition function returns nil, ReentryAction gets called.

package states

import (
	"github.com/fsm/emitable"
	"github.com/fsm/fsm"
)

func GetSampleState(emitter fsm.Emitter, traverser fsm.Traverser) *fsm.State {
	return &fsm.State{
		EntryAction: func() error {
			emitter.Emit(emitable.Image{URL: "https://i.imgur.com/apvk5n0.gif"})

			emitter.Emit(emitable.Typing{Enabled: true})
			emitter.Emit("Greetings traveler, welcome to my shop!")

			emitter.Emit(emitable.Typing{Enabled: true})
			emitter.Emit("Looks like you want to buy some potions.")

			emitter.Emit(emitable.QuickReply{
				Message:       "How many would you like to buy?",
				RepliesFormat: "I sell in quantities of %v",
				Replies:       []string{"1", "5", "10"},
			})
			return nil
		},
		ReentryAction: func() error {
			emitter.Emit(emitable.Typing{Enabled: true})
			emitter.Emit("I'm not quite sure what that means...")

			emitter.Emit(emitable.QuickReply{
				Message:       "What will it be?",
				RepliesFormat: "I sell in quantities of %v",
				Replies:       []string{"1", "5", "10"},
			})
			return nil
		// ...
	}
}```

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.