fast-tavern 0.1.2 → 0.1.3
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.md +48 -147
- package/dist/index.cjs +637 -318
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +329 -345
- package/dist/index.d.ts +329 -345
- package/dist/index.js +629 -316
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,31 +1,25 @@
|
|
|
1
1
|
# fast-tavern
|
|
2
2
|
|
|
3
|
-
fast-tavern
|
|
3
|
+
fast-tavern 是一个**与框架无关**的“提示词组装与调试引擎(prompt engine)”。
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
它以 `st-api-wrapper` 的**新格式字段**为准,把:
|
|
6
|
+
**预设(Preset)/ 世界书(WorldBook)/ 角色卡(CharacterCard)/ 正则脚本(RegexScriptData)/ 宏与变量 / 聊天历史(ChatMessage)**
|
|
7
|
+
按固定流程组装,并输出**可调试的多阶段结果**(便于 UI 预览与定位问题):
|
|
7
8
|
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
9
|
+
- `raw`
|
|
10
|
+
- `afterPreRegex`(为兼容保留:当前等同 `raw`)
|
|
11
|
+
- `afterMacro`
|
|
12
|
+
- `afterPostRegex`(最终)
|
|
12
13
|
|
|
13
14
|
---
|
|
14
15
|
|
|
15
16
|
## 功能概览
|
|
16
17
|
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
- **变量系统**:支持局部/全局变量,在宏阶段线性执行(同时支持 `{{}}` 和 `<<>>` 格式)
|
|
23
|
-
- `{{getvar::name}}` / `<<getvar::name>>` - 获取局部变量
|
|
24
|
-
- `{{setvar::name::value}}` / `<<setvar::name::value>>` - 设置局部变量
|
|
25
|
-
- `{{getglobalvar::name}}` / `<<getglobalvar::name>>` - 获取全局变量
|
|
26
|
-
- `{{setglobalvar::name::value}}` / `<<setglobalvar::name::value>>` - 设置全局变量
|
|
27
|
-
- **多渠道聊天格式输入/输出**:Gemini / OpenAI / Simple / Tagged / Text
|
|
28
|
-
- **输出多阶段结果**:raw、afterPreRegex、afterMacro、afterPostRegex(并提供逐条 perItem 明细)
|
|
18
|
+
- **预设组装**:`position=relative` 作为骨架;`position=fixed` 按 `depth/order` 注入到 `chatHistory`
|
|
19
|
+
- **世界书触发与注入**:`always/keyword/vector`(vector 通过 hook);支持概率与递归控制
|
|
20
|
+
- **正则脚本**:对齐 `RegexScriptData`(targets/view/trim/{{match}}/macroMode/minDepth/maxDepth)
|
|
21
|
+
- **宏与变量**:宏保持简单;变量对齐 `local/global` 且值为 `any`
|
|
22
|
+
- **输出多阶段结果**:`stages.tagged/internal/output/perItem`
|
|
29
23
|
|
|
30
24
|
### system role 策略
|
|
31
25
|
|
|
@@ -54,171 +48,78 @@ npm i fast-tavern
|
|
|
54
48
|
### 本地安装(开发期)
|
|
55
49
|
|
|
56
50
|
```bash
|
|
57
|
-
|
|
51
|
+
# 在你的项目中
|
|
52
|
+
npm i ../fast-tavern
|
|
58
53
|
```
|
|
59
54
|
|
|
60
|
-
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## 开发与发布
|
|
58
|
+
|
|
59
|
+
### 构建
|
|
61
60
|
|
|
62
61
|
```bash
|
|
63
|
-
cd
|
|
62
|
+
cd npm-fast-tavern
|
|
64
63
|
npm i
|
|
65
64
|
npm run build
|
|
66
65
|
```
|
|
67
66
|
|
|
67
|
+
### 发布到 npm
|
|
68
|
+
|
|
69
|
+
1. **更新版本号**:修改 `package.json` 中的 `version`。
|
|
70
|
+
2. **构建项目**:执行 `npm run build`。
|
|
71
|
+
3. **登录 npm**(若未登录):`npm login`。
|
|
72
|
+
4. **执行发布**:`npm publish --access public`。
|
|
73
|
+
|
|
68
74
|
---
|
|
69
75
|
|
|
70
|
-
##
|
|
76
|
+
## 快速开始(最小示例)
|
|
71
77
|
|
|
72
78
|
```ts
|
|
73
79
|
import { buildPrompt, History } from 'fast-tavern';
|
|
74
80
|
|
|
75
81
|
const result = buildPrompt({
|
|
76
|
-
preset, //
|
|
77
|
-
character, // CharacterCard
|
|
78
|
-
persona, // PersonaCard JSON(可选)- 自动提取 user 宏
|
|
82
|
+
preset, // PresetInfo
|
|
83
|
+
character, // CharacterCard(可选)
|
|
79
84
|
globals: {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
// - 或直接 entries 数组
|
|
83
|
-
worldbooks,
|
|
84
|
-
|
|
85
|
-
// 支持一次性传入多个 JSON 文件:
|
|
86
|
-
// - RegexRule[]
|
|
87
|
-
// - 或 { regex_rules: RegexRule[] }
|
|
88
|
-
regexes
|
|
85
|
+
worldBooks,
|
|
86
|
+
regexScripts
|
|
89
87
|
},
|
|
90
88
|
|
|
91
|
-
//
|
|
89
|
+
// 聊天记录(ChatMessage[])
|
|
92
90
|
history: History.openai([
|
|
93
91
|
{ role: 'system', content: 'You are a helpful assistant.' },
|
|
94
92
|
{ role: 'user', content: 'Hello' },
|
|
95
93
|
{ role: 'assistant', content: 'Hi!' }
|
|
96
94
|
]),
|
|
97
95
|
|
|
98
|
-
// 正则视图:
|
|
99
|
-
view: '
|
|
100
|
-
|
|
101
|
-
// 宏变量(可选,会与 character/persona 自动提取的合并,显式传入的优先级更高)
|
|
102
|
-
macros: { custom_var: 'value' },
|
|
96
|
+
// 正则视图:user / model
|
|
97
|
+
view: 'model',
|
|
103
98
|
|
|
104
|
-
//
|
|
105
|
-
|
|
99
|
+
// 宏变量(可选)
|
|
100
|
+
macros: { user: 'Bob' },
|
|
106
101
|
|
|
107
|
-
//
|
|
108
|
-
|
|
102
|
+
// 变量(any)
|
|
103
|
+
variables: { score: 1, cfg: { a: 1 } },
|
|
109
104
|
|
|
110
|
-
// 输出格式:gemini/openai/
|
|
105
|
+
// 输出格式:gemini/openai/text/tagged
|
|
111
106
|
outputFormat: 'openai',
|
|
112
107
|
|
|
113
108
|
// system 输出策略
|
|
114
109
|
systemRolePolicy: 'keep'
|
|
115
110
|
});
|
|
116
111
|
|
|
117
|
-
//
|
|
118
|
-
console.log(result.stages.
|
|
119
|
-
console.log(result.stages.tagged.afterPreRegex);
|
|
120
|
-
console.log(result.stages.tagged.afterMacro);
|
|
121
|
-
console.log(result.stages.tagged.afterPostRegex);
|
|
112
|
+
console.log(result.stages.tagged.afterPostRegex); // UI 预览最推荐
|
|
113
|
+
console.log(result.stages.output.afterPostRegex); // 最终输出
|
|
122
114
|
|
|
123
|
-
|
|
124
|
-
console.log(result.stages.output.afterPostRegex);
|
|
125
|
-
|
|
126
|
-
// 获取变量最终状态(用于持久化)
|
|
127
|
-
console.log(result.variables.local); // 局部变量
|
|
128
|
-
console.log(result.variables.global); // 全局变量
|
|
129
|
-
|
|
130
|
-
// 更细粒度:逐条文本的阶段明细
|
|
131
|
-
console.table(result.stages.perItem);
|
|
115
|
+
console.log(result.variables.local);
|
|
132
116
|
```
|
|
133
117
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
## 核心 API
|
|
137
|
-
|
|
138
|
-
### buildPrompt(params)
|
|
139
|
-
|
|
140
|
-
`buildPrompt()` 是主入口:输入配置与聊天记录,输出多阶段提示词。
|
|
141
|
-
|
|
142
|
-
关键入参(摘选):
|
|
143
|
-
|
|
144
|
-
- `preset: PresetConfig`:预设配置(prompts / regex_rules / world_book)
|
|
145
|
-
- `character?: CharacterCard`:角色卡(可内嵌 world_book / regex_rules,自动提取 char 宏)
|
|
146
|
-
- `persona?: PersonaCard`:用户人设卡(自动提取 user 宏)
|
|
147
|
-
- `globals?: { worldbooks?: ..., regexes?: ... }`:全局世界书/正则(全局正则推荐直接传 rules 数组)
|
|
148
|
-
- `history: HistoryInput | MessageInput`:聊天记录(多格式)
|
|
149
|
-
- `view: 'user_view' | 'model_view'`:正则视图
|
|
150
|
-
- `macros?: Record<string,string>`:宏变量表(会与 character/persona 自动提取的合并)
|
|
151
|
-
- `outputFormat?: 'gemini'|'openai'|'simple'|'text'|'tagged'`:输出格式
|
|
152
|
-
- `systemRolePolicy?: 'keep'|'to_user'`:system 输出策略
|
|
153
|
-
- `options?: { recentHistoryForWorldbook?: number; positionMap?: Record<string,string> }`
|
|
154
|
-
|
|
155
|
-
返回值重点字段:
|
|
156
|
-
|
|
157
|
-
- `result.stages.tagged.*`:带 tag/target 的分段结果(最适合 UI 预览)
|
|
158
|
-
- `result.stages.internal.*`:内部“类 Gemini”messages(Role: system/user/model)
|
|
159
|
-
- `result.stages.output.*`:按 outputFormat 转换后的分阶段输出
|
|
160
|
-
- `result.stages.perItem[]`:逐条文本的 raw/preRegex/macro/postRegex 对比
|
|
161
|
-
|
|
162
|
-
### History.*(推荐)
|
|
163
|
-
|
|
164
|
-
为了避免 `auto` 推断歧义,推荐用工厂函数创建明确格式的 history:
|
|
165
|
-
|
|
166
|
-
```ts
|
|
167
|
-
import { History } from 'fast-tavern';
|
|
168
|
-
|
|
169
|
-
History.gemini(geminiContents)
|
|
170
|
-
History.openai(openaiMessages)
|
|
171
|
-
History.simple(simpleMessages)
|
|
172
|
-
History.tagged(taggedContents)
|
|
173
|
-
History.text('...')
|
|
174
|
-
History.auto(anyMessageInput)
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
### convertMessagesIn / convertMessagesOut
|
|
178
|
-
|
|
179
|
-
用于“渠道格式 ↔ 内部格式”的转换(不做 preset/worldbook/regex/macro 的组装与编译)。
|
|
180
|
-
|
|
181
|
-
```ts
|
|
182
|
-
import { convertMessagesIn, convertMessagesOut } from 'fast-tavern';
|
|
183
|
-
|
|
184
|
-
const { internal } = convertMessagesIn(openaiMessages, 'openai');
|
|
185
|
-
const backToOpenAI = convertMessagesOut(internal, 'openai');
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
> tagged 格式无法从 internal 无损恢复 tag/target,所以 **tagged 输出应使用 buildPrompt 的 `outputFormat:'tagged'`**。
|
|
189
|
-
|
|
190
|
-
---
|
|
191
|
-
|
|
192
|
-
## 数据模型(与 SillyTavern 风格接近)
|
|
193
|
-
|
|
194
|
-
- `PresetConfig`:包含 `prompts: PromptBlock[]`、可选 `regex_rules`、可选 `world_book`
|
|
195
|
-
- `WorldbookEntry`:包含 `mode`、`condition`、`position`、`depth`、`order` 等
|
|
196
|
-
- `RegexRule`:包含 `targets`、`views`、`placement`(before/after macro)
|
|
197
|
-
|
|
198
|
-
具体字段请以类型定义为准:
|
|
199
|
-
|
|
200
|
-
- `src/core/types.ts`
|
|
201
|
-
|
|
202
|
-
---
|
|
203
|
-
|
|
204
|
-
## 目录结构(便于分开调试)
|
|
205
|
-
|
|
206
|
-
- 转换层(每种格式一个文件):`src/core/channels/*`
|
|
207
|
-
- 功能模块(隔离实现):`src/core/modules/*`
|
|
208
|
-
- `worldbook/`:条件触发与合并
|
|
209
|
-
- `assemble/`:组装 tagged prompt list
|
|
210
|
-
- `regex/`:合并规则、应用规则
|
|
211
|
-
- `macro/`:宏替换
|
|
212
|
-
- `pipeline/`:before_regex → macro → after_regex(并输出阶段)
|
|
213
|
-
- `build/`:buildPrompt 总入口
|
|
214
|
-
|
|
215
|
-
---
|
|
216
|
-
|
|
217
|
-
## 常见用法建议
|
|
118
|
+
## 文档
|
|
218
119
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
120
|
+
- **格式与组装流程(详细)**:[`docs/FORMAT_ZH.md`](docs/FORMAT_ZH.md)
|
|
121
|
+
- **API 文档**:[`docs/API_ZH.md`](docs/API_ZH.md)
|
|
122
|
+
- **使用教程(详细示例)**:[`docs/GUIDE_ZH.md`](docs/GUIDE_ZH.md)
|
|
222
123
|
|
|
223
124
|
---
|
|
224
125
|
|