Clean up nested duplicate: v2/v2/kintsugi_vendor/kintsugi/skills/registry.py
Browse files
v2/v2/kintsugi_vendor/kintsugi/skills/registry.py
DELETED
|
@@ -1,303 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Skill chip registry for discovery and routing.
|
| 3 |
-
|
| 4 |
-
This module provides the SkillRegistry class for managing skill chip
|
| 5 |
-
registration, discovery, and lookup. The registry serves as the central
|
| 6 |
-
catalog of available skill chips in the Kintsugi CMA system.
|
| 7 |
-
|
| 8 |
-
Usage:
|
| 9 |
-
from kintsugi.skills.registry import get_registry, register_chip
|
| 10 |
-
from kintsugi.skills.base import BaseSkillChip
|
| 11 |
-
|
| 12 |
-
# Get the global registry
|
| 13 |
-
registry = get_registry()
|
| 14 |
-
|
| 15 |
-
# Register a chip
|
| 16 |
-
register_chip(my_skill_chip)
|
| 17 |
-
|
| 18 |
-
# Look up chips
|
| 19 |
-
chip = registry.get("grant_search")
|
| 20 |
-
fundraising_chips = registry.get_by_domain(SkillDomain.FUNDRAISING)
|
| 21 |
-
"""
|
| 22 |
-
|
| 23 |
-
from __future__ import annotations
|
| 24 |
-
|
| 25 |
-
from typing import TYPE_CHECKING
|
| 26 |
-
|
| 27 |
-
from .base import BaseSkillChip, SkillDomain
|
| 28 |
-
|
| 29 |
-
if TYPE_CHECKING:
|
| 30 |
-
pass
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
class SkillRegistry:
|
| 34 |
-
"""Registry for skill chip discovery and routing.
|
| 35 |
-
|
| 36 |
-
The registry maintains a catalog of all registered skill chips,
|
| 37 |
-
enabling:
|
| 38 |
-
- Name-based lookup for direct chip access
|
| 39 |
-
- Domain-based lookup for routing and discovery
|
| 40 |
-
- Metadata listing for UI and API responses
|
| 41 |
-
|
| 42 |
-
Chips are stored by name with domain indexing for efficient
|
| 43 |
-
lookup in both dimensions.
|
| 44 |
-
|
| 45 |
-
Attributes:
|
| 46 |
-
_chips: Internal mapping of chip names to chip instances
|
| 47 |
-
_by_domain: Index mapping domains to lists of chip names
|
| 48 |
-
|
| 49 |
-
Example:
|
| 50 |
-
registry = SkillRegistry()
|
| 51 |
-
|
| 52 |
-
# Register chips
|
| 53 |
-
registry.register(grant_search_chip)
|
| 54 |
-
registry.register(donor_management_chip)
|
| 55 |
-
|
| 56 |
-
# Look up by name
|
| 57 |
-
chip = registry.get("grant_search")
|
| 58 |
-
|
| 59 |
-
# Look up by domain
|
| 60 |
-
fundraising_chips = registry.get_by_domain(SkillDomain.FUNDRAISING)
|
| 61 |
-
|
| 62 |
-
# Check registration
|
| 63 |
-
if "grant_search" in registry:
|
| 64 |
-
print("Chip is registered")
|
| 65 |
-
"""
|
| 66 |
-
|
| 67 |
-
def __init__(self) -> None:
|
| 68 |
-
"""Initialize an empty registry."""
|
| 69 |
-
self._chips: dict[str, BaseSkillChip] = {}
|
| 70 |
-
self._by_domain: dict[SkillDomain, list[str]] = {}
|
| 71 |
-
|
| 72 |
-
def register(self, chip: BaseSkillChip) -> None:
|
| 73 |
-
"""Register a skill chip.
|
| 74 |
-
|
| 75 |
-
Adds the chip to the registry and indexes it by domain.
|
| 76 |
-
Raises an error if a chip with the same name is already
|
| 77 |
-
registered to prevent accidental overwrites.
|
| 78 |
-
|
| 79 |
-
Args:
|
| 80 |
-
chip: The skill chip instance to register
|
| 81 |
-
|
| 82 |
-
Raises:
|
| 83 |
-
ValueError: If a chip with the same name is already registered
|
| 84 |
-
|
| 85 |
-
Example:
|
| 86 |
-
registry = SkillRegistry()
|
| 87 |
-
registry.register(GrantSearchChip())
|
| 88 |
-
"""
|
| 89 |
-
if chip.name in self._chips:
|
| 90 |
-
raise ValueError(f"Chip '{chip.name}' already registered")
|
| 91 |
-
|
| 92 |
-
self._chips[chip.name] = chip
|
| 93 |
-
|
| 94 |
-
# Index by domain
|
| 95 |
-
if chip.domain not in self._by_domain:
|
| 96 |
-
self._by_domain[chip.domain] = []
|
| 97 |
-
self._by_domain[chip.domain].append(chip.name)
|
| 98 |
-
|
| 99 |
-
def unregister(self, name: str) -> bool:
|
| 100 |
-
"""Unregister a skill chip.
|
| 101 |
-
|
| 102 |
-
Removes the chip from the registry and domain index.
|
| 103 |
-
Returns True if the chip was found and removed, False otherwise.
|
| 104 |
-
|
| 105 |
-
Args:
|
| 106 |
-
name: The name of the chip to unregister
|
| 107 |
-
|
| 108 |
-
Returns:
|
| 109 |
-
True if the chip was found and removed, False if not found
|
| 110 |
-
|
| 111 |
-
Example:
|
| 112 |
-
if registry.unregister("old_chip"):
|
| 113 |
-
print("Chip removed")
|
| 114 |
-
else:
|
| 115 |
-
print("Chip not found")
|
| 116 |
-
"""
|
| 117 |
-
chip = self._chips.pop(name, None)
|
| 118 |
-
if chip is not None:
|
| 119 |
-
# Remove from domain index
|
| 120 |
-
domain_chips = self._by_domain.get(chip.domain, [])
|
| 121 |
-
if name in domain_chips:
|
| 122 |
-
domain_chips.remove(name)
|
| 123 |
-
return True
|
| 124 |
-
return False
|
| 125 |
-
|
| 126 |
-
def get(self, name: str) -> BaseSkillChip | None:
|
| 127 |
-
"""Get a chip by name.
|
| 128 |
-
|
| 129 |
-
Args:
|
| 130 |
-
name: The unique name of the chip to retrieve
|
| 131 |
-
|
| 132 |
-
Returns:
|
| 133 |
-
The chip instance if found, None otherwise
|
| 134 |
-
|
| 135 |
-
Example:
|
| 136 |
-
chip = registry.get("grant_search")
|
| 137 |
-
if chip:
|
| 138 |
-
response = await chip.handle(request, context)
|
| 139 |
-
"""
|
| 140 |
-
return self._chips.get(name)
|
| 141 |
-
|
| 142 |
-
def get_by_domain(self, domain: SkillDomain) -> list[BaseSkillChip]:
|
| 143 |
-
"""Get all chips in a domain.
|
| 144 |
-
|
| 145 |
-
Returns a list of all chip instances registered in the
|
| 146 |
-
specified domain. Useful for domain-specific routing or
|
| 147 |
-
displaying available capabilities.
|
| 148 |
-
|
| 149 |
-
Args:
|
| 150 |
-
domain: The SkillDomain to filter by
|
| 151 |
-
|
| 152 |
-
Returns:
|
| 153 |
-
List of chip instances in the domain (may be empty)
|
| 154 |
-
|
| 155 |
-
Example:
|
| 156 |
-
fundraising_chips = registry.get_by_domain(SkillDomain.FUNDRAISING)
|
| 157 |
-
for chip in fundraising_chips:
|
| 158 |
-
print(f"- {chip.name}: {chip.description}")
|
| 159 |
-
"""
|
| 160 |
-
names = self._by_domain.get(domain, [])
|
| 161 |
-
return [self._chips[name] for name in names if name in self._chips]
|
| 162 |
-
|
| 163 |
-
def list_all(self) -> list[dict]:
|
| 164 |
-
"""List all registered chips with metadata.
|
| 165 |
-
|
| 166 |
-
Returns metadata for all registered chips, useful for
|
| 167 |
-
API responses and UI display.
|
| 168 |
-
|
| 169 |
-
Returns:
|
| 170 |
-
List of dictionaries containing chip metadata
|
| 171 |
-
(see BaseSkillChip.get_info() for structure)
|
| 172 |
-
|
| 173 |
-
Example:
|
| 174 |
-
for chip_info in registry.list_all():
|
| 175 |
-
print(f"{chip_info['name']}: {chip_info['description']}")
|
| 176 |
-
"""
|
| 177 |
-
return [chip.get_info() for chip in self._chips.values()]
|
| 178 |
-
|
| 179 |
-
def list_names(self) -> list[str]:
|
| 180 |
-
"""List all registered chip names.
|
| 181 |
-
|
| 182 |
-
Returns:
|
| 183 |
-
List of registered chip names
|
| 184 |
-
|
| 185 |
-
Example:
|
| 186 |
-
names = registry.list_names()
|
| 187 |
-
print(f"Registered chips: {', '.join(names)}")
|
| 188 |
-
"""
|
| 189 |
-
return list(self._chips.keys())
|
| 190 |
-
|
| 191 |
-
def list_domains(self) -> list[SkillDomain]:
|
| 192 |
-
"""List all domains that have registered chips.
|
| 193 |
-
|
| 194 |
-
Returns:
|
| 195 |
-
List of SkillDomain values that have at least one chip
|
| 196 |
-
|
| 197 |
-
Example:
|
| 198 |
-
domains = registry.list_domains()
|
| 199 |
-
for domain in domains:
|
| 200 |
-
chips = registry.get_by_domain(domain)
|
| 201 |
-
print(f"{domain.value}: {len(chips)} chips")
|
| 202 |
-
"""
|
| 203 |
-
return [domain for domain, chips in self._by_domain.items() if chips]
|
| 204 |
-
|
| 205 |
-
def clear(self) -> None:
|
| 206 |
-
"""Remove all registered chips.
|
| 207 |
-
|
| 208 |
-
Clears both the chip registry and domain index.
|
| 209 |
-
Useful for testing or reinitializing the registry.
|
| 210 |
-
"""
|
| 211 |
-
self._chips.clear()
|
| 212 |
-
self._by_domain.clear()
|
| 213 |
-
|
| 214 |
-
def __len__(self) -> int:
|
| 215 |
-
"""Return the number of registered chips.
|
| 216 |
-
|
| 217 |
-
Returns:
|
| 218 |
-
Count of registered chips
|
| 219 |
-
"""
|
| 220 |
-
return len(self._chips)
|
| 221 |
-
|
| 222 |
-
def __contains__(self, name: str) -> bool:
|
| 223 |
-
"""Check if a chip is registered.
|
| 224 |
-
|
| 225 |
-
Args:
|
| 226 |
-
name: The chip name to check
|
| 227 |
-
|
| 228 |
-
Returns:
|
| 229 |
-
True if the chip is registered, False otherwise
|
| 230 |
-
|
| 231 |
-
Example:
|
| 232 |
-
if "grant_search" in registry:
|
| 233 |
-
chip = registry.get("grant_search")
|
| 234 |
-
"""
|
| 235 |
-
return name in self._chips
|
| 236 |
-
|
| 237 |
-
def __iter__(self):
|
| 238 |
-
"""Iterate over registered chips.
|
| 239 |
-
|
| 240 |
-
Yields:
|
| 241 |
-
BaseSkillChip instances in registration order
|
| 242 |
-
|
| 243 |
-
Example:
|
| 244 |
-
for chip in registry:
|
| 245 |
-
print(chip.name)
|
| 246 |
-
"""
|
| 247 |
-
return iter(self._chips.values())
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
# Global registry instance
|
| 251 |
-
_registry: SkillRegistry | None = None
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
def get_registry() -> SkillRegistry:
|
| 255 |
-
"""Get the global skill registry.
|
| 256 |
-
|
| 257 |
-
Returns the singleton global registry instance, creating it
|
| 258 |
-
if it doesn't exist. This is the primary way to access the
|
| 259 |
-
registry throughout the application.
|
| 260 |
-
|
| 261 |
-
Returns:
|
| 262 |
-
The global SkillRegistry instance
|
| 263 |
-
|
| 264 |
-
Example:
|
| 265 |
-
registry = get_registry()
|
| 266 |
-
chip = registry.get("grant_search")
|
| 267 |
-
"""
|
| 268 |
-
global _registry
|
| 269 |
-
if _registry is None:
|
| 270 |
-
_registry = SkillRegistry()
|
| 271 |
-
return _registry
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
def register_chip(chip: BaseSkillChip) -> None:
|
| 275 |
-
"""Register a chip in the global registry.
|
| 276 |
-
|
| 277 |
-
Convenience function for registering chips without needing
|
| 278 |
-
to call get_registry() first.
|
| 279 |
-
|
| 280 |
-
Args:
|
| 281 |
-
chip: The skill chip instance to register
|
| 282 |
-
|
| 283 |
-
Raises:
|
| 284 |
-
ValueError: If a chip with the same name is already registered
|
| 285 |
-
|
| 286 |
-
Example:
|
| 287 |
-
from kintsugi.skills.registry import register_chip
|
| 288 |
-
|
| 289 |
-
register_chip(GrantSearchChip())
|
| 290 |
-
"""
|
| 291 |
-
get_registry().register(chip)
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
def reset_registry() -> None:
|
| 295 |
-
"""Reset the global registry to empty state.
|
| 296 |
-
|
| 297 |
-
Primarily useful for testing to ensure clean state between tests.
|
| 298 |
-
In production, this should rarely if ever be needed.
|
| 299 |
-
"""
|
| 300 |
-
global _registry
|
| 301 |
-
if _registry is not None:
|
| 302 |
-
_registry.clear()
|
| 303 |
-
_registry = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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