create-windy 0.2.20 → 0.2.22
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.
- package/README.md +1 -1
- package/dist/cli.js +93 -11
- package/package.json +1 -1
- package/template/.windy-template.json +2 -2
- package/template/README.md +1 -0
- package/template/apps/agent-server/Dockerfile +20 -0
- package/template/apps/agent-server/README.md +46 -0
- package/template/apps/agent-server/pyproject.toml +44 -0
- package/template/apps/agent-server/src/southwind_agent_server/__init__.py +5 -0
- package/template/apps/agent-server/src/southwind_agent_server/adk_adapter.py +39 -0
- package/template/apps/agent-server/src/southwind_agent_server/app.py +263 -0
- package/template/apps/agent-server/src/southwind_agent_server/config.py +141 -0
- package/template/apps/agent-server/src/southwind_agent_server/contracts.py +99 -0
- package/template/apps/agent-server/src/southwind_agent_server/deterministic.py +52 -0
- package/template/apps/agent-server/src/southwind_agent_server/engine.py +85 -0
- package/template/apps/agent-server/src/southwind_agent_server/openai_engine.py +197 -0
- package/template/apps/agent-server/src/southwind_agent_server/openai_provider.py +246 -0
- package/template/apps/agent-server/src/southwind_agent_server/service.py +180 -0
- package/template/apps/agent-server/src/southwind_agent_server/sqlite_store.py +317 -0
- package/template/apps/agent-server/src/southwind_agent_server/sse.py +41 -0
- package/template/apps/agent-server/src/southwind_agent_server/store.py +144 -0
- package/template/apps/agent-server/src/southwind_agent_server/tool_host.py +80 -0
- package/template/apps/agent-server/tests/test_api.py +207 -0
- package/template/apps/agent-server/tests/test_config.py +56 -0
- package/template/apps/agent-server/tests/test_openai_provider.py +224 -0
- package/template/apps/agent-server/tests/test_sqlite_store.py +93 -0
- package/template/apps/agent-server/uv.lock +925 -0
- package/template/apps/server/src/agent/execution-grants.ts +89 -0
- package/template/apps/server/src/agent/remote-runtime.ts +128 -0
- package/template/apps/server/src/agent/run-contracts.ts +43 -0
- package/template/apps/server/src/agent/run-facade.test.ts +257 -0
- package/template/apps/server/src/agent/run-facade.ts +190 -0
- package/template/apps/server/src/agent/run-routes.ts +222 -0
- package/template/apps/server/src/agent/runtime-bootstrap.ts +53 -0
- package/template/apps/server/src/agent/tool-host.test.ts +242 -0
- package/template/apps/server/src/agent/tool-host.ts +153 -0
- package/template/apps/server/src/index.ts +26 -22
- package/template/apps/server/src/module-composition.test.ts +9 -0
- package/template/apps/server/src/module-composition.ts +13 -0
- package/template/apps/server/src/module-host/initialize-contracts.ts +67 -0
- package/template/apps/server/src/module-host/initialize.test.ts +69 -0
- package/template/apps/server/src/module-host/initialize.ts +50 -53
- package/template/apps/server/src/module-host.ts +7 -0
- package/template/apps/server/src/module-runtime-validation.ts +39 -0
- package/template/docker-compose.yml +25 -0
- package/template/docs/architecture/ai-runtime.md +744 -0
- package/template/docs/platform/agent-runtime.md +128 -0
- package/template/docs/platform/agent-tools.md +8 -1
- package/template/package.json +1 -0
- package/template/packages/config/index.test.ts +43 -0
- package/template/packages/config/index.ts +1 -0
- package/template/packages/config/package.json +2 -1
- package/template/packages/config/src/ai-openai-compatible.ts +131 -0
- package/template/packages/server-sdk/index.ts +11 -0
- package/template/packages/server-sdk/package.json +4 -2
- package/template/packages/server-sdk/src/ai-operation-registration.ts +31 -0
- package/template/packages/server-sdk/src/module-host.ts +5 -0
- package/template/packages/server-sdk/src/tool-host-port.ts +22 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import hmac
|
|
4
|
+
import os
|
|
5
|
+
import uuid
|
|
6
|
+
from typing import Annotated
|
|
7
|
+
|
|
8
|
+
from fastapi import FastAPI, Header, Query, Request
|
|
9
|
+
from fastapi.exceptions import RequestValidationError
|
|
10
|
+
from fastapi.responses import JSONResponse, StreamingResponse
|
|
11
|
+
from pydantic import ValidationError
|
|
12
|
+
|
|
13
|
+
from .adk_adapter import AdkEngineAdapter
|
|
14
|
+
from .config import EnvironmentSecretResolver, OpenAiCompatibleConfig
|
|
15
|
+
from .contracts import (
|
|
16
|
+
AgentError,
|
|
17
|
+
CancelRunRequest,
|
|
18
|
+
CancelRunResponse,
|
|
19
|
+
RunResultResponse,
|
|
20
|
+
StartRunRequest,
|
|
21
|
+
StartRunResponse,
|
|
22
|
+
)
|
|
23
|
+
from .engine import AgentEngineAdapter
|
|
24
|
+
from .openai_engine import OpenAiCompatibleEngineAdapter
|
|
25
|
+
from .openai_provider import OpenAiCompatibleProvider
|
|
26
|
+
from .service import (
|
|
27
|
+
AgentRunService,
|
|
28
|
+
IdempotencyConflictError,
|
|
29
|
+
OperationNotFoundError,
|
|
30
|
+
RunNotFoundError,
|
|
31
|
+
)
|
|
32
|
+
from .sqlite_store import RunStoreCapacityError, SqliteRunStore
|
|
33
|
+
from .sse import event_stream
|
|
34
|
+
from .store import InMemoryRunStore, RunStore
|
|
35
|
+
from .tool_host import ToolHostClient
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def create_app(
|
|
39
|
+
engine: AgentEngineAdapter | None = None,
|
|
40
|
+
*,
|
|
41
|
+
heartbeat_seconds: float = 15.0,
|
|
42
|
+
internal_token: str | None = None,
|
|
43
|
+
store: RunStore | None = None,
|
|
44
|
+
) -> FastAPI:
|
|
45
|
+
runtime_engine = engine or default_engine()
|
|
46
|
+
runtime = AgentRunService(runtime_engine, store or default_store())
|
|
47
|
+
application = FastAPI(title="Southwind AI Agent Server", version="0.1.0")
|
|
48
|
+
|
|
49
|
+
@application.get("/health/live")
|
|
50
|
+
async def live() -> dict[str, str]:
|
|
51
|
+
return {"status": "live"}
|
|
52
|
+
|
|
53
|
+
@application.get("/health/ready", response_model=None)
|
|
54
|
+
async def ready() -> dict[str, str] | JSONResponse:
|
|
55
|
+
health = getattr(runtime_engine, "health", None)
|
|
56
|
+
if not health:
|
|
57
|
+
return safe_error(
|
|
58
|
+
503,
|
|
59
|
+
"RUNTIME_UNAVAILABLE",
|
|
60
|
+
"Agent Runtime 尚未配置",
|
|
61
|
+
True,
|
|
62
|
+
)
|
|
63
|
+
try:
|
|
64
|
+
await health()
|
|
65
|
+
return {"status": "ready"}
|
|
66
|
+
except Exception:
|
|
67
|
+
return safe_error(
|
|
68
|
+
503,
|
|
69
|
+
"PROVIDER_UNAVAILABLE",
|
|
70
|
+
"模型服务暂不可用",
|
|
71
|
+
True,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
@application.exception_handler(OperationNotFoundError)
|
|
75
|
+
async def operation_not_found(
|
|
76
|
+
_request: Request,
|
|
77
|
+
_error: OperationNotFoundError,
|
|
78
|
+
) -> JSONResponse:
|
|
79
|
+
return safe_error(404, "OPERATION_NOT_FOUND", "Agent Operation 不存在", False)
|
|
80
|
+
|
|
81
|
+
@application.exception_handler(RunNotFoundError)
|
|
82
|
+
async def run_not_found(
|
|
83
|
+
_request: Request,
|
|
84
|
+
_error: RunNotFoundError,
|
|
85
|
+
) -> JSONResponse:
|
|
86
|
+
return safe_error(404, "RUN_CONFLICT", "Agent Run 不存在", False)
|
|
87
|
+
|
|
88
|
+
@application.exception_handler(IdempotencyConflictError)
|
|
89
|
+
async def idempotency_conflict(
|
|
90
|
+
_request: Request,
|
|
91
|
+
_error: IdempotencyConflictError,
|
|
92
|
+
) -> JSONResponse:
|
|
93
|
+
return safe_error(409, "RUN_CONFLICT", "幂等键已用于其他请求", False)
|
|
94
|
+
|
|
95
|
+
@application.exception_handler(RunStoreCapacityError)
|
|
96
|
+
async def run_store_capacity(
|
|
97
|
+
_request: Request,
|
|
98
|
+
_error: RunStoreCapacityError,
|
|
99
|
+
) -> JSONResponse:
|
|
100
|
+
return safe_error(
|
|
101
|
+
429,
|
|
102
|
+
"BUDGET_EXCEEDED",
|
|
103
|
+
"Agent Run 存储容量已满",
|
|
104
|
+
True,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
@application.exception_handler(ValidationError)
|
|
108
|
+
async def validation_error(
|
|
109
|
+
_request: Request,
|
|
110
|
+
_error: ValidationError,
|
|
111
|
+
) -> JSONResponse:
|
|
112
|
+
return safe_error(400, "INVALID_INPUT", "请求格式无效", False)
|
|
113
|
+
|
|
114
|
+
@application.exception_handler(RequestValidationError)
|
|
115
|
+
async def request_validation_error(
|
|
116
|
+
_request: Request,
|
|
117
|
+
_error: RequestValidationError,
|
|
118
|
+
) -> JSONResponse:
|
|
119
|
+
return safe_error(400, "INVALID_INPUT", "请求格式无效", False)
|
|
120
|
+
|
|
121
|
+
@application.post("/api/agent/runs", response_model=StartRunResponse)
|
|
122
|
+
async def start_run(
|
|
123
|
+
request: StartRunRequest,
|
|
124
|
+
idempotency_key: Annotated[str, Header(alias="Idempotency-Key")],
|
|
125
|
+
authorization: Annotated[str | None, Header()] = None,
|
|
126
|
+
) -> StartRunResponse | JSONResponse:
|
|
127
|
+
if not authorized(authorization, internal_token):
|
|
128
|
+
return safe_error(403, "FORBIDDEN", "内部调用认证失败", False)
|
|
129
|
+
if not idempotency_key.strip():
|
|
130
|
+
return safe_error(400, "INVALID_INPUT", "幂等键不能为空", False)
|
|
131
|
+
record = await runtime.start(
|
|
132
|
+
request,
|
|
133
|
+
idempotency_key,
|
|
134
|
+
)
|
|
135
|
+
return StartRunResponse(
|
|
136
|
+
run_id=record.run_id,
|
|
137
|
+
status=record.status,
|
|
138
|
+
events_url=f"/api/agent/runs/{record.run_id}/events",
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
@application.get("/api/agent/runs/{run_id}/events", response_model=None)
|
|
142
|
+
async def get_events(
|
|
143
|
+
run_id: str,
|
|
144
|
+
after: Annotated[int | None, Query(ge=0)] = None,
|
|
145
|
+
last_event_id: Annotated[str | None, Header(alias="Last-Event-ID")] = None,
|
|
146
|
+
authorization: Annotated[str | None, Header()] = None,
|
|
147
|
+
) -> StreamingResponse | JSONResponse:
|
|
148
|
+
if not authorized(authorization, internal_token):
|
|
149
|
+
return safe_error(403, "FORBIDDEN", "内部调用认证失败", False)
|
|
150
|
+
await runtime.get(run_id)
|
|
151
|
+
try:
|
|
152
|
+
cursor = parse_cursor(after, last_event_id)
|
|
153
|
+
except ValueError:
|
|
154
|
+
return safe_error(400, "INVALID_INPUT", "事件游标无效", False)
|
|
155
|
+
return StreamingResponse(
|
|
156
|
+
event_stream(runtime, run_id, cursor, heartbeat_seconds),
|
|
157
|
+
media_type="text/event-stream",
|
|
158
|
+
headers={
|
|
159
|
+
"Cache-Control": "no-cache, no-transform",
|
|
160
|
+
"X-Accel-Buffering": "no",
|
|
161
|
+
},
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
@application.get(
|
|
165
|
+
"/api/agent/runs/{run_id}/result",
|
|
166
|
+
response_model=RunResultResponse,
|
|
167
|
+
)
|
|
168
|
+
async def get_result(
|
|
169
|
+
run_id: str,
|
|
170
|
+
authorization: Annotated[str | None, Header()] = None,
|
|
171
|
+
) -> RunResultResponse | JSONResponse:
|
|
172
|
+
if not authorized(authorization, internal_token):
|
|
173
|
+
return safe_error(403, "FORBIDDEN", "内部调用认证失败", False)
|
|
174
|
+
record = await runtime.get(run_id)
|
|
175
|
+
if record.status == "completed":
|
|
176
|
+
return RunResultResponse(
|
|
177
|
+
run_id=record.run_id,
|
|
178
|
+
status="completed",
|
|
179
|
+
output=record.output,
|
|
180
|
+
)
|
|
181
|
+
if record.status == "cancelled":
|
|
182
|
+
return safe_error(409, "RUN_CANCELLED", "Agent Run 已取消", False)
|
|
183
|
+
if record.status == "failed":
|
|
184
|
+
return safe_error(409, "RUNTIME_UNAVAILABLE", "Agent Run 执行失败", True)
|
|
185
|
+
return safe_error(409, "RUN_CONFLICT", "Agent Run 尚未完成", True)
|
|
186
|
+
|
|
187
|
+
@application.post(
|
|
188
|
+
"/api/agent/runs/{run_id}/cancel",
|
|
189
|
+
response_model=CancelRunResponse,
|
|
190
|
+
)
|
|
191
|
+
async def cancel_run(
|
|
192
|
+
run_id: str,
|
|
193
|
+
_request: CancelRunRequest,
|
|
194
|
+
authorization: Annotated[str | None, Header()] = None,
|
|
195
|
+
) -> CancelRunResponse | JSONResponse:
|
|
196
|
+
if not authorized(authorization, internal_token):
|
|
197
|
+
return safe_error(403, "FORBIDDEN", "内部调用认证失败", False)
|
|
198
|
+
record = await runtime.cancel(run_id)
|
|
199
|
+
return CancelRunResponse(run_id=record.run_id, status=record.status)
|
|
200
|
+
|
|
201
|
+
application.state.run_service = runtime
|
|
202
|
+
return application
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def default_engine() -> AgentEngineAdapter:
|
|
206
|
+
config = OpenAiCompatibleConfig.from_environment()
|
|
207
|
+
if config is None:
|
|
208
|
+
return AdkEngineAdapter()
|
|
209
|
+
internal_token = os.environ.get("AGENT_SERVER_INTERNAL_TOKEN", "").strip()
|
|
210
|
+
if not internal_token:
|
|
211
|
+
raise ValueError("Agent Server Provider 已配置但内部 Token 缺失")
|
|
212
|
+
return OpenAiCompatibleEngineAdapter(
|
|
213
|
+
OpenAiCompatibleProvider(
|
|
214
|
+
config,
|
|
215
|
+
EnvironmentSecretResolver(os.environ),
|
|
216
|
+
),
|
|
217
|
+
ToolHostClient(internal_token),
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def default_store() -> RunStore:
|
|
222
|
+
path = os.environ.get("AGENT_RUN_STORE_PATH", "").strip()
|
|
223
|
+
return SqliteRunStore(path) if path else InMemoryRunStore()
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def authorized(provided: str | None, expected: str | None) -> bool:
|
|
227
|
+
if expected is None:
|
|
228
|
+
return True
|
|
229
|
+
if not expected or not provided or not provided.startswith("Bearer "):
|
|
230
|
+
return False
|
|
231
|
+
return hmac.compare_digest(provided.removeprefix("Bearer "), expected)
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def parse_cursor(after: int | None, last_event_id: str | None) -> int:
|
|
235
|
+
header_cursor = 0
|
|
236
|
+
if last_event_id is not None:
|
|
237
|
+
if not last_event_id.isdigit():
|
|
238
|
+
raise ValueError
|
|
239
|
+
header_cursor = int(last_event_id)
|
|
240
|
+
return max(after or 0, header_cursor)
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def safe_error(
|
|
244
|
+
status_code: int,
|
|
245
|
+
code: str,
|
|
246
|
+
message: str,
|
|
247
|
+
retryable: bool,
|
|
248
|
+
) -> JSONResponse:
|
|
249
|
+
error = AgentError.model_validate(
|
|
250
|
+
{
|
|
251
|
+
"code": code,
|
|
252
|
+
"message": message,
|
|
253
|
+
"requestId": f"request_{uuid.uuid4().hex}",
|
|
254
|
+
"retryable": retryable,
|
|
255
|
+
}
|
|
256
|
+
)
|
|
257
|
+
return JSONResponse(
|
|
258
|
+
status_code=status_code,
|
|
259
|
+
content=error.model_dump(by_alias=True),
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
app = create_app(internal_token=os.environ.get("AGENT_SERVER_INTERNAL_TOKEN"))
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import ipaddress
|
|
4
|
+
import os
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from urllib.parse import urlsplit, urlunsplit
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True)
|
|
11
|
+
class SecretRef:
|
|
12
|
+
provider: str
|
|
13
|
+
locator: str
|
|
14
|
+
|
|
15
|
+
def __str__(self) -> str:
|
|
16
|
+
return "[SecretRef]"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class EnvironmentSecretResolver:
|
|
20
|
+
def __init__(self, environment: Mapping[str, str]) -> None:
|
|
21
|
+
self._environment = environment
|
|
22
|
+
|
|
23
|
+
def resolve(self, ref: SecretRef) -> str | None:
|
|
24
|
+
if ref.provider != "env":
|
|
25
|
+
raise ValueError("Secret Provider 不匹配")
|
|
26
|
+
return self._environment.get(ref.locator)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True)
|
|
30
|
+
class OpenAiCompatibleConfig:
|
|
31
|
+
base_url: str
|
|
32
|
+
model: str
|
|
33
|
+
api_key_ref: SecretRef | None
|
|
34
|
+
connect_timeout_seconds: float = 5.0
|
|
35
|
+
request_timeout_seconds: float = 60.0
|
|
36
|
+
max_response_bytes: int = 2 * 1024 * 1024
|
|
37
|
+
max_tool_rounds: int = 8
|
|
38
|
+
|
|
39
|
+
@classmethod
|
|
40
|
+
def from_environment(
|
|
41
|
+
cls,
|
|
42
|
+
environment: Mapping[str, str] = os.environ,
|
|
43
|
+
) -> OpenAiCompatibleConfig | None:
|
|
44
|
+
base_url = environment.get("QWEN_API_URL", "").strip()
|
|
45
|
+
model = environment.get("QWEN_MODEL", "").strip()
|
|
46
|
+
if not base_url and not model:
|
|
47
|
+
return None
|
|
48
|
+
if not base_url or not model:
|
|
49
|
+
raise ValueError("OpenAI-compatible Endpoint 与 Model 必须同时配置")
|
|
50
|
+
return cls(
|
|
51
|
+
base_url=normalize_base_url(base_url),
|
|
52
|
+
model=model,
|
|
53
|
+
api_key_ref=(
|
|
54
|
+
SecretRef("env", "QWEN_API_KEY")
|
|
55
|
+
if environment.get("QWEN_API_KEY", "").strip()
|
|
56
|
+
else None
|
|
57
|
+
),
|
|
58
|
+
connect_timeout_seconds=read_milliseconds(
|
|
59
|
+
environment,
|
|
60
|
+
"AI_PROVIDER_CONNECT_TIMEOUT_MS",
|
|
61
|
+
5_000,
|
|
62
|
+
100,
|
|
63
|
+
30_000,
|
|
64
|
+
)
|
|
65
|
+
/ 1_000,
|
|
66
|
+
request_timeout_seconds=read_milliseconds(
|
|
67
|
+
environment,
|
|
68
|
+
"AI_PROVIDER_REQUEST_TIMEOUT_MS",
|
|
69
|
+
60_000,
|
|
70
|
+
1_000,
|
|
71
|
+
300_000,
|
|
72
|
+
)
|
|
73
|
+
/ 1_000,
|
|
74
|
+
max_response_bytes=read_integer(
|
|
75
|
+
environment,
|
|
76
|
+
"AI_PROVIDER_MAX_RESPONSE_BYTES",
|
|
77
|
+
2 * 1024 * 1024,
|
|
78
|
+
1_024,
|
|
79
|
+
16 * 1024 * 1024,
|
|
80
|
+
),
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def normalize_base_url(raw: str) -> str:
|
|
85
|
+
parsed = urlsplit(raw.strip())
|
|
86
|
+
if parsed.scheme not in {"http", "https"} or not parsed.hostname:
|
|
87
|
+
raise ValueError("OpenAI-compatible Endpoint 无效")
|
|
88
|
+
if parsed.username or parsed.password or parsed.query or parsed.fragment:
|
|
89
|
+
raise ValueError("OpenAI-compatible Endpoint 不允许凭据、查询串或片段")
|
|
90
|
+
hostname = parsed.hostname.lower()
|
|
91
|
+
if hostname in {
|
|
92
|
+
"169.254.169.254",
|
|
93
|
+
"metadata.google.internal",
|
|
94
|
+
"metadata.azure.internal",
|
|
95
|
+
}:
|
|
96
|
+
raise ValueError("OpenAI-compatible Endpoint 禁止访问云 metadata")
|
|
97
|
+
if parsed.scheme == "http" and not private_hostname(hostname):
|
|
98
|
+
raise ValueError("公网 OpenAI-compatible Endpoint 必须使用 HTTPS")
|
|
99
|
+
path = parsed.path.rstrip("/")
|
|
100
|
+
return urlunsplit((parsed.scheme, parsed.netloc, path, "", ""))
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def private_hostname(hostname: str) -> bool:
|
|
104
|
+
if (
|
|
105
|
+
hostname == "localhost"
|
|
106
|
+
or hostname.endswith((".localhost", ".internal", ".local"))
|
|
107
|
+
or "." not in hostname
|
|
108
|
+
):
|
|
109
|
+
return True
|
|
110
|
+
try:
|
|
111
|
+
address = ipaddress.ip_address(hostname)
|
|
112
|
+
except ValueError:
|
|
113
|
+
return False
|
|
114
|
+
return address.is_private or address.is_loopback
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def read_milliseconds(
|
|
118
|
+
environment: Mapping[str, str],
|
|
119
|
+
key: str,
|
|
120
|
+
default: int,
|
|
121
|
+
minimum: int,
|
|
122
|
+
maximum: int,
|
|
123
|
+
) -> int:
|
|
124
|
+
return read_integer(environment, key, default, minimum, maximum)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def read_integer(
|
|
128
|
+
environment: Mapping[str, str],
|
|
129
|
+
key: str,
|
|
130
|
+
default: int,
|
|
131
|
+
minimum: int,
|
|
132
|
+
maximum: int,
|
|
133
|
+
) -> int:
|
|
134
|
+
raw = environment.get(key)
|
|
135
|
+
try:
|
|
136
|
+
value = default if raw is None else int(raw)
|
|
137
|
+
except ValueError as error:
|
|
138
|
+
raise ValueError(f"{key} 必须是整数") from error
|
|
139
|
+
if not minimum <= value <= maximum:
|
|
140
|
+
raise ValueError(f"{key} 超出允许范围")
|
|
141
|
+
return value
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Literal
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel, ConfigDict, Field
|
|
6
|
+
|
|
7
|
+
type JsonValue = (
|
|
8
|
+
str | int | float | bool | None | list[JsonValue] | dict[str, JsonValue]
|
|
9
|
+
)
|
|
10
|
+
type RunStatus = Literal[
|
|
11
|
+
"accepted",
|
|
12
|
+
"running",
|
|
13
|
+
"awaiting-approval",
|
|
14
|
+
"completed",
|
|
15
|
+
"failed",
|
|
16
|
+
"cancelled",
|
|
17
|
+
]
|
|
18
|
+
type AgentErrorCode = Literal[
|
|
19
|
+
"OPERATION_NOT_FOUND",
|
|
20
|
+
"OPERATION_DISABLED",
|
|
21
|
+
"INVALID_INPUT",
|
|
22
|
+
"FORBIDDEN",
|
|
23
|
+
"LICENSE_NOT_ALLOWED",
|
|
24
|
+
"RUNTIME_UNAVAILABLE",
|
|
25
|
+
"MODEL_ROUTE_UNAVAILABLE",
|
|
26
|
+
"PROVIDER_UNAVAILABLE",
|
|
27
|
+
"RATE_LIMITED",
|
|
28
|
+
"BUDGET_EXCEEDED",
|
|
29
|
+
"TOOL_DENIED",
|
|
30
|
+
"APPROVAL_REQUIRED",
|
|
31
|
+
"OUTPUT_VALIDATION_FAILED",
|
|
32
|
+
"RUN_CONFLICT",
|
|
33
|
+
"RUN_TIMEOUT",
|
|
34
|
+
"RUN_CANCELLED",
|
|
35
|
+
"STREAM_INTERRUPTED",
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def to_camel(value: str) -> str:
|
|
40
|
+
first, *rest = value.split("_")
|
|
41
|
+
return first + "".join(part.capitalize() for part in rest)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class CanonicalModel(BaseModel):
|
|
45
|
+
model_config = ConfigDict(
|
|
46
|
+
alias_generator=to_camel,
|
|
47
|
+
populate_by_name=True,
|
|
48
|
+
extra="forbid",
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class RuntimeToolDefinition(CanonicalModel):
|
|
53
|
+
key: str = Field(min_length=1)
|
|
54
|
+
label: str = Field(min_length=1)
|
|
55
|
+
description: str = Field(min_length=1)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class RuntimeLimits(CanonicalModel):
|
|
59
|
+
max_duration_seconds: int = Field(default=60, ge=1, le=300)
|
|
60
|
+
max_output_bytes: int = Field(default=1_048_576, ge=1_024, le=16_777_216)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class StartRunRequest(CanonicalModel):
|
|
64
|
+
operation_key: str = Field(min_length=1)
|
|
65
|
+
input: JsonValue
|
|
66
|
+
instructions: str = ""
|
|
67
|
+
user_prompt: str = ""
|
|
68
|
+
allowed_tools: list[RuntimeToolDefinition] = Field(default_factory=list)
|
|
69
|
+
limits: RuntimeLimits = Field(default_factory=RuntimeLimits)
|
|
70
|
+
execution_grant: str = ""
|
|
71
|
+
tool_host_url: str = ""
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class StartRunResponse(CanonicalModel):
|
|
75
|
+
run_id: str
|
|
76
|
+
status: RunStatus
|
|
77
|
+
events_url: str
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class RunResultResponse(CanonicalModel):
|
|
81
|
+
run_id: str
|
|
82
|
+
status: Literal["completed"]
|
|
83
|
+
output: JsonValue
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class CancelRunRequest(CanonicalModel):
|
|
87
|
+
reason: str | None = None
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class CancelRunResponse(CanonicalModel):
|
|
91
|
+
run_id: str
|
|
92
|
+
status: RunStatus
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class AgentError(CanonicalModel):
|
|
96
|
+
code: AgentErrorCode
|
|
97
|
+
message: str
|
|
98
|
+
request_id: str
|
|
99
|
+
retryable: bool
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
from collections.abc import AsyncIterator
|
|
5
|
+
|
|
6
|
+
from .engine import (
|
|
7
|
+
EngineCompleted,
|
|
8
|
+
EngineRunRequest,
|
|
9
|
+
EngineSignal,
|
|
10
|
+
OutputDelta,
|
|
11
|
+
UsageReported,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class DeterministicEngineAdapter:
|
|
16
|
+
"""Deterministic test adapter. It never calls a model or remote service."""
|
|
17
|
+
|
|
18
|
+
operations = frozenset({"test.echo", "test.wait", "test.fail"})
|
|
19
|
+
|
|
20
|
+
def __init__(self) -> None:
|
|
21
|
+
self._cancellations: dict[str, asyncio.Event] = {}
|
|
22
|
+
|
|
23
|
+
def supports(self, operation_key: str) -> bool:
|
|
24
|
+
return operation_key in self.operations
|
|
25
|
+
|
|
26
|
+
async def execute(
|
|
27
|
+
self,
|
|
28
|
+
request: EngineRunRequest,
|
|
29
|
+
) -> AsyncIterator[EngineSignal]:
|
|
30
|
+
cancellation = self._cancellations.setdefault(request.run_id, asyncio.Event())
|
|
31
|
+
try:
|
|
32
|
+
if request.operation_key == "test.fail":
|
|
33
|
+
raise RuntimeError("upstream-secret-body")
|
|
34
|
+
if request.operation_key == "test.wait":
|
|
35
|
+
await cancellation.wait()
|
|
36
|
+
return
|
|
37
|
+
|
|
38
|
+
text = "deterministic"
|
|
39
|
+
if isinstance(request.input, dict):
|
|
40
|
+
input_text = request.input.get("text")
|
|
41
|
+
if isinstance(input_text, str):
|
|
42
|
+
text = input_text
|
|
43
|
+
yield OutputDelta(text)
|
|
44
|
+
yield UsageReported({"source": "unknown"})
|
|
45
|
+
yield EngineCompleted(request.input)
|
|
46
|
+
finally:
|
|
47
|
+
self._cancellations.pop(request.run_id, None)
|
|
48
|
+
|
|
49
|
+
async def cancel(self, run_id: str) -> None:
|
|
50
|
+
cancellation = self._cancellations.get(run_id)
|
|
51
|
+
if cancellation is not None:
|
|
52
|
+
cancellation.set()
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import AsyncIterator
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from typing import Protocol
|
|
6
|
+
|
|
7
|
+
from .contracts import AgentErrorCode, JsonValue, RuntimeLimits, RuntimeToolDefinition
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True)
|
|
11
|
+
class EngineRunRequest:
|
|
12
|
+
run_id: str
|
|
13
|
+
operation_key: str
|
|
14
|
+
input: JsonValue
|
|
15
|
+
instructions: str = ""
|
|
16
|
+
user_prompt: str = ""
|
|
17
|
+
allowed_tools: tuple[RuntimeToolDefinition, ...] = ()
|
|
18
|
+
limits: RuntimeLimits = field(default_factory=RuntimeLimits)
|
|
19
|
+
execution_grant: str = ""
|
|
20
|
+
tool_host_url: str = ""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(frozen=True)
|
|
24
|
+
class OutputDelta:
|
|
25
|
+
text: str
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True)
|
|
29
|
+
class ToolRequested:
|
|
30
|
+
tool_call_id: str
|
|
31
|
+
tool_key: str
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass(frozen=True)
|
|
35
|
+
class ApprovalRequired:
|
|
36
|
+
work_item_id: str
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(frozen=True)
|
|
40
|
+
class ToolCompleted:
|
|
41
|
+
tool_call_id: str
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(frozen=True)
|
|
45
|
+
class UsageReported:
|
|
46
|
+
usage: dict[str, JsonValue]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass(frozen=True)
|
|
50
|
+
class EngineCompleted:
|
|
51
|
+
output: JsonValue
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
type EngineSignal = (
|
|
55
|
+
OutputDelta
|
|
56
|
+
| ToolRequested
|
|
57
|
+
| ApprovalRequired
|
|
58
|
+
| ToolCompleted
|
|
59
|
+
| UsageReported
|
|
60
|
+
| EngineCompleted
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class SafeEngineError(Exception):
|
|
65
|
+
def __init__(
|
|
66
|
+
self,
|
|
67
|
+
code: AgentErrorCode,
|
|
68
|
+
message: str,
|
|
69
|
+
*,
|
|
70
|
+
retryable: bool,
|
|
71
|
+
) -> None:
|
|
72
|
+
super().__init__(message)
|
|
73
|
+
self.code = code
|
|
74
|
+
self.safe_message = message
|
|
75
|
+
self.retryable = retryable
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class AgentEngineAdapter(Protocol):
|
|
79
|
+
"""Internal engine seam. HTTP DTO and ADK types must not cross it."""
|
|
80
|
+
|
|
81
|
+
def supports(self, operation_key: str) -> bool: ...
|
|
82
|
+
|
|
83
|
+
def execute(self, request: EngineRunRequest) -> AsyncIterator[EngineSignal]: ...
|
|
84
|
+
|
|
85
|
+
async def cancel(self, run_id: str) -> None: ...
|