Asking

org.llm4s.agent.graph.middleware.AgentMiddleware.Asking
abstract class Asking[Q, Ans] extends AgentMiddleware

A middleware that asks typed questions of type Q and takes answers of type Ans, as org.llm4s.agent.graph.tool.AgentTool.Asking does for a tool: it suspends the run for a reviewer - to approve or edit an answer, or to supply missing information - instead of only blocking or failing.

A hook asks by returning ask - from beforeAgent, afterAgent or wrapModelCall - or askAbout from wrapToolCall. The run suspends at the end of the superstep with a question of its own, keyed by the asking task's interrupt id like any other, so several pending questions are answered together or one at a time. The answer is decoded as Ans when the run is resumed: one that does not decode is refused, the thread unchanged.

Once answered, the hook's whole stack runs again from the outermost middleware - nothing about a stack's position is checkpointed, as for approvals - and answered gives this middleware its question and answer, so it continues instead of asking again. Answers given earlier in the same stack run are kept while it runs again, so two asking middleware in one stack each ask once. A middleware outside the asking one therefore runs twice, and must not depend on running once.

What runs again is the asking task: beforeAgent re-runs the turn's input (nothing of the turn is stored while it waits), afterAgent the final answer, wrapToolCall the tool call (its arguments checked again), and wrapModelCall the model call - so a model wrapper that asks after calling next calls the model again once answered, unless it returns a completion of its own (the calls made before asking are counted in the thread's usage, and announced, while the question waits). A question from a tool wrapper while the tool continues after its own question is refused, as the call's error result.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes

Members list

Value members

Concrete methods

final protected def answered(context: RunContext): Option[Resumed[Q, Ans]]

This middleware's question and its answer, when its hook runs again after a resume; None before.

This middleware's question and its answer, when its hook runs again after a resume; None before.

Attributes

final protected def answered(context: ToolContext): Option[Resumed[Q, Ans]]

answered for a tool call's context.

answered for a tool call's context.

Attributes

final protected def ask(question: Q): Result[Nothing]

Suspends the run with question; return it as the result of beforeAgent, afterAgent or wrapModelCall. A question that does not encode is that Left instead.

Suspends the run with question; return it as the result of beforeAgent, afterAgent or wrapModelCall. A question that does not encode is that Left instead.

Attributes

final protected def askAbout(question: Q): ToolOutcome

Suspends the tool call with question; return it as the result of wrapToolCall.

Suspends the tool call with question; return it as the result of wrapToolCall.

Attributes

Inherited methods

def afterAgent(answer: String, context: RunContext): Result[String]

Sees the run's final answer: returns it, possibly changed, or Left to fail the run.

Sees the run's final answer: returns it, possibly changed, or Left to fail the run.

Attributes

Inherited from:
AgentMiddleware
def beforeAgent(input: String, context: RunContext): Result[String]

Sees the run's input before anything else runs: returns it, possibly changed, or Left to fail the run.

Sees the run's input before anything else runs: returns it, possibly changed, or Left to fail the run.

Attributes

Inherited from:
AgentMiddleware

Middleware this one must run inside of (be wrapped by); each must be registered in the same stack.

Middleware this one must run inside of (be wrapped by); each must be registered in the same stack.

Attributes

Inherited from:
AgentMiddleware

Middleware this one must run outside of (wrap); each must be registered in the same stack.

Middleware this one must run outside of (wrap); each must be registered in the same stack.

Attributes

Inherited from:
AgentMiddleware
def tools: Vector[AgentTool[_]]

Tools this middleware contributes; they join the loop's tool set and are validated like any other.

Tools this middleware contributes; they join the loop's tool set and are validated like any other.

Attributes

Inherited from:
AgentMiddleware

Wraps one model call. A wrapper may rewrite the request (inject a note, filter tools), call next more than once (retry, fallback), transform its result, or return Left, which fails the run. Only an AgentMiddleware.Asking wrapper suspends, by asking its question.

Wraps one model call. A wrapper may rewrite the request (inject a note, filter tools), call next more than once (retry, fallback), transform its result, or return Left, which fails the run. Only an AgentMiddleware.Asking wrapper suspends, by asking its question.

Filtering ModelRequest.tools shapes what the model is offered and is not a permission control: a tool the model calls anyway still runs through wrapToolCall, where denial belongs.

Attributes

Inherited from:
AgentMiddleware
def wrapToolCall(request: ToolCallRequest, context: ToolContext)(next: () => ToolOutcome): ToolOutcome

Wraps one tool call, after its arguments were validated. A wrapper denies with ToolOutcome.Error("Denied: ..."), asks for approval with NeedsApproval(reason) (unless context.approved), fails the run with Fatal, and short-circuits by not calling next. It may call next more than once (retry) and transform the outcome next returns.

Wraps one tool call, after its arguments were validated. A wrapper denies with ToolOutcome.Error("Denied: ..."), asks for approval with NeedsApproval(reason) (unless context.approved), fails the run with Fatal, and short-circuits by not calling next. It may call next more than once (retry) and transform the outcome next returns.

Attributes

Inherited from:
AgentMiddleware
def writes: Set[StateKey[_, _]]

The state keys this middleware's wrapToolCall may add to a Success update.

The state keys this middleware's wrapToolCall may add to a Success update.

Attributes

Inherited from:
AgentMiddleware

Inherited and Abstract methods

This middleware's identifier, unique in its stack; must match [a-zA-Z0-9_-]{1,64}.

This middleware's identifier, unique in its stack; must match [a-zA-Z0-9_-]{1,64}.

Attributes

Inherited from:
AgentMiddleware