|
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | | -""" |
6 | | -Tokonomics: Explorable and Referenceable Tools. |
7 | | -
|
8 | | -=============================================== |
9 | | -
|
10 | | -A library that equips LLM‑agents with: |
11 | | -
|
12 | | -* TTL‑based object storage |
13 | | -* Rich object exploration & reference passing |
14 | | -* Smart decorators for explorable outputs and referenceable inputs |
15 | | -* Strict typing / signature preservation (incl. *async* callables) |
16 | | -* ReST‑style docstring enhancement |
17 | | -* Configurable preview truncation (`max_bytes`) and a user‑supplied |
18 | | - `preview_callback` for custom renderings (e.g. pandas.DataFrame) |
19 | | -""" |
20 | | - |
21 | 5 | from __future__ import annotations |
22 | 6 |
|
23 | 7 | import logging |
24 | 8 | import time |
25 | 9 | import uuid |
26 | 10 | from typing import ( |
27 | 11 | Any, |
28 | | - Generic, |
29 | 12 | Protocol, |
30 | | - TypeVar, |
31 | 13 | ) |
32 | 14 |
|
33 | 15 | import orjson |
34 | 16 |
|
35 | 17 | logger = logging.getLogger(__name__) |
36 | | -# ============================================================================= |
37 | | -# 1 · Generic return‑value wrapper |
38 | | -# ============================================================================= |
39 | | - |
40 | | -_T = TypeVar("_T") |
41 | | - |
42 | | - |
43 | | -class Explorable(Generic[_T]): |
44 | | - """ |
45 | | - Wrapper returned by ``@explorable`` decorated tools. |
46 | | -
|
47 | | - Attributes |
48 | | - ---------- |
49 | | - obj_id : |
50 | | - The internal identifier of the stored object. |
51 | | - value : |
52 | | - The original, *unmodified* object returned by the wrapped tool. |
53 | | - preview : |
54 | | - Rich‑rendered string generated by the explorer. |
55 | | - """ |
56 | | - |
57 | | - __slots__ = ("obj_id", "value", "_preview") |
58 | | - |
59 | | - def __init__(self, obj_id: str, value: _T, preview: str) -> None: |
60 | | - """Initialize Explorable wrapper with object ID, value, and preview.""" |
61 | | - self.obj_id = obj_id |
62 | | - self.value: _T = value |
63 | | - self._preview = preview |
64 | | - |
65 | | - # –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– |
66 | | - # Representations |
67 | | - # –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– |
68 | | - |
69 | | - def __str__(self) -> str: |
70 | | - """Return the preview string representation.""" |
71 | | - return self._preview |
72 | | - |
73 | | - __repr__ = __str__ |
74 | | - |
75 | | - # –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– |
76 | | - # Convenience for notebooks / REPLs |
77 | | - # –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– |
78 | | - |
79 | | - _ipython_display_ = __str__ # Jupyter friendly |
80 | | - |
81 | | - |
82 | | -# ============================================================================= |
83 | | -# 2 · ObjectStore Backend Protocol |
84 | | -# ============================================================================= |
85 | 18 |
|
86 | 19 |
|
87 | 20 | class ObjectStoreBackend(Protocol): |
@@ -178,11 +111,6 @@ def delete(self, key: str) -> bool: |
178 | 111 | return bool(self._client.delete(key)) |
179 | 112 |
|
180 | 113 |
|
181 | | -# ============================================================================= |
182 | | -# 3 · Time‑to‑live based object store |
183 | | -# ============================================================================= |
184 | | - |
185 | | - |
186 | 114 | class ObjectStore: |
187 | 115 | """JSON-based object store with pluggable backends.""" |
188 | 116 |
|
|
0 commit comments