2025-03-19 06:04:06 +00:00
|
|
|
import { google } from "@ai-sdk/google";
|
2025-03-19 12:02:07 +00:00
|
|
|
import { openai } from "@ai-sdk/openai";
|
2025-03-19 06:04:06 +00:00
|
|
|
import { streamText } from "ai";
|
2025-03-19 08:16:44 +00:00
|
|
|
import { z } from "zod";
|
2025-03-19 11:03:37 +00:00
|
|
|
import { readFileSync } from 'fs';
|
|
|
|
|
import { resolve } from 'path';
|
|
|
|
|
|
|
|
|
|
// Allow streaming responses up to 30 seconds
|
2025-03-19 06:04:06 +00:00
|
|
|
export const maxDuration = 30;
|
|
|
|
|
|
2025-03-19 11:03:37 +00:00
|
|
|
// Read the XML guide from file
|
2025-03-19 08:16:44 +00:00
|
|
|
|
2025-03-19 11:03:37 +00:00
|
|
|
const guide = readFileSync(resolve('./app/api/chat/xml_guide.md'), 'utf8');
|
2025-03-19 08:16:44 +00:00
|
|
|
|
2025-03-19 06:04:06 +00:00
|
|
|
export async function POST(req: Request) {
|
2025-03-19 11:03:37 +00:00
|
|
|
const { messages } = await req.json();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Read and escape the guide content
|
2025-03-19 08:40:08 +00:00
|
|
|
const systemMessage = `
|
2025-03-19 12:02:07 +00:00
|
|
|
You are a helpful assistant that can create, edit, and display diagram using draw.io through xml strings.
|
2025-03-19 11:03:37 +00:00
|
|
|
You can use the following tools:
|
|
|
|
|
---Tool1---
|
2025-03-22 14:28:55 +00:00
|
|
|
tool name: display_diagram
|
|
|
|
|
description: Display a diagram on draw.io
|
2025-03-19 11:03:37 +00:00
|
|
|
parameters: {
|
|
|
|
|
xml: string
|
|
|
|
|
}
|
|
|
|
|
---End of tools---
|
|
|
|
|
|
2025-03-22 14:28:55 +00:00
|
|
|
Here is a guide for the XML format:
|
|
|
|
|
"""md
|
|
|
|
|
${guide}
|
|
|
|
|
"""
|
|
|
|
|
You can use the tools to create and manipulate diagrams.
|
2025-03-19 11:03:37 +00:00
|
|
|
You can also answer questions and provide explanations.
|
2025-03-22 14:28:55 +00:00
|
|
|
Note that:
|
|
|
|
|
- If the user wants you to draw something rather than a diagram, you can use the combination of the shapes to draw it.
|
|
|
|
|
- Consider the layout of the diagram and the shapes used to avoid overlapping.
|
|
|
|
|
- Ensure that the XML strings are well-formed and valid.
|
2025-03-19 08:40:08 +00:00
|
|
|
`;
|
2025-03-19 11:03:37 +00:00
|
|
|
|
|
|
|
|
// Add system message if only user message is provided
|
|
|
|
|
const enhancedMessages = messages.length === 1
|
|
|
|
|
? [{ role: "system", content: systemMessage }, ...messages]
|
|
|
|
|
: messages;
|
|
|
|
|
|
|
|
|
|
const result = streamText({
|
2025-03-19 12:02:07 +00:00
|
|
|
// model: google("gemini-2.0-flash"),
|
|
|
|
|
model: openai("gpt-4o"),
|
2025-03-19 11:03:37 +00:00
|
|
|
messages: enhancedMessages,
|
2025-03-19 08:16:44 +00:00
|
|
|
tools: {
|
2025-03-19 11:03:37 +00:00
|
|
|
// Client-side tool that will be executed on the client
|
2025-03-22 14:28:55 +00:00
|
|
|
display_diagram: {
|
|
|
|
|
description: "Display a diagram on draw.io",
|
2025-03-19 11:03:37 +00:00
|
|
|
parameters: z.object({
|
|
|
|
|
xml: z.string().describe("XML string to be displayed on draw.io")
|
|
|
|
|
})
|
2025-03-19 08:16:44 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
temperature: 0,
|
2025-03-19 06:04:06 +00:00
|
|
|
});
|
2025-03-19 08:16:44 +00:00
|
|
|
|
2025-03-19 11:03:37 +00:00
|
|
|
return result.toDataStreamResponse();
|
2025-03-19 08:16:44 +00:00
|
|
|
}
|