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

@zhangzichao2008/agent-lib

v0.1.4

Published

A lightweight agent framework for the Anthropic API

Readme

@zhangzichao2008/agent-lib

一个轻量级 Node.js Agent 库,核心能力是对接 Anthropic Messages API(以及兼容 API),实现标准的 agent-loop:模型可自主发起工具调用(tool_use),库负责执行工具并把结果(tool_result)回填给模型,直到产出最终回答。

安装

npm i @zhangzichao2008/agent-lib

需要 Node.js >= 18。

环境变量

  • 必填:ANTHROPIC_AUTH_TOKENANTHROPIC_API_KEY
  • 可选:ANTHROPIC_BASE_URL(默认 https://api.anthropic.com
  • 可选:ANTHROPIC_MODEL(默认 claude-sonnet-4-6
  • 可选:AGENT_WORK_DIR(工作目录,文件/命令工具的沙箱基准目录)

基本使用

import { Agent, resolveConfig } from '@zhangzichao2008/agent-lib';

async function main() {
  const config = resolveConfig({
    apiKey: process.env.ANTHROPIC_AUTH_TOKEN || process.env.ANTHROPIC_API_KEY || '',
    baseUrl: process.env.ANTHROPIC_BASE_URL,
    model: process.env.ANTHROPIC_MODEL,
  });

  const agent = new Agent(config);

  for await (const event of agent.run('Please list the files in the current directory')) {
    if (event.type === 'message_delta') {
      process.stdout.write(event.data.text as string);
    } else if (event.type === 'complete') {
      console.log('\n\n[Complete]', event.data.final_content);
    } else if (event.type === 'error') {
      console.error('\n[Error]', event.data.message);
    }
  }
}

main().catch(console.error);

事件类型

agent.run() 返回一个 AsyncIterable<Event>,包含以下事件:

| 事件类型 | 说明 | |---------|------| | message_delta | 流式文本增量,event.data.text | | message_stop | 模型回复结束 | | tool_use | 模型发起工具调用,event.data.name / event.data.input | | tool_result | 工具执行完成,event.data.result | | complete | Agent 循环结束,event.data.final_content | | error | 发生错误,event.data.message |

内置工具

  • read_file — 读取文件
  • write_file — 写入/创建文件
  • update_file — 替换文件内容
  • bash — 执行 shell 命令
  • curl — HTTP 请求
  • subagent — 子 Agent 调用

API

resolveConfig(options?)

创建 AgentConfig 配置对象。支持从环境变量和参数中合并配置。

new Agent(config)

interface AgentConfig {
  apiKey: string;
  baseUrl?: string;
  model?: string;
  maxTokens?: number;
  tools?: Tool[];
  workDir?: string;
}

agent.run(prompt)

执行 agent-loop,返回 AsyncIterable<Event>

许可证

Proprietary