-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtanstack-ai-integration.ts
More file actions
76 lines (66 loc) · 2.3 KB
/
tanstack-ai-integration.ts
File metadata and controls
76 lines (66 loc) · 2.3 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
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* This example shows how to use StackOne tools with TanStack AI.
*
* TanStack AI requires Zod schemas for tool input validation.
* This example demonstrates how to wrap StackOne tools for use with TanStack AI
* by creating Zod schemas that match the tool's JSON Schema.
*/
import assert from 'node:assert';
import process from 'node:process';
import { chat } from '@tanstack/ai';
import { openaiText } from '@tanstack/ai-openai';
import { z } from 'zod';
import { StackOneToolSet } from '@stackone/ai';
const apiKey = process.env.STACKONE_API_KEY;
if (!apiKey) {
console.error('STACKONE_API_KEY environment variable is required');
process.exit(1);
}
const tanstackAiIntegration = async (): Promise<void> => {
// Initialize StackOne — reads STACKONE_API_KEY and STACKONE_ACCOUNT_ID from env
const toolset = new StackOneToolSet();
// Fetch tools from StackOne
const tools = await toolset.fetchTools();
// Get a specific tool and create a TanStack AI compatible tool
const employeeTool = tools.getTool('bamboohr_get_employee');
assert(employeeTool !== undefined, 'Expected to find bamboohr_get_employee tool');
// Create a TanStack AI server tool from the StackOne tool
// TanStack AI requires Zod schemas, so we create one that matches the tool's parameters
const getEmployeeTool = {
name: employeeTool.name,
description: employeeTool.description,
// TanStack AI requires Zod schema for input validation
inputSchema: z.object({
id: z.string().describe('The employee ID'),
}),
execute: async (args: { id: string }) => {
return employeeTool.execute(args);
},
};
// Use TanStack AI chat with the tool
// The adapter reads OPENAI_API_KEY from the environment automatically
const adapter = openaiText('gpt-5');
const stream = chat({
adapter,
messages: [
{
role: 'user',
content: 'Get the employee with id: c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA',
},
],
tools: [getEmployeeTool],
});
// Process the stream using AG-UI protocol events
let hasToolCall = false;
for await (const chunk of stream) {
if (chunk.type === 'TOOL_CALL_START') {
hasToolCall = true;
assert(
chunk.toolName === 'bamboohr_get_employee',
'Expected tool call to be bamboohr_get_employee',
);
}
}
assert(hasToolCall, 'Expected at least one tool call');
};
await tanstackAiIntegration();