@copilotkit/react-core
Version:
<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />
83 lines (81 loc) • 2.56 kB
JavaScript
import {
__objRest
} from "./chunk-SKC7AJIV.mjs";
// src/hooks/use-human-in-the-loop.ts
import {
CopilotKitError,
CopilotKitErrorCode,
getZodParameters,
parseJson
} from "@copilotkit/shared";
import { useHumanInTheLoop as useHumanInTheLoopVNext } from "@copilotkitnext/react";
import { ToolCallStatus } from "@copilotkitnext/core";
import React, { useEffect, useRef } from "react";
function useHumanInTheLoop(tool, dependencies) {
const _a = tool, { render } = _a, toolRest = __objRest(_a, ["render"]);
const { name, description, parameters, followUp } = toolRest;
const zodParameters = getZodParameters(parameters);
const renderRef = useRef(null);
useEffect(() => {
renderRef.current = (args) => {
if (typeof render === "string") {
return React.createElement(React.Fragment, null, render);
}
if (!render) {
return null;
}
const renderProps = (() => {
const mappedArgs = args.args;
switch (args.status) {
case ToolCallStatus.InProgress:
return {
args: mappedArgs,
respond: args.respond,
status: args.status,
handler: void 0
};
case ToolCallStatus.Executing:
return {
args: mappedArgs,
respond: args.respond,
status: args.status,
handler: () => {
}
};
case ToolCallStatus.Complete:
return {
args: mappedArgs,
respond: args.respond,
status: args.status,
result: args.result ? parseJson(args.result, args.result) : args.result,
handler: void 0
};
default:
throw new CopilotKitError({
code: CopilotKitErrorCode.UNKNOWN,
message: `Invalid tool call status: ${args.status}`
});
}
})();
const rendered = render(renderProps);
if (typeof rendered === "string") {
return React.createElement(React.Fragment, null, rendered);
}
return rendered != null ? rendered : null;
};
}, [render, ...dependencies != null ? dependencies : []]);
useHumanInTheLoopVNext({
name,
description,
followUp,
parameters: zodParameters,
render: (args) => {
var _a2, _b;
return (_b = (_a2 = renderRef.current) == null ? void 0 : _a2.call(renderRef, args)) != null ? _b : null;
}
});
}
export {
useHumanInTheLoop
};
//# sourceMappingURL=chunk-7DTB7S5V.mjs.map