Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const useWorkflowLayout = (includeComponents?: string[]) => {
};

const handleCopilotClick = () => {
setContext({mode: MODE.CHAT, parameters: {}, source: Source.WORKFLOW_EDITOR});
setContext({mode: MODE.ASK, parameters: {}, source: Source.WORKFLOW_EDITOR});

if (!copilotPanelOpen) {
setCopilotPanelOpen(!copilotPanelOpen);
Expand Down
2 changes: 1 addition & 1 deletion client/src/shared/components/copilot/CopilotButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const CopilotButton = ({parameters = {}, source}: CopilotButtonProps) => {
const ff_1570 = useFeatureFlagsStore()('ff-1570');

const handleClick = () => {
setContext({mode: MODE.CHAT, parameters, source});
setContext({mode: MODE.ASK, parameters, source});

if (!copilotPanelOpen) {
setCopilotPanelOpen(!copilotPanelOpen);
Expand Down
4 changes: 2 additions & 2 deletions client/src/shared/components/copilot/CopilotPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ const CopilotPanel = () => {
</SelectTrigger>

<SelectContent className="min-w-24">
<SelectItem value={MODE.CHAT}>
<SelectItem value={MODE.ASK}>
<span className="text-muted-foreground">
{MODE.CHAT.charAt(0) + MODE.CHAT.slice(1).toLowerCase()}
{MODE.ASK.charAt(0) + MODE.ASK.slice(1).toLowerCase()}
</span>
</SelectItem>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {create} from 'zustand';
import {devtools} from 'zustand/middleware';

export enum MODE {
CHAT = 'CHAT',
ASK = 'ASK',
BUILD = 'BUILD',
}

Expand Down Expand Up @@ -54,7 +54,7 @@ export const useCopilotStore = create<CopilotStateI>()(
},

context: {
mode: MODE.CHAT,
mode: MODE.ASK,
},
setContext: (context) =>
set((state) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,41 @@

package com.bytechef.ee.ai.copilot.agent;

import com.agui.core.agent.AgentSubscriber;
import com.agui.core.agent.AgentSubscriberParams;
import com.agui.core.agent.RunAgentInput;
import com.agui.core.context.Context;
import com.agui.core.event.BaseEvent;
import com.agui.core.exception.AGUIException;
import com.agui.core.message.AssistantMessage;
import com.agui.core.message.BaseMessage;
import com.agui.core.message.SystemMessage;
import com.agui.core.message.UserMessage;
import com.agui.core.state.State;
import com.agui.server.EventFactory;
import com.agui.server.LocalAgent;
import com.agui.spring.ai.SpringAIAgent;
import com.bytechef.ai.mcp.tool.automation.api.ReadProjectTools;
import com.bytechef.ai.mcp.tool.automation.api.ReadProjectWorkflowTools;
import com.bytechef.ai.mcp.tool.automation.api.ProjectTools;
import com.bytechef.ai.mcp.tool.automation.api.ProjectWorkflowTools;
import com.bytechef.ai.mcp.tool.automation.impl.ReadProjectToolsImpl;
import com.bytechef.ai.mcp.tool.automation.impl.ReadProjectWorkflowToolsImpl;
import com.bytechef.atlas.configuration.domain.Workflow;
import com.bytechef.atlas.configuration.service.WorkflowService;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Function;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.tool.ToolCallback;

/**
Expand All @@ -38,18 +56,39 @@ public class WorkflowEditorSpringAIAgent extends SpringAIAgent {
## Additional Rules

- The assistant must not produce visual representations of any kind, including diagrams, charts, UI sketches, images, or pseudo-visuals.
- When operating in CHAT mode, the assistant must not modify, propose modifications to, or generate new versions of the workflow definition. The assistant may only describe, clarify, or explain.
- When operating in ASK mode, the assistant must not modify, propose modifications to, or generate new versions of the workflow definition. The assistant may only describe, clarify, or explain.
- If a current selected node is available, the assistant must prioritize all answers using that node as the primary context.
- If no node is selected, the assistant must use the broader workflow context as the primary basis for responses.
""";

private final WorkflowService workflowService;
private final List<Object> tools;
private final ProjectTools projectTools;
private final ProjectWorkflowTools projectWorkflowTools;
private final ReadProjectTools readProjectTools;
private final ReadProjectWorkflowTools readProjectWorkflowTools;

private final ChatClient chatClient;
private final ChatMemory chatMemory;
private final List<Advisor> advisors;

protected WorkflowEditorSpringAIAgent(final Builder builder, final WorkflowService workflowService)
throws AGUIException {

super(builder);

this.tools = builder.tools;

this.advisors = builder.advisors;
this.chatMemory = builder.chatMemory;
this.projectTools = (ProjectTools) tools.get(0);
this.projectWorkflowTools = (ProjectWorkflowTools) tools.get(1);

this.readProjectTools = new ReadProjectToolsImpl(projectTools);
this.readProjectWorkflowTools = new ReadProjectWorkflowToolsImpl(projectWorkflowTools);

this.workflowService = workflowService;
this.chatClient = builder.chatClient;
}

public static Builder builder() {
Expand Down Expand Up @@ -79,19 +118,124 @@ protected SystemMessage createSystemMessage(State state, List<Context> contexts)
return systemMessage;
}

@Override
protected void run(RunAgentInput input, AgentSubscriber subscriber) {
try {
State state = input.state();

String mode = (String) state.get("mode");

List<Object> selectedTools = new ArrayList<>(tools);

if (mode.equals("ASK")) {
selectedTools.set(0, readProjectTools);
selectedTools.set(1, readProjectWorkflowTools);
} else if (mode.equals("BUILD")) {
selectedTools.set(0, projectTools);
selectedTools.set(1, projectWorkflowTools);
}

SystemMessage systemMessage = createSystemMessage(state, input.context());

UserMessage userMessage = getLatestUserMessage(input.messages());
String userContent = userMessage.getContent();

AssistantMessage assistantMessage = new AssistantMessage();
String messageId = String.valueOf(UUID.randomUUID());

assistantMessage.setId(messageId);

emitEvent(EventFactory.runStartedEvent(input.runId(), input.threadId()), subscriber);
emitEvent(EventFactory.textMessageStartEvent(messageId, "assistant"), subscriber);

ChatClient.ChatClientRequestSpec chatRequest = chatClient
.prompt(Prompt.builder()
.content(userContent)
.build())
.system(systemMessage.getContent())
.tools(selectedTools.toArray(new Object[0]));

if (advisors != null && !advisors.isEmpty()) {
chatRequest = chatRequest.advisors(spec -> spec.param("chat_memory_conversation_id", input.threadId()));
}

if (Objects.nonNull(this.chatMemory)) {
try {
chatRequest.advisors(
PromptChatMemoryAdvisor.builder(this.chatMemory)
.build());
chatRequest.advisors((a) -> a.param("chat_memory_conversation_id", input.threadId()));
} catch (RuntimeException e) {
throw new AGUIException("Could not add chat memory", e);
}
}

List<BaseEvent> deferredEvents = new ArrayList<>();

chatRequest.stream()
.chatResponse()
.subscribe(
chatResponse -> handleChatResponse(chatResponse, assistantMessage, messageId, subscriber),
error -> handleError(error, subscriber),
() -> handleCompletion(input, assistantMessage, messageId, deferredEvents, subscriber));

} catch (AGUIException e) {
emitEvent(EventFactory.runErrorEvent(e.getMessage()), subscriber);
}
}

private void handleChatResponse(
ChatResponse chatResponse, AssistantMessage assistantMessage, String messageId, AgentSubscriber subscriber) {

String content = chatResponse.getResult()
.getOutput()
.getText();

if (content != null && !content.isEmpty()) {
assistantMessage.setContent(
(assistantMessage.getContent() != null ? assistantMessage.getContent() : "") + content);
emitEvent(EventFactory.textMessageContentEvent(messageId, content), subscriber);
}
}

private void handleCompletion(
RunAgentInput input, AssistantMessage assistantMessage, String messageId, List<BaseEvent> deferredEvents,
AgentSubscriber subscriber) {

emitEvent(EventFactory.textMessageEndEvent(messageId), subscriber);
deferredEvents.forEach(event -> emitEvent(event, subscriber));
subscriber.onNewMessage(assistantMessage);
emitEvent(EventFactory.runFinishedEvent(input.threadId(), input.runId()), subscriber);
subscriber.onRunFinalized(new AgentSubscriberParams(input.messages(), this.state, this, input));
}

private void handleError(Throwable error, AgentSubscriber subscriber) {
emitEvent(EventFactory.runErrorEvent(error.getMessage()), subscriber);
}

public static class Builder extends SpringAIAgent.Builder {

private WorkflowService workflowService;
private List<Object> tools;
private ChatClient chatClient;
private ChatModel chatModel;
private ChatMemory chatMemory;
private List<Advisor> advisors;

@SuppressFBWarnings("EI_EXPOSE_REP2")
public Builder chatModel(ChatModel chatModel) {
super.chatModel(chatModel);
this.chatModel = chatModel;

return this;
}

@SuppressFBWarnings("EI_EXPOSE_REP2")
public Builder advisors(List<Advisor> advisors) {
super.advisors(advisors);

this.advisors = advisors;

return this;
}

Expand All @@ -101,9 +245,12 @@ public Builder advisor(Advisor advisor) {
return this;
}

@SuppressFBWarnings("EI_EXPOSE_REP2")
public Builder tools(List<Object> tools) {
super.tools(tools);

this.tools = tools;

return this;
}

Expand Down Expand Up @@ -149,9 +296,12 @@ public Builder systemMessageProvider(Function<LocalAgent, String> systemMessageP
return this;
}

@SuppressFBWarnings("EI_EXPOSE_REP2")
public Builder chatMemory(ChatMemory chatMemory) {
super.chatMemory(chatMemory);

this.chatMemory = chatMemory;

return this;
}

Expand All @@ -169,6 +319,9 @@ public Builder workflowService(final WorkflowService workflowService) {
}

public WorkflowEditorSpringAIAgent build() throws AGUIException {
this.chatClient = ChatClient.builder(chatModel)
.build();

return new WorkflowEditorSpringAIAgent(this, workflowService);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

import com.agui.core.exception.AGUIException;
import com.agui.core.state.State;
import com.bytechef.ai.mcp.tool.automation.ProjectTools;
import com.bytechef.ai.mcp.tool.automation.ProjectWorkflowTools;
import com.bytechef.ai.mcp.tool.automation.impl.ProjectToolsImpl;
import com.bytechef.ai.mcp.tool.automation.impl.ProjectWorkflowToolsImpl;
import com.bytechef.ai.mcp.tool.platform.TaskTools;
import com.bytechef.atlas.configuration.service.WorkflowService;
import com.bytechef.config.ApplicationProperties;
Expand Down Expand Up @@ -251,8 +251,8 @@ public PgVectorStore vectorStore(

@Bean
WorkflowEditorSpringAIAgent workflowEditorSpringAIAgent(
ChatMemory chatMemory, ChatModel chatModel, ProjectTools projectTools,
ProjectWorkflowTools projectWorkflowTools, TaskTools taskTools, WorkflowService workflowService)
ChatMemory chatMemory, ChatModel chatModel, ProjectToolsImpl projectTools,
ProjectWorkflowToolsImpl projectWorkflowTools, TaskTools taskTools, WorkflowService workflowService)
throws AGUIException {

String name = Source.WORKFLOW_EDITOR.name();
Expand All @@ -263,7 +263,7 @@ WorkflowEditorSpringAIAgent workflowEditorSpringAIAgent(
.chatModel(chatModel)
.systemMessage(getSystemPrompt(systemPromptResource))
.state(new State())
.tools(List.of(taskTools, projectWorkflowTools, projectTools))
.tools(List.of(projectTools, projectWorkflowTools, taskTools))
.workflowService(workflowService)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.bytechef.ai.mcp.server.config;

import com.bytechef.ai.mcp.server.security.web.configurer.ManagementMcpServerSecurityConfigurer;
import com.bytechef.ai.mcp.tool.automation.ProjectTools;
import com.bytechef.ai.mcp.tool.automation.ProjectWorkflowTools;
import com.bytechef.ai.mcp.tool.automation.impl.ProjectToolsImpl;
import com.bytechef.ai.mcp.tool.automation.impl.ProjectWorkflowToolsImpl;
import com.bytechef.ai.mcp.tool.platform.TaskTools;
import com.bytechef.platform.configuration.service.PropertyService;
import com.bytechef.platform.security.service.ApiKeyService;
Expand Down Expand Up @@ -54,13 +54,13 @@
@ConditionalOnProperty(name = "bytechef.mcp.server.enabled", havingValue = "true", matchIfMissing = true)
public class ManagementMcpServerConfiguration {

private final ProjectTools projectTools;
private final ProjectWorkflowTools projectWorkflowTools;
private final ProjectToolsImpl projectTools;
private final ProjectWorkflowToolsImpl projectWorkflowTools;
private final TaskTools taskTools;

@SuppressFBWarnings("EI")
public ManagementMcpServerConfiguration(
ProjectTools projectTools, ProjectWorkflowTools projectWorkflowTools, TaskTools taskTools) {
ProjectToolsImpl projectTools, ProjectWorkflowToolsImpl projectWorkflowTools, TaskTools taskTools) {

this.projectTools = projectTools;
this.projectWorkflowTools = projectWorkflowTools;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2025 ByteChef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.bytechef.ai.mcp.tool.automation.api;

import com.bytechef.ai.mcp.tool.automation.impl.ProjectToolsImpl;
import java.util.List;

/**
* @author Marko Kriskovic
*/
public interface ProjectTools extends ReadProjectTools {

ProjectToolsImpl.ProjectInfo createProject(
String name, String description, Long categoryId, Long workspaceId, List<Long> tagIds);

String deleteProject(long projectId);

ProjectToolsImpl.ProjectInfo updateProject(
long projectId, String name, String description, Long categoryId, List<Long> tagIds);

ProjectToolsImpl.ProjectPublishInfo publishProject(long projectId, String description);
}
Loading
Loading