eemario commented on code in PR #1397:
URL: https://github.com/apache/flink-agents/pull/1397#discussion_r4676488983


##########
api/src/main/java/org/apache/flink/agents/api/agents/AgentExecutionOptions.java:
##########
@@ -124,4 +124,16 @@ public class AgentExecutionOptions {
                             "short-term-memory.state-ttl.visibility",
                             ShortTermMemoryTtlVisibility.class,
                             ShortTermMemoryTtlVisibility.NEVER_RETURN_EXPIRED);
+
+    public static final ConfigOption<Integer> LOOP_MAX_TURNS =

Review Comment:
   Done in 60b8af237. Budget options now live in ReActLoopBudgetOptions; the 
Python class stays in the existing core_options module.



##########
api/src/main/java/org/apache/flink/agents/api/chat/model/BaseChatModelSetup.java:
##########
@@ -292,6 +292,15 @@ public List<ChatMessage> prepareRequestMessages(
         return messages;
     }
 
+    /** Applies the setup prompt and calls the model without tools or 
tool-only parameters. */
+    public ChatResult chatWithoutTools(

Review Comment:
   Done in 60b8af237. Java and Python now prepare the messages and reuse chat 
with an explicit empty tool list. Removed chatWithoutTools and retained the 
plain-chat parameter behavior; bridge and tool-parameter tests cover both paths.



##########
api/src/main/java/org/apache/flink/agents/api/agents/ReActLoopBudget.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.flink.agents.api.agents;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonValue;
+
+import javax.annotation.Nullable;
+
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+/** Independent limits for a single chat request and its tool rounds. */
+public final class ReActLoopBudget {
+    public enum BudgetMode {
+        SOFT,
+        HARD
+    }
+
+    @Nullable private final Integer maxTurns;
+    @Nullable private final Long maxTokens;
+    @Nullable private final Duration maxTime;
+    private final BudgetMode mode;
+
+    private ReActLoopBudget(
+            @Nullable Integer maxTurns,
+            @Nullable Duration maxTime,
+            @Nullable Long maxTokens,
+            BudgetMode mode) {
+        if (maxTurns != null && maxTurns <= 0 || maxTokens != null && 
maxTokens <= 0)
+            throw new IllegalArgumentException("ReAct loop limits must be 
positive");
+        if (maxTime != null
+                && (maxTime.isNegative() || maxTime.isZero() || 
maxTime.getNano() % 1_000_000 != 0))
+            throw new IllegalArgumentException(
+                    "max_time must be positive with millisecond precision");
+        if (maxTime != null) maxTime.toMillis();
+        this.maxTurns = maxTurns;
+        this.maxTokens = maxTokens;
+        this.maxTime = maxTime;
+        this.mode = Objects.requireNonNull(mode, "mode");
+    }
+
+    /** Creates a builder with unlimited budgets and soft termination. */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /** Builder for an immutable request-scoped budget. */
+    public static final class Builder {
+        @Nullable private Integer maxTurns;
+        @Nullable private Duration maxTime;
+        @Nullable private Long maxTokens;
+        private BudgetMode mode = BudgetMode.SOFT;
+
+        private Builder() {}
+
+        /** Sets the maximum ordinary inference rounds; null means unlimited. 
*/
+        public Builder maxTurns(@Nullable Integer maxTurns) {
+            this.maxTurns = maxTurns;
+            return this;
+        }
+
+        /** Sets the elapsed-time limit; null means unlimited. */
+        public Builder maxTime(@Nullable Duration maxTime) {
+            this.maxTime = maxTime;
+            return this;
+        }
+
+        /** Sets the cumulative input/output token limit; null means 
unlimited. */
+        public Builder maxTokens(@Nullable Long maxTokens) {
+            this.maxTokens = maxTokens;
+            return this;
+        }
+
+        /** Sets the termination mode; null selects the default SOFT mode. */
+        public Builder mode(@Nullable BudgetMode mode) {
+            this.mode = mode == null ? BudgetMode.SOFT : mode;
+            return this;
+        }
+
+        /**
+         * Builds a budget after validating its limits.
+         *
+         * @throws IllegalArgumentException if a limit is non-positive or time 
has sub-millisecond
+         *     precision
+         * @throws ArithmeticException if the time limit cannot be represented 
in milliseconds as a
+         *     long
+         */
+        public ReActLoopBudget build() {
+            return new ReActLoopBudget(maxTurns, maxTime, maxTokens, mode);
+        }
+    }
+
+    @Nullable
+    public Integer getMaxTurns() {
+        return maxTurns;
+    }
+
+    @Nullable
+    public Long getMaxTokens() {
+        return maxTokens;
+    }
+
+    @Nullable
+    public Duration getMaxTime() {
+        return maxTime;
+    }
+
+    public BudgetMode getMode() {
+        return mode;
+    }
+
+    @JsonValue
+    public Map<String, Object> toMap() {

Review Comment:
   Done in 60b8af237. Java uses Jackson annotations and JavaTimeModule, and 
Python uses Pydantic fields. A small duration serializer keeps ISO durations in 
days/seconds because Java Duration does not accept Pydantic's year 
representation. Cross-language round-trip tests cover long durations too.



##########
api/src/main/java/org/apache/flink/agents/api/agents/ReActLoopBudget.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.flink.agents.api.agents;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonValue;
+
+import javax.annotation.Nullable;
+
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+/** Independent limits for a single chat request and its tool rounds. */
+public final class ReActLoopBudget {
+    public enum BudgetMode {
+        SOFT,
+        HARD
+    }
+
+    @Nullable private final Integer maxTurns;
+    @Nullable private final Long maxTokens;
+    @Nullable private final Duration maxTime;
+    private final BudgetMode mode;
+
+    private ReActLoopBudget(
+            @Nullable Integer maxTurns,
+            @Nullable Duration maxTime,
+            @Nullable Long maxTokens,
+            BudgetMode mode) {
+        if (maxTurns != null && maxTurns <= 0 || maxTokens != null && 
maxTokens <= 0)
+            throw new IllegalArgumentException("ReAct loop limits must be 
positive");
+        if (maxTime != null
+                && (maxTime.isNegative() || maxTime.isZero() || 
maxTime.getNano() % 1_000_000 != 0))
+            throw new IllegalArgumentException(
+                    "max_time must be positive with millisecond precision");
+        if (maxTime != null) maxTime.toMillis();
+        this.maxTurns = maxTurns;
+        this.maxTokens = maxTokens;
+        this.maxTime = maxTime;
+        this.mode = Objects.requireNonNull(mode, "mode");
+    }
+
+    /** Creates a builder with unlimited budgets and soft termination. */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /** Builder for an immutable request-scoped budget. */
+    public static final class Builder {

Review Comment:
   Done in 60b8af237; Builder is now at the end of ReActLoopBudget.



##########
plan/src/main/java/org/apache/flink/agents/plan/actions/ReActLoopBudgetUtils.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.flink.agents.plan.actions;
+
+import org.apache.flink.agents.api.agents.AgentExecutionOptions;
+import org.apache.flink.agents.api.agents.ReActLoopBudget;
+import org.apache.flink.agents.api.chat.messages.ChatResult;
+import org.apache.flink.agents.api.chat.messages.TokenUsage;
+import org.apache.flink.agents.api.context.DurableCallable;
+import org.apache.flink.agents.api.context.MemoryObject;
+import org.apache.flink.agents.api.context.RunnerContext;
+import org.apache.flink.agents.api.event.ChatRequestEvent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+/** Request-scoped loop progress stored in sensory memory as checkpoint-safe 
values. */
+final class ReActLoopBudgetUtils {
+    private ReActLoopBudgetUtils() {}
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(ReActLoopBudgetUtils.class);
+    static final String LOOP_BUDGET_CONTEXT = "_LOOP_BUDGET_CONTEXT";
+
+    static void initialize(RunnerContext ctx, ChatRequestEvent event) throws 
Exception {

Review Comment:
   Done in 60b8af237. Separated budget resolution and time initialization, 
removed redundant checks, extracted state-key constants, and used 
BudgetMode.SOFT.name(). Applied the corresponding Python cleanup.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to