Configuration object for the machine setup
Optional
actions?: {Optional
guards?: {Optional
schemas?: unknownOptional
types?: Omit<SetupTypes<TContext, {An object containing the createMachine
function
Creates an Arvo-compatible XState machine.
The configuration object for the machine
An ArvoMachine instance
setupArvoMachine
is a crucial function in the Arvo ecosystem, designed to create
synchronous state machine orchestrations for Arvo's event-driven architecture.
It builds upon XState, providing a tailored implementation that:
Key features:
enqueueArvoEvent
for Arvo event handlingWhile setupArvoMachine
is based on XState's setup
and createMachine
functions,
it includes Arvo-specific modifications and restrictions. For a deeper understanding
of the underlying XState concepts, refer to the official XState documentation:
Here's a comprehensive example demonstrating how to use setupArvoMachine
:
import { setupArvoMachine } from 'arvo-xstate'
import { createArvoOrchestratorContract, ArvoErrorSchema, createArvoContract } from 'arvo-core'
import { z } from 'zod'
// Define the LLM orchestrator contract
const llmContract = createArvoOrchestratorContract({
uri: `#/orchestrators/llm/`,
type: 'llm',
versions: {
'0.0.1': {
init: z.object({
request: z.string(),
llm: z.enum(['gpt-4', 'gpt-4o']),
}),
complete: z.object({
response: z.string(),
})
}
}
})
// Define the OpenAI service contract
const openAiContract = createArvoContract({
uri: `#/services/openai`,
type: 'com.openai.completions',
versions: {
'0.0.1': {
accepts: z.object({
request: z.string()
}),
emits: {
'evt.openai.completions.success': z.object({
response: z.string(),
})
}
}
}
})
const machineId = 'machineV100'
// Set up the Arvo machine
const llmMachine = setupArvoMachine({
contracts: {
self: llmContract.version('0.0.1'),
services: {
openAiContract.version('0.0.1'),
}
},
types: {
context: {} as {
request: string,
llm: string,
response: string | null,
errors: z.infer<typeof ArvoErrorSchema>[]
},
tags: {} as 'pending' | 'success' | 'error',
},
actions: {
log: ({context, event}) => console.log({context, event})
},
guards: {
isValid: ({context, event}) => Boolean(context.request)
}
}).createMachine({
id: machineId,
context: ({input}) => ({
request: input.request,
llm: input.llm,
response: null,
errors: [],
}),
initial: 'validate',
states: {
validate: {
always: [
{
guard: 'isValid',
target: 'llm',
},
{
target: 'error',
}
]
},
llm: {
entry: [
{
type: 'log',
},
emit(({context}) => ({
type: 'com.openai.completions',
data: {
request: context.request,
},
}))
],
on: {
'evt.openai.completions.success': {
actions: [
assign({response: ({event}) => event.response})
],
target: 'done'
},
'sys.com.openai.completions.error': {
actions: [
assign({errors: ({context, event}) => [...context.errors, event.body]})
],
target: 'error'
}
}
},
done: {
type: 'final'
},
error: {
type: 'final'
},
}
});
This example demonstrates:
emit
bound with Arvo contracts for event emitting and event handling via transitions
Establishes the foundation for creating Arvo-compatible state machines.
This function configures the core elements of an Arvo state machine, including built-in actions like
enqueueArvoEvent
, and enforces Arvo-specific constraints to ensure compatibility with the Arvo event-driven system.