Skip to content

Commit ab2d2ce

Browse files
committed
published too processor
1 parent 916e7dc commit ab2d2ce

File tree

3 files changed

+75
-54
lines changed

3 files changed

+75
-54
lines changed

pyproject.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "chuk-tool-processor"
7-
version = "0.1.0"
7+
version = "0.1.1"
88
description = "Add your description here"
99
readme = "README.md"
1010
requires-python = ">=3.11"
@@ -32,7 +32,4 @@ asyncio_mode = "strict"
3232
dev = [
3333
"pytest-asyncio>=0.26.0",
3434
"pytest>=8.3.5",
35-
"langchain>=0.3.24",
36-
"langchain-community>=0.3.22",
37-
"langchain-openai>=0.3.14",
3835
]

src/chuk_tool_processor/plugins/exporters/langchain/__init__.py

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,99 @@
1-
# chuk_tool_processor/registry/provider.py
21
"""
3-
Global access to *the* tool-registry instance.
2+
Global access to *the* tool registry instance.
3+
4+
There are two public faces:
5+
6+
1. **Module helpers**
7+
• `get_registry()` lazily instantiates a default `InMemoryToolRegistry`
8+
and memoises it in the module-level variable ``_REGISTRY``.
9+
• `set_registry()` lets callers replace or reset that singleton.
10+
11+
2. **`ToolRegistryProvider` shim**
12+
Earlier versions exposed a static wrapper. Tests rely on being able to
13+
monkey-patch the *module-level* factory and to clear the cached instance
14+
by setting `ToolRegistryProvider._registry = None`. We therefore keep a
15+
**separate class-level cache** (`_registry`) and call the *current*
16+
module-level `get_registry()` **only when the cache is empty**.
17+
18+
The contract verified by the test-suite is:
19+
20+
* The module-level factory is invoked **exactly once** per fresh cache.
21+
* `ToolRegistryProvider.set_registry(obj)` overrides subsequent retrievals.
22+
* `ToolRegistryProvider.set_registry(None)` resets the cache so the next
23+
`get_registry()` call invokes (and honours any monkey-patched) factory.
424
"""
525
from __future__ import annotations
626

727
from typing import Optional
828

929
from .interface import ToolRegistryInterface
10-
from .providers.memory import InMemoryToolRegistry # default impl
30+
from .providers.memory import InMemoryToolRegistry
1131

12-
# ───────────────────────────────────────────────────────────────────────────
32+
# --------------------------------------------------------------------------- #
33+
# Module-level singleton used by the helper functions
34+
# --------------------------------------------------------------------------- #
1335
_REGISTRY: Optional[ToolRegistryInterface] = None
14-
# ───────────────────────────────────────────────────────────────────────────
36+
# --------------------------------------------------------------------------- #
37+
38+
39+
def _default_registry() -> ToolRegistryInterface:
40+
"""Create the default in-memory registry."""
41+
return InMemoryToolRegistry()
1542

1643

1744
def get_registry() -> ToolRegistryInterface:
18-
"""Return the single, process-wide registry instance."""
45+
"""
46+
Return the process-wide registry, creating it on first use.
47+
48+
This function *may* be monkey-patched in tests; call it via
49+
``globals()["get_registry"]()`` if you need the latest binding.
50+
"""
1951
global _REGISTRY
2052
if _REGISTRY is None:
21-
_REGISTRY = InMemoryToolRegistry()
53+
_REGISTRY = _default_registry()
2254
return _REGISTRY
2355

2456

25-
def set_registry(registry: ToolRegistryInterface) -> None:
26-
"""Swap in another implementation (tests, multi-process, …)."""
57+
def set_registry(registry: ToolRegistryInterface | None) -> None:
58+
"""
59+
Replace or clear the global registry.
60+
61+
Passing ``None`` resets the singleton so that the next `get_registry()`
62+
call recreates it (useful in tests).
63+
"""
2764
global _REGISTRY
2865
_REGISTRY = registry
2966

3067

31-
# ------------------------------------------------------------------------- #
32-
# 🔌 backward-compat shim – lets old `from … import ToolRegistryProvider`
33-
# statements keep working without changes.
34-
# ------------------------------------------------------------------------- #
35-
class ToolRegistryProvider: # noqa: D401
36-
"""Compatibility wrapper around the new helpers."""
68+
# --------------------------------------------------------------------------- #
69+
# Back-compat shim used by legacy import paths and the test-suite
70+
# --------------------------------------------------------------------------- #
71+
class ToolRegistryProvider: # noqa: D401
72+
"""Legacy static wrapper retaining historical semantics."""
3773

74+
# The test-suite directly mutates this attribute, so we keep it.
75+
_registry: Optional[ToolRegistryInterface] = None
76+
77+
# ------------------------ public API ------------------------ #
3878
@staticmethod
39-
def get_registry() -> ToolRegistryInterface: # same signature
40-
return get_registry()
79+
def get_registry() -> ToolRegistryInterface:
80+
"""
81+
Return the cached instance or, if absent, call the *current*
82+
module-level `get_registry()` exactly once to populate it.
83+
"""
84+
if ToolRegistryProvider._registry is None:
85+
# Honour any runtime monkey-patching of the factory.
86+
ToolRegistryProvider._registry = globals()["get_registry"]()
87+
return ToolRegistryProvider._registry
4188

4289
@staticmethod
43-
def set_registry(registry: ToolRegistryInterface) -> None:
44-
set_registry(registry)
90+
def set_registry(registry: ToolRegistryInterface | None) -> None:
91+
"""
92+
Override the cached registry.
93+
94+
* If ``registry`` is an object, all subsequent `get_registry()`
95+
calls return it without touching the factory.
96+
* If ``registry`` is ``None``, the cache is cleared so the next
97+
`get_registry()` call invokes the (possibly patched) factory.
98+
"""
99+
ToolRegistryProvider._registry = registry

0 commit comments

Comments
 (0)