mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-01-02 22:32:27 +08:00
feat: add system message for flowchart assistance and improve message handling in chat API
This commit is contained in:
@@ -15,7 +15,24 @@ interface ToolContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
const { messages } = await req.json();
|
let { messages } = await req.json();
|
||||||
|
const systemMessage = `
|
||||||
|
You are a helpful assistant that can create, edit, and display flowcharts using draw.io.
|
||||||
|
You can use the following tools:
|
||||||
|
- display_flow_chart: Display a flowchart on draw.io.
|
||||||
|
- fetch_flow_chart: Get the current flowchart XML from draw.io.
|
||||||
|
You can use the tools to create and manipulate flowcharts.
|
||||||
|
You can also answer questions and provide explanations.
|
||||||
|
`;
|
||||||
|
if (messages.length === 1) {
|
||||||
|
messages = [
|
||||||
|
{
|
||||||
|
"role": "system",
|
||||||
|
"content": systemMessage,
|
||||||
|
},
|
||||||
|
...messages,
|
||||||
|
];
|
||||||
|
}
|
||||||
const response = streamText({
|
const response = streamText({
|
||||||
model: google("gemini-2.0-flash"),
|
model: google("gemini-2.0-flash"),
|
||||||
messages,
|
messages,
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ export function extractDiagramXML(xml_svg_string: string): string {
|
|||||||
if (!svgElement) {
|
if (!svgElement) {
|
||||||
throw new Error("No SVG element found in the input string.");
|
throw new Error("No SVG element found in the input string.");
|
||||||
}
|
}
|
||||||
console.log("svgElement", svgElement);
|
|
||||||
// 2. Extract the 'content' attribute
|
// 2. Extract the 'content' attribute
|
||||||
const encodedContent = svgElement.getAttribute('content');
|
const encodedContent = svgElement.getAttribute('content');
|
||||||
|
|
||||||
@@ -34,10 +33,8 @@ export function extractDiagramXML(xml_svg_string: string): string {
|
|||||||
if (!diagramElement) {
|
if (!diagramElement) {
|
||||||
throw new Error("No diagram element found");
|
throw new Error("No diagram element found");
|
||||||
}
|
}
|
||||||
console.log("diagramElement", diagramElement);
|
|
||||||
// 5. Extract base64 encoded data
|
// 5. Extract base64 encoded data
|
||||||
const base64EncodedData = diagramElement.textContent;
|
const base64EncodedData = diagramElement.textContent;
|
||||||
console.log("base64EncodedData", base64EncodedData);
|
|
||||||
|
|
||||||
if (!base64EncodedData) {
|
if (!base64EncodedData) {
|
||||||
throw new Error("No encoded data found in the diagram element");
|
throw new Error("No encoded data found in the diagram element");
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ export default function Home() {
|
|||||||
format: "xmlsvg",
|
format: "xmlsvg",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
console.log("chartXML from page", chartXML);
|
||||||
};
|
};
|
||||||
|
|
||||||
const loadDiagram = (chart: string) => {
|
const loadDiagram = (chart: string) => {
|
||||||
@@ -40,11 +41,15 @@ export default function Home() {
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
<Button
|
||||||
|
onClick={handleExport}
|
||||||
|
>export </Button>
|
||||||
<div className="w-1/3 p-1 border-gray-300">
|
<div className="w-1/3 p-1 border-gray-300">
|
||||||
<ChatPanel
|
<ChatPanel
|
||||||
onDisplayChart={(xml) => loadDiagram(xml)}
|
onDisplayChart={(xml) => loadDiagram(xml)}
|
||||||
onFetchChart={() => {
|
onFetchChart={() => {
|
||||||
handleExport();
|
handleExport();
|
||||||
|
console.log("chartXML from page", chartXML);
|
||||||
return chartXML;
|
return chartXML;
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|||||||
Reference in New Issue
Block a user