@hazeljs/prompts 0.2.0-alpha.1
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/LICENSE +192 -0
- package/README.md +461 -0
- package/dist/__tests__/registry.test.d.ts +2 -0
- package/dist/__tests__/registry.test.d.ts.map +1 -0
- package/dist/__tests__/registry.test.js +255 -0
- package/dist/__tests__/registry.test.js.map +1 -0
- package/dist/__tests__/stores/file.store.test.d.ts +2 -0
- package/dist/__tests__/stores/file.store.test.d.ts.map +1 -0
- package/dist/__tests__/stores/file.store.test.js +134 -0
- package/dist/__tests__/stores/file.store.test.js.map +1 -0
- package/dist/__tests__/stores/memory.store.test.d.ts +2 -0
- package/dist/__tests__/stores/memory.store.test.d.ts.map +1 -0
- package/dist/__tests__/stores/memory.store.test.js +74 -0
- package/dist/__tests__/stores/memory.store.test.js.map +1 -0
- package/dist/__tests__/stores/multi.store.test.d.ts +2 -0
- package/dist/__tests__/stores/multi.store.test.d.ts.map +1 -0
- package/dist/__tests__/stores/multi.store.test.js +81 -0
- package/dist/__tests__/stores/multi.store.test.js.map +1 -0
- package/dist/__tests__/template.test.d.ts +2 -0
- package/dist/__tests__/template.test.d.ts.map +1 -0
- package/dist/__tests__/template.test.js +60 -0
- package/dist/__tests__/template.test.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/registry.d.ts +153 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +288 -0
- package/dist/registry.js.map +1 -0
- package/dist/stores/database.store.d.ts +93 -0
- package/dist/stores/database.store.d.ts.map +1 -0
- package/dist/stores/database.store.js +46 -0
- package/dist/stores/database.store.js.map +1 -0
- package/dist/stores/file.store.d.ts +49 -0
- package/dist/stores/file.store.d.ts.map +1 -0
- package/dist/stores/file.store.js +105 -0
- package/dist/stores/file.store.js.map +1 -0
- package/dist/stores/index.d.ts +10 -0
- package/dist/stores/index.d.ts.map +1 -0
- package/dist/stores/index.js +14 -0
- package/dist/stores/index.js.map +1 -0
- package/dist/stores/memory.store.d.ts +21 -0
- package/dist/stores/memory.store.d.ts.map +1 -0
- package/dist/stores/memory.store.js +63 -0
- package/dist/stores/memory.store.js.map +1 -0
- package/dist/stores/multi.store.d.ts +44 -0
- package/dist/stores/multi.store.d.ts.map +1 -0
- package/dist/stores/multi.store.js +68 -0
- package/dist/stores/multi.store.js.map +1 -0
- package/dist/stores/redis.store.d.ts +65 -0
- package/dist/stores/redis.store.d.ts.map +1 -0
- package/dist/stores/redis.store.js +81 -0
- package/dist/stores/redis.store.js.map +1 -0
- package/dist/stores/store.interface.d.ts +62 -0
- package/dist/stores/store.interface.d.ts.map +1 -0
- package/dist/stores/store.interface.js +3 -0
- package/dist/stores/store.interface.js.map +1 -0
- package/dist/template.d.ts +38 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +46 -0
- package/dist/template.js.map +1 -0
- package/dist/types.d.ts +15 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
Copyright 2024 HazelJS Team
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
22
|
+
|
|
23
|
+
1. Definitions.
|
|
24
|
+
|
|
25
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
26
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
27
|
+
|
|
28
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
29
|
+
the copyright owner that is granting the License.
|
|
30
|
+
|
|
31
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
32
|
+
other entities that control, are controlled by, or are under common
|
|
33
|
+
control with that entity. For the purposes of this definition,
|
|
34
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
35
|
+
direction or management of such entity, whether by contract or
|
|
36
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
37
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
38
|
+
|
|
39
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
40
|
+
exercising permissions granted by this License.
|
|
41
|
+
|
|
42
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
43
|
+
including but not limited to software source code, documentation
|
|
44
|
+
source, and configuration files.
|
|
45
|
+
|
|
46
|
+
"Object" form shall mean any form resulting from mechanical
|
|
47
|
+
transformation or translation of a Source form, including but
|
|
48
|
+
not limited to compiled object code, generated documentation,
|
|
49
|
+
and conversions to other media types.
|
|
50
|
+
|
|
51
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
52
|
+
Object form, made available under the License, as indicated by a
|
|
53
|
+
copyright notice that is included in or attached to the work
|
|
54
|
+
(an example is provided in the Appendix below).
|
|
55
|
+
|
|
56
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
57
|
+
form, that is based on (or derived from) the Work and for which the
|
|
58
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
59
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
60
|
+
of this License, Derivative Works shall not include works that remain
|
|
61
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
62
|
+
the Work and Derivative Works thereof.
|
|
63
|
+
|
|
64
|
+
"Contribution" shall mean any work of authorship, including
|
|
65
|
+
the original version of the Work and any modifications or additions
|
|
66
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
67
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
68
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
69
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
70
|
+
means any form of electronic, verbal, or written communication sent
|
|
71
|
+
to the Licensor or its representatives, including but not limited to
|
|
72
|
+
communication on electronic mailing lists, source code control systems,
|
|
73
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
74
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
75
|
+
excluding communication that is conspicuously marked or otherwise
|
|
76
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
77
|
+
|
|
78
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
79
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
80
|
+
subsequently incorporated within the Work.
|
|
81
|
+
|
|
82
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
83
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
84
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
85
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
86
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
87
|
+
Work and such Derivative Works in Source or Object form.
|
|
88
|
+
|
|
89
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
90
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
91
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
92
|
+
(except as stated in this section) patent license to make, have made,
|
|
93
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
94
|
+
where such license applies only to those patent claims licensable
|
|
95
|
+
by such Contributor that are necessarily infringed by their
|
|
96
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
97
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
98
|
+
institute patent litigation against any entity (including a
|
|
99
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
100
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
101
|
+
or contributory patent infringement, then any patent licenses
|
|
102
|
+
granted to You under this License for that Work shall terminate
|
|
103
|
+
as of the date such litigation is filed.
|
|
104
|
+
|
|
105
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
106
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
107
|
+
modifications, and in Source or Object form, provided that You
|
|
108
|
+
meet the following conditions:
|
|
109
|
+
|
|
110
|
+
(a) You must give any other recipients of the Work or
|
|
111
|
+
Derivative Works a copy of this License; and
|
|
112
|
+
|
|
113
|
+
(b) You must cause any modified files to carry prominent notices
|
|
114
|
+
stating that You changed the files; and
|
|
115
|
+
|
|
116
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
117
|
+
that You distribute, all copyright, patent, trademark, and
|
|
118
|
+
attribution notices from the Source form of the Work,
|
|
119
|
+
excluding those notices that do not pertain to any part of
|
|
120
|
+
the Derivative Works; and
|
|
121
|
+
|
|
122
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
123
|
+
distribution, then any Derivative Works that You distribute must
|
|
124
|
+
include a readable copy of the attribution notices contained
|
|
125
|
+
within such NOTICE file, excluding those notices that do not
|
|
126
|
+
pertain to any part of the Derivative Works, in at least one
|
|
127
|
+
of the following places: within a NOTICE text file distributed
|
|
128
|
+
as part of the Derivative Works; within the Source form or
|
|
129
|
+
documentation, if provided along with the Derivative Works; or,
|
|
130
|
+
within a display generated by the Derivative Works, if and
|
|
131
|
+
wherever such third-party notices normally appear. The contents
|
|
132
|
+
of the NOTICE file are for informational purposes only and
|
|
133
|
+
do not modify the License. You may add Your own attribution
|
|
134
|
+
notices within Derivative Works that You distribute, alongside
|
|
135
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
136
|
+
that such additional attribution notices cannot be construed
|
|
137
|
+
as modifying the License.
|
|
138
|
+
|
|
139
|
+
You may add Your own copyright statement to Your modifications and
|
|
140
|
+
may provide additional or different license terms and conditions
|
|
141
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
142
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
143
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
144
|
+
the conditions stated in this License.
|
|
145
|
+
|
|
146
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
147
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
148
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
149
|
+
this License, without any additional terms or conditions.
|
|
150
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
151
|
+
the terms of any separate license agreement you may have executed
|
|
152
|
+
with Licensor regarding such Contributions.
|
|
153
|
+
|
|
154
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
155
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
156
|
+
except as required for reasonable and customary use in describing the
|
|
157
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
158
|
+
|
|
159
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
160
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
161
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
162
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
163
|
+
implied, including, without limitation, any warranties or conditions
|
|
164
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
165
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
166
|
+
appropriateness of using or redistributing the Work and assume any
|
|
167
|
+
risks associated with Your exercise of permissions under this License.
|
|
168
|
+
|
|
169
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
170
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
171
|
+
unless required by applicable law (such as deliberate and grossly
|
|
172
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
173
|
+
liable to You for damages, including any direct, indirect, special,
|
|
174
|
+
incidental, or consequential damages of any character arising as a
|
|
175
|
+
result of this License or out of the use or inability to use the
|
|
176
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
177
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
178
|
+
other commercial damages or losses), even if such Contributor
|
|
179
|
+
has been advised of the possibility of such damages.
|
|
180
|
+
|
|
181
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
182
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
183
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
184
|
+
or other liability obligations and/or rights consistent with this
|
|
185
|
+
License. However, in accepting such obligations, You may act only
|
|
186
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
187
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
188
|
+
defend, and hold each Contributor harmless for any liability
|
|
189
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
190
|
+
of your accepting any such warranty or additional liability.
|
|
191
|
+
|
|
192
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
# @hazeljs/prompts
|
|
2
|
+
|
|
3
|
+
**Centralized, versioned prompt management for HazelJS AI, RAG, and Agent packages.**
|
|
4
|
+
|
|
5
|
+
Define typed prompt templates with named `{variable}` placeholders, store them in a global registry, and swap any prompt at startup — without touching the code that uses them.
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@hazeljs/prompts)
|
|
8
|
+
[](https://www.npmjs.com/package/@hazeljs/prompts)
|
|
9
|
+
[](https://www.apache.org/licenses/LICENSE-2.0)
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Typed templates** — `PromptTemplate<{ var1, var2 }>` enforces the variable shape at compile time
|
|
14
|
+
- **Global registry** — `PromptRegistry.register()` at import time; `get()` anywhere, zero overhead
|
|
15
|
+
- **Override any prompt** — swap built-in `@hazeljs/agent` and `@hazeljs/rag` prompts at startup with `override()`
|
|
16
|
+
- **Versioning** — every template carries a `version`; retrieve a specific version with `get(key, version)`
|
|
17
|
+
- **5 storage backends** — Memory, File, Redis, Database (generic adapter), MultiStore (fan-out)
|
|
18
|
+
- **Hot-swap** — update prompts in Redis/DB and reload without restarting the process
|
|
19
|
+
- **Zero runtime dependencies** — the core package has no production dependencies
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @hazeljs/prompts
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Optional peer dependencies for store backends:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm install ioredis # RedisStore
|
|
33
|
+
npm install @prisma/client # DatabaseStore with Prisma
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## Quick Start
|
|
39
|
+
|
|
40
|
+
### 1. Define and register a template
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { PromptTemplate, PromptRegistry } from '@hazeljs/prompts';
|
|
44
|
+
|
|
45
|
+
const answerPrompt = new PromptTemplate<{ context: string; question: string }>(
|
|
46
|
+
`Answer the question using only the context below. Be concise.
|
|
47
|
+
|
|
48
|
+
Context: {context}
|
|
49
|
+
|
|
50
|
+
Question: {question}
|
|
51
|
+
|
|
52
|
+
Answer:`,
|
|
53
|
+
{ name: 'RAG Answer', version: '1.0.0' },
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
// Register under a namespaced key — safe to call at module load time
|
|
57
|
+
PromptRegistry.register('myapp:rag:answer', answerPrompt);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 2. Render at runtime
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import { PromptRegistry } from '@hazeljs/prompts';
|
|
64
|
+
|
|
65
|
+
const tpl = PromptRegistry.get<{ context: string; question: string }>('myapp:rag:answer');
|
|
66
|
+
|
|
67
|
+
const prompt = tpl.render({
|
|
68
|
+
context: 'HazelJS is a TypeScript-first framework for AI-native applications.',
|
|
69
|
+
question: 'What is HazelJS?',
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Pass `prompt` to your LLM of choice
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 3. Override at startup
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { PromptRegistry, PromptTemplate } from '@hazeljs/prompts';
|
|
79
|
+
|
|
80
|
+
// Runs before any agent or RAG pipeline is created
|
|
81
|
+
PromptRegistry.override(
|
|
82
|
+
'myapp:rag:answer',
|
|
83
|
+
new PromptTemplate<{ context: string; question: string }>(
|
|
84
|
+
`You are a helpful assistant. Use the context to answer.\nContext: {context}\nQ: {question}\nA:`,
|
|
85
|
+
{ name: 'Custom Answer', version: '2.0.0' },
|
|
86
|
+
),
|
|
87
|
+
);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## PromptTemplate
|
|
93
|
+
|
|
94
|
+
`PromptTemplate<TVariables>` is the core primitive. It holds a template string and metadata, and exposes a single `.render()` method.
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import { PromptTemplate } from '@hazeljs/prompts';
|
|
98
|
+
|
|
99
|
+
// Typed — TypeScript enforces the variable shape
|
|
100
|
+
const tpl = new PromptTemplate<{ name: string; tier: string }>(
|
|
101
|
+
'Hello {name}, you are on the {tier} plan.',
|
|
102
|
+
{ name: 'Welcome Message', version: '1.0.0' },
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
const text = tpl.render({ name: 'Alice', tier: 'pro' });
|
|
106
|
+
// "Hello Alice, you are on the pro plan."
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
**Placeholder rules:**
|
|
110
|
+
- Syntax: `{variableName}` (alphanumeric + underscore)
|
|
111
|
+
- Missing variable → placeholder left as-is (`{missing}` stays `{missing}`)
|
|
112
|
+
- Extra variables in `.render()` are silently ignored
|
|
113
|
+
|
|
114
|
+
### PromptMetadata
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
interface PromptMetadata {
|
|
118
|
+
name: string; // Human-readable display name
|
|
119
|
+
version?: string; // Semver string — enables get(key, version)
|
|
120
|
+
description?: string; // Optional description
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## PromptRegistry
|
|
127
|
+
|
|
128
|
+
A global static class — no instantiation needed. Prompts registered in one module are immediately available across the entire process.
|
|
129
|
+
|
|
130
|
+
### Key naming convention
|
|
131
|
+
|
|
132
|
+
Use a colon-separated `package:scope:action` scheme to avoid collisions:
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
rag:graph:entity-extraction
|
|
136
|
+
agent:supervisor:routing
|
|
137
|
+
myapp:checkout:upsell-prompt
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Sync API
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
import { PromptRegistry } from '@hazeljs/prompts';
|
|
144
|
+
|
|
145
|
+
// Register (no-op if key already exists — safe for default prompts)
|
|
146
|
+
PromptRegistry.register('myapp:qa:answer', template);
|
|
147
|
+
|
|
148
|
+
// Override (always replaces — use at application startup)
|
|
149
|
+
PromptRegistry.override('myapp:qa:answer', customTemplate);
|
|
150
|
+
|
|
151
|
+
// Get latest version (throws if not registered)
|
|
152
|
+
const tpl = PromptRegistry.get('myapp:qa:answer');
|
|
153
|
+
|
|
154
|
+
// Get a specific version
|
|
155
|
+
const v1 = PromptRegistry.get('myapp:qa:answer', '1.0.0');
|
|
156
|
+
|
|
157
|
+
// Check existence
|
|
158
|
+
PromptRegistry.has('myapp:qa:answer'); // → boolean
|
|
159
|
+
PromptRegistry.has('myapp:qa:answer', '1.0.0'); // → boolean for version
|
|
160
|
+
|
|
161
|
+
// List all registered keys
|
|
162
|
+
PromptRegistry.list(); // → string[]
|
|
163
|
+
|
|
164
|
+
// List all cached versions for a key
|
|
165
|
+
PromptRegistry.versions('myapp:qa:answer'); // → ['1.0.0', '2.0.0']
|
|
166
|
+
|
|
167
|
+
// Remove a prompt (useful in tests)
|
|
168
|
+
PromptRegistry.unregister('myapp:qa:answer');
|
|
169
|
+
PromptRegistry.unregister('myapp:qa:answer', '1.0.0'); // specific version only
|
|
170
|
+
|
|
171
|
+
// Clear all (tests only)
|
|
172
|
+
PromptRegistry.clear();
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Async Store API
|
|
176
|
+
|
|
177
|
+
Use these when store backends are configured:
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
// Load from store, falling back through configured stores in order
|
|
181
|
+
const tpl = await PromptRegistry.getAsync('myapp:qa:answer');
|
|
182
|
+
const tplV2 = await PromptRegistry.getAsync('myapp:qa:answer', '2.0.0');
|
|
183
|
+
|
|
184
|
+
// Persist a single prompt to all configured stores
|
|
185
|
+
await PromptRegistry.save('myapp:qa:answer');
|
|
186
|
+
|
|
187
|
+
// Persist all registered prompts
|
|
188
|
+
await PromptRegistry.saveAll();
|
|
189
|
+
|
|
190
|
+
// Load all prompts from the primary store into the cache
|
|
191
|
+
await PromptRegistry.loadAll(); // does not overwrite existing cache entries
|
|
192
|
+
await PromptRegistry.loadAll(true); // overwrite = true
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## Store Backends
|
|
198
|
+
|
|
199
|
+
### MemoryStore
|
|
200
|
+
|
|
201
|
+
In-memory only — useful for testing and explicit in-process prompt libraries:
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
import { MemoryStore, PromptRegistry } from '@hazeljs/prompts';
|
|
205
|
+
|
|
206
|
+
PromptRegistry.configure([new MemoryStore()]);
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### FileStore
|
|
210
|
+
|
|
211
|
+
Persists prompts to a JSON file on disk:
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
import { FileStore, PromptRegistry } from '@hazeljs/prompts';
|
|
215
|
+
|
|
216
|
+
PromptRegistry.configure([new FileStore({ filePath: './prompts/library.json' })]);
|
|
217
|
+
|
|
218
|
+
await PromptRegistry.saveAll(); // write to disk
|
|
219
|
+
await PromptRegistry.loadAll(); // read from disk on startup
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### RedisStore
|
|
223
|
+
|
|
224
|
+
Stores prompts in Redis — ideal for multi-instance deployments and hot-swap:
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
import Redis from 'ioredis';
|
|
228
|
+
import { RedisStore, PromptRegistry } from '@hazeljs/prompts';
|
|
229
|
+
|
|
230
|
+
const redis = new Redis({ host: 'localhost', port: 6379 });
|
|
231
|
+
|
|
232
|
+
PromptRegistry.configure([
|
|
233
|
+
new RedisStore({ client: redis, keyPrefix: 'hazel:prompts:' }),
|
|
234
|
+
]);
|
|
235
|
+
|
|
236
|
+
// Load on startup
|
|
237
|
+
await PromptRegistry.loadAll();
|
|
238
|
+
|
|
239
|
+
// Hot-swap: update a prompt and push it to Redis without restarting
|
|
240
|
+
PromptRegistry.override('myapp:qa:answer', updatedTemplate);
|
|
241
|
+
await PromptRegistry.save('myapp:qa:answer');
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### DatabaseStore
|
|
245
|
+
|
|
246
|
+
Stores prompts in any relational database via a generic adapter:
|
|
247
|
+
|
|
248
|
+
```typescript
|
|
249
|
+
import { DatabaseStore, PromptRegistry } from '@hazeljs/prompts';
|
|
250
|
+
import type { DatabaseAdapter, PromptEntry } from '@hazeljs/prompts';
|
|
251
|
+
|
|
252
|
+
// Implement the adapter for your ORM (Prisma example)
|
|
253
|
+
class PrismaPromptAdapter implements DatabaseAdapter {
|
|
254
|
+
constructor(private readonly prisma: PrismaClient) {}
|
|
255
|
+
|
|
256
|
+
async get(key: string, version?: string): Promise<PromptEntry | undefined> {
|
|
257
|
+
const row = await this.prisma.prompt.findFirst({ where: { key } });
|
|
258
|
+
if (!row) return undefined;
|
|
259
|
+
return { key: row.key, template: row.template, metadata: JSON.parse(row.metadata) };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async set(entry: PromptEntry): Promise<void> {
|
|
263
|
+
await this.prisma.prompt.upsert({
|
|
264
|
+
where: { key: entry.key },
|
|
265
|
+
create: { key: entry.key, template: entry.template, metadata: JSON.stringify(entry.metadata) },
|
|
266
|
+
update: { template: entry.template, metadata: JSON.stringify(entry.metadata) },
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async getAll(): Promise<PromptEntry[]> {
|
|
271
|
+
const rows = await this.prisma.prompt.findMany();
|
|
272
|
+
return rows.map(r => ({ key: r.key, template: r.template, metadata: JSON.parse(r.metadata) }));
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
PromptRegistry.configure([
|
|
277
|
+
new DatabaseStore({ adapter: new PrismaPromptAdapter(new PrismaClient()) }),
|
|
278
|
+
]);
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### MultiStore
|
|
282
|
+
|
|
283
|
+
Fan-out store that writes to all backends simultaneously and reads from the first that has the key. Use for high-availability (Redis primary + file fallback):
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
import { MultiStore, FileStore, RedisStore, PromptRegistry } from '@hazeljs/prompts';
|
|
287
|
+
import Redis from 'ioredis';
|
|
288
|
+
|
|
289
|
+
PromptRegistry.configure([
|
|
290
|
+
new MultiStore([
|
|
291
|
+
new RedisStore({ client: new Redis(process.env.REDIS_URL) }),
|
|
292
|
+
new FileStore({ filePath: './prompts/fallback.json' }),
|
|
293
|
+
]),
|
|
294
|
+
]);
|
|
295
|
+
|
|
296
|
+
await PromptRegistry.saveAll(); // writes to both stores
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### Configuring stores
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
// Replace all stores at once
|
|
303
|
+
PromptRegistry.configure([storeA, storeB]);
|
|
304
|
+
|
|
305
|
+
// Append without replacing
|
|
306
|
+
PromptRegistry.addStore(storeC);
|
|
307
|
+
|
|
308
|
+
// Inspect configured stores
|
|
309
|
+
PromptRegistry.storeNames(); // → ['RedisStore', 'FileStore']
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
---
|
|
313
|
+
|
|
314
|
+
## Overriding Built-In Package Prompts
|
|
315
|
+
|
|
316
|
+
`@hazeljs/agent` and `@hazeljs/rag` register their internal prompts under predictable keys. Override them at application startup to tune behaviour without forking:
|
|
317
|
+
|
|
318
|
+
```typescript
|
|
319
|
+
import { PromptRegistry, PromptTemplate } from '@hazeljs/prompts';
|
|
320
|
+
|
|
321
|
+
// Tune the GraphRAG entity extraction prompt for a legal document corpus
|
|
322
|
+
PromptRegistry.override(
|
|
323
|
+
'rag:graph:entity-extraction',
|
|
324
|
+
new PromptTemplate<{ text: string }>(
|
|
325
|
+
`Extract legal entities (parties, clauses, obligations, dates) from this text.
|
|
326
|
+
Return JSON: { entities: [...], relationships: [...] }
|
|
327
|
+
|
|
328
|
+
Text: {text}`,
|
|
329
|
+
{ name: 'Legal Entity Extraction', version: '1.0.0' },
|
|
330
|
+
),
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
// Customise the supervisor routing prompt used by SupervisorAgent
|
|
334
|
+
PromptRegistry.override(
|
|
335
|
+
'agent:supervisor:routing',
|
|
336
|
+
new PromptTemplate<{ task: string; workers: string }>(
|
|
337
|
+
`You are a project manager. Decompose the task and assign each subtask to the best worker.
|
|
338
|
+
Workers: {workers}
|
|
339
|
+
Task: {task}
|
|
340
|
+
Respond with JSON: [{ "worker": "...", "subtask": "..." }]`,
|
|
341
|
+
{ name: 'Custom Supervisor', version: '2.0.0' },
|
|
342
|
+
),
|
|
343
|
+
);
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
---
|
|
347
|
+
|
|
348
|
+
## Use Cases
|
|
349
|
+
|
|
350
|
+
### Prompt A/B testing
|
|
351
|
+
|
|
352
|
+
Register two versions and switch between them without a deploy:
|
|
353
|
+
|
|
354
|
+
```typescript
|
|
355
|
+
PromptRegistry.register('myapp:qa:answer', promptV1);
|
|
356
|
+
|
|
357
|
+
// Later — override with new version and save to Redis
|
|
358
|
+
PromptRegistry.override('myapp:qa:answer', promptV2);
|
|
359
|
+
await PromptRegistry.save('myapp:qa:answer');
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
### Multi-tenant prompts
|
|
363
|
+
|
|
364
|
+
Load tenant-specific prompts from the database at request time:
|
|
365
|
+
|
|
366
|
+
```typescript
|
|
367
|
+
async function getPromptForTenant(tenantId: string, key: string) {
|
|
368
|
+
const tenantKey = `tenant:${tenantId}:${key}`;
|
|
369
|
+
if (PromptRegistry.has(tenantKey)) return PromptRegistry.get(tenantKey);
|
|
370
|
+
return await PromptRegistry.getAsync(tenantKey); // falls back to DB store
|
|
371
|
+
}
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
### Exposing tools via MCP
|
|
375
|
+
|
|
376
|
+
Use registry-driven prompts inside `@Tool()` methods and expose them as MCP tools:
|
|
377
|
+
|
|
378
|
+
```typescript
|
|
379
|
+
import { PromptRegistry } from '@hazeljs/prompts';
|
|
380
|
+
import { Tool, ToolRegistry } from '@hazeljs/agent';
|
|
381
|
+
import { createMcpServer } from '@hazeljs/mcp';
|
|
382
|
+
|
|
383
|
+
class SupportAgent {
|
|
384
|
+
@Tool({
|
|
385
|
+
description: 'Triage a support issue and return urgency and category.',
|
|
386
|
+
parameters: [
|
|
387
|
+
{ name: 'issue', type: 'string', description: 'Issue description', required: true },
|
|
388
|
+
],
|
|
389
|
+
})
|
|
390
|
+
async triage(input: { issue: string }) {
|
|
391
|
+
const tpl = PromptRegistry.get<{ issue: string }>('support:ticket:triage');
|
|
392
|
+
const prompt = tpl.render(input);
|
|
393
|
+
return await callLLM(prompt);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const registry = new ToolRegistry();
|
|
398
|
+
registry.registerAgentTools('support', new SupportAgent());
|
|
399
|
+
createMcpServer({ registry }).listenStdio();
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
---
|
|
403
|
+
|
|
404
|
+
## API Reference
|
|
405
|
+
|
|
406
|
+
### `PromptTemplate<TVariables>`
|
|
407
|
+
|
|
408
|
+
| Method / Property | Description |
|
|
409
|
+
|---|---|
|
|
410
|
+
| `new PromptTemplate(template, metadata)` | Create a new template |
|
|
411
|
+
| `.template` | Raw template string (read-only) |
|
|
412
|
+
| `.metadata` | `PromptMetadata` object (read-only) |
|
|
413
|
+
| `.render(variables: TVariables)` | Interpolate placeholders and return the rendered string |
|
|
414
|
+
|
|
415
|
+
### `PromptRegistry` (static)
|
|
416
|
+
|
|
417
|
+
| Method | Description |
|
|
418
|
+
|---|---|
|
|
419
|
+
| `register(key, template)` | Register if key not already set |
|
|
420
|
+
| `override(key, template)` | Always register (overwrites existing) |
|
|
421
|
+
| `get(key, version?)` | Sync get — throws if not found |
|
|
422
|
+
| `has(key, version?)` | Returns `boolean` |
|
|
423
|
+
| `list()` | Returns all registered keys |
|
|
424
|
+
| `versions(key)` | Returns all cached version strings for a key |
|
|
425
|
+
| `unregister(key, version?)` | Remove from cache |
|
|
426
|
+
| `clear()` | Remove all from cache |
|
|
427
|
+
| `configure(stores)` | Replace store list |
|
|
428
|
+
| `addStore(store)` | Append a store |
|
|
429
|
+
| `storeNames()` | Names of configured stores |
|
|
430
|
+
| `getAsync(key, version?)` | Async get — falls back to stores |
|
|
431
|
+
| `save(key, version?)` | Persist one prompt to all stores |
|
|
432
|
+
| `saveAll()` | Persist all prompts to all stores |
|
|
433
|
+
| `loadAll(overwrite?)` | Load all from primary store into cache |
|
|
434
|
+
|
|
435
|
+
### `PromptStore` interface
|
|
436
|
+
|
|
437
|
+
```typescript
|
|
438
|
+
interface PromptStore {
|
|
439
|
+
name: string;
|
|
440
|
+
get(key: string, version?: string): Promise<PromptEntry | undefined>;
|
|
441
|
+
set(entry: PromptEntry): Promise<void>;
|
|
442
|
+
getAll(): Promise<PromptEntry[]>;
|
|
443
|
+
}
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
---
|
|
447
|
+
|
|
448
|
+
## License
|
|
449
|
+
|
|
450
|
+
Apache 2.0
|
|
451
|
+
|
|
452
|
+
## Contributing
|
|
453
|
+
|
|
454
|
+
Contributions are welcome! See [CONTRIBUTING.md](../../CONTRIBUTING.md) for details.
|
|
455
|
+
|
|
456
|
+
## Links
|
|
457
|
+
|
|
458
|
+
- [Documentation](https://hazeljs.com/docs/packages/prompts)
|
|
459
|
+
- [GitHub](https://github.com/hazeljs/hazeljs)
|
|
460
|
+
- [Issues](https://github.com/hazeljs/hazeljs/issues)
|
|
461
|
+
- [Discord](https://discord.com/channels/1448263814238965833/1448263814859456575)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/registry.test.ts"],"names":[],"mappings":""}
|