evoloop-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- evoloop_cli/__init__.py +3 -0
- evoloop_cli/__main__.py +10 -0
- evoloop_cli/_skills/evoloop-constitution/SKILL.md +59 -0
- evoloop_cli/_skills/evoloop-constitution/references/writing-guide.md +89 -0
- evoloop_cli/_skills/evoloop-constitution/templates/config.yaml +52 -0
- evoloop_cli/_skills/evoloop-constitution/templates/constitution.md +39 -0
- evoloop_cli/_skills/evoloop-evaluate/SKILL.md +85 -0
- evoloop_cli/_skills/evoloop-evaluate/schemas/parsed_report.schema.json +170 -0
- evoloop_cli/_skills/evoloop-evaluate/scripts/parse_junit_report.py +311 -0
- evoloop_cli/_skills/evoloop-evaluate/scripts/parse_pytest_report.py +276 -0
- evoloop_cli/_skills/evoloop-evaluate/scripts/parse_report.py +101 -0
- evoloop_cli/_skills/evoloop-evaluate/scripts/update_eval_index.py +111 -0
- evoloop_cli/_skills/evoloop-evaluate/scripts/validate_parsed_report.py +178 -0
- evoloop_cli/_skills/evoloop-evaluate/templates/analysis.md +51 -0
- evoloop_cli/_skills/evoloop-evaluate/templates/progress.md +42 -0
- evoloop_cli/_skills/evoloop-evaluate/templates/tasks.md +34 -0
- evoloop_cli/_skills/evoloop-evolve/SKILL.md +76 -0
- evoloop_cli/_skills/evoloop-evolve/scripts/evolve_state.py +333 -0
- evoloop_cli/_skills/evoloop-implement/SKILL.md +57 -0
- evoloop_cli/_skills/evoloop-implement/scripts/check_scope.py +154 -0
- evoloop_cli/_skills/evoloop-implement/scripts/commit_section.py +78 -0
- evoloop_cli/_skills/evoloop-init/SKILL.md +95 -0
- evoloop_cli/_skills/evoloop-init/scripts/scaffold_evals.py +546 -0
- evoloop_cli/_skills/evoloop-init/templates/common/evals/datasets/domain_flows.yaml +33 -0
- evoloop_cli/_skills/evoloop-init/templates/common/evaluations/README.md +9 -0
- evoloop_cli/_skills/evoloop-init/templates/java/src/test/java/__PACKAGE_PATH__/evals/AgentAdapter.java +13 -0
- evoloop_cli/_skills/evoloop-init/templates/java/src/test/java/__PACKAGE_PATH__/evals/AgenticFlowEvalTest.java +71 -0
- evoloop_cli/_skills/evoloop-init/templates/java/src/test/java/__PACKAGE_PATH__/evals/DeterministicEvaluators.java +174 -0
- evoloop_cli/_skills/evoloop-init/templates/java/src/test/java/__PACKAGE_PATH__/evals/EvalFrameworkTest.java +45 -0
- evoloop_cli/_skills/evoloop-init/templates/java/src/test/java/__PACKAGE_PATH__/evals/FlowRunResult.java +25 -0
- evoloop_cli/_skills/evoloop-init/templates/python/evals/__init__.py +1 -0
- evoloop_cli/_skills/evoloop-init/templates/python/evals/adapter.py +63 -0
- evoloop_cli/_skills/evoloop-init/templates/python/evals/deepagents_adapter.py +223 -0
- evoloop_cli/_skills/evoloop-init/templates/python/evals/evaluators/__init__.py +1 -0
- evoloop_cli/_skills/evoloop-init/templates/python/evals/evaluators/deterministic.py +234 -0
- evoloop_cli/_skills/evoloop-init/templates/python/evals/evaluators/llm_judge.py +189 -0
- evoloop_cli/_skills/evoloop-init/templates/python/evals/fakes.py +26 -0
- evoloop_cli/_skills/evoloop-init/templates/python/evals/harness.py +100 -0
- evoloop_cli/_skills/evoloop-init/templates/python/tests/conftest.py +38 -0
- evoloop_cli/_skills/evoloop-init/templates/python/tests/test_agentic_flows.py +36 -0
- evoloop_cli/_skills/evoloop-init/templates/python/tests/test_eval_framework.py +175 -0
- evoloop_cli/_skills/evoloop-new/SKILL.md +125 -0
- evoloop_cli/_skills/evoloop-new/scripts/scaffold_agent.py +911 -0
- evoloop_cli/_skills/evoloop-new/templates/common/.evoloop/constitution.md +34 -0
- evoloop_cli/_skills/evoloop-new/templates/common/.gitignore +22 -0
- evoloop_cli/_skills/evoloop-new/templates/common/AGENTS.md +17 -0
- evoloop_cli/_skills/evoloop-new/templates/common/README.md +83 -0
- evoloop_cli/_skills/evoloop-new/templates/common/skills/__DOMAIN__/SKILL.md +21 -0
- evoloop_cli/_skills/evoloop-new/templates/java/eval_wiring/src/test/java/__PACKAGE_PATH__/evals/AgenticFlowEvalTest.java +113 -0
- evoloop_cli/_skills/evoloop-new/templates/java/eval_wiring/src/test/java/__PACKAGE_PATH__/evals/EvalFrameworkTest.java +85 -0
- evoloop_cli/_skills/evoloop-new/templates/java/eval_wiring/src/test/java/__PACKAGE_PATH__/evals/Fake__DOMAIN_PASCAL__Client.java +37 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/custom/src/main/java/__PACKAGE_PATH__/Application.java +46 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/custom/src/main/java/__PACKAGE_PATH__/OpenAiCompatibleRunner.java +433 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/custom/src/main/java/__PACKAGE_PATH__/__DOMAIN_PASCAL__Agent.java +125 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/langchain4j/src/main/java/__PACKAGE_PATH__/Application.java +46 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/langchain4j/src/main/java/__PACKAGE_PATH__/__DOMAIN_PASCAL__Agent.java +163 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/langchain4j/src/main/java/__PACKAGE_PATH__/__DOMAIN_PASCAL__Tools.java +18 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/micronaut/src/main/java/__PACKAGE_PATH__/Application.java +15 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/micronaut/src/main/java/__PACKAGE_PATH__/__DOMAIN_PASCAL__Agent.java +167 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/micronaut/src/main/java/__PACKAGE_PATH__/__DOMAIN_PASCAL__Controller.java +41 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/micronaut/src/main/java/__PACKAGE_PATH__/__DOMAIN_PASCAL__Factory.java +32 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/micronaut/src/main/java/__PACKAGE_PATH__/__DOMAIN_PASCAL__Tools.java +20 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/micronaut/src/main/resources/application.yml +17 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/spring-ai/src/main/java/__PACKAGE_PATH__/Application.java +17 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/spring-ai/src/main/java/__PACKAGE_PATH__/__DOMAIN_PASCAL__Agent.java +159 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/spring-ai/src/main/java/__PACKAGE_PATH__/__DOMAIN_PASCAL__Config.java +32 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/spring-ai/src/main/java/__PACKAGE_PATH__/__DOMAIN_PASCAL__Controller.java +40 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/spring-ai/src/main/java/__PACKAGE_PATH__/__DOMAIN_PASCAL__Tools.java +25 -0
- evoloop_cli/_skills/evoloop-new/templates/java/frameworks/spring-ai/src/main/resources/application.yml +19 -0
- evoloop_cli/_skills/evoloop-new/templates/java/gradle/build_custom.gradle +33 -0
- evoloop_cli/_skills/evoloop-new/templates/java/gradle/build_langchain4j.gradle +40 -0
- evoloop_cli/_skills/evoloop-new/templates/java/gradle/build_micronaut.gradle +52 -0
- evoloop_cli/_skills/evoloop-new/templates/java/gradle/build_spring-ai.gradle +45 -0
- evoloop_cli/_skills/evoloop-new/templates/java/gradle/settings.gradle +1 -0
- evoloop_cli/_skills/evoloop-new/templates/java/maven/pom_custom.xml +57 -0
- evoloop_cli/_skills/evoloop-new/templates/java/maven/pom_langchain4j.xml +87 -0
- evoloop_cli/_skills/evoloop-new/templates/java/maven/pom_micronaut.xml +101 -0
- evoloop_cli/_skills/evoloop-new/templates/java/maven/pom_spring-ai.xml +61 -0
- evoloop_cli/_skills/evoloop-new/templates/java/src/main/java/__PACKAGE_PATH__/ConversationMemory.java +36 -0
- evoloop_cli/_skills/evoloop-new/templates/java/src/main/java/__PACKAGE_PATH__/Default__DOMAIN_PASCAL__Client.java +9 -0
- evoloop_cli/_skills/evoloop-new/templates/java/src/main/java/__PACKAGE_PATH__/DomainRecord.java +10 -0
- evoloop_cli/_skills/evoloop-new/templates/java/src/main/java/__PACKAGE_PATH__/TurnOutcome.java +15 -0
- evoloop_cli/_skills/evoloop-new/templates/java/src/main/java/__PACKAGE_PATH__/__DOMAIN_PASCAL__Client.java +8 -0
- evoloop_cli/_skills/evoloop-new/templates/python/eval_wiring/evals/fakes.py +22 -0
- evoloop_cli/_skills/evoloop-new/templates/python/eval_wiring/evals/harness_custom.py +186 -0
- evoloop_cli/_skills/evoloop-new/templates/python/eval_wiring/evals/harness_deepagents.py +100 -0
- evoloop_cli/_skills/evoloop-new/templates/python/eval_wiring/tests/test_eval_framework.py +196 -0
- evoloop_cli/_skills/evoloop-new/templates/python/pyproject.toml +18 -0
- evoloop_cli/_skills/evoloop-new/templates/python/src/__PACKAGE__/__init__.py +13 -0
- evoloop_cli/_skills/evoloop-new/templates/python/src/__PACKAGE__/__main__.py +6 -0
- evoloop_cli/_skills/evoloop-new/templates/python/src/__PACKAGE__/agent_custom.py +209 -0
- evoloop_cli/_skills/evoloop-new/templates/python/src/__PACKAGE__/agent_deepagents.py +61 -0
- evoloop_cli/_skills/evoloop-new/templates/python/src/__PACKAGE__/cli.py +71 -0
- evoloop_cli/_skills/evoloop-new/templates/python/src/__PACKAGE__/client.py +31 -0
- evoloop_cli/_skills/evoloop-scenarios/SKILL.md +49 -0
- evoloop_cli/_skills/evoloop-scenarios/scripts/validate_scenarios.py +261 -0
- evoloop_cli/_skills/evoloop-scenarios/templates/scenario_schema.md +49 -0
- evoloop_cli/cli.py +331 -0
- evoloop_cli-0.1.0.dist-info/METADATA +214 -0
- evoloop_cli-0.1.0.dist-info/RECORD +103 -0
- evoloop_cli-0.1.0.dist-info/WHEEL +4 -0
- evoloop_cli-0.1.0.dist-info/entry_points.txt +2 -0
- evoloop_cli-0.1.0.dist-info/licenses/LICENSE +202 -0
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
package __JAVA_PACKAGE__;
|
|
2
|
+
|
|
3
|
+
import java.io.IOException;
|
|
4
|
+
import java.net.URI;
|
|
5
|
+
import java.net.http.HttpClient;
|
|
6
|
+
import java.net.http.HttpRequest;
|
|
7
|
+
import java.net.http.HttpResponse;
|
|
8
|
+
import java.time.Duration;
|
|
9
|
+
import java.util.ArrayList;
|
|
10
|
+
import java.util.LinkedHashMap;
|
|
11
|
+
import java.util.List;
|
|
12
|
+
import java.util.Map;
|
|
13
|
+
import java.util.Set;
|
|
14
|
+
import java.util.function.BiFunction;
|
|
15
|
+
import java.util.function.Function;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Pure-JDK 17 OpenAI-compatible Chat Completions tool-calling runner backed by an
|
|
19
|
+
* order-independent recursive-descent JSON parser (zero external runtime dependencies).
|
|
20
|
+
*/
|
|
21
|
+
public class OpenAiCompatibleRunner {
|
|
22
|
+
|
|
23
|
+
private static final int MAX_TOOL_ROUNDS = 4;
|
|
24
|
+
|
|
25
|
+
private final String baseUrl;
|
|
26
|
+
private final String model;
|
|
27
|
+
private final String apiKey;
|
|
28
|
+
private final Function<String, String> transport;
|
|
29
|
+
|
|
30
|
+
public OpenAiCompatibleRunner(String baseUrl, String model, String apiKey) {
|
|
31
|
+
this(baseUrl, model, apiKey, null);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public OpenAiCompatibleRunner(
|
|
35
|
+
String baseUrl,
|
|
36
|
+
String model,
|
|
37
|
+
String apiKey,
|
|
38
|
+
Function<String, String> transport) {
|
|
39
|
+
this.baseUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl;
|
|
40
|
+
this.model = model;
|
|
41
|
+
this.apiKey = apiKey;
|
|
42
|
+
this.transport = transport != null ? transport : this::postHttp;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@SuppressWarnings("unchecked")
|
|
46
|
+
public TurnOutcome executeTurn(
|
|
47
|
+
String systemPrompt,
|
|
48
|
+
List<ConversationMemory.Message> history,
|
|
49
|
+
String userInput,
|
|
50
|
+
Set<String> toolNames,
|
|
51
|
+
BiFunction<String, String, String> toolExecutor) {
|
|
52
|
+
List<String> messagesJson = new ArrayList<>();
|
|
53
|
+
messagesJson.add(roleContentJson("system", systemPrompt));
|
|
54
|
+
for (ConversationMemory.Message m : history) {
|
|
55
|
+
messagesJson.add(roleContentJson(m.role(), m.content()));
|
|
56
|
+
}
|
|
57
|
+
messagesJson.add(roleContentJson("user", userInput));
|
|
58
|
+
|
|
59
|
+
List<TurnOutcome.ToolInvocation> captured = new ArrayList<>();
|
|
60
|
+
String finalResponse = "";
|
|
61
|
+
|
|
62
|
+
for (int round = 0; round <= MAX_TOOL_ROUNDS; round++) {
|
|
63
|
+
String requestBody = buildRequestBody(messagesJson, toolNames);
|
|
64
|
+
String responseBody = transport.apply(requestBody);
|
|
65
|
+
|
|
66
|
+
Object rootObj = MiniJson.parse(responseBody);
|
|
67
|
+
Map<String, Object> message = extractFirstChoiceMessage(rootObj);
|
|
68
|
+
List<ParsedToolCall> parsedCalls = extractToolCalls(message);
|
|
69
|
+
|
|
70
|
+
if (!parsedCalls.isEmpty()) {
|
|
71
|
+
messagesJson.add(assistantToolCallsJson(parsedCalls));
|
|
72
|
+
for (ParsedToolCall tc : parsedCalls) {
|
|
73
|
+
Map<String, Object> args = parseArgumentsObject(tc.argumentsRaw());
|
|
74
|
+
String query = args.containsKey("query") ? String.valueOf(args.get("query")) : "";
|
|
75
|
+
captured.add(new TurnOutcome.ToolInvocation(tc.name(), args));
|
|
76
|
+
String result = toolExecutor.apply(tc.name(), query);
|
|
77
|
+
messagesJson.add(
|
|
78
|
+
"{\"role\":\"tool\",\"tool_call_id\":" + jsonString(tc.id())
|
|
79
|
+
+ ",\"name\":" + jsonString(tc.name())
|
|
80
|
+
+ ",\"content\":" + jsonString(result) + "}");
|
|
81
|
+
}
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
Object contentObj = message.get("content");
|
|
86
|
+
finalResponse = contentObj == null ? "" : String.valueOf(contentObj).trim();
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return new TurnOutcome(finalResponse, List.copyOf(captured), false);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private String postHttp(String jsonBody) {
|
|
94
|
+
if (!baseUrl.startsWith("http://") && !baseUrl.startsWith("https://")) {
|
|
95
|
+
throw new IllegalArgumentException(
|
|
96
|
+
"Invalid LLM base URL '" + baseUrl + "' (expected http:// or https:// scheme).");
|
|
97
|
+
}
|
|
98
|
+
HttpClient http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(15)).build();
|
|
99
|
+
HttpRequest req = HttpRequest.newBuilder()
|
|
100
|
+
.uri(URI.create(baseUrl + "/chat/completions"))
|
|
101
|
+
.timeout(Duration.ofSeconds(60))
|
|
102
|
+
.header("Content-Type", "application/json")
|
|
103
|
+
.header("Authorization", "Bearer " + apiKey)
|
|
104
|
+
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
|
|
105
|
+
.build();
|
|
106
|
+
try {
|
|
107
|
+
HttpResponse<String> resp = http.send(req, HttpResponse.BodyHandlers.ofString());
|
|
108
|
+
if (resp.statusCode() >= 400) {
|
|
109
|
+
throw new IllegalStateException(
|
|
110
|
+
"LLM endpoint returned HTTP " + resp.statusCode() + ": " + resp.body());
|
|
111
|
+
}
|
|
112
|
+
return resp.body();
|
|
113
|
+
} catch (IOException | InterruptedException exc) {
|
|
114
|
+
if (exc instanceof InterruptedException) {
|
|
115
|
+
Thread.currentThread().interrupt();
|
|
116
|
+
}
|
|
117
|
+
throw new IllegalStateException("LLM endpoint unreachable at " + baseUrl + ": " + exc.getMessage(), exc);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private String buildRequestBody(List<String> messagesJson, Set<String> toolNames) {
|
|
122
|
+
StringBuilder tools = new StringBuilder("[");
|
|
123
|
+
boolean first = true;
|
|
124
|
+
for (String name : toolNames) {
|
|
125
|
+
if (!first) {
|
|
126
|
+
tools.append(',');
|
|
127
|
+
}
|
|
128
|
+
first = false;
|
|
129
|
+
tools.append("{\"type\":\"function\",\"function\":{\"name\":")
|
|
130
|
+
.append(jsonString(name))
|
|
131
|
+
.append(",\"description\":")
|
|
132
|
+
.append(jsonString("Run " + name.replace('_', ' ') + " for a concrete query identifier."))
|
|
133
|
+
.append(",\"parameters\":{\"type\":\"object\",\"properties\":{\"query\":{\"type\":\"string\"}},\"required\":[\"query\"]}}}");
|
|
134
|
+
}
|
|
135
|
+
tools.append(']');
|
|
136
|
+
|
|
137
|
+
return "{\"model\":" + jsonString(model)
|
|
138
|
+
+ ",\"temperature\":0"
|
|
139
|
+
+ ",\"messages\":[" + String.join(",", messagesJson) + "]"
|
|
140
|
+
+ ",\"tools\":" + tools + "}";
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private static String roleContentJson(String role, String content) {
|
|
144
|
+
return "{\"role\":" + jsonString(role) + ",\"content\":" + jsonString(content) + "}";
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private static String assistantToolCallsJson(List<ParsedToolCall> calls) {
|
|
148
|
+
StringBuilder sb = new StringBuilder("{\"role\":\"assistant\",\"content\":\"\",\"tool_calls\":[");
|
|
149
|
+
for (int i = 0; i < calls.size(); i++) {
|
|
150
|
+
if (i > 0) {
|
|
151
|
+
sb.append(',');
|
|
152
|
+
}
|
|
153
|
+
ParsedToolCall tc = calls.get(i);
|
|
154
|
+
String argsStr = tc.argumentsRaw() instanceof String s ? s : toJson(tc.argumentsRaw());
|
|
155
|
+
sb.append("{\"id\":").append(jsonString(tc.id()))
|
|
156
|
+
.append(",\"type\":\"function\",\"function\":{\"name\":").append(jsonString(tc.name()))
|
|
157
|
+
.append(",\"arguments\":").append(jsonString(argsStr))
|
|
158
|
+
.append("}}");
|
|
159
|
+
}
|
|
160
|
+
sb.append("]}");
|
|
161
|
+
return sb.toString();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private record ParsedToolCall(String id, String name, Object argumentsRaw) {}
|
|
165
|
+
|
|
166
|
+
@SuppressWarnings("unchecked")
|
|
167
|
+
private static Map<String, Object> extractFirstChoiceMessage(Object rootObj) {
|
|
168
|
+
if (!(rootObj instanceof Map<?, ?> root)) {
|
|
169
|
+
return Map.of();
|
|
170
|
+
}
|
|
171
|
+
Object choicesObj = root.get("choices");
|
|
172
|
+
if (!(choicesObj instanceof List<?> choices) || choices.isEmpty()) {
|
|
173
|
+
return Map.of();
|
|
174
|
+
}
|
|
175
|
+
Object first = choices.get(0);
|
|
176
|
+
if (first instanceof Map<?, ?> choice && choice.get("message") instanceof Map<?, ?> msg) {
|
|
177
|
+
return (Map<String, Object>) msg;
|
|
178
|
+
}
|
|
179
|
+
return Map.of();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private static List<ParsedToolCall> extractToolCalls(Map<String, Object> message) {
|
|
183
|
+
Object tcObj = message.get("tool_calls");
|
|
184
|
+
if (!(tcObj instanceof List<?> rawList) || rawList.isEmpty()) {
|
|
185
|
+
return List.of();
|
|
186
|
+
}
|
|
187
|
+
List<ParsedToolCall> result = new ArrayList<>();
|
|
188
|
+
int idx = 0;
|
|
189
|
+
for (Object item : rawList) {
|
|
190
|
+
idx++;
|
|
191
|
+
if (!(item instanceof Map<?, ?> tcMap)) {
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
Object fnObj = tcMap.get("function");
|
|
195
|
+
if (!(fnObj instanceof Map<?, ?> fnMap)) {
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
Object nameObj = fnMap.get("name");
|
|
199
|
+
if (nameObj == null) {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
String id = tcMap.get("id") != null ? String.valueOf(tcMap.get("id")) : "call_" + idx;
|
|
203
|
+
String name = String.valueOf(nameObj);
|
|
204
|
+
Object argsRaw = fnMap.containsKey("arguments") ? fnMap.get("arguments") : "{}";
|
|
205
|
+
result.add(new ParsedToolCall(id, name, argsRaw));
|
|
206
|
+
}
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
@SuppressWarnings("unchecked")
|
|
211
|
+
private static Map<String, Object> parseArgumentsObject(Object rawArgs) {
|
|
212
|
+
if (rawArgs instanceof Map<?, ?> map) {
|
|
213
|
+
Map<String, Object> copy = new LinkedHashMap<>();
|
|
214
|
+
map.forEach((k, v) -> copy.put(String.valueOf(k), v));
|
|
215
|
+
return copy;
|
|
216
|
+
}
|
|
217
|
+
if (rawArgs instanceof String s && !s.isBlank()) {
|
|
218
|
+
try {
|
|
219
|
+
Object parsed = MiniJson.parse(s);
|
|
220
|
+
if (parsed instanceof Map<?, ?> map) {
|
|
221
|
+
return (Map<String, Object>) map;
|
|
222
|
+
}
|
|
223
|
+
} catch (IllegalArgumentException ignored) {
|
|
224
|
+
return Map.of();
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return Map.of();
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
static String toJson(Object value) {
|
|
231
|
+
if (value == null) {
|
|
232
|
+
return "null";
|
|
233
|
+
}
|
|
234
|
+
if (value instanceof String s) {
|
|
235
|
+
return jsonString(s);
|
|
236
|
+
}
|
|
237
|
+
if (value instanceof Number || value instanceof Boolean) {
|
|
238
|
+
return String.valueOf(value);
|
|
239
|
+
}
|
|
240
|
+
if (value instanceof Map<?, ?> map) {
|
|
241
|
+
StringBuilder sb = new StringBuilder("{");
|
|
242
|
+
boolean first = true;
|
|
243
|
+
for (Map.Entry<?, ?> entry : map.entrySet()) {
|
|
244
|
+
if (!first) {
|
|
245
|
+
sb.append(',');
|
|
246
|
+
}
|
|
247
|
+
first = false;
|
|
248
|
+
sb.append(jsonString(String.valueOf(entry.getKey()))).append(':').append(toJson(entry.getValue()));
|
|
249
|
+
}
|
|
250
|
+
return sb.append('}').toString();
|
|
251
|
+
}
|
|
252
|
+
if (value instanceof List<?> list) {
|
|
253
|
+
StringBuilder sb = new StringBuilder("[");
|
|
254
|
+
for (int i = 0; i < list.size(); i++) {
|
|
255
|
+
if (i > 0) {
|
|
256
|
+
sb.append(',');
|
|
257
|
+
}
|
|
258
|
+
sb.append(toJson(list.get(i)));
|
|
259
|
+
}
|
|
260
|
+
return sb.append(']').toString();
|
|
261
|
+
}
|
|
262
|
+
return jsonString(String.valueOf(value));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
static String jsonString(String value) {
|
|
266
|
+
if (value == null) {
|
|
267
|
+
return "\"\"";
|
|
268
|
+
}
|
|
269
|
+
StringBuilder sb = new StringBuilder("\"");
|
|
270
|
+
for (int i = 0; i < value.length(); i++) {
|
|
271
|
+
char c = value.charAt(i);
|
|
272
|
+
switch (c) {
|
|
273
|
+
case '"' -> sb.append("\\\"");
|
|
274
|
+
case '\\' -> sb.append("\\\\");
|
|
275
|
+
case '\n' -> sb.append("\\n");
|
|
276
|
+
case '\r' -> sb.append("\\r");
|
|
277
|
+
case '\t' -> sb.append("\\t");
|
|
278
|
+
default -> sb.append(c);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
sb.append('"');
|
|
282
|
+
return sb.toString();
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Minimal RFC 8259 recursive-descent JSON parser (handles arbitrary key order, nested structures, and escapes).
|
|
287
|
+
*/
|
|
288
|
+
static final class MiniJson {
|
|
289
|
+
private final String src;
|
|
290
|
+
private int pos;
|
|
291
|
+
|
|
292
|
+
private MiniJson(String src) {
|
|
293
|
+
this.src = src == null ? "" : src;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
static Object parse(String text) {
|
|
297
|
+
MiniJson parser = new MiniJson(text);
|
|
298
|
+
Object val = parser.parseValue();
|
|
299
|
+
parser.skipWs();
|
|
300
|
+
return val;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
private void skipWs() {
|
|
304
|
+
while (pos < src.length() && Character.isWhitespace(src.charAt(pos))) {
|
|
305
|
+
pos++;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
private Object parseValue() {
|
|
310
|
+
skipWs();
|
|
311
|
+
if (pos >= src.length()) {
|
|
312
|
+
throw new IllegalArgumentException("Unexpected end of JSON");
|
|
313
|
+
}
|
|
314
|
+
return switch (src.charAt(pos)) {
|
|
315
|
+
case '{' -> parseObject();
|
|
316
|
+
case '[' -> parseArray();
|
|
317
|
+
case '"' -> parseString();
|
|
318
|
+
case 't' -> parseLiteral("true", Boolean.TRUE);
|
|
319
|
+
case 'f' -> parseLiteral("false", Boolean.FALSE);
|
|
320
|
+
case 'n' -> parseLiteral("null", null);
|
|
321
|
+
default -> parseNumber();
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
private Map<String, Object> parseObject() {
|
|
326
|
+
pos++; // '{'
|
|
327
|
+
Map<String, Object> map = new LinkedHashMap<>();
|
|
328
|
+
skipWs();
|
|
329
|
+
if (pos < src.length() && src.charAt(pos) == '}') {
|
|
330
|
+
pos++;
|
|
331
|
+
return map;
|
|
332
|
+
}
|
|
333
|
+
while (pos < src.length()) {
|
|
334
|
+
skipWs();
|
|
335
|
+
String key = parseString();
|
|
336
|
+
skipWs();
|
|
337
|
+
if (pos >= src.length() || src.charAt(pos) != ':') {
|
|
338
|
+
throw new IllegalArgumentException("Expected ':' at position " + pos);
|
|
339
|
+
}
|
|
340
|
+
pos++;
|
|
341
|
+
Object val = parseValue();
|
|
342
|
+
map.put(key, val);
|
|
343
|
+
skipWs();
|
|
344
|
+
if (pos < src.length() && src.charAt(pos) == ',') {
|
|
345
|
+
pos++;
|
|
346
|
+
} else if (pos < src.length() && src.charAt(pos) == '}') {
|
|
347
|
+
pos++;
|
|
348
|
+
return map;
|
|
349
|
+
} else {
|
|
350
|
+
throw new IllegalArgumentException("Expected ',' or '}' at position " + pos);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
throw new IllegalArgumentException("Unterminated JSON object");
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
private List<Object> parseArray() {
|
|
357
|
+
pos++; // '['
|
|
358
|
+
List<Object> list = new ArrayList<>();
|
|
359
|
+
skipWs();
|
|
360
|
+
if (pos < src.length() && src.charAt(pos) == ']') {
|
|
361
|
+
pos++;
|
|
362
|
+
return list;
|
|
363
|
+
}
|
|
364
|
+
while (pos < src.length()) {
|
|
365
|
+
list.add(parseValue());
|
|
366
|
+
skipWs();
|
|
367
|
+
if (pos < src.length() && src.charAt(pos) == ',') {
|
|
368
|
+
pos++;
|
|
369
|
+
} else if (pos < src.length() && src.charAt(pos) == ']') {
|
|
370
|
+
pos++;
|
|
371
|
+
return list;
|
|
372
|
+
} else {
|
|
373
|
+
throw new IllegalArgumentException("Expected ',' or ']' at position " + pos);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
throw new IllegalArgumentException("Unterminated JSON array");
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
private String parseString() {
|
|
380
|
+
if (src.charAt(pos) != '"') {
|
|
381
|
+
throw new IllegalArgumentException("Expected '\"' at position " + pos);
|
|
382
|
+
}
|
|
383
|
+
pos++;
|
|
384
|
+
StringBuilder sb = new StringBuilder();
|
|
385
|
+
while (pos < src.length()) {
|
|
386
|
+
char c = src.charAt(pos++);
|
|
387
|
+
if (c == '"') {
|
|
388
|
+
return sb.toString();
|
|
389
|
+
}
|
|
390
|
+
if (c == '\\' && pos < src.length()) {
|
|
391
|
+
char esc = src.charAt(pos++);
|
|
392
|
+
switch (esc) {
|
|
393
|
+
case '"', '\\', '/' -> sb.append(esc);
|
|
394
|
+
case 'b' -> sb.append('\b');
|
|
395
|
+
case 'f' -> sb.append('\f');
|
|
396
|
+
case 'n' -> sb.append('\n');
|
|
397
|
+
case 'r' -> sb.append('\r');
|
|
398
|
+
case 't' -> sb.append('\t');
|
|
399
|
+
case 'u' -> {
|
|
400
|
+
String hex = src.substring(pos, Math.min(pos + 4, src.length()));
|
|
401
|
+
sb.append((char) Integer.parseInt(hex, 16));
|
|
402
|
+
pos += 4;
|
|
403
|
+
}
|
|
404
|
+
default -> sb.append(esc);
|
|
405
|
+
}
|
|
406
|
+
} else {
|
|
407
|
+
sb.append(c);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
throw new IllegalArgumentException("Unterminated JSON string");
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
private Object parseLiteral(String expected, Object value) {
|
|
414
|
+
if (src.startsWith(expected, pos)) {
|
|
415
|
+
pos += expected.length();
|
|
416
|
+
return value;
|
|
417
|
+
}
|
|
418
|
+
throw new IllegalArgumentException("Invalid JSON literal at position " + pos);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
private Number parseNumber() {
|
|
422
|
+
int start = pos;
|
|
423
|
+
while (pos < src.length() && "-0123456789.eE+".indexOf(src.charAt(pos)) >= 0) {
|
|
424
|
+
pos++;
|
|
425
|
+
}
|
|
426
|
+
String numStr = src.substring(start, pos);
|
|
427
|
+
if (numStr.contains(".") || numStr.contains("e") || numStr.contains("E")) {
|
|
428
|
+
return Double.parseDouble(numStr);
|
|
429
|
+
}
|
|
430
|
+
return Long.parseLong(numStr);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
package __JAVA_PACKAGE__;
|
|
2
|
+
|
|
3
|
+
import java.util.List;
|
|
4
|
+
import java.util.Objects;
|
|
5
|
+
import java.util.Set;
|
|
6
|
+
import java.util.function.Function;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Checkpointer-backed pure-JDK 17 __DOMAIN_TITLE__ agent.
|
|
10
|
+
*
|
|
11
|
+
* External APIs are injected through {@link __DOMAIN_PASCAL__Client} and multi-turn state is
|
|
12
|
+
* managed by {@link ConversationMemory} so live evaluations stay deterministic and testable.
|
|
13
|
+
*/
|
|
14
|
+
public class __DOMAIN_PASCAL__Agent {
|
|
15
|
+
|
|
16
|
+
public static final String DEFAULT_LLM_BASE_URL = "http://localhost:8080/v1";
|
|
17
|
+
public static final String DEFAULT_LLM_MODEL = "local-model";
|
|
18
|
+
|
|
19
|
+
// Keep SYSTEM_PROMPT compact (<= 120 words) and refactor rather than append patches.
|
|
20
|
+
public static final String SYSTEM_PROMPT =
|
|
21
|
+
"You are a helpful __DOMAIN_TITLE__ assistant. Follow AGENTS.md strictly. "
|
|
22
|
+
+ "Ground every factual statement in domain tool outputs and never fabricate data. "
|
|
23
|
+
+ "If a request is vague or missing required identifiers, ask one clear clarifying question without calling tools. "
|
|
24
|
+
+ "If a tool returns an error, reply briefly and honestly without guessing missing values. "
|
|
25
|
+
+ "Politely decline requests outside the __DOMAIN__ domain.";
|
|
26
|
+
|
|
27
|
+
public static final Set<String> DOMAIN_TOOLS = __JAVA_DOMAIN_TOOLS_SET__;
|
|
28
|
+
|
|
29
|
+
private final __DOMAIN_PASCAL__Client client;
|
|
30
|
+
private final ConversationMemory memory;
|
|
31
|
+
private final String baseUrl;
|
|
32
|
+
private final String model;
|
|
33
|
+
private final String apiKey;
|
|
34
|
+
private final OpenAiCompatibleRunner runner;
|
|
35
|
+
|
|
36
|
+
public __DOMAIN_PASCAL__Agent(__DOMAIN_PASCAL__Client client) {
|
|
37
|
+
this(client, new ConversationMemory.InMemory(), null, null, null, null);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public __DOMAIN_PASCAL__Agent(
|
|
41
|
+
__DOMAIN_PASCAL__Client client,
|
|
42
|
+
ConversationMemory memory,
|
|
43
|
+
String baseUrl,
|
|
44
|
+
String model) {
|
|
45
|
+
this(client, memory, baseUrl, model, null, null);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public __DOMAIN_PASCAL__Agent(
|
|
49
|
+
__DOMAIN_PASCAL__Client client,
|
|
50
|
+
ConversationMemory memory,
|
|
51
|
+
String baseUrl,
|
|
52
|
+
String model,
|
|
53
|
+
String apiKey,
|
|
54
|
+
Function<String, String> httpTransport) {
|
|
55
|
+
this.client = Objects.requireNonNull(client, "client must not be null");
|
|
56
|
+
this.memory = memory != null ? memory : new ConversationMemory.InMemory();
|
|
57
|
+
this.baseUrl = resolveEnv(baseUrl, "LLM_BASE_URL", DEFAULT_LLM_BASE_URL);
|
|
58
|
+
this.model = resolveEnv(model, "LLM_MODEL", DEFAULT_LLM_MODEL);
|
|
59
|
+
this.apiKey = resolveEnv(apiKey, "LLM_API_KEY", "not-needed");
|
|
60
|
+
this.runner = new OpenAiCompatibleRunner(this.baseUrl, this.model, this.apiKey, httpTransport);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public static __DOMAIN_PASCAL__Agent create(__DOMAIN_PASCAL__Client client) {
|
|
64
|
+
return new __DOMAIN_PASCAL__Agent(client);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public ConversationMemory memory() {
|
|
68
|
+
return memory;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public String baseUrl() {
|
|
72
|
+
return baseUrl;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public String model() {
|
|
76
|
+
return model;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Dispatch a domain tool call against the injected client with uniform error handling.
|
|
81
|
+
*/
|
|
82
|
+
public String invokeTool(String toolName, String query) {
|
|
83
|
+
try {
|
|
84
|
+
return switch (toolName) {
|
|
85
|
+
__JAVA_TOOL_DISPATCH__
|
|
86
|
+
default -> "Error: Unknown tool '" + toolName + "'.";
|
|
87
|
+
};
|
|
88
|
+
} catch (RuntimeException exc) {
|
|
89
|
+
return "Error: " + exc.getMessage();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Persist a user/assistant turn in the configured checkpointer.
|
|
95
|
+
*/
|
|
96
|
+
public void recordTurn(String threadId, String userInput, String assistantReply) {
|
|
97
|
+
memory.append(threadId, new ConversationMemory.Message("user", userInput));
|
|
98
|
+
memory.append(threadId, new ConversationMemory.Message("assistant", assistantReply));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Execute a full conversational turn with automatic tool dispatch and checkpointer persistence.
|
|
103
|
+
*/
|
|
104
|
+
public TurnOutcome runTurn(String threadId, String userInput) {
|
|
105
|
+
List<ConversationMemory.Message> history = memory.getMessages(threadId);
|
|
106
|
+
TurnOutcome outcome = runner.executeTurn(SYSTEM_PROMPT, history, userInput, DOMAIN_TOOLS, this::invokeTool);
|
|
107
|
+
recordTurn(threadId, userInput, outcome.finalResponse());
|
|
108
|
+
return outcome;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Convenience wrapper returning only the assistant's final text reply.
|
|
113
|
+
*/
|
|
114
|
+
public String chat(String threadId, String userInput) {
|
|
115
|
+
return runTurn(threadId, userInput).finalResponse();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private static String resolveEnv(String explicit, String envVar, String fallback) {
|
|
119
|
+
if (explicit != null && !explicit.isBlank()) {
|
|
120
|
+
return explicit;
|
|
121
|
+
}
|
|
122
|
+
String fromEnv = System.getenv(envVar);
|
|
123
|
+
return (fromEnv != null && !fromEnv.isBlank()) ? fromEnv : fallback;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
package __JAVA_PACKAGE__;
|
|
2
|
+
|
|
3
|
+
import java.util.Scanner;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Minimal runnable LangChain4j CLI application for __PROJECT_NAME__.
|
|
7
|
+
*
|
|
8
|
+
* Run single prompt: mvn exec:java -Dexec.args="--prompt 'Lookup item-1'"
|
|
9
|
+
* Run interactive: mvn exec:java (or ./gradlew run)
|
|
10
|
+
*/
|
|
11
|
+
public class Application {
|
|
12
|
+
|
|
13
|
+
public static void main(String[] args) {
|
|
14
|
+
String prompt = null;
|
|
15
|
+
String threadId = "interactive";
|
|
16
|
+
for (int i = 0; i < args.length; i++) {
|
|
17
|
+
if ("--prompt".equals(args[i]) && i + 1 < args.length) {
|
|
18
|
+
prompt = args[++i];
|
|
19
|
+
} else if ("--thread-id".equals(args[i]) && i + 1 < args.length) {
|
|
20
|
+
threadId = args[++i];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
__DOMAIN_PASCAL__Agent agent = __DOMAIN_PASCAL__Agent.create(new Default__DOMAIN_PASCAL__Client());
|
|
25
|
+
|
|
26
|
+
if (prompt != null) {
|
|
27
|
+
System.out.println(agent.chat(threadId, prompt));
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
System.out.println("__DOMAIN_TITLE__ LangChain4j agent ready. Type 'exit' to quit.");
|
|
32
|
+
try (Scanner scanner = new Scanner(System.in)) {
|
|
33
|
+
while (true) {
|
|
34
|
+
System.out.print("you> ");
|
|
35
|
+
if (!scanner.hasNextLine()) {
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
String line = scanner.nextLine().trim();
|
|
39
|
+
if (line.isEmpty() || "exit".equalsIgnoreCase(line) || "quit".equalsIgnoreCase(line)) {
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
System.out.println("agent> " + agent.chat(threadId, line));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|