# copilot-react

> React component that provides an autocompletion for LLM.

Latest version **1.1.0** (published 2024-01-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install copilot-react
pnpm add copilot-react
yarn add copilot-react
bun add copilot-react
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2024-01-24 |
| First published | 2023-09-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 15.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | norami |
| Keywords | copilot, llm, completion, chatgpt, openai, react |

## Links

- npm: https://www.npmjs.com/package/copilot-react
- Repository: https://github.com/honeysol/copilot-js
- Homepage: https://github.com/honeysol/copilot-js#readme
- Issues: https://github.com/honeysol/copilot-js/issues
- npm.io page: https://npm.io/package/copilot-react

## Dependencies (1)

- [copilot-js](https://npm.io/package/copilot-js.md) ^1.1.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2024-01-24
- 1.0.9 — 2024-01-11
- 1.0.8 — 2024-01-05
- 1.0.7 — 2024-01-05
- 1.0.6 — 2024-01-05
- 1.0.5 — 2023-09-08
- 1.0.4 — 2023-09-04
- 1.0.3 — 2023-09-04
- 1.0.2 — 2023-09-03
- 1.0.1 — 2023-09-03
- 1.0.0 — 2023-09-02

## README

# copilot-react

React component that provides an autocompletion for LLM. 

# Background
LLM has changed people's work and lives significantly. However, it does not always return the correct answer and has limited applications. Copilot UI dramatically expands the applications of LLM by allowing collaboration between LLM and humans. For this, high-quality Copilot components are essential for the development of humans with LLM.

# Features
- Easy-to-use UI specialized for natural language input
- Support for text-only or text-containing HTML
- Provide helpers

# Demo
See [/packages/demo](../demo/README.md)

# Guide for users

## normal state:
| Key | Action |
|-----|--------|
| Ctrl+Enter | Start completion |

## completion state:
| Key | Action |
|-----|--------|
| Esc | Collapse completion |
| Character Key | Collapse completion and insert pressed key |

Unlike Github copilot, you can perform operations such as arrow keys, copy, and paste even during completion.

# Quick Example
See [/packages/demo/src/index.tsx](../demo/src/index.tsx) for details.

```tsx
import { fromFetchSSEStream } from "copilot-js/dist/helpers/fromFetchSSEStream";

export const callCompletion = ({
  text,
  precedingText,
  followingText,
  callback,
  apiKey,
}: {
  text: string;
  precedingText?: string;
  followingText?: string;
  callback: (output: string) => void;
  apiKey: string;
}) => {
  const controller = new AbortController();
  const responsePromise = fetch("https://api.openai.com/v1/completions", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${apiKey}`,
    },
    body: JSON.stringify({
      model: "text-davinci-003",
      stream: true,
      prompt: `#instuction${text}\n#output\n${precedingText}`,
      max_tokens: 2000,
      temperature: 0.5,
      suffix: followingText,
      n: 1,
    }),
    signal: controller.signal,
  });
  return fromFetchSSEStream<{ choices: { text: string }[] }>({
    callback: (data) => {
      callback(data.choices[0].text);
    },
    controller,
    responsePromise,
  });
};

const Component = () => {
  const instruction = "write a novel.";
  const [text, setText] = useState<string>("");
  const copilotProps: Pick<
    ComponentProps<typeof Copilot>,
    "onChange" | "handler" | "errorHandler"
  >() => {
    useMemo(
    () => ({
      style: {
        width: "300px",
        minHeight: "100px",
        padding: "5px",
        border: "1px solid #ccc",
        height: "200px",
        overflowY: "auto",
        scrollBehavior: "smooth",
      },
      onChange: (value: string) => {
        setText(value);
      },
      handler: (params) => {
        return callCompletion({
          ...params,
          apiKey: process.env.OPENAI_API_KEY,
          text: instruction,
        });
      },
      errorHandler: async (e) => {
        if (e instanceof FetchResponseError) {
          alert(e.data.error?.message);
        }
      },
    }),
    [apiKey, instruction],
  );
  return <Copilot textOnly={true} value={text} {...copilotProps} />
}

```

# Guide for developers

## Installation

To install copilot-react, you can use npm:

npm i copilot-react

## Step 1 Create a completion handler.

Use helpers in "./dist/helper/*" or write a handler by yourself.
See samples in files: 

| API sample | Description |
|--------------|-------------|
| [/packages/demo/src/api/completion_sse.ts](../demo/src/api/completion_sse.ts) | API sample for fetch() that responds to server-sent event (can also be used for OpenAI API)|
| [/packages/demo/src/api/completion_text.ts](../demo/src/api/completion_text.ts) | API sample for fetch() that responds to text stream |
| [/packages/demo/src/api/completion_openai.ts](../demo/src/api/completion_openai.ts) | API sample for OpenAI official library |

## Step2 Use React component

See [/packages/demo/src/index.tsx](../demo/src/index.tsx) for details.

# API Reference

## Copilot

Component to handle completion

```tsx
<Copilot
  {...{
    value: string;
    textOnly?: boolean = true;
    onChange?: (value: string) => void;
    delay?: number;
    handler: CompletionHandler;
    errorHandler?: ErrorHandler;
    className?: string;
    style?: CSSProperties;

  }}
/>

type CompletionHandler = (params: {
  precedingText?: string | undefined;
  followingText?: string | undefined;
  callback: (output: string) => void;
}) => StreamState;

type StreamState = {
  abort: () => void;
  promise: Promise<void>;
};

type ErrorHandler = (error: any) => void;

```
### Props

- `handler`: Completion handler.
- `value`: Initial value.
- `textOnly` (optional): If true, only text can be inserted. Default true.
- `onChange` (optional): Callback when the value is changed.
- `delay` (optional): Delay to start automatic completion. If not specified, completion will not start automatically.
- `errorHandler` (optional): Error handler, callback error in CompletionHandler.
- `style` (optional): The style object to apply to the component.
- `className` (optional): The CSS class to apply to the component.

---
_Source: https://npm.io/package/copilot-react · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
