django-agent-runtime 0.3.6__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.
Files changed (55) hide show
  1. django_agent_runtime/__init__.py +25 -0
  2. django_agent_runtime/admin.py +155 -0
  3. django_agent_runtime/api/__init__.py +26 -0
  4. django_agent_runtime/api/permissions.py +109 -0
  5. django_agent_runtime/api/serializers.py +114 -0
  6. django_agent_runtime/api/views.py +472 -0
  7. django_agent_runtime/apps.py +26 -0
  8. django_agent_runtime/conf.py +241 -0
  9. django_agent_runtime/examples/__init__.py +10 -0
  10. django_agent_runtime/examples/langgraph_adapter.py +164 -0
  11. django_agent_runtime/examples/langgraph_tools.py +179 -0
  12. django_agent_runtime/examples/simple_chat.py +69 -0
  13. django_agent_runtime/examples/tool_agent.py +157 -0
  14. django_agent_runtime/management/__init__.py +2 -0
  15. django_agent_runtime/management/commands/__init__.py +2 -0
  16. django_agent_runtime/management/commands/runagent.py +419 -0
  17. django_agent_runtime/migrations/0001_initial.py +117 -0
  18. django_agent_runtime/migrations/0002_persistence_models.py +129 -0
  19. django_agent_runtime/migrations/0003_persistenceconversation_active_branch_id_and_more.py +212 -0
  20. django_agent_runtime/migrations/0004_add_anonymous_session_id.py +18 -0
  21. django_agent_runtime/migrations/__init__.py +2 -0
  22. django_agent_runtime/models/__init__.py +54 -0
  23. django_agent_runtime/models/base.py +450 -0
  24. django_agent_runtime/models/concrete.py +146 -0
  25. django_agent_runtime/persistence/__init__.py +60 -0
  26. django_agent_runtime/persistence/helpers.py +148 -0
  27. django_agent_runtime/persistence/models.py +506 -0
  28. django_agent_runtime/persistence/stores.py +1191 -0
  29. django_agent_runtime/runtime/__init__.py +23 -0
  30. django_agent_runtime/runtime/events/__init__.py +65 -0
  31. django_agent_runtime/runtime/events/base.py +135 -0
  32. django_agent_runtime/runtime/events/db.py +129 -0
  33. django_agent_runtime/runtime/events/redis.py +228 -0
  34. django_agent_runtime/runtime/events/sync.py +140 -0
  35. django_agent_runtime/runtime/interfaces.py +475 -0
  36. django_agent_runtime/runtime/llm/__init__.py +91 -0
  37. django_agent_runtime/runtime/llm/anthropic.py +249 -0
  38. django_agent_runtime/runtime/llm/litellm_adapter.py +173 -0
  39. django_agent_runtime/runtime/llm/openai.py +230 -0
  40. django_agent_runtime/runtime/queue/__init__.py +75 -0
  41. django_agent_runtime/runtime/queue/base.py +158 -0
  42. django_agent_runtime/runtime/queue/postgres.py +248 -0
  43. django_agent_runtime/runtime/queue/redis_streams.py +336 -0
  44. django_agent_runtime/runtime/queue/sync.py +277 -0
  45. django_agent_runtime/runtime/registry.py +186 -0
  46. django_agent_runtime/runtime/runner.py +540 -0
  47. django_agent_runtime/runtime/tracing/__init__.py +48 -0
  48. django_agent_runtime/runtime/tracing/langfuse.py +117 -0
  49. django_agent_runtime/runtime/tracing/noop.py +36 -0
  50. django_agent_runtime/urls.py +39 -0
  51. django_agent_runtime-0.3.6.dist-info/METADATA +723 -0
  52. django_agent_runtime-0.3.6.dist-info/RECORD +55 -0
  53. django_agent_runtime-0.3.6.dist-info/WHEEL +5 -0
  54. django_agent_runtime-0.3.6.dist-info/licenses/LICENSE +22 -0
  55. django_agent_runtime-0.3.6.dist-info/top_level.txt +1 -0
@@ -0,0 +1,186 @@
1
+ """
2
+ Runtime registry for discovering and managing agent runtimes.
3
+
4
+ Supports:
5
+ - Manual registration via register_runtime()
6
+ - Settings-based discovery via RUNTIME_REGISTRY
7
+ - Entry-point based discovery for plugins
8
+ """
9
+
10
+ import logging
11
+ from typing import Callable, Optional, Type
12
+
13
+ from django_agent_runtime.runtime.interfaces import AgentRuntime
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+ # Global registry of agent runtimes
18
+ _runtimes: dict[str, AgentRuntime] = {}
19
+ _runtime_factories: dict[str, Callable[[], AgentRuntime]] = {}
20
+ _discovered = False
21
+
22
+
23
+ def register_runtime(
24
+ runtime: AgentRuntime | Type[AgentRuntime] | Callable[[], AgentRuntime],
25
+ key: Optional[str] = None,
26
+ ) -> None:
27
+ """
28
+ Register an agent runtime.
29
+
30
+ Args:
31
+ runtime: Runtime instance, class, or factory function
32
+ key: Optional key override (uses runtime.key if not provided)
33
+
34
+ Examples:
35
+ # Register an instance
36
+ register_runtime(MyRuntime())
37
+
38
+ # Register a class (will be instantiated)
39
+ register_runtime(MyRuntime)
40
+
41
+ # Register with custom key
42
+ register_runtime(MyRuntime(), key="custom-key")
43
+
44
+ # Register a factory
45
+ register_runtime(lambda: MyRuntime(config=get_config()))
46
+ """
47
+ if isinstance(runtime, AgentRuntime):
48
+ # Instance provided
49
+ runtime_key = key or runtime.key
50
+ _runtimes[runtime_key] = runtime
51
+ logger.info(f"Registered agent runtime: {runtime_key}")
52
+
53
+ elif isinstance(runtime, type) and issubclass(runtime, AgentRuntime):
54
+ # Class provided - instantiate it
55
+ instance = runtime()
56
+ runtime_key = key or instance.key
57
+ _runtimes[runtime_key] = instance
58
+ logger.info(f"Registered agent runtime: {runtime_key}")
59
+
60
+ elif callable(runtime):
61
+ # Factory function provided
62
+ if not key:
63
+ raise ValueError("key is required when registering a factory function")
64
+ _runtime_factories[key] = runtime
65
+ logger.info(f"Registered agent runtime factory: {key}")
66
+
67
+ else:
68
+ raise TypeError(
69
+ f"runtime must be AgentRuntime instance, class, or callable, got {type(runtime)}"
70
+ )
71
+
72
+
73
+ def get_runtime(key: str) -> AgentRuntime:
74
+ """
75
+ Get a runtime by key.
76
+
77
+ Args:
78
+ key: Runtime key
79
+
80
+ Returns:
81
+ AgentRuntime instance
82
+
83
+ Raises:
84
+ KeyError: If runtime not found
85
+ """
86
+ # Check instances first
87
+ if key in _runtimes:
88
+ return _runtimes[key]
89
+
90
+ # Check factories
91
+ if key in _runtime_factories:
92
+ instance = _runtime_factories[key]()
93
+ _runtimes[key] = instance
94
+ return instance
95
+
96
+ raise KeyError(f"Agent runtime not found: {key}. Available: {list_runtimes()}")
97
+
98
+
99
+ def list_runtimes() -> list[str]:
100
+ """List all registered runtime keys."""
101
+ return list(set(_runtimes.keys()) | set(_runtime_factories.keys()))
102
+
103
+
104
+ def unregister_runtime(key: str) -> bool:
105
+ """
106
+ Unregister a runtime.
107
+
108
+ Args:
109
+ key: Runtime key
110
+
111
+ Returns:
112
+ True if removed, False if not found
113
+ """
114
+ removed = False
115
+ if key in _runtimes:
116
+ del _runtimes[key]
117
+ removed = True
118
+ if key in _runtime_factories:
119
+ del _runtime_factories[key]
120
+ removed = True
121
+ return removed
122
+
123
+
124
+ def clear_registry() -> None:
125
+ """Clear all registered runtimes. Useful for testing."""
126
+ global _discovered
127
+ _runtimes.clear()
128
+ _runtime_factories.clear()
129
+ _discovered = False
130
+
131
+
132
+ def autodiscover_runtimes() -> None:
133
+ """
134
+ Auto-discover runtimes from settings and entry points.
135
+
136
+ Called automatically when Django starts (in apps.py ready()).
137
+ """
138
+ global _discovered
139
+ if _discovered:
140
+ return
141
+
142
+ _discovered = True
143
+
144
+ # Discover from settings
145
+ _discover_from_settings()
146
+
147
+ # Discover from entry points
148
+ _discover_from_entry_points()
149
+
150
+
151
+ def _discover_from_settings() -> None:
152
+ """Discover runtimes from DJANGO_AGENT_RUNTIME['RUNTIME_REGISTRY']."""
153
+ from django_agent_runtime.conf import runtime_settings
154
+
155
+ settings = runtime_settings()
156
+
157
+ for dotted_path in settings.RUNTIME_REGISTRY:
158
+ try:
159
+ from django.utils.module_loading import import_string
160
+
161
+ register_func = import_string(dotted_path)
162
+ register_func()
163
+ logger.info(f"Loaded runtime registry from: {dotted_path}")
164
+ except Exception as e:
165
+ logger.error(f"Failed to load runtime registry {dotted_path}: {e}")
166
+
167
+
168
+ def _discover_from_entry_points() -> None:
169
+ """Discover runtimes from entry points."""
170
+ try:
171
+ from importlib.metadata import entry_points
172
+ except ImportError:
173
+ from importlib_metadata import entry_points
174
+
175
+ try:
176
+ eps = entry_points(group="django_agent_runtime.runtimes")
177
+ for ep in eps:
178
+ try:
179
+ register_func = ep.load()
180
+ register_func()
181
+ logger.info(f"Loaded runtime from entry point: {ep.name}")
182
+ except Exception as e:
183
+ logger.error(f"Failed to load entry point {ep.name}: {e}")
184
+ except Exception as e:
185
+ logger.debug(f"No entry points found: {e}")
186
+