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
(** Contents on SHELL channels *)
type status =
| SHELL_OK [@name "ok"]
| SHELL_ERROR [@name "error"]
| SHELL_ABORT [@name "abort"]
[@@deriving yojson]
(** {2 Execution requests and replies} *)
type exec_request =
{
exec_code : string [@key "code"];
exec_silent : bool [@key "silent"];
exec_store_history : bool [@key "store_history"];
exec_user_expr : Json.t [@key "user_expressions"];
exec_allow_stdin : bool [@key "allow_stdin"] [@default true];
exec_stop_on_error : bool [@key "stop_on_error"] [@default false];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
type exec_reply =
{
exec_count : int [@key "execution_count"];
exec_status : status Json.enum [@key "status"];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
(** {2 Instrospection} *)
type inspect_request =
{
insp_code : string [@key "code"];
insp_pos : int [@key "cursor_pos"];
insp_detail : int [@key "detail_level"] [@default 0];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
type inspect_reply =
{
insp_status : status Json.enum [@key "status"];
insp_found : bool [@key "found"];
insp_data : Json.t [@key "data"] [@default `Null];
insp_metadata : Json.t [@key "metadata"] [@default `Null];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
(** {2 Completion} *)
type complete_request =
{
cmpl_code : string [@key "code"];
cmpl_pos : int [@key "cursor_pos"];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
type complete_reply =
{
cmpl_matches : string list [@key "matches"];
cmpl_start : int [@key "cursor_start"];
cmpl_end : int [@key "cursor_end"];
cmpl_metadata : Json.t [@key "metadata"];
cmpl_status : status Json.enum [@key "status"];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
(** {2 History} *)
type history_request =
{
hist_output : bool [@key "output"];
hist_raw : bool [@key "raw"];
hist_access_type : string [@key "hist_access_type"];
hist_session : int option [@key "session"] [@default None];
hist_start : int option [@key "start"] [@default None];
hist_stop : int option [@key "stop"] [@default None];
hist_n : int [@key "n"];
hist_pattern : string option [@key "pattern"] [@default None];
hist_unique : bool [@key "unique"] [@default false];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
type history_reply =
{
history : (int option * int * string) list;
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
(** {2 Code completeness} *)
type is_complete_request =
{
is_cmpl_code : string [@key "code"];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
type is_complete_reply =
{
is_cmpl_status : string [@key "status"];
is_cmpl_indent : string option [@key "indent"] [@default None];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
(** {2 Connect} *)
type connect_reply =
{
conn_shell_port : int [@key "shell_port"];
conn_iopub_port : int [@key "iopub_port"];
conn_stdin_port : int [@key "stdin_port"];
conn_hb_port : int [@key "hb_port"];
conn_ctrl_port : int [@key "control_port"];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
(** {2 Comm info} *)
type comm_info_request =
{
ci_target : string option [@key "target_name"] [@default None];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
type comm_info_reply =
{
ci_comms : Json.t [@key "comms"];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
(** {2 Kernel information} *)
type language_info =
{
lang_name : string [@key "name"]; (** language name *)
lang_version : string [@key "version"]; (** language version *)
lang_mimetype : string [@key "mimetype"]; (** mimetype *)
lang_file_ext : string [@key "file_extension"]; (** file extension *)
lang_lexer : string option [@key "pygments_lexer"]; (** pygments lexer *)
lang_mode : Json.t [@key "codemirror_mode"]; (** codemirror mode *)
lang_exporter : string option [@key "nbconverter_exporter"];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
let language_info =
{
lang_name = "OCaml";
lang_version = Sys.ocaml_version;
lang_mimetype = "text/x-ocaml";
lang_file_ext = ".ml";
lang_lexer = Some "OCaml";
lang_mode = `String "text/x-ocaml";
lang_exporter = None;
}
type help_link =
{
help_text : string [@key "text"];
help_url : string [@key "url"];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
let help_links =
[
{
help_text = "ocaml-jupyter";
help_url = "https://akabe.github.io/ocaml-jupyter/";
}
]
type kernel_info_reply =
{
kernel_prot_ver : string [@key "protocol_version"]; (** protocol version *)
kernel_impl : string [@key "implementation"];
kernel_impl_ver : string [@key "implementation_version"];
kernel_banner : string [@key "banner"];
kernel_help_links : help_link list [@key "help_links"];
kernel_lang_info : language_info [@key "language_info"];
kernel_lang : string [@key "language"];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
let kernel_info_reply =
let banner =
Format.sprintf
"OCaml version %s / ocaml-jupyter version %s"
Sys.ocaml_version
Version.version
in
{
kernel_prot_ver = Version.protocol_version;
kernel_impl = "ocaml-jupyter";
kernel_impl_ver = Version.version;
kernel_banner = banner;
kernel_help_links = help_links;
kernel_lang = "OCaml";
kernel_lang_info = language_info;
}
(** {2 Kernel shutdown} *)
type shutdown =
{
shutdown_restart : bool [@key "restart"];
} [@@deriving yojson]
[@@yojson.allow_extra_fields]
(** {2 Request} *)
type request =
| SHELL_KERNEL_INFO_REQ [@name "kernel_info_request"]
| SHELL_EXEC_REQ of exec_request [@name "execute_request"]
| SHELL_INSPECT_REQ of inspect_request [@name "inspect_request"]
| SHELL_COMPLETE_REQ of complete_request [@name "complete_request"]
| SHELL_HISTORY_REQ of history_request [@name "history_request"]
| SHELL_IS_COMPLETE_REQ of is_complete_request [@name "is_complete_request"]
| SHELL_CONNECT_REQ [@name "connect_request"]
| SHELL_COMM_INFO_REQ of comm_info_request [@name "comm_info_request"]
| SHELL_SHUTDOWN_REQ of shutdown [@name "shutdown_request"]
| SHELL_COMM_OPEN of Comm.t [@name "comm_open"]
| SHELL_COMM_MSG of Comm.t [@name "comm_msg"]
| SHELL_COMM_CLOSE of Comm.t [@name "comm_close"]
[@@deriving yojson]
(** {2 Reply} *)
type reply =
| SHELL_KERNEL_INFO_REP of kernel_info_reply [@name "kernel_info_reply"]
| SHELL_EXEC_REP of exec_reply [@name "execute_reply"]
| SHELL_INSPECT_REP of inspect_reply [@name "inspect_reply"]
| SHELL_COMPLETE_REP of complete_reply [@name "complete_reply"]
| SHELL_HISTORY_REP of history_reply [@name "history_reply"]
| SHELL_IS_COMPLETE_REP of is_complete_reply [@name "is_complete_reply"]
| SHELL_CONNECT_REP of connect_reply [@name "connect_reply"]
| SHELL_COMM_INFO_REP of comm_info_reply [@name "comm_info_reply"]
| SHELL_SHUTDOWN_REP of shutdown [@name "shutdown_reply"]
[@@deriving yojson]
let execute_reply ~count status =
SHELL_EXEC_REP { exec_count = count; exec_status = status; }