statepatch 0.2.1__py3-none-any.whl → 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
statepatch/__init__.py CHANGED
@@ -7,16 +7,18 @@ stream at that path; removing it detaches it, live and reusable.
7
7
  """
8
8
 
9
9
  from ._core import Observable, Proxy, append, observable
10
- from ._wire import Listener, Op, Path, Segment, plain
10
+ from ._wire import Listener, Op, Path, Segment, WireEncodingError, plain, snapshot
11
11
 
12
12
  __all__ = [
13
13
  "observable",
14
14
  "Observable",
15
15
  "Proxy",
16
16
  "plain",
17
+ "snapshot",
17
18
  "append",
18
19
  "Op",
19
20
  "Path",
20
21
  "Segment",
21
22
  "Listener",
23
+ "WireEncodingError",
22
24
  ]
statepatch/_core.py CHANGED
@@ -4,7 +4,7 @@ from ._wire import _MISSING, Listener, Op, Path, Segment, _adopt, _index, _snaps
4
4
 
5
5
 
6
6
  class Observable:
7
- __slots__ = ("_state", "_listeners", "_mounts", "_parent")
7
+ __slots__ = ("_state", "_listeners", "_mounts", "_parent", "__weakref__")
8
8
 
9
9
  def __init__(self, initial: Any = None) -> None:
10
10
  if isinstance(initial, Observable):
statepatch/_wire.py CHANGED
@@ -21,9 +21,15 @@ _MAX_SAFE_INT = 2**53 - 1
21
21
  _MISSING: Any = object()
22
22
 
23
23
 
24
+ class WireEncodingError(ValueError):
25
+ """A value failed Statepatch's wire validation."""
26
+
27
+
24
28
  def _snapshot(node: Any) -> Any:
25
- from ._core import Observable
29
+ from ._core import Observable, Proxy
26
30
 
31
+ if isinstance(node, Proxy):
32
+ _, _, node = node._target()
27
33
  if isinstance(node, Observable):
28
34
  return _snapshot(node._state)
29
35
  if isinstance(node, dict):
@@ -46,6 +52,11 @@ def plain(value: Any) -> Any:
46
52
  return _snapshot(node) if mounted else node
47
53
 
48
54
 
55
+ def snapshot(value: Any) -> Any:
56
+ """Copy a value and its mounted observables into an independent plain tree."""
57
+ return _snapshot(value)
58
+
59
+
49
60
  def _adopt(value: Any, base: Path) -> tuple[Any, list[tuple[Path, "Observable"]]]:
50
61
  """Validated deep copy keeping ``Observable`` nodes in place; returns the
51
62
  stored tree and the mount points found inside it (paths rooted at ``base``)."""
@@ -63,17 +74,17 @@ def _adopt(value: Any, base: Path) -> tuple[Any, list[tuple[Path, "Observable"]]
63
74
  return value
64
75
  if isinstance(value, float):
65
76
  if math.isnan(value) or math.isinf(value):
66
- raise ValueError(f"{value!r} is not wire-encodable; floats must be finite")
77
+ raise WireEncodingError(f"{value!r} is not wire-encodable; floats must be finite")
67
78
  return value
68
79
  if isinstance(value, int):
69
80
  if abs(value) > _MAX_SAFE_INT:
70
- raise ValueError(
81
+ raise WireEncodingError(
71
82
  f"integer {value} exceeds the JS safe range (|n| <= 2**53-1); send it as a string"
72
83
  )
73
84
  return value
74
85
  if isinstance(value, datetime):
75
86
  if value.tzinfo is None or value.tzinfo.utcoffset(value) is None:
76
- raise ValueError("naive datetime is not wire-encodable; attach a timezone")
87
+ raise WireEncodingError("naive datetime is not wire-encodable; attach a timezone")
77
88
  return value
78
89
  if isinstance(value, (list, tuple)):
79
90
  return [walk(item, path + (i,)) for i, item in enumerate(value)]
@@ -0,0 +1,42 @@
1
+ Metadata-Version: 2.5
2
+ Name: statepatch
3
+ Version: 0.3.0
4
+ Summary: Immer-style ops over a plain JSON tree: observables that emit every mutation as an op
5
+ Project-URL: Repository, https://github.com/assistant-ui/harness-sdk
6
+ License-Expression: MIT
7
+ Requires-Python: >=3.11
8
+ Description-Content-Type: text/markdown
9
+
10
+ # statepatch
11
+
12
+ Immer-style ops over a plain JSON tree: `observable(initial)` wraps one value, `.value` reads it, and every mutation emits the matching `replace` / `add` / `remove` op to subscribers. Observables compose: one stored inside another mounts its op stream at that path. Part of [harness-sdk](https://github.com/assistant-ui/harness-sdk), under `statewire` and `pinned`.
13
+
14
+ ## Install
15
+
16
+ ```sh
17
+ uv add statepatch
18
+ ```
19
+
20
+ ## Use
21
+
22
+ ```python
23
+ from statepatch import observable
24
+
25
+ state = observable({"runs": []})
26
+ state.subscribe(print)
27
+
28
+ run = observable({"runId": "r1", "queue": []})
29
+ state["runs"].append(run) # add op at ["runs", 0]
30
+ run["queue"].append({"id": "q1"}) # forwarded as add at ["runs", 0, "queue", 0]
31
+ state["runs"].pop(0) # remove op; run is detached and reusable
32
+ ```
33
+
34
+ ## Validation
35
+
36
+ `from statepatch import WireEncodingError` exposes the `ValueError` subclass for invalid wire values.
37
+
38
+ ## Docs
39
+
40
+ - Reference: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/app/docs/reference/packages/page.mdx
41
+ - Guide: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/app/docs/guides/statewire/host/page.mdx
42
+ - Spec: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/app/docs/spec/statewire-protocol/statepatch/page.mdx
@@ -0,0 +1,6 @@
1
+ statepatch/__init__.py,sha256=XoHpDb8Kj8pq2ThmMTdfOHO31ughbii-pdW-LXx5R4I,661
2
+ statepatch/_core.py,sha256=lP_EJmpxp0oYkBIA43MsmVsevDScpxleatkiIQC-RWE,16563
3
+ statepatch/_wire.py,sha256=zs7wNJXK2J_xhMR6GEeBN1yfmMiEG9pfv5BK5VONYF4,3679
4
+ statepatch-0.3.0.dist-info/METADATA,sha256=p7Pxe3mA9V9C-7Rx-8kzm1b_1OARIp9Ab4MKdSnbmC4,1587
5
+ statepatch-0.3.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
6
+ statepatch-0.3.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.32.0
2
+ Generator: hatchling 1.32.4
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,26 +0,0 @@
1
- Metadata-Version: 2.5
2
- Name: statepatch
3
- Version: 0.2.1
4
- Summary: Immer-style ops over a plain JSON tree: observables that emit every mutation as an op
5
- Project-URL: Repository, https://github.com/assistant-ui/harness-sdk
6
- License-Expression: MIT
7
- Requires-Python: >=3.11
8
- Description-Content-Type: text/markdown
9
-
10
- # statepatch
11
-
12
- Immer-style ops over a plain JSON tree.
13
-
14
- `observable(initial)` wraps one value: `.value` reads it, every mutation applies to the tree and emits the matching op (`replace` / `add` / `remove`) to subscribers. Observables compose: an observable stored inside another mounts its op stream at that path.
15
-
16
- ```python
17
- from statepatch import observable
18
-
19
- state = observable({"runs": []})
20
- state.subscribe(print)
21
-
22
- run = observable({"runId": "r1", "queue": []})
23
- state["runs"].append(run) # add op at ["runs", 0]
24
- run["queue"].append({"id": "q1"}) # forwarded as add at ["runs", 0, "queue", 0]
25
- state["runs"].pop(0) # remove op; run is detached and reusable
26
- ```
@@ -1,6 +0,0 @@
1
- statepatch/__init__.py,sha256=VC8GH7hvrO8hmh67wqYPB-nG7TTtQlCYxbaf2P27fCg,591
2
- statepatch/_core.py,sha256=UzFwgQhQBHCptLf4sA_GUdFwXw-AcnyVWPAZwUxStG4,16548
3
- statepatch/_wire.py,sha256=dksEVsJds4MTY4_Wg9PmfHMJATcNAvLZdHnYQWrd0jg,3343
4
- statepatch-0.2.1.dist-info/METADATA,sha256=TuDTL6OF2WH2OwBE2RH91h-s0owbO1ox1x50a6Krqmw,991
5
- statepatch-0.2.1.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
6
- statepatch-0.2.1.dist-info/RECORD,,