Skip to content

Commit 727c7bd

Browse files
Refactor and enhance file editing tooling to support interactive mode
Key Changes: - Class Renaming: The DiffTools class has been renamed to InteractiveFileEditor to better reflect its expanded functionality and interactive nature. - New Method: The editFile method has been introduced to support interactive file editing, allowing users to create or edit text files within the AI environment. - Enhanced Safety: Added a checkPath method to validate file paths before operations, ensuring safe file access and preventing unauthorized access. - Documentation and Descriptions: Updated class descriptions, tool descriptions, and system messages to clearly communicate the interactive nature of the tool. - turning all agents into AiServices: how lanchain4j handles agents is weird, in particular they do not currently support the same event handling system as for normal AI Services. The latter allow to intercept much more in detail what is going on. - UI improvements, for example gray out DiffPane buttons once accepted/rejected
1 parent c5d033c commit 727c7bd

49 files changed

Lines changed: 1086 additions & 563 deletions

Some content is hidden

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

Main.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import javax.swing.JFrame;
2+
import javax.swing.JLabel;
3+
import java.awt.FlowLayout;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
// Create the main window
8+
JFrame frame = new JFrame("Hello App");
9+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
10+
frame.setSize(300, 150);
11+
12+
// Add a label with "Hello" message
13+
JLabel label = new JLabel("Hello to you!");
14+
frame.getContentPane().setLayout(new FlowLayout());
15+
frame.getContentPane().add(label);
16+
17+
// Display the window
18+
frame.setVisible(true);
19+
}
20+
}

src/main/java/io/github/jeddict/ai/agent/DiffTools.java renamed to src/main/java/io/github/jeddict/ai/agent/InteractiveFileEditor.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@
3131
/**
3232
* A tool for performing diff operations.
3333
*/
34-
public class DiffTools extends AbstractTool {
34+
public class InteractiveFileEditor extends AbstractTool {
3535

3636
private final AssistantChat assistantChat;
3737

38-
public DiffTools(String basedir, AssistantChat assistantChat) throws IOException {
38+
public InteractiveFileEditor(String basedir, AssistantChat assistantChat) throws IOException {
3939
super(basedir);
4040
this.assistantChat = assistantChat;
4141
}
4242

4343
@Tool("""
44-
This is a user friendly editor to create or edit text content. Use it to
44+
This is an interactive editor to create or edit text content. Use it to
4545
create new files or update existing files.
4646
4747
Inputs:
@@ -55,9 +55,11 @@ public DiffTools(String basedir, AssistantChat assistantChat) throws IOException
5555
"""
5656
)
5757
@ToolPolicy(INTERACTIVE)
58-
public String diff(final String path, final String content)
58+
public String editFile(final String path, final String content)
5959
throws ToolExecutionException {
60-
progress("∆ Performing diff on " + path + " with proposed content " + StringUtils.abbreviate(content, 40));
60+
progress("∆ Editing " + path + " with proposed content " + StringUtils.abbreviate(content, 40));
61+
62+
checkPath(path);
6163

6264
final CountDownLatch done = new CountDownLatch(1);
6365
final AtomicReference<String> newContent = new AtomicReference();

src/main/java/io/github/jeddict/ai/agent/pair/CodeAdvisor.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package io.github.jeddict.ai.agent.pair;
1717

18-
import dev.langchain4j.agentic.Agent;
1918
import dev.langchain4j.service.SystemMessage;
2019
import dev.langchain4j.service.UserMessage;
2120
import dev.langchain4j.service.V;
@@ -66,7 +65,6 @@ public interface CodeAdvisor extends PairProgrammer {
6665
//
6766
@SystemMessage(SYSTEM_MESSAGE)
6867
@UserMessage(USER_MESSAGE)
69-
@Agent("Suggest up to 3 names for a variable, method or other elements")
7068
List<String> suggest(
7169
@V("element") final String element, // variable, method, method invocation
7270
@V("classes") final String classes, // related classes and method signatures

src/main/java/io/github/jeddict/ai/agent/pair/DBSpecialist.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package io.github.jeddict.ai.agent.pair;
1717

18-
import dev.langchain4j.agentic.Agent;
1918
import dev.langchain4j.service.SystemMessage;
2019
import dev.langchain4j.service.UserMessage;
2120
import dev.langchain4j.service.V;
@@ -28,24 +27,24 @@
2827
*/
2928
public interface DBSpecialist extends PairProgrammer {
3029

31-
public static final String SYSTEM_MESSAGE = """
32-
You are an experience back-end developer specialized in in writing code to
33-
interact with the database. Based on the provided user prompt, you can:
34-
- analyze the provided metadata and generate a relevant SQL query that addresses the user's inquiry
35-
- include a detailed explanation of the query, clarifying its purpose and how it relates to the developer's question
36-
- ensure that the SQL syntax adheres to the database structure, constraints, and relationships
37-
- the full SQL query should be wrapped in ```sql block
38-
- avoid wrapping individual SQL keywords or table/column names in <code> tags, and do not wrap any partial SQL query segments in <code> tags
39-
- generate the appropriate code and include a clear description of its functionality if the user requests specific code snippets
40-
In any case, take into account the following rules:
41-
{{rules}}
42-
""";
30+
public static final String SYSTEM_MESSAGE =
31+
"""
32+
You are an experience back-end developer specialized in in writing code to
33+
interact with the database. Based on the provided user prompt, you can:
34+
- analyze the provided metadata and generate a relevant SQL query that addresses the user's inquiry
35+
- include a detailed explanation of the query, clarifying its purpose and how it relates to the developer's question
36+
- ensure that the SQL syntax adheres to the database structure, constraints, and relationships
37+
- the full SQL query should be wrapped in ```sql block
38+
- avoid wrapping individual SQL keywords or table/column names in <code> tags, and do not wrap any partial SQL query segments in <code> tags
39+
- generate the appropriate code and include a clear description of its functionality if the user requests specific code snippets
40+
In any case, take into account the following rules:
41+
{{rules}}
42+
""";
4343

4444
public static final String USER_MESSAGE = "Given the below metadata, please answer the prompt:\n{{prompt}}\nMetadata: {{metadata}}";
4545

4646
@SystemMessage(SYSTEM_MESSAGE)
4747
@UserMessage(USER_MESSAGE)
48-
@Agent("Assist with database related prompts")
4948
String suggest(
5049
@V("prompt") final String prompt,
5150
@V("metadata") final String metadata,

src/main/java/io/github/jeddict/ai/agent/pair/DiffSpecialist.java

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package io.github.jeddict.ai.agent.pair;
1717

18-
import dev.langchain4j.agentic.Agent;
1918
import dev.langchain4j.service.SystemMessage;
2019
import dev.langchain4j.service.UserMessage;
2120
import dev.langchain4j.service.V;
@@ -73,64 +72,66 @@ static CodeReviewLevel of(String value) {
7372
}
7473
}
7574

76-
public static final String SYSTEM_MESSAGE = """
77-
You are an experience developer specialized in in writing commit messages or
78-
code review based on user request and provided git diff output.
79-
Based on the provided diff and an optional reference comment, you can:
80-
- create commit messages that reflect business or domain features rather than technical details like dependency updates or refactoring
81-
- create the following versions of the commit message:
82-
- Very short
83-
- Short
84-
- Medium length
85-
- Long
86-
- Descriptive
87-
- review the changes and provide feedback following the bewlow instructions:
88-
- base your review strictly on the provided Git diff
89-
- focus areas:
90-
- bugs, unsafe behavior, or runtime risks
91-
- readability, naming, and clarity
92-
- best practices or conventions
93-
- opportunities for simplification or performance improvement
94-
- anchor each suggestion to a specific hunk header from the diff
95-
- DO NOT infer or hallucinate line numbers not present in the diff
96-
- DO NOT reference line numbers or attempt to estimate exact start/end lines
97-
98-
{{format}}
99-
""";
75+
public static final String SYSTEM_MESSAGE =
76+
"""
77+
You are an experience developer specialized in in writing commit messages or
78+
code review based on user request and provided git diff output.
79+
Based on the provided diff and an optional reference comment, you can:
80+
- create commit messages that reflect business or domain features rather than technical details like dependency updates or refactoring
81+
- create the following versions of the commit message:
82+
- Very short
83+
- Short
84+
- Medium length
85+
- Long
86+
- Descriptive
87+
- review the changes and provide feedback following the bewlow instructions:
88+
- base your review strictly on the provided Git diff
89+
- focus areas:
90+
- bugs, unsafe behavior, or runtime risks
91+
- readability, naming, and clarity
92+
- best practices or conventions
93+
- opportunities for simplification or performance improvement
94+
- anchor each suggestion to a specific hunk header from the diff
95+
- DO NOT infer or hallucinate line numbers not present in the diff
96+
- DO NOT reference line numbers or attempt to estimate exact start/end lines
97+
98+
{{format}}
99+
""";
100100

101101
public static final String USER_MESSAGE = "{{prompt}}\n{{diff}}\nDiff description: {{description}}";
102102

103-
public static final String USER_MESSAGE_COMMENT = """
104-
Provide various types of commit messages (very short, short, medium length, long, descriptive)
105-
based on reference comment and changes below.
106-
""";
107-
108-
public static final String OUTPUT_REVIEW = """
109-
Respond only with a YAML array of review suggestions. Each suggestion must include:
110-
- file: the file name
111-
- hunk: the Git diff hunk header (e.g., "@@ -10,7 +10,9 @@")
112-
- type: one of "security", "warning", "info", or "suggestion"
113-
- "security" for vulnerabilities or high-risk flaws
114-
- "warning" for potential bugs or unsafe behavior
115-
- "info" for minor issues or readability
116-
- "suggestion" for non-critical improvements or refactoring
117-
- title: a short title summarizing the issue
118-
- description: a longer explanation or recommendation
119-
120-
Output raw YAML with no markdown, code block, or extra formatting.
121-
122-
Expected YAML format:
123-
124-
- file: src/com/example/MyService.java
125-
hunk: "@@ -42,6 +42,10 @@"
126-
type: warning
127-
title: "Possible null pointer exception"
128-
description: "The 'items' list might be null before iteration. Add a null check to avoid NPE."
129-
""";
103+
public static final String USER_MESSAGE_COMMENT =
104+
"""
105+
Provide various types of commit messages (very short, short, medium length, long, descriptive)
106+
based on reference comment and changes below.
107+
""";
108+
109+
public static final String OUTPUT_REVIEW =
110+
"""
111+
Respond only with a YAML array of review suggestions. Each suggestion must include:
112+
- file: the file name
113+
- hunk: the Git diff hunk header (e.g., "@@ -10,7 +10,9 @@")
114+
- type: one of "security", "warning", "info", or "suggestion"
115+
- "security" for vulnerabilities or high-risk flaws
116+
- "warning" for potential bugs or unsafe behavior
117+
- "info" for minor issues or readability
118+
- "suggestion" for non-critical improvements or refactoring
119+
- title: a short title summarizing the issue
120+
- description: a longer explanation or recommendation
121+
122+
Output raw YAML with no markdown, code block, or extra formatting.
123+
124+
Expected YAML format:
125+
126+
- file: src/com/example/MyService.java
127+
hunk: "@@ -42,6 +42,10 @@"
128+
type: warning
129+
title: "Possible null pointer exception"
130+
description: "The 'items' list might be null before iteration. Add a null check to avoid NPE."
131+
""";
130132

131133
@SystemMessage(SYSTEM_MESSAGE)
132134
@UserMessage(USER_MESSAGE)
133-
@Agent("Suggest commit messages for the given git diff")
134135
String review(
135136
@V("prompt") final String referenceMessage,
136137
@V("diff") final String diff,

src/main/java/io/github/jeddict/ai/agent/pair/FileWizard.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.github.jeddict.ai.agent.pair;
22

3-
import dev.langchain4j.agentic.Agent;
43
import dev.langchain4j.service.SystemMessage;
54
import dev.langchain4j.service.UserMessage;
65
import dev.langchain4j.service.V;
@@ -13,30 +12,30 @@
1312
*/
1413
public interface FileWizard extends PairProgrammer {
1514

16-
static final String SYSTEM_MESSAGE = """
17-
You are a programmer assistant that can generate a new file with code or other
18-
content based on the request and context provided by the user. Generate the file
19-
content only without any description.
20-
Take into account the following general rules: {{globalRules}}
21-
Take into account the following project rules: {{projectRules}}
22-
""";
15+
static final String SYSTEM_MESSAGE =
16+
"""
17+
You are a programmer assistant that can generate a new file with code or other
18+
content based on the request and context provided by the user. Generate the file
19+
content only without any description.
20+
Take into account the following general rules: {{globalRules}}
21+
Take into account the following project rules: {{projectRules}}
22+
""";
2323

24-
static final String USER_MESSAGE_DEFAULT = """
25-
Generate the content of a new file given the below context information.
26-
""";
24+
static final String USER_MESSAGE_DEFAULT =
25+
"Generate the content of a new file given the below context information";
2726

28-
static final String USER_MESSAGE = """
29-
{{prompt}}
30-
filename: {{filename}}
31-
context: {{context}}
32-
content:
33-
```
34-
{{content}}
35-
```
36-
project info: {{project}}
37-
""";
27+
static final String USER_MESSAGE =
28+
"""
29+
{{prompt}}
30+
filename: {{filename}}
31+
context: {{context}}
32+
content:
33+
```
34+
{{content}}
35+
```
36+
project info: {{project}}
37+
""";
3838

39-
@Agent("Generate content for a new file given the provided context")
4039
@SystemMessage(SYSTEM_MESSAGE)
4140
@UserMessage(USER_MESSAGE)
4241
String _newFile_(

src/main/java/io/github/jeddict/ai/agent/pair/Ghostwriter.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.sun.source.tree.Tree;
44
import com.sun.source.util.TreePath;
5-
import dev.langchain4j.agentic.Agent;
65
import dev.langchain4j.service.SystemMessage;
76
import dev.langchain4j.service.UserMessage;
87
import dev.langchain4j.service.V;
@@ -23,30 +22,33 @@ public interface Ghostwriter extends PairProgrammer {
2322

2423
static final String LANGUAGE_JAVA = "Java";
2524

26-
static final String SYSTEM_MESSAGE = """
27-
You are an expert programmer that can suggest code based on the context of the
28-
program and best practices to write good quality code. Generate context-aware,
29-
and syntactically valid Java code suggestions that naturally fit into the
30-
code surrounding the placeholder ${SUGGESTION} to improve code clarity or functionality.
31-
Keep same formatting, structure and convensions. Add the code at the placeholder
32-
location ${SUGGESTION}
33-
{{format}}
34-
""";
35-
36-
static final String USER_MESSAGE = """
37-
{{message}}
38-
Code language: {{language}}
39-
Current code: {{code}}
40-
Current line: {{line}}
41-
Project classes: {{classes}}
42-
Project info: {{project}}
43-
Hint: {{hint}}
44-
""";
45-
46-
static final String USER_MESSAGE_DEFAULT = """
47-
Suggest additional classes, interfaces, enums, or other top-level constructs.
48-
Ensure that the suggestions fit the context of the entire file.
49-
""";
25+
static final String SYSTEM_MESSAGE =
26+
"""
27+
You are an expert programmer that can suggest code based on the context of the
28+
program and best practices to write good quality code. Generate context-aware,
29+
and syntactically valid Java code suggestions that naturally fit into the
30+
code surrounding the placeholder ${SUGGESTION} to improve code clarity or functionality.
31+
Keep same formatting, structure and convensions. Add the code at the placeholder
32+
location ${SUGGESTION}
33+
{{format}}
34+
""";
35+
36+
static final String USER_MESSAGE =
37+
"""
38+
{{message}}
39+
Code language: {{language}}
40+
Current code: {{code}}
41+
Current line: {{line}}
42+
Project classes: {{classes}}
43+
Project info: {{project}}
44+
Hint: {{hint}}
45+
""";
46+
47+
static final String USER_MESSAGE_DEFAULT =
48+
"""
49+
Suggest additional classes, interfaces, enums, or other top-level constructs.
50+
Ensure that the suggestions fit the context of the entire file.
51+
""";
5052

5153
static final String USER_MESSAGE_COMPILATION_UNIT =
5254
"Suggest package declarations, import statements, comments, or annotations for public class.";
@@ -129,7 +131,6 @@ Make sure to escape any double quotes within the snippet and description using a
129131

130132
@SystemMessage(SYSTEM_MESSAGE)
131133
@UserMessage(USER_MESSAGE)
132-
@Agent("Suggest code for the given content and cotext")
133134
String suggest(
134135
@V("message") final String message, // additional user request
135136
@V("language") final String codeLanguage, // the source code language (based on mime type)

src/main/java/io/github/jeddict/ai/agent/pair/HackerWithTools.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public interface HackerWithTools extends Hacker {
3737
- Identify ambiguities, missing requirements, or assumptions.
3838
2. Plan before acting
3939
- Produce a clear, step-by-step plan outlining how you will solve the problem.
40-
-Explicitly state any assumptions made.
40+
- Explicitly state any assumptions made.
4141
3. Use tools deliberately
4242
- Use the provided tools only when they add value (e.g., gathering information, inspecting files, running code).
4343
4. Implement and iterate

0 commit comments

Comments
 (0)