0.0.157 • Published 9 months ago

hydra-ai-backup v0.0.157

Weekly downloads
-
License
ISC
Repository
github
Last release
9 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

9 months ago

0.0.152

9 months ago

0.0.151

9 months ago

0.0.150

9 months ago

0.0.157

9 months ago

0.0.156

9 months ago

0.0.155

9 months ago

0.0.154

9 months ago

0.0.139

10 months ago

0.0.138

10 months ago

0.0.136

10 months ago

0.0.131

10 months ago

0.0.135

10 months ago

0.0.134

10 months ago

0.0.133

10 months ago

0.0.132

10 months ago

0.0.149

9 months ago

0.0.148

9 months ago

0.0.147

10 months ago

0.0.142

10 months ago

0.0.141

10 months ago

0.0.140

10 months ago

0.0.146

10 months ago

0.0.145

10 months ago

0.0.144

10 months ago

0.0.143

10 months ago

0.0.130

10 months ago

0.0.129

10 months ago

0.0.128

10 months ago

0.0.127

10 months ago

0.0.126

10 months ago

0.0.125

10 months ago

0.0.124

10 months ago

0.0.123

10 months ago

0.0.122

10 months ago

0.0.121

10 months ago

0.0.120

10 months ago

0.0.119

10 months ago

0.0.118

10 months ago

0.0.117

10 months ago

0.0.116

10 months ago

0.0.114

10 months ago

0.0.113

10 months ago

0.0.112

10 months ago

0.0.111

10 months ago

0.0.110

10 months ago

0.0.109

10 months ago

0.0.108

10 months ago

0.0.107

10 months ago

0.0.106

10 months ago

0.0.105

10 months ago

0.0.103

10 months ago

0.0.102

10 months ago

0.0.101

11 months ago

0.0.100

11 months ago

0.0.99

11 months ago

0.0.98

11 months ago

0.0.97

11 months ago

0.0.96

11 months ago

0.0.95

11 months ago

0.0.94

11 months ago

0.0.93

11 months ago

0.0.92

11 months ago

0.0.91

11 months ago

0.0.90

11 months ago

0.0.89

11 months ago

0.0.88

11 months ago

0.0.87

11 months ago

0.0.86

11 months ago

0.0.85

11 months ago

0.0.84

11 months ago

0.0.83

11 months ago

0.0.82

11 months ago

0.0.81

11 months ago

0.0.80

11 months ago

0.0.79

11 months ago

0.0.78

11 months ago

0.0.77

11 months ago

0.0.75

11 months ago

0.0.74

11 months ago

0.0.73

11 months ago

0.0.72

11 months ago

0.0.71

11 months ago

0.0.70

11 months ago

0.0.69

11 months ago

0.0.68

11 months ago

0.0.67

11 months ago

0.0.66

11 months ago

0.0.65

11 months ago

0.0.64

11 months ago

0.0.63

11 months ago

0.0.62

11 months ago

0.0.61

11 months ago

0.0.60

11 months ago

0.0.59

11 months ago

0.0.58

11 months ago

0.0.57

11 months ago

0.0.56

11 months ago

0.0.55

11 months ago

0.0.54

11 months ago

0.0.53

11 months ago

0.0.52

11 months ago

0.0.51

11 months ago

0.0.50

11 months ago

0.0.49

11 months ago

0.0.48

11 months ago

0.0.47

11 months ago

0.0.46

11 months ago

0.0.45

11 months ago

0.0.44

11 months ago

0.0.43

11 months ago

0.0.42

11 months ago

0.0.41

11 months ago

0.0.40

11 months ago

0.0.39

12 months ago

0.0.38

12 months ago

0.0.37

12 months ago

0.0.36

12 months ago

0.0.35

12 months ago

0.0.34

12 months ago

0.0.33

12 months ago

0.0.32

12 months ago

0.0.31

12 months ago

0.0.30

12 months ago

0.0.29

12 months ago

0.0.28

12 months ago

0.0.27

12 months ago

0.0.26

12 months ago

0.0.25

12 months ago

0.0.24

12 months ago

0.0.23

12 months ago

0.0.22

12 months ago

0.0.21

12 months ago

0.0.20

12 months ago

0.0.19

12 months ago

0.0.18

12 months ago

0.0.17

12 months ago

0.0.16

12 months ago

0.0.15

12 months ago

0.0.14

12 months ago

0.0.13

12 months ago

0.0.12

12 months ago

0.0.11

12 months ago

0.0.10

12 months ago

0.0.9

12 months ago

0.0.8

12 months ago

0.0.7

12 months ago

0.0.6

12 months ago