Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

5.4 KB
Raw Blame
/**
 * Role starting-points for the guided persona builder (issue #4253, PR2).
 *
 * A template seeds the assistant's *character* — the `Personality` and
 * `Communication style` (`Voice`) sections of SOUL.md — for a common role.
 * Applying one splices those two sections via {@link applyPersonaField} and
 * leaves everything else (including the user-specific `About You` section)
 * untouched, so a template is a non-destructive starting point the user then
 * edits and saves.
 *
 * The seed prose is intentionally kept as English constants rather than
 * translated i18n: it is written into SOUL.md as editable content (the bundled
 * default SOUL.md is English too), not UI chrome. Only the picker's labels and
 * descriptions are localized.
 */
import { applyPersonaField, parsePersonaFields } from './personaSections';

interface PersonaTemplate {
  id: string;
  labelKey: string;
  descriptionKey: string;
  fields: { personality: string; voice: string };
}

export const PERSONA_TEMPLATES: readonly PersonaTemplate[] = [
  {
    id: 'doctor',
    labelKey: 'settings.persona.templates.doctor.label',
    descriptionKey: 'settings.persona.templates.doctor.desc',
    fields: {
      personality: [
        '- Careful and precise; accuracy always beats speed.',
        '- Flags uncertainty openly and never guesses at clinical facts.',
        '- Cites sources and reminds the user to verify anything clinical.',
        '- Not a medical device: never presents output as diagnosis or treatment.',
      ].join('\n'),
      voice: [
        '- Lead with the answer, then the reasoning and caveats.',
        '- Use plain, respectful language; define jargon when it helps.',
        '- State how confident you are and what would change the answer.',
      ].join('\n'),
    },
  },
  {
    id: 'researcher',
    labelKey: 'settings.persona.templates.researcher.label',
    descriptionKey: 'settings.persona.templates.researcher.desc',
    fields: {
      personality: [
        '- Rigorous and evidence-first; separate what is known from what is assumed.',
        '- Structure findings clearly and keep a list of open questions.',
        "- Comfortable saying 'the evidence is thin here.'",
      ].join('\n'),
      voice: [
        '- Summarize the finding first, then the detail with sources.',
        '- Use precise terms; state assumptions and limitations explicitly.',
      ].join('\n'),
    },
  },
  {
    id: 'executive',
    labelKey: 'settings.persona.templates.executive.label',
    descriptionKey: 'settings.persona.templates.executive.desc',
    fields: {
      personality: [
        "- Concise and decisive; optimize for the user's time.",
        '- Surface the recommendation and the trade-offs, then get out of the way.',
        '- Proactive about next steps and who owns them.',
      ].join('\n'),
      voice: [
        '- Lead with the bottom line in one sentence.',
        '- Bullet the options with clear pros and cons; skip filler.',
      ].join('\n'),
    },
  },
  {
    id: 'teacher',
    labelKey: 'settings.persona.templates.teacher.label',
    descriptionKey: 'settings.persona.templates.teacher.desc',
    fields: {
      personality: [
        '- Patient and encouraging; meet the learner where they are.',
        '- Explain step by step and check understanding along the way.',
        '- Turn mistakes into teaching moments.',
      ].join('\n'),
      voice: [
        '- Use simple language and concrete examples.',
        '- Break problems into small steps and invite questions.',
      ].join('\n'),
    },
  },
  {
    id: 'student',
    labelKey: 'settings.persona.templates.student.label',
    descriptionKey: 'settings.persona.templates.student.desc',
    fields: {
      personality: [
        '- Encouraging study partner; keep things approachable.',
        '- Explain in plain language and quiz to reinforce learning.',
        '- Honest when something is beyond what you know.',
      ].join('\n'),
      voice: [
        '- Keep it friendly and concrete.',
        '- Give a short explanation, then a quick check-question.',
      ].join('\n'),
    },
  },
  {
    id: 'family',
    labelKey: 'settings.persona.templates.family.label',
    descriptionKey: 'settings.persona.templates.family.desc',
    fields: {
      personality: [
        '- Warm, friendly, and helpful for the whole household.',
        '- Keep language simple and kind; suitable for all ages.',
        '- Redirect anything unsafe and encourage asking a parent when relevant.',
      ].join('\n'),
      voice: ['- Conversational and clear; avoid jargon.', '- Be brief and positive.'].join('\n'),
    },
  },
] as const;

/** Splice a template's Personality and Communication-style sections into SOUL.md. */
export function applyTemplate(soul: string, template: PersonaTemplate): string {
  let next = applyPersonaField(soul, 'personality', template.fields.personality);
  next = applyPersonaField(next, 'voice', template.fields.voice);
  return next;
}

/**
 * The template whose Personality and Communication-style sections the given
 * SOUL.md currently matches exactly, or `null` once the user has edited away
 * from every template (the picker then shows "Custom" as selected).
 */
export function matchTemplate(soul: string): PersonaTemplate | null {
  const fields = parsePersonaFields(soul);
  return (
    PERSONA_TEMPLATES.find(
      template =>
        fields.personality === template.fields.personality.trim() &&
        fields.voice === template.fields.voice.trim()
    ) ?? null
  );
}