@copilotkit/react-core 1.67.1 → 1.68.0

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.
package/dist/index.cjs CHANGED
@@ -1,13 +1,14 @@
1
1
  "use client";
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
- const require_copilotkit = require('./copilotkit-CmMYFfLV.cjs');
4
+ const require_copilotkit = require('./copilotkit-8KSSmCH_.cjs');
5
5
  let react = require("react");
6
6
  react = require_copilotkit.__toESM(react);
7
7
  let _copilotkit_core = require("@copilotkit/core");
8
8
  let _ag_ui_client = require("@ag-ui/client");
9
9
  let _copilotkit_shared = require("@copilotkit/shared");
10
10
  let _copilotkit_runtime_client_gql = require("@copilotkit/runtime-client-gql");
11
+ let _copilotkit_react_core_v2_context = require("@copilotkit/react-core/v2/context");
11
12
 
12
13
  //#region src/utils/suggestions-constants.ts
13
14
  /**
@@ -37,7 +38,7 @@ function useLazyToolRenderer() {
37
38
  //#endregion
38
39
  //#region src/hooks/use-copilot-chat_internal.ts
39
40
  function useCopilotChatInternal({ suggestions, onInProgress, onSubmitMessage, onStopGeneration, onReloadMessages } = {}) {
40
- const { copilotkit } = require_copilotkit.useCopilotKit();
41
+ const { copilotkit } = (0, _copilotkit_react_core_v2_context.useCopilotKit)();
41
42
  const { threadId, agentSession } = require_copilotkit.useCopilotContext();
42
43
  const existingConfig = require_copilotkit.useCopilotChatConfiguration();
43
44
  const [agentAvailable, setAgentAvailable] = (0, react.useState)(false);
@@ -615,7 +616,7 @@ function useFrontendTool(tool, dependencies) {
615
616
  //#endregion
616
617
  //#region src/hooks/use-render-tool-call.ts
617
618
  function useRenderToolCall(tool, dependencies) {
618
- const { copilotkit } = require_copilotkit.useCopilotKit();
619
+ const { copilotkit } = (0, _copilotkit_react_core_v2_context.useCopilotKit)();
619
620
  const hasAddedRef = (0, react.useRef)(false);
620
621
  (0, react.useEffect)(() => {
621
622
  const { name, parameters, render } = tool;
@@ -1041,8 +1042,7 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
1041
1042
  //#region src/hooks/use-copilot-readable.ts
1042
1043
  /**
1043
1044
  * `useCopilotReadable` is a React hook that provides app-state and other information
1044
- * to the Copilot. Optionally, the hook can also handle hierarchical state within your
1045
- * application, passing these parent-child relationships to the Copilot.
1045
+ * to the Copilot.
1046
1046
  *
1047
1047
  * ## Usage
1048
1048
  *
@@ -1065,75 +1065,74 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
1065
1065
  * }
1066
1066
  * ```
1067
1067
  *
1068
- * ### Nested Components
1068
+ * ### Custom Serialization
1069
1069
  *
1070
- * Optionally, you can maintain the hierarchical structure of information by passing
1071
- * `parentId`. This allows you to use `useCopilotReadable` in nested components:
1070
+ * By default the value is serialized with `JSON.stringify`. Pass `convert` to
1071
+ * control the string the Copilot sees. It is called with the description and the
1072
+ * value, in that order:
1072
1073
  *
1073
- * ```tsx /employeeContextId/1 {17,23}
1074
- * import { useCopilotReadable } from "@copilotkit/react-core";
1074
+ * ```tsx
1075
+ * useCopilotReadable({
1076
+ * description: "The current user",
1077
+ * value: user,
1078
+ * convert: (description, value) => `${description}: ${value.firstName} ${value.lastName}`,
1079
+ * });
1080
+ * ```
1075
1081
  *
1076
- * function Employee(props: EmployeeProps) {
1077
- * const { employeeName, workProfile, metadata } = props;
1082
+ * ### Conditional Usage
1078
1083
  *
1079
- * // propagate any information to copilot
1080
- * const employeeContextId = useCopilotReadable({
1081
- * description: "Employee name",
1082
- * value: employeeName
1083
- * });
1084
+ * Toggle `available` to add or remove the context as your app state changes.
1085
+ * Switching to `"disabled"` removes the entry from the Copilot context; switching
1086
+ * back to `"enabled"` re-adds it.
1084
1087
  *
1085
- * // Pass a parentID to maintain a hierarchical structure.
1086
- * // Especially useful with child React components, list elements, etc.
1087
- * useCopilotReadable({
1088
- * description: "Work profile",
1089
- * value: workProfile.description(),
1090
- * parentId: employeeContextId
1091
- * });
1088
+ * ```tsx
1089
+ * useCopilotReadable({
1090
+ * description: "The list of employees",
1091
+ * value: employees,
1092
+ * available: showEmployees ? "enabled" : "disabled",
1093
+ * });
1094
+ * ```
1092
1095
  *
1093
- * useCopilotReadable({
1094
- * description: "Employee metadata",
1095
- * value: metadata.description(),
1096
- * parentId: employeeContextId
1097
- * });
1096
+ * ### Re-running on Custom Dependencies
1098
1097
  *
1099
- * return (
1100
- * // Render as usual...
1101
- * );
1102
- * }
1098
+ * The context is refreshed whenever `description`, `value`, `convert` or
1099
+ * `available` change. Pass a second argument to add your own dependencies:
1100
+ *
1101
+ * ```tsx
1102
+ * useCopilotReadable(
1103
+ * {
1104
+ * description: "The selected employee",
1105
+ * value: employee,
1106
+ * },
1107
+ * [departmentId],
1108
+ * );
1103
1109
  * ```
1104
1110
  */
1105
1111
  /**
1106
1112
  * Adds the given information to the Copilot context to make it readable by Copilot.
1107
1113
  */
1108
- function useCopilotReadable({ description, value, convert, available }, dependencies) {
1109
- const { copilotkit } = require_copilotkit.useCopilotKit();
1114
+ function useCopilotReadable({ description, value, convert, available = "enabled" }, dependencies) {
1115
+ const { copilotkit } = (0, _copilotkit_react_core_v2_context.useCopilotKit)();
1110
1116
  const ctxIdRef = (0, react.useRef)(void 0);
1111
1117
  (0, react.useEffect)(() => {
1112
1118
  if (!copilotkit) return;
1113
- const found = Object.entries(copilotkit.context).find(([id, ctxItem]) => {
1114
- return JSON.stringify({
1115
- description,
1116
- value
1117
- }) == JSON.stringify(ctxItem);
1118
- });
1119
- if (found) {
1120
- ctxIdRef.current = found[0];
1121
- if (available === "disabled") copilotkit.removeContext(ctxIdRef.current);
1122
- return;
1123
- }
1124
- if (!found && available === "disabled") return;
1125
- ctxIdRef.current = copilotkit.addContext({
1119
+ if (available === "disabled") return;
1120
+ const id = copilotkit.addContext({
1126
1121
  description,
1127
- value: (convert ?? JSON.stringify)(value)
1122
+ value: convert ? convert(description, value) : JSON.stringify(value)
1128
1123
  });
1124
+ ctxIdRef.current = id;
1129
1125
  return () => {
1130
- if (!ctxIdRef.current) return;
1131
- copilotkit.removeContext(ctxIdRef.current);
1126
+ copilotkit.removeContext(id);
1127
+ if (ctxIdRef.current === id) ctxIdRef.current = void 0;
1132
1128
  };
1133
1129
  }, [
1130
+ copilotkit,
1134
1131
  description,
1135
1132
  value,
1136
- convert
1133
+ convert,
1134
+ available,
1135
+ ...dependencies || []
1137
1136
  ]);
1138
1137
  return ctxIdRef.current;
1139
1138
  }
@@ -1268,7 +1267,7 @@ function useAgentNodeName(agentName) {
1268
1267
  */
1269
1268
  function useCoAgent(options) {
1270
1269
  const { agent } = require_copilotkit.useAgent({ agentId: options.name });
1271
- const { copilotkit } = require_copilotkit.useCopilotKit();
1270
+ const { copilotkit } = (0, _copilotkit_react_core_v2_context.useCopilotKit)();
1272
1271
  const nodeName = useAgentNodeName(options.name);
1273
1272
  const handleStateUpdate = (0, react.useCallback)((newState) => {
1274
1273
  if (!agent) return;