Skip to content

Commit

Permalink
chore: update cloudflare worker, fix configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
grabbou committed Jan 26, 2025
1 parent c56b592 commit b7eaa2e
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,7 @@ dist

# Astro
.astro

# Cloudflare
.wrangler
.dev.vars
Binary file modified bun.lockb
Binary file not shown.
11 changes: 11 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ In order to run Slack example, you need to have `SLACK_API_TOKEN` and `SLACK_CHA
- `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

## Cloudflare Worker

We also provide a Cloudflare Worker example.

To run it, you need to have the `wrangler` CLI installed.

```bash
wrangler dev
```

This will start the worker and you can see the logs in the console.
67 changes: 67 additions & 0 deletions example/cloudflare-worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference types="@cloudflare/workers-types" />

import type { ExportedHandler } from '@cloudflare/workers-types'
import { execute } from 'flows-ai'
import * as process from 'process'

/**
* You must define the environment variables in the worker.
* You must also create a .dev.vars file in the root of the project with them,
* if you want to run this locally.
*/
type Worker = ExportedHandler<{
OPENAI_API_KEY: string
SLACK_API_KEY: string
SLACK_CHANNEL_ID: string
FIRECRAWL_API_KEY: string
}>

/**
* Cloudflare Worker example
*/
export default {
async fetch() {
return new Response('This worker does not handle fetch events.', { status: 200 })
},
async scheduled(_req, env) {
/**
* For simplicity, we run worker in nodejs_compat mode.
* This means we can use process.env to set the environment variables.
*/
process.env['OPENAI_API_KEY'] = env.OPENAI_API_KEY
process.env['SLACK_API_KEY'] = env.SLACK_API_KEY
process.env['SLACK_CHANNEL_ID'] = env.SLACK_CHANNEL_ID
process.env['FIRECRAWL_API_KEY'] = env.FIRECRAWL_API_KEY

/**
* Import the agents and flows after setting the environment variables.
*/
const { githubAgent, slackAgent, analysisAgent, npmAgent } = await import('./agents')
const { organizationAnalysisWithSlackMessageFlow } = await import('./flows')

/**
* Execute the flow and log the response (for debugging purposes in Cloudflare Dashboard)
*/
try {
const response = await execute(organizationAnalysisWithSlackMessageFlow, {
agents: {
githubAgent,
slackAgent,
npmAgent,
analysisAgent,
},
input: 'callstackincubator',
onFlowStart: (flow) => {
console.log({ agent: flow.agent.name, input: flow.input })
},
onFlowFinish: (flow, response) => {
console.log({ agent: flow.agent.name, response })
},
})
console.log({ response })
} catch (err) {
console.error(err)
}
},
} satisfies Worker
7 changes: 6 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"private": true,
"devDependencies": {
"@clack/prompts": "^0.9.1",
"flows-ai": "workspace:*"
"@cloudflare/workers-types": "^4.20250124.3",
"flows-ai": "workspace:*",
"wrangler": "^3.105.1"
},
"scripts": {
"dev": "wrangler dev ./cloudflare-worker.ts"
},
"author": "Mike Grabowski <[email protected]>",
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"extends": "../tsconfig.json"
}

11 changes: 11 additions & 0 deletions example/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name = "run-organization-analysis-with-slack-message"
account_id = "509f0d5925f8d9fa1d5bfb6c110a7f03"
workers_dev = true
compatibility_date = "2025-01-26"
compatibility_flags = [ "nodejs_compat" ]

[triggers]
crons = ["0 8 * * 2"]

[observability]
enabled = true

0 comments on commit b7eaa2e

Please sign in to comment.