Skip to content

Instantly share code, notes, and snippets.

@elliottsj
Last active April 3, 2023 15:12
Show Gist options
  • Save elliottsj/48d3cc02b57c2db7b9df650d61e0305a to your computer and use it in GitHub Desktop.
Save elliottsj/48d3cc02b57c2db7b9df650d61e0305a to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const fetchMachine = Machine({
id: 'chat',
initial: 'initial',
context: {
messages: [],
currentMessage: 'x',
model: ''
},
states: {
initial: {
on: {
FETCH: 'loadingChatHistory'
}
},
loadingChatHistory: {
on: {
CHAT_HISTORY_RESOLVE: 'idle',
CHAT_HISTORY_REJECT: 'error'
}
},
idle: {
on: {
INPUT_CHANGE: [
{ target: 'idle', cond: 'isInputEmpty' },
{ target: 'pendingUserMessage', cond: 'isInputNotEmpty' },
],
TEMPERATURE_CHANGE: [
{ target: 'idle', cond: 'isInputEmpty' },
{ target: 'pendingUserMessage', cond: 'isInputNotEmpty' },
]
}
},
pendingUserMessage: {
on: {
INPUT_CHANGE: [
{ target: 'idle', cond: 'isInputEmpty' },
{ target: 'pendingUserMessage', cond: 'isInputNotEmpty' },
],
TEMPERATURE_CHANGE: [
{ target: 'idle', cond: 'isInputEmpty' },
{ target: 'pendingUserMessage', cond: 'isInputNotEmpty' },
],
SEND: 'streamingBotResponse',
}
},
streamingBotResponse: {
on: {
RECEIVE_BOT_TEXT: 'streamingBotResponse',
COMPLETE_BOT_RESPONSE: 'idle',
}
},
error: {
on: {
RETRY: 'loadingChatHistory'
}
}
}
}, {
guards: {
isInputEmpty: (context, event) => {
return context.currentMessage === '';
},
isInputNotEmpty: (context, event) => {
return context.currentMessage !== '';
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment