|
| 1 | +/* |
| 2 | + * Copyright (c) 2021-2025 Datalayer, Inc. |
| 3 | + * |
| 4 | + * MIT License |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * React hooks for ag-ui tool registration |
| 9 | + * |
| 10 | + * @module tools/adapters/agui/hooks |
| 11 | + */ |
| 12 | + |
| 13 | +import { useEffect, useMemo } from "react"; |
| 14 | +import type { ToolDefinition } from "../../definitions/schema"; |
| 15 | +import type { ToolOperation } from "../../core/interfaces"; |
| 16 | +import type { SaaSToolContext } from "../saas/SaaSToolContext"; |
| 17 | +import { createAllCopilotKitActions } from "./AgUIToolAdapter"; |
| 18 | + |
| 19 | +// Type for useCopilotAction (from @copilotkit/react-core) |
| 20 | +type UseCopilotActionFn = (action: { |
| 21 | + name: string; |
| 22 | + description: string; |
| 23 | + parameters: any; |
| 24 | + handler: (params: any) => Promise<string>; |
| 25 | + render?: (props: any) => any; |
| 26 | +}) => void; |
| 27 | + |
| 28 | +/** |
| 29 | + * React hook that automatically registers all Datalayer tools with CopilotKit |
| 30 | + * |
| 31 | + * This hook converts unified tool definitions to CopilotKit actions and |
| 32 | + * registers them using useCopilotAction. |
| 33 | + * |
| 34 | + * @param definitions - Array of tool definitions (defaults to all) |
| 35 | + * @param operations - Registry of core operations (defaults to all) |
| 36 | + * @param context - SaaS tool context (required) |
| 37 | + * @param useCopilotAction - CopilotKit's useCopilotAction hook |
| 38 | + * |
| 39 | + * @example |
| 40 | + * ```typescript |
| 41 | + * import { useCopilotAction } from '@copilotkit/react-core'; |
| 42 | + * import { useNotebookTools } from './tools/adapters/agui'; |
| 43 | + * |
| 44 | + * function NotebookEditor() { |
| 45 | + * const context = useSaaSToolContext(); // Your context hook |
| 46 | + * |
| 47 | + * // Auto-register all notebook tools with CopilotKit |
| 48 | + * useNotebookTools(context, useCopilotAction); |
| 49 | + * |
| 50 | + * return <YourNotebookUI />; |
| 51 | + * } |
| 52 | + * ``` |
| 53 | + */ |
| 54 | +export function useNotebookTools( |
| 55 | + context: SaaSToolContext, |
| 56 | + useCopilotAction: UseCopilotActionFn, |
| 57 | + definitions?: ToolDefinition[], |
| 58 | + operations?: Record<string, ToolOperation<any, any>>, |
| 59 | +): void { |
| 60 | + // Import defaults if not provided |
| 61 | + const { allToolDefinitions } = require("../../definitions/tools"); |
| 62 | + const { allOperations } = require("../../core/operations"); |
| 63 | + |
| 64 | + const toolDefinitions = definitions || allToolDefinitions; |
| 65 | + const toolOperations = operations || allOperations; |
| 66 | + |
| 67 | + // Create CopilotKit actions from definitions |
| 68 | + const actions = useMemo( |
| 69 | + () => createAllCopilotKitActions(toolDefinitions, toolOperations, context), |
| 70 | + [toolDefinitions, toolOperations, context], |
| 71 | + ); |
| 72 | + |
| 73 | + // Register each action with CopilotKit |
| 74 | + useEffect(() => { |
| 75 | + console.log( |
| 76 | + `[ag-ui Tools] Registering ${actions.length} tools with CopilotKit`, |
| 77 | + ); |
| 78 | + |
| 79 | + actions.forEach((action) => { |
| 80 | + useCopilotAction(action); |
| 81 | + console.log(`[ag-ui Tools] ✓ Registered ${action.name}`); |
| 82 | + }); |
| 83 | + |
| 84 | + return () => { |
| 85 | + console.log("[ag-ui Tools] Cleanup (if needed)"); |
| 86 | + }; |
| 87 | + }, [actions, useCopilotAction]); |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * React hook that registers a single tool with CopilotKit |
| 92 | + * |
| 93 | + * Use this for selective tool registration or custom tools. |
| 94 | + * |
| 95 | + * @example |
| 96 | + * ```typescript |
| 97 | + * import { useSingleTool } from './tools/adapters/agui'; |
| 98 | + * |
| 99 | + * function MyComponent() { |
| 100 | + * const context = useSaaSToolContext(); |
| 101 | + * |
| 102 | + * // Register only insertCell tool |
| 103 | + * useSingleTool(insertCellTool, insertCellOperation, context, useCopilotAction); |
| 104 | + * |
| 105 | + * return <UI />; |
| 106 | + * } |
| 107 | + * ``` |
| 108 | + */ |
| 109 | +export function useSingleTool( |
| 110 | + definition: ToolDefinition, |
| 111 | + operation: ToolOperation<any, any>, |
| 112 | + context: SaaSToolContext, |
| 113 | + useCopilotAction: UseCopilotActionFn, |
| 114 | +): void { |
| 115 | + const action = useMemo( |
| 116 | + () => { |
| 117 | + const { createCopilotKitAction } = require("./AgUIToolAdapter"); |
| 118 | + return createCopilotKitAction(definition, operation, context); |
| 119 | + }, |
| 120 | + [definition, operation, context], |
| 121 | + ); |
| 122 | + |
| 123 | + useEffect(() => { |
| 124 | + console.log(`[ag-ui Tools] Registering ${action.name}`); |
| 125 | + useCopilotAction(action); |
| 126 | + |
| 127 | + return () => { |
| 128 | + console.log(`[ag-ui Tools] Cleanup for ${action.name}`); |
| 129 | + }; |
| 130 | + }, [action, useCopilotAction]); |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * React hook that provides tool actions without auto-registration |
| 135 | + * |
| 136 | + * Use this when you want manual control over registration or need |
| 137 | + * to inspect actions before registering. |
| 138 | + * |
| 139 | + * @returns Array of CopilotKit actions ready for registration |
| 140 | + * |
| 141 | + * @example |
| 142 | + * ```typescript |
| 143 | + * function MyComponent() { |
| 144 | + * const context = useSaaSToolContext(); |
| 145 | + * const actions = useToolActions(context); |
| 146 | + * |
| 147 | + * // Manually register specific actions |
| 148 | + * actions.filter(a => a.name.includes('cell')).forEach(useCopilotAction); |
| 149 | + * |
| 150 | + * return <UI />; |
| 151 | + * } |
| 152 | + * ``` |
| 153 | + */ |
| 154 | +export function useToolActions( |
| 155 | + context: SaaSToolContext, |
| 156 | + definitions?: ToolDefinition[], |
| 157 | + operations?: Record<string, ToolOperation<any, any>>, |
| 158 | +): ReturnType<typeof createAllCopilotKitActions> { |
| 159 | + const { allToolDefinitions } = require("../../definitions/tools"); |
| 160 | + const { allOperations } = require("../../core/operations"); |
| 161 | + |
| 162 | + const toolDefinitions = definitions || allToolDefinitions; |
| 163 | + const toolOperations = operations || allOperations; |
| 164 | + |
| 165 | + return useMemo( |
| 166 | + () => createAllCopilotKitActions(toolDefinitions, toolOperations, context), |
| 167 | + [toolDefinitions, toolOperations, context], |
| 168 | + ); |
| 169 | +} |
0 commit comments