@riotprompt/riotprompt 0.0.1
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/.gitcarve/config.yaml +10 -0
- package/.gitcarve/context/content.md +11 -0
- package/.markdown-doctest-setup.mjs +23 -0
- package/.nvmrc +1 -0
- package/LICENSE +190 -0
- package/README.md +513 -0
- package/dist/builder.cjs +152 -0
- package/dist/builder.cjs.map +1 -0
- package/dist/builder.d.ts +37 -0
- package/dist/builder.js +148 -0
- package/dist/builder.js.map +1 -0
- package/dist/chat.cjs +26 -0
- package/dist/chat.cjs.map +1 -0
- package/dist/chat.d.ts +14 -0
- package/dist/chat.js +21 -0
- package/dist/chat.js.map +1 -0
- package/dist/constants.cjs +34 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/constants.d.ts +13 -0
- package/dist/constants.js +23 -0
- package/dist/constants.js.map +1 -0
- package/dist/formatter.cjs +139 -0
- package/dist/formatter.cjs.map +1 -0
- package/dist/formatter.d.ts +88 -0
- package/dist/formatter.js +131 -0
- package/dist/formatter.js.map +1 -0
- package/dist/items/content.cjs +14 -0
- package/dist/items/content.cjs.map +1 -0
- package/dist/items/content.d.ts +3 -0
- package/dist/items/content.js +10 -0
- package/dist/items/content.js.map +1 -0
- package/dist/items/context.cjs +13 -0
- package/dist/items/context.cjs.map +1 -0
- package/dist/items/context.d.ts +3 -0
- package/dist/items/context.js +9 -0
- package/dist/items/context.js.map +1 -0
- package/dist/items/instruction.cjs +13 -0
- package/dist/items/instruction.cjs.map +1 -0
- package/dist/items/instruction.d.ts +3 -0
- package/dist/items/instruction.js +9 -0
- package/dist/items/instruction.js.map +1 -0
- package/dist/items/parameters.cjs +53 -0
- package/dist/items/parameters.cjs.map +1 -0
- package/dist/items/parameters.d.ts +5 -0
- package/dist/items/parameters.js +47 -0
- package/dist/items/parameters.js.map +1 -0
- package/dist/items/section.cjs +120 -0
- package/dist/items/section.cjs.map +1 -0
- package/dist/items/section.d.ts +33 -0
- package/dist/items/section.js +115 -0
- package/dist/items/section.js.map +1 -0
- package/dist/items/trait.cjs +13 -0
- package/dist/items/trait.cjs.map +1 -0
- package/dist/items/trait.d.ts +3 -0
- package/dist/items/trait.js +9 -0
- package/dist/items/trait.js.map +1 -0
- package/dist/items/weighted.cjs +27 -0
- package/dist/items/weighted.cjs.map +1 -0
- package/dist/items/weighted.d.ts +24 -0
- package/dist/items/weighted.js +22 -0
- package/dist/items/weighted.js.map +1 -0
- package/dist/loader.cjs +167 -0
- package/dist/loader.cjs.map +1 -0
- package/dist/loader.d.ts +35 -0
- package/dist/loader.js +161 -0
- package/dist/loader.js.map +1 -0
- package/dist/logger.cjs +51 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.ts +11 -0
- package/dist/logger.js +46 -0
- package/dist/logger.js.map +1 -0
- package/dist/override.cjs +109 -0
- package/dist/override.cjs.map +1 -0
- package/dist/override.d.ts +31 -0
- package/dist/override.js +105 -0
- package/dist/override.js.map +1 -0
- package/dist/parse/markdown.cjs +114 -0
- package/dist/parse/markdown.cjs.map +1 -0
- package/dist/parse/markdown.d.ts +3 -0
- package/dist/parse/markdown.js +110 -0
- package/dist/parse/markdown.js.map +1 -0
- package/dist/parse/text.cjs +33 -0
- package/dist/parse/text.cjs.map +1 -0
- package/dist/parse/text.d.ts +3 -0
- package/dist/parse/text.js +29 -0
- package/dist/parse/text.js.map +1 -0
- package/dist/parser.cjs +99 -0
- package/dist/parser.cjs.map +1 -0
- package/dist/parser.d.ts +21 -0
- package/dist/parser.js +75 -0
- package/dist/parser.js.map +1 -0
- package/dist/prompt.cjs +15 -0
- package/dist/prompt.cjs.map +1 -0
- package/dist/prompt.d.ts +16 -0
- package/dist/prompt.js +11 -0
- package/dist/prompt.js.map +1 -0
- package/dist/riotprompt.cjs +1359 -0
- package/dist/riotprompt.cjs.map +1 -0
- package/dist/riotprompt.d.ts +25 -0
- package/dist/riotprompt.js +21 -0
- package/dist/riotprompt.js.map +1 -0
- package/dist/util/general.cjs +52 -0
- package/dist/util/general.cjs.map +1 -0
- package/dist/util/general.d.ts +4 -0
- package/dist/util/general.js +47 -0
- package/dist/util/general.js.map +1 -0
- package/dist/util/markdown.cjs +115 -0
- package/dist/util/markdown.cjs.map +1 -0
- package/dist/util/markdown.d.ts +7 -0
- package/dist/util/markdown.js +111 -0
- package/dist/util/markdown.js.map +1 -0
- package/dist/util/storage.cjs +155 -0
- package/dist/util/storage.cjs.map +1 -0
- package/dist/util/storage.d.ts +32 -0
- package/dist/util/storage.js +132 -0
- package/dist/util/storage.js.map +1 -0
- package/dist/util/text.cjs +42 -0
- package/dist/util/text.cjs.map +1 -0
- package/dist/util/text.d.ts +1 -0
- package/dist/util/text.js +38 -0
- package/dist/util/text.js.map +1 -0
- package/docs/loader.md +237 -0
- package/docs/override.md +323 -0
- package/docs/parser.md +130 -0
- package/eslint.config.mjs +82 -0
- package/nodemon.json +14 -0
- package/package.json +72 -0
- package/vite.config.ts +114 -0
- package/vitest.config.ts +25 -0
|
@@ -0,0 +1,1359 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const zod = require('zod');
|
|
6
|
+
const fs = require('fs/promises');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const marked = require('marked');
|
|
9
|
+
const fs$1 = require('fs');
|
|
10
|
+
const glob = require('glob');
|
|
11
|
+
const crypto = require('crypto');
|
|
12
|
+
|
|
13
|
+
function _interopNamespaceDefault(e) {
|
|
14
|
+
const n = Object.create(null, { [Symbol.toStringTag]: { value: 'Module' } });
|
|
15
|
+
if (e) {
|
|
16
|
+
for (const k in e) {
|
|
17
|
+
if (k !== 'default') {
|
|
18
|
+
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
19
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: () => e[k]
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
n.default = e;
|
|
27
|
+
return Object.freeze(n);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
|
|
31
|
+
const path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
|
|
32
|
+
const fs__namespace$1 = /*#__PURE__*/_interopNamespaceDefault(fs$1);
|
|
33
|
+
|
|
34
|
+
const parameters.ParametersSchema = zod.z.record(zod.z.string(), zod.z.union([
|
|
35
|
+
zod.z.string(),
|
|
36
|
+
zod.z.number(),
|
|
37
|
+
zod.z.boolean(),
|
|
38
|
+
zod.z.array(zod.z.union([
|
|
39
|
+
zod.z.string(),
|
|
40
|
+
zod.z.number(),
|
|
41
|
+
zod.z.boolean()
|
|
42
|
+
]))
|
|
43
|
+
]));
|
|
44
|
+
const create$9 = (parameters)=>{
|
|
45
|
+
return parameters;
|
|
46
|
+
};
|
|
47
|
+
const apply = (text, parameters)=>{
|
|
48
|
+
if (!parameters) {
|
|
49
|
+
return text;
|
|
50
|
+
}
|
|
51
|
+
// First, trim parameters keys to handle whitespace in placeholder names
|
|
52
|
+
const trimmedParams = {};
|
|
53
|
+
Object.keys(parameters).forEach((key)=>{
|
|
54
|
+
trimmedParams[key.trim()] = parameters[key];
|
|
55
|
+
});
|
|
56
|
+
// Process all placeholders, preserving ones that don't have matching parameters
|
|
57
|
+
return text.replace(/\{\{([^{}]+)\}\}/g, (match, p1)=>{
|
|
58
|
+
const paramKey = p1.trim();
|
|
59
|
+
const parameter = trimmedParams[paramKey];
|
|
60
|
+
if (parameter === undefined) {
|
|
61
|
+
// Preserve the original placeholder if parameter doesn't exist
|
|
62
|
+
return match;
|
|
63
|
+
} else if (typeof parameter === 'string') {
|
|
64
|
+
return parameter;
|
|
65
|
+
} else if (typeof parameter === 'number') {
|
|
66
|
+
return parameter.toString();
|
|
67
|
+
} else if (typeof parameter === 'boolean') {
|
|
68
|
+
return parameter.toString();
|
|
69
|
+
} else if (Array.isArray(parameter)) {
|
|
70
|
+
return parameter.join(', ');
|
|
71
|
+
} else {
|
|
72
|
+
return match;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
zod.z.object({
|
|
78
|
+
text: zod.z.string(),
|
|
79
|
+
weight: zod.z.number().optional()
|
|
80
|
+
});
|
|
81
|
+
const weighted.WeightedOptionsSchema = zod.z.object({
|
|
82
|
+
weight: zod.z.number().optional(),
|
|
83
|
+
parameters: parameters.ParametersSchema.optional()
|
|
84
|
+
});
|
|
85
|
+
const create$8 = (text, options = {})=>{
|
|
86
|
+
const weightedOptions = weighted.WeightedOptionsSchema.parse(options);
|
|
87
|
+
const parameterizedText = apply(text, weightedOptions.parameters);
|
|
88
|
+
return {
|
|
89
|
+
text: parameterizedText,
|
|
90
|
+
weight: weightedOptions.weight
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// Export create function
|
|
95
|
+
const create$7 = (text, options = {})=>{
|
|
96
|
+
const weightedOptions = weighted.WeightedOptionsSchema.parse(options);
|
|
97
|
+
return create$8(text, weightedOptions);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const create$6 = (text, options = {})=>{
|
|
101
|
+
const weightedOptions = weighted.WeightedOptionsSchema.parse(options);
|
|
102
|
+
return create$8(text, weightedOptions);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const create$5 = (text, options = {})=>{
|
|
106
|
+
const weightedOptions = weighted.WeightedOptionsSchema.parse(options);
|
|
107
|
+
return create$8(text, weightedOptions);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const section.SectionOptionsSchema = zod.z.object({
|
|
111
|
+
title: zod.z.string().optional(),
|
|
112
|
+
weight: zod.z.number().optional(),
|
|
113
|
+
itemWeight: zod.z.number().optional(),
|
|
114
|
+
parameters: parameters.ParametersSchema.optional().default({})
|
|
115
|
+
});
|
|
116
|
+
const create$4 = (options = {})=>{
|
|
117
|
+
const items = [];
|
|
118
|
+
const sectionOptions = section.SectionOptionsSchema.parse(options);
|
|
119
|
+
const sectionItemOptions = weighted.WeightedOptionsSchema.parse({
|
|
120
|
+
...sectionOptions,
|
|
121
|
+
weight: sectionOptions.itemWeight
|
|
122
|
+
});
|
|
123
|
+
const append = (item, options = {})=>{
|
|
124
|
+
let itemOptions = weighted.WeightedOptionsSchema.parse(options);
|
|
125
|
+
itemOptions = {
|
|
126
|
+
...sectionItemOptions,
|
|
127
|
+
...itemOptions
|
|
128
|
+
};
|
|
129
|
+
if (Array.isArray(item)) {
|
|
130
|
+
item.forEach((item)=>{
|
|
131
|
+
append(item);
|
|
132
|
+
});
|
|
133
|
+
} else {
|
|
134
|
+
if (typeof item === 'string') {
|
|
135
|
+
items.push(create$8(item, itemOptions));
|
|
136
|
+
} else {
|
|
137
|
+
items.push(item);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return section$1;
|
|
141
|
+
};
|
|
142
|
+
const prepend = (item, options = {})=>{
|
|
143
|
+
let itemOptions = weighted.WeightedOptionsSchema.parse(options);
|
|
144
|
+
itemOptions = {
|
|
145
|
+
...sectionItemOptions,
|
|
146
|
+
...itemOptions
|
|
147
|
+
};
|
|
148
|
+
if (Array.isArray(item)) {
|
|
149
|
+
item.forEach((item)=>{
|
|
150
|
+
prepend(item);
|
|
151
|
+
});
|
|
152
|
+
} else {
|
|
153
|
+
if (typeof item === 'string') {
|
|
154
|
+
items.unshift(create$8(item, itemOptions));
|
|
155
|
+
} else {
|
|
156
|
+
items.unshift(item);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return section$1;
|
|
160
|
+
};
|
|
161
|
+
const insert = (index, item, options = {})=>{
|
|
162
|
+
let itemOptions = weighted.WeightedOptionsSchema.parse(options);
|
|
163
|
+
itemOptions = {
|
|
164
|
+
...sectionItemOptions,
|
|
165
|
+
...itemOptions
|
|
166
|
+
};
|
|
167
|
+
if (Array.isArray(item)) {
|
|
168
|
+
item.forEach((item)=>{
|
|
169
|
+
insert(index, item);
|
|
170
|
+
});
|
|
171
|
+
} else {
|
|
172
|
+
if (typeof item === 'string') {
|
|
173
|
+
items.splice(index, 0, create$8(item, itemOptions));
|
|
174
|
+
} else {
|
|
175
|
+
items.splice(index, 0, item);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return section$1;
|
|
179
|
+
};
|
|
180
|
+
const remove = (index)=>{
|
|
181
|
+
items.splice(index, 1);
|
|
182
|
+
return section$1;
|
|
183
|
+
};
|
|
184
|
+
const replace = (index, item, options = {})=>{
|
|
185
|
+
let itemOptions = weighted.WeightedOptionsSchema.parse(options);
|
|
186
|
+
itemOptions = {
|
|
187
|
+
...sectionItemOptions,
|
|
188
|
+
...itemOptions
|
|
189
|
+
};
|
|
190
|
+
if (typeof item === 'string') {
|
|
191
|
+
items[index] = create$8(item, itemOptions);
|
|
192
|
+
} else {
|
|
193
|
+
items[index] = item;
|
|
194
|
+
}
|
|
195
|
+
return section$1;
|
|
196
|
+
};
|
|
197
|
+
const add = (item, options = {})=>{
|
|
198
|
+
let itemOptions = weighted.WeightedOptionsSchema.parse(options);
|
|
199
|
+
itemOptions = {
|
|
200
|
+
...sectionItemOptions,
|
|
201
|
+
...itemOptions
|
|
202
|
+
};
|
|
203
|
+
return append(item, itemOptions);
|
|
204
|
+
};
|
|
205
|
+
const section$1 = {
|
|
206
|
+
title: sectionOptions.title,
|
|
207
|
+
items,
|
|
208
|
+
weight: sectionOptions.weight,
|
|
209
|
+
add,
|
|
210
|
+
append,
|
|
211
|
+
prepend,
|
|
212
|
+
insert,
|
|
213
|
+
remove,
|
|
214
|
+
replace
|
|
215
|
+
};
|
|
216
|
+
return section$1;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const create$3 = (text, options = {})=>{
|
|
220
|
+
const weightedOptions = weighted.WeightedOptionsSchema.parse(options);
|
|
221
|
+
return create$8(text, weightedOptions);
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const create$2 = ({ persona, instructions, contents, contexts })=>{
|
|
225
|
+
return {
|
|
226
|
+
persona,
|
|
227
|
+
instructions,
|
|
228
|
+
contents,
|
|
229
|
+
contexts
|
|
230
|
+
};
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const constants.DEFAULT_CHARACTER_ENCODING = "utf8";
|
|
234
|
+
const constants.LIBRARY_NAME = "riotprompt";
|
|
235
|
+
const DEFAULT_PERSONA_ROLE = "developer";
|
|
236
|
+
const DEFAULT_IGNORE_PATTERNS = [
|
|
237
|
+
"^\\..*",
|
|
238
|
+
"\\.(jpg|jpeg|png|gif|bmp|svg|webp|ico)$",
|
|
239
|
+
"\\.(mp3|wav|ogg|aac|flac)$",
|
|
240
|
+
"\\.(mp4|mov|avi|mkv|webm)$",
|
|
241
|
+
"\\.(pdf|doc|docx|xls|xlsx|ppt|pptx)$",
|
|
242
|
+
"\\.(zip|tar|gz|rar|7z)$" // Compressed files
|
|
243
|
+
];
|
|
244
|
+
const DEFAULT_SECTION_SEPARATOR = "tag";
|
|
245
|
+
const DEFAULT_SECTION_INDENTATION = true;
|
|
246
|
+
const DEFAULT_SECTION_TITLE_PROPERTY = "title";
|
|
247
|
+
const DEFAULT_FORMAT_OPTIONS = {
|
|
248
|
+
sectionSeparator: DEFAULT_SECTION_SEPARATOR,
|
|
249
|
+
sectionIndentation: DEFAULT_SECTION_INDENTATION,
|
|
250
|
+
sectionTitleProperty: DEFAULT_SECTION_TITLE_PROPERTY,
|
|
251
|
+
sectionDepth: 0
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
const getPersonaRole = (model)=>{
|
|
255
|
+
if (model === "gpt-4o" || model === "gpt-4o-mini") {
|
|
256
|
+
return "system";
|
|
257
|
+
}
|
|
258
|
+
return DEFAULT_PERSONA_ROLE;
|
|
259
|
+
};
|
|
260
|
+
const createRequest = (model)=>{
|
|
261
|
+
const messages = [];
|
|
262
|
+
return {
|
|
263
|
+
model,
|
|
264
|
+
messages,
|
|
265
|
+
addMessage: (message)=>{
|
|
266
|
+
messages.push(message);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
const chat = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
|
|
272
|
+
__proto__: null,
|
|
273
|
+
createRequest,
|
|
274
|
+
getPersonaRole
|
|
275
|
+
}, Symbol.toStringTag, { value: 'Module' }));
|
|
276
|
+
|
|
277
|
+
const DEFAULT_LOGGER = {
|
|
278
|
+
name: 'default',
|
|
279
|
+
debug: (message, ...args)=>console.debug(message, ...args),
|
|
280
|
+
info: (message, ...args)=>console.info(message, ...args),
|
|
281
|
+
warn: (message, ...args)=>console.warn(message, ...args),
|
|
282
|
+
error: (message, ...args)=>console.error(message, ...args),
|
|
283
|
+
verbose: (message, ...args)=>console.log(message, ...args),
|
|
284
|
+
silly: (message, ...args)=>console.log(message, ...args)
|
|
285
|
+
};
|
|
286
|
+
const wrapLogger = (toWrap, name)=>{
|
|
287
|
+
const requiredMethods = [
|
|
288
|
+
'debug',
|
|
289
|
+
'info',
|
|
290
|
+
'warn',
|
|
291
|
+
'error',
|
|
292
|
+
'verbose',
|
|
293
|
+
'silly'
|
|
294
|
+
];
|
|
295
|
+
const missingMethods = requiredMethods.filter((method)=>typeof toWrap[method] !== 'function');
|
|
296
|
+
if (missingMethods.length > 0) {
|
|
297
|
+
throw new Error(`Logger is missing required methods: ${missingMethods.join(', ')}`);
|
|
298
|
+
}
|
|
299
|
+
const log = (level, message, ...args)=>{
|
|
300
|
+
message = `[${constants.LIBRARY_NAME}] ${name ? `[${name}]` : ''}: ${message}`;
|
|
301
|
+
if (level === 'debug') toWrap.debug(message, ...args);
|
|
302
|
+
else if (level === 'info') toWrap.info(message, ...args);
|
|
303
|
+
else if (level === 'warn') toWrap.warn(message, ...args);
|
|
304
|
+
else if (level === 'error') toWrap.error(message, ...args);
|
|
305
|
+
else if (level === 'verbose') toWrap.verbose(message, ...args);
|
|
306
|
+
else if (level === 'silly') toWrap.silly(message, ...args);
|
|
307
|
+
};
|
|
308
|
+
return {
|
|
309
|
+
name: 'wrapped',
|
|
310
|
+
debug: (message, ...args)=>log('debug', message, ...args),
|
|
311
|
+
info: (message, ...args)=>log('info', message, ...args),
|
|
312
|
+
warn: (message, ...args)=>log('warn', message, ...args),
|
|
313
|
+
error: (message, ...args)=>log('error', message, ...args),
|
|
314
|
+
verbose: (message, ...args)=>log('verbose', message, ...args),
|
|
315
|
+
silly: (message, ...args)=>log('silly', message, ...args)
|
|
316
|
+
};
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
const clean = (obj)=>{
|
|
320
|
+
return Object.fromEntries(Object.entries(obj).filter(([_, v])=>v !== undefined));
|
|
321
|
+
};
|
|
322
|
+
//Recursive implementation of jSON.stringify;
|
|
323
|
+
const stringifyJSON = function(obj, visited = new Set()) {
|
|
324
|
+
const arrOfKeyVals = [];
|
|
325
|
+
const arrVals = [];
|
|
326
|
+
let objKeys = [];
|
|
327
|
+
/*********CHECK FOR PRIMITIVE TYPES**********/ if (typeof obj === 'number' || typeof obj === 'boolean' || obj === null) return '' + obj;
|
|
328
|
+
else if (typeof obj === 'string') return '"' + obj + '"';
|
|
329
|
+
/*********DETECT CIRCULAR REFERENCES**********/ if (obj instanceof Object && visited.has(obj)) {
|
|
330
|
+
return '"(circular)"';
|
|
331
|
+
} else if (Array.isArray(obj)) {
|
|
332
|
+
//check for empty array
|
|
333
|
+
if (obj[0] === undefined) return '[]';
|
|
334
|
+
else {
|
|
335
|
+
// Add array to visited before processing its elements
|
|
336
|
+
visited.add(obj);
|
|
337
|
+
obj.forEach(function(el) {
|
|
338
|
+
arrVals.push(stringifyJSON(el, visited));
|
|
339
|
+
});
|
|
340
|
+
return '[' + arrVals + ']';
|
|
341
|
+
}
|
|
342
|
+
} else if (obj instanceof Object) {
|
|
343
|
+
// Add object to visited before processing its properties
|
|
344
|
+
visited.add(obj);
|
|
345
|
+
//get object keys
|
|
346
|
+
objKeys = Object.keys(obj);
|
|
347
|
+
//set key output;
|
|
348
|
+
objKeys.forEach(function(key) {
|
|
349
|
+
const keyOut = '"' + key + '":';
|
|
350
|
+
const keyValOut = obj[key];
|
|
351
|
+
//skip functions and undefined properties
|
|
352
|
+
if (keyValOut instanceof Function || keyValOut === undefined) return; // Skip this entry entirely instead of pushing an empty string
|
|
353
|
+
else if (typeof keyValOut === 'string') arrOfKeyVals.push(keyOut + '"' + keyValOut + '"');
|
|
354
|
+
else if (typeof keyValOut === 'boolean' || typeof keyValOut === 'number' || keyValOut === null) arrOfKeyVals.push(keyOut + keyValOut);
|
|
355
|
+
else if (keyValOut instanceof Object) {
|
|
356
|
+
arrOfKeyVals.push(keyOut + stringifyJSON(keyValOut, visited));
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
return '{' + arrOfKeyVals + '}';
|
|
360
|
+
}
|
|
361
|
+
return '';
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const SectionSeparatorSchema = zod.z.enum([
|
|
365
|
+
"tag",
|
|
366
|
+
"markdown"
|
|
367
|
+
]);
|
|
368
|
+
const SectionTitlePropertySchema = zod.z.enum([
|
|
369
|
+
"title",
|
|
370
|
+
"name"
|
|
371
|
+
]);
|
|
372
|
+
const FormatOptionsSchema = zod.z.object({
|
|
373
|
+
sectionSeparator: SectionSeparatorSchema,
|
|
374
|
+
sectionIndentation: zod.z.boolean(),
|
|
375
|
+
sectionTitleProperty: SectionTitlePropertySchema,
|
|
376
|
+
sectionTitlePrefix: zod.z.string().optional(),
|
|
377
|
+
sectionTitleSeparator: zod.z.string().optional(),
|
|
378
|
+
sectionDepth: zod.z.number().default(1)
|
|
379
|
+
});
|
|
380
|
+
const OptionSchema$1 = zod.z.object({
|
|
381
|
+
logger: zod.z.any().optional().default(DEFAULT_LOGGER),
|
|
382
|
+
formatOptions: FormatOptionsSchema.partial().optional().default(DEFAULT_FORMAT_OPTIONS)
|
|
383
|
+
});
|
|
384
|
+
// Type guard to check if an object is a Section
|
|
385
|
+
function isSection(obj) {
|
|
386
|
+
return obj && typeof obj === 'object' && 'items' in obj && Array.isArray(obj.items);
|
|
387
|
+
}
|
|
388
|
+
// Type guard to check if an object is a Section
|
|
389
|
+
function isWeighted(obj) {
|
|
390
|
+
return obj && typeof obj === 'object' && 'text' in obj;
|
|
391
|
+
}
|
|
392
|
+
const formatter.create = (formatterOptions)=>{
|
|
393
|
+
const options = OptionSchema$1.parse(formatterOptions || {});
|
|
394
|
+
const logger = wrapLogger(options.logger, 'Formatter');
|
|
395
|
+
let formatOptions = DEFAULT_FORMAT_OPTIONS;
|
|
396
|
+
if (options === null || options === void 0 ? void 0 : options.formatOptions) {
|
|
397
|
+
formatOptions = {
|
|
398
|
+
...formatOptions,
|
|
399
|
+
...clean(options.formatOptions)
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
const formatPersona = (model, persona)=>{
|
|
403
|
+
logger.silly(`Formatting persona`);
|
|
404
|
+
if (persona) {
|
|
405
|
+
const formattedPersona = formatSection(persona);
|
|
406
|
+
return {
|
|
407
|
+
role: getPersonaRole(model),
|
|
408
|
+
content: `${formattedPersona}`
|
|
409
|
+
};
|
|
410
|
+
} else {
|
|
411
|
+
throw new Error("Persona is required");
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
const format = (item, sectionDepth)=>{
|
|
415
|
+
logger.silly(`Formatting ${isSection(item) ? "section" : "item"} Item: %s`, stringifyJSON(item));
|
|
416
|
+
const currentSectionDepth = sectionDepth !== null && sectionDepth !== void 0 ? sectionDepth : formatOptions.sectionDepth;
|
|
417
|
+
logger.silly(`\t\tCurrent section depth: ${currentSectionDepth}`);
|
|
418
|
+
let result = "";
|
|
419
|
+
if (isSection(item)) {
|
|
420
|
+
result = formatSection(item, currentSectionDepth + 1);
|
|
421
|
+
} else if (isWeighted(item)) {
|
|
422
|
+
result = item.text;
|
|
423
|
+
} else {
|
|
424
|
+
//If the item is neither a section nor a weighted item, it is empty.
|
|
425
|
+
result = '';
|
|
426
|
+
}
|
|
427
|
+
return result;
|
|
428
|
+
};
|
|
429
|
+
const formatSection = (section, sectionDepth)=>{
|
|
430
|
+
logger.silly(`Formatting section`);
|
|
431
|
+
const currentSectionDepth = sectionDepth !== null && sectionDepth !== void 0 ? sectionDepth : formatOptions.sectionDepth;
|
|
432
|
+
logger.silly(`\t\tCurrent section depth: ${currentSectionDepth}`);
|
|
433
|
+
if (section) {
|
|
434
|
+
const formattedItems = section.items.map((item)=>format(item, currentSectionDepth)).join("\n\n");
|
|
435
|
+
if (formatOptions.sectionSeparator === "tag") {
|
|
436
|
+
var _section_title, _section_title1;
|
|
437
|
+
return `<${(_section_title = section.title) !== null && _section_title !== void 0 ? _section_title : "section"}>\n${formattedItems}\n</${(_section_title1 = section.title) !== null && _section_title1 !== void 0 ? _section_title1 : "section"}>`;
|
|
438
|
+
} else {
|
|
439
|
+
// Default depth to 1 if not provided, resulting in H2 (##) matching the test case.
|
|
440
|
+
const headingLevel = currentSectionDepth;
|
|
441
|
+
const hashes = '#'.repeat(headingLevel);
|
|
442
|
+
logger.silly(`\t\tHeading level: ${headingLevel}`);
|
|
443
|
+
logger.silly(`\t\tSection title: ${section.title}`);
|
|
444
|
+
return `${hashes} ${formatOptions.sectionTitlePrefix ? `${formatOptions.sectionTitlePrefix} ${formatOptions.sectionTitleSeparator} ` : ""}${section.title}\n\n${formattedItems}`;
|
|
445
|
+
}
|
|
446
|
+
} else {
|
|
447
|
+
return '';
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
// Helper function to format arrays of items or sections
|
|
451
|
+
const formatArray = (items, sectionDepth)=>{
|
|
452
|
+
logger.silly(`Formatting array`);
|
|
453
|
+
const currentSectionDepth = sectionDepth !== null && sectionDepth !== void 0 ? sectionDepth : formatOptions.sectionDepth;
|
|
454
|
+
return items.map((item)=>format(item, currentSectionDepth)).join("\n\n");
|
|
455
|
+
};
|
|
456
|
+
const formatPrompt = (model, prompt)=>{
|
|
457
|
+
logger.silly('Formatting prompt');
|
|
458
|
+
const chatRequest = createRequest(model);
|
|
459
|
+
if (prompt.persona) {
|
|
460
|
+
[
|
|
461
|
+
prompt.persona
|
|
462
|
+
].forEach((persona)=>{
|
|
463
|
+
chatRequest.addMessage(formatPersona(model, persona));
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
let formattedAreas = formatSection(prompt.instructions) + '\n\n';
|
|
467
|
+
if (prompt.contents) {
|
|
468
|
+
formattedAreas += formatSection(prompt.contents) + '\n\n';
|
|
469
|
+
}
|
|
470
|
+
if (prompt.contexts) {
|
|
471
|
+
formattedAreas += formatSection(prompt.contexts) + '\n\n';
|
|
472
|
+
}
|
|
473
|
+
chatRequest.addMessage({
|
|
474
|
+
role: "user",
|
|
475
|
+
content: formattedAreas
|
|
476
|
+
});
|
|
477
|
+
return chatRequest;
|
|
478
|
+
};
|
|
479
|
+
return {
|
|
480
|
+
formatPersona,
|
|
481
|
+
format,
|
|
482
|
+
formatPrompt,
|
|
483
|
+
formatArray
|
|
484
|
+
};
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
const formatter = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
|
|
488
|
+
__proto__: null,
|
|
489
|
+
FormatOptionsSchema,
|
|
490
|
+
OptionSchema: OptionSchema$1,
|
|
491
|
+
SectionSeparatorSchema,
|
|
492
|
+
SectionTitlePropertySchema,
|
|
493
|
+
create: formatter.create
|
|
494
|
+
}, Symbol.toStringTag, { value: 'Module' }));
|
|
495
|
+
|
|
496
|
+
const parseMarkdown = (input, options = {})=>{
|
|
497
|
+
let markdownContent;
|
|
498
|
+
if (typeof input === 'string') {
|
|
499
|
+
markdownContent = input;
|
|
500
|
+
} else {
|
|
501
|
+
markdownContent = input.toString();
|
|
502
|
+
}
|
|
503
|
+
const sectionOptions = section.SectionOptionsSchema.parse(options);
|
|
504
|
+
// Use marked.lexer to get tokens without full parsing/rendering
|
|
505
|
+
const tokens = marked.marked.lexer(markdownContent);
|
|
506
|
+
// Create the main section (with a Title from the options)
|
|
507
|
+
const mainSection = create$4(sectionOptions);
|
|
508
|
+
// Track sections at each depth level
|
|
509
|
+
const sectionStack = [
|
|
510
|
+
mainSection
|
|
511
|
+
];
|
|
512
|
+
// Set if we've seen the first token
|
|
513
|
+
let isFirstToken = true;
|
|
514
|
+
// Set the item options
|
|
515
|
+
const itemOptions = weighted.WeightedOptionsSchema.parse({
|
|
516
|
+
...sectionOptions,
|
|
517
|
+
weight: sectionOptions.itemWeight
|
|
518
|
+
});
|
|
519
|
+
for (const token of tokens){
|
|
520
|
+
switch(token.type){
|
|
521
|
+
case 'heading':
|
|
522
|
+
{
|
|
523
|
+
const depth = token.depth;
|
|
524
|
+
// If this is the first token and it's a heading, use it as the main section title
|
|
525
|
+
if (isFirstToken) {
|
|
526
|
+
mainSection.title = token.text;
|
|
527
|
+
isFirstToken = false;
|
|
528
|
+
break;
|
|
529
|
+
}
|
|
530
|
+
isFirstToken = false;
|
|
531
|
+
// Create a new section with this heading
|
|
532
|
+
const newSection = create$4({
|
|
533
|
+
...sectionOptions,
|
|
534
|
+
title: token.text
|
|
535
|
+
});
|
|
536
|
+
// Ensure the section stack has the right size based on this heading's depth
|
|
537
|
+
// (e.g., a depth-2 heading should be added to the depth-1 section)
|
|
538
|
+
// We need to ensure the stack length is exactly depth, not just less than or equal to depth
|
|
539
|
+
while(sectionStack.length > depth && sectionStack.length > 1){
|
|
540
|
+
sectionStack.pop();
|
|
541
|
+
}
|
|
542
|
+
// Make sure we're at the right level for this heading
|
|
543
|
+
// If we stay at the same heading level (e.g., two h2s in sequence),
|
|
544
|
+
// we need to pop once more to get to the parent level
|
|
545
|
+
if (sectionStack.length === depth && sectionStack.length > 1) {
|
|
546
|
+
sectionStack.pop();
|
|
547
|
+
}
|
|
548
|
+
// Add new section to its parent
|
|
549
|
+
const parentSection = sectionStack[sectionStack.length - 1];
|
|
550
|
+
parentSection.add(newSection, itemOptions);
|
|
551
|
+
// Push this section onto the stack
|
|
552
|
+
sectionStack.push(newSection);
|
|
553
|
+
break;
|
|
554
|
+
}
|
|
555
|
+
case 'paragraph':
|
|
556
|
+
{
|
|
557
|
+
isFirstToken = false;
|
|
558
|
+
const instruction = create$8(token.text, itemOptions);
|
|
559
|
+
const currentSection = sectionStack[sectionStack.length - 1];
|
|
560
|
+
currentSection.add(instruction, itemOptions);
|
|
561
|
+
break;
|
|
562
|
+
}
|
|
563
|
+
case 'list':
|
|
564
|
+
{
|
|
565
|
+
isFirstToken = false;
|
|
566
|
+
// Convert list items to instructions
|
|
567
|
+
const listInstructionContent = token.items.map((item)=>`- ${item.text}`).join('\n');
|
|
568
|
+
const listInstruction = create$8(listInstructionContent, itemOptions);
|
|
569
|
+
const currentSection = sectionStack[sectionStack.length - 1];
|
|
570
|
+
currentSection.add(listInstruction, itemOptions);
|
|
571
|
+
break;
|
|
572
|
+
}
|
|
573
|
+
case 'code':
|
|
574
|
+
{
|
|
575
|
+
isFirstToken = false;
|
|
576
|
+
// Represent code blocks as instructions
|
|
577
|
+
const codeInstruction = create$8(`\`\`\`${token.lang || ''}\n${token.text}\n\`\`\``, itemOptions);
|
|
578
|
+
const currentSection = sectionStack[sectionStack.length - 1];
|
|
579
|
+
currentSection.add(codeInstruction, itemOptions);
|
|
580
|
+
break;
|
|
581
|
+
}
|
|
582
|
+
case 'space':
|
|
583
|
+
break;
|
|
584
|
+
default:
|
|
585
|
+
{
|
|
586
|
+
isFirstToken = false;
|
|
587
|
+
// Treat other block tokens' text as instructions for robustness
|
|
588
|
+
if ('text' in token && token.text) {
|
|
589
|
+
const fallbackInstruction = create$8(token.text, itemOptions);
|
|
590
|
+
const currentSection = sectionStack[sectionStack.length - 1];
|
|
591
|
+
currentSection.add(fallbackInstruction, itemOptions);
|
|
592
|
+
}
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
return mainSection;
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
const parseText = (input, options = {})=>{
|
|
601
|
+
let text;
|
|
602
|
+
if (typeof input === 'string') {
|
|
603
|
+
text = input;
|
|
604
|
+
} else {
|
|
605
|
+
text = input.toString();
|
|
606
|
+
}
|
|
607
|
+
const sectionOptions = section.SectionOptionsSchema.parse(options);
|
|
608
|
+
// Set the item options
|
|
609
|
+
const itemOptions = weighted.WeightedOptionsSchema.parse({
|
|
610
|
+
...sectionOptions,
|
|
611
|
+
weight: sectionOptions.itemWeight
|
|
612
|
+
});
|
|
613
|
+
// Split the text on newlines
|
|
614
|
+
const lines = text.split(/\r?\n/).filter((line)=>line.trim().length > 0);
|
|
615
|
+
// Create the main section with the supplied title
|
|
616
|
+
const mainSection = create$4(sectionOptions);
|
|
617
|
+
for (const line of lines){
|
|
618
|
+
const instruction = create$8(line, itemOptions);
|
|
619
|
+
mainSection.add(instruction, itemOptions);
|
|
620
|
+
}
|
|
621
|
+
return mainSection;
|
|
622
|
+
};
|
|
623
|
+
|
|
624
|
+
// Heuristic to check for Markdown syntax. This is not a full parser.
|
|
625
|
+
// It looks for common Markdown patterns.
|
|
626
|
+
const markdownRegex = /^(#+\s|\*\s|-\s|\+\s|>\s|\[.*\]\(.*\)|```|~~~|---\\s*$)/m;
|
|
627
|
+
/**
|
|
628
|
+
* Inspects a string to see if it likely contains Markdown syntax.
|
|
629
|
+
*
|
|
630
|
+
* @param input The string or Buffer content to inspect.
|
|
631
|
+
* @returns True if Markdown syntax is suspected, false otherwise.
|
|
632
|
+
*/ function isMarkdown(input) {
|
|
633
|
+
if (input == null) {
|
|
634
|
+
return false;
|
|
635
|
+
}
|
|
636
|
+
// Convert Buffer to string if necessary
|
|
637
|
+
const content = typeof input === 'string' ? input : input.toString(constants.DEFAULT_CHARACTER_ENCODING);
|
|
638
|
+
if (!content || content.trim() === '') {
|
|
639
|
+
return false; // Empty string is not considered Markdown
|
|
640
|
+
}
|
|
641
|
+
// Check for common Markdown patterns in the entire content
|
|
642
|
+
if (markdownRegex.test(content)) {
|
|
643
|
+
return true;
|
|
644
|
+
}
|
|
645
|
+
// Fallback: Check for a high prevalence of Markdown-like list/header starters
|
|
646
|
+
// or thematic breaks, or code blocks.
|
|
647
|
+
// We'll consider up to the first ~2000 characters, roughly equivalent to the byte check.
|
|
648
|
+
const effectiveContent = content.length > 2000 ? content.substring(0, 2000) : content;
|
|
649
|
+
const lines = effectiveContent.split('\n');
|
|
650
|
+
let markdownFeatureCount = 0;
|
|
651
|
+
const featurePatterns = [
|
|
652
|
+
/^#+\s+.+/,
|
|
653
|
+
/^\s*[*+-]\s+.+/,
|
|
654
|
+
/^\s*>\s+.+/,
|
|
655
|
+
/\[.+\]\(.+\)/,
|
|
656
|
+
/!\[.+\]\(.+\)/,
|
|
657
|
+
/`{1,3}[^`]+`{1,3}/,
|
|
658
|
+
/^\s*_{3,}\s*$/,
|
|
659
|
+
/^\s*-{3,}\s*$/,
|
|
660
|
+
/^\s*\*{3,}\s*$/
|
|
661
|
+
];
|
|
662
|
+
for (const line of lines){
|
|
663
|
+
// Stop checking if we have already found enough features to be confident.
|
|
664
|
+
// This is a small optimization for very long inputs that are clearly markdown early on.
|
|
665
|
+
if (markdownFeatureCount >= 2 && lines.length > 10) {
|
|
666
|
+
const significantLineCountEarly = Math.min(lines.indexOf(line) + 1, 20);
|
|
667
|
+
if (significantLineCountEarly > 0 && markdownFeatureCount / significantLineCountEarly > 0.1) {
|
|
668
|
+
return true;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
for (const pattern of featurePatterns){
|
|
672
|
+
if (pattern.test(line.trim())) {
|
|
673
|
+
markdownFeatureCount++;
|
|
674
|
+
break; // Count each line only once
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
// If more than 5% of the first few lines (up to 20 lines or all lines if fewer)
|
|
679
|
+
// show markdown features, or if there are at least 2 distinct features in short texts,
|
|
680
|
+
// consider it Markdown.
|
|
681
|
+
const significantLineCount = Math.min(lines.length, 20);
|
|
682
|
+
if (significantLineCount > 0) {
|
|
683
|
+
// Calculate the exact threshold percentage
|
|
684
|
+
const thresholdPercentage = markdownFeatureCount / significantLineCount;
|
|
685
|
+
// Check against the 5% threshold (0.05)
|
|
686
|
+
// Using >= 0.05 exactly matches 5%, > 0.05 requires more than 5%
|
|
687
|
+
if (thresholdPercentage >= 0.05 + 0.0001) {
|
|
688
|
+
return true;
|
|
689
|
+
}
|
|
690
|
+
// Other conditions for returning true
|
|
691
|
+
if (markdownFeatureCount >= 1 && significantLineCount <= 5 || markdownFeatureCount >= 2) {
|
|
692
|
+
return true;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
return false;
|
|
696
|
+
} // Example usage (optional, for testing):
|
|
697
|
+
// function testIsMarkdownString() {
|
|
698
|
+
// console.log('--- Testing isMarkdownString ---');
|
|
699
|
+
// const markdown1 = '# Hello World\\nThis is a test.';
|
|
700
|
+
// console.log(`Test 1 (Header): "${markdown1.substring(0,10)}..." -> ${isMarkdownString(markdown1)}`); // true
|
|
701
|
+
// const markdown2 = '* Item 1\\n* Item 2';
|
|
702
|
+
// console.log(`Test 2 (List): "${markdown2.substring(0,10)}..." -> ${isMarkdownString(markdown2)}`); // true
|
|
703
|
+
// const markdown3 = '[Google](https://google.com)';
|
|
704
|
+
// console.log(`Test 3 (Link): "${markdown3.substring(0,15)}..." -> ${isMarkdownString(markdown3)}`); // true
|
|
705
|
+
// const markdown4 = '> This is a quote.';
|
|
706
|
+
// console.log(`Test 4 (Blockquote): "${markdown4.substring(0,10)}..." -> ${isMarkdownString(markdown4)}`); // true
|
|
707
|
+
// const markdown5 = '```javascript\\nconsole.log("hello");\\n```';
|
|
708
|
+
// console.log(`Test 5 (Code block): "${markdown5.substring(0,15)}..." -> ${isMarkdownString(markdown5)}`); // true
|
|
709
|
+
// const text1 = 'This is a plain text string.';
|
|
710
|
+
// console.log(`Test 6 (Plain text): "${text1.substring(0,10)}..." -> ${isMarkdownString(text1)}`); // false
|
|
711
|
+
// const text2 = 'hello_world.this_is_a_test_string_with_underscores_but_not_markdown_thematic_break';
|
|
712
|
+
// console.log(`Test 7 (Long non-markdown): "${text2.substring(0,10)}..." -> ${isMarkdownString(text2)}`); // false
|
|
713
|
+
// const text3 = '<xml><tag>value</tag></xml>';
|
|
714
|
+
// console.log(`Test 8 (XML): "${text3.substring(0,10)}..." -> ${isMarkdownString(text3)}`); // false
|
|
715
|
+
// const shortMarkdown = '# H';
|
|
716
|
+
// console.log(`Test 9 (Short Markdown): "${shortMarkdown}" -> ${isMarkdownString(shortMarkdown)}`); // true
|
|
717
|
+
// const shortNonMarkdown = 'Hello';
|
|
718
|
+
// console.log(`Test 10 (Short Non-Markdown): "${shortNonMarkdown}" -> ${isMarkdownString(shortNonMarkdown)}`); // false
|
|
719
|
+
// const emptyString = '';
|
|
720
|
+
// console.log(`Test 11 (Empty string): "" -> ${isMarkdownString(emptyString)}`); // false
|
|
721
|
+
// const whitespaceString = ' \t \n ';
|
|
722
|
+
// console.log(`Test 12 (Whitespace string): "${whitespaceString.substring(0,5)}..." -> ${isMarkdownString(whitespaceString)}`); // false
|
|
723
|
+
// const markdownWithManyFeatures = `# Title\\n\\n* list\\n* list2\\n\\n> quote here\\n\\n\`\`\`\\ncode\\n\`\`\`\\n\\nnormal text paragraph with a [link](url).\n---\nAnother paragraph.\nThis is just a test string to see how it performs with multiple markdown features present.\nHello world this is a very long line that does not contain any markdown syntax at all, it is just plain text that goes on and on.\n* Another list item\n* And another one\n# Another Header\n## Subheader\nThis is fun.\nOkay I think this is enough.\nFinal line.\nAnother final line.\nOne more for good measure.\nOkay that should be enough lines to test the early exit.\n`;
|
|
724
|
+
// console.log(`Test 13 (Many Features): "${markdownWithManyFeatures.substring(0,10)}..." -> ${isMarkdownString(markdownWithManyFeatures)}`); // true
|
|
725
|
+
// const htmlLike = '<div><p>Hello</p><ul><li>item</li></ul></div>';
|
|
726
|
+
// console.log(`Test 14 (HTML-like): "${htmlLike.substring(0,10)}..." -> ${isMarkdownString(htmlLike)}`); // false
|
|
727
|
+
// console.log('--- End Testing ---');
|
|
728
|
+
// }
|
|
729
|
+
// testIsMarkdownString();
|
|
730
|
+
|
|
731
|
+
// Returns true if the input is likely text, false if likely binary
|
|
732
|
+
function isText(input) {
|
|
733
|
+
let buf;
|
|
734
|
+
if (typeof input === 'string') {
|
|
735
|
+
buf = Buffer.from(input, constants.DEFAULT_CHARACTER_ENCODING);
|
|
736
|
+
} else {
|
|
737
|
+
buf = input;
|
|
738
|
+
}
|
|
739
|
+
// Empty buffers are considered text
|
|
740
|
+
if (buf.length === 0) {
|
|
741
|
+
return true;
|
|
742
|
+
}
|
|
743
|
+
// If the buffer contains null bytes, it's likely binary
|
|
744
|
+
if (buf.includes(0)) {
|
|
745
|
+
return false;
|
|
746
|
+
}
|
|
747
|
+
// For UTF-8 encoded text (including emoji and international characters),
|
|
748
|
+
// convert to string first and check if there are non-printable characters
|
|
749
|
+
const str = buf.toString(constants.DEFAULT_CHARACTER_ENCODING);
|
|
750
|
+
// Count the number of non-printable ASCII characters (excluding common whitespace)
|
|
751
|
+
let nonPrintable = 0;
|
|
752
|
+
const len = Math.min(str.length, 512); // Only check the first 512 characters for performance
|
|
753
|
+
for(let i = 0; i < len; i++){
|
|
754
|
+
const charCode = str.charCodeAt(i);
|
|
755
|
+
// Allow: tab (9), line feed (10), carriage return (13), printable ASCII (32-126)
|
|
756
|
+
// Also allow all non-ASCII Unicode characters (charCode > 127)
|
|
757
|
+
if (charCode !== 9 && charCode !== 10 && charCode !== 13 && (charCode < 32 || charCode > 126 && charCode < 128)) {
|
|
758
|
+
nonPrintable++;
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
// If more than 10% of the checked characters are non-printable, consider it binary
|
|
762
|
+
return nonPrintable / len < 0.1;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
const OptionsSchema$2 = zod.z.object({
|
|
766
|
+
logger: zod.z.any().optional().default(DEFAULT_LOGGER),
|
|
767
|
+
parameters: parameters.ParametersSchema.optional().default({})
|
|
768
|
+
});
|
|
769
|
+
const parser.create = (parserOptions)=>{
|
|
770
|
+
const options = OptionsSchema$2.parse(parserOptions || {});
|
|
771
|
+
const parameters = options.parameters;
|
|
772
|
+
const logger = wrapLogger(options.logger, 'Parser');
|
|
773
|
+
const loadOptions = (sectionOptions = {})=>{
|
|
774
|
+
const currentOptions = section.SectionOptionsSchema.parse(sectionOptions);
|
|
775
|
+
return {
|
|
776
|
+
...currentOptions,
|
|
777
|
+
parameters: {
|
|
778
|
+
...parameters,
|
|
779
|
+
...currentOptions.parameters
|
|
780
|
+
}
|
|
781
|
+
};
|
|
782
|
+
};
|
|
783
|
+
const parseFile = async (filePath, options = {})=>{
|
|
784
|
+
const currentOptions = loadOptions(options);
|
|
785
|
+
try {
|
|
786
|
+
const content = await fs__namespace.readFile(filePath, 'utf-8');
|
|
787
|
+
// Only use the filename as title if no title was explicitly provided
|
|
788
|
+
const fileName = path__namespace.basename(filePath, path__namespace.extname(filePath));
|
|
789
|
+
return parse(content, {
|
|
790
|
+
...currentOptions,
|
|
791
|
+
title: (currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.title) || fileName
|
|
792
|
+
});
|
|
793
|
+
} catch (error) {
|
|
794
|
+
// Log the error or handle it appropriately
|
|
795
|
+
logger.error(`Error reading or parsing file with marked at ${filePath}:`, error);
|
|
796
|
+
throw new Error(`Failed to parse instructions from ${filePath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
797
|
+
}
|
|
798
|
+
};
|
|
799
|
+
/**
|
|
800
|
+
* Reads Markdown content and parses it into a single Section.
|
|
801
|
+
*
|
|
802
|
+
* - If the content starts with a heading, that becomes the title of the returned Section
|
|
803
|
+
* - If no heading at the start, creates a Section with no title
|
|
804
|
+
* - Headers within the content create nested sections based on their depth
|
|
805
|
+
* - All content is organized in a hierarchical structure based on heading levels
|
|
806
|
+
*
|
|
807
|
+
* @param content The content to parse
|
|
808
|
+
* @returns A Section containing all content in a hierarchical structure
|
|
809
|
+
*/ const parse = (content, options = {})=>{
|
|
810
|
+
const currentOptions = loadOptions(options);
|
|
811
|
+
let mainSection;
|
|
812
|
+
if (isMarkdown(content)) {
|
|
813
|
+
mainSection = parseMarkdown(content, currentOptions);
|
|
814
|
+
} else if (isText(content)) {
|
|
815
|
+
mainSection = parseText(content, currentOptions);
|
|
816
|
+
} else {
|
|
817
|
+
throw new Error(`Unsupported content supplied to parse, riotprompt currently only supports markdown and text`);
|
|
818
|
+
}
|
|
819
|
+
return mainSection;
|
|
820
|
+
};
|
|
821
|
+
return {
|
|
822
|
+
parse,
|
|
823
|
+
parseFile
|
|
824
|
+
};
|
|
825
|
+
};
|
|
826
|
+
|
|
827
|
+
const parser = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
|
|
828
|
+
__proto__: null,
|
|
829
|
+
create: parser.create
|
|
830
|
+
}, Symbol.toStringTag, { value: 'Module' }));
|
|
831
|
+
|
|
832
|
+
// eslint-disable-next-line no-restricted-imports
|
|
833
|
+
const create$1 = (params)=>{
|
|
834
|
+
// eslint-disable-next-line no-console
|
|
835
|
+
const log = params.log || console.log;
|
|
836
|
+
const exists = async (path)=>{
|
|
837
|
+
try {
|
|
838
|
+
await fs__namespace$1.promises.stat(path);
|
|
839
|
+
return true;
|
|
840
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
841
|
+
} catch (error) {
|
|
842
|
+
return false;
|
|
843
|
+
}
|
|
844
|
+
};
|
|
845
|
+
const isDirectory = async (path)=>{
|
|
846
|
+
const stats = await fs__namespace$1.promises.stat(path);
|
|
847
|
+
if (!stats.isDirectory()) {
|
|
848
|
+
log(`${path} is not a directory`);
|
|
849
|
+
return false;
|
|
850
|
+
}
|
|
851
|
+
return true;
|
|
852
|
+
};
|
|
853
|
+
const isFile = async (path)=>{
|
|
854
|
+
const stats = await fs__namespace$1.promises.stat(path);
|
|
855
|
+
if (!stats.isFile()) {
|
|
856
|
+
log(`${path} is not a file`);
|
|
857
|
+
return false;
|
|
858
|
+
}
|
|
859
|
+
return true;
|
|
860
|
+
};
|
|
861
|
+
const isReadable = async (path)=>{
|
|
862
|
+
try {
|
|
863
|
+
await fs__namespace$1.promises.access(path, fs__namespace$1.constants.R_OK);
|
|
864
|
+
} catch (error) {
|
|
865
|
+
log(`${path} is not readable: %s %s`, error.message, error.stack);
|
|
866
|
+
return false;
|
|
867
|
+
}
|
|
868
|
+
return true;
|
|
869
|
+
};
|
|
870
|
+
const isWritable = async (path)=>{
|
|
871
|
+
try {
|
|
872
|
+
await fs__namespace$1.promises.access(path, fs__namespace$1.constants.W_OK);
|
|
873
|
+
} catch (error) {
|
|
874
|
+
log(`${path} is not writable: %s %s`, error.message, error.stack);
|
|
875
|
+
return false;
|
|
876
|
+
}
|
|
877
|
+
return true;
|
|
878
|
+
};
|
|
879
|
+
const isFileReadable = async (path)=>{
|
|
880
|
+
return await exists(path) && await isFile(path) && await isReadable(path);
|
|
881
|
+
};
|
|
882
|
+
const isDirectoryWritable = async (path)=>{
|
|
883
|
+
return await exists(path) && await isDirectory(path) && await isWritable(path);
|
|
884
|
+
};
|
|
885
|
+
const isDirectoryReadable = async (path)=>{
|
|
886
|
+
return await exists(path) && await isDirectory(path) && await isReadable(path);
|
|
887
|
+
};
|
|
888
|
+
const createDirectory = async (path)=>{
|
|
889
|
+
try {
|
|
890
|
+
await fs__namespace$1.promises.mkdir(path, {
|
|
891
|
+
recursive: true
|
|
892
|
+
});
|
|
893
|
+
} catch (mkdirError) {
|
|
894
|
+
throw new Error(`Failed to create output directory ${path}: ${mkdirError.message} ${mkdirError.stack}`);
|
|
895
|
+
}
|
|
896
|
+
};
|
|
897
|
+
const readFile = async (path, encoding)=>{
|
|
898
|
+
return await fs__namespace$1.promises.readFile(path, {
|
|
899
|
+
encoding: encoding
|
|
900
|
+
});
|
|
901
|
+
};
|
|
902
|
+
const writeFile = async (path, data, encoding)=>{
|
|
903
|
+
await fs__namespace$1.promises.writeFile(path, data, {
|
|
904
|
+
encoding: encoding
|
|
905
|
+
});
|
|
906
|
+
};
|
|
907
|
+
const forEachFileIn = async (directory, callback, options = {
|
|
908
|
+
pattern: '*.*'
|
|
909
|
+
})=>{
|
|
910
|
+
try {
|
|
911
|
+
let filesProcessed = 0;
|
|
912
|
+
const files = await glob.glob(options.pattern, {
|
|
913
|
+
cwd: directory,
|
|
914
|
+
nodir: true
|
|
915
|
+
});
|
|
916
|
+
for (const file of files){
|
|
917
|
+
await callback(path.join(directory, file));
|
|
918
|
+
filesProcessed++;
|
|
919
|
+
if (options.limit && filesProcessed >= options.limit) {
|
|
920
|
+
log(`Reached limit of ${options.limit} files, stopping`);
|
|
921
|
+
break;
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
} catch (err) {
|
|
925
|
+
throw new Error(`Failed to glob pattern ${options.pattern} in ${directory}: ${err.message}`);
|
|
926
|
+
}
|
|
927
|
+
};
|
|
928
|
+
const readStream = async (path)=>{
|
|
929
|
+
return fs__namespace$1.createReadStream(path);
|
|
930
|
+
};
|
|
931
|
+
const hashFile = async (path, length)=>{
|
|
932
|
+
const file = await readFile(path, 'utf8');
|
|
933
|
+
return crypto.createHash('sha256').update(file).digest('hex').slice(0, length);
|
|
934
|
+
};
|
|
935
|
+
const listFiles = async (directory)=>{
|
|
936
|
+
return await fs__namespace$1.promises.readdir(directory);
|
|
937
|
+
};
|
|
938
|
+
return {
|
|
939
|
+
exists,
|
|
940
|
+
isDirectory,
|
|
941
|
+
isFile,
|
|
942
|
+
isReadable,
|
|
943
|
+
isWritable,
|
|
944
|
+
isFileReadable,
|
|
945
|
+
isDirectoryWritable,
|
|
946
|
+
isDirectoryReadable,
|
|
947
|
+
createDirectory,
|
|
948
|
+
readFile,
|
|
949
|
+
readStream,
|
|
950
|
+
writeFile,
|
|
951
|
+
forEachFileIn,
|
|
952
|
+
hashFile,
|
|
953
|
+
listFiles
|
|
954
|
+
};
|
|
955
|
+
};
|
|
956
|
+
|
|
957
|
+
const OptionsSchema$1 = zod.z.object({
|
|
958
|
+
logger: zod.z.any().optional().default(DEFAULT_LOGGER),
|
|
959
|
+
ignorePatterns: zod.z.array(zod.z.string()).optional().default(DEFAULT_IGNORE_PATTERNS),
|
|
960
|
+
parameters: parameters.ParametersSchema.optional().default({})
|
|
961
|
+
});
|
|
962
|
+
/**
|
|
963
|
+
* Extracts the first header from Markdown text
|
|
964
|
+
* @param markdownText The Markdown text to parse
|
|
965
|
+
* @returns The first header found in the Markdown or null if none is found
|
|
966
|
+
*/ function extractFirstHeader(markdownText) {
|
|
967
|
+
// Regular expression to match Markdown headers (# Header, ## Header, etc.)
|
|
968
|
+
const headerRegex = /^(#{1,6})\s+(.+?)(?:\n|$)/m;
|
|
969
|
+
const match = markdownText.match(headerRegex);
|
|
970
|
+
if (match && match[2]) {
|
|
971
|
+
return match[2].trim();
|
|
972
|
+
}
|
|
973
|
+
return null;
|
|
974
|
+
}
|
|
975
|
+
/**
|
|
976
|
+
* Removes the first header from Markdown text
|
|
977
|
+
* @param markdownText The Markdown text to process
|
|
978
|
+
* @returns The Markdown text without the first header
|
|
979
|
+
*/ function removeFirstHeader(markdownText) {
|
|
980
|
+
// Regular expression to match Markdown headers (# Header, ## Header, etc.)
|
|
981
|
+
const headerRegex = /^(#{1,6})\s+(.+?)(?:\n|$)/m;
|
|
982
|
+
const match = markdownText.match(headerRegex);
|
|
983
|
+
if (match) {
|
|
984
|
+
return markdownText.replace(headerRegex, '').trim();
|
|
985
|
+
}
|
|
986
|
+
return markdownText;
|
|
987
|
+
}
|
|
988
|
+
const loader.create = (loaderOptions)=>{
|
|
989
|
+
const options = OptionsSchema$1.parse(loaderOptions || {});
|
|
990
|
+
const parameters = options.parameters;
|
|
991
|
+
const logger = wrapLogger(options.logger, 'Loader');
|
|
992
|
+
const ignorePatterns = options.ignorePatterns;
|
|
993
|
+
const loadOptions = (sectionOptions = {})=>{
|
|
994
|
+
const currentOptions = section.SectionOptionsSchema.parse(sectionOptions);
|
|
995
|
+
return {
|
|
996
|
+
...currentOptions,
|
|
997
|
+
parameters: {
|
|
998
|
+
...parameters,
|
|
999
|
+
...currentOptions.parameters
|
|
1000
|
+
}
|
|
1001
|
+
};
|
|
1002
|
+
};
|
|
1003
|
+
/**
|
|
1004
|
+
* Loads context from the provided directories and returns instruction sections
|
|
1005
|
+
*
|
|
1006
|
+
* @param contextDirectories Directories containing context files
|
|
1007
|
+
* @returns Array of instruction sections loaded from context directories
|
|
1008
|
+
*/ const load = async (contextDirectories = [], options = {})=>{
|
|
1009
|
+
const sectionOptions = loadOptions(options);
|
|
1010
|
+
logger.debug(`Loading context from ${contextDirectories}`);
|
|
1011
|
+
const contextSections = [];
|
|
1012
|
+
if (!contextDirectories || contextDirectories.length === 0) {
|
|
1013
|
+
logger.debug(`No context directories provided, returning empty context`);
|
|
1014
|
+
return contextSections;
|
|
1015
|
+
}
|
|
1016
|
+
const storage = create$1({
|
|
1017
|
+
log: logger.debug
|
|
1018
|
+
});
|
|
1019
|
+
// Add context sections from each directory
|
|
1020
|
+
for (const contextDir of contextDirectories){
|
|
1021
|
+
try {
|
|
1022
|
+
const dirName = path.basename(contextDir);
|
|
1023
|
+
logger.debug(`Processing context directory ${dirName}`);
|
|
1024
|
+
let mainContextSection;
|
|
1025
|
+
// First check if there's a context.md file
|
|
1026
|
+
const contextFile = path.join(contextDir, 'context.md');
|
|
1027
|
+
if (await storage.exists(contextFile)) {
|
|
1028
|
+
logger.debug(`Found context.md file in ${contextDir}`);
|
|
1029
|
+
const mainContextContent = await storage.readFile(contextFile, 'utf8');
|
|
1030
|
+
// Extract the first header from the Markdown content
|
|
1031
|
+
const firstHeader = extractFirstHeader(mainContextContent);
|
|
1032
|
+
// Use the header from context.md as the section title, or fallback to directory name
|
|
1033
|
+
const sectionTitle = firstHeader || dirName;
|
|
1034
|
+
mainContextSection = create$4({
|
|
1035
|
+
...sectionOptions,
|
|
1036
|
+
title: sectionTitle
|
|
1037
|
+
});
|
|
1038
|
+
// Add content without the header
|
|
1039
|
+
if (firstHeader) {
|
|
1040
|
+
mainContextSection.add(removeFirstHeader(mainContextContent), {
|
|
1041
|
+
...sectionOptions
|
|
1042
|
+
});
|
|
1043
|
+
} else {
|
|
1044
|
+
mainContextSection.add(mainContextContent, {
|
|
1045
|
+
...sectionOptions
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
} else {
|
|
1049
|
+
// If no context.md exists, use directory name as title
|
|
1050
|
+
mainContextSection = create$4({
|
|
1051
|
+
...sectionOptions,
|
|
1052
|
+
title: dirName
|
|
1053
|
+
});
|
|
1054
|
+
}
|
|
1055
|
+
// Get all other files in the directory
|
|
1056
|
+
const files = await storage.listFiles(contextDir);
|
|
1057
|
+
const ignorePatternsRegex = ignorePatterns.map((pattern)=>new RegExp(pattern, 'i'));
|
|
1058
|
+
const filteredFiles = files.filter((file)=>!ignorePatternsRegex.some((regex)=>regex.test(file)));
|
|
1059
|
+
for (const file of filteredFiles){
|
|
1060
|
+
// Skip the context.md file as it's already processed
|
|
1061
|
+
if (file === 'context.md') continue;
|
|
1062
|
+
logger.debug(`Processing file ${file} in ${contextDir}`);
|
|
1063
|
+
const filePath = path.join(contextDir, file);
|
|
1064
|
+
if (await storage.isFile(filePath)) {
|
|
1065
|
+
const fileContent = await storage.readFile(filePath, 'utf8');
|
|
1066
|
+
let sectionName = file;
|
|
1067
|
+
let contentToAdd = fileContent;
|
|
1068
|
+
// Extract header if it exists
|
|
1069
|
+
if (file.endsWith('.md')) {
|
|
1070
|
+
const fileHeader = extractFirstHeader(fileContent);
|
|
1071
|
+
if (fileHeader) {
|
|
1072
|
+
sectionName = fileHeader;
|
|
1073
|
+
// Remove the header from the content
|
|
1074
|
+
contentToAdd = removeFirstHeader(fileContent);
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
// Create a subsection with the extracted name
|
|
1078
|
+
const fileSection = create$4({
|
|
1079
|
+
...sectionOptions,
|
|
1080
|
+
title: sectionName
|
|
1081
|
+
});
|
|
1082
|
+
fileSection.add(contentToAdd, {
|
|
1083
|
+
...sectionOptions
|
|
1084
|
+
});
|
|
1085
|
+
// Add this file section to the main context section
|
|
1086
|
+
mainContextSection.add(fileSection, {
|
|
1087
|
+
...sectionOptions
|
|
1088
|
+
});
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
contextSections.push(mainContextSection);
|
|
1092
|
+
} catch (error) {
|
|
1093
|
+
logger.error(`Error processing context directory ${contextDir}: ${error}`);
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
return contextSections;
|
|
1097
|
+
};
|
|
1098
|
+
return {
|
|
1099
|
+
load
|
|
1100
|
+
};
|
|
1101
|
+
};
|
|
1102
|
+
|
|
1103
|
+
const loader = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
|
|
1104
|
+
__proto__: null,
|
|
1105
|
+
create: loader.create,
|
|
1106
|
+
extractFirstHeader,
|
|
1107
|
+
removeFirstHeader
|
|
1108
|
+
}, Symbol.toStringTag, { value: 'Module' }));
|
|
1109
|
+
|
|
1110
|
+
const OptionsSchema = zod.z.object({
|
|
1111
|
+
logger: zod.z.any().optional().default(DEFAULT_LOGGER),
|
|
1112
|
+
configDir: zod.z.string().default('./overrides'),
|
|
1113
|
+
overrides: zod.z.boolean().default(false),
|
|
1114
|
+
parameters: parameters.ParametersSchema.optional().default({})
|
|
1115
|
+
});
|
|
1116
|
+
const override.create = (overrideOptions = {})=>{
|
|
1117
|
+
const options = OptionsSchema.parse(overrideOptions);
|
|
1118
|
+
const parameters = options.parameters;
|
|
1119
|
+
const logger = wrapLogger(options === null || options === void 0 ? void 0 : options.logger, 'Override');
|
|
1120
|
+
const storage = create$1({
|
|
1121
|
+
log: logger.debug
|
|
1122
|
+
});
|
|
1123
|
+
const loadOptions = (sectionOptions = {})=>{
|
|
1124
|
+
const currentOptions = section.SectionOptionsSchema.parse(sectionOptions);
|
|
1125
|
+
return {
|
|
1126
|
+
...currentOptions,
|
|
1127
|
+
parameters: {
|
|
1128
|
+
...parameters,
|
|
1129
|
+
...currentOptions.parameters
|
|
1130
|
+
}
|
|
1131
|
+
};
|
|
1132
|
+
};
|
|
1133
|
+
const override = async (overrideFile, section, sectionOptions = {})=>{
|
|
1134
|
+
const currentSectionOptions = loadOptions(sectionOptions);
|
|
1135
|
+
const baseFile = path.join(options.configDir, overrideFile);
|
|
1136
|
+
const preFile = baseFile.replace('.md', '-pre.md');
|
|
1137
|
+
const postFile = baseFile.replace('.md', '-post.md');
|
|
1138
|
+
const response = {};
|
|
1139
|
+
if (await storage.exists(preFile)) {
|
|
1140
|
+
logger.silly('Found pre file %s', preFile);
|
|
1141
|
+
const parser$1 = parser.create({
|
|
1142
|
+
logger
|
|
1143
|
+
});
|
|
1144
|
+
response.prepend = await parser$1.parseFile(preFile, currentSectionOptions);
|
|
1145
|
+
}
|
|
1146
|
+
if (await storage.exists(postFile)) {
|
|
1147
|
+
logger.silly('Found post file %s', postFile);
|
|
1148
|
+
const parser$1 = parser.create({
|
|
1149
|
+
logger
|
|
1150
|
+
});
|
|
1151
|
+
response.append = await parser$1.parseFile(postFile, currentSectionOptions);
|
|
1152
|
+
}
|
|
1153
|
+
if (await storage.exists(baseFile)) {
|
|
1154
|
+
logger.silly('Found base file %s', baseFile);
|
|
1155
|
+
if (options.overrides) {
|
|
1156
|
+
logger.warn('WARNING: Core directives are being overwritten by custom configuration');
|
|
1157
|
+
const parser$1 = parser.create({
|
|
1158
|
+
logger
|
|
1159
|
+
});
|
|
1160
|
+
response.override = await parser$1.parseFile(baseFile, currentSectionOptions);
|
|
1161
|
+
} else {
|
|
1162
|
+
logger.error('ERROR: Core directives are being overwritten by custom configuration');
|
|
1163
|
+
throw new Error('Core directives are being overwritten by custom configuration, but overrides are not enabled. Please enable --overrides to use this feature.');
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
return response;
|
|
1167
|
+
};
|
|
1168
|
+
const customize = async (overrideFile, section, sectionOptions = {})=>{
|
|
1169
|
+
const currentSectionOptions = loadOptions(sectionOptions);
|
|
1170
|
+
const { overrideContent, prepend, append } = await override(overrideFile, section, currentSectionOptions);
|
|
1171
|
+
let finalSection = section;
|
|
1172
|
+
if (overrideContent) {
|
|
1173
|
+
if (options.overrides) {
|
|
1174
|
+
logger.warn('Override found, replacing content from file %s', overrideContent);
|
|
1175
|
+
finalSection = overrideContent;
|
|
1176
|
+
} else {
|
|
1177
|
+
logger.error('ERROR: Core directives are being overwritten by custom configuration');
|
|
1178
|
+
throw new Error('Core directives are being overwritten by custom configuration, but overrides are not enabled. Please enable --overrides to use this feature.');
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
if (prepend) {
|
|
1182
|
+
logger.silly('Prepend found, adding to content from file %s', prepend);
|
|
1183
|
+
finalSection = finalSection.prepend(prepend);
|
|
1184
|
+
}
|
|
1185
|
+
if (append) {
|
|
1186
|
+
logger.silly('Append found, adding to content from file %s', append);
|
|
1187
|
+
finalSection = finalSection.append(append);
|
|
1188
|
+
}
|
|
1189
|
+
const formatter$1 = formatter.create({
|
|
1190
|
+
logger
|
|
1191
|
+
});
|
|
1192
|
+
logger.silly('Final section %s:\n\n%s\n\n', logger.name, formatter$1.format(finalSection));
|
|
1193
|
+
return finalSection;
|
|
1194
|
+
};
|
|
1195
|
+
return {
|
|
1196
|
+
override,
|
|
1197
|
+
customize
|
|
1198
|
+
};
|
|
1199
|
+
};
|
|
1200
|
+
|
|
1201
|
+
const override = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
|
|
1202
|
+
__proto__: null,
|
|
1203
|
+
create: override.create
|
|
1204
|
+
}, Symbol.toStringTag, { value: 'Module' }));
|
|
1205
|
+
|
|
1206
|
+
const OptionSchema = zod.z.object({
|
|
1207
|
+
logger: zod.z.any().optional().default(DEFAULT_LOGGER),
|
|
1208
|
+
basePath: zod.z.string(),
|
|
1209
|
+
overridePath: zod.z.string().optional().default("./"),
|
|
1210
|
+
overrides: zod.z.boolean().optional().default(false),
|
|
1211
|
+
parameters: parameters.ParametersSchema.optional().default({})
|
|
1212
|
+
});
|
|
1213
|
+
const create = (builderOptions)=>{
|
|
1214
|
+
const options = OptionSchema.parse(builderOptions);
|
|
1215
|
+
const logger = wrapLogger(options.logger, 'Builder');
|
|
1216
|
+
const parser$1 = parser.create({
|
|
1217
|
+
logger
|
|
1218
|
+
});
|
|
1219
|
+
const override$1 = override.create({
|
|
1220
|
+
logger,
|
|
1221
|
+
configDir: options.overridePath || "./",
|
|
1222
|
+
overrides: options.overrides || false
|
|
1223
|
+
});
|
|
1224
|
+
const loader$1 = loader.create({
|
|
1225
|
+
logger
|
|
1226
|
+
});
|
|
1227
|
+
const personaSection = create$4({
|
|
1228
|
+
title: "Persona"
|
|
1229
|
+
});
|
|
1230
|
+
const contextSection = create$4({
|
|
1231
|
+
title: "Context"
|
|
1232
|
+
});
|
|
1233
|
+
const instructionSection = create$4({
|
|
1234
|
+
title: "Instruction"
|
|
1235
|
+
});
|
|
1236
|
+
const contentSection = create$4({
|
|
1237
|
+
title: "Content"
|
|
1238
|
+
});
|
|
1239
|
+
const parameters = options.parameters;
|
|
1240
|
+
const instance = {};
|
|
1241
|
+
const loadOptions = (sectionOptions = {})=>{
|
|
1242
|
+
const currentOptions = section.SectionOptionsSchema.parse(sectionOptions);
|
|
1243
|
+
return {
|
|
1244
|
+
...currentOptions,
|
|
1245
|
+
parameters: {
|
|
1246
|
+
...parameters,
|
|
1247
|
+
...currentOptions.parameters
|
|
1248
|
+
}
|
|
1249
|
+
};
|
|
1250
|
+
};
|
|
1251
|
+
const loadDirectories = async (directories, sectionOptions = {})=>{
|
|
1252
|
+
const currentOptions = loadOptions(sectionOptions);
|
|
1253
|
+
logger.debug("Loading directories", directories);
|
|
1254
|
+
const sections = await loader$1.load(directories, currentOptions);
|
|
1255
|
+
return sections;
|
|
1256
|
+
};
|
|
1257
|
+
const loadContext = async (contextDirectories, sectionOptions = {})=>{
|
|
1258
|
+
const currentOptions = loadOptions(sectionOptions);
|
|
1259
|
+
logger.debug('Loading context', contextDirectories);
|
|
1260
|
+
const context = await loadDirectories(contextDirectories, currentOptions);
|
|
1261
|
+
contextSection.add(context);
|
|
1262
|
+
return instance;
|
|
1263
|
+
};
|
|
1264
|
+
instance.loadContext = loadContext;
|
|
1265
|
+
const loadContent = async (contentDirectories, sectionOptions = {})=>{
|
|
1266
|
+
const currentOptions = loadOptions(sectionOptions);
|
|
1267
|
+
const content = await loadDirectories(contentDirectories, currentOptions);
|
|
1268
|
+
contentSection.add(content);
|
|
1269
|
+
return instance;
|
|
1270
|
+
};
|
|
1271
|
+
instance.loadContent = loadContent;
|
|
1272
|
+
const loadPath = async (contentPath, sectionOptions = {})=>{
|
|
1273
|
+
const currentOptions = loadOptions(sectionOptions);
|
|
1274
|
+
const defaultPath = path.join(options.basePath, contentPath);
|
|
1275
|
+
const section = await parser$1.parseFile(defaultPath, currentOptions);
|
|
1276
|
+
const overrideSection = await override$1.customize(contentPath, section, currentOptions);
|
|
1277
|
+
return overrideSection;
|
|
1278
|
+
};
|
|
1279
|
+
const addPersonaPath = async (contentPath, sectionOptions = {})=>{
|
|
1280
|
+
const currentOptions = loadOptions(sectionOptions);
|
|
1281
|
+
const persona = await loadPath(contentPath, currentOptions);
|
|
1282
|
+
personaSection.add(persona);
|
|
1283
|
+
return instance;
|
|
1284
|
+
};
|
|
1285
|
+
instance.addPersonaPath = addPersonaPath;
|
|
1286
|
+
const addContextPath = async (contentPath, sectionOptions = {})=>{
|
|
1287
|
+
logger.debug("Adding context path", contentPath);
|
|
1288
|
+
const currentOptions = loadOptions(sectionOptions);
|
|
1289
|
+
const context = await loadPath(contentPath, currentOptions);
|
|
1290
|
+
contextSection.add(context);
|
|
1291
|
+
return instance;
|
|
1292
|
+
};
|
|
1293
|
+
instance.addContextPath = addContextPath;
|
|
1294
|
+
const addInstructionPath = async (contentPath, sectionOptions = {})=>{
|
|
1295
|
+
logger.debug("Adding instruction path", contentPath);
|
|
1296
|
+
const currentOptions = loadOptions(sectionOptions);
|
|
1297
|
+
const instruction = await loadPath(contentPath, currentOptions);
|
|
1298
|
+
instructionSection.add(instruction);
|
|
1299
|
+
return instance;
|
|
1300
|
+
};
|
|
1301
|
+
instance.addInstructionPath = addInstructionPath;
|
|
1302
|
+
const addContentPath = async (contentPath, sectionOptions = {})=>{
|
|
1303
|
+
logger.debug("Adding content path", contentPath);
|
|
1304
|
+
const currentOptions = loadOptions(sectionOptions);
|
|
1305
|
+
const content = await loadPath(contentPath, currentOptions);
|
|
1306
|
+
contentSection.add(content);
|
|
1307
|
+
return instance;
|
|
1308
|
+
};
|
|
1309
|
+
instance.addContentPath = addContentPath;
|
|
1310
|
+
const addContent = async (content, sectionOptions = {})=>{
|
|
1311
|
+
logger.debug("Adding content", typeof content);
|
|
1312
|
+
const currentOptions = loadOptions(sectionOptions);
|
|
1313
|
+
const parsedContentSection = parser$1.parse(content, currentOptions);
|
|
1314
|
+
contentSection.add(parsedContentSection);
|
|
1315
|
+
return instance;
|
|
1316
|
+
};
|
|
1317
|
+
instance.addContent = addContent;
|
|
1318
|
+
const addContext = async (context, sectionOptions = {})=>{
|
|
1319
|
+
logger.debug("Adding context", typeof context);
|
|
1320
|
+
const currentOptions = loadOptions(sectionOptions);
|
|
1321
|
+
const parsedContextSection = parser$1.parse(context, currentOptions);
|
|
1322
|
+
contextSection.add(parsedContextSection);
|
|
1323
|
+
return instance;
|
|
1324
|
+
};
|
|
1325
|
+
instance.addContext = addContext;
|
|
1326
|
+
const build = async ()=>{
|
|
1327
|
+
logger.debug("Building prompt", {});
|
|
1328
|
+
const prompt = create$2({
|
|
1329
|
+
persona: personaSection,
|
|
1330
|
+
contexts: contextSection,
|
|
1331
|
+
instructions: instructionSection,
|
|
1332
|
+
contents: contentSection
|
|
1333
|
+
});
|
|
1334
|
+
return prompt;
|
|
1335
|
+
};
|
|
1336
|
+
instance.build = build;
|
|
1337
|
+
return instance;
|
|
1338
|
+
};
|
|
1339
|
+
|
|
1340
|
+
const builder = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
|
|
1341
|
+
__proto__: null,
|
|
1342
|
+
create
|
|
1343
|
+
}, Symbol.toStringTag, { value: 'Module' }));
|
|
1344
|
+
|
|
1345
|
+
exports.Builder = builder;
|
|
1346
|
+
exports.Chat = chat;
|
|
1347
|
+
exports.Formatter = formatter;
|
|
1348
|
+
exports.Loader = loader;
|
|
1349
|
+
exports.Override = override;
|
|
1350
|
+
exports.Parser = parser;
|
|
1351
|
+
exports.createContent = create$7;
|
|
1352
|
+
exports.createContext = create$6;
|
|
1353
|
+
exports.createInstruction = create$5;
|
|
1354
|
+
exports.createParameters = create$9;
|
|
1355
|
+
exports.createPrompt = create$2;
|
|
1356
|
+
exports.createSection = create$4;
|
|
1357
|
+
exports.createTrait = create$3;
|
|
1358
|
+
exports.createWeighted = create$8;
|
|
1359
|
+
//# sourceMappingURL=riotprompt.cjs.map
|