prompt_manager 0.5.7 → 0.5.8
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/COMMITS.md +196 -0
- data/README.md +485 -203
- data/docs/.keep +0 -0
- data/docs/advanced/custom-keywords.md +421 -0
- data/docs/advanced/dynamic-directives.md +535 -0
- data/docs/advanced/performance.md +612 -0
- data/docs/advanced/search-integration.md +635 -0
- data/docs/api/configuration.md +355 -0
- data/docs/api/directive-processor.md +431 -0
- data/docs/api/prompt-class.md +354 -0
- data/docs/api/storage-adapters.md +462 -0
- data/docs/assets/favicon.ico +1 -0
- data/docs/assets/logo.svg +24 -0
- data/docs/core-features/comments.md +48 -0
- data/docs/core-features/directive-processing.md +38 -0
- data/docs/core-features/erb-integration.md +68 -0
- data/docs/core-features/error-handling.md +197 -0
- data/docs/core-features/parameter-history.md +76 -0
- data/docs/core-features/parameterized-prompts.md +500 -0
- data/docs/core-features/shell-integration.md +79 -0
- data/docs/development/architecture.md +544 -0
- data/docs/development/contributing.md +425 -0
- data/docs/development/roadmap.md +234 -0
- data/docs/development/testing.md +822 -0
- data/docs/examples/advanced.md +523 -0
- data/docs/examples/basic.md +688 -0
- data/docs/examples/real-world.md +776 -0
- data/docs/examples.md +337 -0
- data/docs/getting-started/basic-concepts.md +318 -0
- data/docs/getting-started/installation.md +97 -0
- data/docs/getting-started/quick-start.md +256 -0
- data/docs/index.md +230 -0
- data/docs/migration/v0.9.0.md +459 -0
- data/docs/migration/v1.0.0.md +591 -0
- data/docs/storage/activerecord-adapter.md +348 -0
- data/docs/storage/custom-adapters.md +176 -0
- data/docs/storage/filesystem-adapter.md +236 -0
- data/docs/storage/overview.md +427 -0
- data/examples/advanced_integrations.rb +52 -0
- data/examples/prompts_dir/advanced_demo.txt +79 -0
- data/examples/prompts_dir/directive_example.json +1 -0
- data/examples/prompts_dir/directive_example.txt +8 -0
- data/examples/prompts_dir/todo.json +1 -1
- data/improvement_plan.md +996 -0
- data/lib/prompt_manager/storage/file_system_adapter.rb +8 -2
- data/lib/prompt_manager/version.rb +1 -1
- data/mkdocs.yml +146 -0
- data/prompt_manager_logo.png +0 -0
- metadata +46 -3
- data/LICENSE.txt +0 -21
@@ -0,0 +1,354 @@
|
|
1
|
+
# Prompt Class API Reference
|
2
|
+
|
3
|
+
The `PromptManager::Prompt` class is the core interface for working with prompts in PromptManager.
|
4
|
+
|
5
|
+
## Class Methods
|
6
|
+
|
7
|
+
### `new(id:, **options)`
|
8
|
+
|
9
|
+
Creates a new Prompt instance.
|
10
|
+
|
11
|
+
**Parameters:**
|
12
|
+
- `id` (String): Unique identifier for the prompt
|
13
|
+
- `options` (Hash): Optional configuration parameters
|
14
|
+
|
15
|
+
**Options:**
|
16
|
+
- `erb_flag` (Boolean): Enable ERB template processing (default: false)
|
17
|
+
- `envar_flag` (Boolean): Enable environment variable substitution (default: false)
|
18
|
+
- `storage` (Storage::Base): Custom storage adapter (default: configured adapter)
|
19
|
+
|
20
|
+
**Returns:** `PromptManager::Prompt` instance
|
21
|
+
|
22
|
+
**Examples:**
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
# Basic prompt
|
26
|
+
prompt = PromptManager::Prompt.new(id: 'welcome_message')
|
27
|
+
|
28
|
+
# With ERB processing
|
29
|
+
prompt = PromptManager::Prompt.new(
|
30
|
+
id: 'dynamic_prompt',
|
31
|
+
erb_flag: true
|
32
|
+
)
|
33
|
+
|
34
|
+
# With environment variables
|
35
|
+
prompt = PromptManager::Prompt.new(
|
36
|
+
id: 'system_prompt',
|
37
|
+
envar_flag: true
|
38
|
+
)
|
39
|
+
|
40
|
+
# All options
|
41
|
+
prompt = PromptManager::Prompt.new(
|
42
|
+
id: 'advanced_prompt',
|
43
|
+
erb_flag: true,
|
44
|
+
envar_flag: true
|
45
|
+
)
|
46
|
+
```
|
47
|
+
|
48
|
+
## Instance Methods
|
49
|
+
|
50
|
+
### `render(parameters = {})`
|
51
|
+
|
52
|
+
Renders the prompt with the provided parameters.
|
53
|
+
|
54
|
+
**Parameters:**
|
55
|
+
- `parameters` (Hash): Key-value pairs for parameter substitution
|
56
|
+
|
57
|
+
**Returns:** String - The rendered prompt content
|
58
|
+
|
59
|
+
**Raises:**
|
60
|
+
- `PromptNotFoundError` - If the prompt file cannot be found
|
61
|
+
- `MissingParametersError` - If required parameters are not provided
|
62
|
+
- `DirectiveProcessingError` - If directive processing fails
|
63
|
+
|
64
|
+
**Examples:**
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
# Basic rendering
|
68
|
+
result = prompt.render
|
69
|
+
|
70
|
+
# With parameters
|
71
|
+
result = prompt.render(
|
72
|
+
customer_name: 'John Doe',
|
73
|
+
order_id: 'ORD-123'
|
74
|
+
)
|
75
|
+
|
76
|
+
# With complex parameters
|
77
|
+
result = prompt.render(
|
78
|
+
user: {
|
79
|
+
name: 'Alice',
|
80
|
+
email: 'alice@example.com'
|
81
|
+
},
|
82
|
+
items: ['Item 1', 'Item 2'],
|
83
|
+
total: 99.99
|
84
|
+
)
|
85
|
+
```
|
86
|
+
|
87
|
+
### `save(content, **metadata)`
|
88
|
+
|
89
|
+
Saves prompt content to storage.
|
90
|
+
|
91
|
+
**Parameters:**
|
92
|
+
- `content` (String): The prompt content to save
|
93
|
+
- `metadata` (Hash): Optional metadata to store with the prompt
|
94
|
+
|
95
|
+
**Returns:** Boolean - Success status
|
96
|
+
|
97
|
+
**Examples:**
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
# Save content
|
101
|
+
prompt.save("Hello [NAME], welcome to our service!")
|
102
|
+
|
103
|
+
# Save with metadata
|
104
|
+
prompt.save(
|
105
|
+
"Your order [ORDER_ID] is ready!",
|
106
|
+
category: 'notifications',
|
107
|
+
author: 'system',
|
108
|
+
version: '1.0'
|
109
|
+
)
|
110
|
+
```
|
111
|
+
|
112
|
+
### `content`
|
113
|
+
|
114
|
+
Retrieves the raw prompt content from storage.
|
115
|
+
|
116
|
+
**Returns:** String - The raw prompt content
|
117
|
+
|
118
|
+
**Example:**
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
raw_content = prompt.content
|
122
|
+
puts raw_content # "Hello [NAME]!"
|
123
|
+
```
|
124
|
+
|
125
|
+
### `parameters`
|
126
|
+
|
127
|
+
Extracts parameter names from the prompt content.
|
128
|
+
|
129
|
+
**Returns:** Array<String> - List of parameter names found in the prompt
|
130
|
+
|
131
|
+
**Example:**
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
# Prompt content: "Hello [NAME], your order [ORDER_ID] is ready!"
|
135
|
+
params = prompt.parameters
|
136
|
+
puts params # ['NAME', 'ORDER_ID']
|
137
|
+
```
|
138
|
+
|
139
|
+
### `delete`
|
140
|
+
|
141
|
+
Removes the prompt from storage.
|
142
|
+
|
143
|
+
**Returns:** Boolean - Success status
|
144
|
+
|
145
|
+
**Example:**
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
prompt.delete
|
149
|
+
```
|
150
|
+
|
151
|
+
### `exists?`
|
152
|
+
|
153
|
+
Checks if the prompt exists in storage.
|
154
|
+
|
155
|
+
**Returns:** Boolean - True if prompt exists
|
156
|
+
|
157
|
+
**Example:**
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
if prompt.exists?
|
161
|
+
puts "Prompt found"
|
162
|
+
else
|
163
|
+
puts "Prompt not found"
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
167
|
+
## Properties
|
168
|
+
|
169
|
+
### `id`
|
170
|
+
|
171
|
+
**Type:** String (read-only)
|
172
|
+
|
173
|
+
The unique identifier for the prompt.
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
prompt = PromptManager::Prompt.new(id: 'welcome')
|
177
|
+
puts prompt.id # "welcome"
|
178
|
+
```
|
179
|
+
|
180
|
+
### `erb_flag`
|
181
|
+
|
182
|
+
**Type:** Boolean
|
183
|
+
|
184
|
+
Whether ERB processing is enabled.
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
prompt.erb_flag = true
|
188
|
+
```
|
189
|
+
|
190
|
+
### `envar_flag`
|
191
|
+
|
192
|
+
**Type:** Boolean
|
193
|
+
|
194
|
+
Whether environment variable substitution is enabled.
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
prompt.envar_flag = true
|
198
|
+
```
|
199
|
+
|
200
|
+
### `storage`
|
201
|
+
|
202
|
+
**Type:** Storage::Base
|
203
|
+
|
204
|
+
The storage adapter used by this prompt.
|
205
|
+
|
206
|
+
```ruby
|
207
|
+
prompt.storage = PromptManager::Storage::FileSystemAdapter.new
|
208
|
+
```
|
209
|
+
|
210
|
+
## Parameter Processing
|
211
|
+
|
212
|
+
### Parameter Syntax
|
213
|
+
|
214
|
+
Parameters use square bracket syntax: `[PARAMETER_NAME]`
|
215
|
+
|
216
|
+
```ruby
|
217
|
+
# In prompt file:
|
218
|
+
# "Hello [NAME], your balance is $[BALANCE]"
|
219
|
+
|
220
|
+
prompt.render(
|
221
|
+
name: 'Alice',
|
222
|
+
balance: 1500.00
|
223
|
+
)
|
224
|
+
# Result: "Hello Alice, your balance is $1500.0"
|
225
|
+
```
|
226
|
+
|
227
|
+
### Nested Parameters
|
228
|
+
|
229
|
+
Parameters can reference nested hash values:
|
230
|
+
|
231
|
+
```ruby
|
232
|
+
# In prompt file:
|
233
|
+
# "User: [USER.NAME] ([USER.EMAIL])"
|
234
|
+
|
235
|
+
prompt.render(
|
236
|
+
user: {
|
237
|
+
name: 'Bob',
|
238
|
+
email: 'bob@example.com'
|
239
|
+
}
|
240
|
+
)
|
241
|
+
# Result: "User: Bob (bob@example.com)"
|
242
|
+
```
|
243
|
+
|
244
|
+
### Array Parameters
|
245
|
+
|
246
|
+
Arrays are joined with commas by default:
|
247
|
+
|
248
|
+
```ruby
|
249
|
+
# In prompt file:
|
250
|
+
# "Items: [ITEMS]"
|
251
|
+
|
252
|
+
prompt.render(items: ['Apple', 'Banana', 'Orange'])
|
253
|
+
# Result: "Items: Apple, Banana, Orange"
|
254
|
+
```
|
255
|
+
|
256
|
+
## Error Handling
|
257
|
+
|
258
|
+
### Exception Hierarchy
|
259
|
+
|
260
|
+
```ruby
|
261
|
+
PromptManager::Error
|
262
|
+
├── PromptNotFoundError
|
263
|
+
├── MissingParametersError
|
264
|
+
├── DirectiveProcessingError
|
265
|
+
└── StorageError
|
266
|
+
```
|
267
|
+
|
268
|
+
### Error Details
|
269
|
+
|
270
|
+
```ruby
|
271
|
+
begin
|
272
|
+
prompt.render
|
273
|
+
rescue PromptManager::MissingParametersError => e
|
274
|
+
puts e.message # Human readable message
|
275
|
+
puts e.missing_parameters # Array of missing parameter names
|
276
|
+
puts e.prompt_id # ID of the prompt that failed
|
277
|
+
rescue PromptManager::DirectiveProcessingError => e
|
278
|
+
puts e.message # Error details
|
279
|
+
puts e.line_number # Line where error occurred (if available)
|
280
|
+
puts e.directive # The directive that failed
|
281
|
+
end
|
282
|
+
```
|
283
|
+
|
284
|
+
## Threading and Concurrency
|
285
|
+
|
286
|
+
### Thread Safety
|
287
|
+
|
288
|
+
Prompt instances are **not** thread-safe. Create separate instances for each thread:
|
289
|
+
|
290
|
+
```ruby
|
291
|
+
# Thread-safe usage
|
292
|
+
threads = []
|
293
|
+
(1..10).each do |i|
|
294
|
+
threads << Thread.new do
|
295
|
+
# Each thread gets its own instance
|
296
|
+
prompt = PromptManager::Prompt.new(id: 'worker_prompt')
|
297
|
+
result = prompt.render(worker_id: i)
|
298
|
+
puts result
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
threads.each(&:join)
|
303
|
+
```
|
304
|
+
|
305
|
+
### Shared Storage
|
306
|
+
|
307
|
+
Storage adapters handle their own thread safety. Multiple Prompt instances can safely share the same storage adapter.
|
308
|
+
|
309
|
+
## Best Practices
|
310
|
+
|
311
|
+
### Instance Reuse
|
312
|
+
|
313
|
+
```ruby
|
314
|
+
# Good: Reuse instances when possible
|
315
|
+
prompt = PromptManager::Prompt.new(id: 'email_template')
|
316
|
+
|
317
|
+
customers.each do |customer|
|
318
|
+
email_content = prompt.render(
|
319
|
+
name: customer.name,
|
320
|
+
email: customer.email
|
321
|
+
)
|
322
|
+
send_email(customer, email_content)
|
323
|
+
end
|
324
|
+
```
|
325
|
+
|
326
|
+
### Parameter Validation
|
327
|
+
|
328
|
+
```ruby
|
329
|
+
# Validate parameters before rendering
|
330
|
+
required_params = prompt.parameters
|
331
|
+
missing_params = required_params - params.keys
|
332
|
+
|
333
|
+
unless missing_params.empty?
|
334
|
+
raise "Missing parameters: #{missing_params.join(', ')}"
|
335
|
+
end
|
336
|
+
|
337
|
+
result = prompt.render(params)
|
338
|
+
```
|
339
|
+
|
340
|
+
### Error Recovery
|
341
|
+
|
342
|
+
```ruby
|
343
|
+
def safe_render(prompt_id, params = {})
|
344
|
+
prompt = PromptManager::Prompt.new(id: prompt_id)
|
345
|
+
prompt.render(params)
|
346
|
+
rescue PromptManager::PromptNotFoundError
|
347
|
+
"Default message when prompt unavailable"
|
348
|
+
rescue PromptManager::MissingParametersError => e
|
349
|
+
"Missing: #{e.missing_parameters.join(', ')}"
|
350
|
+
rescue => e
|
351
|
+
Rails.logger.error "Prompt render error: #{e.message}"
|
352
|
+
"An error occurred"
|
353
|
+
end
|
354
|
+
```
|