MCPcopy Create free account
hub / github.com/ChrisFeldmeier/OpenCodeRust / prepare_tools

Function prepare_tools

crates/opencode-provider/src/tools.rs:598–657  ·  view source on GitHub ↗

Prepare tools for OpenAI-compatible chat completions. Mirrors TS `prepareTools()`.

(
    tools: Option<&[InputTool]>,
    tool_choice: Option<&InputToolChoice>,
)

Source from the content-addressed store, hash-verified

596/// Prepare tools for OpenAI-compatible chat completions.
597/// Mirrors TS `prepareTools()`.
598pub fn prepare_tools(
599 tools: Option<&[InputTool]>,
600 tool_choice: Option<&InputToolChoice>,
601) -> PreparedChatTools {
602 let tools = match tools {
603 Some(t) if !t.is_empty() => t,
604 _ => {
605 return PreparedChatTools {
606 tools: None,
607 tool_choice: None,
608 tool_warnings: vec![],
609 }
610 }
611 };
612
613 let mut openai_tools = Vec::new();
614 let mut tool_warnings = Vec::new();
615
616 for tool in tools {
617 match tool {
618 InputTool::Function {
619 name,
620 description,
621 input_schema,
622 } => {
623 openai_tools.push(ChatTool {
624 tool_type: "function".to_string(),
625 function: ChatToolFunction {
626 name: name.clone(),
627 description: description.clone(),
628 parameters: input_schema.clone(),
629 },
630 });
631 }
632 InputTool::ProviderDefined { name, .. } => {
633 tool_warnings.push(CallWarning::UnsupportedTool {
634 tool_name: Some(name.clone()),
635 });
636 }
637 }
638 }
639
640 let mapped_choice = tool_choice.map(|tc| match tc {
641 InputToolChoice::Auto => ChatToolChoice::Mode("auto".to_string()),
642 InputToolChoice::None => ChatToolChoice::Mode("none".to_string()),
643 InputToolChoice::Required => ChatToolChoice::Mode("required".to_string()),
644 InputToolChoice::Tool { tool_name } => ChatToolChoice::Specific(ChatToolChoiceSpecific {
645 choice_type: "function".to_string(),
646 function: ChatToolChoiceFunction {
647 name: tool_name.clone(),
648 },
649 }),
650 });
651
652 PreparedChatTools {
653 tools: Some(openai_tools),
654 tool_choice: mapped_choice,
655 tool_warnings,

Callers

nothing calls this directly

Calls 3

newFunction · 0.85
is_emptyMethod · 0.80
cloneMethod · 0.45

Tested by

no test coverage detected