-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip: broke it * wip: restart * revert: "wip: restart" * feat: fetch it right * chatbot! * fix: lints, etc
- Loading branch information
Showing
9 changed files
with
174 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,5 +32,6 @@ | |
"–": true, | ||
"—": true | ||
} | ||
} | ||
}, | ||
"cSpell.words": ["Preact"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { JSX } from "preact"; | ||
|
||
export function Loading(): JSX.Element { | ||
return <div class="loader" />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { z } from "zod"; | ||
import { useFetchData } from "../../utils/hooks.ts"; | ||
import { messageContentTextSchema } from "../../utils/openai-schemas.ts"; | ||
|
||
export type UseChat = z.infer<typeof useChatSchema>; | ||
|
||
export const useChatSchema = z.object({ | ||
response: messageContentTextSchema, | ||
thread_id: z.string(), | ||
}); | ||
|
||
export function useChat( | ||
message: string, | ||
thread: string | undefined, | ||
): UseChat | undefined { | ||
return useFetchData<UseChat>( | ||
`/api/chat/?${thread ? `thread=${thread}&` : ""}q=${encodeURIComponent( | ||
message, | ||
)}`, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useSignal } from "@preact/signals"; | ||
import { useCallback, useMemo, useState } from "preact/hooks"; | ||
|
||
/** | ||
* A suspense-enabled hook. | ||
*/ | ||
|
||
export function useFetchData<T>(url: string): T | undefined { | ||
const fetchJson = useCallback(async () => { | ||
const res = await fetch(url); | ||
if (!res.ok) { | ||
throw new Error(`Error: ${res.statusText}`); | ||
} | ||
return await res.json(); | ||
}, [url]); | ||
|
||
return use(fetchJson()); | ||
} | ||
|
||
export function use<T>(promise: Promise<T>): T | undefined { | ||
const status = useSignal<"pending" | "fulfilled" | "rejected">("pending"); | ||
const result = useSignal<T | undefined>(undefined); | ||
const error = useSignal<unknown>(undefined); | ||
|
||
const fetchData = useCallback(async () => { | ||
try { | ||
result.value = await promise; | ||
status.value = "fulfilled"; | ||
} catch (e) { | ||
error.value = e; | ||
status.value = "rejected"; | ||
} | ||
}, [promise]); | ||
|
||
// Preact Signals dislike promises. | ||
const [dataPromise] = useState(fetchData); | ||
const data = useMemo(() => dataPromise, [dataPromise]); | ||
|
||
switch (status.value) { | ||
case "pending": | ||
throw data; // Suspend | ||
|
||
case "fulfilled": | ||
return result.value; // Result is a fulfilled promise | ||
|
||
case "rejected": | ||
throw error.value; // Result is an error | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { MessageContentText } from "openai/resources/beta/threads/messages/messages.ts"; | ||
import { z } from "zod"; | ||
|
||
/** | ||
* This is very basic, and doesn't check anything beyond that it's an object. | ||
*/ | ||
export const messageContentTextSchema = z.custom<MessageContentText>( | ||
(val) => z.object({}).safeParse(val).success, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters