Repository navigation
feat(workspace): add ManagedWorkspace to build workspace persistence [PC-5359] #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tudormatei1
wants to merge
1
commit into
main
Choose a base branch
from
feat/PC-5359-coded-workspace-persistence
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| """Shared wiring that persists a runtime's workspace through job attachments.""" | ||
|
|
||
| from typing import Callable, Protocol | ||
|
|
||
| from uipath.core.workspace import AttachmentsProtocol, JobsProtocol | ||
|
|
||
| from uipath.runtime.base import UiPathRuntimeProtocol | ||
| from uipath.runtime.workspace.conversational import ConversationalWorkspaceRuntime | ||
| from uipath.runtime.workspace.hydration import HydrationPolicy, HydrationRuntime | ||
| from uipath.runtime.workspace.hydrator import WorkspaceHydrator | ||
| from uipath.runtime.workspace.registry_store import WorkspaceRegistryStore | ||
| from uipath.runtime.workspace.workspace import Workspace | ||
|
|
||
|
|
||
| class WorkspaceServicesProtocol(Protocol): | ||
| """Platform services the workspace hydrator talks to, such as ``UiPath()``.""" | ||
|
|
||
| @property | ||
| def attachments(self) -> AttachmentsProtocol: | ||
| """Uploads and downloads workspace files.""" | ||
| ... | ||
|
|
||
| @property | ||
| def jobs(self) -> JobsProtocol: | ||
| """Links uploaded files to the current job.""" | ||
| ... | ||
|
|
||
|
|
||
| class ManagedWorkspace: | ||
| """Builds the workspace wrappers for one runtime. | ||
|
|
||
| The wrappers returned by :meth:`wrap` and :meth:`wrap_conversational` share | ||
| one workspace, one hydrator and one registry store. The platform services | ||
| are created on first use: :meth:`wrap` defers them to the first run, while | ||
| :meth:`wrap_conversational` needs them when it is called. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| services: Callable[[], WorkspaceServicesProtocol], | ||
| runtime_id: str, | ||
| job_key: str | None, | ||
| folder_key: str | None = None, | ||
| workspace: Workspace | None = None, | ||
| registry_store: WorkspaceRegistryStore | None = None, | ||
| ) -> None: | ||
| """Initialize the managed workspace. | ||
|
|
||
| Args: | ||
| services: Returns the attachments and jobs services, for example ``UiPath``. | ||
| runtime_id: Runtime id the wrapped runtime was created with. | ||
| job_key: Job that uploaded files are linked to, or ``None`` outside a job. | ||
| folder_key: Orchestrator folder of the attachments. | ||
| workspace: Folder owned by the caller. When omitted, a temporary | ||
| folder is created; disposing the runtime returned by :meth:`wrap` | ||
| removes it, so call :meth:`wrap` once per instance. | ||
| registry_store: Where the uploaded files are recorded. Required by | ||
| :meth:`wrap`; optional for :meth:`wrap_conversational`. | ||
| """ | ||
| self.workspace = workspace if workspace is not None else Workspace.create() | ||
| self.registry_store = registry_store | ||
| self.runtime_id = runtime_id | ||
| self._services = services | ||
| self._job_key = job_key | ||
| self._folder_key = folder_key | ||
| self._hydrator: WorkspaceHydrator | None = None | ||
|
|
||
| def hydrator(self) -> WorkspaceHydrator: | ||
| """Return the shared hydrator, creating the platform services on first use.""" | ||
| if self._hydrator is None: | ||
| services = self._services() | ||
| self._hydrator = WorkspaceHydrator( | ||
| workspace_path=self.workspace.path, | ||
| attachments=services.attachments, | ||
| jobs=services.jobs, | ||
| current_job_key=self._job_key, | ||
| folder_key=self._folder_key, | ||
| ) | ||
| return self._hydrator | ||
|
|
||
| def wrap( | ||
| self, | ||
| runtime: UiPathRuntimeProtocol, | ||
| *, | ||
| policy: HydrationPolicy = HydrationPolicy.SUSPEND_ONLY, | ||
| ) -> HydrationRuntime: | ||
| """Restore the workspace before each run and persist it according to policy.""" | ||
| if self.registry_store is None: | ||
| raise ValueError("Workspace hydration needs a registry store") | ||
| return HydrationRuntime( | ||
| runtime, | ||
| workspace=self.workspace, | ||
| hydrator_factory=self.hydrator, | ||
| registry_store=self.registry_store, | ||
| policy=policy, | ||
| runtime_id=self.runtime_id, | ||
| ) | ||
|
|
||
| def wrap_conversational( | ||
| self, runtime: UiPathRuntimeProtocol | ||
| ) -> ConversationalWorkspaceRuntime: | ||
| """Carry the workspace from one conversation exchange to the next. | ||
|
|
||
| The returned runtime never removes the workspace folder. Without a | ||
| caller-owned ``workspace``, also call :meth:`wrap`, whose runtime removes it. | ||
| """ | ||
| return ConversationalWorkspaceRuntime( | ||
| runtime, | ||
| hydrator=self.hydrator(), | ||
| registry_store=self.registry_store, | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right,
wrap_conversational()builds the services when it's called. The description overstated it; I narrowed it to say onlywrap()defers them.I'm keeping the code as is. The only caller is uipath-agents, and only inside a conversational job, where credentials are always set. Agents already created
UiPath()at that point before this change: https://github.com/UiPath/uipath-agents-python/blob/70c7dbbb54318418d0cb60c881638b2d3885f281/src/uipath_agents/_cli/runtime/factory.py#L398-L405Deferring it would mean changing
ConversationalWorkspaceRuntimeto take a hydrator factory instead of a hydrator, which is outside this PR.