package ocaml-ai-sdk

  1. Overview
  2. Docs
OCaml AI SDK - Provider abstraction for AI models

Install

dune-project
 Dependency

Authors

Maintainers

Sources

ocaml-ai-sdk-0.6.1.tbz
sha256=cb3b82428abcda76c02ec92f8103a4db28edf3f42795794a37135e9a3c40af96
sha512=11be8889ee25bee67b2be00553c42a4692bc73e5ce23985e8bb87f8a07e9d8f556b5a63b219fe5fe30366c8b0d4edae494afa80ccfbfcb112f1fa1e458de55bf

doc/src/ocaml-ai-sdk.ai_core/ui_message_chunk.ml.html

Source file ui_message_chunk.ml

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
open Melange_json.Primitives

type t =
  | Start of {
      message_id : string option;
      message_metadata : Yojson.Basic.t option;
    }
  | Finish of {
      finish_reason : Ai_provider.Finish_reason.t option;
      message_metadata : Yojson.Basic.t option;
    }
  | Abort of { reason : string option }
  | Start_step
  | Finish_step
  | Text_start of { id : string }
  | Text_delta of {
      id : string;
      delta : string;
    }
  | Text_end of { id : string }
  | Reasoning_start of {
      id : string;
      provider_metadata : Yojson.Basic.t option;
    }
  | Reasoning_delta of {
      id : string;
      delta : string;
      provider_metadata : Yojson.Basic.t option;
    }
  | Reasoning_end of {
      id : string;
      provider_metadata : Yojson.Basic.t option;
    }
  | Tool_input_start of {
      tool_call_id : string;
      tool_name : string;
    }
  | Tool_input_delta of {
      tool_call_id : string;
      input_text_delta : string;
    }
  | Tool_input_available of {
      tool_call_id : string;
      tool_name : string;
      input : Yojson.Basic.t;
    }
  | Tool_output_available of {
      tool_call_id : string;
      output : Yojson.Basic.t;
      provider_metadata : Yojson.Basic.t option;
    }
  | Tool_output_error of {
      tool_call_id : string;
      error_text : string;
      provider_metadata : Yojson.Basic.t option;
    }
  | Source_url of {
      source_id : string;
      url : string;
      title : string option;
    }
  | File of {
      url : string;
      media_type : string;
    }
  | Message_metadata of { message_metadata : Yojson.Basic.t }
  | Tool_input_error of {
      tool_call_id : string;
      tool_name : string;
      input : Yojson.Basic.t;
      error_text : string;
    }
  | Tool_output_denied of { tool_call_id : string }
  | Tool_approval_request of {
      approval_id : string;
      tool_call_id : string;
    }
  | Source_document of {
      source_id : string;
      media_type : string;
      title : string;
      filename : string option;
    }
  | Error of { error_text : string }
  | Data of {
      data_type : string;
      id : string option;
      data : Yojson.Basic.t;
    }

(* JSON record types for serialization — field order matches wire format *)

type type_only_json = { type_ : string [@json.key "type"] } [@@deriving to_json]

type start_json = {
  type_ : string; [@json.key "type"]
  message_id : string option; [@json.key "messageId"] [@json.option] [@json.drop_default]
  message_metadata : Melange_json.t option; [@json.key "messageMetadata"] [@json.option] [@json.drop_default]
}
[@@deriving to_json]

type finish_json = {
  type_ : string; [@json.key "type"]
  finish_reason : string option; [@json.key "finishReason"] [@json.option] [@json.drop_default]
  message_metadata : Melange_json.t option; [@json.key "messageMetadata"] [@json.option] [@json.drop_default]
}
[@@deriving to_json]

type abort_json = {
  type_ : string; [@json.key "type"]
  reason : string option; [@json.option] [@json.drop_default]
}
[@@deriving to_json]

type id_json = {
  type_ : string; [@json.key "type"]
  id : string;
}
[@@deriving to_json]

type id_delta_json = {
  type_ : string; [@json.key "type"]
  id : string;
  delta : string;
}
[@@deriving to_json]

type reasoning_id_json = {
  type_ : string; [@json.key "type"]
  id : string;
  provider_metadata : Melange_json.t option; [@json.key "providerMetadata"] [@json.option] [@json.drop_default]
}
[@@deriving to_json]

type reasoning_id_delta_json = {
  type_ : string; [@json.key "type"]
  id : string;
  delta : string;
  provider_metadata : Melange_json.t option; [@json.key "providerMetadata"] [@json.option] [@json.drop_default]
}
[@@deriving to_json]

type tool_input_start_json = {
  type_ : string; [@json.key "type"]
  tool_call_id : string; [@json.key "toolCallId"]
  tool_name : string; [@json.key "toolName"]
}
[@@deriving to_json]

type tool_input_delta_json = {
  type_ : string; [@json.key "type"]
  tool_call_id : string; [@json.key "toolCallId"]
  input_text_delta : string; [@json.key "inputTextDelta"]
}
[@@deriving to_json]

type tool_input_available_json = {
  type_ : string; [@json.key "type"]
  tool_call_id : string; [@json.key "toolCallId"]
  tool_name : string; [@json.key "toolName"]
  input : Melange_json.t;
}
[@@deriving to_json]

type tool_output_available_json = {
  type_ : string; [@json.key "type"]
  tool_call_id : string; [@json.key "toolCallId"]
  output : Melange_json.t;
  provider_metadata : Melange_json.t option; [@json.key "providerMetadata"] [@json.option] [@json.drop_default]
}
[@@deriving to_json]

type tool_output_error_json = {
  type_ : string; [@json.key "type"]
  tool_call_id : string; [@json.key "toolCallId"]
  error_text : string; [@json.key "errorText"]
  provider_metadata : Melange_json.t option; [@json.key "providerMetadata"] [@json.option] [@json.drop_default]
}
[@@deriving to_json]

type tool_approval_request_json = {
  type_ : string; [@json.key "type"]
  approval_id : string; [@json.key "approvalId"]
  tool_call_id : string; [@json.key "toolCallId"]
}
[@@deriving to_json]

type source_url_json = {
  type_ : string; [@json.key "type"]
  source_id : string; [@json.key "sourceId"]
  url : string;
  title : string option; [@json.option] [@json.drop_default]
}
[@@deriving to_json]

type file_json = {
  type_ : string; [@json.key "type"]
  url : string;
  media_type : string; [@json.key "mediaType"]
}
[@@deriving to_json]

type message_metadata_json = {
  type_ : string; [@json.key "type"]
  message_metadata : Melange_json.t; [@json.key "messageMetadata"]
}
[@@deriving to_json]

type tool_input_error_json = {
  type_ : string; [@json.key "type"]
  tool_call_id : string; [@json.key "toolCallId"]
  tool_name : string; [@json.key "toolName"]
  input : Melange_json.t;
  error_text : string; [@json.key "errorText"]
}
[@@deriving to_json]

type tool_output_denied_json = {
  type_ : string; [@json.key "type"]
  tool_call_id : string; [@json.key "toolCallId"]
}
[@@deriving to_json]

type source_document_json = {
  type_ : string; [@json.key "type"]
  source_id : string; [@json.key "sourceId"]
  media_type : string; [@json.key "mediaType"]
  title : string;
  filename : string option; [@json.option] [@json.drop_default]
}
[@@deriving to_json]

type error_json = {
  type_ : string; [@json.key "type"]
  error_text : string; [@json.key "errorText"]
}
[@@deriving to_json]

type data_json = {
  type_ : string; [@json.key "type"]
  id : string option; [@json.option] [@json.drop_default]
  data : Melange_json.t;
}
[@@deriving to_json]

let to_json = function
  | Start { message_id; message_metadata } -> start_json_to_json { type_ = "start"; message_id; message_metadata }
  | Finish { finish_reason; message_metadata } ->
    finish_json_to_json
      {
        type_ = "finish";
        finish_reason = Option.map Ai_provider.Finish_reason.to_string finish_reason;
        message_metadata;
      }
  | Abort { reason } -> abort_json_to_json { type_ = "abort"; reason }
  | Start_step -> type_only_json_to_json { type_ = "start-step" }
  | Finish_step -> type_only_json_to_json { type_ = "finish-step" }
  | Text_start { id } -> id_json_to_json { type_ = "text-start"; id }
  | Text_delta { id; delta } -> id_delta_json_to_json { type_ = "text-delta"; id; delta }
  | Text_end { id } -> id_json_to_json { type_ = "text-end"; id }
  | Reasoning_start { id; provider_metadata } ->
    reasoning_id_json_to_json { type_ = "reasoning-start"; id; provider_metadata }
  | Reasoning_delta { id; delta; provider_metadata } ->
    reasoning_id_delta_json_to_json { type_ = "reasoning-delta"; id; delta; provider_metadata }
  | Reasoning_end { id; provider_metadata } ->
    reasoning_id_json_to_json { type_ = "reasoning-end"; id; provider_metadata }
  | Tool_input_start { tool_call_id; tool_name } ->
    tool_input_start_json_to_json { type_ = "tool-input-start"; tool_call_id; tool_name }
  | Tool_input_delta { tool_call_id; input_text_delta } ->
    tool_input_delta_json_to_json { type_ = "tool-input-delta"; tool_call_id; input_text_delta }
  | Tool_input_available { tool_call_id; tool_name; input } ->
    tool_input_available_json_to_json { type_ = "tool-input-available"; tool_call_id; tool_name; input }
  | Tool_output_available { tool_call_id; output; provider_metadata } ->
    tool_output_available_json_to_json { type_ = "tool-output-available"; tool_call_id; output; provider_metadata }
  | Tool_output_error { tool_call_id; error_text; provider_metadata } ->
    tool_output_error_json_to_json { type_ = "tool-output-error"; tool_call_id; error_text; provider_metadata }
  | Source_url { source_id; url; title } -> source_url_json_to_json { type_ = "source-url"; source_id; url; title }
  | File { url; media_type } -> file_json_to_json { type_ = "file"; url; media_type }
  | Message_metadata { message_metadata } ->
    message_metadata_json_to_json { type_ = "message-metadata"; message_metadata }
  | Tool_input_error { tool_call_id; tool_name; input; error_text } ->
    tool_input_error_json_to_json { type_ = "tool-input-error"; tool_call_id; tool_name; input; error_text }
  | Tool_output_denied { tool_call_id } ->
    tool_output_denied_json_to_json { type_ = "tool-output-denied"; tool_call_id }
  | Tool_approval_request { approval_id; tool_call_id } ->
    tool_approval_request_json_to_json { type_ = "tool-approval-request"; approval_id; tool_call_id }
  | Source_document { source_id; media_type; title; filename } ->
    source_document_json_to_json { type_ = "source-document"; source_id; media_type; title; filename }
  | Error { error_text } -> error_json_to_json { type_ = "error"; error_text }
  | Data { data_type; id; data } -> data_json_to_json { type_ = Printf.sprintf "data-%s" data_type; id; data }