@mistralys/persona-builder 2.2.0 → 2.4.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/dist/index.cjs CHANGED
@@ -27,10 +27,30 @@ function resolvePartials(text, partialsMap, depth = 0) {
27
27
  }
28
28
 
29
29
  // src/engine/conditionals.ts
30
+ var NO_NESTED_IF = String.raw`(?:(?!\{\{#if\b)[\s\S])*?`;
31
+ var ELSE_IF_PATTERN = new RegExp(
32
+ String.raw`\{\{else if (\w+)\}\}(${NO_NESTED_IF})\{\{\/if\}\}`,
33
+ "g"
34
+ );
35
+ function resolveElseIf(text) {
36
+ if (!text.includes("{{else if ")) {
37
+ return text;
38
+ }
39
+ let result = text;
40
+ let prev;
41
+ do {
42
+ prev = result;
43
+ result = result.replace(
44
+ ELSE_IF_PATTERN,
45
+ (_match, flag, content) => `{{else}}{{#if ${flag}}}${content}{{/if}}{{/if}}`
46
+ );
47
+ } while (result !== prev);
48
+ return result;
49
+ }
30
50
  function resolveConditionals(text, context) {
31
- const noNestedIf = String.raw`(?:(?!\{\{#if\b)[\s\S])*?`;
51
+ const normalized = resolveElseIf(text);
32
52
  const pattern = new RegExp(
33
- String.raw`\n*\{\{#if (\w+)\}\}(${noNestedIf})` + String.raw`(?:\{\{else\}\}(${noNestedIf}))?\{\{\/if\}\}\n*`,
53
+ String.raw`\n*\{\{#if (\w+)\}\}(${NO_NESTED_IF})` + String.raw`(?:\{\{else\}\}(${NO_NESTED_IF}))?\{\{\/if\}\}\n*`,
34
54
  "g"
35
55
  );
36
56
  const resolve = (_match, flag, inner, elseInner) => {
@@ -42,7 +62,7 @@ function resolveConditionals(text, context) {
42
62
  }
43
63
  return "\n";
44
64
  };
45
- let result = text;
65
+ let result = normalized;
46
66
  let prev;
47
67
  do {
48
68
  prev = result;
@@ -137,7 +157,7 @@ function runBuildContext(plugins, ctx, persona, suite, target) {
137
157
  let accumulated = ctx;
138
158
  for (const plugin of plugins) {
139
159
  if (typeof plugin.onBuildContext === "function") {
140
- accumulated = plugin.onBuildContext(accumulated, persona, suite, target);
160
+ accumulated = plugin.onBuildContext(accumulated, persona, suite, target) ?? accumulated;
141
161
  }
142
162
  }
143
163
  return accumulated;
@@ -146,11 +166,29 @@ function runPostRender(plugins, rendered, persona, target) {
146
166
  let output = rendered;
147
167
  for (const plugin of plugins) {
148
168
  if (typeof plugin.onPostRender === "function") {
149
- output = plugin.onPostRender(output, persona, target);
169
+ output = plugin.onPostRender(output, persona, target) ?? output;
150
170
  }
151
171
  }
152
172
  return output;
153
173
  }
174
+ function runPartials(plugins, partialsMap, suiteName, suite) {
175
+ let accumulated = partialsMap;
176
+ for (const plugin of plugins) {
177
+ if (typeof plugin.onPartials === "function") {
178
+ accumulated = plugin.onPartials(accumulated, suiteName, suite) ?? accumulated;
179
+ }
180
+ }
181
+ return accumulated;
182
+ }
183
+ function runPersonaPartials(plugins, partialsMap, persona, context, suite, target) {
184
+ let accumulated = partialsMap;
185
+ for (const plugin of plugins) {
186
+ if (typeof plugin.onPersonaPartials === "function") {
187
+ accumulated = plugin.onPersonaPartials(accumulated, persona, context, suite, target) ?? accumulated;
188
+ }
189
+ }
190
+ return accumulated;
191
+ }
154
192
  function runValidate(plugins, persona, suite, target) {
155
193
  const results = [];
156
194
  for (const plugin of plugins) {
@@ -373,9 +411,20 @@ async function buildAgentNameMap(config) {
373
411
  }
374
412
  return agentMap;
375
413
  }
376
- function buildContext(personaMeta, sharedMeta, agentMap = {}, target, registry) {
414
+ function buildContext(options) {
415
+ const {
416
+ personaMeta,
417
+ sharedMeta,
418
+ agentMap = {},
419
+ target,
420
+ registry,
421
+ configVariables,
422
+ suiteVariables
423
+ } = options;
377
424
  const version = typeof personaMeta["version"] === "string" ? personaMeta["version"] : typeof sharedMeta["default_version"] === "string" ? sharedMeta["default_version"] : "0.0.0";
378
425
  const merged = {
426
+ ...configVariables ?? {},
427
+ ...suiteVariables ?? {},
379
428
  ...sharedMeta,
380
429
  ...personaMeta,
381
430
  version
@@ -430,16 +479,32 @@ function buildContext(personaMeta, sharedMeta, agentMap = {}, target, registry)
430
479
  }
431
480
  async function buildPersona(personaYamlPath, suiteName, suiteConfig, sharedMeta, partialsMap, config, plugins, target, agentMap = {}, registry = defaultRegistry) {
432
481
  const personaMeta = await loadPersonaYaml(personaYamlPath);
433
- let context = buildContext(personaMeta, sharedMeta, agentMap, target, registry);
482
+ let context = buildContext({
483
+ personaMeta,
484
+ sharedMeta,
485
+ agentMap,
486
+ target,
487
+ registry,
488
+ configVariables: config.variables,
489
+ suiteVariables: suiteConfig.variables
490
+ });
434
491
  const personaMetaTyped = personaMeta;
435
492
  context = runBuildContext(plugins, context, personaMetaTyped, suiteConfig, target);
493
+ const personaPartialsMap = runPersonaPartials(
494
+ plugins,
495
+ { ...partialsMap },
496
+ personaMetaTyped,
497
+ context,
498
+ suiteConfig,
499
+ target
500
+ );
436
501
  const fmTemplate = resolveFrontmatterTemplate(target, plugins, config.frontmatter, registry);
437
502
  const contentBasename = path4__default.default.basename(personaYamlPath, ".yaml") + ".md";
438
503
  const frontmatter = renderFrontmatter(fmTemplate, context, contentBasename);
439
504
  const contentSubdir = suiteConfig.contentSubdir ?? "content";
440
505
  const contentPath = path4__default.default.join(suiteConfig.srcDir, contentSubdir, contentBasename);
441
506
  const bodyTemplate = normalizeNewlines(await promises.readFile(contentPath, "utf8"));
442
- let body = resolvePartials(bodyTemplate, partialsMap);
507
+ let body = resolvePartials(bodyTemplate, personaPartialsMap);
443
508
  body = resolveConditionals(body, context);
444
509
  body = resolveVariables(body, context, contentBasename);
445
510
  body = collapseBlankLines(body);
@@ -477,7 +542,7 @@ async function buildSuite(suiteName, suiteConfig, config, plugins, target, agent
477
542
  const metaSubdir = suiteConfig.metaSubdir ?? "meta";
478
543
  const sharedYamlPath = path4__default.default.join(suiteConfig.srcDir, metaSubdir, "_shared.yaml");
479
544
  const sharedMeta = await loadRawYaml(sharedYamlPath);
480
- let partialsMap = {};
545
+ let partialsMap = { ...config.partials ?? {} };
481
546
  if (config.sharedPartialsDir && fs.existsSync(config.sharedPartialsDir)) {
482
547
  partialsMap = { ...partialsMap, ...await loadPartials(config.sharedPartialsDir) };
483
548
  }
@@ -487,6 +552,7 @@ async function buildSuite(suiteName, suiteConfig, config, plugins, target, agent
487
552
  partialsMap = { ...partialsMap, ...await loadPartials(suitePartialsDir) };
488
553
  }
489
554
  runSuiteInit(plugins, suiteConfig, sharedMeta);
555
+ partialsMap = runPartials(plugins, partialsMap, suiteName, suiteConfig);
490
556
  const personaYamlPaths = await discoverSuitePersonaYamls(suiteConfig);
491
557
  const results = [];
492
558
  for (const yamlPath of personaYamlPaths) {
@@ -628,6 +694,8 @@ exports.resolveFrontmatterTemplate = resolveFrontmatterTemplate;
628
694
  exports.resolvePartials = resolvePartials;
629
695
  exports.resolveVariables = resolveVariables;
630
696
  exports.runBuildContext = runBuildContext;
697
+ exports.runPartials = runPartials;
698
+ exports.runPersonaPartials = runPersonaPartials;
631
699
  exports.runPostRender = runPostRender;
632
700
  exports.runSuiteInit = runSuiteInit;
633
701
  exports.runValidate = runValidate;