@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/types.ts ADDED
@@ -0,0 +1,380 @@
1
+ /**
2
+ * @wundr/prompt-templates - Type definitions for dynamic prompt templating
3
+ */
4
+
5
+ import { z } from 'zod';
6
+
7
+ /**
8
+ * JSON-compatible primitive types
9
+ */
10
+ export type JsonPrimitive = string | number | boolean | null;
11
+
12
+ /**
13
+ * JSON-compatible value types
14
+ */
15
+ export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
16
+
17
+ /**
18
+ * JSON-compatible object type
19
+ */
20
+ export interface JsonObject {
21
+ readonly [key: string]: JsonValue;
22
+ }
23
+
24
+ /**
25
+ * JSON-compatible array type
26
+ */
27
+ export type JsonArray = ReadonlyArray<JsonValue>;
28
+
29
+ /**
30
+ * Context data passed to templates for rendering
31
+ */
32
+ export interface TemplateContext {
33
+ /** Variables available in the template */
34
+ readonly variables: Record<string, unknown>;
35
+ /** System-level context (e.g., current date, user info) */
36
+ readonly system?: SystemContext;
37
+ /** Memory/history context for conversation-aware templates */
38
+ readonly memory?: MemoryContext;
39
+ /** Tools/functions available to the AI */
40
+ readonly tools?: ToolDefinition[];
41
+ /** Additional metadata */
42
+ readonly metadata?: Record<string, unknown>;
43
+ }
44
+
45
+ /**
46
+ * System-level context available to all templates
47
+ */
48
+ export interface SystemContext {
49
+ /** Current timestamp */
50
+ readonly timestamp?: Date;
51
+ /** User identifier */
52
+ readonly userId?: string;
53
+ /** Session identifier */
54
+ readonly sessionId?: string;
55
+ /** Environment (development, production, test) */
56
+ readonly environment?: 'development' | 'production' | 'test';
57
+ /** Model being used */
58
+ readonly model?: string;
59
+ /** Custom system values */
60
+ readonly [key: string]: unknown;
61
+ }
62
+
63
+ /**
64
+ * Memory context for conversation history
65
+ */
66
+ export interface MemoryContext {
67
+ /** Previous messages in the conversation */
68
+ readonly messages?: ConversationMessage[];
69
+ /** Key facts/context to remember */
70
+ readonly facts?: string[];
71
+ /** Short-term working memory */
72
+ readonly shortTerm?: Record<string, unknown>;
73
+ /** Long-term persistent memory */
74
+ readonly longTerm?: Record<string, unknown>;
75
+ }
76
+
77
+ /**
78
+ * Conversation message structure
79
+ */
80
+ export interface ConversationMessage {
81
+ /** Role of the message sender */
82
+ readonly role: 'system' | 'user' | 'assistant' | 'tool';
83
+ /** Message content */
84
+ readonly content: string;
85
+ /** Optional message name */
86
+ readonly name?: string;
87
+ /** Timestamp when message was sent */
88
+ readonly timestamp?: Date;
89
+ /** Additional message metadata */
90
+ readonly metadata?: Record<string, unknown>;
91
+ }
92
+
93
+ /**
94
+ * Tool/function definition for AI prompts
95
+ */
96
+ export interface ToolDefinition {
97
+ /** Tool name */
98
+ readonly name: string;
99
+ /** Tool description */
100
+ readonly description: string;
101
+ /** Input parameters schema */
102
+ readonly parameters?: ToolParameters;
103
+ /** Whether the tool returns data */
104
+ readonly returnsData?: boolean;
105
+ /** Example usage */
106
+ readonly examples?: string[];
107
+ }
108
+
109
+ /**
110
+ * Tool parameters schema (JSON Schema-like)
111
+ */
112
+ export interface ToolParameters {
113
+ /** Parameter type */
114
+ readonly type: 'object' | 'array' | 'string' | 'number' | 'boolean';
115
+ /** Object properties */
116
+ readonly properties?: Record<string, ToolParameterProperty>;
117
+ /** Required property names */
118
+ readonly required?: string[];
119
+ /** Additional properties allowed */
120
+ readonly additionalProperties?: boolean;
121
+ }
122
+
123
+ /**
124
+ * Individual tool parameter property
125
+ */
126
+ export interface ToolParameterProperty {
127
+ /** Property type */
128
+ readonly type: 'string' | 'number' | 'boolean' | 'array' | 'object';
129
+ /** Property description */
130
+ readonly description?: string;
131
+ /** Enum values for constrained types */
132
+ readonly enum?: readonly (string | number)[];
133
+ /** Default value */
134
+ readonly default?: unknown;
135
+ /** Array item schema */
136
+ readonly items?: ToolParameterProperty;
137
+ }
138
+
139
+ /**
140
+ * Prompt template configuration
141
+ */
142
+ export interface PromptTemplateConfig {
143
+ /** Unique template identifier */
144
+ readonly id: string;
145
+ /** Template name for display */
146
+ readonly name: string;
147
+ /** Template description */
148
+ readonly description?: string;
149
+ /** Template version */
150
+ readonly version: string;
151
+ /** The template content (Handlebars format) */
152
+ readonly template: string;
153
+ /** Default context values */
154
+ readonly defaults?: Partial<TemplateContext>;
155
+ /** Required variables that must be provided */
156
+ readonly requiredVariables?: string[];
157
+ /** Template tags for categorization */
158
+ readonly tags?: string[];
159
+ /** Template author */
160
+ readonly author?: string;
161
+ /** Creation timestamp */
162
+ readonly createdAt?: Date;
163
+ /** Last update timestamp */
164
+ readonly updatedAt?: Date;
165
+ }
166
+
167
+ /**
168
+ * Macro definition for reusable template components
169
+ */
170
+ export interface MacroDefinition {
171
+ /** Macro name */
172
+ readonly name: string;
173
+ /** Macro description */
174
+ readonly description?: string;
175
+ /** Macro template content */
176
+ readonly template: string;
177
+ /** Expected parameters */
178
+ readonly parameters?: MacroParameter[];
179
+ /** Example usage */
180
+ readonly example?: string;
181
+ }
182
+
183
+ /**
184
+ * Macro parameter definition
185
+ */
186
+ export interface MacroParameter {
187
+ /** Parameter name */
188
+ readonly name: string;
189
+ /** Parameter description */
190
+ readonly description?: string;
191
+ /** Parameter type */
192
+ readonly type?: 'string' | 'number' | 'boolean' | 'array' | 'object';
193
+ /** Whether the parameter is required */
194
+ readonly required?: boolean;
195
+ /** Default value */
196
+ readonly default?: unknown;
197
+ }
198
+
199
+ /**
200
+ * Helper function definition
201
+ */
202
+ export interface HelperDefinition {
203
+ /** Helper name */
204
+ readonly name: string;
205
+ /** Helper description */
206
+ readonly description?: string;
207
+ /** The helper function implementation */
208
+ readonly fn: HelperFunction;
209
+ }
210
+
211
+ /**
212
+ * Helper function signature (Handlebars-compatible)
213
+ */
214
+ export type HelperFunction = (
215
+ ...args: unknown[]
216
+ ) => string | SafeString | undefined;
217
+
218
+ /**
219
+ * Handlebars SafeString type for HTML-safe output
220
+ */
221
+ export interface SafeString {
222
+ readonly toHTML: () => string;
223
+ readonly toString: () => string;
224
+ }
225
+
226
+ /**
227
+ * Template render options
228
+ */
229
+ export interface RenderOptions {
230
+ /** Strict mode - throw on undefined variables */
231
+ readonly strict?: boolean;
232
+ /** Custom delimiters for template parsing */
233
+ readonly delimiters?: [string, string];
234
+ /** Enable HTML escaping (default: false for prompts) */
235
+ readonly escapeHtml?: boolean;
236
+ /** Maximum template depth for nested templates */
237
+ readonly maxDepth?: number;
238
+ /** Timeout for template rendering (ms) */
239
+ readonly timeout?: number;
240
+ }
241
+
242
+ /**
243
+ * Template render result
244
+ */
245
+ export interface RenderResult {
246
+ /** Whether rendering was successful */
247
+ readonly success: boolean;
248
+ /** Rendered output (if successful) */
249
+ readonly output?: string;
250
+ /** Error information (if failed) */
251
+ readonly error?: TemplateError;
252
+ /** Rendering metadata */
253
+ readonly metadata?: RenderMetadata;
254
+ }
255
+
256
+ /**
257
+ * Template error information
258
+ */
259
+ export interface TemplateError {
260
+ /** Error code */
261
+ readonly code: string;
262
+ /** Error message */
263
+ readonly message: string;
264
+ /** Line number where error occurred */
265
+ readonly line?: number;
266
+ /** Column number where error occurred */
267
+ readonly column?: number;
268
+ /** Template ID that caused the error */
269
+ readonly templateId?: string;
270
+ /** Original error stack */
271
+ readonly stack?: string;
272
+ }
273
+
274
+ /**
275
+ * Metadata about the render operation
276
+ */
277
+ export interface RenderMetadata {
278
+ /** Time taken to render (ms) */
279
+ readonly renderTime: number;
280
+ /** Template ID used */
281
+ readonly templateId?: string;
282
+ /** Variables that were used */
283
+ readonly usedVariables?: string[];
284
+ /** Macros that were invoked */
285
+ readonly usedMacros?: string[];
286
+ /** Helpers that were invoked */
287
+ readonly usedHelpers?: string[];
288
+ }
289
+
290
+ /**
291
+ * Template loader options
292
+ */
293
+ export interface LoaderOptions {
294
+ /** Base directory for template files */
295
+ readonly baseDir?: string;
296
+ /** File extension for template files */
297
+ readonly extension?: string;
298
+ /** Enable template caching */
299
+ readonly cache?: boolean;
300
+ /** Watch for file changes */
301
+ readonly watch?: boolean;
302
+ }
303
+
304
+ /**
305
+ * Template engine events
306
+ */
307
+ export interface EngineEvents {
308
+ readonly 'template:loaded': { templateId: string };
309
+ readonly 'template:rendered': { templateId: string; renderTime: number };
310
+ readonly 'template:error': { templateId: string; error: TemplateError };
311
+ readonly 'macro:registered': { name: string };
312
+ readonly 'helper:registered': { name: string };
313
+ }
314
+
315
+ /**
316
+ * Template engine event handler
317
+ */
318
+ export type EngineEventHandler<K extends keyof EngineEvents> = (
319
+ event: EngineEvents[K]
320
+ ) => void;
321
+
322
+ // Zod schemas for validation
323
+
324
+ /**
325
+ * Zod schema for PromptTemplateConfig
326
+ */
327
+ export const PromptTemplateConfigSchema = z.object({
328
+ id: z.string().min(1),
329
+ name: z.string().min(1),
330
+ description: z.string().optional(),
331
+ version: z.string().regex(/^\d+\.\d+\.\d+$/),
332
+ template: z.string().min(1),
333
+ defaults: z.record(z.unknown()).optional(),
334
+ requiredVariables: z.array(z.string()).optional(),
335
+ tags: z.array(z.string()).optional(),
336
+ author: z.string().optional(),
337
+ createdAt: z.date().optional(),
338
+ updatedAt: z.date().optional(),
339
+ });
340
+
341
+ /**
342
+ * Zod schema for ToolDefinition
343
+ */
344
+ export const ToolDefinitionSchema = z.object({
345
+ name: z.string().min(1),
346
+ description: z.string().min(1),
347
+ parameters: z
348
+ .object({
349
+ type: z.enum(['object', 'array', 'string', 'number', 'boolean']),
350
+ properties: z.record(z.unknown()).optional(),
351
+ required: z.array(z.string()).optional(),
352
+ additionalProperties: z.boolean().optional(),
353
+ })
354
+ .optional(),
355
+ returnsData: z.boolean().optional(),
356
+ examples: z.array(z.string()).optional(),
357
+ });
358
+
359
+ /**
360
+ * Zod schema for MacroDefinition
361
+ */
362
+ export const MacroDefinitionSchema = z.object({
363
+ name: z.string().min(1),
364
+ description: z.string().optional(),
365
+ template: z.string().min(1),
366
+ parameters: z
367
+ .array(
368
+ z.object({
369
+ name: z.string().min(1),
370
+ description: z.string().optional(),
371
+ type: z
372
+ .enum(['string', 'number', 'boolean', 'array', 'object'])
373
+ .optional(),
374
+ required: z.boolean().optional(),
375
+ default: z.unknown().optional(),
376
+ }),
377
+ )
378
+ .optional(),
379
+ example: z.string().optional(),
380
+ });