-
Notifications
You must be signed in to change notification settings - Fork 1
/
global.d.ts
258 lines (216 loc) · 7.33 KB
/
global.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/** The primitive data-types. */
type Primitives = number | string | boolean | Function | {};
type DefinedOf<T extends {}> = {
[K in keyof T]?: T[K] extends undefined ? never : T[K];
};
type Maybe<T> = T | null | undefined;
namespace NAI {
interface ContextConfig {
prefix: string;
suffix: string;
tokenBudget: number;
reservedTokens: number;
budgetPriority: number;
trimDirection: "trimBottom" | "trimTop" | "doNotTrim";
insertionType: "newline" | "sentence" | "token";
maximumTrimType: "newline" | "sentence" | "token";
insertionPosition: number;
}
interface LoreEntryConfig {
searchRange: number;
enabled: boolean;
forceActivation: boolean;
keyRelative: boolean;
nonStoryActivatable: boolean;
}
interface LoreBookConfig {
orderByKeyLocations: boolean;
}
interface LoreEntry extends LoreEntryConfig {
text: string;
displayName: string;
keys: string[];
contextConfig: ContextConfig;
lastUpdatedAt?: number;
}
interface LoreBook {
lorebookVersion: 2;
settings: LoreBookConfig;
entries: LoreEntry[];
}
}
namespace TLG {
type TypePredicate<T> = (value: any) => value is T;
interface BuilderSettings {
/**
* Iterates through the `text` of a `BuildableEntry` in reverse, which
* is useful for maintaining the same presentation order when inserting
* entries after the story has been inserted.
*/
reversedTextIteration: boolean;
}
namespace Matching {
interface EscapedRegex {
isEscaped: true;
toNAI(): string;
toString(): string;
}
type Phrase = string | RegExp | PhraseExp | EscapedRegex;
type BinaryOperator = (left: Phrase, right: Phrase) => EscapedRegex;
interface ExtBinaryOperator {
(): BinaryOperator;
isExtBinaryOp: true;
}
type PhraseOperator = BinaryOperator | ExtBinaryOperator;
type PhraseOperand = Phrase | PhraseExp;
type PhraseExp = [PhraseOperand, PhraseOperator, PhraseOperand];
}
namespace Config {
type NaiConfig = NAI.ContextConfig | NAI.LoreEntryConfig;
/**
* A common interface for minimum configuration needed by the entry builder to
* construct `NAI.LoreEntry` instances.
*/
interface CommonConfig {
/** The current context configuration. */
context: NAI.ContextConfig;
/** The current entry configuration. */
entry: NAI.LoreEntryConfig;
}
interface State extends BuilderSettings, CommonConfig {
/**
* A list of strategies already applied by ancestor `TLG.BuildableEntry`.
*
* Strategies can use this to see what other strategies have already been applied to
* the `context` and `entry` configuration provided by `State`.
*/
strategyStack: Strategy<string, any>[];
/**
* The current depth in the `TLG.BuildableEntry` tree.
*
* The root entries will have a depth of `0`.
*/
depth: number;
}
interface StrategyMethod<TStrategyConfig, TConfig extends NaiConfig> {
/**
* A method that builds either a `NAI.ContextConfig` or `NAI.LoreEntryConfig`.
*
* @param state
* The current state from the entry builder.
* @param strategyConfig
* The finalized strategy configuration.
*/
(state: State, strategyConfig: TStrategyConfig): TConfig;
}
/**
* Represents a method of manipulating the `context` and `entry` configuration
* as the entry builder traverses the `entries` of `TLG.BuilderConfig`.
*/
interface Strategy<TType extends string, TStrategyConfig> {
/**
* The type of this strategy. Strategies can use this to see what other strategies
* have already been applied to the `context` and `entry` configuration.
*/
type: TType;
/**
* The configuration provided to the strategy, initially.
*/
config: Partial<TStrategyConfig>;
/**
* Applies the strategy to the current state, producing a finalized configuration
* to be passed to `context` and `entry`.
*/
apply: (state: State, currentConfig: Partial<TStrategyConfig>) => TStrategyConfig;
/**
* Extends the strategy for the next state, producing a finalized configuration
* to be passed to `context` and `entry`.
*/
extend: (state: State, currentConfig: Partial<TStrategyConfig>) => TStrategyConfig;
/**
* Outputs a finalized `NAI.ContextConfig` for an entry.
*/
context: StrategyMethod<TStrategyConfig, NAI.ContextConfig>;
/**
* Outputs a finalized `NAI.LoreEntryConfig` for an entry.
*/
entry: StrategyMethod<TStrategyConfig, NAI.LoreEntryConfig>;
}
}
interface BuildableEntryConfig {
/**
* The strategy to use when processing this entry.
*
* Strategies provide a method of manipulating the `context` and `entry` configuration
* as the entry builder traverses the `entries` of `BuilderConfig`.
*/
strategy?: Config.Strategy<string, any>;
/**
* The operator that related `subEntries` will use to combine their parent's
* keys with their own. This will be provided to the `subEntries` as their
* default `rootOp`, if it is not set.
*
* Defaults to the value of `rootOp`.
*/
subOp?: Matching.PhraseOperator;
}
interface BuildableEntry extends BuildableEntryConfig {
/**
* The name for this entry.
*/
name: string;
/**
* The keys inherited from the parent. This is set to whatever keys were
* ultimately used to output the parent entries.
*
* You can also override this with a new array retain a parent-child relationship,
* but refocus the keys for this entry.
*
* When this is a function, it will receive the parent keys, allowing you to
* manipulate or add to the keys without fully replacing them.
*/
baseKeys?: Matching.PhraseOperand[] | ((baseKeys: Matching.PhraseOperand[]) => Matching.PhraseOperand[]);
/**
* The operator that will be used to join the `baseKeys` with the `keys`.
*
* Defaults to `AND`.
*/
baseOp?: Matching.PhraseOperator;
/**
* The keys for the entry. May be an empty array, but must be present.
*/
keys: Matching.PhraseOperand[];
/**
* A string or several strings that will each be converted into an entry.
*/
text?: string | string[];
/**
* Other entries that are related to this entry.
*
* Each sub-entry will require the keys of this entry to match, using the
* operator specified in `subOp`, in order for them to match.
*/
subEntries?: BuildableEntry[];
}
interface BuilderConfig {
/**
* The default strategy to use when an entry in `entries` does not provide one.
*
* Strategies provide a method of manipulating the `context` and `entry` configuration
* as the entry builder traverses the `entries` of `TLG.BuilderConfig`.
*/
strategy?: Config.Strategy<string, any>;
/**
* Settings for the lorebook itself.
*/
settings?: Partial<NAI.LoreBookConfig & BuilderSettings>;
/**
* The list of `TLG.BuildableEntry` to build `NAI.LoreEntry` from.
*/
entries: BuildableEntry[];
}
interface WithDisplay {
/** Shorthand to convert to a JSON string for RunKit. */
forDisplay(): string;
}
}