agentscope-runtime 0.1.6__py3-none-any.whl → 0.2.0__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.
- agentscope_runtime/common/container_clients/__init__.py +0 -0
- agentscope_runtime/{sandbox/manager → common}/container_clients/kubernetes_client.py +546 -6
- agentscope_runtime/engine/__init__.py +12 -0
- agentscope_runtime/engine/agents/agentscope_agent.py +130 -10
- agentscope_runtime/engine/agents/agno_agent.py +8 -10
- agentscope_runtime/engine/agents/langgraph_agent.py +52 -9
- agentscope_runtime/engine/app/__init__.py +6 -0
- agentscope_runtime/engine/app/agent_app.py +239 -0
- agentscope_runtime/engine/app/base_app.py +181 -0
- agentscope_runtime/engine/app/celery_mixin.py +92 -0
- agentscope_runtime/engine/deployers/__init__.py +13 -0
- agentscope_runtime/engine/deployers/adapter/responses/__init__.py +0 -0
- agentscope_runtime/engine/deployers/adapter/responses/response_api_adapter_utils.py +2890 -0
- agentscope_runtime/engine/deployers/adapter/responses/response_api_agent_adapter.py +51 -0
- agentscope_runtime/engine/deployers/adapter/responses/response_api_protocol_adapter.py +314 -0
- agentscope_runtime/engine/deployers/base.py +1 -0
- agentscope_runtime/engine/deployers/cli_fc_deploy.py +203 -0
- agentscope_runtime/engine/deployers/kubernetes_deployer.py +272 -0
- agentscope_runtime/engine/deployers/local_deployer.py +414 -501
- agentscope_runtime/engine/deployers/modelstudio_deployer.py +838 -0
- agentscope_runtime/engine/deployers/utils/__init__.py +0 -0
- agentscope_runtime/engine/deployers/utils/deployment_modes.py +14 -0
- agentscope_runtime/engine/deployers/utils/docker_image_utils/__init__.py +8 -0
- agentscope_runtime/engine/deployers/utils/docker_image_utils/docker_image_builder.py +429 -0
- agentscope_runtime/engine/deployers/utils/docker_image_utils/dockerfile_generator.py +240 -0
- agentscope_runtime/engine/deployers/utils/docker_image_utils/runner_image_factory.py +306 -0
- agentscope_runtime/engine/deployers/utils/package_project_utils.py +1163 -0
- agentscope_runtime/engine/deployers/utils/service_utils/__init__.py +9 -0
- agentscope_runtime/engine/deployers/utils/service_utils/fastapi_factory.py +1064 -0
- agentscope_runtime/engine/deployers/utils/service_utils/fastapi_templates.py +157 -0
- agentscope_runtime/engine/deployers/utils/service_utils/process_manager.py +268 -0
- agentscope_runtime/engine/deployers/utils/service_utils/service_config.py +75 -0
- agentscope_runtime/engine/deployers/utils/service_utils/service_factory.py +220 -0
- agentscope_runtime/engine/deployers/utils/service_utils/standalone_main.py.j2 +211 -0
- agentscope_runtime/engine/deployers/utils/wheel_packager.py +389 -0
- agentscope_runtime/engine/helpers/agent_api_builder.py +651 -0
- agentscope_runtime/engine/runner.py +76 -35
- agentscope_runtime/engine/schemas/agent_schemas.py +112 -2
- agentscope_runtime/engine/schemas/embedding.py +37 -0
- agentscope_runtime/engine/schemas/modelstudio_llm.py +310 -0
- agentscope_runtime/engine/schemas/oai_llm.py +538 -0
- agentscope_runtime/engine/schemas/realtime.py +254 -0
- agentscope_runtime/engine/services/tablestore_memory_service.py +4 -1
- agentscope_runtime/engine/tracing/__init__.py +9 -3
- agentscope_runtime/engine/tracing/asyncio_util.py +24 -0
- agentscope_runtime/engine/tracing/base.py +66 -34
- agentscope_runtime/engine/tracing/local_logging_handler.py +45 -31
- agentscope_runtime/engine/tracing/message_util.py +528 -0
- agentscope_runtime/engine/tracing/tracing_metric.py +20 -8
- agentscope_runtime/engine/tracing/tracing_util.py +130 -0
- agentscope_runtime/engine/tracing/wrapper.py +794 -169
- agentscope_runtime/sandbox/box/base/base_sandbox.py +2 -1
- agentscope_runtime/sandbox/box/browser/browser_sandbox.py +2 -1
- agentscope_runtime/sandbox/box/dummy/dummy_sandbox.py +2 -1
- agentscope_runtime/sandbox/box/filesystem/filesystem_sandbox.py +2 -1
- agentscope_runtime/sandbox/box/gui/gui_sandbox.py +2 -1
- agentscope_runtime/sandbox/box/training_box/training_box.py +0 -42
- agentscope_runtime/sandbox/client/http_client.py +52 -18
- agentscope_runtime/sandbox/constant.py +3 -0
- agentscope_runtime/sandbox/custom/custom_sandbox.py +2 -1
- agentscope_runtime/sandbox/custom/example.py +2 -1
- agentscope_runtime/sandbox/enums.py +0 -1
- agentscope_runtime/sandbox/manager/sandbox_manager.py +29 -22
- agentscope_runtime/sandbox/model/container.py +6 -0
- agentscope_runtime/sandbox/registry.py +1 -1
- agentscope_runtime/sandbox/tools/tool.py +4 -0
- agentscope_runtime/version.py +1 -1
- {agentscope_runtime-0.1.6.dist-info → agentscope_runtime-0.2.0.dist-info}/METADATA +103 -59
- {agentscope_runtime-0.1.6.dist-info → agentscope_runtime-0.2.0.dist-info}/RECORD +87 -52
- {agentscope_runtime-0.1.6.dist-info → agentscope_runtime-0.2.0.dist-info}/entry_points.txt +1 -0
- /agentscope_runtime/{sandbox/manager/container_clients → common}/__init__.py +0 -0
- /agentscope_runtime/{sandbox/manager → common}/collections/__init__.py +0 -0
- /agentscope_runtime/{sandbox/manager → common}/collections/base_mapping.py +0 -0
- /agentscope_runtime/{sandbox/manager → common}/collections/base_queue.py +0 -0
- /agentscope_runtime/{sandbox/manager → common}/collections/base_set.py +0 -0
- /agentscope_runtime/{sandbox/manager → common}/collections/in_memory_mapping.py +0 -0
- /agentscope_runtime/{sandbox/manager → common}/collections/in_memory_queue.py +0 -0
- /agentscope_runtime/{sandbox/manager → common}/collections/in_memory_set.py +0 -0
- /agentscope_runtime/{sandbox/manager → common}/collections/redis_mapping.py +0 -0
- /agentscope_runtime/{sandbox/manager → common}/collections/redis_queue.py +0 -0
- /agentscope_runtime/{sandbox/manager → common}/collections/redis_set.py +0 -0
- /agentscope_runtime/{sandbox/manager → common}/container_clients/agentrun_client.py +0 -0
- /agentscope_runtime/{sandbox/manager → common}/container_clients/base_client.py +0 -0
- /agentscope_runtime/{sandbox/manager → common}/container_clients/docker_client.py +0 -0
- {agentscope_runtime-0.1.6.dist-info → agentscope_runtime-0.2.0.dist-info}/WHEEL +0 -0
- {agentscope_runtime-0.1.6.dist-info → agentscope_runtime-0.2.0.dist-info}/licenses/LICENSE +0 -0
- {agentscope_runtime-0.1.6.dist-info → agentscope_runtime-0.2.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import inspect
|
|
3
|
+
import logging
|
|
4
|
+
import threading
|
|
5
|
+
from typing import Callable, Optional
|
|
6
|
+
|
|
7
|
+
import uvicorn
|
|
8
|
+
from fastapi import FastAPI, Request
|
|
9
|
+
from fastapi.responses import StreamingResponse
|
|
10
|
+
|
|
11
|
+
from .celery_mixin import CeleryMixin
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class BaseApp(FastAPI, CeleryMixin):
|
|
17
|
+
"""
|
|
18
|
+
BaseApp extends FastAPI and integrates with Celery
|
|
19
|
+
for asynchronous background task execution.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
broker_url: Optional[str] = None,
|
|
25
|
+
backend_url: Optional[str] = None,
|
|
26
|
+
**kwargs,
|
|
27
|
+
):
|
|
28
|
+
# Initialize CeleryMixin
|
|
29
|
+
CeleryMixin.__init__(self, broker_url, backend_url)
|
|
30
|
+
|
|
31
|
+
self.server = None
|
|
32
|
+
|
|
33
|
+
# Initialize FastAPI
|
|
34
|
+
FastAPI.__init__(self, **kwargs)
|
|
35
|
+
|
|
36
|
+
def task(self, path: str, queue: str = "celery"):
|
|
37
|
+
"""
|
|
38
|
+
Register an asynchronous task endpoint.
|
|
39
|
+
POST <path> -> Create a task and return task ID
|
|
40
|
+
GET <path>/{task_id} -> Check the task status and result
|
|
41
|
+
Combines Celery and FastAPI routing functionality.
|
|
42
|
+
"""
|
|
43
|
+
if self.celery_app is None:
|
|
44
|
+
raise RuntimeError(
|
|
45
|
+
f"[AgentApp] Cannot register task endpoint '{path}'.\n"
|
|
46
|
+
f"Reason: The @task decorator requires a background task "
|
|
47
|
+
f"queue to run asynchronous jobs.\n\n"
|
|
48
|
+
"If you want to use async task queue, you must initialize "
|
|
49
|
+
"AgentApp with broker_url and backend_url, e.g.: \n\n"
|
|
50
|
+
" app = AgentApp(\n"
|
|
51
|
+
" broker_url='redis://localhost:6379/0',\n"
|
|
52
|
+
" backend_url='redis://localhost:6379/0'\n"
|
|
53
|
+
" )\n",
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
def decorator(func: Callable):
|
|
57
|
+
# Register Celery task using CeleryMixin
|
|
58
|
+
celery_task = self.register_celery_task(func, queue=queue)
|
|
59
|
+
|
|
60
|
+
# Add FastAPI HTTP routes
|
|
61
|
+
@self.post(path)
|
|
62
|
+
async def create_task(request: Request):
|
|
63
|
+
if len(inspect.signature(func).parameters) > 0:
|
|
64
|
+
body = await request.json()
|
|
65
|
+
task = celery_task.delay(body)
|
|
66
|
+
else:
|
|
67
|
+
task = celery_task.delay()
|
|
68
|
+
return {"task_id": task.id}
|
|
69
|
+
|
|
70
|
+
@self.get(path + "/{task_id}")
|
|
71
|
+
async def get_task(task_id: str):
|
|
72
|
+
return self.get_task_status(task_id)
|
|
73
|
+
|
|
74
|
+
return func
|
|
75
|
+
|
|
76
|
+
return decorator
|
|
77
|
+
|
|
78
|
+
def endpoint(self, path: str):
|
|
79
|
+
"""
|
|
80
|
+
Unified POST endpoint decorator.
|
|
81
|
+
Pure FastAPI functionality, independent of Celery.
|
|
82
|
+
Supports:
|
|
83
|
+
- Sync functions
|
|
84
|
+
- Async functions (coroutines)
|
|
85
|
+
- Sync/async generator functions (streaming responses)
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
def decorator(func: Callable):
|
|
89
|
+
is_async_gen = inspect.isasyncgenfunction(func)
|
|
90
|
+
is_sync_gen = inspect.isgeneratorfunction(func)
|
|
91
|
+
|
|
92
|
+
if is_async_gen or is_sync_gen:
|
|
93
|
+
# Handle streaming responses
|
|
94
|
+
async def _stream_generator(request: Request):
|
|
95
|
+
if is_async_gen:
|
|
96
|
+
async for chunk in func(request):
|
|
97
|
+
yield chunk
|
|
98
|
+
else:
|
|
99
|
+
for chunk in func(request):
|
|
100
|
+
yield chunk
|
|
101
|
+
|
|
102
|
+
@self.post(path)
|
|
103
|
+
async def _wrapped(request: Request):
|
|
104
|
+
return StreamingResponse(
|
|
105
|
+
_stream_generator(request),
|
|
106
|
+
media_type="text/plain",
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
else:
|
|
110
|
+
# Handle regular responses
|
|
111
|
+
@self.post(path)
|
|
112
|
+
async def _wrapped(request: Request):
|
|
113
|
+
if inspect.iscoroutinefunction(func):
|
|
114
|
+
return await func(request)
|
|
115
|
+
else:
|
|
116
|
+
return func(request)
|
|
117
|
+
|
|
118
|
+
return func
|
|
119
|
+
|
|
120
|
+
return decorator
|
|
121
|
+
|
|
122
|
+
def run(
|
|
123
|
+
self,
|
|
124
|
+
host="0.0.0.0",
|
|
125
|
+
port=8090,
|
|
126
|
+
embed_task_processor=False,
|
|
127
|
+
**kwargs,
|
|
128
|
+
):
|
|
129
|
+
"""
|
|
130
|
+
Run FastAPI with uvicorn.
|
|
131
|
+
"""
|
|
132
|
+
if embed_task_processor:
|
|
133
|
+
if self.celery_app is None:
|
|
134
|
+
logger.warning(
|
|
135
|
+
"[AgentApp] Celery is not configured. "
|
|
136
|
+
"Cannot run embedded worker.",
|
|
137
|
+
)
|
|
138
|
+
else:
|
|
139
|
+
logger.warning(
|
|
140
|
+
"[AgentApp] embed_task_processor=True: Running "
|
|
141
|
+
"task_processor in embedded thread mode. This is "
|
|
142
|
+
"intended for development/debug purposes only. In "
|
|
143
|
+
"production, run Celery worker in a separate process!",
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
queues = self._registered_queues or {"celery"}
|
|
147
|
+
queue_list = ",".join(sorted(queues))
|
|
148
|
+
|
|
149
|
+
def start_celery_worker():
|
|
150
|
+
logger.info(
|
|
151
|
+
f"[AgentApp] Embedded worker listening "
|
|
152
|
+
f"queues: {queue_list}",
|
|
153
|
+
)
|
|
154
|
+
self.celery_app.worker_main(
|
|
155
|
+
[
|
|
156
|
+
"worker",
|
|
157
|
+
"--loglevel=INFO",
|
|
158
|
+
"-Q",
|
|
159
|
+
queue_list,
|
|
160
|
+
],
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
threading.Thread(
|
|
164
|
+
target=start_celery_worker,
|
|
165
|
+
daemon=True,
|
|
166
|
+
).start()
|
|
167
|
+
logger.info(
|
|
168
|
+
"[AgentApp] Embedded task processor started in background "
|
|
169
|
+
"thread (DEV mode).",
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
# TODO: Add CLI to main entrypoint to control run/deploy
|
|
173
|
+
|
|
174
|
+
config = uvicorn.Config(
|
|
175
|
+
app=self,
|
|
176
|
+
host=host,
|
|
177
|
+
port=port,
|
|
178
|
+
**kwargs,
|
|
179
|
+
)
|
|
180
|
+
self.server = uvicorn.Server(config)
|
|
181
|
+
self.server.run()
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import inspect
|
|
3
|
+
import asyncio
|
|
4
|
+
import logging
|
|
5
|
+
from typing import Callable, Optional, List
|
|
6
|
+
from celery import Celery
|
|
7
|
+
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class CeleryMixin:
|
|
12
|
+
"""
|
|
13
|
+
Celery task processing mixin that provides core Celery functionality.
|
|
14
|
+
Can be reused by BaseApp and FastAPIAppFactory.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(
|
|
18
|
+
self,
|
|
19
|
+
broker_url: Optional[str] = None,
|
|
20
|
+
backend_url: Optional[str] = None,
|
|
21
|
+
):
|
|
22
|
+
if broker_url and backend_url:
|
|
23
|
+
self.celery_app = Celery(
|
|
24
|
+
"agentscope_runtime",
|
|
25
|
+
broker=broker_url,
|
|
26
|
+
backend=backend_url,
|
|
27
|
+
)
|
|
28
|
+
else:
|
|
29
|
+
self.celery_app = None
|
|
30
|
+
|
|
31
|
+
self._registered_queues: set[str] = set()
|
|
32
|
+
|
|
33
|
+
def get_registered_queues(self) -> set[str]:
|
|
34
|
+
return self._registered_queues
|
|
35
|
+
|
|
36
|
+
def register_celery_task(self, func: Callable, queue: str = "celery"):
|
|
37
|
+
"""Register a Celery task for the given function."""
|
|
38
|
+
if self.celery_app is None:
|
|
39
|
+
raise RuntimeError("Celery is not configured.")
|
|
40
|
+
|
|
41
|
+
self._registered_queues.add(queue)
|
|
42
|
+
|
|
43
|
+
@self.celery_app.task(queue=queue)
|
|
44
|
+
def wrapper(*args, **kwargs):
|
|
45
|
+
if inspect.iscoroutinefunction(func):
|
|
46
|
+
return asyncio.run(func(*args, **kwargs))
|
|
47
|
+
else:
|
|
48
|
+
return func(*args, **kwargs)
|
|
49
|
+
|
|
50
|
+
return wrapper
|
|
51
|
+
|
|
52
|
+
def run_task_processor(
|
|
53
|
+
self,
|
|
54
|
+
loglevel: str = "INFO",
|
|
55
|
+
concurrency: Optional[int] = None,
|
|
56
|
+
queues: Optional[List[str]] = None,
|
|
57
|
+
):
|
|
58
|
+
"""Run Celery worker in this process."""
|
|
59
|
+
if self.celery_app is None:
|
|
60
|
+
raise RuntimeError("Celery is not configured.")
|
|
61
|
+
|
|
62
|
+
cmd = ["worker", f"--loglevel={loglevel}"]
|
|
63
|
+
if concurrency:
|
|
64
|
+
cmd.append(f"--concurrency={concurrency}")
|
|
65
|
+
if queues:
|
|
66
|
+
cmd.append(f"-Q {','.join(queues)}")
|
|
67
|
+
|
|
68
|
+
self.celery_app.worker_main(cmd)
|
|
69
|
+
|
|
70
|
+
def get_task_status(self, task_id: str):
|
|
71
|
+
"""Get task status from Celery result backend."""
|
|
72
|
+
if self.celery_app is None:
|
|
73
|
+
return {"error": "Celery not configured"}
|
|
74
|
+
|
|
75
|
+
result = self.celery_app.AsyncResult(task_id)
|
|
76
|
+
if result.state == "PENDING":
|
|
77
|
+
return {"status": "pending", "result": None}
|
|
78
|
+
elif result.state == "SUCCESS":
|
|
79
|
+
return {"status": "finished", "result": result.result}
|
|
80
|
+
elif result.state == "FAILURE":
|
|
81
|
+
return {"status": "error", "result": str(result.info)}
|
|
82
|
+
else:
|
|
83
|
+
return {"status": result.state, "result": None}
|
|
84
|
+
|
|
85
|
+
def submit_task(self, func: Callable, *args, **kwargs):
|
|
86
|
+
"""Submit task directly to Celery queue."""
|
|
87
|
+
if not hasattr(func, "celery_task"):
|
|
88
|
+
raise RuntimeError(
|
|
89
|
+
f"Function {func.__name__} is not registered as a task",
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
return func.celery_task.delay(*args, **kwargs)
|
|
@@ -1,3 +1,16 @@
|
|
|
1
1
|
# -*- coding: utf-8 -*-
|
|
2
2
|
from .base import DeployManager
|
|
3
3
|
from .local_deployer import LocalDeployManager
|
|
4
|
+
from .kubernetes_deployer import (
|
|
5
|
+
KubernetesDeployManager,
|
|
6
|
+
)
|
|
7
|
+
from .modelstudio_deployer import (
|
|
8
|
+
ModelstudioDeployManager,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"DeployManager",
|
|
13
|
+
"LocalDeployManager",
|
|
14
|
+
"KubernetesDeployManager",
|
|
15
|
+
"ModelstudioDeployManager",
|
|
16
|
+
]
|
|
File without changes
|