Skip to content

Commit

Permalink
chore: fix some types
Browse files Browse the repository at this point in the history
  • Loading branch information
AmAzing129 committed Sep 2, 2024
1 parent 55c88ed commit 814cf9e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-ai-lib",
"version": "0.0.2",
"version": "0.0.3-alpha",
"description": "A React hooks library for building AI-powered apps as simple as possible.",
"type": "module",
"source": "src/index.ts",
Expand Down
20 changes: 12 additions & 8 deletions src/hooks/useAIModel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DeepPartial, Schema } from "@ai-sdk/ui-utils";
import type { DeepPartial } from "@ai-sdk/ui-utils";
import type { LanguageModel } from "ai";
import { useEffect, useMemo } from "react";
import type { ZodTypeDef, Schema as zSchema } from "zod";
import type { z } from "zod";
import { useModelContext } from "../provider";
import {
useGenerateObject,
Expand All @@ -15,7 +15,8 @@ interface Options<OBJECT> extends Prompt {
/**
* The schema of the object that the model should generate. Use 'zod' to declare.
*/
schema?: zSchema<OBJECT, ZodTypeDef, OBJECT> | Schema<OBJECT>;
// biome-ignore lint/suspicious/noExplicitAny:
schema?: z.Schema<OBJECT, z.ZodTypeDef, any>;
/**
* Streams the output or not.
*/
Expand All @@ -24,7 +25,9 @@ interface Options<OBJECT> extends Prompt {
* Do something when AI data is generated.
* Use this callback to get the data during streaming rather than through the final 'data'.
*/
onSuccess?: (data: string | DeepPartial<OBJECT> | OBJECT) => void;
onSuccess?: (
data: OBJECT extends string ? string : DeepPartial<OBJECT> | OBJECT,
) => void;
}

interface UseAIModel<D> {
Expand Down Expand Up @@ -94,6 +97,7 @@ export function useAIModel<D = string>(
...prompt,
},
{
onSuccess: onSuccess as (data: string) => void,
enabled: !!stream && !schema && !emptyInput,
},
);
Expand All @@ -104,7 +108,7 @@ export function useAIModel<D = string>(
isFetching: isObjectFetching,
isError: isObjectError,
error: objectError,
} = useGenerateObject<D>(
} = useGenerateObject<z.infer<typeof schema>>(
{
model,
// biome-ignore lint/style/noNonNullAssertion:
Expand All @@ -121,15 +125,15 @@ export function useAIModel<D = string>(
isFetching: isStreamObjectFetching,
isError: isStreamObjectError,
error: streamObjectError,
} = useStreamObject(
} = useStreamObject<z.infer<typeof schema>>(
{
model,
// biome-ignore lint/style/noNonNullAssertion:
schema: schema!,
...prompt,
},
{
onSuccess,
onSuccess: onSuccess as (data: DeepPartial<D> | D) => void,
enabled: !!stream && !!schema && !emptyInput,
},
);
Expand All @@ -140,7 +144,7 @@ export function useAIModel<D = string>(
// biome-ignore lint: onSuccess usually won't change
useEffect(() => {
if (!data) return;
onSuccess?.(data);
(onSuccess as (data: string | D) => void)?.(data);
}, [data]);

return {
Expand Down
11 changes: 5 additions & 6 deletions src/queries/useStreamObject.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import type { Schema } from "@ai-sdk/ui-utils";
import type { DeepPartial } from "@ai-sdk/ui-utils";
import { useQuery } from "@tanstack/react-query";
import { streamObject } from "ai";
import type { LanguageModel } from "ai";
import type { ZodTypeDef, Schema as zSchema } from "zod";
import type { CallSettings, Prompt, TelemetrySettings } from "../types";

// use some of options
type Options = {
type Options<OBJECT> = {
enabled?: boolean;
// biome-ignore lint/suspicious/noExplicitAny: TODO
onSuccess?: (data: any) => void;
onSuccess?: (data: DeepPartial<OBJECT> | OBJECT) => void;
};

// There are three overloads, use this one currently.
Expand All @@ -24,7 +23,7 @@ The language model to use.
The schema of the object that the model should generate.
*/
// biome-ignore lint/suspicious/noExplicitAny: TODO
schema: zSchema<OBJECT, ZodTypeDef, any> | Schema<OBJECT>;
schema: zSchema<OBJECT, ZodTypeDef, any>;
/**
Optional name of the output that should be generated.
Used by some providers for additional LLM guidance, e.g.
Expand Down Expand Up @@ -64,7 +63,7 @@ Callback that is called when the LLM response and the final object validation ar

export function useStreamObject<OBJECT>(
params: StreamObjectParams<OBJECT>,
options?: Options,
options?: Options<OBJECT>,
) {
const query = useQuery<OBJECT>({
queryKey: ["streamObject", JSON.stringify(params.messages)],
Expand Down

0 comments on commit 814cf9e

Please sign in to comment.