-
Notifications
You must be signed in to change notification settings - Fork 6.9k
feat(agents): stage files and download turn artifacts #4009
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
Merged
+1,558
−2
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
23b5e28
[skip ci] apcha/agents-files-artifacts
apcha-oai 0ffdf87
feat(agents): prepare selected files and download turn artifacts
apcha-oai 0beb3b4
fix(agents): retain upload sources and standard request options
apcha-oai 6a548de
fix(agents): preserve async cleanup and offload directory selection
apcha-oai c6a4cea
fix(agents): preserve selected file identity and async responsiveness
apcha-oai 6f18dc6
docs(agents): scope local path-selection assumptions
apcha-oai 6fc7b78
feat(agents): read result artifacts in memory and keep worker cleanup…
apcha-oai a7cc196
test(agents): use consistent asyncio imports
apcha-oai baaef5d
chore(agents): order combined beta helper exports
apcha-oai 62fcfb2
fix(agents): preserve interrupted uploads and defer file limits to API
apcha-oai 4380393
fix(agents): validate file destination ancestors without pairwise scans
apcha-oai b451728
fix(agents): skip directory subtrees outside file selections
apcha-oai 234d5c2
refactor(agents): expand file globs one directory segment at a time
apcha-oai 94688b7
refactor(agents): use standard pathlib glob semantics for file selection
apcha-oai b8653b4
test(agents): consolidate directory selection scenarios
apcha-oai 4e2b103
fix(agents): snapshot async uploads one file at a time
apcha-oai 0aff3cc
docs(agents): note future batch upload opportunity
apcha-oai 51dfaca
docs(agents): clarify application-owned archive orchestration
apcha-oai d7b99b1
Merge branch 'main' into apcha/agents-files-artifacts
apcha-oai e02dd44
Merge branch 'main' into apcha/agents-files-artifacts
apcha-oai 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from os import PathLike | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import httpx2 | ||
|
|
||
| from ._files import _RequestOptions | ||
| from ._result import AgentTurnResult | ||
| from ...._types import Body, Query, Headers, NotGiven, not_given | ||
| from ...._legacy_response import HttpxBinaryResponseContent | ||
| from ....types.beta.agents.sessions.session_artifact import SessionArtifact | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ....resources.beta.agents.sessions.artifacts import Artifacts, AsyncArtifacts | ||
|
|
||
|
|
||
| class AgentResultArtifacts: | ||
| """Beta: look up immutable artifacts from one completed turn.""" | ||
|
|
||
| def __init__(self, resource: Artifacts, result: AgentTurnResult) -> None: | ||
| self._resource = resource | ||
| self._session_id = result.session_id | ||
| self._turn_id = result.turn_id | ||
|
|
||
| def content( | ||
| self, | ||
| path: str, | ||
| *, | ||
| extra_headers: Headers | None = None, | ||
| extra_query: Query | None = None, | ||
| extra_body: Body | None = None, | ||
| timeout: float | httpx2.Timeout | None | NotGiven = not_given, | ||
| ) -> HttpxBinaryResponseContent: | ||
| """Read one exact turn/path using the SDK's native binary response.""" | ||
| options: _RequestOptions = { | ||
| "extra_headers": extra_headers, | ||
| "extra_query": extra_query, | ||
| "extra_body": extra_body, | ||
| "timeout": timeout, | ||
| } | ||
| selected = self._find(path, options) | ||
| return self._resource.content(selected.id, session_id=self._session_id, **options) | ||
|
|
||
| def download( | ||
| self, | ||
| path: str, | ||
| *, | ||
| to: str | PathLike[str], | ||
| extra_headers: Headers | None = None, | ||
| extra_query: Query | None = None, | ||
| extra_body: Body | None = None, | ||
| timeout: float | httpx2.Timeout | None | NotGiven = not_given, | ||
| ) -> SessionArtifact: | ||
| """Download one exact turn/path to an explicit local destination.""" | ||
| options: _RequestOptions = { | ||
| "extra_headers": extra_headers, | ||
| "extra_query": extra_query, | ||
| "extra_body": extra_body, | ||
| "timeout": timeout, | ||
| } | ||
| selected = self._find(path, options) | ||
| with self._resource.with_streaming_response.content( | ||
| selected.id, | ||
| session_id=self._session_id, | ||
| extra_headers=extra_headers, | ||
| extra_query=extra_query, | ||
| extra_body=extra_body, | ||
| timeout=timeout, | ||
| ) as content: | ||
| content.stream_to_file(to) | ||
| return selected | ||
|
|
||
| def _find(self, path: str, options: _RequestOptions) -> SessionArtifact: | ||
| selected: SessionArtifact | None = None | ||
| for artifact in self._resource.list( | ||
| self._session_id, | ||
| **options, | ||
| ): | ||
| if artifact.session_id == self._session_id and artifact.turn_id == self._turn_id and artifact.path == path: | ||
| if selected is not None: | ||
| raise ValueError("More than one artifact matches this turn and path") | ||
| selected = artifact | ||
| if selected is None: | ||
| raise ValueError("No artifact matches this turn and path") | ||
| return selected | ||
|
|
||
|
|
||
| class AsyncAgentResultArtifacts: | ||
| """Beta: async artifact downloads from one completed turn.""" | ||
|
|
||
| def __init__(self, resource: AsyncArtifacts, result: AgentTurnResult) -> None: | ||
| self._resource = resource | ||
| self._session_id = result.session_id | ||
| self._turn_id = result.turn_id | ||
|
|
||
| async def content( | ||
| self, | ||
| path: str, | ||
| *, | ||
| extra_headers: Headers | None = None, | ||
| extra_query: Query | None = None, | ||
| extra_body: Body | None = None, | ||
| timeout: float | httpx2.Timeout | None | NotGiven = not_given, | ||
| ) -> HttpxBinaryResponseContent: | ||
| """Read one exact turn/path using the SDK's native binary response.""" | ||
| options: _RequestOptions = { | ||
| "extra_headers": extra_headers, | ||
| "extra_query": extra_query, | ||
| "extra_body": extra_body, | ||
| "timeout": timeout, | ||
| } | ||
| selected = await self._find(path, options) | ||
| return await self._resource.content(selected.id, session_id=self._session_id, **options) | ||
|
|
||
| async def download( | ||
| self, | ||
| path: str, | ||
| *, | ||
| to: str | PathLike[str], | ||
| extra_headers: Headers | None = None, | ||
| extra_query: Query | None = None, | ||
| extra_body: Body | None = None, | ||
| timeout: float | httpx2.Timeout | None | NotGiven = not_given, | ||
| ) -> SessionArtifact: | ||
| """Download one exact turn/path to an explicit local destination.""" | ||
| options: _RequestOptions = { | ||
| "extra_headers": extra_headers, | ||
| "extra_query": extra_query, | ||
| "extra_body": extra_body, | ||
| "timeout": timeout, | ||
| } | ||
| selected = await self._find(path, options) | ||
| async with self._resource.with_streaming_response.content( | ||
| selected.id, | ||
| session_id=self._session_id, | ||
| extra_headers=extra_headers, | ||
| extra_query=extra_query, | ||
| extra_body=extra_body, | ||
| timeout=timeout, | ||
| ) as content: | ||
| await content.stream_to_file(to) | ||
| return selected | ||
|
|
||
| async def _find(self, path: str, options: _RequestOptions) -> SessionArtifact: | ||
| selected: SessionArtifact | None = None | ||
| async for artifact in self._resource.list( | ||
| self._session_id, | ||
| **options, | ||
| ): | ||
| if artifact.session_id == self._session_id and artifact.turn_id == self._turn_id and artifact.path == path: | ||
| if selected is not None: | ||
| raise ValueError("More than one artifact matches this turn and path") | ||
| selected = artifact | ||
| if selected is None: | ||
| raise ValueError("No artifact matches this turn and path") | ||
| return selected | ||
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.
Uh oh!
There was an error while loading. Please reload this page.