All files / examples openai-integration.ts

0% Statements 0/21
0% Branches 0/4
0% Functions 0/1
0% Lines 0/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72                                                                                                                                               
/**
 * This example shows how to use StackOne tools with OpenAI.
 */
 
import assert from 'node:assert';
import process from 'node:process';
import { StackOneToolSet } from '@stackone/ai';
import OpenAI from 'openai';
 
const apiKey = process.env.STACKONE_API_KEY;
if (!apiKey) {
	console.error('STACKONE_API_KEY environment variable is required');
	process.exit(1);
}
 
// Replace with your actual account ID from StackOne dashboard
const accountId = 'your-hris-account-id';
 
const openaiIntegration = async (): Promise<void> => {
	// Initialise StackOne
	const toolset = new StackOneToolSet({
		accountId,
		baseUrl: process.env.STACKONE_BASE_URL ?? 'https://api.stackone.com',
	});
 
	// Fetch HRIS tools via MCP
	const tools = await toolset.fetchTools({
		actions: ['hris_get_*'],
	});
	const openAITools = tools.toOpenAI();
 
	// Initialise OpenAI client
	const openai = new OpenAI();
 
	// Create a chat completion with tool calls
	const response = await openai.chat.completions.create({
		model: 'gpt-5.1',
		messages: [
			{
				role: 'system',
				content: 'You are a helpful assistant that can access HRIS information.',
			},
			{
				role: 'user',
				content: 'What is the employee with id: c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA phone number?',
			},
		],
		tools: openAITools,
	});
 
	// Verify the response contains tool calls
	assert(response.choices.length > 0, 'Expected at least one choice in the response');
 
	const choice = response.choices[0];
	assert(choice.message.tool_calls !== undefined, 'Expected tool_calls to be defined');
	assert(choice.message.tool_calls.length > 0, 'Expected at least one tool call');
 
	const toolCall = choice.message.tool_calls[0];
	assert(toolCall.type === 'function', 'Expected tool call to be a function');
	assert(
		toolCall.function.name === 'hris_get_employee',
		'Expected tool call to be hris_get_employee',
	);
 
	// Parse the arguments to verify they contain the expected fields
	const args = JSON.parse(toolCall.function.arguments);
	assert(args.id === 'c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA', 'Expected id to match the query');
};
 
// Run the example
await openaiIntegration();