diff --git a/liteflow-react-agent/liteflow-react-agent-core/src/main/java/com/yomahub/liteflow/agent/component/ReActAgentComponent.java b/liteflow-react-agent/liteflow-react-agent-core/src/main/java/com/yomahub/liteflow/agent/component/ReActAgentComponent.java index 8a8580b30..ce1291870 100644 --- a/liteflow-react-agent/liteflow-react-agent-core/src/main/java/com/yomahub/liteflow/agent/component/ReActAgentComponent.java +++ b/liteflow-react-agent/liteflow-react-agent-core/src/main/java/com/yomahub/liteflow/agent/component/ReActAgentComponent.java @@ -8,35 +8,40 @@ import com.yomahub.liteflow.agent.tool.WorkspaceFileTools; import com.yomahub.liteflow.core.NodeComponent; import com.yomahub.liteflow.property.LiteflowConfigGetter; import com.yomahub.liteflow.property.agent.AgentConfig; +import com.yomahub.liteflow.property.agent.MemoryStorageConfig; import com.yomahub.liteflow.property.agent.ShellMode; import com.yomahub.liteflow.slot.Slot; import io.agentscope.core.ReActAgent; import io.agentscope.core.hook.Hook; import io.agentscope.core.memory.InMemoryMemory; import io.agentscope.core.message.Msg; +import com.yomahub.liteflow.agent.model.ModelSpec; import io.agentscope.core.model.Model; import io.agentscope.core.tool.Toolkit; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; /** - * Abstract LiteFlow component that wraps an agentscope ReActAgent. + * 封装 agentscope ReActAgent 的 LiteFlow 抽象组件。 *
- * Subclasses must provide {@link #buildModel}, {@link #systemPrompt}, and - * {@link #userPrompt}. Optional overrides allow custom tools, hooks, and - * lifecycle callbacks. + * 子类必须提供 {@link #model}、{@link #systemPrompt} 和 + * {@link #userPrompt}。可选覆写方法用于自定义工具、钩子和生命周期回调。 *
- * The {@link #process()} method is {@code final} so that the framework can - * guarantee proper session management and agent lifecycle. + * {@link #process()} 方法被声明为 {@code final},由框架统一保证 session + * 管理和 agent 生命周期的正确性。 */ public abstract class ReActAgentComponent extends NodeComponent { - /* ===== Framework-provided final accessor ===== */ + private static final Logger LOG = LoggerFactory.getLogger(ReActAgentComponent.class); + + /* ===== 框架提供的 final 访问器 ===== */ /** - * Returns the agent section of the current LiteflowConfig. + * 返回当前 LiteflowConfig 中的 agent 配置段。 * - * @throws AgentConfigException if liteflow.agent has not been configured + * @throws AgentConfigException 当 liteflow.agent 尚未配置时抛出 */ protected final AgentConfig agentConfig() { AgentConfig c = LiteflowConfigGetter.get().getAgent(); @@ -47,82 +52,95 @@ public abstract class ReActAgentComponent extends NodeComponent { return c; } - /* ===== Must implement ===== */ + /* ===== 必须实现 ===== */ /** - * Build the {@link Model} instance used by the ReActAgent. - * Subclasses typically choose a specific provider (OpenAI, Anthropic, etc.) - * based on configuration from {@link AgentConfig}. + * 构建本组件使用的模型描述符。子类按"哪个平台 + 哪个模型 + 可选高级参数" + * 三段式给出,由框架负责从 {@link AgentConfig} 解析 credential 并构造 + * agentscope {@link Model}。例如: + *
{@code
+ * return DeepSeek.of("deepseek-chat").temperature(0.7);
+ * }
*/
- protected abstract Model buildModel(ReActAgentContext ctx);
+ protected abstract ModelSpec> model(ReActAgentContext ctx);
/**
- * Return the system prompt for the agent. Called once when the agent is built.
+ * Escape hatch:高级用户可整体绕过 {@link ModelSpec} 自行构造 {@link Model}。
+ * 默认实现委派给 {@code model(ctx).resolve(agentConfig())},无需覆写。
+ */
+ protected Model buildModel(ReActAgentContext ctx) {
+ return model(ctx).resolve(agentConfig());
+ }
+
+ /**
+ * 返回 agent 的系统提示词。构建 agent 时只调用一次。
*/
protected abstract String systemPrompt(ReActAgentContext ctx);
/**
- * Return the user prompt for this execution. Called on every {@link #process()}.
+ * 返回本次执行的用户提示词。每次 {@link #process()} 都会调用。
*/
protected abstract String userPrompt(ReActAgentContext ctx);
- /* ===== Optional overrides ===== */
+ /* ===== 可选覆写 ===== */
/**
- * Provide additional tool objects to register with the agent's {@link Toolkit}.
- * Each object's methods annotated with {@code @Tool} will be discovered automatically.
- * Returns an empty list by default.
+ * 提供要注册到 agent {@link Toolkit} 中的额外工具对象。
+ * 对象中标注了 {@code @Tool} 的方法会被自动发现。
+ * 默认返回空列表。
*/
protected List