/ restai / project.py
project.py
 1  from restai.cache import Cache
 2  from restai.models.models import ProjectModel
 3  from restai.vectordb.tools import find_embeddings_path
 4  
 5  
 6  class Project:
 7  
 8      def __init__(self, model: ProjectModel):
 9          self.vector = None
10          self.props = model
11          self.context = None  # Verified context dict (from widget JWT or playground)
12  
13          if self.props.options.cache:
14              self.cache = Cache(self)
15          else:
16              self.cache = None
17  
18          if self.props.type == "rag":
19              find_embeddings_path(self.props.name)
20  
21      def with_context(self, context: dict, prepend_block: bool = True) -> "Project":
22          """Return a new Project with context injected into the system prompt.
23  
24          Used by the playground (raw dict), widget endpoint (verified JWT claims),
25          and block interpreter (propagated to sub-projects).
26          """
27          if not context:
28              return self
29          from restai.utils.widget_context import apply_context
30          modified_props = self.props.model_copy(deep=True)
31          modified_props.system = apply_context(
32              modified_props.system or "", context, prepend_block=prepend_block,
33          )
34          new_project = Project(modified_props)
35          new_project.vector = self.vector
36          new_project.context = context
37          return new_project
38  
39      def delete(self):
40          if self.vector:
41              self.vector.delete()
42          if self.cache:
43              self.cache.delete()