vercel / ai

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

SolidJS Reactive Primitives in useChat Hook #1264

Open Rajaniraiyn opened 2 months ago

Rajaniraiyn commented 2 months ago

Feature Description

The current version of the useChat hook in the SolidJS SDK does not support reactive primitives for the api option. This means that the api option remains static throughout the lifecycle of the hook, unlike in the React version where it can be defined with useState and updated reactively.

To address this, we propose adding support for reactive primitives in the useChat hook for the SolidJS SDK, which would allow the api option to be updated dynamically. This would enable capabilities like:

Use Case

Supporting reactive primitives for the api option in the useChat hook would be beneficial in scenarios where you need to change the AI provider or API based on user interactions or other dynamic conditions. For example:

Additional context

Currently, the SolidJS SDK's useChat hook does not provide the same level of flexibility as the React version when it comes to dynamically updating the api option. This limitation can hinder the development of more advanced and dynamic applications that require runtime changes to the AI provider or API.

By introducing support for reactive primitives in the useChat hook, the SolidJS SDK would align more closely with the React version's capabilities, providing developers with greater flexibility and consistency across different frameworks.

Example Usage

import { createSignal } from 'solid-js';
import { useChat } from 'ai/solid';

const App = () => {
  const [api, setApi] = createSignal('https://example.com/api');

  const { messages, sendMessage } = useChat({ api });

  // Update the API based on user interaction or other logic
  const handleApiChange = (newEnpoint) => {
    setApi(newEndpoint);
  };

  // ...
};

here useChat hook's api option supports both regular string and Accessor<string> types

ian-pascoe commented 4 days ago

My PR also covers this: https://github.com/vercel/ai/pull/2063