npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-redux-chatgpt

v0.1.0

Published

Skip writing business logic and let ChatGPT reduce your actions for you!

Downloads

9

Readme

react-redux-chatgpt

See the blog post for more information!

This plugin can pretend to be your React/Redux app's backend by using ChatGPT as a universal reducer. It feeds the AI your store's initial state and, whenever an action is dispatched by your app, it forwards the action to the AI and asks for the next state.

ChatGPT doesn't have an official API yet, so you'll need to connect this client-side library to a local relay server to proxy your requests to OpenAI.

Check out the counter and TodoMVC demos in this repo for usage examples.

API

new ChatGptBackend(initState[, options])

Initializes a new pretend app backend. This corresponds to a single ChatGPT conversation which the instance will manage for you in the background.

  • initState: The initial state of your Redux store. Usually an object. Must be JSON.stringify compatible.
  • options: An optional object with extra parameters:
    • apiBase: The base URL of the relay server to query. Default: http://127.0.0.1:3000
    • description: A description of your app to pass on to ChatGPT to help it understand what's going on and generate better state transitions.
    • errorHandler: A callback function which will be passed any errors resulting from asynchronously reducing dispatched actions. Default: console.error

ChatGptBackend#store

A standard Redux store object, preconfigured and wired up to ChatGPT. Actions dispatched to this store will be reduced asynchronously by the AI. If additional actions are dispatched while one is still being processed, they'll be queued up and resolved first-in first-out. Multiple actions can be processed in one round-trip to the AI (within reason) by dispatching an array of actions (i.e., backend.store.dispatch([action1, action2, action3])). All actions must be JSON.stringify compatible.

async ChatGptBackend#drain()

Returns a Promise that will be resolved when the action-reducing queue is empty.

async ChatGptBackend#synthesizeReducer()

Ask the AI to generate JavaScript implementing a reducer function for your app which can handle the actions and states the AI has observed so far. Returns a Promise that will be resolved with the AI's response, in Markdown format.

async ChatGptBackend#synthesizeActions(description)

Ask the AI to generate one or more actions from a natural language request, based on the actions it has observed so far and the current state of the store. Returns a Promise that will be resolved with the AI's response as an array of actions.

  • description: A string describing what the user wants to happen (e.g., "mark all todos as complete").

async ChatGptBackend#query(prompt)

Send a custom prompt to the AI. Returns a Promise resolving to a string containing the AI's response. Note, to keep the AI on track in the central action-reducing loop, this will be appended to a separate fork of the underlying conversation. As a result, the AI won't remember previous prompts passed to query when responding to future prompts.

  • prompt: A string prompt to send to ChatGPT.

isThinking(state)

Given the current state of a ChatGptBackend#store, returns a bool indicating whether the backend is currently asynchronously reducing an action with the AI. This is stored in a hidden Symbol field of the state object, which this helper function can read.

ThinkingScreen

A stock React component for displaying a "thinking..." overlay whenever the AI is asynchronously reducing an action. To work around a limitation in React Contexts, this component must be passed a useSelector function which accesses the ChatGptBackend#store:

import { Provider, useSelector } from 'react-redux';
import { ChatGptBackend, ThinkingScreen } from 'react-redux-chatgpt';

const backend = new ChatGptBackend({ count: 0 });

<Provider store={backend.store}>
  <ThinkingScreen useSelector={useSelector} />
</Provider>

AiButtons

A stock React component implementing a floating bottom bar with buttons for experimenting with the AI by synthesizing reducer code, synthesizing actions, dispatching custom actions, and prompting ChatGPT with custom queries. Must be passed a reference to the ChatGptBackend as a prop:

import { AiButtons, ChatGptBackend } from 'react-redux-chatgpt';

const backend = new ChatGptBackend({ count: 0 });

<AiButtons backend={backend} />

Development

pnpm is the recommended package manager. Available scripts:

  • pnpm run fmt:check - Check code style.
  • pnpm run fmt:write - Auto-format all code.

License

Copyright (C) 2022 Michael Smith <[email protected]> (https://spinda.net)

This program is free software: you can redistribute it and/or modify it under the terms of the Mozilla Public License, version 2.0.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Mozilla Public License for more details.

You should have received a copy of the Mozilla Public License along with this program. If not, see https://www.mozilla.org/en-US/MPL/2.0/.

Helpful resources:

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this work by you shall be licensed as above, without any additional terms or conditions.