dr-graph 0.1.0__tar.gz
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.
- dr_graph-0.1.0/.gitignore +26 -0
- dr_graph-0.1.0/LICENSE +21 -0
- dr_graph-0.1.0/PKG-INFO +107 -0
- dr_graph-0.1.0/README.md +86 -0
- dr_graph-0.1.0/pyproject.toml +106 -0
- dr_graph-0.1.0/src/dr_graph/__init__.py +82 -0
- dr_graph-0.1.0/src/dr_graph/builders.py +70 -0
- dr_graph-0.1.0/src/dr_graph/compose.py +109 -0
- dr_graph-0.1.0/src/dr_graph/definition.py +166 -0
- dr_graph-0.1.0/src/dr_graph/errors.py +21 -0
- dr_graph-0.1.0/src/dr_graph/execution.py +250 -0
- dr_graph-0.1.0/src/dr_graph/hashing.py +61 -0
- dr_graph-0.1.0/src/dr_graph/py.typed +0 -0
- dr_graph-0.1.0/src/dr_graph/refs.py +109 -0
- dr_graph-0.1.0/src/dr_graph/results.py +312 -0
- dr_graph-0.1.0/src/dr_graph/spec.py +154 -0
- dr_graph-0.1.0/src/dr_graph/validation.py +145 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
.worktrees
|
|
2
|
+
.DS_Store
|
|
3
|
+
*.swp
|
|
4
|
+
.env
|
|
5
|
+
.env.*
|
|
6
|
+
!.env.example
|
|
7
|
+
.cache/
|
|
8
|
+
.ruff_cache/
|
|
9
|
+
.mypy_cache/
|
|
10
|
+
.pytest_cache/
|
|
11
|
+
.pyre/
|
|
12
|
+
.pyright/
|
|
13
|
+
.venv/
|
|
14
|
+
build/
|
|
15
|
+
dist/
|
|
16
|
+
*.egg-info/
|
|
17
|
+
htmlcov/
|
|
18
|
+
.coverage
|
|
19
|
+
.coverage.*
|
|
20
|
+
.hypothesis/
|
|
21
|
+
.nox/
|
|
22
|
+
.tox/
|
|
23
|
+
.docs/
|
|
24
|
+
**/__pycache__/
|
|
25
|
+
*.py[cod]
|
|
26
|
+
graphify-out/
|
dr_graph-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 danielle rothermel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
dr_graph-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dr-graph
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Hashable graph configs plus a pure, deterministic interpreter.
|
|
5
|
+
Project-URL: Repository, https://github.com/danielle-rothermel/dr-graph
|
|
6
|
+
Project-URL: Issues, https://github.com/danielle-rothermel/dr-graph/issues
|
|
7
|
+
Author-email: Danielle Rothermel <danielle.rothermel@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Python: >=3.12
|
|
18
|
+
Requires-Dist: dr-serialize<0.2.0,>=0.1.0
|
|
19
|
+
Requires-Dist: pydantic>=2.13.4
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# dr-graph
|
|
23
|
+
|
|
24
|
+
Hashable computation-graph configs plus a pure, deterministic
|
|
25
|
+
interpreter. Not a workflow engine.
|
|
26
|
+
|
|
27
|
+
The rule this library exists to serve: **whatever is searched over must
|
|
28
|
+
be data; whatever does the searching is code.** Graph configs are the
|
|
29
|
+
searched-over layer — the motivating use case is experiment conditions
|
|
30
|
+
and optimizer genomes. Optimizers and durable workflows are ordinary
|
|
31
|
+
code that read and write these configs.
|
|
32
|
+
|
|
33
|
+
The [vocabulary sheet](https://danielle-rothermel.github.io/dr-graph/)
|
|
34
|
+
(source: `.defs/vocab.html`) is the authoritative contract: the terms,
|
|
35
|
+
guarantees, scope boundaries, and the mapping from each term to the
|
|
36
|
+
exported names. This README orients; the sheet decides.
|
|
37
|
+
|
|
38
|
+
## What it provides
|
|
39
|
+
|
|
40
|
+
- **`GraphDefinition` / `NodeDefinition`** — a versioned,
|
|
41
|
+
variable-bearing DAG shape that `materialize()`s fully-set
|
|
42
|
+
`GraphConfig`s from per-node Variable assignments.
|
|
43
|
+
- **`GraphConfig` / `NodeConfig`** — the flat, validated config that a
|
|
44
|
+
graph hash identifies; construction enforces the structural
|
|
45
|
+
guarantees (acyclicity, exactly one terminal node, input-source
|
|
46
|
+
legality).
|
|
47
|
+
- **`graph_hash()`** — the config's identity, computed through
|
|
48
|
+
dr-serialize's identity API; the sheet defines its exact coverage
|
|
49
|
+
and format.
|
|
50
|
+
- **`execute_graph()`** — pure sequential execution: resolves each
|
|
51
|
+
node's inputs, calls the injected `run_node` callback, and returns a
|
|
52
|
+
`GraphRunResult`. An optional `completed` mapping skips
|
|
53
|
+
already-paid-for nodes on resume.
|
|
54
|
+
- **`node()` / `graph()` / `inline_subgraph()`** — neutral builders and
|
|
55
|
+
composition by flattening.
|
|
56
|
+
|
|
57
|
+
Everything else — durability, retries, scheduling, persistence,
|
|
58
|
+
prompts, providers — belongs to the caller; the sheet draws the exact
|
|
59
|
+
line. Sequential execution is a feature: deterministic order is what
|
|
60
|
+
makes durable-workflow replay line up.
|
|
61
|
+
|
|
62
|
+
## Ecosystem
|
|
63
|
+
|
|
64
|
+
Part of the `dr-*` family: depends on `dr-serialize` (identity
|
|
65
|
+
hashing); consumed by `whetstone-ai`. Neighbor repos are
|
|
66
|
+
`dr-providers`, `dr-platform`, `dr-code`, and `unitbench`.
|
|
67
|
+
|
|
68
|
+
## Example
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from dr_graph import execute_graph, graph, node
|
|
72
|
+
|
|
73
|
+
config = graph(
|
|
74
|
+
[
|
|
75
|
+
node(
|
|
76
|
+
"encoder",
|
|
77
|
+
node_type="llm_call",
|
|
78
|
+
input_sources={"prompt": "task.prompt"},
|
|
79
|
+
output_field="description",
|
|
80
|
+
),
|
|
81
|
+
node(
|
|
82
|
+
"decoder",
|
|
83
|
+
node_type="llm_call",
|
|
84
|
+
input_sources={"description": "encoder.description"},
|
|
85
|
+
output_field="code",
|
|
86
|
+
),
|
|
87
|
+
],
|
|
88
|
+
terminal="decoder",
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
result = execute_graph(
|
|
92
|
+
graph=config,
|
|
93
|
+
inputs={"prompt": "write an add function"},
|
|
94
|
+
run_node=lambda node_config, inputs: {
|
|
95
|
+
"values": {node_config.output_field: "..."}
|
|
96
|
+
},
|
|
97
|
+
)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Development
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
uv sync
|
|
104
|
+
uv run pytest
|
|
105
|
+
uv run ruff check .
|
|
106
|
+
uv run ty check
|
|
107
|
+
```
|
dr_graph-0.1.0/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# dr-graph
|
|
2
|
+
|
|
3
|
+
Hashable computation-graph configs plus a pure, deterministic
|
|
4
|
+
interpreter. Not a workflow engine.
|
|
5
|
+
|
|
6
|
+
The rule this library exists to serve: **whatever is searched over must
|
|
7
|
+
be data; whatever does the searching is code.** Graph configs are the
|
|
8
|
+
searched-over layer — the motivating use case is experiment conditions
|
|
9
|
+
and optimizer genomes. Optimizers and durable workflows are ordinary
|
|
10
|
+
code that read and write these configs.
|
|
11
|
+
|
|
12
|
+
The [vocabulary sheet](https://danielle-rothermel.github.io/dr-graph/)
|
|
13
|
+
(source: `.defs/vocab.html`) is the authoritative contract: the terms,
|
|
14
|
+
guarantees, scope boundaries, and the mapping from each term to the
|
|
15
|
+
exported names. This README orients; the sheet decides.
|
|
16
|
+
|
|
17
|
+
## What it provides
|
|
18
|
+
|
|
19
|
+
- **`GraphDefinition` / `NodeDefinition`** — a versioned,
|
|
20
|
+
variable-bearing DAG shape that `materialize()`s fully-set
|
|
21
|
+
`GraphConfig`s from per-node Variable assignments.
|
|
22
|
+
- **`GraphConfig` / `NodeConfig`** — the flat, validated config that a
|
|
23
|
+
graph hash identifies; construction enforces the structural
|
|
24
|
+
guarantees (acyclicity, exactly one terminal node, input-source
|
|
25
|
+
legality).
|
|
26
|
+
- **`graph_hash()`** — the config's identity, computed through
|
|
27
|
+
dr-serialize's identity API; the sheet defines its exact coverage
|
|
28
|
+
and format.
|
|
29
|
+
- **`execute_graph()`** — pure sequential execution: resolves each
|
|
30
|
+
node's inputs, calls the injected `run_node` callback, and returns a
|
|
31
|
+
`GraphRunResult`. An optional `completed` mapping skips
|
|
32
|
+
already-paid-for nodes on resume.
|
|
33
|
+
- **`node()` / `graph()` / `inline_subgraph()`** — neutral builders and
|
|
34
|
+
composition by flattening.
|
|
35
|
+
|
|
36
|
+
Everything else — durability, retries, scheduling, persistence,
|
|
37
|
+
prompts, providers — belongs to the caller; the sheet draws the exact
|
|
38
|
+
line. Sequential execution is a feature: deterministic order is what
|
|
39
|
+
makes durable-workflow replay line up.
|
|
40
|
+
|
|
41
|
+
## Ecosystem
|
|
42
|
+
|
|
43
|
+
Part of the `dr-*` family: depends on `dr-serialize` (identity
|
|
44
|
+
hashing); consumed by `whetstone-ai`. Neighbor repos are
|
|
45
|
+
`dr-providers`, `dr-platform`, `dr-code`, and `unitbench`.
|
|
46
|
+
|
|
47
|
+
## Example
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from dr_graph import execute_graph, graph, node
|
|
51
|
+
|
|
52
|
+
config = graph(
|
|
53
|
+
[
|
|
54
|
+
node(
|
|
55
|
+
"encoder",
|
|
56
|
+
node_type="llm_call",
|
|
57
|
+
input_sources={"prompt": "task.prompt"},
|
|
58
|
+
output_field="description",
|
|
59
|
+
),
|
|
60
|
+
node(
|
|
61
|
+
"decoder",
|
|
62
|
+
node_type="llm_call",
|
|
63
|
+
input_sources={"description": "encoder.description"},
|
|
64
|
+
output_field="code",
|
|
65
|
+
),
|
|
66
|
+
],
|
|
67
|
+
terminal="decoder",
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
result = execute_graph(
|
|
71
|
+
graph=config,
|
|
72
|
+
inputs={"prompt": "write an add function"},
|
|
73
|
+
run_node=lambda node_config, inputs: {
|
|
74
|
+
"values": {node_config.output_field: "..."}
|
|
75
|
+
},
|
|
76
|
+
)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Development
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
uv sync
|
|
83
|
+
uv run pytest
|
|
84
|
+
uv run ruff check .
|
|
85
|
+
uv run ty check
|
|
86
|
+
```
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "dr-graph"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Hashable graph configs plus a pure, deterministic interpreter."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = [
|
|
8
|
+
{ name = "Danielle Rothermel", email = "danielle.rothermel@gmail.com" }
|
|
9
|
+
]
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 4 - Beta",
|
|
13
|
+
"Intended Audience :: Developers",
|
|
14
|
+
"License :: OSI Approved :: MIT License",
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Programming Language :: Python :: 3.12",
|
|
17
|
+
"Programming Language :: Python :: 3.13",
|
|
18
|
+
"Typing :: Typed",
|
|
19
|
+
]
|
|
20
|
+
dependencies = [
|
|
21
|
+
# dr-serialize's canonicalization defines graph_hash; bound to the tested
|
|
22
|
+
# 0.1.x range so an upgrade cannot change identities without a golden
|
|
23
|
+
# review.
|
|
24
|
+
"dr-serialize>=0.1.0,<0.2.0",
|
|
25
|
+
"pydantic>=2.13.4",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Repository = "https://github.com/danielle-rothermel/dr-graph"
|
|
30
|
+
Issues = "https://github.com/danielle-rothermel/dr-graph/issues"
|
|
31
|
+
|
|
32
|
+
[build-system]
|
|
33
|
+
requires = ["hatchling"]
|
|
34
|
+
build-backend = "hatchling.build"
|
|
35
|
+
|
|
36
|
+
[dependency-groups]
|
|
37
|
+
dev = [
|
|
38
|
+
"pre-commit>=4.6.0",
|
|
39
|
+
"pytest>=9.1.1",
|
|
40
|
+
"ruff>=0.15.18",
|
|
41
|
+
"ty>=0.0.51",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
[tool.hatch.build.targets.sdist]
|
|
45
|
+
only-include = ["src"]
|
|
46
|
+
|
|
47
|
+
[tool.pytest.ini_options]
|
|
48
|
+
testpaths = ["tests"]
|
|
49
|
+
|
|
50
|
+
[tool.pyright]
|
|
51
|
+
venvPath = "."
|
|
52
|
+
venv = ".venv"
|
|
53
|
+
|
|
54
|
+
[tool.ruff]
|
|
55
|
+
include = ["src/**/*.py", "tests/**/*.py"]
|
|
56
|
+
line-length = 79
|
|
57
|
+
|
|
58
|
+
[tool.ruff.lint]
|
|
59
|
+
select = [
|
|
60
|
+
"A",
|
|
61
|
+
"ARG",
|
|
62
|
+
"ASYNC",
|
|
63
|
+
"B",
|
|
64
|
+
"BLE",
|
|
65
|
+
"C4",
|
|
66
|
+
"DTZ",
|
|
67
|
+
"E",
|
|
68
|
+
"F",
|
|
69
|
+
"FA",
|
|
70
|
+
"FBT",
|
|
71
|
+
"FLY",
|
|
72
|
+
"FURB",
|
|
73
|
+
"G",
|
|
74
|
+
"I",
|
|
75
|
+
"ICN",
|
|
76
|
+
"ISC",
|
|
77
|
+
"LOG",
|
|
78
|
+
"N",
|
|
79
|
+
"NPY",
|
|
80
|
+
"PD",
|
|
81
|
+
"PERF",
|
|
82
|
+
"PIE",
|
|
83
|
+
"PL",
|
|
84
|
+
"PTH",
|
|
85
|
+
"PT",
|
|
86
|
+
"RET",
|
|
87
|
+
"RSE",
|
|
88
|
+
"RUF",
|
|
89
|
+
"S",
|
|
90
|
+
"SIM",
|
|
91
|
+
"SLOT",
|
|
92
|
+
"T10",
|
|
93
|
+
"TC",
|
|
94
|
+
"TID",
|
|
95
|
+
"TRY",
|
|
96
|
+
"UP",
|
|
97
|
+
"W",
|
|
98
|
+
"YTT",
|
|
99
|
+
]
|
|
100
|
+
ignore = ["PLR1711", "S101", "TRY003"]
|
|
101
|
+
|
|
102
|
+
[tool.ruff.lint.per-file-ignores]
|
|
103
|
+
"tests/**/*.py" = ["PLR2004", "PLC0415", "ARG001", "ARG005", "S603", "TC003"]
|
|
104
|
+
|
|
105
|
+
[tool.ty.src]
|
|
106
|
+
include = ["src", "tests"]
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""Hashable computation-graph configs and a pure deterministic interpreter."""
|
|
2
|
+
|
|
3
|
+
from dr_graph.builders import as_node_input_source_ref, graph, node
|
|
4
|
+
from dr_graph.compose import inline_subgraph
|
|
5
|
+
from dr_graph.definition import GraphDefinition, NodeDefinition
|
|
6
|
+
from dr_graph.errors import (
|
|
7
|
+
CompletedNodeError,
|
|
8
|
+
GraphExecutionError,
|
|
9
|
+
GraphValidationError,
|
|
10
|
+
InputResolutionError,
|
|
11
|
+
NodeExecutionError,
|
|
12
|
+
)
|
|
13
|
+
from dr_graph.execution import (
|
|
14
|
+
RunNode,
|
|
15
|
+
execute_graph,
|
|
16
|
+
resolve_node_inputs,
|
|
17
|
+
)
|
|
18
|
+
from dr_graph.hashing import (
|
|
19
|
+
GRAPH_CONFIG_IDENTITY_SCHEMA,
|
|
20
|
+
GRAPH_CONFIG_IDENTITY_SCHEMA_VERSION,
|
|
21
|
+
graph_config_identity_document,
|
|
22
|
+
graph_config_identity_payload,
|
|
23
|
+
graph_hash,
|
|
24
|
+
)
|
|
25
|
+
from dr_graph.refs import (
|
|
26
|
+
NodeInputSourceKind,
|
|
27
|
+
NodeInputSourceRef,
|
|
28
|
+
)
|
|
29
|
+
from dr_graph.results import (
|
|
30
|
+
ClassifiedFailure,
|
|
31
|
+
GraphRunResult,
|
|
32
|
+
GraphRunStatus,
|
|
33
|
+
NodeError,
|
|
34
|
+
NodeOutcome,
|
|
35
|
+
NodeOutcomeStatus,
|
|
36
|
+
NodeOutput,
|
|
37
|
+
TerminalError,
|
|
38
|
+
)
|
|
39
|
+
from dr_graph.spec import (
|
|
40
|
+
FieldRole,
|
|
41
|
+
GraphConfig,
|
|
42
|
+
NodeConfig,
|
|
43
|
+
NodeFieldSpec,
|
|
44
|
+
)
|
|
45
|
+
from dr_graph.validation import validate_graph_external_inputs
|
|
46
|
+
|
|
47
|
+
__all__ = [
|
|
48
|
+
"GRAPH_CONFIG_IDENTITY_SCHEMA",
|
|
49
|
+
"GRAPH_CONFIG_IDENTITY_SCHEMA_VERSION",
|
|
50
|
+
"ClassifiedFailure",
|
|
51
|
+
"CompletedNodeError",
|
|
52
|
+
"FieldRole",
|
|
53
|
+
"GraphConfig",
|
|
54
|
+
"GraphDefinition",
|
|
55
|
+
"GraphExecutionError",
|
|
56
|
+
"GraphRunResult",
|
|
57
|
+
"GraphRunStatus",
|
|
58
|
+
"GraphValidationError",
|
|
59
|
+
"InputResolutionError",
|
|
60
|
+
"NodeConfig",
|
|
61
|
+
"NodeDefinition",
|
|
62
|
+
"NodeError",
|
|
63
|
+
"NodeExecutionError",
|
|
64
|
+
"NodeFieldSpec",
|
|
65
|
+
"NodeInputSourceKind",
|
|
66
|
+
"NodeInputSourceRef",
|
|
67
|
+
"NodeOutcome",
|
|
68
|
+
"NodeOutcomeStatus",
|
|
69
|
+
"NodeOutput",
|
|
70
|
+
"RunNode",
|
|
71
|
+
"TerminalError",
|
|
72
|
+
"as_node_input_source_ref",
|
|
73
|
+
"execute_graph",
|
|
74
|
+
"graph",
|
|
75
|
+
"graph_config_identity_document",
|
|
76
|
+
"graph_config_identity_payload",
|
|
77
|
+
"graph_hash",
|
|
78
|
+
"inline_subgraph",
|
|
79
|
+
"node",
|
|
80
|
+
"resolve_node_inputs",
|
|
81
|
+
"validate_graph_external_inputs",
|
|
82
|
+
]
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""Neutral config-assembly helpers.
|
|
2
|
+
|
|
3
|
+
These cover the common case — node input sources, one declared output field,
|
|
4
|
+
open Variable assignments — without any prompt or provider awareness.
|
|
5
|
+
Domain-aware builders belong app-side.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import TYPE_CHECKING, Any
|
|
11
|
+
|
|
12
|
+
if TYPE_CHECKING:
|
|
13
|
+
from collections.abc import Mapping, Sequence
|
|
14
|
+
|
|
15
|
+
from dr_graph.refs import NodeInputSourceRef
|
|
16
|
+
from dr_graph.spec import FieldRole, GraphConfig, NodeConfig, NodeFieldSpec
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def as_node_input_source_ref(
|
|
20
|
+
ref: str | NodeInputSourceRef,
|
|
21
|
+
) -> NodeInputSourceRef:
|
|
22
|
+
if isinstance(ref, NodeInputSourceRef):
|
|
23
|
+
return ref
|
|
24
|
+
return NodeInputSourceRef.model_validate(ref)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def node( # noqa: PLR0913 -- the config surface, not incidental knobs
|
|
28
|
+
node_id: str,
|
|
29
|
+
*,
|
|
30
|
+
node_type: str,
|
|
31
|
+
output_field: str,
|
|
32
|
+
input_sources: Mapping[str, str | NodeInputSourceRef] | None = None,
|
|
33
|
+
fields: Sequence[NodeFieldSpec] | None = None,
|
|
34
|
+
variables: Mapping[str, Any] | None = None,
|
|
35
|
+
) -> NodeConfig:
|
|
36
|
+
sources = {
|
|
37
|
+
name: as_node_input_source_ref(ref)
|
|
38
|
+
for name, ref in (input_sources or {}).items()
|
|
39
|
+
}
|
|
40
|
+
if fields is None:
|
|
41
|
+
derived = [
|
|
42
|
+
NodeFieldSpec(name=name, role=FieldRole.INPUT) for name in sources
|
|
43
|
+
]
|
|
44
|
+
derived.append(NodeFieldSpec(name=output_field, role=FieldRole.OUTPUT))
|
|
45
|
+
field_specs = tuple(derived)
|
|
46
|
+
else:
|
|
47
|
+
field_specs = tuple(fields)
|
|
48
|
+
return NodeConfig.model_validate(
|
|
49
|
+
{
|
|
50
|
+
"node_id": node_id,
|
|
51
|
+
"node_type": node_type,
|
|
52
|
+
"fields": field_specs,
|
|
53
|
+
"input_sources": sources,
|
|
54
|
+
"output_field": output_field,
|
|
55
|
+
"variables": dict(variables or {}),
|
|
56
|
+
}
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def graph(
|
|
61
|
+
nodes: Sequence[NodeConfig],
|
|
62
|
+
*,
|
|
63
|
+
terminal: str,
|
|
64
|
+
) -> GraphConfig:
|
|
65
|
+
return GraphConfig.model_validate(
|
|
66
|
+
{
|
|
67
|
+
"nodes": tuple(nodes),
|
|
68
|
+
"terminal_node_id": terminal,
|
|
69
|
+
}
|
|
70
|
+
)
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""Inline subgraph composition.
|
|
2
|
+
|
|
3
|
+
v1 represents composition by flattening: `inline_subgraph` returns the
|
|
4
|
+
subgraph's nodes renamed under a prefix, with internal input sources rewired
|
|
5
|
+
and external inputs optionally rebound to parent-side sources. The composed
|
|
6
|
+
graph is an ordinary `GraphConfig`; its `graph_hash` is the hash of the
|
|
7
|
+
flattened config.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from typing import TYPE_CHECKING
|
|
13
|
+
|
|
14
|
+
from dr_graph.builders import as_node_input_source_ref
|
|
15
|
+
from dr_graph.refs import (
|
|
16
|
+
REF_SEPARATOR,
|
|
17
|
+
NodeInputSourceKind,
|
|
18
|
+
NodeInputSourceRef,
|
|
19
|
+
)
|
|
20
|
+
from dr_graph.spec import GraphConfig, NodeConfig
|
|
21
|
+
from dr_graph.validation import external_input_fields
|
|
22
|
+
|
|
23
|
+
if TYPE_CHECKING:
|
|
24
|
+
from collections.abc import Mapping
|
|
25
|
+
|
|
26
|
+
DEFAULT_SUBGRAPH_SEPARATOR = ":"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def prefixed_node_id(
|
|
30
|
+
prefix: str,
|
|
31
|
+
node_id: str,
|
|
32
|
+
*,
|
|
33
|
+
separator: str = DEFAULT_SUBGRAPH_SEPARATOR,
|
|
34
|
+
) -> str:
|
|
35
|
+
return f"{prefix}{separator}{node_id}"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def inline_subgraph(
|
|
39
|
+
subgraph: GraphConfig,
|
|
40
|
+
*,
|
|
41
|
+
prefix: str,
|
|
42
|
+
input_sources: Mapping[str, str | NodeInputSourceRef] | None = None,
|
|
43
|
+
separator: str = DEFAULT_SUBGRAPH_SEPARATOR,
|
|
44
|
+
) -> tuple[NodeConfig, ...]:
|
|
45
|
+
"""Return the subgraph's nodes renamed and rewired for a parent graph.
|
|
46
|
+
|
|
47
|
+
``input_sources`` maps external input fields of the subgraph to parent-side
|
|
48
|
+
sources (parent node outputs or parent external inputs). Unmapped external
|
|
49
|
+
inputs pass through unchanged and must be satisfied by the parent graph's
|
|
50
|
+
external inputs.
|
|
51
|
+
"""
|
|
52
|
+
if not prefix:
|
|
53
|
+
raise ValueError("prefix must be non-empty")
|
|
54
|
+
if REF_SEPARATOR in prefix:
|
|
55
|
+
raise ValueError(f"prefix {prefix!r} cannot contain {REF_SEPARATOR!r}")
|
|
56
|
+
if not separator or REF_SEPARATOR in separator:
|
|
57
|
+
raise ValueError(
|
|
58
|
+
f"separator {separator!r} must be non-empty and cannot "
|
|
59
|
+
f"contain {REF_SEPARATOR!r}"
|
|
60
|
+
)
|
|
61
|
+
remapped = {
|
|
62
|
+
name: as_node_input_source_ref(ref)
|
|
63
|
+
for name, ref in (input_sources or {}).items()
|
|
64
|
+
}
|
|
65
|
+
unknown = sorted(set(remapped) - external_input_fields(subgraph))
|
|
66
|
+
if unknown:
|
|
67
|
+
unknown_list = ", ".join(repr(name) for name in unknown)
|
|
68
|
+
raise ValueError(
|
|
69
|
+
f"input source(s) {unknown_list} are not external inputs "
|
|
70
|
+
"of the subgraph"
|
|
71
|
+
)
|
|
72
|
+
nodes: list[NodeConfig] = []
|
|
73
|
+
for node in subgraph.nodes:
|
|
74
|
+
node_input_sources: dict[str, NodeInputSourceRef] = {}
|
|
75
|
+
for field_name, ref in node.input_sources.items():
|
|
76
|
+
if ref.kind is NodeInputSourceKind.GRAPH_EXTERNAL:
|
|
77
|
+
if ref.field is not None and ref.field in remapped:
|
|
78
|
+
node_input_sources[field_name] = remapped[ref.field]
|
|
79
|
+
else:
|
|
80
|
+
node_input_sources[field_name] = ref
|
|
81
|
+
continue
|
|
82
|
+
node_input_sources[field_name] = NodeInputSourceRef.model_validate(
|
|
83
|
+
{
|
|
84
|
+
"kind": NodeInputSourceKind.NODE_OUTPUT,
|
|
85
|
+
"node_id": prefixed_node_id(
|
|
86
|
+
prefix,
|
|
87
|
+
str(ref.node_id),
|
|
88
|
+
separator=separator,
|
|
89
|
+
),
|
|
90
|
+
"field": ref.field,
|
|
91
|
+
}
|
|
92
|
+
)
|
|
93
|
+
nodes.append(
|
|
94
|
+
NodeConfig.model_validate(
|
|
95
|
+
{
|
|
96
|
+
"node_id": prefixed_node_id(
|
|
97
|
+
prefix,
|
|
98
|
+
node.node_id,
|
|
99
|
+
separator=separator,
|
|
100
|
+
),
|
|
101
|
+
"node_type": node.node_type,
|
|
102
|
+
"fields": node.fields,
|
|
103
|
+
"input_sources": node_input_sources,
|
|
104
|
+
"output_field": node.output_field,
|
|
105
|
+
"variables": dict(node.variables),
|
|
106
|
+
}
|
|
107
|
+
)
|
|
108
|
+
)
|
|
109
|
+
return tuple(nodes)
|