-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathopenai-integration.ts
More file actions
63 lines (52 loc) · 1.95 KB
/
openai-integration.ts
File metadata and controls
63 lines (52 loc) · 1.95 KB
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
/**
* 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);
}
const openaiIntegration = async (): Promise<void> => {
// Initialize StackOne — reads STACKONE_API_KEY and STACKONE_ACCOUNT_ID from env
const toolset = new StackOneToolSet();
// Fetch all tools for this account via MCP
const tools = await toolset.fetchTools();
const openAITools = tools.toOpenAI();
// Initialize 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 BambooHR 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 === 'bamboohr_get_employee',
'Expected tool call to be bamboohr_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();