@statelyai/agent
Version:
Stateful agents that make decisions based on finite-state machine models
40 lines (36 loc) • 884 B
text/typescript
import { createAgent, fromDecision } from '../src';
import { z } from 'zod';
import { setup, createActor } from 'xstate';
import { openai } from '@ai-sdk/openai';
const agent = createAgent({
name: 'simple',
model: openai('gpt-3.5-turbo-16k-0613'),
events: {
'agent.thought': z.object({
text: z.string().describe('The text of the thought'),
}),
},
});
const machine = setup({
actors: { agent: fromDecision(agent) },
}).createMachine({
initial: 'thinking',
states: {
thinking: {
invoke: {
src: 'agent',
input: 'Think about a random topic, and then share that thought.',
},
on: {
'agent.thought': {
actions: ({ event }) => console.log(event.text),
target: 'thought',
},
},
},
thought: {
type: 'final',
},
},
});
const actor = createActor(machine).start();