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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! Workflow-related record marshalling. Opaque handles wrap
//! `blazen_uniffi::workflow::{Event, StepOutput, WorkflowResult}` records
//! with constructor + accessor + free entry points.
//!
//! ## Ownership conventions
//!
//! - Every `*_new*` returns a caller-owned pointer that must be released
//! with the matching `*_free`. Returning null signals an allocation /
//! validation failure (typically a null input pointer or non-UTF-8
//! string content).
//! - String accessors (`*_event_type`, `*_data_json`) return
//! caller-owned heap C strings. Free with
//! [`crate::string::blazen_string_free`].
//! - Numeric accessors return by value.
//! - Vec / enum accessors that produce sub-records (e.g.
//! [`blazen_workflow_result_event`], [`blazen_step_output_single_event`],
//! [`blazen_step_output_multiple_get`]) **clone** the underlying value
//! into a fresh handle the caller owns.
//! - `*_push` adders (e.g. [`blazen_step_output_multiple_push`]) **take
//! ownership** of the pushed handle. Callers MUST NOT separately free
//! the input handle after a successful push; the record now owns it.
//!
//! The cabi surface is built on top of `blazen_uniffi`'s `uniffi::Record`
//! types, which are plain-data Rust structs/enums — there's no shared
//! state to thread through and every operation here is a copy / move
//! against a single owning Box.
// `into_ptr` (and the inner-struct constructors below) are crate-private
// helpers used by Phase R3+ wrappers that surface real `Event` /
// `WorkflowResult` values out of fallible cabi entry points. Their public
// extern siblings are exported via `#[unsafe(no_mangle)]` which keeps the
// linker happy regardless of dead-code warnings.
use c_char;
use ;
use crate;
// ---------------------------------------------------------------------------
// Tag constants for the `StepOutput` enum.
// ---------------------------------------------------------------------------
/// `StepOutput` variant tag for the `None` case — the step performed work
/// but produced no event.
pub const BLAZEN_STEP_OUTPUT_NONE: u32 = 0;
/// `StepOutput` variant tag for the `Single` case — the step produced
/// exactly one event.
pub const BLAZEN_STEP_OUTPUT_SINGLE: u32 = 1;
/// `StepOutput` variant tag for the `Multiple` case — the step produced
/// zero, one, or many events (fan-out).
pub const BLAZEN_STEP_OUTPUT_MULTIPLE: u32 = 2;
// ---------------------------------------------------------------------------
// BlazenEvent
// ---------------------------------------------------------------------------
/// Opaque handle wrapping a `blazen_uniffi::workflow::Event` value.
///
/// Deliberately not `#[repr(C)]` — cbindgen emits this as a forward-
/// declared opaque struct on the C side.
InnerEvent);
/// Constructs a new event with the given `event_type` (e.g. `"StartEvent"`)
/// and JSON-encoded `data_json` payload. Returns null if either pointer is
/// null or refers to non-UTF-8 bytes.
///
/// # Ownership
///
/// Returned pointer is caller-owned. Free with [`blazen_event_free`].
///
/// # Safety
///
/// Both pointers must be null OR valid NUL-terminated UTF-8 buffers that
/// remain live for the duration of the call.
pub unsafe extern "C"
/// Returns the event's `event_type` field as a heap-allocated NUL-terminated
/// UTF-8 C string. Returns null if `event` is null.
///
/// # Ownership
///
/// Caller frees with [`crate::string::blazen_string_free`].
///
/// # Safety
///
/// `event` must be null OR a valid pointer to a `BlazenEvent` previously
/// produced by the cabi surface.
pub unsafe extern "C"
/// Returns the event's `data_json` field as a heap-allocated NUL-terminated
/// UTF-8 C string. Returns null if `event` is null.
///
/// # Ownership
///
/// Caller frees with [`crate::string::blazen_string_free`].
///
/// # Safety
///
/// `event` must be null OR a valid pointer to a `BlazenEvent` previously
/// produced by the cabi surface.
pub unsafe extern "C"
/// Frees a `BlazenEvent` handle previously produced by the cabi surface.
/// No-op on a null pointer.
///
/// # Safety
///
/// `event` must be null OR a pointer previously produced by
/// [`blazen_event_new`] (or any other cabi function documenting
/// `BlazenEvent` ownership-transfer-to-caller semantics). Calling this
/// twice on the same non-null pointer is a double-free.
pub unsafe extern "C"
// ---------------------------------------------------------------------------
// BlazenWorkflowResult
// ---------------------------------------------------------------------------
/// Opaque handle wrapping a `blazen_uniffi::workflow::WorkflowResult`.
///
/// Workflow results are output-only — produced by Rust at the end of a
/// run — so no constructor is exposed across the FFI. The crate-private
/// `into_ptr` helper is the only way to mint one (used by Phase R3+
/// `blazen_workflow_run` wrappers).
InnerWorkflowResult);
/// Returns a freshly-cloned `BlazenEvent` handle holding the terminal event
/// of this workflow run (typically a `"StopEvent"`). Returns null if
/// `result` is null.
///
/// # Ownership
///
/// Returned pointer is caller-owned. Free with [`blazen_event_free`].
///
/// # Safety
///
/// `result` must be null OR a valid pointer to a `BlazenWorkflowResult`
/// previously produced by the cabi surface.
pub unsafe extern "C"
/// Returns the total LLM input-token count accumulated across the run.
/// Returns `0` if `result` is null.
///
/// # Safety
///
/// `result` must be null OR a valid pointer to a `BlazenWorkflowResult`
/// previously produced by the cabi surface.
pub unsafe extern "C"
/// Returns the total LLM output-token count accumulated across the run.
/// Returns `0` if `result` is null.
///
/// # Safety
///
/// `result` must be null OR a valid pointer to a `BlazenWorkflowResult`
/// previously produced by the cabi surface.
pub unsafe extern "C"
/// Returns the total USD cost accumulated across the run. Returns `0.0` if
/// `result` is null or if no pricing data was available for the providers
/// involved.
///
/// # Safety
///
/// `result` must be null OR a valid pointer to a `BlazenWorkflowResult`
/// previously produced by the cabi surface.
pub unsafe extern "C"
/// Frees a `BlazenWorkflowResult` handle previously produced by the cabi
/// surface. No-op on a null pointer.
///
/// # Safety
///
/// `result` must be null OR a pointer previously produced by the cabi
/// surface as a `BlazenWorkflowResult`. Calling this twice on the same
/// non-null pointer is a double-free.
pub unsafe extern "C"
// ---------------------------------------------------------------------------
// BlazenStepOutput
// ---------------------------------------------------------------------------
/// Opaque handle wrapping a `blazen_uniffi::workflow::StepOutput` enum.
///
/// `StepOutput` is what a foreign step handler returns to the Rust
/// workflow engine, so this surface needs both constructors (for foreign
/// step handlers being invoked from Rust via Phase R5 trampolines) and
/// readers (for Rust producing values consumed by foreign code).
///
/// Variant discrimination goes through [`blazen_step_output_kind`] which
/// returns one of the `BLAZEN_STEP_OUTPUT_*` constants.
InnerStepOutput);
/// Constructs a `StepOutput::None` value — the step performed work but
/// produced no event.
///
/// # Ownership
///
/// Returned pointer is caller-owned. Free with [`blazen_step_output_free`].
pub extern "C"
/// Constructs a `StepOutput::Single { event }` value. Consumes ownership
/// of `event` — callers MUST NOT separately free it after this call
/// returns a non-null handle.
///
/// Returns null if `event` is null. On the null-input path the function is
/// a no-op (there's nothing to free).
///
/// # Ownership
///
/// Returned pointer is caller-owned. Free with [`blazen_step_output_free`].
///
/// # Safety
///
/// `event` must be null OR a pointer previously produced by
/// [`blazen_event_new`] (or any cabi function producing a `BlazenEvent`).
/// Calling this twice with the same non-null `event` is a double-free.
pub unsafe extern "C"
/// Constructs an empty `StepOutput::Multiple { events: [] }` value. Use
/// [`blazen_step_output_multiple_push`] to append events to it.
///
/// # Ownership
///
/// Returned pointer is caller-owned. Free with [`blazen_step_output_free`].
pub extern "C"
/// Appends `event` to a `StepOutput::Multiple` value. Consumes ownership
/// of `event` — callers MUST NOT separately free it after a successful
/// push.
///
/// If `output` currently holds `None`, it transitions to
/// `Multiple { events: [event] }`. If it holds `Single { event: prior }`,
/// it transitions to `Multiple { events: [prior, event] }`. If it already
/// holds `Multiple`, the event is appended.
///
/// Returns the previous variant tag (one of the `BLAZEN_STEP_OUTPUT_*`
/// constants) on success, or `u32::MAX` if either pointer is null. On the
/// `u32::MAX` path the function is a no-op and `event` (if non-null) is
/// freed to avoid leaking caller-allocated input on the failure path.
///
/// # Safety
///
/// `output` must be a valid pointer to a `BlazenStepOutput` previously
/// produced by the cabi surface. `event` must be null OR a pointer
/// previously produced by [`blazen_event_new`].
pub unsafe extern "C"
/// Returns the variant tag of `output` — one of the `BLAZEN_STEP_OUTPUT_*`
/// constants. Returns `u32::MAX` if `output` is null.
///
/// # Safety
///
/// `output` must be null OR a valid pointer to a `BlazenStepOutput`
/// previously produced by the cabi surface.
pub unsafe extern "C"
/// Returns a freshly-cloned `BlazenEvent` handle holding the inner event
/// of a `StepOutput::Single` value. Returns null if `output` is null or
/// the variant is not `Single`.
///
/// # Ownership
///
/// Returned pointer is caller-owned. Free with [`blazen_event_free`].
///
/// # Safety
///
/// `output` must be null OR a valid pointer to a `BlazenStepOutput`
/// previously produced by the cabi surface.
pub unsafe extern "C"
/// Returns the number of events in a `StepOutput::Multiple` value, or `0`
/// if `output` is null or the variant is not `Multiple`.
///
/// # Safety
///
/// `output` must be null OR a valid pointer to a `BlazenStepOutput`
/// previously produced by the cabi surface.
pub unsafe extern "C"
/// Returns a freshly-cloned `BlazenEvent` handle holding the event at
/// position `idx` of a `StepOutput::Multiple` value. Returns null if
/// `output` is null, the variant is not `Multiple`, or `idx` is out of
/// bounds.
///
/// # Ownership
///
/// Returned pointer is caller-owned. Free with [`blazen_event_free`].
///
/// # Safety
///
/// `output` must be null OR a valid pointer to a `BlazenStepOutput`
/// previously produced by the cabi surface.
pub unsafe extern "C"
/// Frees a `BlazenStepOutput` handle previously produced by the cabi
/// surface. No-op on a null pointer.
///
/// # Safety
///
/// `output` must be null OR a pointer previously produced by one of the
/// `blazen_step_output_new_*` functions. Calling this twice on the same
/// non-null pointer is a double-free.
pub unsafe extern "C"