Run this notebook cell by cell in a disposable JupyterLab project with its live Python kernel. The setup asks for the absolute Jupyter server root and checks that it exists and contains this kernel’s working folder before making server-root-relative file references. Enter the folder JupyterLab serves, even when this notebook and its kernel are in a nested folder. The generated red PNG is an actual local input, not a camera image.
Tool catalog: browser media foundation
Run this notebook cell by cell in a disposable JupyterLab project with its live Python kernel. The setup asks for the absolute Jupyter server root and checks that it exists and contains this kernel’s working folder before making server-root-relative file references. Enter the folder JupyterLab serves, even when this notebook and its kernel are in a nested folder. The generated red PNG is an actual local input, not a camera image.
Each browser call returns a receipt immediately; wait for the media row to finish and run its inspection cell on a later turn. The AI questions use the selected ChatGPT connection and ask for real tool calls. They do not request device access.
Make an exact local media reference
from pathlib import Path, PurePosixPath
from uuid import uuid4
import hashlib
from PIL import Image
from nbinlineai.tools import browser_capabilities, operation_status, cancel_operation, save_media, release_media
root_text =input('Absolute Jupyter server root (the folder served by JupyterLab): ').strip()
root_choice = Path(root_text).expanduser()
ifnot root_choice.is_absolute():
raiseValueError('Enter an absolute Jupyter server-root folder; do not use the kernel working directory as a guess.')
server_root = root_choice.resolve(strict=True)
ifnot server_root.is_dir() ornot Path.cwd().resolve().is_relative_to(server_root):
raiseValueError('The server root must be an existing folder containing this kernel working directory.')
defserver_ref(path):
"""Make a server-root-relative path for an owned file or save destination."""
target = Path(path).resolve()
return target.relative_to(server_root).as_posix()
defserver_file(reference):
"""Map a server-returned relative path to the configured local root."""ifnotisinstance(reference, str) ornot reference or'\\'in reference:
raiseValueError('Expected a server-root-relative media path.')
relative = PurePosixPath(reference)
if relative.is_absolute() or relative.as_posix() != reference or reference =='.'or'..'in relative.parts:
raiseValueError('Media path must be a clean server-root-relative path.')
candidate = server_root / reference
ifnot candidate.resolve().is_relative_to(server_root):
raiseValueError('Media path escapes the configured server root.')
return candidate
source = Path.cwd() /f'browser-media-source-{uuid4().hex[:8]}.png'
Image.new('RGB', (2, 2), 'red').save(source)
ai_save_to = server_ref(Path.cwd() /f'browser-media-ai-{uuid4().hex[:8]}.png')
source_ref = {'path': server_ref(source), 'sha256': hashlib.sha256(source.read_bytes()).hexdigest()}
source_ref
Use &browser_capabilities now. Report the actual file-save capability and one concrete limit from its result; give the arguments you used. Do not request camera, microphone, or screen access.
Called browser_capabilities with {}. File-based media is supported (file_media_supported: true), and save_media is available. One limit: server media files can be at most 268,435,456 bytes (256 MiB).
Observed live tool calls for the answer above (actual subscription events; private fields shortened).
Use &save_media on the exact PNG in $source_ref, setting save_to to $ai_save_to. Report the immediate operation ID/state only. The later status question will confirm the saved path and hash.
After the AI save media row completes, use &operation_status on its exact operation ID. Report actual state, generated path, SHA-256, and managed media ID only if completed; otherwise stop and retry this status question later.
State: completed. Path: browser-media-ai-375ba9e8.png. SHA-256: de33ddc09a0ba9b83128b6b59461e3be59299575eb68ccf2c03984b504f9fa9c. Managed media ID: ec29c1fc34ba3872a4ee51aa1a52c31d.
Observed live tool calls for the answer above (actual subscription events; private fields shortened).
from nbinlineai.tools import operation_status
assert saved.status =='completed', 'Wait for the save receipt, then rerun this cell.'
lookup = operation_status(saved.operation_id)
lookup
print('Cancel after completed save:', cancelled.status, cancelled.error)
if cancelled.status =='completed':
print('Saved file still exists:', server_file(saved.media['path']).is_file())
Cancel after completed save: completed None
Saved file still exists: True
Use &cancel_operation with operation ID $saved_operation_id after that save completed. Report the actual response and whether the saved file remains; do not say this deletes a file.
cancel_operation returned completed for nd9jKBwlf9mNmjcsU7eTH0MktpK4znFL and still included the saved media path, media/capture-41d0f03d55f26c85.png. Cancellation did not delete the saved file.
Observed live tool calls for the answer above (actual subscription events; private fields shortened).
picture = Image.open(server_file(saved.media['path']))
picture.load()
from nbinlineai.tools import release_media
released = release_media(saved.media['media_id'])
released
Use &release_media only on the managed media ID from the completed AI save status above. Report its immediate operation ID/state. This does not delete the saved file, which the cleanup cell removes by its exact generated path.
Operation ID: 0uvPMekNZv3WR5YLT0_JR39h0VHJd0S-. Immediate result: released: true; no state field was returned.
Observed live tool calls for the answer above (actual subscription events; private fields shortened).
Use &operation_status on the preceding AI release operation ID after it completes. Report its actual terminal state; do not claim release from the initial snapshot.
The release operation’s terminal state is completed.
Observed live tool calls for the answer above (actual subscription events; private fields shortened).
saved_path = server_file(saved.media['path'])
assert saved_path.is_file() and picture.size == (2, 2)
assert hashlib.sha256(saved_path.read_bytes()).hexdigest() == saved.media['sha256']
saved_path.unlink() # Remove only the exact file this example saved.
source.unlink(missing_ok=True)
server_file(ai_save_to).unlink(missing_ok=True) # Only the exact extra AI-save destination.not saved_path.exists() andnot source.exists()