mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-01-02 22:32:27 +08:00
fix: prevent duplicate history entries when edit_diagram tool is called (#64)
- Add handleExportWithoutHistory function for fetching current diagram state without saving to history - Update onFetchChart to accept saveToHistory parameter (defaults to true) - edit_diagram tool now fetches with saveToHistory=false since it only needs the current state - Only the initial form submission saves to history as intended
This commit is contained in:
@@ -27,18 +27,23 @@ export default function ChatPanel({
|
|||||||
const {
|
const {
|
||||||
loadDiagram: onDisplayChart,
|
loadDiagram: onDisplayChart,
|
||||||
handleExport: onExport,
|
handleExport: onExport,
|
||||||
|
handleExportWithoutHistory,
|
||||||
resolverRef,
|
resolverRef,
|
||||||
chartXML,
|
chartXML,
|
||||||
clearDiagram,
|
clearDiagram,
|
||||||
} = useDiagram();
|
} = useDiagram();
|
||||||
|
|
||||||
const onFetchChart = () => {
|
const onFetchChart = (saveToHistory = true) => {
|
||||||
return Promise.race([
|
return Promise.race([
|
||||||
new Promise<string>((resolve) => {
|
new Promise<string>((resolve) => {
|
||||||
if (resolverRef && "current" in resolverRef) {
|
if (resolverRef && "current" in resolverRef) {
|
||||||
resolverRef.current = resolve;
|
resolverRef.current = resolve;
|
||||||
}
|
}
|
||||||
|
if (saveToHistory) {
|
||||||
onExport();
|
onExport();
|
||||||
|
} else {
|
||||||
|
handleExportWithoutHistory();
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
new Promise<string>((_, reject) =>
|
new Promise<string>((_, reject) =>
|
||||||
setTimeout(
|
setTimeout(
|
||||||
@@ -87,7 +92,8 @@ export default function ChatPanel({
|
|||||||
|
|
||||||
let currentXml = "";
|
let currentXml = "";
|
||||||
try {
|
try {
|
||||||
currentXml = await onFetchChart();
|
// Fetch without saving to history - edit_diagram shouldn't create history entry
|
||||||
|
currentXml = await onFetchChart(false);
|
||||||
|
|
||||||
const { replaceXMLParts } = await import("@/lib/utils");
|
const { replaceXMLParts } = await import("@/lib/utils");
|
||||||
const editedXml = replaceXMLParts(currentXml, edits);
|
const editedXml = replaceXMLParts(currentXml, edits);
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ interface DiagramContextType {
|
|||||||
diagramHistory: { svg: string; xml: string }[];
|
diagramHistory: { svg: string; xml: string }[];
|
||||||
loadDiagram: (chart: string) => void;
|
loadDiagram: (chart: string) => void;
|
||||||
handleExport: () => void;
|
handleExport: () => void;
|
||||||
|
handleExportWithoutHistory: () => void;
|
||||||
resolverRef: React.Ref<((value: string) => void) | null>;
|
resolverRef: React.Ref<((value: string) => void) | null>;
|
||||||
drawioRef: React.Ref<DrawIoEmbedRef | null>;
|
drawioRef: React.Ref<DrawIoEmbedRef | null>;
|
||||||
handleDiagramExport: (data: any) => void;
|
handleDiagramExport: (data: any) => void;
|
||||||
@@ -42,6 +43,15 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleExportWithoutHistory = () => {
|
||||||
|
if (drawioRef.current) {
|
||||||
|
// Export without saving to history (for edit_diagram fetching current state)
|
||||||
|
drawioRef.current.exportDiagram({
|
||||||
|
format: "xmlsvg",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const loadDiagram = (chart: string) => {
|
const loadDiagram = (chart: string) => {
|
||||||
if (drawioRef.current) {
|
if (drawioRef.current) {
|
||||||
drawioRef.current.load({
|
drawioRef.current.load({
|
||||||
@@ -124,6 +134,7 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
|
|||||||
diagramHistory,
|
diagramHistory,
|
||||||
loadDiagram,
|
loadDiagram,
|
||||||
handleExport,
|
handleExport,
|
||||||
|
handleExportWithoutHistory,
|
||||||
resolverRef,
|
resolverRef,
|
||||||
drawioRef,
|
drawioRef,
|
||||||
handleDiagramExport,
|
handleDiagramExport,
|
||||||
|
|||||||
Reference in New Issue
Block a user