import { useEffect, useRef, useState } from "react";
import {
Select,
SelectContent,
SelectItem,
SelectSeparator,
SelectTrigger,
SelectValue,
} from "~/components/Select";
import { X as Close } from "lucide-react";
import { Button } from "~/components/Button";
import { Input, TextArea } from "~/components/Input";
import { cn } from "~/utils/cn";
const TEXT_COMMON_STYLE = cn(
"col-[1] row-[1]",
"flex h-full w-full rounded-md border border-slate-300 bg-transparent py-2 px-3 text-sm leading-[22px]"
);
function AutosizingTextArea(props: {
value: string;
onChange: (value: string) => void;
placeholder?: string;
}) {
return (
);
}
function getChatGPTEncoding(
messages: { role: string; content: string; name: string }[],
model:
| "gpt-3.5-turbo"
| "gpt-4-1106-preview"
| "gpt-4"
| "gpt-4o"
| "gpt-4-32k"
) {
const isGpt3 = model === "gpt-3.5-turbo";
const msgSep = isGpt3 ? "\n" : "";
const roleSep = isGpt3 ? "\n" : "<|im_sep|>";
return [
messages
.map(({ name, role, content }) => {
return `<|im_start|>${name || role}${roleSep}${content}<|im_end|>`;
})
.join(msgSep),
`<|im_start|>assistant${roleSep}`,
].join(msgSep);
}
export function ChatGPTEditor(props: {
model:
| "gpt-4"
| "gpt-4-1106-preview"
| "gpt-4-32k"
| "gpt-4o"
| "gpt-3.5-turbo";
onChange: (value: string) => void;
}) {
const [rows, setRows] = useState<
{ role: string; content: string; name: string }[]
>([
{ role: "system", content: "You are a helpful assistant", name: "" },
{ role: "user", content: "", name: "" },
]);
const changeRef = useRef<(value: string) => void>(props.onChange);
// not ideal, but will suffice for now
useEffect(() => void (changeRef.current = props.onChange), [props.onChange]);
useEffect(() => {
changeRef.current?.(getChatGPTEncoding(rows, props.model));
}, [props.model, rows]);
return (
{rows.map((row, i) => (
))}
);
}