Java-friendly wrapper around Agent.
Exposes run(query) and continueConversation(previous, query), returning LlmResult[JAgentResult] so Java callers do not need to deal with Scala's Either or Result types directly, nor read a Scala value: a JAgentResult's accessors return Strings, JDK types and this package's own types. A conversation is a thread of the agent's in-memory runtime, kept until forget removes it.
run, continueConversation, resume and recover block the calling thread and never throw. Interrupting that thread cancels the turn: the call returns a failed result whose error is a CancelledError, with the interrupt flag still set, once the turn has ended (it waits up to 5 seconds for the turn to end, for a provider that ignores its interrupt), and the conversation's thread is left for recover. A thread already interrupted starts no turn. InterruptedException is never thrown, so the methods declare no checked exception and javac rejects a catch (InterruptedException e) around them - test the result for a CancelledError instead. To cancel a turn without interrupting a thread, stream it and call AgentStream.cancel.
A turn whose tools need approval, whose tools or middleware ask a question, or whose task a static breakpoint holds, ends SUSPENDED: its status's pending() - or JAgent.pending - lists what it waits for, and resume(threadId, answers) answers some or all of it and continues. A turn that failed or was cancelled continues with recover(threadId). Both block, as run does.
LlmResult<JAgentResult> turn = agent.run("Deploy the release");
List<Answer> answers = new ArrayList<>();
for (PendingInterrupt p : turn.get().status().pending()) answers.add(Answer.approve(p.id()));
if (!answers.isEmpty()) turn = agent.resume(turn.get().threadId(), answers);
stream, streamResume and streamRecover run a turn on a thread you name and hand its events to an AgentStreamListener as they happen, returning an AgentStream at once - to await the turn's result, or to cancel it. From Java a thread id is a String.
runAsync, continueConversationAsync, resumeAsync and recoverAsync return a CompletableFuture at once and run the turn on another thread - a new virtual thread per turn, or the executor withAsyncExecutor names. The future completes with the JAgentResult, or exceptionally with the LlmException the blocking call's result would hold. cancel(true) interrupts the turn, which then ends as the blocking call interrupted does: cancelled, the thread of a continued, resumed or recovered conversation left for recover.
Obtain instances via Llm4s.createAgent.
JAgent agent = Llm4s.createAgent(client);
LlmResult<JAgentResult> result = agent.run("Summarise the news today");
result.ifSuccess(r -> System.out.println(r.answer().orElse("(" + r.status().kind() + ")")))
.ifFailure(e -> System.err.println(e.getMessage()));
Attributes
- Companion
- object
- Source
- JAgent.scala
- Graph
-
- Supertypes
