nodejs-swarm-openai
Version:
Test implementation of OpenAI Swarm for Node.js ā a multi-agent system with support for finalization, custom agents, and asynchronous tools.
39 lines (34 loc) ⢠1.13 kB
JavaScript
import { Agent } from './agent.js';
import { Swarm } from './swarm.js';
import { convertFunctionToJsonSchema } from './tool.js';
export function printMessagesPretty(messages) {
for (const msg of messages) {
if (msg.role === 'user') {
console.log(`\nš¤ User: ${msg.content}`);
} else if (msg.role === 'assistant') {
if (msg.tool_calls) {
for (const call of msg.tool_calls) {
const args = JSON.stringify(JSON.parse(call.function.arguments), null, 2);
console.log(`š¤ Assistant invoked agent: ${call.function.name} with arguments:\n${args}`);
}
} else {
console.log(`š¤ Assistant: ${msg.content}`);
}
} else if (msg.role === 'tool') {
console.log(`š ļø Tool response: ${msg.content}`);
} else {
console.log(`š ${msg.role}: ${msg.content}`);
}
}
console.log('\nā
Conversation finished.\n');
}
export function getFinalAnswer(messages) {
return [...messages]
.reverse()
.find(m => m.role === 'assistant' && m.content)?.content || null;
}
export {
Agent,
Swarm,
convertFunctionToJsonSchema
};