-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.ts
199 lines (182 loc) · 6.17 KB
/
new.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
import { PlaceholderType, PlaceholderCategory, Placeholder } from "./types";
/**
* A dummy placeholder.
* @returns A placeholder object.
*/
export const dummyPlaceholder = (): Placeholder => {
return {
name: "New Placeholder",
regex: /{{newPlaceholder}}/g,
rules: [],
apply: async (str: string, context?: { [key: string]: unknown }) => ({
result: "",
}),
constant: true,
fn: async (content: string) =>
(await dummyPlaceholder().apply("{{newPlaceholder}}")).result,
description: "A dummy placeholder.",
example: "This is an example of a dummy placeholder: {{newPlaceholder}}",
hintRepresentation: "{{newPlaceholder}}",
fullRepresentation: "Dummy Placeholder",
type: PlaceholderType.Informational,
categories: [],
};
};
/**
* Creates a new placeholder.
* @param name The name of the placeholder.
* @param regex The regex to match the placeholder.
* @param apply_fn The function to apply to the placeholder. Specify either this or replace_with.
* @param replace_with The string to replace the placeholder with. Specify either this or apply_fn.
* @param constant Whether the placeholder is constant.
* @param description A brief description of the placeholder.
* @param example An example of the placeholder.
* @param hintRepresentation A representation of the placeholder to show in hints.
* @param fullRepresentation A representation of the placeholder to show in detailed explanations.
* @param type The type of the placeholder.
* @param categories The categories of the placeholder.
* @returns A placeholder object.
*/
export const newPlaceholder = (
name: string,
options?: {
/**
* The regex to match the placeholder.
*/
regex?: RegExp;
/**
* The string to replace the placeholder with. Specify either this or apply_fn. Can include other placeholders.
*/
replace_with?: string;
/**
* The rules that determine whether or not the placeholder is relevant in the current context.
* @param str The string to check the rules against.
* @param context The context object to store & retrieve values from.
* @returns True if the placeholder should be replaced, false otherwise.
*/
rules?: ((
str: string,
context?: { [key: string]: unknown }
) => Promise<boolean>)[];
/**
* The function to apply to the placeholder. Specify either this or replace_with.
* @param str The string to apply the placeholder to.
* @param context The context object to store & retrieve values from.
* @returns An object containing the result of the placeholder and any other values to store in the context object.
*/
apply_fn?: (
str: string,
context?: { [key: string]: unknown }
) => Promise<{ result: string; [key: string]: unknown }>;
/**
* Whether the placeholder's value is constant over the course of a single run.
*/
constant?: boolean;
/**
* A brief description of the placeholder.
*/
description?: string;
/**
* An example of the placeholder in use.
*/
example?: string;
/**
* A representation of the placeholder to show in hints.
*/
hintRepresentation?: string;
/**
* A representation of the placeholder to show in detailed explanations.
*/
fullRepresentation?: string;
/**
* The type of the placeholder.
*/
type?: PlaceholderType;
/**
* The categories of the placeholder.
*/
categories?: PlaceholderCategory[];
}
) => {
if (options?.apply_fn != undefined && options?.replace_with != undefined)
throw new Error("Cannot specify both apply_fn and replace_with");
if (options?.replace_with != undefined) {
if (options.constant) {
options.apply_fn = async (
str: string,
context?: { [key: string]: unknown }
) => ({
result: options.replace_with || "",
[name]: options.replace_with || "",
});
} else {
options.apply_fn = async (
str: string,
context?: { [key: string]: unknown }
) => ({
result: options.replace_with || "",
[name]: options.replace_with || "",
});
}
}
const newPlaceholder: Placeholder = {
name: name,
regex: options?.regex || new RegExp(`{{${name}}}`, "g"),
rules: options?.rules || [],
apply:
options?.apply_fn ||
(async (str: string, context?: { [key: string]: unknown }) => ({
result: "",
})),
result_keys: [name],
constant: options?.constant || false,
fn: async (content: string) =>
(await newPlaceholder.apply(`{{${name}}}`)).result,
description: options?.description || "",
example: options?.example || "",
hintRepresentation: options?.hintRepresentation || `{{${name}}}`,
fullRepresentation: options?.fullRepresentation || `${name} (Custom)`,
type: options?.type || PlaceholderType.Informational,
categories: options?.categories || [],
};
return newPlaceholder;
};
/**
* Builds a list of placeholders from a list of placeholder names and values.
* @param valueDict A dictionary of placeholder names and values.
* @returns A list of placeholders.
*/
export const buildPlaceholdersFromValueDict = (valueDict: {
[key: string]: string;
}) => {
const placeholders: Placeholder[] = [];
for (const key in valueDict) {
if (Object.prototype.hasOwnProperty.call(valueDict, key)) {
const value = valueDict[key];
placeholders.push(
newPlaceholder(key, { replace_with: value, constant: true })
);
}
}
return placeholders;
};
/**
* Builds a list of placeholders from a list of placeholder names and application functions.
* @param fnDict A dictionary of placeholder names and application functions.
* @returns A list of placeholders.
*/
export const buildPlaceholdersFromFnDict = (fnDict: {
[key: string]: (
str: string,
context?: { [key: string]: unknown }
) => Promise<{ result: string; [key: string]: unknown }>;
}) => {
const placeholders: Placeholder[] = [];
for (const key in fnDict) {
if (Object.prototype.hasOwnProperty.call(fnDict, key)) {
const fn = fnDict[key];
placeholders.push(newPlaceholder(key, { apply_fn: fn }));
}
}
return placeholders;
};