|
| 1 | +/* |
| 2 | + * Copyright 2025-2026 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package com.agentclientprotocol.sdk.agent; |
| 6 | + |
| 7 | +import java.util.function.Function; |
| 8 | + |
| 9 | +import com.agentclientprotocol.sdk.spec.AcpAgentTransport; |
| 10 | +import com.agentclientprotocol.sdk.util.Assert; |
| 11 | + |
| 12 | +/** |
| 13 | + * Factory for creating one ACP agent runtime for one agent-side transport. |
| 14 | + * |
| 15 | + * <p> |
| 16 | + * Listener-backed transports such as remote HTTP transports accept multiple client |
| 17 | + * connections over their lifetime. Each accepted connection needs its own |
| 18 | + * connection-bound agent runtime while reusing the same agent definition. This factory |
| 19 | + * is the explicit public seam for that relationship. |
| 20 | + * </p> |
| 21 | + * |
| 22 | + * @author Kaiser Dandangi |
| 23 | + */ |
| 24 | +@FunctionalInterface |
| 25 | +public interface AcpAgentFactory { |
| 26 | + |
| 27 | + /** |
| 28 | + * Creates a new asynchronous agent runtime for the supplied transport. |
| 29 | + * @param transport per-connection transport |
| 30 | + * @return a fresh asynchronous agent runtime |
| 31 | + */ |
| 32 | + AcpAsyncAgent create(AcpAgentTransport transport); |
| 33 | + |
| 34 | + /** |
| 35 | + * Creates a factory from an asynchronous agent builder function. |
| 36 | + * @param factory function that creates a fresh asynchronous agent per transport |
| 37 | + * @return an agent factory |
| 38 | + */ |
| 39 | + static AcpAgentFactory async(Function<AcpAgentTransport, AcpAsyncAgent> factory) { |
| 40 | + Assert.notNull(factory, "The async factory can not be null"); |
| 41 | + return factory::apply; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Creates a factory from a synchronous agent builder function. |
| 46 | + * |
| 47 | + * <p> |
| 48 | + * Synchronous agents are wrappers around asynchronous agents in this SDK, so the |
| 49 | + * transport seam remains asynchronous underneath while callers may still author |
| 50 | + * agents with the blocking API. |
| 51 | + * </p> |
| 52 | + * @param factory function that creates a fresh synchronous agent per transport |
| 53 | + * @return an agent factory |
| 54 | + */ |
| 55 | + static AcpAgentFactory sync(Function<AcpAgentTransport, AcpSyncAgent> factory) { |
| 56 | + Assert.notNull(factory, "The sync factory can not be null"); |
| 57 | + return transport -> factory.apply(transport).async(); |
| 58 | + } |
| 59 | + |
| 60 | +} |
0 commit comments