1- import type { Pipe } from 'types/pipe' ;
1+ import type { Pipe , ToolCall } from 'types/pipe' ;
22import { getProvider } from './get-provider' ;
33import { getSupportedToolSettings , hasToolSupport } from './has-tool-support' ;
4- import type { ModelParams , Tool } from 'types/providers' ;
4+ import type { ModelParams } from 'types/providers' ;
5+ import type { PipeTool } from 'types/tools' ;
56
6- export function addToolsToParams ( modelParams : ModelParams , pipe : Pipe ) {
7- if ( ! pipe . tools . length ) return ;
7+ export function addToolsToParams (
8+ modelParams : ModelParams ,
9+ pipe : Pipe ,
10+ paramsTools : PipeTool [ ] | undefined
11+ ) {
12+ const pipeTools = pipe . tools as unknown as string [ ] ;
13+ const hasParamsTools = paramsTools && paramsTools . length > 0 ;
14+
15+ // 1. If no tools are provided, return the modelParams as is
16+ if ( ! hasParamsTools && ! pipeTools . length ) return modelParams ;
817
918 const [ providerString , modelName ] = pipe . model . split ( ':' ) ;
1019 const provider = getProvider ( providerString ) ;
@@ -15,21 +24,30 @@ export function addToolsToParams(modelParams: ModelParams, pipe: Pipe) {
1524 provider
1625 } ) ;
1726
18- if ( hasToolCallSupport ) {
19- const { hasParallelToolCallSupport, hasToolChoiceSupport } =
20- getSupportedToolSettings ( {
21- modelName,
22- provider
23- } ) ;
27+ // 2. If the model does not support tool calls, return the modelParams as is
28+ if ( ! hasToolCallSupport ) return modelParams ;
29+
30+ // If tools are provided in request param, prioritize and use them
31+ if ( hasParamsTools ) {
32+ modelParams . tools = paramsTools as ToolCall [ ] ;
33+ }
34+
35+ // If tools are not provided in request param, use the tools from the pipe config
36+ if ( ! hasParamsTools && pipeTools . length ) {
37+ modelParams . tools = pipe . tools as ToolCall [ ] ;
38+ }
2439
25- if ( hasParallelToolCallSupport ) {
26- modelParams . parallel_tool_calls = pipe . parallel_tool_calls ;
27- }
40+ const { hasParallelToolCallSupport, hasToolChoiceSupport } =
41+ getSupportedToolSettings ( {
42+ modelName,
43+ provider
44+ } ) ;
2845
29- if ( hasToolChoiceSupport ) {
30- modelParams . tool_choice = pipe . tool_choice ;
31- }
46+ if ( hasParallelToolCallSupport ) {
47+ modelParams . parallel_tool_calls = pipe . parallel_tool_calls ;
48+ }
3249
33- modelParams . tools = pipe . tools as Tool [ ] ;
50+ if ( hasToolChoiceSupport ) {
51+ modelParams . tool_choice = pipe . tool_choice ;
3452 }
3553}
0 commit comments