objectiveai-api 2.0.5

ObjectiveAI API Server
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
use std::sync::Arc;
use std::time;
use futures::{Stream, StreamExt};
use crate::ctx;
use crate::util::{ChoiceIndexer, StreamOnce};

use objectiveai_sdk::agent::completions::message::{Message, UserMessage, RichContent, RichContentPart};

type LaboratoryExecutionChunk =
    objectiveai_sdk::laboratories::executions::response::streaming::LaboratoryExecutionChunk;
type BuilderChunk =
    objectiveai_sdk::laboratories::executions::response::streaming::BuilderChunk;
type EvaluationChunk =
    objectiveai_sdk::laboratories::executions::response::streaming::EvaluationChunk;
type Object = objectiveai_sdk::laboratories::executions::response::streaming::Object;
type Params = objectiveai_sdk::laboratories::executions::request::LaboratoryExecutionCreateParams;

type Continuation<OPENROUTER, CLAUDEAGENTSDK, CODEXSDK, MOCK> =
    crate::agent::completions::Continuation<
        <OPENROUTER as crate::agent::completions::UpstreamClient<
            objectiveai_sdk::agent::openrouter::Agent, objectiveai_sdk::agent::openrouter::Continuation,
        >>::State,
        <CLAUDEAGENTSDK as crate::agent::completions::UpstreamClient<
            objectiveai_sdk::agent::claude_agent_sdk::Agent, objectiveai_sdk::agent::claude_agent_sdk::Continuation,
        >>::State,
        <CODEXSDK as crate::agent::completions::UpstreamClient<
            objectiveai_sdk::agent::codex_sdk::Agent, objectiveai_sdk::agent::codex_sdk::Continuation,
        >>::State,
        <MOCK as crate::agent::completions::UpstreamClient<
            objectiveai_sdk::agent::mock::Agent, objectiveai_sdk::agent::mock::Continuation,
        >>::State,
    >;

pub fn response_id(created: u64) -> String {
    let uuid = uuid::Uuid::new_v4();
    format!("lbexec-{}-{created}", uuid.simple())
}

/// Laboratory client that runs builder agents in orchestrated environments
/// (Docker containers, GCP instances, etc.) with the embedded
/// objectiveai-mcp-filesystem binary.
pub struct Client<CTXEXT, OPENROUTER, CLAUDEAGENTSDK, CODEXSDK, MOCK, RETRG, RETRF, RETRM, CUSG, LUSG, ORCH> {
    pub agent_client: Arc<
        crate::agent::completions::Client<
            CTXEXT, OPENROUTER, CLAUDEAGENTSDK, CODEXSDK, MOCK, RETRG, RETRF, RETRM, CUSG,
        >,
    >,
    pub retrieve_router:
        Arc<crate::retrieval::retrieve::Router<RETRG, RETRF, RETRM, CTXEXT>>,
    pub usage_handler: Arc<LUSG>,
    pub viewer: Arc<crate::viewer::Client<CTXEXT>>,
    pub orchestrator: Arc<ORCH>,
}

/// Add an MCP server address to an inline agent base.
fn inject_mcp_server(agent: &mut objectiveai_sdk::agent::InlineAgentBase, mcp_url: String) {
    let server = objectiveai_sdk::agent::McpServer {
        url: mcp_url,
        authorization: false,
    };
    match agent {
        objectiveai_sdk::agent::InlineAgentBase::Openrouter(b) => {
            b.mcp_servers.get_or_insert_with(Vec::new).push(server);
        }
        objectiveai_sdk::agent::InlineAgentBase::ClaudeAgentSdk(b) => {
            b.mcp_servers.get_or_insert_with(Vec::new).push(server);
        }
        objectiveai_sdk::agent::InlineAgentBase::CodexSdk(b) => {
            b.mcp_servers.get_or_insert_with(Vec::new).push(server);
        }
        objectiveai_sdk::agent::InlineAgentBase::Mock(b) => {
            b.mcp_servers.get_or_insert_with(Vec::new).push(server);
        }
    }
}

impl<CTXEXT, OPENROUTER, CLAUDEAGENTSDK, CODEXSDK, MOCK, RETRG, RETRF, RETRM, CUSG, LUSG, ORCH>
    Client<CTXEXT, OPENROUTER, CLAUDEAGENTSDK, CODEXSDK, MOCK, RETRG, RETRF, RETRM, CUSG, LUSG, ORCH>
where
    CTXEXT: ctx::ContextExt + Send + Sync + 'static,
    OPENROUTER: crate::agent::completions::UpstreamClient<
            objectiveai_sdk::agent::openrouter::Agent, objectiveai_sdk::agent::openrouter::Continuation,
        > + Send
        + Sync
        + 'static,
    CLAUDEAGENTSDK: crate::agent::completions::UpstreamClient<
            objectiveai_sdk::agent::claude_agent_sdk::Agent, objectiveai_sdk::agent::claude_agent_sdk::Continuation,
        > + Send
        + Sync
        + 'static,
    CODEXSDK: crate::agent::completions::UpstreamClient<
            objectiveai_sdk::agent::codex_sdk::Agent, objectiveai_sdk::agent::codex_sdk::Continuation,
        > + Send
        + Sync
        + 'static,
    MOCK: crate::agent::completions::UpstreamClient<
            objectiveai_sdk::agent::mock::Agent, objectiveai_sdk::agent::mock::Continuation,
        > + Send
        + Sync
        + 'static,
    RETRG: crate::retrieval::retrieve::Client<CTXEXT> + Send + Sync + 'static,
    RETRF: crate::retrieval::retrieve::Client<CTXEXT> + Send + Sync + 'static,
    RETRM: crate::retrieval::retrieve::Client<CTXEXT> + Send + Sync + 'static,
    CUSG: crate::agent::completions::usage_handler::UsageHandler<CTXEXT> + Send + Sync + 'static,
    LUSG: crate::laboratories::executions::usage_handler::UsageHandler<CTXEXT> + Send + Sync + 'static,
    ORCH: crate::laboratories::orchestrator::Orchestrator<CTXEXT>,
{
    pub async fn create_streaming(
        self: Arc<Self>,
        ctx: ctx::Context<CTXEXT, impl crate::ctx::persistent_cache::PersistentCacheClient>,
        request: Arc<Params>,
    ) -> Result<
        impl Stream<Item = LaboratoryExecutionChunk> + Send + 'static,
        super::Error,
    > {
        // Timestamp and identify the execution — before any awaits
        let created = time::SystemTime::now()
            .duration_since(time::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let id = response_id(created);
        let object = Object::LaboratoryExecutionChunk;

        // Send begin to viewer
        self.viewer.send_laboratory_execution_begin(
            ctx.clone(),
            id.clone(),
            request.clone(),
        );

        // Helper: send error to viewer and return it
        let send_err = |e: super::Error| -> super::Error {
            self.viewer.send_laboratory_execution_error(
                ctx.clone(),
                id.clone(),
                objectiveai_sdk::error::ResponseError::from(&e),
            );
            e
        };

        if request.builder_agents.is_empty() {
            return Err(send_err(super::Error::NoBuilderAgents));
        }

        if request.evaluation_agent.is_some() != request.evaluation_output_schema.is_some() {
            return Err(send_err(super::Error::EvaluationConfigMismatch));
        }

        // Spawn builder environments and resolve agents concurrently
        #[cfg(feature = "orchestrator-bollard")]
        let binaries: &[(&str, &[u8])] = &[(
            "objectiveai-mcp-filesystem",
            super::mcp_binary::MCP_FILESYSTEM_BINARY,
        )];
        #[cfg(not(feature = "orchestrator-bollard"))]
        let binaries: &[(&str, &[u8])] = &[];
        let orchestrator_fut = self.orchestrator.spawn_containers(
            &ctx,
            &request.docker_image,
            request.builder_agents.len(),
            &id,
            binaries,
            &[("PORT", "3000")],
        );
        let builder_resolve_futs: Vec<_> = request
            .builder_agents
            .iter()
            .map(|agent_ref| self.retrieve_router.get_agent(&ctx, agent_ref.clone()))
            .collect();
        let eval_resolve_fut = async {
            match &request.evaluation_agent {
                Some(eval_ref) => self.retrieve_router.get_agent(&ctx, eval_ref.clone())
                    .await
                    .map(Some)
                    .map_err(|e| super::Error::AgentCompletion(e.to_string())),
                None => Ok(None),
            }
        };
        let (mcp_urls, resolved_builder_agents, resolved_eval_agent) = tokio::try_join!(
            async { orchestrator_fut.await.map_err(|e| super::Error::Orchestrator(objectiveai_sdk::error::ResponseError::from(&e))) },
            async {
                futures::future::try_join_all(builder_resolve_futs)
                    .await
                    .map_err(|e| super::Error::AgentCompletion(e.to_string()))
            },
            eval_resolve_fut,
        )
        .map_err(&send_err)?;

        let eval_agent = resolved_eval_agent.map(|wf| {
            let eval_agent_base = wf.inline().inner.clone().into_base();
            objectiveai_sdk::agent::InlineAgentBaseWithFallbacksOrRemoteCommitOptional::AgentBase(
                objectiveai_sdk::agent::InlineAgentBaseWithFallbacks {
                    inner: eval_agent_base,
                    fallbacks: None,
                },
            )
        });

        let mut builder_inline_agents = Vec::with_capacity(request.builder_agents.len());
        for (i, builder_agent_wf) in resolved_builder_agents.into_iter().enumerate() {
            let mut builder_agent_base = builder_agent_wf.inline().inner.clone().into_base();
            inject_mcp_server(&mut builder_agent_base, mcp_urls[i].clone());
            builder_inline_agents.push(builder_agent_base);
        }

        // Create agent completions for each builder concurrently
        let indexer = Arc::new(ChoiceIndexer::new(0));
        let agent_client = self.agent_client.clone();

        let streams: Vec<_> = builder_inline_agents
            .into_iter()
            .enumerate()
            .map(|(native_index, builder_agent_base)| {
                let agent_client = agent_client.clone();
                let ctx = ctx.clone();
                let request = request.clone();
                let indexer = indexer.clone();
                let id = id.clone();
                let agent_index = native_index as u64;

                let builder_agent_with_fallbacks = objectiveai_sdk::agent::InlineAgentBaseWithFallbacks {
                    inner: builder_agent_base,
                    fallbacks: None,
                };
                let builder_agent =
                    objectiveai_sdk::agent::InlineAgentBaseWithFallbacksOrRemoteCommitOptional::AgentBase(
                        builder_agent_with_fallbacks,
                    );

                let params = Arc::new(
                    objectiveai_sdk::agent::completions::request::AgentCompletionCreateParams {
                        messages: request.builder_messages.clone(),
                        provider: request.provider.clone(),
                        agent: builder_agent,
                        response_format: None,
                        seed: request.seed,
                        stream: Some(true),
                        continuation: request.builder_continuation.clone(),
                    },
                );

                Box::pin(async_stream::stream! {
                    let stream_result = agent_client
                        .create_streaming(ctx, params, None, None, vec![], indexmap::IndexMap::new(), None, false, None, None, None, None)
                        .await;

                    match stream_result {
                        Ok(stream) => {
                            futures::pin_mut!(stream);
                            while let Some(item) = stream.next().await {
                                match item {
                                    crate::agent::completions::StreamItem::Chunk(chunk) => {
                                        let completion_index = indexer.get(native_index);
                                        yield LaboratoryExecutionChunk {
                                            id: id.clone(),
                                            builders: vec![BuilderChunk {
                                                index: completion_index,
                                                agent_index,
                                                inner: chunk,
                                            }],
                                            evaluations: Vec::new(),
                                            error: None,
                                            created,
                                            object,
                                            usage: None,
                                        };
                                    }
                                    crate::agent::completions::StreamItem::State(_cont) => {
                                        // Continuation state — not used yet
                                    }
                                }
                            }
                        }
                        Err(e) => {
                            let completion_index = indexer.get(native_index);
                            yield LaboratoryExecutionChunk {
                                id: id.clone(),
                                builders: vec![BuilderChunk {
                                    index: completion_index,
                                    agent_index,
                                    inner: objectiveai_sdk::agent::completions::response::streaming::AgentCompletionChunk {
                                        error: Some(objectiveai_sdk::error::ResponseError::from(&e)),
                                        ..Default::default()
                                    },
                                }],
                                evaluations: Vec::new(),
                                error: None,
                                created,
                                object,
                                usage: None,
                            };
                        }
                    }
                }) as std::pin::Pin<Box<dyn Stream<Item = LaboratoryExecutionChunk> + Send>>
            })
            .collect();

        let viewer_client = self.viewer.clone();
        let viewer_ctx = ctx.clone();
        let orchestrator = self.orchestrator.clone();
        let cleanup_ctx = ctx.clone();
        let this = self.clone();
        let mut merged = futures::stream::select_all(streams);
        Ok(async_stream::stream! {
            let mut accumulated_usage = objectiveai_sdk::agent::completions::response::Usage::default();
            let mut errored_agents: std::collections::HashSet<u64> = std::collections::HashSet::new();

            // Phase 1: drain all builder streams
            while let Some(chunk) = merged.next().await {
                for builder in &chunk.builders {
                    if let Some(u) = &builder.inner.usage {
                        accumulated_usage.push(u);
                    }
                    if builder.inner.error.is_some() {
                        errored_agents.insert(builder.agent_index);
                    }
                }
                viewer_client.send_laboratory_execution_continue(viewer_ctx.clone(), chunk.clone());
                yield chunk;
            }

            // Phase 2: spawn evaluations for non-errored builders (only if eval agent provided and not all builders errored)
            if let Some(ref eval_agent) = eval_agent && errored_agents.len() < request.builder_agents.len() {
                let num_agents = request.builder_agents.len() as u64;
                let eval_streams: Vec<_> = (0..num_agents)
                    .filter(|i| !errored_agents.contains(i))
                    .map(|agent_index| {
                        Box::pin(this.clone().create_evaluation_streaming(
                            ctx.clone(),
                            request.clone(),
                            id.clone(),
                            created,
                            object,
                            agent_index,
                            agent_index,
                            eval_agent.clone(),
                        )) as std::pin::Pin<Box<dyn Stream<Item = LaboratoryExecutionChunk> + Send>>
                    })
                    .collect();

                let mut eval_merged = futures::stream::select_all(eval_streams);
                while let Some(chunk) = eval_merged.next().await {
                    for evaluation in &chunk.evaluations {
                        if let Some(u) = &evaluation.inner.usage {
                            accumulated_usage.push(u);
                        }
                    }
                    viewer_client.send_laboratory_execution_continue(viewer_ctx.clone(), chunk.clone());
                    yield chunk;
                }
            }

            let final_chunk = LaboratoryExecutionChunk {
                id: id.clone(),
                builders: Vec::new(),
                evaluations: Vec::new(),
                error: None,
                created,
                object,
                usage: Some(accumulated_usage),
            };
            if !request.persist.unwrap_or(false) {
                let orch = orchestrator.clone();
                let cctx = cleanup_ctx.clone();
                let eid = id.clone();
                let num = request.builder_agents.len();
                tokio::spawn(async move { orch.cleanup_containers(&cctx, &eid, num).await });
            }
            viewer_client.send_laboratory_execution_continue(viewer_ctx.clone(), final_chunk.clone());
            yield final_chunk;
        })
    }

    /// Create a streaming evaluation for a single evaluation agent.
    ///
    /// Appends the evaluation schema to the messages, runs the agent completion,
    /// parses the response as `InputValue`, validates against the schema, and
    /// retries on error up to `max_evaluation_retries`.
    pub fn create_evaluation_streaming(
        self: Arc<Self>,
        ctx: ctx::Context<CTXEXT, impl crate::ctx::persistent_cache::PersistentCacheClient>,
        request: Arc<Params>,
        id: String,
        created: u64,
        object: Object,
        evaluation_index: u64,
        agent_index: u64,
        agent: objectiveai_sdk::agent::InlineAgentBaseWithFallbacksOrRemoteCommitOptional,
    ) -> impl Stream<Item = LaboratoryExecutionChunk> + Send + 'static {
        let agent_client = self.agent_client.clone();
        let max_retries = request.max_evaluation_retries.unwrap_or(3);

        // Build the schema prompt suffix
        let schema_text = format!(
            "## evaluation schema\n\n{}",
            serde_json::to_string_pretty(request.evaluation_output_schema.as_ref().unwrap()).unwrap(),
        );

        // Inject schema into messages: append to last user message or create one
        let mut messages = request.evaluation_messages.clone().unwrap();
        let mut injected = false;
        for msg in messages.iter_mut().rev() {
            if let Message::User(user) = msg {
                match &mut user.content {
                    RichContent::Text(t) => {
                        t.push_str("\n\n");
                        t.push_str(&schema_text);
                    }
                    RichContent::Parts(parts) => {
                        parts.push(RichContentPart::Text {
                            text: format!("\n\n{schema_text}"),
                        });
                    }
                }
                injected = true;
                break;
            }
        }
        if !injected {
            messages.push(Message::User(UserMessage {
                content: RichContent::Text(schema_text.clone()),
                name: None,
            }));
        }

        let params = Arc::new(
            objectiveai_sdk::agent::completions::request::AgentCompletionCreateParams {
                messages,
                provider: request.provider.clone(),
                agent,
                response_format: None,
                seed: request.seed,
                stream: Some(true),
                continuation: request.evaluation_continuation.clone(),
            },
        );

        async_stream::stream! {
            let mut continuation: Option<Continuation<OPENROUTER, CLAUDEAGENTSDK, CODEXSDK, MOCK>> = None;
            let mut retries = 0u32;

            loop {
                // Create agent completion stream
                let stream_result = agent_client
                    .create_streaming(
                        ctx.clone(),
                        params.clone(),
                        continuation.take(),
                        None, // disable_tools
                        vec![], // extra_mcp_servers
                        indexmap::IndexMap::new(), // extra_mcp_headers
                        None,
                        false,
                        None,
                        None,
                        None,
                        None,
                    )
                    .await;

                let mut accumulated_chunk: Option<objectiveai_sdk::agent::completions::response::streaming::AgentCompletionChunk> = None;

                match stream_result {
                    Ok(stream) => {
                        futures::pin_mut!(stream);
                        while let Some(item) = stream.next().await {
                            match item {
                                crate::agent::completions::StreamItem::Chunk(chunk) => {
                                    match &mut accumulated_chunk {
                                        Some(acc) => acc.push(&chunk),
                                        None => accumulated_chunk = Some(chunk.clone()),
                                    }
                                    yield LaboratoryExecutionChunk {
                                        id: id.clone(),
                                        builders: Vec::new(),
                                        evaluations: vec![EvaluationChunk {
                                            index: evaluation_index,
                                            agent_index,
                                            inner: chunk,
                                            output: None,
                                        }],
                                        error: None,
                                        created,
                                        object,
                                        usage: None,
                                    };
                                }
                                crate::agent::completions::StreamItem::State(cont) => {
                                    continuation = Some(cont);
                                }
                            }
                        }
                    }
                    Err(e) => {
                        yield LaboratoryExecutionChunk {
                            id: id.clone(),
                            builders: Vec::new(),
                            evaluations: vec![EvaluationChunk {
                                index: evaluation_index,
                                agent_index,
                                inner: objectiveai_sdk::agent::completions::response::streaming::AgentCompletionChunk {
                                    error: Some(objectiveai_sdk::error::ResponseError::from(&e)),
                                    ..Default::default()
                                },
                                output: None,
                            }],
                            error: None,
                            created,
                            object,
                            usage: None,
                        };
                        break;
                    }
                }

                // Extract assistant content text from accumulated chunks
                let content_text = accumulated_chunk
                    .as_ref()
                    .and_then(|chunk| {
                        chunk.messages.iter().rev().find_map(|msg| {
                            if let objectiveai_sdk::agent::completions::response::streaming::MessageChunk::Assistant(asst) = msg {
                                asst.content.as_ref().map(|c| match c {
                                    RichContent::Text(t) => t.clone(),
                                    RichContent::Parts(parts) => parts
                                        .iter()
                                        .filter_map(|p| match p {
                                            RichContentPart::Text { text } => Some(text.as_str()),
                                            _ => None,
                                        })
                                        .collect::<Vec<_>>()
                                        .join(""),
                                })
                            } else {
                                None
                            }
                        })
                    })
                    .unwrap_or_default();

                // Parse as InputValue
                let parse_result: Result<objectiveai_sdk::functions::expression::InputValue, _> = {
                    let mut de = serde_json::Deserializer::from_str(&content_text);
                    serde_path_to_error::deserialize(&mut de)
                };

                match parse_result {
                    Ok(input_value) => {
                        // Validate against schema (if provided)
                        let valid = request
                            .evaluation_output_schema
                            .as_ref()
                            .unwrap()
                            .validate_input(&input_value);

                        if valid {
                            // Yield final chunk with output
                            yield LaboratoryExecutionChunk {
                                id: id.clone(),
                                builders: Vec::new(),
                                evaluations: vec![EvaluationChunk {
                                    index: evaluation_index,
                                    agent_index,
                                    inner: Default::default(),
                                    output: Some(input_value),
                                }],
                                error: None,
                                created,
                                object,
                                usage: None,
                            };
                            break;
                        }

                        // Schema validation failed
                        let err = super::Error::EvaluationOutputSchemaMismatch;
                        if retries >= max_retries {
                            yield LaboratoryExecutionChunk {
                                id: id.clone(),
                                builders: Vec::new(),
                                evaluations: vec![EvaluationChunk {
                                    index: evaluation_index,
                                    agent_index,
                                    inner: objectiveai_sdk::agent::completions::response::streaming::AgentCompletionChunk {
                                        error: Some(objectiveai_sdk::error::ResponseError::from(&err)),
                                        ..Default::default()
                                    },
                                    output: None,
                                }],
                                error: None,
                                created,
                                object,
                                usage: None,
                            };
                            break;
                        }

                        // Retry with error message
                        let retry_msg = format!(
                            "{}\n\n## error\n\nevaluation output does not match schema",
                            schema_text,
                        );
                        if let Some(ref mut cont) = continuation {
                            cont.push_user_message(UserMessage {
                                content: RichContent::Text(retry_msg),
                                name: None,
                            });
                        }
                        retries += 1;
                    }
                    Err(parse_err) => {
                        // Parse failed
                        let err = super::Error::EvaluationOutputParse(parse_err.to_string());
                        if retries >= max_retries {
                            yield LaboratoryExecutionChunk {
                                id: id.clone(),
                                builders: Vec::new(),
                                evaluations: vec![EvaluationChunk {
                                    index: evaluation_index,
                                    agent_index,
                                    inner: objectiveai_sdk::agent::completions::response::streaming::AgentCompletionChunk {
                                        error: Some(objectiveai_sdk::error::ResponseError::from(&err)),
                                        ..Default::default()
                                    },
                                    output: None,
                                }],
                                error: None,
                                created,
                                object,
                                usage: None,
                            };
                            break;
                        }

                        // Retry with parse error
                        let retry_msg = format!(
                            "{}\n\n## error\n\n{}",
                            schema_text, parse_err,
                        );
                        if let Some(ref mut cont) = continuation {
                            cont.push_user_message(UserMessage {
                                content: RichContent::Text(retry_msg),
                                name: None,
                            });
                        }
                        retries += 1;
                    }
                }
            }
        }
    }

    pub fn create_unary_handle_usage(
        self: Arc<Self>,
        ctx: ctx::Context<CTXEXT, impl crate::ctx::persistent_cache::PersistentCacheClient>,
        request: Arc<Params>,
    ) -> impl std::future::Future<
        Output = Result<
            objectiveai_sdk::laboratories::executions::response::unary::LaboratoryExecution,
            super::Error,
        >,
    > + Send {
        async move {
            let mut aggregate: Option<LaboratoryExecutionChunk> = None;
            let mut stream =
                self.create_streaming_handle_usage(ctx, request).await?;
            while let Some(chunk) = stream.next().await {
                match &mut aggregate {
                    Some(aggregate) => aggregate.push(&chunk),
                    None => aggregate = Some(chunk),
                }
            }
            Ok(aggregate.unwrap().into())
        }
    }

    pub fn create_streaming_handle_usage(
        self: Arc<Self>,
        ctx: ctx::Context<CTXEXT, impl crate::ctx::persistent_cache::PersistentCacheClient>,
        request: Arc<Params>,
    ) -> impl std::future::Future<
        Output = Result<
            impl Stream<Item = LaboratoryExecutionChunk> + Send + Unpin + 'static,
            super::Error,
        >,
    > + Send {
        async move {
            let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
            tokio::spawn(async move {
                let mut aggregate: Option<LaboratoryExecutionChunk> = None;
                let stream = match self
                    .clone()
                    .create_streaming(ctx.clone(), request.clone())
                    .await
                {
                    Ok(stream) => stream,
                    Err(e) => {
                        let _ = tx.send(Err(e));
                        return;
                    }
                };
                futures::pin_mut!(stream);
                while let Some(chunk) = stream.next().await {
                    match &mut aggregate {
                        Some(aggregate) => aggregate.push(&chunk),
                        None => aggregate = Some(chunk.clone()),
                    }
                    if tx.send(Ok(chunk)).is_err() {
                        ctx.cancel();
                    }
                }
                drop(stream);
                drop(tx);
                let response: objectiveai_sdk::laboratories::executions::response::unary::LaboratoryExecution =
                    aggregate.unwrap().into();
                if response.any_usage() {
                    self.usage_handler
                        .handle_usage(ctx, request, response)
                        .await;
                }
            });
            let mut stream =
                tokio_stream::wrappers::UnboundedReceiverStream::new(rx);
            match stream.next().await {
                Some(Ok(chunk)) => {
                    Ok(StreamOnce::new(chunk).chain(stream.map(Result::unwrap)))
                }
                Some(Err(e)) => Err(e),
                None => unreachable!(),
            }
        }
    }
}