Skip to content
Closed
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
93 changes: 92 additions & 1 deletion bun.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
},
"workspaces": [
"packages/*",
"examples/*"
"examples/*",
"packages/core/examples/*"
],
"description": "Official Jules TypeScript SDK",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

Orchestrate complex, long-running coding tasks to an ephemeral cloud environment integrated with a GitHub repo.

## Examples

- [Basic Session](./examples/basic-session/README.md)
- [Advanced Session](./examples/advanced-session/README.md)
- [Agent Workflow](./examples/agent/README.md)
- [Webhook Integration](./examples/webhook/README.md)
- [GitHub Actions](./examples/github-actions/README.md)

## Send work to a Cloud based session

```ts
Expand Down
14 changes: 14 additions & 0 deletions packages/core/examples/advanced-session/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Advanced Session Example

This example demonstrates how to use the Jules SDK to run a session, interact with it, wait for plan approvals, and monitor stream progress while parsing the output for artifacts.

## Running

Ensure you have your `JULES_API_KEY` set.
You can run the script via:

```sh
bun run index.ts
```

This will run the advanced session and stream activities.
81 changes: 81 additions & 0 deletions packages/core/examples/advanced-session/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { jules, JulesError } from '@google/jules-sdk';

/**
* Advanced Session Example
*
* Demonstrates:
* - Interactive session creation
* - Waiting for plan approval
* - Monitoring progress via reactive streams
* - Handling code diffs (changeSet)
*/
async function runAdvancedSession() {
try {
console.log('Starting advanced session...');

// Interactive Sessions allow you to provide feedback and guide the process
const session = await jules.session({
prompt: `Create a simple python script that prints 'Hello Advanced Session!' and test it.`,
// For a repoless session, we don't provide a source
});

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

// Wait for the plan to be generated and ready for approval
console.log('Waiting for plan approval...');
await session.waitFor('awaitingPlanApproval');

console.log('Plan is ready. Approving it now.');
await session.approve();

// Stream activities and artifacts
for await (const activity of session.stream()) {
switch (activity.type) {
case 'planGenerated':
console.log(
'Plan:',
activity.plan?.steps.map((s) => s.title)
);
break;
case 'agentMessaged':
console.log('Agent says:', activity.message);
break;
case 'progressUpdated':
console.log(`Progress: ${activity.title}`);
break;
case 'sessionCompleted':
console.log('Session complete!');
break;
}

// Check artifacts
for (const artifact of activity.artifacts ?? []) {
if (artifact.type === 'bashOutput') {
console.log(`[BASH] ${artifact.toString()}`);
}
if (artifact.type === 'changeSet') {
const parsed = artifact.parsed();
for (const file of parsed.files) {
console.log(
`[DIFF] ${file.path}: +${file.additions} -${file.deletions}`
);
}
}
}
}

const outcome = await session.result();
console.log(`Session finished with state: ${outcome.state}`);

} catch (error) {
if (error instanceof JulesError) {
console.error(`SDK error: ${error.message}`);
} else {
console.error('Unknown error:', error);
}
}
}

if (require.main === module) {
runAdvancedSession();
}
65 changes: 65 additions & 0 deletions packages/core/examples/agent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Agent Workflow Example

This example demonstrates how to use the `jules.all()` API to create an AI agent workflow that manages multiple tasks concurrently. It orchestrates complex, long-running coding tasks to an ephemeral cloud environment.

## Overview

The example runs multiple prompts in parallel using `jules.all()`:

1. Analyzing a repository for improvements.
2. Writing an automation script.
3. Suggesting test cases.

It demonstrates how to configure concurrency, handle errors without stopping the whole process, and process the results (like reading generated files) from each session.

## Prerequisites

- Node.js or Bun installed.
- A Jules API Key. Set it using:
```bash
export JULES_API_KEY=<your-api-key>
```

## Running the Example

You can run this example using `bun`, `tsx`, or `ts-node`:

### Using Bun

```bash
bun run index.ts
```

### Using Node.js and TSX

If you don't have `bun` installed, you can run the example using `tsx`:

```bash
npm install -g tsx
tsx index.ts
```

## Example Output

```text
Starting Agent Workflow Example...
Creating 3 sessions...
Finished creating 3 sessions.

Session jules:session:12345:
State: succeeded
Generated 1 files.
- improvements.md

Session jules:session:67890:
State: succeeded
Generated 1 files.
- deploy.sh

Session jules:session:54321:
State: succeeded
Generated 1 files.
- test_cases.md

Workflow complete.
```
75 changes: 75 additions & 0 deletions packages/core/examples/agent/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { jules } from '@google/jules-sdk';

/**
* Agent Workflow Example
*
* This example demonstrates how to use `jules.all()` to orchestrate
* multiple agent sessions concurrently.
*/
async function main() {
console.log('Starting Agent Workflow Example...');

// Define a list of tasks for the agents to complete
const tasks = [
'Analyze the repository and create a list of potential improvements',
'Write a script to automate the deployment process',
'Review the existing test suite and suggest additional test cases',
];

console.log(`Creating ${tasks.length} sessions...`);

// Use jules.all() to run sessions concurrently
const sessions = await jules.all(
tasks,
(task) => ({
prompt: task,
// In a real scenario, you would provide a source repository:
// source: { github: 'your-org/your-repo', baseBranch: 'main' },
}),
{
concurrency: 2, // Limit concurrency to 2
stopOnError: false, // Continue processing even if one session fails
},
);

console.log(`Finished creating ${sessions.length} sessions.`);

// Process the results
for (const session of sessions) {
console.log(`\nSession ${session.id}:`);

// Wait for the session to complete and get the outcome
try {
const outcome = await session.result();
console.log(` State: ${outcome.state}`);

if (outcome.state === 'failed') {
console.log(` Failed. Check logs or session stream for details.`);
} else {
// Retrieve generated files
const files = outcome.generatedFiles();
if (files.size > 0) {
console.log(` Generated ${files.size} files.`);
for (const [path, _] of files.entries()) {
console.log(` - ${path}`);
}
} else {
console.log(' No files generated.');
}
}
} catch (error) {
console.error(` Error processing session ${session.id}:`, error);
}
}

console.log('\nWorkflow complete.');
}

// Ensure JULES_API_KEY is set
if (!process.env.JULES_API_KEY) {
console.error('Error: JULES_API_KEY environment variable is missing.');
console.log('Please set it using: export JULES_API_KEY=<your-key>');
process.exit(1);
}

main().catch(console.error);
59 changes: 59 additions & 0 deletions packages/core/examples/github-actions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# GitHub Actions Example

This example demonstrates how to use the Jules SDK within a GitHub Action to automate workflows.

## Overview

This example creates a custom GitHub Action that:
1. Takes a `prompt` input.
2. Reads the `JULES_API_KEY` from the environment.
3. Automatically uses the current repository and branch as context.
4. Starts a Jules session to perform the task.
5. Monitors the progress and outputs the resulting Pull Request URL.

## Files

- `index.ts`: The main action logic using the Jules SDK.
- `action.yml`: The metadata file that defines the action inputs, outputs, and runtime.
- `package.json`: Dependencies for the action (`@actions/core`, `@actions/github`, `@google/jules-sdk`).

## Usage

You can use this action in your own GitHub workflows by referencing its path or publishing it.

Here is an example workflow file (`.github/workflows/jules.yml`) that triggers the agent whenever an issue is labeled `agent-fix`:

```yaml
name: Jules Agent Workflow

on:
issues:
types: [labeled]

jobs:
run-jules:
if: github.event.label.name == 'agent-fix'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Run Jules Agent
uses: ./packages/core/examples/github-actions # Path to where this action is located
env:
JULES_API_KEY: ${{ secrets.JULES_API_KEY }}
with:
prompt: "Fix the following issue: ${{ github.event.issue.title }}\n\n${{ github.event.issue.body }}"
```

## Running the Example Locally

To compile the TypeScript action:

```bash
cd packages/core/examples/github-actions
npm install
npm run build
```

The compiled JavaScript will be placed in `dist/index.js`, which is what GitHub Actions will execute.
13 changes: 13 additions & 0 deletions packages/core/examples/github-actions/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: 'Jules Agent Action'
description: 'Run a Jules coding agent session on your repository'
author: 'Google'
inputs:
prompt:
description: 'The instructions for the Jules agent'
required: true
outputs:
pr-url:
description: 'The URL of the Pull Request created by the agent, if any'
runs:
using: 'node20'
main: 'dist/index.js'
Loading
Loading