Skip to content

Commit b7eaa2e

Browse files
committed
chore: update cloudflare worker, fix configuration
1 parent c56b592 commit b7eaa2e

File tree

7 files changed

+100
-1
lines changed

7 files changed

+100
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,7 @@ dist
176176

177177
# Astro
178178
.astro
179+
180+
# Cloudflare
181+
.wrangler
182+
.dev.vars

bun.lockb

23 KB
Binary file not shown.

example/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,14 @@ In order to run Slack example, you need to have `SLACK_API_TOKEN` and `SLACK_CHA
2727
- `bun run-project-analysis.ts facebook/react-native` - Analyzing a single GitHub project
2828
- `bun run-organization-analysis-with-slack-message.ts callstackincubator` - Analyzing an entire GitHub organization and sending the report to Slack
2929

30+
## Cloudflare Worker
31+
32+
We also provide a Cloudflare Worker example.
33+
34+
To run it, you need to have the `wrangler` CLI installed.
35+
36+
```bash
37+
wrangler dev
38+
```
39+
40+
This will start the worker and you can see the logs in the console.

example/cloudflare-worker.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
2+
/// <reference types="@cloudflare/workers-types" />
3+
4+
import type { ExportedHandler } from '@cloudflare/workers-types'
5+
import { execute } from 'flows-ai'
6+
import * as process from 'process'
7+
8+
/**
9+
* You must define the environment variables in the worker.
10+
* You must also create a .dev.vars file in the root of the project with them,
11+
* if you want to run this locally.
12+
*/
13+
type Worker = ExportedHandler<{
14+
OPENAI_API_KEY: string
15+
SLACK_API_KEY: string
16+
SLACK_CHANNEL_ID: string
17+
FIRECRAWL_API_KEY: string
18+
}>
19+
20+
/**
21+
* Cloudflare Worker example
22+
*/
23+
export default {
24+
async fetch() {
25+
return new Response('This worker does not handle fetch events.', { status: 200 })
26+
},
27+
async scheduled(_req, env) {
28+
/**
29+
* For simplicity, we run worker in nodejs_compat mode.
30+
* This means we can use process.env to set the environment variables.
31+
*/
32+
process.env['OPENAI_API_KEY'] = env.OPENAI_API_KEY
33+
process.env['SLACK_API_KEY'] = env.SLACK_API_KEY
34+
process.env['SLACK_CHANNEL_ID'] = env.SLACK_CHANNEL_ID
35+
process.env['FIRECRAWL_API_KEY'] = env.FIRECRAWL_API_KEY
36+
37+
/**
38+
* Import the agents and flows after setting the environment variables.
39+
*/
40+
const { githubAgent, slackAgent, analysisAgent, npmAgent } = await import('./agents')
41+
const { organizationAnalysisWithSlackMessageFlow } = await import('./flows')
42+
43+
/**
44+
* Execute the flow and log the response (for debugging purposes in Cloudflare Dashboard)
45+
*/
46+
try {
47+
const response = await execute(organizationAnalysisWithSlackMessageFlow, {
48+
agents: {
49+
githubAgent,
50+
slackAgent,
51+
npmAgent,
52+
analysisAgent,
53+
},
54+
input: 'callstackincubator',
55+
onFlowStart: (flow) => {
56+
console.log({ agent: flow.agent.name, input: flow.input })
57+
},
58+
onFlowFinish: (flow, response) => {
59+
console.log({ agent: flow.agent.name, response })
60+
},
61+
})
62+
console.log({ response })
63+
} catch (err) {
64+
console.error(err)
65+
}
66+
},
67+
} satisfies Worker

example/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
"private": true,
44
"devDependencies": {
55
"@clack/prompts": "^0.9.1",
6-
"flows-ai": "workspace:*"
6+
"@cloudflare/workers-types": "^4.20250124.3",
7+
"flows-ai": "workspace:*",
8+
"wrangler": "^3.105.1"
9+
},
10+
"scripts": {
11+
"dev": "wrangler dev ./cloudflare-worker.ts"
712
},
813
"author": "Mike Grabowski <[email protected]>",
914
"license": "MIT",

example/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
22
"extends": "../tsconfig.json"
33
}
4+

example/wrangler.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name = "run-organization-analysis-with-slack-message"
2+
account_id = "509f0d5925f8d9fa1d5bfb6c110a7f03"
3+
workers_dev = true
4+
compatibility_date = "2025-01-26"
5+
compatibility_flags = [ "nodejs_compat" ]
6+
7+
[triggers]
8+
crons = ["0 8 * * 2"]
9+
10+
[observability]
11+
enabled = true

0 commit comments

Comments
 (0)