-
Notifications
You must be signed in to change notification settings - Fork 313
Expand file tree
/
Copy pathcode.tsx
More file actions
517 lines (461 loc) · 14.6 KB
/
code.tsx
File metadata and controls
517 lines (461 loc) · 14.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
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
import { useEffect, useState } from 'react';
import { useHotkeys } from 'react-hotkeys-hook';
import { inlineCopilot } from 'codemirror-copilot';
import {
CellType,
CodeCellType,
CodeCellUpdateAttrsType,
CellErrorPayloadType,
CellFormattedPayloadType,
AiGeneratedCellPayloadType,
TsServerDefinitionLocationResponsePayloadType,
TsServerDiagnosticType,
} from '@srcbook/shared';
import { useSettings } from '@/components/use-settings';
import CodeCell from '@srcbook/components/src/components/cells/code';
import { SessionType } from '@/types';
import { CellModeType } from '@srcbook/components/src/types';
import { SessionChannel } from '@/clients/websocket';
import { useCells } from '@srcbook/components/src/components/use-cell';
import { mapCMLocationToTsServer, mapTsServerLocationToCM } from './util';
import { toast } from 'sonner';
import { getFileContent } from '@/lib/server';
import { runCodeiumAiAutocomplete } from '@/lib/ai-autocomplete';
import { tsHover } from '@/components/cells/hover';
import { autocompletion } from '@codemirror/autocomplete';
import { type Diagnostic, linter } from '@codemirror/lint';
import { javascript } from '@codemirror/lang-javascript';
import { getCompletions } from '@/components/cells/get-completions';
import CodeMirror, {
EditorState,
EditorView,
Extension,
KeyBinding,
keymap,
Prec,
} from '@uiw/react-codemirror';
import useTheme from '@srcbook/components/src/components/use-theme';
import { Dialog, DialogContent } from '@srcbook/components/src/components/ui/dialog';
function tsLinter(
cell: CodeCellType,
getTsServerDiagnostics: (id: string) => TsServerDiagnosticType[],
getTsServerSuggestions: (id: string) => TsServerDiagnosticType[],
) {
const semanticDiagnostics = getTsServerDiagnostics(cell.id);
const syntaticDiagnostics = getTsServerSuggestions(cell.id);
const diagnostics = [...syntaticDiagnostics, ...semanticDiagnostics].filter(
isDiagnosticWithLocation,
);
const cm_diagnostics = diagnostics.map((diagnostic) => {
return convertTSDiagnosticToCM(diagnostic, cell.source);
});
return linter(async (): Promise<readonly Diagnostic[]> => {
return cm_diagnostics;
});
}
function tsCategoryToSeverity(
diagnostic: Pick<TsServerDiagnosticType, 'category' | 'code'>,
): Diagnostic['severity'] {
if (diagnostic.code === 7027) {
return 'warning';
}
// force resolve types with fallback
switch (diagnostic.category) {
case 'error':
return 'error';
case 'warning':
return 'warning';
case 'suggestion':
return 'warning';
case 'info':
return 'info';
default:
return 'info';
}
}
function isDiagnosticWithLocation(
diagnostic: TsServerDiagnosticType,
): diagnostic is TsServerDiagnosticType {
return !!(typeof diagnostic.start.line === 'number' && typeof diagnostic.end.line === 'number');
}
function tsDiagnosticMessage(diagnostic: TsServerDiagnosticType): string {
if (typeof diagnostic.text === 'string') {
return diagnostic.text;
}
return JSON.stringify(diagnostic); // Fallback
}
function convertTSDiagnosticToCM(diagnostic: TsServerDiagnosticType, code: string): Diagnostic {
const message = tsDiagnosticMessage(diagnostic);
return {
from: mapTsServerLocationToCM(code, diagnostic.start.line, diagnostic.start.offset),
to: mapTsServerLocationToCM(code, diagnostic.end.line, diagnostic.end.offset),
message: message,
severity: tsCategoryToSeverity(diagnostic),
renderMessage: () => {
const dom = document.createElement('div');
dom.className = 'p-2 space-y-3 border-t max-w-lg max-h-64 text-xs relative';
dom.innerText = message;
return dom;
},
};
}
const DEBOUNCE_DELAY = 500;
type BaseProps = {
session: SessionType;
cell: CodeCellType;
};
type RegularProps = BaseProps & {
readOnly?: false;
channel: SessionChannel;
updateCellOnServer: (cell: CodeCellType, attrs: CodeCellUpdateAttrsType) => void;
onDeleteCell: (cell: CellType) => void;
};
type ReadOnlyProps = BaseProps & { readOnly: true };
type Props = RegularProps | ReadOnlyProps;
export default function ControlledCodeCell(props: Props) {
const { readOnly, cell } = props;
const channel = !readOnly ? props.channel : null;
const { theme, codeTheme } = useTheme();
const [filenameError, _setFilenameError] = useState<string | null>(null);
const [showStdio, setShowStdio] = useState(false);
const [cellMode, setCellMode] = useState<CellModeType>('off');
const [generationType, setGenerationType] = useState<'edit' | 'fix'>('edit');
const [prompt, setPrompt] = useState('');
const [newSource, setNewSource] = useState('');
const [fullscreen, setFullscreen] = useState(false);
const { aiEnabled, codeiumApiKey } = useSettings();
const [isModalOpen, setIsModalOpen] = useState(false);
const [modalContent, setModalContent] = useState('');
useHotkeys(
'mod+enter',
() => {
if (!prompt) return;
if (cellMode !== 'prompting') return;
if (!aiEnabled) return;
generate();
},
{ enableOnFormTags: ['textarea'] },
);
useHotkeys(
'escape',
() => {
if (cellMode === 'prompting') {
setCellMode('off');
setPrompt('');
}
},
{ enableOnFormTags: ['textarea'] },
);
const {
cells,
updateCell: updateCellOnClient,
clearOutput,
getTsServerDiagnostics,
getTsServerSuggestions,
} = useCells();
function setFilenameError(error: string | null) {
_setFilenameError(error);
setTimeout(() => _setFilenameError(null), 3000);
}
useEffect(() => {
if (!channel) {
return;
}
function callback(payload: CellErrorPayloadType) {
if (payload.cellId !== cell.id) {
return;
}
const filenameError = payload.errors.find((e) => e.attribute === 'filename');
if (filenameError) {
setFilenameError(filenameError.message);
}
const formattingError = payload.errors.find((e) => e.attribute === 'formatting');
if (formattingError) {
toast.error(formattingError.message);
setCellMode('off');
}
}
channel.on('cell:error', callback);
return () => channel.off('cell:error', callback);
}, [cell.id, channel]);
useEffect(() => {
if (!channel) {
return;
}
function callback(payload: CellFormattedPayloadType) {
if (payload.cellId === cell.id) {
updateCellOnClient({ ...payload.cell });
setCellMode('off');
}
}
channel.on('cell:formatted', callback);
return () => channel.off('cell:formatted', callback);
}, [cell.id, channel, updateCellOnClient]);
function onUpdateFileName(filename: string) {
if (!channel) {
return;
}
updateCellOnClient({ ...cell, filename });
channel.push('cell:rename', {
cellId: cell.id,
filename,
});
}
useEffect(() => {
if (!channel) {
return;
}
function callback(payload: AiGeneratedCellPayloadType) {
if (payload.cellId !== cell.id) return;
// We move to the "review" stage of the generation process:
setNewSource(payload.output);
setCellMode('reviewing');
}
channel.on('ai:generated', callback);
return () => channel.off('ai:generated', callback);
}, [cell.id, channel]);
const generate = () => {
if (!channel) {
return;
}
setGenerationType('edit');
channel.push('ai:generate', {
cellId: cell.id,
prompt,
});
setCellMode('generating');
};
const aiFixDiagnostics = (diagnostics: string) => {
if (!channel) {
return;
}
setCellMode('fixing');
setGenerationType('fix');
channel.push('ai:fix_diagnostics', {
cellId: cell.id,
diagnostics,
});
};
function runCell() {
if (!channel) {
return false;
}
if (cell.status === 'running') {
return false;
}
setShowStdio(true);
// Update client side only. The server will know it's running from the 'cell:exec' event.
updateCellOnClient({ ...cell, status: 'running' });
clearOutput(cell.id);
// Add artificial delay to allow debounced updates to propagate
// TODO: Handle this in a more robust way
setTimeout(() => {
channel.push('cell:exec', {
cellId: cell.id,
});
}, DEBOUNCE_DELAY + 10);
}
function stopCell() {
if (!channel) {
return;
}
channel.push('cell:stop', { cellId: cell.id });
}
function onRevertDiff() {
setCellMode(generationType === 'edit' ? 'prompting' : 'off');
setNewSource('');
}
function onAcceptDiff() {
if (readOnly) {
return;
}
updateCellOnClient({ ...cell, source: newSource });
props.updateCellOnServer(cell, { source: newSource });
setPrompt('');
setCellMode('off');
}
function formatCell() {
if (!channel) {
return;
}
setCellMode('formatting');
channel.push('cell:format', {
cellId: cell.id,
});
}
async function onGetDefinitionContents(pos: number, cell: CodeCellType): Promise<string> {
return new Promise((resolve, reject) => {
if (!channel) {
return;
}
async function gotoDefCallback({ response }: TsServerDefinitionLocationResponsePayloadType) {
if (!channel) {
return;
}
channel.off('tsserver:cell:definition_location:response', gotoDefCallback);
if (response === null) {
reject(new Error(`Error fetching file content: no response!`));
return;
}
const file_response = await getFileContent(response.file);
if (file_response.result.type === 'cell') {
document
.getElementById(file_response.result.filename)
?.scrollIntoView({ behavior: 'smooth' });
} else {
if (file_response.error) {
reject(new Error(`Error fetching file content: ${file_response.result}`));
} else {
resolve(file_response.result.content);
}
}
}
channel.on('tsserver:cell:definition_location:response', gotoDefCallback);
channel.push('tsserver:cell:definition_location:request', {
cellId: cell.id,
request: { location: mapCMLocationToTsServer(cell.source, pos) },
});
});
}
// The order of these extensions is important.
// We want the errors to be first, so we call tsLinter before tsHover.
const extensions: Array<Extension> = [javascript({ typescript: true })];
if (channel) {
extensions.push(tsHover(cell, channel, theme));
}
extensions.push(tsLinter(cell, getTsServerDiagnostics, getTsServerSuggestions));
if (channel) {
extensions.push(
autocompletion({
override: [(context) => getCompletions(context, cell, channel)],
}),
);
}
extensions.push(
inlineCopilot(async (prefix, suffix) => {
let response;
try {
response = await runCodeiumAiAutocomplete(
codeiumApiKey ?? null,
prefix + suffix,
cell.language,
prefix.length,
cells.filter((c): c is CodeCellType => c.type === 'code' && c.id !== cell.id),
);
} catch (err) {
console.error('Error fetching ai autocomplete suggestion:', err);
return '';
}
const completionItems = response.completionItems ?? [];
const mostLikelyCompletionScore = Math.min(
...completionItems.map((item) => item.completion.score),
);
const mostLikelyCompletion = completionItems.find(
(item) => item.completion.score === mostLikelyCompletionScore,
);
return mostLikelyCompletion?.completionParts[0]?.text ?? '';
}, DEBOUNCE_DELAY),
);
extensions.push(
Prec.highest(
EditorView.domEventHandlers({
click: (e, view) => {
if (!onGetDefinitionContents) {
return;
}
const pos = view.posAtCoords({ x: e.clientX, y: e.clientY });
if (pos && e.altKey) {
onGetDefinitionContents(pos, cell)
.then((result) => {
setModalContent(result);
setIsModalOpen(true);
})
.catch((error) => {
console.error('Error calling onGetDefinitionContents:', error);
toast.error('Error calling goto definition!');
});
}
},
}),
),
);
const keys: Array<KeyBinding> = [];
if (runCell) {
keys.push({
key: 'Mod-Enter',
run: () => {
runCell();
return true;
},
});
}
if (formatCell) {
keys.push({
key: 'Shift-Alt-f',
run: () => {
formatCell();
return true;
},
});
}
extensions.push(Prec.highest(keymap.of(keys)));
if (['generating', 'prompting', 'formatting'].includes(cellMode)) {
extensions.push(EditorView.editable.of(false));
extensions.push(EditorState.readOnly.of(true));
}
if (props.readOnly) {
return <CodeCell readOnly cell={props.cell} session={props.session} codeTheme={codeTheme} />;
} else {
return (
<>
<CodeCell
aiEnabled={aiEnabled}
aiFixDiagnostics={aiFixDiagnostics}
cell={props.cell}
cellMode={cellMode}
filenameError={filenameError}
fullscreen={fullscreen}
generationType={generationType}
newSource={newSource}
onAccept={onAcceptDiff}
onChangeCellModeType={setCellMode}
onChangeFilenameError={setFilenameError}
onChangeFullscreen={setFullscreen}
onChangeGenerationType={setGenerationType}
onChangeNewSource={setNewSource}
onChangePrompt={setPrompt}
onChangeShowStdio={setShowStdio}
onDeleteCell={props.onDeleteCell}
onFormatCell={formatCell}
onGenerate={generate}
onGetDefinitionContents={onGetDefinitionContents}
onRevert={onRevertDiff}
onRunCell={runCell}
onStopCell={stopCell}
onUpdateFileName={onUpdateFileName}
prompt={prompt}
session={props.session}
showStdio={showStdio}
updateCellOnServer={props.updateCellOnServer}
fixDiagnostics={aiFixDiagnostics}
editorExtensions={extensions}
codeTheme={codeTheme}
/>
<Dialog open={isModalOpen} onOpenChange={setIsModalOpen}>
<DialogContent className="w-[80vw] h-[80vh] max-w-none p-0 overflow-scroll">
<CodeMirror
className="overflow-scroll focus-visible:outline-none"
value={modalContent}
theme={codeTheme}
extensions={[
javascript({ typescript: true }),
EditorView.editable.of(false),
EditorState.readOnly.of(true),
]}
/>
</DialogContent>
</Dialog>
</>
);
}
}