tempest_fastapi_sdk.hostbridge¶
Controle da máquina em que o serviço roda: energia, arquivos, PDFs e comandos.
hostbridge
¶
Control the machine a service runs on — power, files, PDFs, commands.
Built for the shape a local assistant actually has: a Python process inside
WSL that needs to reach the Windows host it runs under. It shells out to
powershell.exe and reads the disk through the /mnt drive mounts, and
works unchanged on Windows itself when those binaries resolve.
from tempest_fastapi_sdk.hostbridge import HostBridge, HostBridgeConfig
bridge = HostBridge(
HostBridgeConfig(allowed_base_paths=("/mnt/c/Users/me/Documents",)),
)
info = await bridge.host_info()
text = await bridge.read_text("C:\\Users\\me\\Documents\\notes.txt")
This is a shell on that machine
Whoever reaches these methods can run arbitrary commands as the host
user and power the machine off. Two things follow, and neither is
optional: allowed_base_paths starts empty, denying every path
until it is configured, and
:func:~tempest_fastapi_sdk.hostbridge.make_hostbridge_router takes its
auth dependencies as a required argument and refuses an empty one. The
write side of that router — commands, writes, deletes, power — is behind
destructive=True and off by default.
Nothing here is imported by create_app or mounted anywhere on its own:
a host surface exists because a service asked for it.
On a host with neither PowerShell nor wslpath — a plain Linux box, a
container — calls raise
:class:~tempest_fastapi_sdk.hostbridge.HostUnavailableError (503) rather
than a generic failure, so a caller can degrade instead of reporting that
the action failed.
From inside a container, the host is another machine: run
tempest hostbridge serve on the host (the app
:func:~tempest_fastapi_sdk.hostbridge.make_hostbridge_agent_app builds) and
hold a :class:~tempest_fastapi_sdk.hostbridge.RemoteHostBridge in the
service — same methods, same schemas, same exceptions.
:func:~tempest_fastapi_sdk.hostbridge.hostbridge_from_settings picks one or
the other from HOST_BRIDGE_URL, and code typed against
:class:~tempest_fastapi_sdk.hostbridge.HostBridgeProtocol accepts both.
Needs no extra of its own for the system, file and command surface — it is
standard library, FastAPI and httpx. Reading PDFs needs [pdf-read], and reading
them with layout and tables needs [pdf-layout].
MIN_AGENT_TOKEN_LENGTH
module-attribute
¶
Shortest shared secret the agent accepts, in characters.
tempest secrets generate prints 64 characters and
secrets.token_urlsafe(32) 43; 32 leaves room for a hex secret of 16
bytes and refuses the changeme-class value that ends up in a .env
when someone is in a hurry.
DEFAULT_COMMAND_TIMEOUT
module-attribute
¶
Seconds a host command may run before it is killed.
DEFAULT_MAX_FILE_READ_BYTES
module-attribute
¶
Ceiling for a single text or PDF read, in bytes.
Ported from the windows-bridge-api service this package replaced. It is a
text reader: 10 MiB of text is already far past what a caller can use, and
the cap is what keeps one request from pinning the process's memory.
DEFAULT_REMOTE_CONNECT_TIMEOUT
module-attribute
¶
Seconds to establish the TCP connection to the agent.
Short on purpose: an agent that is not listening refuses at once, and one
that is unreachable should become HOST_UNAVAILABLE quickly rather than
hold the caller for the whole request timeout.
DEFAULT_REMOTE_REQUEST_TIMEOUT
module-attribute
¶
Seconds a call without a timeout of its own may take end to end.
Covers reads, listings, PDFs, host info and power actions — the last two run a host command under the agent's default timeout (30 s unless the agent was configured otherwise), which this leaves room for.
DEFAULT_REMOTE_TIMEOUT_MARGIN
module-attribute
¶
Seconds added to a host-side timeout before the HTTP call gives up.
The agent kills a command at its own timeout and answers
HOST_COMMAND_TIMEOUT; the margin is what lets that answer arrive
instead of the client timing out first and reporting less.
HostBridge
¶
HostBridge(config: HostBridgeConfig | None = None)
Control the machine this process runs on, or the Windows host behind it.
Built for the WSL case — a Linux process reaching the Windows host it
runs under, through powershell.exe and the /mnt drive mounts —
and works unchanged on Windows itself when the binaries resolve. On a
host with neither, every call raises
:class:~tempest_fastapi_sdk.hostbridge.HostUnavailableError instead of
failing in a way a caller cannot tell apart from a real error.
Hold the configuration every call reads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
HostBridgeConfig | None
|
Binaries, timeout and the
allowed base paths. |
None
|
aclose
async
¶
Release nothing: this bridge holds no connection.
Present so a service can close whichever bridge
:func:~tempest_fastapi_sdk.hostbridge.hostbridge_from_settings
handed it — this one or a
:class:~tempest_fastapi_sdk.hostbridge.RemoteHostBridge — without
asking which.
shutdown
async
¶
shutdown(payload: PowerActionSchema | None = None) -> str
Schedule the host to power off.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
PowerActionSchema | None
|
Delay, force flag and message.
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A confirmation sentence. |
Raises:
| Type | Description |
|---|---|
HostCommandError
|
When |
HostUnavailableError
|
When it is absent. |
restart
async
¶
restart(payload: PowerActionSchema | None = None) -> str
Schedule the host to restart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
PowerActionSchema | None
|
Delay, force flag and message.
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A confirmation sentence. |
Raises:
| Type | Description |
|---|---|
HostCommandError
|
When |
HostUnavailableError
|
When it is absent. |
abort_shutdown
async
¶
Cancel a shutdown or restart that has not fired yet.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A confirmation sentence. |
Raises:
| Type | Description |
|---|---|
HostCommandError
|
When |
HostUnavailableError
|
When it is absent. |
lock
async
¶
Lock the host's active session.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A confirmation sentence. |
Raises:
| Type | Description |
|---|---|
HostCommandError
|
When |
HostUnavailableError
|
When it is absent. |
logoff
async
¶
Log the host's active user off, closing their session.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A confirmation sentence. |
Raises:
| Type | Description |
|---|---|
HostCommandError
|
When |
HostUnavailableError
|
When it is absent. |
host_info
async
¶
host_info() -> HostInfoSchema
Report the host's name, user, OS version and uptime.
Returns:
| Name | Type | Description |
|---|---|---|
HostInfoSchema |
HostInfoSchema
|
The collected host information. |
Raises:
| Type | Description |
|---|---|
HostCommandError
|
When PowerShell fails, or answers with something that is not the expected JSON. |
HostUnavailableError
|
When PowerShell is absent. |
run_command
async
¶
run_command(payload: CommandSchema) -> CommandResultSchema
Run a command on the host and return everything it said.
A non-zero exit is returned, not raised: the command ran, and which exit codes are failures is the caller's domain knowledge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
CommandSchema
|
The command, its interpreter and an optional timeout. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
CommandResultSchema |
CommandResultSchema
|
Exit status, output and duration. |
Raises:
| Type | Description |
|---|---|
HostCommandTimeoutError
|
When it outlives its timeout. |
HostUnavailableError
|
When the interpreter is absent. |
read_text
async
¶
read_text(raw_path: str, encoding: str = 'utf-8') -> FileContentSchema
Read a text file from the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_path
|
str
|
The file's path, in WSL or Windows form. |
required |
encoding
|
str
|
Encoding used to decode it. |
'utf-8'
|
Returns:
| Name | Type | Description |
|---|---|---|
FileContentSchema |
FileContentSchema
|
The resolved path, size and decoded text. |
Raises:
| Type | Description |
|---|---|
InvalidHostPathError
|
When the path is not allowed. |
HostFileNotFoundError
|
When there is no file there. |
HostFileTooLargeError
|
When it is over the configured cap. |
HostFileDecodeError
|
When the bytes do not decode — which is what pointing this at a PDF or an image looks like. |
write_text
async
¶
write_text(payload: FileWriteSchema) -> str
Write text to a file on the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
FileWriteSchema
|
Path, content, encoding and whether to append and create parents. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A confirmation sentence. |
Raises:
| Type | Description |
|---|---|
InvalidHostPathError
|
When the path is not allowed. |
OSError
|
When the write itself fails — a full disk, a read-only
mount, a missing parent with |
list_dir
async
¶
list_dir(raw_path: str) -> DirectoryListingSchema
List a directory on the host.
An entry that cannot be stat'ed — a broken link, a file that disappeared mid-walk, one the host user may not see — is skipped rather than failing the listing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_path
|
str
|
The directory's path, in WSL or Windows form. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
DirectoryListingSchema |
DirectoryListingSchema
|
Its entries, directories first and then |
DirectoryListingSchema
|
by name. An empty directory lists nothing and is not an error. |
Raises:
| Type | Description |
|---|---|
InvalidHostPathError
|
When the path is not allowed. |
HostFileNotFoundError
|
When there is no directory there. |
delete
async
¶
delete(payload: FileDeleteSchema) -> str
Delete a file, or an empty directory, on the host.
A non-empty directory is not removed recursively: the call fails
with the OSError the filesystem raises. Erasing a tree is not
something to do as a side effect of a delete call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
FileDeleteSchema
|
The path and whether a missing one is acceptable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A confirmation sentence. |
Raises:
| Type | Description |
|---|---|
InvalidHostPathError
|
When the path is not allowed. |
HostFileNotFoundError
|
When it does not exist and |
OSError
|
When the directory is not empty, or the delete fails. |
read_pdf
async
¶
read_pdf(
raw_path: str, extractor: PdfExtractorEnum = TEXT, password: str | None = None
) -> HostPdfSchema
Read a PDF on the host, page by page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_path
|
str
|
The file's path, in WSL or Windows form. |
required |
extractor
|
PdfExtractorEnum
|
|
TEXT
|
password
|
str | None
|
Password for an encrypted document. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
HostPdfSchema |
HostPdfSchema
|
The path, size and one entry per page. A scanned |
HostPdfSchema
|
page comes back with empty text — there is no OCR here. |
Raises:
| Type | Description |
|---|---|
InvalidHostPathError
|
When the path is not allowed. |
HostFileNotFoundError
|
When there is no file there. |
HostFileTooLargeError
|
When it is over the configured cap. |
PdfDecryptError
|
When it is encrypted and the password is wrong. |
PdfExtractError
|
When parsing fails for another reason. |
pick_file
async
¶
pick_file(payload: FilePickSchema) -> FilePickResultSchema
Open the host's native file-picker and return what was chosen.
This blocks on a person: the dialog stays open until they choose or
close it, bounded by timeout_seconds. Everything the caller
supplies reaches the generated script through
:func:~tempest_fastapi_sdk.hostbridge.shell.ps_single_quote, and
the whole script is passed base64-encoded as -EncodedCommand so
no quoting survives the trip to be reinterpreted.
The dialog is given a hidden, always-on-top owner window. Without an owner it opens behind whatever the person is looking at, and a modal nobody can see reads as a hang.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
FilePickSchema
|
Title, initial directory, filter, multiselect and timeout. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
FilePickResultSchema |
FilePickResultSchema
|
The chosen paths, or |
Raises:
| Type | Description |
|---|---|
InvalidHostPathError
|
When |
HostCommandTimeoutError
|
When nobody answers in time. |
HostCommandError
|
When PowerShell exits non-zero or answers with something that is not the expected JSON. |
HostUnavailableError
|
When PowerShell is absent. |
HostBridgeConfig
dataclass
¶
HostBridgeConfig(
allowed_base_paths: tuple[str, ...] = tuple(),
powershell_binary: str = "powershell.exe",
cmd_binary: str = "cmd.exe",
default_command_timeout: int = DEFAULT_COMMAND_TIMEOUT,
max_file_read_bytes: int = DEFAULT_MAX_FILE_READ_BYTES,
)
Configuration for a :class:~tempest_fastapi_sdk.hostbridge.HostBridge.
Attributes:
| Name | Type | Description |
|---|---|---|
allowed_base_paths |
tuple[str, ...]
|
Directories the file surface may touch. A resolved path outside every one of them is refused. Empty (the default) denies everything. |
powershell_binary |
str
|
Executable used for PowerShell commands.
|
cmd_binary |
str
|
Executable used for |
default_command_timeout |
int
|
Seconds a command may run when the caller does not pass its own timeout. |
max_file_read_bytes |
int
|
Ceiling for a single read. |
HostBridgeRemoteError
¶
HostBridgeRemoteError(
message: str | None = None,
*,
code: str | None = None,
status_code: int | None = None,
details: dict[str, Any] | None = None,
headers: dict[str, str] | None = None,
message_key: str | None = None,
message_params: dict[str, Any] | None = None,
field: str | None = None,
)
Bases: AppException
The host agent answered with a failure this SDK does not know.
Raised by :class:~tempest_fastapi_sdk.hostbridge.RemoteHostBridge for a
response whose code maps to no host bridge exception — an unhandled
error on the agent (a full disk on a write is the usual one), a request
the agent refused as invalid, a proxy in between answering on its own.
details carries the agent's status and code.
HostBridgeUnauthorizedError
¶
HostBridgeUnauthorizedError(
message: str | None = None,
*,
code: str | None = None,
status_code: int | None = None,
details: dict[str, Any] | None = None,
headers: dict[str, str] | None = None,
message_key: str | None = None,
message_params: dict[str, Any] | None = None,
field: str | None = None,
)
Bases: HostUnavailableError
The host agent refused this service's token.
A :class:~tempest_fastapi_sdk.hostbridge.RemoteHostBridge raises it
when the agent answers 401 or 403. It is a configuration fault
between two processes the operator owns — the token on one side does
not match the other — so it answers 503 like
:class:HostUnavailableError, which it extends: forwarding the agent's
401 would tell the end user their own session expired.
HostCommandError
¶
HostCommandTimeoutError
¶
HostFileDecodeError
¶
HostFileDecodeError(
message: str | None = None,
*,
code: str | None = None,
status_code: int | None = None,
details: dict[str, Any] | None = None,
headers: dict[str, str] | None = None,
message_key: str | None = None,
message_params: dict[str, Any] | None = None,
field: str | None = None,
)
Bases: ValidationException
File bytes do not decode with the requested text encoding.
This is what pointing the text reader at a PDF or an image looks like.
Left to propagate as a UnicodeDecodeError it would surface as an
opaque 500, when it is a client error the caller can act on.
HostFileNotFoundError
¶
HostFileTooLargeError
¶
HostUnavailableError
¶
HostUnavailableError(
message: str | None = None,
*,
code: str | None = None,
status_code: int | None = None,
details: dict[str, Any] | None = None,
headers: dict[str, str] | None = None,
message_key: str | None = None,
message_params: dict[str, Any] | None = None,
field: str | None = None,
)
Bases: AppException
The host control surface is not reachable from this process.
Raised when the configured shell or wslpath is missing — which is
what this package looks like on a plain Linux box, or on a Windows host
whose PowerShell is not on PATH. It is distinct from
:class:HostCommandError on purpose: the command never ran, so a
caller can degrade instead of reporting a failed action.
InvalidHostPathError
¶
HostBridgeProtocol
¶
Bases: Protocol
What a host bridge does: power, files, PDFs and commands.
Every method raises the same exception classes whichever implementation answers, so a caller handles a failure once.
shutdown
async
¶
shutdown(payload: PowerActionSchema | None = None) -> str
Schedule the host to power off.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
PowerActionSchema | None
|
Delay, force flag and message. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A confirmation sentence. |
restart
async
¶
restart(payload: PowerActionSchema | None = None) -> str
Schedule the host to restart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
PowerActionSchema | None
|
Delay, force flag and message. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A confirmation sentence. |
abort_shutdown
async
¶
Cancel a shutdown or restart that has not fired yet.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A confirmation sentence. |
lock
async
¶
Lock the host's active session.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A confirmation sentence. |
logoff
async
¶
Log the host's active user off.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A confirmation sentence. |
host_info
async
¶
host_info() -> HostInfoSchema
Report the host's name, user, OS version and uptime.
Returns:
| Name | Type | Description |
|---|---|---|
HostInfoSchema |
HostInfoSchema
|
The collected host information. |
run_command
async
¶
run_command(payload: CommandSchema) -> CommandResultSchema
Run a command on the host and return everything it said.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
CommandSchema
|
The command, its interpreter and timeout. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
CommandResultSchema |
CommandResultSchema
|
Exit status, output and duration. |
read_text
async
¶
read_text(raw_path: str, /, encoding: str = 'utf-8') -> FileContentSchema
Read a text file from the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_path
|
str
|
The file's path, in WSL or Windows form. |
required |
encoding
|
str
|
Encoding used to decode it. |
'utf-8'
|
Returns:
| Name | Type | Description |
|---|---|---|
FileContentSchema |
FileContentSchema
|
The resolved path, size and decoded text. |
write_text
async
¶
write_text(payload: FileWriteSchema) -> str
Write text to a file on the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
FileWriteSchema
|
Path, content and write options. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A confirmation sentence. |
list_dir
async
¶
list_dir(raw_path: str) -> DirectoryListingSchema
List a directory on the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_path
|
str
|
The directory's path, in WSL or Windows form. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
DirectoryListingSchema |
DirectoryListingSchema
|
Its entries. |
delete
async
¶
delete(payload: FileDeleteSchema) -> str
Delete a file, or an empty directory, on the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
FileDeleteSchema
|
The path and whether a missing one is acceptable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A confirmation sentence. |
read_pdf
async
¶
read_pdf(
raw_path: str, /, extractor: PdfExtractorEnum = TEXT, password: str | None = None
) -> HostPdfSchema
Read a PDF on the host, page by page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_path
|
str
|
The file's path, in WSL or Windows form. |
required |
extractor
|
PdfExtractorEnum
|
Which extractor reads it. |
TEXT
|
password
|
str | None
|
Password for an encrypted document. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
HostPdfSchema |
HostPdfSchema
|
The path, size and one entry per page. |
pick_file
async
¶
pick_file(payload: FilePickSchema) -> FilePickResultSchema
Open the host's native file-picker and return what was chosen.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
FilePickSchema
|
Title, start directory, filter and wait. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
FilePickResultSchema |
FilePickResultSchema
|
What the person chose, or that they cancelled. |
RemoteHostBridge
¶
RemoteHostBridge(
base_url: str,
*,
token: str,
prefix: str = "/system",
header_name: str = "X-Token",
default_command_timeout: int = DEFAULT_COMMAND_TIMEOUT,
timeout_margin: float = DEFAULT_REMOTE_TIMEOUT_MARGIN,
request_timeout: float = DEFAULT_REMOTE_REQUEST_TIMEOUT,
connect_timeout: float = DEFAULT_REMOTE_CONNECT_TIMEOUT,
transport: AsyncBaseTransport | None = None,
)
Control a host through the agent running on it.
Same methods, return schemas and exceptions as
:class:~tempest_fastapi_sdk.hostbridge.HostBridge. Failures map back by
the envelope's code; an agent that cannot be reached raises
:class:~tempest_fastapi_sdk.hostbridge.HostUnavailableError, one that
refuses the token
:class:~tempest_fastapi_sdk.hostbridge.HostBridgeUnauthorizedError, and
any other failure
:class:~tempest_fastapi_sdk.hostbridge.HostBridgeRemoteError.
Which surface answers is the agent's decision: a method of the write
side (commands, writes, deletes, power) against an agent started
without --destructive gets a 404 and raises
:class:~tempest_fastapi_sdk.hostbridge.HostBridgeRemoteError.
Holds one httpx.AsyncClient; close it with :meth:aclose or use the
bridge as an async with block.
Point the bridge at an agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_url
|
str
|
The agent's origin, e.g.
|
required |
token
|
str
|
The shared secret the agent checks. Sent in
|
required |
prefix
|
str
|
Where the agent mounted the router. Defaults to
|
'/system'
|
header_name
|
str
|
Header carrying the token. Defaults to
|
'X-Token'
|
default_command_timeout
|
int
|
The agent's default command
timeout, used to size the HTTP timeout of a command that
names none. Defaults to |
DEFAULT_COMMAND_TIMEOUT
|
timeout_margin
|
float
|
Seconds added to a host-side timeout before the HTTP call gives up. |
DEFAULT_REMOTE_TIMEOUT_MARGIN
|
request_timeout
|
float
|
Seconds any other call may take. |
DEFAULT_REMOTE_REQUEST_TIMEOUT
|
connect_timeout
|
float
|
Seconds to establish the connection. |
DEFAULT_REMOTE_CONNECT_TIMEOUT
|
transport
|
AsyncBaseTransport | None
|
Transport for the
client — |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When |
shutdown
async
¶
shutdown(payload: PowerActionSchema | None = None) -> str
Schedule the host to power off.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
PowerActionSchema | None
|
Delay, force flag and message.
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The agent's confirmation sentence. |
restart
async
¶
restart(payload: PowerActionSchema | None = None) -> str
Schedule the host to restart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
PowerActionSchema | None
|
Delay, force flag and message.
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The agent's confirmation sentence. |
abort_shutdown
async
¶
Cancel a shutdown or restart that has not fired yet.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The agent's confirmation sentence. |
lock
async
¶
Lock the host's active session.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The agent's confirmation sentence. |
logoff
async
¶
Log the host's active user off, closing their session.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The agent's confirmation sentence. |
host_info
async
¶
host_info() -> HostInfoSchema
Report the host's name, user, OS version and uptime.
Returns:
| Name | Type | Description |
|---|---|---|
HostInfoSchema |
HostInfoSchema
|
The collected host information. |
run_command
async
¶
run_command(payload: CommandSchema) -> CommandResultSchema
Run a command on the host and return everything it said.
The HTTP timeout is the command's own (or the agent's default) plus
timeout_margin, so the agent's HOST_COMMAND_TIMEOUT arrives
before the client gives up.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
CommandSchema
|
The command, its interpreter and an optional timeout. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
CommandResultSchema |
CommandResultSchema
|
Exit status, output and duration. |
read_text
async
¶
read_text(raw_path: str, encoding: str = 'utf-8') -> FileContentSchema
Read a text file from the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_path
|
str
|
The file's path, in WSL or Windows form. |
required |
encoding
|
str
|
Encoding used to decode it. |
'utf-8'
|
Returns:
| Name | Type | Description |
|---|---|---|
FileContentSchema |
FileContentSchema
|
The resolved path, size and decoded text. |
write_text
async
¶
write_text(payload: FileWriteSchema) -> str
Write text to a file on the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
FileWriteSchema
|
Path, content, encoding and whether to append and create parents. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The agent's confirmation sentence. |
list_dir
async
¶
list_dir(raw_path: str) -> DirectoryListingSchema
List a directory on the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_path
|
str
|
The directory's path, in WSL or Windows form. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
DirectoryListingSchema |
DirectoryListingSchema
|
Its entries, directories first. |
delete
async
¶
delete(payload: FileDeleteSchema) -> str
Delete a file, or an empty directory, on the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
FileDeleteSchema
|
The path and whether a missing one is acceptable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The agent's confirmation sentence. |
read_pdf
async
¶
read_pdf(
raw_path: str, extractor: PdfExtractorEnum = TEXT, password: str | None = None
) -> HostPdfSchema
Read a PDF on the host, page by page.
The password travels as a query parameter, as the router declares it — which is one more reason the agent belongs on loopback or a private bridge network, never behind a logging proxy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_path
|
str
|
The file's path, in WSL or Windows form. |
required |
extractor
|
PdfExtractorEnum
|
|
TEXT
|
password
|
str | None
|
Password for an encrypted document. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
HostPdfSchema |
HostPdfSchema
|
The path, size and one entry per page. |
pick_file
async
¶
pick_file(payload: FilePickSchema) -> FilePickResultSchema
Open the host's native file-picker and return what was chosen.
The HTTP timeout is the picker's own wait plus timeout_margin:
a person choosing a file takes as long as they take.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
FilePickSchema
|
Title, start directory, filter and how long to wait. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
FilePickResultSchema |
FilePickResultSchema
|
What the person chose, or that they |
FilePickResultSchema
|
cancelled. |
CommandResultSchema
¶
Bases: BaseSchema
What a host command produced.
A non-zero return_code is not an error here: the command ran and
said something, and which exit codes matter is the caller's domain
knowledge, not this package's.
Attributes:
| Name | Type | Description |
|---|---|---|
return_code |
int
|
The command's exit status. |
stdout |
str
|
Standard output. |
stderr |
str
|
Standard error. |
duration_seconds |
float
|
Wall-clock time the command ran. |
model_config
class-attribute
instance-attribute
¶
Keep the payload byte-for-byte.
:class:~tempest_fastapi_sdk.BaseSchema strips whitespace from every
string, which is right for a name or an email and wrong for content that
was read off a disk or a pipe: a file whose last line ends in a newline
would come back without it, and writing that back would silently rewrite
the file.
CommandSchema
¶
Bases: BaseSchema
A command to run on the host.
Attributes:
| Name | Type | Description |
|---|---|---|
command |
str
|
The command string. It reaches the interpreter as a single argument, so the interpreter — not a shell in between — is what parses it. |
shell |
Literal['powershell', 'cmd']
|
Which interpreter runs it. |
timeout |
int | None
|
Seconds it may run. |
DirectoryListingSchema
¶
Bases: BaseSchema
The contents of a directory.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The resolved directory that was listed. |
entries |
list[FileInfoSchema]
|
Its entries, directories first and then by name, case-insensitively. An empty directory lists nothing and is not an error. |
FileContentSchema
¶
Bases: BaseSchema
A text file's contents.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The resolved path that was read, which may differ from the one requested — a Windows path comes back in its local form. |
encoding |
str
|
Encoding used to decode it. |
size_bytes |
int
|
Size on disk. |
content |
str
|
The decoded text. |
model_config
class-attribute
instance-attribute
¶
Keep the payload byte-for-byte.
:class:~tempest_fastapi_sdk.BaseSchema strips whitespace from every
string, which is right for a name or an email and wrong for content that
was read off a disk or a pipe: a file whose last line ends in a newline
would come back without it, and writing that back would silently rewrite
the file.
FileDeleteSchema
¶
Bases: BaseSchema
A file or empty directory to remove.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
Absolute path, in WSL or Windows form. |
missing_ok |
bool
|
Succeed instead of failing when it is already gone. |
FileInfoSchema
¶
Bases: BaseSchema
One entry in a directory listing.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The entry's own name. |
path |
str
|
Its full resolved path. |
is_dir |
bool
|
Whether it is a directory. |
size_bytes |
int
|
Size on disk. |
modified_at |
float
|
Unix timestamp of the last modification. |
FilePickResultSchema
¶
Bases: BaseSchema
What the file-picker dialog came back with.
Attributes:
| Name | Type | Description |
|---|---|---|
cancelled |
bool
|
|
paths |
list[str]
|
Chosen paths, in the host's own form. |
FilePickSchema
¶
Bases: BaseSchema
Configuration for the host's native file-picker dialog.
Attributes:
| Name | Type | Description |
|---|---|---|
title |
str
|
The dialog's window title. |
initial_dir |
str | None
|
Directory it opens in. Confined to the allowed base paths like any other path. |
filter |
str
|
The dialog's filter string, in the
|
multiselect |
bool
|
Allow more than one file to be chosen. |
timeout_seconds |
int
|
How long to wait for the person to choose. A dialog nobody answers is what this bounds — without it the call would hang for as long as the window stays open. |
FileWriteSchema
¶
Bases: BaseSchema
A text file to write on the host.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
Absolute path, in WSL or Windows form. |
content |
str
|
Text to write. |
encoding |
str
|
Encoding used to write it. |
append |
bool
|
Append instead of overwriting. |
create_parents |
bool
|
Create missing parent directories. |
model_config
class-attribute
instance-attribute
¶
Write exactly what the caller sent.
See :class:FileContentSchema — a write that strips the content cannot
round-trip a file it just read.
HostInfoSchema
¶
Bases: BaseSchema
Identity and uptime of the host.
Attributes:
| Name | Type | Description |
|---|---|---|
computer_name |
str
|
The machine's name. |
user_name |
str
|
The logged-in user. |
os_version |
str
|
Operating system caption and version. |
uptime_seconds |
float
|
Seconds since the last boot. |
HostMessageSchema
¶
Bases: BaseSchema
A finished action, described in one sentence.
Attributes:
| Name | Type | Description |
|---|---|---|
message |
str
|
What happened, for a human to read. |
HostPdfSchema
¶
Bases: BaseSchema
A PDF on the host, read page by page.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The resolved path that was read. |
extractor |
PdfExtractorEnum
|
Which backend produced the pages. |
page_count |
int
|
Number of pages read. |
size_bytes |
int
|
The file's size on disk. |
pages |
list[PdfPage]
|
One entry per page, in document order. |
PowerActionSchema
¶
Bases: BaseSchema
Arguments for a shutdown or a restart.
Attributes:
| Name | Type | Description |
|---|---|---|
seconds |
int
|
Delay before the action runs. |
force |
bool
|
Close applications without prompting the user. This discards unsaved work — it is off by default for that reason. |
message |
str | None
|
Text shown to the logged-in user. |
CommandResult
dataclass
¶
What a finished subprocess produced.
Attributes:
| Name | Type | Description |
|---|---|---|
return_code |
int
|
The process's exit status, or |
stdout |
str
|
Standard output, decoded with |
stderr |
str
|
Standard error, decoded the same way. |
duration_seconds |
float
|
Wall-clock time the process ran. |
make_hostbridge_agent_app
¶
make_hostbridge_agent_app(
bridge: HostBridgeProtocol,
*,
token: str,
destructive: bool = False,
prefix: str = "/system",
header_name: str = "X-Token",
locale: str = DEFAULT_LOCALE,
min_token_length: int = MIN_AGENT_TOKEN_LENGTH,
) -> FastAPI
Build the app the host runs so a remote service can reach it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bridge
|
HostBridgeProtocol
|
The bridge every route calls — a
:class: |
required |
token
|
str
|
The shared secret every request must carry in
|
required |
destructive
|
bool
|
Also mount the write side — commands, writes,
deletes and power. Defaults to |
False
|
prefix
|
str
|
Where the router is mounted. Defaults to |
'/system'
|
header_name
|
str
|
Header carrying the token. Defaults to
|
'X-Token'
|
locale
|
str
|
Language of the error sentences when the caller's
|
DEFAULT_LOCALE
|
min_token_length
|
int
|
Shortest token accepted. |
MIN_AGENT_TOKEN_LENGTH
|
Returns:
| Name | Type | Description |
|---|---|---|
FastAPI |
FastAPI
|
The app, ready for uvicorn. |
Raises:
| Type | Description |
|---|---|
ValueError
|
When |
validate_agent_token
¶
validate_agent_token(token: str, *, min_length: int = MIN_AGENT_TOKEN_LENGTH) -> str
Refuse a shared secret too weak to front a shell.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The secret, as configured. |
required |
min_length
|
int
|
Minimum length in characters. |
MIN_AGENT_TOKEN_LENGTH
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When it is empty, or shorter than |
hostbridge_from_settings
¶
hostbridge_from_settings(settings: HostBridgeSettings) -> HostBridge | RemoteHostBridge
Build the bridge the settings describe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
HostBridgeSettings
|
A service's settings composing the
|
required |
Returns:
| Type | Description |
|---|---|
HostBridge | RemoteHostBridge
|
HostBridge | RemoteHostBridge: A remote bridge when |
HostBridge | RemoteHostBridge
|
|
HostBridge | RemoteHostBridge
|
|
ensure_path_allowed
¶
Resolve a path and confine it to one of the allowed base directories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Path
|
The path to validate. |
required |
allowed_base_paths
|
tuple[str, ...]
|
Directories the caller may reach. An empty tuple denies every path. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The resolved absolute path, guaranteed to sit under one of the |
Path
|
bases. |
Raises:
| Type | Description |
|---|---|
InvalidHostPathError
|
When the resolved path is outside every base.
The message carries the resolved path, not the one the caller
sent, so a rejected |
to_windows_path
async
¶
Convert a WSL path to its Windows form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
A WSL path ( |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The Windows-style path ( |
Raises:
| Type | Description |
|---|---|
HostUnavailableError
|
When |
InvalidHostPathError
|
When the path cannot be converted. |
to_wsl_path
async
¶
Normalize an input path, in either form, to a local :class:Path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
A WSL path ( |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The path in the form this process can open. |
Raises:
| Type | Description |
|---|---|
HostUnavailableError
|
When translation is needed and |
InvalidHostPathError
|
When the path cannot be converted. |
make_hostbridge_router
¶
make_hostbridge_router(
bridge: HostBridgeProtocol,
*,
dependencies: Sequence[Depends],
prefix: str = "/system",
tags: list[str] | None = None,
destructive: bool = False,
docs_locale: str | None = None,
) -> APIRouter
Build a router over a configured host bridge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bridge
|
HostBridgeProtocol
|
The configured bridge every route
calls — a :class: |
required |
dependencies
|
Sequence[Depends]
|
Auth (and anything else) applied to
every route. Required, and an empty sequence is refused — see
this module's docstring for why. Because they are auth by
contract, every route documents |
required |
prefix
|
str
|
Path prefix for the routes. Defaults to |
'/system'
|
tags
|
list[str] | None
|
OpenAPI tags. Defaults to |
None
|
destructive
|
bool
|
Also mount the write side — command execution,
file writes and deletes, power actions. Defaults to |
False
|
docs_locale
|
str | None
|
Language of the OpenAPI |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
APIRouter |
APIRouter
|
The router, ready for |
Raises:
| Type | Description |
|---|---|
ValueError
|
When |
ps_single_quote
¶
Quote a value as a PowerShell single-quoted literal.
PowerShell's single-quoted string has exactly one escape: a doubled
quote. Nothing else inside is interpreted — no $ expansion, no
backtick escapes — which is what makes this a safe way to put a
caller-supplied title or path into a generated script.
"Quote" means every character in _PS_SINGLE_QUOTE_CHARS, not only
the ASCII apostrophe: the lexer ends a literal on a typographic quote
(U+2019) just as it does on ', so each one is doubled the way
PowerShell's own escaper doubles it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The raw value. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The value wrapped in single quotes, with every inner |
str
|
single-quote character doubled. |
run_cmd
async
¶
run_cmd(
command: str, *, timeout: int | None = None, config: HostBridgeConfig | None = None
) -> CommandResult
Run a cmd.exe command on the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
The command to run after |
required |
timeout
|
int | None
|
Seconds the command may run. |
None
|
config
|
HostBridgeConfig | None
|
Source of the binary and the default timeout. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
CommandResult |
CommandResult
|
The finished process's output. |
Raises:
| Type | Description |
|---|---|
HostUnavailableError
|
When |
HostCommandTimeoutError
|
When the command outlives the timeout. |
run_powershell
async
¶
run_powershell(
command: str, *, timeout: int | None = None, config: HostBridgeConfig | None = None
) -> CommandResult
Run a PowerShell statement on the host.
The interpreter is started with -NoProfile -NonInteractive: a profile
would let whatever the host user configured change the output this
package parses, and an interactive prompt would hang the call until the
timeout rather than failing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
The |
required |
timeout
|
int | None
|
Seconds the command may run. |
None
|
config
|
HostBridgeConfig | None
|
Source of the binary and the default timeout. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
CommandResult |
CommandResult
|
The finished process's output. |
Raises:
| Type | Description |
|---|---|
HostUnavailableError
|
When PowerShell is not on |
HostCommandTimeoutError
|
When the command outlives the timeout. |
run_subprocess
async
¶
run_subprocess(
args: list[str],
*,
timeout: int | None = None,
config: HostBridgeConfig | None = None,
) -> CommandResult
Run a process and capture its output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
list[str]
|
Executable followed by its arguments. Passed to
|
required |
timeout
|
int | None
|
Seconds the process may run. |
None
|
config
|
HostBridgeConfig | None
|
Source of the default timeout.
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
CommandResult |
CommandResult
|
The finished process's exit status, output and duration. |
Raises:
| Type | Description |
|---|---|
HostUnavailableError
|
When the executable is not on |
HostCommandTimeoutError
|
When the process outlives the timeout. It is killed and awaited before this raises, so nothing is left running. |
DEFAULT_COMMAND_TIMEOUT
module-attribute
¶
Seconds a host command may run before it is killed.
DEFAULT_MAX_FILE_READ_BYTES
module-attribute
¶
Ceiling for a single text or PDF read, in bytes.
Ported from the windows-bridge-api service this package replaced. It is a
text reader: 10 MiB of text is already far past what a caller can use, and
the cap is what keeps one request from pinning the process's memory.