Skip to content

Commit

Permalink
Example runner
Browse files Browse the repository at this point in the history
  • Loading branch information
mellson committed Jan 18, 2024
1 parent 89e105a commit 3dc2880
Show file tree
Hide file tree
Showing 8 changed files with 1,224 additions and 1,453 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-tables-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@statelyai/agent': patch
---

Adds a convenient way to run the examples with `pnpm example ${exampleName}`. If no example name is provided, the script will print the available examples. Also, adds a fun little loading animation to the joke example.
31 changes: 28 additions & 3 deletions examples/joke.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import OpenAI from 'openai';
import { assign, fromPromise, createActor, setup, log, raise } from 'xstate';
import { assign, createActor, fromPromise, log, raise, setup } from 'xstate';
import { createAgent } from '../src';
import {
getRandomFunnyPhrase,
getRandomRatingPhrase,
} from '../src/helpers/jokeHelper';
import { loadingAnimation } from '../src/helpers/loader';

const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
Expand Down Expand Up @@ -32,7 +37,7 @@ const rateJoke = agent.fromChatCompletion(

const getTopic = fromPromise(async () => {
const topic = await new Promise<string>((res) => {
console.log('Give me a topic: \n\n');
console.log('Give me a joke topic:');
const listener = (data: Buffer) => {
const result = data.toString().trim();
process.stdin.off('data', listener);
Expand All @@ -56,6 +61,7 @@ const jokeMachine = setup({
jokes: string[];
desire: string | null;
lastRating: string | null;
loader: NodeJS.Timeout | null;
},
input: {} as { topic: string },
},
Expand All @@ -71,6 +77,7 @@ const jokeMachine = setup({
jokes: [],
desire: null,
lastRating: null,
loader: null,
}),
initial: 'waitingForTopic',
states: {
Expand All @@ -86,11 +93,17 @@ const jokeMachine = setup({
},
},
tellingJoke: {
entry: assign({ loader: () => loadingAnimation(getRandomFunnyPhrase()) }),
invoke: {
src: 'getJokeCompletion',
input: ({ context }) => context.topic,
onDone: {
actions: [
({ context }) => {
if (!context.loader) return;
clearInterval(context.loader);
console.log();
},
assign({
jokes: ({ context, event }) =>
context.jokes.concat(event.output.choices[0]!.message.content!),
Expand All @@ -102,11 +115,19 @@ const jokeMachine = setup({
},
},
rateJoke: {
entry: assign({
loader: () => loadingAnimation(getRandomRatingPhrase()),
}),
invoke: {
src: 'rateJoke',
input: ({ context }) => context.jokes[context.jokes.length - 1]!,
onDone: {
actions: [
({ context }) => {
if (!context.loader) return;
clearInterval(context.loader);
console.log();
},
assign({
lastRating: ({ event }) =>
event.output.choices[0]!.message.content!,
Expand Down Expand Up @@ -147,5 +168,9 @@ const jokeMachine = setup({
});

const actor = createActor(jokeMachine);

actor.start();
actor.subscribe((state) => {
if (state.matches('end')) {
process.exit();
}
});
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"build": "tsup src/index.ts --format cjs,esm --dts",
"lint": "tsc",
"test": "vitest run",
"example": "ts-node src/helpers/exampleRunner.ts",
"prepublishOnly": "tsup src/index.ts --dts"
},
"keywords": [],
Expand All @@ -22,7 +23,9 @@
"typescript": "^5.3.3"
},
"dependencies": {
"dotenv": "^16.3.1",
"openai": "^4.24.1",
"ts-node": "^10.9.2",
"xstate": "^5.3.1"
},
"publishConfig": {
Expand Down
Loading

0 comments on commit 3dc2880

Please sign in to comment.