vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
9.48k stars 1.39k forks source link

Add `isLoading` and `stop` functionality to `useObject` hook #2082

Closed coji closed 2 months ago

coji commented 2 months ago

Feature Description

I propose adding two new features to the useObject hook in the Vercel AI SDK:

  1. isLoading: A boolean state that indicates whether the object is currently being generated or updated.
  2. stop: A function that allows users to interrupt the ongoing object generation or update process.

These additions would align the useObject hook more closely with other hooks in the SDK, such as useChat and useCompletion, which already have similar functionalities.

Use Case

Adding isLoading and stop to useObject would significantly improve the user experience in applications using this hook. Here are some specific use cases:

  1. Loading State UI: With isLoading, developers can easily implement loading indicators or disable UI elements while the object is being generated or updated. This provides clear feedback to users about the current state of the operation.

    {isLoading ? <LoadingSpinner /> : <GenerateButton onClick={handleGenerate} />}
  2. Cancellation: The stop function would allow users to cancel long-running or unnecessary object generations, giving them more control over the application's behavior and potentially saving computational resources.

    <button onClick={stop} disabled={!isLoading}>Cancel Generation</button>
  3. Error Handling: isLoading can be used in conjunction with error states to provide more nuanced feedback to users, distinguishing between "loading", "error", and "ready" states.

    {isLoading ? <LoadingSpinner /> : error ? <ErrorMessage /> : <ObjectDisplay />}

These enhancements would make the useObject hook more versatile and user-friendly, aligning it with the high standards of other hooks in the Vercel AI SDK. I believe this addition would be valuable for many developers using the Vercel AI SDK in their projects.

Additional context

No response

lgrammel commented 2 months ago

https://github.com/vercel/ai/pull/2090

lgrammel commented 2 months ago

https://github.com/vercel/ai/pull/2098

coji commented 2 months ago

@lgrammel Thanks for the lightning-fast implementation! Really appreciate it.​​​​​​​​​​​​​​​​