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

⚠️ Fix eslint warnings #72

Merged
merged 3 commits into from
Sep 20, 2023
Merged
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
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"no-await-in-loop": ["off"],
"no-continue": ["off"],
"no-console": ["off"],
"tsdoc/syntax": "warn"
"tsdoc/syntax": "warn",
"@typescript-eslint/no-empty-function": ["off"]
}
}
4 changes: 2 additions & 2 deletions cmd/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default async function agent(token: string): Promise<void> {
);
console.info("Don't forget to set your PROJECT_ID.\n");
console.info("you can get one at https://app.polyfact.com.\n\n");
} catch (error: any) {
console.error("An error occurred:", error.message);
} catch (error: unknown) {
console.error("An error occurred:", error instanceof Error ? error.message : error);
}
}
4 changes: 2 additions & 2 deletions cmd/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ export default async function chat(token: string): Promise<boolean> {
console.info(
`You can now make "cd ${repoName} ; npm install ; npm run dev" to start the chatbot.`,
);
} catch (error: any) {
console.error("An error occurred:", error.message);
} catch (error: unknown) {
console.error("An error occurred:", error instanceof Error ? error.message : error);
}

return true;
Expand Down
6 changes: 3 additions & 3 deletions cmd/docs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function waitSimpleGeneration<T extends GetResult>(
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);

progressBar.start(1, 0);
await new Promise<void>((res, rej) => {
await new Promise<void>((res) => {
const interval = setInterval(async () => {
const { status } = await getFunction(docId, token).catch(() => ({ status: undefined }));

Expand Down Expand Up @@ -137,8 +137,8 @@ export default async function generateDocs(): Promise<string> {
}

return docId;
} catch (error: any) {
console.error("An error occurred:", error.message);
} catch (error: unknown) {
console.error("An error occurred:", error instanceof Error ? error.message : error);
throw error;
}
}
11 changes: 5 additions & 6 deletions lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { MutablePromise } from "./utils";
import { ApiError, ErrorData } from "./helpers/error";

type SimpleProvider = "github" | "google";
type Provider = SimpleProvider | "firebase";
type LoginWithFirebaseInput = { token: string; provider: "firebase" };
type LoginFunctionInput = SimpleProvider | { provider: SimpleProvider } | LoginWithFirebaseInput;

Expand All @@ -18,7 +17,7 @@ const supabaseClient = createClient(
},
);

declare const window: any;
declare const window: Window;

const getSessionMutex = new Mutex();

Expand Down Expand Up @@ -55,7 +54,7 @@ export async function getSession(): Promise<{ token?: string; email?: string }>

token = data.session?.access_token || "";

if (!token) {
if (!token || !data.session?.refresh_token) {
window.localStorage.removeItem("polyfact_refresh_token");
return {};
}
Expand All @@ -79,7 +78,7 @@ export async function oAuthRedirect(
const { data } = await supabaseClient.auth.signInWithOAuth({
...credentials,
options: {
redirectTo: window?.location,
redirectTo: `${window?.location}`,
skipBrowserRedirect: !browserRedirect,
},
});
Expand Down Expand Up @@ -117,9 +116,9 @@ export async function login(
co: MutablePromise<Partial<ClientOptions>>,
): Promise<void> {
await co.deresolve();
console.log("login", input);
if (typeof input === "object" && input.provider === "firebase") {
return signInWithOAuthToken(input.token, "firebase", co, projectOptions);
signInWithOAuthToken(input.token, "firebase", co, projectOptions);
return;
}

const provider = typeof input === "string" ? input : input.provider;
Expand Down
4 changes: 2 additions & 2 deletions lib/chats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class Chat {
await this.clientOptions,
).pipeInto(resultStream);

result.on("data", (d: any) => {
result.on("data", (d: string) => {
aiMessage = aiMessage.concat(d);
});

Expand Down Expand Up @@ -159,7 +159,7 @@ export class Chat {
},
);

return response?.data?.filter((message: any): message is t.TypeOf<typeof Message> =>
return response?.data?.filter((message: unknown): message is t.TypeOf<typeof Message> =>
Message.is(message),
);
} catch (e: unknown) {
Expand Down
69 changes: 7 additions & 62 deletions lib/dataloader/index.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,31 @@
import { Buffer } from "buffer";
import { Memory } from "../memory";
import { transcribe } from "../transcribe";
import { splitString } from "../split";
import { FileInput, fileInputToBuffer } from "../utils";

import { InputClientOptions } from "../clientOpts";

interface MinimalStream {
on(event: string | symbol, listener: (...args: any[]) => void): this;
}

interface FetchReadableStream {
getReader(): {
read(): Promise<{ done: boolean; value?: Uint8Array | undefined }>;
};
}

type LoaderFileInput = MinimalStream | Buffer | FetchReadableStream;

function stream2buffer(stream: MinimalStream): Promise<Buffer> {
return new Promise((resolve, reject) => {
const buf: any[] = [];

stream.on("data", (chunk) => buf.push(chunk));
stream.on("end", () => resolve(Buffer.concat(buf)));
stream.on("error", (err) => reject(err));
});
}

async function fetchStream2buffer(stream: FetchReadableStream): Promise<Buffer> {
const reader = stream.getReader();
const chunks: Uint8Array[] = [];

let done = false;
let value: Uint8Array | undefined;
while (!done) {
// eslint-disable-next-line no-await-in-loop
({ done, value } = await reader.read());
if (value) {
chunks.push(value);
}
}

return Buffer.concat(chunks);
}

async function loaderInputToBuffer(input: LoaderFileInput): Promise<Buffer> {
if (input instanceof Buffer) {
return input;
}

if ("on" in input) {
return stream2buffer(input);
}

if ("getReader" in input) {
return fetchStream2buffer(input);
}

return null as never;
}

// eslint-disable-next-line consistent-return
async function batchify<T extends Array<unknown>>(
array: T,
size: number,
callback: (input: T) => Promise<void>,
): Promise<void> {
if (array.length < size) {
return callback(array);
await callback(array);
return;
}
await callback(array.slice(0, size) as T);
await batchify(array.slice(size) as T, size, callback);
}

export type LoaderFunction = (memory: Memory, clientOptions: InputClientOptions) => Promise<void>;

export function TextFileLoader(file: LoaderFileInput, maxTokenPerChunk = 100): LoaderFunction {
export function TextFileLoader(file: FileInput, maxTokenPerChunk = 100): LoaderFunction {
return async function loadPdfIntoMemory(
memory: Memory,
_clientOptions: InputClientOptions = {},
) {
const fileBuffer = await loaderInputToBuffer(file);
const fileBuffer = await fileInputToBuffer(file);
const splittedFile = splitString(fileBuffer.toString("utf8"), maxTokenPerChunk);

async function addBatchIntoMemory(batches: string[]) {
Expand All @@ -106,12 +51,12 @@ export function StringLoader(str: string, maxTokenPerChunk = 100): LoaderFunctio
};
}

export function AudioLoader(file: LoaderFileInput, maxTokenPerChunk = 100): LoaderFunction {
export function AudioLoader(file: FileInput, maxTokenPerChunk = 100): LoaderFunction {
return async function loadAudioIntoMemory(
memory: Memory,
clientOptions: InputClientOptions = {},
) {
const fileBuffer = await loaderInputToBuffer(file);
const fileBuffer = await fileInputToBuffer(file);
const transcription = await transcribe(fileBuffer, clientOptions);
const transcriptions = splitString(transcription, maxTokenPerChunk);

Expand Down
38 changes: 15 additions & 23 deletions lib/generate.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
/* eslint-disable camelcase */
import axios, { AxiosError } from "axios";
import * as t from "polyfact-io-ts";
import fakeProcess from "process";
import { Readable } from "readable-stream";
import WebSocket from "isomorphic-ws";
import { UUID } from "crypto";
import { InputClientOptions, defaultOptions } from "./clientOpts";
import { Memory } from "./memory";
import { ApiError, ErrorData } from "./helpers/error";
import { loaderToMemory, LoaderFunction } from "./dataloader";

declare const window: any;
declare const window: {
process: typeof fakeProcess;
};

if (typeof window !== "undefined") {
window.process = fakeProcess;
}

const PartialResultType = t.partial({
ressources: t.array(t.type({ id: t.string, content: t.string, similarity: t.number })),
});

const Required = t.type({
result: t.string,
token_usage: t.type({
input: t.number,
output: t.number,
}),
});

const GenerationAPIResponse = t.intersection([Required, PartialResultType]);

export type Exclusive<T, U> =
| (T & Partial<Record<Exclude<keyof U, keyof T>, never>>)
| (U & Partial<Record<Exclude<keyof T, keyof U>, never>>);
Expand Down Expand Up @@ -76,23 +61,22 @@ export type Language =
| "";

export type GenerationSimpleOptions = {
provider?: "openai" | "cohere" | "llama" | "";
provider?: "openai" | "cohere" | "llama" | "" | undefined;
model?: string;
stop?: string[];
temperature?: number;
language?: Language;
};

export type ChatOptions = [{ chatId: string }, {}];
export type ChatOptions = [{ chatId: string }];

export type MemoryOptions = [
{ memoryId: string },
{ memory: Memory },
{ data: [LoaderFunction] | LoaderFunction },
{},
];

export type SystemPromptOptions = [{ systemPromptId: UUID }, { systemPrompt: string }, {}];
export type SystemPromptOptions = [{ systemPromptId: UUID }, { systemPrompt: string }];

export type GenerationWithWebOptions = GenerationSimpleOptions &
NeverN<ChatOptions> &
Expand Down Expand Up @@ -287,12 +271,20 @@ function stream(
return resultStream;
}

const GenerateDataType = t.type({
data: t.string,
});

export function generate(
task: string,
options: GenerationOptions,
clientOptions?: InputClientOptions,
): Generation {
return stream(task, options, clientOptions, (data: any, resultStream: Generation) => {
return stream(task, options, clientOptions, (data: unknown, resultStream: Generation) => {
if (!GenerateDataType.is(data)) {
resultStream.emit("error", "Invalid data");
return;
}
if (data.data === "") {
resultStream.push(null);
} else if (data.data.startsWith("[INFOS]:")) {
Expand Down
1 change: 0 additions & 1 deletion lib/hooks/useAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ const useAgent = (

const start = async (
question: string,
// eslint-disable-next-line @typescript-eslint/no-empty-function
progress: (step: string, result: string) => void = () => {},
): Promise<string> => {
console.info("Starting...");
Expand Down
8 changes: 4 additions & 4 deletions lib/hooks/useChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import type { Chat } from "../chats";

export type Message = {
id: string | null;
chat_id: string; // eslint-disable-line
is_user_message: boolean; // eslint-disable-line
content: string; // eslint-disable-line
created_at: string | null; // eslint-disable-line
chat_id: string; // eslint-disable-line camelcase
is_user_message: boolean; // eslint-disable-line camelcase
content: string; // eslint-disable-line camelcase
created_at: string | null; // eslint-disable-line camelcase
};

export default function useChat(): {
Expand Down
12 changes: 9 additions & 3 deletions lib/probabilistic_helpers/generateWithType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import * as t from "polyfact-io-ts";
import { generate, GenerationOptions } from "../generate";
import { InputClientOptions } from "../clientOpts";

// The ts.io types are way too complex for me to write, I didn't want to spend 2 days fixing this so I
// decided to bypass the typechecker and throw an error at runtime if the type is not supported.

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function typePartial2String(entries: [string, any][], indent: number, partial: boolean): string {
const leftpad = Array(2 * (indent + 1))
.fill(" ")
Expand All @@ -22,6 +26,7 @@ function typePartial2String(entries: [string, any][], indent: number, partial: b
);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function internalTsio2String(type: any, indent: number): string {
const leftpad = Array(2 * indent)
.fill(" ")
Expand All @@ -46,6 +51,7 @@ function internalTsio2String(type: any, indent: number): string {
return type.name;
}
if (type._tag === "UnionType") {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return type.types.map((t: any) => internalTsio2String(t, indent + 1)).join(" | ");
}
if (type._tag === "LiteralType") {
Expand All @@ -72,7 +78,7 @@ function internalTsio2String(type: any, indent: number): string {
);
}

function tsio2String(type: t.TypeC<any>): string {
function tsio2String<T extends t.Props>(type: t.TypeC<T>): string {
const res = JSON.parse(JSON.stringify(type));

return internalTsio2String(res, 0);
Expand Down Expand Up @@ -124,9 +130,9 @@ export async function generateWithType<
}

if (!options?.infos) {
return result as any;
return result as unknown as ReturnType<typeof generateWithType<T, O>>;
}
return { result, tokenUsage } as any;
return { result, tokenUsage } as unknown as ReturnType<typeof generateWithType<T, O>>;
}

throw new Error("Generation failed to match the given type after 5 retry");
Expand Down
Loading