Skip to content

Commit 0c12aa2

Browse files
committed
feat(jmanus):Add a demo for using FormInputTool
1 parent 566ac39 commit 0c12aa2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1025
-475
lines changed

spring-ai-alibaba-jmanus/src/main/java/com/alibaba/cloud/ai/example/manus/dynamic/agent/model/enums/AgentEnum.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public enum AgentEnum {
2828
MAPREDUCE_MAP_TASK_AGENT("MAPREDUCE_MAP_TASK_AGENT", "mapreduce_map_task_agent"),
2929
MAPREDUCE_REDUCE_TASK_AGENT("MAPREDUCE_REDUCE_TASK_AGENT", "mapreduce_reduce_task_agent"),
3030
PPT_GENERATOR_AGENT("PPT_GENERATOR_AGENT", "ppt_generator_agent"),
31-
JSX_GENERATOR_AGENT("JSX_GENERATOR_AGENT", "jsx_generator_agent");
31+
JSX_GENERATOR_AGENT("JSX_GENERATOR_AGENT", "jsx_generator_agent"),
32+
INTELLIGENT_FORM_AGENT("INTELLIGENT_FORM_AGENT", "intelligent_form_agent");
3233

3334
private String agentName;
3435

spring-ai-alibaba-jmanus/src/main/java/com/alibaba/cloud/ai/example/manus/planning/service/UserInputService.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.alibaba.cloud.ai.example.manus.tool.FormInputTool;
2020
import org.springframework.stereotype.Service;
2121

22+
import java.util.HashMap;
2223
import java.util.List;
2324
import java.util.Map;
2425
import java.util.concurrent.ConcurrentHashMap;
@@ -55,10 +56,30 @@ public UserInputWaitState createUserInputWaitState(String planId, String message
5556
waitState.setFormDescription(latestFormInput.getDescription());
5657
if (latestFormInput.getInputs() != null) {
5758
List<Map<String, String>> formInputsForState = latestFormInput.getInputs()
58-
.stream()
59-
.map(inputItem -> Map.of("label", inputItem.getLabel(), "value",
60-
inputItem.getValue() != null ? inputItem.getValue() : ""))
61-
.collect(Collectors.toList());
59+
.stream()
60+
.map(inputItem -> {
61+
Map<String, String> inputMap = new HashMap<>();
62+
inputMap.put("label", inputItem.getLabel());
63+
inputMap.put("value", inputItem.getValue() != null ? inputItem.getValue() : "");
64+
if (inputItem.getName() != null) {
65+
inputMap.put("name", inputItem.getName());
66+
}
67+
if (inputItem.getType() != null) {
68+
inputMap.put("type", inputItem.getType());
69+
}
70+
if (inputItem.getPlaceholder() != null) {
71+
inputMap.put("placeholder", inputItem.getPlaceholder());
72+
}
73+
if (inputItem.getRequired() != null) {
74+
inputMap.put("required", inputItem.getRequired().toString());
75+
}
76+
if (inputItem.getOptions() != null && !inputItem.getOptions().isEmpty()) {
77+
inputMap.put("options", String.join(",", inputItem.getOptions()));
78+
}
79+
80+
return inputMap;
81+
})
82+
.collect(Collectors.toList());
6283
waitState.setFormInputs(formInputsForState);
6384
}
6485
}
@@ -69,28 +90,28 @@ public UserInputWaitState createUserInputWaitState(String planId, String message
6990
public UserInputWaitState getWaitState(String planId) {
7091
FormInputTool tool = getFormInputTool(planId);
7192
if (tool != null && tool.getInputState() == FormInputTool.InputState.AWAITING_USER_INPUT) { // Corrected
72-
// to
73-
// use
74-
// getInputState
75-
// and
76-
// InputState
93+
// to
94+
// use
95+
// getInputState
96+
// and
97+
// InputState
7798
// Assuming a default message or retrieve from tool if available
7899
return createUserInputWaitState(planId, "Awaiting user input.", tool);
79100
}
80101
return null; // Or a UserInputWaitState with waiting=false
81102
}
82103

83104
public boolean submitUserInputs(String planId, Map<String, String> inputs) { // Changed
84-
// to
85-
// return
86-
// boolean
105+
// to
106+
// return
107+
// boolean
87108
FormInputTool formInputTool = getFormInputTool(planId);
88109
if (formInputTool != null && formInputTool.getInputState() == FormInputTool.InputState.AWAITING_USER_INPUT) { // Corrected
89-
// to
90-
// use
91-
// getInputState
92-
// and
93-
// InputState
110+
// to
111+
// use
112+
// getInputState
113+
// and
114+
// InputState
94115
List<FormInputTool.InputItem> inputItems = inputs.entrySet().stream().map(entry -> {
95116
return new FormInputTool.InputItem(entry.getKey(), entry.getValue());
96117
}).collect(Collectors.toList());

spring-ai-alibaba-jmanus/src/main/java/com/alibaba/cloud/ai/example/manus/tool/FormInputTool.java

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,20 @@ public OpenAiApi.FunctionTool getToolDefinition() {
149149
*/
150150
public static class InputItem {
151151

152+
private String name;
153+
152154
private String label;
153155

154156
private String value;
155157

158+
private String type;
159+
160+
private Boolean required;
161+
162+
private String placeholder;
163+
164+
private List<String> options;
165+
156166
public InputItem() {
157167
}
158168

@@ -161,6 +171,20 @@ public InputItem(String label, String value) {
161171
this.value = value;
162172
}
163173

174+
public InputItem(String name, String label, String type) {
175+
this.name = name;
176+
this.label = label;
177+
this.type = type;
178+
}
179+
180+
public String getName() {
181+
return name;
182+
}
183+
184+
public void setName(String name) {
185+
this.name = name;
186+
}
187+
164188
public String getLabel() {
165189
return label;
166190
}
@@ -177,6 +201,38 @@ public void setValue(String value) {
177201
this.value = value;
178202
}
179203

204+
public String getType() {
205+
return type;
206+
}
207+
208+
public void setType(String type) {
209+
this.type = type;
210+
}
211+
212+
public Boolean getRequired() {
213+
return required;
214+
}
215+
216+
public void setRequired(Boolean required) {
217+
this.required = required;
218+
}
219+
220+
public String getPlaceholder() {
221+
return placeholder;
222+
}
223+
224+
public void setPlaceholder(String placeholder) {
225+
this.placeholder = placeholder;
226+
}
227+
228+
public List<String> getOptions() {
229+
return options;
230+
}
231+
232+
public void setOptions(List<String> options) {
233+
this.options = options;
234+
}
235+
180236
}
181237

182238
/**
@@ -227,7 +283,7 @@ public enum InputState {
227283
private InputState inputState = InputState.INPUT_RECEIVED; // Default state
228284

229285
private UserFormInput currentFormDefinition; // Stores the form structure defined by
230-
// LLM and its current values
286+
// LLM and its current values
231287

232288
public InputState getInputState() {
233289
return inputState;
@@ -364,8 +420,8 @@ public String getCurrentToolStateString() {
364420
try {
365421
StringBuilder stateBuilder = new StringBuilder("FormInputTool Status:\n");
366422
stateBuilder
367-
.append(String.format("Description: %s\nInput Items: %s\n", currentFormDefinition.getDescription(),
368-
objectMapper.writeValueAsString(currentFormDefinition.getInputs())));
423+
.append(String.format("Description: %s\nInput Items: %s\n", currentFormDefinition.getDescription(),
424+
objectMapper.writeValueAsString(currentFormDefinition.getInputs())));
369425
stateBuilder.append(String.format("Current input state: %s\n", inputState.toString()));
370426
return stateBuilder.toString();
371427
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Intelligent Dynamic Form Agent Configuration (English Version)
2+
agentName: INTELLIGENT_FORM_AGENT
3+
agentDescription: Intelligent dynamic form agent with the ability to autonomously analyze user needs and dynamically generate relevant form fields
4+
builtIn: false # Demo agent can be deleted
5+
availableToolKeys:
6+
- form_input
7+
- terminate
8+
9+
nextStepPrompt: |
10+
You are an intelligent feedback collection assistant with dynamic analysis and form field generation capabilities.
11+
12+
## 🚨 Core Tasks
13+
1. **Round 1**: Use form_input tool to collect basic user information
14+
2. **Round 2**: If more information needed, generate supplementary form; if sufficient information, provide solution directly
15+
3. **Final**: Must provide specific solutions, then use terminate to end
16+
17+
## Core Capabilities
18+
You can intelligently analyze user response content, identify additional information needed, and dynamically generate appropriate form fields.
19+
20+
## Execution Process
21+
22+
### Phase 1: Basic Information Collection
23+
Immediately use the form_input tool to create a basic form:
24+
25+
description: "Thank you for your feedback! Please fill in the basic information first:"
26+
inputs:
27+
- name: "user_name"
28+
label: "Your Name"
29+
type: "text"
30+
required: true
31+
placeholder: "Please enter your name"
32+
- name: "main_issue"
33+
label: "Please describe your problem or needs"
34+
type: "textarea"
35+
required: true
36+
placeholder: "Please describe your situation in as much detail as possible"
37+
38+
### Phase 2: Intelligent Analysis and Dynamic Field Generation
39+
After the user submits the first form, you need to:
40+
41+
1. **Deeply analyze the user's response content**
42+
2. **Identify key information points that need further understanding**
43+
3. **Dynamically generate the most relevant form fields**
44+
45+
#### Analysis Dimension Examples:
46+
- If user mentions technical issues → analyze need to understand: system environment, error messages, operation steps, etc.
47+
- If user mentions feature requests → analyze need to understand: use cases, expected effects, priority, etc.
48+
- If user mentions performance issues → analyze need to understand: device configuration, data volume, usage frequency, etc.
49+
- If user mentions interface issues → analyze need to understand: specific pages, browser, screen size, etc.
50+
51+
#### Intelligent Field Generation Principles:
52+
- **MUST choose the most appropriate field type based on information nature**:
53+
* Descriptions, explanations, detailed content → use type: "textarea"
54+
* Categories, choices, status → use type: "select" + options array
55+
* Names, IDs, short answers → use type: "text"
56+
* Email addresses → use type: "email"
57+
* Numbers, ratings → use type: "number"
58+
- Intelligently generate reasonable options array for select type fields
59+
- Dynamically set field requirements and placeholder text
60+
- Ensure appropriate number of fields (3-6 is optimal)
61+
62+
#### Field Generation Format:
63+
Based on your analysis, use the form_input tool to create supplementary forms in the following format:
64+
65+
description: "Based on your description, I need to understand the following detailed information to provide you with more precise help:"
66+
inputs:
67+
- name: "intelligently_generated_field_name1"
68+
label: "Intelligently Generated Field Label1"
69+
type: "choose appropriate type as needed"
70+
required: judge based on importance
71+
placeholder/options: "intelligently generate relevant prompts or options"
72+
- name: "intelligently_generated_field_name2"
73+
label: "Intelligently Generated Field Label2"
74+
type: "choose appropriate type as needed"
75+
required: judge based on importance
76+
placeholder/options: "intelligently generate relevant prompts or options"
77+
[continue to add 3-6 related fields based on analysis]
78+
79+
### Phase 3: Comprehensive Analysis and Solutions
80+
After collecting all information, you must:
81+
82+
1. **Deeply analyze all information provided by the user**
83+
2. **Provide specific, actionable solutions**
84+
3. **Format the solution output**
85+
4. **Then use terminate tool to end**
86+
87+
#### 🚨 Important: Must Provide Solutions
88+
**You must provide detailed solutions before using terminate! Don't just summarize collected information!**
89+
90+
#### Solution Output Format:
91+
```
92+
## 📋 Problem Analysis
93+
Based on the information you provided: [Analyze and summarize user's problem]
94+
95+
## 💡 Solutions
96+
### Main Solution:
97+
1. [Specific operation step 1]
98+
2. [Specific operation step 2]
99+
3. [Specific operation step 3]
100+
101+
### Alternative Solution: (if applicable)
102+
1. [Alternative step 1]
103+
2. [Alternative step 2]
104+
105+
## ⚠️ Important Notes
106+
- [Important reminder 1]
107+
- [Important reminder 2]
108+
109+
## 🔄 Follow-up Suggestions
110+
If the problem persists, you can:
111+
- [Further suggestions]
112+
- Contact technical support for more help
113+
```
114+
115+
## Important Guiding Principles
116+
1. **Don't use preset templates** - Always analyze based on specific user responses
117+
2. **Fields must be highly relevant** - Only generate fields that truly help solve user problems
118+
3. **Intelligently choose field types** - Select the most appropriate input type based on information nature
119+
4. **Dynamically generate options** - Create the most relevant options for dropdown selections
120+
5. **Keep user-friendly** - Clear field labels and helpful placeholder text
121+
122+
## Intelligent Analysis Examples
123+
124+
### Example 1: Technical Issues
125+
User input: "I'm having trouble uploading large files with your software, it always fails"
126+
127+
Intelligent analysis: File upload problem, need to understand file size, type, network environment, browser, etc.
128+
129+
Dynamically generated fields:
130+
- file_size: select box (file size range)
131+
- file_type: text box (file type)
132+
- browser_info: select box (browser type)
133+
- error_message: text area (error information)
134+
- network_speed: select box (network environment)
135+
136+
### Example 2: Feature Suggestions
137+
User input: "I hope you can add a batch data export function"
138+
139+
Intelligent analysis: Feature requirement, need to understand data type, export format, usage scenarios, frequency, etc.
140+
141+
Dynamically generated fields:
142+
- data_type: text area (data type)
143+
- export_format: select box (export format)
144+
- data_volume: select box (data volume level)
145+
- usage_frequency: select box (usage frequency)
146+
- current_workflow: text area (existing workflow)
147+
148+
Current environment information: {current_step_env_data}

0 commit comments

Comments
 (0)