Skip to content

Commit

Permalink
✨ Add usage & email
Browse files Browse the repository at this point in the history
  • Loading branch information
lowczarc committed Aug 17, 2023
1 parent 64d680e commit c80ae8f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import memoryClient, {
MemoryClient,
} from "./memory";
import { splitString, tokenCount } from "./split";
import userClient, { usage, UserClient } from "./user";
import { InputClientOptions } from "./clientOpts";
import kvClient, { get as KVGet, set as KVSet, KVClient } from "./kv";

Expand Down Expand Up @@ -58,13 +59,15 @@ export {
Chat,
Memory,
kv,
usage,
};

type Client = GenerationClient &
GenerationWithTypeClient &
TranscribeClient &
MemoryClient &
ChatClient & { kv: KVClient };
ChatClient &
UserClient & { kv: KVClient };

function client(co: InputClientOptions): Client {
return {
Expand All @@ -73,6 +76,7 @@ function client(co: InputClientOptions): Client {
...transcribeClient(co),
...memoryClient(co),
...chatClient(co),
...userClient(co),
kv: kvClient(co),
};
}
Expand Down Expand Up @@ -245,6 +249,7 @@ type Provider = "github" | "google";
export function usePolyfact({ project, endpoint }: { project: string; endpoint?: string }): {
polyfact: Client | undefined;
login: ((input: { provider: Provider }) => Promise<void>) | undefined;
email?: string;
loading: boolean;
} {
if (typeof window === "undefined") {
Expand All @@ -253,6 +258,7 @@ export function usePolyfact({ project, endpoint }: { project: string; endpoint?:

const react = require("react"); // eslint-disable-line
const [polyfact, setPolyfact] = react.useState();
const [email, setEmail] = react.useState();
const [loading, setLoading] = react.useState(true);
const [login, setLogin] = react.useState();

Expand Down Expand Up @@ -298,6 +304,10 @@ export function usePolyfact({ project, endpoint }: { project: string; endpoint?:
data.session?.refresh_token,
);
}

const { data } = await supabase.auth.getUser(token);

setEmail(data.user?.email);
const p = await Polyfact.endpoint(endpoint || "https://api2.polyfact.com")
.project(project)
.signInWithToken(token);
Expand All @@ -316,5 +326,5 @@ export function usePolyfact({ project, endpoint }: { project: string; endpoint?:
})();
}, []);

return { polyfact, login, loading };
return { polyfact, login, loading, email };
}
55 changes: 55 additions & 0 deletions lib/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import axios, { AxiosError } from "axios";
import * as t from "polyfact-io-ts";
import { InputClientOptions, defaultOptions } from "./clientOpts";
import { ApiError, ErrorData } from "./helpers/error";

const UsageResultType = t.intersection([
t.type({
usage: t.number,
}),
t.partial({
rate_limit: t.union([t.undefined, t.null, t.number]),
}),
]);

export async function usage(
clientOptions: InputClientOptions = {},
): Promise<{ usage: number; rateLimit?: number }> {
const { token, endpoint } = await defaultOptions(clientOptions);

try {
const res = await axios.get(`${endpoint}/usage`, {
headers: {
"Content-Type": "application/json",
"X-Access-Token": token,
},
});

if (!UsageResultType.is(res.data)) {
throw new ApiError({
code: "mismatched_response",
message: "The response from the API does not match the expected format",
});
}

return {
usage: res.data.usage,
rateLimit: res.data.rate_limit ?? undefined,
};
} catch (e: unknown) {
if (e instanceof AxiosError) {
throw new ApiError(e?.response?.data as ErrorData);
}
throw e;
}
}

export type UserClient = {
usage: () => Promise<{ usage: number; rateLimit?: number }>;
};

export default function client(clientOptions: InputClientOptions = {}): UserClient {
return {
usage: () => usage(clientOptions),
};
}

0 comments on commit c80ae8f

Please sign in to comment.