UNPKG

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
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 };