接口 ToolExecutionContextProvider

函数接口:
这是一个函数接口, 因此可用作 lambda 表达式或方法引用的赋值目标。

@FunctionalInterface public interface ToolExecutionContextProvider
Provider interface for resolving ToolExecutionContext from external sources.

This interface enables integration with dependency injection frameworks like Spring, allowing tool execution contexts to be resolved from IoC containers, request scopes, thread-local storage, or any other context management mechanism.

Spring Integration Example:


 // 1. Define your context POJO with Spring scope
 @Component
 @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
 public class UserContext {
     private String userId;
     private String sessionId;
     private Map<String, String> permissions;
     // getters/setters...
 }

 // 2. Implement ToolExecutionContextProvider
 public class SpringContextProvider implements ToolExecutionContextProvider {
     private final ApplicationContext applicationContext;

     public SpringContextProvider(ApplicationContext applicationContext) {
         this.applicationContext = applicationContext;
     }

     @Override
     public <T> T resolveContext(Class<T> targetType) {
         try {
             // Try to resolve from Spring container
             if (targetType == ToolExecutionContext.class) {
                 // Convert Spring bean to ToolExecutionContext
                 UserContext userCtx = applicationContext.getBean(UserContext.class);
                 return (T) ToolExecutionContext.of(userCtx);
             } else {
                 // Direct bean resolution for custom types
                 return applicationContext.getBean(targetType);
             }
         } catch (Exception e) {
             return null; // Context not available
         }
     }
 }

 // 3. Register provider in Spring configuration
 @Configuration
 public class AgentScopeConfig {
     @Bean
     public ToolExecutionContextProvider contextProvider(ApplicationContext ctx) {
         SpringContextProvider provider = new SpringContextProvider(ctx);
         ToolExecutionContext.setContextProvider(provider);
         return provider;
     }
 }

 // 4. Use in tool methods - context auto-resolved from Spring!
 @Tool(name = "query_db", description = "Query database")
 public ToolResultBlock queryDb(
     @ToolParam(name = "query") String query,
     UserContext context  // Auto-resolved from Spring RequestScope!
 ) {
     String userId = context.getUserId();
     // ...
 }
 

ThreadLocal Example:


 public class ThreadLocalContextProvider implements ToolExecutionContextProvider {
     private static final ThreadLocal<ToolExecutionContext> CONTEXT = new ThreadLocal<>();

     public static void setContext(ToolExecutionContext ctx) {
         CONTEXT.set(ctx);
     }

     public static void clear() {
         CONTEXT.remove();
     }

     @Override
     public <T> T resolveContext(Class<T> targetType) {
         ToolExecutionContext ctx = CONTEXT.get();
         if (ctx == null) return null;

         if (targetType == ToolExecutionContext.class) {
             return (T) ctx;
         } else {
             return ctx.as(targetType);
         }
     }
 }
 
  • 方法概要

    修饰符和类型
    方法
    说明
    <T> T
    resolveContext(Class<T> targetType)
    Resolves a context instance of the specified type.
  • 方法详细资料

    • resolveContext

      <T> T resolveContext(Class<T> targetType)
      Resolves a context instance of the specified type.

      This method is called by the framework when a tool method parameter requires context injection but no explicit context was provided at the call site. The implementation should attempt to resolve the context from its managed source (Spring container, ThreadLocal, etc.) and return an instance of the requested type.

      类型参数:
      T - The target type
      参数:
      targetType - The target type to resolve (ToolExecutionContext or custom POJO)
      返回:
      The resolved context instance, or null if not available