pyb-ts
Version:
PYB-CLI - Minimal AI Agent with multi-model support and CLI interface
154 lines (153 loc) • 5.07 kB
JavaScript
import { Box, Text } from "ink";
import React, { useMemo } from "react";
import { Select } from "@components/CustomSelect/select";
import { basename, extname } from "path";
import { getTheme } from "@utils/theme";
import {
PermissionRequestTitle,
textColorForRiskScore
} from "@components/permissions/PermissionRequestTitle";
import { logUnaryEvent } from "@utils/unaryLogging";
import { env } from "@utils/env";
import { savePermission } from "@permissions";
import {
toolUseConfirmGetPrefix
} from "@components/permissions/PermissionRequest";
import { existsSync } from "fs";
import chalk from "chalk";
import {
usePermissionRequestLogging
} from "@hooks/usePermissionRequestLogging";
import { FileWriteToolDiff } from "./FileWriteToolDiff.js";
import { useTerminalSize } from "@hooks/useTerminalSize";
function FileWritePermissionRequest({
toolUseConfirm,
onDone,
verbose
}) {
const { file_path, content } = toolUseConfirm.input;
const fileExists = useMemo(() => existsSync(file_path), [file_path]);
const unaryEvent = useMemo(
() => ({
completion_type: "write_file_single",
language_name: extractLanguageName(file_path)
}),
[file_path]
);
const { columns } = useTerminalSize();
usePermissionRequestLogging(toolUseConfirm, unaryEvent);
return /* @__PURE__ */ React.createElement(
Box,
{
flexDirection: "column",
borderStyle: "round",
borderColor: textColorForRiskScore(toolUseConfirm.riskScore),
marginTop: 1,
paddingLeft: 1,
paddingRight: 1,
paddingBottom: 1
},
/* @__PURE__ */ React.createElement(
PermissionRequestTitle,
{
title: `${fileExists ? "Edit" : "Create"} file`,
riskScore: toolUseConfirm.riskScore
}
),
/* @__PURE__ */ React.createElement(Box, { flexDirection: "column" }, /* @__PURE__ */ React.createElement(
FileWriteToolDiff,
{
file_path,
content,
verbose,
width: columns - 12
}
)),
/* @__PURE__ */ React.createElement(Box, { flexDirection: "column" }, /* @__PURE__ */ React.createElement(Text, null, "Do you want to ", fileExists ? "make this edit to" : "create", " ", /* @__PURE__ */ React.createElement(Text, { bold: true }, basename(file_path)), "?"), /* @__PURE__ */ React.createElement(
Select,
{
options: [
{
label: "Yes",
value: "yes"
},
{
label: "Yes, and don't ask again this session",
value: "yes-dont-ask-again"
},
{
label: `No, and provide instructions (${chalk.bold.hex(getTheme().warning)("esc")})`,
value: "no"
}
],
onChange: (newValue) => {
switch (newValue) {
case "yes":
extractLanguageName(file_path).then((language) => {
logUnaryEvent({
completion_type: "write_file_single",
event: "accept",
metadata: {
language_name: language,
message_id: toolUseConfirm.assistantMessage.message.id,
platform: env.platform
}
});
});
toolUseConfirm.onAllow("temporary");
onDone();
break;
case "yes-dont-ask-again":
extractLanguageName(file_path).then((language) => {
logUnaryEvent({
completion_type: "write_file_single",
event: "accept",
metadata: {
language_name: language,
message_id: toolUseConfirm.assistantMessage.message.id,
platform: env.platform
}
});
});
savePermission(
toolUseConfirm.tool,
toolUseConfirm.input,
toolUseConfirmGetPrefix(toolUseConfirm)
).then(() => {
toolUseConfirm.onAllow("permanent");
onDone();
});
break;
case "no":
extractLanguageName(file_path).then((language) => {
logUnaryEvent({
completion_type: "write_file_single",
event: "reject",
metadata: {
language_name: language,
message_id: toolUseConfirm.assistantMessage.message.id,
platform: env.platform
}
});
});
toolUseConfirm.onReject();
onDone();
break;
}
}
}
))
);
}
async function extractLanguageName(file_path) {
const ext = extname(file_path);
if (!ext) {
return "unknown";
}
const Highlight = await import("highlight.js");
return Highlight.default.getLanguage(ext.slice(1))?.name ?? "unknown";
}
export {
FileWritePermissionRequest
};
//# sourceMappingURL=FileWritePermissionRequest.js.map