Skip to content
Open
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 @@ -15,14 +15,22 @@
*/
package io.agentscope.examples.agui.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.agentscope.core.ReActAgent;
import io.agentscope.core.agent.Agent;
import io.agentscope.core.agui.converter.AguiToolConverter;
import io.agentscope.core.agui.model.RunAgentInput;
import io.agentscope.core.formatter.dashscope.DashScopeChatFormatter;
import io.agentscope.core.memory.InMemoryMemory;
import io.agentscope.core.model.DashScopeChatModel;
import io.agentscope.core.model.ToolSchema;
import io.agentscope.core.tool.ToolExecutionContext;
import io.agentscope.core.tool.ToolGroup;
import io.agentscope.core.tool.Toolkit;
import io.agentscope.examples.agui.tools.ExampleTools;
import io.agentscope.examples.agui.tools.UserContext;
import io.agentscope.spring.boot.agui.common.AguiAgentRegistryCustomizer;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand Down Expand Up @@ -54,13 +62,17 @@ public AguiAgentRegistryCustomizer aguiAgentRegistryCustomizer() {

// Example: an agent specialized for calculations
registry.registerFactory("calculator", this::createCalculatorAgent);

// Example: a factory that creates a new agent instance with context
registry.registerFactoryWithInput("context", this::createAgentWithInput);
};

System.out.println("Registered agents with AG-UI registry: default, chat, calculator");
System.out.println("Access agents via:");
System.out.println(" - POST /agui/run (uses default-agent-id from config)");
System.out.println(" - POST /agui/run/chat (uses 'chat' agent)");
System.out.println(" - POST /agui/run with X-Agent-Id header");
System.out.println(" - POST /agui/run/context with context");

return aguiAgentRegistryCustomizer;
}
Expand Down Expand Up @@ -152,6 +164,56 @@ private Agent createCalculatorAgent() {
.build();
}

private static final String TOOL_GROUP_NAME = "agui_tools_group";

private Agent createAgentWithInput(RunAgentInput input) {
String apiKey = getRequiredApiKey();

// Create toolkit with example tools
Toolkit toolkit = new Toolkit();
toolkit.registerTool(new ExampleTools());

AguiToolConverter toolConverter = new AguiToolConverter();
List<ToolSchema> toolSchemas = toolConverter.toToolSchemaList(input.getTools());
if (toolkit.getToolGroup(TOOL_GROUP_NAME) == null) {
toolkit.createToolGroup(TOOL_GROUP_NAME, "Tools for AG-UI", true);
} // 注册到工具组
ToolGroup toolGroup = toolkit.getToolGroup(TOOL_GROUP_NAME);
toolSchemas.forEach(toolSchema -> toolGroup.addTool(toolSchema.getName()));
toolkit.registerSchemas(toolSchemas);

// if (!toolSchemas.isEmpty()) {
// toolkit.registerSchemas(toolSchemas);
// for (ToolSchema toolSchema : toolSchemas) {
// toolkit.addToolToGroup(TOOL_GROUP_NAME, toolSchema.getName());
// }
// }

ObjectMapper om = new ObjectMapper();
ToolExecutionContext.Builder builder = ToolExecutionContext.builder();
UserContext userContext = om.convertValue(input.getForwardedProps(), UserContext.class);
builder.register(userContext);

// Create the agent
return ReActAgent.builder()
.name("AG-UI Assistant")
.sysPrompt(
"You are a helpful AI assistant exposed via the AG-UI protocol. "
+ "You can help users with various tasks including weather queries "
+ "and calculations. Be concise and helpful in your responses.")
.model(
DashScopeChatModel.builder().apiKey(apiKey).modelName("qwen-plus").stream(
true)
.enableThinking(false)
.formatter(new DashScopeChatFormatter())
.build())
.toolkit(toolkit)
.toolExecutionContext(builder.build())
.memory(new InMemoryMemory())
.maxIters(10)
.build();
}

private String getRequiredApiKey() {
String apiKey = System.getenv("DASHSCOPE_API_KEY");
if (apiKey == null || apiKey.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ public ToolResultBlock getCurrentTime() {
return ToolResultBlock.text("Current time: " + now.format(formatter));
}

/**
* Get the current context info.
*
* @return Current context info
*/
@Tool(name = "get_current_context", description = "Get the current context")
public ToolResultBlock getCurrentContext(UserContext context) {

return ToolResultBlock.text(
"Current context: " + context.getContext() + " Current user: " + context.getUser());
}

/**
* Simple expression evaluator.
* Supports basic arithmetic: +, -, *, /
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2024-2026 the original author or authors.
*
* 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
*
* http://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 io.agentscope.examples.agui.tools;

public class UserContext {
private String context;
private String user;

public String getContext() {
return context;
}

public void setContext(String context) {
this.context = context;
}

public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}
}
Loading
Loading