Giter Club home page Giter Club logo

Comments (4)

shuding avatar shuding commented on June 3, 2024

Yes this is something related to Suspense and render(). Thanks for reporting!

from ai.

marcoripa96 avatar marcoripa96 commented on June 3, 2024

Hi! Thanks for the clarification! I just wanted to point out that I am not using the render(). This is also happening using the primitives createStreamableUI and createStreamableValue

from ai.

unstubbable avatar unstubbable commented on June 3, 2024

In the posted example you can work around the mentioned suspense issue (i.e. the whole promise chain is replayed when the fallback is replaced with the children) by skipping the ui.update() step:

   (async () => {
     await sleep(50);
     const textNode = createStreamableValue("");
-    ui.update(<AssistantMessageWithStreamableValue content={textNode.value} />);
+    ui.done(<AssistantMessageWithStreamableValue content={textNode.value} />);
 
     for await (const token of generateTokens(text)) {
       textNode.update(token);
     }
 
     textNode.done();
-    ui.done();
   })();

from ai.

marcoripa96 avatar marcoripa96 commented on June 3, 2024

I'm currently having a hard time working around this issue. I'm currently developing a shopping assistant for a work project. I want to provide some feedback in the UI when some products are added to the cart. Here I post an example of the code:

  completion.onEvent("add_products_to_cart", async (data) => {
    let streamableCart:
      | ReturnType<typeof createStreamableValue<Product>>
      | undefined;

    if (data.state === "START" || !streamableCart) {
      streamableCart = createStreamableValue<Product>();
    } else {
      const { products } = data;

      const product = products[Object.keys(products)[0]][0];

      if (product) {
        ui.update(
          <>
            <EventMessage>
              {Object.keys(products).length} prodotti aggiunti al carrello
            </EventMessage>
            <AddToCart products={streamableCart.value} />
          </>,
        );

        streamableCart.done(product);
      }
    }
  }); 

And for the AddToCart component:

export function AddToCart({
  products,
}: {
  products: StreamableValue<Product>;
}) {
  const { addToCart } = useShoppingCartActions();

  useEffect(() => {
    (async () => {
      let productsToAdd: Product[] = [];
      for await (const product of readStreamableValue(products)) {
        if (product) {
          productsToAdd.push(product);
        }
      }
      if (productsToAdd.length > 0) {
        console.log({ productsToAdd });
        addToCart(productsToAdd);
      }
    })();
  }, [products]);

  return null;
}

The problem I have is that products are added multiple times because the component is mounted multiple times. I haven't found a way to get around this and it's kinda blocking us

from ai.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.