@easbot/llm 0.3.36 → 0.3.39
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/README.en.md +45 -3
- package/README.md +44 -3
- package/dist/chunks/{index.min-XTHFSCRB.cjs → index.min-5MMWVKPD.cjs} +1 -1
- package/dist/chunks/{index.min-PO6HDKHB.mjs → index.min-DHJMPYRN.mjs} +1 -1
- package/dist/index.cjs +120 -120
- package/dist/index.d.cts +26 -10
- package/dist/index.d.ts +26 -10
- package/dist/index.mjs +120 -120
- package/package.json +8 -7
package/README.en.md
CHANGED
|
@@ -6,16 +6,17 @@ English | [中文](./README.md)
|
|
|
6
6
|
|
|
7
7
|
## Version
|
|
8
8
|
|
|
9
|
-
v0.3.
|
|
9
|
+
v0.3.39
|
|
10
10
|
## Current Status (after V4 refactor)
|
|
11
11
|
|
|
12
12
|
This package has evolved from an early placeholder of `@easbot/types` into a production-grade AI gateway layer that hosts two core modules: `provider/*` and `model/*`:
|
|
13
13
|
|
|
14
|
-
- ✅ **`src/provider/`** (
|
|
14
|
+
- ✅ **`src/provider/`** (8 core files + self-developed Copilot SDK): complete industrial-grade provider abstraction
|
|
15
15
|
- `Provider` namespace — 25+ built-in SDK (OpenAI / Anthropic / Azure / Bedrock / Vertex / OpenRouter / Copilot / Ollama / local models, etc.) dynamic loaders, `ModelsDev` catalog merging, model caching, SDK instance pooling
|
|
16
16
|
- `ProviderError` namespace — APICallError parsing (including 25+ "context overflow" regex patterns)
|
|
17
17
|
- `ProviderAuth` namespace — OAuth + API key dual-track authentication
|
|
18
|
-
- `ProviderTransform` namespace — model variant / tool / limit handling
|
|
18
|
+
- `ProviderTransform` namespace — model variant / tool / limit handling; `schema()` delegates to the unified normalization in `tool-schema.ts`
|
|
19
|
+
- `tool-schema.ts` — **tool parameter schema normalization** (greatest common denominator across vendor function calling): inlines `$ref`, flattens unions, strips non-standard keywords, stringifies Gemini enums, and de-coerces at runtime
|
|
19
20
|
- `ModelsDev` namespace — models.dev catalog zod schema + loading + remote refresh
|
|
20
21
|
- `definition.ts` — DEFAULT_ENABLED_PROVIDERS whitelist + CUSTOM_PROVIDER_DEFINITIONS local SDK snapshot + PROVIDER_DISPLAY_NAMES friendly name mapping
|
|
21
22
|
- `sdk/copilot/` — self-developed GitHub Copilot-compatible SDK (chat + responses + 6 tools)
|
|
@@ -149,6 +150,47 @@ Once the holder falls back to a global bucket, the **bucketing semantics split**
|
|
|
149
150
|
> The agent-side `packages/agent/src/tool/registry.ts` is a **pure forwarder** (holds no state);
|
|
150
151
|
> it overlays the agent's own tools onto the llm registry's `builtin` layer via `registerBuiltin()`.
|
|
151
152
|
|
|
153
|
+
## Tool Parameter Schema Normalization (greatest common denominator across vendors)
|
|
154
|
+
|
|
155
|
+
`src/provider/tool-schema.ts` is the **single implementation** of tool parameter schema normalization —
|
|
156
|
+
`ProviderTransform.schema()` is merely a delegating entry point.
|
|
157
|
+
|
|
158
|
+
### Why this layer is needed
|
|
159
|
+
|
|
160
|
+
Among the provider SDKs we integrate, **every one except Google forwards tool schemas verbatim**
|
|
161
|
+
(anthropic → `input_schema`; bedrock → `inputSchema: { json: … }`; openai / openai-compatible / deepseek / mistral / groq / xai → `parameters`).
|
|
162
|
+
In other words: **the schema we emit is the schema the vendor receives** — normalization is entirely our responsibility.
|
|
163
|
+
|
|
164
|
+
Vendor constraints disagree, so we must target their intersection:
|
|
165
|
+
|
|
166
|
+
| Vendor | Rejected / restricted constructs |
|
|
167
|
+
| ----------------- | ------------------------------------------------------------------------ |
|
|
168
|
+
| Anthropic | `$ref` and any reference keyword; top-level `oneOf` / `anyOf` / `allOf` |
|
|
169
|
+
| Bedrock | top-level unions; missing top-level `type` |
|
|
170
|
+
| Zhipu GLM | unions or references at **any depth** → HTTP 500 |
|
|
171
|
+
| Gemini | no `oneOf` (400); function-calling `enum` must be all strings |
|
|
172
|
+
| OpenAI-compatible | verbatim — but OpenRouter may route to Gemini / Bedrock → strictest wins |
|
|
173
|
+
|
|
174
|
+
### Normalization pipeline
|
|
175
|
+
|
|
176
|
+
| Stage | Action |
|
|
177
|
+
| ----- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
178
|
+
| P1 | Clone the input; **never mutate caller-owned objects** (an MCP tool's schema is the very reference held by the client cache) |
|
|
179
|
+
| P2 | Inline `$ref` (`#/$defs/<name>` / `#/definitions/<name>`); strip non-standard keywords (`$defs` / `$schema` / `$id` / `ref`); self-references degrade at an 8-level depth cap |
|
|
180
|
+
| P3 | Recursively eliminate unions: null branch → `nullable`; all-object branches → merge; same-typed literals → merge into `enum`; otherwise take the first branch |
|
|
181
|
+
| P4 | Single-valued `type` (`["string","null"]` → `type` + `nullable`), recurse into `properties` / `items`, align `required` with `properties`, fill missing `items.type` |
|
|
182
|
+
| P5 | Gemini dialect: stringify every `enum` value (and register a coercion) |
|
|
183
|
+
| P6 | Runtime de-coercion: restore model-returned `"2"` → `2` and `"true"` → `true` before the tool executes |
|
|
184
|
+
|
|
185
|
+
### The single mandatory path
|
|
186
|
+
|
|
187
|
+
The **model entry** (`resolveCall` in `src/model/language/language.ts`) calls `adaptTools()` for the whole tool set:
|
|
188
|
+
|
|
189
|
+
- The application layer performs **no schema conversion at all** — it hands the zod schema straight to AI SDK's `tool()`;
|
|
190
|
+
- Doing vendor dialect adaptation in the application layer inevitably misses call sites (every new host — note / memory / plugins — must remember to call it again) and drifts from the llm layer.
|
|
191
|
+
|
|
192
|
+
> Tests: `src/provider/__tests__/tool-schema.test.ts` (the algorithm) and `src/model/language/__tests__/tool-schema.test.ts` (the model entry really applies it).
|
|
193
|
+
|
|
152
194
|
## Generic Tool Permission Model (judge-only, no prompting)
|
|
153
195
|
|
|
154
196
|
`src/tool/buildin/` is the static built-in baseline of 9 generic tools (read / write / edit / grep / glob / bash / webfetch / websearch / todo).
|
package/README.md
CHANGED
|
@@ -6,16 +6,17 @@
|
|
|
6
6
|
|
|
7
7
|
## 版本
|
|
8
8
|
|
|
9
|
-
v0.3.
|
|
9
|
+
v0.3.39
|
|
10
10
|
## 当前状态(V4 重构后)
|
|
11
11
|
|
|
12
12
|
本包已从 `@easbot/types` 的早期占位实现演化为承载 _*provider/* + model/_ 两套核心模块** 的生产级 AI 网关层:
|
|
13
13
|
|
|
14
|
-
- ✅ **`src/provider/`**(
|
|
14
|
+
- ✅ **`src/provider/`**(8 文件):完整的工业级 provider 抽象
|
|
15
15
|
- `Provider` 命名空间 —— 25+ 内置 SDK(OpenAI / Anthropic / Azure / Bedrock / Vertex / OpenRouter / Copilot / Ollama / 本地模型等)的动态加载器、`ModelsDev` catalog 合并、模型缓存、SDK 实例池化
|
|
16
16
|
- `ProviderError` 命名空间 —— APICallError 解析(含 25+ 种"上下文溢出"模式正则识别)
|
|
17
17
|
- `ProviderAuth` 命名空间 —— OAuth + API key 双轨认证
|
|
18
|
-
- `ProviderTransform` 命名空间 —— 模型 variant / tool /
|
|
18
|
+
- `ProviderTransform` 命名空间 —— 模型 variant / tool / 限制处理;`schema()` 委托 `tool-schema.ts` 的统一归一
|
|
19
|
+
- `tool-schema.ts` —— **工具参数 schema 归一**(跨厂商 function calling 的最大公约数):`$ref` 内联、union 拍平、非标关键字剥离、Gemini enum 字符串化 + 运行期 coercion 反解
|
|
19
20
|
- `ModelsDev` 命名空间 —— models.dev catalog 的 zod schema + 加载 + 远程刷新
|
|
20
21
|
- `definition.ts` —— DEFAULT_ENABLED_PROVIDERS 白名单 + CUSTOM_PROVIDER_DEFINITIONS 本地 SDK 快照 + PROVIDER_DISPLAY_NAMES 友好名映射
|
|
21
22
|
- `sdk/copilot/` —— 自研 GitHub Copilot 兼容 SDK(chat + responses + 6 个 tool)
|
|
@@ -151,6 +152,46 @@ tool 相关的权限判定与权限等待**强依赖 `Instance.directory`**:
|
|
|
151
152
|
> agent 侧 `packages/agent/src/tool/registry.ts` 是**纯转发**(不持状态),
|
|
152
153
|
> 通过 `registerBuiltin()` 把 agent 版工具叠加到 llm registry 的 `builtin` 层。
|
|
153
154
|
|
|
155
|
+
## 工具参数 schema 归一(跨厂商 function calling 的最大公约数)
|
|
156
|
+
|
|
157
|
+
`src/provider/tool-schema.ts` 是**工具参数 schema 归一的唯一实现** —— `ProviderTransform.schema()` 只是它的委托入口。
|
|
158
|
+
|
|
159
|
+
### 为什么需要这一层
|
|
160
|
+
|
|
161
|
+
本仓对接的 provider SDK 中,**除 Google 外全部把 tool schema 原样透传**
|
|
162
|
+
(anthropic → `input_schema`;bedrock → `inputSchema: { json: … }`;openai / openai-compatible / deepseek / mistral / groq / xai 等 → `parameters`)。
|
|
163
|
+
即:**我们下发的 schema 就是厂商收到的 schema**,归一责任完全在本仓。
|
|
164
|
+
|
|
165
|
+
各厂商硬约束彼此不一致,只能取交集:
|
|
166
|
+
|
|
167
|
+
| 厂商 | 拒绝 / 限制的构造 |
|
|
168
|
+
| ------------- | --------------------------------------------------------------------- |
|
|
169
|
+
| Anthropic | `$ref` 等引用关键字;顶层 `oneOf` / `anyOf` / `allOf` |
|
|
170
|
+
| Bedrock | 顶层 union;顶层缺 `type` |
|
|
171
|
+
| 智谱 GLM | **任意层级** 的 union 或引用 → HTTP 500 |
|
|
172
|
+
| Gemini | 不支持 `oneOf`(直接 400);function calling 的 `enum` 必须全为字符串 |
|
|
173
|
+
| OpenAI 兼容层 | 透传(但 OpenRouter 会路由到 Gemini / Bedrock → 按最严处理) |
|
|
174
|
+
|
|
175
|
+
### 归一流水线
|
|
176
|
+
|
|
177
|
+
| 阶段 | 动作 |
|
|
178
|
+
| ---- | --------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
179
|
+
| P1 | 克隆输入,**永不修改调用方对象**(MCP 工具的 schema 是客户端缓存的同一引用,就地改写会永久污染缓存) |
|
|
180
|
+
| P2 | `$ref` 内联(`#/$defs/<name>` / `#/definitions/<name>`);剥离 `$defs` / `$schema` / `$id` / `ref` 等非标关键字;自引用按 8 层深度上限退化 |
|
|
181
|
+
| P3 | union 递归消除:null 分支 → `nullable`;全 object 分支 → 合并;同类型字面量 → 合并为 `enum`;否则取第一分支 |
|
|
182
|
+
| P4 | `type` 单值化(`["string","null"]` → `type` + `nullable`)、递归 `properties` / `items`、`required` 与 `properties` 对齐、数组补 `items.type` |
|
|
183
|
+
| P5 | Gemini 方言:`enum` 全字符串化(并登记 coercion) |
|
|
184
|
+
| P6 | 运行期反解:工具执行前把模型回传的 `"2"` 还原为 `2`、`"true"` 还原为 `true` |
|
|
185
|
+
|
|
186
|
+
### 唯一必经点
|
|
187
|
+
|
|
188
|
+
**模型入口**(`src/model/language/language.ts` 的 `resolveCall`)对整组 tools 统一调用 `adaptTools()`:
|
|
189
|
+
|
|
190
|
+
- 应用层**不做任何 schema 转换** —— 直接把 zod schema 交给 AI SDK 的 `tool()`;
|
|
191
|
+
- 厂商方言适配若放在应用层,必然漏调(每新增一个 host —— note / memory / 插件 —— 都得记得再调一次),且口径会与 llm 层漂移。
|
|
192
|
+
|
|
193
|
+
> 单测:`src/provider/__tests__/tool-schema.test.ts`(归一算法)与 `src/model/language/__tests__/tool-schema.test.ts`(模型入口确实生效)。
|
|
194
|
+
|
|
154
195
|
## 通用工具的权限模型(只判定、不弹窗)
|
|
155
196
|
|
|
156
197
|
`src/tool/buildin/` 是 9 个通用工具的静态内置基线(read / write / edit / grep / glob / bash / webfetch / websearch / todo)。
|