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
//! Telemetry init/shutdown + workflow history parsing. Phase R4 Agent C.
//!
//! Feature-gated exporters mirror the uniffi surface one-to-one:
//!
//! - [`blazen_init_langfuse`] (`feature = "langfuse"`)
//! - [`blazen_init_otlp`] (`feature = "otlp"`)
//! - [`blazen_init_prometheus`] (`feature = "prometheus"`)
//!
//! The always-available pieces are [`blazen_shutdown_telemetry`] and
//! [`blazen_parse_workflow_history`] (plus its companion array-free
//! helper).
//!
//! # Ownership conventions
//!
//! - All input strings are BORROWED — caller keeps them alive only for the
//! duration of the cabi call.
//! - On failure, `*out_err` receives a caller-owned `*mut BlazenError`. Free
//! with [`crate::error::blazen_error_free`].
//! - [`blazen_parse_workflow_history`] writes a caller-owned heap array of
//! `*mut BlazenWorkflowHistoryEntry` plus its length. Release the array
//! with [`blazen_workflow_history_entry_array_free`].
use c_char;
use BlazenError as InnerError;
use WorkflowHistoryEntry as InnerWorkflowHistoryEntry;
use crateBlazenError;
use cratecstr_to_str;
use crateBlazenWorkflowHistoryEntry;
// ---------------------------------------------------------------------------
// Shared error-out helpers
// ---------------------------------------------------------------------------
/// Writes `e` to the out-param if non-null and returns `-1`.
///
/// # Safety
///
/// `out_err` must be null OR a valid destination for a single
/// `*mut BlazenError` write.
unsafe
/// Writes a synthesised `Internal` error to the out-param and returns `-1`.
/// Used for null-pointer / UTF-8 input failures where there isn't an
/// originating `InnerError`.
///
/// # Safety
///
/// Same contract as [`write_error`].
unsafe
// ---------------------------------------------------------------------------
// Langfuse
// ---------------------------------------------------------------------------
/// Initialize the Langfuse LLM-observability exporter.
///
/// Spawns a background tokio task that periodically flushes buffered LLM
/// call traces, token usage, and latency data to the Langfuse ingestion
/// API. Call once at process startup, before any traced work.
///
/// Arguments:
/// - `public_key`: Langfuse public API key (HTTP Basic-auth username).
/// - `secret_key`: Langfuse secret API key (HTTP Basic-auth password).
/// - `host`: optional Langfuse host URL; null defaults to
/// `https://cloud.langfuse.com`.
///
/// Returns `0` on success, `-1` on failure (writing the inner error to
/// `*out_err`), or `-2` when `public_key` or `secret_key` is null / not
/// valid UTF-8.
///
/// # Safety
///
/// - `public_key` and `secret_key` must be valid NUL-terminated UTF-8
/// buffers that remain live for the duration of the call.
/// - `host` must be null OR a valid NUL-terminated UTF-8 buffer that
/// remains live for the duration of the call.
/// - `out_err` must be null OR a writable pointer to a `*mut BlazenError`
/// slot.
pub unsafe extern "C"
// ---------------------------------------------------------------------------
// OTLP
// ---------------------------------------------------------------------------
/// Initialize the OpenTelemetry OTLP (gRPC) trace exporter.
///
/// Installs an `opentelemetry-otlp` exporter as the global tracing
/// subscriber.
///
/// Arguments:
/// - `endpoint`: OTLP gRPC endpoint URL (e.g. `"http://localhost:4317"`).
/// - `service_name`: optional service name reported to the backend; null
/// defaults to `"blazen"`.
///
/// Returns `0` on success, `-1` on failure (writing the inner error to
/// `*out_err`), or `-2` when `endpoint` is null / not valid UTF-8.
///
/// # Safety
///
/// - `endpoint` must be a valid NUL-terminated UTF-8 buffer that remains
/// live for the duration of the call.
/// - `service_name` must be null OR a valid NUL-terminated UTF-8 buffer
/// that remains live for the duration of the call.
/// - `out_err` must be null OR a writable pointer to a `*mut BlazenError`
/// slot.
pub unsafe extern "C"
// ---------------------------------------------------------------------------
// Prometheus
// ---------------------------------------------------------------------------
/// Initialize the Prometheus metrics exporter and start its HTTP listener.
///
/// Installs a global `metrics` recorder backed by Prometheus and starts an
/// HTTP server serving the `/metrics` endpoint.
///
/// `listen_address` accepts a `host:port` string (e.g. `"0.0.0.0:9100"`)
/// or a bare port (e.g. `"9100"`). The upstream listener always binds
/// `0.0.0.0`; only the port portion is honored.
///
/// Returns `0` on success, `-1` on failure (writing the inner error to
/// `*out_err`), or `-2` when `listen_address` is null / not valid UTF-8.
///
/// # Safety
///
/// - `listen_address` must be a valid NUL-terminated UTF-8 buffer that
/// remains live for the duration of the call.
/// - `out_err` must be null OR a writable pointer to a `*mut BlazenError`
/// slot.
pub unsafe extern "C"
// ---------------------------------------------------------------------------
// Shutdown
// ---------------------------------------------------------------------------
/// Best-effort flush + shutdown of any initialised telemetry exporters.
///
/// Safe to call even if no exporter was initialised. Currently a no-op on
/// the upstream side — see `blazen_uniffi::telemetry::shutdown_telemetry`
/// for context — but exposed so foreign callers can wire a single
/// "shutdown" hook into their app lifecycle without conditional branching
/// on features.
pub extern "C"
// ---------------------------------------------------------------------------
// Workflow history
// ---------------------------------------------------------------------------
/// Convert a `Vec<WorkflowHistoryEntry>` into a heap-allocated array of
/// `*mut BlazenWorkflowHistoryEntry` plus its length. Mirrors the
/// `checkpoints_to_c_array` helper in `persist.rs`.
/// Decode a JSON-serialised upstream `blazen_telemetry::WorkflowHistory`
/// into a flat array of [`BlazenWorkflowHistoryEntry`] handles.
///
/// The expected input is the exact format produced by
/// `serde_json::to_string(&history)` on a `blazen_telemetry::WorkflowHistory`
/// (i.e. an object with `run_id`, `workflow_name`, and `events`).
///
/// On success returns `0`, writes the array base pointer into `*out_array`,
/// and writes its length into `*out_count`. The array contains a heap
/// pointer per event; release the whole thing with
/// [`blazen_workflow_history_entry_array_free`].
///
/// Returns `-1` on parse failure (writing the inner error to `*out_err`),
/// or `-2` when `history_json` is null or not valid UTF-8.
///
/// When `out_array` is null the freshly built array is freed immediately
/// to avoid a leak — `*out_count` is still populated.
///
/// # Safety
///
/// - `history_json` must be a valid NUL-terminated UTF-8 buffer that
/// remains live for the duration of the call.
/// - `out_array` / `out_count` / `out_err` must be null OR writable
/// pointers to the appropriate slot.
pub unsafe extern "C"
/// Frees an array of `*mut BlazenWorkflowHistoryEntry` previously produced
/// by [`blazen_parse_workflow_history`].
///
/// Releases each entry handle AND the backing slice in one call. No-op on
/// a null `arr` (regardless of `count`).
///
/// # Safety
///
/// `arr` must be null OR a pointer previously produced by
/// [`blazen_parse_workflow_history`], with `count` matching its length.
/// Double-free is undefined behavior; modifying or freeing individual
/// element pointers before this call is also undefined.
pub unsafe extern "C"