Metadata-Version: 2.5
Name: viewport-core
Version: 0.1.1
Summary: The shared runtime under Viewport Evals, HITL and Context: trajectory schemas and types, the Matcher engine, errors, tools and middlewares, and the ambient event sink.
Project-URL: Homepage, https://getviewport.com
Project-URL: Documentation, https://docs.getviewport.com/core
Project-URL: Repository, https://github.com/viewportai/core
Project-URL: Issues, https://github.com/viewportai/core/issues
Author-email: Viewport <hello@getviewport.com>
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: agents,ai,llm,matcher,middleware,tools,trajectory,viewport
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: google-re2>=1.1
Requires-Dist: jsonschema>=4.21
Requires-Dist: pydantic>=2.7
Description-Content-Type: text/markdown

# viewport-core

The shared runtime under Viewport Evals, HITL and Context. See the [repository README](https://github.com/viewportai/core#readme).

```sh
pip install viewport-core   # or: uv add viewport-core
```

## Example

Save this as `example.py` and run `python example.py`. It loads a YAML expectation, calls a tool that takes a card
token, lets a tool server answer instead of running the tool, records the call under a fake clock and checks it:

```python
import asyncio
import os

from pydantic import SecretStr

from viewportai.core import SpecError, compile_matcher, stable_json, tool, tool_server, trace
from viewportai.core.testing import FakeClock, SequentialIds
from viewportai.core.yaml import load_yaml

# A fixed demo key so the marker below is reproducible; real deployments use `openssl rand -hex 32`.
os.environ["VIEWPORT_REDACT_KEY"] = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"


@tool
async def charge_card(amount: float, card_token: SecretStr) -> dict[str, str]:
    """Charge a card."""
    raise RuntimeError("never runs: the tool server answers")


# One parser, written line for line in both languages: the same YAML loads to the same value everywhere.
expected = compile_matcher(load_yaml("tool_name: charge_card\nargs: { amount: { lte: 100 } }\n").value)


# The tool server answers where the tool body would run; Evals replays recorded results this way.
def replay(call: object) -> dict[str, object]:
    return {"result": {"charge_id": "ch_1"}}


async def main() -> None:
    async with trace(clock=FakeClock(), ids=SequentialIds()) as t:
        with tool_server(replay, warn=False):
            await charge_card(42.5, "tok_4242")

    call = t.trajectory.tool_calls()[0]
    print(call.id, call.started_at)
    print(stable_json(call.args))  # the server got "tok_4242"; the recording holds a marker
    print(stable_json(call.result), stable_json(call.attributes))
    print(expected.match(call).status)  # pass

    try:
        load_yaml("args:\n  amount: 1\n  amount: 2\n", file="case.yaml")
    except SpecError as err:
        print(err)


asyncio.run(main())
```

It prints:

```text
evt_1 2026-01-01T00:00:00.000Z
{"amount":42.5,"card_token":"[redacted hmac:b6c02ec2b0f4]"}
{"charge_id":"ch_1"} {"viewport.tool.served":true}
pass
case.yaml is invalid (1 issue)
  case.yaml:3:3 args.amount: duplicate key "amount" (first at line 2)
```

Source, docs and the TypeScript twin: https://github.com/viewportai/core
