Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More XState v5 examples #4345

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/workflow-slack-linear/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
OPENAI_ORGANIZATION="..."
OPENAI_API_KEY="..."
125 changes: 125 additions & 0 deletions examples/workflow-slack-linear/machine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { assign, createMachine, fromPromise } from 'xstate';

const createSlackPost = fromPromise(
async ({
input
}: {
input: {
channel: string;
};
}) => {
// TODO: add Slack integration here
}
);

export const machine = createMachine({
id: 'linear-issues-daily-slack-alert',
types: {
input: {} as {
text: string;
},
context: {} as {
issues: { nodes: any[] } | null;
},
actors: {} as
| {
src: 'Linear.getIssues';
logic: any;
}
| {
src: 'Slack.postMessage';
logic: typeof createSlackPost;
}
| {
src: 'cron';
logic: any;
},
events: {} as { type: 'cron' }
},
context: ({ input }) => ({
issues: null
}),
invoke: {
src: 'cron',
input: {
cron: '0 9 * * 1,2,3,4,5'
}
},
initial: 'Get in-progress issues',
states: {
idle: {
on: {
cron: { target: 'Get in-progress issues' }
}
},
'Get in-progress issues': {
invoke: {
src: 'Linear.getIssues',
input: ({ context }) => ({
first: 20,
filter: {
team: {
id: {
// To get your Team id from within Linear, hit CMD+K and "Copy model UUID"
eq: '<your-team-uuid>'
}
},
assignee: {
email: {
eq: '<assignee-email-address>'
}
},
state: {
name: {
eq: 'In Progress'
}
}
}
}),
onDone: {
actions: assign({
issues: ({ event }) => event.output
}),
target: 'Post to Slack'
}
}
},
'Post to Slack': {
invoke: {
src: 'Slack.postMessage',
input: ({ context }) => ({
channel: process.env.SLACK_CHANNEL_ID!,
// Include text for notifications and blocks to get a rich Slack message in the channel
text: `You have ${
context.issues!.nodes.length
} 'In Progress' issues in Linear!`,
// Create rich Slack messages with the Block Kit builder https://app.slack.com/block-kit-builder/
blocks: context.issues!.nodes.flatMap((issue) => [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `⏳ *${issue.title}*`
},
accessory: {
type: 'button',
text: {
type: 'plain_text',
text: 'View issue',
emoji: true
},
value: 'click_me_123',
url: issue.url,
action_id: 'button-action'
}
},
{
type: 'divider'
}
])
})
},
onDone: { target: 'idle' }
}
}
});
16 changes: 16 additions & 0 deletions examples/workflow-slack-linear/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "workflow-slack-linear",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@linear/sdk": "^8.0.0",
"xstate": "5.0.0-beta.33"
}
}
79 changes: 79 additions & 0 deletions examples/workflow-slack-linear/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/workflow-summarize-openai/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
OPENAI_ORGANIZATION="..."
OPENAI_API_KEY="..."
95 changes: 95 additions & 0 deletions examples/workflow-summarize-openai/machine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { assign, createMachine, fromPromise } from 'xstate';
import OpenAI from 'openai';

const openai = new OpenAI({
organization: process.env.OPENAI_ORGANIZATION,
apiKey: process.env.OPENAI_API_KEY
});

const createChatCompletion = fromPromise(
({
input
}: {
input: OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming;
}) => openai.chat.completions.create(input)
);

const createSlackPost = fromPromise(
async ({
input
}: {
input: {
channel: string;
};
}) => {
// TODO: add Slack integration here
}
);

export const machine = createMachine({
id: 'openai-summarizer',
types: {
input: {} as {
text: string;
},
context: {} as {
text: string;
summary: string | null;
},
actors: {} as
| {
src: 'OpenAI.createChatCompletion';
logic: typeof createChatCompletion;
}
| {
src: 'Slack.postMessage';
logic: typeof createSlackPost;
}
},
context: ({ input }) => ({
text: input.text,
summary: null
}),
initial: 'Generating summary',
states: {
'Generating summary': {
invoke: {
src: 'OpenAI.createChatCompletion',
input: ({ context }) => ({
model: 'gpt-3.5-turbo-16k',
messages: [
{
role: 'user',
content: `Summarize the following text with the most unique and helpful points, into a numbered list of key points and takeaways: \n ${context.text}`
}
]
}),
onDone: {
target: 'Posting to Slack',
actions: assign({
summary: ({ event }) => event.output.choices[0].message.content
})
},
onError: {
target: 'error'
}
}
},
'Posting to Slack': {
invoke: {
src: 'Slack.postMessage',
input: ({ context }) => ({
channel: '<channel ID>',
text: context.summary
})
},
onDone: { target: 'final' }
},
error: {
// ...
},
done: {
type: 'final'
}
}
});
16 changes: 16 additions & 0 deletions examples/workflow-summarize-openai/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "workflow-summarize-openai",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"openai": "^4.11.1",
"xstate": "5.0.0-beta.33"
}
}
Loading