pyb-ts
Version:
PYB-CLI - Minimal AI Agent with multi-model support and CLI interface
88 lines (87 loc) • 2.82 kB
JavaScript
import { useCallback } from "react";
import { hasPermissionsToUseTool } from "@permissions";
import { BashTool, inputSchema } from "@tools/BashTool/BashTool";
import { getCommandSubcommandPrefix } from "@utils/commands";
import { REJECT_MESSAGE } from "@utils/messages";
import { AbortError } from "@utils/errors";
import { logError } from "@utils/log";
function useCanUseTool(setToolUseConfirm) {
return useCallback(
async (tool, input, toolUseContext, assistantMessage) => {
return new Promise((resolve) => {
function logCancelledEvent() {
}
function resolveWithCancelledAndAbortAllToolCalls() {
resolve({
result: false,
message: REJECT_MESSAGE
});
toolUseContext.abortController.abort();
}
if (toolUseContext.abortController.signal.aborted) {
logCancelledEvent();
resolveWithCancelledAndAbortAllToolCalls();
return;
}
return hasPermissionsToUseTool(
tool,
input,
toolUseContext,
assistantMessage
).then(async (result) => {
if (result.result) {
resolve({ result: true });
return;
}
const [description, commandPrefix] = await Promise.all([
tool.description(input),
tool === BashTool ? getCommandSubcommandPrefix(
inputSchema.parse(input).command,
// already validated upstream, so ok to parse (as opposed to safeParse)
toolUseContext.abortController.signal
) : Promise.resolve(null)
]);
if (toolUseContext.abortController.signal.aborted) {
logCancelledEvent();
resolveWithCancelledAndAbortAllToolCalls();
return;
}
setToolUseConfirm({
assistantMessage,
tool,
description,
input,
commandPrefix,
riskScore: null,
onAbort() {
logCancelledEvent();
resolveWithCancelledAndAbortAllToolCalls();
},
onAllow(type) {
if (type === "permanent") {
} else {
}
resolve({ result: true });
},
onReject() {
resolveWithCancelledAndAbortAllToolCalls();
}
});
}).catch((error) => {
if (error instanceof AbortError) {
logCancelledEvent();
resolveWithCancelledAndAbortAllToolCalls();
} else {
logError(error);
}
});
});
},
[setToolUseConfirm]
);
}
var useCanUseTool_default = useCanUseTool;
export {
useCanUseTool_default as default
};
//# sourceMappingURL=useCanUseTool.js.map