Package com.longbridge.agent
Class AgentContext
- java.lang.Object
-
- com.longbridge.agent.AgentContext
-
- All Implemented Interfaces:
AutoCloseable
public class AgentContext extends Object implements AutoCloseable
AI Agent conversation context.Reference: https://open.longbridge.com/en/docs/ai/chat/conversation
-
-
Constructor Summary
Constructors Constructor Description AgentContext()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CompletableFuture<AgentsResponse>agents(String workspaceId, GetAgentsOptions opts)List the Agents in the specified Workspace.voidclose()CompletableFuture<ConversationResponse>continueConversation(String agentId, String chatUid, String messageId, Map<String,Map<String,String>> answersByToolCall)Resume an interrupted conversation, blocking until the run succeeds, is interrupted again, or fails.Flow.Publisher<ConversationStreamEvent>continueConversationStream(String agentId, String chatUid, String messageId, Map<String,Map<String,String>> answersByToolCall)Resume an interrupted conversation, returning aFlow.Publisherof run-progress events over SSE.CompletableFuture<ConversationResponse>conversation(String agentId, String query, String chatUid, String parentMessageId)Start a conversation with the specified Agent, blocking until the run succeeds, is interrupted, or fails.Flow.Publisher<ConversationStreamEvent>conversationStream(String agentId, String query, String chatUid, String parentMessageId)Start a conversation with the specified Agent, returning aFlow.Publisherof run-progress events over SSE.static AgentContextcreate(Config config)Create an AgentContext objectCompletableFuture<AgentsResponse>publicAgents(GetAgentsOptions opts)List all publicly available Agents on the platform (the Explore catalog).CompletableFuture<WorkspacesResponse>workspaces()List the Workspaces the current account belongs to.
-
-
-
Method Detail
-
create
public static AgentContext create(Config config)
Create an AgentContext object- Parameters:
config- Config object- Returns:
- A AgentContext object
-
close
public void close() throws Exception- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-
workspaces
public CompletableFuture<WorkspacesResponse> workspaces() throws OpenApiException
List the Workspaces the current account belongs to.- Returns:
- A Future representing the result of the operation
- Throws:
OpenApiException- If an error occurs
-
agents
public CompletableFuture<AgentsResponse> agents(String workspaceId, GetAgentsOptions opts) throws OpenApiException
List the Agents in the specified Workspace.- Parameters:
workspaceId- Workspace IDopts- Options for this request, may benull- Returns:
- A Future representing the result of the operation
- Throws:
OpenApiException- If an error occurs
-
publicAgents
public CompletableFuture<AgentsResponse> publicAgents(GetAgentsOptions opts) throws OpenApiException
List all publicly available Agents on the platform (the Explore catalog). Not scoped to a Workspace.- Parameters:
opts- Options for this request, may benull- Returns:
- A Future representing the result of the operation
- Throws:
OpenApiException- If an error occurs
-
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 UIDquery- User querychatUid- Conversation identifier to continue an existing chat, ornullto start a new oneparentMessageId-messageIdfrom 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 withchatUid, the parent message must belong to that conversation, and it must benullfor 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 UIDchatUid- Conversation identifiermessageId- ID of the paused message (seeInterrupt.getMessageId())answersByToolCall- Answers keyed bytoolCallId, each value being a map of question text to answer; may benullif 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 aFlow.Publisherof run-progress events over SSE. The run's outcome is carried by aWorkflowFinishedEvent(succeeded, failed, or stopped) or, if the Agent needs more input from you, aHumanInteractionRequiredEventinstead (unless the stream itself errors first, delivered viaFlow.Subscriber#onError) — an interrupted run never emits aWorkflowFinishedEvent. Neither is necessarily the last event delivered — the server may still emit a few more housekeeping events (e.g. aChatTitleUpdatedEvent) before actually closing the connection, so keep consuming untilonCompleterather than stopping as soon as you see one.This method itself performs no I/O — it returns a cold
Flow.Publisherimmediately; the HTTP/SSE connection is only established once a subscriber callssubscribe, matching Reactive Streams' lazy-publisher convention. The returned publisher carries real backpressure: no more events are pulled off the stream than have been requested viaFlow.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 UIDquery- User querychatUid- Conversation identifier to continue an existing chat, ornullto start a new oneparentMessageId-messageIdfrom a previous response, to attach a follow-up after that message. Only valid together withchatUidand must benullfor a new conversation- Returns:
- A cold
Flow.Publisherof conversation stream events
-
continueConversationStream
public Flow.Publisher<ConversationStreamEvent> continueConversationStream(String agentId, String chatUid, String messageId, Map<String,Map<String,String>> answersByToolCall)
Resume an interrupted conversation, returning aFlow.Publisherof run-progress events over SSE. SeeconversationStream(java.lang.String, java.lang.String, java.lang.String, java.lang.String)for the lazy-publisher/backpressure semantics.- Parameters:
agentId- Agent UIDchatUid- Conversation identifiermessageId- ID of the paused message (seeInterrupt.getMessageId())answersByToolCall- Answers keyed bytoolCallId, each value being a map of question text to answer; may benullif there is nothing to answer- Returns:
- A cold
Flow.Publisherof conversation stream events
-
-