npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@humanlayer/agentlayer-core

v0.0.81

Published

The core agent loop for AgentLayer. Wraps Vercel AI SDK's `streamText` in a resumable, approval-aware tool-execution loop, and defines the interfaces that platform packages (`agentlayer-filesystem`, `agentlayer-justbash`) implement. Ships isomorphic tools

Downloads

5,625

Readme

agentlayer-core

The core agent loop for AgentLayer. Wraps Vercel AI SDK's streamText in a resumable, approval-aware tool-execution loop, and defines the interfaces that platform packages (agentlayer-filesystem, agentlayer-justbash) implement. Ships isomorphic tools (subagent, skill, todo-write, web-fetch, structured-output), prompt fragments, and a hooks system for intercepting requests and tool calls.

Install

bun add @humanlayer/agentlayer-core

Subpath exports: @humanlayer/agentlayer-core/prompts, /tools, /hooks, /utils, /interfaces.

Usage

import { Agent, extractLastAssistantText, maxSteps, startState } from '@humanlayer/agentlayer-core'
import { createReadTool, createGlobTool, createGrepTool } from '@humanlayer/agentlayer-filesystem/tools'

const agent = new Agent({
  model: myLanguageModel, // an AI SDK LanguageModel
  tools: {
    read: createReadTool({ cwd: process.cwd() }),
    glob: createGlobTool({ cwd: process.cwd() }),
    grep: createGrepTool({ cwd: process.cwd() }),
  },
  system: ['You review AgentLayer documentation for source changes.'],
  stopWhen: [maxSteps(12)],
})

const result = await agent.run({
  state: startState([{ role: 'user', content: 'Summarize the diff.' }]),
}).result

console.log(extractLastAssistantText(result))

agent.run() returns an AgentRun, an AsyncIterable<AgentEvent> you can stream (text deltas, tool-input deltas, approvalRequested, tokenUsage, …) while also awaiting .result for the final RunResult (finishReason, newMessages, tokenUsage, updated state).

Key concepts

  • defineToolInterface / defineTool (src/define-tool.ts) — separates a tool's shape (name, description, Zod input/output) from its executor. Interfaces like ReadTool live in agentlayer-core; platform packages call ReadTool.define(executor) to supply the actual filesystem/sandbox logic. execute(input, ctx) receives a ToolContext with getContextWindow(), updateContextWindow(), signal, stop(), and (for stateful tools declaring stateKey/stateSchema) getToolState()/updateToolState().
  • AgentState (src/state.ts) — serializable resume token: messages, pendingToolCalls, approvalHistory, toolState, subAgents. Build one with startState(messages); apply approval/denial decisions with withApprovals(state, decisions).
  • Hooks (src/hooks/, exported via ./hooks) — four lifecycle points wired into AgentConfig.hooks: approval (next()/deny()/ask() before a tool runs), preToolUse (mutate input or short-circuit with a cached result), postToolUse (mutate a tool's output), preRequest (transform messages before they hit the model, e.g. truncation/deduplication). Built-in hooks include createApprovalHook, createPreToolUseHook, createPostToolUseHook, createPreRequestHook, plus ready-made ones like deduplicateReads, readTruncationHook, truncateOldBashResults, stripThinkingTokens.
  • Stop conditions (src/stop-conditions.ts) — maxSteps, doomLoop, consecutiveToolFailures, totalToolFailures, toolCalled, toolCompleted, structuredOutputCalled, passed as AgentConfig.stopWhen.
  • Interfaces (src/interfaces/, exported via ./interfaces) — tool shapes only, no execution: ReadTool, ReadMultimodalTool, WriteTool, EditTool, MultiEditTool, ApplyPatchTool, BashTool, GlobTool, GrepTool, ListTool, CodeSearchTool, ListCommentsTool, CreateCommentTool, UpdateCommentTool, CreateFileTool, DeleteFileTool, WebFetchTool, WebSearchTool, SkillTool.
  • Built-in tools (src/tools/, exported via ./tools) — fully implemented, platform-independent: createSubagentsTool, createSkillTool, TodoWriteTool, createWebFetchTool, createStructuredOutputTool.
  • Prompts (src/prompts/, exported via ./prompts) — createAgentSystemPrompt, per-provider system prompt builders (claudePrompt, codexPrompt, geminiPrompt, openaiPrompt), environmentPrompt, repoInstructionsPrompt, and tool-description text constants (READ_DESCRIPTION, BASH_DESCRIPTION, etc).

Tool call lifecycle

flowchart LR
    A["model emits tool call"] --> B{"approval hooks"}
    B -- deny --> R["tool-result: denied"]
    B -- ask --> P["pendingToolCalls\n(approvalRequired)"]
    B -- next --> C{"preToolUse hooks"}
    C -- stop --> S["ctx.stop() / hookStop"]
    C -- toolResult --> R
    C -- next --> D["tool.execute(input, ctx)"]
    D --> E{"postToolUse hooks"}
    E --> F["tool-result appended to state.messages"]

Agent.run() resumes cleanly from any RunResult.state: dangling tool calls from an interrupted run are re-detected on the next run() call and either auto-executed or re-parked, based on state.pendingToolCalls.

Tests

bun test (see test/) covers the loop against a mocked AI SDK model (test/mocks.ts), hooks, approvals, sub-agent pausing/resuming, stop conditions, token usage accounting, and tool interface .define() contracts.