Class AgentContext

    • Constructor Detail

      • AgentContext

        public AgentContext()
    • Method Detail

      • create

        public static AgentContext create​(Config config)
        Create an AgentContext object
        Parameters:
        config - Config object
        Returns:
        A AgentContext object
      • conversation

        public CompletableFuture<ConversationResponse> conversation​(String agentId,
                                                                    String query,
                                                                    String chatUid,
                                                                    String parentMessageId)
                                                             throws OpenApiException
        Start a conversation with the specified Agent, blocking until the run succeeds, is interrupted, or fails.
         
         import com.longbridge.*;
         import com.longbridge.agent.*;
        
         class Main {
             public static void main(String[] args) throws Exception {
                 OAuth oauth = new OAuthBuilder("your-client-id")
                     .build(url -> System.out.println("Visit: " + url)).get();
                 try (Config config = Config.fromOAuth(oauth); AgentContext ctx = AgentContext.create(config)) {
                     WorkspacesResponse workspaces = ctx.workspaces().get();
                     AgentsResponse agents = ctx.agents(workspaces.getWorkspaces()[0].getId(), null).get();
                     ConversationResponse resp = ctx.conversation(agents.getAgents()[0].getUid(),
                             "How has Tesla stock performed recently?", null, null).get();
                     System.out.println(resp);
                 }
             }
         }
         
         
        Parameters:
        agentId - Agent UID
        query - User query
        chatUid - Conversation identifier to continue an existing chat, or null to start a new one
        parentMessageId - messageId from a previous response. Pass it when asking a follow-up in an existing conversation to attach the new message after the specified one, keeping the message stream in order. Only valid together with chatUid, the parent message must belong to that conversation, and it must be null for a new conversation
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • continueConversation

        public CompletableFuture<ConversationResponse> continueConversation​(String agentId,
                                                                            String chatUid,
                                                                            String messageId,
                                                                            Map<String,​Map<String,​String>> answersByToolCall)
                                                                     throws OpenApiException
        Resume an interrupted conversation, blocking until the run succeeds, is interrupted again, or fails.
        Parameters:
        agentId - Agent UID
        chatUid - Conversation identifier
        messageId - ID of the paused message (see Interrupt.getMessageId())
        answersByToolCall - Answers keyed by toolCallId, each value being a map of question text to answer; may be null if there is nothing to answer
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • conversationStream

        public Flow.Publisher<ConversationStreamEvent> conversationStream​(String agentId,
                                                                          String query,
                                                                          String chatUid,
                                                                          String parentMessageId)
        Start a conversation with the specified Agent, returning a Flow.Publisher of run-progress events over SSE. The run's outcome is carried by a WorkflowFinishedEvent (succeeded, failed, or stopped) or, if the Agent needs more input from you, a HumanInteractionRequiredEvent instead (unless the stream itself errors first, delivered via Flow.Subscriber#onError) — an interrupted run never emits a WorkflowFinishedEvent. Neither is necessarily the last event delivered — the server may still emit a few more housekeeping events (e.g. a ChatTitleUpdatedEvent) before actually closing the connection, so keep consuming until onComplete rather than stopping as soon as you see one.

        This method itself performs no I/O — it returns a cold Flow.Publisher immediately; the HTTP/SSE connection is only established once a subscriber calls subscribe, matching Reactive Streams' lazy-publisher convention. The returned publisher carries real backpressure: no more events are pulled off the stream than have been requested via Flow.Subscription.request(long).

         
         Flow.Publisher<ConversationStreamEvent> publisher =
             ctx.conversationStream(agentId, "How has Tesla stock performed recently?", null, null);
         publisher.subscribe(new Flow.Subscriber<ConversationStreamEvent>() {
             public void onSubscribe(Flow.Subscription subscription) {
                 subscription.request(Long.MAX_VALUE); // unbounded demand
             }
             public void onNext(ConversationStreamEvent event) {
                 System.out.println(event);
             }
             public void onError(Throwable err) {
                 System.out.println("failed: " + err.getMessage());
             }
             public void onComplete() {
                 System.out.println("done");
             }
         });
         
         
        Parameters:
        agentId - Agent UID
        query - User query
        chatUid - Conversation identifier to continue an existing chat, or null to start a new one
        parentMessageId - messageId from a previous response, to attach a follow-up after that message. Only valid together with chatUid and must be null for a new conversation
        Returns:
        A cold Flow.Publisher of conversation stream events