UNPKG

@convex-dev/agent

Version:

A agent component for Convex.

98 lines 4.84 kB
import { type ErrorMessage } from "convex-helpers"; import { type UsePaginatedQueryResult } from "convex/react"; import type { UIMessage } from "./toUIMessages.js"; import { toUIMessages } from "./toUIMessages.js"; import type { ThreadQuery, ThreadStreamQuery, ThreadMessagesArgs, ThreadMessagesResult } from "./types.js"; export { optimisticallySendMessage } from "./optimisticallySendMessage.js"; export { useSmoothText } from "./useSmoothText.js"; export { SmoothText } from "./SmoothText.js"; export { toUIMessages, type UIMessage, type ThreadQuery }; /** * A hook that fetches messages from a thread. * * This hook is a wrapper around `usePaginatedQuery` and `useStreamingThreadMessages`. * It will fetch both full messages and streaming messages, and merge them together. * * The query must take as arguments `{ threadId, paginationOpts }` and return a * pagination result of objects that extend `MessageDoc`. * * For streaming, it should look like this: * ```ts * export const listThreadMessages = query({ * args: { * threadId: v.string(), * paginationOpts: paginationOptsValidator, * streamArgs: vStreamArgs, * ... other arguments you want * }, * handler: async (ctx, { threadId, paginationOpts, streamArgs }) => { * // await authorizeThreadAccess(ctx, threadId); * const paginated = await agent.listMessages(ctx, { threadId, paginationOpts }); * const streams = await agent.syncStreams(ctx, { threadId, streamArgs }); * // Here you could filter out / modify the documents & stream deltas. * return { ...paginated, streams }; * }, * }); * ``` * * Then the hook can be used like this: * ```ts * const messages = useThreadMessages( * api.myModule.listThreadMessages, * { threadId }, * { initialNumItems: 10, stream: true } * ); * ``` * * @param query The query to use to fetch messages. * It must take as arguments `{ threadId, paginationOpts }` and return a * pagination result of objects that extend `MessageDoc`. * To support streaming, it must also take in `streamArgs: vStreamArgs` and * return a `streams` object returned from `agent.syncStreams`. * @param args The arguments to pass to the query other than `paginationOpts` * and `streamArgs`. So `{ threadId }` at minimum, plus any other arguments that * you want to pass to the query. * @param options The options for the query. Similar to usePaginatedQuery. * To enable streaming, pass `stream: true`. * @returns The messages. If stream is true, it will return a list of messages * that includes both full messages and streaming messages. */ export declare function useThreadMessages<Query extends ThreadQuery<any, any>>(query: Query, args: ThreadMessagesArgs<Query> | "skip", options: { initialNumItems: number; stream?: Query extends ThreadStreamQuery ? boolean : ErrorMessage<"To enable streaming, your query must take in streamArgs: vStreamArgs and return a streams object returned from agent.syncStreams. See docs.">; }): UsePaginatedQueryResult<ThreadMessagesResult<Query> & { streaming?: boolean; }>; /** * A hook that fetches streaming messages from a thread. * This ONLY returns streaming messages. To get both, use `useThreadMessages`. * * @param query The query to use to fetch messages. * It must take as arguments `{ threadId, paginationOpts, streamArgs }` and * return a `streams` object returned from `agent.syncStreams`. * @param args The arguments to pass to the query other than `paginationOpts` * and `streamArgs`. So `{ threadId }` at minimum, plus any other arguments that * you want to pass to the query. * @returns The streaming messages. */ export declare function useStreamingThreadMessages<Query extends ThreadStreamQuery<any, any>>(query: Query, args: (ThreadMessagesArgs<Query> & { startOrder?: number; }) | "skip"): Array<ThreadMessagesResult<Query>> | undefined; /** * @deprecated use useThreadMessages or useStreamingThreadMessages instead * Use this hook to stream text from a server action, using the * toTextStreamResponse or equivalent HTTP streaming endpoint returning text. * @param url The URL of the server action to stream text from. * e.g. https://....convex.site/yourendpoint * @param threadId The ID of the thread to stream text from. * @param token The auth token to use for the request. * e.g. useAuthToken() from @convex-dev/auth/react * @returns A tuple containing the {text, loading, error} and a function to call the endpoint * with a given prompt, passing up { prompt, threadId } as the body in JSON. */ export declare function useStreamingText(url: string, threadId: string | null, token?: string): readonly [{ readonly text: string; readonly loading: boolean; readonly error: Error | null; }, (prompt: string) => Promise<void>]; //# sourceMappingURL=index.d.ts.map