From ddbf17d3ab10391a0505cf739b9bd61e662b5576 Mon Sep 17 00:00:00 2001 From: nicholascole Date: Mon, 19 Oct 2026 15:49:52 -0700 Subject: [PATCH 1/2] Add JEV examples --- .../ai/examples/ExampleDecisionRouter.java | 63 ++++++++++++++ .../ai/examples/ExampleDecisionRouting.java | 87 +++++++++++++++++++ .../ai/examples/ExampleDecisionTool.java | 56 ++++++++++++ .../org/conductoross/conductor/ai/Agent.java | 22 ++++- .../ai/internal/AgentConfigSerializer.java | 23 ++++- .../conductor/ai/DecisionRouterTest.java | 86 ++++++++++++++++++ .../sdk/workflow/def/tasks/Switch.java | 17 +++- .../sdk/workflow/def/DecisionSwitchTest.java | 74 ++++++++++++++++ docs/agents/README.md | 19 ++++ docs/agents/reference/agent-definition.md | 2 +- docs/agents/reference/agent-schema.json | 17 +++- 11 files changed, 459 insertions(+), 7 deletions(-) create mode 100644 agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionRouter.java create mode 100644 agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionRouting.java create mode 100644 agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionTool.java create mode 100644 conductor-client-ai/src/test/java/org/conductoross/conductor/ai/DecisionRouterTest.java create mode 100644 conductor-client/src/test/java/com/netflix/conductor/sdk/workflow/def/DecisionSwitchTest.java diff --git a/agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionRouter.java b/agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionRouter.java new file mode 100644 index 000000000..bc55380a8 --- /dev/null +++ b/agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionRouter.java @@ -0,0 +1,63 @@ +/* + * Copyright 2025 Conductor 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 org.conductoross.conductor.ai.examples; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.conductoross.conductor.ai.Agent; +import org.conductoross.conductor.ai.AgentRuntime; +import org.conductoross.conductor.ai.enums.Strategy; +import org.conductoross.conductor.ai.model.AgentHandle; +import org.conductoross.conductor.ai.model.AgentResult; +import org.conductoross.conductor.ai.model.ToolDef; + +/** Use a decision tool to choose one of two chat agents, without an SDK worker. */ +public class ExampleDecisionRouter { + public static Agent supportRouter() { + String chatModel = System.getenv().getOrDefault("CONDUCTOR_AGENT_LLM_MODEL", "openai/gpt-4o-mini"); + Agent billing = Agent.builder().name("billing_java").model(chatModel) + .instructions("Help the customer with billing and invoice questions.").build(); + Agent technical = Agent.builder().name("technical_java").model(chatModel) + .instructions("Help the customer troubleshoot product issues.").build(); + Map config = new LinkedHashMap<>(); + config.put("model", "jev-1.13"); + config.put("questions", Map.of("agent", Map.of( + "type", "choice", "instructions", "Choose the right support team.", + "choices", Map.of("billing_java", "Payments and invoices", + "technical_java", "Product errors and troubleshooting")))); + String provider = System.getenv("CONDUCTOR_DECISION_PROVIDER"); + if (provider != null) config.put("provider", provider); + ToolDef selector = ToolDef.builder().name("support_selector_java") + .description("Choose the support agent that should handle the request.") + .toolType("decision").config(config).build(); + return Agent.builder().name("decision_support_router_java").strategy(Strategy.ROUTER) + .router(selector).agents(billing, technical).maxTurns(1).synthesize(false).build(); + } + + public static void main(String[] args) { + try (AgentRuntime runtime = new AgentRuntime()) { + Agent agent = supportRouter(); + if (args.length == 0 || !"--run".equals(args[0])) { + System.out.println(runtime.plan(agent)); + return; + } + runtime.deploy(agent); + AgentHandle handle = runtime.start(agent, "The customer reports a duplicate charge on the latest invoice."); + System.out.println("Execution: " + handle.getExecutionId()); + AgentResult result = handle.waitForResult(120_000, 500); + if (!result.isSuccess()) throw new IllegalStateException(result.getError()); + result.printResult(); + } + } +} diff --git a/agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionRouting.java b/agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionRouting.java new file mode 100644 index 000000000..5cc672d40 --- /dev/null +++ b/agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionRouting.java @@ -0,0 +1,87 @@ +/* + * Copyright 2025 Conductor 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 org.conductoross.conductor.ai.examples; + +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import com.netflix.conductor.common.run.Workflow; +import com.netflix.conductor.sdk.workflow.def.ConductorWorkflow; +import com.netflix.conductor.sdk.workflow.def.tasks.Javascript; +import com.netflix.conductor.sdk.workflow.def.tasks.Switch; +import com.netflix.conductor.sdk.workflow.executor.WorkflowExecutor; + +import io.orkes.conductor.client.ApiClient; + +/** Route a request through one synchronous decision SWITCH and the selected INLINE branch. */ +public class ExampleDecisionRouting { + public static ConductorWorkflow> createWorkflow(WorkflowExecutor executor) { + ConductorWorkflow> workflow = new ConductorWorkflow<>(executor); + workflow.setName("ai_decision_routing_java"); + workflow.setVersion(1); + Switch decision = new Switch("route_request", "route") + .evaluatorType(Switch.DECISION_NAME) + .input("model", "jev-1.13") + .input("state", "${workflow.input.request}") + .input("questions", Map.of("route", Map.of( + "type", "choice", + "instructions", "Choose the team best suited to handle this request.", + "choices", Map.of( + "billing", "Payments, invoices, refunds, or subscriptions.", + "technical", "Errors, outages, or product troubleshooting.")))); + String provider = System.getenv("CONDUCTOR_DECISION_PROVIDER"); + if (provider != null) decision.input("provider", provider); + Javascript billing = new Javascript("handle_billing", + "({team: 'billing', nextAction: 'review_invoice', " + + "message: 'Check invoice line items and payment records.', request: $.request})") + .input("request", "${workflow.input.request}"); + Javascript technical = new Javascript("handle_technical", + "({team: 'technical', nextAction: 'collect_diagnostics', " + + "message: 'Collect error logs and steps to reproduce.', request: $.request})") + .input("request", "${workflow.input.request}"); + decision.switchCase("billing", billing).switchCase("technical", technical); + Javascript selected = new Javascript("selected_result", "$.billing || $.technical") + .input("billing", "${handle_billing.output.result}") + .input("technical", "${handle_technical.output.result}"); + workflow.add(decision); + workflow.add(selected); + workflow.setWorkflowOutput(Map.of( + "selectedCase", "${route_request.output.selectedCase}", + "answers", "${route_request.output.answers}", + "usage", "${route_request.output.usage}", + "cost", "${route_request.output.usage.cost}", + "latencyMs", "${route_request.output.latencyMs}", + "result", "${selected_result.output.result}")); + return workflow; + } + + public static void main(String[] args) throws Exception { + String request = args.length == 0 ? "I was charged twice on my latest invoice." : args[0]; + ApiClient client = new ApiClient(); + WorkflowExecutor executor = new WorkflowExecutor(client, 100); + try { + ConductorWorkflow> workflow = createWorkflow(executor); + workflow.registerWorkflow(true); + Workflow result = executor.executeWorkflow(workflow, Map.of("request", request)) + .get(120, TimeUnit.SECONDS); + System.out.println("Execution: " + result.getWorkflowId()); + if (result.getStatus() != Workflow.WorkflowStatus.COMPLETED) { + throw new IllegalStateException(result.getStatus() + ": " + result.getReasonForIncompletion()); + } + System.out.println(result.getOutput()); + } finally { + executor.shutdown(); + client.shutdown(); + } + } +} diff --git a/agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionTool.java b/agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionTool.java new file mode 100644 index 000000000..c47bf4a90 --- /dev/null +++ b/agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionTool.java @@ -0,0 +1,56 @@ +/* + * Copyright 2025 Conductor 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 org.conductoross.conductor.ai.examples; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.conductoross.conductor.ai.Agent; +import org.conductoross.conductor.ai.AgentRuntime; +import org.conductoross.conductor.ai.model.AgentHandle; +import org.conductoross.conductor.ai.model.AgentResult; +import org.conductoross.conductor.ai.model.ToolDef; + +/** A normal chat agent calling a server-side, branchless decision SWITCH tool. */ +public class ExampleDecisionTool { + public static Agent supportAgent() { + Map config = new LinkedHashMap<>(); + config.put("model", "jev-1.13"); + config.put("questions", Map.of("department", Map.of( + "type", "choice", "instructions", "Choose the correct support department.", + "choices", Map.of("billing", "Payments and invoices", + "technical", "Product errors and troubleshooting")))); + String provider = System.getenv("CONDUCTOR_DECISION_PROVIDER"); + if (provider != null) config.put("provider", provider); + ToolDef classify = ToolDef.builder().name("classify_request_java") + .description("Classify the customer request before answering.").toolType("decision") + .inputSchema(Map.of("type", "object", "properties", Map.of("state", Map.of("type", "string")), + "required", List.of("state"))) + .config(config).build(); + return Agent.builder().name("decision_tool_support_java") + .model(System.getenv().getOrDefault("CONDUCTOR_AGENT_LLM_MODEL", "openai/gpt-4o-mini")) + .instructions("Call classify_request_java before answering the customer.") + .tools(classify).build(); + } + + public static void main(String[] args) { + try (AgentRuntime runtime = new AgentRuntime()) { + AgentHandle handle = runtime.start(supportAgent(), "I was charged twice on my latest invoice."); + System.out.println("Execution: " + handle.getExecutionId()); + AgentResult result = handle.waitForResult(120_000, 500); + if (!result.isSuccess()) throw new IllegalStateException(result.getError()); + result.printResult(); + } + } +} diff --git a/conductor-client-ai/src/main/java/org/conductoross/conductor/ai/Agent.java b/conductor-client-ai/src/main/java/org/conductoross/conductor/ai/Agent.java index af2ceaf00..9f16b425f 100644 --- a/conductor-client-ai/src/main/java/org/conductoross/conductor/ai/Agent.java +++ b/conductor-client-ai/src/main/java/org/conductoross/conductor/ai/Agent.java @@ -63,6 +63,7 @@ public class Agent { private final List agents; private final Strategy strategy; private final Agent router; + private final ToolDef decisionRouter; private final List guardrails; private final int maxTurns; private final Integer maxTokens; @@ -137,6 +138,7 @@ private Agent(Builder builder) { this.agents = builder.agents != null ? new ArrayList<>(builder.agents) : new ArrayList<>(); this.strategy = builder.strategy != null ? builder.strategy : Strategy.HANDOFF; this.router = builder.router; + this.decisionRouter = builder.decisionRouter; this.guardrails = builder.guardrails != null ? new ArrayList<>(builder.guardrails) : new ArrayList<>(); this.maxTurns = builder.maxTurns; this.maxTokens = builder.maxTokens; @@ -198,10 +200,12 @@ private Agent(Builder builder) { } /** - * Returns true if this agent is external (no local model — references a deployed workflow). + * Returns true if this agent references a deployed workflow (no model or decision router). */ public boolean isExternal() { - return model == null || model.isEmpty(); + return (model == null || model.isEmpty()) + && !(strategy == Strategy.ROUTER && decisionRouter != null + && "decision".equals(decisionRouter.getToolType())); } /** @@ -264,6 +268,11 @@ public Agent getRouter() { return router; } + /** Decision selector configuration; it is evaluated by the server without a worker. */ + public ToolDef getDecisionRouter() { + return decisionRouter; + } + public List getGuardrails() { return guardrails; } @@ -498,6 +507,7 @@ public static class Builder { private List agents; private Strategy strategy = Strategy.HANDOFF; private Agent router; + private ToolDef decisionRouter; private List guardrails; private int maxTurns = 25; private Integer maxTokens; @@ -608,6 +618,14 @@ public Builder strategy(Strategy strategy) { /** Set the router agent for ROUTER strategy. */ public Builder router(Agent router) { this.router = router; + this.decisionRouter = null; + return this; + } + + /** Use a decision tool to select a sub-agent without a router LLM or SDK worker. */ + public Builder router(ToolDef router) { + this.decisionRouter = router; + this.router = null; return this; } diff --git a/conductor-client-ai/src/main/java/org/conductoross/conductor/ai/internal/AgentConfigSerializer.java b/conductor-client-ai/src/main/java/org/conductoross/conductor/ai/internal/AgentConfigSerializer.java index 34d7808db..372952344 100644 --- a/conductor-client-ai/src/main/java/org/conductoross/conductor/ai/internal/AgentConfigSerializer.java +++ b/conductor-client-ai/src/main/java/org/conductoross/conductor/ai/internal/AgentConfigSerializer.java @@ -211,7 +211,28 @@ private Map serializeAgent(Agent agent) { } // Router agent (for ROUTER strategy) - if (agent.getRouter() != null) { + if (agent.getDecisionRouter() != null) { + ToolDef router = agent.getDecisionRouter(); + if (!"decision".equals(router.getToolType())) { + throw new IllegalArgumentException("Only a decision ToolDef can be used as a router"); + } + Map config = router.getConfig(); + if (config == null || !(config.get("model") instanceof String model) || model.isBlank()) { + throw new IllegalArgumentException("Decision router tool config requires 'model'"); + } + if (!(config.get("questions") instanceof Map questions) || questions.isEmpty()) { + throw new IllegalArgumentException("Decision router tool config requires 'questions'"); + } + Map selector = new LinkedHashMap<>(); + selector.put("name", router.getName()); + selector.put("kind", "decision"); + selector.put("model", config.get("model")); + selector.put("questions", config.get("questions")); + if (config.get("provider") != null) { + selector.put("provider", config.get("provider")); + } + agentMap.put("router", selector); + } else if (agent.getRouter() != null) { agentMap.put("router", serializeAgent(agent.getRouter())); } diff --git a/conductor-client-ai/src/test/java/org/conductoross/conductor/ai/DecisionRouterTest.java b/conductor-client-ai/src/test/java/org/conductoross/conductor/ai/DecisionRouterTest.java new file mode 100644 index 000000000..74a8dfa3d --- /dev/null +++ b/conductor-client-ai/src/test/java/org/conductoross/conductor/ai/DecisionRouterTest.java @@ -0,0 +1,86 @@ +/* + * Copyright 2025 Conductor 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 org.conductoross.conductor.ai; + +import java.util.Map; + +import org.conductoross.conductor.ai.enums.Strategy; +import org.conductoross.conductor.ai.internal.AgentConfigSerializer; +import org.conductoross.conductor.ai.model.ToolDef; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class DecisionRouterTest { + private static final Map QUESTIONS = Map.of("agent", Map.of("type", "choice", + "choices", Map.of("billing", "Payments", "technical", "Troubleshooting"))); + + private Agent router(ToolDef tool) { + return Agent.builder().name("support").strategy(Strategy.ROUTER).router(tool) + .agents(Agent.builder().name("billing").model("openai/gpt-4o-mini").build()) + .synthesize(false).build(); + } + + @Test + void serializesDecisionSelectorAndKeepsNoModelParentLocal() { + ToolDef tool = ToolDef.builder().name("select").toolType("decision") + .config(Map.of("model", "jev-1.13", "provider", "openrouter", "questions", QUESTIONS)).build(); + Agent agent = router(tool); + Map config = new AgentConfigSerializer().serialize(agent); + assertFalse(agent.isExternal()); + assertNull(agent.getRouter()); + assertSame(tool, agent.getDecisionRouter()); + assertEquals(false, config.get("external")); + assertEquals(false, config.get("synthesize")); + assertEquals(Map.of("name", "select", "kind", "decision", "model", "jev-1.13", + "provider", "openrouter", "questions", QUESTIONS), config.get("router")); + } + + @Test + void omitsUnsetProviderAndPreservesDecisionTool() { + ToolDef tool = ToolDef.builder().name("classify").toolType("decision") + .config(Map.of("model", "jev-1.13", "questions", QUESTIONS)).build(); + Map config = new AgentConfigSerializer().serialize(router(tool)); + assertEquals(Map.of("name", "classify", "kind", "decision", "model", "jev-1.13", + "questions", QUESTIONS), config.get("router")); + Agent normal = Agent.builder().name("assistant").model("openai/gpt-4o-mini").tools(tool).build(); + Map serializedTool = (Map) ((java.util.List) + new AgentConfigSerializer().serialize(normal).get("tools")).get(0); + assertEquals("decision", serializedTool.get("toolType")); + assertEquals(tool.getConfig(), serializedTool.get("config")); + } + + @Test + void rejectsInvalidDecisionSelectors() { + for (Map config : java.util.List.of(Map.of(), + Map.of("model", "", "questions", QUESTIONS), + Map.of("model", "jev-1.13", "questions", Map.of()))) { + ToolDef tool = ToolDef.builder().name("select").toolType("decision").config(config).build(); + assertThrows(IllegalArgumentException.class, () -> new AgentConfigSerializer().serialize(router(tool))); + } + ToolDef worker = ToolDef.builder().name("worker").build(); + assertThrows(IllegalArgumentException.class, () -> new AgentConfigSerializer().serialize(router(worker))); + } + + @Test + void preservesAgentRouterAndExternalReferences() { + Agent selector = Agent.builder().name("selector").model("openai/gpt-4o-mini").build(); + Agent agent = Agent.builder().name("support").strategy(Strategy.ROUTER).router(selector) + .agents(selector).build(); + assertSame(selector, agent.getRouter()); + assertNull(agent.getDecisionRouter()); + assertEquals(new AgentConfigSerializer().serialize(selector), + new AgentConfigSerializer().serialize(agent).get("router")); + assertTrue(Agent.builder().name("existing_workflow").build().isExternal()); + } +} diff --git a/conductor-client/src/main/java/com/netflix/conductor/sdk/workflow/def/tasks/Switch.java b/conductor-client/src/main/java/com/netflix/conductor/sdk/workflow/def/tasks/Switch.java index 14c9d42a1..a4606fc29 100644 --- a/conductor-client/src/main/java/com/netflix/conductor/sdk/workflow/def/tasks/Switch.java +++ b/conductor-client/src/main/java/com/netflix/conductor/sdk/workflow/def/tasks/Switch.java @@ -28,6 +28,10 @@ public class Switch extends Task { public static final String JAVASCRIPT_NAME = "javascript"; + public static final String DECISION_NAME = "decision"; + + private String evaluatorType; + private String caseExpression; private boolean useJavascript; @@ -65,6 +69,8 @@ public Switch(String taskReferenceName, String caseExpression) { Switch(WorkflowTask workflowTask) { super(workflowTask); + this.evaluatorType = workflowTask.getEvaluatorType(); + this.caseExpression = workflowTask.getExpression(); Map> decisions = workflowTask.getDecisionCases(); decisions.entrySet().stream() @@ -85,6 +91,12 @@ public Switch(String taskReferenceName, String caseExpression) { } } + /** Set an explicit server evaluator, for example {@link #DECISION_NAME}. */ + public Switch evaluatorType(String evaluatorType) { + this.evaluatorType = evaluatorType; + return this; + } + public Switch defaultCase(Task... tasks) { defaultTasks = Arrays.asList(tasks); return this; @@ -133,7 +145,10 @@ public Map>> getBranches() { @Override public void updateWorkflowTask(WorkflowTask workflowTask) { - if (useJavascript) { + if (evaluatorType != null) { + workflowTask.setEvaluatorType(evaluatorType); + workflowTask.setExpression(caseExpression); + } else if (useJavascript) { workflowTask.setEvaluatorType(JAVASCRIPT_NAME); workflowTask.setExpression(caseExpression); diff --git a/conductor-client/src/test/java/com/netflix/conductor/sdk/workflow/def/DecisionSwitchTest.java b/conductor-client/src/test/java/com/netflix/conductor/sdk/workflow/def/DecisionSwitchTest.java new file mode 100644 index 000000000..3b3522163 --- /dev/null +++ b/conductor-client/src/test/java/com/netflix/conductor/sdk/workflow/def/DecisionSwitchTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2022 Conductor 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 com.netflix.conductor.sdk.workflow.def; + +import java.util.Map; + +import org.junit.jupiter.api.Test; + +import com.netflix.conductor.common.metadata.workflow.WorkflowTask; +import com.netflix.conductor.sdk.workflow.def.tasks.Javascript; +import com.netflix.conductor.sdk.workflow.def.tasks.Switch; +import com.netflix.conductor.sdk.workflow.def.tasks.TaskRegistry; +import com.netflix.conductor.sdk.workflow.executor.WorkflowExecutor; + +import static org.junit.jupiter.api.Assertions.*; + +class DecisionSwitchTest { + @Test + void preservesExplicitDecisionEvaluatorAndQuestions() { + Switch decision = new Switch("route_request", "route").evaluatorType(Switch.DECISION_NAME) + .input("model", "jev-1.13").input("state", "${workflow.input.request}") + .input("questions", Map.of("route", Map.of("type", "choice"))) + .switchCase("billing", new Javascript("billing", "'invoice'")); + WorkflowTask task = decision.getWorkflowDefTasks().get(0); + assertEquals("SWITCH", task.getType()); + assertEquals("decision", task.getEvaluatorType()); + assertEquals("route", task.getExpression()); + assertFalse(task.getInputParameters().containsKey("switchCaseValue")); + assertEquals("jev-1.13", task.getInputParameters().get("model")); + assertEquals(1, task.getDecisionCases().get("billing").size()); + assertTrue(task.getDefaultCase().isEmpty()); + } + + @Test + void roundTripsDecisionAndLegacySwitchesThroughTaskRegistry() { + WorkflowExecutor.initTaskImplementations(); + for (Switch original : java.util.List.of( + new Switch("decision", "route").evaluatorType("decision") + .input("model", "jev-1.13").switchCase("billing", new Javascript("handle", "'invoice'")), + new Switch("value", "${workflow.input.route}"), + new Switch("javascript", "$.route", true))) { + WorkflowTask before = original.getWorkflowDefTasks().get(0); + WorkflowTask after = TaskRegistry.getTask(before).getWorkflowDefTasks().get(0); + assertEquals(before.getEvaluatorType(), after.getEvaluatorType()); + assertEquals(before.getExpression(), after.getExpression()); + assertEquals(before.getInputParameters(), after.getInputParameters()); + assertEquals(before.getDecisionCases(), after.getDecisionCases()); + } + } + + @Test + void preservesLegacyValueAndJavascriptSwitches() { + WorkflowTask value = new Switch("route", "${workflow.input.route}").getWorkflowDefTasks().get(0); + assertEquals("value-param", value.getEvaluatorType()); + assertEquals("switchCaseValue", value.getExpression()); + assertEquals("${workflow.input.route}", value.getInputParameters().get("switchCaseValue")); + WorkflowTask javascript = new Switch("route", "$.route", true).getWorkflowDefTasks().get(0); + assertEquals("javascript", javascript.getEvaluatorType()); + assertEquals("$.route", javascript.getExpression()); + WorkflowTask explicit = new Switch("route", "route", true).evaluatorType("decision") + .getWorkflowDefTasks().get(0); + assertEquals("decision", explicit.getEvaluatorType()); + } +} diff --git a/docs/agents/README.md b/docs/agents/README.md index 85f4e36d1..15d45bca6 100644 --- a/docs/agents/README.md +++ b/docs/agents/README.md @@ -31,6 +31,25 @@ Replace `` with a published version from [Maven Central](https://search - **[Multi-Agent](concepts/multi-agent.md)** — sequential, parallel, handoff, swarm, and plan-execute agents. - **[Guardrails](concepts/guardrails.md)**, **[Termination](concepts/termination.md)**, **[Callbacks](concepts/callbacks.md)**, **[Stateful Agents](concepts/stateful.md)**, **[Streaming & Human-in-the-Loop](concepts/streaming-hitl.md)**, and **[Structured Output](concepts/structured-output.md)**. +## Decision inference examples + +These examples require a server with the synchronous decision evaluator and server-managed Decision credentials. No Java decision worker is needed. The router and tool examples also require a configured chat provider. Set `CONDUCTOR_DECISION_PROVIDER=openrouter` when using OpenRouter and optionally `CONDUCTOR_AGENT_LLM_MODEL=openai/gpt-4o-mini`. + +- [Traditional workflow](../../agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionRouting.java): one decision-backed SWITCH chooses billing or technical INLINE handling and returns the selected result, answers, usage, cost, and latency. +- [Decision router](../../agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionRouter.java): a decision `ToolDef` selects a child chat agent; `synthesize(false)` returns its result directly. +- [Decision tool](../../agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionTool.java): a chat agent calls a branchless decision SWITCH tool before answering. + +```shell +export CONDUCTOR_SERVER_URL=http://localhost:8080/api +export CONDUCTOR_DECISION_PROVIDER=openrouter +./gradlew :agent-examples:run -PmainClass=org.conductoross.conductor.ai.examples.ExampleDecisionRouting +./gradlew :agent-examples:run -PmainClass=org.conductoross.conductor.ai.examples.ExampleDecisionRouting --args='"The product crashes on startup with error 500. Help troubleshoot it."' +./gradlew :agent-examples:run -PmainClass=org.conductoross.conductor.ai.examples.ExampleDecisionRouter --args=--run +./gradlew :agent-examples:run -PmainClass=org.conductoross.conductor.ai.examples.ExampleDecisionTool +``` + +The router prints its server compilation plan unless `--run` is passed. Workflow and agent names end in `_java` so these examples can coexist with other SDKs. The traditional task uses `.evaluatorType(Switch.DECISION_NAME)` with `model`, optional `provider`, `state`, and a choice question whose name is the SWITCH expression. Its choice keys match the decision-case keys. The server evaluates it synchronously; provider failures fail the workflow without task retries. + ## Framework bridges - **[Google ADK](frameworks/google-adk.md)** — bridge native ADK agents and sub-agent graphs. diff --git a/docs/agents/reference/agent-definition.md b/docs/agents/reference/agent-definition.md index c39fca84d..2e8635eff 100644 --- a/docs/agents/reference/agent-definition.md +++ b/docs/agents/reference/agent-definition.md @@ -15,7 +15,7 @@ below lists its builder method, the JSON key it serializes to, and any behavior | `tools` | `tools(ToolDef...)` | `tools` | List of tool configs. | | `agents` | `agents(Agent...)` | `agents` | Sub-agents (recursive). Requires a strategy. | | `strategy` | `strategy(Strategy)` | `strategy` | Emitted only when sub-agents or planner/fallback slots are present. Default `HANDOFF`. | -| `router` | `router(Agent)` | `router` | For `ROUTER` strategy. Nested agent config. | +| `router` | `router(Agent)` or `router(ToolDef)` | `router` | For `ROUTER` strategy. Nested agent config or decision tool selector (`toolType="decision"`, nonempty `config.model` and `config.questions`, optional `config.provider`). | | `guardrails` | `guardrails(GuardrailDef...)` | `guardrails` | Input/output guardrail configs. | | `maxTurns` | `maxTurns(int)` | `maxTurns` | Default 25. Emitted only if > 0. | | `maxTokens` | `maxTokens(int)` | `maxTokens` | LLM token cap. | diff --git a/docs/agents/reference/agent-schema.json b/docs/agents/reference/agent-schema.json index 5f46e0da7..b9e1f8f7d 100644 --- a/docs/agents/reference/agent-schema.json +++ b/docs/agents/reference/agent-schema.json @@ -55,10 +55,11 @@ "description": "Multi-agent orchestration strategy. Null/absent for a single agent." }, "router": { - "description": "ROUTER strategy router: a nested agent or a worker-task reference.", + "description": "ROUTER strategy router: a nested agent, worker-task reference, or decision selector.", "oneOf": [ { "$ref": "#" }, - { "$ref": "#/$defs/workerRef" } + { "$ref": "#/$defs/workerRef" }, + { "$ref": "#/$defs/decisionRouter" } ] }, "guardrails": { @@ -306,6 +307,18 @@ "taskName": { "type": "string" } } }, + "decisionRouter": { + "type": "object", + "additionalProperties": false, + "required": ["name", "kind", "model", "questions"], + "properties": { + "name": { "type": "string" }, + "kind": { "const": "decision" }, + "model": { "type": "string", "minLength": 1 }, + "provider": { "type": "string" }, + "questions": { "type": "object", "minProperties": 1, "additionalProperties": { "type": "object" } } + } + }, "workerRef": { "type": "object", "additionalProperties": false, From 333c4ff855f06331b11a4453cfc0fad7013aaa8b Mon Sep 17 00:00:00 2001 From: Ling-Sen Peng Date: Thu, 12 Nov 2026 14:20:11 -0800 Subject: [PATCH 2/2] Add JEV decision e2e tests --- .../ai/internal/AgentConfigSerializer.java | 4 +- .../conductor/ai/DecisionRouterTest.java | 5 +- .../sdk/workflow/def/tasks/Switch.java | 18 +- .../sdk/workflow/def/DecisionSwitchTest.java | 7 +- docs/agents/README.md | 2 +- e2e/src/test/java/Suite20DecisionRouting.java | 286 ++++++++++++++++++ 6 files changed, 314 insertions(+), 8 deletions(-) create mode 100644 e2e/src/test/java/Suite20DecisionRouting.java diff --git a/conductor-client-ai/src/main/java/org/conductoross/conductor/ai/internal/AgentConfigSerializer.java b/conductor-client-ai/src/main/java/org/conductoross/conductor/ai/internal/AgentConfigSerializer.java index 372952344..06ecb77d2 100644 --- a/conductor-client-ai/src/main/java/org/conductoross/conductor/ai/internal/AgentConfigSerializer.java +++ b/conductor-client-ai/src/main/java/org/conductoross/conductor/ai/internal/AgentConfigSerializer.java @@ -226,8 +226,8 @@ private Map serializeAgent(Agent agent) { Map selector = new LinkedHashMap<>(); selector.put("name", router.getName()); selector.put("kind", "decision"); - selector.put("model", config.get("model")); - selector.put("questions", config.get("questions")); + selector.put("model", model); + selector.put("questions", questions); if (config.get("provider") != null) { selector.put("provider", config.get("provider")); } diff --git a/conductor-client-ai/src/test/java/org/conductoross/conductor/ai/DecisionRouterTest.java b/conductor-client-ai/src/test/java/org/conductoross/conductor/ai/DecisionRouterTest.java index 74a8dfa3d..31c4669d9 100644 --- a/conductor-client-ai/src/test/java/org/conductoross/conductor/ai/DecisionRouterTest.java +++ b/conductor-client-ai/src/test/java/org/conductoross/conductor/ai/DecisionRouterTest.java @@ -12,6 +12,7 @@ */ package org.conductoross.conductor.ai; +import java.util.List; import java.util.Map; import org.conductoross.conductor.ai.enums.Strategy; @@ -54,7 +55,7 @@ void omitsUnsetProviderAndPreservesDecisionTool() { assertEquals(Map.of("name", "classify", "kind", "decision", "model", "jev-1.13", "questions", QUESTIONS), config.get("router")); Agent normal = Agent.builder().name("assistant").model("openai/gpt-4o-mini").tools(tool).build(); - Map serializedTool = (Map) ((java.util.List) + Map serializedTool = (Map) ((List) new AgentConfigSerializer().serialize(normal).get("tools")).get(0); assertEquals("decision", serializedTool.get("toolType")); assertEquals(tool.getConfig(), serializedTool.get("config")); @@ -62,7 +63,7 @@ void omitsUnsetProviderAndPreservesDecisionTool() { @Test void rejectsInvalidDecisionSelectors() { - for (Map config : java.util.List.of(Map.of(), + for (Map config : List.of(Map.of(), Map.of("model", "", "questions", QUESTIONS), Map.of("model", "jev-1.13", "questions", Map.of()))) { ToolDef tool = ToolDef.builder().name("select").toolType("decision").config(config).build(); diff --git a/conductor-client/src/main/java/com/netflix/conductor/sdk/workflow/def/tasks/Switch.java b/conductor-client/src/main/java/com/netflix/conductor/sdk/workflow/def/tasks/Switch.java index a4606fc29..d9e86daf5 100644 --- a/conductor-client/src/main/java/com/netflix/conductor/sdk/workflow/def/tasks/Switch.java +++ b/conductor-client/src/main/java/com/netflix/conductor/sdk/workflow/def/tasks/Switch.java @@ -21,7 +21,15 @@ import com.netflix.conductor.common.metadata.tasks.TaskType; import com.netflix.conductor.common.metadata.workflow.WorkflowTask; -/** Switch Task */ +/** + * Switch Task. + * + *

By default the case expression is a value parameter, or a JavaScript statement when {@code + * useJavascript} is true. Call {@link #evaluatorType(String)} to use another server evaluator. For + * example, {@link #DECISION_NAME} lets the server choose the branch with a decision model: pass + * {@code model}, {@code state}, {@code questions} and optional {@code provider} as inputs, and set + * the case expression to the name of the choice question whose choices match the case names. + */ public class Switch extends Task { public static final String VALUE_PARAM_NAME = "value-param"; @@ -142,6 +150,14 @@ public Map>> getBranches() { return branches; } + /** + * Returns the evaluator type set explicitly or read from a workflow definition; null if + * neither. + */ + public String getEvaluatorType() { + return evaluatorType; + } + @Override public void updateWorkflowTask(WorkflowTask workflowTask) { diff --git a/conductor-client/src/test/java/com/netflix/conductor/sdk/workflow/def/DecisionSwitchTest.java b/conductor-client/src/test/java/com/netflix/conductor/sdk/workflow/def/DecisionSwitchTest.java index 3b3522163..1abd325f3 100644 --- a/conductor-client/src/test/java/com/netflix/conductor/sdk/workflow/def/DecisionSwitchTest.java +++ b/conductor-client/src/test/java/com/netflix/conductor/sdk/workflow/def/DecisionSwitchTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Conductor Authors. + * Copyright 2025 Conductor 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 @@ -12,6 +12,7 @@ */ package com.netflix.conductor.sdk.workflow.def; +import java.util.List; import java.util.Map; import org.junit.jupiter.api.Test; @@ -31,6 +32,8 @@ void preservesExplicitDecisionEvaluatorAndQuestions() { .input("model", "jev-1.13").input("state", "${workflow.input.request}") .input("questions", Map.of("route", Map.of("type", "choice"))) .switchCase("billing", new Javascript("billing", "'invoice'")); + assertEquals(Switch.DECISION_NAME, decision.getEvaluatorType()); + assertNull(new Switch("route", "${workflow.input.route}").getEvaluatorType()); WorkflowTask task = decision.getWorkflowDefTasks().get(0); assertEquals("SWITCH", task.getType()); assertEquals("decision", task.getEvaluatorType()); @@ -44,7 +47,7 @@ void preservesExplicitDecisionEvaluatorAndQuestions() { @Test void roundTripsDecisionAndLegacySwitchesThroughTaskRegistry() { WorkflowExecutor.initTaskImplementations(); - for (Switch original : java.util.List.of( + for (Switch original : List.of( new Switch("decision", "route").evaluatorType("decision") .input("model", "jev-1.13").switchCase("billing", new Javascript("handle", "'invoice'")), new Switch("value", "${workflow.input.route}"), diff --git a/docs/agents/README.md b/docs/agents/README.md index 15d45bca6..a28671279 100644 --- a/docs/agents/README.md +++ b/docs/agents/README.md @@ -33,7 +33,7 @@ Replace `` with a published version from [Maven Central](https://search ## Decision inference examples -These examples require a server with the synchronous decision evaluator and server-managed Decision credentials. No Java decision worker is needed. The router and tool examples also require a configured chat provider. Set `CONDUCTOR_DECISION_PROVIDER=openrouter` when using OpenRouter and optionally `CONDUCTOR_AGENT_LLM_MODEL=openai/gpt-4o-mini`. +These examples require conductor-oss 3.33.0+ (synchronous decision evaluator) with server-managed Decision credentials. No Java decision worker is needed. The router and tool examples also require a configured chat provider. Set `CONDUCTOR_DECISION_PROVIDER=openrouter` when using OpenRouter and optionally `CONDUCTOR_AGENT_LLM_MODEL=openai/gpt-4o-mini`. - [Traditional workflow](../../agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionRouting.java): one decision-backed SWITCH chooses billing or technical INLINE handling and returns the selected result, answers, usage, cost, and latency. - [Decision router](../../agent-examples/src/main/java/org/conductoross/conductor/ai/examples/ExampleDecisionRouter.java): a decision `ToolDef` selects a child chat agent; `synthesize(false)` returns its result directly. diff --git a/e2e/src/test/java/Suite20DecisionRouting.java b/e2e/src/test/java/Suite20DecisionRouting.java new file mode 100644 index 000000000..480112a29 --- /dev/null +++ b/e2e/src/test/java/Suite20DecisionRouting.java @@ -0,0 +1,286 @@ +/* + * Copyright 2025 Conductor 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. + */ +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.conductoross.conductor.ai.Agent; +import org.conductoross.conductor.ai.AgentConfig; +import org.conductoross.conductor.ai.AgentRuntime; +import org.conductoross.conductor.ai.enums.Strategy; +import org.conductoross.conductor.ai.model.ToolDef; +import org.junit.jupiter.api.*; + +import com.netflix.conductor.client.http.MetadataClient; +import com.netflix.conductor.common.metadata.workflow.WorkflowDef; +import com.netflix.conductor.common.metadata.workflow.WorkflowTask; +import com.netflix.conductor.sdk.workflow.def.ConductorWorkflow; +import com.netflix.conductor.sdk.workflow.def.tasks.Javascript; +import com.netflix.conductor.sdk.workflow.def.tasks.Switch; +import com.netflix.conductor.sdk.workflow.executor.WorkflowExecutor; + +import io.orkes.conductor.client.ApiClient; +import io.orkes.conductor.client.exceptions.AgentAPIException; +import io.orkes.conductor.client.model.agent.CompileResponse; + +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +/** + * Suite 20: JEV decision routing — deterministic, compile/register only. + * + *

Covers the three decision usages without calling any model, so no decision or chat + * credentials are needed: + *

+ * + *

Requires conductor-oss 3.33.0+ (decision support, conductor-oss PR #1664); skipped on + * older servers. + */ +@Tag("e2e") +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class Suite20DecisionRouting extends BaseTest { + + private static final String DECISION_MODEL = "jev-1.13"; + + private static AgentRuntime runtime; + + @BeforeAll + static void setup() { + assumeDecisionCapableServer(); + runtime = new AgentRuntime(new AgentConfig(100, 1)); + } + + @AfterAll + static void teardown() { + if (runtime != null) runtime.close(); + } + + // ── Helpers ─────────────────────────────────────────────────────────── + + /** Skip unless the server version is at least 3.33.0, the first release with decision support. */ + static void assumeDecisionCapableServer() { + String version; + try (HttpClient http = HttpClient.newHttpClient()) { + HttpResponse response = http.send( + HttpRequest.newBuilder() + .uri(URI.create(BASE_URL + "/api/version")) + .timeout(Duration.ofSeconds(5)) + .GET() + .build(), + HttpResponse.BodyHandlers.ofString()); + version = response.body() == null ? "" : response.body().trim(); + } catch (Exception e) { + assumeTrue(false, "Cannot read server version: " + e.getMessage()); + return; + } + Matcher m = Pattern.compile("(\\d+)\\.(\\d+)").matcher(version); + assumeTrue(m.find(), "Unrecognized server version '" + version + "' — skipping decision suite"); + int major = Integer.parseInt(m.group(1)); + int minor = Integer.parseInt(m.group(2)); + assumeTrue(major > 3 || (major == 3 && minor >= 33), + "Server " + version + " predates decision support (needs 3.33.0+) — skipping decision suite"); + } + + private static Map choiceQuestion(Map choices) { + return Map.of("type", "choice", "instructions", "Choose the right support team.", "choices", choices); + } + + private static ToolDef decisionTool(String name, Map questions) { + Map config = new LinkedHashMap<>(); + config.put("model", DECISION_MODEL); + config.put("questions", questions); + return ToolDef.builder().name(name).description("Decision selector.").toolType("decision").config(config) + .build(); + } + + private static Agent decisionRouter(String name, Map choices) { + Agent billing = Agent.builder().name("e2e_java_decision_billing").model(MODEL) + .instructions("Help with billing.").build(); + Agent technical = Agent.builder().name("e2e_java_decision_technical").model(MODEL) + .instructions("Help with product issues.").build(); + return Agent.builder().name(name).strategy(Strategy.ROUTER) + .router(decisionTool(name + "_selector", Map.of("agent", choiceQuestion(choices)))) + .agents(billing, technical).maxTurns(1).synthesize(false).build(); + } + + // ── Tests ───────────────────────────────────────────────────────────── + + /** + * A decision router compiles into a decision SWITCH whose empty branches are the child agents. + * + * COUNTERFACTUAL: if the parent were serialized as external (no model), the server would refuse + * to compile it; if the router were serialized as a nested agent, an LLM task would appear instead. + */ + @Test + @Order(1) + @SuppressWarnings("unchecked") + void test_decision_router_compiles_to_decision_switch() { + Agent agent = decisionRouter("e2e_java_decision_router", Map.of( + "e2e_java_decision_billing", "Payments and invoices", + "e2e_java_decision_technical", "Product errors")); + + CompileResponse plan = runtime.plan(agent); + Map agentDef = getAgentDef(plan); + assertEquals(Boolean.FALSE, agentDef.get("external"), + "decision router parent must not be external. COUNTERFACTUAL: Agent.isExternal() must exclude " + + "ROUTER agents with a decision router."); + Map router = (Map) agentDef.get("router"); + assertNotNull(router, "agentDef.router is missing"); + assertEquals("decision", router.get("kind"), "agentDef.router.kind should be 'decision': " + router); + + List> decisionSwitches = allTasksFlat(plan.getWorkflowDef()).stream() + .filter(t -> "SWITCH".equals(t.get("type")) && "decision".equals(t.get("evaluatorType"))) + .toList(); + assertEquals(1, decisionSwitches.size(), + "expected exactly one decision SWITCH in the compiled router, got " + decisionSwitches.size()); + Map selector = decisionSwitches.get(0); + + assertEquals("agent", selector.get("expression"), "expression should name the agent choice question"); + Map inputs = (Map) selector.get("inputParameters"); + assertEquals(DECISION_MODEL, inputs.get("model")); + assertNotNull(inputs.get("questions"), "questions must be forwarded to the decision SWITCH"); + assertEquals("${workflow.variables.conversation}", inputs.get("state"), + "state must be wired to the conversation"); + + Map> cases = (Map>) selector.get("decisionCases"); + assertEquals(Set.of("e2e_java_decision_billing", "e2e_java_decision_technical"), cases.keySet(), + "decision cases must be the child agent names"); + cases.forEach((name, tasks) -> assertTrue(tasks.isEmpty(), + "selector branch '" + name + "' should be empty; the ROUTER SWITCH runs the agent")); + } + + /** + * The server rejects a decision router whose choices do not match the child agent names. + * + * COUNTERFACTUAL: if this compiled, JEV could select a name with no agent behind it. + */ + @Test + @Order(2) + void test_decision_router_rejects_mismatched_choices() { + Agent agent = decisionRouter("e2e_java_decision_router_mismatch", Map.of( + "billing", "Payments and invoices", + "technical", "Product errors")); + + AgentAPIException ex = assertThrows(AgentAPIException.class, () -> runtime.plan(agent), + "server must refuse a decision router whose choices are not the child agent names"); + assertTrue(ex.getMessage().contains("choices are the configured agent names"), + "unexpected rejection message: " + ex.getMessage()); + } + + /** + * A decision tool's config reaches the compiled workflow so the server can build the SWITCH + * when the LLM calls the tool. + * + * COUNTERFACTUAL: if toolType or config were dropped, the tool would compile as a worker task. + */ + @Test + @Order(3) + @SuppressWarnings("unchecked") + void test_decision_tool_config_reaches_compiled_workflow() { + ToolDef classify = decisionTool("e2e_java_decision_classify", Map.of("department", + choiceQuestion(Map.of("billing", "Payments and invoices", "technical", "Product errors")))); + Agent agent = Agent.builder().name("e2e_java_decision_tool_agent").model(MODEL) + .instructions("Call e2e_java_decision_classify before answering.").tools(classify).build(); + + CompileResponse plan = runtime.plan(agent); + Map agentDef = getAgentDef(plan); + Map tool = ((List>) agentDef.get("tools")).get(0); + assertEquals("decision", tool.get("toolType")); + + assertEquals(DECISION_MODEL, ((Map) tool.get("config")).get("model")); + + // The server builds the decision SWITCH at tool-call time from the config embedded in the + // enrich script, so the compiled workflow must carry it there. + Map enrich = allTasksFlat(plan.getWorkflowDef()).stream() + .filter(t -> "INLINE".equals(t.get("type")) + && String.valueOf(t.get("taskReferenceName")).endsWith("_enrich_tools")) + .findFirst() + .orElseGet(() -> fail("compiled workflow has no *_enrich_tools INLINE task")); + String script = String.valueOf(((Map) enrich.get("inputParameters")).get("expression")); + assertTrue(script.contains("e2e_java_decision_classify") && script.contains(DECISION_MODEL), + "enrich script must embed the decision tool config. COUNTERFACTUAL: without it the tool call " + + "cannot be mapped to a decision SWITCH."); + } + + /** + * A decision SWITCH registers on the server and reads back with its evaluator, expression, + * inputs, and branches intact, and converts back into an SDK Switch unchanged. + * + * COUNTERFACTUAL: if Switch rewrote the evaluator to value-param, the server would store + * value-param and never call the decision model. + */ + @Test + @Order(4) + void test_decision_switch_registers_and_round_trips() { + WorkflowExecutor.initTaskImplementations(); + ConductorWorkflow> workflow = new ConductorWorkflow<>(null); + workflow.setName("e2e_java_decision_switch"); + workflow.setVersion(1); + workflow.setOwnerEmail("e2e@example.com"); + Switch decision = new Switch("route_request", "route") + .evaluatorType(Switch.DECISION_NAME) + .input("model", DECISION_MODEL) + .input("state", "${workflow.input.request}") + .input("questions", Map.of("route", + choiceQuestion(Map.of("billing", "Payments", "technical", "Errors")))) + .switchCase("billing", new Javascript("handle_billing", "'billing'")) + .switchCase("technical", new Javascript("handle_technical", "'technical'")); + workflow.add(decision); + + ApiClient client = new ApiClient(SERVER_URL); + MetadataClient metadata = new MetadataClient(client); + try { + metadata.updateWorkflowDefs(List.of(workflow.toWorkflowDef())); + + WorkflowDef stored = metadata.getWorkflowDef("e2e_java_decision_switch", 1); + WorkflowTask task = stored.getTasks().get(0); + assertEquals("SWITCH", task.getType()); + assertEquals("decision", task.getEvaluatorType(), + "server stored evaluatorType " + task.getEvaluatorType() + + ". COUNTERFACTUAL: Switch.evaluatorType(...) must not be rewritten to value-param."); + assertEquals("route", task.getExpression()); + assertEquals(DECISION_MODEL, task.getInputParameters().get("model")); + assertFalse(task.getInputParameters().containsKey("switchCaseValue"), + "decision SWITCH must not carry the value-param switchCaseValue input"); + assertEquals(Set.of("billing", "technical"), task.getDecisionCases().keySet()); + + WorkflowTask roundTripped = + ConductorWorkflow.fromWorkflowDef(stored).toWorkflowDef().getTasks().get(0); + assertEquals(task.getEvaluatorType(), roundTripped.getEvaluatorType()); + assertEquals(task.getExpression(), roundTripped.getExpression()); + assertEquals(task.getInputParameters(), roundTripped.getInputParameters()); + assertEquals(task.getDecisionCases().keySet(), roundTripped.getDecisionCases().keySet()); + } finally { + try { + metadata.unregisterWorkflowDef("e2e_java_decision_switch", 1); + } catch (Exception ignored) { + // Best-effort cleanup; never mask the test result. + } + client.shutdown(); + } + } +}