my-ai-chat-framework 3.0.0 → 4.0.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 +52 -0
- package/README.md +3 -3
- package/README_ZH.md +22 -5
- package/dist/my-ai-chat-framework.browser.es.js +599 -292
- package/dist/my-ai-chat-framework.browser.es.js.map +1 -1
- package/dist/my-ai-chat-framework.browser.umd.js +601 -291
- package/dist/my-ai-chat-framework.browser.umd.js.map +1 -1
- package/dist/my-ai-chat-framework.node.cjs.js +601 -291
- package/dist/my-ai-chat-framework.node.cjs.js.map +1 -1
- package/docs/DEVELOPER.md +100 -10
- package/docs/README.md +33 -0
- package/package.json +1 -1
- package/src/adapters/openai.js +44 -18
- package/src/core/ChatService.js +250 -141
- package/src/core/Errors.js +13 -0
- package/src/core/SystemPromptStore.js +7 -5
- package/src/index.js +2 -1
- package/src/plugins/continuation.js +156 -0
- package/src/plugins/model-registry.js +16 -3
- package/src/plugins/tool-calling.js +46 -18
- package/src/utils/MessageFormatter.js +138 -46
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,58 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## [4.0.0] - 2026-09
|
|
6
|
+
|
|
7
|
+
> 主题:**把"玩法"从核心搬出去,并给核心补上"出口"**。
|
|
8
|
+
> 一句话:核心只保留"怎么合并、怎么发、怎么映射字段";"续写 / 临时消息"变成可选的 continuation 插件。
|
|
9
|
+
|
|
10
|
+
### Added(清债 · A 组)
|
|
11
|
+
- **A1 删旧通道**:`_hooks` / `beforeRequest` 钩子 / `_processResponse` 全部移除(改用 `pipe` 车间 / `replaceStage`);`beforeSend` 步骤改空实现
|
|
12
|
+
- **A2 可扩展配置白名单**:`registerConfigKeys(keys)` / `unregisterConfigKeys` / `configKeys`
|
|
13
|
+
—— 插件自带配置(如 continuation 的 `autoContinue`)可运行时修改,**不必回头改核心**
|
|
14
|
+
- **A3 错误类型统一**:新增 `ValidationError extends ConfigurationError`,15 处裸 `Error` 全部类型化
|
|
15
|
+
- **A5 流式增量**:`onProgress(snap, delta)` —— `snap` 全量、`delta` 本次新增
|
|
16
|
+
- **A6 SSE 健壮化**:支持 `\r\n`、`data:`(无空格)、多行 `data` 拼接
|
|
17
|
+
|
|
18
|
+
### Changed(架构 · B 组)
|
|
19
|
+
- **B1 每请求独立中断**:`abort()` 中断全部 / `abort(id)` 精确中断 / `activeRequests`;多请求互不干扰(旧版共享单个 controller,会中断错的那个)
|
|
20
|
+
- **B3 消息存储可注入**:`new ChatService({ store })`(为将来的对话树/自定义存储留口)
|
|
21
|
+
- **B4 插件命名空间**:插件 API 挂到 `chat.plugins.<插件名>`,不再直接挂 `chat`(撞名从根上消失);三个插件都提供 `uninstall`
|
|
22
|
+
- **A4 工具去限制 / 参数不默认**(BREAKING):
|
|
23
|
+
- 工具循环**默认不限轮数**(旧版硬编码 5 轮);`timeout` 只在显式传入时生效,并修掉超时泄漏(clearTimeout + AbortController 通知工具)
|
|
24
|
+
- `registerTool` 生成标准 JSON Schema(必填项进顶层 `required`,properties 不再残留)
|
|
25
|
+
- `temperature` / `maxTokens` **不再有默认值** —— 不传就不放进请求体(由 API 用自己的默认)
|
|
26
|
+
|
|
27
|
+
### Docs
|
|
28
|
+
- **docs/API-STABILITY.md**:稳定性契约(Stable 清单 / 四个协议 / 事件 payload 形状 / 兼容纪律 / v3→v4 迁移记录)
|
|
29
|
+
|
|
30
|
+
### Added
|
|
31
|
+
- **出口 API**:`chat.replaceStage(name, fn)` / `restoreStage` / `unstage` / `restage` / `getStage` / `internalStages`
|
|
32
|
+
—— 5 个内部步骤(prepareInput / beforeSend / autoContinue / buildRequest / send)可替换、可关闭、可包装
|
|
33
|
+
- **消息过滤器注册点**:`MessageFormatter.registerFilter(name, fn, { priority })` / `unregisterFilter` / `listFilters`
|
|
34
|
+
—— 决定"哪些消息进入请求"的规则变成可插拔链(同名覆盖,返回旧函数)
|
|
35
|
+
- **continuation 插件**(`src/plugins/continuation.js`,随框架发布,非独立包):
|
|
36
|
+
- 续写检测(`replaceStage('autoContinue', …)`)、ephemeral 过滤规则、`chat.continueLast` / `continueLastStream`
|
|
37
|
+
- `createContinuationPlugin({ autoContinue = false })` + `plugin.uninstall(chat)` 完整还原
|
|
38
|
+
- 导出 `ephemeralFilterRule` / `prepareContinue`(只要规则不要插件时可用)
|
|
39
|
+
- `sendExisting(params, { mergeToEntry })` / `sendExistingStream(params, { mergeToEntry }, onProgress, onDone)`
|
|
40
|
+
—— "把结果并回指定消息"成为公开能力(兼容旧签名)
|
|
41
|
+
|
|
42
|
+
### Changed(BREAKING)
|
|
43
|
+
- **`new ChatService()` 不再自带续写与临时消息**:核心删除了 `continueLast` / `continueLastStream` / `_prepareContinue`,
|
|
44
|
+
`autoContinue` 步骤改为空实现,`MessageFormatter` 不再内置 ephemeral 过滤规则
|
|
45
|
+
→ 需要这些能力的:`chat.use(createContinuationPlugin({ autoContinue: true }))`
|
|
46
|
+
- `config.ephemeralContinue` 移除(`updateConfig` 白名单同步收窄);改用插件选项 `autoContinue`
|
|
47
|
+
- 消息"保留/丢弃"规则改为过滤器实现(行为与 v3.x 一致:ephemeral 底部连续、system 分流、空内容清理)
|
|
48
|
+
|
|
49
|
+
### 迁移(从 3.x 升到 4.0)
|
|
50
|
+
```js
|
|
51
|
+
// 旧:什么都不用做,续写开箱可用
|
|
52
|
+
// 新:一行装配
|
|
53
|
+
import { createContinuationPlugin } from 'my-ai-chat-framework';
|
|
54
|
+
chat.use(createContinuationPlugin({ autoContinue: true }));
|
|
55
|
+
```
|
|
56
|
+
|
|
5
57
|
## [3.0.0] - 2026-08-?
|
|
6
58
|
|
|
7
59
|
### Added
|
package/README.md
CHANGED
|
@@ -136,7 +136,7 @@ const chat = new ChatService({
|
|
|
136
136
|
### Registering a Tool
|
|
137
137
|
|
|
138
138
|
```javascript
|
|
139
|
-
chat.registerTool('get_weather', 'Get current weather for a city',
|
|
139
|
+
chat.plugins['tool-calling'].registerTool('get_weather', 'Get current weather for a city',
|
|
140
140
|
async (args) => {
|
|
141
141
|
// args = { city: 'Beijing' }
|
|
142
142
|
return `Weather in ${args.city}: 22°C, sunny`;
|
|
@@ -172,7 +172,7 @@ Converts internal messages to OpenAI‑compatible format. Supports:
|
|
|
172
172
|
|
|
173
173
|
Detects `tool_calls` in assistant responses, executes registered tools, feeds results back, and continues the conversation (up to `maxIterations` = 5).
|
|
174
174
|
|
|
175
|
-
- `chat.registerTool(name, description, executor, parameters?)` – register a tool
|
|
175
|
+
- `chat.plugins['tool-calling'].registerTool(name, description, executor, parameters?)` – register a tool
|
|
176
176
|
- Automatically injects `tool` role messages into the conversation
|
|
177
177
|
- Recovers from tool execution errors gracefully (logs error, returns error message to model)
|
|
178
178
|
|
|
@@ -231,7 +231,7 @@ unsubscribe(); // stop listening
|
|
|
231
231
|
| `chat.use(plugin)` | `this` | Install a plugin/adapter |
|
|
232
232
|
| `chat.setAdapter(adapter)` | `void` | Manually set the adapter |
|
|
233
233
|
| `chat.on(event, handler)` | `unsubscribe function` | Subscribe to events |
|
|
234
|
-
| `chat.registerTool(name, desc, fn, params?)` | `this` | Register a tool (requires toolCallingPlugin) |
|
|
234
|
+
| `chat.plugins['tool-calling'].registerTool(name, desc, fn, params?)` | `this` | Register a tool (requires toolCallingPlugin) |
|
|
235
235
|
| `chat.messages` | `MessageStore` | Access the message store directly |
|
|
236
236
|
|
|
237
237
|
---
|
package/README_ZH.md
CHANGED
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
- **灵活配置** – 支持平铺和嵌套 `modelParams` 两种配置风格。
|
|
18
18
|
- **事件驱动** – 内置 EventEmitter,支持 `message`、`sending`、`error`、`stream-progress` 事件。
|
|
19
19
|
- **管道车间(v3.0)** – `chat.pipe()` 在任何阶段挂自定义功能(beforeSend/afterSend),核心代码不用改。
|
|
20
|
+
- **出口 API(v4.0)** – `chat.replaceStage()` / `chat.unstage()`:内部 5 个步骤可替换、可关闭、可包装。
|
|
21
|
+
- **玩法插件化(v4.0)** – 续写 / 临时消息住在 `continuation` 插件里(核心不含业务玩法);`MessageFormatter.registerFilter()` 可自定义"哪些消息进请求"。
|
|
20
22
|
- **类型化错误** – `APIError`、`NetworkError`、`ConfigurationError`、`ParsingError`,方便分类处理。
|
|
21
23
|
|
|
22
24
|
---
|
|
@@ -37,9 +39,10 @@ src/
|
|
|
37
39
|
│ └── openai.js # OpenAI 兼容 API 适配器(协议见 assertAdapter)
|
|
38
40
|
├── plugins/
|
|
39
41
|
│ ├── tool-calling.js # 工具调用插件(afterSend 车间,自动检测&循环执行)
|
|
40
|
-
│
|
|
42
|
+
│ ├── model-registry.js # 模型能力表(beforeSend 车间)
|
|
43
|
+
│ └── continuation.js # 续写 / 临时消息玩法(v4.0 从核心迁出,可整体卸载)
|
|
41
44
|
└── utils/
|
|
42
|
-
├── MessageFormatter.js #
|
|
45
|
+
├── MessageFormatter.js # 消息格式转换 + 消息过滤器链(可注册)
|
|
43
46
|
├── typeCheck.js # 类型判断工具
|
|
44
47
|
└── url.js # URL 拼接工具
|
|
45
48
|
tests/ # node:test 单元测试(core / pipeline / integration)
|
|
@@ -134,7 +137,7 @@ const chat = new ChatService({
|
|
|
134
137
|
### 注册工具
|
|
135
138
|
|
|
136
139
|
```javascript
|
|
137
|
-
chat.registerTool('get_weather', '获取指定城市的天气',
|
|
140
|
+
chat.plugins['tool-calling'].registerTool('get_weather', '获取指定城市的天气',
|
|
138
141
|
async (args) => {
|
|
139
142
|
// args = { city: '北京' }
|
|
140
143
|
return `${args.city}天气:22°C,晴`;
|
|
@@ -170,7 +173,7 @@ await chat.send('北京今天天气怎么样?');
|
|
|
170
173
|
|
|
171
174
|
检测助手响应中的 `tool_calls`,执行已注册的工具,将结果回传并继续对话(最多 `maxIterations` = 5 轮)。
|
|
172
175
|
|
|
173
|
-
- `chat.registerTool(name, description, executor, parameters?)` – 注册工具
|
|
176
|
+
- `chat.plugins['tool-calling'].registerTool(name, description, executor, parameters?)` – 注册工具
|
|
174
177
|
- 自动向对话中插入 `role: 'tool'` 消息
|
|
175
178
|
- 工具执行出错时优雅降级(记录日志,将错误信息返回给模型)
|
|
176
179
|
- 配置:`createToolCallingPlugin({ timeout: 30000, maxIterations: 5 })`
|
|
@@ -213,7 +216,7 @@ unsubscribe(); // 停止监听
|
|
|
213
216
|
| `chat.use(plugin)` | `this` | 安装插件/适配器 |
|
|
214
217
|
| `chat.setAdapter(adapter)` | `void` | 手动设置适配器 |
|
|
215
218
|
| `chat.on(event, handler)` | `取消订阅函数` | 订阅事件 |
|
|
216
|
-
| `chat.registerTool(name, desc, fn, params?)` | `this` | 注册工具(需安装 toolCallingPlugin) |
|
|
219
|
+
| `chat.plugins['tool-calling'].registerTool(name, desc, fn, params?)` | `this` | 注册工具(需安装 toolCallingPlugin) |
|
|
217
220
|
| `chat.messages` | `MessageStore` | 直接访问消息存储 |
|
|
218
221
|
|
|
219
222
|
---
|
|
@@ -322,6 +325,20 @@ npm install
|
|
|
322
325
|
# 开发模式(监听文件变化)
|
|
323
326
|
npm run dev
|
|
324
327
|
|
|
328
|
+
## 📚 文档
|
|
329
|
+
|
|
330
|
+
| 我想…… | 看这份 |
|
|
331
|
+
|---|---|
|
|
332
|
+
| 快速抄代码上手 | [docs/COOKBOOK.md](./docs/COOKBOOK.md) —— 16 个常见任务配方 |
|
|
333
|
+
| 查方法 / 事件 / 错误 | [docs/API-REFERENCE.md](./docs/API-REFERENCE.md) |
|
|
334
|
+
| 搞懂设计 | [docs/ARCHITECTURE.md](./docs/ARCHITECTURE.md) |
|
|
335
|
+
| 确认哪些能改 | [docs/API-STABILITY.md](./docs/API-STABILITY.md)(**v4.0 起"只加不改"**) |
|
|
336
|
+
| 全部文档索引 | [docs/README.md](./docs/README.md) |
|
|
337
|
+
|
|
338
|
+
可运行示例:`node examples/selfcheck.js`(无网络自检 21 项)· 打开 `examples/demo.html`(浏览器真实对话)
|
|
339
|
+
|
|
340
|
+
---
|
|
341
|
+
|
|
325
342
|
## 🧩 扩展:管道车间(v3.0)
|
|
326
343
|
|
|
327
344
|
核心流程是一辆小车(ctx)依次开过车间;加新功能 = 注册一个车间,核心代码不用改。
|