Skip to content

Commit

Permalink
🐛 import react bugs on nextjs (#58)
Browse files Browse the repository at this point in the history
* 🐛 Fix generate return/params type

* 🐛 Fix react import bugs by moving hooks to polyfact/hooks
  • Loading branch information
lowczarc authored Sep 11, 2023
1 parent 942a76d commit db8de1a
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 94 deletions.
8 changes: 1 addition & 7 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@ jobs:
- name: Install dependencies using Yarn
run: yarn install

- name: Build lib
run: npm run build

- name: Build bin
run: npm run cmd:build

- name: Publish to npm
run: npm publish
run: npm run publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

Expand Down
19 changes: 6 additions & 13 deletions examples/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,15 @@ import { stdin as input, stdout as output } from "node:process";
import { AudioLoader, Chat } from "../lib/index";
import fs from "fs";

const clientOptions = {
endpoint: "http://localhost:8080",
token: "<YOUR_POLYFACT_TOKEN>",
};
async function main() {
fs.readFile(`${__dirname}/dataloader/AudioLoader.mp3`, async function (err, data) {
if (err) throw err;
const chat = new Chat(
{
autoMemory: true,
provider: "openai",
model: "gpt-3.5-turbo-16k",
// systemPromptId: "8fc39ca4-3941-40d9-824a-5ed283102f6e", // Holy Bible Prompt
},
clientOptions,
);
const chat = new Chat({
autoMemory: true,
provider: "openai",
model: "gpt-3.5-turbo-16k",
// systemPromptId: "8fc39ca4-3941-40d9-824a-5ed283102f6e", // Holy Bible Prompt
});

await chat.dataLoader(AudioLoader(data), (step) => console.log(step));

Expand Down
4 changes: 4 additions & 0 deletions lib/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import usePolyfact from "./usePolyfact";
import useChat from "./useChat";

export { usePolyfact, useChat };
22 changes: 10 additions & 12 deletions lib/hooks/useChat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { useState, useEffect } from "react";
import usePolyfact from "./usePolyfact";
import type { Chat } from "../chats";

export type Message = {
id: string | null;
Expand All @@ -15,19 +17,12 @@ export default function useChat(): {
} {
const { polyfact } = usePolyfact(null);

let react;
try {
react = require("react"); // eslint-disable-line
} catch (_) {
throw new Error("usePolyfact not usable outside of a react environment");
}

const [chat, setChat] = react.useState();
const [history, setHistory] = react.useState([]);
const [messages, setMessages] = react.useState([]);
const [loading, setLoading] = react.useState(true);
const [chat, setChat] = useState<Chat>();
const [history, setHistory] = useState<Message[]>([]);
const [messages, setMessages] = useState<Message[]>([]);
const [loading, setLoading] = useState(true);

react.useEffect(() => {
useEffect(() => {
if (polyfact) {
setChat(new polyfact.Chat());
setHistory([]);
Expand All @@ -36,6 +31,9 @@ export default function useChat(): {
}, [polyfact]);

async function sendMessage(message: string) {
if (chat === undefined) {
throw new Error("Chat not initialized");
}
const userMessage = {
id: null,
chat_id: await chat.chatId,
Expand Down
39 changes: 13 additions & 26 deletions lib/hooks/usePolyfact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createClient } from "@supabase/supabase-js";
import { Mutex } from "async-mutex";
import { supabaseDefaultClient } from "../clientOpts";
import { useState, useEffect } from "react";
import Polyfact, { Client } from "../client";

declare const window: any;
Expand All @@ -22,29 +21,17 @@ export default function usePolyfact(
loading: boolean;
} {
const { project, endpoint } = args || {};
if (typeof window === "undefined") {
console.error("usePolyfact not usable outside of the browser environment");
return {
loading: true,
polyfact: undefined,
login: undefined,
loginWithFirebase: undefined,
};
}
const [polyfact, setPolyfact] = useState<Client>();
const [email, setEmail] = useState<string>();
const [loading, setLoading] = useState(true);
const [login, setLogin] = useState<
((input: { provider: Provider }) => Promise<void>) | undefined
>();
const [loginWithFirebase, setLoginWithFirebase] = useState<
((token: string) => Promise<void>) | undefined
>();

let react;
try {
react = require("react"); // eslint-disable-line
} catch (_) {
throw new Error("usePolyfact not usable outside of a react environment");
}
const [polyfact, setPolyfact] = react.useState();
const [email, setEmail] = react.useState();
const [loading, setLoading] = react.useState(true);
const [login, setLogin] = react.useState();
const [loginWithFirebase, setLoginWithFirebase] = react.useState();

react.useEffect(() => {
useEffect(() => {
if (project) {
(async () => {
await reactMutex.acquire();
Expand Down Expand Up @@ -76,8 +63,8 @@ export default function usePolyfact(
const p = await Polyfact.endpoint(endpoint || "https://api.polyfact.com")
.project(project)
.signInWithFirebaseToken(token);
setLogin();
setLoginWithFirebase();
setLogin(undefined);
setLoginWithFirebase(undefined);
setPolyfact(p);
window.Polyfact = p;
});
Expand Down
4 changes: 0 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ export {
t,
kv,

// hooks
usePolyfact,
useChat,

// Image generation
generateImage,

Expand Down
24 changes: 0 additions & 24 deletions lib/tsconfig.json

This file was deleted.

17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
{
"name": "polyfact",
"version": "0.1.59",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
"main": "index.js",
"types": "index.d.ts",
"author": "Lancelot Owczarczak <[email protected]>",
"license": "MIT",
"bin": "build/polyfact",
"bin": "polyfact",
"dependencies": {
"@supabase/supabase-js": "^2.31.0",
"@types/inquirer": "^9.0.3",
"@types/pdf-parse": "^1.1.1",
"@types/readable-stream": "^4.0.0",
"async-mutex": "^0.4.0",
"axios": "^0.27.2",
"buffer": "^6.0.3",
Expand All @@ -22,6 +19,7 @@
"js-tiktoken": "^1.0.7",
"pdf-parse": "^1.1.1",
"polyfact-io-ts": "^2.2.20",
"react": "^18.2.0",
"readable-stream": "^4.4.2",
"ts-node": "^10.9.1",
"uuid": "^9.0.0",
Expand All @@ -32,6 +30,10 @@
"@types/isomorphic-fetch": "^0.0.36",
"@types/node": "^20.4.5",
"@types/uuid": "^9.0.2",
"@types/inquirer": "^9.0.3",
"@types/pdf-parse": "^1.1.1",
"@types/react": "^18.2.21",
"@types/readable-stream": "^4.0.0",
"@typescript-eslint/eslint-plugin": "^4.14.1",
"@typescript-eslint/parser": "^4.14.1",
"cli-progress": "^3.12.0",
Expand Down Expand Up @@ -67,6 +69,7 @@
"lint:fix": "prettier --write lib/ cmd/ ; eslint --fix lib/ cmd/",
"cmd:build": "esbuild cmd/index.ts --bundle --outfile=build/polyfact.tmp --platform=node && echo '#!/usr/bin/env node\n' | cat - build/polyfact.tmp > build/polyfact && rm build/polyfact.tmp",
"cmd:vanilla-js": "esbuild target/vanilla-js.ts --bundle --minify --target=chrome67,firefox68,edge79,safari15 --outfile=build/vanilla-js.js",
"build": "tsc -p tsconfig.json --declaration --outDir dist"
"build": "tsc --target es2021 --lib es2021,DOM --module commonjs --strict --esModuleInterop --declaration --skipLibCheck --outDir dist --rootDir lib lib/*.ts lib/**/*.ts && npm run cmd:build && cp build/polyfact package.json README.md dist/",
"publish": "npm run build && cd dist && npm publish && cd .."
}
}
40 changes: 39 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,20 @@
resolved "https://registry.npmjs.org/@types/phoenix/-/phoenix-1.6.0.tgz"
integrity sha512-qwfpsHmFuhAS/dVd4uBIraMxRd56vwBUYQGZ6GpXnFuM2XMRFJbIyruFKKlW2daQliuYZwe0qfn/UjFCDKic5g==

"@types/prop-types@*":
version "15.7.5"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.5.tgz#5f19d2b85a98e9558036f6a3cacc8819420f05cf"
integrity sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==

"@types/react@^18.2.21":
version "18.2.21"
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.2.21.tgz#774c37fd01b522d0b91aed04811b58e4e0514ed9"
integrity sha512-neFKG/sBAwGxHgXiIxnbm3/AAVQ/cMRS93hvBpg8xYRbeQSPVABp9U2bRnPf0iI4+Ucdv3plSxKK+3CW2ENJxA==
dependencies:
"@types/prop-types" "*"
"@types/scheduler" "*"
csstype "^3.0.2"

"@types/readable-stream@^4.0.0":
version "4.0.0"
resolved "https://registry.npmjs.org/@types/readable-stream/-/readable-stream-4.0.0.tgz"
Expand All @@ -265,6 +279,11 @@
"@types/node" "*"
safe-buffer "~5.1.1"

"@types/scheduler@*":
version "0.16.3"
resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.3.tgz#cef09e3ec9af1d63d2a6cc5b383a737e24e6dcf5"
integrity sha512-5cJ8CB4yAx7BH1oMvdU0Jh9lrEXyPkar6F9G/ERswkCuvP4KQZfZkSjcMbAICCpQTN4OuZn8tz0HiKv9TGZgrQ==

"@types/through@*":
version "0.0.30"
resolved "https://registry.yarnpkg.com/@types/through/-/through-0.0.30.tgz#e0e42ce77e897bd6aead6f6ea62aeb135b8a3895"
Expand Down Expand Up @@ -716,6 +735,11 @@ cross-spawn@^7.0.2:
shebang-command "^2.0.0"
which "^2.0.1"

csstype@^3.0.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.2.tgz#1d4bf9d572f11c14031f0436e1c10bc1f571f50b"
integrity sha512-I7K1Uu0MBPzaFKg4nI5Q7Vs2t+3gWWW648spaF+Rg7pI9ds18Ugn+lvg4SHczUdKlHI5LWBXyqfS8+DufyBsgQ==

d@1, d@^1.0.1:
version "1.0.1"
resolved "https://registry.npmjs.org/d/-/d-1.0.1.tgz"
Expand Down Expand Up @@ -1890,7 +1914,7 @@ js-tiktoken@^1.0.7:
dependencies:
base64-js "^1.5.1"

js-tokens@^4.0.0:
"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz"
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==
Expand Down Expand Up @@ -1973,6 +1997,13 @@ log-symbols@^4.1.0:
chalk "^4.1.0"
is-unicode-supported "^0.1.0"

loose-envify@^1.1.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
dependencies:
js-tokens "^3.0.0 || ^4.0.0"

lru-cache@^6.0.0:
version "6.0.0"
resolved "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz"
Expand Down Expand Up @@ -2243,6 +2274,13 @@ queue-microtask@^1.2.2:
resolved "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz"
integrity sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==

react@^18.2.0:
version "18.2.0"
resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz#555bd98592883255fa00de14f1151a917b5d77d5"
integrity sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==
dependencies:
loose-envify "^1.1.0"

readable-stream@^3.4.0:
version "3.6.2"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.6.2.tgz#56a9b36ea965c00c5a93ef31eb111a0f11056967"
Expand Down

0 comments on commit db8de1a

Please sign in to comment.