@kernel4632/agent-core weekly downloads statistics
By kernel4632 · npm · AI SDKs
轻量的 AI Agent 核心:给它模型地址和工具对象(工具文件可用 Agent.Tool.load 读成对象、在子进程里跑),自动循环「问模型 → 执行工具 → 再问模型」。支持 OpenAI Chat / Responses、Anthropic、Gemini 四种协议,上下文压缩保留、提示词缓存,纯对话模型也能用工具。
@kernel4632/agent-core has 587 recorded weekly downloads on npm, an increase of 8.1% over the past 30 days, ranking #2218 of 3,247 tracked listings.
About @kernel4632/agent-core
@kernel4632/agent-core
一个轻量的 AI Agent 核心包(TypeScript,运行在 Bun 上)。给它一个模型和一组工具对象,
它就自动循环"问模型 → 执行工具 → 再问模型",直到任务完成。工具可以直接在内存里写,也可以用 Agent.Tool.load 从文件读(在子进程里跑)。
目录
这个包是干什么的
flowchart TD
app["你的程序"] --> create["Agent.create()"]
create --> send["agent.send()"]
send --> loop["loop:自动循环"]
loop --> llm["model:问模型"]
loop --> tool["tools:执行工具对象的 run"]
tool --> loop
模型每次回复要么"调用某个工具",要么"我做完了"。这个包负责把循环跑起来;你只需要写工具,把工具对象交给它。
核心设计
① Agent 是唯一的状态持有者。 宿主能改的东西都是实例上的公开字段:config、history、tools、
contextRatio、ratios(以及 id)。直接改,下一轮请求就生效,不需要调任何"通知"方法。
内部状态(取消器、当前运行、警告去重)是私有字段,模块里没有任何全局可变状态,多个实例完全隔离。
② 核心只认工具对象。 tools 是工具对象数组:{ name, description, parameters, run(args, { signal, toolCallId, onOutput }), timeout?, toModelOutput? }。
工具从哪来核心一概不管:内存里直接写、Agent.Tool.load 读文件、宿主自己把 MCP 服务器的工具包成对象,都是一样的对象。
每一轮请求前现读每个对象的 name / description / parameters,所以对象变了、数组增删了,下一轮就生效。
插件、技能、开关、MCP 连接这些概念属于宿主,核心不知道它们是什么。
③ stop() 立即生效,不等工具。 核心给每次 run 一个 AbortSignal,并让 run 和 abort 赛跑:stop() 一到就把调用记成"工具执行已取消"、循环立刻收尾,
工具晚到的结果直接丢掉。愿意配合的工具自己听 signal;Agent.Tool.load 读出来的文件工具跑在子进程里,abort 时直接被 SIGKILL,
工具死循环、崩溃都不会拖垮 Agent。
④ history 只增不删,只有 loop 写它。 模型消息、工具结果按发生顺序追加;压缩也只追加一条 role: 'compact' 的总结。
每个出口都补一条模型能读的结果(工具失败、调用无效、被取消都会写一条结果),绝不留下"有调用、没结果"的残缺记录。
问模型出错(每次重试前、最终失败、上下文超长,压缩请求也一样)追加一条 role: 'error':content 是给人看的错误描述(分类、状态码、服务端原话;厂商格式解析不了就取原始响应体里的 message),
request 存 { kind, statusCode, model, protocol, attempt | final, compact? };和上一条是同一个错误(内容相同)就不重复记,用户 stop() 不记。
模型回复写进 history 前去掉 AI SDK 的 custom 元数据块(发请求用不上),思考块、签名和各块的 providerOptions 原样保留。
一次模型请求产出一条 assistant(纯调工具也是),上面挂着这次请求的记录;压缩请求的记录挂在它写的 compact 上,形状相同:
request: {
model: string, // 模型名(核心只有模型,没有供应商)
start: number, // 请求发出的毫秒时间戳
first?: number, // 首字耗时(毫秒):start 到第一个正文 / 思考 / 工具调用片段;只有流式有
duration: number, // 完整耗时(毫秒)
usage?: { input?, output?, cacheRead?, cacheWrite? } // 只放供应商真报了的;一个都没报就没有 usage,不补 0
}
计时只算成功的那次尝试(失败的尝试各记一条 error)。这些字段只存在 history 里,发给模型时和 id 一起去掉。
⑤ 停不停由模型和工具决定,没有"最大轮数"。 模型不调工具时,先临时挂一句提醒再问(不进 history),
连续 loop.idleRounds 轮(默认 3)都不调才结束;工具返回 stop: true 立即结束。提醒文字由 loop.idleReminder 决定,只挂在下一次请求上。
安装
需要 Bun ≥ 1.4.4(不支持 Node.js)。
bun add @kernel4632/agent-core
# 或从 GitHub Release 安装最新版
bun add https://github.com/kernel4632/agent-core/releases/latest/download/agent-core.tgz
发布包是单文件 dist/agent-core.js(依赖全部打进去,工具子进程入口也内联在里面)加 dist/types/ 类型声明。
5 分钟快速上手
第一步,写一个工具。它就是一个普通对象:
const sayHello = {
name: 'say_hello',
description: '向指定的人打个招呼',
parameters: { type: 'object', properties: { who: { type: 'string' } }, required: ['who'] },
run({ who }: { who: string }) {
return `你好,${who}!`
},
}
第二步,创建 Agent 并发送指令:
import { Agent } from '@kernel4632/agent-core'
const agent = Agent.create({
baseURL: 'https://api.openai.com/v1',
apiKey: process.env.OPENAI_API_KEY,
model: 'gpt-4o',
system: '你是一个助手。',
tools: [sayHello, ...await Agent.Tool.load(['/abs/path/to/tools'])], // 内存工具和文件工具可以混用
})
const { reason, text, usage } = await agent.send('请向世界打个招呼', {
onEvent: event => { if (event.type === 'text') process.stdout.write(event.text) },
})
离线可跑的完整示例见 examples/minimal:bun examples/minimal/main.ts。
工具
{
name: string, // 工具名(同一轮里重名的,后出现的被跳过并警告)
description: string,
parameters: JSONSchema, // 只收 JSON Schema,顶层必须是 type: 'object'
run(args, { signal, toolCallId, onOutput }) { ... }, // 可以是 async
toModelOutput?({ output, input, toolCallId }) { ... }, // 可选:把结果改写成给模型看的输出块(与 AI SDK 同签名)
timeout?: number, // 可选:单次执行超时(毫秒)
}
- 每轮现读、每轮校验。
name/description/parameters可以是 getter;不合格的对象(缺字段、getter 抛错)被跳过,发一条warning(同一条只发一次)。 - 返回值: 字符串或可 JSON 化的值都行;
{ stop: true, ... }让循环结束(reason 为tool-stop)。默认不截断;设了maxToolOutput时, 超长的文字(含content里的文字块)保留开头 70%、结尾 30%,中间注明省略了多少字符并提示模型缩小范围重新获取。 返回成形块({ type: 'text' | 'json' | 'content' | ... })但形状不对时,模型收到一条说明哪里不对的错误结果。 - 进度:
onOutput(stream, data)实时变成tool-output事件转给宿主。 - 出错就是结果:
run抛错、超时,都会变成一条错误文字结果交给模型(正文就是错误信息),循环继续。 失败时把工具已经通过onOutput产出的内容(头尾各保留一段)放在原因前面,模型能看到它跑到了哪一步。 - 停止和超时: 都会 abort
signal,核心不等工具自己停下,立刻写结果("工具执行已取消" / "工具执行超时")。 没接signal的内存工具会在后台继续跑完(结果和之后的输出都丢掉)——如果它会改文件,stop()之后文件可能还在变,所以长任务请接signal。 同步死循环的内存工具会卡住整个进程,这类工具请用文件写、经Agent.Tool.load放进子进程。 - 调用本身无效(参数解析失败、工具不存在):不执行,
deny也不生效,直接写一条"工具调用无效:原因"结果让模型自己改。 - 工具和 Agent 权限相同(文件、网络、环境变量)。危险操作请在
tool-call事件上调用e.deny(原因?)拦住。
工具文件:Agent.Tool.load
Agent.Tool.load(paths) 把工具文件 / 目录读成工具对象。它挂在 Agent 上,和 Agent.LLM.ask 一样独立于任何实例;核心循环不认识文件路径。
const local = await Agent.Tool.load(['/plugins/a/', '/plugins/b/tool.ts'])
agent.tools = [...local, weather]
agent.tools = agent.tools.filter(tool => !('path' in tool && String(tool.path).startsWith('/plugins/a/'))) // 关掉插件 a
工具文件的默认导出就是上面的工具对象,零依赖,不需要 import 任何东西:
export default {
name: 'say_hello',
description: '向指定的人打个招呼',
parameters: { type: 'object', properties: { who: { type: 'string' } }, required: ['who'] },
run({ who }) { return `你好,${who}!` }, // 也可以是 async generator:每次 yield 实时作为 tool-output 转给宿主
}
```…
Description from the npm listing.
- agent
- ai
- llm
- tool-calling
- bun
- openai
- anthropic
- gemini
- ai-sdk
README badge
Show your rank in a README or on your website. The badge updates automatically.
Charts
Images: 30d · 1y · all time · 1600×900 dark
Adoption over time
7/30/90-day growth
Ranking history
Listing placement
Listing placement
Filled points are observed values; hollow points come from Internet Archive snapshots of the listing page. Dashed segments span periods with no observations — values between points are not interpolated.
Each band is one page of 20. Weekly downloads on a linear scale. Placement is projected from weekly downloads, so it can differ from the marketplace's own sort order. Hover a dot to see the listing.
Recent observations
Full history (128)Similar npm AI SDKs plugins
All 20 @kernel4632/agent-core alternativesData source
Weekly downloads (trailing 7 complete days) come from the official npm downloads API. Downloads include CI and mirrors and are not equivalent to installs or users. npm publishes no ratings.
Listing: https://www.npmjs.com/package/@kernel4632/agent-core. First seen Nov 26, 2026; last successful collection Dec 6, 2026. See the methodology.