openCanvas

This commit is contained in:
董宏凯 committed 2026-05-15 23:27:34 +08:00
1 parent 050b1ad78e
commit 1be600155b
18 files changed
+754 -101

No files matched your search

@@ -11,8 +11,14 @@ import type { CanvasMessage } from "../types/thread";
export function useCanvasStream() {
const navigate = useNavigate();
const { threadId, setThreadId, modelName } = useCanvasThreadContext();
const { messages, setMessages, appendMessage, setArtifact, setIsStreaming } =
useCanvasGraphContext();
const {
messages,
setMessages,
appendMessage,
setArtifact,
setStreamingArtifact,
setIsStreaming,
} = useCanvasGraphContext();
// Ref so callbacks inside streamCanvasRun always see the latest messages
// without adding `messages` to useCallback's dep array (avoids re-creation
@@ -51,24 +57,34 @@ export function useCanvasStream() {
};
// 4. Stream
await streamCanvasRun(activeThreadId, input, {
onMessage(msg) {
// Only surface AI text replies; skip tool messages
if (msg.type === "ai") {
appendMessage(msg);
}
await streamCanvasRun(
activeThreadId,
input,
{
onMessage(msg) {
if (msg.type === "ai" || msg.type === "AIMessageChunk") {
appendMessage(msg);
}
},
onStreamingArtifact(partial) {
setStreamingArtifact(partial);
},
onArtifact(artifact) {
setArtifact(artifact);
setStreamingArtifact(null);
},
onDone() {
setIsStreaming(false);
setStreamingArtifact(null);
},
onError(err) {
console.error("[canvas] stream error:", err);
setIsStreaming(false);
setStreamingArtifact(null);
},
},
onArtifact(artifact) {
setArtifact(artifact);
},
onDone() {
setIsStreaming(false);
},
onError(err) {
console.error("[canvas] stream error:", err);
setIsStreaming(false);
},
});
modelName ?? undefined,
);
},
// messagesRef replaces direct `messages` dep — intentional
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -80,6 +96,7 @@ export function useCanvasStream() {
appendMessage,
setMessages,
setArtifact,
setStreamingArtifact,
setIsStreaming,
],
);