toolsplice 0.0.1__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.
toolsplice/__init__.py ADDED
@@ -0,0 +1,111 @@
1
+ """Toolsplice's native, framework-independent Python core."""
2
+
3
+ from .core import (
4
+ Splice,
5
+ SpliceError,
6
+ SpliceCancelled,
7
+ ToolContract,
8
+ ToolCaller,
9
+ decode,
10
+ explain_error,
11
+ parse_plan,
12
+ prepare_splice,
13
+ )
14
+ from .recording import Recorder, replay
15
+
16
+ __version__ = "0.0.1"
17
+ __all__ = [
18
+ "Splice",
19
+ "SpliceError",
20
+ "SpliceCancelled",
21
+ "ToolContract",
22
+ "ToolCaller",
23
+ "decode",
24
+ "explain_error",
25
+ "parse_plan",
26
+ "prepare_splice",
27
+ "Recorder",
28
+ "replay",
29
+ ]
30
+
31
+ from .contracts import (
32
+ create_contract_from_catalog,
33
+ parse_contract_snapshot,
34
+ select_contract_tools,
35
+ compare_contract_snapshots,
36
+ contract_check_failed,
37
+ definition_contract,
38
+ )
39
+ from .preflight import preflight_splice
40
+ from .portable import (
41
+ create_splice_recorder,
42
+ parse_splice_recording,
43
+ replay_splice_recording,
44
+ )
45
+ from .corrections import parse_contract_correction, apply_contract_correction
46
+ from .execution import (
47
+ BindingCallOptions,
48
+ ToolError,
49
+ ContractValidationError,
50
+ create_tool_caller,
51
+ invoke_tool,
52
+ tool_functions,
53
+ create_contract_caller,
54
+ )
55
+ from .codegen import schema_to_python, emit_splice_module, emit_bindings, emit_module
56
+ from .projects import (
57
+ create_splice_project_files,
58
+ check_splice_project_files,
59
+ initialize_splice_project,
60
+ check_splice_project,
61
+ )
62
+ from .learning import (
63
+ empty_shape,
64
+ observe,
65
+ shape_from_values,
66
+ merge_into,
67
+ to_json_schema,
68
+ has_embedded_json,
69
+ is_safe_value,
70
+ observed_values,
71
+ looks_like_map,
72
+ )
73
+ from .store import (
74
+ ShapeStore,
75
+ ShapeStoreError,
76
+ new_server,
77
+ new_tool,
78
+ record_tool_list,
79
+ record_call,
80
+ tool_output,
81
+ apply_example_policy,
82
+ scrub,
83
+ extract,
84
+ )
85
+ from .diagnostics import (
86
+ create_contract_snapshot,
87
+ diagnose,
88
+ diagnose_tool,
89
+ format_doctor,
90
+ export_fingerprint,
91
+ learned_contract,
92
+ record_drift,
93
+ )
94
+ from .proxy import create_proxy, LearningProxy, describe_output
95
+ from .probe import probe, synthesize_args, list_all_tools
96
+ from .storage import LearningStore
97
+
98
+ __all__.append("LearningStore")
99
+
100
+ __all__ += [
101
+ name
102
+ for name in (
103
+ "create_contract_from_catalog parse_contract_snapshot select_contract_tools compare_contract_snapshots contract_check_failed definition_contract "
104
+ "preflight_splice create_splice_recorder parse_splice_recording replay_splice_recording parse_contract_correction apply_contract_correction "
105
+ "BindingCallOptions ToolError ContractValidationError create_tool_caller invoke_tool tool_functions create_contract_caller "
106
+ "schema_to_python emit_splice_module emit_bindings emit_module create_splice_project_files check_splice_project_files initialize_splice_project check_splice_project "
107
+ "empty_shape observe shape_from_values merge_into to_json_schema has_embedded_json is_safe_value observed_values looks_like_map "
108
+ "ShapeStore ShapeStoreError new_server new_tool record_tool_list record_call tool_output apply_example_policy scrub extract "
109
+ "create_contract_snapshot diagnose diagnose_tool format_doctor export_fingerprint learned_contract record_drift create_proxy LearningProxy describe_output probe synthesize_args list_all_tools"
110
+ ).split()
111
+ ]
toolsplice/__main__.py ADDED
@@ -0,0 +1,3 @@
1
+ from .cli import main
2
+
3
+ main()
toolsplice/_json.py ADDED
@@ -0,0 +1,137 @@
1
+ """Bounded JSON and own-path operations; no I/O or arbitrary object conversion."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import math
7
+ import re
8
+ from hashlib import sha256
9
+ from typing import Any
10
+
11
+ JSON_LIMIT = 5 * 1024 * 1024
12
+ MISSING = object()
13
+
14
+
15
+ def json_bytes(value: Any) -> bytes:
16
+ return json.dumps(
17
+ value, ensure_ascii=False, allow_nan=False, separators=(",", ":")
18
+ ).encode("utf-8")
19
+
20
+
21
+ def copy_json(
22
+ value: Any, limit: int = JSON_LIMIT, *, max_nodes=50000, max_depth=64
23
+ ) -> Any:
24
+ budget = [max_nodes]
25
+
26
+ def visit(v: Any, depth: int) -> None:
27
+ budget[0] -= 1
28
+ if depth > max_depth or budget[0] < 0:
29
+ raise ValueError("JSON complexity limit exceeded")
30
+ if v is None or type(v) in (str, bool):
31
+ return
32
+ if type(v) is int:
33
+ if abs(v) > 9007199254740991:
34
+ raise ValueError(
35
+ "JSON integers must be within the interoperable safe range"
36
+ )
37
+ return
38
+ if type(v) is float and math.isfinite(v):
39
+ return
40
+ if type(v) is list:
41
+ for item in v:
42
+ visit(item, depth + 1)
43
+ return
44
+ if type(v) is dict and all(type(k) is str for k in v):
45
+ for item in v.values():
46
+ visit(item, depth + 1)
47
+ return
48
+ raise ValueError("Expected finite JSON values and string object keys")
49
+
50
+ visit(value, 0)
51
+ try:
52
+ encoded = json_bytes(value)
53
+ except (ValueError, UnicodeError):
54
+ raise ValueError("Expected encodable JSON") from None
55
+ if len(encoded) > limit:
56
+ raise ValueError("JSON byte limit exceeded")
57
+ return json.loads(encoded)
58
+
59
+
60
+ def load_json(text: str) -> Any:
61
+ if not isinstance(text, str) or len(text.encode("utf-8")) > JSON_LIMIT:
62
+ raise ValueError("Expected bounded JSON text")
63
+ try:
64
+ return copy_json(json.loads(text))
65
+ except (ValueError, RecursionError, UnicodeError):
66
+ raise ValueError("Expected bounded finite JSON text") from None
67
+
68
+
69
+ def fingerprint(value: Any) -> str:
70
+ # Python recording identity, deliberately distinct from the JS artifact format.
71
+ value = copy_json(value)
72
+ return sha256(
73
+ json.dumps(
74
+ value,
75
+ sort_keys=True,
76
+ ensure_ascii=False,
77
+ allow_nan=False,
78
+ separators=(",", ":"),
79
+ ).encode("utf-8")
80
+ ).hexdigest()
81
+
82
+
83
+ def parts(pointer: str) -> list[str]:
84
+ if (
85
+ not isinstance(pointer, str)
86
+ or len(pointer) > 2048
87
+ or (pointer and not pointer.startswith("/"))
88
+ ):
89
+ raise ValueError("Expected bounded RFC 6901 pointer")
90
+ if re.search(r"~(?:[^01]|$)", pointer):
91
+ raise ValueError("Invalid pointer escape")
92
+ out = (
93
+ []
94
+ if not pointer
95
+ else [p.replace("~1", "/").replace("~0", "~") for p in pointer[1:].split("/")]
96
+ )
97
+ if len(out) > 32:
98
+ raise ValueError("Pointer depth limit exceeded")
99
+ return out
100
+
101
+
102
+ def escape(key: Any) -> str:
103
+ return str(key).replace("~", "~0").replace("/", "~1")
104
+
105
+
106
+ def get(value: Any, pointer: str) -> Any:
107
+ for key in parts(pointer):
108
+ if type(value) is dict and key in value:
109
+ value = value[key]
110
+ elif (
111
+ type(value) is list
112
+ and re.fullmatch(r"0|[1-9][0-9]*", key)
113
+ and int(key) < len(value)
114
+ ):
115
+ value = value[int(key)]
116
+ else:
117
+ return MISSING
118
+ return value
119
+
120
+
121
+ def put(value: dict, pointer: str, item: Any) -> None:
122
+ keys = parts(pointer)
123
+ if not keys:
124
+ raise ValueError("Destination pointer must name an object field")
125
+ for key in keys[:-1]:
126
+ value = value.setdefault(key, {})
127
+ value[keys[-1]] = copy_json(item)
128
+
129
+
130
+ def equal(a: Any, b: Any) -> bool:
131
+ if type(a) is bool or type(b) is bool:
132
+ return type(a) is type(b) and a == b
133
+ if type(a) is dict and type(b) is dict:
134
+ return a.keys() == b.keys() and all(equal(a[k], b[k]) for k in a)
135
+ if type(a) is list and type(b) is list:
136
+ return len(a) == len(b) and all(equal(x, y) for x, y in zip(a, b))
137
+ return a == b