fabric/graph/agent

An ordinary agent as a managed graph operation. The agent owns its transcript, tools and approvals. The graph owns its child attachment.

let assert Ok(agent) =
  agent.new("researcher", model, tools, policy)
  |> agent.with_answer(summary_codec)
  |> agent.build
let assert Ok(researcher) =
  graph_agent.new(
    run.DefinitionId("research", 1),
    agent,
    input: topic_codec,
    prompt: fn(topic) { "Research " <> topic.name },
  )
  |> graph_agent.runtime(runs, context: fn(_run) { ctx })
let node = definition.node(id, graph_agent.as_operation(researcher), ..)

The operation’s output is the agent’s answer: its type and codec are the agent’s (agent.with_answer; String for a plain agent).

The child agent run inherits its graph parent’s correlation and family root, so its events join the graph’s.

Types

pub type ConfigurationError {
  ChildIdsTooLong(maximum: Int, limit: Int)
}

Constructors

  • ChildIdsTooLong(maximum: Int, limit: Int)

An agent bound as a graph operation. Build one with new.

pub opaque type Definition(context, input, output)
pub type OpenError {
  DifferentStore
  Unreadable(fabric.Error)
  InvalidAttachment
}

Constructors

pub opaque type Runtime(context, input, output)

Values

pub fn as_operation(
  runtime: Runtime(context, input, output),
) -> @internal Operation(
  parent_context,
  input,
  output,
  operation.Kind,
  operation.Recovery,
  operation.Invocation,
  operation.Error,
)

Parent and child use the same supervised store. Context is rebuilt when starting or recovering the child, rather than stored in the graph record.

pub fn child(
  parent: @internal Handle(
    graph.Runtime(parent_context, state, answer),
  ),
  activation: Int,
  runtime: Runtime(context, input, output),
) -> Result(fabric.Run(context, output), OpenError)

Open a retained child, including a completed one. The store and reciprocal attachment must match; use the returned ordinary handle for all agent APIs.

pub fn new(
  identity: run.DefinitionId,
  agent: agent.Agent(context, output),
  input input: codec.Codec(input),
  prompt prompt: fn(input) -> String,
) -> Definition(context, input, output)

agent as the operation identity: the child run’s prompt is prompt(input), and its completed answer is the operation’s output, encoded with the agent’s answer codec. A stored answer that codec does not read is an invalid result the parent must reconcile. Version identity when the prompt, the answer’s meaning or the deployed agent changes. prompt must be pure: recovery and observation may call it again. Only the agent runner performs model and tool effects.

pub fn runtime(
  definition: Definition(context, input, output),
  runs: store.Store,
  context context: fn(run.RunId) -> context,
) -> Result(Runtime(context, input, output), ConfigurationError)

Binds definition to runs, the parent graph’s store. context builds the live context of the child agent run it is given, when the child starts or recovers; it is never stored. ChildIdsTooLong when the agent’s sub-agent limits would make child run ids longer than 128 characters.

✨ Search Document