Class AgentConnectorImpl

java.lang.Object
org.cibseven.connect.impl.AbstractConnector<AgentRequest,AgentResponse>
org.cibseven.connect.ai.agent.impl.AgentConnectorImpl
All Implemented Interfaces:
AgentConnector, Connector<AgentRequest>

public class AgentConnectorImpl extends AbstractConnector<AgentRequest,AgentResponse> implements AgentConnector
Core implementation of the LangChain4j agent connector.

Execution flow per invocation:

  1. Instantiate any tool objects listed in toolClasses (fail fast before network I/O).
  2. Resolve the base URL from the request parameter, the OPENAI_BASE_URL environment variable, or AgentConnectorConstants.DEFAULT_BASE_URL.
  3. Resolve the API key from the request parameter or, for the configured endpoint only, the OPENAI_API_KEY environment variable.
  4. Build an OpenAiChatModel from the request parameters.
  5. Create a stateless AiServices proxy and invoke it with the user message.
  6. Return an AgentResponse containing the output text and a correlation UUID.

Within an agentic ad hoc scope's turn, the model loop keeps a hallucinated tool name, a tool failure, and a tool refusal in bounds rather than failing the step:

  • A tool name outside the scope's catalogue is corrected in chat memory and costs the turn's model-call cap exactly the one call that made it up — see hallucinatedToolMessage(dev.langchain4j.agent.tool.ToolExecutionRequest, org.cibseven.connect.ai.agent.impl.AgentConnectorImpl.AvailableTools) and resolveHallucinatedTool(org.cibseven.connect.ai.agent.AgentRequest, java.lang.String, java.lang.String, org.cibseven.connect.ai.agent.impl.AgentConnectorImpl.AvailableTools) (CIB7-2150).
  • An engine failure ends the step rather than reaching the model in a transaction that can no longer commit — see toolFailure(java.lang.Throwable, dev.langchain4j.service.tool.ToolErrorContext) (CIB7-2141).
  • A refusal (wrong arguments, a blocked activity) is an answer the model reads and may act on in its next call — see immediate(dev.langchain4j.service.tool.ToolProvider) and AgentConnectorImpl.FailureAsResult.
  • Constructor Details

    • AgentConnectorImpl

      public AgentConnectorImpl()
  • Method Details

    • createRequest

      public AgentRequest createRequest()
      Description copied from interface: Connector
      Create a request on the connector.
      Specified by:
      createRequest in interface Connector<AgentRequest>
      Returns:
      the connector-specific request object.
    • execute

      public AgentResponse execute(AgentRequest request)
      Description copied from interface: Connector
      Execute the request on the connector.
      Specified by:
      execute in interface Connector<AgentRequest>
      Parameters:
      request - the request
      Returns:
      the result.
    • createChatMemoryProvider

      protected dev.langchain4j.memory.chat.ChatMemoryProvider createChatMemoryProvider(AgentRequest request)
      Factory method — builds the ChatMemoryProvider used when chat memory is active. Each provider call returns a MessageWindowChatMemory bound to whatever store AgentChatMemoryStore currently holds, so how long the conversation survives depends on that store rather than on this factory.

      With the default ProcessVariableChatMemoryStore it survives between connector invocations, across engine restarts and across engine replicas. It does not when the deployment has switched the store to the JVM-local one, when a custom store was installed via AgentChatMemoryStore.setStore(dev.langchain4j.store.memory.chat.ChatMemoryStore), or when the connector runs outside an engine command context — the process-variable store then falls back to an in-memory buffer.

    • createMcpClients

      protected List<dev.langchain4j.mcp.client.McpClient> createMcpClients(AgentRequest request)
      Factory method — override in tests to inject stubbed MCP clients.

      Builds one client per entry of the mcpServers JSON array. Returns an empty list when mcpServers is not configured.

    • buildMcpClient

      protected dev.langchain4j.mcp.client.McpClient buildMcpClient(String url, Map<String,String> headers)
      Factory method for a single MCP client — override in tests to capture arguments and avoid opening a real transport connection.
    • createContentRetriever

      protected dev.langchain4j.rag.content.retriever.ContentRetriever createContentRetriever(AgentRequest request)
      Factory method — builds and returns a ContentRetriever backed by pgvector when pgHost is present in the request, or null to skip RAG entirely. Override in tests to inject a stubbed retriever.

      The returned ContentRetriever wraps the underlying EmbeddingStoreContentRetriever to (a) keep the existing SLF4J logging and (b) emit a type: "retrieval" event into the AgentChatListener active on the calling thread (via ProcessStarterToolContext.getActiveListener()). The retrieval event carries the query, embedding-model identity, the pgvector store coordinates (host/port/database/table/dimension — never the password), the retrieval parameters (maxResults, minScore), and per-chunk {score, content, length, metadata} entries. Query and chunk text are run through AgentChatListener.maybeRedact(String) so they honour the same redactContent opt-in that chat events already respect. On failure the event carries errorClass, message, and a short stack identical in shape to onError — see EU AI Act Art. 10 (data governance) / Art. 12 (record-keeping) / Art. 26 (deployer monitoring).

    • createRagEmbeddingStore

      protected dev.langchain4j.store.embedding.EmbeddingStore<dev.langchain4j.data.segment.TextSegment> createRagEmbeddingStore(AgentRequest request)
      Factory method — builds the pgvector embedding store used as the RAG backend. Override in tests to inject a stubbed store and avoid opening a real database connection.
    • createEmbeddingModel

      protected dev.langchain4j.model.embedding.EmbeddingModel createEmbeddingModel(AgentRequest request)
      Factory method — override in tests to inject a stubbed embedding model. Uses OpenAiEmbeddingModel at AgentConnectorConstants.DEFAULT_BASE_URL when embeddingModelName is set on the request (the request's baseUrl is not used), otherwise falls back to the local AllMiniLmL6V2EmbeddingModel. The key comes from resolveAgentEmbeddingApiKey(org.cibseven.connect.ai.agent.AgentRequest).
    • createChatModel

      protected dev.langchain4j.model.chat.ChatModel createChatModel(AgentRequest request, String apiKey, String baseUrl, Map<String,String> customHeaders)
      Factory method — override in tests to inject a stubbed chat model.

      Selects the OpenAI Responses API (OpenAiResponsesChatModel) when reasoningSummary is set on the request — that field is only available on the Responses API. Otherwise builds the standard OpenAiChatModel. reasoningEffort, when present, is forwarded to whichever builder is used.