fast-tavern 0.1.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/README.md ADDED
@@ -0,0 +1,202 @@
1
+ # fast-tavern
2
+
3
+ fast-tavern 是一个**与框架无关**(不依赖 Vue/Pinia/浏览器 API)的“提示词处理引擎(prompt engine)”。
4
+
5
+ 它把:**预设(Preset)/ 世界书(Worldbook)/ 角色卡(Character)/ 正则(Regex)/ 宏(Macros)/ 聊天记录(History)**
6
+ 按固定规则组装,并输出清晰的 4 个阶段视图,方便预览与调试:
7
+
8
+ - 原始视图(raw)
9
+ - 宏前正则完视图(afterPreRegex)
10
+ - 宏执行完视图(afterMacro)
11
+ - 宏后正则完视图 / 最终视图(afterPostRegex)
12
+
13
+ ---
14
+
15
+ ## 功能概览
16
+
17
+ - **预设 Prompt Blocks 组装**:relative blocks 作为骨架,fixed blocks 按 depth 插入到聊天历史中
18
+ - **世界书触发与注入**:支持 always / conditional;支持 `<<keywords:a,b>>`;支持正则 condition
19
+ - **正则规则管道**:`before_macro` → `macro` → `after_macro`,并支持按 `target` / `view` 过滤
20
+ - **宏替换**:`{{char}}`、`{{user}}`、`<<user>>` 以及任意 `{{key}}`
21
+ - **多渠道聊天格式输入/输出**:Gemini / OpenAI / Simple / Tagged / Text
22
+ - **输出多阶段结果**:raw、afterPreRegex、afterMacro、afterPostRegex(并提供逐条 perItem 明细)
23
+
24
+ ### system role 策略
25
+
26
+ - 引擎内部 **保留 `system` role**(不会提前降级)。
27
+ - 仅在最终输出(`output` / `stages.output.*`)可通过 `systemRolePolicy` 控制:
28
+ - `keep`:保留 system
29
+ - `to_user`:将 system 降级为 user(保留内容,仅改变角色)
30
+
31
+ ---
32
+
33
+ ## 安装
34
+
35
+ ### 从 npm 安装
36
+
37
+ ```bash
38
+ npm i fast-tavern
39
+ ```
40
+
41
+ ### 本地安装(开发期)
42
+
43
+ ```bash
44
+ npm i ./packages/fast-tavern
45
+ ```
46
+
47
+ 构建(发布前):
48
+
49
+ ```bash
50
+ cd packages/fast-tavern
51
+ npm i
52
+ npm run build
53
+ ```
54
+
55
+ ---
56
+
57
+ ## 快速开始
58
+
59
+ ```ts
60
+ import { buildPrompt, History } from 'fast-tavern';
61
+
62
+ const result = buildPrompt({
63
+ preset, // PresetConfig JSON
64
+ character, // CharacterCard JSON(可选)
65
+ globals: {
66
+ // 支持一次性传入多个 JSON 文件:
67
+ // - worldbook.json({ name, entries })
68
+ // - 或直接 entries 数组
69
+ worldbooks,
70
+
71
+ // 支持一次性传入多个 JSON 文件:
72
+ // - RegexRule[]
73
+ // - 或 { regex_rules: RegexRule[] }
74
+ regexes
75
+ },
76
+
77
+ // 聊天记录入口:推荐用 History.* 明确格式
78
+ history: History.openai([
79
+ { role: 'system', content: 'You are a helpful assistant.' },
80
+ { role: 'user', content: 'Hello' },
81
+ { role: 'assistant', content: 'Hi!' }
82
+ ]),
83
+
84
+ // 正则视图:user_view / model_view
85
+ view: 'model_view',
86
+
87
+ // 宏变量
88
+ macros: { char: 'Alice', user: 'Bob' },
89
+
90
+ // 输出格式:gemini/openai/simple/text/tagged
91
+ outputFormat: 'openai',
92
+
93
+ // system 输出策略
94
+ systemRolePolicy: 'keep'
95
+ });
96
+
97
+ // 多阶段(最推荐用于调试/预览)
98
+ console.log(result.stages.tagged.raw);
99
+ console.log(result.stages.tagged.afterPreRegex);
100
+ console.log(result.stages.tagged.afterMacro);
101
+ console.log(result.stages.tagged.afterPostRegex);
102
+
103
+ // 最终输出(按 outputFormat 转换)
104
+ console.log(result.stages.output.afterPostRegex);
105
+
106
+ // 更细粒度:逐条文本的阶段明细
107
+ console.table(result.stages.perItem);
108
+ ```
109
+
110
+ ---
111
+
112
+ ## 核心 API
113
+
114
+ ### buildPrompt(params)
115
+
116
+ `buildPrompt()` 是主入口:输入配置与聊天记录,输出多阶段提示词。
117
+
118
+ 关键入参(摘选):
119
+
120
+ - `preset: PresetConfig`:预设配置(prompts / regex_rules / world_book)
121
+ - `character?: CharacterCard`:角色卡(可内嵌 world_book / regex_rules)
122
+ - `globals?: { worldbooks?: ..., regexes?: ... }`:全局世界书/正则(全局正则推荐直接传 rules 数组)
123
+ - `history: HistoryInput | MessageInput`:聊天记录(多格式)
124
+ - `view: 'user_view' | 'model_view'`:正则视图
125
+ - `macros: Record<string,string>`:宏变量表
126
+ - `outputFormat?: 'gemini'|'openai'|'simple'|'text'|'tagged'`:输出格式
127
+ - `systemRolePolicy?: 'keep'|'to_user'`:system 输出策略
128
+ - `options?: { recentHistoryForWorldbook?: number; positionMap?: Record<string,string> }`
129
+
130
+ 返回值重点字段:
131
+
132
+ - `result.stages.tagged.*`:带 tag/target 的分段结果(最适合 UI 预览)
133
+ - `result.stages.internal.*`:内部“类 Gemini”messages(Role: system/user/model)
134
+ - `result.stages.output.*`:按 outputFormat 转换后的分阶段输出
135
+ - `result.stages.perItem[]`:逐条文本的 raw/preRegex/macro/postRegex 对比
136
+
137
+ ### History.*(推荐)
138
+
139
+ 为了避免 `auto` 推断歧义,推荐用工厂函数创建明确格式的 history:
140
+
141
+ ```ts
142
+ import { History } from 'fast-tavern';
143
+
144
+ History.gemini(geminiContents)
145
+ History.openai(openaiMessages)
146
+ History.simple(simpleMessages)
147
+ History.tagged(taggedContents)
148
+ History.text('...')
149
+ History.auto(anyMessageInput)
150
+ ```
151
+
152
+ ### convertMessagesIn / convertMessagesOut
153
+
154
+ 用于“渠道格式 ↔ 内部格式”的转换(不做 preset/worldbook/regex/macro 的组装与编译)。
155
+
156
+ ```ts
157
+ import { convertMessagesIn, convertMessagesOut } from 'fast-tavern';
158
+
159
+ const { internal } = convertMessagesIn(openaiMessages, 'openai');
160
+ const backToOpenAI = convertMessagesOut(internal, 'openai');
161
+ ```
162
+
163
+ > tagged 格式无法从 internal 无损恢复 tag/target,所以 **tagged 输出应使用 buildPrompt 的 `outputFormat:'tagged'`**。
164
+
165
+ ---
166
+
167
+ ## 数据模型(与 SillyTavern 风格接近)
168
+
169
+ - `PresetConfig`:包含 `prompts: PromptBlock[]`、可选 `regex_rules`、可选 `world_book`
170
+ - `WorldbookEntry`:包含 `mode`、`condition`、`position`、`depth`、`order` 等
171
+ - `RegexRule`:包含 `targets`、`views`、`placement`(before/after macro)
172
+
173
+ 具体字段请以类型定义为准:
174
+
175
+ - `src/core/types.ts`
176
+
177
+ ---
178
+
179
+ ## 目录结构(便于分开调试)
180
+
181
+ - 转换层(每种格式一个文件):`src/core/channels/*`
182
+ - 功能模块(隔离实现):`src/core/modules/*`
183
+ - `worldbook/`:条件触发与合并
184
+ - `assemble/`:组装 tagged prompt list
185
+ - `regex/`:合并规则、应用规则
186
+ - `macro/`:宏替换
187
+ - `pipeline/`:before_regex → macro → after_regex(并输出阶段)
188
+ - `build/`:buildPrompt 总入口
189
+
190
+ ---
191
+
192
+ ## 常见用法建议
193
+
194
+ 1. **做 UI 预览**:用 `result.stages.tagged.*`(带 tag/target,最直观)
195
+ 2. **发给不同模型接口**:用 `outputFormat` 控制输出格式;如果下游不支持 system,设 `systemRolePolicy:'to_user'`,取 `result.stages.output.afterPostRegex`
196
+ 3. **定位正则/宏导致的问题**:用 `result.stages.perItem` 对比每一条在各阶段的变化
197
+
198
+ ---
199
+
200
+ ## License
201
+
202
+ MIT