@giselles-ai/browser-tool 0.1.11 → 0.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -84,4 +84,45 @@ declare function BrowserToolProvider({ endpoint, children, }: BrowserToolProvide
84
84
 
85
85
  declare function useBrowserTool(): BrowserToolContextValue;
86
86
 
87
- export { type BrowserToolContextValue, BrowserToolProvider, type BrowserToolProviderProps, type BrowserToolRelayHandler, PromptPanel, type PromptPanelProps, browserTool, useBrowserTool };
87
+ type ToolCallInfo = {
88
+ toolName: string;
89
+ toolCallId: string;
90
+ input?: unknown;
91
+ dynamic?: boolean;
92
+ };
93
+ type ToolOutputSuccess = {
94
+ tool: string;
95
+ toolCallId: string;
96
+ output: unknown;
97
+ };
98
+ type ToolOutputError = {
99
+ state: "output-error";
100
+ tool: string;
101
+ toolCallId: string;
102
+ errorText: string;
103
+ };
104
+ type AddToolOutputFn = (output: ToolOutputSuccess | ToolOutputError) => void;
105
+ type BrowserToolChatHandler = {
106
+ onToolCall: (options: {
107
+ toolCall: ToolCallInfo;
108
+ }) => Promise<void>;
109
+ connect: (addToolOutput: AddToolOutputFn) => void;
110
+ };
111
+ /**
112
+ * Hook that encapsulates browser-tool handling for `useChat`'s `onToolCall`.
113
+ *
114
+ * Usage:
115
+ * ```ts
116
+ * const browserTool = useBrowserToolHandler();
117
+ * const { addToolOutput, ...chat } = useChat({
118
+ * sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
119
+ * ...browserTool,
120
+ * });
121
+ * browserTool.connect(addToolOutput);
122
+ * ```
123
+ */
124
+ declare function useBrowserToolHandler(options?: {
125
+ onWarnings?: (warnings: string[]) => void;
126
+ }): BrowserToolChatHandler;
127
+
128
+ export { type BrowserToolChatHandler, type BrowserToolContextValue, BrowserToolProvider, type BrowserToolProviderProps, type BrowserToolRelayHandler, PromptPanel, type PromptPanelProps, browserTool, useBrowserTool, useBrowserToolHandler };
@@ -709,9 +709,80 @@ function PromptPanel({
709
709
  ] })
710
710
  ] }) });
711
711
  }
712
+
713
+ // src/react/use-chat-handler.ts
714
+ import { useCallback as useCallback2, useRef } from "react";
715
+ function isRecord3(value) {
716
+ return Boolean(value) && typeof value === "object";
717
+ }
718
+ function parseExecuteInput(value) {
719
+ if (!isRecord3(value)) {
720
+ return { actions: [], fields: [] };
721
+ }
722
+ return {
723
+ actions: Array.isArray(value.actions) ? value.actions : [],
724
+ fields: Array.isArray(value.fields) ? value.fields : []
725
+ };
726
+ }
727
+ function useBrowserToolHandler(options) {
728
+ const addToolOutputRef = useRef(void 0);
729
+ const onWarningsRef = useRef(options?.onWarnings);
730
+ onWarningsRef.current = options?.onWarnings;
731
+ const onToolCall = useCallback2(
732
+ async ({ toolCall }) => {
733
+ const addToolOutput = addToolOutputRef.current;
734
+ if (!addToolOutput || toolCall.dynamic) {
735
+ return;
736
+ }
737
+ try {
738
+ if (toolCall.toolName === "getFormSnapshot") {
739
+ const fields = snapshot();
740
+ addToolOutput({
741
+ tool: "getFormSnapshot",
742
+ toolCallId: toolCall.toolCallId,
743
+ output: { fields }
744
+ });
745
+ return;
746
+ }
747
+ if (toolCall.toolName === "executeFormActions") {
748
+ const { actions, fields } = parseExecuteInput(toolCall.input);
749
+ const report = execute(actions, fields);
750
+ if (report.warnings.length > 0) {
751
+ onWarningsRef.current?.(report.warnings);
752
+ }
753
+ addToolOutput({
754
+ tool: "executeFormActions",
755
+ toolCallId: toolCall.toolCallId,
756
+ output: { report }
757
+ });
758
+ return;
759
+ }
760
+ addToolOutput({
761
+ state: "output-error",
762
+ tool: toolCall.toolName,
763
+ toolCallId: toolCall.toolCallId,
764
+ errorText: `Unknown tool: ${toolCall.toolName}`
765
+ });
766
+ } catch (error) {
767
+ addToolOutput({
768
+ state: "output-error",
769
+ tool: toolCall.toolName,
770
+ toolCallId: toolCall.toolCallId,
771
+ errorText: error instanceof Error ? error.message : "Tool execution failed."
772
+ });
773
+ }
774
+ },
775
+ []
776
+ );
777
+ const connect = useCallback2((addToolOutput) => {
778
+ addToolOutputRef.current = addToolOutput;
779
+ }, []);
780
+ return { onToolCall, connect };
781
+ }
712
782
  export {
713
783
  BrowserToolProvider,
714
784
  PromptPanel,
715
785
  browserTool,
716
- useBrowserTool
786
+ useBrowserTool,
787
+ useBrowserToolHandler
717
788
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@giselles-ai/browser-tool",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "license": "Apache-2.0",