ai-sdk-next-openai/app/api/use-chat-human-in-the-loop/route.ts
2025-09-26 15:46:29 +00:00

61 lines
1.7 KiB
TypeScript

import { openai } from '@ai-sdk/openai';
import {
createUIMessageStreamResponse,
streamText,
createUIMessageStream,
convertToModelMessages,
stepCountIs,
} from 'ai';
import { processToolCalls } from './utils';
import { tools } from './tools';
import { HumanInTheLoopUIMessage } from './types';
// Allow streaming responses up to 30 seconds
export const maxDuration = 30;
export async function POST(req: Request) {
const { messages }: { messages: HumanInTheLoopUIMessage[] } =
await req.json();
const stream = createUIMessageStream({
originalMessages: messages,
execute: async ({ writer }) => {
// Utility function to handle tools that require human confirmation
// Checks for confirmation in last message and then runs associated tool
const processedMessages = await processToolCalls(
{
messages,
writer,
tools,
},
{
// type-safe object for tools without an execute function
getWeatherInformation: async ({ city }) => {
const conditions = ['sunny', 'cloudy', 'rainy', 'snowy'];
return `The weather in ${city} is ${
conditions[Math.floor(Math.random() * conditions.length)]
}.`;
},
},
);
const result = streamText({
model: openai('gpt-4o'),
messages: convertToModelMessages(processedMessages),
tools,
stopWhen: stepCountIs(5),
});
writer.merge(
result.toUIMessageStream({ originalMessages: processedMessages }),
);
},
onFinish: ({}) => {
// save messages here
console.log('Finished!');
},
});
return createUIMessageStreamResponse({ stream });
}