import OpenAI from 'openai';
async function runConversationalAgent(accountId: string) {
const openai = new OpenAI();
const toolset = new StackOneToolSet();
const tools = await toolset.fetchTools({ accountIds: [accountId] });
const openAITools = tools.toOpenAI();
const messages: OpenAI.Chat.ChatCompletionMessageParam[] = [
{
role: 'system',
content: 'You are an HR assistant with access to employee data.'
}
];
// Multi-turn conversation loop
while (true) {
const userInput = await getUserInput(); // Your function
if (userInput === 'exit') break;
messages.push({ role: 'user', content: userInput });
const completion = await openai.chat.completions.create({
model: 'gpt-5',
messages,
tools: openAITools,
tool_choice: 'auto'
});
const message = completion.choices[0].message;
messages.push(message);
// Execute tool calls if any
if (message.tool_calls) {
for (const toolCall of message.tool_calls) {
const tool = tools.getTool(toolCall.function.name);
if (tool) {
const result = await tool.execute(
JSON.parse(toolCall.function.arguments)
);
messages.push({
role: 'tool',
tool_call_id: toolCall.id,
content: JSON.stringify(result.data)
});
}
}
// Get final response after tool execution
const finalCompletion = await openai.chat.completions.create({
model: 'gpt-5',
messages,
});
console.log(finalCompletion.choices[0].message.content);
messages.push(finalCompletion.choices[0].message);
} else {
console.log(message.content);
}
}
}