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

Function POST

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

Source from the content-addressed store, hash-verified

20});
21
22export async function POST(req: NextRequest) {
23 try {
24 // Extract thread ID, input content, and fileIds from JSON data
25 const data = await req.json();
26 const threadId = data.threadId;
27 const input = data.input;
28 const fileIds = data.fileIds; // This is the new line
29
30 // Log the received thread ID, input, and fileIds for debugging purposes
31 console.log(`inside add_Message -Thread ID: ${threadId}`);
32 console.log(`inside add_Message -Input: ${input}`);
33 console.log(`inside add_Message -File IDs: ${fileIds}`); // This is the new line
34
35 // Validate the input data
36 if (typeof input !== 'string') {
37 throw new Error('Input is not a string');
38 }
39
40 // If input is provided, create a new message in the thread using the OpenAI API
41 if (input) {
42 await openai.beta.threads.messages.create(threadId, {
43 role: "user",
44 content: input,
45 file_ids: fileIds || [], // This is the new line
46 });
47 console.log("add_Message successfully");
48 return NextResponse.json({ message: "Message created successfully" });
49 }
50
51 // Respond with a message indicating no action was performed if input is empty
52 return NextResponse.json({ message: 'No action performed' });
53 } catch (error) {
54 // Error handling with detailed logging
55 if (error instanceof Error) {
56 console.error('Error:', error);
57 return NextResponse.json({ error: error.message });
58 } else {
59 console.error('Unknown error:', error);
60 return NextResponse.json({ error: 'An unknown error occurred' });
61 }
62 }
63}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected