-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCreateReport.tsx
735 lines (694 loc) · 22.5 KB
/
CreateReport.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
"use client";
import React, {
ChangeEvent,
RefObject,
useEffect,
useRef,
useState,
useActionState,
} from "react";
import * as api from "tttc-common/api";
import { Col, Row } from "../layout";
import {
Button,
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
Spinner,
TextArea,
} from "../elements";
import { Input } from "../elements";
import Icons from "@/assets/icons";
import { useCostEstimate } from "./hooks/useCostEstimate";
import { useReactiveValue } from "@/lib/hooks/useReactiveValue";
import { useParseCsv } from "./hooks/useParseCSV";
import { toast } from "sonner";
import {
AlertDialog,
AlertDialogAction,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogCancel,
} from "../elements/alertDialog/AlertDialog";
import Form from "next/form";
import submitAction from "@/features/submission/actions/SubmitAction";
import { z } from "zod";
import { useForm, FormProvider, useFormContext } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { cn } from "@/lib/utils/shadcn";
import * as prompts from "tttc-common/prompts";
import { useUser } from "@/lib/hooks/getUser";
import { useAsyncState } from "@/lib/hooks/useAsyncState";
import { User } from "firebase/auth";
import { useFormStatus } from "react-dom";
import { useRouter } from "next/navigation";
import { signInWithGoogle } from "@/lib/firebase/auth";
import { crux } from "tttc-common/schema";
const fetchToken = async (
user: User | null,
): Promise<["data", string | null] | ["error", string]> => {
try {
if (!user) return ["data", null];
return ["data", await user?.getIdToken()];
} catch (e) {
return ["error", e instanceof Error ? e.message : "An error occured"];
}
};
function getUserToken() {
const user = useUser();
return useAsyncState(() => fetchToken(user), user);
}
const bindTokenToAction = <Input, Output>(
token: string | null,
action: (token: string | null, input: Input) => Promise<Output>,
) => {
return async (_: api.GenerateApiResponse | null, input: Input) => {
return action(token, input);
};
};
const initialState: api.GenerateApiResponse | null = null;
// !!! This is copied from schema.LLMUserConfig. For some reason its resulting in an infinite cycle with useForm. Figure this out later.
const form = z.object({
// apiKey: z.string().optional(),
title: z.string().min(1),
description: z.string().min(1),
systemInstructions: z.string().min(1),
clusteringInstructions: z.string().min(1),
extractionInstructions: z.string().min(1),
dedupInstructions: z.string().min(1),
cruxInstructions: z.string().optional(),
cruxesEnabled: z.boolean().optional(),
});
function Center({ children }: React.PropsWithChildren) {
return (
<div className="w-full h-full content-center justify-items-center">
{children}
</div>
);
}
export default function CreateReport() {
const { isLoading, result } = getUserToken();
if (result === undefined || isLoading)
return (
<Center>
<Spinner />
</Center>
);
else if (result[0] === "error")
return (
<Center>
<p>An error occured...</p>
</Center>
);
else return <CreateReportComponent token={result[1]} />;
}
const SigninModal = ({ isOpen }: { isOpen: boolean }) => {
return (
<Dialog open={isOpen}>
<DialogContent className="gap-10">
<DialogHeader>
<DialogTitle>Login to create a report</DialogTitle>
</DialogHeader>
<DialogDescription>
<Button onClick={signInWithGoogle}>Login</Button>
</DialogDescription>
</DialogContent>
</Dialog>
);
};
const useDeferredValue = <T,>(value: T, delay = 1000): T | "deferred" => {
const [deferredValue, setDeferredValue] = useState<T | "deferred">(
"deferred",
);
useEffect(() => {
// Set up the timer to check the value after the specified delay
const timer = setTimeout(() => {
setDeferredValue(value);
}, delay);
// Clean up the timer if the component unmounts or value changes
return () => clearTimeout(timer);
}, [value, delay]);
return deferredValue;
};
function CreateReportComponent({ token }: { token: string | null }) {
const [modalOpen, setModalOpen] = useState(false);
const submitActionWithToken = bindTokenToAction(token, submitAction);
const [state, formAction] = useActionState(
submitActionWithToken,
initialState,
);
const [files, setFiles] = useState<FileList | undefined>(undefined);
const deferredToken = useDeferredValue(token);
useEffect(() => {
if (deferredToken === "deferred") {
setModalOpen(false);
} else if (deferredToken === null) {
setModalOpen(true);
} else {
setModalOpen(false);
}
}, [deferredToken]);
const methods = useForm<z.infer<typeof form>>({
resolver: zodResolver(form),
mode: "onTouched",
reValidateMode: "onChange",
defaultValues: {
title: "",
description: "",
// apiKey: "placeholder",
systemInstructions: prompts.defaultSystemPrompt,
clusteringInstructions: prompts.defaultClusteringPrompt,
extractionInstructions: prompts.defaultExtractionPrompt,
dedupInstructions: prompts.defaultDedupPrompt,
cruxInstructions: prompts.defaultCruxPrompt,
cruxesEnabled: false,
},
});
const isDisabled = !files?.item(0) || !methods.formState.isValid || !token;
console.log("first,", !files?.item(0));
console.log("second, ", !methods.formState.isValid);
console.log("third", !token);
return (
<FormProvider {...methods}>
<SigninModal isOpen={modalOpen} />
<Form action={formAction}>
<SubmitFormControl response={state}>
<Col gap={8} className="mb-20">
<FormHeader />
<FormDescription />
<FormDataInput files={files} setFiles={setFiles} />
<EnableResearchFeatures />
<CustomizePrompts />
<CostEstimate files={files} />
<div>
<Button size={"sm"} type="submit" disabled={isDisabled}>
Generate the report
</Button>
</div>
<TermsAndConditions />
<br />
</Col>
</SubmitFormControl>
</Form>
</FormProvider>
);
}
const FormHeader = () => (
<Col gap={3}>
<h3>Create a Report</h3>
<div className="text-muted-foreground">
<p>
We send the contents of the data uploaded below through OpenAI’s API to
extract key claims and topics, and store the resuts as a T3C report on
this site. Optionally, you can customize the prompts we use for each
step of the pipeline – e.g. to focus on particular questions,
themes, or perspectives in your data.
</p>
<br />
<p>
Creating a report may take a few minutes, especially for large datasets.
Consider creating a test report with a smaller portion of your dataset
first (10-20 rows).
</p>
<br />
<p>
<strong>For this alpha launch:</strong>
</p>
<ul className="list-disc list-outside pl-6">
<li>
Once you create a report, it is publicly viewable to anyone with the
exact URL (we’re adding password-protected & private reports soon).
</li>
<li>
Dataset uploads are limited to 100KB – but we pay the OpenAI
analysis costs
</li>
<li>
After this alpha phase, we'll support analysis of larger datasets
using your own OpenAI API key
</li>
</ul>
<br />
<p>
Do you have questions, feedback, or interest in working with us directly
on high-impact applications? Reach out at{" "}
<a className="underline" href="mailto:[email protected]">
</a>
</p>
</div>
</Col>
);
const FormDescription = () => {
const { register, formState } = useFormContext();
const { touchedFields, errors } = formState;
return (
<Col gap={4}>
<h4>Report details</h4>
<Col gap={2}>
<Col>
<label htmlFor="title" className="font-medium">
Report title
</label>
<p className="p2 text-muted-foreground">
The report title will be visible at the top of your project
</p>
</Col>
<Input
id="title"
type="text"
placeholder="Type here"
required
className={cn(
touchedFields.title && errors.title && "border-destructive",
)}
{...register("title")}
/>
{touchedFields.title && errors.title && (
<p className="text-destructive text-sm">Add the title</p>
)}
</Col>
<Col gap={2}>
<Col>
<label htmlFor="description" className="font-medium">
General description
</label>
<p className="p2 text-muted-foreground">
Description shows up below the title and doesn’t influence the
contents of the report
</p>
</Col>
<Input
id="description"
type="text"
placeholder="Type here"
required
className={
touchedFields.description &&
errors.description &&
"border-destructive"
}
{...register("description")}
/>
{touchedFields.description && errors.description && (
<p className="text-destructive text-sm">Add the description</p>
)}
</Col>
</Col>
);
};
function PoorlyFormattedModal({
isOpen,
cancelFunc,
proceedFunc,
}: {
isOpen: boolean;
cancelFunc: () => void;
proceedFunc: () => void;
}) {
return (
<AlertDialog open={isOpen}>
<AlertDialogContent className="w-[329px] gap-6">
<AlertDialogHeader>
<AlertDialogTitle>
We detected a poorly formatted csv. Do you want to proceed
</AlertDialogTitle>
<AlertDialogDescription>Lorem ipsum</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel onClick={cancelFunc} asChild>
<Button variant={"outline"}>Cancel</Button>
</AlertDialogCancel>
<AlertDialogAction onClick={proceedFunc} className="bg-destructive">
Proceed
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
}
function FormDataInput({
files,
setFiles,
}: {
files: FileList | undefined;
setFiles: (files: FileList | undefined) => void;
}) {
const inputRef = useRef<HTMLInputElement>(null);
const fileName = useReactiveValue(() => files?.item(0)?.name || "", [files]);
const [modelOpen, setModalOpen] = useState<boolean>(false);
const { result } = useParseCsv(files);
useEffect(() => {
console.log(result);
if (!result) return;
else if (result[0] === "error") {
if (result[1].tag === "Broken file" || result[1].tag === "Size Error") {
const description =
result[1].tag === "Broken file"
? "File is broken or has no data"
: "File is too large - 150kb limit";
toast.error("Error", {
description: description,
position: "top-center",
});
handleReset(inputRef);
} else if (result[1].tag === "Poorly formatted CSV") {
setModalOpen(true);
}
}
}, [result]);
const handleCsvUpload = async (event: ChangeEvent<HTMLInputElement>) => {
const maybeFiles = event.target.files;
if (!maybeFiles) return;
setFiles(maybeFiles);
};
const handleButtonClick = () => inputRef.current?.click();
const handleReset = (ref: RefObject<HTMLInputElement>) => {
if (!ref.current || !ref.current.files) return;
ref.current.value = "";
setFiles(undefined);
};
return (
<>
<PoorlyFormattedModal
isOpen={modelOpen}
cancelFunc={() => {
handleReset(inputRef);
setModalOpen(false);
}}
proceedFunc={() => setModalOpen(false)}
/>
<Col gap={4}>
<h4>Input data (as CSV file)</h4>
<div>
<p className="p2 text-muted-foreground">
Upload your data in .csv format. The file must have the following
columns: “id” (a unique identifier for each comment) and “comment”
(the participant's response). Optionally, include an “interview”
column for participant names; otherwise, participants will be
considered anonymous. CSV data is sent to OpenAI as part of report
generation.
</p>
<br />
<p className="p2 text-muted-foreground">
You can reference a{" "}
<a
className="underline"
target="_blank"
href="https://docs.google.com/spreadsheets/d/15cKedZ-AYPWMJoVFJY6ge9jUEnx1Hu9MHnhQ_E_Z4FA/edit"
>
sample CSV template
</a>{" "}
here to get started.
</p>
<br />
<p className="p2 text-muted-foreground">
<label htmlFor="description" className="font-medium">
Don't want to make your own CSV file?
</label>
<br />
Browse the tabs and select one of our{" "}
<a
className="underline"
target="_blank"
href="https://docs.google.com/spreadsheets/d/15cKedZ-AYPWMJoVFJY6ge9jUEnx1Hu9MHnhQ_E_Z4FA/edit?gid=862995911#gid=862995911"
>
pre-made synthetic datasets
</a>{" "}
to get a feel for how T3C extracts quotes and organizes topics from
general text.
</p>
</div>
<div>
{!(inputRef.current?.files && inputRef.current.files[0]) ? (
<Button
name="csvUpload"
type="button"
onClick={handleButtonClick}
variant={"secondary"}
>
Choose file
</Button>
) : (
<Row gap={3}>
<Button
id="resetData"
name="resetData"
type="reset"
onClick={() => handleReset(inputRef)}
variant={"ghost"}
>
Reset
</Button>
<p className="text-muted-foreground self-center">{fileName}</p>
</Row>
)}
<Input
name="dataInput"
id="dataInput"
type="file"
className="hidden"
onChange={handleCsvUpload}
accept="csv"
required
ref={inputRef}
/>
</div>
</Col>
</>
);
}
const FormOpenAIKey = () => {
const { formState, register } = useFormContext();
const { touchedFields, errors } = formState;
return (
<Col gap={2}>
<label htmlFor="apiKey">
<h4>OpenAI API Key</h4>
</label>
<div>
<p className="p2 text-muted-foreground">
Launching soon: use your own OpenAI key to analyze large datasets. We
will not store your OpenAI API keys, or use them for any purposes
beyond generating this report; API keys are sent through encrypted
channels in our app.
</p>
</div>
<Input
id="apiKey"
type="password"
placeholder="Paste OpenAI API key here"
className={cn(
"sm: w-1/2",
touchedFields.apiKey && errors.apiKey && "border-destructive",
)}
// required
disabled
// {...register("apiKey")}
/>
{touchedFields.apiKey && errors.apiKey && (
<p className="text-destructive text-sm">Add the Key</p>
)}
</Col>
);
};
const EnableResearchFeatures = () => {
const [areCruxesEnabled, setCruxesEnabled] = React.useState(false);
const handleChange = (event) => {
setCruxesEnabled((areCruxesEnabled) => !areCruxesEnabled);
console.log("ARE CRUXES ENABLED? : ", areCruxesEnabled.toString());
};
return (
<Col gap={4}>
<h4>Enable Research Features</h4>
<Col gap={2}>
<Col>
<label htmlFor="title" className="font-medium">
Extract likely crux statements
</label>
<p className="p2 text-muted-foreground">
As an extra processing step, suggest pairs of
perspective-summarizing statements which would best split the
respondents (into agree/disagree sides/groups of about equal size).
</p>
<div style={{ margin: "8px 0" }}>
<input
type="checkbox"
checked={areCruxesEnabled}
onChange={handleChange}
/>
<label style={{ paddingLeft: "8px" }}>
Suggest top crux pairs
<p>Are cruxes enabled? {areCruxesEnabled.toString()}</p>
</label>
</div>
</Col>
</Col>
</Col>
);
};
const CustomizePrompts = () => (
<Col gap={8}>
<Col gap={4}>
<h4>Customize AI prompts</h4>
<p className="p2 text-muted-foreground">
Optionally customize the prompts we use to generate the report, e.g. to
focus on specific questions, topics, or perspectives. Changing these the
prompts will change the resulting report.
</p>
</Col>
<CustomizePromptSection
title="Role prompt for all steps"
subheader="This prompt helps AI understand how to approach generating reports. It is prepended to all the steps of the report generation flow shown below."
inputName="systemInstructions"
/>
<CustomizePromptSection
title="Step 1 – Topics and subtopics prompt"
subheader="In the first step, the AI finds the most frequent topics and subtopics mentioned in the comments and writes short descriptions for each."
inputName="clusteringInstructions"
/>
<CustomizePromptSection
title="Step 2 – Claim extraction prompt"
subheader="In the second step, the AI summarizes each particpant's comments as key claims with supporting quotes from the original text.
It then assigns the claim to the most relevant subtopic in the report. This prompt runs once for each participant's comment"
inputName="extractionInstructions"
/>
<CustomizePromptSection
title="Step 3 – Merging claims prompt"
subheader="In the last step, AI collects very similar or near-duplicate statements under one representative claim"
inputName="dedupInstructions"
/>
<CustomizePromptSection
title="Optional – Suggest crux summary statements of opposing perspectives"
subheader="In this optional research step, AI suggests pairs of 'crux' statements which would best split participants into agree/disagree groups or sides of about equal size"
inputName="cruxInstructions"
/>
</Col>
);
function CustomizePromptSection({
title,
subheader,
inputName,
}: {
title: string;
subheader: string;
inputName: string;
}) {
const { register, formState, getValues, setValue } = useFormContext();
const { touchedFields, errors, defaultValues } = formState;
const showError =
Object.hasOwn(touchedFields, inputName) && Object.hasOwn(errors, inputName);
const changedDefault = getValues(inputName) !== defaultValues![inputName];
return (
<Col gap={3}>
<Col gap={1}>
<p className="font-medium">{title}</p>
<p className="p2 text-muted-foreground">{subheader}</p>
</Col>
<TextArea
id={inputName}
// onChange={(e) => setFormValue(e.target.value)}
// className={`${formValue === defaultValue ? "text-muted-foreground" : ""}`}
className={`${!changedDefault && "text-muted-foreground"} ${showError && "border-destructive"}`}
{...register(inputName)}
required
/>
{showError && <p className="text-destructive text-sm">Add the prompt</p>}
<div>
<Button
variant={"outline"}
disabled={!changedDefault}
onClick={() =>
setValue(inputName, defaultValues![inputName], {
shouldValidate: true,
shouldTouch: true,
})
}
type="button"
>
<Row gap={2} className="items-center">
<Icons.Reset />
Reset to default
</Row>
</Button>
</div>
</Col>
);
}
function CostEstimate({ files }: { files: FileList | undefined }) {
const cost = useCostEstimate(files);
return (
<Col gap={4}>
<h4>Cost</h4>
<Col gap={2} className="p-4 pb-8 border rounded-lg">
<p className="font-medium">{cost}</p>
<p className="text-muted-foreground">
This estimate is based on past reports. Typically, our real cost vary
between by 10-15% up or down. A general guideline is that 1 MB costs
approximately $24, so 0.5 MB would be around $12, and 10 MB about
$120.
</p>
</Col>
</Col>
);
}
function TermsAndConditions() {
return (
<div className="text-muted-foreground">
<h4>Preliminary Terms of Service</h4>
<br />
<p>
By accessing the Talk to the City report creation feature, users comply
with the following terms and conditions:
</p>
<br />
<p>
Data Processing: Upon submission, your data will be transmitted to
OpenAI's API for processing and subsequent storage on our platform.
Users maintain responsibility for the data they submit.
</p>
<br />
<p>Important Disclosures:</p>
<ul className="list-disc list-outside pl-6">
<li>
Exercise appropriate caution when submitting text containing sensitive
or personally identifiable information
</li>
<li>
Generated reports are assigned a unique URL and are publicly
accessible by default
</li>
<li>
Features for private and password-protected reports are in development
</li>
</ul>
</div>
);
}
function FormLoading() {
return (
<Col gap={2} className="w-full h-full items-center justify-center">
<p>Processing your request, you'll be redirected shortly.</p>
<Spinner />
</Col>
);
}
function SubmitFormControl({
children,
response,
}: React.PropsWithChildren<{ response: api.GenerateApiResponse | null }>) {
const { pending } = useFormStatus();
const router = useRouter();
useEffect(() => {
if (response !== null) {
router.push(`/report/${encodeURIComponent(response.jsonUrl)}`);
}
}, [response]);
if (pending) return <FormLoading />;
else return children;
}