| title | Quickstart: CopilotTextarea |
|---|---|
| description | How to integrate CopilotTextarea into your application |
Install the CopilotKit frontend packagess:
```bash npm npm i @copilotkit/react-core @copilotkit/react-textarea ``` ```bash yarn yarn add @copilotkit/react-core @copilotkit/react-textarea ``` ```bash pnpm pnpm add @copilotkit/react-core @copilotkit/react-textarea ```A CopilotKit must wrap all components which interact with CopilotKit.
For simplicity's sake, it's recommended you wrap a CopilotKit around your entire application:
"use client";
import { CopilotKit } from "@copilotkit/react-core";
import "@copilotkit/react-textarea/styles.css"; // also import this if you want to use the CopilotTextarea component
export default function RootLayout({children}) {
return (
<CopilotKit url="/path_to_copilotkit_endpoint/see_below">
{children}
</CopilotKit>
);
}Setup a backend endpoint (and pass its URL as the url argument to CopilotKit, as above).
You likely want to provide external context to inform CopilotTextarea completions, insertions, and edits.
You can do so through the useMakeCopilotReadable and useMakeCopilotDocumentReadable react hooks.
See their respective documentation for additional details.
In particular, you can specify categories which bind specific context to specific textareas, and not to others.
"use client";
import { useMakeCopilotReadable } from '@copilotkit/react-core';
import { useMakeCopilotDocumentReadable, DocumentPointer } from '@copilotkit/react-document';
// You can pass top-level data to the Copilot engine by calling `useMakeCopilotReadable`.
const employeeContextId = useMakeCopilotReadable(employeeName);
// Pass a parentID to maintain a hierarchical structure.
// Especially useful with child React components, list elements, etc.
useMakeCopilotReadable(workProfile.description(), employeeContextId);
useMakeCopilotReadable(metadata.description(), employeeContextId);
const document: DocumentPointer = {
id: "2",
name: "Travel Pet Peeves",
sourceApplication: "Google Docs",
iconImageUri: "/images/GoogleDocs.svg",
getContents: () => { return "hello document" },
} as DocumentPointer;
// ...then, in a react component:
useMakeCopilotDocumentReadable(document);CopilotTextarea is configured essentially identically to a standard react <textarea />.
The primary difference is the additional autosuggestionsConfig variable, which provides copilot-specific configurations.
See the CopilotTextarea documentation for all parameters.
import { CopilotTextarea } from "@copilotkit/react-textarea";
import { useState } from "react";
export function SomeReactComponent() {
const [text, setText] = useState("");
return (
<>
<CopilotTextarea
className="px-4 py-4"
value={text}
onValueChange={(value: string) => setText(value)}
placeholder="What are your plans for your vacation?"
autosuggestionsConfig={{
textareaPurpose: "Travel notes from the user's previous vacations. Likely written in a colloquial style, but adjust as needed.",
chatApiConfigs: {
suggestionsApiConfig: {
forwardedParams: {
max_tokens: 20,
stop: [".", "?", "!"],
},
},
},
}}
/>
</>
);
}