61 lines
1.7 KiB
TypeScript
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 });
|
|
}
|