MCPcopy Create free account
hub / github.com/admineral/OpenAI-Assistant-API-Chat / POST

Function POST

app/api/listMessages/route.ts:21–63  ·  view source on GitHub ↗
(req: NextRequest)

Source from the content-addressed store, hash-verified

19
20// Define an asynchronous POST function to handle incoming requests
21export async function POST(req: NextRequest) {
22 try {
23 // Extract JSON data from the request
24 const data = await req.json();
25
26 // Retrieve 'threadId' from JSON data
27 const threadId = data.threadId;
28
29 // Log the received thread ID for debugging
30 console.log(`Received request with threadId: ${threadId}`);
31
32 // Retrieve messages for the given thread ID using the OpenAI API
33 const messages = await openai.beta.threads.messages.list(threadId);
34
35 messages.data.forEach((message, index) => {
36 console.log(`Message ${index + 1} content:`, message.content);
37 });
38 // Log the count of retrieved messages for debugging
39 console.log(`Retrieved ${messages.data.length} messages`);
40
41
42 // Find the first assistant message
43 const assistantMessage = messages.data.find(message => message.role === 'assistant');
44
45 if (!assistantMessage) {
46 return NextResponse.json({ error: "No assistant message found" });
47 }
48
49 const assistantMessageContent = assistantMessage.content.at(0);
50 if (!assistantMessageContent) {
51 return NextResponse.json({ error: "No assistant message content found" });
52 }
53
54 if (assistantMessageContent.type !== "text") {
55 return NextResponse.json({ error: "Assistant message is not text, only text supported in this demo" });
56 }
57 // Return the retrieved messages as a JSON response
58 return NextResponse.json({ ok: true, messages: assistantMessageContent.text.value });
59 } catch (error) {
60 // Log any errors that occur during the process
61 console.error(`Error occurred: ${error}`);
62 }
63}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected