Repository navigation
Cpk 2442 hitl option 2 add new items - #1945
Conversation
…Implemented a new input field in both demo viewers to allow users to add steps dynamically during the execution phase. Added functionality to handle step addition via keyboard input, enhancing user interaction.
…oop demo viewers. Updated the input field styles in both demo viewers to enhance visual consistency.
… functionality to dynamically add steps via an input field and handle keyboard events for better user interaction during the execution phase.
…create_agents function and directly initialized the CopilotKitSDK with the LangGraph agent, simplifying the code structure.
…graded @CopilotKit packages to version 1.8.14 in demo viewer and updated Python SDK version to 0.1.48 in agent configuration files.
⏭️ Changeset Not RequiredLatest commit: 486fb7c No changes in this PR affected the Changeset is not required for this PR. |
WalkthroughThe changes introduce dynamic step addition in several human-in-the-loop React components by adding state and handlers for a new step input field. Related backend Python agent logic is updated for improved step handling. Dependency versions are incremented in configuration files, and prompt instructions are updated to reference user-added steps in summaries. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant ReactComponent
participant BackendAgent
User->>ReactComponent: Types new step in input
ReactComponent->>ReactComponent: handleAddStep (on Enter key)
ReactComponent->>ReactComponent: Add step to local steps list
User->>ReactComponent: Clicks "Perform Steps"
ReactComponent->>BackendAgent: Send enabled steps (including user-added)
BackendAgent->>BackendAgent: Process steps and respond
BackendAgent->>ReactComponent: Return summary including user-added steps
ReactComponent->>User: Display summary
Possibly related PRs
Suggested labels
Suggested reviewers
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Skipped Deployment
|
|
Preview environments destroyed for this pull request. |
There was a problem hiding this comment.
Actionable comments posted: 3
🔭 Outside diff range comments (2)
examples/demo-viewer/agent/demo/langgraph_server.py (1)
27-70: 🛠️ Refactor suggestionAddress the deprecated CopilotKitSDK usage.
While the inline agent instantiation is cleaner, the code is using
CopilotKitSDKwhich is deprecated since version 0.1.31. This will generate deprecation warnings in the logs.Update to use the recommended
CopilotKitRemoteEndpoint:-from copilotkit import CopilotKitSDK, LangGraphAgent +from copilotkit import CopilotKitRemoteEndpoint, LangGraphAgent -sdk = CopilotKitSDK( +sdk = CopilotKitRemoteEndpoint( agents=[ # Register the LangGraph agent using the LangGraphAgent class LangGraphAgent(examples/demo-viewer/src/files.json (1)
542-547:⚠️ Potential issueRemove leftover
debuggerstatement. There's a straydebuggerinstandard_human_in_the_loop/page.tsxwhich will halt execution in browsers. Please delete it:- debugger
♻️ Duplicate comments (1)
examples/demo-viewer/agent/demo/standard_human_in_the_loop/page.tsx (1)
46-54: Apply the same input validation as suggested for langgraph demo.This function has the same XSS vulnerability as the langgraph implementation. The logic is identical and needs the same security improvements.
Refer to the security fix suggested for the langgraph demo's
handleAddStepfunction.
🧹 Nitpick comments (5)
examples/demo-viewer/agent/demo/langgraph_human_in_the_loop/page.tsx (1)
93-103: Consider adding visual feedback and accessibility attributes.The input field implementation is functional but could benefit from better UX and accessibility improvements.
Apply this diff to improve the input field:
<div className="flex items-center gap-2 mb-4"> <input type="text" className="flex-1 rounded py-2 focus:outline-none" placeholder="Add a new step..." value={newStep} onChange={(e) => setNewStep(e.target.value)} onKeyDown={handleInputKeyDown} - // hidden={status != "executing"} + aria-label="Add new step" + maxLength={100} /> + <button + onClick={handleAddStep} + className="px-3 py-2 bg-blue-500 text-white rounded hover:bg-blue-600" + disabled={!newStep.trim()} + aria-label="Add step" + > + Add + </button> </div>examples/demo-viewer/agent/demo/standard_human_in_the_loop/page.tsx (1)
104-114: Good conditional rendering but consider same UX improvements.The conditional hiding based on status is a good UX decision that's missing from the langgraph demo. However, the input field could still benefit from the same accessibility and validation improvements.
Apply similar improvements as suggested for the langgraph demo, but keep the conditional rendering:
<div className="flex items-center gap-2 mb-4"> <input type="text" className="flex-1 rounded py-2 focus:outline-none" placeholder="Add a new step..." value={newStep} onChange={(e) => setNewStep(e.target.value)} onKeyDown={handleInputKeyDown} hidden={status != "executing"} + aria-label="Add new step" + maxLength={100} /> + {status === "executing" && ( + <button + onClick={handleAddStep} + className="px-3 py-2 bg-blue-500 text-white rounded hover:bg-blue-600" + disabled={!newStep.trim()} + aria-label="Add step" + > + Add + </button> + )} </div>examples/demo-viewer/src/files.json (3)
74-80: Reduce unnecessary useEffect dependencies. The effect inStepsFeedbackis re-run any timelocalStepschanges, although it should only initialize state once when execution starts. RemovelocalStepsfrom the dependency array to avoid redundant calls:-}, [status, args.steps, localSteps]); +}, [status, args.steps]);
484-489: Avoid static delays withasyncio.sleep. Fixed sleep calls can slow the flow and lead to poor UX. Replace or parameterize these with event-driven signals or use CopilotKit streaming hooks (e.g.,copilotkit_emit_state) instead of hardcoded sleeps.
542-547: Correct the console.log message. The log prints"args.steps"but the parameter is nowargs.items. Update the label for clarity:- console.log(args, "args.steps"); + console.log(args, "args.items");
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (2)
examples/demo-viewer/agent/poetry.lockis excluded by!**/*.lockexamples/demo-viewer/pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (8)
examples/demo-viewer/agent/demo/crewai_human_in_the_loop/page.tsx(3 hunks)examples/demo-viewer/agent/demo/langgraph_human_in_the_loop/page.tsx(2 hunks)examples/demo-viewer/agent/demo/langgraph_server.py(1 hunks)examples/demo-viewer/agent/demo/standard_human_in_the_loop/page.tsx(2 hunks)examples/demo-viewer/agent/pyproject.toml(1 hunks)examples/demo-viewer/package.json(1 hunks)examples/demo-viewer/src/files.json(4 hunks)examples/demo-viewer/src/lib/prompts.ts(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
examples/demo-viewer/agent/demo/langgraph_server.py (1)
sdk-python/copilotkit/sdk.py (1)
CopilotKitSDK(391-401)
⏰ Context from checks skipped due to timeout of 90000ms (9)
- GitHub Check: Build Images (next-openai, next-openai)
- GitHub Check: Build Images (coagents-qa-native, ui)
- GitHub Check: Build Images (coagents-qa-native, agent, python, local-deps)
- GitHub Check: Build Images (coagents-qa-text, ui)
- GitHub Check: Build Images (coagents-qa-text, agent, python, local-deps)
- GitHub Check: Build Images (coagents-routing, ui)
- GitHub Check: Build Images (coagents-routing, agent, python, local-deps)
- GitHub Check: Build Images (coagents-research-canvas, ui)
- GitHub Check: Build Images (coagents-research-canvas, agent, python, local-deps)
🔇 Additional comments (11)
examples/demo-viewer/agent/pyproject.toml (1)
10-10: LGTM: Standard dependency version update.The copilotkit version bump from 0.1.46 to 0.1.48 aligns with the frontend dependency updates and supports the new human-in-the-loop features.
examples/demo-viewer/package.json (1)
13-16: LGTM: Dependency versions updated with exact pinning.The @CopilotKit packages are updated to version 1.8.14 with exact version pinning (removing the
^prefix). This ensures reproducible builds for the demo, which is appropriate for this use case.examples/demo-viewer/src/lib/prompts.ts (1)
50-50: LGTM: Prompt instruction updated to support dynamic step addition.The addition of "While summarizing, include the steps that user added as well." properly aligns the prompt with the new UI functionality that allows users to add steps dynamically during execution.
examples/demo-viewer/agent/demo/crewai_human_in_the_loop/page.tsx (4)
71-71: LGTM: State variable for new step input.The
newStepstate is properly initialized to track the input value for adding new steps.
98-106: LGTM: Well-implemented step addition handler.The
handleAddStepfunction includes proper input validation (trimming and empty check), adds new steps with appropriate default status, and clears the input field. The implementation follows React best practices.
108-112: LGTM: Keyboard interaction support.The Enter key handling provides good user experience for adding steps without requiring mouse interaction.
143-153: LGTM: Well-designed input field for step addition.The input field implementation is solid:
- Controlled component with proper state binding
- Hidden when not in executing status (appropriate UX)
- Supports both Enter key and blur interactions
- Clear placeholder text
The conditional visibility (
hidden={status != "executing"}) ensures the input only appears when users can actually add steps.examples/demo-viewer/agent/demo/langgraph_human_in_the_loop/page.tsx (2)
24-24: LGTM: State variable declaration is correct.The
newStepstate variable is properly initialized as an empty string for tracking the input field value.
36-40: LGTM: Keyboard event handler is implemented correctly.The
handleInputKeyDownfunction properly handles the Enter key press to trigger step addition, providing good UX.examples/demo-viewer/agent/demo/standard_human_in_the_loop/page.tsx (2)
44-44: LGTM: State variable declaration is consistent.The
newStepstate variable follows the same pattern as the langgraph implementation, maintaining consistency across demos.
56-60: LGTM: Keyboard handler is implemented consistently.The implementation matches the langgraph demo and provides good user experience.
| const handleAddStep = () => { | ||
| const trimmed = newStep.trim(); | ||
| if (trimmed.length === 0) return; | ||
| setLocalSteps((prevSteps) => [ | ||
| ...prevSteps, | ||
| { description: trimmed, status: "enabled" }, | ||
| ]); | ||
| setNewStep(""); | ||
| }; |
There was a problem hiding this comment.
Add input validation to prevent XSS attacks.
The handleAddStep function lacks proper input sanitization. While basic trimming is performed, there's no validation against potentially malicious content that could lead to XSS vulnerabilities when the step descriptions are rendered.
Consider adding input validation:
const handleAddStep = () => {
const trimmed = newStep.trim();
if (trimmed.length === 0) return;
+ // Add basic validation to prevent XSS
+ if (trimmed.length > 100) {
+ console.warn("Step description too long");
+ return;
+ }
+ if (/<script|javascript:|on\w+=/i.test(trimmed)) {
+ console.warn("Invalid characters in step description");
+ return;
+ }
setLocalSteps((prevSteps) => [
...prevSteps,
{ description: trimmed, status: "enabled" },
]);
setNewStep("");
};📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const handleAddStep = () => { | |
| const trimmed = newStep.trim(); | |
| if (trimmed.length === 0) return; | |
| setLocalSteps((prevSteps) => [ | |
| ...prevSteps, | |
| { description: trimmed, status: "enabled" }, | |
| ]); | |
| setNewStep(""); | |
| }; | |
| const handleAddStep = () => { | |
| const trimmed = newStep.trim(); | |
| if (trimmed.length === 0) return; | |
| // Add basic validation to prevent XSS | |
| if (trimmed.length > 100) { | |
| console.warn("Step description too long"); | |
| return; | |
| } | |
| if (/<script|javascript:|on\w+=/i.test(trimmed)) { | |
| console.warn("Invalid characters in step description"); | |
| return; | |
| } | |
| setLocalSteps((prevSteps) => [ | |
| ...prevSteps, | |
| { description: trimmed, status: "enabled" }, | |
| ]); | |
| setNewStep(""); | |
| }; |
🤖 Prompt for AI Agents
In examples/demo-viewer/agent/demo/langgraph_human_in_the_loop/page.tsx around
lines 26 to 34, the handleAddStep function currently only trims input but does
not sanitize it, which can lead to XSS vulnerabilities when rendering step
descriptions. To fix this, implement input validation that sanitizes or escapes
any potentially malicious content in newStep before adding it to localSteps. Use
a library or custom function to remove or encode harmful characters or scripts
to ensure safe rendering.
| }, | ||
| { | ||
| "name": "page.tsx", | ||
| "content": "\"use client\";\r\nimport React, { useState, useEffect } from \"react\";\r\nimport \"@copilotkit/react-ui/styles.css\";\r\nimport \"./style.css\";\r\nimport { CopilotKit, useCopilotAction } from \"@copilotkit/react-core\";\r\nimport { CopilotChat, useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\r\nimport { chatSuggestions, initialPrompt } from \"@/lib/prompts\";\r\nimport { AGENT_TYPE } from \"@/config\";\r\nconst HumanInTheLoop: React.FC = () => {\r\n return (\r\n <CopilotKit\r\n runtimeUrl={AGENT_TYPE == \"general\" ? \"/api/copilotkit?crewai=true\" : \"/api/copilotkit\"}\r\n showDevConsole={false}\r\n agent=\"human_in_the_loop\"\r\n >\r\n <Chat />\r\n </CopilotKit>\r\n );\r\n};\r\n\r\n\r\nconst Chat = () => {\r\n useCopilotAction({\r\n name: \"generate_task_steps\",\r\n parameters: [\r\n {\r\n name: \"steps\",\r\n type: \"object[]\",\r\n attributes: [\r\n {\r\n name: \"description\",\r\n type: \"string\",\r\n },\r\n {\r\n name: \"status\",\r\n type: \"string\",\r\n enum: [\"enabled\", \"disabled\", \"executing\"],\r\n },\r\n ],\r\n },\r\n ],\r\n renderAndWaitForResponse: ({ args, respond, status }) => {\r\n return <StepsFeedback args={args} respond={respond} status={status} />;\r\n },\r\n });\r\n\r\n\r\n useCopilotChatSuggestions({\r\n instructions: chatSuggestions.humanInTheLoop,\r\n })\r\n\r\n return (\r\n <div className=\"flex justify-center items-center h-full w-full\">\r\n <div className=\"w-8/10 h-8/10 rounded-lg\">\r\n <CopilotChat\r\n className=\"h-full rounded-2xl\"\r\n labels={{ initial: initialPrompt.humanInTheLoop }}\r\n />\r\n </div>\r\n </div>\r\n );\r\n};\r\n\r\nconst StepsFeedback = ({ args, respond, status }: { args: any, respond: any, status: any }) => {\r\n const [localSteps, setLocalSteps] = useState<\r\n {\r\n description: string;\r\n status: \"disabled\" | \"enabled\" | \"executing\";\r\n }[]\r\n >([]);\r\n\r\n useEffect(() => {\r\n if (status === \"executing\" && localSteps.length === 0) {\r\n setLocalSteps(args.steps);\r\n }\r\n }, [status, args.steps, localSteps]);\r\n\r\n if (args.steps === undefined || args.steps.length === 0) {\r\n return <></>;\r\n }\r\n\r\n const steps = localSteps.length > 0 ? localSteps : args.steps;\r\n\r\n const handleCheckboxChange = (index: number) => {\r\n setLocalSteps((prevSteps) =>\r\n prevSteps.map((step, i) =>\r\n i === index\r\n ? {\r\n ...step,\r\n status: step.status === \"enabled\" ? \"disabled\" : \"enabled\",\r\n }\r\n : step\r\n )\r\n );\r\n };\r\n\r\n return (\r\n <div className=\"flex flex-col gap-4 w-[500px] bg-gray-100 rounded-lg p-8 mb-4\">\r\n <div className=\" text-black space-y-2\">\r\n <h2 className=\"text-lg font-bold mb-4\">Select Steps</h2>\r\n {steps.map((step: any, index: any) => (\r\n <div key={index} className=\"text-sm flex items-center\">\r\n <label className=\"flex items-center cursor-pointer\">\r\n <input\r\n type=\"checkbox\"\r\n checked={step.status === \"enabled\"}\r\n onChange={() => {\r\n if (respond) {\r\n handleCheckboxChange(index)\r\n }\r\n }}\r\n className=\"mr-2\"\r\n />\r\n <span\r\n className={\r\n step.status !== \"enabled\" && status != \"inProgress\"\r\n ? \"line-through\"\r\n : \"\"\r\n }\r\n >\r\n {step.description}\r\n </span>\r\n </label>\r\n </div>\r\n ))}\r\n {status === \"executing\" && (\r\n <button\r\n className=\"mt-4 bg-gradient-to-r from-purple-400 to-purple-600 text-white py-2 px-4 rounded cursor-pointer w-48 font-bold\"\r\n onClick={() => {\r\n const selectedSteps = localSteps\r\n .filter((step) => step.status === \"enabled\")\r\n .map((step) => step.description);\r\n respond(\r\n \"The user selected the following steps: \" +\r\n selectedSteps.join(\", \")\r\n );\r\n }}\r\n >\r\n ✨ Perform Steps\r\n </button>\r\n )}\r\n </div>\r\n </div>\r\n );\r\n};\r\n\r\nfunction Spinner() {\r\n return (\r\n <svg\r\n className=\"mr-2 size-3 animate-spin text-slate-500\"\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n fill=\"none\"\r\n viewBox=\"0 0 24 24\"\r\n >\r\n <circle\r\n className=\"opacity-25\"\r\n cx=\"12\"\r\n cy=\"12\"\r\n r=\"10\"\r\n stroke=\"currentColor\"\r\n strokeWidth=\"4\"\r\n ></circle>\r\n <path\r\n className=\"opacity-75\"\r\n fill=\"currentColor\"\r\n d=\"M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z\"\r\n ></path>\r\n </svg>\r\n );\r\n}\r\nexport default HumanInTheLoop;\r\n\r\n", | ||
| "content": "\"use client\";\r\nimport React, { useState, useEffect } from \"react\";\r\nimport \"@copilotkit/react-ui/styles.css\";\r\nimport \"./style.css\";\r\nimport { CopilotKit, useCopilotAction } from \"@copilotkit/react-core\";\r\nimport { CopilotChat, useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\r\nimport { chatSuggestions, initialPrompt } from \"@/lib/prompts\";\r\nimport { AGENT_TYPE } from \"@/config\";\r\nconst HumanInTheLoop: React.FC = () => {\r\n return (\r\n <CopilotKit\r\n runtimeUrl={AGENT_TYPE == \"general\" ? \"/api/copilotkit?crewai=true\" : \"/api/copilotkit\"}\r\n showDevConsole={false}\r\n agent=\"human_in_the_loop\"\r\n >\r\n <Chat />\r\n </CopilotKit>\r\n );\r\n};\r\n\r\n\r\nconst Chat = () => {\r\n useCopilotAction({\r\n name: \"generate_task_steps\",\r\n parameters: [\r\n {\r\n name: \"steps\",\r\n type: \"object[]\",\r\n attributes: [\r\n {\r\n name: \"description\",\r\n type: \"string\",\r\n },\r\n {\r\n name: \"status\",\r\n type: \"string\",\r\n enum: [\"enabled\", \"disabled\", \"executing\"],\r\n },\r\n ],\r\n },\r\n ],\r\n renderAndWaitForResponse: ({ args, respond, status }) => {\r\n return <StepsFeedback args={args} respond={respond} status={status} />;\r\n },\r\n });\r\n\r\n\r\n useCopilotChatSuggestions({\r\n instructions: chatSuggestions.humanInTheLoop,\r\n })\r\n\r\n return (\r\n <div className=\"flex justify-center items-center h-full w-full\">\r\n <div className=\"w-8/10 h-8/10 rounded-lg\">\r\n <CopilotChat\r\n className=\"h-full rounded-2xl\"\r\n labels={{ initial: initialPrompt.humanInTheLoop }}\r\n />\r\n </div>\r\n </div>\r\n );\r\n};\r\n\r\nconst StepsFeedback = ({ args, respond, status }: { args: any, respond: any, status: any }) => {\r\n const [localSteps, setLocalSteps] = useState<\r\n {\r\n description: string;\r\n status: \"disabled\" | \"enabled\" | \"executing\";\r\n }[]\r\n >([]);\r\n const [newStep, setNewStep] = useState(\"\");\r\n\r\n useEffect(() => {\r\n if (status === \"executing\" && localSteps.length === 0) {\r\n setLocalSteps(args.steps);\r\n }\r\n }, [status, args.steps, localSteps]);\r\n\r\n if (args.steps === undefined || args.steps.length === 0) {\r\n return <></>;\r\n }\r\n\r\n const steps = localSteps.length > 0 ? localSteps : args.steps;\r\n\r\n const handleCheckboxChange = (index: number) => {\r\n setLocalSteps((prevSteps) =>\r\n prevSteps.map((step, i) =>\r\n i === index\r\n ? {\r\n ...step,\r\n status: step.status === \"enabled\" ? \"disabled\" : \"enabled\",\r\n }\r\n : step\r\n )\r\n );\r\n };\r\n\r\n const handleAddStep = () => {\r\n const trimmed = newStep.trim();\r\n if (trimmed.length === 0) return;\r\n setLocalSteps((prevSteps) => [\r\n ...prevSteps,\r\n { description: trimmed, status: \"enabled\" },\r\n ]);\r\n setNewStep(\"\");\r\n };\r\n\r\n const handleInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {\r\n if (e.key === \"Enter\") {\r\n handleAddStep();\r\n }\r\n };\r\n\r\n return (\r\n <div className=\"flex flex-col gap-4 w-[500px] bg-gray-100 rounded-lg p-8 mb-4\">\r\n <div className=\" text-black space-y-2\">\r\n <h2 className=\"text-lg font-bold mb-4\">Select Steps</h2>\r\n {steps.map((step: any, index: any) => (\r\n <div key={index} className=\"text-sm flex items-center\">\r\n <label className=\"flex items-center cursor-pointer\">\r\n <input\r\n type=\"checkbox\"\r\n checked={step.status === \"enabled\"}\r\n onChange={() => {\r\n if (respond) {\r\n handleCheckboxChange(index)\r\n }\r\n }}\r\n className=\"mr-2\"\r\n />\r\n <span\r\n className={\r\n step.status !== \"enabled\" && status != \"inProgress\"\r\n ? \"line-through\"\r\n : \"\"\r\n }\r\n >\r\n {step.description}\r\n </span>\r\n </label>\r\n </div>\r\n ))}\r\n <div className=\"flex items-center gap-2 mb-4\">\r\n <input\r\n type=\"text\"\r\n className=\"flex-1 rounded py-2 focus:outline-none\"\r\n placeholder=\"Add a new step...\"\r\n value={newStep}\r\n onChange={(e) => setNewStep(e.target.value)}\r\n onKeyDown={handleInputKeyDown}\r\n hidden={status != \"executing\"}\r\n />\r\n </div>\r\n {status === \"executing\" && (\r\n <button\r\n className=\"mt-4 bg-gradient-to-r from-purple-400 to-purple-600 text-white py-2 px-4 rounded cursor-pointer w-48 font-bold\"\r\n onClick={() => {\r\n const selectedSteps = localSteps\r\n .filter((step) => step.status === \"enabled\")\r\n .map((step) => step.description);\r\n respond(\r\n \"The user selected the following steps: \" +\r\n selectedSteps.join(\", \")\r\n );\r\n }}\r\n >\r\n ✨ Perform Steps\r\n </button>\r\n )}\r\n </div>\r\n </div>\r\n );\r\n};\r\n\r\nfunction Spinner() {\r\n return (\r\n <svg\r\n className=\"mr-2 size-3 animate-spin text-slate-500\"\r\n xmlns=\"http://www.w3.org/2000/svg\"\r\n fill=\"none\"\r\n viewBox=\"0 0 24 24\"\r\n >\r\n <circle\r\n className=\"opacity-25\"\r\n cx=\"12\"\r\n cy=\"12\"\r\n r=\"10\"\r\n stroke=\"currentColor\"\r\n strokeWidth=\"4\"\r\n ></circle>\r\n <path\r\n className=\"opacity-75\"\r\n fill=\"currentColor\"\r\n d=\"M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z\"\r\n ></path>\r\n </svg>\r\n );\r\n}\r\nexport default HumanInTheLoop;\r\n\r\n", | ||
| "path": "crewai_human_in_the_loop\\page.tsx", | ||
| "language": "typescript", | ||
| "type": "file" |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Propagate checkbox toggles back to the agent. Currently handleCheckboxChange updates only local state. To inform the backend of interim step changes, call respond with the updated step list inside the onChange handler:
- onChange={() => { handleCheckboxChange(index); }}
+ onChange={() => {
+ const updated = prevStepsAfterToggle(index);
+ setLocalSteps(updated);
+ respond(updated.map(s => s.description));
+ }}Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In examples/demo-viewer/src/files.json around lines 74 to 80 in
crewai_human_in_the_loop\page.tsx, the handleCheckboxChange function updates
only the localSteps state but does not notify the backend of these changes. To
fix this, modify the onChange handler of the checkbox input to call respond with
the updated steps after toggling the status, ensuring the agent receives the
current step list. This involves updating handleCheckboxChange to accept respond
as a parameter or calling respond directly after updating localSteps with the
new step statuses.
| "name": "page.tsx", | ||
| "content": "\"use client\";\r\nimport React, { useState, useEffect } from \"react\";\r\nimport \"@copilotkit/react-ui/styles.css\";\r\nimport \"./style.css\";\r\nimport { CopilotKit, useLangGraphInterrupt } from \"@copilotkit/react-core\";\r\nimport { CopilotChat, useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\r\nimport { initialPrompt, chatSuggestions } from \"@/lib/prompts\";\r\nimport { AGENT_TYPE } from \"@/config\";\r\nconst HumanInTheLoop: React.FC = () => {\r\n return (\r\n <CopilotKit\r\n runtimeUrl={AGENT_TYPE == \"general\" ? \"/api/copilotkit?langgraph=true\" : \"/api/copilotkit\"}\r\n showDevConsole={false}\r\n agent=\"human_in_the_loop\"\r\n >\r\n <Chat />\r\n </CopilotKit>\r\n );\r\n};\r\n\r\nconst Chat = () => {\r\n useLangGraphInterrupt({\r\n render: ({ event, resolve }) => {\r\n\r\n // Ensure we have valid steps data\r\n let initialSteps = [];\r\n if (event.value && event.value.steps && Array.isArray(event.value.steps)) {\r\n initialSteps = event.value.steps.map((step: any) => ({\r\n description: typeof step === 'string' ? step : step.description || '',\r\n status: (typeof step === 'object' && step.status) ? step.status : 'enabled'\r\n }));\r\n }\r\n\r\n const [localSteps, setLocalSteps] = useState<\r\n {\r\n description: string;\r\n status: \"disabled\" | \"enabled\" | \"executing\";\r\n }[]\r\n >(initialSteps);\r\n\r\n const handleCheckboxChange = (index: number) => {\r\n setLocalSteps((prevSteps) =>\r\n prevSteps.map((step, i) =>\r\n i === index\r\n ? {\r\n ...step,\r\n status: step.status === \"enabled\" ? \"disabled\" : \"enabled\",\r\n }\r\n : step\r\n )\r\n );\r\n };\r\n\r\n return (\r\n <div className=\"flex flex-col gap-4 w-[500px] bg-gray-100 rounded-lg p-8 mb-4\">\r\n <div className=\"text-black space-y-2\">\r\n <h2 className=\"text-lg font-bold mb-4\">Select Steps</h2>\r\n {localSteps.map((step, index) => (\r\n <div key={index} className=\"text-sm flex items-center\">\r\n <label className=\"flex items-center cursor-pointer\">\r\n <input\r\n type=\"checkbox\"\r\n checked={step.status === \"enabled\"}\r\n onChange={() => handleCheckboxChange(index)}\r\n className=\"mr-2\"\r\n />\r\n <span\r\n className={\r\n step.status !== \"enabled\" ? \"line-through\" : \"\"\r\n }\r\n >\r\n {step.description}\r\n </span>\r\n </label>\r\n </div>\r\n ))}\r\n <button\r\n className=\"mt-4 bg-gradient-to-r from-purple-400 to-purple-600 text-white py-2 px-4 rounded cursor-pointer w-48 font-bold\"\r\n onClick={() => {\r\n const selectedSteps = localSteps\r\n .filter((step) => step.status === \"enabled\")\r\n .map((step) => step.description);\r\n resolve(\"The user selected the following steps: \" + selectedSteps.join(\", \"));\r\n }}\r\n >\r\n ✨ Perform Steps\r\n </button>\r\n </div>\r\n </div>\r\n );\r\n },\r\n });\r\n useCopilotChatSuggestions({\r\n instructions: chatSuggestions.humanInTheLoop,\r\n })\r\n return (\r\n <div className=\"flex justify-center items-center h-screen w-screen\">\r\n <div className=\"w-8/10 h-8/10\">\r\n <CopilotChat\r\n className=\"h-full rounded-lg\"\r\n labels={{ initial: initialPrompt.humanInTheLoop }}\r\n />\r\n </div>\r\n </div>\r\n );\r\n};\r\n\r\nexport default HumanInTheLoop;\r\n", | ||
| "content": "\"use client\";\r\nimport React, { useState, useEffect } from \"react\";\r\nimport \"@copilotkit/react-ui/styles.css\";\r\nimport \"./style.css\";\r\nimport { CopilotKit, useLangGraphInterrupt } from \"@copilotkit/react-core\";\r\nimport { CopilotChat, useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\r\nimport { initialPrompt, chatSuggestions } from \"@/lib/prompts\";\r\nimport { AGENT_TYPE } from \"@/config\";\r\nconst HumanInTheLoop: React.FC = () => {\r\n return (\r\n <CopilotKit\r\n runtimeUrl={AGENT_TYPE == \"general\" ? \"/api/copilotkit?langgraph=true\" : \"/api/copilotkit\"}\r\n showDevConsole={false}\r\n agent=\"human_in_the_loop\"\r\n >\r\n <Chat />\r\n </CopilotKit>\r\n );\r\n};\r\n\r\nconst Chat = () => {\r\n useLangGraphInterrupt({\r\n render: ({ event, resolve }) => {\r\n const [newStep, setNewStep] = useState(\"\");\r\n\r\n const handleAddStep = () => {\r\n const trimmed = newStep.trim();\r\n if (trimmed.length === 0) return;\r\n setLocalSteps((prevSteps) => [\r\n ...prevSteps,\r\n { description: trimmed, status: \"enabled\" },\r\n ]);\r\n setNewStep(\"\");\r\n };\r\n\r\n const handleInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {\r\n if (e.key === \"Enter\") {\r\n handleAddStep();\r\n }\r\n };\r\n // Ensure we have valid steps data\r\n let initialSteps = [];\r\n if (event.value && event.value.steps && Array.isArray(event.value.steps)) {\r\n initialSteps = event.value.steps.map((step: any) => ({\r\n description: typeof step === 'string' ? step : step.description || '',\r\n status: (typeof step === 'object' && step.status) ? step.status : 'enabled'\r\n }));\r\n }\r\n\r\n const [localSteps, setLocalSteps] = useState<\r\n {\r\n description: string;\r\n status: \"disabled\" | \"enabled\" | \"executing\";\r\n }[]\r\n >(initialSteps);\r\n\r\n const handleCheckboxChange = (index: number) => {\r\n setLocalSteps((prevSteps) =>\r\n prevSteps.map((step, i) =>\r\n i === index\r\n ? {\r\n ...step,\r\n status: step.status === \"enabled\" ? \"disabled\" : \"enabled\",\r\n }\r\n : step\r\n )\r\n );\r\n };\r\n\r\n return (\r\n <div className=\"flex flex-col gap-4 w-[500px] bg-gray-100 rounded-lg p-8 mb-4\">\r\n <div className=\"text-black space-y-2\">\r\n <h2 className=\"text-lg font-bold mb-4\">Select Steps</h2>\r\n {localSteps.map((step, index) => (\r\n <div key={index} className=\"text-sm flex items-center\">\r\n <label className=\"flex items-center cursor-pointer\">\r\n <input\r\n type=\"checkbox\"\r\n checked={step.status === \"enabled\"}\r\n onChange={() => handleCheckboxChange(index)}\r\n className=\"mr-2\"\r\n />\r\n <span\r\n className={\r\n step.status !== \"enabled\" ? \"line-through\" : \"\"\r\n }\r\n >\r\n {step.description}\r\n </span>\r\n </label>\r\n </div>\r\n ))}\r\n <div className=\"flex items-center gap-2 mb-4\">\r\n <input\r\n type=\"text\"\r\n className=\"flex-1 rounded py-2 focus:outline-none\"\r\n placeholder=\"Add a new step...\"\r\n value={newStep}\r\n onChange={(e) => setNewStep(e.target.value)}\r\n onKeyDown={handleInputKeyDown}\r\n // hidden={status != \"executing\"}\r\n />\r\n </div>\r\n <button\r\n className=\"mt-4 bg-gradient-to-r from-purple-400 to-purple-600 text-white py-2 px-4 rounded cursor-pointer w-48 font-bold\"\r\n onClick={() => {\r\n const selectedSteps = localSteps\r\n .filter((step) => step.status === \"enabled\")\r\n .map((step) => step.description);\r\n resolve(\"The user selected the following steps: \" + selectedSteps.join(\", \"));\r\n }}\r\n >\r\n ✨ Perform Steps\r\n </button>\r\n </div>\r\n </div>\r\n );\r\n },\r\n });\r\n useCopilotChatSuggestions({\r\n instructions: chatSuggestions.humanInTheLoop,\r\n })\r\n return (\r\n <div className=\"flex justify-center items-center h-screen w-screen\">\r\n <div className=\"w-8/10 h-8/10\">\r\n <CopilotChat\r\n className=\"h-full rounded-lg\"\r\n labels={{ initial: initialPrompt.humanInTheLoop }}\r\n />\r\n </div>\r\n </div>\r\n );\r\n};\r\n\r\nexport default HumanInTheLoop;\r\n", | ||
| "path": "langgraph_human_in_the_loop\\page.tsx", | ||
| "language": "typescript", | ||
| "type": "file" |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Reinitialize steps when new interrupt events arrive. The initial state is set once from event.value.steps but never updated on subsequent events. Add an effect to sync localSteps with event.value.steps:
const [localSteps, setLocalSteps] = useState(initialSteps);
+useEffect(() => {
+ if (event.value?.steps) {
+ setLocalSteps(event.value.steps.map(step => ({
+ description: typeof step === 'string' ? step : step.description || '',
+ status: step.status || 'enabled'
+ })));
+ }
+}, [event.value]);🤖 Prompt for AI Agents
In examples/demo-viewer/src/files.json around lines 324 to 328, the localSteps
state is initialized once from event.value.steps but does not update when new
interrupt events arrive. To fix this, add a useEffect hook that listens for
changes in event.value.steps and updates localSteps accordingly, ensuring the
steps reinitialize properly on each new event.
Test ResultsStatus: ✅ Passed (View Run) Commit: 692c5ef Duration: 445.3s Total Tests: 32 Pass Rate: 100.0% Failed Tests: 0 🔍 Detailed Resultscoagents-research-canvasCoAgents Research Canvas (UI) - Local Depenencies
coagents-qa-nativeCoAgents QA Native (UI) - Local
coagents-qa-textCoAgents QA Text (UI) - Local
coagents-routingCoAgents Routing (UI) - Local
next-openaiNext OpenAI - Self Hosted
|
What does this PR do?
(Describe the changes introduced in this PR)
Related PRs and Issues
Checklist
Summary by CodeRabbit
New Features
Improvements
Bug Fixes