AG2B

useAg2bScope

Register a Scope on the surrounding agent for the component's lifetime.

function useAg2bScope(config: ScopeConfig, deps?: unknown[]): void

Reactivity rules

Always-freshenabled, context. The hook re-reads the latest closure on every loop iteration, so inline arrows can close over reactive state without re-registering the scope.

Captured at mountname, label, injection, tools. Tools coming from useAg2bTool are reference-tracked, so changes there cascade automatically. Pass deps only for state the config reads that doesn't fit either path.

Usage

Registering a scope

Pass a ScopeConfig. The Scope is created on mount and unregistered on unmount:

function PullRequestScope() {
  const pr = usePullRequest();

  const approve = useAg2bTool({
    name: 'approvePR',
    description: 'Approve the current pull request.',
    parameters: z.object({ comment: z.string().optional() }),
    handler: ({ comment }) => pr.approve(comment),
  });

  useAg2bScope({ name: 'pullRequest', tools: [approve] });
  return null;
}

Context resolver

context runs every loop iteration and feeds dynamic state into the outgoing LLM request. Always-fresh — inline arrows read the latest snapshot:

useAg2bScope({
  name: 'pullRequest',
  label: 'Pull request',
  tools: [approve],
  context: () => ({
    title: pr.title,
    files: pr.files.length,
    status: pr.status,
  }),
});

Conditional availability

enabled gates the whole scope — tools and context both disappear when it returns false. Always-fresh, same as context:

useAg2bScope({
  name: 'pullRequest',
  tools: [approve],
  enabled: () => pr.status === 'open',
});

For per-tool gating instead of whole-scope gating, put the gate on the tool itself — see useAg2bTool.

Tool deps cascade

Tools created via useAg2bTool are tracked by reference. When a Tool re-creates (e.g., its Zod schema reads a reactive value via the deps arg), the Scope picks up the new reference automatically — no need to list tools in the Scope's deps:

const comment = useAg2bTool({ ... }, [maxLen]); // Tool re-creates on maxLen change
useAg2bScope({ name: 'pullRequest', tools: [comment] }); // Scope auto-cascades

Pass deps to useAg2bScope only to force a re-register from external state the config doesn't read.

On this page