Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions mocks/handlers.anthropic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { http, HttpResponse } from 'msw';
import {
EXAMPLE_ANSWER,
EXAMPLE_TOOL_CALL,
bodyField,
extractTextFromInput,
nextTurn,
} from './handlers.utils';

/** Anthropic Messages, answering the prompt anthropic-integration.ts sends. */
export const anthropicHandlers = [
http.post('https://api.anthropic.com/v1/messages', async ({ request }) => {
const body = await request.json();
const turn = nextTurn(body, extractTextFromInput(bodyField(body, 'messages')));
return HttpResponse.json({
id: 'msg_mock',
type: 'message',
role: 'assistant',
model: 'claude-haiku-4-5',
content:
turn === 'tool_call'
? [{ type: 'tool_use', id: 'toolu_mock_list', ...EXAMPLE_TOOL_CALL }]
: [{ type: 'text', text: turn === 'answer' ? EXAMPLE_ANSWER : 'Mock response' }],
stop_reason: turn === 'tool_call' ? 'tool_use' : 'end_turn',
usage: { input_tokens: 100, output_tokens: 50 },
});
}),
];
257 changes: 61 additions & 196 deletions mocks/handlers.openai.ts
Original file line number Diff line number Diff line change
@@ -1,222 +1,87 @@
import { http, HttpResponse } from 'msw';
import { bodyField, extractTextFromInput, isRecord, offersTools } from './handlers.utils';
import {
EXAMPLE_ANSWER,
EXAMPLE_TOOL_CALL,
bodyField,
extractTextFromInput,
nextTurn,
} from './handlers.utils';

/** The first user message of a chat completion that mentions an employee, or `''`. */
const employeeQuestion = (messages: unknown): string => {
if (!Array.isArray(messages)) {
return '';
}
const candidates: unknown[] = messages;
for (const message of candidates) {
if (isRecord(message) && message.role === 'user' && typeof message.content === 'string') {
if (message.content.includes('employee')) {
return message.content;
}
}
}
return '';
};
const usage = { input_tokens: 100, output_tokens: 50, total_tokens: 150 };

/**
* OpenAI API endpoints for AI SDK and OpenAI integration examples
* OpenAI Responses (used by the AI SDK and openai-responses-integration.ts) and Chat Completions
* (openai-integration.ts), answering the prompt the examples send.
*/
export const openaiHandlers = [
// OpenAI Responses API (used by AI SDK)
http.post('https://api.openai.com/v1/responses', async ({ request }) => {
const body = await request.json();
const userMessage = extractTextFromInput(bodyField(body, 'input'));
const hasTools = offersTools(body);

// For ai-sdk-integration.ts
if (hasTools && userMessage.includes('Get all details')) {
return HttpResponse.json({
id: 'resp_mock_ai_sdk',
object: 'response',
created_at: Date.now(),
model: 'gpt-5',
status: 'completed',
output: [
{
type: 'message',
id: 'msg_mock_ai_sdk',
role: 'assistant',
status: 'completed',
content: [
{
type: 'output_text',
text: 'The employee Michael Scott has the following details: ID c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA, phone +1-555-0100.',
annotations: [],
},
],
},
],
usage: { input_tokens: 100, output_tokens: 50, total_tokens: 150 },
});
}

// For openai-responses-integration.ts
if (hasTools && userMessage.includes('c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA')) {
return HttpResponse.json({
id: 'resp_mock_responses',
object: 'response',
created_at: Date.now(),
model: 'gpt-5',
status: 'completed',
output: [
{
type: 'function_call',
id: 'call_mock_get',
call_id: 'call_mock_get',
name: 'bamboohr_get_employee',
arguments: JSON.stringify({
id: 'c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA',
}),
status: 'completed',
},
],
usage: { input_tokens: 100, output_tokens: 50, total_tokens: 150 },
});
}

// For human-in-the-loop.ts
if (hasTools && userMessage.includes('Create a new employee')) {
return HttpResponse.json({
id: 'resp_mock_hitl',
object: 'response',
created_at: Date.now(),
model: 'gpt-5',
status: 'completed',
output: [
{
type: 'function_call',
id: 'call_mock_create',
call_id: 'call_mock_create',
name: 'bamboohr_create_employee',
arguments: JSON.stringify({
name: 'John Doe',
personal_email: 'john.doe@example.com',
department: 'Engineering',
start_date: '2025-01-01',
hire_date: '2025-01-01',
}),
status: 'completed',
},
],
usage: { input_tokens: 100, output_tokens: 50, total_tokens: 150 },
});
}

// Default response
const turn = nextTurn(body, extractTextFromInput(bodyField(body, 'input')));
const output =
turn === 'tool_call'
? [
{
type: 'function_call',
id: 'call_mock_list',
call_id: 'call_mock_list',
name: EXAMPLE_TOOL_CALL.name,
arguments: JSON.stringify(EXAMPLE_TOOL_CALL.input),
status: 'completed',
},
]
: [
{
type: 'message',
id: 'msg_mock',
role: 'assistant',
status: 'completed',
content: [
{
type: 'output_text',
text: turn === 'answer' ? EXAMPLE_ANSWER : 'Mock response',
annotations: [],
},
],
},
];
return HttpResponse.json({
id: 'resp_default',
id: `resp_mock_${turn}`,
object: 'response',
created_at: Date.now(),
model: 'gpt-5',
status: 'completed',
output: [
{
type: 'message',
id: 'msg_default',
role: 'assistant',
status: 'completed',
content: [{ type: 'output_text', text: 'Mock response', annotations: [] }],
},
],
usage: { input_tokens: 10, output_tokens: 10, total_tokens: 20 },
output,
usage,
});
}),

// OpenAI Chat Completions API (used by OpenAI SDK directly)
http.post('https://api.openai.com/v1/chat/completions', async ({ request }) => {
const body = await request.json();
const userMessage = employeeQuestion(bodyField(body, 'messages'));
const hasTools = offersTools(body);

// For openai-integration.ts - returns tool call for bamboohr_get_employee
if (hasTools && userMessage.includes('c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA')) {
return HttpResponse.json({
id: 'chatcmpl-mock',
object: 'chat.completion',
created: Date.now(),
model: 'gpt-5',
choices: [
{
index: 0,
message: {
role: 'assistant',
content: null,
tool_calls: [
{
id: 'call_mock',
type: 'function',
function: {
name: 'bamboohr_get_employee',
arguments: JSON.stringify({
id: 'c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA',
fields: 'phone_number',
}),
},
},
],
},
finish_reason: 'tool_calls',
},
],
usage: { prompt_tokens: 100, completion_tokens: 50, total_tokens: 150 },
});
}

// For human-in-the-loop.ts - returns tool call for bamboohr_create_employee
if (hasTools && userMessage.includes('Create a new employee')) {
return HttpResponse.json({
id: 'chatcmpl-mock-hitl',
object: 'chat.completion',
created: Date.now(),
model: 'gpt-5',
choices: [
{
index: 0,
message: {
role: 'assistant',
content: null,
tool_calls: [
{
id: 'call_mock_create',
type: 'function',
function: {
name: 'bamboohr_create_employee',
arguments: JSON.stringify({
name: 'John Doe',
personal_email: 'john.doe@example.com',
department: 'Engineering',
start_date: '2025-01-01',
hire_date: '2025-01-01',
}),
},
const turn = nextTurn(body, extractTextFromInput(bodyField(body, 'messages')));
const message =
turn === 'tool_call'
? {
role: 'assistant',
content: null,
tool_calls: [
{
id: 'call_mock_list',
type: 'function',
function: {
name: EXAMPLE_TOOL_CALL.name,
arguments: JSON.stringify(EXAMPLE_TOOL_CALL.input),
},
],
},
finish_reason: 'tool_calls',
},
],
usage: { prompt_tokens: 100, completion_tokens: 50, total_tokens: 150 },
});
}

// Default response
},
],
}
: { role: 'assistant', content: turn === 'answer' ? EXAMPLE_ANSWER : 'Mock response' };
return HttpResponse.json({
id: 'chatcmpl-default',
id: `chatcmpl-mock-${turn}`,
object: 'chat.completion',
created: Date.now(),
model: 'gpt-5',
choices: [
{
index: 0,
message: { role: 'assistant', content: 'Mock response' },
finish_reason: 'stop',
},
],
usage: { prompt_tokens: 10, completion_tokens: 10, total_tokens: 20 },
choices: [{ index: 0, message, finish_reason: turn === 'tool_call' ? 'tool_calls' : 'stop' }],
usage: { prompt_tokens: 100, completion_tokens: 50, total_tokens: 150 },
});
}),
];
8 changes: 7 additions & 1 deletion mocks/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { anthropicHandlers } from './handlers.anthropic';
import { mcpHandlers } from './handlers.mcp';
import { openaiHandlers } from './handlers.openai';
import { stackoneAccountsHandlers } from './handlers.stackone-accounts';

export const handlers = [...openaiHandlers, ...stackoneAccountsHandlers, ...mcpHandlers];
export const handlers = [
...anthropicHandlers,
...openaiHandlers,
...stackoneAccountsHandlers,
...mcpHandlers,
];
Loading
Loading