Spaces:
Running
Running
refactor: update AgenticInterface and related tools to use useSquareStore
Browse files- Replace useChatStore with useSquareStore in AgenticInterface for square color management
- Simplify handler functions for square color tools
- Remove square color state management from useChatStore
src/components/AgenticInterface.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import { useChat } from "@/hooks/useChat";
|
| 2 |
-
import {
|
| 3 |
import {
|
| 4 |
getSquareColorTool,
|
| 5 |
handleGetSquareColor,
|
|
@@ -11,17 +11,16 @@ import { ColorControlForm } from "./ColorControlForm";
|
|
| 11 |
import { ColorVisualizer } from "./ColorVisualizer";
|
| 12 |
|
| 13 |
export function AgenticInterface() {
|
| 14 |
-
const { squareColor, setSquareColor } =
|
| 15 |
|
| 16 |
const { messages, sendMessage, isProcessing, loadingStatus } = useChat([
|
| 17 |
{
|
| 18 |
tool: setSquareColorTool,
|
| 19 |
-
handler:
|
| 20 |
},
|
| 21 |
{
|
| 22 |
tool: getSquareColorTool,
|
| 23 |
-
handler:
|
| 24 |
-
handleGetSquareColor({ color: useChatStore.getState().squareColor }),
|
| 25 |
},
|
| 26 |
]);
|
| 27 |
|
|
|
|
| 1 |
import { useChat } from "@/hooks/useChat";
|
| 2 |
+
import { useSquareStore } from "@/store/useSquareStore";
|
| 3 |
import {
|
| 4 |
getSquareColorTool,
|
| 5 |
handleGetSquareColor,
|
|
|
|
| 11 |
import { ColorVisualizer } from "./ColorVisualizer";
|
| 12 |
|
| 13 |
export function AgenticInterface() {
|
| 14 |
+
const { squareColor, setSquareColor } = useSquareStore();
|
| 15 |
|
| 16 |
const { messages, sendMessage, isProcessing, loadingStatus } = useChat([
|
| 17 |
{
|
| 18 |
tool: setSquareColorTool,
|
| 19 |
+
handler: handleSetSquareColor,
|
| 20 |
},
|
| 21 |
{
|
| 22 |
tool: getSquareColorTool,
|
| 23 |
+
handler: handleGetSquareColor,
|
|
|
|
| 24 |
},
|
| 25 |
]);
|
| 26 |
|
src/store/useChatStore.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
| 1 |
import { create } from "zustand";
|
| 2 |
-
import type {
|
| 3 |
-
ChatMessage,
|
| 4 |
-
FunctionCallResult,
|
| 5 |
-
UIMessage,
|
| 6 |
-
} from "@/types/chat";
|
| 7 |
|
| 8 |
const INITIAL_MESSAGE: UIMessage = {
|
| 9 |
id: 1,
|
|
@@ -12,11 +8,9 @@ const INITIAL_MESSAGE: UIMessage = {
|
|
| 12 |
};
|
| 13 |
|
| 14 |
interface ChatState {
|
| 15 |
-
// UI Messages (displayed in chat)
|
| 16 |
messages: UIMessage[];
|
| 17 |
addMessage: (message: UIMessage) => void;
|
| 18 |
|
| 19 |
-
// ML Conversation (for multi-turn function calling)
|
| 20 |
conversationMessages: ChatMessage[];
|
| 21 |
lastFunctionCall: FunctionCallResult;
|
| 22 |
setConversation: (
|
|
@@ -24,27 +18,17 @@ interface ChatState {
|
|
| 24 |
functionCall: FunctionCallResult,
|
| 25 |
) => void;
|
| 26 |
clearConversation: () => void;
|
| 27 |
-
|
| 28 |
-
// App State
|
| 29 |
-
squareColor: string;
|
| 30 |
-
setSquareColor: (color: string) => void;
|
| 31 |
}
|
| 32 |
|
| 33 |
export const useChatStore = create<ChatState>((set) => ({
|
| 34 |
-
// UI Messages
|
| 35 |
messages: [INITIAL_MESSAGE],
|
| 36 |
addMessage: (message) =>
|
| 37 |
set((state) => ({ messages: [...state.messages, message] })),
|
| 38 |
|
| 39 |
-
// ML Conversation
|
| 40 |
conversationMessages: [],
|
| 41 |
lastFunctionCall: null,
|
| 42 |
setConversation: (conversationMessages, lastFunctionCall) =>
|
| 43 |
set({ conversationMessages, lastFunctionCall }),
|
| 44 |
clearConversation: () =>
|
| 45 |
set({ conversationMessages: [], lastFunctionCall: null }),
|
| 46 |
-
|
| 47 |
-
// App State
|
| 48 |
-
squareColor: "rebeccapurple",
|
| 49 |
-
setSquareColor: (squareColor) => set({ squareColor }),
|
| 50 |
}));
|
|
|
|
| 1 |
import { create } from "zustand";
|
| 2 |
+
import type { ChatMessage, FunctionCallResult, UIMessage } from "@/types/chat";
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
const INITIAL_MESSAGE: UIMessage = {
|
| 5 |
id: 1,
|
|
|
|
| 8 |
};
|
| 9 |
|
| 10 |
interface ChatState {
|
|
|
|
| 11 |
messages: UIMessage[];
|
| 12 |
addMessage: (message: UIMessage) => void;
|
| 13 |
|
|
|
|
| 14 |
conversationMessages: ChatMessage[];
|
| 15 |
lastFunctionCall: FunctionCallResult;
|
| 16 |
setConversation: (
|
|
|
|
| 18 |
functionCall: FunctionCallResult,
|
| 19 |
) => void;
|
| 20 |
clearConversation: () => void;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
|
| 23 |
export const useChatStore = create<ChatState>((set) => ({
|
|
|
|
| 24 |
messages: [INITIAL_MESSAGE],
|
| 25 |
addMessage: (message) =>
|
| 26 |
set((state) => ({ messages: [...state.messages, message] })),
|
| 27 |
|
|
|
|
| 28 |
conversationMessages: [],
|
| 29 |
lastFunctionCall: null,
|
| 30 |
setConversation: (conversationMessages, lastFunctionCall) =>
|
| 31 |
set({ conversationMessages, lastFunctionCall }),
|
| 32 |
clearConversation: () =>
|
| 33 |
set({ conversationMessages: [], lastFunctionCall: null }),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
}));
|
src/store/useSquareStore.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { create } from "zustand";
|
| 2 |
+
|
| 3 |
+
type SquareState = {
|
| 4 |
+
squareColor: string;
|
| 5 |
+
setSquareColor: (color: string) => void;
|
| 6 |
+
};
|
| 7 |
+
|
| 8 |
+
export const useSquareStore = create<SquareState>((set) => ({
|
| 9 |
+
squareColor: "rebeccapurple",
|
| 10 |
+
setSquareColor: (squareColor) => set({ squareColor }),
|
| 11 |
+
}));
|
| 12 |
+
|
| 13 |
+
|
src/tools/definitions/getSquareColor.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
|
|
| 1 |
import type { ToolDefinition } from "@/types/chat";
|
| 2 |
|
| 3 |
-
type GetSquareColorContext = {
|
| 4 |
-
color: string;
|
| 5 |
-
};
|
| 6 |
-
|
| 7 |
export const getSquareColorTool: ToolDefinition = {
|
| 8 |
type: "function",
|
| 9 |
function: {
|
|
@@ -18,6 +15,6 @@ export const getSquareColorTool: ToolDefinition = {
|
|
| 18 |
},
|
| 19 |
};
|
| 20 |
|
| 21 |
-
export function handleGetSquareColor(
|
| 22 |
-
return { color:
|
| 23 |
}
|
|
|
|
| 1 |
+
import { useSquareStore } from "@/store/useSquareStore";
|
| 2 |
import type { ToolDefinition } from "@/types/chat";
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
export const getSquareColorTool: ToolDefinition = {
|
| 5 |
type: "function",
|
| 6 |
function: {
|
|
|
|
| 15 |
},
|
| 16 |
};
|
| 17 |
|
| 18 |
+
export function handleGetSquareColor() {
|
| 19 |
+
return { color: useSquareStore.getState().squareColor };
|
| 20 |
}
|
src/tools/definitions/setSquareColor.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
|
|
| 1 |
import type { ToolDefinition } from "@/types/chat";
|
| 2 |
|
| 3 |
-
type SetSquareColorContext = {
|
| 4 |
-
setSquareColor: (color: string) => void;
|
| 5 |
-
};
|
| 6 |
-
|
| 7 |
export const setSquareColorTool: ToolDefinition = {
|
| 8 |
type: "function",
|
| 9 |
function: {
|
|
@@ -22,12 +19,9 @@ export const setSquareColorTool: ToolDefinition = {
|
|
| 22 |
},
|
| 23 |
};
|
| 24 |
|
| 25 |
-
export function handleSetSquareColor(
|
| 26 |
-
args: Record<string, unknown>,
|
| 27 |
-
ctx: SetSquareColorContext,
|
| 28 |
-
) {
|
| 29 |
const { color } = args;
|
| 30 |
if (typeof color !== "string" || !color) return { error: "Invalid args" };
|
| 31 |
-
|
| 32 |
return { success: true, color };
|
| 33 |
}
|
|
|
|
| 1 |
+
import { useSquareStore } from "@/store/useSquareStore";
|
| 2 |
import type { ToolDefinition } from "@/types/chat";
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
export const setSquareColorTool: ToolDefinition = {
|
| 5 |
type: "function",
|
| 6 |
function: {
|
|
|
|
| 19 |
},
|
| 20 |
};
|
| 21 |
|
| 22 |
+
export function handleSetSquareColor(args: Record<string, unknown>) {
|
|
|
|
|
|
|
|
|
|
| 23 |
const { color } = args;
|
| 24 |
if (typeof color !== "string" || !color) return { error: "Invalid args" };
|
| 25 |
+
useSquareStore.getState().setSquareColor(color);
|
| 26 |
return { success: true, color };
|
| 27 |
}
|