All files / examples openai-responses-integration.ts

0% Statements 0/21
0% Branches 0/2
0% Functions 0/2
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                                                                                                                         
/**
 * This example shows how to use StackOne tools with OpenAI's Responses API.
 */
 
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-stackone-account-id';
 
const openaiResponsesIntegration = async (): Promise<void> => {
	// Initialise StackOne
	const toolset = new StackOneToolSet({ accountId });
 
	// Fetch HRIS tools via MCP
	const tools = await toolset.fetchTools({
		actions: ['_list_*'],
	});
	const openAIResponsesTools = tools.toOpenAIResponses();
 
	// Initialise OpenAI client
	const openai = new OpenAI();
 
	// Create a response with tool calls using the Responses API
	const response = await openai.responses.create({
		model: 'gpt-5.1',
		instructions: 'You are a helpful assistant that can access various tools.',
		input: 'What is the employee with id: c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA phone number?',
		tools: openAIResponsesTools,
	});
 
	// Verify the response contains expected data
	assert(response.id, 'Expected response to have an ID');
	assert(response.model, 'Expected response to have a model');
 
	// Check if the model made any tool calls
	const toolCalls = response.output.filter(
		(item): item is OpenAI.Responses.ResponseFunctionToolCall => item.type === 'function_call',
	);
 
	assert(toolCalls.length > 0, 'Expected at least one tool call');
 
	const toolCall = toolCalls[0];
	assert(toolCall.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.arguments);
	assert(args.id === 'c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA', 'Expected id to match the query');
};
 
// Run the example
await openaiResponsesIntegration();