@wundr.io/prompt-templates 1.0.3

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/src/macros.ts ADDED
@@ -0,0 +1,635 @@
1
+ /**
2
+ * @wundr/prompt-templates - Reusable macro library for prompt templating
3
+ */
4
+
5
+ import type { MacroDefinition } from './types.js';
6
+
7
+ /**
8
+ * System role macro - defines AI assistant behavior
9
+ */
10
+ export const systemRoleMacro: MacroDefinition = {
11
+ name: 'systemRole',
12
+ description: 'Define the AI assistant role and behavior',
13
+ template: `You are {{role}}{{#ifDefined expertise}}, specialized in {{expertise}}{{/ifDefined}}.
14
+
15
+ {{#ifDefined personality}}
16
+ Your personality traits:
17
+ {{bulletList personality}}
18
+ {{/ifDefined}}
19
+
20
+ {{#ifDefined constraints}}
21
+ You must follow these constraints:
22
+ {{bulletList constraints}}
23
+ {{/ifDefined}}`,
24
+ parameters: [
25
+ {
26
+ name: 'role',
27
+ description: 'The role of the AI assistant',
28
+ type: 'string',
29
+ required: true,
30
+ },
31
+ {
32
+ name: 'expertise',
33
+ description: 'Areas of expertise',
34
+ type: 'string',
35
+ required: false,
36
+ },
37
+ {
38
+ name: 'personality',
39
+ description: 'Personality traits',
40
+ type: 'array',
41
+ required: false,
42
+ },
43
+ {
44
+ name: 'constraints',
45
+ description: 'Behavioral constraints',
46
+ type: 'array',
47
+ required: false,
48
+ },
49
+ ],
50
+ example:
51
+ '{{> systemRole role="a helpful assistant" expertise="software development" personality=(array "friendly" "concise") }}',
52
+ };
53
+
54
+ /**
55
+ * Task context macro - provides context for a specific task
56
+ */
57
+ export const taskContextMacro: MacroDefinition = {
58
+ name: 'taskContext',
59
+ description: 'Provide context for a specific task',
60
+ template: `## Task: {{taskName}}
61
+
62
+ {{#ifDefined description}}
63
+ ### Description
64
+ {{description}}
65
+ {{/ifDefined}}
66
+
67
+ {{#ifDefined objectives}}
68
+ ### Objectives
69
+ {{numberedList objectives}}
70
+ {{/ifDefined}}
71
+
72
+ {{#ifDefined constraints}}
73
+ ### Constraints
74
+ {{bulletList constraints}}
75
+ {{/ifDefined}}
76
+
77
+ {{#ifDefined examples}}
78
+ ### Examples
79
+ {{#each examples}}
80
+ **Example {{@index}}:**
81
+ {{#codeBlock language}}
82
+ {{code}}
83
+ {{/codeBlock}}
84
+ {{/each}}
85
+ {{/ifDefined}}`,
86
+ parameters: [
87
+ {
88
+ name: 'taskName',
89
+ description: 'Name of the task',
90
+ type: 'string',
91
+ required: true,
92
+ },
93
+ {
94
+ name: 'description',
95
+ description: 'Task description',
96
+ type: 'string',
97
+ required: false,
98
+ },
99
+ {
100
+ name: 'objectives',
101
+ description: 'List of objectives',
102
+ type: 'array',
103
+ required: false,
104
+ },
105
+ {
106
+ name: 'constraints',
107
+ description: 'Task constraints',
108
+ type: 'array',
109
+ required: false,
110
+ },
111
+ {
112
+ name: 'examples',
113
+ description: 'Example inputs/outputs',
114
+ type: 'array',
115
+ required: false,
116
+ },
117
+ ],
118
+ example:
119
+ '{{> taskContext taskName="Code Review" objectives=(array "Find bugs" "Suggest improvements") }}',
120
+ };
121
+
122
+ /**
123
+ * Output format macro - specifies expected output format
124
+ */
125
+ export const outputFormatMacro: MacroDefinition = {
126
+ name: 'outputFormat',
127
+ description: 'Specify the expected output format',
128
+ template: `## Output Format
129
+
130
+ {{#compare format "eq" "json"}}
131
+ Respond with valid JSON in the following structure:
132
+ {{#codeBlock "json"}}
133
+ {{schema}}
134
+ {{/codeBlock}}
135
+ {{/compare}}
136
+
137
+ {{#compare format "eq" "markdown"}}
138
+ Respond in Markdown format with the following sections:
139
+ {{bulletList sections}}
140
+ {{/compare}}
141
+
142
+ {{#compare format "eq" "structured"}}
143
+ Respond with the following structure:
144
+ {{#each fields}}
145
+ - **{{name}}**: {{description}}{{#ifDefined required}} (required){{/ifDefined}}
146
+ {{/each}}
147
+ {{/compare}}
148
+
149
+ {{#compare format "eq" "freeform"}}
150
+ Respond in natural language.
151
+ {{/compare}}
152
+
153
+ {{#ifDefined example}}
154
+ ### Example Output
155
+ {{#codeBlock language}}
156
+ {{example}}
157
+ {{/codeBlock}}
158
+ {{/ifDefined}}`,
159
+ parameters: [
160
+ {
161
+ name: 'format',
162
+ description: 'Output format type (json, markdown, structured, freeform)',
163
+ type: 'string',
164
+ required: true,
165
+ },
166
+ {
167
+ name: 'schema',
168
+ description: 'JSON schema for json format',
169
+ type: 'string',
170
+ required: false,
171
+ },
172
+ {
173
+ name: 'sections',
174
+ description: 'Section names for markdown format',
175
+ type: 'array',
176
+ required: false,
177
+ },
178
+ {
179
+ name: 'fields',
180
+ description: 'Field definitions for structured format',
181
+ type: 'array',
182
+ required: false,
183
+ },
184
+ {
185
+ name: 'example',
186
+ description: 'Example output',
187
+ type: 'string',
188
+ required: false,
189
+ },
190
+ {
191
+ name: 'language',
192
+ description: 'Code block language for example',
193
+ type: 'string',
194
+ required: false,
195
+ default: '',
196
+ },
197
+ ],
198
+ example:
199
+ '{{> outputFormat format="json" schema=\'{"result": "string", "confidence": "number"}\' }}',
200
+ };
201
+
202
+ /**
203
+ * Conversation history macro - formats previous messages
204
+ */
205
+ export const conversationHistoryMacro: MacroDefinition = {
206
+ name: 'conversationHistory',
207
+ description: 'Include formatted conversation history',
208
+ template: `{{#ifDefined messages}}
209
+ ## Conversation History
210
+
211
+ {{formatMemory messages max=maxMessages format=format}}
212
+ {{/ifDefined}}`,
213
+ parameters: [
214
+ {
215
+ name: 'messages',
216
+ description: 'Array of conversation messages',
217
+ type: 'array',
218
+ required: true,
219
+ },
220
+ {
221
+ name: 'maxMessages',
222
+ description: 'Maximum number of messages to include',
223
+ type: 'number',
224
+ required: false,
225
+ default: 10,
226
+ },
227
+ {
228
+ name: 'format',
229
+ description: 'Output format (default, compact, xml)',
230
+ type: 'string',
231
+ required: false,
232
+ default: 'default',
233
+ },
234
+ ],
235
+ example: '{{> conversationHistory messages=memory.messages maxMessages=5 }}',
236
+ };
237
+
238
+ /**
239
+ * Tools section macro - formats available tools
240
+ */
241
+ export const toolsSectionMacro: MacroDefinition = {
242
+ name: 'toolsSection',
243
+ description: 'Include available tools documentation',
244
+ template: `{{#ifDefined tools}}
245
+ ## Available Tools
246
+
247
+ You have access to the following tools:
248
+
249
+ {{formatTools tools format=format}}
250
+
251
+ {{#ifDefined instructions}}
252
+ ### Tool Usage Instructions
253
+ {{instructions}}
254
+ {{/ifDefined}}
255
+ {{/ifDefined}}`,
256
+ parameters: [
257
+ {
258
+ name: 'tools',
259
+ description: 'Array of tool definitions',
260
+ type: 'array',
261
+ required: true,
262
+ },
263
+ {
264
+ name: 'format',
265
+ description: 'Tool format (default, compact, json)',
266
+ type: 'string',
267
+ required: false,
268
+ default: 'default',
269
+ },
270
+ {
271
+ name: 'instructions',
272
+ description: 'Additional tool usage instructions',
273
+ type: 'string',
274
+ required: false,
275
+ },
276
+ ],
277
+ example: '{{> toolsSection tools=tools format="compact" }}',
278
+ };
279
+
280
+ /**
281
+ * Code context macro - provides code snippet context
282
+ */
283
+ export const codeContextMacro: MacroDefinition = {
284
+ name: 'codeContext',
285
+ description: 'Provide code context for analysis or generation',
286
+ template: `## Code Context
287
+
288
+ {{#ifDefined filename}}
289
+ **File:** \`{{filename}}\`
290
+ {{/ifDefined}}
291
+
292
+ {{#ifDefined language}}
293
+ **Language:** {{language}}
294
+ {{/ifDefined}}
295
+
296
+ {{#codeBlock language}}
297
+ {{code}}
298
+ {{/codeBlock}}
299
+
300
+ {{#ifDefined lineNumbers}}
301
+ **Lines:** {{lineNumbers.start}} - {{lineNumbers.end}}
302
+ {{/ifDefined}}
303
+
304
+ {{#ifDefined relatedFiles}}
305
+ ### Related Files
306
+ {{bulletList relatedFiles}}
307
+ {{/ifDefined}}`,
308
+ parameters: [
309
+ {
310
+ name: 'code',
311
+ description: 'The code snippet',
312
+ type: 'string',
313
+ required: true,
314
+ },
315
+ {
316
+ name: 'language',
317
+ description: 'Programming language',
318
+ type: 'string',
319
+ required: false,
320
+ },
321
+ {
322
+ name: 'filename',
323
+ description: 'Source filename',
324
+ type: 'string',
325
+ required: false,
326
+ },
327
+ {
328
+ name: 'lineNumbers',
329
+ description: 'Line number range',
330
+ type: 'object',
331
+ required: false,
332
+ },
333
+ {
334
+ name: 'relatedFiles',
335
+ description: 'List of related files',
336
+ type: 'array',
337
+ required: false,
338
+ },
339
+ ],
340
+ example:
341
+ '{{> codeContext code=sourceCode language="typescript" filename="index.ts" }}',
342
+ };
343
+
344
+ /**
345
+ * Chain of thought macro - encourages step-by-step reasoning
346
+ */
347
+ export const chainOfThoughtMacro: MacroDefinition = {
348
+ name: 'chainOfThought',
349
+ description: 'Encourage step-by-step reasoning',
350
+ template: `## Reasoning Instructions
351
+
352
+ Think through this problem step by step:
353
+
354
+ {{#ifDefined steps}}
355
+ {{numberedList steps}}
356
+ {{else}}
357
+ 1. First, understand the problem/request completely
358
+ 2. Break down the problem into smaller parts
359
+ 3. Consider different approaches
360
+ 4. Evaluate the pros and cons of each approach
361
+ 5. Select the best approach and implement it
362
+ 6. Verify your solution
363
+ {{/ifDefined}}
364
+
365
+ {{#ifDefined showThinking}}
366
+ Please show your reasoning process before providing the final answer.
367
+ {{/ifDefined}}`,
368
+ parameters: [
369
+ {
370
+ name: 'steps',
371
+ description: 'Custom reasoning steps',
372
+ type: 'array',
373
+ required: false,
374
+ },
375
+ {
376
+ name: 'showThinking',
377
+ description: 'Whether to show reasoning process',
378
+ type: 'boolean',
379
+ required: false,
380
+ default: true,
381
+ },
382
+ ],
383
+ example: '{{> chainOfThought showThinking=true }}',
384
+ };
385
+
386
+ /**
387
+ * Few-shot examples macro - provides learning examples
388
+ */
389
+ export const fewShotExamplesMacro: MacroDefinition = {
390
+ name: 'fewShotExamples',
391
+ description: 'Provide few-shot learning examples',
392
+ template: `## Examples
393
+
394
+ Here are some examples to guide your response:
395
+
396
+ {{#each examples}}
397
+ ### Example {{add @index 1}}
398
+
399
+ **Input:**
400
+ {{#codeBlock inputLanguage}}
401
+ {{input}}
402
+ {{/codeBlock}}
403
+
404
+ **Output:**
405
+ {{#codeBlock outputLanguage}}
406
+ {{output}}
407
+ {{/codeBlock}}
408
+
409
+ {{#ifDefined explanation}}
410
+ **Explanation:** {{explanation}}
411
+ {{/ifDefined}}
412
+
413
+ ---
414
+ {{/each}}`,
415
+ parameters: [
416
+ {
417
+ name: 'examples',
418
+ description: 'Array of input/output examples',
419
+ type: 'array',
420
+ required: true,
421
+ },
422
+ {
423
+ name: 'inputLanguage',
424
+ description: 'Language for input code blocks',
425
+ type: 'string',
426
+ required: false,
427
+ default: '',
428
+ },
429
+ {
430
+ name: 'outputLanguage',
431
+ description: 'Language for output code blocks',
432
+ type: 'string',
433
+ required: false,
434
+ default: '',
435
+ },
436
+ ],
437
+ example:
438
+ '{{> fewShotExamples examples=examples inputLanguage="text" outputLanguage="json" }}',
439
+ };
440
+
441
+ /**
442
+ * Safety guardrails macro - adds safety constraints
443
+ */
444
+ export const safetyGuardrailsMacro: MacroDefinition = {
445
+ name: 'safetyGuardrails',
446
+ description: 'Add safety constraints and guardrails',
447
+ template: `## Safety Guidelines
448
+
449
+ {{#ifDefined level}}
450
+ **Safety Level:** {{uppercase level}}
451
+ {{/ifDefined}}
452
+
453
+ You must adhere to these safety guidelines:
454
+
455
+ {{#ifDefined allowed}}
456
+ ### Allowed Actions
457
+ {{bulletList allowed}}
458
+ {{/ifDefined}}
459
+
460
+ {{#ifDefined prohibited}}
461
+ ### Prohibited Actions
462
+ {{bulletList prohibited}}
463
+ {{/ifDefined}}
464
+
465
+ {{#ifDefined responseGuidelines}}
466
+ ### Response Guidelines
467
+ {{bulletList responseGuidelines}}
468
+ {{/ifDefined}}
469
+
470
+ {{#compare level "eq" "strict"}}
471
+ If a request violates any guideline, politely decline and explain why.
472
+ {{/compare}}`,
473
+ parameters: [
474
+ {
475
+ name: 'level',
476
+ description: 'Safety level (strict, moderate, relaxed)',
477
+ type: 'string',
478
+ required: false,
479
+ default: 'moderate',
480
+ },
481
+ {
482
+ name: 'allowed',
483
+ description: 'List of allowed actions',
484
+ type: 'array',
485
+ required: false,
486
+ },
487
+ {
488
+ name: 'prohibited',
489
+ description: 'List of prohibited actions',
490
+ type: 'array',
491
+ required: false,
492
+ },
493
+ {
494
+ name: 'responseGuidelines',
495
+ description: 'Guidelines for responses',
496
+ type: 'array',
497
+ required: false,
498
+ },
499
+ ],
500
+ example:
501
+ '{{> safetyGuardrails level="strict" prohibited=(array "Generate harmful content" "Share personal data") }}',
502
+ };
503
+
504
+ /**
505
+ * Persona definition macro - creates a detailed persona
506
+ */
507
+ export const personaMacro: MacroDefinition = {
508
+ name: 'persona',
509
+ description: 'Define a detailed AI persona',
510
+ template: `# Persona: {{name}}
511
+
512
+ {{#ifDefined tagline}}
513
+ *{{tagline}}*
514
+ {{/ifDefined}}
515
+
516
+ ## About
517
+ {{description}}
518
+
519
+ {{#ifDefined background}}
520
+ ## Background
521
+ {{background}}
522
+ {{/ifDefined}}
523
+
524
+ {{#ifDefined skills}}
525
+ ## Skills & Expertise
526
+ {{bulletList skills}}
527
+ {{/ifDefined}}
528
+
529
+ {{#ifDefined communication}}
530
+ ## Communication Style
531
+ {{bulletList communication}}
532
+ {{/ifDefined}}
533
+
534
+ {{#ifDefined knowledge}}
535
+ ## Knowledge Base
536
+ {{bulletList knowledge}}
537
+ {{/ifDefined}}
538
+
539
+ {{#ifDefined limitations}}
540
+ ## Limitations
541
+ {{bulletList limitations}}
542
+ {{/ifDefined}}`,
543
+ parameters: [
544
+ {
545
+ name: 'name',
546
+ description: 'Persona name',
547
+ type: 'string',
548
+ required: true,
549
+ },
550
+ {
551
+ name: 'description',
552
+ description: 'Short description',
553
+ type: 'string',
554
+ required: true,
555
+ },
556
+ {
557
+ name: 'tagline',
558
+ description: 'Persona tagline',
559
+ type: 'string',
560
+ required: false,
561
+ },
562
+ {
563
+ name: 'background',
564
+ description: 'Background information',
565
+ type: 'string',
566
+ required: false,
567
+ },
568
+ {
569
+ name: 'skills',
570
+ description: 'List of skills',
571
+ type: 'array',
572
+ required: false,
573
+ },
574
+ {
575
+ name: 'communication',
576
+ description: 'Communication style traits',
577
+ type: 'array',
578
+ required: false,
579
+ },
580
+ {
581
+ name: 'knowledge',
582
+ description: 'Knowledge domains',
583
+ type: 'array',
584
+ required: false,
585
+ },
586
+ {
587
+ name: 'limitations',
588
+ description: 'Known limitations',
589
+ type: 'array',
590
+ required: false,
591
+ },
592
+ ],
593
+ example:
594
+ '{{> persona name="CodeBot" description="A helpful coding assistant" skills=(array "JavaScript" "Python" "TypeScript") }}',
595
+ };
596
+
597
+ /**
598
+ * Get all built-in macro definitions
599
+ *
600
+ * @returns Array of macro definitions
601
+ */
602
+ export function getBuiltinMacros(): MacroDefinition[] {
603
+ return [
604
+ systemRoleMacro,
605
+ taskContextMacro,
606
+ outputFormatMacro,
607
+ conversationHistoryMacro,
608
+ toolsSectionMacro,
609
+ codeContextMacro,
610
+ chainOfThoughtMacro,
611
+ fewShotExamplesMacro,
612
+ safetyGuardrailsMacro,
613
+ personaMacro,
614
+ ];
615
+ }
616
+
617
+ /**
618
+ * Get a macro by name
619
+ *
620
+ * @param name - Macro name
621
+ * @returns Macro definition or undefined
622
+ */
623
+ export function getMacroByName(name: string): MacroDefinition | undefined {
624
+ const macros = getBuiltinMacros();
625
+ return macros.find(macro => macro.name === name);
626
+ }
627
+
628
+ /**
629
+ * Get macro names
630
+ *
631
+ * @returns Array of macro names
632
+ */
633
+ export function getMacroNames(): string[] {
634
+ return getBuiltinMacros().map(macro => macro.name);
635
+ }