Skip to main content

env_encode_error

Function env_encode_error 

Source
pub fn env_encode_error(key: &str, err: Error) -> SError
Expand description

Convert an environment serialization failure into a structured workflow error.

This function is public because exported macros use it in generated code. It is not usually called directly by workflow authors.

Examples found in repository?
examples/tool_calling_and_web_searching.rs (line 592)
547fn receive_response(
548    workflow: &mut Workflow,
549    mut state: ToolLoopState,
550    response: Message,
551    continuation: Continuation,
552    output_key: &str,
553    results_key: &str,
554    after_function: &str,
555) -> Result<(ToolLoopState, ContinuationChoice), handled::SError> {
556    (|| {
557        state.messages.push(MessageParam::from(response.clone()));
558
559        let tool_uses = client_tool_uses(&response);
560        if !tool_uses.is_empty() {
561            let requested = tool_uses.len() as u32;
562            if state.tool_calls_used.saturating_add(requested) > state.max_tool_calls {
563                return Err(demo_error(
564                    "tool-call-budget-exhausted",
565                    "model exceeded the text_editor tool-call budget",
566                )
567                .with_string_field("required_file", &state.required_file)
568                .with_atom_field("max_tool_calls", state.max_tool_calls)
569                .with_atom_field("tool_calls_used", state.tool_calls_used)
570                .with_atom_field("requested_tool_calls", requested));
571            }
572            state.tool_calls_used += requested;
573            // Suspend: the runtime runs each tool_use through the registered
574            // tool and resumes at `after_function` with the results.
575            return Ok((
576                state,
577                continuation.tool_call(tool_uses, results_key, after_function),
578            ));
579        }
580
581        if matches!(response.stop_reason, Some(StopReason::ToolUse)) {
582            return Err(demo_error(
583                "unsupported-tool-use",
584                "model requested tool use but no client-side text editor tool call was present",
585            )
586            .with_string_field("required_file", &state.required_file));
587        }
588
589        let output = read_required_output(&state)?;
590        workflow
591            .into_env(output_key, output)
592            .map_err(|err| langcontinuation::env_encode_error(output_key, err))?;
593        Ok((state, continuation.halt()))
594    })()
595}