pixie-prompts 0.1.10__tar.gz → 0.1.11__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pixie-prompts
3
- Version: 0.1.10
3
+ Version: 0.1.11
4
4
  Summary: Code-first, type-safe prompt management
5
5
  License: MIT
6
6
  License-File: LICENSE
@@ -41,6 +41,10 @@ class PromptLoadError(Exception):
41
41
  super().__init__(message)
42
42
 
43
43
 
44
+ class PromptNotFoundError(KeyError):
45
+ pass
46
+
47
+
44
48
  class BaseUntypedPromptWithCreationTime(BaseUntypedPrompt):
45
49
 
46
50
  def __init__(
@@ -254,7 +258,12 @@ class _FilePromptStorage(PromptStorage):
254
258
  return original is None
255
259
 
256
260
  def get(self, prompt_id: str) -> BaseUntypedPromptWithCreationTime:
257
- return self._prompts[prompt_id]
261
+ try:
262
+ return self._prompts[prompt_id]
263
+ except KeyError:
264
+ raise PromptNotFoundError(
265
+ f"Prompt with ID '{prompt_id}' not found in storage."
266
+ )
258
267
 
259
268
 
260
269
  _storage_instance: PromptStorage | None = None
@@ -306,9 +315,11 @@ class StorageBackedPrompt(Prompt[TPromptVar]):
306
315
  id: str,
307
316
  *,
308
317
  variables_definition: type[TPromptVar] = NoneType,
318
+ default: str | None = None,
309
319
  ) -> None:
310
320
  self._id = id
311
321
  self._variables_definition = variables_definition
322
+ self._default = default
312
323
  self._prompt: BasePrompt[TPromptVar] | None = None
313
324
 
314
325
  @property
@@ -319,24 +330,42 @@ class StorageBackedPrompt(Prompt[TPromptVar]):
319
330
  def variables_definition(self) -> type[TPromptVar]:
320
331
  return self._variables_definition
321
332
 
333
+ @property
334
+ def default(self) -> str | None:
335
+ return self._default
336
+
322
337
  def get_variables_schema(self) -> dict[str, Any]:
323
338
  return variables_definition_to_schema(self._variables_definition)
324
339
 
325
340
  def _get_prompt(self) -> BasePrompt[TPromptVar]:
326
- storage = _ensure_storage_initialized()
327
- if self._prompt is None:
328
- untyped_prompt = storage.get(self.id)
329
- self._prompt = BasePrompt.from_untyped(
330
- untyped_prompt,
341
+ if self._prompt is not None:
342
+ return self._prompt
343
+ try:
344
+ storage = _ensure_storage_initialized()
345
+ if self._prompt is None:
346
+ untyped_prompt = storage.get(self.id)
347
+ self._prompt = BasePrompt.from_untyped(
348
+ untyped_prompt,
349
+ variables_definition=self.variables_definition,
350
+ )
351
+ schema_from_storage = untyped_prompt.get_variables_schema()
352
+ schema_from_definition = self.get_variables_schema()
353
+ if not isSubschema(schema_from_definition, schema_from_storage):
354
+ raise TypeError(
355
+ "Schema from definition is not a subschema of the schema from storage."
356
+ )
357
+ return self._prompt
358
+ except (PromptNotFoundError, PromptLoadError):
359
+ if self.default is None:
360
+ raise PromptNotFoundError(
361
+ f"Cannot load prompt with id '{self.id}' from storage, and no default is provided."
362
+ )
363
+ self._prompt = BasePrompt(
364
+ id=self.id,
365
+ versions={"v0": self.default},
331
366
  variables_definition=self.variables_definition,
332
367
  )
333
- schema_from_storage = untyped_prompt.get_variables_schema()
334
- schema_from_definition = self.get_variables_schema()
335
- if not isSubschema(schema_from_definition, schema_from_storage):
336
- raise TypeError(
337
- "Schema from definition is not a subschema of the schema from storage."
338
- )
339
- return self._prompt
368
+ return self._prompt
340
369
 
341
370
  def actualize(self) -> Self:
342
371
  self._get_prompt()
@@ -4,7 +4,7 @@ packages = [
4
4
  { include = "pixie" },
5
5
  ]
6
6
 
7
- version = "0.1.10"
7
+ version = "0.1.11"
8
8
  description = "Code-first, type-safe prompt management"
9
9
  authors = ["Yiou Li <yol@gopixie.ai>"]
10
10
  license = "MIT"
File without changes
File without changes