0.0.157 • Published 10 months ago

hydra-ai-backup v0.0.157

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

Hydra AI

A framework for creating context-aware UI in React apps. Register your components, and let Hydra decide when to show them and how to hydrate them with the right props and context.

Getting Started

  1. Install the package

    npm i hydra-ai
  2. Set up your API key environment variable

    In a file called .env.local add your API key for the AI provider you want to use:

    OPENAI_API_KEY=<your openai api key>
    # Or for other providers:
    # ANTHROPIC_API_KEY=<your anthropic api key>
    # COHERE_API_KEY=<your cohere api key>
    # GEMINI_API_KEY=<your gemini api key>
    # GROQ_API_KEY=<your groq api key>
    # MISTRAL_API_KEY=<your mistral api key>
    # OPENROUTER_API_KEY=<your openrouter api key>

    This will be used by the HydraClient to make requests to the chosen AI provider.

  3. Initialize HydraClient and Register Components

    Create a new instance of HydraClient, specifying the model and provider:

    import { HydraClient } from "hydra-ai";
    
    const hydra = new HydraClient("gpt-4o", "openai");

    Use the registerComponent method to create a list of components that Hydra can choose from. The method signature is:

    hydra.registerComponent(
      name,
      description,
      component,
      propsDefinition,
      contextTools
    );
    • name: A unique name for the component.
    • description: A description of the component for Hydra to understand when to use it.
    • component: The actual React component.
    • propsDefinition: An object defining each available prop and its type.
    • contextTools: (optional) An array of functions that Hydra can call to gather extra data (e.g., fetching items from an API) when hydrating the component. Find information on how to define contextTools here.

    Here's an example:

    // hydra-client.ts
    
    import { HydraClient } from "hydra-ai";
    import TodoItemCard from "./components/todo-item";
    import TodoList from "./components/todo-list";
    import AddTodoItemForm from "./components/add-todo-form";
    
    const hydra = new HydraClient("gpt-4o", "openai");
    
    hydra.registerComponent(
      "TodoItem",
      "A card representing a todo item",
      TodoItemCard,
      {
        item: "{id: string; title: string; isDone: boolean}",
      }
    );
    
    hydra.registerComponent(
      "TodoList",
      "A list of todo items",
      TodoList,
      {
        todoItems: "{id: string; title: string; isDone: boolean}[]",
      }
    );
    
    hydra.registerComponent(
      "AddTodoItemForm",
      "A form to add a new todo item",
      AddTodoItemForm,
      {}
    );
    
    export default hydra;
  4. Have Hydra Pick and Hydrate Components Based on Context

    To have Hydra use one of the registered components, you can call generateComponent:

    const { component, message } = await hydra.generateComponent(userMessage);

    Here's an example that fetches a component dynamically and renders it:

    "use client";
    
    import { ReactElement, useEffect, useState } from "react";
    import hydra from "./hydra-client";
    
    export default function Home() {
      const [dynamicComponent, setDynamicComponent] = useState<ReactElement | null>(null);
      const [message, setMessage] = useState<string>("");
    
      const fetchComponent = async (userMessage: string) => {
        const { component, message } = await hydra.generateComponent(userMessage);
        setDynamicComponent(component);
        setMessage(message);
      };
    
      useEffect(() => {
        fetchComponent("Show me my todo list");
      }, []);
    
      return (
        <main className="flex min-h-screen flex-col items-center justify-center">
          {message && <p>{message}</p>}
          {dynamicComponent}
        </main>
      );
    }

    If you've registered a weather-related component, Hydra will choose it based on the message and display it in the app. If you included a contextTool with that weather component, Hydra will handle requesting the correct data before filling in the component.

Report a bug or Request a feature

Make a GitHub issue here.

0.0.153

10 months ago

0.0.152

10 months ago

0.0.151

10 months ago

0.0.150

10 months ago

0.0.157

10 months ago

0.0.156

10 months ago

0.0.155

10 months ago

0.0.154

10 months ago

0.0.139

11 months ago

0.0.138

11 months ago

0.0.136

11 months ago

0.0.131

11 months ago

0.0.135

11 months ago

0.0.134

11 months ago

0.0.133

11 months ago

0.0.132

11 months ago

0.0.149

10 months ago

0.0.148

10 months ago

0.0.147

11 months ago

0.0.142

11 months ago

0.0.141

11 months ago

0.0.140

11 months ago

0.0.146

11 months ago

0.0.145

11 months ago

0.0.144

11 months ago

0.0.143

11 months ago

0.0.130

11 months ago

0.0.129

11 months ago

0.0.128

11 months ago

0.0.127

11 months ago

0.0.126

11 months ago

0.0.125

11 months ago

0.0.124

11 months ago

0.0.123

11 months ago

0.0.122

11 months ago

0.0.121

11 months ago

0.0.120

11 months ago

0.0.119

11 months ago

0.0.118

11 months ago

0.0.117

11 months ago

0.0.116

11 months ago

0.0.114

11 months ago

0.0.113

11 months ago

0.0.112

11 months ago

0.0.111

11 months ago

0.0.110

11 months ago

0.0.109

11 months ago

0.0.108

11 months ago

0.0.107

11 months ago

0.0.106

11 months ago

0.0.105

11 months ago

0.0.103

11 months ago

0.0.102

11 months ago

0.0.101

12 months ago

0.0.100

12 months ago

0.0.99

12 months ago

0.0.98

12 months ago

0.0.97

12 months ago

0.0.96

12 months ago

0.0.95

12 months ago

0.0.94

12 months ago

0.0.93

12 months ago

0.0.92

12 months ago

0.0.91

12 months ago

0.0.90

12 months ago

0.0.89

12 months ago

0.0.88

12 months ago

0.0.87

1 year ago

0.0.86

1 year ago

0.0.85

1 year ago

0.0.84

1 year ago

0.0.83

1 year ago

0.0.82

1 year ago

0.0.81

1 year ago

0.0.80

1 year ago

0.0.79

1 year ago

0.0.78

1 year ago

0.0.77

1 year ago

0.0.75

1 year ago

0.0.74

1 year ago

0.0.73

1 year ago

0.0.72

1 year ago

0.0.71

1 year ago

0.0.70

1 year ago

0.0.69

1 year ago

0.0.68

1 year ago

0.0.67

1 year ago

0.0.66

1 year ago

0.0.65

1 year ago

0.0.64

1 year ago

0.0.63

1 year ago

0.0.62

1 year ago

0.0.61

1 year ago

0.0.60

1 year ago

0.0.59

1 year ago

0.0.58

1 year ago

0.0.57

1 year ago

0.0.56

1 year ago

0.0.55

1 year ago

0.0.54

1 year ago

0.0.53

1 year ago

0.0.52

1 year ago

0.0.51

1 year ago

0.0.50

1 year ago

0.0.49

1 year ago

0.0.48

1 year ago

0.0.47

1 year ago

0.0.46

1 year ago

0.0.45

1 year ago

0.0.44

1 year ago

0.0.43

1 year ago

0.0.42

1 year ago

0.0.41

1 year ago

0.0.40

1 year ago

0.0.39

1 year ago

0.0.38

1 year ago

0.0.37

1 year ago

0.0.36

1 year ago

0.0.35

1 year ago

0.0.34

1 year ago

0.0.33

1 year ago

0.0.32

1 year ago

0.0.31

1 year ago

0.0.30

1 year ago

0.0.29

1 year ago

0.0.28

1 year ago

0.0.27

1 year ago

0.0.26

1 year ago

0.0.25

1 year ago

0.0.24

1 year ago

0.0.23

1 year ago

0.0.22

1 year ago

0.0.21

1 year ago

0.0.20

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago