@reinsjs/brain 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/README.md +1 -1
- package/dist/index.d.ts +10 -8
- package/dist/index.js +20 -9
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# @reinsjs/brain
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- a3afbf9: `lazyTools()` builds its menu from every tool bound before it (`SocketSetup.tools`), not only the host's — so tools contributed by an earlier socket, such as `mcpTools({ override: (t, info) => info.annotations?.readOnlyHint ? { ...t, lazy: true } : false })`, are disclosed on demand too. Register `lazyTools()` after the sockets that feed it; `lazy` tools from sockets registered later are still never hidden, and the module now warns once naming them. The "already has a tool named tool_find" check likewise looks at everything bound before it. `lazyMenuOf()` keeps its signature (the parameter was renamed).
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [a3afbf9]
|
|
12
|
+
- @reinsjs/core@0.3.0
|
|
13
|
+
|
|
3
14
|
## 0.2.1
|
|
4
15
|
|
|
5
16
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -43,7 +43,7 @@ const agent = createAgent({
|
|
|
43
43
|
| `approval()` | A deny → ask → allow policy pipeline in `beforeTool`. `deny` leaves an `approval_decision` and blocks; `ask` pauses the run for a human; `allow` leaves no event. Default policy `byRisk`: tools without a declared `risk` are treated as medium and **ask** — mark read-only tools `risk: "low"`. Policy evaluation errors deny (fail-closed). `ttlMs` gives approvals a lifetime: one that arrives later than that (measured between the `approval_request` and the host's `approval_decision` on the timeline) is not honoured — the call is denied with `by: "approval.expired"` and a message telling the model it may re-issue the call for a fresh approval. | nothing |
|
|
44
44
|
| `budget()` | Five limits — `contextTokens`, `totalTokens`, `turns`, `toolCalls`, `wallMs` — checked in `onTurnEnd`. Hitting one while the model wants to continue pauses the run with `reason: "budget"`; resuming starts a fresh run budget. `totalTokens` includes what sub-agents spent through `asTool`. | nothing |
|
|
45
45
|
| `skills()` | [Agent Skills](https://agentskills.io) with progressive disclosure: the system prompt carries only a menu (each `SKILL.md`'s `name` + `description`); the model reads a skill, or one of its supporting files, with `skill_read({ name, path?, range? })`, and the text lands in the timeline as a `tool_result` — so compaction, `recall` and spill apply to it unchanged. Skills are host-authored, so `skill_read` results are trusted like the system prompt (no `<untrusted>` marker). Skills that fail the spec (bad `name`, missing `description`) are skipped with one warning; no `source`, no valid skill, or a host tool already named `skill_read` means nothing is registered (one warning). **Opt-in**: give it a `source`. | a `SkillSource` — the read-only half of any `MemoryStore` (see below), `fsSkillSource(dir)` from `@reinsjs/brain/node` for a folder of `<name>/SKILL.md`, or `inlineSkills({ "code-review": "---\nname: code-review\ndescription: How we review PRs.\n---\n\n1. …" })` for bundled strings (Workers) — the object key must equal the frontmatter `name` |
|
|
46
|
-
| `lazyTools()` | Tool discovery for large
|
|
46
|
+
| `lazyTools()` | Tool discovery for large tool tables — the consumer of `Tool.lazy`. Tools marked `lazy: true` appear in the system prompt only as a menu (`name: one-line summary`, first line of the description by default); the model loads the ones a task needs with `tool_find({ names })`, which returns each tool's full description and input schema, and from the next turn on those tools are in the request's tool list. Which tools are loaded is rebuilt from the timeline (the `tool_find` calls and their non-error results), so pause / resume, later runs on the same session and process restarts agree. Calling a listed-but-unloaded tool is blocked with a pointer to `tool_find`. The menu is built from the tools bound *before* this socket — the host's and those of sockets registered earlier, e.g. `mcpTools({ override: (t, info) => info.annotations?.readOnlyHint ? { ...t, lazy: true } : false })` — so register `lazyTools()` last; `lazy` tools from later sockets are never hidden, one warning names them. No `lazy` tool bound before it, or a tool already named `tool_find`: nothing registered, one warning. **Opt-in**: install it. | `summarize(tool)` to replace the default summary, `summaryChars` (160), `maxPerCall` (20), `rules`, `warn` |
|
|
47
47
|
|
|
48
48
|
### Skills from a database
|
|
49
49
|
|
package/dist/index.d.ts
CHANGED
|
@@ -552,7 +552,7 @@ declare function renderToolFindResult(input: {
|
|
|
552
552
|
* lazy-tools —— 工具懒发现(技术方案 §9.10,D1):大工具表的渐进式披露,`Tool.lazy` 的消费者。
|
|
553
553
|
*
|
|
554
554
|
* 库只做两件事(宪法一):
|
|
555
|
-
* - **让它看见**:系统提示里一份菜单(每件 `lazy: true`
|
|
555
|
+
* - **让它看见**:系统提示里一份菜单(每件 `lazy: true` 的工具的 name + 一行摘要),run 起步算一次、run 内不变、进 configHash。
|
|
556
556
|
* - **给它能力**:`tool_find({ names })` 取回几件工具的完整 description + inputSchema;从下一轮起那几件出现在请求的工具表里。
|
|
557
557
|
* 取哪几件、什么时候取是模型的判断;规则提示只给经验(rules.ts)。
|
|
558
558
|
*
|
|
@@ -568,18 +568,20 @@ declare function renderToolFindResult(input: {
|
|
|
568
568
|
* - **过滤路径**(其余降级层):本轮工具表 = 非 lazy + 已取回的 lazy;取回后的第一个请求缓存前缀重算一次(Anthropic 文档:工具表
|
|
569
569
|
* 变动使三段缓存全失效),所以规则文案要求一次把要用的都取回;实测数字见 `spikes/d1-lazy-tools-cache/`。
|
|
570
570
|
*
|
|
571
|
-
*
|
|
572
|
-
* 标了 lazy
|
|
573
|
-
*
|
|
571
|
+
* 作用范围是**注册在本模块之前已并入的工具**(`SocketSetup.tools`:宿主工具 + 排在前面的 Socket 贡献,2026-09-22 起):MCP 经
|
|
572
|
+
* `override` 标了 lazy 的工具也进菜单,前提是 `lazyTools()` 注册在 `mcpTools()` 之后——静态贡献按注册顺序依次解析,后面的看得见前面的,
|
|
573
|
+
* 反过来不行。注册在本模块之后的 Socket 贡献的 lazy 工具不进菜单也不藏(菜单上没有的工具若被藏起来就等于消失),`beforeModel`
|
|
574
|
+
* 见到这样的工具告警一次指出顺序。模块用对象同一性(WeakSet)记住"哪些工具是菜单工具",同一个 Socket 实例给多个 agent 定义共用也不会串。
|
|
574
575
|
*
|
|
575
576
|
* 模型没取回就直接调菜单里的工具:`beforeTool` 回 `block` 并指向 `tool_find`——不拦的话循环回"未知工具",模型会以为工具不存在;
|
|
576
577
|
* 也不放行,因为请求里没给 schema 的调用入参多半不对(原生路径同样拦:厂商知道这件工具、模型只凭菜单摘要就能报出名字)。
|
|
577
578
|
* 续跑补齐 pending 时没有 beforeModel、表是全表,隐藏工具照常执行。
|
|
578
579
|
*
|
|
579
580
|
* trust:工具说明与 schema 是宿主配置,视同系统提示可信,`tool_find` 声明 `resultTrust: "system"`(与 `skill_read` 同一条理由)。
|
|
581
|
+
* MCP 工具的说明来自服务器,但它本就直接进请求的 tools 块、与系统提示同一信任层级,经 `tool_find` 取回不另降级。
|
|
580
582
|
* `resultPolicy.maxTokens` 按"最大的 maxPerCall 件条目估算之和 + 256"在 setup 时算出:spill 永不把 schema 换成 untrusted 预览。
|
|
581
583
|
*
|
|
582
|
-
*
|
|
584
|
+
* 缺则不注册:此前并入的工具里没有一件 `lazy: true`、或已有同名 `tool_find` → 工具与菜单都不出现,告警一次(同 skills)。
|
|
583
585
|
* 不做:关键词搜索(菜单就是搜索空间,200 行约 4k token 且逐轮不变)、按 provider 原生 deferred-tools 下发(降级层的优化,另立任务)。
|
|
584
586
|
*/
|
|
585
587
|
|
|
@@ -614,10 +616,10 @@ interface LazyToolMenu {
|
|
|
614
616
|
byName: Map<string, Tool>;
|
|
615
617
|
}
|
|
616
618
|
/**
|
|
617
|
-
*
|
|
618
|
-
* 按 name
|
|
619
|
+
* 从一张工具表挑出 `lazy: true` 的做菜单。纯函数,菜单与测试共用;模块传的是 `SocketSetup.tools`(到本模块为止已并入的表)。
|
|
620
|
+
* 按 name 排序,与给的顺序无关——菜单进 configHash,顺序抖动会误判配置漂移。
|
|
619
621
|
*/
|
|
620
|
-
declare function lazyMenuOf(
|
|
622
|
+
declare function lazyMenuOf(tools: readonly Tool[], summarize?: (tool: Tool) => string): LazyToolMenu;
|
|
621
623
|
interface ToolFindInput {
|
|
622
624
|
/** 去重后的名字,保持模型给的顺序 */
|
|
623
625
|
names: string[];
|
package/dist/index.js
CHANGED
|
@@ -992,10 +992,10 @@ function summarizeTool(tool, maxChars = DEFAULT_LAZY_SUMMARY_CHARS) {
|
|
|
992
992
|
if (flat.length <= maxChars) return flat;
|
|
993
993
|
return `${flat.slice(0, Math.max(0, maxChars - 1)).trimEnd()}\u2026`;
|
|
994
994
|
}
|
|
995
|
-
function lazyMenuOf(
|
|
996
|
-
const
|
|
997
|
-
const entries =
|
|
998
|
-
return { tools, entries, byName: new Map(
|
|
995
|
+
function lazyMenuOf(tools, summarize = (t) => summarizeTool(t)) {
|
|
996
|
+
const picked = tools.filter((t) => t.lazy === true).sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
997
|
+
const entries = picked.map((t) => ({ name: t.name, summary: summarize(t).replace(/\s*\n\s*/g, " ") }));
|
|
998
|
+
return { tools: picked, entries, byName: new Map(picked.map((t) => [t.name, t])) };
|
|
999
999
|
}
|
|
1000
1000
|
function parseToolFindInput(raw, maxPerCall = DEFAULT_TOOL_FIND_MAX) {
|
|
1001
1001
|
if (typeof raw !== "object" || raw === null || !Array.isArray(raw.names)) {
|
|
@@ -1065,17 +1065,17 @@ function lazyTools(opts = {}) {
|
|
|
1065
1065
|
const resolveFor = (setup) => {
|
|
1066
1066
|
if (resolved.has(setup)) return resolved.get(setup);
|
|
1067
1067
|
let out;
|
|
1068
|
-
if (setup.
|
|
1068
|
+
if (setup.tools.some((t) => t.name === TOOL_FIND_TOOL_NAME)) {
|
|
1069
1069
|
warnOnce(
|
|
1070
1070
|
"host-tool",
|
|
1071
|
-
`[reins/lazy-tools] The
|
|
1071
|
+
`[reins/lazy-tools] The tool table already has a tool named ${TOOL_FIND_TOOL_NAME} (from the host or a socket registered before lazyTools()), so this module's tool and menu are not registered. Rename that tool to enable them.`
|
|
1072
1072
|
);
|
|
1073
1073
|
} else {
|
|
1074
|
-
const menu = lazyMenuOf(setup.
|
|
1074
|
+
const menu = lazyMenuOf(setup.tools, summarize);
|
|
1075
1075
|
if (menu.tools.length === 0) {
|
|
1076
1076
|
warnOnce(
|
|
1077
1077
|
"no-lazy",
|
|
1078
|
-
"[reins/lazy-tools] No
|
|
1078
|
+
"[reins/lazy-tools] No tool bound before lazyTools() is marked lazy: true, so tool_find and the menu are not registered. Mark the tools you want disclosed on demand with lazy: true, and register lazyTools() after the sockets (such as mcpTools()) that contribute them."
|
|
1079
1079
|
);
|
|
1080
1080
|
} else {
|
|
1081
1081
|
for (const t of menu.tools) menuTools.add(t);
|
|
@@ -1094,7 +1094,18 @@ function lazyTools(opts = {}) {
|
|
|
1094
1094
|
},
|
|
1095
1095
|
beforeModel(ctx) {
|
|
1096
1096
|
const menuNames = /* @__PURE__ */ new Set();
|
|
1097
|
-
|
|
1097
|
+
const late = [];
|
|
1098
|
+
for (const t of ctx.tools) {
|
|
1099
|
+
if (t.lazy !== true) continue;
|
|
1100
|
+
if (menuTools.has(t)) menuNames.add(t.name);
|
|
1101
|
+
else late.push(t.name);
|
|
1102
|
+
}
|
|
1103
|
+
if (late.length > 0) {
|
|
1104
|
+
warnOnce(
|
|
1105
|
+
"late",
|
|
1106
|
+
`[reins/lazy-tools] ${late.length} tool(s) marked lazy: true are not on the menu because their socket is registered after lazyTools(): ${late.sort().join(", ")}. They are sent in full every turn. Register lazyTools() after the sockets that contribute them.`
|
|
1107
|
+
);
|
|
1108
|
+
}
|
|
1098
1109
|
if (menuNames.size === 0) return void 0;
|
|
1099
1110
|
const revealed = revealedLazyTools(ctx.timeline, menuNames);
|
|
1100
1111
|
const hidden = /* @__PURE__ */ new Map();
|