Skip to content

Commit 137fe13

Browse files
authored
feat(studio): add reusable runtime environments (#983)
* feat(studio): add reusable runtime environments * fix: align environment license headers * fix: complete Apache license headers * feat(studio): persist runtime environment selection * fix(studio): support BytePlus environment builds
1 parent efd837a commit 137fe13

118 files changed

Lines changed: 8646 additions & 1416 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/content/docs/framework/frontend.en.mdx‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,34 @@ resources use the selected deployment region. The deployed Function records the
267267
selected AgentKit Sandbox region so temporary chat and Skill-creation sessions
268268
query the same region as their CodeEnv Tools.
269269

270+
### Environment image builds
271+
272+
The Studio Environment page stores environment definitions, Dockerfiles, build
273+
versions, log metadata, and image references in the private Studio TOS bucket.
274+
Creating or saving an environment starts a CodePipeline image build and pushes
275+
the result to Container Registry. The first build creates or reuses managed
276+
workspace, pipeline, and repository resources by default. Volcengine builds use
277+
the Aliyun PyPI mirror, Huawei Cloud Python source mirror, and npmmirror for
278+
Playwright browsers; BytePlus builds use the corresponding official sources.
279+
Cross-version Python combinations compile pinned source releases instead of
280+
depending on GitHub-hosted binaries.
281+
With the account-stable default TOS bucket, Studio reuses the account's
282+
`agentkit-cli-<account-id>` CR instance and creates the
283+
`runtime-environments/base-images` repository inside it.
284+
285+
Existing resources can be supplied independently when deploying Studio:
286+
287+
```bash
288+
veadk studio deploy \
289+
--vefaas-app-name <app-name> \
290+
--environment-cp-workspace <workspace-id-or-name> \
291+
--environment-cr-repository <registry/namespace/repository>
292+
```
293+
294+
These settings are accepted only as CLI flags, not as user environment-variable
295+
inputs. The deployed System Information page shows the resolved CodePipeline and
296+
Container Registry names, ownership mode, and provider console links.
297+
270298
To update an existing Studio deployment, run this command from a VeADK source
271299
checkout:
272300

‎docs/content/docs/framework/frontend.mdx‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,29 @@ veadk studio deploy \
230230
用户池;跨地域命中时会显示 warning 并继续。`--project` 指定 VeFaaS 函数
231231
所属项目,默认为 `default`。
232232

233+
### 环境镜像构建
234+
235+
Studio 的“环境”页面会把环境配置、Dockerfile、构建版本、日志元数据和镜像地址保存到
236+
私有 TOS。创建或保存环境后,Studio 使用 CodePipeline 构建镜像,并推送到
237+
Container Registry。首次构建默认自动创建或复用托管的 Workspace、Pipeline 和镜像仓库。
238+
火山引擎构建会使用阿里云 PyPI 镜像、华为云 Python 源码镜像和 npmmirror 的
239+
Playwright 浏览器镜像;BytePlus 构建使用对应的官方源。跨版本 Python 组合使用固定
240+
补丁版本的源码构建,不依赖 GitHub 托管的二进制。
241+
使用账号级默认 TOS 桶时,CR 会复用 `agentkit-cli-<account-id>` 实例,并在其中创建
242+
`runtime-environments/base-images` 仓库。
243+
244+
也可以在部署 Studio 时通过 Flag 指定已有资源,两项可以独立使用:
245+
246+
```bash
247+
veadk studio deploy \
248+
--vefaas-app-name <app-name> \
249+
--environment-cp-workspace <workspace-id-or-name> \
250+
--environment-cr-repository <registry/namespace/repository>
251+
```
252+
253+
这些配置不从用户环境变量读取。部署后的“系统信息”页面会显示最终使用的
254+
CodePipeline 和 Container Registry 名称、来源及控制台链接。
255+
233256
更新已部署的 Studio 时,在 VeADK 源码目录执行:
234257

235258
```bash

‎frontend/README.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,34 @@ Supported logo formats are PNG, JPEG, GIF, WebP, AVIF, and ICO, up to 5 MB.
449449
configuration. `veadk studio deploy` accepts the same flags and copies either a
450450
local image or a downloaded network image into the VeFaaS deployment package.
451451

452+
## Environment image builds
453+
454+
The Studio `环境` page stores each environment definition, generated Dockerfile,
455+
build version, log metadata, and resulting image reference in the private Studio
456+
TOS bucket. Creating or saving an environment starts an asynchronous
457+
CodePipeline build and pushes the resulting image to Container Registry.
458+
Volcengine builds use the Aliyun PyPI mirror, Huawei Cloud Python source mirror,
459+
and npmmirror for Playwright browsers; BytePlus builds use the corresponding
460+
official sources. Cross-version Python combinations are compiled from pinned
461+
source releases instead of depending on GitHub-hosted binaries.
462+
463+
By default, Studio creates or reuses managed CodePipeline and Container Registry
464+
resources on the first environment build. With the account-stable default TOS
465+
bucket, Studio reuses the account's `agentkit-cli-<account-id>` CR instance and
466+
creates the `runtime-environments/base-images` repository inside it. Existing
467+
resources can be selected at deployment time with flags only:
468+
469+
```bash
470+
veadk studio deploy \
471+
--vefaas-app-name <app-name> \
472+
--environment-cp-workspace <workspace-id-or-name> \
473+
--environment-cr-repository <registry/namespace/repository>
474+
```
475+
476+
Either flag can be supplied independently. The System Information page shows
477+
the resolved workspace, pipeline, repository, ownership mode, and provider
478+
console links. These values are resource identifiers, not credentials.
479+
452480
## In-app Studio updates
453481

454482
Studio deployments use the centrally maintained `veadk-studio` TOS bucket in
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Studio execution-environment backend composition."""
16+
17+
from __future__ import annotations
18+
19+
import os
20+
from collections.abc import Callable, Mapping
21+
from typing import Any
22+
23+
from frontend.server.storage import StudioProvider, StudioStorageConfig
24+
from frontend.server.storage.tos import CredentialResolver, create_tos_client_factory
25+
from veadk.utils.cloud_provider import default_region
26+
27+
from .repository import TosEnvironmentRepository
28+
from .resources import EnvironmentResourceSettings, StudioEnvironmentCloudGateway
29+
from .routes import mount_environment_routes
30+
from .service import EnvironmentService
31+
32+
33+
def create_environment_service(
34+
*,
35+
provider: StudioProvider = "volcengine",
36+
resolve_credentials: CredentialResolver | None = None,
37+
client_factory: Callable[[], Any] | None = None,
38+
environment: Mapping[str, str] | None = None,
39+
) -> EnvironmentService:
40+
storage = StudioStorageConfig.from_env(provider, environment)
41+
if not storage.configured:
42+
return EnvironmentService(
43+
None, None, unavailable_reason=storage.unavailable_reason
44+
)
45+
if client_factory is None:
46+
if resolve_credentials is None:
47+
return EnvironmentService(
48+
None,
49+
None,
50+
unavailable_reason="管理员未配置环境存储与构建凭据。",
51+
)
52+
client_factory = create_tos_client_factory(storage, resolve_credentials)
53+
source = environment if environment is not None else os.environ
54+
deploy_region = str(source.get("VEADK_STUDIO_DEPLOY_REGION") or "").strip()
55+
settings = EnvironmentResourceSettings.from_env(
56+
provider=provider,
57+
region=deploy_region or default_region(provider),
58+
bucket=storage.bucket,
59+
source=source,
60+
)
61+
return EnvironmentService(
62+
TosEnvironmentRepository(
63+
bucket=storage.bucket,
64+
client_factory=client_factory,
65+
),
66+
StudioEnvironmentCloudGateway(
67+
settings,
68+
resolve_credentials=resolve_credentials,
69+
),
70+
)
71+
72+
73+
__all__ = [
74+
"EnvironmentService",
75+
"create_environment_service",
76+
"mount_environment_routes",
77+
]
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Deterministic Dockerfile generation for Studio environments."""
16+
17+
from __future__ import annotations
18+
19+
from dataclasses import dataclass
20+
21+
from .models import EnvironmentInput
22+
23+
24+
@dataclass(frozen=True)
25+
class _Package:
26+
label: str
27+
description: str
28+
installer: str
29+
package_name: str
30+
31+
32+
_OPERATING_SYSTEMS = {
33+
"ubuntu-22.04": ("Ubuntu 22.04", "ubuntu:22.04"),
34+
"ubuntu-24.04": ("Ubuntu 24.04", "ubuntu:24.04"),
35+
}
36+
37+
_PYTHON_PATCH_VERSIONS = {
38+
"3.10": "3.10.18",
39+
"3.12": "3.12.11",
40+
}
41+
42+
_VEADK_RUNTIME_REQUIREMENT = (
43+
'"veadk-python[a2ui,database,eval,extensions,harness,harness-sidecar,pdf,speech]'
44+
'>=1.1.1"'
45+
)
46+
47+
_PACKAGES = {
48+
"lark-cli": _Package("lark-cli", "飞书开放平台命令行工具", "pip", "lark-cli"),
49+
"pandoc": _Package("pandoc", "文档格式转换工具", "apt", "pandoc"),
50+
"opencli": _Package(
51+
"opencli",
52+
"将网站与桌面应用转换为命令行工具",
53+
"npm",
54+
"@jackwener/opencli@1.8.7",
55+
),
56+
"uv": _Package("uv", "快速 Python 包与项目管理器", "pip", "uv"),
57+
"ripgrep": _Package("ripgrep", "高性能文本检索工具", "apt", "ripgrep"),
58+
"jq": _Package("jq", "JSON 查询与转换工具", "apt", "jq"),
59+
"github-cli": _Package("GitHub CLI", "在终端中管理 GitHub 工作流", "apt", "gh"),
60+
"playwright": _Package(
61+
"Playwright", "浏览器自动化与端到端测试", "pip", "playwright"
62+
),
63+
"chromium": _Package("Chromium", "无头浏览器运行时", "apt", "chromium"),
64+
"git": _Package("Git", "代码版本管理", "apt", "git"),
65+
"curl": _Package("curl", "网络请求与文件下载", "apt", "curl"),
66+
"ffmpeg": _Package("FFmpeg", "音视频转码与处理", "apt", "ffmpeg"),
67+
"imagemagick": _Package("ImageMagick", "图片转换与批处理", "apt", "imagemagick"),
68+
}
69+
70+
71+
def build_dockerfile(config: EnvironmentInput) -> str:
72+
"""Build the canonical Dockerfile when the user did not provide one."""
73+
if config.dockerfile:
74+
return validate_dockerfile(config.dockerfile)
75+
76+
os_label, base_image = _OPERATING_SYSTEMS[config.operating_system]
77+
python_version = config.language.removeprefix("python-")
78+
python_patch_version = _PYTHON_PATCH_VERSIONS[python_version]
79+
selected_options = set(config.option_ids)
80+
uses_ubuntu_python = (
81+
config.operating_system == "ubuntu-22.04" and python_version == "3.10"
82+
) or (config.operating_system == "ubuntu-24.04" and python_version == "3.12")
83+
lines = [
84+
f"# Operating system: {os_label}",
85+
f"FROM {base_image}",
86+
"",
87+
"ARG DEBIAN_FRONTEND=noninteractive",
88+
"ARG PIP_INDEX_URL=https://pypi.org/simple",
89+
"ARG PYTHON_SOURCE_BASE_URL=https://www.python.org/ftp/python",
90+
"ARG PLAYWRIGHT_DOWNLOAD_HOST=https://cdn.playwright.dev",
91+
"ARG PIP_DEFAULT_TIMEOUT=300",
92+
"ARG PIP_RETRIES=10",
93+
"",
94+
"# Ubuntu repositories: keep the official hosts, use HTTPS, and tolerate transient builder egress failures.",
95+
'RUN printf \'Acquire::Retries "5";\\nAcquire::ForceIPv4 "true";\\nAcquire::http::Timeout "60";\\nAcquire::https::Timeout "60";\\n\' > /etc/apt/apt.conf.d/80-veadk-network \\',
96+
" && sed -i 's|http://archive.ubuntu.com|https://archive.ubuntu.com|g; s|http://security.ubuntu.com|https://security.ubuntu.com|g' /etc/apt/sources.list /etc/apt/sources.list.d/*.sources 2>/dev/null || true",
97+
"",
98+
"ENV PYTHONDONTWRITEBYTECODE=1 \\",
99+
" PYTHONUNBUFFERED=1 \\",
100+
" PIP_NO_CACHE_DIR=1",
101+
"",
102+
f"# Python {python_version}",
103+
"RUN apt-get update \\",
104+
]
105+
if uses_ubuntu_python:
106+
lines.extend(
107+
(
108+
f" && apt-get install -y --no-install-recommends ca-certificates python{python_version} python{python_version}-venv \\",
109+
f" && python{python_version} -m venv /opt/venv \\",
110+
" && rm -rf /var/lib/apt/lists/*",
111+
)
112+
)
113+
else:
114+
lines.extend(
115+
(
116+
" && apt-get install -y --no-install-recommends build-essential ca-certificates curl libbz2-dev libffi-dev libgdbm-dev liblzma-dev libncursesw5-dev libreadline-dev libsqlite3-dev libssl-dev tk-dev uuid-dev zlib1g-dev \\",
117+
f' && curl --retry 5 --retry-all-errors --connect-timeout 30 -fsSL "${{PYTHON_SOURCE_BASE_URL}}/{python_patch_version}/Python-{python_patch_version}.tgz" -o /tmp/python.tgz \\',
118+
" && mkdir -p /tmp/python-source \\",
119+
" && tar -xzf /tmp/python.tgz --strip-components=1 -C /tmp/python-source \\",
120+
" && cd /tmp/python-source \\",
121+
" && ./configure --prefix=/opt/python --with-ensurepip=install \\",
122+
' && make -j"$(nproc)" \\',
123+
" && make install \\",
124+
f" && /opt/python/bin/python{python_version} -m venv /opt/venv \\",
125+
" && rm -rf /tmp/python-source /tmp/python.tgz /var/lib/apt/lists/*",
126+
)
127+
)
128+
lines.extend(("", 'ENV PATH="/opt/venv/bin:$PATH"'))
129+
if {"playwright", "chromium"} & selected_options:
130+
lines.extend(
131+
(
132+
"",
133+
"ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright \\",
134+
" PLAYWRIGHT_DOWNLOAD_CONNECTION_TIMEOUT=300000",
135+
)
136+
)
137+
lines.extend(
138+
(
139+
"",
140+
"WORKDIR /workspace",
141+
"",
142+
"# VeADK: preinstall the complete Agent runtime dependency profile.",
143+
"# lxml-html-clean: VeADK Studio webpage content runtime dependency.",
144+
"# Agent images based on this environment only need to copy user code.",
145+
"RUN python -m pip install --upgrade \\",
146+
f" {_VEADK_RUNTIME_REQUIREMENT} \\",
147+
' "agentkit-sdk-python==0.8.4" \\',
148+
' "starlette<1.0.0" \\',
149+
" lxml-html-clean",
150+
'ENV VEADK_ENVIRONMENT_IMAGE="1"',
151+
)
152+
)
153+
for option_id in config.option_ids:
154+
package = _PACKAGES[option_id]
155+
lines.extend(("", f"# {package.label}: {package.description}"))
156+
if option_id == "github-cli":
157+
lines.extend(
158+
(
159+
"RUN apt-get update \\",
160+
" && apt-get install -y --no-install-recommends wget \\",
161+
" && mkdir -p -m 755 /etc/apt/keyrings \\",
162+
" && wget -qO /etc/apt/keyrings/githubcli-archive-keyring.gpg https://cli.github.com/packages/githubcli-archive-keyring.gpg \\",
163+
" && chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \\",
164+
' && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" > /etc/apt/sources.list.d/github-cli.list \\',
165+
" && apt-get update \\",
166+
" && apt-get install -y --no-install-recommends gh \\",
167+
" && rm -rf /var/lib/apt/lists/*",
168+
)
169+
)
170+
elif option_id == "opencli":
171+
lines.extend(
172+
(
173+
"RUN apt-get update \\",
174+
" && apt-get install -y --no-install-recommends ca-certificates curl xz-utils \\",
175+
' && node_arch="$(dpkg --print-architecture)" \\',
176+
' && case "$node_arch" in amd64) node_arch=x64 ;; arm64) node_arch=arm64 ;; *) echo "Unsupported architecture: $node_arch" >&2; exit 1 ;; esac \\',
177+
' && curl --retry 5 --connect-timeout 30 -fsSL "https://nodejs.org/dist/v22.18.0/node-v22.18.0-linux-${node_arch}.tar.xz" -o /tmp/node.tar.xz \\',
178+
" && tar -xJf /tmp/node.tar.xz -C /usr/local --strip-components=1 \\",
179+
f" && npm install --global {package.package_name} \\",
180+
" && npm cache clean --force \\",
181+
" && rm -f /tmp/node.tar.xz \\",
182+
" && rm -rf /var/lib/apt/lists/*",
183+
)
184+
)
185+
elif option_id == "playwright":
186+
lines.append("RUN python -m pip install --upgrade playwright")
187+
if "chromium" not in selected_options:
188+
lines.append("RUN python -m playwright install --with-deps chromium")
189+
elif option_id == "chromium":
190+
if "playwright" not in selected_options:
191+
lines.append("RUN python -m pip install --upgrade playwright")
192+
lines.append("RUN python -m playwright install --with-deps chromium")
193+
elif package.installer == "apt":
194+
lines.extend(
195+
(
196+
"RUN apt-get update \\",
197+
f" && apt-get install -y --no-install-recommends {package.package_name} \\",
198+
" && rm -rf /var/lib/apt/lists/*",
199+
)
200+
)
201+
else:
202+
lines.append(f"RUN python -m pip install --upgrade {package.package_name}")
203+
lines.extend(("", 'CMD ["/bin/bash"]'))
204+
return "\n".join(lines)
205+
206+
207+
def validate_dockerfile(value: str) -> str:
208+
dockerfile = value.strip()
209+
if "\x00" in dockerfile:
210+
raise ValueError("Dockerfile 不能包含空字符。")
211+
if not any(
212+
line.lstrip().upper().startswith("FROM ")
213+
for line in dockerfile.splitlines()
214+
if line.strip() and not line.lstrip().startswith("#")
215+
):
216+
raise ValueError("Dockerfile 必须包含 FROM 指令。")
217+
return dockerfile
218+
219+
220+
__all__ = ["build_dockerfile", "validate_dockerfile"]

0 commit comments

Comments
 (0)