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

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"workspaces": [
"packages/*",
"packages/core/examples/*",
"examples/*"
],
"description": "Official Jules TypeScript SDK",
Expand Down
36 changes: 36 additions & 0 deletions packages/core/examples/basic-session/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Basic Session Example

This example demonstrates how to use the Jules SDK to create a simple interactive session. It will use the `jules.session` method to connect to the Jules API, create a session without a specific repository context (a "Repoless" session), provide a prompt to an AI agent, and wait for the results.

## Requirements

- Node.js >= 18 or Bun
- A Jules API Key (`JULES_API_KEY` environment variable)

## Setup

1. Make sure you have installed the SDK dependencies in the project root.

2. Export your Jules API key:

```bash
export JULES_API_KEY="your-api-key-here"
```

## Running the Example

Using `bun`:

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

Using `npm` and `tsx` (or similar TypeScript runner):

```bash
npx tsx index.ts
```

## What it does

The script creates a session using `jules.session` and asks the agent to write a haiku. It then waits for the result and retrieves the output. This is a basic demonstration of the isolated core Jules API.
75 changes: 75 additions & 0 deletions packages/core/examples/basic-session/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { jules } from '@google/jules-sdk';

/**
* Basic Session Example
*
* Demonstrates a simple interaction with the Jules SDK.
* It creates a repoless session (no GitHub source), provides a simple prompt,
* and awaits the final outcome.
*/
async function main() {
// Ensure the JULES_API_KEY environment variable is set.
if (!process.env.JULES_API_KEY) {
console.error('Error: JULES_API_KEY environment variable is not set.');
console.error('Please set it using: export JULES_API_KEY="your-api-key"');
process.exit(1);
}

console.log('Creating a new Jules session...');

try {
// 1. Create a basic session
// We use a repoless session (no source) for this simple demonstration.
const session = await jules.session({
prompt: 'Write a haiku about artificial intelligence programming itself.',
});

console.log(`Session created! ID: ${session.id}`);
console.log('Waiting for the agent to complete the task...');

// 2. Await the result of the session
// This will poll the API until the session reaches a terminal state (completed or failed).
const outcome = await session.result();

console.log('\n--- Session Result ---');
console.log(`State: ${outcome.state}`);

if (outcome.state === 'completed') {
// 3. Retrieve and display the final message from the agent
// Since it's a haiku, we expect it in the generated files or the agent's messages.
// Often, the agent writes its final output to a file or answers in a message.
// Let's print out the latest agent message.
const activities = await jules.select({
from: 'activities',
where: { type: 'agentMessaged', 'session.id': session.id },
order: 'desc',
limit: 1,
});

if (activities.length > 0) {
console.log('\nAgent Message:');
console.log(activities[0].message);
} else {
console.log('\nThe agent did not leave a final message.');

// Let's check generated files
const files = outcome.generatedFiles();
if (files.size > 0) {
console.log('\nGenerated Files:');
for (const [filename, content] of files.entries()) {
console.log(`\nFile: ${filename}`);
console.log(content.content);
}
}
}
} else {
console.error('The session did not complete successfully.');
}

} catch (error) {
console.error('An error occurred during the session:', error);
}
}

// Run the example
main();