-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
180 lines (174 loc) · 4.79 KB
/
index.tsx
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import React, { ComponentProps, useMemo, useRef, useState } from "react";
import { createRoot } from "react-dom/client";
import { Copilot } from "copilot-react";
import { callCompletion_sse as callCompletion } from "./api/completion_sse";
import { FetchResponseError } from "copilot-js/dist/helpers/fromFetchTextStream";
const App = () => {
const [instruction, setInstruction] = useState<string>(
"Write novel in Japanese.",
);
const outputRef = useRef<HTMLTextAreaElement>(null);
const [textOnly, setTextOnly] = useState<boolean>(true);
const [text, setText] = useState<string>("");
const [apiKey, setApiKey] = useState<string>(
() => localStorage.getItem("apiKey") || "",
);
const [saveApiKey, setSaveApiKey] = useState<boolean>(!!apiKey);
const copilotProps: Pick<
ComponentProps<typeof Copilot>,
"onChange" | "handler" | "errorHandler" | "style" | "onSelectionChange"
> = useMemo(
() => ({
style: {
width: "300px",
minHeight: "100px",
padding: "5px",
border: "1px solid #ccc",
height: "200px",
overflowY: "auto",
scrollBehavior: "smooth",
},
onChange: (value: string) => {
setText(value);
},
onSelectionChange: ({
selectionStart,
selectionEnd,
}: {
selectionStart: number;
selectionEnd: number;
}) => {
if (outputRef.current) {
console.log(selectionStart, selectionEnd, outputRef);
outputRef.current.selectionStart = selectionStart;
outputRef.current.selectionEnd = selectionEnd;
}
},
handler: (params) => {
return callCompletion({
...params,
apiKey,
text: instruction,
});
},
errorHandler: async (e) => {
if (e instanceof FetchResponseError) {
alert(e.data.error?.message);
}
},
}),
[apiKey, instruction],
);
return (
<>
<h1>React Copilot Demo</h1>
<div
style={{
display: "flex",
}}
>
<div style={{}}>
<div style={{}}>Copilot</div>
<Copilot
{...copilotProps}
textOnly={textOnly}
value={text}
placeholder="Write something with support of copilot."
/>
Ctrl+Enter: Start completion <br />
Esc: Close completion
</div>
<div style={{ marginLeft: "20px" }}>
<div style={{}}>Instruction</div>
<textarea
style={{
height: "200px",
width: "200px",
border: "1px solid #ccc",
}}
onChange={(e) => {
setInstruction(e.target.value);
}}
value={instruction}
></textarea>
</div>
<div style={{ marginLeft: "20px" }}>
<div style={{}}>Generated text</div>
<textarea
ref={outputRef}
style={{
height: "200px",
width: "200px",
border: "1px solid #ccc",
}}
onChange={(e) => {
setText(e.target.value);
}}
onPaste={(e) => {
setText(e.currentTarget.value);
}}
onDrop={(e) => {
setText(e.currentTarget.value);
}}
value={text}
></textarea>
</div>
<div style={{ marginLeft: "20px" }}>
<div style={{}}>Options</div>
Text Only
<input
type="checkbox"
onChange={(e) => {
setTextOnly(e.currentTarget.checked);
}}
checked={textOnly}
/>
</div>
</div>
OpenAI API Key
<input
type="password"
onChange={(e) => {
setApiKey(e.currentTarget.value);
if (saveApiKey) {
localStorage.setItem("apiKey", e.currentTarget.value);
}
}}
value={apiKey}
/>
Save API key
<input
type="checkbox"
onChange={(e) => {
setSaveApiKey(e.currentTarget.checked);
if (e.currentTarget.checked) {
localStorage.setItem("apiKey", apiKey);
} else {
localStorage.removeItem("apiKey");
}
}}
checked={saveApiKey}
/>
<button
type="button"
onClick={() => {
setSaveApiKey(false);
setApiKey("");
localStorage.removeItem("apiKey");
}}
>
Clear API key
</button>
<br />
(API key is stored in your browser only and is not sent anywhere except
OpenAI.)
</>
);
};
const app = <App />;
const container = document.getElementById("root");
if (container) {
const root = createRoot(container);
root.render(app);
}