tool.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Derived from vercel/ai-elements (skills/ai-elements/scripts/tool.tsx).
  3. * Copyright 2023 Vercel, Inc. Licensed under Apache-2.0.
  4. * Modified by ZCode: local integration, formatting and adaptations.
  5. * See THIRD-PARTY-NOTICES.md in the repository root for license and provenance.
  6. */
  7. "use client";
  8. import {
  9. Confirmation,
  10. ConfirmationAccepted,
  11. ConfirmationAction,
  12. ConfirmationActions,
  13. ConfirmationRejected,
  14. ConfirmationRequest,
  15. ConfirmationTitle,
  16. } from "@/components/ai-elements/confirmation";
  17. import {
  18. Tool,
  19. ToolContent,
  20. ToolHeader,
  21. ToolInput,
  22. ToolOutput,
  23. } from "@/components/ai-elements/tool";
  24. import type { ToolUIPart } from "ai";
  25. import { CheckIcon, XIcon } from "lucide-react";
  26. import { nanoid } from "nanoid";
  27. const handleReject = () => {
  28. // In production, call addConfirmationResponse
  29. };
  30. const handleAccept = () => {
  31. // In production, call addConfirmationResponse
  32. };
  33. const toolCall: ToolUIPart = {
  34. errorText: undefined,
  35. input: {
  36. database: "analytics",
  37. params: ["2024-01-01"],
  38. query: "SELECT COUNT(*) FROM users WHERE created_at >= ?",
  39. },
  40. output: `| User ID | Name | Email | Created At |
  41. |---------|------|-------|------------|
  42. | 1 | John Doe | john@example.com | 2024-01-15 |
  43. | 2 | Jane Smith | jane@example.com | 2024-01-20 |
  44. | 3 | Bob Wilson | bob@example.com | 2024-02-01 |
  45. | 4 | Alice Brown | alice@example.com | 2024-02-10 |
  46. | 5 | Charlie Davis | charlie@example.com | 2024-02-15 |`,
  47. state: "output-available" as const,
  48. toolCallId: nanoid(),
  49. type: "tool-database_query" as const,
  50. };
  51. const Example = () => (
  52. <div className="space-y-4" style={{ minHeight: "1400px" }}>
  53. {/* 1. input-streaming: Pending */}
  54. <Tool defaultOpen>
  55. <ToolHeader state="input-streaming" title="database_query" type="tool-database_query" />
  56. <ToolContent>
  57. <ToolInput input={{}} />
  58. </ToolContent>
  59. </Tool>
  60. {/* 2. approval-requested: Awaiting Approval */}
  61. <Tool>
  62. <ToolHeader
  63. state={"approval-requested" as ToolUIPart["state"]}
  64. title="database_query"
  65. type="tool-database_query"
  66. />
  67. <ToolContent>
  68. <ToolInput input={toolCall.input} />
  69. <Confirmation approval={{ id: nanoid() }} state="approval-requested">
  70. <ConfirmationTitle>
  71. <ConfirmationRequest>
  72. This tool will execute a query on the production database.
  73. </ConfirmationRequest>
  74. <ConfirmationAccepted>
  75. <CheckIcon className="size-4 text-green-600 dark:text-green-400" />
  76. <span>Accepted</span>
  77. </ConfirmationAccepted>
  78. <ConfirmationRejected>
  79. <XIcon className="size-4 text-destructive" />
  80. <span>Rejected</span>
  81. </ConfirmationRejected>
  82. </ConfirmationTitle>
  83. <ConfirmationActions>
  84. <ConfirmationAction onClick={handleReject} variant="outline">
  85. Reject
  86. </ConfirmationAction>
  87. <ConfirmationAction onClick={handleAccept} variant="default">
  88. Accept
  89. </ConfirmationAction>
  90. </ConfirmationActions>
  91. </Confirmation>
  92. </ToolContent>
  93. </Tool>
  94. {/* 3. approval-responded: Responded */}
  95. <Tool>
  96. <ToolHeader
  97. state={"approval-responded" as ToolUIPart["state"]}
  98. title="database_query"
  99. type="tool-database_query"
  100. />
  101. <ToolContent>
  102. <ToolInput input={toolCall.input} />
  103. <Confirmation approval={{ approved: true, id: nanoid() }} state="approval-responded">
  104. <ConfirmationTitle>
  105. <ConfirmationRequest>
  106. This tool will execute a query on the production database.
  107. </ConfirmationRequest>
  108. <ConfirmationAccepted>
  109. <CheckIcon className="size-4 text-green-600 dark:text-green-400" />
  110. <span>Accepted</span>
  111. </ConfirmationAccepted>
  112. <ConfirmationRejected>
  113. <XIcon className="size-4 text-destructive" />
  114. <span>Rejected</span>
  115. </ConfirmationRejected>
  116. </ConfirmationTitle>
  117. </Confirmation>
  118. </ToolContent>
  119. </Tool>
  120. {/* 4. input-available: Running */}
  121. <Tool>
  122. <ToolHeader state="input-available" title="database_query" type="tool-database_query" />
  123. <ToolContent>
  124. <ToolInput input={toolCall.input} />
  125. </ToolContent>
  126. </Tool>
  127. {/* 5. output-available: Completed */}
  128. <Tool>
  129. <ToolHeader state={toolCall.state} type={toolCall.type} />
  130. <ToolContent>
  131. <ToolInput input={toolCall.input} />
  132. <Confirmation approval={{ approved: true, id: nanoid() }} state="output-available">
  133. <ConfirmationTitle>
  134. <ConfirmationRequest>
  135. This tool will execute a query on the production database.
  136. </ConfirmationRequest>
  137. <ConfirmationAccepted>
  138. <CheckIcon className="size-4 text-green-600 dark:text-green-400" />
  139. <span>Accepted</span>
  140. </ConfirmationAccepted>
  141. <ConfirmationRejected>
  142. <XIcon className="size-4 text-destructive" />
  143. <span>Rejected</span>
  144. </ConfirmationRejected>
  145. </ConfirmationTitle>
  146. </Confirmation>
  147. {toolCall.state === "output-available" && (
  148. <ToolOutput errorText={toolCall.errorText} output={toolCall.output} />
  149. )}
  150. </ToolContent>
  151. </Tool>
  152. {/* 6. output-error: Error */}
  153. <Tool>
  154. <ToolHeader state="output-error" title="database_query" type="tool-database_query" />
  155. <ToolContent>
  156. <ToolInput input={toolCall.input} />
  157. <ToolOutput
  158. errorText="Connection timeout: Unable to reach database server"
  159. output={undefined}
  160. />
  161. </ToolContent>
  162. </Tool>
  163. {/* 7. output-denied: Denied */}
  164. <Tool>
  165. <ToolHeader
  166. state={"output-denied" as ToolUIPart["state"]}
  167. title="database_query"
  168. type="tool-database_query"
  169. />
  170. <ToolContent>
  171. <ToolInput input={toolCall.input} />
  172. <Confirmation
  173. approval={{
  174. approved: false,
  175. id: nanoid(),
  176. reason: "Query could impact production performance",
  177. }}
  178. state="output-denied"
  179. >
  180. <ConfirmationTitle>
  181. <ConfirmationRequest>
  182. This tool will execute a query on the production database.
  183. </ConfirmationRequest>
  184. <ConfirmationAccepted>
  185. <CheckIcon className="size-4 text-green-600 dark:text-green-400" />
  186. <span>Accepted</span>
  187. </ConfirmationAccepted>
  188. <ConfirmationRejected>
  189. <XIcon className="size-4 text-destructive" />
  190. <span>Rejected: Query could impact production performance</span>
  191. </ConfirmationRejected>
  192. </ConfirmationTitle>
  193. </Confirmation>
  194. </ToolContent>
  195. </Tool>
  196. </div>
  197. );
  198. export default Example;