-
Notifications
You must be signed in to change notification settings - Fork 2
Feat/custom data files #47
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
18bc376
Add a registry for user-uploaded custom data files
jlee-kitware a0e57a5
Tell the model about uploaded data files in the prompt
jlee-kitware 415e99d
Add a UI to upload and manage custom data files
jlee-kitware b217058
Move the data-upload control to the code pane
jlee-kitware dfd3c7f
Add a Data settings tab for resolved files
jlee-kitware d9a3d1d
Fix Add data click: reach the file input via window.document
jlee-kitware da11d7a
Fix mypy error in the upload content coercion
jlee-kitware 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,33 @@ | ||
| """Sample-data resolution for example-style prompts that read files.""" | ||
| """Data resolution for prompts that read files: sample data and user uploads.""" | ||
|
|
||
| from .resolver import artifacts, available_names, has_data_root, referenced, resolve | ||
| from .resolver import ( | ||
| artifacts, | ||
| available_names, | ||
| cached_names, | ||
| clear_cache, | ||
| has_data_root, | ||
| referenced, | ||
| resolve, | ||
| ) | ||
| from .uploads import ( | ||
| add_upload, | ||
| clear_uploads, | ||
| remove_upload, | ||
| uploaded_names, | ||
| uploaded_path, | ||
| ) | ||
|
|
||
| __all__ = ["artifacts", "available_names", "has_data_root", "referenced", "resolve"] | ||
| __all__ = [ | ||
| "add_upload", | ||
| "artifacts", | ||
| "available_names", | ||
| "cached_names", | ||
| "clear_cache", | ||
| "clear_uploads", | ||
| "has_data_root", | ||
| "referenced", | ||
| "remove_upload", | ||
| "resolve", | ||
| "uploaded_names", | ||
| "uploaded_path", | ||
| ] |
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,87 @@ | ||
| """Registry for user-uploaded custom data files. | ||
|
|
||
| Complements ``resolver.py`` (which fetches known VTK sample data by name) by | ||
| letting users supply their own files. Uploaded files are written to a local | ||
| cache directory and can then be referenced by bare filename in generated code, | ||
| exactly like sample data. User uploads take precedence over sample data of the | ||
| same name. | ||
| """ | ||
|
|
||
| import os | ||
| import re | ||
| from collections.abc import Iterable | ||
| from pathlib import Path | ||
| from typing import cast | ||
|
|
||
| # Characters allowed in a stored filename; anything else is replaced. | ||
| _SAFE_NAME_RE = re.compile(r"[^A-Za-z0-9._-]") | ||
|
|
||
|
|
||
| def _uploads_dir() -> Path: | ||
| base = os.environ.get("XDG_CACHE_HOME") | ||
| root = Path(base) if base else Path.home() / ".cache" | ||
| directory = root / "vtk-prompt" / "uploads" | ||
| directory.mkdir(parents=True, exist_ok=True) | ||
| return directory | ||
|
|
||
|
|
||
| def _safe_name(filename: str) -> str: | ||
| """Reduce an arbitrary filename to a bare, filesystem-safe basename.""" | ||
| name = os.path.basename((filename or "").strip()) | ||
| name = _SAFE_NAME_RE.sub("_", name) | ||
| return name.lstrip(".") or "upload" | ||
|
|
||
|
|
||
| def _as_bytes(content: object) -> bytes: | ||
| """Coerce uploaded file content (bytes, bytearray, str, or ints) to bytes.""" | ||
| if isinstance(content, bytes): | ||
| return content | ||
| if isinstance(content, (bytearray, memoryview)): | ||
| return bytes(content) | ||
| if isinstance(content, str): | ||
| return content.encode("utf-8", "surrogateescape") | ||
| if isinstance(content, Iterable): | ||
| try: | ||
| return bytes(cast(Iterable[int], content)) # e.g. a list of byte values | ||
| except (TypeError, ValueError): | ||
| return b"" | ||
| return b"" | ||
|
|
||
|
|
||
| def add_upload(filename: str, content: object) -> str: | ||
| """Store uploaded content under its sanitized basename; return the path.""" | ||
| dest = _uploads_dir() / _safe_name(filename) | ||
| with open(dest, "wb") as handle: | ||
| handle.write(_as_bytes(content)) | ||
| return str(dest) | ||
|
|
||
|
|
||
| def uploaded_names() -> list[str]: | ||
| """Sorted basenames of all uploaded files currently available.""" | ||
| directory = _uploads_dir() | ||
| return sorted(p.name for p in directory.iterdir() if p.is_file()) | ||
|
|
||
|
|
||
| def uploaded_path(name: str) -> str | None: | ||
| """Local path for an uploaded file by basename, or None if absent.""" | ||
| name = os.path.basename((name or "").strip()) | ||
| if not name: | ||
| return None | ||
| path = _uploads_dir() / name | ||
| return str(path) if path.is_file() else None | ||
|
|
||
|
|
||
| def remove_upload(name: str) -> bool: | ||
| """Delete one uploaded file by basename. Returns True if it existed.""" | ||
| path = uploaded_path(name) | ||
| if not path: | ||
| return False | ||
| Path(path).unlink(missing_ok=True) | ||
| return True | ||
|
|
||
|
|
||
| def clear_uploads() -> None: | ||
| """Remove all uploaded files.""" | ||
| for p in _uploads_dir().iterdir(): | ||
| if p.is_file(): | ||
| p.unlink(missing_ok=True) |
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,5 @@ | ||
| role: user | ||
| content: | | ||
| I have uploaded the following data files. You can load each one directly by its | ||
| bare filename (do not invent a directory path): {{uploaded_files_list}}. | ||
| Choose the appropriate VTK reader for each file based on its extension. | ||
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
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.
We might want to explicly tell to use the vtk mcp tools to lookup for readers if needed