Skip to content

Commit b2eb3ed

Browse files
marko-kriskovicivicac
authored andcommitted
4761 - added getMultipleConnectionsTool support
1 parent 55a5ed1 commit b2eb3ed

5 files changed

Lines changed: 127 additions & 0 deletions

File tree

server/ee/libs/platform/platform-component/platform-component-remote-client/src/main/java/com/bytechef/ee/platform/component/remote/client/service/RemoteClusterElementDefinitionServiceClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ public Object executeTool(
8181
throw new UnsupportedOperationException();
8282
}
8383

84+
@Override
85+
public Object executeTool(
86+
String componentName, int componentVersion, String clusterElementName, Map<String, ?> inputParameters,
87+
Map<String, ?> extensions, Map<String, ComponentConnection> componentConnections, boolean editorEnvironment) {
88+
89+
throw new UnsupportedOperationException();
90+
}
91+
8492
@Override
8593
public String executeWorkflowNodeDescription(
8694
String componentName, int componentVersion, String clusterElementName, Map<String, ?> inputParameters) {

server/libs/modules/components/ai/agent/src/main/java/com/bytechef/component/ai/agent/action/AbstractAiAgentChatAction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.bytechef.platform.component.definition.ai.agent.ChatMemoryFunction;
4040
import com.bytechef.platform.component.definition.ai.agent.GuardrailsFunction;
4141
import com.bytechef.platform.component.definition.ai.agent.ModelFunction;
42+
import com.bytechef.platform.component.definition.ai.agent.MultipleConnectionsToolFunction;
4243
import com.bytechef.platform.component.definition.ai.agent.RagFunction;
4344
import com.bytechef.platform.component.definition.ai.agent.ToolCallbackProviderFunction;
4445
import com.bytechef.platform.component.service.ClusterElementDefinitionService;
@@ -249,6 +250,9 @@ private List<ToolCallback> getToolCallbacks(
249250
} catch (Exception exception) {
250251
throw new RuntimeException(exception);
251252
}
253+
} else if (clusterElementFunction instanceof MultipleConnectionsToolFunction) {
254+
toolCallbacks.add(
255+
aiAgentToolFacade.getFunctionToolCallback(clusterElement, connectionParameters, editorEnvironment));
252256
} else {
253257
ComponentConnection componentConnection = connectionParameters.get(
254258
clusterElement.getWorkflowNodeName());

server/libs/modules/components/ai/agent/src/main/java/com/bytechef/component/ai/agent/facade/AiAgentToolFacade.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,42 @@ public ToolCallback getFunctionToolCallback(
8686
return builder.build();
8787
}
8888

89+
public ToolCallback getFunctionToolCallback(
90+
ClusterElement clusterElement, Map<String, ComponentConnection> componentConnections,
91+
boolean editorEnvironment) {
92+
93+
ClusterElementDefinition clusterElementDefinition =
94+
clusterElementDefinitionService.getClusterElementDefinition(
95+
clusterElement.getComponentName(), clusterElement.getComponentVersion(),
96+
clusterElement.getClusterElementName());
97+
98+
Map<String, ?> toolParameters = clusterElement.getParameters();
99+
100+
List<FromAiResult> fromAiResults = extractFromAiResults(toolParameters);
101+
102+
FunctionToolCallback.Builder<Map<String, Object>, Object> builder = FunctionToolCallback.builder(
103+
getToolName(clusterElementDefinition.getComponentName(), clusterElementDefinition.getName(),
104+
toolParameters),
105+
getMultipleConnectionsToolCallbackFunction(
106+
clusterElement.getComponentName(), clusterElement.getComponentVersion(),
107+
clusterElementDefinition.getName(), toolParameters, clusterElement.getExtensions(),
108+
componentConnections, editorEnvironment))
109+
.inputType(Map.class)
110+
.inputSchema(FromAiInputSchemaUtils.generateInputSchema(fromAiResults));
111+
112+
String toolDescription = getToolDescription(toolParameters, clusterElement.getExtensions());
113+
114+
if (toolDescription == null) {
115+
toolDescription = clusterElementDefinition.getDescription();
116+
}
117+
118+
if (toolDescription != null) {
119+
builder.description(toolDescription);
120+
}
121+
122+
return builder.build();
123+
}
124+
89125
private Function<Map<String, Object>, Object> getFromAiToolCallbackFunction(
90126
String componentName, int componentVersion, String clusterElementName, Map<String, ?> parameters,
91127
@Nullable ComponentConnection componentConnection, boolean editorEnvironment) {
@@ -103,4 +139,21 @@ private Function<Map<String, Object>, Object> getFromAiToolCallbackFunction(
103139
};
104140
}
105141

142+
private Function<Map<String, Object>, Object> getMultipleConnectionsToolCallbackFunction(
143+
String componentName, int componentVersion, String clusterElementName, Map<String, ?> parameters,
144+
Map<String, ?> extensions, Map<String, ComponentConnection> componentConnections, boolean editorEnvironment) {
145+
146+
return request -> {
147+
Map<String, Object> resolvedParameters = new HashMap<>();
148+
149+
for (Map.Entry<String, ?> entry : parameters.entrySet()) {
150+
resolvedParameters.put(entry.getKey(), resolveParameterValue(entry.getValue(), request));
151+
}
152+
153+
return clusterElementDefinitionService.executeTool(
154+
componentName, componentVersion, clusterElementName, MapUtils.concat(request, resolvedParameters),
155+
extensions, componentConnections, editorEnvironment);
156+
};
157+
}
158+
106159
}

server/libs/platform/platform-component/platform-component-api/src/main/java/com/bytechef/platform/component/service/ClusterElementDefinitionService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ Object executeTool(
6161
String componentName, int componentVersion, String clusterElementName, Map<String, ?> inputParameters,
6262
@Nullable ComponentConnection componentConnection, boolean editorEnvironment);
6363

64+
Object executeTool(
65+
String componentName, int componentVersion, String clusterElementName, Map<String, ?> inputParameters,
66+
Map<String, ?> extensions, Map<String, ComponentConnection> componentConnections, boolean editorEnvironment);
67+
6468
String executeWorkflowNodeDescription(
6569
String componentName, int componentVersion, String clusterElementName, Map<String, ?> inputParameters);
6670

server/libs/platform/platform-component/platform-component-service/src/main/java/com/bytechef/platform/component/service/ClusterElementDefinitionServiceImpl.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.bytechef.platform.component.definition.ClusterRootComponentDefinition;
4747
import com.bytechef.platform.component.definition.ParametersFactory;
4848
import com.bytechef.platform.component.definition.PropertyFactory;
49+
import com.bytechef.platform.component.definition.ai.agent.MultipleConnectionsToolFunction;
4950
import com.bytechef.platform.component.definition.ai.agent.ToolCallbackProviderFunction;
5051
import com.bytechef.platform.component.definition.datastream.ClusterElementResolverFunction;
5152
import com.bytechef.platform.component.domain.ClusterElementDefinition;
@@ -206,6 +207,24 @@ public Object executeTool(
206207
clusterElementContext);
207208
}
208209

210+
@Override
211+
public Object executeTool(
212+
String componentName, int componentVersion, String clusterElementName, Map<String, ?> inputParameters,
213+
Map<String, ?> extensions, Map<String, ComponentConnection> componentConnections, boolean editorEnvironment) {
214+
215+
ComponentConnection firstConnection = componentConnections.isEmpty()
216+
? null : componentConnections.values()
217+
.iterator()
218+
.next();
219+
220+
ClusterElementContext clusterElementContext = contextFactory.createClusterElementContext(
221+
componentName, componentVersion, clusterElementName, firstConnection, editorEnvironment);
222+
223+
return doExecuteTool(
224+
componentName, componentVersion, clusterElementName, inputParameters, extensions, componentConnections,
225+
clusterElementContext);
226+
}
227+
209228
@Override
210229
public String executeWorkflowNodeDescription(
211230
String componentName, int componentVersion, String clusterElementName, Map<String, ?> inputParameters) {
@@ -464,6 +483,45 @@ private Object doExecuteTool(
464483
}
465484
}
466485

486+
private Object doExecuteTool(
487+
String componentName, Integer componentVersion, String clusterElementName, Map<String, ?> inputParameterMap,
488+
Map<String, ?> extensionMap, Map<String, ComponentConnection> componentConnections,
489+
ClusterElementContext context) {
490+
491+
Object clusterElement = getClusterElement(componentName, componentVersion, clusterElementName);
492+
493+
Parameters inputParameters = ParametersFactory.create(inputParameterMap);
494+
Parameters connectionParameters = ParametersFactory.create(Map.of());
495+
Parameters extensions = ParametersFactory.create(extensionMap);
496+
497+
try {
498+
if (clusterElement instanceof MultipleConnectionsToolFunction multipleConnectionsToolFunction) {
499+
return multipleConnectionsToolFunction.apply(
500+
inputParameters, connectionParameters, extensions, componentConnections, context);
501+
}
502+
503+
if (clusterElement instanceof ToolCallbackProviderFunction toolCallbackProviderFunction) {
504+
return toolCallbackProviderFunction.apply(inputParameters, connectionParameters, context);
505+
}
506+
507+
if (clusterElement instanceof ToolFunction toolFunction) {
508+
return toolFunction.apply(inputParameters, connectionParameters, context);
509+
}
510+
511+
throw new ExecutionException(
512+
"Unsupported cluster element type: " + clusterElement.getClass()
513+
.getName(),
514+
inputParameters, ClusterElementDefinitionErrorType.EXECUTE_PERFORM);
515+
} catch (Exception exception) {
516+
if (exception instanceof ProviderException) {
517+
throw (ProviderException) exception;
518+
}
519+
520+
throw new ExecutionException(
521+
exception, inputParameterMap, ClusterElementDefinitionErrorType.EXECUTE_PERFORM);
522+
}
523+
}
524+
467525
private String executeWorkflowNodeDescription(
468526
String componentName, int componentVersion, String clusterElementName, Map<String, ?> inputParameters,
469527
ClusterElementContext context) {

0 commit comments

Comments
 (0)