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: prompt uses vector store #71

Merged
merged 8 commits into from Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
123 changes: 123 additions & 0 deletions src/client/atoms/logo/captain.tsx
@@ -0,0 +1,123 @@
import type { SvgIconProps } from "@mui/joy/SvgIcon";
import SvgIcon from "@mui/joy/SvgIcon";

export function Captain(properties: SvgIconProps) {
return (
<SvgIcon {...properties} viewBox="0 0 14.18 17.94">
<path
fill="#046261"
d="M4.73 11.11c-.6 2.35.51 4.25 2.16 5.82l.02.11v.84c-.2-.05-.59-.39-.76-.54-.43-.38-.88-.91-1.24-1.36-1.41-.04-3.69-.61-4.61-1.75-.32-.4-.49-.95.05-1.25.13-.07 1.86-.66 2.36-.87.7-.29 1.36-.66 2.03-1.01Z"
/>
<path
fill="#061f1c"
d="M13.8 12.96c.86.47.06 1.34-.43 1.73-1.05.83-2.76 1.24-4.08 1.29-.34.43-1.5 1.73-1.96 1.9v-1c1.65-1.59 2.71-3.44 2.16-5.8.58.31 1.16.64 1.77.91.65.29 2.42.89 2.54.96Z"
/>
<path
fill="#101916"
d="M10.05 6.61s-.04-.01-.09.06c0 .33 0 .68-.12 1-.01.04-.15.32-.16.33-.06.04-.12.02-.19.02.06-.23.31-2.29.01-2.44-.81-.03-1.62.02-2.43.02-.72 0-1.68-.05-2.32-.02-.11 0-.22-.04-.28.07-.1.03.04-.11.06-.12.12-.04 2.43.1 2.53.02.01-.01.02-.28.01-.29-.09-.12-2.64.07-2.99-.01V5.2c.65.09 2.24 0 2.99 0 .73 0 2.54.04 2.99 0 .05 0 .23-.06.28-.09.06.73.17.91-.29 1.5Z"
/>
<path
fill="#6c5042"
d="M10.05 6.61c.16.41.13.75-.21 1.05.11-.32.13-.66.12-1 .06-.07.09-.05.09-.06Z"
/>
<path
fill="#abbcb6"
d="M9.26 10.64c.03.14.09.41.23.45.55 2.36-.5 4.21-2.16 5.8v1s-.06.1-.19.02c-.04-.25-.03-.52-.02-.77.02-1.03.03-2.05.02-3.08-.02-.02-.04-.03-.05-.06.35-.63.69-1.27 1.08-1.88.1-.15.31-.38.42-.55.18-.27.36-.62.49-.81.06-.09.06-.17.16-.12Z"
/>
<path
fill="#cdd9d3"
d="M7.12 17.13c0 .26-.02.52.02.77-.09 0-.15 0-.23-.02v-.84c.07-.03.16.02.21.09Z"
/>
<path
fill="#f7faf8"
d="M4.76 5.58c.06.6 0 2.12.05 2.5.06.5.56 1.22 1.14 1.02.24-.08.34-.72 1.14-.21v.4c-.14.02-.28.07-.42.09-.23.04-.47.04-.69.12-.05.05.97.29 1.18.28l-.02 1.76c-.24 0-.57 0-.8-.05-.38-.08-.73-.34-1.08-.5.21.38.48.72.7 1.09.19.3 1.03 1.69 1.1 1.83.01.03.05.09.05.09 0 .03.03.04.05.06 0 1.03 0 2.05-.02 3.08-.05-.07-.14-.12-.21-.09l-.02-.11c-1.64-1.58-2.76-3.48-2.16-5.82.02-.08.18-.38.16-.43-1.11-.99-.49-2.14-.47-2.71 0-.14-.02-.29-.05-.42.03-.6-.07-1.22.07-1.8 0-.03.02-.06.02-.09.06-.11.17-.07.28-.07Z"
/>
<path
fill="#0a3933"
d="M4.1 5.2v.05c.34.08 2.89-.11 2.98.01 0 .01 0 .28-.01.29-.1.07-2.41-.06-2.53-.02-.02 0-.16.15-.06.12 0 .03-.02.06-.02.09l-.59-.42c0-.08-.01-.14.02-.14.07.02.14.01.21.02Z"
/>
<path
fill="#05514c"
d="M4.45 5.74c-.14.59-.04 1.2-.07 1.8-.04-.19-.16-.38-.19-.57-.01-.08.02-.37 0-.4-.02-.03-.08 0-.09-.01-.4-.22-.24-.87-.23-1.24l.59.42Z"
/>
<path
fill="#c7b5a6"
d="M4.38 7.55c.03.14.05.28.05.42-.14-.09-.43-.77-.45-.9-.04-.36.08-.32.12-.5.02 0 .08-.01.09.01.02.03-.01.32 0 .4.03.19.15.39.19.57Z"
/>
<path
fill="#061f1c"
d="M12.04 2.77c-.06.67-.89 1.25-1.5 1.29-.61-.66-2.58-.84-3.47-.84 0-.07.03-.12 0-.19l.02-.02c.44-.05.76-.27.98-.64.37-.86-.08.32-.98.29 0-.09 0-.17-.02-.26h.05c.79-.08.9-1.53.05-1.57V0c.62 0 1.18.07 1.77.26.58.18 2.18.97 2.6 1.36.32.3.55.7.5 1.15Z"
/>
<path
fill="#046261"
d="M7.17 0v.82h-.05c-.9.04-.84 1.5-.05 1.57.03.09.02.17.02.26-.42-.01-.79-.27-1.05-.57.13.52.47.93 1.03.95.03.07 0 .11 0 .19-.86 0-2.94.14-3.46.87-.03 0-.2-.08-.25-.09-.57-.18-1.18-.63-1.2-1.29-.02-.95 1.19-1.6 1.93-1.97C5.12.23 6.04 0 7.17 0Z"
/>
<path
fill="#061f1c"
d="M10.47 4.5c.1.13.01.3-.13.35-.31.12-2.71 0-3.27.05 0 0 0-.03-.02-.02V3.87h.02c.65 0 1.32.05 1.96.16.24.04 1.32.31 1.44.47Z"
/>
<path
fill="#f7faf8"
d="M3.72 4.48c.01.05-.15.34.27.4.4.06 2.37-.04 3.06 0 .03 0 .02.02.02.02.01.03 0 .22 0 .3-.75 0-2.34.09-2.98 0-.07-.01-.14 0-.21-.02l-.07-.04c.02-.23-.29-.53-.09-.67Z"
/>
<path
fill="#046261"
d="M7.05 3.87v1.01c-.69-.04-2.65.06-3.06 0-.42-.06-.26-.35-.27-.4v-.02c.99-.48 2.24-.59 3.33-.59Z"
/>
<path
fill="#abbcb6"
d="m10.54 4.05-.07.45c-.12-.16-1.2-.43-1.44-.47-.64-.12-1.31-.16-1.96-.16 0-.22-.01-.44 0-.66.89 0 2.86.19 3.47.84Z"
/>
<path
fill="#f7faf8"
d="M7.08 3.21c-.01.22 0 .44 0 .66h-.02c-1.09 0-2.34.11-3.33.59-.09-.49-.17-.28-.35-.47.05.02.21.1.25.09.52-.72 2.6-.87 3.46-.87ZM7.12.82v1.57h-.05c-.8-.07-.85-1.53.05-1.57Z"
/>
<path fill="#abbcb6" d="M7.17.82c.85.04.74 1.49-.05 1.57V.82h.05Z" />
<path
fill="#f7faf8"
d="M7.1 2.65V3l-.02.02c-.56-.02-.91-.43-1.03-.95.27.3.64.56 1.05.57Z"
/>
<path fill="#b5d4d0" d="M7.1 3v-.35c.9.03 1.35-1.15.98-.29-.23.38-.54.6-.98.64Z" />
<path
fill="#82624e"
d="M9.49 8.02c-.08.32-.42.9-.74 1.03-.92.37-.68-.8-1.68-.16V5.61c.81 0 1.62-.05 2.43-.02.3.15.05 2.21-.01 2.44Z"
/>
<path
fill="#c7b5a6"
d="M7.08 5.6v3.28c-.79-.51-.89.13-1.14.21-.58.2-1.07-.52-1.14-1.02-.05-.37.01-1.89-.05-2.5.64-.02 1.6.02 2.32.02Z"
/>
<path
fill="#82624e"
d="M9.09 10.76c-.13.19-.31.54-.49.81-.11.17-.33.4-.42.55-.36.26-.72.53-1.12.73-.01-.16-.02-.32-.05-.48l-.68-.88c.23.05.56.05.8.05.73-.01 1.41-.29 1.97-.77Z"
/>
<path
fill="#c7b5a6"
d="m6.33 11.48.68.88c.02.16.04.32.05.48v.02l-1.1-.8c-.23-.37-.49-.71-.7-1.09.35.16.7.42 1.08.5Z"
/>
<path
fill="#061f1c"
d="M7.1 13.99s-.03-.07-.05-.09c-.01-.34.02-.69 0-1.03v-.02c.4-.2.76-.47 1.12-.73-.39.61-.73 1.25-1.08 1.88Z"
/>
<path
fill="#046261"
d="M7.05 12.87c.02.34-.01.69 0 1.03-.07-.14-.92-1.53-1.1-1.83l1.1.8Z"
/>
<path
fill="#9cbfb9"
d="M9.26 10.64c-.11-.05-.1.03-.16.12-.56.48-1.23.76-1.97.77l.02-1.76c.31 0 .74-.16 1.03-.27-.25-.06-.59-.17-.84-.2-.08 0-.17-.04-.26-.02v-.4c1-.63.76.53 1.68.16.32-.13.66-.71.74-1.03.07 0 .12.02.19-.02.08.03.16.57.16.71.04.77-.13 1.29-.63 1.88.02.02.04.03.05.06Z"
/>
<path
fill="#6c5042"
d="M7.15 9.77v-.33c.11 0 .26 0 .19-.14.25.03.59.13.84.2-.3.11-.73.26-1.03.27Z"
/>
<path
fill="#271c11"
d="M7.33 9.3c.07.13-.08.14-.19.14-.07 0-.39-.05-.49-.07.14-.02.28-.08.42-.09.09-.02.18.01.26.02Z"
/>
<path
fill="#c7b5a6"
d="M6.65 9.38c.11.02.42.07.49.07v.33c-.21 0-1.24-.23-1.18-.28.22-.08.46-.08.69-.12Z"
/>
</SvgIcon>
);
}
39 changes: 39 additions & 0 deletions src/client/ions/hooks/vector-store.ts
@@ -0,0 +1,39 @@
import { useEffect, useState } from "react";
import { useDebounce } from "use-debounce";

import type { VectorStoreDocument } from "../../../electron/future/services/vector-store";

import { buildKey } from "#/build-key";
import { ID } from "#/enums";

export function useVectorStore(value: string) {
const [query] = useDebounce(value, 1000);
const [results, setResults] = useState<VectorStoreDocument[]>([]);

useEffect(() => {
if (query.trim()) {
window.ipc.send(buildKey([ID.VECTOR_STORE], { suffix: ":search" }), query);
}
}, [query]);

useEffect(() => {
const unsubscribeResult = window.ipc.on(
buildKey([ID.VECTOR_STORE], { suffix: ":result" }),
data => {
console.log(data);
setResults(data);
}
);
const unsubscribeError = window.ipc.on(
buildKey([ID.VECTOR_STORE], { suffix: ":error" }),
error => {
console.log(error);
}
);
return () => {
unsubscribeResult();
unsubscribeError();
};
}, []);
return results;
}
48 changes: 15 additions & 33 deletions src/client/pages/[locale]/core/dashboard.tsx
@@ -1,51 +1,23 @@
import Input from "@mui/joy/Input";
import List from "@mui/joy/List";
import ListItem from "@mui/joy/ListItem";
import ListItemButton from "@mui/joy/ListItemButton";
import Sheet from "@mui/joy/Sheet";
import type { InferGetStaticPropsType } from "next";
import Head from "next/head";
import { useTranslation } from "next-i18next";
import { useEffect, useState } from "react";
import { useDebounce } from "use-debounce";

import type { VectorStoreDocument } from "../../../../electron/future/services/vector-store";
import { useState } from "react";

import { buildKey } from "#/build-key";
import { ID } from "#/enums";
import { Logo } from "@/atoms/logo";
import { useVectorStore } from "@/ions/hooks/vector-store";
import { makeStaticProperties } from "@/ions/i18n/get-static";

export default function Page(_properties: InferGetStaticPropsType<typeof getStaticProps>) {
const { t } = useTranslation(["common", "labels"]);
const [value, setValue] = useState("");
const [results, setResults] = useState<VectorStoreDocument[]>([]);
const [query] = useDebounce(value, 1000);

useEffect(() => {
if (query) {
window.ipc.send(buildKey([ID.VECTOR_STORE], { suffix: ":search" }), query);
}
}, [query]);

useEffect(() => {
const unsubscribeResult = window.ipc.on(
buildKey([ID.VECTOR_STORE], { suffix: ":result" }),
data => {
console.log(data);
setResults(data);
}
);
const unsubscribeError = window.ipc.on(
buildKey([ID.VECTOR_STORE], { suffix: ":error" }),
error => {
console.log(error);
}
);
return () => {
unsubscribeResult();
unsubscribeError();
};
}, []);
const results = useVectorStore(value);

return (
<>
Expand All @@ -65,7 +37,17 @@ export default function Page(_properties: InferGetStaticPropsType<typeof getStat
<Sheet>
<List>
{results.map(result => (
<ListItem key={result.id}>{result.payload.id}</ListItem>
<ListItem key={result.id}>
<ListItemButton
onClick={() => {
window.ipc.send(buildKey([ID.APP], { suffix: ":open" }), {
data: result.payload.id,
});
}}
>
{result.payload.id}
</ListItemButton>
</ListItem>
))}
</List>
</Sheet>
Expand Down
39 changes: 20 additions & 19 deletions src/client/pages/[locale]/prompt.tsx
Expand Up @@ -13,7 +13,9 @@ import { useEffect, useState, useRef } from "react";
import { buildKey } from "#/build-key";
import { ID } from "#/enums";
import { Logo } from "@/atoms/logo";
import { Captain } from "@/atoms/logo/captain";
import { useResizeObserver } from "@/ions/hooks/resize-observer";
import { useVectorStore } from "@/ions/hooks/vector-store";

export function useAutoFocusIPC<T extends HTMLElement>(reference: RefObject<T>) {
useEffect(() => {
Expand All @@ -39,23 +41,11 @@ export default function Page() {
const frameReference = useRef<HTMLDivElement | null>(null);
const promptReference = useRef<HTMLInputElement | null>(null);
const [value, setValue] = useState("");
const [suggestions, setSuggestions] = useState<{ id: string; label: string }[]>([]);
const suggestions = useVectorStore(value);

useAutoFocusIPC(promptReference);
useAutoSizerWindow(frameReference);

useEffect(() => {
const unsubscribe = window.ipc.on(
buildKey([ID.PROMPT], { suffix: ":suggestion" }),
(suggestions_: { id: string; label: string }[]) => {
setSuggestions(suggestions_);
}
);
return () => {
unsubscribe();
};
}, []);

return (
<Box
ref={frameReference}
Expand All @@ -79,8 +69,17 @@ export default function Page() {
<Input
placeholder="I want to draw something..."
endDecorator={
<Box sx={{ flex: 1, display: "flex", alignItems: "center" }}>
<Logo sx={{ height: 30 }} />
<Box
sx={{
flex: 1,
display: "flex",
alignItems: "center",
justifyContent: "center",
height: 44,
width: 44,
}}
>
<Captain sx={{ height: "100%" }} />
</Box>
}
slotProps={{
Expand Down Expand Up @@ -108,9 +107,11 @@ export default function Page() {
onKeyDown={event => {
if (event.key === "Enter" && !event.shiftKey) {
event.preventDefault();
window.ipc.send(buildKey([ID.APP], { suffix: ":open" }), {
data: value,
});
if (suggestions[0]) {
window.ipc.send(buildKey([ID.APP], { suffix: ":open" }), {
data: suggestions[0].payload.id,
});
}
}
}}
/>
Expand Down Expand Up @@ -152,7 +153,7 @@ export default function Page() {
</ListItemDecorator>
<ListItemContent>
<Typography level="h4" component="div">
{suggestion.label}
{suggestion.payload.id}
</Typography>
</ListItemContent>
</ListItemButton>
Expand Down