-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathPromptTab.tsx
More file actions
239 lines (214 loc) · 7.6 KB
/
PromptTab.tsx
File metadata and controls
239 lines (214 loc) · 7.6 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
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
import React, { useCallback, useRef, useState, useMemo } from "react";
import { Info, Pencil } from "lucide-react";
import { StringParam, useQueryParam } from "use-query-params";
import { Button } from "@/ui/button";
import {
PromptVersion,
PromptWithLatestVersion,
PROMPT_TEMPLATE_STRUCTURE,
} from "@/types/prompts";
import Loader from "@/shared/Loader/Loader";
import CodeHighlighter, {
SUPPORTED_LANGUAGE,
} from "@/shared/CodeHighlighter/CodeHighlighter";
import UseThisPromptDialog from "@/v1/pages/PromptPage/PromptTab/UseThisPromptDialog";
import EditPromptVersionDialog from "@/v1/pages/PromptPage/PromptTab/EditPromptVersionDialog";
import CommitHistory from "@/v1/pages/PromptPage/PromptTab/CommitHistory";
import usePromptVersionsById from "@/api/prompts/usePromptVersionsById";
import usePromptVersionById from "@/api/prompts/usePromptVersionById";
import TryInPlaygroundButton from "@/v1/pages/PromptPage/TryInPlaygroundButton";
import ImproveInPlaygroundButton from "@/v1/pages/PromptPage/ImproveInPlaygroundButton";
import ExplainerIcon from "@/shared/ExplainerIcon/ExplainerIcon";
import { EXPLAINER_ID, EXPLAINERS_MAP } from "@/constants/explainers";
import RestoreVersionDialog from "./RestoreVersionDialog";
import ChatPromptView from "./ChatPromptView";
import TextPromptView from "./TextPromptView";
import TagListRenderer from "@/shared/TagListRenderer/TagListRenderer";
import usePromptVersionsUpdateMutation from "@/api/prompts/usePromptVersionsUpdateMutation";
import { usePermissions } from "@/contexts/PermissionsContext";
interface PromptTabInterface {
prompt?: PromptWithLatestVersion;
}
const PromptTab = ({ prompt }: PromptTabInterface) => {
const {
permissions: { canUsePlayground },
} = usePermissions();
const [openUseThisPrompt, setOpenUseThisPrompt] = useState(false);
const [openEditPrompt, setOpenEditPrompt] = useState(false);
const [versionToRestore, setVersionToRestore] =
useState<PromptVersion | null>(null);
const [activeVersionId, setActiveVersionId] = useQueryParam(
"activeVersionId",
StringParam,
);
const editPromptResetKeyRef = useRef(0);
const updateVersionsMutation = usePromptVersionsUpdateMutation();
const { data } = usePromptVersionsById(
{
promptId: prompt?.id || "",
page: 1,
size: 25,
sorting: [{ id: "created_at", desc: true }],
},
{
enabled: !!prompt?.id,
refetchInterval: 30000,
},
);
const versions = data?.content;
const effectiveVersionId = useMemo(() => {
if (activeVersionId && versions?.some((v) => v.id === activeVersionId)) {
return activeVersionId;
}
return prompt?.latest_version?.id ?? versions?.[0]?.id ?? "";
}, [activeVersionId, versions, prompt?.latest_version?.id]);
const { data: activeVersion } = usePromptVersionById(
{
versionId: effectiveVersionId,
},
{
enabled: !!effectiveVersionId,
},
);
const handleOpenEditPrompt = (value: boolean) => {
editPromptResetKeyRef.current = editPromptResetKeyRef.current + 1;
setOpenEditPrompt(value);
};
const handleRestoreVersionClick = (version: PromptVersion) => {
setVersionToRestore(version);
};
const displayText = activeVersion?.template || "";
const versionTags = activeVersion?.tags || [];
const updateVersionTags = useCallback(
(tags: string[]) => {
if (!activeVersion?.id) return;
updateVersionsMutation.mutate({
versionIds: [activeVersion.id],
tags,
mergeTags: false,
});
},
[activeVersion?.id, updateVersionsMutation],
);
const isChatPrompt = useMemo(() => {
return prompt?.template_structure === PROMPT_TEMPLATE_STRUCTURE.CHAT;
}, [prompt?.template_structure]);
if (!prompt) {
return <Loader />;
}
return (
<div className="px-6">
<div className="flex w-full items-center gap-2">
<Button
variant="outline"
size="sm"
onClick={() => setOpenUseThisPrompt(true)}
>
<Info className="mr-1.5 size-3.5" />
Use this prompt
</Button>
{canUsePlayground && (
<TryInPlaygroundButton
prompt={prompt}
activeVersion={activeVersion}
/>
)}
{canUsePlayground && !isChatPrompt && (
<ImproveInPlaygroundButton
prompt={prompt}
activeVersion={activeVersion}
/>
)}
<Button
className="ml-auto"
size="sm"
onClick={() => handleOpenEditPrompt(true)}
>
<Pencil className="mr-1.5 size-3.5" />
Edit prompt
</Button>
</div>
<div className="mt-4 flex gap-6 rounded-md border bg-background p-6">
<div className="flex grow flex-col gap-2">
{isChatPrompt ? (
<ChatPromptView template={displayText} />
) : (
<TextPromptView template={displayText} />
)}
{activeVersion?.metadata && (
<>
<p className="comet-body-s-accented mt-4 text-foreground">
Metadata
</p>
<CodeHighlighter
data={JSON.stringify(activeVersion.metadata, null, 2)}
language={SUPPORTED_LANGUAGE.json}
/>
</>
)}
{activeVersion?.change_description && (
<>
<p className="comet-body-s-accented mt-4 text-foreground">
Commit message
</p>
<div className="comet-body-s flex w-full whitespace-pre-wrap break-all rounded-md bg-primary-foreground p-3">
{activeVersion.change_description}
</div>
</>
)}
<TagListRenderer
tags={versionTags}
onAddTag={(newTag) => updateVersionTags([...versionTags, newTag])}
onDeleteTag={(tag) =>
updateVersionTags(versionTags.filter((t) => t !== tag))
}
align="start"
tooltipText="Version tags list"
placeholderText="New version tag"
addButtonText="Add version tag"
tagType="version tag"
/>
</div>
<div className="w-[380px] shrink-0">
<div className="comet-body-s-accented mb-2 flex items-center gap-1 text-foreground">
Commit history
<ExplainerIcon
{...EXPLAINERS_MAP[EXPLAINER_ID.whats_the_commit_history]}
/>
</div>
<CommitHistory
versions={versions || []}
activeVersionId={effectiveVersionId}
onVersionClick={(version) => setActiveVersionId(version.id)}
onRestoreVersionClick={handleRestoreVersionClick}
latestVersionId={prompt.latest_version?.id}
/>
</div>
</div>
<UseThisPromptDialog
open={openUseThisPrompt}
setOpen={setOpenUseThisPrompt}
promptName={prompt.name}
templateStructure={prompt.template_structure}
/>
<EditPromptVersionDialog
key={editPromptResetKeyRef.current}
open={openEditPrompt}
setOpen={handleOpenEditPrompt}
promptName={prompt.name}
template={activeVersion?.template || ""}
metadata={activeVersion?.metadata}
templateStructure={prompt.template_structure}
type={activeVersion?.type}
onSetActiveVersionId={setActiveVersionId}
/>
<RestoreVersionDialog
open={!!versionToRestore}
setOpen={(v) => setVersionToRestore(v ? versionToRestore : null)}
versionToRestore={versionToRestore}
onSetActiveVersionId={setActiveVersionId}
/>
</div>
);
};
export default PromptTab;