|
| 1 | +import type { RequestHandler } from './$types'; |
| 2 | + |
| 3 | +import { env } from '$env/dynamic/private'; |
| 4 | + |
| 5 | +import { AssistantResponse } from 'ai'; |
| 6 | +import OpenAI from 'openai'; |
| 7 | + |
| 8 | +const openai = new OpenAI({ |
| 9 | + apiKey: env.OPENAI_API_KEY || '', |
| 10 | +}); |
| 11 | + |
| 12 | +const homeTemperatures = { |
| 13 | + bedroom: 20, |
| 14 | + 'home office': 21, |
| 15 | + 'living room': 21, |
| 16 | + kitchen: 22, |
| 17 | + bathroom: 23, |
| 18 | +}; |
| 19 | + |
| 20 | +export const POST = (async ({ request }) => { |
| 21 | + // Parse the request body |
| 22 | + const input: { |
| 23 | + threadId: string | null; |
| 24 | + message: string; |
| 25 | + } = await request.json(); |
| 26 | + |
| 27 | + // Create a thread if needed |
| 28 | + const threadId = input.threadId ?? (await openai.beta.threads.create({})).id; |
| 29 | + |
| 30 | + // Add a message to the thread |
| 31 | + const createdMessage = await openai.beta.threads.messages.create(threadId, { |
| 32 | + role: 'user', |
| 33 | + content: input.message, |
| 34 | + }); |
| 35 | + |
| 36 | + return AssistantResponse( |
| 37 | + { threadId, messageId: createdMessage.id }, |
| 38 | + async ({ forwardStream, sendDataMessage }) => { |
| 39 | + // Run the assistant on the thread |
| 40 | + const runStream = openai.beta.threads.runs.stream(threadId, { |
| 41 | + assistant_id: |
| 42 | + env.ASSISTANT_ID ?? |
| 43 | + (() => { |
| 44 | + throw new Error('ASSISTANT_ID is not set'); |
| 45 | + })(), |
| 46 | + }); |
| 47 | + |
| 48 | + // forward run status would stream message deltas |
| 49 | + let runResult = await forwardStream(runStream); |
| 50 | + |
| 51 | + // status can be: queued, in_progress, requires_action, cancelling, cancelled, failed, completed, or expired |
| 52 | + while ( |
| 53 | + runResult?.status === 'requires_action' && |
| 54 | + runResult.required_action?.type === 'submit_tool_outputs' |
| 55 | + ) { |
| 56 | + const tool_outputs = |
| 57 | + runResult.required_action.submit_tool_outputs.tool_calls.map( |
| 58 | + (toolCall: any) => { |
| 59 | + const parameters = JSON.parse(toolCall.function.arguments); |
| 60 | + |
| 61 | + switch (toolCall.function.name) { |
| 62 | + case 'getRoomTemperature': { |
| 63 | + const temperature = |
| 64 | + homeTemperatures[ |
| 65 | + parameters.room as keyof typeof homeTemperatures |
| 66 | + ]; |
| 67 | + |
| 68 | + return { |
| 69 | + tool_call_id: toolCall.id, |
| 70 | + output: temperature.toString(), |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + case 'setRoomTemperature': { |
| 75 | + const oldTemperature = |
| 76 | + homeTemperatures[ |
| 77 | + parameters.room as keyof typeof homeTemperatures |
| 78 | + ]; |
| 79 | + |
| 80 | + homeTemperatures[ |
| 81 | + parameters.room as keyof typeof homeTemperatures |
| 82 | + ] = parameters.temperature; |
| 83 | + |
| 84 | + sendDataMessage({ |
| 85 | + role: 'data', |
| 86 | + data: { |
| 87 | + oldTemperature, |
| 88 | + newTemperature: parameters.temperature, |
| 89 | + description: `Temperature in ${parameters.room} changed from ${oldTemperature} to ${parameters.temperature}`, |
| 90 | + }, |
| 91 | + }); |
| 92 | + |
| 93 | + return { |
| 94 | + tool_call_id: toolCall.id, |
| 95 | + output: `temperature set successfully`, |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + default: |
| 100 | + throw new Error( |
| 101 | + `Unknown tool call function: ${toolCall.function.name}`, |
| 102 | + ); |
| 103 | + } |
| 104 | + }, |
| 105 | + ); |
| 106 | + |
| 107 | + runResult = await forwardStream( |
| 108 | + openai.beta.threads.runs.submitToolOutputsStream( |
| 109 | + threadId, |
| 110 | + runResult.id, |
| 111 | + { tool_outputs }, |
| 112 | + ), |
| 113 | + ); |
| 114 | + } |
| 115 | + }, |
| 116 | + ); |
| 117 | +}) satisfies RequestHandler; |
0 commit comments