Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(file preview): add response from @web to file previews. #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/components/ChatForm/FilesPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@ import React from "react";
import { Box } from "@radix-ui/themes";
import { Text, TruncateLeft } from "../Text";
import { ChatContextFile } from "../../services/refact";
import { PlainText } from "../ChatContent/PlainText";
import styles from "./ChatForm.module.css";

export const FilesPreview: React.FC<{
files: ChatContextFile[];
files: (ChatContextFile | string)[];
// onRemovePreviewFile: (name: string) => void;
}> = ({ files }) => {
if (files.length === 0) return null;
return (
<Box p="2" pb="0">
{files.map((file, i) => {
if (typeof file === "string") {
return (
<pre key={"plain_text_" + i} className={styles.file}>
<Text size="1" className={styles.file_name}>
<PlainText>{file}</PlainText>
</Text>
</pre>
);
}
const lineText =
file.line1 && file.line2 ? `:${file.line1}-${file.line2}` : "";
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ function useCommandCompletion() {
};
}

function useGetCommandPreviewQuery(query: string): ChatContextFile[] {
function useGetCommandPreviewQuery(
query: string,
): (ChatContextFile | string)[] {
const hasCaps = useHasCaps();
const { data } = commandsApi.useGetCommandPreviewQuery(query, {
skip: !hasCaps,
Expand Down
72 changes: 54 additions & 18 deletions src/services/refact/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const commandsApi = createApi({
}
},
}),
getCommandPreview: builder.query<ChatContextFile[], string>({
getCommandPreview: builder.query<(ChatContextFile | string)[], string>({
queryFn: async (query, api, _opts, baseQuery) => {
const state = api.getState() as RootState;
const port = state.config.lspPort as unknown as number;
Expand All @@ -81,7 +81,7 @@ export const commandsApi = createApi({
});

if (response.error) return { error: response.error };

// console.log(response);
if (
!isCommandPreviewResponse(response.data) &&
!isDetailMessage(response.data)
Expand All @@ -99,13 +99,24 @@ export const commandsApi = createApi({
return { data: [] };
}

const files = response.data.messages.reduce<ChatContextFile[]>(
(acc, { content }) => {
const fileData = parseOrElse<ChatContextFile[]>(content, []);
const files = response.data.messages.reduce<
(ChatContextFile | string)[]
>((acc, message) => {
// can be plain text
if (isCommandFilePreview(message)) {
const fileData = parseOrElse<ChatContextFile[]>(
message.content,
[],
);
return [...acc, ...fileData];
},
[],
);
}

if (isCommandPlainTextPreview(message)) {
// TODO: add name or something
return [...acc, message.content];
}
return acc;
}, []);

return { data: files };
},
Expand Down Expand Up @@ -139,11 +150,44 @@ export function isDetailMessage(json: unknown): json is DetailMessage {
if (!("detail" in json)) return false;
return true;
}
type PreviewPlainText = {
content: string;
role: "plain_text";
};

export type CommandPreviewContent = {
type PreviewFile = {
content: string;
role: "context_file";
};

export type CommandPreviewContent = PreviewPlainText | PreviewFile;

function isCommandFilePreview(json: unknown): json is PreviewFile {
if (!json) return false;
if (typeof json !== "object") return false;
if (!("role" in json)) return false;
if (json.role !== "context_file") return false;
if (!("content" in json)) return false;
if (typeof json.content !== "string") return false;
return true;
}

function isCommandPlainTextPreview(json: unknown): json is PreviewPlainText {
if (!json) return false;
if (typeof json !== "object") return false;
if (!("role" in json)) return false;
if (json.role !== "plain_text") return false;
if (!("content" in json)) return false;
if (typeof json.content !== "string") return false;
return true;
}

function isCommandPreviewContent(json: unknown): json is CommandPreviewContent {
if (isCommandFilePreview(json)) return true;
if (isCommandPlainTextPreview(json)) return true;
return false;
}

export type CommandPreviewResponse = {
messages: CommandPreviewContent[];
};
Expand All @@ -158,13 +202,5 @@ export function isCommandPreviewResponse(

if (!json.messages.length) return true;

const firstMessage: unknown = json.messages[0];
if (!firstMessage) return false;
if (typeof firstMessage !== "object") return false;
if (!("role" in firstMessage)) return false;
if (firstMessage.role !== "context_file") return false;
if (!("content" in firstMessage)) return false;
if (typeof firstMessage.content !== "string") return false;

return true;
return json.messages.every((message) => isCommandPreviewContent(message));
}
Loading