@nexface/chat 0.1.20-alpha.0 → 0.1.20-alpha.1

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.
@@ -46563,6 +46563,30 @@ function validateAgentToolInput(schema, value, path2 = "input") {
46563
46563
  // src/tool-presentation.tsx
46564
46564
  import { useChat, useToolPresentations } from "./react.js";
46565
46565
  import { Fragment as Fragment15, jsx as jsx97, jsxs as jsxs29 } from "react/jsx-runtime";
46566
+ function confirmationInputText(argsText, toolName, toolCallId, messages) {
46567
+ try {
46568
+ const wire = JSON.parse(argsText);
46569
+ if (!wire || typeof wire !== "object" || Array.isArray(wire)) return;
46570
+ if (Object.keys(wire).length !== 1) return;
46571
+ if (Object.hasOwn(wire, "input")) return JSON.stringify(wire.input);
46572
+ const answer = wire.confirmation;
46573
+ if (!answer || typeof answer.requestId !== "string" || typeof answer.approved !== "boolean")
46574
+ return;
46575
+ for (const message of messages)
46576
+ for (const part of message.parts) {
46577
+ if (part.type !== "tool") continue;
46578
+ if (part.toolCallId === toolCallId) return;
46579
+ if (part.name !== toolName || part.result === void 0) continue;
46580
+ const { value, omitted } = decodeToolResult(part.result, toolName);
46581
+ if (!omitted && value && typeof value === "object" && "status" in value && value.status === "confirmation_required" && "requestId" in value && value.requestId === answer.requestId) {
46582
+ const original = JSON.parse(part.arguments);
46583
+ if (original && Object.hasOwn(original, "input")) return JSON.stringify(original.input);
46584
+ }
46585
+ }
46586
+ } catch {
46587
+ }
46588
+ return;
46589
+ }
46566
46590
  var RenderBoundary = class extends Component {
46567
46591
  state = { failed: false };
46568
46592
  static getDerivedStateFromError() {
@@ -46589,21 +46613,34 @@ function ToolPresentationUI({
46589
46613
  const chat = useChat();
46590
46614
  const presentation = registry?.controller?.get(props.toolName);
46591
46615
  const schema = presentation?.definition.inputSchema;
46616
+ const argsText = useMemo13(
46617
+ () => presentation?.confirmationProtocol ? confirmationInputText(props.argsText, props.toolName, props.toolCallId, chat.messages) : props.argsText,
46618
+ [
46619
+ presentation?.confirmationProtocol,
46620
+ props.argsText,
46621
+ props.toolName,
46622
+ props.toolCallId,
46623
+ chat.messages
46624
+ ]
46625
+ );
46592
46626
  const parsed = useMemo13(() => {
46593
- if (!schema) return;
46627
+ if (!schema || argsText === void 0) return;
46594
46628
  try {
46595
- const input = JSON.parse(props.argsText);
46629
+ const input = JSON.parse(argsText);
46596
46630
  if (!validateAgentToolInput(schema, input).length) return { input };
46597
46631
  } catch {
46598
46632
  }
46599
46633
  return;
46600
- }, [schema, props.argsText]);
46634
+ }, [schema, argsText]);
46601
46635
  const decoded = useMemo13(
46602
46636
  () => typeof props.result === "string" ? decodeToolResult(props.result, props.toolName) : void 0,
46603
46637
  [props.result, props.toolName]
46604
46638
  );
46639
+ const confirmation = presentation?.confirmationProtocol && decoded?.value && typeof decoded.value === "object" ? decoded.value : void 0;
46640
+ const confirmationPending = confirmation?.status === "confirmation_required" && typeof confirmation.requestId === "string" && typeof confirmation.message === "string" && typeof confirmation.nextAction === "string" && Object.keys(confirmation).length === 4;
46641
+ const confirmationRejected = confirmation?.status === "rejected" && confirmation.toolExecuted === false && Object.keys(confirmation).every((key) => ["status", "requestId", "toolExecuted"].includes(key));
46605
46642
  const state = tool2?.type === "tool" ? tool2.toolState : void 0;
46606
- const status = state === "cancelled" || state === "rejected" || props.result === void 0 && props.status.type === "incomplete" && props.status.reason === "cancelled" ? "cancelled" : state || props.isError || props.result === void 0 && props.status.type === "incomplete" ? "error" : props.result === void 0 ? "running" : "success";
46643
+ const status = confirmationRejected || state === "cancelled" || state === "rejected" || props.result === void 0 && props.status.type === "incomplete" && props.status.reason === "cancelled" ? "cancelled" : state || props.isError || props.result === void 0 && props.status.type === "incomplete" ? "error" : props.result === void 0 ? "running" : "success";
46607
46644
  const context = useMemo13(
46608
46645
  () => parsed ? {
46609
46646
  input: parsed.input,
@@ -46613,7 +46650,7 @@ function ToolPresentationUI({
46613
46650
  [parsed, status, decoded, props.result]
46614
46651
  );
46615
46652
  const fallback = /* @__PURE__ */ jsx97(ToolCall, { ...props, presentation: tool2 });
46616
- if (!presentation || !context || decoded?.omitted || chat.interrupt?.toolName === props.toolName && tool2?.type === "tool" && tool2.result === void 0)
46653
+ if (!presentation || !context || confirmationPending || decoded?.omitted || chat.interrupt?.toolName === props.toolName && tool2?.type === "tool" && tool2.result === void 0)
46617
46654
  return fallback;
46618
46655
  return /* @__PURE__ */ jsxs29(
46619
46656
  RenderBoundary,
@@ -1,4 +1,4 @@
1
- export declare const TOOL_ERROR_CODES: readonly ["EXECUTION_OUTCOME_UNKNOWN", "ACTION_IN_FLIGHT", "ACTION_NOT_HANDLED", "ACTION_TARGET_UNAVAILABLE", "BROWSER_EXECUTION_FAILED", "BROWSER_SNAPSHOT_REQUIRED", "BROWSER_TOOLS_UNAVAILABLE", "CONTRACT_MISMATCH", "DUPLICATE_BINDING", "INVALID_INPUT", "INVALID_TOOL_RUNTIME", "OPERATION_BUSY", "TOOL_CANCELLED", "TOOL_CONFIRMATION_INVALIDATED", "TOOL_CONFIRMATION_REJECTED", "TOOL_ERROR", "TOOL_REGISTRATION_TIMEOUT", "TOOL_SKIPPED", "TOOL_UNAVAILABLE", "UNKNOWN_TOOL"];
1
+ export declare const TOOL_ERROR_CODES: readonly ["EXECUTION_OUTCOME_UNKNOWN", "ACTION_IN_FLIGHT", "ACTION_NOT_HANDLED", "ACTION_TARGET_UNAVAILABLE", "BROWSER_EXECUTION_FAILED", "BROWSER_SNAPSHOT_REQUIRED", "BROWSER_TOOLS_UNAVAILABLE", "CONTRACT_MISMATCH", "DUPLICATE_BINDING", "INVALID_INPUT", "INVALID_CONFIRMATION_RESULT", "TOOL_CONFIRMATION_LIMIT", "INVALID_TOOL_RUNTIME", "OPERATION_BUSY", "TOOL_CANCELLED", "TOOL_CONFIRMATION_INVALIDATED", "TOOL_CONFIRMATION_REJECTED", "TOOL_ERROR", "TOOL_REGISTRATION_TIMEOUT", "TOOL_SKIPPED", "TOOL_UNAVAILABLE", "UNKNOWN_TOOL"];
2
2
  type AgentToolErrorCode = (typeof TOOL_ERROR_CODES)[number];
3
3
  export declare class AgentToolError extends Error {
4
4
  readonly code: AgentToolErrorCode;
@@ -10,6 +10,8 @@ export const TOOL_ERROR_CODES = [
10
10
  "CONTRACT_MISMATCH",
11
11
  "DUPLICATE_BINDING",
12
12
  "INVALID_INPUT",
13
+ "INVALID_CONFIRMATION_RESULT",
14
+ "TOOL_CONFIRMATION_LIMIT",
13
15
  "INVALID_TOOL_RUNTIME",
14
16
  "OPERATION_BUSY",
15
17
  "TOOL_CANCELLED",
@@ -6,6 +6,7 @@ export interface ToolRenderContext<TInput = unknown, TResult = unknown> {
6
6
  }
7
7
  export interface ToolPresentation {
8
8
  definition: ReturnType<ToolRuntime["getTools"]>[number]["definition"];
9
+ confirmationProtocol?: true;
9
10
  render(context: ToolRenderContext): unknown;
10
11
  }
11
12
  export interface ToolPresentationController {
@@ -5,6 +5,46 @@ import { decodeToolResult } from "./tool-result.js";
5
5
  import { useChat, useToolPresentations } from "./react.js";
6
6
  import { ToolCall } from "./ui/home-demo/trace.js";
7
7
  import { cn } from "./ui/utils.js";
8
+ function confirmationInputText(argsText, toolName, toolCallId, messages) {
9
+ try {
10
+ const wire = JSON.parse(argsText);
11
+ if (!wire || typeof wire !== "object" || Array.isArray(wire))
12
+ return;
13
+ if (Object.keys(wire).length !== 1)
14
+ return;
15
+ if (Object.hasOwn(wire, "input"))
16
+ return JSON.stringify(wire.input);
17
+ const answer = wire.confirmation;
18
+ if (!answer || typeof answer.requestId !== "string" || typeof answer.approved !== "boolean")
19
+ return;
20
+ // Use the original receipt so settled calls also render after history recovery.
21
+ for (const message of messages)
22
+ for (const part of message.parts) {
23
+ if (part.type !== "tool")
24
+ continue;
25
+ if (part.toolCallId === toolCallId)
26
+ return;
27
+ if (part.name !== toolName || part.result === undefined)
28
+ continue;
29
+ const { value, omitted } = decodeToolResult(part.result, toolName);
30
+ if (!omitted &&
31
+ value &&
32
+ typeof value === "object" &&
33
+ "status" in value &&
34
+ value.status === "confirmation_required" &&
35
+ "requestId" in value &&
36
+ value.requestId === answer.requestId) {
37
+ const original = JSON.parse(part.arguments);
38
+ if (original && Object.hasOwn(original, "input"))
39
+ return JSON.stringify(original.input);
40
+ }
41
+ }
42
+ }
43
+ catch {
44
+ /* Invalid or unavailable business arguments use the default UI. */
45
+ }
46
+ return;
47
+ }
8
48
  class RenderBoundary extends Component {
9
49
  state = { failed: false };
10
50
  static getDerivedStateFromError() {
@@ -25,11 +65,20 @@ export function ToolPresentationUI({ presentation: tool, ...props }) {
25
65
  const chat = useChat();
26
66
  const presentation = registry?.controller?.get(props.toolName);
27
67
  const schema = presentation?.definition.inputSchema;
68
+ const argsText = useMemo(() => presentation?.confirmationProtocol
69
+ ? confirmationInputText(props.argsText, props.toolName, props.toolCallId, chat.messages)
70
+ : props.argsText, [
71
+ presentation?.confirmationProtocol,
72
+ props.argsText,
73
+ props.toolName,
74
+ props.toolCallId,
75
+ chat.messages,
76
+ ]);
28
77
  const parsed = useMemo(() => {
29
- if (!schema)
78
+ if (!schema || argsText === undefined)
30
79
  return;
31
80
  try {
32
- const input = JSON.parse(props.argsText);
81
+ const input = JSON.parse(argsText);
33
82
  if (!validateAgentToolInput(schema, input).length)
34
83
  return { input };
35
84
  }
@@ -37,10 +86,22 @@ export function ToolPresentationUI({ presentation: tool, ...props }) {
37
86
  /* Incomplete or invalid arguments use the default UI. */
38
87
  }
39
88
  return;
40
- }, [schema, props.argsText]);
89
+ }, [schema, argsText]);
41
90
  const decoded = useMemo(() => typeof props.result === "string" ? decodeToolResult(props.result, props.toolName) : undefined, [props.result, props.toolName]);
91
+ const confirmation = presentation?.confirmationProtocol && decoded?.value && typeof decoded.value === "object"
92
+ ? decoded.value
93
+ : undefined;
94
+ const confirmationPending = confirmation?.status === "confirmation_required" &&
95
+ typeof confirmation.requestId === "string" &&
96
+ typeof confirmation.message === "string" &&
97
+ typeof confirmation.nextAction === "string" &&
98
+ Object.keys(confirmation).length === 4;
99
+ const confirmationRejected = confirmation?.status === "rejected" &&
100
+ confirmation.toolExecuted === false &&
101
+ Object.keys(confirmation).every((key) => ["status", "requestId", "toolExecuted"].includes(key));
42
102
  const state = tool?.type === "tool" ? tool.toolState : undefined;
43
- const status = state === "cancelled" ||
103
+ const status = confirmationRejected ||
104
+ state === "cancelled" ||
44
105
  state === "rejected" ||
45
106
  (props.result === undefined &&
46
107
  props.status.type === "incomplete" &&
@@ -61,6 +122,7 @@ export function ToolPresentationUI({ presentation: tool, ...props }) {
61
122
  const fallback = _jsx(ToolCall, { ...props, presentation: tool });
62
123
  if (!presentation ||
63
124
  !context ||
125
+ confirmationPending ||
64
126
  decoded?.omitted ||
65
127
  (chat.interrupt?.toolName === props.toolName &&
66
128
  tool?.type === "tool" &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nexface/chat",
3
- "version": "0.1.20-alpha.0",
3
+ "version": "0.1.20-alpha.1",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "sideEffects": [
@@ -19,7 +19,7 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "@ag-ui/core": "0.0.59",
22
- "@nexface/agent": "0.1.20-alpha.0",
22
+ "@nexface/agent": "0.1.20-alpha.1",
23
23
  "class-variance-authority": "0.7.1",
24
24
  "clsx": "2.1.1",
25
25
  "fast-json-patch": "3.1.1",
@@ -28,7 +28,7 @@
28
28
  "use-sync-external-store": "1.7.0"
29
29
  },
30
30
  "peerDependencies": {
31
- "@nexface/tools": "0.1.20-alpha.0",
31
+ "@nexface/tools": "0.1.20-alpha.1",
32
32
  "react": ">=17",
33
33
  "react-dom": ">=17"
34
34
  },