openai-swarmjs
Version:
Agentic framework inspired from OpenAI's swarm framework for TS, JS
90 lines (85 loc) • 3.5 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const repl_1 = require("../repl");
const chalk_1 = __importDefault(require("chalk"));
async function create_greeting(names, timeOfDay) {
return `Good jolly ${timeOfDay}, ${names}!`;
}
async function get_time_of_day(timeZone = '+00:00') {
const date = new Date();
let hour;
const tzString = typeof timeZone === 'object' ? timeZone.timeZone : timeZone;
const timezoneMap = {
'PST': 'America/Los_Angeles',
'EST': 'America/New_York',
'MST': 'America/Denver',
'CST': 'America/Chicago',
};
const ianaTimezone = timezoneMap[tzString.toUpperCase()] || tzString;
if (ianaTimezone.includes('/')) {
try {
const options = {
timeZone: ianaTimezone,
hour: "numeric",
hour12: false
};
hour = parseInt(new Intl.DateTimeFormat('en-US', options).format(date));
}
catch (error) {
throw new Error("Invalid IANA timezone name");
}
}
else {
const tzParts = tzString.match(/^([+-])(\d{2}):?(\d{2})?$/);
if (!tzParts) {
throw new Error("Invalid timezone format. Expected +HH:MM or -HH:MM");
}
const sign = tzParts[1] === '+' ? -1 : 1;
const hours = parseInt(tzParts[2]);
const minutes = tzParts[3] ? parseInt(tzParts[3]) : 0;
const requestedOffset = sign * (hours * 60 + minutes);
const offsetDiff = requestedOffset + date.getTimezoneOffset();
hour = (date.getHours() + Math.floor(offsetDiff / 60)) % 24;
if (hour < 0)
hour += 24;
}
if (hour < 12)
return "morning";
else if (hour < 17)
return "afternoon";
else
return "evening";
}
create_greeting.description = 'Given a person\'s name, return a greeting message.';
get_time_of_day.description = 'Get the time of day (morning, afternoon, or evening) for the given timezone.';
const functions = [create_greeting, get_time_of_day];
// Create a single agent that handles both timezone and greeting functionality
const simpleAgent = {
name: 'GreetingAssistant',
model: 'gpt-4',
instructions: `You are a helpful assistant that creates personalized greetings for users based on their timezone.
When a user provides their name and timezone information (either in a standard format like '+HH:MM' or common abbreviations like 'PST', 'EST'), you should:
1. Use the get_time_of_day function to determine the appropriate time of day for their timezone
2. Use the create_greeting function to generate a personalized greeting with their name and the correct time of day
3. Respond with the greeting and optionally engage in friendly conversation
If the timezone is missing, use '+00:00' (UTC) as the default.
If the name is unclear, politely ask for clarification.
Examples of valid inputs:
- "Hi, I'm Alice in PST"
- "My name is Bob and I'm in +05:30"
- "Hello from EST, I'm Charlie"
- "I'm David"
Always maintain a friendly and welcoming tone.`,
functions: functions,
toolChoice: 'auto',
parallelToolCalls: true,
};
// Run the example
(0, repl_1.runExample)('SimpleAgent', () => simpleAgent)
.catch((error) => {
console.error(chalk_1.default.red('Error:'), error);
process.exit(1);
});
;