* Map OpenAI tool_choice to AI SDK toolChoice format.
( toolChoice: OpenAI.Chat.ChatCompletionCreateParams["tool_choice"], )
| 107 | * Map OpenAI tool_choice to AI SDK toolChoice format. |
| 108 | */ |
| 109 | protected mapToolChoice( |
| 110 | toolChoice: OpenAI.Chat.ChatCompletionCreateParams["tool_choice"], |
| 111 | ): "auto" | "none" | "required" | { type: "tool"; toolName: string } | undefined { |
| 112 | if (!toolChoice) { |
| 113 | return undefined |
| 114 | } |
| 115 | |
| 116 | // Handle string values |
| 117 | if (typeof toolChoice === "string") { |
| 118 | switch (toolChoice) { |
| 119 | case "auto": |
| 120 | return "auto" |
| 121 | case "none": |
| 122 | return "none" |
| 123 | case "required": |
| 124 | return "required" |
| 125 | default: |
| 126 | return "auto" |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Handle object values (OpenAI ChatCompletionNamedToolChoice format) |
| 131 | if (typeof toolChoice === "object" && "type" in toolChoice) { |
| 132 | if (toolChoice.type === "function" && "function" in toolChoice && toolChoice.function?.name) { |
| 133 | return { type: "tool", toolName: toolChoice.function.name } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return undefined |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get the max tokens parameter to include in the request. |