Skip to content

Commit

Permalink
feat(playground): add Node.js ESM example for KaibanJS
Browse files Browse the repository at this point in the history
- Created a new playground example demonstrating KaibanJS usage in Node.js
- Implemented both ESM and CommonJS versions of a team workflow
- Added comprehensive README with setup instructions and code examples
- Included .env.example for API key configuration
- Set up package.json with scripts for running ESM and CJS versions
  • Loading branch information
darielnoel committed Jan 30, 2025
1 parent 81c8afc commit e3bf624
Show file tree
Hide file tree
Showing 6 changed files with 2,444 additions and 0 deletions.
2 changes: 2 additions & 0 deletions playground/nodejs-esm/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# OpenAI API Key for KaibanJS
OPENAI_API_KEY=your-api-key-here
66 changes: 66 additions & 0 deletions playground/nodejs-esm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Node.js ESM Example

This example demonstrates using KaibanJS in a Node.js environment with ESM (ECMAScript Modules) imports. It includes both ESM and CommonJS versions of the same code to showcase compatibility across different module systems.

## Environment Setup

- Node.js version: >=21.0.0 (tested with Node.js 21)
- KaibanJS version: ^0.14.0

## Project Structure

```
nodejs-esm/
├── .env # Environment variables configuration
├── index.js # ESM version using import statements
├── index.cjs # CommonJS version using require statements
└── package.json # Project configuration with "type": "module"
```

## Key Features

- Demonstrates KaibanJS usage in a Node.js environment
- Shows both ESM and CommonJS module system compatibility
- Implements a complete team workflow with multiple agents and tasks
- Includes proper error handling and workflow status monitoring

## Getting Started

1. Install dependencies:
```bash
npm install
```

2. Configure your environment:
- Copy `.env.example` to `.env` (if not already done)
- Add your OpenAI API key to `.env`:
```
OPENAI_API_KEY=your-api-key-here
```
3. Run the examples:
```bash
# Run ESM version
npm start
# Run CommonJS version
npm run start:cjs
```

## Code Examples

### ESM Version (index.js)
```javascript
import { Agent, Task, Team } from 'kaibanjs';
```

### CommonJS Version (index.cjs)
```javascript
const { Agent, Task, Team } = require('kaibanjs');
```

## Notes

- This example demonstrates that KaibanJS works in Node.js environments without requiring React as a dependency
- The example uses zustand's core functionality without React-specific features
- Both ESM and CommonJS versions implement the same functionality to showcase module system compatibility
82 changes: 82 additions & 0 deletions playground/nodejs-esm/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// CommonJS version
const { Agent, Task, Team } = require('kaibanjs');
const dotenv = require('dotenv');

dotenv.config();

// Create multiple agents with different roles
const researcher = new Agent({
name: 'ResearchBot',
role: 'Research Specialist',
goal: 'Gather and analyze information',
background: 'Expert in data collection and analysis',
});

const writer = new Agent({
name: 'WriterBot',
role: 'Content Writer',
goal: 'Create engaging content from research',
background: 'Professional content creator and editor',
});

const reviewer = new Agent({
name: 'ReviewBot',
role: 'Quality Reviewer',
goal: 'Ensure content meets quality standards',
background: 'Quality assurance specialist',
});

// Create tasks for each agent
const researchTask = new Task({
title: 'Research Topic',
description: 'Research the given topic and extract key information',
expectedOutput: 'Structured research data',
agent: researcher,
});

const writingTask = new Task({
title: 'Create Content',
description: 'Transform research into engaging content',
expectedOutput: 'Draft content',
agent: writer,
});

const reviewTask = new Task({
title: 'Review Content',
description: 'Review and polish the content',
expectedOutput: 'Final polished content',
agent: reviewer,
});

// Create and configure the team
const team = new Team({
name: 'Content Creation Team',
agents: [researcher, writer, reviewer],
tasks: [researchTask, writingTask, reviewTask],
inputs: {
topic:
'The impact of artificial intelligence on modern software development',
},
env: { OPENAI_API_KEY: process.env.OPENAI_API_KEY },
});

// Subscribe to team status updates
const unsubscribe = team.subscribeToChanges(
(updatedFields) => {
console.log('Team Status Updated:', updatedFields);
},
['teamWorkflowStatus']
);

// Start the team workflow
console.log('Starting team workflow...');
(async () => {
try {
const result = await team.start();
console.log('Final Result:', result);
} catch (error) {
console.error('Error during workflow:', error);
} finally {
unsubscribe();
}
})();
80 changes: 80 additions & 0 deletions playground/nodejs-esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// ESM version
import { Agent, Task, Team } from 'kaibanjs';
import * as dotenv from 'dotenv';

dotenv.config();

// Create multiple agents with different roles
const researcher = new Agent({
name: 'ResearchBot',
role: 'Research Specialist',
goal: 'Gather and analyze information',
background: 'Expert in data collection and analysis',
});

const writer = new Agent({
name: 'WriterBot',
role: 'Content Writer',
goal: 'Create engaging content from research',
background: 'Professional content creator and editor',
});

const reviewer = new Agent({
name: 'ReviewBot',
role: 'Quality Reviewer',
goal: 'Ensure content meets quality standards',
background: 'Quality assurance specialist',
});

// Create tasks for each agent
const researchTask = new Task({
title: 'Research Topic',
description: 'Research the given topic and extract key information',
expectedOutput: 'Structured research data',
agent: researcher,
});

const writingTask = new Task({
title: 'Create Content',
description: 'Transform research into engaging content',
expectedOutput: 'Draft content',
agent: writer,
});

const reviewTask = new Task({
title: 'Review Content',
description: 'Review and polish the content',
expectedOutput: 'Final polished content',
agent: reviewer,
});

// Create and configure the team
const team = new Team({
name: 'Content Creation Team',
agents: [researcher, writer, reviewer],
tasks: [researchTask, writingTask, reviewTask],
inputs: {
topic:
'The impact of artificial intelligence on modern software development',
},
env: { OPENAI_API_KEY: process.env.OPENAI_API_KEY },
});

// Subscribe to team status updates
const unsubscribe = team.subscribeToChanges(
(updatedFields) => {
console.log('Team Status Updated:', updatedFields);
},
['teamWorkflowStatus']
);

// Start the team workflow
console.log('Starting team workflow...');
try {
const result = await team.start();
console.log('Final Result:', result);
} catch (error) {
console.error('Error during workflow:', error);
} finally {
unsubscribe();
}
Loading

0 comments on commit e3bf624

Please sign in to comment.