-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy pathcomplete-python.ts
More file actions
162 lines (150 loc) · 4.58 KB
/
complete-python.ts
File metadata and controls
162 lines (150 loc) · 4.58 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
import { IOServer, Socket } from './index.js'
import { Session } from '../types.js'
import { z } from 'zod'
import { logger } from '../logger.js'
import { DocumentPersistor } from '../yjs/v2/persistors.js'
import { PythonSuggestion } from '@briefer/types'
import { getDocumentSourceWithBlockStartPos } from '@briefer/editor'
import { getCompletion } from '../python/index.js'
import { getDocId, getYDocForUpdate } from '../yjs/v2/index.js'
import { isAuthorizedForDocument } from '../auth/token.js'
export type PythonCompletionMessage = {
status: 'success'
value: PythonSuggestion
}
const completePython =
(io: IOServer, socket: Socket, { user, userWorkspaces }: Session) =>
async (data: unknown) => {
const parsedData = z
.object({
documentId: z.string(),
blockId: z.string(),
modelId: z.string(),
position: z.number(),
currentWord: z.string().nullable(),
})
.safeParse(data)
if (!parsedData.success) {
socket.emit('python-completion', {
status: 'invalid-payload',
})
return
}
const { blockId, documentId, modelId, position, currentWord } =
parsedData.data
const doc = await isAuthorizedForDocument(documentId, user.id)
const role = doc ? userWorkspaces[doc.workspaceId]?.role : null
if (!doc || !role || role === 'viewer') {
socket.disconnect(true)
return
}
try {
const id = getDocId(documentId, null)
await getYDocForUpdate(
id,
io,
doc.id,
doc.workspaceId,
async (yDoc) => {
const { source, blockStartPos } = getDocumentSourceWithBlockStartPos(
yDoc.ydoc,
blockId
)
const finalPosition = blockStartPos + position
const completion = await getCompletion(
doc.workspaceId,
doc.id,
source,
finalPosition
)
if (completion.content.status === 'abort') {
socket.emit('python-completion', {
status: 'success',
documentId,
blockId,
modelId,
position,
currentWord,
suggestions: [],
})
return
}
if (completion.content.status === 'error') {
logger.error(
{
documentId,
blockId,
ename: completion.content.ename,
evalue: completion.content.evalue,
traceback: completion.content.traceback,
},
'Python completion error'
)
socket.emit('python-completion', {
status: 'unexpected-error',
})
return
}
const suggestions: PythonSuggestion[] = []
const matches = new Set(completion.content.matches)
const metadata = z
.object({
_jupyter_types_experimental: z.array(z.record(z.unknown())),
})
.safeParse(completion.content.metadata)
if (metadata.success) {
const rawSuggestions = metadata.data._jupyter_types_experimental
for (const rawSuggestion of rawSuggestions) {
const parsed = PythonSuggestion.safeParse(rawSuggestion)
if (parsed.success) {
suggestions.push(parsed.data)
matches.delete(parsed.data.text)
} else {
logger.error(
{
documentId,
blockId,
jupyterType: rawSuggestion,
error: parsed.error,
},
'Failed to parse jupyter type'
)
}
}
}
for (const match of matches) {
suggestions.push({
start: completion.content.cursor_start,
end: completion.content.cursor_end,
text: match,
type: 'text',
signature: '',
})
}
socket.emit('python-completion', {
status: 'success',
documentId,
blockId,
modelId,
position,
currentWord,
suggestions,
})
},
new DocumentPersistor(documentId)
)
} catch (err) {
logger.error(
{
documentId,
blockId,
error: err,
},
'Failed to get python completion'
)
socket.emit('python-completion', {
status: 'unexpected-error',
})
}
}
export default completePython