When enabled (default), a {@link CodeactToolSignatureAgentHook} is automatically
+ * registered, which injects Python class/function stubs for all registered CodeactTools
+ * into the messages before the Agent starts. This allows the LLM to correctly reference
+ * these tools when writing code in {@code write_code}.
+ *
+ *
Disable this if you provide your own tool signature injection mechanism
+ * (e.g., a custom PromptContributor).
+ */
+ public CodeactAgentBuilder enableToolSignatureInjection(boolean enable) {
+ this.enableToolSignatureInjection = enable;
+ return this;
+ }
+
/**
* Allow IO operations in GraalVM (security)
*/
@@ -578,6 +596,12 @@ public CodeactAgent build() {
this.hooks.add(new CodeactToolsStateInitHook(this.codeactToolRegistry));
logger.info("CodeactAgentBuilder#build - reason=自动注册CodeactToolsStateInitHook");
+ // 自动注册 CodeactToolSignatureAgentHook,将工具的 Python 签名注入到 Prompt 中
+ if (this.enableToolSignatureInjection) {
+ this.hooks.add(new CodeactToolSignatureAgentHook(this.codeactToolRegistry, this.language));
+ logger.info("CodeactAgentBuilder#build - reason=自动注册CodeactToolSignatureAgentHook");
+ }
+
// Initialize CodeContext if not provided
if (this.codeContext == null) {
this.codeContext = new CodeContext(this.language);
diff --git a/assistant-agent-autoconfigure/src/main/java/com/alibaba/assistant/agent/autoconfigure/hook/CodeactToolSignatureAgentHook.java b/assistant-agent-autoconfigure/src/main/java/com/alibaba/assistant/agent/autoconfigure/hook/CodeactToolSignatureAgentHook.java
new file mode 100644
index 00000000..cb193247
--- /dev/null
+++ b/assistant-agent-autoconfigure/src/main/java/com/alibaba/assistant/agent/autoconfigure/hook/CodeactToolSignatureAgentHook.java
@@ -0,0 +1,167 @@
+/*
+ * Copyright 2024-2025 the original author or 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
+ *
+ * https://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.alibaba.assistant.agent.autoconfigure.hook;
+
+import com.alibaba.assistant.agent.common.constant.HookPriorityConstants;
+import com.alibaba.assistant.agent.common.enums.Language;
+import com.alibaba.assistant.agent.core.tool.CodeactToolRegistry;
+import com.alibaba.cloud.ai.graph.OverAllState;
+import com.alibaba.cloud.ai.graph.RunnableConfig;
+import com.alibaba.cloud.ai.graph.agent.hook.AgentHook;
+import com.alibaba.cloud.ai.graph.agent.hook.HookPosition;
+import com.alibaba.cloud.ai.graph.agent.hook.HookPositions;
+import com.alibaba.cloud.ai.graph.agent.hook.JumpTo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.ai.chat.messages.AssistantMessage;
+import org.springframework.ai.chat.messages.Message;
+import org.springframework.ai.chat.messages.ToolResponseMessage;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * CodeactTool 签名注入 Hook
+ *
+ *