Skip to content

Commit

Permalink
feat: add ability to stat files in workspaces
Browse files Browse the repository at this point in the history
Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Oct 28, 2024
1 parent c3d4ba7 commit 5a758ca
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions gptscript/fileinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from datetime import datetime

from pydantic import BaseModel


class FileInfo(BaseModel):
workspaceID: str
name: str
size: int
modTime: datetime
15 changes: 15 additions & 0 deletions gptscript/gptscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from gptscript.confirm import AuthResponse
from gptscript.credentials import Credential, to_credential
from gptscript.datasets import DatasetMeta, Dataset, DatasetElementMeta, DatasetElement
from gptscript.fileinfo import FileInfo
from gptscript.frame import RunFrame, CallFrame, PromptFrame, Program
from gptscript.opts import GlobalOptions
from gptscript.prompt import PromptResponse
Expand Down Expand Up @@ -402,6 +403,20 @@ async def read_file_in_workspace(self, file_path: str, workspace_id: str = "") -
}
))

async def stat_file_in_workspace(self, file_path: str, workspace_id: str = "") -> FileInfo:
if workspace_id == "":
workspace_id = os.environ["GPTSCRIPT_WORKSPACE_ID"]

return FileInfo.model_validate_json(await self._run_basic_command(
"workspaces/stat-file",
{
"id": workspace_id,
"filePath": file_path,
"workspaceTool": self.opts.WorkspaceTool,
"env": self.opts.Env,
}
))


def _get_command():
if os.getenv("GPTSCRIPT_BIN") is not None:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_gptscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,17 @@ async def test_create_read_and_delete_file_in_workspace(gptscript):
await gptscript.write_file_in_workspace("test.txt", b"test", workspace_id)
contents = await gptscript.read_file_in_workspace("test.txt", workspace_id)
assert contents == b"test"

file_info = await gptscript.stat_file_in_workspace("test.txt", workspace_id)
assert file_info.name == "test.txt"
assert file_info.size == 4
assert file_info.modTime.hour == datetime.now(
tz=file_info.modTime.tzinfo,
).hour and file_info.modTime < datetime.now(
tz=file_info.modTime.tzinfo,
)

assert file_info.workspaceID == workspace_id
await gptscript.delete_file_in_workspace("test.txt", workspace_id)
await gptscript.delete_workspace(workspace_id)

Expand Down Expand Up @@ -857,6 +868,16 @@ async def test_create_read_and_delete_file_in_workspaces3(gptscript):
await gptscript.write_file_in_workspace("test.txt", b"test", workspace_id)
contents = await gptscript.read_file_in_workspace("test.txt", workspace_id)
assert contents == b"test"

file_info = await gptscript.stat_file_in_workspace("test.txt", workspace_id)
assert file_info.name == "test.txt"
assert file_info.size == 4
assert file_info.modTime.hour == datetime.now(
tz=file_info.modTime.tzinfo,
).hour and file_info.modTime < datetime.now(
tz=file_info.modTime.tzinfo,
)

await gptscript.delete_file_in_workspace("test.txt", workspace_id)
await gptscript.delete_workspace(workspace_id)

Expand Down

0 comments on commit 5a758ca

Please sign in to comment.