Skip to content
Merged
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
26 changes: 23 additions & 3 deletions bun.lock

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

1 change: 1 addition & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Orchestrate complex, long-running coding tasks to an ephemeral cloud environment
## Examples

- [Basic Session](./examples/basic-session/README.md)
- [Cloudflare Workers](./examples/cloudflare-workers/README.md)
- [Advanced Session](./examples/advanced-session/README.md)
- [Agent Workflow](./examples/agent/README.md)
- [Webhook Integration](./examples/webhook/README.md)
Expand Down
34 changes: 34 additions & 0 deletions packages/core/examples/cloudflare-workers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Cloudflare Workers Example

This example demonstrates how to use the Jules SDK within a Cloudflare Worker environment. By intercepting incoming HTTP `POST` requests, the worker can automatically trigger and orchestrate a new coding session through the `@google/jules-sdk`.

This is especially useful for edge-based event processing (e.g., handling incoming webhooks from external services like Stripe or GitHub directly at the edge) and kicking off agent workflows globally without dedicated infrastructure.

## Prerequisites

- Ensure you have [Bun](https://bun.sh/) installed, or another compatible runtime like Node.js.
- Ensure your `JULES_API_KEY` is set as an environment variable in your local shell or your Cloudflare environment bindings.

## Running the Example Locally

The example uses a mocked entry point for the Cloudflare worker module via `index.ts`. To ensure it builds and can run basic checks in the monorepo context:

1. Build the module:

```bash
bun run build
```

2. To run the handler script as a standard Bun process (noting it simulates the Worker `fetch` function structure):

```bash
bun run start
```

*(Note: While `bun run start` simply executes `index.ts`, a true worker testing environment typically requires `wrangler` and a test server setup. This simple repository example demonstrates the SDK's structural integration.)*

## Notes

- In a real-world Cloudflare deployment, the `JULES_API_KEY` should be set via Cloudflare secrets using `wrangler secret put JULES_API_KEY`. It would be available on the `env` object inside the `fetch` handler.
- If your environment provides the API key via `env.JULES_API_KEY` rather than the global `process.env`, you can customize the instantiation using `jules.with({ apiKey: env.JULES_API_KEY })`.
- Make sure to modify the target `source` in `index.ts` to match the specific GitHub repository or branching strategy your worker intends to automate.
64 changes: 64 additions & 0 deletions packages/core/examples/cloudflare-workers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { jules } from '@google/jules-sdk';

/**
* Cloudflare Worker Example
*
* This example demonstrates how to use the Jules SDK within a Cloudflare Worker environment.
* The worker intercepts incoming HTTP POST requests (e.g., a webhook or custom event trigger)
* and starts a new Jules coding session.
*/
export default {
async fetch(request: Request, env: any, ctx: any): Promise<Response> {
// Only accept POST requests for this example
if (request.method !== 'POST') {
return new Response('Method Not Allowed. Please send a POST request.', { status: 405 });
}

try {
// Parse the incoming JSON payload
const payload = await request.json().catch(() => ({}));
console.log('Received payload:', payload);

// We can use the global `jules` object since JULES_API_KEY should be passed as an environment variable
// or configured globally in the environment (e.g. via .env in local dev or Cloudflare bindings).
// Note: If JULES_API_KEY is not available globally, you can construct a custom instance
// of Jules SDK using `jules.with({ apiKey: env.JULES_API_KEY })`.

// Construct a prompt dynamically from the payload
const promptText = `Process this event triggered from a Cloudflare Worker: ${JSON.stringify(payload)}`;

// Start a Jules session
const session = await jules.session({
prompt: promptText,
// Define a target source context (replace with your repository/branch as needed)
source: { github: 'davideast/dataprompt', baseBranch: 'main' },
});

console.log(`Successfully created Jules session: ${session.id}`);

// Return a successful JSON response with the created session ID
return new Response(JSON.stringify({
success: true,
message: 'Cloudflare Worker processed event and created a session.',
sessionId: session.id,
}), {
status: 200,
headers: {
'Content-Type': 'application/json',
},
});

} catch (error) {
console.error('Error creating session:', error);

return new Response(JSON.stringify({
success: false,
message: 'Internal Server Error while creating Jules session',
error: error instanceof Error ? error.message : String(error)
}), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
},
};
16 changes: 16 additions & 0 deletions packages/core/examples/cloudflare-workers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "cloudflare-workers",
"version": "1.0.0",
"description": "Example demonstrating how to use the Jules SDK within a Cloudflare Worker.",
"main": "index.ts",
"scripts": {
"build": "bun build index.ts --target=node",
"start": "bun run index.ts"
},
"dependencies": {
"@google/jules-sdk": "workspace:*"
},
"devDependencies": {
"bun-types": "^1.1.8"
}
}
Loading