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
(** {1 Main Kernel Loop} *)
open Result
open Lwt.Infix
open Protocol_j
module M = Message
exception Restart
(** {2 Prelude} *)
type 'a or_error = ('a, string) result
type json = Yojson.Safe.t
type mime_data = {
mime_type: string;
mime_content: string;
mime_b64: bool;
}
type mime_data_bundle = mime_data list
module Kernel = struct
type exec_action =
| Mime of mime_data_bundle
type exec_status_ok = {
msg: string option;
actions: exec_action list;
}
type completion_status = {
completion_matches: string list;
completion_start: int;
completion_end: int;
}
type is_complete_reply =
| Is_complete
| Is_not_complete of string
type history_request = Protocol_j.history_request
type inspect_request = Protocol_j.inspect_request = {
ir_code: string;
ir_cursor_pos: int;
ir_detail_level: int;
}
type inspect_reply_ok = {
iro_status: string;
iro_found: bool;
iro_data: mime_data_bundle;
}
let mime ?(base64=false) ~ty x =
Mime [{mime_type=ty; mime_content=x; mime_b64=base64}]
let ok ?(actions=[]) msg = {msg; actions}
type t = {
init: unit -> unit Lwt.t;
exec: count:int -> string -> exec_status_ok or_error Lwt.t;
is_complete: string -> is_complete_reply Lwt.t;
language: string;
language_version: int list;
banner: string option;
file_extension: string;
mime_type: string option;
codemirror_mode: string option;
complete: pos:int -> string -> completion_status Lwt.t;
inspect: inspect_request -> inspect_reply_ok or_error Lwt.t;
history: history_request -> string list Lwt.t;
}
let make
?banner
?(file_extension=".txt")
?mime_type
?codemirror_mode
?(init=fun () -> Lwt.return_unit)
?(is_complete=fun _ -> Lwt.return Is_complete)
?(complete=fun ~pos _i ->
Lwt.return {completion_matches=[]; completion_start=pos;completion_end=pos})
?(inspect=fun _ -> Lwt.return (Error "no inspection implemented"))
?(history=fun _ -> Lwt.return [])
~language_version
~language
~exec
() : t =
{ banner; file_extension; mime_type; language; language_version;
is_complete; history; exec; complete; inspect; init; codemirror_mode
}
end
type t = {
sockets: Sockets.t;
key: string option;
kernel: Kernel.t;
mutable e_count: int;
}
let make ?key sockets kernel : t =
{ key; sockets; kernel; e_count=0; }
type iopub_message =
| Iopub_send_message of Message.content
| Iopub_send_mime of mime_data_bundle
let string_of_message content =
let msg_type = M.msg_type_of_content content in
Printf.sprintf "{`%s` content `%s`}"
msg_type (M.json_of_content content)
let string_of_mime_data (m:mime_data): string =
Printf.sprintf "mime{ty=%s; content=%S; base64=%B}"
m.mime_type m.mime_content m.mime_b64
let string_of_mime_data_bundle l : string =
"[" ^ String.concat "\n" (List.map string_of_mime_data l) ^ "]"
let string_of_iopub_message = function
| Iopub_send_message content ->
Printf.sprintf "send_message %s" (string_of_message content)
| Iopub_send_mime l ->
Printf.sprintf "send_mime %s" (string_of_mime_data_bundle l)
let dict_of_mime_bundle (l:mime_data_bundle): json =
let l =
l
|> List.map (fun m ->
let data =
if not m.mime_b64 then m.mime_content
else Base64.encode m.mime_content
in
m.mime_type, `String data)
in
`Assoc l
let mime_message_content (m:mime_data_bundle) : M.content =
Message.Display_data (Protocol_j.({
dd_data = dict_of_mime_bundle m;
dd_metadata = `Assoc [];
dd_transient=None;
}))
let send_shell (t:t) ~parent (content:M.content): unit Lwt.t =
Log.debug (fun k->k "send_shell `%s`" (string_of_message content));
let socket = t.sockets.Sockets.shell in
let msg_type = M.msg_type_of_content content in
let msg' = M.make ~parent ~msg_type content in
M.send ?key:t.key socket msg'
let send_iopub (t:t) ?parent (m:iopub_message): unit Lwt.t =
Log.debug (fun k->k "send_iopub `%s`" (string_of_iopub_message m));
let socket = t.sockets.Sockets.iopub in
let send_message msg_type content =
let msg' = match parent with
| None -> M.make_first ~msg_type content
| Some parent -> M.make ~parent ~msg_type content
in
M.send ?key:t.key socket msg'
in
let send_mime (l:mime_data_bundle) =
let content = mime_message_content l in
let msg_type = M.msg_type_of_content content in
send_message msg_type content
in
begin match m with
| Iopub_send_message content ->
let msg_type = M.msg_type_of_content content in
send_message msg_type content
| Iopub_send_mime l ->
send_mime l
end
let within_status ~parent (t:t) ~f =
send_iopub ~parent t
(Iopub_send_message (M.Status { execution_state = "busy" }))
>>= fun () ->
Lwt.finalize
f
(fun () ->
send_iopub ~parent t
(Iopub_send_message
(M.Status { execution_state = "idle" })))
let execute_request (t:t) ~parent e : unit Lwt.t =
if not e.silent then (
t.e_count <- t.e_count + 1;
);
let execution_count = t.e_count in
send_iopub t ~parent
(Iopub_send_message
(M.Execute_input {
pi_code = e.code;
pi_execution_count = execution_count;
}))
>>= fun () ->
t.kernel.Kernel.exec ~count:execution_count e.code >>= fun status ->
let reply_status_ok (s:string option) = match s with
| None -> Lwt.return_unit
| Some msg ->
send_iopub t ~parent (Iopub_send_message
(M.Execute_result {
po_execution_count = execution_count;
po_data = `Assoc ["text/plain", `String msg];
po_metadata = `Assoc []; }))
>|= fun _ -> ()
and side_action (s:Kernel.exec_action) : unit Lwt.t = match s with
| Kernel.Mime l -> send_iopub t ~parent (Iopub_send_mime l)
in
begin match status with
| Ok ok ->
send_shell t ~parent
(M.Execute_reply {
status = "ok";
execution_count;
ename = None; evalue = None; traceback = None; payload = None;
er_user_expressions = None;
})
>>= fun () ->
reply_status_ok ok.Kernel.msg
>>= fun () ->
Lwt_list.iter_p side_action ok.Kernel.actions
| Error err_msg ->
let content =
M.Execute_reply {
status = "error";
execution_count;
ename = Some "error"; evalue = Some err_msg;
traceback = Some ["<eval>"]; payload = None;
er_user_expressions = None;
}
in
Log.debug (fun k->k "send ERROR `%s`" (M.json_of_content content));
send_shell t ~parent content
>>=fun () ->
send_iopub t ~parent
(Iopub_send_message (M.Execute_error {
err_ename="error";
err_evalue=err_msg;
err_traceback=[
"ERROR " ^ err_msg;
"evaluating " ^ e.code;
];
}))
end
let kernel_info_request (t:t) ~parent =
let str_of_version l = String.concat "." (List.map string_of_int l) in
begin
send_shell t ~parent (M.Kernel_info_reply {
implementation = t.kernel.Kernel.language;
implementation_version =
str_of_version t.kernel.Kernel.language_version;
protocol_version = "5.0";
language_info = {
li_name = t.kernel.Kernel.language;
li_version = str_of_version t.kernel.Kernel.language_version;
li_mimetype=(match t.kernel.Kernel.mime_type with
| Some m -> m
| None -> "text"
);
li_file_extension=t.kernel.Kernel.file_extension;
li_codemirror_mode=t.kernel.Kernel.codemirror_mode;
};
banner= (match t.kernel.Kernel.banner with
| None -> ""
| Some b -> b);
help_links=[];
})
end
let comm_info_request (t:t) ~parent =
send_shell t ~parent M.Comm_info_reply
let shutdown_request (t:t) ~parent (r:shutdown) : 'a Lwt.t =
Log.info (fun k->k "received shutdown request...");
Lwt.catch
(fun () -> send_shell t ~parent (M.Shutdown_reply r))
(fun e ->
Log.err (fun k->k "exn %s when replying to shutdown request" (Printexc.to_string e));
Lwt.return_unit)
>>= fun () ->
Lwt.fail (if r.restart then Restart else Exit)
let handle_invalid_message () =
Lwt.fail (Failure "Invalid message on shell socket")
let byte_pos_of_utf_pos s ~cursor_pos : int =
let dec = Uutf.decoder ~encoding:`UTF_8 (`String s) in
let rec iter n =
if n=0 then Uutf.decoder_byte_count dec
else match Uutf.decode dec with
| `Await -> assert false
| `End -> String.length s + 1
| `Malformed _ ->
iter (n-1)
| `Uchar _ ->
iter (n-1)
in
assert (cursor_pos >= 0);
iter cursor_pos
let utf_pos_of_byte_pos s ~pos : int =
let dec = Uutf.decoder ~encoding:`UTF_8 (`String s) in
let rec iter n =
if Uutf.decoder_byte_count dec >= pos
then Uutf.decoder_count dec
else match Uutf.decode dec with
| `Await -> assert false
| `End -> Uutf.decoder_count dec + 1
| `Malformed _ ->
iter (n-1)
| `Uchar _ ->
iter (n-1)
in
assert (pos >= 0);
iter pos
let complete_request t ~parent (r:complete_request): unit Lwt.t =
begin
let pos = byte_pos_of_utf_pos ~cursor_pos:r.cursor_pos r.line in
t.kernel.Kernel.complete ~pos r.line
end >>= fun st ->
let content = {
matches=st.Kernel.completion_matches;
cursor_start=utf_pos_of_byte_pos r.line ~pos:st.Kernel.completion_start;
cursor_end=utf_pos_of_byte_pos r.line ~pos:st.Kernel.completion_end;
cr_status="ok";
} in
send_shell t ~parent (M.Complete_reply content)
let is_complete_request t ~parent (r:is_complete_request): unit Lwt.t =
t.kernel.Kernel.is_complete r.icr_code
>>= fun st ->
let content = match st with
| Kernel.Is_complete ->
{icr_status="complete"; icr_indent=""}
| Kernel.Is_not_complete icr_indent ->
{icr_status="incomplete"; icr_indent}
in
send_shell t ~parent (M.Is_complete_reply content)
let inspect_request (t:t) ~parent (r:Kernel.inspect_request) =
begin
let pos = byte_pos_of_utf_pos ~cursor_pos:r.ir_cursor_pos r.ir_code in
t.kernel.Kernel.inspect {r with ir_cursor_pos=pos}
end >>= fun res ->
let content = match res with
| Ok r ->
{
ir_status = "ok";
ir_found = Some r.Kernel.iro_found;
ir_data = Some (dict_of_mime_bundle r.Kernel.iro_data);
ir_metadata=None;
ir_ename =None; ir_evalue=None; ir_traceback=None;
}
| Error err_msg ->
{
ir_status = "error";
ir_found=None; ir_data=None; ir_metadata=None;
ir_ename = Some "error";
ir_evalue = Some err_msg;
ir_traceback = Some ["<inspect_request>"];
}
in
send_shell t ~parent (M.Inspect_reply content)
let connect_request _socket _msg = ()
let history_request t ~parent x =
t.kernel.Kernel.history x >>= fun history ->
let content = {history} in
send_shell t ~parent (M.History_reply content)
type run_result =
| Run_stop
| Run_restart
let run (t:t) : run_result Lwt.t =
let () = Sys.catch_break true in
Log.debug (fun k->k "run on sockets...");
let heartbeat =
Sockets.heartbeat t.sockets >|= fun () -> Run_stop
in
send_iopub t
(Iopub_send_message (M.Status { execution_state = "starting" }))
>>= fun () ->
t.kernel.Kernel.init ()
>>= fun () ->
let handle_message () =
let open Sockets in
Lwt.pick
[ M.recv t.sockets.shell;
M.recv t.sockets.control;
]
>>= fun m ->
Log.debug (fun k->k "received message `%s`, content `%s`"
(M.msg_type_of_content m.M.content)
(M.json_of_content m.M.content));
begin match m.M.content with
| M.Kernel_info_request ->
within_status ~parent:m t
~f:(fun () -> kernel_info_request t ~parent:m)
| M.Comm_info_request _r -> comm_info_request t ~parent:m
| M.Execute_request x ->
within_status ~parent:m t
~f:(fun () -> execute_request t ~parent:m x)
| M.Connect_request ->
Log.warn (fun k->k "warning: received deprecated connect_request");
connect_request t m; Lwt.return_unit
| M.Inspect_request x ->
within_status ~parent:m t ~f:(fun () -> inspect_request t ~parent:m x)
| M.Complete_request x ->
within_status ~parent:m t ~f:(fun () -> complete_request t ~parent:m x)
| M.Is_complete_request x ->
within_status ~parent:m t ~f:(fun () -> is_complete_request t ~parent:m x)
| M.History_request x ->
within_status ~parent:m t ~f:(fun () -> history_request t ~parent:m x)
| M.Shutdown_request x -> shutdown_request t ~parent:m x
| M.Connect_reply _ | M.Kernel_info_reply _
| M.Shutdown_reply _ | M.Execute_reply _
| M.Inspect_reply _ | M.Complete_reply _ | M.Is_complete_reply _
| M.History_reply _ | M.Status _ | M.Execute_input _
| M.Execute_result _ | M.Stream _ | M.Display_data _
| M.Execute_error _ | M.Clear _ | M.Comm_info_reply ->
handle_invalid_message ()
| M.Comm_open -> Lwt.return_unit
end
in
let rec run () =
begin
Lwt.catch
(fun () -> handle_message() >|= fun _ -> Ok ())
(function
| Sys.Break ->
Log.debug (fun k->k "Sys.Break");
Lwt.return_ok ()
| Restart ->
Log.info (fun k->k "Restart");
Lwt.return_error Run_restart
| Exit ->
Log.info (fun k->k "Exiting, as requested");
Lwt.return_error Run_stop
| e -> Lwt.fail e)
end >>= function
| Ok () -> run()
| Error e -> Lwt.return e
in
Lwt.pick [run (); heartbeat]