forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-copilot-additional-instructions.ts
More file actions
98 lines (92 loc) · 2.49 KB
/
Copy pathuse-copilot-additional-instructions.ts
File metadata and controls
98 lines (92 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* `useCopilotAdditionalInstructions` is a React hook that provides additional instructions
* to the Copilot.
*
* ## Usage
*
* ### Simple Usage
*
* In its most basic usage, useCopilotAdditionalInstructions accepts a single string argument
* representing the instructions to be added to the Copilot.
*
* ```tsx
* import { useCopilotAdditionalInstructions } from "@copilotkit/react-core";
*
* export function MyComponent() {
* useCopilotAdditionalInstructions({
* instructions: "Do not answer questions about the weather.",
* });
* }
* ```
*
* ### Conditional Usage
*
* You can also conditionally add instructions based on the state of your app.
*
* ```tsx
* import { useCopilotAdditionalInstructions } from "@copilotkit/react-core";
*
* export function MyComponent() {
* const [showInstructions, setShowInstructions] = useState(false);
*
* useCopilotAdditionalInstructions({
* available: showInstructions ? "enabled" : "disabled",
* instructions: "Do not answer questions about the weather.",
* });
* }
* ```
*/
import { useEffect } from "react";
import { useCopilotContext } from "../context/copilot-context";
/**
* Options for the useCopilotAdditionalInstructions hook.
*/
export interface UseCopilotAdditionalInstructionsOptions {
/**
* The instructions to be added to the Copilot. Will be added to the instructions like so:
*
* ```txt
* You are a helpful assistant.
* Additionally, follow these instructions:
* - Do not answer questions about the weather.
* - Do not answer questions about the stock market.
* ```
*/
instructions: string;
/**
* Whether the instructions are available to the Copilot.
*/
available?: "enabled" | "disabled";
}
/**
* Adds the given instructions to the Copilot context.
*/
export function useCopilotAdditionalInstructions(
{
instructions,
available = "enabled",
}: UseCopilotAdditionalInstructionsOptions,
dependencies?: any[],
) {
const { setAdditionalInstructions } = useCopilotContext();
useEffect(() => {
if (available === "disabled") return;
setAdditionalInstructions((prevInstructions) => [
...(prevInstructions || []),
instructions,
]);
return () => {
setAdditionalInstructions(
(prevInstructions) =>
prevInstructions?.filter(
(instruction) => instruction !== instructions,
) || [],
);
};
}, [
available,
instructions,
setAdditionalInstructions,
...(dependencies || []),
]);
}