Skip to content

Commit

Permalink
chore: add few more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
grabbou committed Jan 26, 2025
1 parent 60d4425 commit 2e731a4
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 11 deletions.
1 change: 1 addition & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ In order to run Slack example, you need to have `SLACK_API_TOKEN` and `SLACK_CHA
- `bun run-organization-analysis.ts callstackincubator` - Analyzing an entire GitHub organization
- `bun run-project-analysis.ts facebook/react-native` - Analyzing a single GitHub project
- `bun run-organization-analysis-with-slack-message.ts callstackincubator` - Analyzing an entire GitHub organization and sending the report to Slack
- `bun run-newsletter-analysis.ts` - Summarizing the "This Week in React" newsletter content and sending it to Slack

## Cloudflare Worker

Expand Down
11 changes: 10 additions & 1 deletion example/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export const slackAgent = agent({
system: `
You are a Slack agent.
You can send messages to Slack.
Your messages should be concise and to the point.
You always start with a friendly greeting in a casual, developer-friendly tone.
`,
tools: createAISDKTools(slack),
Expand All @@ -63,3 +62,13 @@ export const analysisAgent = agent({
Focus on identifying patterns, trends, and key insights.
`,
})

export const contentAgent = agent({
model: openai('gpt-4o'),
system: `
You are a content analysis agent.
You can analyze web content and extract information from it.
Your summaries are clear, focused, and highlight what's most relevant for developers.
`,
tools: createAISDKTools(firecrawl),
})
48 changes: 47 additions & 1 deletion example/flows.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { forEach, parallel, sequence } from 'flows-ai/flows'
import s from 'dedent'
import { forEach, oneOf, parallel, sequence } from 'flows-ai/flows'
import { z } from 'zod'

/**
Expand Down Expand Up @@ -77,3 +78,48 @@ export const organizationAnalysisWithSlackMessageFlow = sequence([
input: `Send the report to the channel "${process.env['SLACK_CHANNEL_ID']}"`,
},
])

/**
* Flow for analyzing newsletter content and creating summaries
*/
export const newsletterSummaryFlow = sequence([
{
agent: 'contentAgent',
input: s`
Extract all links from the provided newsletter URL.
For each link, determine if it is an article (📜) or release (📦).
Return first 5 links with URL whose type is either "article" or "release".
Skip any links that point to "x.com" or "twitter.com".
`,
},
forEach({
item: z.object({
url: z.string().describe('The URL of the content'),
type: z.enum(['article', 'release']).describe('The type of content'),
}),
input: oneOf([
{
when: 'It is an article',
input: {
agent: 'contentAgent',
input: 'Create a bullet-point summary of the key points from this article.',
},
},
{
when: 'It is a release',
input: {
agent: 'contentAgent',
input:
'Analyze the release notes, identify any breaking changes, and highlight the most important new feature.',
},
},
]),
}),
{
agent: 'slackAgent',
input: s`
Send all summaries to the channel "${process.env['SLACK_CHANNEL_ID']}".
The message must start with the newsletter issue number.
`,
},
])
23 changes: 23 additions & 0 deletions example/run-newsletter-analysis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { execute } from 'flows-ai'

import { contentAgent, slackAgent } from './agents'
import { newsletterSummaryFlow } from './flows'

/**
* Summarize the newsletter content and send it to Slack
*/
const response = await execute(newsletterSummaryFlow, {
agents: {
contentAgent,
slackAgent,
},
input: 'https://thisweekinreact.com/newsletter/218',
onFlowStart: (flow) => {
console.log('Executing', flow.agent.name)
},
onFlowFinish(flow, result) {
console.log('Finished', flow.agent.name, result)
},
})

console.log(response)
4 changes: 0 additions & 4 deletions example/run-organization-analysis-with-slack-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ if (!projectName) {
)
}

if (!process.env['SLACK_API_TOKEN']) {
throw new Error('Please set SLACK_API_TOKEN environment variable.')
}

const channelId = process.env['SLACK_CHANNEL_ID']
if (!channelId) {
throw new Error('Please set SLACK_CHANNEL_ID environment variable.')
Expand Down
5 changes: 0 additions & 5 deletions packages/flows-ai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,6 @@ export const makeOneOfAgent: AgentFactory<OneOfFlowDefinition> =
.describe('The index of the condition that is met, or -1 if no condition was met.'),
}),
})
console.log(s`
Here is the context: ${JSON.stringify(context)}
Here is the array of conditions: ${JSON.stringify(conditions)}
`)
console.log(condition.object)
const index = condition.object.index
if (index === -1) {
throw new Error('No condition was satisfied')
Expand Down

0 comments on commit 2e731a4

Please sign in to comment.