@copilotkitnext/react 1.54.0-next.6 → 1.54.0-next.8

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.
@@ -10,7 +10,7 @@ function useAgentContext(context) {
10
10
  if (typeof value === "string") return value;
11
11
  return JSON.stringify(value);
12
12
  }, [value]);
13
- (0, react.useEffect)(() => {
13
+ (0, react.useLayoutEffect)(() => {
14
14
  if (!copilotkit) return;
15
15
  const id = copilotkit.addContext({
16
16
  description,
@@ -1 +1 @@
1
- {"version":3,"file":"use-agent-context.cjs","names":["useCopilotKit"],"sources":["../../src/hooks/use-agent-context.tsx"],"sourcesContent":["import { useCopilotKit } from \"../providers/CopilotKitProvider\";\nimport { useEffect, useMemo } from \"react\";\n\n/**\n * Represents any value that can be serialized to JSON.\n */\nexport type JsonSerializable =\n | string\n | number\n | boolean\n | null\n | JsonSerializable[]\n | { [key: string]: JsonSerializable };\n\n/**\n * Context configuration for useAgentContext.\n * Accepts any JSON-serializable value which will be converted to a string.\n */\nexport interface AgentContextInput {\n /** A human-readable description of what this context represents */\n description: string;\n /** The context value - will be converted to a JSON string if not already a string */\n value: JsonSerializable;\n}\n\nexport function useAgentContext(context: AgentContextInput) {\n const { description, value } = context;\n const { copilotkit } = useCopilotKit();\n\n const stringValue = useMemo(() => {\n if (typeof value === \"string\") {\n return value;\n }\n return JSON.stringify(value);\n }, [value]);\n\n useEffect(() => {\n if (!copilotkit) return;\n\n const id = copilotkit.addContext({ description, value: stringValue });\n return () => {\n copilotkit.removeContext(id);\n };\n }, [description, stringValue, copilotkit]);\n}\n"],"mappings":";;;;;AAyBA,SAAgB,gBAAgB,SAA4B;CAC1D,MAAM,EAAE,aAAa,UAAU;CAC/B,MAAM,EAAE,eAAeA,0CAAe;CAEtC,MAAM,uCAA4B;AAChC,MAAI,OAAO,UAAU,SACnB,QAAO;AAET,SAAO,KAAK,UAAU,MAAM;IAC3B,CAAC,MAAM,CAAC;AAEX,4BAAgB;AACd,MAAI,CAAC,WAAY;EAEjB,MAAM,KAAK,WAAW,WAAW;GAAE;GAAa,OAAO;GAAa,CAAC;AACrE,eAAa;AACX,cAAW,cAAc,GAAG;;IAE7B;EAAC;EAAa;EAAa;EAAW,CAAC"}
1
+ {"version":3,"file":"use-agent-context.cjs","names":["useCopilotKit"],"sources":["../../src/hooks/use-agent-context.tsx"],"sourcesContent":["import { useCopilotKit } from \"../providers/CopilotKitProvider\";\nimport { useLayoutEffect, useMemo } from \"react\";\n\n/**\n * Represents any value that can be serialized to JSON.\n */\nexport type JsonSerializable =\n | string\n | number\n | boolean\n | null\n | JsonSerializable[]\n | { [key: string]: JsonSerializable };\n\n/**\n * Context configuration for useAgentContext.\n * Accepts any JSON-serializable value which will be converted to a string.\n */\nexport interface AgentContextInput {\n /** A human-readable description of what this context represents */\n description: string;\n /** The context value - will be converted to a JSON string if not already a string */\n value: JsonSerializable;\n}\n\nexport function useAgentContext(context: AgentContextInput) {\n const { description, value } = context;\n const { copilotkit } = useCopilotKit();\n\n const stringValue = useMemo(() => {\n if (typeof value === \"string\") {\n return value;\n }\n return JSON.stringify(value);\n }, [value]);\n\n useLayoutEffect(() => {\n if (!copilotkit) return;\n\n const id = copilotkit.addContext({ description, value: stringValue });\n return () => {\n copilotkit.removeContext(id);\n };\n }, [description, stringValue, copilotkit]);\n}\n"],"mappings":";;;;;AAyBA,SAAgB,gBAAgB,SAA4B;CAC1D,MAAM,EAAE,aAAa,UAAU;CAC/B,MAAM,EAAE,eAAeA,0CAAe;CAEtC,MAAM,uCAA4B;AAChC,MAAI,OAAO,UAAU,SACnB,QAAO;AAET,SAAO,KAAK,UAAU,MAAM;IAC3B,CAAC,MAAM,CAAC;AAEX,kCAAsB;AACpB,MAAI,CAAC,WAAY;EAEjB,MAAM,KAAK,WAAW,WAAW;GAAE;GAAa,OAAO;GAAa,CAAC;AACrE,eAAa;AACX,cAAW,cAAc,GAAG;;IAE7B;EAAC;EAAa;EAAa;EAAW,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import { useCopilotKit } from "../providers/CopilotKitProvider.mjs";
2
- import { useEffect, useMemo } from "react";
2
+ import { useLayoutEffect, useMemo } from "react";
3
3
 
4
4
  //#region src/hooks/use-agent-context.tsx
5
5
  function useAgentContext(context) {
@@ -9,7 +9,7 @@ function useAgentContext(context) {
9
9
  if (typeof value === "string") return value;
10
10
  return JSON.stringify(value);
11
11
  }, [value]);
12
- useEffect(() => {
12
+ useLayoutEffect(() => {
13
13
  if (!copilotkit) return;
14
14
  const id = copilotkit.addContext({
15
15
  description,
@@ -1 +1 @@
1
- {"version":3,"file":"use-agent-context.mjs","names":[],"sources":["../../src/hooks/use-agent-context.tsx"],"sourcesContent":["import { useCopilotKit } from \"../providers/CopilotKitProvider\";\nimport { useEffect, useMemo } from \"react\";\n\n/**\n * Represents any value that can be serialized to JSON.\n */\nexport type JsonSerializable =\n | string\n | number\n | boolean\n | null\n | JsonSerializable[]\n | { [key: string]: JsonSerializable };\n\n/**\n * Context configuration for useAgentContext.\n * Accepts any JSON-serializable value which will be converted to a string.\n */\nexport interface AgentContextInput {\n /** A human-readable description of what this context represents */\n description: string;\n /** The context value - will be converted to a JSON string if not already a string */\n value: JsonSerializable;\n}\n\nexport function useAgentContext(context: AgentContextInput) {\n const { description, value } = context;\n const { copilotkit } = useCopilotKit();\n\n const stringValue = useMemo(() => {\n if (typeof value === \"string\") {\n return value;\n }\n return JSON.stringify(value);\n }, [value]);\n\n useEffect(() => {\n if (!copilotkit) return;\n\n const id = copilotkit.addContext({ description, value: stringValue });\n return () => {\n copilotkit.removeContext(id);\n };\n }, [description, stringValue, copilotkit]);\n}\n"],"mappings":";;;;AAyBA,SAAgB,gBAAgB,SAA4B;CAC1D,MAAM,EAAE,aAAa,UAAU;CAC/B,MAAM,EAAE,eAAe,eAAe;CAEtC,MAAM,cAAc,cAAc;AAChC,MAAI,OAAO,UAAU,SACnB,QAAO;AAET,SAAO,KAAK,UAAU,MAAM;IAC3B,CAAC,MAAM,CAAC;AAEX,iBAAgB;AACd,MAAI,CAAC,WAAY;EAEjB,MAAM,KAAK,WAAW,WAAW;GAAE;GAAa,OAAO;GAAa,CAAC;AACrE,eAAa;AACX,cAAW,cAAc,GAAG;;IAE7B;EAAC;EAAa;EAAa;EAAW,CAAC"}
1
+ {"version":3,"file":"use-agent-context.mjs","names":[],"sources":["../../src/hooks/use-agent-context.tsx"],"sourcesContent":["import { useCopilotKit } from \"../providers/CopilotKitProvider\";\nimport { useLayoutEffect, useMemo } from \"react\";\n\n/**\n * Represents any value that can be serialized to JSON.\n */\nexport type JsonSerializable =\n | string\n | number\n | boolean\n | null\n | JsonSerializable[]\n | { [key: string]: JsonSerializable };\n\n/**\n * Context configuration for useAgentContext.\n * Accepts any JSON-serializable value which will be converted to a string.\n */\nexport interface AgentContextInput {\n /** A human-readable description of what this context represents */\n description: string;\n /** The context value - will be converted to a JSON string if not already a string */\n value: JsonSerializable;\n}\n\nexport function useAgentContext(context: AgentContextInput) {\n const { description, value } = context;\n const { copilotkit } = useCopilotKit();\n\n const stringValue = useMemo(() => {\n if (typeof value === \"string\") {\n return value;\n }\n return JSON.stringify(value);\n }, [value]);\n\n useLayoutEffect(() => {\n if (!copilotkit) return;\n\n const id = copilotkit.addContext({ description, value: stringValue });\n return () => {\n copilotkit.removeContext(id);\n };\n }, [description, stringValue, copilotkit]);\n}\n"],"mappings":";;;;AAyBA,SAAgB,gBAAgB,SAA4B;CAC1D,MAAM,EAAE,aAAa,UAAU;CAC/B,MAAM,EAAE,eAAe,eAAe;CAEtC,MAAM,cAAc,cAAc;AAChC,MAAI,OAAO,UAAU,SACnB,QAAO;AAET,SAAO,KAAK,UAAU,MAAM;IAC3B,CAAC,MAAM,CAAC;AAEX,uBAAsB;AACpB,MAAI,CAAC,WAAY;EAEjB,MAAM,KAAK,WAAW,WAAW;GAAE;GAAa,OAAO;GAAa,CAAC;AACrE,eAAa;AACX,cAAW,cAAc,GAAG;;IAE7B;EAAC;EAAa;EAAa;EAAW,CAAC"}
package/dist/index.umd.js CHANGED
@@ -2021,6 +2021,22 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
2021
2021
  subscribe(subscriber) {
2022
2022
  return super.subscribe(subscriber);
2023
2023
  }
2024
+ /**
2025
+ * Wait for pending React state updates before the follow-up agent run.
2026
+ *
2027
+ * When a frontend tool handler calls setState(), React 18 batches the update
2028
+ * and schedules a commit via its internal scheduler (MessageChannel). The
2029
+ * useAgentContext hook registers context via useLayoutEffect, which runs
2030
+ * synchronously after React commits that batch.
2031
+ *
2032
+ * Awaiting a zero-delay timeout yields to the macrotask queue. React's
2033
+ * MessageChannel task runs first, committing the pending state and running
2034
+ * useLayoutEffect (which updates the context store). The follow-up runAgent
2035
+ * call then reads fresh context.
2036
+ */
2037
+ async waitForPendingFrameworkUpdates() {
2038
+ await new Promise((resolve) => setTimeout(resolve, 0));
2039
+ }
2024
2040
  };
2025
2041
 
2026
2042
  //#endregion
@@ -2984,7 +3000,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
2984
3000
  if (typeof value === "string") return value;
2985
3001
  return JSON.stringify(value);
2986
3002
  }, [value]);
2987
- (0, react.useEffect)(() => {
3003
+ (0, react.useLayoutEffect)(() => {
2988
3004
  if (!copilotkit) return;
2989
3005
  const id = copilotkit.addContext({
2990
3006
  description,