@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/cli.js CHANGED
@@ -19,10 +19,30 @@ function resolvePartials(text, partialsMap, depth = 0) {
19
19
  }
20
20
 
21
21
  // src/engine/conditionals.ts
22
+ var NO_NESTED_IF = String.raw`(?:(?!\{\{#if\b)[\s\S])*?`;
23
+ var ELSE_IF_PATTERN = new RegExp(
24
+ String.raw`\{\{else if (\w+)\}\}(${NO_NESTED_IF})\{\{\/if\}\}`,
25
+ "g"
26
+ );
27
+ function resolveElseIf(text) {
28
+ if (!text.includes("{{else if ")) {
29
+ return text;
30
+ }
31
+ let result = text;
32
+ let prev;
33
+ do {
34
+ prev = result;
35
+ result = result.replace(
36
+ ELSE_IF_PATTERN,
37
+ (_match, flag, content) => `{{else}}{{#if ${flag}}}${content}{{/if}}{{/if}}`
38
+ );
39
+ } while (result !== prev);
40
+ return result;
41
+ }
22
42
  function resolveConditionals(text, context) {
23
- const noNestedIf = String.raw`(?:(?!\{\{#if\b)[\s\S])*?`;
43
+ const normalized = resolveElseIf(text);
24
44
  const pattern = new RegExp(
25
- String.raw`\n*\{\{#if (\w+)\}\}(${noNestedIf})` + String.raw`(?:\{\{else\}\}(${noNestedIf}))?\{\{\/if\}\}\n*`,
45
+ String.raw`\n*\{\{#if (\w+)\}\}(${NO_NESTED_IF})` + String.raw`(?:\{\{else\}\}(${NO_NESTED_IF}))?\{\{\/if\}\}\n*`,
26
46
  "g"
27
47
  );
28
48
  const resolve = (_match, flag, inner, elseInner) => {
@@ -34,7 +54,7 @@ function resolveConditionals(text, context) {
34
54
  }
35
55
  return "\n";
36
56
  };
37
- let result = text;
57
+ let result = normalized;
38
58
  let prev;
39
59
  do {
40
60
  prev = result;
@@ -103,7 +123,7 @@ function runBuildContext(plugins, ctx, persona, suite, target) {
103
123
  let accumulated = ctx;
104
124
  for (const plugin of plugins) {
105
125
  if (typeof plugin.onBuildContext === "function") {
106
- accumulated = plugin.onBuildContext(accumulated, persona, suite, target);
126
+ accumulated = plugin.onBuildContext(accumulated, persona, suite, target) ?? accumulated;
107
127
  }
108
128
  }
109
129
  return accumulated;
@@ -112,11 +132,29 @@ function runPostRender(plugins, rendered, persona, target) {
112
132
  let output = rendered;
113
133
  for (const plugin of plugins) {
114
134
  if (typeof plugin.onPostRender === "function") {
115
- output = plugin.onPostRender(output, persona, target);
135
+ output = plugin.onPostRender(output, persona, target) ?? output;
116
136
  }
117
137
  }
118
138
  return output;
119
139
  }
140
+ function runPartials(plugins, partialsMap, suiteName, suite) {
141
+ let accumulated = partialsMap;
142
+ for (const plugin of plugins) {
143
+ if (typeof plugin.onPartials === "function") {
144
+ accumulated = plugin.onPartials(accumulated, suiteName, suite) ?? accumulated;
145
+ }
146
+ }
147
+ return accumulated;
148
+ }
149
+ function runPersonaPartials(plugins, partialsMap, persona, context, suite, target) {
150
+ let accumulated = partialsMap;
151
+ for (const plugin of plugins) {
152
+ if (typeof plugin.onPersonaPartials === "function") {
153
+ accumulated = plugin.onPersonaPartials(accumulated, persona, context, suite, target) ?? accumulated;
154
+ }
155
+ }
156
+ return accumulated;
157
+ }
120
158
  function runValidate(plugins, persona, suite, target) {
121
159
  const results = [];
122
160
  for (const plugin of plugins) {
@@ -339,9 +377,20 @@ async function buildAgentNameMap(config) {
339
377
  }
340
378
  return agentMap;
341
379
  }
342
- function buildContext(personaMeta, sharedMeta, agentMap = {}, target, registry) {
380
+ function buildContext(options) {
381
+ const {
382
+ personaMeta,
383
+ sharedMeta,
384
+ agentMap = {},
385
+ target,
386
+ registry,
387
+ configVariables,
388
+ suiteVariables
389
+ } = options;
343
390
  const version = typeof personaMeta["version"] === "string" ? personaMeta["version"] : typeof sharedMeta["default_version"] === "string" ? sharedMeta["default_version"] : "0.0.0";
344
391
  const merged = {
392
+ ...configVariables ?? {},
393
+ ...suiteVariables ?? {},
345
394
  ...sharedMeta,
346
395
  ...personaMeta,
347
396
  version
@@ -396,16 +445,32 @@ function buildContext(personaMeta, sharedMeta, agentMap = {}, target, registry)
396
445
  }
397
446
  async function buildPersona(personaYamlPath, suiteName, suiteConfig, sharedMeta, partialsMap, config, plugins, target, agentMap = {}, registry = defaultRegistry) {
398
447
  const personaMeta = await loadPersonaYaml(personaYamlPath);
399
- let context = buildContext(personaMeta, sharedMeta, agentMap, target, registry);
448
+ let context = buildContext({
449
+ personaMeta,
450
+ sharedMeta,
451
+ agentMap,
452
+ target,
453
+ registry,
454
+ configVariables: config.variables,
455
+ suiteVariables: suiteConfig.variables
456
+ });
400
457
  const personaMetaTyped = personaMeta;
401
458
  context = runBuildContext(plugins, context, personaMetaTyped, suiteConfig, target);
459
+ const personaPartialsMap = runPersonaPartials(
460
+ plugins,
461
+ { ...partialsMap },
462
+ personaMetaTyped,
463
+ context,
464
+ suiteConfig,
465
+ target
466
+ );
402
467
  const fmTemplate = resolveFrontmatterTemplate(target, plugins, config.frontmatter, registry);
403
468
  const contentBasename = path2.basename(personaYamlPath, ".yaml") + ".md";
404
469
  const frontmatter = renderFrontmatter(fmTemplate, context, contentBasename);
405
470
  const contentSubdir = suiteConfig.contentSubdir ?? "content";
406
471
  const contentPath = path2.join(suiteConfig.srcDir, contentSubdir, contentBasename);
407
472
  const bodyTemplate = normalizeNewlines(await readFile(contentPath, "utf8"));
408
- let body = resolvePartials(bodyTemplate, partialsMap);
473
+ let body = resolvePartials(bodyTemplate, personaPartialsMap);
409
474
  body = resolveConditionals(body, context);
410
475
  body = resolveVariables(body, context, contentBasename);
411
476
  body = collapseBlankLines(body);
@@ -443,7 +508,7 @@ async function buildSuite(suiteName, suiteConfig, config, plugins, target, agent
443
508
  const metaSubdir = suiteConfig.metaSubdir ?? "meta";
444
509
  const sharedYamlPath = path2.join(suiteConfig.srcDir, metaSubdir, "_shared.yaml");
445
510
  const sharedMeta = await loadRawYaml(sharedYamlPath);
446
- let partialsMap = {};
511
+ let partialsMap = { ...config.partials ?? {} };
447
512
  if (config.sharedPartialsDir && existsSync(config.sharedPartialsDir)) {
448
513
  partialsMap = { ...partialsMap, ...await loadPartials(config.sharedPartialsDir) };
449
514
  }
@@ -453,6 +518,7 @@ async function buildSuite(suiteName, suiteConfig, config, plugins, target, agent
453
518
  partialsMap = { ...partialsMap, ...await loadPartials(suitePartialsDir) };
454
519
  }
455
520
  runSuiteInit(plugins, suiteConfig, sharedMeta);
521
+ partialsMap = runPartials(plugins, partialsMap, suiteName, suiteConfig);
456
522
  const personaYamlPaths = await discoverSuitePersonaYamls(suiteConfig);
457
523
  const results = [];
458
524
  for (const yamlPath of personaYamlPaths) {